balloc.c 24.0 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 29 30
/*
 * 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/quotaops.h>
#include <linux/buffer_head.h>
#include <linux/bitops.h>

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

M
Marcin Slusarz 已提交
31 32
#define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
#define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
L
Linus Torvalds 已提交
33 34
#define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
#define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size)
M
Marcin Slusarz 已提交
35 36
#define udf_find_next_one_bit(addr, size, offset) \
		find_next_one_bit(addr, size, offset)
L
Linus Torvalds 已提交
37 38

#define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
M
Marcin Slusarz 已提交
39 40
#define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y)
#define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y))
L
Linus Torvalds 已提交
41 42 43 44
#define uintBPL_t uint(BITS_PER_LONG)
#define uint(x) xuint(x)
#define xuint(x) __le ## x

45
static inline int find_next_one_bit(void *addr, int size, int offset)
L
Linus Torvalds 已提交
46
{
47 48
	uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
	int result = offset & ~(BITS_PER_LONG - 1);
L
Linus Torvalds 已提交
49 50 51 52 53
	unsigned long tmp;

	if (offset >= size)
		return size;
	size -= result;
54 55
	offset &= (BITS_PER_LONG - 1);
	if (offset) {
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64
		tmp = leBPL_to_cpup(p++);
		tmp &= ~0UL << offset;
		if (size < BITS_PER_LONG)
			goto found_first;
		if (tmp)
			goto found_middle;
		size -= BITS_PER_LONG;
		result += BITS_PER_LONG;
	}
65
	while (size & ~(BITS_PER_LONG - 1)) {
M
Marcin Slusarz 已提交
66 67
		tmp = leBPL_to_cpup(p++);
		if (tmp)
L
Linus Torvalds 已提交
68 69 70 71 72 73 74
			goto found_middle;
		result += BITS_PER_LONG;
		size -= BITS_PER_LONG;
	}
	if (!size)
		return result;
	tmp = leBPL_to_cpup(p);
75
found_first:
76
	tmp &= ~0UL >> (BITS_PER_LONG - size);
77
found_middle:
L
Linus Torvalds 已提交
78 79 80 81 82 83
	return result + ffz(~tmp);
}

#define find_first_one_bit(addr, size)\
	find_next_one_bit((addr), (size), 0)

84 85 86
static int read_block_bitmap(struct super_block *sb,
			     struct udf_bitmap *bitmap, unsigned int block,
			     unsigned long bitmap_nr)
L
Linus Torvalds 已提交
87 88 89 90 91 92
{
	struct buffer_head *bh = NULL;
	int retval = 0;
	kernel_lb_addr loc;

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

	bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block));
M
Marcin Slusarz 已提交
96
	if (!bh)
L
Linus Torvalds 已提交
97
		retval = -EIO;
M
Marcin Slusarz 已提交
98

L
Linus Torvalds 已提交
99 100 101 102
	bitmap->s_block_bitmap[bitmap_nr] = bh;
	return retval;
}

103 104 105
static int __load_block_bitmap(struct super_block *sb,
			       struct udf_bitmap *bitmap,
			       unsigned int block_group)
L
Linus Torvalds 已提交
106 107 108 109
{
	int retval = 0;
	int nr_groups = bitmap->s_nr_groups;

110 111 112
	if (block_group >= nr_groups) {
		udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
			  nr_groups);
L
Linus Torvalds 已提交
113 114
	}

115
	if (bitmap->s_block_bitmap[block_group]) {
L
Linus Torvalds 已提交
116
		return block_group;
117 118 119
	} else {
		retval = read_block_bitmap(sb, bitmap, block_group,
					   block_group);
L
Linus Torvalds 已提交
120 121 122 123 124 125
		if (retval < 0)
			return retval;
		return block_group;
	}
}

126 127 128
static inline int load_block_bitmap(struct super_block *sb,
				    struct udf_bitmap *bitmap,
				    unsigned int block_group)
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142
{
	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;
}

143 144 145 146 147
static bool udf_add_free_space(struct udf_sb_info *sbi,
				u16 partition, u32 cnt)
{
	struct logicalVolIntegrityDesc *lvid;

M
Marcin Slusarz 已提交
148
	if (sbi->s_lvid_bh == NULL)
149 150 151
		return false;

	lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
152
	le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
153 154 155
	return true;
}

156 157 158 159 160
static void udf_bitmap_free_blocks(struct super_block *sb,
				   struct inode *inode,
				   struct udf_bitmap *bitmap,
				   kernel_lb_addr bloc, uint32_t offset,
				   uint32_t count)
L
Linus Torvalds 已提交
161 162
{
	struct udf_sb_info *sbi = UDF_SB(sb);
163
	struct buffer_head *bh = NULL;
L
Linus Torvalds 已提交
164 165 166 167 168 169 170
	unsigned long block;
	unsigned long block_group;
	unsigned long bit;
	unsigned long i;
	int bitmap_nr;
	unsigned long overflow;

I
Ingo Molnar 已提交
171
	mutex_lock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
172
	if (bloc.logicalBlockNum < 0 ||
M
Marcin Slusarz 已提交
173 174
	    (bloc.logicalBlockNum + count) >
		sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
175 176
		udf_debug("%d < %d || %d + %d > %d\n",
			  bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
M
Marcin Slusarz 已提交
177 178
			  sbi->s_partmaps[bloc.partitionReferenceNum].
							s_partition_len);
L
Linus Torvalds 已提交
179 180 181
		goto error_return;
	}

M
Marcin Slusarz 已提交
182 183
	block = bloc.logicalBlockNum + offset +
		(sizeof(struct spaceBitmapDesc) << 3);
L
Linus Torvalds 已提交
184

185 186 187 188 189 190 191 192 193 194 195
	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 已提交
196
		}
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		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",
					((char *)bh->b_data)[(bit + i) >> 3]);
			} else {
				if (inode)
					DQUOT_FREE_BLOCK(inode, 1);
				udf_add_free_space(sbi, sbi->s_partition, 1);
			}
		}
		mark_buffer_dirty(bh);
		if (overflow) {
			block += count;
			count = overflow;
		}
	} while (overflow);

220
error_return:
L
Linus Torvalds 已提交
221
	sb->s_dirt = 1;
M
Marcin Slusarz 已提交
222 223
	if (sbi->s_lvid_bh)
		mark_buffer_dirty(sbi->s_lvid_bh);
I
Ingo Molnar 已提交
224
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
225 226
}

227 228 229 230 231
static int udf_bitmap_prealloc_blocks(struct super_block *sb,
				      struct inode *inode,
				      struct udf_bitmap *bitmap,
				      uint16_t partition, uint32_t first_block,
				      uint32_t block_count)
L
Linus Torvalds 已提交
232 233 234 235 236 237
{
	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 已提交
238
	__u32 part_len;
L
Linus Torvalds 已提交
239

I
Ingo Molnar 已提交
240
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
241 242
	part_len = sbi->s_partmaps[partition].s_partition_len;
	if (first_block < 0 || first_block >= part_len)
L
Linus Torvalds 已提交
243 244
		goto out;

M
Marcin Slusarz 已提交
245 246
	if (first_block + block_count > part_len)
		block_count = part_len - first_block;
L
Linus Torvalds 已提交
247

248 249 250 251 252
	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 已提交
253

254 255 256 257
		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 已提交
258

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

261 262 263 264 265 266 267 268 269 270 271 272 273 274
		while (bit < (sb->s_blocksize << 3) && block_count > 0) {
			if (!udf_test_bit(bit, bh->b_data))
				goto out;
			else if (DQUOT_PREALLOC_BLOCK(inode, 1))
				goto out;
			else if (!udf_clear_bit(bit, bh->b_data)) {
				udf_debug("bit already cleared for block %d\n", bit);
				DQUOT_FREE_BLOCK(inode, 1);
				goto out;
			}
			block_count--;
			alloc_count++;
			bit++;
			block++;
L
Linus Torvalds 已提交
275
		}
276 277 278
		mark_buffer_dirty(bh);
	} while (block_count > 0);

279
out:
280
	if (udf_add_free_space(sbi, partition, -alloc_count))
M
Marcin Slusarz 已提交
281
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
282
	sb->s_dirt = 1;
I
Ingo Molnar 已提交
283
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
284 285 286
	return alloc_count;
}

287 288 289 290
static int udf_bitmap_new_block(struct super_block *sb,
				struct inode *inode,
				struct udf_bitmap *bitmap, uint16_t partition,
				uint32_t goal, int *err)
L
Linus Torvalds 已提交
291 292
{
	struct udf_sb_info *sbi = UDF_SB(sb);
293
	int newbit, bit = 0, block, block_group, group_start;
L
Linus Torvalds 已提交
294 295 296 297 298 299
	int end_goal, nr_groups, bitmap_nr, i;
	struct buffer_head *bh = NULL;
	char *ptr;
	int newblock = 0;

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

302
repeat:
M
Marcin Slusarz 已提交
303
	if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
304 305 306 307 308 309 310 311 312 313 314
		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];
315 316
	ptr = memscan((char *)bh->b_data + group_start, 0xFF,
		      sb->s_blocksize - group_start);
L
Linus Torvalds 已提交
317

318
	if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
L
Linus Torvalds 已提交
319
		bit = block % (sb->s_blocksize << 3);
320
		if (udf_test_bit(bit, bh->b_data))
L
Linus Torvalds 已提交
321
			goto got_block;
322

L
Linus Torvalds 已提交
323 324 325 326
		end_goal = (bit + 63) & ~63;
		bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
		if (bit < end_goal)
			goto got_block;
327

M
Marcin Slusarz 已提交
328 329
		ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
			      sb->s_blocksize - ((bit + 7) >> 3));
L
Linus Torvalds 已提交
330
		newbit = (ptr - ((char *)bh->b_data)) << 3;
331
		if (newbit < sb->s_blocksize << 3) {
L
Linus Torvalds 已提交
332 333 334
			bit = newbit;
			goto search_back;
		}
335

M
Marcin Slusarz 已提交
336 337
		newbit = udf_find_next_one_bit(bh->b_data,
					       sb->s_blocksize << 3, bit);
338
		if (newbit < sb->s_blocksize << 3) {
L
Linus Torvalds 已提交
339 340 341 342 343
			bit = newbit;
			goto got_block;
		}
	}

344 345
	for (i = 0; i < (nr_groups * 2); i++) {
		block_group++;
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353
		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];
354
		if (i < nr_groups) {
355 356
			ptr = memscan((char *)bh->b_data + group_start, 0xFF,
				      sb->s_blocksize - group_start);
357
			if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
L
Linus Torvalds 已提交
358 359 360
				bit = (ptr - ((char *)bh->b_data)) << 3;
				break;
			}
361
		} else {
362 363 364
			bit = udf_find_next_one_bit((char *)bh->b_data,
						    sb->s_blocksize << 3,
						    group_start << 3);
L
Linus Torvalds 已提交
365 366 367 368
			if (bit < sb->s_blocksize << 3)
				break;
		}
	}
369
	if (i >= (nr_groups * 2)) {
I
Ingo Molnar 已提交
370
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
371 372 373 374 375
		return newblock;
	}
	if (bit < sb->s_blocksize << 3)
		goto search_back;
	else
M
Marcin Slusarz 已提交
376 377
		bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
					    group_start << 3);
378
	if (bit >= sb->s_blocksize << 3) {
I
Ingo Molnar 已提交
379
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
380 381 382
		return 0;
	}

383
search_back:
M
Marcin Slusarz 已提交
384 385 386 387 388 389
	i = 0;
	while (i < 7 && bit > (group_start << 3) &&
	       udf_test_bit(bit - 1, bh->b_data)) {
		++i;
		--bit;
	}
L
Linus Torvalds 已提交
390

391
got_block:
L
Linus Torvalds 已提交
392 393 394 395

	/*
	 * Check quota for allocation of this block.
	 */
