balloc.c 19.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * balloc.c
 *
 * PURPOSE
 *	Block allocation handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *  (C) 1999-2001 Ben Fennema
 *  (C) 1999 Stelias Computing Inc
 *
 * HISTORY
 *
 *  02/24/99 blf  Created.
 *
 */

#include "udfdecl.h"

#include <linux/bitops.h>

#include "udf_i.h"
#include "udf_sb.h"

A
Akinobu Mita 已提交
29 30 31 32
#define udf_clear_bit	__test_and_clear_bit_le
#define udf_set_bit	__test_and_set_bit_le
#define udf_test_bit	test_bit_le
#define udf_find_next_one_bit	find_next_bit_le
L
Linus Torvalds 已提交
33

34 35 36
static int read_block_bitmap(struct super_block *sb,
			     struct udf_bitmap *bitmap, unsigned int block,
			     unsigned long bitmap_nr)
L
Linus Torvalds 已提交
37 38 39
{
	struct buffer_head *bh = NULL;
	int retval = 0;
40
	struct kernel_lb_addr loc;
L
Linus Torvalds 已提交
41 42

	loc.logicalBlockNum = bitmap->s_extPosition;
M
Marcin Slusarz 已提交
43
	loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
L
Linus Torvalds 已提交
44

45
	bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
M
Marcin Slusarz 已提交
46
	if (!bh)
L
Linus Torvalds 已提交
47
		retval = -EIO;
M
Marcin Slusarz 已提交
48

L
Linus Torvalds 已提交
49 50 51 52
	bitmap->s_block_bitmap[bitmap_nr] = bh;
	return retval;
}

53 54 55
static int __load_block_bitmap(struct super_block *sb,
			       struct udf_bitmap *bitmap,
			       unsigned int block_group)
L
Linus Torvalds 已提交
56 57 58 59
{
	int retval = 0;
	int nr_groups = bitmap->s_nr_groups;

60
	if (block_group >= nr_groups) {
J
Joe Perches 已提交
61 62
		udf_debug("block_group (%d) > nr_groups (%d)\n",
			  block_group, nr_groups);
L
Linus Torvalds 已提交
63 64
	}

65
	if (bitmap->s_block_bitmap[block_group])
L
Linus Torvalds 已提交
66
		return block_group;
67 68 69 70 71 72

	retval = read_block_bitmap(sb, bitmap, block_group, block_group);
	if (retval < 0)
		return retval;

	return block_group;
L
Linus Torvalds 已提交
73 74
}

75 76 77
static inline int load_block_bitmap(struct super_block *sb,
				    struct udf_bitmap *bitmap,
				    unsigned int block_group)
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	int slot;

	slot = __load_block_bitmap(sb, bitmap, block_group);

	if (slot < 0)
		return slot;

	if (!bitmap->s_block_bitmap[slot])
		return -EIO;

	return slot;
}

92
static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
93
{
94
	struct udf_sb_info *sbi = UDF_SB(sb);
95 96
	struct logicalVolIntegrityDesc *lvid;

97 98
	if (!sbi->s_lvid_bh)
		return;
99 100

	lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
101
	le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
102
	udf_updated_lvid(sb);
103 104
}

105 106
static void udf_bitmap_free_blocks(struct super_block *sb,
				   struct udf_bitmap *bitmap,
107 108
				   struct kernel_lb_addr *bloc,
				   uint32_t offset,
109
				   uint32_t count)
