File Index
Files and directories
Sign in
FILE

mount_clnt.c

File preview with syntax highlighting.

linux-6.19.14/fs/nfs/mount_clnt.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * In-kernel MOUNT protocol client
  4 *
  5 * Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de>
  6 */
  7
  8#include <linux/types.h>
  9#include <linux/socket.h>
 10#include <linux/kernel.h>
 11#include <linux/errno.h>
 12#include <linux/uio.h>
 13#include <linux/net.h>
 14#include <linux/in.h>
 15#include <linux/sunrpc/clnt.h>
 16#include <linux/sunrpc/sched.h>
 17#include <linux/nfs_fs.h>
 18#include "internal.h"
 19
 20#define NFSDBG_FACILITY	NFSDBG_MOUNT
 21
 22/*
 23 * Defined by RFC 1094, section A.3; and RFC 1813, section 5.1.4
 24 */
 25#define MNTPATHLEN		(1024)
 26
 27/*
 28 * XDR data type sizes
 29 */
 30#define encode_dirpath_sz	(1 + XDR_QUADLEN(MNTPATHLEN))
 31#define MNT_status_sz		(1)
 32#define MNT_fhandle_sz		XDR_QUADLEN(NFS2_FHSIZE)
 33#define MNT_fhandlev3_sz	XDR_QUADLEN(NFS3_FHSIZE)
 34#define MNT_authflav3_sz	(1 + NFS_MAX_SECFLAVORS)
 35
 36/*
 37 * XDR argument and result sizes
 38 */
 39#define MNT_enc_dirpath_sz	encode_dirpath_sz
 40#define MNT_dec_mountres_sz	(MNT_status_sz + MNT_fhandle_sz)
 41#define MNT_dec_mountres3_sz	(MNT_status_sz + MNT_fhandlev3_sz + \
 42				 MNT_authflav3_sz)
 43
 44/*
 45 * Defined by RFC 1094, section A.5
 46 */
 47enum {
 48	MOUNTPROC_NULL		= 0,
 49	MOUNTPROC_MNT		= 1,
 50	MOUNTPROC_DUMP		= 2,
 51	MOUNTPROC_UMNT		= 3,
 52	MOUNTPROC_UMNTALL	= 4,
 53	MOUNTPROC_EXPORT	= 5,
 54};
 55
 56/*
 57 * Defined by RFC 1813, section 5.2
 58 */
 59enum {
 60	MOUNTPROC3_NULL		= 0,
 61	MOUNTPROC3_MNT		= 1,
 62	MOUNTPROC3_DUMP		= 2,
 63	MOUNTPROC3_UMNT		= 3,
 64	MOUNTPROC3_UMNTALL	= 4,
 65	MOUNTPROC3_EXPORT	= 5,
 66};
 67
 68static const struct rpc_program mnt_program;
 69
 70/*
 71 * Defined by OpenGroup XNFS Version 3W, chapter 8
 72 */
 73enum mountstat {
 74	MNT_OK			= 0,
 75	MNT_EPERM		= 1,
 76	MNT_ENOENT		= 2,
 77	MNT_EACCES		= 13,
 78	MNT_EINVAL		= 22,
 79};
 80
 81static struct {
 82	u32 status;
 83	int errno;
 84} mnt_errtbl[] = {
 85	{ .status = MNT_OK,			.errno = 0,		},
 86	{ .status = MNT_EPERM,			.errno = -EPERM,	},
 87	{ .status = MNT_ENOENT,			.errno = -ENOENT,	},
 88	{ .status = MNT_EACCES,			.errno = -EACCES,	},
 89	{ .status = MNT_EINVAL,			.errno = -EINVAL,	},
 90};
 91
 92/*
 93 * Defined by RFC 1813, section 5.1.5
 94 */
 95enum mountstat3 {
 96	MNT3_OK			= 0,		/* no error */
 97	MNT3ERR_PERM		= 1,		/* Not owner */
 98	MNT3ERR_NOENT		= 2,		/* No such file or directory */
 99	MNT3ERR_IO		= 5,		/* I/O error */
100	MNT3ERR_ACCES		= 13,		/* Permission denied */
101	MNT3ERR_NOTDIR		= 20,		/* Not a directory */
102	MNT3ERR_INVAL		= 22,		/* Invalid argument */
103	MNT3ERR_NAMETOOLONG	= 63,		/* Filename too long */
104	MNT3ERR_NOTSUPP		= 10004,	/* Operation not supported */
105	MNT3ERR_SERVERFAULT	= 10006,	/* A failure on the server */
106};
107
108static struct {
109	u32 status;
110	int errno;
111} mnt3_errtbl[] = {
112	{ .status = MNT3_OK,			.errno = 0,		},
113	{ .status = MNT3ERR_PERM,		.errno = -EPERM,	},
114	{ .status = MNT3ERR_NOENT,		.errno = -ENOENT,	},
115	{ .status = MNT3ERR_IO,			.errno = -EIO,		},
116	{ .status = MNT3ERR_ACCES,		.errno = -EACCES,	},
117	{ .status = MNT3ERR_NOTDIR,		.errno = -ENOTDIR,	},
118	{ .status = MNT3ERR_INVAL,		.errno = -EINVAL,	},
119	{ .status = MNT3ERR_NAMETOOLONG,	.errno = -ENAMETOOLONG,	},
120	{ .status = MNT3ERR_NOTSUPP,		.errno = -ENOTSUPP,	},
121	{ .status = MNT3ERR_SERVERFAULT,	.errno = -EREMOTEIO,	},
122};
123
124struct mountres {
125	int errno;
126	struct nfs_fh *fh;
127	unsigned int *auth_count;
128	rpc_authflavor_t *auth_flavors;
129};
130
131/**
132 * nfs_mount - Obtain an NFS file handle for the given host and path
133 * @info: pointer to mount request arguments
134 * @timeo: deciseconds the mount waits for a response before it retries
135 * @retrans: number of times the mount retries a request
136 *
137 * Uses timeout parameters specified by caller. On successful return, the
138 * auth_flavs list and auth_flav_len will be populated with the list from the
139 * server or a faked-up list if the server didn't provide one.
140 */
141int nfs_mount(struct nfs_mount_request *info, int timeo, int retrans)
142{
143	struct rpc_timeout mnt_timeout;
144	struct mountres	result = {
145		.fh		= info->fh,
146		.auth_count	= info->auth_flav_len,
147		.auth_flavors	= info->auth_flavs,
148	};
149	struct rpc_message msg	= {
150		.rpc_argp	= info->dirpath,
151		.rpc_resp	= &result,
152	};
153	struct rpc_create_args args = {
154		.net		= info->net,
155		.protocol	= info->protocol,
156		.address	= (struct sockaddr *)info->sap,
157		.addrsize	= info->salen,
158		.timeout	= &mnt_timeout,
159		.servername	= info->hostname,
160		.program	= &mnt_program,
161		.version	= info->version,
162		.authflavor	= RPC_AUTH_UNIX,
163		.cred		= current_cred(),
164	};
165	struct rpc_clnt		*mnt_clnt;
166	int			status;
167
168	dprintk("NFS: sending MNT request for %s:%s\n",
169		(info->hostname ? info->hostname : "server"),
170			info->dirpath);
171
172	if (strlen(info->dirpath) > MNTPATHLEN)
173		return -ENAMETOOLONG;
174
175	if (info->noresvport)
176		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
177
178	nfs_init_timeout_values(&mnt_timeout, info->protocol, timeo, retrans);
179	mnt_clnt = rpc_create(&args);
180	if (IS_ERR(mnt_clnt))
181		goto out_clnt_err;
182
183	if (info->version == NFS_MNT3_VERSION)
184		msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
185	else
186		msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
187
188	status = rpc_call_sync(mnt_clnt, &msg, RPC_TASK_SOFT|RPC_TASK_TIMEOUT);
189	rpc_shutdown_client(mnt_clnt);
190
191	if (status < 0)
192		goto out_call_err;
193	if (result.errno != 0)
194		goto out_mnt_err;
195
196	dprintk("NFS: MNT request succeeded\n");
197	status = 0;
198
199	/*
200	 * If the server didn't provide a flavor list, allow the
201	 * client to try any flavor.
202	 */
203	if (info->version != NFS_MNT3_VERSION || *info->auth_flav_len == 0) {
204		dprintk("NFS: Faking up auth_flavs list\n");
205		info->auth_flavs[0] = RPC_AUTH_NULL;
206		*info->auth_flav_len = 1;
207	}
208out:
209	return status;
210
211out_clnt_err:
212	status = PTR_ERR(mnt_clnt);
213	dprintk("NFS: failed to create MNT RPC client, status=%d\n", status);
214	goto out;
215
216out_call_err:
217	dprintk("NFS: MNT request failed, status=%d\n", status);
218	goto out;
219
220out_mnt_err:
221	dprintk("NFS: MNT server returned result %d\n", result.errno);
222	status = result.errno;
223	goto out;
224}
225
226/*
227 * XDR encode/decode functions for MOUNT
228 */
229
230static void encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
231{
232	const u32 pathname_len = strlen(pathname);
233	__be32 *p;
234
235	p = xdr_reserve_space(xdr, 4 + pathname_len);
236	xdr_encode_opaque(p, pathname, pathname_len);
237}
238
239static void mnt_xdr_enc_dirpath(struct rpc_rqst *req, struct xdr_stream *xdr,
240				const void *dirpath)
241{
242	encode_mntdirpath(xdr, dirpath);
243}
244
245/*
246 * RFC 1094: "A non-zero status indicates some sort of error.  In this
247 * case, the status is a UNIX error number."  This can be problematic
248 * if the server and client use different errno values for the same
249 * error.
250 *
251 * However, the OpenGroup XNFS spec provides a simple mapping that is
252 * independent of local errno values on the server and the client.
253 */
254static int decode_status(struct xdr_stream *xdr, struct mountres *res)
255{
256	unsigned int i;
257	u32 status;
258	__be32 *p;
259
260	p = xdr_inline_decode(xdr, 4);
261	if (unlikely(p == NULL))
262		return -EIO;
263	status = be32_to_cpup(p);
264
265	for (i = 0; i < ARRAY_SIZE(mnt_errtbl); i++) {
266		if (mnt_errtbl[i].status == status) {
267			res->errno = mnt_errtbl[i].errno;
268			return 0;
269		}
270	}
271
272	dprintk("NFS: unrecognized MNT status code: %u\n", status);
273	res->errno = -EACCES;
274	return 0;
275}
276
277static int decode_fhandle(struct xdr_stream *xdr, struct mountres *res)
278{
279	struct nfs_fh *fh = res->fh;
280	__be32 *p;
281
282	p = xdr_inline_decode(xdr, NFS2_FHSIZE);
283	if (unlikely(p == NULL))
284		return -EIO;
285
286	fh->size = NFS2_FHSIZE;
287	memcpy(fh->data, p, NFS2_FHSIZE);
288	return 0;
289}
290
291static int mnt_xdr_dec_mountres(struct rpc_rqst *req,
292				struct xdr_stream *xdr,
293				void *data)
294{
295	struct mountres *res = data;
296	int status;
297
298	status = decode_status(xdr, res);
299	if (unlikely(status != 0 || res->errno != 0))
300		return status;
301	return decode_fhandle(xdr, res);
302}
303
304static int decode_fhs_status(struct xdr_stream *xdr, struct mountres *res)
305{
306	unsigned int i;
307	u32 status;
308	__be32 *p;
309
310	p = xdr_inline_decode(xdr, 4);
311	if (unlikely(p == NULL))
312		return -EIO;
313	status = be32_to_cpup(p);
314
315	for (i = 0; i < ARRAY_SIZE(mnt3_errtbl); i++) {
316		if (mnt3_errtbl[i].status == status) {
317			res->errno = mnt3_errtbl[i].errno;
318			return 0;
319		}
320	}
321
322	dprintk("NFS: unrecognized MNT3 status code: %u\n", status);
323	res->errno = -EACCES;
324	return 0;
325}
326
327static int decode_fhandle3(struct xdr_stream *xdr, struct mountres *res)
328{
329	struct nfs_fh *fh = res->fh;
330	u32 size;
331	__be32 *p;
332
333	p = xdr_inline_decode(xdr, 4);
334	if (unlikely(p == NULL))
335		return -EIO;
336
337	size = be32_to_cpup(p);
338	if (size > NFS3_FHSIZE || size == 0)
339		return -EIO;
340
341	p = xdr_inline_decode(xdr, size);
342	if (unlikely(p == NULL))
343		return -EIO;
344
345	fh->size = size;
346	memcpy(fh->data, p, size);
347	return 0;
348}
349
350static int decode_auth_flavors(struct xdr_stream *xdr, struct mountres *res)
351{
352	rpc_authflavor_t *flavors = res->auth_flavors;
353	unsigned int *count = res->auth_count;
354	u32 entries, i;
355	__be32 *p;
356
357	if (*count == 0)
358		return 0;
359
360	p = xdr_inline_decode(xdr, 4);
361	if (unlikely(p == NULL))
362		return -EIO;
363	entries = be32_to_cpup(p);
364	dprintk("NFS: received %u auth flavors\n", entries);
365	if (entries > NFS_MAX_SECFLAVORS)
366		entries = NFS_MAX_SECFLAVORS;
367
368	p = xdr_inline_decode(xdr, 4 * entries);
369	if (unlikely(p == NULL))
370		return -EIO;
371
372	if (entries > *count)
373		entries = *count;
374
375	for (i = 0; i < entries; i++) {
376		flavors[i] = be32_to_cpup(p++);
377		dprintk("NFS:   auth flavor[%u]: %d\n", i, flavors[i]);
378	}
379	*count = i;
380
381	return 0;
382}
383
384static int mnt_xdr_dec_mountres3(struct rpc_rqst *req,
385				 struct xdr_stream *xdr,
386				 void *data)
387{
388	struct mountres *res = data;
389	int status;
390
391	status = decode_fhs_status(xdr, res);
392	if (unlikely(status != 0 || res->errno != 0))
393		return status;
394	status = decode_fhandle3(xdr, res);
395	if (unlikely(status != 0)) {
396		res->errno = -EBADHANDLE;
397		return 0;
398	}
399	return decode_auth_flavors(xdr, res);
400}
401
402static const struct rpc_procinfo mnt_procedures[] = {
403	[MOUNTPROC_MNT] = {
404		.p_proc		= MOUNTPROC_MNT,
405		.p_encode	= mnt_xdr_enc_dirpath,
406		.p_decode	= mnt_xdr_dec_mountres,
407		.p_arglen	= MNT_enc_dirpath_sz,
408		.p_replen	= MNT_dec_mountres_sz,
409		.p_statidx	= MOUNTPROC_MNT,
410		.p_name		= "MOUNT",
411	},
412	[MOUNTPROC_UMNT] = {
413		.p_proc		= MOUNTPROC_UMNT,
414		.p_encode	= mnt_xdr_enc_dirpath,
415		.p_arglen	= MNT_enc_dirpath_sz,
416		.p_statidx	= MOUNTPROC_UMNT,
417		.p_name		= "UMOUNT",
418	},
419};
420
421static const struct rpc_procinfo mnt3_procedures[] = {
422	[MOUNTPROC3_MNT] = {
423		.p_proc		= MOUNTPROC3_MNT,
424		.p_encode	= mnt_xdr_enc_dirpath,
425		.p_decode	= mnt_xdr_dec_mountres3,
426		.p_arglen	= MNT_enc_dirpath_sz,
427		.p_replen	= MNT_dec_mountres3_sz,
428		.p_statidx	= MOUNTPROC3_MNT,
429		.p_name		= "MOUNT",
430	},
431	[MOUNTPROC3_UMNT] = {
432		.p_proc		= MOUNTPROC3_UMNT,
433		.p_encode	= mnt_xdr_enc_dirpath,
434		.p_arglen	= MNT_enc_dirpath_sz,
435		.p_statidx	= MOUNTPROC3_UMNT,
436		.p_name		= "UMOUNT",
437	},
438};
439
440static unsigned int mnt_counts[ARRAY_SIZE(mnt_procedures)];
441static const struct rpc_version mnt_version1 = {
442	.number		= 1,
443	.nrprocs	= ARRAY_SIZE(mnt_procedures),
444	.procs		= mnt_procedures,
445	.counts		= mnt_counts,
446};
447
448static unsigned int mnt3_counts[ARRAY_SIZE(mnt3_procedures)];
449static const struct rpc_version mnt_version3 = {
450	.number		= 3,
451	.nrprocs	= ARRAY_SIZE(mnt3_procedures),
452	.procs		= mnt3_procedures,
453	.counts		= mnt3_counts,
454};
455
456static const struct rpc_version *mnt_version[] = {
457	NULL,
458	&mnt_version1,
459	NULL,
460	&mnt_version3,
461};
462
463static struct rpc_stat mnt_stats;
464
465static const struct rpc_program mnt_program = {
466	.name		= "mount",
467	.number		= NFS_MNT_PROGRAM,
468	.nrvers		= ARRAY_SIZE(mnt_version),
469	.version	= mnt_version,
470	.stats		= &mnt_stats,
471};
~/ Read-only file preview.