Merge tag 'mfd-next-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[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
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;
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
b490bdd6 221static udf_pblk_t 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);
b490bdd6
SM
226 int newbit, bit = 0;
227 udf_pblk_t block;
228 int block_group, group_start;
1da177e4
LT
229 int end_goal, nr_groups, bitmap_nr, i;
230 struct buffer_head *bh = NULL;
231 char *ptr;
b490bdd6 232 udf_pblk_t newblock = 0;
1da177e4
LT
233
234 *err = -ENOSPC;
1e7933de 235 mutex_lock(&sbi->s_alloc_mutex);
1da177e4 236
28de7948 237repeat:
3391faa4 238 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
239 goal = 0;
240
241 nr_groups = bitmap->s_nr_groups;
242 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
243 block_group = block >> (sb->s_blocksize_bits + 3);
244 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
245
246 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
247 if (bitmap_nr < 0)
248 goto error_return;
249 bh = bitmap->s_block_bitmap[bitmap_nr];
28de7948
CG
250 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
251 sb->s_blocksize - group_start);
1da177e4 252
cb00ea35 253 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4 254 bit = block % (sb->s_blocksize << 3);
28de7948 255 if (udf_test_bit(bit, bh->b_data))
1da177e4 256 goto got_block;
28de7948 257
1da177e4
LT
258 end_goal = (bit + 63) & ~63;
259 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
260 if (bit < end_goal)
261 goto got_block;
28de7948 262
4b11111a
MS
263 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
264 sb->s_blocksize - ((bit + 7) >> 3));
1da177e4 265 newbit = (ptr - ((char *)bh->b_data)) << 3;
cb00ea35 266 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
267 bit = newbit;
268 goto search_back;
269 }
28de7948 270
4b11111a
MS
271 newbit = udf_find_next_one_bit(bh->b_data,
272 sb->s_blocksize << 3, bit);
cb00ea35 273 if (newbit < sb->s_blocksize << 3) {
1da177e4
LT
274 bit = newbit;
275 goto got_block;
276 }
277 }
278
cb00ea35
CG
279 for (i = 0; i < (nr_groups * 2); i++) {
280 block_group++;
1da177e4
LT
281 if (block_group >= nr_groups)
282 block_group = 0;
283 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
284
285 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
286 if (bitmap_nr < 0)
287 goto error_return;
288 bh = bitmap->s_block_bitmap[bitmap_nr];
cb00ea35 289 if (i < nr_groups) {
28de7948
CG
290 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
291 sb->s_blocksize - group_start);
cb00ea35 292 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
1da177e4
LT
293 bit = (ptr - ((char *)bh->b_data)) << 3;
294 break;
295 }
cb00ea35 296 } else {
6f644e5f 297 bit = udf_find_next_one_bit(bh->b_data,
28de7948
CG
298 sb->s_blocksize << 3,
299 group_start << 3);
1da177e4
LT
300 if (bit < sb->s_blocksize << 3)
301 break;
302 }
303 }
cb00ea35 304 if (i >= (nr_groups * 2)) {
1e7933de 305 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
306 return newblock;
307 }
308 if (bit < sb->s_blocksize << 3)
309 goto search_back;
310 else
4b11111a
MS
311 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
312 group_start << 3);
cb00ea35 313 if (bit >= sb->s_blocksize << 3) {
1e7933de 314 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
315 return 0;
316 }
317
28de7948 318search_back:
4b11111a
MS
319 i = 0;
320 while (i < 7 && bit > (group_start << 3) &&
321 udf_test_bit(bit - 1, bh->b_data)) {
322 ++i;
323 --bit;
324 }
1da177e4 325
28de7948 326got_block:
1da177e4 327 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
28de7948 328 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 329
cb00ea35 330 if (!udf_clear_bit(bit, bh->b_data)) {
1da177e4
LT
331 udf_debug("bit already cleared for block %d\n", bit);
332 goto repeat;
333 }
334
335 mark_buffer_dirty(bh);
336
146bca72 337 udf_add_free_space(sb, partition, -1);
1e7933de 338 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
339 *err = 0;
340 return newblock;
341
28de7948 342error_return:
1da177e4 343 *err = -EIO;
1e7933de 344 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
345 return 0;
346}
347
cb00ea35 348static void udf_table_free_blocks(struct super_block *sb,
cb00ea35 349 struct inode *table,
97e961fd
PE
350 struct kernel_lb_addr *bloc,
351 uint32_t offset,
cb00ea35 352 uint32_t count)
1da177e4
LT
353{
354 struct udf_sb_info *sbi = UDF_SB(sb);
97e961fd 355 struct udf_part_map *partmap;
1da177e4 356 uint32_t start, end;
ff116fc8 357 uint32_t elen;
5ca4e4be 358 struct kernel_lb_addr eloc;
ff116fc8 359 struct extent_position oepos, epos;
1da177e4 360 int8_t etype;
48d6d8ff 361 struct udf_inode_info *iinfo;
1da177e4 362
1e7933de 363 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 364 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
365 if (bloc->logicalBlockNum + count < count ||
366 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
fcbf7637 367 udf_debug("%u < %d || %u + %u > %u\n",
a983f368
JP
368 bloc->logicalBlockNum, 0,
369 bloc->logicalBlockNum, count,
97e961fd 370 partmap->s_partition_len);
1da177e4
LT
371 goto error_return;
372 }
373
48d6d8ff 374 iinfo = UDF_I(table);
146bca72 375 udf_add_free_space(sb, sbi->s_partition, count);
1da177e4 376
97e961fd
PE
377 start = bloc->logicalBlockNum + offset;
378 end = bloc->logicalBlockNum + offset + count - 1;
1da177e4 379
ff116fc8 380 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
1da177e4 381 elen = 0;
48d6d8ff 382 epos.block = oepos.block = iinfo->i_location;
ff116fc8 383 epos.bh = oepos.bh = NULL;
1da177e4 384
28de7948
CG
385 while (count &&
386 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
4b11111a
MS
387 if (((eloc.logicalBlockNum +
388 (elen >> sb->s_blocksize_bits)) == start)) {
389 if ((0x3FFFFFFF - elen) <
390 (count << sb->s_blocksize_bits)) {
391 uint32_t tmp = ((0x3FFFFFFF - elen) >>
392 sb->s_blocksize_bits);
393 count -= tmp;
394 start += tmp;
395 elen = (etype << 30) |
396 (0x40000000 - sb->s_blocksize);
cb00ea35 397 } else {
4b11111a
MS
398 elen = (etype << 30) |
399 (elen +
400 (count << sb->s_blocksize_bits));
1da177e4
LT
401 start += count;
402 count = 0;
403 }
97e961fd 404 udf_write_aext(table, &oepos, &eloc, elen, 1);
cb00ea35 405 } else if (eloc.logicalBlockNum == (end + 1)) {
4b11111a
MS
406 if ((0x3FFFFFFF - elen) <
407 (count << sb->s_blocksize_bits)) {
408 uint32_t tmp = ((0x3FFFFFFF - elen) >>
409 sb->s_blocksize_bits);
410 count -= tmp;
411 end -= tmp;
412 eloc.logicalBlockNum -= tmp;
413 elen = (etype << 30) |
414 (0x40000000 - sb->s_blocksize);
cb00ea35 415 } else {
1da177e4 416 eloc.logicalBlockNum = start;
4b11111a
MS
417 elen = (etype << 30) |
418 (elen +
419 (count << sb->s_blocksize_bits));
1da177e4
LT
420 end -= count;
421 count = 0;
422 }
97e961fd 423 udf_write_aext(table, &oepos, &eloc, elen, 1);
1da177e4
LT
424 }
425
cb00ea35 426 if (epos.bh != oepos.bh) {
ff116fc8 427 oepos.block = epos.block;
3bf25cb4
JK
428 brelse(oepos.bh);
429 get_bh(epos.bh);
ff116fc8
JK
430 oepos.bh = epos.bh;
431 oepos.offset = 0;
28de7948 432 } else {
ff116fc8 433 oepos.offset = epos.offset;
28de7948 434 }
1da177e4
LT
435 }
436
cb00ea35 437 if (count) {
28de7948 438 /*
4b11111a
MS
439 * NOTE: we CANNOT use udf_add_aext here, as it can try to
440 * allocate a new block, and since we hold the super block
441 * lock already very bad things would happen :)
28de7948
CG
442 *
443 * We copy the behavior of udf_add_aext, but instead of
444 * trying to allocate a new block close to the existing one,
445 * we just steal a block from the extent we are trying to add.
446 *
447 * It would be nice if the blocks were close together, but it
448 * isn't required.
cb00ea35 449 */
1da177e4
LT
450
451 int adsize;
1da177e4
LT
452
453 eloc.logicalBlockNum = start;
28de7948
CG
454 elen = EXT_RECORDED_ALLOCATED |
455 (count << sb->s_blocksize_bits);
1da177e4 456
48d6d8ff 457 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 458 adsize = sizeof(struct short_ad);
48d6d8ff 459 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 460 adsize = sizeof(struct long_ad);
48d6d8ff 461 else {
3bf25cb4
JK
462 brelse(oepos.bh);
463 brelse(epos.bh);
1da177e4
LT
464 goto error_return;
465 }
466
cb00ea35 467 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
1da177e4 468 /* Steal a block from the extent being free'd */
fcea62ba
JK
469 udf_setup_indirect_aext(table, eloc.logicalBlockNum,
470 &epos);
471
cb00ea35 472 eloc.logicalBlockNum++;
1da177e4 473 elen -= sb->s_blocksize;
1da177e4
LT
474 }
475
4b11111a 476 /* It's possible that stealing the block emptied the extent */
fcea62ba
JK
477 if (elen)
478 __udf_add_aext(table, &epos, &eloc, elen, 1);
1da177e4
LT
479 }
480
3bf25cb4
JK
481 brelse(epos.bh);
482 brelse(oepos.bh);
1da177e4 483
28de7948 484error_return:
1e7933de 485 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
486 return;
487}
488
cb00ea35 489static int udf_table_prealloc_blocks(struct super_block *sb,
cb00ea35
CG
490 struct inode *table, uint16_t partition,
491 uint32_t first_block, uint32_t block_count)
1da177e4
LT
492{
493 struct udf_sb_info *sbi = UDF_SB(sb);
494 int alloc_count = 0;
ff116fc8 495 uint32_t elen, adsize;
5ca4e4be 496 struct kernel_lb_addr eloc;
ff116fc8 497 struct extent_position epos;
1da177e4 498 int8_t etype = -1;
48d6d8ff 499 struct udf_inode_info *iinfo;
1da177e4 500
3391faa4 501 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
502 return 0;
503
48d6d8ff
MS
504 iinfo = UDF_I(table);
505 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 506 adsize = sizeof(struct short_ad);
48d6d8ff 507 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 508 adsize = sizeof(struct long_ad);
1da177e4
LT
509 else
510 return 0;
511
1e7933de 512 mutex_lock(&sbi->s_alloc_mutex);
ff116fc8 513 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 514 epos.block = iinfo->i_location;
ff116fc8 515 epos.bh = NULL;
1da177e4
LT
516 eloc.logicalBlockNum = 0xFFFFFFFF;
517
28de7948
CG
518 while (first_block != eloc.logicalBlockNum &&
519 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
fcbf7637 520 udf_debug("eloc=%u, elen=%u, first_block=%u\n",
cb00ea35 521 eloc.logicalBlockNum, elen, first_block);
28de7948 522 ; /* empty loop body */
1da177e4
LT
523 }
524
cb00ea35 525 if (first_block == eloc.logicalBlockNum) {
ff116fc8 526 epos.offset -= adsize;
1da177e4
LT
527
528 alloc_count = (elen >> sb->s_blocksize_bits);
36350462 529 if (alloc_count > block_count) {
1da177e4
LT
530 alloc_count = block_count;
531 eloc.logicalBlockNum += alloc_count;
532 elen -= (alloc_count << sb->s_blocksize_bits);
97e961fd 533 udf_write_aext(table, &epos, &eloc,
4b11111a
MS
534 (etype << 30) | elen, 1);
535 } else
6c1e4d06 536 udf_delete_aext(table, epos);
28de7948 537 } else {
1da177e4 538 alloc_count = 0;
28de7948 539 }
1da177e4 540
3bf25cb4 541 brelse(epos.bh);
1da177e4 542
146bca72
JK
543 if (alloc_count)
544 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 545 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
546 return alloc_count;
547}
548
b490bdd6 549static udf_pblk_t udf_table_new_block(struct super_block *sb,
cb00ea35
CG
550 struct inode *table, uint16_t partition,
551 uint32_t goal, int *err)
1da177e4
LT
552{
553 struct udf_sb_info *sbi = UDF_SB(sb);
554 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
b490bdd6
SM
555 udf_pblk_t newblock = 0;
556 uint32_t adsize;
ff116fc8 557 uint32_t elen, goal_elen = 0;
5ca4e4be 558 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
ff116fc8 559 struct extent_position epos, goal_epos;
1da177e4 560 int8_t etype;
48d6d8ff 561 struct udf_inode_info *iinfo = UDF_I(table);
1da177e4
LT
562
563 *err = -ENOSPC;
564
48d6d8ff 565 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 566 adsize = sizeof(struct short_ad);
48d6d8ff 567 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 568 adsize = sizeof(struct long_ad);
1da177e4
LT
569 else
570 return newblock;
571
1e7933de 572 mutex_lock(&sbi->s_alloc_mutex);
3391faa4 573 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
574 goal = 0;
575
4b11111a
MS
576 /* We search for the closest matching block to goal. If we find
577 a exact hit, we stop. Otherwise we keep going till we run out
578 of extents. We store the buffer_head, bloc, and extoffset
579 of the current closest match and use that when we are done.
cb00ea35 580 */
ff116fc8 581 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 582 epos.block = iinfo->i_location;
ff116fc8 583 epos.bh = goal_epos.bh = NULL;
1da177e4 584
28de7948
CG
585 while (spread &&
586 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
cb00ea35 587 if (goal >= eloc.logicalBlockNum) {
4b11111a
MS
588 if (goal < eloc.logicalBlockNum +
589 (elen >> sb->s_blocksize_bits))
1da177e4
LT
590 nspread = 0;
591 else
592 nspread = goal - eloc.logicalBlockNum -
28de7948
CG
593 (elen >> sb->s_blocksize_bits);
594 } else {
1da177e4 595 nspread = eloc.logicalBlockNum - goal;
28de7948 596 }
1da177e4 597
cb00ea35 598 if (nspread < spread) {
1da177e4 599 spread = nspread;
cb00ea35 600 if (goal_epos.bh != epos.bh) {
3bf25cb4 601 brelse(goal_epos.bh);
ff116fc8 602 goal_epos.bh = epos.bh;
3bf25cb4 603 get_bh(goal_epos.bh);
1da177e4 604 }
ff116fc8
JK
605 goal_epos.block = epos.block;
606 goal_epos.offset = epos.offset - adsize;
1da177e4
LT
607 goal_eloc = eloc;
608 goal_elen = (etype << 30) | elen;
609 }
610 }
611
3bf25cb4 612 brelse(epos.bh);
1da177e4 613
cb00ea35 614 if (spread == 0xFFFFFFFF) {
3bf25cb4 615 brelse(goal_epos.bh);
1e7933de 616 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
617 return 0;
618 }
619
620 /* Only allocate blocks from the beginning of the extent.
621 That way, we only delete (empty) extents, never have to insert an
622 extent because of splitting */
623 /* This works, but very poorly.... */
624
625 newblock = goal_eloc.logicalBlockNum;
cb00ea35 626 goal_eloc.logicalBlockNum++;
1da177e4 627 goal_elen -= sb->s_blocksize;
1da177e4
LT
628
629 if (goal_elen)
97e961fd 630 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
1da177e4 631 else
6c1e4d06 632 udf_delete_aext(table, goal_epos);
3bf25cb4 633 brelse(goal_epos.bh);
1da177e4 634
146bca72 635 udf_add_free_space(sb, partition, -1);
1da177e4 636
1e7933de 637 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
638 *err = 0;
639 return newblock;
640}
641
97e961fd
PE
642void udf_free_blocks(struct super_block *sb, struct inode *inode,
643 struct kernel_lb_addr *bloc, uint32_t offset,
644 uint32_t count)
1da177e4 645{
97e961fd 646 uint16_t partition = bloc->partitionReferenceNum;
6c79e987 647 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1da177e4 648
6c79e987 649 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
fd4287db 650 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
e650b94a 651 bloc, offset, count);
6c79e987 652 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
fd4287db 653 udf_table_free_blocks(sb, map->s_uspace.s_table,
e650b94a 654 bloc, offset, count);
6c79e987 655 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
fd4287db 656 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
e650b94a 657 bloc, offset, count);
6c79e987 658 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
fd4287db 659 udf_table_free_blocks(sb, map->s_fspace.s_table,
e650b94a 660 bloc, offset, count);
28de7948 661 }
fd4287db
JK
662
663 if (inode) {
664 inode_sub_bytes(inode,
665 ((sector_t)count) << sb->s_blocksize_bits);
666 }
1da177e4
LT
667}
668
cb00ea35
CG
669inline int udf_prealloc_blocks(struct super_block *sb,
670 struct inode *inode,
671 uint16_t partition, uint32_t first_block,
672 uint32_t block_count)
1da177e4 673{
6c79e987 674 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1be440de 675 int allocated;
6c79e987 676
4b11111a 677 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
678 allocated = udf_bitmap_prealloc_blocks(sb,
679 map->s_uspace.s_bitmap,
680 partition, first_block,
681 block_count);
4b11111a 682 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
683 allocated = udf_table_prealloc_blocks(sb,
684 map->s_uspace.s_table,
685 partition, first_block,
686 block_count);
4b11111a 687 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
fd4287db
JK
688 allocated = udf_bitmap_prealloc_blocks(sb,
689 map->s_fspace.s_bitmap,
690 partition, first_block,
691 block_count);
4b11111a 692 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
fd4287db
JK
693 allocated = udf_table_prealloc_blocks(sb,
694 map->s_fspace.s_table,
695 partition, first_block,
696 block_count);
4b11111a 697 else
1da177e4 698 return 0;
fd4287db
JK
699
700 if (inode && allocated > 0)
701 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
702 return allocated;
1da177e4
LT
703}
704
b490bdd6 705inline udf_pblk_t udf_new_block(struct super_block *sb,
cb00ea35
CG
706 struct inode *inode,
707 uint16_t partition, uint32_t goal, int *err)
1da177e4 708{
6c79e987 709 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
b490bdd6 710 udf_pblk_t block;
3bf25cb4 711
4b11111a 712 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
713 block = udf_bitmap_new_block(sb,
714 map->s_uspace.s_bitmap,
715 partition, goal, err);
4b11111a 716 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
717 block = udf_table_new_block(sb,
718 map->s_uspace.s_table,
28de7948 719 partition, goal, err);
fd4287db
JK
720 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
721 block = udf_bitmap_new_block(sb,
722 map->s_fspace.s_bitmap,
723 partition, goal, err);
4b11111a 724 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
fd4287db
JK
725 block = udf_table_new_block(sb,
726 map->s_fspace.s_table,
727 partition, goal, err);
4b11111a 728 else {
1da177e4
LT
729 *err = -EIO;
730 return 0;
731 }
fd4287db
JK
732 if (inode && block)
733 inode_add_bytes(inode, sb->s_blocksize);
734 return block;
1da177e4 735}