Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 2 | * In-kernel MOUNT protocol client |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/socket.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/uio.h> |
| 12 | #include <linux/net.h> |
| 13 | #include <linux/in.h> |
| 14 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sunrpc/sched.h> |
| 16 | #include <linux/nfs_fs.h> |
Steve Dickson | 8491945 | 2008-04-11 20:03:06 -0400 | [diff] [blame] | 17 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #ifdef RPC_DEBUG |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 20 | # define NFSDBG_FACILITY NFSDBG_MOUNT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #endif |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | static struct rpc_program mnt_program; |
| 24 | |
| 25 | struct mnt_fhstatus { |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 26 | u32 status; |
| 27 | struct nfs_fh *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 30 | /** |
| 31 | * nfs_mount - Obtain an NFS file handle for the given host and path |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 32 | * @info: pointer to mount request arguments |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 33 | * |
| 34 | * Uses default timeout parameters specified by underlying transport. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 36 | int nfs_mount(struct nfs_mount_request *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct mnt_fhstatus result = { |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 39 | .fh = info->fh |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | }; |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 41 | struct rpc_message msg = { |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 42 | .rpc_argp = info->dirpath, |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 43 | .rpc_resp = &result, |
| 44 | }; |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 45 | struct rpc_create_args args = { |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 46 | .protocol = info->protocol, |
| 47 | .address = info->sap, |
| 48 | .addrsize = info->salen, |
| 49 | .servername = info->hostname, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 50 | .program = &mnt_program, |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 51 | .version = info->version, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 52 | .authflavor = RPC_AUTH_UNIX, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 53 | }; |
| 54 | struct rpc_clnt *mnt_clnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 57 | dprintk("NFS: sending MNT request for %s:%s\n", |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 58 | (info->hostname ? info->hostname : "server"), |
| 59 | info->dirpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Chuck Lever | 50a737f | 2008-12-23 15:21:37 -0500 | [diff] [blame] | 61 | if (info->noresvport) |
| 62 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; |
| 63 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 64 | mnt_clnt = rpc_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (IS_ERR(mnt_clnt)) |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 66 | goto out_clnt_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 68 | if (info->version == NFS_MNT3_VERSION) |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 69 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT]; |
| 70 | else |
| 71 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT]; |
| 72 | |
| 73 | status = rpc_call_sync(mnt_clnt, &msg, 0); |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 74 | rpc_shutdown_client(mnt_clnt); |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 75 | |
| 76 | if (status < 0) |
| 77 | goto out_call_err; |
| 78 | if (result.status != 0) |
| 79 | goto out_mnt_err; |
| 80 | |
| 81 | dprintk("NFS: MNT request succeeded\n"); |
| 82 | status = 0; |
| 83 | |
| 84 | out: |
| 85 | return status; |
| 86 | |
| 87 | out_clnt_err: |
| 88 | status = PTR_ERR(mnt_clnt); |
| 89 | dprintk("NFS: failed to create RPC client, status=%d\n", status); |
| 90 | goto out; |
| 91 | |
| 92 | out_call_err: |
| 93 | dprintk("NFS: failed to start MNT request, status=%d\n", status); |
| 94 | goto out; |
| 95 | |
| 96 | out_mnt_err: |
| 97 | dprintk("NFS: MNT server returned result %d\n", result.status); |
Steve Dickson | 8491945 | 2008-04-11 20:03:06 -0400 | [diff] [blame] | 98 | status = nfs_stat_to_errno(result.status); |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 99 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* |
| 103 | * XDR encode/decode functions for MOUNT |
| 104 | */ |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 105 | static int xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p, |
| 106 | const char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | p = xdr_encode_string(p, path); |
| 109 | |
| 110 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 114 | static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p, |
| 115 | struct mnt_fhstatus *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | struct nfs_fh *fh = res->fh; |
| 118 | |
| 119 | if ((res->status = ntohl(*p++)) == 0) { |
| 120 | fh->size = NFS2_FHSIZE; |
| 121 | memcpy(fh->data, p, NFS2_FHSIZE); |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 126 | static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p, |
| 127 | struct mnt_fhstatus *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | struct nfs_fh *fh = res->fh; |
Trond Myklebust | b7e2445 | 2008-06-19 15:21:11 -0400 | [diff] [blame] | 130 | unsigned size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | if ((res->status = ntohl(*p++)) == 0) { |
Trond Myklebust | b7e2445 | 2008-06-19 15:21:11 -0400 | [diff] [blame] | 133 | size = ntohl(*p++); |
| 134 | if (size <= NFS3_FHSIZE && size != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | fh->size = size; |
| 136 | memcpy(fh->data, p, size); |
| 137 | } else |
| 138 | res->status = -EBADHANDLE; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | #define MNT_dirpath_sz (1 + 256) |
| 144 | #define MNT_fhstatus_sz (1 + 8) |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 145 | #define MNT_fhstatus3_sz (1 + 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 147 | static struct rpc_procinfo mnt_procedures[] = { |
| 148 | [MNTPROC_MNT] = { |
| 149 | .p_proc = MNTPROC_MNT, |
| 150 | .p_encode = (kxdrproc_t) xdr_encode_dirpath, |
| 151 | .p_decode = (kxdrproc_t) xdr_decode_fhstatus, |
| 152 | .p_arglen = MNT_dirpath_sz, |
| 153 | .p_replen = MNT_fhstatus_sz, |
| 154 | .p_statidx = MNTPROC_MNT, |
| 155 | .p_name = "MOUNT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | }, |
| 157 | }; |
| 158 | |
| 159 | static struct rpc_procinfo mnt3_procedures[] = { |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 160 | [MOUNTPROC3_MNT] = { |
| 161 | .p_proc = MOUNTPROC3_MNT, |
| 162 | .p_encode = (kxdrproc_t) xdr_encode_dirpath, |
| 163 | .p_decode = (kxdrproc_t) xdr_decode_fhstatus3, |
| 164 | .p_arglen = MNT_dirpath_sz, |
| 165 | .p_replen = MNT_fhstatus3_sz, |
| 166 | .p_statidx = MOUNTPROC3_MNT, |
| 167 | .p_name = "MOUNT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | }, |
| 169 | }; |
| 170 | |
| 171 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 172 | static struct rpc_version mnt_version1 = { |
| 173 | .number = 1, |
| 174 | .nrprocs = 2, |
| 175 | .procs = mnt_procedures, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 178 | static struct rpc_version mnt_version3 = { |
| 179 | .number = 3, |
| 180 | .nrprocs = 2, |
| 181 | .procs = mnt3_procedures, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 184 | static struct rpc_version *mnt_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | NULL, |
| 186 | &mnt_version1, |
| 187 | NULL, |
| 188 | &mnt_version3, |
| 189 | }; |
| 190 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 191 | static struct rpc_stat mnt_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 193 | static struct rpc_program mnt_program = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | .name = "mount", |
| 195 | .number = NFS_MNT_PROGRAM, |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 196 | .nrvers = ARRAY_SIZE(mnt_version), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | .version = mnt_version, |
| 198 | .stats = &mnt_stats, |
| 199 | }; |