Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/nfsctl.c |
| 3 | * |
| 4 | * This should eventually move to userland. |
| 5 | * |
| 6 | */ |
Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 7 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Randy Dunlap | 9789cfe | 2008-11-19 11:46:46 -0800 | [diff] [blame] | 13 | #include <linux/cred.h> |
| 14 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
| 22 | * open a file on nfsd fs |
| 23 | */ |
| 24 | |
| 25 | static struct file *do_open(char *name, int flags) |
| 26 | { |
| 27 | struct nameidata nd; |
Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 28 | struct vfsmount *mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | int error; |
| 30 | |
Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 31 | mnt = do_kern_mount("nfsd", 0, "nfsd", NULL); |
| 32 | if (IS_ERR(mnt)) |
| 33 | return (struct file *)mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 35 | error = vfs_path_lookup(mnt->mnt_root, mnt, name, 0, &nd); |
| 36 | mntput(mnt); /* drop do_kern_mount reference */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | if (error) |
| 38 | return ERR_PTR(error); |
| 39 | |
| 40 | if (flags == O_RDWR) |
| 41 | error = may_open(&nd,MAY_READ|MAY_WRITE,FMODE_READ|FMODE_WRITE); |
| 42 | else |
| 43 | error = may_open(&nd, MAY_WRITE, FMODE_WRITE); |
| 44 | |
| 45 | if (!error) |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 46 | return dentry_open(nd.path.dentry, nd.path.mnt, flags, |
| 47 | current_cred()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 49 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return ERR_PTR(error); |
| 51 | } |
| 52 | |
| 53 | static struct { |
| 54 | char *name; int wsize; int rsize; |
| 55 | } map[] = { |
| 56 | [NFSCTL_SVC] = { |
| 57 | .name = ".svc", |
| 58 | .wsize = sizeof(struct nfsctl_svc) |
| 59 | }, |
| 60 | [NFSCTL_ADDCLIENT] = { |
| 61 | .name = ".add", |
| 62 | .wsize = sizeof(struct nfsctl_client) |
| 63 | }, |
| 64 | [NFSCTL_DELCLIENT] = { |
| 65 | .name = ".del", |
| 66 | .wsize = sizeof(struct nfsctl_client) |
| 67 | }, |
| 68 | [NFSCTL_EXPORT] = { |
| 69 | .name = ".export", |
| 70 | .wsize = sizeof(struct nfsctl_export) |
| 71 | }, |
| 72 | [NFSCTL_UNEXPORT] = { |
| 73 | .name = ".unexport", |
| 74 | .wsize = sizeof(struct nfsctl_export) |
| 75 | }, |
| 76 | [NFSCTL_GETFD] = { |
| 77 | .name = ".getfd", |
| 78 | .wsize = sizeof(struct nfsctl_fdparm), |
| 79 | .rsize = NFS_FHSIZE |
| 80 | }, |
| 81 | [NFSCTL_GETFS] = { |
| 82 | .name = ".getfs", |
| 83 | .wsize = sizeof(struct nfsctl_fsparm), |
| 84 | .rsize = sizeof(struct knfsd_fh) |
| 85 | }, |
| 86 | }; |
| 87 | |
| 88 | long |
| 89 | asmlinkage sys_nfsservctl(int cmd, struct nfsctl_arg __user *arg, void __user *res) |
| 90 | { |
| 91 | struct file *file; |
| 92 | void __user *p = &arg->u; |
| 93 | int version; |
| 94 | int err; |
| 95 | |
| 96 | if (copy_from_user(&version, &arg->ca_version, sizeof(int))) |
| 97 | return -EFAULT; |
| 98 | |
Peter Staubach | 85c6932 | 2006-03-16 23:04:02 -0800 | [diff] [blame] | 99 | if (version != NFSCTL_VERSION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 102 | if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return -EINVAL; |
| 104 | |
| 105 | file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY); |
| 106 | if (IS_ERR(file)) |
| 107 | return PTR_ERR(file); |
| 108 | err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos); |
| 109 | if (err >= 0 && map[cmd].rsize) |
| 110 | err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos); |
| 111 | if (err >= 0) |
| 112 | err = 0; |
| 113 | fput(file); |
| 114 | return err; |
| 115 | } |