linux/fs/xfs/xfs_rtalloc.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000-2005 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 "xfs.h"
  19#include "xfs_fs.h"
  20#include "xfs_types.h"
  21#include "xfs_bit.h"
  22#include "xfs_log.h"
  23#include "xfs_inum.h"
  24#include "xfs_trans.h"
  25#include "xfs_sb.h"
  26#include "xfs_ag.h"
  27#include "xfs_dir2.h"
  28#include "xfs_dmapi.h"
  29#include "xfs_mount.h"
  30#include "xfs_bmap_btree.h"
  31#include "xfs_alloc_btree.h"
  32#include "xfs_ialloc_btree.h"
  33#include "xfs_dir2_sf.h"
  34#include "xfs_attr_sf.h"
  35#include "xfs_dinode.h"
  36#include "xfs_inode.h"
  37#include "xfs_btree.h"
  38#include "xfs_ialloc.h"
  39#include "xfs_alloc.h"
  40#include "xfs_bmap.h"
  41#include "xfs_rtalloc.h"
  42#include "xfs_fsops.h"
  43#include "xfs_error.h"
  44#include "xfs_rw.h"
  45#include "xfs_inode_item.h"
  46#include "xfs_trans_space.h"
  47
  48
  49/*
  50 * Prototypes for internal functions.
  51 */
  52
  53
  54STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  55                xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
  56STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
  57                xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
  58STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  59                xfs_extlen_t, int, xfs_rtblock_t *, int *);
  60STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  61                xfs_rtblock_t, xfs_rtblock_t *);
  62STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  63                xfs_rtblock_t, xfs_rtblock_t *);
  64STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
  65                xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
  66STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  67                xfs_extlen_t, int);
  68STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
  69                xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
  70
  71/*
  72 * Internal functions.
  73 */
  74
  75/*
  76 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
  77 */
  78STATIC int
  79xfs_lowbit32(
  80        __uint32_t      v)
  81{
  82        if (v)
  83                return ffs(v) - 1;
  84        return -1;
  85}
  86
  87/*
  88 * Allocate space to the bitmap or summary file, and zero it, for growfs.
  89 */
  90STATIC int                              /* error */
  91xfs_growfs_rt_alloc(
  92        xfs_mount_t     *mp,            /* file system mount point */
  93        xfs_extlen_t    oblocks,        /* old count of blocks */
  94        xfs_extlen_t    nblocks,        /* new count of blocks */
  95        xfs_ino_t       ino)            /* inode number (bitmap/summary) */
  96{
  97        xfs_fileoff_t   bno;            /* block number in file */
  98        xfs_buf_t       *bp;            /* temporary buffer for zeroing */
  99        int             cancelflags;    /* flags for xfs_trans_cancel */
 100        int             committed;      /* transaction committed flag */
 101        xfs_daddr_t     d;              /* disk block address */
 102        int             error;          /* error return value */
 103        xfs_fsblock_t   firstblock;     /* first block allocated in xaction */
 104        xfs_bmap_free_t flist;          /* list of freed blocks */
 105        xfs_fsblock_t   fsbno;          /* filesystem block for bno */
 106        xfs_inode_t     *ip;            /* pointer to incore inode */
 107        xfs_bmbt_irec_t map;            /* block map output */
 108        int             nmap;           /* number of block maps */
 109        int             resblks;        /* space reservation */
 110        xfs_trans_t     *tp;            /* transaction pointer */
 111
 112        /*
 113         * Allocate space to the file, as necessary.
 114         */
 115        while (oblocks < nblocks) {
 116                tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
 117                resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
 118                cancelflags = 0;
 119                /*
 120                 * Reserve space & log for one extent added to the file.
 121                 */
 122                if ((error = xfs_trans_reserve(tp, resblks,
 123                                XFS_GROWRTALLOC_LOG_RES(mp), 0,
 124                                XFS_TRANS_PERM_LOG_RES,
 125                                XFS_DEFAULT_PERM_LOG_COUNT)))
 126                        goto error_exit;
 127                cancelflags = XFS_TRANS_RELEASE_LOG_RES;
 128                /*
 129                 * Lock the inode.
 130                 */
 131                if ((error = xfs_trans_iget(mp, tp, ino, 0,
 132                                                XFS_ILOCK_EXCL, &ip)))
 133                        goto error_exit;
 134                XFS_BMAP_INIT(&flist, &firstblock);
 135                /*
 136                 * Allocate blocks to the bitmap file.
 137                 */
 138                nmap = 1;
 139                cancelflags |= XFS_TRANS_ABORT;
 140                error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
 141                        XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
 142                        resblks, &map, &nmap, &flist, NULL);
 143                if (!error && nmap < 1)
 144                        error = XFS_ERROR(ENOSPC);
 145                if (error)
 146                        goto error_exit;
 147                /*
 148                 * Free any blocks freed up in the transaction, then commit.
 149                 */
 150                error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
 151                if (error)
 152                        goto error_exit;
 153                xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
 154                /*
 155                 * Now we need to clear the allocated blocks.
 156                 * Do this one block per transaction, to keep it simple.
 157                 */
 158                cancelflags = 0;
 159                for (bno = map.br_startoff, fsbno = map.br_startblock;
 160                     bno < map.br_startoff + map.br_blockcount;
 161                     bno++, fsbno++) {
 162                        tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
 163                        /*
 164                         * Reserve log for one block zeroing.
 165                         */
 166                        if ((error = xfs_trans_reserve(tp, 0,
 167                                        XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
 168                                goto error_exit;
 169                        /*
 170                         * Lock the bitmap inode.
 171                         */
 172                        if ((error = xfs_trans_iget(mp, tp, ino, 0,
 173                                                        XFS_ILOCK_EXCL, &ip)))
 174                                goto error_exit;
 175                        /*
 176                         * Get a buffer for the block.
 177                         */
 178                        d = XFS_FSB_TO_DADDR(mp, fsbno);
 179                        bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
 180                                mp->m_bsize, 0);
 181                        if (bp == NULL) {
 182                                error = XFS_ERROR(EIO);
 183                                goto error_exit;
 184                        }
 185                        memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
 186                        xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
 187                        /*
 188                         * Commit the transaction.
 189                         */
 190                        xfs_trans_commit(tp, 0, NULL);
 191                }
 192                /*
 193                 * Go on to the next extent, if any.
 194                 */
 195                oblocks = map.br_startoff + map.br_blockcount;
 196        }
 197        return 0;
 198error_exit:
 199        xfs_trans_cancel(tp, cancelflags);
 200        return error;
 201}
 202
 203/*
 204 * Attempt to allocate an extent minlen<=len<=maxlen starting from
 205 * bitmap block bbno.  If we don't get maxlen then use prod to trim
 206 * the length, if given.  Returns error; returns starting block in *rtblock.
 207 * The lengths are all in rtextents.
 208 */
 209STATIC int                              /* error */
 210xfs_rtallocate_extent_block(
 211        xfs_mount_t     *mp,            /* file system mount point */
 212        xfs_trans_t     *tp,            /* transaction pointer */
 213        xfs_rtblock_t   bbno,           /* bitmap block number */
 214        xfs_extlen_t    minlen,         /* minimum length to allocate */
 215        xfs_extlen_t    maxlen,         /* maximum length to allocate */
 216        xfs_extlen_t    *len,           /* out: actual length allocated */
 217        xfs_rtblock_t   *nextp,         /* out: next block to try */
 218        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
 219        xfs_fsblock_t   *rsb,           /* in/out: summary block number */
 220        xfs_extlen_t    prod,           /* extent product factor */
 221        xfs_rtblock_t   *rtblock)       /* out: start block allocated */
 222{
 223        xfs_rtblock_t   besti;          /* best rtblock found so far */
 224        xfs_rtblock_t   bestlen;        /* best length found so far */
 225        xfs_rtblock_t   end;            /* last rtblock in chunk */
 226        int             error;          /* error value */
 227        xfs_rtblock_t   i;              /* current rtblock trying */
 228        xfs_rtblock_t   next;           /* next rtblock to try */
 229        int             stat;           /* status from internal calls */
 230
 231        /*
 232         * Loop over all the extents starting in this bitmap block,
 233         * looking for one that's long enough.
 234         */
 235        for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
 236                end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
 237             i <= end;
 238             i++) {
 239                /*
 240                 * See if there's a free extent of maxlen starting at i.
 241                 * If it's not so then next will contain the first non-free.
 242                 */
 243                error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
 244                if (error) {
 245                        return error;
 246                }
 247                if (stat) {
 248                        /*
 249                         * i for maxlen is all free, allocate and return that.
 250                         */
 251                        error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
 252                                rsb);
 253                        if (error) {
 254                                return error;
 255                        }
 256                        *len = maxlen;
 257                        *rtblock = i;
 258                        return 0;
 259                }
 260                /*
 261                 * In the case where we have a variable-sized allocation
 262                 * request, figure out how big this free piece is,
 263                 * and if it's big enough for the minimum, and the best
 264                 * so far, remember it.
 265                 */
 266                if (minlen < maxlen) {
 267                        xfs_rtblock_t   thislen;        /* this extent size */
 268
 269                        thislen = next - i;
 270                        if (thislen >= minlen && thislen > bestlen) {
 271                                besti = i;
 272                                bestlen = thislen;
 273                        }
 274                }
 275                /*
 276                 * If not done yet, find the start of the next free space.
 277                 */
 278                if (next < end) {
 279                        error = xfs_rtfind_forw(mp, tp, next, end, &i);
 280                        if (error) {
 281                                return error;
 282                        }
 283                } else
 284                        break;
 285        }
 286        /*
 287         * Searched the whole thing & didn't find a maxlen free extent.
 288         */
 289        if (minlen < maxlen && besti != -1) {
 290                xfs_extlen_t    p;      /* amount to trim length by */
 291
 292                /*
 293                 * If size should be a multiple of prod, make that so.
 294                 */
 295                if (prod > 1 && (p = do_mod(bestlen, prod)))
 296                        bestlen -= p;
 297                /*
 298                 * Allocate besti for bestlen & return that.
 299                 */
 300                error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
 301                if (error) {
 302                        return error;
 303                }
 304                *len = bestlen;
 305                *rtblock = besti;
 306                return 0;
 307        }
 308        /*
 309         * Allocation failed.  Set *nextp to the next block to try.
 310         */
 311        *nextp = next;
 312        *rtblock = NULLRTBLOCK;
 313        return 0;
 314}
 315
 316/*
 317 * Allocate an extent of length minlen<=len<=maxlen, starting at block
 318 * bno.  If we don't get maxlen then use prod to trim the length, if given.
 319 * Returns error; returns starting block in *rtblock.
 320 * The lengths are all in rtextents.
 321 */
 322STATIC int                              /* error */
 323xfs_rtallocate_extent_exact(
 324        xfs_mount_t     *mp,            /* file system mount point */
 325        xfs_trans_t     *tp,            /* transaction pointer */
 326        xfs_rtblock_t   bno,            /* starting block number to allocate */
 327        xfs_extlen_t    minlen,         /* minimum length to allocate */
 328        xfs_extlen_t    maxlen,         /* maximum length to allocate */
 329        xfs_extlen_t    *len,           /* out: actual length allocated */
 330        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
 331        xfs_fsblock_t   *rsb,           /* in/out: summary block number */
 332        xfs_extlen_t    prod,           /* extent product factor */
 333        xfs_rtblock_t   *rtblock)       /* out: start block allocated */
 334{
 335        int             error;          /* error value */
 336        xfs_extlen_t    i;              /* extent length trimmed due to prod */
 337        int             isfree;         /* extent is free */
 338        xfs_rtblock_t   next;           /* next block to try (dummy) */
 339
 340        ASSERT(minlen % prod == 0 && maxlen % prod == 0);
 341        /*
 342         * Check if the range in question (for maxlen) is free.
 343         */
 344        error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
 345        if (error) {
 346                return error;
 347        }
 348        if (isfree) {
 349                /*
 350                 * If it is, allocate it and return success.
 351                 */
 352                error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
 353                if (error) {
 354                        return error;
 355                }
 356                *len = maxlen;
 357                *rtblock = bno;
 358                return 0;
 359        }
 360        /*
 361         * If not, allocate what there is, if it's at least minlen.
 362         */
 363        maxlen = next - bno;
 364        if (maxlen < minlen) {
 365                /*
 366                 * Failed, return failure status.
 367                 */
 368                *rtblock = NULLRTBLOCK;
 369                return 0;
 370        }
 371        /*
 372         * Trim off tail of extent, if prod is specified.
 373         */
 374        if (prod > 1 && (i = maxlen % prod)) {
 375                maxlen -= i;
 376                if (maxlen < minlen) {
 377                        /*
 378                         * Now we can't do it, return failure status.
 379                         */
 380                        *rtblock = NULLRTBLOCK;
 381                        return 0;
 382                }
 383        }
 384        /*
 385         * Allocate what we can and return it.
 386         */
 387        error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
 388        if (error) {
 389                return error;
 390        }
 391        *len = maxlen;
 392        *rtblock = bno;
 393        return 0;
 394}
 395
 396/*
 397 * Allocate an extent of length minlen<=len<=maxlen, starting as near
 398 * to bno as possible.  If we don't get maxlen then use prod to trim
 399 * the length, if given.  The lengths are all in rtextents.
 400 */
 401STATIC int                              /* error */
 402xfs_rtallocate_extent_near(
 403        xfs_mount_t     *mp,            /* file system mount point */
 404        xfs_trans_t     *tp,            /* transaction pointer */
 405        xfs_rtblock_t   bno,            /* starting block number to allocate */
 406        xfs_extlen_t    minlen,         /* minimum length to allocate */
 407        xfs_extlen_t    maxlen,         /* maximum length to allocate */
 408        xfs_extlen_t    *len,           /* out: actual length allocated */
 409        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
 410        xfs_fsblock_t   *rsb,           /* in/out: summary block number */
 411        xfs_extlen_t    prod,           /* extent product factor */
 412        xfs_rtblock_t   *rtblock)       /* out: start block allocated */
 413{
 414        int             any;            /* any useful extents from summary */
 415        xfs_rtblock_t   bbno;           /* bitmap block number */
 416        int             error;          /* error value */
 417        int             i;              /* bitmap block offset (loop control) */
 418        int             j;              /* secondary loop control */
 419        int             log2len;        /* log2 of minlen */
 420        xfs_rtblock_t   n;              /* next block to try */
 421        xfs_rtblock_t   r;              /* result block */
 422
 423        ASSERT(minlen % prod == 0 && maxlen % prod == 0);
 424        /*
 425         * If the block number given is off the end, silently set it to
 426         * the last block.
 427         */
 428        if (bno >= mp->m_sb.sb_rextents)
 429                bno = mp->m_sb.sb_rextents - 1;
 430        /*
 431         * Try the exact allocation first.
 432         */
 433        error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
 434                rbpp, rsb, prod, &r);
 435        if (error) {
 436                return error;
 437        }
 438        /*
 439         * If the exact allocation worked, return that.
 440         */
 441        if (r != NULLRTBLOCK) {
 442                *rtblock = r;
 443                return 0;
 444        }
 445        bbno = XFS_BITTOBLOCK(mp, bno);
 446        i = 0;
 447        log2len = xfs_highbit32(minlen);
 448        /*
 449         * Loop over all bitmap blocks (bbno + i is current block).
 450         */
 451        for (;;) {
 452                /*
 453                 * Get summary information of extents of all useful levels
 454                 * starting in this bitmap block.
 455                 */
 456                error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
 457                        bbno + i, rbpp, rsb, &any);
 458                if (error) {
 459                        return error;
 460                }
 461                /*
 462                 * If there are any useful extents starting here, try
 463                 * allocating one.
 464                 */
 465                if (any) {
 466                        /*
 467                         * On the positive side of the starting location.
 468                         */
 469                        if (i >= 0) {
 470                                /*
 471                                 * Try to allocate an extent starting in
 472                                 * this block.
 473                                 */
 474                                error = xfs_rtallocate_extent_block(mp, tp,
 475                                        bbno + i, minlen, maxlen, len, &n, rbpp,
 476                                        rsb, prod, &r);
 477                                if (error) {
 478                                        return error;
 479                                }
 480                                /*
 481                                 * If it worked, return it.
 482                                 */
 483                                if (r != NULLRTBLOCK) {
 484                                        *rtblock = r;
 485                                        return 0;
 486                                }
 487                        }
 488                        /*
 489                         * On the negative side of the starting location.
 490                         */
 491                        else {          /* i < 0 */
 492                                /*
 493                                 * Loop backwards through the bitmap blocks from
 494                                 * the starting point-1 up to where we are now.
 495                                 * There should be an extent which ends in this
 496                                 * bitmap block and is long enough.
 497                                 */
 498                                for (j = -1; j > i; j--) {
 499                                        /*
 500                                         * Grab the summary information for
 501                                         * this bitmap block.
 502                                         */
 503                                        error = xfs_rtany_summary(mp, tp,
 504                                                log2len, mp->m_rsumlevels - 1,
 505                                                bbno + j, rbpp, rsb, &any);
 506                                        if (error) {
 507                                                return error;
 508                                        }
 509                                        /*
 510                                         * If there's no extent given in the
 511                                         * summary that means the extent we
 512                                         * found must carry over from an
 513                                         * earlier block.  If there is an
 514                                         * extent given, we've already tried
 515                                         * that allocation, don't do it again.
 516                                         */
 517                                        if (any)
 518                                                continue;
 519                                        error = xfs_rtallocate_extent_block(mp,
 520                                                tp, bbno + j, minlen, maxlen,
 521                                                len, &n, rbpp, rsb, prod, &r);
 522                                        if (error) {
 523                                                return error;
 524                                        }
 525                                        /*
 526                                         * If it works, return the extent.
 527                                         */
 528                                        if (r != NULLRTBLOCK) {
 529                                                *rtblock = r;
 530                                                return 0;
 531                                        }
 532                                }
 533                                /*
 534                                 * There weren't intervening bitmap blocks
 535                                 * with a long enough extent, or the
 536                                 * allocation didn't work for some reason
 537                                 * (i.e. it's a little * too short).
 538                                 * Try to allocate from the summary block
 539                                 * that we found.
 540                                 */
 541                                error = xfs_rtallocate_extent_block(mp, tp,
 542                                        bbno + i, minlen, maxlen, len, &n, rbpp,
 543                                        rsb, prod, &r);
 544                                if (error) {
 545                                        return error;
 546                                }
 547                                /*
 548                                 * If it works, return the extent.
 549                                 */
 550                                if (r != NULLRTBLOCK) {
 551                                        *rtblock = r;
 552                                        return 0;
 553                                }
 554                        }
 555                }
 556                /*
 557                 * Loop control.  If we were on the positive side, and there's
 558                 * still more blocks on the negative side, go there.
 559                 */
 560                if (i > 0 && (int)bbno - i >= 0)
 561                        i = -i;
 562                /*
 563                 * If positive, and no more negative, but there are more
 564                 * positive, go there.
 565                 */
 566                else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
 567                        i++;
 568                /*
 569                 * If negative or 0 (just started), and there are positive
 570                 * blocks to go, go there.  The 0 case moves to block 1.
 571                 */
 572                else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
 573                        i = 1 - i;
 574                /*
 575                 * If negative or 0 and there are more negative blocks,
 576                 * go there.
 577                 */
 578                else if (i <= 0 && (int)bbno + i > 0)
 579                        i--;
 580                /*
 581                 * Must be done.  Return failure.
 582                 */
 583                else
 584                        break;
 585        }
 586        *rtblock = NULLRTBLOCK;
 587        return 0;
 588}
 589
 590/*
 591 * Allocate an extent of length minlen<=len<=maxlen, with no position
 592 * specified.  If we don't get maxlen then use prod to trim
 593 * the length, if given.  The lengths are all in rtextents.
 594 */
 595STATIC int                              /* error */
 596xfs_rtallocate_extent_size(
 597        xfs_mount_t     *mp,            /* file system mount point */
 598        xfs_trans_t     *tp,            /* transaction pointer */
 599        xfs_extlen_t    minlen,         /* minimum length to allocate */
 600        xfs_extlen_t    maxlen,         /* maximum length to allocate */
 601        xfs_extlen_t    *len,           /* out: actual length allocated */
 602        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
 603        xfs_fsblock_t   *rsb,           /* in/out: summary block number */
 604        xfs_extlen_t    prod,           /* extent product factor */
 605        xfs_rtblock_t   *rtblock)       /* out: start block allocated */
 606{
 607        int             error;          /* error value */
 608        int             i;              /* bitmap block number */
 609        int             l;              /* level number (loop control) */
 610        xfs_rtblock_t   n;              /* next block to be tried */
 611        xfs_rtblock_t   r;              /* result block number */
 612        xfs_suminfo_t   sum;            /* summary information for extents */
 613
 614        ASSERT(minlen % prod == 0 && maxlen % prod == 0);
 615        /*
 616         * Loop over all the levels starting with maxlen.
 617         * At each level, look at all the bitmap blocks, to see if there
 618         * are extents starting there that are long enough (>= maxlen).
 619         * Note, only on the initial level can the allocation fail if
 620         * the summary says there's an extent.
 621         */
 622        for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
 623                /*
 624                 * Loop over all the bitmap blocks.
 625                 */
 626                for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
 627                        /*
 628                         * Get the summary for this level/block.
 629                         */
 630                        error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
 631                                &sum);
 632                        if (error) {
 633                                return error;
 634                        }
 635                        /*
 636                         * Nothing there, on to the next block.
 637                         */
 638                        if (!sum)
 639                                continue;
 640                        /*
 641                         * Try allocating the extent.
 642                         */
 643                        error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
 644                                maxlen, len, &n, rbpp, rsb, prod, &r);
 645                        if (error) {
 646                                return error;
 647                        }
 648                        /*
 649                         * If it worked, return that.
 650                         */
 651                        if (r != NULLRTBLOCK) {
 652                                *rtblock = r;
 653                                return 0;
 654                        }
 655                        /*
 656                         * If the "next block to try" returned from the
 657                         * allocator is beyond the next bitmap block,
 658                         * skip to that bitmap block.
 659                         */
 660                        if (XFS_BITTOBLOCK(mp, n) > i + 1)
 661                                i = XFS_BITTOBLOCK(mp, n) - 1;
 662                }
 663        }
 664        /*
 665         * Didn't find any maxlen blocks.  Try smaller ones, unless
 666         * we're asking for a fixed size extent.
 667         */
 668        if (minlen > --maxlen) {
 669                *rtblock = NULLRTBLOCK;
 670                return 0;
 671        }
 672        /*
 673         * Loop over sizes, from maxlen down to minlen.
 674         * This time, when we do the allocations, allow smaller ones
 675         * to succeed.
 676         */
 677        for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
 678                /*
 679                 * Loop over all the bitmap blocks, try an allocation
 680                 * starting in that block.
 681                 */
 682                for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
 683                        /*
 684                         * Get the summary information for this level/block.
 685                         */
 686                        error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
 687                                                  &sum);
 688                        if (error) {
 689                                return error;
 690                        }
 691                        /*
 692                         * If nothing there, go on to next.
 693                         */
 694                        if (!sum)
 695                                continue;
 696                        /*
 697                         * Try the allocation.  Make sure the specified
 698                         * minlen/maxlen are in the possible range for
 699                         * this summary level.
 700                         */
 701                        error = xfs_rtallocate_extent_block(mp, tp, i,
 702                                        XFS_RTMAX(minlen, 1 << l),
 703                                        XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
 704                                        len, &n, rbpp, rsb, prod, &r);
 705                        if (error) {
 706                                return error;
 707                        }
 708                        /*
 709                         * If it worked, return that extent.
 710                         */
 711                        if (r != NULLRTBLOCK) {
 712                                *rtblock = r;
 713                                return 0;
 714                        }
 715                        /*
 716                         * If the "next block to try" returned from the
 717                         * allocator is beyond the next bitmap block,
 718                         * skip to that bitmap block.
 719                         */
 720                        if (XFS_BITTOBLOCK(mp, n) > i + 1)
 721                                i = XFS_BITTOBLOCK(mp, n) - 1;
 722                }
 723        }
 724        /*
 725         * Got nothing, return failure.
 726         */
 727        *rtblock = NULLRTBLOCK;
 728        return 0;
 729}
 730
 731/*
 732 * Mark an extent specified by start and len allocated.
 733 * Updates all the summary information as well as the bitmap.
 734 */
 735STATIC int                              /* error */
 736xfs_rtallocate_range(
 737        xfs_mount_t     *mp,            /* file system mount point */
 738        xfs_trans_t     *tp,            /* transaction pointer */
 739        xfs_rtblock_t   start,          /* start block to allocate */
 740        xfs_extlen_t    len,            /* length to allocate */
 741        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
 742        xfs_fsblock_t   *rsb)           /* in/out: summary block number */
 743{
 744        xfs_rtblock_t   end;            /* end of the allocated extent */
 745        int             error;          /* error value */
 746        xfs_rtblock_t   postblock;      /* first block allocated > end */
 747        xfs_rtblock_t   preblock;       /* first block allocated < start */
 748
 749        end = start + len - 1;
 750        /*
 751         * Assume we're allocating out of the middle of a free extent.
 752         * We need to find the beginning and end of the extent so we can
 753         * properly update the summary.
 754         */
 755        error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
 756        if (error) {
 757                return error;
 758        }
 759        /*
 760         * Find the next allocated block (end of free extent).
 761         */
 762        error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
 763                &postblock);
 764        if (error) {
 765                return error;
 766        }
 767        /*
 768         * Decrement the summary information corresponding to the entire
 769         * (old) free extent.
 770         */
 771        error = xfs_rtmodify_summary(mp, tp,
 772                XFS_RTBLOCKLOG(postblock + 1 - preblock),
 773                XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
 774        if (error) {
 775                return error;
 776        }
 777        /*
 778         * If there are blocks not being allocated at the front of the
 779         * old extent, add summary data for them to be free.
 780         */
 781        if (preblock < start) {
 782                error = xfs_rtmodify_summary(mp, tp,
 783                        XFS_RTBLOCKLOG(start - preblock),
 784                        XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
 785                if (error) {
 786                        return error;
 787                }
 788        }
 789        /*
 790         * If there are blocks not being allocated at the end of the
 791         * old extent, add summary data for them to be free.
 792         */
 793        if (postblock > end) {
 794                error = xfs_rtmodify_summary(mp, tp,
 795                        XFS_RTBLOCKLOG(postblock - end),
 796                        XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
 797                if (error) {
 798                        return error;
 799                }
 800        }
 801        /*
 802         * Modify the bitmap to mark this extent allocated.
 803         */
 804        error = xfs_rtmodify_range(mp, tp, start, len, 0);
 805        return error;
 806}
 807
 808/*
 809 * Return whether there are any free extents in the size range given
 810 * by low and high, for the bitmap block bbno.
 811 */
 812STATIC int                              /* error */
 813xfs_rtany_summary(
 814        xfs_mount_t     *mp,            /* file system mount structure */
 815        xfs_trans_t     *tp,            /* transaction pointer */
 816        int             low,            /* low log2 extent size */
 817        int             high,           /* high log2 extent size */
 818        xfs_rtblock_t   bbno,           /* bitmap block number */
 819        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
 820        xfs_fsblock_t   *rsb,           /* in/out: summary block number */
 821        int             *stat)          /* out: any good extents here? */
 822{
 823        int             error;          /* error value */
 824        int             log;            /* loop counter, log2 of ext. size */
 825        xfs_suminfo_t   sum;            /* summary data */
 826
 827        /*
 828         * Loop over logs of extent sizes.  Order is irrelevant.
 829         */
 830        for (log = low; log <= high; log++) {
 831                /*
 832                 * Get one summary datum.
 833                 */
 834                error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
 835                if (error) {
 836                        return error;
 837                }
 838                /*
 839                 * If there are any, return success.
 840                 */
 841                if (sum) {
 842                        *stat = 1;
 843                        return 0;
 844                }
 845        }
 846        /*
 847         * Found nothing, return failure.
 848         */
 849        *stat = 0;
 850        return 0;
 851}
 852
 853/*
 854 * Get a buffer for the bitmap or summary file block specified.
 855 * The buffer is returned read and locked.
 856 */
 857STATIC int                              /* error */
 858xfs_rtbuf_get(
 859        xfs_mount_t     *mp,            /* file system mount structure */
 860        xfs_trans_t     *tp,            /* transaction pointer */
 861        xfs_rtblock_t   block,          /* block number in bitmap or summary */
 862        int             issum,          /* is summary not bitmap */
 863        xfs_buf_t       **bpp)          /* output: buffer for the block */
 864{
 865        xfs_buf_t       *bp;            /* block buffer, result */
 866        xfs_daddr_t     d;              /* disk addr of block */
 867        int             error;          /* error value */
 868        xfs_fsblock_t   fsb;            /* fs block number for block */
 869        xfs_inode_t     *ip;            /* bitmap or summary inode */
 870
 871        ip = issum ? mp->m_rsumip : mp->m_rbmip;
 872        /*
 873         * Map from the file offset (block) and inode number to the
 874         * file system block.
 875         */
 876        error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
 877        if (error) {
 878                return error;
 879        }
 880        ASSERT(fsb != NULLFSBLOCK);
 881        /*
 882         * Convert to disk address for buffer cache.
 883         */
 884        d = XFS_FSB_TO_DADDR(mp, fsb);
 885        /*
 886         * Read the buffer.
 887         */
 888        error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
 889                                   mp->m_bsize, 0, &bp);
 890        if (error) {
 891                return error;
 892        }
 893        ASSERT(bp && !XFS_BUF_GETERROR(bp));
 894        *bpp = bp;
 895        return 0;
 896}
 897
 898#ifdef DEBUG
 899/*
 900 * Check that the given extent (block range) is allocated already.
 901 */
 902STATIC int                              /* error */
 903xfs_rtcheck_alloc_range(
 904        xfs_mount_t     *mp,            /* file system mount point */
 905        xfs_trans_t     *tp,            /* transaction pointer */
 906        xfs_rtblock_t   bno,            /* starting block number of extent */
 907        xfs_extlen_t    len,            /* length of extent */
 908        int             *stat)          /* out: 1 for allocated, 0 for not */
 909{
 910        xfs_rtblock_t   new;            /* dummy for xfs_rtcheck_range */
 911
 912        return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
 913}
 914#endif
 915
 916#ifdef DEBUG
 917/*
 918 * Check whether the given block in the bitmap has the given value.
 919 */
 920STATIC int                              /* 1 for matches, 0 for not */
 921xfs_rtcheck_bit(
 922        xfs_mount_t     *mp,            /* file system mount structure */
 923        xfs_trans_t     *tp,            /* transaction pointer */
 924        xfs_rtblock_t   start,          /* bit (block) to check */
 925        int             val)            /* 1 for free, 0 for allocated */
 926{
 927        int             bit;            /* bit number in the word */
 928        xfs_rtblock_t   block;          /* bitmap block number */
 929        xfs_buf_t       *bp;            /* buf for the block */
 930        xfs_rtword_t    *bufp;          /* pointer into the buffer */
 931        /* REFERENCED */
 932        int             error;          /* error value */
 933        xfs_rtword_t    wdiff;          /* difference between bit & expected */
 934        int             word;           /* word number in the buffer */
 935        xfs_rtword_t    wval;           /* word value from buffer */
 936
 937        block = XFS_BITTOBLOCK(mp, start);
 938        error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
 939        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
 940        word = XFS_BITTOWORD(mp, start);
 941        bit = (int)(start & (XFS_NBWORD - 1));
 942        wval = bufp[word];
 943        xfs_trans_brelse(tp, bp);
 944        wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
 945        return !wdiff;
 946}
 947#endif  /* DEBUG */
 948
 949#if 0
 950/*
 951 * Check that the given extent (block range) is free already.
 952 */
 953STATIC int                              /* error */
 954xfs_rtcheck_free_range(
 955        xfs_mount_t     *mp,            /* file system mount point */
 956        xfs_trans_t     *tp,            /* transaction pointer */
 957        xfs_rtblock_t   bno,            /* starting block number of extent */
 958        xfs_extlen_t    len,            /* length of extent */
 959        int             *stat)          /* out: 1 for free, 0 for not */
 960{
 961        xfs_rtblock_t   new;            /* dummy for xfs_rtcheck_range */
 962
 963        return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
 964}
 965#endif
 966
 967/*
 968 * Check that the given range is either all allocated (val = 0) or
 969 * all free (val = 1).
 970 */
 971STATIC int                              /* error */
 972xfs_rtcheck_range(
 973        xfs_mount_t     *mp,            /* file system mount point */
 974        xfs_trans_t     *tp,            /* transaction pointer */
 975        xfs_rtblock_t   start,          /* starting block number of extent */
 976        xfs_extlen_t    len,            /* length of extent */
 977        int             val,            /* 1 for free, 0 for allocated */
 978        xfs_rtblock_t   *new,           /* out: first block not matching */
 979        int             *stat)          /* out: 1 for matches, 0 for not */
 980{
 981        xfs_rtword_t    *b;             /* current word in buffer */
 982        int             bit;            /* bit number in the word */
 983        xfs_rtblock_t   block;          /* bitmap block number */
 984        xfs_buf_t       *bp;            /* buf for the block */
 985        xfs_rtword_t    *bufp;          /* starting word in buffer */
 986        int             error;          /* error value */
 987        xfs_rtblock_t   i;              /* current bit number rel. to start */
 988        xfs_rtblock_t   lastbit;        /* last useful bit in word */
 989        xfs_rtword_t    mask;           /* mask of relevant bits for value */
 990        xfs_rtword_t    wdiff;          /* difference from wanted value */
 991        int             word;           /* word number in the buffer */
 992
 993        /*
 994         * Compute starting bitmap block number
 995         */
 996        block = XFS_BITTOBLOCK(mp, start);
 997        /*
 998         * Read the bitmap block.
 999         */
1000        error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1001        if (error) {
1002                return error;
1003        }
1004        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1005        /*
1006         * Compute the starting word's address, and starting bit.
1007         */
1008        word = XFS_BITTOWORD(mp, start);
1009        b = &bufp[word];
1010        bit = (int)(start & (XFS_NBWORD - 1));
1011        /*
1012         * 0 (allocated) => all zero's; 1 (free) => all one's.
1013         */
1014        val = -val;
1015        /*
1016         * If not starting on a word boundary, deal with the first
1017         * (partial) word.
1018         */
1019        if (bit) {
1020                /*
1021                 * Compute first bit not examined.
1022                 */
1023                lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1024                /*
1025                 * Mask of relevant bits.
1026                 */
1027                mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1028                /*
1029                 * Compute difference between actual and desired value.
1030                 */
1031                if ((wdiff = (*b ^ val) & mask)) {
1032                        /*
1033                         * Different, compute first wrong bit and return.
1034                         */
1035                        xfs_trans_brelse(tp, bp);
1036                        i = XFS_RTLOBIT(wdiff) - bit;
1037                        *new = start + i;
1038                        *stat = 0;
1039                        return 0;
1040                }
1041                i = lastbit - bit;
1042                /*
1043                 * Go on to next block if that's where the next word is
1044                 * and we need the next word.
1045                 */
1046                if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1047                        /*
1048                         * If done with this block, get the next one.
1049                         */
1050                        xfs_trans_brelse(tp, bp);
1051                        error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1052                        if (error) {
1053                                return error;
1054                        }
1055                        b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1056                        word = 0;
1057                } else {
1058                        /*
1059                         * Go on to the next word in the buffer.
1060                         */
1061                        b++;
1062                }
1063        } else {
1064                /*
1065                 * Starting on a word boundary, no partial word.
1066                 */
1067                i = 0;
1068        }
1069        /*
1070         * Loop over whole words in buffers.  When we use up one buffer
1071         * we move on to the next one.
1072         */
1073        while (len - i >= XFS_NBWORD) {
1074                /*
1075                 * Compute difference between actual and desired value.
1076                 */
1077                if ((wdiff = *b ^ val)) {
1078                        /*
1079                         * Different, compute first wrong bit and return.
1080                         */
1081                        xfs_trans_brelse(tp, bp);
1082                        i += XFS_RTLOBIT(wdiff);
1083                        *new = start + i;
1084                        *stat = 0;
1085                        return 0;
1086                }
1087                i += XFS_NBWORD;
1088                /*
1089                 * Go on to next block if that's where the next word is
1090                 * and we need the next word.
1091                 */
1092                if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1093                        /*
1094                         * If done with this block, get the next one.
1095                         */
1096                        xfs_trans_brelse(tp, bp);
1097                        error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1098                        if (error) {
1099                                return error;
1100                        }
1101                        b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1102                        word = 0;
1103                } else {
1104                        /*
1105                         * Go on to the next word in the buffer.
1106                         */
1107                        b++;
1108                }
1109        }
1110        /*
1111         * If not ending on a word boundary, deal with the last
1112         * (partial) word.
1113         */
1114        if ((lastbit = len - i)) {
1115                /*
1116                 * Mask of relevant bits.
1117                 */
1118                mask = ((xfs_rtword_t)1 << lastbit) - 1;
1119                /*
1120                 * Compute difference between actual and desired value.
1121                 */
1122                if ((wdiff = (*b ^ val) & mask)) {
1123                        /*
1124                         * Different, compute first wrong bit and return.
1125                         */
1126                        xfs_trans_brelse(tp, bp);
1127                        i += XFS_RTLOBIT(wdiff);
1128                        *new = start + i;
1129                        *stat = 0;
1130                        return 0;
1131                } else
1132                        i = len;
1133        }
1134        /*
1135         * Successful, return.
1136         */
1137        xfs_trans_brelse(tp, bp);
1138        *new = start + i;
1139        *stat = 1;
1140        return 0;
1141}
1142
1143/*
1144 * Copy and transform the summary file, given the old and new
1145 * parameters in the mount structures.
1146 */
1147STATIC int                              /* error */
1148xfs_rtcopy_summary(
1149        xfs_mount_t     *omp,           /* old file system mount point */
1150        xfs_mount_t     *nmp,           /* new file system mount point */
1151        xfs_trans_t     *tp)            /* transaction pointer */
1152{
1153        xfs_rtblock_t   bbno;           /* bitmap block number */
1154        xfs_buf_t       *bp;            /* summary buffer */
1155        int             error;          /* error return value */
1156        int             log;            /* summary level number (log length) */
1157        xfs_suminfo_t   sum;            /* summary data */
1158        xfs_fsblock_t   sumbno;         /* summary block number */
1159
1160        bp = NULL;
1161        for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1162                for (bbno = omp->m_sb.sb_rbmblocks - 1;
1163                     (xfs_srtblock_t)bbno >= 0;
1164                     bbno--) {
1165                        error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1166                                &sumbno, &sum);
1167                        if (error)
1168                                return error;
1169                        if (sum == 0)
1170                                continue;
1171                        error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1172                                &bp, &sumbno);
1173                        if (error)
1174                                return error;
1175                        error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1176                                &bp, &sumbno);
1177                        if (error)
1178                                return error;
1179                        ASSERT(sum > 0);
1180                }
1181        }
1182        return 0;
1183}
1184
1185/*
1186 * Searching backward from start to limit, find the first block whose
1187 * allocated/free state is different from start's.
1188 */
1189STATIC int                              /* error */
1190xfs_rtfind_back(
1191        xfs_mount_t     *mp,            /* file system mount point */
1192        xfs_trans_t     *tp,            /* transaction pointer */
1193        xfs_rtblock_t   start,          /* starting block to look at */
1194        xfs_rtblock_t   limit,          /* last block to look at */
1195        xfs_rtblock_t   *rtblock)       /* out: start block found */
1196{
1197        xfs_rtword_t    *b;             /* current word in buffer */
1198        int             bit;            /* bit number in the word */
1199        xfs_rtblock_t   block;          /* bitmap block number */
1200        xfs_buf_t       *bp;            /* buf for the block */
1201        xfs_rtword_t    *bufp;          /* starting word in buffer */
1202        int             error;          /* error value */
1203        xfs_rtblock_t   firstbit;       /* first useful bit in the word */
1204        xfs_rtblock_t   i;              /* current bit number rel. to start */
1205        xfs_rtblock_t   len;            /* length of inspected area */
1206        xfs_rtword_t    mask;           /* mask of relevant bits for value */
1207        xfs_rtword_t    want;           /* mask for "good" values */
1208        xfs_rtword_t    wdiff;          /* difference from wanted value */
1209        int             word;           /* word number in the buffer */
1210
1211        /*
1212         * Compute and read in starting bitmap block for starting block.
1213         */
1214        block = XFS_BITTOBLOCK(mp, start);
1215        error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1216        if (error) {
1217                return error;
1218        }
1219        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1220        /*
1221         * Get the first word's index & point to it.
1222         */
1223        word = XFS_BITTOWORD(mp, start);
1224        b = &bufp[word];
1225        bit = (int)(start & (XFS_NBWORD - 1));
1226        len = start - limit + 1;
1227        /*
1228         * Compute match value, based on the bit at start: if 1 (free)
1229         * then all-ones, else all-zeroes.
1230         */
1231        want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1232        /*
1233         * If the starting position is not word-aligned, deal with the
1234         * partial word.
1235         */
1236        if (bit < XFS_NBWORD - 1) {
1237                /*
1238                 * Calculate first (leftmost) bit number to look at,
1239                 * and mask for all the relevant bits in this word.
1240                 */
1241                firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1242                mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1243                        firstbit;
1244                /*
1245                 * Calculate the difference between the value there
1246                 * and what we're looking for.
1247                 */
1248                if ((wdiff = (*b ^ want) & mask)) {
1249                        /*
1250                         * Different.  Mark where we are and return.
1251                         */
1252                        xfs_trans_brelse(tp, bp);
1253                        i = bit - XFS_RTHIBIT(wdiff);
1254                        *rtblock = start - i + 1;
1255                        return 0;
1256                }
1257                i = bit - firstbit + 1;
1258                /*
1259                 * Go on to previous block if that's where the previous word is
1260                 * and we need the previous word.
1261                 */
1262                if (--word == -1 && i < len) {
1263                        /*
1264                         * If done with this block, get the previous one.
1265                         */
1266                        xfs_trans_brelse(tp, bp);
1267                        error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1268                        if (error) {
1269                                return error;
1270                        }
1271                        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1272                        word = XFS_BLOCKWMASK(mp);
1273                        b = &bufp[word];
1274                } else {
1275                        /*
1276                         * Go on to the previous word in the buffer.
1277                         */
1278                        b--;
1279                }
1280        } else {
1281                /*
1282                 * Starting on a word boundary, no partial word.
1283                 */
1284                i = 0;
1285        }
1286        /*
1287         * Loop over whole words in buffers.  When we use up one buffer
1288         * we move on to the previous one.
1289         */
1290        while (len - i >= XFS_NBWORD) {
1291                /*
1292                 * Compute difference between actual and desired value.
1293                 */
1294                if ((wdiff = *b ^ want)) {
1295                        /*
1296                         * Different, mark where we are and return.
1297                         */
1298                        xfs_trans_brelse(tp, bp);
1299                        i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1300                        *rtblock = start - i + 1;
1301                        return 0;
1302                }
1303                i += XFS_NBWORD;
1304                /*
1305                 * Go on to previous block if that's where the previous word is
1306                 * and we need the previous word.
1307                 */
1308                if (--word == -1 && i < len) {
1309                        /*
1310                         * If done with this block, get the previous one.
1311                         */
1312                        xfs_trans_brelse(tp, bp);
1313                        error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1314                        if (error) {
1315                                return error;
1316                        }
1317                        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1318                        word = XFS_BLOCKWMASK(mp);
1319                        b = &bufp[word];
1320                } else {
1321                        /*
1322                         * Go on to the previous word in the buffer.
1323                         */
1324                        b--;
1325                }
1326        }
1327        /*
1328         * If not ending on a word boundary, deal with the last
1329         * (partial) word.
1330         */
1331        if (len - i) {
1332                /*
1333                 * Calculate first (leftmost) bit number to look at,
1334                 * and mask for all the relevant bits in this word.
1335                 */
1336                firstbit = XFS_NBWORD - (len - i);
1337                mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1338                /*
1339                 * Compute difference between actual and desired value.
1340                 */
1341                if ((wdiff = (*b ^ want) & mask)) {
1342                        /*
1343                         * Different, mark where we are and return.
1344                         */
1345                        xfs_trans_brelse(tp, bp);
1346                        i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1347                        *rtblock = start - i + 1;
1348                        return 0;
1349                } else
1350                        i = len;
1351        }
1352        /*
1353         * No match, return that we scanned the whole area.
1354         */
1355        xfs_trans_brelse(tp, bp);
1356        *rtblock = start - i + 1;
1357        return 0;
1358}
1359
1360/*
1361 * Searching forward from start to limit, find the first block whose
1362 * allocated/free state is different from start's.
1363 */
1364STATIC int                              /* error */
1365xfs_rtfind_forw(
1366        xfs_mount_t     *mp,            /* file system mount point */
1367        xfs_trans_t     *tp,            /* transaction pointer */
1368        xfs_rtblock_t   start,          /* starting block to look at */
1369        xfs_rtblock_t   limit,          /* last block to look at */
1370        xfs_rtblock_t   *rtblock)       /* out: start block found */
1371{
1372        xfs_rtword_t    *b;             /* current word in buffer */
1373        int             bit;            /* bit number in the word */
1374        xfs_rtblock_t   block;          /* bitmap block number */
1375        xfs_buf_t       *bp;            /* buf for the block */
1376        xfs_rtword_t    *bufp;          /* starting word in buffer */
1377        int             error;          /* error value */
1378        xfs_rtblock_t   i;              /* current bit number rel. to start */
1379        xfs_rtblock_t   lastbit;        /* last useful bit in the word */
1380        xfs_rtblock_t   len;            /* length of inspected area */
1381        xfs_rtword_t    mask;           /* mask of relevant bits for value */
1382        xfs_rtword_t    want;           /* mask for "good" values */
1383        xfs_rtword_t    wdiff;          /* difference from wanted value */
1384        int             word;           /* word number in the buffer */
1385
1386        /*
1387         * Compute and read in starting bitmap block for starting block.
1388         */
1389        block = XFS_BITTOBLOCK(mp, start);
1390        error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1391        if (error) {
1392                return error;
1393        }
1394        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1395        /*
1396         * Get the first word's index & point to it.
1397         */
1398        word = XFS_BITTOWORD(mp, start);
1399        b = &bufp[word];
1400        bit = (int)(start & (XFS_NBWORD - 1));
1401        len = limit - start + 1;
1402        /*
1403         * Compute match value, based on the bit at start: if 1 (free)
1404         * then all-ones, else all-zeroes.
1405         */
1406        want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1407        /*
1408         * If the starting position is not word-aligned, deal with the
1409         * partial word.
1410         */
1411        if (bit) {
1412                /*
1413                 * Calculate last (rightmost) bit number to look at,
1414                 * and mask for all the relevant bits in this word.
1415                 */
1416                lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1417                mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1418                /*
1419                 * Calculate the difference between the value there
1420                 * and what we're looking for.
1421                 */
1422                if ((wdiff = (*b ^ want) & mask)) {
1423                        /*
1424                         * Different.  Mark where we are and return.
1425                         */
1426                        xfs_trans_brelse(tp, bp);
1427                        i = XFS_RTLOBIT(wdiff) - bit;
1428                        *rtblock = start + i - 1;
1429                        return 0;
1430                }
1431                i = lastbit - bit;
1432                /*
1433                 * Go on to next block if that's where the next word is
1434                 * and we need the next word.
1435                 */
1436                if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1437                        /*
1438                         * If done with this block, get the previous one.
1439                         */
1440                        xfs_trans_brelse(tp, bp);
1441                        error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1442                        if (error) {
1443                                return error;
1444                        }
1445                        b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1446                        word = 0;
1447                } else {
1448                        /*
1449                         * Go on to the previous word in the buffer.
1450                         */
1451                        b++;
1452                }
1453        } else {
1454                /*
1455                 * Starting on a word boundary, no partial word.
1456                 */
1457                i = 0;
1458        }
1459        /*
1460         * Loop over whole words in buffers.  When we use up one buffer
1461         * we move on to the next one.
1462         */
1463        while (len - i >= XFS_NBWORD) {
1464                /*
1465                 * Compute difference between actual and desired value.
1466                 */
1467                if ((wdiff = *b ^ want)) {
1468                        /*
1469                         * Different, mark where we are and return.
1470                         */
1471                        xfs_trans_brelse(tp, bp);
1472                        i += XFS_RTLOBIT(wdiff);
1473                        *rtblock = start + i - 1;
1474                        return 0;
1475                }
1476                i += XFS_NBWORD;
1477                /*
1478                 * Go on to next block if that's where the next word is
1479                 * and we need the next word.
1480                 */
1481                if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1482                        /*
1483                         * If done with this block, get the next one.
1484                         */
1485                        xfs_trans_brelse(tp, bp);
1486                        error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1487                        if (error) {
1488                                return error;
1489                        }
1490                        b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1491                        word = 0;
1492                } else {
1493                        /*
1494                         * Go on to the next word in the buffer.
1495                         */
1496                        b++;
1497                }
1498        }
1499        /*
1500         * If not ending on a word boundary, deal with the last
1501         * (partial) word.
1502         */
1503        if ((lastbit = len - i)) {
1504                /*
1505                 * Calculate mask for all the relevant bits in this word.
1506                 */
1507                mask = ((xfs_rtword_t)1 << lastbit) - 1;
1508                /*
1509                 * Compute difference between actual and desired value.
1510                 */
1511                if ((wdiff = (*b ^ want) & mask)) {
1512                        /*
1513                         * Different, mark where we are and return.
1514                         */
1515                        xfs_trans_brelse(tp, bp);
1516                        i += XFS_RTLOBIT(wdiff);
1517                        *rtblock = start + i - 1;
1518                        return 0;
1519                } else
1520                        i = len;
1521        }
1522        /*
1523         * No match, return that we scanned the whole area.
1524         */
1525        xfs_trans_brelse(tp, bp);
1526        *rtblock = start + i - 1;
1527        return 0;
1528}
1529
1530/*
1531 * Mark an extent specified by start and len freed.
1532 * Updates all the summary information as well as the bitmap.
1533 */
1534STATIC int                              /* error */
1535xfs_rtfree_range(
1536        xfs_mount_t     *mp,            /* file system mount point */
1537        xfs_trans_t     *tp,            /* transaction pointer */
1538        xfs_rtblock_t   start,          /* starting block to free */
1539        xfs_extlen_t    len,            /* length to free */
1540        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
1541        xfs_fsblock_t   *rsb)           /* in/out: summary block number */
1542{
1543        xfs_rtblock_t   end;            /* end of the freed extent */
1544        int             error;          /* error value */
1545        xfs_rtblock_t   postblock;      /* first block freed > end */
1546        xfs_rtblock_t   preblock;       /* first block freed < start */
1547
1548        end = start + len - 1;
1549        /*
1550         * Modify the bitmap to mark this extent freed.
1551         */
1552        error = xfs_rtmodify_range(mp, tp, start, len, 1);
1553        if (error) {
1554                return error;
1555        }
1556        /*
1557         * Assume we're freeing out of the middle of an allocated extent.
1558         * We need to find the beginning and end of the extent so we can
1559         * properly update the summary.
1560         */
1561        error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1562        if (error) {
1563                return error;
1564        }
1565        /*
1566         * Find the next allocated block (end of allocated extent).
1567         */
1568        error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1569                &postblock);
1570        /*
1571         * If there are blocks not being freed at the front of the
1572         * old extent, add summary data for them to be allocated.
1573         */
1574        if (preblock < start) {
1575                error = xfs_rtmodify_summary(mp, tp,
1576                        XFS_RTBLOCKLOG(start - preblock),
1577                        XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1578                if (error) {
1579                        return error;
1580                }
1581        }
1582        /*
1583         * If there are blocks not being freed at the end of the
1584         * old extent, add summary data for them to be allocated.
1585         */
1586        if (postblock > end) {
1587                error = xfs_rtmodify_summary(mp, tp,
1588                        XFS_RTBLOCKLOG(postblock - end),
1589                        XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1590                if (error) {
1591                        return error;
1592                }
1593        }
1594        /*
1595         * Increment the summary information corresponding to the entire
1596         * (new) free extent.
1597         */
1598        error = xfs_rtmodify_summary(mp, tp,
1599                XFS_RTBLOCKLOG(postblock + 1 - preblock),
1600                XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1601        return error;
1602}
1603
1604/*
1605 * Read and return the summary information for a given extent size,
1606 * bitmap block combination.
1607 * Keeps track of a current summary block, so we don't keep reading
1608 * it from the buffer cache.
1609 */
1610STATIC int                              /* error */
1611xfs_rtget_summary(
1612        xfs_mount_t     *mp,            /* file system mount structure */
1613        xfs_trans_t     *tp,            /* transaction pointer */
1614        int             log,            /* log2 of extent size */
1615        xfs_rtblock_t   bbno,           /* bitmap block number */
1616        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
1617        xfs_fsblock_t   *rsb,           /* in/out: summary block number */
1618        xfs_suminfo_t   *sum)           /* out: summary info for this block */
1619{
1620        xfs_buf_t       *bp;            /* buffer for summary block */
1621        int             error;          /* error value */
1622        xfs_fsblock_t   sb;             /* summary fsblock */
1623        int             so;             /* index into the summary file */
1624        xfs_suminfo_t   *sp;            /* pointer to returned data */
1625
1626        /*
1627         * Compute entry number in the summary file.
1628         */
1629        so = XFS_SUMOFFS(mp, log, bbno);
1630        /*
1631         * Compute the block number in the summary file.
1632         */
1633        sb = XFS_SUMOFFSTOBLOCK(mp, so);
1634        /*
1635         * If we have an old buffer, and the block number matches, use that.
1636         */
1637        if (rbpp && *rbpp && *rsb == sb)
1638                bp = *rbpp;
1639        /*
1640         * Otherwise we have to get the buffer.
1641         */
1642        else {
1643                /*
1644                 * If there was an old one, get rid of it first.
1645                 */
1646                if (rbpp && *rbpp)
1647                        xfs_trans_brelse(tp, *rbpp);
1648                error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1649                if (error) {
1650                        return error;
1651                }
1652                /*
1653                 * Remember this buffer and block for the next call.
1654                 */
1655                if (rbpp) {
1656                        *rbpp = bp;
1657                        *rsb = sb;
1658                }
1659        }
1660        /*
1661         * Point to the summary information & copy it out.
1662         */
1663        sp = XFS_SUMPTR(mp, bp, so);
1664        *sum = *sp;
1665        /*
1666         * Drop the buffer if we're not asked to remember it.
1667         */
1668        if (!rbpp)
1669                xfs_trans_brelse(tp, bp);
1670        return 0;
1671}
1672
1673/*
1674 * Set the given range of bitmap bits to the given value.
1675 * Do whatever I/O and logging is required.
1676 */
1677STATIC int                              /* error */
1678xfs_rtmodify_range(
1679        xfs_mount_t     *mp,            /* file system mount point */
1680        xfs_trans_t     *tp,            /* transaction pointer */
1681        xfs_rtblock_t   start,          /* starting block to modify */
1682        xfs_extlen_t    len,            /* length of extent to modify */
1683        int             val)            /* 1 for free, 0 for allocated */
1684{
1685        xfs_rtword_t    *b;             /* current word in buffer */
1686        int             bit;            /* bit number in the word */
1687        xfs_rtblock_t   block;          /* bitmap block number */
1688        xfs_buf_t       *bp;            /* buf for the block */
1689        xfs_rtword_t    *bufp;          /* starting word in buffer */
1690        int             error;          /* error value */
1691        xfs_rtword_t    *first;         /* first used word in the buffer */
1692        int             i;              /* current bit number rel. to start */
1693        int             lastbit;        /* last useful bit in word */
1694        xfs_rtword_t    mask;           /* mask o frelevant bits for value */
1695        int             word;           /* word number in the buffer */
1696
1697        /*
1698         * Compute starting bitmap block number.
1699         */
1700        block = XFS_BITTOBLOCK(mp, start);
1701        /*
1702         * Read the bitmap block, and point to its data.
1703         */
1704        error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1705        if (error) {
1706                return error;
1707        }
1708        bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1709        /*
1710         * Compute the starting word's address, and starting bit.
1711         */
1712        word = XFS_BITTOWORD(mp, start);
1713        first = b = &bufp[word];
1714        bit = (int)(start & (XFS_NBWORD - 1));
1715        /*
1716         * 0 (allocated) => all zeroes; 1 (free) => all ones.
1717         */
1718        val = -val;
1719        /*
1720         * If not starting on a word boundary, deal with the first
1721         * (partial) word.
1722         */
1723        if (bit) {
1724                /*
1725                 * Compute first bit not changed and mask of relevant bits.
1726                 */
1727                lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1728                mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1729                /*
1730                 * Set/clear the active bits.
1731                 */
1732                if (val)
1733                        *b |= mask;
1734                else
1735                        *b &= ~mask;
1736                i = lastbit - bit;
1737                /*
1738                 * Go on to the next block if that's where the next word is
1739                 * and we need the next word.
1740                 */
1741                if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1742                        /*
1743                         * Log the changed part of this block.
1744                         * Get the next one.
1745                         */
1746                        xfs_trans_log_buf(tp, bp,
1747                                (uint)((char *)first - (char *)bufp),
1748                                (uint)((char *)b - (char *)bufp));
1749                        error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1750                        if (error) {
1751                                return error;
1752                        }
1753                        first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1754                        word = 0;
1755                } else {
1756                        /*
1757                         * Go on to the next word in the buffer
1758                         */
1759                        b++;
1760                }
1761        } else {
1762                /*
1763                 * Starting on a word boundary, no partial word.
1764                 */
1765                i = 0;
1766        }
1767        /*
1768         * Loop over whole words in buffers.  When we use up one buffer
1769         * we move on to the next one.
1770         */
1771        while (len - i >= XFS_NBWORD) {
1772                /*
1773                 * Set the word value correctly.
1774                 */
1775                *b = val;
1776                i += XFS_NBWORD;
1777                /*
1778                 * Go on to the next block if that's where the next word is
1779                 * and we need the next word.
1780                 */
1781                if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1782                        /*
1783                         * Log the changed part of this block.
1784                         * Get the next one.
1785                         */
1786                        xfs_trans_log_buf(tp, bp,
1787                                (uint)((char *)first - (char *)bufp),
1788                                (uint)((char *)b - (char *)bufp));
1789                        error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1790                        if (error) {
1791                                return error;
1792                        }
1793                        first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1794                        word = 0;
1795                } else {
1796                        /*
1797                         * Go on to the next word in the buffer
1798                         */
1799                        b++;
1800                }
1801        }
1802        /*
1803         * If not ending on a word boundary, deal with the last
1804         * (partial) word.
1805         */
1806        if ((lastbit = len - i)) {
1807                /*
1808                 * Compute a mask of relevant bits.
1809                 */
1810                bit = 0;
1811                mask = ((xfs_rtword_t)1 << lastbit) - 1;
1812                /*
1813                 * Set/clear the active bits.
1814                 */
1815                if (val)
1816                        *b |= mask;
1817                else
1818                        *b &= ~mask;
1819                b++;
1820        }
1821        /*
1822         * Log any remaining changed bytes.
1823         */
1824        if (b > first)
1825                xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1826                        (uint)((char *)b - (char *)bufp - 1));
1827        return 0;
1828}
1829
1830/*
1831 * Read and modify the summary information for a given extent size,
1832 * bitmap block combination.
1833 * Keeps track of a current summary block, so we don't keep reading
1834 * it from the buffer cache.
1835 */
1836STATIC int                              /* error */
1837xfs_rtmodify_summary(
1838        xfs_mount_t     *mp,            /* file system mount point */
1839        xfs_trans_t     *tp,            /* transaction pointer */
1840        int             log,            /* log2 of extent size */
1841        xfs_rtblock_t   bbno,           /* bitmap block number */
1842        int             delta,          /* change to make to summary info */
1843        xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
1844        xfs_fsblock_t   *rsb)           /* in/out: summary block number */
1845{
1846        xfs_buf_t       *bp;            /* buffer for the summary block */
1847        int             error;          /* error value */
1848        xfs_fsblock_t   sb;             /* summary fsblock */
1849        int             so;             /* index into the summary file */
1850        xfs_suminfo_t   *sp;            /* pointer to returned data */
1851
1852        /*
1853         * Compute entry number in the summary file.
1854         */
1855        so = XFS_SUMOFFS(mp, log, bbno);
1856        /*
1857         * Compute the block number in the summary file.
1858         */
1859        sb = XFS_SUMOFFSTOBLOCK(mp, so);
1860        /*
1861         * If we have an old buffer, and the block number matches, use that.
1862         */
1863        if (rbpp && *rbpp && *rsb == sb)
1864                bp = *rbpp;
1865        /*
1866         * Otherwise we have to get the buffer.
1867         */
1868        else {
1869                /*
1870                 * If there was an old one, get rid of it first.
1871                 */
1872                if (rbpp && *rbpp)
1873                        xfs_trans_brelse(tp, *rbpp);
1874                error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1875                if (error) {
1876                        return error;
1877                }
1878                /*
1879                 * Remember this buffer and block for the next call.
1880                 */
1881                if (rbpp) {
1882                        *rbpp = bp;
1883                        *rsb = sb;
1884                }
1885        }
1886        /*
1887         * Point to the summary information, modify and log it.
1888         */
1889        sp = XFS_SUMPTR(mp, bp, so);
1890        *sp += delta;
1891        xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1892                (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1893        return 0;
1894}
1895
1896/*
1897 * Visible (exported) functions.
1898 */
1899
1900/*
1901 * Grow the realtime area of the filesystem.
1902 */
1903int
1904xfs_growfs_rt(
1905        xfs_mount_t     *mp,            /* mount point for filesystem */
1906        xfs_growfs_rt_t *in)            /* growfs rt input struct */
1907{
1908        xfs_rtblock_t   bmbno;          /* bitmap block number */
1909        xfs_buf_t       *bp;            /* temporary buffer */
1910        int             cancelflags;    /* flags for xfs_trans_cancel */
1911        int             error;          /* error return value */
1912        xfs_inode_t     *ip;            /* bitmap inode, used as lock */
1913        xfs_mount_t     *nmp;           /* new (fake) mount structure */
1914        xfs_drfsbno_t   nrblocks;       /* new number of realtime blocks */
1915        xfs_extlen_t    nrbmblocks;     /* new number of rt bitmap blocks */
1916        xfs_drtbno_t    nrextents;      /* new number of realtime extents */
1917        uint8_t         nrextslog;      /* new log2 of sb_rextents */
1918        xfs_extlen_t    nrsumblocks;    /* new number of summary blocks */
1919        uint            nrsumlevels;    /* new rt summary levels */
1920        uint            nrsumsize;      /* new size of rt summary, bytes */
1921        xfs_sb_t        *nsbp;          /* new superblock */
1922        xfs_extlen_t    rbmblocks;      /* current number of rt bitmap blocks */
1923        xfs_extlen_t    rsumblocks;     /* current number of rt summary blks */
1924        xfs_sb_t        *sbp;           /* old superblock */
1925        xfs_fsblock_t   sumbno;         /* summary block number */
1926        xfs_trans_t     *tp;            /* transaction pointer */
1927
1928        sbp = &mp->m_sb;
1929        /*
1930         * Initial error checking.
1931         */
1932        if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1933            (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1934            (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1935                return XFS_ERROR(EINVAL);
1936        /*
1937         * Read in the last block of the device, make sure it exists.
1938         */
1939        error = xfs_read_buf(mp, mp->m_rtdev_targp,
1940                        XFS_FSB_TO_BB(mp, in->newblocks - 1),
1941                        XFS_FSB_TO_BB(mp, 1), 0, &bp);
1942        if (error)
1943                return error;
1944        ASSERT(bp);
1945        xfs_buf_relse(bp);
1946        /*
1947         * Calculate new parameters.  These are the final values to be reached.
1948         */
1949        nrextents = nrblocks;
1950        do_div(nrextents, in->extsize);
1951        nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1952        nrextslog = xfs_highbit32(nrextents);
1953        nrsumlevels = nrextslog + 1;
1954        nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1955        nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1956        nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1957        /*
1958         * New summary size can't be more than half the size of
1959         * the log.  This prevents us from getting a log overflow,
1960         * since we'll log basically the whole summary file at once.
1961         */
1962        if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1963                return XFS_ERROR(EINVAL);
1964        /*
1965         * Get the old block counts for bitmap and summary inodes.
1966         * These can't change since other growfs callers are locked out.
1967         */
1968        rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1969        rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1970        /*
1971         * Allocate space to the bitmap and summary files, as necessary.
1972         */
1973        if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1974                        mp->m_sb.sb_rbmino)))
1975                return error;
1976        if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1977                        mp->m_sb.sb_rsumino)))
1978                return error;
1979        /*
1980         * Allocate a new (fake) mount/sb.
1981         */
1982        nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1983        /*
1984         * Loop over the bitmap blocks.
1985         * We will do everything one bitmap block at a time.
1986         * Skip the current block if it is exactly full.
1987         * This also deals with the case where there were no rtextents before.
1988         */
1989        for (bmbno = sbp->sb_rbmblocks -
1990                     ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1991             bmbno < nrbmblocks;
1992             bmbno++) {
1993                *nmp = *mp;
1994                nsbp = &nmp->m_sb;
1995                /*
1996                 * Calculate new sb and mount fields for this round.
1997                 */
1998                nsbp->sb_rextsize = in->extsize;
1999                nsbp->sb_rbmblocks = bmbno + 1;
2000                nsbp->sb_rblocks =
2001                        XFS_RTMIN(nrblocks,
2002                                  nsbp->sb_rbmblocks * NBBY *
2003                                  nsbp->sb_blocksize * nsbp->sb_rextsize);
2004                nsbp->sb_rextents = nsbp->sb_rblocks;
2005                do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
2006                nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2007                nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2008                nrsumsize =
2009                        (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2010                        nsbp->sb_rbmblocks;
2011                nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2012                nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2013                /*
2014                 * Start a transaction, get the log reservation.
2015                 */
2016                tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2017                cancelflags = 0;
2018                if ((error = xfs_trans_reserve(tp, 0,
2019                                XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2020                        break;
2021                /*
2022                 * Lock out other callers by grabbing the bitmap inode lock.
2023                 */
2024                if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2025                                                XFS_ILOCK_EXCL, &ip)))
2026                        break;
2027                ASSERT(ip == mp->m_rbmip);
2028                /*
2029                 * Update the bitmap inode's size.
2030                 */
2031                mp->m_rbmip->i_d.di_size =
2032                        nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2033                xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2034                cancelflags |= XFS_TRANS_ABORT;
2035                /*
2036                 * Get the summary inode into the transaction.
2037                 */
2038                if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
2039                                                XFS_ILOCK_EXCL, &ip)))
2040                        break;
2041                ASSERT(ip == mp->m_rsumip);
2042                /*
2043                 * Update the summary inode's size.
2044                 */
2045                mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2046                xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2047                /*
2048                 * Copy summary data from old to new sizes.
2049                 * Do this when the real size (not block-aligned) changes.
2050                 */
2051                if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2052                    mp->m_rsumlevels != nmp->m_rsumlevels) {
2053                        error = xfs_rtcopy_summary(mp, nmp, tp);
2054                        if (error)
2055                                break;
2056                }
2057                /*
2058                 * Update superblock fields.
2059                 */
2060                if (nsbp->sb_rextsize != sbp->sb_rextsize)
2061                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2062                                nsbp->sb_rextsize - sbp->sb_rextsize);
2063                if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2064                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2065                                nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2066                if (nsbp->sb_rblocks != sbp->sb_rblocks)
2067                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2068                                nsbp->sb_rblocks - sbp->sb_rblocks);
2069                if (nsbp->sb_rextents != sbp->sb_rextents)
2070                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2071                                nsbp->sb_rextents - sbp->sb_rextents);
2072                if (nsbp->sb_rextslog != sbp->sb_rextslog)
2073                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2074                                nsbp->sb_rextslog - sbp->sb_rextslog);
2075                /*
2076                 * Free new extent.
2077                 */
2078                bp = NULL;
2079                error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2080                        nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2081                if (error)
2082                        break;
2083                /*
2084                 * Mark more blocks free in the superblock.
2085                 */
2086                xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2087                        nsbp->sb_rextents - sbp->sb_rextents);
2088                /*
2089                 * Update mp values into the real mp structure.
2090                 */
2091                mp->m_rsumlevels = nrsumlevels;
2092                mp->m_rsumsize = nrsumsize;
2093                /*
2094                 * Commit the transaction.
2095                 */
2096                xfs_trans_commit(tp, 0, NULL);
2097        }
2098
2099        if (error)
2100                xfs_trans_cancel(tp, cancelflags);
2101
2102        /*
2103         * Free the fake mp structure.
2104         */
2105        kmem_free(nmp, sizeof(*nmp));
2106
2107        return error;
2108}
2109
2110/*
2111 * Allocate an extent in the realtime subvolume, with the usual allocation
2112 * parameters.  The length units are all in realtime extents, as is the
2113 * result block number.
2114 */
2115int                                     /* error */
2116xfs_rtallocate_extent(
2117        xfs_trans_t     *tp,            /* transaction pointer */
2118        xfs_rtblock_t   bno,            /* starting block number to allocate */
2119        xfs_extlen_t    minlen,         /* minimum length to allocate */
2120        xfs_extlen_t    maxlen,         /* maximum length to allocate */
2121        xfs_extlen_t    *len,           /* out: actual length allocated */
2122        xfs_alloctype_t type,           /* allocation type XFS_ALLOCTYPE... */
2123        int             wasdel,         /* was a delayed allocation extent */
2124        xfs_extlen_t    prod,           /* extent product factor */
2125        xfs_rtblock_t   *rtblock)       /* out: start block allocated */
2126{
2127        int             error;          /* error value */
2128        xfs_inode_t     *ip;            /* inode for bitmap file */
2129        xfs_mount_t     *mp;            /* file system mount structure */
2130        xfs_rtblock_t   r;              /* result allocated block */
2131        xfs_fsblock_t   sb;             /* summary file block number */
2132        xfs_buf_t       *sumbp;         /* summary file block buffer */
2133
2134        ASSERT(minlen > 0 && minlen <= maxlen);
2135        mp = tp->t_mountp;
2136        /*
2137         * If prod is set then figure out what to do to minlen and maxlen.
2138         */
2139        if (prod > 1) {
2140                xfs_extlen_t    i;
2141
2142                if ((i = maxlen % prod))
2143                        maxlen -= i;
2144                if ((i = minlen % prod))
2145                        minlen += prod - i;
2146                if (maxlen < minlen) {
2147                        *rtblock = NULLRTBLOCK;
2148                        return 0;
2149                }
2150        }
2151        /*
2152         * Lock out other callers by grabbing the bitmap inode lock.
2153         */
2154        if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2155                                        XFS_ILOCK_EXCL, &ip)))
2156                return error;
2157        sumbp = NULL;
2158        /*
2159         * Allocate by size, or near another block, or exactly at some block.
2160         */
2161        switch (type) {
2162        case XFS_ALLOCTYPE_ANY_AG:
2163                error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2164                                &sumbp, &sb, prod, &r);
2165                break;
2166        case XFS_ALLOCTYPE_NEAR_BNO:
2167                error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2168                                len, &sumbp, &sb, prod, &r);
2169                break;
2170        case XFS_ALLOCTYPE_THIS_BNO:
2171                error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2172                                len, &sumbp, &sb, prod, &r);
2173                break;
2174        default:
2175                ASSERT(0);
2176        }
2177        if (error) {
2178                return error;
2179        }
2180        /*
2181         * If it worked, update the superblock.
2182         */
2183        if (r != NULLRTBLOCK) {
2184                long    slen = (long)*len;
2185
2186                ASSERT(*len >= minlen && *len <= maxlen);
2187                if (wasdel)
2188                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2189                else
2190                        xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2191        }
2192        *rtblock = r;
2193        return 0;
2194}
2195
2196/*
2197 * Free an extent in the realtime subvolume.  Length is expressed in
2198 * realtime extents, as is the block number.
2199 */
2200int                                     /* error */
2201xfs_rtfree_extent(
2202        xfs_trans_t     *tp,            /* transaction pointer */
2203        xfs_rtblock_t   bno,            /* starting block number to free */
2204        xfs_extlen_t    len)            /* length of extent freed */
2205{
2206        int             error;          /* error value */
2207        xfs_inode_t     *ip;            /* bitmap file inode */
2208        xfs_mount_t     *mp;            /* file system mount structure */
2209        xfs_fsblock_t   sb;             /* summary file block number */
2210        xfs_buf_t       *sumbp;         /* summary file block buffer */
2211
2212        mp = tp->t_mountp;
2213        /*
2214         * Synchronize by locking the bitmap inode.
2215         */
2216        if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2217                                        XFS_ILOCK_EXCL, &ip)))
2218                return error;
2219#if defined(__KERNEL__) && defined(DEBUG)
2220        /*
2221         * Check to see that this whole range is currently allocated.
2222         */
2223        {
2224                int     stat;           /* result from checking range */
2225
2226                error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2227                if (error) {
2228                        return error;
2229                }
2230                ASSERT(stat);
2231        }
2232#endif
2233        sumbp = NULL;
2234        /*
2235         * Free the range of realtime blocks.
2236         */
2237        error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2238        if (error) {
2239                return error;
2240        }
2241        /*
2242         * Mark more blocks free in the superblock.
2243         */
2244        xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2245        /*
2246         * If we've now freed all the blocks, reset the file sequence
2247         * number to 0.
2248         */
2249        if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2250            mp->m_sb.sb_rextents) {
2251                if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2252                        ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2253                *(__uint64_t *)&ip->i_d.di_atime = 0;
2254                xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2255        }
2256        return 0;
2257}
2258
2259/*
2260 * Initialize realtime fields in the mount structure.
2261 */
2262int                             /* error */
2263xfs_rtmount_init(
2264        xfs_mount_t     *mp)    /* file system mount structure */
2265{
2266        xfs_buf_t       *bp;    /* buffer for last block of subvolume */
2267        xfs_daddr_t     d;      /* address of last block of subvolume */
2268        int             error;  /* error return value */
2269        xfs_sb_t        *sbp;   /* filesystem superblock copy in mount */
2270
2271        sbp = &mp->m_sb;
2272        if (sbp->sb_rblocks == 0)
2273                return 0;
2274        if (mp->m_rtdev_targp == NULL) {
2275                cmn_err(CE_WARN,
2276        "XFS: This filesystem has a realtime volume, use rtdev=device option");
2277                return XFS_ERROR(ENODEV);
2278        }
2279        mp->m_rsumlevels = sbp->sb_rextslog + 1;
2280        mp->m_rsumsize =
2281                (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2282                sbp->sb_rbmblocks;
2283        mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2284        mp->m_rbmip = mp->m_rsumip = NULL;
2285        /*
2286         * Check that the realtime section is an ok size.
2287         */
2288        d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2289        if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2290                cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2291                        (unsigned long long) XFS_BB_TO_FSB(mp, d),
2292                        (unsigned long long) mp->m_sb.sb_rblocks);
2293                return XFS_ERROR(E2BIG);
2294        }
2295        error = xfs_read_buf(mp, mp->m_rtdev_targp,
2296                                d - XFS_FSB_TO_BB(mp, 1),
2297                                XFS_FSB_TO_BB(mp, 1), 0, &bp);
2298        if (error) {
2299                cmn_err(CE_WARN,
2300        "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2301                if (error == ENOSPC)
2302                        return XFS_ERROR(E2BIG);
2303                return error;
2304        }
2305        xfs_buf_relse(bp);
2306        return 0;
2307}
2308
2309/*
2310 * Get the bitmap and summary inodes into the mount structure
2311 * at mount time.
2312 */
2313int                                     /* error */
2314xfs_rtmount_inodes(
2315        xfs_mount_t     *mp)            /* file system mount structure */
2316{
2317        int             error;          /* error return value */
2318        xfs_sb_t        *sbp;
2319
2320        sbp = &mp->m_sb;
2321        if (sbp->sb_rbmino == NULLFSINO)
2322                return 0;
2323        error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2324        if (error)
2325                return error;
2326        ASSERT(mp->m_rbmip != NULL);
2327        ASSERT(sbp->sb_rsumino != NULLFSINO);
2328        error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2329        if (error) {
2330                VN_RELE(XFS_ITOV(mp->m_rbmip));
2331                return error;
2332        }
2333        ASSERT(mp->m_rsumip != NULL);
2334        return 0;
2335}
2336
2337/*
2338 * Pick an extent for allocation at the start of a new realtime file.
2339 * Use the sequence number stored in the atime field of the bitmap inode.
2340 * Translate this to a fraction of the rtextents, and return the product
2341 * of rtextents and the fraction.
2342 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2343 */
2344int                                     /* error */
2345xfs_rtpick_extent(
2346        xfs_mount_t     *mp,            /* file system mount point */
2347        xfs_trans_t     *tp,            /* transaction pointer */
2348        xfs_extlen_t    len,            /* allocation length (rtextents) */
2349        xfs_rtblock_t   *pick)          /* result rt extent */
2350{
2351        xfs_rtblock_t   b;              /* result block */
2352        int             error;          /* error return value */
2353        xfs_inode_t     *ip;            /* bitmap incore inode */
2354        int             log2;           /* log of sequence number */
2355        __uint64_t      resid;          /* residual after log removed */
2356        __uint64_t      seq;            /* sequence number of file creation */
2357        __uint64_t      *seqp;          /* pointer to seqno in inode */
2358
2359        if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2360                                        XFS_ILOCK_EXCL, &ip)))
2361                return error;
2362        ASSERT(ip == mp->m_rbmip);
2363        seqp = (__uint64_t *)&ip->i_d.di_atime;
2364        if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2365                ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2366                *seqp = 0;
2367        }
2368        seq = *seqp;
2369        if ((log2 = xfs_highbit64(seq)) == -1)
2370                b = 0;
2371        else {
2372                resid = seq - (1ULL << log2);
2373                b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2374                    (log2 + 1);
2375                if (b >= mp->m_sb.sb_rextents)
2376                        b = do_mod(b, mp->m_sb.sb_rextents);
2377                if (b + len > mp->m_sb.sb_rextents)
2378                        b = mp->m_sb.sb_rextents - len;
2379        }
2380        *seqp = seq + 1;
2381        xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2382        *pick = b;
2383        return 0;
2384}
2385
2386#ifdef DEBUG
2387/*
2388 * Debug code: print out the value of a range in the bitmap.
2389 */
2390void
2391xfs_rtprint_range(
2392        xfs_mount_t     *mp,            /* file system mount structure */
2393        xfs_trans_t     *tp,            /* transaction pointer */
2394        xfs_rtblock_t   start,          /* starting block to print */
2395        xfs_extlen_t    len)            /* length to print */
2396{
2397        xfs_extlen_t    i;              /* block number in the extent */
2398
2399        cmn_err(CE_DEBUG, "%Ld: ", (long long)start);
2400        for (i = 0; i < len; i++)
2401                cmn_err(CE_DEBUG, "%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2402        cmn_err(CE_DEBUG, "\n");
2403}
2404
2405/*
2406 * Debug code: print the summary file.
2407 */
2408void
2409xfs_rtprint_summary(
2410        xfs_mount_t     *mp,            /* file system mount structure */
2411        xfs_trans_t     *tp)            /* transaction pointer */
2412{
2413        xfs_suminfo_t   c;              /* summary data */
2414        xfs_rtblock_t   i;              /* bitmap block number */
2415        int             l;              /* summary information level */
2416        int             p;              /* flag for printed anything */
2417        xfs_fsblock_t   sb;             /* summary block number */
2418        xfs_buf_t       *sumbp;         /* summary block buffer */
2419
2420        sumbp = NULL;
2421        for (l = 0; l < mp->m_rsumlevels; l++) {
2422                for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2423                        (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2424                        if (c) {
2425                                if (!p) {
2426                                        cmn_err(CE_DEBUG, "%Ld-%Ld:", 1LL << l,
2427                                                XFS_RTMIN((1LL << l) +
2428                                                          ((1LL << l) - 1LL),
2429                                                         mp->m_sb.sb_rextents));
2430                                        p = 1;
2431                                }
2432                                cmn_err(CE_DEBUG, " %Ld:%d", (long long)i, c);
2433                        }
2434                }
2435                if (p)
2436                        cmn_err(CE_DEBUG, "\n");
2437        }
2438        if (sumbp)
2439                xfs_trans_brelse(tp, sumbp);
2440}
2441#endif  /* DEBUG */
2442
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.