balloc.c 22.5 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
#define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
M
Marcin Slusarz 已提交
34
#define udf_find_next_one_bit(addr, size, offset) \
A
Akinobu Mita 已提交
35
		ext2_find_next_bit(addr, size, offset)
L
Linus Torvalds 已提交
36

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

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

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

L
Linus Torvalds 已提交
52 53 54 55
	bitmap->s_block_bitmap[bitmap_nr] = bh;
	return retval;
}

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

63 64 65
	if (block_group >= nr_groups) {
		udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
			  nr_groups);
L
Linus Torvalds 已提交
66 67
	}

68
	if (bitmap->s_block_bitmap[block_group]) {
L
Linus Torvalds 已提交
69
		return block_group;
70 71 72
	} else {
		retval = read_block_bitmap(sb, bitmap, block_group,
					   block_group);
L
Linus Torvalds 已提交
73 74 75 76 77 78
		if (retval < 0)
			return retval;
		return block_group;
	}
}

79 80 81
static inline int load_block_bitmap(struct super_block *sb,
				    struct udf_bitmap *bitmap,
				    unsigned int block_group)
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
	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;
}

96
static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
97
{
98
	struct udf_sb_info *sbi = UDF_SB(sb);
99 100
	struct logicalVolIntegrityDesc *lvid;

101 102
	if (!sbi->s_lvid_bh)
		return;
103 104

	lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
105
	le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
106
	udf_updated_lvid(sb);
107 108
}

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

I
Ingo Molnar 已提交
126
	mutex_lock(&sbi->s_alloc_mutex);
127 128 129 130
	partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
	if (bloc->logicalBlockNum < 0 ||
	    (bloc->logicalBlockNum + count) >
		partmap->s_partition_len) {
131
		udf_debug("%d < %d || %d + %d > %d\n",
132 133
			  bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
			  count, partmap->s_partition_len);
L
Linus Torvalds 已提交
134 135 136
		goto error_return;
	}

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

140 141 142 143 144 145 146 147 148 149 150
	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 已提交
151
		}
152 153 154 155 156 157 158 159 160 161 162 163
		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)
164
					dquot_free_block(inode, 1);
165
				udf_add_free_space(sb, sbi->s_partition, 1);
166 167 168 169 170 171 172 173 174
			}
		}
		mark_buffer_dirty(bh);
		if (overflow) {
			block += count;
			count = overflow;
		}
	} while (overflow);

175
error_return:
I
Ingo Molnar 已提交
176
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
177 178
}

179 180 181 182 183
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 已提交
184 185 186 187 188 189
{
	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 已提交
190
	__u32 part_len;
L
Linus Torvalds 已提交
191

I
Ingo Molnar 已提交
192
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
193
	part_len = sbi->s_partmaps[partition].s_partition_len;
194
	if (first_block >= part_len)
L
Linus Torvalds 已提交
195 196
		goto out;

M
Marcin Slusarz 已提交
197 198
	if (first_block + block_count > part_len)
		block_count = part_len - first_block;
L
Linus Torvalds 已提交
199

200 201 202 203 204
	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 已提交
205

206 207 208 209
		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 已提交
210

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

213 214 215
		while (bit < (sb->s_blocksize << 3) && block_count > 0) {
			if (!udf_test_bit(bit, bh->b_data))
				goto out;
216
			else if (dquot_prealloc_block(inode, 1))
217 218 219
				goto out;
			else if (!udf_clear_bit(bit, bh->b_data)) {
				udf_debug("bit already cleared for block %d\n", bit);
220
				dquot_free_block(inode, 1);
221 222 223 224 225 226
				goto out;
			}
			block_count--;
			alloc_count++;
			bit++;
			block++;
L
Linus Torvalds 已提交
227
		}
228 229 230
		mark_buffer_dirty(bh);
	} while (block_count > 0);

231
out:
232
	udf_add_free_space(sb, partition, -alloc_count);
