linux/fs/xfs/xfs_ialloc.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000-2002,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_mount.h"
  28#include "xfs_bmap_btree.h"
  29#include "xfs_alloc_btree.h"
  30#include "xfs_ialloc_btree.h"
  31#include "xfs_dinode.h"
  32#include "xfs_inode.h"
  33#include "xfs_btree.h"
  34#include "xfs_ialloc.h"
  35#include "xfs_alloc.h"
  36#include "xfs_rtalloc.h"
  37#include "xfs_error.h"
  38#include "xfs_bmap.h"
  39#include "xfs_cksum.h"
  40#include "xfs_buf_item.h"
  41#include "xfs_icreate_item.h"
  42
  43
  44/*
  45 * Allocation group level functions.
  46 */
  47static inline int
  48xfs_ialloc_cluster_alignment(
  49        xfs_alloc_arg_t *args)
  50{
  51        if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
  52            args->mp->m_sb.sb_inoalignmt >=
  53             XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
  54                return args->mp->m_sb.sb_inoalignmt;
  55        return 1;
  56}
  57
  58/*
  59 * Lookup a record by ino in the btree given by cur.
  60 */
  61int                                     /* error */
  62xfs_inobt_lookup(
  63        struct xfs_btree_cur    *cur,   /* btree cursor */
  64        xfs_agino_t             ino,    /* starting inode of chunk */
  65        xfs_lookup_t            dir,    /* <=, >=, == */
  66        int                     *stat)  /* success/failure */
  67{
  68        cur->bc_rec.i.ir_startino = ino;
  69        cur->bc_rec.i.ir_freecount = 0;
  70        cur->bc_rec.i.ir_free = 0;
  71        return xfs_btree_lookup(cur, dir, stat);
  72}
  73
  74/*
  75 * Update the record referred to by cur to the value given.
  76 * This either works (return 0) or gets an EFSCORRUPTED error.
  77 */
  78STATIC int                              /* error */
  79xfs_inobt_update(
  80        struct xfs_btree_cur    *cur,   /* btree cursor */
  81        xfs_inobt_rec_incore_t  *irec)  /* btree record */
  82{
  83        union xfs_btree_rec     rec;
  84
  85        rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
  86        rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
  87        rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
  88        return xfs_btree_update(cur, &rec);
  89}
  90
  91/*
  92 * Get the data from the pointed-to record.
  93 */
  94int                                     /* error */
  95xfs_inobt_get_rec(
  96        struct xfs_btree_cur    *cur,   /* btree cursor */
  97        xfs_inobt_rec_incore_t  *irec,  /* btree record */
  98        int                     *stat)  /* output: success/failure */
  99{
 100        union xfs_btree_rec     *rec;
 101        int                     error;
 102
 103        error = xfs_btree_get_rec(cur, &rec, stat);
 104        if (!error && *stat == 1) {
 105                irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
 106                irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
 107                irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
 108        }
 109        return error;
 110}
 111
 112/*
 113 * Verify that the number of free inodes in the AGI is correct.
 114 */
 115#ifdef DEBUG
 116STATIC int
 117xfs_check_agi_freecount(
 118        struct xfs_btree_cur    *cur,
 119        struct xfs_agi          *agi)
 120{
 121        if (cur->bc_nlevels == 1) {
 122                xfs_inobt_rec_incore_t rec;
 123                int             freecount = 0;
 124                int             error;
 125                int             i;
 126
 127                error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
 128                if (error)
 129                        return error;
 130
 131                do {
 132                        error = xfs_inobt_get_rec(cur, &rec, &i);
 133                        if (error)
 134                                return error;
 135
 136                        if (i) {
 137                                freecount += rec.ir_freecount;
 138                                error = xfs_btree_increment(cur, 0, &i);
 139                                if (error)
 140                                        return error;
 141                        }
 142                } while (i == 1);
 143
 144                if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
 145                        ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
 146        }
 147        return 0;
 148}
 149#else
 150#define xfs_check_agi_freecount(cur, agi)       0
 151#endif
 152
 153/*
 154 * Initialise a new set of inodes. When called without a transaction context
 155 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
 156 * than logging them (which in a transaction context puts them into the AIL
 157 * for writeback rather than the xfsbufd queue).
 158 */
 159int
 160xfs_ialloc_inode_init(
 161        struct xfs_mount        *mp,
 162        struct xfs_trans        *tp,
 163        struct list_head        *buffer_list,
 164        xfs_agnumber_t          agno,
 165        xfs_agblock_t           agbno,
 166        xfs_agblock_t           length,
 167        unsigned int            gen)
 168{
 169        struct xfs_buf          *fbuf;
 170        struct xfs_dinode       *free;
 171        int                     blks_per_cluster, nbufs, ninodes;
 172        int                     version;
 173        int                     i, j;
 174        xfs_daddr_t             d;
 175        xfs_ino_t               ino = 0;
 176
 177        /*
 178         * Loop over the new block(s), filling in the inodes.
 179         * For small block sizes, manipulate the inodes in buffers
 180         * which are multiples of the blocks size.
 181         */
 182        if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
 183                blks_per_cluster = 1;
 184                nbufs = length;
 185                ninodes = mp->m_sb.sb_inopblock;
 186        } else {
 187                blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
 188                                   mp->m_sb.sb_blocksize;
 189                nbufs = length / blks_per_cluster;
 190                ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
 191        }
 192
 193        /*
 194         * Figure out what version number to use in the inodes we create.  If
 195         * the superblock version has caught up to the one that supports the new
 196         * inode format, then use the new inode version.  Otherwise use the old
 197         * version so that old kernels will continue to be able to use the file
 198         * system.
 199         *
 200         * For v3 inodes, we also need to write the inode number into the inode,
 201         * so calculate the first inode number of the chunk here as
 202         * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
 203         * across multiple filesystem blocks (such as a cluster) and so cannot
 204         * be used in the cluster buffer loop below.
 205         *
 206         * Further, because we are writing the inode directly into the buffer
 207         * and calculating a CRC on the entire inode, we have ot log the entire
 208         * inode so that the entire range the CRC covers is present in the log.
 209         * That means for v3 inode we log the entire buffer rather than just the
 210         * inode cores.
 211         */
 212        if (xfs_sb_version_hascrc(&mp->m_sb)) {
 213                version = 3;
 214                ino = XFS_AGINO_TO_INO(mp, agno,
 215                                       XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
 216
 217                /*
 218                 * log the initialisation that is about to take place as an
 219                 * logical operation. This means the transaction does not
 220                 * need to log the physical changes to the inode buffers as log
 221                 * recovery will know what initialisation is actually needed.
 222                 * Hence we only need to log the buffers as "ordered" buffers so
 223                 * they track in the AIL as if they were physically logged.
 224                 */
 225                if (tp)
 226                        xfs_icreate_log(tp, agno, agbno, XFS_IALLOC_INODES(mp),
 227                                        mp->m_sb.sb_inodesize, length, gen);
 228        } else if (xfs_sb_version_hasnlink(&mp->m_sb))
 229                version = 2;
 230        else
 231                version = 1;
 232
 233        for (j = 0; j < nbufs; j++) {
 234                /*
 235                 * Get the block.
 236                 */
 237                d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
 238                fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
 239                                         mp->m_bsize * blks_per_cluster,
 240                                         XBF_UNMAPPED);
 241                if (!fbuf)
 242                        return ENOMEM;
 243
 244                /* Initialize the inode buffers and log them appropriately. */
 245                fbuf->b_ops = &xfs_inode_buf_ops;
 246                xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
 247                for (i = 0; i < ninodes; i++) {
 248                        int     ioffset = i << mp->m_sb.sb_inodelog;
 249                        uint    isize = xfs_dinode_size(version);
 250
 251                        free = xfs_make_iptr(mp, fbuf, i);
 252                        free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
 253                        free->di_version = version;
 254                        free->di_gen = cpu_to_be32(gen);
 255                        free->di_next_unlinked = cpu_to_be32(NULLAGINO);
 256
 257                        if (version == 3) {
 258                                free->di_ino = cpu_to_be64(ino);
 259                                ino++;
 260                                uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
 261                                xfs_dinode_calc_crc(mp, free);
 262                        } else if (tp) {
 263                                /* just log the inode core */
 264                                xfs_trans_log_buf(tp, fbuf, ioffset,
 265                                                  ioffset + isize - 1);
 266                        }
 267                }
 268
 269                if (tp) {
 270                        /*
 271                         * Mark the buffer as an inode allocation buffer so it
 272                         * sticks in AIL at the point of this allocation
 273                         * transaction. This ensures the they are on disk before
 274                         * the tail of the log can be moved past this
 275                         * transaction (i.e. by preventing relogging from moving
 276                         * it forward in the log).
 277                         */
 278                        xfs_trans_inode_alloc_buf(tp, fbuf);
 279                        if (version == 3) {
 280                                /*
 281                                 * Mark the buffer as ordered so that they are
 282                                 * not physically logged in the transaction but
 283                                 * still tracked in the AIL as part of the
 284                                 * transaction and pin the log appropriately.
 285                                 */
 286                                xfs_trans_ordered_buf(tp, fbuf);
 287                                xfs_trans_log_buf(tp, fbuf, 0,
 288                                                  BBTOB(fbuf->b_length) - 1);
 289                        }
 290                } else {
 291                        fbuf->b_flags |= XBF_DONE;
 292                        xfs_buf_delwri_queue(fbuf, buffer_list);
 293                        xfs_buf_relse(fbuf);
 294                }
 295        }
 296        return 0;
 297}
 298
 299/*
 300 * Allocate new inodes in the allocation group specified by agbp.
 301 * Return 0 for success, else error code.
 302 */
 303STATIC int                              /* error code or 0 */
 304xfs_ialloc_ag_alloc(
 305        xfs_trans_t     *tp,            /* transaction pointer */
 306        xfs_buf_t       *agbp,          /* alloc group buffer */
 307        int             *alloc)
 308{
 309        xfs_agi_t       *agi;           /* allocation group header */
 310        xfs_alloc_arg_t args;           /* allocation argument structure */
 311        xfs_btree_cur_t *cur;           /* inode btree cursor */
 312        xfs_agnumber_t  agno;
 313        int             error;
 314        int             i;
 315        xfs_agino_t     newino;         /* new first inode's number */
 316        xfs_agino_t     newlen;         /* new number of inodes */
 317        xfs_agino_t     thisino;        /* current inode number, for loop */
 318        int             isaligned = 0;  /* inode allocation at stripe unit */
 319                                        /* boundary */
 320        struct xfs_perag *pag;
 321
 322        memset(&args, 0, sizeof(args));
 323        args.tp = tp;
 324        args.mp = tp->t_mountp;
 325
 326        /*
 327         * Locking will ensure that we don't have two callers in here
 328         * at one time.
 329         */
 330        newlen = XFS_IALLOC_INODES(args.mp);
 331        if (args.mp->m_maxicount &&
 332            args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
 333                return XFS_ERROR(ENOSPC);
 334        args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
 335        /*
 336         * First try to allocate inodes contiguous with the last-allocated
 337         * chunk of inodes.  If the filesystem is striped, this will fill
 338         * an entire stripe unit with inodes.
 339         */
 340        agi = XFS_BUF_TO_AGI(agbp);
 341        newino = be32_to_cpu(agi->agi_newino);
 342        agno = be32_to_cpu(agi->agi_seqno);
 343        args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
 344                        XFS_IALLOC_BLOCKS(args.mp);
 345        if (likely(newino != NULLAGINO &&
 346                  (args.agbno < be32_to_cpu(agi->agi_length)))) {
 347                args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
 348                args.type = XFS_ALLOCTYPE_THIS_BNO;
 349                args.prod = 1;
 350
 351                /*
 352                 * We need to take into account alignment here to ensure that
 353                 * we don't modify the free list if we fail to have an exact
 354                 * block. If we don't have an exact match, and every oher
 355                 * attempt allocation attempt fails, we'll end up cancelling
 356                 * a dirty transaction and shutting down.
 357                 *
 358                 * For an exact allocation, alignment must be 1,
 359                 * however we need to take cluster alignment into account when
 360                 * fixing up the freelist. Use the minalignslop field to
 361                 * indicate that extra blocks might be required for alignment,
 362                 * but not to use them in the actual exact allocation.
 363                 */
 364                args.alignment = 1;
 365                args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
 366
 367                /* Allow space for the inode btree to split. */
 368                args.minleft = args.mp->m_in_maxlevels - 1;
 369                if ((error = xfs_alloc_vextent(&args)))
 370                        return error;
 371        } else
 372                args.fsbno = NULLFSBLOCK;
 373
 374        if (unlikely(args.fsbno == NULLFSBLOCK)) {
 375                /*
 376                 * Set the alignment for the allocation.
 377                 * If stripe alignment is turned on then align at stripe unit
 378                 * boundary.
 379                 * If the cluster size is smaller than a filesystem block
 380                 * then we're doing I/O for inodes in filesystem block size
 381                 * pieces, so don't need alignment anyway.
 382                 */
 383                isaligned = 0;
 384                if (args.mp->m_sinoalign) {
 385                        ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
 386                        args.alignment = args.mp->m_dalign;
 387                        isaligned = 1;
 388                } else
 389                        args.alignment = xfs_ialloc_cluster_alignment(&args);
 390                /*
 391                 * Need to figure out where to allocate the inode blocks.
 392                 * Ideally they should be spaced out through the a.g.
 393                 * For now, just allocate blocks up front.
 394                 */
 395                args.agbno = be32_to_cpu(agi->agi_root);
 396                args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
 397                /*
 398                 * Allocate a fixed-size extent of inodes.
 399                 */
 400                args.type = XFS_ALLOCTYPE_NEAR_BNO;
 401                args.prod = 1;
 402                /*
 403                 * Allow space for the inode btree to split.
 404                 */
 405                args.minleft = args.mp->m_in_maxlevels - 1;
 406                if ((error = xfs_alloc_vextent(&args)))
 407                        return error;
 408        }
 409
 410        /*
 411         * If stripe alignment is turned on, then try again with cluster
 412         * alignment.
 413         */
 414        if (isaligned && args.fsbno == NULLFSBLOCK) {
 415                args.type = XFS_ALLOCTYPE_NEAR_BNO;
 416                args.agbno = be32_to_cpu(agi->agi_root);
 417                args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
 418                args.alignment = xfs_ialloc_cluster_alignment(&args);
 419                if ((error = xfs_alloc_vextent(&args)))
 420                        return error;
 421        }
 422
 423        if (args.fsbno == NULLFSBLOCK) {
 424                *alloc = 0;
 425                return 0;
 426        }
 427        ASSERT(args.len == args.minlen);
 428
 429        /*
 430         * Stamp and write the inode buffers.
 431         *
 432         * Seed the new inode cluster with a random generation number. This
 433         * prevents short-term reuse of generation numbers if a chunk is
 434         * freed and then immediately reallocated. We use random numbers
 435         * rather than a linear progression to prevent the next generation
 436         * number from being easily guessable.
 437         */
 438        error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
 439                        args.len, prandom_u32());
 440
 441        if (error)
 442                return error;
 443        /*
 444         * Convert the results.
 445         */
 446        newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
 447        be32_add_cpu(&agi->agi_count, newlen);
 448        be32_add_cpu(&agi->agi_freecount, newlen);
 449        pag = xfs_perag_get(args.mp, agno);
 450        pag->pagi_freecount += newlen;
 451        xfs_perag_put(pag);
 452        agi->agi_newino = cpu_to_be32(newino);
 453
 454        /*
 455         * Insert records describing the new inode chunk into the btree.
 456         */
 457        cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
 458        for (thisino = newino;
 459             thisino < newino + newlen;
 460             thisino += XFS_INODES_PER_CHUNK) {
 461                cur->bc_rec.i.ir_startino = thisino;
 462                cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
 463                cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
 464                error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
 465                if (error) {
 466                        xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
 467                        return error;
 468                }
 469                ASSERT(i == 0);
 470                error = xfs_btree_insert(cur, &i);
 471                if (error) {
 472                        xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
 473                        return error;
 474                }
 475                ASSERT(i == 1);
 476        }
 477        xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
 478        /*
 479         * Log allocation group header fields
 480         */
 481        xfs_ialloc_log_agi(tp, agbp,
 482                XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
 483        /*
 484         * Modify/log superblock values for inode count and inode free count.
 485         */
 486        xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
 487        xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
 488        *alloc = 1;
 489        return 0;
 490}
 491
 492STATIC xfs_agnumber_t
 493xfs_ialloc_next_ag(
 494        xfs_mount_t     *mp)
 495{
 496        xfs_agnumber_t  agno;
 497
 498        spin_lock(&mp->m_agirotor_lock);
 499        agno = mp->m_agirotor;
 500        if (++mp->m_agirotor >= mp->m_maxagi)
 501                mp->m_agirotor = 0;
 502        spin_unlock(&mp->m_agirotor_lock);
 503
 504        return agno;
 505}
 506
 507/*
 508 * Select an allocation group to look for a free inode in, based on the parent
 509 * inode and then mode.  Return the allocation group buffer.
 510 */
 511STATIC xfs_agnumber_t
 512xfs_ialloc_ag_select(
 513        xfs_trans_t     *tp,            /* transaction pointer */
 514        xfs_ino_t       parent,         /* parent directory inode number */
 515        umode_t         mode,           /* bits set to indicate file type */
 516        int             okalloc)        /* ok to allocate more space */
 517{
 518        xfs_agnumber_t  agcount;        /* number of ag's in the filesystem */
 519        xfs_agnumber_t  agno;           /* current ag number */
 520        int             flags;          /* alloc buffer locking flags */
 521        xfs_extlen_t    ineed;          /* blocks needed for inode allocation */
 522        xfs_extlen_t    longest = 0;    /* longest extent available */
 523        xfs_mount_t     *mp;            /* mount point structure */
 524        int             needspace;      /* file mode implies space allocated */
 525        xfs_perag_t     *pag;           /* per allocation group data */
 526        xfs_agnumber_t  pagno;          /* parent (starting) ag number */
 527        int             error;
 528
 529        /*
 530         * Files of these types need at least one block if length > 0
 531         * (and they won't fit in the inode, but that's hard to figure out).
 532         */
 533        needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
 534        mp = tp->t_mountp;
 535        agcount = mp->m_maxagi;
 536        if (S_ISDIR(mode))
 537                pagno = xfs_ialloc_next_ag(mp);
 538        else {
 539                pagno = XFS_INO_TO_AGNO(mp, parent);
 540                if (pagno >= agcount)
 541                        pagno = 0;
 542        }
 543
 544        ASSERT(pagno < agcount);
 545
 546        /*
 547         * Loop through allocation groups, looking for one with a little
 548         * free space in it.  Note we don't look for free inodes, exactly.
 549         * Instead, we include whether there is a need to allocate inodes
 550         * to mean that blocks must be allocated for them,
 551         * if none are currently free.
 552         */
 553        agno = pagno;
 554        flags = XFS_ALLOC_FLAG_TRYLOCK;
 555        for (;;) {
 556                pag = xfs_perag_get(mp, agno);
 557                if (!pag->pagi_inodeok) {
 558                        xfs_ialloc_next_ag(mp);
 559                        goto nextag;
 560                }
 561
 562                if (!pag->pagi_init) {
 563                        error = xfs_ialloc_pagi_init(mp, tp, agno);
 564                        if (error)
 565                                goto nextag;
 566                }
 567
 568                if (pag->pagi_freecount) {
 569                        xfs_perag_put(pag);
 570                        return agno;
 571                }
 572
 573                if (!okalloc)
 574                        goto nextag;
 575
 576                if (!pag->pagf_init) {
 577                        error = xfs_alloc_pagf_init(mp, tp, agno, flags);
 578                        if (error)
 579                                goto nextag;
 580                }
 581
 582                /*
 583                 * Is there enough free space for the file plus a block of
 584                 * inodes? (if we need to allocate some)?
 585                 */
 586                ineed = XFS_IALLOC_BLOCKS(mp);
 587                longest = pag->pagf_longest;
 588                if (!longest)
 589                        longest = pag->pagf_flcount > 0;
 590
 591                if (pag->pagf_freeblks >= needspace + ineed &&
 592                    longest >= ineed) {
 593                        xfs_perag_put(pag);
 594                        return agno;
 595                }
 596nextag:
 597                xfs_perag_put(pag);
 598                /*
 599                 * No point in iterating over the rest, if we're shutting
 600                 * down.
 601                 */
 602                if (XFS_FORCED_SHUTDOWN(mp))
 603                        return NULLAGNUMBER;
 604                agno++;
 605                if (agno >= agcount)
 606                        agno = 0;
 607                if (agno == pagno) {
 608                        if (flags == 0)
 609                                return NULLAGNUMBER;
 610                        flags = 0;
 611                }
 612        }
 613}
 614
 615/*
 616 * Try to retrieve the next record to the left/right from the current one.
 617 */
 618STATIC int
 619xfs_ialloc_next_rec(
 620        struct xfs_btree_cur    *cur,
 621        xfs_inobt_rec_incore_t  *rec,
 622        int                     *done,
 623        int                     left)
 624{
 625        int                     error;
 626        int                     i;
 627
 628        if (left)
 629                error = xfs_btree_decrement(cur, 0, &i);
 630        else
 631                error = xfs_btree_increment(cur, 0, &i);
 632
 633        if (error)
 634                return error;
 635        *done = !i;
 636        if (i) {
 637                error = xfs_inobt_get_rec(cur, rec, &i);
 638                if (error)
 639                        return error;
 640                XFS_WANT_CORRUPTED_RETURN(i == 1);
 641        }
 642
 643        return 0;
 644}
 645
 646STATIC int
 647xfs_ialloc_get_rec(
 648        struct xfs_btree_cur    *cur,
 649        xfs_agino_t             agino,
 650        xfs_inobt_rec_incore_t  *rec,
 651        int                     *done)
 652{
 653        int                     error;
 654        int                     i;
 655
 656        error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
 657        if (error)
 658                return error;
 659        *done = !i;
 660        if (i) {
 661                error = xfs_inobt_get_rec(cur, rec, &i);
 662                if (error)
 663                        return error;
 664                XFS_WANT_CORRUPTED_RETURN(i == 1);
 665        }
 666
 667        return 0;
 668}
 669
 670/*
 671 * Allocate an inode.
 672 *
 673 * The caller selected an AG for us, and made sure that free inodes are
 674 * available.
 675 */
 676STATIC int
 677xfs_dialloc_ag(
 678        struct xfs_trans        *tp,
 679        struct xfs_buf          *agbp,
 680        xfs_ino_t               parent,
 681        xfs_ino_t               *inop)
 682{
 683        struct xfs_mount        *mp = tp->t_mountp;
 684        struct xfs_agi          *agi = XFS_BUF_TO_AGI(agbp);
 685        xfs_agnumber_t          agno = be32_to_cpu(agi->agi_seqno);
 686        xfs_agnumber_t          pagno = XFS_INO_TO_AGNO(mp, parent);
 687        xfs_agino_t             pagino = XFS_INO_TO_AGINO(mp, parent);
 688        struct xfs_perag        *pag;
 689        struct xfs_btree_cur    *cur, *tcur;
 690        struct xfs_inobt_rec_incore rec, trec;
 691        xfs_ino_t               ino;
 692        int                     error;
 693        int                     offset;
 694        int                     i, j;
 695
 696        pag = xfs_perag_get(mp, agno);
 697
 698        ASSERT(pag->pagi_init);
 699        ASSERT(pag->pagi_inodeok);
 700        ASSERT(pag->pagi_freecount > 0);
 701
 702 restart_pagno:
 703        cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
 704        /*
 705         * If pagino is 0 (this is the root inode allocation) use newino.
 706         * This must work because we've just allocated some.
 707         */
 708        if (!pagino)
 709                pagino = be32_to_cpu(agi->agi_newino);
 710
 711        error = xfs_check_agi_freecount(cur, agi);
 712        if (error)
 713                goto error0;
 714
 715        /*
 716         * If in the same AG as the parent, try to get near the parent.
 717         */
 718        if (pagno == agno) {
 719                int             doneleft;       /* done, to the left */
 720                int             doneright;      /* done, to the right */
 721                int             searchdistance = 10;
 722
 723                error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
 724                if (error)
 725                        goto error0;
 726                XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
 727
 728                error = xfs_inobt_get_rec(cur, &rec, &j);
 729                if (error)
 730                        goto error0;
 731                XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
 732
 733                if (rec.ir_freecount > 0) {
 734                        /*
 735                         * Found a free inode in the same chunk
 736                         * as the parent, done.
 737                         */
 738                        goto alloc_inode;
 739                }
 740
 741
 742                /*
 743                 * In the same AG as parent, but parent's chunk is full.
 744                 */
 745
 746                /* duplicate the cursor, search left & right simultaneously */
 747                error = xfs_btree_dup_cursor(cur, &tcur);
 748                if (error)
 749                        goto error0;
 750
 751                /*
 752                 * Skip to last blocks looked up if same parent inode.
 753                 */
 754                if (pagino != NULLAGINO &&
 755                    pag->pagl_pagino == pagino &&
 756                    pag->pagl_leftrec != NULLAGINO &&
 757                    pag->pagl_rightrec != NULLAGINO) {
 758                        error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
 759                                                   &trec, &doneleft);
 760                        if (error)
 761                                goto error1;
 762
 763                        error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
 764                                                   &rec, &doneright);
 765                        if (error)
 766                                goto error1;
 767                } else {
 768                        /* search left with tcur, back up 1 record */
 769                        error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
 770                        if (error)
 771                                goto error1;
 772
 773                        /* search right with cur, go forward 1 record. */
 774                        error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
 775                        if (error)
 776                                goto error1;
 777                }
 778
 779                /*
 780                 * Loop until we find an inode chunk with a free inode.
 781                 */
 782                while (!doneleft || !doneright) {
 783                        int     useleft;  /* using left inode chunk this time */
 784
 785                        if (!--searchdistance) {
 786                                /*
 787                                 * Not in range - save last search
 788                                 * location and allocate a new inode
 789                                 */
 790                                xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
 791                                pag->pagl_leftrec = trec.ir_startino;
 792                                pag->pagl_rightrec = rec.ir_startino;
 793                                pag->pagl_pagino = pagino;
 794                                goto newino;
 795                        }
 796
 797                        /* figure out the closer block if both are valid. */
 798                        if (!doneleft && !doneright) {
 799                                useleft = pagino -
 800                                 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
 801                                  rec.ir_startino - pagino;
 802                        } else {
 803                                useleft = !doneleft;
 804                        }
 805
 806                        /* free inodes to the left? */
 807                        if (useleft && trec.ir_freecount) {
 808                                rec = trec;
 809                                xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
 810                                cur = tcur;
 811
 812                                pag->pagl_leftrec = trec.ir_startino;
 813                                pag->pagl_rightrec = rec.ir_startino;
 814                                pag->pagl_pagino = pagino;
 815                                goto alloc_inode;
 816                        }
 817
 818                        /* free inodes to the right? */
 819                        if (!useleft && rec.ir_freecount) {
 820                                xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
 821
 822                                pag->pagl_leftrec = trec.ir_startino;
 823                                pag->pagl_rightrec = rec.ir_startino;
 824                                pag->pagl_pagino = pagino;
 825                                goto alloc_inode;
 826                        }
 827
 828                        /* get next record to check */
 829                        if (useleft) {
 830                                error = xfs_ialloc_next_rec(tcur, &trec,
 831                                                                 &doneleft, 1);
 832                        } else {
 833                                error = xfs_ialloc_next_rec(cur, &rec,
 834                                                                 &doneright, 0);
 835                        }
 836                        if (error)
 837                                goto error1;
 838                }
 839
 840                /*
 841                 * We've reached the end of the btree. because
 842                 * we are only searching a small chunk of the
 843                 * btree each search, there is obviously free
 844                 * inodes closer to the parent inode than we
 845                 * are now. restart the search again.
 846                 */
 847                pag->pagl_pagino = NULLAGINO;
 848                pag->pagl_leftrec = NULLAGINO;
 849                pag->pagl_rightrec = NULLAGINO;
 850                xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
 851                xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
 852                goto restart_pagno;
 853        }
 854
 855        /*
 856         * In a different AG from the parent.
 857         * See if the most recently allocated block has any free.
 858         */
 859newino:
 860        if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
 861                error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
 862                                         XFS_LOOKUP_EQ, &i);
 863                if (error)
 864                        goto error0;
 865
 866                if (i == 1) {
 867                        error = xfs_inobt_get_rec(cur, &rec, &j);
 868                        if (error)
 869                                goto error0;
 870
 871                        if (j == 1 && rec.ir_freecount > 0) {
 872                                /*
 873                                 * The last chunk allocated in the group
 874                                 * still has a free inode.
 875                                 */
 876                                goto alloc_inode;
 877                        }
 878                }
 879        }
 880
 881        /*
 882         * None left in the last group, search the whole AG
 883         */
 884        error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
 885        if (error)
 886                goto error0;
 887        XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
 888
 889        for (;;) {
 890                error = xfs_inobt_get_rec(cur, &rec, &i);
 891                if (error)
 892                        goto error0;
 893                XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
 894                if (rec.ir_freecount > 0)
 895                        break;
 896                error = xfs_btree_increment(cur, 0, &i);
 897                if (error)
 898                        goto error0;
 899                XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
 900        }
 901
 902alloc_inode:
 903        offset = xfs_lowbit64(rec.ir_free);
 904        ASSERT(offset >= 0);
 905        ASSERT(offset < XFS_INODES_PER_CHUNK);
 906        ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
 907                                   XFS_INODES_PER_CHUNK) == 0);
 908        ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
 909        rec.ir_free &= ~XFS_INOBT_MASK(offset);
 910        rec.ir_freecount--;
 911        error = xfs_inobt_update(cur, &rec);
 912        if (error)
 913                goto error0;
 914        be32_add_cpu(&agi->agi_freecount, -1);
 915        xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
 916        pag->pagi_freecount--;
 917
 918        error = xfs_check_agi_freecount(cur, agi);
 919        if (error)
 920                goto error0;
 921
 922        xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
 923        xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
 924        xfs_perag_put(pag);
 925        *inop = ino;
 926        return 0;
 927error1:
 928        xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
 929error0:
 930        xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
 931        xfs_perag_put(pag);
 932        return error;
 933}
 934
 935/*
 936 * Allocate an inode on disk.
 937 *
 938 * Mode is used to tell whether the new inode will need space, and whether it
 939 * is a directory.
 940 *
 941 * This function is designed to be called twice if it has to do an allocation
 942 * to make more free inodes.  On the first call, *IO_agbp should be set to NULL.
 943 * If an inode is available without having to performn an allocation, an inode
 944 * number is returned.  In this case, *IO_agbp is set to NULL.  If an allocation
 945 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
 946 * The caller should then commit the current transaction, allocate a
 947 * new transaction, and call xfs_dialloc() again, passing in the previous value
 948 * of *IO_agbp.  IO_agbp should be held across the transactions. Since the AGI
 949 * buffer is locked across the two calls, the second call is guaranteed to have
 950 * a free inode available.
 951 *
 952 * Once we successfully pick an inode its number is returned and the on-disk
 953 * data structures are updated.  The inode itself is not read in, since doing so
 954 * would break ordering constraints with xfs_reclaim.
 955 */
 956int
 957xfs_dialloc(
 958        struct xfs_trans        *tp,
 959        xfs_ino_t               parent,
 960        umode_t                 mode,
 961        int                     okalloc,
 962        struct xfs_buf          **IO_agbp,
 963        xfs_ino_t               *inop)
 964{
 965        struct xfs_mount        *mp = tp->t_mountp;
 966        struct xfs_buf          *agbp;
 967        xfs_agnumber_t          agno;
 968        int                     error;
 969        int                     ialloced;
 970        int                     noroom = 0;
 971        xfs_agnumber_t          start_agno;
 972        struct xfs_perag        *pag;
 973
 974        if (*IO_agbp) {
 975                /*
 976                 * If the caller passes in a pointer to the AGI buffer,
 977                 * continue where we left off before.  In this case, we
 978                 * know that the allocation group has free inodes.
 979                 */
 980                agbp = *IO_agbp;
 981                goto out_alloc;
 982        }
 983
 984        /*
 985         * We do not have an agbp, so select an initial allocation
 986         * group for inode allocation.
 987         */
 988        start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
 989        if (start_agno == NULLAGNUMBER) {
 990                *inop = NULLFSINO;
 991                return 0;
 992        }
 993
 994        /*
 995         * If we have already hit the ceiling of inode blocks then clear
 996         * okalloc so we scan all available agi structures for a free
 997         * inode.
 998         */
 999        if (mp->m_maxicount &&
1000            mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
1001                noroom = 1;
1002                okalloc = 0;
1003        }
1004
1005        /*
1006         * Loop until we find an allocation group that either has free inodes
1007         * or in which we can allocate some inodes.  Iterate through the
1008         * allocation groups upward, wrapping at the end.
1009         */
1010        agno = start_agno;
1011        for (;;) {
1012                pag = xfs_perag_get(mp, agno);
1013                if (!pag->pagi_inodeok) {
1014                        xfs_ialloc_next_ag(mp);
1015                        goto nextag;
1016                }
1017
1018                if (!pag->pagi_init) {
1019                        error = xfs_ialloc_pagi_init(mp, tp, agno);
1020                        if (error)
1021                                goto out_error;
1022                }
1023
1024                /*
1025                 * Do a first racy fast path check if this AG is usable.
1026                 */
1027                if (!pag->pagi_freecount && !okalloc)
1028                        goto nextag;
1029
1030                /*
1031                 * Then read in the AGI buffer and recheck with the AGI buffer
1032                 * lock held.
1033                 */
1034                error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1035                if (error)
1036                        goto out_error;
1037
1038                if (pag->pagi_freecount) {
1039                        xfs_perag_put(pag);
1040                        goto out_alloc;
1041                }
1042
1043                if (!okalloc)
1044                        goto nextag_relse_buffer;
1045
1046
1047                error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1048                if (error) {
1049                        xfs_trans_brelse(tp, agbp);
1050
1051                        if (error != ENOSPC)
1052                                goto out_error;
1053
1054                        xfs_perag_put(pag);
1055                        *inop = NULLFSINO;
1056                        return 0;
1057                }
1058
1059                if (ialloced) {
1060                        /*
1061                         * We successfully allocated some inodes, return
1062                         * the current context to the caller so that it
1063                         * can commit the current transaction and call
1064                         * us again where we left off.
1065                         */
1066                        ASSERT(pag->pagi_freecount > 0);
1067                        xfs_perag_put(pag);
1068
1069                        *IO_agbp = agbp;
1070                        *inop = NULLFSINO;
1071                        return 0;
1072                }
1073
1074nextag_relse_buffer:
1075                xfs_trans_brelse(tp, agbp);
1076nextag:
1077                xfs_perag_put(pag);
1078                if (++agno == mp->m_sb.sb_agcount)
1079                        agno = 0;
1080                if (agno == start_agno) {
1081                        *inop = NULLFSINO;
1082                        return noroom ? ENOSPC : 0;
1083                }
1084        }
1085
1086out_alloc:
1087        *IO_agbp = NULL;
1088        return xfs_dialloc_ag(tp, agbp, parent, inop);
1089out_error:
1090        xfs_perag_put(pag);
1091        return XFS_ERROR(error);
1092}
1093
1094/*
1095 * Free disk inode.  Carefully avoids touching the incore inode, all
1096 * manipulations incore are the caller's responsibility.
1097 * The on-disk inode is not changed by this operation, only the
1098 * btree (free inode mask) is changed.
1099 */
1100int
1101xfs_difree(
1102        xfs_trans_t     *tp,            /* transaction pointer */
1103        xfs_ino_t       inode,          /* inode to be freed */
1104        xfs_bmap_free_t *flist,         /* extents to free */
1105        int             *delete,        /* set if inode cluster was deleted */
1106        xfs_ino_t       *first_ino)     /* first inode in deleted cluster */
1107{
1108        /* REFERENCED */
1109        xfs_agblock_t   agbno;  /* block number containing inode */
1110        xfs_buf_t       *agbp;  /* buffer containing allocation group header */
1111        xfs_agino_t     agino;  /* inode number relative to allocation group */
1112        xfs_agnumber_t  agno;   /* allocation group number */
1113        xfs_agi_t       *agi;   /* allocation group header */
1114        xfs_btree_cur_t *cur;   /* inode btree cursor */
1115        int             error;  /* error return value */
1116        int             i;      /* result code */
1117        int             ilen;   /* inodes in an inode cluster */
1118        xfs_mount_t     *mp;    /* mount structure for filesystem */
1119        int             off;    /* offset of inode in inode chunk */
1120        xfs_inobt_rec_incore_t rec;     /* btree record */
1121        struct xfs_perag *pag;
1122
1123        mp = tp->t_mountp;
1124
1125        /*
1126         * Break up inode number into its components.
1127         */
1128        agno = XFS_INO_TO_AGNO(mp, inode);
1129        if (agno >= mp->m_sb.sb_agcount)  {
1130                xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1131                        __func__, agno, mp->m_sb.sb_agcount);
1132                ASSERT(0);
1133                return XFS_ERROR(EINVAL);
1134        }
1135        agino = XFS_INO_TO_AGINO(mp, inode);
1136        if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  {
1137                xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1138                        __func__, (unsigned long long)inode,
1139                        (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1140                ASSERT(0);
1141                return XFS_ERROR(EINVAL);
1142        }
1143        agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1144        if (agbno >= mp->m_sb.sb_agblocks)  {
1145                xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1146                        __func__, agbno, mp->m_sb.sb_agblocks);
1147                ASSERT(0);
1148                return XFS_ERROR(EINVAL);
1149        }
1150        /*
1151         * Get the allocation group header.
1152         */
1153        error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1154        if (error) {
1155                xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1156                        __func__, error);
1157                return error;
1158        }
1159        agi = XFS_BUF_TO_AGI(agbp);
1160        ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1161        ASSERT(agbno < be32_to_cpu(agi->agi_length));
1162        /*
1163         * Initialize the cursor.
1164         */
1165        cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1166
1167        error = xfs_check_agi_freecount(cur, agi);
1168        if (error)
1169                goto error0;
1170
1171        /*
1172         * Look for the entry describing this inode.
1173         */
1174        if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
1175                xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1176                        __func__, error);
1177                goto error0;
1178        }
1179        XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1180        error = xfs_inobt_get_rec(cur, &rec, &i);
1181        if (error) {
1182                xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1183                        __func__, error);
1184                goto error0;
1185        }
1186        XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1187        /*
1188         * Get the offset in the inode chunk.
1189         */
1190        off = agino - rec.ir_startino;
1191        ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1192        ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1193        /*
1194         * Mark the inode free & increment the count.
1195         */
1196        rec.ir_free |= XFS_INOBT_MASK(off);
1197        rec.ir_freecount++;
1198
1199        /*
1200         * When an inode cluster is free, it becomes eligible for removal
1201         */
1202        if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1203            (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1204
1205                *delete = 1;
1206                *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1207
1208                /*
1209                 * Remove the inode cluster from the AGI B+Tree, adjust the
1210                 * AGI and Superblock inode counts, and mark the disk space
1211                 * to be freed when the transaction is committed.
1212                 */
1213                ilen = XFS_IALLOC_INODES(mp);
1214                be32_add_cpu(&agi->agi_count, -ilen);
1215                be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1216                xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1217                pag = xfs_perag_get(mp, agno);
1218                pag->pagi_freecount -= ilen - 1;
1219                xfs_perag_put(pag);
1220                xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1221                xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1222
1223                if ((error = xfs_btree_delete(cur, &i))) {
1224                        xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1225                                __func__, error);
1226                        goto error0;
1227                }
1228
1229                xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1230                                agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1231                                XFS_IALLOC_BLOCKS(mp), flist, mp);
1232        } else {
1233                *delete = 0;
1234
1235                error = xfs_inobt_update(cur, &rec);
1236                if (error) {
1237                        xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1238                                __func__, error);
1239                        goto error0;
1240                }
1241
1242                /* 
1243                 * Change the inode free counts and log the ag/sb changes.
1244                 */
1245                be32_add_cpu(&agi->agi_freecount, 1);
1246                xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1247                pag = xfs_perag_get(mp, agno);
1248                pag->pagi_freecount++;
1249                xfs_perag_put(pag);
1250                xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1251        }
1252
1253        error = xfs_check_agi_freecount(cur, agi);
1254        if (error)
1255                goto error0;
1256
1257        xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1258        return 0;
1259
1260error0:
1261        xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1262        return error;
1263}
1264
1265STATIC int
1266xfs_imap_lookup(
1267        struct xfs_mount        *mp,
1268        struct xfs_trans        *tp,
1269        xfs_agnumber_t          agno,
1270        xfs_agino_t             agino,
1271        xfs_agblock_t           agbno,
1272        xfs_agblock_t           *chunk_agbno,
1273        xfs_agblock_t           *offset_agbno,
1274        int                     flags)
1275{
1276        struct xfs_inobt_rec_incore rec;
1277        struct xfs_btree_cur    *cur;
1278        struct xfs_buf          *agbp;
1279        int                     error;
1280        int                     i;
1281
1282        error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1283        if (error) {
1284                xfs_alert(mp,
1285                        "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1286                        __func__, error, agno);
1287                return error;
1288        }
1289
1290        /*
1291         * Lookup the inode record for the given agino. If the record cannot be
1292         * found, then it's an invalid inode number and we should abort. Once
1293         * we have a record, we need to ensure it contains the inode number
1294         * we are looking up.
1295         */
1296        cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1297        error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
1298        if (!error) {
1299                if (i)
1300                        error = xfs_inobt_get_rec(cur, &rec, &i);
1301                if (!error && i == 0)
1302                        error = EINVAL;
1303        }
1304
1305        xfs_trans_brelse(tp, agbp);
1306        xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1307        if (error)
1308                return error;
1309
1310        /* check that the returned record contains the required inode */
1311        if (rec.ir_startino > agino ||
1312            rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino)
1313                return EINVAL;
1314
1315        /* for untrusted inodes check it is allocated first */
1316        if ((flags & XFS_IGET_UNTRUSTED) &&
1317            (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1318                return EINVAL;
1319
1320        *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1321        *offset_agbno = agbno - *chunk_agbno;
1322        return 0;
1323}
1324
1325/*
1326 * Return the location of the inode in imap, for mapping it into a buffer.
1327 */
1328int
1329xfs_imap(
1330        xfs_mount_t      *mp,   /* file system mount structure */
1331        xfs_trans_t      *tp,   /* transaction pointer */
1332        xfs_ino_t       ino,    /* inode to locate */
1333        struct xfs_imap *imap,  /* location map structure */
1334        uint            flags)  /* flags for inode btree lookup */
1335{
1336        xfs_agblock_t   agbno;  /* block number of inode in the alloc group */
1337        xfs_agino_t     agino;  /* inode number within alloc group */
1338        xfs_agnumber_t  agno;   /* allocation group number */
1339        int             blks_per_cluster; /* num blocks per inode cluster */
1340        xfs_agblock_t   chunk_agbno;    /* first block in inode chunk */
1341        xfs_agblock_t   cluster_agbno;  /* first block in inode cluster */
1342        int             error;  /* error code */
1343        int             offset; /* index of inode in its buffer */
1344        int             offset_agbno;   /* blks from chunk start to inode */
1345
1346        ASSERT(ino != NULLFSINO);
1347
1348        /*
1349         * Split up the inode number into its parts.
1350         */
1351        agno = XFS_INO_TO_AGNO(mp, ino);
1352        agino = XFS_INO_TO_AGINO(mp, ino);
1353        agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1354        if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1355            ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1356#ifdef DEBUG
1357                /*
1358                 * Don't output diagnostic information for untrusted inodes
1359                 * as they can be invalid without implying corruption.
1360                 */
1361                if (flags & XFS_IGET_UNTRUSTED)
1362                        return XFS_ERROR(EINVAL);
1363                if (agno >= mp->m_sb.sb_agcount) {
1364                        xfs_alert(mp,
1365                                "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1366                                __func__, agno, mp->m_sb.sb_agcount);
1367                }
1368                if (agbno >= mp->m_sb.sb_agblocks) {
1369                        xfs_alert(mp,
1370                "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1371                                __func__, (unsigned long long)agbno,
1372                                (unsigned long)mp->m_sb.sb_agblocks);
1373                }
1374                if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1375                        xfs_alert(mp,
1376                "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1377                                __func__, ino,
1378                                XFS_AGINO_TO_INO(mp, agno, agino));
1379                }
1380                xfs_stack_trace();
1381#endif /* DEBUG */
1382                return XFS_ERROR(EINVAL);
1383        }
1384
1385        blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1386
1387        /*
1388         * For bulkstat and handle lookups, we have an untrusted inode number
1389         * that we have to verify is valid. We cannot do this just by reading
1390         * the inode buffer as it may have been unlinked and removed leaving
1391         * inodes in stale state on disk. Hence we have to do a btree lookup
1392         * in all cases where an untrusted inode number is passed.
1393         */
1394        if (flags & XFS_IGET_UNTRUSTED) {
1395                error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1396                                        &chunk_agbno, &offset_agbno, flags);
1397                if (error)
1398                        return error;
1399                goto out_map;
1400        }
1401
1402        /*
1403         * If the inode cluster size is the same as the blocksize or
1404         * smaller we get to the buffer by simple arithmetics.
1405         */
1406        if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1407                offset = XFS_INO_TO_OFFSET(mp, ino);
1408                ASSERT(offset < mp->m_sb.sb_inopblock);
1409
1410                imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1411                imap->im_len = XFS_FSB_TO_BB(mp, 1);
1412                imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1413                return 0;
1414        }
1415
1416        /*
1417         * If the inode chunks are aligned then use simple maths to
1418         * find the location. Otherwise we have to do a btree
1419         * lookup to find the location.
1420         */
1421        if (mp->m_inoalign_mask) {
1422                offset_agbno = agbno & mp->m_inoalign_mask;
1423                chunk_agbno = agbno - offset_agbno;
1424        } else {
1425                error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1426                                        &chunk_agbno, &offset_agbno, flags);
1427                if (error)
1428                        return error;
1429        }
1430
1431out_map:
1432        ASSERT(agbno >= chunk_agbno);
1433        cluster_agbno = chunk_agbno +
1434                ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1435        offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1436                XFS_INO_TO_OFFSET(mp, ino);
1437
1438        imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1439        imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1440        imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1441
1442        /*
1443         * If the inode number maps to a block outside the bounds
1444         * of the file system then return NULL rather than calling
1445         * read_buf and panicing when we get an error from the
1446         * driver.
1447         */
1448        if ((imap->im_blkno + imap->im_len) >
1449            XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1450                xfs_alert(mp,
1451        "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1452                        __func__, (unsigned long long) imap->im_blkno,
1453                        (unsigned long long) imap->im_len,
1454                        XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1455                return XFS_ERROR(EINVAL);
1456        }
1457        return 0;
1458}
1459
1460/*
1461 * Compute and fill in value of m_in_maxlevels.
1462 */
1463void
1464xfs_ialloc_compute_maxlevels(
1465        xfs_mount_t     *mp)            /* file system mount structure */
1466{
1467        int             level;
1468        uint            maxblocks;
1469        uint            maxleafents;
1470        int             minleafrecs;
1471        int             minnoderecs;
1472
1473        maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1474                XFS_INODES_PER_CHUNK_LOG;
1475        minleafrecs = mp->m_alloc_mnr[0];
1476        minnoderecs = mp->m_alloc_mnr[1];
1477        maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1478        for (level = 1; maxblocks > 1; level++)
1479                maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1480        mp->m_in_maxlevels = level;
1481}
1482
1483/*
1484 * Log specified fields for the ag hdr (inode section)
1485 */
1486void
1487xfs_ialloc_log_agi(
1488        xfs_trans_t     *tp,            /* transaction pointer */
1489        xfs_buf_t       *bp,            /* allocation group header buffer */
1490        int             fields)         /* bitmask of fields to log */
1491{
1492        int                     first;          /* first byte number */
1493        int                     last;           /* last byte number */
1494        static const short      offsets[] = {   /* field starting offsets */
1495                                        /* keep in sync with bit definitions */
1496                offsetof(xfs_agi_t, agi_magicnum),
1497                offsetof(xfs_agi_t, agi_versionnum),
1498                offsetof(xfs_agi_t, agi_seqno),
1499                offsetof(xfs_agi_t, agi_length),
1500                offsetof(xfs_agi_t, agi_count),
1501                offsetof(xfs_agi_t, agi_root),
1502                offsetof(xfs_agi_t, agi_level),
1503                offsetof(xfs_agi_t, agi_freecount),
1504                offsetof(xfs_agi_t, agi_newino),
1505                offsetof(xfs_agi_t, agi_dirino),
1506                offsetof(xfs_agi_t, agi_unlinked),
1507                sizeof(xfs_agi_t)
1508        };
1509#ifdef DEBUG
1510        xfs_agi_t               *agi;   /* allocation group header */
1511
1512        agi = XFS_BUF_TO_AGI(bp);
1513        ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1514#endif
1515        /*
1516         * Compute byte offsets for the first and last fields.
1517         */
1518        xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1519        /*
1520         * Log the allocation group inode header buffer.
1521         */
1522        xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
1523        xfs_trans_log_buf(tp, bp, first, last);
1524}
1525
1526#ifdef DEBUG
1527STATIC void
1528xfs_check_agi_unlinked(
1529        struct xfs_agi          *agi)
1530{
1531        int                     i;
1532
1533        for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1534                ASSERT(agi->agi_unlinked[i]);
1535}
1536#else
1537#define xfs_check_agi_unlinked(agi)
1538#endif
1539
1540static bool
1541xfs_agi_verify(
1542        struct xfs_buf  *bp)
1543{
1544        struct xfs_mount *mp = bp->b_target->bt_mount;
1545        struct xfs_agi  *agi = XFS_BUF_TO_AGI(bp);
1546
1547        if (xfs_sb_version_hascrc(&mp->m_sb) &&
1548            !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
1549                        return false;
1550        /*
1551         * Validate the magic number of the agi block.
1552         */
1553        if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
1554                return false;
1555        if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
1556                return false;
1557
1558        /*
1559         * during growfs operations, the perag is not fully initialised,
1560         * so we can't use it for any useful checking. growfs ensures we can't
1561         * use it by using uncached buffers that don't have the perag attached
1562         * so we can detect and avoid this problem.
1563         */
1564        if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
1565                return false;
1566
1567        xfs_check_agi_unlinked(agi);
1568        return true;
1569}
1570
1571static void
1572xfs_agi_read_verify(
1573        struct xfs_buf  *bp)
1574{
1575        struct xfs_mount *mp = bp->b_target->bt_mount;
1576        int             agi_ok = 1;
1577
1578        if (xfs_sb_version_hascrc(&mp->m_sb))
1579                agi_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
1580                                          offsetof(struct xfs_agi, agi_crc));
1581        agi_ok = agi_ok && xfs_agi_verify(bp);
1582
1583        if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1584                        XFS_RANDOM_IALLOC_READ_AGI))) {
1585                XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1586                xfs_buf_ioerror(bp, EFSCORRUPTED);
1587        }
1588}
1589
1590static void
1591xfs_agi_write_verify(
1592        struct xfs_buf  *bp)
1593{
1594        struct xfs_mount *mp = bp->b_target->bt_mount;
1595        struct xfs_buf_log_item *bip = bp->b_fspriv;
1596
1597        if (!xfs_agi_verify(bp)) {
1598                XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1599                xfs_buf_ioerror(bp, EFSCORRUPTED);
1600                return;
1601        }
1602
1603        if (!xfs_sb_version_hascrc(&mp->m_sb))
1604                return;
1605
1606        if (bip)
1607                XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1608        xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
1609                         offsetof(struct xfs_agi, agi_crc));
1610}
1611
1612const struct xfs_buf_ops xfs_agi_buf_ops = {
1613        .verify_read = xfs_agi_read_verify,
1614        .verify_write = xfs_agi_write_verify,
1615};
1616
1617/*
1618 * Read in the allocation group header (inode allocation section)
1619 */
1620int
1621xfs_read_agi(
1622        struct xfs_mount        *mp,    /* file system mount structure */
1623        struct xfs_trans        *tp,    /* transaction pointer */
1624        xfs_agnumber_t          agno,   /* allocation group number */
1625        struct xfs_buf          **bpp)  /* allocation group hdr buf */
1626{
1627        int                     error;
1628
1629        ASSERT(agno != NULLAGNUMBER);
1630
1631        error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1632                        XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1633                        XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
1634        if (error)
1635                return error;
1636
1637        ASSERT(!xfs_buf_geterror(*bpp));
1638        xfs_buf_set_ref(*bpp, XFS_AGI_REF);
1639        return 0;
1640}
1641
1642int
1643xfs_ialloc_read_agi(
1644        struct xfs_mount        *mp,    /* file system mount structure */
1645        struct xfs_trans        *tp,    /* transaction pointer */
1646        xfs_agnumber_t          agno,   /* allocation group number */
1647        struct xfs_buf          **bpp)  /* allocation group hdr buf */
1648{
1649        struct xfs_agi          *agi;   /* allocation group header */
1650        struct xfs_perag        *pag;   /* per allocation group data */
1651        int                     error;
1652
1653        error = xfs_read_agi(mp, tp, agno, bpp);
1654        if (error)
1655                return error;
1656
1657        agi = XFS_BUF_TO_AGI(*bpp);
1658        pag = xfs_perag_get(mp, agno);
1659        if (!pag->pagi_init) {
1660                pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1661                pag->pagi_count = be32_to_cpu(agi->agi_count);
1662                pag->pagi_init = 1;
1663        }
1664
1665        /*
1666         * It's possible for these to be out of sync if
1667         * we are in the middle of a forced shutdown.
1668         */
1669        ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1670                XFS_FORCED_SHUTDOWN(mp));
1671        xfs_perag_put(pag);
1672        return 0;
1673}
1674
1675/*
1676 * Read in the agi to initialise the per-ag data in the mount structure
1677 */
1678int
1679xfs_ialloc_pagi_init(
1680        xfs_mount_t     *mp,            /* file system mount structure */
1681        xfs_trans_t     *tp,            /* transaction pointer */
1682        xfs_agnumber_t  agno)           /* allocation group number */
1683{
1684        xfs_buf_t       *bp = NULL;
1685        int             error;
1686
1687        error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1688        if (error)
1689                return error;
1690        if (bp)
1691                xfs_trans_brelse(tp, bp);
1692        return 0;
1693}
1694
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.