balloc.c 23.9 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
do_more:
L
Linus Torvalds 已提交
187 188 189 190 191 192 193
	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.
	 */
194
	if (bit + count > (sb->s_blocksize << 3)) {
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202
		overflow = bit + count - (sb->s_blocksize << 3);
		count -= overflow;
	}
	bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
	if (bitmap_nr < 0)
		goto error_return;

	bh = bitmap->s_block_bitmap[bitmap_nr];
203 204
	for (i = 0; i < count; i++) {
		if (udf_set_bit(bit + i, bh->b_data)) {
L
Linus Torvalds 已提交
205
			udf_debug("bit %ld already set\n", bit + i);
M
Marcin Slusarz 已提交
206 207
			udf_debug("byte=%2x\n",
				  ((char *)bh->b_data)[(bit + i) >> 3]);
208
		} else {
L
Linus Torvalds 已提交
209 210
			if (inode)
				DQUOT_FREE_BLOCK(inode, 1);
211
			udf_add_free_space(sbi, sbi->s_partition, 1);
L
Linus Torvalds 已提交
212 213 214
		}
	}
	mark_buffer_dirty(bh);
215
	if (overflow) {
L
Linus Torvalds 已提交
216 217 218 219
		block += count;
		count = overflow;
		goto do_more;
	}
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
	return;
}

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
repeat:
250
	nr_groups = udf_compute_nr_groups(sb, partition);
L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259 260 261
	block = first_block + (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 out;
	bh = bitmap->s_block_bitmap[bitmap_nr];

	bit = block % (sb->s_blocksize << 3);

262
	while (bit < (sb->s_blocksize << 3) && block_count > 0) {
263
		if (!udf_test_bit(bit, bh->b_data)) {
L
Linus Torvalds 已提交
264
			goto out;
265
		} else if (DQUOT_PREALLOC_BLOCK(inode, 1)) {
L
Linus Torvalds 已提交
266
			goto out;
267
		} else if (!udf_clear_bit(bit, bh->b_data)) {
L
Linus Torvalds 已提交
268 269 270 271
			udf_debug("bit already cleared for block %d\n", bit);
			DQUOT_FREE_BLOCK(inode, 1);
			goto out;
		}
272 273 274 275
		block_count--;
		alloc_count++;
		bit++;
		block++;
L
Linus Torvalds 已提交
276 277 278 279
	}
	mark_buffer_dirty(bh);
	if (block_count > 0)
		goto repeat;
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;
J
Jan Kara 已提交
463 464
	epos.block = oepos.block = UDF_I_LOCATION(table);
	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_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT) {
L
Linus Torvalds 已提交
543
			adsize = sizeof(short_ad);
544
		} else if (UDF_I_ALLOCTYPE(table) == 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
				sptr = UDF_I_DATA(table) + epos.offset - adsize;
M
Marcin Slusarz 已提交
577 578
				dptr = epos.bh->b_data +
					sizeof(struct allocExtDesc);
L
Linus Torvalds 已提交
579
				memcpy(dptr, sptr, adsize);
M
Marcin Slusarz 已提交
580 581
				epos.offset = sizeof(struct allocExtDesc) +
						adsize;
582
			} else {
J
Jan Kara 已提交
583
				loffset = epos.offset + adsize;
L
Linus Torvalds 已提交
584
				aed->lengthAllocDescs = cpu_to_le32(0);
585
				if (oepos.bh) {
586
					sptr = oepos.bh->b_data + epos.offset;
M
Marcin Slusarz 已提交
587 588
					aed = (struct allocExtDesc *)
						oepos.bh->b_data;
L
Linus Torvalds 已提交
589
					aed->lengthAllocDescs =
M
Marcin Slusarz 已提交
590 591 592
						cpu_to_le32(le32_to_cpu(
							aed->lengthAllocDescs) +
								adsize);
593
				} else {
594
					sptr = UDF_I_DATA(table) + epos.offset;
L
Linus Torvalds 已提交
595 596 597
					UDF_I_LENALLOC(table) += adsize;
					mark_inode_dirty(table);
				}
598
				epos.offset = sizeof(struct allocExtDesc);
L
Linus Torvalds 已提交
599
			}
M
Marcin Slusarz 已提交
600
			if (sbi->s_udfrev >= 0x0200)
M
Marcin Slusarz 已提交
601 602 603
				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
					    3, 1, epos.block.logicalBlockNum,
					    sizeof(tag));
L
Linus Torvalds 已提交
604
			else
M
Marcin Slusarz 已提交
605 606 607
				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
					    2, 1, epos.block.logicalBlockNum,
					    sizeof(tag));
608

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

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

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

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

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

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

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

	if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
		adsize = sizeof(short_ad);
	else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
		adsize = sizeof(long_ad);
	else
		return 0;

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

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

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

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

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

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

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

	*err = -ENOSPC;

	if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
		adsize = sizeof(short_ad);
	else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
		adsize = sizeof(long_ad);
	else
		return newblock;

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

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

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

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

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

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

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

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

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

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

829 830 831 832
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 已提交
833 834
{
	uint16_t partition = bloc.partitionReferenceNum;
M
Marcin Slusarz 已提交
835
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
L
Linus Torvalds 已提交
836

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

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

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

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

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