Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / nfsd / nfsfh.c
blobbca2f4ad581feb8ff8c377cad70b235705bf6159
1 /*
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>
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/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>
25 #include "auth.h"
27 #define NFSDDBG_FACILITY NFSDDBG_FH
30 static int nfsd_nr_verified;
31 static int nfsd_nr_put;
34 * our acceptability function.
35 * if NOSUBTREECHECK, accept anything
36 * if not, require that we can walk up to exp->ex_dentry
37 * doing some checks on the 'x' bits
39 static int nfsd_acceptable(void *expv, struct dentry *dentry)
41 struct svc_export *exp = expv;
42 int rv;
43 struct dentry *tdentry;
44 struct dentry *parent;
46 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
47 return 1;
49 tdentry = dget(dentry);
50 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
51 /* make sure parents give x permission to user */
52 int err;
53 parent = dget_parent(tdentry);
54 err = permission(parent->d_inode, MAY_EXEC, NULL);
55 if (err < 0) {
56 dput(parent);
57 break;
59 dput(tdentry);
60 tdentry = parent;
62 if (tdentry != exp->ex_path.dentry)
63 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
64 rv = (tdentry == exp->ex_path.dentry);
65 dput(tdentry);
66 return rv;
69 /* Type check. The correct error return for type mismatches does not seem to be
70 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
71 * comment in the NFSv3 spec says this is incorrect (implementation notes for
72 * the write call).
74 static inline __be32
75 nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
77 /* Type can be negative when creating hardlinks - not to a dir */
78 if (type > 0 && (mode & S_IFMT) != type) {
79 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
80 return nfserr_symlink;
81 else if (type == S_IFDIR)
82 return nfserr_notdir;
83 else if ((mode & S_IFMT) == S_IFDIR)
84 return nfserr_isdir;
85 else
86 return nfserr_inval;
88 if (type < 0 && (mode & S_IFMT) == -type) {
89 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
90 return nfserr_symlink;
91 else if (type == -S_IFDIR)
92 return nfserr_isdir;
93 else
94 return nfserr_notdir;
96 return 0;
99 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
100 struct svc_export *exp)
102 /* Check if the request originated from a secure port. */
103 if (!rqstp->rq_secure && EX_SECURE(exp)) {
104 <<<<<<< HEAD:fs/nfsd/nfsfh.c
105 char buf[RPC_MAX_ADDRBUFLEN];
106 =======
107 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
108 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfsd/nfsfh.c
109 dprintk(KERN_WARNING
110 "nfsd: request from insecure port %s!\n",
111 svc_print_addr(rqstp, buf, sizeof(buf)));
112 return nfserr_perm;
115 /* Set user creds for this exportpoint */
116 return nfserrno(nfsd_setuser(rqstp, exp));
120 * Perform sanity checks on the dentry in a client's file handle.
122 * Note that the file handle dentry may need to be freed even after
123 * an error return.
125 * This is only called at the start of an nfsproc call, so fhp points to
126 * a svc_fh which is all 0 except for the over-the-wire file handle.
128 __be32
129 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
131 struct knfsd_fh *fh = &fhp->fh_handle;
132 struct svc_export *exp = NULL;
133 struct dentry *dentry;
134 __be32 error = 0;
136 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
138 if (!fhp->fh_dentry) {
139 struct fid *fid = NULL, sfid;
140 int fileid_type;
141 int data_left = fh->fh_size/4;
143 error = nfserr_stale;
144 if (rqstp->rq_vers > 2)
145 error = nfserr_badhandle;
146 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
147 return nfserr_nofilehandle;
149 if (fh->fh_version == 1) {
150 int len;
151 if (--data_left<0) goto out;
152 switch (fh->fh_auth_type) {
153 case 0: break;
154 default: goto out;
156 len = key_len(fh->fh_fsid_type) / 4;
157 if (len == 0) goto out;
158 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
159 /* deprecated, convert to type 3 */
160 len = key_len(FSID_ENCODE_DEV)/4;
161 fh->fh_fsid_type = FSID_ENCODE_DEV;
162 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
163 fh->fh_fsid[1] = fh->fh_fsid[2];
165 if ((data_left -= len)<0) goto out;
166 exp = rqst_exp_find(rqstp, fh->fh_fsid_type,
167 fh->fh_auth);
168 fid = (struct fid *)(fh->fh_auth + len);
169 } else {
170 __u32 tfh[2];
171 dev_t xdev;
172 ino_t xino;
173 if (fh->fh_size != NFS_FHSIZE)
174 goto out;
175 /* assume old filehandle format */
176 xdev = old_decode_dev(fh->ofh_xdev);
177 xino = u32_to_ino_t(fh->ofh_xino);
178 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
179 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
182 error = nfserr_stale;
183 if (PTR_ERR(exp) == -ENOENT)
184 goto out;
186 if (IS_ERR(exp)) {
187 error = nfserrno(PTR_ERR(exp));
188 goto out;
191 error = nfsd_setuser_and_check_port(rqstp, exp);
192 if (error)
193 goto out;
196 * Look up the dentry using the NFS file handle.
198 error = nfserr_stale;
199 if (rqstp->rq_vers > 2)
200 error = nfserr_badhandle;
202 if (fh->fh_version != 1) {
203 sfid.i32.ino = fh->ofh_ino;
204 sfid.i32.gen = fh->ofh_generation;
205 sfid.i32.parent_ino = fh->ofh_dirino;
206 fid = &sfid;
207 data_left = 3;
208 if (fh->ofh_dirino == 0)
209 fileid_type = FILEID_INO32_GEN;
210 else
211 fileid_type = FILEID_INO32_GEN_PARENT;
212 } else
213 fileid_type = fh->fh_fileid_type;
215 if (fileid_type == FILEID_ROOT)
216 dentry = dget(exp->ex_path.dentry);
217 else {
218 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
219 data_left, fileid_type,
220 nfsd_acceptable, exp);
222 if (dentry == NULL)
223 goto out;
224 if (IS_ERR(dentry)) {
225 if (PTR_ERR(dentry) != -EINVAL)
226 error = nfserrno(PTR_ERR(dentry));
227 goto out;
230 if (S_ISDIR(dentry->d_inode->i_mode) &&
231 (dentry->d_flags & DCACHE_DISCONNECTED)) {
232 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
233 dentry->d_parent->d_name.name, dentry->d_name.name);
236 fhp->fh_dentry = dentry;
237 fhp->fh_export = exp;
238 nfsd_nr_verified++;
239 <<<<<<< HEAD:fs/nfsd/nfsfh.c
240 =======
241 cache_get(&exp->h);
242 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfsd/nfsfh.c
243 } else {
245 * just rechecking permissions
246 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
247 * does as well)
249 dprintk("nfsd: fh_verify - just checking\n");
250 dentry = fhp->fh_dentry;
251 exp = fhp->fh_export;
252 <<<<<<< HEAD:fs/nfsd/nfsfh.c
253 =======
254 cache_get(&exp->h);
255 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfsd/nfsfh.c
257 * Set user creds for this exportpoint; necessary even
258 * in the "just checking" case because this may be a
259 * filehandle that was created by fh_compose, and that
260 * is about to be used in another nfsv4 compound
261 * operation.
263 error = nfsd_setuser_and_check_port(rqstp, exp);
264 if (error)
265 goto out;
267 <<<<<<< HEAD:fs/nfsd/nfsfh.c
268 cache_get(&exp->h);
270 =======
271 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfsd/nfsfh.c
273 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
274 if (error)
275 goto out;
277 if (!(access & MAY_LOCK)) {
279 * pseudoflavor restrictions are not enforced on NLM,
280 * which clients virtually always use auth_sys for,
281 * even while using RPCSEC_GSS for NFS.
283 error = check_nfsd_access(exp, rqstp);
284 if (error)
285 goto out;
288 /* Finally, check access permissions. */
289 error = nfsd_permission(rqstp, exp, dentry, access);
291 if (error) {
292 dprintk("fh_verify: %s/%s permission failure, "
293 "acc=%x, error=%d\n",
294 dentry->d_parent->d_name.name,
295 dentry->d_name.name,
296 access, ntohl(error));
298 out:
299 if (exp && !IS_ERR(exp))
300 exp_put(exp);
301 if (error == nfserr_stale)
302 nfsdstats.fh_stale++;
303 return error;
308 * Compose a file handle for an NFS reply.
310 * Note that when first composed, the dentry may not yet have
311 * an inode. In this case a call to fh_update should be made
312 * before the fh goes out on the wire ...
314 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
315 struct dentry *dentry)
317 if (dentry != exp->ex_path.dentry) {
318 struct fid *fid = (struct fid *)
319 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
320 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
321 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
323 fhp->fh_handle.fh_fileid_type =
324 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
325 fhp->fh_handle.fh_size += maxsize * 4;
326 } else {
327 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
332 * for composing old style file handles
334 static inline void _fh_update_old(struct dentry *dentry,
335 struct svc_export *exp,
336 struct knfsd_fh *fh)
338 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
339 fh->ofh_generation = dentry->d_inode->i_generation;
340 if (S_ISDIR(dentry->d_inode->i_mode) ||
341 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
342 fh->ofh_dirino = 0;
345 __be32
346 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
347 struct svc_fh *ref_fh)
349 /* ref_fh is a reference file handle.
350 * if it is non-null and for the same filesystem, then we should compose
351 * a filehandle which is of the same version, where possible.
352 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
353 * Then create a 32byte filehandle using nfs_fhbase_old
357 u8 version;
358 u8 fsid_type = 0;
359 struct inode * inode = dentry->d_inode;
360 struct dentry *parent = dentry->d_parent;
361 __u32 *datap;
362 dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev;
363 int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root);
365 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
366 MAJOR(ex_dev), MINOR(ex_dev),
367 (long) exp->ex_path.dentry->d_inode->i_ino,
368 parent->d_name.name, dentry->d_name.name,
369 (inode ? inode->i_ino : 0));
371 /* Choose filehandle version and fsid type based on
372 * the reference filehandle (if it is in the same export)
373 * or the export options.
375 retry:
376 version = 1;
377 if (ref_fh && ref_fh->fh_export == exp) {
378 version = ref_fh->fh_handle.fh_version;
379 fsid_type = ref_fh->fh_handle.fh_fsid_type;
381 if (ref_fh == fhp)
382 fh_put(ref_fh);
383 ref_fh = NULL;
385 switch (version) {
386 case 0xca:
387 fsid_type = FSID_DEV;
388 break;
389 case 1:
390 break;
391 default:
392 goto retry;
395 /* Need to check that this type works for this
396 * export point. As the fsid -> filesystem mapping
397 * was guided by user-space, there is no guarantee
398 * that the filesystem actually supports that fsid
399 * type. If it doesn't we loop around again without
400 * ref_fh set.
402 switch(fsid_type) {
403 case FSID_DEV:
404 if (!old_valid_dev(ex_dev))
405 goto retry;
406 /* FALL THROUGH */
407 case FSID_MAJOR_MINOR:
408 case FSID_ENCODE_DEV:
409 if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
410 & FS_REQUIRES_DEV))
411 goto retry;
412 break;
413 case FSID_NUM:
414 if (! (exp->ex_flags & NFSEXP_FSID))
415 goto retry;
416 break;
417 case FSID_UUID8:
418 case FSID_UUID16:
419 if (!root_export)
420 goto retry;
421 /* fall through */
422 case FSID_UUID4_INUM:
423 case FSID_UUID16_INUM:
424 if (exp->ex_uuid == NULL)
425 goto retry;
426 break;
428 } else if (exp->ex_uuid) {
429 if (fhp->fh_maxsize >= 64) {
430 if (root_export)
431 fsid_type = FSID_UUID16;
432 else
433 fsid_type = FSID_UUID16_INUM;
434 } else {
435 if (root_export)
436 fsid_type = FSID_UUID8;
437 else
438 fsid_type = FSID_UUID4_INUM;
440 } else if (exp->ex_flags & NFSEXP_FSID)
441 fsid_type = FSID_NUM;
442 else if (!old_valid_dev(ex_dev))
443 /* for newer device numbers, we must use a newer fsid format */
444 fsid_type = FSID_ENCODE_DEV;
445 else
446 fsid_type = FSID_DEV;
448 if (ref_fh == fhp)
449 fh_put(ref_fh);
451 if (fhp->fh_locked || fhp->fh_dentry) {
452 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
453 parent->d_name.name, dentry->d_name.name);
455 if (fhp->fh_maxsize < NFS_FHSIZE)
456 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
457 fhp->fh_maxsize,
458 parent->d_name.name, dentry->d_name.name);
460 fhp->fh_dentry = dget(dentry); /* our internal copy */
461 fhp->fh_export = exp;
462 cache_get(&exp->h);
464 if (version == 0xca) {
465 /* old style filehandle please */
466 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
467 fhp->fh_handle.fh_size = NFS_FHSIZE;
468 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
469 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
470 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
471 fhp->fh_handle.ofh_xino =
472 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
473 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
474 if (inode)
475 _fh_update_old(dentry, exp, &fhp->fh_handle);
476 } else {
477 int len;
478 fhp->fh_handle.fh_version = 1;
479 fhp->fh_handle.fh_auth_type = 0;
480 datap = fhp->fh_handle.fh_auth+0;
481 fhp->fh_handle.fh_fsid_type = fsid_type;
482 mk_fsid(fsid_type, datap, ex_dev,
483 exp->ex_path.dentry->d_inode->i_ino,
484 exp->ex_fsid, exp->ex_uuid);
486 len = key_len(fsid_type);
487 datap += len/4;
488 fhp->fh_handle.fh_size = 4 + len;
490 if (inode)
491 _fh_update(fhp, exp, dentry);
492 if (fhp->fh_handle.fh_fileid_type == 255)
493 return nfserr_opnotsupp;
496 nfsd_nr_verified++;
497 return 0;
501 * Update file handle information after changing a dentry.
502 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
504 __be32
505 fh_update(struct svc_fh *fhp)
507 struct dentry *dentry;
509 if (!fhp->fh_dentry)
510 goto out_bad;
512 dentry = fhp->fh_dentry;
513 if (!dentry->d_inode)
514 goto out_negative;
515 if (fhp->fh_handle.fh_version != 1) {
516 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
517 } else {
518 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
519 goto out;
521 _fh_update(fhp, fhp->fh_export, dentry);
522 if (fhp->fh_handle.fh_fileid_type == 255)
523 return nfserr_opnotsupp;
525 out:
526 return 0;
528 out_bad:
529 printk(KERN_ERR "fh_update: fh not verified!\n");
530 goto out;
531 out_negative:
532 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
533 dentry->d_parent->d_name.name, dentry->d_name.name);
534 goto out;
538 * Release a file handle.
540 void
541 fh_put(struct svc_fh *fhp)
543 struct dentry * dentry = fhp->fh_dentry;
544 struct svc_export * exp = fhp->fh_export;
545 if (dentry) {
546 fh_unlock(fhp);
547 fhp->fh_dentry = NULL;
548 dput(dentry);
549 #ifdef CONFIG_NFSD_V3
550 fhp->fh_pre_saved = 0;
551 fhp->fh_post_saved = 0;
552 #endif
553 nfsd_nr_put++;
555 if (exp) {
556 cache_put(&exp->h, &svc_export_cache);
557 fhp->fh_export = NULL;
559 return;
563 * Shorthand for dprintk()'s
565 char * SVCFH_fmt(struct svc_fh *fhp)
567 struct knfsd_fh *fh = &fhp->fh_handle;
569 static char buf[80];
570 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
571 fh->fh_size,
572 fh->fh_base.fh_pad[0],
573 fh->fh_base.fh_pad[1],
574 fh->fh_base.fh_pad[2],
575 fh->fh_base.fh_pad[3],
576 fh->fh_base.fh_pad[4],
577 fh->fh_base.fh_pad[5]);
578 return buf;
581 enum fsid_source fsid_source(struct svc_fh *fhp)
583 if (fhp->fh_handle.fh_version != 1)
584 return FSIDSOURCE_DEV;
585 switch(fhp->fh_handle.fh_fsid_type) {
586 case FSID_DEV:
587 case FSID_ENCODE_DEV:
588 case FSID_MAJOR_MINOR:
589 if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
590 & FS_REQUIRES_DEV)
591 return FSIDSOURCE_DEV;
592 break;
593 case FSID_NUM:
594 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
595 return FSIDSOURCE_FSID;
596 break;
597 default:
598 break;
600 /* either a UUID type filehandle, or the filehandle doesn't
601 * match the export.
603 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
604 return FSIDSOURCE_FSID;
605 if (fhp->fh_export->ex_uuid)
606 return FSIDSOURCE_UUID;
607 return FSIDSOURCE_DEV;