nfsctl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * fs/nfsctl.c
  3. *
  4. * This should eventually move to userland.
  5. *
  6. */
  7. #include <linux/types.h>
  8. #include <linux/file.h>
  9. #include <linux/fs.h>
  10. #include <linux/sunrpc/svc.h>
  11. #include <linux/nfsd/nfsd.h>
  12. #include <linux/nfsd/syscall.h>
  13. #include <linux/cred.h>
  14. #include <linux/sched.h>
  15. #include <linux/linkage.h>
  16. #include <linux/namei.h>
  17. #include <linux/mount.h>
  18. #include <linux/syscalls.h>
  19. #include <asm/uaccess.h>
  20. /*
  21. * open a file on nfsd fs
  22. */
  23. static struct file *do_open(char *name, int flags)
  24. {
  25. struct nameidata nd;
  26. struct vfsmount *mnt;
  27. int error;
  28. mnt = do_kern_mount("nfsd", 0, "nfsd", NULL);
  29. if (IS_ERR(mnt))
  30. return (struct file *)mnt;
  31. error = vfs_path_lookup(mnt->mnt_root, mnt, name, 0, &nd);
  32. mntput(mnt); /* drop do_kern_mount reference */
  33. if (error)
  34. return ERR_PTR(error);
  35. if (flags == O_RDWR)
  36. error = may_open(&nd,MAY_READ|MAY_WRITE,FMODE_READ|FMODE_WRITE);
  37. else
  38. error = may_open(&nd, MAY_WRITE, FMODE_WRITE);
  39. if (!error)
  40. return dentry_open(nd.path.dentry, nd.path.mnt, flags,
  41. current_cred());
  42. path_put(&nd.path);
  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. return -EINVAL;
  90. if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name)
  91. return -EINVAL;
  92. file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);
  93. if (IS_ERR(file))
  94. return PTR_ERR(file);
  95. err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos);
  96. if (err >= 0 && map[cmd].rsize)
  97. err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos);
  98. if (err >= 0)
  99. err = 0;
  100. fput(file);
  101. return err;
  102. }