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