nfsctl.c 30 KB

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