write_foo functions are called to handle IO to files in /proc/fs/nfsd/. They can be called at any time and so generally need locking to ensure they don't happen at an awkward time. Many already take nfsd_mutex and check if nfsd_serv has been set. This ensures they only run when the server is fully configured. write_filehandle() does *not* need locking. It interacts with the export table which is set up when the netns is set up, so it is always valid and it has its own locking. write_filehandle() is needed before the nfs server is started so checking nfsd_serv would be wrong. The remaining files which do not have any locking are write_v4_end_grace(), write_unlock_ip(), and write_unlock_fs(). None of these make sense when the nfs server is not running and there is evidence that write_v4_end_grace() can race with ->client_tracking_op setup/shutdown and cause problems. This patch adds locking to these three and ensures the "unlock" functions abort if ->nfsd_serv is not set. Reported-and-tested-by: Li Lingfeng <lilingfeng3@xxxxxxxxxx> Signed-off-by: NeilBrown <neil@xxxxxxxxxx> --- fs/nfsd/nfsctl.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/fs/nfsd/nfsctl.c b/fs/nfsd/nfsctl.c index 3f3e9f6c4250..6b0cad81b5fa 100644 --- a/fs/nfsd/nfsctl.c +++ b/fs/nfsd/nfsctl.c @@ -214,13 +214,18 @@ static inline struct net *netns(struct file *file) * returns one if one or more locks were not released * On error: return code is negative errno value */ -static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) +static ssize_t __write_unlock_ip(struct file *file, char *buf, size_t size) { struct sockaddr_storage address; struct sockaddr *sap = (struct sockaddr *)&address; size_t salen = sizeof(address); char *fo_path; struct net *net = netns(file); + struct nfsd_net *nn = net_generic(net, nfsd_net_id); + + if (!nn->nfsd_serv) + /* There cannot be any files to unlock */ + return -EINVAL; /* sanity check */ if (size == 0) @@ -240,6 +245,16 @@ static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) return nlmsvc_unlock_all_by_ip(sap); } +static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) +{ + ssize_t ret; + + mutex_lock(&nfsd_mutex); + ret = __write_unlock_ip(file, buf, size); + mutex_unlock(&nfsd_mutex); + return ret; +} + /* * write_unlock_fs - Release all locks on a local file system * @@ -254,11 +269,16 @@ static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) * returns one if one or more locks were not released * On error: return code is negative errno value */ -static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) +static ssize_t __write_unlock_fs(struct file *file, char *buf, size_t size) { struct path path; char *fo_path; int error; + struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); + + if (!nn->nfsd_serv) + /* There cannot be any files to unlock */ + return -EINVAL; /* sanity check */ if (size == 0) @@ -291,6 +311,16 @@ static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) return error; } +static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) +{ + ssize_t ret; + + mutex_lock(&nfsd_mutex); + ret = __write_unlock_fs(file, buf, size); + mutex_unlock(&nfsd_mutex); + return ret; +} + /* * write_filehandle - Get a variable-length NFS file handle by path * @@ -1082,10 +1112,14 @@ static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) case 'Y': case 'y': case '1': - if (!nn->nfsd_serv) + mutex_lock(&nfsd_mutex); + if (!nn->nfsd_serv) { + mutex_unlock(&nfsd_mutex); return -EBUSY; + } trace_nfsd_end_grace(netns(file)); nfsd4_end_grace(nn); + mutex_unlock(&nfsd_mutex); break; default: return -EINVAL; -- 2.49.0