From patchwork Thu Jul 25 17:23:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Logan Gunthorpe X-Patchwork-Id: 11059375 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 71EC413A0 for ; Thu, 25 Jul 2019 17:23:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5841A2896A for ; Thu, 25 Jul 2019 17:23:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4C7BE28A20; Thu, 25 Jul 2019 17:23:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D069C28A0C for ; Thu, 25 Jul 2019 17:23:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729872AbfGYRXr (ORCPT ); Thu, 25 Jul 2019 13:23:47 -0400 Received: from ale.deltatee.com ([207.54.116.67]:39620 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729870AbfGYRXq (ORCPT ); Thu, 25 Jul 2019 13:23:46 -0400 Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hqhSv-0001JF-V8; Thu, 25 Jul 2019 11:23:45 -0600 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.89) (envelope-from ) id 1hqhSv-0001n8-0Z; Thu, 25 Jul 2019 11:23:41 -0600 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Christoph Hellwig , Sagi Grimberg , Keith Busch , Jens Axboe , Chaitanya Kulkarni , Max Gurtovoy , Stephen Bates , Logan Gunthorpe , Greg Kroah-Hartman , Alexander Viro Date: Thu, 25 Jul 2019 11:23:21 -0600 Message-Id: <20190725172335.6825-3-logang@deltatee.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190725172335.6825-1-logang@deltatee.com> References: <20190725172335.6825-1-logang@deltatee.com> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, hch@lst.de, sagi@grimberg.me, kbusch@kernel.org, axboe@fb.com, Chaitanya.Kulkarni@wdc.com, maxg@mellanox.com, sbates@raithlin.com, logang@deltatee.com, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk X-SA-Exim-Mail-From: gunthorp@deltatee.com Subject: [PATCH v6 02/16] chardev: introduce cdev_get_by_path() X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP cdev_get_by_path() attempts to retrieve a struct cdev from a path name. It is analagous to blkdev_get_by_path(). This will be necessary to create a nvme_ctrl_get_by_path()to support NVMe-OF passthru. Signed-off-by: Logan Gunthorpe Cc: Greg Kroah-Hartman Cc: Alexander Viro --- fs/char_dev.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/cdev.h | 1 + 2 files changed, 35 insertions(+) diff --git a/fs/char_dev.c b/fs/char_dev.c index 5cc53bd5f654..25037d642ff8 100644 --- a/fs/char_dev.c +++ b/fs/char_dev.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "internal.h" @@ -403,6 +404,38 @@ static struct cdev *cdev_lookup(struct inode *inode) return p; } +struct cdev *cdev_get_by_path(const char *pathname) +{ + struct inode *inode; + struct cdev *cdev; + 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); + + if (!may_open_dev(&path)) { + cdev = ERR_PTR(-EACCES); + goto out; + } + + inode = d_backing_inode(path.dentry); + if (!S_ISCHR(inode->i_mode)) { + cdev = ERR_PTR(-EINVAL); + goto out; + } + + cdev = cdev_lookup(inode); + +out: + path_put(&path); + return cdev; +} + /* * Called every time a character special file is opened */ @@ -690,5 +723,6 @@ EXPORT_SYMBOL(cdev_add); EXPORT_SYMBOL(cdev_set_parent); EXPORT_SYMBOL(cdev_device_add); EXPORT_SYMBOL(cdev_device_del); +EXPORT_SYMBOL(cdev_get_by_path); EXPORT_SYMBOL(__register_chrdev); EXPORT_SYMBOL(__unregister_chrdev); diff --git a/include/linux/cdev.h b/include/linux/cdev.h index 0e8cd6293deb..c7f2df2ca69a 100644 --- a/include/linux/cdev.h +++ b/include/linux/cdev.h @@ -24,6 +24,7 @@ void cdev_init(struct cdev *, const struct file_operations *); struct cdev *cdev_alloc(void); +struct cdev *cdev_get_by_path(const char *pathname); void cdev_put(struct cdev *p); int cdev_add(struct cdev *, dev_t, unsigned);