nfsctl.c 2.4 KB

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