Merge tag 'bootconfig-v6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[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
1da177e4
LT
24#include <linux/bitops.h>
25
26#include "udf_i.h"
27#include "udf_sb.h"
28
9ad1e1e4
AM
29#define udf_clear_bit __test_and_clear_bit_le
30#define udf_set_bit __test_and_set_bit_le
31#define udf_test_bit test_bit_le
32#define udf_find_next_one_bit find_next_bit_le
1da177e4 33
cb00ea35
CG
34static int read_block_bitmap(struct super_block *sb,
35 struct udf_bitmap *bitmap, unsigned int block,
36 unsigned long bitmap_nr)
1da177e4
LT
37{
38 struct buffer_head *bh = NULL;
1e0d4adf
VE
39 int i;
40 int max_bits, off, count;
5ca4e4be 41 struct kernel_lb_addr loc;
1da177e4
LT
42
43 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 44 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1da177e4 45
101ee137 46 bh = sb_bread(sb, udf_get_lb_pblock(sb, &loc, block));
1e0d4adf 47 bitmap->s_block_bitmap[bitmap_nr] = bh;
4b11111a 48 if (!bh)
1e0d4adf 49 return -EIO;
4b11111a 50
1e0d4adf
VE
51 /* Check consistency of Space Bitmap buffer. */
52 max_bits = sb->s_blocksize * 8;
53 if (!bitmap_nr) {
54 off = sizeof(struct spaceBitmapDesc) << 3;
55 count = min(max_bits - off, bitmap->s_nr_groups);
56 } else {
57 /*
58 * Rough check if bitmap number is too big to have any bitmap
59 * blocks reserved.
60 */
61 if (bitmap_nr >
62 (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2)
63 return 0;
64 off = 0;
65 count = bitmap->s_nr_groups - bitmap_nr * max_bits +
66 (sizeof(struct spaceBitmapDesc) << 3);
67 count = min(count, max_bits);
68 }
69
70 for (i = 0; i < count; i++)
71 if (udf_test_bit(i + off, bh->b_data))
72 return -EFSCORRUPTED;
73 return 0;
1da177e4
LT
74}
75
cb00ea35
CG
76static int __load_block_bitmap(struct super_block *sb,
77 struct udf_bitmap *bitmap,
78 unsigned int block_group)
1da177e4
LT
79{
80 int retval = 0;
81 int nr_groups = bitmap->s_nr_groups;
82
cb00ea35 83 if (block_group >= nr_groups) {
fcbf7637 84 udf_debug("block_group (%u) > nr_groups (%d)\n",
a983f368 85 block_group, nr_groups);
1da177e4
LT
86 }
87
6fbaad87 88 if (bitmap->s_block_bitmap[block_group])
1da177e4 89 return block_group;
6fbaad87
FF
90
91 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
92 if (retval < 0)
93 return retval;
94
95 return block_group;
1da177e4
LT
96}
97
cb00ea35
CG
98static inline int load_block_bitmap(struct super_block *sb,
99 struct udf_bitmap *bitmap,
100 unsigned int block_group)
1da177e4
LT
101{
102 int slot;
103
104 slot = __load_block_bitmap(sb, bitmap, block_group);
105
106 if (slot < 0)
107 return slot;
108
109 if (!bitmap->s_block_bitmap[slot])
110 return -EIO;
111
112 return slot;
113}
114
146bca72 115static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
742ba02a 116{
146bca72 117 struct udf_sb_info *sbi = UDF_SB(sb);
742ba02a
MS
118 struct logicalVolIntegrityDesc *lvid;
119
146bca72
JK
120 if (!sbi->s_lvid_bh)
121 return;
742ba02a
MS
122
123 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
c2104fda 124 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
146bca72 125 udf_updated_lvid(sb);
742ba02a
MS
126}
127
cb00ea35 128static void udf_bitmap_free_blocks(struct super_block *sb,
cb00ea35 129 struct udf_bitmap *bitmap,
97e961fd
PE
130 struct kernel_lb_addr *bloc,
131 uint32_t offset,
cb00ea35 132 uint32_t count)
1da177e4
LT
133{
134 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 135 struct buffer_head *bh = NULL;
97e961fd 136 struct udf_part_map *partmap;
1da177e4
LT
137 unsigned long block;
138 unsigned long block_group;
139 unsigned long bit;
140 unsigned long i;
141 int bitmap_nr;
142 unsigned long overflow;
143
1e7933de 144 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 145 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
146 if (bloc->logicalBlockNum + count < count ||
147 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
fcbf7637 148 udf_debug("%u < %d || %u + %u > %u\n",
a983f368
JP
149 bloc->logicalBlockNum, 0,
150 bloc->logicalBlockNum, count,
151 partmap->s_partition_len);
1da177e4
LT
152 goto error_return;
153 }
154
97e961fd 155 block = bloc->logicalBlockNum + offset +
4b11111a 156 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 157
4daa1b87
MS
158 do {
159 overflow = 0;
160 block_group = block >> (sb->s_blocksize_bits + 3);
161 bit = block % (sb->s_blocksize << 3);
162
163 /*
164 * Check to see if we are freeing blocks across a group boundary.
165 */
166 if (bit + count > (sb->s_blocksize << 3)) {
167 overflow = bit + count - (sb->s_blocksize << 3);
168 count -= overflow;
1da177e4 169 }
4daa1b87
MS
170 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
171 if (bitmap_nr < 0)
172 goto error_return;
173
174 bh = bitmap->s_block_bitmap[bitmap_nr];
175 for (i = 0; i < count; i++) {
176 if (udf_set_bit(bit + i, bh->b_data)) {
fcbf7637 177 udf_debug("bit %lu already set\n", bit + i);
4daa1b87 178 udf_debug("byte=%2x\n",
fcbf7637 179 ((__u8 *)bh->b_data)[(bit + i) >> 3]);
4daa1b87
MS
180 }
181 }
7abc2e45 182 udf_add_free_space(sb, sbi->s_partition, count);
4daa1b87
MS
183 mark_buffer_dirty(bh);
184 if (overflow) {
185 block += count;
186 count = overflow;
187 }
188 } while (overflow);
189
28de7948 190error_return:
1e7933de 191 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
192}
193
cb00ea35 194static int udf_bitmap_prealloc_blocks(struct super_block *sb,
cb00ea35
CG
195 struct udf_bitmap *bitmap,
196 uint16_t partition, uint32_t first_block,
197 uint32_t block_count)
1da177e4
LT
198{
199 struct udf_sb_info *sbi = UDF_SB(sb);
200 int alloc_count = 0;
849fe89c
CIK
201 int bit, block, block_group;
202 int bitmap_nr;
1da177e4 203 struct buffer_head *bh;
6c79e987 204 __u32 part_len;
1da177e4 205
1e7933de 206 mutex_lock(&sbi->s_alloc_mutex);
6c79e987 207 part_len = sbi->s_partmaps[partition].s_partition_len;
3391faa4 208 if (first_block >= part_len)
1da177e4
LT
209 goto out;
210
6c79e987
MS
211 if (first_block + block_count > part_len)
212 block_count = part_len - first_block;
1da177e4 213
4daa1b87 214 do {
4daa1b87
MS
215 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
216 block_group = block >> (sb->s_blocksize_bits + 3);
1da177e4 217
4daa1b87
MS
218 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
219 if (bitmap_nr < 0)
220 goto out;
221 bh = bitmap->s_block_bitmap[bitmap_nr];
1da177e4 222
4daa1b87 223 bit = block % (sb->s_blocksize << 3);
1da177e4 224
4daa1b87 225 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
36350462 226 if (!udf_clear_bit(bit, bh->b_data))
4daa1b87 227 goto out;
4daa1b87
MS
228 block_count--;
229 alloc_count++;
230 bit++;
231 block++;
1da177e4 232 }
4daa1b87
MS
233 mark_buffer_dirty(bh);
234 } while (block_count > 0);
235
28de7948 236out:
146bca72 237 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 238 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
239 return alloc_count;
240}
241
b490bdd6 242static udf_pblk_t udf_bitmap_new_block(struct super_block *sb,
cb00ea35
CG
243 struct udf_bitmap *bitmap, uint16_t partition,
244 uint32_t goal, int *err)
1da177e4
LT
245{
246 struct udf_sb_info *sbi = UDF_SB(sb);
b490bdd6
SM
247 int newbit, bit = 0;
248 udf_pblk_t block;
249 int block_group, group_start;
1da177e4
LT
250 int end_goal, nr_groups, bitmap_nr, i;
251 struct buffer_head *bh = NULL;
252 char *ptr;
b490bdd6 253 udf_pblk_t newblock = 0;
1da177e4
LT
254
255 *err = -ENOSPC;
1e7933de 256 mutex_lock(&sbi->s_alloc_mutex);
1da177e4 257
28de7948 258repeat:
3391faa4 259 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
260 goal = 0;
261
262 nr_groups = bitmap->s_nr_groups;
263 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
264 block_group = block >> (sb->s_blocksize_bits + 3);
265 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
266
267 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
268 if (bitmap_nr < 0)
269 goto error_return;
270 bh = bitmap->s_block_bitmap[bitmap_nr];
28de7948
CG
271 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
272 sb->s_blocksize - group_start);
1da177e4 273
cb00ea35 274 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4 275 bit = block % (sb->s_blocksize << 3);
28de7948 276 if (udf_test_bit(bit, bh->b_data))
1da177e4 277 goto got_block;
28de7948 278
1da177e4
LT
279 end_goal = (bit + 63) & ~63;
280 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
281 if (bit < end_goal)
282 goto got_block;
28de7948 283
4b11111a
MS
284 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
285 sb->s_blocksize - ((bit + 7) >> 3));
1da177e4 286 newbit = (ptr - ((char *)bh->b_data)) << 3;
cb00ea35 287 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
288 bit = newbit;
289 goto search_back;
290 }
28de7948 291
4b11111a
MS
292 newbit = udf_find_next_one_bit(bh->b_data,
293 sb->s_blocksize << 3, bit);
cb00ea35 294 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
295 bit = newbit;
296 goto got_block;
297 }
298 }
299
cb00ea35
CG
300 for (i = 0; i < (nr_groups * 2); i++) {
301 block_group++;
1da177e4
LT
302 if (block_group >= nr_groups)
303 block_group = 0;
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];
cb00ea35 310 if (i < nr_groups) {
28de7948
CG
311 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
312 sb->s_blocksize - group_start);
cb00ea35 313 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4
LT
314 bit = (ptr - ((char *)bh->b_data)) << 3;
315 break;
316 }
cb00ea35 317 } else {
6f644e5f 318 bit = udf_find_next_one_bit(bh->b_data,
28de7948
CG
319 sb->s_blocksize << 3,
320 group_start << 3);
1da177e4
LT
321 if (bit < sb->s_blocksize << 3)
322 break;
323 }
324 }
cb00ea35 325 if (i >= (nr_groups * 2)) {
1e7933de 326 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
327 return newblock;
328 }
329 if (bit < sb->s_blocksize << 3)
330 goto search_back;
331 else
4b11111a
MS
332 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
333 group_start << 3);
cb00ea35 334 if (bit >= sb->s_blocksize << 3) {
1e7933de 335 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
336 return 0;
337 }
338
28de7948 339search_back:
4b11111a
MS
340 i = 0;
341 while (i < 7 && bit > (group_start << 3) &&
342 udf_test_bit(bit - 1, bh->b_data)) {
343 ++i;
344 --bit;
345 }
1da177e4 346
28de7948 347got_block:
1da177e4 348 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
28de7948 349 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 350
56db1991
SM
351 if (newblock >= sbi->s_partmaps[partition].s_partition_len) {
352 /*
353 * Ran off the end of the bitmap, and bits following are
354 * non-compliant (not all zero)
355 */
356 udf_err(sb, "bitmap for partition %d corrupted (block %u marked"
357 " as free, partition length is %u)\n", partition,
358 newblock, sbi->s_partmaps[partition].s_partition_len);
359 goto error_return;
360 }
361
cb00ea35 362 if (!udf_clear_bit(bit, bh->b_data)) {
1da177e4
LT
363 udf_debug("bit already cleared for block %d\n", bit);
364 goto repeat;
365 }
366
367 mark_buffer_dirty(bh);
368
146bca72 369 udf_add_free_space(sb, partition, -1);
1e7933de 370 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
371 *err = 0;
372 return newblock;
373
28de7948 374error_return:
1da177e4 375 *err = -EIO;
1e7933de 376 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
377 return 0;
378}
379
cb00ea35 380static void udf_table_free_blocks(struct super_block *sb,
cb00ea35 381 struct inode *table,
97e961fd
PE
382 struct kernel_lb_addr *bloc,
383 uint32_t offset,
cb00ea35 384 uint32_t count)
1da177e4
LT
385{
386 struct udf_sb_info *sbi = UDF_SB(sb);
97e961fd 387 struct udf_part_map *partmap;
1da177e4 388 uint32_t start, end;
ff116fc8 389 uint32_t elen;
5ca4e4be 390 struct kernel_lb_addr eloc;
ff116fc8 391 struct extent_position oepos, epos;
1da177e4 392 int8_t etype;
48d6d8ff 393 struct udf_inode_info *iinfo;
1da177e4 394
1e7933de 395 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 396 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
397 if (bloc->logicalBlockNum + count < count ||
398 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
fcbf7637 399 udf_debug("%u < %d || %u + %u > %u\n",
a983f368
JP
400 bloc->logicalBlockNum, 0,
401 bloc->logicalBlockNum, count,
97e961fd 402 partmap->s_partition_len);
1da177e4
LT
403 goto error_return;
404 }
405
48d6d8ff 406 iinfo = UDF_I(table);
146bca72 407 udf_add_free_space(sb, sbi->s_partition, count);
1da177e4 408
97e961fd
PE
409 start = bloc->logicalBlockNum + offset;
410 end = bloc->logicalBlockNum + offset + count - 1;
1da177e4 411
ff116fc8 412 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
1da177e4 413 elen = 0;
48d6d8ff 414 epos.block = oepos.block = iinfo->i_location;
ff116fc8 415 epos.bh = oepos.bh = NULL;
1da177e4 416
28de7948
CG
417 while (count &&
418 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
4b11111a
MS
419 if (((eloc.logicalBlockNum +
420 (elen >> sb->s_blocksize_bits)) == start)) {
421 if ((0x3FFFFFFF - elen) <
422 (count << sb->s_blocksize_bits)) {
423 uint32_t tmp = ((0x3FFFFFFF - elen) >>
424 sb->s_blocksize_bits);
425 count -= tmp;
426 start += tmp;
427 elen = (etype << 30) |
428 (0x40000000 - sb->s_blocksize);
cb00ea35 429 } else {
4b11111a
MS
430 elen = (etype << 30) |
431 (elen +
432 (count << sb->s_blocksize_bits));
1da177e4
LT
433 start += count;
434 count = 0;
435 }
97e961fd 436 udf_write_aext(table, &oepos, &eloc, elen, 1);
cb00ea35 437 } else if (eloc.logicalBlockNum == (end + 1)) {
4b11111a
MS
438 if ((0x3FFFFFFF - elen) <
439 (count << sb->s_blocksize_bits)) {
440 uint32_t tmp = ((0x3FFFFFFF - elen) >>
441 sb->s_blocksize_bits);
442 count -= tmp;
443 end -= tmp;
444 eloc.logicalBlockNum -= tmp;
445 elen = (etype << 30) |
446 (0x40000000 - sb->s_blocksize);
cb00ea35 447 } else {
1da177e4 448 eloc.logicalBlockNum = start;
4b11111a
MS
449 elen = (etype << 30) |
450 (elen +
451 (count << sb->s_blocksize_bits));
1da177e4
LT
452 end -= count;
453 count = 0;
454 }
97e961fd 455 udf_write_aext(table, &oepos, &eloc, elen, 1);
1da177e4
LT
456 }
457
cb00ea35 458 if (epos.bh != oepos.bh) {
ff116fc8 459 oepos.block = epos.block;
3bf25cb4
JK
460 brelse(oepos.bh);
461 get_bh(epos.bh);
ff116fc8
JK
462 oepos.bh = epos.bh;
463 oepos.offset = 0;
28de7948 464 } else {
ff116fc8 465 oepos.offset = epos.offset;
28de7948 466 }
1da177e4
LT
467 }
468
cb00ea35 469 if (count) {
28de7948 470 /*
4b11111a
MS
471 * NOTE: we CANNOT use udf_add_aext here, as it can try to
472 * allocate a new block, and since we hold the super block
473 * lock already very bad things would happen :)
28de7948
CG
474 *
475 * We copy the behavior of udf_add_aext, but instead of
476 * trying to allocate a new block close to the existing one,
477 * we just steal a block from the extent we are trying to add.
478 *
479 * It would be nice if the blocks were close together, but it
480 * isn't required.
cb00ea35 481 */
1da177e4
LT
482
483 int adsize;
1da177e4
LT
484
485 eloc.logicalBlockNum = start;
28de7948
CG
486 elen = EXT_RECORDED_ALLOCATED |
487 (count << sb->s_blocksize_bits);
1da177e4 488
48d6d8ff 489 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 490 adsize = sizeof(struct short_ad);
48d6d8ff 491 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 492 adsize = sizeof(struct long_ad);
48d6d8ff 493 else {
3bf25cb4
JK
494 brelse(oepos.bh);
495 brelse(epos.bh);
1da177e4
LT
496 goto error_return;
497 }
498
cb00ea35 499 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
1da177e4 500 /* Steal a block from the extent being free'd */
fcea62ba
JK
501 udf_setup_indirect_aext(table, eloc.logicalBlockNum,
502 &epos);
503
cb00ea35 504 eloc.logicalBlockNum++;
1da177e4 505 elen -= sb->s_blocksize;
1da177e4
LT
506 }
507
4b11111a 508 /* It's possible that stealing the block emptied the extent */
fcea62ba
JK
509 if (elen)
510 __udf_add_aext(table, &epos, &eloc, elen, 1);
1da177e4
LT
511 }
512
3bf25cb4
JK
513 brelse(epos.bh);
514 brelse(oepos.bh);
1da177e4 515
28de7948 516error_return:
1e7933de 517 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
518 return;
519}
520
cb00ea35 521static int udf_table_prealloc_blocks(struct super_block *sb,
cb00ea35
CG
522 struct inode *table, uint16_t partition,
523 uint32_t first_block, uint32_t block_count)
1da177e4
LT
524{
525 struct udf_sb_info *sbi = UDF_SB(sb);
526 int alloc_count = 0;
ff116fc8 527 uint32_t elen, adsize;
5ca4e4be 528 struct kernel_lb_addr eloc;
ff116fc8 529 struct extent_position epos;
1da177e4 530 int8_t etype = -1;
48d6d8ff 531 struct udf_inode_info *iinfo;
1da177e4 532
3391faa4 533 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
534 return 0;
535
48d6d8ff
MS
536 iinfo = UDF_I(table);
537 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 538 adsize = sizeof(struct short_ad);
48d6d8ff 539 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 540 adsize = sizeof(struct long_ad);
1da177e4
LT
541 else
542 return 0;
543
1e7933de 544 mutex_lock(&sbi->s_alloc_mutex);
ff116fc8 545 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 546 epos.block = iinfo->i_location;
ff116fc8 547 epos.bh = NULL;
1da177e4
LT
548 eloc.logicalBlockNum = 0xFFFFFFFF;
549
28de7948
CG
550 while (first_block != eloc.logicalBlockNum &&
551 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
fcbf7637 552 udf_debug("eloc=%u, elen=%u, first_block=%u\n",
cb00ea35 553 eloc.logicalBlockNum, elen, first_block);
28de7948 554 ; /* empty loop body */
1da177e4
LT
555 }
556
cb00ea35 557 if (first_block == eloc.logicalBlockNum) {
ff116fc8 558 epos.offset -= adsize;
1da177e4
LT
559
560 alloc_count = (elen >> sb->s_blocksize_bits);
36350462 561 if (alloc_count > block_count) {
1da177e4
LT
562 alloc_count = block_count;
563 eloc.logicalBlockNum += alloc_count;
564 elen -= (alloc_count << sb->s_blocksize_bits);
97e961fd 565 udf_write_aext(table, &epos, &eloc,
4b11111a
MS
566 (etype << 30) | elen, 1);
567 } else
6c1e4d06 568 udf_delete_aext(table, epos);
28de7948 569 } else {
1da177e4 570 alloc_count = 0;
28de7948 571 }
1da177e4 572
3bf25cb4 573 brelse(epos.bh);
1da177e4 574
146bca72
JK
575 if (alloc_count)
576 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 577 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
578 return alloc_count;
579}
580
b490bdd6 581static udf_pblk_t udf_table_new_block(struct super_block *sb,
cb00ea35
CG
582 struct inode *table, uint16_t partition,
583 uint32_t goal, int *err)
1da177e4
LT
584{
585 struct udf_sb_info *sbi = UDF_SB(sb);
586 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
b490bdd6
SM
587 udf_pblk_t newblock = 0;
588 uint32_t adsize;
ff116fc8 589 uint32_t elen, goal_elen = 0;
3f649ab7 590 struct kernel_lb_addr eloc, goal_eloc;
ff116fc8 591 struct extent_position epos, goal_epos;
1da177e4 592 int8_t etype;
48d6d8ff 593 struct udf_inode_info *iinfo = UDF_I(table);
1da177e4
LT
594
595 *err = -ENOSPC;
596
48d6d8ff 597 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 598 adsize = sizeof(struct short_ad);
48d6d8ff 599 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 600 adsize = sizeof(struct long_ad);
1da177e4
LT
601 else
602 return newblock;
603
1e7933de 604 mutex_lock(&sbi->s_alloc_mutex);
3391faa4 605 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
606 goal = 0;
607
4b11111a
MS
608 /* We search for the closest matching block to goal. If we find
609 a exact hit, we stop. Otherwise we keep going till we run out
610 of extents. We store the buffer_head, bloc, and extoffset
611 of the current closest match and use that when we are done.
cb00ea35 612 */
ff116fc8 613 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 614 epos.block = iinfo->i_location;
ff116fc8 615 epos.bh = goal_epos.bh = NULL;
1da177e4 616
28de7948
CG
617 while (spread &&
618 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
cb00ea35 619 if (goal >= eloc.logicalBlockNum) {
4b11111a
MS
620 if (goal < eloc.logicalBlockNum +
621 (elen >> sb->s_blocksize_bits))
1da177e4
LT
622 nspread = 0;
623 else
624 nspread = goal - eloc.logicalBlockNum -
28de7948
CG
625 (elen >> sb->s_blocksize_bits);
626 } else {
1da177e4 627 nspread = eloc.logicalBlockNum - goal;
28de7948 628 }
1da177e4 629
cb00ea35 630 if (nspread < spread) {
1da177e4 631 spread = nspread;
cb00ea35 632 if (goal_epos.bh != epos.bh) {
3bf25cb4 633 brelse(goal_epos.bh);
ff116fc8 634 goal_epos.bh = epos.bh;
3bf25cb4 635 get_bh(goal_epos.bh);
1da177e4 636 }
ff116fc8
JK
637 goal_epos.block = epos.block;
638 goal_epos.offset = epos.offset - adsize;
1da177e4
LT
639 goal_eloc = eloc;
640 goal_elen = (etype << 30) | elen;
641 }
642 }
643
3bf25cb4 644 brelse(epos.bh);
1da177e4 645
cb00ea35 646 if (spread == 0xFFFFFFFF) {
3bf25cb4 647 brelse(goal_epos.bh);
1e7933de 648 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
649 return 0;
650 }
651
652 /* Only allocate blocks from the beginning of the extent.
653 That way, we only delete (empty) extents, never have to insert an
654 extent because of splitting */
655 /* This works, but very poorly.... */
656
657 newblock = goal_eloc.logicalBlockNum;
cb00ea35 658 goal_eloc.logicalBlockNum++;
1da177e4 659 goal_elen -= sb->s_blocksize;
1da177e4
LT
660
661 if (goal_elen)
97e961fd 662 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
1da177e4 663 else
6c1e4d06 664 udf_delete_aext(table, goal_epos);
3bf25cb4 665 brelse(goal_epos.bh);
1da177e4 666
146bca72 667 udf_add_free_space(sb, partition, -1);
1da177e4 668
1e7933de 669 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
670 *err = 0;
671 return newblock;
672}
673
97e961fd
PE
674void udf_free_blocks(struct super_block *sb, struct inode *inode,
675 struct kernel_lb_addr *bloc, uint32_t offset,
676 uint32_t count)
1da177e4 677{
97e961fd 678 uint16_t partition = bloc->partitionReferenceNum;
6c79e987 679 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1da177e4 680
6c79e987 681 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
fd4287db 682 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
e650b94a 683 bloc, offset, count);
6c79e987 684 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
fd4287db 685 udf_table_free_blocks(sb, map->s_uspace.s_table,
e650b94a 686 bloc, offset, count);
28de7948 687 }
fd4287db
JK
688
689 if (inode) {
690 inode_sub_bytes(inode,
691 ((sector_t)count) << sb->s_blocksize_bits);
692 }
1da177e4
LT
693}
694
cb00ea35
CG
695inline int udf_prealloc_blocks(struct super_block *sb,
696 struct inode *inode,
697 uint16_t partition, uint32_t first_block,
698 uint32_t block_count)
1da177e4 699{
6c79e987 700 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1be440de 701 int allocated;
6c79e987 702
4b11111a 703 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
704 allocated = udf_bitmap_prealloc_blocks(sb,
705 map->s_uspace.s_bitmap,
706 partition, first_block,
707 block_count);
4b11111a 708 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
709 allocated = udf_table_prealloc_blocks(sb,
710 map->s_uspace.s_table,
711 partition, first_block,
712 block_count);
4b11111a 713 else
1da177e4 714 return 0;
fd4287db
JK
715
716 if (inode && allocated > 0)
717 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
718 return allocated;
1da177e4
LT
719}
720
b490bdd6 721inline udf_pblk_t udf_new_block(struct super_block *sb,
cb00ea35
CG
722 struct inode *inode,
723 uint16_t partition, uint32_t goal, int *err)
1da177e4 724{
6c79e987 725 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
b490bdd6 726 udf_pblk_t block;
3bf25cb4 727
4b11111a 728 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
729 block = udf_bitmap_new_block(sb,
730 map->s_uspace.s_bitmap,
731 partition, goal, err);
4b11111a 732 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
733 block = udf_table_new_block(sb,
734 map->s_uspace.s_table,
28de7948 735 partition, goal, err);
4b11111a 736 else {
1da177e4
LT
737 *err = -EIO;
738 return 0;
739 }
fd4287db
JK
740 if (inode && block)
741 inode_add_bytes(inode, sb->s_blocksize);
742 return block;
1da177e4 743}