ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / udf / balloc.c
1 /*
2  * balloc.c
3  *
4  * PURPOSE
5  *      Block allocation handling routines for the OSTA-UDF(tm) filesystem.
6  *
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
11  *
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.
17  *
18  *  (C) 1999-2001 Ben Fennema
19  *  (C) 1999 Stelias Computing Inc
20  *
21  * HISTORY
22  *
23  *  02/24/99 blf  Created.
24  *
25  */
26
27 #include "udfdecl.h"
28
29 #include <linux/quotaops.h>
30 #include <linux/buffer_head.h>
31 #include <asm/bitops.h>
32
33 #include "udf_i.h"
34 #include "udf_sb.h"
35
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)
41
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) uint ## x ## _t
48
49 extern inline int find_next_one_bit (void * addr, int size, int offset)
50 {
51         uintBPL_t * p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
52         uintBPL_t result = offset & ~(BITS_PER_LONG-1);
53         uintBPL_t tmp;
54
55         if (offset >= size)
56                 return size;
57         size -= result;
58         offset &= (BITS_PER_LONG-1);
59         if (offset)
60         {
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;
69         }
70         while (size & ~(BITS_PER_LONG-1))
71         {
72                 if ((tmp = leBPL_to_cpup(p++)))
73                         goto found_middle;
74                 result += BITS_PER_LONG;
75                 size -= BITS_PER_LONG;
76         }
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);
84 }
85
86 #define find_first_one_bit(addr, size)\
87         find_next_one_bit((addr), (size), 0)
88
89 static int read_block_bitmap(struct super_block * sb,
90         struct udf_bitmap *bitmap, unsigned int block, unsigned long bitmap_nr)
91 {
92         struct buffer_head *bh = NULL;
93         int retval = 0;
94         lb_addr loc;
95
96         loc.logicalBlockNum = bitmap->s_extPosition;
97         loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
98
99         bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block));
100         if (!bh)
101         {
102                 retval = -EIO;
103         }
104         bitmap->s_block_bitmap[bitmap_nr] = bh;
105         return retval;
106 }
107
108 static int __load_block_bitmap(struct super_block * sb,
109         struct udf_bitmap *bitmap, unsigned int block_group)
110 {
111         int retval = 0;
112         int nr_groups = bitmap->s_nr_groups;
113
114         if (block_group >= nr_groups)
115         {
116                 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group, nr_groups);
117         }
118
119         if (bitmap->s_block_bitmap[block_group])
120                 return block_group;
121         else
122         {
123                 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
124                 if (retval < 0)
125                         return retval;
126                 return block_group;
127         }
128 }
129
130 static inline int load_block_bitmap(struct super_block * sb,
131         struct udf_bitmap *bitmap, unsigned int block_group)
132 {
133         int slot;
134
135         slot = __load_block_bitmap(sb, bitmap, block_group);
136
137         if (slot < 0)
138                 return slot;
139
140         if (!bitmap->s_block_bitmap[slot])
141                 return -EIO;
142
143         return slot;
144 }
145
146 static void udf_bitmap_free_blocks(struct super_block * sb,
147         struct inode * inode,
148         struct udf_bitmap *bitmap, lb_addr bloc, uint32_t offset, uint32_t count)
149 {
150         struct buffer_head * bh = NULL;
151         unsigned long block;
152         unsigned long block_group;
153         unsigned long bit;
154         unsigned long i;
155         int bitmap_nr;
156         unsigned long overflow;
157
158         lock_super(sb);
159         if (bloc.logicalBlockNum < 0 ||
160                 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum))
161         {
162                 udf_debug("%d < %d || %d + %d > %d\n",
163                         bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
164                         UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum));
165                 goto error_return;
166         }
167
168         block = bloc.logicalBlockNum + offset + (sizeof(struct spaceBitmapDesc) << 3);
169
170 do_more:
171         overflow = 0;
172         block_group = block >> (sb->s_blocksize_bits + 3);
173         bit = block % (sb->s_blocksize << 3);
174
175         /*
176          * Check to see if we are freeing blocks across a group boundary.
177          */
178         if (bit + count > (sb->s_blocksize << 3))
179         {
180                 overflow = bit + count - (sb->s_blocksize << 3);
181                 count -= overflow;
182         }
183         bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
184         if (bitmap_nr < 0)
185                 goto error_return;
186
187         bh = bitmap->s_block_bitmap[bitmap_nr];
188         for (i=0; i < count; i++)
189         {
190                 if (udf_set_bit(bit + i, bh->b_data))
191                 {
192                         udf_debug("bit %ld already set\n", bit + i);
193                         udf_debug("byte=%2x\n", ((char *)bh->b_data)[(bit + i) >> 3]);
194                 }
195                 else
196                 {
197                         if (inode)
198                                 DQUOT_FREE_BLOCK(inode, 1);
199                         if (UDF_SB_LVIDBH(sb))
200                         {
201                                 UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)] =
202                                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)])+1);
203                         }
204                 }
205         }
206         mark_buffer_dirty(bh);
207         if (overflow)
208         {
209                 block += count;
210                 count = overflow;
211                 goto do_more;
212         }
213 error_return:
214         sb->s_dirt = 1;
215         if (UDF_SB_LVIDBH(sb))
216                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
217         unlock_super(sb);
218         return;
219 }
220
221 static int udf_bitmap_prealloc_blocks(struct super_block * sb,
222         struct inode * inode,
223         struct udf_bitmap *bitmap, uint16_t partition, uint32_t first_block,
224         uint32_t block_count)
225 {
226         int alloc_count = 0;
227         int bit, block, block_group, group_start;
228         int nr_groups, bitmap_nr;
229         struct buffer_head *bh;
230
231         lock_super(sb);
232
233         if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
234                 goto out;
235
236         if (first_block + block_count > UDF_SB_PARTLEN(sb, partition))
237                 block_count = UDF_SB_PARTLEN(sb, partition) - first_block;
238
239 repeat:
240         nr_groups = (UDF_SB_PARTLEN(sb, partition) +
241                 (sizeof(struct spaceBitmapDesc) << 3) + (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
242         block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
243         block_group = block >> (sb->s_blocksize_bits + 3);
244         group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
245
246         bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
247         if (bitmap_nr < 0)
248                 goto out;
249         bh = bitmap->s_block_bitmap[bitmap_nr];
250
251         bit = block % (sb->s_blocksize << 3);
252
253         while (bit < (sb->s_blocksize << 3) && block_count > 0)
254         {
255                 if (!udf_test_bit(bit, bh->b_data))
256                         goto out;
257                 else if (DQUOT_PREALLOC_BLOCK(inode, 1))
258                         goto out;
259                 else if (!udf_clear_bit(bit, bh->b_data))
260                 {
261                         udf_debug("bit already cleared for block %d\n", bit);
262                         DQUOT_FREE_BLOCK(inode, 1);
263                         goto out;
264                 }
265                 block_count --;
266                 alloc_count ++;
267                 bit ++;
268                 block ++;
269         }
270         mark_buffer_dirty(bh);
271         if (block_count > 0)
272                 goto repeat;
273 out:
274         if (UDF_SB_LVIDBH(sb))
275         {
276                 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
277                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-alloc_count);
278                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
279         }
280         sb->s_dirt = 1;
281         unlock_super(sb);
282         return alloc_count;
283 }
284
285 static int udf_bitmap_new_block(struct super_block * sb,
286         struct inode * inode,
287         struct udf_bitmap *bitmap, uint16_t partition, uint32_t goal, int *err)
288 {
289         int newbit, bit=0, block, block_group, group_start;
290         int end_goal, nr_groups, bitmap_nr, i;
291         struct buffer_head *bh = NULL;
292         char *ptr;
293         int newblock = 0;
294
295         *err = -ENOSPC;
296         lock_super(sb);
297
298 repeat:
299         if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
300                 goal = 0;
301
302         nr_groups = bitmap->s_nr_groups;
303         block = goal + (sizeof(struct spaceBitmapDesc) << 3);
304         block_group = block >> (sb->s_blocksize_bits + 3);
305         group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
306
307         bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
308         if (bitmap_nr < 0)
309                 goto error_return;
310         bh = bitmap->s_block_bitmap[bitmap_nr];
311         ptr = memscan((char *)bh->b_data + group_start, 0xFF, sb->s_blocksize - group_start);
312
313         if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize)
314         {
315                 bit = block % (sb->s_blocksize << 3);
316
317                 if (udf_test_bit(bit, bh->b_data))
318                 {
319                         goto got_block;
320                 }
321                 end_goal = (bit + 63) & ~63;
322                 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
323                 if (bit < end_goal)
324                         goto got_block;
325                 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, sb->s_blocksize - ((bit + 7) >> 3));
326                 newbit = (ptr - ((char *)bh->b_data)) << 3;
327                 if (newbit < sb->s_blocksize << 3)
328                 {
329                         bit = newbit;
330                         goto search_back;
331                 }
332                 newbit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, bit);
333                 if (newbit < sb->s_blocksize << 3)
334                 {
335                         bit = newbit;
336                         goto got_block;
337                 }
338         }
339
340         for (i=0; i<(nr_groups*2); i++)
341         {
342                 block_group ++;
343                 if (block_group >= nr_groups)
344                         block_group = 0;
345                 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
346
347                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
348                 if (bitmap_nr < 0)
349                         goto error_return;
350                 bh = bitmap->s_block_bitmap[bitmap_nr];
351                 if (i < nr_groups)
352                 {
353                         ptr = memscan((char *)bh->b_data + group_start, 0xFF, sb->s_blocksize - group_start);
354                         if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize)
355                         {
356                                 bit = (ptr - ((char *)bh->b_data)) << 3;
357                                 break;
358                         }
359                 }
360                 else
361                 {
362                         bit = udf_find_next_one_bit((char *)bh->b_data, sb->s_blocksize << 3, group_start << 3);
363                         if (bit < sb->s_blocksize << 3)
364                                 break;
365                 }
366         }
367         if (i >= (nr_groups*2))
368         {
369                 unlock_super(sb);
370                 return newblock;
371         }
372         if (bit < sb->s_blocksize << 3)
373                 goto search_back;
374         else
375                 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, group_start << 3);
376         if (bit >= sb->s_blocksize << 3)
377         {
378                 unlock_super(sb);
379                 return 0;
380         }
381
382 search_back:
383         for (i=0; i<7 && bit > (group_start << 3) && udf_test_bit(bit - 1, bh->b_data); i++, bit--);
384
385 got_block:
386
387         /*
388          * Check quota for allocation of this block.
389          */
390         if (inode && DQUOT_ALLOC_BLOCK(inode, 1))
391         {
392                 unlock_super(sb);
393                 *err = -EDQUOT;
394                 return 0;
395         }
396
397         newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
398                 (sizeof(struct spaceBitmapDesc) << 3);
399
400         if (!udf_clear_bit(bit, bh->b_data))
401         {
402                 udf_debug("bit already cleared for block %d\n", bit);
403                 goto repeat;
404         }
405
406         mark_buffer_dirty(bh);
407
408         if (UDF_SB_LVIDBH(sb))
409         {
410                 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
411                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-1);
412                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
413         }
414         sb->s_dirt = 1;
415         unlock_super(sb);
416         *err = 0;
417         return newblock;
418
419 error_return:
420         *err = -EIO;
421         unlock_super(sb);
422         return 0;
423 }
424
425 static void udf_table_free_blocks(struct super_block * sb,
426         struct inode * inode,
427         struct inode * table, lb_addr bloc, uint32_t offset, uint32_t count)
428 {
429         uint32_t start, end;
430         uint32_t nextoffset, oextoffset, elen;
431         lb_addr nbloc, obloc, eloc;
432         struct buffer_head *obh, *nbh;
433         int8_t etype;
434         int i;
435
436         lock_super(sb);
437         if (bloc.logicalBlockNum < 0 ||
438                 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum))
439         {
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;
444         }
445
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))
451         {
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));
455         }
456
457         start = bloc.logicalBlockNum + offset;
458         end = bloc.logicalBlockNum + offset + count - 1;
459
460         oextoffset = nextoffset = sizeof(struct unallocSpaceEntry);
461         elen = 0;
462         obloc = nbloc = UDF_I_LOCATION(table);
463
464         obh = nbh = NULL;
465
466         while (count && (etype =
467                 udf_next_aext(table, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1)) != -1)
468         {
469                 if (((eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits)) ==
470                         start))
471                 {
472                         if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits))
473                         {
474                                 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
475                                 start += ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
476                                 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
477                         }
478                         else
479                         {
480                                 elen = (etype << 30) |
481                                         (elen + (count << sb->s_blocksize_bits));
482                                 start += count;
483                                 count = 0;
484                         }
485                         udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
486                 }
487                 else if (eloc.logicalBlockNum == (end + 1))
488                 {
489                         if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits))
490                         {
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);
496                         }
497                         else
498                         {
499                                 eloc.logicalBlockNum = start;
500                                 elen = (etype << 30) |
501                                         (elen + (count << sb->s_blocksize_bits));
502                                 end -= count;
503                                 count = 0;
504                         }
505                         udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
506                 }
507
508                 if (nbh != obh)
509                 {
510                         i = -1;
511                         obloc = nbloc;
512                         udf_release_data(obh);
513                         atomic_inc(&nbh->b_count);
514                         obh = nbh;
515                         oextoffset = 0;
516                 }
517                 else
518                         oextoffset = nextoffset;
519         }
520
521         if (count)
522         {
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 :)
526
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.
530
531                                  It would be nice if the blocks were close together, but it
532                                  isn't required.
533                 */
534
535                 int adsize;
536                 short_ad *sad = NULL;
537                 long_ad *lad = NULL;
538                 struct allocExtDesc *aed;
539
540                 eloc.logicalBlockNum = start;
541                 elen = EXT_RECORDED_ALLOCATED |
542                         (count << sb->s_blocksize_bits);
543
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
549                 {
550                         udf_release_data(obh);
551                         udf_release_data(nbh);
552                         goto error_return;
553                 }
554
555                 if (nextoffset + (2 * adsize) > sb->s_blocksize)
556                 {
557                         char *sptr, *dptr;
558                         int loffset;
559         
560                         udf_release_data(obh);
561                         obh = nbh;
562                         obloc = nbloc;
563                         oextoffset = nextoffset;
564
565                         /* Steal a block from the extent being free'd */
566                         nbloc.logicalBlockNum = eloc.logicalBlockNum;
567                         eloc.logicalBlockNum ++;
568                         elen -= sb->s_blocksize;
569
570                         if (!(nbh = udf_tread(sb,
571                                 udf_get_lb_pblock(sb, nbloc, 0))))
572                         {
573                                 udf_release_data(obh);
574                                 goto error_return;
575                         }
576                         aed = (struct allocExtDesc *)(nbh->b_data);
577                         aed->previousAllocExtLocation = cpu_to_le32(obloc.logicalBlockNum);
578                         if (nextoffset + adsize > sb->s_blocksize)
579                         {
580                                 loffset = nextoffset;
581                                 aed->lengthAllocDescs = cpu_to_le32(adsize);
582                                 if (obh)
583                                         sptr = UDF_I_DATA(inode) + nextoffset -  udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode) - adsize;
584                                 else
585                                         sptr = obh->b_data + nextoffset - adsize;
586                                 dptr = nbh->b_data + sizeof(struct allocExtDesc);
587                                 memcpy(dptr, sptr, adsize);
588                                 nextoffset = sizeof(struct allocExtDesc) + adsize;
589                         }
590                         else
591                         {
592                                 loffset = nextoffset + adsize;
593                                 aed->lengthAllocDescs = cpu_to_le32(0);
594                                 sptr = (obh)->b_data + nextoffset;
595                                 nextoffset = sizeof(struct allocExtDesc);
596
597                                 if (obh)
598                                 {
599                                         aed = (struct allocExtDesc *)(obh)->b_data;
600                                         aed->lengthAllocDescs =
601                                                 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
602                                 }
603                                 else
604                                 {
605                                         UDF_I_LENALLOC(table) += adsize;
606                                         mark_inode_dirty(table);
607                                 }
608                         }
609                         if (UDF_SB_UDFREV(sb) >= 0x0200)
610                                 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
611                                         nbloc.logicalBlockNum, sizeof(tag));
612                         else
613                                 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
614                                         nbloc.logicalBlockNum, sizeof(tag));
615                         switch (UDF_I_ALLOCTYPE(table))
616                         {
617                                 case ICBTAG_FLAG_AD_SHORT:
618                                 {
619                                         sad = (short_ad *)sptr;
620                                         sad->extLength = cpu_to_le32(
621                                                 EXT_NEXT_EXTENT_ALLOCDECS |
622                                                 sb->s_blocksize);
623                                         sad->extPosition = cpu_to_le32(nbloc.logicalBlockNum);
624                                         break;
625                                 }
626                                 case ICBTAG_FLAG_AD_LONG:
627                                 {
628                                         lad = (long_ad *)sptr;
629                                         lad->extLength = cpu_to_le32(
630                                                 EXT_NEXT_EXTENT_ALLOCDECS |
631                                                 sb->s_blocksize);
632                                         lad->extLocation = cpu_to_lelb(nbloc);
633                                         break;
634                                 }
635                         }
636                         if (obh)
637                         {
638                                 udf_update_tag(obh->b_data, loffset);
639                                 mark_buffer_dirty(obh);
640                         }
641                         else
642                                 mark_inode_dirty(table);
643                 }
644
645                 if (elen) /* It's possible that stealing the block emptied the extent */
646                 {
647                         udf_write_aext(table, nbloc, &nextoffset, eloc, elen, nbh, 1);
648
649                         if (!nbh)
650                         {
651                                 UDF_I_LENALLOC(table) += adsize;
652                                 mark_inode_dirty(table);
653                         }
654                         else
655                         {
656                                 aed = (struct allocExtDesc *)nbh->b_data;
657                                 aed->lengthAllocDescs =
658                                         cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
659                                 udf_update_tag(nbh->b_data, nextoffset);
660                                 mark_buffer_dirty(nbh);
661                         }
662                 }
663         }
664
665         udf_release_data(nbh);
666         udf_release_data(obh);
667
668 error_return:
669         sb->s_dirt = 1;
670         unlock_super(sb);
671         return;
672 }
673
674 static int udf_table_prealloc_blocks(struct super_block * sb,
675         struct inode * inode,
676         struct inode *table, uint16_t partition, uint32_t first_block,
677         uint32_t block_count)
678 {
679         int alloc_count = 0;
680         uint32_t extoffset, elen, adsize;
681         lb_addr bloc, eloc;
682         struct buffer_head *bh;
683         int8_t etype = -1;
684
685         if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
686                 return 0;
687
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;
694
695         lock_super(sb);
696
697         extoffset = sizeof(struct unallocSpaceEntry);
698         bloc = UDF_I_LOCATION(table);
699
700         bh = NULL;
701         eloc.logicalBlockNum = 0xFFFFFFFF;
702
703         while (first_block != eloc.logicalBlockNum && (etype =
704                 udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
705         {
706                 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
707                         eloc.logicalBlockNum, elen, first_block);
708                 ; /* empty loop body */
709         }
710
711         if (first_block == eloc.logicalBlockNum)
712         {
713                 extoffset -= adsize;
714
715                 alloc_count = (elen >> sb->s_blocksize_bits);
716                 if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count))
717                         alloc_count = 0;
718                 else if (alloc_count > block_count)
719                 {
720                         alloc_count = block_count;
721                         eloc.logicalBlockNum += alloc_count;
722                         elen -= (alloc_count << sb->s_blocksize_bits);
723                         udf_write_aext(table, bloc, &extoffset, eloc, (etype << 30) | elen, bh, 1);
724                 }
725                 else
726                         udf_delete_aext(table, bloc, extoffset, eloc, (etype << 30) | elen, bh);
727         }
728         else
729                 alloc_count = 0;
730
731         udf_release_data(bh);
732
733         if (alloc_count && UDF_SB_LVIDBH(sb))
734         {
735                 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
736                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-alloc_count);
737                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
738                 sb->s_dirt = 1;
739         }
740         unlock_super(sb);
741         return alloc_count;
742 }
743
744 static int udf_table_new_block(struct super_block * sb,
745         struct inode * inode,
746         struct inode *table, uint16_t partition, uint32_t goal, int *err)
747 {
748         uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
749         uint32_t newblock = 0, adsize;
750         uint32_t extoffset, goal_extoffset, elen, goal_elen = 0;
751         lb_addr bloc, goal_bloc, eloc, goal_eloc;
752         struct buffer_head *bh, *goal_bh;
753         int8_t etype;
754
755         *err = -ENOSPC;
756
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;
763
764         lock_super(sb);
765
766         if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
767                 goal = 0;
768
769         /* We search for the closest matching block to goal. If we find a exact hit,
770            we stop. Otherwise we keep going till we run out of extents.
771            We store the buffer_head, bloc, and extoffset of the current closest
772            match and use that when we are done.
773         */
774
775         extoffset = sizeof(struct unallocSpaceEntry);
776         bloc = UDF_I_LOCATION(table);
777
778         goal_bh = bh = NULL;
779
780         while (spread && (etype =
781                 udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
782         {
783                 if (goal >= eloc.logicalBlockNum)
784                 {
785                         if (goal < eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits))
786                                 nspread = 0;
787                         else
788                                 nspread = goal - eloc.logicalBlockNum -
789                                         (elen >> sb->s_blocksize_bits);
790                 }
791                 else
792                         nspread = eloc.logicalBlockNum - goal;
793
794                 if (nspread < spread)
795                 {
796                         spread = nspread;
797                         if (goal_bh != bh)
798                         {
799                                 udf_release_data(goal_bh);
800                                 goal_bh = bh;
801                                 atomic_inc(&goal_bh->b_count);
802                         }
803                         goal_bloc = bloc;
804                         goal_extoffset = extoffset - adsize;
805                         goal_eloc = eloc;
806                         goal_elen = (etype << 30) | elen;
807                 }
808         }
809
810         udf_release_data(bh);
811
812         if (spread == 0xFFFFFFFF)
813         {
814                 udf_release_data(goal_bh);
815                 unlock_super(sb);
816                 return 0;
817         }
818
819         /* Only allocate blocks from the beginning of the extent.
820            That way, we only delete (empty) extents, never have to insert an
821            extent because of splitting */
822         /* This works, but very poorly.... */
823
824         newblock = goal_eloc.logicalBlockNum;
825         goal_eloc.logicalBlockNum ++;
826         goal_elen -= sb->s_blocksize;
827
828         if (inode && DQUOT_ALLOC_BLOCK(inode, 1))
829         {
830                 udf_release_data(goal_bh);
831                 unlock_super(sb);
832                 *err = -EDQUOT;
833                 return 0;
834         }
835
836         if (goal_elen)
837                 udf_write_aext(table, goal_bloc, &goal_extoffset, goal_eloc, goal_elen, goal_bh, 1);
838         else
839                 udf_delete_aext(table, goal_bloc, goal_extoffset, goal_eloc, goal_elen, goal_bh);
840         udf_release_data(goal_bh);
841
842         if (UDF_SB_LVIDBH(sb))
843         {
844                 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
845                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-1);
846                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
847         }
848
849         sb->s_dirt = 1;
850         unlock_super(sb);
851         *err = 0;
852         return newblock;
853 }
854
855 inline void udf_free_blocks(struct super_block * sb,
856         struct inode * inode,
857         lb_addr bloc, uint32_t offset, uint32_t count)
858 {
859         uint16_t partition = bloc.partitionReferenceNum;
860
861         if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
862         {
863                 return udf_bitmap_free_blocks(sb, inode,
864                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
865                         bloc, offset, count);
866         }
867         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
868         {
869                 return udf_table_free_blocks(sb, inode,
870                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
871                         bloc, offset, count);
872         }
873         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
874         {
875                 return udf_bitmap_free_blocks(sb, inode,
876                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
877                         bloc, offset, count);
878         }
879         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
880         {
881                 return udf_table_free_blocks(sb, inode,
882                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
883                         bloc, offset, count);
884         }
885         else
886                 return;
887 }
888
889 inline int udf_prealloc_blocks(struct super_block * sb,
890         struct inode * inode,
891         uint16_t partition, uint32_t first_block, uint32_t block_count)
892 {
893         if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
894         {
895                 return udf_bitmap_prealloc_blocks(sb, inode,
896                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
897                         partition, first_block, block_count);
898         }
899         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
900         {
901                 return udf_table_prealloc_blocks(sb, inode,
902                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
903                         partition, first_block, block_count);
904         }
905         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
906         {
907                 return udf_bitmap_prealloc_blocks(sb, inode,
908                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
909                         partition, first_block, block_count);
910         }
911         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
912         {
913                 return udf_table_prealloc_blocks(sb, inode,
914                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
915                         partition, first_block, block_count);
916         }
917         else
918                 return 0;
919 }
920
921 inline int udf_new_block(struct super_block * sb,
922         struct inode * inode,
923         uint16_t partition, uint32_t goal, int *err)
924 {
925         if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
926         {
927                 return udf_bitmap_new_block(sb, inode,
928                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
929                         partition, goal, err);
930         }
931         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
932         {
933                 return udf_table_new_block(sb, inode,
934                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
935                         partition, goal, err);
936         }
937         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
938         {
939                 return udf_bitmap_new_block(sb, inode,
940                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
941                         partition, goal, err);
942         }
943         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
944         {
945                 return udf_table_new_block(sb, inode,
946                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
947                         partition, goal, err);
948         }
949         else
950         {
951                 *err = -EIO;
952                 return 0;
953         }
954 }