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