GNU Linux-libre 4.14.328-gnu1
[releases.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  * 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/bitops.h>
25
26 #include "udf_i.h"
27 #include "udf_sb.h"
28
29 #define udf_clear_bit   __test_and_clear_bit_le
30 #define udf_set_bit     __test_and_set_bit_le
31 #define udf_test_bit    test_bit_le
32 #define udf_find_next_one_bit   find_next_bit_le
33
34 static int read_block_bitmap(struct super_block *sb,
35                              struct udf_bitmap *bitmap, unsigned int block,
36                              unsigned long bitmap_nr)
37 {
38         struct buffer_head *bh = NULL;
39         int i;
40         int max_bits, off, count;
41         struct kernel_lb_addr loc;
42
43         loc.logicalBlockNum = bitmap->s_extPosition;
44         loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
45
46         bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
47         bitmap->s_block_bitmap[bitmap_nr] = bh;
48         if (!bh)
49                 return -EIO;
50
51         /* Check consistency of Space Bitmap buffer. */
52         max_bits = sb->s_blocksize * 8;
53         if (!bitmap_nr) {
54                 off = sizeof(struct spaceBitmapDesc) << 3;
55                 count = min(max_bits - off, bitmap->s_nr_groups);
56         } else {
57                 /*
58                  * Rough check if bitmap number is too big to have any bitmap
59                  * blocks reserved.
60                  */
61                 if (bitmap_nr >
62                     (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2)
63                         return 0;
64                 off = 0;
65                 count = bitmap->s_nr_groups - bitmap_nr * max_bits +
66                                 (sizeof(struct spaceBitmapDesc) << 3);
67                 count = min(count, max_bits);
68         }
69
70         for (i = 0; i < count; i++)
71                 if (udf_test_bit(i + off, bh->b_data))
72                         return -EFSCORRUPTED;
73         return 0;
74 }
75
76 static int __load_block_bitmap(struct super_block *sb,
77                                struct udf_bitmap *bitmap,
78                                unsigned int block_group)
79 {
80         int retval = 0;
81         int nr_groups = bitmap->s_nr_groups;
82
83         if (block_group >= nr_groups) {
84                 udf_debug("block_group (%d) > nr_groups (%d)\n",
85                           block_group, nr_groups);
86         }
87
88         if (bitmap->s_block_bitmap[block_group])
89                 return block_group;
90
91         retval = read_block_bitmap(sb, bitmap, block_group, block_group);
92         if (retval < 0)
93                 return retval;
94
95         return block_group;
96 }
97
98 static inline int load_block_bitmap(struct super_block *sb,
99                                     struct udf_bitmap *bitmap,
100                                     unsigned int block_group)
101 {
102         int slot;
103
104         slot = __load_block_bitmap(sb, bitmap, block_group);
105
106         if (slot < 0)
107                 return slot;
108
109         if (!bitmap->s_block_bitmap[slot])
110                 return -EIO;
111
112         return slot;
113 }
114
115 static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
116 {
117         struct udf_sb_info *sbi = UDF_SB(sb);
118         struct logicalVolIntegrityDesc *lvid;
119
120         if (!sbi->s_lvid_bh)
121                 return;
122
123         lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
124         le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
125         udf_updated_lvid(sb);
126 }
127
128 static void udf_bitmap_free_blocks(struct super_block *sb,
129                                    struct udf_bitmap *bitmap,
130                                    struct kernel_lb_addr *bloc,
131                                    uint32_t offset,
132                                    uint32_t count)
133 {
134         struct udf_sb_info *sbi = UDF_SB(sb);
135         struct buffer_head *bh = NULL;
136         struct udf_part_map *partmap;
137         unsigned long block;
138         unsigned long block_group;
139         unsigned long bit;
140         unsigned long i;
141         int bitmap_nr;
142         unsigned long overflow;
143
144         mutex_lock(&sbi->s_alloc_mutex);
145         partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
146         if (bloc->logicalBlockNum + count < count ||
147             (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
148                 udf_debug("%d < %d || %d + %d > %d\n",
149                           bloc->logicalBlockNum, 0,
150                           bloc->logicalBlockNum, count,
151                           partmap->s_partition_len);
152                 goto error_return;
153         }
154
155         block = bloc->logicalBlockNum + offset +
156                 (sizeof(struct spaceBitmapDesc) << 3);
157
158         do {
159                 overflow = 0;
160                 block_group = block >> (sb->s_blocksize_bits + 3);
161                 bit = block % (sb->s_blocksize << 3);
162
163                 /*
164                 * Check to see if we are freeing blocks across a group boundary.
165                 */
166                 if (bit + count > (sb->s_blocksize << 3)) {
167                         overflow = bit + count - (sb->s_blocksize << 3);
168                         count -= overflow;
169                 }
170                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
171                 if (bitmap_nr < 0)
172                         goto error_return;
173
174                 bh = bitmap->s_block_bitmap[bitmap_nr];
175                 for (i = 0; i < count; i++) {
176                         if (udf_set_bit(bit + i, bh->b_data)) {
177                                 udf_debug("bit %ld already set\n", bit + i);
178                                 udf_debug("byte=%2x\n",
179                                           ((char *)bh->b_data)[(bit + i) >> 3]);
180                         }
181                 }
182                 udf_add_free_space(sb, sbi->s_partition, count);
183                 mark_buffer_dirty(bh);
184                 if (overflow) {
185                         block += count;
186                         count = overflow;
187                 }
188         } while (overflow);
189
190 error_return:
191         mutex_unlock(&sbi->s_alloc_mutex);
192 }
193
194 static int udf_bitmap_prealloc_blocks(struct super_block *sb,
195                                       struct udf_bitmap *bitmap,
196                                       uint16_t partition, uint32_t first_block,
197                                       uint32_t block_count)
198 {
199         struct udf_sb_info *sbi = UDF_SB(sb);
200         int alloc_count = 0;
201         int bit, block, block_group, group_start;
202         int nr_groups, bitmap_nr;
203         struct buffer_head *bh;
204         __u32 part_len;
205
206         mutex_lock(&sbi->s_alloc_mutex);
207         part_len = sbi->s_partmaps[partition].s_partition_len;
208         if (first_block >= part_len)
209                 goto out;
210
211         if (first_block + block_count > part_len)
212                 block_count = part_len - first_block;
213
214         do {
215                 nr_groups = udf_compute_nr_groups(sb, partition);
216                 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
217                 block_group = block >> (sb->s_blocksize_bits + 3);
218                 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
219
220                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
221                 if (bitmap_nr < 0)
222                         goto out;
223                 bh = bitmap->s_block_bitmap[bitmap_nr];
224
225                 bit = block % (sb->s_blocksize << 3);
226
227                 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
228                         if (!udf_clear_bit(bit, bh->b_data))
229                                 goto out;
230                         block_count--;
231                         alloc_count++;
232                         bit++;
233                         block++;
234                 }
235                 mark_buffer_dirty(bh);
236         } while (block_count > 0);
237
238 out:
239         udf_add_free_space(sb, partition, -alloc_count);
240         mutex_unlock(&sbi->s_alloc_mutex);
241         return alloc_count;
242 }
243
244 static int udf_bitmap_new_block(struct super_block *sb,
245                                 struct udf_bitmap *bitmap, uint16_t partition,
246                                 uint32_t goal, int *err)
247 {
248         struct udf_sb_info *sbi = UDF_SB(sb);
249         int newbit, bit = 0, block, block_group, group_start;
250         int end_goal, nr_groups, bitmap_nr, i;
251         struct buffer_head *bh = NULL;
252         char *ptr;
253         int newblock = 0;
254
255         *err = -ENOSPC;
256         mutex_lock(&sbi->s_alloc_mutex);
257
258 repeat:
259         if (goal >= sbi->s_partmaps[partition].s_partition_len)
260                 goal = 0;
261
262         nr_groups = bitmap->s_nr_groups;
263         block = goal + (sizeof(struct spaceBitmapDesc) << 3);
264         block_group = block >> (sb->s_blocksize_bits + 3);
265         group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
266
267         bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
268         if (bitmap_nr < 0)
269                 goto error_return;
270         bh = bitmap->s_block_bitmap[bitmap_nr];
271         ptr = memscan((char *)bh->b_data + group_start, 0xFF,
272                       sb->s_blocksize - group_start);
273
274         if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
275                 bit = block % (sb->s_blocksize << 3);
276                 if (udf_test_bit(bit, bh->b_data))
277                         goto got_block;
278
279                 end_goal = (bit + 63) & ~63;
280                 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
281                 if (bit < end_goal)
282                         goto got_block;
283
284                 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
285                               sb->s_blocksize - ((bit + 7) >> 3));
286                 newbit = (ptr - ((char *)bh->b_data)) << 3;
287                 if (newbit < sb->s_blocksize << 3) {
288                         bit = newbit;
289                         goto search_back;
290                 }
291
292                 newbit = udf_find_next_one_bit(bh->b_data,
293                                                sb->s_blocksize << 3, bit);
294                 if (newbit < sb->s_blocksize << 3) {
295                         bit = newbit;
296                         goto got_block;
297                 }
298         }
299
300         for (i = 0; i < (nr_groups * 2); i++) {
301                 block_group++;
302                 if (block_group >= nr_groups)
303                         block_group = 0;
304                 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
305
306                 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
307                 if (bitmap_nr < 0)
308                         goto error_return;
309                 bh = bitmap->s_block_bitmap[bitmap_nr];
310                 if (i < nr_groups) {
311                         ptr = memscan((char *)bh->b_data + group_start, 0xFF,
312                                       sb->s_blocksize - group_start);
313                         if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
314                                 bit = (ptr - ((char *)bh->b_data)) << 3;
315                                 break;
316                         }
317                 } else {
318                         bit = udf_find_next_one_bit(bh->b_data,
319                                                     sb->s_blocksize << 3,
320                                                     group_start << 3);
321                         if (bit < sb->s_blocksize << 3)
322                                 break;
323                 }
324         }
325         if (i >= (nr_groups * 2)) {
326                 mutex_unlock(&sbi->s_alloc_mutex);
327                 return newblock;
328         }
329         if (bit < sb->s_blocksize << 3)
330                 goto search_back;
331         else
332                 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
333                                             group_start << 3);
334         if (bit >= sb->s_blocksize << 3) {
335                 mutex_unlock(&sbi->s_alloc_mutex);
336                 return 0;
337         }
338
339 search_back:
340         i = 0;
341         while (i < 7 && bit > (group_start << 3) &&
342                udf_test_bit(bit - 1, bh->b_data)) {
343                 ++i;
344                 --bit;
345         }
346
347 got_block:
348         newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
349                 (sizeof(struct spaceBitmapDesc) << 3);
350
351         if (!udf_clear_bit(bit, bh->b_data)) {
352                 udf_debug("bit already cleared for block %d\n", bit);
353                 goto repeat;
354         }
355
356         mark_buffer_dirty(bh);
357
358         udf_add_free_space(sb, partition, -1);
359         mutex_unlock(&sbi->s_alloc_mutex);
360         *err = 0;
361         return newblock;
362
363 error_return:
364         *err = -EIO;
365         mutex_unlock(&sbi->s_alloc_mutex);
366         return 0;
367 }
368
369 static void udf_table_free_blocks(struct super_block *sb,
370                                   struct inode *table,
371                                   struct kernel_lb_addr *bloc,
372                                   uint32_t offset,
373                                   uint32_t count)
374 {
375         struct udf_sb_info *sbi = UDF_SB(sb);
376         struct udf_part_map *partmap;
377         uint32_t start, end;
378         uint32_t elen;
379         struct kernel_lb_addr eloc;
380         struct extent_position oepos, epos;
381         int8_t etype;
382         struct udf_inode_info *iinfo;
383
384         mutex_lock(&sbi->s_alloc_mutex);
385         partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
386         if (bloc->logicalBlockNum + count < count ||
387             (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
388                 udf_debug("%d < %d || %d + %d > %d\n",
389                           bloc->logicalBlockNum, 0,
390                           bloc->logicalBlockNum, count,
391                           partmap->s_partition_len);
392                 goto error_return;
393         }
394
395         iinfo = UDF_I(table);
396         udf_add_free_space(sb, sbi->s_partition, count);
397
398         start = bloc->logicalBlockNum + offset;
399         end = bloc->logicalBlockNum + offset + count - 1;
400
401         epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
402         elen = 0;
403         epos.block = oepos.block = iinfo->i_location;
404         epos.bh = oepos.bh = NULL;
405
406         while (count &&
407                (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
408                 if (((eloc.logicalBlockNum +
409                         (elen >> sb->s_blocksize_bits)) == start)) {
410                         if ((0x3FFFFFFF - elen) <
411                                         (count << sb->s_blocksize_bits)) {
412                                 uint32_t tmp = ((0x3FFFFFFF - elen) >>
413                                                         sb->s_blocksize_bits);
414                                 count -= tmp;
415                                 start += tmp;
416                                 elen = (etype << 30) |
417                                         (0x40000000 - sb->s_blocksize);
418                         } else {
419                                 elen = (etype << 30) |
420                                         (elen +
421                                         (count << sb->s_blocksize_bits));
422                                 start += count;
423                                 count = 0;
424                         }
425                         udf_write_aext(table, &oepos, &eloc, elen, 1);
426                 } else if (eloc.logicalBlockNum == (end + 1)) {
427                         if ((0x3FFFFFFF - elen) <
428                                         (count << sb->s_blocksize_bits)) {
429                                 uint32_t tmp = ((0x3FFFFFFF - elen) >>
430                                                 sb->s_blocksize_bits);
431                                 count -= tmp;
432                                 end -= tmp;
433                                 eloc.logicalBlockNum -= tmp;
434                                 elen = (etype << 30) |
435                                         (0x40000000 - sb->s_blocksize);
436                         } else {
437                                 eloc.logicalBlockNum = start;
438                                 elen = (etype << 30) |
439                                         (elen +
440                                         (count << sb->s_blocksize_bits));
441                                 end -= count;
442                                 count = 0;
443                         }
444                         udf_write_aext(table, &oepos, &eloc, elen, 1);
445                 }
446
447                 if (epos.bh != oepos.bh) {
448                         oepos.block = epos.block;
449                         brelse(oepos.bh);
450                         get_bh(epos.bh);
451                         oepos.bh = epos.bh;
452                         oepos.offset = 0;
453                 } else {
454                         oepos.offset = epos.offset;
455                 }
456         }
457
458         if (count) {
459                 /*
460                  * NOTE: we CANNOT use udf_add_aext here, as it can try to
461                  * allocate a new block, and since we hold the super block
462                  * lock already very bad things would happen :)
463                  *
464                  * We copy the behavior of udf_add_aext, but instead of
465                  * trying to allocate a new block close to the existing one,
466                  * we just steal a block from the extent we are trying to add.
467                  *
468                  * It would be nice if the blocks were close together, but it
469                  * isn't required.
470                  */
471
472                 int adsize;
473
474                 eloc.logicalBlockNum = start;
475                 elen = EXT_RECORDED_ALLOCATED |
476                         (count << sb->s_blocksize_bits);
477
478                 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
479                         adsize = sizeof(struct short_ad);
480                 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
481                         adsize = sizeof(struct long_ad);
482                 else {
483                         brelse(oepos.bh);
484                         brelse(epos.bh);
485                         goto error_return;
486                 }
487
488                 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
489                         /* Steal a block from the extent being free'd */
490                         udf_setup_indirect_aext(table, eloc.logicalBlockNum,
491                                                 &epos);
492
493                         eloc.logicalBlockNum++;
494                         elen -= sb->s_blocksize;
495                 }
496
497                 /* It's possible that stealing the block emptied the extent */
498                 if (elen)
499                         __udf_add_aext(table, &epos, &eloc, elen, 1);
500         }
501
502         brelse(epos.bh);
503         brelse(oepos.bh);
504
505 error_return:
506         mutex_unlock(&sbi->s_alloc_mutex);
507         return;
508 }
509
510 static int udf_table_prealloc_blocks(struct super_block *sb,
511                                      struct inode *table, uint16_t partition,
512                                      uint32_t first_block, uint32_t block_count)
513 {
514         struct udf_sb_info *sbi = UDF_SB(sb);
515         int alloc_count = 0;
516         uint32_t elen, adsize;
517         struct kernel_lb_addr eloc;
518         struct extent_position epos;
519         int8_t etype = -1;
520         struct udf_inode_info *iinfo;
521
522         if (first_block >= sbi->s_partmaps[partition].s_partition_len)
523                 return 0;
524
525         iinfo = UDF_I(table);
526         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
527                 adsize = sizeof(struct short_ad);
528         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
529                 adsize = sizeof(struct long_ad);
530         else
531                 return 0;
532
533         mutex_lock(&sbi->s_alloc_mutex);
534         epos.offset = sizeof(struct unallocSpaceEntry);
535         epos.block = iinfo->i_location;
536         epos.bh = NULL;
537         eloc.logicalBlockNum = 0xFFFFFFFF;
538
539         while (first_block != eloc.logicalBlockNum &&
540                (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
541                 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
542                           eloc.logicalBlockNum, elen, first_block);
543                 ; /* empty loop body */
544         }
545
546         if (first_block == eloc.logicalBlockNum) {
547                 epos.offset -= adsize;
548
549                 alloc_count = (elen >> sb->s_blocksize_bits);
550                 if (alloc_count > block_count) {
551                         alloc_count = block_count;
552                         eloc.logicalBlockNum += alloc_count;
553                         elen -= (alloc_count << sb->s_blocksize_bits);
554                         udf_write_aext(table, &epos, &eloc,
555                                         (etype << 30) | elen, 1);
556                 } else
557                         udf_delete_aext(table, epos);
558         } else {
559                 alloc_count = 0;
560         }
561
562         brelse(epos.bh);
563
564         if (alloc_count)
565                 udf_add_free_space(sb, partition, -alloc_count);
566         mutex_unlock(&sbi->s_alloc_mutex);
567         return alloc_count;
568 }
569
570 static int udf_table_new_block(struct super_block *sb,
571                                struct inode *table, uint16_t partition,
572                                uint32_t goal, int *err)
573 {
574         struct udf_sb_info *sbi = UDF_SB(sb);
575         uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
576         uint32_t newblock = 0, adsize;
577         uint32_t elen, goal_elen = 0;
578         struct kernel_lb_addr eloc, goal_eloc;
579         struct extent_position epos, goal_epos;
580         int8_t etype;
581         struct udf_inode_info *iinfo = UDF_I(table);
582
583         *err = -ENOSPC;
584
585         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
586                 adsize = sizeof(struct short_ad);
587         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
588                 adsize = sizeof(struct long_ad);
589         else
590                 return newblock;
591
592         mutex_lock(&sbi->s_alloc_mutex);
593         if (goal >= sbi->s_partmaps[partition].s_partition_len)
594                 goal = 0;
595
596         /* We search for the closest matching block to goal. If we find
597            a exact hit, we stop. Otherwise we keep going till we run out
598            of extents. We store the buffer_head, bloc, and extoffset
599            of the current closest match and use that when we are done.
600          */
601         epos.offset = sizeof(struct unallocSpaceEntry);
602         epos.block = iinfo->i_location;
603         epos.bh = goal_epos.bh = NULL;
604
605         while (spread &&
606                (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
607                 if (goal >= eloc.logicalBlockNum) {
608                         if (goal < eloc.logicalBlockNum +
609                                         (elen >> sb->s_blocksize_bits))
610                                 nspread = 0;
611                         else
612                                 nspread = goal - eloc.logicalBlockNum -
613                                         (elen >> sb->s_blocksize_bits);
614                 } else {
615                         nspread = eloc.logicalBlockNum - goal;
616                 }
617
618                 if (nspread < spread) {
619                         spread = nspread;
620                         if (goal_epos.bh != epos.bh) {
621                                 brelse(goal_epos.bh);
622                                 goal_epos.bh = epos.bh;
623                                 get_bh(goal_epos.bh);
624                         }
625                         goal_epos.block = epos.block;
626                         goal_epos.offset = epos.offset - adsize;
627                         goal_eloc = eloc;
628                         goal_elen = (etype << 30) | elen;
629                 }
630         }
631
632         brelse(epos.bh);
633
634         if (spread == 0xFFFFFFFF) {
635                 brelse(goal_epos.bh);
636                 mutex_unlock(&sbi->s_alloc_mutex);
637                 return 0;
638         }
639
640         /* Only allocate blocks from the beginning of the extent.
641            That way, we only delete (empty) extents, never have to insert an
642            extent because of splitting */
643         /* This works, but very poorly.... */
644
645         newblock = goal_eloc.logicalBlockNum;
646         goal_eloc.logicalBlockNum++;
647         goal_elen -= sb->s_blocksize;
648
649         if (goal_elen)
650                 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
651         else
652                 udf_delete_aext(table, goal_epos);
653         brelse(goal_epos.bh);
654
655         udf_add_free_space(sb, partition, -1);
656
657         mutex_unlock(&sbi->s_alloc_mutex);
658         *err = 0;
659         return newblock;
660 }
661
662 void udf_free_blocks(struct super_block *sb, struct inode *inode,
663                      struct kernel_lb_addr *bloc, uint32_t offset,
664                      uint32_t count)
665 {
666         uint16_t partition = bloc->partitionReferenceNum;
667         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
668
669         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
670                 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
671                                        bloc, offset, count);
672         } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
673                 udf_table_free_blocks(sb, map->s_uspace.s_table,
674                                       bloc, offset, count);
675         } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
676                 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
677                                        bloc, offset, count);
678         } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
679                 udf_table_free_blocks(sb, map->s_fspace.s_table,
680                                       bloc, offset, count);
681         }
682
683         if (inode) {
684                 inode_sub_bytes(inode,
685                                 ((sector_t)count) << sb->s_blocksize_bits);
686         }
687 }
688
689 inline int udf_prealloc_blocks(struct super_block *sb,
690                                struct inode *inode,
691                                uint16_t partition, uint32_t first_block,
692                                uint32_t block_count)
693 {
694         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
695         int allocated;
696
697         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
698                 allocated = udf_bitmap_prealloc_blocks(sb,
699                                                        map->s_uspace.s_bitmap,
700                                                        partition, first_block,
701                                                        block_count);
702         else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
703                 allocated = udf_table_prealloc_blocks(sb,
704                                                       map->s_uspace.s_table,
705                                                       partition, first_block,
706                                                       block_count);
707         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
708                 allocated = udf_bitmap_prealloc_blocks(sb,
709                                                        map->s_fspace.s_bitmap,
710                                                        partition, first_block,
711                                                        block_count);
712         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
713                 allocated = udf_table_prealloc_blocks(sb,
714                                                       map->s_fspace.s_table,
715                                                       partition, first_block,
716                                                       block_count);
717         else
718                 return 0;
719
720         if (inode && allocated > 0)
721                 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
722         return allocated;
723 }
724
725 inline int udf_new_block(struct super_block *sb,
726                          struct inode *inode,
727                          uint16_t partition, uint32_t goal, int *err)
728 {
729         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
730         int block;
731
732         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
733                 block = udf_bitmap_new_block(sb,
734                                              map->s_uspace.s_bitmap,
735                                              partition, goal, err);
736         else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
737                 block = udf_table_new_block(sb,
738                                             map->s_uspace.s_table,
739                                             partition, goal, err);
740         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
741                 block = udf_bitmap_new_block(sb,
742                                              map->s_fspace.s_bitmap,
743                                              partition, goal, err);
744         else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
745                 block = udf_table_new_block(sb,
746                                             map->s_fspace.s_table,
747                                             partition, goal, err);
748         else {
749                 *err = -EIO;
750                 return 0;
751         }
752         if (inode && block)
753                 inode_add_bytes(inode, sb->s_blocksize);
754         return block;
755 }