2 * linux/fs/nfsd/nfsfh.c
4 * NFS server file handle treatment.
9 * ... and again Southern-Winter 2001 to support export_operations
12 #include <linux/slab.h>
13 #include <linux/smp_lock.h>
15 #include <linux/unistd.h>
16 #include <linux/string.h>
17 #include <linux/stat.h>
18 #include <linux/dcache.h>
19 #include <linux/mount.h>
20 #include <asm/pgtable.h>
22 #include <linux/sunrpc/clnt.h>
23 #include <linux/sunrpc/svc.h>
24 #include <linux/nfsd/nfsd.h>
26 #define NFSDDBG_FACILITY NFSDDBG_FH
29 static int nfsd_nr_verified;
30 static int nfsd_nr_put;
32 extern struct export_operations export_op_default;
34 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
37 * our acceptability function.
38 * if NOSUBTREECHECK, accept anything
39 * if not, require that we can walk up to exp->ex_dentry
40 * doing some checks on the 'x' bits
42 static int nfsd_acceptable(void *expv, struct dentry *dentry)
44 struct svc_export *exp = expv;
46 struct dentry *tdentry;
47 struct dentry *parent;
49 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
52 tdentry = dget(dentry);
53 while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
54 /* make sure parents give x permission to user */
56 parent = dget_parent(tdentry);
57 err = permission(parent->d_inode, MAY_EXEC, NULL);
65 if (tdentry != exp->ex_dentry)
66 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
67 rv = (tdentry == exp->ex_dentry);
72 /* Type check. The correct error return for type mismatches does not seem to be
73 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
74 * comment in the NFSv3 spec says this is incorrect (implementation notes for
78 nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
80 /* Type can be negative when creating hardlinks - not to a dir */
81 if (type > 0 && (mode & S_IFMT) != type) {
82 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
83 return nfserr_symlink;
84 else if (type == S_IFDIR)
86 else if ((mode & S_IFMT) == S_IFDIR)
91 if (type < 0 && (mode & S_IFMT) == -type) {
92 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
93 return nfserr_symlink;
94 else if (type == -S_IFDIR)
103 * Perform sanity checks on the dentry in a client's file handle.
105 * Note that the file handle dentry may need to be freed even after
108 * This is only called at the start of an nfsproc call, so fhp points to
109 * a svc_fh which is all 0 except for the over-the-wire file handle.
112 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
114 struct knfsd_fh *fh = &fhp->fh_handle;
115 struct svc_export *exp = NULL;
116 struct dentry *dentry;
119 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
121 if (!fhp->fh_dentry) {
123 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
125 int data_left = fh->fh_size/4;
127 error = nfserr_stale;
128 if (rqstp->rq_client == NULL)
130 if (rqstp->rq_vers > 2)
131 error = nfserr_badhandle;
132 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
133 return nfserr_nofilehandle;
135 if (fh->fh_version == 1) {
138 if (--data_left<0) goto out;
139 switch (fh->fh_auth_type) {
143 len = key_len(fh->fh_fsid_type) / 4;
144 if (len == 0) goto out;
145 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
146 /* deprecated, convert to type 3 */
147 len = key_len(FSID_ENCODE_DEV)/4;
148 fh->fh_fsid_type = FSID_ENCODE_DEV;
149 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
150 fh->fh_fsid[1] = fh->fh_fsid[2];
152 if ((data_left -= len)<0) goto out;
153 exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
158 if (fh->fh_size != NFS_FHSIZE)
160 /* assume old filehandle format */
161 xdev = old_decode_dev(fh->ofh_xdev);
162 xino = u32_to_ino_t(fh->ofh_xino);
163 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
164 exp = exp_find(rqstp->rq_client, FSID_DEV, tfh,
168 if (IS_ERR(exp) && (PTR_ERR(exp) == -EAGAIN
169 || PTR_ERR(exp) == -ETIMEDOUT)) {
170 error = nfserrno(PTR_ERR(exp));
174 error = nfserr_stale;
175 if (!exp || IS_ERR(exp))
178 /* Check if the request originated from a secure port. */
180 if (!rqstp->rq_secure && EX_SECURE(exp)) {
181 char buf[RPC_MAX_ADDRBUFLEN];
183 "nfsd: request from insecure port %s!\n",
184 svc_print_addr(rqstp, buf, sizeof(buf)));
188 /* Set user creds for this exportpoint */
189 error = nfserrno(nfsd_setuser(rqstp, exp));
194 * Look up the dentry using the NFS file handle.
196 error = nfserr_stale;
197 if (rqstp->rq_vers > 2)
198 error = nfserr_badhandle;
200 if (fh->fh_version != 1) {
201 tfh[0] = fh->ofh_ino;
202 tfh[1] = fh->ofh_generation;
203 tfh[2] = fh->ofh_dirino;
206 if (fh->ofh_dirino == 0)
211 fileid_type = fh->fh_fileid_type;
213 if (fileid_type == 0)
214 dentry = dget(exp->ex_dentry);
216 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
217 dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
220 nfsd_acceptable, exp);
224 if (IS_ERR(dentry)) {
225 if (PTR_ERR(dentry) != -EINVAL)
226 error = nfserrno(PTR_ERR(dentry));
230 if (S_ISDIR(dentry->d_inode->i_mode) &&
231 (dentry->d_flags & DCACHE_DISCONNECTED)) {
232 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
233 dentry->d_parent->d_name.name, dentry->d_name.name);
236 fhp->fh_dentry = dentry;
237 fhp->fh_export = exp;
240 /* just rechecking permissions
241 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
243 dprintk("nfsd: fh_verify - just checking\n");
244 dentry = fhp->fh_dentry;
245 exp = fhp->fh_export;
246 /* Set user creds for this exportpoint; necessary even
247 * in the "just checking" case because this may be a
248 * filehandle that was created by fh_compose, and that
249 * is about to be used in another nfsv4 compound
251 error = nfserrno(nfsd_setuser(rqstp, exp));
258 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
262 /* Finally, check access permissions. */
263 error = nfsd_permission(exp, dentry, access);
266 dprintk("fh_verify: %s/%s permission failure, "
267 "acc=%x, error=%d\n",
268 dentry->d_parent->d_name.name,
270 access, ntohl(error));
273 if (exp && !IS_ERR(exp))
275 if (error == nfserr_stale)
276 nfsdstats.fh_stale++;
282 * Compose a file handle for an NFS reply.
284 * Note that when first composed, the dentry may not yet have
285 * an inode. In this case a call to fh_update should be made
286 * before the fh goes out on the wire ...
288 static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
289 __u32 *datap, int *maxsize)
291 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
293 if (dentry == exp->ex_dentry) {
298 return CALL(nop,encode_fh)(dentry, datap, maxsize,
299 !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
303 * for composing old style file handles
305 static inline void _fh_update_old(struct dentry *dentry,
306 struct svc_export *exp,
309 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
310 fh->ofh_generation = dentry->d_inode->i_generation;
311 if (S_ISDIR(dentry->d_inode->i_mode) ||
312 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
317 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
318 struct svc_fh *ref_fh)
320 /* ref_fh is a reference file handle.
321 * if it is non-null and for the same filesystem, then we should compose
322 * a filehandle which is of the same version, where possible.
323 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
324 * Then create a 32byte filehandle using nfs_fhbase_old
330 struct inode * inode = dentry->d_inode;
331 struct dentry *parent = dentry->d_parent;
333 dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
334 int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
336 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
337 MAJOR(ex_dev), MINOR(ex_dev),
338 (long) exp->ex_dentry->d_inode->i_ino,
339 parent->d_name.name, dentry->d_name.name,
340 (inode ? inode->i_ino : 0));
342 /* Choose filehandle version and fsid type based on
343 * the reference filehandle (if it is in the same export)
344 * or the export options.
346 if (ref_fh && ref_fh->fh_export == exp) {
347 version = ref_fh->fh_handle.fh_version;
349 fsid_type = FSID_DEV;
351 fsid_type = ref_fh->fh_handle.fh_fsid_type;
352 /* We know this version/type works for this export
353 * so there is no need for further checks.
355 } else if (exp->ex_uuid) {
356 if (fhp->fh_maxsize >= 64) {
358 fsid_type = FSID_UUID16;
360 fsid_type = FSID_UUID16_INUM;
363 fsid_type = FSID_UUID8;
365 fsid_type = FSID_UUID4_INUM;
367 } else if (exp->ex_flags & NFSEXP_FSID)
368 fsid_type = FSID_NUM;
369 else if (!old_valid_dev(ex_dev))
370 /* for newer device numbers, we must use a newer fsid format */
371 fsid_type = FSID_ENCODE_DEV;
373 fsid_type = FSID_DEV;
378 if (fhp->fh_locked || fhp->fh_dentry) {
379 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
380 parent->d_name.name, dentry->d_name.name);
382 if (fhp->fh_maxsize < NFS_FHSIZE)
383 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
385 parent->d_name.name, dentry->d_name.name);
387 fhp->fh_dentry = dget(dentry); /* our internal copy */
388 fhp->fh_export = exp;
391 if (version == 0xca) {
392 /* old style filehandle please */
393 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
394 fhp->fh_handle.fh_size = NFS_FHSIZE;
395 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
396 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
397 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
398 fhp->fh_handle.ofh_xino =
399 ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
400 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
402 _fh_update_old(dentry, exp, &fhp->fh_handle);
405 fhp->fh_handle.fh_version = 1;
406 fhp->fh_handle.fh_auth_type = 0;
407 datap = fhp->fh_handle.fh_auth+0;
408 fhp->fh_handle.fh_fsid_type = fsid_type;
409 mk_fsid(fsid_type, datap, ex_dev,
410 exp->ex_dentry->d_inode->i_ino,
411 exp->ex_fsid, exp->ex_uuid);
413 len = key_len(fsid_type);
415 fhp->fh_handle.fh_size = 4 + len;
418 int size = (fhp->fh_maxsize-len-4)/4;
419 fhp->fh_handle.fh_fileid_type =
420 _fh_update(dentry, exp, datap, &size);
421 fhp->fh_handle.fh_size += size*4;
423 if (fhp->fh_handle.fh_fileid_type == 255)
424 return nfserr_opnotsupp;
432 * Update file handle information after changing a dentry.
433 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
436 fh_update(struct svc_fh *fhp)
438 struct dentry *dentry;
444 dentry = fhp->fh_dentry;
445 if (!dentry->d_inode)
447 if (fhp->fh_handle.fh_version != 1) {
448 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
451 if (fhp->fh_handle.fh_fileid_type != 0)
453 datap = fhp->fh_handle.fh_auth+
454 fhp->fh_handle.fh_size/4 -1;
455 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
456 fhp->fh_handle.fh_fileid_type =
457 _fh_update(dentry, fhp->fh_export, datap, &size);
458 fhp->fh_handle.fh_size += size*4;
459 if (fhp->fh_handle.fh_fileid_type == 255)
460 return nfserr_opnotsupp;
466 printk(KERN_ERR "fh_update: fh not verified!\n");
469 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
470 dentry->d_parent->d_name.name, dentry->d_name.name);
475 * Release a file handle.
478 fh_put(struct svc_fh *fhp)
480 struct dentry * dentry = fhp->fh_dentry;
481 struct svc_export * exp = fhp->fh_export;
484 fhp->fh_dentry = NULL;
486 #ifdef CONFIG_NFSD_V3
487 fhp->fh_pre_saved = 0;
488 fhp->fh_post_saved = 0;
493 cache_put(&exp->h, &svc_export_cache);
494 fhp->fh_export = NULL;
500 * Shorthand for dprintk()'s
502 char * SVCFH_fmt(struct svc_fh *fhp)
504 struct knfsd_fh *fh = &fhp->fh_handle;
507 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
509 fh->fh_base.fh_pad[0],
510 fh->fh_base.fh_pad[1],
511 fh->fh_base.fh_pad[2],
512 fh->fh_base.fh_pad[3],
513 fh->fh_base.fh_pad[4],
514 fh->fh_base.fh_pad[5]);
518 enum fsid_source fsid_source(struct svc_fh *fhp)
520 if (fhp->fh_handle.fh_version != 1)
521 return FSIDSOURCE_DEV;
522 switch(fhp->fh_handle.fh_fsid_type) {
524 case FSID_ENCODE_DEV:
525 case FSID_MAJOR_MINOR:
526 return FSIDSOURCE_DEV;
528 return FSIDSOURCE_FSID;
530 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
531 return FSIDSOURCE_FSID;
533 return FSIDSOURCE_UUID;