Merge tag 'xfs-6.7-merge-2' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-2.6-microblaze.git] / fs / xfs / libxfs / xfs_rtbitmap.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_bmap.h"
16 #include "xfs_trans.h"
17 #include "xfs_rtalloc.h"
18 #include "xfs_error.h"
19 #include "xfs_rtbitmap.h"
20
21 /*
22  * Realtime allocator bitmap functions shared with userspace.
23  */
24
25 /*
26  * Real time buffers need verifiers to avoid runtime warnings during IO.
27  * We don't have anything to verify, however, so these are just dummy
28  * operations.
29  */
30 static void
31 xfs_rtbuf_verify_read(
32         struct xfs_buf  *bp)
33 {
34         return;
35 }
36
37 static void
38 xfs_rtbuf_verify_write(
39         struct xfs_buf  *bp)
40 {
41         return;
42 }
43
44 const struct xfs_buf_ops xfs_rtbuf_ops = {
45         .name = "rtbuf",
46         .verify_read = xfs_rtbuf_verify_read,
47         .verify_write = xfs_rtbuf_verify_write,
48 };
49
50 /* Release cached rt bitmap and summary buffers. */
51 void
52 xfs_rtbuf_cache_relse(
53         struct xfs_rtalloc_args *args)
54 {
55         if (args->rbmbp) {
56                 xfs_trans_brelse(args->tp, args->rbmbp);
57                 args->rbmbp = NULL;
58                 args->rbmoff = NULLFILEOFF;
59         }
60         if (args->sumbp) {
61                 xfs_trans_brelse(args->tp, args->sumbp);
62                 args->sumbp = NULL;
63                 args->sumoff = NULLFILEOFF;
64         }
65 }
66
67 /*
68  * Get a buffer for the bitmap or summary file block specified.
69  * The buffer is returned read and locked.
70  */
71 int
72 xfs_rtbuf_get(
73         struct xfs_rtalloc_args *args,
74         xfs_fileoff_t           block,  /* block number in bitmap or summary */
75         int                     issum)  /* is summary not bitmap */
76 {
77         struct xfs_mount        *mp = args->mp;
78         struct xfs_buf          **cbpp; /* cached block buffer */
79         xfs_fileoff_t           *coffp; /* cached block number */
80         struct xfs_buf          *bp;    /* block buffer, result */
81         struct xfs_inode        *ip;    /* bitmap or summary inode */
82         struct xfs_bmbt_irec    map;
83         enum xfs_blft           type;
84         int                     nmap = 1;
85         int                     error;
86
87         if (issum) {
88                 cbpp = &args->sumbp;
89                 coffp = &args->sumoff;
90                 ip = mp->m_rsumip;
91                 type = XFS_BLFT_RTSUMMARY_BUF;
92         } else {
93                 cbpp = &args->rbmbp;
94                 coffp = &args->rbmoff;
95                 ip = mp->m_rbmip;
96                 type = XFS_BLFT_RTBITMAP_BUF;
97         }
98
99         /*
100          * If we have a cached buffer, and the block number matches, use that.
101          */
102         if (*cbpp && *coffp == block)
103                 return 0;
104
105         /*
106          * Otherwise we have to have to get the buffer.  If there was an old
107          * one, get rid of it first.
108          */
109         if (*cbpp) {
110                 xfs_trans_brelse(args->tp, *cbpp);
111                 *cbpp = NULL;
112         }
113
114         error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
115         if (error)
116                 return error;
117
118         if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map)))
119                 return -EFSCORRUPTED;
120
121         ASSERT(map.br_startblock != NULLFSBLOCK);
122         error = xfs_trans_read_buf(mp, args->tp, mp->m_ddev_targp,
123                                    XFS_FSB_TO_DADDR(mp, map.br_startblock),
124                                    mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
125         if (error)
126                 return error;
127
128         xfs_trans_buf_set_type(args->tp, bp, type);
129         *cbpp = bp;
130         *coffp = block;
131         return 0;
132 }
133
134 /*
135  * Searching backward from start to limit, find the first block whose
136  * allocated/free state is different from start's.
137  */
138 int
139 xfs_rtfind_back(
140         struct xfs_rtalloc_args *args,
141         xfs_rtxnum_t            start,  /* starting rtext to look at */
142         xfs_rtxnum_t            limit,  /* last rtext to look at */
143         xfs_rtxnum_t            *rtx)   /* out: start rtext found */
144 {
145         struct xfs_mount        *mp = args->mp;
146         int                     bit;    /* bit number in the word */
147         xfs_fileoff_t           block;  /* bitmap block number */
148         int                     error;  /* error value */
149         xfs_rtxnum_t            firstbit; /* first useful bit in the word */
150         xfs_rtxnum_t            i;      /* current bit number rel. to start */
151         xfs_rtxnum_t            len;    /* length of inspected area */
152         xfs_rtword_t            mask;   /* mask of relevant bits for value */
153         xfs_rtword_t            want;   /* mask for "good" values */
154         xfs_rtword_t            wdiff;  /* difference from wanted value */
155         xfs_rtword_t            incore;
156         unsigned int            word;   /* word number in the buffer */
157
158         /*
159          * Compute and read in starting bitmap block for starting block.
160          */
161         block = xfs_rtx_to_rbmblock(mp, start);
162         error = xfs_rtbitmap_read_buf(args, block);
163         if (error)
164                 return error;
165
166         /*
167          * Get the first word's index & point to it.
168          */
169         word = xfs_rtx_to_rbmword(mp, start);
170         bit = (int)(start & (XFS_NBWORD - 1));
171         len = start - limit + 1;
172         /*
173          * Compute match value, based on the bit at start: if 1 (free)
174          * then all-ones, else all-zeroes.
175          */
176         incore = xfs_rtbitmap_getword(args, word);
177         want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
178         /*
179          * If the starting position is not word-aligned, deal with the
180          * partial word.
181          */
182         if (bit < XFS_NBWORD - 1) {
183                 /*
184                  * Calculate first (leftmost) bit number to look at,
185                  * and mask for all the relevant bits in this word.
186                  */
187                 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
188                 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
189                         firstbit;
190                 /*
191                  * Calculate the difference between the value there
192                  * and what we're looking for.
193                  */
194                 if ((wdiff = (incore ^ want) & mask)) {
195                         /*
196                          * Different.  Mark where we are and return.
197                          */
198                         i = bit - XFS_RTHIBIT(wdiff);
199                         *rtx = start - i + 1;
200                         return 0;
201                 }
202                 i = bit - firstbit + 1;
203                 /*
204                  * Go on to previous block if that's where the previous word is
205                  * and we need the previous word.
206                  */
207                 if (--word == -1 && i < len) {
208                         /*
209                          * If done with this block, get the previous one.
210                          */
211                         error = xfs_rtbitmap_read_buf(args, --block);
212                         if (error)
213                                 return error;
214
215                         word = mp->m_blockwsize - 1;
216                 }
217         } else {
218                 /*
219                  * Starting on a word boundary, no partial word.
220                  */
221                 i = 0;
222         }
223         /*
224          * Loop over whole words in buffers.  When we use up one buffer
225          * we move on to the previous one.
226          */
227         while (len - i >= XFS_NBWORD) {
228                 /*
229                  * Compute difference between actual and desired value.
230                  */
231                 incore = xfs_rtbitmap_getword(args, word);
232                 if ((wdiff = incore ^ want)) {
233                         /*
234                          * Different, mark where we are and return.
235                          */
236                         i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
237                         *rtx = start - i + 1;
238                         return 0;
239                 }
240                 i += XFS_NBWORD;
241                 /*
242                  * Go on to previous block if that's where the previous word is
243                  * and we need the previous word.
244                  */
245                 if (--word == -1 && i < len) {
246                         /*
247                          * If done with this block, get the previous one.
248                          */
249                         error = xfs_rtbitmap_read_buf(args, --block);
250                         if (error)
251                                 return error;
252
253                         word = mp->m_blockwsize - 1;
254                 }
255         }
256         /*
257          * If not ending on a word boundary, deal with the last
258          * (partial) word.
259          */
260         if (len - i) {
261                 /*
262                  * Calculate first (leftmost) bit number to look at,
263                  * and mask for all the relevant bits in this word.
264                  */
265                 firstbit = XFS_NBWORD - (len - i);
266                 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
267                 /*
268                  * Compute difference between actual and desired value.
269                  */
270                 incore = xfs_rtbitmap_getword(args, word);
271                 if ((wdiff = (incore ^ want) & mask)) {
272                         /*
273                          * Different, mark where we are and return.
274                          */
275                         i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
276                         *rtx = start - i + 1;
277                         return 0;
278                 } else
279                         i = len;
280         }
281         /*
282          * No match, return that we scanned the whole area.
283          */
284         *rtx = start - i + 1;
285         return 0;
286 }
287
288 /*
289  * Searching forward from start to limit, find the first block whose
290  * allocated/free state is different from start's.
291  */
292 int
293 xfs_rtfind_forw(
294         struct xfs_rtalloc_args *args,
295         xfs_rtxnum_t            start,  /* starting rtext to look at */
296         xfs_rtxnum_t            limit,  /* last rtext to look at */
297         xfs_rtxnum_t            *rtx)   /* out: start rtext found */
298 {
299         struct xfs_mount        *mp = args->mp;
300         int                     bit;    /* bit number in the word */
301         xfs_fileoff_t           block;  /* bitmap block number */
302         int                     error;
303         xfs_rtxnum_t            i;      /* current bit number rel. to start */
304         xfs_rtxnum_t            lastbit;/* last useful bit in the word */
305         xfs_rtxnum_t            len;    /* length of inspected area */
306         xfs_rtword_t            mask;   /* mask of relevant bits for value */
307         xfs_rtword_t            want;   /* mask for "good" values */
308         xfs_rtword_t            wdiff;  /* difference from wanted value */
309         xfs_rtword_t            incore;
310         unsigned int            word;   /* word number in the buffer */
311
312         /*
313          * Compute and read in starting bitmap block for starting block.
314          */
315         block = xfs_rtx_to_rbmblock(mp, start);
316         error = xfs_rtbitmap_read_buf(args, block);
317         if (error)
318                 return error;
319
320         /*
321          * Get the first word's index & point to it.
322          */
323         word = xfs_rtx_to_rbmword(mp, start);
324         bit = (int)(start & (XFS_NBWORD - 1));
325         len = limit - start + 1;
326         /*
327          * Compute match value, based on the bit at start: if 1 (free)
328          * then all-ones, else all-zeroes.
329          */
330         incore = xfs_rtbitmap_getword(args, word);
331         want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
332         /*
333          * If the starting position is not word-aligned, deal with the
334          * partial word.
335          */
336         if (bit) {
337                 /*
338                  * Calculate last (rightmost) bit number to look at,
339                  * and mask for all the relevant bits in this word.
340                  */
341                 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
342                 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
343                 /*
344                  * Calculate the difference between the value there
345                  * and what we're looking for.
346                  */
347                 if ((wdiff = (incore ^ want) & mask)) {
348                         /*
349                          * Different.  Mark where we are and return.
350                          */
351                         i = XFS_RTLOBIT(wdiff) - bit;
352                         *rtx = start + i - 1;
353                         return 0;
354                 }
355                 i = lastbit - bit;
356                 /*
357                  * Go on to next block if that's where the next word is
358                  * and we need the next word.
359                  */
360                 if (++word == mp->m_blockwsize && i < len) {
361                         /*
362                          * If done with this block, get the previous one.
363                          */
364                         error = xfs_rtbitmap_read_buf(args, ++block);
365                         if (error)
366                                 return error;
367
368                         word = 0;
369                 }
370         } else {
371                 /*
372                  * Starting on a word boundary, no partial word.
373                  */
374                 i = 0;
375         }
376         /*
377          * Loop over whole words in buffers.  When we use up one buffer
378          * we move on to the next one.
379          */
380         while (len - i >= XFS_NBWORD) {
381                 /*
382                  * Compute difference between actual and desired value.
383                  */
384                 incore = xfs_rtbitmap_getword(args, word);
385                 if ((wdiff = incore ^ want)) {
386                         /*
387                          * Different, mark where we are and return.
388                          */
389                         i += XFS_RTLOBIT(wdiff);
390                         *rtx = start + i - 1;
391                         return 0;
392                 }
393                 i += XFS_NBWORD;
394                 /*
395                  * Go on to next block if that's where the next word is
396                  * and we need the next word.
397                  */
398                 if (++word == mp->m_blockwsize && i < len) {
399                         /*
400                          * If done with this block, get the next one.
401                          */
402                         error = xfs_rtbitmap_read_buf(args, ++block);
403                         if (error)
404                                 return error;
405
406                         word = 0;
407                 }
408         }
409         /*
410          * If not ending on a word boundary, deal with the last
411          * (partial) word.
412          */
413         if ((lastbit = len - i)) {
414                 /*
415                  * Calculate mask for all the relevant bits in this word.
416                  */
417                 mask = ((xfs_rtword_t)1 << lastbit) - 1;
418                 /*
419                  * Compute difference between actual and desired value.
420                  */
421                 incore = xfs_rtbitmap_getword(args, word);
422                 if ((wdiff = (incore ^ want) & mask)) {
423                         /*
424                          * Different, mark where we are and return.
425                          */
426                         i += XFS_RTLOBIT(wdiff);
427                         *rtx = start + i - 1;
428                         return 0;
429                 } else
430                         i = len;
431         }
432         /*
433          * No match, return that we scanned the whole area.
434          */
435         *rtx = start + i - 1;
436         return 0;
437 }
438
439 /* Log rtsummary counter at @infoword. */
440 static inline void
441 xfs_trans_log_rtsummary(
442         struct xfs_rtalloc_args *args,
443         unsigned int            infoword)
444 {
445         struct xfs_buf          *bp = args->sumbp;
446         size_t                  first, last;
447
448         first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr;
449         last = first + sizeof(xfs_suminfo_t) - 1;
450
451         xfs_trans_log_buf(args->tp, bp, first, last);
452 }
453
454 /*
455  * Read and/or modify the summary information for a given extent size,
456  * bitmap block combination.
457  * Keeps track of a current summary block, so we don't keep reading
458  * it from the buffer cache.
459  *
460  * Summary information is returned in *sum if specified.
461  * If no delta is specified, returns summary only.
462  */
463 int
464 xfs_rtmodify_summary_int(
465         struct xfs_rtalloc_args *args,
466         int                     log,    /* log2 of extent size */
467         xfs_fileoff_t           bbno,   /* bitmap block number */
468         int                     delta,  /* change to make to summary info */
469         xfs_suminfo_t           *sum)   /* out: summary info for this block */
470 {
471         struct xfs_mount        *mp = args->mp;
472         int                     error;
473         xfs_fileoff_t           sb;     /* summary fsblock */
474         xfs_rtsumoff_t          so;     /* index into the summary file */
475         unsigned int            infoword;
476
477         /*
478          * Compute entry number in the summary file.
479          */
480         so = xfs_rtsumoffs(mp, log, bbno);
481         /*
482          * Compute the block number in the summary file.
483          */
484         sb = xfs_rtsumoffs_to_block(mp, so);
485
486         error = xfs_rtsummary_read_buf(args, sb);
487         if (error)
488                 return error;
489
490         /*
491          * Point to the summary information, modify/log it, and/or copy it out.
492          */
493         infoword = xfs_rtsumoffs_to_infoword(mp, so);
494         if (delta) {
495                 xfs_suminfo_t   val = xfs_suminfo_add(args, infoword, delta);
496
497                 if (mp->m_rsum_cache) {
498                         if (val == 0 && log + 1 == mp->m_rsum_cache[bbno])
499                                 mp->m_rsum_cache[bbno] = log;
500                         if (val != 0 && log >= mp->m_rsum_cache[bbno])
501                                 mp->m_rsum_cache[bbno] = log + 1;
502                 }
503                 xfs_trans_log_rtsummary(args, infoword);
504                 if (sum)
505                         *sum = val;
506         } else if (sum) {
507                 *sum = xfs_suminfo_get(args, infoword);
508         }
509         return 0;
510 }
511
512 int
513 xfs_rtmodify_summary(
514         struct xfs_rtalloc_args *args,
515         int                     log,    /* log2 of extent size */
516         xfs_fileoff_t           bbno,   /* bitmap block number */
517         int                     delta)  /* in/out: summary block number */
518 {
519         return xfs_rtmodify_summary_int(args, log, bbno, delta, NULL);
520 }
521
522 /* Log rtbitmap block from the word @from to the byte before @next. */
523 static inline void
524 xfs_trans_log_rtbitmap(
525         struct xfs_rtalloc_args *args,
526         unsigned int            from,
527         unsigned int            next)
528 {
529         struct xfs_buf          *bp = args->rbmbp;
530         size_t                  first, last;
531
532         first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr;
533         last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr;
534
535         xfs_trans_log_buf(args->tp, bp, first, last);
536 }
537
538 /*
539  * Set the given range of bitmap bits to the given value.
540  * Do whatever I/O and logging is required.
541  */
542 int
543 xfs_rtmodify_range(
544         struct xfs_rtalloc_args *args,
545         xfs_rtxnum_t            start,  /* starting rtext to modify */
546         xfs_rtxlen_t            len,    /* length of extent to modify */
547         int                     val)    /* 1 for free, 0 for allocated */
548 {
549         struct xfs_mount        *mp = args->mp;
550         int                     bit;    /* bit number in the word */
551         xfs_fileoff_t           block;  /* bitmap block number */
552         int                     error;
553         int                     i;      /* current bit number rel. to start */
554         int                     lastbit; /* last useful bit in word */
555         xfs_rtword_t            mask;    /* mask of relevant bits for value */
556         xfs_rtword_t            incore;
557         unsigned int            firstword; /* first word used in the buffer */
558         unsigned int            word;   /* word number in the buffer */
559
560         /*
561          * Compute starting bitmap block number.
562          */
563         block = xfs_rtx_to_rbmblock(mp, start);
564         /*
565          * Read the bitmap block, and point to its data.
566          */
567         error = xfs_rtbitmap_read_buf(args, block);
568         if (error)
569                 return error;
570
571         /*
572          * Compute the starting word's address, and starting bit.
573          */
574         firstword = word = xfs_rtx_to_rbmword(mp, start);
575         bit = (int)(start & (XFS_NBWORD - 1));
576         /*
577          * 0 (allocated) => all zeroes; 1 (free) => all ones.
578          */
579         val = -val;
580         /*
581          * If not starting on a word boundary, deal with the first
582          * (partial) word.
583          */
584         if (bit) {
585                 /*
586                  * Compute first bit not changed and mask of relevant bits.
587                  */
588                 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
589                 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
590                 /*
591                  * Set/clear the active bits.
592                  */
593                 incore = xfs_rtbitmap_getword(args, word);
594                 if (val)
595                         incore |= mask;
596                 else
597                         incore &= ~mask;
598                 xfs_rtbitmap_setword(args, word, incore);
599                 i = lastbit - bit;
600                 /*
601                  * Go on to the next block if that's where the next word is
602                  * and we need the next word.
603                  */
604                 if (++word == mp->m_blockwsize && i < len) {
605                         /*
606                          * Log the changed part of this block.
607                          * Get the next one.
608                          */
609                         xfs_trans_log_rtbitmap(args, firstword, word);
610                         error = xfs_rtbitmap_read_buf(args, ++block);
611                         if (error)
612                                 return error;
613
614                         firstword = word = 0;
615                 }
616         } else {
617                 /*
618                  * Starting on a word boundary, no partial word.
619                  */
620                 i = 0;
621         }
622         /*
623          * Loop over whole words in buffers.  When we use up one buffer
624          * we move on to the next one.
625          */
626         while (len - i >= XFS_NBWORD) {
627                 /*
628                  * Set the word value correctly.
629                  */
630                 xfs_rtbitmap_setword(args, word, val);
631                 i += XFS_NBWORD;
632                 /*
633                  * Go on to the next block if that's where the next word is
634                  * and we need the next word.
635                  */
636                 if (++word == mp->m_blockwsize && i < len) {
637                         /*
638                          * Log the changed part of this block.
639                          * Get the next one.
640                          */
641                         xfs_trans_log_rtbitmap(args, firstword, word);
642                         error = xfs_rtbitmap_read_buf(args, ++block);
643                         if (error)
644                                 return error;
645
646                         firstword = word = 0;
647                 }
648         }
649         /*
650          * If not ending on a word boundary, deal with the last
651          * (partial) word.
652          */
653         if ((lastbit = len - i)) {
654                 /*
655                  * Compute a mask of relevant bits.
656                  */
657                 mask = ((xfs_rtword_t)1 << lastbit) - 1;
658                 /*
659                  * Set/clear the active bits.
660                  */
661                 incore = xfs_rtbitmap_getword(args, word);
662                 if (val)
663                         incore |= mask;
664                 else
665                         incore &= ~mask;
666                 xfs_rtbitmap_setword(args, word, incore);
667                 word++;
668         }
669         /*
670          * Log any remaining changed bytes.
671          */
672         if (word > firstword)
673                 xfs_trans_log_rtbitmap(args, firstword, word);
674         return 0;
675 }
676
677 /*
678  * Mark an extent specified by start and len freed.
679  * Updates all the summary information as well as the bitmap.
680  */
681 int
682 xfs_rtfree_range(
683         struct xfs_rtalloc_args *args,
684         xfs_rtxnum_t            start,  /* starting rtext to free */
685         xfs_rtxlen_t            len)    /* in/out: summary block number */
686 {
687         struct xfs_mount        *mp = args->mp;
688         xfs_rtxnum_t            end;    /* end of the freed extent */
689         int                     error;  /* error value */
690         xfs_rtxnum_t            postblock; /* first rtext freed > end */
691         xfs_rtxnum_t            preblock;  /* first rtext freed < start */
692
693         end = start + len - 1;
694         /*
695          * Modify the bitmap to mark this extent freed.
696          */
697         error = xfs_rtmodify_range(args, start, len, 1);
698         if (error) {
699                 return error;
700         }
701         /*
702          * Assume we're freeing out of the middle of an allocated extent.
703          * We need to find the beginning and end of the extent so we can
704          * properly update the summary.
705          */
706         error = xfs_rtfind_back(args, start, 0, &preblock);
707         if (error) {
708                 return error;
709         }
710         /*
711          * Find the next allocated block (end of allocated extent).
712          */
713         error = xfs_rtfind_forw(args, end, mp->m_sb.sb_rextents - 1,
714                         &postblock);
715         if (error)
716                 return error;
717         /*
718          * If there are blocks not being freed at the front of the
719          * old extent, add summary data for them to be allocated.
720          */
721         if (preblock < start) {
722                 error = xfs_rtmodify_summary(args,
723                                 XFS_RTBLOCKLOG(start - preblock),
724                                 xfs_rtx_to_rbmblock(mp, preblock), -1);
725                 if (error) {
726                         return error;
727                 }
728         }
729         /*
730          * If there are blocks not being freed at the end of the
731          * old extent, add summary data for them to be allocated.
732          */
733         if (postblock > end) {
734                 error = xfs_rtmodify_summary(args,
735                                 XFS_RTBLOCKLOG(postblock - end),
736                                 xfs_rtx_to_rbmblock(mp, end + 1), -1);
737                 if (error) {
738                         return error;
739                 }
740         }
741         /*
742          * Increment the summary information corresponding to the entire
743          * (new) free extent.
744          */
745         return xfs_rtmodify_summary(args,
746                         XFS_RTBLOCKLOG(postblock + 1 - preblock),
747                         xfs_rtx_to_rbmblock(mp, preblock), 1);
748 }
749
750 /*
751  * Check that the given range is either all allocated (val = 0) or
752  * all free (val = 1).
753  */
754 int
755 xfs_rtcheck_range(
756         struct xfs_rtalloc_args *args,
757         xfs_rtxnum_t            start,  /* starting rtext number of extent */
758         xfs_rtxlen_t            len,    /* length of extent */
759         int                     val,    /* 1 for free, 0 for allocated */
760         xfs_rtxnum_t            *new,   /* out: first rtext not matching */
761         int                     *stat)  /* out: 1 for matches, 0 for not */
762 {
763         struct xfs_mount        *mp = args->mp;
764         int                     bit;    /* bit number in the word */
765         xfs_fileoff_t           block;  /* bitmap block number */
766         int                     error;
767         xfs_rtxnum_t            i;      /* current bit number rel. to start */
768         xfs_rtxnum_t            lastbit; /* last useful bit in word */
769         xfs_rtword_t            mask;   /* mask of relevant bits for value */
770         xfs_rtword_t            wdiff;  /* difference from wanted value */
771         xfs_rtword_t            incore;
772         unsigned int            word;   /* word number in the buffer */
773
774         /*
775          * Compute starting bitmap block number
776          */
777         block = xfs_rtx_to_rbmblock(mp, start);
778         /*
779          * Read the bitmap block.
780          */
781         error = xfs_rtbitmap_read_buf(args, block);
782         if (error)
783                 return error;
784
785         /*
786          * Compute the starting word's address, and starting bit.
787          */
788         word = xfs_rtx_to_rbmword(mp, start);
789         bit = (int)(start & (XFS_NBWORD - 1));
790         /*
791          * 0 (allocated) => all zero's; 1 (free) => all one's.
792          */
793         val = -val;
794         /*
795          * If not starting on a word boundary, deal with the first
796          * (partial) word.
797          */
798         if (bit) {
799                 /*
800                  * Compute first bit not examined.
801                  */
802                 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
803                 /*
804                  * Mask of relevant bits.
805                  */
806                 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
807                 /*
808                  * Compute difference between actual and desired value.
809                  */
810                 incore = xfs_rtbitmap_getword(args, word);
811                 if ((wdiff = (incore ^ val) & mask)) {
812                         /*
813                          * Different, compute first wrong bit and return.
814                          */
815                         i = XFS_RTLOBIT(wdiff) - bit;
816                         *new = start + i;
817                         *stat = 0;
818                         return 0;
819                 }
820                 i = lastbit - bit;
821                 /*
822                  * Go on to next block if that's where the next word is
823                  * and we need the next word.
824                  */
825                 if (++word == mp->m_blockwsize && i < len) {
826                         /*
827                          * If done with this block, get the next one.
828                          */
829                         error = xfs_rtbitmap_read_buf(args, ++block);
830                         if (error)
831                                 return error;
832
833                         word = 0;
834                 }
835         } else {
836                 /*
837                  * Starting on a word boundary, no partial word.
838                  */
839                 i = 0;
840         }
841         /*
842          * Loop over whole words in buffers.  When we use up one buffer
843          * we move on to the next one.
844          */
845         while (len - i >= XFS_NBWORD) {
846                 /*
847                  * Compute difference between actual and desired value.
848                  */
849                 incore = xfs_rtbitmap_getword(args, word);
850                 if ((wdiff = incore ^ val)) {
851                         /*
852                          * Different, compute first wrong bit and return.
853                          */
854                         i += XFS_RTLOBIT(wdiff);
855                         *new = start + i;
856                         *stat = 0;
857                         return 0;
858                 }
859                 i += XFS_NBWORD;
860                 /*
861                  * Go on to next block if that's where the next word is
862                  * and we need the next word.
863                  */
864                 if (++word == mp->m_blockwsize && i < len) {
865                         /*
866                          * If done with this block, get the next one.
867                          */
868                         error = xfs_rtbitmap_read_buf(args, ++block);
869                         if (error)
870                                 return error;
871
872                         word = 0;
873                 }
874         }
875         /*
876          * If not ending on a word boundary, deal with the last
877          * (partial) word.
878          */
879         if ((lastbit = len - i)) {
880                 /*
881                  * Mask of relevant bits.
882                  */
883                 mask = ((xfs_rtword_t)1 << lastbit) - 1;
884                 /*
885                  * Compute difference between actual and desired value.
886                  */
887                 incore = xfs_rtbitmap_getword(args, word);
888                 if ((wdiff = (incore ^ val) & mask)) {
889                         /*
890                          * Different, compute first wrong bit and return.
891                          */
892                         i += XFS_RTLOBIT(wdiff);
893                         *new = start + i;
894                         *stat = 0;
895                         return 0;
896                 } else
897                         i = len;
898         }
899         /*
900          * Successful, return.
901          */
902         *new = start + i;
903         *stat = 1;
904         return 0;
905 }
906
907 #ifdef DEBUG
908 /*
909  * Check that the given extent (block range) is allocated already.
910  */
911 STATIC int
912 xfs_rtcheck_alloc_range(
913         struct xfs_rtalloc_args *args,
914         xfs_rtxnum_t            start,  /* starting rtext number of extent */
915         xfs_rtxlen_t            len)    /* length of extent */
916 {
917         xfs_rtxnum_t            new;    /* dummy for xfs_rtcheck_range */
918         int                     stat;
919         int                     error;
920
921         error = xfs_rtcheck_range(args, start, len, 0, &new, &stat);
922         if (error)
923                 return error;
924         ASSERT(stat);
925         return 0;
926 }
927 #else
928 #define xfs_rtcheck_alloc_range(a,b,l)  (0)
929 #endif
930 /*
931  * Free an extent in the realtime subvolume.  Length is expressed in
932  * realtime extents, as is the block number.
933  */
934 int
935 xfs_rtfree_extent(
936         struct xfs_trans        *tp,    /* transaction pointer */
937         xfs_rtxnum_t            start,  /* starting rtext number to free */
938         xfs_rtxlen_t            len)    /* length of extent freed */
939 {
940         struct xfs_mount        *mp = tp->t_mountp;
941         struct xfs_rtalloc_args args = {
942                 .mp             = mp,
943                 .tp             = tp,
944         };
945         int                     error;
946         struct timespec64       atime;
947
948         ASSERT(mp->m_rbmip->i_itemp != NULL);
949         ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
950
951         error = xfs_rtcheck_alloc_range(&args, start, len);
952         if (error)
953                 return error;
954
955         /*
956          * Free the range of realtime blocks.
957          */
958         error = xfs_rtfree_range(&args, start, len);
959         if (error)
960                 goto out;
961
962         /*
963          * Mark more blocks free in the superblock.
964          */
965         xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
966         /*
967          * If we've now freed all the blocks, reset the file sequence
968          * number to 0.
969          */
970         if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
971             mp->m_sb.sb_rextents) {
972                 if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM))
973                         mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
974
975                 atime = inode_get_atime(VFS_I(mp->m_rbmip));
976                 atime.tv_sec = 0;
977                 inode_set_atime_to_ts(VFS_I(mp->m_rbmip), atime);
978                 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
979         }
980         error = 0;
981 out:
982         xfs_rtbuf_cache_relse(&args);
983         return error;
984 }
985
986 /*
987  * Free some blocks in the realtime subvolume.  rtbno and rtlen are in units of
988  * rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen
989  * cannot exceed XFS_MAX_BMBT_EXTLEN.
990  */
991 int
992 xfs_rtfree_blocks(
993         struct xfs_trans        *tp,
994         xfs_fsblock_t           rtbno,
995         xfs_filblks_t           rtlen)
996 {
997         struct xfs_mount        *mp = tp->t_mountp;
998         xfs_rtxnum_t            start;
999         xfs_filblks_t           len;
1000         xfs_extlen_t            mod;
1001
1002         ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
1003
1004         len = xfs_rtb_to_rtxrem(mp, rtlen, &mod);
1005         if (mod) {
1006                 ASSERT(mod == 0);
1007                 return -EIO;
1008         }
1009
1010         start = xfs_rtb_to_rtxrem(mp, rtbno, &mod);
1011         if (mod) {
1012                 ASSERT(mod == 0);
1013                 return -EIO;
1014         }
1015
1016         return xfs_rtfree_extent(tp, start, len);
1017 }
1018
1019 /* Find all the free records within a given range. */
1020 int
1021 xfs_rtalloc_query_range(
1022         struct xfs_mount                *mp,
1023         struct xfs_trans                *tp,
1024         const struct xfs_rtalloc_rec    *low_rec,
1025         const struct xfs_rtalloc_rec    *high_rec,
1026         xfs_rtalloc_query_range_fn      fn,
1027         void                            *priv)
1028 {
1029         struct xfs_rtalloc_args         args = {
1030                 .mp                     = mp,
1031                 .tp                     = tp,
1032         };
1033         struct xfs_rtalloc_rec          rec;
1034         xfs_rtxnum_t                    rtstart;
1035         xfs_rtxnum_t                    rtend;
1036         xfs_rtxnum_t                    high_key;
1037         int                             is_free;
1038         int                             error = 0;
1039
1040         if (low_rec->ar_startext > high_rec->ar_startext)
1041                 return -EINVAL;
1042         if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
1043             low_rec->ar_startext == high_rec->ar_startext)
1044                 return 0;
1045
1046         high_key = min(high_rec->ar_startext, mp->m_sb.sb_rextents - 1);
1047
1048         /* Iterate the bitmap, looking for discrepancies. */
1049         rtstart = low_rec->ar_startext;
1050         while (rtstart <= high_key) {
1051                 /* Is the first block free? */
1052                 error = xfs_rtcheck_range(&args, rtstart, 1, 1, &rtend,
1053                                 &is_free);
1054                 if (error)
1055                         break;
1056
1057                 /* How long does the extent go for? */
1058                 error = xfs_rtfind_forw(&args, rtstart, high_key, &rtend);
1059                 if (error)
1060                         break;
1061
1062                 if (is_free) {
1063                         rec.ar_startext = rtstart;
1064                         rec.ar_extcount = rtend - rtstart + 1;
1065
1066                         error = fn(mp, tp, &rec, priv);
1067                         if (error)
1068                                 break;
1069                 }
1070
1071                 rtstart = rtend + 1;
1072         }
1073
1074         xfs_rtbuf_cache_relse(&args);
1075         return error;
1076 }
1077
1078 /* Find all the free records. */
1079 int
1080 xfs_rtalloc_query_all(
1081         struct xfs_mount                *mp,
1082         struct xfs_trans                *tp,
1083         xfs_rtalloc_query_range_fn      fn,
1084         void                            *priv)
1085 {
1086         struct xfs_rtalloc_rec          keys[2];
1087
1088         keys[0].ar_startext = 0;
1089         keys[1].ar_startext = mp->m_sb.sb_rextents - 1;
1090         keys[0].ar_extcount = keys[1].ar_extcount = 0;
1091
1092         return xfs_rtalloc_query_range(mp, tp, &keys[0], &keys[1], fn, priv);
1093 }
1094
1095 /* Is the given extent all free? */
1096 int
1097 xfs_rtalloc_extent_is_free(
1098         struct xfs_mount                *mp,
1099         struct xfs_trans                *tp,
1100         xfs_rtxnum_t                    start,
1101         xfs_rtxlen_t                    len,
1102         bool                            *is_free)
1103 {
1104         struct xfs_rtalloc_args         args = {
1105                 .mp                     = mp,
1106                 .tp                     = tp,
1107         };
1108         xfs_rtxnum_t                    end;
1109         int                             matches;
1110         int                             error;
1111
1112         error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches);
1113         xfs_rtbuf_cache_relse(&args);
1114         if (error)
1115                 return error;
1116
1117         *is_free = matches;
1118         return 0;
1119 }
1120
1121 /*
1122  * Compute the number of rtbitmap blocks needed to track the given number of rt
1123  * extents.
1124  */
1125 xfs_filblks_t
1126 xfs_rtbitmap_blockcount(
1127         struct xfs_mount        *mp,
1128         xfs_rtbxlen_t           rtextents)
1129 {
1130         return howmany_64(rtextents, NBBY * mp->m_sb.sb_blocksize);
1131 }
1132
1133 /*
1134  * Compute the number of rtbitmap words needed to populate every block of a
1135  * bitmap that is large enough to track the given number of rt extents.
1136  */
1137 unsigned long long
1138 xfs_rtbitmap_wordcount(
1139         struct xfs_mount        *mp,
1140         xfs_rtbxlen_t           rtextents)
1141 {
1142         xfs_filblks_t           blocks;
1143
1144         blocks = xfs_rtbitmap_blockcount(mp, rtextents);
1145         return XFS_FSB_TO_B(mp, blocks) >> XFS_WORDLOG;
1146 }
1147
1148 /* Compute the number of rtsummary blocks needed to track the given rt space. */
1149 xfs_filblks_t
1150 xfs_rtsummary_blockcount(
1151         struct xfs_mount        *mp,
1152         unsigned int            rsumlevels,
1153         xfs_extlen_t            rbmblocks)
1154 {
1155         unsigned long long      rsumwords;
1156
1157         rsumwords = (unsigned long long)rsumlevels * rbmblocks;
1158         return XFS_B_TO_FSB(mp, rsumwords << XFS_WORDLOG);
1159 }
1160
1161 /*
1162  * Compute the number of rtsummary info words needed to populate every block of
1163  * a summary file that is large enough to track the given rt space.
1164  */
1165 unsigned long long
1166 xfs_rtsummary_wordcount(
1167         struct xfs_mount        *mp,
1168         unsigned int            rsumlevels,
1169         xfs_extlen_t            rbmblocks)
1170 {
1171         xfs_filblks_t           blocks;
1172
1173         blocks = xfs_rtsummary_blockcount(mp, rsumlevels, rbmblocks);
1174         return XFS_FSB_TO_B(mp, blocks) >> XFS_WORDLOG;
1175 }