nfsctl.c 13 KB

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