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