Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * NFS server file handle treatment.
0004  *
0005  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
0006  * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
0007  * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
0008  * ... and again Southern-Winter 2001 to support export_operations
0009  */
0010 
0011 #include <linux/exportfs.h>
0012 
0013 #include <linux/sunrpc/svcauth_gss.h>
0014 #include "nfsd.h"
0015 #include "vfs.h"
0016 #include "auth.h"
0017 #include "trace.h"
0018 
0019 #define NFSDDBG_FACILITY        NFSDDBG_FH
0020 
0021 
0022 /*
0023  * our acceptability function.
0024  * if NOSUBTREECHECK, accept anything
0025  * if not, require that we can walk up to exp->ex_dentry
0026  * doing some checks on the 'x' bits
0027  */
0028 static int nfsd_acceptable(void *expv, struct dentry *dentry)
0029 {
0030     struct svc_export *exp = expv;
0031     int rv;
0032     struct dentry *tdentry;
0033     struct dentry *parent;
0034 
0035     if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
0036         return 1;
0037 
0038     tdentry = dget(dentry);
0039     while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
0040         /* make sure parents give x permission to user */
0041         int err;
0042         parent = dget_parent(tdentry);
0043         err = inode_permission(&init_user_ns,
0044                        d_inode(parent), MAY_EXEC);
0045         if (err < 0) {
0046             dput(parent);
0047             break;
0048         }
0049         dput(tdentry);
0050         tdentry = parent;
0051     }
0052     if (tdentry != exp->ex_path.dentry)
0053         dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
0054     rv = (tdentry == exp->ex_path.dentry);
0055     dput(tdentry);
0056     return rv;
0057 }
0058 
0059 /* Type check. The correct error return for type mismatches does not seem to be
0060  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
0061  * comment in the NFSv3 spec says this is incorrect (implementation notes for
0062  * the write call).
0063  */
0064 static inline __be32
0065 nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
0066         umode_t requested)
0067 {
0068     umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
0069 
0070     if (requested == 0) /* the caller doesn't care */
0071         return nfs_ok;
0072     if (mode == requested) {
0073         if (mode == S_IFDIR && !d_can_lookup(dentry)) {
0074             WARN_ON_ONCE(1);
0075             return nfserr_notdir;
0076         }
0077         return nfs_ok;
0078     }
0079     /*
0080      * v4 has an error more specific than err_notdir which we should
0081      * return in preference to err_notdir:
0082      */
0083     if (rqstp->rq_vers == 4 && mode == S_IFLNK)
0084         return nfserr_symlink;
0085     if (requested == S_IFDIR)
0086         return nfserr_notdir;
0087     if (mode == S_IFDIR)
0088         return nfserr_isdir;
0089     return nfserr_inval;
0090 }
0091 
0092 static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
0093 {
0094     if (flags & NFSEXP_INSECURE_PORT)
0095         return true;
0096     /* We don't require gss requests to use low ports: */
0097     if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
0098         return true;
0099     return test_bit(RQ_SECURE, &rqstp->rq_flags);
0100 }
0101 
0102 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
0103                       struct svc_export *exp)
0104 {
0105     int flags = nfsexp_flags(rqstp, exp);
0106 
0107     /* Check if the request originated from a secure port. */
0108     if (!nfsd_originating_port_ok(rqstp, flags)) {
0109         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
0110         dprintk("nfsd: request from insecure port %s!\n",
0111                 svc_print_addr(rqstp, buf, sizeof(buf)));
0112         return nfserr_perm;
0113     }
0114 
0115     /* Set user creds for this exportpoint */
0116     return nfserrno(nfsd_setuser(rqstp, exp));
0117 }
0118 
0119 static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
0120     struct dentry *dentry, struct svc_export *exp)
0121 {
0122     if (!(exp->ex_flags & NFSEXP_V4ROOT))
0123         return nfs_ok;
0124     /*
0125      * v2/v3 clients have no need for the V4ROOT export--they use
0126      * the mount protocl instead; also, further V4ROOT checks may be
0127      * in v4-specific code, in which case v2/v3 clients could bypass
0128      * them.
0129      */
0130     if (!nfsd_v4client(rqstp))
0131         return nfserr_stale;
0132     /*
0133      * We're exposing only the directories and symlinks that have to be
0134      * traversed on the way to real exports:
0135      */
0136     if (unlikely(!d_is_dir(dentry) &&
0137              !d_is_symlink(dentry)))
0138         return nfserr_stale;
0139     /*
0140      * A pseudoroot export gives permission to access only one
0141      * single directory; the kernel has to make another upcall
0142      * before granting access to anything else under it:
0143      */
0144     if (unlikely(dentry != exp->ex_path.dentry))
0145         return nfserr_stale;
0146     return nfs_ok;
0147 }
0148 
0149 /*
0150  * Use the given filehandle to look up the corresponding export and
0151  * dentry.  On success, the results are used to set fh_export and
0152  * fh_dentry.
0153  */
0154 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
0155 {
0156     struct knfsd_fh *fh = &fhp->fh_handle;
0157     struct fid *fid = NULL;
0158     struct svc_export *exp;
0159     struct dentry *dentry;
0160     int fileid_type;
0161     int data_left = fh->fh_size/4;
0162     int len;
0163     __be32 error;
0164 
0165     error = nfserr_stale;
0166     if (rqstp->rq_vers > 2)
0167         error = nfserr_badhandle;
0168     if (rqstp->rq_vers == 4 && fh->fh_size == 0)
0169         return nfserr_nofilehandle;
0170 
0171     if (fh->fh_version != 1)
0172         return error;
0173 
0174     if (--data_left < 0)
0175         return error;
0176     if (fh->fh_auth_type != 0)
0177         return error;
0178     len = key_len(fh->fh_fsid_type) / 4;
0179     if (len == 0)
0180         return error;
0181     if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
0182         /* deprecated, convert to type 3 */
0183         len = key_len(FSID_ENCODE_DEV)/4;
0184         fh->fh_fsid_type = FSID_ENCODE_DEV;
0185         /*
0186          * struct knfsd_fh uses host-endian fields, which are
0187          * sometimes used to hold net-endian values. This
0188          * confuses sparse, so we must use __force here to
0189          * keep it from complaining.
0190          */
0191         fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
0192                               ntohl((__force __be32)fh->fh_fsid[1])));
0193         fh->fh_fsid[1] = fh->fh_fsid[2];
0194     }
0195     data_left -= len;
0196     if (data_left < 0)
0197         return error;
0198     exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid);
0199     fid = (struct fid *)(fh->fh_fsid + len);
0200 
0201     error = nfserr_stale;
0202     if (IS_ERR(exp)) {
0203         trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
0204 
0205         if (PTR_ERR(exp) == -ENOENT)
0206             return error;
0207 
0208         return nfserrno(PTR_ERR(exp));
0209     }
0210 
0211     if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
0212         /* Elevate privileges so that the lack of 'r' or 'x'
0213          * permission on some parent directory will
0214          * not stop exportfs_decode_fh from being able
0215          * to reconnect a directory into the dentry cache.
0216          * The same problem can affect "SUBTREECHECK" exports,
0217          * but as nfsd_acceptable depends on correct
0218          * access control settings being in effect, we cannot
0219          * fix that case easily.
0220          */
0221         struct cred *new = prepare_creds();
0222         if (!new) {
0223             error =  nfserrno(-ENOMEM);
0224             goto out;
0225         }
0226         new->cap_effective =
0227             cap_raise_nfsd_set(new->cap_effective,
0228                        new->cap_permitted);
0229         put_cred(override_creds(new));
0230         put_cred(new);
0231     } else {
0232         error = nfsd_setuser_and_check_port(rqstp, exp);
0233         if (error)
0234             goto out;
0235     }
0236 
0237     /*
0238      * Look up the dentry using the NFS file handle.
0239      */
0240     error = nfserr_stale;
0241     if (rqstp->rq_vers > 2)
0242         error = nfserr_badhandle;
0243 
0244     fileid_type = fh->fh_fileid_type;
0245 
0246     if (fileid_type == FILEID_ROOT)
0247         dentry = dget(exp->ex_path.dentry);
0248     else {
0249         dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
0250                         data_left, fileid_type,
0251                         nfsd_acceptable, exp);
0252         if (IS_ERR_OR_NULL(dentry)) {
0253             trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
0254                     dentry ?  PTR_ERR(dentry) : -ESTALE);
0255             switch (PTR_ERR(dentry)) {
0256             case -ENOMEM:
0257             case -ETIMEDOUT:
0258                 break;
0259             default:
0260                 dentry = ERR_PTR(-ESTALE);
0261             }
0262         }
0263     }
0264     if (dentry == NULL)
0265         goto out;
0266     if (IS_ERR(dentry)) {
0267         if (PTR_ERR(dentry) != -EINVAL)
0268             error = nfserrno(PTR_ERR(dentry));
0269         goto out;
0270     }
0271 
0272     if (d_is_dir(dentry) &&
0273             (dentry->d_flags & DCACHE_DISCONNECTED)) {
0274         printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
0275                 dentry);
0276     }
0277 
0278     fhp->fh_dentry = dentry;
0279     fhp->fh_export = exp;
0280 
0281     switch (rqstp->rq_vers) {
0282     case 4:
0283         if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
0284             fhp->fh_no_atomic_attr = true;
0285         break;
0286     case 3:
0287         if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
0288             fhp->fh_no_wcc = true;
0289         break;
0290     case 2:
0291         fhp->fh_no_wcc = true;
0292     }
0293 
0294     return 0;
0295 out:
0296     exp_put(exp);
0297     return error;
0298 }
0299 
0300 /**
0301  * fh_verify - filehandle lookup and access checking
0302  * @rqstp: pointer to current rpc request
0303  * @fhp: filehandle to be verified
0304  * @type: expected type of object pointed to by filehandle
0305  * @access: type of access needed to object
0306  *
0307  * Look up a dentry from the on-the-wire filehandle, check the client's
0308  * access to the export, and set the current task's credentials.
0309  *
0310  * Regardless of success or failure of fh_verify(), fh_put() should be
0311  * called on @fhp when the caller is finished with the filehandle.
0312  *
0313  * fh_verify() may be called multiple times on a given filehandle, for
0314  * example, when processing an NFSv4 compound.  The first call will look
0315  * up a dentry using the on-the-wire filehandle.  Subsequent calls will
0316  * skip the lookup and just perform the other checks and possibly change
0317  * the current task's credentials.
0318  *
0319  * @type specifies the type of object expected using one of the S_IF*
0320  * constants defined in include/linux/stat.h.  The caller may use zero
0321  * to indicate that it doesn't care, or a negative integer to indicate
0322  * that it expects something not of the given type.
0323  *
0324  * @access is formed from the NFSD_MAY_* constants defined in
0325  * fs/nfsd/vfs.h.
0326  */
0327 __be32
0328 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
0329 {
0330     struct svc_export *exp = NULL;
0331     struct dentry   *dentry;
0332     __be32      error;
0333 
0334     if (!fhp->fh_dentry) {
0335         error = nfsd_set_fh_dentry(rqstp, fhp);
0336         if (error)
0337             goto out;
0338     }
0339     dentry = fhp->fh_dentry;
0340     exp = fhp->fh_export;
0341 
0342     trace_nfsd_fh_verify(rqstp, fhp, type, access);
0343 
0344     /*
0345      * We still have to do all these permission checks, even when
0346      * fh_dentry is already set:
0347      *  - fh_verify may be called multiple times with different
0348      *    "access" arguments (e.g. nfsd_proc_create calls
0349      *    fh_verify(...,NFSD_MAY_EXEC) first, then later (in
0350      *    nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
0351      *  - in the NFSv4 case, the filehandle may have been filled
0352      *    in by fh_compose, and given a dentry, but further
0353      *    compound operations performed with that filehandle
0354      *    still need permissions checks.  In the worst case, a
0355      *    mountpoint crossing may have changed the export
0356      *    options, and we may now need to use a different uid
0357      *    (for example, if different id-squashing options are in
0358      *    effect on the new filesystem).
0359      */
0360     error = check_pseudo_root(rqstp, dentry, exp);
0361     if (error)
0362         goto out;
0363 
0364     error = nfsd_setuser_and_check_port(rqstp, exp);
0365     if (error)
0366         goto out;
0367 
0368     error = nfsd_mode_check(rqstp, dentry, type);
0369     if (error)
0370         goto out;
0371 
0372     /*
0373      * pseudoflavor restrictions are not enforced on NLM,
0374      * which clients virtually always use auth_sys for,
0375      * even while using RPCSEC_GSS for NFS.
0376      */
0377     if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
0378         goto skip_pseudoflavor_check;
0379     /*
0380      * Clients may expect to be able to use auth_sys during mount,
0381      * even if they use gss for everything else; see section 2.3.2
0382      * of rfc 2623.
0383      */
0384     if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
0385             && exp->ex_path.dentry == dentry)
0386         goto skip_pseudoflavor_check;
0387 
0388     error = check_nfsd_access(exp, rqstp);
0389     if (error)
0390         goto out;
0391 
0392 skip_pseudoflavor_check:
0393     /* Finally, check access permissions. */
0394     error = nfsd_permission(rqstp, exp, dentry, access);
0395 
0396     if (error) {
0397         dprintk("fh_verify: %pd2 permission failure, "
0398             "acc=%x, error=%d\n",
0399             dentry,
0400             access, ntohl(error));
0401     }
0402 out:
0403     if (error == nfserr_stale)
0404         nfsd_stats_fh_stale_inc(exp);
0405     return error;
0406 }
0407 
0408 
0409 /*
0410  * Compose a file handle for an NFS reply.
0411  *
0412  * Note that when first composed, the dentry may not yet have
0413  * an inode.  In this case a call to fh_update should be made
0414  * before the fh goes out on the wire ...
0415  */
0416 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
0417         struct dentry *dentry)
0418 {
0419     if (dentry != exp->ex_path.dentry) {
0420         struct fid *fid = (struct fid *)
0421             (fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
0422         int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
0423         int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
0424 
0425         fhp->fh_handle.fh_fileid_type =
0426             exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
0427         fhp->fh_handle.fh_size += maxsize * 4;
0428     } else {
0429         fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
0430     }
0431 }
0432 
0433 static bool is_root_export(struct svc_export *exp)
0434 {
0435     return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
0436 }
0437 
0438 static struct super_block *exp_sb(struct svc_export *exp)
0439 {
0440     return exp->ex_path.dentry->d_sb;
0441 }
0442 
0443 static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
0444 {
0445     switch (fsid_type) {
0446     case FSID_DEV:
0447         if (!old_valid_dev(exp_sb(exp)->s_dev))
0448             return false;
0449         fallthrough;
0450     case FSID_MAJOR_MINOR:
0451     case FSID_ENCODE_DEV:
0452         return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
0453     case FSID_NUM:
0454         return exp->ex_flags & NFSEXP_FSID;
0455     case FSID_UUID8:
0456     case FSID_UUID16:
0457         if (!is_root_export(exp))
0458             return false;
0459         fallthrough;
0460     case FSID_UUID4_INUM:
0461     case FSID_UUID16_INUM:
0462         return exp->ex_uuid != NULL;
0463     }
0464     return true;
0465 }
0466 
0467 
0468 static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
0469 {
0470     u8 version;
0471     u8 fsid_type;
0472 retry:
0473     version = 1;
0474     if (ref_fh && ref_fh->fh_export == exp) {
0475         version = ref_fh->fh_handle.fh_version;
0476         fsid_type = ref_fh->fh_handle.fh_fsid_type;
0477 
0478         ref_fh = NULL;
0479 
0480         switch (version) {
0481         case 0xca:
0482             fsid_type = FSID_DEV;
0483             break;
0484         case 1:
0485             break;
0486         default:
0487             goto retry;
0488         }
0489 
0490         /*
0491          * As the fsid -> filesystem mapping was guided by
0492          * user-space, there is no guarantee that the filesystem
0493          * actually supports that fsid type. If it doesn't we
0494          * loop around again without ref_fh set.
0495          */
0496         if (!fsid_type_ok_for_exp(fsid_type, exp))
0497             goto retry;
0498     } else if (exp->ex_flags & NFSEXP_FSID) {
0499         fsid_type = FSID_NUM;
0500     } else if (exp->ex_uuid) {
0501         if (fhp->fh_maxsize >= 64) {
0502             if (is_root_export(exp))
0503                 fsid_type = FSID_UUID16;
0504             else
0505                 fsid_type = FSID_UUID16_INUM;
0506         } else {
0507             if (is_root_export(exp))
0508                 fsid_type = FSID_UUID8;
0509             else
0510                 fsid_type = FSID_UUID4_INUM;
0511         }
0512     } else if (!old_valid_dev(exp_sb(exp)->s_dev))
0513         /* for newer device numbers, we must use a newer fsid format */
0514         fsid_type = FSID_ENCODE_DEV;
0515     else
0516         fsid_type = FSID_DEV;
0517     fhp->fh_handle.fh_version = version;
0518     if (version)
0519         fhp->fh_handle.fh_fsid_type = fsid_type;
0520 }
0521 
0522 __be32
0523 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
0524        struct svc_fh *ref_fh)
0525 {
0526     /* ref_fh is a reference file handle.
0527      * if it is non-null and for the same filesystem, then we should compose
0528      * a filehandle which is of the same version, where possible.
0529      */
0530 
0531     struct inode * inode = d_inode(dentry);
0532     dev_t ex_dev = exp_sb(exp)->s_dev;
0533 
0534     dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
0535         MAJOR(ex_dev), MINOR(ex_dev),
0536         (long) d_inode(exp->ex_path.dentry)->i_ino,
0537         dentry,
0538         (inode ? inode->i_ino : 0));
0539 
0540     /* Choose filehandle version and fsid type based on
0541      * the reference filehandle (if it is in the same export)
0542      * or the export options.
0543      */
0544     set_version_and_fsid_type(fhp, exp, ref_fh);
0545 
0546     /* If we have a ref_fh, then copy the fh_no_wcc setting from it. */
0547     fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
0548 
0549     if (ref_fh == fhp)
0550         fh_put(ref_fh);
0551 
0552     if (fhp->fh_dentry) {
0553         printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
0554                dentry);
0555     }
0556     if (fhp->fh_maxsize < NFS_FHSIZE)
0557         printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
0558                fhp->fh_maxsize,
0559                dentry);
0560 
0561     fhp->fh_dentry = dget(dentry); /* our internal copy */
0562     fhp->fh_export = exp_get(exp);
0563 
0564     fhp->fh_handle.fh_size =
0565         key_len(fhp->fh_handle.fh_fsid_type) + 4;
0566     fhp->fh_handle.fh_auth_type = 0;
0567 
0568     mk_fsid(fhp->fh_handle.fh_fsid_type,
0569         fhp->fh_handle.fh_fsid,
0570         ex_dev,
0571         d_inode(exp->ex_path.dentry)->i_ino,
0572         exp->ex_fsid, exp->ex_uuid);
0573 
0574     if (inode)
0575         _fh_update(fhp, exp, dentry);
0576     if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
0577         fh_put(fhp);
0578         return nfserr_opnotsupp;
0579     }
0580 
0581     return 0;
0582 }
0583 
0584 /*
0585  * Update file handle information after changing a dentry.
0586  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
0587  */
0588 __be32
0589 fh_update(struct svc_fh *fhp)
0590 {
0591     struct dentry *dentry;
0592 
0593     if (!fhp->fh_dentry)
0594         goto out_bad;
0595 
0596     dentry = fhp->fh_dentry;
0597     if (d_really_is_negative(dentry))
0598         goto out_negative;
0599     if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
0600         return 0;
0601 
0602     _fh_update(fhp, fhp->fh_export, dentry);
0603     if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
0604         return nfserr_opnotsupp;
0605     return 0;
0606 out_bad:
0607     printk(KERN_ERR "fh_update: fh not verified!\n");
0608     return nfserr_serverfault;
0609 out_negative:
0610     printk(KERN_ERR "fh_update: %pd2 still negative!\n",
0611         dentry);
0612     return nfserr_serverfault;
0613 }
0614 
0615 /**
0616  * fh_fill_pre_attrs - Fill in pre-op attributes
0617  * @fhp: file handle to be updated
0618  *
0619  */
0620 void fh_fill_pre_attrs(struct svc_fh *fhp)
0621 {
0622     bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
0623     struct inode *inode;
0624     struct kstat stat;
0625     __be32 err;
0626 
0627     if (fhp->fh_no_wcc || fhp->fh_pre_saved)
0628         return;
0629 
0630     inode = d_inode(fhp->fh_dentry);
0631     err = fh_getattr(fhp, &stat);
0632     if (err) {
0633         /* Grab the times from inode anyway */
0634         stat.mtime = inode->i_mtime;
0635         stat.ctime = inode->i_ctime;
0636         stat.size  = inode->i_size;
0637     }
0638     if (v4)
0639         fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
0640 
0641     fhp->fh_pre_mtime = stat.mtime;
0642     fhp->fh_pre_ctime = stat.ctime;
0643     fhp->fh_pre_size  = stat.size;
0644     fhp->fh_pre_saved = true;
0645 }
0646 
0647 /**
0648  * fh_fill_post_attrs - Fill in post-op attributes
0649  * @fhp: file handle to be updated
0650  *
0651  */
0652 void fh_fill_post_attrs(struct svc_fh *fhp)
0653 {
0654     bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
0655     struct inode *inode = d_inode(fhp->fh_dentry);
0656     __be32 err;
0657 
0658     if (fhp->fh_no_wcc)
0659         return;
0660 
0661     if (fhp->fh_post_saved)
0662         printk("nfsd: inode locked twice during operation.\n");
0663 
0664     err = fh_getattr(fhp, &fhp->fh_post_attr);
0665     if (err) {
0666         fhp->fh_post_saved = false;
0667         fhp->fh_post_attr.ctime = inode->i_ctime;
0668     } else
0669         fhp->fh_post_saved = true;
0670     if (v4)
0671         fhp->fh_post_change =
0672             nfsd4_change_attribute(&fhp->fh_post_attr, inode);
0673 }
0674 
0675 /**
0676  * fh_fill_both_attrs - Fill pre-op and post-op attributes
0677  * @fhp: file handle to be updated
0678  *
0679  * This is used when the directory wasn't changed, but wcc attributes
0680  * are needed anyway.
0681  */
0682 void fh_fill_both_attrs(struct svc_fh *fhp)
0683 {
0684     fh_fill_post_attrs(fhp);
0685     if (!fhp->fh_post_saved)
0686         return;
0687     fhp->fh_pre_change = fhp->fh_post_change;
0688     fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
0689     fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
0690     fhp->fh_pre_size = fhp->fh_post_attr.size;
0691     fhp->fh_pre_saved = true;
0692 }
0693 
0694 /*
0695  * Release a file handle.
0696  */
0697 void
0698 fh_put(struct svc_fh *fhp)
0699 {
0700     struct dentry * dentry = fhp->fh_dentry;
0701     struct svc_export * exp = fhp->fh_export;
0702     if (dentry) {
0703         fhp->fh_dentry = NULL;
0704         dput(dentry);
0705         fh_clear_pre_post_attrs(fhp);
0706     }
0707     fh_drop_write(fhp);
0708     if (exp) {
0709         exp_put(exp);
0710         fhp->fh_export = NULL;
0711     }
0712     fhp->fh_no_wcc = false;
0713     return;
0714 }
0715 
0716 /*
0717  * Shorthand for dprintk()'s
0718  */
0719 char * SVCFH_fmt(struct svc_fh *fhp)
0720 {
0721     struct knfsd_fh *fh = &fhp->fh_handle;
0722     static char buf[2+1+1+64*3+1];
0723 
0724     if (fh->fh_size < 0 || fh->fh_size> 64)
0725         return "bad-fh";
0726     sprintf(buf, "%d: %*ph", fh->fh_size, fh->fh_size, fh->fh_raw);
0727     return buf;
0728 }
0729 
0730 enum fsid_source fsid_source(const struct svc_fh *fhp)
0731 {
0732     if (fhp->fh_handle.fh_version != 1)
0733         return FSIDSOURCE_DEV;
0734     switch(fhp->fh_handle.fh_fsid_type) {
0735     case FSID_DEV:
0736     case FSID_ENCODE_DEV:
0737     case FSID_MAJOR_MINOR:
0738         if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
0739             return FSIDSOURCE_DEV;
0740         break;
0741     case FSID_NUM:
0742         if (fhp->fh_export->ex_flags & NFSEXP_FSID)
0743             return FSIDSOURCE_FSID;
0744         break;
0745     default:
0746         break;
0747     }
0748     /* either a UUID type filehandle, or the filehandle doesn't
0749      * match the export.
0750      */
0751     if (fhp->fh_export->ex_flags & NFSEXP_FSID)
0752         return FSIDSOURCE_FSID;
0753     if (fhp->fh_export->ex_uuid)
0754         return FSIDSOURCE_UUID;
0755     return FSIDSOURCE_DEV;
0756 }