Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp291609ybt; Wed, 17 Jun 2020 00:41:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywz2uHtufOXH04fmYb0pmLs0tqQTQQha9uG0LbJ2I8/XVLVi/w3qkVORBWt4mxzT0Az2No X-Received: by 2002:aa7:c6c7:: with SMTP id b7mr6073556eds.213.1592379669482; Wed, 17 Jun 2020 00:41:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592379669; cv=none; d=google.com; s=arc-20160816; b=ZuhwVwolcMu+330cmdeHOKk5f6tt0Rm2UlMwH/q6csPX1/BVi2LMMYZY+e2LaP6wp5 TFKnJ0nM0y0UXiW8Y7yN20/VEssAjgqMGtahgBH/P+808+zLcEitVpLAMrnUEF5HAH2Q aRZZUvXf3IEif8kz0YIROEYWYn5a491t+/BXO9dsMBY+SckSQSrZtK7XIOiHb7d+pbQC R1UowS9cf11lvs0LnAPoiFVuEDh2jRU+wi1jGKT9JJ5NiEqDG1Hhhedkbi2MG77iJsKu Er6pnWoXP+MTzds/cFlijcTNJibeGvtaE52yJQiNYO2T/X2p+oZ2b440iPfY++SPzC3B i+Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature:dkim-signature; bh=YP81i3I+Z4aS6pPC7VYSp+SNREBPRhMa7fFT8ZR5kf8=; b=Vrz/W3AUwgTrzBrdue4U4i2xkUEh+ZnEnnATPX1yLmLs1eWx/jHR7PjSNVDjmeVHup EK8KcG4Q/gwNPCdQoBSoWyHcC+yp0ErH8TjhYThbi74DB/5InW7nmMeQORI+T8DWoo+f cJY+UOd66xRblSqXJPlEU71hC1k85Cl7vAqy6YAfHlahMtbuYui5p7LVQlDQRwXuVdIp ZAelkH+hNiBJOXEsbtdBcvqIm1aFLmbZX/m9D/kZCe58x1LCjmv0rHpa2RuFTLRaKyQB MZsKCq05coYaOOG69XV83ZfqE04zhQcsFpv2jPcyweeIHz8hvHj3BPo7hmucYG1G4YxI H+bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@themaw.net header.s=fm3 header.b=VARnqolE; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=kA62n4dM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b5si14779374edz.386.2020.06.17.00.40.47; Wed, 17 Jun 2020 00:41:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@themaw.net header.s=fm3 header.b=VARnqolE; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=kA62n4dM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726890AbgFQHia (ORCPT + 99 others); Wed, 17 Jun 2020 03:38:30 -0400 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:43099 "EHLO wout5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726769AbgFQHiI (ORCPT ); Wed, 17 Jun 2020 03:38:08 -0400 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id D04F1561; Wed, 17 Jun 2020 03:38:06 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 17 Jun 2020 03:38:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=themaw.net; h= subject:from:to:cc:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; s=fm3; bh= YP81i3I+Z4aS6pPC7VYSp+SNREBPRhMa7fFT8ZR5kf8=; b=VARnqolEz9kMpyOG rueMBTTc+omAW2E/8JihoNv6+SbQLONF4FMTPJhJzGUjCqPFVvgTtZklKCqXuKQH sao7DxZ/XzN70CRTPA4E4bnihrSsTnOOIekMaob8Kp/iVRStsBGraheOmaT7Shij E2oMFstbgp00D+43D+GzXGoFCCP4ZW+v9Ejjg0RjLeg2HecT1MBulnu37pbCEi7Q aveHGFplNn/At35OKw02tWbRBE1KLjGR2XACHGKDsn+FJjU71RjR3x7pIkVa5Fis OPsVsMPv+PWwWLkrQAkF4XEl0yK87QYQ65mNLiGKQ6HfHfhOyBZfwq+pNTdpYmk6 Ncy7RA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=YP81i3I+Z4aS6pPC7VYSp+SNREBPRhMa7fFT8ZR5k f8=; b=kA62n4dMK9q8KoCq0Z/fLzeUfL/w+UQpRJXyfpc9mpEclDNnJO5790ci1 sle1qA3nJDbJmmv6h/EyIeivRAfB2Ojg4FjP3UhE3Ug9g5LNKRhB7SaOEs9iYc5g 9BS3u0SQ8s5wQzb5snW//h3SN9xqTQ0h0CnNZcih2lX2k51leP8zb5jQXast5Slk 5HOBBME9yEJXmHyaai5/Kk324Nxye1TngWDKxqLztPnUMYwQv7/mQukwpiMY/XfP MyJ5Aqj6o1C/NLvDN+5UUIk0eGdJbbKApq1euafVZD80efG3+2ma2Jj3IHGXqaA1 MD0I5gGbGC1OWOQpG17pQz8z0yqXA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrudejuddguddvhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuhffvfffkjghffgggtgfgsehtjedttddtreejnecuhfhrohhmpefkrghn ucfmvghnthcuoehrrghvvghnsehthhgvmhgrfidrnhgvtheqnecuggftrfgrthhtvghrnh epvddvvdfgleefhfelgfekvdejjefhvdetfeevueeggeefhfdujeegveejveejgfdunecu kfhppeehkedrjedrudelgedrkeejnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrg hmpehmrghilhhfrhhomheprhgrvhgvnhesthhhvghmrgifrdhnvght X-ME-Proxy: Received: from mickey.themaw.net (58-7-194-87.dyn.iinet.net.au [58.7.194.87]) by mail.messagingengine.com (Postfix) with ESMTPA id EC8ED328005D; Wed, 17 Jun 2020 03:38:05 -0400 (EDT) Received: from mickey.themaw.net (localhost [127.0.0.1]) by mickey.themaw.net (Postfix) with ESMTP id 79164A0314; Wed, 17 Jun 2020 15:38:03 +0800 (AWST) Subject: [PATCH v2 4/6] kernfs: use revision to identify directory node changes From: Ian Kent To: Greg Kroah-Hartman Cc: Tejun Heo , Stephen Rothwell , Andrew Morton , Al Viro , Rick Lindsley , David Howells , Miklos Szeredi , linux-fsdevel , Kernel Mailing List Date: Wed, 17 Jun 2020 15:38:03 +0800 Message-ID: <159237948345.89469.9839924207092477321.stgit@mickey.themaw.net> In-Reply-To: <159237905950.89469.6559073274338175600.stgit@mickey.themaw.net> References: <159237905950.89469.6559073274338175600.stgit@mickey.themaw.net> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a kernfs directory node hasn't changed there's no need to search for an added (or removed) child dentry. Add a revision counter to kernfs directory nodes so it can be used to detect if a directory node has changed. Signed-off-by: Ian Kent --- fs/kernfs/dir.c | 17 +++++++++++++++-- fs/kernfs/kernfs-internal.h | 24 ++++++++++++++++++++++++ include/linux/kernfs.h | 5 +++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index f4943329e578..03f4f179bbc4 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -383,6 +383,7 @@ static int kernfs_link_sibling(struct kernfs_node *kn) /* successfully added, account subdir number */ if (kernfs_type(kn) == KERNFS_DIR) kn->parent->dir.subdirs++; + kernfs_inc_rev(kn->parent); return 0; } @@ -405,6 +406,7 @@ static bool kernfs_unlink_sibling(struct kernfs_node *kn) if (kernfs_type(kn) == KERNFS_DIR) kn->parent->dir.subdirs--; + kernfs_inc_rev(kn->parent); rb_erase(&kn->rb, &kn->parent->dir.children); RB_CLEAR_NODE(&kn->rb); @@ -1044,9 +1046,16 @@ struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent, static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) { + struct kernfs_node *parent; struct kernfs_node *kn; if (flags & LOOKUP_RCU) { + /* Directory node changed? */ + parent = kernfs_dentry_node(dentry->d_parent); + + if (!kernfs_dir_changed(parent, dentry)) + return 1; + kn = kernfs_dentry_node(dentry); if (!kn) { /* Negative hashed dentry, tell the VFS to switch to @@ -1093,8 +1102,6 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) kn = kernfs_dentry_node(dentry); if (!kn) { - struct kernfs_node *parent; - /* If the kernfs node can be found this is a stale negative * hashed dentry so it must be discarded and the lookup redone. */ @@ -1102,6 +1109,10 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) if (parent) { const void *ns = NULL; + /* Directory node changed? */ + if (kernfs_dir_changed(parent, dentry)) + goto out_bad; + if (kernfs_ns_enabled(parent)) ns = kernfs_info(dentry->d_parent->d_sb)->ns; kn = kernfs_find_ns(parent, dentry->d_name.name, ns); @@ -1156,6 +1167,8 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, down_read(&kernfs_rwsem); + kernfs_set_rev(dentry, parent); + if (kernfs_ns_enabled(parent)) ns = kernfs_info(dir->i_sb)->ns; diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 097c1a989aa4..a7b0e2074260 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -82,6 +82,30 @@ static inline struct kernfs_node *kernfs_dentry_node(struct dentry *dentry) return d_inode(dentry)->i_private; } +static inline void kernfs_set_rev(struct dentry *dentry, + struct kernfs_node *kn) +{ + dentry->d_time = kn->dir.rev; +} + +static inline void kernfs_inc_rev(struct kernfs_node *kn) +{ + if (kernfs_type(kn) == KERNFS_DIR) { + if (!++kn->dir.rev) + kn->dir.rev++; + } +} + +static inline bool kernfs_dir_changed(struct kernfs_node *kn, + struct dentry *dentry) +{ + if (kernfs_type(kn) == KERNFS_DIR) { + if (kn->dir.rev != dentry->d_time) + return true; + } + return false; +} + extern const struct super_operations kernfs_sops; extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache; diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 89f6a4214a70..74727d98e380 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -98,6 +98,11 @@ struct kernfs_elem_dir { * better directly in kernfs_node but is here to save space. */ struct kernfs_root *root; + /* + * Monotonic revision counter, used to identify if a directory + * node has changed during revalidation. + */ + unsigned long rev; }; struct kernfs_elem_symlink {