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