Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
0004  * All Rights Reserved.
0005  */
0006 
0007 #include "xfs.h"
0008 #include "xfs_fs.h"
0009 #include "xfs_shared.h"
0010 #include "xfs_format.h"
0011 #include "xfs_log_format.h"
0012 #include "xfs_trans_resv.h"
0013 #include "xfs_mount.h"
0014 #include "xfs_inode.h"
0015 #include "xfs_trans.h"
0016 #include "xfs_inode_item.h"
0017 #include "xfs_btree.h"
0018 #include "xfs_bmap_btree.h"
0019 #include "xfs_bmap.h"
0020 #include "xfs_error.h"
0021 #include "xfs_trace.h"
0022 #include "xfs_da_format.h"
0023 #include "xfs_da_btree.h"
0024 #include "xfs_dir2_priv.h"
0025 #include "xfs_attr_leaf.h"
0026 #include "xfs_types.h"
0027 #include "xfs_errortag.h"
0028 
0029 struct kmem_cache *xfs_ifork_cache;
0030 
0031 void
0032 xfs_init_local_fork(
0033     struct xfs_inode    *ip,
0034     int         whichfork,
0035     const void      *data,
0036     int64_t         size)
0037 {
0038     struct xfs_ifork    *ifp = xfs_ifork_ptr(ip, whichfork);
0039     int         mem_size = size;
0040     bool            zero_terminate;
0041 
0042     /*
0043      * If we are using the local fork to store a symlink body we need to
0044      * zero-terminate it so that we can pass it back to the VFS directly.
0045      * Overallocate the in-memory fork by one for that and add a zero
0046      * to terminate it below.
0047      */
0048     zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
0049     if (zero_terminate)
0050         mem_size++;
0051 
0052     if (size) {
0053         ifp->if_u1.if_data = kmem_alloc(mem_size, KM_NOFS);
0054         memcpy(ifp->if_u1.if_data, data, size);
0055         if (zero_terminate)
0056             ifp->if_u1.if_data[size] = '\0';
0057     } else {
0058         ifp->if_u1.if_data = NULL;
0059     }
0060 
0061     ifp->if_bytes = size;
0062 }
0063 
0064 /*
0065  * The file is in-lined in the on-disk inode.
0066  */
0067 STATIC int
0068 xfs_iformat_local(
0069     struct xfs_inode    *ip,
0070     struct xfs_dinode   *dip,
0071     int         whichfork,
0072     int         size)
0073 {
0074     /*
0075      * If the size is unreasonable, then something
0076      * is wrong and we just bail out rather than crash in
0077      * kmem_alloc() or memcpy() below.
0078      */
0079     if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
0080         xfs_warn(ip->i_mount,
0081     "corrupt inode %Lu (bad size %d for local fork, size = %zd).",
0082             (unsigned long long) ip->i_ino, size,
0083             XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
0084         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
0085                 "xfs_iformat_local", dip, sizeof(*dip),
0086                 __this_address);
0087         return -EFSCORRUPTED;
0088     }
0089 
0090     xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
0091     return 0;
0092 }
0093 
0094 /*
0095  * The file consists of a set of extents all of which fit into the on-disk
0096  * inode.
0097  */
0098 STATIC int
0099 xfs_iformat_extents(
0100     struct xfs_inode    *ip,
0101     struct xfs_dinode   *dip,
0102     int         whichfork)
0103 {
0104     struct xfs_mount    *mp = ip->i_mount;
0105     struct xfs_ifork    *ifp = xfs_ifork_ptr(ip, whichfork);
0106     int         state = xfs_bmap_fork_to_state(whichfork);
0107     xfs_extnum_t        nex = xfs_dfork_nextents(dip, whichfork);
0108     int         size = nex * sizeof(xfs_bmbt_rec_t);
0109     struct xfs_iext_cursor  icur;
0110     struct xfs_bmbt_rec *dp;
0111     struct xfs_bmbt_irec    new;
0112     int         i;
0113 
0114     /*
0115      * If the number of extents is unreasonable, then something is wrong and
0116      * we just bail out rather than crash in kmem_alloc() or memcpy() below.
0117      */
0118     if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
0119         xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
0120             ip->i_ino, nex);
0121         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
0122                 "xfs_iformat_extents(1)", dip, sizeof(*dip),
0123                 __this_address);
0124         return -EFSCORRUPTED;
0125     }
0126 
0127     ifp->if_bytes = 0;
0128     ifp->if_u1.if_root = NULL;
0129     ifp->if_height = 0;
0130     if (size) {
0131         dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
0132 
0133         xfs_iext_first(ifp, &icur);
0134         for (i = 0; i < nex; i++, dp++) {
0135             xfs_failaddr_t  fa;
0136 
0137             xfs_bmbt_disk_get_all(dp, &new);
0138             fa = xfs_bmap_validate_extent(ip, whichfork, &new);
0139             if (fa) {
0140                 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
0141                         "xfs_iformat_extents(2)",
0142                         dp, sizeof(*dp), fa);
0143                 return -EFSCORRUPTED;
0144             }
0145 
0146             xfs_iext_insert(ip, &icur, &new, state);
0147             trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
0148             xfs_iext_next(ifp, &icur);
0149         }
0150     }
0151     return 0;
0152 }
0153 
0154 /*
0155  * The file has too many extents to fit into
0156  * the inode, so they are in B-tree format.
0157  * Allocate a buffer for the root of the B-tree
0158  * and copy the root into it.  The i_extents
0159  * field will remain NULL until all of the
0160  * extents are read in (when they are needed).
0161  */
0162 STATIC int
0163 xfs_iformat_btree(
0164     struct xfs_inode    *ip,
0165     struct xfs_dinode   *dip,
0166     int         whichfork)
0167 {
0168     struct xfs_mount    *mp = ip->i_mount;
0169     xfs_bmdr_block_t    *dfp;
0170     struct xfs_ifork    *ifp;
0171     /* REFERENCED */
0172     int         nrecs;
0173     int         size;
0174     int         level;
0175 
0176     ifp = xfs_ifork_ptr(ip, whichfork);
0177     dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
0178     size = XFS_BMAP_BROOT_SPACE(mp, dfp);
0179     nrecs = be16_to_cpu(dfp->bb_numrecs);
0180     level = be16_to_cpu(dfp->bb_level);
0181 
0182     /*
0183      * blow out if -- fork has less extents than can fit in
0184      * fork (fork shouldn't be a btree format), root btree
0185      * block has more records than can fit into the fork,
0186      * or the number of extents is greater than the number of
0187      * blocks.
0188      */
0189     if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
0190              nrecs == 0 ||
0191              XFS_BMDR_SPACE_CALC(nrecs) >
0192                     XFS_DFORK_SIZE(dip, mp, whichfork) ||
0193              ifp->if_nextents > ip->i_nblocks) ||
0194              level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
0195         xfs_warn(mp, "corrupt inode %Lu (btree).",
0196                     (unsigned long long) ip->i_ino);
0197         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
0198                 "xfs_iformat_btree", dfp, size,
0199                 __this_address);
0200         return -EFSCORRUPTED;
0201     }
0202 
0203     ifp->if_broot_bytes = size;
0204     ifp->if_broot = kmem_alloc(size, KM_NOFS);
0205     ASSERT(ifp->if_broot != NULL);
0206     /*
0207      * Copy and convert from the on-disk structure
0208      * to the in-memory structure.
0209      */
0210     xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
0211              ifp->if_broot, size);
0212 
0213     ifp->if_bytes = 0;
0214     ifp->if_u1.if_root = NULL;
0215     ifp->if_height = 0;
0216     return 0;
0217 }
0218 
0219 int
0220 xfs_iformat_data_fork(
0221     struct xfs_inode    *ip,
0222     struct xfs_dinode   *dip)
0223 {
0224     struct inode        *inode = VFS_I(ip);
0225     int         error;
0226 
0227     /*
0228      * Initialize the extent count early, as the per-format routines may
0229      * depend on it.
0230      */
0231     ip->i_df.if_format = dip->di_format;
0232     ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
0233 
0234     switch (inode->i_mode & S_IFMT) {
0235     case S_IFIFO:
0236     case S_IFCHR:
0237     case S_IFBLK:
0238     case S_IFSOCK:
0239         ip->i_disk_size = 0;
0240         inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
0241         return 0;
0242     case S_IFREG:
0243     case S_IFLNK:
0244     case S_IFDIR:
0245         switch (ip->i_df.if_format) {
0246         case XFS_DINODE_FMT_LOCAL:
0247             error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
0248                     be64_to_cpu(dip->di_size));
0249             if (!error)
0250                 error = xfs_ifork_verify_local_data(ip);
0251             return error;
0252         case XFS_DINODE_FMT_EXTENTS:
0253             return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
0254         case XFS_DINODE_FMT_BTREE:
0255             return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
0256         default:
0257             xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
0258                     dip, sizeof(*dip), __this_address);
0259             return -EFSCORRUPTED;
0260         }
0261         break;
0262     default:
0263         xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
0264                 sizeof(*dip), __this_address);
0265         return -EFSCORRUPTED;
0266     }
0267 }
0268 
0269 static uint16_t
0270 xfs_dfork_attr_shortform_size(
0271     struct xfs_dinode       *dip)
0272 {
0273     struct xfs_attr_shortform   *atp =
0274         (struct xfs_attr_shortform *)XFS_DFORK_APTR(dip);
0275 
0276     return be16_to_cpu(atp->hdr.totsize);
0277 }
0278 
0279 void
0280 xfs_ifork_init_attr(
0281     struct xfs_inode    *ip,
0282     enum xfs_dinode_fmt format,
0283     xfs_extnum_t        nextents)
0284 {
0285     ip->i_af.if_format = format;
0286     ip->i_af.if_nextents = nextents;
0287 }
0288 
0289 void
0290 xfs_ifork_zap_attr(
0291     struct xfs_inode    *ip)
0292 {
0293     xfs_idestroy_fork(&ip->i_af);
0294     memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
0295     ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
0296 }
0297 
0298 int
0299 xfs_iformat_attr_fork(
0300     struct xfs_inode    *ip,
0301     struct xfs_dinode   *dip)
0302 {
0303     xfs_extnum_t        naextents = xfs_dfork_attr_extents(dip);
0304     int         error = 0;
0305 
0306     /*
0307      * Initialize the extent count early, as the per-format routines may
0308      * depend on it.
0309      */
0310     xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
0311 
0312     switch (ip->i_af.if_format) {
0313     case XFS_DINODE_FMT_LOCAL:
0314         error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
0315                 xfs_dfork_attr_shortform_size(dip));
0316         if (!error)
0317             error = xfs_ifork_verify_local_attr(ip);
0318         break;
0319     case XFS_DINODE_FMT_EXTENTS:
0320         error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
0321         break;
0322     case XFS_DINODE_FMT_BTREE:
0323         error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
0324         break;
0325     default:
0326         xfs_inode_verifier_error(ip, error, __func__, dip,
0327                 sizeof(*dip), __this_address);
0328         error = -EFSCORRUPTED;
0329         break;
0330     }
0331 
0332     if (error)
0333         xfs_ifork_zap_attr(ip);
0334     return error;
0335 }
0336 
0337 /*
0338  * Reallocate the space for if_broot based on the number of records
0339  * being added or deleted as indicated in rec_diff.  Move the records
0340  * and pointers in if_broot to fit the new size.  When shrinking this
0341  * will eliminate holes between the records and pointers created by
0342  * the caller.  When growing this will create holes to be filled in
0343  * by the caller.
0344  *
0345  * The caller must not request to add more records than would fit in
0346  * the on-disk inode root.  If the if_broot is currently NULL, then
0347  * if we are adding records, one will be allocated.  The caller must also
0348  * not request that the number of records go below zero, although
0349  * it can go to zero.
0350  *
0351  * ip -- the inode whose if_broot area is changing
0352  * ext_diff -- the change in the number of records, positive or negative,
0353  *   requested for the if_broot array.
0354  */
0355 void
0356 xfs_iroot_realloc(
0357     xfs_inode_t     *ip,
0358     int         rec_diff,
0359     int         whichfork)
0360 {
0361     struct xfs_mount    *mp = ip->i_mount;
0362     int         cur_max;
0363     struct xfs_ifork    *ifp;
0364     struct xfs_btree_block  *new_broot;
0365     int         new_max;
0366     size_t          new_size;
0367     char            *np;
0368     char            *op;
0369 
0370     /*
0371      * Handle the degenerate case quietly.
0372      */
0373     if (rec_diff == 0) {
0374         return;
0375     }
0376 
0377     ifp = xfs_ifork_ptr(ip, whichfork);
0378     if (rec_diff > 0) {
0379         /*
0380          * If there wasn't any memory allocated before, just
0381          * allocate it now and get out.
0382          */
0383         if (ifp->if_broot_bytes == 0) {
0384             new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
0385             ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
0386             ifp->if_broot_bytes = (int)new_size;
0387             return;
0388         }
0389 
0390         /*
0391          * If there is already an existing if_broot, then we need
0392          * to realloc() it and shift the pointers to their new
0393          * location.  The records don't change location because
0394          * they are kept butted up against the btree block header.
0395          */
0396         cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
0397         new_max = cur_max + rec_diff;
0398         new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
0399         ifp->if_broot = krealloc(ifp->if_broot, new_size,
0400                      GFP_NOFS | __GFP_NOFAIL);
0401         op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
0402                              ifp->if_broot_bytes);
0403         np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
0404                              (int)new_size);
0405         ifp->if_broot_bytes = (int)new_size;
0406         ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
0407             xfs_inode_fork_size(ip, whichfork));
0408         memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
0409         return;
0410     }
0411 
0412     /*
0413      * rec_diff is less than 0.  In this case, we are shrinking the
0414      * if_broot buffer.  It must already exist.  If we go to zero
0415      * records, just get rid of the root and clear the status bit.
0416      */
0417     ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
0418     cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
0419     new_max = cur_max + rec_diff;
0420     ASSERT(new_max >= 0);
0421     if (new_max > 0)
0422         new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
0423     else
0424         new_size = 0;
0425     if (new_size > 0) {
0426         new_broot = kmem_alloc(new_size, KM_NOFS);
0427         /*
0428          * First copy over the btree block header.
0429          */
0430         memcpy(new_broot, ifp->if_broot,
0431             XFS_BMBT_BLOCK_LEN(ip->i_mount));
0432     } else {
0433         new_broot = NULL;
0434     }
0435 
0436     /*
0437      * Only copy the records and pointers if there are any.
0438      */
0439     if (new_max > 0) {
0440         /*
0441          * First copy the records.
0442          */
0443         op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
0444         np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
0445         memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
0446 
0447         /*
0448          * Then copy the pointers.
0449          */
0450         op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
0451                              ifp->if_broot_bytes);
0452         np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
0453                              (int)new_size);
0454         memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
0455     }
0456     kmem_free(ifp->if_broot);
0457     ifp->if_broot = new_broot;
0458     ifp->if_broot_bytes = (int)new_size;
0459     if (ifp->if_broot)
0460         ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
0461             xfs_inode_fork_size(ip, whichfork));
0462     return;
0463 }
0464 
0465 
0466 /*
0467  * This is called when the amount of space needed for if_data
0468  * is increased or decreased.  The change in size is indicated by
0469  * the number of bytes that need to be added or deleted in the
0470  * byte_diff parameter.
0471  *
0472  * If the amount of space needed has decreased below the size of the
0473  * inline buffer, then switch to using the inline buffer.  Otherwise,
0474  * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
0475  * to what is needed.
0476  *
0477  * ip -- the inode whose if_data area is changing
0478  * byte_diff -- the change in the number of bytes, positive or negative,
0479  *   requested for the if_data array.
0480  */
0481 void
0482 xfs_idata_realloc(
0483     struct xfs_inode    *ip,
0484     int64_t         byte_diff,
0485     int         whichfork)
0486 {
0487     struct xfs_ifork    *ifp = xfs_ifork_ptr(ip, whichfork);
0488     int64_t         new_size = ifp->if_bytes + byte_diff;
0489 
0490     ASSERT(new_size >= 0);
0491     ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
0492 
0493     if (byte_diff == 0)
0494         return;
0495 
0496     if (new_size == 0) {
0497         kmem_free(ifp->if_u1.if_data);
0498         ifp->if_u1.if_data = NULL;
0499         ifp->if_bytes = 0;
0500         return;
0501     }
0502 
0503     ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, new_size,
0504                       GFP_NOFS | __GFP_NOFAIL);
0505     ifp->if_bytes = new_size;
0506 }
0507 
0508 void
0509 xfs_idestroy_fork(
0510     struct xfs_ifork    *ifp)
0511 {
0512     if (ifp->if_broot != NULL) {
0513         kmem_free(ifp->if_broot);
0514         ifp->if_broot = NULL;
0515     }
0516 
0517     switch (ifp->if_format) {
0518     case XFS_DINODE_FMT_LOCAL:
0519         kmem_free(ifp->if_u1.if_data);
0520         ifp->if_u1.if_data = NULL;
0521         break;
0522     case XFS_DINODE_FMT_EXTENTS:
0523     case XFS_DINODE_FMT_BTREE:
0524         if (ifp->if_height)
0525             xfs_iext_destroy(ifp);
0526         break;
0527     }
0528 }
0529 
0530 /*
0531  * Convert in-core extents to on-disk form
0532  *
0533  * In the case of the data fork, the in-core and on-disk fork sizes can be
0534  * different due to delayed allocation extents. We only copy on-disk extents
0535  * here, so callers must always use the physical fork size to determine the
0536  * size of the buffer passed to this routine.  We will return the size actually
0537  * used.
0538  */
0539 int
0540 xfs_iextents_copy(
0541     struct xfs_inode    *ip,
0542     struct xfs_bmbt_rec *dp,
0543     int         whichfork)
0544 {
0545     int         state = xfs_bmap_fork_to_state(whichfork);
0546     struct xfs_ifork    *ifp = xfs_ifork_ptr(ip, whichfork);
0547     struct xfs_iext_cursor  icur;
0548     struct xfs_bmbt_irec    rec;
0549     int64_t         copied = 0;
0550 
0551     ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
0552     ASSERT(ifp->if_bytes > 0);
0553 
0554     for_each_xfs_iext(ifp, &icur, &rec) {
0555         if (isnullstartblock(rec.br_startblock))
0556             continue;
0557         ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
0558         xfs_bmbt_disk_set_all(dp, &rec);
0559         trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
0560         copied += sizeof(struct xfs_bmbt_rec);
0561         dp++;
0562     }
0563 
0564     ASSERT(copied > 0);
0565     ASSERT(copied <= ifp->if_bytes);
0566     return copied;
0567 }
0568 
0569 /*
0570  * Each of the following cases stores data into the same region
0571  * of the on-disk inode, so only one of them can be valid at
0572  * any given time. While it is possible to have conflicting formats
0573  * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
0574  * in EXTENTS format, this can only happen when the fork has
0575  * changed formats after being modified but before being flushed.
0576  * In these cases, the format always takes precedence, because the
0577  * format indicates the current state of the fork.
0578  */
0579 void
0580 xfs_iflush_fork(
0581     struct xfs_inode    *ip,
0582     struct xfs_dinode   *dip,
0583     struct xfs_inode_log_item *iip,
0584     int         whichfork)
0585 {
0586     char            *cp;
0587     struct xfs_ifork    *ifp;
0588     xfs_mount_t     *mp;
0589     static const short  brootflag[2] =
0590         { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
0591     static const short  dataflag[2] =
0592         { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
0593     static const short  extflag[2] =
0594         { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
0595 
0596     if (!iip)
0597         return;
0598     ifp = xfs_ifork_ptr(ip, whichfork);
0599     /*
0600      * This can happen if we gave up in iformat in an error path,
0601      * for the attribute fork.
0602      */
0603     if (!ifp) {
0604         ASSERT(whichfork == XFS_ATTR_FORK);
0605         return;
0606     }
0607     cp = XFS_DFORK_PTR(dip, whichfork);
0608     mp = ip->i_mount;
0609     switch (ifp->if_format) {
0610     case XFS_DINODE_FMT_LOCAL:
0611         if ((iip->ili_fields & dataflag[whichfork]) &&
0612             (ifp->if_bytes > 0)) {
0613             ASSERT(ifp->if_u1.if_data != NULL);
0614             ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
0615             memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
0616         }
0617         break;
0618 
0619     case XFS_DINODE_FMT_EXTENTS:
0620         if ((iip->ili_fields & extflag[whichfork]) &&
0621             (ifp->if_bytes > 0)) {
0622             ASSERT(ifp->if_nextents > 0);
0623             (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
0624                 whichfork);
0625         }
0626         break;
0627 
0628     case XFS_DINODE_FMT_BTREE:
0629         if ((iip->ili_fields & brootflag[whichfork]) &&
0630             (ifp->if_broot_bytes > 0)) {
0631             ASSERT(ifp->if_broot != NULL);
0632             ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
0633                     xfs_inode_fork_size(ip, whichfork));
0634             xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
0635                 (xfs_bmdr_block_t *)cp,
0636                 XFS_DFORK_SIZE(dip, mp, whichfork));
0637         }
0638         break;
0639 
0640     case XFS_DINODE_FMT_DEV:
0641         if (iip->ili_fields & XFS_ILOG_DEV) {
0642             ASSERT(whichfork == XFS_DATA_FORK);
0643             xfs_dinode_put_rdev(dip,
0644                     linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
0645         }
0646         break;
0647 
0648     default:
0649         ASSERT(0);
0650         break;
0651     }
0652 }
0653 
0654 /* Convert bmap state flags to an inode fork. */
0655 struct xfs_ifork *
0656 xfs_iext_state_to_fork(
0657     struct xfs_inode    *ip,
0658     int         state)
0659 {
0660     if (state & BMAP_COWFORK)
0661         return ip->i_cowfp;
0662     else if (state & BMAP_ATTRFORK)
0663         return &ip->i_af;
0664     return &ip->i_df;
0665 }
0666 
0667 /*
0668  * Initialize an inode's copy-on-write fork.
0669  */
0670 void
0671 xfs_ifork_init_cow(
0672     struct xfs_inode    *ip)
0673 {
0674     if (ip->i_cowfp)
0675         return;
0676 
0677     ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
0678                        GFP_NOFS | __GFP_NOFAIL);
0679     ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
0680 }
0681 
0682 /* Verify the inline contents of the data fork of an inode. */
0683 int
0684 xfs_ifork_verify_local_data(
0685     struct xfs_inode    *ip)
0686 {
0687     xfs_failaddr_t      fa = NULL;
0688 
0689     switch (VFS_I(ip)->i_mode & S_IFMT) {
0690     case S_IFDIR:
0691         fa = xfs_dir2_sf_verify(ip);
0692         break;
0693     case S_IFLNK:
0694         fa = xfs_symlink_shortform_verify(ip);
0695         break;
0696     default:
0697         break;
0698     }
0699 
0700     if (fa) {
0701         xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
0702                 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
0703         return -EFSCORRUPTED;
0704     }
0705 
0706     return 0;
0707 }
0708 
0709 /* Verify the inline contents of the attr fork of an inode. */
0710 int
0711 xfs_ifork_verify_local_attr(
0712     struct xfs_inode    *ip)
0713 {
0714     struct xfs_ifork    *ifp = &ip->i_af;
0715     xfs_failaddr_t      fa;
0716 
0717     if (!xfs_inode_has_attr_fork(ip))
0718         fa = __this_address;
0719     else
0720         fa = xfs_attr_shortform_verify(ip);
0721 
0722     if (fa) {
0723         xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
0724                 ifp->if_u1.if_data, ifp->if_bytes, fa);
0725         return -EFSCORRUPTED;
0726     }
0727 
0728     return 0;
0729 }
0730 
0731 int
0732 xfs_iext_count_may_overflow(
0733     struct xfs_inode    *ip,
0734     int         whichfork,
0735     int         nr_to_add)
0736 {
0737     struct xfs_ifork    *ifp = xfs_ifork_ptr(ip, whichfork);
0738     uint64_t        max_exts;
0739     uint64_t        nr_exts;
0740 
0741     if (whichfork == XFS_COW_FORK)
0742         return 0;
0743 
0744     max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
0745                 whichfork);
0746 
0747     if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
0748         max_exts = 10;
0749 
0750     nr_exts = ifp->if_nextents + nr_to_add;
0751     if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
0752         return -EFBIG;
0753 
0754     return 0;
0755 }
0756 
0757 /*
0758  * Upgrade this inode's extent counter fields to be able to handle a potential
0759  * increase in the extent count by nr_to_add.  Normally this is the same
0760  * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
0761  */
0762 int
0763 xfs_iext_count_upgrade(
0764     struct xfs_trans    *tp,
0765     struct xfs_inode    *ip,
0766     uint            nr_to_add)
0767 {
0768     ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
0769 
0770     if (!xfs_has_large_extent_counts(ip->i_mount) ||
0771         xfs_inode_has_large_extent_counts(ip) ||
0772         XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
0773         return -EFBIG;
0774 
0775     ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
0776     xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0777 
0778     return 0;
0779 }