L
Linus Torvalds 已提交
110 111
{
	struct udf_sb_info *sbi = UDF_SB(sb);
112
	struct buffer_head *bh = NULL;
113
	struct udf_part_map *partmap;
L
Linus Torvalds 已提交
114 115 116 117 118 119 120
	unsigned long block;
	unsigned long block_group;
	unsigned long bit;
	unsigned long i;
	int bitmap_nr;
	unsigned long overflow;

I
Ingo Molnar 已提交
121
	mutex_lock(&sbi->s_alloc_mutex);
122
	partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
D
Dan Carpenter 已提交
123 124
	if (bloc->logicalBlockNum + count < count ||
	    (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
125
		udf_debug("%d < %d || %d + %d > %d\n",
J
Joe Perches 已提交
126 127 128
			  bloc->logicalBlockNum, 0,
			  bloc->logicalBlockNum, count,
			  partmap->s_partition_len);
L
Linus Torvalds 已提交
129 130 131
		goto error_return;
	}

132
	block = bloc->logicalBlockNum + offset +
M
Marcin Slusarz 已提交
133
		(sizeof(struct spaceBitmapDesc) << 3);
L
Linus Torvalds 已提交
134

135 136 137 138 139 140 141 142 143 144 145
	do {
		overflow = 0;
		block_group = block >> (sb->s_blocksize_bits + 3);
		bit = block % (sb->s_blocksize << 3);

		/*
		* Check to see if we are freeing blocks across a group boundary.
		*/
		if (bit + count > (sb->s_blocksize << 3)) {
			overflow = bit + count - (sb->s_blocksize << 3);
			count -= overflow;
L
Linus Torvalds 已提交
146
		}
147 148 149 150 151 152 153 154 155
		bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
		if (bitmap_nr < 0)
			goto error_return;

		bh = bitmap->s_block_bitmap[bitmap_nr];
		for (i = 0; i < count; i++) {
			if (udf_set_bit(bit + i, bh->b_data)) {
				udf_debug("bit %ld already set\n", bit + i);
				udf_debug("byte=%2x\n",
J
Joe Perches 已提交
156
					  ((char *)bh->b_data)[(bit + i) >> 3]);
157 158
			}
		}
159
		udf_add_free_space(sb, sbi->s_partition, count);
160 161 162 163 164 165 166
		mark_buffer_dirty(bh);
		if (overflow) {
			block += count;
			count = overflow;
		}
	} while (overflow);

167
error_return:
I
Ingo Molnar 已提交
168
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
169 170
}

171 172 173 174
static int udf_bitmap_prealloc_blocks(struct super_block *sb,
				      struct udf_bitmap *bitmap,
				      uint16_t partition, uint32_t first_block,
				      uint32_t block_count)
L
Linus Torvalds 已提交
175 176 177 178 179 180
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	int alloc_count = 0;
	int bit, block, block_group, group_start;
	int nr_groups, bitmap_nr;
	struct buffer_head *bh;
M
Marcin Slusarz 已提交
181
	__u32 part_len;
L
Linus Torvalds 已提交
182

I
Ingo Molnar 已提交
183
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
184
	part_len = sbi->s_partmaps[partition].s_partition_len;
185
	if (first_block >= part_len)
L
Linus Torvalds 已提交
186 187
		goto out;

M
Marcin Slusarz 已提交
188 189
	if (first_block + block_count > part_len)
		block_count = part_len - first_block;
L
Linus Torvalds 已提交
190

191 192 193 194 195
	do {
		nr_groups = udf_compute_nr_groups(sb, partition);
		block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
		block_group = block >> (sb->s_blocksize_bits + 3);
		group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
L
Linus Torvalds 已提交
196

197 198 199 200
		bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
		if (bitmap_nr < 0)
			goto out;
		bh = bitmap->s_block_bitmap[bitmap_nr];
L
Linus Torvalds 已提交
201

202
		bit = block % (sb->s_blocksize << 3);
L
Linus Torvalds 已提交
203

204
		while (bit < (sb->s_blocksize << 3) && block_count > 0) {
J
Jan Kara 已提交
205
			if (!udf_clear_bit(bit, bh->b_data))
206 207 208 209 210
				goto out;
			block_count--;
			alloc_count++;
			bit++;
			block++;
L
Linus Torvalds 已提交
211
		}
212 213 214
		mark_buffer_dirty(bh);
	} while (block_count > 0);

215
out:
216
	udf_add_free_space(sb, partition, -alloc_count);
I
Ingo Molnar 已提交
217
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
218 219 220
	return alloc_count;
}

221 222 223
static int udf_bitmap_new_block(struct super_block *sb,
				struct udf_bitmap *bitmap, uint16_t partition,
				uint32_t goal, int *err)
