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