From patchwork Tue Jul 21 08:37:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 498070 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E8E51140E0F for ; Tue, 21 Jul 2015 18:45:35 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZHTA6-0003WD-8i; Tue, 21 Jul 2015 08:44:30 +0000 Received: from [59.151.112.132] (helo=heian.cn.fujitsu.com) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZHT9V-0002qj-7L for linux-mtd@lists.infradead.org; Tue, 21 Jul 2015 08:43:54 +0000 X-IronPort-AV: E=Sophos;i="5.13,665,1427731200"; d="scan'208";a="98671198" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 21 Jul 2015 16:47:14 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t6L8fiLL004999; Tue, 21 Jul 2015 16:41:44 +0800 Received: from yds-PC.g08.fujitsu.local (10.167.226.66) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Tue, 21 Jul 2015 16:43:31 +0800 From: Dongsheng Yang To: , , Subject: [PATCH 05/25] fs: char_dev: introduce lookup_cdev function to find cdev by name Date: Tue, 21 Jul 2015 16:37:36 +0800 Message-ID: <1437467876-22106-6-git-send-email-yangds.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1437467876-22106-1-git-send-email-yangds.fnst@cn.fujitsu.com> References: <1437467876-22106-1-git-send-email-yangds.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.66] X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150721_014353_446073_5CD235BC X-CRM114-Status: GOOD ( 11.97 ) X-Spam-Score: -1.1 (-) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-1.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.8 RDNS_NONE Delivered to internal network by a host with no rDNS X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fsdevel@vger.kernel.org, Dongsheng Yang , linux-mtd@lists.infradead.org Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Function lookup_cdev works similar with lookup_bdev, we can get a cdev instance by lookup_cdev with a parameter of dev name. This function will be used in quotactl to get a cdev by a dev name. Signed-off-by: Dongsheng Yang --- fs/char_dev.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 74 insertions(+) diff --git a/fs/char_dev.c b/fs/char_dev.c index ea06a3d..899f08b 100644 --- a/fs/char_dev.c +++ b/fs/char_dev.c @@ -22,6 +22,9 @@ #include #include +#include +#include + #include "internal.h" static struct kobj_map *cdev_map; @@ -439,6 +442,74 @@ static int exact_lock(dev_t dev, void *data) return cdev_get(p) ? 0 : -1; } +static struct cdev *cd_acquire(struct inode *inode) +{ + struct cdev *cdev; + struct cdev *new = NULL; + + spin_lock(&cdev_lock); + cdev = inode->i_cdev; + if (!cdev) { + struct kobject *kobj; + int idx; + spin_unlock(&cdev_lock); + kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx); + if (!kobj) { + cdev = NULL; + goto out; + } + new = container_of(kobj, struct cdev, kobj); + spin_lock(&cdev_lock); + /* Check i_cdev again in case somebody beat us to it while + we dropped the lock. */ + cdev = inode->i_cdev; + if (!cdev) { + inode->i_cdev = cdev = new; + list_add(&inode->i_devices, &cdev->list); + } + } + + if (!cdev_get(cdev)) + cdev = NULL; + spin_unlock(&cdev_lock); + +out: + return cdev; +} + +struct cdev *lookup_cdev(const char *pathname) +{ + struct cdev *cdev; + struct inode *inode; + struct path path; + int error; + + if (!pathname || !*pathname) + return ERR_PTR(-EINVAL); + + error = kern_path(pathname, LOOKUP_FOLLOW, &path); + if (error) + return ERR_PTR(error); + + inode = d_backing_inode(path.dentry); + error = -ENODEV; + if (!S_ISCHR(inode->i_mode)) + goto fail; + error = -EACCES; + if (path.mnt->mnt_flags & MNT_NODEV) + goto fail; + error = -ENXIO; + cdev = cd_acquire(inode); + if (!cdev) + goto fail; +out: + path_put(&path); + return cdev; +fail: + cdev = ERR_PTR(error); + goto out; +} + /** * cdev_add() - add a char device to the system * @p: the cdev structure for the device @@ -561,6 +632,7 @@ void __init chrdev_init(void) EXPORT_SYMBOL(register_chrdev_region); EXPORT_SYMBOL(unregister_chrdev_region); EXPORT_SYMBOL(alloc_chrdev_region); +EXPORT_SYMBOL(lookup_cdev); EXPORT_SYMBOL(cdev_init); EXPORT_SYMBOL(cdev_alloc); EXPORT_SYMBOL(cdev_del); diff --git a/include/linux/fs.h b/include/linux/fs.h index 5c7d789..860b235 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2308,6 +2308,8 @@ extern void __unregister_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name); extern void unregister_chrdev_region(dev_t, unsigned); extern void chrdev_show(struct seq_file *,off_t); +extern struct cdev *lookup_cdev(const char *); + static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)