balloc.c 23.7 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
{
	struct buffer_head *bh = NULL;
	int retval = 0;
90
	struct kernel_lb_addr loc;
L
Linus Torvalds 已提交
91 92

	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
static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
144
{
145
	struct udf_sb_info *sbi = UDF_SB(sb);
146 147
	struct logicalVolIntegrityDesc *lvid;

148 149
	if (!sbi->s_lvid_bh)
		return;
150 151

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

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

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

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

187 188 189 190 191 192 193 194 195 196 197
	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 已提交
198
		}
199 200 201 202 203 204 205 206 207 208 209 210
		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)
211
					vfs_dq_free_block(inode, 1);
212
				udf_add_free_space(sb, sbi->s_partition, 1);
213 214 215 216 217 218 219 220 221
			}
		}
		mark_buffer_dirty(bh);
		if (overflow) {
			block += count;
			count = overflow;
		}
	} while (overflow);

222
error_return:
I
Ingo Molnar 已提交
223
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
224 225
}

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

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

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

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

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

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

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

278
out:
279
	udf_add_free_space(sb, partition, -alloc_count);
I
Ingo Molnar 已提交
280
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
281 282 283
	return alloc_count;
}

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

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

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

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

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

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

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

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

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

388
got_block:
L
Linus Torvalds 已提交
389 390 391 392

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

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

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

	mark_buffer_dirty(bh);

409
	udf_add_free_space(sb, partition, -1);
I
Ingo Molnar 已提交
410
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
411 412 413
	*err = 0;
	return newblock;

414
error_return:
L
Linus Torvalds 已提交
415
	*err = -EIO;
I
Ingo Molnar 已提交
416
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
417 418 419
	return 0;
}

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

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

448
	iinfo = UDF_I(table);
M
Marcin Slusarz 已提交
449 450
	/* We do this up front - There are some error conditions that
	   could occure, but.. oh well */
L
Linus Torvalds 已提交
451
	if (inode)
452
		vfs_dq_free_block(inode, count);
453
	udf_add_free_space(sb, sbi->s_partition, count);
L
Linus Torvalds 已提交
454

455 456
	start = bloc->logicalBlockNum + offset;
	end = bloc->logicalBlockNum + offset + count - 1;
L
Linus Torvalds 已提交
457

J
Jan Kara 已提交
458
	epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
L
Linus Torvalds 已提交
459
	elen = 0;
460
	epos.block = oepos.block = iinfo->i_location;
J
Jan Kara 已提交
461
	epos.bh = oepos.bh = NULL;
L
Linus Torvalds 已提交
462

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

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