L
Linus Torvalds 已提交
224 225
{
	struct udf_sb_info *sbi = UDF_SB(sb);
226
	int newbit, bit = 0, block, block_group, group_start;
L
Linus Torvalds 已提交
227 228 229 230 231 232
	int end_goal, nr_groups, bitmap_nr, i;
	struct buffer_head *bh = NULL;
	char *ptr;
	int newblock = 0;

	*err = -ENOSPC;
I
Ingo Molnar 已提交
233
	mutex_lock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
234

235
repeat:
236
	if (goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247
		goal = 0;

	nr_groups = bitmap->s_nr_groups;
	block = goal + (sizeof(struct spaceBitmapDesc) << 3);
	block_group = block >> (sb->s_blocksize_bits + 3);
	group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);

	bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
	if (bitmap_nr < 0)
		goto error_return;
	bh = bitmap->s_block_bitmap[bitmap_nr];
248 249
	ptr = memscan((char *)bh->b_data + group_start, 0xFF,
		      sb->s_blocksize - group_start);
L
Linus Torvalds 已提交
250

251
	if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
L
Linus Torvalds 已提交
252
		bit = block % (sb->s_blocksize << 3);
253
		if (udf_test_bit(bit, bh->b_data))
L
Linus Torvalds 已提交
254
			goto got_block;
255

L
Linus Torvalds 已提交
256 257 258 259
		end_goal = (bit + 63) & ~63;
		bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
		if (bit < end_goal)
			goto got_block;
260

M
Marcin Slusarz 已提交
261 262
		ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
			      sb->s_blocksize - ((bit + 7) >> 3));
L
Linus Torvalds 已提交
263
		newbit = (ptr - ((char *)bh->b_data)) << 3;
264
		if (newbit < sb->s_blocksize << 3) {
L
Linus Torvalds 已提交
265 266 267
			bit = newbit;
			goto search_back;
		}
268

M
Marcin Slusarz 已提交
269 270
		newbit = udf_find_next_one_bit(bh->b_data,
					       sb->s_blocksize << 3, bit);
271
		if (newbit < sb->s_blocksize << 3) {
L
Linus Torvalds 已提交
272 273 274 275 276
			bit = newbit;
			goto got_block;
		}
	}

277 278
	for (i = 0; i < (nr_groups * 2); i++) {
		block_group++;
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286
		if (block_group >= nr_groups)
			block_group = 0;
		group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);

		bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
		if (bitmap_nr < 0)
			goto error_return;
		bh = bitmap->s_block_bitmap[bitmap_nr];
287
		if (i < nr_groups) {
288 289
			ptr = memscan((char *)bh->b_data + group_start, 0xFF,
				      sb->s_blocksize - group_start);
290
			if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
L
Linus Torvalds 已提交
291 292 293
				bit = (ptr - ((char *)bh->b_data)) << 3;
				break;
			}
294
		} else {
D
Dirk Behme 已提交
295
			bit = udf_find_next_one_bit(bh->b_data,
296 297
						    sb->s_blocksize << 3,
						    group_start << 3);
L
Linus Torvalds 已提交
298 299 300 301
			if (bit < sb->s_blocksize << 3)
				break;
		}
	}
302
	if (i >= (nr_groups * 2)) {
I
Ingo Molnar 已提交
303
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
304 305 306 307 308
		return newblock;
	}
	if (bit < sb->s_blocksize << 3)
		goto search_back;
	else
M
Marcin Slusarz 已提交
309 310
		bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
					    group_start << 3);
311
	if (bit >= sb->s_blocksize << 3) {
I
Ingo Molnar 已提交
312
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
313 314 315
		return 0;
	}

316
search_back:
M
Marcin Slusarz 已提交
317 318 319 320 321 322
	i = 0;
	while (i < 7 && bit > (group_start << 3) &&
	       udf_test_bit(bit - 1, bh->b_data)) {
		++i;
		--bit;
	}
L
Linus Torvalds 已提交
323

324
got_block:
L
Linus Torvalds 已提交
325
	newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
326
		(sizeof(struct spaceBitmapDesc) << 3);
L
Linus Torvalds 已提交
327

328
	if (!udf_clear_bit(bit, bh->b_data)) {
L
Linus Torvalds 已提交
329 330 331 332 333 334
		udf_debug("bit already cleared for block %d\n", bit);
		goto repeat;
	}

	mark_buffer_dirty(bh);

335
	udf_add_free_space(sb, partition, -1);
I
Ingo Molnar 已提交
336
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
337 338 339
	*err = 0;
	return newblock;

340
error_return:
L
Linus Torvalds 已提交
341
	*err = -EIO;
I
Ingo Molnar 已提交
342
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
343 344 345
	return 0;
}

346 347
static void udf_table_free_blocks(struct super_block *sb,
				  struct inode *table,
348 349
				  struct kernel_lb_addr *bloc,
				  uint32_t offset,
350
				  uint32_t count)
