]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_rtalloc.c
xfsprogs: Die dir1 Die!
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_rtalloc.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
da23017d 3 * All Rights Reserved.
5000d01d 4 *
da23017d
NS
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
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
NS
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.
5000d01d 13 *
da23017d
NS
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
2bd0ea18
NS
17 */
18
5e656dbb
BN
19#include <xfs.h>
20
2bd0ea18 21/*
5e656dbb 22 * Prototypes for internal functions.
2bd0ea18 23 */
dfc130f3 24
2bd0ea18 25
5e656dbb
BN
26STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
27 xfs_rtblock_t, xfs_rtblock_t *);
28STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
29 xfs_rtblock_t, xfs_rtblock_t *);
30STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
31 xfs_extlen_t, int);
32STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
33 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
34
35/*
36 * Internal functions.
37 */
2bd0ea18
NS
38
39/*
40 * Get a buffer for the bitmap or summary file block specified.
41 * The buffer is returned read and locked.
42 */
43STATIC int /* error */
44xfs_rtbuf_get(
45 xfs_mount_t *mp, /* file system mount structure */
46 xfs_trans_t *tp, /* transaction pointer */
47 xfs_rtblock_t block, /* block number in bitmap or summary */
48 int issum, /* is summary not bitmap */
e49e365f 49 xfs_buf_t **bpp) /* output: buffer for the block */
2bd0ea18 50{
e49e365f
NS
51 xfs_buf_t *bp; /* block buffer, result */
52 xfs_daddr_t d; /* disk addr of block */
2bd0ea18
NS
53 int error; /* error value */
54 xfs_fsblock_t fsb; /* fs block number for block */
55 xfs_inode_t *ip; /* bitmap or summary inode */
56
57 ip = issum ? mp->m_rsumip : mp->m_rbmip;
58 /*
59 * Map from the file offset (block) and inode number to the
60 * file system block.
61 */
62 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
63 if (error) {
64 return error;
65 }
66 ASSERT(fsb != NULLFSBLOCK);
67 /*
68 * Convert to disk address for buffer cache.
69 */
70 d = XFS_FSB_TO_DADDR(mp, fsb);
71 /*
72 * Read the buffer.
73 */
74 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
75 mp->m_bsize, 0, &bp);
76 if (error) {
77 return error;
78 }
79 ASSERT(bp && !XFS_BUF_GETERROR(bp));
80 *bpp = bp;
81 return 0;
82}
83
84/*
85 * Searching backward from start to limit, find the first block whose
86 * allocated/free state is different from start's.
87 */
88STATIC int /* error */
89xfs_rtfind_back(
90 xfs_mount_t *mp, /* file system mount point */
91 xfs_trans_t *tp, /* transaction pointer */
92 xfs_rtblock_t start, /* starting block to look at */
93 xfs_rtblock_t limit, /* last block to look at */
94 xfs_rtblock_t *rtblock) /* out: start block found */
95{
96 xfs_rtword_t *b; /* current word in buffer */
97 int bit; /* bit number in the word */
98 xfs_rtblock_t block; /* bitmap block number */
e49e365f 99 xfs_buf_t *bp; /* buf for the block */
2bd0ea18
NS
100 xfs_rtword_t *bufp; /* starting word in buffer */
101 int error; /* error value */
102 xfs_rtblock_t firstbit; /* first useful bit in the word */
103 xfs_rtblock_t i; /* current bit number rel. to start */
104 xfs_rtblock_t len; /* length of inspected area */
105 xfs_rtword_t mask; /* mask of relevant bits for value */
106 xfs_rtword_t want; /* mask for "good" values */
107 xfs_rtword_t wdiff; /* difference from wanted value */
108 int word; /* word number in the buffer */
109
110 /*
111 * Compute and read in starting bitmap block for starting block.
112 */
113 block = XFS_BITTOBLOCK(mp, start);
114 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
115 if (error) {
116 return error;
117 }
118 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
119 /*
120 * Get the first word's index & point to it.
121 */
122 word = XFS_BITTOWORD(mp, start);
123 b = &bufp[word];
124 bit = (int)(start & (XFS_NBWORD - 1));
125 len = start - limit + 1;
126 /*
127 * Compute match value, based on the bit at start: if 1 (free)
128 * then all-ones, else all-zeroes.
129 */
130 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
131 /*
132 * If the starting position is not word-aligned, deal with the
133 * partial word.
134 */
135 if (bit < XFS_NBWORD - 1) {
136 /*
137 * Calculate first (leftmost) bit number to look at,
138 * and mask for all the relevant bits in this word.
139 */
140 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
141 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
142 firstbit;
143 /*
144 * Calculate the difference between the value there
145 * and what we're looking for.
146 */
0e266570 147 if ((wdiff = (*b ^ want) & mask)) {
2bd0ea18
NS
148 /*
149 * Different. Mark where we are and return.
150 */
151 xfs_trans_brelse(tp, bp);
152 i = bit - XFS_RTHIBIT(wdiff);
153 *rtblock = start - i + 1;
154 return 0;
155 }
156 i = bit - firstbit + 1;
157 /*
158 * Go on to previous block if that's where the previous word is
159 * and we need the previous word.
160 */
161 if (--word == -1 && i < len) {
162 /*
163 * If done with this block, get the previous one.
164 */
165 xfs_trans_brelse(tp, bp);
166 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
167 if (error) {
168 return error;
169 }
170 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
171 word = XFS_BLOCKWMASK(mp);
172 b = &bufp[word];
173 } else {
174 /*
175 * Go on to the previous word in the buffer.
176 */
177 b--;
178 }
179 } else {
180 /*
181 * Starting on a word boundary, no partial word.
182 */
183 i = 0;
184 }
185 /*
186 * Loop over whole words in buffers. When we use up one buffer
187 * we move on to the previous one.
188 */
189 while (len - i >= XFS_NBWORD) {
190 /*
191 * Compute difference between actual and desired value.
192 */
0e266570 193 if ((wdiff = *b ^ want)) {
2bd0ea18
NS
194 /*
195 * Different, mark where we are and return.
196 */
197 xfs_trans_brelse(tp, bp);
198 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
199 *rtblock = start - i + 1;
200 return 0;
201 }
202 i += XFS_NBWORD;
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 xfs_trans_brelse(tp, bp);
212 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
213 if (error) {
214 return error;
215 }
216 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
217 word = XFS_BLOCKWMASK(mp);
218 b = &bufp[word];
219 } else {
220 /*
221 * Go on to the previous word in the buffer.
222 */
223 b--;
224 }
225 }
226 /*
227 * If not ending on a word boundary, deal with the last
228 * (partial) word.
229 */
230 if (len - i) {
231 /*
232 * Calculate first (leftmost) bit number to look at,
233 * and mask for all the relevant bits in this word.
234 */
235 firstbit = XFS_NBWORD - (len - i);
236 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
237 /*
238 * Compute difference between actual and desired value.
239 */
0e266570 240 if ((wdiff = (*b ^ want) & mask)) {
2bd0ea18
NS
241 /*
242 * Different, mark where we are and return.
243 */
244 xfs_trans_brelse(tp, bp);
245 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
246 *rtblock = start - i + 1;
247 return 0;
248 } else
249 i = len;
250 }
251 /*
252 * No match, return that we scanned the whole area.
253 */
254 xfs_trans_brelse(tp, bp);
255 *rtblock = start - i + 1;
256 return 0;
257}
258
259/*
260 * Searching forward from start to limit, find the first block whose
261 * allocated/free state is different from start's.
262 */
263STATIC int /* error */
264xfs_rtfind_forw(
265 xfs_mount_t *mp, /* file system mount point */
266 xfs_trans_t *tp, /* transaction pointer */
267 xfs_rtblock_t start, /* starting block to look at */
268 xfs_rtblock_t limit, /* last block to look at */
269 xfs_rtblock_t *rtblock) /* out: start block found */
270{
271 xfs_rtword_t *b; /* current word in buffer */
272 int bit; /* bit number in the word */
273 xfs_rtblock_t block; /* bitmap block number */
e49e365f 274 xfs_buf_t *bp; /* buf for the block */
2bd0ea18
NS
275 xfs_rtword_t *bufp; /* starting word in buffer */
276 int error; /* error value */
277 xfs_rtblock_t i; /* current bit number rel. to start */
278 xfs_rtblock_t lastbit; /* last useful bit in the word */
279 xfs_rtblock_t len; /* length of inspected area */
280 xfs_rtword_t mask; /* mask of relevant bits for value */
281 xfs_rtword_t want; /* mask for "good" values */
282 xfs_rtword_t wdiff; /* difference from wanted value */
283 int word; /* word number in the buffer */
284
285 /*
286 * Compute and read in starting bitmap block for starting block.
287 */
288 block = XFS_BITTOBLOCK(mp, start);
289 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
290 if (error) {
291 return error;
292 }
293 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
294 /*
295 * Get the first word's index & point to it.
296 */
297 word = XFS_BITTOWORD(mp, start);
298 b = &bufp[word];
299 bit = (int)(start & (XFS_NBWORD - 1));
300 len = limit - start + 1;
301 /*
302 * Compute match value, based on the bit at start: if 1 (free)
303 * then all-ones, else all-zeroes.
304 */
305 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
306 /*
307 * If the starting position is not word-aligned, deal with the
308 * partial word.
309 */
310 if (bit) {
311 /*
312 * Calculate last (rightmost) bit number to look at,
313 * and mask for all the relevant bits in this word.
314 */
315 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
316 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
317 /*
318 * Calculate the difference between the value there
319 * and what we're looking for.
320 */
0e266570 321 if ((wdiff = (*b ^ want) & mask)) {
2bd0ea18
NS
322 /*
323 * Different. Mark where we are and return.
324 */
325 xfs_trans_brelse(tp, bp);
326 i = XFS_RTLOBIT(wdiff) - bit;
327 *rtblock = start + i - 1;
328 return 0;
329 }
330 i = lastbit - bit;
331 /*
332 * Go on to next block if that's where the next word is
333 * and we need the next word.
334 */
335 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
336 /*
337 * If done with this block, get the previous one.
338 */
339 xfs_trans_brelse(tp, bp);
340 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
341 if (error) {
342 return error;
343 }
344 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
345 word = 0;
346 } else {
347 /*
348 * Go on to the previous word in the buffer.
349 */
350 b++;
351 }
352 } else {
353 /*
354 * Starting on a word boundary, no partial word.
355 */
356 i = 0;
357 }
358 /*
359 * Loop over whole words in buffers. When we use up one buffer
360 * we move on to the next one.
361 */
362 while (len - i >= XFS_NBWORD) {
363 /*
364 * Compute difference between actual and desired value.
365 */
0e266570 366 if ((wdiff = *b ^ want)) {
2bd0ea18
NS
367 /*
368 * Different, mark where we are and return.
369 */
370 xfs_trans_brelse(tp, bp);
371 i += XFS_RTLOBIT(wdiff);
372 *rtblock = start + i - 1;
373 return 0;
374 }
375 i += XFS_NBWORD;
376 /*
377 * Go on to next block if that's where the next word is
378 * and we need the next word.
379 */
380 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
381 /*
382 * If done with this block, get the next one.
383 */
384 xfs_trans_brelse(tp, bp);
385 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
386 if (error) {
387 return error;
388 }
389 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
390 word = 0;
391 } else {
392 /*
393 * Go on to the next word in the buffer.
394 */
395 b++;
396 }
397 }
398 /*
399 * If not ending on a word boundary, deal with the last
400 * (partial) word.
401 */
0e266570 402 if ((lastbit = len - i)) {
2bd0ea18
NS
403 /*
404 * Calculate mask for all the relevant bits in this word.
405 */
406 mask = ((xfs_rtword_t)1 << lastbit) - 1;
407 /*
408 * Compute difference between actual and desired value.
409 */
0e266570 410 if ((wdiff = (*b ^ want) & mask)) {
2bd0ea18
NS
411 /*
412 * Different, mark where we are and return.
413 */
414 xfs_trans_brelse(tp, bp);
415 i += XFS_RTLOBIT(wdiff);
416 *rtblock = start + i - 1;
417 return 0;
418 } else
419 i = len;
420 }
421 /*
422 * No match, return that we scanned the whole area.
423 */
424 xfs_trans_brelse(tp, bp);
425 *rtblock = start + i - 1;
426 return 0;
427}
428
429/*
430 * Mark an extent specified by start and len freed.
431 * Updates all the summary information as well as the bitmap.
432 */
433STATIC int /* error */
434xfs_rtfree_range(
435 xfs_mount_t *mp, /* file system mount point */
436 xfs_trans_t *tp, /* transaction pointer */
437 xfs_rtblock_t start, /* starting block to free */
438 xfs_extlen_t len, /* length to free */
e49e365f 439 xfs_buf_t **rbpp, /* in/out: summary block buffer */
2bd0ea18
NS
440 xfs_fsblock_t *rsb) /* in/out: summary block number */
441{
442 xfs_rtblock_t end; /* end of the freed extent */
443 int error; /* error value */
444 xfs_rtblock_t postblock; /* first block freed > end */
445 xfs_rtblock_t preblock; /* first block freed < start */
446
447 end = start + len - 1;
448 /*
449 * Modify the bitmap to mark this extent freed.
450 */
451 error = xfs_rtmodify_range(mp, tp, start, len, 1);
452 if (error) {
453 return error;
454 }
455 /*
456 * Assume we're freeing out of the middle of an allocated extent.
457 * We need to find the beginning and end of the extent so we can
458 * properly update the summary.
459 */
460 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
461 if (error) {
462 return error;
463 }
464 /*
465 * Find the next allocated block (end of allocated extent).
466 */
467 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
468 &postblock);
904f8474
DC
469 if (error)
470 return error;
2bd0ea18
NS
471 /*
472 * If there are blocks not being freed at the front of the
473 * old extent, add summary data for them to be allocated.
474 */
475 if (preblock < start) {
476 error = xfs_rtmodify_summary(mp, tp,
477 XFS_RTBLOCKLOG(start - preblock),
478 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
479 if (error) {
480 return error;
481 }
482 }
483 /*
484 * If there are blocks not being freed at the end of the
485 * old extent, add summary data for them to be allocated.
486 */
487 if (postblock > end) {
488 error = xfs_rtmodify_summary(mp, tp,
489 XFS_RTBLOCKLOG(postblock - end),
490 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
491 if (error) {
492 return error;
493 }
494 }
495 /*
496 * Increment the summary information corresponding to the entire
497 * (new) free extent.
498 */
499 error = xfs_rtmodify_summary(mp, tp,
500 XFS_RTBLOCKLOG(postblock + 1 - preblock),
501 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
502 return error;
503}
504
505/*
506 * Set the given range of bitmap bits to the given value.
507 * Do whatever I/O and logging is required.
508 */
509STATIC int /* error */
510xfs_rtmodify_range(
511 xfs_mount_t *mp, /* file system mount point */
512 xfs_trans_t *tp, /* transaction pointer */
513 xfs_rtblock_t start, /* starting block to modify */
514 xfs_extlen_t len, /* length of extent to modify */
515 int val) /* 1 for free, 0 for allocated */
516{
517 xfs_rtword_t *b; /* current word in buffer */
518 int bit; /* bit number in the word */
519 xfs_rtblock_t block; /* bitmap block number */
006d8e9a 520 xfs_buf_t *bp; /* buf for the block */
2bd0ea18
NS
521 xfs_rtword_t *bufp; /* starting word in buffer */
522 int error; /* error value */
523 xfs_rtword_t *first; /* first used word in the buffer */
524 int i; /* current bit number rel. to start */
525 int lastbit; /* last useful bit in word */
526 xfs_rtword_t mask; /* mask o frelevant bits for value */
527 int word; /* word number in the buffer */
528
529 /*
530 * Compute starting bitmap block number.
531 */
532 block = XFS_BITTOBLOCK(mp, start);
533 /*
534 * Read the bitmap block, and point to its data.
535 */
536 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
537 if (error) {
538 return error;
539 }
540 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
541 /*
542 * Compute the starting word's address, and starting bit.
543 */
544 word = XFS_BITTOWORD(mp, start);
545 first = b = &bufp[word];
546 bit = (int)(start & (XFS_NBWORD - 1));
547 /*
548 * 0 (allocated) => all zeroes; 1 (free) => all ones.
549 */
550 val = -val;
551 /*
552 * If not starting on a word boundary, deal with the first
553 * (partial) word.
554 */
555 if (bit) {
556 /*
557 * Compute first bit not changed and mask of relevant bits.
558 */
559 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
560 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
561 /*
562 * Set/clear the active bits.
563 */
564 if (val)
565 *b |= mask;
566 else
567 *b &= ~mask;
568 i = lastbit - bit;
569 /*
570 * Go on to the next block if that's where the next word is
571 * and we need the next word.
572 */
573 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
574 /*
575 * Log the changed part of this block.
576 * Get the next one.
577 */
578 xfs_trans_log_buf(tp, bp,
579 (uint)((char *)first - (char *)bufp),
580 (uint)((char *)b - (char *)bufp));
581 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
582 if (error) {
583 return error;
584 }
585 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
586 word = 0;
587 } else {
588 /*
589 * Go on to the next word in the buffer
590 */
591 b++;
592 }
593 } else {
594 /*
595 * Starting on a word boundary, no partial word.
596 */
597 i = 0;
598 }
599 /*
600 * Loop over whole words in buffers. When we use up one buffer
601 * we move on to the next one.
602 */
603 while (len - i >= XFS_NBWORD) {
604 /*
605 * Set the word value correctly.
606 */
607 *b = val;
608 i += XFS_NBWORD;
609 /*
610 * Go on to the next block if that's where the next word is
611 * and we need the next word.
612 */
613 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
614 /*
615 * Log the changed part of this block.
616 * Get the next one.
617 */
618 xfs_trans_log_buf(tp, bp,
619 (uint)((char *)first - (char *)bufp),
620 (uint)((char *)b - (char *)bufp));
621 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
622 if (error) {
623 return error;
624 }
625 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
626 word = 0;
627 } else {
628 /*
629 * Go on to the next word in the buffer
630 */
631 b++;
632 }
633 }
634 /*
635 * If not ending on a word boundary, deal with the last
636 * (partial) word.
637 */
0e266570 638 if ((lastbit = len - i)) {
2bd0ea18
NS
639 /*
640 * Compute a mask of relevant bits.
641 */
642 bit = 0;
643 mask = ((xfs_rtword_t)1 << lastbit) - 1;
644 /*
645 * Set/clear the active bits.
646 */
647 if (val)
648 *b |= mask;
649 else
650 *b &= ~mask;
651 b++;
652 }
653 /*
654 * Log any remaining changed bytes.
655 */
656 if (b > first)
657 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
658 (uint)((char *)b - (char *)bufp - 1));
659 return 0;
660}
661
662/*
663 * Read and modify the summary information for a given extent size,
664 * bitmap block combination.
665 * Keeps track of a current summary block, so we don't keep reading
666 * it from the buffer cache.
667 */
668STATIC int /* error */
669xfs_rtmodify_summary(
670 xfs_mount_t *mp, /* file system mount point */
671 xfs_trans_t *tp, /* transaction pointer */
672 int log, /* log2 of extent size */
673 xfs_rtblock_t bbno, /* bitmap block number */
674 int delta, /* change to make to summary info */
e49e365f 675 xfs_buf_t **rbpp, /* in/out: summary block buffer */
2bd0ea18
NS
676 xfs_fsblock_t *rsb) /* in/out: summary block number */
677{
e49e365f 678 xfs_buf_t *bp; /* buffer for the summary block */
2bd0ea18
NS
679 int error; /* error value */
680 xfs_fsblock_t sb; /* summary fsblock */
681 int so; /* index into the summary file */
682 xfs_suminfo_t *sp; /* pointer to returned data */
683
684 /*
685 * Compute entry number in the summary file.
686 */
687 so = XFS_SUMOFFS(mp, log, bbno);
688 /*
689 * Compute the block number in the summary file.
690 */
691 sb = XFS_SUMOFFSTOBLOCK(mp, so);
692 /*
693 * If we have an old buffer, and the block number matches, use that.
694 */
695 if (rbpp && *rbpp && *rsb == sb)
696 bp = *rbpp;
697 /*
698 * Otherwise we have to get the buffer.
699 */
700 else {
701 /*
702 * If there was an old one, get rid of it first.
703 */
704 if (rbpp && *rbpp)
705 xfs_trans_brelse(tp, *rbpp);
706 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
707 if (error) {
708 return error;
709 }
710 /*
711 * Remember this buffer and block for the next call.
712 */
713 if (rbpp) {
714 *rbpp = bp;
715 *rsb = sb;
716 }
717 }
718 /*
719 * Point to the summary information, modify and log it.
720 */
721 sp = XFS_SUMPTR(mp, bp, so);
722 *sp += delta;
723 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
724 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
725 return 0;
726}
727
728/*
5000d01d 729 * Free an extent in the realtime subvolume. Length is expressed in
2bd0ea18
NS
730 * realtime extents, as is the block number.
731 */
732int /* error */
733xfs_rtfree_extent(
734 xfs_trans_t *tp, /* transaction pointer */
735 xfs_rtblock_t bno, /* starting block number to free */
736 xfs_extlen_t len) /* length of extent freed */
737{
738 int error; /* error value */
739 xfs_inode_t *ip; /* bitmap file inode */
740 xfs_mount_t *mp; /* file system mount structure */
741 xfs_fsblock_t sb; /* summary file block number */
e49e365f 742 xfs_buf_t *sumbp; /* summary file block buffer */
2bd0ea18
NS
743
744 mp = tp->t_mountp;
745 /*
746 * Synchronize by locking the bitmap inode.
747 */
5e656dbb
BN
748 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
749 XFS_ILOCK_EXCL, &ip)))
2bd0ea18 750 return error;
2bd0ea18
NS
751#if defined(__KERNEL__) && defined(DEBUG)
752 /*
753 * Check to see that this whole range is currently allocated.
754 */
755 {
756 int stat; /* result from checking range */
757
758 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
759 if (error) {
760 return error;
761 }
762 ASSERT(stat);
763 }
764#endif
765 sumbp = NULL;
766 /*
767 * Free the range of realtime blocks.
768 */
769 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
770 if (error) {
771 return error;
772 }
773 /*
774 * Mark more blocks free in the superblock.
775 */
776 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
777 /*
778 * If we've now freed all the blocks, reset the file sequence
779 * number to 0.
780 */
781 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
782 mp->m_sb.sb_rextents) {
783 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
784 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
785 *(__uint64_t *)&ip->i_d.di_atime = 0;
786 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
787 }
788 return 0;
789}