I
Ingo Molnar 已提交
233
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
234 235 236
	return alloc_count;
}

237 238 239 240
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 已提交
241 242
{
	struct udf_sb_info *sbi = UDF_SB(sb);
243
	int newbit, bit = 0, block, block_group, group_start;
L
Linus Torvalds 已提交
244 245 246 247 248 249
	int end_goal, nr_groups, bitmap_nr, i;
	struct buffer_head *bh = NULL;
	char *ptr;
	int newblock = 0;

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

252
repeat:
253
	if (goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264
		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];
265 266
	ptr = memscan((char *)bh->b_data + group_start, 0xFF,
		      sb->s_blocksize - group_start);
L
Linus Torvalds 已提交
267

268
	if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
L
Linus Torvalds 已提交
269
		bit = block % (sb->s_blocksize << 3);
270
		if (udf_test_bit(bit, bh->b_data))
L
Linus Torvalds 已提交
271
			goto got_block;
272

L
Linus Torvalds 已提交
273 274 275 276
		end_goal = (bit + 63) & ~63;
		bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
		if (bit < end_goal)
			goto got_block;
277

M
Marcin Slusarz 已提交
278 279
		ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
			      sb->s_blocksize - ((bit + 7) >> 3));
L
Linus Torvalds 已提交
280
		newbit = (ptr - ((char *)bh->b_data)) << 3;
281
		if (newbit < sb->s_blocksize << 3) {
L
Linus Torvalds 已提交
282 283 284
			bit = newbit;
			goto search_back;
		}
285

M
Marcin Slusarz 已提交
286 287
		newbit = udf_find_next_one_bit(bh->b_data,
					       sb->s_blocksize << 3, bit);
288
		if (newbit < sb->s_blocksize << 3) {
L
Linus Torvalds 已提交
289 290 291 292 293
			bit = newbit;
			goto got_block;
		}
	}

294 295
	for (i = 0; i < (nr_groups * 2); i++) {
		block_group++;
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303
		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];
304
		if (i < nr_groups) {
305 306
			ptr = memscan((char *)bh->b_data + group_start, 0xFF,
				      sb->s_blocksize - group_start);
307
			if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
L
Linus Torvalds 已提交
308 309 310
				bit = (ptr - ((char *)bh->b_data)) << 3;
				break;
			}
311
		} else {
312 313 314
			bit = udf_find_next_one_bit((char *)bh->b_data,
						    sb->s_blocksize << 3,
						    group_start << 3);
L
Linus Torvalds 已提交
315 316 317 318
			if (bit < sb->s_blocksize << 3)
				break;
		}
	}
319
	if (i >= (nr_groups * 2)) {
I
Ingo Molnar 已提交
320
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
321 322 323 324 325
		return newblock;
	}
	if (bit < sb->s_blocksize << 3)
		goto search_back;
	else
M
Marcin Slusarz 已提交
326 327
		bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
					    group_start << 3);
328
	if (bit >= sb->s_blocksize << 3) {
I
Ingo Molnar 已提交
329
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
330 331 332
		return 0;
	}

333
search_back:
M
Marcin Slusarz 已提交
334 335 336 337 338 339
	i = 0;
	while (i < 7 && bit > (group_start << 3) &&
	       udf_test_bit(bit - 1, bh->b_data)) {
		++i;
		--bit;
	}
L
Linus Torvalds 已提交
340

341
got_block:
L
Linus Torvalds 已提交
342 343 344 345

	/*
	 * Check quota for allocation of this block.
	 */
346 347 348 349 350 351 352 353
	if (inode) {
		int ret = dquot_alloc_block(inode, 1);

		if (ret) {
			mutex_unlock(&sbi->s_alloc_mutex);
			*err = ret;
			return 0;
		}
L
Linus Torvalds 已提交
354 355 356
	}

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

359
	if (!udf_clear_bit(bit, bh->b_data)) {
L
Linus Torvalds 已提交
360 361 362 363 364 365
		udf_debug("bit already cleared for block %d\n", bit);
		goto repeat;
	}

	mark_buffer_dirty(bh);