L
Linus Torvalds 已提交
351 352
{
	struct udf_sb_info *sbi = UDF_SB(sb);
353
	struct udf_part_map *partmap;
L
Linus Torvalds 已提交
354
	uint32_t start, end;
J
Jan Kara 已提交
355
	uint32_t elen;
356
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
357
	struct extent_position oepos, epos;
L
Linus Torvalds 已提交
358
	int8_t etype;
359
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
360

I
Ingo Molnar 已提交
361
	mutex_lock(&sbi->s_alloc_mutex);
362
	partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
D
Dan Carpenter 已提交
363 364
	if (bloc->logicalBlockNum + count < count ||
	    (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
365
		udf_debug("%d < %d || %d + %d > %d\n",
J
Joe Perches 已提交
366 367
			  bloc->logicalBlockNum, 0,
			  bloc->logicalBlockNum, count,
368
			  partmap->s_partition_len);
L
Linus Torvalds 已提交
369 370 371
		goto error_return;
	}

372
	iinfo = UDF_I(table);
373
	udf_add_free_space(sb, sbi->s_partition, count);
L
Linus Torvalds 已提交
374

375 376
	start = bloc->logicalBlockNum + offset;
	end = bloc->logicalBlockNum + offset + count - 1;
L
Linus Torvalds 已提交
377

J
Jan Kara 已提交
378
	epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
L
Linus Torvalds 已提交
379
	elen = 0;
380
	epos.block = oepos.block = iinfo->i_location;
J
Jan Kara 已提交
381
	epos.bh = oepos.bh = NULL;
L
Linus Torvalds 已提交
382

383 384
	while (count &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
M
Marcin Slusarz 已提交
385 386 387 388 389 390 391 392 393 394
		if (((eloc.logicalBlockNum +
			(elen >> sb->s_blocksize_bits)) == start)) {
			if ((0x3FFFFFFF - elen) <
					(count << sb->s_blocksize_bits)) {
				uint32_t tmp = ((0x3FFFFFFF - elen) >>
							sb->s_blocksize_bits);
				count -= tmp;
				start += tmp;
				elen = (etype << 30) |
					(0x40000000 - sb->s_blocksize);
395
			} else {
M
Marcin Slusarz 已提交
396 397 398
				elen = (etype << 30) |
					(elen +
					(count << sb->s_blocksize_bits));
L
Linus Torvalds 已提交
399 400 401
				start += count;
				count = 0;
			}
402
			udf_write_aext(table, &oepos, &eloc, elen, 1);
403
		} else if (eloc.logicalBlockNum == (end + 1)) {
M
Marcin Slusarz 已提交
404 405 406 407 408 409 410 411 412
			if ((0x3FFFFFFF - elen) <
					(count << sb->s_blocksize_bits)) {
				uint32_t tmp = ((0x3FFFFFFF - elen) >>
						sb->s_blocksize_bits);
				count -= tmp;
				end -= tmp;
				eloc.logicalBlockNum -= tmp;
				elen = (etype << 30) |
					(0x40000000 - sb->s_blocksize);
413
			} else {
L
Linus Torvalds 已提交
414
				eloc.logicalBlockNum = start;
M
Marcin Slusarz 已提交
415 416 417
				elen = (etype << 30) |
					(elen +
					(count << sb->s_blocksize_bits));
L
Linus Torvalds 已提交
418 419 420
				end -= count;
				count = 0;
			}
421
			udf_write_aext(table, &oepos, &eloc, elen, 1);
L
Linus Torvalds 已提交
422 423
		}

424
		if (epos.bh != oepos.bh) {
J
Jan Kara 已提交
425
			oepos.block = epos.block;
J
Jan Kara 已提交
426 427
			brelse(oepos.bh);
			get_bh(epos.bh);
J
Jan Kara 已提交
428 429
			oepos.bh = epos.bh;
			oepos.offset = 0;
430
		} else {
J
Jan Kara 已提交
431
			oepos.offset = epos.offset;
432
		}
L
Linus Torvalds 已提交
433 434
	}

435
	if (count) {
436
		/*
M
Marcin Slusarz 已提交
437 438 439
		 * NOTE: we CANNOT use udf_add_aext here, as it can try to
		 * allocate a new block, and since we hold the super block
		 * lock already very bad things would happen :)
440 441 442 443 444 445 446
		 *
		 * We copy the behavior of udf_add_aext, but instead of
		 * trying to allocate a new block close to the existing one,
		 * we just steal a block from the extent we are trying to add.
		 *
		 * It would be nice if the blocks were close together, but it
		 * isn't required.
447
		 */
L
Linus Torvalds 已提交
448 449 450 451

		int adsize;

		eloc.logicalBlockNum = start;
452 453
		elen = EXT_RECORDED_ALLOCATED |
			(count << sb->s_blocksize_bits);
L
Linus Torvalds 已提交
454

455
		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
456
			adsize = sizeof(struct short_ad);
457
		else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
458
			adsize = sizeof(struct long_ad);
459
		else {
J
Jan Kara 已提交
460 461
			brelse(oepos.bh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
462 463 464
			goto error_return;
		}

465
		if (epos.offset + (2 * adsize) > sb->s_blocksize) {
L
Linus Torvalds 已提交
466
			/* Steal a block from the extent being free'd */
467 468 469
			udf_setup_indirect_aext(table, eloc.logicalBlockNum,
						&epos);

470
			eloc.logicalBlockNum++;
L
Linus Torvalds 已提交
471 472 473
			elen -= sb->s_blocksize;
		}

M
Marcin Slusarz 已提交
474
		/* It's possible that stealing the block emptied the extent */
475 476
		if (elen)
			__udf_add_aext(table, &epos, &eloc, elen, 1);
L
Linus Torvalds 已提交
477 478
	}

J
Jan Kara 已提交
479 480
	brelse(epos.bh);
	brelse(oepos.bh);
L
Linus Torvalds 已提交
481

482
error_return:
I
Ingo Molnar 已提交
483
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
484 485 486
	return;
}

487 488 489
static int udf_table_prealloc_blocks(struct super_block *sb,
				     struct inode *table, uint16_t partition,
				     uint32_t first_block, uint32_t block_count)
L
Linus Torvalds 已提交
490 491 492
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	int alloc_count = 0;
J
Jan Kara 已提交
493
	uint32_t elen, adsize;
494
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
495
	struct extent_position epos;
L
Linus Torvalds 已提交
496
	int8_t etype = -1;
497
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
498

499
	if (first_block >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
500 501
		return 0;

502 503
	iinfo = UDF_I(table);
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
504
		adsize = sizeof(struct short_ad);
505
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
506
		adsize = sizeof(struct long_ad);
L
Linus Torvalds 已提交
507 508 509
	else
		return 0;

I
Ingo Molnar 已提交
510
	mutex_lock(&sbi->s_alloc_mutex);
J
Jan Kara 已提交
511
	epos.offset = sizeof(struct unallocSpaceEntry);
512
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
513
	epos.bh = NULL;
L
Linus Torvalds 已提交
514 515
	eloc.logicalBlockNum = 0xFFFFFFFF;

516 517
	while (first_block != eloc.logicalBlockNum &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
L
Linus Torvalds 已提交
518
		udf_debug("eloc=%d, elen=%d, first_block=%d\n",
519
			  eloc.logicalBlockNum, elen, first_block);
520
		; /* empty loop body */
L
Linus Torvalds 已提交
521 522
	}

523
	if (first_block == eloc.logicalBlockNum) {
J
Jan Kara 已提交
524
		epos.offset -= adsize;
L
Linus Torvalds 已提交
525 526

		alloc_count = (elen >> sb->s_blocksize_bits);
J
Jan Kara 已提交
527
		if (alloc_count > block_count) {
L
Linus Torvalds 已提交
528 529 530
			alloc_count = block_count;
			eloc.logicalBlockNum += alloc_count;
			elen -= (alloc_count << sb->s_blocksize_bits);
531
			udf_write_aext(table, &epos, &eloc,
M
Marcin Slusarz 已提交
532 533 534 535
					(etype << 30) | elen, 1);
		} else
			udf_delete_aext(table, epos, eloc,
					(etype << 30) | elen);
536
	} else {
L
Linus Torvalds 已提交
537
		alloc_count = 0;
538
	}
L
Linus Torvalds 已提交
539

J
Jan Kara 已提交
540
	brelse(epos.bh);
L
Linus Torvalds 已提交
541

542 543
	if (alloc_count)
		udf_add_free_space(sb, partition, -alloc_count);
I
Ingo Molnar 已提交
544
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
545 546 547
	return alloc_count;
}

548 549 550
static int udf_table_new_block(struct super_block *sb,
			       struct inode *table, uint16_t partition,
			       uint32_t goal, int *err)
L
Linus Torvalds 已提交
551 552 553 554
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
	uint32_t newblock = 0, adsize;
J
Jan Kara 已提交
555
	uint32_t elen, goal_elen = 0;
556
	struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
J
Jan Kara 已提交
557
	struct extent_position epos, goal_epos;
L
Linus Torvalds 已提交
558
	int8_t etype;
559
	struct udf_inode_info *iinfo = UDF_I(table);
L
Linus Torvalds 已提交
560 561 562

	*err = -ENOSPC;

563
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
564
		adsize = sizeof(struct short_ad);
565
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
566
		adsize = sizeof(struct long_ad);
L
Linus Torvalds 已提交
567 568 569
	else
		return newblock;

I
Ingo Molnar 已提交
570
	mutex_lock(&sbi->s_alloc_mutex);
571
	if (goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
572 573
		goal = 0;

M
Marcin Slusarz 已提交
574 575 576 577
	/* We search for the closest matching block to goal. If we find
	   a exact hit, we stop. Otherwise we keep going till we run out
	   of extents. We store the buffer_head, bloc, and extoffset
	   of the current closest match and use that when we are done.
578
	 */
J
Jan Kara 已提交
579
	epos.offset = sizeof(struct unallocSpaceEntry);
580
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
581
	epos.bh = goal_epos.bh = NULL;
L
Linus Torvalds 已提交
582

583 584
	while (spread &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
585
		if (goal >= eloc.logicalBlockNum) {
M
Marcin Slusarz 已提交
586 587
			if (goal < eloc.logicalBlockNum +
					(elen >> sb->s_blocksize_bits))
L
Linus Torvalds 已提交
588 589 590
				nspread = 0;
			else
				nspread = goal - eloc.logicalBlockNum -
591 592
					(elen >> sb->s_blocksize_bits);
		} else {
L
Linus Torvalds 已提交
593
			nspread = eloc.logicalBlockNum - goal;
594
		}
L
Linus Torvalds 已提交
595

596
		if (nspread < spread) {
L
Linus Torvalds 已提交
597
			spread = nspread;
598
			if (goal_epos.bh != epos.bh) {
J
Jan Kara 已提交
599
				brelse(goal_epos.bh);
J
Jan Kara 已提交
600
				goal_epos.bh = epos.bh;
J
Jan Kara 已提交
601
				get_bh(goal_epos.bh);
L
Linus Torvalds 已提交
602
			}
J
Jan Kara 已提交
603 604
			goal_epos.block = epos.block;
			goal_epos.offset = epos.offset - adsize;
L
Linus Torvalds 已提交
605 606 607 608 609
			goal_eloc = eloc;
			goal_elen = (etype << 30) | elen;
		}
	}

J
Jan Kara 已提交
610
	brelse(epos.bh);
L
Linus Torvalds 已提交
611

612
	if (spread == 0xFFFFFFFF) {
J
Jan Kara 已提交
613
		brelse(goal_epos.bh);
I
Ingo Molnar 已提交
614
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
615 616 617 618 619 620 621 622 623
		return 0;
	}

	/* Only allocate blocks from the beginning of the extent.
	   That way, we only delete (empty) extents, never have to insert an
	   extent because of splitting */
	/* This works, but very poorly.... */

	newblock = goal_eloc.logicalBlockNum;
624
	goal_eloc.logicalBlockNum++;
L
Linus Torvalds 已提交
625 626 627
	goal_elen -= sb->s_blocksize;

	if (goal_elen)
628
		udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
L
Linus Torvalds 已提交
629
	else
J
Jan Kara 已提交
630
		udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
J
Jan Kara 已提交
631
	brelse(goal_epos.bh);
L
Linus Torvalds 已提交
632

633
	udf_add_free_space(sb, partition, -1);
L
Linus Torvalds 已提交
634

I
Ingo Molnar 已提交
635
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
636 637 638 639
	*err = 0;
	return newblock;
}

640 641 642
void udf_free_blocks(struct super_block *sb, struct inode *inode,
		     struct kernel_lb_addr *bloc, uint32_t offset,
		     uint32_t count)
L
Linus Torvalds 已提交
643
{
644
	uint16_t partition = bloc->partitionReferenceNum;
M
Marcin Slusarz 已提交
645
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
L
Linus Torvalds 已提交
646

M
Marcin Slusarz 已提交
647
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
J
Jan Kara 已提交
648
		udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
J
Jan Kara 已提交
649
				       bloc, offset, count);
M
Marcin Slusarz 已提交
650
	} else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
J
Jan Kara 已提交
651
		udf_table_free_blocks(sb, map->s_uspace.s_table,
J
Jan Kara 已提交
652
				      bloc, offset, count);
M
Marcin Slusarz 已提交
653
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
J
Jan Kara 已提交
654
		udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
J
Jan Kara 已提交
655
				       bloc, offset, count);
