nfsctl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * Syscall interface to knfsd.
  3. *
  4. * Copyright (C) 1995, 1996 Olaf Kirch <[email protected]>
  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/addr.h>
  12. #include <linux/sunrpc/gss_api.h>
  13. #include <linux/sunrpc/gss_krb5_enctypes.h>
  14. #include <linux/sunrpc/rpc_pipe_fs.h>
  15. #include <linux/module.h>
  16. #include "idmap.h"
  17. #include "nfsd.h"
  18. #include "cache.h"
  19. #include "state.h"
  20. #include "netns.h"
  21. #include "pnfs.h"
  22. /*
  23. * We have a single directory with several nodes in it.
  24. */
  25. enum {
  26. NFSD_Root = 1,
  27. NFSD_List,
  28. NFSD_Export_features,
  29. NFSD_Fh,
  30. NFSD_FO_UnlockIP,
  31. NFSD_FO_UnlockFS,
  32. NFSD_Threads,
  33. NFSD_Pool_Threads,
  34. NFSD_Pool_Stats,
  35. NFSD_Reply_Cache_Stats,
  36. NFSD_Versions,
  37. NFSD_Ports,
  38. NFSD_MaxBlkSize,
  39. NFSD_MaxConnections,
  40. NFSD_SupportedEnctypes,
  41. /*
  42. * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
  43. * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
  44. */
  45. #ifdef CONFIG_NFSD_V4
  46. NFSD_Leasetime,
  47. NFSD_Gracetime,
  48. NFSD_RecoveryDir,
  49. NFSD_V4EndGrace,
  50. #endif
  51. };
  52. /*
  53. * write() for these nodes.
  54. */
  55. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  56. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  57. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  58. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  59. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  60. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  61. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  62. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  63. static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
  64. #ifdef CONFIG_NFSD_V4
  65. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  66. static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
  67. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  68. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
  69. #endif
  70. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  71. [NFSD_Fh] = write_filehandle,
  72. [NFSD_FO_UnlockIP] = write_unlock_ip,
  73. [NFSD_FO_UnlockFS] = write_unlock_fs,
  74. [NFSD_Threads] = write_threads,
  75. [NFSD_Pool_Threads] = write_pool_threads,
  76. [NFSD_Versions] = write_versions,
  77. [NFSD_Ports] = write_ports,
  78. [NFSD_MaxBlkSize] = write_maxblksize,
  79. [NFSD_MaxConnections] = write_maxconn,
  80. #ifdef CONFIG_NFSD_V4
  81. [NFSD_Leasetime] = write_leasetime,
  82. [NFSD_Gracetime] = write_gracetime,
  83. [NFSD_RecoveryDir] = write_recoverydir,
  84. [NFSD_V4EndGrace] = write_v4_end_grace,
  85. #endif
  86. };
  87. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  88. {
  89. ino_t ino = file_inode(file)->i_ino;
  90. char *data;
  91. ssize_t rv;
  92. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  93. return -EINVAL;
  94. data = simple_transaction_get(file, buf, size);
  95. if (IS_ERR(data))
  96. return PTR_ERR(data);
  97. rv = write_op[ino](file, data, size);
  98. if (rv >= 0) {
  99. simple_transaction_set(file, rv);
  100. rv = size;
  101. }
  102. return rv;
  103. }
  104. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  105. {
  106. if (! file->private_data) {
  107. /* An attempt to read a transaction file without writing
  108. * causes a 0-byte write so that the file can return
  109. * state information
  110. */
  111. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  112. if (rv < 0)
  113. return rv;
  114. }
  115. return simple_transaction_read(file, buf, size, pos);
  116. }
  117. static const struct file_operations transaction_ops = {
  118. .write = nfsctl_transaction_write,
  119. .read = nfsctl_transaction_read,
  120. .release = simple_transaction_release,
  121. .llseek = default_llseek,
  122. };
  123. static int exports_net_open(struct net *net, struct file *file)
  124. {
  125. int err;
  126. struct seq_file *seq;
  127. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  128. err = seq_open(file, &nfs_exports_op);
  129. if (err)
  130. return err;
  131. seq = file->private_data;
  132. seq->private = nn->svc_export_cache;
  133. return 0;
  134. }
  135. static int exports_proc_open(struct inode *inode, struct file *file)
  136. {
  137. return exports_net_open(current->nsproxy->net_ns, file);
  138. }
  139. static const struct file_operations exports_proc_operations = {
  140. .open = exports_proc_open,
  141. .read = seq_read,
  142. .llseek = seq_lseek,
  143. .release = seq_release,
  144. };
  145. static int exports_nfsd_open(struct inode *inode, struct file *file)
  146. {
  147. return exports_net_open(inode->i_sb->s_fs_info, file);
  148. }
  149. static const struct file_operations exports_nfsd_operations = {
  150. .open = exports_nfsd_open,
  151. .read = seq_read,
  152. .llseek = seq_lseek,
  153. .release = seq_release,
  154. };
  155. static int export_features_show(struct seq_file *m, void *v)
  156. {
  157. seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
  158. return 0;
  159. }
  160. static int export_features_open(struct inode *inode, struct file *file)
  161. {
  162. return single_open(file, export_features_show, NULL);
  163. }
  164. static const struct file_operations export_features_operations = {
  165. .open = export_features_open,
  166. .read = seq_read,
  167. .llseek = seq_lseek,
  168. .release = single_release,
  169. };
  170. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  171. static int supported_enctypes_show(struct seq_file *m, void *v)
  172. {
  173. seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
  174. return 0;
  175. }
  176. static int supported_enctypes_open(struct inode *inode, struct file *file)
  177. {
  178. return single_open(file, supported_enctypes_show, NULL);
  179. }
  180. static const struct file_operations supported_enctypes_ops = {
  181. .open = supported_enctypes_open,
  182. .read = seq_read,
  183. .llseek = seq_lseek,
  184. .release = single_release,
  185. };
  186. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  187. static const struct file_operations pool_stats_operations = {
  188. .open = nfsd_pool_stats_open,
  189. .read = seq_read,
  190. .llseek = seq_lseek,
  191. .release = nfsd_pool_stats_release,
  192. };
  193. static struct file_operations reply_cache_stats_operations = {
  194. .open = nfsd_reply_cache_stats_open,
  195. .read = seq_read,
  196. .llseek = seq_lseek,
  197. .release = single_release,
  198. };
  199. /*----------------------------------------------------------------------------*/
  200. /*
  201. * payload - write methods
  202. */
  203. static inline struct net *netns(struct file *file)
  204. {
  205. return file_inode(file)->i_sb->s_fs_info;
  206. }
  207. /**
  208. * write_unlock_ip - Release all locks used by a client
  209. *
  210. * Experimental.
  211. *
  212. * Input:
  213. * buf: '\n'-terminated C string containing a
  214. * presentation format IP address
  215. * size: length of C string in @buf
  216. * Output:
  217. * On success: returns zero if all specified locks were released;
  218. * returns one if one or more locks were not released
  219. * On error: return code is negative errno value
  220. */
  221. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  222. {
  223. struct sockaddr_storage address;
  224. struct sockaddr *sap = (struct sockaddr *)&address;
  225. size_t salen = sizeof(address);
  226. char *fo_path;
  227. struct net *net = netns(file);
  228. /* sanity check */
  229. if (size == 0)
  230. return -EINVAL;
  231. if (buf[size-1] != '\n')
  232. return -EINVAL;
  233. fo_path = buf;
  234. if (qword_get(&buf, fo_path, size) < 0)
  235. return -EINVAL;
  236. if (rpc_pton(net, fo_path, size, sap, salen) == 0)
  237. return -EINVAL;
  238. return nlmsvc_unlock_all_by_ip(sap);
  239. }
  240. /**
  241. * write_unlock_fs - Release all locks on a local file system
  242. *
  243. * Experimental.
  244. *
  245. * Input:
  246. * buf: '\n'-terminated C string containing the
  247. * absolute pathname of a local file system
  248. * size: length of C string in @buf
  249. * Output:
  250. * On success: returns zero if all specified locks were released;
  251. * returns one if one or more locks were not released
  252. * On error: return code is negative errno value
  253. */
  254. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  255. {
  256. struct path path;
  257. char *fo_path;
  258. int error;
  259. /* sanity check */
  260. if (size == 0)
  261. return -EINVAL;
  262. if (buf[size-1] != '\n')
  263. return -EINVAL;
  264. fo_path = buf;
  265. if (qword_get(&buf, fo_path, size) < 0)
  266. return -EINVAL;
  267. error = kern_path(fo_path, 0, &path);
  268. if (error)
  269. return error;
  270. /*
  271. * XXX: Needs better sanity checking. Otherwise we could end up
  272. * releasing locks on the wrong file system.
  273. *
  274. * For example:
  275. * 1. Does the path refer to a directory?
  276. * 2. Is that directory a mount point, or
  277. * 3. Is that directory the root of an exported file system?
  278. */
  279. error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
  280. path_put(&path);
  281. return error;
  282. }
  283. /**
  284. * write_filehandle - Get a variable-length NFS file handle by path
  285. *
  286. * On input, the buffer contains a '\n'-terminated C string comprised of
  287. * three alphanumeric words separated by whitespace. The string may
  288. * contain escape sequences.
  289. *
  290. * Input:
  291. * buf:
  292. * domain: client domain name
  293. * path: export pathname
  294. * maxsize: numeric maximum size of
  295. * @buf
  296. * size: length of C string in @buf
  297. * Output:
  298. * On success: passed-in buffer filled with '\n'-terminated C
  299. * string containing a ASCII hex text version
  300. * of the NFS file handle;
  301. * return code is the size in bytes of the string
  302. * On error: return code is negative errno value
  303. */
  304. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  305. {
  306. char *dname, *path;
  307. int uninitialized_var(maxsize);
  308. char *mesg = buf;
  309. int len;
  310. struct auth_domain *dom;
  311. struct knfsd_fh fh;
  312. if (size == 0)
  313. return -EINVAL;
  314. if (buf[size-1] != '\n')
  315. return -EINVAL;
  316. buf[size-1] = 0;
  317. dname = mesg;
  318. len = qword_get(&mesg, dname, size);
  319. if (len <= 0)
  320. return -EINVAL;
  321. path = dname+len+1;
  322. len = qword_get(&mesg, path, size);
  323. if (len <= 0)
  324. return -EINVAL;
  325. len = get_int(&mesg, &maxsize);
  326. if (len)
  327. return len;
  328. if (maxsize < NFS_FHSIZE)
  329. return -EINVAL;
  330. maxsize = min(maxsize, NFS3_FHSIZE);
  331. if (qword_get(&mesg, mesg, size)>0)
  332. return -EINVAL;
  333. /* we have all the words, they are in buf.. */
  334. dom = unix_domain_find(dname);
  335. if (!dom)
  336. return -ENOMEM;
  337. len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
  338. auth_domain_put(dom);
  339. if (len)
  340. return len;
  341. mesg = buf;
  342. len = SIMPLE_TRANSACTION_LIMIT;
  343. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  344. mesg[-1] = '\n';
  345. return mesg - buf;
  346. }
  347. /**
  348. * write_threads - Start NFSD, or report the current number of running threads
  349. *
  350. * Input:
  351. * buf: ignored
  352. * size: zero
  353. * Output:
  354. * On success: passed-in buffer filled with '\n'-terminated C
  355. * string numeric value representing the number of
  356. * running NFSD threads;
  357. * return code is the size in bytes of the string
  358. * On error: return code is zero
  359. *
  360. * OR
  361. *
  362. * Input:
  363. * buf: C string containing an unsigned
  364. * integer value representing the
  365. * number of NFSD threads to start
  366. * size: non-zero length of C string in @buf
  367. * Output:
  368. * On success: NFS service is started;
  369. * passed-in buffer filled with '\n'-terminated C
  370. * string numeric value representing the number of
  371. * running NFSD threads;
  372. * return code is the size in bytes of the string
  373. * On error: return code is zero or a negative errno value
  374. */
  375. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  376. {
  377. char *mesg = buf;
  378. int rv;
  379. struct net *net = netns(file);
  380. if (size > 0) {
  381. int newthreads;
  382. rv = get_int(&mesg, &newthreads);
  383. if (rv)
  384. return rv;
  385. if (newthreads < 0)
  386. return -EINVAL;
  387. rv = nfsd_svc(newthreads, net);
  388. if (rv < 0)
  389. return rv;
  390. } else
  391. rv = nfsd_nrthreads(net);
  392. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
  393. }
  394. /**
  395. * write_pool_threads - Set or report the current number of threads per pool
  396. *
  397. * Input:
  398. * buf: ignored
  399. * size: zero
  400. *
  401. * OR
  402. *
  403. * Input:
  404. * buf: C string containing whitespace-
  405. * separated unsigned integer values
  406. * representing the number of NFSD
  407. * threads to start in each pool
  408. * size: non-zero length of C string in @buf
  409. * Output:
  410. * On success: passed-in buffer filled with '\n'-terminated C
  411. * string containing integer values representing the
  412. * number of NFSD threads in each pool;
  413. * return code is the size in bytes of the string
  414. * On error: return code is zero or a negative errno value
  415. */
  416. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  417. {
  418. /* if size > 0, look for an array of number of threads per node
  419. * and apply them then write out number of threads per node as reply
  420. */
  421. char *mesg = buf;
  422. int i;
  423. int rv;
  424. int len;
  425. int npools;
  426. int *nthreads;
  427. struct net *net = netns(file);
  428. mutex_lock(&nfsd_mutex);
  429. npools = nfsd_nrpools(net);
  430. if (npools == 0) {
  431. /*
  432. * NFS is shut down. The admin can start it by
  433. * writing to the threads file but NOT the pool_threads
  434. * file, sorry. Report zero threads.
  435. */
  436. mutex_unlock(&nfsd_mutex);
  437. strcpy(buf, "0\n");
  438. return strlen(buf);
  439. }
  440. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  441. rv = -ENOMEM;
  442. if (nthreads == NULL)
  443. goto out_free;
  444. if (size > 0) {
  445. for (i = 0; i < npools; i++) {
  446. rv = get_int(&mesg, &nthreads[i]);
  447. if (rv == -ENOENT)
  448. break; /* fewer numbers than pools */
  449. if (rv)
  450. goto out_free; /* syntax error */
  451. rv = -EINVAL;
  452. if (nthreads[i] < 0)
  453. goto out_free;
  454. }
  455. rv = nfsd_set_nrthreads(i, nthreads, net);
  456. if (rv)
  457. goto out_free;
  458. }
  459. rv = nfsd_get_nrthreads(npools, nthreads, net);
  460. if (rv)
  461. goto out_free;
  462. mesg = buf;
  463. size = SIMPLE_TRANSACTION_LIMIT;
  464. for (i = 0; i < npools && size > 0; i++) {
  465. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  466. len = strlen(mesg);
  467. size -= len;
  468. mesg += len;
  469. }
  470. rv = mesg - buf;
  471. out_free:
  472. kfree(nthreads);
  473. mutex_unlock(&nfsd_mutex);
  474. return rv;
  475. }
  476. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  477. {
  478. char *mesg = buf;
  479. char *vers, *minorp, sign;
  480. int len, num, remaining;
  481. unsigned minor;
  482. ssize_t tlen = 0;
  483. char *sep;
  484. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  485. if (size>0) {
  486. if (nn->nfsd_serv)
  487. /* Cannot change versions without updating
  488. * nn->nfsd_serv->sv_xdrsize, and reallocing
  489. * rq_argp and rq_resp
  490. */
  491. return -EBUSY;
  492. if (buf[size-1] != '\n')
  493. return -EINVAL;
  494. buf[size-1] = 0;
  495. vers = mesg;
  496. len = qword_get(&mesg, vers, size);
  497. if (len <= 0) return -EINVAL;
  498. do {
  499. sign = *vers;
  500. if (sign == '+' || sign == '-')
  501. num = simple_strtol((vers+1), &minorp, 0);
  502. else
  503. num = simple_strtol(vers, &minorp, 0);
  504. if (*minorp == '.') {
  505. if (num != 4)
  506. return -EINVAL;
  507. minor = simple_strtoul(minorp+1, NULL, 0);
  508. if (minor == 0)
  509. return -EINVAL;
  510. if (nfsd_minorversion(minor, sign == '-' ?
  511. NFSD_CLEAR : NFSD_SET) < 0)
  512. return -EINVAL;
  513. goto next;
  514. }
  515. switch(num) {
  516. case 2:
  517. case 3:
  518. case 4:
  519. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  520. break;
  521. default:
  522. return -EINVAL;
  523. }
  524. next:
  525. vers += len + 1;
  526. } while ((len = qword_get(&mesg, vers, size)) > 0);
  527. /* If all get turned off, turn them back on, as
  528. * having no versions is BAD
  529. */
  530. nfsd_reset_versions();
  531. }
  532. /* Now write current state into reply buffer */
  533. len = 0;
  534. sep = "";
  535. remaining = SIMPLE_TRANSACTION_LIMIT;
  536. for (num=2 ; num <= 4 ; num++)
  537. if (nfsd_vers(num, NFSD_AVAIL)) {
  538. len = snprintf(buf, remaining, "%s%c%d", sep,
  539. nfsd_vers(num, NFSD_TEST)?'+':'-',
  540. num);
  541. sep = " ";
  542. if (len >= remaining)
  543. break;
  544. remaining -= len;
  545. buf += len;
  546. tlen += len;
  547. }
  548. if (nfsd_vers(4, NFSD_AVAIL))
  549. for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
  550. minor++) {
  551. len = snprintf(buf, remaining, " %c4.%u",
  552. (nfsd_vers(4, NFSD_TEST) &&
  553. nfsd_minorversion(minor, NFSD_TEST)) ?
  554. '+' : '-',
  555. minor);
  556. if (len >= remaining)
  557. break;
  558. remaining -= len;
  559. buf += len;
  560. tlen += len;
  561. }
  562. len = snprintf(buf, remaining, "\n");
  563. if (len >= remaining)
  564. return -EINVAL;
  565. return tlen + len;
  566. }
  567. /**
  568. * write_versions - Set or report the available NFS protocol versions
  569. *
  570. * Input:
  571. * buf: ignored
  572. * size: zero
  573. * Output:
  574. * On success: passed-in buffer filled with '\n'-terminated C
  575. * string containing positive or negative integer
  576. * values representing the current status of each
  577. * protocol version;
  578. * return code is the size in bytes of the string
  579. * On error: return code is zero or a negative errno value
  580. *
  581. * OR
  582. *
  583. * Input:
  584. * buf: C string containing whitespace-
  585. * separated positive or negative
  586. * integer values representing NFS
  587. * protocol versions to enable ("+n")
  588. * or disable ("-n")
  589. * size: non-zero length of C string in @buf
  590. * Output:
  591. * On success: status of zero or more protocol versions has
  592. * been updated; passed-in buffer filled with
  593. * '\n'-terminated C string containing positive
  594. * or negative integer values representing the
  595. * current status of each protocol version;
  596. * return code is the size in bytes of the string
  597. * On error: return code is zero or a negative errno value
  598. */
  599. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  600. {
  601. ssize_t rv;
  602. mutex_lock(&nfsd_mutex);
  603. rv = __write_versions(file, buf, size);
  604. mutex_unlock(&nfsd_mutex);
  605. return rv;
  606. }
  607. /*
  608. * Zero-length write. Return a list of NFSD's current listener
  609. * transports.
  610. */
  611. static ssize_t __write_ports_names(char *buf, struct net *net)
  612. {
  613. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  614. if (nn->nfsd_serv == NULL)
  615. return 0;
  616. return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
  617. }
  618. /*
  619. * A single 'fd' number was written, in which case it must be for
  620. * a socket of a supported family/protocol, and we use it as an
  621. * nfsd listener.
  622. */
  623. static ssize_t __write_ports_addfd(char *buf, struct net *net)
  624. {
  625. char *mesg = buf;
  626. int fd, err;
  627. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  628. err = get_int(&mesg, &fd);
  629. if (err != 0 || fd < 0)
  630. return -EINVAL;
  631. if (svc_alien_sock(net, fd)) {
  632. printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
  633. return -EINVAL;
  634. }
  635. err = nfsd_create_serv(net);
  636. if (err != 0)
  637. return err;
  638. err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
  639. if (err < 0) {
  640. nfsd_destroy(net);
  641. return err;
  642. }
  643. /* Decrease the count, but don't shut down the service */
  644. nn->nfsd_serv->sv_nrthreads--;
  645. return err;
  646. }
  647. /*
  648. * A transport listener is added by writing it's transport name and
  649. * a port number.
  650. */
  651. static ssize_t __write_ports_addxprt(char *buf, struct net *net)
  652. {
  653. char transport[16];
  654. struct svc_xprt *xprt;
  655. int port, err;
  656. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  657. if (sscanf(buf, "%15s %5u", transport, &port) != 2)
  658. return -EINVAL;
  659. if (port < 1 || port > USHRT_MAX)
  660. return -EINVAL;
  661. err = nfsd_create_serv(net);
  662. if (err != 0)
  663. return err;
  664. err = svc_create_xprt(nn->nfsd_serv, transport, net,
  665. PF_INET, port, SVC_SOCK_ANONYMOUS);
  666. if (err < 0)
  667. goto out_err;
  668. err = svc_create_xprt(nn->nfsd_serv, transport, net,
  669. PF_INET6, port, SVC_SOCK_ANONYMOUS);
  670. if (err < 0 && err != -EAFNOSUPPORT)
  671. goto out_close;
  672. /* Decrease the count, but don't shut down the service */
  673. nn->nfsd_serv->sv_nrthreads--;
  674. return 0;
  675. out_close:
  676. xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
  677. if (xprt != NULL) {
  678. svc_close_xprt(xprt);
  679. svc_xprt_put(xprt);
  680. }
  681. out_err:
  682. nfsd_destroy(net);
  683. return err;
  684. }
  685. static ssize_t __write_ports(struct file *file, char *buf, size_t size,
  686. struct net *net)
  687. {
  688. if (size == 0)
  689. return __write_ports_names(buf, net);
  690. if (isdigit(buf[0]))
  691. return __write_ports_addfd(buf, net);
  692. if (isalpha(buf[0]))
  693. return __write_ports_addxprt(buf, net);
  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 transport
  731. * name and an unsigned integer value
  732. * representing the port to listen on,
  733. * separated by whitespace
  734. * size: non-zero length of C string in @buf
  735. * Output:
  736. * On success: returns zero; NFS service is started
  737. * On error: return code is a negative errno value
  738. */
  739. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  740. {
  741. ssize_t rv;
  742. mutex_lock(&nfsd_mutex);
  743. rv = __write_ports(file, buf, size, netns(file));
  744. mutex_unlock(&nfsd_mutex);
  745. return rv;
  746. }
  747. int nfsd_max_blksize;
  748. /**
  749. * write_maxblksize - Set or report the current NFS blksize
  750. *
  751. * Input:
  752. * buf: ignored
  753. * size: zero
  754. *
  755. * OR
  756. *
  757. * Input:
  758. * buf: C string containing an unsigned
  759. * integer value representing the new
  760. * NFS blksize
  761. * size: non-zero length of C string in @buf
  762. * Output:
  763. * On success: passed-in buffer filled with '\n'-terminated C string
  764. * containing numeric value of the current NFS blksize
  765. * setting;
  766. * return code is the size in bytes of the string
  767. * On error: return code is zero or a negative errno value
  768. */
  769. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  770. {
  771. char *mesg = buf;
  772. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  773. if (size > 0) {
  774. int bsize;
  775. int rv = get_int(&mesg, &bsize);
  776. if (rv)
  777. return rv;
  778. /* force bsize into allowed range and
  779. * required alignment.
  780. */
  781. bsize = max_t(int, bsize, 1024);
  782. bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
  783. bsize &= ~(1024-1);
  784. mutex_lock(&nfsd_mutex);
  785. if (nn->nfsd_serv) {
  786. mutex_unlock(&nfsd_mutex);
  787. return -EBUSY;
  788. }
  789. nfsd_max_blksize = bsize;
  790. mutex_unlock(&nfsd_mutex);
  791. }
  792. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  793. nfsd_max_blksize);
  794. }
  795. /**
  796. * write_maxconn - Set or report the current max number of connections
  797. *
  798. * Input:
  799. * buf: ignored
  800. * size: zero
  801. * OR
  802. *
  803. * Input:
  804. * buf: C string containing an unsigned
  805. * integer value representing the new
  806. * number of max connections
  807. * size: non-zero length of C string in @buf
  808. * Output:
  809. * On success: passed-in buffer filled with '\n'-terminated C string
  810. * containing numeric value of max_connections setting
  811. * for this net namespace;
  812. * return code is the size in bytes of the string
  813. * On error: return code is zero or a negative errno value
  814. */
  815. static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
  816. {
  817. char *mesg = buf;
  818. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  819. unsigned int maxconn = nn->max_connections;
  820. if (size > 0) {
  821. int rv = get_uint(&mesg, &maxconn);
  822. if (rv)
  823. return rv;
  824. nn->max_connections = maxconn;
  825. }
  826. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
  827. }
  828. #ifdef CONFIG_NFSD_V4
  829. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
  830. time_t *time, struct nfsd_net *nn)
  831. {
  832. char *mesg = buf;
  833. int rv, i;
  834. if (size > 0) {
  835. if (nn->nfsd_serv)
  836. return -EBUSY;
  837. rv = get_int(&mesg, &i);
  838. if (rv)
  839. return rv;
  840. /*
  841. * Some sanity checking. We don't have a reason for
  842. * these particular numbers, but problems with the
  843. * extremes are:
  844. * - Too short: the briefest network outage may
  845. * cause clients to lose all their locks. Also,
  846. * the frequent polling may be wasteful.
  847. * - Too long: do you really want reboot recovery
  848. * to take more than an hour? Or to make other
  849. * clients wait an hour before being able to
  850. * revoke a dead client's locks?
  851. */
  852. if (i < 10 || i > 3600)
  853. return -EINVAL;
  854. *time = i;
  855. }
  856. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
  857. }
  858. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
  859. time_t *time, struct nfsd_net *nn)
  860. {
  861. ssize_t rv;
  862. mutex_lock(&nfsd_mutex);
  863. rv = __nfsd4_write_time(file, buf, size, time, nn);
  864. mutex_unlock(&nfsd_mutex);
  865. return rv;
  866. }
  867. /**
  868. * write_leasetime - Set or report the current NFSv4 lease time
  869. *
  870. * Input:
  871. * buf: ignored
  872. * size: zero
  873. *
  874. * OR
  875. *
  876. * Input:
  877. * buf: C string containing an unsigned
  878. * integer value representing the new
  879. * NFSv4 lease expiry time
  880. * size: non-zero length of C string in @buf
  881. * Output:
  882. * On success: passed-in buffer filled with '\n'-terminated C
  883. * string containing unsigned integer value of the
  884. * current lease expiry time;
  885. * return code is the size in bytes of the string
  886. * On error: return code is zero or a negative errno value
  887. */
  888. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  889. {
  890. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  891. return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
  892. }
  893. /**
  894. * write_gracetime - Set or report current NFSv4 grace period time
  895. *
  896. * As above, but sets the time of the NFSv4 grace period.
  897. *
  898. * Note this should never be set to less than the *previous*
  899. * lease-period time, but we don't try to enforce this. (In the common
  900. * case (a new boot), we don't know what the previous lease time was
  901. * anyway.)
  902. */
  903. static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
  904. {
  905. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  906. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
  907. }
  908. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
  909. struct nfsd_net *nn)
  910. {
  911. char *mesg = buf;
  912. char *recdir;
  913. int len, status;
  914. if (size > 0) {
  915. if (nn->nfsd_serv)
  916. return -EBUSY;
  917. if (size > PATH_MAX || buf[size-1] != '\n')
  918. return -EINVAL;
  919. buf[size-1] = 0;
  920. recdir = mesg;
  921. len = qword_get(&mesg, recdir, size);
  922. if (len <= 0)
  923. return -EINVAL;
  924. status = nfs4_reset_recoverydir(recdir);
  925. if (status)
  926. return status;
  927. }
  928. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  929. nfs4_recoverydir());
  930. }
  931. /**
  932. * write_recoverydir - Set or report the pathname of the recovery directory
  933. *
  934. * Input:
  935. * buf: ignored
  936. * size: zero
  937. *
  938. * OR
  939. *
  940. * Input:
  941. * buf: C string containing the pathname
  942. * of the directory on a local file
  943. * system containing permanent NFSv4
  944. * recovery data
  945. * size: non-zero length of C string in @buf
  946. * Output:
  947. * On success: passed-in buffer filled with '\n'-terminated C string
  948. * containing the current recovery pathname setting;
  949. * return code is the size in bytes of the string
  950. * On error: return code is zero or a negative errno value
  951. */
  952. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  953. {
  954. ssize_t rv;
  955. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  956. mutex_lock(&nfsd_mutex);
  957. rv = __write_recoverydir(file, buf, size, nn);
  958. mutex_unlock(&nfsd_mutex);
  959. return rv;
  960. }
  961. /**
  962. * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
  963. *
  964. * Input:
  965. * buf: ignored
  966. * size: zero
  967. * OR
  968. *
  969. * Input:
  970. * buf: any value
  971. * size: non-zero length of C string in @buf
  972. * Output:
  973. * passed-in buffer filled with "Y" or "N" with a newline
  974. * and NULL-terminated C string. This indicates whether
  975. * the grace period has ended in the current net
  976. * namespace. Return code is the size in bytes of the
  977. * string. Writing a string that starts with 'Y', 'y', or
  978. * '1' to the file will end the grace period for nfsd's v4
  979. * lock manager.
  980. */
  981. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
  982. {
  983. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  984. if (size > 0) {
  985. switch(buf[0]) {
  986. case 'Y':
  987. case 'y':
  988. case '1':
  989. if (!nn->nfsd_serv)
  990. return -EBUSY;
  991. nfsd4_end_grace(nn);
  992. break;
  993. default:
  994. return -EINVAL;
  995. }
  996. }
  997. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
  998. nn->grace_ended ? 'Y' : 'N');
  999. }
  1000. #endif
  1001. /*----------------------------------------------------------------------------*/
  1002. /*
  1003. * populating the filesystem.
  1004. */
  1005. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  1006. {
  1007. static struct tree_descr nfsd_files[] = {
  1008. [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
  1009. [NFSD_Export_features] = {"export_features",
  1010. &export_features_operations, S_IRUGO},
  1011. [NFSD_FO_UnlockIP] = {"unlock_ip",
  1012. &transaction_ops, S_IWUSR|S_IRUSR},
  1013. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  1014. &transaction_ops, S_IWUSR|S_IRUSR},
  1015. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  1016. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1017. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1018. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  1019. [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
  1020. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  1021. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  1022. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  1023. [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
  1024. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  1025. [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
  1026. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  1027. #ifdef CONFIG_NFSD_V4
  1028. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1029. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1030. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  1031. [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
  1032. #endif
  1033. /* last one */ {""}
  1034. };
  1035. get_net(sb->s_fs_info);
  1036. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  1037. }
  1038. static struct dentry *nfsd_mount(struct file_system_type *fs_type,
  1039. int flags, const char *dev_name, void *data)
  1040. {
  1041. struct net *net = current->nsproxy->net_ns;
  1042. return mount_ns(fs_type, flags, data, net, net->user_ns, nfsd_fill_super);
  1043. }
  1044. static void nfsd_umount(struct super_block *sb)
  1045. {
  1046. struct net *net = sb->s_fs_info;
  1047. kill_litter_super(sb);
  1048. put_net(net);
  1049. }
  1050. static struct file_system_type nfsd_fs_type = {
  1051. .owner = THIS_MODULE,
  1052. .name = "nfsd",
  1053. .mount = nfsd_mount,
  1054. .kill_sb = nfsd_umount,
  1055. };
  1056. MODULE_ALIAS_FS("nfsd");
  1057. #ifdef CONFIG_PROC_FS
  1058. static int create_proc_exports_entry(void)
  1059. {
  1060. struct proc_dir_entry *entry;
  1061. entry = proc_mkdir("fs/nfs", NULL);
  1062. if (!entry)
  1063. return -ENOMEM;
  1064. entry = proc_create("exports", 0, entry,
  1065. &exports_proc_operations);
  1066. if (!entry) {
  1067. remove_proc_entry("fs/nfs", NULL);
  1068. return -ENOMEM;
  1069. }
  1070. return 0;
  1071. }
  1072. #else /* CONFIG_PROC_FS */
  1073. static int create_proc_exports_entry(void)
  1074. {
  1075. return 0;
  1076. }
  1077. #endif
  1078. int nfsd_net_id;
  1079. static __net_init int nfsd_init_net(struct net *net)
  1080. {
  1081. int retval;
  1082. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1083. retval = nfsd_export_init(net);
  1084. if (retval)
  1085. goto out_export_error;
  1086. retval = nfsd_idmap_init(net);
  1087. if (retval)
  1088. goto out_idmap_error;
  1089. nn->nfsd4_lease = 90; /* default lease time */
  1090. nn->nfsd4_grace = 90;
  1091. nn->clverifier_counter = prandom_u32();
  1092. nn->clientid_counter = prandom_u32();
  1093. return 0;
  1094. out_idmap_error:
  1095. nfsd_export_shutdown(net);
  1096. out_export_error:
  1097. return retval;
  1098. }
  1099. static __net_exit void nfsd_exit_net(struct net *net)
  1100. {
  1101. nfsd_idmap_shutdown(net);
  1102. nfsd_export_shutdown(net);
  1103. }
  1104. static struct pernet_operations nfsd_net_ops = {
  1105. .init = nfsd_init_net,
  1106. .exit = nfsd_exit_net,
  1107. .id = &nfsd_net_id,
  1108. .size = sizeof(struct nfsd_net),
  1109. };
  1110. static int __init init_nfsd(void)
  1111. {
  1112. int retval;
  1113. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 [email protected]).\n");
  1114. retval = register_pernet_subsys(&nfsd_net_ops);
  1115. if (retval < 0)
  1116. return retval;
  1117. retval = register_cld_notifier();
  1118. if (retval)
  1119. goto out_unregister_pernet;
  1120. retval = nfsd4_init_slabs();
  1121. if (retval)
  1122. goto out_unregister_notifier;
  1123. retval = nfsd4_init_pnfs();
  1124. if (retval)
  1125. goto out_free_slabs;
  1126. retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
  1127. if (retval)
  1128. goto out_exit_pnfs;
  1129. nfsd_stat_init(); /* Statistics */
  1130. retval = nfsd_reply_cache_init();
  1131. if (retval)
  1132. goto out_free_stat;
  1133. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1134. retval = create_proc_exports_entry();
  1135. if (retval)
  1136. goto out_free_lockd;
  1137. retval = register_filesystem(&nfsd_fs_type);
  1138. if (retval)
  1139. goto out_free_all;
  1140. return 0;
  1141. out_free_all:
  1142. remove_proc_entry("fs/nfs/exports", NULL);
  1143. remove_proc_entry("fs/nfs", NULL);
  1144. out_free_lockd:
  1145. nfsd_lockd_shutdown();
  1146. nfsd_reply_cache_shutdown();
  1147. out_free_stat:
  1148. nfsd_stat_shutdown();
  1149. nfsd_fault_inject_cleanup();
  1150. out_exit_pnfs:
  1151. nfsd4_exit_pnfs();
  1152. out_free_slabs:
  1153. nfsd4_free_slabs();
  1154. out_unregister_notifier:
  1155. unregister_cld_notifier();
  1156. out_unregister_pernet:
  1157. unregister_pernet_subsys(&nfsd_net_ops);
  1158. return retval;
  1159. }
  1160. static void __exit exit_nfsd(void)
  1161. {
  1162. nfsd_reply_cache_shutdown();
  1163. remove_proc_entry("fs/nfs/exports", NULL);
  1164. remove_proc_entry("fs/nfs", NULL);
  1165. nfsd_stat_shutdown();
  1166. nfsd_lockd_shutdown();
  1167. nfsd4_free_slabs();
  1168. nfsd4_exit_pnfs();
  1169. nfsd_fault_inject_cleanup();
  1170. unregister_filesystem(&nfsd_fs_type);
  1171. unregister_cld_notifier();
  1172. unregister_pernet_subsys(&nfsd_net_ops);
  1173. }
  1174. MODULE_AUTHOR("Olaf Kirch <[email protected]>");
  1175. MODULE_LICENSE("GPL");
  1176. module_init(init_nfsd)
  1177. module_exit(exit_nfsd)