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