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/sched.h>
13 #include <linux/slab.h>
14 #include <linux/smp_lock.h>
16 #include <linux/unistd.h>
17 #include <linux/string.h>
18 #include <linux/stat.h>
19 #include <linux/dcache.h>
20 #include <linux/mount.h>
21 #include <asm/pgtable.h>
23 #include <linux/sunrpc/svc.h>
24 #include <linux/nfsd/nfsd.h>
26 #define NFSDDBG_FACILITY NFSDDBG_FH
27 #define NFSD_PARANOIA 1
28 /* #define NFSD_DEBUG_VERBOSE 1 */
31 static int nfsd_nr_verified
;
32 static int nfsd_nr_put
;
34 extern struct export_operations export_op_default
;
36 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
39 * our acceptability function.
40 * if NOSUBTREECHECK, accept anything
41 * if not, require that we can walk up to exp->ex_dentry
42 * doing some checks on the 'x' bits
44 static int nfsd_acceptable(void *expv
, struct dentry
*dentry
)
46 struct svc_export
*exp
= expv
;
48 struct dentry
*tdentry
;
49 struct dentry
*parent
;
51 if (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
)
54 tdentry
= dget(dentry
);
55 while (tdentry
!= exp
->ex_dentry
&& ! IS_ROOT(tdentry
)) {
56 /* make sure parents give x permission to user */
58 parent
= dget_parent(tdentry
);
59 err
= permission(parent
->d_inode
, MAY_EXEC
, NULL
);
67 if (tdentry
!= exp
->ex_dentry
)
68 dprintk("nfsd_acceptable failed at %p %s\n", tdentry
, tdentry
->d_name
.name
);
69 rv
= (tdentry
== exp
->ex_dentry
);
74 /* Type check. The correct error return for type mismatches does not seem to be
75 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
76 * comment in the NFSv3 spec says this is incorrect (implementation notes for
80 nfsd_mode_check(struct svc_rqst
*rqstp
, umode_t mode
, int type
)
82 /* Type can be negative when creating hardlinks - not to a dir */
83 if (type
> 0 && (mode
& S_IFMT
) != type
) {
84 if (rqstp
->rq_vers
== 4 && (mode
& S_IFMT
) == S_IFLNK
)
85 return nfserr_symlink
;
86 else if (type
== S_IFDIR
)
88 else if ((mode
& S_IFMT
) == S_IFDIR
)
93 if (type
< 0 && (mode
& S_IFMT
) == -type
) {
94 if (rqstp
->rq_vers
== 4 && (mode
& S_IFMT
) == S_IFLNK
)
95 return nfserr_symlink
;
96 else if (type
== -S_IFDIR
)
105 * Perform sanity checks on the dentry in a client's file handle.
107 * Note that the file handle dentry may need to be freed even after
110 * This is only called at the start of an nfsproc call, so fhp points to
111 * a svc_fh which is all 0 except for the over-the-wire file handle.
114 fh_verify(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
, int access
)
116 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
117 struct svc_export
*exp
= NULL
;
118 struct dentry
*dentry
;
121 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp
));
123 /* keep this filehandle for possible reference when encoding attributes */
124 rqstp
->rq_reffh
= fh
;
126 if (!fhp
->fh_dentry
) {
128 __u32 tfh
[3]; /* filehandle fragment for oldstyle filehandles */
130 int data_left
= fh
->fh_size
/4;
132 error
= nfserr_stale
;
133 if (rqstp
->rq_client
== NULL
)
135 if (rqstp
->rq_vers
> 2)
136 error
= nfserr_badhandle
;
137 if (rqstp
->rq_vers
== 4 && fh
->fh_size
== 0)
138 return nfserr_nofilehandle
;
140 if (fh
->fh_version
== 1) {
143 if (--data_left
<0) goto out
;
144 switch (fh
->fh_auth_type
) {
148 len
= key_len(fh
->fh_fsid_type
) / 4;
149 if (len
== 0) goto out
;
150 if (fh
->fh_fsid_type
== 2) {
151 /* deprecated, convert to type 3 */
153 fh
->fh_fsid_type
= 3;
154 fh
->fh_fsid
[0] = new_encode_dev(MKDEV(ntohl(fh
->fh_fsid
[0]), ntohl(fh
->fh_fsid
[1])));
155 fh
->fh_fsid
[1] = fh
->fh_fsid
[2];
157 if ((data_left
-= len
)<0) goto out
;
158 exp
= exp_find(rqstp
->rq_client
, fh
->fh_fsid_type
, datap
, &rqstp
->rq_chandle
);
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_v0(tfh
, xdev
, xino
);
169 exp
= exp_find(rqstp
->rq_client
, 0, tfh
, &rqstp
->rq_chandle
);
172 error
= nfserr_dropit
;
173 if (IS_ERR(exp
) && PTR_ERR(exp
) == -EAGAIN
)
176 error
= nfserr_stale
;
177 if (!exp
|| IS_ERR(exp
))
180 /* Check if the request originated from a secure port. */
182 if (!rqstp
->rq_secure
&& EX_SECURE(exp
)) {
184 "nfsd: request from insecure port (%u.%u.%u.%u:%d)!\n",
185 NIPQUAD(rqstp
->rq_addr
.sin_addr
.s_addr
),
186 ntohs(rqstp
->rq_addr
.sin_port
));
190 /* Set user creds for this exportpoint */
191 error
= nfsd_setuser(rqstp
, exp
);
193 error
= nfserrno(error
);
198 * Look up the dentry using the NFS file handle.
200 error
= nfserr_stale
;
201 if (rqstp
->rq_vers
> 2)
202 error
= nfserr_badhandle
;
204 if (fh
->fh_version
!= 1) {
205 tfh
[0] = fh
->ofh_ino
;
206 tfh
[1] = fh
->ofh_generation
;
207 tfh
[2] = fh
->ofh_dirino
;
210 if (fh
->ofh_dirino
== 0)
215 fileid_type
= fh
->fh_fileid_type
;
217 if (fileid_type
== 0)
218 dentry
= dget(exp
->ex_dentry
);
220 struct export_operations
*nop
= exp
->ex_mnt
->mnt_sb
->s_export_op
;
221 dentry
= CALL(nop
,decode_fh
)(exp
->ex_mnt
->mnt_sb
,
224 nfsd_acceptable
, exp
);
228 if (IS_ERR(dentry
)) {
229 if (PTR_ERR(dentry
) != -EINVAL
)
230 error
= nfserrno(PTR_ERR(dentry
));
234 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
235 (dentry
->d_flags
& DCACHE_DISCONNECTED
)) {
236 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
237 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
241 fhp
->fh_dentry
= dentry
;
242 fhp
->fh_export
= exp
;
245 /* just rechecking permissions
246 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
248 dprintk("nfsd: fh_verify - just checking\n");
249 dentry
= fhp
->fh_dentry
;
250 exp
= fhp
->fh_export
;
254 error
= nfsd_mode_check(rqstp
, dentry
->d_inode
->i_mode
, type
);
258 /* Finally, check access permissions. */
259 error
= nfsd_permission(exp
, dentry
, access
);
261 #ifdef NFSD_PARANOIA_EXTREME
263 printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
264 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, access
, (error
>> 24));
268 if (exp
&& !IS_ERR(exp
))
270 if (error
== nfserr_stale
)
271 nfsdstats
.fh_stale
++;
277 * Compose a file handle for an NFS reply.
279 * Note that when first composed, the dentry may not yet have
280 * an inode. In this case a call to fh_update should be made
281 * before the fh goes out on the wire ...
283 static inline int _fh_update(struct dentry
*dentry
, struct svc_export
*exp
,
284 __u32
*datap
, int *maxsize
)
286 struct export_operations
*nop
= exp
->ex_mnt
->mnt_sb
->s_export_op
;
288 if (dentry
== exp
->ex_dentry
) {
293 return CALL(nop
,encode_fh
)(dentry
, datap
, maxsize
,
294 !(exp
->ex_flags
&NFSEXP_NOSUBTREECHECK
));
298 * for composing old style file handles
300 static inline void _fh_update_old(struct dentry
*dentry
,
301 struct svc_export
*exp
,
304 fh
->ofh_ino
= ino_t_to_u32(dentry
->d_inode
->i_ino
);
305 fh
->ofh_generation
= dentry
->d_inode
->i_generation
;
306 if (S_ISDIR(dentry
->d_inode
->i_mode
) ||
307 (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
))
312 fh_compose(struct svc_fh
*fhp
, struct svc_export
*exp
, struct dentry
*dentry
, struct svc_fh
*ref_fh
)
314 /* ref_fh is a reference file handle.
315 * if it is non-null, then we should compose a filehandle which is
316 * of the same version, where possible.
317 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
318 * Then create a 32byte filehandle using nfs_fhbase_old
322 u8 ref_fh_version
= 0;
323 u8 ref_fh_fsid_type
= 0;
324 struct inode
* inode
= dentry
->d_inode
;
325 struct dentry
*parent
= dentry
->d_parent
;
327 dev_t ex_dev
= exp
->ex_dentry
->d_inode
->i_sb
->s_dev
;
329 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
330 MAJOR(ex_dev
), MINOR(ex_dev
),
331 (long) exp
->ex_dentry
->d_inode
->i_ino
,
332 parent
->d_name
.name
, dentry
->d_name
.name
,
333 (inode
? inode
->i_ino
: 0));
336 ref_fh_version
= ref_fh
->fh_handle
.fh_version
;
337 if (ref_fh_version
== 0xca)
338 ref_fh_fsid_type
= 0;
340 ref_fh_fsid_type
= ref_fh
->fh_handle
.fh_fsid_type
;
341 if (ref_fh_fsid_type
> 3)
342 ref_fh_fsid_type
= 0;
344 /* make sure ref_fh type works for given export */
345 if (ref_fh_fsid_type
== 1 &&
346 !(exp
->ex_flags
& NFSEXP_FSID
)) {
347 /* if we don't have an fsid, we cannot provide one... */
348 ref_fh_fsid_type
= 0;
350 } else if (exp
->ex_flags
& NFSEXP_FSID
)
351 ref_fh_fsid_type
= 1;
353 if (!old_valid_dev(ex_dev
) && ref_fh_fsid_type
== 0) {
354 /* for newer device numbers, we must use a newer fsid format */
356 ref_fh_fsid_type
= 3;
358 if (old_valid_dev(ex_dev
) &&
359 (ref_fh_fsid_type
== 2 || ref_fh_fsid_type
== 3))
360 /* must use type1 for smaller device numbers */
361 ref_fh_fsid_type
= 0;
366 if (fhp
->fh_locked
|| fhp
->fh_dentry
) {
367 printk(KERN_ERR
"fh_compose: fh %s/%s not initialized!\n",
368 parent
->d_name
.name
, dentry
->d_name
.name
);
370 if (fhp
->fh_maxsize
< NFS_FHSIZE
)
371 printk(KERN_ERR
"fh_compose: called with maxsize %d! %s/%s\n",
372 fhp
->fh_maxsize
, parent
->d_name
.name
, dentry
->d_name
.name
);
374 fhp
->fh_dentry
= dget(dentry
); /* our internal copy */
375 fhp
->fh_export
= exp
;
378 if (ref_fh_version
== 0xca) {
379 /* old style filehandle please */
380 memset(&fhp
->fh_handle
.fh_base
, 0, NFS_FHSIZE
);
381 fhp
->fh_handle
.fh_size
= NFS_FHSIZE
;
382 fhp
->fh_handle
.ofh_dcookie
= 0xfeebbaca;
383 fhp
->fh_handle
.ofh_dev
= old_encode_dev(ex_dev
);
384 fhp
->fh_handle
.ofh_xdev
= fhp
->fh_handle
.ofh_dev
;
385 fhp
->fh_handle
.ofh_xino
= ino_t_to_u32(exp
->ex_dentry
->d_inode
->i_ino
);
386 fhp
->fh_handle
.ofh_dirino
= ino_t_to_u32(parent_ino(dentry
));
388 _fh_update_old(dentry
, exp
, &fhp
->fh_handle
);
391 fhp
->fh_handle
.fh_version
= 1;
392 fhp
->fh_handle
.fh_auth_type
= 0;
393 datap
= fhp
->fh_handle
.fh_auth
+0;
394 fhp
->fh_handle
.fh_fsid_type
= ref_fh_fsid_type
;
395 switch (ref_fh_fsid_type
) {
399 * 2byte major, 2byte minor, 4byte inode
401 mk_fsid_v0(datap
, ex_dev
,
402 exp
->ex_dentry
->d_inode
->i_ino
);
405 /* fsid_type 1 == 4 bytes filesystem id */
406 mk_fsid_v1(datap
, exp
->ex_fsid
);
411 * 4byte major, 4byte minor, 4byte inode
413 mk_fsid_v2(datap
, ex_dev
,
414 exp
->ex_dentry
->d_inode
->i_ino
);
419 * 4byte devicenumber, 4byte inode
421 mk_fsid_v3(datap
, ex_dev
,
422 exp
->ex_dentry
->d_inode
->i_ino
);
425 len
= key_len(ref_fh_fsid_type
);
427 fhp
->fh_handle
.fh_size
= 4 + len
;
430 int size
= (fhp
->fh_maxsize
-len
-4)/4;
431 fhp
->fh_handle
.fh_fileid_type
=
432 _fh_update(dentry
, exp
, datap
, &size
);
433 fhp
->fh_handle
.fh_size
+= size
*4;
435 if (fhp
->fh_handle
.fh_fileid_type
== 255)
436 return nfserr_opnotsupp
;
444 * Update file handle information after changing a dentry.
445 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
448 fh_update(struct svc_fh
*fhp
)
450 struct dentry
*dentry
;
456 dentry
= fhp
->fh_dentry
;
457 if (!dentry
->d_inode
)
459 if (fhp
->fh_handle
.fh_version
!= 1) {
460 _fh_update_old(dentry
, fhp
->fh_export
, &fhp
->fh_handle
);
463 if (fhp
->fh_handle
.fh_fileid_type
!= 0)
465 datap
= fhp
->fh_handle
.fh_auth
+
466 fhp
->fh_handle
.fh_size
/4 -1;
467 size
= (fhp
->fh_maxsize
- fhp
->fh_handle
.fh_size
)/4;
468 fhp
->fh_handle
.fh_fileid_type
=
469 _fh_update(dentry
, fhp
->fh_export
, datap
, &size
);
470 fhp
->fh_handle
.fh_size
+= size
*4;
471 if (fhp
->fh_handle
.fh_fileid_type
== 255)
472 return nfserr_opnotsupp
;
478 printk(KERN_ERR
"fh_update: fh not verified!\n");
481 printk(KERN_ERR
"fh_update: %s/%s still negative!\n",
482 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
485 printk(KERN_ERR
"fh_update: %s/%s already up-to-date!\n",
486 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
491 * Release a file handle.
494 fh_put(struct svc_fh
*fhp
)
496 struct dentry
* dentry
= fhp
->fh_dentry
;
497 struct svc_export
* exp
= fhp
->fh_export
;
500 fhp
->fh_dentry
= NULL
;
502 #ifdef CONFIG_NFSD_V3
503 fhp
->fh_pre_saved
= 0;
504 fhp
->fh_post_saved
= 0;
509 svc_export_put(&exp
->h
, &svc_export_cache
);
510 fhp
->fh_export
= NULL
;
516 * Shorthand for dprintk()'s
518 char * SVCFH_fmt(struct svc_fh
*fhp
)
520 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
523 sprintf(buf
, "%d: %08x %08x %08x %08x %08x %08x",
525 fh
->fh_base
.fh_pad
[0],
526 fh
->fh_base
.fh_pad
[1],
527 fh
->fh_base
.fh_pad
[2],
528 fh
->fh_base
.fh_pad
[3],
529 fh
->fh_base
.fh_pad
[4],
530 fh
->fh_base
.fh_pad
[5]);