blob: 3f67d9dc8631803fcdae54e1caf5cfe2dc56bd7f [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
24#include <linux/quotaops.h>
25#include <linux/buffer_head.h>
26#include <linux/bitops.h>
27
28#include "udf_i.h"
29#include "udf_sb.h"
30
31#define udf_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
32#define udf_set_bit(nr,addr) ext2_set_bit(nr,addr)
33#define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
34#define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size)
35#define udf_find_next_one_bit(addr, size, offset) find_next_one_bit(addr, size, offset)
36
37#define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
38#define leNUM_to_cpup(x,y) xleNUM_to_cpup(x,y)
39#define xleNUM_to_cpup(x,y) (le ## x ## _to_cpup(y))
40#define uintBPL_t uint(BITS_PER_LONG)
41#define uint(x) xuint(x)
42#define xuint(x) __le ## x
43
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070044static inline int find_next_one_bit(void *addr, int size, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070046 uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
47 int result = offset & ~(BITS_PER_LONG - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 unsigned long tmp;
49
50 if (offset >= size)
51 return size;
52 size -= result;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053 offset &= (BITS_PER_LONG - 1);
54 if (offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 tmp = leBPL_to_cpup(p++);
56 tmp &= ~0UL << offset;
57 if (size < BITS_PER_LONG)
58 goto found_first;
59 if (tmp)
60 goto found_middle;
61 size -= BITS_PER_LONG;
62 result += BITS_PER_LONG;
63 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070064 while (size & ~(BITS_PER_LONG - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if ((tmp = leBPL_to_cpup(p++)))
66 goto found_middle;
67 result += BITS_PER_LONG;
68 size -= BITS_PER_LONG;
69 }
70 if (!size)
71 return result;
72 tmp = leBPL_to_cpup(p);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070073found_first:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070074 tmp &= ~0UL >> (BITS_PER_LONG - size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070075found_middle:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return result + ffz(~tmp);
77}
78
79#define find_first_one_bit(addr, size)\
80 find_next_one_bit((addr), (size), 0)
81
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070082static int read_block_bitmap(struct super_block *sb,
83 struct udf_bitmap *bitmap, unsigned int block,
84 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct buffer_head *bh = NULL;
87 int retval = 0;
88 kernel_lb_addr loc;
89
90 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080091 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 retval = -EIO;
96 }
97 bitmap->s_block_bitmap[bitmap_nr] = bh;
98 return retval;
99}
100
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700101static int __load_block_bitmap(struct super_block *sb,
102 struct udf_bitmap *bitmap,
103 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 int retval = 0;
106 int nr_groups = bitmap->s_nr_groups;
107
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 if (block_group >= nr_groups) {
109 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
110 nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700113 if (bitmap->s_block_bitmap[block_group]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return block_group;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700115 } else {
116 retval = read_block_bitmap(sb, bitmap, block_group,
117 block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (retval < 0)
119 return retval;
120 return block_group;
121 }
122}
123
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700124static inline int load_block_bitmap(struct super_block *sb,
125 struct udf_bitmap *bitmap,
126 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int slot;
129
130 slot = __load_block_bitmap(sb, bitmap, block_group);
131
132 if (slot < 0)
133 return slot;
134
135 if (!bitmap->s_block_bitmap[slot])
136 return -EIO;
137
138 return slot;
139}
140
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700141static void udf_bitmap_free_blocks(struct super_block *sb,
142 struct inode *inode,
143 struct udf_bitmap *bitmap,
144 kernel_lb_addr bloc, uint32_t offset,
145 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700148 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 unsigned long block;
150 unsigned long block_group;
151 unsigned long bit;
152 unsigned long i;
153 int bitmap_nr;
154 unsigned long overflow;
155
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800156 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (bloc.logicalBlockNum < 0 ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800158 (bloc.logicalBlockNum + count) > sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700159 udf_debug("%d < %d || %d + %d > %d\n",
160 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800161 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 goto error_return;
163 }
164
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 block = bloc.logicalBlockNum + offset + (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700167do_more:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 overflow = 0;
169 block_group = block >> (sb->s_blocksize_bits + 3);
170 bit = block % (sb->s_blocksize << 3);
171
172 /*
173 * Check to see if we are freeing blocks across a group boundary.
174 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700175 if (bit + count > (sb->s_blocksize << 3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 overflow = bit + count - (sb->s_blocksize << 3);
177 count -= overflow;
178 }
179 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
180 if (bitmap_nr < 0)
181 goto error_return;
182
183 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700184 for (i = 0; i < count; i++) {
185 if (udf_set_bit(bit + i, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 udf_debug("bit %ld already set\n", bit + i);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700187 udf_debug("byte=%2x\n", ((char *)bh->b_data)[(bit + i) >> 3]);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700188 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (inode)
190 DQUOT_FREE_BLOCK(inode, 1);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800191 if (sbi->s_lvid_bh) {
192 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
193 lvid->freeSpaceTable[sbi->s_partition] =
194 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 }
197 }
198 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700199 if (overflow) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 block += count;
201 count = overflow;
202 goto do_more;
203 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700204error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 sb->s_dirt = 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800206 if (sbi->s_lvid_bh)
207 mark_buffer_dirty(sbi->s_lvid_bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800208 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return;
210}
211
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700212static int udf_bitmap_prealloc_blocks(struct super_block *sb,
213 struct inode *inode,
214 struct udf_bitmap *bitmap,
215 uint16_t partition, uint32_t first_block,
216 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct udf_sb_info *sbi = UDF_SB(sb);
219 int alloc_count = 0;
220 int bit, block, block_group, group_start;
221 int nr_groups, bitmap_nr;
222 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800223 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800225 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800226 part_len = sbi->s_partmaps[partition].s_partition_len;
227 if (first_block < 0 || first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
229
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800230 if (first_block + block_count > part_len)
231 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700233repeat:
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800234 nr_groups = udf_compute_nr_groups(sb, partition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
236 block_group = block >> (sb->s_blocksize_bits + 3);
237 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
238
239 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
240 if (bitmap_nr < 0)
241 goto out;
242 bh = bitmap->s_block_bitmap[bitmap_nr];
243
244 bit = block % (sb->s_blocksize << 3);
245
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700246 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700247 if (!udf_test_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700249 } else if (DQUOT_PREALLOC_BLOCK(inode, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700251 } else if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 udf_debug("bit already cleared for block %d\n", bit);
253 DQUOT_FREE_BLOCK(inode, 1);
254 goto out;
255 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700256 block_count--;
257 alloc_count++;
258 bit++;
259 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 mark_buffer_dirty(bh);
262 if (block_count > 0)
263 goto repeat;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700264out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800265 if (sbi->s_lvid_bh) {
266 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
267 lvid->freeSpaceTable[partition] =
268 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - alloc_count);
269 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800272 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return alloc_count;
274}
275
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700276static int udf_bitmap_new_block(struct super_block *sb,
277 struct inode *inode,
278 struct udf_bitmap *bitmap, uint16_t partition,
279 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700282 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int end_goal, nr_groups, bitmap_nr, i;
284 struct buffer_head *bh = NULL;
285 char *ptr;
286 int newblock = 0;
287
288 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800289 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700291repeat:
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800292 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goal = 0;
294
295 nr_groups = bitmap->s_nr_groups;
296 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
297 block_group = block >> (sb->s_blocksize_bits + 3);
298 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
299
300 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
301 if (bitmap_nr < 0)
302 goto error_return;
303 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700304 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
305 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700307 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700309 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 end_goal = (bit + 63) & ~63;
313 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
314 if (bit < end_goal)
315 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700316
317 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700319 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 bit = newbit;
321 goto search_back;
322 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700323
324 newbit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700325 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 bit = newbit;
327 goto got_block;
328 }
329 }
330
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700331 for (i = 0; i < (nr_groups * 2); i++) {
332 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (block_group >= nr_groups)
334 block_group = 0;
335 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
336
337 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
338 if (bitmap_nr < 0)
339 goto error_return;
340 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700342 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
343 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700344 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 bit = (ptr - ((char *)bh->b_data)) << 3;
346 break;
347 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700349 bit = udf_find_next_one_bit((char *)bh->b_data,
350 sb->s_blocksize << 3,
351 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (bit < sb->s_blocksize << 3)
353 break;
354 }
355 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700356 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800357 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return newblock;
359 }
360 if (bit < sb->s_blocksize << 3)
361 goto search_back;
362 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700363 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700364 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800365 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367 }
368
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700369search_back:
370 for (i = 0; i < 7 && bit > (group_start << 3) && udf_test_bit(bit - 1, bh->b_data); i++, bit--)
371 ; /* empty loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700373got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * Check quota for allocation of this block.
377 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700378 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800379 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *err = -EDQUOT;
381 return 0;
382 }
383
384 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700387 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 udf_debug("bit already cleared for block %d\n", bit);
389 goto repeat;
390 }
391
392 mark_buffer_dirty(bh);
393
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800394 if (sbi->s_lvid_bh) {
395 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
396 lvid->freeSpaceTable[partition] =
397 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - 1);
398 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800401 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *err = 0;
403 return newblock;
404
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800407 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409}
410
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700411static void udf_table_free_blocks(struct super_block *sb,
412 struct inode *inode,
413 struct inode *table,
414 kernel_lb_addr bloc, uint32_t offset,
415 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct udf_sb_info *sbi = UDF_SB(sb);
418 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700419 uint32_t elen;
420 kernel_lb_addr eloc;
421 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int8_t etype;
423 int i;
424
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800425 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (bloc.logicalBlockNum < 0 ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800427 (bloc.logicalBlockNum + count) > sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700428 udf_debug("%d < %d || %d + %d > %d\n",
429 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800430 sbi->s_partmaps[bloc.partitionReferenceNum]->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto error_return;
432 }
433
434 /* We do this up front - There are some error conditions that could occure,
435 but.. oh well */
436 if (inode)
437 DQUOT_FREE_BLOCK(inode, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800438 if (sbi->s_lvid_bh) {
439 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
440 lvid->freeSpaceTable[sbi->s_partition] =
441 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]) + count);
442 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 start = bloc.logicalBlockNum + offset;
446 end = bloc.logicalBlockNum + offset + count - 1;
447
Jan Karaff116fc2007-05-08 00:35:14 -0700448 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 elen = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700450 epos.block = oepos.block = UDF_I_LOCATION(table);
451 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700453 while (count &&
454 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
455 if (((eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits)) == start)) {
456 if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits)) {
457 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
458 start += ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
459 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700460 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700461 elen = (etype << 30) | (elen + (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 start += count;
463 count = 0;
464 }
Jan Karaff116fc2007-05-08 00:35:14 -0700465 udf_write_aext(table, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700466 } else if (eloc.logicalBlockNum == (end + 1)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700467 if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits)) {
468 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
469 end -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
470 eloc.logicalBlockNum -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
471 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700472 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700474 elen = (etype << 30) | (elen + (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 end -= count;
476 count = 0;
477 }
Jan Karaff116fc2007-05-08 00:35:14 -0700478 udf_write_aext(table, &oepos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700481 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700483 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700484 brelse(oepos.bh);
485 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700486 oepos.bh = epos.bh;
487 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700488 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700489 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700493 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700494 /*
495 * NOTE: we CANNOT use udf_add_aext here, as it can try to allocate
496 * a new block, and since we hold the super block lock already
497 * very bad things would happen :)
498 *
499 * We copy the behavior of udf_add_aext, but instead of
500 * trying to allocate a new block close to the existing one,
501 * we just steal a block from the extent we are trying to add.
502 *
503 * It would be nice if the blocks were close together, but it
504 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 int adsize;
508 short_ad *sad = NULL;
509 long_ad *lad = NULL;
510 struct allocExtDesc *aed;
511
512 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700513 elen = EXT_RECORDED_ALLOCATED |
514 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700516 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 adsize = sizeof(short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700518 } else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 adsize = sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700520 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700521 brelse(oepos.bh);
522 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto error_return;
524 }
525
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700526 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 char *sptr, *dptr;
528 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700529
Jan Kara3bf25cb2007-05-08 00:35:16 -0700530 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700531 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700534 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700535 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 elen -= sb->s_blocksize;
537
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700538 if (!(epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, epos.block, 0)))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700539 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto error_return;
541 }
Jan Karaff116fc2007-05-08 00:35:14 -0700542 aed = (struct allocExtDesc *)(epos.bh->b_data);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700543 aed->previousAllocExtLocation = cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700545 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 aed->lengthAllocDescs = cpu_to_le32(adsize);
Jan Karaf5cc15d2007-08-30 23:56:22 -0700547 sptr = UDF_I_DATA(table) + epos.offset - adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700548 dptr = epos.bh->b_data + sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 memcpy(dptr, sptr, adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550 epos.offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700552 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700554 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700555 sptr = oepos.bh->b_data + epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700558 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700559 } else {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700560 sptr = UDF_I_DATA(table) + epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 UDF_I_LENALLOC(table) += adsize;
562 mark_inode_dirty(table);
563 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700564 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800566 if (sbi->s_udfrev >= 0x0200)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700567 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, 3, 1,
568 epos.block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700570 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, 2, 1,
571 epos.block.logicalBlockNum, sizeof(tag));
572
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700573 switch (UDF_I_ALLOCTYPE(table)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700574 case ICBTAG_FLAG_AD_SHORT:
575 sad = (short_ad *)sptr;
576 sad->extLength = cpu_to_le32(
577 EXT_NEXT_EXTENT_ALLOCDECS |
578 sb->s_blocksize);
579 sad->extPosition = cpu_to_le32(epos.block.logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700581 case ICBTAG_FLAG_AD_LONG:
582 lad = (long_ad *)sptr;
583 lad->extLength = cpu_to_le32(
584 EXT_NEXT_EXTENT_ALLOCDECS |
585 sb->s_blocksize);
586 lad->extLocation = cpu_to_lelb(epos.block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700589 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700590 udf_update_tag(oepos.bh->b_data, loffset);
591 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700592 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700597 if (elen) { /* It's possible that stealing the block emptied the extent */
Jan Karaff116fc2007-05-08 00:35:14 -0700598 udf_write_aext(table, &epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700600 if (!epos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 UDF_I_LENALLOC(table) += adsize;
602 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700603 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700604 aed = (struct allocExtDesc *)epos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700606 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700607 udf_update_tag(epos.bh->b_data, epos.offset);
608 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 }
611 }
612
Jan Kara3bf25cb2007-05-08 00:35:16 -0700613 brelse(epos.bh);
614 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700616error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800618 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return;
620}
621
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700622static int udf_table_prealloc_blocks(struct super_block *sb,
623 struct inode *inode,
624 struct inode *table, uint16_t partition,
625 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct udf_sb_info *sbi = UDF_SB(sb);
628 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700629 uint32_t elen, adsize;
630 kernel_lb_addr eloc;
631 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int8_t etype = -1;
633
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800634 if (first_block < 0 || first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636
637 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
638 adsize = sizeof(short_ad);
639 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
640 adsize = sizeof(long_ad);
641 else
642 return 0;
643
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800644 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700645 epos.offset = sizeof(struct unallocSpaceEntry);
646 epos.block = UDF_I_LOCATION(table);
647 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 eloc.logicalBlockNum = 0xFFFFFFFF;
649
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700650 while (first_block != eloc.logicalBlockNum &&
651 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700653 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700654 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700657 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700658 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 alloc_count = (elen >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700661 if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700663 } else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 alloc_count = block_count;
665 eloc.logicalBlockNum += alloc_count;
666 elen -= (alloc_count << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700667 udf_write_aext(table, &epos, eloc, (etype << 30) | elen, 1);
668 } else {
669 udf_delete_aext(table, epos, eloc, (etype << 30) | elen);
670 }
671 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jan Kara3bf25cb2007-05-08 00:35:16 -0700675 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800677 if (alloc_count && sbi->s_lvid_bh) {
678 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
679 lvid->freeSpaceTable[partition] =
680 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - alloc_count);
681 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 sb->s_dirt = 1;
683 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800684 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return alloc_count;
686}
687
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700688static int udf_table_new_block(struct super_block *sb,
689 struct inode *inode,
690 struct inode *table, uint16_t partition,
691 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct udf_sb_info *sbi = UDF_SB(sb);
694 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
695 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700696 uint32_t elen, goal_elen = 0;
WANG Cong3ad90ec2007-10-16 23:30:17 -0700697 kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700698 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int8_t etype;
700
701 *err = -ENOSPC;
702
703 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
704 adsize = sizeof(short_ad);
705 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
706 adsize = sizeof(long_ad);
707 else
708 return newblock;
709
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800710 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800711 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goal = 0;
713
714 /* We search for the closest matching block to goal. If we find a exact hit,
715 we stop. Otherwise we keep going till we run out of extents.
716 We store the buffer_head, bloc, and extoffset of the current closest
717 match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700718 */
Jan Karaff116fc2007-05-08 00:35:14 -0700719 epos.offset = sizeof(struct unallocSpaceEntry);
720 epos.block = UDF_I_LOCATION(table);
721 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700723 while (spread &&
724 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700725 if (goal >= eloc.logicalBlockNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700726 if (goal < eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 nspread = 0;
728 else
729 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700730 (elen >> sb->s_blocksize_bits);
731 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700735 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700737 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700738 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700739 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700740 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Jan Karaff116fc2007-05-08 00:35:14 -0700742 goal_epos.block = epos.block;
743 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goal_eloc = eloc;
745 goal_elen = (etype << 30) | elen;
746 }
747 }
748
Jan Kara3bf25cb2007-05-08 00:35:16 -0700749 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700751 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700752 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800753 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755 }
756
757 /* Only allocate blocks from the beginning of the extent.
758 That way, we only delete (empty) extents, never have to insert an
759 extent because of splitting */
760 /* This works, but very poorly.... */
761
762 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goal_elen -= sb->s_blocksize;
765
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700766 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700767 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800768 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *err = -EDQUOT;
770 return 0;
771 }
772
773 if (goal_elen)
Jan Karaff116fc2007-05-08 00:35:14 -0700774 udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 else
Jan Karaff116fc2007-05-08 00:35:14 -0700776 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700777 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800779 if (sbi->s_lvid_bh) {
780 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
781 lvid->freeSpaceTable[partition] =
782 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - 1);
783 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800787 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *err = 0;
789 return newblock;
790}
791
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792inline void udf_free_blocks(struct super_block *sb,
793 struct inode *inode,
794 kernel_lb_addr bloc, uint32_t offset,
795 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 uint16_t partition = bloc.partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800798 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800800 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return udf_bitmap_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800802 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700803 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800804 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return udf_table_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800806 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800808 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return udf_bitmap_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800810 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700811 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800812 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return udf_table_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800814 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700815 bloc, offset, count);
816 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700821inline int udf_prealloc_blocks(struct super_block *sb,
822 struct inode *inode,
823 uint16_t partition, uint32_t first_block,
824 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800826 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
827
828 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800830 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700831 partition, first_block, block_count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800832 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800834 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700835 partition, first_block, block_count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800836 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800838 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700839 partition, first_block, block_count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700843 partition, first_block, block_count);
844 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700849inline int udf_new_block(struct super_block *sb,
850 struct inode *inode,
851 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Jan Kara3bf25cb2007-05-08 00:35:16 -0700853 int ret;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800854 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700855
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800856 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700857 ret = udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800858 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700859 partition, goal, err);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700860 return ret;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800861 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800863 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700864 partition, goal, err);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800865 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800867 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700868 partition, goal, err);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800869 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800871 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700872 partition, goal, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700873 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 *err = -EIO;
875 return 0;
876 }
877}