udf: fix default mode and dmode options handling
[safe/jmp/linux-2.6] / fs / udf / balloc.c
1 /*
2  * balloc.c
3  *
4  * PURPOSE
5  *      Block allocation handling routines for the OSTA-UDF(tm) filesystem.
6  *
7  * COPYRIGHT
8  *      This file is distributed under the terms of the GNU General Public
9  *      License (GPL). Copies of the GPL can be obtained from:
10  *              ftp://prep.ai.mit.edu/pub/gnu/GPL
11  *      Each contributing author retains all rights to their own work.
12  *
13  *  (C) 1999-2001 Ben Fennema
14  *  (C) 1999 Stelias Computing Inc
15  *
16  * HISTORY
17  *
18  *  02/24/99 blf  Created.
19  *
20  */
21
22 #include "udfdecl.h"
23
24 #include <linux/quotaops.h>
25 #include <linux/buffer_head.h>
26 #include <linux/bitops.h>
27
28 #include "udf_i.h"
29 #include "udf_sb.h"
30
31 #define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
32 #define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
33 #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
34 #define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size)
35 #define udf_find_next_one_bit(addr, size, offset) \
36                 find_next_one_bit(addr, size, offset)
37
38 #define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
39 #define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y)
40 #define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y))
41 #define uintBPL_t uint(BITS_PER_LONG)
42 #define uint(x) xuint(x)
43 #define xuint(x) __le ## x
44
45 static inline int find_next_one_bit(void *addr, int size, int offset)
46 {
47         uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
48         int result = offset & ~(BITS_PER_LONG - 1);
49         unsigned long tmp;
50
51         if (offset >= size)
52                 return size;
53         size -= result;
54         offset &= (BITS_PER_LONG - 1);
55         if (offset) {
56                 tmp = leBPL_to_cpup(p++);
57                 tmp &= ~0UL << offset;
58                 if (size < BITS_PER_LONG)
59                         goto found_first;
60                 if (tmp)
61                         goto found_middle;
62                 size -= BITS_PER_LONG;
63                 result += BITS_PER_LONG;
64         }
65         while (size & ~(BITS_PER_LONG - 1)) {
66                 tmp = leBPL_to_cpup(p++);
67                 if (tmp)
68                         goto found_middle;
69                 result += BITS_PER_LONG;
70                 size -= BITS_PER_LONG;
71         }
72         if (!size)
73                 return result;
74         tmp = leBPL_to_cpup(p);
75 found_first:
76         tmp &= ~0UL >> (BITS_PER_LONG - size);
77 found_middle:
78         return result + ffz(~tmp);
79 }
80
81 #define find_first_one_bit(addr, size)\
82         find_next_one_bit((addr), (size), 0)
83
84 static int read_block_bitmap(struct super_block *sb,
85                              struct udf_bitmap *bitmap, unsigned int block,
86                              unsigned long bitmap_nr)
87 {
88         struct buffer_head *bh = NULL;
89         int retval = 0;
90         struct kernel_lb_addr loc;
91
92         loc.logicalBlockNum = bitmap->s_extPosition;
93         loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
94
95         bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
96         if (!bh)
97                 retval = -EIO;
98
99         bitmap->s_block_bitmap[bitmap_nr] = bh;
100         return retval;
101 }
102
103 static int __load_block_bitmap(struct super_block *sb,
104                                struct udf_bitmap *bitmap,
105                                unsigned int block_group)
106 {
107         int retval = 0;
108         int nr_groups = bitmap->s_nr_groups;
109
110         if (block_group >= nr_groups) {
111                 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
112                           nr_groups);
113         }
114
115         if (bitmap->s_block_bitmap[block_group]) {
116                 return block_group;
117         } else {
118                 retval = read_block_bitmap(sb, bitmap, block_group,
119                                            block_group);
120                 if (retval < 0)
121                         return retval;
122                 return block_group;
123         }
124 }
125
126 static inline int load_block_bitmap(struct super_block *sb,
127                                     struct udf_bitmap *bitmap,
128                                     unsigned int block_group)
129 {
130         int slot;
131
132         slot = __load_block_bitmap(sb, bitmap, block_group);
133
134         if (slot < 0)
135                 return slot;
136
137         if (!bitmap->s_block_bitmap[slot])
138                 return -EIO;
139
140         return slot;
141 }
142
143 static bool udf_add_free_space(struct udf_sb_info *sbi,
144                                 u16 partition, u32 cnt)
145 {
146         struct logicalVolIntegrityDesc *lvid;
147
148         if (sbi->s_lvid_bh == NULL)
149                 return false;
150
151         lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
152         le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
153         return true;
154 }
155
156 static void udf_bitmap_free_blocks(struct super_block *sb,
157                                    struct inode *inode,
158                                    struct udf_bitmap *bitmap,
159                                    struct kernel_lb_addr *bloc,
160                                    uint32_t offset,
161                                    uint32_t count)
162 {
163         struct udf_sb_info *sbi = UDF_SB(sb);
164         struct buffer_head *bh = NULL;
165         struct udf_part_map *partmap;
166         unsigned long block;
167         unsigned long block_group;
168         unsigned long bit;
169         unsigned long i;
170         int bitmap_nr;
171         unsigned long overflow;
172
173         mutex_lock(&sbi->s_alloc_mutex);
174         partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
175         if (bloc->logicalBlockNum < 0 ||
176             (bloc->logicalBlockNum + count) >
177                 partmap->s_partition_len) {
178                 udf_debug("%d < %d || %d + %d > %d\n",
179                           bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
180                           count, partmap->s_partition_len);
181                 goto error_return;
182         }
183
184         block = bloc->logicalBlockNum + offset +
185                 (sizeof(struct spaceBitmapDesc) << 3);
186
187         do {
188                 overflow = 0;
189                 block_group = block >> (sb->s_blocksize_bits + 3);
190                 bit = block % (sb->s_blocksize << 3);
191
192                 /*
193                 * Check to see if we are freeing blocks across a group boundary.
194                 */
195                 if (bit + count > (sb->s_blocksize << 3)) {
196                         overflow = bit + count - (sb->s_blocksize << 3);
197                         count -= overflow;
198                 }
199                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
200                 if (bitmap_nr < 0)
201                         goto error_return;
202
203                 bh = bitmap->s_block_bitmap[bitmap_nr];
204                 for (i = 0; i < count; i++) {
205                         if (udf_set_bit(bit + i, bh->b_data)) {
206                                 udf_debug("bit %ld already set\n", bit + i);
207                                 udf_debug("byte=%2x\n",
208                                         ((char *)bh->b_data)[(bit + i) >> 3]);
209                         } else {
210                                 if (inode)
211                                         vfs_dq_free_block(inode, 1);
212                                 udf_add_free_space(sbi, sbi->s_partition, 1);
213                         }
214                 }
215                 mark_buffer_dirty(bh);
216                 if (overflow) {
217                         block += count;
218                         count = overflow;
219                 }
220         } while (overflow);
221
222 error_return:
223         sb->s_dirt = 1;
224         if (sbi->s_lvid_bh)
225                 mark_buffer_dirty(sbi->s_lvid_bh);
226         mutex_unlock(&sbi->s_alloc_mutex);
227 }
228
229 static int udf_bitmap_prealloc_blocks(struct super_block *sb,
230                                       struct inode *inode,
231                                       struct udf_bitmap *bitmap,
232                                       uint16_t partition, uint32_t first_block,
233                                       uint32_t block_count)
234 {
235         struct udf_sb_info *sbi = UDF_SB(sb);
236         int alloc_count = 0;
237         int bit, block, block_group, group_start;
238         int nr_groups, bitmap_nr;
239         struct buffer_head *bh;
240         __u32 part_len;
241
242         mutex_lock(&sbi->s_alloc_mutex);
243         part_len = sbi->s_partmaps[partition].s_partition_len;
244         if (first_block < 0 || first_block >= part_len)
245                 goto out;
246
247         if (first_block + block_count > part_len)
248                 block_count = part_len - first_block;
249
250         do {
251                 nr_groups = udf_compute_nr_groups(sb, partition);
252                 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
253                 block_group = block >> (sb->s_blocksize_bits + 3);
254                 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
255
256                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
257                 if (bitmap_nr < 0)
258                         goto out;
259                 bh = bitmap->s_block_bitmap[bitmap_nr];
260
261                 bit = block % (sb->s_blocksize << 3);
262
263                 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
264                         if (!udf_test_bit(bit, bh->b_data))
265                                 goto out;
266                         else if (vfs_dq_prealloc_block(inode, 1))
267                                 goto out;
268                         else if (!udf_clear_bit(bit, bh->b_data)) {
269                                 udf_debug("bit already cleared for block %d\n", bit);
270                                 vfs_dq_free_block(inode, 1);
271                                 goto out;
272                         }
273                         block_count--;
274                         alloc_count++;
275                         bit++;
276                         block++;
277                 }
278                 mark_buffer_dirty(bh);
279         } while (block_count > 0);
280
281 out:
282         if (udf_add_free_space(sbi, partition, -alloc_count))
283                 mark_buffer_dirty(sbi->s_lvid_bh);
284         sb->s_dirt = 1;
285         mutex_unlock(&sbi->s_alloc_mutex);
286         return alloc_count;
287 }
288
289 static int udf_bitmap_new_block(struct super_block *sb,
290                                 struct inode *inode,
291                                 struct udf_bitmap *bitmap, uint16_t partition,
292                                 uint32_t goal, int *err)
293 {
294         struct udf_sb_info *sbi = UDF_SB(sb);
295         int newbit, bit = 0, block, block_group, group_start;
296         int end_goal, nr_groups, bitmap_nr, i;
297         struct buffer_head *bh = NULL;
298         char *ptr;
299         int newblock = 0;
300
301         *err = -ENOSPC;
302         mutex_lock(&sbi->s_alloc_mutex);
303
304 repeat:
305         if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
306                 goal = 0;
307
308         nr_groups = bitmap->s_nr_groups;
309         block = goal + (sizeof(struct spaceBitmapDesc) << 3);
310         block_group = block >> (sb->s_blocksize_bits + 3);
311         group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
312
313         bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
314         if (bitmap_nr < 0)
315                 goto error_return;
316         bh = bitmap->s_block_bitmap[bitmap_nr];
317         ptr = memscan((char *)bh->b_data + group_start, 0xFF,
318                       sb->s_blocksize - group_start);
319
320         if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
321                 bit = block % (sb->s_blocksize << 3);
322                 if (udf_test_bit(bit, bh->b_data))
323                         goto got_block;
324
325                 end_goal = (bit + 63) & ~63;
326                 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
327                 if (bit < end_goal)
328                         goto got_block;
329
330                 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
331                               sb->s_blocksize - ((bit + 7) >> 3));
332                 newbit = (ptr - ((char *)bh->b_data)) << 3;
333                 if (newbit < sb->s_blocksize << 3) {
334                         bit = newbit;
335                         goto search_back;
336                 }
337
338                 newbit = udf_find_next_one_bit(bh->b_data,
339                                                sb->s_blocksize << 3, bit);
340                 if (newbit < sb->s_blocksize << 3) {
341                         bit = newbit;
342                         goto got_block;
343                 }
344         }
345
346         for (i = 0; i < (nr_groups * 2); i++) {
347                 block_group++;
348                 if (block_group >= nr_groups)
349                         block_group = 0;
350                 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
351
352                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
353                 if (bitmap_nr < 0)
354                         goto error_return;
355                 bh = bitmap->s_block_bitmap[bitmap_nr];
356                 if (i < nr_groups) {
357                         ptr = memscan((char *)bh->b_data + group_start, 0xFF,
358                                       sb->s_blocksize - group_start);
359                         if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
360                                 bit = (ptr - ((char *)bh->b_data)) << 3;
361                                 break;
362                         }
363                 } else {
364                         bit = udf_find_next_one_bit((char *)bh->b_data,
365                                                     sb->s_blocksize << 3,
366                                                     group_start << 3);
367                         if (bit < sb->s_blocksize << 3)
368                                 break;
369                 }
370         }
371         if (i >= (nr_groups * 2)) {
372                 mutex_unlock(&sbi->s_alloc_mutex);
373                 return newblock;
374         }
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,
379                                             group_start << 3);
380         if (bit >= sb->s_blocksize << 3) {
381                 mutex_unlock(&sbi->s_alloc_mutex);
382                 return 0;
383         }
384
385 search_back:
386         i = 0;
387         while (i < 7 && bit > (group_start << 3) &&
388                udf_test_bit(bit - 1, bh->b_data)) {
389                 ++i;
390                 --bit;
391         }
392
393 got_block:
394
395         /*
396          * Check quota for allocation of this block.
397          */
398         if (inode && vfs_dq_alloc_block(inode, 1)) {
399                 mutex_unlock(&sbi->s_alloc_mutex);
400                 *err = -EDQUOT;
401                 return 0;
402         }
403
404         newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
405                 (sizeof(struct spaceBitmapDesc) << 3);
406
407         if (!udf_clear_bit(bit, bh->b_data)) {
408                 udf_debug("bit already cleared for block %d\n", bit);
409                 goto repeat;
410         }
411
412         mark_buffer_dirty(bh);
413
414         if (udf_add_free_space(sbi, partition, -1))
415                 mark_buffer_dirty(sbi->s_lvid_bh);
416         sb->s_dirt = 1;
417         mutex_unlock(&sbi->s_alloc_mutex);
418         *err = 0;
419         return newblock;
420
421 error_return:
422         *err = -EIO;
423         mutex_unlock(&sbi->s_alloc_mutex);
424         return 0;
425 }
426
427 static void udf_table_free_blocks(struct super_block *sb,
428                                   struct inode *inode,
429                                   struct inode *table,
430                                   struct kernel_lb_addr *bloc,
431                                   uint32_t offset,
432                                   uint32_t count)
433 {
434         struct udf_sb_info *sbi = UDF_SB(sb);
435         struct udf_part_map *partmap;
436         uint32_t start, end;
437         uint32_t elen;
438         struct kernel_lb_addr eloc;
439         struct extent_position oepos, epos;
440         int8_t etype;
441         int i;
442         struct udf_inode_info *iinfo;
443
444         mutex_lock(&sbi->s_alloc_mutex);
445         partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
446         if (bloc->logicalBlockNum < 0 ||
447             (bloc->logicalBlockNum + count) >
448                 partmap->s_partition_len) {
449                 udf_debug("%d < %d || %d + %d > %d\n",
450                           bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
451                           partmap->s_partition_len);
452                 goto error_return;
453         }
454
455         iinfo = UDF_I(table);
456         /* We do this up front - There are some error conditions that
457            could occure, but.. oh well */
458         if (inode)
459                 vfs_dq_free_block(inode, count);
460         if (udf_add_free_space(sbi, sbi->s_partition, count))
461                 mark_buffer_dirty(sbi->s_lvid_bh);
462
463         start = bloc->logicalBlockNum + offset;
464         end = bloc->logicalBlockNum + offset + count - 1;
465
466         epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
467         elen = 0;
468         epos.block = oepos.block = iinfo->i_location;
469         epos.bh = oepos.bh = NULL;
470
471         while (count &&
472                (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
473                 if (((eloc.logicalBlockNum +
474                         (elen >> sb->s_blocksize_bits)) == start)) {
475                         if ((0x3FFFFFFF - elen) <
476                                         (count << sb->s_blocksize_bits)) {
477                                 uint32_t tmp = ((0x3FFFFFFF - elen) >>
478                                                         sb->s_blocksize_bits);
479                                 count -= tmp;
480                                 start += tmp;
481                                 elen = (etype << 30) |
482                                         (0x40000000 - sb->s_blocksize);
483                         } else {
484                                 elen = (etype << 30) |
485                                         (elen +
486                                         (count << sb->s_blocksize_bits));
487                                 start += count;
488                                 count = 0;
489                         }
490                         udf_write_aext(table, &oepos, &eloc, elen, 1);
491                 } else if (eloc.logicalBlockNum == (end + 1)) {
492                         if ((0x3FFFFFFF - elen) <
493                                         (count << sb->s_blocksize_bits)) {
494                                 uint32_t tmp = ((0x3FFFFFFF - elen) >>
495                                                 sb->s_blocksize_bits);
496                                 count -= tmp;
497                                 end -= tmp;
498                                 eloc.logicalBlockNum -= tmp;
499                                 elen = (etype << 30) |
500                                         (0x40000000 - sb->s_blocksize);
501                         } else {
502                                 eloc.logicalBlockNum = start;
503                                 elen = (etype << 30) |
504                                         (elen +
505                                         (count << sb->s_blocksize_bits));
506                                 end -= count;
507                                 count = 0;
508                         }
509                         udf_write_aext(table, &oepos, &eloc, elen, 1);
510                 }
511
512                 if (epos.bh != oepos.bh) {
513                         i = -1;
514                         oepos.block = epos.block;
515                         brelse(oepos.bh);
516                         get_bh(epos.bh);
517                         oepos.bh = epos.bh;
518                         oepos.offset = 0;
519                 } else {
520                         oepos.offset = epos.offset;
521                 }
522         }
523
524         if (count) {
525                 /*
526                  * NOTE: we CANNOT use udf_add_aext here, as it can try to
527                  * allocate a new block, and since we hold the super block
528                  * lock already very bad things would happen :)
529                  *
530                  * We copy the behavior of udf_add_aext, but instead of
531                  * trying to allocate a new block close to the existing one,
532                  * we just steal a block from the extent we are trying to add.
533                  *
534                  * It would be nice if the blocks were close together, but it
535                  * isn't required.
536                  */
537
538                 int adsize;
539                 struct short_ad *sad = NULL;
540                 struct long_ad *lad = NULL;
541                 struct allocExtDesc *aed;
542
543                 eloc.logicalBlockNum = start;
544                 elen = EXT_RECORDED_ALLOCATED |
545                         (count << sb->s_blocksize_bits);
546
547                 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
548                         adsize = sizeof(struct short_ad);
549                 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
550                         adsize = sizeof(struct long_ad);
551                 else {
552                         brelse(oepos.bh);
553                         brelse(epos.bh);
554                         goto error_return;
555                 }
556
557                 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
558                         char *sptr, *dptr;
559                         int loffset;
560
561                         brelse(oepos.bh);
562                         oepos = epos;
563
564                         /* Steal a block from the extent being free'd */
565                         epos.block.logicalBlockNum = eloc.logicalBlockNum;
566                         eloc.logicalBlockNum++;
567                         elen -= sb->s_blocksize;
568
569                         epos.bh = udf_tread(sb,
570                                         udf_get_lb_pblock(sb, &epos.block, 0));
571                         if (!epos.bh) {
572                                 brelse(oepos.bh);
573                                 goto error_return;
574                         }
575                         aed = (struct allocExtDesc *)(epos.bh->b_data);
576                         aed->previousAllocExtLocation =
577                                 cpu_to_le32(oepos.block.logicalBlockNum);
578                         if (epos.offset + adsize > sb->s_blocksize) {
579                                 loffset = epos.offset;
580                                 aed->lengthAllocDescs = cpu_to_le32(adsize);
581                                 sptr = iinfo->i_ext.i_data + epos.offset
582                                                                 - adsize;
583                                 dptr = epos.bh->b_data +
584                                         sizeof(struct allocExtDesc);
585                                 memcpy(dptr, sptr, adsize);
586                                 epos.offset = sizeof(struct allocExtDesc) +
587                                                 adsize;
588                         } else {
589                                 loffset = epos.offset + adsize;
590                                 aed->lengthAllocDescs = cpu_to_le32(0);
591                                 if (oepos.bh) {
592                                         sptr = oepos.bh->b_data + epos.offset;
593                                         aed = (struct allocExtDesc *)
594                                                 oepos.bh->b_data;
595                                         le32_add_cpu(&aed->lengthAllocDescs,
596                                                         adsize);
597                                 } else {
598                                         sptr = iinfo->i_ext.i_data +
599                                                                 epos.offset;
600                                         iinfo->i_lenAlloc += adsize;
601                                         mark_inode_dirty(table);
602                                 }
603                                 epos.offset = sizeof(struct allocExtDesc);
604                         }
605                         if (sbi->s_udfrev >= 0x0200)
606                                 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
607                                             3, 1, epos.block.logicalBlockNum,
608                                             sizeof(struct tag));
609                         else
610                                 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
611                                             2, 1, epos.block.logicalBlockNum,
612                                             sizeof(struct tag));
613
614                         switch (iinfo->i_alloc_type) {
615                         case ICBTAG_FLAG_AD_SHORT:
616                                 sad = (struct short_ad *)sptr;
617                                 sad->extLength = cpu_to_le32(
618                                         EXT_NEXT_EXTENT_ALLOCDECS |
619                                         sb->s_blocksize);
620                                 sad->extPosition =
621                                         cpu_to_le32(epos.block.logicalBlockNum);
622                                 break;
623                         case ICBTAG_FLAG_AD_LONG:
624                                 lad = (struct long_ad *)sptr;
625                                 lad->extLength = cpu_to_le32(
626                                         EXT_NEXT_EXTENT_ALLOCDECS |
627                                         sb->s_blocksize);
628                                 lad->extLocation =
629                                         cpu_to_lelb(epos.block);
630                                 break;
631                         }
632                         if (oepos.bh) {
633                                 udf_update_tag(oepos.bh->b_data, loffset);
634                                 mark_buffer_dirty(oepos.bh);
635                         } else {
636                                 mark_inode_dirty(table);
637                         }
638                 }
639
640                 /* It's possible that stealing the block emptied the extent */
641                 if (elen) {
642                         udf_write_aext(table, &epos, &eloc, elen, 1);
643
644                         if (!epos.bh) {
645                                 iinfo->i_lenAlloc += adsize;
646                                 mark_inode_dirty(table);
647                         } else {
648                                 aed = (struct allocExtDesc *)epos.bh->b_data;
649                                 le32_add_cpu(&aed->lengthAllocDescs, adsize);
650                                 udf_update_tag(epos.bh->b_data, epos.offset);
651                                 mark_buffer_dirty(epos.bh);
652                         }
653                 }
654         }
655
656         brelse(epos.bh);
657         brelse(oepos.bh);
658
659 error_return:
660         sb->s_dirt = 1;
661         mutex_unlock(&sbi->s_alloc_mutex);
662         return;
663 }
664
665 static int udf_table_prealloc_blocks(struct super_block *sb,
666                                      struct inode *inode,
667                                      struct inode *table, uint16_t partition,
668                                      uint32_t first_block, uint32_t block_count)
669 {
670         struct udf_sb_info *sbi = UDF_SB(sb);
671         int alloc_count = 0;
672         uint32_t elen, adsize;
673         struct kernel_lb_addr eloc;
674         struct extent_position epos;
675         int8_t etype = -1;
676         struct udf_inode_info *iinfo;
677
678         if (first_block < 0 ||
679                 first_block >= sbi->s_partmaps[partition].s_partition_len)
680                 return 0;
681
682         iinfo = UDF_I(table);
683         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
684                 adsize = sizeof(struct short_ad);
685         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
686                 adsize = sizeof(struct long_ad);
687         else
688                 return 0;
689
690         mutex_lock(&sbi->s_alloc_mutex);
691         epos.offset = sizeof(struct unallocSpaceEntry);
692         epos.block = iinfo->i_location;
693         epos.bh = NULL;
694         eloc.logicalBlockNum = 0xFFFFFFFF;
695
696         while (first_block != eloc.logicalBlockNum &&
697                (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
698                 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
699                           eloc.logicalBlockNum, elen, first_block);
700                 ; /* empty loop body */
701         }
702
703         if (first_block == eloc.logicalBlockNum) {
704                 epos.offset -= adsize;
705
706                 alloc_count = (elen >> sb->s_blocksize_bits);
707                 if (inode && vfs_dq_prealloc_block(inode,
708                         alloc_count > block_count ? block_count : alloc_count))
709                         alloc_count = 0;
710                 else if (alloc_count > block_count) {
711                         alloc_count = block_count;
712                         eloc.logicalBlockNum += alloc_count;
713                         elen -= (alloc_count << sb->s_blocksize_bits);
714                         udf_write_aext(table, &epos, &eloc,
715                                         (etype << 30) | elen, 1);
716                 } else
717                         udf_delete_aext(table, epos, eloc,
718                                         (etype << 30) | elen);
719         } else {
720                 alloc_count = 0;
721         }
722
723         brelse(epos.bh);
724
725         if (alloc_count && udf_add_free_space(sbi, partition, -alloc_count)) {
726                 mark_buffer_dirty(sbi->s_lvid_bh);
727                 sb->s_dirt = 1;
728         }
729         mutex_unlock(&sbi->s_alloc_mutex);
730         return alloc_count;
731 }
732
733 static int udf_table_new_block(struct super_block *sb,
734                                struct inode *inode,
735                                struct inode *table, uint16_t partition,
736                                uint32_t goal, int *err)
737 {
738         struct udf_sb_info *sbi = UDF_SB(sb);
739         uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
740         uint32_t newblock = 0, adsize;
741         uint32_t elen, goal_elen = 0;
742         struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
743         struct extent_position epos, goal_epos;
744         int8_t etype;
745         struct udf_inode_info *iinfo = UDF_I(table);
746
747         *err = -ENOSPC;
748
749         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
750                 adsize = sizeof(struct short_ad);
751         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
752                 adsize = sizeof(struct long_ad);
753         else
754                 return newblock;
755
756         mutex_lock(&sbi->s_alloc_mutex);
757         if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
758                 goal = 0;
759
760         /* We search for the closest matching block to goal. If we find
761            a exact hit, we stop. Otherwise we keep going till we run out
762            of extents. We store the buffer_head, bloc, and extoffset
763            of the current closest match and use that when we are done.
764          */
765         epos.offset = sizeof(struct unallocSpaceEntry);
766         epos.block = iinfo->i_location;
767         epos.bh = goal_epos.bh = NULL;
768
769         while (spread &&
770                (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
771                 if (goal >= eloc.logicalBlockNum) {
772                         if (goal < eloc.logicalBlockNum +
773                                         (elen >> sb->s_blocksize_bits))
774                                 nspread = 0;
775                         else
776                                 nspread = goal - eloc.logicalBlockNum -
777                                         (elen >> sb->s_blocksize_bits);
778                 } else {
779                         nspread = eloc.logicalBlockNum - goal;
780                 }
781
782                 if (nspread < spread) {
783                         spread = nspread;
784                         if (goal_epos.bh != epos.bh) {
785                                 brelse(goal_epos.bh);
786                                 goal_epos.bh = epos.bh;
787                                 get_bh(goal_epos.bh);
788                         }
789                         goal_epos.block = epos.block;
790                         goal_epos.offset = epos.offset - adsize;
791                         goal_eloc = eloc;
792                         goal_elen = (etype << 30) | elen;
793                 }
794         }
795
796         brelse(epos.bh);
797
798         if (spread == 0xFFFFFFFF) {
799                 brelse(goal_epos.bh);
800                 mutex_unlock(&sbi->s_alloc_mutex);
801                 return 0;
802         }
803
804         /* Only allocate blocks from the beginning of the extent.
805            That way, we only delete (empty) extents, never have to insert an
806            extent because of splitting */
807         /* This works, but very poorly.... */
808
809         newblock = goal_eloc.logicalBlockNum;
810         goal_eloc.logicalBlockNum++;
811         goal_elen -= sb->s_blocksize;
812
813         if (inode && vfs_dq_alloc_block(inode, 1)) {
814                 brelse(goal_epos.bh);
815                 mutex_unlock(&sbi->s_alloc_mutex);
816                 *err = -EDQUOT;
817                 return 0;
818         }
819
820         if (goal_elen)
821                 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
822         else
823                 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
824         brelse(goal_epos.bh);
825
826         if (udf_add_free_space(sbi, partition, -1))
827                 mark_buffer_dirty(sbi->s_lvid_bh);
828
829         sb->s_dirt = 1;
830         mutex_unlock(&sbi->s_alloc_mutex);
831         *err = 0;
832         return newblock;
833 }
834
835 void udf_free_blocks(struct super_block *sb, struct inode *inode,
836                      struct kernel_lb_addr *bloc, uint32_t offset,
837                      uint32_t count)
838 {
839         uint16_t partition = bloc->partitionReferenceNum;
840         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
841
842         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
843                 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
844                                        bloc, offset, count);
845         } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
846                 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
847                                       bloc, offset, count);
848         } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
849                 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
850                                        bloc, offset, count);
851         } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
852                 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
853                                       bloc, offset, count);
854         }
855 }
856
857 inline int udf_prealloc_blocks(struct super_block *sb,
858                                struct inode *inode,
859                                uint16_t partition, uint32_t first_block,
860                                uint32_t block_count)
861 {
862         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
863
864         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
865                 return udf_bitmap_prealloc_blocks(sb, inode,
866                                                   map->s_uspace.s_bitmap,
867                                                   partition, first_block,
868                                                   block_count);
869         else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
870                 return udf_table_prealloc_blocks(sb, inode,
871                                                  map->s_uspace.s_table,
872                                                  partition, first_block,
873                                                  block_count);
874         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
875                 return udf_bitmap_prealloc_blocks(sb, inode,
876                                                   map->s_fspace.s_bitmap,
877                                                   partition, first_block,
878                                                   block_count);
879         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
880                 return udf_table_prealloc_blocks(sb, inode,
881                                                  map->s_fspace.s_table,
882                                                  partition, first_block,
883                                                  block_count);
884         else
885                 return 0;
886 }
887
888 inline int udf_new_block(struct super_block *sb,
889                          struct inode *inode,
890                          uint16_t partition, uint32_t goal, int *err)
891 {
892         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
893
894         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
895                 return udf_bitmap_new_block(sb, inode,
896                                            map->s_uspace.s_bitmap,
897                                            partition, goal, err);
898         else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
899                 return udf_table_new_block(sb, inode,
900                                            map->s_uspace.s_table,
901                                            partition, goal, err);
902         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
903                 return udf_bitmap_new_block(sb, inode,
904                                             map->s_fspace.s_bitmap,
905                                             partition, goal, err);
906         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
907                 return udf_table_new_block(sb, inode,
908                                            map->s_fspace.s_table,
909                                            partition, goal, err);
910         else {
911                 *err = -EIO;
912                 return 0;
913         }
914 }