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.
344 if (ref_fh && ref_fh->fh_export == exp) {
345 version = ref_fh->fh_handle.fh_version;
347 fsid_type = FSID_DEV;
349 fsid_type = ref_fh->fh_handle.fh_fsid_type;
350 /* We know this version/type works for this export
351 * so there is no need for further checks.
353 } else if (exp->ex_uuid) {
354 if (fhp->fh_maxsize >= 64) {
356 fsid_type = FSID_UUID16;
358 fsid_type = FSID_UUID16_INUM;
361 fsid_type = FSID_UUID8;
363 fsid_type = FSID_UUID4_INUM;
365 } else if (exp->ex_flags & NFSEXP_FSID)
366 fsid_type = FSID_NUM;
367 else if (!old_valid_dev(ex_dev))
368 /* for newer device numbers, we must use a newer fsid format */
369 fsid_type = FSID_ENCODE_DEV;
371 fsid_type = FSID_DEV;
376 if (fhp->fh_locked || fhp->fh_dentry) {
377 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
378 parent->d_name.name, dentry->d_name.name);
380 if (fhp->fh_maxsize < NFS_FHSIZE)
381 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
383 parent->d_name.name, dentry->d_name.name);
385 fhp->fh_dentry = dget(dentry); /* our internal copy */
386 fhp->fh_export = exp;
389 if (version == 0xca) {
390 /* old style filehandle please */
391 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
392 fhp->fh_handle.fh_size = NFS_FHSIZE;
393 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
394 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
395 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
396 fhp->fh_handle.ofh_xino =
397 ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
398 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
400 _fh_update_old(dentry, exp, &fhp->fh_handle);
403 fhp->fh_handle.fh_version = 1;
404 fhp->fh_handle.fh_auth_type = 0;
405 datap = fhp->fh_handle.fh_auth+0;
406 fhp->fh_handle.fh_fsid_type = fsid_type;
407 mk_fsid(fsid_type, datap, ex_dev,
408 exp->ex_dentry->d_inode->i_ino,
409 exp->ex_fsid, exp->ex_uuid);
411 len = key_len(fsid_type);
413 fhp->fh_handle.fh_size = 4 + len;
416 int size = (fhp->fh_maxsize-len-4)/4;
417 fhp->fh_handle.fh_fileid_type =
418 _fh_update(dentry, exp, datap, &size);
419 fhp->fh_handle.fh_size += size*4;
421 if (fhp->fh_handle.fh_fileid_type == 255)
422 return nfserr_opnotsupp;
430 * Update file handle information after changing a dentry.
431 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
434 fh_update(struct svc_fh *fhp)
436 struct dentry *dentry;
442 dentry = fhp->fh_dentry;
443 if (!dentry->d_inode)
445 if (fhp->fh_handle.fh_version != 1) {
446 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
449 if (fhp->fh_handle.fh_fileid_type != 0)
451 datap = fhp->fh_handle.fh_auth+
452 fhp->fh_handle.fh_size/4 -1;
453 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
454 fhp->fh_handle.fh_fileid_type =
455 _fh_update(dentry, fhp->fh_export, datap, &size);
456 fhp->fh_handle.fh_size += size*4;
457 if (fhp->fh_handle.fh_fileid_type == 255)
458 return nfserr_opnotsupp;
464 printk(KERN_ERR "fh_update: fh not verified!\n");
467 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
468 dentry->d_parent->d_name.name, dentry->d_name.name);
473 * Release a file handle.
476 fh_put(struct svc_fh *fhp)
478 struct dentry * dentry = fhp->fh_dentry;
479 struct svc_export * exp = fhp->fh_export;
482 fhp->fh_dentry = NULL;
484 #ifdef CONFIG_NFSD_V3
485 fhp->fh_pre_saved = 0;
486 fhp->fh_post_saved = 0;
491 cache_put(&exp->h, &svc_export_cache);
492 fhp->fh_export = NULL;
498 * Shorthand for dprintk()'s
500 char * SVCFH_fmt(struct svc_fh *fhp)
502 struct knfsd_fh *fh = &fhp->fh_handle;
505 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
507 fh->fh_base.fh_pad[0],
508 fh->fh_base.fh_pad[1],
509 fh->fh_base.fh_pad[2],
510 fh->fh_base.fh_pad[3],
511 fh->fh_base.fh_pad[4],
512 fh->fh_base.fh_pad[5]);
516 enum fsid_source fsid_source(struct svc_fh *fhp)
518 if (fhp->fh_handle.fh_version != 1)
519 return FSIDSOURCE_DEV;
520 switch(fhp->fh_handle.fh_fsid_type) {
522 case FSID_ENCODE_DEV:
523 case FSID_MAJOR_MINOR:
524 return FSIDSOURCE_DEV;
526 return FSIDSOURCE_FSID;
528 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
529 return FSIDSOURCE_FSID;
531 return FSIDSOURCE_UUID;