Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / udf / balloc.c
CommitLineData
1da177e4
LT
1/*
2 * balloc.c
3 *
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
6 *
1da177e4
LT
7 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1999-2001 Ben Fennema
14 * (C) 1999 Stelias Computing Inc
15 *
16 * HISTORY
17 *
18 * 02/24/99 blf Created.
19 *
20 */
21
22#include "udfdecl.h"
23
24#include <linux/quotaops.h>
25#include <linux/buffer_head.h>
26#include <linux/bitops.h>
27
28#include "udf_i.h"
29#include "udf_sb.h"
30
4b11111a
MS
31#define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
32#define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
1da177e4 33#define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
4b11111a 34#define udf_find_next_one_bit(addr, size, offset) \
3a065fcf 35 ext2_find_next_bit(addr, size, offset)
1da177e4 36
cb00ea35
CG
37static int read_block_bitmap(struct super_block *sb,
38 struct udf_bitmap *bitmap, unsigned int block,
39 unsigned long bitmap_nr)
1da177e4
LT
40{
41 struct buffer_head *bh = NULL;
42 int retval = 0;
5ca4e4be 43 struct kernel_lb_addr loc;
1da177e4
LT
44
45 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 46 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1da177e4 47
97e961fd 48 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
4b11111a 49 if (!bh)
1da177e4 50 retval = -EIO;
4b11111a 51
1da177e4
LT
52 bitmap->s_block_bitmap[bitmap_nr] = bh;
53 return retval;
54}
55
cb00ea35
CG
56static int __load_block_bitmap(struct super_block *sb,
57 struct udf_bitmap *bitmap,
58 unsigned int block_group)
1da177e4
LT
59{
60 int retval = 0;
61 int nr_groups = bitmap->s_nr_groups;
62
cb00ea35
CG
63 if (block_group >= nr_groups) {
64 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
65 nr_groups);
1da177e4
LT
66 }
67
28de7948 68 if (bitmap->s_block_bitmap[block_group]) {
1da177e4 69 return block_group;
28de7948
CG
70 } else {
71 retval = read_block_bitmap(sb, bitmap, block_group,
72 block_group);
1da177e4
LT
73 if (retval < 0)
74 return retval;
75 return block_group;
76 }
77}
78
cb00ea35
CG
79static inline int load_block_bitmap(struct super_block *sb,
80 struct udf_bitmap *bitmap,
81 unsigned int block_group)
1da177e4
LT
82{
83 int slot;
84
85 slot = __load_block_bitmap(sb, bitmap, block_group);
86
87 if (slot < 0)
88 return slot;
89
90 if (!bitmap->s_block_bitmap[slot])
91 return -EIO;
92
93 return slot;
94}
95
146bca72 96static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
742ba02a 97{
146bca72 98 struct udf_sb_info *sbi = UDF_SB(sb);
742ba02a
MS
99 struct logicalVolIntegrityDesc *lvid;
100
146bca72
JK
101 if (!sbi->s_lvid_bh)
102 return;
742ba02a
MS
103
104 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
c2104fda 105 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
146bca72 106 udf_updated_lvid(sb);
742ba02a
MS
107}
108
cb00ea35
CG
109static void udf_bitmap_free_blocks(struct super_block *sb,
110 struct inode *inode,
111 struct udf_bitmap *bitmap,
97e961fd
PE
112 struct kernel_lb_addr *bloc,
113 uint32_t offset,
cb00ea35 114 uint32_t count)
1da177e4
LT
115{
116 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 117 struct buffer_head *bh = NULL;
97e961fd 118 struct udf_part_map *partmap;
1da177e4
LT
119 unsigned long block;
120 unsigned long block_group;
121 unsigned long bit;
122 unsigned long i;
123 int bitmap_nr;
124 unsigned long overflow;
125
1e7933de 126 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 127 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
128 if (bloc->logicalBlockNum + count < count ||
129 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
28de7948 130 udf_debug("%d < %d || %d + %d > %d\n",
97e961fd
PE
131 bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
132 count, partmap->s_partition_len);
1da177e4
LT
133 goto error_return;
134 }
135
97e961fd 136 block = bloc->logicalBlockNum + offset +
4b11111a 137 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 138
4daa1b87
MS
139 do {
140 overflow = 0;
141 block_group = block >> (sb->s_blocksize_bits + 3);
142 bit = block % (sb->s_blocksize << 3);
143
144 /*
145 * Check to see if we are freeing blocks across a group boundary.
146 */
147 if (bit + count > (sb->s_blocksize << 3)) {
148 overflow = bit + count - (sb->s_blocksize << 3);
149 count -= overflow;
1da177e4 150 }
4daa1b87
MS
151 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
152 if (bitmap_nr < 0)
153 goto error_return;
154
155 bh = bitmap->s_block_bitmap[bitmap_nr];
156 for (i = 0; i < count; i++) {
157 if (udf_set_bit(bit + i, bh->b_data)) {
158 udf_debug("bit %ld already set\n", bit + i);
159 udf_debug("byte=%2x\n",
160 ((char *)bh->b_data)[(bit + i) >> 3]);
161 } else {
162 if (inode)
5dd4056d 163 dquot_free_block(inode, 1);
146bca72 164 udf_add_free_space(sb, sbi->s_partition, 1);
4daa1b87
MS
165 }
166 }
167 mark_buffer_dirty(bh);
168 if (overflow) {
169 block += count;
170 count = overflow;
171 }
172 } while (overflow);
173
28de7948 174error_return:
1e7933de 175 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
176}
177
cb00ea35
CG
178static int udf_bitmap_prealloc_blocks(struct super_block *sb,
179 struct inode *inode,
180 struct udf_bitmap *bitmap,
181 uint16_t partition, uint32_t first_block,
182 uint32_t block_count)
1da177e4
LT
183{
184 struct udf_sb_info *sbi = UDF_SB(sb);
185 int alloc_count = 0;
186 int bit, block, block_group, group_start;
187 int nr_groups, bitmap_nr;
188 struct buffer_head *bh;
6c79e987 189 __u32 part_len;
1da177e4 190
1e7933de 191 mutex_lock(&sbi->s_alloc_mutex);
6c79e987 192 part_len = sbi->s_partmaps[partition].s_partition_len;
3391faa4 193 if (first_block >= part_len)
1da177e4
LT
194 goto out;
195
6c79e987
MS
196 if (first_block + block_count > part_len)
197 block_count = part_len - first_block;
1da177e4 198
4daa1b87
MS
199 do {
200 nr_groups = udf_compute_nr_groups(sb, partition);
201 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
202 block_group = block >> (sb->s_blocksize_bits + 3);
203 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
1da177e4 204
4daa1b87
MS
205 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
206 if (bitmap_nr < 0)
207 goto out;
208 bh = bitmap->s_block_bitmap[bitmap_nr];
1da177e4 209
4daa1b87 210 bit = block % (sb->s_blocksize << 3);
1da177e4 211
4daa1b87
MS
212 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
213 if (!udf_test_bit(bit, bh->b_data))
214 goto out;
5dd4056d 215 else if (dquot_prealloc_block(inode, 1))
4daa1b87
MS
216 goto out;
217 else if (!udf_clear_bit(bit, bh->b_data)) {
218 udf_debug("bit already cleared for block %d\n", bit);
5dd4056d 219 dquot_free_block(inode, 1);
4daa1b87
MS
220 goto out;
221 }
222 block_count--;
223 alloc_count++;
224 bit++;
225 block++;
1da177e4 226 }
4daa1b87
MS
227 mark_buffer_dirty(bh);
228 } while (block_count > 0);
229
28de7948 230out:
146bca72 231 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 232 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
233 return alloc_count;
234}
235
cb00ea35
CG
236static int udf_bitmap_new_block(struct super_block *sb,
237 struct inode *inode,
238 struct udf_bitmap *bitmap, uint16_t partition,
239 uint32_t goal, int *err)
1da177e4
LT
240{
241 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 242 int newbit, bit = 0, block, block_group, group_start;
1da177e4
LT
243 int end_goal, nr_groups, bitmap_nr, i;
244 struct buffer_head *bh = NULL;
245 char *ptr;
246 int newblock = 0;
247
248 *err = -ENOSPC;
1e7933de 249 mutex_lock(&sbi->s_alloc_mutex);
1da177e4 250
28de7948 251repeat:
3391faa4 252 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
253 goal = 0;
254
255 nr_groups = bitmap->s_nr_groups;
256 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
257 block_group = block >> (sb->s_blocksize_bits + 3);
258 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
259
260 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
261 if (bitmap_nr < 0)
262 goto error_return;
263 bh = bitmap->s_block_bitmap[bitmap_nr];
28de7948
CG
264 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
265 sb->s_blocksize - group_start);
1da177e4 266
cb00ea35 267 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4 268 bit = block % (sb->s_blocksize << 3);
28de7948 269 if (udf_test_bit(bit, bh->b_data))
1da177e4 270 goto got_block;
28de7948 271
1da177e4
LT
272 end_goal = (bit + 63) & ~63;
273 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
274 if (bit < end_goal)
275 goto got_block;
28de7948 276
4b11111a
MS
277 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
278 sb->s_blocksize - ((bit + 7) >> 3));
1da177e4 279 newbit = (ptr - ((char *)bh->b_data)) << 3;
cb00ea35 280 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
281 bit = newbit;
282 goto search_back;
283 }
28de7948 284
4b11111a
MS
285 newbit = udf_find_next_one_bit(bh->b_data,
286 sb->s_blocksize << 3, bit);
cb00ea35 287 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
288 bit = newbit;
289 goto got_block;
290 }
291 }
292
cb00ea35
CG
293 for (i = 0; i < (nr_groups * 2); i++) {
294 block_group++;
1da177e4
LT
295 if (block_group >= nr_groups)
296 block_group = 0;
297 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
298
299 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
300 if (bitmap_nr < 0)
301 goto error_return;
302 bh = bitmap->s_block_bitmap[bitmap_nr];
cb00ea35 303 if (i < nr_groups) {
28de7948
CG
304 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
305 sb->s_blocksize - group_start);
cb00ea35 306 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4
LT
307 bit = (ptr - ((char *)bh->b_data)) << 3;
308 break;
309 }
cb00ea35 310 } else {
28de7948
CG
311 bit = udf_find_next_one_bit((char *)bh->b_data,
312 sb->s_blocksize << 3,
313 group_start << 3);
1da177e4
LT
314 if (bit < sb->s_blocksize << 3)
315 break;
316 }
317 }
cb00ea35 318 if (i >= (nr_groups * 2)) {
1e7933de 319 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
320 return newblock;
321 }
322 if (bit < sb->s_blocksize << 3)
323 goto search_back;
324 else
4b11111a
MS
325 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
326 group_start << 3);
cb00ea35 327 if (bit >= sb->s_blocksize << 3) {
1e7933de 328 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
329 return 0;
330 }
331
28de7948 332search_back:
4b11111a
MS
333 i = 0;
334 while (i < 7 && bit > (group_start << 3) &&
335 udf_test_bit(bit - 1, bh->b_data)) {
336 ++i;
337 --bit;
338 }
1da177e4 339
28de7948 340got_block:
1da177e4
LT
341
342 /*
343 * Check quota for allocation of this block.
344 */
5dd4056d
CH
345 if (inode) {
346 int ret = dquot_alloc_block(inode, 1);
347
348 if (ret) {
349 mutex_unlock(&sbi->s_alloc_mutex);
350 *err = ret;
351 return 0;
352 }
1da177e4
LT
353 }
354
355 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
28de7948 356 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 357
cb00ea35 358 if (!udf_clear_bit(bit, bh->b_data)) {
1da177e4
LT
359 udf_debug("bit already cleared for block %d\n", bit);
360 goto repeat;
361 }
362
363 mark_buffer_dirty(bh);
364
146bca72 365 udf_add_free_space(sb, partition, -1);
1e7933de 366 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
367 *err = 0;
368 return newblock;
369
28de7948 370error_return:
1da177e4 371 *err = -EIO;
1e7933de 372 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
373 return 0;
374}
375
cb00ea35
CG
376static void udf_table_free_blocks(struct super_block *sb,
377 struct inode *inode,
378 struct inode *table,
97e961fd
PE
379 struct kernel_lb_addr *bloc,
380 uint32_t offset,
cb00ea35 381 uint32_t count)
1da177e4
LT
382{
383 struct udf_sb_info *sbi = UDF_SB(sb);
97e961fd 384 struct udf_part_map *partmap;
1da177e4 385 uint32_t start, end;
ff116fc8 386 uint32_t elen;
5ca4e4be 387 struct kernel_lb_addr eloc;
ff116fc8 388 struct extent_position oepos, epos;
1da177e4
LT
389 int8_t etype;
390 int i;
48d6d8ff 391 struct udf_inode_info *iinfo;
1da177e4 392
1e7933de 393 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 394 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
395 if (bloc->logicalBlockNum + count < count ||
396 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
28de7948 397 udf_debug("%d < %d || %d + %d > %d\n",
1fefd086 398 bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
97e961fd 399 partmap->s_partition_len);
1da177e4
LT
400 goto error_return;
401 }
402
48d6d8ff 403 iinfo = UDF_I(table);
4b11111a
MS
404 /* We do this up front - There are some error conditions that
405 could occure, but.. oh well */
1da177e4 406 if (inode)
5dd4056d 407 dquot_free_block(inode, count);
146bca72 408 udf_add_free_space(sb, sbi->s_partition, count);
1da177e4 409
97e961fd
PE
410 start = bloc->logicalBlockNum + offset;
411 end = bloc->logicalBlockNum + offset + count - 1;
1da177e4 412
ff116fc8 413 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
1da177e4 414 elen = 0;
48d6d8ff 415 epos.block = oepos.block = iinfo->i_location;
ff116fc8 416 epos.bh = oepos.bh = NULL;
1da177e4 417
28de7948
CG
418 while (count &&
419 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
4b11111a
MS
420 if (((eloc.logicalBlockNum +
421 (elen >> sb->s_blocksize_bits)) == start)) {
422 if ((0x3FFFFFFF - elen) <
423 (count << sb->s_blocksize_bits)) {
424 uint32_t tmp = ((0x3FFFFFFF - elen) >>
425 sb->s_blocksize_bits);
426 count -= tmp;
427 start += tmp;
428 elen = (etype << 30) |
429 (0x40000000 - sb->s_blocksize);
cb00ea35 430 } else {
4b11111a
MS
431 elen = (etype << 30) |
432 (elen +
433 (count << sb->s_blocksize_bits));
1da177e4
LT
434 start += count;
435 count = 0;
436 }
97e961fd 437 udf_write_aext(table, &oepos, &eloc, elen, 1);
cb00ea35 438 } else if (eloc.logicalBlockNum == (end + 1)) {
4b11111a
MS
439 if ((0x3FFFFFFF - elen) <
440 (count << sb->s_blocksize_bits)) {
441 uint32_t tmp = ((0x3FFFFFFF - elen) >>
442 sb->s_blocksize_bits);
443 count -= tmp;
444 end -= tmp;
445 eloc.logicalBlockNum -= tmp;
446 elen = (etype << 30) |
447 (0x40000000 - sb->s_blocksize);
cb00ea35 448 } else {
1da177e4 449 eloc.logicalBlockNum = start;
4b11111a
MS
450 elen = (etype << 30) |
451 (elen +
452 (count << sb->s_blocksize_bits));
1da177e4
LT
453 end -= count;
454 count = 0;
455 }
97e961fd 456 udf_write_aext(table, &oepos, &eloc, elen, 1);
1da177e4
LT
457 }
458
cb00ea35 459 if (epos.bh != oepos.bh) {
1da177e4 460 i = -1;
ff116fc8 461 oepos.block = epos.block;
3bf25cb4
JK
462 brelse(oepos.bh);
463 get_bh(epos.bh);
ff116fc8
JK
464 oepos.bh = epos.bh;
465 oepos.offset = 0;
28de7948 466 } else {
ff116fc8 467 oepos.offset = epos.offset;
28de7948 468 }
1da177e4
LT
469 }
470
cb00ea35 471 if (count) {
28de7948 472 /*
4b11111a
MS
473 * NOTE: we CANNOT use udf_add_aext here, as it can try to
474 * allocate a new block, and since we hold the super block
475 * lock already very bad things would happen :)
28de7948
CG
476 *
477 * We copy the behavior of udf_add_aext, but instead of
478 * trying to allocate a new block close to the existing one,
479 * we just steal a block from the extent we are trying to add.
480 *
481 * It would be nice if the blocks were close together, but it
482 * isn't required.
cb00ea35 483 */
1da177e4
LT
484
485 int adsize;
5ca4e4be
PE
486 struct short_ad *sad = NULL;
487 struct long_ad *lad = NULL;
1da177e4
LT
488 struct allocExtDesc *aed;
489
490 eloc.logicalBlockNum = start;
28de7948
CG
491 elen = EXT_RECORDED_ALLOCATED |
492 (count << sb->s_blocksize_bits);
1da177e4 493
48d6d8ff 494 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 495 adsize = sizeof(struct short_ad);
48d6d8ff 496 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 497 adsize = sizeof(struct long_ad);
48d6d8ff 498 else {
3bf25cb4
JK
499 brelse(oepos.bh);
500 brelse(epos.bh);
1da177e4
LT
501 goto error_return;
502 }
503
cb00ea35 504 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
391e8bbd 505 unsigned char *sptr, *dptr;
1da177e4 506 int loffset;
cb00ea35 507
3bf25cb4 508 brelse(oepos.bh);
ff116fc8 509 oepos = epos;
1da177e4
LT
510
511 /* Steal a block from the extent being free'd */
ff116fc8 512 epos.block.logicalBlockNum = eloc.logicalBlockNum;
cb00ea35 513 eloc.logicalBlockNum++;
1da177e4
LT
514 elen -= sb->s_blocksize;
515
4b11111a 516 epos.bh = udf_tread(sb,
97e961fd 517 udf_get_lb_pblock(sb, &epos.block, 0));
4b11111a 518 if (!epos.bh) {
3bf25cb4 519 brelse(oepos.bh);
1da177e4
LT
520 goto error_return;
521 }
ff116fc8 522 aed = (struct allocExtDesc *)(epos.bh->b_data);
4b11111a
MS
523 aed->previousAllocExtLocation =
524 cpu_to_le32(oepos.block.logicalBlockNum);
cb00ea35 525 if (epos.offset + adsize > sb->s_blocksize) {
ff116fc8 526 loffset = epos.offset;
1da177e4 527 aed->lengthAllocDescs = cpu_to_le32(adsize);
48d6d8ff 528 sptr = iinfo->i_ext.i_data + epos.offset
c0b34438 529 - adsize;
4b11111a
MS
530 dptr = epos.bh->b_data +
531 sizeof(struct allocExtDesc);
1da177e4 532 memcpy(dptr, sptr, adsize);
4b11111a
MS
533 epos.offset = sizeof(struct allocExtDesc) +
534 adsize;
cb00ea35 535 } else {
ff116fc8 536 loffset = epos.offset + adsize;
1da177e4 537 aed->lengthAllocDescs = cpu_to_le32(0);
cb00ea35 538 if (oepos.bh) {
f5cc15da 539 sptr = oepos.bh->b_data + epos.offset;
4b11111a
MS
540 aed = (struct allocExtDesc *)
541 oepos.bh->b_data;
c2104fda 542 le32_add_cpu(&aed->lengthAllocDescs,
543 adsize);
cb00ea35 544 } else {
48d6d8ff 545 sptr = iinfo->i_ext.i_data +
c0b34438 546 epos.offset;
48d6d8ff 547 iinfo->i_lenAlloc += adsize;
1da177e4
LT
548 mark_inode_dirty(table);
549 }
f5cc15da 550 epos.offset = sizeof(struct allocExtDesc);
1da177e4 551 }
6c79e987 552 if (sbi->s_udfrev >= 0x0200)
4b11111a
MS
553 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
554 3, 1, epos.block.logicalBlockNum,
5ca4e4be 555 sizeof(struct tag));
1da177e4 556 else
4b11111a
MS
557 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
558 2, 1, epos.block.logicalBlockNum,
5ca4e4be 559 sizeof(struct tag));
28de7948 560
48d6d8ff 561 switch (iinfo->i_alloc_type) {
4b11111a 562 case ICBTAG_FLAG_AD_SHORT:
5ca4e4be 563 sad = (struct short_ad *)sptr;
4b11111a
MS
564 sad->extLength = cpu_to_le32(
565 EXT_NEXT_EXTENT_ALLOCDECS |
566 sb->s_blocksize);
567 sad->extPosition =
568 cpu_to_le32(epos.block.logicalBlockNum);
569 break;
570 case ICBTAG_FLAG_AD_LONG:
5ca4e4be 571 lad = (struct long_ad *)sptr;
4b11111a
MS
572 lad->extLength = cpu_to_le32(
573 EXT_NEXT_EXTENT_ALLOCDECS |
574 sb->s_blocksize);
575 lad->extLocation =
576 cpu_to_lelb(epos.block);
577 break;
1da177e4 578 }
cb00ea35 579 if (oepos.bh) {
ff116fc8
JK
580 udf_update_tag(oepos.bh->b_data, loffset);
581 mark_buffer_dirty(oepos.bh);
28de7948 582 } else {
1da177e4 583 mark_inode_dirty(table);
28de7948 584 }
1da177e4
LT
585 }
586
4b11111a
MS
587 /* It's possible that stealing the block emptied the extent */
588 if (elen) {
97e961fd 589 udf_write_aext(table, &epos, &eloc, elen, 1);
1da177e4 590
cb00ea35 591 if (!epos.bh) {
48d6d8ff 592 iinfo->i_lenAlloc += adsize;
1da177e4 593 mark_inode_dirty(table);
cb00ea35 594 } else {
ff116fc8 595 aed = (struct allocExtDesc *)epos.bh->b_data;
c2104fda 596 le32_add_cpu(&aed->lengthAllocDescs, adsize);
ff116fc8
JK
597 udf_update_tag(epos.bh->b_data, epos.offset);
598 mark_buffer_dirty(epos.bh);
1da177e4
LT
599 }
600 }
601 }
602
3bf25cb4
JK
603 brelse(epos.bh);
604 brelse(oepos.bh);
1da177e4 605
28de7948 606error_return:
1e7933de 607 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
608 return;
609}
610
cb00ea35
CG
611static int udf_table_prealloc_blocks(struct super_block *sb,
612 struct inode *inode,
613 struct inode *table, uint16_t partition,
614 uint32_t first_block, uint32_t block_count)
1da177e4
LT
615{
616 struct udf_sb_info *sbi = UDF_SB(sb);
617 int alloc_count = 0;
ff116fc8 618 uint32_t elen, adsize;
5ca4e4be 619 struct kernel_lb_addr eloc;
ff116fc8 620 struct extent_position epos;
1da177e4 621 int8_t etype = -1;
48d6d8ff 622 struct udf_inode_info *iinfo;
1da177e4 623
3391faa4 624 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
625 return 0;
626
48d6d8ff
MS
627 iinfo = UDF_I(table);
628 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 629 adsize = sizeof(struct short_ad);
48d6d8ff 630 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 631 adsize = sizeof(struct long_ad);
1da177e4
LT
632 else
633 return 0;
634
1e7933de 635 mutex_lock(&sbi->s_alloc_mutex);
ff116fc8 636 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 637 epos.block = iinfo->i_location;
ff116fc8 638 epos.bh = NULL;
1da177e4
LT
639 eloc.logicalBlockNum = 0xFFFFFFFF;
640
28de7948
CG
641 while (first_block != eloc.logicalBlockNum &&
642 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
1da177e4 643 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
cb00ea35 644 eloc.logicalBlockNum, elen, first_block);
28de7948 645 ; /* empty loop body */
1da177e4
LT
646 }
647
cb00ea35 648 if (first_block == eloc.logicalBlockNum) {
ff116fc8 649 epos.offset -= adsize;
1da177e4
LT
650
651 alloc_count = (elen >> sb->s_blocksize_bits);
5dd4056d 652 if (inode && dquot_prealloc_block(inode,
4b11111a 653 alloc_count > block_count ? block_count : alloc_count))
1da177e4 654 alloc_count = 0;
4b11111a 655 else if (alloc_count > block_count) {
1da177e4
LT
656 alloc_count = block_count;
657 eloc.logicalBlockNum += alloc_count;
658 elen -= (alloc_count << sb->s_blocksize_bits);
97e961fd 659 udf_write_aext(table, &epos, &eloc,
4b11111a
MS
660 (etype << 30) | elen, 1);
661 } else
662 udf_delete_aext(table, epos, eloc,
663 (etype << 30) | elen);
28de7948 664 } else {
1da177e4 665 alloc_count = 0;
28de7948 666 }
1da177e4 667
3bf25cb4 668 brelse(epos.bh);
1da177e4 669
146bca72
JK
670 if (alloc_count)
671 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 672 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
673 return alloc_count;
674}
675
cb00ea35
CG
676static int udf_table_new_block(struct super_block *sb,
677 struct inode *inode,
678 struct inode *table, uint16_t partition,
679 uint32_t goal, int *err)
1da177e4
LT
680{
681 struct udf_sb_info *sbi = UDF_SB(sb);
682 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
683 uint32_t newblock = 0, adsize;
ff116fc8 684 uint32_t elen, goal_elen = 0;
5ca4e4be 685 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
ff116fc8 686 struct extent_position epos, goal_epos;
1da177e4 687 int8_t etype;
48d6d8ff 688 struct udf_inode_info *iinfo = UDF_I(table);
1da177e4
LT
689
690 *err = -ENOSPC;
691
48d6d8ff 692 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 693 adsize = sizeof(struct short_ad);
48d6d8ff 694 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 695 adsize = sizeof(struct long_ad);
1da177e4
LT
696 else
697 return newblock;
698
1e7933de 699 mutex_lock(&sbi->s_alloc_mutex);
3391faa4 700 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
701 goal = 0;
702
4b11111a
MS
703 /* We search for the closest matching block to goal. If we find
704 a exact hit, we stop. Otherwise we keep going till we run out
705 of extents. We store the buffer_head, bloc, and extoffset
706 of the current closest match and use that when we are done.
cb00ea35 707 */
ff116fc8 708 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 709 epos.block = iinfo->i_location;
ff116fc8 710 epos.bh = goal_epos.bh = NULL;
1da177e4 711
28de7948
CG
712 while (spread &&
713 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
cb00ea35 714 if (goal >= eloc.logicalBlockNum) {
4b11111a
MS
715 if (goal < eloc.logicalBlockNum +
716 (elen >> sb->s_blocksize_bits))
1da177e4
LT
717 nspread = 0;
718 else
719 nspread = goal - eloc.logicalBlockNum -
28de7948
CG
720 (elen >> sb->s_blocksize_bits);
721 } else {
1da177e4 722 nspread = eloc.logicalBlockNum - goal;
28de7948 723 }
1da177e4 724
cb00ea35 725 if (nspread < spread) {
1da177e4 726 spread = nspread;
cb00ea35 727 if (goal_epos.bh != epos.bh) {
3bf25cb4 728 brelse(goal_epos.bh);
ff116fc8 729 goal_epos.bh = epos.bh;
3bf25cb4 730 get_bh(goal_epos.bh);
1da177e4 731 }
ff116fc8
JK
732 goal_epos.block = epos.block;
733 goal_epos.offset = epos.offset - adsize;
1da177e4
LT
734 goal_eloc = eloc;
735 goal_elen = (etype << 30) | elen;
736 }
737 }
738
3bf25cb4 739 brelse(epos.bh);
1da177e4 740
cb00ea35 741 if (spread == 0xFFFFFFFF) {
3bf25cb4 742 brelse(goal_epos.bh);
1e7933de 743 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
744 return 0;
745 }
746
747 /* Only allocate blocks from the beginning of the extent.
748 That way, we only delete (empty) extents, never have to insert an
749 extent because of splitting */
750 /* This works, but very poorly.... */
751
752 newblock = goal_eloc.logicalBlockNum;
cb00ea35 753 goal_eloc.logicalBlockNum++;
1da177e4 754 goal_elen -= sb->s_blocksize;
5dd4056d
CH
755 if (inode) {
756 *err = dquot_alloc_block(inode, 1);
757 if (*err) {
758 brelse(goal_epos.bh);
759 mutex_unlock(&sbi->s_alloc_mutex);
760 return 0;
761 }
1da177e4
LT
762 }
763
764 if (goal_elen)
97e961fd 765 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
1da177e4 766 else
ff116fc8 767 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
3bf25cb4 768 brelse(goal_epos.bh);
1da177e4 769
146bca72 770 udf_add_free_space(sb, partition, -1);
1da177e4 771
1e7933de 772 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
773 *err = 0;
774 return newblock;
775}
776
97e961fd
PE
777void udf_free_blocks(struct super_block *sb, struct inode *inode,
778 struct kernel_lb_addr *bloc, uint32_t offset,
779 uint32_t count)
1da177e4 780{
97e961fd 781 uint16_t partition = bloc->partitionReferenceNum;
6c79e987 782 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1da177e4 783
6c79e987 784 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
e650b94a
JK
785 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
786 bloc, offset, count);
6c79e987 787 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
e650b94a
JK
788 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
789 bloc, offset, count);
6c79e987 790 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
e650b94a
JK
791 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
792 bloc, offset, count);
6c79e987 793 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
e650b94a
JK
794 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
795 bloc, offset, count);
28de7948 796 }
1da177e4
LT
797}
798
cb00ea35
CG
799inline int udf_prealloc_blocks(struct super_block *sb,
800 struct inode *inode,
801 uint16_t partition, uint32_t first_block,
802 uint32_t block_count)
1da177e4 803{
6c79e987
MS
804 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
805
4b11111a 806 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1da177e4 807 return udf_bitmap_prealloc_blocks(sb, inode,
6c79e987 808 map->s_uspace.s_bitmap,
4b11111a
MS
809 partition, first_block,
810 block_count);
811 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1da177e4 812 return udf_table_prealloc_blocks(sb, inode,
6c79e987 813 map->s_uspace.s_table,
4b11111a
MS
814 partition, first_block,
815 block_count);
816 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1da177e4 817 return udf_bitmap_prealloc_blocks(sb, inode,
6c79e987 818 map->s_fspace.s_bitmap,
4b11111a
MS
819 partition, first_block,
820 block_count);
821 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1da177e4 822 return udf_table_prealloc_blocks(sb, inode,
6c79e987 823 map->s_fspace.s_table,
4b11111a
MS
824 partition, first_block,
825 block_count);
826 else
1da177e4
LT
827 return 0;
828}
829
cb00ea35
CG
830inline int udf_new_block(struct super_block *sb,
831 struct inode *inode,
832 uint16_t partition, uint32_t goal, int *err)
1da177e4 833{
6c79e987 834 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
3bf25cb4 835
4b11111a
MS
836 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
837 return udf_bitmap_new_block(sb, inode,
6c79e987 838 map->s_uspace.s_bitmap,
28de7948 839 partition, goal, err);
4b11111a 840 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1da177e4 841 return udf_table_new_block(sb, inode,
6c79e987 842 map->s_uspace.s_table,
28de7948 843 partition, goal, err);
4b11111a 844 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1da177e4 845 return udf_bitmap_new_block(sb, inode,
6c79e987 846 map->s_fspace.s_bitmap,
28de7948 847 partition, goal, err);
4b11111a 848 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1da177e4 849 return udf_table_new_block(sb, inode,
6c79e987 850 map->s_fspace.s_table,
28de7948 851 partition, goal, err);
4b11111a 852 else {
1da177e4
LT
853 *err = -EIO;
854 return 0;
855 }
856}