nfsctl.c 20 KB

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