| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * balloc.c | 
 | 3 |  * | 
 | 4 |  * PURPOSE | 
 | 5 |  *	Block allocation handling routines for the OSTA-UDF(tm) filesystem. | 
 | 6 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * COPYRIGHT | 
 | 8 |  *	This file is distributed under the terms of the GNU General Public | 
 | 9 |  *	License (GPL). Copies of the GPL can be obtained from: | 
 | 10 |  *		ftp://prep.ai.mit.edu/pub/gnu/GPL | 
 | 11 |  *	Each contributing author retains all rights to their own work. | 
 | 12 |  * | 
 | 13 |  *  (C) 1999-2001 Ben Fennema | 
 | 14 |  *  (C) 1999 Stelias Computing Inc | 
 | 15 |  * | 
 | 16 |  * HISTORY | 
 | 17 |  * | 
 | 18 |  *  02/24/99 blf  Created. | 
 | 19 |  * | 
 | 20 |  */ | 
 | 21 |  | 
 | 22 | #include "udfdecl.h" | 
 | 23 |  | 
 | 24 | #include <linux/quotaops.h> | 
 | 25 | #include <linux/buffer_head.h> | 
 | 26 | #include <linux/bitops.h> | 
 | 27 |  | 
 | 28 | #include "udf_i.h" | 
 | 29 | #include "udf_sb.h" | 
 | 30 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 31 | #define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr) | 
 | 32 | #define udf_set_bit(nr, addr) ext2_set_bit(nr, addr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr) | 
 | 34 | #define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size) | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 35 | #define udf_find_next_one_bit(addr, size, offset) \ | 
 | 36 | 		find_next_one_bit(addr, size, offset) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
 | 38 | #define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x) | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 39 | #define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y) | 
 | 40 | #define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define uintBPL_t uint(BITS_PER_LONG) | 
 | 42 | #define uint(x) xuint(x) | 
 | 43 | #define xuint(x) __le ## x | 
 | 44 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 45 | static inline int find_next_one_bit(void *addr, int size, int offset) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 47 | 	uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG); | 
 | 48 | 	int result = offset & ~(BITS_PER_LONG - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | 	unsigned long tmp; | 
 | 50 |  | 
 | 51 | 	if (offset >= size) | 
 | 52 | 		return size; | 
 | 53 | 	size -= result; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 54 | 	offset &= (BITS_PER_LONG - 1); | 
 | 55 | 	if (offset) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 		tmp = leBPL_to_cpup(p++); | 
 | 57 | 		tmp &= ~0UL << offset; | 
 | 58 | 		if (size < BITS_PER_LONG) | 
 | 59 | 			goto found_first; | 
 | 60 | 		if (tmp) | 
 | 61 | 			goto found_middle; | 
 | 62 | 		size -= BITS_PER_LONG; | 
 | 63 | 		result += BITS_PER_LONG; | 
 | 64 | 	} | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 65 | 	while (size & ~(BITS_PER_LONG - 1)) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 66 | 		tmp = leBPL_to_cpup(p++); | 
 | 67 | 		if (tmp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | 			goto found_middle; | 
 | 69 | 		result += BITS_PER_LONG; | 
 | 70 | 		size -= BITS_PER_LONG; | 
 | 71 | 	} | 
 | 72 | 	if (!size) | 
 | 73 | 		return result; | 
 | 74 | 	tmp = leBPL_to_cpup(p); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 75 | found_first: | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 76 | 	tmp &= ~0UL >> (BITS_PER_LONG - size); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 77 | found_middle: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 	return result + ffz(~tmp); | 
 | 79 | } | 
 | 80 |  | 
 | 81 | #define find_first_one_bit(addr, size)\ | 
 | 82 | 	find_next_one_bit((addr), (size), 0) | 
 | 83 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 84 | static int read_block_bitmap(struct super_block *sb, | 
 | 85 | 			     struct udf_bitmap *bitmap, unsigned int block, | 
 | 86 | 			     unsigned long bitmap_nr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { | 
 | 88 | 	struct buffer_head *bh = NULL; | 
 | 89 | 	int retval = 0; | 
 | 90 | 	kernel_lb_addr loc; | 
 | 91 |  | 
 | 92 | 	loc.logicalBlockNum = bitmap->s_extPosition; | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 93 | 	loc.partitionReferenceNum = UDF_SB(sb)->s_partition; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 |  | 
 | 95 | 	bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block)); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 96 | 	if (!bh) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 		retval = -EIO; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 98 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | 	bitmap->s_block_bitmap[bitmap_nr] = bh; | 
 | 100 | 	return retval; | 
 | 101 | } | 
 | 102 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 103 | static int __load_block_bitmap(struct super_block *sb, | 
 | 104 | 			       struct udf_bitmap *bitmap, | 
 | 105 | 			       unsigned int block_group) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { | 
 | 107 | 	int retval = 0; | 
 | 108 | 	int nr_groups = bitmap->s_nr_groups; | 
 | 109 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 110 | 	if (block_group >= nr_groups) { | 
 | 111 | 		udf_debug("block_group (%d) > nr_groups (%d)\n", block_group, | 
 | 112 | 			  nr_groups); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | 	} | 
 | 114 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 115 | 	if (bitmap->s_block_bitmap[block_group]) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 		return block_group; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 117 | 	} else { | 
 | 118 | 		retval = read_block_bitmap(sb, bitmap, block_group, | 
 | 119 | 					   block_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | 		if (retval < 0) | 
 | 121 | 			return retval; | 
 | 122 | 		return block_group; | 
 | 123 | 	} | 
 | 124 | } | 
 | 125 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 126 | static inline int load_block_bitmap(struct super_block *sb, | 
 | 127 | 				    struct udf_bitmap *bitmap, | 
 | 128 | 				    unsigned int block_group) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { | 
 | 130 | 	int slot; | 
 | 131 |  | 
 | 132 | 	slot = __load_block_bitmap(sb, bitmap, block_group); | 
 | 133 |  | 
 | 134 | 	if (slot < 0) | 
 | 135 | 		return slot; | 
 | 136 |  | 
 | 137 | 	if (!bitmap->s_block_bitmap[slot]) | 
 | 138 | 		return -EIO; | 
 | 139 |  | 
 | 140 | 	return slot; | 
 | 141 | } | 
 | 142 |  | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 143 | static bool udf_add_free_space(struct udf_sb_info *sbi, | 
 | 144 | 				u16 partition, u32 cnt) | 
 | 145 | { | 
 | 146 | 	struct logicalVolIntegrityDesc *lvid; | 
 | 147 |  | 
| Marcin Slusarz | cba4435 | 2008-02-13 15:03:33 -0800 | [diff] [blame] | 148 | 	if (sbi->s_lvid_bh == NULL) | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 149 | 		return false; | 
 | 150 |  | 
 | 151 | 	lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data; | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 152 | 	le32_add_cpu(&lvid->freeSpaceTable[partition], cnt); | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 153 | 	return true; | 
 | 154 | } | 
 | 155 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 156 | static void udf_bitmap_free_blocks(struct super_block *sb, | 
 | 157 | 				   struct inode *inode, | 
 | 158 | 				   struct udf_bitmap *bitmap, | 
 | 159 | 				   kernel_lb_addr bloc, uint32_t offset, | 
 | 160 | 				   uint32_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { | 
 | 162 | 	struct udf_sb_info *sbi = UDF_SB(sb); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 163 | 	struct buffer_head *bh = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | 	unsigned long block; | 
 | 165 | 	unsigned long block_group; | 
 | 166 | 	unsigned long bit; | 
 | 167 | 	unsigned long i; | 
 | 168 | 	int bitmap_nr; | 
 | 169 | 	unsigned long overflow; | 
 | 170 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 171 | 	mutex_lock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | 	if (bloc.logicalBlockNum < 0 || | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 173 | 	    (bloc.logicalBlockNum + count) > | 
 | 174 | 		sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 175 | 		udf_debug("%d < %d || %d + %d > %d\n", | 
 | 176 | 			  bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count, | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 177 | 			  sbi->s_partmaps[bloc.partitionReferenceNum]. | 
 | 178 | 							s_partition_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | 		goto error_return; | 
 | 180 | 	} | 
 | 181 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 182 | 	block = bloc.logicalBlockNum + offset + | 
 | 183 | 		(sizeof(struct spaceBitmapDesc) << 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 |  | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 185 | 	do { | 
 | 186 | 		overflow = 0; | 
 | 187 | 		block_group = block >> (sb->s_blocksize_bits + 3); | 
 | 188 | 		bit = block % (sb->s_blocksize << 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 |  | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 190 | 		/* | 
 | 191 | 		* Check to see if we are freeing blocks across a group boundary. | 
 | 192 | 		*/ | 
 | 193 | 		if (bit + count > (sb->s_blocksize << 3)) { | 
 | 194 | 			overflow = bit + count - (sb->s_blocksize << 3); | 
 | 195 | 			count -= overflow; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 		} | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 197 | 		bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | 
 | 198 | 		if (bitmap_nr < 0) | 
 | 199 | 			goto error_return; | 
 | 200 |  | 
 | 201 | 		bh = bitmap->s_block_bitmap[bitmap_nr]; | 
 | 202 | 		for (i = 0; i < count; i++) { | 
 | 203 | 			if (udf_set_bit(bit + i, bh->b_data)) { | 
 | 204 | 				udf_debug("bit %ld already set\n", bit + i); | 
 | 205 | 				udf_debug("byte=%2x\n", | 
 | 206 | 					((char *)bh->b_data)[(bit + i) >> 3]); | 
 | 207 | 			} else { | 
 | 208 | 				if (inode) | 
 | 209 | 					DQUOT_FREE_BLOCK(inode, 1); | 
 | 210 | 				udf_add_free_space(sbi, sbi->s_partition, 1); | 
 | 211 | 			} | 
 | 212 | 		} | 
 | 213 | 		mark_buffer_dirty(bh); | 
 | 214 | 		if (overflow) { | 
 | 215 | 			block += count; | 
 | 216 | 			count = overflow; | 
 | 217 | 		} | 
 | 218 | 	} while (overflow); | 
 | 219 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 220 | error_return: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 	sb->s_dirt = 1; | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 222 | 	if (sbi->s_lvid_bh) | 
 | 223 | 		mark_buffer_dirty(sbi->s_lvid_bh); | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 224 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } | 
 | 226 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 227 | static int udf_bitmap_prealloc_blocks(struct super_block *sb, | 
 | 228 | 				      struct inode *inode, | 
 | 229 | 				      struct udf_bitmap *bitmap, | 
 | 230 | 				      uint16_t partition, uint32_t first_block, | 
 | 231 | 				      uint32_t block_count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { | 
 | 233 | 	struct udf_sb_info *sbi = UDF_SB(sb); | 
 | 234 | 	int alloc_count = 0; | 
 | 235 | 	int bit, block, block_group, group_start; | 
 | 236 | 	int nr_groups, bitmap_nr; | 
 | 237 | 	struct buffer_head *bh; | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 238 | 	__u32 part_len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 240 | 	mutex_lock(&sbi->s_alloc_mutex); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 241 | 	part_len = sbi->s_partmaps[partition].s_partition_len; | 
 | 242 | 	if (first_block < 0 || first_block >= part_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | 		goto out; | 
 | 244 |  | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 245 | 	if (first_block + block_count > part_len) | 
 | 246 | 		block_count = part_len - first_block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 248 | 	do { | 
 | 249 | 		nr_groups = udf_compute_nr_groups(sb, partition); | 
 | 250 | 		block = first_block + (sizeof(struct spaceBitmapDesc) << 3); | 
 | 251 | 		block_group = block >> (sb->s_blocksize_bits + 3); | 
 | 252 | 		group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 |  | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 254 | 		bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | 
 | 255 | 		if (bitmap_nr < 0) | 
 | 256 | 			goto out; | 
 | 257 | 		bh = bitmap->s_block_bitmap[bitmap_nr]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 |  | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 259 | 		bit = block % (sb->s_blocksize << 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 |  | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 261 | 		while (bit < (sb->s_blocksize << 3) && block_count > 0) { | 
 | 262 | 			if (!udf_test_bit(bit, bh->b_data)) | 
 | 263 | 				goto out; | 
 | 264 | 			else if (DQUOT_PREALLOC_BLOCK(inode, 1)) | 
 | 265 | 				goto out; | 
 | 266 | 			else if (!udf_clear_bit(bit, bh->b_data)) { | 
 | 267 | 				udf_debug("bit already cleared for block %d\n", bit); | 
 | 268 | 				DQUOT_FREE_BLOCK(inode, 1); | 
 | 269 | 				goto out; | 
 | 270 | 			} | 
 | 271 | 			block_count--; | 
 | 272 | 			alloc_count++; | 
 | 273 | 			bit++; | 
 | 274 | 			block++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | 		} | 
| Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 276 | 		mark_buffer_dirty(bh); | 
 | 277 | 	} while (block_count > 0); | 
 | 278 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 279 | out: | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 280 | 	if (udf_add_free_space(sbi, partition, -alloc_count)) | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 281 | 		mark_buffer_dirty(sbi->s_lvid_bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 	sb->s_dirt = 1; | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 283 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | 	return alloc_count; | 
 | 285 | } | 
 | 286 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 287 | static int udf_bitmap_new_block(struct super_block *sb, | 
 | 288 | 				struct inode *inode, | 
 | 289 | 				struct udf_bitmap *bitmap, uint16_t partition, | 
 | 290 | 				uint32_t goal, int *err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { | 
 | 292 | 	struct udf_sb_info *sbi = UDF_SB(sb); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 293 | 	int newbit, bit = 0, block, block_group, group_start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | 	int end_goal, nr_groups, bitmap_nr, i; | 
 | 295 | 	struct buffer_head *bh = NULL; | 
 | 296 | 	char *ptr; | 
 | 297 | 	int newblock = 0; | 
 | 298 |  | 
 | 299 | 	*err = -ENOSPC; | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 300 | 	mutex_lock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 302 | repeat: | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 303 | 	if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | 		goal = 0; | 
 | 305 |  | 
 | 306 | 	nr_groups = bitmap->s_nr_groups; | 
 | 307 | 	block = goal + (sizeof(struct spaceBitmapDesc) << 3); | 
 | 308 | 	block_group = block >> (sb->s_blocksize_bits + 3); | 
 | 309 | 	group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | 
 | 310 |  | 
 | 311 | 	bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | 
 | 312 | 	if (bitmap_nr < 0) | 
 | 313 | 		goto error_return; | 
 | 314 | 	bh = bitmap->s_block_bitmap[bitmap_nr]; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 315 | 	ptr = memscan((char *)bh->b_data + group_start, 0xFF, | 
 | 316 | 		      sb->s_blocksize - group_start); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 318 | 	if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | 		bit = block % (sb->s_blocksize << 3); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 320 | 		if (udf_test_bit(bit, bh->b_data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | 			goto got_block; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 322 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | 		end_goal = (bit + 63) & ~63; | 
 | 324 | 		bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); | 
 | 325 | 		if (bit < end_goal) | 
 | 326 | 			goto got_block; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 327 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 328 | 		ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, | 
 | 329 | 			      sb->s_blocksize - ((bit + 7) >> 3)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 		newbit = (ptr - ((char *)bh->b_data)) << 3; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 331 | 		if (newbit < sb->s_blocksize << 3) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | 			bit = newbit; | 
 | 333 | 			goto search_back; | 
 | 334 | 		} | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 335 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 336 | 		newbit = udf_find_next_one_bit(bh->b_data, | 
 | 337 | 					       sb->s_blocksize << 3, bit); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 338 | 		if (newbit < sb->s_blocksize << 3) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | 			bit = newbit; | 
 | 340 | 			goto got_block; | 
 | 341 | 		} | 
 | 342 | 	} | 
 | 343 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 344 | 	for (i = 0; i < (nr_groups * 2); i++) { | 
 | 345 | 		block_group++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | 		if (block_group >= nr_groups) | 
 | 347 | 			block_group = 0; | 
 | 348 | 		group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | 
 | 349 |  | 
 | 350 | 		bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | 
 | 351 | 		if (bitmap_nr < 0) | 
 | 352 | 			goto error_return; | 
 | 353 | 		bh = bitmap->s_block_bitmap[bitmap_nr]; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 354 | 		if (i < nr_groups) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 355 | 			ptr = memscan((char *)bh->b_data + group_start, 0xFF, | 
 | 356 | 				      sb->s_blocksize - group_start); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 357 | 			if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | 				bit = (ptr - ((char *)bh->b_data)) << 3; | 
 | 359 | 				break; | 
 | 360 | 			} | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 361 | 		} else { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 362 | 			bit = udf_find_next_one_bit((char *)bh->b_data, | 
 | 363 | 						    sb->s_blocksize << 3, | 
 | 364 | 						    group_start << 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 			if (bit < sb->s_blocksize << 3) | 
 | 366 | 				break; | 
 | 367 | 		} | 
 | 368 | 	} | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 369 | 	if (i >= (nr_groups * 2)) { | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 370 | 		mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | 		return newblock; | 
 | 372 | 	} | 
 | 373 | 	if (bit < sb->s_blocksize << 3) | 
 | 374 | 		goto search_back; | 
 | 375 | 	else | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 376 | 		bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, | 
 | 377 | 					    group_start << 3); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 378 | 	if (bit >= sb->s_blocksize << 3) { | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 379 | 		mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | 		return 0; | 
 | 381 | 	} | 
 | 382 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 383 | search_back: | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 384 | 	i = 0; | 
 | 385 | 	while (i < 7 && bit > (group_start << 3) && | 
 | 386 | 	       udf_test_bit(bit - 1, bh->b_data)) { | 
 | 387 | 		++i; | 
 | 388 | 		--bit; | 
 | 389 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 391 | got_block: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 |  | 
 | 393 | 	/* | 
 | 394 | 	 * Check quota for allocation of this block. | 
 | 395 | 	 */ | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 396 | 	if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) { | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 397 | 		mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | 		*err = -EDQUOT; | 
 | 399 | 		return 0; | 
 | 400 | 	} | 
 | 401 |  | 
 | 402 | 	newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 403 | 		(sizeof(struct spaceBitmapDesc) << 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 405 | 	if (!udf_clear_bit(bit, bh->b_data)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | 		udf_debug("bit already cleared for block %d\n", bit); | 
 | 407 | 		goto repeat; | 
 | 408 | 	} | 
 | 409 |  | 
 | 410 | 	mark_buffer_dirty(bh); | 
 | 411 |  | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 412 | 	if (udf_add_free_space(sbi, partition, -1)) | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 413 | 		mark_buffer_dirty(sbi->s_lvid_bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | 	sb->s_dirt = 1; | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 415 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | 	*err = 0; | 
 | 417 | 	return newblock; | 
 | 418 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 419 | error_return: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | 	*err = -EIO; | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 421 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | 	return 0; | 
 | 423 | } | 
 | 424 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 425 | static void udf_table_free_blocks(struct super_block *sb, | 
 | 426 | 				  struct inode *inode, | 
 | 427 | 				  struct inode *table, | 
 | 428 | 				  kernel_lb_addr bloc, uint32_t offset, | 
 | 429 | 				  uint32_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { | 
 | 431 | 	struct udf_sb_info *sbi = UDF_SB(sb); | 
 | 432 | 	uint32_t start, end; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 433 | 	uint32_t elen; | 
 | 434 | 	kernel_lb_addr eloc; | 
 | 435 | 	struct extent_position oepos, epos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | 	int8_t etype; | 
 | 437 | 	int i; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 438 | 	struct udf_inode_info *iinfo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 440 | 	mutex_lock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | 	if (bloc.logicalBlockNum < 0 || | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 442 | 	    (bloc.logicalBlockNum + count) > | 
 | 443 | 		sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 444 | 		udf_debug("%d < %d || %d + %d > %d\n", | 
 | 445 | 			  bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count, | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 446 | 			  sbi->s_partmaps[bloc.partitionReferenceNum]. | 
 | 447 | 							s_partition_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | 		goto error_return; | 
 | 449 | 	} | 
 | 450 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 451 | 	iinfo = UDF_I(table); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 452 | 	/* We do this up front - There are some error conditions that | 
 | 453 | 	   could occure, but.. oh well */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | 	if (inode) | 
 | 455 | 		DQUOT_FREE_BLOCK(inode, count); | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 456 | 	if (udf_add_free_space(sbi, sbi->s_partition, count)) | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 457 | 		mark_buffer_dirty(sbi->s_lvid_bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 |  | 
 | 459 | 	start = bloc.logicalBlockNum + offset; | 
 | 460 | 	end = bloc.logicalBlockNum + offset + count - 1; | 
 | 461 |  | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 462 | 	epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | 	elen = 0; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 464 | 	epos.block = oepos.block = iinfo->i_location; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 465 | 	epos.bh = oepos.bh = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 467 | 	while (count && | 
 | 468 | 	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 469 | 		if (((eloc.logicalBlockNum + | 
 | 470 | 			(elen >> sb->s_blocksize_bits)) == start)) { | 
 | 471 | 			if ((0x3FFFFFFF - elen) < | 
 | 472 | 					(count << sb->s_blocksize_bits)) { | 
 | 473 | 				uint32_t tmp = ((0x3FFFFFFF - elen) >> | 
 | 474 | 							sb->s_blocksize_bits); | 
 | 475 | 				count -= tmp; | 
 | 476 | 				start += tmp; | 
 | 477 | 				elen = (etype << 30) | | 
 | 478 | 					(0x40000000 - sb->s_blocksize); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 479 | 			} else { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 480 | 				elen = (etype << 30) | | 
 | 481 | 					(elen + | 
 | 482 | 					(count << sb->s_blocksize_bits)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | 				start += count; | 
 | 484 | 				count = 0; | 
 | 485 | 			} | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 486 | 			udf_write_aext(table, &oepos, eloc, elen, 1); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 487 | 		} else if (eloc.logicalBlockNum == (end + 1)) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 488 | 			if ((0x3FFFFFFF - elen) < | 
 | 489 | 					(count << sb->s_blocksize_bits)) { | 
 | 490 | 				uint32_t tmp = ((0x3FFFFFFF - elen) >> | 
 | 491 | 						sb->s_blocksize_bits); | 
 | 492 | 				count -= tmp; | 
 | 493 | 				end -= tmp; | 
 | 494 | 				eloc.logicalBlockNum -= tmp; | 
 | 495 | 				elen = (etype << 30) | | 
 | 496 | 					(0x40000000 - sb->s_blocksize); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 497 | 			} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | 				eloc.logicalBlockNum = start; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 499 | 				elen = (etype << 30) | | 
 | 500 | 					(elen + | 
 | 501 | 					(count << sb->s_blocksize_bits)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | 				end -= count; | 
 | 503 | 				count = 0; | 
 | 504 | 			} | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 505 | 			udf_write_aext(table, &oepos, eloc, elen, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | 		} | 
 | 507 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 508 | 		if (epos.bh != oepos.bh) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | 			i = -1; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 510 | 			oepos.block = epos.block; | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 511 | 			brelse(oepos.bh); | 
 | 512 | 			get_bh(epos.bh); | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 513 | 			oepos.bh = epos.bh; | 
 | 514 | 			oepos.offset = 0; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 515 | 		} else { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 516 | 			oepos.offset = epos.offset; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 517 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | 	} | 
 | 519 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 520 | 	if (count) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 521 | 		/* | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 522 | 		 * NOTE: we CANNOT use udf_add_aext here, as it can try to | 
 | 523 | 		 * allocate a new block, and since we hold the super block | 
 | 524 | 		 * lock already very bad things would happen :) | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 525 | 		 * | 
 | 526 | 		 * We copy the behavior of udf_add_aext, but instead of | 
 | 527 | 		 * trying to allocate a new block close to the existing one, | 
 | 528 | 		 * we just steal a block from the extent we are trying to add. | 
 | 529 | 		 * | 
 | 530 | 		 * It would be nice if the blocks were close together, but it | 
 | 531 | 		 * isn't required. | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 532 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 |  | 
 | 534 | 		int adsize; | 
 | 535 | 		short_ad *sad = NULL; | 
 | 536 | 		long_ad *lad = NULL; | 
 | 537 | 		struct allocExtDesc *aed; | 
 | 538 |  | 
 | 539 | 		eloc.logicalBlockNum = start; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 540 | 		elen = EXT_RECORDED_ALLOCATED | | 
 | 541 | 			(count << sb->s_blocksize_bits); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 543 | 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | 			adsize = sizeof(short_ad); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 545 | 		else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | 			adsize = sizeof(long_ad); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 547 | 		else { | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 548 | 			brelse(oepos.bh); | 
 | 549 | 			brelse(epos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | 			goto error_return; | 
 | 551 | 		} | 
 | 552 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 553 | 		if (epos.offset + (2 * adsize) > sb->s_blocksize) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | 			char *sptr, *dptr; | 
 | 555 | 			int loffset; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 556 |  | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 557 | 			brelse(oepos.bh); | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 558 | 			oepos = epos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 |  | 
 | 560 | 			/* Steal a block from the extent being free'd */ | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 561 | 			epos.block.logicalBlockNum = eloc.logicalBlockNum; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 562 | 			eloc.logicalBlockNum++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | 			elen -= sb->s_blocksize; | 
 | 564 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 565 | 			epos.bh = udf_tread(sb, | 
 | 566 | 					udf_get_lb_pblock(sb, epos.block, 0)); | 
 | 567 | 			if (!epos.bh) { | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 568 | 				brelse(oepos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | 				goto error_return; | 
 | 570 | 			} | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 571 | 			aed = (struct allocExtDesc *)(epos.bh->b_data); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 572 | 			aed->previousAllocExtLocation = | 
 | 573 | 				cpu_to_le32(oepos.block.logicalBlockNum); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 574 | 			if (epos.offset + adsize > sb->s_blocksize) { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 575 | 				loffset = epos.offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | 				aed->lengthAllocDescs = cpu_to_le32(adsize); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 577 | 				sptr = iinfo->i_ext.i_data + epos.offset | 
| Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 578 | 								- adsize; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 579 | 				dptr = epos.bh->b_data + | 
 | 580 | 					sizeof(struct allocExtDesc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | 				memcpy(dptr, sptr, adsize); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 582 | 				epos.offset = sizeof(struct allocExtDesc) + | 
 | 583 | 						adsize; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 584 | 			} else { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 585 | 				loffset = epos.offset + adsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | 				aed->lengthAllocDescs = cpu_to_le32(0); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 587 | 				if (oepos.bh) { | 
| Jan Kara | f5cc15d | 2007-08-30 23:56:22 -0700 | [diff] [blame] | 588 | 					sptr = oepos.bh->b_data + epos.offset; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 589 | 					aed = (struct allocExtDesc *) | 
 | 590 | 						oepos.bh->b_data; | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 591 | 					le32_add_cpu(&aed->lengthAllocDescs, | 
 | 592 | 							adsize); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 593 | 				} else { | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 594 | 					sptr = iinfo->i_ext.i_data + | 
| Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 595 | 								epos.offset; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 596 | 					iinfo->i_lenAlloc += adsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | 					mark_inode_dirty(table); | 
 | 598 | 				} | 
| Jan Kara | f5cc15d | 2007-08-30 23:56:22 -0700 | [diff] [blame] | 599 | 				epos.offset = sizeof(struct allocExtDesc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | 			} | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 601 | 			if (sbi->s_udfrev >= 0x0200) | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 602 | 				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, | 
 | 603 | 					    3, 1, epos.block.logicalBlockNum, | 
 | 604 | 					    sizeof(tag)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | 			else | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 606 | 				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, | 
 | 607 | 					    2, 1, epos.block.logicalBlockNum, | 
 | 608 | 					    sizeof(tag)); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 609 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 610 | 			switch (iinfo->i_alloc_type) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 611 | 			case ICBTAG_FLAG_AD_SHORT: | 
 | 612 | 				sad = (short_ad *)sptr; | 
 | 613 | 				sad->extLength = cpu_to_le32( | 
 | 614 | 					EXT_NEXT_EXTENT_ALLOCDECS | | 
 | 615 | 					sb->s_blocksize); | 
 | 616 | 				sad->extPosition = | 
 | 617 | 					cpu_to_le32(epos.block.logicalBlockNum); | 
 | 618 | 				break; | 
 | 619 | 			case ICBTAG_FLAG_AD_LONG: | 
 | 620 | 				lad = (long_ad *)sptr; | 
 | 621 | 				lad->extLength = cpu_to_le32( | 
 | 622 | 					EXT_NEXT_EXTENT_ALLOCDECS | | 
 | 623 | 					sb->s_blocksize); | 
 | 624 | 				lad->extLocation = | 
 | 625 | 					cpu_to_lelb(epos.block); | 
 | 626 | 				break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | 			} | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 628 | 			if (oepos.bh) { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 629 | 				udf_update_tag(oepos.bh->b_data, loffset); | 
 | 630 | 				mark_buffer_dirty(oepos.bh); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 631 | 			} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | 				mark_inode_dirty(table); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 633 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | 		} | 
 | 635 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 636 | 		/* It's possible that stealing the block emptied the extent */ | 
 | 637 | 		if (elen) { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 638 | 			udf_write_aext(table, &epos, eloc, elen, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 640 | 			if (!epos.bh) { | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 641 | 				iinfo->i_lenAlloc += adsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | 				mark_inode_dirty(table); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 643 | 			} else { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 644 | 				aed = (struct allocExtDesc *)epos.bh->b_data; | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 645 | 				le32_add_cpu(&aed->lengthAllocDescs, adsize); | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 646 | 				udf_update_tag(epos.bh->b_data, epos.offset); | 
 | 647 | 				mark_buffer_dirty(epos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | 			} | 
 | 649 | 		} | 
 | 650 | 	} | 
 | 651 |  | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 652 | 	brelse(epos.bh); | 
 | 653 | 	brelse(oepos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 655 | error_return: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | 	sb->s_dirt = 1; | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 657 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | 	return; | 
 | 659 | } | 
 | 660 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 661 | static int udf_table_prealloc_blocks(struct super_block *sb, | 
 | 662 | 				     struct inode *inode, | 
 | 663 | 				     struct inode *table, uint16_t partition, | 
 | 664 | 				     uint32_t first_block, uint32_t block_count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { | 
 | 666 | 	struct udf_sb_info *sbi = UDF_SB(sb); | 
 | 667 | 	int alloc_count = 0; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 668 | 	uint32_t elen, adsize; | 
 | 669 | 	kernel_lb_addr eloc; | 
 | 670 | 	struct extent_position epos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | 	int8_t etype = -1; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 672 | 	struct udf_inode_info *iinfo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 674 | 	if (first_block < 0 || | 
 | 675 | 		first_block >= sbi->s_partmaps[partition].s_partition_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | 		return 0; | 
 | 677 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 678 | 	iinfo = UDF_I(table); | 
 | 679 | 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | 		adsize = sizeof(short_ad); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 681 | 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | 		adsize = sizeof(long_ad); | 
 | 683 | 	else | 
 | 684 | 		return 0; | 
 | 685 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 686 | 	mutex_lock(&sbi->s_alloc_mutex); | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 687 | 	epos.offset = sizeof(struct unallocSpaceEntry); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 688 | 	epos.block = iinfo->i_location; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 689 | 	epos.bh = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | 	eloc.logicalBlockNum = 0xFFFFFFFF; | 
 | 691 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 692 | 	while (first_block != eloc.logicalBlockNum && | 
 | 693 | 	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | 		udf_debug("eloc=%d, elen=%d, first_block=%d\n", | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 695 | 			  eloc.logicalBlockNum, elen, first_block); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 696 | 		; /* empty loop body */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | 	} | 
 | 698 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 699 | 	if (first_block == eloc.logicalBlockNum) { | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 700 | 		epos.offset -= adsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 |  | 
 | 702 | 		alloc_count = (elen >> sb->s_blocksize_bits); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 703 | 		if (inode && DQUOT_PREALLOC_BLOCK(inode, | 
 | 704 | 			alloc_count > block_count ? block_count : alloc_count)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | 			alloc_count = 0; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 706 | 		else if (alloc_count > block_count) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | 			alloc_count = block_count; | 
 | 708 | 			eloc.logicalBlockNum += alloc_count; | 
 | 709 | 			elen -= (alloc_count << sb->s_blocksize_bits); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 710 | 			udf_write_aext(table, &epos, eloc, | 
 | 711 | 					(etype << 30) | elen, 1); | 
 | 712 | 		} else | 
 | 713 | 			udf_delete_aext(table, epos, eloc, | 
 | 714 | 					(etype << 30) | elen); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 715 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | 		alloc_count = 0; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 717 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 |  | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 719 | 	brelse(epos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 |  | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 721 | 	if (alloc_count && udf_add_free_space(sbi, partition, -alloc_count)) { | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 722 | 		mark_buffer_dirty(sbi->s_lvid_bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | 		sb->s_dirt = 1; | 
 | 724 | 	} | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 725 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | 	return alloc_count; | 
 | 727 | } | 
 | 728 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 729 | static int udf_table_new_block(struct super_block *sb, | 
 | 730 | 			       struct inode *inode, | 
 | 731 | 			       struct inode *table, uint16_t partition, | 
 | 732 | 			       uint32_t goal, int *err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | { | 
 | 734 | 	struct udf_sb_info *sbi = UDF_SB(sb); | 
 | 735 | 	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; | 
 | 736 | 	uint32_t newblock = 0, adsize; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 737 | 	uint32_t elen, goal_elen = 0; | 
| WANG Cong | 3ad90ec | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 738 | 	kernel_lb_addr eloc, uninitialized_var(goal_eloc); | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 739 | 	struct extent_position epos, goal_epos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | 	int8_t etype; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 741 | 	struct udf_inode_info *iinfo = UDF_I(table); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 |  | 
 | 743 | 	*err = -ENOSPC; | 
 | 744 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 745 | 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | 		adsize = sizeof(short_ad); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 747 | 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | 		adsize = sizeof(long_ad); | 
 | 749 | 	else | 
 | 750 | 		return newblock; | 
 | 751 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 752 | 	mutex_lock(&sbi->s_alloc_mutex); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 753 | 	if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | 		goal = 0; | 
 | 755 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 756 | 	/* We search for the closest matching block to goal. If we find | 
 | 757 | 	   a exact hit, we stop. Otherwise we keep going till we run out | 
 | 758 | 	   of extents. We store the buffer_head, bloc, and extoffset | 
 | 759 | 	   of the current closest match and use that when we are done. | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 760 | 	 */ | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 761 | 	epos.offset = sizeof(struct unallocSpaceEntry); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 762 | 	epos.block = iinfo->i_location; | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 763 | 	epos.bh = goal_epos.bh = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 765 | 	while (spread && | 
 | 766 | 	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 767 | 		if (goal >= eloc.logicalBlockNum) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 768 | 			if (goal < eloc.logicalBlockNum + | 
 | 769 | 					(elen >> sb->s_blocksize_bits)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | 				nspread = 0; | 
 | 771 | 			else | 
 | 772 | 				nspread = goal - eloc.logicalBlockNum - | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 773 | 					(elen >> sb->s_blocksize_bits); | 
 | 774 | 		} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | 			nspread = eloc.logicalBlockNum - goal; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 776 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 778 | 		if (nspread < spread) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | 			spread = nspread; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 780 | 			if (goal_epos.bh != epos.bh) { | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 781 | 				brelse(goal_epos.bh); | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 782 | 				goal_epos.bh = epos.bh; | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 783 | 				get_bh(goal_epos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | 			} | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 785 | 			goal_epos.block = epos.block; | 
 | 786 | 			goal_epos.offset = epos.offset - adsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | 			goal_eloc = eloc; | 
 | 788 | 			goal_elen = (etype << 30) | elen; | 
 | 789 | 		} | 
 | 790 | 	} | 
 | 791 |  | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 792 | 	brelse(epos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 794 | 	if (spread == 0xFFFFFFFF) { | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 795 | 		brelse(goal_epos.bh); | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 796 | 		mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | 		return 0; | 
 | 798 | 	} | 
 | 799 |  | 
 | 800 | 	/* Only allocate blocks from the beginning of the extent. | 
 | 801 | 	   That way, we only delete (empty) extents, never have to insert an | 
 | 802 | 	   extent because of splitting */ | 
 | 803 | 	/* This works, but very poorly.... */ | 
 | 804 |  | 
 | 805 | 	newblock = goal_eloc.logicalBlockNum; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 806 | 	goal_eloc.logicalBlockNum++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | 	goal_elen -= sb->s_blocksize; | 
 | 808 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 809 | 	if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) { | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 810 | 		brelse(goal_epos.bh); | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 811 | 		mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | 		*err = -EDQUOT; | 
 | 813 | 		return 0; | 
 | 814 | 	} | 
 | 815 |  | 
 | 816 | 	if (goal_elen) | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 817 | 		udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | 	else | 
| Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 819 | 		udf_delete_aext(table, goal_epos, goal_eloc, goal_elen); | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 820 | 	brelse(goal_epos.bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 |  | 
| Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 822 | 	if (udf_add_free_space(sbi, partition, -1)) | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 823 | 		mark_buffer_dirty(sbi->s_lvid_bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 |  | 
 | 825 | 	sb->s_dirt = 1; | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 826 | 	mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | 	*err = 0; | 
 | 828 | 	return newblock; | 
 | 829 | } | 
 | 830 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 831 | inline void udf_free_blocks(struct super_block *sb, | 
 | 832 | 			    struct inode *inode, | 
 | 833 | 			    kernel_lb_addr bloc, uint32_t offset, | 
 | 834 | 			    uint32_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | { | 
 | 836 | 	uint16_t partition = bloc.partitionReferenceNum; | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 837 | 	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 |  | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 839 | 	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | 		return udf_bitmap_free_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 841 | 					      map->s_uspace.s_bitmap, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 842 | 					      bloc, offset, count); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 843 | 	} else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | 		return udf_table_free_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 845 | 					     map->s_uspace.s_table, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 846 | 					     bloc, offset, count); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 847 | 	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | 		return udf_bitmap_free_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 849 | 					      map->s_fspace.s_bitmap, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 850 | 					      bloc, offset, count); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 851 | 	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | 		return udf_table_free_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 853 | 					     map->s_fspace.s_table, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 854 | 					     bloc, offset, count); | 
 | 855 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | 		return; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 857 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } | 
 | 859 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 860 | inline int udf_prealloc_blocks(struct super_block *sb, | 
 | 861 | 			       struct inode *inode, | 
 | 862 | 			       uint16_t partition, uint32_t first_block, | 
 | 863 | 			       uint32_t block_count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 865 | 	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; | 
 | 866 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 867 | 	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | 		return udf_bitmap_prealloc_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 869 | 						  map->s_uspace.s_bitmap, | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 870 | 						  partition, first_block, | 
 | 871 | 						  block_count); | 
 | 872 | 	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | 		return udf_table_prealloc_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 874 | 						 map->s_uspace.s_table, | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 875 | 						 partition, first_block, | 
 | 876 | 						 block_count); | 
 | 877 | 	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | 		return udf_bitmap_prealloc_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 879 | 						  map->s_fspace.s_bitmap, | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 880 | 						  partition, first_block, | 
 | 881 | 						  block_count); | 
 | 882 | 	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | 		return udf_table_prealloc_blocks(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 884 | 						 map->s_fspace.s_table, | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 885 | 						 partition, first_block, | 
 | 886 | 						 block_count); | 
 | 887 | 	else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | 		return 0; | 
 | 889 | } | 
 | 890 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 891 | inline int udf_new_block(struct super_block *sb, | 
 | 892 | 			 struct inode *inode, | 
 | 893 | 			 uint16_t partition, uint32_t goal, int *err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 895 | 	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; | 
| Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 896 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 897 | 	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) | 
 | 898 | 		return udf_bitmap_new_block(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 899 | 					   map->s_uspace.s_bitmap, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 900 | 					   partition, goal, err); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 901 | 	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | 		return udf_table_new_block(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 903 | 					   map->s_uspace.s_table, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 904 | 					   partition, goal, err); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 905 | 	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | 		return udf_bitmap_new_block(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 907 | 					    map->s_fspace.s_bitmap, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 908 | 					    partition, goal, err); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 909 | 	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | 		return udf_table_new_block(sb, inode, | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 911 | 					   map->s_fspace.s_table, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 912 | 					   partition, goal, err); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 913 | 	else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | 		*err = -EIO; | 
 | 915 | 		return 0; | 
 | 916 | 	} | 
 | 917 | } |