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