nfsctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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_threads(struct file *file, char *buf, size_t size);
  82. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  83. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  84. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  85. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  86. #ifdef CONFIG_NFSD_V4
  87. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  88. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  89. #endif
  90. static ssize_t failover_unlock_ip(struct file *file, char *buf, size_t size);
  91. static ssize_t failover_unlock_fs(struct file *file, char *buf, size_t size);
  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] = failover_unlock_ip,
  102. [NFSD_FO_UnlockFS] = failover_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 failover_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, NIPQUAD_FMT "%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 failover_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) return -EINVAL;
  343. path = dname+len+1;
  344. len = qword_get(&mesg, path, size);
  345. if (len <= 0) return -EINVAL;
  346. len = get_int(&mesg, &maxsize);
  347. if (len)
  348. return len;
  349. if (maxsize < NFS_FHSIZE)
  350. return -EINVAL;
  351. if (maxsize > NFS3_FHSIZE)
  352. maxsize = NFS3_FHSIZE;
  353. if (qword_get(&mesg, mesg, size)>0)
  354. return -EINVAL;
  355. /* we have all the words, they are in buf.. */
  356. dom = unix_domain_find(dname);
  357. if (!dom)
  358. return -ENOMEM;
  359. len = exp_rootfh(dom, path, &fh, maxsize);
  360. auth_domain_put(dom);
  361. if (len)
  362. return len;
  363. mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
  364. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  365. mesg[-1] = '\n';
  366. return mesg - buf;
  367. }
  368. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  369. {
  370. /* if size > 0, look for a number of threads and call nfsd_svc
  371. * then write out number of threads as reply
  372. */
  373. char *mesg = buf;
  374. int rv;
  375. if (size > 0) {
  376. int newthreads;
  377. rv = get_int(&mesg, &newthreads);
  378. if (rv)
  379. return rv;
  380. if (newthreads <0)
  381. return -EINVAL;
  382. rv = nfsd_svc(2049, newthreads);
  383. if (rv)
  384. return rv;
  385. }
  386. sprintf(buf, "%d\n", nfsd_nrthreads());
  387. return strlen(buf);
  388. }
  389. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  390. {
  391. /* if size > 0, look for an array of number of threads per node
  392. * and apply them then write out number of threads per node as reply
  393. */
  394. char *mesg = buf;
  395. int i;
  396. int rv;
  397. int len;
  398. int npools;
  399. int *nthreads;
  400. mutex_lock(&nfsd_mutex);
  401. npools = nfsd_nrpools();
  402. if (npools == 0) {
  403. /*
  404. * NFS is shut down. The admin can start it by
  405. * writing to the threads file but NOT the pool_threads
  406. * file, sorry. Report zero threads.
  407. */
  408. mutex_unlock(&nfsd_mutex);
  409. strcpy(buf, "0\n");
  410. return strlen(buf);
  411. }
  412. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  413. rv = -ENOMEM;
  414. if (nthreads == NULL)
  415. goto out_free;
  416. if (size > 0) {
  417. for (i = 0; i < npools; i++) {
  418. rv = get_int(&mesg, &nthreads[i]);
  419. if (rv == -ENOENT)
  420. break; /* fewer numbers than pools */
  421. if (rv)
  422. goto out_free; /* syntax error */
  423. rv = -EINVAL;
  424. if (nthreads[i] < 0)
  425. goto out_free;
  426. }
  427. rv = nfsd_set_nrthreads(i, nthreads);
  428. if (rv)
  429. goto out_free;
  430. }
  431. rv = nfsd_get_nrthreads(npools, nthreads);
  432. if (rv)
  433. goto out_free;
  434. mesg = buf;
  435. size = SIMPLE_TRANSACTION_LIMIT;
  436. for (i = 0; i < npools && size > 0; i++) {
  437. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  438. len = strlen(mesg);
  439. size -= len;
  440. mesg += len;
  441. }
  442. mutex_unlock(&nfsd_mutex);
  443. return (mesg-buf);
  444. out_free:
  445. kfree(nthreads);
  446. mutex_unlock(&nfsd_mutex);
  447. return rv;
  448. }
  449. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  450. {
  451. /*
  452. * Format:
  453. * [-/+]vers [-/+]vers ...
  454. */
  455. char *mesg = buf;
  456. char *vers, sign;
  457. int len, num;
  458. ssize_t tlen = 0;
  459. char *sep;
  460. if (size>0) {
  461. if (nfsd_serv)
  462. /* Cannot change versions without updating
  463. * nfsd_serv->sv_xdrsize, and reallocing
  464. * rq_argp and rq_resp
  465. */
  466. return -EBUSY;
  467. if (buf[size-1] != '\n')
  468. return -EINVAL;
  469. buf[size-1] = 0;
  470. vers = mesg;
  471. len = qword_get(&mesg, vers, size);
  472. if (len <= 0) return -EINVAL;
  473. do {
  474. sign = *vers;
  475. if (sign == '+' || sign == '-')
  476. num = simple_strtol((vers+1), NULL, 0);
  477. else
  478. num = simple_strtol(vers, NULL, 0);
  479. switch(num) {
  480. case 2:
  481. case 3:
  482. case 4:
  483. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  484. break;
  485. default:
  486. return -EINVAL;
  487. }
  488. vers += len + 1;
  489. tlen += len;
  490. } while ((len = qword_get(&mesg, vers, size)) > 0);
  491. /* If all get turned off, turn them back on, as
  492. * having no versions is BAD
  493. */
  494. nfsd_reset_versions();
  495. }
  496. /* Now write current state into reply buffer */
  497. len = 0;
  498. sep = "";
  499. for (num=2 ; num <= 4 ; num++)
  500. if (nfsd_vers(num, NFSD_AVAIL)) {
  501. len += sprintf(buf+len, "%s%c%d", sep,
  502. nfsd_vers(num, NFSD_TEST)?'+':'-',
  503. num);
  504. sep = " ";
  505. }
  506. len += sprintf(buf+len, "\n");
  507. return len;
  508. }
  509. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  510. {
  511. ssize_t rv;
  512. mutex_lock(&nfsd_mutex);
  513. rv = __write_versions(file, buf, size);
  514. mutex_unlock(&nfsd_mutex);
  515. return rv;
  516. }
  517. static ssize_t __write_ports(struct file *file, char *buf, size_t size)
  518. {
  519. if (size == 0) {
  520. int len = 0;
  521. if (nfsd_serv)
  522. len = svc_xprt_names(nfsd_serv, buf, 0);
  523. return len;
  524. }
  525. /* Either a single 'fd' number is written, in which
  526. * case it must be for a socket of a supported family/protocol,
  527. * and we use it as an nfsd socket, or
  528. * A '-' followed by the 'name' of a socket in which case
  529. * we close the socket.
  530. */
  531. if (isdigit(buf[0])) {
  532. char *mesg = buf;
  533. int fd;
  534. int err;
  535. err = get_int(&mesg, &fd);
  536. if (err)
  537. return -EINVAL;
  538. if (fd < 0)
  539. return -EINVAL;
  540. err = nfsd_create_serv();
  541. if (!err) {
  542. err = svc_addsock(nfsd_serv, fd, buf);
  543. if (err >= 0) {
  544. err = lockd_up();
  545. if (err < 0)
  546. svc_sock_names(buf+strlen(buf)+1, nfsd_serv, buf);
  547. }
  548. /* Decrease the count, but don't shutdown the
  549. * the service
  550. */
  551. nfsd_serv->sv_nrthreads--;
  552. }
  553. return err < 0 ? err : 0;
  554. }
  555. if (buf[0] == '-' && isdigit(buf[1])) {
  556. char *toclose = kstrdup(buf+1, GFP_KERNEL);
  557. int len = 0;
  558. if (!toclose)
  559. return -ENOMEM;
  560. if (nfsd_serv)
  561. len = svc_sock_names(buf, nfsd_serv, toclose);
  562. if (len >= 0)
  563. lockd_down();
  564. kfree(toclose);
  565. return len;
  566. }
  567. /*
  568. * Add a transport listener by writing it's transport name
  569. */
  570. if (isalpha(buf[0])) {
  571. int err;
  572. char transport[16];
  573. int port;
  574. if (sscanf(buf, "%15s %4d", transport, &port) == 2) {
  575. err = nfsd_create_serv();
  576. if (!err) {
  577. err = svc_create_xprt(nfsd_serv,
  578. transport, port,
  579. SVC_SOCK_ANONYMOUS);
  580. if (err == -ENOENT)
  581. /* Give a reasonable perror msg for
  582. * bad transport string */
  583. err = -EPROTONOSUPPORT;
  584. }
  585. return err < 0 ? err : 0;
  586. }
  587. }
  588. /*
  589. * Remove a transport by writing it's transport name and port number
  590. */
  591. if (buf[0] == '-' && isalpha(buf[1])) {
  592. struct svc_xprt *xprt;
  593. int err = -EINVAL;
  594. char transport[16];
  595. int port;
  596. if (sscanf(&buf[1], "%15s %4d", transport, &port) == 2) {
  597. if (port == 0)
  598. return -EINVAL;
  599. if (nfsd_serv) {
  600. xprt = svc_find_xprt(nfsd_serv, transport,
  601. AF_UNSPEC, port);
  602. if (xprt) {
  603. svc_close_xprt(xprt);
  604. svc_xprt_put(xprt);
  605. err = 0;
  606. } else
  607. err = -ENOTCONN;
  608. }
  609. return err < 0 ? err : 0;
  610. }
  611. }
  612. return -EINVAL;
  613. }
  614. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  615. {
  616. ssize_t rv;
  617. mutex_lock(&nfsd_mutex);
  618. rv = __write_ports(file, buf, size);
  619. mutex_unlock(&nfsd_mutex);
  620. return rv;
  621. }
  622. int nfsd_max_blksize;
  623. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  624. {
  625. char *mesg = buf;
  626. if (size > 0) {
  627. int bsize;
  628. int rv = get_int(&mesg, &bsize);
  629. if (rv)
  630. return rv;
  631. /* force bsize into allowed range and
  632. * required alignment.
  633. */
  634. if (bsize < 1024)
  635. bsize = 1024;
  636. if (bsize > NFSSVC_MAXBLKSIZE)
  637. bsize = NFSSVC_MAXBLKSIZE;
  638. bsize &= ~(1024-1);
  639. mutex_lock(&nfsd_mutex);
  640. if (nfsd_serv && nfsd_serv->sv_nrthreads) {
  641. mutex_unlock(&nfsd_mutex);
  642. return -EBUSY;
  643. }
  644. nfsd_max_blksize = bsize;
  645. mutex_unlock(&nfsd_mutex);
  646. }
  647. return sprintf(buf, "%d\n", nfsd_max_blksize);
  648. }
  649. #ifdef CONFIG_NFSD_V4
  650. extern time_t nfs4_leasetime(void);
  651. static ssize_t __write_leasetime(struct file *file, char *buf, size_t size)
  652. {
  653. /* if size > 10 seconds, call
  654. * nfs4_reset_lease() then write out the new lease (seconds) as reply
  655. */
  656. char *mesg = buf;
  657. int rv, lease;
  658. if (size > 0) {
  659. if (nfsd_serv)
  660. return -EBUSY;
  661. rv = get_int(&mesg, &lease);
  662. if (rv)
  663. return rv;
  664. if (lease < 10 || lease > 3600)
  665. return -EINVAL;
  666. nfs4_reset_lease(lease);
  667. }
  668. sprintf(buf, "%ld\n", nfs4_lease_time());
  669. return strlen(buf);
  670. }
  671. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  672. {
  673. ssize_t rv;
  674. mutex_lock(&nfsd_mutex);
  675. rv = __write_leasetime(file, buf, size);
  676. mutex_unlock(&nfsd_mutex);
  677. return rv;
  678. }
  679. extern char *nfs4_recoverydir(void);
  680. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
  681. {
  682. char *mesg = buf;
  683. char *recdir;
  684. int len, status;
  685. if (size > 0) {
  686. if (nfsd_serv)
  687. return -EBUSY;
  688. if (size > PATH_MAX || buf[size-1] != '\n')
  689. return -EINVAL;
  690. buf[size-1] = 0;
  691. recdir = mesg;
  692. len = qword_get(&mesg, recdir, size);
  693. if (len <= 0)
  694. return -EINVAL;
  695. status = nfs4_reset_recoverydir(recdir);
  696. }
  697. sprintf(buf, "%s\n", nfs4_recoverydir());
  698. return strlen(buf);
  699. }
  700. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  701. {
  702. ssize_t rv;
  703. mutex_lock(&nfsd_mutex);
  704. rv = __write_recoverydir(file, buf, size);
  705. mutex_unlock(&nfsd_mutex);
  706. return rv;
  707. }
  708. #endif
  709. /*----------------------------------------------------------------------------*/
  710. /*
  711. * populating the filesystem.
  712. */
  713. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  714. {
  715. static struct tree_descr nfsd_files[] = {
  716. [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
  717. [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
  718. [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
  719. [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
  720. [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
  721. [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
  722. [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
  723. [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
  724. [NFSD_FO_UnlockIP] = {"unlock_ip",
  725. &transaction_ops, S_IWUSR|S_IRUSR},
  726. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  727. &transaction_ops, S_IWUSR|S_IRUSR},
  728. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  729. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  730. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  731. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  732. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  733. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  734. #ifdef CONFIG_NFSD_V4
  735. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  736. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  737. #endif
  738. /* last one */ {""}
  739. };
  740. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  741. }
  742. static int nfsd_get_sb(struct file_system_type *fs_type,
  743. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  744. {
  745. return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
  746. }
  747. static struct file_system_type nfsd_fs_type = {
  748. .owner = THIS_MODULE,
  749. .name = "nfsd",
  750. .get_sb = nfsd_get_sb,
  751. .kill_sb = kill_litter_super,
  752. };
  753. #ifdef CONFIG_PROC_FS
  754. static int create_proc_exports_entry(void)
  755. {
  756. struct proc_dir_entry *entry;
  757. entry = proc_mkdir("fs/nfs", NULL);
  758. if (!entry)
  759. return -ENOMEM;
  760. entry = proc_create("exports", 0, entry, &exports_operations);
  761. if (!entry)
  762. return -ENOMEM;
  763. return 0;
  764. }
  765. #else /* CONFIG_PROC_FS */
  766. static int create_proc_exports_entry(void)
  767. {
  768. return 0;
  769. }
  770. #endif
  771. static int __init init_nfsd(void)
  772. {
  773. int retval;
  774. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  775. retval = nfs4_state_init(); /* nfs4 locking state */
  776. if (retval)
  777. return retval;
  778. nfsd_stat_init(); /* Statistics */
  779. retval = nfsd_reply_cache_init();
  780. if (retval)
  781. goto out_free_stat;
  782. retval = nfsd_export_init();
  783. if (retval)
  784. goto out_free_cache;
  785. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  786. retval = nfsd_idmap_init();
  787. if (retval)
  788. goto out_free_lockd;
  789. retval = create_proc_exports_entry();
  790. if (retval)
  791. goto out_free_idmap;
  792. retval = register_filesystem(&nfsd_fs_type);
  793. if (retval)
  794. goto out_free_all;
  795. return 0;
  796. out_free_all:
  797. remove_proc_entry("fs/nfs/exports", NULL);
  798. remove_proc_entry("fs/nfs", NULL);
  799. out_free_idmap:
  800. nfsd_idmap_shutdown();
  801. out_free_lockd:
  802. nfsd_lockd_shutdown();
  803. nfsd_export_shutdown();
  804. out_free_cache:
  805. nfsd_reply_cache_shutdown();
  806. out_free_stat:
  807. nfsd_stat_shutdown();
  808. nfsd4_free_slabs();
  809. return retval;
  810. }
  811. static void __exit exit_nfsd(void)
  812. {
  813. nfsd_export_shutdown();
  814. nfsd_reply_cache_shutdown();
  815. remove_proc_entry("fs/nfs/exports", NULL);
  816. remove_proc_entry("fs/nfs", NULL);
  817. nfsd_stat_shutdown();
  818. nfsd_lockd_shutdown();
  819. nfsd_idmap_shutdown();
  820. nfsd4_free_slabs();
  821. unregister_filesystem(&nfsd_fs_type);
  822. }
  823. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  824. MODULE_LICENSE("GPL");
  825. module_init(init_nfsd)
  826. module_exit(exit_nfsd)