18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * balloc.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * PURPOSE 58c2ecf20Sopenharmony_ci * Block allocation handling routines for the OSTA-UDF(tm) filesystem. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * COPYRIGHT 88c2ecf20Sopenharmony_ci * This file is distributed under the terms of the GNU General Public 98c2ecf20Sopenharmony_ci * License (GPL). Copies of the GPL can be obtained from: 108c2ecf20Sopenharmony_ci * ftp://prep.ai.mit.edu/pub/gnu/GPL 118c2ecf20Sopenharmony_ci * Each contributing author retains all rights to their own work. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * (C) 1999-2001 Ben Fennema 148c2ecf20Sopenharmony_ci * (C) 1999 Stelias Computing Inc 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * HISTORY 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * 02/24/99 blf Created. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "udfdecl.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include <linux/bitops.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include "udf_i.h" 278c2ecf20Sopenharmony_ci#include "udf_sb.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define udf_clear_bit __test_and_clear_bit_le 308c2ecf20Sopenharmony_ci#define udf_set_bit __test_and_set_bit_le 318c2ecf20Sopenharmony_ci#define udf_test_bit test_bit_le 328c2ecf20Sopenharmony_ci#define udf_find_next_one_bit find_next_bit_le 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic int read_block_bitmap(struct super_block *sb, 358c2ecf20Sopenharmony_ci struct udf_bitmap *bitmap, unsigned int block, 368c2ecf20Sopenharmony_ci unsigned long bitmap_nr) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct buffer_head *bh = NULL; 398c2ecf20Sopenharmony_ci int i; 408c2ecf20Sopenharmony_ci int max_bits, off, count; 418c2ecf20Sopenharmony_ci struct kernel_lb_addr loc; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci loc.logicalBlockNum = bitmap->s_extPosition; 448c2ecf20Sopenharmony_ci loc.partitionReferenceNum = UDF_SB(sb)->s_partition; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block)); 478c2ecf20Sopenharmony_ci bitmap->s_block_bitmap[bitmap_nr] = bh; 488c2ecf20Sopenharmony_ci if (!bh) 498c2ecf20Sopenharmony_ci return -EIO; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci /* Check consistency of Space Bitmap buffer. */ 528c2ecf20Sopenharmony_ci max_bits = sb->s_blocksize * 8; 538c2ecf20Sopenharmony_ci if (!bitmap_nr) { 548c2ecf20Sopenharmony_ci off = sizeof(struct spaceBitmapDesc) << 3; 558c2ecf20Sopenharmony_ci count = min(max_bits - off, bitmap->s_nr_groups); 568c2ecf20Sopenharmony_ci } else { 578c2ecf20Sopenharmony_ci /* 588c2ecf20Sopenharmony_ci * Rough check if bitmap number is too big to have any bitmap 598c2ecf20Sopenharmony_ci * blocks reserved. 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_ci if (bitmap_nr > 628c2ecf20Sopenharmony_ci (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2) 638c2ecf20Sopenharmony_ci return 0; 648c2ecf20Sopenharmony_ci off = 0; 658c2ecf20Sopenharmony_ci count = bitmap->s_nr_groups - bitmap_nr * max_bits + 668c2ecf20Sopenharmony_ci (sizeof(struct spaceBitmapDesc) << 3); 678c2ecf20Sopenharmony_ci count = min(count, max_bits); 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) 718c2ecf20Sopenharmony_ci if (udf_test_bit(i + off, bh->b_data)) 728c2ecf20Sopenharmony_ci return -EFSCORRUPTED; 738c2ecf20Sopenharmony_ci return 0; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic int __load_block_bitmap(struct super_block *sb, 778c2ecf20Sopenharmony_ci struct udf_bitmap *bitmap, 788c2ecf20Sopenharmony_ci unsigned int block_group) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci int retval = 0; 818c2ecf20Sopenharmony_ci int nr_groups = bitmap->s_nr_groups; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (block_group >= nr_groups) { 848c2ecf20Sopenharmony_ci udf_debug("block_group (%u) > nr_groups (%d)\n", 858c2ecf20Sopenharmony_ci block_group, nr_groups); 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (bitmap->s_block_bitmap[block_group]) 898c2ecf20Sopenharmony_ci return block_group; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci retval = read_block_bitmap(sb, bitmap, block_group, block_group); 928c2ecf20Sopenharmony_ci if (retval < 0) 938c2ecf20Sopenharmony_ci return retval; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci return block_group; 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic inline int load_block_bitmap(struct super_block *sb, 998c2ecf20Sopenharmony_ci struct udf_bitmap *bitmap, 1008c2ecf20Sopenharmony_ci unsigned int block_group) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci int slot; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci slot = __load_block_bitmap(sb, bitmap, block_group); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci if (slot < 0) 1078c2ecf20Sopenharmony_ci return slot; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci if (!bitmap->s_block_bitmap[slot]) 1108c2ecf20Sopenharmony_ci return -EIO; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci return slot; 1138c2ecf20Sopenharmony_ci} 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_cistatic void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 1188c2ecf20Sopenharmony_ci struct logicalVolIntegrityDesc *lvid; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (!sbi->s_lvid_bh) 1218c2ecf20Sopenharmony_ci return; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data; 1248c2ecf20Sopenharmony_ci le32_add_cpu(&lvid->freeSpaceTable[partition], cnt); 1258c2ecf20Sopenharmony_ci udf_updated_lvid(sb); 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic void udf_bitmap_free_blocks(struct super_block *sb, 1298c2ecf20Sopenharmony_ci struct udf_bitmap *bitmap, 1308c2ecf20Sopenharmony_ci struct kernel_lb_addr *bloc, 1318c2ecf20Sopenharmony_ci uint32_t offset, 1328c2ecf20Sopenharmony_ci uint32_t count) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 1358c2ecf20Sopenharmony_ci struct buffer_head *bh = NULL; 1368c2ecf20Sopenharmony_ci struct udf_part_map *partmap; 1378c2ecf20Sopenharmony_ci unsigned long block; 1388c2ecf20Sopenharmony_ci unsigned long block_group; 1398c2ecf20Sopenharmony_ci unsigned long bit; 1408c2ecf20Sopenharmony_ci unsigned long i; 1418c2ecf20Sopenharmony_ci int bitmap_nr; 1428c2ecf20Sopenharmony_ci unsigned long overflow; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci mutex_lock(&sbi->s_alloc_mutex); 1458c2ecf20Sopenharmony_ci partmap = &sbi->s_partmaps[bloc->partitionReferenceNum]; 1468c2ecf20Sopenharmony_ci if (bloc->logicalBlockNum + count < count || 1478c2ecf20Sopenharmony_ci (bloc->logicalBlockNum + count) > partmap->s_partition_len) { 1488c2ecf20Sopenharmony_ci udf_debug("%u < %d || %u + %u > %u\n", 1498c2ecf20Sopenharmony_ci bloc->logicalBlockNum, 0, 1508c2ecf20Sopenharmony_ci bloc->logicalBlockNum, count, 1518c2ecf20Sopenharmony_ci partmap->s_partition_len); 1528c2ecf20Sopenharmony_ci goto error_return; 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci block = bloc->logicalBlockNum + offset + 1568c2ecf20Sopenharmony_ci (sizeof(struct spaceBitmapDesc) << 3); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci do { 1598c2ecf20Sopenharmony_ci overflow = 0; 1608c2ecf20Sopenharmony_ci block_group = block >> (sb->s_blocksize_bits + 3); 1618c2ecf20Sopenharmony_ci bit = block % (sb->s_blocksize << 3); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* 1648c2ecf20Sopenharmony_ci * Check to see if we are freeing blocks across a group boundary. 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ci if (bit + count > (sb->s_blocksize << 3)) { 1678c2ecf20Sopenharmony_ci overflow = bit + count - (sb->s_blocksize << 3); 1688c2ecf20Sopenharmony_ci count -= overflow; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci bitmap_nr = load_block_bitmap(sb, bitmap, block_group); 1718c2ecf20Sopenharmony_ci if (bitmap_nr < 0) 1728c2ecf20Sopenharmony_ci goto error_return; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci bh = bitmap->s_block_bitmap[bitmap_nr]; 1758c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) { 1768c2ecf20Sopenharmony_ci if (udf_set_bit(bit + i, bh->b_data)) { 1778c2ecf20Sopenharmony_ci udf_debug("bit %lu already set\n", bit + i); 1788c2ecf20Sopenharmony_ci udf_debug("byte=%2x\n", 1798c2ecf20Sopenharmony_ci ((__u8 *)bh->b_data)[(bit + i) >> 3]); 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci udf_add_free_space(sb, sbi->s_partition, count); 1838c2ecf20Sopenharmony_ci mark_buffer_dirty(bh); 1848c2ecf20Sopenharmony_ci if (overflow) { 1858c2ecf20Sopenharmony_ci block += count; 1868c2ecf20Sopenharmony_ci count = overflow; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci } while (overflow); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cierror_return: 1918c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic int udf_bitmap_prealloc_blocks(struct super_block *sb, 1958c2ecf20Sopenharmony_ci struct udf_bitmap *bitmap, 1968c2ecf20Sopenharmony_ci uint16_t partition, uint32_t first_block, 1978c2ecf20Sopenharmony_ci uint32_t block_count) 1988c2ecf20Sopenharmony_ci{ 1998c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 2008c2ecf20Sopenharmony_ci int alloc_count = 0; 2018c2ecf20Sopenharmony_ci int bit, block, block_group; 2028c2ecf20Sopenharmony_ci int bitmap_nr; 2038c2ecf20Sopenharmony_ci struct buffer_head *bh; 2048c2ecf20Sopenharmony_ci __u32 part_len; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci mutex_lock(&sbi->s_alloc_mutex); 2078c2ecf20Sopenharmony_ci part_len = sbi->s_partmaps[partition].s_partition_len; 2088c2ecf20Sopenharmony_ci if (first_block >= part_len) 2098c2ecf20Sopenharmony_ci goto out; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (first_block + block_count > part_len) 2128c2ecf20Sopenharmony_ci block_count = part_len - first_block; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci do { 2158c2ecf20Sopenharmony_ci block = first_block + (sizeof(struct spaceBitmapDesc) << 3); 2168c2ecf20Sopenharmony_ci block_group = block >> (sb->s_blocksize_bits + 3); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci bitmap_nr = load_block_bitmap(sb, bitmap, block_group); 2198c2ecf20Sopenharmony_ci if (bitmap_nr < 0) 2208c2ecf20Sopenharmony_ci goto out; 2218c2ecf20Sopenharmony_ci bh = bitmap->s_block_bitmap[bitmap_nr]; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci bit = block % (sb->s_blocksize << 3); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci while (bit < (sb->s_blocksize << 3) && block_count > 0) { 2268c2ecf20Sopenharmony_ci if (!udf_clear_bit(bit, bh->b_data)) 2278c2ecf20Sopenharmony_ci goto out; 2288c2ecf20Sopenharmony_ci block_count--; 2298c2ecf20Sopenharmony_ci alloc_count++; 2308c2ecf20Sopenharmony_ci bit++; 2318c2ecf20Sopenharmony_ci block++; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci mark_buffer_dirty(bh); 2348c2ecf20Sopenharmony_ci } while (block_count > 0); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ciout: 2378c2ecf20Sopenharmony_ci udf_add_free_space(sb, partition, -alloc_count); 2388c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 2398c2ecf20Sopenharmony_ci return alloc_count; 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_cistatic udf_pblk_t udf_bitmap_new_block(struct super_block *sb, 2438c2ecf20Sopenharmony_ci struct udf_bitmap *bitmap, uint16_t partition, 2448c2ecf20Sopenharmony_ci uint32_t goal, int *err) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 2478c2ecf20Sopenharmony_ci int newbit, bit = 0; 2488c2ecf20Sopenharmony_ci udf_pblk_t block; 2498c2ecf20Sopenharmony_ci int block_group, group_start; 2508c2ecf20Sopenharmony_ci int end_goal, nr_groups, bitmap_nr, i; 2518c2ecf20Sopenharmony_ci struct buffer_head *bh = NULL; 2528c2ecf20Sopenharmony_ci char *ptr; 2538c2ecf20Sopenharmony_ci udf_pblk_t newblock = 0; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci *err = -ENOSPC; 2568c2ecf20Sopenharmony_ci mutex_lock(&sbi->s_alloc_mutex); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_cirepeat: 2598c2ecf20Sopenharmony_ci if (goal >= sbi->s_partmaps[partition].s_partition_len) 2608c2ecf20Sopenharmony_ci goal = 0; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci nr_groups = bitmap->s_nr_groups; 2638c2ecf20Sopenharmony_ci block = goal + (sizeof(struct spaceBitmapDesc) << 3); 2648c2ecf20Sopenharmony_ci block_group = block >> (sb->s_blocksize_bits + 3); 2658c2ecf20Sopenharmony_ci group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci bitmap_nr = load_block_bitmap(sb, bitmap, block_group); 2688c2ecf20Sopenharmony_ci if (bitmap_nr < 0) 2698c2ecf20Sopenharmony_ci goto error_return; 2708c2ecf20Sopenharmony_ci bh = bitmap->s_block_bitmap[bitmap_nr]; 2718c2ecf20Sopenharmony_ci ptr = memscan((char *)bh->b_data + group_start, 0xFF, 2728c2ecf20Sopenharmony_ci sb->s_blocksize - group_start); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { 2758c2ecf20Sopenharmony_ci bit = block % (sb->s_blocksize << 3); 2768c2ecf20Sopenharmony_ci if (udf_test_bit(bit, bh->b_data)) 2778c2ecf20Sopenharmony_ci goto got_block; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci end_goal = (bit + 63) & ~63; 2808c2ecf20Sopenharmony_ci bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); 2818c2ecf20Sopenharmony_ci if (bit < end_goal) 2828c2ecf20Sopenharmony_ci goto got_block; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, 2858c2ecf20Sopenharmony_ci sb->s_blocksize - ((bit + 7) >> 3)); 2868c2ecf20Sopenharmony_ci newbit = (ptr - ((char *)bh->b_data)) << 3; 2878c2ecf20Sopenharmony_ci if (newbit < sb->s_blocksize << 3) { 2888c2ecf20Sopenharmony_ci bit = newbit; 2898c2ecf20Sopenharmony_ci goto search_back; 2908c2ecf20Sopenharmony_ci } 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci newbit = udf_find_next_one_bit(bh->b_data, 2938c2ecf20Sopenharmony_ci sb->s_blocksize << 3, bit); 2948c2ecf20Sopenharmony_ci if (newbit < sb->s_blocksize << 3) { 2958c2ecf20Sopenharmony_ci bit = newbit; 2968c2ecf20Sopenharmony_ci goto got_block; 2978c2ecf20Sopenharmony_ci } 2988c2ecf20Sopenharmony_ci } 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci for (i = 0; i < (nr_groups * 2); i++) { 3018c2ecf20Sopenharmony_ci block_group++; 3028c2ecf20Sopenharmony_ci if (block_group >= nr_groups) 3038c2ecf20Sopenharmony_ci block_group = 0; 3048c2ecf20Sopenharmony_ci group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci bitmap_nr = load_block_bitmap(sb, bitmap, block_group); 3078c2ecf20Sopenharmony_ci if (bitmap_nr < 0) 3088c2ecf20Sopenharmony_ci goto error_return; 3098c2ecf20Sopenharmony_ci bh = bitmap->s_block_bitmap[bitmap_nr]; 3108c2ecf20Sopenharmony_ci if (i < nr_groups) { 3118c2ecf20Sopenharmony_ci ptr = memscan((char *)bh->b_data + group_start, 0xFF, 3128c2ecf20Sopenharmony_ci sb->s_blocksize - group_start); 3138c2ecf20Sopenharmony_ci if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { 3148c2ecf20Sopenharmony_ci bit = (ptr - ((char *)bh->b_data)) << 3; 3158c2ecf20Sopenharmony_ci break; 3168c2ecf20Sopenharmony_ci } 3178c2ecf20Sopenharmony_ci } else { 3188c2ecf20Sopenharmony_ci bit = udf_find_next_one_bit(bh->b_data, 3198c2ecf20Sopenharmony_ci sb->s_blocksize << 3, 3208c2ecf20Sopenharmony_ci group_start << 3); 3218c2ecf20Sopenharmony_ci if (bit < sb->s_blocksize << 3) 3228c2ecf20Sopenharmony_ci break; 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci } 3258c2ecf20Sopenharmony_ci if (i >= (nr_groups * 2)) { 3268c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 3278c2ecf20Sopenharmony_ci return newblock; 3288c2ecf20Sopenharmony_ci } 3298c2ecf20Sopenharmony_ci if (bit < sb->s_blocksize << 3) 3308c2ecf20Sopenharmony_ci goto search_back; 3318c2ecf20Sopenharmony_ci else 3328c2ecf20Sopenharmony_ci bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, 3338c2ecf20Sopenharmony_ci group_start << 3); 3348c2ecf20Sopenharmony_ci if (bit >= sb->s_blocksize << 3) { 3358c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 3368c2ecf20Sopenharmony_ci return 0; 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cisearch_back: 3408c2ecf20Sopenharmony_ci i = 0; 3418c2ecf20Sopenharmony_ci while (i < 7 && bit > (group_start << 3) && 3428c2ecf20Sopenharmony_ci udf_test_bit(bit - 1, bh->b_data)) { 3438c2ecf20Sopenharmony_ci ++i; 3448c2ecf20Sopenharmony_ci --bit; 3458c2ecf20Sopenharmony_ci } 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_cigot_block: 3488c2ecf20Sopenharmony_ci newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - 3498c2ecf20Sopenharmony_ci (sizeof(struct spaceBitmapDesc) << 3); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci if (newblock >= sbi->s_partmaps[partition].s_partition_len) { 3528c2ecf20Sopenharmony_ci /* 3538c2ecf20Sopenharmony_ci * Ran off the end of the bitmap, and bits following are 3548c2ecf20Sopenharmony_ci * non-compliant (not all zero) 3558c2ecf20Sopenharmony_ci */ 3568c2ecf20Sopenharmony_ci udf_err(sb, "bitmap for partition %d corrupted (block %u marked" 3578c2ecf20Sopenharmony_ci " as free, partition length is %u)\n", partition, 3588c2ecf20Sopenharmony_ci newblock, sbi->s_partmaps[partition].s_partition_len); 3598c2ecf20Sopenharmony_ci goto error_return; 3608c2ecf20Sopenharmony_ci } 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci if (!udf_clear_bit(bit, bh->b_data)) { 3638c2ecf20Sopenharmony_ci udf_debug("bit already cleared for block %d\n", bit); 3648c2ecf20Sopenharmony_ci goto repeat; 3658c2ecf20Sopenharmony_ci } 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci mark_buffer_dirty(bh); 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci udf_add_free_space(sb, partition, -1); 3708c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 3718c2ecf20Sopenharmony_ci *err = 0; 3728c2ecf20Sopenharmony_ci return newblock; 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cierror_return: 3758c2ecf20Sopenharmony_ci *err = -EIO; 3768c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 3778c2ecf20Sopenharmony_ci return 0; 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic void udf_table_free_blocks(struct super_block *sb, 3818c2ecf20Sopenharmony_ci struct inode *table, 3828c2ecf20Sopenharmony_ci struct kernel_lb_addr *bloc, 3838c2ecf20Sopenharmony_ci uint32_t offset, 3848c2ecf20Sopenharmony_ci uint32_t count) 3858c2ecf20Sopenharmony_ci{ 3868c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 3878c2ecf20Sopenharmony_ci struct udf_part_map *partmap; 3888c2ecf20Sopenharmony_ci uint32_t start, end; 3898c2ecf20Sopenharmony_ci uint32_t elen; 3908c2ecf20Sopenharmony_ci struct kernel_lb_addr eloc; 3918c2ecf20Sopenharmony_ci struct extent_position oepos, epos; 3928c2ecf20Sopenharmony_ci int8_t etype; 3938c2ecf20Sopenharmony_ci struct udf_inode_info *iinfo; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci mutex_lock(&sbi->s_alloc_mutex); 3968c2ecf20Sopenharmony_ci partmap = &sbi->s_partmaps[bloc->partitionReferenceNum]; 3978c2ecf20Sopenharmony_ci if (bloc->logicalBlockNum + count < count || 3988c2ecf20Sopenharmony_ci (bloc->logicalBlockNum + count) > partmap->s_partition_len) { 3998c2ecf20Sopenharmony_ci udf_debug("%u < %d || %u + %u > %u\n", 4008c2ecf20Sopenharmony_ci bloc->logicalBlockNum, 0, 4018c2ecf20Sopenharmony_ci bloc->logicalBlockNum, count, 4028c2ecf20Sopenharmony_ci partmap->s_partition_len); 4038c2ecf20Sopenharmony_ci goto error_return; 4048c2ecf20Sopenharmony_ci } 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci iinfo = UDF_I(table); 4078c2ecf20Sopenharmony_ci udf_add_free_space(sb, sbi->s_partition, count); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci start = bloc->logicalBlockNum + offset; 4108c2ecf20Sopenharmony_ci end = bloc->logicalBlockNum + offset + count - 1; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); 4138c2ecf20Sopenharmony_ci elen = 0; 4148c2ecf20Sopenharmony_ci epos.block = oepos.block = iinfo->i_location; 4158c2ecf20Sopenharmony_ci epos.bh = oepos.bh = NULL; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci while (count && 4188c2ecf20Sopenharmony_ci (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { 4198c2ecf20Sopenharmony_ci if (((eloc.logicalBlockNum + 4208c2ecf20Sopenharmony_ci (elen >> sb->s_blocksize_bits)) == start)) { 4218c2ecf20Sopenharmony_ci if ((0x3FFFFFFF - elen) < 4228c2ecf20Sopenharmony_ci (count << sb->s_blocksize_bits)) { 4238c2ecf20Sopenharmony_ci uint32_t tmp = ((0x3FFFFFFF - elen) >> 4248c2ecf20Sopenharmony_ci sb->s_blocksize_bits); 4258c2ecf20Sopenharmony_ci count -= tmp; 4268c2ecf20Sopenharmony_ci start += tmp; 4278c2ecf20Sopenharmony_ci elen = (etype << 30) | 4288c2ecf20Sopenharmony_ci (0x40000000 - sb->s_blocksize); 4298c2ecf20Sopenharmony_ci } else { 4308c2ecf20Sopenharmony_ci elen = (etype << 30) | 4318c2ecf20Sopenharmony_ci (elen + 4328c2ecf20Sopenharmony_ci (count << sb->s_blocksize_bits)); 4338c2ecf20Sopenharmony_ci start += count; 4348c2ecf20Sopenharmony_ci count = 0; 4358c2ecf20Sopenharmony_ci } 4368c2ecf20Sopenharmony_ci udf_write_aext(table, &oepos, &eloc, elen, 1); 4378c2ecf20Sopenharmony_ci } else if (eloc.logicalBlockNum == (end + 1)) { 4388c2ecf20Sopenharmony_ci if ((0x3FFFFFFF - elen) < 4398c2ecf20Sopenharmony_ci (count << sb->s_blocksize_bits)) { 4408c2ecf20Sopenharmony_ci uint32_t tmp = ((0x3FFFFFFF - elen) >> 4418c2ecf20Sopenharmony_ci sb->s_blocksize_bits); 4428c2ecf20Sopenharmony_ci count -= tmp; 4438c2ecf20Sopenharmony_ci end -= tmp; 4448c2ecf20Sopenharmony_ci eloc.logicalBlockNum -= tmp; 4458c2ecf20Sopenharmony_ci elen = (etype << 30) | 4468c2ecf20Sopenharmony_ci (0x40000000 - sb->s_blocksize); 4478c2ecf20Sopenharmony_ci } else { 4488c2ecf20Sopenharmony_ci eloc.logicalBlockNum = start; 4498c2ecf20Sopenharmony_ci elen = (etype << 30) | 4508c2ecf20Sopenharmony_ci (elen + 4518c2ecf20Sopenharmony_ci (count << sb->s_blocksize_bits)); 4528c2ecf20Sopenharmony_ci end -= count; 4538c2ecf20Sopenharmony_ci count = 0; 4548c2ecf20Sopenharmony_ci } 4558c2ecf20Sopenharmony_ci udf_write_aext(table, &oepos, &eloc, elen, 1); 4568c2ecf20Sopenharmony_ci } 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci if (epos.bh != oepos.bh) { 4598c2ecf20Sopenharmony_ci oepos.block = epos.block; 4608c2ecf20Sopenharmony_ci brelse(oepos.bh); 4618c2ecf20Sopenharmony_ci get_bh(epos.bh); 4628c2ecf20Sopenharmony_ci oepos.bh = epos.bh; 4638c2ecf20Sopenharmony_ci oepos.offset = 0; 4648c2ecf20Sopenharmony_ci } else { 4658c2ecf20Sopenharmony_ci oepos.offset = epos.offset; 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci } 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci if (count) { 4708c2ecf20Sopenharmony_ci /* 4718c2ecf20Sopenharmony_ci * NOTE: we CANNOT use udf_add_aext here, as it can try to 4728c2ecf20Sopenharmony_ci * allocate a new block, and since we hold the super block 4738c2ecf20Sopenharmony_ci * lock already very bad things would happen :) 4748c2ecf20Sopenharmony_ci * 4758c2ecf20Sopenharmony_ci * We copy the behavior of udf_add_aext, but instead of 4768c2ecf20Sopenharmony_ci * trying to allocate a new block close to the existing one, 4778c2ecf20Sopenharmony_ci * we just steal a block from the extent we are trying to add. 4788c2ecf20Sopenharmony_ci * 4798c2ecf20Sopenharmony_ci * It would be nice if the blocks were close together, but it 4808c2ecf20Sopenharmony_ci * isn't required. 4818c2ecf20Sopenharmony_ci */ 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci int adsize; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci eloc.logicalBlockNum = start; 4868c2ecf20Sopenharmony_ci elen = EXT_RECORDED_ALLOCATED | 4878c2ecf20Sopenharmony_ci (count << sb->s_blocksize_bits); 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 4908c2ecf20Sopenharmony_ci adsize = sizeof(struct short_ad); 4918c2ecf20Sopenharmony_ci else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 4928c2ecf20Sopenharmony_ci adsize = sizeof(struct long_ad); 4938c2ecf20Sopenharmony_ci else { 4948c2ecf20Sopenharmony_ci brelse(oepos.bh); 4958c2ecf20Sopenharmony_ci brelse(epos.bh); 4968c2ecf20Sopenharmony_ci goto error_return; 4978c2ecf20Sopenharmony_ci } 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci if (epos.offset + (2 * adsize) > sb->s_blocksize) { 5008c2ecf20Sopenharmony_ci /* Steal a block from the extent being free'd */ 5018c2ecf20Sopenharmony_ci udf_setup_indirect_aext(table, eloc.logicalBlockNum, 5028c2ecf20Sopenharmony_ci &epos); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci eloc.logicalBlockNum++; 5058c2ecf20Sopenharmony_ci elen -= sb->s_blocksize; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci /* It's possible that stealing the block emptied the extent */ 5098c2ecf20Sopenharmony_ci if (elen) 5108c2ecf20Sopenharmony_ci __udf_add_aext(table, &epos, &eloc, elen, 1); 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci brelse(epos.bh); 5148c2ecf20Sopenharmony_ci brelse(oepos.bh); 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_cierror_return: 5178c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 5188c2ecf20Sopenharmony_ci return; 5198c2ecf20Sopenharmony_ci} 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_cistatic int udf_table_prealloc_blocks(struct super_block *sb, 5228c2ecf20Sopenharmony_ci struct inode *table, uint16_t partition, 5238c2ecf20Sopenharmony_ci uint32_t first_block, uint32_t block_count) 5248c2ecf20Sopenharmony_ci{ 5258c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 5268c2ecf20Sopenharmony_ci int alloc_count = 0; 5278c2ecf20Sopenharmony_ci uint32_t elen, adsize; 5288c2ecf20Sopenharmony_ci struct kernel_lb_addr eloc; 5298c2ecf20Sopenharmony_ci struct extent_position epos; 5308c2ecf20Sopenharmony_ci int8_t etype = -1; 5318c2ecf20Sopenharmony_ci struct udf_inode_info *iinfo; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci if (first_block >= sbi->s_partmaps[partition].s_partition_len) 5348c2ecf20Sopenharmony_ci return 0; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci iinfo = UDF_I(table); 5378c2ecf20Sopenharmony_ci if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 5388c2ecf20Sopenharmony_ci adsize = sizeof(struct short_ad); 5398c2ecf20Sopenharmony_ci else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 5408c2ecf20Sopenharmony_ci adsize = sizeof(struct long_ad); 5418c2ecf20Sopenharmony_ci else 5428c2ecf20Sopenharmony_ci return 0; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci mutex_lock(&sbi->s_alloc_mutex); 5458c2ecf20Sopenharmony_ci epos.offset = sizeof(struct unallocSpaceEntry); 5468c2ecf20Sopenharmony_ci epos.block = iinfo->i_location; 5478c2ecf20Sopenharmony_ci epos.bh = NULL; 5488c2ecf20Sopenharmony_ci eloc.logicalBlockNum = 0xFFFFFFFF; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci while (first_block != eloc.logicalBlockNum && 5518c2ecf20Sopenharmony_ci (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { 5528c2ecf20Sopenharmony_ci udf_debug("eloc=%u, elen=%u, first_block=%u\n", 5538c2ecf20Sopenharmony_ci eloc.logicalBlockNum, elen, first_block); 5548c2ecf20Sopenharmony_ci ; /* empty loop body */ 5558c2ecf20Sopenharmony_ci } 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_ci if (first_block == eloc.logicalBlockNum) { 5588c2ecf20Sopenharmony_ci epos.offset -= adsize; 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci alloc_count = (elen >> sb->s_blocksize_bits); 5618c2ecf20Sopenharmony_ci if (alloc_count > block_count) { 5628c2ecf20Sopenharmony_ci alloc_count = block_count; 5638c2ecf20Sopenharmony_ci eloc.logicalBlockNum += alloc_count; 5648c2ecf20Sopenharmony_ci elen -= (alloc_count << sb->s_blocksize_bits); 5658c2ecf20Sopenharmony_ci udf_write_aext(table, &epos, &eloc, 5668c2ecf20Sopenharmony_ci (etype << 30) | elen, 1); 5678c2ecf20Sopenharmony_ci } else 5688c2ecf20Sopenharmony_ci udf_delete_aext(table, epos); 5698c2ecf20Sopenharmony_ci } else { 5708c2ecf20Sopenharmony_ci alloc_count = 0; 5718c2ecf20Sopenharmony_ci } 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci brelse(epos.bh); 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci if (alloc_count) 5768c2ecf20Sopenharmony_ci udf_add_free_space(sb, partition, -alloc_count); 5778c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 5788c2ecf20Sopenharmony_ci return alloc_count; 5798c2ecf20Sopenharmony_ci} 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_cistatic udf_pblk_t udf_table_new_block(struct super_block *sb, 5828c2ecf20Sopenharmony_ci struct inode *table, uint16_t partition, 5838c2ecf20Sopenharmony_ci uint32_t goal, int *err) 5848c2ecf20Sopenharmony_ci{ 5858c2ecf20Sopenharmony_ci struct udf_sb_info *sbi = UDF_SB(sb); 5868c2ecf20Sopenharmony_ci uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; 5878c2ecf20Sopenharmony_ci udf_pblk_t newblock = 0; 5888c2ecf20Sopenharmony_ci uint32_t adsize; 5898c2ecf20Sopenharmony_ci uint32_t elen, goal_elen = 0; 5908c2ecf20Sopenharmony_ci struct kernel_lb_addr eloc, goal_eloc; 5918c2ecf20Sopenharmony_ci struct extent_position epos, goal_epos; 5928c2ecf20Sopenharmony_ci int8_t etype; 5938c2ecf20Sopenharmony_ci struct udf_inode_info *iinfo = UDF_I(table); 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci *err = -ENOSPC; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 5988c2ecf20Sopenharmony_ci adsize = sizeof(struct short_ad); 5998c2ecf20Sopenharmony_ci else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 6008c2ecf20Sopenharmony_ci adsize = sizeof(struct long_ad); 6018c2ecf20Sopenharmony_ci else 6028c2ecf20Sopenharmony_ci return newblock; 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_ci mutex_lock(&sbi->s_alloc_mutex); 6058c2ecf20Sopenharmony_ci if (goal >= sbi->s_partmaps[partition].s_partition_len) 6068c2ecf20Sopenharmony_ci goal = 0; 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci /* We search for the closest matching block to goal. If we find 6098c2ecf20Sopenharmony_ci a exact hit, we stop. Otherwise we keep going till we run out 6108c2ecf20Sopenharmony_ci of extents. We store the buffer_head, bloc, and extoffset 6118c2ecf20Sopenharmony_ci of the current closest match and use that when we are done. 6128c2ecf20Sopenharmony_ci */ 6138c2ecf20Sopenharmony_ci epos.offset = sizeof(struct unallocSpaceEntry); 6148c2ecf20Sopenharmony_ci epos.block = iinfo->i_location; 6158c2ecf20Sopenharmony_ci epos.bh = goal_epos.bh = NULL; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci while (spread && 6188c2ecf20Sopenharmony_ci (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { 6198c2ecf20Sopenharmony_ci if (goal >= eloc.logicalBlockNum) { 6208c2ecf20Sopenharmony_ci if (goal < eloc.logicalBlockNum + 6218c2ecf20Sopenharmony_ci (elen >> sb->s_blocksize_bits)) 6228c2ecf20Sopenharmony_ci nspread = 0; 6238c2ecf20Sopenharmony_ci else 6248c2ecf20Sopenharmony_ci nspread = goal - eloc.logicalBlockNum - 6258c2ecf20Sopenharmony_ci (elen >> sb->s_blocksize_bits); 6268c2ecf20Sopenharmony_ci } else { 6278c2ecf20Sopenharmony_ci nspread = eloc.logicalBlockNum - goal; 6288c2ecf20Sopenharmony_ci } 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci if (nspread < spread) { 6318c2ecf20Sopenharmony_ci spread = nspread; 6328c2ecf20Sopenharmony_ci if (goal_epos.bh != epos.bh) { 6338c2ecf20Sopenharmony_ci brelse(goal_epos.bh); 6348c2ecf20Sopenharmony_ci goal_epos.bh = epos.bh; 6358c2ecf20Sopenharmony_ci get_bh(goal_epos.bh); 6368c2ecf20Sopenharmony_ci } 6378c2ecf20Sopenharmony_ci goal_epos.block = epos.block; 6388c2ecf20Sopenharmony_ci goal_epos.offset = epos.offset - adsize; 6398c2ecf20Sopenharmony_ci goal_eloc = eloc; 6408c2ecf20Sopenharmony_ci goal_elen = (etype << 30) | elen; 6418c2ecf20Sopenharmony_ci } 6428c2ecf20Sopenharmony_ci } 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci brelse(epos.bh); 6458c2ecf20Sopenharmony_ci 6468c2ecf20Sopenharmony_ci if (spread == 0xFFFFFFFF) { 6478c2ecf20Sopenharmony_ci brelse(goal_epos.bh); 6488c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 6498c2ecf20Sopenharmony_ci return 0; 6508c2ecf20Sopenharmony_ci } 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci /* Only allocate blocks from the beginning of the extent. 6538c2ecf20Sopenharmony_ci That way, we only delete (empty) extents, never have to insert an 6548c2ecf20Sopenharmony_ci extent because of splitting */ 6558c2ecf20Sopenharmony_ci /* This works, but very poorly.... */ 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci newblock = goal_eloc.logicalBlockNum; 6588c2ecf20Sopenharmony_ci goal_eloc.logicalBlockNum++; 6598c2ecf20Sopenharmony_ci goal_elen -= sb->s_blocksize; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci if (goal_elen) 6628c2ecf20Sopenharmony_ci udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1); 6638c2ecf20Sopenharmony_ci else 6648c2ecf20Sopenharmony_ci udf_delete_aext(table, goal_epos); 6658c2ecf20Sopenharmony_ci brelse(goal_epos.bh); 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci udf_add_free_space(sb, partition, -1); 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci mutex_unlock(&sbi->s_alloc_mutex); 6708c2ecf20Sopenharmony_ci *err = 0; 6718c2ecf20Sopenharmony_ci return newblock; 6728c2ecf20Sopenharmony_ci} 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_civoid udf_free_blocks(struct super_block *sb, struct inode *inode, 6758c2ecf20Sopenharmony_ci struct kernel_lb_addr *bloc, uint32_t offset, 6768c2ecf20Sopenharmony_ci uint32_t count) 6778c2ecf20Sopenharmony_ci{ 6788c2ecf20Sopenharmony_ci uint16_t partition = bloc->partitionReferenceNum; 6798c2ecf20Sopenharmony_ci struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { 6828c2ecf20Sopenharmony_ci udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap, 6838c2ecf20Sopenharmony_ci bloc, offset, count); 6848c2ecf20Sopenharmony_ci } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { 6858c2ecf20Sopenharmony_ci udf_table_free_blocks(sb, map->s_uspace.s_table, 6868c2ecf20Sopenharmony_ci bloc, offset, count); 6878c2ecf20Sopenharmony_ci } 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci if (inode) { 6908c2ecf20Sopenharmony_ci inode_sub_bytes(inode, 6918c2ecf20Sopenharmony_ci ((sector_t)count) << sb->s_blocksize_bits); 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ci} 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ciinline int udf_prealloc_blocks(struct super_block *sb, 6968c2ecf20Sopenharmony_ci struct inode *inode, 6978c2ecf20Sopenharmony_ci uint16_t partition, uint32_t first_block, 6988c2ecf20Sopenharmony_ci uint32_t block_count) 6998c2ecf20Sopenharmony_ci{ 7008c2ecf20Sopenharmony_ci struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; 7018c2ecf20Sopenharmony_ci int allocated; 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) 7048c2ecf20Sopenharmony_ci allocated = udf_bitmap_prealloc_blocks(sb, 7058c2ecf20Sopenharmony_ci map->s_uspace.s_bitmap, 7068c2ecf20Sopenharmony_ci partition, first_block, 7078c2ecf20Sopenharmony_ci block_count); 7088c2ecf20Sopenharmony_ci else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) 7098c2ecf20Sopenharmony_ci allocated = udf_table_prealloc_blocks(sb, 7108c2ecf20Sopenharmony_ci map->s_uspace.s_table, 7118c2ecf20Sopenharmony_ci partition, first_block, 7128c2ecf20Sopenharmony_ci block_count); 7138c2ecf20Sopenharmony_ci else 7148c2ecf20Sopenharmony_ci return 0; 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci if (inode && allocated > 0) 7178c2ecf20Sopenharmony_ci inode_add_bytes(inode, allocated << sb->s_blocksize_bits); 7188c2ecf20Sopenharmony_ci return allocated; 7198c2ecf20Sopenharmony_ci} 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ciinline udf_pblk_t udf_new_block(struct super_block *sb, 7228c2ecf20Sopenharmony_ci struct inode *inode, 7238c2ecf20Sopenharmony_ci uint16_t partition, uint32_t goal, int *err) 7248c2ecf20Sopenharmony_ci{ 7258c2ecf20Sopenharmony_ci struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; 7268c2ecf20Sopenharmony_ci udf_pblk_t block; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) 7298c2ecf20Sopenharmony_ci block = udf_bitmap_new_block(sb, 7308c2ecf20Sopenharmony_ci map->s_uspace.s_bitmap, 7318c2ecf20Sopenharmony_ci partition, goal, err); 7328c2ecf20Sopenharmony_ci else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) 7338c2ecf20Sopenharmony_ci block = udf_table_new_block(sb, 7348c2ecf20Sopenharmony_ci map->s_uspace.s_table, 7358c2ecf20Sopenharmony_ci partition, goal, err); 7368c2ecf20Sopenharmony_ci else { 7378c2ecf20Sopenharmony_ci *err = -EIO; 7388c2ecf20Sopenharmony_ci return 0; 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci if (inode && block) 7418c2ecf20Sopenharmony_ci inode_add_bytes(inode, sb->s_blocksize); 7428c2ecf20Sopenharmony_ci return block; 7438c2ecf20Sopenharmony_ci} 744