366
	udf_add_free_space(sb, partition, -1);
I
Ingo Molnar 已提交
367
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
368 369 370
	*err = 0;
	return newblock;

371
error_return:
L
Linus Torvalds 已提交
372
	*err = -EIO;
I
Ingo Molnar 已提交
373
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
374 375 376
	return 0;
}

377 378 379
static void udf_table_free_blocks(struct super_block *sb,
				  struct inode *inode,
				  struct inode *table,
380 381
				  struct kernel_lb_addr *bloc,
				  uint32_t offset,
382
				  uint32_t count)
L
Linus Torvalds 已提交
383 384
{
	struct udf_sb_info *sbi = UDF_SB(sb);
385
	struct udf_part_map *partmap;
L
Linus Torvalds 已提交
386
	uint32_t start, end;
J
Jan Kara 已提交
387
	uint32_t elen;
388
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
389
	struct extent_position oepos, epos;
L
Linus Torvalds 已提交
390 391
	int8_t etype;
	int i;
392
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
393

I
Ingo Molnar 已提交
394
	mutex_lock(&sbi->s_alloc_mutex);
395 396 397 398
	partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
	if (bloc->logicalBlockNum < 0 ||
	    (bloc->logicalBlockNum + count) >
		partmap->s_partition_len) {
399
		udf_debug("%d < %d || %d + %d > %d\n",
400
			  bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
401
			  partmap->s_partition_len);
L
Linus Torvalds 已提交
402 403 404
		goto error_return;
	}

405
	iinfo = UDF_I(table);
M
Marcin Slusarz 已提交
406 407
	/* We do this up front - There are some error conditions that
	   could occure, but.. oh well */
L
Linus Torvalds 已提交
408
	if (inode)
409
		dquot_free_block(inode, count);
410
	udf_add_free_space(sb, sbi->s_partition, count);
L
Linus Torvalds 已提交
411

412 413
	start = bloc->logicalBlockNum + offset;
	end = bloc->logicalBlockNum + offset + count - 1;
L
Linus Torvalds 已提交
414

J
Jan Kara 已提交
415
	epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
L
Linus Torvalds 已提交
416
	elen = 0;
417
	epos.block = oepos.block = iinfo->i_location;
J
Jan Kara 已提交
418
	epos.bh = oepos.bh = NULL;
L
Linus Torvalds 已提交
419

420 421
	while (count &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
M
Marcin Slusarz 已提交
422 423 424 425 426 427 428 429 430 431
		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);
432
			} else {
M
Marcin Slusarz 已提交
433 434 435
				elen = (etype << 30) |
					(elen +
					(count << sb->s_blocksize_bits));
L
Linus Torvalds 已提交
436 437 438
				start += count;
				count = 0;
			}
439
			udf_write_aext(table, &oepos, &eloc, elen, 1);
440
		} else if (eloc.logicalBlockNum == (end + 1)) {
M
Marcin Slusarz 已提交
441 442 443 444 445 446 447 448 449
			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);
450
			} else {
L
Linus Torvalds 已提交
451
				eloc.logicalBlockNum = start;
M
Marcin Slusarz 已提交
452 453 454
				elen = (etype << 30) |
					(elen +
					(count << sb->s_blocksize_bits));
L
Linus Torvalds 已提交
455 456 457
				end -= count;
				count = 0;
			}
458
			udf_write_aext(table, &oepos, &eloc, elen, 1);
L
Linus Torvalds 已提交
459 460
		}

461
		if (epos.bh != oepos.bh) {
L
Linus Torvalds 已提交
462
			i = -1;
J
Jan Kara 已提交
463
			oepos.block = epos.block;
J
Jan Kara 已提交
464 465
			brelse(oepos.bh);
			get_bh(epos.bh);
J
Jan Kara 已提交
466 467
			oepos.bh = epos.bh;
			oepos.offset = 0;
468
		} else {
J
Jan Kara 已提交
469
			oepos.offset = epos.offset;
470
		}
