[NETFILTER]: nf_conntrack_sip: create signalling expectations
[linux-2.6/mini2440.git] / fs / nfsd / nfsfh.c
blob3e6b3f41ee1f342918e6e3f57493f3c0ab21b033
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 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
105 dprintk(KERN_WARNING
106 "nfsd: request from insecure port %s!\n",
107 svc_print_addr(rqstp, buf, sizeof(buf)));
108 return nfserr_perm;
111 /* Set user creds for this exportpoint */
112 return nfserrno(nfsd_setuser(rqstp, exp));
116 * Perform sanity checks on the dentry in a client's file handle.
118 * Note that the file handle dentry may need to be freed even after
119 * an error return.
121 * This is only called at the start of an nfsproc call, so fhp points to
122 * a svc_fh which is all 0 except for the over-the-wire file handle.
124 __be32
125 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
127 struct knfsd_fh *fh = &fhp->fh_handle;
128 struct svc_export *exp = NULL;
129 struct dentry *dentry;
130 __be32 error = 0;
132 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
134 if (!fhp->fh_dentry) {
135 struct fid *fid = NULL, sfid;
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 if (--data_left<0) goto out;
148 switch (fh->fh_auth_type) {
149 case 0: break;
150 default: goto out;
152 len = key_len(fh->fh_fsid_type) / 4;
153 if (len == 0) goto out;
154 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
155 /* deprecated, convert to type 3 */
156 len = key_len(FSID_ENCODE_DEV)/4;
157 fh->fh_fsid_type = FSID_ENCODE_DEV;
158 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
159 fh->fh_fsid[1] = fh->fh_fsid[2];
161 if ((data_left -= len)<0) goto out;
162 exp = rqst_exp_find(rqstp, fh->fh_fsid_type,
163 fh->fh_auth);
164 fid = (struct fid *)(fh->fh_auth + len);
165 } else {
166 __u32 tfh[2];
167 dev_t xdev;
168 ino_t xino;
169 if (fh->fh_size != NFS_FHSIZE)
170 goto out;
171 /* assume old filehandle format */
172 xdev = old_decode_dev(fh->ofh_xdev);
173 xino = u32_to_ino_t(fh->ofh_xino);
174 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
175 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
178 error = nfserr_stale;
179 if (PTR_ERR(exp) == -ENOENT)
180 goto out;
182 if (IS_ERR(exp)) {
183 error = nfserrno(PTR_ERR(exp));
184 goto out;
187 error = nfsd_setuser_and_check_port(rqstp, exp);
188 if (error)
189 goto out;
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 sfid.i32.ino = fh->ofh_ino;
200 sfid.i32.gen = fh->ofh_generation;
201 sfid.i32.parent_ino = fh->ofh_dirino;
202 fid = &sfid;
203 data_left = 3;
204 if (fh->ofh_dirino == 0)
205 fileid_type = FILEID_INO32_GEN;
206 else
207 fileid_type = FILEID_INO32_GEN_PARENT;
208 } else
209 fileid_type = fh->fh_fileid_type;
211 if (fileid_type == FILEID_ROOT)
212 dentry = dget(exp->ex_path.dentry);
213 else {
214 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
215 data_left, fileid_type,
216 nfsd_acceptable, exp);
218 if (dentry == NULL)
219 goto out;
220 if (IS_ERR(dentry)) {
221 if (PTR_ERR(dentry) != -EINVAL)
222 error = nfserrno(PTR_ERR(dentry));
223 goto out;
226 if (S_ISDIR(dentry->d_inode->i_mode) &&
227 (dentry->d_flags & DCACHE_DISCONNECTED)) {
228 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
229 dentry->d_parent->d_name.name, dentry->d_name.name);
232 fhp->fh_dentry = dentry;
233 fhp->fh_export = exp;
234 nfsd_nr_verified++;
235 cache_get(&exp->h);
236 } else {
238 * just rechecking permissions
239 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
240 * does as well)
242 dprintk("nfsd: fh_verify - just checking\n");
243 dentry = fhp->fh_dentry;
244 exp = fhp->fh_export;
245 cache_get(&exp->h);
247 * Set user creds for this exportpoint; necessary even
248 * in the "just checking" case because this may be a
249 * filehandle that was created by fh_compose, and that
250 * is about to be used in another nfsv4 compound
251 * operation.
253 error = nfsd_setuser_and_check_port(rqstp, exp);
254 if (error)
255 goto out;
258 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
259 if (error)
260 goto out;
262 if (!(access & MAY_LOCK)) {
264 * pseudoflavor restrictions are not enforced on NLM,
265 * which clients virtually always use auth_sys for,
266 * even while using RPCSEC_GSS for NFS.
268 error = check_nfsd_access(exp, rqstp);
269 if (error)
270 goto out;
273 /* Finally, check access permissions. */
274 error = nfsd_permission(rqstp, exp, dentry, access);
276 if (error) {
277 dprintk("fh_verify: %s/%s permission failure, "
278 "acc=%x, error=%d\n",
279 dentry->d_parent->d_name.name,
280 dentry->d_name.name,
281 access, ntohl(error));
283 out:
284 if (exp && !IS_ERR(exp))
285 exp_put(exp);
286 if (error == nfserr_stale)
287 nfsdstats.fh_stale++;
288 return error;
293 * Compose a file handle for an NFS reply.
295 * Note that when first composed, the dentry may not yet have
296 * an inode. In this case a call to fh_update should be made
297 * before the fh goes out on the wire ...
299 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
300 struct dentry *dentry)
302 if (dentry != exp->ex_path.dentry) {
303 struct fid *fid = (struct fid *)
304 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
305 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
306 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
308 fhp->fh_handle.fh_fileid_type =
309 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
310 fhp->fh_handle.fh_size += maxsize * 4;
311 } else {
312 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
317 * for composing old style file handles
319 static inline void _fh_update_old(struct dentry *dentry,
320 struct svc_export *exp,
321 struct knfsd_fh *fh)
323 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
324 fh->ofh_generation = dentry->d_inode->i_generation;
325 if (S_ISDIR(dentry->d_inode->i_mode) ||
326 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
327 fh->ofh_dirino = 0;
330 __be32
331 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
332 struct svc_fh *ref_fh)
334 /* ref_fh is a reference file handle.
335 * if it is non-null and for the same filesystem, then we should compose
336 * a filehandle which is of the same version, where possible.
337 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
338 * Then create a 32byte filehandle using nfs_fhbase_old
342 u8 version;
343 u8 fsid_type = 0;
344 struct inode * inode = dentry->d_inode;
345 struct dentry *parent = dentry->d_parent;
346 __u32 *datap;
347 dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev;
348 int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root);
350 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
351 MAJOR(ex_dev), MINOR(ex_dev),
352 (long) exp->ex_path.dentry->d_inode->i_ino,
353 parent->d_name.name, dentry->d_name.name,
354 (inode ? inode->i_ino : 0));
356 /* Choose filehandle version and fsid type based on
357 * the reference filehandle (if it is in the same export)
358 * or the export options.
360 retry:
361 version = 1;
362 if (ref_fh && ref_fh->fh_export == exp) {
363 version = ref_fh->fh_handle.fh_version;
364 fsid_type = ref_fh->fh_handle.fh_fsid_type;
366 if (ref_fh == fhp)
367 fh_put(ref_fh);
368 ref_fh = NULL;
370 switch (version) {
371 case 0xca:
372 fsid_type = FSID_DEV;
373 break;
374 case 1:
375 break;
376 default:
377 goto retry;
380 /* Need to check that this type works for this
381 * export point. As the fsid -> filesystem mapping
382 * was guided by user-space, there is no guarantee
383 * that the filesystem actually supports that fsid
384 * type. If it doesn't we loop around again without
385 * ref_fh set.
387 switch(fsid_type) {
388 case FSID_DEV:
389 if (!old_valid_dev(ex_dev))
390 goto retry;
391 /* FALL THROUGH */
392 case FSID_MAJOR_MINOR:
393 case FSID_ENCODE_DEV:
394 if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
395 & FS_REQUIRES_DEV))
396 goto retry;
397 break;
398 case FSID_NUM:
399 if (! (exp->ex_flags & NFSEXP_FSID))
400 goto retry;
401 break;
402 case FSID_UUID8:
403 case FSID_UUID16:
404 if (!root_export)
405 goto retry;
406 /* fall through */
407 case FSID_UUID4_INUM:
408 case FSID_UUID16_INUM:
409 if (exp->ex_uuid == NULL)
410 goto retry;
411 break;
413 } else if (exp->ex_uuid) {
414 if (fhp->fh_maxsize >= 64) {
415 if (root_export)
416 fsid_type = FSID_UUID16;
417 else
418 fsid_type = FSID_UUID16_INUM;
419 } else {
420 if (root_export)
421 fsid_type = FSID_UUID8;
422 else
423 fsid_type = FSID_UUID4_INUM;
425 } else if (exp->ex_flags & NFSEXP_FSID)
426 fsid_type = FSID_NUM;
427 else if (!old_valid_dev(ex_dev))
428 /* for newer device numbers, we must use a newer fsid format */
429 fsid_type = FSID_ENCODE_DEV;
430 else
431 fsid_type = FSID_DEV;
433 if (ref_fh == fhp)
434 fh_put(ref_fh);
436 if (fhp->fh_locked || fhp->fh_dentry) {
437 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
438 parent->d_name.name, dentry->d_name.name);
440 if (fhp->fh_maxsize < NFS_FHSIZE)
441 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
442 fhp->fh_maxsize,
443 parent->d_name.name, dentry->d_name.name);
445 fhp->fh_dentry = dget(dentry); /* our internal copy */
446 fhp->fh_export = exp;
447 cache_get(&exp->h);
449 if (version == 0xca) {
450 /* old style filehandle please */
451 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
452 fhp->fh_handle.fh_size = NFS_FHSIZE;
453 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
454 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
455 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
456 fhp->fh_handle.ofh_xino =
457 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
458 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
459 if (inode)
460 _fh_update_old(dentry, exp, &fhp->fh_handle);
461 } else {
462 int len;
463 fhp->fh_handle.fh_version = 1;
464 fhp->fh_handle.fh_auth_type = 0;
465 datap = fhp->fh_handle.fh_auth+0;
466 fhp->fh_handle.fh_fsid_type = fsid_type;
467 mk_fsid(fsid_type, datap, ex_dev,
468 exp->ex_path.dentry->d_inode->i_ino,
469 exp->ex_fsid, exp->ex_uuid);
471 len = key_len(fsid_type);
472 datap += len/4;
473 fhp->fh_handle.fh_size = 4 + len;
475 if (inode)
476 _fh_update(fhp, exp, dentry);
477 if (fhp->fh_handle.fh_fileid_type == 255)
478 return nfserr_opnotsupp;
481 nfsd_nr_verified++;
482 return 0;
486 * Update file handle information after changing a dentry.
487 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
489 __be32
490 fh_update(struct svc_fh *fhp)
492 struct dentry *dentry;
494 if (!fhp->fh_dentry)
495 goto out_bad;
497 dentry = fhp->fh_dentry;
498 if (!dentry->d_inode)
499 goto out_negative;
500 if (fhp->fh_handle.fh_version != 1) {
501 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
502 } else {
503 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
504 goto out;
506 _fh_update(fhp, fhp->fh_export, dentry);
507 if (fhp->fh_handle.fh_fileid_type == 255)
508 return nfserr_opnotsupp;
510 out:
511 return 0;
513 out_bad:
514 printk(KERN_ERR "fh_update: fh not verified!\n");
515 goto out;
516 out_negative:
517 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
518 dentry->d_parent->d_name.name, dentry->d_name.name);
519 goto out;
523 * Release a file handle.
525 void
526 fh_put(struct svc_fh *fhp)
528 struct dentry * dentry = fhp->fh_dentry;
529 struct svc_export * exp = fhp->fh_export;
530 if (dentry) {
531 fh_unlock(fhp);
532 fhp->fh_dentry = NULL;
533 dput(dentry);
534 #ifdef CONFIG_NFSD_V3
535 fhp->fh_pre_saved = 0;
536 fhp->fh_post_saved = 0;
537 #endif
538 nfsd_nr_put++;
540 if (exp) {
541 cache_put(&exp->h, &svc_export_cache);
542 fhp->fh_export = NULL;
544 return;
548 * Shorthand for dprintk()'s
550 char * SVCFH_fmt(struct svc_fh *fhp)
552 struct knfsd_fh *fh = &fhp->fh_handle;
554 static char buf[80];
555 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
556 fh->fh_size,
557 fh->fh_base.fh_pad[0],
558 fh->fh_base.fh_pad[1],
559 fh->fh_base.fh_pad[2],
560 fh->fh_base.fh_pad[3],
561 fh->fh_base.fh_pad[4],
562 fh->fh_base.fh_pad[5]);
563 return buf;
566 enum fsid_source fsid_source(struct svc_fh *fhp)
568 if (fhp->fh_handle.fh_version != 1)
569 return FSIDSOURCE_DEV;
570 switch(fhp->fh_handle.fh_fsid_type) {
571 case FSID_DEV:
572 case FSID_ENCODE_DEV:
573 case FSID_MAJOR_MINOR:
574 if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
575 & FS_REQUIRES_DEV)
576 return FSIDSOURCE_DEV;
577 break;
578 case FSID_NUM:
579 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
580 return FSIDSOURCE_FSID;
581 break;
582 default:
583 break;
585 /* either a UUID type filehandle, or the filehandle doesn't
586 * match the export.
588 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
589 return FSIDSOURCE_FSID;
590 if (fhp->fh_export->ex_uuid)
591 return FSIDSOURCE_UUID;
592 return FSIDSOURCE_DEV;