]>
Commit | Line | Data |
---|---|---|
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 |
35 | static 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 |
54 | static 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 |
77 | static 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 | 94 | static 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 | 107 | static 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 | 169 | error_return: |
1e7933de | 170 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
171 | } |
172 | ||
cb00ea35 | 173 | static 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 | 217 | out: |
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 | 223 | static 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 | 237 | repeat: |
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 | 318 | search_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 | 326 | got_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 | 342 | error_return: |
1da177e4 | 343 | *err = -EIO; |
1e7933de | 344 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
345 | return 0; |
346 | } | |
347 | ||
cb00ea35 | 348 | static 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 LT |
360 | int8_t etype; |
361 | int i; | |
48d6d8ff | 362 | struct udf_inode_info *iinfo; |
1da177e4 | 363 | |
1e7933de | 364 | mutex_lock(&sbi->s_alloc_mutex); |
97e961fd | 365 | partmap = &sbi->s_partmaps[bloc->partitionReferenceNum]; |
69ecbbed DC |
366 | if (bloc->logicalBlockNum + count < count || |
367 | (bloc->logicalBlockNum + count) > partmap->s_partition_len) { | |
28de7948 | 368 | udf_debug("%d < %d || %d + %d > %d\n", |
a983f368 JP |
369 | bloc->logicalBlockNum, 0, |
370 | bloc->logicalBlockNum, count, | |
97e961fd | 371 | partmap->s_partition_len); |
1da177e4 LT |
372 | goto error_return; |
373 | } | |
374 | ||
48d6d8ff | 375 | iinfo = UDF_I(table); |
146bca72 | 376 | udf_add_free_space(sb, sbi->s_partition, count); |
1da177e4 | 377 | |
97e961fd PE |
378 | start = bloc->logicalBlockNum + offset; |
379 | end = bloc->logicalBlockNum + offset + count - 1; | |
1da177e4 | 380 | |
ff116fc8 | 381 | epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); |
1da177e4 | 382 | elen = 0; |
48d6d8ff | 383 | epos.block = oepos.block = iinfo->i_location; |
ff116fc8 | 384 | epos.bh = oepos.bh = NULL; |
1da177e4 | 385 | |
28de7948 CG |
386 | while (count && |
387 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
4b11111a MS |
388 | if (((eloc.logicalBlockNum + |
389 | (elen >> sb->s_blocksize_bits)) == start)) { | |
390 | if ((0x3FFFFFFF - elen) < | |
391 | (count << sb->s_blocksize_bits)) { | |
392 | uint32_t tmp = ((0x3FFFFFFF - elen) >> | |
393 | sb->s_blocksize_bits); | |
394 | count -= tmp; | |
395 | start += tmp; | |
396 | elen = (etype << 30) | | |
397 | (0x40000000 - sb->s_blocksize); | |
cb00ea35 | 398 | } else { |
4b11111a MS |
399 | elen = (etype << 30) | |
400 | (elen + | |
401 | (count << sb->s_blocksize_bits)); | |
1da177e4 LT |
402 | start += count; |
403 | count = 0; | |
404 | } | |
97e961fd | 405 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
cb00ea35 | 406 | } else if (eloc.logicalBlockNum == (end + 1)) { |
4b11111a MS |
407 | if ((0x3FFFFFFF - elen) < |
408 | (count << sb->s_blocksize_bits)) { | |
409 | uint32_t tmp = ((0x3FFFFFFF - elen) >> | |
410 | sb->s_blocksize_bits); | |
411 | count -= tmp; | |
412 | end -= tmp; | |
413 | eloc.logicalBlockNum -= tmp; | |
414 | elen = (etype << 30) | | |
415 | (0x40000000 - sb->s_blocksize); | |
cb00ea35 | 416 | } else { |
1da177e4 | 417 | eloc.logicalBlockNum = start; |
4b11111a MS |
418 | elen = (etype << 30) | |
419 | (elen + | |
420 | (count << sb->s_blocksize_bits)); | |
1da177e4 LT |
421 | end -= count; |
422 | count = 0; | |
423 | } | |
97e961fd | 424 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
1da177e4 LT |
425 | } |
426 | ||
cb00ea35 | 427 | if (epos.bh != oepos.bh) { |
1da177e4 | 428 | i = -1; |
ff116fc8 | 429 | oepos.block = epos.block; |
3bf25cb4 JK |
430 | brelse(oepos.bh); |
431 | get_bh(epos.bh); | |
ff116fc8 JK |
432 | oepos.bh = epos.bh; |
433 | oepos.offset = 0; | |
28de7948 | 434 | } else { |
ff116fc8 | 435 | oepos.offset = epos.offset; |
28de7948 | 436 | } |
1da177e4 LT |
437 | } |
438 | ||
cb00ea35 | 439 | if (count) { |
28de7948 | 440 | /* |
4b11111a MS |
441 | * NOTE: we CANNOT use udf_add_aext here, as it can try to |
442 | * allocate a new block, and since we hold the super block | |
443 | * lock already very bad things would happen :) | |
28de7948 CG |
444 | * |
445 | * We copy the behavior of udf_add_aext, but instead of | |
446 | * trying to allocate a new block close to the existing one, | |
447 | * we just steal a block from the extent we are trying to add. | |
448 | * | |
449 | * It would be nice if the blocks were close together, but it | |
450 | * isn't required. | |
cb00ea35 | 451 | */ |
1da177e4 LT |
452 | |
453 | int adsize; | |
5ca4e4be PE |
454 | struct short_ad *sad = NULL; |
455 | struct long_ad *lad = NULL; | |
1da177e4 LT |
456 | struct allocExtDesc *aed; |
457 | ||
458 | eloc.logicalBlockNum = start; | |
28de7948 CG |
459 | elen = EXT_RECORDED_ALLOCATED | |
460 | (count << sb->s_blocksize_bits); | |
1da177e4 | 461 | |
48d6d8ff | 462 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 463 | adsize = sizeof(struct short_ad); |
48d6d8ff | 464 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 465 | adsize = sizeof(struct long_ad); |
48d6d8ff | 466 | else { |
3bf25cb4 JK |
467 | brelse(oepos.bh); |
468 | brelse(epos.bh); | |
1da177e4 LT |
469 | goto error_return; |
470 | } | |
471 | ||
cb00ea35 | 472 | if (epos.offset + (2 * adsize) > sb->s_blocksize) { |
391e8bbd | 473 | unsigned char *sptr, *dptr; |
1da177e4 | 474 | int loffset; |
cb00ea35 | 475 | |
3bf25cb4 | 476 | brelse(oepos.bh); |
ff116fc8 | 477 | oepos = epos; |
1da177e4 LT |
478 | |
479 | /* Steal a block from the extent being free'd */ | |
ff116fc8 | 480 | epos.block.logicalBlockNum = eloc.logicalBlockNum; |
cb00ea35 | 481 | eloc.logicalBlockNum++; |
1da177e4 LT |
482 | elen -= sb->s_blocksize; |
483 | ||
4b11111a | 484 | epos.bh = udf_tread(sb, |
97e961fd | 485 | udf_get_lb_pblock(sb, &epos.block, 0)); |
4b11111a | 486 | if (!epos.bh) { |
3bf25cb4 | 487 | brelse(oepos.bh); |
1da177e4 LT |
488 | goto error_return; |
489 | } | |
ff116fc8 | 490 | aed = (struct allocExtDesc *)(epos.bh->b_data); |
4b11111a MS |
491 | aed->previousAllocExtLocation = |
492 | cpu_to_le32(oepos.block.logicalBlockNum); | |
cb00ea35 | 493 | if (epos.offset + adsize > sb->s_blocksize) { |
ff116fc8 | 494 | loffset = epos.offset; |
1da177e4 | 495 | aed->lengthAllocDescs = cpu_to_le32(adsize); |
48d6d8ff | 496 | sptr = iinfo->i_ext.i_data + epos.offset |
c0b34438 | 497 | - adsize; |
4b11111a MS |
498 | dptr = epos.bh->b_data + |
499 | sizeof(struct allocExtDesc); | |
1da177e4 | 500 | memcpy(dptr, sptr, adsize); |
4b11111a MS |
501 | epos.offset = sizeof(struct allocExtDesc) + |
502 | adsize; | |
cb00ea35 | 503 | } else { |
ff116fc8 | 504 | loffset = epos.offset + adsize; |
1da177e4 | 505 | aed->lengthAllocDescs = cpu_to_le32(0); |
cb00ea35 | 506 | if (oepos.bh) { |
f5cc15da | 507 | sptr = oepos.bh->b_data + epos.offset; |
4b11111a MS |
508 | aed = (struct allocExtDesc *) |
509 | oepos.bh->b_data; | |
c2104fda | 510 | le32_add_cpu(&aed->lengthAllocDescs, |
511 | adsize); | |
cb00ea35 | 512 | } else { |
48d6d8ff | 513 | sptr = iinfo->i_ext.i_data + |
c0b34438 | 514 | epos.offset; |
48d6d8ff | 515 | iinfo->i_lenAlloc += adsize; |
1da177e4 LT |
516 | mark_inode_dirty(table); |
517 | } | |
f5cc15da | 518 | epos.offset = sizeof(struct allocExtDesc); |
1da177e4 | 519 | } |
6c79e987 | 520 | if (sbi->s_udfrev >= 0x0200) |
4b11111a MS |
521 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
522 | 3, 1, epos.block.logicalBlockNum, | |
5ca4e4be | 523 | sizeof(struct tag)); |
1da177e4 | 524 | else |
4b11111a MS |
525 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
526 | 2, 1, epos.block.logicalBlockNum, | |
5ca4e4be | 527 | sizeof(struct tag)); |
28de7948 | 528 | |
48d6d8ff | 529 | switch (iinfo->i_alloc_type) { |
4b11111a | 530 | case ICBTAG_FLAG_AD_SHORT: |
5ca4e4be | 531 | sad = (struct short_ad *)sptr; |
4b11111a MS |
532 | sad->extLength = cpu_to_le32( |
533 | EXT_NEXT_EXTENT_ALLOCDECS | | |
534 | sb->s_blocksize); | |
535 | sad->extPosition = | |
536 | cpu_to_le32(epos.block.logicalBlockNum); | |
537 | break; | |
538 | case ICBTAG_FLAG_AD_LONG: | |
5ca4e4be | 539 | lad = (struct long_ad *)sptr; |
4b11111a MS |
540 | lad->extLength = cpu_to_le32( |
541 | EXT_NEXT_EXTENT_ALLOCDECS | | |
542 | sb->s_blocksize); | |
543 | lad->extLocation = | |
544 | cpu_to_lelb(epos.block); | |
545 | break; | |
1da177e4 | 546 | } |
cb00ea35 | 547 | if (oepos.bh) { |
ff116fc8 JK |
548 | udf_update_tag(oepos.bh->b_data, loffset); |
549 | mark_buffer_dirty(oepos.bh); | |
28de7948 | 550 | } else { |
1da177e4 | 551 | mark_inode_dirty(table); |
28de7948 | 552 | } |
1da177e4 LT |
553 | } |
554 | ||
4b11111a MS |
555 | /* It's possible that stealing the block emptied the extent */ |
556 | if (elen) { | |
97e961fd | 557 | udf_write_aext(table, &epos, &eloc, elen, 1); |
1da177e4 | 558 | |
cb00ea35 | 559 | if (!epos.bh) { |
48d6d8ff | 560 | iinfo->i_lenAlloc += adsize; |
1da177e4 | 561 | mark_inode_dirty(table); |
cb00ea35 | 562 | } else { |
ff116fc8 | 563 | aed = (struct allocExtDesc *)epos.bh->b_data; |
c2104fda | 564 | le32_add_cpu(&aed->lengthAllocDescs, adsize); |
ff116fc8 JK |
565 | udf_update_tag(epos.bh->b_data, epos.offset); |
566 | mark_buffer_dirty(epos.bh); | |
1da177e4 LT |
567 | } |
568 | } | |
569 | } | |
570 | ||
3bf25cb4 JK |
571 | brelse(epos.bh); |
572 | brelse(oepos.bh); | |
1da177e4 | 573 | |
28de7948 | 574 | error_return: |
1e7933de | 575 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
576 | return; |
577 | } | |
578 | ||
cb00ea35 | 579 | static int udf_table_prealloc_blocks(struct super_block *sb, |
cb00ea35 CG |
580 | struct inode *table, uint16_t partition, |
581 | uint32_t first_block, uint32_t block_count) | |
1da177e4 LT |
582 | { |
583 | struct udf_sb_info *sbi = UDF_SB(sb); | |
584 | int alloc_count = 0; | |
ff116fc8 | 585 | uint32_t elen, adsize; |
5ca4e4be | 586 | struct kernel_lb_addr eloc; |
ff116fc8 | 587 | struct extent_position epos; |
1da177e4 | 588 | int8_t etype = -1; |
48d6d8ff | 589 | struct udf_inode_info *iinfo; |
1da177e4 | 590 | |
3391faa4 | 591 | if (first_block >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
592 | return 0; |
593 | ||
48d6d8ff MS |
594 | iinfo = UDF_I(table); |
595 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | |
5ca4e4be | 596 | adsize = sizeof(struct short_ad); |
48d6d8ff | 597 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 598 | adsize = sizeof(struct long_ad); |
1da177e4 LT |
599 | else |
600 | return 0; | |
601 | ||
1e7933de | 602 | mutex_lock(&sbi->s_alloc_mutex); |
ff116fc8 | 603 | epos.offset = sizeof(struct unallocSpaceEntry); |
48d6d8ff | 604 | epos.block = iinfo->i_location; |
ff116fc8 | 605 | epos.bh = NULL; |
1da177e4 LT |
606 | eloc.logicalBlockNum = 0xFFFFFFFF; |
607 | ||
28de7948 CG |
608 | while (first_block != eloc.logicalBlockNum && |
609 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
1da177e4 | 610 | udf_debug("eloc=%d, elen=%d, first_block=%d\n", |
cb00ea35 | 611 | eloc.logicalBlockNum, elen, first_block); |
28de7948 | 612 | ; /* empty loop body */ |
1da177e4 LT |
613 | } |
614 | ||
cb00ea35 | 615 | if (first_block == eloc.logicalBlockNum) { |
ff116fc8 | 616 | epos.offset -= adsize; |
1da177e4 LT |
617 | |
618 | alloc_count = (elen >> sb->s_blocksize_bits); | |
36350462 | 619 | if (alloc_count > block_count) { |
1da177e4 LT |
620 | alloc_count = block_count; |
621 | eloc.logicalBlockNum += alloc_count; | |
622 | elen -= (alloc_count << sb->s_blocksize_bits); | |
97e961fd | 623 | udf_write_aext(table, &epos, &eloc, |
4b11111a MS |
624 | (etype << 30) | elen, 1); |
625 | } else | |
626 | udf_delete_aext(table, epos, eloc, | |
627 | (etype << 30) | elen); | |
28de7948 | 628 | } else { |
1da177e4 | 629 | alloc_count = 0; |
28de7948 | 630 | } |
1da177e4 | 631 | |
3bf25cb4 | 632 | brelse(epos.bh); |
1da177e4 | 633 | |
146bca72 JK |
634 | if (alloc_count) |
635 | udf_add_free_space(sb, partition, -alloc_count); | |
1e7933de | 636 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
637 | return alloc_count; |
638 | } | |
639 | ||
cb00ea35 | 640 | static int udf_table_new_block(struct super_block *sb, |
cb00ea35 CG |
641 | struct inode *table, uint16_t partition, |
642 | uint32_t goal, int *err) | |
1da177e4 LT |
643 | { |
644 | struct udf_sb_info *sbi = UDF_SB(sb); | |
645 | uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; | |
646 | uint32_t newblock = 0, adsize; | |
ff116fc8 | 647 | uint32_t elen, goal_elen = 0; |
5ca4e4be | 648 | struct kernel_lb_addr eloc, uninitialized_var(goal_eloc); |
ff116fc8 | 649 | struct extent_position epos, goal_epos; |
1da177e4 | 650 | int8_t etype; |
48d6d8ff | 651 | struct udf_inode_info *iinfo = UDF_I(table); |
1da177e4 LT |
652 | |
653 | *err = -ENOSPC; | |
654 | ||
48d6d8ff | 655 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 656 | adsize = sizeof(struct short_ad); |
48d6d8ff | 657 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 658 | adsize = sizeof(struct long_ad); |
1da177e4 LT |
659 | else |
660 | return newblock; | |
661 | ||
1e7933de | 662 | mutex_lock(&sbi->s_alloc_mutex); |
3391faa4 | 663 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
664 | goal = 0; |
665 | ||
4b11111a MS |
666 | /* We search for the closest matching block to goal. If we find |
667 | a exact hit, we stop. Otherwise we keep going till we run out | |
668 | of extents. We store the buffer_head, bloc, and extoffset | |
669 | of the current closest match and use that when we are done. | |
cb00ea35 | 670 | */ |
ff116fc8 | 671 | epos.offset = sizeof(struct unallocSpaceEntry); |
48d6d8ff | 672 | epos.block = iinfo->i_location; |
ff116fc8 | 673 | epos.bh = goal_epos.bh = NULL; |
1da177e4 | 674 | |
28de7948 CG |
675 | while (spread && |
676 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
cb00ea35 | 677 | if (goal >= eloc.logicalBlockNum) { |
4b11111a MS |
678 | if (goal < eloc.logicalBlockNum + |
679 | (elen >> sb->s_blocksize_bits)) | |
1da177e4 LT |
680 | nspread = 0; |
681 | else | |
682 | nspread = goal - eloc.logicalBlockNum - | |
28de7948 CG |
683 | (elen >> sb->s_blocksize_bits); |
684 | } else { | |
1da177e4 | 685 | nspread = eloc.logicalBlockNum - goal; |
28de7948 | 686 | } |
1da177e4 | 687 | |
cb00ea35 | 688 | if (nspread < spread) { |
1da177e4 | 689 | spread = nspread; |
cb00ea35 | 690 | if (goal_epos.bh != epos.bh) { |
3bf25cb4 | 691 | brelse(goal_epos.bh); |
ff116fc8 | 692 | goal_epos.bh = epos.bh; |
3bf25cb4 | 693 | get_bh(goal_epos.bh); |
1da177e4 | 694 | } |
ff116fc8 JK |
695 | goal_epos.block = epos.block; |
696 | goal_epos.offset = epos.offset - adsize; | |
1da177e4 LT |
697 | goal_eloc = eloc; |
698 | goal_elen = (etype << 30) | elen; | |
699 | } | |
700 | } | |
701 | ||
3bf25cb4 | 702 | brelse(epos.bh); |
1da177e4 | 703 | |
cb00ea35 | 704 | if (spread == 0xFFFFFFFF) { |
3bf25cb4 | 705 | brelse(goal_epos.bh); |
1e7933de | 706 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
707 | return 0; |
708 | } | |
709 | ||
710 | /* Only allocate blocks from the beginning of the extent. | |
711 | That way, we only delete (empty) extents, never have to insert an | |
712 | extent because of splitting */ | |
713 | /* This works, but very poorly.... */ | |
714 | ||
715 | newblock = goal_eloc.logicalBlockNum; | |
cb00ea35 | 716 | goal_eloc.logicalBlockNum++; |
1da177e4 | 717 | goal_elen -= sb->s_blocksize; |
1da177e4 LT |
718 | |
719 | if (goal_elen) | |
97e961fd | 720 | udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1); |
1da177e4 | 721 | else |
ff116fc8 | 722 | udf_delete_aext(table, goal_epos, goal_eloc, goal_elen); |
3bf25cb4 | 723 | brelse(goal_epos.bh); |
1da177e4 | 724 | |
146bca72 | 725 | udf_add_free_space(sb, partition, -1); |
1da177e4 | 726 | |
1e7933de | 727 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
728 | *err = 0; |
729 | return newblock; | |
730 | } | |
731 | ||
97e961fd PE |
732 | void udf_free_blocks(struct super_block *sb, struct inode *inode, |
733 | struct kernel_lb_addr *bloc, uint32_t offset, | |
734 | uint32_t count) | |
1da177e4 | 735 | { |
97e961fd | 736 | uint16_t partition = bloc->partitionReferenceNum; |
6c79e987 | 737 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
1da177e4 | 738 | |
6c79e987 | 739 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { |
fd4287db | 740 | udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap, |
e650b94a | 741 | bloc, offset, count); |
6c79e987 | 742 | } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { |
fd4287db | 743 | udf_table_free_blocks(sb, map->s_uspace.s_table, |
e650b94a | 744 | bloc, offset, count); |
6c79e987 | 745 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) { |
fd4287db | 746 | udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap, |
e650b94a | 747 | bloc, offset, count); |
6c79e987 | 748 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) { |
fd4287db | 749 | udf_table_free_blocks(sb, map->s_fspace.s_table, |
e650b94a | 750 | bloc, offset, count); |
28de7948 | 751 | } |
fd4287db JK |
752 | |
753 | if (inode) { | |
754 | inode_sub_bytes(inode, | |
755 | ((sector_t)count) << sb->s_blocksize_bits); | |
756 | } | |
1da177e4 LT |
757 | } |
758 | ||
cb00ea35 CG |
759 | inline int udf_prealloc_blocks(struct super_block *sb, |
760 | struct inode *inode, | |
761 | uint16_t partition, uint32_t first_block, | |
762 | uint32_t block_count) | |
1da177e4 | 763 | { |
6c79e987 | 764 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
fd4287db | 765 | sector_t allocated; |
6c79e987 | 766 | |
4b11111a | 767 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
fd4287db JK |
768 | allocated = udf_bitmap_prealloc_blocks(sb, |
769 | map->s_uspace.s_bitmap, | |
770 | partition, first_block, | |
771 | block_count); | |
4b11111a | 772 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
fd4287db JK |
773 | allocated = udf_table_prealloc_blocks(sb, |
774 | map->s_uspace.s_table, | |
775 | partition, first_block, | |
776 | block_count); | |
4b11111a | 777 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) |
fd4287db JK |
778 | allocated = udf_bitmap_prealloc_blocks(sb, |
779 | map->s_fspace.s_bitmap, | |
780 | partition, first_block, | |
781 | block_count); | |
4b11111a | 782 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) |
fd4287db JK |
783 | allocated = udf_table_prealloc_blocks(sb, |
784 | map->s_fspace.s_table, | |
785 | partition, first_block, | |
786 | block_count); | |
4b11111a | 787 | else |
1da177e4 | 788 | return 0; |
fd4287db JK |
789 | |
790 | if (inode && allocated > 0) | |
791 | inode_add_bytes(inode, allocated << sb->s_blocksize_bits); | |
792 | return allocated; | |
1da177e4 LT |
793 | } |
794 | ||
cb00ea35 CG |
795 | inline int udf_new_block(struct super_block *sb, |
796 | struct inode *inode, | |
797 | uint16_t partition, uint32_t goal, int *err) | |
1da177e4 | 798 | { |
6c79e987 | 799 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
fd4287db | 800 | int block; |
3bf25cb4 | 801 | |
4b11111a | 802 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
fd4287db JK |
803 | block = udf_bitmap_new_block(sb, |
804 | map->s_uspace.s_bitmap, | |
805 | partition, goal, err); | |
4b11111a | 806 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
fd4287db JK |
807 | block = udf_table_new_block(sb, |
808 | map->s_uspace.s_table, | |
28de7948 | 809 | partition, goal, err); |
fd4287db JK |
810 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) |
811 | block = udf_bitmap_new_block(sb, | |
812 | map->s_fspace.s_bitmap, | |
813 | partition, goal, err); | |
4b11111a | 814 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) |
fd4287db JK |
815 | block = udf_table_new_block(sb, |
816 | map->s_fspace.s_table, | |
817 | partition, goal, err); | |
4b11111a | 818 | else { |
1da177e4 LT |
819 | *err = -EIO; |
820 | return 0; | |
821 | } | |
fd4287db JK |
822 | if (inode && block) |
823 | inode_add_bytes(inode, sb->s_blocksize); | |
824 | return block; | |
1da177e4 | 825 | } |