25 #include <linux/bitops.h>
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
37 unsigned long bitmap_nr)
39 struct buffer_head *bh =
NULL;
46 bh =
udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
61 if (block_group >= nr_groups) {
62 udf_debug(
"block_group (%d) > nr_groups (%d)\n",
63 block_group, nr_groups);
69 retval = read_block_bitmap(sb, bitmap, block_group,
77 static inline int load_block_bitmap(
struct super_block *sb,
79 unsigned int block_group)
83 slot = __load_block_bitmap(sb, bitmap, block_group);
104 udf_updated_lvid(sb);
107 static void udf_bitmap_free_blocks(
struct super_block *sb,
114 struct buffer_head *bh =
NULL;
149 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
154 for (i = 0; i <
count; i++) {
156 udf_debug(
"bit %ld already set\n", bit + i);
158 ((
char *)bh->b_data)[(bit + i) >> 3]);
173 static int udf_bitmap_prealloc_blocks(
struct super_block *sb,
181 int nr_groups, bitmap_nr;
182 struct buffer_head *bh;
186 part_len = sbi->
s_partmaps[partition].s_partition_len;
187 if (first_block >= part_len)
190 if (first_block + block_count > part_len)
191 block_count = part_len - first_block;
199 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
206 while (bit < (sb->
s_blocksize << 3) && block_count > 0) {
215 }
while (block_count > 0);
218 udf_add_free_space(sb, partition, -alloc_count);
223 static int udf_bitmap_new_block(
struct super_block *sb,
229 int end_goal, nr_groups, bitmap_nr,
i;
230 struct buffer_head *bh =
NULL;
238 if (goal >= sbi->
s_partmaps[partition].s_partition_len)
246 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
250 ptr =
memscan((
char *)bh->b_data + group_start, 0xFF,
253 if ((ptr - ((
char *)bh->b_data)) < sb->
s_blocksize) {
258 end_goal = (bit + 63) & ~63;
263 ptr =
memscan((
char *)bh->b_data + (bit >> 3), 0xFF,
265 newbit = (ptr - ((
char *)bh->b_data)) << 3;
279 for (i = 0; i < (nr_groups * 2); i++) {
281 if (block_group >= nr_groups)
285 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
290 ptr =
memscan((
char *)bh->b_data + group_start, 0xFF,
292 if ((ptr - ((
char *)bh->b_data)) < sb->
s_blocksize) {
293 bit = (ptr - ((
char *)bh->b_data)) << 3;
304 if (i >= (nr_groups * 2)) {
320 while (i < 7 && bit > (group_start << 3) &&
331 udf_debug(
"bit already cleared for block %d\n", bit);
337 udf_add_free_space(sb, partition, -1);
348 static void udf_table_free_blocks(
struct super_block *sb,
375 iinfo = UDF_I(table);
384 epos.bh = oepos.bh =
NULL;
387 (etype =
udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
388 if (((eloc.logicalBlockNum +
390 if ((0x3FFFFFFF - elen) <
396 elen = (etype << 30) |
399 elen = (etype << 30) |
406 }
else if (eloc.logicalBlockNum == (end + 1)) {
407 if ((0x3FFFFFFF - elen) <
409 uint32_t tmp = ((0x3FFFFFFF - elen) >>
413 eloc.logicalBlockNum -=
tmp;
414 elen = (etype << 30) |
417 eloc.logicalBlockNum =
start;
418 elen = (etype << 30) |
427 if (epos.bh != oepos.bh) {
429 oepos.block = epos.block;
435 oepos.offset = epos.offset;
458 eloc.logicalBlockNum =
start;
465 adsize =
sizeof(
struct long_ad);
472 if (epos.offset + (2 * adsize) > sb->
s_blocksize) {
473 unsigned char *sptr, *dptr;
480 epos.block.logicalBlockNum = eloc.logicalBlockNum;
481 eloc.logicalBlockNum++;
485 udf_get_lb_pblock(sb, &epos.block, 0));
494 loffset = epos.offset;
498 dptr = epos.bh->b_data +
500 memcpy(dptr, sptr, adsize);
504 loffset = epos.offset + adsize;
507 sptr = oepos.bh->b_data + epos.offset;
516 mark_inode_dirty(table);
522 3, 1, epos.block.logicalBlockNum,
526 2, 1, epos.block.logicalBlockNum,
544 cpu_to_lelb(epos.block);
551 mark_inode_dirty(table);
561 mark_inode_dirty(table);
579 static int udf_table_prealloc_blocks(
struct super_block *sb,
591 if (first_block >= sbi->
s_partmaps[partition].s_partition_len)
594 iinfo = UDF_I(table);
598 adsize =
sizeof(
struct long_ad);
606 eloc.logicalBlockNum = 0xFFFFFFFF;
608 while (first_block != eloc.logicalBlockNum &&
609 (etype =
udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
610 udf_debug(
"eloc=%d, elen=%d, first_block=%d\n",
611 eloc.logicalBlockNum, elen, first_block);
615 if (first_block == eloc.logicalBlockNum) {
616 epos.offset -= adsize;
619 if (alloc_count > block_count) {
621 eloc.logicalBlockNum += alloc_count;
624 (etype << 30) | elen, 1);
627 (etype << 30) | elen);
635 udf_add_free_space(sb, partition, -alloc_count);
640 static int udf_table_new_block(
struct super_block *sb,
645 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
658 adsize =
sizeof(
struct long_ad);
663 if (goal >= sbi->
s_partmaps[partition].s_partition_len)
673 epos.bh = goal_epos.bh =
NULL;
676 (etype =
udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
677 if (goal >= eloc.logicalBlockNum) {
678 if (goal < eloc.logicalBlockNum +
682 nspread = goal - eloc.logicalBlockNum -
685 nspread = eloc.logicalBlockNum - goal;
688 if (nspread < spread) {
690 if (goal_epos.bh != epos.bh) {
691 brelse(goal_epos.bh);
692 goal_epos.bh = epos.bh;
693 get_bh(goal_epos.bh);
695 goal_epos.block = epos.block;
696 goal_epos.offset = epos.offset - adsize;
698 goal_elen = (etype << 30) | elen;
704 if (spread == 0xFFFFFFFF) {
705 brelse(goal_epos.bh);
715 newblock = goal_eloc.logicalBlockNum;
716 goal_eloc.logicalBlockNum++;
723 brelse(goal_epos.bh);
725 udf_add_free_space(sb, partition, -1);
741 bloc, offset, count);
744 bloc, offset, count);
747 bloc, offset, count);
750 bloc, offset, count);
768 allocated = udf_bitmap_prealloc_blocks(sb,
770 partition, first_block,
773 allocated = udf_table_prealloc_blocks(sb,
775 partition, first_block,
778 allocated = udf_bitmap_prealloc_blocks(sb,
780 partition, first_block,
783 allocated = udf_table_prealloc_blocks(sb,
785 partition, first_block,
790 if (inode && allocated > 0)
803 block = udf_bitmap_new_block(sb,
805 partition, goal, err);
807 block = udf_table_new_block(sb,
809 partition, goal, err);
811 block = udf_bitmap_new_block(sb,
813 partition, goal, err);
815 block = udf_table_new_block(sb,
817 partition, goal, err);