100ae5641162b97c7fb0e57032a9b30ecc5b899e
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/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>
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
;
43 struct dentry
*tdentry
;
44 struct dentry
*parent
;
46 if (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
)
49 tdentry
= dget(dentry
);
50 while (tdentry
!= exp
->ex_path
.dentry
&& !IS_ROOT(tdentry
)) {
51 /* make sure parents give x permission to user */
53 parent
= dget_parent(tdentry
);
54 err
= permission(parent
->d_inode
, MAY_EXEC
, NULL
);
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
);
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
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
)
83 else if ((mode
& S_IFMT
) == S_IFDIR
)
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
)
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
]);
106 "nfsd: request from insecure port %s!\n",
107 svc_print_addr(rqstp
, buf
, sizeof(buf
)));
111 /* Set user creds for this exportpoint */
112 return nfserrno(nfsd_setuser(rqstp
, exp
));
116 * Use the given filehandle to look up the corresponding export and
117 * dentry. On success, the results are used to set fh_export and
120 static __be32
nfsd_set_fh_dentry(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
)
122 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
123 struct fid
*fid
= NULL
, sfid
;
124 struct svc_export
*exp
;
125 struct dentry
*dentry
;
127 int data_left
= fh
->fh_size
/4;
130 error
= nfserr_stale
;
131 if (rqstp
->rq_vers
> 2)
132 error
= nfserr_badhandle
;
133 if (rqstp
->rq_vers
== 4 && fh
->fh_size
== 0)
134 return nfserr_nofilehandle
;
136 if (fh
->fh_version
== 1) {
141 if (fh
->fh_auth_type
!= 0)
143 len
= key_len(fh
->fh_fsid_type
) / 4;
146 if (fh
->fh_fsid_type
== FSID_MAJOR_MINOR
) {
147 /* deprecated, convert to type 3 */
148 len
= key_len(FSID_ENCODE_DEV
)/4;
149 fh
->fh_fsid_type
= FSID_ENCODE_DEV
;
150 fh
->fh_fsid
[0] = new_encode_dev(MKDEV(ntohl(fh
->fh_fsid
[0]), ntohl(fh
->fh_fsid
[1])));
151 fh
->fh_fsid
[1] = fh
->fh_fsid
[2];
156 exp
= rqst_exp_find(rqstp
, fh
->fh_fsid_type
, fh
->fh_auth
);
157 fid
= (struct fid
*)(fh
->fh_auth
+ len
);
163 if (fh
->fh_size
!= NFS_FHSIZE
)
165 /* assume old filehandle format */
166 xdev
= old_decode_dev(fh
->ofh_xdev
);
167 xino
= u32_to_ino_t(fh
->ofh_xino
);
168 mk_fsid(FSID_DEV
, tfh
, xdev
, xino
, 0, NULL
);
169 exp
= rqst_exp_find(rqstp
, FSID_DEV
, tfh
);
172 error
= nfserr_stale
;
173 if (PTR_ERR(exp
) == -ENOENT
)
177 return nfserrno(PTR_ERR(exp
));
179 error
= nfsd_setuser_and_check_port(rqstp
, exp
);
184 * Look up the dentry using the NFS file handle.
186 error
= nfserr_stale
;
187 if (rqstp
->rq_vers
> 2)
188 error
= nfserr_badhandle
;
190 if (fh
->fh_version
!= 1) {
191 sfid
.i32
.ino
= fh
->ofh_ino
;
192 sfid
.i32
.gen
= fh
->ofh_generation
;
193 sfid
.i32
.parent_ino
= fh
->ofh_dirino
;
196 if (fh
->ofh_dirino
== 0)
197 fileid_type
= FILEID_INO32_GEN
;
199 fileid_type
= FILEID_INO32_GEN_PARENT
;
201 fileid_type
= fh
->fh_fileid_type
;
203 if (fileid_type
== FILEID_ROOT
)
204 dentry
= dget(exp
->ex_path
.dentry
);
206 dentry
= exportfs_decode_fh(exp
->ex_path
.mnt
, fid
,
207 data_left
, fileid_type
,
208 nfsd_acceptable
, exp
);
212 if (IS_ERR(dentry
)) {
213 if (PTR_ERR(dentry
) != -EINVAL
)
214 error
= nfserrno(PTR_ERR(dentry
));
218 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
219 (dentry
->d_flags
& DCACHE_DISCONNECTED
)) {
220 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
221 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
224 fhp
->fh_dentry
= dentry
;
225 fhp
->fh_export
= exp
;
234 * Perform sanity checks on the dentry in a client's file handle.
236 * Note that the file handle dentry may need to be freed even after
239 * This is only called at the start of an nfsproc call, so fhp points to
240 * a svc_fh which is all 0 except for the over-the-wire file handle.
243 fh_verify(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
, int access
)
245 struct svc_export
*exp
;
246 struct dentry
*dentry
;
249 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp
));
251 if (!fhp
->fh_dentry
) {
252 error
= nfsd_set_fh_dentry(rqstp
, fhp
);
255 dentry
= fhp
->fh_dentry
;
256 exp
= fhp
->fh_export
;
259 * just rechecking permissions
260 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
263 dprintk("nfsd: fh_verify - just checking\n");
264 dentry
= fhp
->fh_dentry
;
265 exp
= fhp
->fh_export
;
267 * Set user creds for this exportpoint; necessary even
268 * in the "just checking" case because this may be a
269 * filehandle that was created by fh_compose, and that
270 * is about to be used in another nfsv4 compound
273 error
= nfsd_setuser_and_check_port(rqstp
, exp
);
278 error
= nfsd_mode_check(rqstp
, dentry
->d_inode
->i_mode
, type
);
282 if (!(access
& MAY_LOCK
)) {
284 * pseudoflavor restrictions are not enforced on NLM,
285 * which clients virtually always use auth_sys for,
286 * even while using RPCSEC_GSS for NFS.
288 error
= check_nfsd_access(exp
, rqstp
);
293 /* Finally, check access permissions. */
294 error
= nfsd_permission(rqstp
, exp
, dentry
, access
);
297 dprintk("fh_verify: %s/%s permission failure, "
298 "acc=%x, error=%d\n",
299 dentry
->d_parent
->d_name
.name
,
301 access
, ntohl(error
));
304 if (error
== nfserr_stale
)
305 nfsdstats
.fh_stale
++;
311 * Compose a file handle for an NFS reply.
313 * Note that when first composed, the dentry may not yet have
314 * an inode. In this case a call to fh_update should be made
315 * before the fh goes out on the wire ...
317 static void _fh_update(struct svc_fh
*fhp
, struct svc_export
*exp
,
318 struct dentry
*dentry
)
320 if (dentry
!= exp
->ex_path
.dentry
) {
321 struct fid
*fid
= (struct fid
*)
322 (fhp
->fh_handle
.fh_auth
+ fhp
->fh_handle
.fh_size
/4 - 1);
323 int maxsize
= (fhp
->fh_maxsize
- fhp
->fh_handle
.fh_size
)/4;
324 int subtreecheck
= !(exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
);
326 fhp
->fh_handle
.fh_fileid_type
=
327 exportfs_encode_fh(dentry
, fid
, &maxsize
, subtreecheck
);
328 fhp
->fh_handle
.fh_size
+= maxsize
* 4;
330 fhp
->fh_handle
.fh_fileid_type
= FILEID_ROOT
;
335 * for composing old style file handles
337 static inline void _fh_update_old(struct dentry
*dentry
,
338 struct svc_export
*exp
,
341 fh
->ofh_ino
= ino_t_to_u32(dentry
->d_inode
->i_ino
);
342 fh
->ofh_generation
= dentry
->d_inode
->i_generation
;
343 if (S_ISDIR(dentry
->d_inode
->i_mode
) ||
344 (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
))
349 fh_compose(struct svc_fh
*fhp
, struct svc_export
*exp
, struct dentry
*dentry
,
350 struct svc_fh
*ref_fh
)
352 /* ref_fh is a reference file handle.
353 * if it is non-null and for the same filesystem, then we should compose
354 * a filehandle which is of the same version, where possible.
355 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
356 * Then create a 32byte filehandle using nfs_fhbase_old
362 struct inode
* inode
= dentry
->d_inode
;
363 struct dentry
*parent
= dentry
->d_parent
;
365 dev_t ex_dev
= exp
->ex_path
.dentry
->d_inode
->i_sb
->s_dev
;
366 int root_export
= (exp
->ex_path
.dentry
== exp
->ex_path
.dentry
->d_sb
->s_root
);
368 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
369 MAJOR(ex_dev
), MINOR(ex_dev
),
370 (long) exp
->ex_path
.dentry
->d_inode
->i_ino
,
371 parent
->d_name
.name
, dentry
->d_name
.name
,
372 (inode
? inode
->i_ino
: 0));
374 /* Choose filehandle version and fsid type based on
375 * the reference filehandle (if it is in the same export)
376 * or the export options.
380 if (ref_fh
&& ref_fh
->fh_export
== exp
) {
381 version
= ref_fh
->fh_handle
.fh_version
;
382 fsid_type
= ref_fh
->fh_handle
.fh_fsid_type
;
390 fsid_type
= FSID_DEV
;
398 /* Need to check that this type works for this
399 * export point. As the fsid -> filesystem mapping
400 * was guided by user-space, there is no guarantee
401 * that the filesystem actually supports that fsid
402 * type. If it doesn't we loop around again without
407 if (!old_valid_dev(ex_dev
))
410 case FSID_MAJOR_MINOR
:
411 case FSID_ENCODE_DEV
:
412 if (!(exp
->ex_path
.dentry
->d_inode
->i_sb
->s_type
->fs_flags
417 if (! (exp
->ex_flags
& NFSEXP_FSID
))
425 case FSID_UUID4_INUM
:
426 case FSID_UUID16_INUM
:
427 if (exp
->ex_uuid
== NULL
)
431 } else if (exp
->ex_uuid
) {
432 if (fhp
->fh_maxsize
>= 64) {
434 fsid_type
= FSID_UUID16
;
436 fsid_type
= FSID_UUID16_INUM
;
439 fsid_type
= FSID_UUID8
;
441 fsid_type
= FSID_UUID4_INUM
;
443 } else if (exp
->ex_flags
& NFSEXP_FSID
)
444 fsid_type
= FSID_NUM
;
445 else if (!old_valid_dev(ex_dev
))
446 /* for newer device numbers, we must use a newer fsid format */
447 fsid_type
= FSID_ENCODE_DEV
;
449 fsid_type
= FSID_DEV
;
454 if (fhp
->fh_locked
|| fhp
->fh_dentry
) {
455 printk(KERN_ERR
"fh_compose: fh %s/%s not initialized!\n",
456 parent
->d_name
.name
, dentry
->d_name
.name
);
458 if (fhp
->fh_maxsize
< NFS_FHSIZE
)
459 printk(KERN_ERR
"fh_compose: called with maxsize %d! %s/%s\n",
461 parent
->d_name
.name
, dentry
->d_name
.name
);
463 fhp
->fh_dentry
= dget(dentry
); /* our internal copy */
464 fhp
->fh_export
= exp
;
467 if (version
== 0xca) {
468 /* old style filehandle please */
469 memset(&fhp
->fh_handle
.fh_base
, 0, NFS_FHSIZE
);
470 fhp
->fh_handle
.fh_size
= NFS_FHSIZE
;
471 fhp
->fh_handle
.ofh_dcookie
= 0xfeebbaca;
472 fhp
->fh_handle
.ofh_dev
= old_encode_dev(ex_dev
);
473 fhp
->fh_handle
.ofh_xdev
= fhp
->fh_handle
.ofh_dev
;
474 fhp
->fh_handle
.ofh_xino
=
475 ino_t_to_u32(exp
->ex_path
.dentry
->d_inode
->i_ino
);
476 fhp
->fh_handle
.ofh_dirino
= ino_t_to_u32(parent_ino(dentry
));
478 _fh_update_old(dentry
, exp
, &fhp
->fh_handle
);
481 fhp
->fh_handle
.fh_version
= 1;
482 fhp
->fh_handle
.fh_auth_type
= 0;
483 datap
= fhp
->fh_handle
.fh_auth
+0;
484 fhp
->fh_handle
.fh_fsid_type
= fsid_type
;
485 mk_fsid(fsid_type
, datap
, ex_dev
,
486 exp
->ex_path
.dentry
->d_inode
->i_ino
,
487 exp
->ex_fsid
, exp
->ex_uuid
);
489 len
= key_len(fsid_type
);
491 fhp
->fh_handle
.fh_size
= 4 + len
;
494 _fh_update(fhp
, exp
, dentry
);
495 if (fhp
->fh_handle
.fh_fileid_type
== 255)
496 return nfserr_opnotsupp
;
504 * Update file handle information after changing a dentry.
505 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
508 fh_update(struct svc_fh
*fhp
)
510 struct dentry
*dentry
;
515 dentry
= fhp
->fh_dentry
;
516 if (!dentry
->d_inode
)
518 if (fhp
->fh_handle
.fh_version
!= 1) {
519 _fh_update_old(dentry
, fhp
->fh_export
, &fhp
->fh_handle
);
521 if (fhp
->fh_handle
.fh_fileid_type
!= FILEID_ROOT
)
524 _fh_update(fhp
, fhp
->fh_export
, dentry
);
525 if (fhp
->fh_handle
.fh_fileid_type
== 255)
526 return nfserr_opnotsupp
;
532 printk(KERN_ERR
"fh_update: fh not verified!\n");
535 printk(KERN_ERR
"fh_update: %s/%s still negative!\n",
536 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
541 * Release a file handle.
544 fh_put(struct svc_fh
*fhp
)
546 struct dentry
* dentry
= fhp
->fh_dentry
;
547 struct svc_export
* exp
= fhp
->fh_export
;
550 fhp
->fh_dentry
= NULL
;
552 #ifdef CONFIG_NFSD_V3
553 fhp
->fh_pre_saved
= 0;
554 fhp
->fh_post_saved
= 0;
559 cache_put(&exp
->h
, &svc_export_cache
);
560 fhp
->fh_export
= NULL
;
566 * Shorthand for dprintk()'s
568 char * SVCFH_fmt(struct svc_fh
*fhp
)
570 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
573 sprintf(buf
, "%d: %08x %08x %08x %08x %08x %08x",
575 fh
->fh_base
.fh_pad
[0],
576 fh
->fh_base
.fh_pad
[1],
577 fh
->fh_base
.fh_pad
[2],
578 fh
->fh_base
.fh_pad
[3],
579 fh
->fh_base
.fh_pad
[4],
580 fh
->fh_base
.fh_pad
[5]);
584 enum fsid_source
fsid_source(struct svc_fh
*fhp
)
586 if (fhp
->fh_handle
.fh_version
!= 1)
587 return FSIDSOURCE_DEV
;
588 switch(fhp
->fh_handle
.fh_fsid_type
) {
590 case FSID_ENCODE_DEV
:
591 case FSID_MAJOR_MINOR
:
592 if (fhp
->fh_export
->ex_path
.dentry
->d_inode
->i_sb
->s_type
->fs_flags
594 return FSIDSOURCE_DEV
;
597 if (fhp
->fh_export
->ex_flags
& NFSEXP_FSID
)
598 return FSIDSOURCE_FSID
;
603 /* either a UUID type filehandle, or the filehandle doesn't
606 if (fhp
->fh_export
->ex_flags
& NFSEXP_FSID
)
607 return FSIDSOURCE_FSID
;
608 if (fhp
->fh_export
->ex_uuid
)
609 return FSIDSOURCE_UUID
;
610 return FSIDSOURCE_DEV
;