Skip to contentSkip to conversation

fs/nfsd/nfs4recover.c:406 purge_old() error: uninitialized symbol 'child'.

PATCH In review kernel test robot 4 participants 11 messages Sep 13, 2025 – Sep 17, 2025 NFS server

lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw

BCC: [email protected]
CC: [email protected]
TO: NeilBrown <[email protected]>
CC: Chuck Lever <[email protected]>
CC: Jeff Layton <[email protected]>

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux nfsd-testing
head:   454e99ac23dc63c1472eb5df8f97c961c0f178c9
commit: 30424fb94b7bea14e27922669924948d6320e991 [45/50] nfsd: move name lookup out of nfsd4_list_rec_dir()

:::::: branch date: 19 hours ago
:::::: commit date: 5 days ago
config: sparc64-randconfig-r072-20250913 (https://download.01.org/0day-ci/archive/20250913/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 15.1.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/nfsd/nfs4recover.c:406 purge_old() error: uninitialized symbol 'child'.

vim +/child +406 fs/nfsd/nfs4recover.c

c7b9a45927e74c8 NeilBrown         2005-06-23  397  
c7b9a45927e74c8 NeilBrown         2005-06-23  398  static int
30424fb94b7bea1 NeilBrown         2025-09-08  399  purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
c7b9a45927e74c8 NeilBrown         2005-06-23  400  {
c7b9a45927e74c8 NeilBrown         2005-06-23  401  	int status;
30424fb94b7bea1 NeilBrown         2025-09-08  402  	struct dentry *child;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  403  	struct xdr_netobj name;
c7b9a45927e74c8 NeilBrown         2005-06-23  404  
30424fb94b7bea1 NeilBrown         2025-09-08  405  	if (strlen(cname) != HEXDIR_LEN - 1) {
6b1891052a3f8e3 Scott Mayhew      2019-03-26 @406  		printk("%s: illegal name %pd in recovery directory\n",
6b1891052a3f8e3 Scott Mayhew      2019-03-26  407  				__func__, child);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  408  		/* Keep trying; maybe the others are OK: */
b37ad28bcaa7c48 Al Viro           2006-10-19  409  		return 0;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  410  	}
30424fb94b7bea1 NeilBrown         2025-09-08  411  	name.data = kstrdup(cname, GFP_KERNEL);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  412  	if (!name.data) {
6b1891052a3f8e3 Scott Mayhew      2019-03-26  413  		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8e3 Scott Mayhew      2019-03-26  414  			__func__);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  415  		goto out;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  416  	}
6b1891052a3f8e3 Scott Mayhew      2019-03-26  417  	name.len = HEXDIR_LEN;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  418  	if (nfs4_has_reclaimed_state(name, nn))
6b1891052a3f8e3 Scott Mayhew      2019-03-26  419  		goto out_free;
c7b9a45927e74c8 NeilBrown         2005-06-23  420  
30424fb94b7bea1 NeilBrown         2025-09-08  421  	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
30424fb94b7bea1 NeilBrown         2025-09-08  422  	child = lookup_one(&nop_mnt_idmap, &QSTR(cname), parent);
30424fb94b7bea1 NeilBrown         2025-09-08  423  	if (!IS_ERR(child)) {
abf08576afe3150 Christian Brauner 2023-01-13  424  		status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
c7b9a45927e74c8 NeilBrown         2005-06-23  425  		if (status)
a6a9f18f0a9f943 Al Viro           2013-09-16  426  			printk("failed to remove client recovery directory %pd\n",
a6a9f18f0a9f943 Al Viro           2013-09-16  427  			       child);
30424fb94b7bea1 NeilBrown         2025-09-08  428  		dput(child);
30424fb94b7bea1 NeilBrown         2025-09-08  429  	}
30424fb94b7bea1 NeilBrown         2025-09-08  430  	inode_unlock(d_inode(parent));
30424fb94b7bea1 NeilBrown         2025-09-08  431  
6b1891052a3f8e3 Scott Mayhew      2019-03-26  432  out_free:
6b1891052a3f8e3 Scott Mayhew      2019-03-26  433  	kfree(name.data);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  434  out:
c7b9a45927e74c8 NeilBrown         2005-06-23  435  	/* Keep trying, success or failure: */
b37ad28bcaa7c48 Al Viro           2006-10-19  436  	return 0;
c7b9a45927e74c8 NeilBrown         2005-06-23  437  }
c7b9a45927e74c8 NeilBrown         2005-06-23  438  

:::::: The code at line 406 was first introduced by commit
:::::: 6b1891052a3f8e3c3217e8512bbed2fd6252977b nfsd: make nfs4_client_reclaim use an xdr_netobj instead of a fixed char array

