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/mount.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/sunrpc/svc.h>
22 #include <linux/nfsd/nfsd.h>
24 #define NFSDDBG_FACILITY NFSDDBG_FH
27 static int nfsd_nr_verified;
28 static int nfsd_nr_put;
30 extern struct export_operations export_op_default;
32 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
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
40 static int nfsd_acceptable(void *expv, struct dentry *dentry)
42 struct svc_export *exp = expv;
44 struct dentry *tdentry;
45 struct dentry *parent;
47 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
50 tdentry = dget(dentry);
51 while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
52 /* make sure parents give x permission to user */
54 parent = dget_parent(tdentry);
55 err = permission(parent->d_inode, MAY_EXEC, NULL);
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);
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
76 nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
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)
84 else if ((mode & S_IFMT) == S_IFDIR)
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)
101 * Perform sanity checks on the dentry in a client's file handle.
103 * Note that the file handle dentry may need to be freed even after
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.
110 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
112 struct knfsd_fh *fh = &fhp->fh_handle;
113 struct svc_export *exp = NULL;
114 struct dentry *dentry;
117 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
119 if (!fhp->fh_dentry) {
121 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
123 int data_left = fh->fh_size/4;
125 error = nfserr_stale;
126 if (rqstp->rq_client == NULL)
128 if (rqstp->rq_vers > 2)
129 error = nfserr_badhandle;
130 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
131 return nfserr_nofilehandle;
133 if (fh->fh_version == 1) {
136 if (--data_left<0) goto out;
137 switch (fh->fh_auth_type) {
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];
150 if ((data_left -= len)<0) goto out;
151 exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
156 if (fh->fh_size != NFS_FHSIZE)
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,
166 if (IS_ERR(exp) && (PTR_ERR(exp) == -EAGAIN
167 || PTR_ERR(exp) == -ETIMEDOUT)) {
168 error = nfserrno(PTR_ERR(exp));
172 error = nfserr_stale;
173 if (!exp || IS_ERR(exp))
176 /* Check if the request originated from a secure port. */
178 if (!rqstp->rq_secure && EX_SECURE(exp)) {
179 char buf[RPC_MAX_ADDRBUFLEN];
181 "nfsd: request from insecure port %s!\n",
182 svc_print_addr(rqstp, buf, sizeof(buf)));
186 /* Set user creds for this exportpoint */
187 error = nfserrno(nfsd_setuser(rqstp, exp));
192 * Look up the dentry using the NFS file handle.
194 error = nfserr_stale;
195 if (rqstp->rq_vers > 2)
196 error = nfserr_badhandle;
198 if (fh->fh_version != 1) {
199 tfh[0] = fh->ofh_ino;
200 tfh[1] = fh->ofh_generation;
201 tfh[2] = fh->ofh_dirino;
204 if (fh->ofh_dirino == 0)
209 fileid_type = fh->fh_fileid_type;
211 if (fileid_type == 0)
212 dentry = dget(exp->ex_dentry);
214 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
215 dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
218 nfsd_acceptable, exp);
222 if (IS_ERR(dentry)) {
223 if (PTR_ERR(dentry) != -EINVAL)
224 error = nfserrno(PTR_ERR(dentry));
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);
234 fhp->fh_dentry = dentry;
235 fhp->fh_export = exp;
238 /* just rechecking permissions
239 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
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
249 error = nfserrno(nfsd_setuser(rqstp, exp));
256 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
260 /* Finally, check access permissions. */
261 error = nfsd_permission(exp, dentry, access);
264 dprintk("fh_verify: %s/%s permission failure, "
265 "acc=%x, error=%d\n",
266 dentry->d_parent->d_name.name,
268 access, ntohl(error));
271 if (exp && !IS_ERR(exp))
273 if (error == nfserr_stale)
274 nfsdstats.fh_stale++;
280 * Compose a file handle for an NFS reply.
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 ...
286 static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
287 __u32 *datap, int *maxsize)
289 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
291 if (dentry == exp->ex_dentry) {
296 return CALL(nop,encode_fh)(dentry, datap, maxsize,
297 !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
301 * for composing old style file handles
303 static inline void _fh_update_old(struct dentry *dentry,
304 struct svc_export *exp,
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))
315 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
316 struct svc_fh *ref_fh)
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
328 struct inode * inode = dentry->d_inode;
329 struct dentry *parent = dentry->d_parent;
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);
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));
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.
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;
356 fsid_type = FSID_DEV;
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
373 if (!old_valid_dev(ex_dev))
376 case FSID_MAJOR_MINOR:
377 case FSID_ENCODE_DEV:
378 if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags
383 if (! (exp->ex_flags & NFSEXP_FSID))
391 case FSID_UUID4_INUM:
392 case FSID_UUID16_INUM:
393 if (exp->ex_uuid == NULL)
397 } else if (exp->ex_uuid) {
398 if (fhp->fh_maxsize >= 64) {
400 fsid_type = FSID_UUID16;
402 fsid_type = FSID_UUID16_INUM;
405 fsid_type = FSID_UUID8;
407 fsid_type = FSID_UUID4_INUM;
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;
415 fsid_type = FSID_DEV;
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);
424 if (fhp->fh_maxsize < NFS_FHSIZE)
425 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
427 parent->d_name.name, dentry->d_name.name);
429 fhp->fh_dentry = dget(dentry); /* our internal copy */
430 fhp->fh_export = exp;
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));
444 _fh_update_old(dentry, exp, &fhp->fh_handle);
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);
455 len = key_len(fsid_type);
457 fhp->fh_handle.fh_size = 4 + len;
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;
465 if (fhp->fh_handle.fh_fileid_type == 255)
466 return nfserr_opnotsupp;
474 * Update file handle information after changing a dentry.
475 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
478 fh_update(struct svc_fh *fhp)
480 struct dentry *dentry;
486 dentry = fhp->fh_dentry;
487 if (!dentry->d_inode)
489 if (fhp->fh_handle.fh_version != 1) {
490 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
493 if (fhp->fh_handle.fh_fileid_type != 0)
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;
508 printk(KERN_ERR "fh_update: fh not verified!\n");
511 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
512 dentry->d_parent->d_name.name, dentry->d_name.name);
517 * Release a file handle.
520 fh_put(struct svc_fh *fhp)
522 struct dentry * dentry = fhp->fh_dentry;
523 struct svc_export * exp = fhp->fh_export;
526 fhp->fh_dentry = NULL;
528 #ifdef CONFIG_NFSD_V3
529 fhp->fh_pre_saved = 0;
530 fhp->fh_post_saved = 0;
535 cache_put(&exp->h, &svc_export_cache);
536 fhp->fh_export = NULL;
542 * Shorthand for dprintk()'s
544 char * SVCFH_fmt(struct svc_fh *fhp)
546 struct knfsd_fh *fh = &fhp->fh_handle;
549 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
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]);
560 enum fsid_source fsid_source(struct svc_fh *fhp)
562 if (fhp->fh_handle.fh_version != 1)
563 return FSIDSOURCE_DEV;
564 switch(fhp->fh_handle.fh_fsid_type) {
566 case FSID_ENCODE_DEV:
567 case FSID_MAJOR_MINOR:
568 return FSIDSOURCE_DEV;
570 return FSIDSOURCE_FSID;
572 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
573 return FSIDSOURCE_FSID;
575 return FSIDSOURCE_UUID;