Lines Matching +full:block +full:- +full:offset
1 // SPDX-License-Identifier: GPL-2.0-only
6 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
9 * (C) 1999-2001 Ben Fennema
32 struct udf_bitmap *bitmap, unsigned int block, in read_block_bitmap() argument
40 loc.logicalBlockNum = bitmap->s_extPosition; in read_block_bitmap()
41 loc.partitionReferenceNum = UDF_SB(sb)->s_partition; in read_block_bitmap()
43 bh = sb_bread(sb, udf_get_lb_pblock(sb, &loc, block)); in read_block_bitmap()
44 bitmap->s_block_bitmap[bitmap_nr] = bh; in read_block_bitmap()
46 return -EIO; in read_block_bitmap()
49 max_bits = sb->s_blocksize * 8; in read_block_bitmap()
52 count = min(max_bits - off, bitmap->s_nr_groups); in read_block_bitmap()
59 (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2) in read_block_bitmap()
62 count = bitmap->s_nr_groups - bitmap_nr * max_bits + in read_block_bitmap()
68 if (udf_test_bit(i + off, bh->b_data)) { in read_block_bitmap()
69 bitmap->s_block_bitmap[bitmap_nr] = in read_block_bitmap()
70 ERR_PTR(-EFSCORRUPTED); in read_block_bitmap()
72 return -EFSCORRUPTED; in read_block_bitmap()
82 int nr_groups = bitmap->s_nr_groups; in load_block_bitmap()
89 if (bitmap->s_block_bitmap[block_group]) { in load_block_bitmap()
94 if (IS_ERR(bitmap->s_block_bitmap[block_group])) in load_block_bitmap()
95 return PTR_ERR(bitmap->s_block_bitmap[block_group]); in load_block_bitmap()
111 if (!sbi->s_lvid_bh) in udf_add_free_space()
114 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data; in udf_add_free_space()
115 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt); in udf_add_free_space()
122 uint32_t offset, in udf_bitmap_free_blocks() argument
127 unsigned long block; in udf_bitmap_free_blocks() local
134 mutex_lock(&sbi->s_alloc_mutex); in udf_bitmap_free_blocks()
136 block = bloc->logicalBlockNum + offset + in udf_bitmap_free_blocks()
140 block_group = block >> (sb->s_blocksize_bits + 3); in udf_bitmap_free_blocks()
141 bit = block % (sb->s_blocksize << 3); in udf_bitmap_free_blocks()
146 if (bit + count > (sb->s_blocksize << 3)) { in udf_bitmap_free_blocks()
147 overflow = bit + count - (sb->s_blocksize << 3); in udf_bitmap_free_blocks()
148 count -= overflow; in udf_bitmap_free_blocks()
154 bh = bitmap->s_block_bitmap[bitmap_nr]; in udf_bitmap_free_blocks()
156 if (udf_set_bit(bit + i, bh->b_data)) { in udf_bitmap_free_blocks()
159 ((__u8 *)bh->b_data)[(bit + i) >> 3]); in udf_bitmap_free_blocks()
162 udf_add_free_space(sb, sbi->s_partition, count); in udf_bitmap_free_blocks()
165 block += count; in udf_bitmap_free_blocks()
171 mutex_unlock(&sbi->s_alloc_mutex); in udf_bitmap_free_blocks()
181 int bit, block, block_group; in udf_bitmap_prealloc_blocks() local
186 mutex_lock(&sbi->s_alloc_mutex); in udf_bitmap_prealloc_blocks()
187 part_len = sbi->s_partmaps[partition].s_partition_len; in udf_bitmap_prealloc_blocks()
192 block_count = part_len - first_block; in udf_bitmap_prealloc_blocks()
195 block = first_block + (sizeof(struct spaceBitmapDesc) << 3); in udf_bitmap_prealloc_blocks()
196 block_group = block >> (sb->s_blocksize_bits + 3); in udf_bitmap_prealloc_blocks()
201 bh = bitmap->s_block_bitmap[bitmap_nr]; in udf_bitmap_prealloc_blocks()
203 bit = block % (sb->s_blocksize << 3); in udf_bitmap_prealloc_blocks()
205 while (bit < (sb->s_blocksize << 3) && block_count > 0) { in udf_bitmap_prealloc_blocks()
206 if (!udf_clear_bit(bit, bh->b_data)) in udf_bitmap_prealloc_blocks()
208 block_count--; in udf_bitmap_prealloc_blocks()
211 block++; in udf_bitmap_prealloc_blocks()
217 udf_add_free_space(sb, partition, -alloc_count); in udf_bitmap_prealloc_blocks()
218 mutex_unlock(&sbi->s_alloc_mutex); in udf_bitmap_prealloc_blocks()
228 udf_pblk_t block; in udf_bitmap_new_block() local
235 *err = -ENOSPC; in udf_bitmap_new_block()
236 mutex_lock(&sbi->s_alloc_mutex); in udf_bitmap_new_block()
239 if (goal >= sbi->s_partmaps[partition].s_partition_len) in udf_bitmap_new_block()
242 nr_groups = bitmap->s_nr_groups; in udf_bitmap_new_block()
243 block = goal + (sizeof(struct spaceBitmapDesc) << 3); in udf_bitmap_new_block()
244 block_group = block >> (sb->s_blocksize_bits + 3); in udf_bitmap_new_block()
250 bh = bitmap->s_block_bitmap[bitmap_nr]; in udf_bitmap_new_block()
251 ptr = memscan((char *)bh->b_data + group_start, 0xFF, in udf_bitmap_new_block()
252 sb->s_blocksize - group_start); in udf_bitmap_new_block()
254 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { in udf_bitmap_new_block()
255 bit = block % (sb->s_blocksize << 3); in udf_bitmap_new_block()
256 if (udf_test_bit(bit, bh->b_data)) in udf_bitmap_new_block()
260 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); in udf_bitmap_new_block()
264 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, in udf_bitmap_new_block()
265 sb->s_blocksize - ((bit + 7) >> 3)); in udf_bitmap_new_block()
266 newbit = (ptr - ((char *)bh->b_data)) << 3; in udf_bitmap_new_block()
267 if (newbit < sb->s_blocksize << 3) { in udf_bitmap_new_block()
272 newbit = udf_find_next_one_bit(bh->b_data, in udf_bitmap_new_block()
273 sb->s_blocksize << 3, bit); in udf_bitmap_new_block()
274 if (newbit < sb->s_blocksize << 3) { in udf_bitmap_new_block()
289 bh = bitmap->s_block_bitmap[bitmap_nr]; in udf_bitmap_new_block()
291 ptr = memscan((char *)bh->b_data + group_start, 0xFF, in udf_bitmap_new_block()
292 sb->s_blocksize - group_start); in udf_bitmap_new_block()
293 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { in udf_bitmap_new_block()
294 bit = (ptr - ((char *)bh->b_data)) << 3; in udf_bitmap_new_block()
298 bit = udf_find_next_one_bit(bh->b_data, in udf_bitmap_new_block()
299 sb->s_blocksize << 3, in udf_bitmap_new_block()
301 if (bit < sb->s_blocksize << 3) in udf_bitmap_new_block()
306 mutex_unlock(&sbi->s_alloc_mutex); in udf_bitmap_new_block()
309 if (bit < sb->s_blocksize << 3) in udf_bitmap_new_block()
312 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, in udf_bitmap_new_block()
314 if (bit >= sb->s_blocksize << 3) { in udf_bitmap_new_block()
315 mutex_unlock(&sbi->s_alloc_mutex); in udf_bitmap_new_block()
322 udf_test_bit(bit - 1, bh->b_data)) { in udf_bitmap_new_block()
324 --bit; in udf_bitmap_new_block()
328 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - in udf_bitmap_new_block()
331 if (newblock >= sbi->s_partmaps[partition].s_partition_len) { in udf_bitmap_new_block()
334 * non-compliant (not all zero) in udf_bitmap_new_block()
336 udf_err(sb, "bitmap for partition %d corrupted (block %u marked" in udf_bitmap_new_block()
338 newblock, sbi->s_partmaps[partition].s_partition_len); in udf_bitmap_new_block()
342 if (!udf_clear_bit(bit, bh->b_data)) { in udf_bitmap_new_block()
343 udf_debug("bit already cleared for block %d\n", bit); in udf_bitmap_new_block()
349 udf_add_free_space(sb, partition, -1); in udf_bitmap_new_block()
350 mutex_unlock(&sbi->s_alloc_mutex); in udf_bitmap_new_block()
355 *err = -EIO; in udf_bitmap_new_block()
356 mutex_unlock(&sbi->s_alloc_mutex); in udf_bitmap_new_block()
363 uint32_t offset, in udf_table_free_blocks() argument
375 mutex_lock(&sbi->s_alloc_mutex); in udf_table_free_blocks()
377 udf_add_free_space(sb, sbi->s_partition, count); in udf_table_free_blocks()
379 start = bloc->logicalBlockNum + offset; in udf_table_free_blocks()
380 end = bloc->logicalBlockNum + offset + count - 1; in udf_table_free_blocks()
382 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); in udf_table_free_blocks()
384 epos.block = oepos.block = iinfo->i_location; in udf_table_free_blocks()
394 (elen >> sb->s_blocksize_bits)) == start)) { in udf_table_free_blocks()
395 if ((0x3FFFFFFF - elen) < in udf_table_free_blocks()
396 (count << sb->s_blocksize_bits)) { in udf_table_free_blocks()
397 uint32_t tmp = ((0x3FFFFFFF - elen) >> in udf_table_free_blocks()
398 sb->s_blocksize_bits); in udf_table_free_blocks()
399 count -= tmp; in udf_table_free_blocks()
402 (0x40000000 - sb->s_blocksize); in udf_table_free_blocks()
406 (count << sb->s_blocksize_bits)); in udf_table_free_blocks()
412 if ((0x3FFFFFFF - elen) < in udf_table_free_blocks()
413 (count << sb->s_blocksize_bits)) { in udf_table_free_blocks()
414 uint32_t tmp = ((0x3FFFFFFF - elen) >> in udf_table_free_blocks()
415 sb->s_blocksize_bits); in udf_table_free_blocks()
416 count -= tmp; in udf_table_free_blocks()
417 end -= tmp; in udf_table_free_blocks()
418 eloc.logicalBlockNum -= tmp; in udf_table_free_blocks()
420 (0x40000000 - sb->s_blocksize); in udf_table_free_blocks()
425 (count << sb->s_blocksize_bits)); in udf_table_free_blocks()
426 end -= count; in udf_table_free_blocks()
433 oepos.block = epos.block; in udf_table_free_blocks()
437 oepos.offset = 0; in udf_table_free_blocks()
439 oepos.offset = epos.offset; in udf_table_free_blocks()
446 * allocate a new block, and since we hold the super block in udf_table_free_blocks()
450 * trying to allocate a new block close to the existing one, in udf_table_free_blocks()
451 * we just steal a block from the extent we are trying to add. in udf_table_free_blocks()
461 (count << sb->s_blocksize_bits); in udf_table_free_blocks()
463 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) in udf_table_free_blocks()
465 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) in udf_table_free_blocks()
470 if (epos.offset + (2 * adsize) > sb->s_blocksize) { in udf_table_free_blocks()
471 /* Steal a block from the extent being free'd */ in udf_table_free_blocks()
476 elen -= sb->s_blocksize; in udf_table_free_blocks()
479 /* It's possible that stealing the block emptied the extent */ in udf_table_free_blocks()
488 mutex_unlock(&sbi->s_alloc_mutex); in udf_table_free_blocks()
501 int8_t etype = -1; in udf_table_prealloc_blocks()
505 if (first_block >= sbi->s_partmaps[partition].s_partition_len) in udf_table_prealloc_blocks()
509 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) in udf_table_prealloc_blocks()
511 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) in udf_table_prealloc_blocks()
516 mutex_lock(&sbi->s_alloc_mutex); in udf_table_prealloc_blocks()
517 epos.offset = sizeof(struct unallocSpaceEntry); in udf_table_prealloc_blocks()
518 epos.block = iinfo->i_location; in udf_table_prealloc_blocks()
533 epos.offset -= adsize; in udf_table_prealloc_blocks()
535 alloc_count = (elen >> sb->s_blocksize_bits); in udf_table_prealloc_blocks()
539 elen -= (alloc_count << sb->s_blocksize_bits); in udf_table_prealloc_blocks()
552 udf_add_free_space(sb, partition, -alloc_count); in udf_table_prealloc_blocks()
553 mutex_unlock(&sbi->s_alloc_mutex); in udf_table_prealloc_blocks()
572 *err = -ENOSPC; in udf_table_new_block()
574 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) in udf_table_new_block()
576 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) in udf_table_new_block()
581 mutex_lock(&sbi->s_alloc_mutex); in udf_table_new_block()
582 if (goal >= sbi->s_partmaps[partition].s_partition_len) in udf_table_new_block()
585 /* We search for the closest matching block to goal. If we find in udf_table_new_block()
590 epos.offset = sizeof(struct unallocSpaceEntry); in udf_table_new_block()
591 epos.block = iinfo->i_location; in udf_table_new_block()
600 (elen >> sb->s_blocksize_bits)) in udf_table_new_block()
603 nspread = goal - eloc.logicalBlockNum - in udf_table_new_block()
604 (elen >> sb->s_blocksize_bits); in udf_table_new_block()
606 nspread = eloc.logicalBlockNum - goal; in udf_table_new_block()
616 goal_epos.block = epos.block; in udf_table_new_block()
617 goal_epos.offset = epos.offset - adsize; in udf_table_new_block()
627 mutex_unlock(&sbi->s_alloc_mutex); in udf_table_new_block()
640 goal_elen -= sb->s_blocksize; in udf_table_new_block()
648 udf_add_free_space(sb, partition, -1); in udf_table_new_block()
650 mutex_unlock(&sbi->s_alloc_mutex); in udf_table_new_block()
656 struct kernel_lb_addr *bloc, uint32_t offset, in udf_free_blocks() argument
659 uint16_t partition = bloc->partitionReferenceNum; in udf_free_blocks()
660 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; in udf_free_blocks()
663 if (check_add_overflow(bloc->logicalBlockNum, offset, &blk) || in udf_free_blocks()
665 bloc->logicalBlockNum + count > map->s_partition_len) { in udf_free_blocks()
668 partition, bloc->logicalBlockNum, offset, count, in udf_free_blocks()
669 map->s_partition_len); in udf_free_blocks()
673 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { in udf_free_blocks()
674 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap, in udf_free_blocks()
675 bloc, offset, count); in udf_free_blocks()
676 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { in udf_free_blocks()
677 udf_table_free_blocks(sb, map->s_uspace.s_table, in udf_free_blocks()
678 bloc, offset, count); in udf_free_blocks()
683 ((sector_t)count) << sb->s_blocksize_bits); in udf_free_blocks()
692 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; in udf_prealloc_blocks()
695 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) in udf_prealloc_blocks()
697 map->s_uspace.s_bitmap, in udf_prealloc_blocks()
700 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) in udf_prealloc_blocks()
702 map->s_uspace.s_table, in udf_prealloc_blocks()
709 inode_add_bytes(inode, allocated << sb->s_blocksize_bits); in udf_prealloc_blocks()
717 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; in udf_new_block()
718 udf_pblk_t block; in udf_new_block() local
720 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) in udf_new_block()
721 block = udf_bitmap_new_block(sb, in udf_new_block()
722 map->s_uspace.s_bitmap, in udf_new_block()
724 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) in udf_new_block()
725 block = udf_table_new_block(sb, in udf_new_block()
726 map->s_uspace.s_table, in udf_new_block()
729 *err = -EIO; in udf_new_block()
732 if (inode && block) in udf_new_block()
733 inode_add_bytes(inode, sb->s_blocksize); in udf_new_block()
734 return block; in udf_new_block()