LCOV - code coverage report
Current view: top level - fs/xfs - xfs_rtalloc.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 758 1031 73.5 %
Date: 2023-07-31 20:08:27 Functions: 28 35 80.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_mount.h"
      14             : #include "xfs_inode.h"
      15             : #include "xfs_bmap.h"
      16             : #include "xfs_bmap_btree.h"
      17             : #include "xfs_trans.h"
      18             : #include "xfs_trans_space.h"
      19             : #include "xfs_icache.h"
      20             : #include "xfs_rtalloc.h"
      21             : #include "xfs_sb.h"
      22             : #include "xfs_log_priv.h"
      23             : #include "xfs_health.h"
      24             : #include "xfs_trace.h"
      25             : #include "xfs_da_format.h"
      26             : #include "xfs_imeta.h"
      27             : #include "xfs_imeta_utils.h"
      28             : #include "xfs_rtbitmap.h"
      29             : #include "xfs_rtgroup.h"
      30             : #include "xfs_error.h"
      31             : #include "xfs_btree.h"
      32             : #include "xfs_rmap.h"
      33             : #include "xfs_rtrmap_btree.h"
      34             : #include "xfs_rtrefcount_btree.h"
      35             : #include "xfs_quota.h"
      36             : 
      37             : /*
      38             :  * Realtime metadata files are not quite regular files because userspace can't
      39             :  * access the realtime bitmap directly, and because we take the ILOCK of the rt
      40             :  * bitmap file while holding the ILOCK of a regular realtime file.  This double
      41             :  * locking confuses lockdep, so create different lockdep classes here to help
      42             :  * it keep things straight.
      43             :  */
      44             : static struct lock_class_key xfs_rbmip_key;
      45             : static struct lock_class_key xfs_rsumip_key;
      46             : static struct lock_class_key xfs_rrmapip_key;
      47             : static struct lock_class_key xfs_rrefcountip_key;
      48             : 
      49             : /*
      50             :  * Read and return the summary information for a given extent size,
      51             :  * bitmap block combination.
      52             :  * Keeps track of a current summary block, so we don't keep reading
      53             :  * it from the buffer cache.
      54             :  */
      55             : static int
      56             : xfs_rtget_summary(
      57             :         xfs_mount_t     *mp,            /* file system mount structure */
      58             :         xfs_trans_t     *tp,            /* transaction pointer */
      59             :         int             log,            /* log2 of extent size */
      60             :         xfs_fileoff_t   bbno,           /* bitmap block number */
      61             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
      62             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
      63             :         xfs_suminfo_t   *sum)           /* out: summary info for this block */
      64             : {
      65  1740854650 :         return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
      66             : }
      67             : 
      68             : /*
      69             :  * Return whether there are any free extents in the size range given
      70             :  * by low and high, for the bitmap block bbno.
      71             :  */
      72             : STATIC int                              /* error */
      73   213176043 : xfs_rtany_summary(
      74             :         xfs_mount_t     *mp,            /* file system mount structure */
      75             :         xfs_trans_t     *tp,            /* transaction pointer */
      76             :         int             low,            /* low log2 extent size */
      77             :         int             high,           /* high log2 extent size */
      78             :         xfs_fileoff_t   bbno,           /* bitmap block number */
      79             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
      80             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
      81             :         int             *stat)          /* out: any good extents here? */
      82             : {
      83   213176043 :         int             error;          /* error value */
      84   213176043 :         int             log;            /* loop counter, log2 of ext. size */
      85   213176043 :         xfs_suminfo_t   sum;            /* summary data */
      86             : 
      87             :         /* There are no extents at levels < m_rsum_cache[bbno]. */
      88   213176043 :         if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
      89             :                 low = mp->m_rsum_cache[bbno];
      90             : 
      91             :         /*
      92             :          * Loop over logs of extent sizes.
      93             :          */
      94   361492250 :         for (log = low; log <= high; log++) {
      95             :                 /*
      96             :                  * Get one summary datum.
      97             :                  */
      98   169794982 :                 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
      99   169794982 :                 if (error) {
     100           0 :                         return error;
     101             :                 }
     102             :                 /*
     103             :                  * If there are any, return success.
     104             :                  */
     105   169794982 :                 if (sum) {
     106    21478775 :                         *stat = 1;
     107    21478775 :                         goto out;
     108             :                 }
     109             :         }
     110             :         /*
     111             :          * Found nothing, return failure.
     112             :          */
     113   191697268 :         *stat = 0;
     114   213176043 : out:
     115             :         /* There were no extents at levels < log. */
     116   213176043 :         if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
     117     7032027 :                 mp->m_rsum_cache[bbno] = log;
     118             :         return 0;
     119             : }
     120             : 
     121             : 
     122             : /*
     123             :  * Copy and transform the summary file, given the old and new
     124             :  * parameters in the mount structures.
     125             :  */
     126             : STATIC int                              /* error */
     127           4 : xfs_rtcopy_summary(
     128             :         xfs_mount_t     *omp,           /* old file system mount point */
     129             :         xfs_mount_t     *nmp,           /* new file system mount point */
     130             :         xfs_trans_t     *tp)            /* transaction pointer */
     131             : {
     132           4 :         xfs_fileoff_t   bbno;           /* bitmap block number */
     133           4 :         struct xfs_buf  *bp;            /* summary buffer */
     134           4 :         int             error;          /* error return value */
     135           4 :         int             log;            /* summary level number (log length) */
     136           4 :         xfs_suminfo_t   sum;            /* summary data */
     137           4 :         xfs_fileoff_t   sumbno;         /* summary block number */
     138             : 
     139           4 :         bp = NULL;
     140          66 :         for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
     141          62 :                 for (bbno = omp->m_sb.sb_rbmblocks - 1;
     142         174 :                      (xfs_srtblock_t)bbno >= 0;
     143         112 :                      bbno--) {
     144         112 :                         error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
     145             :                                 &sumbno, &sum);
     146         112 :                         if (error)
     147           0 :                                 return error;
     148         112 :                         if (sum == 0)
     149         105 :                                 continue;
     150           7 :                         error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
     151             :                                 &bp, &sumbno);
     152           7 :                         if (error)
     153           0 :                                 return error;
     154           7 :                         error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
     155             :                                 &bp, &sumbno);
     156           7 :                         if (error)
     157           0 :                                 return error;
     158           7 :                         ASSERT(sum > 0);
     159             :                 }
     160             :         }
     161             :         return 0;
     162             : }
     163             : /*
     164             :  * Mark an extent specified by start and len allocated.
     165             :  * Updates all the summary information as well as the bitmap.
     166             :  */
     167             : STATIC int                              /* error */
     168    23769774 : xfs_rtallocate_range(
     169             :         xfs_mount_t     *mp,            /* file system mount point */
     170             :         xfs_trans_t     *tp,            /* transaction pointer */
     171             :         xfs_rtxnum_t    start,          /* start rtext to allocate */
     172             :         xfs_rtxlen_t    len,            /* length to allocate */
     173             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     174             :         xfs_fileoff_t   *rsb)           /* in/out: summary block number */
     175             : {
     176    23769774 :         xfs_rtxnum_t    end;            /* end of the allocated rtext */
     177    23769774 :         int             error;          /* error value */
     178    23769774 :         xfs_rtxnum_t    postblock = 0;  /* first rtext allocated > end */
     179    23769774 :         xfs_rtxnum_t    preblock = 0;   /* first rtext allocated < start */
     180             : 
     181    23769774 :         end = start + len - 1;
     182             :         /*
     183             :          * Assume we're allocating out of the middle of a free extent.
     184             :          * We need to find the beginning and end of the extent so we can
     185             :          * properly update the summary.
     186             :          */
     187    23769774 :         error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
     188    23769774 :         if (error) {
     189             :                 return error;
     190             :         }
     191             :         /*
     192             :          * Find the next allocated block (end of free extent).
     193             :          */
     194    23769774 :         error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
     195             :                 &postblock);
     196    23769774 :         if (error) {
     197             :                 return error;
     198             :         }
     199             :         /*
     200             :          * Decrement the summary information corresponding to the entire
     201             :          * (old) free extent.
     202             :          */
     203    23769774 :         error = xfs_rtmodify_summary(mp, tp,
     204    23769774 :                 XFS_RTBLOCKLOG(postblock + 1 - preblock),
     205             :                 xfs_rtx_to_rbmblock(mp, preblock), -1, rbpp, rsb);
     206    23769774 :         if (error) {
     207             :                 return error;
     208             :         }
     209             :         /*
     210             :          * If there are blocks not being allocated at the front of the
     211             :          * old extent, add summary data for them to be free.
     212             :          */
     213    23769774 :         if (preblock < start) {
     214      501412 :                 error = xfs_rtmodify_summary(mp, tp,
     215             :                         XFS_RTBLOCKLOG(start - preblock),
     216             :                         xfs_rtx_to_rbmblock(mp, preblock), 1, rbpp, rsb);
     217      250706 :                 if (error) {
     218             :                         return error;
     219             :                 }
     220             :         }
     221             :         /*
     222             :          * If there are blocks not being allocated at the end of the
     223             :          * old extent, add summary data for them to be free.
     224             :          */
     225    23769774 :         if (postblock > end) {
     226    19528814 :                 error = xfs_rtmodify_summary(mp, tp,
     227             :                         XFS_RTBLOCKLOG(postblock - end),
     228             :                         xfs_rtx_to_rbmblock(mp, end + 1), 1, rbpp, rsb);
     229     9764407 :                 if (error) {
     230             :                         return error;
     231             :                 }
     232             :         }
     233             :         /*
     234             :          * Modify the bitmap to mark this extent allocated.
     235             :          */
     236    23769774 :         error = xfs_rtmodify_range(mp, tp, start, len, 0);
     237    23769774 :         return error;
     238             : }
     239             : 
     240             : /*
     241             :  * Attempt to allocate an extent minlen<=len<=maxlen starting from
     242             :  * bitmap block bbno.  If we don't get maxlen then use prod to trim
     243             :  * the length, if given.  Returns error; returns starting block in *rtx.
     244             :  * The lengths are all in rtextents.
     245             :  */
     246             : STATIC int                              /* error */
     247    67641067 : xfs_rtallocate_extent_block(
     248             :         xfs_mount_t     *mp,            /* file system mount point */
     249             :         xfs_trans_t     *tp,            /* transaction pointer */
     250             :         xfs_fileoff_t   bbno,           /* bitmap block number */
     251             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     252             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     253             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     254             :         xfs_rtxnum_t    *nextp,         /* out: next rtext to try */
     255             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     256             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     257             :         xfs_rtxlen_t    prod,           /* extent product factor */
     258             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     259             : {
     260    67641067 :         xfs_rtxnum_t    besti;          /* best rtext found so far */
     261    67641067 :         xfs_rtxnum_t    bestlen;        /* best length found so far */
     262    67641067 :         xfs_rtxnum_t    end;            /* last rtext in chunk */
     263    67641067 :         int             error;          /* error value */
     264    67641067 :         xfs_rtxnum_t    i;              /* current rtext trying */
     265    67641067 :         xfs_rtxnum_t    next;           /* next rtext to try */
     266    67641067 :         int             stat;           /* status from internal calls */
     267             : 
     268             :         /*
     269             :          * Loop over all the extents starting in this bitmap block,
     270             :          * looking for one that's long enough.
     271             :          */
     272    67641067 :         for (i = xfs_rbmblock_to_rtx(mp, bbno), besti = -1, bestlen = 0,
     273   135282134 :                 end = xfs_rbmblock_to_rtx(mp, bbno + 1) - 1;
     274   318103505 :              i <= end;
     275   250462438 :              i++) {
     276             :                 /*
     277             :                  * Make sure we don't run off the end of the rt volume.  Be
     278             :                  * careful that adjusting maxlen downwards doesn't cause us to
     279             :                  * fail the alignment checks.
     280             :                  */
     281   261312892 :                 maxlen = min(mp->m_sb.sb_rextents, i + maxlen) - i;
     282   261312892 :                 maxlen -= maxlen % prod;
     283             : 
     284             :                 /*
     285             :                  * See if there's a free extent of maxlen starting at i.
     286             :                  * If it's not so then next will contain the first non-free.
     287             :                  */
     288   261312892 :                 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
     289   261312892 :                 if (error) {
     290           0 :                         return error;
     291             :                 }
     292   261312892 :                 if (stat) {
     293             :                         /*
     294             :                          * i for maxlen is all free, allocate and return that.
     295             :                          */
     296    10824690 :                         error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
     297             :                                 rsb);
     298    10824690 :                         if (error) {
     299             :                                 return error;
     300             :                         }
     301    10824690 :                         *len = maxlen;
     302    10824690 :                         *rtx = i;
     303    10824690 :                         return 0;
     304             :                 }
     305             :                 /*
     306             :                  * In the case where we have a variable-sized allocation
     307             :                  * request, figure out how big this free piece is,
     308             :                  * and if it's big enough for the minimum, and the best
     309             :                  * so far, remember it.
     310             :                  */
     311   250488202 :                 if (minlen < maxlen) {
     312   219701614 :                         xfs_rtxnum_t    thislen;        /* this extent size */
     313             : 
     314   219701614 :                         thislen = next - i;
     315   219701614 :                         if (thislen >= minlen && thislen > bestlen) {
     316    29173077 :                                 besti = i;
     317    29173077 :                                 bestlen = thislen;
     318             :                         }
     319             :                 }
     320             :                 /*
     321             :                  * If not done yet, find the start of the next free space.
     322             :                  */
     323   250488202 :                 if (next < end) {
     324   250462438 :                         error = xfs_rtfind_forw(mp, tp, next, end, &i);
     325   250462438 :                         if (error) {
     326           0 :                                 return error;
     327             :                         }
     328             :                 } else
     329             :                         break;
     330             :         }
     331             :         /*
     332             :          * Searched the whole thing & didn't find a maxlen free extent.
     333             :          */
     334    56816377 :         if (minlen < maxlen && besti != -1) {
     335    12404769 :                 xfs_rtxlen_t    p;      /* amount to trim length by */
     336             : 
     337             :                 /*
     338             :                  * If size should be a multiple of prod, make that so.
     339             :                  */
     340    12404769 :                 if (prod > 1) {
     341         190 :                         div_u64_rem(bestlen, prod, &p);
     342         190 :                         if (p)
     343         161 :                                 bestlen -= p;
     344             :                 }
     345             : 
     346             :                 /*
     347             :                  * Allocate besti for bestlen & return that.
     348             :                  */
     349    12404769 :                 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
     350    12404769 :                 if (error) {
     351             :                         return error;
     352             :                 }
     353    12404769 :                 *len = bestlen;
     354    12404769 :                 *rtx = besti;
     355    12404769 :                 return 0;
     356             :         }
     357             :         /*
     358             :          * Allocation failed.  Set *nextp to the next block to try.
     359             :          */
     360    44411608 :         *nextp = next;
     361    44411608 :         *rtx = NULLRTEXTNO;
     362    44411608 :         return 0;
     363             : }
     364             : 
     365             : /*
     366             :  * Allocate an extent of length minlen<=len<=maxlen, starting at block
     367             :  * bno.  If we don't get maxlen then use prod to trim the length, if given.
     368             :  * Returns error; returns starting block in *rtx.
     369             :  * The lengths are all in rtextents.
     370             :  */
     371             : STATIC int                              /* error */
     372    22019008 : xfs_rtallocate_extent_exact(
     373             :         xfs_mount_t     *mp,            /* file system mount point */
     374             :         xfs_trans_t     *tp,            /* transaction pointer */
     375             :         xfs_rtxnum_t    start,          /* starting rtext number to allocate */
     376             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     377             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     378             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     379             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     380             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     381             :         xfs_rtxlen_t    prod,           /* extent product factor */
     382             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     383             : {
     384    22019008 :         int             error;          /* error value */
     385    22019008 :         xfs_rtxlen_t    i;              /* extent length trimmed due to prod */
     386    22019008 :         int             isfree;         /* extent is free */
     387    22019008 :         xfs_rtxnum_t    next;           /* next rtext to try (dummy) */
     388             : 
     389    22019008 :         ASSERT(minlen % prod == 0);
     390    22019008 :         ASSERT(maxlen % prod == 0);
     391             :         /*
     392             :          * Check if the range in question (for maxlen) is free.
     393             :          */
     394    22019008 :         error = xfs_rtcheck_range(mp, tp, start, maxlen, 1, &next, &isfree);
     395    22019008 :         if (error) {
     396             :                 return error;
     397             :         }
     398    22019008 :         if (isfree) {
     399             :                 /*
     400             :                  * If it is, allocate it and return success.
     401             :                  */
     402      393849 :                 error = xfs_rtallocate_range(mp, tp, start, maxlen, rbpp, rsb);
     403      393849 :                 if (error) {
     404             :                         return error;
     405             :                 }
     406      393849 :                 *len = maxlen;
     407      393849 :                 *rtx = start;
     408      393849 :                 return 0;
     409             :         }
     410             :         /*
     411             :          * If not, allocate what there is, if it's at least minlen.
     412             :          */
     413    21625159 :         maxlen = next - start;
     414    21625159 :         if (maxlen < minlen) {
     415             :                 /*
     416             :                  * Failed, return failure status.
     417             :                  */
     418    21478693 :                 *rtx = NULLRTEXTNO;
     419    21478693 :                 return 0;
     420             :         }
     421             :         /*
     422             :          * Trim off tail of extent, if prod is specified.
     423             :          */
     424      146466 :         if (prod > 1 && (i = maxlen % prod)) {
     425          33 :                 maxlen -= i;
     426          33 :                 if (maxlen < minlen) {
     427             :                         /*
     428             :                          * Now we can't do it, return failure status.
     429             :                          */
     430           0 :                         *rtx = NULLRTEXTNO;
     431           0 :                         return 0;
     432             :                 }
     433             :         }
     434             :         /*
     435             :          * Allocate what we can and return it.
     436             :          */
     437      146466 :         error = xfs_rtallocate_range(mp, tp, start, maxlen, rbpp, rsb);
     438      146466 :         if (error) {
     439             :                 return error;
     440             :         }
     441      146466 :         *len = maxlen;
     442      146466 :         *rtx = start;
     443      146466 :         return 0;
     444             : }
     445             : 
     446             : /*
     447             :  * Allocate an extent of length minlen<=len<=maxlen, starting as near
     448             :  * to start as possible.  If we don't get maxlen then use prod to trim
     449             :  * the length, if given.  The lengths are all in rtextents.
     450             :  */
     451             : STATIC int                              /* error */
     452    22019008 : xfs_rtallocate_extent_near(
     453             :         xfs_mount_t     *mp,            /* file system mount point */
     454             :         xfs_trans_t     *tp,            /* transaction pointer */
     455             :         xfs_rtxnum_t    start,          /* starting rtext number to allocate */
     456             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     457             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     458             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     459             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     460             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     461             :         xfs_rtxlen_t    prod,           /* extent product factor */
     462             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     463             : {
     464    22019008 :         int             any;            /* any useful extents from summary */
     465    22019008 :         xfs_fileoff_t   bbno;           /* bitmap block number */
     466    22019008 :         int             error;          /* error value */
     467    22019008 :         int             i;              /* bitmap block offset (loop control) */
     468    22019008 :         int             j;              /* secondary loop control */
     469    22019008 :         int             log2len;        /* log2 of minlen */
     470    22019008 :         xfs_rtxnum_t    n;              /* next rtext to try */
     471    22019008 :         xfs_rtxnum_t    r;              /* result rtext */
     472             : 
     473    22019008 :         ASSERT(minlen % prod == 0);
     474    22019008 :         ASSERT(maxlen % prod == 0);
     475             : 
     476             :         /*
     477             :          * If the block number given is off the end, silently set it to
     478             :          * the last block.
     479             :          */
     480    22019008 :         if (start >= mp->m_sb.sb_rextents)
     481           0 :                 start = mp->m_sb.sb_rextents - 1;
     482             : 
     483             :         /*
     484             :          * Make sure we don't run off the end of the rt volume.  Be careful
     485             :          * that adjusting maxlen downwards doesn't cause us to fail the
     486             :          * alignment checks.
     487             :          */
     488    22019008 :         maxlen = min(mp->m_sb.sb_rextents, start + maxlen) - start;
     489    22019008 :         maxlen -= maxlen % prod;
     490    22019008 :         if (maxlen < minlen) {
     491           0 :                 *rtx = NULLRTEXTNO;
     492           0 :                 return 0;
     493             :         }
     494             : 
     495             :         /*
     496             :          * Try the exact allocation first.
     497             :          */
     498    22019008 :         error = xfs_rtallocate_extent_exact(mp, tp, start, minlen, maxlen, len,
     499             :                 rbpp, rsb, prod, &r);
     500    22019008 :         if (error) {
     501             :                 return error;
     502             :         }
     503             :         /*
     504             :          * If the exact allocation worked, return that.
     505             :          */
     506    22019008 :         if (r != NULLRTEXTNO) {
     507      540315 :                 *rtx = r;
     508      540315 :                 return 0;
     509             :         }
     510    21478693 :         bbno = xfs_rtx_to_rbmblock(mp, start);
     511    21478693 :         i = 0;
     512    21478693 :         ASSERT(minlen != 0);
     513    21478693 :         log2len = xfs_highbit32(minlen);
     514             :         /*
     515             :          * Loop over all bitmap blocks (bbno + i is current block).
     516             :          */
     517   169279887 :         for (;;) {
     518             :                 /*
     519             :                  * Get summary information of extents of all useful levels
     520             :                  * starting in this bitmap block.
     521             :                  */
     522   169279887 :                 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
     523             :                         bbno + i, rbpp, rsb, &any);
     524   169279887 :                 if (error) {
     525           0 :                         return error;
     526             :                 }
     527             :                 /*
     528             :                  * If there are any useful extents starting here, try
     529             :                  * allocating one.
     530             :                  */
     531   169279887 :                 if (any) {
     532             :                         /*
     533             :                          * On the positive side of the starting location.
     534             :                          */
     535    21478750 :                         if (i >= 0) {
     536             :                                 /*
     537             :                                  * Try to allocate an extent starting in
     538             :                                  * this block.
     539             :                                  */
     540    15953147 :                                 error = xfs_rtallocate_extent_block(mp, tp,
     541             :                                         bbno + i, minlen, maxlen, len, &n, rbpp,
     542             :                                         rsb, prod, &r);
     543    15953147 :                                 if (error) {
     544           0 :                                         return error;
     545             :                                 }
     546             :                                 /*
     547             :                                  * If it worked, return it.
     548             :                                  */
     549    15953147 :                                 if (r != NULLRTEXTNO) {
     550    15953112 :                                         *rtx = r;
     551    15953112 :                                         return 0;
     552             :                                 }
     553             :                         }
     554             :                         /*
     555             :                          * On the negative side of the starting location.
     556             :                          */
     557             :                         else {          /* i < 0 */
     558             :                                 /*
     559             :                                  * Loop backwards through the bitmap blocks from
     560             :                                  * the starting point-1 up to where we are now.
     561             :                                  * There should be an extent which ends in this
     562             :                                  * bitmap block and is long enough.
     563             :                                  */
     564    49353930 :                                 for (j = -1; j > i; j--) {
     565             :                                         /*
     566             :                                          * Grab the summary information for
     567             :                                          * this bitmap block.
     568             :                                          */
     569    43896156 :                                         error = xfs_rtany_summary(mp, tp,
     570    43896156 :                                                 log2len, mp->m_rsumlevels - 1,
     571             :                                                 bbno + j, rbpp, rsb, &any);
     572    43896156 :                                         if (error) {
     573           0 :                                                 return error;
     574             :                                         }
     575             :                                         /*
     576             :                                          * If there's no extent given in the
     577             :                                          * summary that means the extent we
     578             :                                          * found must carry over from an
     579             :                                          * earlier block.  If there is an
     580             :                                          * extent given, we've already tried
     581             :                                          * that allocation, don't do it again.
     582             :                                          */
     583    43896156 :                                         if (any)
     584          25 :                                                 continue;
     585    43896131 :                                         error = xfs_rtallocate_extent_block(mp,
     586             :                                                 tp, bbno + j, minlen, maxlen,
     587             :                                                 len, &n, rbpp, rsb, prod, &r);
     588    43896131 :                                         if (error) {
     589           0 :                                                 return error;
     590             :                                         }
     591             :                                         /*
     592             :                                          * If it works, return the extent.
     593             :                                          */
     594    43896131 :                                         if (r != NULLRTEXTNO) {
     595       67829 :                                                 *rtx = r;
     596       67829 :                                                 return 0;
     597             :                                         }
     598             :                                 }
     599             :                                 /*
     600             :                                  * There weren't intervening bitmap blocks
     601             :                                  * with a long enough extent, or the
     602             :                                  * allocation didn't work for some reason
     603             :                                  * (i.e. it's a little * too short).
     604             :                                  * Try to allocate from the summary block
     605             :                                  * that we found.
     606             :                                  */
     607     5457774 :                                 error = xfs_rtallocate_extent_block(mp, tp,
     608             :                                         bbno + i, minlen, maxlen, len, &n, rbpp,
     609             :                                         rsb, prod, &r);
     610     5457774 :                                 if (error) {
     611           0 :                                         return error;
     612             :                                 }
     613             :                                 /*
     614             :                                  * If it works, return the extent.
     615             :                                  */
     616     5457774 :                                 if (r != NULLRTEXTNO) {
     617     5457752 :                                         *rtx = r;
     618     5457752 :                                         return 0;
     619             :                                 }
     620             :                         }
     621             :                 }
     622             :                 /*
     623             :                  * Loop control.  If we were on the positive side, and there's
     624             :                  * still more blocks on the negative side, go there.
     625             :                  */
     626   147801194 :                 if (i > 0 && (int)bbno - i >= 0)
     627    59246996 :                         i = -i;
     628             :                 /*
     629             :                  * If positive, and no more negative, but there are more
     630             :                  * positive, go there.
     631             :                  */
     632    88554198 :                 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
     633     3295362 :                         i++;
     634             :                 /*
     635             :                  * If negative or 0 (just started), and there are positive
     636             :                  * blocks to go, go there.  The 0 case moves to block 1.
     637             :                  */
     638    85258836 :                 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
     639    66419856 :                         i = 1 - i;
     640             :                 /*
     641             :                  * If negative or 0 and there are more negative blocks,
     642             :                  * go there.
     643             :                  */
     644    18838980 :                 else if (i <= 0 && (int)bbno + i > 0)
     645    18838980 :                         i--;
     646             :                 /*
     647             :                  * Must be done.  Return failure.
     648             :                  */
     649             :                 else
     650             :                         break;
     651             :         }
     652           0 :         *rtx = NULLRTEXTNO;
     653           0 :         return 0;
     654             : }
     655             : 
     656             : /*
     657             :  * Allocate an extent of length minlen<=len<=maxlen, with no position
     658             :  * specified.  If we don't get maxlen then use prod to trim
     659             :  * the length, if given.  The lengths are all in rtextents.
     660             :  */
     661             : STATIC int                              /* error */
     662     1750766 : xfs_rtallocate_extent_size(
     663             :         xfs_mount_t     *mp,            /* file system mount point */
     664             :         xfs_trans_t     *tp,            /* transaction pointer */
     665             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     666             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     667             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     668             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     669             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     670             :         xfs_rtxlen_t    prod,           /* extent product factor */
     671             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     672             : {
     673     1750766 :         int             error;          /* error value */
     674     1750766 :         xfs_fileoff_t   i;              /* bitmap block number */
     675     1750766 :         int             l;              /* level number (loop control) */
     676     1750766 :         xfs_rtxnum_t    n;              /* next rtext to be tried */
     677     1750766 :         xfs_rtxnum_t    r;              /* result rtext number */
     678     1750766 :         xfs_suminfo_t   sum;            /* summary information for extents */
     679             : 
     680     1750766 :         ASSERT(minlen % prod == 0);
     681     1750766 :         ASSERT(maxlen % prod == 0);
     682     1750766 :         ASSERT(maxlen != 0);
     683             : 
     684             :         /*
     685             :          * Loop over all the levels starting with maxlen.
     686             :          * At each level, look at all the bitmap blocks, to see if there
     687             :          * are extents starting there that are long enough (>= maxlen).
     688             :          * Note, only on the initial level can the allocation fail if
     689             :          * the summary says there's an extent.
     690             :          */
     691     8278895 :         for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
     692             :                 /*
     693             :                  * Loop over all the bitmap blocks.
     694             :                  */
     695  1575836865 :                 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
     696             :                         /*
     697             :                          * Get the summary for this level/block.
     698             :                          */
     699  1571059502 :                         error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
     700             :                                 &sum);
     701  1571059502 :                         if (error) {
     702           0 :                                 return error;
     703             :                         }
     704             :                         /*
     705             :                          * Nothing there, on to the next block.
     706             :                          */
     707  1571059502 :                         if (!sum)
     708  1568725489 :                                 continue;
     709             :                         /*
     710             :                          * Try allocating the extent.
     711             :                          */
     712     2334013 :                         error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
     713             :                                 maxlen, len, &n, rbpp, rsb, prod, &r);
     714     2334013 :                         if (error) {
     715           0 :                                 return error;
     716             :                         }
     717             :                         /*
     718             :                          * If it worked, return that.
     719             :                          */
     720     2334013 :                         if (r != NULLRTEXTNO) {
     721     1750764 :                                 *rtx = r;
     722     1750764 :                                 return 0;
     723             :                         }
     724             :                         /*
     725             :                          * If the "next block to try" returned from the
     726             :                          * allocator is beyond the next bitmap block,
     727             :                          * skip to that bitmap block.
     728             :                          */
     729     1166498 :                         if (xfs_rtx_to_rbmblock(mp, n) > i + 1)
     730           0 :                                 i = xfs_rtx_to_rbmblock(mp, n) - 1;
     731             :                 }
     732             :         }
     733             :         /*
     734             :          * Didn't find any maxlen blocks.  Try smaller ones, unless
     735             :          * we're asking for a fixed size extent.
     736             :          */
     737           2 :         if (minlen > --maxlen) {
     738           0 :                 *rtx = NULLRTEXTNO;
     739           0 :                 return 0;
     740             :         }
     741           2 :         ASSERT(minlen != 0);
     742           2 :         ASSERT(maxlen != 0);
     743             : 
     744             :         /*
     745             :          * Loop over sizes, from maxlen down to minlen.
     746             :          * This time, when we do the allocations, allow smaller ones
     747             :          * to succeed.
     748             :          */
     749          14 :         for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
     750             :                 /*
     751             :                  * Loop over all the bitmap blocks, try an allocation
     752             :                  * starting in that block.
     753             :                  */
     754          58 :                 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
     755             :                         /*
     756             :                          * Get the summary information for this level/block.
     757             :                          */
     758          54 :                         error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
     759             :                                                   &sum);
     760          54 :                         if (error) {
     761           0 :                                 return error;
     762             :                         }
     763             :                         /*
     764             :                          * If nothing there, go on to next.
     765             :                          */
     766          54 :                         if (!sum)
     767          52 :                                 continue;
     768             :                         /*
     769             :                          * Try the allocation.  Make sure the specified
     770             :                          * minlen/maxlen are in the possible range for
     771             :                          * this summary level.
     772             :                          */
     773           2 :                         error = xfs_rtallocate_extent_block(mp, tp, i,
     774           2 :                                         XFS_RTMAX(minlen, 1 << l),
     775           2 :                                         XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
     776             :                                         len, &n, rbpp, rsb, prod, &r);
     777           2 :                         if (error) {
     778           0 :                                 return error;
     779             :                         }
     780             :                         /*
     781             :                          * If it worked, return that extent.
     782             :                          */
     783           2 :                         if (r != NULLRTEXTNO) {
     784           2 :                                 *rtx = r;
     785           2 :                                 return 0;
     786             :                         }
     787             :                         /*
     788             :                          * If the "next block to try" returned from the
     789             :                          * allocator is beyond the next bitmap block,
     790             :                          * skip to that bitmap block.
     791             :                          */
     792           0 :                         if (xfs_rtx_to_rbmblock(mp, n) > i + 1)
     793           0 :                                 i = xfs_rtx_to_rbmblock(mp, n) - 1;
     794             :                 }
     795             :         }
     796             :         /*
     797             :          * Got nothing, return failure.
     798             :          */
     799           0 :         *rtx = NULLRTEXTNO;
     800           0 :         return 0;
     801             : }
     802             : 
     803             : /* Get a buffer for the block. */
     804             : static int
     805          12 : xfs_growfs_init_rtbuf(
     806             :         struct xfs_trans        *tp,
     807             :         struct xfs_inode        *ip,
     808             :         xfs_fsblock_t           fsbno,
     809             :         enum xfs_blft           buf_type)
     810             : {
     811          12 :         struct xfs_mount        *mp = tp->t_mountp;
     812          12 :         struct xfs_buf          *bp;
     813          12 :         xfs_daddr_t             d;
     814          12 :         int                     error;
     815             : 
     816          12 :         d = XFS_FSB_TO_DADDR(mp, fsbno);
     817          12 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, 0,
     818             :                         &bp);
     819          12 :         if (error)
     820             :                 return error;
     821             : 
     822          12 :         xfs_trans_buf_set_type(tp, bp, buf_type);
     823          12 :         bp->b_ops = xfs_rtblock_ops(mp, buf_type == XFS_BLFT_RTSUMMARY_BUF);
     824          12 :         memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
     825             : 
     826          12 :         if (xfs_has_rtgroups(mp)) {
     827           3 :                 struct xfs_rtbuf_blkinfo        *hdr = bp->b_addr;
     828             : 
     829           3 :                 if (buf_type == XFS_BLFT_RTBITMAP_BUF)
     830           3 :                         hdr->rt_magic = cpu_to_be32(XFS_RTBITMAP_MAGIC);
     831             :                 else
     832           0 :                         hdr->rt_magic = cpu_to_be32(XFS_RTSUMMARY_MAGIC);
     833           3 :                 hdr->rt_owner = cpu_to_be64(ip->i_ino);
     834           3 :                 hdr->rt_blkno = cpu_to_be64(d);
     835           3 :                 uuid_copy(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid);
     836             :         }
     837             : 
     838          12 :         xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
     839          12 :         return 0;
     840             : }
     841             : 
     842             : /*
     843             :  * Allocate space to the bitmap or summary file, and zero it, for growfs.
     844             :  */
     845             : STATIC int
     846           5 : xfs_growfs_rt_alloc(
     847             :         struct xfs_mount        *mp,            /* file system mount point */
     848             :         xfs_extlen_t            oblocks,        /* old count of blocks */
     849             :         xfs_extlen_t            nblocks,        /* new count of blocks */
     850             :         struct xfs_inode        *ip)            /* inode (bitmap/summary) */
     851             : {
     852           5 :         xfs_fileoff_t           bno;            /* block number in file */
     853           5 :         int                     error;          /* error return value */
     854           5 :         xfs_fsblock_t           fsbno;          /* filesystem block for bno */
     855           5 :         struct xfs_bmbt_irec    map;            /* block map output */
     856           5 :         int                     nmap;           /* number of block maps */
     857           5 :         int                     resblks;        /* space reservation */
     858           5 :         enum xfs_blft           buf_type;
     859           5 :         struct xfs_trans        *tp;
     860             : 
     861           5 :         if (ip == mp->m_rsumip)
     862             :                 buf_type = XFS_BLFT_RTSUMMARY_BUF;
     863             :         else
     864           3 :                 buf_type = XFS_BLFT_RTBITMAP_BUF;
     865             : 
     866             :         /*
     867             :          * Allocate space to the file, as necessary.
     868             :          */
     869          15 :         while (oblocks < nblocks) {
     870          11 :                 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
     871             :                 /*
     872             :                  * Reserve space & log for one extent added to the file.
     873             :                  */
     874          11 :                 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_growrtalloc,
     875             :                                 resblks, 0, false, &tp);
     876          11 :                 if (error)
     877           0 :                         return error;
     878             : 
     879          11 :                 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
     880             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
     881          11 :                 if (error == -EFBIG)
     882           1 :                         error = xfs_iext_count_upgrade(tp, ip,
     883             :                                         XFS_IEXT_ADD_NOSPLIT_CNT);
     884          11 :                 if (error)
     885           1 :                         goto out_trans_cancel;
     886             : 
     887             :                 /*
     888             :                  * Allocate blocks to the bitmap file.
     889             :                  */
     890          10 :                 nmap = 1;
     891          10 :                 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
     892             :                                         XFS_BMAPI_METADATA, 0, &map, &nmap);
     893          10 :                 if (!error && nmap < 1)
     894             :                         error = -ENOSPC;
     895          10 :                 if (error)
     896           0 :                         goto out_trans_cancel;
     897             :                 /*
     898             :                  * Free any blocks freed up in the transaction, then commit.
     899             :                  */
     900          10 :                 error = xfs_trans_commit(tp);
     901          10 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     902          10 :                 if (error)
     903           0 :                         return error;
     904             :                 /*
     905             :                  * Now we need to clear the allocated blocks.
     906             :                  * Do this one block per transaction, to keep it simple.
     907             :                  */
     908          10 :                 for (bno = map.br_startoff, fsbno = map.br_startblock;
     909          22 :                      bno < map.br_startoff + map.br_blockcount;
     910          12 :                      bno++, fsbno++) {
     911             :                         /*
     912             :                          * Reserve log for one block zeroing.
     913             :                          */
     914          12 :                         error = xfs_trans_alloc_inode(ip,
     915             :                                         &M_RES(mp)->tr_growrtzero, 0, 0, false,
     916             :                                         &tp);
     917          12 :                         if (error)
     918           0 :                                 return error;
     919             : 
     920          12 :                         error = xfs_growfs_init_rtbuf(tp, ip, fsbno, buf_type);
     921          12 :                         if (error)
     922           0 :                                 goto out_trans_cancel;
     923             : 
     924             :                         /*
     925             :                          * Commit the transaction.
     926             :                          */
     927          12 :                         error = xfs_trans_commit(tp);
     928          12 :                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     929          12 :                         if (error)
     930           0 :                                 return error;
     931             :                 }
     932             :                 /*
     933             :                  * Go on to the next extent, if any.
     934             :                  */
     935          10 :                 oblocks = map.br_startoff + map.br_blockcount;
     936             :         }
     937             : 
     938             :         return 0;
     939             : 
     940           1 : out_trans_cancel:
     941           1 :         xfs_trans_cancel(tp);
     942           1 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     943           1 :         return error;
     944             : }
     945             : 
     946             : static void
     947       24322 : xfs_alloc_rsum_cache(
     948             :         xfs_mount_t     *mp,            /* file system mount structure */
     949             :         xfs_extlen_t    rbmblocks)      /* number of rt bitmap blocks */
     950             : {
     951             :         /*
     952             :          * The rsum cache is initialized to all zeroes, which is trivially a
     953             :          * lower bound on the minimum level with any free extents. We can
     954             :          * continue without the cache if it couldn't be allocated.
     955             :          */
     956       24322 :         mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
     957       24322 :         if (!mp->m_rsum_cache)
     958           0 :                 xfs_warn(mp, "could not allocate realtime summary cache");
     959       24322 : }
     960             : 
     961             : /*
     962             :  * Visible (exported) functions.
     963             :  */
     964             : 
     965             : static int
     966           5 : xfs_growfs_rt_free_new(
     967             :         struct xfs_trans        *tp,
     968             :         struct xfs_mount        *nmp,
     969             :         xfs_rtbxlen_t           *freed_rtx)
     970             : {
     971           5 :         struct xfs_mount        *mp = tp->t_mountp;
     972           5 :         struct xfs_sb           *sbp = &mp->m_sb;
     973           5 :         struct xfs_sb           *nsbp = &nmp->m_sb;
     974           5 :         struct xfs_buf          *bp = NULL;
     975           5 :         xfs_fileoff_t           sumbno;
     976           5 :         xfs_rtblock_t           rtbno, next_rtbno;
     977           5 :         int                     error = 0;
     978             : 
     979           5 :         if (!xfs_has_rtgroups(mp)) {
     980           0 :                 *freed_rtx = nsbp->sb_rextents - sbp->sb_rextents;
     981           0 :                 return xfs_rtfree_range(nmp, tp, sbp->sb_rextents, *freed_rtx,
     982             :                                 &bp, &sumbno);
     983             :         }
     984             : 
     985           5 :         *freed_rtx = 0;
     986             : 
     987           5 :         rtbno = xfs_rtx_to_rtb(nmp, sbp->sb_rextents);
     988           5 :         next_rtbno = xfs_rtx_to_rtb(nmp, nsbp->sb_rextents);
     989          20 :         while (rtbno < next_rtbno) {
     990          15 :                 xfs_rtxnum_t    start_rtx, next_rtx;
     991          15 :                 xfs_rtblock_t   next_free_rtbno;
     992          15 :                 xfs_rgnumber_t  rgno;
     993          15 :                 xfs_rgblock_t   rgbno;
     994             : 
     995             :                 /*
     996             :                  * Compute the first new extent that we want to free, being
     997             :                  * careful to skip past a realtime superblock at the start of
     998             :                  * the new region.
     999             :                  */
    1000          15 :                 rgbno = xfs_rtb_to_rgbno(nmp, rtbno, &rgno);
    1001          15 :                 if (rgbno == 0) {
    1002          10 :                         rtbno += nsbp->sb_rextsize;
    1003          10 :                         if (rtbno >= next_rtbno)
    1004             :                                 break;
    1005             :                 }
    1006             : 
    1007          15 :                 start_rtx = xfs_rtb_to_rtxt(nmp, rtbno);
    1008             : 
    1009             :                 /*
    1010             :                  * Stop freeing either at the end of the new rt section or at
    1011             :                  * the start of the next realtime group.
    1012             :                  */
    1013          15 :                 next_free_rtbno = xfs_rgbno_to_rtb(nmp, rgno + 1, 0);
    1014          15 :                 next_rtx = xfs_rtb_to_rtxt(nmp, next_free_rtbno);
    1015          15 :                 next_rtx = min(next_rtx, nsbp->sb_rextents);
    1016             : 
    1017          15 :                 bp = NULL;
    1018          15 :                 *freed_rtx += next_rtx - start_rtx;
    1019          15 :                 error = xfs_rtfree_range(nmp, tp, start_rtx,
    1020             :                                 next_rtx - start_rtx, &bp, &sumbno);
    1021          15 :                 if (error)
    1022             :                         break;
    1023             : 
    1024          15 :                 rtbno = next_free_rtbno;
    1025             :         }
    1026             : 
    1027             :         return error;
    1028             : }
    1029             : 
    1030             : static int
    1031           0 : xfs_growfs_rt_init_primary(
    1032             :         struct xfs_mount        *mp)
    1033             : {
    1034           0 :         struct xfs_buf          *rtsb_bp;
    1035           0 :         int                     error;
    1036             : 
    1037           0 :         error = xfs_buf_get_uncached(mp->m_rtdev_targp, XFS_FSB_TO_BB(mp, 1),
    1038             :                         0, &rtsb_bp);
    1039           0 :         if (error)
    1040             :                 return error;
    1041             : 
    1042           0 :         rtsb_bp->b_maps[0].bm_bn = XFS_RTSB_DADDR;
    1043           0 :         rtsb_bp->b_ops = &xfs_rtsb_buf_ops;
    1044             : 
    1045           0 :         xfs_rtgroup_update_super(rtsb_bp, mp->m_sb_bp);
    1046           0 :         mp->m_rtsb_bp = rtsb_bp;
    1047           0 :         xfs_buf_unlock(rtsb_bp);
    1048           0 :         return 0;
    1049             : }
    1050             : 
    1051             : /* Add a metadata inode for a realtime rmap btree. */
    1052             : static int
    1053          18 : xfs_growfsrt_create_rtrmap(
    1054             :         struct xfs_rtgroup      *rtg)
    1055             : {
    1056          18 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1057          18 :         struct xfs_imeta_update upd;
    1058          18 :         struct xfs_rmap_irec    rmap = {
    1059             :                 .rm_startblock  = 0,
    1060          18 :                 .rm_blockcount  = mp->m_sb.sb_rextsize,
    1061             :                 .rm_owner       = XFS_RMAP_OWN_FS,
    1062             :                 .rm_offset      = 0,
    1063             :                 .rm_flags       = 0,
    1064             :         };
    1065          18 :         struct xfs_btree_cur    *cur;
    1066          18 :         struct xfs_imeta_path   *path;
    1067          18 :         struct xfs_inode        *ip = NULL;
    1068          18 :         int                     error;
    1069             : 
    1070          18 :         if (!xfs_has_rtrmapbt(mp) || rtg->rtg_rmapip)
    1071             :                 return 0;
    1072             : 
    1073          10 :         error = xfs_rtrmapbt_create_path(mp, rtg->rtg_rgno, &path);
    1074          10 :         if (error)
    1075             :                 return error;
    1076             : 
    1077          10 :         error = xfs_imeta_ensure_dirpath(mp, path);
    1078          10 :         if (error)
    1079           0 :                 goto out_path;
    1080             : 
    1081          10 :         error = xfs_imeta_start_create(mp, path, &upd);
    1082          10 :         if (error)
    1083           0 :                 goto out_path;
    1084             : 
    1085          10 :         error = xfs_rtrmapbt_create(&upd, &ip);
    1086          10 :         if (error)
    1087           0 :                 goto out_cancel;
    1088             : 
    1089          10 :         lockdep_set_class(&ip->i_lock.mr_lock, &xfs_rrmapip_key);
    1090             : 
    1091             :         /* Rmap the rtgroup superblock; this had better fit in the data fork. */
    1092          10 :         cur = xfs_rtrmapbt_init_cursor(mp, upd.tp, rtg, ip);
    1093          10 :         error = xfs_rmap_map_raw(cur, &rmap);
    1094          10 :         xfs_btree_del_cursor(cur, error);
    1095          10 :         if (error)
    1096           0 :                 goto out_cancel;
    1097             : 
    1098          10 :         error = xfs_imeta_commit_update(&upd);
    1099          10 :         if (error)
    1100           0 :                 goto out_path;
    1101             : 
    1102          10 :         xfs_imeta_free_path(path);
    1103          10 :         xfs_finish_inode_setup(ip);
    1104          10 :         rtg->rtg_rmapip = ip;
    1105          10 :         return 0;
    1106             : 
    1107           0 : out_cancel:
    1108           0 :         xfs_imeta_cancel_update(&upd, error);
    1109             :         /* Have to finish setting up the inode to ensure it's deleted. */
    1110           0 :         if (ip) {
    1111           0 :                 xfs_finish_inode_setup(ip);
    1112           0 :                 xfs_irele(ip);
    1113             :         }
    1114           0 : out_path:
    1115           0 :         xfs_imeta_free_path(path);
    1116           0 :         return error;
    1117             : }
    1118             : 
    1119             : /* Add a metadata inode for a realtime refcount btree. */
    1120             : static int
    1121          18 : xfs_growfsrt_create_rtrefcount(
    1122             :         struct xfs_rtgroup      *rtg)
    1123             : {
    1124          18 :         struct xfs_imeta_update upd;
    1125          18 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1126          18 :         struct xfs_imeta_path   *path;
    1127          18 :         struct xfs_inode        *ip = NULL;
    1128          18 :         int                     error;
    1129             : 
    1130          18 :         if (!xfs_has_rtreflink(mp) || rtg->rtg_refcountip)
    1131             :                 return 0;
    1132             : 
    1133          10 :         error = xfs_rtrefcountbt_create_path(mp, rtg->rtg_rgno, &path);
    1134          10 :         if (error)
    1135             :                 return error;
    1136             : 
    1137          10 :         error = xfs_imeta_ensure_dirpath(mp, path);
    1138          10 :         if (error)
    1139           0 :                 goto out_path;
    1140             : 
    1141          10 :         error = xfs_imeta_start_create(mp, path, &upd);
    1142          10 :         if (error)
    1143           0 :                 goto out_path;
    1144             : 
    1145          10 :         error = xfs_rtrefcountbt_create(&upd, &ip);
    1146          10 :         if (error)
    1147           0 :                 goto out_cancel;
    1148             : 
    1149          10 :         lockdep_set_class(&ip->i_lock.mr_lock, &xfs_rrefcountip_key);
    1150             : 
    1151          10 :         error = xfs_imeta_commit_update(&upd);
    1152          10 :         if (error)
    1153           0 :                 goto out_path;
    1154             : 
    1155          10 :         xfs_imeta_free_path(path);
    1156          10 :         xfs_finish_inode_setup(ip);
    1157          10 :         rtg->rtg_refcountip = ip;
    1158          10 :         return 0;
    1159             : 
    1160             : out_cancel:
    1161           0 :         xfs_imeta_cancel_update(&upd, error);
    1162             :         /* Have to finish setting up the inode to ensure it's deleted. */
    1163           0 :         if (ip) {
    1164           0 :                 xfs_finish_inode_setup(ip);
    1165           0 :                 xfs_irele(ip);
    1166             :         }
    1167           0 : out_path:
    1168           0 :         xfs_imeta_free_path(path);
    1169           0 :         return error;
    1170             : }
    1171             : 
    1172             : /*
    1173             :  * Check that changes to the realtime geometry won't affect the minimum
    1174             :  * log size, which would cause the fs to become unusable.
    1175             :  */
    1176             : int
    1177         339 : xfs_growfs_check_rtgeom(
    1178             :         const struct xfs_mount  *mp,
    1179             :         xfs_rfsblock_t          dblocks,
    1180             :         xfs_rfsblock_t          rblocks,
    1181             :         xfs_agblock_t           rextsize,
    1182             :         xfs_rtblock_t           rextents,
    1183             :         xfs_extlen_t            rbmblocks,
    1184             :         uint8_t                 rextslog)
    1185             : {
    1186         339 :         struct xfs_mount        *fake_mp;
    1187         339 :         int                     min_logfsbs;
    1188             : 
    1189         339 :         fake_mp = kmem_alloc(sizeof(struct xfs_mount), KM_MAYFAIL);
    1190         339 :         if (!fake_mp)
    1191             :                 return -ENOMEM;
    1192             : 
    1193             :         /*
    1194             :          * Create a dummy xfs_mount with the new rt geometry, and compute the
    1195             :          * new minimum log size.  This ensures that the log is big enough to
    1196             :          * handle the larger transactions that we could start sending.
    1197             :          */
    1198         678 :         memcpy(fake_mp, mp, sizeof(struct xfs_mount));
    1199             : 
    1200         339 :         fake_mp->m_sb.sb_dblocks = dblocks;
    1201         339 :         fake_mp->m_sb.sb_rblocks = rblocks;
    1202         339 :         fake_mp->m_sb.sb_rextents = rextents;
    1203         339 :         fake_mp->m_sb.sb_rextsize = rextsize;
    1204         339 :         fake_mp->m_sb.sb_rbmblocks = rbmblocks;
    1205         339 :         fake_mp->m_sb.sb_rextslog = rextslog;
    1206         339 :         if (rblocks > 0)
    1207           3 :                 fake_mp->m_features |= XFS_FEAT_REALTIME;
    1208             : 
    1209         339 :         xfs_rtrmapbt_compute_maxlevels(fake_mp);
    1210         339 :         xfs_rtrefcountbt_compute_maxlevels(fake_mp);
    1211             : 
    1212         339 :         xfs_trans_resv_calc(fake_mp, M_RES(fake_mp));
    1213         339 :         min_logfsbs = xfs_log_calc_minimum_size(fake_mp);
    1214         339 :         trace_xfs_growfs_check_rtgeom(mp, min_logfsbs);
    1215             : 
    1216         339 :         kmem_free(fake_mp);
    1217             : 
    1218         339 :         if (mp->m_sb.sb_logblocks < min_logfsbs)
    1219           0 :                 return -ENOSPC;
    1220             : 
    1221             :         return 0;
    1222             : }
    1223             : 
    1224             : /*
    1225             :  * Grow the realtime area of the filesystem.
    1226             :  */
    1227             : int
    1228           3 : xfs_growfs_rt(
    1229             :         xfs_mount_t     *mp,            /* mount point for filesystem */
    1230             :         xfs_growfs_rt_t *in)            /* growfs rt input struct */
    1231             : {
    1232           3 :         xfs_fileoff_t   bmbno;          /* bitmap block number */
    1233           3 :         struct xfs_buf  *bp;            /* temporary buffer */
    1234           3 :         int             error;          /* error return value */
    1235           3 :         xfs_mount_t     *nmp;           /* new (fake) mount structure */
    1236           3 :         xfs_rfsblock_t  nrblocks;       /* new number of realtime blocks */
    1237           3 :         xfs_extlen_t    nrbmblocks;     /* new number of rt bitmap blocks */
    1238           3 :         xfs_rtxnum_t    nrextents;      /* new number of realtime extents */
    1239           3 :         uint8_t         nrextslog;      /* new log2 of sb_rextents */
    1240           3 :         xfs_extlen_t    nrsumblocks;    /* new number of summary blocks */
    1241           3 :         uint            nrsumlevels;    /* new rt summary levels */
    1242           3 :         uint            nrsumsize;      /* new size of rt summary, bytes */
    1243           3 :         xfs_sb_t        *nsbp;          /* new superblock */
    1244           3 :         xfs_extlen_t    rbmblocks;      /* current number of rt bitmap blocks */
    1245           3 :         xfs_extlen_t    rsumblocks;     /* current number of rt summary blks */
    1246           3 :         xfs_sb_t        *sbp;           /* old superblock */
    1247           3 :         uint8_t         *rsum_cache;    /* old summary cache */
    1248             : 
    1249           3 :         sbp = &mp->m_sb;
    1250             : 
    1251           3 :         if (!capable(CAP_SYS_ADMIN))
    1252             :                 return -EPERM;
    1253             : 
    1254             :         /* Needs to have been mounted with an rt device. */
    1255           3 :         if (!XFS_IS_REALTIME_MOUNT(mp))
    1256             :                 return -EINVAL;
    1257             :         /*
    1258             :          * Mount should fail if the rt bitmap/summary files don't load, but
    1259             :          * we'll check anyway.
    1260             :          */
    1261           3 :         if (!mp->m_rbmip || !mp->m_rsumip)
    1262             :                 return -EINVAL;
    1263             : 
    1264             :         /* Shrink not supported. */
    1265           3 :         if (in->newblocks <= sbp->sb_rblocks)
    1266             :                 return -EINVAL;
    1267             : 
    1268             :         /* Can only change rt extent size when adding rt volume. */
    1269           3 :         if (sbp->sb_rblocks > 0 && in->extsize != sbp->sb_rextsize)
    1270             :                 return -EINVAL;
    1271             : 
    1272             :         /* Range check the extent size. */
    1273           3 :         if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
    1274             :             XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
    1275             :                 return -EINVAL;
    1276             : 
    1277             :         /* Unsupported realtime features. */
    1278           3 :         if (!xfs_has_rtgroups(mp) && (xfs_has_rmapbt(mp) || xfs_has_reflink(mp)))
    1279             :                 return -EOPNOTSUPP;
    1280           5 :         if (xfs_has_reflink(mp) && !is_power_of_2(mp->m_sb.sb_rextsize) &&
    1281           0 :             (XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) & ~PAGE_MASK))
    1282             :                 return -EOPNOTSUPP;
    1283             : 
    1284           3 :         nrblocks = in->newblocks;
    1285           3 :         error = xfs_sb_validate_fsb_count(sbp, nrblocks);
    1286           3 :         if (error)
    1287             :                 return error;
    1288             :         /*
    1289             :          * Read in the last block of the device, make sure it exists.
    1290             :          */
    1291           3 :         error = xfs_buf_read_uncached(mp->m_rtdev_targp,
    1292           3 :                                 XFS_FSB_TO_BB(mp, nrblocks - 1),
    1293           3 :                                 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
    1294           3 :         if (error)
    1295             :                 return error;
    1296           3 :         xfs_buf_relse(bp);
    1297             : 
    1298             :         /*
    1299             :          * Calculate new parameters.  These are the final values to be reached.
    1300             :          */
    1301           3 :         nrextents = nrblocks;
    1302           3 :         do_div(nrextents, in->extsize);
    1303           3 :         nrbmblocks = xfs_rtbitmap_blockcount(mp, nrextents);
    1304           3 :         nrextslog = xfs_highbit32(nrextents);
    1305           3 :         nrsumlevels = nrextslog + 1;
    1306           3 :         nrsumblocks = xfs_rtsummary_blockcount(mp, nrsumlevels, nrbmblocks);
    1307           3 :         nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
    1308             :         /*
    1309             :          * New summary size can't be more than half the size of
    1310             :          * the log.  This prevents us from getting a log overflow,
    1311             :          * since we'll log basically the whole summary file at once.
    1312             :          */
    1313           3 :         if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
    1314             :                 return -EINVAL;
    1315             : 
    1316             :         /* Make sure the new fs size won't cause problems with the log. */
    1317           3 :         error = xfs_growfs_check_rtgeom(mp, mp->m_sb.sb_dblocks, nrblocks,
    1318             :                         in->extsize, nrextents, nrbmblocks, nrextslog);
    1319           3 :         if (error)
    1320             :                 return error;
    1321             : 
    1322             :         /* Allocate the new rt group structures */
    1323           3 :         if (xfs_has_rtgroups(mp)) {
    1324           2 :                 uint64_t        new_rgcount;
    1325             : 
    1326           2 :                 new_rgcount = howmany_64(nrblocks, mp->m_sb.sb_rgblocks);
    1327           2 :                 if (new_rgcount > XFS_MAX_RGNUMBER)
    1328             :                         return -EINVAL;
    1329             : 
    1330             :                 /*
    1331             :                  * We don't support changing the group size to match the extent
    1332             :                  * size, even if the size of the rt section is currently zero.
    1333             :                  */
    1334           2 :                 if (mp->m_sb.sb_rgblocks % in->extsize != 0)
    1335             :                         return -EOPNOTSUPP;
    1336             : 
    1337           2 :                 if (mp->m_sb.sb_rblocks == 0) {
    1338           0 :                         error = xfs_growfs_rt_init_primary(mp);
    1339           0 :                         if (error)
    1340             :                                 return error;
    1341             :                 }
    1342             : 
    1343           2 :                 if (new_rgcount > mp->m_sb.sb_rgcount) {
    1344           2 :                         error = xfs_initialize_rtgroups(mp, new_rgcount);
    1345           2 :                         if (error)
    1346             :                                 return error;
    1347             :                 }
    1348             :         }
    1349             : 
    1350             :         /*
    1351             :          * Get the old block counts for bitmap and summary inodes.
    1352             :          * These can't change since other growfs callers are locked out.
    1353             :          */
    1354           3 :         rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
    1355           3 :         rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
    1356             :         /*
    1357             :          * Allocate space to the bitmap and summary files, as necessary.
    1358             :          */
    1359           3 :         error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
    1360           3 :         if (error)
    1361             :                 return error;
    1362           2 :         error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
    1363           2 :         if (error)
    1364             :                 return error;
    1365             : 
    1366           2 :         rsum_cache = mp->m_rsum_cache;
    1367           2 :         if (nrbmblocks != sbp->sb_rbmblocks)
    1368           1 :                 xfs_alloc_rsum_cache(mp, nrbmblocks);
    1369             : 
    1370             :         /*
    1371             :          * Allocate a new (fake) mount/sb.
    1372             :          */
    1373           2 :         nmp = kmem_alloc(sizeof(*nmp), 0);
    1374             :         /*
    1375             :          * Loop over the bitmap blocks.
    1376             :          * We will do everything one bitmap block at a time.
    1377             :          * Skip the current block if it is exactly full.
    1378             :          * This also deals with the case where there were no rtextents before.
    1379             :          */
    1380           2 :         bmbno = sbp->sb_rbmblocks;
    1381           4 :         if (xfs_rtx_to_rbmword(mp, sbp->sb_rextents) != 0)
    1382           2 :                 bmbno--;
    1383           7 :         for (; bmbno < nrbmblocks; bmbno++) {
    1384           5 :                 struct xfs_trans        *tp;
    1385           5 :                 struct xfs_rtgroup      *rtg;
    1386           5 :                 xfs_rfsblock_t          nrblocks_step;
    1387           5 :                 xfs_rtbxlen_t           freed_rtx = 0;
    1388           5 :                 xfs_rgnumber_t          last_rgno = mp->m_sb.sb_rgcount - 1;
    1389             : 
    1390           5 :                 *nmp = *mp;
    1391           5 :                 nsbp = &nmp->m_sb;
    1392             :                 /*
    1393             :                  * Calculate new sb and mount fields for this round.
    1394             :                  */
    1395           5 :                 nsbp->sb_rextsize = in->extsize;
    1396           5 :                 nmp->m_rtxblklog = -1; /* don't use shift or masking */
    1397           5 :                 nsbp->sb_rbmblocks = bmbno + 1;
    1398           5 :                 nrblocks_step = (bmbno + 1) * mp->m_rtx_per_rbmblock *
    1399           5 :                                 nsbp->sb_rextsize;
    1400           5 :                 nsbp->sb_rblocks = min(nrblocks, nrblocks_step);
    1401           5 :                 nsbp->sb_rextents = xfs_rtb_to_rtxt(nmp, nsbp->sb_rblocks);
    1402           5 :                 ASSERT(nsbp->sb_rextents != 0);
    1403           5 :                 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
    1404           5 :                 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
    1405           5 :                 nrsumblocks = xfs_rtsummary_blockcount(mp, nrsumlevels,
    1406             :                                 nsbp->sb_rbmblocks);
    1407           5 :                 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
    1408             : 
    1409           5 :                 if (xfs_has_rtgroups(mp)) {
    1410           5 :                         xfs_rgnumber_t  rgno = last_rgno;
    1411             : 
    1412           5 :                         nsbp->sb_rgcount = howmany_64(nsbp->sb_rblocks,
    1413             :                                                       nsbp->sb_rgblocks);
    1414             : 
    1415          23 :                         for_each_rtgroup_range(mp, rgno, nsbp->sb_rgcount, rtg) {
    1416          18 :                                 error = xfs_growfsrt_create_rtrmap(rtg);
    1417          18 :                                 if (error) {
    1418           0 :                                         xfs_rtgroup_rele(rtg);
    1419           0 :                                         break;
    1420             :                                 }
    1421             : 
    1422          18 :                                 error = xfs_growfsrt_create_rtrefcount(rtg);
    1423          18 :                                 if (error) {
    1424           0 :                                         xfs_rtgroup_rele(rtg);
    1425           0 :                                         break;
    1426             :                                 }
    1427             :                         }
    1428             :                 }
    1429             : 
    1430             :                 /*
    1431             :                  * Start a transaction, get the log reservation.
    1432             :                  */
    1433           5 :                 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
    1434             :                                 &tp);
    1435           5 :                 if (error)
    1436             :                         break;
    1437             :                 /*
    1438             :                  * Lock out other callers by grabbing the bitmap and summary
    1439             :                  * inode locks and joining them to the transaction.
    1440             :                  */
    1441           5 :                 xfs_rtbitmap_lock(tp, mp);
    1442             :                 /*
    1443             :                  * Update the bitmap inode's size ondisk and incore.  We need
    1444             :                  * to update the incore size so that inode inactivation won't
    1445             :                  * punch what it thinks are "posteof" blocks.
    1446             :                  */
    1447           5 :                 mp->m_rbmip->i_disk_size =
    1448           5 :                         nsbp->sb_rbmblocks * nsbp->sb_blocksize;
    1449           5 :                 i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_disk_size);
    1450           5 :                 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
    1451             :                 /*
    1452             :                  * Update the summary inode's size.  We need to update the
    1453             :                  * incore size so that inode inactivation won't punch what it
    1454             :                  * thinks are "posteof" blocks.
    1455             :                  */
    1456           5 :                 mp->m_rsumip->i_disk_size = nmp->m_rsumsize;
    1457           5 :                 i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_disk_size);
    1458           5 :                 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
    1459             :                 /*
    1460             :                  * Copy summary data from old to new sizes.
    1461             :                  * Do this when the real size (not block-aligned) changes.
    1462             :                  */
    1463           5 :                 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
    1464           2 :                     mp->m_rsumlevels != nmp->m_rsumlevels) {
    1465           4 :                         error = xfs_rtcopy_summary(mp, nmp, tp);
    1466           4 :                         if (error)
    1467           0 :                                 goto error_cancel;
    1468             :                 }
    1469             : 
    1470             :                 /*
    1471             :                  * Update superblock fields.
    1472             :                  */
    1473           5 :                 if (nsbp->sb_rextsize != sbp->sb_rextsize)
    1474           0 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
    1475           0 :                                 nsbp->sb_rextsize - sbp->sb_rextsize);
    1476           5 :                 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
    1477           3 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
    1478           3 :                                 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
    1479           5 :                 if (nsbp->sb_rblocks != sbp->sb_rblocks)
    1480           5 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
    1481           5 :                                 nsbp->sb_rblocks - sbp->sb_rblocks);
    1482           5 :                 if (nsbp->sb_rextents != sbp->sb_rextents)
    1483           5 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
    1484           5 :                                 nsbp->sb_rextents - sbp->sb_rextents);
    1485           5 :                 if (nsbp->sb_rextslog != sbp->sb_rextslog)
    1486           3 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
    1487           3 :                                 nsbp->sb_rextslog - sbp->sb_rextslog);
    1488           5 :                 if (nsbp->sb_rgcount != sbp->sb_rgcount)
    1489           4 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RGCOUNT,
    1490           4 :                                 nsbp->sb_rgcount - sbp->sb_rgcount);
    1491             :                 /*
    1492             :                  * Free new extent.
    1493             :                  */
    1494           5 :                 error = xfs_growfs_rt_free_new(tp, nmp, &freed_rtx);
    1495           5 :                 if (error) {
    1496           0 : error_cancel:
    1497           0 :                         xfs_trans_cancel(tp);
    1498           0 :                         break;
    1499             :                 }
    1500             :                 /*
    1501             :                  * Mark more blocks free in the superblock.
    1502             :                  */
    1503           5 :                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, freed_rtx);
    1504             :                 /*
    1505             :                  * Update mp values into the real mp structure.
    1506             :                  */
    1507           5 :                 mp->m_rsumlevels = nrsumlevels;
    1508           5 :                 mp->m_rsumsize = nrsumsize;
    1509             : 
    1510           5 :                 error = xfs_trans_commit(tp);
    1511           5 :                 if (error)
    1512             :                         break;
    1513             : 
    1514          20 :                 for_each_rtgroup_from(mp, last_rgno, rtg)
    1515          15 :                         rtg->rtg_blockcount = xfs_rtgroup_block_count(mp,
    1516             :                                                                 rtg->rtg_rgno);
    1517             : 
    1518             :                 /*
    1519             :                  * Ensure the mount RT feature flag is now set, and compute new
    1520             :                  * maxlevels for rt btrees.
    1521             :                  */
    1522           5 :                 mp->m_features |= XFS_FEAT_REALTIME;
    1523           5 :                 xfs_rtrmapbt_compute_maxlevels(mp);
    1524           5 :                 xfs_rtrefcountbt_compute_maxlevels(mp);
    1525             :         }
    1526           2 :         if (error)
    1527           0 :                 goto out_free;
    1528             : 
    1529             :         /* Update secondary superblocks now the physical grow has completed */
    1530           2 :         error = xfs_update_secondary_sbs(mp);
    1531           2 :         if (error)
    1532           0 :                 goto out_free;
    1533             : 
    1534           2 :         error = xfs_rtgroup_update_secondary_sbs(mp);
    1535           2 :         if (error)
    1536           0 :                 goto out_free;
    1537             : 
    1538             :         /* Reset the rt metadata btree space reservations. */
    1539           2 :         xfs_rt_resv_free(mp);
    1540           2 :         error = xfs_rt_resv_init(mp);
    1541           2 :         if (error == -ENOSPC)
    1542           0 :                 error = 0;
    1543             : 
    1544           2 : out_free:
    1545             :         /*
    1546             :          * Free the fake mp structure.
    1547             :          */
    1548           2 :         kmem_free(nmp);
    1549             : 
    1550             :         /*
    1551             :          * If we had to allocate a new rsum_cache, we either need to free the
    1552             :          * old one (if we succeeded) or free the new one and restore the old one
    1553             :          * (if there was an error).
    1554             :          */
    1555           2 :         if (rsum_cache != mp->m_rsum_cache) {
    1556           1 :                 if (error) {
    1557           0 :                         kmem_free(mp->m_rsum_cache);
    1558           0 :                         mp->m_rsum_cache = rsum_cache;
    1559             :                 } else {
    1560           1 :                         kmem_free(rsum_cache);
    1561             :                 }
    1562             :         }
    1563             : 
    1564             :         return error;
    1565             : }
    1566             : 
    1567             : /*
    1568             :  * Allocate an extent in the realtime subvolume, with the usual allocation
    1569             :  * parameters.  The length units are all in realtime extents, as is the
    1570             :  * result block number.
    1571             :  */
    1572             : int                                     /* error */
    1573    23769774 : xfs_rtallocate_extent(
    1574             :         xfs_trans_t     *tp,            /* transaction pointer */
    1575             :         xfs_rtxnum_t    start,          /* starting rtext number to allocate */
    1576             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
    1577             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
    1578             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
    1579             :         int             wasdel,         /* was a delayed allocation extent */
    1580             :         xfs_rtxlen_t    prod,           /* extent product factor */
    1581             :         xfs_rtxnum_t    *rtblock)       /* out: start rtext allocated */
    1582             : {
    1583    23769774 :         xfs_mount_t     *mp = tp->t_mountp;
    1584    23769774 :         int             error;          /* error value */
    1585    23769774 :         xfs_rtxnum_t    r;              /* result allocated rtext */
    1586    23769774 :         xfs_fileoff_t   sb;             /* summary file block number */
    1587    23769774 :         struct xfs_buf  *sumbp;         /* summary file block buffer */
    1588             : 
    1589    23769774 :         ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
    1590    23769774 :         ASSERT(minlen > 0 && minlen <= maxlen);
    1591             : 
    1592             :         /*
    1593             :          * If prod is set then figure out what to do to minlen and maxlen.
    1594             :          */
    1595    23769774 :         if (prod > 1) {
    1596      310751 :                 xfs_rtxlen_t    i;
    1597             : 
    1598      310751 :                 if ((i = maxlen % prod))
    1599           0 :                         maxlen -= i;
    1600      310751 :                 if ((i = minlen % prod))
    1601      301835 :                         minlen += prod - i;
    1602      310751 :                 if (maxlen < minlen) {
    1603           0 :                         *rtblock = NULLRTEXTNO;
    1604           0 :                         return 0;
    1605             :                 }
    1606             :         }
    1607             : 
    1608    23769774 : retry:
    1609    23769774 :         sumbp = NULL;
    1610    23769774 :         if (start == 0) {
    1611     1750766 :                 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
    1612             :                                 &sumbp,     &sb, prod, &r);
    1613             :         } else {
    1614    22019008 :                 error = xfs_rtallocate_extent_near(mp, tp, start, minlen, maxlen,
    1615             :                                 len, &sumbp, &sb, prod, &r);
    1616             :         }
    1617             : 
    1618    23769774 :         if (error)
    1619           0 :                 return error;
    1620             : 
    1621             :         /*
    1622             :          * If it worked, update the superblock.
    1623             :          */
    1624    23769774 :         if (r != NULLRTEXTNO) {
    1625    23769774 :                 long    slen = (long)*len;
    1626             : 
    1627    23769774 :                 ASSERT(*len >= minlen && *len <= maxlen);
    1628    23769774 :                 if (wasdel)
    1629           0 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
    1630             :                 else
    1631    23769774 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
    1632           0 :         } else if (prod > 1) {
    1633           0 :                 prod = 1;
    1634           0 :                 goto retry;
    1635             :         }
    1636             : 
    1637    23769774 :         *rtblock = r;
    1638    23769774 :         return 0;
    1639             : }
    1640             : 
    1641             : /* Read the primary realtime group's superblock and attach it to the mount. */
    1642             : int
    1643       24343 : xfs_rtmount_readsb(
    1644             :         struct xfs_mount        *mp)
    1645             : {
    1646       24343 :         struct xfs_buf          *bp;
    1647       24343 :         int                     error;
    1648             : 
    1649       24343 :         if (!xfs_has_rtgroups(mp))
    1650             :                 return 0;
    1651       23728 :         if (mp->m_sb.sb_rblocks == 0)
    1652             :                 return 0;
    1653         239 :         if (mp->m_rtdev_targp == NULL) {
    1654           0 :                 xfs_warn(mp,
    1655             :         "Filesystem has a realtime volume, use rtdev=device option");
    1656           0 :                 return -ENODEV;
    1657             :         }
    1658             : 
    1659             :         /* m_blkbb_log is not set up yet */
    1660         239 :         error = xfs_buf_read_uncached(mp->m_rtdev_targp, XFS_RTSB_DADDR,
    1661         239 :                         mp->m_sb.sb_blocksize >> BBSHIFT, XBF_NO_IOACCT, &bp,
    1662             :                         &xfs_rtsb_buf_ops);
    1663         239 :         if (error) {
    1664           0 :                 xfs_warn(mp, "rt sb validate failed with error %d.", error);
    1665             :                 /* bad CRC means corrupted metadata */
    1666           0 :                 if (error == -EFSBADCRC)
    1667           0 :                         error = -EFSCORRUPTED;
    1668           0 :                 return error;
    1669             :         }
    1670             : 
    1671         239 :         mp->m_rtsb_bp = bp;
    1672         239 :         xfs_buf_unlock(bp);
    1673         239 :         return 0;
    1674             : }
    1675             : 
    1676             : /* Detach the realtime superblock from the mount and free it. */
    1677             : void
    1678       24349 : xfs_rtmount_freesb(
    1679             :         struct xfs_mount        *mp)
    1680             : {
    1681       24349 :         struct xfs_buf          *bp = mp->m_rtsb_bp;
    1682             : 
    1683       24349 :         if (!bp)
    1684             :                 return;
    1685             : 
    1686         241 :         xfs_buf_lock(bp);
    1687         241 :         mp->m_rtsb_bp = NULL;
    1688         241 :         xfs_buf_relse(bp);
    1689             : }
    1690             : 
    1691             : /*
    1692             :  * Initialize realtime fields in the mount structure.
    1693             :  */
    1694             : int                             /* error */
    1695       24333 : xfs_rtmount_init(
    1696             :         struct xfs_mount        *mp)    /* file system mount structure */
    1697             : {
    1698       24333 :         struct xfs_buf          *bp;    /* buffer for last block of subvolume */
    1699       24333 :         struct xfs_sb           *sbp;   /* filesystem superblock copy in mount */
    1700       24333 :         xfs_daddr_t             d;      /* address of last block of subvolume */
    1701       24333 :         unsigned int            rsumblocks;
    1702       24333 :         int                     error;
    1703             : 
    1704       24333 :         sbp = &mp->m_sb;
    1705       24333 :         if (sbp->sb_rblocks == 0)
    1706             :                 return 0;
    1707         241 :         if (mp->m_rtdev_targp == NULL) {
    1708           0 :                 xfs_warn(mp,
    1709             :         "Filesystem has a realtime volume, use rtdev=device option");
    1710           0 :                 return -ENODEV;
    1711             :         }
    1712         241 :         mp->m_rsumlevels = sbp->sb_rextslog + 1;
    1713         241 :         rsumblocks = xfs_rtsummary_blockcount(mp, mp->m_rsumlevels,
    1714             :                         mp->m_sb.sb_rbmblocks);
    1715         241 :         mp->m_rsumsize = XFS_FSB_TO_B(mp, rsumblocks);
    1716         241 :         mp->m_rbmip = mp->m_rsumip = NULL;
    1717             :         /*
    1718             :          * Check that the realtime section is an ok size.
    1719             :          */
    1720         241 :         d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
    1721         241 :         if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
    1722           0 :                 xfs_warn(mp, "realtime mount -- %llu != %llu",
    1723             :                         (unsigned long long) XFS_BB_TO_FSB(mp, d),
    1724             :                         (unsigned long long) mp->m_sb.sb_rblocks);
    1725           0 :                 return -EFBIG;
    1726             :         }
    1727         241 :         error = xfs_buf_read_uncached(mp->m_rtdev_targp,
    1728         241 :                                         d - XFS_FSB_TO_BB(mp, 1),
    1729             :                                         XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
    1730         241 :         if (error) {
    1731           0 :                 xfs_warn(mp, "realtime device size check failed");
    1732           0 :                 return error;
    1733             :         }
    1734         241 :         xfs_buf_relse(bp);
    1735         241 :         return 0;
    1736             : }
    1737             : 
    1738             : static int
    1739           0 : xfs_rtalloc_count_frextent(
    1740             :         struct xfs_mount                *mp,
    1741             :         struct xfs_trans                *tp,
    1742             :         const struct xfs_rtalloc_rec    *rec,
    1743             :         void                            *priv)
    1744             : {
    1745           0 :         uint64_t                        *valp = priv;
    1746             : 
    1747           0 :         *valp += rec->ar_extcount;
    1748           0 :         return 0;
    1749             : }
    1750             : 
    1751             : /*
    1752             :  * Reinitialize the number of free realtime extents from the realtime bitmap.
    1753             :  * Callers must ensure that there is no other activity in the filesystem.
    1754             :  */
    1755             : int
    1756           0 : xfs_rtalloc_reinit_frextents(
    1757             :         struct xfs_mount        *mp)
    1758             : {
    1759           0 :         uint64_t                val = 0;
    1760           0 :         int                     error;
    1761             : 
    1762           0 :         xfs_rtbitmap_lock_shared(mp, XFS_RBMLOCK_BITMAP);
    1763           0 :         error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
    1764             :                         &val);
    1765           0 :         xfs_rtbitmap_unlock_shared(mp, XFS_RBMLOCK_BITMAP);
    1766           0 :         if (error)
    1767             :                 return error;
    1768             : 
    1769           0 :         spin_lock(&mp->m_sb_lock);
    1770           0 :         mp->m_sb.sb_frextents = val;
    1771           0 :         spin_unlock(&mp->m_sb_lock);
    1772           0 :         percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
    1773           0 :         return 0;
    1774             : }
    1775             : 
    1776             : /* Free space reservations for rt metadata inodes. */
    1777             : void
    1778         574 : xfs_rt_resv_free(
    1779             :         struct xfs_mount        *mp)
    1780             : {
    1781         574 :         struct xfs_rtgroup      *rtg;
    1782         574 :         xfs_rgnumber_t          rgno;
    1783             : 
    1784        3470 :         for_each_rtgroup(mp, rgno, rtg) {
    1785        2896 :                 xfs_imeta_resv_free_inode(rtg->rtg_refcountip);
    1786        2896 :                 xfs_imeta_resv_free_inode(rtg->rtg_rmapip);
    1787             :         }
    1788         574 : }
    1789             : 
    1790             : /* Reserve space for rt metadata inodes' space expansion. */
    1791             : int
    1792         572 : xfs_rt_resv_init(
    1793             :         struct xfs_mount        *mp)
    1794             : {
    1795         572 :         struct xfs_rtgroup      *rtg;
    1796         572 :         xfs_filblks_t           ask;
    1797         572 :         xfs_rgnumber_t          rgno;
    1798         572 :         int                     error = 0;
    1799             : 
    1800        3448 :         for_each_rtgroup(mp, rgno, rtg) {
    1801        2876 :                 int             err2;
    1802             : 
    1803        2876 :                 ask = xfs_rtrmapbt_calc_reserves(mp);
    1804        2876 :                 err2 = xfs_imeta_resv_init_inode(rtg->rtg_rmapip, ask);
    1805        2876 :                 if (err2 && !error)
    1806           0 :                         error = err2;
    1807             : 
    1808        2876 :                 ask = xfs_rtrefcountbt_calc_reserves(mp);
    1809        2876 :                 err2 = xfs_imeta_resv_init_inode(rtg->rtg_refcountip, ask);
    1810        2876 :                 if (err2 && !error)
    1811           0 :                         error = err2;
    1812             :         }
    1813             : 
    1814         572 :         return error;
    1815             : }
    1816             : 
    1817             : static inline int
    1818             : __xfs_rt_iget(
    1819             :         struct xfs_mount        *mp,
    1820             :         xfs_ino_t               ino,
    1821             :         struct lock_class_key   *lockdep_key,
    1822             :         const char              *lockdep_key_name,
    1823             :         struct xfs_inode        **ipp)
    1824             : {
    1825       51064 :         int                     error;
    1826             : 
    1827       51064 :         error = xfs_imeta_iget(mp, ino, XFS_DIR3_FT_REG_FILE, ipp);
    1828       51064 :         if (error)
    1829           0 :                 return error;
    1830             : 
    1831             :         lockdep_set_class_and_name(&(*ipp)->i_lock.mr_lock, lockdep_key,
    1832             :                         lockdep_key_name);
    1833             :         return 0;
    1834             : }
    1835             : 
    1836             : #define xfs_rt_iget(mp, ino, lockdep_key, ipp) \
    1837             :         __xfs_rt_iget((mp), (ino), (lockdep_key), #lockdep_key, (ipp))
    1838             : 
    1839             : /* Load realtime rmap btree inode. */
    1840             : STATIC int
    1841        1211 : xfs_rtmount_rmapbt(
    1842             :         struct xfs_rtgroup      *rtg)
    1843             : {
    1844        1211 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1845        1211 :         struct xfs_imeta_path   *path;
    1846        1211 :         struct xfs_inode        *ip;
    1847        1211 :         xfs_ino_t               ino;
    1848        1211 :         int                     error;
    1849             : 
    1850        1211 :         if (!xfs_has_rtrmapbt(mp))
    1851             :                 return 0;
    1852             : 
    1853        1211 :         error = xfs_rtrmapbt_create_path(mp, rtg->rtg_rgno, &path);
    1854        1211 :         if (error)
    1855             :                 return error;
    1856             : 
    1857        1211 :         error = xfs_imeta_lookup(mp, path, &ino);
    1858        1211 :         if (error)
    1859           0 :                 goto out_path;
    1860             : 
    1861        1211 :         if (ino == NULLFSINO) {
    1862           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_RMAPBT);
    1863           0 :                 error = -EFSCORRUPTED;
    1864           0 :                 goto out_path;
    1865             :         }
    1866             : 
    1867        1211 :         error = xfs_rt_iget(mp, ino, &xfs_rrmapip_key, &ip);
    1868        1211 :         if (error)
    1869           0 :                 goto out_path;
    1870             : 
    1871        1211 :         if (XFS_IS_CORRUPT(mp, ip->i_df.if_format != XFS_DINODE_FMT_RMAP)) {
    1872           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_RMAPBT);
    1873           0 :                 error = -EFSCORRUPTED;
    1874           0 :                 goto out_rele;
    1875             :         }
    1876             : 
    1877        1211 :         rtg->rtg_rmapip = ip;
    1878        1211 :         ip = NULL;
    1879        1211 : out_rele:
    1880        1211 :         if (ip)
    1881           0 :                 xfs_imeta_irele(ip);
    1882        1211 : out_path:
    1883        1211 :         xfs_imeta_free_path(path);
    1884        1211 :         return error;
    1885             : }
    1886             : 
    1887             : /*
    1888             :  * Read in the bmbt of an rt metadata inode so that we never have to load them
    1889             :  * at runtime.  This enables the use of shared ILOCKs for rtbitmap scans.  Use
    1890             :  * an empty transaction to avoid deadlocking on loops in the bmbt.
    1891             :  */
    1892             : static inline int
    1893       48642 : xfs_rtmount_iread_extents(
    1894             :         struct xfs_inode        *ip)
    1895             : {
    1896       48642 :         struct xfs_trans        *tp;
    1897       48642 :         int                     error;
    1898             : 
    1899       48642 :         error = xfs_trans_alloc_empty(ip->i_mount, &tp);
    1900       48642 :         if (error)
    1901             :                 return error;
    1902             : 
    1903       48642 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1904             : 
    1905       48642 :         error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
    1906       48642 :         if (error)
    1907           0 :                 goto out_unlock;
    1908             : 
    1909       48642 :         if (xfs_inode_has_attr_fork(ip)) {
    1910       47296 :                 error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
    1911       47296 :                 if (error)
    1912           0 :                         goto out_unlock;
    1913             :         }
    1914             : 
    1915       48642 : out_unlock:
    1916       48642 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1917       48642 :         xfs_trans_cancel(tp);
    1918       48642 :         return error;
    1919             : }
    1920             : 
    1921             : /* Load realtime refcount btree inode. */
    1922             : STATIC int
    1923        1211 : xfs_rtmount_refcountbt(
    1924             :         struct xfs_rtgroup      *rtg)
    1925             : {
    1926        1211 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1927        1211 :         struct xfs_imeta_path   *path;
    1928        1211 :         struct xfs_inode        *ip;
    1929        1211 :         xfs_ino_t               ino;
    1930        1211 :         int                     error;
    1931             : 
    1932        1211 :         if (!xfs_has_rtreflink(mp))
    1933             :                 return 0;
    1934             : 
    1935        1211 :         error = xfs_rtrefcountbt_create_path(mp, rtg->rtg_rgno, &path);
    1936        1211 :         if (error)
    1937             :                 return error;
    1938             : 
    1939        1211 :         error = xfs_imeta_lookup(mp, path, &ino);
    1940        1211 :         if (error)
    1941           0 :                 goto out_path;
    1942             : 
    1943        1211 :         if (ino == NULLFSINO) {
    1944           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_REFCNTBT);
    1945           0 :                 error = -EFSCORRUPTED;
    1946           0 :                 goto out_path;
    1947             :         }
    1948             : 
    1949        1211 :         error = xfs_rt_iget(mp, ino, &xfs_rrefcountip_key, &ip);
    1950        1211 :         if (error)
    1951           0 :                 goto out_path;
    1952             : 
    1953        1211 :         if (XFS_IS_CORRUPT(mp, ip->i_df.if_format != XFS_DINODE_FMT_REFCOUNT)) {
    1954           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_REFCNTBT);
    1955           0 :                 error = -EFSCORRUPTED;
    1956           0 :                 goto out_rele;
    1957             :         }
    1958             : 
    1959        1211 :         rtg->rtg_refcountip = ip;
    1960        1211 :         ip = NULL;
    1961        1211 : out_rele:
    1962        1211 :         if (ip)
    1963           0 :                 xfs_imeta_irele(ip);
    1964        1211 : out_path:
    1965        1211 :         xfs_imeta_free_path(path);
    1966        1211 :         return error;
    1967             : }
    1968             : 
    1969             : /*
    1970             :  * Get the bitmap and summary inodes and the summary cache into the mount
    1971             :  * structure at mount time.
    1972             :  */
    1973             : int
    1974       24321 : xfs_rtmount_inodes(
    1975             :         struct xfs_mount        *mp)            /* file system mount structure */
    1976             : {
    1977       24321 :         struct xfs_sb           *sbp;
    1978       24321 :         struct xfs_rtgroup      *rtg;
    1979       24321 :         xfs_rgnumber_t          rgno;
    1980       24321 :         int                     error;          /* error return value */
    1981             : 
    1982       24321 :         sbp = &mp->m_sb;
    1983       24321 :         error = xfs_rt_iget(mp, mp->m_sb.sb_rbmino, &xfs_rbmip_key,
    1984             :                         &mp->m_rbmip);
    1985       24321 :         if (xfs_metadata_is_sick(error))
    1986           0 :                 xfs_rt_mark_sick(mp, XFS_SICK_RT_BITMAP);
    1987       24321 :         if (error)
    1988             :                 return error;
    1989       24321 :         ASSERT(mp->m_rbmip != NULL);
    1990             : 
    1991       24321 :         error = xfs_rtmount_iread_extents(mp->m_rbmip);
    1992       24321 :         if (error)
    1993           0 :                 goto out_rele_bitmap;
    1994             : 
    1995       24321 :         error = xfs_rt_iget(mp, mp->m_sb.sb_rsumino, &xfs_rsumip_key,
    1996             :                         &mp->m_rsumip);
    1997       24321 :         if (xfs_metadata_is_sick(error))
    1998           0 :                 xfs_rt_mark_sick(mp, XFS_SICK_RT_SUMMARY);
    1999       24321 :         if (error)
    2000           0 :                 goto out_rele_bitmap;
    2001       24321 :         ASSERT(mp->m_rsumip != NULL);
    2002             : 
    2003       24321 :         error = xfs_rtmount_iread_extents(mp->m_rsumip);
    2004       24321 :         if (error)
    2005           0 :                 goto out_rele_summary;
    2006             : 
    2007       25532 :         for_each_rtgroup(mp, rgno, rtg) {
    2008        1211 :                 rtg->rtg_blockcount = xfs_rtgroup_block_count(mp,
    2009             :                                                               rtg->rtg_rgno);
    2010             : 
    2011        1211 :                 error = xfs_rtmount_rmapbt(rtg);
    2012        1211 :                 if (error) {
    2013           0 :                         xfs_rtgroup_rele(rtg);
    2014           0 :                         goto out_rele_rtgroup;
    2015             :                 }
    2016             : 
    2017        1211 :                 error = xfs_rtmount_refcountbt(rtg);
    2018        1211 :                 if (error) {
    2019           0 :                         xfs_rtgroup_rele(rtg);
    2020           0 :                         goto out_rele_rtgroup;
    2021             :                 }
    2022             :         }
    2023             : 
    2024       24321 :         xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
    2025       24321 :         return 0;
    2026             : 
    2027           0 : out_rele_rtgroup:
    2028           0 :         for_each_rtgroup(mp, rgno, rtg) {
    2029           0 :                 if (rtg->rtg_refcountip)
    2030           0 :                         xfs_imeta_irele(rtg->rtg_refcountip);
    2031           0 :                 rtg->rtg_refcountip = NULL;
    2032             : 
    2033           0 :                 if (rtg->rtg_rmapip)
    2034           0 :                         xfs_imeta_irele(rtg->rtg_rmapip);
    2035           0 :                 rtg->rtg_rmapip = NULL;
    2036             :         }
    2037           0 : out_rele_summary:
    2038           0 :         xfs_imeta_irele(mp->m_rsumip);
    2039           0 : out_rele_bitmap:
    2040           0 :         xfs_imeta_irele(mp->m_rbmip);
    2041           0 :         return error;
    2042             : }
    2043             : 
    2044             : /*
    2045             :  * Attach dquots for realtime metadata files.  Prior to the introduction of the
    2046             :  * metadata directory tree, the rtbitmap and rtsummary inodes were counted in
    2047             :  * the root dquot icount, so we must dqattach them to maintain correct counts.
    2048             :  */
    2049             : int
    2050       22302 : xfs_rtmount_dqattach(
    2051             :         struct xfs_mount        *mp)
    2052             : {
    2053       22302 :         int                     error;
    2054             : 
    2055       22302 :         if (xfs_has_metadir(mp))
    2056             :                 return 0;
    2057             : 
    2058         247 :         error = xfs_qm_dqattach(mp->m_rbmip);
    2059         247 :         if (error)
    2060             :                 return error;
    2061             : 
    2062         247 :         return xfs_qm_dqattach(mp->m_rsumip);
    2063             : }
    2064             : 
    2065             : void
    2066       24327 : xfs_rtunmount_inodes(
    2067             :         struct xfs_mount        *mp)
    2068             : {
    2069       24327 :         struct xfs_rtgroup      *rtg;
    2070       24327 :         xfs_rgnumber_t          rgno;
    2071             : 
    2072       24327 :         kmem_free(mp->m_rsum_cache);
    2073             : 
    2074       25558 :         for_each_rtgroup(mp, rgno, rtg) {
    2075        1231 :                 if (rtg->rtg_refcountip)
    2076        1231 :                         xfs_imeta_irele(rtg->rtg_refcountip);
    2077        1231 :                 rtg->rtg_refcountip = NULL;
    2078             : 
    2079        1231 :                 if (rtg->rtg_rmapip)
    2080        1231 :                         xfs_imeta_irele(rtg->rtg_rmapip);
    2081        1231 :                 rtg->rtg_rmapip = NULL;
    2082             :         }
    2083       24327 :         if (mp->m_rbmip)
    2084       24327 :                 xfs_imeta_irele(mp->m_rbmip);
    2085       24327 :         if (mp->m_rsumip)
    2086       24327 :                 xfs_imeta_irele(mp->m_rsumip);
    2087       24327 : }
    2088             : 
    2089             : /*
    2090             :  * Pick an extent for allocation at the start of a new realtime file.
    2091             :  * Use the sequence number stored in the atime field of the bitmap inode.
    2092             :  * Translate this to a fraction of the rtextents, and return the product
    2093             :  * of rtextents and the fraction.
    2094             :  * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
    2095             :  */
    2096             : int                                     /* error */
    2097       32118 : xfs_rtpick_extent(
    2098             :         xfs_mount_t     *mp,            /* file system mount point */
    2099             :         xfs_trans_t     *tp,            /* transaction pointer */
    2100             :         xfs_rtxlen_t    len,            /* allocation length (rtextents) */
    2101             :         xfs_rtxnum_t    *pick)          /* result rt extent */
    2102             : {
    2103       32118 :         xfs_rtxnum_t    b;              /* result rtext */
    2104       32118 :         int             log2;           /* log of sequence number */
    2105       32118 :         uint64_t        resid;          /* residual after log removed */
    2106       32118 :         uint64_t        seq;            /* sequence number of file creation */
    2107       32118 :         uint64_t        *seqp;          /* pointer to seqno in inode */
    2108             : 
    2109       32118 :         ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
    2110             : 
    2111       32118 :         seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
    2112       32118 :         if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
    2113           0 :                 mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
    2114           0 :                 *seqp = 0;
    2115             :         }
    2116       32118 :         seq = *seqp;
    2117       40479 :         if ((log2 = xfs_highbit64(seq)) == -1)
    2118             :                 b = 0;
    2119             :         else {
    2120        8361 :                 resid = seq - (1ULL << log2);
    2121        8361 :                 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
    2122             :                     (log2 + 1);
    2123        8361 :                 if (b >= mp->m_sb.sb_rextents)
    2124           0 :                         div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
    2125        8361 :                 if (b + len > mp->m_sb.sb_rextents)
    2126           0 :                         b = mp->m_sb.sb_rextents - len;
    2127             :         }
    2128       32118 :         *seqp = seq + 1;
    2129       32118 :         xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
    2130       32118 :         *pick = b;
    2131       32118 :         return 0;
    2132             : }
    2133             : 
    2134             : /*
    2135             :  * Decide if this is an unwritten extent that isn't aligned to a rt extent
    2136             :  * boundary.  If it is, shorten the mapping so that we're ready to convert
    2137             :  * everything up to the next rt extent to a zeroed written extent.  If not,
    2138             :  * return false.
    2139             :  */
    2140             : static inline bool
    2141           0 : xfs_rtfile_want_conversion(
    2142             :         struct xfs_mount        *mp,
    2143             :         struct xfs_bmbt_irec    *irec)
    2144             : {
    2145           0 :         xfs_fileoff_t           rext_next;
    2146           0 :         xfs_extlen_t            modoff, modcnt;
    2147             : 
    2148           0 :         if (irec->br_state != XFS_EXT_UNWRITTEN)
    2149             :                 return false;
    2150             : 
    2151           0 :         xfs_rtb_to_rtx(mp, irec->br_startoff, &modoff);
    2152           0 :         if (modoff == 0) {
    2153           0 :                 xfs_rtbxlen_t   rexts;
    2154             : 
    2155           0 :                 rexts = xfs_rtb_to_rtx(mp, irec->br_blockcount, &modcnt);
    2156           0 :                 if (rexts > 0) {
    2157             :                         /*
    2158             :                          * Unwritten mapping starts at an rt extent boundary
    2159             :                          * and is longer than one rt extent.  Round the length
    2160             :                          * down to the nearest extent but don't select it for
    2161             :                          * conversion.
    2162             :                          */
    2163           0 :                         irec->br_blockcount -= modcnt;
    2164           0 :                         modcnt = 0;
    2165             :                 }
    2166             : 
    2167             :                 /* Unwritten mapping is perfectly aligned, do not convert. */
    2168           0 :                 if (modcnt == 0)
    2169           0 :                         return false;
    2170             :         }
    2171             : 
    2172             :         /*
    2173             :          * Unaligned and unwritten; trim to the current rt extent and select it
    2174             :          * for conversion.
    2175             :          */
    2176           0 :         rext_next = (irec->br_startoff - modoff) + mp->m_sb.sb_rextsize;
    2177           0 :         xfs_trim_extent(irec, irec->br_startoff, rext_next - irec->br_startoff);
    2178           0 :         return true;
    2179             : }
    2180             : 
    2181             : /*
    2182             :  * Find an unwritten extent in the given file range, zero it, and convert the
    2183             :  * mapping to written.  Adjust the scan cursor on the way out.
    2184             :  */
    2185             : STATIC int
    2186           0 : xfs_rtfile_convert_one(
    2187             :         struct xfs_inode        *ip,
    2188             :         xfs_fileoff_t           *offp,
    2189             :         xfs_fileoff_t           endoff)
    2190             : {
    2191           0 :         struct xfs_bmbt_irec    irec;
    2192           0 :         struct xfs_mount        *mp = ip->i_mount;
    2193           0 :         struct xfs_trans        *tp;
    2194           0 :         unsigned int            resblks;
    2195           0 :         int                     nmap;
    2196           0 :         int                     error;
    2197             : 
    2198           0 :         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 1);
    2199           0 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
    2200           0 :         if (error)
    2201             :                 return error;
    2202             : 
    2203           0 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    2204           0 :         xfs_trans_ijoin(tp, ip, 0);
    2205             : 
    2206             :         /*
    2207             :          * Read the mapping.  If we find an unwritten extent that isn't aligned
    2208             :          * to an rt extent boundary...
    2209             :          */
    2210           0 : retry:
    2211           0 :         nmap = 1;
    2212           0 :         error = xfs_bmapi_read(ip, *offp, endoff - *offp, &irec, &nmap, 0);
    2213           0 :         if (error)
    2214           0 :                 goto out_cancel;
    2215           0 :         ASSERT(nmap == 1);
    2216           0 :         ASSERT(irec.br_startoff == *offp);
    2217           0 :         if (!xfs_rtfile_want_conversion(mp, &irec)) {
    2218           0 :                 *offp = irec.br_startoff + irec.br_blockcount;
    2219           0 :                 if (*offp >= endoff)
    2220           0 :                         goto out_cancel;
    2221           0 :                 goto retry;
    2222             :         }
    2223             : 
    2224             :         /*
    2225             :          * ...make sure this partially unwritten rt extent gets converted to a
    2226             :          * zeroed written extent that we can remap.
    2227             :          */
    2228           0 :         nmap = 1;
    2229           0 :         error = xfs_bmapi_write(tp, ip, irec.br_startoff, irec.br_blockcount,
    2230             :                         XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO, 0, &irec, &nmap);
    2231           0 :         if (error)
    2232           0 :                 goto out_cancel;
    2233           0 :         ASSERT(nmap == 1);
    2234           0 :         if (irec.br_state != XFS_EXT_NORM) {
    2235           0 :                 ASSERT(0);
    2236           0 :                 error = -EIO;
    2237           0 :                 goto out_cancel;
    2238             :         }
    2239           0 :         error = xfs_trans_commit(tp);
    2240           0 :         if (error)
    2241           0 :                 goto out_unlock;
    2242             : 
    2243           0 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    2244           0 :         *offp = irec.br_startoff + irec.br_blockcount;
    2245           0 :         return 0;
    2246             : 
    2247           0 : out_cancel:
    2248           0 :         xfs_trans_cancel(tp);
    2249           0 : out_unlock:
    2250           0 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    2251           0 :         return error;
    2252             : }
    2253             : 
    2254             : /*
    2255             :  * For all realtime extents backing the given range of a file, search for
    2256             :  * unwritten mappings that do not cover a full rt extent and convert them
    2257             :  * to zeroed written mappings.  The goal is to end up with one mapping per rt
    2258             :  * extent so that we can perform a remapping operation.  Callers must ensure
    2259             :  * that there are no dirty pages in the given range.
    2260             :  */
    2261             : int
    2262           0 : xfs_rtfile_convert_unwritten(
    2263             :         struct xfs_inode        *ip,
    2264             :         loff_t                  pos,
    2265             :         uint64_t                len)
    2266             : {
    2267           0 :         struct xfs_mount        *mp = ip->i_mount;
    2268           0 :         xfs_fileoff_t           off;
    2269           0 :         xfs_fileoff_t           endoff;
    2270           0 :         int                     error;
    2271             : 
    2272           0 :         if (mp->m_sb.sb_rextsize == 1)
    2273             :                 return 0;
    2274             : 
    2275           0 :         off = xfs_rtb_rounddown_rtx(mp, XFS_B_TO_FSBT(mp, pos));
    2276           0 :         endoff = xfs_rtb_roundup_rtx(mp, XFS_B_TO_FSB(mp, pos + len));
    2277             : 
    2278           0 :         trace_xfs_rtfile_convert_unwritten(ip, pos, len);
    2279             : 
    2280           0 :         while (off < endoff) {
    2281           0 :                 if (fatal_signal_pending(current))
    2282             :                         return -EINTR;
    2283             : 
    2284           0 :                 error = xfs_rtfile_convert_one(ip, &off, endoff);
    2285           0 :                 if (error)
    2286           0 :                         return error;
    2287             :         }
    2288             : 
    2289             :         return 0;
    2290             : }
    2291             : 
    2292             : /*
    2293             :  * Find the next free realtime extent starting at @rtx and going no higher than
    2294             :  * @end_rtx.  Set @rtx and @len_rtx to whatever free extents we find, or to
    2295             :  * @end_rtx if we find no space.
    2296             :  */
    2297             : int
    2298           0 : xfs_rtalloc_find_freesp(
    2299             :         struct xfs_trans        *tp,
    2300             :         xfs_rtxnum_t            *rtx,
    2301             :         xfs_rtxnum_t            end_rtx,
    2302             :         xfs_rtxlen_t            *len_rtx)
    2303             : {
    2304           0 :         struct xfs_mount        *mp = tp->t_mountp;
    2305           0 :         unsigned int            max_rt_extlen;
    2306           0 :         int                     error;
    2307             : 
    2308           0 :         trace_xfs_rtalloc_find_freesp(mp, *rtx, end_rtx - *rtx);
    2309             : 
    2310           0 :         max_rt_extlen = xfs_rtb_to_rtxt(mp, XFS_MAX_BMBT_EXTLEN);
    2311             : 
    2312           0 :         while (*rtx < end_rtx) {
    2313           0 :                 xfs_rtblock_t   range_end_rtx;
    2314           0 :                 int             is_free = 0;
    2315             : 
    2316             :                 /* Is the first block in the range free? */
    2317           0 :                 error = xfs_rtcheck_range(mp, tp, *rtx, 1, 1, &range_end_rtx,
    2318             :                                 &is_free);
    2319           0 :                 if (error)
    2320           0 :                         return error;
    2321             : 
    2322             :                 /* Free or not, how many more rtx have the same status? */
    2323           0 :                 error = xfs_rtfind_forw(mp, tp, *rtx, end_rtx,
    2324             :                                 &range_end_rtx);
    2325           0 :                 if (error)
    2326           0 :                         return error;
    2327             : 
    2328           0 :                 if (is_free) {
    2329           0 :                         trace_xfs_rtalloc_find_freesp_done(mp, *rtx, *len_rtx);
    2330           0 :                         *len_rtx = min_t(xfs_rtblock_t, max_rt_extlen,
    2331             :                                          range_end_rtx - *rtx + 1);
    2332           0 :                         return 0;
    2333             :                 }
    2334             : 
    2335           0 :                 *rtx = range_end_rtx + 1;
    2336             :         }
    2337             : 
    2338             :         return 0;
    2339             : }

Generated by: LCOV version 1.14