5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hpesjro.fc.hp.com
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1999-2001 Ben Fennema
19 * (C) 1999 Stelias Computing Inc
23 * 02/24/99 blf Created.
29 #include <linux/quotaops.h>
30 #include <linux/buffer_head.h>
31 #include <linux/bitops.h>
36 #define udf_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
37 #define udf_set_bit(nr,addr) ext2_set_bit(nr,addr)
38 #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
39 #define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size)
40 #define udf_find_next_one_bit(addr, size, offset) find_next_one_bit(addr, size, offset)
42 #define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
43 #define leNUM_to_cpup(x,y) xleNUM_to_cpup(x,y)
44 #define xleNUM_to_cpup(x,y) (le ## x ## _to_cpup(y))
45 #define uintBPL_t uint(BITS_PER_LONG)
46 #define uint(x) xuint(x)
47 #define xuint(x) __le ## x
49 extern inline int find_next_one_bit (void * addr
, int size
, int offset
)
51 uintBPL_t
* p
= ((uintBPL_t
*) addr
) + (offset
/ BITS_PER_LONG
);
52 int result
= offset
& ~(BITS_PER_LONG
-1);
58 offset
&= (BITS_PER_LONG
-1);
61 tmp
= leBPL_to_cpup(p
++);
62 tmp
&= ~0UL << offset
;
63 if (size
< BITS_PER_LONG
)
67 size
-= BITS_PER_LONG
;
68 result
+= BITS_PER_LONG
;
70 while (size
& ~(BITS_PER_LONG
-1))
72 if ((tmp
= leBPL_to_cpup(p
++)))
74 result
+= BITS_PER_LONG
;
75 size
-= BITS_PER_LONG
;
79 tmp
= leBPL_to_cpup(p
);
81 tmp
&= ~0UL >> (BITS_PER_LONG
-size
);
83 return result
+ ffz(~tmp
);
86 #define find_first_one_bit(addr, size)\
87 find_next_one_bit((addr), (size), 0)
89 static int read_block_bitmap(struct super_block
* sb
,
90 struct udf_bitmap
*bitmap
, unsigned int block
, unsigned long bitmap_nr
)
92 struct buffer_head
*bh
= NULL
;
96 loc
.logicalBlockNum
= bitmap
->s_extPosition
;
97 loc
.partitionReferenceNum
= UDF_SB_PARTITION(sb
);
99 bh
= udf_tread(sb
, udf_get_lb_pblock(sb
, loc
, block
));
104 bitmap
->s_block_bitmap
[bitmap_nr
] = bh
;
108 static int __load_block_bitmap(struct super_block
* sb
,
109 struct udf_bitmap
*bitmap
, unsigned int block_group
)
112 int nr_groups
= bitmap
->s_nr_groups
;
114 if (block_group
>= nr_groups
)
116 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group
, nr_groups
);
119 if (bitmap
->s_block_bitmap
[block_group
])
123 retval
= read_block_bitmap(sb
, bitmap
, block_group
, block_group
);
130 static inline int load_block_bitmap(struct super_block
* sb
,
131 struct udf_bitmap
*bitmap
, unsigned int block_group
)
135 slot
= __load_block_bitmap(sb
, bitmap
, block_group
);
140 if (!bitmap
->s_block_bitmap
[slot
])
146 static void udf_bitmap_free_blocks(struct super_block
* sb
,
147 struct inode
* inode
,
148 struct udf_bitmap
*bitmap
,
149 kernel_lb_addr bloc
, uint32_t offset
, uint32_t count
)
151 struct udf_sb_info
*sbi
= UDF_SB(sb
);
152 struct buffer_head
* bh
= NULL
;
154 unsigned long block_group
;
158 unsigned long overflow
;
160 down(&sbi
->s_alloc_sem
);
161 if (bloc
.logicalBlockNum
< 0 ||
162 (bloc
.logicalBlockNum
+ count
) > UDF_SB_PARTLEN(sb
, bloc
.partitionReferenceNum
))
164 udf_debug("%d < %d || %d + %d > %d\n",
165 bloc
.logicalBlockNum
, 0, bloc
.logicalBlockNum
, count
,
166 UDF_SB_PARTLEN(sb
, bloc
.partitionReferenceNum
));
170 block
= bloc
.logicalBlockNum
+ offset
+ (sizeof(struct spaceBitmapDesc
) << 3);
174 block_group
= block
>> (sb
->s_blocksize_bits
+ 3);
175 bit
= block
% (sb
->s_blocksize
<< 3);
178 * Check to see if we are freeing blocks across a group boundary.
180 if (bit
+ count
> (sb
->s_blocksize
<< 3))
182 overflow
= bit
+ count
- (sb
->s_blocksize
<< 3);
185 bitmap_nr
= load_block_bitmap(sb
, bitmap
, block_group
);
189 bh
= bitmap
->s_block_bitmap
[bitmap_nr
];
190 for (i
=0; i
< count
; i
++)
192 if (udf_set_bit(bit
+ i
, bh
->b_data
))
194 udf_debug("bit %ld already set\n", bit
+ i
);
195 udf_debug("byte=%2x\n", ((char *)bh
->b_data
)[(bit
+ i
) >> 3]);
200 DQUOT_FREE_BLOCK(inode
, 1);
201 if (UDF_SB_LVIDBH(sb
))
203 UDF_SB_LVID(sb
)->freeSpaceTable
[UDF_SB_PARTITION(sb
)] =
204 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb
)->freeSpaceTable
[UDF_SB_PARTITION(sb
)])+1);
208 mark_buffer_dirty(bh
);
217 if (UDF_SB_LVIDBH(sb
))
218 mark_buffer_dirty(UDF_SB_LVIDBH(sb
));
219 up(&sbi
->s_alloc_sem
);
223 static int udf_bitmap_prealloc_blocks(struct super_block
* sb
,
224 struct inode
* inode
,
225 struct udf_bitmap
*bitmap
, uint16_t partition
, uint32_t first_block
,
226 uint32_t block_count
)
228 struct udf_sb_info
*sbi
= UDF_SB(sb
);
230 int bit
, block
, block_group
, group_start
;
231 int nr_groups
, bitmap_nr
;
232 struct buffer_head
*bh
;
234 down(&sbi
->s_alloc_sem
);
235 if (first_block
< 0 || first_block
>= UDF_SB_PARTLEN(sb
, partition
))
238 if (first_block
+ block_count
> UDF_SB_PARTLEN(sb
, partition
))
239 block_count
= UDF_SB_PARTLEN(sb
, partition
) - first_block
;
242 nr_groups
= (UDF_SB_PARTLEN(sb
, partition
) +
243 (sizeof(struct spaceBitmapDesc
) << 3) + (sb
->s_blocksize
* 8) - 1) / (sb
->s_blocksize
* 8);
244 block
= first_block
+ (sizeof(struct spaceBitmapDesc
) << 3);
245 block_group
= block
>> (sb
->s_blocksize_bits
+ 3);
246 group_start
= block_group
? 0 : sizeof(struct spaceBitmapDesc
);
248 bitmap_nr
= load_block_bitmap(sb
, bitmap
, block_group
);
251 bh
= bitmap
->s_block_bitmap
[bitmap_nr
];
253 bit
= block
% (sb
->s_blocksize
<< 3);
255 while (bit
< (sb
->s_blocksize
<< 3) && block_count
> 0)
257 if (!udf_test_bit(bit
, bh
->b_data
))
259 else if (DQUOT_PREALLOC_BLOCK(inode
, 1))
261 else if (!udf_clear_bit(bit
, bh
->b_data
))
263 udf_debug("bit already cleared for block %d\n", bit
);
264 DQUOT_FREE_BLOCK(inode
, 1);
272 mark_buffer_dirty(bh
);
276 if (UDF_SB_LVIDBH(sb
))
278 UDF_SB_LVID(sb
)->freeSpaceTable
[partition
] =
279 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb
)->freeSpaceTable
[partition
])-alloc_count
);
280 mark_buffer_dirty(UDF_SB_LVIDBH(sb
));
283 up(&sbi
->s_alloc_sem
);
287 static int udf_bitmap_new_block(struct super_block
* sb
,
288 struct inode
* inode
,
289 struct udf_bitmap
*bitmap
, uint16_t partition
, uint32_t goal
, int *err
)
291 struct udf_sb_info
*sbi
= UDF_SB(sb
);
292 int newbit
, bit
=0, block
, block_group
, group_start
;
293 int end_goal
, nr_groups
, bitmap_nr
, i
;
294 struct buffer_head
*bh
= NULL
;
299 down(&sbi
->s_alloc_sem
);
302 if (goal
< 0 || goal
>= UDF_SB_PARTLEN(sb
, partition
))
305 nr_groups
= bitmap
->s_nr_groups
;
306 block
= goal
+ (sizeof(struct spaceBitmapDesc
) << 3);
307 block_group
= block
>> (sb
->s_blocksize_bits
+ 3);
308 group_start
= block_group
? 0 : sizeof(struct spaceBitmapDesc
);
310 bitmap_nr
= load_block_bitmap(sb
, bitmap
, block_group
);
313 bh
= bitmap
->s_block_bitmap
[bitmap_nr
];
314 ptr
= memscan((char *)bh
->b_data
+ group_start
, 0xFF, sb
->s_blocksize
- group_start
);
316 if ((ptr
- ((char *)bh
->b_data
)) < sb
->s_blocksize
)
318 bit
= block
% (sb
->s_blocksize
<< 3);
320 if (udf_test_bit(bit
, bh
->b_data
))
324 end_goal
= (bit
+ 63) & ~63;
325 bit
= udf_find_next_one_bit(bh
->b_data
, end_goal
, bit
);
328 ptr
= memscan((char *)bh
->b_data
+ (bit
>> 3), 0xFF, sb
->s_blocksize
- ((bit
+ 7) >> 3));
329 newbit
= (ptr
- ((char *)bh
->b_data
)) << 3;
330 if (newbit
< sb
->s_blocksize
<< 3)
335 newbit
= udf_find_next_one_bit(bh
->b_data
, sb
->s_blocksize
<< 3, bit
);
336 if (newbit
< sb
->s_blocksize
<< 3)
343 for (i
=0; i
<(nr_groups
*2); i
++)
346 if (block_group
>= nr_groups
)
348 group_start
= block_group
? 0 : sizeof(struct spaceBitmapDesc
);
350 bitmap_nr
= load_block_bitmap(sb
, bitmap
, block_group
);
353 bh
= bitmap
->s_block_bitmap
[bitmap_nr
];
356 ptr
= memscan((char *)bh
->b_data
+ group_start
, 0xFF, sb
->s_blocksize
- group_start
);
357 if ((ptr
- ((char *)bh
->b_data
)) < sb
->s_blocksize
)
359 bit
= (ptr
- ((char *)bh
->b_data
)) << 3;
365 bit
= udf_find_next_one_bit((char *)bh
->b_data
, sb
->s_blocksize
<< 3, group_start
<< 3);
366 if (bit
< sb
->s_blocksize
<< 3)
370 if (i
>= (nr_groups
*2))
372 up(&sbi
->s_alloc_sem
);
375 if (bit
< sb
->s_blocksize
<< 3)
378 bit
= udf_find_next_one_bit(bh
->b_data
, sb
->s_blocksize
<< 3, group_start
<< 3);
379 if (bit
>= sb
->s_blocksize
<< 3)
381 up(&sbi
->s_alloc_sem
);
386 for (i
=0; i
<7 && bit
> (group_start
<< 3) && udf_test_bit(bit
- 1, bh
->b_data
); i
++, bit
--);
391 * Check quota for allocation of this block.
393 if (inode
&& DQUOT_ALLOC_BLOCK(inode
, 1))
395 up(&sbi
->s_alloc_sem
);
400 newblock
= bit
+ (block_group
<< (sb
->s_blocksize_bits
+ 3)) -
401 (sizeof(struct spaceBitmapDesc
) << 3);
403 if (!udf_clear_bit(bit
, bh
->b_data
))
405 udf_debug("bit already cleared for block %d\n", bit
);
409 mark_buffer_dirty(bh
);
411 if (UDF_SB_LVIDBH(sb
))
413 UDF_SB_LVID(sb
)->freeSpaceTable
[partition
] =
414 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb
)->freeSpaceTable
[partition
])-1);
415 mark_buffer_dirty(UDF_SB_LVIDBH(sb
));
418 up(&sbi
->s_alloc_sem
);
424 up(&sbi
->s_alloc_sem
);
428 static void udf_table_free_blocks(struct super_block
* sb
,
429 struct inode
* inode
,
430 struct inode
* table
,
431 kernel_lb_addr bloc
, uint32_t offset
, uint32_t count
)
433 struct udf_sb_info
*sbi
= UDF_SB(sb
);
435 uint32_t nextoffset
, oextoffset
, elen
;
436 kernel_lb_addr nbloc
, obloc
, eloc
;
437 struct buffer_head
*obh
, *nbh
;
441 down(&sbi
->s_alloc_sem
);
442 if (bloc
.logicalBlockNum
< 0 ||
443 (bloc
.logicalBlockNum
+ count
) > UDF_SB_PARTLEN(sb
, bloc
.partitionReferenceNum
))
445 udf_debug("%d < %d || %d + %d > %d\n",
446 bloc
.logicalBlockNum
, 0, bloc
.logicalBlockNum
, count
,
447 UDF_SB_PARTLEN(sb
, bloc
.partitionReferenceNum
));
451 /* We do this up front - There are some error conditions that could occure,
454 DQUOT_FREE_BLOCK(inode
, count
);
455 if (UDF_SB_LVIDBH(sb
))
457 UDF_SB_LVID(sb
)->freeSpaceTable
[UDF_SB_PARTITION(sb
)] =
458 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb
)->freeSpaceTable
[UDF_SB_PARTITION(sb
)])+count
);
459 mark_buffer_dirty(UDF_SB_LVIDBH(sb
));
462 start
= bloc
.logicalBlockNum
+ offset
;
463 end
= bloc
.logicalBlockNum
+ offset
+ count
- 1;
465 oextoffset
= nextoffset
= sizeof(struct unallocSpaceEntry
);
467 obloc
= nbloc
= UDF_I_LOCATION(table
);
471 while (count
&& (etype
=
472 udf_next_aext(table
, &nbloc
, &nextoffset
, &eloc
, &elen
, &nbh
, 1)) != -1)
474 if (((eloc
.logicalBlockNum
+ (elen
>> sb
->s_blocksize_bits
)) ==
477 if ((0x3FFFFFFF - elen
) < (count
<< sb
->s_blocksize_bits
))
479 count
-= ((0x3FFFFFFF - elen
) >> sb
->s_blocksize_bits
);
480 start
+= ((0x3FFFFFFF - elen
) >> sb
->s_blocksize_bits
);
481 elen
= (etype
<< 30) | (0x40000000 - sb
->s_blocksize
);
485 elen
= (etype
<< 30) |
486 (elen
+ (count
<< sb
->s_blocksize_bits
));
490 udf_write_aext(table
, obloc
, &oextoffset
, eloc
, elen
, obh
, 1);
492 else if (eloc
.logicalBlockNum
== (end
+ 1))
494 if ((0x3FFFFFFF - elen
) < (count
<< sb
->s_blocksize_bits
))
496 count
-= ((0x3FFFFFFF - elen
) >> sb
->s_blocksize_bits
);
497 end
-= ((0x3FFFFFFF - elen
) >> sb
->s_blocksize_bits
);
498 eloc
.logicalBlockNum
-=
499 ((0x3FFFFFFF - elen
) >> sb
->s_blocksize_bits
);
500 elen
= (etype
<< 30) | (0x40000000 - sb
->s_blocksize
);
504 eloc
.logicalBlockNum
= start
;
505 elen
= (etype
<< 30) |
506 (elen
+ (count
<< sb
->s_blocksize_bits
));
510 udf_write_aext(table
, obloc
, &oextoffset
, eloc
, elen
, obh
, 1);
517 udf_release_data(obh
);
518 atomic_inc(&nbh
->b_count
);
523 oextoffset
= nextoffset
;
528 /* NOTE: we CANNOT use udf_add_aext here, as it can try to allocate
529 a new block, and since we hold the super block lock already
530 very bad things would happen :)
532 We copy the behavior of udf_add_aext, but instead of
533 trying to allocate a new block close to the existing one,
534 we just steal a block from the extent we are trying to add.
536 It would be nice if the blocks were close together, but it
541 short_ad
*sad
= NULL
;
543 struct allocExtDesc
*aed
;
545 eloc
.logicalBlockNum
= start
;
546 elen
= EXT_RECORDED_ALLOCATED
|
547 (count
<< sb
->s_blocksize_bits
);
549 if (UDF_I_ALLOCTYPE(table
) == ICBTAG_FLAG_AD_SHORT
)
550 adsize
= sizeof(short_ad
);
551 else if (UDF_I_ALLOCTYPE(table
) == ICBTAG_FLAG_AD_LONG
)
552 adsize
= sizeof(long_ad
);
555 udf_release_data(obh
);
556 udf_release_data(nbh
);
560 if (nextoffset
+ (2 * adsize
) > sb
->s_blocksize
)
565 udf_release_data(obh
);
568 oextoffset
= nextoffset
;
570 /* Steal a block from the extent being free'd */
571 nbloc
.logicalBlockNum
= eloc
.logicalBlockNum
;
572 eloc
.logicalBlockNum
++;
573 elen
-= sb
->s_blocksize
;
575 if (!(nbh
= udf_tread(sb
,
576 udf_get_lb_pblock(sb
, nbloc
, 0))))
578 udf_release_data(obh
);
581 aed
= (struct allocExtDesc
*)(nbh
->b_data
);
582 aed
->previousAllocExtLocation
= cpu_to_le32(obloc
.logicalBlockNum
);
583 if (nextoffset
+ adsize
> sb
->s_blocksize
)
585 loffset
= nextoffset
;
586 aed
->lengthAllocDescs
= cpu_to_le32(adsize
);
588 sptr
= UDF_I_DATA(inode
) + nextoffset
- udf_file_entry_alloc_offset(inode
) + UDF_I_LENEATTR(inode
) - adsize
;
590 sptr
= obh
->b_data
+ nextoffset
- adsize
;
591 dptr
= nbh
->b_data
+ sizeof(struct allocExtDesc
);
592 memcpy(dptr
, sptr
, adsize
);
593 nextoffset
= sizeof(struct allocExtDesc
) + adsize
;
597 loffset
= nextoffset
+ adsize
;
598 aed
->lengthAllocDescs
= cpu_to_le32(0);
599 sptr
= (obh
)->b_data
+ nextoffset
;
600 nextoffset
= sizeof(struct allocExtDesc
);
604 aed
= (struct allocExtDesc
*)(obh
)->b_data
;
605 aed
->lengthAllocDescs
=
606 cpu_to_le32(le32_to_cpu(aed
->lengthAllocDescs
) + adsize
);
610 UDF_I_LENALLOC(table
) += adsize
;
611 mark_inode_dirty(table
);
614 if (UDF_SB_UDFREV(sb
) >= 0x0200)
615 udf_new_tag(nbh
->b_data
, TAG_IDENT_AED
, 3, 1,
616 nbloc
.logicalBlockNum
, sizeof(tag
));
618 udf_new_tag(nbh
->b_data
, TAG_IDENT_AED
, 2, 1,
619 nbloc
.logicalBlockNum
, sizeof(tag
));
620 switch (UDF_I_ALLOCTYPE(table
))
622 case ICBTAG_FLAG_AD_SHORT
:
624 sad
= (short_ad
*)sptr
;
625 sad
->extLength
= cpu_to_le32(
626 EXT_NEXT_EXTENT_ALLOCDECS
|
628 sad
->extPosition
= cpu_to_le32(nbloc
.logicalBlockNum
);
631 case ICBTAG_FLAG_AD_LONG
:
633 lad
= (long_ad
*)sptr
;
634 lad
->extLength
= cpu_to_le32(
635 EXT_NEXT_EXTENT_ALLOCDECS
|
637 lad
->extLocation
= cpu_to_lelb(nbloc
);
643 udf_update_tag(obh
->b_data
, loffset
);
644 mark_buffer_dirty(obh
);
647 mark_inode_dirty(table
);
650 if (elen
) /* It's possible that stealing the block emptied the extent */
652 udf_write_aext(table
, nbloc
, &nextoffset
, eloc
, elen
, nbh
, 1);
656 UDF_I_LENALLOC(table
) += adsize
;
657 mark_inode_dirty(table
);
661 aed
= (struct allocExtDesc
*)nbh
->b_data
;
662 aed
->lengthAllocDescs
=
663 cpu_to_le32(le32_to_cpu(aed
->lengthAllocDescs
) + adsize
);
664 udf_update_tag(nbh
->b_data
, nextoffset
);
665 mark_buffer_dirty(nbh
);
670 udf_release_data(nbh
);
671 udf_release_data(obh
);
675 up(&sbi
->s_alloc_sem
);
679 static int udf_table_prealloc_blocks(struct super_block
* sb
,
680 struct inode
* inode
,
681 struct inode
*table
, uint16_t partition
, uint32_t first_block
,
682 uint32_t block_count
)
684 struct udf_sb_info
*sbi
= UDF_SB(sb
);
686 uint32_t extoffset
, elen
, adsize
;
687 kernel_lb_addr bloc
, eloc
;
688 struct buffer_head
*bh
;
691 if (first_block
< 0 || first_block
>= UDF_SB_PARTLEN(sb
, partition
))
694 if (UDF_I_ALLOCTYPE(table
) == ICBTAG_FLAG_AD_SHORT
)
695 adsize
= sizeof(short_ad
);
696 else if (UDF_I_ALLOCTYPE(table
) == ICBTAG_FLAG_AD_LONG
)
697 adsize
= sizeof(long_ad
);
701 down(&sbi
->s_alloc_sem
);
702 extoffset
= sizeof(struct unallocSpaceEntry
);
703 bloc
= UDF_I_LOCATION(table
);
706 eloc
.logicalBlockNum
= 0xFFFFFFFF;
708 while (first_block
!= eloc
.logicalBlockNum
&& (etype
=
709 udf_next_aext(table
, &bloc
, &extoffset
, &eloc
, &elen
, &bh
, 1)) != -1)
711 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
712 eloc
.logicalBlockNum
, elen
, first_block
);
713 ; /* empty loop body */
716 if (first_block
== eloc
.logicalBlockNum
)
720 alloc_count
= (elen
>> sb
->s_blocksize_bits
);
721 if (inode
&& DQUOT_PREALLOC_BLOCK(inode
, alloc_count
> block_count
? block_count
: alloc_count
))
723 else if (alloc_count
> block_count
)
725 alloc_count
= block_count
;
726 eloc
.logicalBlockNum
+= alloc_count
;
727 elen
-= (alloc_count
<< sb
->s_blocksize_bits
);
728 udf_write_aext(table
, bloc
, &extoffset
, eloc
, (etype
<< 30) | elen
, bh
, 1);
731 udf_delete_aext(table
, bloc
, extoffset
, eloc
, (etype
<< 30) | elen
, bh
);
736 udf_release_data(bh
);
738 if (alloc_count
&& UDF_SB_LVIDBH(sb
))
740 UDF_SB_LVID(sb
)->freeSpaceTable
[partition
] =
741 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb
)->freeSpaceTable
[partition
])-alloc_count
);
742 mark_buffer_dirty(UDF_SB_LVIDBH(sb
));
745 up(&sbi
->s_alloc_sem
);
749 static int udf_table_new_block(struct super_block
* sb
,
750 struct inode
* inode
,
751 struct inode
*table
, uint16_t partition
, uint32_t goal
, int *err
)
753 struct udf_sb_info
*sbi
= UDF_SB(sb
);
754 uint32_t spread
= 0xFFFFFFFF, nspread
= 0xFFFFFFFF;
755 uint32_t newblock
= 0, adsize
;
756 uint32_t extoffset
, goal_extoffset
, elen
, goal_elen
= 0;
757 kernel_lb_addr bloc
, goal_bloc
, eloc
, goal_eloc
;
758 struct buffer_head
*bh
, *goal_bh
;
763 if (UDF_I_ALLOCTYPE(table
) == ICBTAG_FLAG_AD_SHORT
)
764 adsize
= sizeof(short_ad
);
765 else if (UDF_I_ALLOCTYPE(table
) == ICBTAG_FLAG_AD_LONG
)
766 adsize
= sizeof(long_ad
);
770 down(&sbi
->s_alloc_sem
);
771 if (goal
< 0 || goal
>= UDF_SB_PARTLEN(sb
, partition
))
774 /* We search for the closest matching block to goal. If we find a exact hit,
775 we stop. Otherwise we keep going till we run out of extents.
776 We store the buffer_head, bloc, and extoffset of the current closest
777 match and use that when we are done.
780 extoffset
= sizeof(struct unallocSpaceEntry
);
781 bloc
= UDF_I_LOCATION(table
);
785 while (spread
&& (etype
=
786 udf_next_aext(table
, &bloc
, &extoffset
, &eloc
, &elen
, &bh
, 1)) != -1)
788 if (goal
>= eloc
.logicalBlockNum
)
790 if (goal
< eloc
.logicalBlockNum
+ (elen
>> sb
->s_blocksize_bits
))
793 nspread
= goal
- eloc
.logicalBlockNum
-
794 (elen
>> sb
->s_blocksize_bits
);
797 nspread
= eloc
.logicalBlockNum
- goal
;
799 if (nspread
< spread
)
804 udf_release_data(goal_bh
);
806 atomic_inc(&goal_bh
->b_count
);
809 goal_extoffset
= extoffset
- adsize
;
811 goal_elen
= (etype
<< 30) | elen
;
815 udf_release_data(bh
);
817 if (spread
== 0xFFFFFFFF)
819 udf_release_data(goal_bh
);
820 up(&sbi
->s_alloc_sem
);
824 /* Only allocate blocks from the beginning of the extent.
825 That way, we only delete (empty) extents, never have to insert an
826 extent because of splitting */
827 /* This works, but very poorly.... */
829 newblock
= goal_eloc
.logicalBlockNum
;
830 goal_eloc
.logicalBlockNum
++;
831 goal_elen
-= sb
->s_blocksize
;
833 if (inode
&& DQUOT_ALLOC_BLOCK(inode
, 1))
835 udf_release_data(goal_bh
);
836 up(&sbi
->s_alloc_sem
);
842 udf_write_aext(table
, goal_bloc
, &goal_extoffset
, goal_eloc
, goal_elen
, goal_bh
, 1);
844 udf_delete_aext(table
, goal_bloc
, goal_extoffset
, goal_eloc
, goal_elen
, goal_bh
);
845 udf_release_data(goal_bh
);
847 if (UDF_SB_LVIDBH(sb
))
849 UDF_SB_LVID(sb
)->freeSpaceTable
[partition
] =
850 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb
)->freeSpaceTable
[partition
])-1);
851 mark_buffer_dirty(UDF_SB_LVIDBH(sb
));
855 up(&sbi
->s_alloc_sem
);
860 inline void udf_free_blocks(struct super_block
* sb
,
861 struct inode
* inode
,
862 kernel_lb_addr bloc
, uint32_t offset
, uint32_t count
)
864 uint16_t partition
= bloc
.partitionReferenceNum
;
866 if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_UNALLOC_BITMAP
)
868 return udf_bitmap_free_blocks(sb
, inode
,
869 UDF_SB_PARTMAPS(sb
)[partition
].s_uspace
.s_bitmap
,
870 bloc
, offset
, count
);
872 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_UNALLOC_TABLE
)
874 return udf_table_free_blocks(sb
, inode
,
875 UDF_SB_PARTMAPS(sb
)[partition
].s_uspace
.s_table
,
876 bloc
, offset
, count
);
878 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_FREED_BITMAP
)
880 return udf_bitmap_free_blocks(sb
, inode
,
881 UDF_SB_PARTMAPS(sb
)[partition
].s_fspace
.s_bitmap
,
882 bloc
, offset
, count
);
884 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_FREED_TABLE
)
886 return udf_table_free_blocks(sb
, inode
,
887 UDF_SB_PARTMAPS(sb
)[partition
].s_fspace
.s_table
,
888 bloc
, offset
, count
);
894 inline int udf_prealloc_blocks(struct super_block
* sb
,
895 struct inode
* inode
,
896 uint16_t partition
, uint32_t first_block
, uint32_t block_count
)
898 if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_UNALLOC_BITMAP
)
900 return udf_bitmap_prealloc_blocks(sb
, inode
,
901 UDF_SB_PARTMAPS(sb
)[partition
].s_uspace
.s_bitmap
,
902 partition
, first_block
, block_count
);
904 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_UNALLOC_TABLE
)
906 return udf_table_prealloc_blocks(sb
, inode
,
907 UDF_SB_PARTMAPS(sb
)[partition
].s_uspace
.s_table
,
908 partition
, first_block
, block_count
);
910 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_FREED_BITMAP
)
912 return udf_bitmap_prealloc_blocks(sb
, inode
,
913 UDF_SB_PARTMAPS(sb
)[partition
].s_fspace
.s_bitmap
,
914 partition
, first_block
, block_count
);
916 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_FREED_TABLE
)
918 return udf_table_prealloc_blocks(sb
, inode
,
919 UDF_SB_PARTMAPS(sb
)[partition
].s_fspace
.s_table
,
920 partition
, first_block
, block_count
);
926 inline int udf_new_block(struct super_block
* sb
,
927 struct inode
* inode
,
928 uint16_t partition
, uint32_t goal
, int *err
)
930 if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_UNALLOC_BITMAP
)
932 return udf_bitmap_new_block(sb
, inode
,
933 UDF_SB_PARTMAPS(sb
)[partition
].s_uspace
.s_bitmap
,
934 partition
, goal
, err
);
936 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_UNALLOC_TABLE
)
938 return udf_table_new_block(sb
, inode
,
939 UDF_SB_PARTMAPS(sb
)[partition
].s_uspace
.s_table
,
940 partition
, goal
, err
);
942 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_FREED_BITMAP
)
944 return udf_bitmap_new_block(sb
, inode
,
945 UDF_SB_PARTMAPS(sb
)[partition
].s_fspace
.s_bitmap
,
946 partition
, goal
, err
);
948 else if (UDF_SB_PARTFLAGS(sb
, partition
) & UDF_PART_FLAG_FREED_TABLE
)
950 return udf_table_new_block(sb
, inode
,
951 UDF_SB_PARTMAPS(sb
)[partition
].s_fspace
.s_table
,
952 partition
, goal
, err
);