Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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