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