:::::: TO: Scott Mayhew <[email protected]>
:::::: CC: J. Bruce Fields <[email protected]>

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw
Quoted 28 lines · Dan Carpenter
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux nfsd-testing
head:   454e99ac23dc63c1472eb5df8f97c961c0f178c9
commit: 30424fb94b7bea14e27922669924948d6320e991 [45/50] nfsd: move name lookup out of nfsd4_list_rec_dir()
config: sparc64-randconfig-r072-20250913 (https://download.01.org/0day-ci/archive/20250913/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 15.1.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/nfsd/nfs4recover.c:406 purge_old() error: uninitialized symbol 'child'.

vim +/child +406 fs/nfsd/nfs4recover.c

c7b9a45927e74c8 NeilBrown         2005-06-23  398  static int
30424fb94b7bea1 NeilBrown         2025-09-08  399  purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
c7b9a45927e74c8 NeilBrown         2005-06-23  400  {
c7b9a45927e74c8 NeilBrown         2005-06-23  401  	int status;
30424fb94b7bea1 NeilBrown         2025-09-08  402  	struct dentry *child;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  403  	struct xdr_netobj name;
c7b9a45927e74c8 NeilBrown         2005-06-23  404  
30424fb94b7bea1 NeilBrown         2025-09-08  405  	if (strlen(cname) != HEXDIR_LEN - 1) {
6b1891052a3f8e3 Scott Mayhew      2019-03-26 @406  		printk("%s: illegal name %pd in recovery directory\n",
6b1891052a3f8e3 Scott Mayhew      2019-03-26  407  				__func__, child);
                                                                                          ^^^^^
Quoted 1 line

s/child/cname/

Also s/%pd/%s/

Chuck: do you want to just edit the patch, or should I recend?

Thanks Dan.

NeilBrown

Quoted 37 lines
6b1891052a3f8e3 Scott Mayhew      2019-03-26  408  		/* Keep trying; maybe the others are OK: */
b37ad28bcaa7c48 Al Viro           2006-10-19  409  		return 0;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  410  	}
30424fb94b7bea1 NeilBrown         2025-09-08  411  	name.data = kstrdup(cname, GFP_KERNEL);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  412  	if (!name.data) {
6b1891052a3f8e3 Scott Mayhew      2019-03-26  413  		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8e3 Scott Mayhew      2019-03-26  414  			__func__);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  415  		goto out;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  416  	}
6b1891052a3f8e3 Scott Mayhew      2019-03-26  417  	name.len = HEXDIR_LEN;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  418  	if (nfs4_has_reclaimed_state(name, nn))
6b1891052a3f8e3 Scott Mayhew      2019-03-26  419  		goto out_free;
c7b9a45927e74c8 NeilBrown         2005-06-23  420  
30424fb94b7bea1 NeilBrown         2025-09-08  421  	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
30424fb94b7bea1 NeilBrown         2025-09-08  422  	child = lookup_one(&nop_mnt_idmap, &QSTR(cname), parent);
30424fb94b7bea1 NeilBrown         2025-09-08  423  	if (!IS_ERR(child)) {
abf08576afe3150 Christian Brauner 2023-01-13  424  		status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
c7b9a45927e74c8 NeilBrown         2005-06-23  425  		if (status)
a6a9f18f0a9f943 Al Viro           2013-09-16  426  			printk("failed to remove client recovery directory %pd\n",
a6a9f18f0a9f943 Al Viro           2013-09-16  427  			       child);
30424fb94b7bea1 NeilBrown         2025-09-08  428  		dput(child);
30424fb94b7bea1 NeilBrown         2025-09-08  429  	}
30424fb94b7bea1 NeilBrown         2025-09-08  430  	inode_unlock(d_inode(parent));
30424fb94b7bea1 NeilBrown         2025-09-08  431  
6b1891052a3f8e3 Scott Mayhew      2019-03-26  432  out_free:
6b1891052a3f8e3 Scott Mayhew      2019-03-26  433  	kfree(name.data);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  434  out:
c7b9a45927e74c8 NeilBrown         2005-06-23  435  	/* Keep trying, success or failure: */
b37ad28bcaa7c48 Al Viro           2006-10-19  436  	return 0;
c7b9a45927e74c8 NeilBrown         2005-06-23  437  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
Quoted 31 lines · NeilBrown
Quoted 28 lines · Dan Carpenter
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux nfsd-testing
head:   454e99ac23dc63c1472eb5df8f97c961c0f178c9
commit: 30424fb94b7bea14e27922669924948d6320e991 [45/50] nfsd: move name lookup out of nfsd4_list_rec_dir()
config: sparc64-randconfig-r072-20250913 (https://download.01.org/0day-ci/archive/20250913/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 15.1.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/nfsd/nfs4recover.c:406 purge_old() error: uninitialized symbol 'child'.

vim +/child +406 fs/nfsd/nfs4recover.c

c7b9a45927e74c8 NeilBrown         2005-06-23  398  static int
30424fb94b7bea1 NeilBrown         2025-09-08  399  purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
c7b9a45927e74c8 NeilBrown         2005-06-23  400  {
c7b9a45927e74c8 NeilBrown         2005-06-23  401  	int status;
30424fb94b7bea1 NeilBrown         2025-09-08  402  	struct dentry *child;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  403  	struct xdr_netobj name;
c7b9a45927e74c8 NeilBrown         2005-06-23  404  
30424fb94b7bea1 NeilBrown         2025-09-08  405  	if (strlen(cname) != HEXDIR_LEN - 1) {
6b1891052a3f8e3 Scott Mayhew      2019-03-26 @406  		printk("%s: illegal name %pd in recovery directory\n",
6b1891052a3f8e3 Scott Mayhew      2019-03-26  407  				__func__, child);
                                                                                          ^^^^^
Quoted 1 line

s/child/cname/

Quoted 1 line

Also s/%pd/%s/

Indeed.

Quoted 1 line

Chuck: do you want to just edit the patch, or should I recend?

Yeah, let's get the corrected patch archived on lore. Please resend.

Quoted 43 lines

Thanks Dan.

NeilBrown

Quoted 37 lines
6b1891052a3f8e3 Scott Mayhew      2019-03-26  408  		/* Keep trying; maybe the others are OK: */
b37ad28bcaa7c48 Al Viro           2006-10-19  409  		return 0;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  410  	}
30424fb94b7bea1 NeilBrown         2025-09-08  411  	name.data = kstrdup(cname, GFP_KERNEL);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  412  	if (!name.data) {
6b1891052a3f8e3 Scott Mayhew      2019-03-26  413  		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8e3 Scott Mayhew      2019-03-26  414  			__func__);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  415  		goto out;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  416  	}
6b1891052a3f8e3 Scott Mayhew      2019-03-26  417  	name.len = HEXDIR_LEN;
6b1891052a3f8e3 Scott Mayhew      2019-03-26  418  	if (nfs4_has_reclaimed_state(name, nn))
6b1891052a3f8e3 Scott Mayhew      2019-03-26  419  		goto out_free;
c7b9a45927e74c8 NeilBrown         2005-06-23  420  
30424fb94b7bea1 NeilBrown         2025-09-08  421  	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
30424fb94b7bea1 NeilBrown         2025-09-08  422  	child = lookup_one(&nop_mnt_idmap, &QSTR(cname), parent);
30424fb94b7bea1 NeilBrown         2025-09-08  423  	if (!IS_ERR(child)) {
abf08576afe3150 Christian Brauner 2023-01-13  424  		status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
c7b9a45927e74c8 NeilBrown         2005-06-23  425  		if (status)
a6a9f18f0a9f943 Al Viro           2013-09-16  426  			printk("failed to remove client recovery directory %pd\n",
a6a9f18f0a9f943 Al Viro           2013-09-16  427  			       child);
30424fb94b7bea1 NeilBrown         2025-09-08  428  		dput(child);
30424fb94b7bea1 NeilBrown         2025-09-08  429  	}
30424fb94b7bea1 NeilBrown         2025-09-08  430  	inode_unlock(d_inode(parent));
30424fb94b7bea1 NeilBrown         2025-09-08  431  
6b1891052a3f8e3 Scott Mayhew      2019-03-26  432  out_free:
6b1891052a3f8e3 Scott Mayhew      2019-03-26  433  	kfree(name.data);
6b1891052a3f8e3 Scott Mayhew      2019-03-26  434  out:
c7b9a45927e74c8 NeilBrown         2005-06-23  435  	/* Keep trying, success or failure: */
b37ad28bcaa7c48 Al Viro           2006-10-19  436  	return 0;
c7b9a45927e74c8 NeilBrown         2005-06-23  437  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Signature · 1 line
Chuck Lever
#3 lore raw

PATCH v2 1/1 nfsd: change nfs4_client_to_reclaim() to allocate data

The calling convention for nfs4_client_to_reclaim() is clumsy in that the caller need to free memory if the function fails. It is much cleaner if the function frees its own memory.

This patch changes nfs4_client_to_reclaim() to re-allocate the .data fields to be stored in the newly allocated struct nfs4_client_reclaim, and to free everything on failure.

__cld_pipe_inprogress_downcall() needs to allocate the data anyway to copy it from user-space, so now that data is allocated twice. I think that is a small price to pay for a cleaner interface.

---

This version fixes the problem Dan found with "child" being used uninitialised in "purge_old()".

 fs/nfsd/nfs4recover.c | 71 +++++++++++++++----------------------------
 fs/nfsd/nfs4state.c   | 22 ++++++++++++--
 2 files changed, 44 insertions(+), 49 deletions(-)
fs/nfsd/nfs4recover.c +25 -46
index 990fe3d958ed..b1005abcb903 100644
@@ -147,24 +147,13 @@ legacy_recdir_name_error(struct nfs4_client *clp, int error)
 
 static void
 __nfsd4_create_reclaim_record_grace(struct nfs4_client *clp,
-		const char *dname, int len, struct nfsd_net *nn)
+				    char *dname, struct nfsd_net *nn)
 {
-	struct xdr_netobj name;
+	struct xdr_netobj name = { .len = strlen(dname), .data = dname };
 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
 	struct nfs4_client_reclaim *crp;
 
-	name.data = kmemdup(dname, len, GFP_KERNEL);
-	if (!name.data) {
-		dprintk("%s: failed to allocate memory for name.data!\n",
-			__func__);
-		return;
-	}
-	name.len = len;
 	crp = nfs4_client_to_reclaim(name, princhash, nn);
-	if (!crp) {
-		kfree(name.data);
-		return;
-	}
 	crp->cr_clp = clp;
 }
 
@@ -223,8 +212,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
 	inode_unlock(d_inode(dir));
 	if (status == 0) {
 		if (nn->in_grace)
-			__nfsd4_create_reclaim_record_grace(clp, dname,
-					HEXDIR_LEN, nn);
+			__nfsd4_create_reclaim_record_grace(clp, dname, nn);
 		vfs_fsync(nn->rec_file, 0);
 	} else {
 		printk(KERN_ERR "NFSD: failed to write recovery record"
@@ -403,8 +391,8 @@ purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
 	struct xdr_netobj name;
 
 	if (strlen(cname) != HEXDIR_LEN - 1) {
-		printk("%s: illegal name %pd in recovery directory\n",
-				__func__, child);
+		printk("%s: illegal name %s in recovery directory\n",
+				__func__, cname);
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}
@@ -461,7 +449,7 @@ nfsd4_recdir_purge_old(struct nfsd_net *nn)
 static int
 load_recdir(struct dentry *parent, char *cname, struct nfsd_net *nn)
 {
-	struct xdr_netobj name;
+	struct xdr_netobj name = { .len = HEXDIR_LEN, .data = cname };
 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
 
 	if (strlen(cname) != HEXDIR_LEN - 1) {
@@ -470,16 +458,7 @@ load_recdir(struct dentry *parent, char *cname, struct nfsd_net *nn)
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}
-	name.data = kstrdup(cname, GFP_KERNEL);
-	if (!name.data) {
-		dprintk("%s: failed to allocate memory for name.data!\n",
-			__func__);
-		goto out;
-	}
-	name.len = HEXDIR_LEN;
-	if (!nfs4_client_to_reclaim(name, princhash, nn))
-		kfree(name.data);
-out:
+	nfs4_client_to_reclaim(name, princhash, nn);
 	return 0;
 }
 
@@ -777,6 +756,8 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 {
 	uint8_t cmd, princhashlen;
 	struct xdr_netobj name, princhash = { .len = 0, .data = NULL };
+	char *namecopy __free(kfree) = NULL;
+	char *princhashcopy __free(kfree) = NULL;
 	uint16_t namelen;
 
 	if (get_user(cmd, &cmsg->cm_cmd)) {
@@ -794,19 +775,19 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 				dprintk("%s: invalid namelen (%u)", __func__, namelen);
 				return -EINVAL;
 			}
-			name.data = memdup_user(&ci->cc_name.cn_id, namelen);
-			if (IS_ERR(name.data))
-				return PTR_ERR(name.data);
+			namecopy = memdup_user(&ci->cc_name.cn_id, namelen);
+			if (IS_ERR(namecopy))
+				return PTR_ERR(namecopy);
+			name.data = namecopy;
 			name.len = namelen;
 			get_user(princhashlen, &ci->cc_princhash.cp_len);
 			if (princhashlen > 0) {
-				princhash.data = memdup_user(
-						&ci->cc_princhash.cp_data,
-						princhashlen);
-				if (IS_ERR(princhash.data)) {
-					kfree(name.data);
-					return PTR_ERR(princhash.data);
-				}
+				princhashcopy = memdup_user(
+					&ci->cc_princhash.cp_data,
+					princhashlen);
+				if (IS_ERR(princhashcopy))
+					return PTR_ERR(princhashcopy);
+				princhash.data = princhashcopy;
 				princhash.len = princhashlen;
 			} else
 				princhash.len = 0;
@@ -820,9 +801,10 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 				dprintk("%s: invalid namelen (%u)", __func__, namelen);
 				return -EINVAL;
 			}
-			name.data = memdup_user(&cnm->cn_id, namelen);
-			if (IS_ERR(name.data))
-				return PTR_ERR(name.data);
+			namecopy = memdup_user(&cnm->cn_id, namelen);
+			if (IS_ERR(namecopy))
+				return PTR_ERR(namecopy);
+			name.data = namecopy;
 			name.len = namelen;
 		}
 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
@@ -830,15 +812,12 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 			struct cld_net *cn = nn->cld_net;
 
 			name.len = name.len - 5;
-			memmove(name.data, name.data + 5, name.len);
+			name.data = name.data + 5;
 			cn->cn_has_legacy = true;
 		}
 #endif
-		if (!nfs4_client_to_reclaim(name, princhash, nn)) {
-			kfree(name.data);
-			kfree(princhash.data);
+		if (!nfs4_client_to_reclaim(name, princhash, nn))
 			return -EFAULT;
-		}
 		return nn->client_tracking_ops->msglen;
 	}
 	return -EFAULT;
