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