nfsctl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/nfsd/syscall.h>
  11. #include <linux/cred.h>
  12. #include <linux/sched.h>
  13. #include <linux/linkage.h>
  14. #include <linux/namei.h>
  15. #include <linux/mount.h>
  16. #include <linux/syscalls.h>
  17. #include <asm/uaccess.h>
  18. /*
  19. * open a file on nfsd fs
  20. */
  21. static struct file *do_open(char *name, int flags)
  22. {
  23. struct nameidata nd;
  24. struct vfsmount *mnt;
  25. int error;
  26. mnt = do_kern_mount("nfsd", 0, "nfsd", NULL);
  27. if (IS_ERR(mnt))
  28. return (struct file *)mnt;
  29. error = vfs_path_lookup(mnt->mnt_root, mnt, name, 0, &nd);
  30. mntput(mnt); /* drop do_kern_mount reference */
  31. if (error)
  32. return ERR_PTR(error);
  33. if (flags == O_RDWR)
  34. error = may_open(&nd.path, MAY_READ|MAY_WRITE,
  35. FMODE_READ|FMODE_WRITE);
  36. else
  37. error = may_open(&nd.path, MAY_WRITE, FMODE_WRITE);
  38. if (!error)
  39. return dentry_open(nd.path.dentry, nd.path.mnt, flags,
  40. current_cred());
  41. path_put(&nd.path);
  42. return ERR_PTR(error);
  43. }
  44. static struct {
  45. char *name; int wsize; int rsize;
  46. } map[] = {
  47. [NFSCTL_SVC] = {
  48. .name = ".svc",
  49. .wsize = sizeof(struct nfsctl_svc)
  50. },
  51. [NFSCTL_ADDCLIENT] = {
  52. .name = ".add",
  53. .wsize = sizeof(struct nfsctl_client)
  54. },
  55. [NFSCTL_DELCLIENT] = {
  56. .name = ".del",
  57. .wsize = sizeof(struct nfsctl_client)
  58. },
  59. [NFSCTL_EXPORT] = {
  60. .name = ".export",
  61. .wsize = sizeof(struct nfsctl_export)
  62. },
  63. [NFSCTL_UNEXPORT] = {
  64. .name = ".unexport",
  65. .wsize = sizeof(struct nfsctl_export)
  66. },
  67. [NFSCTL_GETFD] = {
  68. .name = ".getfd",
  69. .wsize = sizeof(struct nfsctl_fdparm),
  70. .rsize = NFS_FHSIZE
  71. },
  72. [NFSCTL_GETFS] = {
  73. .name = ".getfs",
  74. .wsize = sizeof(struct nfsctl_fsparm),
  75. .rsize = sizeof(struct knfsd_fh)
  76. },
  77. };
  78. SYSCALL_DEFINE3(nfsservctl, int, cmd, struct nfsctl_arg __user *, arg,
  79. void __user *, res)
  80. {
  81. struct file *file;
  82. void __user *p = &arg->u;
  83. int version;
  84. int err;
  85. if (copy_from_user(&version, &arg->ca_version, sizeof(int)))
  86. return -EFAULT;
  87. if (version != NFSCTL_VERSION)
  88. return -EINVAL;
  89. if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name)
  90. return -EINVAL;
  91. file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);
  92. if (IS_ERR(file))
  93. return PTR_ERR(file);
  94. err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos);
  95. if (err >= 0 && map[cmd].rsize)
  96. err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos);
  97. if (err >= 0)
  98. err = 0;
  99. fput(file);
  100. return err;
  101. }