blob: 50e3b9302722faa6276fd42be6b111788cefbb65 [file] [log] [blame]
Dave Chinnerc963c612013-10-15 09:17:56 +11001/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_shared.h"
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
24#include "xfs_bit.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
28#include "xfs_inode.h"
29#include "xfs_bmap.h"
30#include "xfs_bmap_util.h"
31#include "xfs_bmap_btree.h"
32#include "xfs_alloc.h"
33#include "xfs_error.h"
34#include "xfs_trans.h"
35#include "xfs_trans_space.h"
36#include "xfs_trace.h"
37#include "xfs_buf.h"
38#include "xfs_icache.h"
39#include "xfs_dinode.h"
Dave Chinner632b89e2013-10-29 22:11:58 +110040#include "xfs_rtalloc.h"
Dave Chinnerc963c612013-10-15 09:17:56 +110041
42
43/*
44 * Realtime allocator bitmap functions shared with userspace.
45 */
46
47/*
48 * Get a buffer for the bitmap or summary file block specified.
49 * The buffer is returned read and locked.
50 */
51int
52xfs_rtbuf_get(
53 xfs_mount_t *mp, /* file system mount structure */
54 xfs_trans_t *tp, /* transaction pointer */
55 xfs_rtblock_t block, /* block number in bitmap or summary */
56 int issum, /* is summary not bitmap */
57 xfs_buf_t **bpp) /* output: buffer for the block */
58{
59 xfs_buf_t *bp; /* block buffer, result */
60 xfs_inode_t *ip; /* bitmap or summary inode */
61 xfs_bmbt_irec_t map;
62 int nmap = 1;
63 int error; /* error value */
64
65 ip = issum ? mp->m_rsumip : mp->m_rbmip;
66
67 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
68 if (error)
69 return error;
70
71 ASSERT(map.br_startblock != NULLFSBLOCK);
72 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
73 XFS_FSB_TO_DADDR(mp, map.br_startblock),
74 mp->m_bsize, 0, &bp, NULL);
75 if (error)
76 return error;
Dave Chinnerc963c612013-10-15 09:17:56 +110077 *bpp = bp;
78 return 0;
79}
80
81/*
82 * Searching backward from start to limit, find the first block whose
83 * allocated/free state is different from start's.
84 */
85int
86xfs_rtfind_back(
87 xfs_mount_t *mp, /* file system mount point */
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_rtblock_t start, /* starting block to look at */
90 xfs_rtblock_t limit, /* last block to look at */
91 xfs_rtblock_t *rtblock) /* out: start block found */
92{
93 xfs_rtword_t *b; /* current word in buffer */
94 int bit; /* bit number in the word */
95 xfs_rtblock_t block; /* bitmap block number */
96 xfs_buf_t *bp; /* buf for the block */
97 xfs_rtword_t *bufp; /* starting word in buffer */
98 int error; /* error value */
99 xfs_rtblock_t firstbit; /* first useful bit in the word */
100 xfs_rtblock_t i; /* current bit number rel. to start */
101 xfs_rtblock_t len; /* length of inspected area */
102 xfs_rtword_t mask; /* mask of relevant bits for value */
103 xfs_rtword_t want; /* mask for "good" values */
104 xfs_rtword_t wdiff; /* difference from wanted value */
105 int word; /* word number in the buffer */
106
107 /*
108 * Compute and read in starting bitmap block for starting block.
109 */
110 block = XFS_BITTOBLOCK(mp, start);
111 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
112 if (error) {
113 return error;
114 }
115 bufp = bp->b_addr;
116 /*
117 * Get the first word's index & point to it.
118 */
119 word = XFS_BITTOWORD(mp, start);
120 b = &bufp[word];
121 bit = (int)(start & (XFS_NBWORD - 1));
122 len = start - limit + 1;
123 /*
124 * Compute match value, based on the bit at start: if 1 (free)
125 * then all-ones, else all-zeroes.
126 */
127 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
128 /*
129 * If the starting position is not word-aligned, deal with the
130 * partial word.
131 */
132 if (bit < XFS_NBWORD - 1) {
133 /*
134 * Calculate first (leftmost) bit number to look at,
135 * and mask for all the relevant bits in this word.
136 */
137 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
138 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
139 firstbit;
140 /*
141 * Calculate the difference between the value there
142 * and what we're looking for.
143 */
144 if ((wdiff = (*b ^ want) & mask)) {
145 /*
146 * Different. Mark where we are and return.
147 */
148 xfs_trans_brelse(tp, bp);
149 i = bit - XFS_RTHIBIT(wdiff);
150 *rtblock = start - i + 1;
151 return 0;
152 }
153 i = bit - firstbit + 1;
154 /*
155 * Go on to previous block if that's where the previous word is
156 * and we need the previous word.
157 */
158 if (--word == -1 && i < len) {
159 /*
160 * If done with this block, get the previous one.
161 */
162 xfs_trans_brelse(tp, bp);
163 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
164 if (error) {
165 return error;
166 }
167 bufp = bp->b_addr;
168 word = XFS_BLOCKWMASK(mp);
169 b = &bufp[word];
170 } else {
171 /*
172 * Go on to the previous word in the buffer.
173 */
174 b--;
175 }
176 } else {
177 /*
178 * Starting on a word boundary, no partial word.
179 */
180 i = 0;
181 }
182 /*
183 * Loop over whole words in buffers. When we use up one buffer
184 * we move on to the previous one.
185 */
186 while (len - i >= XFS_NBWORD) {
187 /*
188 * Compute difference between actual and desired value.
189 */
190 if ((wdiff = *b ^ want)) {
191 /*
192 * Different, mark where we are and return.
193 */
194 xfs_trans_brelse(tp, bp);
195 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
196 *rtblock = start - i + 1;
197 return 0;
198 }
199 i += XFS_NBWORD;
200 /*
201 * Go on to previous block if that's where the previous word is
202 * and we need the previous word.
203 */
204 if (--word == -1 && i < len) {
205 /*
206 * If done with this block, get the previous one.
207 */
208 xfs_trans_brelse(tp, bp);
209 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
210 if (error) {
211 return error;
212 }
213 bufp = bp->b_addr;
214 word = XFS_BLOCKWMASK(mp);
215 b = &bufp[word];
216 } else {
217 /*
218 * Go on to the previous word in the buffer.
219 */
220 b--;
221 }
222 }
223 /*
224 * If not ending on a word boundary, deal with the last
225 * (partial) word.
226 */
227 if (len - i) {
228 /*
229 * Calculate first (leftmost) bit number to look at,
230 * and mask for all the relevant bits in this word.
231 */
232 firstbit = XFS_NBWORD - (len - i);
233 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
234 /*
235 * Compute difference between actual and desired value.
236 */
237 if ((wdiff = (*b ^ want) & mask)) {
238 /*
239 * Different, mark where we are and return.
240 */
241 xfs_trans_brelse(tp, bp);
242 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
243 *rtblock = start - i + 1;
244 return 0;
245 } else
246 i = len;
247 }
248 /*
249 * No match, return that we scanned the whole area.
250 */
251 xfs_trans_brelse(tp, bp);
252 *rtblock = start - i + 1;
253 return 0;
254}
255
256/*
257 * Searching forward from start to limit, find the first block whose
258 * allocated/free state is different from start's.
259 */
260int
261xfs_rtfind_forw(
262 xfs_mount_t *mp, /* file system mount point */
263 xfs_trans_t *tp, /* transaction pointer */
264 xfs_rtblock_t start, /* starting block to look at */
265 xfs_rtblock_t limit, /* last block to look at */
266 xfs_rtblock_t *rtblock) /* out: start block found */
267{
268 xfs_rtword_t *b; /* current word in buffer */
269 int bit; /* bit number in the word */
270 xfs_rtblock_t block; /* bitmap block number */
271 xfs_buf_t *bp; /* buf for the block */
272 xfs_rtword_t *bufp; /* starting word in buffer */
273 int error; /* error value */
274 xfs_rtblock_t i; /* current bit number rel. to start */
275 xfs_rtblock_t lastbit; /* last useful bit in the word */
276 xfs_rtblock_t len; /* length of inspected area */
277 xfs_rtword_t mask; /* mask of relevant bits for value */
278 xfs_rtword_t want; /* mask for "good" values */
279 xfs_rtword_t wdiff; /* difference from wanted value */
280 int word; /* word number in the buffer */
281
282 /*
283 * Compute and read in starting bitmap block for starting block.
284 */
285 block = XFS_BITTOBLOCK(mp, start);
286 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
287 if (error) {
288 return error;
289 }
290 bufp = bp->b_addr;
291 /*
292 * Get the first word's index & point to it.
293 */
294 word = XFS_BITTOWORD(mp, start);
295 b = &bufp[word];
296 bit = (int)(start & (XFS_NBWORD - 1));
297 len = limit - start + 1;
298 /*
299 * Compute match value, based on the bit at start: if 1 (free)
300 * then all-ones, else all-zeroes.
301 */
302 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
303 /*
304 * If the starting position is not word-aligned, deal with the
305 * partial word.
306 */
307 if (bit) {
308 /*
309 * Calculate last (rightmost) bit number to look at,
310 * and mask for all the relevant bits in this word.
311 */
312 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
313 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
314 /*
315 * Calculate the difference between the value there
316 * and what we're looking for.
317 */
318 if ((wdiff = (*b ^ want) & mask)) {
319 /*
320 * Different. Mark where we are and return.
321 */
322 xfs_trans_brelse(tp, bp);
323 i = XFS_RTLOBIT(wdiff) - bit;
324 *rtblock = start + i - 1;
325 return 0;
326 }
327 i = lastbit - bit;
328 /*
329 * Go on to next block if that's where the next word is
330 * and we need the next word.
331 */
332 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
333 /*
334 * If done with this block, get the previous one.
335 */
336 xfs_trans_brelse(tp, bp);
337 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
338 if (error) {
339 return error;
340 }
341 b = bufp = bp->b_addr;
342 word = 0;
343 } else {
344 /*
345 * Go on to the previous word in the buffer.
346 */
347 b++;
348 }
349 } else {
350 /*
351 * Starting on a word boundary, no partial word.
352 */
353 i = 0;
354 }
355 /*
356 * Loop over whole words in buffers. When we use up one buffer
357 * we move on to the next one.
358 */
359 while (len - i >= XFS_NBWORD) {
360 /*
361 * Compute difference between actual and desired value.
362 */
363 if ((wdiff = *b ^ want)) {
364 /*
365 * Different, mark where we are and return.
366 */
367 xfs_trans_brelse(tp, bp);
368 i += XFS_RTLOBIT(wdiff);
369 *rtblock = start + i - 1;
370 return 0;
371 }
372 i += XFS_NBWORD;
373 /*
374 * Go on to next block if that's where the next word is
375 * and we need the next word.
376 */
377 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
378 /*
379 * If done with this block, get the next one.
380 */
381 xfs_trans_brelse(tp, bp);
382 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
383 if (error) {
384 return error;
385 }
386 b = bufp = bp->b_addr;
387 word = 0;
388 } else {
389 /*
390 * Go on to the next word in the buffer.
391 */
392 b++;
393 }
394 }
395 /*
396 * If not ending on a word boundary, deal with the last
397 * (partial) word.
398 */
399 if ((lastbit = len - i)) {
400 /*
401 * Calculate mask for all the relevant bits in this word.
402 */
403 mask = ((xfs_rtword_t)1 << lastbit) - 1;
404 /*
405 * Compute difference between actual and desired value.
406 */
407 if ((wdiff = (*b ^ want) & mask)) {
408 /*
409 * Different, mark where we are and return.
410 */
411 xfs_trans_brelse(tp, bp);
412 i += XFS_RTLOBIT(wdiff);
413 *rtblock = start + i - 1;
414 return 0;
415 } else
416 i = len;
417 }
418 /*
419 * No match, return that we scanned the whole area.
420 */
421 xfs_trans_brelse(tp, bp);
422 *rtblock = start + i - 1;
423 return 0;
424}
425
426/*
Eric Sandeenafabfd32014-09-09 11:58:42 +1000427 * Read and/or modify the summary information for a given extent size,
Dave Chinnerc963c612013-10-15 09:17:56 +1100428 * bitmap block combination.
429 * Keeps track of a current summary block, so we don't keep reading
430 * it from the buffer cache.
Eric Sandeenafabfd32014-09-09 11:58:42 +1000431 *
432 * Summary information is returned in *sum if specified.
433 * If no delta is specified, returns summary only.
Dave Chinnerc963c612013-10-15 09:17:56 +1100434 */
435int
Eric Sandeenafabfd32014-09-09 11:58:42 +1000436xfs_rtmodify_summary_int(
437 xfs_mount_t *mp, /* file system mount structure */
Dave Chinnerc963c612013-10-15 09:17:56 +1100438 xfs_trans_t *tp, /* transaction pointer */
439 int log, /* log2 of extent size */
440 xfs_rtblock_t bbno, /* bitmap block number */
441 int delta, /* change to make to summary info */
442 xfs_buf_t **rbpp, /* in/out: summary block buffer */
Eric Sandeenafabfd32014-09-09 11:58:42 +1000443 xfs_fsblock_t *rsb, /* in/out: summary block number */
444 xfs_suminfo_t *sum) /* out: summary info for this block */
Dave Chinnerc963c612013-10-15 09:17:56 +1100445{
446 xfs_buf_t *bp; /* buffer for the summary block */
447 int error; /* error value */
448 xfs_fsblock_t sb; /* summary fsblock */
449 int so; /* index into the summary file */
450 xfs_suminfo_t *sp; /* pointer to returned data */
451
452 /*
453 * Compute entry number in the summary file.
454 */
455 so = XFS_SUMOFFS(mp, log, bbno);
456 /*
457 * Compute the block number in the summary file.
458 */
459 sb = XFS_SUMOFFSTOBLOCK(mp, so);
460 /*
461 * If we have an old buffer, and the block number matches, use that.
462 */
463 if (rbpp && *rbpp && *rsb == sb)
464 bp = *rbpp;
465 /*
466 * Otherwise we have to get the buffer.
467 */
468 else {
469 /*
470 * If there was an old one, get rid of it first.
471 */
472 if (rbpp && *rbpp)
473 xfs_trans_brelse(tp, *rbpp);
474 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
475 if (error) {
476 return error;
477 }
478 /*
479 * Remember this buffer and block for the next call.
480 */
481 if (rbpp) {
482 *rbpp = bp;
483 *rsb = sb;
484 }
485 }
486 /*
Eric Sandeenafabfd32014-09-09 11:58:42 +1000487 * Point to the summary information, modify/log it, and/or copy it out.
Dave Chinnerc963c612013-10-15 09:17:56 +1100488 */
489 sp = XFS_SUMPTR(mp, bp, so);
Eric Sandeenafabfd32014-09-09 11:58:42 +1000490 if (delta) {
491 uint first = (uint)((char *)sp - (char *)bp->b_addr);
492
493 *sp += delta;
494 xfs_trans_log_buf(tp, bp, first, first + sizeof(*sp) - 1);
495 }
496 if (sum) {
497 /*
498 * Drop the buffer if we're not asked to remember it.
499 */
500 if (!rbpp)
501 xfs_trans_brelse(tp, bp);
502 *sum = *sp;
503 }
Dave Chinnerc963c612013-10-15 09:17:56 +1100504 return 0;
505}
506
Eric Sandeenafabfd32014-09-09 11:58:42 +1000507int
508xfs_rtmodify_summary(
509 xfs_mount_t *mp, /* file system mount structure */
510 xfs_trans_t *tp, /* transaction pointer */
511 int log, /* log2 of extent size */
512 xfs_rtblock_t bbno, /* bitmap block number */
513 int delta, /* change to make to summary info */
514 xfs_buf_t **rbpp, /* in/out: summary block buffer */
515 xfs_fsblock_t *rsb) /* in/out: summary block number */
516{
517 return xfs_rtmodify_summary_int(mp, tp, log, bbno,
518 delta, rbpp, rsb, NULL);
519}
520
Dave Chinnerc963c612013-10-15 09:17:56 +1100521/*
522 * Set the given range of bitmap bits to the given value.
523 * Do whatever I/O and logging is required.
524 */
525int
526xfs_rtmodify_range(
527 xfs_mount_t *mp, /* file system mount point */
528 xfs_trans_t *tp, /* transaction pointer */
529 xfs_rtblock_t start, /* starting block to modify */
530 xfs_extlen_t len, /* length of extent to modify */
531 int val) /* 1 for free, 0 for allocated */
532{
533 xfs_rtword_t *b; /* current word in buffer */
534 int bit; /* bit number in the word */
535 xfs_rtblock_t block; /* bitmap block number */
536 xfs_buf_t *bp; /* buf for the block */
537 xfs_rtword_t *bufp; /* starting word in buffer */
538 int error; /* error value */
539 xfs_rtword_t *first; /* first used word in the buffer */
540 int i; /* current bit number rel. to start */
541 int lastbit; /* last useful bit in word */
542 xfs_rtword_t mask; /* mask o frelevant bits for value */
543 int word; /* word number in the buffer */
544
545 /*
546 * Compute starting bitmap block number.
547 */
548 block = XFS_BITTOBLOCK(mp, start);
549 /*
550 * Read the bitmap block, and point to its data.
551 */
552 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
553 if (error) {
554 return error;
555 }
556 bufp = bp->b_addr;
557 /*
558 * Compute the starting word's address, and starting bit.
559 */
560 word = XFS_BITTOWORD(mp, start);
561 first = b = &bufp[word];
562 bit = (int)(start & (XFS_NBWORD - 1));
563 /*
564 * 0 (allocated) => all zeroes; 1 (free) => all ones.
565 */
566 val = -val;
567 /*
568 * If not starting on a word boundary, deal with the first
569 * (partial) word.
570 */
571 if (bit) {
572 /*
573 * Compute first bit not changed and mask of relevant bits.
574 */
575 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
576 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
577 /*
578 * Set/clear the active bits.
579 */
580 if (val)
581 *b |= mask;
582 else
583 *b &= ~mask;
584 i = lastbit - bit;
585 /*
586 * Go on to the next block if that's where the next word is
587 * and we need the next word.
588 */
589 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
590 /*
591 * Log the changed part of this block.
592 * Get the next one.
593 */
594 xfs_trans_log_buf(tp, bp,
595 (uint)((char *)first - (char *)bufp),
596 (uint)((char *)b - (char *)bufp));
597 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
598 if (error) {
599 return error;
600 }
601 first = b = bufp = bp->b_addr;
602 word = 0;
603 } else {
604 /*
605 * Go on to the next word in the buffer
606 */
607 b++;
608 }
609 } else {
610 /*
611 * Starting on a word boundary, no partial word.
612 */
613 i = 0;
614 }
615 /*
616 * Loop over whole words in buffers. When we use up one buffer
617 * we move on to the next one.
618 */
619 while (len - i >= XFS_NBWORD) {
620 /*
621 * Set the word value correctly.
622 */
623 *b = val;
624 i += XFS_NBWORD;
625 /*
626 * Go on to the next block if that's where the next word is
627 * and we need the next word.
628 */
629 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
630 /*
631 * Log the changed part of this block.
632 * Get the next one.
633 */
634 xfs_trans_log_buf(tp, bp,
635 (uint)((char *)first - (char *)bufp),
636 (uint)((char *)b - (char *)bufp));
637 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
638 if (error) {
639 return error;
640 }
641 first = b = bufp = bp->b_addr;
642 word = 0;
643 } else {
644 /*
645 * Go on to the next word in the buffer
646 */
647 b++;
648 }
649 }
650 /*
651 * If not ending on a word boundary, deal with the last
652 * (partial) word.
653 */
654 if ((lastbit = len - i)) {
655 /*
656 * Compute a mask of relevant bits.
657 */
658 bit = 0;
659 mask = ((xfs_rtword_t)1 << lastbit) - 1;
660 /*
661 * Set/clear the active bits.
662 */
663 if (val)
664 *b |= mask;
665 else
666 *b &= ~mask;
667 b++;
668 }
669 /*
670 * Log any remaining changed bytes.
671 */
672 if (b > first)
673 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
674 (uint)((char *)b - (char *)bufp - 1));
675 return 0;
676}
677
678/*
679 * Mark an extent specified by start and len freed.
680 * Updates all the summary information as well as the bitmap.
681 */
682int
683xfs_rtfree_range(
684 xfs_mount_t *mp, /* file system mount point */
685 xfs_trans_t *tp, /* transaction pointer */
686 xfs_rtblock_t start, /* starting block to free */
687 xfs_extlen_t len, /* length to free */
688 xfs_buf_t **rbpp, /* in/out: summary block buffer */
689 xfs_fsblock_t *rsb) /* in/out: summary block number */
690{
691 xfs_rtblock_t end; /* end of the freed extent */
692 int error; /* error value */
693 xfs_rtblock_t postblock; /* first block freed > end */
694 xfs_rtblock_t preblock; /* first block freed < start */
695
696 end = start + len - 1;
697 /*
698 * Modify the bitmap to mark this extent freed.
699 */
700 error = xfs_rtmodify_range(mp, tp, start, len, 1);
701 if (error) {
702 return error;
703 }
704 /*
705 * Assume we're freeing out of the middle of an allocated extent.
706 * We need to find the beginning and end of the extent so we can
707 * properly update the summary.
708 */
709 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
710 if (error) {
711 return error;
712 }
713 /*
714 * Find the next allocated block (end of allocated extent).
715 */
716 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
717 &postblock);
718 if (error)
719 return error;
720 /*
721 * If there are blocks not being freed at the front of the
722 * old extent, add summary data for them to be allocated.
723 */
724 if (preblock < start) {
725 error = xfs_rtmodify_summary(mp, tp,
726 XFS_RTBLOCKLOG(start - preblock),
727 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
728 if (error) {
729 return error;
730 }
731 }
732 /*
733 * If there are blocks not being freed at the end of the
734 * old extent, add summary data for them to be allocated.
735 */
736 if (postblock > end) {
737 error = xfs_rtmodify_summary(mp, tp,
738 XFS_RTBLOCKLOG(postblock - end),
739 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
740 if (error) {
741 return error;
742 }
743 }
744 /*
745 * Increment the summary information corresponding to the entire
746 * (new) free extent.
747 */
748 error = xfs_rtmodify_summary(mp, tp,
749 XFS_RTBLOCKLOG(postblock + 1 - preblock),
750 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
751 return error;
752}
753
754/*
755 * Check that the given range is either all allocated (val = 0) or
756 * all free (val = 1).
757 */
758int
759xfs_rtcheck_range(
760 xfs_mount_t *mp, /* file system mount point */
761 xfs_trans_t *tp, /* transaction pointer */
762 xfs_rtblock_t start, /* starting block number of extent */
763 xfs_extlen_t len, /* length of extent */
764 int val, /* 1 for free, 0 for allocated */
765 xfs_rtblock_t *new, /* out: first block not matching */
766 int *stat) /* out: 1 for matches, 0 for not */
767{
768 xfs_rtword_t *b; /* current word in buffer */
769 int bit; /* bit number in the word */
770 xfs_rtblock_t block; /* bitmap block number */
771 xfs_buf_t *bp; /* buf for the block */
772 xfs_rtword_t *bufp; /* starting word in buffer */
773 int error; /* error value */
774 xfs_rtblock_t i; /* current bit number rel. to start */
775 xfs_rtblock_t lastbit; /* last useful bit in word */
776 xfs_rtword_t mask; /* mask of relevant bits for value */
777 xfs_rtword_t wdiff; /* difference from wanted value */
778 int word; /* word number in the buffer */
779
780 /*
781 * Compute starting bitmap block number
782 */
783 block = XFS_BITTOBLOCK(mp, start);
784 /*
785 * Read the bitmap block.
786 */
787 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
788 if (error) {
789 return error;
790 }
791 bufp = bp->b_addr;
792 /*
793 * Compute the starting word's address, and starting bit.
794 */
795 word = XFS_BITTOWORD(mp, start);
796 b = &bufp[word];
797 bit = (int)(start & (XFS_NBWORD - 1));
798 /*
799 * 0 (allocated) => all zero's; 1 (free) => all one's.
800 */
801 val = -val;
802 /*
803 * If not starting on a word boundary, deal with the first
804 * (partial) word.
805 */
806 if (bit) {
807 /*
808 * Compute first bit not examined.
809 */
810 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
811 /*
812 * Mask of relevant bits.
813 */
814 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
815 /*
816 * Compute difference between actual and desired value.
817 */
818 if ((wdiff = (*b ^ val) & mask)) {
819 /*
820 * Different, compute first wrong bit and return.
821 */
822 xfs_trans_brelse(tp, bp);
823 i = XFS_RTLOBIT(wdiff) - bit;
824 *new = start + i;
825 *stat = 0;
826 return 0;
827 }
828 i = lastbit - bit;
829 /*
830 * Go on to next block if that's where the next word is
831 * and we need the next word.
832 */
833 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
834 /*
835 * If done with this block, get the next one.
836 */
837 xfs_trans_brelse(tp, bp);
838 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
839 if (error) {
840 return error;
841 }
842 b = bufp = bp->b_addr;
843 word = 0;
844 } else {
845 /*
846 * Go on to the next word in the buffer.
847 */
848 b++;
849 }
850 } else {
851 /*
852 * Starting on a word boundary, no partial word.
853 */
854 i = 0;
855 }
856 /*
857 * Loop over whole words in buffers. When we use up one buffer
858 * we move on to the next one.
859 */
860 while (len - i >= XFS_NBWORD) {
861 /*
862 * Compute difference between actual and desired value.
863 */
864 if ((wdiff = *b ^ val)) {
865 /*
866 * Different, compute first wrong bit and return.
867 */
868 xfs_trans_brelse(tp, bp);
869 i += XFS_RTLOBIT(wdiff);
870 *new = start + i;
871 *stat = 0;
872 return 0;
873 }
874 i += XFS_NBWORD;
875 /*
876 * Go on to next block if that's where the next word is
877 * and we need the next word.
878 */
879 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
880 /*
881 * If done with this block, get the next one.
882 */
883 xfs_trans_brelse(tp, bp);
884 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
885 if (error) {
886 return error;
887 }
888 b = bufp = bp->b_addr;
889 word = 0;
890 } else {
891 /*
892 * Go on to the next word in the buffer.
893 */
894 b++;
895 }
896 }
897 /*
898 * If not ending on a word boundary, deal with the last
899 * (partial) word.
900 */
901 if ((lastbit = len - i)) {
902 /*
903 * Mask of relevant bits.
904 */
905 mask = ((xfs_rtword_t)1 << lastbit) - 1;
906 /*
907 * Compute difference between actual and desired value.
908 */
909 if ((wdiff = (*b ^ val) & mask)) {
910 /*
911 * Different, compute first wrong bit and return.
912 */
913 xfs_trans_brelse(tp, bp);
914 i += XFS_RTLOBIT(wdiff);
915 *new = start + i;
916 *stat = 0;
917 return 0;
918 } else
919 i = len;
920 }
921 /*
922 * Successful, return.
923 */
924 xfs_trans_brelse(tp, bp);
925 *new = start + i;
926 *stat = 1;
927 return 0;
928}
929
930#ifdef DEBUG
931/*
932 * Check that the given extent (block range) is allocated already.
933 */
934STATIC int /* error */
935xfs_rtcheck_alloc_range(
936 xfs_mount_t *mp, /* file system mount point */
937 xfs_trans_t *tp, /* transaction pointer */
938 xfs_rtblock_t bno, /* starting block number of extent */
939 xfs_extlen_t len) /* length of extent */
940{
941 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
942 int stat;
943 int error;
944
945 error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
946 if (error)
947 return error;
948 ASSERT(stat);
949 return 0;
950}
951#else
952#define xfs_rtcheck_alloc_range(m,t,b,l) (0)
953#endif
954/*
955 * Free an extent in the realtime subvolume. Length is expressed in
956 * realtime extents, as is the block number.
957 */
958int /* error */
959xfs_rtfree_extent(
960 xfs_trans_t *tp, /* transaction pointer */
961 xfs_rtblock_t bno, /* starting block number to free */
962 xfs_extlen_t len) /* length of extent freed */
963{
964 int error; /* error value */
965 xfs_mount_t *mp; /* file system mount structure */
966 xfs_fsblock_t sb; /* summary file block number */
967 xfs_buf_t *sumbp = NULL; /* summary file block buffer */
968
969 mp = tp->t_mountp;
970
971 ASSERT(mp->m_rbmip->i_itemp != NULL);
972 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
973
974 error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
975 if (error)
976 return error;
977
978 /*
979 * Free the range of realtime blocks.
980 */
981 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
982 if (error) {
983 return error;
984 }
985 /*
986 * Mark more blocks free in the superblock.
987 */
988 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
989 /*
990 * If we've now freed all the blocks, reset the file sequence
991 * number to 0.
992 */
993 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
994 mp->m_sb.sb_rextents) {
995 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
996 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
997 *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
998 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
999 }
1000 return 0;
1001}
1002