109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Syscall interface to knfsd. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 85a0e3ad6STejun Heo #include <linux/slab.h> 93f8206d4SAl Viro #include <linux/namei.h> 10b41b66d6SNeilBrown #include <linux/ctype.h> 1196a374a3SDavid Howells #include <linux/fs_context.h> 121da177e4SLinus Torvalds 1380212d59SNeilBrown #include <linux/sunrpc/svcsock.h> 144373ea84SWendy Cheng #include <linux/lockd/lockd.h> 155976687aSJeff Layton #include <linux/sunrpc/addr.h> 16b0b0c0a2SKevin Coffman #include <linux/sunrpc/gss_api.h> 17813fd320SJeff Layton #include <linux/sunrpc/rpc_pipe_fs.h> 18924f4fb0SLorenzo Bianconi #include <linux/sunrpc/svc.h> 19143cb494SPaul Gortmaker #include <linux/module.h> 20e8a79fb1SJ. Bruce Fields #include <linux/fsnotify.h> 211da177e4SLinus Torvalds 222ca72e17SJ. Bruce Fields #include "idmap.h" 239a74af21SBoaz Harrosh #include "nfsd.h" 249a74af21SBoaz Harrosh #include "cache.h" 25f3c7521fSBryan Schumaker #include "state.h" 267ea34ac1SJeff Layton #include "netns.h" 279cf514ccSChristoph Hellwig #include "pnfs.h" 282e6c6e4cSChuck Lever #include "filecache.h" 2939d432fcSChuck Lever #include "trace.h" 3013727f85SLorenzo Bianconi #include "netlink.h" 319a74af21SBoaz Harrosh 321da177e4SLinus Torvalds /* 33b0b0c0a2SKevin Coffman * We have a single directory with several nodes in it. 341da177e4SLinus Torvalds */ 351da177e4SLinus Torvalds enum { 361da177e4SLinus Torvalds NFSD_Root = 1, 371da177e4SLinus Torvalds NFSD_List, 3820ad856eSAmir Goldstein NFSD_Export_Stats, 39e8e8753fSJ. Bruce Fields NFSD_Export_features, 401da177e4SLinus Torvalds NFSD_Fh, 414373ea84SWendy Cheng NFSD_FO_UnlockIP, 4217efa372SWendy Cheng NFSD_FO_UnlockFS, 431da177e4SLinus Torvalds NFSD_Threads, 44eed2965aSGreg Banks NFSD_Pool_Threads, 4503cf6c9fSGreg Banks NFSD_Pool_Stats, 46a2f999a3SJeff Layton NFSD_Reply_Cache_Stats, 4770c3b76cSNeilBrown NFSD_Versions, 4880212d59SNeilBrown NFSD_Ports, 49596bbe53SNeilBrown NFSD_MaxBlkSize, 505b8db00bSJeff Layton NFSD_MaxConnections, 512e6c6e4cSChuck Lever NFSD_Filecache, 521da177e4SLinus Torvalds NFSD_Leasetime, 53efc4bb4fSJ. Bruce Fields NFSD_Gracetime, 540964a3d3SNeilBrown NFSD_RecoveryDir, 557f5ef2e9SJeff Layton NFSD_V4EndGrace, 56e8a79fb1SJ. Bruce Fields NFSD_MaxReserved 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * write() for these nodes. 611da177e4SLinus Torvalds */ 621da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size); 63b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size); 64b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size); 651da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size); 66eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size); 6770c3b76cSNeilBrown static ssize_t write_versions(struct file *file, char *buf, size_t size); 6880212d59SNeilBrown static ssize_t write_ports(struct file *file, char *buf, size_t size); 69596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); 705b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size); 7170c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 721da177e4SLinus Torvalds static ssize_t write_leasetime(struct file *file, char *buf, size_t size); 73efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size); 7474fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 750964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size); 7674fd4873SJeff Layton #endif 777f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size); 7870c3b76cSNeilBrown #endif 791da177e4SLinus Torvalds 80c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 811da177e4SLinus Torvalds [NFSD_Fh] = write_filehandle, 82b046ccdcSChuck Lever [NFSD_FO_UnlockIP] = write_unlock_ip, 83b046ccdcSChuck Lever [NFSD_FO_UnlockFS] = write_unlock_fs, 841da177e4SLinus Torvalds [NFSD_Threads] = write_threads, 85eed2965aSGreg Banks [NFSD_Pool_Threads] = write_pool_threads, 8670c3b76cSNeilBrown [NFSD_Versions] = write_versions, 8780212d59SNeilBrown [NFSD_Ports] = write_ports, 88596bbe53SNeilBrown [NFSD_MaxBlkSize] = write_maxblksize, 895b8db00bSJeff Layton [NFSD_MaxConnections] = write_maxconn, 9070c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 911da177e4SLinus Torvalds [NFSD_Leasetime] = write_leasetime, 92efc4bb4fSJ. Bruce Fields [NFSD_Gracetime] = write_gracetime, 9374fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 940964a3d3SNeilBrown [NFSD_RecoveryDir] = write_recoverydir, 9574fd4873SJeff Layton #endif 967f5ef2e9SJeff Layton [NFSD_V4EndGrace] = write_v4_end_grace, 9770c3b76cSNeilBrown #endif 981da177e4SLinus Torvalds }; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 1011da177e4SLinus Torvalds { 102496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 1031da177e4SLinus Torvalds char *data; 1041da177e4SLinus Torvalds ssize_t rv; 1051da177e4SLinus Torvalds 106e8c96f8cSTobias Klauser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 1071da177e4SLinus Torvalds return -EINVAL; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 1101da177e4SLinus Torvalds if (IS_ERR(data)) 1111da177e4SLinus Torvalds return PTR_ERR(data); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 1143434d7aaSChuck Lever if (rv < 0) 1151da177e4SLinus Torvalds return rv; 1163434d7aaSChuck Lever 1173434d7aaSChuck Lever simple_transaction_set(file, rv); 1183434d7aaSChuck Lever return size; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1217390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 1227390022dSNeilBrown { 1237390022dSNeilBrown if (! file->private_data) { 1247390022dSNeilBrown /* An attempt to read a transaction file without writing 1257390022dSNeilBrown * causes a 0-byte write so that the file can return 1267390022dSNeilBrown * state information 1277390022dSNeilBrown */ 1287390022dSNeilBrown ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos); 1297390022dSNeilBrown if (rv < 0) 1307390022dSNeilBrown return rv; 1317390022dSNeilBrown } 1327390022dSNeilBrown return simple_transaction_read(file, buf, size, pos); 1337390022dSNeilBrown } 1347390022dSNeilBrown 1354b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = { 1361da177e4SLinus Torvalds .write = nfsctl_transaction_write, 1377390022dSNeilBrown .read = nfsctl_transaction_read, 1381da177e4SLinus Torvalds .release = simple_transaction_release, 1396038f373SArnd Bergmann .llseek = default_llseek, 1401da177e4SLinus Torvalds }; 1411da177e4SLinus Torvalds 14296d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file) 1431da177e4SLinus Torvalds { 144f2c7ea10SStanislav Kinsbursky int err; 145f2c7ea10SStanislav Kinsbursky struct seq_file *seq; 14696d851c4SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 147f2c7ea10SStanislav Kinsbursky 148f2c7ea10SStanislav Kinsbursky err = seq_open(file, &nfs_exports_op); 149f2c7ea10SStanislav Kinsbursky if (err) 150f2c7ea10SStanislav Kinsbursky return err; 151f2c7ea10SStanislav Kinsbursky 152f2c7ea10SStanislav Kinsbursky seq = file->private_data; 153e5f06f72SStanislav Kinsbursky seq->private = nn->svc_export_cache; 154f2c7ea10SStanislav Kinsbursky return 0; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 15796d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file) 15896d851c4SStanislav Kinsbursky { 15996d851c4SStanislav Kinsbursky return exports_net_open(inode->i_sb->s_fs_info, file); 16096d851c4SStanislav Kinsbursky } 16196d851c4SStanislav Kinsbursky 16296d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = { 16396d851c4SStanislav Kinsbursky .open = exports_nfsd_open, 1641da177e4SLinus Torvalds .read = seq_read, 1651da177e4SLinus Torvalds .llseek = seq_lseek, 1661da177e4SLinus Torvalds .release = seq_release, 1671da177e4SLinus Torvalds }; 1681da177e4SLinus Torvalds 169e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v) 170e8e8753fSJ. Bruce Fields { 171e8e8753fSJ. Bruce Fields seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS); 172e8e8753fSJ. Bruce Fields return 0; 173e8e8753fSJ. Bruce Fields } 174e8e8753fSJ. Bruce Fields 1759beeaab8SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(export_features); 176e8e8753fSJ. Bruce Fields 177828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = { 17803cf6c9fSGreg Banks .open = nfsd_pool_stats_open, 17903cf6c9fSGreg Banks .read = seq_read, 18003cf6c9fSGreg Banks .llseek = seq_lseek, 1817b207ccdSNeilBrown .release = seq_release, 18203cf6c9fSGreg Banks }; 18303cf6c9fSGreg Banks 18464776611SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats); 185a2f999a3SJeff Layton 1861342f9ddSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats); 1872e6c6e4cSChuck Lever 1881da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/ 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * payload - write methods 1911da177e4SLinus Torvalds */ 1921da177e4SLinus Torvalds 193244c7d44SAl Viro static inline struct net *netns(struct file *file) 194244c7d44SAl Viro { 195244c7d44SAl Viro return file_inode(file)->i_sb->s_fs_info; 196244c7d44SAl Viro } 1971da177e4SLinus Torvalds 198f2453978SChuck Lever /* 199262a0982SChuck Lever * write_unlock_ip - Release all locks used by a client 200262a0982SChuck Lever * 201262a0982SChuck Lever * Experimental. 202262a0982SChuck Lever * 203262a0982SChuck Lever * Input: 204262a0982SChuck Lever * buf: '\n'-terminated C string containing a 2054116092bSChuck Lever * presentation format IP address 206262a0982SChuck Lever * size: length of C string in @buf 207262a0982SChuck Lever * Output: 208262a0982SChuck Lever * On success: returns zero if all specified locks were released; 209262a0982SChuck Lever * returns one if one or more locks were not released 210262a0982SChuck Lever * On error: return code is negative errno value 211262a0982SChuck Lever */ 212b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) 2134373ea84SWendy Cheng { 2144116092bSChuck Lever struct sockaddr_storage address; 2154116092bSChuck Lever struct sockaddr *sap = (struct sockaddr *)&address; 2164116092bSChuck Lever size_t salen = sizeof(address); 217367c8c7bSChuck Lever char *fo_path; 218244c7d44SAl Viro struct net *net = netns(file); 2194373ea84SWendy Cheng 2204373ea84SWendy Cheng /* sanity check */ 2214373ea84SWendy Cheng if (size == 0) 2224373ea84SWendy Cheng return -EINVAL; 2234373ea84SWendy Cheng 2244373ea84SWendy Cheng if (buf[size-1] != '\n') 2254373ea84SWendy Cheng return -EINVAL; 2264373ea84SWendy Cheng 2274373ea84SWendy Cheng fo_path = buf; 2284373ea84SWendy Cheng if (qword_get(&buf, fo_path, size) < 0) 2294373ea84SWendy Cheng return -EINVAL; 2304373ea84SWendy Cheng 23111f77942SStanislav Kinsbursky if (rpc_pton(net, fo_path, size, sap, salen) == 0) 2324373ea84SWendy Cheng return -EINVAL; 2334373ea84SWendy Cheng 23439d432fcSChuck Lever trace_nfsd_ctl_unlock_ip(net, buf); 2354116092bSChuck Lever return nlmsvc_unlock_all_by_ip(sap); 2364373ea84SWendy Cheng } 2374373ea84SWendy Cheng 238f2453978SChuck Lever /* 239262a0982SChuck Lever * write_unlock_fs - Release all locks on a local file system 240262a0982SChuck Lever * 241262a0982SChuck Lever * Experimental. 242262a0982SChuck Lever * 243262a0982SChuck Lever * Input: 244262a0982SChuck Lever * buf: '\n'-terminated C string containing the 245262a0982SChuck Lever * absolute pathname of a local file system 246262a0982SChuck Lever * size: length of C string in @buf 247262a0982SChuck Lever * Output: 248262a0982SChuck Lever * On success: returns zero if all specified locks were released; 249262a0982SChuck Lever * returns one if one or more locks were not released 250262a0982SChuck Lever * On error: return code is negative errno value 251262a0982SChuck Lever */ 252b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) 25317efa372SWendy Cheng { 254a63bb996SAl Viro struct path path; 25517efa372SWendy Cheng char *fo_path; 25617efa372SWendy Cheng int error; 25717efa372SWendy Cheng 25817efa372SWendy Cheng /* sanity check */ 25917efa372SWendy Cheng if (size == 0) 26017efa372SWendy Cheng return -EINVAL; 26117efa372SWendy Cheng 26217efa372SWendy Cheng if (buf[size-1] != '\n') 26317efa372SWendy Cheng return -EINVAL; 26417efa372SWendy Cheng 26517efa372SWendy Cheng fo_path = buf; 26617efa372SWendy Cheng if (qword_get(&buf, fo_path, size) < 0) 26717efa372SWendy Cheng return -EINVAL; 26839d432fcSChuck Lever trace_nfsd_ctl_unlock_fs(netns(file), fo_path); 269a63bb996SAl Viro error = kern_path(fo_path, 0, &path); 27017efa372SWendy Cheng if (error) 27117efa372SWendy Cheng return error; 27217efa372SWendy Cheng 273262a0982SChuck Lever /* 274262a0982SChuck Lever * XXX: Needs better sanity checking. Otherwise we could end up 275262a0982SChuck Lever * releasing locks on the wrong file system. 276262a0982SChuck Lever * 277262a0982SChuck Lever * For example: 278262a0982SChuck Lever * 1. Does the path refer to a directory? 279262a0982SChuck Lever * 2. Is that directory a mount point, or 280262a0982SChuck Lever * 3. Is that directory the root of an exported file system? 281262a0982SChuck Lever */ 282d8c9584eSAl Viro error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 2831ac3629bSNeilBrown nfsd4_revoke_states(netns(file), path.dentry->d_sb); 28417efa372SWendy Cheng 285a63bb996SAl Viro path_put(&path); 28617efa372SWendy Cheng return error; 28717efa372SWendy Cheng } 28817efa372SWendy Cheng 289f2453978SChuck Lever /* 290262a0982SChuck Lever * write_filehandle - Get a variable-length NFS file handle by path 291262a0982SChuck Lever * 292262a0982SChuck Lever * On input, the buffer contains a '\n'-terminated C string comprised of 293262a0982SChuck Lever * three alphanumeric words separated by whitespace. The string may 294262a0982SChuck Lever * contain escape sequences. 295262a0982SChuck Lever * 296262a0982SChuck Lever * Input: 297262a0982SChuck Lever * buf: 298262a0982SChuck Lever * domain: client domain name 299262a0982SChuck Lever * path: export pathname 300262a0982SChuck Lever * maxsize: numeric maximum size of 301262a0982SChuck Lever * @buf 302262a0982SChuck Lever * size: length of C string in @buf 303262a0982SChuck Lever * Output: 304262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 305262a0982SChuck Lever * string containing a ASCII hex text version 306262a0982SChuck Lever * of the NFS file handle; 307262a0982SChuck Lever * return code is the size in bytes of the string 308262a0982SChuck Lever * On error: return code is negative errno value 309262a0982SChuck Lever */ 3101da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds char *dname, *path; 3133f649ab7SKees Cook int maxsize; 3141da177e4SLinus Torvalds char *mesg = buf; 3151da177e4SLinus Torvalds int len; 3161da177e4SLinus Torvalds struct auth_domain *dom; 3171da177e4SLinus Torvalds struct knfsd_fh fh; 3181da177e4SLinus Torvalds 31987d26ea7SJ. Bruce Fields if (size == 0) 32087d26ea7SJ. Bruce Fields return -EINVAL; 32187d26ea7SJ. Bruce Fields 3221da177e4SLinus Torvalds if (buf[size-1] != '\n') 3231da177e4SLinus Torvalds return -EINVAL; 3241da177e4SLinus Torvalds buf[size-1] = 0; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds dname = mesg; 3271da177e4SLinus Torvalds len = qword_get(&mesg, dname, size); 32854224f04SChuck Lever if (len <= 0) 32954224f04SChuck Lever return -EINVAL; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds path = dname+len+1; 3321da177e4SLinus Torvalds len = qword_get(&mesg, path, size); 33354224f04SChuck Lever if (len <= 0) 33454224f04SChuck Lever return -EINVAL; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds len = get_int(&mesg, &maxsize); 3371da177e4SLinus Torvalds if (len) 3381da177e4SLinus Torvalds return len; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds if (maxsize < NFS_FHSIZE) 3411da177e4SLinus Torvalds return -EINVAL; 3423c7aa15dSKinglong Mee maxsize = min(maxsize, NFS3_FHSIZE); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds if (qword_get(&mesg, mesg, size) > 0) 3451da177e4SLinus Torvalds return -EINVAL; 3461da177e4SLinus Torvalds 34739d432fcSChuck Lever trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize); 34839d432fcSChuck Lever 3491da177e4SLinus Torvalds /* we have all the words, they are in buf.. */ 3501da177e4SLinus Torvalds dom = unix_domain_find(dname); 3511da177e4SLinus Torvalds if (!dom) 3521da177e4SLinus Torvalds return -ENOMEM; 3531da177e4SLinus Torvalds 354244c7d44SAl Viro len = exp_rootfh(netns(file), dom, path, &fh, maxsize); 3551da177e4SLinus Torvalds auth_domain_put(dom); 3561da177e4SLinus Torvalds if (len) 3571da177e4SLinus Torvalds return len; 3581da177e4SLinus Torvalds 35954224f04SChuck Lever mesg = buf; 36054224f04SChuck Lever len = SIMPLE_TRANSACTION_LIMIT; 361d8b26071SNeilBrown qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size); 3621da177e4SLinus Torvalds mesg[-1] = '\n'; 3631da177e4SLinus Torvalds return mesg - buf; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 366f2453978SChuck Lever /* 367262a0982SChuck Lever * write_threads - Start NFSD, or report the current number of running threads 368262a0982SChuck Lever * 369262a0982SChuck Lever * Input: 370262a0982SChuck Lever * buf: ignored 371262a0982SChuck Lever * size: zero 372262a0982SChuck Lever * Output: 373262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 374262a0982SChuck Lever * string numeric value representing the number of 375262a0982SChuck Lever * running NFSD threads; 376262a0982SChuck Lever * return code is the size in bytes of the string 377262a0982SChuck Lever * On error: return code is zero 378262a0982SChuck Lever * 379262a0982SChuck Lever * OR 380262a0982SChuck Lever * 381262a0982SChuck Lever * Input: 382262a0982SChuck Lever * buf: C string containing an unsigned 383262a0982SChuck Lever * integer value representing the 384262a0982SChuck Lever * number of NFSD threads to start 385262a0982SChuck Lever * size: non-zero length of C string in @buf 386262a0982SChuck Lever * Output: 387262a0982SChuck Lever * On success: NFS service is started; 388262a0982SChuck Lever * passed-in buffer filled with '\n'-terminated C 389262a0982SChuck Lever * string numeric value representing the number of 390262a0982SChuck Lever * running NFSD threads; 391262a0982SChuck Lever * return code is the size in bytes of the string 392262a0982SChuck Lever * On error: return code is zero or a negative errno value 393262a0982SChuck Lever */ 3941da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds char *mesg = buf; 3971da177e4SLinus Torvalds int rv; 398244c7d44SAl Viro struct net *net = netns(file); 399d41a9417SStanislav Kinsbursky 4001da177e4SLinus Torvalds if (size > 0) { 4011da177e4SLinus Torvalds int newthreads; 4021da177e4SLinus Torvalds rv = get_int(&mesg, &newthreads); 4031da177e4SLinus Torvalds if (rv) 4041da177e4SLinus Torvalds return rv; 4051da177e4SLinus Torvalds if (newthreads < 0) 4061da177e4SLinus Torvalds return -EINVAL; 40739d432fcSChuck Lever trace_nfsd_ctl_threads(net, newthreads); 4080842b4c8SJeff Layton mutex_lock(&nfsd_mutex); 4099077d598SJeff Layton rv = nfsd_svc(newthreads, net, file->f_cred, NULL); 4100842b4c8SJeff Layton mutex_unlock(&nfsd_mutex); 41182e12fe9SNeilBrown if (rv < 0) 4121da177e4SLinus Torvalds return rv; 41382e12fe9SNeilBrown } else 4149dd9845fSStanislav Kinsbursky rv = nfsd_nrthreads(net); 415e06b6405SChuck Lever 41682e12fe9SNeilBrown return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 419f2453978SChuck Lever /* 420262a0982SChuck Lever * write_pool_threads - Set or report the current number of threads per pool 421262a0982SChuck Lever * 422262a0982SChuck Lever * Input: 423262a0982SChuck Lever * buf: ignored 424262a0982SChuck Lever * size: zero 425262a0982SChuck Lever * 426262a0982SChuck Lever * OR 427262a0982SChuck Lever * 428262a0982SChuck Lever * Input: 429262a0982SChuck Lever * buf: C string containing whitespace- 430262a0982SChuck Lever * separated unsigned integer values 431262a0982SChuck Lever * representing the number of NFSD 432262a0982SChuck Lever * threads to start in each pool 433262a0982SChuck Lever * size: non-zero length of C string in @buf 434262a0982SChuck Lever * Output: 435262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 436262a0982SChuck Lever * string containing integer values representing the 437262a0982SChuck Lever * number of NFSD threads in each pool; 438262a0982SChuck Lever * return code is the size in bytes of the string 439262a0982SChuck Lever * On error: return code is zero or a negative errno value 440262a0982SChuck Lever */ 441eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size) 442eed2965aSGreg Banks { 443eed2965aSGreg Banks /* if size > 0, look for an array of number of threads per node 444eed2965aSGreg Banks * and apply them then write out number of threads per node as reply 445eed2965aSGreg Banks */ 446eed2965aSGreg Banks char *mesg = buf; 447eed2965aSGreg Banks int i; 448eed2965aSGreg Banks int rv; 449eed2965aSGreg Banks int len; 450bedbdd8bSNeil Brown int npools; 451eed2965aSGreg Banks int *nthreads; 452244c7d44SAl Viro struct net *net = netns(file); 453eed2965aSGreg Banks 454bedbdd8bSNeil Brown mutex_lock(&nfsd_mutex); 4559dd9845fSStanislav Kinsbursky npools = nfsd_nrpools(net); 456eed2965aSGreg Banks if (npools == 0) { 457eed2965aSGreg Banks /* 458eed2965aSGreg Banks * NFS is shut down. The admin can start it by 459eed2965aSGreg Banks * writing to the threads file but NOT the pool_threads 460eed2965aSGreg Banks * file, sorry. Report zero threads. 461eed2965aSGreg Banks */ 462bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 463eed2965aSGreg Banks strcpy(buf, "0\n"); 464eed2965aSGreg Banks return strlen(buf); 465eed2965aSGreg Banks } 466eed2965aSGreg Banks 467eed2965aSGreg Banks nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL); 468bedbdd8bSNeil Brown rv = -ENOMEM; 469eed2965aSGreg Banks if (nthreads == NULL) 470bedbdd8bSNeil Brown goto out_free; 471eed2965aSGreg Banks 472eed2965aSGreg Banks if (size > 0) { 473eed2965aSGreg Banks for (i = 0; i < npools; i++) { 474eed2965aSGreg Banks rv = get_int(&mesg, &nthreads[i]); 475eed2965aSGreg Banks if (rv == -ENOENT) 476eed2965aSGreg Banks break; /* fewer numbers than pools */ 477eed2965aSGreg Banks if (rv) 478eed2965aSGreg Banks goto out_free; /* syntax error */ 479eed2965aSGreg Banks rv = -EINVAL; 480eed2965aSGreg Banks if (nthreads[i] < 0) 481eed2965aSGreg Banks goto out_free; 48239d432fcSChuck Lever trace_nfsd_ctl_pool_threads(net, i, nthreads[i]); 483eed2965aSGreg Banks } 4843938a0d5SStanislav Kinsbursky rv = nfsd_set_nrthreads(i, nthreads, net); 485eed2965aSGreg Banks if (rv) 486eed2965aSGreg Banks goto out_free; 487eed2965aSGreg Banks } 488eed2965aSGreg Banks 4899dd9845fSStanislav Kinsbursky rv = nfsd_get_nrthreads(npools, nthreads, net); 490eed2965aSGreg Banks if (rv) 491eed2965aSGreg Banks goto out_free; 492eed2965aSGreg Banks 493eed2965aSGreg Banks mesg = buf; 494eed2965aSGreg Banks size = SIMPLE_TRANSACTION_LIMIT; 495eed2965aSGreg Banks for (i = 0; i < npools && size > 0; i++) { 496eed2965aSGreg Banks snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' ')); 497eed2965aSGreg Banks len = strlen(mesg); 498eed2965aSGreg Banks size -= len; 499eed2965aSGreg Banks mesg += len; 500eed2965aSGreg Banks } 501413d63d7SJ. Bruce Fields rv = mesg - buf; 502eed2965aSGreg Banks out_free: 503eed2965aSGreg Banks kfree(nthreads); 504bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 505eed2965aSGreg Banks return rv; 506eed2965aSGreg Banks } 507eed2965aSGreg Banks 508ff7d1179STrond Myklebust static ssize_t 509e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining, 510e333f3bbSTrond Myklebust const char *sep, unsigned vers, int minor) 511ff7d1179STrond Myklebust { 512abcb4dacSNeilBrown const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u"; 513e333f3bbSTrond Myklebust bool supported = !!nfsd_vers(nn, vers, NFSD_TEST); 514ff7d1179STrond Myklebust 515abcb4dacSNeilBrown if (vers == 4 && minor >= 0 && 516e333f3bbSTrond Myklebust !nfsd_minorversion(nn, minor, NFSD_TEST)) 517ff7d1179STrond Myklebust supported = false; 518abcb4dacSNeilBrown if (minor == 0 && supported) 519abcb4dacSNeilBrown /* 520abcb4dacSNeilBrown * special case for backward compatability. 521abcb4dacSNeilBrown * +4.0 is never reported, it is implied by 522abcb4dacSNeilBrown * +4, unless -4.0 is present. 523abcb4dacSNeilBrown */ 524abcb4dacSNeilBrown return 0; 525ff7d1179STrond Myklebust return snprintf(buf, remaining, format, sep, 526ff7d1179STrond Myklebust supported ? '+' : '-', vers, minor); 527ff7d1179STrond Myklebust } 528ff7d1179STrond Myklebust 5293dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size) 53070c3b76cSNeilBrown { 53170c3b76cSNeilBrown char *mesg = buf; 5328daf220aSBenny Halevy char *vers, *minorp, sign; 533261758b5SChuck Lever int len, num, remaining; 53470c3b76cSNeilBrown ssize_t tlen = 0; 53570c3b76cSNeilBrown char *sep; 536244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 53770c3b76cSNeilBrown 53870c3b76cSNeilBrown if (size > 0) { 5399dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) 5406658d3a7SNeilBrown /* Cannot change versions without updating 5419dd9845fSStanislav Kinsbursky * nn->nfsd_serv->sv_xdrsize, and reallocing 5426658d3a7SNeilBrown * rq_argp and rq_resp 5436658d3a7SNeilBrown */ 54470c3b76cSNeilBrown return -EBUSY; 54570c3b76cSNeilBrown if (buf[size-1] != '\n') 54670c3b76cSNeilBrown return -EINVAL; 54770c3b76cSNeilBrown buf[size-1] = 0; 54839d432fcSChuck Lever trace_nfsd_ctl_version(netns(file), buf); 54970c3b76cSNeilBrown 55070c3b76cSNeilBrown vers = mesg; 55170c3b76cSNeilBrown len = qword_get(&mesg, vers, size); 55270c3b76cSNeilBrown if (len <= 0) return -EINVAL; 55370c3b76cSNeilBrown do { 554d3635ff0STrond Myklebust enum vers_op cmd; 555abcb4dacSNeilBrown unsigned minor; 55670c3b76cSNeilBrown sign = *vers; 55770c3b76cSNeilBrown if (sign == '+' || sign == '-') 5588daf220aSBenny Halevy num = simple_strtol((vers+1), &minorp, 0); 55970c3b76cSNeilBrown else 5608daf220aSBenny Halevy num = simple_strtol(vers, &minorp, 0); 5618daf220aSBenny Halevy if (*minorp == '.') { 562ff89be87SJ. Bruce Fields if (num != 4) 5638daf220aSBenny Halevy return -EINVAL; 564e35659f1SNeilBrown if (kstrtouint(minorp+1, 0, &minor) < 0) 5658daf220aSBenny Halevy return -EINVAL; 566abcb4dacSNeilBrown } 567abcb4dacSNeilBrown 568d3635ff0STrond Myklebust cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET; 56970c3b76cSNeilBrown switch(num) { 5702f3a4b2aSJeff Layton #ifdef CONFIG_NFSD_V2 57170c3b76cSNeilBrown case 2: 5722f3a4b2aSJeff Layton #endif 57370c3b76cSNeilBrown case 3: 574e333f3bbSTrond Myklebust nfsd_vers(nn, num, cmd); 575d3635ff0STrond Myklebust break; 57670c3b76cSNeilBrown case 4: 577abcb4dacSNeilBrown if (*minorp == '.') { 578e333f3bbSTrond Myklebust if (nfsd_minorversion(nn, minor, cmd) < 0) 579abcb4dacSNeilBrown return -EINVAL; 580e333f3bbSTrond Myklebust } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) { 581abcb4dacSNeilBrown /* 582abcb4dacSNeilBrown * Either we have +4 and no minors are enabled, 583abcb4dacSNeilBrown * or we have -4 and at least one minor is enabled. 584abcb4dacSNeilBrown * In either case, propagate 'cmd' to all minors. 585abcb4dacSNeilBrown */ 586abcb4dacSNeilBrown minor = 0; 587e333f3bbSTrond Myklebust while (nfsd_minorversion(nn, minor, cmd) >= 0) 588abcb4dacSNeilBrown minor++; 589abcb4dacSNeilBrown } 59070c3b76cSNeilBrown break; 59170c3b76cSNeilBrown default: 5928e823bafSJeff Layton /* Ignore requests to disable non-existent versions */ 5938e823bafSJeff Layton if (cmd == NFSD_SET) 59470c3b76cSNeilBrown return -EINVAL; 59570c3b76cSNeilBrown } 59670c3b76cSNeilBrown vers += len + 1; 59770c3b76cSNeilBrown } while ((len = qword_get(&mesg, vers, size)) > 0); 59870c3b76cSNeilBrown /* If all get turned off, turn them back on, as 59970c3b76cSNeilBrown * having no versions is BAD 60070c3b76cSNeilBrown */ 601e333f3bbSTrond Myklebust nfsd_reset_versions(nn); 60270c3b76cSNeilBrown } 603261758b5SChuck Lever 60470c3b76cSNeilBrown /* Now write current state into reply buffer */ 60570c3b76cSNeilBrown sep = ""; 606261758b5SChuck Lever remaining = SIMPLE_TRANSACTION_LIMIT; 607ff7d1179STrond Myklebust for (num=2 ; num <= 4 ; num++) { 608abcb4dacSNeilBrown int minor; 609e333f3bbSTrond Myklebust if (!nfsd_vers(nn, num, NFSD_AVAIL)) 610e35659f1SNeilBrown continue; 611abcb4dacSNeilBrown 612abcb4dacSNeilBrown minor = -1; 613ff7d1179STrond Myklebust do { 614e333f3bbSTrond Myklebust len = nfsd_print_version_support(nn, buf, remaining, 615ff7d1179STrond Myklebust sep, num, minor); 616818f2f57SDan Carpenter if (len >= remaining) 617ff7d1179STrond Myklebust goto out; 618261758b5SChuck Lever remaining -= len; 619261758b5SChuck Lever buf += len; 620261758b5SChuck Lever tlen += len; 621ff7d1179STrond Myklebust minor++; 622abcb4dacSNeilBrown if (len) 623ff7d1179STrond Myklebust sep = " "; 624ff7d1179STrond Myklebust } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION); 625261758b5SChuck Lever } 626ff7d1179STrond Myklebust out: 627261758b5SChuck Lever len = snprintf(buf, remaining, "\n"); 628818f2f57SDan Carpenter if (len >= remaining) 629261758b5SChuck Lever return -EINVAL; 630261758b5SChuck Lever return tlen + len; 63170c3b76cSNeilBrown } 63270c3b76cSNeilBrown 633f2453978SChuck Lever /* 634262a0982SChuck Lever * write_versions - Set or report the available NFS protocol versions 635262a0982SChuck Lever * 636262a0982SChuck Lever * Input: 637262a0982SChuck Lever * buf: ignored 638262a0982SChuck Lever * size: zero 639262a0982SChuck Lever * Output: 640262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 641262a0982SChuck Lever * string containing positive or negative integer 642262a0982SChuck Lever * values representing the current status of each 643262a0982SChuck Lever * protocol version; 644262a0982SChuck Lever * return code is the size in bytes of the string 645262a0982SChuck Lever * On error: return code is zero or a negative errno value 646262a0982SChuck Lever * 647262a0982SChuck Lever * OR 648262a0982SChuck Lever * 649262a0982SChuck Lever * Input: 650262a0982SChuck Lever * buf: C string containing whitespace- 651262a0982SChuck Lever * separated positive or negative 652262a0982SChuck Lever * integer values representing NFS 653262a0982SChuck Lever * protocol versions to enable ("+n") 654262a0982SChuck Lever * or disable ("-n") 655262a0982SChuck Lever * size: non-zero length of C string in @buf 656262a0982SChuck Lever * Output: 657262a0982SChuck Lever * On success: status of zero or more protocol versions has 658262a0982SChuck Lever * been updated; passed-in buffer filled with 659262a0982SChuck Lever * '\n'-terminated C string containing positive 660262a0982SChuck Lever * or negative integer values representing the 661262a0982SChuck Lever * current status of each protocol version; 662262a0982SChuck Lever * return code is the size in bytes of the string 663262a0982SChuck Lever * On error: return code is zero or a negative errno value 664262a0982SChuck Lever */ 6653dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size) 6663dd98a3bSJeff Layton { 6673dd98a3bSJeff Layton ssize_t rv; 6683dd98a3bSJeff Layton 6693dd98a3bSJeff Layton mutex_lock(&nfsd_mutex); 6703dd98a3bSJeff Layton rv = __write_versions(file, buf, size); 6713dd98a3bSJeff Layton mutex_unlock(&nfsd_mutex); 6723dd98a3bSJeff Layton return rv; 6733dd98a3bSJeff Layton } 6743dd98a3bSJeff Layton 6754cd5dc75SChuck Lever /* 6760a5372d8SChuck Lever * Zero-length write. Return a list of NFSD's current listener 6770a5372d8SChuck Lever * transports. 6780a5372d8SChuck Lever */ 6799dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net) 6800a5372d8SChuck Lever { 6819dd9845fSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 6829dd9845fSStanislav Kinsbursky 6839dd9845fSStanislav Kinsbursky if (nn->nfsd_serv == NULL) 6840a5372d8SChuck Lever return 0; 6859dd9845fSStanislav Kinsbursky return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); 6860a5372d8SChuck Lever } 6870a5372d8SChuck Lever 6880a5372d8SChuck Lever /* 6890b7c2f6fSChuck Lever * A single 'fd' number was written, in which case it must be for 6900b7c2f6fSChuck Lever * a socket of a supported family/protocol, and we use it as an 6910b7c2f6fSChuck Lever * nfsd listener. 6920b7c2f6fSChuck Lever */ 6934df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred) 6940b7c2f6fSChuck Lever { 6950b7c2f6fSChuck Lever char *mesg = buf; 6960b7c2f6fSChuck Lever int fd, err; 6979dd9845fSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 69864e63041SJeff Layton struct svc_serv *serv; 6990b7c2f6fSChuck Lever 7000b7c2f6fSChuck Lever err = get_int(&mesg, &fd); 7010b7c2f6fSChuck Lever if (err != 0 || fd < 0) 7020b7c2f6fSChuck Lever return -EINVAL; 70339d432fcSChuck Lever trace_nfsd_ctl_ports_addfd(net, fd); 7040b7c2f6fSChuck Lever 7056777436bSStanislav Kinsbursky err = nfsd_create_serv(net); 7060b7c2f6fSChuck Lever if (err != 0) 7070b7c2f6fSChuck Lever return err; 7080b7c2f6fSChuck Lever 70964e63041SJeff Layton serv = nn->nfsd_serv; 71064e63041SJeff Layton err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); 711ea068badSChuck Lever 7121e3577a4SNeilBrown if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 71317419aefSNeilBrown nfsd_destroy_serv(net); 71417419aefSNeilBrown 715ea068badSChuck Lever return err; 7160b7c2f6fSChuck Lever } 7170b7c2f6fSChuck Lever 7180b7c2f6fSChuck Lever /* 719442a6290SChuck Lever * A transport listener is added by writing its transport name and 7204eb68c26SChuck Lever * a port number. 7214eb68c26SChuck Lever */ 7224df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred) 7234eb68c26SChuck Lever { 7244eb68c26SChuck Lever char transport[16]; 72537498292SChuck Lever struct svc_xprt *xprt; 7264eb68c26SChuck Lever int port, err; 7279dd9845fSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 72864e63041SJeff Layton struct svc_serv *serv; 7294eb68c26SChuck Lever 730a10fded1SJ. Bruce Fields if (sscanf(buf, "%15s %5u", transport, &port) != 2) 7314eb68c26SChuck Lever return -EINVAL; 7324eb68c26SChuck Lever 7334be929beSAlexey Dobriyan if (port < 1 || port > USHRT_MAX) 7344eb68c26SChuck Lever return -EINVAL; 73539d432fcSChuck Lever trace_nfsd_ctl_ports_addxprt(net, transport, port); 7364eb68c26SChuck Lever 7376777436bSStanislav Kinsbursky err = nfsd_create_serv(net); 7384eb68c26SChuck Lever if (err != 0) 7394eb68c26SChuck Lever return err; 7404eb68c26SChuck Lever 74164e63041SJeff Layton serv = nn->nfsd_serv; 74264e63041SJeff Layton err = svc_xprt_create(serv, transport, net, 7434df493a2STrond Myklebust PF_INET, port, SVC_SOCK_ANONYMOUS, cred); 74468717908SChuck Lever if (err < 0) 74537498292SChuck Lever goto out_err; 74637498292SChuck Lever 74764e63041SJeff Layton err = svc_xprt_create(serv, transport, net, 7484df493a2STrond Myklebust PF_INET6, port, SVC_SOCK_ANONYMOUS, cred); 74937498292SChuck Lever if (err < 0 && err != -EAFNOSUPPORT) 75037498292SChuck Lever goto out_close; 7510cd14a06SJeff Layton 7524eb68c26SChuck Lever return 0; 75337498292SChuck Lever out_close: 75464e63041SJeff Layton xprt = svc_find_xprt(serv, transport, net, PF_INET, port); 75537498292SChuck Lever if (xprt != NULL) { 7564355d767SChuck Lever svc_xprt_close(xprt); 75737498292SChuck Lever svc_xprt_put(xprt); 75837498292SChuck Lever } 75937498292SChuck Lever out_err: 7601e3577a4SNeilBrown if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 76117419aefSNeilBrown nfsd_destroy_serv(net); 7622a501f55SNeilBrown 76337498292SChuck Lever return err; 7644eb68c26SChuck Lever } 7654eb68c26SChuck Lever 76608160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size, 76708160352SStanislav Kinsbursky struct net *net) 76880212d59SNeilBrown { 7690a5372d8SChuck Lever if (size == 0) 7709dd9845fSStanislav Kinsbursky return __write_ports_names(buf, net); 7710b7c2f6fSChuck Lever 7720b7c2f6fSChuck Lever if (isdigit(buf[0])) 7734df493a2STrond Myklebust return __write_ports_addfd(buf, net, file->f_cred); 77482d56591SChuck Lever 7754eb68c26SChuck Lever if (isalpha(buf[0])) 7764df493a2STrond Myklebust return __write_ports_addxprt(buf, net, file->f_cred); 7774cd5dc75SChuck Lever 778b41b66d6SNeilBrown return -EINVAL; 779b41b66d6SNeilBrown } 78080212d59SNeilBrown 781f2453978SChuck Lever /* 782262a0982SChuck Lever * write_ports - Pass a socket file descriptor or transport name to listen on 783262a0982SChuck Lever * 784262a0982SChuck Lever * Input: 785262a0982SChuck Lever * buf: ignored 786262a0982SChuck Lever * size: zero 787262a0982SChuck Lever * Output: 788262a0982SChuck Lever * On success: passed-in buffer filled with a '\n'-terminated C 789262a0982SChuck Lever * string containing a whitespace-separated list of 790262a0982SChuck Lever * named NFSD listeners; 791262a0982SChuck Lever * return code is the size in bytes of the string 792262a0982SChuck Lever * On error: return code is zero or a negative errno value 793262a0982SChuck Lever * 794262a0982SChuck Lever * OR 795262a0982SChuck Lever * 796262a0982SChuck Lever * Input: 797262a0982SChuck Lever * buf: C string containing an unsigned 798262a0982SChuck Lever * integer value representing a bound 799262a0982SChuck Lever * but unconnected socket that is to be 800c71206a7SChuck Lever * used as an NFSD listener; listen(3) 801c71206a7SChuck Lever * must be called for a SOCK_STREAM 802c71206a7SChuck Lever * socket, otherwise it is ignored 803262a0982SChuck Lever * size: non-zero length of C string in @buf 804262a0982SChuck Lever * Output: 805262a0982SChuck Lever * On success: NFS service is started; 806262a0982SChuck Lever * passed-in buffer filled with a '\n'-terminated C 807262a0982SChuck Lever * string containing a unique alphanumeric name of 808262a0982SChuck Lever * the listener; 809262a0982SChuck Lever * return code is the size in bytes of the string 810262a0982SChuck Lever * On error: return code is a negative errno value 811262a0982SChuck Lever * 812262a0982SChuck Lever * OR 813262a0982SChuck Lever * 814262a0982SChuck Lever * Input: 815262a0982SChuck Lever * buf: C string containing a transport 816262a0982SChuck Lever * name and an unsigned integer value 817262a0982SChuck Lever * representing the port to listen on, 818262a0982SChuck Lever * separated by whitespace 819262a0982SChuck Lever * size: non-zero length of C string in @buf 820262a0982SChuck Lever * Output: 821262a0982SChuck Lever * On success: returns zero; NFS service is started 822262a0982SChuck Lever * On error: return code is a negative errno value 823262a0982SChuck Lever */ 824bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size) 825bedbdd8bSNeil Brown { 826bedbdd8bSNeil Brown ssize_t rv; 8273dd98a3bSJeff Layton 828bedbdd8bSNeil Brown mutex_lock(&nfsd_mutex); 829244c7d44SAl Viro rv = __write_ports(file, buf, size, netns(file)); 830bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 831bedbdd8bSNeil Brown return rv; 832bedbdd8bSNeil Brown } 833bedbdd8bSNeil Brown 834bedbdd8bSNeil Brown 835596bbe53SNeilBrown int nfsd_max_blksize; 836596bbe53SNeilBrown 837f2453978SChuck Lever /* 838262a0982SChuck Lever * write_maxblksize - Set or report the current NFS blksize 839262a0982SChuck Lever * 840262a0982SChuck Lever * Input: 841262a0982SChuck Lever * buf: ignored 842262a0982SChuck Lever * size: zero 843262a0982SChuck Lever * 844262a0982SChuck Lever * OR 845262a0982SChuck Lever * 846262a0982SChuck Lever * Input: 847262a0982SChuck Lever * buf: C string containing an unsigned 848262a0982SChuck Lever * integer value representing the new 849262a0982SChuck Lever * NFS blksize 850262a0982SChuck Lever * size: non-zero length of C string in @buf 851262a0982SChuck Lever * Output: 852262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C string 853262a0982SChuck Lever * containing numeric value of the current NFS blksize 854262a0982SChuck Lever * setting; 855262a0982SChuck Lever * return code is the size in bytes of the string 856262a0982SChuck Lever * On error: return code is zero or a negative errno value 857262a0982SChuck Lever */ 858596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size) 859596bbe53SNeilBrown { 860596bbe53SNeilBrown char *mesg = buf; 861244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 8629dd9845fSStanislav Kinsbursky 863596bbe53SNeilBrown if (size > 0) { 864596bbe53SNeilBrown int bsize; 865596bbe53SNeilBrown int rv = get_int(&mesg, &bsize); 866596bbe53SNeilBrown if (rv) 867596bbe53SNeilBrown return rv; 86839d432fcSChuck Lever trace_nfsd_ctl_maxblksize(netns(file), bsize); 86939d432fcSChuck Lever 870596bbe53SNeilBrown /* force bsize into allowed range and 871596bbe53SNeilBrown * required alignment. 872596bbe53SNeilBrown */ 8733c7aa15dSKinglong Mee bsize = max_t(int, bsize, 1024); 8743c7aa15dSKinglong Mee bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE); 875596bbe53SNeilBrown bsize &= ~(1024-1); 876bedbdd8bSNeil Brown mutex_lock(&nfsd_mutex); 8779dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) { 878bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 879596bbe53SNeilBrown return -EBUSY; 880596bbe53SNeilBrown } 881596bbe53SNeilBrown nfsd_max_blksize = bsize; 882bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 883596bbe53SNeilBrown } 884e06b6405SChuck Lever 885e06b6405SChuck Lever return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", 886e06b6405SChuck Lever nfsd_max_blksize); 887596bbe53SNeilBrown } 888596bbe53SNeilBrown 889f2453978SChuck Lever /* 8905b8db00bSJeff Layton * write_maxconn - Set or report the current max number of connections 8915b8db00bSJeff Layton * 8925b8db00bSJeff Layton * Input: 8935b8db00bSJeff Layton * buf: ignored 8945b8db00bSJeff Layton * size: zero 8955b8db00bSJeff Layton * OR 8965b8db00bSJeff Layton * 8975b8db00bSJeff Layton * Input: 8985b8db00bSJeff Layton * buf: C string containing an unsigned 8995b8db00bSJeff Layton * integer value representing the new 9005b8db00bSJeff Layton * number of max connections 9015b8db00bSJeff Layton * size: non-zero length of C string in @buf 9025b8db00bSJeff Layton * Output: 9035b8db00bSJeff Layton * On success: passed-in buffer filled with '\n'-terminated C string 9045b8db00bSJeff Layton * containing numeric value of max_connections setting 9055b8db00bSJeff Layton * for this net namespace; 9065b8db00bSJeff Layton * return code is the size in bytes of the string 9075b8db00bSJeff Layton * On error: return code is zero or a negative errno value 9085b8db00bSJeff Layton */ 9095b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size) 9105b8db00bSJeff Layton { 9115b8db00bSJeff Layton char *mesg = buf; 912244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 9135b8db00bSJeff Layton unsigned int maxconn = nn->max_connections; 9145b8db00bSJeff Layton 9155b8db00bSJeff Layton if (size > 0) { 9165b8db00bSJeff Layton int rv = get_uint(&mesg, &maxconn); 9175b8db00bSJeff Layton 9185b8db00bSJeff Layton if (rv) 9195b8db00bSJeff Layton return rv; 92039d432fcSChuck Lever trace_nfsd_ctl_maxconn(netns(file), maxconn); 9215b8db00bSJeff Layton nn->max_connections = maxconn; 9225b8db00bSJeff Layton } 9235b8db00bSJeff Layton 9245b8db00bSJeff Layton return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn); 9255b8db00bSJeff Layton } 9265b8db00bSJeff Layton 92770c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 9289dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, 92920b7d86fSArnd Bergmann time64_t *time, struct nfsd_net *nn) 9301da177e4SLinus Torvalds { 93139d432fcSChuck Lever struct dentry *dentry = file_dentry(file); 9321da177e4SLinus Torvalds char *mesg = buf; 933f0135740SJ. Bruce Fields int rv, i; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds if (size > 0) { 9369dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) 9373dd98a3bSJeff Layton return -EBUSY; 938f0135740SJ. Bruce Fields rv = get_int(&mesg, &i); 9391da177e4SLinus Torvalds if (rv) 9401da177e4SLinus Torvalds return rv; 94139d432fcSChuck Lever trace_nfsd_ctl_time(netns(file), dentry->d_name.name, 94239d432fcSChuck Lever dentry->d_name.len, i); 94339d432fcSChuck Lever 944e7b184f1SJ. Bruce Fields /* 945e7b184f1SJ. Bruce Fields * Some sanity checking. We don't have a reason for 946e7b184f1SJ. Bruce Fields * these particular numbers, but problems with the 947e7b184f1SJ. Bruce Fields * extremes are: 948e7b184f1SJ. Bruce Fields * - Too short: the briefest network outage may 949e7b184f1SJ. Bruce Fields * cause clients to lose all their locks. Also, 950e7b184f1SJ. Bruce Fields * the frequent polling may be wasteful. 951e7b184f1SJ. Bruce Fields * - Too long: do you really want reboot recovery 952e7b184f1SJ. Bruce Fields * to take more than an hour? Or to make other 953e7b184f1SJ. Bruce Fields * clients wait an hour before being able to 954e7b184f1SJ. Bruce Fields * revoke a dead client's locks? 955e7b184f1SJ. Bruce Fields */ 956f0135740SJ. Bruce Fields if (i < 10 || i > 3600) 9571da177e4SLinus Torvalds return -EINVAL; 958f0135740SJ. Bruce Fields *time = i; 9591da177e4SLinus Torvalds } 960e06b6405SChuck Lever 96120b7d86fSArnd Bergmann return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time); 962f0135740SJ. Bruce Fields } 963f0135740SJ. Bruce Fields 9649dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, 96520b7d86fSArnd Bergmann time64_t *time, struct nfsd_net *nn) 966f0135740SJ. Bruce Fields { 967f0135740SJ. Bruce Fields ssize_t rv; 968f0135740SJ. Bruce Fields 969f0135740SJ. Bruce Fields mutex_lock(&nfsd_mutex); 9709dd9845fSStanislav Kinsbursky rv = __nfsd4_write_time(file, buf, size, time, nn); 971f0135740SJ. Bruce Fields mutex_unlock(&nfsd_mutex); 972f0135740SJ. Bruce Fields return rv; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 975f2453978SChuck Lever /* 976262a0982SChuck Lever * write_leasetime - Set or report the current NFSv4 lease time 977262a0982SChuck Lever * 978262a0982SChuck Lever * Input: 979262a0982SChuck Lever * buf: ignored 980262a0982SChuck Lever * size: zero 981262a0982SChuck Lever * 982262a0982SChuck Lever * OR 983262a0982SChuck Lever * 984262a0982SChuck Lever * Input: 985262a0982SChuck Lever * buf: C string containing an unsigned 986262a0982SChuck Lever * integer value representing the new 987262a0982SChuck Lever * NFSv4 lease expiry time 988262a0982SChuck Lever * size: non-zero length of C string in @buf 989262a0982SChuck Lever * Output: 990262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 991262a0982SChuck Lever * string containing unsigned integer value of the 992262a0982SChuck Lever * current lease expiry time; 993262a0982SChuck Lever * return code is the size in bytes of the string 994262a0982SChuck Lever * On error: return code is zero or a negative errno value 995262a0982SChuck Lever */ 9963dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size) 9973dd98a3bSJeff Layton { 998244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 9999dd9845fSStanislav Kinsbursky return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn); 10003dd98a3bSJeff Layton } 10013dd98a3bSJeff Layton 1002f2453978SChuck Lever /* 1003efc4bb4fSJ. Bruce Fields * write_gracetime - Set or report current NFSv4 grace period time 1004efc4bb4fSJ. Bruce Fields * 1005efc4bb4fSJ. Bruce Fields * As above, but sets the time of the NFSv4 grace period. 1006efc4bb4fSJ. Bruce Fields * 1007efc4bb4fSJ. Bruce Fields * Note this should never be set to less than the *previous* 1008efc4bb4fSJ. Bruce Fields * lease-period time, but we don't try to enforce this. (In the common 1009efc4bb4fSJ. Bruce Fields * case (a new boot), we don't know what the previous lease time was 1010efc4bb4fSJ. Bruce Fields * anyway.) 1011efc4bb4fSJ. Bruce Fields */ 1012efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size) 1013efc4bb4fSJ. Bruce Fields { 1014244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 10159dd9845fSStanislav Kinsbursky return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn); 1016efc4bb4fSJ. Bruce Fields } 1017efc4bb4fSJ. Bruce Fields 101874fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 10199dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size, 10209dd9845fSStanislav Kinsbursky struct nfsd_net *nn) 10210964a3d3SNeilBrown { 10220964a3d3SNeilBrown char *mesg = buf; 10230964a3d3SNeilBrown char *recdir; 10240964a3d3SNeilBrown int len, status; 10250964a3d3SNeilBrown 10263dd98a3bSJeff Layton if (size > 0) { 10279dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) 10283dd98a3bSJeff Layton return -EBUSY; 10293dd98a3bSJeff Layton if (size > PATH_MAX || buf[size-1] != '\n') 10300964a3d3SNeilBrown return -EINVAL; 10310964a3d3SNeilBrown buf[size-1] = 0; 10320964a3d3SNeilBrown 10330964a3d3SNeilBrown recdir = mesg; 10340964a3d3SNeilBrown len = qword_get(&mesg, recdir, size); 10350964a3d3SNeilBrown if (len <= 0) 10360964a3d3SNeilBrown return -EINVAL; 103739d432fcSChuck Lever trace_nfsd_ctl_recoverydir(netns(file), recdir); 10380964a3d3SNeilBrown 10390964a3d3SNeilBrown status = nfs4_reset_recoverydir(recdir); 104069049961SAndi Kleen if (status) 104169049961SAndi Kleen return status; 10423dd98a3bSJeff Layton } 10433d72ab8fSChuck Lever 10443d72ab8fSChuck Lever return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n", 10453d72ab8fSChuck Lever nfs4_recoverydir()); 10460964a3d3SNeilBrown } 10473dd98a3bSJeff Layton 1048f2453978SChuck Lever /* 1049262a0982SChuck Lever * write_recoverydir - Set or report the pathname of the recovery directory 1050262a0982SChuck Lever * 1051262a0982SChuck Lever * Input: 1052262a0982SChuck Lever * buf: ignored 1053262a0982SChuck Lever * size: zero 1054262a0982SChuck Lever * 1055262a0982SChuck Lever * OR 1056262a0982SChuck Lever * 1057262a0982SChuck Lever * Input: 1058262a0982SChuck Lever * buf: C string containing the pathname 1059262a0982SChuck Lever * of the directory on a local file 1060262a0982SChuck Lever * system containing permanent NFSv4 1061262a0982SChuck Lever * recovery data 1062262a0982SChuck Lever * size: non-zero length of C string in @buf 1063262a0982SChuck Lever * Output: 1064262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C string 1065262a0982SChuck Lever * containing the current recovery pathname setting; 1066262a0982SChuck Lever * return code is the size in bytes of the string 1067262a0982SChuck Lever * On error: return code is zero or a negative errno value 1068262a0982SChuck Lever */ 10693dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) 10703dd98a3bSJeff Layton { 10713dd98a3bSJeff Layton ssize_t rv; 1072244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 10733dd98a3bSJeff Layton 10743dd98a3bSJeff Layton mutex_lock(&nfsd_mutex); 10759dd9845fSStanislav Kinsbursky rv = __write_recoverydir(file, buf, size, nn); 10763dd98a3bSJeff Layton mutex_unlock(&nfsd_mutex); 10773dd98a3bSJeff Layton return rv; 10783dd98a3bSJeff Layton } 107974fd4873SJeff Layton #endif 10803dd98a3bSJeff Layton 1081f2453978SChuck Lever /* 10827f5ef2e9SJeff Layton * write_v4_end_grace - release grace period for nfsd's v4.x lock manager 10837f5ef2e9SJeff Layton * 10847f5ef2e9SJeff Layton * Input: 10857f5ef2e9SJeff Layton * buf: ignored 10867f5ef2e9SJeff Layton * size: zero 10877f5ef2e9SJeff Layton * OR 10887f5ef2e9SJeff Layton * 10897f5ef2e9SJeff Layton * Input: 10907f5ef2e9SJeff Layton * buf: any value 10917f5ef2e9SJeff Layton * size: non-zero length of C string in @buf 10927f5ef2e9SJeff Layton * Output: 10937f5ef2e9SJeff Layton * passed-in buffer filled with "Y" or "N" with a newline 10947f5ef2e9SJeff Layton * and NULL-terminated C string. This indicates whether 10957f5ef2e9SJeff Layton * the grace period has ended in the current net 10967f5ef2e9SJeff Layton * namespace. Return code is the size in bytes of the 10977f5ef2e9SJeff Layton * string. Writing a string that starts with 'Y', 'y', or 10987f5ef2e9SJeff Layton * '1' to the file will end the grace period for nfsd's v4 10997f5ef2e9SJeff Layton * lock manager. 11007f5ef2e9SJeff Layton */ 11017f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) 11027f5ef2e9SJeff Layton { 1103244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 11047f5ef2e9SJeff Layton 11057f5ef2e9SJeff Layton if (size > 0) { 11067f5ef2e9SJeff Layton switch(buf[0]) { 11077f5ef2e9SJeff Layton case 'Y': 11087f5ef2e9SJeff Layton case 'y': 11097f5ef2e9SJeff Layton case '1': 1110dd838821SYihao Wu if (!nn->nfsd_serv) 111162a063b8SJ. Bruce Fields return -EBUSY; 111239d432fcSChuck Lever trace_nfsd_end_grace(netns(file)); 11138073a98eSChuck Lever nfsd4_end_grace(nn); 11147f5ef2e9SJeff Layton break; 11157f5ef2e9SJeff Layton default: 11167f5ef2e9SJeff Layton return -EINVAL; 11177f5ef2e9SJeff Layton } 11187f5ef2e9SJeff Layton } 11197f5ef2e9SJeff Layton 11207f5ef2e9SJeff Layton return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n", 11217f5ef2e9SJeff Layton nn->grace_ended ? 'Y' : 'N'); 11227f5ef2e9SJeff Layton } 11237f5ef2e9SJeff Layton 112470c3b76cSNeilBrown #endif 11250964a3d3SNeilBrown 11261da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/ 11271da177e4SLinus Torvalds /* 11281da177e4SLinus Torvalds * populating the filesystem. 11291da177e4SLinus Torvalds */ 11301da177e4SLinus Torvalds 1131e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */ 1132e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode) 1133e8a79fb1SJ. Bruce Fields { 1134e8a79fb1SJ. Bruce Fields struct inode *inode = new_inode(sb); 1135e8a79fb1SJ. Bruce Fields if (!inode) 1136e8a79fb1SJ. Bruce Fields return NULL; 1137e8a79fb1SJ. Bruce Fields /* Following advice from simple_fill_super documentation: */ 1138e8a79fb1SJ. Bruce Fields inode->i_ino = iunique(sb, NFSD_MaxReserved); 1139e8a79fb1SJ. Bruce Fields inode->i_mode = mode; 114011fec9b9SJeff Layton simple_inode_init_ts(inode); 1141e8a79fb1SJ. Bruce Fields switch (mode & S_IFMT) { 1142e8a79fb1SJ. Bruce Fields case S_IFDIR: 1143e8a79fb1SJ. Bruce Fields inode->i_fop = &simple_dir_operations; 1144e8a79fb1SJ. Bruce Fields inode->i_op = &simple_dir_inode_operations; 1145e8a79fb1SJ. Bruce Fields inc_nlink(inode); 114676c50eb7SGustavo A. R. Silva break; 11474df750c9SChuck Lever case S_IFLNK: 11484df750c9SChuck Lever inode->i_op = &simple_symlink_inode_operations; 11494df750c9SChuck Lever break; 1150e8a79fb1SJ. Bruce Fields default: 1151e8a79fb1SJ. Bruce Fields break; 1152e8a79fb1SJ. Bruce Fields } 1153e8a79fb1SJ. Bruce Fields return inode; 1154e8a79fb1SJ. Bruce Fields } 1155e8a79fb1SJ. Bruce Fields 1156bebd6997SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl) 1157e8a79fb1SJ. Bruce Fields { 1158e8a79fb1SJ. Bruce Fields struct inode *inode; 1159e8a79fb1SJ. Bruce Fields 1160e8a79fb1SJ. Bruce Fields inode = nfsd_get_inode(dir->i_sb, mode); 1161e8a79fb1SJ. Bruce Fields if (!inode) 1162e8a79fb1SJ. Bruce Fields return -ENOMEM; 1163bebd6997SJ. Bruce Fields if (ncl) { 1164bebd6997SJ. Bruce Fields inode->i_private = ncl; 1165bebd6997SJ. Bruce Fields kref_get(&ncl->cl_ref); 1166bebd6997SJ. Bruce Fields } 1167e8a79fb1SJ. Bruce Fields d_add(dentry, inode); 1168e8a79fb1SJ. Bruce Fields inc_nlink(dir); 1169e8a79fb1SJ. Bruce Fields fsnotify_mkdir(dir, dentry); 1170e8a79fb1SJ. Bruce Fields return 0; 1171e8a79fb1SJ. Bruce Fields } 1172e8a79fb1SJ. Bruce Fields 1173e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name) 1174e8a79fb1SJ. Bruce Fields { 1175e8a79fb1SJ. Bruce Fields struct inode *dir = parent->d_inode; 1176e8a79fb1SJ. Bruce Fields struct dentry *dentry; 1177e8a79fb1SJ. Bruce Fields int ret = -ENOMEM; 1178e8a79fb1SJ. Bruce Fields 1179e8a79fb1SJ. Bruce Fields inode_lock(dir); 1180e8a79fb1SJ. Bruce Fields dentry = d_alloc_name(parent, name); 1181e8a79fb1SJ. Bruce Fields if (!dentry) 1182e8a79fb1SJ. Bruce Fields goto out_err; 1183bebd6997SJ. Bruce Fields ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl); 1184e8a79fb1SJ. Bruce Fields if (ret) 1185e8a79fb1SJ. Bruce Fields goto out_err; 1186e8a79fb1SJ. Bruce Fields out: 1187e8a79fb1SJ. Bruce Fields inode_unlock(dir); 1188e8a79fb1SJ. Bruce Fields return dentry; 1189e8a79fb1SJ. Bruce Fields out_err: 1190d6846bfbSTetsuo Handa dput(dentry); 1191e8a79fb1SJ. Bruce Fields dentry = ERR_PTR(ret); 1192e8a79fb1SJ. Bruce Fields goto out; 1193e8a79fb1SJ. Bruce Fields } 1194e8a79fb1SJ. Bruce Fields 11954df750c9SChuck Lever #if IS_ENABLED(CONFIG_SUNRPC_GSS) 11964df750c9SChuck Lever static int __nfsd_symlink(struct inode *dir, struct dentry *dentry, 11974df750c9SChuck Lever umode_t mode, const char *content) 11984df750c9SChuck Lever { 11994df750c9SChuck Lever struct inode *inode; 12004df750c9SChuck Lever 12014df750c9SChuck Lever inode = nfsd_get_inode(dir->i_sb, mode); 12024df750c9SChuck Lever if (!inode) 12034df750c9SChuck Lever return -ENOMEM; 12044df750c9SChuck Lever 12054df750c9SChuck Lever inode->i_link = (char *)content; 12064df750c9SChuck Lever inode->i_size = strlen(content); 12074df750c9SChuck Lever 12084df750c9SChuck Lever d_add(dentry, inode); 12094df750c9SChuck Lever inc_nlink(dir); 12104df750c9SChuck Lever fsnotify_create(dir, dentry); 12114df750c9SChuck Lever return 0; 12124df750c9SChuck Lever } 12134df750c9SChuck Lever 12144df750c9SChuck Lever /* 12154df750c9SChuck Lever * @content is assumed to be a NUL-terminated string that lives 12164df750c9SChuck Lever * longer than the symlink itself. 12174df750c9SChuck Lever */ 121839d432fcSChuck Lever static void _nfsd_symlink(struct dentry *parent, const char *name, 12194df750c9SChuck Lever const char *content) 12204df750c9SChuck Lever { 12214df750c9SChuck Lever struct inode *dir = parent->d_inode; 12224df750c9SChuck Lever struct dentry *dentry; 12234b471a8bSChuck Lever int ret; 12244df750c9SChuck Lever 12254df750c9SChuck Lever inode_lock(dir); 12264df750c9SChuck Lever dentry = d_alloc_name(parent, name); 12274df750c9SChuck Lever if (!dentry) 12284b471a8bSChuck Lever goto out; 12294df750c9SChuck Lever ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content); 12304df750c9SChuck Lever if (ret) 12314b471a8bSChuck Lever dput(dentry); 12324df750c9SChuck Lever out: 12334df750c9SChuck Lever inode_unlock(dir); 12344df750c9SChuck Lever } 12354df750c9SChuck Lever #else 123639d432fcSChuck Lever static inline void _nfsd_symlink(struct dentry *parent, const char *name, 12374df750c9SChuck Lever const char *content) 12384df750c9SChuck Lever { 12394df750c9SChuck Lever } 12404df750c9SChuck Lever 12414df750c9SChuck Lever #endif 12424df750c9SChuck Lever 1243b7a14708SAl Viro static void clear_ncl(struct dentry *dentry) 1244e8a79fb1SJ. Bruce Fields { 1245b7a14708SAl Viro struct inode *inode = d_inode(dentry); 124697ad4031SJ. Bruce Fields struct nfsdfs_client *ncl = inode->i_private; 124797ad4031SJ. Bruce Fields 1248b7a14708SAl Viro spin_lock(&inode->i_lock); 124997ad4031SJ. Bruce Fields inode->i_private = NULL; 1250b7a14708SAl Viro spin_unlock(&inode->i_lock); 125197ad4031SJ. Bruce Fields kref_put(&ncl->cl_ref, ncl->cl_release); 125297ad4031SJ. Bruce Fields } 125397ad4031SJ. Bruce Fields 125497ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode) 125597ad4031SJ. Bruce Fields { 125697ad4031SJ. Bruce Fields struct nfsdfs_client *nc; 125797ad4031SJ. Bruce Fields 1258b7a14708SAl Viro spin_lock(&inode->i_lock); 1259b7a14708SAl Viro nc = inode->i_private; 1260b7a14708SAl Viro if (nc) 1261b7a14708SAl Viro kref_get(&nc->cl_ref); 1262b7a14708SAl Viro spin_unlock(&inode->i_lock); 126397ad4031SJ. Bruce Fields return nc; 126497ad4031SJ. Bruce Fields } 126597ad4031SJ. Bruce Fields 126697ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share 126797ad4031SJ. Bruce Fields * code instead. */ 126897ad4031SJ. Bruce Fields static int nfsdfs_create_files(struct dentry *root, 1269472d155aSNeilBrown const struct tree_descr *files, 1270b7a14708SAl Viro struct nfsdfs_client *ncl, 1271472d155aSNeilBrown struct dentry **fdentries) 127297ad4031SJ. Bruce Fields { 127397ad4031SJ. Bruce Fields struct inode *dir = d_inode(root); 127497ad4031SJ. Bruce Fields struct inode *inode; 127597ad4031SJ. Bruce Fields struct dentry *dentry; 127697ad4031SJ. Bruce Fields int i; 127797ad4031SJ. Bruce Fields 127897ad4031SJ. Bruce Fields inode_lock(dir); 127997ad4031SJ. Bruce Fields for (i = 0; files->name && files->name[0]; i++, files++) { 128097ad4031SJ. Bruce Fields dentry = d_alloc_name(root, files->name); 128197ad4031SJ. Bruce Fields if (!dentry) 128297ad4031SJ. Bruce Fields goto out; 128397ad4031SJ. Bruce Fields inode = nfsd_get_inode(d_inode(root)->i_sb, 128497ad4031SJ. Bruce Fields S_IFREG | files->mode); 128597ad4031SJ. Bruce Fields if (!inode) { 128697ad4031SJ. Bruce Fields dput(dentry); 128797ad4031SJ. Bruce Fields goto out; 128897ad4031SJ. Bruce Fields } 1289b7a14708SAl Viro kref_get(&ncl->cl_ref); 129097ad4031SJ. Bruce Fields inode->i_fop = files->ops; 1291b7a14708SAl Viro inode->i_private = ncl; 129297ad4031SJ. Bruce Fields d_add(dentry, inode); 129397ad4031SJ. Bruce Fields fsnotify_create(dir, dentry); 1294472d155aSNeilBrown if (fdentries) 1295472d155aSNeilBrown fdentries[i] = dentry; 129697ad4031SJ. Bruce Fields } 129797ad4031SJ. Bruce Fields inode_unlock(dir); 129897ad4031SJ. Bruce Fields return 0; 129997ad4031SJ. Bruce Fields out: 130097ad4031SJ. Bruce Fields inode_unlock(dir); 130197ad4031SJ. Bruce Fields return -ENOMEM; 130297ad4031SJ. Bruce Fields } 130397ad4031SJ. Bruce Fields 130497ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */ 130597ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn, 130697ad4031SJ. Bruce Fields struct nfsdfs_client *ncl, u32 id, 1307472d155aSNeilBrown const struct tree_descr *files, 1308472d155aSNeilBrown struct dentry **fdentries) 130997ad4031SJ. Bruce Fields { 131097ad4031SJ. Bruce Fields struct dentry *dentry; 1311e8a79fb1SJ. Bruce Fields char name[11]; 131297ad4031SJ. Bruce Fields int ret; 1313e8a79fb1SJ. Bruce Fields 1314bf5ed3e3SJ. Bruce Fields sprintf(name, "%u", id); 1315e8a79fb1SJ. Bruce Fields 131697ad4031SJ. Bruce Fields dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name); 131797ad4031SJ. Bruce Fields if (IS_ERR(dentry)) /* XXX: tossing errors? */ 131897ad4031SJ. Bruce Fields return NULL; 1319b7a14708SAl Viro ret = nfsdfs_create_files(dentry, files, ncl, fdentries); 132097ad4031SJ. Bruce Fields if (ret) { 132197ad4031SJ. Bruce Fields nfsd_client_rmdir(dentry); 132297ad4031SJ. Bruce Fields return NULL; 132397ad4031SJ. Bruce Fields } 132497ad4031SJ. Bruce Fields return dentry; 1325e8a79fb1SJ. Bruce Fields } 1326e8a79fb1SJ. Bruce Fields 1327e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */ 1328e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry) 1329e8a79fb1SJ. Bruce Fields { 1330b7a14708SAl Viro simple_recursive_removal(dentry, clear_ncl); 1331e8a79fb1SJ. Bruce Fields } 1332e8a79fb1SJ. Bruce Fields 133396a374a3SDavid Howells static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc) 13341da177e4SLinus Torvalds { 1335e8a79fb1SJ. Bruce Fields struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 1336e8a79fb1SJ. Bruce Fields nfsd_net_id); 1337e8a79fb1SJ. Bruce Fields struct dentry *dentry; 1338e8a79fb1SJ. Bruce Fields int ret; 1339e8a79fb1SJ. Bruce Fields 1340cda37124SEric Biggers static const struct tree_descr nfsd_files[] = { 134196d851c4SStanislav Kinsbursky [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO}, 134220ad856eSAmir Goldstein /* Per-export io stats use same ops as exports file */ 134320ad856eSAmir Goldstein [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO}, 1344e8e8753fSJ. Bruce Fields [NFSD_Export_features] = {"export_features", 13459beeaab8SChenXiaoSong &export_features_fops, S_IRUGO}, 13464373ea84SWendy Cheng [NFSD_FO_UnlockIP] = {"unlock_ip", 13474373ea84SWendy Cheng &transaction_ops, S_IWUSR|S_IRUSR}, 134817efa372SWendy Cheng [NFSD_FO_UnlockFS] = {"unlock_filesystem", 134917efa372SWendy Cheng &transaction_ops, S_IWUSR|S_IRUSR}, 13501da177e4SLinus Torvalds [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR}, 13511da177e4SLinus Torvalds [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1352eed2965aSGreg Banks [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR}, 135303cf6c9fSGreg Banks [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO}, 135464776611SChenXiaoSong [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", 135564776611SChenXiaoSong &nfsd_reply_cache_stats_fops, S_IRUGO}, 135670c3b76cSNeilBrown [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR}, 135780212d59SNeilBrown [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, 1358596bbe53SNeilBrown [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO}, 13595b8db00bSJeff Layton [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO}, 13601342f9ddSChenXiaoSong [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO}, 13611da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4 13621da177e4SLinus Torvalds [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1363efc4bb4fSJ. Bruce Fields [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR}, 13640770249bSNeilBrown #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 13650964a3d3SNeilBrown [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR}, 13660770249bSNeilBrown #endif 13677f5ef2e9SJeff Layton [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO}, 13681da177e4SLinus Torvalds #endif 13691da177e4SLinus Torvalds /* last one */ {""} 13701da177e4SLinus Torvalds }; 137196a374a3SDavid Howells 1372e8a79fb1SJ. Bruce Fields ret = simple_fill_super(sb, 0x6e667364, nfsd_files); 1373e8a79fb1SJ. Bruce Fields if (ret) 1374e8a79fb1SJ. Bruce Fields return ret; 137539d432fcSChuck Lever _nfsd_symlink(sb->s_root, "supported_krb5_enctypes", 13764df750c9SChuck Lever "/proc/net/rpc/gss_krb5_enctypes"); 1377e8a79fb1SJ. Bruce Fields dentry = nfsd_mkdir(sb->s_root, NULL, "clients"); 1378e8a79fb1SJ. Bruce Fields if (IS_ERR(dentry)) 1379e8a79fb1SJ. Bruce Fields return PTR_ERR(dentry); 1380e8a79fb1SJ. Bruce Fields nn->nfsd_client_dir = dentry; 1381e8a79fb1SJ. Bruce Fields return 0; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 138496a374a3SDavid Howells static int nfsd_fs_get_tree(struct fs_context *fc) 13851da177e4SLinus Torvalds { 1386533770ccSAl Viro return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns)); 138796a374a3SDavid Howells } 138896a374a3SDavid Howells 138996a374a3SDavid Howells static void nfsd_fs_free_fc(struct fs_context *fc) 139096a374a3SDavid Howells { 139196a374a3SDavid Howells if (fc->s_fs_info) 139296a374a3SDavid Howells put_net(fc->s_fs_info); 139396a374a3SDavid Howells } 139496a374a3SDavid Howells 139596a374a3SDavid Howells static const struct fs_context_operations nfsd_fs_context_ops = { 139696a374a3SDavid Howells .free = nfsd_fs_free_fc, 139796a374a3SDavid Howells .get_tree = nfsd_fs_get_tree, 139896a374a3SDavid Howells }; 139996a374a3SDavid Howells 140096a374a3SDavid Howells static int nfsd_init_fs_context(struct fs_context *fc) 140196a374a3SDavid Howells { 140296a374a3SDavid Howells put_user_ns(fc->user_ns); 140396a374a3SDavid Howells fc->user_ns = get_user_ns(fc->net_ns->user_ns); 140496a374a3SDavid Howells fc->ops = &nfsd_fs_context_ops; 140596a374a3SDavid Howells return 0; 140611f77942SStanislav Kinsbursky } 140711f77942SStanislav Kinsbursky 140811f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb) 140911f77942SStanislav Kinsbursky { 141011f77942SStanislav Kinsbursky struct net *net = sb->s_fs_info; 141111f77942SStanislav Kinsbursky 1412c6c7f2a8STrond Myklebust nfsd_shutdown_threads(net); 1413c6c7f2a8STrond Myklebust 141411f77942SStanislav Kinsbursky kill_litter_super(sb); 141511f77942SStanislav Kinsbursky put_net(net); 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = { 14191da177e4SLinus Torvalds .owner = THIS_MODULE, 14201da177e4SLinus Torvalds .name = "nfsd", 142196a374a3SDavid Howells .init_fs_context = nfsd_init_fs_context, 142211f77942SStanislav Kinsbursky .kill_sb = nfsd_umount, 14231da177e4SLinus Torvalds }; 14247f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd"); 14251da177e4SLinus Torvalds 1426e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS 1427340086daSTom Rix 1428340086daSTom Rix static int exports_proc_open(struct inode *inode, struct file *file) 1429340086daSTom Rix { 1430340086daSTom Rix return exports_net_open(current->nsproxy->net_ns, file); 1431340086daSTom Rix } 1432340086daSTom Rix 1433340086daSTom Rix static const struct proc_ops exports_proc_ops = { 1434340086daSTom Rix .proc_open = exports_proc_open, 1435340086daSTom Rix .proc_read = seq_read, 1436340086daSTom Rix .proc_lseek = seq_lseek, 1437340086daSTom Rix .proc_release = seq_release, 1438340086daSTom Rix }; 1439340086daSTom Rix 1440e331f606SJ. Bruce Fields static int create_proc_exports_entry(void) 1441e331f606SJ. Bruce Fields { 1442e331f606SJ. Bruce Fields struct proc_dir_entry *entry; 1443e331f606SJ. Bruce Fields 1444e331f606SJ. Bruce Fields entry = proc_mkdir("fs/nfs", NULL); 1445e331f606SJ. Bruce Fields if (!entry) 1446e331f606SJ. Bruce Fields return -ENOMEM; 144797a32539SAlexey Dobriyan entry = proc_create("exports", 0, entry, &exports_proc_ops); 1448ff7c4b36Sfanchaoting if (!entry) { 1449ff7c4b36Sfanchaoting remove_proc_entry("fs/nfs", NULL); 1450e331f606SJ. Bruce Fields return -ENOMEM; 1451ff7c4b36Sfanchaoting } 1452e331f606SJ. Bruce Fields return 0; 1453e331f606SJ. Bruce Fields } 1454e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */ 1455e331f606SJ. Bruce Fields static int create_proc_exports_entry(void) 1456e331f606SJ. Bruce Fields { 1457e331f606SJ. Bruce Fields return 0; 1458e331f606SJ. Bruce Fields } 1459e331f606SJ. Bruce Fields #endif 1460e331f606SJ. Bruce Fields 1461c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id; 14625717e012SStanislav Kinsbursky 14635e092be7SChuck Lever /** 1464bd9d6a3eSLorenzo Bianconi * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit 1465bd9d6a3eSLorenzo Bianconi * @cb: netlink metadata and command arguments 1466bd9d6a3eSLorenzo Bianconi * 1467bd9d6a3eSLorenzo Bianconi * Return values: 1468bd9d6a3eSLorenzo Bianconi * %0: The rpc_status_get command may proceed 1469bd9d6a3eSLorenzo Bianconi * %-ENODEV: There is no NFSD running in this namespace 1470bd9d6a3eSLorenzo Bianconi */ 147113727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb) 147213727f85SLorenzo Bianconi { 1473bd9d6a3eSLorenzo Bianconi struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id); 1474bd9d6a3eSLorenzo Bianconi int ret = -ENODEV; 1475bd9d6a3eSLorenzo Bianconi 1476bd9d6a3eSLorenzo Bianconi mutex_lock(&nfsd_mutex); 14771bd773b4SNeilBrown if (nn->nfsd_serv) 1478bd9d6a3eSLorenzo Bianconi ret = 0; 14791bd773b4SNeilBrown else 1480bd9d6a3eSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1481bd9d6a3eSLorenzo Bianconi 1482bd9d6a3eSLorenzo Bianconi return ret; 1483bd9d6a3eSLorenzo Bianconi } 1484bd9d6a3eSLorenzo Bianconi 1485bd9d6a3eSLorenzo Bianconi static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb, 1486bd9d6a3eSLorenzo Bianconi struct netlink_callback *cb, 1487bd9d6a3eSLorenzo Bianconi struct nfsd_genl_rqstp *rqstp) 1488bd9d6a3eSLorenzo Bianconi { 1489bd9d6a3eSLorenzo Bianconi void *hdr; 1490bd9d6a3eSLorenzo Bianconi u32 i; 1491bd9d6a3eSLorenzo Bianconi 1492bd9d6a3eSLorenzo Bianconi hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1493bd9d6a3eSLorenzo Bianconi &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET); 1494bd9d6a3eSLorenzo Bianconi if (!hdr) 1495bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1496bd9d6a3eSLorenzo Bianconi 1497bd9d6a3eSLorenzo Bianconi if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) || 1498bd9d6a3eSLorenzo Bianconi nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) || 1499bd9d6a3eSLorenzo Bianconi nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) || 1500bd9d6a3eSLorenzo Bianconi nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) || 1501bd9d6a3eSLorenzo Bianconi nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) || 1502bd9d6a3eSLorenzo Bianconi nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME, 1503bd9d6a3eSLorenzo Bianconi ktime_to_us(rqstp->rq_stime), 1504bd9d6a3eSLorenzo Bianconi NFSD_A_RPC_STATUS_PAD)) 1505bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1506bd9d6a3eSLorenzo Bianconi 1507bd9d6a3eSLorenzo Bianconi switch (rqstp->rq_saddr.sa_family) { 1508bd9d6a3eSLorenzo Bianconi case AF_INET: { 1509bd9d6a3eSLorenzo Bianconi const struct sockaddr_in *s_in, *d_in; 1510bd9d6a3eSLorenzo Bianconi 1511bd9d6a3eSLorenzo Bianconi s_in = (const struct sockaddr_in *)&rqstp->rq_saddr; 1512bd9d6a3eSLorenzo Bianconi d_in = (const struct sockaddr_in *)&rqstp->rq_daddr; 1513bd9d6a3eSLorenzo Bianconi if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4, 1514bd9d6a3eSLorenzo Bianconi s_in->sin_addr.s_addr) || 1515bd9d6a3eSLorenzo Bianconi nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4, 1516bd9d6a3eSLorenzo Bianconi d_in->sin_addr.s_addr) || 1517bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1518bd9d6a3eSLorenzo Bianconi s_in->sin_port) || 1519bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1520bd9d6a3eSLorenzo Bianconi d_in->sin_port)) 1521bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1522bd9d6a3eSLorenzo Bianconi break; 1523bd9d6a3eSLorenzo Bianconi } 1524bd9d6a3eSLorenzo Bianconi case AF_INET6: { 1525bd9d6a3eSLorenzo Bianconi const struct sockaddr_in6 *s_in, *d_in; 1526bd9d6a3eSLorenzo Bianconi 1527bd9d6a3eSLorenzo Bianconi s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr; 1528bd9d6a3eSLorenzo Bianconi d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr; 1529bd9d6a3eSLorenzo Bianconi if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6, 1530bd9d6a3eSLorenzo Bianconi &s_in->sin6_addr) || 1531bd9d6a3eSLorenzo Bianconi nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6, 1532bd9d6a3eSLorenzo Bianconi &d_in->sin6_addr) || 1533bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1534bd9d6a3eSLorenzo Bianconi s_in->sin6_port) || 1535bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1536bd9d6a3eSLorenzo Bianconi d_in->sin6_port)) 1537bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1538bd9d6a3eSLorenzo Bianconi break; 1539bd9d6a3eSLorenzo Bianconi } 1540bd9d6a3eSLorenzo Bianconi } 1541bd9d6a3eSLorenzo Bianconi 1542bd9d6a3eSLorenzo Bianconi for (i = 0; i < rqstp->rq_opcnt; i++) 1543bd9d6a3eSLorenzo Bianconi if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS, 1544bd9d6a3eSLorenzo Bianconi rqstp->rq_opnum[i])) 1545bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1546bd9d6a3eSLorenzo Bianconi 1547bd9d6a3eSLorenzo Bianconi genlmsg_end(skb, hdr); 154813727f85SLorenzo Bianconi return 0; 154913727f85SLorenzo Bianconi } 155013727f85SLorenzo Bianconi 1551bd9d6a3eSLorenzo Bianconi /** 1552bd9d6a3eSLorenzo Bianconi * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit 1553bd9d6a3eSLorenzo Bianconi * @skb: reply buffer 1554bd9d6a3eSLorenzo Bianconi * @cb: netlink metadata and command arguments 1555bd9d6a3eSLorenzo Bianconi * 1556bd9d6a3eSLorenzo Bianconi * Returns the size of the reply or a negative errno. 1557bd9d6a3eSLorenzo Bianconi */ 155813727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, 155913727f85SLorenzo Bianconi struct netlink_callback *cb) 156013727f85SLorenzo Bianconi { 1561bd9d6a3eSLorenzo Bianconi struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id); 1562bd9d6a3eSLorenzo Bianconi int i, ret, rqstp_index = 0; 1563bd9d6a3eSLorenzo Bianconi 1564bd9d6a3eSLorenzo Bianconi rcu_read_lock(); 1565bd9d6a3eSLorenzo Bianconi 1566bd9d6a3eSLorenzo Bianconi for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) { 1567bd9d6a3eSLorenzo Bianconi struct svc_rqst *rqstp; 1568bd9d6a3eSLorenzo Bianconi 1569bd9d6a3eSLorenzo Bianconi if (i < cb->args[0]) /* already consumed */ 1570bd9d6a3eSLorenzo Bianconi continue; 1571bd9d6a3eSLorenzo Bianconi 1572bd9d6a3eSLorenzo Bianconi rqstp_index = 0; 1573bd9d6a3eSLorenzo Bianconi list_for_each_entry_rcu(rqstp, 1574bd9d6a3eSLorenzo Bianconi &nn->nfsd_serv->sv_pools[i].sp_all_threads, 1575bd9d6a3eSLorenzo Bianconi rq_all) { 1576bd9d6a3eSLorenzo Bianconi struct nfsd_genl_rqstp genl_rqstp; 1577bd9d6a3eSLorenzo Bianconi unsigned int status_counter; 1578bd9d6a3eSLorenzo Bianconi 1579bd9d6a3eSLorenzo Bianconi if (rqstp_index++ < cb->args[1]) /* already consumed */ 1580bd9d6a3eSLorenzo Bianconi continue; 1581bd9d6a3eSLorenzo Bianconi /* 1582bd9d6a3eSLorenzo Bianconi * Acquire rq_status_counter before parsing the rqst 1583bd9d6a3eSLorenzo Bianconi * fields. rq_status_counter is set to an odd value in 1584bd9d6a3eSLorenzo Bianconi * order to notify the consumers the rqstp fields are 1585bd9d6a3eSLorenzo Bianconi * meaningful. 1586bd9d6a3eSLorenzo Bianconi */ 1587bd9d6a3eSLorenzo Bianconi status_counter = 1588bd9d6a3eSLorenzo Bianconi smp_load_acquire(&rqstp->rq_status_counter); 1589bd9d6a3eSLorenzo Bianconi if (!(status_counter & 1)) 1590bd9d6a3eSLorenzo Bianconi continue; 1591bd9d6a3eSLorenzo Bianconi 1592bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_xid = rqstp->rq_xid; 1593bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_flags = rqstp->rq_flags; 1594bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_vers = rqstp->rq_vers; 1595bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_prog = rqstp->rq_prog; 1596bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_proc = rqstp->rq_proc; 1597bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_stime = rqstp->rq_stime; 1598bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_opcnt = 0; 1599bd9d6a3eSLorenzo Bianconi memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp), 1600bd9d6a3eSLorenzo Bianconi sizeof(struct sockaddr)); 1601bd9d6a3eSLorenzo Bianconi memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp), 1602bd9d6a3eSLorenzo Bianconi sizeof(struct sockaddr)); 1603bd9d6a3eSLorenzo Bianconi 1604bd9d6a3eSLorenzo Bianconi #ifdef CONFIG_NFSD_V4 1605bd9d6a3eSLorenzo Bianconi if (rqstp->rq_vers == NFS4_VERSION && 1606bd9d6a3eSLorenzo Bianconi rqstp->rq_proc == NFSPROC4_COMPOUND) { 1607bd9d6a3eSLorenzo Bianconi /* NFSv4 compound */ 1608bd9d6a3eSLorenzo Bianconi struct nfsd4_compoundargs *args; 1609bd9d6a3eSLorenzo Bianconi int j; 1610bd9d6a3eSLorenzo Bianconi 1611bd9d6a3eSLorenzo Bianconi args = rqstp->rq_argp; 1612bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_opcnt = args->opcnt; 1613bd9d6a3eSLorenzo Bianconi for (j = 0; j < genl_rqstp.rq_opcnt; j++) 1614bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_opnum[j] = 1615bd9d6a3eSLorenzo Bianconi args->ops[j].opnum; 1616bd9d6a3eSLorenzo Bianconi } 1617bd9d6a3eSLorenzo Bianconi #endif /* CONFIG_NFSD_V4 */ 1618bd9d6a3eSLorenzo Bianconi 1619bd9d6a3eSLorenzo Bianconi /* 1620bd9d6a3eSLorenzo Bianconi * Acquire rq_status_counter before reporting the rqst 1621bd9d6a3eSLorenzo Bianconi * fields to the user. 1622bd9d6a3eSLorenzo Bianconi */ 1623bd9d6a3eSLorenzo Bianconi if (smp_load_acquire(&rqstp->rq_status_counter) != 1624bd9d6a3eSLorenzo Bianconi status_counter) 1625bd9d6a3eSLorenzo Bianconi continue; 1626bd9d6a3eSLorenzo Bianconi 1627bd9d6a3eSLorenzo Bianconi ret = nfsd_genl_rpc_status_compose_msg(skb, cb, 1628bd9d6a3eSLorenzo Bianconi &genl_rqstp); 1629bd9d6a3eSLorenzo Bianconi if (ret) 1630bd9d6a3eSLorenzo Bianconi goto out; 1631bd9d6a3eSLorenzo Bianconi } 163213727f85SLorenzo Bianconi } 163313727f85SLorenzo Bianconi 1634bd9d6a3eSLorenzo Bianconi cb->args[0] = i; 1635bd9d6a3eSLorenzo Bianconi cb->args[1] = rqstp_index; 1636bd9d6a3eSLorenzo Bianconi ret = skb->len; 1637bd9d6a3eSLorenzo Bianconi out: 1638bd9d6a3eSLorenzo Bianconi rcu_read_unlock(); 1639bd9d6a3eSLorenzo Bianconi 1640bd9d6a3eSLorenzo Bianconi return ret; 1641bd9d6a3eSLorenzo Bianconi } 1642bd9d6a3eSLorenzo Bianconi 1643bd9d6a3eSLorenzo Bianconi /** 1644bd9d6a3eSLorenzo Bianconi * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing 1645bd9d6a3eSLorenzo Bianconi * @cb: netlink metadata and command arguments 1646bd9d6a3eSLorenzo Bianconi * 1647bd9d6a3eSLorenzo Bianconi * Return values: 1648bd9d6a3eSLorenzo Bianconi * %0: Success 1649bd9d6a3eSLorenzo Bianconi */ 165013727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb) 165113727f85SLorenzo Bianconi { 1652bd9d6a3eSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1653bd9d6a3eSLorenzo Bianconi 165413727f85SLorenzo Bianconi return 0; 165513727f85SLorenzo Bianconi } 165613727f85SLorenzo Bianconi 16575e092be7SChuck Lever /** 1658924f4fb0SLorenzo Bianconi * nfsd_nl_threads_set_doit - set the number of running threads 1659924f4fb0SLorenzo Bianconi * @skb: reply buffer 1660924f4fb0SLorenzo Bianconi * @info: netlink metadata and command arguments 1661924f4fb0SLorenzo Bianconi * 1662924f4fb0SLorenzo Bianconi * Return 0 on success or a negative errno. 1663924f4fb0SLorenzo Bianconi */ 1664924f4fb0SLorenzo Bianconi int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info) 1665924f4fb0SLorenzo Bianconi { 1666924f4fb0SLorenzo Bianconi int nthreads = 0, count = 0, nrpools, ret = -EOPNOTSUPP, rem; 1667924f4fb0SLorenzo Bianconi struct net *net = genl_info_net(info); 1668924f4fb0SLorenzo Bianconi struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1669924f4fb0SLorenzo Bianconi const struct nlattr *attr; 1670924f4fb0SLorenzo Bianconi const char *scope = NULL; 1671924f4fb0SLorenzo Bianconi 1672924f4fb0SLorenzo Bianconi if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS)) 1673924f4fb0SLorenzo Bianconi return -EINVAL; 1674924f4fb0SLorenzo Bianconi 1675924f4fb0SLorenzo Bianconi /* count number of SERVER_THREADS values */ 1676924f4fb0SLorenzo Bianconi nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) { 1677924f4fb0SLorenzo Bianconi if (nla_type(attr) == NFSD_A_SERVER_THREADS) 1678924f4fb0SLorenzo Bianconi count++; 1679924f4fb0SLorenzo Bianconi } 1680924f4fb0SLorenzo Bianconi 1681924f4fb0SLorenzo Bianconi mutex_lock(&nfsd_mutex); 1682924f4fb0SLorenzo Bianconi 1683924f4fb0SLorenzo Bianconi nrpools = nfsd_nrpools(net); 1684924f4fb0SLorenzo Bianconi if (nrpools && count > nrpools) 1685924f4fb0SLorenzo Bianconi count = nrpools; 1686924f4fb0SLorenzo Bianconi 1687924f4fb0SLorenzo Bianconi /* XXX: make this handle non-global pool-modes */ 1688924f4fb0SLorenzo Bianconi if (count > 1) 1689924f4fb0SLorenzo Bianconi goto out_unlock; 1690924f4fb0SLorenzo Bianconi 1691924f4fb0SLorenzo Bianconi nthreads = nla_get_u32(info->attrs[NFSD_A_SERVER_THREADS]); 1692924f4fb0SLorenzo Bianconi if (info->attrs[NFSD_A_SERVER_GRACETIME] || 1693924f4fb0SLorenzo Bianconi info->attrs[NFSD_A_SERVER_LEASETIME] || 1694924f4fb0SLorenzo Bianconi info->attrs[NFSD_A_SERVER_SCOPE]) { 1695924f4fb0SLorenzo Bianconi ret = -EBUSY; 1696924f4fb0SLorenzo Bianconi if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads) 1697924f4fb0SLorenzo Bianconi goto out_unlock; 1698924f4fb0SLorenzo Bianconi 1699924f4fb0SLorenzo Bianconi ret = -EINVAL; 1700924f4fb0SLorenzo Bianconi attr = info->attrs[NFSD_A_SERVER_GRACETIME]; 1701924f4fb0SLorenzo Bianconi if (attr) { 1702924f4fb0SLorenzo Bianconi u32 gracetime = nla_get_u32(attr); 1703924f4fb0SLorenzo Bianconi 1704924f4fb0SLorenzo Bianconi if (gracetime < 10 || gracetime > 3600) 1705924f4fb0SLorenzo Bianconi goto out_unlock; 1706924f4fb0SLorenzo Bianconi 1707924f4fb0SLorenzo Bianconi nn->nfsd4_grace = gracetime; 1708924f4fb0SLorenzo Bianconi } 1709924f4fb0SLorenzo Bianconi 1710924f4fb0SLorenzo Bianconi attr = info->attrs[NFSD_A_SERVER_LEASETIME]; 1711924f4fb0SLorenzo Bianconi if (attr) { 1712924f4fb0SLorenzo Bianconi u32 leasetime = nla_get_u32(attr); 1713924f4fb0SLorenzo Bianconi 1714924f4fb0SLorenzo Bianconi if (leasetime < 10 || leasetime > 3600) 1715924f4fb0SLorenzo Bianconi goto out_unlock; 1716924f4fb0SLorenzo Bianconi 1717924f4fb0SLorenzo Bianconi nn->nfsd4_lease = leasetime; 1718924f4fb0SLorenzo Bianconi } 1719924f4fb0SLorenzo Bianconi 1720924f4fb0SLorenzo Bianconi attr = info->attrs[NFSD_A_SERVER_SCOPE]; 1721924f4fb0SLorenzo Bianconi if (attr) 1722924f4fb0SLorenzo Bianconi scope = nla_data(attr); 1723924f4fb0SLorenzo Bianconi } 1724924f4fb0SLorenzo Bianconi 1725924f4fb0SLorenzo Bianconi ret = nfsd_svc(nthreads, net, get_current_cred(), scope); 1726924f4fb0SLorenzo Bianconi 1727924f4fb0SLorenzo Bianconi out_unlock: 1728924f4fb0SLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1729924f4fb0SLorenzo Bianconi 1730924f4fb0SLorenzo Bianconi return ret == nthreads ? 0 : ret; 1731924f4fb0SLorenzo Bianconi } 1732924f4fb0SLorenzo Bianconi 1733924f4fb0SLorenzo Bianconi /** 1734924f4fb0SLorenzo Bianconi * nfsd_nl_threads_get_doit - get the number of running threads 1735924f4fb0SLorenzo Bianconi * @skb: reply buffer 1736924f4fb0SLorenzo Bianconi * @info: netlink metadata and command arguments 1737924f4fb0SLorenzo Bianconi * 1738924f4fb0SLorenzo Bianconi * Return 0 on success or a negative errno. 1739924f4fb0SLorenzo Bianconi */ 1740924f4fb0SLorenzo Bianconi int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info) 1741924f4fb0SLorenzo Bianconi { 1742924f4fb0SLorenzo Bianconi struct net *net = genl_info_net(info); 1743924f4fb0SLorenzo Bianconi struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1744924f4fb0SLorenzo Bianconi void *hdr; 1745924f4fb0SLorenzo Bianconi int err; 1746924f4fb0SLorenzo Bianconi 1747924f4fb0SLorenzo Bianconi skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1748924f4fb0SLorenzo Bianconi if (!skb) 1749924f4fb0SLorenzo Bianconi return -ENOMEM; 1750924f4fb0SLorenzo Bianconi 1751924f4fb0SLorenzo Bianconi hdr = genlmsg_iput(skb, info); 1752924f4fb0SLorenzo Bianconi if (!hdr) { 1753924f4fb0SLorenzo Bianconi err = -EMSGSIZE; 1754924f4fb0SLorenzo Bianconi goto err_free_msg; 1755924f4fb0SLorenzo Bianconi } 1756924f4fb0SLorenzo Bianconi 1757924f4fb0SLorenzo Bianconi mutex_lock(&nfsd_mutex); 1758924f4fb0SLorenzo Bianconi 1759924f4fb0SLorenzo Bianconi err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME, 1760924f4fb0SLorenzo Bianconi nn->nfsd4_grace) || 1761924f4fb0SLorenzo Bianconi nla_put_u32(skb, NFSD_A_SERVER_LEASETIME, 1762924f4fb0SLorenzo Bianconi nn->nfsd4_lease) || 1763924f4fb0SLorenzo Bianconi nla_put_string(skb, NFSD_A_SERVER_SCOPE, 1764924f4fb0SLorenzo Bianconi nn->nfsd_name); 1765924f4fb0SLorenzo Bianconi if (err) 1766924f4fb0SLorenzo Bianconi goto err_unlock; 1767924f4fb0SLorenzo Bianconi 1768924f4fb0SLorenzo Bianconi if (nn->nfsd_serv) { 1769924f4fb0SLorenzo Bianconi int i; 1770924f4fb0SLorenzo Bianconi 1771924f4fb0SLorenzo Bianconi for (i = 0; i < nfsd_nrpools(net); ++i) { 1772924f4fb0SLorenzo Bianconi struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i]; 1773924f4fb0SLorenzo Bianconi 1774924f4fb0SLorenzo Bianconi err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 1775924f4fb0SLorenzo Bianconi atomic_read(&sp->sp_nrthreads)); 1776924f4fb0SLorenzo Bianconi if (err) 1777924f4fb0SLorenzo Bianconi goto err_unlock; 1778924f4fb0SLorenzo Bianconi } 1779924f4fb0SLorenzo Bianconi } else { 1780924f4fb0SLorenzo Bianconi err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0); 1781924f4fb0SLorenzo Bianconi if (err) 1782924f4fb0SLorenzo Bianconi goto err_unlock; 1783924f4fb0SLorenzo Bianconi } 1784924f4fb0SLorenzo Bianconi 1785924f4fb0SLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1786924f4fb0SLorenzo Bianconi 1787924f4fb0SLorenzo Bianconi genlmsg_end(skb, hdr); 1788924f4fb0SLorenzo Bianconi 1789924f4fb0SLorenzo Bianconi return genlmsg_reply(skb, info); 1790924f4fb0SLorenzo Bianconi 1791924f4fb0SLorenzo Bianconi err_unlock: 1792924f4fb0SLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1793924f4fb0SLorenzo Bianconi err_free_msg: 1794924f4fb0SLorenzo Bianconi nlmsg_free(skb); 1795924f4fb0SLorenzo Bianconi 1796924f4fb0SLorenzo Bianconi return err; 1797924f4fb0SLorenzo Bianconi } 1798924f4fb0SLorenzo Bianconi 1799924f4fb0SLorenzo Bianconi /** 1800*5a939beaSLorenzo Bianconi * nfsd_nl_version_set_doit - set the nfs enabled versions 1801*5a939beaSLorenzo Bianconi * @skb: reply buffer 1802*5a939beaSLorenzo Bianconi * @info: netlink metadata and command arguments 1803*5a939beaSLorenzo Bianconi * 1804*5a939beaSLorenzo Bianconi * Return 0 on success or a negative errno. 1805*5a939beaSLorenzo Bianconi */ 1806*5a939beaSLorenzo Bianconi int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info) 1807*5a939beaSLorenzo Bianconi { 1808*5a939beaSLorenzo Bianconi const struct nlattr *attr; 1809*5a939beaSLorenzo Bianconi struct nfsd_net *nn; 1810*5a939beaSLorenzo Bianconi int i, rem; 1811*5a939beaSLorenzo Bianconi 1812*5a939beaSLorenzo Bianconi if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION)) 1813*5a939beaSLorenzo Bianconi return -EINVAL; 1814*5a939beaSLorenzo Bianconi 1815*5a939beaSLorenzo Bianconi mutex_lock(&nfsd_mutex); 1816*5a939beaSLorenzo Bianconi 1817*5a939beaSLorenzo Bianconi nn = net_generic(genl_info_net(info), nfsd_net_id); 1818*5a939beaSLorenzo Bianconi if (nn->nfsd_serv) { 1819*5a939beaSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1820*5a939beaSLorenzo Bianconi return -EBUSY; 1821*5a939beaSLorenzo Bianconi } 1822*5a939beaSLorenzo Bianconi 1823*5a939beaSLorenzo Bianconi /* clear current supported versions. */ 1824*5a939beaSLorenzo Bianconi nfsd_vers(nn, 2, NFSD_CLEAR); 1825*5a939beaSLorenzo Bianconi nfsd_vers(nn, 3, NFSD_CLEAR); 1826*5a939beaSLorenzo Bianconi for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) 1827*5a939beaSLorenzo Bianconi nfsd_minorversion(nn, i, NFSD_CLEAR); 1828*5a939beaSLorenzo Bianconi 1829*5a939beaSLorenzo Bianconi nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) { 1830*5a939beaSLorenzo Bianconi struct nlattr *tb[NFSD_A_VERSION_MAX + 1]; 1831*5a939beaSLorenzo Bianconi u32 major, minor = 0; 1832*5a939beaSLorenzo Bianconi bool enabled; 1833*5a939beaSLorenzo Bianconi 1834*5a939beaSLorenzo Bianconi if (nla_type(attr) != NFSD_A_SERVER_PROTO_VERSION) 1835*5a939beaSLorenzo Bianconi continue; 1836*5a939beaSLorenzo Bianconi 1837*5a939beaSLorenzo Bianconi if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr, 1838*5a939beaSLorenzo Bianconi nfsd_version_nl_policy, info->extack) < 0) 1839*5a939beaSLorenzo Bianconi continue; 1840*5a939beaSLorenzo Bianconi 1841*5a939beaSLorenzo Bianconi if (!tb[NFSD_A_VERSION_MAJOR]) 1842*5a939beaSLorenzo Bianconi continue; 1843*5a939beaSLorenzo Bianconi 1844*5a939beaSLorenzo Bianconi major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]); 1845*5a939beaSLorenzo Bianconi if (tb[NFSD_A_VERSION_MINOR]) 1846*5a939beaSLorenzo Bianconi minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]); 1847*5a939beaSLorenzo Bianconi 1848*5a939beaSLorenzo Bianconi enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]); 1849*5a939beaSLorenzo Bianconi 1850*5a939beaSLorenzo Bianconi switch (major) { 1851*5a939beaSLorenzo Bianconi case 4: 1852*5a939beaSLorenzo Bianconi nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR); 1853*5a939beaSLorenzo Bianconi break; 1854*5a939beaSLorenzo Bianconi case 3: 1855*5a939beaSLorenzo Bianconi case 2: 1856*5a939beaSLorenzo Bianconi if (!minor) 1857*5a939beaSLorenzo Bianconi nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR); 1858*5a939beaSLorenzo Bianconi break; 1859*5a939beaSLorenzo Bianconi default: 1860*5a939beaSLorenzo Bianconi break; 1861*5a939beaSLorenzo Bianconi } 1862*5a939beaSLorenzo Bianconi } 1863*5a939beaSLorenzo Bianconi 1864*5a939beaSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1865*5a939beaSLorenzo Bianconi 1866*5a939beaSLorenzo Bianconi return 0; 1867*5a939beaSLorenzo Bianconi } 1868*5a939beaSLorenzo Bianconi 1869*5a939beaSLorenzo Bianconi /** 1870*5a939beaSLorenzo Bianconi * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions 1871*5a939beaSLorenzo Bianconi * @skb: reply buffer 1872*5a939beaSLorenzo Bianconi * @info: netlink metadata and command arguments 1873*5a939beaSLorenzo Bianconi * 1874*5a939beaSLorenzo Bianconi * Return 0 on success or a negative errno. 1875*5a939beaSLorenzo Bianconi */ 1876*5a939beaSLorenzo Bianconi int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info) 1877*5a939beaSLorenzo Bianconi { 1878*5a939beaSLorenzo Bianconi struct nfsd_net *nn; 1879*5a939beaSLorenzo Bianconi int i, err; 1880*5a939beaSLorenzo Bianconi void *hdr; 1881*5a939beaSLorenzo Bianconi 1882*5a939beaSLorenzo Bianconi skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1883*5a939beaSLorenzo Bianconi if (!skb) 1884*5a939beaSLorenzo Bianconi return -ENOMEM; 1885*5a939beaSLorenzo Bianconi 1886*5a939beaSLorenzo Bianconi hdr = genlmsg_iput(skb, info); 1887*5a939beaSLorenzo Bianconi if (!hdr) { 1888*5a939beaSLorenzo Bianconi err = -EMSGSIZE; 1889*5a939beaSLorenzo Bianconi goto err_free_msg; 1890*5a939beaSLorenzo Bianconi } 1891*5a939beaSLorenzo Bianconi 1892*5a939beaSLorenzo Bianconi mutex_lock(&nfsd_mutex); 1893*5a939beaSLorenzo Bianconi nn = net_generic(genl_info_net(info), nfsd_net_id); 1894*5a939beaSLorenzo Bianconi 1895*5a939beaSLorenzo Bianconi for (i = 2; i <= 4; i++) { 1896*5a939beaSLorenzo Bianconi int j; 1897*5a939beaSLorenzo Bianconi 1898*5a939beaSLorenzo Bianconi for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) { 1899*5a939beaSLorenzo Bianconi struct nlattr *attr; 1900*5a939beaSLorenzo Bianconi 1901*5a939beaSLorenzo Bianconi /* Don't record any versions the kernel doesn't have 1902*5a939beaSLorenzo Bianconi * compiled in 1903*5a939beaSLorenzo Bianconi */ 1904*5a939beaSLorenzo Bianconi if (!nfsd_support_version(i)) 1905*5a939beaSLorenzo Bianconi continue; 1906*5a939beaSLorenzo Bianconi 1907*5a939beaSLorenzo Bianconi /* NFSv{2,3} does not support minor numbers */ 1908*5a939beaSLorenzo Bianconi if (i < 4 && j) 1909*5a939beaSLorenzo Bianconi continue; 1910*5a939beaSLorenzo Bianconi 1911*5a939beaSLorenzo Bianconi attr = nla_nest_start(skb, 1912*5a939beaSLorenzo Bianconi NFSD_A_SERVER_PROTO_VERSION); 1913*5a939beaSLorenzo Bianconi if (!attr) { 1914*5a939beaSLorenzo Bianconi err = -EINVAL; 1915*5a939beaSLorenzo Bianconi goto err_nfsd_unlock; 1916*5a939beaSLorenzo Bianconi } 1917*5a939beaSLorenzo Bianconi 1918*5a939beaSLorenzo Bianconi if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) || 1919*5a939beaSLorenzo Bianconi nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) { 1920*5a939beaSLorenzo Bianconi err = -EINVAL; 1921*5a939beaSLorenzo Bianconi goto err_nfsd_unlock; 1922*5a939beaSLorenzo Bianconi } 1923*5a939beaSLorenzo Bianconi 1924*5a939beaSLorenzo Bianconi /* Set the enabled flag if the version is enabled */ 1925*5a939beaSLorenzo Bianconi if (nfsd_vers(nn, i, NFSD_TEST) && 1926*5a939beaSLorenzo Bianconi (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) && 1927*5a939beaSLorenzo Bianconi nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) { 1928*5a939beaSLorenzo Bianconi err = -EINVAL; 1929*5a939beaSLorenzo Bianconi goto err_nfsd_unlock; 1930*5a939beaSLorenzo Bianconi } 1931*5a939beaSLorenzo Bianconi 1932*5a939beaSLorenzo Bianconi nla_nest_end(skb, attr); 1933*5a939beaSLorenzo Bianconi } 1934*5a939beaSLorenzo Bianconi } 1935*5a939beaSLorenzo Bianconi 1936*5a939beaSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1937*5a939beaSLorenzo Bianconi genlmsg_end(skb, hdr); 1938*5a939beaSLorenzo Bianconi 1939*5a939beaSLorenzo Bianconi return genlmsg_reply(skb, info); 1940*5a939beaSLorenzo Bianconi 1941*5a939beaSLorenzo Bianconi err_nfsd_unlock: 1942*5a939beaSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1943*5a939beaSLorenzo Bianconi err_free_msg: 1944*5a939beaSLorenzo Bianconi nlmsg_free(skb); 1945*5a939beaSLorenzo Bianconi 1946*5a939beaSLorenzo Bianconi return err; 1947*5a939beaSLorenzo Bianconi } 1948*5a939beaSLorenzo Bianconi 1949*5a939beaSLorenzo Bianconi /** 19505e092be7SChuck Lever * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace 19515e092be7SChuck Lever * @net: a freshly-created network namespace 19525e092be7SChuck Lever * 19535e092be7SChuck Lever * This information stays around as long as the network namespace is 19545e092be7SChuck Lever * alive whether or not there is an NFSD instance running in the 19555e092be7SChuck Lever * namespace. 19565e092be7SChuck Lever * 19575e092be7SChuck Lever * Returns zero on success, or a negative errno otherwise. 19585e092be7SChuck Lever */ 19595e092be7SChuck Lever static __net_init int nfsd_net_init(struct net *net) 19605717e012SStanislav Kinsbursky { 19615717e012SStanislav Kinsbursky int retval; 19623d733711SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 19635717e012SStanislav Kinsbursky 19645717e012SStanislav Kinsbursky retval = nfsd_export_init(net); 19655717e012SStanislav Kinsbursky if (retval) 19665717e012SStanislav Kinsbursky goto out_export_error; 1967f69adb2fSStanislav Kinsbursky retval = nfsd_idmap_init(net); 1968f69adb2fSStanislav Kinsbursky if (retval) 1969f69adb2fSStanislav Kinsbursky goto out_idmap_error; 19707d12cce8SKefeng Wang retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL, 19717d12cce8SKefeng Wang NFSD_STATS_COUNTERS_NUM); 1972ed9ab734SJeff Layton if (retval) 1973ed9ab734SJeff Layton goto out_repcache_error; 197416fb9808SJosef Bacik memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats)); 197516fb9808SJosef Bacik nn->nfsd_svcstats.program = &nfsd_program; 1976e333f3bbSTrond Myklebust nn->nfsd_versions = NULL; 1977e333f3bbSTrond Myklebust nn->nfsd4_minorversions = NULL; 1978f385f7d2SDai Ngo nfsd4_init_leases_net(nn); 197991d2e9b5SChuck Lever get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key)); 198091d2e9b5SChuck Lever seqlock_init(&nn->writeverf_lock); 198193483ac5SJosef Bacik nfsd_proc_stat_init(net); 19822c830dd7SJ. Bruce Fields 19835717e012SStanislav Kinsbursky return 0; 19845717e012SStanislav Kinsbursky 1985ed9ab734SJeff Layton out_repcache_error: 1986ed9ab734SJeff Layton nfsd_idmap_shutdown(net); 1987f69adb2fSStanislav Kinsbursky out_idmap_error: 1988f69adb2fSStanislav Kinsbursky nfsd_export_shutdown(net); 19895717e012SStanislav Kinsbursky out_export_error: 19905717e012SStanislav Kinsbursky return retval; 19915717e012SStanislav Kinsbursky } 19925717e012SStanislav Kinsbursky 19935e092be7SChuck Lever /** 19945e092be7SChuck Lever * nfsd_net_exit - Release the nfsd_net portion of a net namespace 19955e092be7SChuck Lever * @net: a network namespace that is about to be destroyed 19965e092be7SChuck Lever * 19975e092be7SChuck Lever */ 19985e092be7SChuck Lever static __net_exit void nfsd_net_exit(struct net *net) 19995717e012SStanislav Kinsbursky { 2000ed9ab734SJeff Layton struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2001ed9ab734SJeff Layton 200293483ac5SJosef Bacik nfsd_proc_stat_shutdown(net); 20037d12cce8SKefeng Wang percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM); 2004f69adb2fSStanislav Kinsbursky nfsd_idmap_shutdown(net); 20055717e012SStanislav Kinsbursky nfsd_export_shutdown(net); 2006ed9ab734SJeff Layton nfsd_netns_free_versions(nn); 20075717e012SStanislav Kinsbursky } 20085717e012SStanislav Kinsbursky 20097ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = { 20105e092be7SChuck Lever .init = nfsd_net_init, 20115e092be7SChuck Lever .exit = nfsd_net_exit, 20127ea34ac1SJeff Layton .id = &nfsd_net_id, 20137ea34ac1SJeff Layton .size = sizeof(struct nfsd_net), 20147ea34ac1SJeff Layton }; 20157ea34ac1SJeff Layton 20161da177e4SLinus Torvalds static int __init init_nfsd(void) 20171da177e4SLinus Torvalds { 20181da177e4SLinus Torvalds int retval; 20191da177e4SLinus Torvalds 2020bb7ffbf2SGiuseppe Cantavenera retval = nfsd4_init_slabs(); 2021bb7ffbf2SGiuseppe Cantavenera if (retval) 2022b10252c7SAlexander Sverdlin return retval; 20239cf514ccSChristoph Hellwig retval = nfsd4_init_pnfs(); 202465178db4SBryan Schumaker if (retval) 202565178db4SBryan Schumaker goto out_free_slabs; 2026027690c7SJ. Bruce Fields retval = nfsd_drc_slab_create(); 2027027690c7SJ. Bruce Fields if (retval) 20284b148854SJosef Bacik goto out_free_pnfs; 20291da177e4SLinus Torvalds nfsd_lockd_init(); /* lockd->nfsd callbacks */ 2030e331f606SJ. Bruce Fields retval = create_proc_exports_entry(); 2031e331f606SJ. Bruce Fields if (retval) 2032f69adb2fSStanislav Kinsbursky goto out_free_lockd; 2033bd5ae928SJ. Bruce Fields retval = register_pernet_subsys(&nfsd_net_ops); 2034bd5ae928SJ. Bruce Fields if (retval < 0) 20356f6f84aaSZhang Xiaoxu goto out_free_exports; 2036b10252c7SAlexander Sverdlin retval = register_cld_notifier(); 2037b10252c7SAlexander Sverdlin if (retval) 203862fdb65eSZhang Xiaoxu goto out_free_subsys; 2039d76cc46bSDai Ngo retval = nfsd4_create_laundry_wq(); 2040d76cc46bSDai Ngo if (retval) 20416f6f84aaSZhang Xiaoxu goto out_free_cld; 20426f6f84aaSZhang Xiaoxu retval = register_filesystem(&nfsd_fs_type); 20436f6f84aaSZhang Xiaoxu if (retval) 2044d76cc46bSDai Ngo goto out_free_all; 2045bd9d6a3eSLorenzo Bianconi retval = genl_register_family(&nfsd_nl_family); 2046bd9d6a3eSLorenzo Bianconi if (retval) 2047bd9d6a3eSLorenzo Bianconi goto out_free_all; 2048bd9d6a3eSLorenzo Bianconi 204926808d3fSJ. Bruce Fields return 0; 205026808d3fSJ. Bruce Fields out_free_all: 20516f6f84aaSZhang Xiaoxu nfsd4_destroy_laundry_wq(); 20526f6f84aaSZhang Xiaoxu out_free_cld: 205362fdb65eSZhang Xiaoxu unregister_cld_notifier(); 205462fdb65eSZhang Xiaoxu out_free_subsys: 2055b10252c7SAlexander Sverdlin unregister_pernet_subsys(&nfsd_net_ops); 2056bd5ae928SJ. Bruce Fields out_free_exports: 20571da177e4SLinus Torvalds remove_proc_entry("fs/nfs/exports", NULL); 20581da177e4SLinus Torvalds remove_proc_entry("fs/nfs", NULL); 2059dbf847ecSJ. Bruce Fields out_free_lockd: 20601da177e4SLinus Torvalds nfsd_lockd_shutdown(); 2061027690c7SJ. Bruce Fields nfsd_drc_slab_free(); 2062e567b98cSAmir Goldstein out_free_pnfs: 20639cf514ccSChristoph Hellwig nfsd4_exit_pnfs(); 206465178db4SBryan Schumaker out_free_slabs: 206546b25895SJ. Bruce Fields nfsd4_free_slabs(); 20661da177e4SLinus Torvalds return retval; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds static void __exit exit_nfsd(void) 20701da177e4SLinus Torvalds { 2071bd9d6a3eSLorenzo Bianconi genl_unregister_family(&nfsd_nl_family); 20726f6f84aaSZhang Xiaoxu unregister_filesystem(&nfsd_fs_type); 2073d76cc46bSDai Ngo nfsd4_destroy_laundry_wq(); 2074b10252c7SAlexander Sverdlin unregister_cld_notifier(); 2075bd5ae928SJ. Bruce Fields unregister_pernet_subsys(&nfsd_net_ops); 2076027690c7SJ. Bruce Fields nfsd_drc_slab_free(); 20771da177e4SLinus Torvalds remove_proc_entry("fs/nfs/exports", NULL); 20781da177e4SLinus Torvalds remove_proc_entry("fs/nfs", NULL); 20791da177e4SLinus Torvalds nfsd_lockd_shutdown(); 2080e8ff2a84SJ. Bruce Fields nfsd4_free_slabs(); 20819cf514ccSChristoph Hellwig nfsd4_exit_pnfs(); 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 20855865bafaSJeff Layton MODULE_DESCRIPTION("In-kernel NFS server"); 20861da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 20871da177e4SLinus Torvalds module_init(init_nfsd) 20881da177e4SLinus Torvalds module_exit(exit_nfsd) 2089