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_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_dentry
)
63 dprintk("nfsd_acceptable failed at %p %s\n", tdentry
, tdentry
->d_name
.name
);
64 rv
= (tdentry
== exp
->ex_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 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 * 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
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.
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
;
132 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp
));
134 if (!fhp
->fh_dentry
) {
135 struct fid
*fid
= NULL
, sfid
;
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) {
147 if (--data_left
<0) goto out
;
148 switch (fh
->fh_auth_type
) {
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
,
164 fid
= (struct fid
*)(fh
->fh_auth
+ len
);
169 if (fh
->fh_size
!= NFS_FHSIZE
)
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
)
183 error
= nfserrno(PTR_ERR(exp
));
187 error
= nfsd_setuser_and_check_port(rqstp
, exp
);
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
;
204 if (fh
->ofh_dirino
== 0)
205 fileid_type
= FILEID_INO32_GEN
;
207 fileid_type
= FILEID_INO32_GEN_PARENT
;
209 fileid_type
= fh
->fh_fileid_type
;
211 if (fileid_type
== FILEID_ROOT
)
212 dentry
= dget(exp
->ex_dentry
);
214 dentry
= exportfs_decode_fh(exp
->ex_mnt
, fid
,
215 data_left
, fileid_type
,
216 nfsd_acceptable
, exp
);
220 if (IS_ERR(dentry
)) {
221 if (PTR_ERR(dentry
) != -EINVAL
)
222 error
= nfserrno(PTR_ERR(dentry
));
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
;
237 * just rechecking permissions
238 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
241 dprintk("nfsd: fh_verify - just checking\n");
242 dentry
= fhp
->fh_dentry
;
243 exp
= fhp
->fh_export
;
245 * Set user creds for this exportpoint; necessary even
246 * in the "just checking" case because this may be a
247 * filehandle that was created by fh_compose, and that
248 * is about to be used in another nfsv4 compound
251 error
= nfsd_setuser_and_check_port(rqstp
, exp
);
258 error
= nfsd_mode_check(rqstp
, dentry
->d_inode
->i_mode
, type
);
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
);
273 /* Finally, check access permissions. */
274 error
= nfsd_permission(rqstp
, exp
, dentry
, access
);
277 dprintk("fh_verify: %s/%s permission failure, "
278 "acc=%x, error=%d\n",
279 dentry
->d_parent
->d_name
.name
,
281 access
, ntohl(error
));
284 if (exp
&& !IS_ERR(exp
))
286 if (error
== nfserr_stale
)
287 nfsdstats
.fh_stale
++;
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_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;
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
,
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
))
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
344 struct inode
* inode
= dentry
->d_inode
;
345 struct dentry
*parent
= dentry
->d_parent
;
347 dev_t ex_dev
= exp
->ex_dentry
->d_inode
->i_sb
->s_dev
;
348 int root_export
= (exp
->ex_dentry
== exp
->ex_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_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.
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
;
372 fsid_type
= FSID_DEV
;
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
389 if (!old_valid_dev(ex_dev
))
392 case FSID_MAJOR_MINOR
:
393 case FSID_ENCODE_DEV
:
394 if (!(exp
->ex_dentry
->d_inode
->i_sb
->s_type
->fs_flags
399 if (! (exp
->ex_flags
& NFSEXP_FSID
))
407 case FSID_UUID4_INUM
:
408 case FSID_UUID16_INUM
:
409 if (exp
->ex_uuid
== NULL
)
413 } else if (exp
->ex_uuid
) {
414 if (fhp
->fh_maxsize
>= 64) {
416 fsid_type
= FSID_UUID16
;
418 fsid_type
= FSID_UUID16_INUM
;
421 fsid_type
= FSID_UUID8
;
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
;
431 fsid_type
= FSID_DEV
;
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",
443 parent
->d_name
.name
, dentry
->d_name
.name
);
445 fhp
->fh_dentry
= dget(dentry
); /* our internal copy */
446 fhp
->fh_export
= exp
;
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_dentry
->d_inode
->i_ino
);
458 fhp
->fh_handle
.ofh_dirino
= ino_t_to_u32(parent_ino(dentry
));
460 _fh_update_old(dentry
, exp
, &fhp
->fh_handle
);
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_dentry
->d_inode
->i_ino
,
469 exp
->ex_fsid
, exp
->ex_uuid
);
471 len
= key_len(fsid_type
);
473 fhp
->fh_handle
.fh_size
= 4 + len
;
476 _fh_update(fhp
, exp
, dentry
);
477 if (fhp
->fh_handle
.fh_fileid_type
== 255)
478 return nfserr_opnotsupp
;
486 * Update file handle information after changing a dentry.
487 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
490 fh_update(struct svc_fh
*fhp
)
492 struct dentry
*dentry
;
497 dentry
= fhp
->fh_dentry
;
498 if (!dentry
->d_inode
)
500 if (fhp
->fh_handle
.fh_version
!= 1) {
501 _fh_update_old(dentry
, fhp
->fh_export
, &fhp
->fh_handle
);
503 if (fhp
->fh_handle
.fh_fileid_type
!= FILEID_ROOT
)
506 _fh_update(fhp
, fhp
->fh_export
, dentry
);
507 if (fhp
->fh_handle
.fh_fileid_type
== 255)
508 return nfserr_opnotsupp
;
514 printk(KERN_ERR
"fh_update: fh not verified!\n");
517 printk(KERN_ERR
"fh_update: %s/%s still negative!\n",
518 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
523 * Release a file handle.
526 fh_put(struct svc_fh
*fhp
)
528 struct dentry
* dentry
= fhp
->fh_dentry
;
529 struct svc_export
* exp
= fhp
->fh_export
;
532 fhp
->fh_dentry
= NULL
;
534 #ifdef CONFIG_NFSD_V3
535 fhp
->fh_pre_saved
= 0;
536 fhp
->fh_post_saved
= 0;
541 cache_put(&exp
->h
, &svc_export_cache
);
542 fhp
->fh_export
= NULL
;
548 * Shorthand for dprintk()'s
550 char * SVCFH_fmt(struct svc_fh
*fhp
)
552 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
555 sprintf(buf
, "%d: %08x %08x %08x %08x %08x %08x",
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]);
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
) {
572 case FSID_ENCODE_DEV
:
573 case FSID_MAJOR_MINOR
:
574 if (fhp
->fh_export
->ex_dentry
->d_inode
->i_sb
->s_type
->fs_flags
576 return FSIDSOURCE_DEV
;
579 if (fhp
->fh_export
->ex_flags
& NFSEXP_FSID
)
580 return FSIDSOURCE_FSID
;
585 /* either a UUID type filehandle, or the filehandle doesn't
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
;