b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * balloc.c |
| 3 | * |
| 4 | * PURPOSE |
| 5 | * Block allocation handling routines for the OSTA-UDF(tm) filesystem. |
| 6 | * |
| 7 | * COPYRIGHT |
| 8 | * This file is distributed under the terms of the GNU General Public |
| 9 | * License (GPL). Copies of the GPL can be obtained from: |
| 10 | * ftp://prep.ai.mit.edu/pub/gnu/GPL |
| 11 | * Each contributing author retains all rights to their own work. |
| 12 | * |
| 13 | * (C) 1999-2001 Ben Fennema |
| 14 | * (C) 1999 Stelias Computing Inc |
| 15 | * |
| 16 | * HISTORY |
| 17 | * |
| 18 | * 02/24/99 blf Created. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "udfdecl.h" |
| 23 | |
| 24 | #include <linux/bitops.h> |
| 25 | #include <linux/overflow.h> |
| 26 | |
| 27 | #include "udf_i.h" |
| 28 | #include "udf_sb.h" |
| 29 | |
| 30 | #define udf_clear_bit __test_and_clear_bit_le |
| 31 | #define udf_set_bit __test_and_set_bit_le |
| 32 | #define udf_test_bit test_bit_le |
| 33 | #define udf_find_next_one_bit find_next_bit_le |
| 34 | |
| 35 | static int read_block_bitmap(struct super_block *sb, |
| 36 | struct udf_bitmap *bitmap, unsigned int block, |
| 37 | unsigned long bitmap_nr) |
| 38 | { |
| 39 | struct buffer_head *bh = NULL; |
| 40 | int i; |
| 41 | int max_bits, off, count; |
| 42 | struct kernel_lb_addr loc; |
| 43 | |
| 44 | loc.logicalBlockNum = bitmap->s_extPosition; |
| 45 | loc.partitionReferenceNum = UDF_SB(sb)->s_partition; |
| 46 | |
| 47 | bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block)); |
| 48 | bitmap->s_block_bitmap[bitmap_nr] = bh; |
| 49 | if (!bh) |
| 50 | return -EIO; |
| 51 | |
| 52 | /* Check consistency of Space Bitmap buffer. */ |
| 53 | max_bits = sb->s_blocksize * 8; |
| 54 | if (!bitmap_nr) { |
| 55 | off = sizeof(struct spaceBitmapDesc) << 3; |
| 56 | count = min(max_bits - off, bitmap->s_nr_groups); |
| 57 | } else { |
| 58 | /* |
| 59 | * Rough check if bitmap number is too big to have any bitmap |
| 60 | * blocks reserved. |
| 61 | */ |
| 62 | if (bitmap_nr > |
| 63 | (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2) |
| 64 | return 0; |
| 65 | off = 0; |
| 66 | count = bitmap->s_nr_groups - bitmap_nr * max_bits + |
| 67 | (sizeof(struct spaceBitmapDesc) << 3); |
| 68 | count = min(count, max_bits); |
| 69 | } |
| 70 | |
| 71 | for (i = 0; i < count; i++) |
| 72 | if (udf_test_bit(i + off, bh->b_data)) { |
| 73 | bitmap->s_block_bitmap[bitmap_nr] = |
| 74 | ERR_PTR(-EFSCORRUPTED); |
| 75 | brelse(bh); |
| 76 | return -EFSCORRUPTED; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int __load_block_bitmap(struct super_block *sb, |
| 82 | struct udf_bitmap *bitmap, |
| 83 | unsigned int block_group) |
| 84 | { |
| 85 | int retval = 0; |
| 86 | int nr_groups = bitmap->s_nr_groups; |
| 87 | |
| 88 | if (block_group >= nr_groups) { |
| 89 | udf_debug("block_group (%u) > nr_groups (%d)\n", |
| 90 | block_group, nr_groups); |
| 91 | } |
| 92 | |
| 93 | if (bitmap->s_block_bitmap[block_group]) { |
| 94 | /* |
| 95 | * The bitmap failed verification in the past. No point in |
| 96 | * trying again. |
| 97 | */ |
| 98 | if (IS_ERR(bitmap->s_block_bitmap[block_group])) |
| 99 | return PTR_ERR(bitmap->s_block_bitmap[block_group]); |
| 100 | return block_group; |
| 101 | } |
| 102 | |
| 103 | retval = read_block_bitmap(sb, bitmap, block_group, block_group); |
| 104 | if (retval < 0) |
| 105 | return retval; |
| 106 | |
| 107 | return block_group; |
| 108 | } |
| 109 | |
| 110 | static inline int load_block_bitmap(struct super_block *sb, |
| 111 | struct udf_bitmap *bitmap, |
| 112 | unsigned int block_group) |
| 113 | { |
| 114 | int slot; |
| 115 | |
| 116 | slot = __load_block_bitmap(sb, bitmap, block_group); |
| 117 | |
| 118 | if (slot < 0) |
| 119 | return slot; |
| 120 | |
| 121 | if (!bitmap->s_block_bitmap[slot]) |
| 122 | return -EIO; |
| 123 | |
| 124 | return slot; |
| 125 | } |
| 126 | |
| 127 | static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt) |
| 128 | { |
| 129 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 130 | struct logicalVolIntegrityDesc *lvid; |
| 131 | |
| 132 | if (!sbi->s_lvid_bh) |
| 133 | return; |
| 134 | |
| 135 | lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data; |
| 136 | le32_add_cpu(&lvid->freeSpaceTable[partition], cnt); |
| 137 | udf_updated_lvid(sb); |
| 138 | } |
| 139 | |
| 140 | static void udf_bitmap_free_blocks(struct super_block *sb, |
| 141 | struct udf_bitmap *bitmap, |
| 142 | struct kernel_lb_addr *bloc, |
| 143 | uint32_t offset, |
| 144 | uint32_t count) |
| 145 | { |
| 146 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 147 | struct buffer_head *bh = NULL; |
| 148 | unsigned long block; |
| 149 | unsigned long block_group; |
| 150 | unsigned long bit; |
| 151 | unsigned long i; |
| 152 | int bitmap_nr; |
| 153 | unsigned long overflow; |
| 154 | |
| 155 | mutex_lock(&sbi->s_alloc_mutex); |
| 156 | /* We make sure this cannot overflow when mounting the filesystem */ |
| 157 | block = bloc->logicalBlockNum + offset + |
| 158 | (sizeof(struct spaceBitmapDesc) << 3); |
| 159 | do { |
| 160 | overflow = 0; |
| 161 | block_group = block >> (sb->s_blocksize_bits + 3); |
| 162 | bit = block % (sb->s_blocksize << 3); |
| 163 | |
| 164 | /* |
| 165 | * Check to see if we are freeing blocks across a group boundary. |
| 166 | */ |
| 167 | if (bit + count > (sb->s_blocksize << 3)) { |
| 168 | overflow = bit + count - (sb->s_blocksize << 3); |
| 169 | count -= overflow; |
| 170 | } |
| 171 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 172 | if (bitmap_nr < 0) |
| 173 | goto error_return; |
| 174 | |
| 175 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
| 176 | for (i = 0; i < count; i++) { |
| 177 | if (udf_set_bit(bit + i, bh->b_data)) { |
| 178 | udf_debug("bit %lu already set\n", bit + i); |
| 179 | udf_debug("byte=%2x\n", |
| 180 | ((__u8 *)bh->b_data)[(bit + i) >> 3]); |
| 181 | } |
| 182 | } |
| 183 | udf_add_free_space(sb, sbi->s_partition, count); |
| 184 | mark_buffer_dirty(bh); |
| 185 | if (overflow) { |
| 186 | block += count; |
| 187 | count = overflow; |
| 188 | } |
| 189 | } while (overflow); |
| 190 | |
| 191 | error_return: |
| 192 | mutex_unlock(&sbi->s_alloc_mutex); |
| 193 | } |
| 194 | |
| 195 | static int udf_bitmap_prealloc_blocks(struct super_block *sb, |
| 196 | struct udf_bitmap *bitmap, |
| 197 | uint16_t partition, uint32_t first_block, |
| 198 | uint32_t block_count) |
| 199 | { |
| 200 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 201 | int alloc_count = 0; |
| 202 | int bit, block, block_group; |
| 203 | int bitmap_nr; |
| 204 | struct buffer_head *bh; |
| 205 | __u32 part_len; |
| 206 | |
| 207 | mutex_lock(&sbi->s_alloc_mutex); |
| 208 | part_len = sbi->s_partmaps[partition].s_partition_len; |
| 209 | if (first_block >= part_len) |
| 210 | goto out; |
| 211 | |
| 212 | if (first_block + block_count > part_len) |
| 213 | block_count = part_len - first_block; |
| 214 | |
| 215 | do { |
| 216 | block = first_block + (sizeof(struct spaceBitmapDesc) << 3); |
| 217 | block_group = block >> (sb->s_blocksize_bits + 3); |
| 218 | |
| 219 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 220 | if (bitmap_nr < 0) |
| 221 | goto out; |
| 222 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
| 223 | |
| 224 | bit = block % (sb->s_blocksize << 3); |
| 225 | |
| 226 | while (bit < (sb->s_blocksize << 3) && block_count > 0) { |
| 227 | if (!udf_clear_bit(bit, bh->b_data)) |
| 228 | goto out; |
| 229 | block_count--; |
| 230 | alloc_count++; |
| 231 | bit++; |
| 232 | block++; |
| 233 | } |
| 234 | mark_buffer_dirty(bh); |
| 235 | } while (block_count > 0); |
| 236 | |
| 237 | out: |
| 238 | udf_add_free_space(sb, partition, -alloc_count); |
| 239 | mutex_unlock(&sbi->s_alloc_mutex); |
| 240 | return alloc_count; |
| 241 | } |
| 242 | |
| 243 | static udf_pblk_t udf_bitmap_new_block(struct super_block *sb, |
| 244 | struct udf_bitmap *bitmap, uint16_t partition, |
| 245 | uint32_t goal, int *err) |
| 246 | { |
| 247 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 248 | int newbit, bit = 0; |
| 249 | udf_pblk_t block; |
| 250 | int block_group, group_start; |
| 251 | int end_goal, nr_groups, bitmap_nr, i; |
| 252 | struct buffer_head *bh = NULL; |
| 253 | char *ptr; |
| 254 | udf_pblk_t newblock = 0; |
| 255 | |
| 256 | *err = -ENOSPC; |
| 257 | mutex_lock(&sbi->s_alloc_mutex); |
| 258 | |
| 259 | repeat: |
| 260 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
| 261 | goal = 0; |
| 262 | |
| 263 | nr_groups = bitmap->s_nr_groups; |
| 264 | block = goal + (sizeof(struct spaceBitmapDesc) << 3); |
| 265 | block_group = block >> (sb->s_blocksize_bits + 3); |
| 266 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); |
| 267 | |
| 268 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 269 | if (bitmap_nr < 0) |
| 270 | goto error_return; |
| 271 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
| 272 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
| 273 | sb->s_blocksize - group_start); |
| 274 | |
| 275 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
| 276 | bit = block % (sb->s_blocksize << 3); |
| 277 | if (udf_test_bit(bit, bh->b_data)) |
| 278 | goto got_block; |
| 279 | |
| 280 | end_goal = (bit + 63) & ~63; |
| 281 | bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); |
| 282 | if (bit < end_goal) |
| 283 | goto got_block; |
| 284 | |
| 285 | ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, |
| 286 | sb->s_blocksize - ((bit + 7) >> 3)); |
| 287 | newbit = (ptr - ((char *)bh->b_data)) << 3; |
| 288 | if (newbit < sb->s_blocksize << 3) { |
| 289 | bit = newbit; |
| 290 | goto search_back; |
| 291 | } |
| 292 | |
| 293 | newbit = udf_find_next_one_bit(bh->b_data, |
| 294 | sb->s_blocksize << 3, bit); |
| 295 | if (newbit < sb->s_blocksize << 3) { |
| 296 | bit = newbit; |
| 297 | goto got_block; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | for (i = 0; i < (nr_groups * 2); i++) { |
| 302 | block_group++; |
| 303 | if (block_group >= nr_groups) |
| 304 | block_group = 0; |
| 305 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); |
| 306 | |
| 307 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 308 | if (bitmap_nr < 0) |
| 309 | goto error_return; |
| 310 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
| 311 | if (i < nr_groups) { |
| 312 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
| 313 | sb->s_blocksize - group_start); |
| 314 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
| 315 | bit = (ptr - ((char *)bh->b_data)) << 3; |
| 316 | break; |
| 317 | } |
| 318 | } else { |
| 319 | bit = udf_find_next_one_bit(bh->b_data, |
| 320 | sb->s_blocksize << 3, |
| 321 | group_start << 3); |
| 322 | if (bit < sb->s_blocksize << 3) |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | if (i >= (nr_groups * 2)) { |
| 327 | mutex_unlock(&sbi->s_alloc_mutex); |
| 328 | return newblock; |
| 329 | } |
| 330 | if (bit < sb->s_blocksize << 3) |
| 331 | goto search_back; |
| 332 | else |
| 333 | bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, |
| 334 | group_start << 3); |
| 335 | if (bit >= sb->s_blocksize << 3) { |
| 336 | mutex_unlock(&sbi->s_alloc_mutex); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | search_back: |
| 341 | i = 0; |
| 342 | while (i < 7 && bit > (group_start << 3) && |
| 343 | udf_test_bit(bit - 1, bh->b_data)) { |
| 344 | ++i; |
| 345 | --bit; |
| 346 | } |
| 347 | |
| 348 | got_block: |
| 349 | newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - |
| 350 | (sizeof(struct spaceBitmapDesc) << 3); |
| 351 | |
| 352 | if (newblock >= sbi->s_partmaps[partition].s_partition_len) { |
| 353 | /* |
| 354 | * Ran off the end of the bitmap, and bits following are |
| 355 | * non-compliant (not all zero) |
| 356 | */ |
| 357 | udf_err(sb, "bitmap for partition %d corrupted (block %u marked" |
| 358 | " as free, partition length is %u)\n", partition, |
| 359 | newblock, sbi->s_partmaps[partition].s_partition_len); |
| 360 | goto error_return; |
| 361 | } |
| 362 | |
| 363 | if (!udf_clear_bit(bit, bh->b_data)) { |
| 364 | udf_debug("bit already cleared for block %d\n", bit); |
| 365 | goto repeat; |
| 366 | } |
| 367 | |
| 368 | mark_buffer_dirty(bh); |
| 369 | |
| 370 | udf_add_free_space(sb, partition, -1); |
| 371 | mutex_unlock(&sbi->s_alloc_mutex); |
| 372 | *err = 0; |
| 373 | return newblock; |
| 374 | |
| 375 | error_return: |
| 376 | *err = -EIO; |
| 377 | mutex_unlock(&sbi->s_alloc_mutex); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void udf_table_free_blocks(struct super_block *sb, |
| 382 | struct inode *table, |
| 383 | struct kernel_lb_addr *bloc, |
| 384 | uint32_t offset, |
| 385 | uint32_t count) |
| 386 | { |
| 387 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 388 | uint32_t start, end; |
| 389 | uint32_t elen; |
| 390 | struct kernel_lb_addr eloc; |
| 391 | struct extent_position oepos, epos; |
| 392 | int8_t etype; |
| 393 | struct udf_inode_info *iinfo; |
| 394 | |
| 395 | mutex_lock(&sbi->s_alloc_mutex); |
| 396 | iinfo = UDF_I(table); |
| 397 | udf_add_free_space(sb, sbi->s_partition, count); |
| 398 | |
| 399 | start = bloc->logicalBlockNum + offset; |
| 400 | end = bloc->logicalBlockNum + offset + count - 1; |
| 401 | |
| 402 | epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); |
| 403 | elen = 0; |
| 404 | epos.block = oepos.block = iinfo->i_location; |
| 405 | epos.bh = oepos.bh = NULL; |
| 406 | |
| 407 | while (count && |
| 408 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
| 409 | if (((eloc.logicalBlockNum + |
| 410 | (elen >> sb->s_blocksize_bits)) == start)) { |
| 411 | if ((0x3FFFFFFF - elen) < |
| 412 | (count << sb->s_blocksize_bits)) { |
| 413 | uint32_t tmp = ((0x3FFFFFFF - elen) >> |
| 414 | sb->s_blocksize_bits); |
| 415 | count -= tmp; |
| 416 | start += tmp; |
| 417 | elen = (etype << 30) | |
| 418 | (0x40000000 - sb->s_blocksize); |
| 419 | } else { |
| 420 | elen = (etype << 30) | |
| 421 | (elen + |
| 422 | (count << sb->s_blocksize_bits)); |
| 423 | start += count; |
| 424 | count = 0; |
| 425 | } |
| 426 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
| 427 | } else if (eloc.logicalBlockNum == (end + 1)) { |
| 428 | if ((0x3FFFFFFF - elen) < |
| 429 | (count << sb->s_blocksize_bits)) { |
| 430 | uint32_t tmp = ((0x3FFFFFFF - elen) >> |
| 431 | sb->s_blocksize_bits); |
| 432 | count -= tmp; |
| 433 | end -= tmp; |
| 434 | eloc.logicalBlockNum -= tmp; |
| 435 | elen = (etype << 30) | |
| 436 | (0x40000000 - sb->s_blocksize); |
| 437 | } else { |
| 438 | eloc.logicalBlockNum = start; |
| 439 | elen = (etype << 30) | |
| 440 | (elen + |
| 441 | (count << sb->s_blocksize_bits)); |
| 442 | end -= count; |
| 443 | count = 0; |
| 444 | } |
| 445 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
| 446 | } |
| 447 | |
| 448 | if (epos.bh != oepos.bh) { |
| 449 | oepos.block = epos.block; |
| 450 | brelse(oepos.bh); |
| 451 | get_bh(epos.bh); |
| 452 | oepos.bh = epos.bh; |
| 453 | oepos.offset = 0; |
| 454 | } else { |
| 455 | oepos.offset = epos.offset; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (count) { |
| 460 | /* |
| 461 | * NOTE: we CANNOT use udf_add_aext here, as it can try to |
| 462 | * allocate a new block, and since we hold the super block |
| 463 | * lock already very bad things would happen :) |
| 464 | * |
| 465 | * We copy the behavior of udf_add_aext, but instead of |
| 466 | * trying to allocate a new block close to the existing one, |
| 467 | * we just steal a block from the extent we are trying to add. |
| 468 | * |
| 469 | * It would be nice if the blocks were close together, but it |
| 470 | * isn't required. |
| 471 | */ |
| 472 | |
| 473 | int adsize; |
| 474 | |
| 475 | eloc.logicalBlockNum = start; |
| 476 | elen = EXT_RECORDED_ALLOCATED | |
| 477 | (count << sb->s_blocksize_bits); |
| 478 | |
| 479 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
| 480 | adsize = sizeof(struct short_ad); |
| 481 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
| 482 | adsize = sizeof(struct long_ad); |
| 483 | else { |
| 484 | brelse(oepos.bh); |
| 485 | brelse(epos.bh); |
| 486 | goto error_return; |
| 487 | } |
| 488 | |
| 489 | if (epos.offset + (2 * adsize) > sb->s_blocksize) { |
| 490 | /* Steal a block from the extent being free'd */ |
| 491 | udf_setup_indirect_aext(table, eloc.logicalBlockNum, |
| 492 | &epos); |
| 493 | |
| 494 | eloc.logicalBlockNum++; |
| 495 | elen -= sb->s_blocksize; |
| 496 | } |
| 497 | |
| 498 | /* It's possible that stealing the block emptied the extent */ |
| 499 | if (elen) |
| 500 | __udf_add_aext(table, &epos, &eloc, elen, 1); |
| 501 | } |
| 502 | |
| 503 | brelse(epos.bh); |
| 504 | brelse(oepos.bh); |
| 505 | |
| 506 | error_return: |
| 507 | mutex_unlock(&sbi->s_alloc_mutex); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | static int udf_table_prealloc_blocks(struct super_block *sb, |
| 512 | struct inode *table, uint16_t partition, |
| 513 | uint32_t first_block, uint32_t block_count) |
| 514 | { |
| 515 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 516 | int alloc_count = 0; |
| 517 | uint32_t elen, adsize; |
| 518 | struct kernel_lb_addr eloc; |
| 519 | struct extent_position epos; |
| 520 | int8_t etype = -1; |
| 521 | struct udf_inode_info *iinfo; |
| 522 | |
| 523 | if (first_block >= sbi->s_partmaps[partition].s_partition_len) |
| 524 | return 0; |
| 525 | |
| 526 | iinfo = UDF_I(table); |
| 527 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
| 528 | adsize = sizeof(struct short_ad); |
| 529 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
| 530 | adsize = sizeof(struct long_ad); |
| 531 | else |
| 532 | return 0; |
| 533 | |
| 534 | mutex_lock(&sbi->s_alloc_mutex); |
| 535 | epos.offset = sizeof(struct unallocSpaceEntry); |
| 536 | epos.block = iinfo->i_location; |
| 537 | epos.bh = NULL; |
| 538 | eloc.logicalBlockNum = 0xFFFFFFFF; |
| 539 | |
| 540 | while (first_block != eloc.logicalBlockNum && |
| 541 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
| 542 | udf_debug("eloc=%u, elen=%u, first_block=%u\n", |
| 543 | eloc.logicalBlockNum, elen, first_block); |
| 544 | ; /* empty loop body */ |
| 545 | } |
| 546 | |
| 547 | if (first_block == eloc.logicalBlockNum) { |
| 548 | epos.offset -= adsize; |
| 549 | |
| 550 | alloc_count = (elen >> sb->s_blocksize_bits); |
| 551 | if (alloc_count > block_count) { |
| 552 | alloc_count = block_count; |
| 553 | eloc.logicalBlockNum += alloc_count; |
| 554 | elen -= (alloc_count << sb->s_blocksize_bits); |
| 555 | udf_write_aext(table, &epos, &eloc, |
| 556 | (etype << 30) | elen, 1); |
| 557 | } else |
| 558 | udf_delete_aext(table, epos); |
| 559 | } else { |
| 560 | alloc_count = 0; |
| 561 | } |
| 562 | |
| 563 | brelse(epos.bh); |
| 564 | |
| 565 | if (alloc_count) |
| 566 | udf_add_free_space(sb, partition, -alloc_count); |
| 567 | mutex_unlock(&sbi->s_alloc_mutex); |
| 568 | return alloc_count; |
| 569 | } |
| 570 | |
| 571 | static udf_pblk_t udf_table_new_block(struct super_block *sb, |
| 572 | struct inode *table, uint16_t partition, |
| 573 | uint32_t goal, int *err) |
| 574 | { |
| 575 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 576 | uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; |
| 577 | udf_pblk_t newblock = 0; |
| 578 | uint32_t adsize; |
| 579 | uint32_t elen, goal_elen = 0; |
| 580 | struct kernel_lb_addr eloc, goal_eloc; |
| 581 | struct extent_position epos, goal_epos; |
| 582 | int8_t etype; |
| 583 | struct udf_inode_info *iinfo = UDF_I(table); |
| 584 | |
| 585 | *err = -ENOSPC; |
| 586 | |
| 587 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
| 588 | adsize = sizeof(struct short_ad); |
| 589 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
| 590 | adsize = sizeof(struct long_ad); |
| 591 | else |
| 592 | return newblock; |
| 593 | |
| 594 | mutex_lock(&sbi->s_alloc_mutex); |
| 595 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
| 596 | goal = 0; |
| 597 | |
| 598 | /* We search for the closest matching block to goal. If we find |
| 599 | a exact hit, we stop. Otherwise we keep going till we run out |
| 600 | of extents. We store the buffer_head, bloc, and extoffset |
| 601 | of the current closest match and use that when we are done. |
| 602 | */ |
| 603 | epos.offset = sizeof(struct unallocSpaceEntry); |
| 604 | epos.block = iinfo->i_location; |
| 605 | epos.bh = goal_epos.bh = NULL; |
| 606 | |
| 607 | while (spread && |
| 608 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
| 609 | if (goal >= eloc.logicalBlockNum) { |
| 610 | if (goal < eloc.logicalBlockNum + |
| 611 | (elen >> sb->s_blocksize_bits)) |
| 612 | nspread = 0; |
| 613 | else |
| 614 | nspread = goal - eloc.logicalBlockNum - |
| 615 | (elen >> sb->s_blocksize_bits); |
| 616 | } else { |
| 617 | nspread = eloc.logicalBlockNum - goal; |
| 618 | } |
| 619 | |
| 620 | if (nspread < spread) { |
| 621 | spread = nspread; |
| 622 | if (goal_epos.bh != epos.bh) { |
| 623 | brelse(goal_epos.bh); |
| 624 | goal_epos.bh = epos.bh; |
| 625 | get_bh(goal_epos.bh); |
| 626 | } |
| 627 | goal_epos.block = epos.block; |
| 628 | goal_epos.offset = epos.offset - adsize; |
| 629 | goal_eloc = eloc; |
| 630 | goal_elen = (etype << 30) | elen; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | brelse(epos.bh); |
| 635 | |
| 636 | if (spread == 0xFFFFFFFF) { |
| 637 | brelse(goal_epos.bh); |
| 638 | mutex_unlock(&sbi->s_alloc_mutex); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | /* Only allocate blocks from the beginning of the extent. |
| 643 | That way, we only delete (empty) extents, never have to insert an |
| 644 | extent because of splitting */ |
| 645 | /* This works, but very poorly.... */ |
| 646 | |
| 647 | newblock = goal_eloc.logicalBlockNum; |
| 648 | goal_eloc.logicalBlockNum++; |
| 649 | goal_elen -= sb->s_blocksize; |
| 650 | |
| 651 | if (goal_elen) |
| 652 | udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1); |
| 653 | else |
| 654 | udf_delete_aext(table, goal_epos); |
| 655 | brelse(goal_epos.bh); |
| 656 | |
| 657 | udf_add_free_space(sb, partition, -1); |
| 658 | |
| 659 | mutex_unlock(&sbi->s_alloc_mutex); |
| 660 | *err = 0; |
| 661 | return newblock; |
| 662 | } |
| 663 | |
| 664 | void udf_free_blocks(struct super_block *sb, struct inode *inode, |
| 665 | struct kernel_lb_addr *bloc, uint32_t offset, |
| 666 | uint32_t count) |
| 667 | { |
| 668 | uint16_t partition = bloc->partitionReferenceNum; |
| 669 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
| 670 | uint32_t blk; |
| 671 | |
| 672 | if (check_add_overflow(bloc->logicalBlockNum, offset, &blk) || |
| 673 | check_add_overflow(blk, count, &blk) || |
| 674 | bloc->logicalBlockNum + count > map->s_partition_len) { |
| 675 | udf_debug("Invalid request to free blocks: (%d, %u), off %u, " |
| 676 | "len %u, partition len %u\n", |
| 677 | partition, bloc->logicalBlockNum, offset, count, |
| 678 | map->s_partition_len); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { |
| 683 | udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap, |
| 684 | bloc, offset, count); |
| 685 | } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { |
| 686 | udf_table_free_blocks(sb, map->s_uspace.s_table, |
| 687 | bloc, offset, count); |
| 688 | } |
| 689 | |
| 690 | if (inode) { |
| 691 | inode_sub_bytes(inode, |
| 692 | ((sector_t)count) << sb->s_blocksize_bits); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | inline int udf_prealloc_blocks(struct super_block *sb, |
| 697 | struct inode *inode, |
| 698 | uint16_t partition, uint32_t first_block, |
| 699 | uint32_t block_count) |
| 700 | { |
| 701 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
| 702 | int allocated; |
| 703 | |
| 704 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
| 705 | allocated = udf_bitmap_prealloc_blocks(sb, |
| 706 | map->s_uspace.s_bitmap, |
| 707 | partition, first_block, |
| 708 | block_count); |
| 709 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
| 710 | allocated = udf_table_prealloc_blocks(sb, |
| 711 | map->s_uspace.s_table, |
| 712 | partition, first_block, |
| 713 | block_count); |
| 714 | else |
| 715 | return 0; |
| 716 | |
| 717 | if (inode && allocated > 0) |
| 718 | inode_add_bytes(inode, allocated << sb->s_blocksize_bits); |
| 719 | return allocated; |
| 720 | } |
| 721 | |
| 722 | inline udf_pblk_t udf_new_block(struct super_block *sb, |
| 723 | struct inode *inode, |
| 724 | uint16_t partition, uint32_t goal, int *err) |
| 725 | { |
| 726 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
| 727 | udf_pblk_t block; |
| 728 | |
| 729 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
| 730 | block = udf_bitmap_new_block(sb, |
| 731 | map->s_uspace.s_bitmap, |
| 732 | partition, goal, err); |
| 733 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
| 734 | block = udf_table_new_block(sb, |
| 735 | map->s_uspace.s_table, |
| 736 | partition, goal, err); |
| 737 | else { |
| 738 | *err = -EIO; |
| 739 | return 0; |
| 740 | } |
| 741 | if (inode && block) |
| 742 | inode_add_bytes(inode, sb->s_blocksize); |
| 743 | return block; |
| 744 | } |