more changes on original files
[linux-2.4.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/locks.h>
30 #include <linux/quotaops.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 = udf_tread(sb, udf_get_lb_pblock(sb, nbloc, 0));
465         atomic_inc(&nbh->b_count);
466
467         while (count && (etype =
468                 udf_next_aext(table, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1)) != -1)
469         {
470                 if (((eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits)) ==
471                         start))
472                 {
473                         if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits))
474                         {
475                                 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
476                                 start += ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
477                                 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
478                         }
479                         else
480                         {
481                                 elen = (etype << 30) |
482                                         (elen + (count << sb->s_blocksize_bits));
483                                 start += count;
484                                 count = 0;
485                         }
486                         udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
487                 }
488                 else if (eloc.logicalBlockNum == (end + 1))
489                 {
490                         if ((0x3FFFFFFF - elen) < (count << sb->s_blocksize_bits))
491                         {
492                                 count -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
493                                 end -= ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
494                                 eloc.logicalBlockNum -=
495                                         ((0x3FFFFFFF - elen) >> sb->s_blocksize_bits);
496                                 elen = (etype << 30) | (0x40000000 - sb->s_blocksize);
497                         }
498                         else
499                         {
500                                 eloc.logicalBlockNum = start;
501                                 elen = (etype << 30) |
502                                         (elen + (count << sb->s_blocksize_bits));
503                                 end -= count;
504                                 count = 0;
505                         }
506                         udf_write_aext(table, obloc, &oextoffset, eloc, elen, obh, 1);
507                 }
508
509                 if (memcmp(&nbloc, &obloc, sizeof(lb_addr)))
510                 {
511                         i = -1;
512                         obloc = nbloc;
513                         udf_release_data(obh);
514                         atomic_inc(&nbh->b_count);
515                         obh = nbh;
516                         oextoffset = 0;
517                 }
518                 else
519                         oextoffset = nextoffset;
520         }
521
522         if (count)
523         {
524                 /* NOTE: we CANNOT use udf_add_aext here, as it can try to allocate
525                                  a new block, and since we hold the super block lock already
526                                  very bad things would happen :)
527
528                                  We copy the behavior of udf_add_aext, but instead of
529                                  trying to allocate a new block close to the existing one,
530                                  we just steal a block from the extent we are trying to add.
531
532                                  It would be nice if the blocks were close together, but it
533                                  isn't required.
534                 */
535
536                 int adsize;
537                 short_ad *sad = NULL;
538                 long_ad *lad = NULL;
539                 struct allocExtDesc *aed;
540
541                 eloc.logicalBlockNum = start;
542                 elen = EXT_RECORDED_ALLOCATED |
543                         (count << sb->s_blocksize_bits);
544
545                 if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
546                         adsize = sizeof(short_ad);
547                 else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
548                         adsize = sizeof(long_ad);
549                 else
550                 {
551                         udf_release_data(obh);
552                         udf_release_data(nbh);
553                         goto error_return;
554                 }
555
556                 if (nextoffset + (2 * adsize) > sb->s_blocksize)
557                 {
558                         char *sptr, *dptr;
559                         int loffset;
560         
561                         udf_release_data(obh);
562                         obh = nbh;
563                         obloc = nbloc;
564                         oextoffset = nextoffset;
565
566                         /* Steal a block from the extent being free'd */
567                         nbloc.logicalBlockNum = eloc.logicalBlockNum;
568                         eloc.logicalBlockNum ++;
569                         elen -= sb->s_blocksize;
570
571                         if (!(nbh = udf_tread(sb,
572                                 udf_get_lb_pblock(sb, nbloc, 0))))
573                         {
574                                 udf_release_data(obh);
575                                 goto error_return;
576                         }
577                         aed = (struct allocExtDesc *)(nbh->b_data);
578                         aed->previousAllocExtLocation = cpu_to_le32(obloc.logicalBlockNum);
579                         if (nextoffset + adsize > sb->s_blocksize)
580                         {
581                                 loffset = nextoffset;
582                                 aed->lengthAllocDescs = cpu_to_le32(adsize);
583                                 sptr = (obh)->b_data + nextoffset - adsize;
584                                 dptr = nbh->b_data + sizeof(struct allocExtDesc);
585                                 memcpy(dptr, sptr, adsize);
586                                 nextoffset = sizeof(struct allocExtDesc) + adsize;
587                         }
588                         else
589                         {
590                                 loffset = nextoffset + adsize;
591                                 aed->lengthAllocDescs = cpu_to_le32(0);
592                                 sptr = (obh)->b_data + nextoffset;
593                                 nextoffset = sizeof(struct allocExtDesc);
594         
595                                 if (memcmp(&UDF_I_LOCATION(table), &obloc, sizeof(lb_addr)))
596                                 {
597                                         aed = (struct allocExtDesc *)(obh)->b_data;
598                                         aed->lengthAllocDescs =
599                                                 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
600                                 }
601                                 else
602                                 {
603                                         UDF_I_LENALLOC(table) += adsize;
604                                         mark_inode_dirty(table);
605                                 }
606                         }
607                         if (UDF_SB_UDFREV(sb) >= 0x0200)
608                                 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
609                                         nbloc.logicalBlockNum, sizeof(tag));
610                         else
611                                 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
612                                         nbloc.logicalBlockNum, sizeof(tag));
613                         switch (UDF_I_ALLOCTYPE(table))
614                         {
615                                 case ICBTAG_FLAG_AD_SHORT:
616                                 {
617                                         sad = (short_ad *)sptr;
618                                         sad->extLength = cpu_to_le32(
619                                                 EXT_NEXT_EXTENT_ALLOCDECS |
620                                                 sb->s_blocksize);
621                                         sad->extPosition = cpu_to_le32(nbloc.logicalBlockNum);
622                                         break;
623                                 }
624                                 case ICBTAG_FLAG_AD_LONG:
625                                 {
626                                         lad = (long_ad *)sptr;
627                                         lad->extLength = cpu_to_le32(
628                                                 EXT_NEXT_EXTENT_ALLOCDECS |
629                                                 sb->s_blocksize);
630                                         lad->extLocation = cpu_to_lelb(nbloc);
631                                         break;
632                                 }
633                         }
634                         udf_update_tag(obh->b_data, loffset);
635                         mark_buffer_dirty(obh);
636                 }
637
638                 if (elen) /* It's possible that stealing the block emptied the extent */
639                 {
640                         udf_write_aext(table, nbloc, &nextoffset, eloc, elen, nbh, 1);
641
642                         if (!memcmp(&UDF_I_LOCATION(table), &nbloc, sizeof(lb_addr)))
643                         {
644                                 UDF_I_LENALLOC(table) += adsize;
645                                 mark_inode_dirty(table);
646                         }
647                         else
648                         {
649                                 aed = (struct allocExtDesc *)nbh->b_data;
650                                 aed->lengthAllocDescs =
651                                         cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
652                                 udf_update_tag(nbh->b_data, nextoffset);
653                                 mark_buffer_dirty(nbh);
654                         }
655                 }
656         }
657
658         udf_release_data(nbh);
659         udf_release_data(obh);
660
661 error_return:
662         sb->s_dirt = 1;
663         unlock_super(sb);
664         return;
665 }
666
667 static int udf_table_prealloc_blocks(struct super_block * sb,
668         struct inode * inode,
669         struct inode *table, uint16_t partition, uint32_t first_block,
670         uint32_t block_count)
671 {
672         int alloc_count = 0;
673         uint32_t extoffset, elen, adsize;
674         lb_addr bloc, eloc;
675         struct buffer_head *bh;
676         int8_t etype = -1;
677
678         if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
679                 return 0;
680
681         if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
682                 adsize = sizeof(short_ad);
683         else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
684                 adsize = sizeof(long_ad);
685         else
686                 return 0;
687
688         lock_super(sb);
689
690         extoffset = sizeof(struct unallocSpaceEntry);
691         bloc = UDF_I_LOCATION(table);
692
693         bh = udf_tread(sb, udf_get_lb_pblock(sb, bloc, 0));
694         eloc.logicalBlockNum = 0xFFFFFFFF;
695
696         while (first_block != eloc.logicalBlockNum && (etype =
697                 udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
698         {
699                 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
700                         eloc.logicalBlockNum, elen, first_block);
701                 ; /* empty loop body */
702         }
703
704         if (first_block == eloc.logicalBlockNum)
705         {
706                 extoffset -= adsize;
707
708                 alloc_count = (elen >> sb->s_blocksize_bits);
709                 if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count))
710                         alloc_count = 0;
711                 else if (alloc_count > block_count)
712                 {
713                         alloc_count = block_count;
714                         eloc.logicalBlockNum += alloc_count;
715                         elen -= (alloc_count << sb->s_blocksize_bits);
716                         udf_write_aext(table, bloc, &extoffset, eloc, (etype << 30) | elen, bh, 1);
717                 }
718                 else
719                         udf_delete_aext(table, bloc, extoffset, eloc, (etype << 30) | elen, bh);
720         }
721         else
722                 alloc_count = 0;
723
724         udf_release_data(bh);
725
726         if (alloc_count && UDF_SB_LVIDBH(sb))
727         {
728                 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
729                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-alloc_count);
730                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
731                 sb->s_dirt = 1;
732         }
733         unlock_super(sb);
734         return alloc_count;
735 }
736
737 static int udf_table_new_block(struct super_block * sb,
738         struct inode * inode,
739         struct inode *table, uint16_t partition, uint32_t goal, int *err)
740 {
741         uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
742         uint32_t newblock = 0, adsize;
743         uint32_t extoffset, goal_extoffset, elen, goal_elen = 0;
744         lb_addr bloc, goal_bloc, eloc, goal_eloc;
745         struct buffer_head *bh, *goal_bh;
746         int8_t etype;
747
748         *err = -ENOSPC;
749
750         if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
751                 adsize = sizeof(short_ad);
752         else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
753                 adsize = sizeof(long_ad);
754         else
755                 return newblock;
756
757         lock_super(sb);
758
759         if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
760                 goal = 0;
761
762         /* We search for the closest matching block to goal. If we find a exact hit,
763            we stop. Otherwise we keep going till we run out of extents.
764            We store the buffer_head, bloc, and extoffset of the current closest
765            match and use that when we are done.
766         */
767
768         extoffset = sizeof(struct unallocSpaceEntry);
769         bloc = UDF_I_LOCATION(table);
770
771         goal_bh = bh = udf_tread(sb, udf_get_lb_pblock(sb, bloc, 0));
772         atomic_inc(&goal_bh->b_count);
773
774         while (spread && (etype =
775                 udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
776         {
777                 if (goal >= eloc.logicalBlockNum)
778                 {
779                         if (goal < eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits))
780                                 nspread = 0;
781                         else
782                                 nspread = goal - eloc.logicalBlockNum -
783                                         (elen >> sb->s_blocksize_bits);
784                 }
785                 else
786                         nspread = eloc.logicalBlockNum - goal;
787
788                 if (nspread < spread)
789                 {
790                         spread = nspread;
791                         if (goal_bh != bh)
792                         {
793                                 udf_release_data(goal_bh);
794                                 goal_bh = bh;
795                                 atomic_inc(&goal_bh->b_count);
796                         }
797                         goal_bloc = bloc;
798                         goal_extoffset = extoffset - adsize;
799                         goal_eloc = eloc;
800                         goal_elen = (etype << 30) | elen;
801                 }
802         }
803
804         udf_release_data(bh);
805
806         if (spread == 0xFFFFFFFF)
807         {
808                 udf_release_data(goal_bh);
809                 unlock_super(sb);
810                 return 0;
811         }
812
813         /* Only allocate blocks from the beginning of the extent.
814            That way, we only delete (empty) extents, never have to insert an
815            extent because of splitting */
816         /* This works, but very poorly.... */
817
818         newblock = goal_eloc.logicalBlockNum;
819         goal_eloc.logicalBlockNum ++;
820         goal_elen -= sb->s_blocksize;
821
822         if (inode && DQUOT_ALLOC_BLOCK(inode, 1))
823         {
824                 udf_release_data(goal_bh);
825                 unlock_super(sb);
826                 *err = -EDQUOT;
827                 return 0;
828         }
829
830         if (goal_elen)
831                 udf_write_aext(table, goal_bloc, &goal_extoffset, goal_eloc, goal_elen, goal_bh, 1);
832         else
833                 udf_delete_aext(table, goal_bloc, goal_extoffset, goal_eloc, goal_elen, goal_bh);
834         udf_release_data(goal_bh);
835
836         if (UDF_SB_LVIDBH(sb))
837         {
838                 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
839                         cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-1);
840                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
841         }
842
843         sb->s_dirt = 1;
844         unlock_super(sb);
845         *err = 0;
846         return newblock;
847 }
848
849 inline void udf_free_blocks(struct super_block * sb,
850         struct inode * inode,
851         lb_addr bloc, uint32_t offset, uint32_t count)
852 {
853         uint16_t partition = bloc.partitionReferenceNum;
854
855         if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
856         {
857                 return udf_bitmap_free_blocks(sb, inode,
858                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
859                         bloc, offset, count);
860         }
861         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
862         {
863                 return udf_table_free_blocks(sb, inode,
864                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
865                         bloc, offset, count);
866         }
867         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
868         {
869                 return udf_bitmap_free_blocks(sb, inode,
870                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
871                         bloc, offset, count);
872         }
873         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
874         {
875                 return udf_table_free_blocks(sb, inode,
876                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
877                         bloc, offset, count);
878         }
879         else
880                 return;
881 }
882
883 inline int udf_prealloc_blocks(struct super_block * sb,
884         struct inode * inode,
885         uint16_t partition, uint32_t first_block, uint32_t block_count)
886 {
887         if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
888         {
889                 return udf_bitmap_prealloc_blocks(sb, inode,
890                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
891                         partition, first_block, block_count);
892         }
893         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
894         {
895                 return udf_table_prealloc_blocks(sb, inode,
896                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
897                         partition, first_block, block_count);
898         }
899         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
900         {
901                 return udf_bitmap_prealloc_blocks(sb, inode,
902                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
903                         partition, first_block, block_count);
904         }
905         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
906         {
907                 return udf_table_prealloc_blocks(sb, inode,
908                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
909                         partition, first_block, block_count);
910         }
911         else
912                 return 0;
913 }
914
915 inline int udf_new_block(struct super_block * sb,
916         struct inode * inode,
917         uint16_t partition, uint32_t goal, int *err)
918 {
919         if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
920         {
921                 return udf_bitmap_new_block(sb, inode,
922                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
923                         partition, goal, err);
924         }
925         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE)
926         {
927                 return udf_table_new_block(sb, inode,
928                         UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
929                         partition, goal, err);
930         }
931         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
932         {
933                 return udf_bitmap_new_block(sb, inode,
934                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
935                         partition, goal, err);
936         }
937         else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE)
938         {
939                 return udf_table_new_block(sb, inode,
940                         UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
941                         partition, goal, err);
942         }
943         else
944         {
945                 *err = -EIO;
946                 return 0;
947         }
948 }