]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/udf/balloc.c
Merge tag 'devicetree-fixes-for-5.11-2' of git://git.kernel.org/pub/scm/linux/kernel...
[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) {
fcbf7637 61 udf_debug("block_group (%u) > nr_groups (%d)\n",
a983f368 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) {
fcbf7637 125 udf_debug("%u < %d || %u + %u > %u\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)) {
fcbf7637 154 udf_debug("bit %lu already set\n", bit + i);
4daa1b87 155 udf_debug("byte=%2x\n",
fcbf7637 156 ((__u8 *)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;
849fe89c
CIK
178 int bit, block, block_group;
179 int bitmap_nr;
1da177e4 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 191 do {
4daa1b87
MS
192 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
193 block_group = block >> (sb->s_blocksize_bits + 3);
1da177e4 194
4daa1b87
MS
195 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
196 if (bitmap_nr < 0)
197 goto out;
198 bh = bitmap->s_block_bitmap[bitmap_nr];
1da177e4 199
4daa1b87 200 bit = block % (sb->s_blocksize << 3);
1da177e4 201
4daa1b87 202 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
36350462 203 if (!udf_clear_bit(bit, bh->b_data))
4daa1b87 204 goto out;
4daa1b87
MS
205 block_count--;
206 alloc_count++;
207 bit++;
208 block++;
1da177e4 209 }
4daa1b87
MS
210 mark_buffer_dirty(bh);
211 } while (block_count > 0);
212
28de7948 213out:
146bca72 214 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 215 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
216 return alloc_count;
217}
218
b490bdd6 219static udf_pblk_t udf_bitmap_new_block(struct super_block *sb,
cb00ea35
CG
220 struct udf_bitmap *bitmap, uint16_t partition,
221 uint32_t goal, int *err)
1da177e4
LT
222{
223 struct udf_sb_info *sbi = UDF_SB(sb);
b490bdd6
SM
224 int newbit, bit = 0;
225 udf_pblk_t block;
226 int block_group, group_start;
1da177e4
LT
227 int end_goal, nr_groups, bitmap_nr, i;
228 struct buffer_head *bh = NULL;
229 char *ptr;
b490bdd6 230 udf_pblk_t newblock = 0;
1da177e4
LT
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
56db1991
SM
328 if (newblock >= sbi->s_partmaps[partition].s_partition_len) {
329 /*
330 * Ran off the end of the bitmap, and bits following are
331 * non-compliant (not all zero)
332 */
333 udf_err(sb, "bitmap for partition %d corrupted (block %u marked"
334 " as free, partition length is %u)\n", partition,
335 newblock, sbi->s_partmaps[partition].s_partition_len);
336 goto error_return;
337 }
338
cb00ea35 339 if (!udf_clear_bit(bit, bh->b_data)) {
1da177e4
LT
340 udf_debug("bit already cleared for block %d\n", bit);
341 goto repeat;
342 }
343
344 mark_buffer_dirty(bh);
345
146bca72 346 udf_add_free_space(sb, partition, -1);
1e7933de 347 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
348 *err = 0;
349 return newblock;
350
28de7948 351error_return:
1da177e4 352 *err = -EIO;
1e7933de 353 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
354 return 0;
355}
356
cb00ea35 357static void udf_table_free_blocks(struct super_block *sb,
cb00ea35 358 struct inode *table,
97e961fd
PE
359 struct kernel_lb_addr *bloc,
360 uint32_t offset,
cb00ea35 361 uint32_t count)
1da177e4
LT
362{
363 struct udf_sb_info *sbi = UDF_SB(sb);
97e961fd 364 struct udf_part_map *partmap;
1da177e4 365 uint32_t start, end;
ff116fc8 366 uint32_t elen;
5ca4e4be 367 struct kernel_lb_addr eloc;
ff116fc8 368 struct extent_position oepos, epos;
1da177e4 369 int8_t etype;
48d6d8ff 370 struct udf_inode_info *iinfo;
1da177e4 371
1e7933de 372 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 373 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
374 if (bloc->logicalBlockNum + count < count ||
375 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
fcbf7637 376 udf_debug("%u < %d || %u + %u > %u\n",
a983f368
JP
377 bloc->logicalBlockNum, 0,
378 bloc->logicalBlockNum, count,
97e961fd 379 partmap->s_partition_len);
1da177e4
LT
380 goto error_return;
381 }
382
48d6d8ff 383 iinfo = UDF_I(table);
146bca72 384 udf_add_free_space(sb, sbi->s_partition, count);
1da177e4 385
97e961fd
PE
386 start = bloc->logicalBlockNum + offset;
387 end = bloc->logicalBlockNum + offset + count - 1;
1da177e4 388
ff116fc8 389 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
1da177e4 390 elen = 0;
48d6d8ff 391 epos.block = oepos.block = iinfo->i_location;
ff116fc8 392 epos.bh = oepos.bh = NULL;
1da177e4 393
28de7948
CG
394 while (count &&
395 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
4b11111a
MS
396 if (((eloc.logicalBlockNum +
397 (elen >> sb->s_blocksize_bits)) == start)) {
398 if ((0x3FFFFFFF - elen) <
399 (count << sb->s_blocksize_bits)) {
400 uint32_t tmp = ((0x3FFFFFFF - elen) >>
401 sb->s_blocksize_bits);
402 count -= tmp;
403 start += tmp;
404 elen = (etype << 30) |
405 (0x40000000 - sb->s_blocksize);
cb00ea35 406 } else {
4b11111a
MS
407 elen = (etype << 30) |
408 (elen +
409 (count << sb->s_blocksize_bits));
1da177e4
LT
410 start += count;
411 count = 0;
412 }
97e961fd 413 udf_write_aext(table, &oepos, &eloc, elen, 1);
cb00ea35 414 } else if (eloc.logicalBlockNum == (end + 1)) {
4b11111a
MS
415 if ((0x3FFFFFFF - elen) <
416 (count << sb->s_blocksize_bits)) {
417 uint32_t tmp = ((0x3FFFFFFF - elen) >>
418 sb->s_blocksize_bits);
419 count -= tmp;
420 end -= tmp;
421 eloc.logicalBlockNum -= tmp;
422 elen = (etype << 30) |
423 (0x40000000 - sb->s_blocksize);
cb00ea35 424 } else {
1da177e4 425 eloc.logicalBlockNum = start;
4b11111a
MS
426 elen = (etype << 30) |
427 (elen +
428 (count << sb->s_blocksize_bits));
1da177e4
LT
429 end -= count;
430 count = 0;
431 }
97e961fd 432 udf_write_aext(table, &oepos, &eloc, elen, 1);
1da177e4
LT
433 }
434
cb00ea35 435 if (epos.bh != oepos.bh) {
ff116fc8 436 oepos.block = epos.block;
3bf25cb4
JK
437 brelse(oepos.bh);
438 get_bh(epos.bh);
ff116fc8
JK
439 oepos.bh = epos.bh;
440 oepos.offset = 0;
28de7948 441 } else {
ff116fc8 442 oepos.offset = epos.offset;
28de7948 443 }
1da177e4
LT
444 }
445
cb00ea35 446 if (count) {
28de7948 447 /*
4b11111a
MS
448 * NOTE: we CANNOT use udf_add_aext here, as it can try to
449 * allocate a new block, and since we hold the super block
450 * lock already very bad things would happen :)
28de7948
CG
451 *
452 * We copy the behavior of udf_add_aext, but instead of
453 * trying to allocate a new block close to the existing one,
454 * we just steal a block from the extent we are trying to add.
455 *
456 * It would be nice if the blocks were close together, but it
457 * isn't required.
cb00ea35 458 */
1da177e4
LT
459
460 int adsize;
1da177e4
LT
461
462 eloc.logicalBlockNum = start;
28de7948
CG
463 elen = EXT_RECORDED_ALLOCATED |
464 (count << sb->s_blocksize_bits);
1da177e4 465
48d6d8ff 466 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 467 adsize = sizeof(struct short_ad);
48d6d8ff 468 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 469 adsize = sizeof(struct long_ad);
48d6d8ff 470 else {
3bf25cb4
JK
471 brelse(oepos.bh);
472 brelse(epos.bh);
1da177e4
LT
473 goto error_return;
474 }
475
cb00ea35 476 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
1da177e4 477 /* Steal a block from the extent being free'd */
fcea62ba
JK
478 udf_setup_indirect_aext(table, eloc.logicalBlockNum,
479 &epos);
480
cb00ea35 481 eloc.logicalBlockNum++;
1da177e4 482 elen -= sb->s_blocksize;
1da177e4
LT
483 }
484
4b11111a 485 /* It's possible that stealing the block emptied the extent */
fcea62ba
JK
486 if (elen)
487 __udf_add_aext(table, &epos, &eloc, elen, 1);
1da177e4
LT
488 }
489
3bf25cb4
JK
490 brelse(epos.bh);
491 brelse(oepos.bh);
1da177e4 492
28de7948 493error_return:
1e7933de 494 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
495 return;
496}
497
cb00ea35 498static int udf_table_prealloc_blocks(struct super_block *sb,
cb00ea35
CG
499 struct inode *table, uint16_t partition,
500 uint32_t first_block, uint32_t block_count)
1da177e4
LT
501{
502 struct udf_sb_info *sbi = UDF_SB(sb);
503 int alloc_count = 0;
ff116fc8 504 uint32_t elen, adsize;
5ca4e4be 505 struct kernel_lb_addr eloc;
ff116fc8 506 struct extent_position epos;
1da177e4 507 int8_t etype = -1;
48d6d8ff 508 struct udf_inode_info *iinfo;
1da177e4 509
3391faa4 510 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
511 return 0;
512
48d6d8ff
MS
513 iinfo = UDF_I(table);
514 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 515 adsize = sizeof(struct short_ad);
48d6d8ff 516 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 517 adsize = sizeof(struct long_ad);
1da177e4
LT
518 else
519 return 0;
520
1e7933de 521 mutex_lock(&sbi->s_alloc_mutex);
ff116fc8 522 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 523 epos.block = iinfo->i_location;
ff116fc8 524 epos.bh = NULL;
1da177e4
LT
525 eloc.logicalBlockNum = 0xFFFFFFFF;
526
28de7948
CG
527 while (first_block != eloc.logicalBlockNum &&
528 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
fcbf7637 529 udf_debug("eloc=%u, elen=%u, first_block=%u\n",
cb00ea35 530 eloc.logicalBlockNum, elen, first_block);
28de7948 531 ; /* empty loop body */
1da177e4
LT
532 }
533
cb00ea35 534 if (first_block == eloc.logicalBlockNum) {
ff116fc8 535 epos.offset -= adsize;
1da177e4
LT
536
537 alloc_count = (elen >> sb->s_blocksize_bits);
36350462 538 if (alloc_count > block_count) {
1da177e4
LT
539 alloc_count = block_count;
540 eloc.logicalBlockNum += alloc_count;
541 elen -= (alloc_count << sb->s_blocksize_bits);
97e961fd 542 udf_write_aext(table, &epos, &eloc,
4b11111a
MS
543 (etype << 30) | elen, 1);
544 } else
6c1e4d06 545 udf_delete_aext(table, epos);
28de7948 546 } else {
1da177e4 547 alloc_count = 0;
28de7948 548 }
1da177e4 549
3bf25cb4 550 brelse(epos.bh);
1da177e4 551
146bca72
JK
552 if (alloc_count)
553 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 554 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
555 return alloc_count;
556}
557
b490bdd6 558static udf_pblk_t udf_table_new_block(struct super_block *sb,
cb00ea35
CG
559 struct inode *table, uint16_t partition,
560 uint32_t goal, int *err)
1da177e4
LT
561{
562 struct udf_sb_info *sbi = UDF_SB(sb);
563 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
b490bdd6
SM
564 udf_pblk_t newblock = 0;
565 uint32_t adsize;
ff116fc8 566 uint32_t elen, goal_elen = 0;
3f649ab7 567 struct kernel_lb_addr eloc, goal_eloc;
ff116fc8 568 struct extent_position epos, goal_epos;
1da177e4 569 int8_t etype;
48d6d8ff 570 struct udf_inode_info *iinfo = UDF_I(table);
1da177e4
LT
571
572 *err = -ENOSPC;
573
48d6d8ff 574 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 575 adsize = sizeof(struct short_ad);
48d6d8ff 576 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 577 adsize = sizeof(struct long_ad);
1da177e4
LT
578 else
579 return newblock;
580
1e7933de 581 mutex_lock(&sbi->s_alloc_mutex);
3391faa4 582 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
583 goal = 0;
584
4b11111a
MS
585 /* We search for the closest matching block to goal. If we find
586 a exact hit, we stop. Otherwise we keep going till we run out
587 of extents. We store the buffer_head, bloc, and extoffset
588 of the current closest match and use that when we are done.
cb00ea35 589 */
ff116fc8 590 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 591 epos.block = iinfo->i_location;
ff116fc8 592 epos.bh = goal_epos.bh = NULL;
1da177e4 593
28de7948
CG
594 while (spread &&
595 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
cb00ea35 596 if (goal >= eloc.logicalBlockNum) {
4b11111a
MS
597 if (goal < eloc.logicalBlockNum +
598 (elen >> sb->s_blocksize_bits))
1da177e4
LT
599 nspread = 0;
600 else
601 nspread = goal - eloc.logicalBlockNum -
28de7948
CG
602 (elen >> sb->s_blocksize_bits);
603 } else {
1da177e4 604 nspread = eloc.logicalBlockNum - goal;
28de7948 605 }
1da177e4 606
cb00ea35 607 if (nspread < spread) {
1da177e4 608 spread = nspread;
cb00ea35 609 if (goal_epos.bh != epos.bh) {
3bf25cb4 610 brelse(goal_epos.bh);
ff116fc8 611 goal_epos.bh = epos.bh;
3bf25cb4 612 get_bh(goal_epos.bh);
1da177e4 613 }
ff116fc8
JK
614 goal_epos.block = epos.block;
615 goal_epos.offset = epos.offset - adsize;
1da177e4
LT
616 goal_eloc = eloc;
617 goal_elen = (etype << 30) | elen;
618 }
619 }
620
3bf25cb4 621 brelse(epos.bh);
1da177e4 622
cb00ea35 623 if (spread == 0xFFFFFFFF) {
3bf25cb4 624 brelse(goal_epos.bh);
1e7933de 625 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
626 return 0;
627 }
628
629 /* Only allocate blocks from the beginning of the extent.
630 That way, we only delete (empty) extents, never have to insert an
631 extent because of splitting */
632 /* This works, but very poorly.... */
633
634 newblock = goal_eloc.logicalBlockNum;
cb00ea35 635 goal_eloc.logicalBlockNum++;
1da177e4 636 goal_elen -= sb->s_blocksize;
1da177e4
LT
637
638 if (goal_elen)
97e961fd 639 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
1da177e4 640 else
6c1e4d06 641 udf_delete_aext(table, goal_epos);
3bf25cb4 642 brelse(goal_epos.bh);
1da177e4 643
146bca72 644 udf_add_free_space(sb, partition, -1);
1da177e4 645
1e7933de 646 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
647 *err = 0;
648 return newblock;
649}
650
97e961fd
PE
651void udf_free_blocks(struct super_block *sb, struct inode *inode,
652 struct kernel_lb_addr *bloc, uint32_t offset,
653 uint32_t count)
1da177e4 654{
97e961fd 655 uint16_t partition = bloc->partitionReferenceNum;
6c79e987 656 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1da177e4 657
6c79e987 658 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
fd4287db 659 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
e650b94a 660 bloc, offset, count);
6c79e987 661 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
fd4287db 662 udf_table_free_blocks(sb, map->s_uspace.s_table,
e650b94a 663 bloc, offset, count);
28de7948 664 }
fd4287db
JK
665
666 if (inode) {
667 inode_sub_bytes(inode,
668 ((sector_t)count) << sb->s_blocksize_bits);
669 }
1da177e4
LT
670}
671
cb00ea35
CG
672inline int udf_prealloc_blocks(struct super_block *sb,
673 struct inode *inode,
674 uint16_t partition, uint32_t first_block,
675 uint32_t block_count)
1da177e4 676{
6c79e987 677 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1be440de 678 int allocated;
6c79e987 679
4b11111a 680 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
681 allocated = udf_bitmap_prealloc_blocks(sb,
682 map->s_uspace.s_bitmap,
683 partition, first_block,
684 block_count);
4b11111a 685 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
686 allocated = udf_table_prealloc_blocks(sb,
687 map->s_uspace.s_table,
688 partition, first_block,
689 block_count);
4b11111a 690 else
1da177e4 691 return 0;
fd4287db
JK
692
693 if (inode && allocated > 0)
694 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
695 return allocated;
1da177e4
LT
696}
697
b490bdd6 698inline udf_pblk_t udf_new_block(struct super_block *sb,
cb00ea35
CG
699 struct inode *inode,
700 uint16_t partition, uint32_t goal, int *err)
1da177e4 701{
6c79e987 702 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
b490bdd6 703 udf_pblk_t block;
3bf25cb4 704
4b11111a 705 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
706 block = udf_bitmap_new_block(sb,
707 map->s_uspace.s_bitmap,
708 partition, goal, err);
4b11111a 709 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
710 block = udf_table_new_block(sb,
711 map->s_uspace.s_table,
28de7948 712 partition, goal, err);
4b11111a 713 else {
1da177e4
LT
714 *err = -EIO;
715 return 0;
716 }
fd4287db
JK
717 if (inode && block)
718 inode_add_bytes(inode, sb->s_blocksize);
719 return block;
1da177e4 720}