396
	if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
I
Ingo Molnar 已提交
397
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
398 399 400 401 402
		*err = -EDQUOT;
		return 0;
	}

	newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
403
		(sizeof(struct spaceBitmapDesc) << 3);
L
Linus Torvalds 已提交
404

405
	if (!udf_clear_bit(bit, bh->b_data)) {
L
Linus Torvalds 已提交
406 407 408 409 410 411
		udf_debug("bit already cleared for block %d\n", bit);
		goto repeat;
	}

	mark_buffer_dirty(bh);

412
	if (udf_add_free_space(sbi, partition, -1))
M
Marcin Slusarz 已提交
413
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
414
	sb->s_dirt = 1;
I
Ingo Molnar 已提交
415
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
416 417 418
	*err = 0;
	return newblock;

419
error_return:
L
Linus Torvalds 已提交
420
	*err = -EIO;
I
Ingo Molnar 已提交
421
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
422 423 424
	return 0;
}

425 426 427 428 429
static void udf_table_free_blocks(struct super_block *sb,
				  struct inode *inode,
				  struct inode *table,
				  kernel_lb_addr bloc, uint32_t offset,
				  uint32_t count)
L
Linus Torvalds 已提交
430 431 432
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t start, end;
J
Jan Kara 已提交
433 434 435
	uint32_t elen;
	kernel_lb_addr eloc;
	struct extent_position oepos, epos;
