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 148 149 150 151 152 153 154 155 156
static bool udf_add_free_space(struct udf_sb_info *sbi,
				u16 partition, u32 cnt)
{
	struct logicalVolIntegrityDesc *lvid;

	if (sbi->s_lvid_bh)
		return false;

	lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
	lvid->freeSpaceTable[partition] = cpu_to_le32(le32_to_cpu(
					lvid->freeSpaceTable[partition]) + cnt);
	return true;
}

157 158 159 160 161
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 已提交
162 163
{
	struct udf_sb_info *sbi = UDF_SB(sb);
164
	struct buffer_head *bh = NULL;
L
Linus Torvalds 已提交
165 166 167 168 169 170 171
	unsigned long block;
	unsigned long block_group;
	unsigned long bit;
	unsigned long i;
	int bitmap_nr;
	unsigned long overflow;

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

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

186 187 188 189 190 191 192 193 194 195 196
	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 已提交
197
		}
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		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);

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

228 229 230 231 232
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 已提交
233 234 235 236 237 238
{
	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 已提交
239
	__u32 part_len;
L
Linus Torvalds 已提交
240

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

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

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

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

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

262 263 264 265 266 267 268 269 270 271 272 273 274 275
		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 已提交
276
		}
277 278 279
		mark_buffer_dirty(bh);
	} while (block_count > 0);

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

288 289 290 291
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 已提交
292 293
{
	struct udf_sb_info *sbi = UDF_SB(sb);
294
	int newbit, bit = 0, block, block_group, group_start;
L
Linus Torvalds 已提交
295 296 297 298 299 300
	int end_goal, nr_groups, bitmap_nr, i;
	struct buffer_head *bh = NULL;
	char *ptr;
	int newblock = 0;

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

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

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

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

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

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

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

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

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

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

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

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

	mark_buffer_dirty(bh);

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

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

426 427 428 429 430
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 已提交
431 432 433
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t start, end;
J
Jan Kara 已提交
434 435 436
	uint32_t elen;
	kernel_lb_addr eloc;
	struct extent_position oepos, epos;
L
Linus Torvalds 已提交
437 438 439
	int8_t etype;
	int i;

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;
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

611
			switch (UDF_I(table)->i_alloc_type) {
M
Marcin Slusarz 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
			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 已提交
628
			}
629
			if (oepos.bh) {
J
Jan Kara 已提交
630 631
				udf_update_tag(oepos.bh->b_data, loffset);
				mark_buffer_dirty(oepos.bh);
632
			} else {
L
Linus Torvalds 已提交
633
				mark_inode_dirty(table);
634
			}
L
Linus Torvalds 已提交
635 636
		}

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

641
			if (!epos.bh) {
642
				UDF_I(table)->i_lenAlloc += adsize;
L
Linus Torvalds 已提交
643
				mark_inode_dirty(table);
644
			} else {
J
Jan Kara 已提交
645
				aed = (struct allocExtDesc *)epos.bh->b_data;
L
Linus Torvalds 已提交
646
				aed->lengthAllocDescs =
M
Marcin Slusarz 已提交
647 648
					cpu_to_le32(le32_to_cpu(
					    aed->lengthAllocDescs) + adsize);
J
Jan Kara 已提交
649 650
				udf_update_tag(epos.bh->b_data, epos.offset);
				mark_buffer_dirty(epos.bh);
L
Linus Torvalds 已提交
651 652 653 654
			}
		}
	}

J
Jan Kara 已提交
655 656
	brelse(epos.bh);
	brelse(oepos.bh);
L
Linus Torvalds 已提交
657

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

664 665 666 667
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 已提交
668 669 670
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	int alloc_count = 0;
J
Jan Kara 已提交
671 672 673
	uint32_t elen, adsize;
	kernel_lb_addr eloc;
	struct extent_position epos;
L
Linus Torvalds 已提交
674 675
	int8_t etype = -1;

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

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

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

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

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

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

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

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

730 731 732 733
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 已提交
734 735 736 737
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
	uint32_t newblock = 0, adsize;
J
Jan Kara 已提交
738
	uint32_t elen, goal_elen = 0;
739
	kernel_lb_addr eloc, uninitialized_var(goal_eloc);
J
Jan Kara 已提交
740
	struct extent_position epos, goal_epos;
L
Linus Torvalds 已提交
741 742 743 744
	int8_t etype;

	*err = -ENOSPC;

745
	if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
L
Linus Torvalds 已提交
746
		adsize = sizeof(short_ad);
747
	else if (UDF_I(table)->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 = UDF_I(table)->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;
	}
}