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 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 143 | static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt) |
Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 144 | { |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 145 | struct udf_sb_info *sbi = UDF_SB(sb); |
Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 146 | struct logicalVolIntegrityDesc *lvid; |
| 147 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 148 | if (!sbi->s_lvid_bh) |
| 149 | return; |
Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 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); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 153 | udf_updated_lvid(sb); |
Marcin Slusarz | 742ba02 | 2008-02-08 04:20:40 -0800 | [diff] [blame] | 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); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 212 | udf_add_free_space(sb, sbi->s_partition, 1); |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 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: |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 223 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 226 | static int udf_bitmap_prealloc_blocks(struct super_block *sb, |
| 227 | struct inode *inode, |
| 228 | struct udf_bitmap *bitmap, |
| 229 | uint16_t partition, uint32_t first_block, |
| 230 | uint32_t block_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 233 | int alloc_count = 0; |
| 234 | int bit, block, block_group, group_start; |
| 235 | int nr_groups, bitmap_nr; |
| 236 | struct buffer_head *bh; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 237 | __u32 part_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 239 | mutex_lock(&sbi->s_alloc_mutex); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 240 | part_len = sbi->s_partmaps[partition].s_partition_len; |
Roel Kluin | 3391faa | 2009-06-22 23:12:29 +0200 | [diff] [blame] | 241 | if (first_block >= part_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | goto out; |
| 243 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 244 | if (first_block + block_count > part_len) |
| 245 | block_count = part_len - first_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 247 | do { |
| 248 | nr_groups = udf_compute_nr_groups(sb, partition); |
| 249 | block = first_block + (sizeof(struct spaceBitmapDesc) << 3); |
| 250 | block_group = block >> (sb->s_blocksize_bits + 3); |
| 251 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 253 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 254 | if (bitmap_nr < 0) |
| 255 | goto out; |
| 256 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 258 | bit = block % (sb->s_blocksize << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 260 | while (bit < (sb->s_blocksize << 3) && block_count > 0) { |
| 261 | if (!udf_test_bit(bit, bh->b_data)) |
| 262 | goto out; |
Jan Kara | bacfb7c | 2009-01-26 17:20:46 +0100 | [diff] [blame] | 263 | else if (vfs_dq_prealloc_block(inode, 1)) |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 264 | goto out; |
| 265 | else if (!udf_clear_bit(bit, bh->b_data)) { |
| 266 | udf_debug("bit already cleared for block %d\n", bit); |
Jan Kara | bacfb7c | 2009-01-26 17:20:46 +0100 | [diff] [blame] | 267 | vfs_dq_free_block(inode, 1); |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 268 | goto out; |
| 269 | } |
| 270 | block_count--; |
| 271 | alloc_count++; |
| 272 | bit++; |
| 273 | block++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
Marcin Slusarz | 4daa1b8 | 2008-02-08 04:20:41 -0800 | [diff] [blame] | 275 | mark_buffer_dirty(bh); |
| 276 | } while (block_count > 0); |
| 277 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 278 | out: |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 279 | udf_add_free_space(sb, partition, -alloc_count); |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 280 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return alloc_count; |
| 282 | } |
| 283 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 284 | static int udf_bitmap_new_block(struct super_block *sb, |
| 285 | struct inode *inode, |
| 286 | struct udf_bitmap *bitmap, uint16_t partition, |
| 287 | uint32_t goal, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct udf_sb_info *sbi = UDF_SB(sb); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 290 | int newbit, bit = 0, block, block_group, group_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | int end_goal, nr_groups, bitmap_nr, i; |
| 292 | struct buffer_head *bh = NULL; |
| 293 | char *ptr; |
| 294 | int newblock = 0; |
| 295 | |
| 296 | *err = -ENOSPC; |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 297 | mutex_lock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 299 | repeat: |
Roel Kluin | 3391faa | 2009-06-22 23:12:29 +0200 | [diff] [blame] | 300 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | goal = 0; |
| 302 | |
| 303 | nr_groups = bitmap->s_nr_groups; |
| 304 | block = goal + (sizeof(struct spaceBitmapDesc) << 3); |
| 305 | block_group = block >> (sb->s_blocksize_bits + 3); |
| 306 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); |
| 307 | |
| 308 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 309 | if (bitmap_nr < 0) |
| 310 | goto error_return; |
| 311 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 312 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
| 313 | sb->s_blocksize - group_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 315 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | bit = block % (sb->s_blocksize << 3); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 317 | if (udf_test_bit(bit, bh->b_data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | goto got_block; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | end_goal = (bit + 63) & ~63; |
| 321 | bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); |
| 322 | if (bit < end_goal) |
| 323 | goto got_block; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 324 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 325 | ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, |
| 326 | sb->s_blocksize - ((bit + 7) >> 3)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | newbit = (ptr - ((char *)bh->b_data)) << 3; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 328 | if (newbit < sb->s_blocksize << 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | bit = newbit; |
| 330 | goto search_back; |
| 331 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 332 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 333 | newbit = udf_find_next_one_bit(bh->b_data, |
| 334 | sb->s_blocksize << 3, bit); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 335 | if (newbit < sb->s_blocksize << 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | bit = newbit; |
| 337 | goto got_block; |
| 338 | } |
| 339 | } |
| 340 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 341 | for (i = 0; i < (nr_groups * 2); i++) { |
| 342 | block_group++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | if (block_group >= nr_groups) |
| 344 | block_group = 0; |
| 345 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); |
| 346 | |
| 347 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
| 348 | if (bitmap_nr < 0) |
| 349 | goto error_return; |
| 350 | bh = bitmap->s_block_bitmap[bitmap_nr]; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 351 | if (i < nr_groups) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 352 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
| 353 | sb->s_blocksize - group_start); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 354 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | bit = (ptr - ((char *)bh->b_data)) << 3; |
| 356 | break; |
| 357 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 358 | } else { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 359 | bit = udf_find_next_one_bit((char *)bh->b_data, |
| 360 | sb->s_blocksize << 3, |
| 361 | group_start << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (bit < sb->s_blocksize << 3) |
| 363 | break; |
| 364 | } |
| 365 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 366 | if (i >= (nr_groups * 2)) { |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 367 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return newblock; |
| 369 | } |
| 370 | if (bit < sb->s_blocksize << 3) |
| 371 | goto search_back; |
| 372 | else |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 373 | bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, |
| 374 | group_start << 3); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 375 | if (bit >= sb->s_blocksize << 3) { |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 376 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 380 | search_back: |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 381 | i = 0; |
| 382 | while (i < 7 && bit > (group_start << 3) && |
| 383 | udf_test_bit(bit - 1, bh->b_data)) { |
| 384 | ++i; |
| 385 | --bit; |
| 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 388 | got_block: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * Check quota for allocation of this block. |
| 392 | */ |
Jan Kara | bacfb7c | 2009-01-26 17:20:46 +0100 | [diff] [blame] | 393 | if (inode && vfs_dq_alloc_block(inode, 1)) { |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 394 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | *err = -EDQUOT; |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 400 | (sizeof(struct spaceBitmapDesc) << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 402 | if (!udf_clear_bit(bit, bh->b_data)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | udf_debug("bit already cleared for block %d\n", bit); |
| 404 | goto repeat; |
| 405 | } |
| 406 | |
| 407 | mark_buffer_dirty(bh); |
| 408 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 409 | udf_add_free_space(sb, partition, -1); |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 410 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | *err = 0; |
| 412 | return newblock; |
| 413 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 414 | error_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | *err = -EIO; |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 416 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 420 | static void udf_table_free_blocks(struct super_block *sb, |
| 421 | struct inode *inode, |
| 422 | struct inode *table, |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 423 | struct kernel_lb_addr *bloc, |
| 424 | uint32_t offset, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 425 | uint32_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct udf_sb_info *sbi = UDF_SB(sb); |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 428 | struct udf_part_map *partmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | uint32_t start, end; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 430 | uint32_t elen; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 431 | struct kernel_lb_addr eloc; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 432 | struct extent_position oepos, epos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | int8_t etype; |
| 434 | int i; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 435 | struct udf_inode_info *iinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 437 | mutex_lock(&sbi->s_alloc_mutex); |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 438 | partmap = &sbi->s_partmaps[bloc->partitionReferenceNum]; |
| 439 | if (bloc->logicalBlockNum < 0 || |
| 440 | (bloc->logicalBlockNum + count) > |
| 441 | partmap->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, |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 444 | partmap->s_partition_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | goto error_return; |
| 446 | } |
| 447 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 448 | iinfo = UDF_I(table); |
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) |
Jan Kara | bacfb7c | 2009-01-26 17:20:46 +0100 | [diff] [blame] | 452 | vfs_dq_free_block(inode, count); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 453 | udf_add_free_space(sb, sbi->s_partition, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 455 | start = bloc->logicalBlockNum + offset; |
| 456 | end = bloc->logicalBlockNum + offset + count - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 458 | epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | elen = 0; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 460 | epos.block = oepos.block = iinfo->i_location; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 461 | epos.bh = oepos.bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 463 | while (count && |
| 464 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 465 | if (((eloc.logicalBlockNum + |
| 466 | (elen >> sb->s_blocksize_bits)) == start)) { |
| 467 | if ((0x3FFFFFFF - elen) < |
| 468 | (count << sb->s_blocksize_bits)) { |
| 469 | uint32_t tmp = ((0x3FFFFFFF - elen) >> |
| 470 | sb->s_blocksize_bits); |
| 471 | count -= tmp; |
| 472 | start += tmp; |
| 473 | elen = (etype << 30) | |
| 474 | (0x40000000 - sb->s_blocksize); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 475 | } else { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 476 | elen = (etype << 30) | |
| 477 | (elen + |
| 478 | (count << sb->s_blocksize_bits)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | start += count; |
| 480 | count = 0; |
| 481 | } |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 482 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 483 | } else if (eloc.logicalBlockNum == (end + 1)) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 484 | if ((0x3FFFFFFF - elen) < |
| 485 | (count << sb->s_blocksize_bits)) { |
| 486 | uint32_t tmp = ((0x3FFFFFFF - elen) >> |
| 487 | sb->s_blocksize_bits); |
| 488 | count -= tmp; |
| 489 | end -= tmp; |
| 490 | eloc.logicalBlockNum -= tmp; |
| 491 | elen = (etype << 30) | |
| 492 | (0x40000000 - sb->s_blocksize); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 493 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | eloc.logicalBlockNum = start; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 495 | elen = (etype << 30) | |
| 496 | (elen + |
| 497 | (count << sb->s_blocksize_bits)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | end -= count; |
| 499 | count = 0; |
| 500 | } |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 501 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 504 | if (epos.bh != oepos.bh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | i = -1; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 506 | oepos.block = epos.block; |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 507 | brelse(oepos.bh); |
| 508 | get_bh(epos.bh); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 509 | oepos.bh = epos.bh; |
| 510 | oepos.offset = 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 511 | } else { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 512 | oepos.offset = epos.offset; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 516 | if (count) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 517 | /* |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 518 | * NOTE: we CANNOT use udf_add_aext here, as it can try to |
| 519 | * allocate a new block, and since we hold the super block |
| 520 | * lock already very bad things would happen :) |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 521 | * |
| 522 | * We copy the behavior of udf_add_aext, but instead of |
| 523 | * trying to allocate a new block close to the existing one, |
| 524 | * we just steal a block from the extent we are trying to add. |
| 525 | * |
| 526 | * It would be nice if the blocks were close together, but it |
| 527 | * isn't required. |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 528 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | int adsize; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 531 | struct short_ad *sad = NULL; |
| 532 | struct long_ad *lad = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | struct allocExtDesc *aed; |
| 534 | |
| 535 | eloc.logicalBlockNum = start; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 536 | elen = EXT_RECORDED_ALLOCATED | |
| 537 | (count << sb->s_blocksize_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 539 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 540 | adsize = sizeof(struct short_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 541 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 542 | adsize = sizeof(struct long_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 543 | else { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 544 | brelse(oepos.bh); |
| 545 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | goto error_return; |
| 547 | } |
| 548 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 549 | if (epos.offset + (2 * adsize) > sb->s_blocksize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | char *sptr, *dptr; |
| 551 | int loffset; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 552 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 553 | brelse(oepos.bh); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 554 | oepos = epos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* Steal a block from the extent being free'd */ |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 557 | epos.block.logicalBlockNum = eloc.logicalBlockNum; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 558 | eloc.logicalBlockNum++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | elen -= sb->s_blocksize; |
| 560 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 561 | epos.bh = udf_tread(sb, |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 562 | udf_get_lb_pblock(sb, &epos.block, 0)); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 563 | if (!epos.bh) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 564 | brelse(oepos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | goto error_return; |
| 566 | } |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 567 | aed = (struct allocExtDesc *)(epos.bh->b_data); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 568 | aed->previousAllocExtLocation = |
| 569 | cpu_to_le32(oepos.block.logicalBlockNum); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 570 | if (epos.offset + adsize > sb->s_blocksize) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 571 | loffset = epos.offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | aed->lengthAllocDescs = cpu_to_le32(adsize); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 573 | sptr = iinfo->i_ext.i_data + epos.offset |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 574 | - adsize; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 575 | dptr = epos.bh->b_data + |
| 576 | sizeof(struct allocExtDesc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | memcpy(dptr, sptr, adsize); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 578 | epos.offset = sizeof(struct allocExtDesc) + |
| 579 | adsize; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 580 | } else { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 581 | loffset = epos.offset + adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | aed->lengthAllocDescs = cpu_to_le32(0); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 583 | if (oepos.bh) { |
Jan Kara | f5cc15d | 2007-08-30 23:56:22 -0700 | [diff] [blame] | 584 | sptr = oepos.bh->b_data + epos.offset; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 585 | aed = (struct allocExtDesc *) |
| 586 | oepos.bh->b_data; |
marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 587 | le32_add_cpu(&aed->lengthAllocDescs, |
| 588 | adsize); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 589 | } else { |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 590 | sptr = iinfo->i_ext.i_data + |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 591 | epos.offset; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 592 | iinfo->i_lenAlloc += adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | mark_inode_dirty(table); |
| 594 | } |
Jan Kara | f5cc15d | 2007-08-30 23:56:22 -0700 | [diff] [blame] | 595 | epos.offset = sizeof(struct allocExtDesc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 597 | if (sbi->s_udfrev >= 0x0200) |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 598 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
| 599 | 3, 1, epos.block.logicalBlockNum, |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 600 | sizeof(struct tag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | else |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 602 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
| 603 | 2, 1, epos.block.logicalBlockNum, |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 604 | sizeof(struct tag)); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 605 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 606 | switch (iinfo->i_alloc_type) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 607 | case ICBTAG_FLAG_AD_SHORT: |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 608 | sad = (struct short_ad *)sptr; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 609 | sad->extLength = cpu_to_le32( |
| 610 | EXT_NEXT_EXTENT_ALLOCDECS | |
| 611 | sb->s_blocksize); |
| 612 | sad->extPosition = |
| 613 | cpu_to_le32(epos.block.logicalBlockNum); |
| 614 | break; |
| 615 | case ICBTAG_FLAG_AD_LONG: |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 616 | lad = (struct long_ad *)sptr; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 617 | lad->extLength = cpu_to_le32( |
| 618 | EXT_NEXT_EXTENT_ALLOCDECS | |
| 619 | sb->s_blocksize); |
| 620 | lad->extLocation = |
| 621 | cpu_to_lelb(epos.block); |
| 622 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 624 | if (oepos.bh) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 625 | udf_update_tag(oepos.bh->b_data, loffset); |
| 626 | mark_buffer_dirty(oepos.bh); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 627 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | mark_inode_dirty(table); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 632 | /* It's possible that stealing the block emptied the extent */ |
| 633 | if (elen) { |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 634 | udf_write_aext(table, &epos, &eloc, elen, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 636 | if (!epos.bh) { |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 637 | iinfo->i_lenAlloc += adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | mark_inode_dirty(table); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 639 | } else { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 640 | aed = (struct allocExtDesc *)epos.bh->b_data; |
marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 641 | le32_add_cpu(&aed->lengthAllocDescs, adsize); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 642 | udf_update_tag(epos.bh->b_data, epos.offset); |
| 643 | mark_buffer_dirty(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 648 | brelse(epos.bh); |
| 649 | brelse(oepos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 651 | error_return: |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 652 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return; |
| 654 | } |
| 655 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 656 | static int udf_table_prealloc_blocks(struct super_block *sb, |
| 657 | struct inode *inode, |
| 658 | struct inode *table, uint16_t partition, |
| 659 | uint32_t first_block, uint32_t block_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
| 661 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 662 | int alloc_count = 0; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 663 | uint32_t elen, adsize; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 664 | struct kernel_lb_addr eloc; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 665 | struct extent_position epos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | int8_t etype = -1; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 667 | struct udf_inode_info *iinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Roel Kluin | 3391faa | 2009-06-22 23:12:29 +0200 | [diff] [blame] | 669 | if (first_block >= sbi->s_partmaps[partition].s_partition_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 672 | iinfo = UDF_I(table); |
| 673 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 674 | adsize = sizeof(struct short_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 675 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 676 | adsize = sizeof(struct long_ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | else |
| 678 | return 0; |
| 679 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 680 | mutex_lock(&sbi->s_alloc_mutex); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 681 | epos.offset = sizeof(struct unallocSpaceEntry); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 682 | epos.block = iinfo->i_location; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 683 | epos.bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | eloc.logicalBlockNum = 0xFFFFFFFF; |
| 685 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 686 | while (first_block != eloc.logicalBlockNum && |
| 687 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | udf_debug("eloc=%d, elen=%d, first_block=%d\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 689 | eloc.logicalBlockNum, elen, first_block); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 690 | ; /* empty loop body */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 693 | if (first_block == eloc.logicalBlockNum) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 694 | epos.offset -= adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
| 696 | alloc_count = (elen >> sb->s_blocksize_bits); |
Jan Kara | bacfb7c | 2009-01-26 17:20:46 +0100 | [diff] [blame] | 697 | if (inode && vfs_dq_prealloc_block(inode, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 698 | alloc_count > block_count ? block_count : alloc_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | alloc_count = 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 700 | else if (alloc_count > block_count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | alloc_count = block_count; |
| 702 | eloc.logicalBlockNum += alloc_count; |
| 703 | elen -= (alloc_count << sb->s_blocksize_bits); |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 704 | udf_write_aext(table, &epos, &eloc, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 705 | (etype << 30) | elen, 1); |
| 706 | } else |
| 707 | udf_delete_aext(table, epos, eloc, |
| 708 | (etype << 30) | elen); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 709 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | alloc_count = 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 713 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 715 | if (alloc_count) |
| 716 | udf_add_free_space(sb, partition, -alloc_count); |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 717 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return alloc_count; |
| 719 | } |
| 720 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 721 | static int udf_table_new_block(struct super_block *sb, |
| 722 | struct inode *inode, |
| 723 | struct inode *table, uint16_t partition, |
| 724 | uint32_t goal, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
| 726 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 727 | uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; |
| 728 | uint32_t newblock = 0, adsize; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 729 | uint32_t elen, goal_elen = 0; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 730 | struct kernel_lb_addr eloc, uninitialized_var(goal_eloc); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 731 | struct extent_position epos, goal_epos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | int8_t etype; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 733 | struct udf_inode_info *iinfo = UDF_I(table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | *err = -ENOSPC; |
| 736 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 737 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 738 | adsize = sizeof(struct short_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 739 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 740 | adsize = sizeof(struct long_ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | else |
| 742 | return newblock; |
| 743 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 744 | mutex_lock(&sbi->s_alloc_mutex); |
Roel Kluin | 3391faa | 2009-06-22 23:12:29 +0200 | [diff] [blame] | 745 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | goal = 0; |
| 747 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 748 | /* We search for the closest matching block to goal. If we find |
| 749 | a exact hit, we stop. Otherwise we keep going till we run out |
| 750 | of extents. We store the buffer_head, bloc, and extoffset |
| 751 | of the current closest match and use that when we are done. |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 752 | */ |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 753 | epos.offset = sizeof(struct unallocSpaceEntry); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 754 | epos.block = iinfo->i_location; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 755 | epos.bh = goal_epos.bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 757 | while (spread && |
| 758 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 759 | if (goal >= eloc.logicalBlockNum) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 760 | if (goal < eloc.logicalBlockNum + |
| 761 | (elen >> sb->s_blocksize_bits)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | nspread = 0; |
| 763 | else |
| 764 | nspread = goal - eloc.logicalBlockNum - |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 765 | (elen >> sb->s_blocksize_bits); |
| 766 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | nspread = eloc.logicalBlockNum - goal; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 770 | if (nspread < spread) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | spread = nspread; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 772 | if (goal_epos.bh != epos.bh) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 773 | brelse(goal_epos.bh); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 774 | goal_epos.bh = epos.bh; |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 775 | get_bh(goal_epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 777 | goal_epos.block = epos.block; |
| 778 | goal_epos.offset = epos.offset - adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | goal_eloc = eloc; |
| 780 | goal_elen = (etype << 30) | elen; |
| 781 | } |
| 782 | } |
| 783 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 784 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 786 | if (spread == 0xFFFFFFFF) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 787 | brelse(goal_epos.bh); |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 788 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /* Only allocate blocks from the beginning of the extent. |
| 793 | That way, we only delete (empty) extents, never have to insert an |
| 794 | extent because of splitting */ |
| 795 | /* This works, but very poorly.... */ |
| 796 | |
| 797 | newblock = goal_eloc.logicalBlockNum; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 798 | goal_eloc.logicalBlockNum++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | goal_elen -= sb->s_blocksize; |
| 800 | |
Jan Kara | bacfb7c | 2009-01-26 17:20:46 +0100 | [diff] [blame] | 801 | if (inode && vfs_dq_alloc_block(inode, 1)) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 802 | brelse(goal_epos.bh); |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 803 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | *err = -EDQUOT; |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | if (goal_elen) |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 809 | udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 811 | udf_delete_aext(table, goal_epos, goal_eloc, goal_elen); |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 812 | brelse(goal_epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 814 | udf_add_free_space(sb, partition, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 816 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | *err = 0; |
| 818 | return newblock; |
| 819 | } |
| 820 | |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 821 | void udf_free_blocks(struct super_block *sb, struct inode *inode, |
| 822 | struct kernel_lb_addr *bloc, uint32_t offset, |
| 823 | uint32_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 825 | uint16_t partition = bloc->partitionReferenceNum; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 826 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 828 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { |
Jan Kara | e650b94 | 2008-12-01 13:06:10 +0100 | [diff] [blame] | 829 | udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap, |
| 830 | bloc, offset, count); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 831 | } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { |
Jan Kara | e650b94 | 2008-12-01 13:06:10 +0100 | [diff] [blame] | 832 | udf_table_free_blocks(sb, inode, map->s_uspace.s_table, |
| 833 | bloc, offset, count); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 834 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) { |
Jan Kara | e650b94 | 2008-12-01 13:06:10 +0100 | [diff] [blame] | 835 | udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap, |
| 836 | bloc, offset, count); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 837 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) { |
Jan Kara | e650b94 | 2008-12-01 13:06:10 +0100 | [diff] [blame] | 838 | udf_table_free_blocks(sb, inode, map->s_fspace.s_table, |
| 839 | bloc, offset, count); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 843 | inline int udf_prealloc_blocks(struct super_block *sb, |
| 844 | struct inode *inode, |
| 845 | uint16_t partition, uint32_t first_block, |
| 846 | uint32_t block_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 848 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
| 849 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 850 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | return udf_bitmap_prealloc_blocks(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 852 | map->s_uspace.s_bitmap, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 853 | partition, first_block, |
| 854 | block_count); |
| 855 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | return udf_table_prealloc_blocks(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 857 | map->s_uspace.s_table, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 858 | partition, first_block, |
| 859 | block_count); |
| 860 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return udf_bitmap_prealloc_blocks(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 862 | map->s_fspace.s_bitmap, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 863 | partition, first_block, |
| 864 | block_count); |
| 865 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | return udf_table_prealloc_blocks(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 867 | map->s_fspace.s_table, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 868 | partition, first_block, |
| 869 | block_count); |
| 870 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 874 | inline int udf_new_block(struct super_block *sb, |
| 875 | struct inode *inode, |
| 876 | uint16_t partition, uint32_t goal, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 878 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 879 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 880 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
| 881 | return udf_bitmap_new_block(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 882 | map->s_uspace.s_bitmap, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 883 | partition, goal, err); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 884 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | return udf_table_new_block(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 886 | map->s_uspace.s_table, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 887 | partition, goal, err); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 888 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return udf_bitmap_new_block(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 890 | map->s_fspace.s_bitmap, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 891 | partition, goal, err); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 892 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return udf_table_new_block(sb, inode, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 894 | map->s_fspace.s_table, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 895 | partition, goal, err); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 896 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | *err = -EIO; |
| 898 | return 0; |
| 899 | } |
| 900 | } |