2 * linux/fs/nfsd/nfsfh.c
4 * NFS server file handle treatment.
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
12 #include <linux/slab.h>
14 #include <linux/unistd.h>
15 #include <linux/string.h>
16 #include <linux/stat.h>
17 #include <linux/dcache.h>
18 #include <linux/exportfs.h>
19 #include <linux/mount.h>
21 #include <linux/sunrpc/clnt.h>
22 #include <linux/sunrpc/svc.h>
23 #include <linux/sunrpc/svcauth_gss.h>
24 #include <linux/nfsd/nfsd.h>
26 #define NFSDDBG_FACILITY NFSDDBG_FH
29 static int nfsd_nr_verified;
30 static int nfsd_nr_put;
33 * our acceptability function.
34 * if NOSUBTREECHECK, accept anything
35 * if not, require that we can walk up to exp->ex_dentry
36 * doing some checks on the 'x' bits
38 static int nfsd_acceptable(void *expv, struct dentry *dentry)
40 struct svc_export *exp = expv;
42 struct dentry *tdentry;
43 struct dentry *parent;
45 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
48 tdentry = dget(dentry);
49 while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
50 /* make sure parents give x permission to user */
52 parent = dget_parent(tdentry);
53 err = permission(parent->d_inode, MAY_EXEC, NULL);
61 if (tdentry != exp->ex_dentry)
62 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
63 rv = (tdentry == exp->ex_dentry);
68 /* Type check. The correct error return for type mismatches does not seem to be
69 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
70 * comment in the NFSv3 spec says this is incorrect (implementation notes for
74 nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
76 /* Type can be negative when creating hardlinks - not to a dir */
77 if (type > 0 && (mode & S_IFMT) != type) {
78 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
79 return nfserr_symlink;
80 else if (type == S_IFDIR)
82 else if ((mode & S_IFMT) == S_IFDIR)
87 if (type < 0 && (mode & S_IFMT) == -type) {
88 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
89 return nfserr_symlink;
90 else if (type == -S_IFDIR)
99 * Perform sanity checks on the dentry in a client's file handle.
101 * Note that the file handle dentry may need to be freed even after
104 * This is only called at the start of an nfsproc call, so fhp points to
105 * a svc_fh which is all 0 except for the over-the-wire file handle.
108 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
110 struct knfsd_fh *fh = &fhp->fh_handle;
111 struct svc_export *exp = NULL;
112 struct dentry *dentry;
115 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
117 if (!fhp->fh_dentry) {
119 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
121 int data_left = fh->fh_size/4;
123 error = nfserr_stale;
124 if (rqstp->rq_vers > 2)
125 error = nfserr_badhandle;
126 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
127 return nfserr_nofilehandle;
129 if (fh->fh_version == 1) {
132 if (--data_left<0) goto out;
133 switch (fh->fh_auth_type) {
137 len = key_len(fh->fh_fsid_type) / 4;
138 if (len == 0) goto out;
139 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
140 /* deprecated, convert to type 3 */
141 len = key_len(FSID_ENCODE_DEV)/4;
142 fh->fh_fsid_type = FSID_ENCODE_DEV;
143 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
144 fh->fh_fsid[1] = fh->fh_fsid[2];
146 if ((data_left -= len)<0) goto out;
147 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, datap);
152 if (fh->fh_size != NFS_FHSIZE)
154 /* assume old filehandle format */
155 xdev = old_decode_dev(fh->ofh_xdev);
156 xino = u32_to_ino_t(fh->ofh_xino);
157 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
158 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
161 error = nfserr_stale;
162 if (PTR_ERR(exp) == -ENOENT)
166 error = nfserrno(PTR_ERR(exp));
170 /* Check if the request originated from a secure port. */
172 if (!rqstp->rq_secure && EX_SECURE(exp)) {
173 char buf[RPC_MAX_ADDRBUFLEN];
175 "nfsd: request from insecure port %s!\n",
176 svc_print_addr(rqstp, buf, sizeof(buf)));
180 /* Set user creds for this exportpoint */
181 error = nfserrno(nfsd_setuser(rqstp, exp));
186 * Look up the dentry using the NFS file handle.
188 error = nfserr_stale;
189 if (rqstp->rq_vers > 2)
190 error = nfserr_badhandle;
192 if (fh->fh_version != 1) {
193 tfh[0] = fh->ofh_ino;
194 tfh[1] = fh->ofh_generation;
195 tfh[2] = fh->ofh_dirino;
198 if (fh->ofh_dirino == 0)
203 fileid_type = fh->fh_fileid_type;
205 if (fileid_type == 0)
206 dentry = dget(exp->ex_dentry);
208 dentry = exportfs_decode_fh(exp->ex_mnt, datap,
209 data_left, fileid_type,
210 nfsd_acceptable, exp);
214 if (IS_ERR(dentry)) {
215 if (PTR_ERR(dentry) != -EINVAL)
216 error = nfserrno(PTR_ERR(dentry));
220 if (S_ISDIR(dentry->d_inode->i_mode) &&
221 (dentry->d_flags & DCACHE_DISCONNECTED)) {
222 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
223 dentry->d_parent->d_name.name, dentry->d_name.name);
226 fhp->fh_dentry = dentry;
227 fhp->fh_export = exp;
230 /* just rechecking permissions
231 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
233 dprintk("nfsd: fh_verify - just checking\n");
234 dentry = fhp->fh_dentry;
235 exp = fhp->fh_export;
236 /* Set user creds for this exportpoint; necessary even
237 * in the "just checking" case because this may be a
238 * filehandle that was created by fh_compose, and that
239 * is about to be used in another nfsv4 compound
241 error = nfserrno(nfsd_setuser(rqstp, exp));
248 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
252 /* Check security flavor */
253 error = check_nfsd_access(exp, rqstp);
257 /* Finally, check access permissions. */
258 error = nfsd_permission(exp, dentry, access);
261 dprintk("fh_verify: %s/%s permission failure, "
262 "acc=%x, error=%d\n",
263 dentry->d_parent->d_name.name,
265 access, ntohl(error));
268 if (exp && !IS_ERR(exp))
270 if (error == nfserr_stale)
271 nfsdstats.fh_stale++;
277 * Compose a file handle for an NFS reply.
279 * Note that when first composed, the dentry may not yet have
280 * an inode. In this case a call to fh_update should be made
281 * before the fh goes out on the wire ...
283 static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
284 __u32 *datap, int *maxsize)
286 if (dentry == exp->ex_dentry) {
291 return exportfs_encode_fh(dentry, datap, maxsize,
292 !(exp->ex_flags & NFSEXP_NOSUBTREECHECK));
296 * for composing old style file handles
298 static inline void _fh_update_old(struct dentry *dentry,
299 struct svc_export *exp,
302 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
303 fh->ofh_generation = dentry->d_inode->i_generation;
304 if (S_ISDIR(dentry->d_inode->i_mode) ||
305 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
310 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
311 struct svc_fh *ref_fh)
313 /* ref_fh is a reference file handle.
314 * if it is non-null and for the same filesystem, then we should compose
315 * a filehandle which is of the same version, where possible.
316 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
317 * Then create a 32byte filehandle using nfs_fhbase_old
323 struct inode * inode = dentry->d_inode;
324 struct dentry *parent = dentry->d_parent;
326 dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
327 int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
329 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
330 MAJOR(ex_dev), MINOR(ex_dev),
331 (long) exp->ex_dentry->d_inode->i_ino,
332 parent->d_name.name, dentry->d_name.name,
333 (inode ? inode->i_ino : 0));
335 /* Choose filehandle version and fsid type based on
336 * the reference filehandle (if it is in the same export)
337 * or the export options.
341 if (ref_fh && ref_fh->fh_export == exp) {
342 version = ref_fh->fh_handle.fh_version;
343 fsid_type = ref_fh->fh_handle.fh_fsid_type;
351 fsid_type = FSID_DEV;
359 /* Need to check that this type works for this
360 * export point. As the fsid -> filesystem mapping
361 * was guided by user-space, there is no guarantee
362 * that the filesystem actually supports that fsid
363 * type. If it doesn't we loop around again without
368 if (!old_valid_dev(ex_dev))
371 case FSID_MAJOR_MINOR:
372 case FSID_ENCODE_DEV:
373 if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags
378 if (! (exp->ex_flags & NFSEXP_FSID))
386 case FSID_UUID4_INUM:
387 case FSID_UUID16_INUM:
388 if (exp->ex_uuid == NULL)
392 } else if (exp->ex_uuid) {
393 if (fhp->fh_maxsize >= 64) {
395 fsid_type = FSID_UUID16;
397 fsid_type = FSID_UUID16_INUM;
400 fsid_type = FSID_UUID8;
402 fsid_type = FSID_UUID4_INUM;
404 } else if (exp->ex_flags & NFSEXP_FSID)
405 fsid_type = FSID_NUM;
406 else if (!old_valid_dev(ex_dev))
407 /* for newer device numbers, we must use a newer fsid format */
408 fsid_type = FSID_ENCODE_DEV;
410 fsid_type = FSID_DEV;
415 if (fhp->fh_locked || fhp->fh_dentry) {
416 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
417 parent->d_name.name, dentry->d_name.name);
419 if (fhp->fh_maxsize < NFS_FHSIZE)
420 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
422 parent->d_name.name, dentry->d_name.name);
424 fhp->fh_dentry = dget(dentry); /* our internal copy */
425 fhp->fh_export = exp;
428 if (version == 0xca) {
429 /* old style filehandle please */
430 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
431 fhp->fh_handle.fh_size = NFS_FHSIZE;
432 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
433 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
434 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
435 fhp->fh_handle.ofh_xino =
436 ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
437 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
439 _fh_update_old(dentry, exp, &fhp->fh_handle);
442 fhp->fh_handle.fh_version = 1;
443 fhp->fh_handle.fh_auth_type = 0;
444 datap = fhp->fh_handle.fh_auth+0;
445 fhp->fh_handle.fh_fsid_type = fsid_type;
446 mk_fsid(fsid_type, datap, ex_dev,
447 exp->ex_dentry->d_inode->i_ino,
448 exp->ex_fsid, exp->ex_uuid);
450 len = key_len(fsid_type);
452 fhp->fh_handle.fh_size = 4 + len;
455 int size = (fhp->fh_maxsize-len-4)/4;
456 fhp->fh_handle.fh_fileid_type =
457 _fh_update(dentry, exp, datap, &size);
458 fhp->fh_handle.fh_size += size*4;
460 if (fhp->fh_handle.fh_fileid_type == 255)
461 return nfserr_opnotsupp;
469 * Update file handle information after changing a dentry.
470 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
473 fh_update(struct svc_fh *fhp)
475 struct dentry *dentry;
481 dentry = fhp->fh_dentry;
482 if (!dentry->d_inode)
484 if (fhp->fh_handle.fh_version != 1) {
485 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
488 if (fhp->fh_handle.fh_fileid_type != 0)
490 datap = fhp->fh_handle.fh_auth+
491 fhp->fh_handle.fh_size/4 -1;
492 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
493 fhp->fh_handle.fh_fileid_type =
494 _fh_update(dentry, fhp->fh_export, datap, &size);
495 fhp->fh_handle.fh_size += size*4;
496 if (fhp->fh_handle.fh_fileid_type == 255)
497 return nfserr_opnotsupp;
503 printk(KERN_ERR "fh_update: fh not verified!\n");
506 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
507 dentry->d_parent->d_name.name, dentry->d_name.name);
512 * Release a file handle.
515 fh_put(struct svc_fh *fhp)
517 struct dentry * dentry = fhp->fh_dentry;
518 struct svc_export * exp = fhp->fh_export;
521 fhp->fh_dentry = NULL;
523 #ifdef CONFIG_NFSD_V3
524 fhp->fh_pre_saved = 0;
525 fhp->fh_post_saved = 0;
530 cache_put(&exp->h, &svc_export_cache);
531 fhp->fh_export = NULL;
537 * Shorthand for dprintk()'s
539 char * SVCFH_fmt(struct svc_fh *fhp)
541 struct knfsd_fh *fh = &fhp->fh_handle;
544 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
546 fh->fh_base.fh_pad[0],
547 fh->fh_base.fh_pad[1],
548 fh->fh_base.fh_pad[2],
549 fh->fh_base.fh_pad[3],
550 fh->fh_base.fh_pad[4],
551 fh->fh_base.fh_pad[5]);
555 enum fsid_source fsid_source(struct svc_fh *fhp)
557 if (fhp->fh_handle.fh_version != 1)
558 return FSIDSOURCE_DEV;
559 switch(fhp->fh_handle.fh_fsid_type) {
561 case FSID_ENCODE_DEV:
562 case FSID_MAJOR_MINOR:
563 return FSIDSOURCE_DEV;
565 return FSIDSOURCE_FSID;
567 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
568 return FSIDSOURCE_FSID;
570 return FSIDSOURCE_UUID;