udf: create common function for tag checksumming
[linux-block.git] / fs / udf / balloc.c
CommitLineData
1da177e4
LT
1/*
2 * balloc.c
3 *
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
6 *
1da177e4
LT
7 * 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
4b11111a
MS
31#define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
32#define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
1da177e4
LT
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)
4b11111a
MS
35#define udf_find_next_one_bit(addr, size, offset) \
36 find_next_one_bit(addr, size, offset)
1da177e4
LT
37
38#define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
4b11111a
MS
39#define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y)
40#define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y))
1da177e4
LT
41#define uintBPL_t uint(BITS_PER_LONG)
42#define uint(x) xuint(x)
43#define xuint(x) __le ## x
44
cb00ea35 45static inline int find_next_one_bit(void *addr, int size, int offset)
1da177e4 46{
cb00ea35
CG
47 uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
48 int result = offset & ~(BITS_PER_LONG - 1);
1da177e4
LT
49 unsigned long tmp;
50
51 if (offset >= size)
52 return size;
53 size -= result;
cb00ea35
CG
54 offset &= (BITS_PER_LONG - 1);
55 if (offset) {
1da177e4
LT
56 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 }
cb00ea35 65 while (size & ~(BITS_PER_LONG - 1)) {
4b11111a
MS
66 tmp = leBPL_to_cpup(p++);
67 if (tmp)
1da177e4
LT
68 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);
28de7948 75found_first:
cb00ea35 76 tmp &= ~0UL >> (BITS_PER_LONG - size);
28de7948 77found_middle:
1da177e4
LT
78 return result + ffz(~tmp);
79}
80
81#define find_first_one_bit(addr, size)\
82 find_next_one_bit((addr), (size), 0)
83
cb00ea35
CG
84static int read_block_bitmap(struct super_block *sb,
85 struct udf_bitmap *bitmap, unsigned int block,
86 unsigned long bitmap_nr)
1da177e4
LT
87{
88 struct buffer_head *bh = NULL;
89 int retval = 0;
90 kernel_lb_addr loc;
91
92 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 93 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1da177e4
LT
94
95 bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block));
4b11111a 96 if (!bh)
1da177e4 97 retval = -EIO;
4b11111a 98
1da177e4
LT
99 bitmap->s_block_bitmap[bitmap_nr] = bh;
100 return retval;
101}
102
cb00ea35
CG
103static int __load_block_bitmap(struct super_block *sb,
104 struct udf_bitmap *bitmap,
105 unsigned int block_group)
1da177e4
LT
106{
107 int retval = 0;
108 int nr_groups = bitmap->s_nr_groups;
109
cb00ea35
CG
110 if (block_group >= nr_groups) {
111 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
112 nr_groups);
1da177e4
LT
113 }
114
28de7948 115 if (bitmap->s_block_bitmap[block_group]) {
1da177e4 116 return block_group;
28de7948
CG
117 } else {
118 retval = read_block_bitmap(sb, bitmap, block_group,
119 block_group);
1da177e4
LT
120 if (retval < 0)
121 return retval;
122 return block_group;
123 }
124}
125
cb00ea35
CG
126static inline int load_block_bitmap(struct super_block *sb,
127 struct udf_bitmap *bitmap,
128 unsigned int block_group)
1da177e4
LT
129{
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
cb00ea35
CG
143static 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)
1da177e4
LT
148{
149 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 150 struct buffer_head *bh = NULL;
1da177e4
LT
151 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
1e7933de 158 mutex_lock(&sbi->s_alloc_mutex);
1da177e4 159 if (bloc.logicalBlockNum < 0 ||
4b11111a
MS
160 (bloc.logicalBlockNum + count) >
161 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
28de7948
CG
162 udf_debug("%d < %d || %d + %d > %d\n",
163 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
4b11111a
MS
164 sbi->s_partmaps[bloc.partitionReferenceNum].
165 s_partition_len);
1da177e4
LT
166 goto error_return;
167 }
168
4b11111a
MS
169 block = bloc.logicalBlockNum + offset +
170 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 171
28de7948 172do_more:
1da177e4
LT
173 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 */
cb00ea35 180 if (bit + count > (sb->s_blocksize << 3)) {
1da177e4
LT
181 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];
cb00ea35
CG
189 for (i = 0; i < count; i++) {
190 if (udf_set_bit(bit + i, bh->b_data)) {
1da177e4 191 udf_debug("bit %ld already set\n", bit + i);
4b11111a
MS
192 udf_debug("byte=%2x\n",
193 ((char *)bh->b_data)[(bit + i) >> 3]);
cb00ea35 194 } else {
1da177e4
LT
195 if (inode)
196 DQUOT_FREE_BLOCK(inode, 1);
6c79e987
MS
197 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);
1da177e4
LT
201 }
202 }
203 }
204 mark_buffer_dirty(bh);
cb00ea35 205 if (overflow) {
1da177e4
LT
206 block += count;
207 count = overflow;
208 goto do_more;
209 }
28de7948 210error_return:
1da177e4 211 sb->s_dirt = 1;
6c79e987
MS
212 if (sbi->s_lvid_bh)
213 mark_buffer_dirty(sbi->s_lvid_bh);
1e7933de 214 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
215 return;
216}
217
cb00ea35
CG
218static 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)
1da177e4
LT
223{
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;
6c79e987 229 __u32 part_len;
1da177e4 230
1e7933de 231 mutex_lock(&sbi->s_alloc_mutex);
6c79e987
MS
232 part_len = sbi->s_partmaps[partition].s_partition_len;
233 if (first_block < 0 || first_block >= part_len)
1da177e4
LT
234 goto out;
235
6c79e987
MS
236 if (first_block + block_count > part_len)
237 block_count = part_len - first_block;
1da177e4 238
28de7948 239repeat:
883cb9d1 240 nr_groups = udf_compute_nr_groups(sb, partition);
1da177e4
LT
241 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
cb00ea35 252 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
28de7948 253 if (!udf_test_bit(bit, bh->b_data)) {
1da177e4 254 goto out;
28de7948 255 } else if (DQUOT_PREALLOC_BLOCK(inode, 1)) {
1da177e4 256 goto out;
28de7948 257 } else if (!udf_clear_bit(bit, bh->b_data)) {
1da177e4
LT
258 udf_debug("bit already cleared for block %d\n", bit);
259 DQUOT_FREE_BLOCK(inode, 1);
260 goto out;
261 }
cb00ea35
CG
262 block_count--;
263 alloc_count++;
264 bit++;
265 block++;
1da177e4
LT
266 }
267 mark_buffer_dirty(bh);
268 if (block_count > 0)
269 goto repeat;
28de7948 270out:
6c79e987
MS
271 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);
1da177e4
LT
276 }
277 sb->s_dirt = 1;
1e7933de 278 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
279 return alloc_count;
280}
281
cb00ea35
CG
282static 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)
1da177e4
LT
286{
287 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 288 int newbit, bit = 0, block, block_group, group_start;
1da177e4
LT
289 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;
1e7933de 295 mutex_lock(&sbi->s_alloc_mutex);
1da177e4 296
28de7948 297repeat:
6c79e987 298 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
299 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];
28de7948
CG
310 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
311 sb->s_blocksize - group_start);
1da177e4 312
cb00ea35 313 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4 314 bit = block % (sb->s_blocksize << 3);
28de7948 315 if (udf_test_bit(bit, bh->b_data))
1da177e4 316 goto got_block;
28de7948 317
1da177e4
LT
318 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;
28de7948 322
4b11111a
MS
323 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
324 sb->s_blocksize - ((bit + 7) >> 3));
1da177e4 325 newbit = (ptr - ((char *)bh->b_data)) << 3;
cb00ea35 326 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
327 bit = newbit;
328 goto search_back;
329 }
28de7948 330
4b11111a
MS
331 newbit = udf_find_next_one_bit(bh->b_data,
332 sb->s_blocksize << 3, bit);
cb00ea35 333 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
334 bit = newbit;
335 goto got_block;
336 }
337 }
338
cb00ea35
CG
339 for (i = 0; i < (nr_groups * 2); i++) {
340 block_group++;
1da177e4
LT
341 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];
cb00ea35 349 if (i < nr_groups) {
28de7948
CG
350 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
351 sb->s_blocksize - group_start);
cb00ea35 352 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4
LT
353 bit = (ptr - ((char *)bh->b_data)) << 3;
354 break;
355 }
cb00ea35 356 } else {
28de7948
CG
357 bit = udf_find_next_one_bit((char *)bh->b_data,
358 sb->s_blocksize << 3,
359 group_start << 3);
1da177e4
LT
360 if (bit < sb->s_blocksize << 3)
361 break;
362 }
363 }
cb00ea35 364 if (i >= (nr_groups * 2)) {
1e7933de 365 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
366 return newblock;
367 }
368 if (bit < sb->s_blocksize << 3)
369 goto search_back;
370 else
4b11111a
MS
371 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
372 group_start << 3);
cb00ea35 373 if (bit >= sb->s_blocksize << 3) {
1e7933de 374 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
375 return 0;
376 }
377
28de7948 378search_back:
4b11111a
MS
379 i = 0;
380 while (i < 7 && bit > (group_start << 3) &&
381 udf_test_bit(bit - 1, bh->b_data)) {
382 ++i;
383 --bit;
384 }
1da177e4 385
28de7948 386got_block:
1da177e4
LT
387
388 /*
389 * Check quota for allocation of this block.
390 */
cb00ea35 391 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
1e7933de 392 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
393 *err = -EDQUOT;
394 return 0;
395 }
396
397 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
28de7948 398 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 399
cb00ea35 400 if (!udf_clear_bit(bit, bh->b_data)) {
1da177e4
LT
401 udf_debug("bit already cleared for block %d\n", bit);
402 goto repeat;
403 }
404
405 mark_buffer_dirty(bh);
406
6c79e987
MS
407 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);
1da177e4
LT
412 }
413 sb->s_dirt = 1;
1e7933de 414 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
415 *err = 0;
416 return newblock;
417
28de7948 418error_return:
1da177e4 419 *err = -EIO;
1e7933de 420 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
421 return 0;
422}
423
cb00ea35
CG
424static 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)
1da177e4
LT
429{
430 struct udf_sb_info *sbi = UDF_SB(sb);
431 uint32_t start, end;
ff116fc8
JK
432 uint32_t elen;
433 kernel_lb_addr eloc;
434 struct extent_position oepos, epos;
1da177e4
LT
435 int8_t etype;
436 int i;
437
1e7933de 438 mutex_lock(&sbi->s_alloc_mutex);
1da177e4 439 if (bloc.logicalBlockNum < 0 ||
4b11111a
MS
440 (bloc.logicalBlockNum + count) >
441 sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
28de7948
CG
442 udf_debug("%d < %d || %d + %d > %d\n",
443 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
4b11111a
MS
444 sbi->s_partmaps[bloc.partitionReferenceNum].
445 s_partition_len);
1da177e4
LT
446 goto error_return;
447 }
448
4b11111a
MS
449 /* We do this up front - There are some error conditions that
450 could occure, but.. oh well */
1da177e4
LT
451 if (inode)
452 DQUOT_FREE_BLOCK(inode, count);
6c79e987
MS
453 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);
1da177e4
LT
458 }
459
460 start = bloc.logicalBlockNum + offset;
461 end = bloc.logicalBlockNum + offset + count - 1;
462
ff116fc8 463 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
1da177e4 464 elen = 0;
ff116fc8
JK
465 epos.block = oepos.block = UDF_I_LOCATION(table);
466 epos.bh = oepos.bh = NULL;
1da177e4 467
28de7948
CG
468 while (count &&
469 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
4b11111a
MS
470 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);
cb00ea35 480 } else {
4b11111a
MS
481 elen = (etype << 30) |
482 (elen +
483 (count << sb->s_blocksize_bits));
1da177e4
LT
484 start += count;
485 count = 0;
486 }
ff116fc8 487 udf_write_aext(table, &oepos, eloc, elen, 1);
cb00ea35 488 } else if (eloc.logicalBlockNum == (end + 1)) {
4b11111a
MS
489 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);
cb00ea35 498 } else {
1da177e4 499 eloc.logicalBlockNum = start;
4b11111a
MS
500 elen = (etype << 30) |
501 (elen +
502 (count << sb->s_blocksize_bits));
1da177e4
LT
503 end -= count;
504 count = 0;
505 }
ff116fc8 506 udf_write_aext(table, &oepos, eloc, elen, 1);
1da177e4
LT
507 }
508
cb00ea35 509 if (epos.bh != oepos.bh) {
1da177e4 510 i = -1;
ff116fc8 511 oepos.block = epos.block;
3bf25cb4
JK
512 brelse(oepos.bh);
513 get_bh(epos.bh);
ff116fc8
JK
514 oepos.bh = epos.bh;
515 oepos.offset = 0;
28de7948 516 } else {
ff116fc8 517 oepos.offset = epos.offset;
28de7948 518 }
1da177e4
LT
519 }
520
cb00ea35 521 if (count) {
28de7948 522 /*
4b11111a
MS
523 * 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 :)
28de7948
CG
526 *
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.
cb00ea35 533 */
1da177e4
LT
534
535 int adsize;
536 short_ad *sad = NULL;
537 long_ad *lad = NULL;
538 struct allocExtDesc *aed;
539
540 eloc.logicalBlockNum = start;
28de7948
CG
541 elen = EXT_RECORDED_ALLOCATED |
542 (count << sb->s_blocksize_bits);
1da177e4 543
28de7948 544 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT) {
1da177e4 545 adsize = sizeof(short_ad);
28de7948 546 } else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG) {
1da177e4 547 adsize = sizeof(long_ad);
28de7948 548 } else {
3bf25cb4
JK
549 brelse(oepos.bh);
550 brelse(epos.bh);
1da177e4
LT
551 goto error_return;
552 }
553
cb00ea35 554 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
1da177e4
LT
555 char *sptr, *dptr;
556 int loffset;
cb00ea35 557
3bf25cb4 558 brelse(oepos.bh);
ff116fc8 559 oepos = epos;
1da177e4
LT
560
561 /* Steal a block from the extent being free'd */
ff116fc8 562 epos.block.logicalBlockNum = eloc.logicalBlockNum;
cb00ea35 563 eloc.logicalBlockNum++;
1da177e4
LT
564 elen -= sb->s_blocksize;
565
4b11111a
MS
566 epos.bh = udf_tread(sb,
567 udf_get_lb_pblock(sb, epos.block, 0));
568 if (!epos.bh) {
3bf25cb4 569 brelse(oepos.bh);
1da177e4
LT
570 goto error_return;
571 }
ff116fc8 572 aed = (struct allocExtDesc *)(epos.bh->b_data);
4b11111a
MS
573 aed->previousAllocExtLocation =
574 cpu_to_le32(oepos.block.logicalBlockNum);
cb00ea35 575 if (epos.offset + adsize > sb->s_blocksize) {
ff116fc8 576 loffset = epos.offset;
1da177e4 577 aed->lengthAllocDescs = cpu_to_le32(adsize);
f5cc15da 578 sptr = UDF_I_DATA(table) + epos.offset - adsize;
4b11111a
MS
579 dptr = epos.bh->b_data +
580 sizeof(struct allocExtDesc);
1da177e4 581 memcpy(dptr, sptr, adsize);
4b11111a
MS
582 epos.offset = sizeof(struct allocExtDesc) +
583 adsize;
cb00ea35 584 } else {
ff116fc8 585 loffset = epos.offset + adsize;
1da177e4 586 aed->lengthAllocDescs = cpu_to_le32(0);
cb00ea35 587 if (oepos.bh) {
f5cc15da 588 sptr = oepos.bh->b_data + epos.offset;
4b11111a
MS
589 aed = (struct allocExtDesc *)
590 oepos.bh->b_data;
1da177e4 591 aed->lengthAllocDescs =
4b11111a
MS
592 cpu_to_le32(le32_to_cpu(
593 aed->lengthAllocDescs) +
594 adsize);
cb00ea35 595 } else {
f5cc15da 596 sptr = UDF_I_DATA(table) + epos.offset;
1da177e4
LT
597 UDF_I_LENALLOC(table) += adsize;
598 mark_inode_dirty(table);
599 }
f5cc15da 600 epos.offset = sizeof(struct allocExtDesc);
1da177e4 601 }
6c79e987 602 if (sbi->s_udfrev >= 0x0200)
4b11111a
MS
603 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
604 3, 1, epos.block.logicalBlockNum,
605 sizeof(tag));
1da177e4 606 else
4b11111a
MS
607 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
608 2, 1, epos.block.logicalBlockNum,
609 sizeof(tag));
28de7948 610
cb00ea35 611 switch (UDF_I_ALLOCTYPE(table)) {
4b11111a
MS
612 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;
1da177e4 628 }
cb00ea35 629 if (oepos.bh) {
ff116fc8
JK
630 udf_update_tag(oepos.bh->b_data, loffset);
631 mark_buffer_dirty(oepos.bh);
28de7948 632 } else {
1da177e4 633 mark_inode_dirty(table);
28de7948 634 }
1da177e4
LT
635 }
636
4b11111a
MS
637 /* It's possible that stealing the block emptied the extent */
638 if (elen) {
ff116fc8 639 udf_write_aext(table, &epos, eloc, elen, 1);
1da177e4 640
cb00ea35 641 if (!epos.bh) {
1da177e4
LT
642 UDF_I_LENALLOC(table) += adsize;
643 mark_inode_dirty(table);
cb00ea35 644 } else {
ff116fc8 645 aed = (struct allocExtDesc *)epos.bh->b_data;
1da177e4 646 aed->lengthAllocDescs =
4b11111a
MS
647 cpu_to_le32(le32_to_cpu(
648 aed->lengthAllocDescs) + adsize);
ff116fc8
JK
649 udf_update_tag(epos.bh->b_data, epos.offset);
650 mark_buffer_dirty(epos.bh);
1da177e4
LT
651 }
652 }
653 }
654
3bf25cb4
JK
655 brelse(epos.bh);
656 brelse(oepos.bh);
1da177e4 657
28de7948 658error_return:
1da177e4 659 sb->s_dirt = 1;
1e7933de 660 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
661 return;
662}
663
cb00ea35
CG
664static 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)
1da177e4
LT
668{
669 struct udf_sb_info *sbi = UDF_SB(sb);
670 int alloc_count = 0;
ff116fc8
JK
671 uint32_t elen, adsize;
672 kernel_lb_addr eloc;
673 struct extent_position epos;
1da177e4
LT
674 int8_t etype = -1;
675
4b11111a
MS
676 if (first_block < 0 ||
677 first_block >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
678 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
1e7933de 687 mutex_lock(&sbi->s_alloc_mutex);
ff116fc8
JK
688 epos.offset = sizeof(struct unallocSpaceEntry);
689 epos.block = UDF_I_LOCATION(table);
690 epos.bh = NULL;
1da177e4
LT
691 eloc.logicalBlockNum = 0xFFFFFFFF;
692
28de7948
CG
693 while (first_block != eloc.logicalBlockNum &&
694 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
1da177e4 695 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
cb00ea35 696 eloc.logicalBlockNum, elen, first_block);
28de7948 697 ; /* empty loop body */
1da177e4
LT
698 }
699
cb00ea35 700 if (first_block == eloc.logicalBlockNum) {
ff116fc8 701 epos.offset -= adsize;
1da177e4
LT
702
703 alloc_count = (elen >> sb->s_blocksize_bits);
4b11111a
MS
704 if (inode && DQUOT_PREALLOC_BLOCK(inode,
705 alloc_count > block_count ? block_count : alloc_count))
1da177e4 706 alloc_count = 0;
4b11111a 707 else if (alloc_count > block_count) {
1da177e4
LT
708 alloc_count = block_count;
709 eloc.logicalBlockNum += alloc_count;
710 elen -= (alloc_count << sb->s_blocksize_bits);
4b11111a
MS
711 udf_write_aext(table, &epos, eloc,
712 (etype << 30) | elen, 1);
713 } else
714 udf_delete_aext(table, epos, eloc,
715 (etype << 30) | elen);
28de7948 716 } else {
1da177e4 717 alloc_count = 0;
28de7948 718 }
1da177e4 719
3bf25cb4 720 brelse(epos.bh);
1da177e4 721
6c79e987
MS
722 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);
1da177e4
LT
727 sb->s_dirt = 1;
728 }
1e7933de 729 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
730 return alloc_count;
731}
732
cb00ea35
CG
733static 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)
1da177e4
LT
737{
738 struct udf_sb_info *sbi = UDF_SB(sb);
739 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
740 uint32_t newblock = 0, adsize;
ff116fc8 741 uint32_t elen, goal_elen = 0;
3ad90ec0 742 kernel_lb_addr eloc, uninitialized_var(goal_eloc);
ff116fc8 743 struct extent_position epos, goal_epos;
1da177e4
LT
744 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
1e7933de 755 mutex_lock(&sbi->s_alloc_mutex);
6c79e987 756 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
757 goal = 0;
758
4b11111a
MS
759 /* 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.
cb00ea35 763 */
ff116fc8
JK
764 epos.offset = sizeof(struct unallocSpaceEntry);
765 epos.block = UDF_I_LOCATION(table);
766 epos.bh = goal_epos.bh = NULL;
1da177e4 767
28de7948
CG
768 while (spread &&
769 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
cb00ea35 770 if (goal >= eloc.logicalBlockNum) {
4b11111a
MS
771 if (goal < eloc.logicalBlockNum +
772 (elen >> sb->s_blocksize_bits))
1da177e4
LT
773 nspread = 0;
774 else
775 nspread = goal - eloc.logicalBlockNum -
28de7948
CG
776 (elen >> sb->s_blocksize_bits);
777 } else {
1da177e4 778 nspread = eloc.logicalBlockNum - goal;
28de7948 779 }
1da177e4 780
cb00ea35 781 if (nspread < spread) {
1da177e4 782 spread = nspread;
cb00ea35 783 if (goal_epos.bh != epos.bh) {
3bf25cb4 784 brelse(goal_epos.bh);
ff116fc8 785 goal_epos.bh = epos.bh;
3bf25cb4 786 get_bh(goal_epos.bh);
1da177e4 787 }
ff116fc8
JK
788 goal_epos.block = epos.block;
789 goal_epos.offset = epos.offset - adsize;
1da177e4
LT
790 goal_eloc = eloc;
791 goal_elen = (etype << 30) | elen;
792 }
793 }
794
3bf25cb4 795 brelse(epos.bh);
1da177e4 796
cb00ea35 797 if (spread == 0xFFFFFFFF) {
3bf25cb4 798 brelse(goal_epos.bh);
1e7933de 799 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
800 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;
cb00ea35 809 goal_eloc.logicalBlockNum++;
1da177e4
LT
810 goal_elen -= sb->s_blocksize;
811
cb00ea35 812 if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
3bf25cb4 813 brelse(goal_epos.bh);
1e7933de 814 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
815 *err = -EDQUOT;
816 return 0;
817 }
818
819 if (goal_elen)
ff116fc8 820 udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
1da177e4 821 else
ff116fc8 822 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
3bf25cb4 823 brelse(goal_epos.bh);
1da177e4 824
6c79e987
MS
825 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);
1da177e4
LT
830 }
831
832 sb->s_dirt = 1;
1e7933de 833 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
834 *err = 0;
835 return newblock;
836}
837
cb00ea35
CG
838inline void udf_free_blocks(struct super_block *sb,
839 struct inode *inode,
840 kernel_lb_addr bloc, uint32_t offset,
841 uint32_t count)
1da177e4
LT
842{
843 uint16_t partition = bloc.partitionReferenceNum;
6c79e987 844 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1da177e4 845
6c79e987 846 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
1da177e4 847 return udf_bitmap_free_blocks(sb, inode,
6c79e987 848 map->s_uspace.s_bitmap,
28de7948 849 bloc, offset, count);
6c79e987 850 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
1da177e4 851 return udf_table_free_blocks(sb, inode,
6c79e987 852 map->s_uspace.s_table,
28de7948 853 bloc, offset, count);
6c79e987 854 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
1da177e4 855 return udf_bitmap_free_blocks(sb, inode,
6c79e987 856 map->s_fspace.s_bitmap,
28de7948 857 bloc, offset, count);
6c79e987 858 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
1da177e4 859 return udf_table_free_blocks(sb, inode,
6c79e987 860 map->s_fspace.s_table,
28de7948
CG
861 bloc, offset, count);
862 } else {
1da177e4 863 return;
28de7948 864 }
1da177e4
LT
865}
866
cb00ea35
CG
867inline 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)
1da177e4 871{
6c79e987
MS
872 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
873
4b11111a 874 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1da177e4 875 return udf_bitmap_prealloc_blocks(sb, inode,
6c79e987 876 map->s_uspace.s_bitmap,
4b11111a
MS
877 partition, first_block,
878 block_count);
879 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1da177e4 880 return udf_table_prealloc_blocks(sb, inode,
6c79e987 881 map->s_uspace.s_table,
4b11111a
MS
882 partition, first_block,
883 block_count);
884 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1da177e4 885 return udf_bitmap_prealloc_blocks(sb, inode,
6c79e987 886 map->s_fspace.s_bitmap,
4b11111a
MS
887 partition, first_block,
888 block_count);
889 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1da177e4 890 return udf_table_prealloc_blocks(sb, inode,
6c79e987 891 map->s_fspace.s_table,
4b11111a
MS
892 partition, first_block,
893 block_count);
894 else
1da177e4
LT
895 return 0;
896}
897
cb00ea35
CG
898inline int udf_new_block(struct super_block *sb,
899 struct inode *inode,
900 uint16_t partition, uint32_t goal, int *err)
1da177e4 901{
6c79e987 902 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
3bf25cb4 903
4b11111a
MS
904 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
905 return udf_bitmap_new_block(sb, inode,
6c79e987 906 map->s_uspace.s_bitmap,
28de7948 907 partition, goal, err);
4b11111a 908 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1da177e4 909 return udf_table_new_block(sb, inode,
6c79e987 910 map->s_uspace.s_table,
28de7948 911 partition, goal, err);
4b11111a 912 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1da177e4 913 return udf_bitmap_new_block(sb, inode,
6c79e987 914 map->s_fspace.s_bitmap,
28de7948 915 partition, goal, err);
4b11111a 916 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1da177e4 917 return udf_table_new_block(sb, inode,
6c79e987 918 map->s_fspace.s_table,
28de7948 919 partition, goal, err);
4b11111a 920 else {
1da177e4
LT
921 *err = -EIO;
922 return 0;
923 }
924}