fs/nfsd/nfs4state.c +19 -3
index e45f67924bd0..fa073353f30b 100644
@@ -8801,9 +8801,6 @@ nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
 
 /*
  * failure => all reset bets are off, nfserr_no_grace...
- *
- * The caller is responsible for freeing name.data if NULL is returned (it
- * will be freed in nfs4_remove_reclaim_record in the normal case).
  */
 struct nfs4_client_reclaim *
 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
@@ -8812,6 +8809,22 @@ nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
 	unsigned int strhashval;
 	struct nfs4_client_reclaim *crp;
 
+	name.data = kmemdup(name.data, name.len, GFP_KERNEL);
+	if (!name.data) {
+		dprintk("%s: failed to allocate memory for name.data!\n",
+			__func__);
+		return NULL;
+	}
+	if (princhash.len) {
+		princhash.data = kmemdup(princhash.data, princhash.len, GFP_KERNEL);
+		if (!princhash.data) {
+			dprintk("%s: failed to allocate memory for princhash.data!\n",
+				__func__);
+			kfree(name.data);
+			return NULL;
+		}
+	} else
+		princhash.data = NULL;
 	crp = alloc_reclaim();
 	if (crp) {
 		strhashval = clientstr_hashval(name);
@@ -8823,6 +8836,9 @@ nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
 		crp->cr_princhash.len = princhash.len;
 		crp->cr_clp = NULL;
 		nn->reclaim_str_hashtbl_size++;
+	} else {
+		kfree(name.data);
+		kfree(princhash.data);
 	}
 	return crp;
 }

