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
10 * (C) 1999 Stelias Computing Inc
14 * 02/24/99 blf Created.
20 #include <linux/bitops.h>
21 #include <linux/overflow.h>
26 #define udf_clear_bit __test_and_clear_bit_le
27 #define udf_set_bit __test_and_set_bit_le
28 #define udf_test_bit test_bit_le
29 #define udf_find_next_one_bit find_next_bit_le
31 static int read_block_bitmap(struct super_block *sb,
32 struct udf_bitmap *bitmap, unsigned int block,
33 unsigned long bitmap_nr)
35 struct buffer_head *bh = NULL;
37 int max_bits, off, count;
38 struct kernel_lb_addr loc;
40 loc.logicalBlockNum = bitmap->s_extPosition;
41 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
43 bh = sb_bread(sb, udf_get_lb_pblock(sb, &loc, block));
44 bitmap->s_block_bitmap[bitmap_nr] = bh;
48 /* Check consistency of Space Bitmap buffer. */
49 max_bits = sb->s_blocksize * 8;
51 off = sizeof(struct spaceBitmapDesc) << 3;
52 count = min(max_bits - off, bitmap->s_nr_groups);
55 * Rough check if bitmap number is too big to have any bitmap
59 (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2)
62 count = bitmap->s_nr_groups - bitmap_nr * max_bits +
63 (sizeof(struct spaceBitmapDesc) << 3);
64 count = min(count, max_bits);
67 for (i = 0; i < count; i++)
68 if (udf_test_bit(i + off, bh->b_data)) {
69 bitmap->s_block_bitmap[bitmap_nr] =
70 ERR_PTR(-EFSCORRUPTED);
77 static int load_block_bitmap(struct super_block *sb,
78 struct udf_bitmap *bitmap,
79 unsigned int block_group)
82 int nr_groups = bitmap->s_nr_groups;
84 if (block_group >= nr_groups) {
85 udf_debug("block_group (%u) > nr_groups (%d)\n",
86 block_group, nr_groups);
89 if (bitmap->s_block_bitmap[block_group]) {
91 * The bitmap failed verification in the past. No point in
94 if (IS_ERR(bitmap->s_block_bitmap[block_group]))
95 return PTR_ERR(bitmap->s_block_bitmap[block_group]);
99 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
106 static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
108 struct udf_sb_info *sbi = UDF_SB(sb);
109 struct logicalVolIntegrityDesc *lvid;
114 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
115 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
116 udf_updated_lvid(sb);
119 static void udf_bitmap_free_blocks(struct super_block *sb,
120 struct udf_bitmap *bitmap,
121 struct kernel_lb_addr *bloc,
125 struct udf_sb_info *sbi = UDF_SB(sb);
126 struct buffer_head *bh = NULL;
128 unsigned long block_group;
132 unsigned long overflow;
134 mutex_lock(&sbi->s_alloc_mutex);
135 /* We make sure this cannot overflow when mounting the filesystem */
136 block = bloc->logicalBlockNum + offset +
137 (sizeof(struct spaceBitmapDesc) << 3);
140 block_group = block >> (sb->s_blocksize_bits + 3);
141 bit = block % (sb->s_blocksize << 3);
144 * Check to see if we are freeing blocks across a group boundary.
146 if (bit + count > (sb->s_blocksize << 3)) {
147 overflow = bit + count - (sb->s_blocksize << 3);
150 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
154 bh = bitmap->s_block_bitmap[bitmap_nr];
155 for (i = 0; i < count; i++) {
156 if (udf_set_bit(bit + i, bh->b_data)) {
157 udf_debug("bit %lu already set\n", bit + i);
158 udf_debug("byte=%2x\n",
159 ((__u8 *)bh->b_data)[(bit + i) >> 3]);
162 udf_add_free_space(sb, sbi->s_partition, count);
163 mark_buffer_dirty(bh);
171 mutex_unlock(&sbi->s_alloc_mutex);
174 static int udf_bitmap_prealloc_blocks(struct super_block *sb,
175 struct udf_bitmap *bitmap,
176 uint16_t partition, uint32_t first_block,
177 uint32_t block_count)
179 struct udf_sb_info *sbi = UDF_SB(sb);
181 int bit, block, block_group;
183 struct buffer_head *bh;
186 mutex_lock(&sbi->s_alloc_mutex);
187 part_len = sbi->s_partmaps[partition].s_partition_len;
188 if (first_block >= part_len)
191 if (first_block + block_count > part_len)
192 block_count = part_len - first_block;
195 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
196 block_group = block >> (sb->s_blocksize_bits + 3);
198 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
201 bh = bitmap->s_block_bitmap[bitmap_nr];
203 bit = block % (sb->s_blocksize << 3);
205 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
206 if (!udf_clear_bit(bit, bh->b_data))
213 mark_buffer_dirty(bh);
214 } while (block_count > 0);
217 udf_add_free_space(sb, partition, -alloc_count);
218 mutex_unlock(&sbi->s_alloc_mutex);
222 static udf_pblk_t udf_bitmap_new_block(struct super_block *sb,
223 struct udf_bitmap *bitmap, uint16_t partition,
224 uint32_t goal, int *err)
226 struct udf_sb_info *sbi = UDF_SB(sb);
229 int block_group, group_start;
230 int end_goal, nr_groups, bitmap_nr, i;
231 struct buffer_head *bh = NULL;
233 udf_pblk_t newblock = 0;
236 mutex_lock(&sbi->s_alloc_mutex);
239 if (goal >= sbi->s_partmaps[partition].s_partition_len)
242 nr_groups = bitmap->s_nr_groups;
243 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
244 block_group = block >> (sb->s_blocksize_bits + 3);
245 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
247 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
250 bh = bitmap->s_block_bitmap[bitmap_nr];
251 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
252 sb->s_blocksize - group_start);
254 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
255 bit = block % (sb->s_blocksize << 3);
256 if (udf_test_bit(bit, bh->b_data))
259 end_goal = (bit + 63) & ~63;
260 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
264 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
265 sb->s_blocksize - ((bit + 7) >> 3));
266 newbit = (ptr - ((char *)bh->b_data)) << 3;
267 if (newbit < sb->s_blocksize << 3) {
272 newbit = udf_find_next_one_bit(bh->b_data,
273 sb->s_blocksize << 3, bit);
274 if (newbit < sb->s_blocksize << 3) {
280 for (i = 0; i < (nr_groups * 2); i++) {
282 if (block_group >= nr_groups)
284 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
286 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
289 bh = bitmap->s_block_bitmap[bitmap_nr];
291 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
292 sb->s_blocksize - group_start);
293 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
294 bit = (ptr - ((char *)bh->b_data)) << 3;
298 bit = udf_find_next_one_bit(bh->b_data,
299 sb->s_blocksize << 3,
301 if (bit < sb->s_blocksize << 3)
305 if (i >= (nr_groups * 2)) {
306 mutex_unlock(&sbi->s_alloc_mutex);
309 if (bit < sb->s_blocksize << 3)
312 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
314 if (bit >= sb->s_blocksize << 3) {
315 mutex_unlock(&sbi->s_alloc_mutex);
321 while (i < 7 && bit > (group_start << 3) &&
322 udf_test_bit(bit - 1, bh->b_data)) {
328 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
329 (sizeof(struct spaceBitmapDesc) << 3);
331 if (newblock >= sbi->s_partmaps[partition].s_partition_len) {
333 * Ran off the end of the bitmap, and bits following are
334 * non-compliant (not all zero)
336 udf_err(sb, "bitmap for partition %d corrupted (block %u marked"
337 " as free, partition length is %u)\n", partition,
338 newblock, sbi->s_partmaps[partition].s_partition_len);
342 if (!udf_clear_bit(bit, bh->b_data)) {
343 udf_debug("bit already cleared for block %d\n", bit);
347 mark_buffer_dirty(bh);
349 udf_add_free_space(sb, partition, -1);
350 mutex_unlock(&sbi->s_alloc_mutex);
356 mutex_unlock(&sbi->s_alloc_mutex);
360 static void udf_table_free_blocks(struct super_block *sb,
362 struct kernel_lb_addr *bloc,
366 struct udf_sb_info *sbi = UDF_SB(sb);
369 struct kernel_lb_addr eloc;
370 struct extent_position oepos, epos;
372 struct udf_inode_info *iinfo;
375 mutex_lock(&sbi->s_alloc_mutex);
376 iinfo = UDF_I(table);
377 udf_add_free_space(sb, sbi->s_partition, count);
379 start = bloc->logicalBlockNum + offset;
380 end = bloc->logicalBlockNum + offset + count - 1;
382 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
384 epos.block = oepos.block = iinfo->i_location;
385 epos.bh = oepos.bh = NULL;
388 ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
393 if (((eloc.logicalBlockNum +
394 (elen >> sb->s_blocksize_bits)) == start)) {
395 if ((0x3FFFFFFF - elen) <
396 (count << sb->s_blocksize_bits)) {
397 uint32_t tmp = ((0x3FFFFFFF - elen) >>
398 sb->s_blocksize_bits);
401 elen = (etype << 30) |
402 (0x40000000 - sb->s_blocksize);
404 elen = (etype << 30) |
406 (count << sb->s_blocksize_bits));
410 udf_write_aext(table, &oepos, &eloc, elen, 1);
411 } else if (eloc.logicalBlockNum == (end + 1)) {
412 if ((0x3FFFFFFF - elen) <
413 (count << sb->s_blocksize_bits)) {
414 uint32_t tmp = ((0x3FFFFFFF - elen) >>
415 sb->s_blocksize_bits);
418 eloc.logicalBlockNum -= tmp;
419 elen = (etype << 30) |
420 (0x40000000 - sb->s_blocksize);
422 eloc.logicalBlockNum = start;
423 elen = (etype << 30) |
425 (count << sb->s_blocksize_bits));
429 udf_write_aext(table, &oepos, &eloc, elen, 1);
432 if (epos.bh != oepos.bh) {
433 oepos.block = epos.block;
439 oepos.offset = epos.offset;
445 * NOTE: we CANNOT use udf_add_aext here, as it can try to
446 * allocate a new block, and since we hold the super block
447 * lock already very bad things would happen :)
449 * We copy the behavior of udf_add_aext, but instead of
450 * trying to allocate a new block close to the existing one,
451 * we just steal a block from the extent we are trying to add.
453 * It would be nice if the blocks were close together, but it
459 eloc.logicalBlockNum = start;
460 elen = EXT_RECORDED_ALLOCATED |
461 (count << sb->s_blocksize_bits);
463 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
464 adsize = sizeof(struct short_ad);
465 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
466 adsize = sizeof(struct long_ad);
470 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
471 /* Steal a block from the extent being free'd */
472 udf_setup_indirect_aext(table, eloc.logicalBlockNum,
475 eloc.logicalBlockNum++;
476 elen -= sb->s_blocksize;
479 /* It's possible that stealing the block emptied the extent */
481 __udf_add_aext(table, &epos, &eloc, elen, 1);
488 mutex_unlock(&sbi->s_alloc_mutex);
492 static int udf_table_prealloc_blocks(struct super_block *sb,
493 struct inode *table, uint16_t partition,
494 uint32_t first_block, uint32_t block_count)
496 struct udf_sb_info *sbi = UDF_SB(sb);
498 uint32_t elen, adsize;
499 struct kernel_lb_addr eloc;
500 struct extent_position epos;
502 struct udf_inode_info *iinfo;
505 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
508 iinfo = UDF_I(table);
509 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
510 adsize = sizeof(struct short_ad);
511 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
512 adsize = sizeof(struct long_ad);
516 mutex_lock(&sbi->s_alloc_mutex);
517 epos.offset = sizeof(struct unallocSpaceEntry);
518 epos.block = iinfo->i_location;
520 eloc.logicalBlockNum = 0xFFFFFFFF;
522 while (first_block != eloc.logicalBlockNum) {
523 ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
528 udf_debug("eloc=%u, elen=%u, first_block=%u\n",
529 eloc.logicalBlockNum, elen, first_block);
532 if (first_block == eloc.logicalBlockNum) {
533 epos.offset -= adsize;
535 alloc_count = (elen >> sb->s_blocksize_bits);
536 if (alloc_count > block_count) {
537 alloc_count = block_count;
538 eloc.logicalBlockNum += alloc_count;
539 elen -= (alloc_count << sb->s_blocksize_bits);
540 udf_write_aext(table, &epos, &eloc,
541 (etype << 30) | elen, 1);
543 udf_delete_aext(table, epos);
552 udf_add_free_space(sb, partition, -alloc_count);
553 mutex_unlock(&sbi->s_alloc_mutex);
557 static udf_pblk_t udf_table_new_block(struct super_block *sb,
558 struct inode *table, uint16_t partition,
559 uint32_t goal, int *err)
561 struct udf_sb_info *sbi = UDF_SB(sb);
562 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
563 udf_pblk_t newblock = 0;
565 uint32_t elen, goal_elen = 0;
566 struct kernel_lb_addr eloc, goal_eloc;
567 struct extent_position epos, goal_epos;
569 struct udf_inode_info *iinfo = UDF_I(table);
574 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
575 adsize = sizeof(struct short_ad);
576 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
577 adsize = sizeof(struct long_ad);
581 mutex_lock(&sbi->s_alloc_mutex);
582 if (goal >= sbi->s_partmaps[partition].s_partition_len)
585 /* We search for the closest matching block to goal. If we find
586 a exact hit, we stop. Otherwise we keep going till we run out
587 of extents. We store the buffer_head, bloc, and extoffset
588 of the current closest match and use that when we are done.
590 epos.offset = sizeof(struct unallocSpaceEntry);
591 epos.block = iinfo->i_location;
592 epos.bh = goal_epos.bh = NULL;
595 ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
598 if (goal >= eloc.logicalBlockNum) {
599 if (goal < eloc.logicalBlockNum +
600 (elen >> sb->s_blocksize_bits))
603 nspread = goal - eloc.logicalBlockNum -
604 (elen >> sb->s_blocksize_bits);
606 nspread = eloc.logicalBlockNum - goal;
609 if (nspread < spread) {
611 if (goal_epos.bh != epos.bh) {
612 brelse(goal_epos.bh);
613 goal_epos.bh = epos.bh;
614 get_bh(goal_epos.bh);
616 goal_epos.block = epos.block;
617 goal_epos.offset = epos.offset - adsize;
619 goal_elen = (etype << 30) | elen;
625 if (ret < 0 || spread == 0xFFFFFFFF) {
626 brelse(goal_epos.bh);
627 mutex_unlock(&sbi->s_alloc_mutex);
633 /* Only allocate blocks from the beginning of the extent.
634 That way, we only delete (empty) extents, never have to insert an
635 extent because of splitting */
636 /* This works, but very poorly.... */
638 newblock = goal_eloc.logicalBlockNum;
639 goal_eloc.logicalBlockNum++;
640 goal_elen -= sb->s_blocksize;
643 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
645 udf_delete_aext(table, goal_epos);
646 brelse(goal_epos.bh);
648 udf_add_free_space(sb, partition, -1);
650 mutex_unlock(&sbi->s_alloc_mutex);
655 void udf_free_blocks(struct super_block *sb, struct inode *inode,
656 struct kernel_lb_addr *bloc, uint32_t offset,
659 uint16_t partition = bloc->partitionReferenceNum;
660 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
663 if (check_add_overflow(bloc->logicalBlockNum, offset, &blk) ||
664 check_add_overflow(blk, count, &blk) ||
665 bloc->logicalBlockNum + count > map->s_partition_len) {
666 udf_debug("Invalid request to free blocks: (%d, %u), off %u, "
667 "len %u, partition len %u\n",
668 partition, bloc->logicalBlockNum, offset, count,
669 map->s_partition_len);
673 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
674 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
675 bloc, offset, count);
676 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
677 udf_table_free_blocks(sb, map->s_uspace.s_table,
678 bloc, offset, count);
682 inode_sub_bytes(inode,
683 ((sector_t)count) << sb->s_blocksize_bits);
687 inline int udf_prealloc_blocks(struct super_block *sb,
689 uint16_t partition, uint32_t first_block,
690 uint32_t block_count)
692 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
695 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
696 allocated = udf_bitmap_prealloc_blocks(sb,
697 map->s_uspace.s_bitmap,
698 partition, first_block,
700 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
701 allocated = udf_table_prealloc_blocks(sb,
702 map->s_uspace.s_table,
703 partition, first_block,
708 if (inode && allocated > 0)
709 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
713 inline udf_pblk_t udf_new_block(struct super_block *sb,
715 uint16_t partition, uint32_t goal, int *err)
717 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
720 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
721 block = udf_bitmap_new_block(sb,
722 map->s_uspace.s_bitmap,
723 partition, goal, err);
724 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
725 block = udf_table_new_block(sb,
726 map->s_uspace.s_table,
727 partition, goal, err);
733 inode_add_bytes(inode, sb->s_blocksize);