M
Marcin Slusarz 已提交
656
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
J
Jan Kara 已提交
657
		udf_table_free_blocks(sb, map->s_fspace.s_table,
J
Jan Kara 已提交
658
				      bloc, offset, count);
659
	}
J
Jan Kara 已提交
660 661 662 663 664

	if (inode) {
		inode_sub_bytes(inode,
				((sector_t)count) << sb->s_blocksize_bits);
	}
L
Linus Torvalds 已提交
665 666
}

667 668 669 670
inline int udf_prealloc_blocks(struct super_block *sb,
			       struct inode *inode,
			       uint16_t partition, uint32_t first_block,
			       uint32_t block_count)
L
Linus Torvalds 已提交
671
{
M
Marcin Slusarz 已提交
672
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
673
	int allocated;
M
Marcin Slusarz 已提交
674

M
Marcin Slusarz 已提交
675
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
J
Jan Kara 已提交
676 677 678 679
		allocated = udf_bitmap_prealloc_blocks(sb,
						       map->s_uspace.s_bitmap,
						       partition, first_block,
						       block_count);
M
Marcin Slusarz 已提交
680
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
J
Jan Kara 已提交
681 682 683 684
		allocated = udf_table_prealloc_blocks(sb,
						      map->s_uspace.s_table,
						      partition, first_block,
						      block_count);
M
Marcin Slusarz 已提交
685
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
J
Jan Kara 已提交
686 687 688 689
		allocated = udf_bitmap_prealloc_blocks(sb,
						       map->s_fspace.s_bitmap,
						       partition, first_block,
						       block_count);
M
Marcin Slusarz 已提交
690
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
J
Jan Kara 已提交
691 692 693 694
		allocated = udf_table_prealloc_blocks(sb,
						      map->s_fspace.s_table,
						      partition, first_block,
						      block_count);
M
Marcin Slusarz 已提交
695
	else
L
Linus Torvalds 已提交
696
		return 0;
J
Jan Kara 已提交
697 698 699 700

	if (inode && allocated > 0)
		inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
	return allocated;
L
Linus Torvalds 已提交
701 702
}

