Input: Add driver for ITM Touch USB touchscreens.
[firewire-audio.git] / fs / udf / balloc.c
blobb9ded26b10a90b49972e3654d15ad198054c5faf
1 /*
2 * balloc.c
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
7 * CONTACTS
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
12 * COPYRIGHT
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
21 * HISTORY
23 * 02/24/99 blf Created.
27 #include "udfdecl.h"
29 #include <linux/quotaops.h>
30 #include <linux/buffer_head.h>
31 #include <linux/bitops.h>
33 #include "udf_i.h"
34 #include "udf_sb.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);
53 unsigned long tmp;
55 if (offset >= size)
56 return size;
57 size -= result;
58 offset &= (BITS_PER_LONG-1);
59 if (offset)
61 tmp = leBPL_to_cpup(p++);
62 tmp &= ~0UL << offset;
63 if (size < BITS_PER_LONG)
64 goto found_first;
65 if (tmp)
66 goto found_middle;
67 size -= BITS_PER_LONG;
68 result += BITS_PER_LONG;
70 while (size & ~(BITS_PER_LONG-1))
72 if ((tmp = leBPL_to_cpup(p++)))
73 goto found_middle;
74 result += BITS_PER_LONG;
75 size -= BITS_PER_LONG;
77 if (!size)
78 return result;
79 tmp = leBPL_to_cpup(p);
80 found_first:
81 tmp &= ~0UL >> (BITS_PER_LONG-size);
82 found_middle:
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;
93 int retval = 0;
94 kernel_lb_addr loc;
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));
100 if (!bh)
102 retval = -EIO;
104 bitmap->s_block_bitmap[bitmap_nr] = bh;
105 return retval;
108 static int __load_block_bitmap(struct super_block * sb,
109 struct udf_bitmap *bitmap, unsigned int block_group)
111 int retval = 0;
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])
120 return block_group;
121 else
123 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
124 if (retval < 0)
125 return retval;
126 return block_group;
130 static inline int load_block_bitmap(struct super_block * sb,
131 struct udf_bitmap *bitmap, unsigned int block_group)
133 int slot;
135 slot = __load_block_bitmap(sb, bitmap, block_group);
137 if (slot < 0)
138 return slot;
140 if (!bitmap->s_block_bitmap[slot])
141 return -EIO;
143 return 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;
153 unsigned long block;
154 unsigned long block_group;
155 unsigned long bit;
156 unsigned long i;
157 int bitmap_nr;
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));
167 goto error_return;
170 block = bloc.logicalBlockNum + offset + (sizeof(struct spaceBitmapDesc) << 3);
172 do_more:
173 overflow = 0;
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);
183 count -= overflow;
185 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
186 if (bitmap_nr < 0)
187 goto error_return;
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]);
197 else
199 if (inode)
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);
209 if (overflow)
211 block += count;
212 count = overflow;
213 goto do_more;
215 error_return:
216 sb->s_dirt = 1;
217 if (UDF_SB_LVIDBH(sb))
218 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
219 up(&sbi->s_alloc_sem);
220 return;
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);
229 int alloc_count = 0;
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))
236 goto out;
238 if (first_block + block_count > UDF_SB_PARTLEN(sb, partition))
239 block_count = UDF_SB_PARTLEN(sb, partition) - first_block;
241 repeat:
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);
249 if (bitmap_nr < 0)
250 goto out;
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))
258 goto out;
259 else if (DQUOT_PREALLOC_BLOCK(inode, 1))
260 goto out;
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);
265 goto out;
267 block_count --;
268 alloc_count ++;
269 bit ++;
270 block ++;
272 mark_buffer_dirty(bh);
273 if (block_count > 0)
274 goto repeat;
275 out:
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));
282 sb->s_dirt = 1;
283 up(&sbi->s_alloc_sem);
284 return alloc_count;
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;
295 char *ptr;
296 int newblock = 0;
298 *err = -ENOSPC;
299 down(&sbi->s_alloc_sem);
301 repeat:
302 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
303 goal = 0;
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);
311 if (bitmap_nr < 0)
312 goto error_return;
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))
322 goto got_block;
324 end_goal = (bit + 63) & ~63;
325 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
326 if (bit < end_goal)
327 goto got_block;
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)
332 bit = newbit;
333 goto search_back;
335 newbit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, bit);
336 if (newbit < sb->s_blocksize << 3)
338 bit = newbit;
339 goto got_block;
343 for (i=0; i<(nr_groups*2); i++)
345 block_group ++;
346 if (block_group >= nr_groups)
347 block_group = 0;
348 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
350 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
351 if (bitmap_nr < 0)
352 goto error_return;
353 bh = bitmap->s_block_bitmap[bitmap_nr];
354 if (i < nr_groups)
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;
360 break;
363 else
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)
367 break;
370 if (i >= (nr_groups*2))
372 up(&sbi->s_alloc_sem);
373 return newblock;
375 if (bit < sb->s_blocksize << 3)
376 goto search_back;
377 else
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);
382 return 0;
385 search_back:
386 for (i=0; i<7 && bit > (group_start << 3) && udf_test_bit(bit - 1, bh->b_data); i++, bit--);
388 got_block:
391 * Check quota for allocation of this block.
393 if (inode && DQUOT_ALLOC_BLOCK(inode, 1))
395 up(&sbi->s_alloc_sem);
396 *err = -EDQUOT;
397 return 0;
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);
406 goto repeat;
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));
417 sb->s_dirt = 1;
418 up(&sbi->s_alloc_sem);
419 *err = 0;
420 return newblock;
422 error_return:
423 *err = -EIO;
424 up(&sbi->s_alloc_sem);
425 return 0;
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);
434 uint32_t start, end;
435 uint32_t nextoffset, oextoffset, elen;
436 kernel_lb_addr nbloc, obloc, eloc;
437 struct buffer_head *obh, *nbh;
438 int8_t etype;
439 int i;
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));
448 goto error_return;
451 /* We do this up front - There are some error conditions that could occure,
452 but.. oh well */
453 if (inode)
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);
466 elen = 0;
467 obloc = nbloc = UDF_I_LOCATION(table);
469 obh = nbh = NULL;
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)) ==
475 start))
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);
483 else
485 elen = (etype << 30) |
486 (elen + (count << sb->s_blocksize_bits));
487 start += count;
488 count = 0;
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);
502 else
504 eloc.logicalBlockNum = start;
505 elen = (etype << 30) |
506 (elen + (count << sb->s_blocksize_bits));
507 end -= count;
508 count = 0;
510 udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
513 if (nbh != obh)
515 i = -1;
516 obloc = nbloc;
517 udf_release_data(obh);
518 atomic_inc(&nbh->b_count);
519 obh = nbh;
520 oextoffset = 0;
522 else
523 oextoffset = nextoffset;
526 if (count)
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
537 isn't required.
540 int adsize;
541 short_ad *sad = NULL;
542 long_ad *lad = 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);
553 else
555 udf_release_data(obh);
556 udf_release_data(nbh);
557 goto error_return;
560 if (nextoffset + (2 * adsize) > sb->s_blocksize)
562 char *sptr, *dptr;
563 int loffset;
565 udf_release_data(obh);
566 obh = nbh;
567 obloc = nbloc;
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);
579 goto error_return;
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);
587 if (obh)
588 sptr = UDF_I_DATA(inode) + nextoffset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode) - adsize;
589 else
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;
595 else
597 loffset = nextoffset + adsize;
598 aed->lengthAllocDescs = cpu_to_le32(0);
599 sptr = (obh)->b_data + nextoffset;
600 nextoffset = sizeof(struct allocExtDesc);
602 if (obh)
604 aed = (struct allocExtDesc *)(obh)->b_data;
605 aed->lengthAllocDescs =
606 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
608 else
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));
617 else
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 |
627 sb->s_blocksize);
628 sad->extPosition = cpu_to_le32(nbloc.logicalBlockNum);
629 break;
631 case ICBTAG_FLAG_AD_LONG:
633 lad = (long_ad *)sptr;
634 lad->extLength = cpu_to_le32(
635 EXT_NEXT_EXTENT_ALLOCDECS |
636 sb->s_blocksize);
637 lad->extLocation = cpu_to_lelb(nbloc);
638 break;
641 if (obh)
643 udf_update_tag(obh->b_data, loffset);
644 mark_buffer_dirty(obh);
646 else
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);
654 if (!nbh)
656 UDF_I_LENALLOC(table) += adsize;
657 mark_inode_dirty(table);
659 else
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);
673 error_return:
674 sb->s_dirt = 1;
675 up(&sbi->s_alloc_sem);
676 return;
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);
685 int alloc_count = 0;
686 uint32_t extoffset, elen, adsize;
687 kernel_lb_addr bloc, eloc;
688 struct buffer_head *bh;
689 int8_t etype = -1;
691 if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
692 return 0;
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);
698 else
699 return 0;
701 down(&sbi->s_alloc_sem);
702 extoffset = sizeof(struct unallocSpaceEntry);
703 bloc = UDF_I_LOCATION(table);
705 bh = NULL;
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)
718 extoffset -= adsize;
720 alloc_count = (elen >> sb->s_blocksize_bits);
721 if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count))
722 alloc_count = 0;
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);
730 else
731 udf_delete_aext(table, bloc, extoffset, eloc, (etype << 30) | elen, bh);
733 else
734 alloc_count = 0;
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));
743 sb->s_dirt = 1;
745 up(&sbi->s_alloc_sem);
746 return alloc_count;
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;
759 int8_t etype;
761 *err = -ENOSPC;
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);
767 else
768 return newblock;
770 down(&sbi->s_alloc_sem);
771 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
772 goal = 0;
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);
783 goal_bh = bh = NULL;
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))
791 nspread = 0;
792 else
793 nspread = goal - eloc.logicalBlockNum -
794 (elen >> sb->s_blocksize_bits);
796 else
797 nspread = eloc.logicalBlockNum - goal;
799 if (nspread < spread)
801 spread = nspread;
802 if (goal_bh != bh)
804 udf_release_data(goal_bh);
805 goal_bh = bh;
806 atomic_inc(&goal_bh->b_count);
808 goal_bloc = bloc;
809 goal_extoffset = extoffset - adsize;
810 goal_eloc = eloc;
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);
821 return 0;
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);
837 *err = -EDQUOT;
838 return 0;
841 if (goal_elen)
842 udf_write_aext(table, goal_bloc, &goal_extoffset, goal_eloc, goal_elen, goal_bh, 1);
843 else
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));
854 sb->s_dirt = 1;
855 up(&sbi->s_alloc_sem);
856 *err = 0;
857 return newblock;
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);
890 else
891 return;
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);
922 else
923 return 0;
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);
954 else
956 *err = -EIO;
957 return 0;