xref: /linux/fs/udf/balloc.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
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 
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 
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 
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 
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 
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 
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 
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 
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 
506 	if (first_block >= sbi->s_partmaps[partition].s_partition_len)
507 		return 0;
508 
509 	iinfo = UDF_I(table);
510 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
511 		adsize = sizeof(struct short_ad);
512 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
513 		adsize = sizeof(struct long_ad);
514 	else
515 		return 0;
516 
517 	mutex_lock(&sbi->s_alloc_mutex);
518 	epos.offset = sizeof(struct unallocSpaceEntry);
519 	epos.block = iinfo->i_location;
520 	epos.bh = NULL;
521 	eloc.logicalBlockNum = 0xFFFFFFFF;
522 
523 	while (first_block != eloc.logicalBlockNum) {
524 		ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
525 		if (ret < 0)
526 			goto err_out;
527 		if (ret == 0)
528 			break;
529 		udf_debug("eloc=%u, elen=%u, first_block=%u\n",
530 			  eloc.logicalBlockNum, elen, first_block);
531 	}
532 
533 	if (first_block == eloc.logicalBlockNum) {
534 		epos.offset -= adsize;
535 
536 		alloc_count = (elen >> sb->s_blocksize_bits);
537 		if (alloc_count > block_count) {
538 			alloc_count = block_count;
539 			eloc.logicalBlockNum += alloc_count;
540 			elen -= (alloc_count << sb->s_blocksize_bits);
541 			udf_write_aext(table, &epos, &eloc,
542 					(etype << 30) | elen, 1);
543 		} else
544 			udf_delete_aext(table, epos);
545 	} else {
546 		alloc_count = 0;
547 	}
548 
549 err_out:
550 	brelse(epos.bh);
551 
552 	if (alloc_count)
553 		udf_add_free_space(sb, partition, -alloc_count);
554 	mutex_unlock(&sbi->s_alloc_mutex);
555 	return alloc_count;
556 }
557 
558 static udf_pblk_t udf_table_new_block(struct super_block *sb,
559 			       struct inode *table, uint16_t partition,
560 			       uint32_t goal, int *err)
561 {
562 	struct udf_sb_info *sbi = UDF_SB(sb);
563 	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
564 	udf_pblk_t newblock = 0;
565 	uint32_t adsize;
566 	uint32_t elen, goal_elen = 0;
567 	struct kernel_lb_addr eloc, goal_eloc;
568 	struct extent_position epos, goal_epos;
569 	int8_t etype;
570 	struct udf_inode_info *iinfo = UDF_I(table);
571 	int ret = 0;
572 
573 	*err = -ENOSPC;
574 
575 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
576 		adsize = sizeof(struct short_ad);
577 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
578 		adsize = sizeof(struct long_ad);
579 	else
580 		return newblock;
581 
582 	mutex_lock(&sbi->s_alloc_mutex);
583 	if (goal >= sbi->s_partmaps[partition].s_partition_len)
584 		goal = 0;
585 
586 	/* We search for the closest matching block to goal. If we find
587 	   a exact hit, we stop. Otherwise we keep going till we run out
588 	   of extents. We store the buffer_head, bloc, and extoffset
589 	   of the current closest match and use that when we are done.
590 	 */
591 	epos.offset = sizeof(struct unallocSpaceEntry);
592 	epos.block = iinfo->i_location;
593 	epos.bh = goal_epos.bh = NULL;
594 
595 	while (spread) {
596 		ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
597 		if (ret <= 0)
598 			break;
599 		if (goal >= eloc.logicalBlockNum) {
600 			if (goal < eloc.logicalBlockNum +
601 					(elen >> sb->s_blocksize_bits))
602 				nspread = 0;
603 			else
604 				nspread = goal - eloc.logicalBlockNum -
605 					(elen >> sb->s_blocksize_bits);
606 		} else {
607 			nspread = eloc.logicalBlockNum - goal;
608 		}
609 
610 		if (nspread < spread) {
611 			spread = nspread;
612 			if (goal_epos.bh != epos.bh) {
613 				brelse(goal_epos.bh);
614 				goal_epos.bh = epos.bh;
615 				get_bh(goal_epos.bh);
616 			}
617 			goal_epos.block = epos.block;
618 			goal_epos.offset = epos.offset - adsize;
619 			goal_eloc = eloc;
620 			goal_elen = (etype << 30) | elen;
621 		}
622 	}
623 
624 	brelse(epos.bh);
625 
626 	if (ret < 0 || spread == 0xFFFFFFFF) {
627 		brelse(goal_epos.bh);
628 		mutex_unlock(&sbi->s_alloc_mutex);
629 		if (ret < 0)
630 			*err = ret;
631 		return 0;
632 	}
633 
634 	/* Only allocate blocks from the beginning of the extent.
635 	   That way, we only delete (empty) extents, never have to insert an
636 	   extent because of splitting */
637 	/* This works, but very poorly.... */
638 
639 	newblock = goal_eloc.logicalBlockNum;
640 	goal_eloc.logicalBlockNum++;
641 	goal_elen -= sb->s_blocksize;
642 
643 	if (goal_elen)
644 		udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
645 	else
646 		udf_delete_aext(table, goal_epos);
647 	brelse(goal_epos.bh);
648 
649 	udf_add_free_space(sb, partition, -1);
650 
651 	mutex_unlock(&sbi->s_alloc_mutex);
652 	*err = 0;
653 	return newblock;
654 }
655 
656 void udf_free_blocks(struct super_block *sb, struct inode *inode,
657 		     struct kernel_lb_addr *bloc, uint32_t offset,
658 		     uint32_t count)
659 {
660 	uint16_t partition = bloc->partitionReferenceNum;
661 	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
662 	uint32_t blk;
663 
664 	if (check_add_overflow(bloc->logicalBlockNum, offset, &blk) ||
665 	    check_add_overflow(blk, count, &blk) ||
666 	    blk > map->s_partition_len) {
667 		udf_debug("Invalid request to free blocks: (%d, %u), off %u, "
668 			  "len %u, partition len %u\n",
669 			  partition, bloc->logicalBlockNum, offset, count,
670 			  map->s_partition_len);
671 		return;
672 	}
673 
674 	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
675 		udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
676 				       bloc, offset, count);
677 	} else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
678 		udf_table_free_blocks(sb, map->s_uspace.s_table,
679 				      bloc, offset, count);
680 	}
681 
682 	if (inode) {
683 		inode_sub_bytes(inode,
684 				((sector_t)count) << sb->s_blocksize_bits);
685 	}
686 }
687 
688 inline int udf_prealloc_blocks(struct super_block *sb,
689 			       struct inode *inode,
690 			       uint16_t partition, uint32_t first_block,
691 			       uint32_t block_count)
692 {
693 	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
694 	int allocated;
695 
696 	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
697 		allocated = udf_bitmap_prealloc_blocks(sb,
698 						       map->s_uspace.s_bitmap,
699 						       partition, first_block,
700 						       block_count);
701 	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
702 		allocated = udf_table_prealloc_blocks(sb,
703 						      map->s_uspace.s_table,
704 						      partition, first_block,
705 						      block_count);
706 	else
707 		return 0;
708 
709 	if (inode && allocated > 0)
710 		inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
711 	return allocated;
712 }
713 
714 inline udf_pblk_t udf_new_block(struct super_block *sb,
715 			 struct inode *inode,
716 			 uint16_t partition, uint32_t goal, int *err)
717 {
718 	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
719 	udf_pblk_t block;
720 
721 	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
722 		block = udf_bitmap_new_block(sb,
723 					     map->s_uspace.s_bitmap,
724 					     partition, goal, err);
725 	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
726 		block = udf_table_new_block(sb,
727 					    map->s_uspace.s_table,
728 					    partition, goal, err);
729 	else {
730 		*err = -EIO;
731 		return 0;
732 	}
733 	if (inode && block)
734 		inode_add_bytes(inode, sb->s_blocksize);
735 	return block;
736 }
737