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