blob: 95518a9f589e395fac2adb4d6bb46d335b977e11 [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) {
62 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
63 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,
108 struct inode *inode,
109 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200110 struct kernel_lb_addr *bloc,
111 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700112 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700115 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200116 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long block;
118 unsigned long block_group;
119 unsigned long bit;
120 unsigned long i;
121 int bitmap_nr;
122 unsigned long overflow;
123
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800124 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200125 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300126 if (bloc->logicalBlockNum + count < count ||
127 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700128 udf_debug("%d < %d || %d + %d > %d\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +0200129 bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
130 count, 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",
158 ((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,
174 struct inode *inode,
175 struct udf_bitmap *bitmap,
176 uint16_t partition, uint32_t first_block,
177 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct udf_sb_info *sbi = UDF_SB(sb);
180 int alloc_count = 0;
181 int bit, block, block_group, group_start;
182 int nr_groups, bitmap_nr;
183 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800184 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800186 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800187 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200188 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out;
190
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800191 if (first_block + block_count > part_len)
192 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800194 do {
195 nr_groups = udf_compute_nr_groups(sb, partition);
196 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
197 block_group = block >> (sb->s_blocksize_bits + 3);
198 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800200 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
201 if (bitmap_nr < 0)
202 goto out;
203 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800205 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800207 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Jan Kara36350462010-05-19 16:28:56 +0200208 if (!udf_clear_bit(bit, bh->b_data))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800209 goto out;
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800210 block_count--;
211 alloc_count++;
212 bit++;
213 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800215 mark_buffer_dirty(bh);
216 } while (block_count > 0);
217
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218out:
Jan Kara146bca72009-03-16 18:27:37 +0100219 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800220 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return alloc_count;
222}
223
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700224static int udf_bitmap_new_block(struct super_block *sb,
225 struct inode *inode,
226 struct udf_bitmap *bitmap, uint16_t partition,
227 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700230 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int end_goal, nr_groups, bitmap_nr, i;
232 struct buffer_head *bh = NULL;
233 char *ptr;
234 int newblock = 0;
235
236 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800237 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200240 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goal = 0;
242
243 nr_groups = bitmap->s_nr_groups;
244 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
245 block_group = block >> (sb->s_blocksize_bits + 3);
246 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
247
248 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
249 if (bitmap_nr < 0)
250 goto error_return;
251 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700252 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
253 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700255 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 end_goal = (bit + 63) & ~63;
261 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
262 if (bit < end_goal)
263 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700264
Marcin Slusarz4b111112008-02-08 04:20:36 -0800265 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
266 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700268 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 bit = newbit;
270 goto search_back;
271 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700272
Marcin Slusarz4b111112008-02-08 04:20:36 -0800273 newbit = udf_find_next_one_bit(bh->b_data,
274 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700275 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 bit = newbit;
277 goto got_block;
278 }
279 }
280
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700281 for (i = 0; i < (nr_groups * 2); i++) {
282 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (block_group >= nr_groups)
284 block_group = 0;
285 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
286
287 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
288 if (bitmap_nr < 0)
289 goto error_return;
290 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700292 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
293 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 bit = (ptr - ((char *)bh->b_data)) << 3;
296 break;
297 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 } else {
Dirk Behme6f644e52011-02-22 14:04:19 -0500299 bit = udf_find_next_one_bit(bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700300 sb->s_blocksize << 3,
301 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (bit < sb->s_blocksize << 3)
303 break;
304 }
305 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700306 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800307 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return newblock;
309 }
310 if (bit < sb->s_blocksize << 3)
311 goto search_back;
312 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800313 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
314 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700315 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800316 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318 }
319
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700320search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800321 i = 0;
322 while (i < 7 && bit > (group_start << 3) &&
323 udf_test_bit(bit - 1, bh->b_data)) {
324 ++i;
325 --bit;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700330 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 udf_debug("bit already cleared for block %d\n", bit);
334 goto repeat;
335 }
336
337 mark_buffer_dirty(bh);
338
Jan Kara146bca72009-03-16 18:27:37 +0100339 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800340 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *err = 0;
342 return newblock;
343
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700344error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800346 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700350static void udf_table_free_blocks(struct super_block *sb,
351 struct inode *inode,
352 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200353 struct kernel_lb_addr *bloc,
354 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700355 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200358 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700360 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200361 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700362 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int8_t etype;
364 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800365 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800367 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200368 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300369 if (bloc->logicalBlockNum + count < count ||
370 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700371 udf_debug("%d < %d || %d + %d > %d\n",
Jan Kara1fefd082009-11-30 19:47:10 +0100372 bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200373 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto error_return;
375 }
376
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800377 iinfo = UDF_I(table);
Jan Kara146bca72009-03-16 18:27:37 +0100378 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Pekka Enberg97e961f2008-10-15 12:29:03 +0200380 start = bloc->logicalBlockNum + offset;
381 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jan Karaff116fc2007-05-08 00:35:14 -0700383 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800385 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700386 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700388 while (count &&
389 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800390 if (((eloc.logicalBlockNum +
391 (elen >> sb->s_blocksize_bits)) == start)) {
392 if ((0x3FFFFFFF - elen) <
393 (count << sb->s_blocksize_bits)) {
394 uint32_t tmp = ((0x3FFFFFFF - elen) >>
395 sb->s_blocksize_bits);
396 count -= tmp;
397 start += tmp;
398 elen = (etype << 30) |
399 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700400 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800401 elen = (etype << 30) |
402 (elen +
403 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 start += count;
405 count = 0;
406 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200407 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700408 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800409 if ((0x3FFFFFFF - elen) <
410 (count << sb->s_blocksize_bits)) {
411 uint32_t tmp = ((0x3FFFFFFF - elen) >>
412 sb->s_blocksize_bits);
413 count -= tmp;
414 end -= tmp;
415 eloc.logicalBlockNum -= tmp;
416 elen = (etype << 30) |
417 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700418 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800420 elen = (etype << 30) |
421 (elen +
422 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 end -= count;
424 count = 0;
425 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200426 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700429 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700431 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700432 brelse(oepos.bh);
433 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700434 oepos.bh = epos.bh;
435 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700437 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700441 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700442 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800443 * NOTE: we CANNOT use udf_add_aext here, as it can try to
444 * allocate a new block, and since we hold the super block
445 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700446 *
447 * We copy the behavior of udf_add_aext, but instead of
448 * trying to allocate a new block close to the existing one,
449 * we just steal a block from the extent we are trying to add.
450 *
451 * It would be nice if the blocks were close together, but it
452 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200456 struct short_ad *sad = NULL;
457 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct allocExtDesc *aed;
459
460 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700461 elen = EXT_RECORDED_ALLOCATED |
462 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800464 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200465 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800466 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200467 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800468 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700469 brelse(oepos.bh);
470 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto error_return;
472 }
473
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700474 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500475 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700477
Jan Kara3bf25cb2007-05-08 00:35:16 -0700478 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700479 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700482 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700483 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 elen -= sb->s_blocksize;
485
Marcin Slusarz4b111112008-02-08 04:20:36 -0800486 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200487 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800488 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700489 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto error_return;
491 }
Jan Karaff116fc2007-05-08 00:35:14 -0700492 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800493 aed->previousAllocExtLocation =
494 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700495 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700496 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800498 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800499 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800500 dptr = epos.bh->b_data +
501 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800503 epos.offset = sizeof(struct allocExtDesc) +
504 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700506 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700508 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700509 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800510 aed = (struct allocExtDesc *)
511 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100512 le32_add_cpu(&aed->lengthAllocDescs,
513 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700514 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800515 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800516 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800517 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 mark_inode_dirty(table);
519 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700520 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800522 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800523 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
524 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200525 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800527 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
528 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200529 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700530
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800531 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800532 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200533 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800534 sad->extLength = cpu_to_le32(
535 EXT_NEXT_EXTENT_ALLOCDECS |
536 sb->s_blocksize);
537 sad->extPosition =
538 cpu_to_le32(epos.block.logicalBlockNum);
539 break;
540 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200541 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800542 lad->extLength = cpu_to_le32(
543 EXT_NEXT_EXTENT_ALLOCDECS |
544 sb->s_blocksize);
545 lad->extLocation =
546 cpu_to_lelb(epos.block);
547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700549 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700550 udf_update_tag(oepos.bh->b_data, loffset);
551 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700552 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Marcin Slusarz4b111112008-02-08 04:20:36 -0800557 /* It's possible that stealing the block emptied the extent */
558 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200559 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700561 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800562 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700564 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700565 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100566 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700567 udf_update_tag(epos.bh->b_data, epos.offset);
568 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 }
571 }
572
Jan Kara3bf25cb2007-05-08 00:35:16 -0700573 brelse(epos.bh);
574 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700576error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800577 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return;
579}
580
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700581static int udf_table_prealloc_blocks(struct super_block *sb,
582 struct inode *inode,
583 struct inode *table, uint16_t partition,
584 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct udf_sb_info *sbi = UDF_SB(sb);
587 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700588 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200589 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700590 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800592 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Roel Kluin3391faa2009-06-22 23:12:29 +0200594 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800597 iinfo = UDF_I(table);
598 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200599 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800600 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200601 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 else
603 return 0;
604
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800605 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700606 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800607 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700608 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 eloc.logicalBlockNum = 0xFFFFFFFF;
610
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700611 while (first_block != eloc.logicalBlockNum &&
612 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700615 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700618 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700619 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Kara36350462010-05-19 16:28:56 +0200622 if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 alloc_count = block_count;
624 eloc.logicalBlockNum += alloc_count;
625 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200626 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800627 (etype << 30) | elen, 1);
628 } else
629 udf_delete_aext(table, epos, eloc,
630 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jan Kara3bf25cb2007-05-08 00:35:16 -0700635 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jan Kara146bca72009-03-16 18:27:37 +0100637 if (alloc_count)
638 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800639 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return alloc_count;
641}
642
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643static int udf_table_new_block(struct super_block *sb,
644 struct inode *inode,
645 struct inode *table, uint16_t partition,
646 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct udf_sb_info *sbi = UDF_SB(sb);
649 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
650 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700651 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200652 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700653 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800655 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 *err = -ENOSPC;
658
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800659 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200660 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800661 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200662 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 else
664 return newblock;
665
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800666 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200667 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goal = 0;
669
Marcin Slusarz4b111112008-02-08 04:20:36 -0800670 /* We search for the closest matching block to goal. If we find
671 a exact hit, we stop. Otherwise we keep going till we run out
672 of extents. We store the buffer_head, bloc, and extoffset
673 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700674 */
Jan Karaff116fc2007-05-08 00:35:14 -0700675 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800676 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700677 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700679 while (spread &&
680 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700681 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800682 if (goal < eloc.logicalBlockNum +
683 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 nspread = 0;
685 else
686 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700687 (elen >> sb->s_blocksize_bits);
688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700692 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700694 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700695 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700696 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700697 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Jan Karaff116fc2007-05-08 00:35:14 -0700699 goal_epos.block = epos.block;
700 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goal_eloc = eloc;
702 goal_elen = (etype << 30) | elen;
703 }
704 }
705
Jan Kara3bf25cb2007-05-08 00:35:16 -0700706 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700708 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700709 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800710 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return 0;
712 }
713
714 /* Only allocate blocks from the beginning of the extent.
715 That way, we only delete (empty) extents, never have to insert an
716 extent because of splitting */
717 /* This works, but very poorly.... */
718
719 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goal_elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200724 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else
Jan Karaff116fc2007-05-08 00:35:14 -0700726 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700727 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Jan Kara146bca72009-03-16 18:27:37 +0100729 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800731 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *err = 0;
733 return newblock;
734}
735
Pekka Enberg97e961f2008-10-15 12:29:03 +0200736void udf_free_blocks(struct super_block *sb, struct inode *inode,
737 struct kernel_lb_addr *bloc, uint32_t offset,
738 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200740 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800741 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800743 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100744 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
745 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800746 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100747 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
748 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800749 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100750 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
751 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800752 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100753 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
754 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758inline int udf_prealloc_blocks(struct super_block *sb,
759 struct inode *inode,
760 uint16_t partition, uint32_t first_block,
761 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800763 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
764
Marcin Slusarz4b111112008-02-08 04:20:36 -0800765 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800767 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800768 partition, first_block,
769 block_count);
770 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800772 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800773 partition, first_block,
774 block_count);
775 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800777 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800778 partition, first_block,
779 block_count);
780 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800782 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800783 partition, first_block,
784 block_count);
785 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787}
788
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700789inline int udf_new_block(struct super_block *sb,
790 struct inode *inode,
791 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800793 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700794
Marcin Slusarz4b111112008-02-08 04:20:36 -0800795 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
796 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800797 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700798 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800799 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800801 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700802 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800803 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800805 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700806 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800807 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800809 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700810 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *err = -EIO;
813 return 0;
814 }
815}