blob: b608efaa4cee16426016f8702268967f943b9621 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * balloc.c
3 *
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/buffer_head.h>
25#include <linux/bitops.h>
26
27#include "udf_i.h"
28#include "udf_sb.h"
29
Marcin Slusarz4b111112008-02-08 04:20:36 -080030#define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
31#define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
Marcin Slusarz4b111112008-02-08 04:20:36 -080033#define udf_find_next_one_bit(addr, size, offset) \
Akinobu Mita3a065fc2010-02-23 23:11:13 +090034 ext2_find_next_bit(addr, size, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070036static int read_block_bitmap(struct super_block *sb,
37 struct udf_bitmap *bitmap, unsigned int block,
38 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 struct buffer_head *bh = NULL;
41 int retval = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020042 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080045 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Pekka Enberg97e961f2008-10-15 12:29:03 +020047 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080048 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 bitmap->s_block_bitmap[bitmap_nr] = bh;
52 return retval;
53}
54
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070055static int __load_block_bitmap(struct super_block *sb,
56 struct udf_bitmap *bitmap,
57 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 int retval = 0;
60 int nr_groups = bitmap->s_nr_groups;
61
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070062 if (block_group >= nr_groups) {
63 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
64 nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070067 if (bitmap->s_block_bitmap[block_group]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return block_group;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070069 } else {
70 retval = read_block_bitmap(sb, bitmap, block_group,
71 block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (retval < 0)
73 return retval;
74 return block_group;
75 }
76}
77
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070078static inline int load_block_bitmap(struct super_block *sb,
79 struct udf_bitmap *bitmap,
80 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
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 Kara146bca72009-03-16 18:27:37 +010095static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
Marcin Slusarz742ba022008-02-08 04:20:40 -080096{
Jan Kara146bca72009-03-16 18:27:37 +010097 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -080098 struct logicalVolIntegrityDesc *lvid;
99
Jan Kara146bca72009-03-16 18:27:37 +0100100 if (!sbi->s_lvid_bh)
101 return;
Marcin Slusarz742ba022008-02-08 04:20:40 -0800102
103 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100104 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
Jan Kara146bca72009-03-16 18:27:37 +0100105 udf_updated_lvid(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800106}
107
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108static void udf_bitmap_free_blocks(struct super_block *sb,
109 struct inode *inode,
110 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200111 struct kernel_lb_addr *bloc,
112 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200117 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 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 Molnar1e7933d2006-03-23 03:00:44 -0800125 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200126 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300127 if (bloc->logicalBlockNum + count < count ||
128 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700129 udf_debug("%d < %d || %d + %d > %d\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +0200130 bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
131 count, partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 goto error_return;
133 }
134
Pekka Enberg97e961f2008-10-15 12:29:03 +0200135 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800136 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800138 do {
139 overflow = 0;
140 block_group = block >> (sb->s_blocksize_bits + 3);
141 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800143 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800150 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]);
160 } else {
Jan Kara146bca72009-03-16 18:27:37 +0100161 udf_add_free_space(sb, sbi->s_partition, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800162 }
163 }
164 mark_buffer_dirty(bh);
165 if (overflow) {
166 block += count;
167 count = overflow;
168 }
169 } while (overflow);
170
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700171error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800172 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700175static int udf_bitmap_prealloc_blocks(struct super_block *sb,
176 struct inode *inode,
177 struct udf_bitmap *bitmap,
178 uint16_t partition, uint32_t first_block,
179 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct udf_sb_info *sbi = UDF_SB(sb);
182 int alloc_count = 0;
183 int bit, block, block_group, group_start;
184 int nr_groups, bitmap_nr;
185 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800186 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800188 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800189 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200190 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto out;
192
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800193 if (first_block + block_count > part_len)
194 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800196 do {
197 nr_groups = udf_compute_nr_groups(sb, partition);
198 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
199 block_group = block >> (sb->s_blocksize_bits + 3);
200 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800202 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
203 if (bitmap_nr < 0)
204 goto out;
205 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800207 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800209 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Jan Kara36350462010-05-19 16:28:56 +0200210 if (!udf_clear_bit(bit, bh->b_data))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800211 goto out;
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800212 block_count--;
213 alloc_count++;
214 bit++;
215 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800217 mark_buffer_dirty(bh);
218 } while (block_count > 0);
219
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220out:
Jan Kara146bca72009-03-16 18:27:37 +0100221 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800222 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return alloc_count;
224}
225
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226static int udf_bitmap_new_block(struct super_block *sb,
227 struct inode *inode,
228 struct udf_bitmap *bitmap, uint16_t partition,
229 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int end_goal, nr_groups, bitmap_nr, i;
234 struct buffer_head *bh = NULL;
235 char *ptr;
236 int newblock = 0;
237
238 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800239 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200242 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goal = 0;
244
245 nr_groups = bitmap->s_nr_groups;
246 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
247 block_group = block >> (sb->s_blocksize_bits + 3);
248 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
249
250 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
251 if (bitmap_nr < 0)
252 goto error_return;
253 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700254 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
255 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700257 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700259 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 end_goal = (bit + 63) & ~63;
263 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
264 if (bit < end_goal)
265 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700266
Marcin Slusarz4b111112008-02-08 04:20:36 -0800267 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
268 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700270 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 bit = newbit;
272 goto search_back;
273 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700274
Marcin Slusarz4b111112008-02-08 04:20:36 -0800275 newbit = udf_find_next_one_bit(bh->b_data,
276 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700277 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 bit = newbit;
279 goto got_block;
280 }
281 }
282
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700283 for (i = 0; i < (nr_groups * 2); i++) {
284 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (block_group >= nr_groups)
286 block_group = 0;
287 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
288
289 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
290 if (bitmap_nr < 0)
291 goto error_return;
292 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700293 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700294 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
295 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700296 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 bit = (ptr - ((char *)bh->b_data)) << 3;
298 break;
299 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700300 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700301 bit = udf_find_next_one_bit((char *)bh->b_data,
302 sb->s_blocksize << 3,
303 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (bit < sb->s_blocksize << 3)
305 break;
306 }
307 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800309 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return newblock;
311 }
312 if (bit < sb->s_blocksize << 3)
313 goto search_back;
314 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800315 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
316 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700317 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800318 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
320 }
321
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700322search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800323 i = 0;
324 while (i < 7 && bit > (group_start << 3) &&
325 udf_test_bit(bit - 1, bh->b_data)) {
326 ++i;
327 --bit;
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700330got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700334 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 udf_debug("bit already cleared for block %d\n", bit);
336 goto repeat;
337 }
338
339 mark_buffer_dirty(bh);
340
Jan Kara146bca72009-03-16 18:27:37 +0100341 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800342 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *err = 0;
344 return newblock;
345
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700346error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800348 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352static void udf_table_free_blocks(struct super_block *sb,
353 struct inode *inode,
354 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200355 struct kernel_lb_addr *bloc,
356 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700357 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200360 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700362 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200363 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700364 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int8_t etype;
366 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800367 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800369 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200370 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300371 if (bloc->logicalBlockNum + count < count ||
372 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700373 udf_debug("%d < %d || %d + %d > %d\n",
Jan Kara1fefd082009-11-30 19:47:10 +0100374 bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200375 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto error_return;
377 }
378
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800379 iinfo = UDF_I(table);
Jan Kara146bca72009-03-16 18:27:37 +0100380 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Pekka Enberg97e961f2008-10-15 12:29:03 +0200382 start = bloc->logicalBlockNum + offset;
383 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jan Karaff116fc2007-05-08 00:35:14 -0700385 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800387 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700388 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700390 while (count &&
391 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800392 if (((eloc.logicalBlockNum +
393 (elen >> sb->s_blocksize_bits)) == start)) {
394 if ((0x3FFFFFFF - elen) <
395 (count << sb->s_blocksize_bits)) {
396 uint32_t tmp = ((0x3FFFFFFF - elen) >>
397 sb->s_blocksize_bits);
398 count -= tmp;
399 start += tmp;
400 elen = (etype << 30) |
401 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700402 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800403 elen = (etype << 30) |
404 (elen +
405 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 start += count;
407 count = 0;
408 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200409 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700410 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800411 if ((0x3FFFFFFF - elen) <
412 (count << sb->s_blocksize_bits)) {
413 uint32_t tmp = ((0x3FFFFFFF - elen) >>
414 sb->s_blocksize_bits);
415 count -= tmp;
416 end -= tmp;
417 eloc.logicalBlockNum -= tmp;
418 elen = (etype << 30) |
419 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 elen = (etype << 30) |
423 (elen +
424 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 end -= count;
426 count = 0;
427 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200428 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700431 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700433 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700434 brelse(oepos.bh);
435 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700436 oepos.bh = epos.bh;
437 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700438 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700439 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700443 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700444 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800445 * NOTE: we CANNOT use udf_add_aext here, as it can try to
446 * allocate a new block, and since we hold the super block
447 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700448 *
449 * We copy the behavior of udf_add_aext, but instead of
450 * trying to allocate a new block close to the existing one,
451 * we just steal a block from the extent we are trying to add.
452 *
453 * It would be nice if the blocks were close together, but it
454 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200458 struct short_ad *sad = NULL;
459 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct allocExtDesc *aed;
461
462 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700463 elen = EXT_RECORDED_ALLOCATED |
464 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800466 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200467 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800468 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200469 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800470 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700471 brelse(oepos.bh);
472 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto error_return;
474 }
475
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700476 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500477 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700479
Jan Kara3bf25cb2007-05-08 00:35:16 -0700480 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700481 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700484 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700485 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 elen -= sb->s_blocksize;
487
Marcin Slusarz4b111112008-02-08 04:20:36 -0800488 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200489 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800490 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700491 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto error_return;
493 }
Jan Karaff116fc2007-05-08 00:35:14 -0700494 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800495 aed->previousAllocExtLocation =
496 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700497 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700498 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800500 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800501 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800502 dptr = epos.bh->b_data +
503 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800505 epos.offset = sizeof(struct allocExtDesc) +
506 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700507 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700508 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700510 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700511 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800512 aed = (struct allocExtDesc *)
513 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100514 le32_add_cpu(&aed->lengthAllocDescs,
515 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700516 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800517 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800518 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800519 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 mark_inode_dirty(table);
521 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700522 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800524 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800525 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
526 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200527 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800529 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
530 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200531 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700532
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800533 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800534 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200535 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800536 sad->extLength = cpu_to_le32(
537 EXT_NEXT_EXTENT_ALLOCDECS |
538 sb->s_blocksize);
539 sad->extPosition =
540 cpu_to_le32(epos.block.logicalBlockNum);
541 break;
542 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200543 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800544 lad->extLength = cpu_to_le32(
545 EXT_NEXT_EXTENT_ALLOCDECS |
546 sb->s_blocksize);
547 lad->extLocation =
548 cpu_to_lelb(epos.block);
549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700552 udf_update_tag(oepos.bh->b_data, loffset);
553 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Marcin Slusarz4b111112008-02-08 04:20:36 -0800559 /* It's possible that stealing the block emptied the extent */
560 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200561 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800564 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700566 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700567 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100568 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700569 udf_update_tag(epos.bh->b_data, epos.offset);
570 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 }
573 }
574
Jan Kara3bf25cb2007-05-08 00:35:16 -0700575 brelse(epos.bh);
576 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700578error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800579 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return;
581}
582
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700583static int udf_table_prealloc_blocks(struct super_block *sb,
584 struct inode *inode,
585 struct inode *table, uint16_t partition,
586 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct udf_sb_info *sbi = UDF_SB(sb);
589 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700590 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200591 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700592 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800594 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Roel Kluin3391faa2009-06-22 23:12:29 +0200596 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
598
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800599 iinfo = UDF_I(table);
600 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200601 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800602 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200603 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 else
605 return 0;
606
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800607 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700608 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800609 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700610 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 eloc.logicalBlockNum = 0xFFFFFFFF;
612
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700613 while (first_block != eloc.logicalBlockNum &&
614 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700616 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700617 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700620 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700621 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Kara36350462010-05-19 16:28:56 +0200624 if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 alloc_count = block_count;
626 eloc.logicalBlockNum += alloc_count;
627 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200628 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800629 (etype << 30) | elen, 1);
630 } else
631 udf_delete_aext(table, epos, eloc,
632 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700633 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jan Kara3bf25cb2007-05-08 00:35:16 -0700637 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jan Kara146bca72009-03-16 18:27:37 +0100639 if (alloc_count)
640 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800641 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return alloc_count;
643}
644
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700645static int udf_table_new_block(struct super_block *sb,
646 struct inode *inode,
647 struct inode *table, uint16_t partition,
648 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 struct udf_sb_info *sbi = UDF_SB(sb);
651 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
652 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700653 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200654 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700655 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800657 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 *err = -ENOSPC;
660
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800661 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200662 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800663 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200664 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 else
666 return newblock;
667
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800668 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200669 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goal = 0;
671
Marcin Slusarz4b111112008-02-08 04:20:36 -0800672 /* We search for the closest matching block to goal. If we find
673 a exact hit, we stop. Otherwise we keep going till we run out
674 of extents. We store the buffer_head, bloc, and extoffset
675 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700676 */
Jan Karaff116fc2007-05-08 00:35:14 -0700677 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800678 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700679 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700681 while (spread &&
682 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700683 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800684 if (goal < eloc.logicalBlockNum +
685 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 nspread = 0;
687 else
688 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700689 (elen >> sb->s_blocksize_bits);
690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700694 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700696 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700697 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700698 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700699 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Jan Karaff116fc2007-05-08 00:35:14 -0700701 goal_epos.block = epos.block;
702 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goal_eloc = eloc;
704 goal_elen = (etype << 30) | elen;
705 }
706 }
707
Jan Kara3bf25cb2007-05-08 00:35:16 -0700708 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700710 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700711 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800712 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714 }
715
716 /* Only allocate blocks from the beginning of the extent.
717 That way, we only delete (empty) extents, never have to insert an
718 extent because of splitting */
719 /* This works, but very poorly.... */
720
721 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700722 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goal_elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200726 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 else
Jan Karaff116fc2007-05-08 00:35:14 -0700728 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700729 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jan Kara146bca72009-03-16 18:27:37 +0100731 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800733 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 *err = 0;
735 return newblock;
736}
737
Pekka Enberg97e961f2008-10-15 12:29:03 +0200738void udf_free_blocks(struct super_block *sb, struct inode *inode,
739 struct kernel_lb_addr *bloc, uint32_t offset,
740 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200742 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800743 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800745 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100746 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
747 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800748 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100749 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
750 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800751 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100752 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
753 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800754 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100755 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
756 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700760inline int udf_prealloc_blocks(struct super_block *sb,
761 struct inode *inode,
762 uint16_t partition, uint32_t first_block,
763 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800765 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
766
Marcin Slusarz4b111112008-02-08 04:20:36 -0800767 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800769 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800770 partition, first_block,
771 block_count);
772 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800774 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800775 partition, first_block,
776 block_count);
777 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800779 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800780 partition, first_block,
781 block_count);
782 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800784 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800785 partition, first_block,
786 block_count);
787 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
789}
790
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791inline int udf_new_block(struct super_block *sb,
792 struct inode *inode,
793 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800795 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700796
Marcin Slusarz4b111112008-02-08 04:20:36 -0800797 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
798 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800799 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700800 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800801 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800803 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700804 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800805 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800807 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700808 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800809 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800811 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700812 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800813 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *err = -EIO;
815 return 0;
816 }
817}