L
Linus Torvalds 已提交
471 472
	}

473
	if (count) {
474
		/*
M
Marcin Slusarz 已提交
475 476 477
		 * 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 :)
478 479 480 481 482 483 484
		 *
		 * 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.
485
		 */
L
Linus Torvalds 已提交
486 487

		int adsize;
488 489
		struct short_ad *sad = NULL;
		struct long_ad *lad = NULL;
L
Linus Torvalds 已提交
490 491 492
		struct allocExtDesc *aed;

		eloc.logicalBlockNum = start;
493 494
		elen = EXT_RECORDED_ALLOCATED |
			(count << sb->s_blocksize_bits);
L
Linus Torvalds 已提交
495

496
		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
497
			adsize = sizeof(struct short_ad);
498
		else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
499
			adsize = sizeof(struct long_ad);
500
		else {
J
Jan Kara 已提交
501 502
			brelse(oepos.bh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
503 504 505
			goto error_return;
		}

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

J
Jan Kara 已提交
510
			brelse(oepos.bh);
J
Jan Kara 已提交
511
			oepos = epos;
L
Linus Torvalds 已提交
512 513

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

M
Marcin Slusarz 已提交
518
			epos.bh = udf_tread(sb,
519
					udf_get_lb_pblock(sb, &epos.block, 0));
M
Marcin Slusarz 已提交
520
			if (!epos.bh) {
J
Jan Kara 已提交
521
				brelse(oepos.bh);
L
Linus Torvalds 已提交
522 523
				goto error_return;
			}
J
Jan Kara 已提交
524
			aed = (struct allocExtDesc *)(epos.bh->b_data);
M
Marcin Slusarz 已提交
525 526
			aed->previousAllocExtLocation =
				cpu_to_le32(oepos.block.logicalBlockNum);
527
			if (epos.offset + adsize > sb->s_blocksize) {
J
Jan Kara 已提交
528
				loffset = epos.offset;
L
Linus Torvalds 已提交
529
				aed->lengthAllocDescs = cpu_to_le32(adsize);
530
				sptr = iinfo->i_ext.i_data + epos.offset
531
								- adsize;
M
Marcin Slusarz 已提交
532 533
				dptr = epos.bh->b_data +
					sizeof(struct allocExtDesc);
L
Linus Torvalds 已提交
534
				memcpy(dptr, sptr, adsize);
M
Marcin Slusarz 已提交
535 536
				epos.offset = sizeof(struct allocExtDesc) +
						adsize;
537
			} else {
J
Jan Kara 已提交
538
				loffset = epos.offset + adsize;
L
Linus Torvalds 已提交
539
				aed->lengthAllocDescs = cpu_to_le32(0);
540
				if (oepos.bh) {
541
					sptr = oepos.bh->b_data + epos.offset;
M
Marcin Slusarz 已提交
542 543
					aed = (struct allocExtDesc *)
						oepos.bh->b_data;
544 545
					le32_add_cpu(&aed->lengthAllocDescs,
							adsize);
546
				} else {
547
					sptr = iinfo->i_ext.i_data +
548
								epos.offset;
549
					iinfo->i_lenAlloc += adsize;
L
Linus Torvalds 已提交
550 551
					mark_inode_dirty(table);
				}
552
				epos.offset = sizeof(struct allocExtDesc);
L
Linus Torvalds 已提交
553
			}
M
Marcin Slusarz 已提交
554
			if (sbi->s_udfrev >= 0x0200)
M
Marcin Slusarz 已提交
555 556
				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
					    3, 1, epos.block.logicalBlockNum,
557
					    sizeof(struct tag));
L
Linus Torvalds 已提交
558
			else
M
Marcin Slusarz 已提交
559 560
				udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
					    2, 1, epos.block.logicalBlockNum,
561
					    sizeof(struct tag));
562