L
Linus Torvalds 已提交
436 437
	int8_t etype;
	int i;
438
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
439

I
Ingo Molnar 已提交
440
	mutex_lock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
441
	if (bloc.logicalBlockNum < 0 ||
M
Marcin Slusarz 已提交
442 443
	    (bloc.logicalBlockNum + count) >
		sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) {
444 445
		udf_debug("%d < %d || %d + %d > %d\n",
			  bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
M
Marcin Slusarz 已提交
446 447
			  sbi->s_partmaps[bloc.partitionReferenceNum].
							s_partition_len);
L
Linus Torvalds 已提交
448 449 450
		goto error_return;
	}

451
	iinfo = UDF_I(table);
M
Marcin Slusarz 已提交
452 453
	/* We do this up front - There are some error conditions that
	   could occure, but.. oh well */
L
Linus Torvalds 已提交
454 455
	if (inode)
		DQUOT_FREE_BLOCK(inode, count);
456
	if (udf_add_free_space(sbi, sbi->s_partition, count))
M
Marcin Slusarz 已提交
457
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
458 459 460 461

	start = bloc.logicalBlockNum + offset;
	end = bloc.logicalBlockNum + offset + count - 1;

J
Jan Kara 已提交
462
	epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
L
Linus Torvalds 已提交
463
	elen = 0;
