blob: 1ba2baaf43671889f2eb0339e92996a1bf41f7ca [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
Akinobu Mita9ad1e1e2011-03-23 16:42:11 -070030#define udf_clear_bit __test_and_clear_bit_le
31#define udf_set_bit __test_and_set_bit_le
32#define udf_test_bit test_bit_le
33#define udf_find_next_one_bit find_next_bit_le
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070035static int read_block_bitmap(struct super_block *sb,
36 struct udf_bitmap *bitmap, unsigned int block,
37 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 struct buffer_head *bh = NULL;
40 int retval = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020041 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080044 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Pekka Enberg97e961f2008-10-15 12:29:03 +020046 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080047 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 bitmap->s_block_bitmap[bitmap_nr] = bh;
51 return retval;
52}
53
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054static int __load_block_bitmap(struct super_block *sb,
55 struct udf_bitmap *bitmap,
56 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 int retval = 0;
59 int nr_groups = bitmap->s_nr_groups;
60
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061 if (block_group >= nr_groups) {
Joe Perchesa983f362011-10-10 01:08:07 -070062 udf_debug("block_group (%d) > nr_groups (%d)\n",
63 block_group, nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070066 if (bitmap->s_block_bitmap[block_group]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return block_group;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070068 } else {
69 retval = read_block_bitmap(sb, bitmap, block_group,
70 block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (retval < 0)
72 return retval;
73 return block_group;
74 }
75}
76
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070077static inline int load_block_bitmap(struct super_block *sb,
78 struct udf_bitmap *bitmap,
79 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 int slot;
82
83 slot = __load_block_bitmap(sb, bitmap, block_group);
84
85 if (slot < 0)
86 return slot;
87
88 if (!bitmap->s_block_bitmap[slot])
89 return -EIO;
90
91 return slot;
92}
93
Jan Kara146bca72009-03-16 18:27:37 +010094static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
Marcin Slusarz742ba022008-02-08 04:20:40 -080095{
Jan Kara146bca72009-03-16 18:27:37 +010096 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -080097 struct logicalVolIntegrityDesc *lvid;
98
Jan Kara146bca72009-03-16 18:27:37 +010099 if (!sbi->s_lvid_bh)
100 return;
Marcin Slusarz742ba022008-02-08 04:20:40 -0800101
102 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100103 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
Jan Kara146bca72009-03-16 18:27:37 +0100104 udf_updated_lvid(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800105}
106
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107static void udf_bitmap_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200109 struct kernel_lb_addr *bloc,
110 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700111 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200115 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned long block;
117 unsigned long block_group;
118 unsigned long bit;
119 unsigned long i;
120 int bitmap_nr;
121 unsigned long overflow;
122
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800123 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200124 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300125 if (bloc->logicalBlockNum + count < count ||
126 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700127 udf_debug("%d < %d || %d + %d > %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700128 bloc->logicalBlockNum, 0,
129 bloc->logicalBlockNum, count,
130 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto error_return;
132 }
133
Pekka Enberg97e961f2008-10-15 12:29:03 +0200134 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800135 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800137 do {
138 overflow = 0;
139 block_group = block >> (sb->s_blocksize_bits + 3);
140 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800142 /*
143 * Check to see if we are freeing blocks across a group boundary.
144 */
145 if (bit + count > (sb->s_blocksize << 3)) {
146 overflow = bit + count - (sb->s_blocksize << 3);
147 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800149 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
150 if (bitmap_nr < 0)
151 goto error_return;
152
153 bh = bitmap->s_block_bitmap[bitmap_nr];
154 for (i = 0; i < count; i++) {
155 if (udf_set_bit(bit + i, bh->b_data)) {
156 udf_debug("bit %ld already set\n", bit + i);
157 udf_debug("byte=%2x\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700158 ((char *)bh->b_data)[(bit + i) >> 3]);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800159 }
160 }
Jan Kara7abc2e42010-10-20 22:32:02 +0200161 udf_add_free_space(sb, sbi->s_partition, count);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800162 mark_buffer_dirty(bh);
163 if (overflow) {
164 block += count;
165 count = overflow;
166 }
167 } while (overflow);
168
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700169error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800170 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700173static int udf_bitmap_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700174 struct udf_bitmap *bitmap,
175 uint16_t partition, uint32_t first_block,
176 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct udf_sb_info *sbi = UDF_SB(sb);
179 int alloc_count = 0;
180 int bit, block, block_group, group_start;
181 int nr_groups, bitmap_nr;
182 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800183 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800185 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800186 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200187 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
189
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800190 if (first_block + block_count > part_len)
191 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800193 do {
194 nr_groups = udf_compute_nr_groups(sb, partition);
195 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
196 block_group = block >> (sb->s_blocksize_bits + 3);
197 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800199 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
200 if (bitmap_nr < 0)
201 goto out;
202 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800204 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800206 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Jan Kara36350462010-05-19 16:28:56 +0200207 if (!udf_clear_bit(bit, bh->b_data))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800208 goto out;
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800209 block_count--;
210 alloc_count++;
211 bit++;
212 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800214 mark_buffer_dirty(bh);
215 } while (block_count > 0);
216
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700217out:
Jan Kara146bca72009-03-16 18:27:37 +0100218 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800219 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return alloc_count;
221}
222
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223static int udf_bitmap_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700224 struct udf_bitmap *bitmap, uint16_t partition,
225 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700228 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int end_goal, nr_groups, bitmap_nr, i;
230 struct buffer_head *bh = NULL;
231 char *ptr;
232 int newblock = 0;
233
234 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800235 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200238 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goal = 0;
240
241 nr_groups = bitmap->s_nr_groups;
242 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
243 block_group = block >> (sb->s_blocksize_bits + 3);
244 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
245
246 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
247 if (bitmap_nr < 0)
248 goto error_return;
249 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
251 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700253 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700255 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 end_goal = (bit + 63) & ~63;
259 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
260 if (bit < end_goal)
261 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700262
Marcin Slusarz4b111112008-02-08 04:20:36 -0800263 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
264 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700266 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 bit = newbit;
268 goto search_back;
269 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700270
Marcin Slusarz4b111112008-02-08 04:20:36 -0800271 newbit = udf_find_next_one_bit(bh->b_data,
272 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700273 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 bit = newbit;
275 goto got_block;
276 }
277 }
278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 for (i = 0; i < (nr_groups * 2); i++) {
280 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (block_group >= nr_groups)
282 block_group = 0;
283 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
284
285 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
286 if (bitmap_nr < 0)
287 goto error_return;
288 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700290 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
291 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700292 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 bit = (ptr - ((char *)bh->b_data)) << 3;
294 break;
295 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700296 } else {
Dirk Behme6f644e52011-02-22 14:04:19 -0500297 bit = udf_find_next_one_bit(bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700298 sb->s_blocksize << 3,
299 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (bit < sb->s_blocksize << 3)
301 break;
302 }
303 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700304 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800305 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return newblock;
307 }
308 if (bit < sb->s_blocksize << 3)
309 goto search_back;
310 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800311 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
312 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700313 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800314 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316 }
317
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700318search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800319 i = 0;
320 while (i < 7 && bit > (group_start << 3) &&
321 udf_test_bit(bit - 1, bh->b_data)) {
322 ++i;
323 --bit;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700326got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700330 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 udf_debug("bit already cleared for block %d\n", bit);
332 goto repeat;
333 }
334
335 mark_buffer_dirty(bh);
336
Jan Kara146bca72009-03-16 18:27:37 +0100337 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800338 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *err = 0;
340 return newblock;
341
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700342error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800344 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348static void udf_table_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700349 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200350 struct kernel_lb_addr *bloc,
351 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200355 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700357 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200358 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700359 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int8_t etype;
361 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800362 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800364 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200365 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300366 if (bloc->logicalBlockNum + count < count ||
367 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700368 udf_debug("%d < %d || %d + %d > %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700369 bloc->logicalBlockNum, 0,
370 bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200371 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 goto error_return;
373 }
374
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800375 iinfo = UDF_I(table);
Jan Kara146bca72009-03-16 18:27:37 +0100376 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Pekka Enberg97e961f2008-10-15 12:29:03 +0200378 start = bloc->logicalBlockNum + offset;
379 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jan Karaff116fc2007-05-08 00:35:14 -0700381 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800383 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700384 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700386 while (count &&
387 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800388 if (((eloc.logicalBlockNum +
389 (elen >> sb->s_blocksize_bits)) == start)) {
390 if ((0x3FFFFFFF - elen) <
391 (count << sb->s_blocksize_bits)) {
392 uint32_t tmp = ((0x3FFFFFFF - elen) >>
393 sb->s_blocksize_bits);
394 count -= tmp;
395 start += tmp;
396 elen = (etype << 30) |
397 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700398 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800399 elen = (etype << 30) |
400 (elen +
401 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 start += count;
403 count = 0;
404 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200405 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700406 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800407 if ((0x3FFFFFFF - elen) <
408 (count << sb->s_blocksize_bits)) {
409 uint32_t tmp = ((0x3FFFFFFF - elen) >>
410 sb->s_blocksize_bits);
411 count -= tmp;
412 end -= tmp;
413 eloc.logicalBlockNum -= tmp;
414 elen = (etype << 30) |
415 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700416 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800418 elen = (etype << 30) |
419 (elen +
420 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 end -= count;
422 count = 0;
423 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200424 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700429 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700430 brelse(oepos.bh);
431 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700432 oepos.bh = epos.bh;
433 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700434 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700435 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700440 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800441 * NOTE: we CANNOT use udf_add_aext here, as it can try to
442 * allocate a new block, and since we hold the super block
443 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700444 *
445 * We copy the behavior of udf_add_aext, but instead of
446 * trying to allocate a new block close to the existing one,
447 * we just steal a block from the extent we are trying to add.
448 *
449 * It would be nice if the blocks were close together, but it
450 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200454 struct short_ad *sad = NULL;
455 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct allocExtDesc *aed;
457
458 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700459 elen = EXT_RECORDED_ALLOCATED |
460 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800462 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200463 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800464 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200465 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800466 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700467 brelse(oepos.bh);
468 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto error_return;
470 }
471
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700472 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500473 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700475
Jan Kara3bf25cb2007-05-08 00:35:16 -0700476 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700477 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700480 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700481 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 elen -= sb->s_blocksize;
483
Marcin Slusarz4b111112008-02-08 04:20:36 -0800484 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200485 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800486 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700487 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto error_return;
489 }
Jan Karaff116fc2007-05-08 00:35:14 -0700490 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800491 aed->previousAllocExtLocation =
492 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700493 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700494 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800496 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800497 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800498 dptr = epos.bh->b_data +
499 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800501 epos.offset = sizeof(struct allocExtDesc) +
502 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700503 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700504 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700506 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700507 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800508 aed = (struct allocExtDesc *)
509 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100510 le32_add_cpu(&aed->lengthAllocDescs,
511 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700512 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800513 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800514 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800515 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 mark_inode_dirty(table);
517 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700518 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800520 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800521 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
522 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200523 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800525 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
526 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200527 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700528
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800529 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800530 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200531 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800532 sad->extLength = cpu_to_le32(
533 EXT_NEXT_EXTENT_ALLOCDECS |
534 sb->s_blocksize);
535 sad->extPosition =
536 cpu_to_le32(epos.block.logicalBlockNum);
537 break;
538 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200539 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800540 lad->extLength = cpu_to_le32(
541 EXT_NEXT_EXTENT_ALLOCDECS |
542 sb->s_blocksize);
543 lad->extLocation =
544 cpu_to_lelb(epos.block);
545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700548 udf_update_tag(oepos.bh->b_data, loffset);
549 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Marcin Slusarz4b111112008-02-08 04:20:36 -0800555 /* It's possible that stealing the block emptied the extent */
556 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200557 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700559 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800560 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700562 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700563 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100564 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700565 udf_update_tag(epos.bh->b_data, epos.offset);
566 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
569 }
570
Jan Kara3bf25cb2007-05-08 00:35:16 -0700571 brelse(epos.bh);
572 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700574error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800575 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return;
577}
578
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700579static int udf_table_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700580 struct inode *table, uint16_t partition,
581 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct udf_sb_info *sbi = UDF_SB(sb);
584 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700585 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200586 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700587 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800589 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Roel Kluin3391faa2009-06-22 23:12:29 +0200591 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800594 iinfo = UDF_I(table);
595 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200596 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800597 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200598 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 else
600 return 0;
601
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800602 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700603 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800604 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700605 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 eloc.logicalBlockNum = 0xFFFFFFFF;
607
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700608 while (first_block != eloc.logicalBlockNum &&
609 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700612 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700615 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700616 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Kara36350462010-05-19 16:28:56 +0200619 if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 alloc_count = block_count;
621 eloc.logicalBlockNum += alloc_count;
622 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200623 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800624 (etype << 30) | elen, 1);
625 } else
626 udf_delete_aext(table, epos, eloc,
627 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700628 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jan Kara3bf25cb2007-05-08 00:35:16 -0700632 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Jan Kara146bca72009-03-16 18:27:37 +0100634 if (alloc_count)
635 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800636 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return alloc_count;
638}
639
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640static int udf_table_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 struct inode *table, uint16_t partition,
642 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct udf_sb_info *sbi = UDF_SB(sb);
645 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
646 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700647 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200648 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700649 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800651 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 *err = -ENOSPC;
654
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800655 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200656 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800657 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200658 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 else
660 return newblock;
661
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800662 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200663 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goal = 0;
665
Marcin Slusarz4b111112008-02-08 04:20:36 -0800666 /* We search for the closest matching block to goal. If we find
667 a exact hit, we stop. Otherwise we keep going till we run out
668 of extents. We store the buffer_head, bloc, and extoffset
669 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700670 */
Jan Karaff116fc2007-05-08 00:35:14 -0700671 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800672 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700673 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700675 while (spread &&
676 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700677 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800678 if (goal < eloc.logicalBlockNum +
679 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 nspread = 0;
681 else
682 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700683 (elen >> sb->s_blocksize_bits);
684 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700688 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700690 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700691 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700692 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700693 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Jan Karaff116fc2007-05-08 00:35:14 -0700695 goal_epos.block = epos.block;
696 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goal_eloc = eloc;
698 goal_elen = (etype << 30) | elen;
699 }
700 }
701
Jan Kara3bf25cb2007-05-08 00:35:16 -0700702 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700704 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700705 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800706 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708 }
709
710 /* Only allocate blocks from the beginning of the extent.
711 That way, we only delete (empty) extents, never have to insert an
712 extent because of splitting */
713 /* This works, but very poorly.... */
714
715 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700716 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goal_elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200720 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 else
Jan Karaff116fc2007-05-08 00:35:14 -0700722 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700723 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jan Kara146bca72009-03-16 18:27:37 +0100725 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800727 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *err = 0;
729 return newblock;
730}
731
Pekka Enberg97e961f2008-10-15 12:29:03 +0200732void udf_free_blocks(struct super_block *sb, struct inode *inode,
733 struct kernel_lb_addr *bloc, uint32_t offset,
734 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200736 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800737 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800739 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100740 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100741 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800742 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100743 udf_table_free_blocks(sb, map->s_uspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100744 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800745 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100746 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100747 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800748 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100749 udf_table_free_blocks(sb, map->s_fspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100750 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700751 }
Jan Karafd4287d2012-02-16 13:00:14 +0100752
753 if (inode) {
754 inode_sub_bytes(inode,
755 ((sector_t)count) << sb->s_blocksize_bits);
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700759inline 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 Torvalds1da177e2005-04-16 15:20:36 -0700763{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800764 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Karafd4287d2012-02-16 13:00:14 +0100765 sector_t allocated;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800766
Marcin Slusarz4b111112008-02-08 04:20:36 -0800767 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100768 allocated = udf_bitmap_prealloc_blocks(sb,
769 map->s_uspace.s_bitmap,
770 partition, first_block,
771 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800772 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100773 allocated = udf_table_prealloc_blocks(sb,
774 map->s_uspace.s_table,
775 partition, first_block,
776 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800777 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100778 allocated = udf_bitmap_prealloc_blocks(sb,
779 map->s_fspace.s_bitmap,
780 partition, first_block,
781 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800782 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100783 allocated = udf_table_prealloc_blocks(sb,
784 map->s_fspace.s_table,
785 partition, first_block,
786 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800787 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
Jan Karafd4287d2012-02-16 13:00:14 +0100789
790 if (inode && allocated > 0)
791 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
792 return allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700795inline int udf_new_block(struct super_block *sb,
796 struct inode *inode,
797 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800799 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Karafd4287d2012-02-16 13:00:14 +0100800 int block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700801
Marcin Slusarz4b111112008-02-08 04:20:36 -0800802 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100803 block = udf_bitmap_new_block(sb,
804 map->s_uspace.s_bitmap,
805 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800806 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100807 block = udf_table_new_block(sb,
808 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700809 partition, goal, err);
Jan Karafd4287d2012-02-16 13:00:14 +0100810 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
811 block = udf_bitmap_new_block(sb,
812 map->s_fspace.s_bitmap,
813 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800814 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100815 block = udf_table_new_block(sb,
816 map->s_fspace.s_table,
817 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *err = -EIO;
820 return 0;
821 }
Jan Karafd4287d2012-02-16 13:00:14 +0100822 if (inode && block)
823 inode_add_bytes(inode, sb->s_blocksize);
824 return block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}