nfsctl.c 12 KB

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