nfsctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * linux/fs/nfsd/nfsctl.c
  3. *
  4. * Syscall interface to knfsd.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/linkage.h>
  10. #include <linux/time.h>
  11. #include <linux/errno.h>
  12. #include <linux/fs.h>
  13. #include <linux/namei.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/net.h>
  16. #include <linux/in.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/unistd.h>
  19. #include <linux/slab.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/init.h>
  24. #include <linux/inet.h>
  25. #include <linux/string.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/ctype.h>
  28. #include <linux/nfs.h>
  29. #include <linux/nfsd_idmap.h>
  30. #include <linux/lockd/bind.h>
  31. #include <linux/sunrpc/svc.h>
  32. #include <linux/sunrpc/svcsock.h>
  33. #include <linux/nfsd/nfsd.h>
  34. #include <linux/nfsd/cache.h>
  35. #include <linux/nfsd/xdr.h>
  36. #include <linux/nfsd/syscall.h>
  37. #include <linux/lockd/lockd.h>
  38. #include <asm/uaccess.h>
  39. #include <net/ipv6.h>
  40. /*
  41. * We have a single directory with 9 nodes in it.
  42. */
  43. enum {
  44. NFSD_Root = 1,
  45. NFSD_Svc,
  46. NFSD_Add,
  47. NFSD_Del,
  48. NFSD_Export,
  49. NFSD_Unexport,
  50. NFSD_Getfd,
  51. NFSD_Getfs,
  52. NFSD_List,
  53. NFSD_Fh,
  54. NFSD_FO_UnlockIP,
  55. NFSD_FO_UnlockFS,
  56. NFSD_Threads,
  57. NFSD_Pool_Threads,
  58. NFSD_Versions,
  59. NFSD_Ports,
  60. NFSD_MaxBlkSize,
  61. /*
  62. * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
  63. * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
  64. */
  65. #ifdef CONFIG_NFSD_V4
  66. NFSD_Leasetime,
  67. NFSD_RecoveryDir,
  68. #endif
  69. };
  70. /*
  71. * write() for these nodes.
  72. */
  73. static ssize_t write_svc(struct file *file, char *buf, size_t size);
  74. static ssize_t write_add(struct file *file, char *buf, size_t size);
  75. static ssize_t write_del(struct file *file, char *buf, size_t size);
  76. static ssize_t write_export(struct file *file, char *buf, size_t size);
  77. static ssize_t write_unexport(struct file *file, char *buf, size_t size);
  78. static ssize_t write_getfd(struct file *file, char *buf, size_t size);
  79. static ssize_t write_getfs(struct file *file, char *buf, size_t size);
  80. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  81. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  82. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  83. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  84. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  85. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  86. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  87. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  88. #ifdef CONFIG_NFSD_V4
  89. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  90. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  91. #endif
  92. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  93. [NFSD_Svc] = write_svc,
  94. [NFSD_Add] = write_add,
  95. [NFSD_Del] = write_del,
  96. [NFSD_Export] = write_export,
  97. [NFSD_Unexport] = write_unexport,
  98. [NFSD_Getfd] = write_getfd,
  99. [NFSD_Getfs] = write_getfs,
  100. [NFSD_Fh] = write_filehandle,
  101. [NFSD_FO_UnlockIP] = write_unlock_ip,
  102. [NFSD_FO_UnlockFS] = write_unlock_fs,
  103. [NFSD_Threads] = write_threads,
  104. [NFSD_Pool_Threads] = write_pool_threads,
  105. [NFSD_Versions] = write_versions,
  106. [NFSD_Ports] = write_ports,
  107. [NFSD_MaxBlkSize] = write_maxblksize,
  108. #ifdef CONFIG_NFSD_V4
  109. [NFSD_Leasetime] = write_leasetime,
  110. [NFSD_RecoveryDir] = write_recoverydir,
  111. #endif
  112. };
  113. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  114. {
  115. ino_t ino = file->f_path.dentry->d_inode->i_ino;
  116. char *data;
  117. ssize_t rv;
  118. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  119. return -EINVAL;
  120. data = simple_transaction_get(file, buf, size);
  121. if (IS_ERR(data))
  122. return PTR_ERR(data);
  123. rv = write_op[ino](file, data, size);
  124. if (rv >= 0) {
  125. simple_transaction_set(file, rv);
  126. rv = size;
  127. }
  128. return rv;
  129. }
  130. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  131. {
  132. if (! file->private_data) {
  133. /* An attempt to read a transaction file without writing
  134. * causes a 0-byte write so that the file can return
  135. * state information
  136. */
  137. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  138. if (rv < 0)
  139. return rv;
  140. }
  141. return simple_transaction_read(file, buf, size, pos);
  142. }
  143. static const struct file_operations transaction_ops = {
  144. .write = nfsctl_transaction_write,
  145. .read = nfsctl_transaction_read,
  146. .release = simple_transaction_release,
  147. };
  148. static int exports_open(struct inode *inode, struct file *file)
  149. {
  150. return seq_open(file, &nfs_exports_op);
  151. }
  152. static const struct file_operations exports_operations = {
  153. .open = exports_open,
  154. .read = seq_read,
  155. .llseek = seq_lseek,
  156. .release = seq_release,
  157. .owner = THIS_MODULE,
  158. };
  159. /*----------------------------------------------------------------------------*/
  160. /*
  161. * payload - write methods
  162. * If the method has a response, the response should be put in buf,
  163. * and the length returned. Otherwise return 0 or and -error.
  164. */
  165. static ssize_t write_svc(struct file *file, char *buf, size_t size)
  166. {
  167. struct nfsctl_svc *data;
  168. if (size < sizeof(*data))
  169. return -EINVAL;
  170. data = (struct nfsctl_svc*) buf;
  171. return nfsd_svc(data->svc_port, data->svc_nthreads);
  172. }
  173. static ssize_t write_add(struct file *file, char *buf, size_t size)
  174. {
  175. struct nfsctl_client *data;
  176. if (size < sizeof(*data))
  177. return -EINVAL;
  178. data = (struct nfsctl_client *)buf;
  179. return exp_addclient(data);
  180. }
  181. static ssize_t write_del(struct file *file, char *buf, size_t size)
  182. {
  183. struct nfsctl_client *data;
  184. if (size < sizeof(*data))
  185. return -EINVAL;
  186. data = (struct nfsctl_client *)buf;
  187. return exp_delclient(data);
  188. }
  189. static ssize_t write_export(struct file *file, char *buf, size_t size)
  190. {
  191. struct nfsctl_export *data;
  192. if (size < sizeof(*data))
  193. return -EINVAL;
  194. data = (struct nfsctl_export*)buf;
  195. return exp_export(data);
  196. }
  197. static ssize_t write_unexport(struct file *file, char *buf, size_t size)
  198. {
  199. struct nfsctl_export *data;
  200. if (size < sizeof(*data))
  201. return -EINVAL;
  202. data = (struct nfsctl_export*)buf;
  203. return exp_unexport(data);
  204. }
  205. static ssize_t write_getfs(struct file *file, char *buf, size_t size)
  206. {
  207. struct nfsctl_fsparm *data;
  208. struct sockaddr_in *sin;
  209. struct auth_domain *clp;
  210. int err = 0;
  211. struct knfsd_fh *res;
  212. struct in6_addr in6;
  213. if (size < sizeof(*data))
  214. return -EINVAL;
  215. data = (struct nfsctl_fsparm*)buf;
  216. err = -EPROTONOSUPPORT;
  217. if (data->gd_addr.sa_family != AF_INET)
  218. goto out;
  219. sin = (struct sockaddr_in *)&data->gd_addr;
  220. if (data->gd_maxlen > NFS3_FHSIZE)
  221. data->gd_maxlen = NFS3_FHSIZE;
  222. res = (struct knfsd_fh*)buf;
  223. exp_readlock();
  224. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
  225. clp = auth_unix_lookup(&in6);
  226. if (!clp)
  227. err = -EPERM;
  228. else {
  229. err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
  230. auth_domain_put(clp);
  231. }
  232. exp_readunlock();
  233. if (err == 0)
  234. err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
  235. out:
  236. return err;
  237. }
  238. static ssize_t write_getfd(struct file *file, char *buf, size_t size)
  239. {
  240. struct nfsctl_fdparm *data;
  241. struct sockaddr_in *sin;
  242. struct auth_domain *clp;
  243. int err = 0;
  244. struct knfsd_fh fh;
  245. char *res;
  246. struct in6_addr in6;
  247. if (size < sizeof(*data))
  248. return -EINVAL;
  249. data = (struct nfsctl_fdparm*)buf;
  250. err = -EPROTONOSUPPORT;
  251. if (data->gd_addr.sa_family != AF_INET)
  252. goto out;
  253. err = -EINVAL;
  254. if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
  255. goto out;
  256. res = buf;
  257. sin = (struct sockaddr_in *)&data->gd_addr;
  258. exp_readlock();
  259. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
  260. clp = auth_unix_lookup(&in6);
  261. if (!clp)
  262. err = -EPERM;
  263. else {
  264. err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
  265. auth_domain_put(clp);
  266. }
  267. exp_readunlock();
  268. if (err == 0) {
  269. memset(res,0, NFS_FHSIZE);
  270. memcpy(res, &fh.fh_base, fh.fh_size);
  271. err = NFS_FHSIZE;
  272. }
  273. out:
  274. return err;
  275. }
  276. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  277. {
  278. struct sockaddr_in sin = {
  279. .sin_family = AF_INET,
  280. };
  281. int b1, b2, b3, b4;
  282. char c;
  283. char *fo_path;
  284. /* sanity check */
  285. if (size == 0)
  286. return -EINVAL;
  287. if (buf[size-1] != '\n')
  288. return -EINVAL;
  289. fo_path = buf;
  290. if (qword_get(&buf, fo_path, size) < 0)
  291. return -EINVAL;
  292. /* get ipv4 address */
  293. if (sscanf(fo_path, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
  294. return -EINVAL;
  295. if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255)
  296. return -EINVAL;
  297. sin.sin_addr.s_addr = htonl((b1 << 24) | (b2 << 16) | (b3 << 8) | b4);
  298. return nlmsvc_unlock_all_by_ip((struct sockaddr *)&sin);
  299. }
  300. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  301. {
  302. struct path path;
  303. char *fo_path;
  304. int error;
  305. /* sanity check */
  306. if (size == 0)
  307. return -EINVAL;
  308. if (buf[size-1] != '\n')
  309. return -EINVAL;
  310. fo_path = buf;
  311. if (qword_get(&buf, fo_path, size) < 0)
  312. return -EINVAL;
  313. error = kern_path(fo_path, 0, &path);
  314. if (error)
  315. return error;
  316. error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
  317. path_put(&path);
  318. return error;
  319. }
  320. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  321. {
  322. /* request is:
  323. * domain path maxsize
  324. * response is
  325. * filehandle
  326. *
  327. * qword quoting is used, so filehandle will be \x....
  328. */
  329. char *dname, *path;
  330. int uninitialized_var(maxsize);
  331. char *mesg = buf;
  332. int len;
  333. struct auth_domain *dom;
  334. struct knfsd_fh fh;
  335. if (size == 0)
  336. return -EINVAL;
  337. if (buf[size-1] != '\n')
  338. return -EINVAL;
  339. buf[size-1] = 0;
  340. dname = mesg;
  341. len = qword_get(&mesg, dname, size);
  342. if (len <= 0)
  343. return -EINVAL;
  344. path = dname+len+1;
  345. len = qword_get(&mesg, path, size);
  346. if (len <= 0)
  347. return -EINVAL;
  348. len = get_int(&mesg, &maxsize);
  349. if (len)
  350. return len;
  351. if (maxsize < NFS_FHSIZE)
  352. return -EINVAL;
  353. if (maxsize > NFS3_FHSIZE)
  354. maxsize = NFS3_FHSIZE;
  355. if (qword_get(&mesg, mesg, size)>0)
  356. return -EINVAL;
  357. /* we have all the words, they are in buf.. */
  358. dom = unix_domain_find(dname);
  359. if (!dom)
  360. return -ENOMEM;
  361. len = exp_rootfh(dom, path, &fh, maxsize);
  362. auth_domain_put(dom);
  363. if (len)
  364. return len;
  365. mesg = buf;
  366. len = SIMPLE_TRANSACTION_LIMIT;
  367. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  368. mesg[-1] = '\n';
  369. return mesg - buf;
  370. }
  371. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  372. {
  373. /* if size > 0, look for a number of threads and call nfsd_svc
  374. * then write out number of threads as reply
  375. */
  376. char *mesg = buf;
  377. int rv;
  378. if (size > 0) {
  379. int newthreads;
  380. rv = get_int(&mesg, &newthreads);
  381. if (rv)
  382. return rv;
  383. if (newthreads < 0)
  384. return -EINVAL;
  385. rv = nfsd_svc(NFS_PORT, newthreads);
  386. if (rv)
  387. return rv;
  388. }
  389. sprintf(buf, "%d\n", nfsd_nrthreads());
  390. return strlen(buf);
  391. }
  392. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  393. {
  394. /* if size > 0, look for an array of number of threads per node
  395. * and apply them then write out number of threads per node as reply
  396. */
  397. char *mesg = buf;
  398. int i;
  399. int rv;
  400. int len;
  401. int npools;
  402. int *nthreads;
  403. mutex_lock(&nfsd_mutex);
  404. npools = nfsd_nrpools();
  405. if (npools == 0) {
  406. /*
  407. * NFS is shut down. The admin can start it by
  408. * writing to the threads file but NOT the pool_threads
  409. * file, sorry. Report zero threads.
  410. */
  411. mutex_unlock(&nfsd_mutex);
  412. strcpy(buf, "0\n");
  413. return strlen(buf);
  414. }
  415. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  416. rv = -ENOMEM;
  417. if (nthreads == NULL)
  418. goto out_free;
  419. if (size > 0) {
  420. for (i = 0; i < npools; i++) {
  421. rv = get_int(&mesg, &nthreads[i]);
  422. if (rv == -ENOENT)
  423. break; /* fewer numbers than pools */
  424. if (rv)
  425. goto out_free; /* syntax error */
  426. rv = -EINVAL;
  427. if (nthreads[i] < 0)
  428. goto out_free;
  429. }
  430. rv = nfsd_set_nrthreads(i, nthreads);
  431. if (rv)
  432. goto out_free;
  433. }
  434. rv = nfsd_get_nrthreads(npools, nthreads);
  435. if (rv)
  436. goto out_free;
  437. mesg = buf;
  438. size = SIMPLE_TRANSACTION_LIMIT;
  439. for (i = 0; i < npools && size > 0; i++) {
  440. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  441. len = strlen(mesg);
  442. size -= len;
  443. mesg += len;
  444. }
  445. mutex_unlock(&nfsd_mutex);
  446. return (mesg-buf);
  447. out_free:
  448. kfree(nthreads);
  449. mutex_unlock(&nfsd_mutex);
  450. return rv;
  451. }
  452. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  453. {
  454. /*
  455. * Format:
  456. * [-/+]vers [-/+]vers ...
  457. */
  458. char *mesg = buf;
  459. char *vers, sign;
  460. int len, num;
  461. ssize_t tlen = 0;
  462. char *sep;
  463. if (size>0) {
  464. if (nfsd_serv)
  465. /* Cannot change versions without updating
  466. * nfsd_serv->sv_xdrsize, and reallocing
  467. * rq_argp and rq_resp
  468. */
  469. return -EBUSY;
  470. if (buf[size-1] != '\n')
  471. return -EINVAL;
  472. buf[size-1] = 0;
  473. vers = mesg;
  474. len = qword_get(&mesg, vers, size);
  475. if (len <= 0) return -EINVAL;
  476. do {
  477. sign = *vers;
  478. if (sign == '+' || sign == '-')
  479. num = simple_strtol((vers+1), NULL, 0);
  480. else
  481. num = simple_strtol(vers, NULL, 0);
  482. switch(num) {
  483. case 2:
  484. case 3:
  485. case 4:
  486. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. vers += len + 1;
  492. tlen += len;
  493. } while ((len = qword_get(&mesg, vers, size)) > 0);
  494. /* If all get turned off, turn them back on, as
  495. * having no versions is BAD
  496. */
  497. nfsd_reset_versions();
  498. }
  499. /* Now write current state into reply buffer */
  500. len = 0;
  501. sep = "";
  502. for (num=2 ; num <= 4 ; num++)
  503. if (nfsd_vers(num, NFSD_AVAIL)) {
  504. len += sprintf(buf+len, "%s%c%d", sep,
  505. nfsd_vers(num, NFSD_TEST)?'+':'-',
  506. num);
  507. sep = " ";
  508. }
  509. len += sprintf(buf+len, "\n");
  510. return len;
  511. }
  512. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  513. {
  514. ssize_t rv;
  515. mutex_lock(&nfsd_mutex);
  516. rv = __write_versions(file, buf, size);
  517. mutex_unlock(&nfsd_mutex);
  518. return rv;
  519. }
  520. static ssize_t __write_ports(struct file *file, char *buf, size_t size)
  521. {
  522. if (size == 0) {
  523. int len = 0;
  524. if (nfsd_serv)
  525. len = svc_xprt_names(nfsd_serv, buf, 0);
  526. return len;
  527. }
  528. /* Either a single 'fd' number is written, in which
  529. * case it must be for a socket of a supported family/protocol,
  530. * and we use it as an nfsd socket, or
  531. * A '-' followed by the 'name' of a socket in which case
  532. * we close the socket.
  533. */
  534. if (isdigit(buf[0])) {
  535. char *mesg = buf;
  536. int fd;
  537. int err;
  538. err = get_int(&mesg, &fd);
  539. if (err)
  540. return -EINVAL;
  541. if (fd < 0)
  542. return -EINVAL;
  543. err = nfsd_create_serv();
  544. if (!err) {
  545. err = svc_addsock(nfsd_serv, fd, buf);
  546. if (err >= 0) {
  547. err = lockd_up();
  548. if (err < 0)
  549. svc_sock_names(buf+strlen(buf)+1, nfsd_serv, buf);
  550. }
  551. /* Decrease the count, but don't shutdown the
  552. * the service
  553. */
  554. nfsd_serv->sv_nrthreads--;
  555. }
  556. return err < 0 ? err : 0;
  557. }
  558. if (buf[0] == '-' && isdigit(buf[1])) {
  559. char *toclose = kstrdup(buf+1, GFP_KERNEL);
  560. int len = 0;
  561. if (!toclose)
  562. return -ENOMEM;
  563. if (nfsd_serv)
  564. len = svc_sock_names(buf, nfsd_serv, toclose);
  565. if (len >= 0)
  566. lockd_down();
  567. kfree(toclose);
  568. return len;
  569. }
  570. /*
  571. * Add a transport listener by writing it's transport name
  572. */
  573. if (isalpha(buf[0])) {
  574. int err;
  575. char transport[16];
  576. int port;
  577. if (sscanf(buf, "%15s %4d", transport, &port) == 2) {
  578. err = nfsd_create_serv();
  579. if (!err) {
  580. err = svc_create_xprt(nfsd_serv,
  581. transport, port,
  582. SVC_SOCK_ANONYMOUS);
  583. if (err == -ENOENT)
  584. /* Give a reasonable perror msg for
  585. * bad transport string */
  586. err = -EPROTONOSUPPORT;
  587. }
  588. return err < 0 ? err : 0;
  589. }
  590. }
  591. /*
  592. * Remove a transport by writing it's transport name and port number
  593. */
  594. if (buf[0] == '-' && isalpha(buf[1])) {
  595. struct svc_xprt *xprt;
  596. int err = -EINVAL;
  597. char transport[16];
  598. int port;
  599. if (sscanf(&buf[1], "%15s %4d", transport, &port) == 2) {
  600. if (port == 0)
  601. return -EINVAL;
  602. if (nfsd_serv) {
  603. xprt = svc_find_xprt(nfsd_serv, transport,
  604. AF_UNSPEC, port);
  605. if (xprt) {
  606. svc_close_xprt(xprt);
  607. svc_xprt_put(xprt);
  608. err = 0;
  609. } else
  610. err = -ENOTCONN;
  611. }
  612. return err < 0 ? err : 0;
  613. }
  614. }
  615. return -EINVAL;
  616. }
  617. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  618. {
  619. ssize_t rv;
  620. mutex_lock(&nfsd_mutex);
  621. rv = __write_ports(file, buf, size);
  622. mutex_unlock(&nfsd_mutex);
  623. return rv;
  624. }
  625. int nfsd_max_blksize;
  626. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  627. {
  628. char *mesg = buf;
  629. if (size > 0) {
  630. int bsize;
  631. int rv = get_int(&mesg, &bsize);
  632. if (rv)
  633. return rv;
  634. /* force bsize into allowed range and
  635. * required alignment.
  636. */
  637. if (bsize < 1024)
  638. bsize = 1024;
  639. if (bsize > NFSSVC_MAXBLKSIZE)
  640. bsize = NFSSVC_MAXBLKSIZE;
  641. bsize &= ~(1024-1);
  642. mutex_lock(&nfsd_mutex);
  643. if (nfsd_serv && nfsd_serv->sv_nrthreads) {
  644. mutex_unlock(&nfsd_mutex);
  645. return -EBUSY;
  646. }
  647. nfsd_max_blksize = bsize;
  648. mutex_unlock(&nfsd_mutex);
  649. }
  650. return sprintf(buf, "%d\n", nfsd_max_blksize);
  651. }
  652. #ifdef CONFIG_NFSD_V4
  653. extern time_t nfs4_leasetime(void);
  654. static ssize_t __write_leasetime(struct file *file, char *buf, size_t size)
  655. {
  656. /* if size > 10 seconds, call
  657. * nfs4_reset_lease() then write out the new lease (seconds) as reply
  658. */
  659. char *mesg = buf;
  660. int rv, lease;
  661. if (size > 0) {
  662. if (nfsd_serv)
  663. return -EBUSY;
  664. rv = get_int(&mesg, &lease);
  665. if (rv)
  666. return rv;
  667. if (lease < 10 || lease > 3600)
  668. return -EINVAL;
  669. nfs4_reset_lease(lease);
  670. }
  671. sprintf(buf, "%ld\n", nfs4_lease_time());
  672. return strlen(buf);
  673. }
  674. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  675. {
  676. ssize_t rv;
  677. mutex_lock(&nfsd_mutex);
  678. rv = __write_leasetime(file, buf, size);
  679. mutex_unlock(&nfsd_mutex);
  680. return rv;
  681. }
  682. extern char *nfs4_recoverydir(void);
  683. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
  684. {
  685. char *mesg = buf;
  686. char *recdir;
  687. int len, status;
  688. if (size > 0) {
  689. if (nfsd_serv)
  690. return -EBUSY;
  691. if (size > PATH_MAX || buf[size-1] != '\n')
  692. return -EINVAL;
  693. buf[size-1] = 0;
  694. recdir = mesg;
  695. len = qword_get(&mesg, recdir, size);
  696. if (len <= 0)
  697. return -EINVAL;
  698. status = nfs4_reset_recoverydir(recdir);
  699. }
  700. sprintf(buf, "%s\n", nfs4_recoverydir());
  701. return strlen(buf);
  702. }
  703. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  704. {
  705. ssize_t rv;
  706. mutex_lock(&nfsd_mutex);
  707. rv = __write_recoverydir(file, buf, size);
  708. mutex_unlock(&nfsd_mutex);
  709. return rv;
  710. }
  711. #endif
  712. /*----------------------------------------------------------------------------*/
  713. /*
  714. * populating the filesystem.
  715. */
  716. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  717. {
  718. static struct tree_descr nfsd_files[] = {
  719. [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
  720. [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
  721. [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
  722. [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
  723. [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
  724. [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
  725. [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
  726. [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
  727. [NFSD_FO_UnlockIP] = {"unlock_ip",
  728. &transaction_ops, S_IWUSR|S_IRUSR},
  729. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  730. &transaction_ops, S_IWUSR|S_IRUSR},
  731. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  732. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  733. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  734. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  735. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  736. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  737. #ifdef CONFIG_NFSD_V4
  738. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  739. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  740. #endif
  741. /* last one */ {""}
  742. };
  743. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  744. }
  745. static int nfsd_get_sb(struct file_system_type *fs_type,
  746. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  747. {
  748. return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
  749. }
  750. static struct file_system_type nfsd_fs_type = {
  751. .owner = THIS_MODULE,
  752. .name = "nfsd",
  753. .get_sb = nfsd_get_sb,
  754. .kill_sb = kill_litter_super,
  755. };
  756. #ifdef CONFIG_PROC_FS
  757. static int create_proc_exports_entry(void)
  758. {
  759. struct proc_dir_entry *entry;
  760. entry = proc_mkdir("fs/nfs", NULL);
  761. if (!entry)
  762. return -ENOMEM;
  763. entry = proc_create("exports", 0, entry, &exports_operations);
  764. if (!entry)
  765. return -ENOMEM;
  766. return 0;
  767. }
  768. #else /* CONFIG_PROC_FS */
  769. static int create_proc_exports_entry(void)
  770. {
  771. return 0;
  772. }
  773. #endif
  774. static int __init init_nfsd(void)
  775. {
  776. int retval;
  777. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  778. retval = nfs4_state_init(); /* nfs4 locking state */
  779. if (retval)
  780. return retval;
  781. nfsd_stat_init(); /* Statistics */
  782. retval = nfsd_reply_cache_init();
  783. if (retval)
  784. goto out_free_stat;
  785. retval = nfsd_export_init();
  786. if (retval)
  787. goto out_free_cache;
  788. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  789. retval = nfsd_idmap_init();
  790. if (retval)
  791. goto out_free_lockd;
  792. retval = create_proc_exports_entry();
  793. if (retval)
  794. goto out_free_idmap;
  795. retval = register_filesystem(&nfsd_fs_type);
  796. if (retval)
  797. goto out_free_all;
  798. return 0;
  799. out_free_all:
  800. remove_proc_entry("fs/nfs/exports", NULL);
  801. remove_proc_entry("fs/nfs", NULL);
  802. out_free_idmap:
  803. nfsd_idmap_shutdown();
  804. out_free_lockd:
  805. nfsd_lockd_shutdown();
  806. nfsd_export_shutdown();
  807. out_free_cache:
  808. nfsd_reply_cache_shutdown();
  809. out_free_stat:
  810. nfsd_stat_shutdown();
  811. nfsd4_free_slabs();
  812. return retval;
  813. }
  814. static void __exit exit_nfsd(void)
  815. {
  816. nfsd_export_shutdown();
  817. nfsd_reply_cache_shutdown();
  818. remove_proc_entry("fs/nfs/exports", NULL);
  819. remove_proc_entry("fs/nfs", NULL);
  820. nfsd_stat_shutdown();
  821. nfsd_lockd_shutdown();
  822. nfsd_idmap_shutdown();
  823. nfsd4_free_slabs();
  824. unregister_filesystem(&nfsd_fs_type);
  825. }
  826. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  827. MODULE_LICENSE("GPL");
  828. module_init(init_nfsd)
  829. module_exit(exit_nfsd)