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