blob: ab26176f6b91129e924351c067aad96b909223ec [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;
91 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
92
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 ||
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700158 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum)) {
159 udf_debug("%d < %d || %d + %d > %d\n",
160 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
161 UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum));
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);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700191 if (UDF_SB_LVIDBH(sb)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700192 UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)] =
193 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 }
196 }
197 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198 if (overflow) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 block += count;
200 count = overflow;
201 goto do_more;
202 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700203error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 sb->s_dirt = 1;
205 if (UDF_SB_LVIDBH(sb))
206 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800207 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return;
209}
210
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700211static int udf_bitmap_prealloc_blocks(struct super_block *sb,
212 struct inode *inode,
213 struct udf_bitmap *bitmap,
214 uint16_t partition, uint32_t first_block,
215 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct udf_sb_info *sbi = UDF_SB(sb);
218 int alloc_count = 0;
219 int bit, block, block_group, group_start;
220 int nr_groups, bitmap_nr;
221 struct buffer_head *bh;
222
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800223 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
225 goto out;
226
227 if (first_block + block_count > UDF_SB_PARTLEN(sb, partition))
228 block_count = UDF_SB_PARTLEN(sb, partition) - first_block;
229
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700230repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 nr_groups = (UDF_SB_PARTLEN(sb, partition) +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 (sizeof(struct spaceBitmapDesc) << 3) +
233 (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
235 block_group = block >> (sb->s_blocksize_bits + 3);
236 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
237
238 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
239 if (bitmap_nr < 0)
240 goto out;
241 bh = bitmap->s_block_bitmap[bitmap_nr];
242
243 bit = block % (sb->s_blocksize << 3);
244
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700245 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700246 if (!udf_test_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700248 } else if (DQUOT_PREALLOC_BLOCK(inode, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250 } else if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 udf_debug("bit already cleared for block %d\n", bit);
252 DQUOT_FREE_BLOCK(inode, 1);
253 goto out;
254 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700255 block_count--;
256 alloc_count++;
257 bit++;
258 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 mark_buffer_dirty(bh);
261 if (block_count > 0)
262 goto repeat;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700263out:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700264 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700266 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition]) - alloc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
268 }
269 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800270 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return alloc_count;
272}
273
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700274static int udf_bitmap_new_block(struct super_block *sb,
275 struct inode *inode,
276 struct udf_bitmap *bitmap, uint16_t partition,
277 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700280 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int end_goal, nr_groups, bitmap_nr, i;
282 struct buffer_head *bh = NULL;
283 char *ptr;
284 int newblock = 0;
285
286 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800287 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700289repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
291 goal = 0;
292
293 nr_groups = bitmap->s_nr_groups;
294 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
295 block_group = block >> (sb->s_blocksize_bits + 3);
296 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
297
298 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
299 if (bitmap_nr < 0)
300 goto error_return;
301 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700302 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
303 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700305 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700307 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 end_goal = (bit + 63) & ~63;
311 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
312 if (bit < end_goal)
313 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700314
315 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700317 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 bit = newbit;
319 goto search_back;
320 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700321
322 newbit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700323 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 bit = newbit;
325 goto got_block;
326 }
327 }
328
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700329 for (i = 0; i < (nr_groups * 2); i++) {
330 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (block_group >= nr_groups)
332 block_group = 0;
333 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
334
335 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
336 if (bitmap_nr < 0)
337 goto error_return;
338 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700340 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
341 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700342 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 bit = (ptr - ((char *)bh->b_data)) << 3;
344 break;
345 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700347 bit = udf_find_next_one_bit((char *)bh->b_data,
348 sb->s_blocksize << 3,
349 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (bit < sb->s_blocksize << 3)
351 break;
352 }
353 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700354 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800355 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return newblock;
357 }
358 if (bit < sb->s_blocksize << 3)
359 goto search_back;
360 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700362 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800363 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365 }
366
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700367search_back:
368 for (i = 0; i < 7 && bit > (group_start << 3) && udf_test_bit(bit - 1, bh->b_data); i++, bit--)
369 ; /* empty loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700371got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
374 * Check quota for allocation of this block.
375 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700376 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800377 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *err = -EDQUOT;
379 return 0;
380 }
381
382 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700383 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700385 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 udf_debug("bit already cleared for block %d\n", bit);
387 goto repeat;
388 }
389
390 mark_buffer_dirty(bh);
391
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700392 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700394 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition]) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
396 }
397 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800398 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *err = 0;
400 return newblock;
401
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700402error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800404 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406}
407
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700408static void udf_table_free_blocks(struct super_block *sb,
409 struct inode *inode,
410 struct inode *table,
411 kernel_lb_addr bloc, uint32_t offset,
412 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct udf_sb_info *sbi = UDF_SB(sb);
415 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700416 uint32_t elen;
417 kernel_lb_addr eloc;
418 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int8_t etype;
420 int i;
421
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800422 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (bloc.logicalBlockNum < 0 ||
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700424 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum)) {
425 udf_debug("%d < %d || %d + %d > %d\n",
426 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
427 UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto error_return;
429 }
430
431 /* We do this up front - There are some error conditions that could occure,
432 but.. oh well */
433 if (inode)
434 DQUOT_FREE_BLOCK(inode, count);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700435 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700437 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
439 }
440
441 start = bloc.logicalBlockNum + offset;
442 end = bloc.logicalBlockNum + offset + count - 1;
443
Jan Karaff116fc2007-05-08 00:35:14 -0700444 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 elen = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700446 epos.block = oepos.block = UDF_I_LOCATION(table);
447 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700449 while (count &&
450 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
451 if (((eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits)) == start)) {
452 if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits)) {
453 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
454 start += ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
455 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700457 elen = (etype << 30) | (elen + (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 start += count;
459 count = 0;
460 }
Jan Karaff116fc2007-05-08 00:35:14 -0700461 udf_write_aext(table, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462 } else if (eloc.logicalBlockNum == (end + 1)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700463 if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits)) {
464 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
465 end -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
466 eloc.logicalBlockNum -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
467 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700470 elen = (etype << 30) | (elen + (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 end -= count;
472 count = 0;
473 }
Jan Karaff116fc2007-05-08 00:35:14 -0700474 udf_write_aext(table, &oepos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700477 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700479 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700480 brelse(oepos.bh);
481 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700482 oepos.bh = epos.bh;
483 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700484 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700485 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700489 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700490 /*
491 * NOTE: we CANNOT use udf_add_aext here, as it can try to allocate
492 * a new block, and since we hold the super block lock already
493 * very bad things would happen :)
494 *
495 * We copy the behavior of udf_add_aext, but instead of
496 * trying to allocate a new block close to the existing one,
497 * we just steal a block from the extent we are trying to add.
498 *
499 * It would be nice if the blocks were close together, but it
500 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 int adsize;
504 short_ad *sad = NULL;
505 long_ad *lad = NULL;
506 struct allocExtDesc *aed;
507
508 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700509 elen = EXT_RECORDED_ALLOCATED |
510 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700512 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 adsize = sizeof(short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700514 } else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 adsize = sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700516 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700517 brelse(oepos.bh);
518 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto error_return;
520 }
521
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700522 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 char *sptr, *dptr;
524 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525
Jan Kara3bf25cb2007-05-08 00:35:16 -0700526 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700527 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700530 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 elen -= sb->s_blocksize;
533
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700534 if (!(epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, epos.block, 0)))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700535 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto error_return;
537 }
Jan Karaff116fc2007-05-08 00:35:14 -0700538 aed = (struct allocExtDesc *)(epos.bh->b_data);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700539 aed->previousAllocExtLocation = cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700540 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700541 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 aed->lengthAllocDescs = cpu_to_le32(adsize);
Jan Karaf5cc15d2007-08-30 23:56:22 -0700543 sptr = UDF_I_DATA(table) + epos.offset - adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700544 dptr = epos.bh->b_data + sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 memcpy(dptr, sptr, adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546 epos.offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700548 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700550 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700551 sptr = oepos.bh->b_data + epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700552 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555 } else {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700556 sptr = UDF_I_DATA(table) + epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 UDF_I_LENALLOC(table) += adsize;
558 mark_inode_dirty(table);
559 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700560 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 if (UDF_SB_UDFREV(sb) >= 0x0200)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700563 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, 3, 1,
564 epos.block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700566 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, 2, 1,
567 epos.block.logicalBlockNum, sizeof(tag));
568
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700569 switch (UDF_I_ALLOCTYPE(table)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700570 case ICBTAG_FLAG_AD_SHORT:
571 sad = (short_ad *)sptr;
572 sad->extLength = cpu_to_le32(
573 EXT_NEXT_EXTENT_ALLOCDECS |
574 sb->s_blocksize);
575 sad->extPosition = cpu_to_le32(epos.block.logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700577 case ICBTAG_FLAG_AD_LONG:
578 lad = (long_ad *)sptr;
579 lad->extLength = cpu_to_le32(
580 EXT_NEXT_EXTENT_ALLOCDECS |
581 sb->s_blocksize);
582 lad->extLocation = cpu_to_lelb(epos.block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700585 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700586 udf_update_tag(oepos.bh->b_data, loffset);
587 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700588 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700593 if (elen) { /* It's possible that stealing the block emptied the extent */
Jan Karaff116fc2007-05-08 00:35:14 -0700594 udf_write_aext(table, &epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700596 if (!epos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 UDF_I_LENALLOC(table) += adsize;
598 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700599 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700600 aed = (struct allocExtDesc *)epos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700602 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700603 udf_update_tag(epos.bh->b_data, epos.offset);
604 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 }
607 }
608
Jan Kara3bf25cb2007-05-08 00:35:16 -0700609 brelse(epos.bh);
610 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700612error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800614 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return;
616}
617
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700618static int udf_table_prealloc_blocks(struct super_block *sb,
619 struct inode *inode,
620 struct inode *table, uint16_t partition,
621 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct udf_sb_info *sbi = UDF_SB(sb);
624 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700625 uint32_t elen, adsize;
626 kernel_lb_addr eloc;
627 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int8_t etype = -1;
629
630 if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
631 return 0;
632
633 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
634 adsize = sizeof(short_ad);
635 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
636 adsize = sizeof(long_ad);
637 else
638 return 0;
639
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800640 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700641 epos.offset = sizeof(struct unallocSpaceEntry);
642 epos.block = UDF_I_LOCATION(table);
643 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 eloc.logicalBlockNum = 0xFFFFFFFF;
645
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700646 while (first_block != eloc.logicalBlockNum &&
647 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700650 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700653 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700654 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 alloc_count = (elen >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700657 if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700659 } else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 alloc_count = block_count;
661 eloc.logicalBlockNum += alloc_count;
662 elen -= (alloc_count << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700663 udf_write_aext(table, &epos, eloc, (etype << 30) | elen, 1);
664 } else {
665 udf_delete_aext(table, epos, eloc, (etype << 30) | elen);
666 }
667 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jan Kara3bf25cb2007-05-08 00:35:16 -0700671 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700673 if (alloc_count && UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700675 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition]) - alloc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
677 sb->s_dirt = 1;
678 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800679 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return alloc_count;
681}
682
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700683static int udf_table_new_block(struct super_block *sb,
684 struct inode *inode,
685 struct inode *table, uint16_t partition,
686 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct udf_sb_info *sbi = UDF_SB(sb);
689 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
690 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700691 uint32_t elen, goal_elen = 0;
WANG Cong3ad90ec2007-10-16 23:30:17 -0700692 kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700693 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int8_t etype;
695
696 *err = -ENOSPC;
697
698 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
699 adsize = sizeof(short_ad);
700 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
701 adsize = sizeof(long_ad);
702 else
703 return newblock;
704
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800705 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
707 goal = 0;
708
709 /* We search for the closest matching block to goal. If we find a exact hit,
710 we stop. Otherwise we keep going till we run out of extents.
711 We store the buffer_head, bloc, and extoffset of the current closest
712 match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700713 */
Jan Karaff116fc2007-05-08 00:35:14 -0700714 epos.offset = sizeof(struct unallocSpaceEntry);
715 epos.block = UDF_I_LOCATION(table);
716 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700718 while (spread &&
719 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720 if (goal >= eloc.logicalBlockNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700721 if (goal < eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 nspread = 0;
723 else
724 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700725 (elen >> sb->s_blocksize_bits);
726 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700730 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700732 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700733 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700734 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700735 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Jan Karaff116fc2007-05-08 00:35:14 -0700737 goal_epos.block = epos.block;
738 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goal_eloc = eloc;
740 goal_elen = (etype << 30) | elen;
741 }
742 }
743
Jan Kara3bf25cb2007-05-08 00:35:16 -0700744 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700746 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700747 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800748 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750 }
751
752 /* Only allocate blocks from the beginning of the extent.
753 That way, we only delete (empty) extents, never have to insert an
754 extent because of splitting */
755 /* This works, but very poorly.... */
756
757 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goal_elen -= sb->s_blocksize;
760
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700761 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700762 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800763 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *err = -EDQUOT;
765 return 0;
766 }
767
768 if (goal_elen)
Jan Karaff116fc2007-05-08 00:35:14 -0700769 udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 else
Jan Karaff116fc2007-05-08 00:35:14 -0700771 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700772 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700776 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition]) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
778 }
779
780 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800781 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *err = 0;
783 return newblock;
784}
785
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786inline void udf_free_blocks(struct super_block *sb,
787 struct inode *inode,
788 kernel_lb_addr bloc, uint32_t offset,
789 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 uint16_t partition = bloc.partitionReferenceNum;
792
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700793 if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return udf_bitmap_free_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700795 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
796 bloc, offset, count);
797 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return udf_table_free_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700799 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
800 bloc, offset, count);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return udf_bitmap_free_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700803 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
804 bloc, offset, count);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700805 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return udf_table_free_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
808 bloc, offset, count);
809 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700814inline int udf_prealloc_blocks(struct super_block *sb,
815 struct inode *inode,
816 uint16_t partition, uint32_t first_block,
817 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700819 if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return udf_bitmap_prealloc_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700821 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
822 partition, first_block, block_count);
823 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return udf_table_prealloc_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700825 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
826 partition, first_block, block_count);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700827 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return udf_bitmap_prealloc_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700829 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
830 partition, first_block, block_count);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700831 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return udf_table_prealloc_blocks(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700833 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
834 partition, first_block, block_count);
835 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700840inline int udf_new_block(struct super_block *sb,
841 struct inode *inode,
842 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Jan Kara3bf25cb2007-05-08 00:35:16 -0700844 int ret;
845
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700846 if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700847 ret = udf_bitmap_new_block(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700848 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
849 partition, goal, err);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700850 return ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700851 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return udf_table_new_block(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700853 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
854 partition, goal, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700855 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return udf_bitmap_new_block(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700857 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
858 partition, goal, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700859 } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return udf_table_new_block(sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700861 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
862 partition, goal, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700863 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *err = -EIO;
865 return 0;
866 }
867}