blob: ca905a5bb1ba7457270953e18e06cd2b3b7596de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Chuck Lever19207232007-07-01 12:13:33 -04002 * In-kernel MOUNT protocol client
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/sched.h>
16#include <linux/nfs_fs.h>
Steve Dickson84919452008-04-11 20:03:06 -040017#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#ifdef RPC_DEBUG
Chuck Lever3ea97302007-07-01 12:13:27 -040020# define NFSDBG_FACILITY NFSDBG_MOUNT
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#endif
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static struct rpc_program mnt_program;
24
25struct mnt_fhstatus {
Chuck Lever19207232007-07-01 12:13:33 -040026 u32 status;
27 struct nfs_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
29
Chuck Lever3ea97302007-07-01 12:13:27 -040030/**
31 * nfs_mount - Obtain an NFS file handle for the given host and path
Chuck Leverc5d120f2008-12-23 15:21:35 -050032 * @info: pointer to mount request arguments
Chuck Lever3ea97302007-07-01 12:13:27 -040033 *
34 * Uses default timeout parameters specified by underlying transport.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
Chuck Leverc5d120f2008-12-23 15:21:35 -050036int nfs_mount(struct nfs_mount_request *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct mnt_fhstatus result = {
Chuck Leverc5d120f2008-12-23 15:21:35 -050039 .fh = info->fh
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 };
Chuck Leverdead28d2006-03-20 13:44:23 -050041 struct rpc_message msg = {
Chuck Leverc5d120f2008-12-23 15:21:35 -050042 .rpc_argp = info->dirpath,
Chuck Leverdead28d2006-03-20 13:44:23 -050043 .rpc_resp = &result,
44 };
Chuck Lever3ea97302007-07-01 12:13:27 -040045 struct rpc_create_args args = {
Chuck Leverc5d120f2008-12-23 15:21:35 -050046 .protocol = info->protocol,
47 .address = info->sap,
48 .addrsize = info->salen,
49 .servername = info->hostname,
Chuck Lever3ea97302007-07-01 12:13:27 -040050 .program = &mnt_program,
Chuck Leverc5d120f2008-12-23 15:21:35 -050051 .version = info->version,
Chuck Lever3ea97302007-07-01 12:13:27 -040052 .authflavor = RPC_AUTH_UNIX,
Chuck Lever3ea97302007-07-01 12:13:27 -040053 };
54 struct rpc_clnt *mnt_clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Chuck Lever3ea97302007-07-01 12:13:27 -040057 dprintk("NFS: sending MNT request for %s:%s\n",
Chuck Leverc5d120f2008-12-23 15:21:35 -050058 (info->hostname ? info->hostname : "server"),
59 info->dirpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Chuck Lever50a737f2008-12-23 15:21:37 -050061 if (info->noresvport)
62 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
63
Chuck Lever3ea97302007-07-01 12:13:27 -040064 mnt_clnt = rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (IS_ERR(mnt_clnt))
Chuck Lever013a8c12007-07-01 12:13:38 -040066 goto out_clnt_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Chuck Leverc5d120f2008-12-23 15:21:35 -050068 if (info->version == NFS_MNT3_VERSION)
Chuck Leverdead28d2006-03-20 13:44:23 -050069 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 Myklebust90c57552007-06-09 19:49:36 -040074 rpc_shutdown_client(mnt_clnt);
Chuck Lever013a8c12007-07-01 12:13:38 -040075
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
84out:
85 return status;
86
87out_clnt_err:
88 status = PTR_ERR(mnt_clnt);
89 dprintk("NFS: failed to create RPC client, status=%d\n", status);
90 goto out;
91
92out_call_err:
93 dprintk("NFS: failed to start MNT request, status=%d\n", status);
94 goto out;
95
96out_mnt_err:
97 dprintk("NFS: MNT server returned result %d\n", result.status);
Steve Dickson84919452008-04-11 20:03:06 -040098 status = nfs_stat_to_errno(result.status);
Chuck Lever013a8c12007-07-01 12:13:38 -040099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * XDR encode/decode functions for MOUNT
104 */
Chuck Lever19207232007-07-01 12:13:33 -0400105static int xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p,
106 const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
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 Lever19207232007-07-01 12:13:33 -0400114static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p,
115 struct mnt_fhstatus *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
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 Lever19207232007-07-01 12:13:33 -0400126static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p,
127 struct mnt_fhstatus *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct nfs_fh *fh = res->fh;
Trond Myklebustb7e24452008-06-19 15:21:11 -0400130 unsigned size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if ((res->status = ntohl(*p++)) == 0) {
Trond Myklebustb7e24452008-06-19 15:21:11 -0400133 size = ntohl(*p++);
134 if (size <= NFS3_FHSIZE && size != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 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 Lever2bea90d2007-03-29 16:47:53 -0400145#define MNT_fhstatus3_sz (1 + 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Chuck Lever19207232007-07-01 12:13:33 -0400147static 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 Torvalds1da177e2005-04-16 15:20:36 -0700156 },
157};
158
159static struct rpc_procinfo mnt3_procedures[] = {
Chuck Lever19207232007-07-01 12:13:33 -0400160 [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 Torvalds1da177e2005-04-16 15:20:36 -0700168 },
169};
170
171
Chuck Lever19207232007-07-01 12:13:33 -0400172static struct rpc_version mnt_version1 = {
173 .number = 1,
174 .nrprocs = 2,
175 .procs = mnt_procedures,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Chuck Lever19207232007-07-01 12:13:33 -0400178static struct rpc_version mnt_version3 = {
179 .number = 3,
180 .nrprocs = 2,
181 .procs = mnt3_procedures,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Chuck Lever19207232007-07-01 12:13:33 -0400184static struct rpc_version *mnt_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 NULL,
186 &mnt_version1,
187 NULL,
188 &mnt_version3,
189};
190
Chuck Lever19207232007-07-01 12:13:33 -0400191static struct rpc_stat mnt_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Chuck Lever19207232007-07-01 12:13:33 -0400193static struct rpc_program mnt_program = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .name = "mount",
195 .number = NFS_MNT_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800196 .nrvers = ARRAY_SIZE(mnt_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .version = mnt_version,
198 .stats = &mnt_stats,
199};