MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / udf / balloc.c
blobea521f846d97a4e3109b82feb8bb83f6d9201a1c
1 /*
2 * balloc.c
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1999-2001 Ben Fennema
14 * (C) 1999 Stelias Computing Inc
16 * HISTORY
18 * 02/24/99 blf Created.
22 #include "udfdecl.h"
24 #include <linux/quotaops.h>
25 #include <linux/buffer_head.h>
26 #include <linux/bitops.h>
28 #include "udf_i.h"
29 #include "udf_sb.h"
31 #define udf_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
32 #define udf_set_bit(nr,addr) ext2_set_bit(nr,addr)
33 #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
34 #define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size)
35 #define udf_find_next_one_bit(addr, size, offset) find_next_one_bit(addr, size, offset)
37 #define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
38 #define leNUM_to_cpup(x,y) xleNUM_to_cpup(x,y)
39 #define xleNUM_to_cpup(x,y) (le ## x ## _to_cpup(y))
40 #define uintBPL_t uint(BITS_PER_LONG)
41 #define uint(x) xuint(x)
42 #define xuint(x) __le ## x
44 static inline int find_next_one_bit (void * addr, int size, int offset)
46 uintBPL_t * p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
47 int result = offset & ~(BITS_PER_LONG-1);
48 unsigned long tmp;
50 if (offset >= size)
51 return size;
52 size -= result;
53 offset &= (BITS_PER_LONG-1);
54 if (offset)
56 tmp = leBPL_to_cpup(p++);
57 tmp &= ~0UL << offset;
58 if (size < BITS_PER_LONG)
59 goto found_first;
60 if (tmp)
61 goto found_middle;
62 size -= BITS_PER_LONG;
63 result += BITS_PER_LONG;
65 while (size & ~(BITS_PER_LONG-1))
67 if ((tmp = leBPL_to_cpup(p++)))
68 goto found_middle;
69 result += BITS_PER_LONG;
70 size -= BITS_PER_LONG;
72 if (!size)
73 return result;
74 tmp = leBPL_to_cpup(p);
75 found_first:
76 tmp &= ~0UL >> (BITS_PER_LONG-size);
77 found_middle:
78 return result + ffz(~tmp);
81 #define find_first_one_bit(addr, size)\
82 find_next_one_bit((addr), (size), 0)
84 static int read_block_bitmap(struct super_block * sb,
85 struct udf_bitmap *bitmap, unsigned int block, unsigned long bitmap_nr)
87 struct buffer_head *bh = NULL;
88 int retval = 0;
89 kernel_lb_addr loc;
91 loc.logicalBlockNum = bitmap->s_extPosition;
92 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
94 bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block));
95 if (!bh)
97 retval = -EIO;
99 bitmap->s_block_bitmap[bitmap_nr] = bh;
100 return retval;
103 static int __load_block_bitmap(struct super_block * sb,
104 struct udf_bitmap *bitmap, unsigned int block_group)
106 int retval = 0;
107 int nr_groups = bitmap->s_nr_groups;
109 if (block_group >= nr_groups)
111 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group, nr_groups);
114 if (bitmap->s_block_bitmap[block_group])
115 return block_group;
116 else
118 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
119 if (retval < 0)
120 return retval;
121 return block_group;
125 static inline int load_block_bitmap(struct super_block * sb,
126 struct udf_bitmap *bitmap, unsigned int block_group)
128 int slot;
130 slot = __load_block_bitmap(sb, bitmap, block_group);
132 if (slot < 0)
133 return slot;
135 if (!bitmap->s_block_bitmap[slot])
136 return -EIO;
138 return slot;
141 static void udf_bitmap_free_blocks(struct super_block * sb,
142 struct inode * inode,
143 struct udf_bitmap *bitmap,
144 kernel_lb_addr bloc, uint32_t offset, uint32_t count)
146 struct udf_sb_info *sbi = UDF_SB(sb);
147 struct buffer_head * bh = NULL;
148 unsigned long block;
149 unsigned long block_group;
150 unsigned long bit;
151 unsigned long i;
152 int bitmap_nr;
153 unsigned long overflow;
155 mutex_lock(&sbi->s_alloc_mutex);
156 if (bloc.logicalBlockNum < 0 ||
157 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum))
159 udf_debug("%d < %d || %d + %d > %d\n",
160 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
161 UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum));
162 goto error_return;
165 block = bloc.logicalBlockNum + offset + (sizeof(struct spaceBitmapDesc) << 3);
167 do_more:
168 overflow = 0;
169 block_group = block >> (sb->s_blocksize_bits + 3);
170 bit = block % (sb->s_blocksize << 3);
173 * Check to see if we are freeing blocks across a group boundary.
175 if (bit + count > (sb->s_blocksize << 3))
177 overflow = bit + count - (sb->s_blocksize << 3);
178 count -= overflow;
180 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
181 if (bitmap_nr < 0)
182 goto error_return;
184 bh = bitmap->s_block_bitmap[bitmap_nr];
185 for (i=0; i < count; i++)
187 if (udf_set_bit(bit + i, bh->b_data))
189 udf_debug("bit %ld already set\n", bit + i);
190 udf_debug("byte=%2x\n", ((char *)bh->b_data)[(bit + i) >> 3]);
192 else
194 if (inode)
195 DQUOT_FREE_BLOCK(inode, 1);
196 if (UDF_SB_LVIDBH(sb))
198 UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)] =
199 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)])+1);
203 mark_buffer_dirty(bh);
204 if (overflow)
206 block += count;
207 count = overflow;
208 goto do_more;
210 error_return:
211 sb->s_dirt = 1;
212 if (UDF_SB_LVIDBH(sb))
213 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
214 mutex_unlock(&sbi->s_alloc_mutex);
215 return;
218 static int udf_bitmap_prealloc_blocks(struct super_block * sb,
219 struct inode * inode,
220 struct udf_bitmap *bitmap, uint16_t partition, uint32_t first_block,
221 uint32_t block_count)
223 struct udf_sb_info *sbi = UDF_SB(sb);
224 int alloc_count = 0;
225 int bit, block, block_group, group_start;
226 int nr_groups, bitmap_nr;
227 struct buffer_head *bh;
229 mutex_lock(&sbi->s_alloc_mutex);
230 if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
231 goto out;
233 if (first_block + block_count > UDF_SB_PARTLEN(sb, partition))
234 block_count = UDF_SB_PARTLEN(sb, partition) - first_block;
236 repeat:
237 nr_groups = (UDF_SB_PARTLEN(sb, partition) +
238 (sizeof(struct spaceBitmapDesc) << 3) + (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
239 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
240 block_group = block >> (sb->s_blocksize_bits + 3);
241 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
243 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
244 if (bitmap_nr < 0)
245 goto out;
246 bh = bitmap->s_block_bitmap[bitmap_nr];
248 bit = block % (sb->s_blocksize << 3);
250 while (bit < (sb->s_blocksize << 3) && block_count > 0)
252 if (!udf_test_bit(bit, bh->b_data))
253 goto out;
254 else if (DQUOT_PREALLOC_BLOCK(inode, 1))
255 goto out;
256 else if (!udf_clear_bit(bit, bh->b_data))
258 udf_debug("bit already cleared for block %d\n", bit);
259 DQUOT_FREE_BLOCK(inode, 1);
260 goto out;
262 block_count --;
263 alloc_count ++;
264 bit ++;
265 block ++;
267 mark_buffer_dirty(bh);
268 if (block_count > 0)
269 goto repeat;
270 out:
271 if (UDF_SB_LVIDBH(sb))
273 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
274 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-alloc_count);
275 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
277 sb->s_dirt = 1;
278 mutex_unlock(&sbi->s_alloc_mutex);
279 return alloc_count;
282 static int udf_bitmap_new_block(struct super_block * sb,
283 struct inode * inode,
284 struct udf_bitmap *bitmap, uint16_t partition, uint32_t goal, int *err)
286 struct udf_sb_info *sbi = UDF_SB(sb);
287 int newbit, bit=0, block, block_group, group_start;
288 int end_goal, nr_groups, bitmap_nr, i;
289 struct buffer_head *bh = NULL;
290 char *ptr;
291 int newblock = 0;
293 *err = -ENOSPC;
294 mutex_lock(&sbi->s_alloc_mutex);
296 repeat:
297 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
298 goal = 0;
300 nr_groups = bitmap->s_nr_groups;
301 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
302 block_group = block >> (sb->s_blocksize_bits + 3);
303 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
305 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
306 if (bitmap_nr < 0)
307 goto error_return;
308 bh = bitmap->s_block_bitmap[bitmap_nr];
309 ptr = memscan((char *)bh->b_data + group_start, 0xFF, sb->s_blocksize - group_start);
311 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize)
313 bit = block % (sb->s_blocksize << 3);
315 if (udf_test_bit(bit, bh->b_data))
317 goto got_block;
319 end_goal = (bit + 63) & ~63;
320 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
321 if (bit < end_goal)
322 goto got_block;
323 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, sb->s_blocksize - ((bit + 7) >> 3));
324 newbit = (ptr - ((char *)bh->b_data)) << 3;
325 if (newbit < sb->s_blocksize << 3)
327 bit = newbit;
328 goto search_back;
330 newbit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, bit);
331 if (newbit < sb->s_blocksize << 3)
333 bit = newbit;
334 goto got_block;
338 for (i=0; i<(nr_groups*2); i++)
340 block_group ++;
341 if (block_group >= nr_groups)
342 block_group = 0;
343 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
345 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
346 if (bitmap_nr < 0)
347 goto error_return;
348 bh = bitmap->s_block_bitmap[bitmap_nr];
349 if (i < nr_groups)
351 ptr = memscan((char *)bh->b_data + group_start, 0xFF, sb->s_blocksize - group_start);
352 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize)
354 bit = (ptr - ((char *)bh->b_data)) << 3;
355 break;
358 else
360 bit = udf_find_next_one_bit((char *)bh->b_data, sb->s_blocksize << 3, group_start << 3);
361 if (bit < sb->s_blocksize << 3)
362 break;
365 if (i >= (nr_groups*2))
367 mutex_unlock(&sbi->s_alloc_mutex);
368 return newblock;
370 if (bit < sb->s_blocksize << 3)
371 goto search_back;
372 else
373 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, group_start << 3);
374 if (bit >= sb->s_blocksize << 3)
376 mutex_unlock(&sbi->s_alloc_mutex);
377 return 0;
380 search_back:
381 for (i=0; i<7 && bit > (group_start << 3) && udf_test_bit(bit - 1, bh->b_data); i++, bit--);
383 got_block:
386 * Check quota for allocation of this block.
388 if (inode && DQUOT_ALLOC_BLOCK(inode, 1))
390 mutex_unlock(&sbi->s_alloc_mutex);
391 *err = -EDQUOT;
392 return 0;
395 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
396 (sizeof(struct spaceBitmapDesc) << 3);
398 if (!udf_clear_bit(bit, bh->b_data))
400 udf_debug("bit already cleared for block %d\n", bit);
401 goto repeat;
404 mark_buffer_dirty(bh);
406 if (UDF_SB_LVIDBH(sb))
408 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
409 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-1);
410 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
412 sb->s_dirt = 1;
413 mutex_unlock(&sbi->s_alloc_mutex);
414 *err = 0;
415 return newblock;
417 error_return:
418 *err = -EIO;
419 mutex_unlock(&sbi->s_alloc_mutex);
420 return 0;
423 static void udf_table_free_blocks(struct super_block * sb,
424 struct inode * inode,
425 struct inode * table,
426 kernel_lb_addr bloc, uint32_t offset, uint32_t count)
428 struct udf_sb_info *sbi = UDF_SB(sb);
429 uint32_t start, end;
430 uint32_t nextoffset, oextoffset, elen;
431 kernel_lb_addr nbloc, obloc, eloc;
432 struct buffer_head *obh, *nbh;
433 int8_t etype;
434 int i;
436 mutex_lock(&sbi->s_alloc_mutex);
437 if (bloc.logicalBlockNum < 0 ||
438 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum))
440 udf_debug("%d < %d || %d + %d > %d\n",
441 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
442 UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum));
443 goto error_return;
446 /* We do this up front - There are some error conditions that could occure,
447 but.. oh well */
448 if (inode)
449 DQUOT_FREE_BLOCK(inode, count);
450 if (UDF_SB_LVIDBH(sb))
452 UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)] =
453 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)])+count);
454 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
457 start = bloc.logicalBlockNum + offset;
458 end = bloc.logicalBlockNum + offset + count - 1;
460 oextoffset = nextoffset = sizeof(struct unallocSpaceEntry);
461 elen = 0;
462 obloc = nbloc = UDF_I_LOCATION(table);
464 obh = nbh = NULL;
466 while (count && (etype =
467 udf_next_aext(table, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1)) != -1)
469 if (((eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits)) ==
470 start))
472 if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits))
474 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
475 start += ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
476 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
478 else
480 elen = (etype << 30) |
481 (elen + (count << sb->s_blocksize_bits));
482 start += count;
483 count = 0;
485 udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
487 else if (eloc.logicalBlockNum == (end + 1))
489 if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits))
491 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
492 end -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
493 eloc.logicalBlockNum -=
494 ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
495 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
497 else
499 eloc.logicalBlockNum = start;
500 elen = (etype << 30) |
501 (elen + (count << sb->s_blocksize_bits));
502 end -= count;
503 count = 0;
505 udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
508 if (nbh != obh)
510 i = -1;
511 obloc = nbloc;
512 udf_release_data(obh);
513 atomic_inc(&nbh->b_count);
514 obh = nbh;
515 oextoffset = 0;
517 else
518 oextoffset = nextoffset;
521 if (count)
523 /* NOTE: we CANNOT use udf_add_aext here, as it can try to allocate
524 a new block, and since we hold the super block lock already
525 very bad things would happen :)
527 We copy the behavior of udf_add_aext, but instead of
528 trying to allocate a new block close to the existing one,
529 we just steal a block from the extent we are trying to add.
531 It would be nice if the blocks were close together, but it
532 isn't required.
535 int adsize;
536 short_ad *sad = NULL;
537 long_ad *lad = NULL;
538 struct allocExtDesc *aed;
540 eloc.logicalBlockNum = start;
541 elen = EXT_RECORDED_ALLOCATED |
542 (count << sb->s_blocksize_bits);
544 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
545 adsize = sizeof(short_ad);
546 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
547 adsize = sizeof(long_ad);
548 else
550 udf_release_data(obh);
551 udf_release_data(nbh);
552 goto error_return;
555 if (nextoffset + (2 * adsize) > sb->s_blocksize)
557 char *sptr, *dptr;
558 int loffset;
560 udf_release_data(obh);
561 obh = nbh;
562 obloc = nbloc;
563 oextoffset = nextoffset;
565 /* Steal a block from the extent being free'd */
566 nbloc.logicalBlockNum = eloc.logicalBlockNum;
567 eloc.logicalBlockNum ++;
568 elen -= sb->s_blocksize;
570 if (!(nbh = udf_tread(sb,
571 udf_get_lb_pblock(sb, nbloc, 0))))
573 udf_release_data(obh);
574 goto error_return;
576 aed = (struct allocExtDesc *)(nbh->b_data);
577 aed->previousAllocExtLocation = cpu_to_le32(obloc.logicalBlockNum);
578 if (nextoffset + adsize > sb->s_blocksize)
580 loffset = nextoffset;
581 aed->lengthAllocDescs = cpu_to_le32(adsize);
582 sptr = UDF_I_DATA(inode) + nextoffset -
583 udf_file_entry_alloc_offset(inode) +
584 UDF_I_LENEATTR(inode) - adsize;
585 dptr = nbh->b_data + sizeof(struct allocExtDesc);
586 memcpy(dptr, sptr, adsize);
587 nextoffset = sizeof(struct allocExtDesc) + adsize;
589 else
591 loffset = nextoffset + adsize;
592 aed->lengthAllocDescs = cpu_to_le32(0);
593 sptr = (obh)->b_data + nextoffset;
594 nextoffset = sizeof(struct allocExtDesc);
596 if (obh)
598 aed = (struct allocExtDesc *)(obh)->b_data;
599 aed->lengthAllocDescs =
600 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
602 else
604 UDF_I_LENALLOC(table) += adsize;
605 mark_inode_dirty(table);
608 if (UDF_SB_UDFREV(sb) >= 0x0200)
609 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
610 nbloc.logicalBlockNum, sizeof(tag));
611 else
612 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
613 nbloc.logicalBlockNum, sizeof(tag));
614 switch (UDF_I_ALLOCTYPE(table))
616 case ICBTAG_FLAG_AD_SHORT:
618 sad = (short_ad *)sptr;
619 sad->extLength = cpu_to_le32(
620 EXT_NEXT_EXTENT_ALLOCDECS |
621 sb->s_blocksize);
622 sad->extPosition = cpu_to_le32(nbloc.logicalBlockNum);
623 break;
625 case ICBTAG_FLAG_AD_LONG:
627 lad = (long_ad *)sptr;
628 lad->extLength = cpu_to_le32(
629 EXT_NEXT_EXTENT_ALLOCDECS |
630 sb->s_blocksize);
631 lad->extLocation = cpu_to_lelb(nbloc);
632 break;
635 if (obh)
637 udf_update_tag(obh->b_data, loffset);
638 mark_buffer_dirty(obh);
640 else
641 mark_inode_dirty(table);
644 if (elen) /* It's possible that stealing the block emptied the extent */
646 udf_write_aext(table, nbloc, &nextoffset, eloc, elen, nbh, 1);
648 if (!nbh)
650 UDF_I_LENALLOC(table) += adsize;
651 mark_inode_dirty(table);
653 else
655 aed = (struct allocExtDesc *)nbh->b_data;
656 aed->lengthAllocDescs =
657 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
658 udf_update_tag(nbh->b_data, nextoffset);
659 mark_buffer_dirty(nbh);
664 udf_release_data(nbh);
665 udf_release_data(obh);
667 error_return:
668 sb->s_dirt = 1;
669 mutex_unlock(&sbi->s_alloc_mutex);
670 return;
673 static int udf_table_prealloc_blocks(struct super_block * sb,
674 struct inode * inode,
675 struct inode *table, uint16_t partition, uint32_t first_block,
676 uint32_t block_count)
678 struct udf_sb_info *sbi = UDF_SB(sb);
679 int alloc_count = 0;
680 uint32_t extoffset, elen, adsize;
681 kernel_lb_addr bloc, eloc;
682 struct buffer_head *bh;
683 int8_t etype = -1;
685 if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
686 return 0;
688 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
689 adsize = sizeof(short_ad);
690 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
691 adsize = sizeof(long_ad);
692 else
693 return 0;
695 mutex_lock(&sbi->s_alloc_mutex);
696 extoffset = sizeof(struct unallocSpaceEntry);
697 bloc = UDF_I_LOCATION(table);
699 bh = NULL;
700 eloc.logicalBlockNum = 0xFFFFFFFF;
702 while (first_block != eloc.logicalBlockNum && (etype =
703 udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
705 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
706 eloc.logicalBlockNum, elen, first_block);
707 ; /* empty loop body */
710 if (first_block == eloc.logicalBlockNum)
712 extoffset -= adsize;
714 alloc_count = (elen >> sb->s_blocksize_bits);
715 if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count))
716 alloc_count = 0;
717 else if (alloc_count > block_count)
719 alloc_count = block_count;
720 eloc.logicalBlockNum += alloc_count;
721 elen -= (alloc_count << sb->s_blocksize_bits);
722 udf_write_aext(table, bloc, &extoffset, eloc, (etype << 30) | elen, bh, 1);
724 else
725 udf_delete_aext(table, bloc, extoffset, eloc, (etype << 30) | elen, bh);
727 else
728 alloc_count = 0;
730 udf_release_data(bh);
732 if (alloc_count && UDF_SB_LVIDBH(sb))
734 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
735 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-alloc_count);
736 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
737 sb->s_dirt = 1;
739 mutex_unlock(&sbi->s_alloc_mutex);
740 return alloc_count;
743 static int udf_table_new_block(struct super_block * sb,
744 struct inode * inode,
745 struct inode *table, uint16_t partition, uint32_t goal, int *err)
747 struct udf_sb_info *sbi = UDF_SB(sb);
748 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
749 uint32_t newblock = 0, adsize;
750 uint32_t extoffset, goal_extoffset, elen, goal_elen = 0;
751 kernel_lb_addr bloc, goal_bloc, eloc, goal_eloc;
752 struct buffer_head *bh, *goal_bh;
753 int8_t etype;
755 *err = -ENOSPC;
757 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
758 adsize = sizeof(short_ad);
759 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
760 adsize = sizeof(long_ad);
761 else
762 return newblock;
764 mutex_lock(&sbi->s_alloc_mutex);
765 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
766 goal = 0;
768 /* We search for the closest matching block to goal. If we find a exact hit,
769 we stop. Otherwise we keep going till we run out of extents.
770 We store the buffer_head, bloc, and extoffset of the current closest
771 match and use that when we are done.
774 extoffset = sizeof(struct unallocSpaceEntry);
775 bloc = UDF_I_LOCATION(table);
777 goal_bh = bh = NULL;
779 while (spread && (etype =
780 udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
782 if (goal >= eloc.logicalBlockNum)
784 if (goal < eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits))
785 nspread = 0;
786 else
787 nspread = goal - eloc.logicalBlockNum -
788 (elen >> sb->s_blocksize_bits);
790 else
791 nspread = eloc.logicalBlockNum - goal;
793 if (nspread < spread)
795 spread = nspread;
796 if (goal_bh != bh)
798 udf_release_data(goal_bh);
799 goal_bh = bh;
800 atomic_inc(&goal_bh->b_count);
802 goal_bloc = bloc;
803 goal_extoffset = extoffset - adsize;
804 goal_eloc = eloc;
805 goal_elen = (etype << 30) | elen;
809 udf_release_data(bh);
811 if (spread == 0xFFFFFFFF)
813 udf_release_data(goal_bh);
814 mutex_unlock(&sbi->s_alloc_mutex);
815 return 0;
818 /* Only allocate blocks from the beginning of the extent.
819 That way, we only delete (empty) extents, never have to insert an
820 extent because of splitting */
821 /* This works, but very poorly.... */
823 newblock = goal_eloc.logicalBlockNum;
824 goal_eloc.logicalBlockNum ++;
825 goal_elen -= sb->s_blocksize;
827 if (inode && DQUOT_ALLOC_BLOCK(inode, 1))
829 udf_release_data(goal_bh);
830 mutex_unlock(&sbi->s_alloc_mutex);
831 *err = -EDQUOT;
832 return 0;
835 if (goal_elen)
836 udf_write_aext(table, goal_bloc, &goal_extoffset, goal_eloc, goal_elen, goal_bh, 1);
837 else
838 udf_delete_aext(table, goal_bloc, goal_extoffset, goal_eloc, goal_elen, goal_bh);
839 udf_release_data(goal_bh);
841 if (UDF_SB_LVIDBH(sb))
843 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
844 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-1);
845 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
848 sb->s_dirt = 1;
849 mutex_unlock(&sbi->s_alloc_mutex);
850 *err = 0;
851 return newblock;
854 inline void udf_free_blocks(struct super_block * sb,
855 struct inode * inode,
856 kernel_lb_addr bloc, uint32_t offset, uint32_t count)
858 uint16_t partition = bloc.partitionReferenceNum;
860 if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
862 return udf_bitmap_free_blocks(sb, inode,
863 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
864 bloc, offset, count);
866 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
868 return udf_table_free_blocks(sb, inode,
869 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
870 bloc, offset, count);
872 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
874 return udf_bitmap_free_blocks(sb, inode,
875 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
876 bloc, offset, count);
878 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
880 return udf_table_free_blocks(sb, inode,
881 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
882 bloc, offset, count);
884 else
885 return;
888 inline int udf_prealloc_blocks(struct super_block * sb,
889 struct inode * inode,
890 uint16_t partition, uint32_t first_block, uint32_t block_count)
892 if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
894 return udf_bitmap_prealloc_blocks(sb, inode,
895 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
896 partition, first_block, block_count);
898 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
900 return udf_table_prealloc_blocks(sb, inode,
901 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
902 partition, first_block, block_count);
904 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
906 return udf_bitmap_prealloc_blocks(sb, inode,
907 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
908 partition, first_block, block_count);
910 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
912 return udf_table_prealloc_blocks(sb, inode,
913 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
914 partition, first_block, block_count);
916 else
917 return 0;
920 inline int udf_new_block(struct super_block * sb,
921 struct inode * inode,
922 uint16_t partition, uint32_t goal, int *err)
924 if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
926 return udf_bitmap_new_block(sb, inode,
927 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
928 partition, goal, err);
930 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
932 return udf_table_new_block(sb, inode,
933 UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
934 partition, goal, err);
936 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
938 return udf_bitmap_new_block(sb, inode,
939 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
940 partition, goal, err);
942 else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
944 return udf_table_new_block(sb, inode,
945 UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
946 partition, goal, err);
948 else
950 *err = -EIO;
951 return 0;