nfsctl.c 29 KB

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