nfsctl.c 33 KB

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