563
			switch (iinfo->i_alloc_type) {
M
Marcin Slusarz 已提交
564
			case ICBTAG_FLAG_AD_SHORT:
565
				sad = (struct short_ad *)sptr;
M
Marcin Slusarz 已提交
566 567 568 569 570 571 572
				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:
573
				lad = (struct long_ad *)sptr;
M
Marcin Slusarz 已提交
574 575 576 577 578 579
				lad->extLength = cpu_to_le32(
					EXT_NEXT_EXTENT_ALLOCDECS |
					sb->s_blocksize);
				lad->extLocation =
					cpu_to_lelb(epos.block);
				break;
L
Linus Torvalds 已提交
580
			}
581
			if (oepos.bh) {
J
Jan Kara 已提交
582 583
				udf_update_tag(oepos.bh->b_data, loffset);
				mark_buffer_dirty(oepos.bh);
584
			} else {
L
Linus Torvalds 已提交
585
				mark_inode_dirty(table);
586
			}
L
Linus Torvalds 已提交
587 588
		}

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

593
			if (!epos.bh) {
594
				iinfo->i_lenAlloc += adsize;
L
Linus Torvalds 已提交
595
				mark_inode_dirty(table);
596
			} else {
J
Jan Kara 已提交
597
				aed = (struct allocExtDesc *)epos.bh->b_data;
598
				le32_add_cpu(&aed->lengthAllocDescs, adsize);
J
Jan Kara 已提交
599 600
				udf_update_tag(epos.bh->b_data, epos.offset);
				mark_buffer_dirty(epos.bh);
L
Linus Torvalds 已提交
601 602 603 604
			}
		}
	}

J
Jan Kara 已提交
605 606
	brelse(epos.bh);
	brelse(oepos.bh);
L
Linus Torvalds 已提交
607

608
error_return:
I
Ingo Molnar 已提交
609
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
610 611 612
	return;
}

613 614 615 616
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 已提交
617 618 619
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	int alloc_count = 0;
J
Jan Kara 已提交
620
	uint32_t elen, adsize;
621
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
622
	struct extent_position epos;
L
Linus Torvalds 已提交
623
	int8_t etype = -1;
624
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
625

626
	if (first_block >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
627 628
		return 0;

629 630
	iinfo = UDF_I(table);
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
631
		adsize = sizeof(struct short_ad);
632
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
633
		adsize = sizeof(struct long_ad);
L
Linus Torvalds 已提交
634 635 636
	else
		return 0;

I
Ingo Molnar 已提交
637
	mutex_lock(&sbi->s_alloc_mutex);
J
Jan Kara 已提交
638
	epos.offset = sizeof(struct unallocSpaceEntry);
639
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
640
	epos.bh = NULL;
L
Linus Torvalds 已提交
641 642
	eloc.logicalBlockNum = 0xFFFFFFFF;

643 644
	while (first_block != eloc.logicalBlockNum &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
L
Linus Torvalds 已提交
645
		udf_debug("eloc=%d, elen=%d, first_block=%d\n",
646
			  eloc.logicalBlockNum, elen, first_block);
647
		; /* empty loop body */
L
Linus Torvalds 已提交
648 649
	}

650
	if (first_block == eloc.logicalBlockNum) {
J
Jan Kara 已提交
651
		epos.offset -= adsize;
L
Linus Torvalds 已提交
652 653

		alloc_count = (elen >> sb->s_blocksize_bits);
654
		if (inode && dquot_prealloc_block(inode,
M
Marcin Slusarz 已提交
655
			alloc_count > block_count ? block_count : alloc_count))
L
Linus Torvalds 已提交
656
			alloc_count = 0;
M
Marcin Slusarz 已提交
657
		else if (alloc_count > block_count) {
L
Linus Torvalds 已提交
658 659 660
			alloc_count = block_count;
			eloc.logicalBlockNum += alloc_count;
			elen -= (alloc_count << sb->s_blocksize_bits);
661
			udf_write_aext(table, &epos, &eloc,
M
Marcin Slusarz 已提交
662 663 664 665
					(etype << 30) | elen, 1);
		} else
			udf_delete_aext(table, epos, eloc,
					(etype << 30) | elen);
666
	} else {
L
Linus Torvalds 已提交
667
		alloc_count = 0;
668
	}
L
Linus Torvalds 已提交
669

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

672 673
	if (alloc_count)
		udf_add_free_space(sb, partition, -alloc_count);
I
Ingo Molnar 已提交
674
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
675 676 677
	return alloc_count;
}

