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/sched.h>
13 #include <linux/slab.h>
14 #include <linux/smp_lock.h>
16 #include <linux/unistd.h>
17 #include <linux/string.h>
18 #include <linux/stat.h>
19 #include <linux/dcache.h>
20 #include <linux/mount.h>
21 #include <asm/pgtable.h>
23 #include <linux/sunrpc/svc.h>
24 #include <linux/nfsd/nfsd.h>
26 #define NFSDDBG_FACILITY NFSDDBG_FH
27 #define NFSD_PARANOIA 1
28 /* #define NFSD_DEBUG_VERBOSE 1 */
31 static int nfsd_nr_verified;
32 static int nfsd_nr_put;
34 extern struct export_operations export_op_default;
36 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
39 * our acceptability function.
40 * if NOSUBTREECHECK, accept anything
41 * if not, require that we can walk up to exp->ex_dentry
42 * doing some checks on the 'x' bits
44 int nfsd_acceptable(void *expv, struct dentry *dentry)
46 struct svc_export *exp = expv;
48 struct dentry *tdentry;
49 struct dentry *parent;
51 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
54 tdentry = dget(dentry);
55 while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
56 /* make sure parents give x permission to user */
58 parent = dget_parent(tdentry);
59 err = permission(parent->d_inode, MAY_EXEC, NULL);
67 if (tdentry != exp->ex_dentry)
68 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
69 rv = (tdentry == exp->ex_dentry);
76 * Perform sanity checks on the dentry in a client's file handle.
78 * Note that the file handle dentry may need to be freed even after
81 * This is only called at the start of an nfsproc call, so fhp points to
82 * a svc_fh which is all 0 except for the over-the-wire file handle.
85 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
87 struct knfsd_fh *fh = &fhp->fh_handle;
88 struct svc_export *exp = NULL;
89 struct dentry *dentry;
93 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
95 /* keep this filehandle for possible reference when encoding attributes */
98 if (!fhp->fh_dentry) {
100 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
102 int data_left = fh->fh_size/4;
104 error = nfserr_stale;
105 if (rqstp->rq_client == NULL)
107 if (rqstp->rq_vers > 2)
108 error = nfserr_badhandle;
109 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
110 return nfserr_nofilehandle;
112 if (fh->fh_version == 1) {
115 if (--data_left<0) goto out;
116 switch (fh->fh_auth_type) {
120 len = key_len(fh->fh_fsid_type) / 4;
121 if (len == 0) goto out;
122 if (fh->fh_fsid_type == 2) {
123 /* deprecated, convert to type 3 */
125 fh->fh_fsid_type = 3;
126 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
127 fh->fh_fsid[1] = fh->fh_fsid[2];
129 if ((data_left -= len)<0) goto out;
130 exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
135 if (fh->fh_size != NFS_FHSIZE)
137 /* assume old filehandle format */
138 xdev = old_decode_dev(fh->ofh_xdev);
139 xino = u32_to_ino_t(fh->ofh_xino);
140 mk_fsid_v0(tfh, xdev, xino);
141 exp = exp_find(rqstp->rq_client, 0, tfh, &rqstp->rq_chandle);
144 error = nfserr_dropit;
145 if (IS_ERR(exp) && PTR_ERR(exp) == -EAGAIN) {
146 dprintk("nfsd: fh_verify failed: nfserr_dropit\n");
149 error = nfserr_stale;
150 if (!exp || IS_ERR(exp)) {
151 dprintk("nfsd: fh_verify failed: nfserr_stale\n");
154 /* Check if the request originated from a secure port. */
156 if (!rqstp->rq_secure && EX_SECURE(exp)) {
158 "nfsd: request from insecure port (%u.%u.%u.%u:%d)!\n",
159 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
160 ntohs(rqstp->rq_addr.sin_port));
164 /* Set user creds for this exportpoint */
165 error = nfsd_setuser(rqstp, exp);
167 dprintk("nfsd: nfsd_setuser failed: %d\n", error);
168 error = nfserrno(error);
173 * Look up the dentry using the NFS file handle.
175 error = nfserr_stale;
176 if (rqstp->rq_vers > 2)
177 error = nfserr_badhandle;
179 if (fh->fh_version != 1) {
180 tfh[0] = fh->ofh_ino;
181 tfh[1] = fh->ofh_generation;
182 tfh[2] = fh->ofh_dirino;
185 if (fh->ofh_dirino == 0)
190 fileid_type = fh->fh_fileid_type;
192 if (fileid_type == 0)
193 dentry = dget(exp->ex_dentry);
195 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
196 dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
199 nfsd_acceptable, exp);
203 if (IS_ERR(dentry)) {
204 dprintk("nfsd: CALL(nop,decode_fh) failed: %ld\n", PTR_ERR(dentry));
205 if (PTR_ERR(dentry) != -EINVAL)
206 error = nfserrno(PTR_ERR(dentry));
210 if (S_ISDIR(dentry->d_inode->i_mode) &&
211 (dentry->d_flags & DCACHE_DISCONNECTED)) {
212 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
213 dentry->d_parent->d_name.name, dentry->d_name.name);
217 fhp->fh_dentry = dentry;
218 fhp->fh_export = exp;
221 /* just rechecking permissions
222 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
224 dprintk("nfsd: fh_verify - just checking\n");
225 dentry = fhp->fh_dentry;
226 exp = fhp->fh_export;
230 inode = dentry->d_inode;
233 /* Type check. The correct error return for type mismatches
234 * does not seem to be generally agreed upon. SunOS seems to
235 * use EISDIR if file isn't S_IFREG; a comment in the NFSv3
236 * spec says this is incorrect (implementation notes for the
240 /* Type can be negative when creating hardlinks - not to a dir */
241 if (type > 0 && (inode->i_mode & S_IFMT) != type) {
242 if (rqstp->rq_vers == 4 && (inode->i_mode & S_IFMT) == S_IFLNK)
243 error = nfserr_symlink;
244 else if (type == S_IFDIR)
245 error = nfserr_notdir;
246 else if ((inode->i_mode & S_IFMT) == S_IFDIR)
247 error = nfserr_isdir;
249 error = nfserr_inval;
250 dprintk("nfsd: bad type: %d\n", ntohl(error));
253 if (type < 0 && (inode->i_mode & S_IFMT) == -type) {
254 if (rqstp->rq_vers == 4 && (inode->i_mode & S_IFMT) == S_IFLNK)
255 error = nfserr_symlink;
256 else if (type == -S_IFDIR)
257 error = nfserr_isdir;
259 error = nfserr_notdir;
260 dprintk("nfsd: bad type2: %d\n", ntohl(error));
264 /* Finally, check access permissions. */
265 error = nfsd_permission(exp, dentry, access);
267 #ifdef NFSD_PARANOIA_EXTREME
269 printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
270 dentry->d_parent->d_name.name, dentry->d_name.name, access, (error >> 24));
274 if (exp && !IS_ERR(exp))
276 if (error == nfserr_stale)
277 nfsdstats.fh_stale++;
283 * Compose a file handle for an NFS reply.
285 * Note that when first composed, the dentry may not yet have
286 * an inode. In this case a call to fh_update should be made
287 * before the fh goes out on the wire ...
289 inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
290 __u32 *datap, int *maxsize)
292 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
294 if (dentry == exp->ex_dentry) {
299 return CALL(nop,encode_fh)(dentry, datap, maxsize,
300 !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
304 * for composing old style file handles
306 inline void _fh_update_old(struct dentry *dentry, struct svc_export *exp,
309 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
310 fh->ofh_generation = dentry->d_inode->i_generation;
311 if (S_ISDIR(dentry->d_inode->i_mode) ||
312 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
317 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, struct svc_fh *ref_fh)
319 /* ref_fh is a reference file handle.
320 * if it is non-null, then we should compose a filehandle which is
321 * of the same version, where possible.
322 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
323 * Then create a 32byte filehandle using nfs_fhbase_old
327 u8 ref_fh_version = 0;
328 u8 ref_fh_fsid_type = 0;
329 struct inode * inode = dentry->d_inode;
330 struct dentry *parent = dentry->d_parent;
332 dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
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));
341 ref_fh_version = ref_fh->fh_handle.fh_version;
342 if (ref_fh_version == 0xca)
343 ref_fh_fsid_type = 0;
345 ref_fh_fsid_type = ref_fh->fh_handle.fh_fsid_type;
346 if (ref_fh_fsid_type > 3)
347 ref_fh_fsid_type = 0;
349 /* make sure ref_fh type works for given export */
350 if (ref_fh_fsid_type == 1 &&
351 !(exp->ex_flags & NFSEXP_FSID)) {
352 /* if we don't have an fsid, we cannot provide one... */
353 ref_fh_fsid_type = 0;
355 } else if (exp->ex_flags & NFSEXP_FSID)
356 ref_fh_fsid_type = 1;
358 if (!old_valid_dev(ex_dev) && ref_fh_fsid_type == 0) {
359 /* for newer device numbers, we must use a newer fsid format */
361 ref_fh_fsid_type = 3;
363 if (old_valid_dev(ex_dev) &&
364 (ref_fh_fsid_type == 2 || ref_fh_fsid_type == 3))
365 /* must use type1 for smaller device numbers */
366 ref_fh_fsid_type = 0;
371 if (fhp->fh_locked || fhp->fh_dentry) {
372 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
373 parent->d_name.name, dentry->d_name.name);
375 if (fhp->fh_maxsize < NFS_FHSIZE)
376 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
377 fhp->fh_maxsize, parent->d_name.name, dentry->d_name.name);
379 fhp->fh_dentry = dget(dentry); /* our internal copy */
380 fhp->fh_export = exp;
383 if (ref_fh_version == 0xca) {
384 /* old style filehandle please */
385 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
386 fhp->fh_handle.fh_size = NFS_FHSIZE;
387 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
388 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
389 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
390 fhp->fh_handle.ofh_xino = ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
391 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
393 _fh_update_old(dentry, exp, &fhp->fh_handle);
396 fhp->fh_handle.fh_version = 1;
397 fhp->fh_handle.fh_auth_type = 0;
398 datap = fhp->fh_handle.fh_auth+0;
399 fhp->fh_handle.fh_fsid_type = ref_fh_fsid_type;
400 switch (ref_fh_fsid_type) {
404 * 2byte major, 2byte minor, 4byte inode
406 mk_fsid_v0(datap, ex_dev,
407 exp->ex_dentry->d_inode->i_ino);
410 /* fsid_type 1 == 4 bytes filesystem id */
411 mk_fsid_v1(datap, exp->ex_fsid);
416 * 4byte major, 4byte minor, 4byte inode
418 mk_fsid_v2(datap, ex_dev,
419 exp->ex_dentry->d_inode->i_ino);
424 * 4byte devicenumber, 4byte inode
426 mk_fsid_v3(datap, ex_dev,
427 exp->ex_dentry->d_inode->i_ino);
430 len = key_len(ref_fh_fsid_type);
432 fhp->fh_handle.fh_size = 4 + len;
435 int size = (fhp->fh_maxsize-len-4)/4;
436 fhp->fh_handle.fh_fileid_type =
437 _fh_update(dentry, exp, datap, &size);
438 fhp->fh_handle.fh_size += size*4;
440 if (fhp->fh_handle.fh_fileid_type == 255)
441 return nfserr_opnotsupp;
449 * Update file handle information after changing a dentry.
450 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
453 fh_update(struct svc_fh *fhp)
455 struct dentry *dentry;
461 dentry = fhp->fh_dentry;
462 if (!dentry->d_inode)
464 if (fhp->fh_handle.fh_version != 1) {
465 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
468 if (fhp->fh_handle.fh_fileid_type != 0)
470 datap = fhp->fh_handle.fh_auth+
471 fhp->fh_handle.fh_size/4 -1;
472 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
473 fhp->fh_handle.fh_fileid_type =
474 _fh_update(dentry, fhp->fh_export, datap, &size);
475 fhp->fh_handle.fh_size += size*4;
476 if (fhp->fh_handle.fh_fileid_type == 255)
477 return nfserr_opnotsupp;
483 printk(KERN_ERR "fh_update: fh not verified!\n");
486 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
487 dentry->d_parent->d_name.name, dentry->d_name.name);
490 printk(KERN_ERR "fh_update: %s/%s already up-to-date!\n",
491 dentry->d_parent->d_name.name, dentry->d_name.name);
496 * Release a file handle.
499 fh_put(struct svc_fh *fhp)
501 struct dentry * dentry = fhp->fh_dentry;
502 struct svc_export * exp = fhp->fh_export;
505 fhp->fh_dentry = NULL;
507 #ifdef CONFIG_NFSD_V3
508 fhp->fh_pre_saved = 0;
509 fhp->fh_post_saved = 0;
514 svc_export_put(&exp->h, &svc_export_cache);
515 fhp->fh_export = NULL;
521 * Shorthand for dprintk()'s
523 char * SVCFH_fmt(struct svc_fh *fhp)
525 struct knfsd_fh *fh = &fhp->fh_handle;
528 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
530 fh->fh_base.fh_pad[0],
531 fh->fh_base.fh_pad[1],
532 fh->fh_base.fh_pad[2],
533 fh->fh_base.fh_pad[3],
534 fh->fh_base.fh_pad[4],
535 fh->fh_base.fh_pad[5]);