nfsctl.c 30 KB

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