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