678 679 680 681
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 已提交
682 683 684 685
{
	struct udf_sb_info *sbi = UDF_SB(sb);
	uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
	uint32_t newblock = 0, adsize;
J
Jan Kara 已提交
686
	uint32_t elen, goal_elen = 0;
687
	struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
J
Jan Kara 已提交
688
	struct extent_position epos, goal_epos;
L
Linus Torvalds 已提交
689
	int8_t etype;
690
	struct udf_inode_info *iinfo = UDF_I(table);
L
Linus Torvalds 已提交
691 692 693

	*err = -ENOSPC;

694
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
695
		adsize = sizeof(struct short_ad);
696
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
697
		adsize = sizeof(struct long_ad);
L
Linus Torvalds 已提交
698 699 700
	else
		return newblock;

I
Ingo Molnar 已提交
701
	mutex_lock(&sbi->s_alloc_mutex);
702
	if (goal >= sbi->s_partmaps[partition].s_partition_len)
L
Linus Torvalds 已提交
703 704
		goal = 0;

M
Marcin Slusarz 已提交
705 706 707 708
	/* 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.
709
	 */
J
Jan Kara 已提交
710
	epos.offset = sizeof(struct unallocSpaceEntry);
711
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
712
	epos.bh = goal_epos.bh = NULL;
L
Linus Torvalds 已提交
713

714 715
	while (spread &&
	       (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
716
		if (goal >= eloc.logicalBlockNum) {
M
Marcin Slusarz 已提交
717 718
			if (goal < eloc.logicalBlockNum +
					(elen >> sb->s_blocksize_bits))
L
Linus Torvalds 已提交
719 720 721
				nspread = 0;
			else
				nspread = goal - eloc.logicalBlockNum -
722 723
					(elen >> sb->s_blocksize_bits);
		} else {
L
Linus Torvalds 已提交
724
			nspread = eloc.logicalBlockNum - goal;
725
		}
L
Linus Torvalds 已提交
726

727
		if (nspread < spread) {
L
Linus Torvalds 已提交
728
			spread = nspread;
729
			if (goal_epos.bh != epos.bh) {
J
Jan Kara 已提交
730
				brelse(goal_epos.bh);
J
Jan Kara 已提交
731
				goal_epos.bh = epos.bh;
J
Jan Kara 已提交
732
				get_bh(goal_epos.bh);
L
Linus Torvalds 已提交
733
			}
J
Jan Kara 已提交
734 735
			goal_epos.block = epos.block;
			goal_epos.offset = epos.offset - adsize;
L
Linus Torvalds 已提交
736 737 738 739 740
			goal_eloc = eloc;
			goal_elen = (etype << 30) | elen;
		}
	}

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

743
	if (spread == 0xFFFFFFFF) {
J
Jan Kara 已提交
744
		brelse(goal_epos.bh);
I
Ingo Molnar 已提交
745
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
746 747 748 749 750 751 752 753 754
		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;
755
	goal_eloc.logicalBlockNum++;
L
Linus Torvalds 已提交
756
	goal_elen -= sb->s_blocksize;
757 758 759 760 761 762 763
	if (inode) {
		*err = dquot_alloc_block(inode, 1);
		if (*err) {
			brelse(goal_epos.bh);
			mutex_unlock(&sbi->s_alloc_mutex);
			return 0;
		}
L
Linus Torvalds 已提交
764 765 766
	}

	if (goal_elen)
767
		udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
L
Linus Torvalds 已提交
768
	else
J
Jan Kara 已提交
769
		udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
J
Jan Kara 已提交
770
	brelse(goal_epos.bh);
L
Linus Torvalds 已提交
771

772
	udf_add_free_space(sb, partition, -1);
L
Linus Torvalds 已提交
773

I
Ingo Molnar 已提交
774
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
775 776 777 778
	*err = 0;
	return newblock;
}

779 780 781
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 已提交
782
{
783
	uint16_t partition = bloc->partitionReferenceNum;
M
Marcin Slusarz 已提交
784
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
L
Linus Torvalds 已提交
785

M
Marcin Slusarz 已提交
786
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
J
Jan Kara 已提交
787 788
		udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
				       bloc, offset, count);
M
Marcin Slusarz 已提交
789
	} else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
J
Jan Kara 已提交
790 791
		udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
				      bloc, offset, count);