464
	epos.block = oepos.block = iinfo->i_location;
J
Jan Kara 已提交
465
	epos.bh = oepos.bh = NULL;
L
Linus Torvalds 已提交
466

467 468
	while (count &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
M
Marcin Slusarz 已提交
469 470 471 472 473 474 475 476 477 478
		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);
479
			} else {
M
Marcin Slusarz 已提交
480 481 482
				elen = (etype << 30) |
					(elen +
					(count << sb->s_blocksize_bits));
L
Linus Torvalds 已提交
483 484 485
				start += count;
				count = 0;
			}
J
Jan Kara 已提交
486
			udf_write_aext(table, &oepos, eloc, elen, 1);
487
		} else if (eloc.logicalBlockNum == (end + 1)) {
M
Marcin Slusarz 已提交
488 489 490 491 492 493 494 495 496
			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);
497
			} else {
L
Linus Torvalds 已提交
498
				eloc.logicalBlockNum = start;
M
Marcin Slusarz 已提交
499 500 501
				elen = (etype << 30) |
					(elen +
					(count << sb->s_blocksize_bits));
L
Linus Torvalds 已提交
502 503 504
				end -= count;
				count = 0;
			}
J
Jan Kara 已提交
505
			udf_write_aext(table, &oepos, eloc, elen, 1);
L
Linus Torvalds 已提交
506 507
		}

508
		if (epos.bh != oepos.bh) {
L
Linus Torvalds 已提交
509
			i = -1;
J
Jan Kara 已提交
510
			oepos.block = epos.block;
J
Jan Kara 已提交
511 512
			brelse(oepos.bh);
			get_bh(epos.bh);
J
Jan Kara 已提交
513 514
			oepos.bh = epos.bh;
			oepos.offset = 0;
515
		} else {
J
Jan Kara 已提交
516
			oepos.offset = epos.offset;
517
		}
L
Linus Torvalds 已提交
518 519
	}

