11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NFS server file handle treatment. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> 51da177e4SLinus Torvalds * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org> 61da177e4SLinus Torvalds * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999 71da177e4SLinus Torvalds * ... and again Southern-Winter 2001 to support export_operations 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 10a5694255SChristoph Hellwig #include <linux/exportfs.h> 111da177e4SLinus Torvalds 1232c1eb0cSAndy Adamson #include <linux/sunrpc/svcauth_gss.h> 139a74af21SBoaz Harrosh #include "nfsd.h" 140a3adadeSJ. Bruce Fields #include "vfs.h" 152e8138a2SJ. Bruce Fields #include "auth.h" 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #define NFSDDBG_FACILITY NFSDDBG_FH 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds /* 211da177e4SLinus Torvalds * our acceptability function. 221da177e4SLinus Torvalds * if NOSUBTREECHECK, accept anything 231da177e4SLinus Torvalds * if not, require that we can walk up to exp->ex_dentry 241da177e4SLinus Torvalds * doing some checks on the 'x' bits 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds static int nfsd_acceptable(void *expv, struct dentry *dentry) 271da177e4SLinus Torvalds { 281da177e4SLinus Torvalds struct svc_export *exp = expv; 291da177e4SLinus Torvalds int rv; 301da177e4SLinus Torvalds struct dentry *tdentry; 311da177e4SLinus Torvalds struct dentry *parent; 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) 341da177e4SLinus Torvalds return 1; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds tdentry = dget(dentry); 3754775491SJan Blunck while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) { 381da177e4SLinus Torvalds /* make sure parents give x permission to user */ 391da177e4SLinus Torvalds int err; 401da177e4SLinus Torvalds parent = dget_parent(tdentry); 41f419a2e3SAl Viro err = inode_permission(parent->d_inode, MAY_EXEC); 421da177e4SLinus Torvalds if (err < 0) { 431da177e4SLinus Torvalds dput(parent); 441da177e4SLinus Torvalds break; 451da177e4SLinus Torvalds } 461da177e4SLinus Torvalds dput(tdentry); 471da177e4SLinus Torvalds tdentry = parent; 481da177e4SLinus Torvalds } 4954775491SJan Blunck if (tdentry != exp->ex_path.dentry) 5097e47fa1SAl Viro dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry); 5154775491SJan Blunck rv = (tdentry == exp->ex_path.dentry); 521da177e4SLinus Torvalds dput(tdentry); 531da177e4SLinus Torvalds return rv; 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* Type check. The correct error return for type mismatches does not seem to be 571da177e4SLinus Torvalds * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a 581da177e4SLinus Torvalds * comment in the NFSv3 spec says this is incorrect (implementation notes for 591da177e4SLinus Torvalds * the write call). 601da177e4SLinus Torvalds */ 6183b11340SAl Viro static inline __be32 62175a4eb7SAl Viro nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, umode_t requested) 631da177e4SLinus Torvalds { 64e10f9e14SJ. Bruce Fields mode &= S_IFMT; 65e10f9e14SJ. Bruce Fields 66e10f9e14SJ. Bruce Fields if (requested == 0) /* the caller doesn't care */ 67e10f9e14SJ. Bruce Fields return nfs_ok; 68e10f9e14SJ. Bruce Fields if (mode == requested) 69e10f9e14SJ. Bruce Fields return nfs_ok; 70e10f9e14SJ. Bruce Fields /* 71e10f9e14SJ. Bruce Fields * v4 has an error more specific than err_notdir which we should 72e10f9e14SJ. Bruce Fields * return in preference to err_notdir: 73e10f9e14SJ. Bruce Fields */ 74e10f9e14SJ. Bruce Fields if (rqstp->rq_vers == 4 && mode == S_IFLNK) 751da177e4SLinus Torvalds return nfserr_symlink; 76e10f9e14SJ. Bruce Fields if (requested == S_IFDIR) 771da177e4SLinus Torvalds return nfserr_notdir; 78e10f9e14SJ. Bruce Fields if (mode == S_IFDIR) 791da177e4SLinus Torvalds return nfserr_isdir; 801da177e4SLinus Torvalds return nfserr_inval; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 836fa02839SJ. Bruce Fields static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp, 846fa02839SJ. Bruce Fields struct svc_export *exp) 856fa02839SJ. Bruce Fields { 8612045a6eSJ. Bruce Fields int flags = nfsexp_flags(rqstp, exp); 8712045a6eSJ. Bruce Fields 886fa02839SJ. Bruce Fields /* Check if the request originated from a secure port. */ 893d354cbcSJ. Bruce Fields if (!rqstp->rq_secure && !(flags & NFSEXP_INSECURE_PORT)) { 905216a8e7SPavel Emelyanov RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); 91a48fd0f9SKinglong Mee dprintk("nfsd: request from insecure port %s!\n", 926fa02839SJ. Bruce Fields svc_print_addr(rqstp, buf, sizeof(buf))); 936fa02839SJ. Bruce Fields return nfserr_perm; 946fa02839SJ. Bruce Fields } 956fa02839SJ. Bruce Fields 966fa02839SJ. Bruce Fields /* Set user creds for this exportpoint */ 976fa02839SJ. Bruce Fields return nfserrno(nfsd_setuser(rqstp, exp)); 986fa02839SJ. Bruce Fields } 996fa02839SJ. Bruce Fields 10003a816b4SSteve Dickson static inline __be32 check_pseudo_root(struct svc_rqst *rqstp, 10103a816b4SSteve Dickson struct dentry *dentry, struct svc_export *exp) 10203a816b4SSteve Dickson { 10303a816b4SSteve Dickson if (!(exp->ex_flags & NFSEXP_V4ROOT)) 10403a816b4SSteve Dickson return nfs_ok; 10503a816b4SSteve Dickson /* 10603a816b4SSteve Dickson * v2/v3 clients have no need for the V4ROOT export--they use 10703a816b4SSteve Dickson * the mount protocl instead; also, further V4ROOT checks may be 10803a816b4SSteve Dickson * in v4-specific code, in which case v2/v3 clients could bypass 10903a816b4SSteve Dickson * them. 11003a816b4SSteve Dickson */ 11103a816b4SSteve Dickson if (!nfsd_v4client(rqstp)) 11203a816b4SSteve Dickson return nfserr_stale; 11303a816b4SSteve Dickson /* 11403a816b4SSteve Dickson * We're exposing only the directories and symlinks that have to be 11503a816b4SSteve Dickson * traversed on the way to real exports: 11603a816b4SSteve Dickson */ 11703a816b4SSteve Dickson if (unlikely(!S_ISDIR(dentry->d_inode->i_mode) && 11803a816b4SSteve Dickson !S_ISLNK(dentry->d_inode->i_mode))) 11903a816b4SSteve Dickson return nfserr_stale; 12003a816b4SSteve Dickson /* 12103a816b4SSteve Dickson * A pseudoroot export gives permission to access only one 12203a816b4SSteve Dickson * single directory; the kernel has to make another upcall 12303a816b4SSteve Dickson * before granting access to anything else under it: 12403a816b4SSteve Dickson */ 12503a816b4SSteve Dickson if (unlikely(dentry != exp->ex_path.dentry)) 12603a816b4SSteve Dickson return nfserr_stale; 12703a816b4SSteve Dickson return nfs_ok; 12803a816b4SSteve Dickson } 12903a816b4SSteve Dickson 1301da177e4SLinus Torvalds /* 13103550facSJ. Bruce Fields * Use the given filehandle to look up the corresponding export and 13203550facSJ. Bruce Fields * dentry. On success, the results are used to set fh_export and 13303550facSJ. Bruce Fields * fh_dentry. 1341da177e4SLinus Torvalds */ 13503550facSJ. Bruce Fields static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds struct knfsd_fh *fh = &fhp->fh_handle; 1386e91ea2bSChristoph Hellwig struct fid *fid = NULL, sfid; 13903550facSJ. Bruce Fields struct svc_export *exp; 14003550facSJ. Bruce Fields struct dentry *dentry; 1411da177e4SLinus Torvalds int fileid_type; 1421da177e4SLinus Torvalds int data_left = fh->fh_size/4; 14303550facSJ. Bruce Fields __be32 error; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds error = nfserr_stale; 1461da177e4SLinus Torvalds if (rqstp->rq_vers > 2) 1471da177e4SLinus Torvalds error = nfserr_badhandle; 1481da177e4SLinus Torvalds if (rqstp->rq_vers == 4 && fh->fh_size == 0) 1491da177e4SLinus Torvalds return nfserr_nofilehandle; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds if (fh->fh_version == 1) { 1521da177e4SLinus Torvalds int len; 15303550facSJ. Bruce Fields 15403550facSJ. Bruce Fields if (--data_left < 0) 15503550facSJ. Bruce Fields return error; 15603550facSJ. Bruce Fields if (fh->fh_auth_type != 0) 15703550facSJ. Bruce Fields return error; 1581da177e4SLinus Torvalds len = key_len(fh->fh_fsid_type) / 4; 15903550facSJ. Bruce Fields if (len == 0) 16003550facSJ. Bruce Fields return error; 161af6a4e28SNeilBrown if (fh->fh_fsid_type == FSID_MAJOR_MINOR) { 1621da177e4SLinus Torvalds /* deprecated, convert to type 3 */ 163af6a4e28SNeilBrown len = key_len(FSID_ENCODE_DEV)/4; 164af6a4e28SNeilBrown fh->fh_fsid_type = FSID_ENCODE_DEV; 16594ec938bSJeff Layton /* 16694ec938bSJeff Layton * struct knfsd_fh uses host-endian fields, which are 16794ec938bSJeff Layton * sometimes used to hold net-endian values. This 16894ec938bSJeff Layton * confuses sparse, so we must use __force here to 16994ec938bSJeff Layton * keep it from complaining. 17094ec938bSJeff Layton */ 17194ec938bSJeff Layton fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]), 17294ec938bSJeff Layton ntohl((__force __be32)fh->fh_fsid[1]))); 1731da177e4SLinus Torvalds fh->fh_fsid[1] = fh->fh_fsid[2]; 1741da177e4SLinus Torvalds } 17503550facSJ. Bruce Fields data_left -= len; 17603550facSJ. Bruce Fields if (data_left < 0) 17703550facSJ. Bruce Fields return error; 1785409e46fSChristoph Hellwig exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid); 1795409e46fSChristoph Hellwig fid = (struct fid *)(fh->fh_fsid + len); 1801da177e4SLinus Torvalds } else { 1816e91ea2bSChristoph Hellwig __u32 tfh[2]; 1821da177e4SLinus Torvalds dev_t xdev; 1831da177e4SLinus Torvalds ino_t xino; 18403550facSJ. Bruce Fields 1851da177e4SLinus Torvalds if (fh->fh_size != NFS_FHSIZE) 18603550facSJ. Bruce Fields return error; 1871da177e4SLinus Torvalds /* assume old filehandle format */ 1881da177e4SLinus Torvalds xdev = old_decode_dev(fh->ofh_xdev); 1891da177e4SLinus Torvalds xino = u32_to_ino_t(fh->ofh_xino); 190af6a4e28SNeilBrown mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL); 1910989a788SJ. Bruce Fields exp = rqst_exp_find(rqstp, FSID_DEV, tfh); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1942d3bb252SJ. Bruce Fields error = nfserr_stale; 1952d3bb252SJ. Bruce Fields if (PTR_ERR(exp) == -ENOENT) 19603550facSJ. Bruce Fields return error; 1972d3bb252SJ. Bruce Fields 19803550facSJ. Bruce Fields if (IS_ERR(exp)) 19903550facSJ. Bruce Fields return nfserrno(PTR_ERR(exp)); 2001da177e4SLinus Torvalds 201496d6c32SNeil Brown if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) { 202496d6c32SNeil Brown /* Elevate privileges so that the lack of 'r' or 'x' 203496d6c32SNeil Brown * permission on some parent directory will 204496d6c32SNeil Brown * not stop exportfs_decode_fh from being able 205496d6c32SNeil Brown * to reconnect a directory into the dentry cache. 206496d6c32SNeil Brown * The same problem can affect "SUBTREECHECK" exports, 207496d6c32SNeil Brown * but as nfsd_acceptable depends on correct 208496d6c32SNeil Brown * access control settings being in effect, we cannot 209496d6c32SNeil Brown * fix that case easily. 210496d6c32SNeil Brown */ 211d84f4f99SDavid Howells struct cred *new = prepare_creds(); 212*027bc41aSKinglong Mee if (!new) { 213*027bc41aSKinglong Mee error = nfserrno(-ENOMEM); 214*027bc41aSKinglong Mee goto out; 215*027bc41aSKinglong Mee } 216d84f4f99SDavid Howells new->cap_effective = 217d84f4f99SDavid Howells cap_raise_nfsd_set(new->cap_effective, 218d84f4f99SDavid Howells new->cap_permitted); 219d84f4f99SDavid Howells put_cred(override_creds(new)); 220d84f4f99SDavid Howells put_cred(new); 221496d6c32SNeil Brown } else { 2226fa02839SJ. Bruce Fields error = nfsd_setuser_and_check_port(rqstp, exp); 223d1bbf14fSNeilBrown if (error) 224d1bbf14fSNeilBrown goto out; 225496d6c32SNeil Brown } 226d1bbf14fSNeilBrown 2271da177e4SLinus Torvalds /* 2281da177e4SLinus Torvalds * Look up the dentry using the NFS file handle. 2291da177e4SLinus Torvalds */ 2301da177e4SLinus Torvalds error = nfserr_stale; 2311da177e4SLinus Torvalds if (rqstp->rq_vers > 2) 2321da177e4SLinus Torvalds error = nfserr_badhandle; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds if (fh->fh_version != 1) { 2356e91ea2bSChristoph Hellwig sfid.i32.ino = fh->ofh_ino; 2366e91ea2bSChristoph Hellwig sfid.i32.gen = fh->ofh_generation; 2376e91ea2bSChristoph Hellwig sfid.i32.parent_ino = fh->ofh_dirino; 2386e91ea2bSChristoph Hellwig fid = &sfid; 2391da177e4SLinus Torvalds data_left = 3; 2401da177e4SLinus Torvalds if (fh->ofh_dirino == 0) 2416e91ea2bSChristoph Hellwig fileid_type = FILEID_INO32_GEN; 2421da177e4SLinus Torvalds else 2436e91ea2bSChristoph Hellwig fileid_type = FILEID_INO32_GEN_PARENT; 2441da177e4SLinus Torvalds } else 2451da177e4SLinus Torvalds fileid_type = fh->fh_fileid_type; 2461da177e4SLinus Torvalds 2476e91ea2bSChristoph Hellwig if (fileid_type == FILEID_ROOT) 24854775491SJan Blunck dentry = dget(exp->ex_path.dentry); 2491da177e4SLinus Torvalds else { 25054775491SJan Blunck dentry = exportfs_decode_fh(exp->ex_path.mnt, fid, 251d37065cdSChristoph Hellwig data_left, fileid_type, 2521da177e4SLinus Torvalds nfsd_acceptable, exp); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds if (dentry == NULL) 2551da177e4SLinus Torvalds goto out; 2561da177e4SLinus Torvalds if (IS_ERR(dentry)) { 2571da177e4SLinus Torvalds if (PTR_ERR(dentry) != -EINVAL) 2581da177e4SLinus Torvalds error = nfserrno(PTR_ERR(dentry)); 2591da177e4SLinus Torvalds goto out; 2601da177e4SLinus Torvalds } 26134e9a63bSNeilBrown 2621da177e4SLinus Torvalds if (S_ISDIR(dentry->d_inode->i_mode) && 2631da177e4SLinus Torvalds (dentry->d_flags & DCACHE_DISCONNECTED)) { 26497e47fa1SAl Viro printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n", 26597e47fa1SAl Viro dentry); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds fhp->fh_dentry = dentry; 2691da177e4SLinus Torvalds fhp->fh_export = exp; 27003550facSJ. Bruce Fields return 0; 27103550facSJ. Bruce Fields out: 27203550facSJ. Bruce Fields exp_put(exp); 27303550facSJ. Bruce Fields return error; 27403550facSJ. Bruce Fields } 27503550facSJ. Bruce Fields 276b3d47676SJ. Bruce Fields /** 277b3d47676SJ. Bruce Fields * fh_verify - filehandle lookup and access checking 278b3d47676SJ. Bruce Fields * @rqstp: pointer to current rpc request 279b3d47676SJ. Bruce Fields * @fhp: filehandle to be verified 280b3d47676SJ. Bruce Fields * @type: expected type of object pointed to by filehandle 281b3d47676SJ. Bruce Fields * @access: type of access needed to object 28203550facSJ. Bruce Fields * 283b3d47676SJ. Bruce Fields * Look up a dentry from the on-the-wire filehandle, check the client's 284b3d47676SJ. Bruce Fields * access to the export, and set the current task's credentials. 28503550facSJ. Bruce Fields * 286b3d47676SJ. Bruce Fields * Regardless of success or failure of fh_verify(), fh_put() should be 287b3d47676SJ. Bruce Fields * called on @fhp when the caller is finished with the filehandle. 288b3d47676SJ. Bruce Fields * 289b3d47676SJ. Bruce Fields * fh_verify() may be called multiple times on a given filehandle, for 290b3d47676SJ. Bruce Fields * example, when processing an NFSv4 compound. The first call will look 291b3d47676SJ. Bruce Fields * up a dentry using the on-the-wire filehandle. Subsequent calls will 292b3d47676SJ. Bruce Fields * skip the lookup and just perform the other checks and possibly change 293b3d47676SJ. Bruce Fields * the current task's credentials. 294b3d47676SJ. Bruce Fields * 295b3d47676SJ. Bruce Fields * @type specifies the type of object expected using one of the S_IF* 296b3d47676SJ. Bruce Fields * constants defined in include/linux/stat.h. The caller may use zero 297b3d47676SJ. Bruce Fields * to indicate that it doesn't care, or a negative integer to indicate 298b3d47676SJ. Bruce Fields * that it expects something not of the given type. 299b3d47676SJ. Bruce Fields * 300b3d47676SJ. Bruce Fields * @access is formed from the NFSD_MAY_* constants defined in 301b3d47676SJ. Bruce Fields * include/linux/nfsd/nfsd.h. 30203550facSJ. Bruce Fields */ 30303550facSJ. Bruce Fields __be32 304175a4eb7SAl Viro fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access) 30503550facSJ. Bruce Fields { 30603550facSJ. Bruce Fields struct svc_export *exp; 30703550facSJ. Bruce Fields struct dentry *dentry; 30803550facSJ. Bruce Fields __be32 error; 30903550facSJ. Bruce Fields 31003550facSJ. Bruce Fields dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp)); 31103550facSJ. Bruce Fields 31203550facSJ. Bruce Fields if (!fhp->fh_dentry) { 31303550facSJ. Bruce Fields error = nfsd_set_fh_dentry(rqstp, fhp); 31403550facSJ. Bruce Fields if (error) 31503550facSJ. Bruce Fields goto out; 316864f0f61SJ. Bruce Fields } 3171da177e4SLinus Torvalds dentry = fhp->fh_dentry; 3181da177e4SLinus Torvalds exp = fhp->fh_export; 3196fa02839SJ. Bruce Fields /* 320864f0f61SJ. Bruce Fields * We still have to do all these permission checks, even when 321864f0f61SJ. Bruce Fields * fh_dentry is already set: 322864f0f61SJ. Bruce Fields * - fh_verify may be called multiple times with different 323864f0f61SJ. Bruce Fields * "access" arguments (e.g. nfsd_proc_create calls 324864f0f61SJ. Bruce Fields * fh_verify(...,NFSD_MAY_EXEC) first, then later (in 325864f0f61SJ. Bruce Fields * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE). 326864f0f61SJ. Bruce Fields * - in the NFSv4 case, the filehandle may have been filled 327864f0f61SJ. Bruce Fields * in by fh_compose, and given a dentry, but further 328864f0f61SJ. Bruce Fields * compound operations performed with that filehandle 329864f0f61SJ. Bruce Fields * still need permissions checks. In the worst case, a 330864f0f61SJ. Bruce Fields * mountpoint crossing may have changed the export 331864f0f61SJ. Bruce Fields * options, and we may now need to use a different uid 332864f0f61SJ. Bruce Fields * (for example, if different id-squashing options are in 333864f0f61SJ. Bruce Fields * effect on the new filesystem). 3346fa02839SJ. Bruce Fields */ 33503a816b4SSteve Dickson error = check_pseudo_root(rqstp, dentry, exp); 33603a816b4SSteve Dickson if (error) 33703a816b4SSteve Dickson goto out; 33803a816b4SSteve Dickson 3396fa02839SJ. Bruce Fields error = nfsd_setuser_and_check_port(rqstp, exp); 3407fc90ec9SJ. Bruce Fields if (error) 3417fc90ec9SJ. Bruce Fields goto out; 3427fc90ec9SJ. Bruce Fields 3431da177e4SLinus Torvalds error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type); 3441da177e4SLinus Torvalds if (error) 3451da177e4SLinus Torvalds goto out; 3461da177e4SLinus Torvalds 3479091224fSJ. Bruce Fields /* 3489091224fSJ. Bruce Fields * pseudoflavor restrictions are not enforced on NLM, 3499091224fSJ. Bruce Fields * which clients virtually always use auth_sys for, 3509091224fSJ. Bruce Fields * even while using RPCSEC_GSS for NFS. 3519091224fSJ. Bruce Fields */ 352204f4ce7SJ. Bruce Fields if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS) 35304716e66SJ. Bruce Fields goto skip_pseudoflavor_check; 35404716e66SJ. Bruce Fields /* 35504716e66SJ. Bruce Fields * Clients may expect to be able to use auth_sys during mount, 35604716e66SJ. Bruce Fields * even if they use gss for everything else; see section 2.3.2 35704716e66SJ. Bruce Fields * of rfc 2623. 35804716e66SJ. Bruce Fields */ 35904716e66SJ. Bruce Fields if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT 36004716e66SJ. Bruce Fields && exp->ex_path.dentry == dentry) 36104716e66SJ. Bruce Fields goto skip_pseudoflavor_check; 36204716e66SJ. Bruce Fields 36332c1eb0cSAndy Adamson error = check_nfsd_access(exp, rqstp); 36432c1eb0cSAndy Adamson if (error) 36532c1eb0cSAndy Adamson goto out; 36632c1eb0cSAndy Adamson 36704716e66SJ. Bruce Fields skip_pseudoflavor_check: 3681da177e4SLinus Torvalds /* Finally, check access permissions. */ 3690ec757dfSJ. Bruce Fields error = nfsd_permission(rqstp, exp, dentry, access); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds if (error) { 37297e47fa1SAl Viro dprintk("fh_verify: %pd2 permission failure, " 37334e9a63bSNeilBrown "acc=%x, error=%d\n", 37497e47fa1SAl Viro dentry, 375fc2dd2e5SAl Viro access, ntohl(error)); 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds out: 3781da177e4SLinus Torvalds if (error == nfserr_stale) 3791da177e4SLinus Torvalds nfsdstats.fh_stale++; 3801da177e4SLinus Torvalds return error; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* 3851da177e4SLinus Torvalds * Compose a file handle for an NFS reply. 3861da177e4SLinus Torvalds * 3871da177e4SLinus Torvalds * Note that when first composed, the dentry may not yet have 3881da177e4SLinus Torvalds * an inode. In this case a call to fh_update should be made 3891da177e4SLinus Torvalds * before the fh goes out on the wire ... 3901da177e4SLinus Torvalds */ 3916e91ea2bSChristoph Hellwig static void _fh_update(struct svc_fh *fhp, struct svc_export *exp, 3926e91ea2bSChristoph Hellwig struct dentry *dentry) 3931da177e4SLinus Torvalds { 39454775491SJan Blunck if (dentry != exp->ex_path.dentry) { 3956e91ea2bSChristoph Hellwig struct fid *fid = (struct fid *) 3965409e46fSChristoph Hellwig (fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1); 3976e91ea2bSChristoph Hellwig int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4; 3986e91ea2bSChristoph Hellwig int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK); 3991da177e4SLinus Torvalds 4006e91ea2bSChristoph Hellwig fhp->fh_handle.fh_fileid_type = 4016e91ea2bSChristoph Hellwig exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck); 4026e91ea2bSChristoph Hellwig fhp->fh_handle.fh_size += maxsize * 4; 4036e91ea2bSChristoph Hellwig } else { 4046e91ea2bSChristoph Hellwig fhp->fh_handle.fh_fileid_type = FILEID_ROOT; 4056e91ea2bSChristoph Hellwig } 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* 4091da177e4SLinus Torvalds * for composing old style file handles 4101da177e4SLinus Torvalds */ 4111da177e4SLinus Torvalds static inline void _fh_update_old(struct dentry *dentry, 4121da177e4SLinus Torvalds struct svc_export *exp, 4131da177e4SLinus Torvalds struct knfsd_fh *fh) 4141da177e4SLinus Torvalds { 4151da177e4SLinus Torvalds fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino); 4161da177e4SLinus Torvalds fh->ofh_generation = dentry->d_inode->i_generation; 4171da177e4SLinus Torvalds if (S_ISDIR(dentry->d_inode->i_mode) || 4181da177e4SLinus Torvalds (exp->ex_flags & NFSEXP_NOSUBTREECHECK)) 4191da177e4SLinus Torvalds fh->ofh_dirino = 0; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4228e498751SJ. Bruce Fields static bool is_root_export(struct svc_export *exp) 4238e498751SJ. Bruce Fields { 4248e498751SJ. Bruce Fields return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root; 4258e498751SJ. Bruce Fields } 4268e498751SJ. Bruce Fields 4278e498751SJ. Bruce Fields static struct super_block *exp_sb(struct svc_export *exp) 4288e498751SJ. Bruce Fields { 4298e498751SJ. Bruce Fields return exp->ex_path.dentry->d_inode->i_sb; 4308e498751SJ. Bruce Fields } 4318e498751SJ. Bruce Fields 4328e498751SJ. Bruce Fields static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp) 4338e498751SJ. Bruce Fields { 4348e498751SJ. Bruce Fields switch (fsid_type) { 4358e498751SJ. Bruce Fields case FSID_DEV: 4368e498751SJ. Bruce Fields if (!old_valid_dev(exp_sb(exp)->s_dev)) 4378e498751SJ. Bruce Fields return 0; 4388e498751SJ. Bruce Fields /* FALL THROUGH */ 4398e498751SJ. Bruce Fields case FSID_MAJOR_MINOR: 4408e498751SJ. Bruce Fields case FSID_ENCODE_DEV: 4418e498751SJ. Bruce Fields return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV; 4428e498751SJ. Bruce Fields case FSID_NUM: 4438e498751SJ. Bruce Fields return exp->ex_flags & NFSEXP_FSID; 4448e498751SJ. Bruce Fields case FSID_UUID8: 4458e498751SJ. Bruce Fields case FSID_UUID16: 4468e498751SJ. Bruce Fields if (!is_root_export(exp)) 4478e498751SJ. Bruce Fields return 0; 4488e498751SJ. Bruce Fields /* fall through */ 4498e498751SJ. Bruce Fields case FSID_UUID4_INUM: 4508e498751SJ. Bruce Fields case FSID_UUID16_INUM: 4518e498751SJ. Bruce Fields return exp->ex_uuid != NULL; 4528e498751SJ. Bruce Fields } 4538e498751SJ. Bruce Fields return 1; 4548e498751SJ. Bruce Fields } 4558e498751SJ. Bruce Fields 456bc6c53d5SJ. Bruce Fields 457bc6c53d5SJ. Bruce Fields static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh) 4581da177e4SLinus Torvalds { 459b41eeef1SNeilBrown u8 version; 460bc6c53d5SJ. Bruce Fields u8 fsid_type; 461b41eeef1SNeilBrown retry: 462b41eeef1SNeilBrown version = 1; 4637e405364SNeilBrown if (ref_fh && ref_fh->fh_export == exp) { 464982aedfdSNeilBrown version = ref_fh->fh_handle.fh_version; 465982aedfdSNeilBrown fsid_type = ref_fh->fh_handle.fh_fsid_type; 466b41eeef1SNeilBrown 467b41eeef1SNeilBrown ref_fh = NULL; 468b41eeef1SNeilBrown 469b41eeef1SNeilBrown switch (version) { 470b41eeef1SNeilBrown case 0xca: 471b41eeef1SNeilBrown fsid_type = FSID_DEV; 472b41eeef1SNeilBrown break; 473b41eeef1SNeilBrown case 1: 474b41eeef1SNeilBrown break; 475b41eeef1SNeilBrown default: 476b41eeef1SNeilBrown goto retry; 477b41eeef1SNeilBrown } 478b41eeef1SNeilBrown 4798e498751SJ. Bruce Fields /* 4808e498751SJ. Bruce Fields * As the fsid -> filesystem mapping was guided by 4818e498751SJ. Bruce Fields * user-space, there is no guarantee that the filesystem 4828e498751SJ. Bruce Fields * actually supports that fsid type. If it doesn't we 4838e498751SJ. Bruce Fields * loop around again without ref_fh set. 484982aedfdSNeilBrown */ 4858e498751SJ. Bruce Fields if (!fsid_type_ok_for_exp(fsid_type, exp)) 486b41eeef1SNeilBrown goto retry; 48730fa8c01SSteve Dickson } else if (exp->ex_flags & NFSEXP_FSID) { 48830fa8c01SSteve Dickson fsid_type = FSID_NUM; 489af6a4e28SNeilBrown } else if (exp->ex_uuid) { 490af6a4e28SNeilBrown if (fhp->fh_maxsize >= 64) { 4918e498751SJ. Bruce Fields if (is_root_export(exp)) 492af6a4e28SNeilBrown fsid_type = FSID_UUID16; 493af6a4e28SNeilBrown else 494af6a4e28SNeilBrown fsid_type = FSID_UUID16_INUM; 495af6a4e28SNeilBrown } else { 4968e498751SJ. Bruce Fields if (is_root_export(exp)) 497af6a4e28SNeilBrown fsid_type = FSID_UUID8; 498af6a4e28SNeilBrown else 499af6a4e28SNeilBrown fsid_type = FSID_UUID4_INUM; 500af6a4e28SNeilBrown } 501bc6c53d5SJ. Bruce Fields } else if (!old_valid_dev(exp_sb(exp)->s_dev)) 5021da177e4SLinus Torvalds /* for newer device numbers, we must use a newer fsid format */ 503af6a4e28SNeilBrown fsid_type = FSID_ENCODE_DEV; 504982aedfdSNeilBrown else 505af6a4e28SNeilBrown fsid_type = FSID_DEV; 506bc6c53d5SJ. Bruce Fields fhp->fh_handle.fh_version = version; 507bc6c53d5SJ. Bruce Fields if (version) 508bc6c53d5SJ. Bruce Fields fhp->fh_handle.fh_fsid_type = fsid_type; 509bc6c53d5SJ. Bruce Fields } 510bc6c53d5SJ. Bruce Fields 511bc6c53d5SJ. Bruce Fields __be32 512bc6c53d5SJ. Bruce Fields fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, 513bc6c53d5SJ. Bruce Fields struct svc_fh *ref_fh) 514bc6c53d5SJ. Bruce Fields { 515bc6c53d5SJ. Bruce Fields /* ref_fh is a reference file handle. 516bc6c53d5SJ. Bruce Fields * if it is non-null and for the same filesystem, then we should compose 517bc6c53d5SJ. Bruce Fields * a filehandle which is of the same version, where possible. 518bc6c53d5SJ. Bruce Fields * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca 519bc6c53d5SJ. Bruce Fields * Then create a 32byte filehandle using nfs_fhbase_old 520bc6c53d5SJ. Bruce Fields * 521bc6c53d5SJ. Bruce Fields */ 522bc6c53d5SJ. Bruce Fields 523bc6c53d5SJ. Bruce Fields struct inode * inode = dentry->d_inode; 524bc6c53d5SJ. Bruce Fields dev_t ex_dev = exp_sb(exp)->s_dev; 525bc6c53d5SJ. Bruce Fields 52697e47fa1SAl Viro dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n", 527bc6c53d5SJ. Bruce Fields MAJOR(ex_dev), MINOR(ex_dev), 528bc6c53d5SJ. Bruce Fields (long) exp->ex_path.dentry->d_inode->i_ino, 52997e47fa1SAl Viro dentry, 530bc6c53d5SJ. Bruce Fields (inode ? inode->i_ino : 0)); 531bc6c53d5SJ. Bruce Fields 532bc6c53d5SJ. Bruce Fields /* Choose filehandle version and fsid type based on 533bc6c53d5SJ. Bruce Fields * the reference filehandle (if it is in the same export) 534bc6c53d5SJ. Bruce Fields * or the export options. 535bc6c53d5SJ. Bruce Fields */ 536bc6c53d5SJ. Bruce Fields set_version_and_fsid_type(fhp, exp, ref_fh); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds if (ref_fh == fhp) 5391da177e4SLinus Torvalds fh_put(ref_fh); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds if (fhp->fh_locked || fhp->fh_dentry) { 54297e47fa1SAl Viro printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n", 54397e47fa1SAl Viro dentry); 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds if (fhp->fh_maxsize < NFS_FHSIZE) 54697e47fa1SAl Viro printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n", 547982aedfdSNeilBrown fhp->fh_maxsize, 54897e47fa1SAl Viro dentry); 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds fhp->fh_dentry = dget(dentry); /* our internal copy */ 551bf18f163SKinglong Mee fhp->fh_export = exp_get(exp); 5521da177e4SLinus Torvalds 553bc6c53d5SJ. Bruce Fields if (fhp->fh_handle.fh_version == 0xca) { 5541da177e4SLinus Torvalds /* old style filehandle please */ 5551da177e4SLinus Torvalds memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE); 5561da177e4SLinus Torvalds fhp->fh_handle.fh_size = NFS_FHSIZE; 5571da177e4SLinus Torvalds fhp->fh_handle.ofh_dcookie = 0xfeebbaca; 5581da177e4SLinus Torvalds fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev); 5591da177e4SLinus Torvalds fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev; 560982aedfdSNeilBrown fhp->fh_handle.ofh_xino = 56154775491SJan Blunck ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino); 5621da177e4SLinus Torvalds fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry)); 5631da177e4SLinus Torvalds if (inode) 5641da177e4SLinus Torvalds _fh_update_old(dentry, exp, &fhp->fh_handle); 5651da177e4SLinus Torvalds } else { 5665409e46fSChristoph Hellwig fhp->fh_handle.fh_size = 5675409e46fSChristoph Hellwig key_len(fhp->fh_handle.fh_fsid_type) + 4; 5681da177e4SLinus Torvalds fhp->fh_handle.fh_auth_type = 0; 5695409e46fSChristoph Hellwig 5705409e46fSChristoph Hellwig mk_fsid(fhp->fh_handle.fh_fsid_type, 5715409e46fSChristoph Hellwig fhp->fh_handle.fh_fsid, 5725409e46fSChristoph Hellwig ex_dev, 57354775491SJan Blunck exp->ex_path.dentry->d_inode->i_ino, 574af6a4e28SNeilBrown exp->ex_fsid, exp->ex_uuid); 575af6a4e28SNeilBrown 5766e91ea2bSChristoph Hellwig if (inode) 5776e91ea2bSChristoph Hellwig _fh_update(fhp, exp, dentry); 578216b6cbdSNamjae Jeon if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) { 5791be10a88SJ. Bruce Fields fh_put(fhp); 5801da177e4SLinus Torvalds return nfserr_opnotsupp; 5811da177e4SLinus Torvalds } 5821be10a88SJ. Bruce Fields } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds return 0; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* 5881da177e4SLinus Torvalds * Update file handle information after changing a dentry. 5891da177e4SLinus Torvalds * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create 5901da177e4SLinus Torvalds */ 59183b11340SAl Viro __be32 5921da177e4SLinus Torvalds fh_update(struct svc_fh *fhp) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds struct dentry *dentry; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds if (!fhp->fh_dentry) 5971da177e4SLinus Torvalds goto out_bad; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds dentry = fhp->fh_dentry; 6001da177e4SLinus Torvalds if (!dentry->d_inode) 6011da177e4SLinus Torvalds goto out_negative; 6021da177e4SLinus Torvalds if (fhp->fh_handle.fh_version != 1) { 6031da177e4SLinus Torvalds _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle); 6041da177e4SLinus Torvalds } else { 6056e91ea2bSChristoph Hellwig if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT) 60649e73720SJ. Bruce Fields return 0; 6076e91ea2bSChristoph Hellwig 6086e91ea2bSChristoph Hellwig _fh_update(fhp, fhp->fh_export, dentry); 609216b6cbdSNamjae Jeon if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) 6101da177e4SLinus Torvalds return nfserr_opnotsupp; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds return 0; 6131da177e4SLinus Torvalds out_bad: 6141da177e4SLinus Torvalds printk(KERN_ERR "fh_update: fh not verified!\n"); 61549e73720SJ. Bruce Fields return nfserr_serverfault; 6161da177e4SLinus Torvalds out_negative: 61797e47fa1SAl Viro printk(KERN_ERR "fh_update: %pd2 still negative!\n", 61897e47fa1SAl Viro dentry); 61949e73720SJ. Bruce Fields return nfserr_serverfault; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /* 6231da177e4SLinus Torvalds * Release a file handle. 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds void 6261da177e4SLinus Torvalds fh_put(struct svc_fh *fhp) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds struct dentry * dentry = fhp->fh_dentry; 6291da177e4SLinus Torvalds struct svc_export * exp = fhp->fh_export; 6301da177e4SLinus Torvalds if (dentry) { 6311da177e4SLinus Torvalds fh_unlock(fhp); 6321da177e4SLinus Torvalds fhp->fh_dentry = NULL; 6331da177e4SLinus Torvalds dput(dentry); 6341da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3 6351da177e4SLinus Torvalds fhp->fh_pre_saved = 0; 6361da177e4SLinus Torvalds fhp->fh_post_saved = 0; 6371da177e4SLinus Torvalds #endif 6381da177e4SLinus Torvalds } 6394a55c101SJan Kara fh_drop_write(fhp); 6401da177e4SLinus Torvalds if (exp) { 641a09581f2SStanislav Kinsbursky exp_put(exp); 6421da177e4SLinus Torvalds fhp->fh_export = NULL; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds return; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* 6481da177e4SLinus Torvalds * Shorthand for dprintk()'s 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds char * SVCFH_fmt(struct svc_fh *fhp) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds struct knfsd_fh *fh = &fhp->fh_handle; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds static char buf[80]; 6551da177e4SLinus Torvalds sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x", 6561da177e4SLinus Torvalds fh->fh_size, 6571da177e4SLinus Torvalds fh->fh_base.fh_pad[0], 6581da177e4SLinus Torvalds fh->fh_base.fh_pad[1], 6591da177e4SLinus Torvalds fh->fh_base.fh_pad[2], 6601da177e4SLinus Torvalds fh->fh_base.fh_pad[3], 6611da177e4SLinus Torvalds fh->fh_base.fh_pad[4], 6621da177e4SLinus Torvalds fh->fh_base.fh_pad[5]); 6631da177e4SLinus Torvalds return buf; 6641da177e4SLinus Torvalds } 665af6a4e28SNeilBrown 666af6a4e28SNeilBrown enum fsid_source fsid_source(struct svc_fh *fhp) 667af6a4e28SNeilBrown { 668af6a4e28SNeilBrown if (fhp->fh_handle.fh_version != 1) 669af6a4e28SNeilBrown return FSIDSOURCE_DEV; 670af6a4e28SNeilBrown switch(fhp->fh_handle.fh_fsid_type) { 671af6a4e28SNeilBrown case FSID_DEV: 672af6a4e28SNeilBrown case FSID_ENCODE_DEV: 673af6a4e28SNeilBrown case FSID_MAJOR_MINOR: 6748e498751SJ. Bruce Fields if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV) 675af6a4e28SNeilBrown return FSIDSOURCE_DEV; 676b8da0d1cSNeil Brown break; 677af6a4e28SNeilBrown case FSID_NUM: 678af6a4e28SNeilBrown if (fhp->fh_export->ex_flags & NFSEXP_FSID) 679af6a4e28SNeilBrown return FSIDSOURCE_FSID; 680b8da0d1cSNeil Brown break; 681b8da0d1cSNeil Brown default: 682b8da0d1cSNeil Brown break; 683af6a4e28SNeilBrown } 684b8da0d1cSNeil Brown /* either a UUID type filehandle, or the filehandle doesn't 685b8da0d1cSNeil Brown * match the export. 686b8da0d1cSNeil Brown */ 687b8da0d1cSNeil Brown if (fhp->fh_export->ex_flags & NFSEXP_FSID) 688b8da0d1cSNeil Brown return FSIDSOURCE_FSID; 689b8da0d1cSNeil Brown if (fhp->fh_export->ex_uuid) 690b8da0d1cSNeil Brown return FSIDSOURCE_UUID; 691b8da0d1cSNeil Brown return FSIDSOURCE_DEV; 692af6a4e28SNeilBrown } 693