]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/udf/balloc.c
udf: remove unused variable in udf_table_free_blocks()
[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/buffer_head.h>
25#include <linux/bitops.h>
26
27#include "udf_i.h"
28#include "udf_sb.h"
29
9ad1e1e4
AM
30#define udf_clear_bit __test_and_clear_bit_le
31#define udf_set_bit __test_and_set_bit_le
32#define udf_test_bit test_bit_le
33#define udf_find_next_one_bit find_next_bit_le
1da177e4 34
cb00ea35
CG
35static int read_block_bitmap(struct super_block *sb,
36 struct udf_bitmap *bitmap, unsigned int block,
37 unsigned long bitmap_nr)
1da177e4
LT
38{
39 struct buffer_head *bh = NULL;
40 int retval = 0;
5ca4e4be 41 struct kernel_lb_addr loc;
1da177e4
LT
42
43 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 44 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1da177e4 45
97e961fd 46 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
4b11111a 47 if (!bh)
1da177e4 48 retval = -EIO;
4b11111a 49
1da177e4
LT
50 bitmap->s_block_bitmap[bitmap_nr] = bh;
51 return retval;
52}
53
cb00ea35
CG
54static int __load_block_bitmap(struct super_block *sb,
55 struct udf_bitmap *bitmap,
56 unsigned int block_group)
1da177e4
LT
57{
58 int retval = 0;
59 int nr_groups = bitmap->s_nr_groups;
60
cb00ea35 61 if (block_group >= nr_groups) {
a983f368
JP
62 udf_debug("block_group (%d) > nr_groups (%d)\n",
63 block_group, nr_groups);
1da177e4
LT
64 }
65
28de7948 66 if (bitmap->s_block_bitmap[block_group]) {
1da177e4 67 return block_group;
28de7948
CG
68 } else {
69 retval = read_block_bitmap(sb, bitmap, block_group,
70 block_group);
1da177e4
LT
71 if (retval < 0)
72 return retval;
73 return block_group;
74 }
75}
76
cb00ea35
CG
77static inline int load_block_bitmap(struct super_block *sb,
78 struct udf_bitmap *bitmap,
79 unsigned int block_group)
1da177e4
LT
80{
81 int slot;
82
83 slot = __load_block_bitmap(sb, bitmap, block_group);
84
85 if (slot < 0)
86 return slot;
87
88 if (!bitmap->s_block_bitmap[slot])
89 return -EIO;
90
91 return slot;
92}
93
146bca72 94static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
742ba02a 95{
146bca72 96 struct udf_sb_info *sbi = UDF_SB(sb);
742ba02a
MS
97 struct logicalVolIntegrityDesc *lvid;
98
146bca72
JK
99 if (!sbi->s_lvid_bh)
100 return;
742ba02a
MS
101
102 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
c2104fda 103 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
146bca72 104 udf_updated_lvid(sb);
742ba02a
MS
105}
106
cb00ea35 107static void udf_bitmap_free_blocks(struct super_block *sb,
cb00ea35 108 struct udf_bitmap *bitmap,
97e961fd
PE
109 struct kernel_lb_addr *bloc,
110 uint32_t offset,
cb00ea35 111 uint32_t count)
1da177e4
LT
112{
113 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 114 struct buffer_head *bh = NULL;
97e961fd 115 struct udf_part_map *partmap;
1da177e4
LT
116 unsigned long block;
117 unsigned long block_group;
118 unsigned long bit;
119 unsigned long i;
120 int bitmap_nr;
121 unsigned long overflow;
122
1e7933de 123 mutex_lock(&sbi->s_alloc_mutex);
97e961fd 124 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
69ecbbed
DC
125 if (bloc->logicalBlockNum + count < count ||
126 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
28de7948 127 udf_debug("%d < %d || %d + %d > %d\n",
a983f368
JP
128 bloc->logicalBlockNum, 0,
129 bloc->logicalBlockNum, count,
130 partmap->s_partition_len);
1da177e4
LT
131 goto error_return;
132 }
133
97e961fd 134 block = bloc->logicalBlockNum + offset +
4b11111a 135 (sizeof(struct spaceBitmapDesc) << 3);
1da177e4 136
4daa1b87
MS
137 do {
138 overflow = 0;
139 block_group = block >> (sb->s_blocksize_bits + 3);
140 bit = block % (sb->s_blocksize << 3);
141
142 /*
143 * Check to see if we are freeing blocks across a group boundary.
144 */
145 if (bit + count > (sb->s_blocksize << 3)) {
146 overflow = bit + count - (sb->s_blocksize << 3);
147 count -= overflow;
1da177e4 148 }
4daa1b87
MS
149 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
150 if (bitmap_nr < 0)
151 goto error_return;
152
153 bh = bitmap->s_block_bitmap[bitmap_nr];
154 for (i = 0; i < count; i++) {
155 if (udf_set_bit(bit + i, bh->b_data)) {
156 udf_debug("bit %ld already set\n", bit + i);
157 udf_debug("byte=%2x\n",
a983f368 158 ((char *)bh->b_data)[(bit + i) >> 3]);
4daa1b87
MS
159 }
160 }
7abc2e45 161 udf_add_free_space(sb, sbi->s_partition, count);
4daa1b87
MS
162 mark_buffer_dirty(bh);
163 if (overflow) {
164 block += count;
165 count = overflow;
166 }
167 } while (overflow);
168
28de7948 169error_return:
1e7933de 170 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
171}
172
cb00ea35 173static int udf_bitmap_prealloc_blocks(struct super_block *sb,
cb00ea35
CG
174 struct udf_bitmap *bitmap,
175 uint16_t partition, uint32_t first_block,
176 uint32_t block_count)
1da177e4
LT
177{
178 struct udf_sb_info *sbi = UDF_SB(sb);
179 int alloc_count = 0;
180 int bit, block, block_group, group_start;
181 int nr_groups, bitmap_nr;
182 struct buffer_head *bh;
6c79e987 183 __u32 part_len;
1da177e4 184
1e7933de 185 mutex_lock(&sbi->s_alloc_mutex);
6c79e987 186 part_len = sbi->s_partmaps[partition].s_partition_len;
3391faa4 187 if (first_block >= part_len)
1da177e4
LT
188 goto out;
189
6c79e987
MS
190 if (first_block + block_count > part_len)
191 block_count = part_len - first_block;
1da177e4 192
4daa1b87
MS
193 do {
194 nr_groups = udf_compute_nr_groups(sb, partition);
195 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
196 block_group = block >> (sb->s_blocksize_bits + 3);
197 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
1da177e4 198
4daa1b87
MS
199 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
200 if (bitmap_nr < 0)
201 goto out;
202 bh = bitmap->s_block_bitmap[bitmap_nr];
1da177e4 203
4daa1b87 204 bit = block % (sb->s_blocksize << 3);
1da177e4 205
4daa1b87 206 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
36350462 207 if (!udf_clear_bit(bit, bh->b_data))
4daa1b87 208 goto out;
4daa1b87
MS
209 block_count--;
210 alloc_count++;
211 bit++;
212 block++;
1da177e4 213 }
4daa1b87
MS
214 mark_buffer_dirty(bh);
215 } while (block_count > 0);
216
28de7948 217out:
146bca72 218 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 219 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
220 return alloc_count;
221}
222
cb00ea35 223static int udf_bitmap_new_block(struct super_block *sb,
cb00ea35
CG
224 struct udf_bitmap *bitmap, uint16_t partition,
225 uint32_t goal, int *err)
1da177e4
LT
226{
227 struct udf_sb_info *sbi = UDF_SB(sb);
cb00ea35 228 int newbit, bit = 0, block, block_group, group_start;
1da177e4
LT
229 int end_goal, nr_groups, bitmap_nr, i;
230 struct buffer_head *bh = NULL;
231 char *ptr;
232 int newblock = 0;
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) {
28de7948 367 udf_debug("%d < %d || %d + %d > %d\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;
5ca4e4be
PE
452 struct short_ad *sad = NULL;
453 struct long_ad *lad = NULL;
1da177e4
LT
454 struct allocExtDesc *aed;
455
456 eloc.logicalBlockNum = start;
28de7948
CG
457 elen = EXT_RECORDED_ALLOCATED |
458 (count << sb->s_blocksize_bits);
1da177e4 459
48d6d8ff 460 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 461 adsize = sizeof(struct short_ad);
48d6d8ff 462 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 463 adsize = sizeof(struct long_ad);
48d6d8ff 464 else {
3bf25cb4
JK
465 brelse(oepos.bh);
466 brelse(epos.bh);
1da177e4
LT
467 goto error_return;
468 }
469
cb00ea35 470 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
391e8bbd 471 unsigned char *sptr, *dptr;
1da177e4 472 int loffset;
cb00ea35 473
3bf25cb4 474 brelse(oepos.bh);
ff116fc8 475 oepos = epos;
1da177e4
LT
476
477 /* Steal a block from the extent being free'd */
ff116fc8 478 epos.block.logicalBlockNum = eloc.logicalBlockNum;
cb00ea35 479 eloc.logicalBlockNum++;
1da177e4
LT
480 elen -= sb->s_blocksize;
481
4b11111a 482 epos.bh = udf_tread(sb,
97e961fd 483 udf_get_lb_pblock(sb, &epos.block, 0));
4b11111a 484 if (!epos.bh) {
3bf25cb4 485 brelse(oepos.bh);
1da177e4
LT
486 goto error_return;
487 }
ff116fc8 488 aed = (struct allocExtDesc *)(epos.bh->b_data);
4b11111a
MS
489 aed->previousAllocExtLocation =
490 cpu_to_le32(oepos.block.logicalBlockNum);
cb00ea35 491 if (epos.offset + adsize > sb->s_blocksize) {
ff116fc8 492 loffset = epos.offset;
1da177e4 493 aed->lengthAllocDescs = cpu_to_le32(adsize);
48d6d8ff 494 sptr = iinfo->i_ext.i_data + epos.offset
c0b34438 495 - adsize;
4b11111a
MS
496 dptr = epos.bh->b_data +
497 sizeof(struct allocExtDesc);
1da177e4 498 memcpy(dptr, sptr, adsize);
4b11111a
MS
499 epos.offset = sizeof(struct allocExtDesc) +
500 adsize;
cb00ea35 501 } else {
ff116fc8 502 loffset = epos.offset + adsize;
1da177e4 503 aed->lengthAllocDescs = cpu_to_le32(0);
cb00ea35 504 if (oepos.bh) {
f5cc15da 505 sptr = oepos.bh->b_data + epos.offset;
4b11111a
MS
506 aed = (struct allocExtDesc *)
507 oepos.bh->b_data;
c2104fda 508 le32_add_cpu(&aed->lengthAllocDescs,
509 adsize);
cb00ea35 510 } else {
48d6d8ff 511 sptr = iinfo->i_ext.i_data +
c0b34438 512 epos.offset;
48d6d8ff 513 iinfo->i_lenAlloc += adsize;
1da177e4
LT
514 mark_inode_dirty(table);
515 }
f5cc15da 516 epos.offset = sizeof(struct allocExtDesc);
1da177e4 517 }
6c79e987 518 if (sbi->s_udfrev >= 0x0200)
4b11111a
MS
519 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
520 3, 1, epos.block.logicalBlockNum,
5ca4e4be 521 sizeof(struct tag));
1da177e4 522 else
4b11111a
MS
523 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
524 2, 1, epos.block.logicalBlockNum,
5ca4e4be 525 sizeof(struct tag));
28de7948 526
48d6d8ff 527 switch (iinfo->i_alloc_type) {
4b11111a 528 case ICBTAG_FLAG_AD_SHORT:
5ca4e4be 529 sad = (struct short_ad *)sptr;
4b11111a
MS
530 sad->extLength = cpu_to_le32(
531 EXT_NEXT_EXTENT_ALLOCDECS |
532 sb->s_blocksize);
533 sad->extPosition =
534 cpu_to_le32(epos.block.logicalBlockNum);
535 break;
536 case ICBTAG_FLAG_AD_LONG:
5ca4e4be 537 lad = (struct long_ad *)sptr;
4b11111a
MS
538 lad->extLength = cpu_to_le32(
539 EXT_NEXT_EXTENT_ALLOCDECS |
540 sb->s_blocksize);
541 lad->extLocation =
542 cpu_to_lelb(epos.block);
543 break;
1da177e4 544 }
cb00ea35 545 if (oepos.bh) {
ff116fc8
JK
546 udf_update_tag(oepos.bh->b_data, loffset);
547 mark_buffer_dirty(oepos.bh);
28de7948 548 } else {
1da177e4 549 mark_inode_dirty(table);
28de7948 550 }
1da177e4
LT
551 }
552
4b11111a
MS
553 /* It's possible that stealing the block emptied the extent */
554 if (elen) {
97e961fd 555 udf_write_aext(table, &epos, &eloc, elen, 1);
1da177e4 556
cb00ea35 557 if (!epos.bh) {
48d6d8ff 558 iinfo->i_lenAlloc += adsize;
1da177e4 559 mark_inode_dirty(table);
cb00ea35 560 } else {
ff116fc8 561 aed = (struct allocExtDesc *)epos.bh->b_data;
c2104fda 562 le32_add_cpu(&aed->lengthAllocDescs, adsize);
ff116fc8
JK
563 udf_update_tag(epos.bh->b_data, epos.offset);
564 mark_buffer_dirty(epos.bh);
1da177e4
LT
565 }
566 }
567 }
568
3bf25cb4
JK
569 brelse(epos.bh);
570 brelse(oepos.bh);
1da177e4 571
28de7948 572error_return:
1e7933de 573 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
574 return;
575}
576
cb00ea35 577static int udf_table_prealloc_blocks(struct super_block *sb,
cb00ea35
CG
578 struct inode *table, uint16_t partition,
579 uint32_t first_block, uint32_t block_count)
1da177e4
LT
580{
581 struct udf_sb_info *sbi = UDF_SB(sb);
582 int alloc_count = 0;
ff116fc8 583 uint32_t elen, adsize;
5ca4e4be 584 struct kernel_lb_addr eloc;
ff116fc8 585 struct extent_position epos;
1da177e4 586 int8_t etype = -1;
48d6d8ff 587 struct udf_inode_info *iinfo;
1da177e4 588
3391faa4 589 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
590 return 0;
591
48d6d8ff
MS
592 iinfo = UDF_I(table);
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 0;
599
1e7933de 600 mutex_lock(&sbi->s_alloc_mutex);
ff116fc8 601 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 602 epos.block = iinfo->i_location;
ff116fc8 603 epos.bh = NULL;
1da177e4
LT
604 eloc.logicalBlockNum = 0xFFFFFFFF;
605
28de7948
CG
606 while (first_block != eloc.logicalBlockNum &&
607 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
1da177e4 608 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
cb00ea35 609 eloc.logicalBlockNum, elen, first_block);
28de7948 610 ; /* empty loop body */
1da177e4
LT
611 }
612
cb00ea35 613 if (first_block == eloc.logicalBlockNum) {
ff116fc8 614 epos.offset -= adsize;
1da177e4
LT
615
616 alloc_count = (elen >> sb->s_blocksize_bits);
36350462 617 if (alloc_count > block_count) {
1da177e4
LT
618 alloc_count = block_count;
619 eloc.logicalBlockNum += alloc_count;
620 elen -= (alloc_count << sb->s_blocksize_bits);
97e961fd 621 udf_write_aext(table, &epos, &eloc,
4b11111a
MS
622 (etype << 30) | elen, 1);
623 } else
624 udf_delete_aext(table, epos, eloc,
625 (etype << 30) | elen);
28de7948 626 } else {
1da177e4 627 alloc_count = 0;
28de7948 628 }
1da177e4 629
3bf25cb4 630 brelse(epos.bh);
1da177e4 631
146bca72
JK
632 if (alloc_count)
633 udf_add_free_space(sb, partition, -alloc_count);
1e7933de 634 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
635 return alloc_count;
636}
637
cb00ea35 638static int udf_table_new_block(struct super_block *sb,
cb00ea35
CG
639 struct inode *table, uint16_t partition,
640 uint32_t goal, int *err)
1da177e4
LT
641{
642 struct udf_sb_info *sbi = UDF_SB(sb);
643 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
644 uint32_t newblock = 0, adsize;
ff116fc8 645 uint32_t elen, goal_elen = 0;
5ca4e4be 646 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
ff116fc8 647 struct extent_position epos, goal_epos;
1da177e4 648 int8_t etype;
48d6d8ff 649 struct udf_inode_info *iinfo = UDF_I(table);
1da177e4
LT
650
651 *err = -ENOSPC;
652
48d6d8ff 653 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 654 adsize = sizeof(struct short_ad);
48d6d8ff 655 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 656 adsize = sizeof(struct long_ad);
1da177e4
LT
657 else
658 return newblock;
659
1e7933de 660 mutex_lock(&sbi->s_alloc_mutex);
3391faa4 661 if (goal >= sbi->s_partmaps[partition].s_partition_len)
1da177e4
LT
662 goal = 0;
663
4b11111a
MS
664 /* We search for the closest matching block to goal. If we find
665 a exact hit, we stop. Otherwise we keep going till we run out
666 of extents. We store the buffer_head, bloc, and extoffset
667 of the current closest match and use that when we are done.
cb00ea35 668 */
ff116fc8 669 epos.offset = sizeof(struct unallocSpaceEntry);
48d6d8ff 670 epos.block = iinfo->i_location;
ff116fc8 671 epos.bh = goal_epos.bh = NULL;
1da177e4 672
28de7948
CG
673 while (spread &&
674 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
cb00ea35 675 if (goal >= eloc.logicalBlockNum) {
4b11111a
MS
676 if (goal < eloc.logicalBlockNum +
677 (elen >> sb->s_blocksize_bits))
1da177e4
LT
678 nspread = 0;
679 else
680 nspread = goal - eloc.logicalBlockNum -
28de7948
CG
681 (elen >> sb->s_blocksize_bits);
682 } else {
1da177e4 683 nspread = eloc.logicalBlockNum - goal;
28de7948 684 }
1da177e4 685
cb00ea35 686 if (nspread < spread) {
1da177e4 687 spread = nspread;
cb00ea35 688 if (goal_epos.bh != epos.bh) {
3bf25cb4 689 brelse(goal_epos.bh);
ff116fc8 690 goal_epos.bh = epos.bh;
3bf25cb4 691 get_bh(goal_epos.bh);
1da177e4 692 }
ff116fc8
JK
693 goal_epos.block = epos.block;
694 goal_epos.offset = epos.offset - adsize;
1da177e4
LT
695 goal_eloc = eloc;
696 goal_elen = (etype << 30) | elen;
697 }
698 }
699
3bf25cb4 700 brelse(epos.bh);
1da177e4 701
cb00ea35 702 if (spread == 0xFFFFFFFF) {
3bf25cb4 703 brelse(goal_epos.bh);
1e7933de 704 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
705 return 0;
706 }
707
708 /* Only allocate blocks from the beginning of the extent.
709 That way, we only delete (empty) extents, never have to insert an
710 extent because of splitting */
711 /* This works, but very poorly.... */
712
713 newblock = goal_eloc.logicalBlockNum;
cb00ea35 714 goal_eloc.logicalBlockNum++;
1da177e4 715 goal_elen -= sb->s_blocksize;
1da177e4
LT
716
717 if (goal_elen)
97e961fd 718 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
1da177e4 719 else
ff116fc8 720 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
3bf25cb4 721 brelse(goal_epos.bh);
1da177e4 722
146bca72 723 udf_add_free_space(sb, partition, -1);
1da177e4 724
1e7933de 725 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
726 *err = 0;
727 return newblock;
728}
729
97e961fd
PE
730void udf_free_blocks(struct super_block *sb, struct inode *inode,
731 struct kernel_lb_addr *bloc, uint32_t offset,
732 uint32_t count)
1da177e4 733{
97e961fd 734 uint16_t partition = bloc->partitionReferenceNum;
6c79e987 735 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1da177e4 736
6c79e987 737 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
fd4287db 738 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
e650b94a 739 bloc, offset, count);
6c79e987 740 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
fd4287db 741 udf_table_free_blocks(sb, map->s_uspace.s_table,
e650b94a 742 bloc, offset, count);
6c79e987 743 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
fd4287db 744 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
e650b94a 745 bloc, offset, count);
6c79e987 746 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
fd4287db 747 udf_table_free_blocks(sb, map->s_fspace.s_table,
e650b94a 748 bloc, offset, count);
28de7948 749 }
fd4287db
JK
750
751 if (inode) {
752 inode_sub_bytes(inode,
753 ((sector_t)count) << sb->s_blocksize_bits);
754 }
1da177e4
LT
755}
756
cb00ea35
CG
757inline int udf_prealloc_blocks(struct super_block *sb,
758 struct inode *inode,
759 uint16_t partition, uint32_t first_block,
760 uint32_t block_count)
1da177e4 761{
6c79e987 762 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
fd4287db 763 sector_t allocated;
6c79e987 764
4b11111a 765 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
766 allocated = udf_bitmap_prealloc_blocks(sb,
767 map->s_uspace.s_bitmap,
768 partition, first_block,
769 block_count);
4b11111a 770 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
771 allocated = udf_table_prealloc_blocks(sb,
772 map->s_uspace.s_table,
773 partition, first_block,
774 block_count);
4b11111a 775 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
fd4287db
JK
776 allocated = udf_bitmap_prealloc_blocks(sb,
777 map->s_fspace.s_bitmap,
778 partition, first_block,
779 block_count);
4b11111a 780 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
fd4287db
JK
781 allocated = udf_table_prealloc_blocks(sb,
782 map->s_fspace.s_table,
783 partition, first_block,
784 block_count);
4b11111a 785 else
1da177e4 786 return 0;
fd4287db
JK
787
788 if (inode && allocated > 0)
789 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
790 return allocated;
1da177e4
LT
791}
792
cb00ea35
CG
793inline int udf_new_block(struct super_block *sb,
794 struct inode *inode,
795 uint16_t partition, uint32_t goal, int *err)
1da177e4 796{
6c79e987 797 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
fd4287db 798 int block;
3bf25cb4 799
4b11111a 800 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
fd4287db
JK
801 block = udf_bitmap_new_block(sb,
802 map->s_uspace.s_bitmap,
803 partition, goal, err);
4b11111a 804 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
fd4287db
JK
805 block = udf_table_new_block(sb,
806 map->s_uspace.s_table,
28de7948 807 partition, goal, err);
fd4287db
JK
808 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
809 block = udf_bitmap_new_block(sb,
810 map->s_fspace.s_bitmap,
811 partition, goal, err);
4b11111a 812 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
fd4287db
JK
813 block = udf_table_new_block(sb,
814 map->s_fspace.s_table,
815 partition, goal, err);
4b11111a 816 else {
1da177e4
LT
817 *err = -EIO;
818 return 0;
819 }
fd4287db
JK
820 if (inode && block)
821 inode_add_bytes(inode, sb->s_blocksize);
822 return block;
1da177e4 823}