nfsctl.c 29 KB

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