nfsctl.c 11 KB

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