base-commit: b8cf39957931982091e6446a98f151a79aeea07b
prerequisite-patch-id: cf499d1388be776efe8f32ad8e6cd758bf3944da
prerequisite-patch-id: 7a60f48240b09e24d2b1a3ddac945c09aa0bb8d6

Signature · 1 line
2.50.0.107.gf914562f5916.dirty
#4 lore raw

Re: [PATCH v2] nfsd: change nfs4_client_to_reclaim() to allocate data

Quoted 64 lines · NeilBrown

The calling convention for nfs4_client_to_reclaim() is clumsy in that the caller need to free memory if the function fails. It is much cleaner if the function frees its own memory.

This patch changes nfs4_client_to_reclaim() to re-allocate the .data fields to be stored in the newly allocated struct nfs4_client_reclaim, and to free everything on failure.

__cld_pipe_inprogress_downcall() needs to allocate the data anyway to copy it from user-space, so now that data is allocated twice. I think that is a small price to pay for a cleaner interface.

---

This version fixes the problem Dan found with "child" being used uninitialised in "purge_old()".

 fs/nfsd/nfs4recover.c | 71 +++++++++++++++----------------------------
 fs/nfsd/nfs4state.c   | 22 ++++++++++++--
 2 files changed, 44 insertions(+), 49 deletions(-)
