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