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, |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 53 | .flags = 0, |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 54 | }; |
| 55 | struct rpc_clnt *mnt_clnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 58 | dprintk("NFS: sending MNT request for %s:%s\n", |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 59 | (info->hostname ? info->hostname : "server"), |
| 60 | info->dirpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Chuck Lever | 3ea9730 | 2007-07-01 12:13:27 -0400 | [diff] [blame] | 62 | mnt_clnt = rpc_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | if (IS_ERR(mnt_clnt)) |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 64 | goto out_clnt_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 66 | if (info->version == NFS_MNT3_VERSION) |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 67 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT]; |
| 68 | else |
| 69 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT]; |
| 70 | |
| 71 | status = rpc_call_sync(mnt_clnt, &msg, 0); |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 72 | rpc_shutdown_client(mnt_clnt); |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 73 | |
| 74 | if (status < 0) |
| 75 | goto out_call_err; |
| 76 | if (result.status != 0) |
| 77 | goto out_mnt_err; |
| 78 | |
| 79 | dprintk("NFS: MNT request succeeded\n"); |
| 80 | status = 0; |
| 81 | |
| 82 | out: |
| 83 | return status; |
| 84 | |
| 85 | out_clnt_err: |
| 86 | status = PTR_ERR(mnt_clnt); |
| 87 | dprintk("NFS: failed to create RPC client, status=%d\n", status); |
| 88 | goto out; |
| 89 | |
| 90 | out_call_err: |
| 91 | dprintk("NFS: failed to start MNT request, status=%d\n", status); |
| 92 | goto out; |
| 93 | |
| 94 | out_mnt_err: |
| 95 | dprintk("NFS: MNT server returned result %d\n", result.status); |
Steve Dickson | 8491945 | 2008-04-11 20:03:06 -0400 | [diff] [blame] | 96 | status = nfs_stat_to_errno(result.status); |
Chuck Lever | 013a8c1 | 2007-07-01 12:13:38 -0400 | [diff] [blame] | 97 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* |
| 101 | * XDR encode/decode functions for MOUNT |
| 102 | */ |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 103 | static int xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p, |
| 104 | const char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | p = xdr_encode_string(p, path); |
| 107 | |
| 108 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 109 | return 0; |
| 110 | } |
| 111 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 112 | static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p, |
| 113 | struct mnt_fhstatus *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | struct nfs_fh *fh = res->fh; |
| 116 | |
| 117 | if ((res->status = ntohl(*p++)) == 0) { |
| 118 | fh->size = NFS2_FHSIZE; |
| 119 | memcpy(fh->data, p, NFS2_FHSIZE); |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 124 | static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p, |
| 125 | struct mnt_fhstatus *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | struct nfs_fh *fh = res->fh; |
Trond Myklebust | b7e2445 | 2008-06-19 15:21:11 -0400 | [diff] [blame] | 128 | unsigned size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | if ((res->status = ntohl(*p++)) == 0) { |
Trond Myklebust | b7e2445 | 2008-06-19 15:21:11 -0400 | [diff] [blame] | 131 | size = ntohl(*p++); |
| 132 | if (size <= NFS3_FHSIZE && size != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | fh->size = size; |
| 134 | memcpy(fh->data, p, size); |
| 135 | } else |
| 136 | res->status = -EBADHANDLE; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | #define MNT_dirpath_sz (1 + 256) |
| 142 | #define MNT_fhstatus_sz (1 + 8) |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 143 | #define MNT_fhstatus3_sz (1 + 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 145 | static struct rpc_procinfo mnt_procedures[] = { |
| 146 | [MNTPROC_MNT] = { |
| 147 | .p_proc = MNTPROC_MNT, |
| 148 | .p_encode = (kxdrproc_t) xdr_encode_dirpath, |
| 149 | .p_decode = (kxdrproc_t) xdr_decode_fhstatus, |
| 150 | .p_arglen = MNT_dirpath_sz, |
| 151 | .p_replen = MNT_fhstatus_sz, |
| 152 | .p_statidx = MNTPROC_MNT, |
| 153 | .p_name = "MOUNT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }, |
| 155 | }; |
| 156 | |
| 157 | static struct rpc_procinfo mnt3_procedures[] = { |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 158 | [MOUNTPROC3_MNT] = { |
| 159 | .p_proc = MOUNTPROC3_MNT, |
| 160 | .p_encode = (kxdrproc_t) xdr_encode_dirpath, |
| 161 | .p_decode = (kxdrproc_t) xdr_decode_fhstatus3, |
| 162 | .p_arglen = MNT_dirpath_sz, |
| 163 | .p_replen = MNT_fhstatus3_sz, |
| 164 | .p_statidx = MOUNTPROC3_MNT, |
| 165 | .p_name = "MOUNT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | }, |
| 167 | }; |
| 168 | |
| 169 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 170 | static struct rpc_version mnt_version1 = { |
| 171 | .number = 1, |
| 172 | .nrprocs = 2, |
| 173 | .procs = mnt_procedures, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 176 | static struct rpc_version mnt_version3 = { |
| 177 | .number = 3, |
| 178 | .nrprocs = 2, |
| 179 | .procs = mnt3_procedures, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 182 | static struct rpc_version *mnt_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | NULL, |
| 184 | &mnt_version1, |
| 185 | NULL, |
| 186 | &mnt_version3, |
| 187 | }; |
| 188 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 189 | static struct rpc_stat mnt_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Chuck Lever | 1920723 | 2007-07-01 12:13:33 -0400 | [diff] [blame] | 191 | static struct rpc_program mnt_program = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | .name = "mount", |
| 193 | .number = NFS_MNT_PROGRAM, |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 194 | .nrvers = ARRAY_SIZE(mnt_version), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | .version = mnt_version, |
| 196 | .stats = &mnt_stats, |
| 197 | }; |