| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * linux/fs/nfs/mount_clnt.c | 
|  | 3 | * | 
|  | 4 | * MOUNT client to support NFSroot. | 
|  | 5 | * | 
|  | 6 | * Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de> | 
|  | 7 | */ | 
|  | 8 |  | 
|  | 9 | #include <linux/types.h> | 
|  | 10 | #include <linux/socket.h> | 
|  | 11 | #include <linux/kernel.h> | 
|  | 12 | #include <linux/errno.h> | 
|  | 13 | #include <linux/uio.h> | 
|  | 14 | #include <linux/net.h> | 
|  | 15 | #include <linux/in.h> | 
|  | 16 | #include <linux/sunrpc/clnt.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/sunrpc/sched.h> | 
|  | 18 | #include <linux/nfs_fs.h> | 
|  | 19 |  | 
|  | 20 | #ifdef RPC_DEBUG | 
|  | 21 | # define NFSDBG_FACILITY	NFSDBG_ROOT | 
|  | 22 | #endif | 
|  | 23 |  | 
|  | 24 | /* | 
|  | 25 | #define MOUNT_PROGRAM		100005 | 
|  | 26 | #define MOUNT_VERSION		1 | 
|  | 27 | #define MOUNT_MNT		1 | 
|  | 28 | #define MOUNT_UMNT		3 | 
|  | 29 | */ | 
|  | 30 |  | 
| Chuck Lever | 43780b8 | 2007-07-01 12:13:22 -0400 | [diff] [blame^] | 31 | static struct rpc_clnt *	mnt_create(struct sockaddr_in *, int, int); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static struct rpc_program	mnt_program; | 
|  | 33 |  | 
|  | 34 | struct mnt_fhstatus { | 
|  | 35 | unsigned int		status; | 
|  | 36 | struct nfs_fh *		fh; | 
|  | 37 | }; | 
|  | 38 |  | 
|  | 39 | /* | 
|  | 40 | * Obtain an NFS file handle for the given host and path | 
|  | 41 | */ | 
|  | 42 | int | 
|  | 43 | nfsroot_mount(struct sockaddr_in *addr, char *path, struct nfs_fh *fh, | 
|  | 44 | int version, int protocol) | 
|  | 45 | { | 
|  | 46 | struct rpc_clnt		*mnt_clnt; | 
|  | 47 | struct mnt_fhstatus	result = { | 
|  | 48 | .fh		= fh | 
|  | 49 | }; | 
| Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 50 | struct rpc_message msg	= { | 
|  | 51 | .rpc_argp	= path, | 
|  | 52 | .rpc_resp	= &result, | 
|  | 53 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int			status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | dprintk("NFS:      nfs_mount(%08x:%s)\n", | 
|  | 57 | (unsigned)ntohl(addr->sin_addr.s_addr), path); | 
|  | 58 |  | 
| Chuck Lever | 43780b8 | 2007-07-01 12:13:22 -0400 | [diff] [blame^] | 59 | mnt_clnt = mnt_create(addr, version, protocol); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | if (IS_ERR(mnt_clnt)) | 
|  | 61 | return PTR_ERR(mnt_clnt); | 
|  | 62 |  | 
| Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 63 | if (version == NFS_MNT3_VERSION) | 
|  | 64 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT]; | 
|  | 65 | else | 
|  | 66 | msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT]; | 
|  | 67 |  | 
|  | 68 | status = rpc_call_sync(mnt_clnt, &msg, 0); | 
| Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 69 | rpc_shutdown_client(mnt_clnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return status < 0? status : (result.status? -EACCES : 0); | 
|  | 71 | } | 
|  | 72 |  | 
| Chuck Lever | 43780b8 | 2007-07-01 12:13:22 -0400 | [diff] [blame^] | 73 | static struct rpc_clnt *mnt_create(struct sockaddr_in *srvaddr, int version, | 
|  | 74 | int protocol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { | 
| Chuck Lever | 058ad9c | 2006-08-27 17:23:53 -0400 | [diff] [blame] | 76 | struct rpc_create_args args = { | 
|  | 77 | .protocol	= protocol, | 
|  | 78 | .address	= (struct sockaddr *)srvaddr, | 
|  | 79 | .addrsize	= sizeof(*srvaddr), | 
| Chuck Lever | 058ad9c | 2006-08-27 17:23:53 -0400 | [diff] [blame] | 80 | .program	= &mnt_program, | 
|  | 81 | .version	= version, | 
|  | 82 | .authflavor	= RPC_AUTH_UNIX, | 
| Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 83 | .flags		= RPC_CLNT_CREATE_INTR, | 
| Chuck Lever | 058ad9c | 2006-08-27 17:23:53 -0400 | [diff] [blame] | 84 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
| Chuck Lever | 058ad9c | 2006-08-27 17:23:53 -0400 | [diff] [blame] | 86 | return rpc_create(&args); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
|  | 89 | /* | 
|  | 90 | * XDR encode/decode functions for MOUNT | 
|  | 91 | */ | 
|  | 92 | static int | 
| Al Viro | d21ec0c | 2006-10-19 23:28:52 -0700 | [diff] [blame] | 93 | xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p, const char *path) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { | 
|  | 95 | p = xdr_encode_string(p, path); | 
|  | 96 |  | 
|  | 97 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); | 
|  | 98 | return 0; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static int | 
| Al Viro | d21ec0c | 2006-10-19 23:28:52 -0700 | [diff] [blame] | 102 | xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p, struct mnt_fhstatus *res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { | 
|  | 104 | struct nfs_fh *fh = res->fh; | 
|  | 105 |  | 
|  | 106 | if ((res->status = ntohl(*p++)) == 0) { | 
|  | 107 | fh->size = NFS2_FHSIZE; | 
|  | 108 | memcpy(fh->data, p, NFS2_FHSIZE); | 
|  | 109 | } | 
|  | 110 | return 0; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | static int | 
| Al Viro | d21ec0c | 2006-10-19 23:28:52 -0700 | [diff] [blame] | 114 | xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p, struct mnt_fhstatus *res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { | 
|  | 116 | struct nfs_fh *fh = res->fh; | 
|  | 117 |  | 
|  | 118 | if ((res->status = ntohl(*p++)) == 0) { | 
|  | 119 | int size = ntohl(*p++); | 
|  | 120 | if (size <= NFS3_FHSIZE) { | 
|  | 121 | fh->size = size; | 
|  | 122 | memcpy(fh->data, p, size); | 
|  | 123 | } else | 
|  | 124 | res->status = -EBADHANDLE; | 
|  | 125 | } | 
|  | 126 | return 0; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | #define MNT_dirpath_sz		(1 + 256) | 
|  | 130 | #define MNT_fhstatus_sz		(1 + 8) | 
| Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 131 | #define MNT_fhstatus3_sz	(1 + 16) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | static struct rpc_procinfo	mnt_procedures[] = { | 
|  | 134 | [MNTPROC_MNT] = { | 
|  | 135 | .p_proc		= MNTPROC_MNT, | 
|  | 136 | .p_encode		= (kxdrproc_t) xdr_encode_dirpath, | 
|  | 137 | .p_decode		= (kxdrproc_t) xdr_decode_fhstatus, | 
| Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 138 | .p_arglen		= MNT_dirpath_sz, | 
|  | 139 | .p_replen		= MNT_fhstatus_sz, | 
| Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 140 | .p_statidx		= MNTPROC_MNT, | 
|  | 141 | .p_name		= "MOUNT", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | }, | 
|  | 143 | }; | 
|  | 144 |  | 
|  | 145 | static struct rpc_procinfo mnt3_procedures[] = { | 
|  | 146 | [MOUNTPROC3_MNT] = { | 
|  | 147 | .p_proc		= MOUNTPROC3_MNT, | 
|  | 148 | .p_encode		= (kxdrproc_t) xdr_encode_dirpath, | 
|  | 149 | .p_decode		= (kxdrproc_t) xdr_decode_fhstatus3, | 
| Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 150 | .p_arglen		= MNT_dirpath_sz, | 
|  | 151 | .p_replen		= MNT_fhstatus3_sz, | 
| Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 152 | .p_statidx		= MOUNTPROC3_MNT, | 
|  | 153 | .p_name		= "MOUNT", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }, | 
|  | 155 | }; | 
|  | 156 |  | 
|  | 157 |  | 
|  | 158 | static struct rpc_version	mnt_version1 = { | 
|  | 159 | .number		= 1, | 
|  | 160 | .nrprocs 	= 2, | 
|  | 161 | .procs 		= mnt_procedures | 
|  | 162 | }; | 
|  | 163 |  | 
|  | 164 | static struct rpc_version       mnt_version3 = { | 
|  | 165 | .number		= 3, | 
|  | 166 | .nrprocs	= 2, | 
|  | 167 | .procs		= mnt3_procedures | 
|  | 168 | }; | 
|  | 169 |  | 
|  | 170 | static struct rpc_version *	mnt_version[] = { | 
|  | 171 | NULL, | 
|  | 172 | &mnt_version1, | 
|  | 173 | NULL, | 
|  | 174 | &mnt_version3, | 
|  | 175 | }; | 
|  | 176 |  | 
|  | 177 | static struct rpc_stat		mnt_stats; | 
|  | 178 |  | 
|  | 179 | static struct rpc_program	mnt_program = { | 
|  | 180 | .name		= "mount", | 
|  | 181 | .number		= NFS_MNT_PROGRAM, | 
| Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 182 | .nrvers		= ARRAY_SIZE(mnt_version), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | .version	= mnt_version, | 
|  | 184 | .stats		= &mnt_stats, | 
|  | 185 | }; |