nfsctl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * fs/nfsctl.c
  3. *
  4. * This should eventually move to userland.
  5. *
  6. */
  7. #include <linux/config.h>
  8. #include <linux/types.h>
  9. #include <linux/file.h>
  10. #include <linux/fs.h>
  11. #include <linux/sunrpc/svc.h>
  12. #include <linux/nfsd/nfsd.h>
  13. #include <linux/nfsd/syscall.h>
  14. #include <linux/linkage.h>
  15. #include <linux/namei.h>
  16. #include <linux/mount.h>
  17. #include <linux/syscalls.h>
  18. #include <asm/uaccess.h>
  19. /*
  20. * open a file on nfsd fs
  21. */
  22. static struct file *do_open(char *name, int flags)
  23. {
  24. struct nameidata nd;
  25. int error;
  26. nd.mnt = do_kern_mount("nfsd", 0, "nfsd", NULL);
  27. if (IS_ERR(nd.mnt))
  28. return (struct file *)nd.mnt;
  29. nd.dentry = dget(nd.mnt->mnt_root);
  30. nd.last_type = LAST_ROOT;
  31. nd.flags = 0;
  32. nd.depth = 0;
  33. error = path_walk(name, &nd);
  34. if (error)
  35. return ERR_PTR(error);
  36. if (flags == O_RDWR)
  37. error = may_open(&nd,MAY_READ|MAY_WRITE,FMODE_READ|FMODE_WRITE);
  38. else
  39. error = may_open(&nd, MAY_WRITE, FMODE_WRITE);
  40. if (!error)
  41. return dentry_open(nd.dentry, nd.mnt, flags);
  42. path_release(&nd);
  43. return ERR_PTR(error);
  44. }
  45. static struct {
  46. char *name; int wsize; int rsize;
  47. } map[] = {
  48. [NFSCTL_SVC] = {
  49. .name = ".svc",
  50. .wsize = sizeof(struct nfsctl_svc)
  51. },
  52. [NFSCTL_ADDCLIENT] = {
  53. .name = ".add",
  54. .wsize = sizeof(struct nfsctl_client)
  55. },
  56. [NFSCTL_DELCLIENT] = {
  57. .name = ".del",
  58. .wsize = sizeof(struct nfsctl_client)
  59. },
  60. [NFSCTL_EXPORT] = {
  61. .name = ".export",
  62. .wsize = sizeof(struct nfsctl_export)
  63. },
  64. [NFSCTL_UNEXPORT] = {
  65. .name = ".unexport",
  66. .wsize = sizeof(struct nfsctl_export)
  67. },
  68. [NFSCTL_GETFD] = {
  69. .name = ".getfd",
  70. .wsize = sizeof(struct nfsctl_fdparm),
  71. .rsize = NFS_FHSIZE
  72. },
  73. [NFSCTL_GETFS] = {
  74. .name = ".getfs",
  75. .wsize = sizeof(struct nfsctl_fsparm),
  76. .rsize = sizeof(struct knfsd_fh)
  77. },
  78. };
  79. long
  80. asmlinkage sys_nfsservctl(int cmd, struct nfsctl_arg __user *arg, void __user *res)
  81. {
  82. struct file *file;
  83. void __user *p = &arg->u;
  84. int version;
  85. int err;
  86. if (copy_from_user(&version, &arg->ca_version, sizeof(int)))
  87. return -EFAULT;
  88. if (version != NFSCTL_VERSION) {
  89. printk(KERN_WARNING "nfsd: incompatible version in syscall.\n");
  90. return -EINVAL;
  91. }
  92. if (cmd < 0 || cmd >= sizeof(map)/sizeof(map[0]) || !map[cmd].name)
  93. return -EINVAL;
  94. file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);
  95. if (IS_ERR(file))
  96. return PTR_ERR(file);
  97. err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos);
  98. if (err >= 0 && map[cmd].rsize)
  99. err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos);
  100. if (err >= 0)
  101. err = 0;
  102. fput(file);
  103. return err;
  104. }