| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 |
* All Rights Reserved. |
| 5 |
*/ |
| 6 |
#include "xfs_platform.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_bmap_btree.h" |
| 17 |
#include "xfs_trans_space.h" |
| 18 |
#include "xfs_trans.h" |
| 19 |
#include "xfs_rtalloc.h" |
| 20 |
#include "xfs_error.h" |
| 21 |
#include "xfs_rtbitmap.h" |
| 22 |
#include "xfs_health.h" |
| 23 |
#include "xfs_sb.h" |
| 24 |
#include "xfs_errortag.h" |
| 25 |
#include "xfs_log.h" |
| 26 |
#include "xfs_buf_item.h" |
| 27 |
#include "xfs_extent_busy.h" |
| 28 |
|
| 29 |
/* |
| 30 |
* Realtime allocator bitmap functions shared with userspace. |
| 31 |
*/ |
| 32 |
|
| 33 |
static xfs_failaddr_t |
| 34 |
xfs_rtbuf_verify( |
| 35 |
struct xfs_buf *bp) |
| 36 |
{ |
| 37 |
struct xfs_mount *mp = bp->b_mount; |
| 38 |
struct xfs_rtbuf_blkinfo *hdr = bp->b_addr; |
| 39 |
|
| 40 |
if (!xfs_verify_magic(bp, hdr->rt_magic)) |
| 41 |
return __this_address; |
| 42 |
if (!xfs_has_rtgroups(mp)) |
| 43 |
return __this_address; |
| 44 |
if (!xfs_has_crc(mp)) |
| 45 |
return __this_address; |
| 46 |
if (!uuid_equal(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid)) |
| 47 |
return __this_address; |
| 48 |
if (hdr->rt_blkno != cpu_to_be64(xfs_buf_daddr(bp))) |
| 49 |
return __this_address; |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
xfs_rtbuf_verify_read( |
| 55 |
struct xfs_buf *bp) |
| 56 |
{ |
| 57 |
struct xfs_mount *mp = bp->b_mount; |
| 58 |
struct xfs_rtbuf_blkinfo *hdr = bp->b_addr; |
| 59 |
xfs_failaddr_t fa; |
| 60 |
|
| 61 |
if (!xfs_has_rtgroups(mp)) |
| 62 |
return; |
| 63 |
|
| 64 |
if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr->rt_lsn))) { |
| 65 |
fa = __this_address; |
| 66 |
goto fail; |
| 67 |
} |
| 68 |
|
| 69 |
if (!xfs_buf_verify_cksum(bp, XFS_RTBUF_CRC_OFF)) { |
| 70 |
fa = __this_address; |
| 71 |
goto fail; |
| 72 |
} |
| 73 |
|
| 74 |
fa = xfs_rtbuf_verify(bp); |
| 75 |
if (fa) |
| 76 |
goto fail; |
| 77 |
|
| 78 |
return; |
| 79 |
fail: |
| 80 |
xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
xfs_rtbuf_verify_write( |
| 85 |
struct xfs_buf *bp) |
| 86 |
{ |
| 87 |
struct xfs_mount *mp = bp->b_mount; |
| 88 |
struct xfs_rtbuf_blkinfo *hdr = bp->b_addr; |
| 89 |
struct xfs_buf_log_item *bip = bp->b_log_item; |
| 90 |
xfs_failaddr_t fa; |
| 91 |
|
| 92 |
if (!xfs_has_rtgroups(mp)) |
| 93 |
return; |
| 94 |
|
| 95 |
fa = xfs_rtbuf_verify(bp); |
| 96 |
if (fa) { |
| 97 |
xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if (bip) |
| 102 |
hdr->rt_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 103 |
xfs_buf_update_cksum(bp, XFS_RTBUF_CRC_OFF); |
| 104 |
} |
| 105 |
|
| 106 |
const struct xfs_buf_ops xfs_rtbuf_ops = { |
| 107 |
.name = "rtbuf", |
| 108 |
.verify_read = xfs_rtbuf_verify_read, |
| 109 |
.verify_write = xfs_rtbuf_verify_write, |
| 110 |
}; |
| 111 |
|
| 112 |
const struct xfs_buf_ops xfs_rtbitmap_buf_ops = { |
| 113 |
.name = "xfs_rtbitmap", |
| 114 |
.magic = { 0, cpu_to_be32(XFS_RTBITMAP_MAGIC) }, |
| 115 |
.verify_read = xfs_rtbuf_verify_read, |
| 116 |
.verify_write = xfs_rtbuf_verify_write, |
| 117 |
.verify_struct = xfs_rtbuf_verify, |
| 118 |
}; |
| 119 |
|
| 120 |
const struct xfs_buf_ops xfs_rtsummary_buf_ops = { |
| 121 |
.name = "xfs_rtsummary", |
| 122 |
.magic = { 0, cpu_to_be32(XFS_RTSUMMARY_MAGIC) }, |
| 123 |
.verify_read = xfs_rtbuf_verify_read, |
| 124 |
.verify_write = xfs_rtbuf_verify_write, |
| 125 |
.verify_struct = xfs_rtbuf_verify, |
| 126 |
}; |
| 127 |
|
| 128 |
/* Release cached rt bitmap and summary buffers. */ |
| 129 |
void |
| 130 |
xfs_rtbuf_cache_relse( |
| 131 |
struct xfs_rtalloc_args *args) |
| 132 |
{ |
| 133 |
if (args->rbmbp) { |
| 134 |
xfs_trans_brelse(args->tp, args->rbmbp); |
| 135 |
args->rbmbp = NULL; |
| 136 |
args->rbmoff = NULLFILEOFF; |
| 137 |
} |
| 138 |
if (args->sumbp) { |
| 139 |
xfs_trans_brelse(args->tp, args->sumbp); |
| 140 |
args->sumbp = NULL; |
| 141 |
args->sumoff = NULLFILEOFF; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Get a buffer for the bitmap or summary file block specified. |
| 147 |
* The buffer is returned read and locked. |
| 148 |
*/ |
| 149 |
static int |
| 150 |
xfs_rtbuf_get( |
| 151 |
struct xfs_rtalloc_args *args, |
| 152 |
xfs_fileoff_t block, /* block number in bitmap or summary */ |
| 153 |
enum xfs_rtg_inodes type) |
| 154 |
{ |
| 155 |
struct xfs_inode *ip = args->rtg->rtg_inodes[type]; |
| 156 |
struct xfs_mount *mp = args->mp; |
| 157 |
struct xfs_buf **cbpp; /* cached block buffer */ |
| 158 |
xfs_fileoff_t *coffp; /* cached block number */ |
| 159 |
struct xfs_buf *bp; /* block buffer, result */ |
| 160 |
struct xfs_bmbt_irec map; |
| 161 |
enum xfs_blft buf_type; |
| 162 |
int nmap = 1; |
| 163 |
int error; |
| 164 |
|
| 165 |
switch (type) { |
| 166 |
case XFS_RTGI_SUMMARY: |
| 167 |
cbpp = &args->sumbp; |
| 168 |
coffp = &args->sumoff; |
| 169 |
buf_type = XFS_BLFT_RTSUMMARY_BUF; |
| 170 |
break; |
| 171 |
case XFS_RTGI_BITMAP: |
| 172 |
cbpp = &args->rbmbp; |
| 173 |
coffp = &args->rbmoff; |
| 174 |
buf_type = XFS_BLFT_RTBITMAP_BUF; |
| 175 |
break; |
| 176 |
default: |
| 177 |
return -EINVAL; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* If we have a cached buffer, and the block number matches, use that. |
| 182 |
*/ |
| 183 |
if (*cbpp && *coffp == block) |
| 184 |
return 0; |
| 185 |
|
| 186 |
/* |
| 187 |
* Otherwise we have to have to get the buffer. If there was an old |
| 188 |
* one, get rid of it first. |
| 189 |
*/ |
| 190 |
if (*cbpp) { |
| 191 |
xfs_trans_brelse(args->tp, *cbpp); |
| 192 |
*cbpp = NULL; |
| 193 |
} |
| 194 |
|
| 195 |
error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0); |
| 196 |
if (error) |
| 197 |
return error; |
| 198 |
|
| 199 |
if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map))) { |
| 200 |
xfs_rtginode_mark_sick(args->rtg, type); |
| 201 |
return -EFSCORRUPTED; |
| 202 |
} |
| 203 |
|
| 204 |
ASSERT(map.br_startblock != NULLFSBLOCK); |
| 205 |
error = xfs_trans_read_buf(mp, args->tp, mp->m_ddev_targp, |
| 206 |
XFS_FSB_TO_DADDR(mp, map.br_startblock), |
| 207 |
mp->m_bsize, 0, &bp, |
| 208 |
xfs_rtblock_ops(mp, type)); |
| 209 |
if (xfs_metadata_is_sick(error)) |
| 210 |
xfs_rtginode_mark_sick(args->rtg, type); |
| 211 |
if (error) |
| 212 |
return error; |
| 213 |
|
| 214 |
if (xfs_has_rtgroups(mp)) { |
| 215 |
struct xfs_rtbuf_blkinfo *hdr = bp->b_addr; |
| 216 |
|
| 217 |
if (hdr->rt_owner != cpu_to_be64(ip->i_ino)) { |
| 218 |
xfs_buf_mark_corrupt(bp); |
| 219 |
xfs_trans_brelse(args->tp, bp); |
| 220 |
xfs_rtginode_mark_sick(args->rtg, type); |
| 221 |
return -EFSCORRUPTED; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
xfs_trans_buf_set_type(args->tp, bp, buf_type); |
| 226 |
*cbpp = bp; |
| 227 |
*coffp = block; |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
int |
| 232 |
xfs_rtbitmap_read_buf( |
| 233 |
struct xfs_rtalloc_args *args, |
| 234 |
xfs_fileoff_t block) |
| 235 |
{ |
| 236 |
struct xfs_mount *mp = args->mp; |
| 237 |
|
| 238 |
if (XFS_IS_CORRUPT(mp, block >= mp->m_sb.sb_rbmblocks)) { |
| 239 |
xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_BITMAP); |
| 240 |
return -EFSCORRUPTED; |
| 241 |
} |
| 242 |
|
| 243 |
return xfs_rtbuf_get(args, block, XFS_RTGI_BITMAP); |
| 244 |
} |
| 245 |
|
| 246 |
int |
| 247 |
xfs_rtsummary_read_buf( |
| 248 |
struct xfs_rtalloc_args *args, |
| 249 |
xfs_fileoff_t block) |
| 250 |
{ |
| 251 |
struct xfs_mount *mp = args->mp; |
| 252 |
|
| 253 |
if (XFS_IS_CORRUPT(mp, block >= mp->m_rsumblocks)) { |
| 254 |
xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_SUMMARY); |
| 255 |
return -EFSCORRUPTED; |
| 256 |
} |
| 257 |
return xfs_rtbuf_get(args, block, XFS_RTGI_SUMMARY); |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* Searching backward from start find the first block whose allocated/free state |
| 262 |
* is different from start's. |
| 263 |
*/ |
| 264 |
int |
| 265 |
xfs_rtfind_back( |
| 266 |
struct xfs_rtalloc_args *args, |
| 267 |
xfs_rtxnum_t start, /* starting rtext to look at */ |
| 268 |
xfs_rtxnum_t *rtx) /* out: start rtext found */ |
| 269 |
{ |
| 270 |
struct xfs_mount *mp = args->mp; |
| 271 |
int bit; /* bit number in the word */ |
| 272 |
xfs_fileoff_t block; /* bitmap block number */ |
| 273 |
int error; /* error value */ |
| 274 |
xfs_rtxnum_t firstbit; /* first useful bit in the word */ |
| 275 |
xfs_rtxnum_t i; /* current bit number rel. to start */ |
| 276 |
xfs_rtxnum_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 |
xfs_rtword_t incore; |
| 281 |
unsigned int word; /* word number in the buffer */ |
| 282 |
|
| 283 |
/* |
| 284 |
* Compute and read in starting bitmap block for starting block. |
| 285 |
*/ |
| 286 |
block = xfs_rtx_to_rbmblock(mp, start); |
| 287 |
error = xfs_rtbitmap_read_buf(args, block); |
| 288 |
if (error) |
| 289 |
return error; |
| 290 |
|
| 291 |
/* |
| 292 |
* Get the first word's index & point to it. |
| 293 |
*/ |
| 294 |
word = xfs_rtx_to_rbmword(mp, start); |
| 295 |
bit = (int)(start & (XFS_NBWORD - 1)); |
| 296 |
len = start + 1; |
| 297 |
/* |
| 298 |
* Compute match value, based on the bit at start: if 1 (free) |
| 299 |
* then all-ones, else all-zeroes. |
| 300 |
*/ |
| 301 |
incore = xfs_rtbitmap_getword(args, word); |
| 302 |
want = (incore & ((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 < XFS_NBWORD - 1) { |
| 308 |
/* |
| 309 |
* Calculate first (leftmost) bit number to look at, |
| 310 |
* and mask for all the relevant bits in this word. |
| 311 |
*/ |
| 312 |
firstbit = max_t(xfs_srtblock_t, bit - len + 1, 0); |
| 313 |
mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) << |
| 314 |
firstbit; |
| 315 |
/* |
| 316 |
* Calculate the difference between the value there |
| 317 |
* and what we're looking for. |
| 318 |
*/ |
| 319 |
if ((wdiff = (incore ^ want) & mask)) { |
| 320 |
/* |
| 321 |
* Different. Mark where we are and return. |
| 322 |
*/ |
| 323 |
i = bit - xfs_highbit32(wdiff); |
| 324 |
*rtx = start - i + 1; |
| 325 |
return 0; |
| 326 |
} |
| 327 |
i = bit - firstbit + 1; |
| 328 |
/* |
| 329 |
* Go on to previous block if that's where the previous word is |
| 330 |
* and we need the previous word. |
| 331 |
*/ |
| 332 |
if (--word == -1 && i < len) { |
| 333 |
/* |
| 334 |
* If done with this block, get the previous one. |
| 335 |
*/ |
| 336 |
error = xfs_rtbitmap_read_buf(args, --block); |
| 337 |
if (error) |
| 338 |
return error; |
| 339 |
|
| 340 |
word = mp->m_blockwsize - 1; |
| 341 |
} |
| 342 |
} else { |
| 343 |
/* |
| 344 |
* Starting on a word boundary, no partial word. |
| 345 |
*/ |
| 346 |
i = 0; |
| 347 |
} |
| 348 |
/* |
| 349 |
* Loop over whole words in buffers. When we use up one buffer |
| 350 |
* we move on to the previous one. |
| 351 |
*/ |
| 352 |
while (len - i >= XFS_NBWORD) { |
| 353 |
/* |
| 354 |
* Compute difference between actual and desired value. |
| 355 |
*/ |
| 356 |
incore = xfs_rtbitmap_getword(args, word); |
| 357 |
if ((wdiff = incore ^ want)) { |
| 358 |
/* |
| 359 |
* Different, mark where we are and return. |
| 360 |
*/ |
| 361 |
i += XFS_NBWORD - 1 - xfs_highbit32(wdiff); |
| 362 |
*rtx = start - i + 1; |
| 363 |
return 0; |
| 364 |
} |
| 365 |
i += XFS_NBWORD; |
| 366 |
/* |
| 367 |
* Go on to previous block if that's where the previous word is |
| 368 |
* and we need the previous word. |
| 369 |
*/ |
| 370 |
if (--word == -1 && i < len) { |
| 371 |
/* |
| 372 |
* If done with this block, get the previous one. |
| 373 |
*/ |
| 374 |
error = xfs_rtbitmap_read_buf(args, --block); |
| 375 |
if (error) |
| 376 |
return error; |
| 377 |
|
| 378 |
word = mp->m_blockwsize - 1; |
| 379 |
} |
| 380 |
} |
| 381 |
/* |
| 382 |
* If not ending on a word boundary, deal with the last |
| 383 |
* (partial) word. |
| 384 |
*/ |
| 385 |
if (len - i) { |
| 386 |
/* |
| 387 |
* Calculate first (leftmost) bit number to look at, |
| 388 |
* and mask for all the relevant bits in this word. |
| 389 |
*/ |
| 390 |
firstbit = XFS_NBWORD - (len - i); |
| 391 |
mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit; |
| 392 |
/* |
| 393 |
* Compute difference between actual and desired value. |
| 394 |
*/ |
| 395 |
incore = xfs_rtbitmap_getword(args, word); |
| 396 |
if ((wdiff = (incore ^ want) & mask)) { |
| 397 |
/* |
| 398 |
* Different, mark where we are and return. |
| 399 |
*/ |
| 400 |
i += XFS_NBWORD - 1 - xfs_highbit32(wdiff); |
| 401 |
*rtx = start - i + 1; |
| 402 |
return 0; |
| 403 |
} else |
| 404 |
i = len; |
| 405 |
} |
| 406 |
/* |
| 407 |
* No match, return that we scanned the whole area. |
| 408 |
*/ |
| 409 |
*rtx = start - i + 1; |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
* Searching forward from start to limit, find the first block whose |
| 415 |
* allocated/free state is different from start's. |
| 416 |
*/ |
| 417 |
int |
| 418 |
xfs_rtfind_forw( |
| 419 |
struct xfs_rtalloc_args *args, |
| 420 |
xfs_rtxnum_t start, /* starting rtext to look at */ |
| 421 |
xfs_rtxnum_t limit, /* last rtext to look at */ |
| 422 |
xfs_rtxnum_t *rtx) /* out: start rtext found */ |
| 423 |
{ |
| 424 |
struct xfs_mount *mp = args->mp; |
| 425 |
int bit; /* bit number in the word */ |
| 426 |
xfs_fileoff_t block; /* bitmap block number */ |
| 427 |
int error; |
| 428 |
xfs_rtxnum_t i; /* current bit number rel. to start */ |
| 429 |
xfs_rtxnum_t lastbit;/* last useful bit in the word */ |
| 430 |
xfs_rtxnum_t len; /* length of inspected area */ |
| 431 |
xfs_rtword_t mask; /* mask of relevant bits for value */ |
| 432 |
xfs_rtword_t want; /* mask for "good" values */ |
| 433 |
xfs_rtword_t wdiff; /* difference from wanted value */ |
| 434 |
xfs_rtword_t incore; |
| 435 |
unsigned int word; /* word number in the buffer */ |
| 436 |
|
| 437 |
ASSERT(start <= limit); |
| 438 |
|
| 439 |
/* |
| 440 |
* Compute and read in starting bitmap block for starting block. |
| 441 |
*/ |
| 442 |
block = xfs_rtx_to_rbmblock(mp, start); |
| 443 |
error = xfs_rtbitmap_read_buf(args, block); |
| 444 |
if (error) |
| 445 |
return error; |
| 446 |
|
| 447 |
/* |
| 448 |
* Get the first word's index & point to it. |
| 449 |
*/ |
| 450 |
word = xfs_rtx_to_rbmword(mp, start); |
| 451 |
bit = (int)(start & (XFS_NBWORD - 1)); |
| 452 |
len = limit - start + 1; |
| 453 |
/* |
| 454 |
* Compute match value, based on the bit at start: if 1 (free) |
| 455 |
* then all-ones, else all-zeroes. |
| 456 |
*/ |
| 457 |
incore = xfs_rtbitmap_getword(args, word); |
| 458 |
want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0; |
| 459 |
/* |
| 460 |
* If the starting position is not word-aligned, deal with the |
| 461 |
* partial word. |
| 462 |
*/ |
| 463 |
if (bit) { |
| 464 |
/* |
| 465 |
* Calculate last (rightmost) bit number to look at, |
| 466 |
* and mask for all the relevant bits in this word. |
| 467 |
*/ |
| 468 |
lastbit = min(bit + len, XFS_NBWORD); |
| 469 |
mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit; |
| 470 |
/* |
| 471 |
* Calculate the difference between the value there |
| 472 |
* and what we're looking for. |
| 473 |
*/ |
| 474 |
if ((wdiff = (incore ^ want) & mask)) { |
| 475 |
/* |
| 476 |
* Different. Mark where we are and return. |
| 477 |
*/ |
| 478 |
i = xfs_lowbit32(wdiff) - bit; |
| 479 |
*rtx = start + i - 1; |
| 480 |
return 0; |
| 481 |
} |
| 482 |
i = lastbit - bit; |
| 483 |
/* |
| 484 |
* Go on to next block if that's where the next word is |
| 485 |
* and we need the next word. |
| 486 |
*/ |
| 487 |
if (++word == mp->m_blockwsize && i < len) { |
| 488 |
/* |
| 489 |
* If done with this block, get the previous one. |
| 490 |
*/ |
| 491 |
error = xfs_rtbitmap_read_buf(args, ++block); |
| 492 |
if (error) |
| 493 |
return error; |
| 494 |
|
| 495 |
word = 0; |
| 496 |
} |
| 497 |
} else { |
| 498 |
/* |
| 499 |
* Starting on a word boundary, no partial word. |
| 500 |
*/ |
| 501 |
i = 0; |
| 502 |
} |
| 503 |
/* |
| 504 |
* Loop over whole words in buffers. When we use up one buffer |
| 505 |
* we move on to the next one. |
| 506 |
*/ |
| 507 |
while (len - i >= XFS_NBWORD) { |
| 508 |
/* |
| 509 |
* Compute difference between actual and desired value. |
| 510 |
*/ |
| 511 |
incore = xfs_rtbitmap_getword(args, word); |
| 512 |
if ((wdiff = incore ^ want)) { |
| 513 |
/* |
| 514 |
* Different, mark where we are and return. |
| 515 |
*/ |
| 516 |
i += xfs_lowbit32(wdiff); |
| 517 |
*rtx = start + i - 1; |
| 518 |
return 0; |
| 519 |
} |
| 520 |
i += XFS_NBWORD; |
| 521 |
/* |
| 522 |
* Go on to next block if that's where the next word is |
| 523 |
* and we need the next word. |
| 524 |
*/ |
| 525 |
if (++word == mp->m_blockwsize && i < len) { |
| 526 |
/* |
| 527 |
* If done with this block, get the next one. |
| 528 |
*/ |
| 529 |
error = xfs_rtbitmap_read_buf(args, ++block); |
| 530 |
if (error) |
| 531 |
return error; |
| 532 |
|
| 533 |
word = 0; |
| 534 |
} |
| 535 |
} |
| 536 |
/* |
| 537 |
* If not ending on a word boundary, deal with the last |
| 538 |
* (partial) word. |
| 539 |
*/ |
| 540 |
if ((lastbit = len - i)) { |
| 541 |
/* |
| 542 |
* Calculate mask for all the relevant bits in this word. |
| 543 |
*/ |
| 544 |
mask = ((xfs_rtword_t)1 << lastbit) - 1; |
| 545 |
/* |
| 546 |
* Compute difference between actual and desired value. |
| 547 |
*/ |
| 548 |
incore = xfs_rtbitmap_getword(args, word); |
| 549 |
if ((wdiff = (incore ^ want) & mask)) { |
| 550 |
/* |
| 551 |
* Different, mark where we are and return. |
| 552 |
*/ |
| 553 |
i += xfs_lowbit32(wdiff); |
| 554 |
*rtx = start + i - 1; |
| 555 |
return 0; |
| 556 |
} else |
| 557 |
i = len; |
| 558 |
} |
| 559 |
/* |
| 560 |
* No match, return that we scanned the whole area. |
| 561 |
*/ |
| 562 |
*rtx = start + i - 1; |
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
/* Log rtsummary counter at @infoword. */ |
| 567 |
static inline void |
| 568 |
xfs_trans_log_rtsummary( |
| 569 |
struct xfs_rtalloc_args *args, |
| 570 |
unsigned int infoword) |
| 571 |
{ |
| 572 |
struct xfs_buf *bp = args->sumbp; |
| 573 |
size_t first, last; |
| 574 |
|
| 575 |
first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr; |
| 576 |
last = first + sizeof(xfs_suminfo_t) - 1; |
| 577 |
|
| 578 |
xfs_trans_log_buf(args->tp, bp, first, last); |
| 579 |
} |
| 580 |
|
| 581 |
/* |
| 582 |
* Modify the summary information for a given extent size, bitmap block |
| 583 |
* combination. |
| 584 |
*/ |
| 585 |
int |
| 586 |
xfs_rtmodify_summary( |
| 587 |
struct xfs_rtalloc_args *args, |
| 588 |
int log, /* log2 of extent size */ |
| 589 |
xfs_fileoff_t bbno, /* bitmap block number */ |
| 590 |
int delta) /* in/out: summary block number */ |
| 591 |
{ |
| 592 |
struct xfs_mount *mp = args->mp; |
| 593 |
xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno); |
| 594 |
uint8_t *rsum_cache = args->rtg->rtg_rsum_cache; |
| 595 |
unsigned int infoword; |
| 596 |
xfs_suminfo_t val; |
| 597 |
int error; |
| 598 |
|
| 599 |
error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so)); |
| 600 |
if (error) |
| 601 |
return error; |
| 602 |
|
| 603 |
infoword = xfs_rtsumoffs_to_infoword(mp, so); |
| 604 |
val = xfs_suminfo_add(args, infoword, delta); |
| 605 |
|
| 606 |
if (rsum_cache) { |
| 607 |
if (val == 0 && log + 1 == rsum_cache[bbno]) |
| 608 |
rsum_cache[bbno] = log; |
| 609 |
if (val != 0 && log >= rsum_cache[bbno]) |
| 610 |
rsum_cache[bbno] = log + 1; |
| 611 |
} |
| 612 |
|
| 613 |
xfs_trans_log_rtsummary(args, infoword); |
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
/* |
| 618 |
* Read and return the summary information for a given extent size, bitmap block |
| 619 |
* combination. |
| 620 |
*/ |
| 621 |
int |
| 622 |
xfs_rtget_summary( |
| 623 |
struct xfs_rtalloc_args *args, |
| 624 |
int log, /* log2 of extent size */ |
| 625 |
xfs_fileoff_t bbno, /* bitmap block number */ |
| 626 |
xfs_suminfo_t *sum) /* out: summary info for this block */ |
| 627 |
{ |
| 628 |
struct xfs_mount *mp = args->mp; |
| 629 |
xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno); |
| 630 |
int error; |
| 631 |
|
| 632 |
error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so)); |
| 633 |
if (!error) |
| 634 |
*sum = xfs_suminfo_get(args, xfs_rtsumoffs_to_infoword(mp, so)); |
| 635 |
return error; |
| 636 |
} |
| 637 |
|
| 638 |
/* Log rtbitmap block from the word @from to the byte before @next. */ |
| 639 |
static inline void |
| 640 |
xfs_trans_log_rtbitmap( |
| 641 |
struct xfs_rtalloc_args *args, |
| 642 |
unsigned int from, |
| 643 |
unsigned int next) |
| 644 |
{ |
| 645 |
struct xfs_buf *bp = args->rbmbp; |
| 646 |
size_t first, last; |
| 647 |
|
| 648 |
first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr; |
| 649 |
last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr; |
| 650 |
|
| 651 |
xfs_trans_log_buf(args->tp, bp, first, last); |
| 652 |
} |
| 653 |
|
| 654 |
/* |
| 655 |
* Set the given range of bitmap bits to the given value. |
| 656 |
* Do whatever I/O and logging is required. |
| 657 |
*/ |
| 658 |
int |
| 659 |
xfs_rtmodify_range( |
| 660 |
struct xfs_rtalloc_args *args, |
| 661 |
xfs_rtxnum_t start, /* starting rtext to modify */ |
| 662 |
xfs_rtxlen_t len, /* length of extent to modify */ |
| 663 |
int val) /* 1 for free, 0 for allocated */ |
| 664 |
{ |
| 665 |
struct xfs_mount *mp = args->mp; |
| 666 |
int bit; /* bit number in the word */ |
| 667 |
xfs_fileoff_t block; /* bitmap block number */ |
| 668 |
int error; |
| 669 |
int i; /* current bit number rel. to start */ |
| 670 |
int lastbit; /* last useful bit in word */ |
| 671 |
xfs_rtword_t mask; /* mask of relevant bits for value */ |
| 672 |
xfs_rtword_t incore; |
| 673 |
unsigned int firstword; /* first word used in the buffer */ |
| 674 |
unsigned int word; /* word number in the buffer */ |
| 675 |
|
| 676 |
/* |
| 677 |
* Compute starting bitmap block number. |
| 678 |
*/ |
| 679 |
block = xfs_rtx_to_rbmblock(mp, start); |
| 680 |
/* |
| 681 |
* Read the bitmap block, and point to its data. |
| 682 |
*/ |
| 683 |
error = xfs_rtbitmap_read_buf(args, block); |
| 684 |
if (error) |
| 685 |
return error; |
| 686 |
|
| 687 |
/* |
| 688 |
* Compute the starting word's address, and starting bit. |
| 689 |
*/ |
| 690 |
firstword = word = xfs_rtx_to_rbmword(mp, start); |
| 691 |
bit = (int)(start & (XFS_NBWORD - 1)); |
| 692 |
/* |
| 693 |
* 0 (allocated) => all zeroes; 1 (free) => all ones. |
| 694 |
*/ |
| 695 |
val = -val; |
| 696 |
/* |
| 697 |
* If not starting on a word boundary, deal with the first |
| 698 |
* (partial) word. |
| 699 |
*/ |
| 700 |
if (bit) { |
| 701 |
/* |
| 702 |
* Compute first bit not changed and mask of relevant bits. |
| 703 |
*/ |
| 704 |
lastbit = min(bit + len, XFS_NBWORD); |
| 705 |
mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit; |
| 706 |
/* |
| 707 |
* Set/clear the active bits. |
| 708 |
*/ |
| 709 |
incore = xfs_rtbitmap_getword(args, word); |
| 710 |
if (val) |
| 711 |
incore |= mask; |
| 712 |
else |
| 713 |
incore &= ~mask; |
| 714 |
xfs_rtbitmap_setword(args, word, incore); |
| 715 |
i = lastbit - bit; |
| 716 |
/* |
| 717 |
* Go on to the next block if that's where the next word is |
| 718 |
* and we need the next word. |
| 719 |
*/ |
| 720 |
if (++word == mp->m_blockwsize && i < len) { |
| 721 |
/* |
| 722 |
* Log the changed part of this block. |
| 723 |
* Get the next one. |
| 724 |
*/ |
| 725 |
xfs_trans_log_rtbitmap(args, firstword, word); |
| 726 |
error = xfs_rtbitmap_read_buf(args, ++block); |
| 727 |
if (error) |
| 728 |
return error; |
| 729 |
|
| 730 |
firstword = word = 0; |
| 731 |
} |
| 732 |
} else { |
| 733 |
/* |
| 734 |
* Starting on a word boundary, no partial word. |
| 735 |
*/ |
| 736 |
i = 0; |
| 737 |
} |
| 738 |
/* |
| 739 |
* Loop over whole words in buffers. When we use up one buffer |
| 740 |
* we move on to the next one. |
| 741 |
*/ |
| 742 |
while (len - i >= XFS_NBWORD) { |
| 743 |
/* |
| 744 |
* Set the word value correctly. |
| 745 |
*/ |
| 746 |
xfs_rtbitmap_setword(args, word, val); |
| 747 |
i += XFS_NBWORD; |
| 748 |
/* |
| 749 |
* Go on to the next block if that's where the next word is |
| 750 |
* and we need the next word. |
| 751 |
*/ |
| 752 |
if (++word == mp->m_blockwsize && i < len) { |
| 753 |
/* |
| 754 |
* Log the changed part of this block. |
| 755 |
* Get the next one. |
| 756 |
*/ |
| 757 |
xfs_trans_log_rtbitmap(args, firstword, word); |
| 758 |
error = xfs_rtbitmap_read_buf(args, ++block); |
| 759 |
if (error) |
| 760 |
return error; |
| 761 |
|
| 762 |
firstword = word = 0; |
| 763 |
} |
| 764 |
} |
| 765 |
/* |
| 766 |
* If not ending on a word boundary, deal with the last |
| 767 |
* (partial) word. |
| 768 |
*/ |
| 769 |
if ((lastbit = len - i)) { |
| 770 |
/* |
| 771 |
* Compute a mask of relevant bits. |
| 772 |
*/ |
| 773 |
mask = ((xfs_rtword_t)1 << lastbit) - 1; |
| 774 |
/* |
| 775 |
* Set/clear the active bits. |
| 776 |
*/ |
| 777 |
incore = xfs_rtbitmap_getword(args, word); |
| 778 |
if (val) |
| 779 |
incore |= mask; |
| 780 |
else |
| 781 |
incore &= ~mask; |
| 782 |
xfs_rtbitmap_setword(args, word, incore); |
| 783 |
word++; |
| 784 |
} |
| 785 |
/* |
| 786 |
* Log any remaining changed bytes. |
| 787 |
*/ |
| 788 |
if (word > firstword) |
| 789 |
xfs_trans_log_rtbitmap(args, firstword, word); |
| 790 |
return 0; |
| 791 |
} |
| 792 |
|
| 793 |
/* |
| 794 |
* Mark an extent specified by start and len freed. |
| 795 |
* Updates all the summary information as well as the bitmap. |
| 796 |
*/ |
| 797 |
int |
| 798 |
xfs_rtfree_range( |
| 799 |
struct xfs_rtalloc_args *args, |
| 800 |
xfs_rtxnum_t start, /* starting rtext to free */ |
| 801 |
xfs_rtxlen_t len) /* in/out: summary block number */ |
| 802 |
{ |
| 803 |
struct xfs_mount *mp = args->mp; |
| 804 |
xfs_rtxnum_t end; /* end of the freed extent */ |
| 805 |
int error; /* error value */ |
| 806 |
xfs_rtxnum_t postblock; /* first rtext freed > end */ |
| 807 |
xfs_rtxnum_t preblock; /* first rtext freed < start */ |
| 808 |
|
| 809 |
end = start + len - 1; |
| 810 |
/* |
| 811 |
* Modify the bitmap to mark this extent freed. |
| 812 |
*/ |
| 813 |
error = xfs_rtmodify_range(args, start, len, 1); |
| 814 |
if (error) { |
| 815 |
return error; |
| 816 |
} |
| 817 |
/* |
| 818 |
* Assume we're freeing out of the middle of an allocated extent. |
| 819 |
* We need to find the beginning and end of the extent so we can |
| 820 |
* properly update the summary. |
| 821 |
*/ |
| 822 |
error = xfs_rtfind_back(args, start, &preblock); |
| 823 |
if (error) { |
| 824 |
return error; |
| 825 |
} |
| 826 |
/* |
| 827 |
* Find the next allocated block (end of allocated extent). |
| 828 |
*/ |
| 829 |
error = xfs_rtfind_forw(args, end, args->rtg->rtg_extents - 1, |
| 830 |
&postblock); |
| 831 |
if (error) |
| 832 |
return error; |
| 833 |
/* |
| 834 |
* If there are blocks not being freed at the front of the |
| 835 |
* old extent, add summary data for them to be allocated. |
| 836 |
*/ |
| 837 |
if (preblock < start) { |
| 838 |
error = xfs_rtmodify_summary(args, |
| 839 |
xfs_highbit64(start - preblock), |
| 840 |
xfs_rtx_to_rbmblock(mp, preblock), -1); |
| 841 |
if (error) { |
| 842 |
return error; |
| 843 |
} |
| 844 |
} |
| 845 |
/* |
| 846 |
* If there are blocks not being freed at the end of the |
| 847 |
* old extent, add summary data for them to be allocated. |
| 848 |
*/ |
| 849 |
if (postblock > end) { |
| 850 |
error = xfs_rtmodify_summary(args, |
| 851 |
xfs_highbit64(postblock - end), |
| 852 |
xfs_rtx_to_rbmblock(mp, end + 1), -1); |
| 853 |
if (error) { |
| 854 |
return error; |
| 855 |
} |
| 856 |
} |
| 857 |
/* |
| 858 |
* Increment the summary information corresponding to the entire |
| 859 |
* (new) free extent. |
| 860 |
*/ |
| 861 |
return xfs_rtmodify_summary(args, |
| 862 |
xfs_highbit64(postblock + 1 - preblock), |
| 863 |
xfs_rtx_to_rbmblock(mp, preblock), 1); |
| 864 |
} |
| 865 |
|
| 866 |
/* |
| 867 |
* Check that the given range is either all allocated (val = 0) or |
| 868 |
* all free (val = 1). |
| 869 |
*/ |
| 870 |
int |
| 871 |
xfs_rtcheck_range( |
| 872 |
struct xfs_rtalloc_args *args, |
| 873 |
xfs_rtxnum_t start, /* starting rtext number of extent */ |
| 874 |
xfs_rtxlen_t len, /* length of extent */ |
| 875 |
int val, /* 1 for free, 0 for allocated */ |
| 876 |
xfs_rtxnum_t *new, /* out: first rtext not matching */ |
| 877 |
int *stat) /* out: 1 for matches, 0 for not */ |
| 878 |
{ |
| 879 |
struct xfs_mount *mp = args->mp; |
| 880 |
int bit; /* bit number in the word */ |
| 881 |
xfs_fileoff_t block; /* bitmap block number */ |
| 882 |
int error; |
| 883 |
xfs_rtxnum_t i; /* current bit number rel. to start */ |
| 884 |
xfs_rtxnum_t lastbit; /* last useful bit in word */ |
| 885 |
xfs_rtword_t mask; /* mask of relevant bits for value */ |
| 886 |
xfs_rtword_t wdiff; /* difference from wanted value */ |
| 887 |
xfs_rtword_t incore; |
| 888 |
unsigned int word; /* word number in the buffer */ |
| 889 |
|
| 890 |
/* |
| 891 |
* Compute starting bitmap block number |
| 892 |
*/ |
| 893 |
block = xfs_rtx_to_rbmblock(mp, start); |
| 894 |
/* |
| 895 |
* Read the bitmap block. |
| 896 |
*/ |
| 897 |
error = xfs_rtbitmap_read_buf(args, block); |
| 898 |
if (error) |
| 899 |
return error; |
| 900 |
|
| 901 |
/* |
| 902 |
* Compute the starting word's address, and starting bit. |
| 903 |
*/ |
| 904 |
word = xfs_rtx_to_rbmword(mp, start); |
| 905 |
bit = (int)(start & (XFS_NBWORD - 1)); |
| 906 |
/* |
| 907 |
* 0 (allocated) => all zero's; 1 (free) => all one's. |
| 908 |
*/ |
| 909 |
val = -val; |
| 910 |
/* |
| 911 |
* If not starting on a word boundary, deal with the first |
| 912 |
* (partial) word. |
| 913 |
*/ |
| 914 |
if (bit) { |
| 915 |
/* |
| 916 |
* Compute first bit not examined. |
| 917 |
*/ |
| 918 |
lastbit = min(bit + len, XFS_NBWORD); |
| 919 |
/* |
| 920 |
* Mask of relevant bits. |
| 921 |
*/ |
| 922 |
mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit; |
| 923 |
/* |
| 924 |
* Compute difference between actual and desired value. |
| 925 |
*/ |
| 926 |
incore = xfs_rtbitmap_getword(args, word); |
| 927 |
if ((wdiff = (incore ^ val) & mask)) { |
| 928 |
/* |
| 929 |
* Different, compute first wrong bit and return. |
| 930 |
*/ |
| 931 |
i = xfs_lowbit32(wdiff) - bit; |
| 932 |
*new = start + i; |
| 933 |
*stat = 0; |
| 934 |
return 0; |
| 935 |
} |
| 936 |
i = lastbit - bit; |
| 937 |
/* |
| 938 |
* Go on to next block if that's where the next word is |
| 939 |
* and we need the next word. |
| 940 |
*/ |
| 941 |
if (++word == mp->m_blockwsize && i < len) { |
| 942 |
/* |
| 943 |
* If done with this block, get the next one. |
| 944 |
*/ |
| 945 |
error = xfs_rtbitmap_read_buf(args, ++block); |
| 946 |
if (error) |
| 947 |
return error; |
| 948 |
|
| 949 |
word = 0; |
| 950 |
} |
| 951 |
} else { |
| 952 |
/* |
| 953 |
* Starting on a word boundary, no partial word. |
| 954 |
*/ |
| 955 |
i = 0; |
| 956 |
} |
| 957 |
/* |
| 958 |
* Loop over whole words in buffers. When we use up one buffer |
| 959 |
* we move on to the next one. |
| 960 |
*/ |
| 961 |
while (len - i >= XFS_NBWORD) { |
| 962 |
/* |
| 963 |
* Compute difference between actual and desired value. |
| 964 |
*/ |
| 965 |
incore = xfs_rtbitmap_getword(args, word); |
| 966 |
if ((wdiff = incore ^ val)) { |
| 967 |
/* |
| 968 |
* Different, compute first wrong bit and return. |
| 969 |
*/ |
| 970 |
i += xfs_lowbit32(wdiff); |
| 971 |
*new = start + i; |
| 972 |
*stat = 0; |
| 973 |
return 0; |
| 974 |
} |
| 975 |
i += XFS_NBWORD; |
| 976 |
/* |
| 977 |
* Go on to next block if that's where the next word is |
| 978 |
* and we need the next word. |
| 979 |
*/ |
| 980 |
if (++word == mp->m_blockwsize && i < len) { |
| 981 |
/* |
| 982 |
* If done with this block, get the next one. |
| 983 |
*/ |
| 984 |
error = xfs_rtbitmap_read_buf(args, ++block); |
| 985 |
if (error) |
| 986 |
return error; |
| 987 |
|
| 988 |
word = 0; |
| 989 |
} |
| 990 |
} |
| 991 |
/* |
| 992 |
* If not ending on a word boundary, deal with the last |
| 993 |
* (partial) word. |
| 994 |
*/ |
| 995 |
if ((lastbit = len - i)) { |
| 996 |
/* |
| 997 |
* Mask of relevant bits. |
| 998 |
*/ |
| 999 |
mask = ((xfs_rtword_t)1 << lastbit) - 1; |
| 1000 |
/* |
| 1001 |
* Compute difference between actual and desired value. |
| 1002 |
*/ |
| 1003 |
incore = xfs_rtbitmap_getword(args, word); |
| 1004 |
if ((wdiff = (incore ^ val) & mask)) { |
| 1005 |
/* |
| 1006 |
* Different, compute first wrong bit and return. |
| 1007 |
*/ |
| 1008 |
i += xfs_lowbit32(wdiff); |
| 1009 |
*new = start + i; |
| 1010 |
*stat = 0; |
| 1011 |
return 0; |
| 1012 |
} else |
| 1013 |
i = len; |
| 1014 |
} |
| 1015 |
/* |
| 1016 |
* Successful, return. |
| 1017 |
*/ |
| 1018 |
*new = start + i; |
| 1019 |
*stat = 1; |
| 1020 |
return 0; |
| 1021 |
} |
| 1022 |
|
| 1023 |
#ifdef DEBUG |
| 1024 |
/* |
| 1025 |
* Check that the given extent (block range) is allocated already. |
| 1026 |
*/ |
| 1027 |
STATIC int |
| 1028 |
xfs_rtcheck_alloc_range( |
| 1029 |
struct xfs_rtalloc_args *args, |
| 1030 |
xfs_rtxnum_t start, /* starting rtext number of extent */ |
| 1031 |
xfs_rtxlen_t len) /* length of extent */ |
| 1032 |
{ |
| 1033 |
xfs_rtxnum_t new; /* dummy for xfs_rtcheck_range */ |
| 1034 |
int stat; |
| 1035 |
int error; |
| 1036 |
|
| 1037 |
error = xfs_rtcheck_range(args, start, len, 0, &new, &stat); |
| 1038 |
if (error) |
| 1039 |
return error; |
| 1040 |
ASSERT(stat); |
| 1041 |
return 0; |
| 1042 |
} |
| 1043 |
#else |
| 1044 |
#define xfs_rtcheck_alloc_range(a,b,l) (0) |
| 1045 |
#endif |
| 1046 |
/* |
| 1047 |
* Free an extent in the realtime subvolume. Length is expressed in |
| 1048 |
* realtime extents, as is the block number. |
| 1049 |
*/ |
| 1050 |
int |
| 1051 |
xfs_rtfree_extent( |
| 1052 |
struct xfs_trans *tp, /* transaction pointer */ |
| 1053 |
struct xfs_rtgroup *rtg, |
| 1054 |
xfs_rtxnum_t start, /* starting rtext number to free */ |
| 1055 |
xfs_rtxlen_t len) /* length of extent freed */ |
| 1056 |
{ |
| 1057 |
struct xfs_mount *mp = tp->t_mountp; |
| 1058 |
struct xfs_inode *rbmip = rtg_bitmap(rtg); |
| 1059 |
struct xfs_rtalloc_args args = { |
| 1060 |
.mp = mp, |
| 1061 |
.tp = tp, |
| 1062 |
.rtg = rtg, |
| 1063 |
}; |
| 1064 |
int error; |
| 1065 |
struct timespec64 atime; |
| 1066 |
|
| 1067 |
ASSERT(rbmip->i_itemp != NULL); |
| 1068 |
xfs_assert_ilocked(rbmip, XFS_ILOCK_EXCL); |
| 1069 |
|
| 1070 |
if (XFS_TEST_ERROR(mp, XFS_ERRTAG_FREE_EXTENT)) |
| 1071 |
return -EIO; |
| 1072 |
|
| 1073 |
error = xfs_rtcheck_alloc_range(&args, start, len); |
| 1074 |
if (error) |
| 1075 |
return error; |
| 1076 |
|
| 1077 |
/* |
| 1078 |
* Free the range of realtime blocks. |
| 1079 |
*/ |
| 1080 |
error = xfs_rtfree_range(&args, start, len); |
| 1081 |
if (error) |
| 1082 |
goto out; |
| 1083 |
|
| 1084 |
/* |
| 1085 |
* Mark more blocks free in the superblock. |
| 1086 |
*/ |
| 1087 |
xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len); |
| 1088 |
|
| 1089 |
/* |
| 1090 |
* If we've now freed all the blocks, reset the file sequence |
| 1091 |
* number to 0 for pre-RTG file systems. |
| 1092 |
*/ |
| 1093 |
if (!xfs_has_rtgroups(mp) && |
| 1094 |
tp->t_frextents_delta + mp->m_sb.sb_frextents == |
| 1095 |
mp->m_sb.sb_rextents) { |
| 1096 |
if (!(rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) |
| 1097 |
rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM; |
| 1098 |
|
| 1099 |
atime = inode_get_atime(VFS_I(rbmip)); |
| 1100 |
atime.tv_sec = 0; |
| 1101 |
inode_set_atime_to_ts(VFS_I(rbmip), atime); |
| 1102 |
xfs_trans_log_inode(tp, rbmip, XFS_ILOG_CORE); |
| 1103 |
} |
| 1104 |
error = 0; |
| 1105 |
out: |
| 1106 |
xfs_rtbuf_cache_relse(&args); |
| 1107 |
return error; |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* |
| 1111 |
* Free some blocks in the realtime subvolume. rtbno and rtlen are in units of |
| 1112 |
* rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen |
| 1113 |
* cannot exceed XFS_MAX_BMBT_EXTLEN. |
| 1114 |
*/ |
| 1115 |
int |
| 1116 |
xfs_rtfree_blocks( |
| 1117 |
struct xfs_trans *tp, |
| 1118 |
struct xfs_rtgroup *rtg, |
| 1119 |
xfs_fsblock_t rtbno, |
| 1120 |
xfs_filblks_t rtlen) |
| 1121 |
{ |
| 1122 |
struct xfs_mount *mp = tp->t_mountp; |
| 1123 |
xfs_extlen_t mod; |
| 1124 |
int error; |
| 1125 |
|
| 1126 |
ASSERT(!xfs_has_zoned(mp)); |
| 1127 |
ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN); |
| 1128 |
|
| 1129 |
mod = xfs_blen_to_rtxoff(mp, rtlen); |
| 1130 |
if (mod) { |
| 1131 |
ASSERT(mod == 0); |
| 1132 |
return -EIO; |
| 1133 |
} |
| 1134 |
|
| 1135 |
mod = xfs_rtb_to_rtxoff(mp, rtbno); |
| 1136 |
if (mod) { |
| 1137 |
ASSERT(mod == 0); |
| 1138 |
return -EIO; |
| 1139 |
} |
| 1140 |
|
| 1141 |
error = xfs_rtfree_extent(tp, rtg, xfs_rtb_to_rtx(mp, rtbno), |
| 1142 |
xfs_extlen_to_rtxlen(mp, rtlen)); |
| 1143 |
if (error) |
| 1144 |
return error; |
| 1145 |
|
| 1146 |
if (xfs_has_rtgroups(mp)) |
| 1147 |
xfs_extent_busy_insert(tp, rtg_group(rtg), |
| 1148 |
xfs_rtb_to_rgbno(mp, rtbno), rtlen, 0); |
| 1149 |
|
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* Find all the free records within a given range. */ |
| 1154 |
int |
| 1155 |
xfs_rtalloc_query_range( |
| 1156 |
struct xfs_rtgroup *rtg, |
| 1157 |
struct xfs_trans *tp, |
| 1158 |
xfs_rtxnum_t start, |
| 1159 |
xfs_rtxnum_t end, |
| 1160 |
xfs_rtalloc_query_range_fn fn, |
| 1161 |
void *priv) |
| 1162 |
{ |
| 1163 |
struct xfs_mount *mp = rtg_mount(rtg); |
| 1164 |
struct xfs_rtalloc_args args = { |
| 1165 |
.rtg = rtg, |
| 1166 |
.mp = mp, |
| 1167 |
.tp = tp, |
| 1168 |
}; |
| 1169 |
int error = 0; |
| 1170 |
|
| 1171 |
if (start > end) |
| 1172 |
return -EINVAL; |
| 1173 |
if (start == end || start >= rtg->rtg_extents) |
| 1174 |
return 0; |
| 1175 |
|
| 1176 |
end = min(end, rtg->rtg_extents - 1); |
| 1177 |
|
| 1178 |
if (xfs_has_zoned(mp)) |
| 1179 |
return -EINVAL; |
| 1180 |
|
| 1181 |
/* Iterate the bitmap, looking for discrepancies. */ |
| 1182 |
while (start <= end) { |
| 1183 |
struct xfs_rtalloc_rec rec; |
| 1184 |
int is_free; |
| 1185 |
xfs_rtxnum_t rtend; |
| 1186 |
|
| 1187 |
/* Is the first block free? */ |
| 1188 |
error = xfs_rtcheck_range(&args, start, 1, 1, &rtend, |
| 1189 |
&is_free); |
| 1190 |
if (error) |
| 1191 |
break; |
| 1192 |
|
| 1193 |
/* How long does the extent go for? */ |
| 1194 |
error = xfs_rtfind_forw(&args, start, end, &rtend); |
| 1195 |
if (error) |
| 1196 |
break; |
| 1197 |
|
| 1198 |
if (is_free) { |
| 1199 |
rec.ar_startext = start; |
| 1200 |
rec.ar_extcount = rtend - start + 1; |
| 1201 |
|
| 1202 |
error = fn(rtg, tp, &rec, priv); |
| 1203 |
if (error) |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
|
| 1207 |
start = rtend + 1; |
| 1208 |
} |
| 1209 |
|
| 1210 |
xfs_rtbuf_cache_relse(&args); |
| 1211 |
return error; |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* Find all the free records. */ |
| 1215 |
int |
| 1216 |
xfs_rtalloc_query_all( |
| 1217 |
struct xfs_rtgroup *rtg, |
| 1218 |
struct xfs_trans *tp, |
| 1219 |
xfs_rtalloc_query_range_fn fn, |
| 1220 |
void *priv) |
| 1221 |
{ |
| 1222 |
return xfs_rtalloc_query_range(rtg, tp, 0, rtg->rtg_extents - 1, fn, |
| 1223 |
priv); |
| 1224 |
} |
| 1225 |
|
| 1226 |
/* Is the given extent all free? */ |
| 1227 |
int |
| 1228 |
xfs_rtalloc_extent_is_free( |
| 1229 |
struct xfs_rtgroup *rtg, |
| 1230 |
struct xfs_trans *tp, |
| 1231 |
xfs_rtxnum_t start, |
| 1232 |
xfs_rtxlen_t len, |
| 1233 |
bool *is_free) |
| 1234 |
{ |
| 1235 |
struct xfs_rtalloc_args args = { |
| 1236 |
.mp = rtg_mount(rtg), |
| 1237 |
.rtg = rtg, |
| 1238 |
.tp = tp, |
| 1239 |
}; |
| 1240 |
xfs_rtxnum_t end; |
| 1241 |
int matches; |
| 1242 |
int error; |
| 1243 |
|
| 1244 |
error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches); |
| 1245 |
xfs_rtbuf_cache_relse(&args); |
| 1246 |
if (error) |
| 1247 |
return error; |
| 1248 |
|
| 1249 |
*is_free = matches; |
| 1250 |
return 0; |
| 1251 |
} |
| 1252 |
|
| 1253 |
/* Compute the number of rt extents tracked by a single bitmap block. */ |
| 1254 |
xfs_rtxnum_t |
| 1255 |
xfs_rtbitmap_rtx_per_rbmblock( |
| 1256 |
struct xfs_mount *mp) |
| 1257 |
{ |
| 1258 |
unsigned int rbmblock_bytes = mp->m_sb.sb_blocksize; |
| 1259 |
|
| 1260 |
if (xfs_has_rtgroups(mp)) |
| 1261 |
rbmblock_bytes -= sizeof(struct xfs_rtbuf_blkinfo); |
| 1262 |
|
| 1263 |
return rbmblock_bytes * NBBY; |
| 1264 |
} |
| 1265 |
|
| 1266 |
/* |
| 1267 |
* Compute the number of rtbitmap blocks needed to track the given number of rt |
| 1268 |
* extents. |
| 1269 |
*/ |
| 1270 |
xfs_filblks_t |
| 1271 |
xfs_rtbitmap_blockcount_len( |
| 1272 |
struct xfs_mount *mp, |
| 1273 |
xfs_rtbxlen_t rtextents) |
| 1274 |
{ |
| 1275 |
if (xfs_has_zoned(mp)) |
| 1276 |
return 0; |
| 1277 |
return howmany_64(rtextents, xfs_rtbitmap_rtx_per_rbmblock(mp)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
/* How many rt extents does each rtbitmap file track? */ |
| 1281 |
static inline xfs_rtbxlen_t |
| 1282 |
xfs_rtbitmap_bitcount( |
| 1283 |
struct xfs_mount *mp) |
| 1284 |
{ |
| 1285 |
if (!mp->m_sb.sb_rextents) |
| 1286 |
return 0; |
| 1287 |
|
| 1288 |
/* rtgroup size can be nonzero even if rextents is zero */ |
| 1289 |
if (xfs_has_rtgroups(mp)) |
| 1290 |
return mp->m_sb.sb_rgextents; |
| 1291 |
|
| 1292 |
return mp->m_sb.sb_rextents; |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* |
| 1296 |
* Compute the number of rtbitmap blocks used for a given file system. |
| 1297 |
*/ |
| 1298 |
xfs_filblks_t |
| 1299 |
xfs_rtbitmap_blockcount( |
| 1300 |
struct xfs_mount *mp) |
| 1301 |
{ |
| 1302 |
return xfs_rtbitmap_blockcount_len(mp, xfs_rtbitmap_bitcount(mp)); |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* |
| 1306 |
* Compute the geometry of the rtsummary file needed to track the given rt |
| 1307 |
* space. |
| 1308 |
*/ |
| 1309 |
xfs_filblks_t |
| 1310 |
xfs_rtsummary_blockcount( |
| 1311 |
struct xfs_mount *mp, |
| 1312 |
unsigned int *rsumlevels) |
| 1313 |
{ |
| 1314 |
xfs_rtbxlen_t rextents = xfs_rtbitmap_bitcount(mp); |
| 1315 |
unsigned long long rsumwords; |
| 1316 |
|
| 1317 |
if (xfs_has_zoned(mp)) { |
| 1318 |
*rsumlevels = 0; |
| 1319 |
return 0; |
| 1320 |
} |
| 1321 |
|
| 1322 |
*rsumlevels = xfs_compute_rextslog(rextents) + 1; |
| 1323 |
rsumwords = xfs_rtbitmap_blockcount_len(mp, rextents) * (*rsumlevels); |
| 1324 |
return howmany_64(rsumwords, mp->m_blockwsize); |
| 1325 |
} |
| 1326 |
|
| 1327 |
static int |
| 1328 |
xfs_rtfile_alloc_blocks( |
| 1329 |
struct xfs_inode *ip, |
| 1330 |
xfs_fileoff_t offset_fsb, |
| 1331 |
xfs_filblks_t count_fsb, |
| 1332 |
struct xfs_bmbt_irec *map) |
| 1333 |
{ |
| 1334 |
struct xfs_mount *mp = ip->i_mount; |
| 1335 |
struct xfs_trans *tp; |
| 1336 |
int nmap = 1; |
| 1337 |
int error; |
| 1338 |
|
| 1339 |
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, |
| 1340 |
XFS_GROWFSRT_SPACE_RES(mp, count_fsb), 0, 0, &tp); |
| 1341 |
if (error) |
| 1342 |
return error; |
| 1343 |
|
| 1344 |
xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1345 |
xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 1346 |
|
| 1347 |
error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, |
| 1348 |
XFS_IEXT_ADD_NOSPLIT_CNT); |
| 1349 |
if (error) |
| 1350 |
goto out_trans_cancel; |
| 1351 |
|
| 1352 |
error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, |
| 1353 |
XFS_BMAPI_METADATA, 0, map, &nmap); |
| 1354 |
if (error) |
| 1355 |
goto out_trans_cancel; |
| 1356 |
|
| 1357 |
return xfs_trans_commit(tp); |
| 1358 |
|
| 1359 |
out_trans_cancel: |
| 1360 |
xfs_trans_cancel(tp); |
| 1361 |
return error; |
| 1362 |
} |
| 1363 |
|
| 1364 |
/* Get a buffer for the block. */ |
| 1365 |
static int |
| 1366 |
xfs_rtfile_initialize_block( |
| 1367 |
struct xfs_rtgroup *rtg, |
| 1368 |
enum xfs_rtg_inodes type, |
| 1369 |
xfs_fsblock_t fsbno, |
| 1370 |
void *data) |
| 1371 |
{ |
| 1372 |
struct xfs_mount *mp = rtg_mount(rtg); |
| 1373 |
struct xfs_inode *ip = rtg->rtg_inodes[type]; |
| 1374 |
struct xfs_trans *tp; |
| 1375 |
struct xfs_buf *bp; |
| 1376 |
void *bufdata; |
| 1377 |
const size_t copylen = mp->m_blockwsize << XFS_WORDLOG; |
| 1378 |
enum xfs_blft buf_type; |
| 1379 |
int error; |
| 1380 |
|
| 1381 |
if (type == XFS_RTGI_BITMAP) |
| 1382 |
buf_type = XFS_BLFT_RTBITMAP_BUF; |
| 1383 |
else if (type == XFS_RTGI_SUMMARY) |
| 1384 |
buf_type = XFS_BLFT_RTSUMMARY_BUF; |
| 1385 |
else |
| 1386 |
return -EINVAL; |
| 1387 |
|
| 1388 |
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero, 0, 0, 0, &tp); |
| 1389 |
if (error) |
| 1390 |
return error; |
| 1391 |
xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1392 |
xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 1393 |
|
| 1394 |
error = xfs_trans_get_buf(tp, mp->m_ddev_targp, |
| 1395 |
XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp); |
| 1396 |
if (error) { |
| 1397 |
xfs_trans_cancel(tp); |
| 1398 |
return error; |
| 1399 |
} |
| 1400 |
bufdata = bp->b_addr; |
| 1401 |
|
| 1402 |
xfs_trans_buf_set_type(tp, bp, buf_type); |
| 1403 |
bp->b_ops = xfs_rtblock_ops(mp, type); |
| 1404 |
|
| 1405 |
if (xfs_has_rtgroups(mp)) { |
| 1406 |
struct xfs_rtbuf_blkinfo *hdr = bp->b_addr; |
| 1407 |
|
| 1408 |
if (type == XFS_RTGI_BITMAP) |
| 1409 |
hdr->rt_magic = cpu_to_be32(XFS_RTBITMAP_MAGIC); |
| 1410 |
else |
| 1411 |
hdr->rt_magic = cpu_to_be32(XFS_RTSUMMARY_MAGIC); |
| 1412 |
hdr->rt_owner = cpu_to_be64(ip->i_ino); |
| 1413 |
hdr->rt_blkno = cpu_to_be64(XFS_FSB_TO_DADDR(mp, fsbno)); |
| 1414 |
hdr->rt_lsn = 0; |
| 1415 |
uuid_copy(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid); |
| 1416 |
|
| 1417 |
bufdata += sizeof(*hdr); |
| 1418 |
} |
| 1419 |
|
| 1420 |
if (data) |
| 1421 |
memcpy(bufdata, data, copylen); |
| 1422 |
else |
| 1423 |
memset(bufdata, 0, copylen); |
| 1424 |
xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1); |
| 1425 |
return xfs_trans_commit(tp); |
| 1426 |
} |
| 1427 |
|
| 1428 |
/* |
| 1429 |
* Allocate space to the bitmap or summary file, and zero it, for growfs. |
| 1430 |
* @data must be a contiguous buffer large enough to fill all blocks in the |
| 1431 |
* file; or NULL to initialize the contents to zeroes. |
| 1432 |
*/ |
| 1433 |
int |
| 1434 |
xfs_rtfile_initialize_blocks( |
| 1435 |
struct xfs_rtgroup *rtg, |
| 1436 |
enum xfs_rtg_inodes type, |
| 1437 |
xfs_fileoff_t offset_fsb, /* offset to start from */ |
| 1438 |
xfs_fileoff_t end_fsb, /* offset to allocate to */ |
| 1439 |
void *data) /* data to fill the blocks */ |
| 1440 |
{ |
| 1441 |
struct xfs_mount *mp = rtg_mount(rtg); |
| 1442 |
const size_t copylen = mp->m_blockwsize << XFS_WORDLOG; |
| 1443 |
|
| 1444 |
while (offset_fsb < end_fsb) { |
| 1445 |
struct xfs_bmbt_irec map; |
| 1446 |
xfs_filblks_t i; |
| 1447 |
int error; |
| 1448 |
|
| 1449 |
error = xfs_rtfile_alloc_blocks(rtg->rtg_inodes[type], |
| 1450 |
offset_fsb, end_fsb - offset_fsb, &map); |
| 1451 |
if (error) |
| 1452 |
return error; |
| 1453 |
|
| 1454 |
/* |
| 1455 |
* Now we need to clear the allocated blocks. |
| 1456 |
* |
| 1457 |
* Do this one block per transaction, to keep it simple. |
| 1458 |
*/ |
| 1459 |
for (i = 0; i < map.br_blockcount; i++) { |
| 1460 |
error = xfs_rtfile_initialize_block(rtg, type, |
| 1461 |
map.br_startblock + i, data); |
| 1462 |
if (error) |
| 1463 |
return error; |
| 1464 |
if (data) |
| 1465 |
data += copylen; |
| 1466 |
} |
| 1467 |
|
| 1468 |
offset_fsb = map.br_startoff + map.br_blockcount; |
| 1469 |
} |
| 1470 |
|
| 1471 |
return 0; |
| 1472 |
} |
| 1473 |
|
| 1474 |
int |
| 1475 |
xfs_rtbitmap_create( |
| 1476 |
struct xfs_rtgroup *rtg, |
| 1477 |
struct xfs_inode *ip, |
| 1478 |
struct xfs_trans *tp, |
| 1479 |
bool init) |
| 1480 |
{ |
| 1481 |
struct xfs_mount *mp = rtg_mount(rtg); |
| 1482 |
|
| 1483 |
ip->i_disk_size = mp->m_sb.sb_rbmblocks * mp->m_sb.sb_blocksize; |
| 1484 |
if (init && !xfs_has_rtgroups(mp)) { |
| 1485 |
ip->i_diflags |= XFS_DIFLAG_NEWRTBM; |
| 1486 |
inode_set_atime(VFS_I(ip), 0, 0); |
| 1487 |
} |
| 1488 |
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1489 |
return 0; |
| 1490 |
} |
| 1491 |
|
| 1492 |
int |
| 1493 |
xfs_rtsummary_create( |
| 1494 |
struct xfs_rtgroup *rtg, |
| 1495 |
struct xfs_inode *ip, |
| 1496 |
struct xfs_trans *tp, |
| 1497 |
bool init) |
| 1498 |
{ |
| 1499 |
struct xfs_mount *mp = rtg_mount(rtg); |
| 1500 |
|
| 1501 |
ip->i_disk_size = mp->m_rsumblocks * mp->m_sb.sb_blocksize; |
| 1502 |
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1503 |
return 0; |
| 1504 |
} |