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 | |
Chuck Lever | 2ad7809 | 2009-06-17 18:02:11 -0700 | [diff] [blame] | 23 | /* |
Chuck Lever | 29a1bd6 | 2009-06-17 18:02:11 -0700 | [diff] [blame^] | 24 | * Defined by RFC 1094, section A.3; and RFC 1813, section 5.1.4 |
| 25 | */ |
| 26 | #define MNTPATHLEN (1024) |
| 27 | |
| 28 | /* |
| 29 | * XDR data type sizes |
| 30 | */ |
| 31 | #define encode_dirpath_sz (1 + XDR_QUADLEN(MNTPATHLEN)) |
| 32 | |
| 33 | /* |
| 34 | * XDR argument and result sizes |
| 35 | */ |
| 36 | #define MNT_enc_dirpath_sz encode_dirpath_sz |
| 37 | |
| 38 | /* |
Chuck Lever | 2ad7809 | 2009-06-17 18:02:11 -0700 | [diff] [blame] | 39 | * Defined by RFC 1094, section A.5 |
| 40 | */ |
| 41 | enum { |
| 42 | MOUNTPROC_NULL = 0, |
| 43 | MOUNTPROC_MNT = 1, |
| 44 | MOUNTPROC_DUMP = 2, |
| 45 | MOUNTPROC_UMNT = 3, |
| 46 | MOUNTPROC_UMNTALL = 4, |
| 47 | MOUNTPROC_EXPORT = 5, |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * Defined by RFC 1813, section 5.2 |
| 52 | */ |
| 53 | enum { |
| 54 | MOUNTPROC3_NULL = 0, |
| 55 | MOUNTPROC3_MNT = 1, |
| 56 | MOUNTPROC3_DUMP = 2, |
| 57 | MOUNTPROC3_UMNT = 3, |
| 58 | MOUNTPROC3_UMNTALL = 4, |
| 59 | MOUNTPROC3_EXPORT = 5, |
| 60 | }; |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static struct rpc_program mnt_program; |
| 63 | |
| 64 | struct mnt_fhstatus { |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 65 | u32 status; |
| 66 | struct nfs_fh *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 69 | /** |
| 70 | * 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] | 71 | * @info: pointer to mount request arguments |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 72 | * |
| 73 | * Uses default timeout parameters specified by underlying transport. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 75 | int nfs_mount(struct nfs_mount_request *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct mnt_fhstatus result = { |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 78 | .fh = info->fh |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | }; |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 80 | struct rpc_message msg = { |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 81 | .rpc_argp = info->dirpath, |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 82 | .rpc_resp = &result, |
| 83 | }; |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 84 | struct rpc_create_args args = { |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 85 | .protocol = info->protocol, |
| 86 | .address = info->sap, |
| 87 | .addrsize = info->salen, |
| 88 | .servername = info->hostname, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 89 | .program = &mnt_program, |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 90 | .version = info->version, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 91 | .authflavor = RPC_AUTH_UNIX, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 92 | }; |
| 93 | struct rpc_clnt *mnt_clnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 96 | dprintk("NFS: sending MNT request for %s:%s\n", |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 97 | (info->hostname ? info->hostname : "server"), |
| 98 | info->dirpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Chuck Lever | 50a737f | 2008-12-23 15:21:37 -0500 | [diff] [blame] | 100 | if (info->noresvport) |
| 101 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; |
| 102 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 103 | mnt_clnt = rpc_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (IS_ERR(mnt_clnt)) |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 105 | goto out_clnt_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 107 | if (info->version == NFS_MNT3_VERSION) |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 108 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT]; |
| 109 | else |
Chuck Lever | 2ad7809 | 2009-06-17 18:02:11 -0700 | [diff] [blame] | 110 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT]; |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 111 | |
| 112 | status = rpc_call_sync(mnt_clnt, &msg, 0); |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 113 | rpc_shutdown_client(mnt_clnt); |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 114 | |
| 115 | if (status < 0) |
| 116 | goto out_call_err; |
| 117 | if (result.status != 0) |
| 118 | goto out_mnt_err; |
| 119 | |
| 120 | dprintk("NFS: MNT request succeeded\n"); |
| 121 | status = 0; |
| 122 | |
| 123 | out: |
| 124 | return status; |
| 125 | |
| 126 | out_clnt_err: |
| 127 | status = PTR_ERR(mnt_clnt); |
| 128 | dprintk("NFS: failed to create RPC client, status=%d\n", status); |
| 129 | goto out; |
| 130 | |
| 131 | out_call_err: |
| 132 | dprintk("NFS: failed to start MNT request, status=%d\n", status); |
| 133 | goto out; |
| 134 | |
| 135 | out_mnt_err: |
| 136 | dprintk("NFS: MNT server returned result %d\n", result.status); |
Steve Dickson | 8491945 | 2008-04-11 20:03:06 -0400 | [diff] [blame] | 137 | status = nfs_stat_to_errno(result.status); |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 138 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | /* |
| 142 | * XDR encode/decode functions for MOUNT |
| 143 | */ |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 144 | static int xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p, |
| 145 | const char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | p = xdr_encode_string(p, path); |
| 148 | |
| 149 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 150 | return 0; |
| 151 | } |
| 152 | |
Chuck Lever | 29a1bd6 | 2009-06-17 18:02:11 -0700 | [diff] [blame^] | 153 | static int encode_mntdirpath(struct xdr_stream *xdr, const char *pathname) |
| 154 | { |
| 155 | const u32 pathname_len = strlen(pathname); |
| 156 | __be32 *p; |
| 157 | |
| 158 | if (unlikely(pathname_len > MNTPATHLEN)) |
| 159 | return -EIO; |
| 160 | |
| 161 | p = xdr_reserve_space(xdr, sizeof(u32) + pathname_len); |
| 162 | if (unlikely(p == NULL)) |
| 163 | return -EIO; |
| 164 | xdr_encode_opaque(p, pathname, pathname_len); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int mnt_enc_dirpath(struct rpc_rqst *req, __be32 *p, |
| 170 | const char *dirpath) |
| 171 | { |
| 172 | struct xdr_stream xdr; |
| 173 | |
| 174 | xdr_init_encode(&xdr, &req->rq_snd_buf, p); |
| 175 | return encode_mntdirpath(&xdr, dirpath); |
| 176 | } |
| 177 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 178 | static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p, |
| 179 | struct mnt_fhstatus *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | struct nfs_fh *fh = res->fh; |
| 182 | |
| 183 | if ((res->status = ntohl(*p++)) == 0) { |
| 184 | fh->size = NFS2_FHSIZE; |
| 185 | memcpy(fh->data, p, NFS2_FHSIZE); |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 190 | static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p, |
| 191 | struct mnt_fhstatus *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct nfs_fh *fh = res->fh; |
Trond Myklebust | b7e2445 | 2008-06-19 15:21:11 -0400 | [diff] [blame] | 194 | unsigned size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | if ((res->status = ntohl(*p++)) == 0) { |
Trond Myklebust | b7e2445 | 2008-06-19 15:21:11 -0400 | [diff] [blame] | 197 | size = ntohl(*p++); |
| 198 | if (size <= NFS3_FHSIZE && size != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | fh->size = size; |
| 200 | memcpy(fh->data, p, size); |
| 201 | } else |
| 202 | res->status = -EBADHANDLE; |
| 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | #define MNT_fhstatus_sz (1 + 8) |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 208 | #define MNT_fhstatus3_sz (1 + 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 210 | static struct rpc_procinfo mnt_procedures[] = { |
Chuck Lever | 2ad7809 | 2009-06-17 18:02:11 -0700 | [diff] [blame] | 211 | [MOUNTPROC_MNT] = { |
| 212 | .p_proc = MOUNTPROC_MNT, |
Chuck Lever | 29a1bd6 | 2009-06-17 18:02:11 -0700 | [diff] [blame^] | 213 | .p_encode = (kxdrproc_t)mnt_enc_dirpath, |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 214 | .p_decode = (kxdrproc_t) xdr_decode_fhstatus, |
Chuck Lever | 29a1bd6 | 2009-06-17 18:02:11 -0700 | [diff] [blame^] | 215 | .p_arglen = MNT_enc_dirpath_sz, |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 216 | .p_replen = MNT_fhstatus_sz, |
Chuck Lever | 2ad7809 | 2009-06-17 18:02:11 -0700 | [diff] [blame] | 217 | .p_statidx = MOUNTPROC_MNT, |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 218 | .p_name = "MOUNT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | }, |
| 220 | }; |
| 221 | |
| 222 | static struct rpc_procinfo mnt3_procedures[] = { |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 223 | [MOUNTPROC3_MNT] = { |
| 224 | .p_proc = MOUNTPROC3_MNT, |
Chuck Lever | 29a1bd6 | 2009-06-17 18:02:11 -0700 | [diff] [blame^] | 225 | .p_encode = (kxdrproc_t)mnt_enc_dirpath, |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 226 | .p_decode = (kxdrproc_t) xdr_decode_fhstatus3, |
Chuck Lever | 29a1bd6 | 2009-06-17 18:02:11 -0700 | [diff] [blame^] | 227 | .p_arglen = MNT_enc_dirpath_sz, |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 228 | .p_replen = MNT_fhstatus3_sz, |
| 229 | .p_statidx = MOUNTPROC3_MNT, |
| 230 | .p_name = "MOUNT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | }, |
| 232 | }; |
| 233 | |
| 234 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 235 | static struct rpc_version mnt_version1 = { |
| 236 | .number = 1, |
| 237 | .nrprocs = 2, |
| 238 | .procs = mnt_procedures, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 241 | static struct rpc_version mnt_version3 = { |
| 242 | .number = 3, |
| 243 | .nrprocs = 2, |
| 244 | .procs = mnt3_procedures, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 247 | static struct rpc_version *mnt_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | NULL, |
| 249 | &mnt_version1, |
| 250 | NULL, |
| 251 | &mnt_version3, |
| 252 | }; |
| 253 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 254 | static struct rpc_stat mnt_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 256 | static struct rpc_program mnt_program = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | .name = "mount", |
| 258 | .number = NFS_MNT_PROGRAM, |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 259 | .nrvers = ARRAY_SIZE(mnt_version), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | .version = mnt_version, |
| 261 | .stats = &mnt_stats, |
| 262 | }; |