nfsctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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/config.h>
  9. #include <linux/module.h>
  10. #include <linux/linkage.h>
  11. #include <linux/time.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.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/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_Leasetime,
  49. NFSD_RecoveryDir,
  50. };
  51. /*
  52. * write() for these nodes.
  53. */
  54. static ssize_t write_svc(struct file *file, char *buf, size_t size);
  55. static ssize_t write_add(struct file *file, char *buf, size_t size);
  56. static ssize_t write_del(struct file *file, char *buf, size_t size);
  57. static ssize_t write_export(struct file *file, char *buf, size_t size);
  58. static ssize_t write_unexport(struct file *file, char *buf, size_t size);
  59. static ssize_t write_getfd(struct file *file, char *buf, size_t size);
  60. static ssize_t write_getfs(struct file *file, char *buf, size_t size);
  61. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  62. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  63. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  64. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  65. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  66. [NFSD_Svc] = write_svc,
  67. [NFSD_Add] = write_add,
  68. [NFSD_Del] = write_del,
  69. [NFSD_Export] = write_export,
  70. [NFSD_Unexport] = write_unexport,
  71. [NFSD_Getfd] = write_getfd,
  72. [NFSD_Getfs] = write_getfs,
  73. [NFSD_Fh] = write_filehandle,
  74. [NFSD_Threads] = write_threads,
  75. [NFSD_Leasetime] = write_leasetime,
  76. [NFSD_RecoveryDir] = write_recoverydir,
  77. };
  78. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  79. {
  80. ino_t ino = file->f_dentry->d_inode->i_ino;
  81. char *data;
  82. ssize_t rv;
  83. if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
  84. return -EINVAL;
  85. data = simple_transaction_get(file, buf, size);
  86. if (IS_ERR(data))
  87. return PTR_ERR(data);
  88. rv = write_op[ino](file, data, size);
  89. if (rv>0) {
  90. simple_transaction_set(file, rv);
  91. rv = size;
  92. }
  93. return rv;
  94. }
  95. static struct file_operations transaction_ops = {
  96. .write = nfsctl_transaction_write,
  97. .read = simple_transaction_read,
  98. .release = simple_transaction_release,
  99. };
  100. extern struct seq_operations nfs_exports_op;
  101. static int exports_open(struct inode *inode, struct file *file)
  102. {
  103. return seq_open(file, &nfs_exports_op);
  104. }
  105. static struct file_operations exports_operations = {
  106. .open = exports_open,
  107. .read = seq_read,
  108. .llseek = seq_lseek,
  109. .release = seq_release,
  110. };
  111. /*----------------------------------------------------------------------------*/
  112. /*
  113. * payload - write methods
  114. * If the method has a response, the response should be put in buf,
  115. * and the length returned. Otherwise return 0 or and -error.
  116. */
  117. static ssize_t write_svc(struct file *file, char *buf, size_t size)
  118. {
  119. struct nfsctl_svc *data;
  120. if (size < sizeof(*data))
  121. return -EINVAL;
  122. data = (struct nfsctl_svc*) buf;
  123. return nfsd_svc(data->svc_port, data->svc_nthreads);
  124. }
  125. static ssize_t write_add(struct file *file, char *buf, size_t size)
  126. {
  127. struct nfsctl_client *data;
  128. if (size < sizeof(*data))
  129. return -EINVAL;
  130. data = (struct nfsctl_client *)buf;
  131. return exp_addclient(data);
  132. }
  133. static ssize_t write_del(struct file *file, char *buf, size_t size)
  134. {
  135. struct nfsctl_client *data;
  136. if (size < sizeof(*data))
  137. return -EINVAL;
  138. data = (struct nfsctl_client *)buf;
  139. return exp_delclient(data);
  140. }
  141. static ssize_t write_export(struct file *file, char *buf, size_t size)
  142. {
  143. struct nfsctl_export *data;
  144. if (size < sizeof(*data))
  145. return -EINVAL;
  146. data = (struct nfsctl_export*)buf;
  147. return exp_export(data);
  148. }
  149. static ssize_t write_unexport(struct file *file, char *buf, size_t size)
  150. {
  151. struct nfsctl_export *data;
  152. if (size < sizeof(*data))
  153. return -EINVAL;
  154. data = (struct nfsctl_export*)buf;
  155. return exp_unexport(data);
  156. }
  157. static ssize_t write_getfs(struct file *file, char *buf, size_t size)
  158. {
  159. struct nfsctl_fsparm *data;
  160. struct sockaddr_in *sin;
  161. struct auth_domain *clp;
  162. int err = 0;
  163. struct knfsd_fh *res;
  164. if (size < sizeof(*data))
  165. return -EINVAL;
  166. data = (struct nfsctl_fsparm*)buf;
  167. err = -EPROTONOSUPPORT;
  168. if (data->gd_addr.sa_family != AF_INET)
  169. goto out;
  170. sin = (struct sockaddr_in *)&data->gd_addr;
  171. if (data->gd_maxlen > NFS3_FHSIZE)
  172. data->gd_maxlen = NFS3_FHSIZE;
  173. res = (struct knfsd_fh*)buf;
  174. exp_readlock();
  175. if (!(clp = auth_unix_lookup(sin->sin_addr)))
  176. err = -EPERM;
  177. else {
  178. err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
  179. auth_domain_put(clp);
  180. }
  181. exp_readunlock();
  182. if (err == 0)
  183. err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
  184. out:
  185. return err;
  186. }
  187. static ssize_t write_getfd(struct file *file, char *buf, size_t size)
  188. {
  189. struct nfsctl_fdparm *data;
  190. struct sockaddr_in *sin;
  191. struct auth_domain *clp;
  192. int err = 0;
  193. struct knfsd_fh fh;
  194. char *res;
  195. if (size < sizeof(*data))
  196. return -EINVAL;
  197. data = (struct nfsctl_fdparm*)buf;
  198. err = -EPROTONOSUPPORT;
  199. if (data->gd_addr.sa_family != AF_INET)
  200. goto out;
  201. err = -EINVAL;
  202. if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
  203. goto out;
  204. res = buf;
  205. sin = (struct sockaddr_in *)&data->gd_addr;
  206. exp_readlock();
  207. if (!(clp = auth_unix_lookup(sin->sin_addr)))
  208. err = -EPERM;
  209. else {
  210. err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
  211. auth_domain_put(clp);
  212. }
  213. exp_readunlock();
  214. if (err == 0) {
  215. memset(res,0, NFS_FHSIZE);
  216. memcpy(res, &fh.fh_base, fh.fh_size);
  217. err = NFS_FHSIZE;
  218. }
  219. out:
  220. return err;
  221. }
  222. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  223. {
  224. /* request is:
  225. * domain path maxsize
  226. * response is
  227. * filehandle
  228. *
  229. * qword quoting is used, so filehandle will be \x....
  230. */
  231. char *dname, *path;
  232. int maxsize;
  233. char *mesg = buf;
  234. int len;
  235. struct auth_domain *dom;
  236. struct knfsd_fh fh;
  237. if (buf[size-1] != '\n')
  238. return -EINVAL;
  239. buf[size-1] = 0;
  240. dname = mesg;
  241. len = qword_get(&mesg, dname, size);
  242. if (len <= 0) return -EINVAL;
  243. path = dname+len+1;
  244. len = qword_get(&mesg, path, size);
  245. if (len <= 0) return -EINVAL;
  246. len = get_int(&mesg, &maxsize);
  247. if (len)
  248. return len;
  249. if (maxsize < NFS_FHSIZE)
  250. return -EINVAL;
  251. if (maxsize > NFS3_FHSIZE)
  252. maxsize = NFS3_FHSIZE;
  253. if (qword_get(&mesg, mesg, size)>0)
  254. return -EINVAL;
  255. /* we have all the words, they are in buf.. */
  256. dom = unix_domain_find(dname);
  257. if (!dom)
  258. return -ENOMEM;
  259. len = exp_rootfh(dom, path, &fh, maxsize);
  260. auth_domain_put(dom);
  261. if (len)
  262. return len;
  263. mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
  264. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  265. mesg[-1] = '\n';
  266. return mesg - buf;
  267. }
  268. extern int nfsd_nrthreads(void);
  269. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  270. {
  271. /* if size > 0, look for a number of threads and call nfsd_svc
  272. * then write out number of threads as reply
  273. */
  274. char *mesg = buf;
  275. int rv;
  276. if (size > 0) {
  277. int newthreads;
  278. rv = get_int(&mesg, &newthreads);
  279. if (rv)
  280. return rv;
  281. if (newthreads <0)
  282. return -EINVAL;
  283. rv = nfsd_svc(2049, newthreads);
  284. if (rv)
  285. return rv;
  286. }
  287. sprintf(buf, "%d\n", nfsd_nrthreads());
  288. return strlen(buf);
  289. }
  290. extern time_t nfs4_leasetime(void);
  291. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  292. {
  293. /* if size > 10 seconds, call
  294. * nfs4_reset_lease() then write out the new lease (seconds) as reply
  295. */
  296. char *mesg = buf;
  297. int rv;
  298. if (size > 0) {
  299. int lease;
  300. rv = get_int(&mesg, &lease);
  301. if (rv)
  302. return rv;
  303. if (lease < 10 || lease > 3600)
  304. return -EINVAL;
  305. nfs4_reset_lease(lease);
  306. }
  307. sprintf(buf, "%ld\n", nfs4_lease_time());
  308. return strlen(buf);
  309. }
  310. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  311. {
  312. char *mesg = buf;
  313. char *recdir;
  314. int len, status;
  315. if (size > PATH_MAX || buf[size-1] != '\n')
  316. return -EINVAL;
  317. buf[size-1] = 0;
  318. recdir = mesg;
  319. len = qword_get(&mesg, recdir, size);
  320. if (len <= 0)
  321. return -EINVAL;
  322. status = nfs4_reset_recoverydir(recdir);
  323. return strlen(buf);
  324. }
  325. /*----------------------------------------------------------------------------*/
  326. /*
  327. * populating the filesystem.
  328. */
  329. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  330. {
  331. static struct tree_descr nfsd_files[] = {
  332. [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
  333. [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
  334. [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
  335. [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
  336. [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
  337. [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
  338. [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
  339. [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
  340. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  341. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  342. #ifdef CONFIG_NFSD_V4
  343. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  344. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  345. #endif
  346. /* last one */ {""}
  347. };
  348. return simple_fill_super(sb, 0x6e667364, nfsd_files);
  349. }
  350. static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
  351. int flags, const char *dev_name, void *data)
  352. {
  353. return get_sb_single(fs_type, flags, data, nfsd_fill_super);
  354. }
  355. static struct file_system_type nfsd_fs_type = {
  356. .owner = THIS_MODULE,
  357. .name = "nfsd",
  358. .get_sb = nfsd_get_sb,
  359. .kill_sb = kill_litter_super,
  360. };
  361. static int __init init_nfsd(void)
  362. {
  363. int retval;
  364. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  365. nfsd_stat_init(); /* Statistics */
  366. nfsd_cache_init(); /* RPC reply cache */
  367. nfsd_export_init(); /* Exports table */
  368. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  369. nfs4_state_init(); /* NFSv4 locking state */
  370. nfsd_idmap_init(); /* Name to ID mapping */
  371. if (proc_mkdir("fs/nfs", NULL)) {
  372. struct proc_dir_entry *entry;
  373. entry = create_proc_entry("fs/nfs/exports", 0, NULL);
  374. if (entry)
  375. entry->proc_fops = &exports_operations;
  376. }
  377. retval = register_filesystem(&nfsd_fs_type);
  378. if (retval) {
  379. nfsd_export_shutdown();
  380. nfsd_cache_shutdown();
  381. remove_proc_entry("fs/nfs/exports", NULL);
  382. remove_proc_entry("fs/nfs", NULL);
  383. nfsd_stat_shutdown();
  384. nfsd_lockd_shutdown();
  385. }
  386. return retval;
  387. }
  388. static void __exit exit_nfsd(void)
  389. {
  390. nfsd_export_shutdown();
  391. nfsd_cache_shutdown();
  392. remove_proc_entry("fs/nfs/exports", NULL);
  393. remove_proc_entry("fs/nfs", NULL);
  394. nfsd_stat_shutdown();
  395. nfsd_lockd_shutdown();
  396. nfsd_idmap_shutdown();
  397. unregister_filesystem(&nfsd_fs_type);
  398. }
  399. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  400. MODULE_LICENSE("GPL");
  401. module_init(init_nfsd)
  402. module_exit(exit_nfsd)