From patchwork Tue Apr 14 12:43:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 11487471 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7AB8B1894 for ; Tue, 14 Apr 2020 12:45:49 +0000 (UTC) Received: from aserp2120.oracle.com (aserp2120.oracle.com [141.146.126.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4813D2076B for ; Tue, 14 Apr 2020 12:45:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4813D2076B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=ocfs2-devel-bounces@oss.oracle.com Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03ECi7G7155401; Tue, 14 Apr 2020 12:45:24 GMT Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by aserp2120.oracle.com with ESMTP id 30b5um4crx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Apr 2020 12:45:24 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03ECfoci054673; Tue, 14 Apr 2020 12:45:23 GMT Received: from oss.oracle.com (oss-old-reserved.oracle.com [137.254.22.2]) by userp3030.oracle.com with ESMTP id 30bqchjgbp-1 (version=TLSv1 cipher=AES256-SHA bits=256 verify=NO); Tue, 14 Apr 2020 12:45:23 +0000 Received: from localhost ([127.0.0.1] helo=lb-oss.oracle.com) by oss.oracle.com with esmtp (Exim 4.63) (envelope-from ) id 1jOKwL-0003dM-Vq; Tue, 14 Apr 2020 05:45:21 -0700 Received: from userp3030.oracle.com ([156.151.31.80]) by oss.oracle.com with esmtp (Exim 4.63) (envelope-from ) id 1jOKvX-0003Vj-OS for ocfs2-devel@oss.oracle.com; Tue, 14 Apr 2020 05:44:31 -0700 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03ECfoEM054632 for ; Tue, 14 Apr 2020 12:44:31 GMT Received: from userp2040.oracle.com (userp2040.oracle.com [156.151.31.90]) by userp3030.oracle.com with ESMTP id 30bqchjdpg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 14 Apr 2020 12:44:31 +0000 Received: from pps.filterd (userp2040.oracle.com [127.0.0.1]) by userp2040.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03ECiNde027512 for ; Tue, 14 Apr 2020 12:44:30 GMT Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by userp2040.oracle.com with ESMTP id 30d6x0xcw6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=OK) for ; Tue, 14 Apr 2020 12:44:29 +0000 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-231-4cEvOpopOQG44m5ppjeMaw-1; Tue, 14 Apr 2020 08:44:18 -0400 X-MC-Unique: 4cEvOpopOQG44m5ppjeMaw-1 Received: by mail-wm1-f71.google.com with SMTP id s15so3723969wmc.0 for ; Tue, 14 Apr 2020 05:44:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sI5vxC1ZvKgnSHwtEppycudchqTAWMoyZNsGMD6XBfs=; b=tMpSvc79feAJ1tdvDuBXHd7/6CpuSmHRd5Yge5ND/Loag5DVfhFZqBe/MKAygxhCQa j0qUX+vYeCzNnGnDUgE+6z6E2m9bB4L7L9TEwRpYQVzmVSDQAzKpEEjSarYhJVHbseus DIAOH9zX4WRnpDUqkQp8mX/yjaXB4YI5Tw9iGkEY5xm0443+BB9i+XftsiLcTxWEYfR1 EYnzY1UfMsIXqnAAGu+LMevF2ih2D496kL7/iRdL2eEALaL+OpW2tPLnmCD8ldZO6Wgm uc6GeM3CZDnR8dO4Nme1sCYt/+CXrBDc9l+DhyAuaxQDinSSNzs2tan3sNKuPvgpjwbi PpnA== X-Gm-Message-State: AGi0PubuuCKr3BlxxLgpKUZ6qmzhdjdLIzoDHrN3IgKZ/m5py33dlPiq MqMd8kRrQUkLwR3kINZBYe1bIQbrJThRunpvoUvApyZTy1QXIxpfO11t6tGnClbEBOJWFmPtvs+ 0biijwl1wOTJuhpBa8Wlofw== X-Received: by 2002:a1c:147:: with SMTP id 68mr24228935wmb.28.1586868257524; Tue, 14 Apr 2020 05:44:17 -0700 (PDT) X-Google-Smtp-Source: APiQypJ6wN8g3wQLLN2KDP/D/IgWf7DatiJ17rYX27xAXjxyuMyzqvoWzXAAA6WPVEIYS0CMkz6Yow== X-Received: by 2002:a1c:147:: with SMTP id 68mr24228852wmb.28.1586868257170; Tue, 14 Apr 2020 05:44:17 -0700 (PDT) Received: from localhost.localdomain ([194.230.155.210]) by smtp.gmail.com with ESMTPSA id m14sm16948816wrs.76.2020.04.14.05.44.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 05:44:16 -0700 (PDT) From: Emanuele Giuseppe Esposito To: linux-nfs@vger.kernel.org Date: Tue, 14 Apr 2020 14:43:00 +0200 Message-Id: <20200414124304.4470-7-eesposit@redhat.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200414124304.4470-1-eesposit@redhat.com> References: <20200414124304.4470-1-eesposit@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Proofpoint-SPF-Result: pass X-Proofpoint-SPF-Record: v=spf1 ip4:103.23.64.2 ip4:103.23.65.2 ip4:103.23.66.26 ip4:103.23.67.26 ip4:107.21.15.141 ip4:108.177.8.0/21 ip4:13.111.0.0/16 ip4:136.147.128.0/20 ip4:136.147.176.0/20 ip4:148.105.8.0/21 ip4:149.72.0.0/16 ip4:149.96.1.26 ip4:149.96.13.2 ip4:149.96.132.2 ip4:149.96.133.2 ip4:149.96.14.2 ip4:149.96.194.2 ip4:149.96.195.2 ip4:149.96.2.26 include:spf1.redhat.com -all X-Proofpoint-SPF-VenPass: Allowed X-PDR: PASS X-Source-IP: 207.211.31.81 X-ServerName: us-smtp-1.mimecast.com X-Proofpoint-SPF-Result: pass X-Proofpoint-SPF-Record: v=spf1 ip4:103.23.64.2 ip4:103.23.65.2 ip4:103.23.66.26 ip4:103.23.67.26 ip4:107.21.15.141 ip4:108.177.8.0/21 ip4:13.111.0.0/16 ip4:136.147.128.0/20 ip4:136.147.176.0/20 ip4:148.105.8.0/21 ip4:149.72.0.0/16 ip4:149.96.1.26 ip4:149.96.13.2 ip4:149.96.132.2 ip4:149.96.133.2 ip4:149.96.14.2 ip4:149.96.194.2 ip4:149.96.195.2 ip4:149.96.2.26 include:spf1.redhat.com -all X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9590 signatures=668686 X-Proofpoint-Spam-Details: rule=tap_spam_policies_safe policy=tap_spam_policies score=0 classifier= adjust=0 reason=safe scancount=1 engine=8.12.0-2003020000 definitions=main-2004140037 X-Spam: OrgSafeList X-SpamRule: orgsafelist X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=4 mlxlogscore=999 bulkscore=0 malwarescore=0 phishscore=0 mlxscore=0 spamscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004140105 X-Mailman-Approved-At: Tue, 14 Apr 2020 05:45:19 -0700 Cc: Song Liu , linux-usb@vger.kernel.org, bpf@vger.kernel.org, "Rafael J. Wysocki" , David Airlie , Benjamin Herrenschmidt , Heiko Carstens , Alexei Starovoitov , dri-devel@lists.freedesktop.org, "J. Bruce Fields" , Hugh Dickins , Paul Mackerras , John Johansen , linux-s390@vger.kernel.org, Andrew Donnellan , Emanuele Giuseppe Esposito , Matthew Garrett , linux-efi@vger.kernel.org, Arnd Bergmann , Daniel Borkmann , Christian Borntraeger , linux-rdma@vger.kernel.org, Michael Ellerman , Anton Vorontsov , John Fastabend , James Morris , Ard Biesheuvel , Jason Gunthorpe , Doug Ledford , oprofile-list@lists.sf.net, Yonghong Song , Ian Kent , Andrii Nakryiko , Alexey Dobriyan , "Serge E. Hallyn" , netdev@vger.kernel.org, Robert Richter , Thomas Zimmermann , Vasily Gorbik , Tony Luck , Kees Cook , "James E.J. Bottomley" , autofs@vger.kernel.org, Maarten Lankhorst , Uma Krishnan , Maxime Ripard , linux-fsdevel@vger.kernel.org, "Manoj N. Kumar" , Alexander Viro , Jakub Kicinski , KP Singh , Trond Myklebust , "Matthew R. Ochs" , "David S. Miller" , Felipe Balbi , Mike Marciniszyn , Iurii Zaikin , linux-scsi@vger.kernel.org, linux-mm@kvack.org, Greg Kroah-Hartman , Dennis Dalessandro , Miklos Szeredi , linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Anna Schumaker , Luis Chamberlain , Chuck Lever , Jeremy Kerr , Daniel Vetter , Colin Cross , Frederic Barrat , Paolo Bonzini , Mike Kravetz , linuxppc-dev@lists.ozlabs.org, Martin KaFai Lau , ocfs2-devel@oss.oracle.com Subject: [Ocfs2-devel] [PATCH 6/8] simplefs: add file creation functions X-BeenThere: ocfs2-devel@oss.oracle.com X-Mailman-Version: 2.1.9 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: ocfs2-devel-bounces@oss.oracle.com Errors-To: ocfs2-devel-bounces@oss.oracle.com X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9590 signatures=668686 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 phishscore=0 mlxscore=0 spamscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004140105 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9590 signatures=668686 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 clxscore=1015 bulkscore=0 mlxscore=0 mlxlogscore=999 lowpriorityscore=0 impostorscore=0 adultscore=0 phishscore=0 spamscore=0 suspectscore=0 malwarescore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004140105 A bunch of code is duplicated between debugfs and tracefs, unify it to the simplefs library. The code is very similar, except that dentry and inode creation are unified into a single function (unlike start_creating in debugfs and tracefs, which only takes care of dentries). This adds an output parameter to the creation functions, but pushes all error recovery into fs/simplefs.c. Signed-off-by: Emanuele Giuseppe Esposito --- fs/simplefs.c | 150 +++++++++++++++++++++++++++++++++++++++ include/linux/simplefs.h | 19 +++++ 2 files changed, 169 insertions(+) diff --git a/fs/simplefs.c b/fs/simplefs.c index c59eb8d996be..3e48a288beb3 100644 --- a/fs/simplefs.c +++ b/fs/simplefs.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include #include +#include +#include static DEFINE_SPINLOCK(pin_fs_lock); @@ -42,3 +44,151 @@ struct inode *simple_alloc_anon_inode(struct simple_fs *fs) return alloc_anon_inode(fs->mount->mnt_sb); } EXPORT_SYMBOL(simple_alloc_anon_inode); + +static struct dentry *failed_creating(struct simple_fs *fs, struct dentry *dentry) +{ + inode_unlock(d_inode(dentry->d_parent)); + dput(dentry); + simple_release_fs(fs); + return ERR_PTR(-ENOMEM); +} + +struct dentry *simplefs_create_dentry(struct simple_fs *fs, struct file_system_type *type, + const char *name, struct dentry *parent, + struct inode **inode) +{ + struct dentry *dentry; + int error; + + pr_debug("creating file '%s'\n", name); + + if (IS_ERR(parent)) + return parent; + + error = simple_pin_fs(fs, type); + if (error) { + pr_err("Unable to pin filesystem for file '%s'\n", name); + return ERR_PTR(error); + } + + /* If the parent is not specified, we create it in the root. + * We need the root dentry to do this, which is in the super + * block. A pointer to that is in the struct vfsmount that we + * have around. + */ + if (!parent) + parent = fs->mount->mnt_root; + + inode_lock(d_inode(parent)); + dentry = lookup_one_len(name, parent, strlen(name)); + if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { + if (d_is_dir(dentry)) + pr_err("Directory '%s' with parent '%s' already present!\n", + name, parent->d_name.name); + else + pr_err("File '%s' in directory '%s' already present!\n", + name, parent->d_name.name); + dput(dentry); + dentry = ERR_PTR(-EEXIST); + } + + if (IS_ERR(dentry)) { + inode_unlock(d_inode(parent)); + simple_release_fs(fs); + } + + + if (IS_ERR(dentry)) + return dentry; + + *inode = simple_new_inode(fs->mount->mnt_sb); + if (unlikely(!(*inode))) { + pr_err("out of free inodes, can not create file '%s'\n", + name); + return failed_creating(fs, dentry); + } + + return dentry; +} +EXPORT_SYMBOL(simplefs_create_dentry); + +struct dentry *simplefs_create_file(struct simple_fs *fs, struct file_system_type *type, + const char *name, umode_t mode, + struct dentry *parent, void *data, + struct inode **inode) +{ + struct dentry *dentry; + + WARN_ON((mode & S_IFMT) && !S_ISREG(mode)); + mode |= S_IFREG; + + dentry = simplefs_create_dentry(fs, type, name, parent, inode); + + if (IS_ERR(dentry)) + return dentry; + + (*inode)->i_mode = mode; + (*inode)->i_private = data; + + return dentry; +} +EXPORT_SYMBOL(simplefs_create_file); + +struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode) +{ + d_instantiate(dentry, inode); + if (S_ISDIR(inode->i_mode)) { + inc_nlink(d_inode(dentry->d_parent)); + fsnotify_mkdir(d_inode(dentry->d_parent), dentry); + } else { + fsnotify_create(d_inode(dentry->d_parent), dentry); + } + inode_unlock(d_inode(dentry->d_parent)); + return dentry; +} +EXPORT_SYMBOL(simplefs_finish_dentry); + +struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type, + const char *name, umode_t mode, struct dentry *parent, + struct inode **inode) +{ + struct dentry *dentry; + + WARN_ON((mode & S_IFMT) && !S_ISDIR(mode)); + mode |= S_IFDIR; + + dentry = simplefs_create_dentry(fs, type, name, parent, inode); + if (IS_ERR(dentry)) + return dentry; + + (*inode)->i_mode = mode; + (*inode)->i_op = &simple_dir_inode_operations; + (*inode)->i_fop = &simple_dir_operations; + + /* directory inodes start off with i_nlink == 2 (for "." entry) */ + inc_nlink(*inode); + return dentry; +} +EXPORT_SYMBOL(simplefs_create_dir); + +struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type, + const char *name, struct dentry *parent, + const char *target, struct inode **inode) +{ + struct dentry *dentry; + char *link = kstrdup(target, GFP_KERNEL); + if (!link) + return ERR_PTR(-ENOMEM); + + dentry = simplefs_create_dentry(fs, type, name, parent, inode); + if (IS_ERR(dentry)) { + kfree_link(link); + return dentry; + } + + (*inode)->i_mode = S_IFLNK | S_IRWXUGO; + (*inode)->i_link = link; + (*inode)->i_op = &simple_symlink_inode_operations; + return dentry; +} +EXPORT_SYMBOL(simplefs_create_symlink); diff --git a/include/linux/simplefs.h b/include/linux/simplefs.h index c62ab526414e..cc53eed0bc3d 100644 --- a/include/linux/simplefs.h +++ b/include/linux/simplefs.h @@ -14,4 +14,23 @@ extern void simple_release_fs(struct simple_fs *); extern struct inode *simple_alloc_anon_inode(struct simple_fs *fs); +extern struct dentry *simplefs_create_dentry(struct simple_fs *fs, + struct file_system_type *type, + const char *name, struct dentry *parent, + struct inode **inode); +struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode); + +extern struct dentry *simplefs_create_file(struct simple_fs *fs, + struct file_system_type *type, + const char *name, umode_t mode, + struct dentry *parent, void *data, + struct inode **inode); +extern struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type, + const char *name, umode_t mode, struct dentry *parent, + struct inode **inode); +extern struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type, + const char *name, struct dentry *parent, + const char *target, struct inode **inode); + + #endif