nfsctl.c 30 KB

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