520
	if (count) {
521
		/*
M
Marcin Slusarz 已提交
522 523 524
		 * 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 :)
525 526 527 528 529 530 531
		 *
		 * 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.
532
		 */
L
Linus Torvalds 已提交
533 534 535 536 537 538 539

		int adsize;
		short_ad *sad = NULL;
		long_ad *lad = NULL;
		struct allocExtDesc *aed;

		eloc.logicalBlockNum = start;
540 541
		elen = EXT_RECORDED_ALLOCATED |
			(count << sb->s_blocksize_bits);
L
Linus Torvalds 已提交
542

543
		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
L
Linus Torvalds 已提交
544
			adsize = sizeof(short_ad);
545
		else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
L
Linus Torvalds 已提交
546
			adsize = sizeof(long_ad);
547
		else {
J
Jan Kara 已提交
548 549
			brelse(oepos.bh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
550 551 552
			goto error_return;
		}

553
		if (epos.offset + (2 * adsize) > sb->s_blocksize) {
L
Linus Torvalds 已提交
554 555
			char *sptr, *dptr;
			int loffset;
556

J
Jan Kara 已提交
557
			brelse(oepos.bh);
J
Jan Kara 已提交
558
			oepos = epos;
L
Linus Torvalds 已提交
559 560

			/* Steal a block from the extent being free'd */
J
Jan Kara 已提交
561
			epos.block.logicalBlockNum = eloc.logicalBlockNum;
562
			eloc.logicalBlockNum++;
L
Linus Torvalds 已提交
563 564
			elen -= sb->s_blocksize;

M
Marcin Slusarz 已提交
565 566 567
			epos.bh = udf_tread(sb,
					udf_get_lb_pblock(sb, epos.block, 0));
			if (!epos.bh) {
J
Jan Kara 已提交
568
				brelse(oepos.bh);
L
Linus Torvalds 已提交
569 570
				goto error_return;
			}
J
Jan Kara 已提交
571
			aed = (struct allocExtDesc *)(epos.bh->b_data);
M
Marcin Slusarz 已提交
572 573
			aed->previousAllocExtLocation =
				cpu_to_le32(oepos.block.logicalBlockNum);
574
			if (epos.offset + adsize > sb->s_blocksize) {
J
Jan Kara 已提交
575
				loffset = epos.offset;
L
Linus Torvalds 已提交
576
				aed->lengthAllocDescs = cpu_to_le32(adsize);
577
				sptr = iinfo->i_ext.i_data + epos.offset
578
								- adsize;
M
Marcin Slusarz 已提交
579 580
				dptr = epos.bh->b_data +
					sizeof(struct allocExtDesc);
L
Linus Torvalds 已提交
581
				memcpy(dptr, sptr, adsize);
M
Marcin Slusarz 已提交
582 583
				epos.offset = sizeof(struct allocExtDesc) +
						adsize;
584
			} else {
J
Jan Kara 已提交
585
				loffset = epos.offset + adsize;
L
Linus Torvalds 已提交
586
				aed->lengthAllocDescs = cpu_to_le32(0);
587
				if (oepos.bh) {
588
					sptr = oepos.bh->b_data + epos.offset;
M
Marcin Slusarz 已提交
589 590
					aed = (struct allocExtDesc *)
						oepos.bh->b_data;
591 592
					le32_add_cpu(&aed->lengthAllocDescs,
							adsize);
593
				} else {
594
					sptr = iinfo->i_ext.i_data +
595
								epos.offset;
596
					iinfo->i_lenAlloc += adsize;
L
Linus Torvalds 已提交
597 598
					mark_inode_dirty(table);
				}
599
				epos.offset = sizeof(struct allocExtDesc);
L
Linus Torvalds 已提交
600
			}
M
Marcin Slusarz 已提交
601
			if (sbi->s_udfrev >= 0x0200)
M
Marcin Slusarz 已提交
602 603 604
				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
					    3, 1, epos.block.logicalBlockNum,
					    sizeof(tag));
L
Linus Torvalds 已提交
605
			else
M
Marcin Slusarz 已提交
606 607 608
				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
					    2, 1, epos.block.logicalBlockNum,
					    sizeof(tag));
609

610
			switch (iinfo->i_alloc_type) {
M
Marcin Slusarz 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
			case ICBTAG_FLAG_AD_SHORT:
				sad = (short_ad *)sptr;
				sad->extLength = cpu_to_le32(
					EXT_NEXT_EXTENT_ALLOCDECS |
					sb->s_blocksize);
				sad->extPosition =
					cpu_to_le32(epos.block.logicalBlockNum);
				break;
			case ICBTAG_FLAG_AD_LONG:
				lad = (long_ad *)sptr;
				lad->extLength = cpu_to_le32(
					EXT_NEXT_EXTENT_ALLOCDECS |
					sb->s_blocksize);
				lad->extLocation =
					cpu_to_lelb(epos.block);
				break;
L
Linus Torvalds 已提交
627
			}
628
			if (oepos.bh) {
J
Jan Kara 已提交
629 630
				udf_update_tag(oepos.bh->b_data, loffset);
				mark_buffer_dirty(oepos.bh);
631
			} else {
L
Linus Torvalds 已提交
632
				mark_inode_dirty(table);
633
			}
L
Linus Torvalds 已提交
634 635
		}