703 704 705
inline int udf_new_block(struct super_block *sb,
			 struct inode *inode,
			 uint16_t partition, uint32_t goal, int *err)
L
Linus Torvalds 已提交
706
{
M
Marcin Slusarz 已提交
707
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
J
Jan Kara 已提交
708
	int block;
J
Jan Kara 已提交
709

M
Marcin Slusarz 已提交
710
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
J
Jan Kara 已提交
711 712 713
		block = udf_bitmap_new_block(sb,
					     map->s_uspace.s_bitmap,
					     partition, goal, err);
M
Marcin Slusarz 已提交
714
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
J
Jan Kara 已提交
715 716
		block = udf_table_new_block(sb,
					    map->s_uspace.s_table,
717
					    partition, goal, err);
J
Jan Kara 已提交
718 719 720 721
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
		block = udf_bitmap_new_block(sb,
					     map->s_fspace.s_bitmap,
					     partition, goal, err);
M
Marcin Slusarz 已提交
722
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
J
Jan Kara 已提交
723 724 725
		block = udf_table_new_block(sb,
					    map->s_fspace.s_table,
					    partition, goal, err);
M
Marcin Slusarz 已提交
726
	else {
L
Linus Torvalds 已提交
727 728 729
		*err = -EIO;
		return 0;
	}
J
Jan Kara 已提交
730 731 732
	if (inode && block)
		inode_add_bytes(inode, sb->s_blocksize);
	return block;
L
Linus Torvalds 已提交
733
}