nfsctl.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*
  2. * Syscall interface to knfsd.
  3. *
  4. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/namei.h>
  8. #include <linux/ctype.h>
  9. #include <linux/sunrpc/svcsock.h>
  10. #include <linux/lockd/lockd.h>
  11. #include <linux/sunrpc/addr.h>
  12. #include <linux/sunrpc/gss_api.h>
  13. #include <linux/sunrpc/gss_krb5_enctypes.h>
  14. #include <linux/sunrpc/rpc_pipe_fs.h>
  15. #include <linux/module.h>
  16. #include "idmap.h"
  17. #include "nfsd.h"
  18. #include "cache.h"
  19. #include "state.h"
  20. #include "netns.h"
  21. /*
  22. * We have a single directory with several nodes in it.
  23. */
  24. enum {
  25. NFSD_Root = 1,
  26. NFSD_List,
  27. NFSD_Export_features,
  28. NFSD_Fh,
  29. NFSD_FO_UnlockIP,
  30. NFSD_FO_UnlockFS,
  31. NFSD_Threads,
  32. NFSD_Pool_Threads,
  33. NFSD_Pool_Stats,
  34. NFSD_Reply_Cache_Stats,
  35. NFSD_Versions,
  36. NFSD_Ports,
  37. NFSD_MaxBlkSize,
  38. NFSD_SupportedEnctypes,
  39. /*
  40. * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
  41. * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
  42. */
  43. #ifdef CONFIG_NFSD_V4
  44. NFSD_Leasetime,
  45. NFSD_Gracetime,
  46. NFSD_RecoveryDir,
  47. #endif
  48. };
  49. /*
  50. * write() for these nodes.
  51. */
  52. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  53. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  54. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  55. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  56. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  57. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  58. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  59. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  60. #ifdef CONFIG_NFSD_V4
  61. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  62. static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
  63. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  64. #endif
  65. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  66. [NFSD_Fh] = write_filehandle,
  67. [NFSD_FO_UnlockIP] = write_unlock_ip,
  68. [NFSD_FO_UnlockFS] = write_unlock_fs,
  69. [NFSD_Threads] = write_threads,
  70. [NFSD_Pool_Threads] = write_pool_threads,
  71. [NFSD_Versions] = write_versions,
  72. [NFSD_Ports] = write_ports,
  73. [NFSD_MaxBlkSize] = write_maxblksize,
  74. #ifdef CONFIG_NFSD_V4
  75. [NFSD_Leasetime] = write_leasetime,
  76. [NFSD_Gracetime] = write_gracetime,
  77. [NFSD_RecoveryDir] = write_recoverydir,
  78. #endif
  79. };
  80. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  81. {
  82. ino_t ino = file_inode(file)->i_ino;
  83. char *data;
  84. ssize_t rv;
  85. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  86. return -EINVAL;
  87. data = simple_transaction_get(file, buf, size);
  88. if (IS_ERR(data))
  89. return PTR_ERR(data);
  90. rv = write_op[ino](file, data, size);
  91. if (rv >= 0) {
  92. simple_transaction_set(file, rv);
  93. rv = size;
  94. }
  95. return rv;
  96. }
  97. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  98. {
  99. if (! file->private_data) {
  100. /* An attempt to read a transaction file without writing
  101. * causes a 0-byte write so that the file can return
  102. * state information
  103. */
  104. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  105. if (rv < 0)
  106. return rv;
  107. }
  108. return simple_transaction_read(file, buf, size, pos);
  109. }
  110. static const struct file_operations transaction_ops = {
  111. .write = nfsctl_transaction_write,
  112. .read = nfsctl_transaction_read,
  113. .release = simple_transaction_release,
  114. .llseek = default_llseek,
  115. };
  116. static int exports_net_open(struct net *net, struct file *file)
  117. {
  118. int err;
  119. struct seq_file *seq;
  120. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  121. err = seq_open(file, &nfs_exports_op);
  122. if (err)
  123. return err;
  124. seq = file->private_data;
  125. seq->private = nn->svc_export_cache;
  126. return 0;
  127. }
  128. static int exports_proc_open(struct inode *inode, struct file *file)
  129. {
  130. return exports_net_open(current->nsproxy->net_ns, file);
  131. }
  132. static const struct file_operations exports_proc_operations = {
  133. .open = exports_proc_open,
  134. .read = seq_read,
  135. .llseek = seq_lseek,
  136. .release = seq_release,
  137. .owner = THIS_MODULE,
  138. };
  139. static int exports_nfsd_open(struct inode *inode, struct file *file)
  140. {
  141. return exports_net_open(inode->i_sb->s_fs_info, file);
  142. }
  143. static const struct file_operations exports_nfsd_operations = {
  144. .open = exports_nfsd_open,
  145. .read = seq_read,
  146. .llseek = seq_lseek,
  147. .release = seq_release,
  148. .owner = THIS_MODULE,
  149. };
  150. static int export_features_show(struct seq_file *m, void *v)
  151. {
  152. seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
  153. return 0;
  154. }
  155. static int export_features_open(struct inode *inode, struct file *file)
  156. {
  157. return single_open(file, export_features_show, NULL);
  158. }
  159. static const struct file_operations export_features_operations = {
  160. .open = export_features_open,
  161. .read = seq_read,
  162. .llseek = seq_lseek,
  163. .release = single_release,
  164. };
  165. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  166. static int supported_enctypes_show(struct seq_file *m, void *v)
  167. {
  168. seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
  169. return 0;
  170. }
  171. static int supported_enctypes_open(struct inode *inode, struct file *file)
  172. {
  173. return single_open(file, supported_enctypes_show, NULL);
  174. }
  175. static const struct file_operations supported_enctypes_ops = {
  176. .open = supported_enctypes_open,
  177. .read = seq_read,
  178. .llseek = seq_lseek,
  179. .release = single_release,
  180. };
  181. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  182. static const struct file_operations pool_stats_operations = {
  183. .open = nfsd_pool_stats_open,
  184. .read = seq_read,
  185. .llseek = seq_lseek,
  186. .release = nfsd_pool_stats_release,
  187. .owner = THIS_MODULE,
  188. };
  189. static struct file_operations reply_cache_stats_operations = {
  190. .open = nfsd_reply_cache_stats_open,
  191. .read = seq_read,
  192. .llseek = seq_lseek,
  193. .release = single_release,
  194. };
  195. /*----------------------------------------------------------------------------*/
  196. /*
  197. * payload - write methods
  198. */
  199. /**
  200. * write_unlock_ip - Release all locks used by a client
  201. *
  202. * Experimental.
  203. *
  204. * Input:
  205. * buf: '\n'-terminated C string containing a
  206. * presentation format IP address
  207. * size: length of C string in @buf
  208. * Output:
  209. * On success: returns zero if all specified locks were released;
  210. * returns one if one or more locks were not released
  211. * On error: return code is negative errno value
  212. */
  213. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  214. {
  215. struct sockaddr_storage address;
  216. struct sockaddr *sap = (struct sockaddr *)&address;
  217. size_t salen = sizeof(address);
  218. char *fo_path;
  219. struct net *net = file->f_dentry->d_sb->s_fs_info;
  220. /* sanity check */
  221. if (size == 0)
  222. return -EINVAL;
  223. if (buf[size-1] != '\n')
  224. return -EINVAL;
  225. fo_path = buf;
  226. if (qword_get(&buf, fo_path, size) < 0)
  227. return -EINVAL;
  228. if (rpc_pton(net, fo_path, size, sap, salen) == 0)
  229. return -EINVAL;
  230. return nlmsvc_unlock_all_by_ip(sap);
  231. }
  232. /**
  233. * write_unlock_fs - Release all locks on a local file system
  234. *
  235. * Experimental.
  236. *
  237. * Input:
  238. * buf: '\n'-terminated C string containing the
  239. * absolute pathname of a local file system
  240. * size: length of C string in @buf
  241. * Output:
  242. * On success: returns zero if all specified locks were released;
  243. * returns one if one or more locks were not released
  244. * On error: return code is negative errno value
  245. */
  246. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  247. {
  248. struct path path;
  249. char *fo_path;
  250. int error;
  251. /* sanity check */
  252. if (size == 0)
  253. return -EINVAL;
  254. if (buf[size-1] != '\n')
  255. return -EINVAL;
  256. fo_path = buf;
  257. if (qword_get(&buf, fo_path, size) < 0)
  258. return -EINVAL;
  259. error = kern_path(fo_path, 0, &path);
  260. if (error)
  261. return error;
  262. /*
  263. * XXX: Needs better sanity checking. Otherwise we could end up
  264. * releasing locks on the wrong file system.
  265. *
  266. * For example:
  267. * 1. Does the path refer to a directory?
  268. * 2. Is that directory a mount point, or
  269. * 3. Is that directory the root of an exported file system?
  270. */
  271. error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
  272. path_put(&path);
  273. return error;
  274. }
  275. /**
  276. * write_filehandle - Get a variable-length NFS file handle by path
  277. *
  278. * On input, the buffer contains a '\n'-terminated C string comprised of
  279. * three alphanumeric words separated by whitespace. The string may
  280. * contain escape sequences.
  281. *
  282. * Input:
  283. * buf:
  284. * domain: client domain name
  285. * path: export pathname
  286. * maxsize: numeric maximum size of
  287. * @buf
  288. * size: length of C string in @buf
  289. * Output:
  290. * On success: passed-in buffer filled with '\n'-terminated C
  291. * string containing a ASCII hex text version
  292. * of the NFS file handle;
  293. * return code is the size in bytes of the string
  294. * On error: return code is negative errno value
  295. */
  296. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  297. {
  298. char *dname, *path;
  299. int uninitialized_var(maxsize);
  300. char *mesg = buf;
  301. int len;
  302. struct auth_domain *dom;
  303. struct knfsd_fh fh;
  304. struct net *net = file->f_dentry->d_sb->s_fs_info;
  305. if (size == 0)
  306. return -EINVAL;
  307. if (buf[size-1] != '\n')
  308. return -EINVAL;
  309. buf[size-1] = 0;
  310. dname = mesg;
  311. len = qword_get(&mesg, dname, size);
  312. if (len <= 0)
  313. return -EINVAL;
  314. path = dname+len+1;
  315. len = qword_get(&mesg, path, size);
  316. if (len <= 0)
  317. return -EINVAL;
  318. len = get_int(&mesg, &maxsize);
  319. if (len)
  320. return len;
  321. if (maxsize < NFS_FHSIZE)
  322. return -EINVAL;
  323. if (maxsize > NFS3_FHSIZE)
  324. maxsize = NFS3_FHSIZE;
  325. if (qword_get(&mesg, mesg, size)>0)
  326. return -EINVAL;
  327. /* we have all the words, they are in buf.. */
  328. dom = unix_domain_find(dname);
  329. if (!dom)
  330. return -ENOMEM;
  331. len = exp_rootfh(net, dom, path, &fh, maxsize);
  332. auth_domain_put(dom);
  333. if (len)
  334. return len;
  335. mesg = buf;
  336. len = SIMPLE_TRANSACTION_LIMIT;
  337. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  338. mesg[-1] = '\n';
  339. return mesg - buf;
  340. }
  341. /**
  342. * write_threads - Start NFSD, or report the current number of running threads
  343. *
  344. * Input:
  345. * buf: ignored
  346. * size: zero
  347. * Output:
  348. * On success: passed-in buffer filled with '\n'-terminated C
  349. * string numeric value representing the number of
  350. * running NFSD threads;
  351. * return code is the size in bytes of the string
  352. * On error: return code is zero
  353. *
  354. * OR
  355. *
  356. * Input:
  357. * buf: C string containing an unsigned
  358. * integer value representing the
  359. * number of NFSD threads to start
  360. * size: non-zero length of C string in @buf
  361. * Output:
  362. * On success: NFS service is started;
  363. * passed-in buffer filled with '\n'-terminated C
  364. * string numeric value representing the number of
  365. * running NFSD threads;
  366. * return code is the size in bytes of the string
  367. * On error: return code is zero or a negative errno value
  368. */
  369. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  370. {
  371. char *mesg = buf;
  372. int rv;
  373. struct net *net = file->f_dentry->d_sb->s_fs_info;
  374. if (size > 0) {
  375. int newthreads;
  376. rv = get_int(&mesg, &newthreads);
  377. if (rv)
  378. return rv;
  379. if (newthreads < 0)
  380. return -EINVAL;
  381. rv = nfsd_svc(newthreads, net);
  382. if (rv < 0)
  383. return rv;
  384. } else
  385. rv = nfsd_nrthreads(net);
  386. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
  387. }
  388. /**
  389. * write_pool_threads - Set or report the current number of threads per pool
  390. *
  391. * Input:
  392. * buf: ignored
  393. * size: zero
  394. *
  395. * OR
  396. *
  397. * Input:
  398. * buf: C string containing whitespace-
  399. * separated unsigned integer values
  400. * representing the number of NFSD
  401. * threads to start in each pool
  402. * size: non-zero length of C string in @buf
  403. * Output:
  404. * On success: passed-in buffer filled with '\n'-terminated C
  405. * string containing integer values representing the
  406. * number of NFSD threads in each pool;
  407. * return code is the size in bytes of the string
  408. * On error: return code is zero or a negative errno value
  409. */
  410. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  411. {
  412. /* if size > 0, look for an array of number of threads per node
  413. * and apply them then write out number of threads per node as reply
  414. */
  415. char *mesg = buf;
  416. int i;
  417. int rv;
  418. int len;
  419. int npools;
  420. int *nthreads;
  421. struct net *net = file->f_dentry->d_sb->s_fs_info;
  422. mutex_lock(&nfsd_mutex);
  423. npools = nfsd_nrpools(net);
  424. if (npools == 0) {
  425. /*
  426. * NFS is shut down. The admin can start it by
  427. * writing to the threads file but NOT the pool_threads
  428. * file, sorry. Report zero threads.
  429. */
  430. mutex_unlock(&nfsd_mutex);
  431. strcpy(buf, "0\n");
  432. return strlen(buf);
  433. }
  434. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  435. rv = -ENOMEM;
  436. if (nthreads == NULL)
  437. goto out_free;
  438. if (size > 0) {
  439. for (i = 0; i < npools; i++) {
  440. rv = get_int(&mesg, &nthreads[i]);
  441. if (rv == -ENOENT)
  442. break; /* fewer numbers than pools */
  443. if (rv)
  444. goto out_free; /* syntax error */
  445. rv = -EINVAL;
  446. if (nthreads[i] < 0)
  447. goto out_free;
  448. }
  449. rv = nfsd_set_nrthreads(i, nthreads, net);
  450. if (rv)
  451. goto out_free;
  452. }
  453. rv = nfsd_get_nrthreads(npools, nthreads, net);
  454. if (rv)
  455. goto out_free;
  456. mesg = buf;
  457. size = SIMPLE_TRANSACTION_LIMIT;
  458. for (i = 0; i < npools && size > 0; i++) {
  459. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  460. len = strlen(mesg);
  461. size -= len;
  462. mesg += len;
  463. }
  464. rv = mesg - buf;
  465. out_free:
  466. kfree(nthreads);
  467. mutex_unlock(&nfsd_mutex);
  468. return rv;
  469. }
  470. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  471. {
  472. char *mesg = buf;
  473. char *vers, *minorp, sign;
  474. int len, num, remaining;
  475. unsigned minor;
  476. ssize_t tlen = 0;
  477. char *sep;
  478. struct net *net = file->f_dentry->d_sb->s_fs_info;
  479. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  480. if (size>0) {
  481. if (nn->nfsd_serv)
  482. /* Cannot change versions without updating
  483. * nn->nfsd_serv->sv_xdrsize, and reallocing
  484. * rq_argp and rq_resp
  485. */
  486. return -EBUSY;
  487. if (buf[size-1] != '\n')
  488. return -EINVAL;
  489. buf[size-1] = 0;
  490. vers = mesg;
  491. len = qword_get(&mesg, vers, size);
  492. if (len <= 0) return -EINVAL;
  493. do {
  494. sign = *vers;
  495. if (sign == '+' || sign == '-')
  496. num = simple_strtol((vers+1), &minorp, 0);
  497. else
  498. num = simple_strtol(vers, &minorp, 0);
  499. if (*minorp == '.') {
  500. if (num != 4)
  501. return -EINVAL;
  502. minor = simple_strtoul(minorp+1, NULL, 0);
  503. if (minor == 0)
  504. return -EINVAL;
  505. if (nfsd_minorversion(minor, sign == '-' ?
  506. NFSD_CLEAR : NFSD_SET) < 0)
  507. return -EINVAL;
  508. goto next;
  509. }
  510. switch(num) {
  511. case 2:
  512. case 3:
  513. case 4:
  514. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  515. break;
  516. default:
  517. return -EINVAL;
  518. }
  519. next:
  520. vers += len + 1;
  521. } while ((len = qword_get(&mesg, vers, size)) > 0);
  522. /* If all get turned off, turn them back on, as
  523. * having no versions is BAD
  524. */
  525. nfsd_reset_versions();
  526. }
  527. /* Now write current state into reply buffer */
  528. len = 0;
  529. sep = "";
  530. remaining = SIMPLE_TRANSACTION_LIMIT;
  531. for (num=2 ; num <= 4 ; num++)
  532. if (nfsd_vers(num, NFSD_AVAIL)) {
  533. len = snprintf(buf, remaining, "%s%c%d", sep,
  534. nfsd_vers(num, NFSD_TEST)?'+':'-',
  535. num);
  536. sep = " ";
  537. if (len > remaining)
  538. break;
  539. remaining -= len;
  540. buf += len;
  541. tlen += len;
  542. }
  543. if (nfsd_vers(4, NFSD_AVAIL))
  544. for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
  545. minor++) {
  546. len = snprintf(buf, remaining, " %c4.%u",
  547. (nfsd_vers(4, NFSD_TEST) &&
  548. nfsd_minorversion(minor, NFSD_TEST)) ?
  549. '+' : '-',
  550. minor);
  551. if (len > remaining)
  552. break;
  553. remaining -= len;
  554. buf += len;
  555. tlen += len;
  556. }
  557. len = snprintf(buf, remaining, "\n");
  558. if (len > remaining)
  559. return -EINVAL;
  560. return tlen + len;
  561. }
  562. /**
  563. * write_versions - Set or report the available NFS protocol versions
  564. *
  565. * Input:
  566. * buf: ignored
  567. * size: zero
  568. * Output:
  569. * On success: passed-in buffer filled with '\n'-terminated C
  570. * string containing positive or negative integer
  571. * values representing the current status of each
  572. * protocol version;
  573. * return code is the size in bytes of the string
  574. * On error: return code is zero or a negative errno value
  575. *
  576. * OR
  577. *
  578. * Input:
  579. * buf: C string containing whitespace-
  580. * separated positive or negative
  581. * integer values representing NFS
  582. * protocol versions to enable ("+n")
  583. * or disable ("-n")
  584. * size: non-zero length of C string in @buf
  585. * Output:
  586. * On success: status of zero or more protocol versions has
  587. * been updated; passed-in buffer filled with
  588. * '\n'-terminated C string containing positive
  589. * or negative integer values representing the
  590. * current status of each protocol version;
  591. * return code is the size in bytes of the string
  592. * On error: return code is zero or a negative errno value
  593. */
  594. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  595. {
  596. ssize_t rv;
  597. mutex_lock(&nfsd_mutex);
  598. rv = __write_versions(file, buf, size);
  599. mutex_unlock(&nfsd_mutex);
  600. return rv;
  601. }
  602. /*
  603. * Zero-length write. Return a list of NFSD's current listener
  604. * transports.
  605. */
  606. static ssize_t __write_ports_names(char *buf, struct net *net)
  607. {
  608. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  609. if (nn->nfsd_serv == NULL)
  610. return 0;
  611. return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
  612. }
  613. /*
  614. * A single 'fd' number was written, in which case it must be for
  615. * a socket of a supported family/protocol, and we use it as an
  616. * nfsd listener.
  617. */
  618. static ssize_t __write_ports_addfd(char *buf, struct net *net)
  619. {
  620. char *mesg = buf;
  621. int fd, err;
  622. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  623. err = get_int(&mesg, &fd);
  624. if (err != 0 || fd < 0)
  625. return -EINVAL;
  626. err = nfsd_create_serv(net);
  627. if (err != 0)
  628. return err;
  629. err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
  630. if (err < 0) {
  631. nfsd_destroy(net);
  632. return err;
  633. }
  634. /* Decrease the count, but don't shut down the service */
  635. nn->nfsd_serv->sv_nrthreads--;
  636. return err;
  637. }
  638. /*
  639. * A transport listener is added by writing it's transport name and
  640. * a port number.
  641. */
  642. static ssize_t __write_ports_addxprt(char *buf, struct net *net)
  643. {
  644. char transport[16];
  645. struct svc_xprt *xprt;
  646. int port, err;
  647. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  648. if (sscanf(buf, "%15s %5u", transport, &port) != 2)
  649. return -EINVAL;
  650. if (port < 1 || port > USHRT_MAX)
  651. return -EINVAL;
  652. err = nfsd_create_serv(net);
  653. if (err != 0)
  654. return err;
  655. err = svc_create_xprt(nn->nfsd_serv, transport, net,
  656. PF_INET, port, SVC_SOCK_ANONYMOUS);
  657. if (err < 0)
  658. goto out_err;
  659. err = svc_create_xprt(nn->nfsd_serv, transport, net,
  660. PF_INET6, port, SVC_SOCK_ANONYMOUS);
  661. if (err < 0 && err != -EAFNOSUPPORT)
  662. goto out_close;
  663. /* Decrease the count, but don't shut down the service */
  664. nn->nfsd_serv->sv_nrthreads--;
  665. return 0;
  666. out_close:
  667. xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
  668. if (xprt != NULL) {
  669. svc_close_xprt(xprt);
  670. svc_xprt_put(xprt);
  671. }
  672. out_err:
  673. nfsd_destroy(net);
  674. return err;
  675. }
  676. static ssize_t __write_ports(struct file *file, char *buf, size_t size,
  677. struct net *net)
  678. {
  679. if (size == 0)
  680. return __write_ports_names(buf, net);
  681. if (isdigit(buf[0]))
  682. return __write_ports_addfd(buf, net);
  683. if (isalpha(buf[0]))
  684. return __write_ports_addxprt(buf, net);
  685. return -EINVAL;
  686. }
  687. /**
  688. * write_ports - Pass a socket file descriptor or transport name to listen on
  689. *
  690. * Input:
  691. * buf: ignored
  692. * size: zero
  693. * Output:
  694. * On success: passed-in buffer filled with a '\n'-terminated C
  695. * string containing a whitespace-separated list of
  696. * named NFSD listeners;
  697. * return code is the size in bytes of the string
  698. * On error: return code is zero or a negative errno value
  699. *
  700. * OR
  701. *
  702. * Input:
  703. * buf: C string containing an unsigned
  704. * integer value representing a bound
  705. * but unconnected socket that is to be
  706. * used as an NFSD listener; listen(3)
  707. * must be called for a SOCK_STREAM
  708. * socket, otherwise it is ignored
  709. * size: non-zero length of C string in @buf
  710. * Output:
  711. * On success: NFS service is started;
  712. * passed-in buffer filled with a '\n'-terminated C
  713. * string containing a unique alphanumeric name of
  714. * the listener;
  715. * return code is the size in bytes of the string
  716. * On error: return code is a negative errno value
  717. *
  718. * OR
  719. *
  720. * Input:
  721. * buf: C string containing a transport
  722. * name and an unsigned integer value
  723. * representing the port to listen on,
  724. * separated by whitespace
  725. * size: non-zero length of C string in @buf
  726. * Output:
  727. * On success: returns zero; NFS service is started
  728. * On error: return code is a negative errno value
  729. */
  730. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  731. {
  732. ssize_t rv;
  733. struct net *net = file->f_dentry->d_sb->s_fs_info;
  734. mutex_lock(&nfsd_mutex);
  735. rv = __write_ports(file, buf, size, net);
  736. mutex_unlock(&nfsd_mutex);
  737. return rv;
  738. }
  739. int nfsd_max_blksize;
  740. /**
  741. * write_maxblksize - Set or report the current NFS blksize
  742. *
  743. * Input:
  744. * buf: ignored
  745. * size: zero
  746. *
  747. * OR
  748. *
  749. * Input:
  750. * buf: C string containing an unsigned
  751. * integer value representing the new
  752. * NFS blksize
  753. * size: non-zero length of C string in @buf
  754. * Output:
  755. * On success: passed-in buffer filled with '\n'-terminated C string
  756. * containing numeric value of the current NFS blksize
  757. * setting;
  758. * return code is the size in bytes of the string
  759. * On error: return code is zero or a negative errno value
  760. */
  761. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  762. {
  763. char *mesg = buf;
  764. struct net *net = file->f_dentry->d_sb->s_fs_info;
  765. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  766. if (size > 0) {
  767. int bsize;
  768. int rv = get_int(&mesg, &bsize);
  769. if (rv)
  770. return rv;
  771. /* force bsize into allowed range and
  772. * required alignment.
  773. */
  774. if (bsize < 1024)
  775. bsize = 1024;
  776. if (bsize > NFSSVC_MAXBLKSIZE)
  777. bsize = NFSSVC_MAXBLKSIZE;
  778. bsize &= ~(1024-1);
  779. mutex_lock(&nfsd_mutex);
  780. if (nn->nfsd_serv) {
  781. mutex_unlock(&nfsd_mutex);
  782. return -EBUSY;
  783. }
  784. nfsd_max_blksize = bsize;
  785. mutex_unlock(&nfsd_mutex);
  786. }
  787. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  788. nfsd_max_blksize);
  789. }
  790. #ifdef CONFIG_NFSD_V4
  791. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
  792. time_t *time, struct nfsd_net *nn)
  793. {
  794. char *mesg = buf;
  795. int rv, i;
  796. if (size > 0) {
  797. if (nn->nfsd_serv)
  798. return -EBUSY;
  799. rv = get_int(&mesg, &i);
  800. if (rv)
  801. return rv;
  802. /*
  803. * Some sanity checking. We don't have a reason for
  804. * these particular numbers, but problems with the
  805. * extremes are:
  806. * - Too short: the briefest network outage may
  807. * cause clients to lose all their locks. Also,
  808. * the frequent polling may be wasteful.
  809. * - Too long: do you really want reboot recovery
  810. * to take more than an hour? Or to make other
  811. * clients wait an hour before being able to
  812. * revoke a dead client's locks?
  813. */
  814. if (i < 10 || i > 3600)
  815. return -EINVAL;
  816. *time = i;
  817. }
  818. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
  819. }
  820. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
  821. time_t *time, struct nfsd_net *nn)
  822. {
  823. ssize_t rv;
  824. mutex_lock(&nfsd_mutex);
  825. rv = __nfsd4_write_time(file, buf, size, time, nn);
  826. mutex_unlock(&nfsd_mutex);
  827. return rv;
  828. }
  829. /**
  830. * write_leasetime - Set or report the current NFSv4 lease time
  831. *
  832. * Input:
  833. * buf: ignored
  834. * size: zero
  835. *
  836. * OR
  837. *
  838. * Input:
  839. * buf: C string containing an unsigned
  840. * integer value representing the new
  841. * NFSv4 lease expiry time
  842. * size: non-zero length of C string in @buf
  843. * Output:
  844. * On success: passed-in buffer filled with '\n'-terminated C
  845. * string containing unsigned integer value of the
  846. * current lease expiry time;
  847. * return code is the size in bytes of the string
  848. * On error: return code is zero or a negative errno value
  849. */
  850. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  851. {
  852. struct net *net = file->f_dentry->d_sb->s_fs_info;
  853. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  854. return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
  855. }
  856. /**
  857. * write_gracetime - Set or report current NFSv4 grace period time
  858. *
  859. * As above, but sets the time of the NFSv4 grace period.
  860. *
  861. * Note this should never be set to less than the *previous*
  862. * lease-period time, but we don't try to enforce this. (In the common
  863. * case (a new boot), we don't know what the previous lease time was
  864. * anyway.)
  865. */
  866. static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
  867. {
  868. struct net *net = file->f_dentry->d_sb->s_fs_info;
  869. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  870. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
  871. }
  872. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
  873. struct nfsd_net *nn)
  874. {
  875. char *mesg = buf;
  876. char *recdir;
  877. int len, status;
  878. if (size > 0) {
  879. if (nn->nfsd_serv)
  880. return -EBUSY;
  881. if (size > PATH_MAX || buf[size-1] != '\n')
  882. return -EINVAL;
  883. buf[size-1] = 0;
  884. recdir = mesg;
  885. len = qword_get(&mesg, recdir, size);
  886. if (len <= 0)
  887. return -EINVAL;
  888. status = nfs4_reset_recoverydir(recdir);
  889. if (status)
  890. return status;
  891. }
  892. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  893. nfs4_recoverydir());
  894. }
  895. /**
  896. * write_recoverydir - Set or report the pathname of the recovery directory
  897. *
  898. * Input:
  899. * buf: ignored
  900. * size: zero
  901. *
  902. * OR
  903. *
  904. * Input:
  905. * buf: C string containing the pathname
  906. * of the directory on a local file
  907. * system containing permanent NFSv4
  908. * recovery data
  909. * size: non-zero length of C string in @buf
  910. * Output:
  911. * On success: passed-in buffer filled with '\n'-terminated C string
  912. * containing the current recovery pathname setting;
  913. * return code is the size in bytes of the string
  914. * On error: return code is zero or a negative errno value
  915. */
  916. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  917. {
  918. ssize_t rv;
  919. struct net *net = file->f_dentry->d_sb->s_fs_info;
  920. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  921. mutex_lock(&nfsd_mutex);
  922. rv = __write_recoverydir(file, buf, size, nn);
  923. mutex_unlock(&nfsd_mutex);
  924. return rv;
  925. }
  926. #endif
  927. /*----------------------------------------------------------------------------*/
  928. /*
  929. * populating the filesystem.
  930. */
  931. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  932. {
  933. static struct tree_descr nfsd_files[] = {
  934. [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
  935. [NFSD_Export_features] = {"export_features",
  936. &export_features_operations, S_IRUGO},
  937. [NFSD_FO_UnlockIP] = {"unlock_ip",
  938. &transaction_ops, S_IWUSR|S_IRUSR},
  939. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  940. &transaction_ops, S_IWUSR|S_IRUSR},
  941. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  942. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  943. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  944. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  945. [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
  946. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  947. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  948. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  949. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  950. [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
  951. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  952. #ifdef CONFIG_NFSD_V4
  953. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  954. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  955. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  956. #endif
  957. /* last one */ {""}
  958. };
  959. struct net *net = data;
  960. int ret;
  961. ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
  962. if (ret)
  963. return ret;
  964. sb->s_fs_info = get_net(net);
  965. return 0;
  966. }
  967. static struct dentry *nfsd_mount(struct file_system_type *fs_type,
  968. int flags, const char *dev_name, void *data)
  969. {
  970. return mount_ns(fs_type, flags, current->nsproxy->net_ns, nfsd_fill_super);
  971. }
  972. static void nfsd_umount(struct super_block *sb)
  973. {
  974. struct net *net = sb->s_fs_info;
  975. kill_litter_super(sb);
  976. put_net(net);
  977. }
  978. static struct file_system_type nfsd_fs_type = {
  979. .owner = THIS_MODULE,
  980. .name = "nfsd",
  981. .mount = nfsd_mount,
  982. .kill_sb = nfsd_umount,
  983. };
  984. MODULE_ALIAS_FS("nfsd");
  985. #ifdef CONFIG_PROC_FS
  986. static int create_proc_exports_entry(void)
  987. {
  988. struct proc_dir_entry *entry;
  989. entry = proc_mkdir("fs/nfs", NULL);
  990. if (!entry)
  991. return -ENOMEM;
  992. entry = proc_create("exports", 0, entry,
  993. &exports_proc_operations);
  994. if (!entry) {
  995. remove_proc_entry("fs/nfs", NULL);
  996. return -ENOMEM;
  997. }
  998. return 0;
  999. }
  1000. #else /* CONFIG_PROC_FS */
  1001. static int create_proc_exports_entry(void)
  1002. {
  1003. return 0;
  1004. }
  1005. #endif
  1006. int nfsd_net_id;
  1007. static __net_init int nfsd_init_net(struct net *net)
  1008. {
  1009. int retval;
  1010. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1011. retval = nfsd_export_init(net);
  1012. if (retval)
  1013. goto out_export_error;
  1014. retval = nfsd_idmap_init(net);
  1015. if (retval)
  1016. goto out_idmap_error;
  1017. nn->nfsd4_lease = 90; /* default lease time */
  1018. nn->nfsd4_grace = 90;
  1019. return 0;
  1020. out_idmap_error:
  1021. nfsd_export_shutdown(net);
  1022. out_export_error:
  1023. return retval;
  1024. }
  1025. static __net_exit void nfsd_exit_net(struct net *net)
  1026. {
  1027. nfsd_idmap_shutdown(net);
  1028. nfsd_export_shutdown(net);
  1029. }
  1030. static struct pernet_operations nfsd_net_ops = {
  1031. .init = nfsd_init_net,
  1032. .exit = nfsd_exit_net,
  1033. .id = &nfsd_net_id,
  1034. .size = sizeof(struct nfsd_net),
  1035. };
  1036. static int __init init_nfsd(void)
  1037. {
  1038. int retval;
  1039. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  1040. retval = register_cld_notifier();
  1041. if (retval)
  1042. return retval;
  1043. retval = register_pernet_subsys(&nfsd_net_ops);
  1044. if (retval < 0)
  1045. goto out_unregister_notifier;
  1046. retval = nfsd4_init_slabs();
  1047. if (retval)
  1048. goto out_unregister_pernet;
  1049. nfs4_state_init();
  1050. retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
  1051. if (retval)
  1052. goto out_free_slabs;
  1053. nfsd_stat_init(); /* Statistics */
  1054. retval = nfsd_reply_cache_init();
  1055. if (retval)
  1056. goto out_free_stat;
  1057. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1058. retval = create_proc_exports_entry();
  1059. if (retval)
  1060. goto out_free_lockd;
  1061. retval = register_filesystem(&nfsd_fs_type);
  1062. if (retval)
  1063. goto out_free_all;
  1064. return 0;
  1065. out_free_all:
  1066. remove_proc_entry("fs/nfs/exports", NULL);
  1067. remove_proc_entry("fs/nfs", NULL);
  1068. out_free_lockd:
  1069. nfsd_lockd_shutdown();
  1070. nfsd_reply_cache_shutdown();
  1071. out_free_stat:
  1072. nfsd_stat_shutdown();
  1073. nfsd_fault_inject_cleanup();
  1074. out_free_slabs:
  1075. nfsd4_free_slabs();
  1076. out_unregister_pernet:
  1077. unregister_pernet_subsys(&nfsd_net_ops);
  1078. out_unregister_notifier:
  1079. unregister_cld_notifier();
  1080. return retval;
  1081. }
  1082. static void __exit exit_nfsd(void)
  1083. {
  1084. nfsd_reply_cache_shutdown();
  1085. remove_proc_entry("fs/nfs/exports", NULL);
  1086. remove_proc_entry("fs/nfs", NULL);
  1087. nfsd_stat_shutdown();
  1088. nfsd_lockd_shutdown();
  1089. nfsd4_free_slabs();
  1090. nfsd_fault_inject_cleanup();
  1091. unregister_filesystem(&nfsd_fs_type);
  1092. unregister_pernet_subsys(&nfsd_net_ops);
  1093. unregister_cld_notifier();
  1094. }
  1095. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1096. MODULE_LICENSE("GPL");
  1097. module_init(init_nfsd)
  1098. module_exit(exit_nfsd)