516
	if (count) {
517
		/*
M
Marcin Slusarz 已提交
518 519 520
		 * 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 :)
521 522 523 524 525 526 527
		 *
		 * 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.
528
		 */
L
Linus Torvalds 已提交
529 530

		int adsize;
531 532
		struct short_ad *sad = NULL;
		struct long_ad *lad = NULL;
L
Linus Torvalds 已提交
533 534 535
		struct allocExtDesc *aed;

		eloc.logicalBlockNum = start;
536 537
		elen = EXT_RECORDED_ALLOCATED |
			(count << sb->s_blocksize_bits);
L
Linus Torvalds 已提交
538

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

549
		if (epos.offset + (2 * adsize) > sb->s_blocksize) {
A
Al Viro 已提交
550
			unsigned char *sptr, *dptr;
L
Linus Torvalds 已提交
551
			int loffset;
552

J
Jan Kara 已提交
553
			brelse(oepos.bh);
J
Jan Kara 已提交
554
			oepos = epos;
L
Linus Torvalds 已提交
555 556

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

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

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

M
Marcin Slusarz 已提交
632 633
		/* It's possible that stealing the block emptied the extent */
		if (elen) {
634
			udf_write_aext(table, &epos, &eloc, elen, 1);
L
Linus Torvalds 已提交
635

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

J
Jan Kara 已提交
648 649
	brelse(epos.bh);
	brelse(oepos.bh);
L
Linus Torvalds 已提交
650

651
error_return:
I
Ingo Molnar 已提交
652
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
653 654 655
	return;
}

656 657 658 659
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 已提交
660 661 662
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	int alloc_count = 0;
J
Jan Kara 已提交
663
	uint32_t elen, adsize;
664
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
665
	struct extent_position epos;
L
Linus Torvalds 已提交
666
	int8_t etype = -1;
667
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
668

669
	if (first_block >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
670 671
		return 0;

672 673
	iinfo = UDF_I(table);
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
674
		adsize = sizeof(struct short_ad);
675
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
676
		adsize = sizeof(struct long_ad);
L
Linus Torvalds 已提交
677 678 679
	else
		return 0;

I
Ingo Molnar 已提交
680
	mutex_lock(&sbi->s_alloc_mutex);
J
Jan Kara 已提交
681
	epos.offset = sizeof(struct unallocSpaceEntry);
682
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
683
	epos.bh = NULL;
L
Linus Torvalds 已提交
684 685
	eloc.logicalBlockNum = 0xFFFFFFFF;

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

693
	if (first_block == eloc.logicalBlockNum) {
J
Jan Kara 已提交
694
		epos.offset -= adsize;
L
Linus Torvalds 已提交
695 696

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

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

715 716
	if (alloc_count)
		udf_add_free_space(sb, partition, -alloc_count);
I
Ingo Molnar 已提交
717
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
718 719 720
	return alloc_count;
}

721 722 723 724
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 已提交
725 726 727 728
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
	uint32_t newblock = 0, adsize;
J
Jan Kara 已提交
729
	uint32_t elen, goal_elen = 0;
730
	struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
J
Jan Kara 已提交
731
	struct extent_position epos, goal_epos;
L
Linus Torvalds 已提交
732
	int8_t etype;
733
	struct udf_inode_info *iinfo = UDF_I(table);
L
Linus Torvalds 已提交
734 735 736

	*err = -ENOSPC;

737
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
738
		adsize = sizeof(struct short_ad);
739
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
740
		adsize = sizeof(struct long_ad);
L
Linus Torvalds 已提交
741 742 743
	else
		return newblock;

I
Ingo Molnar 已提交
744
	mutex_lock(&sbi->s_alloc_mutex);
745
	if (goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
746 747
		goal = 0;

M
Marcin Slusarz 已提交
748 749 750 751
	/* 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.
752
	 */
J
Jan Kara 已提交
753
	epos.offset = sizeof(struct unallocSpaceEntry);
754
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
755
	epos.bh = goal_epos.bh = NULL;
L
Linus Torvalds 已提交
756

757 758
	while (spread &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
759
		if (goal >= eloc.logicalBlockNum) {
M
Marcin Slusarz 已提交
760 761
			if (goal < eloc.logicalBlockNum +
					(elen >> sb->s_blocksize_bits))
L
Linus Torvalds 已提交
762 763 764
				nspread = 0;
			else
				nspread = goal - eloc.logicalBlockNum -
765 766
					(elen >> sb->s_blocksize_bits);
		} else {
L
Linus Torvalds 已提交
767
			nspread = eloc.logicalBlockNum - goal;
768
		}
L
Linus Torvalds 已提交
769

770
		if (nspread < spread) {
L
Linus Torvalds 已提交
771
			spread = nspread;
772
			if (goal_epos.bh != epos.bh) {
J
Jan Kara 已提交
773
				brelse(goal_epos.bh);
J
Jan Kara 已提交
774
				goal_epos.bh = epos.bh;
J
Jan Kara 已提交
775
				get_bh(goal_epos.bh);
L
Linus Torvalds 已提交
776
			}
J
Jan Kara 已提交
777 778
			goal_epos.block = epos.block;
			goal_epos.offset = epos.offset - adsize;
L
Linus Torvalds 已提交
779 780 781 782 783
			goal_eloc = eloc;
			goal_elen = (etype << 30) | elen;
		}
	}

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

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

801
	if (inode && vfs_dq_alloc_block(inode, 1)) {
J
Jan Kara 已提交
802
		brelse(goal_epos.bh);
I
Ingo Molnar 已提交
803
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
804 805 806 807 808
		*err = -EDQUOT;
		return 0;
	}

	if (goal_elen)
809
		udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
L
Linus Torvalds 已提交
810
	else
J
Jan Kara 已提交
811
		udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
J
Jan Kara 已提交
812
	brelse(goal_epos.bh);
L
Linus Torvalds 已提交
813

814
	udf_add_free_space(sb, partition, -1);
L
Linus Torvalds 已提交
815

I
Ingo Molnar 已提交
816
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
817 818 819 820
	*err = 0;
	return newblock;
}

821 822 823
void udf_free_blocks(struct super_block *sb, struct inode *inode,
		     struct kernel_lb_addr *bloc, uint32_t offset,
		     uint32_t count)
L
Linus Torvalds 已提交
824
{
825
	uint16_t partition = bloc->partitionReferenceNum;
M
Marcin Slusarz 已提交
826
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
L
Linus Torvalds 已提交
827

M
Marcin Slusarz 已提交
828
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
J
Jan Kara 已提交
829 830
		udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
				       bloc, offset, count);
M
Marcin Slusarz 已提交
831
	} else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
J
Jan Kara 已提交
832 833
		udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
				      bloc, offset, count);
M
Marcin Slusarz 已提交
834
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
J
Jan Kara 已提交
835 836
		udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
				       bloc, offset, count);
