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