M
Marcin Slusarz 已提交
636 637
		/* It's possible that stealing the block emptied the extent */
		if (elen) {
J
Jan Kara 已提交
638
			udf_write_aext(table, &epos, eloc, elen, 1);
L
Linus Torvalds 已提交
639

640
			if (!epos.bh) {
641
				iinfo->i_lenAlloc += adsize;
L
Linus Torvalds 已提交
642
				mark_inode_dirty(table);
643
			} else {
J
Jan Kara 已提交
644
				aed = (struct allocExtDesc *)epos.bh->b_data;
645
				le32_add_cpu(&aed->lengthAllocDescs, adsize);
J
Jan Kara 已提交
646 647
				udf_update_tag(epos.bh->b_data, epos.offset);
				mark_buffer_dirty(epos.bh);
L
Linus Torvalds 已提交
648 649 650 651
			}
		}
	}

J
Jan Kara 已提交
652 653
	brelse(epos.bh);
	brelse(oepos.bh);
L
Linus Torvalds 已提交
654

655
error_return:
L
Linus Torvalds 已提交
656
	sb->s_dirt = 1;
I
Ingo Molnar 已提交
657
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
658 659 660
	return;
}

661 662 663 664
static int udf_table_prealloc_blocks(struct super_block *sb,
				     struct inode *inode,
				     struct inode *table, uint16_t partition,
				     uint32_t first_block, uint32_t block_count)
L
Linus Torvalds 已提交
665 666 667
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	int alloc_count = 0;
J
Jan Kara 已提交
668 669 670
	uint32_t elen, adsize;
	kernel_lb_addr eloc;
	struct extent_position epos;
L
Linus Torvalds 已提交
671
	int8_t etype = -1;
672
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
673

