1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_log_format.h"
11*4882a593Smuzhiyun #include "xfs_trans_resv.h"
12*4882a593Smuzhiyun #include "xfs_bit.h"
13*4882a593Smuzhiyun #include "xfs_mount.h"
14*4882a593Smuzhiyun #include "xfs_inode.h"
15*4882a593Smuzhiyun #include "xfs_bmap.h"
16*4882a593Smuzhiyun #include "xfs_bmap_btree.h"
17*4882a593Smuzhiyun #include "xfs_trans.h"
18*4882a593Smuzhiyun #include "xfs_trans_space.h"
19*4882a593Smuzhiyun #include "xfs_icache.h"
20*4882a593Smuzhiyun #include "xfs_rtalloc.h"
21*4882a593Smuzhiyun #include "xfs_sb.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Read and return the summary information for a given extent size,
25*4882a593Smuzhiyun * bitmap block combination.
26*4882a593Smuzhiyun * Keeps track of a current summary block, so we don't keep reading
27*4882a593Smuzhiyun * it from the buffer cache.
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun static int
xfs_rtget_summary(xfs_mount_t * mp,xfs_trans_t * tp,int log,xfs_rtblock_t bbno,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb,xfs_suminfo_t * sum)30*4882a593Smuzhiyun xfs_rtget_summary(
31*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount structure */
32*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
33*4882a593Smuzhiyun int log, /* log2 of extent size */
34*4882a593Smuzhiyun xfs_rtblock_t bbno, /* bitmap block number */
35*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
36*4882a593Smuzhiyun xfs_fsblock_t *rsb, /* in/out: summary block number */
37*4882a593Smuzhiyun xfs_suminfo_t *sum) /* out: summary info for this block */
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun * Return whether there are any free extents in the size range given
44*4882a593Smuzhiyun * by low and high, for the bitmap block bbno.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun STATIC int /* error */
xfs_rtany_summary(xfs_mount_t * mp,xfs_trans_t * tp,int low,int high,xfs_rtblock_t bbno,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb,int * stat)47*4882a593Smuzhiyun xfs_rtany_summary(
48*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount structure */
49*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
50*4882a593Smuzhiyun int low, /* low log2 extent size */
51*4882a593Smuzhiyun int high, /* high log2 extent size */
52*4882a593Smuzhiyun xfs_rtblock_t bbno, /* bitmap block number */
53*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
54*4882a593Smuzhiyun xfs_fsblock_t *rsb, /* in/out: summary block number */
55*4882a593Smuzhiyun int *stat) /* out: any good extents here? */
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun int error; /* error value */
58*4882a593Smuzhiyun int log; /* loop counter, log2 of ext. size */
59*4882a593Smuzhiyun xfs_suminfo_t sum; /* summary data */
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* There are no extents at levels < m_rsum_cache[bbno]. */
62*4882a593Smuzhiyun if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
63*4882a593Smuzhiyun low = mp->m_rsum_cache[bbno];
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun * Loop over logs of extent sizes.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun for (log = low; log <= high; log++) {
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * Get one summary datum.
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
73*4882a593Smuzhiyun if (error) {
74*4882a593Smuzhiyun return error;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * If there are any, return success.
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun if (sum) {
80*4882a593Smuzhiyun *stat = 1;
81*4882a593Smuzhiyun goto out;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun * Found nothing, return failure.
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun *stat = 0;
88*4882a593Smuzhiyun out:
89*4882a593Smuzhiyun /* There were no extents at levels < log. */
90*4882a593Smuzhiyun if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
91*4882a593Smuzhiyun mp->m_rsum_cache[bbno] = log;
92*4882a593Smuzhiyun return 0;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * Copy and transform the summary file, given the old and new
98*4882a593Smuzhiyun * parameters in the mount structures.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun STATIC int /* error */
xfs_rtcopy_summary(xfs_mount_t * omp,xfs_mount_t * nmp,xfs_trans_t * tp)101*4882a593Smuzhiyun xfs_rtcopy_summary(
102*4882a593Smuzhiyun xfs_mount_t *omp, /* old file system mount point */
103*4882a593Smuzhiyun xfs_mount_t *nmp, /* new file system mount point */
104*4882a593Smuzhiyun xfs_trans_t *tp) /* transaction pointer */
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun xfs_rtblock_t bbno; /* bitmap block number */
107*4882a593Smuzhiyun xfs_buf_t *bp; /* summary buffer */
108*4882a593Smuzhiyun int error; /* error return value */
109*4882a593Smuzhiyun int log; /* summary level number (log length) */
110*4882a593Smuzhiyun xfs_suminfo_t sum; /* summary data */
111*4882a593Smuzhiyun xfs_fsblock_t sumbno; /* summary block number */
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun bp = NULL;
114*4882a593Smuzhiyun for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
115*4882a593Smuzhiyun for (bbno = omp->m_sb.sb_rbmblocks - 1;
116*4882a593Smuzhiyun (xfs_srtblock_t)bbno >= 0;
117*4882a593Smuzhiyun bbno--) {
118*4882a593Smuzhiyun error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
119*4882a593Smuzhiyun &sumbno, &sum);
120*4882a593Smuzhiyun if (error)
121*4882a593Smuzhiyun return error;
122*4882a593Smuzhiyun if (sum == 0)
123*4882a593Smuzhiyun continue;
124*4882a593Smuzhiyun error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
125*4882a593Smuzhiyun &bp, &sumbno);
126*4882a593Smuzhiyun if (error)
127*4882a593Smuzhiyun return error;
128*4882a593Smuzhiyun error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
129*4882a593Smuzhiyun &bp, &sumbno);
130*4882a593Smuzhiyun if (error)
131*4882a593Smuzhiyun return error;
132*4882a593Smuzhiyun ASSERT(sum > 0);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun return 0;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * Mark an extent specified by start and len allocated.
139*4882a593Smuzhiyun * Updates all the summary information as well as the bitmap.
140*4882a593Smuzhiyun */
141*4882a593Smuzhiyun STATIC int /* error */
xfs_rtallocate_range(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t start,xfs_extlen_t len,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb)142*4882a593Smuzhiyun xfs_rtallocate_range(
143*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount point */
144*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
145*4882a593Smuzhiyun xfs_rtblock_t start, /* start block to allocate */
146*4882a593Smuzhiyun xfs_extlen_t len, /* length to allocate */
147*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
148*4882a593Smuzhiyun xfs_fsblock_t *rsb) /* in/out: summary block number */
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun xfs_rtblock_t end; /* end of the allocated extent */
151*4882a593Smuzhiyun int error; /* error value */
152*4882a593Smuzhiyun xfs_rtblock_t postblock = 0; /* first block allocated > end */
153*4882a593Smuzhiyun xfs_rtblock_t preblock = 0; /* first block allocated < start */
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun end = start + len - 1;
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun * Assume we're allocating out of the middle of a free extent.
158*4882a593Smuzhiyun * We need to find the beginning and end of the extent so we can
159*4882a593Smuzhiyun * properly update the summary.
160*4882a593Smuzhiyun */
161*4882a593Smuzhiyun error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
162*4882a593Smuzhiyun if (error) {
163*4882a593Smuzhiyun return error;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * Find the next allocated block (end of free extent).
167*4882a593Smuzhiyun */
168*4882a593Smuzhiyun error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
169*4882a593Smuzhiyun &postblock);
170*4882a593Smuzhiyun if (error) {
171*4882a593Smuzhiyun return error;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun /*
174*4882a593Smuzhiyun * Decrement the summary information corresponding to the entire
175*4882a593Smuzhiyun * (old) free extent.
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun error = xfs_rtmodify_summary(mp, tp,
178*4882a593Smuzhiyun XFS_RTBLOCKLOG(postblock + 1 - preblock),
179*4882a593Smuzhiyun XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
180*4882a593Smuzhiyun if (error) {
181*4882a593Smuzhiyun return error;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * If there are blocks not being allocated at the front of the
185*4882a593Smuzhiyun * old extent, add summary data for them to be free.
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun if (preblock < start) {
188*4882a593Smuzhiyun error = xfs_rtmodify_summary(mp, tp,
189*4882a593Smuzhiyun XFS_RTBLOCKLOG(start - preblock),
190*4882a593Smuzhiyun XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
191*4882a593Smuzhiyun if (error) {
192*4882a593Smuzhiyun return error;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * If there are blocks not being allocated at the end of the
197*4882a593Smuzhiyun * old extent, add summary data for them to be free.
198*4882a593Smuzhiyun */
199*4882a593Smuzhiyun if (postblock > end) {
200*4882a593Smuzhiyun error = xfs_rtmodify_summary(mp, tp,
201*4882a593Smuzhiyun XFS_RTBLOCKLOG(postblock - end),
202*4882a593Smuzhiyun XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
203*4882a593Smuzhiyun if (error) {
204*4882a593Smuzhiyun return error;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun * Modify the bitmap to mark this extent allocated.
209*4882a593Smuzhiyun */
210*4882a593Smuzhiyun error = xfs_rtmodify_range(mp, tp, start, len, 0);
211*4882a593Smuzhiyun return error;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * Attempt to allocate an extent minlen<=len<=maxlen starting from
216*4882a593Smuzhiyun * bitmap block bbno. If we don't get maxlen then use prod to trim
217*4882a593Smuzhiyun * the length, if given. Returns error; returns starting block in *rtblock.
218*4882a593Smuzhiyun * The lengths are all in rtextents.
219*4882a593Smuzhiyun */
220*4882a593Smuzhiyun STATIC int /* error */
xfs_rtallocate_extent_block(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t bbno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,xfs_rtblock_t * nextp,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)221*4882a593Smuzhiyun xfs_rtallocate_extent_block(
222*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount point */
223*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
224*4882a593Smuzhiyun xfs_rtblock_t bbno, /* bitmap block number */
225*4882a593Smuzhiyun xfs_extlen_t minlen, /* minimum length to allocate */
226*4882a593Smuzhiyun xfs_extlen_t maxlen, /* maximum length to allocate */
227*4882a593Smuzhiyun xfs_extlen_t *len, /* out: actual length allocated */
228*4882a593Smuzhiyun xfs_rtblock_t *nextp, /* out: next block to try */
229*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
230*4882a593Smuzhiyun xfs_fsblock_t *rsb, /* in/out: summary block number */
231*4882a593Smuzhiyun xfs_extlen_t prod, /* extent product factor */
232*4882a593Smuzhiyun xfs_rtblock_t *rtblock) /* out: start block allocated */
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun xfs_rtblock_t besti; /* best rtblock found so far */
235*4882a593Smuzhiyun xfs_rtblock_t bestlen; /* best length found so far */
236*4882a593Smuzhiyun xfs_rtblock_t end; /* last rtblock in chunk */
237*4882a593Smuzhiyun int error; /* error value */
238*4882a593Smuzhiyun xfs_rtblock_t i; /* current rtblock trying */
239*4882a593Smuzhiyun xfs_rtblock_t next; /* next rtblock to try */
240*4882a593Smuzhiyun int stat; /* status from internal calls */
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /*
243*4882a593Smuzhiyun * Loop over all the extents starting in this bitmap block,
244*4882a593Smuzhiyun * looking for one that's long enough.
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
247*4882a593Smuzhiyun end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
248*4882a593Smuzhiyun i <= end;
249*4882a593Smuzhiyun i++) {
250*4882a593Smuzhiyun /* Make sure we don't scan off the end of the rt volume. */
251*4882a593Smuzhiyun maxlen = min(mp->m_sb.sb_rextents, i + maxlen) - i;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /*
254*4882a593Smuzhiyun * See if there's a free extent of maxlen starting at i.
255*4882a593Smuzhiyun * If it's not so then next will contain the first non-free.
256*4882a593Smuzhiyun */
257*4882a593Smuzhiyun error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
258*4882a593Smuzhiyun if (error) {
259*4882a593Smuzhiyun return error;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun if (stat) {
262*4882a593Smuzhiyun /*
263*4882a593Smuzhiyun * i for maxlen is all free, allocate and return that.
264*4882a593Smuzhiyun */
265*4882a593Smuzhiyun error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
266*4882a593Smuzhiyun rsb);
267*4882a593Smuzhiyun if (error) {
268*4882a593Smuzhiyun return error;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun *len = maxlen;
271*4882a593Smuzhiyun *rtblock = i;
272*4882a593Smuzhiyun return 0;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun /*
275*4882a593Smuzhiyun * In the case where we have a variable-sized allocation
276*4882a593Smuzhiyun * request, figure out how big this free piece is,
277*4882a593Smuzhiyun * and if it's big enough for the minimum, and the best
278*4882a593Smuzhiyun * so far, remember it.
279*4882a593Smuzhiyun */
280*4882a593Smuzhiyun if (minlen < maxlen) {
281*4882a593Smuzhiyun xfs_rtblock_t thislen; /* this extent size */
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun thislen = next - i;
284*4882a593Smuzhiyun if (thislen >= minlen && thislen > bestlen) {
285*4882a593Smuzhiyun besti = i;
286*4882a593Smuzhiyun bestlen = thislen;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * If not done yet, find the start of the next free space.
291*4882a593Smuzhiyun */
292*4882a593Smuzhiyun if (next < end) {
293*4882a593Smuzhiyun error = xfs_rtfind_forw(mp, tp, next, end, &i);
294*4882a593Smuzhiyun if (error) {
295*4882a593Smuzhiyun return error;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun } else
298*4882a593Smuzhiyun break;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun /*
301*4882a593Smuzhiyun * Searched the whole thing & didn't find a maxlen free extent.
302*4882a593Smuzhiyun */
303*4882a593Smuzhiyun if (minlen < maxlen && besti != -1) {
304*4882a593Smuzhiyun xfs_extlen_t p; /* amount to trim length by */
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /*
307*4882a593Smuzhiyun * If size should be a multiple of prod, make that so.
308*4882a593Smuzhiyun */
309*4882a593Smuzhiyun if (prod > 1) {
310*4882a593Smuzhiyun div_u64_rem(bestlen, prod, &p);
311*4882a593Smuzhiyun if (p)
312*4882a593Smuzhiyun bestlen -= p;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * Allocate besti for bestlen & return that.
317*4882a593Smuzhiyun */
318*4882a593Smuzhiyun error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
319*4882a593Smuzhiyun if (error) {
320*4882a593Smuzhiyun return error;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun *len = bestlen;
323*4882a593Smuzhiyun *rtblock = besti;
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun /*
327*4882a593Smuzhiyun * Allocation failed. Set *nextp to the next block to try.
328*4882a593Smuzhiyun */
329*4882a593Smuzhiyun *nextp = next;
330*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
331*4882a593Smuzhiyun return 0;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /*
335*4882a593Smuzhiyun * Allocate an extent of length minlen<=len<=maxlen, starting at block
336*4882a593Smuzhiyun * bno. If we don't get maxlen then use prod to trim the length, if given.
337*4882a593Smuzhiyun * Returns error; returns starting block in *rtblock.
338*4882a593Smuzhiyun * The lengths are all in rtextents.
339*4882a593Smuzhiyun */
340*4882a593Smuzhiyun STATIC int /* error */
xfs_rtallocate_extent_exact(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t bno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)341*4882a593Smuzhiyun xfs_rtallocate_extent_exact(
342*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount point */
343*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
344*4882a593Smuzhiyun xfs_rtblock_t bno, /* starting block number to allocate */
345*4882a593Smuzhiyun xfs_extlen_t minlen, /* minimum length to allocate */
346*4882a593Smuzhiyun xfs_extlen_t maxlen, /* maximum length to allocate */
347*4882a593Smuzhiyun xfs_extlen_t *len, /* out: actual length allocated */
348*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
349*4882a593Smuzhiyun xfs_fsblock_t *rsb, /* in/out: summary block number */
350*4882a593Smuzhiyun xfs_extlen_t prod, /* extent product factor */
351*4882a593Smuzhiyun xfs_rtblock_t *rtblock) /* out: start block allocated */
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun int error; /* error value */
354*4882a593Smuzhiyun xfs_extlen_t i; /* extent length trimmed due to prod */
355*4882a593Smuzhiyun int isfree; /* extent is free */
356*4882a593Smuzhiyun xfs_rtblock_t next; /* next block to try (dummy) */
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun ASSERT(minlen % prod == 0 && maxlen % prod == 0);
359*4882a593Smuzhiyun /*
360*4882a593Smuzhiyun * Check if the range in question (for maxlen) is free.
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
363*4882a593Smuzhiyun if (error) {
364*4882a593Smuzhiyun return error;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun if (isfree) {
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * If it is, allocate it and return success.
369*4882a593Smuzhiyun */
370*4882a593Smuzhiyun error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
371*4882a593Smuzhiyun if (error) {
372*4882a593Smuzhiyun return error;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun *len = maxlen;
375*4882a593Smuzhiyun *rtblock = bno;
376*4882a593Smuzhiyun return 0;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun /*
379*4882a593Smuzhiyun * If not, allocate what there is, if it's at least minlen.
380*4882a593Smuzhiyun */
381*4882a593Smuzhiyun maxlen = next - bno;
382*4882a593Smuzhiyun if (maxlen < minlen) {
383*4882a593Smuzhiyun /*
384*4882a593Smuzhiyun * Failed, return failure status.
385*4882a593Smuzhiyun */
386*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
387*4882a593Smuzhiyun return 0;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun /*
390*4882a593Smuzhiyun * Trim off tail of extent, if prod is specified.
391*4882a593Smuzhiyun */
392*4882a593Smuzhiyun if (prod > 1 && (i = maxlen % prod)) {
393*4882a593Smuzhiyun maxlen -= i;
394*4882a593Smuzhiyun if (maxlen < minlen) {
395*4882a593Smuzhiyun /*
396*4882a593Smuzhiyun * Now we can't do it, return failure status.
397*4882a593Smuzhiyun */
398*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
399*4882a593Smuzhiyun return 0;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun /*
403*4882a593Smuzhiyun * Allocate what we can and return it.
404*4882a593Smuzhiyun */
405*4882a593Smuzhiyun error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
406*4882a593Smuzhiyun if (error) {
407*4882a593Smuzhiyun return error;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun *len = maxlen;
410*4882a593Smuzhiyun *rtblock = bno;
411*4882a593Smuzhiyun return 0;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun /*
415*4882a593Smuzhiyun * Allocate an extent of length minlen<=len<=maxlen, starting as near
416*4882a593Smuzhiyun * to bno as possible. If we don't get maxlen then use prod to trim
417*4882a593Smuzhiyun * the length, if given. The lengths are all in rtextents.
418*4882a593Smuzhiyun */
419*4882a593Smuzhiyun STATIC int /* error */
xfs_rtallocate_extent_near(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t bno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)420*4882a593Smuzhiyun xfs_rtallocate_extent_near(
421*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount point */
422*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
423*4882a593Smuzhiyun xfs_rtblock_t bno, /* starting block number to allocate */
424*4882a593Smuzhiyun xfs_extlen_t minlen, /* minimum length to allocate */
425*4882a593Smuzhiyun xfs_extlen_t maxlen, /* maximum length to allocate */
426*4882a593Smuzhiyun xfs_extlen_t *len, /* out: actual length allocated */
427*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
428*4882a593Smuzhiyun xfs_fsblock_t *rsb, /* in/out: summary block number */
429*4882a593Smuzhiyun xfs_extlen_t prod, /* extent product factor */
430*4882a593Smuzhiyun xfs_rtblock_t *rtblock) /* out: start block allocated */
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun int any; /* any useful extents from summary */
433*4882a593Smuzhiyun xfs_rtblock_t bbno; /* bitmap block number */
434*4882a593Smuzhiyun int error; /* error value */
435*4882a593Smuzhiyun int i; /* bitmap block offset (loop control) */
436*4882a593Smuzhiyun int j; /* secondary loop control */
437*4882a593Smuzhiyun int log2len; /* log2 of minlen */
438*4882a593Smuzhiyun xfs_rtblock_t n; /* next block to try */
439*4882a593Smuzhiyun xfs_rtblock_t r; /* result block */
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun ASSERT(minlen % prod == 0 && maxlen % prod == 0);
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun * If the block number given is off the end, silently set it to
444*4882a593Smuzhiyun * the last block.
445*4882a593Smuzhiyun */
446*4882a593Smuzhiyun if (bno >= mp->m_sb.sb_rextents)
447*4882a593Smuzhiyun bno = mp->m_sb.sb_rextents - 1;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* Make sure we don't run off the end of the rt volume. */
450*4882a593Smuzhiyun maxlen = min(mp->m_sb.sb_rextents, bno + maxlen) - bno;
451*4882a593Smuzhiyun if (maxlen < minlen) {
452*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
453*4882a593Smuzhiyun return 0;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun /*
457*4882a593Smuzhiyun * Try the exact allocation first.
458*4882a593Smuzhiyun */
459*4882a593Smuzhiyun error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
460*4882a593Smuzhiyun rbpp, rsb, prod, &r);
461*4882a593Smuzhiyun if (error) {
462*4882a593Smuzhiyun return error;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun * If the exact allocation worked, return that.
466*4882a593Smuzhiyun */
467*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
468*4882a593Smuzhiyun *rtblock = r;
469*4882a593Smuzhiyun return 0;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun bbno = XFS_BITTOBLOCK(mp, bno);
472*4882a593Smuzhiyun i = 0;
473*4882a593Smuzhiyun ASSERT(minlen != 0);
474*4882a593Smuzhiyun log2len = xfs_highbit32(minlen);
475*4882a593Smuzhiyun /*
476*4882a593Smuzhiyun * Loop over all bitmap blocks (bbno + i is current block).
477*4882a593Smuzhiyun */
478*4882a593Smuzhiyun for (;;) {
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun * Get summary information of extents of all useful levels
481*4882a593Smuzhiyun * starting in this bitmap block.
482*4882a593Smuzhiyun */
483*4882a593Smuzhiyun error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
484*4882a593Smuzhiyun bbno + i, rbpp, rsb, &any);
485*4882a593Smuzhiyun if (error) {
486*4882a593Smuzhiyun return error;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * If there are any useful extents starting here, try
490*4882a593Smuzhiyun * allocating one.
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun if (any) {
493*4882a593Smuzhiyun /*
494*4882a593Smuzhiyun * On the positive side of the starting location.
495*4882a593Smuzhiyun */
496*4882a593Smuzhiyun if (i >= 0) {
497*4882a593Smuzhiyun /*
498*4882a593Smuzhiyun * Try to allocate an extent starting in
499*4882a593Smuzhiyun * this block.
500*4882a593Smuzhiyun */
501*4882a593Smuzhiyun error = xfs_rtallocate_extent_block(mp, tp,
502*4882a593Smuzhiyun bbno + i, minlen, maxlen, len, &n, rbpp,
503*4882a593Smuzhiyun rsb, prod, &r);
504*4882a593Smuzhiyun if (error) {
505*4882a593Smuzhiyun return error;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun /*
508*4882a593Smuzhiyun * If it worked, return it.
509*4882a593Smuzhiyun */
510*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
511*4882a593Smuzhiyun *rtblock = r;
512*4882a593Smuzhiyun return 0;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun /*
516*4882a593Smuzhiyun * On the negative side of the starting location.
517*4882a593Smuzhiyun */
518*4882a593Smuzhiyun else { /* i < 0 */
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun * Loop backwards through the bitmap blocks from
521*4882a593Smuzhiyun * the starting point-1 up to where we are now.
522*4882a593Smuzhiyun * There should be an extent which ends in this
523*4882a593Smuzhiyun * bitmap block and is long enough.
524*4882a593Smuzhiyun */
525*4882a593Smuzhiyun for (j = -1; j > i; j--) {
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun * Grab the summary information for
528*4882a593Smuzhiyun * this bitmap block.
529*4882a593Smuzhiyun */
530*4882a593Smuzhiyun error = xfs_rtany_summary(mp, tp,
531*4882a593Smuzhiyun log2len, mp->m_rsumlevels - 1,
532*4882a593Smuzhiyun bbno + j, rbpp, rsb, &any);
533*4882a593Smuzhiyun if (error) {
534*4882a593Smuzhiyun return error;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun /*
537*4882a593Smuzhiyun * If there's no extent given in the
538*4882a593Smuzhiyun * summary that means the extent we
539*4882a593Smuzhiyun * found must carry over from an
540*4882a593Smuzhiyun * earlier block. If there is an
541*4882a593Smuzhiyun * extent given, we've already tried
542*4882a593Smuzhiyun * that allocation, don't do it again.
543*4882a593Smuzhiyun */
544*4882a593Smuzhiyun if (any)
545*4882a593Smuzhiyun continue;
546*4882a593Smuzhiyun error = xfs_rtallocate_extent_block(mp,
547*4882a593Smuzhiyun tp, bbno + j, minlen, maxlen,
548*4882a593Smuzhiyun len, &n, rbpp, rsb, prod, &r);
549*4882a593Smuzhiyun if (error) {
550*4882a593Smuzhiyun return error;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun /*
553*4882a593Smuzhiyun * If it works, return the extent.
554*4882a593Smuzhiyun */
555*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
556*4882a593Smuzhiyun *rtblock = r;
557*4882a593Smuzhiyun return 0;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun /*
561*4882a593Smuzhiyun * There weren't intervening bitmap blocks
562*4882a593Smuzhiyun * with a long enough extent, or the
563*4882a593Smuzhiyun * allocation didn't work for some reason
564*4882a593Smuzhiyun * (i.e. it's a little * too short).
565*4882a593Smuzhiyun * Try to allocate from the summary block
566*4882a593Smuzhiyun * that we found.
567*4882a593Smuzhiyun */
568*4882a593Smuzhiyun error = xfs_rtallocate_extent_block(mp, tp,
569*4882a593Smuzhiyun bbno + i, minlen, maxlen, len, &n, rbpp,
570*4882a593Smuzhiyun rsb, prod, &r);
571*4882a593Smuzhiyun if (error) {
572*4882a593Smuzhiyun return error;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun /*
575*4882a593Smuzhiyun * If it works, return the extent.
576*4882a593Smuzhiyun */
577*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
578*4882a593Smuzhiyun *rtblock = r;
579*4882a593Smuzhiyun return 0;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun /*
584*4882a593Smuzhiyun * Loop control. If we were on the positive side, and there's
585*4882a593Smuzhiyun * still more blocks on the negative side, go there.
586*4882a593Smuzhiyun */
587*4882a593Smuzhiyun if (i > 0 && (int)bbno - i >= 0)
588*4882a593Smuzhiyun i = -i;
589*4882a593Smuzhiyun /*
590*4882a593Smuzhiyun * If positive, and no more negative, but there are more
591*4882a593Smuzhiyun * positive, go there.
592*4882a593Smuzhiyun */
593*4882a593Smuzhiyun else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
594*4882a593Smuzhiyun i++;
595*4882a593Smuzhiyun /*
596*4882a593Smuzhiyun * If negative or 0 (just started), and there are positive
597*4882a593Smuzhiyun * blocks to go, go there. The 0 case moves to block 1.
598*4882a593Smuzhiyun */
599*4882a593Smuzhiyun else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
600*4882a593Smuzhiyun i = 1 - i;
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun * If negative or 0 and there are more negative blocks,
603*4882a593Smuzhiyun * go there.
604*4882a593Smuzhiyun */
605*4882a593Smuzhiyun else if (i <= 0 && (int)bbno + i > 0)
606*4882a593Smuzhiyun i--;
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun * Must be done. Return failure.
609*4882a593Smuzhiyun */
610*4882a593Smuzhiyun else
611*4882a593Smuzhiyun break;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
614*4882a593Smuzhiyun return 0;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /*
618*4882a593Smuzhiyun * Allocate an extent of length minlen<=len<=maxlen, with no position
619*4882a593Smuzhiyun * specified. If we don't get maxlen then use prod to trim
620*4882a593Smuzhiyun * the length, if given. The lengths are all in rtextents.
621*4882a593Smuzhiyun */
622*4882a593Smuzhiyun STATIC int /* error */
xfs_rtallocate_extent_size(xfs_mount_t * mp,xfs_trans_t * tp,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,xfs_buf_t ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)623*4882a593Smuzhiyun xfs_rtallocate_extent_size(
624*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount point */
625*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
626*4882a593Smuzhiyun xfs_extlen_t minlen, /* minimum length to allocate */
627*4882a593Smuzhiyun xfs_extlen_t maxlen, /* maximum length to allocate */
628*4882a593Smuzhiyun xfs_extlen_t *len, /* out: actual length allocated */
629*4882a593Smuzhiyun xfs_buf_t **rbpp, /* in/out: summary block buffer */
630*4882a593Smuzhiyun xfs_fsblock_t *rsb, /* in/out: summary block number */
631*4882a593Smuzhiyun xfs_extlen_t prod, /* extent product factor */
632*4882a593Smuzhiyun xfs_rtblock_t *rtblock) /* out: start block allocated */
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun int error; /* error value */
635*4882a593Smuzhiyun int i; /* bitmap block number */
636*4882a593Smuzhiyun int l; /* level number (loop control) */
637*4882a593Smuzhiyun xfs_rtblock_t n; /* next block to be tried */
638*4882a593Smuzhiyun xfs_rtblock_t r; /* result block number */
639*4882a593Smuzhiyun xfs_suminfo_t sum; /* summary information for extents */
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun ASSERT(minlen % prod == 0 && maxlen % prod == 0);
642*4882a593Smuzhiyun ASSERT(maxlen != 0);
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /*
645*4882a593Smuzhiyun * Loop over all the levels starting with maxlen.
646*4882a593Smuzhiyun * At each level, look at all the bitmap blocks, to see if there
647*4882a593Smuzhiyun * are extents starting there that are long enough (>= maxlen).
648*4882a593Smuzhiyun * Note, only on the initial level can the allocation fail if
649*4882a593Smuzhiyun * the summary says there's an extent.
650*4882a593Smuzhiyun */
651*4882a593Smuzhiyun for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun * Loop over all the bitmap blocks.
654*4882a593Smuzhiyun */
655*4882a593Smuzhiyun for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun * Get the summary for this level/block.
658*4882a593Smuzhiyun */
659*4882a593Smuzhiyun error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
660*4882a593Smuzhiyun &sum);
661*4882a593Smuzhiyun if (error) {
662*4882a593Smuzhiyun return error;
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun /*
665*4882a593Smuzhiyun * Nothing there, on to the next block.
666*4882a593Smuzhiyun */
667*4882a593Smuzhiyun if (!sum)
668*4882a593Smuzhiyun continue;
669*4882a593Smuzhiyun /*
670*4882a593Smuzhiyun * Try allocating the extent.
671*4882a593Smuzhiyun */
672*4882a593Smuzhiyun error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
673*4882a593Smuzhiyun maxlen, len, &n, rbpp, rsb, prod, &r);
674*4882a593Smuzhiyun if (error) {
675*4882a593Smuzhiyun return error;
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun /*
678*4882a593Smuzhiyun * If it worked, return that.
679*4882a593Smuzhiyun */
680*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
681*4882a593Smuzhiyun *rtblock = r;
682*4882a593Smuzhiyun return 0;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun /*
685*4882a593Smuzhiyun * If the "next block to try" returned from the
686*4882a593Smuzhiyun * allocator is beyond the next bitmap block,
687*4882a593Smuzhiyun * skip to that bitmap block.
688*4882a593Smuzhiyun */
689*4882a593Smuzhiyun if (XFS_BITTOBLOCK(mp, n) > i + 1)
690*4882a593Smuzhiyun i = XFS_BITTOBLOCK(mp, n) - 1;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun /*
694*4882a593Smuzhiyun * Didn't find any maxlen blocks. Try smaller ones, unless
695*4882a593Smuzhiyun * we're asking for a fixed size extent.
696*4882a593Smuzhiyun */
697*4882a593Smuzhiyun if (minlen > --maxlen) {
698*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
699*4882a593Smuzhiyun return 0;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun ASSERT(minlen != 0);
702*4882a593Smuzhiyun ASSERT(maxlen != 0);
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun /*
705*4882a593Smuzhiyun * Loop over sizes, from maxlen down to minlen.
706*4882a593Smuzhiyun * This time, when we do the allocations, allow smaller ones
707*4882a593Smuzhiyun * to succeed.
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun * Loop over all the bitmap blocks, try an allocation
712*4882a593Smuzhiyun * starting in that block.
713*4882a593Smuzhiyun */
714*4882a593Smuzhiyun for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
715*4882a593Smuzhiyun /*
716*4882a593Smuzhiyun * Get the summary information for this level/block.
717*4882a593Smuzhiyun */
718*4882a593Smuzhiyun error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
719*4882a593Smuzhiyun &sum);
720*4882a593Smuzhiyun if (error) {
721*4882a593Smuzhiyun return error;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun /*
724*4882a593Smuzhiyun * If nothing there, go on to next.
725*4882a593Smuzhiyun */
726*4882a593Smuzhiyun if (!sum)
727*4882a593Smuzhiyun continue;
728*4882a593Smuzhiyun /*
729*4882a593Smuzhiyun * Try the allocation. Make sure the specified
730*4882a593Smuzhiyun * minlen/maxlen are in the possible range for
731*4882a593Smuzhiyun * this summary level.
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun error = xfs_rtallocate_extent_block(mp, tp, i,
734*4882a593Smuzhiyun XFS_RTMAX(minlen, 1 << l),
735*4882a593Smuzhiyun XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
736*4882a593Smuzhiyun len, &n, rbpp, rsb, prod, &r);
737*4882a593Smuzhiyun if (error) {
738*4882a593Smuzhiyun return error;
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun /*
741*4882a593Smuzhiyun * If it worked, return that extent.
742*4882a593Smuzhiyun */
743*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
744*4882a593Smuzhiyun *rtblock = r;
745*4882a593Smuzhiyun return 0;
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun /*
748*4882a593Smuzhiyun * If the "next block to try" returned from the
749*4882a593Smuzhiyun * allocator is beyond the next bitmap block,
750*4882a593Smuzhiyun * skip to that bitmap block.
751*4882a593Smuzhiyun */
752*4882a593Smuzhiyun if (XFS_BITTOBLOCK(mp, n) > i + 1)
753*4882a593Smuzhiyun i = XFS_BITTOBLOCK(mp, n) - 1;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun /*
757*4882a593Smuzhiyun * Got nothing, return failure.
758*4882a593Smuzhiyun */
759*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
760*4882a593Smuzhiyun return 0;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /*
764*4882a593Smuzhiyun * Allocate space to the bitmap or summary file, and zero it, for growfs.
765*4882a593Smuzhiyun */
766*4882a593Smuzhiyun STATIC int
xfs_growfs_rt_alloc(struct xfs_mount * mp,xfs_extlen_t oblocks,xfs_extlen_t nblocks,struct xfs_inode * ip)767*4882a593Smuzhiyun xfs_growfs_rt_alloc(
768*4882a593Smuzhiyun struct xfs_mount *mp, /* file system mount point */
769*4882a593Smuzhiyun xfs_extlen_t oblocks, /* old count of blocks */
770*4882a593Smuzhiyun xfs_extlen_t nblocks, /* new count of blocks */
771*4882a593Smuzhiyun struct xfs_inode *ip) /* inode (bitmap/summary) */
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun xfs_fileoff_t bno; /* block number in file */
774*4882a593Smuzhiyun struct xfs_buf *bp; /* temporary buffer for zeroing */
775*4882a593Smuzhiyun xfs_daddr_t d; /* disk block address */
776*4882a593Smuzhiyun int error; /* error return value */
777*4882a593Smuzhiyun xfs_fsblock_t fsbno; /* filesystem block for bno */
778*4882a593Smuzhiyun struct xfs_bmbt_irec map; /* block map output */
779*4882a593Smuzhiyun int nmap; /* number of block maps */
780*4882a593Smuzhiyun int resblks; /* space reservation */
781*4882a593Smuzhiyun enum xfs_blft buf_type;
782*4882a593Smuzhiyun struct xfs_trans *tp;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun if (ip == mp->m_rsumip)
785*4882a593Smuzhiyun buf_type = XFS_BLFT_RTSUMMARY_BUF;
786*4882a593Smuzhiyun else
787*4882a593Smuzhiyun buf_type = XFS_BLFT_RTBITMAP_BUF;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun /*
790*4882a593Smuzhiyun * Allocate space to the file, as necessary.
791*4882a593Smuzhiyun */
792*4882a593Smuzhiyun while (oblocks < nblocks) {
793*4882a593Smuzhiyun resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
794*4882a593Smuzhiyun /*
795*4882a593Smuzhiyun * Reserve space & log for one extent added to the file.
796*4882a593Smuzhiyun */
797*4882a593Smuzhiyun error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
798*4882a593Smuzhiyun 0, 0, &tp);
799*4882a593Smuzhiyun if (error)
800*4882a593Smuzhiyun return error;
801*4882a593Smuzhiyun /*
802*4882a593Smuzhiyun * Lock the inode.
803*4882a593Smuzhiyun */
804*4882a593Smuzhiyun xfs_ilock(ip, XFS_ILOCK_EXCL);
805*4882a593Smuzhiyun xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun /*
808*4882a593Smuzhiyun * Allocate blocks to the bitmap file.
809*4882a593Smuzhiyun */
810*4882a593Smuzhiyun nmap = 1;
811*4882a593Smuzhiyun error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
812*4882a593Smuzhiyun XFS_BMAPI_METADATA, 0, &map, &nmap);
813*4882a593Smuzhiyun if (!error && nmap < 1)
814*4882a593Smuzhiyun error = -ENOSPC;
815*4882a593Smuzhiyun if (error)
816*4882a593Smuzhiyun goto out_trans_cancel;
817*4882a593Smuzhiyun /*
818*4882a593Smuzhiyun * Free any blocks freed up in the transaction, then commit.
819*4882a593Smuzhiyun */
820*4882a593Smuzhiyun error = xfs_trans_commit(tp);
821*4882a593Smuzhiyun if (error)
822*4882a593Smuzhiyun return error;
823*4882a593Smuzhiyun /*
824*4882a593Smuzhiyun * Now we need to clear the allocated blocks.
825*4882a593Smuzhiyun * Do this one block per transaction, to keep it simple.
826*4882a593Smuzhiyun */
827*4882a593Smuzhiyun for (bno = map.br_startoff, fsbno = map.br_startblock;
828*4882a593Smuzhiyun bno < map.br_startoff + map.br_blockcount;
829*4882a593Smuzhiyun bno++, fsbno++) {
830*4882a593Smuzhiyun /*
831*4882a593Smuzhiyun * Reserve log for one block zeroing.
832*4882a593Smuzhiyun */
833*4882a593Smuzhiyun error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
834*4882a593Smuzhiyun 0, 0, 0, &tp);
835*4882a593Smuzhiyun if (error)
836*4882a593Smuzhiyun return error;
837*4882a593Smuzhiyun /*
838*4882a593Smuzhiyun * Lock the bitmap inode.
839*4882a593Smuzhiyun */
840*4882a593Smuzhiyun xfs_ilock(ip, XFS_ILOCK_EXCL);
841*4882a593Smuzhiyun xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
842*4882a593Smuzhiyun /*
843*4882a593Smuzhiyun * Get a buffer for the block.
844*4882a593Smuzhiyun */
845*4882a593Smuzhiyun d = XFS_FSB_TO_DADDR(mp, fsbno);
846*4882a593Smuzhiyun error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
847*4882a593Smuzhiyun mp->m_bsize, 0, &bp);
848*4882a593Smuzhiyun if (error)
849*4882a593Smuzhiyun goto out_trans_cancel;
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, bp, buf_type);
852*4882a593Smuzhiyun bp->b_ops = &xfs_rtbuf_ops;
853*4882a593Smuzhiyun memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
854*4882a593Smuzhiyun xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
855*4882a593Smuzhiyun /*
856*4882a593Smuzhiyun * Commit the transaction.
857*4882a593Smuzhiyun */
858*4882a593Smuzhiyun error = xfs_trans_commit(tp);
859*4882a593Smuzhiyun if (error)
860*4882a593Smuzhiyun return error;
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun /*
863*4882a593Smuzhiyun * Go on to the next extent, if any.
864*4882a593Smuzhiyun */
865*4882a593Smuzhiyun oblocks = map.br_startoff + map.br_blockcount;
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun return 0;
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun out_trans_cancel:
871*4882a593Smuzhiyun xfs_trans_cancel(tp);
872*4882a593Smuzhiyun return error;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun static void
xfs_alloc_rsum_cache(xfs_mount_t * mp,xfs_extlen_t rbmblocks)876*4882a593Smuzhiyun xfs_alloc_rsum_cache(
877*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount structure */
878*4882a593Smuzhiyun xfs_extlen_t rbmblocks) /* number of rt bitmap blocks */
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun /*
881*4882a593Smuzhiyun * The rsum cache is initialized to all zeroes, which is trivially a
882*4882a593Smuzhiyun * lower bound on the minimum level with any free extents. We can
883*4882a593Smuzhiyun * continue without the cache if it couldn't be allocated.
884*4882a593Smuzhiyun */
885*4882a593Smuzhiyun mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
886*4882a593Smuzhiyun if (!mp->m_rsum_cache)
887*4882a593Smuzhiyun xfs_warn(mp, "could not allocate realtime summary cache");
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun /*
891*4882a593Smuzhiyun * Visible (exported) functions.
892*4882a593Smuzhiyun */
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /*
895*4882a593Smuzhiyun * Grow the realtime area of the filesystem.
896*4882a593Smuzhiyun */
897*4882a593Smuzhiyun int
xfs_growfs_rt(xfs_mount_t * mp,xfs_growfs_rt_t * in)898*4882a593Smuzhiyun xfs_growfs_rt(
899*4882a593Smuzhiyun xfs_mount_t *mp, /* mount point for filesystem */
900*4882a593Smuzhiyun xfs_growfs_rt_t *in) /* growfs rt input struct */
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun xfs_rtblock_t bmbno; /* bitmap block number */
903*4882a593Smuzhiyun xfs_buf_t *bp; /* temporary buffer */
904*4882a593Smuzhiyun int error; /* error return value */
905*4882a593Smuzhiyun xfs_mount_t *nmp; /* new (fake) mount structure */
906*4882a593Smuzhiyun xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
907*4882a593Smuzhiyun xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
908*4882a593Smuzhiyun xfs_rtblock_t nrextents; /* new number of realtime extents */
909*4882a593Smuzhiyun uint8_t nrextslog; /* new log2 of sb_rextents */
910*4882a593Smuzhiyun xfs_extlen_t nrsumblocks; /* new number of summary blocks */
911*4882a593Smuzhiyun uint nrsumlevels; /* new rt summary levels */
912*4882a593Smuzhiyun uint nrsumsize; /* new size of rt summary, bytes */
913*4882a593Smuzhiyun xfs_sb_t *nsbp; /* new superblock */
914*4882a593Smuzhiyun xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
915*4882a593Smuzhiyun xfs_extlen_t rsumblocks; /* current number of rt summary blks */
916*4882a593Smuzhiyun xfs_sb_t *sbp; /* old superblock */
917*4882a593Smuzhiyun xfs_fsblock_t sumbno; /* summary block number */
918*4882a593Smuzhiyun uint8_t *rsum_cache; /* old summary cache */
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun sbp = &mp->m_sb;
921*4882a593Smuzhiyun /*
922*4882a593Smuzhiyun * Initial error checking.
923*4882a593Smuzhiyun */
924*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
925*4882a593Smuzhiyun return -EPERM;
926*4882a593Smuzhiyun if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
927*4882a593Smuzhiyun (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
928*4882a593Smuzhiyun (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
929*4882a593Smuzhiyun return -EINVAL;
930*4882a593Smuzhiyun if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
931*4882a593Smuzhiyun return error;
932*4882a593Smuzhiyun /*
933*4882a593Smuzhiyun * Read in the last block of the device, make sure it exists.
934*4882a593Smuzhiyun */
935*4882a593Smuzhiyun error = xfs_buf_read_uncached(mp->m_rtdev_targp,
936*4882a593Smuzhiyun XFS_FSB_TO_BB(mp, nrblocks - 1),
937*4882a593Smuzhiyun XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
938*4882a593Smuzhiyun if (error)
939*4882a593Smuzhiyun return error;
940*4882a593Smuzhiyun xfs_buf_relse(bp);
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun /*
943*4882a593Smuzhiyun * Calculate new parameters. These are the final values to be reached.
944*4882a593Smuzhiyun */
945*4882a593Smuzhiyun nrextents = nrblocks;
946*4882a593Smuzhiyun do_div(nrextents, in->extsize);
947*4882a593Smuzhiyun nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
948*4882a593Smuzhiyun nrextslog = xfs_highbit32(nrextents);
949*4882a593Smuzhiyun nrsumlevels = nrextslog + 1;
950*4882a593Smuzhiyun nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
951*4882a593Smuzhiyun nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
952*4882a593Smuzhiyun nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
953*4882a593Smuzhiyun /*
954*4882a593Smuzhiyun * New summary size can't be more than half the size of
955*4882a593Smuzhiyun * the log. This prevents us from getting a log overflow,
956*4882a593Smuzhiyun * since we'll log basically the whole summary file at once.
957*4882a593Smuzhiyun */
958*4882a593Smuzhiyun if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
959*4882a593Smuzhiyun return -EINVAL;
960*4882a593Smuzhiyun /*
961*4882a593Smuzhiyun * Get the old block counts for bitmap and summary inodes.
962*4882a593Smuzhiyun * These can't change since other growfs callers are locked out.
963*4882a593Smuzhiyun */
964*4882a593Smuzhiyun rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
965*4882a593Smuzhiyun rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
966*4882a593Smuzhiyun /*
967*4882a593Smuzhiyun * Allocate space to the bitmap and summary files, as necessary.
968*4882a593Smuzhiyun */
969*4882a593Smuzhiyun error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
970*4882a593Smuzhiyun if (error)
971*4882a593Smuzhiyun return error;
972*4882a593Smuzhiyun error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
973*4882a593Smuzhiyun if (error)
974*4882a593Smuzhiyun return error;
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun rsum_cache = mp->m_rsum_cache;
977*4882a593Smuzhiyun if (nrbmblocks != sbp->sb_rbmblocks)
978*4882a593Smuzhiyun xfs_alloc_rsum_cache(mp, nrbmblocks);
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun /*
981*4882a593Smuzhiyun * Allocate a new (fake) mount/sb.
982*4882a593Smuzhiyun */
983*4882a593Smuzhiyun nmp = kmem_alloc(sizeof(*nmp), 0);
984*4882a593Smuzhiyun /*
985*4882a593Smuzhiyun * Loop over the bitmap blocks.
986*4882a593Smuzhiyun * We will do everything one bitmap block at a time.
987*4882a593Smuzhiyun * Skip the current block if it is exactly full.
988*4882a593Smuzhiyun * This also deals with the case where there were no rtextents before.
989*4882a593Smuzhiyun */
990*4882a593Smuzhiyun for (bmbno = sbp->sb_rbmblocks -
991*4882a593Smuzhiyun ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
992*4882a593Smuzhiyun bmbno < nrbmblocks;
993*4882a593Smuzhiyun bmbno++) {
994*4882a593Smuzhiyun xfs_trans_t *tp;
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun *nmp = *mp;
997*4882a593Smuzhiyun nsbp = &nmp->m_sb;
998*4882a593Smuzhiyun /*
999*4882a593Smuzhiyun * Calculate new sb and mount fields for this round.
1000*4882a593Smuzhiyun */
1001*4882a593Smuzhiyun nsbp->sb_rextsize = in->extsize;
1002*4882a593Smuzhiyun nsbp->sb_rbmblocks = bmbno + 1;
1003*4882a593Smuzhiyun nsbp->sb_rblocks =
1004*4882a593Smuzhiyun XFS_RTMIN(nrblocks,
1005*4882a593Smuzhiyun nsbp->sb_rbmblocks * NBBY *
1006*4882a593Smuzhiyun nsbp->sb_blocksize * nsbp->sb_rextsize);
1007*4882a593Smuzhiyun nsbp->sb_rextents = nsbp->sb_rblocks;
1008*4882a593Smuzhiyun do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1009*4882a593Smuzhiyun ASSERT(nsbp->sb_rextents != 0);
1010*4882a593Smuzhiyun nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1011*4882a593Smuzhiyun nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1012*4882a593Smuzhiyun nrsumsize =
1013*4882a593Smuzhiyun (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1014*4882a593Smuzhiyun nsbp->sb_rbmblocks;
1015*4882a593Smuzhiyun nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1016*4882a593Smuzhiyun nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1017*4882a593Smuzhiyun /*
1018*4882a593Smuzhiyun * Start a transaction, get the log reservation.
1019*4882a593Smuzhiyun */
1020*4882a593Smuzhiyun error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
1021*4882a593Smuzhiyun &tp);
1022*4882a593Smuzhiyun if (error)
1023*4882a593Smuzhiyun break;
1024*4882a593Smuzhiyun /*
1025*4882a593Smuzhiyun * Lock out other callers by grabbing the bitmap inode lock.
1026*4882a593Smuzhiyun */
1027*4882a593Smuzhiyun xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
1028*4882a593Smuzhiyun xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
1029*4882a593Smuzhiyun /*
1030*4882a593Smuzhiyun * Update the bitmap inode's size ondisk and incore. We need
1031*4882a593Smuzhiyun * to update the incore size so that inode inactivation won't
1032*4882a593Smuzhiyun * punch what it thinks are "posteof" blocks.
1033*4882a593Smuzhiyun */
1034*4882a593Smuzhiyun mp->m_rbmip->i_d.di_size =
1035*4882a593Smuzhiyun nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1036*4882a593Smuzhiyun i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_d.di_size);
1037*4882a593Smuzhiyun xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1038*4882a593Smuzhiyun /*
1039*4882a593Smuzhiyun * Get the summary inode into the transaction.
1040*4882a593Smuzhiyun */
1041*4882a593Smuzhiyun xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
1042*4882a593Smuzhiyun xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1043*4882a593Smuzhiyun /*
1044*4882a593Smuzhiyun * Update the summary inode's size. We need to update the
1045*4882a593Smuzhiyun * incore size so that inode inactivation won't punch what it
1046*4882a593Smuzhiyun * thinks are "posteof" blocks.
1047*4882a593Smuzhiyun */
1048*4882a593Smuzhiyun mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1049*4882a593Smuzhiyun i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_d.di_size);
1050*4882a593Smuzhiyun xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1051*4882a593Smuzhiyun /*
1052*4882a593Smuzhiyun * Copy summary data from old to new sizes.
1053*4882a593Smuzhiyun * Do this when the real size (not block-aligned) changes.
1054*4882a593Smuzhiyun */
1055*4882a593Smuzhiyun if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1056*4882a593Smuzhiyun mp->m_rsumlevels != nmp->m_rsumlevels) {
1057*4882a593Smuzhiyun error = xfs_rtcopy_summary(mp, nmp, tp);
1058*4882a593Smuzhiyun if (error)
1059*4882a593Smuzhiyun goto error_cancel;
1060*4882a593Smuzhiyun }
1061*4882a593Smuzhiyun /*
1062*4882a593Smuzhiyun * Update superblock fields.
1063*4882a593Smuzhiyun */
1064*4882a593Smuzhiyun if (nsbp->sb_rextsize != sbp->sb_rextsize)
1065*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1066*4882a593Smuzhiyun nsbp->sb_rextsize - sbp->sb_rextsize);
1067*4882a593Smuzhiyun if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1068*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1069*4882a593Smuzhiyun nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1070*4882a593Smuzhiyun if (nsbp->sb_rblocks != sbp->sb_rblocks)
1071*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1072*4882a593Smuzhiyun nsbp->sb_rblocks - sbp->sb_rblocks);
1073*4882a593Smuzhiyun if (nsbp->sb_rextents != sbp->sb_rextents)
1074*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1075*4882a593Smuzhiyun nsbp->sb_rextents - sbp->sb_rextents);
1076*4882a593Smuzhiyun if (nsbp->sb_rextslog != sbp->sb_rextslog)
1077*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1078*4882a593Smuzhiyun nsbp->sb_rextslog - sbp->sb_rextslog);
1079*4882a593Smuzhiyun /*
1080*4882a593Smuzhiyun * Free new extent.
1081*4882a593Smuzhiyun */
1082*4882a593Smuzhiyun bp = NULL;
1083*4882a593Smuzhiyun error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1084*4882a593Smuzhiyun nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
1085*4882a593Smuzhiyun if (error) {
1086*4882a593Smuzhiyun error_cancel:
1087*4882a593Smuzhiyun xfs_trans_cancel(tp);
1088*4882a593Smuzhiyun break;
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun /*
1091*4882a593Smuzhiyun * Mark more blocks free in the superblock.
1092*4882a593Smuzhiyun */
1093*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1094*4882a593Smuzhiyun nsbp->sb_rextents - sbp->sb_rextents);
1095*4882a593Smuzhiyun /*
1096*4882a593Smuzhiyun * Update mp values into the real mp structure.
1097*4882a593Smuzhiyun */
1098*4882a593Smuzhiyun mp->m_rsumlevels = nrsumlevels;
1099*4882a593Smuzhiyun mp->m_rsumsize = nrsumsize;
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun error = xfs_trans_commit(tp);
1102*4882a593Smuzhiyun if (error)
1103*4882a593Smuzhiyun break;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun if (error)
1106*4882a593Smuzhiyun goto out_free;
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /* Update secondary superblocks now the physical grow has completed */
1109*4882a593Smuzhiyun error = xfs_update_secondary_sbs(mp);
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun out_free:
1112*4882a593Smuzhiyun /*
1113*4882a593Smuzhiyun * Free the fake mp structure.
1114*4882a593Smuzhiyun */
1115*4882a593Smuzhiyun kmem_free(nmp);
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /*
1118*4882a593Smuzhiyun * If we had to allocate a new rsum_cache, we either need to free the
1119*4882a593Smuzhiyun * old one (if we succeeded) or free the new one and restore the old one
1120*4882a593Smuzhiyun * (if there was an error).
1121*4882a593Smuzhiyun */
1122*4882a593Smuzhiyun if (rsum_cache != mp->m_rsum_cache) {
1123*4882a593Smuzhiyun if (error) {
1124*4882a593Smuzhiyun kmem_free(mp->m_rsum_cache);
1125*4882a593Smuzhiyun mp->m_rsum_cache = rsum_cache;
1126*4882a593Smuzhiyun } else {
1127*4882a593Smuzhiyun kmem_free(rsum_cache);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun return error;
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun /*
1135*4882a593Smuzhiyun * Allocate an extent in the realtime subvolume, with the usual allocation
1136*4882a593Smuzhiyun * parameters. The length units are all in realtime extents, as is the
1137*4882a593Smuzhiyun * result block number.
1138*4882a593Smuzhiyun */
1139*4882a593Smuzhiyun int /* error */
xfs_rtallocate_extent(xfs_trans_t * tp,xfs_rtblock_t bno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,int wasdel,xfs_extlen_t prod,xfs_rtblock_t * rtblock)1140*4882a593Smuzhiyun xfs_rtallocate_extent(
1141*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
1142*4882a593Smuzhiyun xfs_rtblock_t bno, /* starting block number to allocate */
1143*4882a593Smuzhiyun xfs_extlen_t minlen, /* minimum length to allocate */
1144*4882a593Smuzhiyun xfs_extlen_t maxlen, /* maximum length to allocate */
1145*4882a593Smuzhiyun xfs_extlen_t *len, /* out: actual length allocated */
1146*4882a593Smuzhiyun int wasdel, /* was a delayed allocation extent */
1147*4882a593Smuzhiyun xfs_extlen_t prod, /* extent product factor */
1148*4882a593Smuzhiyun xfs_rtblock_t *rtblock) /* out: start block allocated */
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun xfs_mount_t *mp = tp->t_mountp;
1151*4882a593Smuzhiyun int error; /* error value */
1152*4882a593Smuzhiyun xfs_rtblock_t r; /* result allocated block */
1153*4882a593Smuzhiyun xfs_fsblock_t sb; /* summary file block number */
1154*4882a593Smuzhiyun xfs_buf_t *sumbp; /* summary file block buffer */
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1157*4882a593Smuzhiyun ASSERT(minlen > 0 && minlen <= maxlen);
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun /*
1160*4882a593Smuzhiyun * If prod is set then figure out what to do to minlen and maxlen.
1161*4882a593Smuzhiyun */
1162*4882a593Smuzhiyun if (prod > 1) {
1163*4882a593Smuzhiyun xfs_extlen_t i;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun if ((i = maxlen % prod))
1166*4882a593Smuzhiyun maxlen -= i;
1167*4882a593Smuzhiyun if ((i = minlen % prod))
1168*4882a593Smuzhiyun minlen += prod - i;
1169*4882a593Smuzhiyun if (maxlen < minlen) {
1170*4882a593Smuzhiyun *rtblock = NULLRTBLOCK;
1171*4882a593Smuzhiyun return 0;
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun retry:
1176*4882a593Smuzhiyun sumbp = NULL;
1177*4882a593Smuzhiyun if (bno == 0) {
1178*4882a593Smuzhiyun error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1179*4882a593Smuzhiyun &sumbp, &sb, prod, &r);
1180*4882a593Smuzhiyun } else {
1181*4882a593Smuzhiyun error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1182*4882a593Smuzhiyun len, &sumbp, &sb, prod, &r);
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun if (error)
1186*4882a593Smuzhiyun return error;
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun /*
1189*4882a593Smuzhiyun * If it worked, update the superblock.
1190*4882a593Smuzhiyun */
1191*4882a593Smuzhiyun if (r != NULLRTBLOCK) {
1192*4882a593Smuzhiyun long slen = (long)*len;
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun ASSERT(*len >= minlen && *len <= maxlen);
1195*4882a593Smuzhiyun if (wasdel)
1196*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1197*4882a593Smuzhiyun else
1198*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1199*4882a593Smuzhiyun } else if (prod > 1) {
1200*4882a593Smuzhiyun prod = 1;
1201*4882a593Smuzhiyun goto retry;
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun *rtblock = r;
1205*4882a593Smuzhiyun return 0;
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun /*
1209*4882a593Smuzhiyun * Initialize realtime fields in the mount structure.
1210*4882a593Smuzhiyun */
1211*4882a593Smuzhiyun int /* error */
xfs_rtmount_init(struct xfs_mount * mp)1212*4882a593Smuzhiyun xfs_rtmount_init(
1213*4882a593Smuzhiyun struct xfs_mount *mp) /* file system mount structure */
1214*4882a593Smuzhiyun {
1215*4882a593Smuzhiyun struct xfs_buf *bp; /* buffer for last block of subvolume */
1216*4882a593Smuzhiyun struct xfs_sb *sbp; /* filesystem superblock copy in mount */
1217*4882a593Smuzhiyun xfs_daddr_t d; /* address of last block of subvolume */
1218*4882a593Smuzhiyun int error;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun sbp = &mp->m_sb;
1221*4882a593Smuzhiyun if (sbp->sb_rblocks == 0)
1222*4882a593Smuzhiyun return 0;
1223*4882a593Smuzhiyun if (mp->m_rtdev_targp == NULL) {
1224*4882a593Smuzhiyun xfs_warn(mp,
1225*4882a593Smuzhiyun "Filesystem has a realtime volume, use rtdev=device option");
1226*4882a593Smuzhiyun return -ENODEV;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun mp->m_rsumlevels = sbp->sb_rextslog + 1;
1229*4882a593Smuzhiyun mp->m_rsumsize =
1230*4882a593Smuzhiyun (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1231*4882a593Smuzhiyun sbp->sb_rbmblocks;
1232*4882a593Smuzhiyun mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1233*4882a593Smuzhiyun mp->m_rbmip = mp->m_rsumip = NULL;
1234*4882a593Smuzhiyun /*
1235*4882a593Smuzhiyun * Check that the realtime section is an ok size.
1236*4882a593Smuzhiyun */
1237*4882a593Smuzhiyun d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1238*4882a593Smuzhiyun if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1239*4882a593Smuzhiyun xfs_warn(mp, "realtime mount -- %llu != %llu",
1240*4882a593Smuzhiyun (unsigned long long) XFS_BB_TO_FSB(mp, d),
1241*4882a593Smuzhiyun (unsigned long long) mp->m_sb.sb_rblocks);
1242*4882a593Smuzhiyun return -EFBIG;
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1245*4882a593Smuzhiyun d - XFS_FSB_TO_BB(mp, 1),
1246*4882a593Smuzhiyun XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1247*4882a593Smuzhiyun if (error) {
1248*4882a593Smuzhiyun xfs_warn(mp, "realtime device size check failed");
1249*4882a593Smuzhiyun return error;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun xfs_buf_relse(bp);
1252*4882a593Smuzhiyun return 0;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /*
1256*4882a593Smuzhiyun * Get the bitmap and summary inodes and the summary cache into the mount
1257*4882a593Smuzhiyun * structure at mount time.
1258*4882a593Smuzhiyun */
1259*4882a593Smuzhiyun int /* error */
xfs_rtmount_inodes(xfs_mount_t * mp)1260*4882a593Smuzhiyun xfs_rtmount_inodes(
1261*4882a593Smuzhiyun xfs_mount_t *mp) /* file system mount structure */
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun int error; /* error return value */
1264*4882a593Smuzhiyun xfs_sb_t *sbp;
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun sbp = &mp->m_sb;
1267*4882a593Smuzhiyun error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1268*4882a593Smuzhiyun if (error)
1269*4882a593Smuzhiyun return error;
1270*4882a593Smuzhiyun ASSERT(mp->m_rbmip != NULL);
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1273*4882a593Smuzhiyun if (error) {
1274*4882a593Smuzhiyun xfs_irele(mp->m_rbmip);
1275*4882a593Smuzhiyun return error;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun ASSERT(mp->m_rsumip != NULL);
1278*4882a593Smuzhiyun xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
1279*4882a593Smuzhiyun return 0;
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun void
xfs_rtunmount_inodes(struct xfs_mount * mp)1283*4882a593Smuzhiyun xfs_rtunmount_inodes(
1284*4882a593Smuzhiyun struct xfs_mount *mp)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun kmem_free(mp->m_rsum_cache);
1287*4882a593Smuzhiyun if (mp->m_rbmip)
1288*4882a593Smuzhiyun xfs_irele(mp->m_rbmip);
1289*4882a593Smuzhiyun if (mp->m_rsumip)
1290*4882a593Smuzhiyun xfs_irele(mp->m_rsumip);
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun /*
1294*4882a593Smuzhiyun * Pick an extent for allocation at the start of a new realtime file.
1295*4882a593Smuzhiyun * Use the sequence number stored in the atime field of the bitmap inode.
1296*4882a593Smuzhiyun * Translate this to a fraction of the rtextents, and return the product
1297*4882a593Smuzhiyun * of rtextents and the fraction.
1298*4882a593Smuzhiyun * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1299*4882a593Smuzhiyun */
1300*4882a593Smuzhiyun int /* error */
xfs_rtpick_extent(xfs_mount_t * mp,xfs_trans_t * tp,xfs_extlen_t len,xfs_rtblock_t * pick)1301*4882a593Smuzhiyun xfs_rtpick_extent(
1302*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount point */
1303*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
1304*4882a593Smuzhiyun xfs_extlen_t len, /* allocation length (rtextents) */
1305*4882a593Smuzhiyun xfs_rtblock_t *pick) /* result rt extent */
1306*4882a593Smuzhiyun {
1307*4882a593Smuzhiyun xfs_rtblock_t b; /* result block */
1308*4882a593Smuzhiyun int log2; /* log of sequence number */
1309*4882a593Smuzhiyun uint64_t resid; /* residual after log removed */
1310*4882a593Smuzhiyun uint64_t seq; /* sequence number of file creation */
1311*4882a593Smuzhiyun uint64_t *seqp; /* pointer to seqno in inode */
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
1316*4882a593Smuzhiyun if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
1317*4882a593Smuzhiyun mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
1318*4882a593Smuzhiyun *seqp = 0;
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun seq = *seqp;
1321*4882a593Smuzhiyun if ((log2 = xfs_highbit64(seq)) == -1)
1322*4882a593Smuzhiyun b = 0;
1323*4882a593Smuzhiyun else {
1324*4882a593Smuzhiyun resid = seq - (1ULL << log2);
1325*4882a593Smuzhiyun b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1326*4882a593Smuzhiyun (log2 + 1);
1327*4882a593Smuzhiyun if (b >= mp->m_sb.sb_rextents)
1328*4882a593Smuzhiyun div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
1329*4882a593Smuzhiyun if (b + len > mp->m_sb.sb_rextents)
1330*4882a593Smuzhiyun b = mp->m_sb.sb_rextents - len;
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun *seqp = seq + 1;
1333*4882a593Smuzhiyun xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1334*4882a593Smuzhiyun *pick = b;
1335*4882a593Smuzhiyun return 0;
1336*4882a593Smuzhiyun }
1337