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/exportfs.h>
14 #include <linux/sunrpc/svcauth_gss.h>
19 #define NFSDDBG_FACILITY NFSDDBG_FH
23 * our acceptability function.
24 * if NOSUBTREECHECK, accept anything
25 * if not, require that we can walk up to exp->ex_dentry
26 * doing some checks on the 'x' bits
28 static int nfsd_acceptable(void *expv, struct dentry *dentry)
30 struct svc_export *exp = expv;
32 struct dentry *tdentry;
33 struct dentry *parent;
35 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
38 tdentry = dget(dentry);
39 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
40 /* make sure parents give x permission to user */
42 parent = dget_parent(tdentry);
43 err = inode_permission(parent->d_inode, MAY_EXEC);
51 if (tdentry != exp->ex_path.dentry)
52 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
53 rv = (tdentry == exp->ex_path.dentry);
58 /* Type check. The correct error return for type mismatches does not seem to be
59 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
60 * comment in the NFSv3 spec says this is incorrect (implementation notes for
64 nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
66 /* Type can be negative when creating hardlinks - not to a dir */
67 if (type > 0 && (mode & S_IFMT) != type) {
68 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
69 return nfserr_symlink;
70 else if (type == S_IFDIR)
72 else if ((mode & S_IFMT) == S_IFDIR)
77 if (type < 0 && (mode & S_IFMT) == -type) {
78 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
79 return nfserr_symlink;
80 else if (type == -S_IFDIR)
88 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
89 struct svc_export *exp)
91 /* Check if the request originated from a secure port. */
92 if (!rqstp->rq_secure && EX_SECURE(exp)) {
93 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
95 "nfsd: request from insecure port %s!\n",
96 svc_print_addr(rqstp, buf, sizeof(buf)));
100 /* Set user creds for this exportpoint */
101 return nfserrno(nfsd_setuser(rqstp, exp));
105 * Use the given filehandle to look up the corresponding export and
106 * dentry. On success, the results are used to set fh_export and
109 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
111 struct knfsd_fh *fh = &fhp->fh_handle;
112 struct fid *fid = NULL, sfid;
113 struct svc_export *exp;
114 struct dentry *dentry;
116 int data_left = fh->fh_size/4;
119 error = nfserr_stale;
120 if (rqstp->rq_vers > 2)
121 error = nfserr_badhandle;
122 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
123 return nfserr_nofilehandle;
125 if (fh->fh_version == 1) {
130 if (fh->fh_auth_type != 0)
132 len = key_len(fh->fh_fsid_type) / 4;
135 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
136 /* deprecated, convert to type 3 */
137 len = key_len(FSID_ENCODE_DEV)/4;
138 fh->fh_fsid_type = FSID_ENCODE_DEV;
139 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
140 fh->fh_fsid[1] = fh->fh_fsid[2];
145 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
146 fid = (struct fid *)(fh->fh_auth + len);
152 if (fh->fh_size != NFS_FHSIZE)
154 /* assume old filehandle format */
155 xdev = old_decode_dev(fh->ofh_xdev);
156 xino = u32_to_ino_t(fh->ofh_xino);
157 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
158 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
161 error = nfserr_stale;
162 if (PTR_ERR(exp) == -ENOENT)
166 return nfserrno(PTR_ERR(exp));
168 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
169 /* Elevate privileges so that the lack of 'r' or 'x'
170 * permission on some parent directory will
171 * not stop exportfs_decode_fh from being able
172 * to reconnect a directory into the dentry cache.
173 * The same problem can affect "SUBTREECHECK" exports,
174 * but as nfsd_acceptable depends on correct
175 * access control settings being in effect, we cannot
176 * fix that case easily.
178 struct cred *new = prepare_creds();
180 return nfserrno(-ENOMEM);
182 cap_raise_nfsd_set(new->cap_effective,
184 put_cred(override_creds(new));
187 error = nfsd_setuser_and_check_port(rqstp, exp);
193 * Look up the dentry using the NFS file handle.
195 error = nfserr_stale;
196 if (rqstp->rq_vers > 2)
197 error = nfserr_badhandle;
199 if (fh->fh_version != 1) {
200 sfid.i32.ino = fh->ofh_ino;
201 sfid.i32.gen = fh->ofh_generation;
202 sfid.i32.parent_ino = fh->ofh_dirino;
205 if (fh->ofh_dirino == 0)
206 fileid_type = FILEID_INO32_GEN;
208 fileid_type = FILEID_INO32_GEN_PARENT;
210 fileid_type = fh->fh_fileid_type;
212 if (fileid_type == FILEID_ROOT)
213 dentry = dget(exp->ex_path.dentry);
215 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
216 data_left, fileid_type,
217 nfsd_acceptable, exp);
221 if (IS_ERR(dentry)) {
222 if (PTR_ERR(dentry) != -EINVAL)
223 error = nfserrno(PTR_ERR(dentry));
227 if (S_ISDIR(dentry->d_inode->i_mode) &&
228 (dentry->d_flags & DCACHE_DISCONNECTED)) {
229 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
230 dentry->d_parent->d_name.name, dentry->d_name.name);
233 fhp->fh_dentry = dentry;
234 fhp->fh_export = exp;
242 * fh_verify - filehandle lookup and access checking
243 * @rqstp: pointer to current rpc request
244 * @fhp: filehandle to be verified
245 * @type: expected type of object pointed to by filehandle
246 * @access: type of access needed to object
248 * Look up a dentry from the on-the-wire filehandle, check the client's
249 * access to the export, and set the current task's credentials.
251 * Regardless of success or failure of fh_verify(), fh_put() should be
252 * called on @fhp when the caller is finished with the filehandle.
254 * fh_verify() may be called multiple times on a given filehandle, for
255 * example, when processing an NFSv4 compound. The first call will look
256 * up a dentry using the on-the-wire filehandle. Subsequent calls will
257 * skip the lookup and just perform the other checks and possibly change
258 * the current task's credentials.
260 * @type specifies the type of object expected using one of the S_IF*
261 * constants defined in include/linux/stat.h. The caller may use zero
262 * to indicate that it doesn't care, or a negative integer to indicate
263 * that it expects something not of the given type.
265 * @access is formed from the NFSD_MAY_* constants defined in
266 * include/linux/nfsd/nfsd.h.
269 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
271 struct svc_export *exp;
272 struct dentry *dentry;
275 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
277 if (!fhp->fh_dentry) {
278 error = nfsd_set_fh_dentry(rqstp, fhp);
282 dentry = fhp->fh_dentry;
283 exp = fhp->fh_export;
285 * We still have to do all these permission checks, even when
286 * fh_dentry is already set:
287 * - fh_verify may be called multiple times with different
288 * "access" arguments (e.g. nfsd_proc_create calls
289 * fh_verify(...,NFSD_MAY_EXEC) first, then later (in
290 * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
291 * - in the NFSv4 case, the filehandle may have been filled
292 * in by fh_compose, and given a dentry, but further
293 * compound operations performed with that filehandle
294 * still need permissions checks. In the worst case, a
295 * mountpoint crossing may have changed the export
296 * options, and we may now need to use a different uid
297 * (for example, if different id-squashing options are in
298 * effect on the new filesystem).
300 error = nfsd_setuser_and_check_port(rqstp, exp);
304 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
309 * pseudoflavor restrictions are not enforced on NLM,
310 * which clients virtually always use auth_sys for,
311 * even while using RPCSEC_GSS for NFS.
313 if (access & NFSD_MAY_LOCK)
314 goto skip_pseudoflavor_check;
316 * Clients may expect to be able to use auth_sys during mount,
317 * even if they use gss for everything else; see section 2.3.2
320 if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
321 && exp->ex_path.dentry == dentry)
322 goto skip_pseudoflavor_check;
324 error = check_nfsd_access(exp, rqstp);
328 skip_pseudoflavor_check:
329 /* Finally, check access permissions. */
330 error = nfsd_permission(rqstp, exp, dentry, access);
333 dprintk("fh_verify: %s/%s permission failure, "
334 "acc=%x, error=%d\n",
335 dentry->d_parent->d_name.name,
337 access, ntohl(error));
340 if (error == nfserr_stale)
341 nfsdstats.fh_stale++;
347 * Compose a file handle for an NFS reply.
349 * Note that when first composed, the dentry may not yet have
350 * an inode. In this case a call to fh_update should be made
351 * before the fh goes out on the wire ...
353 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
354 struct dentry *dentry)
356 if (dentry != exp->ex_path.dentry) {
357 struct fid *fid = (struct fid *)
358 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
359 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
360 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
362 fhp->fh_handle.fh_fileid_type =
363 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
364 fhp->fh_handle.fh_size += maxsize * 4;
366 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
371 * for composing old style file handles
373 static inline void _fh_update_old(struct dentry *dentry,
374 struct svc_export *exp,
377 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
378 fh->ofh_generation = dentry->d_inode->i_generation;
379 if (S_ISDIR(dentry->d_inode->i_mode) ||
380 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
384 static bool is_root_export(struct svc_export *exp)
386 return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
389 static struct super_block *exp_sb(struct svc_export *exp)
391 return exp->ex_path.dentry->d_inode->i_sb;
394 static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
398 if (!old_valid_dev(exp_sb(exp)->s_dev))
401 case FSID_MAJOR_MINOR:
402 case FSID_ENCODE_DEV:
403 return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
405 return exp->ex_flags & NFSEXP_FSID;
408 if (!is_root_export(exp))
411 case FSID_UUID4_INUM:
412 case FSID_UUID16_INUM:
413 return exp->ex_uuid != NULL;
419 static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
425 if (ref_fh && ref_fh->fh_export == exp) {
426 version = ref_fh->fh_handle.fh_version;
427 fsid_type = ref_fh->fh_handle.fh_fsid_type;
433 fsid_type = FSID_DEV;
442 * As the fsid -> filesystem mapping was guided by
443 * user-space, there is no guarantee that the filesystem
444 * actually supports that fsid type. If it doesn't we
445 * loop around again without ref_fh set.
447 if (!fsid_type_ok_for_exp(fsid_type, exp))
449 } else if (exp->ex_flags & NFSEXP_FSID) {
450 fsid_type = FSID_NUM;
451 } else if (exp->ex_uuid) {
452 if (fhp->fh_maxsize >= 64) {
453 if (is_root_export(exp))
454 fsid_type = FSID_UUID16;
456 fsid_type = FSID_UUID16_INUM;
458 if (is_root_export(exp))
459 fsid_type = FSID_UUID8;
461 fsid_type = FSID_UUID4_INUM;
463 } else if (!old_valid_dev(exp_sb(exp)->s_dev))
464 /* for newer device numbers, we must use a newer fsid format */
465 fsid_type = FSID_ENCODE_DEV;
467 fsid_type = FSID_DEV;
468 fhp->fh_handle.fh_version = version;
470 fhp->fh_handle.fh_fsid_type = fsid_type;
474 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
475 struct svc_fh *ref_fh)
477 /* ref_fh is a reference file handle.
478 * if it is non-null and for the same filesystem, then we should compose
479 * a filehandle which is of the same version, where possible.
480 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
481 * Then create a 32byte filehandle using nfs_fhbase_old
485 struct inode * inode = dentry->d_inode;
486 struct dentry *parent = dentry->d_parent;
488 dev_t ex_dev = exp_sb(exp)->s_dev;
490 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
491 MAJOR(ex_dev), MINOR(ex_dev),
492 (long) exp->ex_path.dentry->d_inode->i_ino,
493 parent->d_name.name, dentry->d_name.name,
494 (inode ? inode->i_ino : 0));
496 /* Choose filehandle version and fsid type based on
497 * the reference filehandle (if it is in the same export)
498 * or the export options.
500 set_version_and_fsid_type(fhp, exp, ref_fh);
505 if (fhp->fh_locked || fhp->fh_dentry) {
506 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
507 parent->d_name.name, dentry->d_name.name);
509 if (fhp->fh_maxsize < NFS_FHSIZE)
510 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
512 parent->d_name.name, dentry->d_name.name);
514 fhp->fh_dentry = dget(dentry); /* our internal copy */
515 fhp->fh_export = exp;
518 if (fhp->fh_handle.fh_version == 0xca) {
519 /* old style filehandle please */
520 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
521 fhp->fh_handle.fh_size = NFS_FHSIZE;
522 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
523 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
524 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
525 fhp->fh_handle.ofh_xino =
526 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
527 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
529 _fh_update_old(dentry, exp, &fhp->fh_handle);
532 fhp->fh_handle.fh_auth_type = 0;
533 datap = fhp->fh_handle.fh_auth+0;
534 mk_fsid(fhp->fh_handle.fh_fsid_type, datap, ex_dev,
535 exp->ex_path.dentry->d_inode->i_ino,
536 exp->ex_fsid, exp->ex_uuid);
538 len = key_len(fhp->fh_handle.fh_fsid_type);
540 fhp->fh_handle.fh_size = 4 + len;
543 _fh_update(fhp, exp, dentry);
544 if (fhp->fh_handle.fh_fileid_type == 255) {
546 return nfserr_opnotsupp;
554 * Update file handle information after changing a dentry.
555 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
558 fh_update(struct svc_fh *fhp)
560 struct dentry *dentry;
565 dentry = fhp->fh_dentry;
566 if (!dentry->d_inode)
568 if (fhp->fh_handle.fh_version != 1) {
569 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
571 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
574 _fh_update(fhp, fhp->fh_export, dentry);
575 if (fhp->fh_handle.fh_fileid_type == 255)
576 return nfserr_opnotsupp;
582 printk(KERN_ERR "fh_update: fh not verified!\n");
585 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
586 dentry->d_parent->d_name.name, dentry->d_name.name);
591 * Release a file handle.
594 fh_put(struct svc_fh *fhp)
596 struct dentry * dentry = fhp->fh_dentry;
597 struct svc_export * exp = fhp->fh_export;
600 fhp->fh_dentry = NULL;
602 #ifdef CONFIG_NFSD_V3
603 fhp->fh_pre_saved = 0;
604 fhp->fh_post_saved = 0;
608 cache_put(&exp->h, &svc_export_cache);
609 fhp->fh_export = NULL;
615 * Shorthand for dprintk()'s
617 char * SVCFH_fmt(struct svc_fh *fhp)
619 struct knfsd_fh *fh = &fhp->fh_handle;
622 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
624 fh->fh_base.fh_pad[0],
625 fh->fh_base.fh_pad[1],
626 fh->fh_base.fh_pad[2],
627 fh->fh_base.fh_pad[3],
628 fh->fh_base.fh_pad[4],
629 fh->fh_base.fh_pad[5]);
633 enum fsid_source fsid_source(struct svc_fh *fhp)
635 if (fhp->fh_handle.fh_version != 1)
636 return FSIDSOURCE_DEV;
637 switch(fhp->fh_handle.fh_fsid_type) {
639 case FSID_ENCODE_DEV:
640 case FSID_MAJOR_MINOR:
641 if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
642 return FSIDSOURCE_DEV;
645 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
646 return FSIDSOURCE_FSID;
651 /* either a UUID type filehandle, or the filehandle doesn't
654 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
655 return FSIDSOURCE_FSID;
656 if (fhp->fh_export->ex_uuid)
657 return FSIDSOURCE_UUID;
658 return FSIDSOURCE_DEV;