M
Marcin Slusarz 已提交
674 675
	if (first_block < 0 ||
		first_block >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
676 677
		return 0;

678 679
	iinfo = UDF_I(table);
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
L
Linus Torvalds 已提交
680
		adsize = sizeof(short_ad);
681
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
L
Linus Torvalds 已提交
682 683 684 685
		adsize = sizeof(long_ad);
	else
		return 0;

I
Ingo Molnar 已提交
686
	mutex_lock(&sbi->s_alloc_mutex);
J
Jan Kara 已提交
687
	epos.offset = sizeof(struct unallocSpaceEntry);
688
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
689
	epos.bh = NULL;
L
Linus Torvalds 已提交
690 691
	eloc.logicalBlockNum = 0xFFFFFFFF;

692 693
	while (first_block != eloc.logicalBlockNum &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
L
Linus Torvalds 已提交
694
		udf_debug("eloc=%d, elen=%d, first_block=%d\n",
695
			  eloc.logicalBlockNum, elen, first_block);
696
		; /* empty loop body */
L
Linus Torvalds 已提交
697 698
	}

699
	if (first_block == eloc.logicalBlockNum) {
J
Jan Kara 已提交
700
		epos.offset -= adsize;
L
Linus Torvalds 已提交
701 702

		alloc_count = (elen >> sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
703 704
		if (inode && DQUOT_PREALLOC_BLOCK(inode,
			alloc_count > block_count ? block_count : alloc_count))
L
Linus Torvalds 已提交
705
			alloc_count = 0;
M
Marcin Slusarz 已提交
706
		else if (alloc_count > block_count) {
L
Linus Torvalds 已提交
707 708 709
			alloc_count = block_count;
			eloc.logicalBlockNum += alloc_count;
			elen -= (alloc_count << sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
710 711 712 713 714
			udf_write_aext(table, &epos, eloc,
					(etype << 30) | elen, 1);
		} else
			udf_delete_aext(table, epos, eloc,
					(etype << 30) | elen);
715
	} else {
L
Linus Torvalds 已提交
716
		alloc_count = 0;
717
	}
L
Linus Torvalds 已提交
718

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

721
	if (alloc_count && udf_add_free_space(sbi, partition, -alloc_count)) {
M
Marcin Slusarz 已提交
722
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
723 724
		sb->s_dirt = 1;
	}
I
Ingo Molnar 已提交
725
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
726 727 728
	return alloc_count;
}

729 730 731 732
static int udf_table_new_block(struct super_block *sb,
			       struct inode *inode,
			       struct inode *table, uint16_t partition,
			       uint32_t goal, int *err)
L
Linus Torvalds 已提交
733 734 735 736
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
	uint32_t newblock = 0, adsize;
J
Jan Kara 已提交
737
	uint32_t elen, goal_elen = 0;
738
	kernel_lb_addr eloc, uninitialized_var(goal_eloc);
J
Jan Kara 已提交
739
	struct extent_position epos, goal_epos;
L
Linus Torvalds 已提交
740
	int8_t etype;
741
	struct udf_inode_info *iinfo = UDF_I(table);
L
Linus Torvalds 已提交
742 743 744

	*err = -ENOSPC;

745
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
L
Linus Torvalds 已提交
746
		adsize = sizeof(short_ad);
747
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
L
Linus Torvalds 已提交
748 749 750 751
		adsize = sizeof(long_ad);
	else
		return newblock;

I
Ingo Molnar 已提交
752
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
753
	if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
754 755
		goal = 0;

M
Marcin Slusarz 已提交
756 757 758 759
	/* 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.
760
	 */
J
Jan Kara 已提交
761
	epos.offset = sizeof(struct unallocSpaceEntry);
762
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
763
	epos.bh = goal_epos.bh = NULL;
L
Linus Torvalds 已提交
764

765 766
	while (spread &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
767
		if (goal >= eloc.logicalBlockNum) {
M
Marcin Slusarz 已提交
768 769
			if (goal < eloc.logicalBlockNum +
					(elen >> sb->s_blocksize_bits))
L
Linus Torvalds 已提交
770 771 772
				nspread = 0;
			else
				nspread = goal - eloc.logicalBlockNum -
773 774
					(elen >> sb->s_blocksize_bits);
		} else {
L
Linus Torvalds 已提交
775
			nspread = eloc.logicalBlockNum - goal;
776
		}
L
Linus Torvalds 已提交
777

778
		if (nspread < spread) {
L
Linus Torvalds 已提交
779
			spread = nspread;
780
			if (goal_epos.bh != epos.bh) {
J
Jan Kara 已提交
781
				brelse(goal_epos.bh);
J
Jan Kara 已提交
782
				goal_epos.bh = epos.bh;
J
Jan Kara 已提交
783
				get_bh(goal_epos.bh);
L
Linus Torvalds 已提交
784
			}
J
Jan Kara 已提交
785 786
			goal_epos.block = epos.block;
			goal_epos.offset = epos.offset - adsize;
L
Linus Torvalds 已提交
787 788 789 790 791
			goal_eloc = eloc;
			goal_elen = (etype << 30) | elen;
		}
	}

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

794
	if (spread == 0xFFFFFFFF) {
J
Jan Kara 已提交
795
		brelse(goal_epos.bh);
I
Ingo Molnar 已提交
796
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804 805
		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;
806
	goal_eloc.logicalBlockNum++;
L
Linus Torvalds 已提交
807 808
	goal_elen -= sb->s_blocksize;

809
	if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
J
Jan Kara 已提交
810
		brelse(goal_epos.bh);
I
Ingo Molnar 已提交
811
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
812 813 814 815 816
		*err = -EDQUOT;
		return 0;
	}

	if (goal_elen)
J
Jan Kara 已提交
817
		udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
L
Linus Torvalds 已提交
818
	else
J
Jan Kara 已提交
819
		udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
J
Jan Kara 已提交
820
	brelse(goal_epos.bh);
L
Linus Torvalds 已提交
821

822
	if (udf_add_free_space(sbi, partition, -1))
M
Marcin Slusarz 已提交
823
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
824 825

	sb->s_dirt = 1;
I
Ingo Molnar 已提交
826
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
827 828 829 830
	*err = 0;
	return newblock;
}

831 832 833 834
inline void udf_free_blocks(struct super_block *sb,
			    struct inode *inode,
			    kernel_lb_addr bloc, uint32_t offset,
			    uint32_t count)
L
Linus Torvalds 已提交
835 836
{
	uint16_t partition = bloc.partitionReferenceNum;
M
Marcin Slusarz 已提交
837
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
L
Linus Torvalds 已提交
838

M
Marcin Slusarz 已提交
839
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
L
Linus Torvalds 已提交
840
		return udf_bitmap_free_blocks(sb, inode,
M
Marcin Slusarz 已提交
841
					      map->s_uspace.s_bitmap,
842
					      bloc, offset, count);
M
Marcin Slusarz 已提交
843
	} else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
L
Linus Torvalds 已提交
844
		return udf_table_free_blocks(sb, inode,
M
Marcin Slusarz 已提交
845
					     map->s_uspace.s_table,
846
					     bloc, offset, count);
M
Marcin Slusarz 已提交
847
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
L
Linus Torvalds 已提交
848
		return udf_bitmap_free_blocks(sb, inode,
M
Marcin Slusarz 已提交
849
					      map->s_fspace.s_bitmap,
850
					      bloc, offset, count);
M
Marcin Slusarz 已提交
851
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
L
Linus Torvalds 已提交
852
		return udf_table_free_blocks(sb, inode,
M
Marcin Slusarz 已提交
853
					     map->s_fspace.s_table,
854 855
					     bloc, offset, count);
	} else {
L
Linus Torvalds 已提交
856
		return;
857
	}
