blob: 989259655b401066f0a1635660de8064523e6a12 [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
Marcin Slusarz742ba022008-02-08 04:20:40 -0800143static bool udf_add_free_space(struct udf_sb_info *sbi,
144 u16 partition, u32 cnt)
145{
146 struct logicalVolIntegrityDesc *lvid;
147
148 if (sbi->s_lvid_bh)
149 return false;
150
151 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
152 lvid->freeSpaceTable[partition] = cpu_to_le32(le32_to_cpu(
153 lvid->freeSpaceTable[partition]) + cnt);
154 return true;
155}
156
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700157static void udf_bitmap_free_blocks(struct super_block *sb,
158 struct inode *inode,
159 struct udf_bitmap *bitmap,
160 kernel_lb_addr bloc, uint32_t offset,
161 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 unsigned long block;
166 unsigned long block_group;
167 unsigned long bit;
168 unsigned long i;
169 int bitmap_nr;
170 unsigned long overflow;
171
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800172 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (bloc.logicalBlockNum < 0 ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800174 (bloc.logicalBlockNum + count) >
175 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700176 udf_debug("%d < %d || %d + %d > %d\n",
177 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800178 sbi->s_partmaps[bloc.partitionReferenceNum].
179 s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto error_return;
181 }
182
Marcin Slusarz4b111112008-02-08 04:20:36 -0800183 block = bloc.logicalBlockNum + offset +
184 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800186 do {
187 overflow = 0;
188 block_group = block >> (sb->s_blocksize_bits + 3);
189 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800191 /*
192 * Check to see if we are freeing blocks across a group boundary.
193 */
194 if (bit + count > (sb->s_blocksize << 3)) {
195 overflow = bit + count - (sb->s_blocksize << 3);
196 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800198 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
199 if (bitmap_nr < 0)
200 goto error_return;
201
202 bh = bitmap->s_block_bitmap[bitmap_nr];
203 for (i = 0; i < count; i++) {
204 if (udf_set_bit(bit + i, bh->b_data)) {
205 udf_debug("bit %ld already set\n", bit + i);
206 udf_debug("byte=%2x\n",
207 ((char *)bh->b_data)[(bit + i) >> 3]);
208 } else {
209 if (inode)
210 DQUOT_FREE_BLOCK(inode, 1);
211 udf_add_free_space(sbi, sbi->s_partition, 1);
212 }
213 }
214 mark_buffer_dirty(bh);
215 if (overflow) {
216 block += count;
217 count = overflow;
218 }
219 } while (overflow);
220
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 sb->s_dirt = 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800223 if (sbi->s_lvid_bh)
224 mark_buffer_dirty(sbi->s_lvid_bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800225 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700228static int udf_bitmap_prealloc_blocks(struct super_block *sb,
229 struct inode *inode,
230 struct udf_bitmap *bitmap,
231 uint16_t partition, uint32_t first_block,
232 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct udf_sb_info *sbi = UDF_SB(sb);
235 int alloc_count = 0;
236 int bit, block, block_group, group_start;
237 int nr_groups, bitmap_nr;
238 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800239 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800241 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800242 part_len = sbi->s_partmaps[partition].s_partition_len;
243 if (first_block < 0 || first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto out;
245
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800246 if (first_block + block_count > part_len)
247 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800249 do {
250 nr_groups = udf_compute_nr_groups(sb, partition);
251 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
252 block_group = block >> (sb->s_blocksize_bits + 3);
253 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800255 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
256 if (bitmap_nr < 0)
257 goto out;
258 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800260 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800262 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
263 if (!udf_test_bit(bit, bh->b_data))
264 goto out;
265 else if (DQUOT_PREALLOC_BLOCK(inode, 1))
266 goto out;
267 else if (!udf_clear_bit(bit, bh->b_data)) {
268 udf_debug("bit already cleared for block %d\n", bit);
269 DQUOT_FREE_BLOCK(inode, 1);
270 goto out;
271 }
272 block_count--;
273 alloc_count++;
274 bit++;
275 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800277 mark_buffer_dirty(bh);
278 } while (block_count > 0);
279
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700280out:
Marcin Slusarz742ba022008-02-08 04:20:40 -0800281 if (udf_add_free_space(sbi, partition, -alloc_count))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800282 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800284 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return alloc_count;
286}
287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288static int udf_bitmap_new_block(struct super_block *sb,
289 struct inode *inode,
290 struct udf_bitmap *bitmap, uint16_t partition,
291 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int end_goal, nr_groups, bitmap_nr, i;
296 struct buffer_head *bh = NULL;
297 char *ptr;
298 int newblock = 0;
299
300 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800301 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700303repeat:
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800304 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goal = 0;
306
307 nr_groups = bitmap->s_nr_groups;
308 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
309 block_group = block >> (sb->s_blocksize_bits + 3);
310 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
311
312 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
313 if (bitmap_nr < 0)
314 goto error_return;
315 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700316 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
317 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700319 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700321 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 end_goal = (bit + 63) & ~63;
325 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
326 if (bit < end_goal)
327 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328
Marcin Slusarz4b111112008-02-08 04:20:36 -0800329 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
330 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 bit = newbit;
334 goto search_back;
335 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700336
Marcin Slusarz4b111112008-02-08 04:20:36 -0800337 newbit = udf_find_next_one_bit(bh->b_data,
338 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 bit = newbit;
341 goto got_block;
342 }
343 }
344
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700345 for (i = 0; i < (nr_groups * 2); i++) {
346 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (block_group >= nr_groups)
348 block_group = 0;
349 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
350
351 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
352 if (bitmap_nr < 0)
353 goto error_return;
354 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700355 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700356 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
357 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700358 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 bit = (ptr - ((char *)bh->b_data)) << 3;
360 break;
361 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700362 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700363 bit = udf_find_next_one_bit((char *)bh->b_data,
364 sb->s_blocksize << 3,
365 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (bit < sb->s_blocksize << 3)
367 break;
368 }
369 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700370 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800371 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return newblock;
373 }
374 if (bit < sb->s_blocksize << 3)
375 goto search_back;
376 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800377 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
378 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700379 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800380 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
382 }
383
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700384search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800385 i = 0;
386 while (i < 7 && bit > (group_start << 3) &&
387 udf_test_bit(bit - 1, bh->b_data)) {
388 ++i;
389 --bit;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
395 * Check quota for allocation of this block.
396 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700397 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800398 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *err = -EDQUOT;
400 return 0;
401 }
402
403 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700404 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700406 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 udf_debug("bit already cleared for block %d\n", bit);
408 goto repeat;
409 }
410
411 mark_buffer_dirty(bh);
412
Marcin Slusarz742ba022008-02-08 04:20:40 -0800413 if (udf_add_free_space(sbi, partition, -1))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800414 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800416 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *err = 0;
418 return newblock;
419
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700420error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800422 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700426static void udf_table_free_blocks(struct super_block *sb,
427 struct inode *inode,
428 struct inode *table,
429 kernel_lb_addr bloc, uint32_t offset,
430 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct udf_sb_info *sbi = UDF_SB(sb);
433 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700434 uint32_t elen;
435 kernel_lb_addr eloc;
436 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int8_t etype;
438 int i;
439
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800440 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (bloc.logicalBlockNum < 0 ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800442 (bloc.logicalBlockNum + count) >
443 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700444 udf_debug("%d < %d || %d + %d > %d\n",
445 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800446 sbi->s_partmaps[bloc.partitionReferenceNum].
447 s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto error_return;
449 }
450
Marcin Slusarz4b111112008-02-08 04:20:36 -0800451 /* We do this up front - There are some error conditions that
452 could occure, but.. oh well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (inode)
454 DQUOT_FREE_BLOCK(inode, count);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800455 if (udf_add_free_space(sbi, sbi->s_partition, count))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800456 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 start = bloc.logicalBlockNum + offset;
459 end = bloc.logicalBlockNum + offset + count - 1;
460
Jan Karaff116fc2007-05-08 00:35:14 -0700461 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 elen = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700463 epos.block = oepos.block = UDF_I_LOCATION(table);
464 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700466 while (count &&
467 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800468 if (((eloc.logicalBlockNum +
469 (elen >> sb->s_blocksize_bits)) == start)) {
470 if ((0x3FFFFFFF - elen) <
471 (count << sb->s_blocksize_bits)) {
472 uint32_t tmp = ((0x3FFFFFFF - elen) >>
473 sb->s_blocksize_bits);
474 count -= tmp;
475 start += tmp;
476 elen = (etype << 30) |
477 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700478 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800479 elen = (etype << 30) |
480 (elen +
481 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 start += count;
483 count = 0;
484 }
Jan Karaff116fc2007-05-08 00:35:14 -0700485 udf_write_aext(table, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700486 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800487 if ((0x3FFFFFFF - elen) <
488 (count << sb->s_blocksize_bits)) {
489 uint32_t tmp = ((0x3FFFFFFF - elen) >>
490 sb->s_blocksize_bits);
491 count -= tmp;
492 end -= tmp;
493 eloc.logicalBlockNum -= tmp;
494 elen = (etype << 30) |
495 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700496 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800498 elen = (etype << 30) |
499 (elen +
500 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 end -= count;
502 count = 0;
503 }
Jan Karaff116fc2007-05-08 00:35:14 -0700504 udf_write_aext(table, &oepos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700507 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700509 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700510 brelse(oepos.bh);
511 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700512 oepos.bh = epos.bh;
513 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700514 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700515 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700519 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700520 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800521 * NOTE: we CANNOT use udf_add_aext here, as it can try to
522 * allocate a new block, and since we hold the super block
523 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700524 *
525 * We copy the behavior of udf_add_aext, but instead of
526 * trying to allocate a new block close to the existing one,
527 * we just steal a block from the extent we are trying to add.
528 *
529 * It would be nice if the blocks were close together, but it
530 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 int adsize;
534 short_ad *sad = NULL;
535 long_ad *lad = NULL;
536 struct allocExtDesc *aed;
537
538 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700539 elen = EXT_RECORDED_ALLOCATED |
540 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700542 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 adsize = sizeof(short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700544 } else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 adsize = sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700547 brelse(oepos.bh);
548 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto error_return;
550 }
551
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700552 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 char *sptr, *dptr;
554 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555
Jan Kara3bf25cb2007-05-08 00:35:16 -0700556 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700557 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700560 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700561 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 elen -= sb->s_blocksize;
563
Marcin Slusarz4b111112008-02-08 04:20:36 -0800564 epos.bh = udf_tread(sb,
565 udf_get_lb_pblock(sb, epos.block, 0));
566 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700567 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto error_return;
569 }
Jan Karaff116fc2007-05-08 00:35:14 -0700570 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800571 aed->previousAllocExtLocation =
572 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700573 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700574 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 aed->lengthAllocDescs = cpu_to_le32(adsize);
Jan Karaf5cc15d2007-08-30 23:56:22 -0700576 sptr = UDF_I_DATA(table) + epos.offset - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800577 dptr = epos.bh->b_data +
578 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800580 epos.offset = sizeof(struct allocExtDesc) +
581 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700582 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700583 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700585 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700586 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800587 aed = (struct allocExtDesc *)
588 oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800590 cpu_to_le32(le32_to_cpu(
591 aed->lengthAllocDescs) +
592 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 } else {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700594 sptr = UDF_I_DATA(table) + epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 UDF_I_LENALLOC(table) += adsize;
596 mark_inode_dirty(table);
597 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700598 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800600 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800601 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
602 3, 1, epos.block.logicalBlockNum,
603 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800605 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
606 2, 1, epos.block.logicalBlockNum,
607 sizeof(tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700608
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700609 switch (UDF_I_ALLOCTYPE(table)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800610 case ICBTAG_FLAG_AD_SHORT:
611 sad = (short_ad *)sptr;
612 sad->extLength = cpu_to_le32(
613 EXT_NEXT_EXTENT_ALLOCDECS |
614 sb->s_blocksize);
615 sad->extPosition =
616 cpu_to_le32(epos.block.logicalBlockNum);
617 break;
618 case ICBTAG_FLAG_AD_LONG:
619 lad = (long_ad *)sptr;
620 lad->extLength = cpu_to_le32(
621 EXT_NEXT_EXTENT_ALLOCDECS |
622 sb->s_blocksize);
623 lad->extLocation =
624 cpu_to_lelb(epos.block);
625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700627 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700628 udf_update_tag(oepos.bh->b_data, loffset);
629 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700630 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
Marcin Slusarz4b111112008-02-08 04:20:36 -0800635 /* It's possible that stealing the block emptied the extent */
636 if (elen) {
Jan Karaff116fc2007-05-08 00:35:14 -0700637 udf_write_aext(table, &epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700639 if (!epos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 UDF_I_LENALLOC(table) += adsize;
641 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700642 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700643 aed = (struct allocExtDesc *)epos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800645 cpu_to_le32(le32_to_cpu(
646 aed->lengthAllocDescs) + adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700647 udf_update_tag(epos.bh->b_data, epos.offset);
648 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 }
651 }
652
Jan Kara3bf25cb2007-05-08 00:35:16 -0700653 brelse(epos.bh);
654 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700656error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800658 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660}
661
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700662static int udf_table_prealloc_blocks(struct super_block *sb,
663 struct inode *inode,
664 struct inode *table, uint16_t partition,
665 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct udf_sb_info *sbi = UDF_SB(sb);
668 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700669 uint32_t elen, adsize;
670 kernel_lb_addr eloc;
671 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int8_t etype = -1;
673
Marcin Slusarz4b111112008-02-08 04:20:36 -0800674 if (first_block < 0 ||
675 first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677
678 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
679 adsize = sizeof(short_ad);
680 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
681 adsize = sizeof(long_ad);
682 else
683 return 0;
684
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800685 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700686 epos.offset = sizeof(struct unallocSpaceEntry);
687 epos.block = UDF_I_LOCATION(table);
688 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 eloc.logicalBlockNum = 0xFFFFFFFF;
690
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700691 while (first_block != eloc.logicalBlockNum &&
692 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700694 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700695 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700698 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700699 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 alloc_count = (elen >> sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800702 if (inode && DQUOT_PREALLOC_BLOCK(inode,
703 alloc_count > block_count ? block_count : alloc_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 alloc_count = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800705 else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 alloc_count = block_count;
707 eloc.logicalBlockNum += alloc_count;
708 elen -= (alloc_count << sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800709 udf_write_aext(table, &epos, eloc,
710 (etype << 30) | elen, 1);
711 } else
712 udf_delete_aext(table, epos, eloc,
713 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700714 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jan Kara3bf25cb2007-05-08 00:35:16 -0700718 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Marcin Slusarz742ba022008-02-08 04:20:40 -0800720 if (alloc_count && udf_add_free_space(sbi, partition, -alloc_count)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800721 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sb->s_dirt = 1;
723 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800724 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return alloc_count;
726}
727
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700728static int udf_table_new_block(struct super_block *sb,
729 struct inode *inode,
730 struct inode *table, uint16_t partition,
731 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct udf_sb_info *sbi = UDF_SB(sb);
734 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
735 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700736 uint32_t elen, goal_elen = 0;
WANG Cong3ad90ec2007-10-16 23:30:17 -0700737 kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700738 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int8_t etype;
740
741 *err = -ENOSPC;
742
743 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
744 adsize = sizeof(short_ad);
745 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
746 adsize = sizeof(long_ad);
747 else
748 return newblock;
749
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800750 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800751 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goal = 0;
753
Marcin Slusarz4b111112008-02-08 04:20:36 -0800754 /* We search for the closest matching block to goal. If we find
755 a exact hit, we stop. Otherwise we keep going till we run out
756 of extents. We store the buffer_head, bloc, and extoffset
757 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 */
Jan Karaff116fc2007-05-08 00:35:14 -0700759 epos.offset = sizeof(struct unallocSpaceEntry);
760 epos.block = UDF_I_LOCATION(table);
761 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700763 while (spread &&
764 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700765 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800766 if (goal < eloc.logicalBlockNum +
767 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 nspread = 0;
769 else
770 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700771 (elen >> sb->s_blocksize_bits);
772 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700776 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700779 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700780 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700781 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Jan Karaff116fc2007-05-08 00:35:14 -0700783 goal_epos.block = epos.block;
784 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goal_eloc = eloc;
786 goal_elen = (etype << 30) | elen;
787 }
788 }
789
Jan Kara3bf25cb2007-05-08 00:35:16 -0700790 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700793 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800794 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796 }
797
798 /* Only allocate blocks from the beginning of the extent.
799 That way, we only delete (empty) extents, never have to insert an
800 extent because of splitting */
801 /* This works, but very poorly.... */
802
803 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700804 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goal_elen -= sb->s_blocksize;
806
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700807 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700808 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800809 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *err = -EDQUOT;
811 return 0;
812 }
813
814 if (goal_elen)
Jan Karaff116fc2007-05-08 00:35:14 -0700815 udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 else
Jan Karaff116fc2007-05-08 00:35:14 -0700817 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700818 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Marcin Slusarz742ba022008-02-08 04:20:40 -0800820 if (udf_add_free_space(sbi, partition, -1))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800821 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800824 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *err = 0;
826 return newblock;
827}
828
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700829inline void udf_free_blocks(struct super_block *sb,
830 struct inode *inode,
831 kernel_lb_addr bloc, uint32_t offset,
832 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 uint16_t partition = bloc.partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800835 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800837 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return udf_bitmap_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800839 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700840 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800841 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return udf_table_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800843 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700844 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800845 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return udf_bitmap_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800847 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700848 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800849 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return udf_table_free_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800851 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700852 bloc, offset, count);
853 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858inline int udf_prealloc_blocks(struct super_block *sb,
859 struct inode *inode,
860 uint16_t partition, uint32_t first_block,
861 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800863 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
864
Marcin Slusarz4b111112008-02-08 04:20:36 -0800865 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800867 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800868 partition, first_block,
869 block_count);
870 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800872 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800873 partition, first_block,
874 block_count);
875 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800877 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800878 partition, first_block,
879 block_count);
880 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800882 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800883 partition, first_block,
884 block_count);
885 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 0;
887}
888
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700889inline int udf_new_block(struct super_block *sb,
890 struct inode *inode,
891 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800893 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700894
Marcin Slusarz4b111112008-02-08 04:20:36 -0800895 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
896 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800897 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700898 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800899 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800901 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700902 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800903 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800905 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700906 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800907 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800909 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700910 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800911 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 *err = -EIO;
913 return 0;
914 }
915}