nfsctl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, flags);
  35. else
  36. error = may_open(&nd.path, MAY_WRITE, flags);
  37. if (!error)
  38. return dentry_open(nd.path.dentry, nd.path.mnt, flags,
  39. current_cred());
  40. path_put(&nd.path);
  41. return ERR_PTR(error);
  42. }
  43. static struct {
  44. char *name; int wsize; int rsize;
  45. } map[] = {
  46. [NFSCTL_SVC] = {
  47. .name = ".svc",
  48. .wsize = sizeof(struct nfsctl_svc)
  49. },
  50. [NFSCTL_ADDCLIENT] = {
  51. .name = ".add",
  52. .wsize = sizeof(struct nfsctl_client)
  53. },
  54. [NFSCTL_DELCLIENT] = {
  55. .name = ".del",
  56. .wsize = sizeof(struct nfsctl_client)
  57. },
  58. [NFSCTL_EXPORT] = {
  59. .name = ".export",
  60. .wsize = sizeof(struct nfsctl_export)
  61. },
  62. [NFSCTL_UNEXPORT] = {
  63. .name = ".unexport",
  64. .wsize = sizeof(struct nfsctl_export)
  65. },
  66. [NFSCTL_GETFD] = {
  67. .name = ".getfd",
  68. .wsize = sizeof(struct nfsctl_fdparm),
  69. .rsize = NFS_FHSIZE
  70. },
  71. [NFSCTL_GETFS] = {
  72. .name = ".getfs",
  73. .wsize = sizeof(struct nfsctl_fsparm),
  74. .rsize = sizeof(struct knfsd_fh)
  75. },
  76. };
  77. SYSCALL_DEFINE3(nfsservctl, int, cmd, struct nfsctl_arg __user *, arg,
  78. void __user *, res)
  79. {
  80. struct file *file;
  81. void __user *p = &arg->u;
  82. int version;
  83. int err;
  84. if (copy_from_user(&version, &arg->ca_version, sizeof(int)))
  85. return -EFAULT;
  86. if (version != NFSCTL_VERSION)
  87. return -EINVAL;
  88. if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name)
  89. return -EINVAL;
  90. file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);
  91. if (IS_ERR(file))
  92. return PTR_ERR(file);
  93. err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos);
  94. if (err >= 0 && map[cmd].rsize)
  95. err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos);
  96. if (err >= 0)
  97. err = 0;
  98. fput(file);
  99. return err;
  100. }