nfsctl.c 29 KB

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