1/*
2 * linux/fs/nfsd/nfsfh.c
3 *
4 * NFS server file handle treatment.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
8 * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
9 * ... and again Southern-Winter 2001 to support export_operations
10 */
11
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/unistd.h>
15#include <linux/string.h>
16#include <linux/stat.h>
17#include <linux/dcache.h>
18#include <linux/mount.h>
19
20#include <linux/sunrpc/clnt.h>
21#include <linux/sunrpc/svc.h>
22#include <linux/nfsd/nfsd.h>
23
24#define NFSDDBG_FACILITY		NFSDDBG_FH
25
26
27static int nfsd_nr_verified;
28static int nfsd_nr_put;
29
30extern struct export_operations export_op_default;
31
32#define	CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
33
34/*
35 * our acceptability function.
36 * if NOSUBTREECHECK, accept anything
37 * if not, require that we can walk up to exp->ex_dentry
38 * doing some checks on the 'x' bits
39 */
40static int nfsd_acceptable(void *expv, struct dentry *dentry)
41{
42	struct svc_export *exp = expv;
43	int rv;
44	struct dentry *tdentry;
45	struct dentry *parent;
46
47	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
48		return 1;
49
50	tdentry = dget(dentry);
51	while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
52		/* make sure parents give x permission to user */
53		int err;
54		parent = dget_parent(tdentry);
55		err = permission(parent->d_inode, MAY_EXEC, NULL);
56		if (err < 0) {
57			dput(parent);
58			break;
59		}
60		dput(tdentry);
61		tdentry = parent;
62	}
63	if (tdentry != exp->ex_dentry)
64		dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
65	rv = (tdentry == exp->ex_dentry);
66	dput(tdentry);
67	return rv;
68}
69
70/* Type check. The correct error return for type mismatches does not seem to be
71 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
72 * comment in the NFSv3 spec says this is incorrect (implementation notes for
73 * the write call).
74 */
75static inline __be32
76nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
77{
78	/* Type can be negative when creating hardlinks - not to a dir */
79	if (type > 0 && (mode & S_IFMT) != type) {
80		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
81			return nfserr_symlink;
82		else if (type == S_IFDIR)
83			return nfserr_notdir;
84		else if ((mode & S_IFMT) == S_IFDIR)
85			return nfserr_isdir;
86		else
87			return nfserr_inval;
88	}
89	if (type < 0 && (mode & S_IFMT) == -type) {
90		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
91			return nfserr_symlink;
92		else if (type == -S_IFDIR)
93			return nfserr_isdir;
94		else
95			return nfserr_notdir;
96	}
97	return 0;
98}
99
100/*
101 * Perform sanity checks on the dentry in a client's file handle.
102 *
103 * Note that the file handle dentry may need to be freed even after
104 * an error return.
105 *
106 * This is only called at the start of an nfsproc call, so fhp points to
107 * a svc_fh which is all 0 except for the over-the-wire file handle.
108 */
109__be32
110fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
111{
112	struct knfsd_fh	*fh = &fhp->fh_handle;
113	struct svc_export *exp = NULL;
114	struct dentry	*dentry;
115	__be32		error = 0;
116
117	dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
118
119	if (!fhp->fh_dentry) {
120		__u32 *datap=NULL;
121		__u32 tfh[3];		/* filehandle fragment for oldstyle filehandles */
122		int fileid_type;
123		int data_left = fh->fh_size/4;
124
125		error = nfserr_stale;
126		if (rqstp->rq_client == NULL)
127			goto out;
128		if (rqstp->rq_vers > 2)
129			error = nfserr_badhandle;
130		if (rqstp->rq_vers == 4 && fh->fh_size == 0)
131			return nfserr_nofilehandle;
132
133		if (fh->fh_version == 1) {
134			int len;
135			datap = fh->fh_auth;
136			if (--data_left<0) goto out;
137			switch (fh->fh_auth_type) {
138			case 0: break;
139			default: goto out;
140			}
141			len = key_len(fh->fh_fsid_type) / 4;
142			if (len == 0) goto out;
143			if  (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
144				/* deprecated, convert to type 3 */
145				len = key_len(FSID_ENCODE_DEV)/4;
146				fh->fh_fsid_type = FSID_ENCODE_DEV;
147				fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
148				fh->fh_fsid[1] = fh->fh_fsid[2];
149			}
150			if ((data_left -= len)<0) goto out;
151			exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
152			datap += len;
153		} else {
154			dev_t xdev;
155			ino_t xino;
156			if (fh->fh_size != NFS_FHSIZE)
157				goto out;
158			/* assume old filehandle format */
159			xdev = old_decode_dev(fh->ofh_xdev);
160			xino = u32_to_ino_t(fh->ofh_xino);
161			mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
162			exp = exp_find(rqstp->rq_client, FSID_DEV, tfh,
163				       &rqstp->rq_chandle);
164		}
165
166		if (IS_ERR(exp) && (PTR_ERR(exp) == -EAGAIN
167				|| PTR_ERR(exp) == -ETIMEDOUT)) {
168			error = nfserrno(PTR_ERR(exp));
169			goto out;
170		}
171
172		error = nfserr_stale;
173		if (!exp || IS_ERR(exp))
174			goto out;
175
176		/* Check if the request originated from a secure port. */
177		error = nfserr_perm;
178		if (!rqstp->rq_secure && EX_SECURE(exp)) {
179			char buf[RPC_MAX_ADDRBUFLEN];
180			printk(KERN_WARNING
181			       "nfsd: request from insecure port %s!\n",
182			       svc_print_addr(rqstp, buf, sizeof(buf)));
183			goto out;
184		}
185
186		/* Set user creds for this exportpoint */
187		error = nfserrno(nfsd_setuser(rqstp, exp));
188		if (error)
189			goto out;
190
191		/*
192		 * Look up the dentry using the NFS file handle.
193		 */
194		error = nfserr_stale;
195		if (rqstp->rq_vers > 2)
196			error = nfserr_badhandle;
197
198		if (fh->fh_version != 1) {
199			tfh[0] = fh->ofh_ino;
200			tfh[1] = fh->ofh_generation;
201			tfh[2] = fh->ofh_dirino;
202			datap = tfh;
203			data_left = 3;
204			if (fh->ofh_dirino == 0)
205				fileid_type = 1;
206			else
207				fileid_type = 2;
208		} else
209			fileid_type = fh->fh_fileid_type;
210
211		if (fileid_type == 0)
212			dentry = dget(exp->ex_dentry);
213		else {
214			struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
215			dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
216						     datap, data_left,
217						     fileid_type,
218						     nfsd_acceptable, exp);
219		}
220		if (dentry == NULL)
221			goto out;
222		if (IS_ERR(dentry)) {
223			if (PTR_ERR(dentry) != -EINVAL)
224				error = nfserrno(PTR_ERR(dentry));
225			goto out;
226		}
227
228		if (S_ISDIR(dentry->d_inode->i_mode) &&
229		    (dentry->d_flags & DCACHE_DISCONNECTED)) {
230			printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
231			       dentry->d_parent->d_name.name, dentry->d_name.name);
232		}
233
234		fhp->fh_dentry = dentry;
235		fhp->fh_export = exp;
236		nfsd_nr_verified++;
237	} else {
238		/* just rechecking permissions
239		 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
240		 */
241		dprintk("nfsd: fh_verify - just checking\n");
242		dentry = fhp->fh_dentry;
243		exp = fhp->fh_export;
244		/* Set user creds for this exportpoint; necessary even
245		 * in the "just checking" case because this may be a
246		 * filehandle that was created by fh_compose, and that
247		 * is about to be used in another nfsv4 compound
248		 * operation */
249		error = nfserrno(nfsd_setuser(rqstp, exp));
250		if (error)
251			goto out;
252	}
253	cache_get(&exp->h);
254
255
256	error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
257	if (error)
258		goto out;
259
260	/* Finally, check access permissions. */
261	error = nfsd_permission(exp, dentry, access);
262
263	if (error) {
264		dprintk("fh_verify: %s/%s permission failure, "
265			"acc=%x, error=%d\n",
266			dentry->d_parent->d_name.name,
267			dentry->d_name.name,
268			access, ntohl(error));
269	}
270out:
271	if (exp && !IS_ERR(exp))
272		exp_put(exp);
273	if (error == nfserr_stale)
274		nfsdstats.fh_stale++;
275	return error;
276}
277
278
279/*
280 * Compose a file handle for an NFS reply.
281 *
282 * Note that when first composed, the dentry may not yet have
283 * an inode.  In this case a call to fh_update should be made
284 * before the fh goes out on the wire ...
285 */
286static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
287			     __u32 *datap, int *maxsize)
288{
289	struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
290
291	if (dentry == exp->ex_dentry) {
292		*maxsize = 0;
293		return 0;
294	}
295
296	return CALL(nop,encode_fh)(dentry, datap, maxsize,
297			  !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
298}
299
300/*
301 * for composing old style file handles
302 */
303static inline void _fh_update_old(struct dentry *dentry,
304				  struct svc_export *exp,
305				  struct knfsd_fh *fh)
306{
307	fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
308	fh->ofh_generation = dentry->d_inode->i_generation;
309	if (S_ISDIR(dentry->d_inode->i_mode) ||
310	    (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
311		fh->ofh_dirino = 0;
312}
313
314__be32
315fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
316	   struct svc_fh *ref_fh)
317{
318	/* ref_fh is a reference file handle.
319	 * if it is non-null and for the same filesystem, then we should compose
320	 * a filehandle which is of the same version, where possible.
321	 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
322	 * Then create a 32byte filehandle using nfs_fhbase_old
323	 *
324	 */
325
326	u8 version;
327	u8 fsid_type = 0;
328	struct inode * inode = dentry->d_inode;
329	struct dentry *parent = dentry->d_parent;
330	__u32 *datap;
331	dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
332	int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
333
334	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
335		MAJOR(ex_dev), MINOR(ex_dev),
336		(long) exp->ex_dentry->d_inode->i_ino,
337		parent->d_name.name, dentry->d_name.name,
338		(inode ? inode->i_ino : 0));
339
340	/* Choose filehandle version and fsid type based on
341	 * the reference filehandle (if it is in the same export)
342	 * or the export options.
343	 */
344 retry:
345	version = 1;
346	if (ref_fh && ref_fh->fh_export == exp) {
347		version = ref_fh->fh_handle.fh_version;
348		fsid_type = ref_fh->fh_handle.fh_fsid_type;
349
350		if (ref_fh == fhp)
351			fh_put(ref_fh);
352		ref_fh = NULL;
353
354		switch (version) {
355		case 0xca:
356			fsid_type = FSID_DEV;
357			break;
358		case 1:
359			break;
360		default:
361			goto retry;
362		}
363
364		/* Need to check that this type works for this
365		 * export point.  As the fsid -> filesystem mapping
366		 * was guided by user-space, there is no guarantee
367		 * that the filesystem actually supports that fsid
368		 * type. If it doesn't we loop around again without
369		 * ref_fh set.
370		 */
371		switch(fsid_type) {
372		case FSID_DEV:
373			if (!old_valid_dev(ex_dev))
374				goto retry;
375			/* FALL THROUGH */
376		case FSID_MAJOR_MINOR:
377		case FSID_ENCODE_DEV:
378			if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags
379			      & FS_REQUIRES_DEV))
380				goto retry;
381			break;
382		case FSID_NUM:
383			if (! (exp->ex_flags & NFSEXP_FSID))
384				goto retry;
385			break;
386		case FSID_UUID8:
387		case FSID_UUID16:
388			if (!root_export)
389				goto retry;
390			/* fall through */
391		case FSID_UUID4_INUM:
392		case FSID_UUID16_INUM:
393			if (exp->ex_uuid == NULL)
394				goto retry;
395			break;
396		}
397	} else if (exp->ex_uuid) {
398		if (fhp->fh_maxsize >= 64) {
399			if (root_export)
400				fsid_type = FSID_UUID16;
401			else
402				fsid_type = FSID_UUID16_INUM;
403		} else {
404			if (root_export)
405				fsid_type = FSID_UUID8;
406			else
407				fsid_type = FSID_UUID4_INUM;
408		}
409	} else if (exp->ex_flags & NFSEXP_FSID)
410		fsid_type = FSID_NUM;
411	else if (!old_valid_dev(ex_dev))
412		/* for newer device numbers, we must use a newer fsid format */
413		fsid_type = FSID_ENCODE_DEV;
414	else
415		fsid_type = FSID_DEV;
416
417	if (ref_fh == fhp)
418		fh_put(ref_fh);
419
420	if (fhp->fh_locked || fhp->fh_dentry) {
421		printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
422		       parent->d_name.name, dentry->d_name.name);
423	}
424	if (fhp->fh_maxsize < NFS_FHSIZE)
425		printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
426		       fhp->fh_maxsize,
427		       parent->d_name.name, dentry->d_name.name);
428
429	fhp->fh_dentry = dget(dentry); /* our internal copy */
430	fhp->fh_export = exp;
431	cache_get(&exp->h);
432
433	if (version == 0xca) {
434		/* old style filehandle please */
435		memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
436		fhp->fh_handle.fh_size = NFS_FHSIZE;
437		fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
438		fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
439		fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
440		fhp->fh_handle.ofh_xino =
441			ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
442		fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
443		if (inode)
444			_fh_update_old(dentry, exp, &fhp->fh_handle);
445	} else {
446		int len;
447		fhp->fh_handle.fh_version = 1;
448		fhp->fh_handle.fh_auth_type = 0;
449		datap = fhp->fh_handle.fh_auth+0;
450		fhp->fh_handle.fh_fsid_type = fsid_type;
451		mk_fsid(fsid_type, datap, ex_dev,
452			exp->ex_dentry->d_inode->i_ino,
453			exp->ex_fsid, exp->ex_uuid);
454
455		len = key_len(fsid_type);
456		datap += len/4;
457		fhp->fh_handle.fh_size = 4 + len;
458
459		if (inode) {
460			int size = (fhp->fh_maxsize-len-4)/4;
461			fhp->fh_handle.fh_fileid_type =
462				_fh_update(dentry, exp, datap, &size);
463			fhp->fh_handle.fh_size += size*4;
464		}
465		if (fhp->fh_handle.fh_fileid_type == 255)
466			return nfserr_opnotsupp;
467	}
468
469	nfsd_nr_verified++;
470	return 0;
471}
472
473/*
474 * Update file handle information after changing a dentry.
475 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
476 */
477__be32
478fh_update(struct svc_fh *fhp)
479{
480	struct dentry *dentry;
481	__u32 *datap;
482
483	if (!fhp->fh_dentry)
484		goto out_bad;
485
486	dentry = fhp->fh_dentry;
487	if (!dentry->d_inode)
488		goto out_negative;
489	if (fhp->fh_handle.fh_version != 1) {
490		_fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
491	} else {
492		int size;
493		if (fhp->fh_handle.fh_fileid_type != 0)
494			goto out;
495		datap = fhp->fh_handle.fh_auth+
496			fhp->fh_handle.fh_size/4 -1;
497		size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
498		fhp->fh_handle.fh_fileid_type =
499			_fh_update(dentry, fhp->fh_export, datap, &size);
500		fhp->fh_handle.fh_size += size*4;
501		if (fhp->fh_handle.fh_fileid_type == 255)
502			return nfserr_opnotsupp;
503	}
504out:
505	return 0;
506
507out_bad:
508	printk(KERN_ERR "fh_update: fh not verified!\n");
509	goto out;
510out_negative:
511	printk(KERN_ERR "fh_update: %s/%s still negative!\n",
512		dentry->d_parent->d_name.name, dentry->d_name.name);
513	goto out;
514}
515
516/*
517 * Release a file handle.
518 */
519void
520fh_put(struct svc_fh *fhp)
521{
522	struct dentry * dentry = fhp->fh_dentry;
523	struct svc_export * exp = fhp->fh_export;
524	if (dentry) {
525		fh_unlock(fhp);
526		fhp->fh_dentry = NULL;
527		dput(dentry);
528#ifdef CONFIG_NFSD_V3
529		fhp->fh_pre_saved = 0;
530		fhp->fh_post_saved = 0;
531#endif
532		nfsd_nr_put++;
533	}
534	if (exp) {
535		cache_put(&exp->h, &svc_export_cache);
536		fhp->fh_export = NULL;
537	}
538	return;
539}
540
541/*
542 * Shorthand for dprintk()'s
543 */
544char * SVCFH_fmt(struct svc_fh *fhp)
545{
546	struct knfsd_fh *fh = &fhp->fh_handle;
547
548	static char buf[80];
549	sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
550		fh->fh_size,
551		fh->fh_base.fh_pad[0],
552		fh->fh_base.fh_pad[1],
553		fh->fh_base.fh_pad[2],
554		fh->fh_base.fh_pad[3],
555		fh->fh_base.fh_pad[4],
556		fh->fh_base.fh_pad[5]);
557	return buf;
558}
559
560enum fsid_source fsid_source(struct svc_fh *fhp)
561{
562	if (fhp->fh_handle.fh_version != 1)
563		return FSIDSOURCE_DEV;
564	switch(fhp->fh_handle.fh_fsid_type) {
565	case FSID_DEV:
566	case FSID_ENCODE_DEV:
567	case FSID_MAJOR_MINOR:
568		return FSIDSOURCE_DEV;
569	case FSID_NUM:
570		return FSIDSOURCE_FSID;
571	default:
572		if (fhp->fh_export->ex_flags & NFSEXP_FSID)
573			return FSIDSOURCE_FSID;
574		else
575			return FSIDSOURCE_UUID;
576	}
577}
578