nfsctl.c 31 KB

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