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; 165*94ec938bSJeff Layton /* 166*94ec938bSJeff Layton * struct knfsd_fh uses host-endian fields, which are 167*94ec938bSJeff Layton * sometimes used to hold net-endian values. This 168*94ec938bSJeff Layton * confuses sparse, so we must use __force here to 169*94ec938bSJeff Layton * keep it from complaining. 170*94ec938bSJeff Layton */ 171*94ec938bSJeff Layton fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]), 172*94ec938bSJeff 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(); 212d84f4f99SDavid Howells if (!new) 213d84f4f99SDavid Howells return nfserrno(-ENOMEM); 214d84f4f99SDavid Howells new->cap_effective = 215d84f4f99SDavid Howells cap_raise_nfsd_set(new->cap_effective, 216d84f4f99SDavid Howells new->cap_permitted); 217d84f4f99SDavid Howells put_cred(override_creds(new)); 218d84f4f99SDavid Howells put_cred(new); 219496d6c32SNeil Brown } else { 2206fa02839SJ. Bruce Fields error = nfsd_setuser_and_check_port(rqstp, exp); 221d1bbf14fSNeilBrown if (error) 222d1bbf14fSNeilBrown goto out; 223496d6c32SNeil Brown } 224d1bbf14fSNeilBrown 2251da177e4SLinus Torvalds /* 2261da177e4SLinus Torvalds * Look up the dentry using the NFS file handle. 2271da177e4SLinus Torvalds */ 2281da177e4SLinus Torvalds error = nfserr_stale; 2291da177e4SLinus Torvalds if (rqstp->rq_vers > 2) 2301da177e4SLinus Torvalds error = nfserr_badhandle; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (fh->fh_version != 1) { 2336e91ea2bSChristoph Hellwig sfid.i32.ino = fh->ofh_ino; 2346e91ea2bSChristoph Hellwig sfid.i32.gen = fh->ofh_generation; 2356e91ea2bSChristoph Hellwig sfid.i32.parent_ino = fh->ofh_dirino; 2366e91ea2bSChristoph Hellwig fid = &sfid; 2371da177e4SLinus Torvalds data_left = 3; 2381da177e4SLinus Torvalds if (fh->ofh_dirino == 0) 2396e91ea2bSChristoph Hellwig fileid_type = FILEID_INO32_GEN; 2401da177e4SLinus Torvalds else 2416e91ea2bSChristoph Hellwig fileid_type = FILEID_INO32_GEN_PARENT; 2421da177e4SLinus Torvalds } else 2431da177e4SLinus Torvalds fileid_type = fh->fh_fileid_type; 2441da177e4SLinus Torvalds 2456e91ea2bSChristoph Hellwig if (fileid_type == FILEID_ROOT) 24654775491SJan Blunck dentry = dget(exp->ex_path.dentry); 2471da177e4SLinus Torvalds else { 24854775491SJan Blunck dentry = exportfs_decode_fh(exp->ex_path.mnt, fid, 249d37065cdSChristoph Hellwig data_left, fileid_type, 2501da177e4SLinus Torvalds nfsd_acceptable, exp); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds if (dentry == NULL) 2531da177e4SLinus Torvalds goto out; 2541da177e4SLinus Torvalds if (IS_ERR(dentry)) { 2551da177e4SLinus Torvalds if (PTR_ERR(dentry) != -EINVAL) 2561da177e4SLinus Torvalds error = nfserrno(PTR_ERR(dentry)); 2571da177e4SLinus Torvalds goto out; 2581da177e4SLinus Torvalds } 25934e9a63bSNeilBrown 2601da177e4SLinus Torvalds if (S_ISDIR(dentry->d_inode->i_mode) && 2611da177e4SLinus Torvalds (dentry->d_flags & DCACHE_DISCONNECTED)) { 26297e47fa1SAl Viro printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n", 26397e47fa1SAl Viro dentry); 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds fhp->fh_dentry = dentry; 2671da177e4SLinus Torvalds fhp->fh_export = exp; 26803550facSJ. Bruce Fields return 0; 26903550facSJ. Bruce Fields out: 27003550facSJ. Bruce Fields exp_put(exp); 27103550facSJ. Bruce Fields return error; 27203550facSJ. Bruce Fields } 27303550facSJ. Bruce Fields 274b3d47676SJ. Bruce Fields /** 275b3d47676SJ. Bruce Fields * fh_verify - filehandle lookup and access checking 276b3d47676SJ. Bruce Fields * @rqstp: pointer to current rpc request 277b3d47676SJ. Bruce Fields * @fhp: filehandle to be verified 278b3d47676SJ. Bruce Fields * @type: expected type of object pointed to by filehandle 279b3d47676SJ. Bruce Fields * @access: type of access needed to object 28003550facSJ. Bruce Fields * 281b3d47676SJ. Bruce Fields * Look up a dentry from the on-the-wire filehandle, check the client's 282b3d47676SJ. Bruce Fields * access to the export, and set the current task's credentials. 28303550facSJ. Bruce Fields * 284b3d47676SJ. Bruce Fields * Regardless of success or failure of fh_verify(), fh_put() should be 285b3d47676SJ. Bruce Fields * called on @fhp when the caller is finished with the filehandle. 286b3d47676SJ. Bruce Fields * 287b3d47676SJ. Bruce Fields * fh_verify() may be called multiple times on a given filehandle, for 288b3d47676SJ. Bruce Fields * example, when processing an NFSv4 compound. The first call will look 289b3d47676SJ. Bruce Fields * up a dentry using the on-the-wire filehandle. Subsequent calls will 290b3d47676SJ. Bruce Fields * skip the lookup and just perform the other checks and possibly change 291b3d47676SJ. Bruce Fields * the current task's credentials. 292b3d47676SJ. Bruce Fields * 293b3d47676SJ. Bruce Fields * @type specifies the type of object expected using one of the S_IF* 294b3d47676SJ. Bruce Fields * constants defined in include/linux/stat.h. The caller may use zero 295b3d47676SJ. Bruce Fields * to indicate that it doesn't care, or a negative integer to indicate 296b3d47676SJ. Bruce Fields * that it expects something not of the given type. 297b3d47676SJ. Bruce Fields * 298b3d47676SJ. Bruce Fields * @access is formed from the NFSD_MAY_* constants defined in 299b3d47676SJ. Bruce Fields * include/linux/nfsd/nfsd.h. 30003550facSJ. Bruce Fields */ 30103550facSJ. Bruce Fields __be32 302175a4eb7SAl Viro fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access) 30303550facSJ. Bruce Fields { 30403550facSJ. Bruce Fields struct svc_export *exp; 30503550facSJ. Bruce Fields struct dentry *dentry; 30603550facSJ. Bruce Fields __be32 error; 30703550facSJ. Bruce Fields 30803550facSJ. Bruce Fields dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp)); 30903550facSJ. Bruce Fields 31003550facSJ. Bruce Fields if (!fhp->fh_dentry) { 31103550facSJ. Bruce Fields error = nfsd_set_fh_dentry(rqstp, fhp); 31203550facSJ. Bruce Fields if (error) 31303550facSJ. Bruce Fields goto out; 314864f0f61SJ. Bruce Fields } 3151da177e4SLinus Torvalds dentry = fhp->fh_dentry; 3161da177e4SLinus Torvalds exp = fhp->fh_export; 3176fa02839SJ. Bruce Fields /* 318864f0f61SJ. Bruce Fields * We still have to do all these permission checks, even when 319864f0f61SJ. Bruce Fields * fh_dentry is already set: 320864f0f61SJ. Bruce Fields * - fh_verify may be called multiple times with different 321864f0f61SJ. Bruce Fields * "access" arguments (e.g. nfsd_proc_create calls 322864f0f61SJ. Bruce Fields * fh_verify(...,NFSD_MAY_EXEC) first, then later (in 323864f0f61SJ. Bruce Fields * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE). 324864f0f61SJ. Bruce Fields * - in the NFSv4 case, the filehandle may have been filled 325864f0f61SJ. Bruce Fields * in by fh_compose, and given a dentry, but further 326864f0f61SJ. Bruce Fields * compound operations performed with that filehandle 327864f0f61SJ. Bruce Fields * still need permissions checks. In the worst case, a 328864f0f61SJ. Bruce Fields * mountpoint crossing may have changed the export 329864f0f61SJ. Bruce Fields * options, and we may now need to use a different uid 330864f0f61SJ. Bruce Fields * (for example, if different id-squashing options are in 331864f0f61SJ. Bruce Fields * effect on the new filesystem). 3326fa02839SJ. Bruce Fields */ 33303a816b4SSteve Dickson error = check_pseudo_root(rqstp, dentry, exp); 33403a816b4SSteve Dickson if (error) 33503a816b4SSteve Dickson goto out; 33603a816b4SSteve Dickson 3376fa02839SJ. Bruce Fields error = nfsd_setuser_and_check_port(rqstp, exp); 3387fc90ec9SJ. Bruce Fields if (error) 3397fc90ec9SJ. Bruce Fields goto out; 3407fc90ec9SJ. Bruce Fields 3411da177e4SLinus Torvalds error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type); 3421da177e4SLinus Torvalds if (error) 3431da177e4SLinus Torvalds goto out; 3441da177e4SLinus Torvalds 3459091224fSJ. Bruce Fields /* 3469091224fSJ. Bruce Fields * pseudoflavor restrictions are not enforced on NLM, 3479091224fSJ. Bruce Fields * which clients virtually always use auth_sys for, 3489091224fSJ. Bruce Fields * even while using RPCSEC_GSS for NFS. 3499091224fSJ. Bruce Fields */ 350204f4ce7SJ. Bruce Fields if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS) 35104716e66SJ. Bruce Fields goto skip_pseudoflavor_check; 35204716e66SJ. Bruce Fields /* 35304716e66SJ. Bruce Fields * Clients may expect to be able to use auth_sys during mount, 35404716e66SJ. Bruce Fields * even if they use gss for everything else; see section 2.3.2 35504716e66SJ. Bruce Fields * of rfc 2623. 35604716e66SJ. Bruce Fields */ 35704716e66SJ. Bruce Fields if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT 35804716e66SJ. Bruce Fields && exp->ex_path.dentry == dentry) 35904716e66SJ. Bruce Fields goto skip_pseudoflavor_check; 36004716e66SJ. Bruce Fields 36132c1eb0cSAndy Adamson error = check_nfsd_access(exp, rqstp); 36232c1eb0cSAndy Adamson if (error) 36332c1eb0cSAndy Adamson goto out; 36432c1eb0cSAndy Adamson 36504716e66SJ. Bruce Fields skip_pseudoflavor_check: 3661da177e4SLinus Torvalds /* Finally, check access permissions. */ 3670ec757dfSJ. Bruce Fields error = nfsd_permission(rqstp, exp, dentry, access); 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (error) { 37097e47fa1SAl Viro dprintk("fh_verify: %pd2 permission failure, " 37134e9a63bSNeilBrown "acc=%x, error=%d\n", 37297e47fa1SAl Viro dentry, 373fc2dd2e5SAl Viro access, ntohl(error)); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds out: 3761da177e4SLinus Torvalds if (error == nfserr_stale) 3771da177e4SLinus Torvalds nfsdstats.fh_stale++; 3781da177e4SLinus Torvalds return error; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds /* 3831da177e4SLinus Torvalds * Compose a file handle for an NFS reply. 3841da177e4SLinus Torvalds * 3851da177e4SLinus Torvalds * Note that when first composed, the dentry may not yet have 3861da177e4SLinus Torvalds * an inode. In this case a call to fh_update should be made 3871da177e4SLinus Torvalds * before the fh goes out on the wire ... 3881da177e4SLinus Torvalds */ 3896e91ea2bSChristoph Hellwig static void _fh_update(struct svc_fh *fhp, struct svc_export *exp, 3906e91ea2bSChristoph Hellwig struct dentry *dentry) 3911da177e4SLinus Torvalds { 39254775491SJan Blunck if (dentry != exp->ex_path.dentry) { 3936e91ea2bSChristoph Hellwig struct fid *fid = (struct fid *) 3945409e46fSChristoph Hellwig (fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1); 3956e91ea2bSChristoph Hellwig int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4; 3966e91ea2bSChristoph Hellwig int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK); 3971da177e4SLinus Torvalds 3986e91ea2bSChristoph Hellwig fhp->fh_handle.fh_fileid_type = 3996e91ea2bSChristoph Hellwig exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck); 4006e91ea2bSChristoph Hellwig fhp->fh_handle.fh_size += maxsize * 4; 4016e91ea2bSChristoph Hellwig } else { 4026e91ea2bSChristoph Hellwig fhp->fh_handle.fh_fileid_type = FILEID_ROOT; 4036e91ea2bSChristoph Hellwig } 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 4071da177e4SLinus Torvalds * for composing old style file handles 4081da177e4SLinus Torvalds */ 4091da177e4SLinus Torvalds static inline void _fh_update_old(struct dentry *dentry, 4101da177e4SLinus Torvalds struct svc_export *exp, 4111da177e4SLinus Torvalds struct knfsd_fh *fh) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino); 4141da177e4SLinus Torvalds fh->ofh_generation = dentry->d_inode->i_generation; 4151da177e4SLinus Torvalds if (S_ISDIR(dentry->d_inode->i_mode) || 4161da177e4SLinus Torvalds (exp->ex_flags & NFSEXP_NOSUBTREECHECK)) 4171da177e4SLinus Torvalds fh->ofh_dirino = 0; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4208e498751SJ. Bruce Fields static bool is_root_export(struct svc_export *exp) 4218e498751SJ. Bruce Fields { 4228e498751SJ. Bruce Fields return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root; 4238e498751SJ. Bruce Fields } 4248e498751SJ. Bruce Fields 4258e498751SJ. Bruce Fields static struct super_block *exp_sb(struct svc_export *exp) 4268e498751SJ. Bruce Fields { 4278e498751SJ. Bruce Fields return exp->ex_path.dentry->d_inode->i_sb; 4288e498751SJ. Bruce Fields } 4298e498751SJ. Bruce Fields 4308e498751SJ. Bruce Fields static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp) 4318e498751SJ. Bruce Fields { 4328e498751SJ. Bruce Fields switch (fsid_type) { 4338e498751SJ. Bruce Fields case FSID_DEV: 4348e498751SJ. Bruce Fields if (!old_valid_dev(exp_sb(exp)->s_dev)) 4358e498751SJ. Bruce Fields return 0; 4368e498751SJ. Bruce Fields /* FALL THROUGH */ 4378e498751SJ. Bruce Fields case FSID_MAJOR_MINOR: 4388e498751SJ. Bruce Fields case FSID_ENCODE_DEV: 4398e498751SJ. Bruce Fields return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV; 4408e498751SJ. Bruce Fields case FSID_NUM: 4418e498751SJ. Bruce Fields return exp->ex_flags & NFSEXP_FSID; 4428e498751SJ. Bruce Fields case FSID_UUID8: 4438e498751SJ. Bruce Fields case FSID_UUID16: 4448e498751SJ. Bruce Fields if (!is_root_export(exp)) 4458e498751SJ. Bruce Fields return 0; 4468e498751SJ. Bruce Fields /* fall through */ 4478e498751SJ. Bruce Fields case FSID_UUID4_INUM: 4488e498751SJ. Bruce Fields case FSID_UUID16_INUM: 4498e498751SJ. Bruce Fields return exp->ex_uuid != NULL; 4508e498751SJ. Bruce Fields } 4518e498751SJ. Bruce Fields return 1; 4528e498751SJ. Bruce Fields } 4538e498751SJ. Bruce Fields 454bc6c53d5SJ. Bruce Fields 455bc6c53d5SJ. Bruce Fields static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh) 4561da177e4SLinus Torvalds { 457b41eeef1SNeilBrown u8 version; 458bc6c53d5SJ. Bruce Fields u8 fsid_type; 459b41eeef1SNeilBrown retry: 460b41eeef1SNeilBrown version = 1; 4617e405364SNeilBrown if (ref_fh && ref_fh->fh_export == exp) { 462982aedfdSNeilBrown version = ref_fh->fh_handle.fh_version; 463982aedfdSNeilBrown fsid_type = ref_fh->fh_handle.fh_fsid_type; 464b41eeef1SNeilBrown 465b41eeef1SNeilBrown ref_fh = NULL; 466b41eeef1SNeilBrown 467b41eeef1SNeilBrown switch (version) { 468b41eeef1SNeilBrown case 0xca: 469b41eeef1SNeilBrown fsid_type = FSID_DEV; 470b41eeef1SNeilBrown break; 471b41eeef1SNeilBrown case 1: 472b41eeef1SNeilBrown break; 473b41eeef1SNeilBrown default: 474b41eeef1SNeilBrown goto retry; 475b41eeef1SNeilBrown } 476b41eeef1SNeilBrown 4778e498751SJ. Bruce Fields /* 4788e498751SJ. Bruce Fields * As the fsid -> filesystem mapping was guided by 4798e498751SJ. Bruce Fields * user-space, there is no guarantee that the filesystem 4808e498751SJ. Bruce Fields * actually supports that fsid type. If it doesn't we 4818e498751SJ. Bruce Fields * loop around again without ref_fh set. 482982aedfdSNeilBrown */ 4838e498751SJ. Bruce Fields if (!fsid_type_ok_for_exp(fsid_type, exp)) 484b41eeef1SNeilBrown goto retry; 48530fa8c01SSteve Dickson } else if (exp->ex_flags & NFSEXP_FSID) { 48630fa8c01SSteve Dickson fsid_type = FSID_NUM; 487af6a4e28SNeilBrown } else if (exp->ex_uuid) { 488af6a4e28SNeilBrown if (fhp->fh_maxsize >= 64) { 4898e498751SJ. Bruce Fields if (is_root_export(exp)) 490af6a4e28SNeilBrown fsid_type = FSID_UUID16; 491af6a4e28SNeilBrown else 492af6a4e28SNeilBrown fsid_type = FSID_UUID16_INUM; 493af6a4e28SNeilBrown } else { 4948e498751SJ. Bruce Fields if (is_root_export(exp)) 495af6a4e28SNeilBrown fsid_type = FSID_UUID8; 496af6a4e28SNeilBrown else 497af6a4e28SNeilBrown fsid_type = FSID_UUID4_INUM; 498af6a4e28SNeilBrown } 499bc6c53d5SJ. Bruce Fields } else if (!old_valid_dev(exp_sb(exp)->s_dev)) 5001da177e4SLinus Torvalds /* for newer device numbers, we must use a newer fsid format */ 501af6a4e28SNeilBrown fsid_type = FSID_ENCODE_DEV; 502982aedfdSNeilBrown else 503af6a4e28SNeilBrown fsid_type = FSID_DEV; 504bc6c53d5SJ. Bruce Fields fhp->fh_handle.fh_version = version; 505bc6c53d5SJ. Bruce Fields if (version) 506bc6c53d5SJ. Bruce Fields fhp->fh_handle.fh_fsid_type = fsid_type; 507bc6c53d5SJ. Bruce Fields } 508bc6c53d5SJ. Bruce Fields 509bc6c53d5SJ. Bruce Fields __be32 510bc6c53d5SJ. Bruce Fields fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, 511bc6c53d5SJ. Bruce Fields struct svc_fh *ref_fh) 512bc6c53d5SJ. Bruce Fields { 513bc6c53d5SJ. Bruce Fields /* ref_fh is a reference file handle. 514bc6c53d5SJ. Bruce Fields * if it is non-null and for the same filesystem, then we should compose 515bc6c53d5SJ. Bruce Fields * a filehandle which is of the same version, where possible. 516bc6c53d5SJ. Bruce Fields * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca 517bc6c53d5SJ. Bruce Fields * Then create a 32byte filehandle using nfs_fhbase_old 518bc6c53d5SJ. Bruce Fields * 519bc6c53d5SJ. Bruce Fields */ 520bc6c53d5SJ. Bruce Fields 521bc6c53d5SJ. Bruce Fields struct inode * inode = dentry->d_inode; 522bc6c53d5SJ. Bruce Fields dev_t ex_dev = exp_sb(exp)->s_dev; 523bc6c53d5SJ. Bruce Fields 52497e47fa1SAl Viro dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n", 525bc6c53d5SJ. Bruce Fields MAJOR(ex_dev), MINOR(ex_dev), 526bc6c53d5SJ. Bruce Fields (long) exp->ex_path.dentry->d_inode->i_ino, 52797e47fa1SAl Viro dentry, 528bc6c53d5SJ. Bruce Fields (inode ? inode->i_ino : 0)); 529bc6c53d5SJ. Bruce Fields 530bc6c53d5SJ. Bruce Fields /* Choose filehandle version and fsid type based on 531bc6c53d5SJ. Bruce Fields * the reference filehandle (if it is in the same export) 532bc6c53d5SJ. Bruce Fields * or the export options. 533bc6c53d5SJ. Bruce Fields */ 534bc6c53d5SJ. Bruce Fields set_version_and_fsid_type(fhp, exp, ref_fh); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (ref_fh == fhp) 5371da177e4SLinus Torvalds fh_put(ref_fh); 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds if (fhp->fh_locked || fhp->fh_dentry) { 54097e47fa1SAl Viro printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n", 54197e47fa1SAl Viro dentry); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds if (fhp->fh_maxsize < NFS_FHSIZE) 54497e47fa1SAl Viro printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n", 545982aedfdSNeilBrown fhp->fh_maxsize, 54697e47fa1SAl Viro dentry); 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds fhp->fh_dentry = dget(dentry); /* our internal copy */ 549bf18f163SKinglong Mee fhp->fh_export = exp_get(exp); 5501da177e4SLinus Torvalds 551bc6c53d5SJ. Bruce Fields if (fhp->fh_handle.fh_version == 0xca) { 5521da177e4SLinus Torvalds /* old style filehandle please */ 5531da177e4SLinus Torvalds memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE); 5541da177e4SLinus Torvalds fhp->fh_handle.fh_size = NFS_FHSIZE; 5551da177e4SLinus Torvalds fhp->fh_handle.ofh_dcookie = 0xfeebbaca; 5561da177e4SLinus Torvalds fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev); 5571da177e4SLinus Torvalds fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev; 558982aedfdSNeilBrown fhp->fh_handle.ofh_xino = 55954775491SJan Blunck ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino); 5601da177e4SLinus Torvalds fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry)); 5611da177e4SLinus Torvalds if (inode) 5621da177e4SLinus Torvalds _fh_update_old(dentry, exp, &fhp->fh_handle); 5631da177e4SLinus Torvalds } else { 5645409e46fSChristoph Hellwig fhp->fh_handle.fh_size = 5655409e46fSChristoph Hellwig key_len(fhp->fh_handle.fh_fsid_type) + 4; 5661da177e4SLinus Torvalds fhp->fh_handle.fh_auth_type = 0; 5675409e46fSChristoph Hellwig 5685409e46fSChristoph Hellwig mk_fsid(fhp->fh_handle.fh_fsid_type, 5695409e46fSChristoph Hellwig fhp->fh_handle.fh_fsid, 5705409e46fSChristoph Hellwig ex_dev, 57154775491SJan Blunck exp->ex_path.dentry->d_inode->i_ino, 572af6a4e28SNeilBrown exp->ex_fsid, exp->ex_uuid); 573af6a4e28SNeilBrown 5746e91ea2bSChristoph Hellwig if (inode) 5756e91ea2bSChristoph Hellwig _fh_update(fhp, exp, dentry); 576216b6cbdSNamjae Jeon if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) { 5771be10a88SJ. Bruce Fields fh_put(fhp); 5781da177e4SLinus Torvalds return nfserr_opnotsupp; 5791da177e4SLinus Torvalds } 5801be10a88SJ. Bruce Fields } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds return 0; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /* 5861da177e4SLinus Torvalds * Update file handle information after changing a dentry. 5871da177e4SLinus Torvalds * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create 5881da177e4SLinus Torvalds */ 58983b11340SAl Viro __be32 5901da177e4SLinus Torvalds fh_update(struct svc_fh *fhp) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds struct dentry *dentry; 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds if (!fhp->fh_dentry) 5951da177e4SLinus Torvalds goto out_bad; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds dentry = fhp->fh_dentry; 5981da177e4SLinus Torvalds if (!dentry->d_inode) 5991da177e4SLinus Torvalds goto out_negative; 6001da177e4SLinus Torvalds if (fhp->fh_handle.fh_version != 1) { 6011da177e4SLinus Torvalds _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle); 6021da177e4SLinus Torvalds } else { 6036e91ea2bSChristoph Hellwig if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT) 60449e73720SJ. Bruce Fields return 0; 6056e91ea2bSChristoph Hellwig 6066e91ea2bSChristoph Hellwig _fh_update(fhp, fhp->fh_export, dentry); 607216b6cbdSNamjae Jeon if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) 6081da177e4SLinus Torvalds return nfserr_opnotsupp; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds return 0; 6111da177e4SLinus Torvalds out_bad: 6121da177e4SLinus Torvalds printk(KERN_ERR "fh_update: fh not verified!\n"); 61349e73720SJ. Bruce Fields return nfserr_serverfault; 6141da177e4SLinus Torvalds out_negative: 61597e47fa1SAl Viro printk(KERN_ERR "fh_update: %pd2 still negative!\n", 61697e47fa1SAl Viro dentry); 61749e73720SJ. Bruce Fields return nfserr_serverfault; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds /* 6211da177e4SLinus Torvalds * Release a file handle. 6221da177e4SLinus Torvalds */ 6231da177e4SLinus Torvalds void 6241da177e4SLinus Torvalds fh_put(struct svc_fh *fhp) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds struct dentry * dentry = fhp->fh_dentry; 6271da177e4SLinus Torvalds struct svc_export * exp = fhp->fh_export; 6281da177e4SLinus Torvalds if (dentry) { 6291da177e4SLinus Torvalds fh_unlock(fhp); 6301da177e4SLinus Torvalds fhp->fh_dentry = NULL; 6311da177e4SLinus Torvalds dput(dentry); 6321da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3 6331da177e4SLinus Torvalds fhp->fh_pre_saved = 0; 6341da177e4SLinus Torvalds fhp->fh_post_saved = 0; 6351da177e4SLinus Torvalds #endif 6361da177e4SLinus Torvalds } 6374a55c101SJan Kara fh_drop_write(fhp); 6381da177e4SLinus Torvalds if (exp) { 639a09581f2SStanislav Kinsbursky exp_put(exp); 6401da177e4SLinus Torvalds fhp->fh_export = NULL; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds return; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* 6461da177e4SLinus Torvalds * Shorthand for dprintk()'s 6471da177e4SLinus Torvalds */ 6481da177e4SLinus Torvalds char * SVCFH_fmt(struct svc_fh *fhp) 6491da177e4SLinus Torvalds { 6501da177e4SLinus Torvalds struct knfsd_fh *fh = &fhp->fh_handle; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds static char buf[80]; 6531da177e4SLinus Torvalds sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x", 6541da177e4SLinus Torvalds fh->fh_size, 6551da177e4SLinus Torvalds fh->fh_base.fh_pad[0], 6561da177e4SLinus Torvalds fh->fh_base.fh_pad[1], 6571da177e4SLinus Torvalds fh->fh_base.fh_pad[2], 6581da177e4SLinus Torvalds fh->fh_base.fh_pad[3], 6591da177e4SLinus Torvalds fh->fh_base.fh_pad[4], 6601da177e4SLinus Torvalds fh->fh_base.fh_pad[5]); 6611da177e4SLinus Torvalds return buf; 6621da177e4SLinus Torvalds } 663af6a4e28SNeilBrown 664af6a4e28SNeilBrown enum fsid_source fsid_source(struct svc_fh *fhp) 665af6a4e28SNeilBrown { 666af6a4e28SNeilBrown if (fhp->fh_handle.fh_version != 1) 667af6a4e28SNeilBrown return FSIDSOURCE_DEV; 668af6a4e28SNeilBrown switch(fhp->fh_handle.fh_fsid_type) { 669af6a4e28SNeilBrown case FSID_DEV: 670af6a4e28SNeilBrown case FSID_ENCODE_DEV: 671af6a4e28SNeilBrown case FSID_MAJOR_MINOR: 6728e498751SJ. Bruce Fields if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV) 673af6a4e28SNeilBrown return FSIDSOURCE_DEV; 674b8da0d1cSNeil Brown break; 675af6a4e28SNeilBrown case FSID_NUM: 676af6a4e28SNeilBrown if (fhp->fh_export->ex_flags & NFSEXP_FSID) 677af6a4e28SNeilBrown return FSIDSOURCE_FSID; 678b8da0d1cSNeil Brown break; 679b8da0d1cSNeil Brown default: 680b8da0d1cSNeil Brown break; 681af6a4e28SNeilBrown } 682b8da0d1cSNeil Brown /* either a UUID type filehandle, or the filehandle doesn't 683b8da0d1cSNeil Brown * match the export. 684b8da0d1cSNeil Brown */ 685b8da0d1cSNeil Brown if (fhp->fh_export->ex_flags & NFSEXP_FSID) 686b8da0d1cSNeil Brown return FSIDSOURCE_FSID; 687b8da0d1cSNeil Brown if (fhp->fh_export->ex_uuid) 688b8da0d1cSNeil Brown return FSIDSOURCE_UUID; 689b8da0d1cSNeil Brown return FSIDSOURCE_DEV; 690af6a4e28SNeilBrown } 691