blob: dc9f8a96b6e4ebde8bbc9efeac0be15fc4a764ec [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
Marcin Slusarz4b111112008-02-08 04:20:36 -080031#define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
32#define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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)
Marcin Slusarz4b111112008-02-08 04:20:36 -080035#define udf_find_next_one_bit(addr, size, offset) \
36 find_next_one_bit(addr, size, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
Marcin Slusarz4b111112008-02-08 04:20:36 -080039#define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y)
40#define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define uintBPL_t uint(BITS_PER_LONG)
42#define uint(x) xuint(x)
43#define xuint(x) __le ## x
44
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070045static inline int find_next_one_bit(void *addr, int size, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070047 uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
48 int result = offset & ~(BITS_PER_LONG - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned long tmp;
50
51 if (offset >= size)
52 return size;
53 size -= result;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054 offset &= (BITS_PER_LONG - 1);
55 if (offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 tmp = leBPL_to_cpup(p++);
57 tmp &= ~0UL << offset;
58 if (size < BITS_PER_LONG)
59 goto found_first;
60 if (tmp)
61 goto found_middle;
62 size -= BITS_PER_LONG;
63 result += BITS_PER_LONG;
64 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 while (size & ~(BITS_PER_LONG - 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -080066 tmp = leBPL_to_cpup(p++);
67 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 goto found_middle;
69 result += BITS_PER_LONG;
70 size -= BITS_PER_LONG;
71 }
72 if (!size)
73 return result;
74 tmp = leBPL_to_cpup(p);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070075found_first:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070076 tmp &= ~0UL >> (BITS_PER_LONG - size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070077found_middle:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return result + ffz(~tmp);
79}
80
81#define find_first_one_bit(addr, size)\
82 find_next_one_bit((addr), (size), 0)
83
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084static int read_block_bitmap(struct super_block *sb,
85 struct udf_bitmap *bitmap, unsigned int block,
86 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct buffer_head *bh = NULL;
89 int retval = 0;
90 kernel_lb_addr loc;
91
92 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080093 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080096 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 bitmap->s_block_bitmap[bitmap_nr] = bh;
100 return retval;
101}
102
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700103static int __load_block_bitmap(struct super_block *sb,
104 struct udf_bitmap *bitmap,
105 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int retval = 0;
108 int nr_groups = bitmap->s_nr_groups;
109
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 if (block_group >= nr_groups) {
111 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
112 nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700115 if (bitmap->s_block_bitmap[block_group]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return block_group;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700117 } else {
118 retval = read_block_bitmap(sb, bitmap, block_group,
119 block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (retval < 0)
121 return retval;
122 return block_group;
123 }
124}
125
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700126static inline int load_block_bitmap(struct super_block *sb,
127 struct udf_bitmap *bitmap,
128 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 int slot;
131
132 slot = __load_block_bitmap(sb, bitmap, block_group);
133
134 if (slot < 0)
135 return slot;
136
137 if (!bitmap->s_block_bitmap[slot])
138 return -EIO;
139
140 return slot;
141}
142
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700143static void udf_bitmap_free_blocks(struct super_block *sb,
144 struct inode *inode,
145 struct udf_bitmap *bitmap,
146 kernel_lb_addr bloc, uint32_t offset,
147 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned long block;
152 unsigned long block_group;
153 unsigned long bit;
154 unsigned long i;
155 int bitmap_nr;
156 unsigned long overflow;
157
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800158 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (bloc.logicalBlockNum < 0 ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800160 (bloc.logicalBlockNum + count) >
161 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700162 udf_debug("%d < %d || %d + %d > %d\n",
163 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800164 sbi->s_partmaps[bloc.partitionReferenceNum].
165 s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto error_return;
167 }
168
Marcin Slusarz4b111112008-02-08 04:20:36 -0800169 block = bloc.logicalBlockNum + offset +
170 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700172do_more:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 overflow = 0;
174 block_group = block >> (sb->s_blocksize_bits + 3);
175 bit = block % (sb->s_blocksize << 3);
176
177 /*
178 * Check to see if we are freeing blocks across a group boundary.
179 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700180 if (bit + count > (sb->s_blocksize << 3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 overflow = bit + count - (sb->s_blocksize << 3);
182 count -= overflow;
183 }
184 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
185 if (bitmap_nr < 0)
186 goto error_return;
187
188 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189 for (i = 0; i < count; i++) {
190 if (udf_set_bit(bit + i, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 udf_debug("bit %ld already set\n", bit + i);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800192 udf_debug("byte=%2x\n",
193 ((char *)bh->b_data)[(bit + i) >> 3]);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700194 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (inode)
196 DQUOT_FREE_BLOCK(inode, 1);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800197 if (sbi->s_lvid_bh) {
198 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
199 lvid->freeSpaceTable[sbi->s_partition] =
200 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 }
203 }
204 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 if (overflow) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 block += count;
207 count = overflow;
208 goto do_more;
209 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700210error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sb->s_dirt = 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800212 if (sbi->s_lvid_bh)
213 mark_buffer_dirty(sbi->s_lvid_bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800214 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return;
216}
217
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218static int udf_bitmap_prealloc_blocks(struct super_block *sb,
219 struct inode *inode,
220 struct udf_bitmap *bitmap,
221 uint16_t partition, uint32_t first_block,
222 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct udf_sb_info *sbi = UDF_SB(sb);
225 int alloc_count = 0;
226 int bit, block, block_group, group_start;
227 int nr_groups, bitmap_nr;
228 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800229 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800231 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800232 part_len = sbi->s_partmaps[partition].s_partition_len;
233 if (first_block < 0 || first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out;
235
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800236 if (first_block + block_count > part_len)
237 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239repeat:
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800240 nr_groups = udf_compute_nr_groups(sb, partition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
242 block_group = block >> (sb->s_blocksize_bits + 3);
243 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
244
245 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
246 if (bitmap_nr < 0)
247 goto out;
248 bh = bitmap->s_block_bitmap[bitmap_nr];
249
250 bit = block % (sb->s_blocksize << 3);
251
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700252 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700253 if (!udf_test_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700255 } else if (DQUOT_PREALLOC_BLOCK(inode, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257 } else if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 udf_debug("bit already cleared for block %d\n", bit);
259 DQUOT_FREE_BLOCK(inode, 1);
260 goto out;
261 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700262 block_count--;
263 alloc_count++;
264 bit++;
265 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 mark_buffer_dirty(bh);
268 if (block_count > 0)
269 goto repeat;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700270out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800271 if (sbi->s_lvid_bh) {
272 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
273 lvid->freeSpaceTable[partition] =
274 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - alloc_count);
275 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800278 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return alloc_count;
280}
281
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700282static int udf_bitmap_new_block(struct super_block *sb,
283 struct inode *inode,
284 struct udf_bitmap *bitmap, uint16_t partition,
285 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int end_goal, nr_groups, bitmap_nr, i;
290 struct buffer_head *bh = NULL;
291 char *ptr;
292 int newblock = 0;
293
294 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800295 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700297repeat:
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800298 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goal = 0;
300
301 nr_groups = bitmap->s_nr_groups;
302 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
303 block_group = block >> (sb->s_blocksize_bits + 3);
304 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
305
306 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
307 if (bitmap_nr < 0)
308 goto error_return;
309 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700310 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
311 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700313 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700315 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 end_goal = (bit + 63) & ~63;
319 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
320 if (bit < end_goal)
321 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700322
Marcin Slusarz4b111112008-02-08 04:20:36 -0800323 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
324 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700326 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 bit = newbit;
328 goto search_back;
329 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700330
Marcin Slusarz4b111112008-02-08 04:20:36 -0800331 newbit = udf_find_next_one_bit(bh->b_data,
332 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700333 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 bit = newbit;
335 goto got_block;
336 }
337 }
338
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 for (i = 0; i < (nr_groups * 2); i++) {
340 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (block_group >= nr_groups)
342 block_group = 0;
343 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
344
345 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
346 if (bitmap_nr < 0)
347 goto error_return;
348 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700349 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700350 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
351 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 bit = (ptr - ((char *)bh->b_data)) << 3;
354 break;
355 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700356 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700357 bit = udf_find_next_one_bit((char *)bh->b_data,
358 sb->s_blocksize << 3,
359 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (bit < sb->s_blocksize << 3)
361 break;
362 }
363 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700364 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800365 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return newblock;
367 }
368 if (bit < sb->s_blocksize << 3)
369 goto search_back;
370 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800371 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
372 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700373 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800374 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376 }
377
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700378search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800379 i = 0;
380 while (i < 7 && bit > (group_start << 3) &&
381 udf_test_bit(bit - 1, bh->b_data)) {
382 ++i;
383 --bit;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700386got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /*
389 * Check quota for allocation of this block.
390 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700391 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800392 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *err = -EDQUOT;
394 return 0;
395 }
396
397 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700398 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700400 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 udf_debug("bit already cleared for block %d\n", bit);
402 goto repeat;
403 }
404
405 mark_buffer_dirty(bh);
406
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800407 if (sbi->s_lvid_bh) {
408 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
409 lvid->freeSpaceTable[partition] =
410 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - 1);
411 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800414 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *err = 0;
416 return newblock;
417
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700418error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800420 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424static void udf_table_free_blocks(struct super_block *sb,
425 struct inode *inode,
426 struct inode *table,
427 kernel_lb_addr bloc, uint32_t offset,
428 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct udf_sb_info *sbi = UDF_SB(sb);
431 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700432 uint32_t elen;
433 kernel_lb_addr eloc;
434 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int8_t etype;
436 int i;
437
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800438 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (bloc.logicalBlockNum < 0 ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800440 (bloc.logicalBlockNum + count) >
441 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700442 udf_debug("%d < %d || %d + %d > %d\n",
443 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800444 sbi->s_partmaps[bloc.partitionReferenceNum].
445 s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto error_return;
447 }
448
Marcin Slusarz4b111112008-02-08 04:20:36 -0800449 /* We do this up front - There are some error conditions that
450 could occure, but.. oh well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (inode)
452 DQUOT_FREE_BLOCK(inode, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800453 if (sbi->s_lvid_bh) {
454 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
455 lvid->freeSpaceTable[sbi->s_partition] =
456 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]) + count);
457 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 start = bloc.logicalBlockNum + offset;
461 end = bloc.logicalBlockNum + offset + count - 1;
462
Jan Karaff116fc2007-05-08 00:35:14 -0700463 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 elen = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700465 epos.block = oepos.block = UDF_I_LOCATION(table);
466 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700468 while (count &&
469 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800470 if (((eloc.logicalBlockNum +
471 (elen >> sb->s_blocksize_bits)) == start)) {
472 if ((0x3FFFFFFF - elen) <
473 (count << sb->s_blocksize_bits)) {
474 uint32_t tmp = ((0x3FFFFFFF - elen) >>
475 sb->s_blocksize_bits);
476 count -= tmp;
477 start += tmp;
478 elen = (etype << 30) |
479 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700480 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800481 elen = (etype << 30) |
482 (elen +
483 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 start += count;
485 count = 0;
486 }
Jan Karaff116fc2007-05-08 00:35:14 -0700487 udf_write_aext(table, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700488 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800489 if ((0x3FFFFFFF - elen) <
490 (count << sb->s_blocksize_bits)) {
491 uint32_t tmp = ((0x3FFFFFFF - elen) >>
492 sb->s_blocksize_bits);
493 count -= tmp;
494 end -= tmp;
495 eloc.logicalBlockNum -= tmp;
496 elen = (etype << 30) |
497 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700498 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800500 elen = (etype << 30) |
501 (elen +
502 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 end -= count;
504 count = 0;
505 }
Jan Karaff116fc2007-05-08 00:35:14 -0700506 udf_write_aext(table, &oepos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700509 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700511 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700512 brelse(oepos.bh);
513 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700514 oepos.bh = epos.bh;
515 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700516 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700517 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700522 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800523 * NOTE: we CANNOT use udf_add_aext here, as it can try to
524 * allocate a new block, and since we hold the super block
525 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700526 *
527 * We copy the behavior of udf_add_aext, but instead of
528 * trying to allocate a new block close to the existing one,
529 * we just steal a block from the extent we are trying to add.
530 *
531 * It would be nice if the blocks were close together, but it
532 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 int adsize;
536 short_ad *sad = NULL;
537 long_ad *lad = NULL;
538 struct allocExtDesc *aed;
539
540 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700541 elen = EXT_RECORDED_ALLOCATED |
542 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700544 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 adsize = sizeof(short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546 } else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 adsize = sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700548 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700549 brelse(oepos.bh);
550 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto error_return;
552 }
553
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700554 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 char *sptr, *dptr;
556 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700557
Jan Kara3bf25cb2007-05-08 00:35:16 -0700558 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700559 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700562 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 elen -= sb->s_blocksize;
565
Marcin Slusarz4b111112008-02-08 04:20:36 -0800566 epos.bh = udf_tread(sb,
567 udf_get_lb_pblock(sb, epos.block, 0));
568 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700569 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto error_return;
571 }
Jan Karaff116fc2007-05-08 00:35:14 -0700572 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800573 aed->previousAllocExtLocation =
574 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700576 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 aed->lengthAllocDescs = cpu_to_le32(adsize);
Jan Karaf5cc15d2007-08-30 23:56:22 -0700578 sptr = UDF_I_DATA(table) + epos.offset - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800579 dptr = epos.bh->b_data +
580 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800582 epos.offset = sizeof(struct allocExtDesc) +
583 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700584 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700585 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700588 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800589 aed = (struct allocExtDesc *)
590 oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800592 cpu_to_le32(le32_to_cpu(
593 aed->lengthAllocDescs) +
594 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700595 } else {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700596 sptr = UDF_I_DATA(table) + epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 UDF_I_LENALLOC(table) += adsize;
598 mark_inode_dirty(table);
599 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700600 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800602 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800603 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
604 3, 1, epos.block.logicalBlockNum,
605 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800607 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
608 2, 1, epos.block.logicalBlockNum,
609 sizeof(tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700610
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 switch (UDF_I_ALLOCTYPE(table)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800612 case ICBTAG_FLAG_AD_SHORT:
613 sad = (short_ad *)sptr;
614 sad->extLength = cpu_to_le32(
615 EXT_NEXT_EXTENT_ALLOCDECS |
616 sb->s_blocksize);
617 sad->extPosition =
618 cpu_to_le32(epos.block.logicalBlockNum);
619 break;
620 case ICBTAG_FLAG_AD_LONG:
621 lad = (long_ad *)sptr;
622 lad->extLength = cpu_to_le32(
623 EXT_NEXT_EXTENT_ALLOCDECS |
624 sb->s_blocksize);
625 lad->extLocation =
626 cpu_to_lelb(epos.block);
627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700629 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700630 udf_update_tag(oepos.bh->b_data, loffset);
631 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Marcin Slusarz4b111112008-02-08 04:20:36 -0800637 /* It's possible that stealing the block emptied the extent */
638 if (elen) {
Jan Karaff116fc2007-05-08 00:35:14 -0700639 udf_write_aext(table, &epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 if (!epos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 UDF_I_LENALLOC(table) += adsize;
643 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700645 aed = (struct allocExtDesc *)epos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800647 cpu_to_le32(le32_to_cpu(
648 aed->lengthAllocDescs) + adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700649 udf_update_tag(epos.bh->b_data, epos.offset);
650 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
653 }
654
Jan Kara3bf25cb2007-05-08 00:35:16 -0700655 brelse(epos.bh);
656 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700658error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800660 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
662}
663
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700664static int udf_table_prealloc_blocks(struct super_block *sb,
665 struct inode *inode,
666 struct inode *table, uint16_t partition,
667 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct udf_sb_info *sbi = UDF_SB(sb);
670 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700671 uint32_t elen, adsize;
672 kernel_lb_addr eloc;
673 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int8_t etype = -1;
675
Marcin Slusarz4b111112008-02-08 04:20:36 -0800676 if (first_block < 0 ||
677 first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679
680 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
681 adsize = sizeof(short_ad);
682 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
683 adsize = sizeof(long_ad);
684 else
685 return 0;
686
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800687 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700688 epos.offset = sizeof(struct unallocSpaceEntry);
689 epos.block = UDF_I_LOCATION(table);
690 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 eloc.logicalBlockNum = 0xFFFFFFFF;
692
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700693 while (first_block != eloc.logicalBlockNum &&
694 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700696 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700697 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700700 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700701 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 alloc_count = (elen >> sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800704 if (inode && DQUOT_PREALLOC_BLOCK(inode,
705 alloc_count > block_count ? block_count : alloc_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 alloc_count = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800707 else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 alloc_count = block_count;
709 eloc.logicalBlockNum += alloc_count;
710 elen -= (alloc_count << sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800711 udf_write_aext(table, &epos, eloc,
712 (etype << 30) | elen, 1);
713 } else
714 udf_delete_aext(table, epos, eloc,
715 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700716 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jan Kara3bf25cb2007-05-08 00:35:16 -0700720 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800722 if (alloc_count && sbi->s_lvid_bh) {
723 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
724 lvid->freeSpaceTable[partition] =
725 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - alloc_count);
726 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 sb->s_dirt = 1;
728 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800729 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return alloc_count;
731}
732
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700733static int udf_table_new_block(struct super_block *sb,
734 struct inode *inode,
735 struct inode *table, uint16_t partition,
736 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct udf_sb_info *sbi = UDF_SB(sb);
739 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
740 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700741 uint32_t elen, goal_elen = 0;
WANG Cong3ad90ec2007-10-16 23:30:17 -0700742 kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700743 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int8_t etype;
745
746 *err = -ENOSPC;
747
748 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
749 adsize = sizeof(short_ad);
750 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
751 adsize = sizeof(long_ad);
752 else
753 return newblock;
754
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800755 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800756 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goal = 0;
758
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 /* We search for the closest matching block to goal. If we find
760 a exact hit, we stop. Otherwise we keep going till we run out
761 of extents. We store the buffer_head, bloc, and extoffset
762 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 */
Jan Karaff116fc2007-05-08 00:35:14 -0700764 epos.offset = sizeof(struct unallocSpaceEntry);
765 epos.block = UDF_I_LOCATION(table);
766 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700768 while (spread &&
769 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700770 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800771 if (goal < eloc.logicalBlockNum +
772 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 nspread = 0;
774 else
775 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700776 (elen >> sb->s_blocksize_bits);
777 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700781 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700784 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700785 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700786 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Jan Karaff116fc2007-05-08 00:35:14 -0700788 goal_epos.block = epos.block;
789 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goal_eloc = eloc;
791 goal_elen = (etype << 30) | elen;
792 }
793 }
794
Jan Kara3bf25cb2007-05-08 00:35:16 -0700795 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700797 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700798 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800799 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801 }
802
803 /* Only allocate blocks from the beginning of the extent.
804 That way, we only delete (empty) extents, never have to insert an
805 extent because of splitting */
806 /* This works, but very poorly.... */
807
808 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700809 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goal_elen -= sb->s_blocksize;
811
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700812 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700813 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800814 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 *err = -EDQUOT;
816 return 0;
817 }
818
819 if (goal_elen)
Jan Karaff116fc2007-05-08 00:35:14 -0700820 udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 else
Jan Karaff116fc2007-05-08 00:35:14 -0700822 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700823 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800825 if (sbi->s_lvid_bh) {
826 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
827 lvid->freeSpaceTable[partition] =
828 cpu_to_le32(le32_to_cpu(lvid->freeSpaceTable[partition]) - 1);
829 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800833 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *err = 0;
835 return newblock;
836}
837
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700838inline void udf_free_blocks(struct super_block *sb,
839 struct inode *inode,
840 kernel_lb_addr bloc, uint32_t offset,
841 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 uint16_t partition = bloc.partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800844 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800846 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return udf_bitmap_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800848 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700849 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800850 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return udf_table_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800852 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700853 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800854 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return udf_bitmap_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800856 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700857 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800858 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return udf_table_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800860 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700861 bloc, offset, count);
862 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700867inline int udf_prealloc_blocks(struct super_block *sb,
868 struct inode *inode,
869 uint16_t partition, uint32_t first_block,
870 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800872 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
873
Marcin Slusarz4b111112008-02-08 04:20:36 -0800874 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800876 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800877 partition, first_block,
878 block_count);
879 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800881 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800882 partition, first_block,
883 block_count);
884 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800886 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800887 partition, first_block,
888 block_count);
889 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800891 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800892 partition, first_block,
893 block_count);
894 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896}
897
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700898inline int udf_new_block(struct super_block *sb,
899 struct inode *inode,
900 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800902 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700903
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
905 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800906 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700907 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800908 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800910 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700911 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800912 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800914 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700915 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800916 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800918 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700919 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800920 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *err = -EIO;
922 return 0;
923 }
924}