M
Marcin Slusarz 已提交
837
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
J
Jan Kara 已提交
838 839
		udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
				      bloc, offset, count);
840
	}
L
Linus Torvalds 已提交
841 842
}

843 844 845 846
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 已提交
847
{
M
Marcin Slusarz 已提交
848 849
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];

M
Marcin Slusarz 已提交
850
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
L
Linus Torvalds 已提交
851
		return udf_bitmap_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
852
						  map->s_uspace.s_bitmap,
M
Marcin Slusarz 已提交
853 854 855
						  partition, first_block,
						  block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
L
Linus Torvalds 已提交
856
		return udf_table_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
857
						 map->s_uspace.s_table,
M
Marcin Slusarz 已提交
858 859 860
						 partition, first_block,
						 block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
L
Linus Torvalds 已提交
861
		return udf_bitmap_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
862
						  map->s_fspace.s_bitmap,
M
Marcin Slusarz 已提交
863 864 865
						  partition, first_block,
						  block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
L
Linus Torvalds 已提交
866
		return udf_table_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
867
						 map->s_fspace.s_table,
M
Marcin Slusarz 已提交
868 869 870
						 partition, first_block,
						 block_count);
	else
L
Linus Torvalds 已提交
871 872 873
		return 0;
}

874 875 876
inline int udf_new_block(struct super_block *sb,
			 struct inode *inode,
			 uint16_t partition, uint32_t goal, int *err)
L
Linus Torvalds 已提交
877
{
M
Marcin Slusarz 已提交
878
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
J
Jan Kara 已提交
879

M
Marcin Slusarz 已提交
880 881
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
		return udf_bitmap_new_block(sb, inode,
M
Marcin Slusarz 已提交
882
					   map->s_uspace.s_bitmap,
883
					   partition, goal, err);
M
Marcin Slusarz 已提交
884
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
L
Linus Torvalds 已提交
885
		return udf_table_new_block(sb, inode,
M
Marcin Slusarz 已提交
886
					   map->s_uspace.s_table,
887
					   partition, goal, err);
M
Marcin Slusarz 已提交
888
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
L
Linus Torvalds 已提交
889
		return udf_bitmap_new_block(sb, inode,
M
Marcin Slusarz 已提交
890
					    map->s_fspace.s_bitmap,
891
					    partition, goal, err);
M
Marcin Slusarz 已提交
892
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
L
Linus Torvalds 已提交
893
		return udf_table_new_block(sb, inode,
M
Marcin Slusarz 已提交
894
					   map->s_fspace.s_table,
895
					   partition, goal, err);
M
Marcin Slusarz 已提交
896
	else {
L
Linus Torvalds 已提交
897 898 899 900
		*err = -EIO;
		return 0;
	}
}