L
Linus Torvalds 已提交
858 859
}

860 861 862 863
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 已提交
864
{
M
Marcin Slusarz 已提交
865 866
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];

M
Marcin Slusarz 已提交
867
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
L
Linus Torvalds 已提交
868
		return udf_bitmap_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
869
						  map->s_uspace.s_bitmap,
M
Marcin Slusarz 已提交
870 871 872
						  partition, first_block,
						  block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
L
Linus Torvalds 已提交
873
		return udf_table_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
874
						 map->s_uspace.s_table,
M
Marcin Slusarz 已提交
875 876 877
						 partition, first_block,
						 block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
L
Linus Torvalds 已提交
878
		return udf_bitmap_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
879
						  map->s_fspace.s_bitmap,
M
Marcin Slusarz 已提交
880 881 882
						  partition, first_block,
						  block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
L
Linus Torvalds 已提交
883
		return udf_table_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
884
						 map->s_fspace.s_table,
M
Marcin Slusarz 已提交
885 886 887
						 partition, first_block,
						 block_count);
	else
L
Linus Torvalds 已提交
888 889 890
		return 0;
}

891 892 893
inline int udf_new_block(struct super_block *sb,
			 struct inode *inode,
			 uint16_t partition, uint32_t goal, int *err)
L
Linus Torvalds 已提交
894
{
M
Marcin Slusarz 已提交
895
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
J
Jan Kara 已提交
896

M
Marcin Slusarz 已提交
897 898
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
		return udf_bitmap_new_block(sb, inode,
M
Marcin Slusarz 已提交
899
					   map->s_uspace.s_bitmap,
900
					   partition, goal, err);
M
Marcin Slusarz 已提交
901
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
L
Linus Torvalds 已提交
902
		return udf_table_new_block(sb, inode,
M
Marcin Slusarz 已提交
903
					   map->s_uspace.s_table,
904
					   partition, goal, err);
M
Marcin Slusarz 已提交
905
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
L
Linus Torvalds 已提交
906
		return udf_bitmap_new_block(sb, inode,
M
Marcin Slusarz 已提交
907
					    map->s_fspace.s_bitmap,
908
					    partition, goal, err);
M
Marcin Slusarz 已提交
909
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
L
Linus Torvalds 已提交
910
		return udf_table_new_block(sb, inode,
M
Marcin Slusarz 已提交
911
					   map->s_fspace.s_table,
912
					   partition, goal, err);
M
Marcin Slusarz 已提交
913
	else {
L
Linus Torvalds 已提交
914 915 916 917
		*err = -EIO;
		return 0;
	}
}