M
Marcin Slusarz 已提交
792
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
J
Jan Kara 已提交
793 794
		udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
				       bloc, offset, count);
M
Marcin Slusarz 已提交
795
	} else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
J
Jan Kara 已提交
796 797
		udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
				      bloc, offset, count);
798
	}
L
Linus Torvalds 已提交
799 800
}

801 802 803 804
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 已提交
805
{
M
Marcin Slusarz 已提交
806 807
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];

M
Marcin Slusarz 已提交
808
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
L
Linus Torvalds 已提交
809
		return udf_bitmap_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
810
						  map->s_uspace.s_bitmap,
M
Marcin Slusarz 已提交
811 812 813
						  partition, first_block,
						  block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
L
Linus Torvalds 已提交
814
		return udf_table_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
815
						 map->s_uspace.s_table,
M
Marcin Slusarz 已提交
816 817 818
						 partition, first_block,
						 block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
L
Linus Torvalds 已提交
819
		return udf_bitmap_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
820
						  map->s_fspace.s_bitmap,
M
Marcin Slusarz 已提交
821 822 823
						  partition, first_block,
						  block_count);
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
L
Linus Torvalds 已提交
824
		return udf_table_prealloc_blocks(sb, inode,
M
Marcin Slusarz 已提交
825
						 map->s_fspace.s_table,
M
Marcin Slusarz 已提交
826 827 828
						 partition, first_block,
						 block_count);
	else
L
Linus Torvalds 已提交
829 830 831
		return 0;
}

832 833 834
inline int udf_new_block(struct super_block *sb,
			 struct inode *inode,
			 uint16_t partition, uint32_t goal, int *err)
L
Linus Torvalds 已提交
835
{
M
Marcin Slusarz 已提交
836
	struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
J
Jan Kara 已提交
837

M
Marcin Slusarz 已提交
838 839
	if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
		return udf_bitmap_new_block(sb, inode,
M
Marcin Slusarz 已提交
840
					   map->s_uspace.s_bitmap,
841
					   partition, goal, err);
M
Marcin Slusarz 已提交
842
	else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
L
Linus Torvalds 已提交
843
		return udf_table_new_block(sb, inode,
M
Marcin Slusarz 已提交
844
					   map->s_uspace.s_table,
845
					   partition, goal, err);
M
Marcin Slusarz 已提交
846
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
L
Linus Torvalds 已提交
847
		return udf_bitmap_new_block(sb, inode,
M
Marcin Slusarz 已提交
848
					    map->s_fspace.s_bitmap,
849
					    partition, goal, err);
M
Marcin Slusarz 已提交
850
	else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
L
Linus Torvalds 已提交
851
		return udf_table_new_block(sb, inode,
M
Marcin Slusarz 已提交
852
					   map->s_fspace.s_table,
853
					   partition, goal, err);
M
Marcin Slusarz 已提交
854
	else {
L
Linus Torvalds 已提交
855 856 857 858
		*err = -EIO;
		return 0;
	}
}