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. };
  157. /*----------------------------------------------------------------------------*/
  158. /*
  159. * payload - write methods
  160. * If the method has a response, the response should be put in buf,
  161. * and the length returned. Otherwise return 0 or and -error.
  162. */
  163. static ssize_t write_svc(struct file *file, char *buf, size_t size)
  164. {
  165. struct nfsctl_svc *data;
  166. if (size < sizeof(*data))
  167. return -EINVAL;
  168. data = (struct nfsctl_svc*) buf;
  169. return nfsd_svc(data->svc_port, data->svc_nthreads);
  170. }
  171. static ssize_t write_add(struct file *file, char *buf, size_t size)
  172. {
  173. struct nfsctl_client *data;
  174. if (size < sizeof(*data))
  175. return -EINVAL;
  176. data = (struct nfsctl_client *)buf;
  177. return exp_addclient(data);
  178. }
  179. static ssize_t write_del(struct file *file, char *buf, size_t size)
  180. {
  181. struct nfsctl_client *data;
  182. if (size < sizeof(*data))
  183. return -EINVAL;
  184. data = (struct nfsctl_client *)buf;
  185. return exp_delclient(data);
  186. }
  187. static ssize_t write_export(struct file *file, char *buf, size_t size)
  188. {
  189. struct nfsctl_export *data;
  190. if (size < sizeof(*data))
  191. return -EINVAL;
  192. data = (struct nfsctl_export*)buf;
  193. return exp_export(data);
  194. }
  195. static ssize_t write_unexport(struct file *file, char *buf, size_t size)
  196. {
  197. struct nfsctl_export *data;
  198. if (size < sizeof(*data))
  199. return -EINVAL;
  200. data = (struct nfsctl_export*)buf;
  201. return exp_unexport(data);
  202. }
  203. static ssize_t write_getfs(struct file *file, char *buf, size_t size)
  204. {
  205. struct nfsctl_fsparm *data;
  206. struct sockaddr_in *sin;
  207. struct auth_domain *clp;
  208. int err = 0;
  209. struct knfsd_fh *res;
  210. struct in6_addr in6;
  211. if (size < sizeof(*data))
  212. return -EINVAL;
  213. data = (struct nfsctl_fsparm*)buf;
  214. err = -EPROTONOSUPPORT;
  215. if (data->gd_addr.sa_family != AF_INET)
  216. goto out;
  217. sin = (struct sockaddr_in *)&data->gd_addr;
  218. if (data->gd_maxlen > NFS3_FHSIZE)
  219. data->gd_maxlen = NFS3_FHSIZE;
  220. res = (struct knfsd_fh*)buf;
  221. exp_readlock();
  222. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
  223. clp = auth_unix_lookup(&in6);
  224. if (!clp)
  225. err = -EPERM;
  226. else {
  227. err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
  228. auth_domain_put(clp);
  229. }
  230. exp_readunlock();
  231. if (err == 0)
  232. err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
  233. out:
  234. return err;
  235. }
  236. static ssize_t write_getfd(struct file *file, char *buf, size_t size)
  237. {
  238. struct nfsctl_fdparm *data;
  239. struct sockaddr_in *sin;
  240. struct auth_domain *clp;
  241. int err = 0;
  242. struct knfsd_fh fh;
  243. char *res;
  244. struct in6_addr in6;
  245. if (size < sizeof(*data))
  246. return -EINVAL;
  247. data = (struct nfsctl_fdparm*)buf;
  248. err = -EPROTONOSUPPORT;
  249. if (data->gd_addr.sa_family != AF_INET)
  250. goto out;
  251. err = -EINVAL;
  252. if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
  253. goto out;
  254. res = buf;
  255. sin = (struct sockaddr_in *)&data->gd_addr;
  256. exp_readlock();
  257. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
  258. clp = auth_unix_lookup(&in6);
  259. if (!clp)
  260. err = -EPERM;
  261. else {
  262. err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
  263. auth_domain_put(clp);
  264. }
  265. exp_readunlock();
  266. if (err == 0) {
  267. memset(res,0, NFS_FHSIZE);
  268. memcpy(res, &fh.fh_base, fh.fh_size);
  269. err = NFS_FHSIZE;
  270. }
  271. out:
  272. return err;
  273. }
  274. static ssize_t failover_unlock_ip(struct file *file, char *buf, size_t size)
  275. {
  276. __be32 server_ip;
  277. char *fo_path, c;
  278. int b1, b2, b3, b4;
  279. /* sanity check */
  280. if (size == 0)
  281. return -EINVAL;
  282. if (buf[size-1] != '\n')
  283. return -EINVAL;
  284. fo_path = buf;
  285. if (qword_get(&buf, fo_path, size) < 0)
  286. return -EINVAL;
  287. /* get ipv4 address */
  288. if (sscanf(fo_path, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
  289. return -EINVAL;
  290. server_ip = htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
  291. return nlmsvc_unlock_all_by_ip(server_ip);
  292. }
  293. static ssize_t failover_unlock_fs(struct file *file, char *buf, size_t size)
  294. {
  295. struct nameidata nd;
  296. char *fo_path;
  297. int error;
  298. /* sanity check */
  299. if (size == 0)
  300. return -EINVAL;
  301. if (buf[size-1] != '\n')
  302. return -EINVAL;
  303. fo_path = buf;
  304. if (qword_get(&buf, fo_path, size) < 0)
  305. return -EINVAL;
  306. error = path_lookup(fo_path, 0, &nd);
  307. if (error)
  308. return error;
  309. error = nlmsvc_unlock_all_by_sb(nd.path.mnt->mnt_sb);
  310. path_put(&nd.path);
  311. return error;
  312. }
  313. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  314. {
  315. /* request is:
  316. * domain path maxsize
  317. * response is
  318. * filehandle
  319. *
  320. * qword quoting is used, so filehandle will be \x....
  321. */
  322. char *dname, *path;
  323. int uninitialized_var(maxsize);
  324. char *mesg = buf;
  325. int len;
  326. struct auth_domain *dom;
  327. struct knfsd_fh fh;
  328. if (size == 0)
  329. return -EINVAL;
  330. if (buf[size-1] != '\n')
  331. return -EINVAL;
  332. buf[size-1] = 0;
  333. dname = mesg;
  334. len = qword_get(&mesg, dname, size);
  335. if (len <= 0) return -EINVAL;
  336. path = dname+len+1;
  337. len = qword_get(&mesg, path, size);
  338. if (len <= 0) return -EINVAL;
  339. len = get_int(&mesg, &maxsize);
  340. if (len)
  341. return len;
  342. if (maxsize < NFS_FHSIZE)
  343. return -EINVAL;
  344. if (maxsize > NFS3_FHSIZE)
  345. maxsize = NFS3_FHSIZE;
  346. if (qword_get(&mesg, mesg, size)>0)
  347. return -EINVAL;
  348. /* we have all the words, they are in buf.. */
  349. dom = unix_domain_find(dname);
  350. if (!dom)
  351. return -ENOMEM;
  352. len = exp_rootfh(dom, path, &fh, maxsize);
  353. auth_domain_put(dom);
  354. if (len)
  355. return len;
  356. mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
  357. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  358. mesg[-1] = '\n';
  359. return mesg - buf;
  360. }
  361. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  362. {
  363. /* if size > 0, look for a number of threads and call nfsd_svc
  364. * then write out number of threads as reply
  365. */
  366. char *mesg = buf;
  367. int rv;
  368. if (size > 0) {
  369. int newthreads;
  370. rv = get_int(&mesg, &newthreads);
  371. if (rv)
  372. return rv;
  373. if (newthreads <0)
  374. return -EINVAL;
  375. rv = nfsd_svc(2049, newthreads);
  376. if (rv)
  377. return rv;
  378. }
  379. sprintf(buf, "%d\n", nfsd_nrthreads());
  380. return strlen(buf);
  381. }
  382. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  383. {
  384. /* if size > 0, look for an array of number of threads per node
  385. * and apply them then write out number of threads per node as reply
  386. */
  387. char *mesg = buf;
  388. int i;
  389. int rv;
  390. int len;
  391. int npools = nfsd_nrpools();
  392. int *nthreads;
  393. if (npools == 0) {
  394. /*
  395. * NFS is shut down. The admin can start it by
  396. * writing to the threads file but NOT the pool_threads
  397. * file, sorry. Report zero threads.
  398. */
  399. strcpy(buf, "0\n");
  400. return strlen(buf);
  401. }
  402. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  403. if (nthreads == NULL)
  404. return -ENOMEM;
  405. if (size > 0) {
  406. for (i = 0; i < npools; i++) {
  407. rv = get_int(&mesg, &nthreads[i]);
  408. if (rv == -ENOENT)
  409. break; /* fewer numbers than pools */
  410. if (rv)
  411. goto out_free; /* syntax error */
  412. rv = -EINVAL;
  413. if (nthreads[i] < 0)
  414. goto out_free;
  415. }
  416. rv = nfsd_set_nrthreads(i, nthreads);
  417. if (rv)
  418. goto out_free;
  419. }
  420. rv = nfsd_get_nrthreads(npools, nthreads);
  421. if (rv)
  422. goto out_free;
  423. mesg = buf;
  424. size = SIMPLE_TRANSACTION_LIMIT;
  425. for (i = 0; i < npools && size > 0; i++) {
  426. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  427. len = strlen(mesg);
  428. size -= len;
  429. mesg += len;
  430. }
  431. return (mesg-buf);
  432. out_free:
  433. kfree(nthreads);
  434. return rv;
  435. }
  436. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  437. {
  438. /*
  439. * Format:
  440. * [-/+]vers [-/+]vers ...
  441. */
  442. char *mesg = buf;
  443. char *vers, sign;
  444. int len, num;
  445. ssize_t tlen = 0;
  446. char *sep;
  447. if (size>0) {
  448. if (nfsd_serv)
  449. /* Cannot change versions without updating
  450. * nfsd_serv->sv_xdrsize, and reallocing
  451. * rq_argp and rq_resp
  452. */
  453. return -EBUSY;
  454. if (buf[size-1] != '\n')
  455. return -EINVAL;
  456. buf[size-1] = 0;
  457. vers = mesg;
  458. len = qword_get(&mesg, vers, size);
  459. if (len <= 0) return -EINVAL;
  460. do {
  461. sign = *vers;
  462. if (sign == '+' || sign == '-')
  463. num = simple_strtol((vers+1), NULL, 0);
  464. else
  465. num = simple_strtol(vers, NULL, 0);
  466. switch(num) {
  467. case 2:
  468. case 3:
  469. case 4:
  470. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  471. break;
  472. default:
  473. return -EINVAL;
  474. }
  475. vers += len + 1;
  476. tlen += len;
  477. } while ((len = qword_get(&mesg, vers, size)) > 0);
  478. /* If all get turned off, turn them back on, as
  479. * having no versions is BAD
  480. */
  481. nfsd_reset_versions();
  482. }
  483. /* Now write current state into reply buffer */
  484. len = 0;
  485. sep = "";
  486. for (num=2 ; num <= 4 ; num++)
  487. if (nfsd_vers(num, NFSD_AVAIL)) {
  488. len += sprintf(buf+len, "%s%c%d", sep,
  489. nfsd_vers(num, NFSD_TEST)?'+':'-',
  490. num);
  491. sep = " ";
  492. }
  493. len += sprintf(buf+len, "\n");
  494. return len;
  495. }
  496. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  497. {
  498. if (size == 0) {
  499. int len = 0;
  500. lock_kernel();
  501. if (nfsd_serv)
  502. len = svc_xprt_names(nfsd_serv, buf, 0);
  503. unlock_kernel();
  504. return len;
  505. }
  506. /* Either a single 'fd' number is written, in which
  507. * case it must be for a socket of a supported family/protocol,
  508. * and we use it as an nfsd socket, or
  509. * A '-' followed by the 'name' of a socket in which case
  510. * we close the socket.
  511. */
  512. if (isdigit(buf[0])) {
  513. char *mesg = buf;
  514. int fd;
  515. int err;
  516. err = get_int(&mesg, &fd);
  517. if (err)
  518. return -EINVAL;
  519. if (fd < 0)
  520. return -EINVAL;
  521. err = nfsd_create_serv();
  522. if (!err) {
  523. int proto = 0;
  524. err = svc_addsock(nfsd_serv, fd, buf, &proto);
  525. if (err >= 0) {
  526. err = lockd_up(proto);
  527. if (err < 0)
  528. svc_sock_names(buf+strlen(buf)+1, nfsd_serv, buf);
  529. }
  530. /* Decrease the count, but don't shutdown the
  531. * the service
  532. */
  533. lock_kernel();
  534. nfsd_serv->sv_nrthreads--;
  535. unlock_kernel();
  536. }
  537. return err < 0 ? err : 0;
  538. }
  539. if (buf[0] == '-' && isdigit(buf[1])) {
  540. char *toclose = kstrdup(buf+1, GFP_KERNEL);
  541. int len = 0;
  542. if (!toclose)
  543. return -ENOMEM;
  544. lock_kernel();
  545. if (nfsd_serv)
  546. len = svc_sock_names(buf, nfsd_serv, toclose);
  547. unlock_kernel();
  548. if (len >= 0)
  549. lockd_down();
  550. kfree(toclose);
  551. return len;
  552. }
  553. /*
  554. * Add a transport listener by writing it's transport name
  555. */
  556. if (isalpha(buf[0])) {
  557. int err;
  558. char transport[16];
  559. int port;
  560. if (sscanf(buf, "%15s %4d", transport, &port) == 2) {
  561. err = nfsd_create_serv();
  562. if (!err) {
  563. err = svc_create_xprt(nfsd_serv,
  564. transport, port,
  565. SVC_SOCK_ANONYMOUS);
  566. if (err == -ENOENT)
  567. /* Give a reasonable perror msg for
  568. * bad transport string */
  569. err = -EPROTONOSUPPORT;
  570. }
  571. return err < 0 ? err : 0;
  572. }
  573. }
  574. /*
  575. * Remove a transport by writing it's transport name and port number
  576. */
  577. if (buf[0] == '-' && isalpha(buf[1])) {
  578. struct svc_xprt *xprt;
  579. int err = -EINVAL;
  580. char transport[16];
  581. int port;
  582. if (sscanf(&buf[1], "%15s %4d", transport, &port) == 2) {
  583. if (port == 0)
  584. return -EINVAL;
  585. lock_kernel();
  586. if (nfsd_serv) {
  587. xprt = svc_find_xprt(nfsd_serv, transport,
  588. AF_UNSPEC, port);
  589. if (xprt) {
  590. svc_close_xprt(xprt);
  591. svc_xprt_put(xprt);
  592. err = 0;
  593. } else
  594. err = -ENOTCONN;
  595. }
  596. unlock_kernel();
  597. return err < 0 ? err : 0;
  598. }
  599. }
  600. return -EINVAL;
  601. }
  602. int nfsd_max_blksize;
  603. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  604. {
  605. char *mesg = buf;
  606. if (size > 0) {
  607. int bsize;
  608. int rv = get_int(&mesg, &bsize);
  609. if (rv)
  610. return rv;
  611. /* force bsize into allowed range and
  612. * required alignment.
  613. */
  614. if (bsize < 1024)
  615. bsize = 1024;
  616. if (bsize > NFSSVC_MAXBLKSIZE)
  617. bsize = NFSSVC_MAXBLKSIZE;
  618. bsize &= ~(1024-1);
  619. lock_kernel();
  620. if (nfsd_serv && nfsd_serv->sv_nrthreads) {
  621. unlock_kernel();
  622. return -EBUSY;
  623. }
  624. nfsd_max_blksize = bsize;
  625. unlock_kernel();
  626. }
  627. return sprintf(buf, "%d\n", nfsd_max_blksize);
  628. }
  629. #ifdef CONFIG_NFSD_V4
  630. extern time_t nfs4_leasetime(void);
  631. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  632. {
  633. /* if size > 10 seconds, call
  634. * nfs4_reset_lease() then write out the new lease (seconds) as reply
  635. */
  636. char *mesg = buf;
  637. int rv;
  638. if (size > 0) {
  639. int lease;
  640. rv = get_int(&mesg, &lease);
  641. if (rv)
  642. return rv;
  643. if (lease < 10 || lease > 3600)
  644. return -EINVAL;
  645. nfs4_reset_lease(lease);
  646. }
  647. sprintf(buf, "%ld\n", nfs4_lease_time());
  648. return strlen(buf);
  649. }
  650. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  651. {
  652. char *mesg = buf;
  653. char *recdir;
  654. int len, status;
  655. if (size == 0 || size > PATH_MAX || buf[size-1] != '\n')
  656. return -EINVAL;
  657. buf[size-1] = 0;
  658. recdir = mesg;
  659. len = qword_get(&mesg, recdir, size);
  660. if (len <= 0)
  661. return -EINVAL;
  662. status = nfs4_reset_recoverydir(recdir);
  663. return strlen(buf);
  664. }
  665. #endif
  666. /*----------------------------------------------------------------------------*/
  667. /*
  668. * populating the filesystem.
  669. */
  670. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  671. {
  672. static struct tree_descr nfsd_files[] = {
  673. [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
  674. [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
  675. [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
  676. [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
  677. [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
  678. [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
  679. [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
  680. [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
  681. [NFSD_FO_UnlockIP] = {"unlock_ip",
  682. &transaction_ops, S_IWUSR|S_IRUSR},
  683. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  684. &transaction_ops, S_IWUSR|S_IRUSR},
  685. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  686. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  687. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  688. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  689. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  690. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  691. #ifdef CONFIG_NFSD_V4
  692. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  693. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  694. #endif
  695. /* last one */ {""}
  696. };
  697. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  698. }
  699. static int nfsd_get_sb(struct file_system_type *fs_type,
  700. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  701. {
  702. return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
  703. }
  704. static struct file_system_type nfsd_fs_type = {
  705. .owner = THIS_MODULE,
  706. .name = "nfsd",
  707. .get_sb = nfsd_get_sb,
  708. .kill_sb = kill_litter_super,
  709. };
  710. #ifdef CONFIG_PROC_FS
  711. static int create_proc_exports_entry(void)
  712. {
  713. struct proc_dir_entry *entry;
  714. entry = proc_mkdir("fs/nfs", NULL);
  715. if (!entry)
  716. return -ENOMEM;
  717. entry = create_proc_entry("fs/nfs/exports", 0, NULL);
  718. if (!entry)
  719. return -ENOMEM;
  720. entry->proc_fops = &exports_operations;
  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)