fs/nfsd/nfs4recover.c +3 -15
index 990fe3d958ed..b1005abcb903 100644
@@ -147,24 +147,13 @@ legacy_recdir_name_error(struct nfs4_client *clp, int error)
 
 static void
 __nfsd4_create_reclaim_record_grace(struct nfs4_client *clp,
-		const char *dname, int len, struct nfsd_net *nn)
+				    char *dname, struct nfsd_net *nn)
 {
-	struct xdr_netobj name;
+	struct xdr_netobj name = { .len = strlen(dname), .data = dname };
 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
 	struct nfs4_client_reclaim *crp;
 
-	name.data = kmemdup(dname, len, GFP_KERNEL);
-	if (!name.data) {
-		dprintk("%s: failed to allocate memory for name.data!\n",
-			__func__);
-		return;
-	}
-	name.len = len;
 	crp = nfs4_client_to_reclaim(name, princhash, nn);
-	if (!crp) {
-		kfree(name.data);
-		return;
-	}
 	crp->cr_clp = clp;
 }
 
@@ -223,8 +212,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
 	inode_unlock(d_inode(dir));
 	if (status == 0) {
 		if (nn->in_grace)
-			__nfsd4_create_reclaim_record_grace(clp, dname,
-					HEXDIR_LEN, nn);
+			__nfsd4_create_reclaim_record_grace(clp, dname, nn);
 		vfs_fsync(nn->rec_file, 0);
 	} else {
 		printk(KERN_ERR "NFSD: failed to write recovery record"

fs/nfsd/nfs4recover.c purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn) · line 398

Quoted 10 lines
fs/nfsd/nfs4recover.c +2 -2
@@ -403,8 +391,8 @@ purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
 	struct xdr_netobj name;
 
 	if (strlen(cname) != HEXDIR_LEN - 1) {
-		printk("%s: illegal name %pd in recovery directory\n",
-				__func__, child);
+		printk("%s: illegal name %s in recovery directory\n",
+				__func__, cname);
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}

Hi Neil,

It's the previous patch ("nfsd: move name lookup out of nfsd4_list_rec_dir()" that introduced this bug. Can you resend that one?

Signature · 1 line
Chuck Lever
#5 lore raw

Re: [PATCH v2] nfsd: change nfs4_client_to_reclaim() to allocate data

Quoted 6 lines · Chuck Lever

Hi Neil,

It's the previous patch ("nfsd: move name lookup out of nfsd4_list_rec_dir()" that introduced this bug. Can you resend that one?

Argg... of course it was. You'll have it soon.

Thanks,
NeilBrown

#6 lore raw

PATCH v2 1/1 nfsd: move name lookup out of nfsd4_list_rec_dir()

In reply to Chuck Lever

nfsd4_list_rec_dir() is called with two different callbacks. One of the callbacks uses vfs_rmdir() to remove the directory. The other doesn't use the dentry at all, just the name.

As only one callback needs the dentry, this patch moves the lookup into that callback. This prepares of changes to how directory operations are locked.

---

This time with the fix applied to the right patch :-(

 fs/nfsd/nfs4recover.c | 54 +++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 28 deletions(-)
fs/nfsd/nfs4recover.c +26 -28
index e2b9472e5c78..e9d09541161c 100644
@@ -237,7 +237,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
 	nfs4_reset_creds(original_cred);
 }
 
-typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
+typedef int (recdir_func)(struct dentry *, char *, struct nfsd_net *);
 
 struct name_list {
 	char name[HEXDIR_LEN];
@@ -291,24 +291,14 @@ nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
 	}
 
 	status = iterate_dir(nn->rec_file, &ctx.ctx);
-	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
 
 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
-		if (!status) {
-			struct dentry *dentry;
-			dentry = lookup_one(&nop_mnt_idmap,
-					    &QSTR(entry->name), dir);
-			if (IS_ERR(dentry)) {
-				status = PTR_ERR(dentry);
-				break;
-			}
-			status = f(dir, dentry, nn);
-			dput(dentry);
-		}
+		if (!status)
+			status = f(dir, entry->name, nn);
+
 		list_del(&entry->list);
 		kfree(entry);
 	}
