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> 18143cb494SPaul Gortmaker #include <linux/module.h> 19e8a79fb1SJ. Bruce Fields #include <linux/fsnotify.h> 201da177e4SLinus Torvalds 212ca72e17SJ. Bruce Fields #include "idmap.h" 229a74af21SBoaz Harrosh #include "nfsd.h" 239a74af21SBoaz Harrosh #include "cache.h" 24f3c7521fSBryan Schumaker #include "state.h" 257ea34ac1SJeff Layton #include "netns.h" 269cf514ccSChristoph Hellwig #include "pnfs.h" 272e6c6e4cSChuck Lever #include "filecache.h" 2839d432fcSChuck Lever #include "trace.h" 2913727f85SLorenzo Bianconi #include "netlink.h" 309a74af21SBoaz Harrosh 311da177e4SLinus Torvalds /* 32b0b0c0a2SKevin Coffman * We have a single directory with several nodes in it. 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds enum { 351da177e4SLinus Torvalds NFSD_Root = 1, 361da177e4SLinus Torvalds NFSD_List, 3720ad856eSAmir Goldstein NFSD_Export_Stats, 38e8e8753fSJ. Bruce Fields NFSD_Export_features, 391da177e4SLinus Torvalds NFSD_Fh, 404373ea84SWendy Cheng NFSD_FO_UnlockIP, 4117efa372SWendy Cheng NFSD_FO_UnlockFS, 421da177e4SLinus Torvalds NFSD_Threads, 43eed2965aSGreg Banks NFSD_Pool_Threads, 4403cf6c9fSGreg Banks NFSD_Pool_Stats, 45a2f999a3SJeff Layton NFSD_Reply_Cache_Stats, 4670c3b76cSNeilBrown NFSD_Versions, 4780212d59SNeilBrown NFSD_Ports, 48596bbe53SNeilBrown NFSD_MaxBlkSize, 495b8db00bSJeff Layton NFSD_MaxConnections, 502e6c6e4cSChuck Lever NFSD_Filecache, 5170c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 521da177e4SLinus Torvalds NFSD_Leasetime, 53efc4bb4fSJ. Bruce Fields NFSD_Gracetime, 540964a3d3SNeilBrown NFSD_RecoveryDir, 557f5ef2e9SJeff Layton NFSD_V4EndGrace, 5670c3b76cSNeilBrown #endif 57e8a79fb1SJ. Bruce Fields NFSD_MaxReserved 581da177e4SLinus Torvalds }; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* 611da177e4SLinus Torvalds * write() for these nodes. 621da177e4SLinus Torvalds */ 631da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size); 64b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size); 65b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size); 661da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size); 67eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size); 6870c3b76cSNeilBrown static ssize_t write_versions(struct file *file, char *buf, size_t size); 6980212d59SNeilBrown static ssize_t write_ports(struct file *file, char *buf, size_t size); 70596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); 715b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size); 7270c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 731da177e4SLinus Torvalds static ssize_t write_leasetime(struct file *file, char *buf, size_t size); 74efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size); 7574fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 760964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size); 7774fd4873SJeff Layton #endif 787f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size); 7970c3b76cSNeilBrown #endif 801da177e4SLinus Torvalds 81c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 821da177e4SLinus Torvalds [NFSD_Fh] = write_filehandle, 83b046ccdcSChuck Lever [NFSD_FO_UnlockIP] = write_unlock_ip, 84b046ccdcSChuck Lever [NFSD_FO_UnlockFS] = write_unlock_fs, 851da177e4SLinus Torvalds [NFSD_Threads] = write_threads, 86eed2965aSGreg Banks [NFSD_Pool_Threads] = write_pool_threads, 8770c3b76cSNeilBrown [NFSD_Versions] = write_versions, 8880212d59SNeilBrown [NFSD_Ports] = write_ports, 89596bbe53SNeilBrown [NFSD_MaxBlkSize] = write_maxblksize, 905b8db00bSJeff Layton [NFSD_MaxConnections] = write_maxconn, 9170c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 921da177e4SLinus Torvalds [NFSD_Leasetime] = write_leasetime, 93efc4bb4fSJ. Bruce Fields [NFSD_Gracetime] = write_gracetime, 9474fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 950964a3d3SNeilBrown [NFSD_RecoveryDir] = write_recoverydir, 9674fd4873SJeff Layton #endif 977f5ef2e9SJeff Layton [NFSD_V4EndGrace] = write_v4_end_grace, 9870c3b76cSNeilBrown #endif 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 1021da177e4SLinus Torvalds { 103496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 1041da177e4SLinus Torvalds char *data; 1051da177e4SLinus Torvalds ssize_t rv; 1061da177e4SLinus Torvalds 107e8c96f8cSTobias Klauser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 1081da177e4SLinus Torvalds return -EINVAL; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 1111da177e4SLinus Torvalds if (IS_ERR(data)) 1121da177e4SLinus Torvalds return PTR_ERR(data); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 1153434d7aaSChuck Lever if (rv < 0) 1161da177e4SLinus Torvalds return rv; 1173434d7aaSChuck Lever 1183434d7aaSChuck Lever simple_transaction_set(file, rv); 1193434d7aaSChuck Lever return size; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1227390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 1237390022dSNeilBrown { 1247390022dSNeilBrown if (! file->private_data) { 1257390022dSNeilBrown /* An attempt to read a transaction file without writing 1267390022dSNeilBrown * causes a 0-byte write so that the file can return 1277390022dSNeilBrown * state information 1287390022dSNeilBrown */ 1297390022dSNeilBrown ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos); 1307390022dSNeilBrown if (rv < 0) 1317390022dSNeilBrown return rv; 1327390022dSNeilBrown } 1337390022dSNeilBrown return simple_transaction_read(file, buf, size, pos); 1347390022dSNeilBrown } 1357390022dSNeilBrown 1364b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = { 1371da177e4SLinus Torvalds .write = nfsctl_transaction_write, 1387390022dSNeilBrown .read = nfsctl_transaction_read, 1391da177e4SLinus Torvalds .release = simple_transaction_release, 1406038f373SArnd Bergmann .llseek = default_llseek, 1411da177e4SLinus Torvalds }; 1421da177e4SLinus Torvalds 14396d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file) 1441da177e4SLinus Torvalds { 145f2c7ea10SStanislav Kinsbursky int err; 146f2c7ea10SStanislav Kinsbursky struct seq_file *seq; 14796d851c4SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 148f2c7ea10SStanislav Kinsbursky 149f2c7ea10SStanislav Kinsbursky err = seq_open(file, &nfs_exports_op); 150f2c7ea10SStanislav Kinsbursky if (err) 151f2c7ea10SStanislav Kinsbursky return err; 152f2c7ea10SStanislav Kinsbursky 153f2c7ea10SStanislav Kinsbursky seq = file->private_data; 154e5f06f72SStanislav Kinsbursky seq->private = nn->svc_export_cache; 155f2c7ea10SStanislav Kinsbursky return 0; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 15896d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file) 15996d851c4SStanislav Kinsbursky { 16096d851c4SStanislav Kinsbursky return exports_net_open(inode->i_sb->s_fs_info, file); 16196d851c4SStanislav Kinsbursky } 16296d851c4SStanislav Kinsbursky 16396d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = { 16496d851c4SStanislav Kinsbursky .open = exports_nfsd_open, 1651da177e4SLinus Torvalds .read = seq_read, 1661da177e4SLinus Torvalds .llseek = seq_lseek, 1671da177e4SLinus Torvalds .release = seq_release, 1681da177e4SLinus Torvalds }; 1691da177e4SLinus Torvalds 170e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v) 171e8e8753fSJ. Bruce Fields { 172e8e8753fSJ. Bruce Fields seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS); 173e8e8753fSJ. Bruce Fields return 0; 174e8e8753fSJ. Bruce Fields } 175e8e8753fSJ. Bruce Fields 1769beeaab8SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(export_features); 177e8e8753fSJ. Bruce Fields 178828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = { 17903cf6c9fSGreg Banks .open = nfsd_pool_stats_open, 18003cf6c9fSGreg Banks .read = seq_read, 18103cf6c9fSGreg Banks .llseek = seq_lseek, 1827b207ccdSNeilBrown .release = seq_release, 18303cf6c9fSGreg Banks }; 18403cf6c9fSGreg Banks 18564776611SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats); 186a2f999a3SJeff Layton 1871342f9ddSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats); 1882e6c6e4cSChuck Lever 1891da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/ 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * payload - write methods 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds 194244c7d44SAl Viro static inline struct net *netns(struct file *file) 195244c7d44SAl Viro { 196244c7d44SAl Viro return file_inode(file)->i_sb->s_fs_info; 197244c7d44SAl Viro } 1981da177e4SLinus Torvalds 199f2453978SChuck Lever /* 200262a0982SChuck Lever * write_unlock_ip - Release all locks used by a client 201262a0982SChuck Lever * 202262a0982SChuck Lever * Experimental. 203262a0982SChuck Lever * 204262a0982SChuck Lever * Input: 205262a0982SChuck Lever * buf: '\n'-terminated C string containing a 2064116092bSChuck Lever * presentation format IP address 207262a0982SChuck Lever * size: length of C string in @buf 208262a0982SChuck Lever * Output: 209262a0982SChuck Lever * On success: returns zero if all specified locks were released; 210262a0982SChuck Lever * returns one if one or more locks were not released 211262a0982SChuck Lever * On error: return code is negative errno value 212262a0982SChuck Lever */ 213b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) 2144373ea84SWendy Cheng { 2154116092bSChuck Lever struct sockaddr_storage address; 2164116092bSChuck Lever struct sockaddr *sap = (struct sockaddr *)&address; 2174116092bSChuck Lever size_t salen = sizeof(address); 218367c8c7bSChuck Lever char *fo_path; 219244c7d44SAl Viro struct net *net = netns(file); 2204373ea84SWendy Cheng 2214373ea84SWendy Cheng /* sanity check */ 2224373ea84SWendy Cheng if (size == 0) 2234373ea84SWendy Cheng return -EINVAL; 2244373ea84SWendy Cheng 2254373ea84SWendy Cheng if (buf[size-1] != '\n') 2264373ea84SWendy Cheng return -EINVAL; 2274373ea84SWendy Cheng 2284373ea84SWendy Cheng fo_path = buf; 2294373ea84SWendy Cheng if (qword_get(&buf, fo_path, size) < 0) 2304373ea84SWendy Cheng return -EINVAL; 2314373ea84SWendy Cheng 23211f77942SStanislav Kinsbursky if (rpc_pton(net, fo_path, size, sap, salen) == 0) 2334373ea84SWendy Cheng return -EINVAL; 2344373ea84SWendy Cheng 23539d432fcSChuck Lever trace_nfsd_ctl_unlock_ip(net, buf); 2364116092bSChuck Lever return nlmsvc_unlock_all_by_ip(sap); 2374373ea84SWendy Cheng } 2384373ea84SWendy Cheng 239f2453978SChuck Lever /* 240262a0982SChuck Lever * write_unlock_fs - Release all locks on a local file system 241262a0982SChuck Lever * 242262a0982SChuck Lever * Experimental. 243262a0982SChuck Lever * 244262a0982SChuck Lever * Input: 245262a0982SChuck Lever * buf: '\n'-terminated C string containing the 246262a0982SChuck Lever * absolute pathname of a local file system 247262a0982SChuck Lever * size: length of C string in @buf 248262a0982SChuck Lever * Output: 249262a0982SChuck Lever * On success: returns zero if all specified locks were released; 250262a0982SChuck Lever * returns one if one or more locks were not released 251262a0982SChuck Lever * On error: return code is negative errno value 252262a0982SChuck Lever */ 253b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) 25417efa372SWendy Cheng { 255a63bb996SAl Viro struct path path; 25617efa372SWendy Cheng char *fo_path; 25717efa372SWendy Cheng int error; 25817efa372SWendy Cheng 25917efa372SWendy Cheng /* sanity check */ 26017efa372SWendy Cheng if (size == 0) 26117efa372SWendy Cheng return -EINVAL; 26217efa372SWendy Cheng 26317efa372SWendy Cheng if (buf[size-1] != '\n') 26417efa372SWendy Cheng return -EINVAL; 26517efa372SWendy Cheng 26617efa372SWendy Cheng fo_path = buf; 26717efa372SWendy Cheng if (qword_get(&buf, fo_path, size) < 0) 26817efa372SWendy Cheng return -EINVAL; 26939d432fcSChuck Lever trace_nfsd_ctl_unlock_fs(netns(file), fo_path); 270a63bb996SAl Viro error = kern_path(fo_path, 0, &path); 27117efa372SWendy Cheng if (error) 27217efa372SWendy Cheng return error; 27317efa372SWendy Cheng 274262a0982SChuck Lever /* 275262a0982SChuck Lever * XXX: Needs better sanity checking. Otherwise we could end up 276262a0982SChuck Lever * releasing locks on the wrong file system. 277262a0982SChuck Lever * 278262a0982SChuck Lever * For example: 279262a0982SChuck Lever * 1. Does the path refer to a directory? 280262a0982SChuck Lever * 2. Is that directory a mount point, or 281262a0982SChuck Lever * 3. Is that directory the root of an exported file system? 282262a0982SChuck Lever */ 283d8c9584eSAl Viro error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 284*1ac3629bSNeilBrown nfsd4_revoke_states(netns(file), path.dentry->d_sb); 28517efa372SWendy Cheng 286a63bb996SAl Viro path_put(&path); 28717efa372SWendy Cheng return error; 28817efa372SWendy Cheng } 28917efa372SWendy Cheng 290f2453978SChuck Lever /* 291262a0982SChuck Lever * write_filehandle - Get a variable-length NFS file handle by path 292262a0982SChuck Lever * 293262a0982SChuck Lever * On input, the buffer contains a '\n'-terminated C string comprised of 294262a0982SChuck Lever * three alphanumeric words separated by whitespace. The string may 295262a0982SChuck Lever * contain escape sequences. 296262a0982SChuck Lever * 297262a0982SChuck Lever * Input: 298262a0982SChuck Lever * buf: 299262a0982SChuck Lever * domain: client domain name 300262a0982SChuck Lever * path: export pathname 301262a0982SChuck Lever * maxsize: numeric maximum size of 302262a0982SChuck Lever * @buf 303262a0982SChuck Lever * size: length of C string in @buf 304262a0982SChuck Lever * Output: 305262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 306262a0982SChuck Lever * string containing a ASCII hex text version 307262a0982SChuck Lever * of the NFS file handle; 308262a0982SChuck Lever * return code is the size in bytes of the string 309262a0982SChuck Lever * On error: return code is negative errno value 310262a0982SChuck Lever */ 3111da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds char *dname, *path; 3143f649ab7SKees Cook int maxsize; 3151da177e4SLinus Torvalds char *mesg = buf; 3161da177e4SLinus Torvalds int len; 3171da177e4SLinus Torvalds struct auth_domain *dom; 3181da177e4SLinus Torvalds struct knfsd_fh fh; 3191da177e4SLinus Torvalds 32087d26ea7SJ. Bruce Fields if (size == 0) 32187d26ea7SJ. Bruce Fields return -EINVAL; 32287d26ea7SJ. Bruce Fields 3231da177e4SLinus Torvalds if (buf[size-1] != '\n') 3241da177e4SLinus Torvalds return -EINVAL; 3251da177e4SLinus Torvalds buf[size-1] = 0; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds dname = mesg; 3281da177e4SLinus Torvalds len = qword_get(&mesg, dname, size); 32954224f04SChuck Lever if (len <= 0) 33054224f04SChuck Lever return -EINVAL; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds path = dname+len+1; 3331da177e4SLinus Torvalds len = qword_get(&mesg, path, size); 33454224f04SChuck Lever if (len <= 0) 33554224f04SChuck Lever return -EINVAL; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds len = get_int(&mesg, &maxsize); 3381da177e4SLinus Torvalds if (len) 3391da177e4SLinus Torvalds return len; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds if (maxsize < NFS_FHSIZE) 3421da177e4SLinus Torvalds return -EINVAL; 3433c7aa15dSKinglong Mee maxsize = min(maxsize, NFS3_FHSIZE); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (qword_get(&mesg, mesg, size) > 0) 3461da177e4SLinus Torvalds return -EINVAL; 3471da177e4SLinus Torvalds 34839d432fcSChuck Lever trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize); 34939d432fcSChuck Lever 3501da177e4SLinus Torvalds /* we have all the words, they are in buf.. */ 3511da177e4SLinus Torvalds dom = unix_domain_find(dname); 3521da177e4SLinus Torvalds if (!dom) 3531da177e4SLinus Torvalds return -ENOMEM; 3541da177e4SLinus Torvalds 355244c7d44SAl Viro len = exp_rootfh(netns(file), dom, path, &fh, maxsize); 3561da177e4SLinus Torvalds auth_domain_put(dom); 3571da177e4SLinus Torvalds if (len) 3581da177e4SLinus Torvalds return len; 3591da177e4SLinus Torvalds 36054224f04SChuck Lever mesg = buf; 36154224f04SChuck Lever len = SIMPLE_TRANSACTION_LIMIT; 362d8b26071SNeilBrown qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size); 3631da177e4SLinus Torvalds mesg[-1] = '\n'; 3641da177e4SLinus Torvalds return mesg - buf; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 367f2453978SChuck Lever /* 368262a0982SChuck Lever * write_threads - Start NFSD, or report the current number of running threads 369262a0982SChuck Lever * 370262a0982SChuck Lever * Input: 371262a0982SChuck Lever * buf: ignored 372262a0982SChuck Lever * size: zero 373262a0982SChuck Lever * Output: 374262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 375262a0982SChuck Lever * string numeric value representing the number of 376262a0982SChuck Lever * running NFSD threads; 377262a0982SChuck Lever * return code is the size in bytes of the string 378262a0982SChuck Lever * On error: return code is zero 379262a0982SChuck Lever * 380262a0982SChuck Lever * OR 381262a0982SChuck Lever * 382262a0982SChuck Lever * Input: 383262a0982SChuck Lever * buf: C string containing an unsigned 384262a0982SChuck Lever * integer value representing the 385262a0982SChuck Lever * number of NFSD threads to start 386262a0982SChuck Lever * size: non-zero length of C string in @buf 387262a0982SChuck Lever * Output: 388262a0982SChuck Lever * On success: NFS service is started; 389262a0982SChuck Lever * passed-in buffer filled with '\n'-terminated C 390262a0982SChuck Lever * string numeric value representing the number of 391262a0982SChuck Lever * running NFSD threads; 392262a0982SChuck Lever * return code is the size in bytes of the string 393262a0982SChuck Lever * On error: return code is zero or a negative errno value 394262a0982SChuck Lever */ 3951da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds char *mesg = buf; 3981da177e4SLinus Torvalds int rv; 399244c7d44SAl Viro struct net *net = netns(file); 400d41a9417SStanislav Kinsbursky 4011da177e4SLinus Torvalds if (size > 0) { 4021da177e4SLinus Torvalds int newthreads; 4031da177e4SLinus Torvalds rv = get_int(&mesg, &newthreads); 4041da177e4SLinus Torvalds if (rv) 4051da177e4SLinus Torvalds return rv; 4061da177e4SLinus Torvalds if (newthreads < 0) 4071da177e4SLinus Torvalds return -EINVAL; 40839d432fcSChuck Lever trace_nfsd_ctl_threads(net, newthreads); 4094df493a2STrond Myklebust rv = nfsd_svc(newthreads, net, file->f_cred); 41082e12fe9SNeilBrown if (rv < 0) 4111da177e4SLinus Torvalds return rv; 41282e12fe9SNeilBrown } else 4139dd9845fSStanislav Kinsbursky rv = nfsd_nrthreads(net); 414e06b6405SChuck Lever 41582e12fe9SNeilBrown return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 418f2453978SChuck Lever /* 419262a0982SChuck Lever * write_pool_threads - Set or report the current number of threads per pool 420262a0982SChuck Lever * 421262a0982SChuck Lever * Input: 422262a0982SChuck Lever * buf: ignored 423262a0982SChuck Lever * size: zero 424262a0982SChuck Lever * 425262a0982SChuck Lever * OR 426262a0982SChuck Lever * 427262a0982SChuck Lever * Input: 428262a0982SChuck Lever * buf: C string containing whitespace- 429262a0982SChuck Lever * separated unsigned integer values 430262a0982SChuck Lever * representing the number of NFSD 431262a0982SChuck Lever * threads to start in each pool 432262a0982SChuck Lever * size: non-zero length of C string in @buf 433262a0982SChuck Lever * Output: 434262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 435262a0982SChuck Lever * string containing integer values representing the 436262a0982SChuck Lever * number of NFSD threads in each pool; 437262a0982SChuck Lever * return code is the size in bytes of the string 438262a0982SChuck Lever * On error: return code is zero or a negative errno value 439262a0982SChuck Lever */ 440eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size) 441eed2965aSGreg Banks { 442eed2965aSGreg Banks /* if size > 0, look for an array of number of threads per node 443eed2965aSGreg Banks * and apply them then write out number of threads per node as reply 444eed2965aSGreg Banks */ 445eed2965aSGreg Banks char *mesg = buf; 446eed2965aSGreg Banks int i; 447eed2965aSGreg Banks int rv; 448eed2965aSGreg Banks int len; 449bedbdd8bSNeil Brown int npools; 450eed2965aSGreg Banks int *nthreads; 451244c7d44SAl Viro struct net *net = netns(file); 452eed2965aSGreg Banks 453bedbdd8bSNeil Brown mutex_lock(&nfsd_mutex); 4549dd9845fSStanislav Kinsbursky npools = nfsd_nrpools(net); 455eed2965aSGreg Banks if (npools == 0) { 456eed2965aSGreg Banks /* 457eed2965aSGreg Banks * NFS is shut down. The admin can start it by 458eed2965aSGreg Banks * writing to the threads file but NOT the pool_threads 459eed2965aSGreg Banks * file, sorry. Report zero threads. 460eed2965aSGreg Banks */ 461bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 462eed2965aSGreg Banks strcpy(buf, "0\n"); 463eed2965aSGreg Banks return strlen(buf); 464eed2965aSGreg Banks } 465eed2965aSGreg Banks 466eed2965aSGreg Banks nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL); 467bedbdd8bSNeil Brown rv = -ENOMEM; 468eed2965aSGreg Banks if (nthreads == NULL) 469bedbdd8bSNeil Brown goto out_free; 470eed2965aSGreg Banks 471eed2965aSGreg Banks if (size > 0) { 472eed2965aSGreg Banks for (i = 0; i < npools; i++) { 473eed2965aSGreg Banks rv = get_int(&mesg, &nthreads[i]); 474eed2965aSGreg Banks if (rv == -ENOENT) 475eed2965aSGreg Banks break; /* fewer numbers than pools */ 476eed2965aSGreg Banks if (rv) 477eed2965aSGreg Banks goto out_free; /* syntax error */ 478eed2965aSGreg Banks rv = -EINVAL; 479eed2965aSGreg Banks if (nthreads[i] < 0) 480eed2965aSGreg Banks goto out_free; 48139d432fcSChuck Lever trace_nfsd_ctl_pool_threads(net, i, nthreads[i]); 482eed2965aSGreg Banks } 4833938a0d5SStanislav Kinsbursky rv = nfsd_set_nrthreads(i, nthreads, net); 484eed2965aSGreg Banks if (rv) 485eed2965aSGreg Banks goto out_free; 486eed2965aSGreg Banks } 487eed2965aSGreg Banks 4889dd9845fSStanislav Kinsbursky rv = nfsd_get_nrthreads(npools, nthreads, net); 489eed2965aSGreg Banks if (rv) 490eed2965aSGreg Banks goto out_free; 491eed2965aSGreg Banks 492eed2965aSGreg Banks mesg = buf; 493eed2965aSGreg Banks size = SIMPLE_TRANSACTION_LIMIT; 494eed2965aSGreg Banks for (i = 0; i < npools && size > 0; i++) { 495eed2965aSGreg Banks snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' ')); 496eed2965aSGreg Banks len = strlen(mesg); 497eed2965aSGreg Banks size -= len; 498eed2965aSGreg Banks mesg += len; 499eed2965aSGreg Banks } 500413d63d7SJ. Bruce Fields rv = mesg - buf; 501eed2965aSGreg Banks out_free: 502eed2965aSGreg Banks kfree(nthreads); 503bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 504eed2965aSGreg Banks return rv; 505eed2965aSGreg Banks } 506eed2965aSGreg Banks 507ff7d1179STrond Myklebust static ssize_t 508e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining, 509e333f3bbSTrond Myklebust const char *sep, unsigned vers, int minor) 510ff7d1179STrond Myklebust { 511abcb4dacSNeilBrown const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u"; 512e333f3bbSTrond Myklebust bool supported = !!nfsd_vers(nn, vers, NFSD_TEST); 513ff7d1179STrond Myklebust 514abcb4dacSNeilBrown if (vers == 4 && minor >= 0 && 515e333f3bbSTrond Myklebust !nfsd_minorversion(nn, minor, NFSD_TEST)) 516ff7d1179STrond Myklebust supported = false; 517abcb4dacSNeilBrown if (minor == 0 && supported) 518abcb4dacSNeilBrown /* 519abcb4dacSNeilBrown * special case for backward compatability. 520abcb4dacSNeilBrown * +4.0 is never reported, it is implied by 521abcb4dacSNeilBrown * +4, unless -4.0 is present. 522abcb4dacSNeilBrown */ 523abcb4dacSNeilBrown return 0; 524ff7d1179STrond Myklebust return snprintf(buf, remaining, format, sep, 525ff7d1179STrond Myklebust supported ? '+' : '-', vers, minor); 526ff7d1179STrond Myklebust } 527ff7d1179STrond Myklebust 5283dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size) 52970c3b76cSNeilBrown { 53070c3b76cSNeilBrown char *mesg = buf; 5318daf220aSBenny Halevy char *vers, *minorp, sign; 532261758b5SChuck Lever int len, num, remaining; 53370c3b76cSNeilBrown ssize_t tlen = 0; 53470c3b76cSNeilBrown char *sep; 535244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 53670c3b76cSNeilBrown 53770c3b76cSNeilBrown if (size > 0) { 5389dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) 5396658d3a7SNeilBrown /* Cannot change versions without updating 5409dd9845fSStanislav Kinsbursky * nn->nfsd_serv->sv_xdrsize, and reallocing 5416658d3a7SNeilBrown * rq_argp and rq_resp 5426658d3a7SNeilBrown */ 54370c3b76cSNeilBrown return -EBUSY; 54470c3b76cSNeilBrown if (buf[size-1] != '\n') 54570c3b76cSNeilBrown return -EINVAL; 54670c3b76cSNeilBrown buf[size-1] = 0; 54739d432fcSChuck Lever trace_nfsd_ctl_version(netns(file), buf); 54870c3b76cSNeilBrown 54970c3b76cSNeilBrown vers = mesg; 55070c3b76cSNeilBrown len = qword_get(&mesg, vers, size); 55170c3b76cSNeilBrown if (len <= 0) return -EINVAL; 55270c3b76cSNeilBrown do { 553d3635ff0STrond Myklebust enum vers_op cmd; 554abcb4dacSNeilBrown unsigned minor; 55570c3b76cSNeilBrown sign = *vers; 55670c3b76cSNeilBrown if (sign == '+' || sign == '-') 5578daf220aSBenny Halevy num = simple_strtol((vers+1), &minorp, 0); 55870c3b76cSNeilBrown else 5598daf220aSBenny Halevy num = simple_strtol(vers, &minorp, 0); 5608daf220aSBenny Halevy if (*minorp == '.') { 561ff89be87SJ. Bruce Fields if (num != 4) 5628daf220aSBenny Halevy return -EINVAL; 563e35659f1SNeilBrown if (kstrtouint(minorp+1, 0, &minor) < 0) 5648daf220aSBenny Halevy return -EINVAL; 565abcb4dacSNeilBrown } 566abcb4dacSNeilBrown 567d3635ff0STrond Myklebust cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET; 56870c3b76cSNeilBrown switch(num) { 5692f3a4b2aSJeff Layton #ifdef CONFIG_NFSD_V2 57070c3b76cSNeilBrown case 2: 5712f3a4b2aSJeff Layton #endif 57270c3b76cSNeilBrown case 3: 573e333f3bbSTrond Myklebust nfsd_vers(nn, num, cmd); 574d3635ff0STrond Myklebust break; 57570c3b76cSNeilBrown case 4: 576abcb4dacSNeilBrown if (*minorp == '.') { 577e333f3bbSTrond Myklebust if (nfsd_minorversion(nn, minor, cmd) < 0) 578abcb4dacSNeilBrown return -EINVAL; 579e333f3bbSTrond Myklebust } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) { 580abcb4dacSNeilBrown /* 581abcb4dacSNeilBrown * Either we have +4 and no minors are enabled, 582abcb4dacSNeilBrown * or we have -4 and at least one minor is enabled. 583abcb4dacSNeilBrown * In either case, propagate 'cmd' to all minors. 584abcb4dacSNeilBrown */ 585abcb4dacSNeilBrown minor = 0; 586e333f3bbSTrond Myklebust while (nfsd_minorversion(nn, minor, cmd) >= 0) 587abcb4dacSNeilBrown minor++; 588abcb4dacSNeilBrown } 58970c3b76cSNeilBrown break; 59070c3b76cSNeilBrown default: 5918e823bafSJeff Layton /* Ignore requests to disable non-existent versions */ 5928e823bafSJeff Layton if (cmd == NFSD_SET) 59370c3b76cSNeilBrown return -EINVAL; 59470c3b76cSNeilBrown } 59570c3b76cSNeilBrown vers += len + 1; 59670c3b76cSNeilBrown } while ((len = qword_get(&mesg, vers, size)) > 0); 59770c3b76cSNeilBrown /* If all get turned off, turn them back on, as 59870c3b76cSNeilBrown * having no versions is BAD 59970c3b76cSNeilBrown */ 600e333f3bbSTrond Myklebust nfsd_reset_versions(nn); 60170c3b76cSNeilBrown } 602261758b5SChuck Lever 60370c3b76cSNeilBrown /* Now write current state into reply buffer */ 60470c3b76cSNeilBrown sep = ""; 605261758b5SChuck Lever remaining = SIMPLE_TRANSACTION_LIMIT; 606ff7d1179STrond Myklebust for (num=2 ; num <= 4 ; num++) { 607abcb4dacSNeilBrown int minor; 608e333f3bbSTrond Myklebust if (!nfsd_vers(nn, num, NFSD_AVAIL)) 609e35659f1SNeilBrown continue; 610abcb4dacSNeilBrown 611abcb4dacSNeilBrown minor = -1; 612ff7d1179STrond Myklebust do { 613e333f3bbSTrond Myklebust len = nfsd_print_version_support(nn, buf, remaining, 614ff7d1179STrond Myklebust sep, num, minor); 615818f2f57SDan Carpenter if (len >= remaining) 616ff7d1179STrond Myklebust goto out; 617261758b5SChuck Lever remaining -= len; 618261758b5SChuck Lever buf += len; 619261758b5SChuck Lever tlen += len; 620ff7d1179STrond Myklebust minor++; 621abcb4dacSNeilBrown if (len) 622ff7d1179STrond Myklebust sep = " "; 623ff7d1179STrond Myklebust } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION); 624261758b5SChuck Lever } 625ff7d1179STrond Myklebust out: 626261758b5SChuck Lever len = snprintf(buf, remaining, "\n"); 627818f2f57SDan Carpenter if (len >= remaining) 628261758b5SChuck Lever return -EINVAL; 629261758b5SChuck Lever return tlen + len; 63070c3b76cSNeilBrown } 63170c3b76cSNeilBrown 632f2453978SChuck Lever /* 633262a0982SChuck Lever * write_versions - Set or report the available NFS protocol versions 634262a0982SChuck Lever * 635262a0982SChuck Lever * Input: 636262a0982SChuck Lever * buf: ignored 637262a0982SChuck Lever * size: zero 638262a0982SChuck Lever * Output: 639262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 640262a0982SChuck Lever * string containing positive or negative integer 641262a0982SChuck Lever * values representing the current status of each 642262a0982SChuck Lever * protocol version; 643262a0982SChuck Lever * return code is the size in bytes of the string 644262a0982SChuck Lever * On error: return code is zero or a negative errno value 645262a0982SChuck Lever * 646262a0982SChuck Lever * OR 647262a0982SChuck Lever * 648262a0982SChuck Lever * Input: 649262a0982SChuck Lever * buf: C string containing whitespace- 650262a0982SChuck Lever * separated positive or negative 651262a0982SChuck Lever * integer values representing NFS 652262a0982SChuck Lever * protocol versions to enable ("+n") 653262a0982SChuck Lever * or disable ("-n") 654262a0982SChuck Lever * size: non-zero length of C string in @buf 655262a0982SChuck Lever * Output: 656262a0982SChuck Lever * On success: status of zero or more protocol versions has 657262a0982SChuck Lever * been updated; passed-in buffer filled with 658262a0982SChuck Lever * '\n'-terminated C string containing positive 659262a0982SChuck Lever * or negative integer values representing the 660262a0982SChuck Lever * current status of each protocol version; 661262a0982SChuck Lever * return code is the size in bytes of the string 662262a0982SChuck Lever * On error: return code is zero or a negative errno value 663262a0982SChuck Lever */ 6643dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size) 6653dd98a3bSJeff Layton { 6663dd98a3bSJeff Layton ssize_t rv; 6673dd98a3bSJeff Layton 6683dd98a3bSJeff Layton mutex_lock(&nfsd_mutex); 6693dd98a3bSJeff Layton rv = __write_versions(file, buf, size); 6703dd98a3bSJeff Layton mutex_unlock(&nfsd_mutex); 6713dd98a3bSJeff Layton return rv; 6723dd98a3bSJeff Layton } 6733dd98a3bSJeff Layton 6744cd5dc75SChuck Lever /* 6750a5372d8SChuck Lever * Zero-length write. Return a list of NFSD's current listener 6760a5372d8SChuck Lever * transports. 6770a5372d8SChuck Lever */ 6789dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net) 6790a5372d8SChuck Lever { 6809dd9845fSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 6819dd9845fSStanislav Kinsbursky 6829dd9845fSStanislav Kinsbursky if (nn->nfsd_serv == NULL) 6830a5372d8SChuck Lever return 0; 6849dd9845fSStanislav Kinsbursky return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); 6850a5372d8SChuck Lever } 6860a5372d8SChuck Lever 6870a5372d8SChuck Lever /* 6880b7c2f6fSChuck Lever * A single 'fd' number was written, in which case it must be for 6890b7c2f6fSChuck Lever * a socket of a supported family/protocol, and we use it as an 6900b7c2f6fSChuck Lever * nfsd listener. 6910b7c2f6fSChuck Lever */ 6924df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred) 6930b7c2f6fSChuck Lever { 6940b7c2f6fSChuck Lever char *mesg = buf; 6950b7c2f6fSChuck Lever int fd, err; 6969dd9845fSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 69764e63041SJeff Layton struct svc_serv *serv; 6980b7c2f6fSChuck Lever 6990b7c2f6fSChuck Lever err = get_int(&mesg, &fd); 7000b7c2f6fSChuck Lever if (err != 0 || fd < 0) 7010b7c2f6fSChuck Lever return -EINVAL; 70239d432fcSChuck Lever trace_nfsd_ctl_ports_addfd(net, fd); 7030b7c2f6fSChuck Lever 7046777436bSStanislav Kinsbursky err = nfsd_create_serv(net); 7050b7c2f6fSChuck Lever if (err != 0) 7060b7c2f6fSChuck Lever return err; 7070b7c2f6fSChuck Lever 70864e63041SJeff Layton serv = nn->nfsd_serv; 70964e63041SJeff Layton err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); 710ea068badSChuck Lever 7111e3577a4SNeilBrown if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 71217419aefSNeilBrown nfsd_destroy_serv(net); 71317419aefSNeilBrown 714ea068badSChuck Lever return err; 7150b7c2f6fSChuck Lever } 7160b7c2f6fSChuck Lever 7170b7c2f6fSChuck Lever /* 718442a6290SChuck Lever * A transport listener is added by writing its transport name and 7194eb68c26SChuck Lever * a port number. 7204eb68c26SChuck Lever */ 7214df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred) 7224eb68c26SChuck Lever { 7234eb68c26SChuck Lever char transport[16]; 72437498292SChuck Lever struct svc_xprt *xprt; 7254eb68c26SChuck Lever int port, err; 7269dd9845fSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 72764e63041SJeff Layton struct svc_serv *serv; 7284eb68c26SChuck Lever 729a10fded1SJ. Bruce Fields if (sscanf(buf, "%15s %5u", transport, &port) != 2) 7304eb68c26SChuck Lever return -EINVAL; 7314eb68c26SChuck Lever 7324be929beSAlexey Dobriyan if (port < 1 || port > USHRT_MAX) 7334eb68c26SChuck Lever return -EINVAL; 73439d432fcSChuck Lever trace_nfsd_ctl_ports_addxprt(net, transport, port); 7354eb68c26SChuck Lever 7366777436bSStanislav Kinsbursky err = nfsd_create_serv(net); 7374eb68c26SChuck Lever if (err != 0) 7384eb68c26SChuck Lever return err; 7394eb68c26SChuck Lever 74064e63041SJeff Layton serv = nn->nfsd_serv; 74164e63041SJeff Layton err = svc_xprt_create(serv, transport, net, 7424df493a2STrond Myklebust PF_INET, port, SVC_SOCK_ANONYMOUS, cred); 74368717908SChuck Lever if (err < 0) 74437498292SChuck Lever goto out_err; 74537498292SChuck Lever 74664e63041SJeff Layton err = svc_xprt_create(serv, transport, net, 7474df493a2STrond Myklebust PF_INET6, port, SVC_SOCK_ANONYMOUS, cred); 74837498292SChuck Lever if (err < 0 && err != -EAFNOSUPPORT) 74937498292SChuck Lever goto out_close; 7500cd14a06SJeff Layton 7514eb68c26SChuck Lever return 0; 75237498292SChuck Lever out_close: 75364e63041SJeff Layton xprt = svc_find_xprt(serv, transport, net, PF_INET, port); 75437498292SChuck Lever if (xprt != NULL) { 7554355d767SChuck Lever svc_xprt_close(xprt); 75637498292SChuck Lever svc_xprt_put(xprt); 75737498292SChuck Lever } 75837498292SChuck Lever out_err: 7591e3577a4SNeilBrown if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 76017419aefSNeilBrown nfsd_destroy_serv(net); 7612a501f55SNeilBrown 76237498292SChuck Lever return err; 7634eb68c26SChuck Lever } 7644eb68c26SChuck Lever 76508160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size, 76608160352SStanislav Kinsbursky struct net *net) 76780212d59SNeilBrown { 7680a5372d8SChuck Lever if (size == 0) 7699dd9845fSStanislav Kinsbursky return __write_ports_names(buf, net); 7700b7c2f6fSChuck Lever 7710b7c2f6fSChuck Lever if (isdigit(buf[0])) 7724df493a2STrond Myklebust return __write_ports_addfd(buf, net, file->f_cred); 77382d56591SChuck Lever 7744eb68c26SChuck Lever if (isalpha(buf[0])) 7754df493a2STrond Myklebust return __write_ports_addxprt(buf, net, file->f_cred); 7764cd5dc75SChuck Lever 777b41b66d6SNeilBrown return -EINVAL; 778b41b66d6SNeilBrown } 77980212d59SNeilBrown 780f2453978SChuck Lever /* 781262a0982SChuck Lever * write_ports - Pass a socket file descriptor or transport name to listen on 782262a0982SChuck Lever * 783262a0982SChuck Lever * Input: 784262a0982SChuck Lever * buf: ignored 785262a0982SChuck Lever * size: zero 786262a0982SChuck Lever * Output: 787262a0982SChuck Lever * On success: passed-in buffer filled with a '\n'-terminated C 788262a0982SChuck Lever * string containing a whitespace-separated list of 789262a0982SChuck Lever * named NFSD listeners; 790262a0982SChuck Lever * return code is the size in bytes of the string 791262a0982SChuck Lever * On error: return code is zero or a negative errno value 792262a0982SChuck Lever * 793262a0982SChuck Lever * OR 794262a0982SChuck Lever * 795262a0982SChuck Lever * Input: 796262a0982SChuck Lever * buf: C string containing an unsigned 797262a0982SChuck Lever * integer value representing a bound 798262a0982SChuck Lever * but unconnected socket that is to be 799c71206a7SChuck Lever * used as an NFSD listener; listen(3) 800c71206a7SChuck Lever * must be called for a SOCK_STREAM 801c71206a7SChuck Lever * socket, otherwise it is ignored 802262a0982SChuck Lever * size: non-zero length of C string in @buf 803262a0982SChuck Lever * Output: 804262a0982SChuck Lever * On success: NFS service is started; 805262a0982SChuck Lever * passed-in buffer filled with a '\n'-terminated C 806262a0982SChuck Lever * string containing a unique alphanumeric name of 807262a0982SChuck Lever * the listener; 808262a0982SChuck Lever * return code is the size in bytes of the string 809262a0982SChuck Lever * On error: return code is a negative errno value 810262a0982SChuck Lever * 811262a0982SChuck Lever * OR 812262a0982SChuck Lever * 813262a0982SChuck Lever * Input: 814262a0982SChuck Lever * buf: C string containing a transport 815262a0982SChuck Lever * name and an unsigned integer value 816262a0982SChuck Lever * representing the port to listen on, 817262a0982SChuck Lever * separated by whitespace 818262a0982SChuck Lever * size: non-zero length of C string in @buf 819262a0982SChuck Lever * Output: 820262a0982SChuck Lever * On success: returns zero; NFS service is started 821262a0982SChuck Lever * On error: return code is a negative errno value 822262a0982SChuck Lever */ 823bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size) 824bedbdd8bSNeil Brown { 825bedbdd8bSNeil Brown ssize_t rv; 8263dd98a3bSJeff Layton 827bedbdd8bSNeil Brown mutex_lock(&nfsd_mutex); 828244c7d44SAl Viro rv = __write_ports(file, buf, size, netns(file)); 829bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 830bedbdd8bSNeil Brown return rv; 831bedbdd8bSNeil Brown } 832bedbdd8bSNeil Brown 833bedbdd8bSNeil Brown 834596bbe53SNeilBrown int nfsd_max_blksize; 835596bbe53SNeilBrown 836f2453978SChuck Lever /* 837262a0982SChuck Lever * write_maxblksize - Set or report the current NFS blksize 838262a0982SChuck Lever * 839262a0982SChuck Lever * Input: 840262a0982SChuck Lever * buf: ignored 841262a0982SChuck Lever * size: zero 842262a0982SChuck Lever * 843262a0982SChuck Lever * OR 844262a0982SChuck Lever * 845262a0982SChuck Lever * Input: 846262a0982SChuck Lever * buf: C string containing an unsigned 847262a0982SChuck Lever * integer value representing the new 848262a0982SChuck Lever * NFS blksize 849262a0982SChuck Lever * size: non-zero length of C string in @buf 850262a0982SChuck Lever * Output: 851262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C string 852262a0982SChuck Lever * containing numeric value of the current NFS blksize 853262a0982SChuck Lever * setting; 854262a0982SChuck Lever * return code is the size in bytes of the string 855262a0982SChuck Lever * On error: return code is zero or a negative errno value 856262a0982SChuck Lever */ 857596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size) 858596bbe53SNeilBrown { 859596bbe53SNeilBrown char *mesg = buf; 860244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 8619dd9845fSStanislav Kinsbursky 862596bbe53SNeilBrown if (size > 0) { 863596bbe53SNeilBrown int bsize; 864596bbe53SNeilBrown int rv = get_int(&mesg, &bsize); 865596bbe53SNeilBrown if (rv) 866596bbe53SNeilBrown return rv; 86739d432fcSChuck Lever trace_nfsd_ctl_maxblksize(netns(file), bsize); 86839d432fcSChuck Lever 869596bbe53SNeilBrown /* force bsize into allowed range and 870596bbe53SNeilBrown * required alignment. 871596bbe53SNeilBrown */ 8723c7aa15dSKinglong Mee bsize = max_t(int, bsize, 1024); 8733c7aa15dSKinglong Mee bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE); 874596bbe53SNeilBrown bsize &= ~(1024-1); 875bedbdd8bSNeil Brown mutex_lock(&nfsd_mutex); 8769dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) { 877bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 878596bbe53SNeilBrown return -EBUSY; 879596bbe53SNeilBrown } 880596bbe53SNeilBrown nfsd_max_blksize = bsize; 881bedbdd8bSNeil Brown mutex_unlock(&nfsd_mutex); 882596bbe53SNeilBrown } 883e06b6405SChuck Lever 884e06b6405SChuck Lever return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", 885e06b6405SChuck Lever nfsd_max_blksize); 886596bbe53SNeilBrown } 887596bbe53SNeilBrown 888f2453978SChuck Lever /* 8895b8db00bSJeff Layton * write_maxconn - Set or report the current max number of connections 8905b8db00bSJeff Layton * 8915b8db00bSJeff Layton * Input: 8925b8db00bSJeff Layton * buf: ignored 8935b8db00bSJeff Layton * size: zero 8945b8db00bSJeff Layton * OR 8955b8db00bSJeff Layton * 8965b8db00bSJeff Layton * Input: 8975b8db00bSJeff Layton * buf: C string containing an unsigned 8985b8db00bSJeff Layton * integer value representing the new 8995b8db00bSJeff Layton * number of max connections 9005b8db00bSJeff Layton * size: non-zero length of C string in @buf 9015b8db00bSJeff Layton * Output: 9025b8db00bSJeff Layton * On success: passed-in buffer filled with '\n'-terminated C string 9035b8db00bSJeff Layton * containing numeric value of max_connections setting 9045b8db00bSJeff Layton * for this net namespace; 9055b8db00bSJeff Layton * return code is the size in bytes of the string 9065b8db00bSJeff Layton * On error: return code is zero or a negative errno value 9075b8db00bSJeff Layton */ 9085b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size) 9095b8db00bSJeff Layton { 9105b8db00bSJeff Layton char *mesg = buf; 911244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 9125b8db00bSJeff Layton unsigned int maxconn = nn->max_connections; 9135b8db00bSJeff Layton 9145b8db00bSJeff Layton if (size > 0) { 9155b8db00bSJeff Layton int rv = get_uint(&mesg, &maxconn); 9165b8db00bSJeff Layton 9175b8db00bSJeff Layton if (rv) 9185b8db00bSJeff Layton return rv; 91939d432fcSChuck Lever trace_nfsd_ctl_maxconn(netns(file), maxconn); 9205b8db00bSJeff Layton nn->max_connections = maxconn; 9215b8db00bSJeff Layton } 9225b8db00bSJeff Layton 9235b8db00bSJeff Layton return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn); 9245b8db00bSJeff Layton } 9255b8db00bSJeff Layton 92670c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4 9279dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, 92820b7d86fSArnd Bergmann time64_t *time, struct nfsd_net *nn) 9291da177e4SLinus Torvalds { 93039d432fcSChuck Lever struct dentry *dentry = file_dentry(file); 9311da177e4SLinus Torvalds char *mesg = buf; 932f0135740SJ. Bruce Fields int rv, i; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds if (size > 0) { 9359dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) 9363dd98a3bSJeff Layton return -EBUSY; 937f0135740SJ. Bruce Fields rv = get_int(&mesg, &i); 9381da177e4SLinus Torvalds if (rv) 9391da177e4SLinus Torvalds return rv; 94039d432fcSChuck Lever trace_nfsd_ctl_time(netns(file), dentry->d_name.name, 94139d432fcSChuck Lever dentry->d_name.len, i); 94239d432fcSChuck Lever 943e7b184f1SJ. Bruce Fields /* 944e7b184f1SJ. Bruce Fields * Some sanity checking. We don't have a reason for 945e7b184f1SJ. Bruce Fields * these particular numbers, but problems with the 946e7b184f1SJ. Bruce Fields * extremes are: 947e7b184f1SJ. Bruce Fields * - Too short: the briefest network outage may 948e7b184f1SJ. Bruce Fields * cause clients to lose all their locks. Also, 949e7b184f1SJ. Bruce Fields * the frequent polling may be wasteful. 950e7b184f1SJ. Bruce Fields * - Too long: do you really want reboot recovery 951e7b184f1SJ. Bruce Fields * to take more than an hour? Or to make other 952e7b184f1SJ. Bruce Fields * clients wait an hour before being able to 953e7b184f1SJ. Bruce Fields * revoke a dead client's locks? 954e7b184f1SJ. Bruce Fields */ 955f0135740SJ. Bruce Fields if (i < 10 || i > 3600) 9561da177e4SLinus Torvalds return -EINVAL; 957f0135740SJ. Bruce Fields *time = i; 9581da177e4SLinus Torvalds } 959e06b6405SChuck Lever 96020b7d86fSArnd Bergmann return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time); 961f0135740SJ. Bruce Fields } 962f0135740SJ. Bruce Fields 9639dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, 96420b7d86fSArnd Bergmann time64_t *time, struct nfsd_net *nn) 965f0135740SJ. Bruce Fields { 966f0135740SJ. Bruce Fields ssize_t rv; 967f0135740SJ. Bruce Fields 968f0135740SJ. Bruce Fields mutex_lock(&nfsd_mutex); 9699dd9845fSStanislav Kinsbursky rv = __nfsd4_write_time(file, buf, size, time, nn); 970f0135740SJ. Bruce Fields mutex_unlock(&nfsd_mutex); 971f0135740SJ. Bruce Fields return rv; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 974f2453978SChuck Lever /* 975262a0982SChuck Lever * write_leasetime - Set or report the current NFSv4 lease time 976262a0982SChuck Lever * 977262a0982SChuck Lever * Input: 978262a0982SChuck Lever * buf: ignored 979262a0982SChuck Lever * size: zero 980262a0982SChuck Lever * 981262a0982SChuck Lever * OR 982262a0982SChuck Lever * 983262a0982SChuck Lever * Input: 984262a0982SChuck Lever * buf: C string containing an unsigned 985262a0982SChuck Lever * integer value representing the new 986262a0982SChuck Lever * NFSv4 lease expiry time 987262a0982SChuck Lever * size: non-zero length of C string in @buf 988262a0982SChuck Lever * Output: 989262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C 990262a0982SChuck Lever * string containing unsigned integer value of the 991262a0982SChuck Lever * current lease expiry time; 992262a0982SChuck Lever * return code is the size in bytes of the string 993262a0982SChuck Lever * On error: return code is zero or a negative errno value 994262a0982SChuck Lever */ 9953dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size) 9963dd98a3bSJeff Layton { 997244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 9989dd9845fSStanislav Kinsbursky return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn); 9993dd98a3bSJeff Layton } 10003dd98a3bSJeff Layton 1001f2453978SChuck Lever /* 1002efc4bb4fSJ. Bruce Fields * write_gracetime - Set or report current NFSv4 grace period time 1003efc4bb4fSJ. Bruce Fields * 1004efc4bb4fSJ. Bruce Fields * As above, but sets the time of the NFSv4 grace period. 1005efc4bb4fSJ. Bruce Fields * 1006efc4bb4fSJ. Bruce Fields * Note this should never be set to less than the *previous* 1007efc4bb4fSJ. Bruce Fields * lease-period time, but we don't try to enforce this. (In the common 1008efc4bb4fSJ. Bruce Fields * case (a new boot), we don't know what the previous lease time was 1009efc4bb4fSJ. Bruce Fields * anyway.) 1010efc4bb4fSJ. Bruce Fields */ 1011efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size) 1012efc4bb4fSJ. Bruce Fields { 1013244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 10149dd9845fSStanislav Kinsbursky return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn); 1015efc4bb4fSJ. Bruce Fields } 1016efc4bb4fSJ. Bruce Fields 101774fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 10189dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size, 10199dd9845fSStanislav Kinsbursky struct nfsd_net *nn) 10200964a3d3SNeilBrown { 10210964a3d3SNeilBrown char *mesg = buf; 10220964a3d3SNeilBrown char *recdir; 10230964a3d3SNeilBrown int len, status; 10240964a3d3SNeilBrown 10253dd98a3bSJeff Layton if (size > 0) { 10269dd9845fSStanislav Kinsbursky if (nn->nfsd_serv) 10273dd98a3bSJeff Layton return -EBUSY; 10283dd98a3bSJeff Layton if (size > PATH_MAX || buf[size-1] != '\n') 10290964a3d3SNeilBrown return -EINVAL; 10300964a3d3SNeilBrown buf[size-1] = 0; 10310964a3d3SNeilBrown 10320964a3d3SNeilBrown recdir = mesg; 10330964a3d3SNeilBrown len = qword_get(&mesg, recdir, size); 10340964a3d3SNeilBrown if (len <= 0) 10350964a3d3SNeilBrown return -EINVAL; 103639d432fcSChuck Lever trace_nfsd_ctl_recoverydir(netns(file), recdir); 10370964a3d3SNeilBrown 10380964a3d3SNeilBrown status = nfs4_reset_recoverydir(recdir); 103969049961SAndi Kleen if (status) 104069049961SAndi Kleen return status; 10413dd98a3bSJeff Layton } 10423d72ab8fSChuck Lever 10433d72ab8fSChuck Lever return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n", 10443d72ab8fSChuck Lever nfs4_recoverydir()); 10450964a3d3SNeilBrown } 10463dd98a3bSJeff Layton 1047f2453978SChuck Lever /* 1048262a0982SChuck Lever * write_recoverydir - Set or report the pathname of the recovery directory 1049262a0982SChuck Lever * 1050262a0982SChuck Lever * Input: 1051262a0982SChuck Lever * buf: ignored 1052262a0982SChuck Lever * size: zero 1053262a0982SChuck Lever * 1054262a0982SChuck Lever * OR 1055262a0982SChuck Lever * 1056262a0982SChuck Lever * Input: 1057262a0982SChuck Lever * buf: C string containing the pathname 1058262a0982SChuck Lever * of the directory on a local file 1059262a0982SChuck Lever * system containing permanent NFSv4 1060262a0982SChuck Lever * recovery data 1061262a0982SChuck Lever * size: non-zero length of C string in @buf 1062262a0982SChuck Lever * Output: 1063262a0982SChuck Lever * On success: passed-in buffer filled with '\n'-terminated C string 1064262a0982SChuck Lever * containing the current recovery pathname setting; 1065262a0982SChuck Lever * return code is the size in bytes of the string 1066262a0982SChuck Lever * On error: return code is zero or a negative errno value 1067262a0982SChuck Lever */ 10683dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) 10693dd98a3bSJeff Layton { 10703dd98a3bSJeff Layton ssize_t rv; 1071244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 10723dd98a3bSJeff Layton 10733dd98a3bSJeff Layton mutex_lock(&nfsd_mutex); 10749dd9845fSStanislav Kinsbursky rv = __write_recoverydir(file, buf, size, nn); 10753dd98a3bSJeff Layton mutex_unlock(&nfsd_mutex); 10763dd98a3bSJeff Layton return rv; 10773dd98a3bSJeff Layton } 107874fd4873SJeff Layton #endif 10793dd98a3bSJeff Layton 1080f2453978SChuck Lever /* 10817f5ef2e9SJeff Layton * write_v4_end_grace - release grace period for nfsd's v4.x lock manager 10827f5ef2e9SJeff Layton * 10837f5ef2e9SJeff Layton * Input: 10847f5ef2e9SJeff Layton * buf: ignored 10857f5ef2e9SJeff Layton * size: zero 10867f5ef2e9SJeff Layton * OR 10877f5ef2e9SJeff Layton * 10887f5ef2e9SJeff Layton * Input: 10897f5ef2e9SJeff Layton * buf: any value 10907f5ef2e9SJeff Layton * size: non-zero length of C string in @buf 10917f5ef2e9SJeff Layton * Output: 10927f5ef2e9SJeff Layton * passed-in buffer filled with "Y" or "N" with a newline 10937f5ef2e9SJeff Layton * and NULL-terminated C string. This indicates whether 10947f5ef2e9SJeff Layton * the grace period has ended in the current net 10957f5ef2e9SJeff Layton * namespace. Return code is the size in bytes of the 10967f5ef2e9SJeff Layton * string. Writing a string that starts with 'Y', 'y', or 10977f5ef2e9SJeff Layton * '1' to the file will end the grace period for nfsd's v4 10987f5ef2e9SJeff Layton * lock manager. 10997f5ef2e9SJeff Layton */ 11007f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) 11017f5ef2e9SJeff Layton { 1102244c7d44SAl Viro struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 11037f5ef2e9SJeff Layton 11047f5ef2e9SJeff Layton if (size > 0) { 11057f5ef2e9SJeff Layton switch(buf[0]) { 11067f5ef2e9SJeff Layton case 'Y': 11077f5ef2e9SJeff Layton case 'y': 11087f5ef2e9SJeff Layton case '1': 1109dd838821SYihao Wu if (!nn->nfsd_serv) 111062a063b8SJ. Bruce Fields return -EBUSY; 111139d432fcSChuck Lever trace_nfsd_end_grace(netns(file)); 11128073a98eSChuck Lever nfsd4_end_grace(nn); 11137f5ef2e9SJeff Layton break; 11147f5ef2e9SJeff Layton default: 11157f5ef2e9SJeff Layton return -EINVAL; 11167f5ef2e9SJeff Layton } 11177f5ef2e9SJeff Layton } 11187f5ef2e9SJeff Layton 11197f5ef2e9SJeff Layton return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n", 11207f5ef2e9SJeff Layton nn->grace_ended ? 'Y' : 'N'); 11217f5ef2e9SJeff Layton } 11227f5ef2e9SJeff Layton 112370c3b76cSNeilBrown #endif 11240964a3d3SNeilBrown 11251da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/ 11261da177e4SLinus Torvalds /* 11271da177e4SLinus Torvalds * populating the filesystem. 11281da177e4SLinus Torvalds */ 11291da177e4SLinus Torvalds 1130e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */ 1131e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode) 1132e8a79fb1SJ. Bruce Fields { 1133e8a79fb1SJ. Bruce Fields struct inode *inode = new_inode(sb); 1134e8a79fb1SJ. Bruce Fields if (!inode) 1135e8a79fb1SJ. Bruce Fields return NULL; 1136e8a79fb1SJ. Bruce Fields /* Following advice from simple_fill_super documentation: */ 1137e8a79fb1SJ. Bruce Fields inode->i_ino = iunique(sb, NFSD_MaxReserved); 1138e8a79fb1SJ. Bruce Fields inode->i_mode = mode; 113911fec9b9SJeff Layton simple_inode_init_ts(inode); 1140e8a79fb1SJ. Bruce Fields switch (mode & S_IFMT) { 1141e8a79fb1SJ. Bruce Fields case S_IFDIR: 1142e8a79fb1SJ. Bruce Fields inode->i_fop = &simple_dir_operations; 1143e8a79fb1SJ. Bruce Fields inode->i_op = &simple_dir_inode_operations; 1144e8a79fb1SJ. Bruce Fields inc_nlink(inode); 114576c50eb7SGustavo A. R. Silva break; 11464df750c9SChuck Lever case S_IFLNK: 11474df750c9SChuck Lever inode->i_op = &simple_symlink_inode_operations; 11484df750c9SChuck Lever break; 1149e8a79fb1SJ. Bruce Fields default: 1150e8a79fb1SJ. Bruce Fields break; 1151e8a79fb1SJ. Bruce Fields } 1152e8a79fb1SJ. Bruce Fields return inode; 1153e8a79fb1SJ. Bruce Fields } 1154e8a79fb1SJ. Bruce Fields 1155bebd6997SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl) 1156e8a79fb1SJ. Bruce Fields { 1157e8a79fb1SJ. Bruce Fields struct inode *inode; 1158e8a79fb1SJ. Bruce Fields 1159e8a79fb1SJ. Bruce Fields inode = nfsd_get_inode(dir->i_sb, mode); 1160e8a79fb1SJ. Bruce Fields if (!inode) 1161e8a79fb1SJ. Bruce Fields return -ENOMEM; 1162bebd6997SJ. Bruce Fields if (ncl) { 1163bebd6997SJ. Bruce Fields inode->i_private = ncl; 1164bebd6997SJ. Bruce Fields kref_get(&ncl->cl_ref); 1165bebd6997SJ. Bruce Fields } 1166e8a79fb1SJ. Bruce Fields d_add(dentry, inode); 1167e8a79fb1SJ. Bruce Fields inc_nlink(dir); 1168e8a79fb1SJ. Bruce Fields fsnotify_mkdir(dir, dentry); 1169e8a79fb1SJ. Bruce Fields return 0; 1170e8a79fb1SJ. Bruce Fields } 1171e8a79fb1SJ. Bruce Fields 1172e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name) 1173e8a79fb1SJ. Bruce Fields { 1174e8a79fb1SJ. Bruce Fields struct inode *dir = parent->d_inode; 1175e8a79fb1SJ. Bruce Fields struct dentry *dentry; 1176e8a79fb1SJ. Bruce Fields int ret = -ENOMEM; 1177e8a79fb1SJ. Bruce Fields 1178e8a79fb1SJ. Bruce Fields inode_lock(dir); 1179e8a79fb1SJ. Bruce Fields dentry = d_alloc_name(parent, name); 1180e8a79fb1SJ. Bruce Fields if (!dentry) 1181e8a79fb1SJ. Bruce Fields goto out_err; 1182bebd6997SJ. Bruce Fields ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl); 1183e8a79fb1SJ. Bruce Fields if (ret) 1184e8a79fb1SJ. Bruce Fields goto out_err; 1185e8a79fb1SJ. Bruce Fields out: 1186e8a79fb1SJ. Bruce Fields inode_unlock(dir); 1187e8a79fb1SJ. Bruce Fields return dentry; 1188e8a79fb1SJ. Bruce Fields out_err: 1189d6846bfbSTetsuo Handa dput(dentry); 1190e8a79fb1SJ. Bruce Fields dentry = ERR_PTR(ret); 1191e8a79fb1SJ. Bruce Fields goto out; 1192e8a79fb1SJ. Bruce Fields } 1193e8a79fb1SJ. Bruce Fields 11944df750c9SChuck Lever #if IS_ENABLED(CONFIG_SUNRPC_GSS) 11954df750c9SChuck Lever static int __nfsd_symlink(struct inode *dir, struct dentry *dentry, 11964df750c9SChuck Lever umode_t mode, const char *content) 11974df750c9SChuck Lever { 11984df750c9SChuck Lever struct inode *inode; 11994df750c9SChuck Lever 12004df750c9SChuck Lever inode = nfsd_get_inode(dir->i_sb, mode); 12014df750c9SChuck Lever if (!inode) 12024df750c9SChuck Lever return -ENOMEM; 12034df750c9SChuck Lever 12044df750c9SChuck Lever inode->i_link = (char *)content; 12054df750c9SChuck Lever inode->i_size = strlen(content); 12064df750c9SChuck Lever 12074df750c9SChuck Lever d_add(dentry, inode); 12084df750c9SChuck Lever inc_nlink(dir); 12094df750c9SChuck Lever fsnotify_create(dir, dentry); 12104df750c9SChuck Lever return 0; 12114df750c9SChuck Lever } 12124df750c9SChuck Lever 12134df750c9SChuck Lever /* 12144df750c9SChuck Lever * @content is assumed to be a NUL-terminated string that lives 12154df750c9SChuck Lever * longer than the symlink itself. 12164df750c9SChuck Lever */ 121739d432fcSChuck Lever static void _nfsd_symlink(struct dentry *parent, const char *name, 12184df750c9SChuck Lever const char *content) 12194df750c9SChuck Lever { 12204df750c9SChuck Lever struct inode *dir = parent->d_inode; 12214df750c9SChuck Lever struct dentry *dentry; 12224b471a8bSChuck Lever int ret; 12234df750c9SChuck Lever 12244df750c9SChuck Lever inode_lock(dir); 12254df750c9SChuck Lever dentry = d_alloc_name(parent, name); 12264df750c9SChuck Lever if (!dentry) 12274b471a8bSChuck Lever goto out; 12284df750c9SChuck Lever ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content); 12294df750c9SChuck Lever if (ret) 12304b471a8bSChuck Lever dput(dentry); 12314df750c9SChuck Lever out: 12324df750c9SChuck Lever inode_unlock(dir); 12334df750c9SChuck Lever } 12344df750c9SChuck Lever #else 123539d432fcSChuck Lever static inline void _nfsd_symlink(struct dentry *parent, const char *name, 12364df750c9SChuck Lever const char *content) 12374df750c9SChuck Lever { 12384df750c9SChuck Lever } 12394df750c9SChuck Lever 12404df750c9SChuck Lever #endif 12414df750c9SChuck Lever 1242b7a14708SAl Viro static void clear_ncl(struct dentry *dentry) 1243e8a79fb1SJ. Bruce Fields { 1244b7a14708SAl Viro struct inode *inode = d_inode(dentry); 124597ad4031SJ. Bruce Fields struct nfsdfs_client *ncl = inode->i_private; 124697ad4031SJ. Bruce Fields 1247b7a14708SAl Viro spin_lock(&inode->i_lock); 124897ad4031SJ. Bruce Fields inode->i_private = NULL; 1249b7a14708SAl Viro spin_unlock(&inode->i_lock); 125097ad4031SJ. Bruce Fields kref_put(&ncl->cl_ref, ncl->cl_release); 125197ad4031SJ. Bruce Fields } 125297ad4031SJ. Bruce Fields 125397ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode) 125497ad4031SJ. Bruce Fields { 125597ad4031SJ. Bruce Fields struct nfsdfs_client *nc; 125697ad4031SJ. Bruce Fields 1257b7a14708SAl Viro spin_lock(&inode->i_lock); 1258b7a14708SAl Viro nc = inode->i_private; 1259b7a14708SAl Viro if (nc) 1260b7a14708SAl Viro kref_get(&nc->cl_ref); 1261b7a14708SAl Viro spin_unlock(&inode->i_lock); 126297ad4031SJ. Bruce Fields return nc; 126397ad4031SJ. Bruce Fields } 126497ad4031SJ. Bruce Fields 126597ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share 126697ad4031SJ. Bruce Fields * code instead. */ 126797ad4031SJ. Bruce Fields static int nfsdfs_create_files(struct dentry *root, 1268472d155aSNeilBrown const struct tree_descr *files, 1269b7a14708SAl Viro struct nfsdfs_client *ncl, 1270472d155aSNeilBrown struct dentry **fdentries) 127197ad4031SJ. Bruce Fields { 127297ad4031SJ. Bruce Fields struct inode *dir = d_inode(root); 127397ad4031SJ. Bruce Fields struct inode *inode; 127497ad4031SJ. Bruce Fields struct dentry *dentry; 127597ad4031SJ. Bruce Fields int i; 127697ad4031SJ. Bruce Fields 127797ad4031SJ. Bruce Fields inode_lock(dir); 127897ad4031SJ. Bruce Fields for (i = 0; files->name && files->name[0]; i++, files++) { 127997ad4031SJ. Bruce Fields dentry = d_alloc_name(root, files->name); 128097ad4031SJ. Bruce Fields if (!dentry) 128197ad4031SJ. Bruce Fields goto out; 128297ad4031SJ. Bruce Fields inode = nfsd_get_inode(d_inode(root)->i_sb, 128397ad4031SJ. Bruce Fields S_IFREG | files->mode); 128497ad4031SJ. Bruce Fields if (!inode) { 128597ad4031SJ. Bruce Fields dput(dentry); 128697ad4031SJ. Bruce Fields goto out; 128797ad4031SJ. Bruce Fields } 1288b7a14708SAl Viro kref_get(&ncl->cl_ref); 128997ad4031SJ. Bruce Fields inode->i_fop = files->ops; 1290b7a14708SAl Viro inode->i_private = ncl; 129197ad4031SJ. Bruce Fields d_add(dentry, inode); 129297ad4031SJ. Bruce Fields fsnotify_create(dir, dentry); 1293472d155aSNeilBrown if (fdentries) 1294472d155aSNeilBrown fdentries[i] = dentry; 129597ad4031SJ. Bruce Fields } 129697ad4031SJ. Bruce Fields inode_unlock(dir); 129797ad4031SJ. Bruce Fields return 0; 129897ad4031SJ. Bruce Fields out: 129997ad4031SJ. Bruce Fields inode_unlock(dir); 130097ad4031SJ. Bruce Fields return -ENOMEM; 130197ad4031SJ. Bruce Fields } 130297ad4031SJ. Bruce Fields 130397ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */ 130497ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn, 130597ad4031SJ. Bruce Fields struct nfsdfs_client *ncl, u32 id, 1306472d155aSNeilBrown const struct tree_descr *files, 1307472d155aSNeilBrown struct dentry **fdentries) 130897ad4031SJ. Bruce Fields { 130997ad4031SJ. Bruce Fields struct dentry *dentry; 1310e8a79fb1SJ. Bruce Fields char name[11]; 131197ad4031SJ. Bruce Fields int ret; 1312e8a79fb1SJ. Bruce Fields 1313bf5ed3e3SJ. Bruce Fields sprintf(name, "%u", id); 1314e8a79fb1SJ. Bruce Fields 131597ad4031SJ. Bruce Fields dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name); 131697ad4031SJ. Bruce Fields if (IS_ERR(dentry)) /* XXX: tossing errors? */ 131797ad4031SJ. Bruce Fields return NULL; 1318b7a14708SAl Viro ret = nfsdfs_create_files(dentry, files, ncl, fdentries); 131997ad4031SJ. Bruce Fields if (ret) { 132097ad4031SJ. Bruce Fields nfsd_client_rmdir(dentry); 132197ad4031SJ. Bruce Fields return NULL; 132297ad4031SJ. Bruce Fields } 132397ad4031SJ. Bruce Fields return dentry; 1324e8a79fb1SJ. Bruce Fields } 1325e8a79fb1SJ. Bruce Fields 1326e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */ 1327e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry) 1328e8a79fb1SJ. Bruce Fields { 1329b7a14708SAl Viro simple_recursive_removal(dentry, clear_ncl); 1330e8a79fb1SJ. Bruce Fields } 1331e8a79fb1SJ. Bruce Fields 133296a374a3SDavid Howells static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc) 13331da177e4SLinus Torvalds { 1334e8a79fb1SJ. Bruce Fields struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 1335e8a79fb1SJ. Bruce Fields nfsd_net_id); 1336e8a79fb1SJ. Bruce Fields struct dentry *dentry; 1337e8a79fb1SJ. Bruce Fields int ret; 1338e8a79fb1SJ. Bruce Fields 1339cda37124SEric Biggers static const struct tree_descr nfsd_files[] = { 134096d851c4SStanislav Kinsbursky [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO}, 134120ad856eSAmir Goldstein /* Per-export io stats use same ops as exports file */ 134220ad856eSAmir Goldstein [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO}, 1343e8e8753fSJ. Bruce Fields [NFSD_Export_features] = {"export_features", 13449beeaab8SChenXiaoSong &export_features_fops, S_IRUGO}, 13454373ea84SWendy Cheng [NFSD_FO_UnlockIP] = {"unlock_ip", 13464373ea84SWendy Cheng &transaction_ops, S_IWUSR|S_IRUSR}, 134717efa372SWendy Cheng [NFSD_FO_UnlockFS] = {"unlock_filesystem", 134817efa372SWendy Cheng &transaction_ops, S_IWUSR|S_IRUSR}, 13491da177e4SLinus Torvalds [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR}, 13501da177e4SLinus Torvalds [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1351eed2965aSGreg Banks [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR}, 135203cf6c9fSGreg Banks [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO}, 135364776611SChenXiaoSong [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", 135464776611SChenXiaoSong &nfsd_reply_cache_stats_fops, S_IRUGO}, 135570c3b76cSNeilBrown [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR}, 135680212d59SNeilBrown [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, 1357596bbe53SNeilBrown [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO}, 13585b8db00bSJeff Layton [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO}, 13591342f9ddSChenXiaoSong [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO}, 13601da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4 13611da177e4SLinus Torvalds [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1362efc4bb4fSJ. Bruce Fields [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR}, 13630964a3d3SNeilBrown [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR}, 13647f5ef2e9SJeff Layton [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO}, 13651da177e4SLinus Torvalds #endif 13661da177e4SLinus Torvalds /* last one */ {""} 13671da177e4SLinus Torvalds }; 136896a374a3SDavid Howells 1369e8a79fb1SJ. Bruce Fields ret = simple_fill_super(sb, 0x6e667364, nfsd_files); 1370e8a79fb1SJ. Bruce Fields if (ret) 1371e8a79fb1SJ. Bruce Fields return ret; 137239d432fcSChuck Lever _nfsd_symlink(sb->s_root, "supported_krb5_enctypes", 13734df750c9SChuck Lever "/proc/net/rpc/gss_krb5_enctypes"); 1374e8a79fb1SJ. Bruce Fields dentry = nfsd_mkdir(sb->s_root, NULL, "clients"); 1375e8a79fb1SJ. Bruce Fields if (IS_ERR(dentry)) 1376e8a79fb1SJ. Bruce Fields return PTR_ERR(dentry); 1377e8a79fb1SJ. Bruce Fields nn->nfsd_client_dir = dentry; 1378e8a79fb1SJ. Bruce Fields return 0; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 138196a374a3SDavid Howells static int nfsd_fs_get_tree(struct fs_context *fc) 13821da177e4SLinus Torvalds { 1383533770ccSAl Viro return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns)); 138496a374a3SDavid Howells } 138596a374a3SDavid Howells 138696a374a3SDavid Howells static void nfsd_fs_free_fc(struct fs_context *fc) 138796a374a3SDavid Howells { 138896a374a3SDavid Howells if (fc->s_fs_info) 138996a374a3SDavid Howells put_net(fc->s_fs_info); 139096a374a3SDavid Howells } 139196a374a3SDavid Howells 139296a374a3SDavid Howells static const struct fs_context_operations nfsd_fs_context_ops = { 139396a374a3SDavid Howells .free = nfsd_fs_free_fc, 139496a374a3SDavid Howells .get_tree = nfsd_fs_get_tree, 139596a374a3SDavid Howells }; 139696a374a3SDavid Howells 139796a374a3SDavid Howells static int nfsd_init_fs_context(struct fs_context *fc) 139896a374a3SDavid Howells { 139996a374a3SDavid Howells put_user_ns(fc->user_ns); 140096a374a3SDavid Howells fc->user_ns = get_user_ns(fc->net_ns->user_ns); 140196a374a3SDavid Howells fc->ops = &nfsd_fs_context_ops; 140296a374a3SDavid Howells return 0; 140311f77942SStanislav Kinsbursky } 140411f77942SStanislav Kinsbursky 140511f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb) 140611f77942SStanislav Kinsbursky { 140711f77942SStanislav Kinsbursky struct net *net = sb->s_fs_info; 140811f77942SStanislav Kinsbursky 1409c6c7f2a8STrond Myklebust nfsd_shutdown_threads(net); 1410c6c7f2a8STrond Myklebust 141111f77942SStanislav Kinsbursky kill_litter_super(sb); 141211f77942SStanislav Kinsbursky put_net(net); 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = { 14161da177e4SLinus Torvalds .owner = THIS_MODULE, 14171da177e4SLinus Torvalds .name = "nfsd", 141896a374a3SDavid Howells .init_fs_context = nfsd_init_fs_context, 141911f77942SStanislav Kinsbursky .kill_sb = nfsd_umount, 14201da177e4SLinus Torvalds }; 14217f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd"); 14221da177e4SLinus Torvalds 1423e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS 1424340086daSTom Rix 1425340086daSTom Rix static int exports_proc_open(struct inode *inode, struct file *file) 1426340086daSTom Rix { 1427340086daSTom Rix return exports_net_open(current->nsproxy->net_ns, file); 1428340086daSTom Rix } 1429340086daSTom Rix 1430340086daSTom Rix static const struct proc_ops exports_proc_ops = { 1431340086daSTom Rix .proc_open = exports_proc_open, 1432340086daSTom Rix .proc_read = seq_read, 1433340086daSTom Rix .proc_lseek = seq_lseek, 1434340086daSTom Rix .proc_release = seq_release, 1435340086daSTom Rix }; 1436340086daSTom Rix 1437e331f606SJ. Bruce Fields static int create_proc_exports_entry(void) 1438e331f606SJ. Bruce Fields { 1439e331f606SJ. Bruce Fields struct proc_dir_entry *entry; 1440e331f606SJ. Bruce Fields 1441e331f606SJ. Bruce Fields entry = proc_mkdir("fs/nfs", NULL); 1442e331f606SJ. Bruce Fields if (!entry) 1443e331f606SJ. Bruce Fields return -ENOMEM; 144497a32539SAlexey Dobriyan entry = proc_create("exports", 0, entry, &exports_proc_ops); 1445ff7c4b36Sfanchaoting if (!entry) { 1446ff7c4b36Sfanchaoting remove_proc_entry("fs/nfs", NULL); 1447e331f606SJ. Bruce Fields return -ENOMEM; 1448ff7c4b36Sfanchaoting } 1449e331f606SJ. Bruce Fields return 0; 1450e331f606SJ. Bruce Fields } 1451e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */ 1452e331f606SJ. Bruce Fields static int create_proc_exports_entry(void) 1453e331f606SJ. Bruce Fields { 1454e331f606SJ. Bruce Fields return 0; 1455e331f606SJ. Bruce Fields } 1456e331f606SJ. Bruce Fields #endif 1457e331f606SJ. Bruce Fields 1458c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id; 14595717e012SStanislav Kinsbursky 14605e092be7SChuck Lever /** 1461bd9d6a3eSLorenzo Bianconi * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit 1462bd9d6a3eSLorenzo Bianconi * @cb: netlink metadata and command arguments 1463bd9d6a3eSLorenzo Bianconi * 1464bd9d6a3eSLorenzo Bianconi * Return values: 1465bd9d6a3eSLorenzo Bianconi * %0: The rpc_status_get command may proceed 1466bd9d6a3eSLorenzo Bianconi * %-ENODEV: There is no NFSD running in this namespace 1467bd9d6a3eSLorenzo Bianconi */ 146813727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb) 146913727f85SLorenzo Bianconi { 1470bd9d6a3eSLorenzo Bianconi struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id); 1471bd9d6a3eSLorenzo Bianconi int ret = -ENODEV; 1472bd9d6a3eSLorenzo Bianconi 1473bd9d6a3eSLorenzo Bianconi mutex_lock(&nfsd_mutex); 14741bd773b4SNeilBrown if (nn->nfsd_serv) 1475bd9d6a3eSLorenzo Bianconi ret = 0; 14761bd773b4SNeilBrown else 1477bd9d6a3eSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1478bd9d6a3eSLorenzo Bianconi 1479bd9d6a3eSLorenzo Bianconi return ret; 1480bd9d6a3eSLorenzo Bianconi } 1481bd9d6a3eSLorenzo Bianconi 1482bd9d6a3eSLorenzo Bianconi static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb, 1483bd9d6a3eSLorenzo Bianconi struct netlink_callback *cb, 1484bd9d6a3eSLorenzo Bianconi struct nfsd_genl_rqstp *rqstp) 1485bd9d6a3eSLorenzo Bianconi { 1486bd9d6a3eSLorenzo Bianconi void *hdr; 1487bd9d6a3eSLorenzo Bianconi u32 i; 1488bd9d6a3eSLorenzo Bianconi 1489bd9d6a3eSLorenzo Bianconi hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1490bd9d6a3eSLorenzo Bianconi &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET); 1491bd9d6a3eSLorenzo Bianconi if (!hdr) 1492bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1493bd9d6a3eSLorenzo Bianconi 1494bd9d6a3eSLorenzo Bianconi if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) || 1495bd9d6a3eSLorenzo Bianconi nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) || 1496bd9d6a3eSLorenzo Bianconi nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) || 1497bd9d6a3eSLorenzo Bianconi nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) || 1498bd9d6a3eSLorenzo Bianconi nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) || 1499bd9d6a3eSLorenzo Bianconi nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME, 1500bd9d6a3eSLorenzo Bianconi ktime_to_us(rqstp->rq_stime), 1501bd9d6a3eSLorenzo Bianconi NFSD_A_RPC_STATUS_PAD)) 1502bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1503bd9d6a3eSLorenzo Bianconi 1504bd9d6a3eSLorenzo Bianconi switch (rqstp->rq_saddr.sa_family) { 1505bd9d6a3eSLorenzo Bianconi case AF_INET: { 1506bd9d6a3eSLorenzo Bianconi const struct sockaddr_in *s_in, *d_in; 1507bd9d6a3eSLorenzo Bianconi 1508bd9d6a3eSLorenzo Bianconi s_in = (const struct sockaddr_in *)&rqstp->rq_saddr; 1509bd9d6a3eSLorenzo Bianconi d_in = (const struct sockaddr_in *)&rqstp->rq_daddr; 1510bd9d6a3eSLorenzo Bianconi if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4, 1511bd9d6a3eSLorenzo Bianconi s_in->sin_addr.s_addr) || 1512bd9d6a3eSLorenzo Bianconi nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4, 1513bd9d6a3eSLorenzo Bianconi d_in->sin_addr.s_addr) || 1514bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1515bd9d6a3eSLorenzo Bianconi s_in->sin_port) || 1516bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1517bd9d6a3eSLorenzo Bianconi d_in->sin_port)) 1518bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1519bd9d6a3eSLorenzo Bianconi break; 1520bd9d6a3eSLorenzo Bianconi } 1521bd9d6a3eSLorenzo Bianconi case AF_INET6: { 1522bd9d6a3eSLorenzo Bianconi const struct sockaddr_in6 *s_in, *d_in; 1523bd9d6a3eSLorenzo Bianconi 1524bd9d6a3eSLorenzo Bianconi s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr; 1525bd9d6a3eSLorenzo Bianconi d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr; 1526bd9d6a3eSLorenzo Bianconi if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6, 1527bd9d6a3eSLorenzo Bianconi &s_in->sin6_addr) || 1528bd9d6a3eSLorenzo Bianconi nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6, 1529bd9d6a3eSLorenzo Bianconi &d_in->sin6_addr) || 1530bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1531bd9d6a3eSLorenzo Bianconi s_in->sin6_port) || 1532bd9d6a3eSLorenzo Bianconi nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1533bd9d6a3eSLorenzo Bianconi d_in->sin6_port)) 1534bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1535bd9d6a3eSLorenzo Bianconi break; 1536bd9d6a3eSLorenzo Bianconi } 1537bd9d6a3eSLorenzo Bianconi } 1538bd9d6a3eSLorenzo Bianconi 1539bd9d6a3eSLorenzo Bianconi for (i = 0; i < rqstp->rq_opcnt; i++) 1540bd9d6a3eSLorenzo Bianconi if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS, 1541bd9d6a3eSLorenzo Bianconi rqstp->rq_opnum[i])) 1542bd9d6a3eSLorenzo Bianconi return -ENOBUFS; 1543bd9d6a3eSLorenzo Bianconi 1544bd9d6a3eSLorenzo Bianconi genlmsg_end(skb, hdr); 154513727f85SLorenzo Bianconi return 0; 154613727f85SLorenzo Bianconi } 154713727f85SLorenzo Bianconi 1548bd9d6a3eSLorenzo Bianconi /** 1549bd9d6a3eSLorenzo Bianconi * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit 1550bd9d6a3eSLorenzo Bianconi * @skb: reply buffer 1551bd9d6a3eSLorenzo Bianconi * @cb: netlink metadata and command arguments 1552bd9d6a3eSLorenzo Bianconi * 1553bd9d6a3eSLorenzo Bianconi * Returns the size of the reply or a negative errno. 1554bd9d6a3eSLorenzo Bianconi */ 155513727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, 155613727f85SLorenzo Bianconi struct netlink_callback *cb) 155713727f85SLorenzo Bianconi { 1558bd9d6a3eSLorenzo Bianconi struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id); 1559bd9d6a3eSLorenzo Bianconi int i, ret, rqstp_index = 0; 1560bd9d6a3eSLorenzo Bianconi 1561bd9d6a3eSLorenzo Bianconi rcu_read_lock(); 1562bd9d6a3eSLorenzo Bianconi 1563bd9d6a3eSLorenzo Bianconi for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) { 1564bd9d6a3eSLorenzo Bianconi struct svc_rqst *rqstp; 1565bd9d6a3eSLorenzo Bianconi 1566bd9d6a3eSLorenzo Bianconi if (i < cb->args[0]) /* already consumed */ 1567bd9d6a3eSLorenzo Bianconi continue; 1568bd9d6a3eSLorenzo Bianconi 1569bd9d6a3eSLorenzo Bianconi rqstp_index = 0; 1570bd9d6a3eSLorenzo Bianconi list_for_each_entry_rcu(rqstp, 1571bd9d6a3eSLorenzo Bianconi &nn->nfsd_serv->sv_pools[i].sp_all_threads, 1572bd9d6a3eSLorenzo Bianconi rq_all) { 1573bd9d6a3eSLorenzo Bianconi struct nfsd_genl_rqstp genl_rqstp; 1574bd9d6a3eSLorenzo Bianconi unsigned int status_counter; 1575bd9d6a3eSLorenzo Bianconi 1576bd9d6a3eSLorenzo Bianconi if (rqstp_index++ < cb->args[1]) /* already consumed */ 1577bd9d6a3eSLorenzo Bianconi continue; 1578bd9d6a3eSLorenzo Bianconi /* 1579bd9d6a3eSLorenzo Bianconi * Acquire rq_status_counter before parsing the rqst 1580bd9d6a3eSLorenzo Bianconi * fields. rq_status_counter is set to an odd value in 1581bd9d6a3eSLorenzo Bianconi * order to notify the consumers the rqstp fields are 1582bd9d6a3eSLorenzo Bianconi * meaningful. 1583bd9d6a3eSLorenzo Bianconi */ 1584bd9d6a3eSLorenzo Bianconi status_counter = 1585bd9d6a3eSLorenzo Bianconi smp_load_acquire(&rqstp->rq_status_counter); 1586bd9d6a3eSLorenzo Bianconi if (!(status_counter & 1)) 1587bd9d6a3eSLorenzo Bianconi continue; 1588bd9d6a3eSLorenzo Bianconi 1589bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_xid = rqstp->rq_xid; 1590bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_flags = rqstp->rq_flags; 1591bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_vers = rqstp->rq_vers; 1592bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_prog = rqstp->rq_prog; 1593bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_proc = rqstp->rq_proc; 1594bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_stime = rqstp->rq_stime; 1595bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_opcnt = 0; 1596bd9d6a3eSLorenzo Bianconi memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp), 1597bd9d6a3eSLorenzo Bianconi sizeof(struct sockaddr)); 1598bd9d6a3eSLorenzo Bianconi memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp), 1599bd9d6a3eSLorenzo Bianconi sizeof(struct sockaddr)); 1600bd9d6a3eSLorenzo Bianconi 1601bd9d6a3eSLorenzo Bianconi #ifdef CONFIG_NFSD_V4 1602bd9d6a3eSLorenzo Bianconi if (rqstp->rq_vers == NFS4_VERSION && 1603bd9d6a3eSLorenzo Bianconi rqstp->rq_proc == NFSPROC4_COMPOUND) { 1604bd9d6a3eSLorenzo Bianconi /* NFSv4 compound */ 1605bd9d6a3eSLorenzo Bianconi struct nfsd4_compoundargs *args; 1606bd9d6a3eSLorenzo Bianconi int j; 1607bd9d6a3eSLorenzo Bianconi 1608bd9d6a3eSLorenzo Bianconi args = rqstp->rq_argp; 1609bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_opcnt = args->opcnt; 1610bd9d6a3eSLorenzo Bianconi for (j = 0; j < genl_rqstp.rq_opcnt; j++) 1611bd9d6a3eSLorenzo Bianconi genl_rqstp.rq_opnum[j] = 1612bd9d6a3eSLorenzo Bianconi args->ops[j].opnum; 1613bd9d6a3eSLorenzo Bianconi } 1614bd9d6a3eSLorenzo Bianconi #endif /* CONFIG_NFSD_V4 */ 1615bd9d6a3eSLorenzo Bianconi 1616bd9d6a3eSLorenzo Bianconi /* 1617bd9d6a3eSLorenzo Bianconi * Acquire rq_status_counter before reporting the rqst 1618bd9d6a3eSLorenzo Bianconi * fields to the user. 1619bd9d6a3eSLorenzo Bianconi */ 1620bd9d6a3eSLorenzo Bianconi if (smp_load_acquire(&rqstp->rq_status_counter) != 1621bd9d6a3eSLorenzo Bianconi status_counter) 1622bd9d6a3eSLorenzo Bianconi continue; 1623bd9d6a3eSLorenzo Bianconi 1624bd9d6a3eSLorenzo Bianconi ret = nfsd_genl_rpc_status_compose_msg(skb, cb, 1625bd9d6a3eSLorenzo Bianconi &genl_rqstp); 1626bd9d6a3eSLorenzo Bianconi if (ret) 1627bd9d6a3eSLorenzo Bianconi goto out; 1628bd9d6a3eSLorenzo Bianconi } 162913727f85SLorenzo Bianconi } 163013727f85SLorenzo Bianconi 1631bd9d6a3eSLorenzo Bianconi cb->args[0] = i; 1632bd9d6a3eSLorenzo Bianconi cb->args[1] = rqstp_index; 1633bd9d6a3eSLorenzo Bianconi ret = skb->len; 1634bd9d6a3eSLorenzo Bianconi out: 1635bd9d6a3eSLorenzo Bianconi rcu_read_unlock(); 1636bd9d6a3eSLorenzo Bianconi 1637bd9d6a3eSLorenzo Bianconi return ret; 1638bd9d6a3eSLorenzo Bianconi } 1639bd9d6a3eSLorenzo Bianconi 1640bd9d6a3eSLorenzo Bianconi /** 1641bd9d6a3eSLorenzo Bianconi * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing 1642bd9d6a3eSLorenzo Bianconi * @cb: netlink metadata and command arguments 1643bd9d6a3eSLorenzo Bianconi * 1644bd9d6a3eSLorenzo Bianconi * Return values: 1645bd9d6a3eSLorenzo Bianconi * %0: Success 1646bd9d6a3eSLorenzo Bianconi */ 164713727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb) 164813727f85SLorenzo Bianconi { 1649bd9d6a3eSLorenzo Bianconi mutex_unlock(&nfsd_mutex); 1650bd9d6a3eSLorenzo Bianconi 165113727f85SLorenzo Bianconi return 0; 165213727f85SLorenzo Bianconi } 165313727f85SLorenzo Bianconi 16545e092be7SChuck Lever /** 16555e092be7SChuck Lever * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace 16565e092be7SChuck Lever * @net: a freshly-created network namespace 16575e092be7SChuck Lever * 16585e092be7SChuck Lever * This information stays around as long as the network namespace is 16595e092be7SChuck Lever * alive whether or not there is an NFSD instance running in the 16605e092be7SChuck Lever * namespace. 16615e092be7SChuck Lever * 16625e092be7SChuck Lever * Returns zero on success, or a negative errno otherwise. 16635e092be7SChuck Lever */ 16645e092be7SChuck Lever static __net_init int nfsd_net_init(struct net *net) 16655717e012SStanislav Kinsbursky { 16665717e012SStanislav Kinsbursky int retval; 16673d733711SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 16685717e012SStanislav Kinsbursky 16695717e012SStanislav Kinsbursky retval = nfsd_export_init(net); 16705717e012SStanislav Kinsbursky if (retval) 16715717e012SStanislav Kinsbursky goto out_export_error; 1672f69adb2fSStanislav Kinsbursky retval = nfsd_idmap_init(net); 1673f69adb2fSStanislav Kinsbursky if (retval) 1674f69adb2fSStanislav Kinsbursky goto out_idmap_error; 16754b148854SJosef Bacik retval = nfsd_stat_counters_init(nn); 1676ed9ab734SJeff Layton if (retval) 1677ed9ab734SJeff Layton goto out_repcache_error; 167816fb9808SJosef Bacik memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats)); 167916fb9808SJosef Bacik nn->nfsd_svcstats.program = &nfsd_program; 1680e333f3bbSTrond Myklebust nn->nfsd_versions = NULL; 1681e333f3bbSTrond Myklebust nn->nfsd4_minorversions = NULL; 1682f385f7d2SDai Ngo nfsd4_init_leases_net(nn); 168391d2e9b5SChuck Lever get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key)); 168491d2e9b5SChuck Lever seqlock_init(&nn->writeverf_lock); 168593483ac5SJosef Bacik nfsd_proc_stat_init(net); 16862c830dd7SJ. Bruce Fields 16875717e012SStanislav Kinsbursky return 0; 16885717e012SStanislav Kinsbursky 1689ed9ab734SJeff Layton out_repcache_error: 1690ed9ab734SJeff Layton nfsd_idmap_shutdown(net); 1691f69adb2fSStanislav Kinsbursky out_idmap_error: 1692f69adb2fSStanislav Kinsbursky nfsd_export_shutdown(net); 16935717e012SStanislav Kinsbursky out_export_error: 16945717e012SStanislav Kinsbursky return retval; 16955717e012SStanislav Kinsbursky } 16965717e012SStanislav Kinsbursky 16975e092be7SChuck Lever /** 16985e092be7SChuck Lever * nfsd_net_exit - Release the nfsd_net portion of a net namespace 16995e092be7SChuck Lever * @net: a network namespace that is about to be destroyed 17005e092be7SChuck Lever * 17015e092be7SChuck Lever */ 17025e092be7SChuck Lever static __net_exit void nfsd_net_exit(struct net *net) 17035717e012SStanislav Kinsbursky { 1704ed9ab734SJeff Layton struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1705ed9ab734SJeff Layton 170693483ac5SJosef Bacik nfsd_proc_stat_shutdown(net); 17074b148854SJosef Bacik nfsd_stat_counters_destroy(nn); 1708f69adb2fSStanislav Kinsbursky nfsd_idmap_shutdown(net); 17095717e012SStanislav Kinsbursky nfsd_export_shutdown(net); 1710ed9ab734SJeff Layton nfsd_netns_free_versions(nn); 17115717e012SStanislav Kinsbursky } 17125717e012SStanislav Kinsbursky 17137ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = { 17145e092be7SChuck Lever .init = nfsd_net_init, 17155e092be7SChuck Lever .exit = nfsd_net_exit, 17167ea34ac1SJeff Layton .id = &nfsd_net_id, 17177ea34ac1SJeff Layton .size = sizeof(struct nfsd_net), 17187ea34ac1SJeff Layton }; 17197ea34ac1SJeff Layton 17201da177e4SLinus Torvalds static int __init init_nfsd(void) 17211da177e4SLinus Torvalds { 17221da177e4SLinus Torvalds int retval; 17231da177e4SLinus Torvalds 1724bb7ffbf2SGiuseppe Cantavenera retval = nfsd4_init_slabs(); 1725bb7ffbf2SGiuseppe Cantavenera if (retval) 1726b10252c7SAlexander Sverdlin return retval; 17279cf514ccSChristoph Hellwig retval = nfsd4_init_pnfs(); 172865178db4SBryan Schumaker if (retval) 172965178db4SBryan Schumaker goto out_free_slabs; 1730027690c7SJ. Bruce Fields retval = nfsd_drc_slab_create(); 1731027690c7SJ. Bruce Fields if (retval) 17324b148854SJosef Bacik goto out_free_pnfs; 17331da177e4SLinus Torvalds nfsd_lockd_init(); /* lockd->nfsd callbacks */ 1734e331f606SJ. Bruce Fields retval = create_proc_exports_entry(); 1735e331f606SJ. Bruce Fields if (retval) 1736f69adb2fSStanislav Kinsbursky goto out_free_lockd; 1737bd5ae928SJ. Bruce Fields retval = register_pernet_subsys(&nfsd_net_ops); 1738bd5ae928SJ. Bruce Fields if (retval < 0) 17396f6f84aaSZhang Xiaoxu goto out_free_exports; 1740b10252c7SAlexander Sverdlin retval = register_cld_notifier(); 1741b10252c7SAlexander Sverdlin if (retval) 174262fdb65eSZhang Xiaoxu goto out_free_subsys; 1743d76cc46bSDai Ngo retval = nfsd4_create_laundry_wq(); 1744d76cc46bSDai Ngo if (retval) 17456f6f84aaSZhang Xiaoxu goto out_free_cld; 17466f6f84aaSZhang Xiaoxu retval = register_filesystem(&nfsd_fs_type); 17476f6f84aaSZhang Xiaoxu if (retval) 1748d76cc46bSDai Ngo goto out_free_all; 1749bd9d6a3eSLorenzo Bianconi retval = genl_register_family(&nfsd_nl_family); 1750bd9d6a3eSLorenzo Bianconi if (retval) 1751bd9d6a3eSLorenzo Bianconi goto out_free_all; 1752bd9d6a3eSLorenzo Bianconi 175326808d3fSJ. Bruce Fields return 0; 175426808d3fSJ. Bruce Fields out_free_all: 17556f6f84aaSZhang Xiaoxu nfsd4_destroy_laundry_wq(); 17566f6f84aaSZhang Xiaoxu out_free_cld: 175762fdb65eSZhang Xiaoxu unregister_cld_notifier(); 175862fdb65eSZhang Xiaoxu out_free_subsys: 1759b10252c7SAlexander Sverdlin unregister_pernet_subsys(&nfsd_net_ops); 1760bd5ae928SJ. Bruce Fields out_free_exports: 17611da177e4SLinus Torvalds remove_proc_entry("fs/nfs/exports", NULL); 17621da177e4SLinus Torvalds remove_proc_entry("fs/nfs", NULL); 1763dbf847ecSJ. Bruce Fields out_free_lockd: 17641da177e4SLinus Torvalds nfsd_lockd_shutdown(); 1765027690c7SJ. Bruce Fields nfsd_drc_slab_free(); 1766e567b98cSAmir Goldstein out_free_pnfs: 17679cf514ccSChristoph Hellwig nfsd4_exit_pnfs(); 176865178db4SBryan Schumaker out_free_slabs: 176946b25895SJ. Bruce Fields nfsd4_free_slabs(); 17701da177e4SLinus Torvalds return retval; 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds static void __exit exit_nfsd(void) 17741da177e4SLinus Torvalds { 1775bd9d6a3eSLorenzo Bianconi genl_unregister_family(&nfsd_nl_family); 17766f6f84aaSZhang Xiaoxu unregister_filesystem(&nfsd_fs_type); 1777d76cc46bSDai Ngo nfsd4_destroy_laundry_wq(); 1778b10252c7SAlexander Sverdlin unregister_cld_notifier(); 1779bd5ae928SJ. Bruce Fields unregister_pernet_subsys(&nfsd_net_ops); 1780027690c7SJ. Bruce Fields nfsd_drc_slab_free(); 17811da177e4SLinus Torvalds remove_proc_entry("fs/nfs/exports", NULL); 17821da177e4SLinus Torvalds remove_proc_entry("fs/nfs", NULL); 17831da177e4SLinus Torvalds nfsd_lockd_shutdown(); 1784e8ff2a84SJ. Bruce Fields nfsd4_free_slabs(); 17859cf514ccSChristoph Hellwig nfsd4_exit_pnfs(); 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 17895865bafaSJeff Layton MODULE_DESCRIPTION("In-kernel NFS server"); 17901da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 17911da177e4SLinus Torvalds module_init(init_nfsd) 17921da177e4SLinus Torvalds module_exit(exit_nfsd) 1793