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