Linux 2.6.23.13
[linux/fpc-iii.git] / fs / nfsd / nfsfh.c
blob51221be291b4d92383f6ceb638ee1681b975f81f
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>
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;
41 int rv;
42 struct dentry *tdentry;
43 struct dentry *parent;
45 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
46 return 1;
48 tdentry = dget(dentry);
49 while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
50 /* make sure parents give x permission to user */
51 int err;
52 parent = dget_parent(tdentry);
53 err = permission(parent->d_inode, MAY_EXEC, NULL);
54 if (err < 0) {
55 dput(parent);
56 break;
58 dput(tdentry);
59 tdentry = parent;
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);
64 dput(tdentry);
65 return rv;
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
71 * the write call).
73 static inline __be32
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)
81 return nfserr_notdir;
82 else if ((mode & S_IFMT) == S_IFDIR)
83 return nfserr_isdir;
84 else
85 return nfserr_inval;
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)
91 return nfserr_isdir;
92 else
93 return nfserr_notdir;
95 return 0;
98 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
99 struct svc_export *exp)
101 /* Check if the request originated from a secure port. */
102 if (!rqstp->rq_secure && EX_SECURE(exp)) {
103 char buf[RPC_MAX_ADDRBUFLEN];
104 dprintk(KERN_WARNING
105 "nfsd: request from insecure port %s!\n",
106 svc_print_addr(rqstp, buf, sizeof(buf)));
107 return nfserr_perm;
110 /* Set user creds for this exportpoint */
111 return nfserrno(nfsd_setuser(rqstp, exp));
115 * Perform sanity checks on the dentry in a client's file handle.
117 * Note that the file handle dentry may need to be freed even after
118 * an error return.
120 * This is only called at the start of an nfsproc call, so fhp points to
121 * a svc_fh which is all 0 except for the over-the-wire file handle.
123 __be32
124 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
126 struct knfsd_fh *fh = &fhp->fh_handle;
127 struct svc_export *exp = NULL;
128 struct dentry *dentry;
129 __be32 error = 0;
131 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
133 if (!fhp->fh_dentry) {
134 __u32 *datap=NULL;
135 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
136 int fileid_type;
137 int data_left = fh->fh_size/4;
139 error = nfserr_stale;
140 if (rqstp->rq_vers > 2)
141 error = nfserr_badhandle;
142 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
143 return nfserr_nofilehandle;
145 if (fh->fh_version == 1) {
146 int len;
147 datap = fh->fh_auth;
148 if (--data_left<0) goto out;
149 switch (fh->fh_auth_type) {
150 case 0: break;
151 default: goto out;
153 len = key_len(fh->fh_fsid_type) / 4;
154 if (len == 0) goto out;
155 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
156 /* deprecated, convert to type 3 */
157 len = key_len(FSID_ENCODE_DEV)/4;
158 fh->fh_fsid_type = FSID_ENCODE_DEV;
159 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
160 fh->fh_fsid[1] = fh->fh_fsid[2];
162 if ((data_left -= len)<0) goto out;
163 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, datap);
164 datap += len;
165 } else {
166 dev_t xdev;
167 ino_t xino;
168 if (fh->fh_size != NFS_FHSIZE)
169 goto out;
170 /* assume old filehandle format */
171 xdev = old_decode_dev(fh->ofh_xdev);
172 xino = u32_to_ino_t(fh->ofh_xino);
173 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
174 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
177 error = nfserr_stale;
178 if (PTR_ERR(exp) == -ENOENT)
179 goto out;
181 if (IS_ERR(exp)) {
182 error = nfserrno(PTR_ERR(exp));
183 goto out;
186 error = nfsd_setuser_and_check_port(rqstp, exp);
187 if (error)
188 goto out;
191 * Look up the dentry using the NFS file handle.
193 error = nfserr_stale;
194 if (rqstp->rq_vers > 2)
195 error = nfserr_badhandle;
197 if (fh->fh_version != 1) {
198 tfh[0] = fh->ofh_ino;
199 tfh[1] = fh->ofh_generation;
200 tfh[2] = fh->ofh_dirino;
201 datap = tfh;
202 data_left = 3;
203 if (fh->ofh_dirino == 0)
204 fileid_type = 1;
205 else
206 fileid_type = 2;
207 } else
208 fileid_type = fh->fh_fileid_type;
210 if (fileid_type == 0)
211 dentry = dget(exp->ex_dentry);
212 else {
213 dentry = exportfs_decode_fh(exp->ex_mnt, datap,
214 data_left, fileid_type,
215 nfsd_acceptable, exp);
217 if (dentry == NULL)
218 goto out;
219 if (IS_ERR(dentry)) {
220 if (PTR_ERR(dentry) != -EINVAL)
221 error = nfserrno(PTR_ERR(dentry));
222 goto out;
225 if (S_ISDIR(dentry->d_inode->i_mode) &&
226 (dentry->d_flags & DCACHE_DISCONNECTED)) {
227 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
228 dentry->d_parent->d_name.name, dentry->d_name.name);
231 fhp->fh_dentry = dentry;
232 fhp->fh_export = exp;
233 nfsd_nr_verified++;
234 } else {
236 * just rechecking permissions
237 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
238 * does as well)
240 dprintk("nfsd: fh_verify - just checking\n");
241 dentry = fhp->fh_dentry;
242 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
248 * operation.
250 error = nfsd_setuser_and_check_port(rqstp, exp);
251 if (error)
252 goto out;
254 cache_get(&exp->h);
257 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
258 if (error)
259 goto out;
261 if (!(access & MAY_LOCK)) {
263 * pseudoflavor restrictions are not enforced on NLM,
264 * which clients virtually always use auth_sys for,
265 * even while using RPCSEC_GSS for NFS.
267 error = check_nfsd_access(exp, rqstp);
268 if (error)
269 goto out;
272 /* Finally, check access permissions. */
273 error = nfsd_permission(rqstp, exp, dentry, access);
275 if (error) {
276 dprintk("fh_verify: %s/%s permission failure, "
277 "acc=%x, error=%d\n",
278 dentry->d_parent->d_name.name,
279 dentry->d_name.name,
280 access, ntohl(error));
282 out:
283 if (exp && !IS_ERR(exp))
284 exp_put(exp);
285 if (error == nfserr_stale)
286 nfsdstats.fh_stale++;
287 return error;
292 * Compose a file handle for an NFS reply.
294 * Note that when first composed, the dentry may not yet have
295 * an inode. In this case a call to fh_update should be made
296 * before the fh goes out on the wire ...
298 static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
299 __u32 *datap, int *maxsize)
301 if (dentry == exp->ex_dentry) {
302 *maxsize = 0;
303 return 0;
306 return exportfs_encode_fh(dentry, datap, maxsize,
307 !(exp->ex_flags & NFSEXP_NOSUBTREECHECK));
311 * for composing old style file handles
313 static inline void _fh_update_old(struct dentry *dentry,
314 struct svc_export *exp,
315 struct knfsd_fh *fh)
317 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
318 fh->ofh_generation = dentry->d_inode->i_generation;
319 if (S_ISDIR(dentry->d_inode->i_mode) ||
320 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
321 fh->ofh_dirino = 0;
324 __be32
325 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
326 struct svc_fh *ref_fh)
328 /* ref_fh is a reference file handle.
329 * if it is non-null and for the same filesystem, then we should compose
330 * a filehandle which is of the same version, where possible.
331 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
332 * Then create a 32byte filehandle using nfs_fhbase_old
336 u8 version;
337 u8 fsid_type = 0;
338 struct inode * inode = dentry->d_inode;
339 struct dentry *parent = dentry->d_parent;
340 __u32 *datap;
341 dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
342 int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
344 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
345 MAJOR(ex_dev), MINOR(ex_dev),
346 (long) exp->ex_dentry->d_inode->i_ino,
347 parent->d_name.name, dentry->d_name.name,
348 (inode ? inode->i_ino : 0));
350 /* Choose filehandle version and fsid type based on
351 * the reference filehandle (if it is in the same export)
352 * or the export options.
354 retry:
355 version = 1;
356 if (ref_fh && ref_fh->fh_export == exp) {
357 version = ref_fh->fh_handle.fh_version;
358 fsid_type = ref_fh->fh_handle.fh_fsid_type;
360 if (ref_fh == fhp)
361 fh_put(ref_fh);
362 ref_fh = NULL;
364 switch (version) {
365 case 0xca:
366 fsid_type = FSID_DEV;
367 break;
368 case 1:
369 break;
370 default:
371 goto retry;
374 /* Need to check that this type works for this
375 * export point. As the fsid -> filesystem mapping
376 * was guided by user-space, there is no guarantee
377 * that the filesystem actually supports that fsid
378 * type. If it doesn't we loop around again without
379 * ref_fh set.
381 switch(fsid_type) {
382 case FSID_DEV:
383 if (!old_valid_dev(ex_dev))
384 goto retry;
385 /* FALL THROUGH */
386 case FSID_MAJOR_MINOR:
387 case FSID_ENCODE_DEV:
388 if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags
389 & FS_REQUIRES_DEV))
390 goto retry;
391 break;
392 case FSID_NUM:
393 if (! (exp->ex_flags & NFSEXP_FSID))
394 goto retry;
395 break;
396 case FSID_UUID8:
397 case FSID_UUID16:
398 if (!root_export)
399 goto retry;
400 /* fall through */
401 case FSID_UUID4_INUM:
402 case FSID_UUID16_INUM:
403 if (exp->ex_uuid == NULL)
404 goto retry;
405 break;
407 } else if (exp->ex_uuid) {
408 if (fhp->fh_maxsize >= 64) {
409 if (root_export)
410 fsid_type = FSID_UUID16;
411 else
412 fsid_type = FSID_UUID16_INUM;
413 } else {
414 if (root_export)
415 fsid_type = FSID_UUID8;
416 else
417 fsid_type = FSID_UUID4_INUM;
419 } else if (exp->ex_flags & NFSEXP_FSID)
420 fsid_type = FSID_NUM;
421 else if (!old_valid_dev(ex_dev))
422 /* for newer device numbers, we must use a newer fsid format */
423 fsid_type = FSID_ENCODE_DEV;
424 else
425 fsid_type = FSID_DEV;
427 if (ref_fh == fhp)
428 fh_put(ref_fh);
430 if (fhp->fh_locked || fhp->fh_dentry) {
431 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
432 parent->d_name.name, dentry->d_name.name);
434 if (fhp->fh_maxsize < NFS_FHSIZE)
435 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
436 fhp->fh_maxsize,
437 parent->d_name.name, dentry->d_name.name);
439 fhp->fh_dentry = dget(dentry); /* our internal copy */
440 fhp->fh_export = exp;
441 cache_get(&exp->h);
443 if (version == 0xca) {
444 /* old style filehandle please */
445 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
446 fhp->fh_handle.fh_size = NFS_FHSIZE;
447 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
448 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
449 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
450 fhp->fh_handle.ofh_xino =
451 ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
452 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
453 if (inode)
454 _fh_update_old(dentry, exp, &fhp->fh_handle);
455 } else {
456 int len;
457 fhp->fh_handle.fh_version = 1;
458 fhp->fh_handle.fh_auth_type = 0;
459 datap = fhp->fh_handle.fh_auth+0;
460 fhp->fh_handle.fh_fsid_type = fsid_type;
461 mk_fsid(fsid_type, datap, ex_dev,
462 exp->ex_dentry->d_inode->i_ino,
463 exp->ex_fsid, exp->ex_uuid);
465 len = key_len(fsid_type);
466 datap += len/4;
467 fhp->fh_handle.fh_size = 4 + len;
469 if (inode) {
470 int size = (fhp->fh_maxsize-len-4)/4;
471 fhp->fh_handle.fh_fileid_type =
472 _fh_update(dentry, exp, datap, &size);
473 fhp->fh_handle.fh_size += size*4;
475 if (fhp->fh_handle.fh_fileid_type == 255)
476 return nfserr_opnotsupp;
479 nfsd_nr_verified++;
480 return 0;
484 * Update file handle information after changing a dentry.
485 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
487 __be32
488 fh_update(struct svc_fh *fhp)
490 struct dentry *dentry;
491 __u32 *datap;
493 if (!fhp->fh_dentry)
494 goto out_bad;
496 dentry = fhp->fh_dentry;
497 if (!dentry->d_inode)
498 goto out_negative;
499 if (fhp->fh_handle.fh_version != 1) {
500 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
501 } else {
502 int size;
503 if (fhp->fh_handle.fh_fileid_type != 0)
504 goto out;
505 datap = fhp->fh_handle.fh_auth+
506 fhp->fh_handle.fh_size/4 -1;
507 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
508 fhp->fh_handle.fh_fileid_type =
509 _fh_update(dentry, fhp->fh_export, datap, &size);
510 fhp->fh_handle.fh_size += size*4;
511 if (fhp->fh_handle.fh_fileid_type == 255)
512 return nfserr_opnotsupp;
514 out:
515 return 0;
517 out_bad:
518 printk(KERN_ERR "fh_update: fh not verified!\n");
519 goto out;
520 out_negative:
521 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
522 dentry->d_parent->d_name.name, dentry->d_name.name);
523 goto out;
527 * Release a file handle.
529 void
530 fh_put(struct svc_fh *fhp)
532 struct dentry * dentry = fhp->fh_dentry;
533 struct svc_export * exp = fhp->fh_export;
534 if (dentry) {
535 fh_unlock(fhp);
536 fhp->fh_dentry = NULL;
537 dput(dentry);
538 #ifdef CONFIG_NFSD_V3
539 fhp->fh_pre_saved = 0;
540 fhp->fh_post_saved = 0;
541 #endif
542 nfsd_nr_put++;
544 if (exp) {
545 cache_put(&exp->h, &svc_export_cache);
546 fhp->fh_export = NULL;
548 return;
552 * Shorthand for dprintk()'s
554 char * SVCFH_fmt(struct svc_fh *fhp)
556 struct knfsd_fh *fh = &fhp->fh_handle;
558 static char buf[80];
559 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
560 fh->fh_size,
561 fh->fh_base.fh_pad[0],
562 fh->fh_base.fh_pad[1],
563 fh->fh_base.fh_pad[2],
564 fh->fh_base.fh_pad[3],
565 fh->fh_base.fh_pad[4],
566 fh->fh_base.fh_pad[5]);
567 return buf;
570 enum fsid_source fsid_source(struct svc_fh *fhp)
572 if (fhp->fh_handle.fh_version != 1)
573 return FSIDSOURCE_DEV;
574 switch(fhp->fh_handle.fh_fsid_type) {
575 case FSID_DEV:
576 case FSID_ENCODE_DEV:
577 case FSID_MAJOR_MINOR:
578 if (fhp->fh_export->ex_dentry->d_inode->i_sb->s_type->fs_flags
579 & FS_REQUIRES_DEV)
580 return FSIDSOURCE_DEV;
581 break;
582 case FSID_NUM:
583 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
584 return FSIDSOURCE_FSID;
585 break;
586 default:
587 break;
589 /* either a UUID type filehandle, or the filehandle doesn't
590 * match the export.
592 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
593 return FSIDSOURCE_FSID;
594 if (fhp->fh_export->ex_uuid)
595 return FSIDSOURCE_UUID;
596 return FSIDSOURCE_DEV;