nfsctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/string.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/ctype.h>
  26. #include <linux/nfs.h>
  27. #include <linux/nfsd_idmap.h>
  28. #include <linux/lockd/bind.h>
  29. #include <linux/sunrpc/svc.h>
  30. #include <linux/sunrpc/svcsock.h>
  31. #include <linux/nfsd/nfsd.h>
  32. #include <linux/nfsd/cache.h>
  33. #include <linux/nfsd/xdr.h>
  34. #include <linux/nfsd/syscall.h>
  35. #include <linux/nfsd/interface.h>
  36. #include <asm/uaccess.h>
  37. /*
  38. * We have a single directory with 9 nodes in it.
  39. */
  40. enum {
  41. NFSD_Root = 1,
  42. NFSD_Svc,
  43. NFSD_Add,
  44. NFSD_Del,
  45. NFSD_Export,
  46. NFSD_Unexport,
  47. NFSD_Getfd,
  48. NFSD_Getfs,
  49. NFSD_List,
  50. NFSD_Fh,
  51. NFSD_Threads,
  52. NFSD_Versions,
  53. NFSD_Ports,
  54. /*
  55. * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
  56. * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
  57. */
  58. #ifdef CONFIG_NFSD_V4
  59. NFSD_Leasetime,
  60. NFSD_RecoveryDir,
  61. #endif
  62. };
  63. /*
  64. * write() for these nodes.
  65. */
  66. static ssize_t write_svc(struct file *file, char *buf, size_t size);
  67. static ssize_t write_add(struct file *file, char *buf, size_t size);
  68. static ssize_t write_del(struct file *file, char *buf, size_t size);
  69. static ssize_t write_export(struct file *file, char *buf, size_t size);
  70. static ssize_t write_unexport(struct file *file, char *buf, size_t size);
  71. static ssize_t write_getfd(struct file *file, char *buf, size_t size);
  72. static ssize_t write_getfs(struct file *file, char *buf, size_t size);
  73. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  74. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  75. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  76. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  77. #ifdef CONFIG_NFSD_V4
  78. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  79. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  80. #endif
  81. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  82. [NFSD_Svc] = write_svc,
  83. [NFSD_Add] = write_add,
  84. [NFSD_Del] = write_del,
  85. [NFSD_Export] = write_export,
  86. [NFSD_Unexport] = write_unexport,
  87. [NFSD_Getfd] = write_getfd,
  88. [NFSD_Getfs] = write_getfs,
  89. [NFSD_Fh] = write_filehandle,
  90. [NFSD_Threads] = write_threads,
  91. [NFSD_Versions] = write_versions,
  92. [NFSD_Ports] = write_ports,
  93. #ifdef CONFIG_NFSD_V4
  94. [NFSD_Leasetime] = write_leasetime,
  95. [NFSD_RecoveryDir] = write_recoverydir,
  96. #endif
  97. };
  98. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  99. {
  100. ino_t ino = file->f_dentry->d_inode->i_ino;
  101. char *data;
  102. ssize_t rv;
  103. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  104. return -EINVAL;
  105. data = simple_transaction_get(file, buf, size);
  106. if (IS_ERR(data))
  107. return PTR_ERR(data);
  108. rv = write_op[ino](file, data, size);
  109. if (rv>0) {
  110. simple_transaction_set(file, rv);
  111. rv = size;
  112. }
  113. return rv;
  114. }
  115. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  116. {
  117. if (! file->private_data) {
  118. /* An attempt to read a transaction file without writing
  119. * causes a 0-byte write so that the file can return
  120. * state information
  121. */
  122. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  123. if (rv < 0)
  124. return rv;
  125. }
  126. return simple_transaction_read(file, buf, size, pos);
  127. }
  128. static const struct file_operations transaction_ops = {
  129. .write = nfsctl_transaction_write,
  130. .read = nfsctl_transaction_read,
  131. .release = simple_transaction_release,
  132. };
  133. extern struct seq_operations nfs_exports_op;
  134. static int exports_open(struct inode *inode, struct file *file)
  135. {
  136. return seq_open(file, &nfs_exports_op);
  137. }
  138. static const struct file_operations exports_operations = {
  139. .open = exports_open,
  140. .read = seq_read,
  141. .llseek = seq_lseek,
  142. .release = seq_release,
  143. };
  144. /*----------------------------------------------------------------------------*/
  145. /*
  146. * payload - write methods
  147. * If the method has a response, the response should be put in buf,
  148. * and the length returned. Otherwise return 0 or and -error.
  149. */
  150. static ssize_t write_svc(struct file *file, char *buf, size_t size)
  151. {
  152. struct nfsctl_svc *data;
  153. if (size < sizeof(*data))
  154. return -EINVAL;
  155. data = (struct nfsctl_svc*) buf;
  156. return nfsd_svc(data->svc_port, data->svc_nthreads);
  157. }
  158. static ssize_t write_add(struct file *file, char *buf, size_t size)
  159. {
  160. struct nfsctl_client *data;
  161. if (size < sizeof(*data))
  162. return -EINVAL;
  163. data = (struct nfsctl_client *)buf;
  164. return exp_addclient(data);
  165. }
  166. static ssize_t write_del(struct file *file, char *buf, size_t size)
  167. {
  168. struct nfsctl_client *data;
  169. if (size < sizeof(*data))
  170. return -EINVAL;
  171. data = (struct nfsctl_client *)buf;
  172. return exp_delclient(data);
  173. }
  174. static ssize_t write_export(struct file *file, char *buf, size_t size)
  175. {
  176. struct nfsctl_export *data;
  177. if (size < sizeof(*data))
  178. return -EINVAL;
  179. data = (struct nfsctl_export*)buf;
  180. return exp_export(data);
  181. }
  182. static ssize_t write_unexport(struct file *file, char *buf, size_t size)
  183. {
  184. struct nfsctl_export *data;
  185. if (size < sizeof(*data))
  186. return -EINVAL;
  187. data = (struct nfsctl_export*)buf;
  188. return exp_unexport(data);
  189. }
  190. static ssize_t write_getfs(struct file *file, char *buf, size_t size)
  191. {
  192. struct nfsctl_fsparm *data;
  193. struct sockaddr_in *sin;
  194. struct auth_domain *clp;
  195. int err = 0;
  196. struct knfsd_fh *res;
  197. if (size < sizeof(*data))
  198. return -EINVAL;
  199. data = (struct nfsctl_fsparm*)buf;
  200. err = -EPROTONOSUPPORT;
  201. if (data->gd_addr.sa_family != AF_INET)
  202. goto out;
  203. sin = (struct sockaddr_in *)&data->gd_addr;
  204. if (data->gd_maxlen > NFS3_FHSIZE)
  205. data->gd_maxlen = NFS3_FHSIZE;
  206. res = (struct knfsd_fh*)buf;
  207. exp_readlock();
  208. if (!(clp = auth_unix_lookup(sin->sin_addr)))
  209. err = -EPERM;
  210. else {
  211. err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
  212. auth_domain_put(clp);
  213. }
  214. exp_readunlock();
  215. if (err == 0)
  216. err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
  217. out:
  218. return err;
  219. }
  220. static ssize_t write_getfd(struct file *file, char *buf, size_t size)
  221. {
  222. struct nfsctl_fdparm *data;
  223. struct sockaddr_in *sin;
  224. struct auth_domain *clp;
  225. int err = 0;
  226. struct knfsd_fh fh;
  227. char *res;
  228. if (size < sizeof(*data))
  229. return -EINVAL;
  230. data = (struct nfsctl_fdparm*)buf;
  231. err = -EPROTONOSUPPORT;
  232. if (data->gd_addr.sa_family != AF_INET)
  233. goto out;
  234. err = -EINVAL;
  235. if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
  236. goto out;
  237. res = buf;
  238. sin = (struct sockaddr_in *)&data->gd_addr;
  239. exp_readlock();
  240. if (!(clp = auth_unix_lookup(sin->sin_addr)))
  241. err = -EPERM;
  242. else {
  243. err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
  244. auth_domain_put(clp);
  245. }
  246. exp_readunlock();
  247. if (err == 0) {
  248. memset(res,0, NFS_FHSIZE);
  249. memcpy(res, &fh.fh_base, fh.fh_size);
  250. err = NFS_FHSIZE;
  251. }
  252. out:
  253. return err;
  254. }
  255. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  256. {
  257. /* request is:
  258. * domain path maxsize
  259. * response is
  260. * filehandle
  261. *
  262. * qword quoting is used, so filehandle will be \x....
  263. */
  264. char *dname, *path;
  265. int maxsize;
  266. char *mesg = buf;
  267. int len;
  268. struct auth_domain *dom;
  269. struct knfsd_fh fh;
  270. if (buf[size-1] != '\n')
  271. return -EINVAL;
  272. buf[size-1] = 0;
  273. dname = mesg;
  274. len = qword_get(&mesg, dname, size);
  275. if (len <= 0) return -EINVAL;
  276. path = dname+len+1;
  277. len = qword_get(&mesg, path, size);
  278. if (len <= 0) return -EINVAL;
  279. len = get_int(&mesg, &maxsize);
  280. if (len)
  281. return len;
  282. if (maxsize < NFS_FHSIZE)
  283. return -EINVAL;
  284. if (maxsize > NFS3_FHSIZE)
  285. maxsize = NFS3_FHSIZE;
  286. if (qword_get(&mesg, mesg, size)>0)
  287. return -EINVAL;
  288. /* we have all the words, they are in buf.. */
  289. dom = unix_domain_find(dname);
  290. if (!dom)
  291. return -ENOMEM;
  292. len = exp_rootfh(dom, path, &fh, maxsize);
  293. auth_domain_put(dom);
  294. if (len)
  295. return len;
  296. mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
  297. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  298. mesg[-1] = '\n';
  299. return mesg - buf;
  300. }
  301. extern int nfsd_nrthreads(void);
  302. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  303. {
  304. /* if size > 0, look for a number of threads and call nfsd_svc
  305. * then write out number of threads as reply
  306. */
  307. char *mesg = buf;
  308. int rv;
  309. if (size > 0) {
  310. int newthreads;
  311. rv = get_int(&mesg, &newthreads);
  312. if (rv)
  313. return rv;
  314. if (newthreads <0)
  315. return -EINVAL;
  316. rv = nfsd_svc(2049, newthreads);
  317. if (rv)
  318. return rv;
  319. }
  320. sprintf(buf, "%d\n", nfsd_nrthreads());
  321. return strlen(buf);
  322. }
  323. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  324. {
  325. /*
  326. * Format:
  327. * [-/+]vers [-/+]vers ...
  328. */
  329. char *mesg = buf;
  330. char *vers, sign;
  331. int len, num;
  332. ssize_t tlen = 0;
  333. char *sep;
  334. if (size>0) {
  335. if (nfsd_serv)
  336. /* Cannot change versions without updating
  337. * nfsd_serv->sv_xdrsize, and reallocing
  338. * rq_argp and rq_resp
  339. */
  340. return -EBUSY;
  341. if (buf[size-1] != '\n')
  342. return -EINVAL;
  343. buf[size-1] = 0;
  344. vers = mesg;
  345. len = qword_get(&mesg, vers, size);
  346. if (len <= 0) return -EINVAL;
  347. do {
  348. sign = *vers;
  349. if (sign == '+' || sign == '-')
  350. num = simple_strtol((vers+1), NULL, 0);
  351. else
  352. num = simple_strtol(vers, NULL, 0);
  353. switch(num) {
  354. case 2:
  355. case 3:
  356. case 4:
  357. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. vers += len + 1;
  363. tlen += len;
  364. } while ((len = qword_get(&mesg, vers, size)) > 0);
  365. /* If all get turned off, turn them back on, as
  366. * having no versions is BAD
  367. */
  368. nfsd_reset_versions();
  369. }
  370. /* Now write current state into reply buffer */
  371. len = 0;
  372. sep = "";
  373. for (num=2 ; num <= 4 ; num++)
  374. if (nfsd_vers(num, NFSD_AVAIL)) {
  375. len += sprintf(buf+len, "%s%c%d", sep,
  376. nfsd_vers(num, NFSD_TEST)?'+':'-',
  377. num);
  378. sep = " ";
  379. }
  380. len += sprintf(buf+len, "\n");
  381. return len;
  382. }
  383. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  384. {
  385. if (size == 0) {
  386. int len = 0;
  387. lock_kernel();
  388. if (nfsd_serv)
  389. len = svc_sock_names(buf, nfsd_serv, NULL);
  390. unlock_kernel();
  391. return len;
  392. }
  393. /* Either a single 'fd' number is written, in which
  394. * case it must be for a socket of a supported family/protocol,
  395. * and we use it as an nfsd socket, or
  396. * A '-' followed by the 'name' of a socket in which case
  397. * we close the socket.
  398. */
  399. if (isdigit(buf[0])) {
  400. char *mesg = buf;
  401. int fd;
  402. int err;
  403. err = get_int(&mesg, &fd);
  404. if (err)
  405. return -EINVAL;
  406. if (fd < 0)
  407. return -EINVAL;
  408. err = nfsd_create_serv();
  409. if (!err) {
  410. int proto = 0;
  411. err = lockd_up(proto);
  412. if (!err) {
  413. err = svc_addsock(nfsd_serv, fd, buf, &proto);
  414. if (err)
  415. lockd_down();
  416. }
  417. /* Decrease the count, but don't shutdown the
  418. * the service
  419. */
  420. nfsd_serv->sv_nrthreads--;
  421. }
  422. return err;
  423. }
  424. if (buf[0] == '-') {
  425. char *toclose = kstrdup(buf+1, GFP_KERNEL);
  426. int len = 0;
  427. if (!toclose)
  428. return -ENOMEM;
  429. lock_kernel();
  430. if (nfsd_serv)
  431. len = svc_sock_names(buf, nfsd_serv, toclose);
  432. unlock_kernel();
  433. kfree(toclose);
  434. return len;
  435. }
  436. return -EINVAL;
  437. }
  438. #ifdef CONFIG_NFSD_V4
  439. extern time_t nfs4_leasetime(void);
  440. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  441. {
  442. /* if size > 10 seconds, call
  443. * nfs4_reset_lease() then write out the new lease (seconds) as reply
  444. */
  445. char *mesg = buf;
  446. int rv;
  447. if (size > 0) {
  448. int lease;
  449. rv = get_int(&mesg, &lease);
  450. if (rv)
  451. return rv;
  452. if (lease < 10 || lease > 3600)
  453. return -EINVAL;
  454. nfs4_reset_lease(lease);
  455. }
  456. sprintf(buf, "%ld\n", nfs4_lease_time());
  457. return strlen(buf);
  458. }
  459. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  460. {
  461. char *mesg = buf;
  462. char *recdir;
  463. int len, status;
  464. if (size > PATH_MAX || buf[size-1] != '\n')
  465. return -EINVAL;
  466. buf[size-1] = 0;
  467. recdir = mesg;
  468. len = qword_get(&mesg, recdir, size);
  469. if (len <= 0)
  470. return -EINVAL;
  471. status = nfs4_reset_recoverydir(recdir);
  472. return strlen(buf);
  473. }
  474. #endif
  475. /*----------------------------------------------------------------------------*/
  476. /*
  477. * populating the filesystem.
  478. */
  479. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  480. {
  481. static struct tree_descr nfsd_files[] = {
  482. [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
  483. [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
  484. [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
  485. [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
  486. [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
  487. [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
  488. [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
  489. [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
  490. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  491. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  492. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  493. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  494. #ifdef CONFIG_NFSD_V4
  495. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  496. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  497. #endif
  498. /* last one */ {""}
  499. };
  500. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  501. }
  502. static int nfsd_get_sb(struct file_system_type *fs_type,
  503. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  504. {
  505. return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
  506. }
  507. static struct file_system_type nfsd_fs_type = {
  508. .owner = THIS_MODULE,
  509. .name = "nfsd",
  510. .get_sb = nfsd_get_sb,
  511. .kill_sb = kill_litter_super,
  512. };
  513. static int __init init_nfsd(void)
  514. {
  515. int retval;
  516. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  517. nfsd_stat_init(); /* Statistics */
  518. nfsd_cache_init(); /* RPC reply cache */
  519. nfsd_export_init(); /* Exports table */
  520. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  521. nfs4_state_init(); /* NFSv4 locking state */
  522. nfsd_idmap_init(); /* Name to ID mapping */
  523. if (proc_mkdir("fs/nfs", NULL)) {
  524. struct proc_dir_entry *entry;
  525. entry = create_proc_entry("fs/nfs/exports", 0, NULL);
  526. if (entry)
  527. entry->proc_fops = &exports_operations;
  528. }
  529. retval = register_filesystem(&nfsd_fs_type);
  530. if (retval) {
  531. nfsd_export_shutdown();
  532. nfsd_cache_shutdown();
  533. remove_proc_entry("fs/nfs/exports", NULL);
  534. remove_proc_entry("fs/nfs", NULL);
  535. nfsd_stat_shutdown();
  536. nfsd_lockd_shutdown();
  537. }
  538. return retval;
  539. }
  540. static void __exit exit_nfsd(void)
  541. {
  542. nfsd_export_shutdown();
  543. nfsd_cache_shutdown();
  544. remove_proc_entry("fs/nfs/exports", NULL);
  545. remove_proc_entry("fs/nfs", NULL);
  546. nfsd_stat_shutdown();
  547. nfsd_lockd_shutdown();
  548. nfsd_idmap_shutdown();
  549. unregister_filesystem(&nfsd_fs_type);
  550. }
  551. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  552. MODULE_LICENSE("GPL");
  553. module_init(init_nfsd)
  554. module_exit(exit_nfsd)