linux/fs/xfs/libxfs/xfs_inode_fork.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
   3 * All Rights Reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or
   6 * modify it under the terms of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it would be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write the Free Software Foundation,
  16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17 */
  18#include <linux/log2.h>
  19
  20#include "xfs.h"
  21#include "xfs_fs.h"
  22#include "xfs_format.h"
  23#include "xfs_log_format.h"
  24#include "xfs_trans_resv.h"
  25#include "xfs_mount.h"
  26#include "xfs_inode.h"
  27#include "xfs_trans.h"
  28#include "xfs_inode_item.h"
  29#include "xfs_btree.h"
  30#include "xfs_bmap_btree.h"
  31#include "xfs_bmap.h"
  32#include "xfs_error.h"
  33#include "xfs_trace.h"
  34#include "xfs_attr_sf.h"
  35#include "xfs_da_format.h"
  36#include "xfs_da_btree.h"
  37#include "xfs_dir2_priv.h"
  38
  39kmem_zone_t *xfs_ifork_zone;
  40
  41STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  42STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  43STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  44
  45/*
  46 * Move inode type and inode format specific information from the
  47 * on-disk inode to the in-core inode.  For fifos, devs, and sockets
  48 * this means set if_rdev to the proper value.  For files, directories,
  49 * and symlinks this means to bring in the in-line data or extent
  50 * pointers.  For a file in B-tree format, only the root is immediately
  51 * brought in-core.  The rest will be in-lined in if_extents when it
  52 * is first referenced (see xfs_iread_extents()).
  53 */
  54int
  55xfs_iformat_fork(
  56        xfs_inode_t             *ip,
  57        xfs_dinode_t            *dip)
  58{
  59        xfs_attr_shortform_t    *atp;
  60        int                     size;
  61        int                     error = 0;
  62        xfs_fsize_t             di_size;
  63
  64        if (unlikely(be32_to_cpu(dip->di_nextents) +
  65                     be16_to_cpu(dip->di_anextents) >
  66                     be64_to_cpu(dip->di_nblocks))) {
  67                xfs_warn(ip->i_mount,
  68                        "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  69                        (unsigned long long)ip->i_ino,
  70                        (int)(be32_to_cpu(dip->di_nextents) +
  71                              be16_to_cpu(dip->di_anextents)),
  72                        (unsigned long long)
  73                                be64_to_cpu(dip->di_nblocks));
  74                XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  75                                     ip->i_mount, dip);
  76                return -EFSCORRUPTED;
  77        }
  78
  79        if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
  80                xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
  81                        (unsigned long long)ip->i_ino,
  82                        dip->di_forkoff);
  83                XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  84                                     ip->i_mount, dip);
  85                return -EFSCORRUPTED;
  86        }
  87
  88        if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
  89                     !ip->i_mount->m_rtdev_targp)) {
  90                xfs_warn(ip->i_mount,
  91                        "corrupt dinode %Lu, has realtime flag set.",
  92                        ip->i_ino);
  93                XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
  94                                     XFS_ERRLEVEL_LOW, ip->i_mount, dip);
  95                return -EFSCORRUPTED;
  96        }
  97
  98        if (unlikely(xfs_is_reflink_inode(ip) &&
  99            (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
 100                xfs_warn(ip->i_mount,
 101                        "corrupt dinode %llu, wrong file type for reflink.",
 102                        ip->i_ino);
 103                XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
 104                                     XFS_ERRLEVEL_LOW, ip->i_mount, dip);
 105                return -EFSCORRUPTED;
 106        }
 107
 108        if (unlikely(xfs_is_reflink_inode(ip) &&
 109            (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
 110                xfs_warn(ip->i_mount,
 111                        "corrupt dinode %llu, has reflink+realtime flag set.",
 112                        ip->i_ino);
 113                XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
 114                                     XFS_ERRLEVEL_LOW, ip->i_mount, dip);
 115                return -EFSCORRUPTED;
 116        }
 117
 118        switch (VFS_I(ip)->i_mode & S_IFMT) {
 119        case S_IFIFO:
 120        case S_IFCHR:
 121        case S_IFBLK:
 122        case S_IFSOCK:
 123                if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
 124                        XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
 125                                              ip->i_mount, dip);
 126                        return -EFSCORRUPTED;
 127                }
 128                ip->i_d.di_size = 0;
 129                ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
 130                break;
 131
 132        case S_IFREG:
 133        case S_IFLNK:
 134        case S_IFDIR:
 135                switch (dip->di_format) {
 136                case XFS_DINODE_FMT_LOCAL:
 137                        /*
 138                         * no local regular files yet
 139                         */
 140                        if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
 141                                xfs_warn(ip->i_mount,
 142                        "corrupt inode %Lu (local format for regular file).",
 143                                        (unsigned long long) ip->i_ino);
 144                                XFS_CORRUPTION_ERROR("xfs_iformat(4)",
 145                                                     XFS_ERRLEVEL_LOW,
 146                                                     ip->i_mount, dip);
 147                                return -EFSCORRUPTED;
 148                        }
 149
 150                        di_size = be64_to_cpu(dip->di_size);
 151                        if (unlikely(di_size < 0 ||
 152                                     di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
 153                                xfs_warn(ip->i_mount,
 154                        "corrupt inode %Lu (bad size %Ld for local inode).",
 155                                        (unsigned long long) ip->i_ino,
 156                                        (long long) di_size);
 157                                XFS_CORRUPTION_ERROR("xfs_iformat(5)",
 158                                                     XFS_ERRLEVEL_LOW,
 159                                                     ip->i_mount, dip);
 160                                return -EFSCORRUPTED;
 161                        }
 162
 163                        size = (int)di_size;
 164                        error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
 165                        break;
 166                case XFS_DINODE_FMT_EXTENTS:
 167                        error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
 168                        break;
 169                case XFS_DINODE_FMT_BTREE:
 170                        error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
 171                        break;
 172                default:
 173                        XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
 174                                         ip->i_mount);
 175                        return -EFSCORRUPTED;
 176                }
 177                break;
 178
 179        default:
 180                XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
 181                return -EFSCORRUPTED;
 182        }
 183        if (error)
 184                return error;
 185
 186        /* Check inline dir contents. */
 187        if (S_ISDIR(VFS_I(ip)->i_mode) &&
 188            dip->di_format == XFS_DINODE_FMT_LOCAL) {
 189                error = xfs_dir2_sf_verify(ip);
 190                if (error) {
 191                        xfs_idestroy_fork(ip, XFS_DATA_FORK);
 192                        return error;
 193                }
 194        }
 195
 196        if (xfs_is_reflink_inode(ip)) {
 197                ASSERT(ip->i_cowfp == NULL);
 198                xfs_ifork_init_cow(ip);
 199        }
 200
 201        if (!XFS_DFORK_Q(dip))
 202                return 0;
 203
 204        ASSERT(ip->i_afp == NULL);
 205        ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
 206
 207        switch (dip->di_aformat) {
 208        case XFS_DINODE_FMT_LOCAL:
 209                atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
 210                size = be16_to_cpu(atp->hdr.totsize);
 211
 212                if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
 213                        xfs_warn(ip->i_mount,
 214                                "corrupt inode %Lu (bad attr fork size %Ld).",
 215                                (unsigned long long) ip->i_ino,
 216                                (long long) size);
 217                        XFS_CORRUPTION_ERROR("xfs_iformat(8)",
 218                                             XFS_ERRLEVEL_LOW,
 219                                             ip->i_mount, dip);
 220                        error = -EFSCORRUPTED;
 221                        break;
 222                }
 223
 224                error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
 225                break;
 226        case XFS_DINODE_FMT_EXTENTS:
 227                error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
 228                break;
 229        case XFS_DINODE_FMT_BTREE:
 230                error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
 231                break;
 232        default:
 233                error = -EFSCORRUPTED;
 234                break;
 235        }
 236        if (error) {
 237                kmem_zone_free(xfs_ifork_zone, ip->i_afp);
 238                ip->i_afp = NULL;
 239                if (ip->i_cowfp)
 240                        kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
 241                ip->i_cowfp = NULL;
 242                xfs_idestroy_fork(ip, XFS_DATA_FORK);
 243        }
 244        return error;
 245}
 246
 247void
 248xfs_init_local_fork(
 249        struct xfs_inode        *ip,
 250        int                     whichfork,
 251        const void              *data,
 252        int                     size)
 253{
 254        struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
 255        int                     mem_size = size, real_size = 0;
 256        bool                    zero_terminate;
 257
 258        /*
 259         * If we are using the local fork to store a symlink body we need to
 260         * zero-terminate it so that we can pass it back to the VFS directly.
 261         * Overallocate the in-memory fork by one for that and add a zero
 262         * to terminate it below.
 263         */
 264        zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
 265        if (zero_terminate)
 266                mem_size++;
 267
 268        if (size == 0)
 269                ifp->if_u1.if_data = NULL;
 270        else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
 271                ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
 272        else {
 273                real_size = roundup(mem_size, 4);
 274                ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
 275        }
 276
 277        if (size) {
 278                memcpy(ifp->if_u1.if_data, data, size);
 279                if (zero_terminate)
 280                        ifp->if_u1.if_data[size] = '\0';
 281        }
 282
 283        ifp->if_bytes = size;
 284        ifp->if_real_bytes = real_size;
 285        ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
 286        ifp->if_flags |= XFS_IFINLINE;
 287}
 288
 289/*
 290 * The file is in-lined in the on-disk inode.
 291 * If it fits into if_inline_data, then copy
 292 * it there, otherwise allocate a buffer for it
 293 * and copy the data there.  Either way, set
 294 * if_data to point at the data.
 295 * If we allocate a buffer for the data, make
 296 * sure that its size is a multiple of 4 and
 297 * record the real size in i_real_bytes.
 298 */
 299STATIC int
 300xfs_iformat_local(
 301        xfs_inode_t     *ip,
 302        xfs_dinode_t    *dip,
 303        int             whichfork,
 304        int             size)
 305{
 306        /*
 307         * If the size is unreasonable, then something
 308         * is wrong and we just bail out rather than crash in
 309         * kmem_alloc() or memcpy() below.
 310         */
 311        if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
 312                xfs_warn(ip->i_mount,
 313        "corrupt inode %Lu (bad size %d for local fork, size = %d).",
 314                        (unsigned long long) ip->i_ino, size,
 315                        XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
 316                XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
 317                                     ip->i_mount, dip);
 318                return -EFSCORRUPTED;
 319        }
 320
 321        xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
 322        return 0;
 323}
 324
 325/*
 326 * The file consists of a set of extents all of which fit into the on-disk
 327 * inode.  If there are few enough extents to fit into the if_inline_ext, then
 328 * copy them there.  Otherwise allocate a buffer for them and copy them into it.
 329 * Either way, set if_extents to point at the extents.
 330 */
 331STATIC int
 332xfs_iformat_extents(
 333        struct xfs_inode        *ip,
 334        struct xfs_dinode       *dip,
 335        int                     whichfork)
 336{
 337        struct xfs_mount        *mp = ip->i_mount;
 338        struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
 339        int                     nex = XFS_DFORK_NEXTENTS(dip, whichfork);
 340        int                     size = nex * sizeof(xfs_bmbt_rec_t);
 341        struct xfs_bmbt_rec     *dp;
 342        int                     i;
 343
 344        /*
 345         * If the number of extents is unreasonable, then something is wrong and
 346         * we just bail out rather than crash in kmem_alloc() or memcpy() below.
 347         */
 348        if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
 349                xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
 350                        (unsigned long long) ip->i_ino, nex);
 351                XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
 352                                     mp, dip);
 353                return -EFSCORRUPTED;
 354        }
 355
 356        ifp->if_real_bytes = 0;
 357        if (nex == 0)
 358                ifp->if_u1.if_extents = NULL;
 359        else if (nex <= XFS_INLINE_EXTS)
 360                ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
 361        else
 362                xfs_iext_add(ifp, 0, nex);
 363
 364        ifp->if_bytes = size;
 365        if (size) {
 366                dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
 367                for (i = 0; i < nex; i++, dp++) {
 368                        xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
 369                        ep->l0 = get_unaligned_be64(&dp->l0);
 370                        ep->l1 = get_unaligned_be64(&dp->l1);
 371                        if (!xfs_bmbt_validate_extent(mp, whichfork, ep)) {
 372                                XFS_ERROR_REPORT("xfs_iformat_extents(2)",
 373                                                 XFS_ERRLEVEL_LOW, mp);
 374                                return -EFSCORRUPTED;
 375                        }
 376                }
 377                XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
 378        }
 379        ifp->if_flags |= XFS_IFEXTENTS;
 380        return 0;
 381}
 382
 383/*
 384 * The file has too many extents to fit into
 385 * the inode, so they are in B-tree format.
 386 * Allocate a buffer for the root of the B-tree
 387 * and copy the root into it.  The i_extents
 388 * field will remain NULL until all of the
 389 * extents are read in (when they are needed).
 390 */
 391STATIC int
 392xfs_iformat_btree(
 393        xfs_inode_t             *ip,
 394        xfs_dinode_t            *dip,
 395        int                     whichfork)
 396{
 397        struct xfs_mount        *mp = ip->i_mount;
 398        xfs_bmdr_block_t        *dfp;
 399        xfs_ifork_t             *ifp;
 400        /* REFERENCED */
 401        int                     nrecs;
 402        int                     size;
 403        int                     level;
 404
 405        ifp = XFS_IFORK_PTR(ip, whichfork);
 406        dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
 407        size = XFS_BMAP_BROOT_SPACE(mp, dfp);
 408        nrecs = be16_to_cpu(dfp->bb_numrecs);
 409        level = be16_to_cpu(dfp->bb_level);
 410
 411        /*
 412         * blow out if -- fork has less extents than can fit in
 413         * fork (fork shouldn't be a btree format), root btree
 414         * block has more records than can fit into the fork,
 415         * or the number of extents is greater than the number of
 416         * blocks.
 417         */
 418        if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
 419                                        XFS_IFORK_MAXEXT(ip, whichfork) ||
 420                     XFS_BMDR_SPACE_CALC(nrecs) >
 421                                        XFS_DFORK_SIZE(dip, mp, whichfork) ||
 422                     XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
 423                     level == 0 || level > XFS_BTREE_MAXLEVELS) {
 424                xfs_warn(mp, "corrupt inode %Lu (btree).",
 425                                        (unsigned long long) ip->i_ino);
 426                XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
 427                                         mp, dip);
 428                return -EFSCORRUPTED;
 429        }
 430
 431        ifp->if_broot_bytes = size;
 432        ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
 433        ASSERT(ifp->if_broot != NULL);
 434        /*
 435         * Copy and convert from the on-disk structure
 436         * to the in-memory structure.
 437         */
 438        xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
 439                         ifp->if_broot, size);
 440        ifp->if_flags &= ~XFS_IFEXTENTS;
 441        ifp->if_flags |= XFS_IFBROOT;
 442
 443        return 0;
 444}
 445
 446/*
 447 * Read in extents from a btree-format inode.
 448 * Allocate and fill in if_extents.  Real work is done in xfs_bmap.c.
 449 */
 450int
 451xfs_iread_extents(
 452        xfs_trans_t     *tp,
 453        xfs_inode_t     *ip,
 454        int             whichfork)
 455{
 456        int             error;
 457        xfs_ifork_t     *ifp;
 458        xfs_extnum_t    nextents;
 459
 460        ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
 461
 462        if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
 463                XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
 464                                 ip->i_mount);
 465                return -EFSCORRUPTED;
 466        }
 467        nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
 468        ifp = XFS_IFORK_PTR(ip, whichfork);
 469
 470        /*
 471         * We know that the size is valid (it's checked in iformat_btree)
 472         */
 473        ifp->if_bytes = ifp->if_real_bytes = 0;
 474        xfs_iext_add(ifp, 0, nextents);
 475        error = xfs_bmap_read_extents(tp, ip, whichfork);
 476        if (error) {
 477                xfs_iext_destroy(ifp);
 478                return error;
 479        }
 480        ifp->if_flags |= XFS_IFEXTENTS;
 481        return 0;
 482}
 483/*
 484 * Reallocate the space for if_broot based on the number of records
 485 * being added or deleted as indicated in rec_diff.  Move the records
 486 * and pointers in if_broot to fit the new size.  When shrinking this
 487 * will eliminate holes between the records and pointers created by
 488 * the caller.  When growing this will create holes to be filled in
 489 * by the caller.
 490 *
 491 * The caller must not request to add more records than would fit in
 492 * the on-disk inode root.  If the if_broot is currently NULL, then
 493 * if we are adding records, one will be allocated.  The caller must also
 494 * not request that the number of records go below zero, although
 495 * it can go to zero.
 496 *
 497 * ip -- the inode whose if_broot area is changing
 498 * ext_diff -- the change in the number of records, positive or negative,
 499 *       requested for the if_broot array.
 500 */
 501void
 502xfs_iroot_realloc(
 503        xfs_inode_t             *ip,
 504        int                     rec_diff,
 505        int                     whichfork)
 506{
 507        struct xfs_mount        *mp = ip->i_mount;
 508        int                     cur_max;
 509        xfs_ifork_t             *ifp;
 510        struct xfs_btree_block  *new_broot;
 511        int                     new_max;
 512        size_t                  new_size;
 513        char                    *np;
 514        char                    *op;
 515
 516        /*
 517         * Handle the degenerate case quietly.
 518         */
 519        if (rec_diff == 0) {
 520                return;
 521        }
 522
 523        ifp = XFS_IFORK_PTR(ip, whichfork);
 524        if (rec_diff > 0) {
 525                /*
 526                 * If there wasn't any memory allocated before, just
 527                 * allocate it now and get out.
 528                 */
 529                if (ifp->if_broot_bytes == 0) {
 530                        new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
 531                        ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
 532                        ifp->if_broot_bytes = (int)new_size;
 533                        return;
 534                }
 535
 536                /*
 537                 * If there is already an existing if_broot, then we need
 538                 * to realloc() it and shift the pointers to their new
 539                 * location.  The records don't change location because
 540                 * they are kept butted up against the btree block header.
 541                 */
 542                cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
 543                new_max = cur_max + rec_diff;
 544                new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
 545                ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
 546                                KM_SLEEP | KM_NOFS);
 547                op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
 548                                                     ifp->if_broot_bytes);
 549                np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
 550                                                     (int)new_size);
 551                ifp->if_broot_bytes = (int)new_size;
 552                ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
 553                        XFS_IFORK_SIZE(ip, whichfork));
 554                memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
 555                return;
 556        }
 557
 558        /*
 559         * rec_diff is less than 0.  In this case, we are shrinking the
 560         * if_broot buffer.  It must already exist.  If we go to zero
 561         * records, just get rid of the root and clear the status bit.
 562         */
 563        ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
 564        cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
 565        new_max = cur_max + rec_diff;
 566        ASSERT(new_max >= 0);
 567        if (new_max > 0)
 568                new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
 569        else
 570                new_size = 0;
 571        if (new_size > 0) {
 572                new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
 573                /*
 574                 * First copy over the btree block header.
 575                 */
 576                memcpy(new_broot, ifp->if_broot,
 577                        XFS_BMBT_BLOCK_LEN(ip->i_mount));
 578        } else {
 579                new_broot = NULL;
 580                ifp->if_flags &= ~XFS_IFBROOT;
 581        }
 582
 583        /*
 584         * Only copy the records and pointers if there are any.
 585         */
 586        if (new_max > 0) {
 587                /*
 588                 * First copy the records.
 589                 */
 590                op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
 591                np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
 592                memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
 593
 594                /*
 595                 * Then copy the pointers.
 596                 */
 597                op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
 598                                                     ifp->if_broot_bytes);
 599                np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
 600                                                     (int)new_size);
 601                memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
 602        }
 603        kmem_free(ifp->if_broot);
 604        ifp->if_broot = new_broot;
 605        ifp->if_broot_bytes = (int)new_size;
 606        if (ifp->if_broot)
 607                ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
 608                        XFS_IFORK_SIZE(ip, whichfork));
 609        return;
 610}
 611
 612
 613/*
 614 * This is called when the amount of space needed for if_data
 615 * is increased or decreased.  The change in size is indicated by
 616 * the number of bytes that need to be added or deleted in the
 617 * byte_diff parameter.
 618 *
 619 * If the amount of space needed has decreased below the size of the
 620 * inline buffer, then switch to using the inline buffer.  Otherwise,
 621 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
 622 * to what is needed.
 623 *
 624 * ip -- the inode whose if_data area is changing
 625 * byte_diff -- the change in the number of bytes, positive or negative,
 626 *       requested for the if_data array.
 627 */
 628void
 629xfs_idata_realloc(
 630        xfs_inode_t     *ip,
 631        int             byte_diff,
 632        int             whichfork)
 633{
 634        xfs_ifork_t     *ifp;
 635        int             new_size;
 636        int             real_size;
 637
 638        if (byte_diff == 0) {
 639                return;
 640        }
 641
 642        ifp = XFS_IFORK_PTR(ip, whichfork);
 643        new_size = (int)ifp->if_bytes + byte_diff;
 644        ASSERT(new_size >= 0);
 645
 646        if (new_size == 0) {
 647                if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
 648                        kmem_free(ifp->if_u1.if_data);
 649                }
 650                ifp->if_u1.if_data = NULL;
 651                real_size = 0;
 652        } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
 653                /*
 654                 * If the valid extents/data can fit in if_inline_ext/data,
 655                 * copy them from the malloc'd vector and free it.
 656                 */
 657                if (ifp->if_u1.if_data == NULL) {
 658                        ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
 659                } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
 660                        ASSERT(ifp->if_real_bytes != 0);
 661                        memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
 662                              new_size);
 663                        kmem_free(ifp->if_u1.if_data);
 664                        ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
 665                }
 666                real_size = 0;
 667        } else {
 668                /*
 669                 * Stuck with malloc/realloc.
 670                 * For inline data, the underlying buffer must be
 671                 * a multiple of 4 bytes in size so that it can be
 672                 * logged and stay on word boundaries.  We enforce
 673                 * that here.
 674                 */
 675                real_size = roundup(new_size, 4);
 676                if (ifp->if_u1.if_data == NULL) {
 677                        ASSERT(ifp->if_real_bytes == 0);
 678                        ifp->if_u1.if_data = kmem_alloc(real_size,
 679                                                        KM_SLEEP | KM_NOFS);
 680                } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
 681                        /*
 682                         * Only do the realloc if the underlying size
 683                         * is really changing.
 684                         */
 685                        if (ifp->if_real_bytes != real_size) {
 686                                ifp->if_u1.if_data =
 687                                        kmem_realloc(ifp->if_u1.if_data,
 688                                                        real_size,
 689                                                        KM_SLEEP | KM_NOFS);
 690                        }
 691                } else {
 692                        ASSERT(ifp->if_real_bytes == 0);
 693                        ifp->if_u1.if_data = kmem_alloc(real_size,
 694                                                        KM_SLEEP | KM_NOFS);
 695                        memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
 696                                ifp->if_bytes);
 697                }
 698        }
 699        ifp->if_real_bytes = real_size;
 700        ifp->if_bytes = new_size;
 701        ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
 702}
 703
 704void
 705xfs_idestroy_fork(
 706        xfs_inode_t     *ip,
 707        int             whichfork)
 708{
 709        xfs_ifork_t     *ifp;
 710
 711        ifp = XFS_IFORK_PTR(ip, whichfork);
 712        if (ifp->if_broot != NULL) {
 713                kmem_free(ifp->if_broot);
 714                ifp->if_broot = NULL;
 715        }
 716
 717        /*
 718         * If the format is local, then we can't have an extents
 719         * array so just look for an inline data array.  If we're
 720         * not local then we may or may not have an extents list,
 721         * so check and free it up if we do.
 722         */
 723        if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
 724                if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
 725                    (ifp->if_u1.if_data != NULL)) {
 726                        ASSERT(ifp->if_real_bytes != 0);
 727                        kmem_free(ifp->if_u1.if_data);
 728                        ifp->if_u1.if_data = NULL;
 729                        ifp->if_real_bytes = 0;
 730                }
 731        } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
 732                   ((ifp->if_flags & XFS_IFEXTIREC) ||
 733                    ((ifp->if_u1.if_extents != NULL) &&
 734                     (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
 735                ASSERT(ifp->if_real_bytes != 0);
 736                xfs_iext_destroy(ifp);
 737        }
 738        ASSERT(ifp->if_u1.if_extents == NULL ||
 739               ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
 740        ASSERT(ifp->if_real_bytes == 0);
 741        if (whichfork == XFS_ATTR_FORK) {
 742                kmem_zone_free(xfs_ifork_zone, ip->i_afp);
 743                ip->i_afp = NULL;
 744        } else if (whichfork == XFS_COW_FORK) {
 745                kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
 746                ip->i_cowfp = NULL;
 747        }
 748}
 749
 750/* Count number of incore extents based on if_bytes */
 751xfs_extnum_t
 752xfs_iext_count(struct xfs_ifork *ifp)
 753{
 754        return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
 755}
 756
 757/*
 758 * Convert in-core extents to on-disk form
 759 *
 760 * For either the data or attr fork in extent format, we need to endian convert
 761 * the in-core extent as we place them into the on-disk inode.
 762 *
 763 * In the case of the data fork, the in-core and on-disk fork sizes can be
 764 * different due to delayed allocation extents. We only copy on-disk extents
 765 * here, so callers must always use the physical fork size to determine the
 766 * size of the buffer passed to this routine.  We will return the size actually
 767 * used.
 768 */
 769int
 770xfs_iextents_copy(
 771        xfs_inode_t             *ip,
 772        xfs_bmbt_rec_t          *dp,
 773        int                     whichfork)
 774{
 775        int                     copied;
 776        int                     i;
 777        xfs_ifork_t             *ifp;
 778        int                     nrecs;
 779        xfs_fsblock_t           start_block;
 780
 781        ifp = XFS_IFORK_PTR(ip, whichfork);
 782        ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
 783        ASSERT(ifp->if_bytes > 0);
 784
 785        nrecs = xfs_iext_count(ifp);
 786        XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
 787        ASSERT(nrecs > 0);
 788
 789        /*
 790         * There are some delayed allocation extents in the
 791         * inode, so copy the extents one at a time and skip
 792         * the delayed ones.  There must be at least one
 793         * non-delayed extent.
 794         */
 795        copied = 0;
 796        for (i = 0; i < nrecs; i++) {
 797                xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
 798
 799                ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, ep));
 800
 801                start_block = xfs_bmbt_get_startblock(ep);
 802                if (isnullstartblock(start_block)) {
 803                        /*
 804                         * It's a delayed allocation extent, so skip it.
 805                         */
 806                        continue;
 807                }
 808
 809                /* Translate to on disk format */
 810                put_unaligned_be64(ep->l0, &dp->l0);
 811                put_unaligned_be64(ep->l1, &dp->l1);
 812                dp++;
 813                copied++;
 814        }
 815        ASSERT(copied != 0);
 816
 817        return (copied * (uint)sizeof(xfs_bmbt_rec_t));
 818}
 819
 820/*
 821 * Each of the following cases stores data into the same region
 822 * of the on-disk inode, so only one of them can be valid at
 823 * any given time. While it is possible to have conflicting formats
 824 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
 825 * in EXTENTS format, this can only happen when the fork has
 826 * changed formats after being modified but before being flushed.
 827 * In these cases, the format always takes precedence, because the
 828 * format indicates the current state of the fork.
 829 */
 830void
 831xfs_iflush_fork(
 832        xfs_inode_t             *ip,
 833        xfs_dinode_t            *dip,
 834        xfs_inode_log_item_t    *iip,
 835        int                     whichfork)
 836{
 837        char                    *cp;
 838        xfs_ifork_t             *ifp;
 839        xfs_mount_t             *mp;
 840        static const short      brootflag[2] =
 841                { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
 842        static const short      dataflag[2] =
 843                { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
 844        static const short      extflag[2] =
 845                { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
 846
 847        if (!iip)
 848                return;
 849        ifp = XFS_IFORK_PTR(ip, whichfork);
 850        /*
 851         * This can happen if we gave up in iformat in an error path,
 852         * for the attribute fork.
 853         */
 854        if (!ifp) {
 855                ASSERT(whichfork == XFS_ATTR_FORK);
 856                return;
 857        }
 858        cp = XFS_DFORK_PTR(dip, whichfork);
 859        mp = ip->i_mount;
 860        switch (XFS_IFORK_FORMAT(ip, whichfork)) {
 861        case XFS_DINODE_FMT_LOCAL:
 862                if ((iip->ili_fields & dataflag[whichfork]) &&
 863                    (ifp->if_bytes > 0)) {
 864                        ASSERT(ifp->if_u1.if_data != NULL);
 865                        ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
 866                        memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
 867                }
 868                break;
 869
 870        case XFS_DINODE_FMT_EXTENTS:
 871                ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
 872                       !(iip->ili_fields & extflag[whichfork]));
 873                if ((iip->ili_fields & extflag[whichfork]) &&
 874                    (ifp->if_bytes > 0)) {
 875                        ASSERT(xfs_iext_get_ext(ifp, 0));
 876                        ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
 877                        (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
 878                                whichfork);
 879                }
 880                break;
 881
 882        case XFS_DINODE_FMT_BTREE:
 883                if ((iip->ili_fields & brootflag[whichfork]) &&
 884                    (ifp->if_broot_bytes > 0)) {
 885                        ASSERT(ifp->if_broot != NULL);
 886                        ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
 887                                XFS_IFORK_SIZE(ip, whichfork));
 888                        xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
 889                                (xfs_bmdr_block_t *)cp,
 890                                XFS_DFORK_SIZE(dip, mp, whichfork));
 891                }
 892                break;
 893
 894        case XFS_DINODE_FMT_DEV:
 895                if (iip->ili_fields & XFS_ILOG_DEV) {
 896                        ASSERT(whichfork == XFS_DATA_FORK);
 897                        xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
 898                }
 899                break;
 900
 901        case XFS_DINODE_FMT_UUID:
 902                if (iip->ili_fields & XFS_ILOG_UUID) {
 903                        ASSERT(whichfork == XFS_DATA_FORK);
 904                        memcpy(XFS_DFORK_DPTR(dip),
 905                               &ip->i_df.if_u2.if_uuid,
 906                               sizeof(uuid_t));
 907                }
 908                break;
 909
 910        default:
 911                ASSERT(0);
 912                break;
 913        }
 914}
 915
 916/*
 917 * Return a pointer to the extent record at file index idx.
 918 */
 919xfs_bmbt_rec_host_t *
 920xfs_iext_get_ext(
 921        xfs_ifork_t     *ifp,           /* inode fork pointer */
 922        xfs_extnum_t    idx)            /* index of target extent */
 923{
 924        ASSERT(idx >= 0);
 925        ASSERT(idx < xfs_iext_count(ifp));
 926
 927        if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
 928                return ifp->if_u1.if_ext_irec->er_extbuf;
 929        } else if (ifp->if_flags & XFS_IFEXTIREC) {
 930                xfs_ext_irec_t  *erp;           /* irec pointer */
 931                int             erp_idx = 0;    /* irec index */
 932                xfs_extnum_t    page_idx = idx; /* ext index in target list */
 933
 934                erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
 935                return &erp->er_extbuf[page_idx];
 936        } else if (ifp->if_bytes) {
 937                return &ifp->if_u1.if_extents[idx];
 938        } else {
 939                return NULL;
 940        }
 941}
 942
 943/* Convert bmap state flags to an inode fork. */
 944struct xfs_ifork *
 945xfs_iext_state_to_fork(
 946        struct xfs_inode        *ip,
 947        int                     state)
 948{
 949        if (state & BMAP_COWFORK)
 950                return ip->i_cowfp;
 951        else if (state & BMAP_ATTRFORK)
 952                return ip->i_afp;
 953        return &ip->i_df;
 954}
 955
 956/*
 957 * Insert new item(s) into the extent records for incore inode
 958 * fork 'ifp'.  'count' new items are inserted at index 'idx'.
 959 */
 960void
 961xfs_iext_insert(
 962        xfs_inode_t     *ip,            /* incore inode pointer */
 963        xfs_extnum_t    idx,            /* starting index of new items */
 964        xfs_extnum_t    count,          /* number of inserted items */
 965        xfs_bmbt_irec_t *new,           /* items to insert */
 966        int             state)          /* type of extent conversion */
 967{
 968        xfs_ifork_t     *ifp = xfs_iext_state_to_fork(ip, state);
 969        xfs_extnum_t    i;              /* extent record index */
 970
 971        trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
 972
 973        ASSERT(ifp->if_flags & XFS_IFEXTENTS);
 974        xfs_iext_add(ifp, idx, count);
 975        for (i = idx; i < idx + count; i++, new++)
 976                xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
 977}
 978
 979/*
 980 * This is called when the amount of space required for incore file
 981 * extents needs to be increased. The ext_diff parameter stores the
 982 * number of new extents being added and the idx parameter contains
 983 * the extent index where the new extents will be added. If the new
 984 * extents are being appended, then we just need to (re)allocate and
 985 * initialize the space. Otherwise, if the new extents are being
 986 * inserted into the middle of the existing entries, a bit more work
 987 * is required to make room for the new extents to be inserted. The
 988 * caller is responsible for filling in the new extent entries upon
 989 * return.
 990 */
 991void
 992xfs_iext_add(
 993        xfs_ifork_t     *ifp,           /* inode fork pointer */
 994        xfs_extnum_t    idx,            /* index to begin adding exts */
 995        int             ext_diff)       /* number of extents to add */
 996{
 997        int             byte_diff;      /* new bytes being added */
 998        int             new_size;       /* size of extents after adding */
 999        xfs_extnum_t    nextents;       /* number of extents in file */
1000
1001        nextents = xfs_iext_count(ifp);
1002        ASSERT((idx >= 0) && (idx <= nextents));
1003        byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1004        new_size = ifp->if_bytes + byte_diff;
1005        /*
1006         * If the new number of extents (nextents + ext_diff)
1007         * fits inside the inode, then continue to use the inline
1008         * extent buffer.
1009         */
1010        if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1011                if (idx < nextents) {
1012                        memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1013                                &ifp->if_u2.if_inline_ext[idx],
1014                                (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1015                        memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1016                }
1017                ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1018                ifp->if_real_bytes = 0;
1019        }
1020        /*
1021         * Otherwise use a linear (direct) extent list.
1022         * If the extents are currently inside the inode,
1023         * xfs_iext_realloc_direct will switch us from
1024         * inline to direct extent allocation mode.
1025         */
1026        else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1027                xfs_iext_realloc_direct(ifp, new_size);
1028                if (idx < nextents) {
1029                        memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1030                                &ifp->if_u1.if_extents[idx],
1031                                (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1032                        memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1033                }
1034        }
1035        /* Indirection array */
1036        else {
1037                xfs_ext_irec_t  *erp;
1038                int             erp_idx = 0;
1039                int             page_idx = idx;
1040
1041                ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1042                if (ifp->if_flags & XFS_IFEXTIREC) {
1043                        erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1044                } else {
1045                        xfs_iext_irec_init(ifp);
1046                        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1047                        erp = ifp->if_u1.if_ext_irec;
1048                }
1049                /* Extents fit in target extent page */
1050                if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1051                        if (page_idx < erp->er_extcount) {
1052                                memmove(&erp->er_extbuf[page_idx + ext_diff],
1053                                        &erp->er_extbuf[page_idx],
1054                                        (erp->er_extcount - page_idx) *
1055                                        sizeof(xfs_bmbt_rec_t));
1056                                memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1057                        }
1058                        erp->er_extcount += ext_diff;
1059                        xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1060                }
1061                /* Insert a new extent page */
1062                else if (erp) {
1063                        xfs_iext_add_indirect_multi(ifp,
1064                                erp_idx, page_idx, ext_diff);
1065                }
1066                /*
1067                 * If extent(s) are being appended to the last page in
1068                 * the indirection array and the new extent(s) don't fit
1069                 * in the page, then erp is NULL and erp_idx is set to
1070                 * the next index needed in the indirection array.
1071                 */
1072                else {
1073                        uint    count = ext_diff;
1074
1075                        while (count) {
1076                                erp = xfs_iext_irec_new(ifp, erp_idx);
1077                                erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1078                                count -= erp->er_extcount;
1079                                if (count)
1080                                        erp_idx++;
1081                        }
1082                }
1083        }
1084        ifp->if_bytes = new_size;
1085}
1086
1087/*
1088 * This is called when incore extents are being added to the indirection
1089 * array and the new extents do not fit in the target extent list. The
1090 * erp_idx parameter contains the irec index for the target extent list
1091 * in the indirection array, and the idx parameter contains the extent
1092 * index within the list. The number of extents being added is stored
1093 * in the count parameter.
1094 *
1095 *    |-------|   |-------|
1096 *    |       |   |       |    idx - number of extents before idx
1097 *    |  idx  |   | count |
1098 *    |       |   |       |    count - number of extents being inserted at idx
1099 *    |-------|   |-------|
1100 *    | count |   | nex2  |    nex2 - number of extents after idx + count
1101 *    |-------|   |-------|
1102 */
1103void
1104xfs_iext_add_indirect_multi(
1105        xfs_ifork_t     *ifp,                   /* inode fork pointer */
1106        int             erp_idx,                /* target extent irec index */
1107        xfs_extnum_t    idx,                    /* index within target list */
1108        int             count)                  /* new extents being added */
1109{
1110        int             byte_diff;              /* new bytes being added */
1111        xfs_ext_irec_t  *erp;                   /* pointer to irec entry */
1112        xfs_extnum_t    ext_diff;               /* number of extents to add */
1113        xfs_extnum_t    ext_cnt;                /* new extents still needed */
1114        xfs_extnum_t    nex2;                   /* extents after idx + count */
1115        xfs_bmbt_rec_t  *nex2_ep = NULL;        /* temp list for nex2 extents */
1116        int             nlists;                 /* number of irec's (lists) */
1117
1118        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1119        erp = &ifp->if_u1.if_ext_irec[erp_idx];
1120        nex2 = erp->er_extcount - idx;
1121        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1122
1123        /*
1124         * Save second part of target extent list
1125         * (all extents past */
1126        if (nex2) {
1127                byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1128                nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1129                memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1130                erp->er_extcount -= nex2;
1131                xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1132                memset(&erp->er_extbuf[idx], 0, byte_diff);
1133        }
1134
1135        /*
1136         * Add the new extents to the end of the target
1137         * list, then allocate new irec record(s) and
1138         * extent buffer(s) as needed to store the rest
1139         * of the new extents.
1140         */
1141        ext_cnt = count;
1142        ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1143        if (ext_diff) {
1144                erp->er_extcount += ext_diff;
1145                xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1146                ext_cnt -= ext_diff;
1147        }
1148        while (ext_cnt) {
1149                erp_idx++;
1150                erp = xfs_iext_irec_new(ifp, erp_idx);
1151                ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1152                erp->er_extcount = ext_diff;
1153                xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1154                ext_cnt -= ext_diff;
1155        }
1156
1157        /* Add nex2 extents back to indirection array */
1158        if (nex2) {
1159                xfs_extnum_t    ext_avail;
1160                int             i;
1161
1162                byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1163                ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1164                i = 0;
1165                /*
1166                 * If nex2 extents fit in the current page, append
1167                 * nex2_ep after the new extents.
1168                 */
1169                if (nex2 <= ext_avail) {
1170                        i = erp->er_extcount;
1171                }
1172                /*
1173                 * Otherwise, check if space is available in the
1174                 * next page.
1175                 */
1176                else if ((erp_idx < nlists - 1) &&
1177                         (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1178                          ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1179                        erp_idx++;
1180                        erp++;
1181                        /* Create a hole for nex2 extents */
1182                        memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1183                                erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1184                }
1185                /*
1186                 * Final choice, create a new extent page for
1187                 * nex2 extents.
1188                 */
1189                else {
1190                        erp_idx++;
1191                        erp = xfs_iext_irec_new(ifp, erp_idx);
1192                }
1193                memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1194                kmem_free(nex2_ep);
1195                erp->er_extcount += nex2;
1196                xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1197        }
1198}
1199
1200/*
1201 * This is called when the amount of space required for incore file
1202 * extents needs to be decreased. The ext_diff parameter stores the
1203 * number of extents to be removed and the idx parameter contains
1204 * the extent index where the extents will be removed from.
1205 *
1206 * If the amount of space needed has decreased below the linear
1207 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1208 * extent array.  Otherwise, use kmem_realloc() to adjust the
1209 * size to what is needed.
1210 */
1211void
1212xfs_iext_remove(
1213        xfs_inode_t     *ip,            /* incore inode pointer */
1214        xfs_extnum_t    idx,            /* index to begin removing exts */
1215        int             ext_diff,       /* number of extents to remove */
1216        int             state)          /* type of extent conversion */
1217{
1218        xfs_ifork_t     *ifp = xfs_iext_state_to_fork(ip, state);
1219        xfs_extnum_t    nextents;       /* number of extents in file */
1220        int             new_size;       /* size of extents after removal */
1221
1222        trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1223
1224        ASSERT(ext_diff > 0);
1225        nextents = xfs_iext_count(ifp);
1226        new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1227
1228        if (new_size == 0) {
1229                xfs_iext_destroy(ifp);
1230        } else if (ifp->if_flags & XFS_IFEXTIREC) {
1231                xfs_iext_remove_indirect(ifp, idx, ext_diff);
1232        } else if (ifp->if_real_bytes) {
1233                xfs_iext_remove_direct(ifp, idx, ext_diff);
1234        } else {
1235                xfs_iext_remove_inline(ifp, idx, ext_diff);
1236        }
1237        ifp->if_bytes = new_size;
1238}
1239
1240/*
1241 * This removes ext_diff extents from the inline buffer, beginning
1242 * at extent index idx.
1243 */
1244void
1245xfs_iext_remove_inline(
1246        xfs_ifork_t     *ifp,           /* inode fork pointer */
1247        xfs_extnum_t    idx,            /* index to begin removing exts */
1248        int             ext_diff)       /* number of extents to remove */
1249{
1250        int             nextents;       /* number of extents in file */
1251
1252        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1253        ASSERT(idx < XFS_INLINE_EXTS);
1254        nextents = xfs_iext_count(ifp);
1255        ASSERT(((nextents - ext_diff) > 0) &&
1256                (nextents - ext_diff) < XFS_INLINE_EXTS);
1257
1258        if (idx + ext_diff < nextents) {
1259                memmove(&ifp->if_u2.if_inline_ext[idx],
1260                        &ifp->if_u2.if_inline_ext[idx + ext_diff],
1261                        (nextents - (idx + ext_diff)) *
1262                         sizeof(xfs_bmbt_rec_t));
1263                memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1264                        0, ext_diff * sizeof(xfs_bmbt_rec_t));
1265        } else {
1266                memset(&ifp->if_u2.if_inline_ext[idx], 0,
1267                        ext_diff * sizeof(xfs_bmbt_rec_t));
1268        }
1269}
1270
1271/*
1272 * This removes ext_diff extents from a linear (direct) extent list,
1273 * beginning at extent index idx. If the extents are being removed
1274 * from the end of the list (ie. truncate) then we just need to re-
1275 * allocate the list to remove the extra space. Otherwise, if the
1276 * extents are being removed from the middle of the existing extent
1277 * entries, then we first need to move the extent records beginning
1278 * at idx + ext_diff up in the list to overwrite the records being
1279 * removed, then remove the extra space via kmem_realloc.
1280 */
1281void
1282xfs_iext_remove_direct(
1283        xfs_ifork_t     *ifp,           /* inode fork pointer */
1284        xfs_extnum_t    idx,            /* index to begin removing exts */
1285        int             ext_diff)       /* number of extents to remove */
1286{
1287        xfs_extnum_t    nextents;       /* number of extents in file */
1288        int             new_size;       /* size of extents after removal */
1289
1290        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1291        new_size = ifp->if_bytes -
1292                (ext_diff * sizeof(xfs_bmbt_rec_t));
1293        nextents = xfs_iext_count(ifp);
1294
1295        if (new_size == 0) {
1296                xfs_iext_destroy(ifp);
1297                return;
1298        }
1299        /* Move extents up in the list (if needed) */
1300        if (idx + ext_diff < nextents) {
1301                memmove(&ifp->if_u1.if_extents[idx],
1302                        &ifp->if_u1.if_extents[idx + ext_diff],
1303                        (nextents - (idx + ext_diff)) *
1304                         sizeof(xfs_bmbt_rec_t));
1305        }
1306        memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1307                0, ext_diff * sizeof(xfs_bmbt_rec_t));
1308        /*
1309         * Reallocate the direct extent list. If the extents
1310         * will fit inside the inode then xfs_iext_realloc_direct
1311         * will switch from direct to inline extent allocation
1312         * mode for us.
1313         */
1314        xfs_iext_realloc_direct(ifp, new_size);
1315        ifp->if_bytes = new_size;
1316}
1317
1318/*
1319 * This is called when incore extents are being removed from the
1320 * indirection array and the extents being removed span multiple extent
1321 * buffers. The idx parameter contains the file extent index where we
1322 * want to begin removing extents, and the count parameter contains
1323 * how many extents need to be removed.
1324 *
1325 *    |-------|   |-------|
1326 *    | nex1  |   |       |    nex1 - number of extents before idx
1327 *    |-------|   | count |
1328 *    |       |   |       |    count - number of extents being removed at idx
1329 *    | count |   |-------|
1330 *    |       |   | nex2  |    nex2 - number of extents after idx + count
1331 *    |-------|   |-------|
1332 */
1333void
1334xfs_iext_remove_indirect(
1335        xfs_ifork_t     *ifp,           /* inode fork pointer */
1336        xfs_extnum_t    idx,            /* index to begin removing extents */
1337        int             count)          /* number of extents to remove */
1338{
1339        xfs_ext_irec_t  *erp;           /* indirection array pointer */
1340        int             erp_idx = 0;    /* indirection array index */
1341        xfs_extnum_t    ext_cnt;        /* extents left to remove */
1342        xfs_extnum_t    ext_diff;       /* extents to remove in current list */
1343        xfs_extnum_t    nex1;           /* number of extents before idx */
1344        xfs_extnum_t    nex2;           /* extents after idx + count */
1345        int             page_idx = idx; /* index in target extent list */
1346
1347        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1348        erp = xfs_iext_idx_to_irec(ifp,  &page_idx, &erp_idx, 0);
1349        ASSERT(erp != NULL);
1350        nex1 = page_idx;
1351        ext_cnt = count;
1352        while (ext_cnt) {
1353                nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1354                ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1355                /*
1356                 * Check for deletion of entire list;
1357                 * xfs_iext_irec_remove() updates extent offsets.
1358                 */
1359                if (ext_diff == erp->er_extcount) {
1360                        xfs_iext_irec_remove(ifp, erp_idx);
1361                        ext_cnt -= ext_diff;
1362                        nex1 = 0;
1363                        if (ext_cnt) {
1364                                ASSERT(erp_idx < ifp->if_real_bytes /
1365                                        XFS_IEXT_BUFSZ);
1366                                erp = &ifp->if_u1.if_ext_irec[erp_idx];
1367                                nex1 = 0;
1368                                continue;
1369                        } else {
1370                                break;
1371                        }
1372                }
1373                /* Move extents up (if needed) */
1374                if (nex2) {
1375                        memmove(&erp->er_extbuf[nex1],
1376                                &erp->er_extbuf[nex1 + ext_diff],
1377                                nex2 * sizeof(xfs_bmbt_rec_t));
1378                }
1379                /* Zero out rest of page */
1380                memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1381                        ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1382                /* Update remaining counters */
1383                erp->er_extcount -= ext_diff;
1384                xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1385                ext_cnt -= ext_diff;
1386                nex1 = 0;
1387                erp_idx++;
1388                erp++;
1389        }
1390        ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1391        xfs_iext_irec_compact(ifp);
1392}
1393
1394/*
1395 * Create, destroy, or resize a linear (direct) block of extents.
1396 */
1397void
1398xfs_iext_realloc_direct(
1399        xfs_ifork_t     *ifp,           /* inode fork pointer */
1400        int             new_size)       /* new size of extents after adding */
1401{
1402        int             rnew_size;      /* real new size of extents */
1403
1404        rnew_size = new_size;
1405
1406        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1407                ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1408                 (new_size != ifp->if_real_bytes)));
1409
1410        /* Free extent records */
1411        if (new_size == 0) {
1412                xfs_iext_destroy(ifp);
1413        }
1414        /* Resize direct extent list and zero any new bytes */
1415        else if (ifp->if_real_bytes) {
1416                /* Check if extents will fit inside the inode */
1417                if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1418                        xfs_iext_direct_to_inline(ifp, new_size /
1419                                (uint)sizeof(xfs_bmbt_rec_t));
1420                        ifp->if_bytes = new_size;
1421                        return;
1422                }
1423                if (!is_power_of_2(new_size)){
1424                        rnew_size = roundup_pow_of_two(new_size);
1425                }
1426                if (rnew_size != ifp->if_real_bytes) {
1427                        ifp->if_u1.if_extents =
1428                                kmem_realloc(ifp->if_u1.if_extents,
1429                                                rnew_size, KM_NOFS);
1430                }
1431                if (rnew_size > ifp->if_real_bytes) {
1432                        memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1433                                (uint)sizeof(xfs_bmbt_rec_t)], 0,
1434                                rnew_size - ifp->if_real_bytes);
1435                }
1436        }
1437        /* Switch from the inline extent buffer to a direct extent list */
1438        else {
1439                if (!is_power_of_2(new_size)) {
1440                        rnew_size = roundup_pow_of_two(new_size);
1441                }
1442                xfs_iext_inline_to_direct(ifp, rnew_size);
1443        }
1444        ifp->if_real_bytes = rnew_size;
1445        ifp->if_bytes = new_size;
1446}
1447
1448/*
1449 * Switch from linear (direct) extent records to inline buffer.
1450 */
1451void
1452xfs_iext_direct_to_inline(
1453        xfs_ifork_t     *ifp,           /* inode fork pointer */
1454        xfs_extnum_t    nextents)       /* number of extents in file */
1455{
1456        ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1457        ASSERT(nextents <= XFS_INLINE_EXTS);
1458        /*
1459         * The inline buffer was zeroed when we switched
1460         * from inline to direct extent allocation mode,
1461         * so we don't need to clear it here.
1462         */
1463        memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1464                nextents * sizeof(xfs_bmbt_rec_t));
1465        kmem_free(ifp->if_u1.if_extents);
1466        ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1467        ifp->if_real_bytes = 0;
1468}
1469
1470/*
1471 * Switch from inline buffer to linear (direct) extent records.
1472 * new_size should already be rounded up to the next power of 2
1473 * by the caller (when appropriate), so use new_size as it is.
1474 * However, since new_size may be rounded up, we can't update
1475 * if_bytes here. It is the caller's responsibility to update
1476 * if_bytes upon return.
1477 */
1478void
1479xfs_iext_inline_to_direct(
1480        xfs_ifork_t     *ifp,           /* inode fork pointer */
1481        int             new_size)       /* number of extents in file */
1482{
1483        ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1484        memset(ifp->if_u1.if_extents, 0, new_size);
1485        if (ifp->if_bytes) {
1486                memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1487                        ifp->if_bytes);
1488                memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1489                        sizeof(xfs_bmbt_rec_t));
1490        }
1491        ifp->if_real_bytes = new_size;
1492}
1493
1494/*
1495 * Resize an extent indirection array to new_size bytes.
1496 */
1497STATIC void
1498xfs_iext_realloc_indirect(
1499        xfs_ifork_t     *ifp,           /* inode fork pointer */
1500        int             new_size)       /* new indirection array size */
1501{
1502        int             nlists;         /* number of irec's (ex lists) */
1503        int             size;           /* current indirection array size */
1504
1505        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1506        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1507        size = nlists * sizeof(xfs_ext_irec_t);
1508        ASSERT(ifp->if_real_bytes);
1509        ASSERT((new_size >= 0) && (new_size != size));
1510        if (new_size == 0) {
1511                xfs_iext_destroy(ifp);
1512        } else {
1513                ifp->if_u1.if_ext_irec =
1514                        kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1515        }
1516}
1517
1518/*
1519 * Switch from indirection array to linear (direct) extent allocations.
1520 */
1521STATIC void
1522xfs_iext_indirect_to_direct(
1523         xfs_ifork_t    *ifp)           /* inode fork pointer */
1524{
1525        xfs_bmbt_rec_host_t *ep;        /* extent record pointer */
1526        xfs_extnum_t    nextents;       /* number of extents in file */
1527        int             size;           /* size of file extents */
1528
1529        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1530        nextents = xfs_iext_count(ifp);
1531        ASSERT(nextents <= XFS_LINEAR_EXTS);
1532        size = nextents * sizeof(xfs_bmbt_rec_t);
1533
1534        xfs_iext_irec_compact_pages(ifp);
1535        ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1536
1537        ep = ifp->if_u1.if_ext_irec->er_extbuf;
1538        kmem_free(ifp->if_u1.if_ext_irec);
1539        ifp->if_flags &= ~XFS_IFEXTIREC;
1540        ifp->if_u1.if_extents = ep;
1541        ifp->if_bytes = size;
1542        if (nextents < XFS_LINEAR_EXTS) {
1543                xfs_iext_realloc_direct(ifp, size);
1544        }
1545}
1546
1547/*
1548 * Remove all records from the indirection array.
1549 */
1550STATIC void
1551xfs_iext_irec_remove_all(
1552        struct xfs_ifork *ifp)
1553{
1554        int             nlists;
1555        int             i;
1556
1557        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1558        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1559        for (i = 0; i < nlists; i++)
1560                kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1561        kmem_free(ifp->if_u1.if_ext_irec);
1562        ifp->if_flags &= ~XFS_IFEXTIREC;
1563}
1564
1565/*
1566 * Free incore file extents.
1567 */
1568void
1569xfs_iext_destroy(
1570        xfs_ifork_t     *ifp)           /* inode fork pointer */
1571{
1572        if (ifp->if_flags & XFS_IFEXTIREC) {
1573                xfs_iext_irec_remove_all(ifp);
1574        } else if (ifp->if_real_bytes) {
1575                kmem_free(ifp->if_u1.if_extents);
1576        } else if (ifp->if_bytes) {
1577                memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1578                        sizeof(xfs_bmbt_rec_t));
1579        }
1580        ifp->if_u1.if_extents = NULL;
1581        ifp->if_real_bytes = 0;
1582        ifp->if_bytes = 0;
1583}
1584
1585/*
1586 * Return a pointer to the extent record for file system block bno.
1587 */
1588xfs_bmbt_rec_host_t *                   /* pointer to found extent record */
1589xfs_iext_bno_to_ext(
1590        xfs_ifork_t     *ifp,           /* inode fork pointer */
1591        xfs_fileoff_t   bno,            /* block number to search for */
1592        xfs_extnum_t    *idxp)          /* index of target extent */
1593{
1594        xfs_bmbt_rec_host_t *base;      /* pointer to first extent */
1595        xfs_filblks_t   blockcount = 0; /* number of blocks in extent */
1596        xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1597        xfs_ext_irec_t  *erp = NULL;    /* indirection array pointer */
1598        int             high;           /* upper boundary in search */
1599        xfs_extnum_t    idx = 0;        /* index of target extent */
1600        int             low;            /* lower boundary in search */
1601        xfs_extnum_t    nextents;       /* number of file extents */
1602        xfs_fileoff_t   startoff = 0;   /* start offset of extent */
1603
1604        nextents = xfs_iext_count(ifp);
1605        if (nextents == 0) {
1606                *idxp = 0;
1607                return NULL;
1608        }
1609        low = 0;
1610        if (ifp->if_flags & XFS_IFEXTIREC) {
1611                /* Find target extent list */
1612                int     erp_idx = 0;
1613                erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1614                base = erp->er_extbuf;
1615                high = erp->er_extcount - 1;
1616        } else {
1617                base = ifp->if_u1.if_extents;
1618                high = nextents - 1;
1619        }
1620        /* Binary search extent records */
1621        while (low <= high) {
1622                idx = (low + high) >> 1;
1623                ep = base + idx;
1624                startoff = xfs_bmbt_get_startoff(ep);
1625                blockcount = xfs_bmbt_get_blockcount(ep);
1626                if (bno < startoff) {
1627                        high = idx - 1;
1628                } else if (bno >= startoff + blockcount) {
1629                        low = idx + 1;
1630                } else {
1631                        /* Convert back to file-based extent index */
1632                        if (ifp->if_flags & XFS_IFEXTIREC) {
1633                                idx += erp->er_extoff;
1634                        }
1635                        *idxp = idx;
1636                        return ep;
1637                }
1638        }
1639        /* Convert back to file-based extent index */
1640        if (ifp->if_flags & XFS_IFEXTIREC) {
1641                idx += erp->er_extoff;
1642        }
1643        if (bno >= startoff + blockcount) {
1644                if (++idx == nextents) {
1645                        ep = NULL;
1646                } else {
1647                        ep = xfs_iext_get_ext(ifp, idx);
1648                }
1649        }
1650        *idxp = idx;
1651        return ep;
1652}
1653
1654/*
1655 * Return a pointer to the indirection array entry containing the
1656 * extent record for filesystem block bno. Store the index of the
1657 * target irec in *erp_idxp.
1658 */
1659xfs_ext_irec_t *                        /* pointer to found extent record */
1660xfs_iext_bno_to_irec(
1661        xfs_ifork_t     *ifp,           /* inode fork pointer */
1662        xfs_fileoff_t   bno,            /* block number to search for */
1663        int             *erp_idxp)      /* irec index of target ext list */
1664{
1665        xfs_ext_irec_t  *erp = NULL;    /* indirection array pointer */
1666        xfs_ext_irec_t  *erp_next;      /* next indirection array entry */
1667        int             erp_idx;        /* indirection array index */
1668        int             nlists;         /* number of extent irec's (lists) */
1669        int             high;           /* binary search upper limit */
1670        int             low;            /* binary search lower limit */
1671
1672        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1673        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1674        erp_idx = 0;
1675        low = 0;
1676        high = nlists - 1;
1677        while (low <= high) {
1678                erp_idx = (low + high) >> 1;
1679                erp = &ifp->if_u1.if_ext_irec[erp_idx];
1680                erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1681                if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1682                        high = erp_idx - 1;
1683                } else if (erp_next && bno >=
1684                           xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1685                        low = erp_idx + 1;
1686                } else {
1687                        break;
1688                }
1689        }
1690        *erp_idxp = erp_idx;
1691        return erp;
1692}
1693
1694/*
1695 * Return a pointer to the indirection array entry containing the
1696 * extent record at file extent index *idxp. Store the index of the
1697 * target irec in *erp_idxp and store the page index of the target
1698 * extent record in *idxp.
1699 */
1700xfs_ext_irec_t *
1701xfs_iext_idx_to_irec(
1702        xfs_ifork_t     *ifp,           /* inode fork pointer */
1703        xfs_extnum_t    *idxp,          /* extent index (file -> page) */
1704        int             *erp_idxp,      /* pointer to target irec */
1705        int             realloc)        /* new bytes were just added */
1706{
1707        xfs_ext_irec_t  *prev;          /* pointer to previous irec */
1708        xfs_ext_irec_t  *erp = NULL;    /* pointer to current irec */
1709        int             erp_idx;        /* indirection array index */
1710        int             nlists;         /* number of irec's (ex lists) */
1711        int             high;           /* binary search upper limit */
1712        int             low;            /* binary search lower limit */
1713        xfs_extnum_t    page_idx = *idxp; /* extent index in target list */
1714
1715        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1716        ASSERT(page_idx >= 0);
1717        ASSERT(page_idx <= xfs_iext_count(ifp));
1718        ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1719
1720        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1721        erp_idx = 0;
1722        low = 0;
1723        high = nlists - 1;
1724
1725        /* Binary search extent irec's */
1726        while (low <= high) {
1727                erp_idx = (low + high) >> 1;
1728                erp = &ifp->if_u1.if_ext_irec[erp_idx];
1729                prev = erp_idx > 0 ? erp - 1 : NULL;
1730                if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1731                     realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1732                        high = erp_idx - 1;
1733                } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1734                           (page_idx == erp->er_extoff + erp->er_extcount &&
1735                            !realloc)) {
1736                        low = erp_idx + 1;
1737                } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1738                           erp->er_extcount == XFS_LINEAR_EXTS) {
1739                        ASSERT(realloc);
1740                        page_idx = 0;
1741                        erp_idx++;
1742                        erp = erp_idx < nlists ? erp + 1 : NULL;
1743                        break;
1744                } else {
1745                        page_idx -= erp->er_extoff;
1746                        break;
1747                }
1748        }
1749        *idxp = page_idx;
1750        *erp_idxp = erp_idx;
1751        return erp;
1752}
1753
1754/*
1755 * Allocate and initialize an indirection array once the space needed
1756 * for incore extents increases above XFS_IEXT_BUFSZ.
1757 */
1758void
1759xfs_iext_irec_init(
1760        xfs_ifork_t     *ifp)           /* inode fork pointer */
1761{
1762        xfs_ext_irec_t  *erp;           /* indirection array pointer */
1763        xfs_extnum_t    nextents;       /* number of extents in file */
1764
1765        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1766        nextents = xfs_iext_count(ifp);
1767        ASSERT(nextents <= XFS_LINEAR_EXTS);
1768
1769        erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1770
1771        if (nextents == 0) {
1772                ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1773        } else if (!ifp->if_real_bytes) {
1774                xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1775        } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1776                xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1777        }
1778        erp->er_extbuf = ifp->if_u1.if_extents;
1779        erp->er_extcount = nextents;
1780        erp->er_extoff = 0;
1781
1782        ifp->if_flags |= XFS_IFEXTIREC;
1783        ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1784        ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1785        ifp->if_u1.if_ext_irec = erp;
1786
1787        return;
1788}
1789
1790/*
1791 * Allocate and initialize a new entry in the indirection array.
1792 */
1793xfs_ext_irec_t *
1794xfs_iext_irec_new(
1795        xfs_ifork_t     *ifp,           /* inode fork pointer */
1796        int             erp_idx)        /* index for new irec */
1797{
1798        xfs_ext_irec_t  *erp;           /* indirection array pointer */
1799        int             i;              /* loop counter */
1800        int             nlists;         /* number of irec's (ex lists) */
1801
1802        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1803        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1804
1805        /* Resize indirection array */
1806        xfs_iext_realloc_indirect(ifp, ++nlists *
1807                                  sizeof(xfs_ext_irec_t));
1808        /*
1809         * Move records down in the array so the
1810         * new page can use erp_idx.
1811         */
1812        erp = ifp->if_u1.if_ext_irec;
1813        for (i = nlists - 1; i > erp_idx; i--) {
1814                memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1815        }
1816        ASSERT(i == erp_idx);
1817
1818        /* Initialize new extent record */
1819        erp = ifp->if_u1.if_ext_irec;
1820        erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1821        ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1822        memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1823        erp[erp_idx].er_extcount = 0;
1824        erp[erp_idx].er_extoff = erp_idx > 0 ?
1825                erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1826        return (&erp[erp_idx]);
1827}
1828
1829/*
1830 * Remove a record from the indirection array.
1831 */
1832void
1833xfs_iext_irec_remove(
1834        xfs_ifork_t     *ifp,           /* inode fork pointer */
1835        int             erp_idx)        /* irec index to remove */
1836{
1837        xfs_ext_irec_t  *erp;           /* indirection array pointer */
1838        int             i;              /* loop counter */
1839        int             nlists;         /* number of irec's (ex lists) */
1840
1841        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1842        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1843        erp = &ifp->if_u1.if_ext_irec[erp_idx];
1844        if (erp->er_extbuf) {
1845                xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1846                        -erp->er_extcount);
1847                kmem_free(erp->er_extbuf);
1848        }
1849        /* Compact extent records */
1850        erp = ifp->if_u1.if_ext_irec;
1851        for (i = erp_idx; i < nlists - 1; i++) {
1852                memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1853        }
1854        /*
1855         * Manually free the last extent record from the indirection
1856         * array.  A call to xfs_iext_realloc_indirect() with a size
1857         * of zero would result in a call to xfs_iext_destroy() which
1858         * would in turn call this function again, creating a nasty
1859         * infinite loop.
1860         */
1861        if (--nlists) {
1862                xfs_iext_realloc_indirect(ifp,
1863                        nlists * sizeof(xfs_ext_irec_t));
1864        } else {
1865                kmem_free(ifp->if_u1.if_ext_irec);
1866        }
1867        ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1868}
1869
1870/*
1871 * This is called to clean up large amounts of unused memory allocated
1872 * by the indirection array.  Before compacting anything though, verify
1873 * that the indirection array is still needed and switch back to the
1874 * linear extent list (or even the inline buffer) if possible.  The
1875 * compaction policy is as follows:
1876 *
1877 *    Full Compaction: Extents fit into a single page (or inline buffer)
1878 * Partial Compaction: Extents occupy less than 50% of allocated space
1879 *      No Compaction: Extents occupy at least 50% of allocated space
1880 */
1881void
1882xfs_iext_irec_compact(
1883        xfs_ifork_t     *ifp)           /* inode fork pointer */
1884{
1885        xfs_extnum_t    nextents;       /* number of extents in file */
1886        int             nlists;         /* number of irec's (ex lists) */
1887
1888        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1889        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1890        nextents = xfs_iext_count(ifp);
1891
1892        if (nextents == 0) {
1893                xfs_iext_destroy(ifp);
1894        } else if (nextents <= XFS_INLINE_EXTS) {
1895                xfs_iext_indirect_to_direct(ifp);
1896                xfs_iext_direct_to_inline(ifp, nextents);
1897        } else if (nextents <= XFS_LINEAR_EXTS) {
1898                xfs_iext_indirect_to_direct(ifp);
1899        } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1900                xfs_iext_irec_compact_pages(ifp);
1901        }
1902}
1903
1904/*
1905 * Combine extents from neighboring extent pages.
1906 */
1907void
1908xfs_iext_irec_compact_pages(
1909        xfs_ifork_t     *ifp)           /* inode fork pointer */
1910{
1911        xfs_ext_irec_t  *erp, *erp_next;/* pointers to irec entries */
1912        int             erp_idx = 0;    /* indirection array index */
1913        int             nlists;         /* number of irec's (ex lists) */
1914
1915        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1916        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1917        while (erp_idx < nlists - 1) {
1918                erp = &ifp->if_u1.if_ext_irec[erp_idx];
1919                erp_next = erp + 1;
1920                if (erp_next->er_extcount <=
1921                    (XFS_LINEAR_EXTS - erp->er_extcount)) {
1922                        memcpy(&erp->er_extbuf[erp->er_extcount],
1923                                erp_next->er_extbuf, erp_next->er_extcount *
1924                                sizeof(xfs_bmbt_rec_t));
1925                        erp->er_extcount += erp_next->er_extcount;
1926                        /*
1927                         * Free page before removing extent record
1928                         * so er_extoffs don't get modified in
1929                         * xfs_iext_irec_remove.
1930                         */
1931                        kmem_free(erp_next->er_extbuf);
1932                        erp_next->er_extbuf = NULL;
1933                        xfs_iext_irec_remove(ifp, erp_idx + 1);
1934                        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1935                } else {
1936                        erp_idx++;
1937                }
1938        }
1939}
1940
1941/*
1942 * This is called to update the er_extoff field in the indirection
1943 * array when extents have been added or removed from one of the
1944 * extent lists. erp_idx contains the irec index to begin updating
1945 * at and ext_diff contains the number of extents that were added
1946 * or removed.
1947 */
1948void
1949xfs_iext_irec_update_extoffs(
1950        xfs_ifork_t     *ifp,           /* inode fork pointer */
1951        int             erp_idx,        /* irec index to update */
1952        int             ext_diff)       /* number of new extents */
1953{
1954        int             i;              /* loop counter */
1955        int             nlists;         /* number of irec's (ex lists */
1956
1957        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1958        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1959        for (i = erp_idx; i < nlists; i++) {
1960                ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1961        }
1962}
1963
1964/*
1965 * Initialize an inode's copy-on-write fork.
1966 */
1967void
1968xfs_ifork_init_cow(
1969        struct xfs_inode        *ip)
1970{
1971        if (ip->i_cowfp)
1972                return;
1973
1974        ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
1975                                       KM_SLEEP | KM_NOFS);
1976        ip->i_cowfp->if_flags = XFS_IFEXTENTS;
1977        ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
1978        ip->i_cnextents = 0;
1979}
1980
1981/*
1982 * Lookup the extent covering bno.
1983 *
1984 * If there is an extent covering bno return the extent index, and store the
1985 * expanded extent structure in *gotp, and the extent index in *idx.
1986 * If there is no extent covering bno, but there is an extent after it (e.g.
1987 * it lies in a hole) return that extent in *gotp and its index in *idx
1988 * instead.
1989 * If bno is beyond the last extent return false, and return the index after
1990 * the last valid index in *idxp.
1991 */
1992bool
1993xfs_iext_lookup_extent(
1994        struct xfs_inode        *ip,
1995        struct xfs_ifork        *ifp,
1996        xfs_fileoff_t           bno,
1997        xfs_extnum_t            *idxp,
1998        struct xfs_bmbt_irec    *gotp)
1999{
2000        struct xfs_bmbt_rec_host *ep;
2001
2002        XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2003
2004        ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2005        if (!ep)
2006                return false;
2007        xfs_bmbt_get_all(ep, gotp);
2008        return true;
2009}
2010
2011/*
2012 * Return true if there is an extent at index idx, and return the expanded
2013 * extent structure at idx in that case.  Else return false.
2014 */
2015bool
2016xfs_iext_get_extent(
2017        struct xfs_ifork        *ifp,
2018        xfs_extnum_t            idx,
2019        struct xfs_bmbt_irec    *gotp)
2020{
2021        if (idx < 0 || idx >= xfs_iext_count(ifp))
2022                return false;
2023        xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);
2024        return true;
2025}
2026