nfsctl.c 33 KB

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