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 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 read_lock(&dparent_lock
);
59 parent
= dget(tdentry
->d_parent
);
60 read_unlock(&dparent_lock
);
61 err
= permission(parent
->d_inode
, S_IXOTH
);
69 if (tdentry
!= exp
->ex_dentry
)
70 dprintk("nfsd_acceptable failed at %p %s\n", tdentry
, tdentry
->d_name
.name
);
71 rv
= (tdentry
== exp
->ex_dentry
);
78 * Perform sanity checks on the dentry in a client's file handle.
80 * Note that the file handle dentry may need to be freed even after
83 * This is only called at the start of an nfsproc call, so fhp points to
84 * a svc_fh which is all 0 except for the over-the-wire file handle.
87 fh_verify(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
, int access
)
89 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
90 struct svc_export
*exp
= NULL
;
91 struct dentry
*dentry
;
95 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp
));
97 /* keep this filehandle for possible reference when encoding attributes */
100 if (!fhp
->fh_dentry
) {
102 __u32 tfh
[3]; /* filehandle fragment for oldstyle filehandles */
104 int data_left
= fh
->fh_size
/4;
106 error
= nfserr_stale
;
107 if (rqstp
->rq_client
== NULL
)
109 if (rqstp
->rq_vers
> 2)
110 error
= nfserr_badhandle
;
111 if (rqstp
->rq_vers
== 4 && fh
->fh_size
== 0)
112 return nfserr_nofilehandle
;
114 if (fh
->fh_version
== 1) {
117 if (--data_left
<0) goto out
;
118 switch (fh
->fh_auth_type
) {
123 switch (fh
->fh_fsid_type
) {
133 if ((data_left
-= len
)<0) goto out
;
134 exp
= exp_find(rqstp
->rq_client
, fh
->fh_fsid_type
, datap
, &rqstp
->rq_chandle
);
139 if (fh
->fh_size
!= NFS_FHSIZE
)
141 /* assume old filehandle format */
142 xdev
= u32_to_dev_t(fh
->ofh_xdev
);
143 xino
= u32_to_ino_t(fh
->ofh_xino
);
144 mk_fsid_v0(tfh
, xdev
, xino
);
145 exp
= exp_find(rqstp
->rq_client
, 0, tfh
, &rqstp
->rq_chandle
);
148 error
= nfserr_dropit
;
152 error
= nfserr_stale
;
156 /* Check if the request originated from a secure port. */
158 if (!rqstp
->rq_secure
&& EX_SECURE(exp
)) {
160 "nfsd: request from insecure port (%08x:%d)!\n",
161 ntohl(rqstp
->rq_addr
.sin_addr
.s_addr
),
162 ntohs(rqstp
->rq_addr
.sin_port
));
166 /* Set user creds if we haven't done so already. */
167 nfsd_setuser(rqstp
, exp
);
170 * Look up the dentry using the NFS file handle.
172 error
= nfserr_stale
;
173 if (rqstp
->rq_vers
> 2)
174 error
= nfserr_badhandle
;
176 if (fh
->fh_version
!= 1) {
177 tfh
[0] = fh
->ofh_ino
;
178 tfh
[1] = fh
->ofh_generation
;
179 tfh
[2] = fh
->ofh_dirino
;
182 if (fh
->ofh_dirino
== 0)
187 fileid_type
= fh
->fh_fileid_type
;
189 if (fileid_type
== 0)
190 dentry
= dget(exp
->ex_dentry
);
192 struct export_operations
*nop
= exp
->ex_mnt
->mnt_sb
->s_export_op
;
193 dentry
= CALL(nop
,decode_fh
)(exp
->ex_mnt
->mnt_sb
,
196 nfsd_acceptable
, exp
);
200 if (IS_ERR(dentry
)) {
201 if (PTR_ERR(dentry
) != -EINVAL
)
202 error
= nfserrno(PTR_ERR(dentry
));
206 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
207 (dentry
->d_flags
& DCACHE_DISCONNECTED
)) {
208 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
209 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
213 fhp
->fh_dentry
= dentry
;
214 fhp
->fh_export
= exp
;
217 /* just rechecking permissions
218 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
220 dprintk("nfsd: fh_verify - just checking\n");
221 dentry
= fhp
->fh_dentry
;
222 exp
= fhp
->fh_export
;
226 inode
= dentry
->d_inode
;
228 /* Type check. The correct error return for type mismatches
229 * does not seem to be generally agreed upon. SunOS seems to
230 * use EISDIR if file isn't S_IFREG; a comment in the NFSv3
231 * spec says this is incorrect (implementation notes for the
235 /* Type can be negative when creating hardlinks - not to a dir */
236 if (type
> 0 && (inode
->i_mode
& S_IFMT
) != type
) {
237 if (rqstp
->rq_vers
== 4 && (inode
->i_mode
& S_IFMT
) == S_IFLNK
)
238 error
= nfserr_symlink
;
239 else if (type
== S_IFDIR
)
240 error
= nfserr_notdir
;
241 else if ((inode
->i_mode
& S_IFMT
) == S_IFDIR
)
242 error
= nfserr_isdir
;
244 error
= nfserr_inval
;
247 if (type
< 0 && (inode
->i_mode
& S_IFMT
) == -type
) {
248 if (rqstp
->rq_vers
== 4 && (inode
->i_mode
& S_IFMT
) == S_IFLNK
)
249 error
= nfserr_symlink
;
250 else if (type
== -S_IFDIR
)
251 error
= nfserr_isdir
;
253 error
= nfserr_notdir
;
257 /* Finally, check access permissions. */
258 error
= nfsd_permission(exp
, dentry
, access
);
260 #ifdef NFSD_PARANOIA_EXTREME
262 printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
263 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, access
, (error
>> 24));
267 if (exp
&& !IS_ERR(exp
))
269 if (error
== nfserr_stale
)
270 nfsdstats
.fh_stale
++;
276 * Compose a file handle for an NFS reply.
278 * Note that when first composed, the dentry may not yet have
279 * an inode. In this case a call to fh_update should be made
280 * before the fh goes out on the wire ...
282 inline int _fh_update(struct dentry
*dentry
, struct svc_export
*exp
,
283 __u32
*datap
, int *maxsize
)
285 struct export_operations
*nop
= exp
->ex_mnt
->mnt_sb
->s_export_op
;
287 if (dentry
== exp
->ex_dentry
) {
292 return CALL(nop
,encode_fh
)(dentry
, datap
, maxsize
,
293 !(exp
->ex_flags
&NFSEXP_NOSUBTREECHECK
));
297 * for composing old style file handles
299 inline void _fh_update_old(struct dentry
*dentry
, struct svc_export
*exp
,
302 fh
->ofh_ino
= ino_t_to_u32(dentry
->d_inode
->i_ino
);
303 fh
->ofh_generation
= dentry
->d_inode
->i_generation
;
304 if (S_ISDIR(dentry
->d_inode
->i_mode
) ||
305 (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
))
310 fh_compose(struct svc_fh
*fhp
, struct svc_export
*exp
, struct dentry
*dentry
, struct svc_fh
*ref_fh
)
312 /* ref_fh is a reference file handle.
313 * if it is non-null, then we should compose a filehandle which is
314 * of the same version, where possible.
315 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
316 * Then create a 32byte filehandle using nfs_fhbase_old
320 u8 ref_fh_version
= 0;
321 u8 ref_fh_fsid_type
= 0;
322 struct inode
* inode
= dentry
->d_inode
;
323 struct dentry
*parent
= dentry
->d_parent
;
325 dev_t ex_dev
= exp
->ex_dentry
->d_inode
->i_sb
->s_dev
;
327 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
328 MAJOR(ex_dev
), MINOR(ex_dev
),
329 (long) exp
->ex_dentry
->d_inode
->i_ino
,
330 parent
->d_name
.name
, dentry
->d_name
.name
,
331 (inode
? inode
->i_ino
: 0));
334 ref_fh_version
= ref_fh
->fh_handle
.fh_version
;
335 ref_fh_fsid_type
= ref_fh
->fh_handle
.fh_fsid_type
;
340 if (fhp
->fh_locked
|| fhp
->fh_dentry
) {
341 printk(KERN_ERR
"fh_compose: fh %s/%s not initialized!\n",
342 parent
->d_name
.name
, dentry
->d_name
.name
);
344 if (fhp
->fh_maxsize
< NFS_FHSIZE
)
345 printk(KERN_ERR
"fh_compose: called with maxsize %d! %s/%s\n",
346 fhp
->fh_maxsize
, parent
->d_name
.name
, dentry
->d_name
.name
);
348 fhp
->fh_dentry
= dentry
; /* our internal copy */
349 fhp
->fh_export
= exp
;
352 if (ref_fh_version
== 0xca) {
353 /* old style filehandle please */
354 memset(&fhp
->fh_handle
.fh_base
, 0, NFS_FHSIZE
);
355 fhp
->fh_handle
.fh_size
= NFS_FHSIZE
;
356 fhp
->fh_handle
.ofh_dcookie
= 0xfeebbaca;
357 fhp
->fh_handle
.ofh_dev
= htonl((MAJOR(ex_dev
)<<16)| MINOR(ex_dev
));
358 fhp
->fh_handle
.ofh_xdev
= fhp
->fh_handle
.ofh_dev
;
359 fhp
->fh_handle
.ofh_xino
= ino_t_to_u32(exp
->ex_dentry
->d_inode
->i_ino
);
360 fhp
->fh_handle
.ofh_dirino
= ino_t_to_u32(parent_ino(dentry
));
362 _fh_update_old(dentry
, exp
, &fhp
->fh_handle
);
364 fhp
->fh_handle
.fh_version
= 1;
365 fhp
->fh_handle
.fh_auth_type
= 0;
366 datap
= fhp
->fh_handle
.fh_auth
+0;
367 if ((exp
->ex_flags
& NFSEXP_FSID
) &&
368 (ref_fh_fsid_type
== 1)) {
369 fhp
->fh_handle
.fh_fsid_type
= 1;
370 /* fsid_type 1 == 4 bytes filesystem id */
371 mk_fsid_v1(datap
, exp
->ex_fsid
);
373 fhp
->fh_handle
.fh_size
= 2*4;
375 fhp
->fh_handle
.fh_fsid_type
= 0;
376 /* fsid_type 0 == 2byte major, 2byte minor, 4byte inode */
377 mk_fsid_v0(datap
, ex_dev
, exp
->ex_dentry
->d_inode
->i_ino
);
379 fhp
->fh_handle
.fh_size
= 3*4;
382 int size
= fhp
->fh_maxsize
/4 - 3;
383 fhp
->fh_handle
.fh_fileid_type
=
384 _fh_update(dentry
, exp
, datap
, &size
);
385 fhp
->fh_handle
.fh_size
+= size
*4;
387 if (fhp
->fh_handle
.fh_fileid_type
== 255)
388 return nfserr_opnotsupp
;
396 * Update file handle information after changing a dentry.
397 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
400 fh_update(struct svc_fh
*fhp
)
402 struct dentry
*dentry
;
408 dentry
= fhp
->fh_dentry
;
409 if (!dentry
->d_inode
)
411 if (fhp
->fh_handle
.fh_version
!= 1) {
412 _fh_update_old(dentry
, fhp
->fh_export
, &fhp
->fh_handle
);
415 if (fhp
->fh_handle
.fh_fileid_type
!= 0)
417 datap
= fhp
->fh_handle
.fh_auth
+
418 fhp
->fh_handle
.fh_size
/4 -1;
419 size
= (fhp
->fh_maxsize
- fhp
->fh_handle
.fh_size
)/4;
420 fhp
->fh_handle
.fh_fileid_type
=
421 _fh_update(dentry
, fhp
->fh_export
, datap
, &size
);
422 fhp
->fh_handle
.fh_size
+= size
*4;
423 if (fhp
->fh_handle
.fh_fileid_type
== 255)
424 return nfserr_opnotsupp
;
430 printk(KERN_ERR
"fh_update: fh not verified!\n");
433 printk(KERN_ERR
"fh_update: %s/%s still negative!\n",
434 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
437 printk(KERN_ERR
"fh_update: %s/%s already up-to-date!\n",
438 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
443 * Release a file handle.
446 fh_put(struct svc_fh
*fhp
)
448 struct dentry
* dentry
= fhp
->fh_dentry
;
449 struct svc_export
* exp
= fhp
->fh_export
;
452 fhp
->fh_dentry
= NULL
;
454 #ifdef CONFIG_NFSD_V3
455 fhp
->fh_pre_saved
= 0;
456 fhp
->fh_post_saved
= 0;
461 svc_export_put(&exp
->h
, &svc_export_cache
);
462 fhp
->fh_export
= NULL
;