nfsctl.c 13 KB

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