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