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