nfsctl.c 13 KB

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