nfsctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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();
  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();
  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);
  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. if (size>0) {
  455. if (nfsd_serv)
  456. /* Cannot change versions without updating
  457. * nfsd_serv->sv_xdrsize, and reallocing
  458. * rq_argp and rq_resp
  459. */
  460. return -EBUSY;
  461. if (buf[size-1] != '\n')
  462. return -EINVAL;
  463. buf[size-1] = 0;
  464. vers = mesg;
  465. len = qword_get(&mesg, vers, size);
  466. if (len <= 0) return -EINVAL;
  467. do {
  468. sign = *vers;
  469. if (sign == '+' || sign == '-')
  470. num = simple_strtol((vers+1), &minorp, 0);
  471. else
  472. num = simple_strtol(vers, &minorp, 0);
  473. if (*minorp == '.') {
  474. if (num < 4)
  475. return -EINVAL;
  476. minor = simple_strtoul(minorp+1, NULL, 0);
  477. if (minor == 0)
  478. return -EINVAL;
  479. if (nfsd_minorversion(minor, sign == '-' ?
  480. NFSD_CLEAR : NFSD_SET) < 0)
  481. return -EINVAL;
  482. goto next;
  483. }
  484. switch(num) {
  485. case 2:
  486. case 3:
  487. case 4:
  488. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  489. break;
  490. default:
  491. return -EINVAL;
  492. }
  493. next:
  494. vers += len + 1;
  495. } while ((len = qword_get(&mesg, vers, size)) > 0);
  496. /* If all get turned off, turn them back on, as
  497. * having no versions is BAD
  498. */
  499. nfsd_reset_versions();
  500. }
  501. /* Now write current state into reply buffer */
  502. len = 0;
  503. sep = "";
  504. remaining = SIMPLE_TRANSACTION_LIMIT;
  505. for (num=2 ; num <= 4 ; num++)
  506. if (nfsd_vers(num, NFSD_AVAIL)) {
  507. len = snprintf(buf, remaining, "%s%c%d", sep,
  508. nfsd_vers(num, NFSD_TEST)?'+':'-',
  509. num);
  510. sep = " ";
  511. if (len > remaining)
  512. break;
  513. remaining -= len;
  514. buf += len;
  515. tlen += len;
  516. }
  517. if (nfsd_vers(4, NFSD_AVAIL))
  518. for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
  519. minor++) {
  520. len = snprintf(buf, remaining, " %c4.%u",
  521. (nfsd_vers(4, NFSD_TEST) &&
  522. nfsd_minorversion(minor, NFSD_TEST)) ?
  523. '+' : '-',
  524. minor);
  525. if (len > remaining)
  526. break;
  527. remaining -= len;
  528. buf += len;
  529. tlen += len;
  530. }
  531. len = snprintf(buf, remaining, "\n");
  532. if (len > remaining)
  533. return -EINVAL;
  534. return tlen + len;
  535. }
  536. /**
  537. * write_versions - Set or report the available NFS protocol versions
  538. *
  539. * Input:
  540. * buf: ignored
  541. * size: zero
  542. * Output:
  543. * On success: passed-in buffer filled with '\n'-terminated C
  544. * string containing positive or negative integer
  545. * values representing the current status of each
  546. * protocol version;
  547. * return code is the size in bytes of the string
  548. * On error: return code is zero or a negative errno value
  549. *
  550. * OR
  551. *
  552. * Input:
  553. * buf: C string containing whitespace-
  554. * separated positive or negative
  555. * integer values representing NFS
  556. * protocol versions to enable ("+n")
  557. * or disable ("-n")
  558. * size: non-zero length of C string in @buf
  559. * Output:
  560. * On success: status of zero or more protocol versions has
  561. * been updated; passed-in buffer filled with
  562. * '\n'-terminated C string containing positive
  563. * or negative integer values representing the
  564. * current status of each protocol version;
  565. * return code is the size in bytes of the string
  566. * On error: return code is zero or a negative errno value
  567. */
  568. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  569. {
  570. ssize_t rv;
  571. mutex_lock(&nfsd_mutex);
  572. rv = __write_versions(file, buf, size);
  573. mutex_unlock(&nfsd_mutex);
  574. return rv;
  575. }
  576. /*
  577. * Zero-length write. Return a list of NFSD's current listener
  578. * transports.
  579. */
  580. static ssize_t __write_ports_names(char *buf)
  581. {
  582. if (nfsd_serv == NULL)
  583. return 0;
  584. return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
  585. }
  586. /*
  587. * A single 'fd' number was written, in which case it must be for
  588. * a socket of a supported family/protocol, and we use it as an
  589. * nfsd listener.
  590. */
  591. static ssize_t __write_ports_addfd(char *buf)
  592. {
  593. char *mesg = buf;
  594. int fd, err;
  595. struct net *net = &init_net;
  596. err = get_int(&mesg, &fd);
  597. if (err != 0 || fd < 0)
  598. return -EINVAL;
  599. err = nfsd_create_serv(net);
  600. if (err != 0)
  601. return err;
  602. err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
  603. if (err < 0) {
  604. nfsd_destroy(net);
  605. return err;
  606. }
  607. /* Decrease the count, but don't shut down the service */
  608. nfsd_serv->sv_nrthreads--;
  609. return err;
  610. }
  611. /*
  612. * A transport listener is added by writing it's transport name and
  613. * a port number.
  614. */
  615. static ssize_t __write_ports_addxprt(char *buf)
  616. {
  617. char transport[16];
  618. struct svc_xprt *xprt;
  619. int port, err;
  620. struct net *net = &init_net;
  621. if (sscanf(buf, "%15s %5u", transport, &port) != 2)
  622. return -EINVAL;
  623. if (port < 1 || port > USHRT_MAX)
  624. return -EINVAL;
  625. err = nfsd_create_serv(net);
  626. if (err != 0)
  627. return err;
  628. err = svc_create_xprt(nfsd_serv, transport, net,
  629. PF_INET, port, SVC_SOCK_ANONYMOUS);
  630. if (err < 0)
  631. goto out_err;
  632. err = svc_create_xprt(nfsd_serv, transport, net,
  633. PF_INET6, port, SVC_SOCK_ANONYMOUS);
  634. if (err < 0 && err != -EAFNOSUPPORT)
  635. goto out_close;
  636. /* Decrease the count, but don't shut down the service */
  637. nfsd_serv->sv_nrthreads--;
  638. return 0;
  639. out_close:
  640. xprt = svc_find_xprt(nfsd_serv, transport, net, PF_INET, port);
  641. if (xprt != NULL) {
  642. svc_close_xprt(xprt);
  643. svc_xprt_put(xprt);
  644. }
  645. out_err:
  646. nfsd_destroy(net);
  647. return err;
  648. }
  649. static ssize_t __write_ports(struct file *file, char *buf, size_t size)
  650. {
  651. if (size == 0)
  652. return __write_ports_names(buf);
  653. if (isdigit(buf[0]))
  654. return __write_ports_addfd(buf);
  655. if (isalpha(buf[0]))
  656. return __write_ports_addxprt(buf);
  657. return -EINVAL;
  658. }
  659. /**
  660. * write_ports - Pass a socket file descriptor or transport name to listen on
  661. *
  662. * Input:
  663. * buf: ignored
  664. * size: zero
  665. * Output:
  666. * On success: passed-in buffer filled with a '\n'-terminated C
  667. * string containing a whitespace-separated list of
  668. * named NFSD listeners;
  669. * return code is the size in bytes of the string
  670. * On error: return code is zero or a negative errno value
  671. *
  672. * OR
  673. *
  674. * Input:
  675. * buf: C string containing an unsigned
  676. * integer value representing a bound
  677. * but unconnected socket that is to be
  678. * used as an NFSD listener; listen(3)
  679. * must be called for a SOCK_STREAM
  680. * socket, otherwise it is ignored
  681. * size: non-zero length of C string in @buf
  682. * Output:
  683. * On success: NFS service is started;
  684. * passed-in buffer filled with a '\n'-terminated C
  685. * string containing a unique alphanumeric name of
  686. * the listener;
  687. * return code is the size in bytes of the string
  688. * On error: return code is a negative errno value
  689. *
  690. * OR
  691. *
  692. * Input:
  693. * buf: C string containing a transport
  694. * name and an unsigned integer value
  695. * representing the port to listen on,
  696. * separated by whitespace
  697. * size: non-zero length of C string in @buf
  698. * Output:
  699. * On success: returns zero; NFS service is started
  700. * On error: return code is a negative errno value
  701. */
  702. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  703. {
  704. ssize_t rv;
  705. mutex_lock(&nfsd_mutex);
  706. rv = __write_ports(file, buf, size);
  707. mutex_unlock(&nfsd_mutex);
  708. return rv;
  709. }
  710. int nfsd_max_blksize;
  711. /**
  712. * write_maxblksize - Set or report the current NFS blksize
  713. *
  714. * Input:
  715. * buf: ignored
  716. * size: zero
  717. *
  718. * OR
  719. *
  720. * Input:
  721. * buf: C string containing an unsigned
  722. * integer value representing the new
  723. * NFS blksize
  724. * size: non-zero length of C string in @buf
  725. * Output:
  726. * On success: passed-in buffer filled with '\n'-terminated C string
  727. * containing numeric value of the current NFS blksize
  728. * setting;
  729. * return code is the size in bytes of the string
  730. * On error: return code is zero or a negative errno value
  731. */
  732. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  733. {
  734. char *mesg = buf;
  735. if (size > 0) {
  736. int bsize;
  737. int rv = get_int(&mesg, &bsize);
  738. if (rv)
  739. return rv;
  740. /* force bsize into allowed range and
  741. * required alignment.
  742. */
  743. if (bsize < 1024)
  744. bsize = 1024;
  745. if (bsize > NFSSVC_MAXBLKSIZE)
  746. bsize = NFSSVC_MAXBLKSIZE;
  747. bsize &= ~(1024-1);
  748. mutex_lock(&nfsd_mutex);
  749. if (nfsd_serv) {
  750. mutex_unlock(&nfsd_mutex);
  751. return -EBUSY;
  752. }
  753. nfsd_max_blksize = bsize;
  754. mutex_unlock(&nfsd_mutex);
  755. }
  756. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  757. nfsd_max_blksize);
  758. }
  759. #ifdef CONFIG_NFSD_V4
  760. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
  761. {
  762. char *mesg = buf;
  763. int rv, i;
  764. if (size > 0) {
  765. if (nfsd_serv)
  766. return -EBUSY;
  767. rv = get_int(&mesg, &i);
  768. if (rv)
  769. return rv;
  770. /*
  771. * Some sanity checking. We don't have a reason for
  772. * these particular numbers, but problems with the
  773. * extremes are:
  774. * - Too short: the briefest network outage may
  775. * cause clients to lose all their locks. Also,
  776. * the frequent polling may be wasteful.
  777. * - Too long: do you really want reboot recovery
  778. * to take more than an hour? Or to make other
  779. * clients wait an hour before being able to
  780. * revoke a dead client's locks?
  781. */
  782. if (i < 10 || i > 3600)
  783. return -EINVAL;
  784. *time = i;
  785. }
  786. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
  787. }
  788. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
  789. {
  790. ssize_t rv;
  791. mutex_lock(&nfsd_mutex);
  792. rv = __nfsd4_write_time(file, buf, size, time);
  793. mutex_unlock(&nfsd_mutex);
  794. return rv;
  795. }
  796. /**
  797. * write_leasetime - Set or report the current NFSv4 lease time
  798. *
  799. * Input:
  800. * buf: ignored
  801. * size: zero
  802. *
  803. * OR
  804. *
  805. * Input:
  806. * buf: C string containing an unsigned
  807. * integer value representing the new
  808. * NFSv4 lease expiry time
  809. * size: non-zero length of C string in @buf
  810. * Output:
  811. * On success: passed-in buffer filled with '\n'-terminated C
  812. * string containing unsigned integer value of the
  813. * current lease expiry time;
  814. * return code is the size in bytes of the string
  815. * On error: return code is zero or a negative errno value
  816. */
  817. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  818. {
  819. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  820. return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease);
  821. }
  822. /**
  823. * write_gracetime - Set or report current NFSv4 grace period time
  824. *
  825. * As above, but sets the time of the NFSv4 grace period.
  826. *
  827. * Note this should never be set to less than the *previous*
  828. * lease-period time, but we don't try to enforce this. (In the common
  829. * case (a new boot), we don't know what the previous lease time was
  830. * anyway.)
  831. */
  832. static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
  833. {
  834. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  835. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace);
  836. }
  837. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
  838. {
  839. char *mesg = buf;
  840. char *recdir;
  841. int len, status;
  842. if (size > 0) {
  843. if (nfsd_serv)
  844. return -EBUSY;
  845. if (size > PATH_MAX || buf[size-1] != '\n')
  846. return -EINVAL;
  847. buf[size-1] = 0;
  848. recdir = mesg;
  849. len = qword_get(&mesg, recdir, size);
  850. if (len <= 0)
  851. return -EINVAL;
  852. status = nfs4_reset_recoverydir(recdir);
  853. if (status)
  854. return status;
  855. }
  856. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  857. nfs4_recoverydir());
  858. }
  859. /**
  860. * write_recoverydir - Set or report the pathname of the recovery directory
  861. *
  862. * Input:
  863. * buf: ignored
  864. * size: zero
  865. *
  866. * OR
  867. *
  868. * Input:
  869. * buf: C string containing the pathname
  870. * of the directory on a local file
  871. * system containing permanent NFSv4
  872. * recovery data
  873. * size: non-zero length of C string in @buf
  874. * Output:
  875. * On success: passed-in buffer filled with '\n'-terminated C string
  876. * containing the current recovery pathname setting;
  877. * return code is the size in bytes of the string
  878. * On error: return code is zero or a negative errno value
  879. */
  880. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  881. {
  882. ssize_t rv;
  883. mutex_lock(&nfsd_mutex);
  884. rv = __write_recoverydir(file, buf, size);
  885. mutex_unlock(&nfsd_mutex);
  886. return rv;
  887. }
  888. #endif
  889. /*----------------------------------------------------------------------------*/
  890. /*
  891. * populating the filesystem.
  892. */
  893. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  894. {
  895. static struct tree_descr nfsd_files[] = {
  896. [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
  897. [NFSD_Export_features] = {"export_features",
  898. &export_features_operations, S_IRUGO},
  899. [NFSD_FO_UnlockIP] = {"unlock_ip",
  900. &transaction_ops, S_IWUSR|S_IRUSR},
  901. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  902. &transaction_ops, S_IWUSR|S_IRUSR},
  903. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  904. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  905. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  906. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  907. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  908. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  909. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  910. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  911. [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
  912. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  913. #ifdef CONFIG_NFSD_V4
  914. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  915. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  916. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  917. #endif
  918. /* last one */ {""}
  919. };
  920. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  921. }
  922. static struct dentry *nfsd_mount(struct file_system_type *fs_type,
  923. int flags, const char *dev_name, void *data)
  924. {
  925. return mount_single(fs_type, flags, data, nfsd_fill_super);
  926. }
  927. static struct file_system_type nfsd_fs_type = {
  928. .owner = THIS_MODULE,
  929. .name = "nfsd",
  930. .mount = nfsd_mount,
  931. .kill_sb = kill_litter_super,
  932. };
  933. #ifdef CONFIG_PROC_FS
  934. static int create_proc_exports_entry(void)
  935. {
  936. struct proc_dir_entry *entry;
  937. entry = proc_mkdir("fs/nfs", NULL);
  938. if (!entry)
  939. return -ENOMEM;
  940. entry = proc_create("exports", 0, entry, &exports_operations);
  941. if (!entry)
  942. return -ENOMEM;
  943. return 0;
  944. }
  945. #else /* CONFIG_PROC_FS */
  946. static int create_proc_exports_entry(void)
  947. {
  948. return 0;
  949. }
  950. #endif
  951. int nfsd_net_id;
  952. static __net_init int nfsd_init_net(struct net *net)
  953. {
  954. int retval;
  955. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  956. retval = nfsd_export_init(net);
  957. if (retval)
  958. goto out_export_error;
  959. retval = nfsd_idmap_init(net);
  960. if (retval)
  961. goto out_idmap_error;
  962. nn->nfsd4_lease = 90; /* default lease time */
  963. nn->nfsd4_grace = 90;
  964. return 0;
  965. out_idmap_error:
  966. nfsd_export_shutdown(net);
  967. out_export_error:
  968. return retval;
  969. }
  970. static __net_exit void nfsd_exit_net(struct net *net)
  971. {
  972. nfsd_idmap_shutdown(net);
  973. nfsd_export_shutdown(net);
  974. }
  975. static struct pernet_operations nfsd_net_ops = {
  976. .init = nfsd_init_net,
  977. .exit = nfsd_exit_net,
  978. .id = &nfsd_net_id,
  979. .size = sizeof(struct nfsd_net),
  980. };
  981. static int __init init_nfsd(void)
  982. {
  983. int retval;
  984. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  985. retval = register_cld_notifier();
  986. if (retval)
  987. return retval;
  988. retval = register_pernet_subsys(&nfsd_net_ops);
  989. if (retval < 0)
  990. goto out_unregister_notifier;
  991. retval = nfsd4_init_slabs();
  992. if (retval)
  993. goto out_unregister_pernet;
  994. nfs4_state_init();
  995. retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
  996. if (retval)
  997. goto out_free_slabs;
  998. nfsd_stat_init(); /* Statistics */
  999. retval = nfsd_reply_cache_init();
  1000. if (retval)
  1001. goto out_free_stat;
  1002. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1003. retval = create_proc_exports_entry();
  1004. if (retval)
  1005. goto out_free_lockd;
  1006. retval = register_filesystem(&nfsd_fs_type);
  1007. if (retval)
  1008. goto out_free_all;
  1009. return 0;
  1010. out_free_all:
  1011. remove_proc_entry("fs/nfs/exports", NULL);
  1012. remove_proc_entry("fs/nfs", NULL);
  1013. out_free_lockd:
  1014. nfsd_lockd_shutdown();
  1015. nfsd_reply_cache_shutdown();
  1016. out_free_stat:
  1017. nfsd_stat_shutdown();
  1018. nfsd_fault_inject_cleanup();
  1019. out_free_slabs:
  1020. nfsd4_free_slabs();
  1021. out_unregister_pernet:
  1022. unregister_pernet_subsys(&nfsd_net_ops);
  1023. out_unregister_notifier:
  1024. unregister_cld_notifier();
  1025. return retval;
  1026. }
  1027. static void __exit exit_nfsd(void)
  1028. {
  1029. nfsd_reply_cache_shutdown();
  1030. remove_proc_entry("fs/nfs/exports", NULL);
  1031. remove_proc_entry("fs/nfs", NULL);
  1032. nfsd_stat_shutdown();
  1033. nfsd_lockd_shutdown();
  1034. nfsd4_free_slabs();
  1035. nfsd_fault_inject_cleanup();
  1036. unregister_filesystem(&nfsd_fs_type);
  1037. unregister_pernet_subsys(&nfsd_net_ops);
  1038. unregister_cld_notifier();
  1039. }
  1040. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1041. MODULE_LICENSE("GPL");
  1042. module_init(init_nfsd)
  1043. module_exit(exit_nfsd)