-	inode_unlock(d_inode(dir));
 	nfs4_reset_creds(original_cred);
 
 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
@@ -406,18 +396,19 @@ nfsd4_remove_clid_dir(struct nfs4_client *clp)
 }
 
 static int
-purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
+purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
 {
 	int status;
+	struct dentry *child;
 	struct xdr_netobj name;
 
-	if (child->d_name.len != HEXDIR_LEN - 1) {
-		printk("%s: illegal name %pd in recovery directory\n",
-				__func__, child);
+	if (strlen(cname) != HEXDIR_LEN - 1) {
+		printk("%s: illegal name %s in recovery directory\n",
+				__func__, cname);
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}
-	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
+	name.data = kstrdup(cname, GFP_KERNEL);
 	if (!name.data) {
 		dprintk("%s: failed to allocate memory for name.data!\n",
 			__func__);
@@ -427,10 +418,17 @@ purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
 	if (nfs4_has_reclaimed_state(name, nn))
 		goto out_free;
 
-	status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
-	if (status)
-		printk("failed to remove client recovery directory %pd\n",
-				child);
+	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
+	child = lookup_one(&nop_mnt_idmap, &QSTR(cname), parent);
+	if (!IS_ERR(child)) {
+		status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
+		if (status)
+			printk("failed to remove client recovery directory %pd\n",
+			       child);
+		dput(child);
+	}
+	inode_unlock(d_inode(parent));
+
 out_free:
 	kfree(name.data);
 out:
@@ -461,18 +459,18 @@ nfsd4_recdir_purge_old(struct nfsd_net *nn)
 }
 
 static int
-load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
+load_recdir(struct dentry *parent, char *cname, struct nfsd_net *nn)
 {
 	struct xdr_netobj name;
 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
 
-	if (child->d_name.len != HEXDIR_LEN - 1) {
-		printk("%s: illegal name %pd in recovery directory\n",
-				__func__, child);
+	if (strlen(cname) != HEXDIR_LEN - 1) {
+		printk("%s: illegal name %s in recovery directory\n",
+				__func__, cname);
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}
-	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
+	name.data = kstrdup(cname, GFP_KERNEL);
 	if (!name.data) {
 		dprintk("%s: failed to allocate memory for name.data!\n",
 			__func__);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
#7 lore raw

Re: [PATCH v2] nfsd: move name lookup out of nfsd4_list_rec_dir()

Quoted 9 lines · NeilBrown

nfsd4_list_rec_dir() is called with two different callbacks. One of the callbacks uses vfs_rmdir() to remove the directory. The other doesn't use the dentry at all, just the name.

As only one callback needs the dentry, this patch moves the lookup into that callback. This prepares of changes to how directory operations are locked.

[...]

Applied to nfsd-testing, thanks!

[1/1] nfsd: move name lookup out of nfsd4_list_rec_dir()

      commit: 2f9cc710ed12ee9b3767586cc99cbd61125a08b3

"checkpatch.pl" reports:

From:/Signed-off-by: email address mismatch: 'From: NeilBrown <[email protected]>' != 'Signed-off-by: NeilBrown <[email protected]>'

"b4 am" reports:

✗ BADSIG: DKIM/messagingengine.com
✗ BADSIG: DKIM/ownmail.net

The patch did not go to [email protected], so it is archived only under [email protected].

Since this is already the second resend, I've applied it anyway.

--
Chuck Lever <[email protected]>

#8 lore raw

Re: [PATCH v2] nfsd: move name lookup out of nfsd4_list_rec_dir()

Quoted 9 lines · Chuck Lever
Quoted 8 lines · NeilBrown

nfsd4_list_rec_dir() is called with two different callbacks. One of the callbacks uses vfs_rmdir() to remove the directory. The other doesn't use the dentry at all, just the name.

As only one callback needs the dentry, this patch moves the lookup into that callback. This prepares of changes to how directory operations are locked.

Quoted 6 lines
Quoted 1 line

[...]

Applied to nfsd-testing, thanks!

[1/1] nfsd: move name lookup out of nfsd4_list_rec_dir()

      commit: 2f9cc710ed12ee9b3767586cc99cbd61125a08b3

Thanks.

Quoted 4 lines

"checkpatch.pl" reports:

From:/Signed-off-by: email address mismatch: 'From: NeilBrown <[email protected]>' != 'Signed-off-by: NeilBrown <[email protected]>'

That's my fault.
I had wanted to use "[email protected]" for everything but it turns out that I cannot control SPF and DKIM for that (it would be trivial for the owner of brown.name to enable that, but they won't.).
So I've created [email protected] and use that for sending, but set reply-to to [email protected] for sign patch creation.
But my habit of removing the the first 3 lines of "git format-patch" output when posting a patch will have to change.

However ... the email you replied to had "Reply-To: NeilBrown <[email protected]>" but your reply went to "NeilBrown <[email protected]>".
Any idea why?

Quoted 5 lines

"b4 am" reports:

✗ BADSIG: DKIM/messagingengine.com
✗ BADSIG: DKIM/ownmail.net

That is weird. The DKIM signatures should be good....

Thanks, and apoligies for messing up,
NeilBrown

Quoted 9 lines

The patch did not go to [email protected], so it is archived only under [email protected].

Since this is already the second resend, I've applied it anyway.

--
Chuck Lever <[email protected]>

#9 lore raw

Re: [PATCH v2] nfsd: move name lookup out of nfsd4_list_rec_dir()

Quoted 3 lines · NeilBrown

However ... the email you replied to had "Reply-To: NeilBrown <[email protected]>" but your reply went to "NeilBrown <[email protected]>".
Any idea why?

No idea. I'm traveling this week, so I'm stuck using some older versions of git/stgit. That could be it.

Signature · 1 line
Chuck Lever
maintainer Reviewed-by on v2 1/1
#10 lore raw

Re: [PATCH v2] nfsd: move name lookup out of nfsd4_list_rec_dir()

In reply to NeilBrown
Quoted 118 lines · NeilBrown

nfsd4_list_rec_dir() is called with two different callbacks. One of the callbacks uses vfs_rmdir() to remove the directory. The other doesn't use the dentry at all, just the name.

As only one callback needs the dentry, this patch moves the lookup into that callback. This prepares of changes to how directory operations are locked.

---

This time with the fix applied to the right patch :-(

 fs/nfsd/nfs4recover.c | 54 +++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 28 deletions(-)
fs/nfsd/nfs4recover.c +23 -27
index e2b9472e5c78..e9d09541161c 100644
@@ -237,7 +237,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
 	nfs4_reset_creds(original_cred);
 }
 
-typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
+typedef int (recdir_func)(struct dentry *, char *, struct nfsd_net *);
 
 struct name_list {
 	char name[HEXDIR_LEN];
@@ -291,24 +291,14 @@ nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
 	}
 
 	status = iterate_dir(nn->rec_file, &ctx.ctx);
-	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
 
 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
-		if (!status) {
-			struct dentry *dentry;
-			dentry = lookup_one(&nop_mnt_idmap,
-					    &QSTR(entry->name), dir);
-			if (IS_ERR(dentry)) {
-				status = PTR_ERR(dentry);
-				break;
-			}
-			status = f(dir, dentry, nn);
-			dput(dentry);
-		}
+		if (!status)
+			status = f(dir, entry->name, nn);
+
 		list_del(&entry->list);
 		kfree(entry);
 	}
-	inode_unlock(d_inode(dir));
 	nfs4_reset_creds(original_cred);
 
 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
@@ -406,18 +396,19 @@ nfsd4_remove_clid_dir(struct nfs4_client *clp)
 }
 
 static int
-purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
+purge_old(struct dentry *parent, char *cname, struct nfsd_net *nn)
 {
 	int status;
+	struct dentry *child;
 	struct xdr_netobj name;
 
-	if (child->d_name.len != HEXDIR_LEN - 1) {
-		printk("%s: illegal name %pd in recovery directory\n",
-				__func__, child);
+	if (strlen(cname) != HEXDIR_LEN - 1) {
+		printk("%s: illegal name %s in recovery directory\n",
+				__func__, cname);
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}
-	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
+	name.data = kstrdup(cname, GFP_KERNEL);
 	if (!name.data) {
 		dprintk("%s: failed to allocate memory for name.data!\n",
 			__func__);
@@ -427,10 +418,17 @@ purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
 	if (nfs4_has_reclaimed_state(name, nn))
 		goto out_free;
 
-	status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
-	if (status)
-		printk("failed to remove client recovery directory %pd\n",
-				child);
+	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
+	child = lookup_one(&nop_mnt_idmap, &QSTR(cname), parent);
+	if (!IS_ERR(child)) {
+		status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
+		if (status)
+			printk("failed to remove client recovery directory %pd\n",
+			       child);
+		dput(child);
+	}
+	inode_unlock(d_inode(parent));
+
 out_free:
 	kfree(name.data);
 out:
@@ -461,18 +459,18 @@ nfsd4_recdir_purge_old(struct nfsd_net *nn)
 }
 
 static int
-load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
+load_recdir(struct dentry *parent, char *cname, struct nfsd_net *nn)
 {
 	struct xdr_netobj name;
 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
 
-	if (child->d_name.len != HEXDIR_LEN - 1) {
-		printk("%s: illegal name %pd in recovery directory\n",
-				__func__, child);
+	if (strlen(cname) != HEXDIR_LEN - 1) {

fs/nfsd/nfs4recover.c nfsd4_recdir_purge_old(struct nfsd_net *nn) · line 476

Quoted 10 lines
fs/nfsd/nfs4recover.c +3 -1
+		printk("%s: illegal name %s in recovery directory\n",
+				__func__, cname);
 		/* Keep trying; maybe the others are OK: */
 		return 0;
 	}
-	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
+	name.data = kstrdup(cname, GFP_KERNEL);
 	if (!name.data) {
 		dprintk("%s: failed to allocate memory for name.data!\n",
 			__func__);