]>
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 CG |
107 | static void udf_bitmap_free_blocks(struct super_block *sb, |
108 | struct inode *inode, | |
109 | struct udf_bitmap *bitmap, | |
97e961fd PE |
110 | struct kernel_lb_addr *bloc, |
111 | uint32_t offset, | |
cb00ea35 | 112 | uint32_t count) |
1da177e4 LT |
113 | { |
114 | struct udf_sb_info *sbi = UDF_SB(sb); | |
cb00ea35 | 115 | struct buffer_head *bh = NULL; |
97e961fd | 116 | struct udf_part_map *partmap; |
1da177e4 LT |
117 | unsigned long block; |
118 | unsigned long block_group; | |
119 | unsigned long bit; | |
120 | unsigned long i; | |
121 | int bitmap_nr; | |
122 | unsigned long overflow; | |
123 | ||
1e7933de | 124 | mutex_lock(&sbi->s_alloc_mutex); |
97e961fd | 125 | partmap = &sbi->s_partmaps[bloc->partitionReferenceNum]; |
69ecbbed DC |
126 | if (bloc->logicalBlockNum + count < count || |
127 | (bloc->logicalBlockNum + count) > partmap->s_partition_len) { | |
28de7948 | 128 | udf_debug("%d < %d || %d + %d > %d\n", |
a983f368 JP |
129 | bloc->logicalBlockNum, 0, |
130 | bloc->logicalBlockNum, count, | |
131 | partmap->s_partition_len); | |
1da177e4 LT |
132 | goto error_return; |
133 | } | |
134 | ||
97e961fd | 135 | block = bloc->logicalBlockNum + offset + |
4b11111a | 136 | (sizeof(struct spaceBitmapDesc) << 3); |
1da177e4 | 137 | |
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)) { | |
157 | udf_debug("bit %ld already set\n", bit + i); | |
158 | udf_debug("byte=%2x\n", | |
a983f368 | 159 | ((char *)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 CG |
174 | static int udf_bitmap_prealloc_blocks(struct super_block *sb, |
175 | struct inode *inode, | |
176 | struct udf_bitmap *bitmap, | |
177 | uint16_t partition, uint32_t first_block, | |
178 | uint32_t block_count) | |
1da177e4 LT |
179 | { |
180 | struct udf_sb_info *sbi = UDF_SB(sb); | |
181 | int alloc_count = 0; | |
182 | int bit, block, block_group, group_start; | |
183 | int nr_groups, bitmap_nr; | |
184 | struct buffer_head *bh; | |
6c79e987 | 185 | __u32 part_len; |
1da177e4 | 186 | |
1e7933de | 187 | mutex_lock(&sbi->s_alloc_mutex); |
6c79e987 | 188 | part_len = sbi->s_partmaps[partition].s_partition_len; |
3391faa4 | 189 | if (first_block >= part_len) |
1da177e4 LT |
190 | goto out; |
191 | ||
6c79e987 MS |
192 | if (first_block + block_count > part_len) |
193 | block_count = part_len - first_block; | |
1da177e4 | 194 | |
4daa1b87 MS |
195 | do { |
196 | nr_groups = udf_compute_nr_groups(sb, partition); | |
197 | block = first_block + (sizeof(struct spaceBitmapDesc) << 3); | |
198 | block_group = block >> (sb->s_blocksize_bits + 3); | |
199 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | |
1da177e4 | 200 | |
4daa1b87 MS |
201 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
202 | if (bitmap_nr < 0) | |
203 | goto out; | |
204 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
1da177e4 | 205 | |
4daa1b87 | 206 | bit = block % (sb->s_blocksize << 3); |
1da177e4 | 207 | |
4daa1b87 | 208 | while (bit < (sb->s_blocksize << 3) && block_count > 0) { |
36350462 | 209 | if (!udf_clear_bit(bit, bh->b_data)) |
4daa1b87 | 210 | goto out; |
4daa1b87 MS |
211 | block_count--; |
212 | alloc_count++; | |
213 | bit++; | |
214 | block++; | |
1da177e4 | 215 | } |
4daa1b87 MS |
216 | mark_buffer_dirty(bh); |
217 | } while (block_count > 0); | |
218 | ||
28de7948 | 219 | out: |
146bca72 | 220 | udf_add_free_space(sb, partition, -alloc_count); |
1e7933de | 221 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
222 | return alloc_count; |
223 | } | |
224 | ||
cb00ea35 CG |
225 | static int udf_bitmap_new_block(struct super_block *sb, |
226 | struct inode *inode, | |
227 | struct udf_bitmap *bitmap, uint16_t partition, | |
228 | uint32_t goal, int *err) | |
1da177e4 LT |
229 | { |
230 | struct udf_sb_info *sbi = UDF_SB(sb); | |
cb00ea35 | 231 | int newbit, bit = 0, block, block_group, group_start; |
1da177e4 LT |
232 | int end_goal, nr_groups, bitmap_nr, i; |
233 | struct buffer_head *bh = NULL; | |
234 | char *ptr; | |
235 | int newblock = 0; | |
236 | ||
237 | *err = -ENOSPC; | |
1e7933de | 238 | mutex_lock(&sbi->s_alloc_mutex); |
1da177e4 | 239 | |
28de7948 | 240 | repeat: |
3391faa4 | 241 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
242 | goal = 0; |
243 | ||
244 | nr_groups = bitmap->s_nr_groups; | |
245 | block = goal + (sizeof(struct spaceBitmapDesc) << 3); | |
246 | block_group = block >> (sb->s_blocksize_bits + 3); | |
247 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | |
248 | ||
249 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | |
250 | if (bitmap_nr < 0) | |
251 | goto error_return; | |
252 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
28de7948 CG |
253 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
254 | sb->s_blocksize - group_start); | |
1da177e4 | 255 | |
cb00ea35 | 256 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
1da177e4 | 257 | bit = block % (sb->s_blocksize << 3); |
28de7948 | 258 | if (udf_test_bit(bit, bh->b_data)) |
1da177e4 | 259 | goto got_block; |
28de7948 | 260 | |
1da177e4 LT |
261 | end_goal = (bit + 63) & ~63; |
262 | bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); | |
263 | if (bit < end_goal) | |
264 | goto got_block; | |
28de7948 | 265 | |
4b11111a MS |
266 | ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, |
267 | sb->s_blocksize - ((bit + 7) >> 3)); | |
1da177e4 | 268 | newbit = (ptr - ((char *)bh->b_data)) << 3; |
cb00ea35 | 269 | if (newbit < sb->s_blocksize << 3) { |
1da177e4 LT |
270 | bit = newbit; |
271 | goto search_back; | |
272 | } | |
28de7948 | 273 | |
4b11111a MS |
274 | newbit = udf_find_next_one_bit(bh->b_data, |
275 | sb->s_blocksize << 3, bit); | |
cb00ea35 | 276 | if (newbit < sb->s_blocksize << 3) { |
1da177e4 LT |
277 | bit = newbit; |
278 | goto got_block; | |
279 | } | |
280 | } | |
281 | ||
cb00ea35 CG |
282 | for (i = 0; i < (nr_groups * 2); i++) { |
283 | block_group++; | |
1da177e4 LT |
284 | if (block_group >= nr_groups) |
285 | block_group = 0; | |
286 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | |
287 | ||
288 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | |
289 | if (bitmap_nr < 0) | |
290 | goto error_return; | |
291 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
cb00ea35 | 292 | if (i < nr_groups) { |
28de7948 CG |
293 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
294 | sb->s_blocksize - group_start); | |
cb00ea35 | 295 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
1da177e4 LT |
296 | bit = (ptr - ((char *)bh->b_data)) << 3; |
297 | break; | |
298 | } | |
cb00ea35 | 299 | } else { |
6f644e5f | 300 | bit = udf_find_next_one_bit(bh->b_data, |
28de7948 CG |
301 | sb->s_blocksize << 3, |
302 | group_start << 3); | |
1da177e4 LT |
303 | if (bit < sb->s_blocksize << 3) |
304 | break; | |
305 | } | |
306 | } | |
cb00ea35 | 307 | if (i >= (nr_groups * 2)) { |
1e7933de | 308 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
309 | return newblock; |
310 | } | |
311 | if (bit < sb->s_blocksize << 3) | |
312 | goto search_back; | |
313 | else | |
4b11111a MS |
314 | bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, |
315 | group_start << 3); | |
cb00ea35 | 316 | if (bit >= sb->s_blocksize << 3) { |
1e7933de | 317 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
318 | return 0; |
319 | } | |
320 | ||
28de7948 | 321 | search_back: |
4b11111a MS |
322 | i = 0; |
323 | while (i < 7 && bit > (group_start << 3) && | |
324 | udf_test_bit(bit - 1, bh->b_data)) { | |
325 | ++i; | |
326 | --bit; | |
327 | } | |
1da177e4 | 328 | |
28de7948 | 329 | got_block: |
1da177e4 | 330 | newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - |
28de7948 | 331 | (sizeof(struct spaceBitmapDesc) << 3); |
1da177e4 | 332 | |
cb00ea35 | 333 | if (!udf_clear_bit(bit, bh->b_data)) { |
1da177e4 LT |
334 | udf_debug("bit already cleared for block %d\n", bit); |
335 | goto repeat; | |
336 | } | |
337 | ||
338 | mark_buffer_dirty(bh); | |
339 | ||
146bca72 | 340 | udf_add_free_space(sb, partition, -1); |
1e7933de | 341 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
342 | *err = 0; |
343 | return newblock; | |
344 | ||
28de7948 | 345 | error_return: |
1da177e4 | 346 | *err = -EIO; |
1e7933de | 347 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
348 | return 0; |
349 | } | |
350 | ||
cb00ea35 CG |
351 | static void udf_table_free_blocks(struct super_block *sb, |
352 | struct inode *inode, | |
353 | struct inode *table, | |
97e961fd PE |
354 | struct kernel_lb_addr *bloc, |
355 | uint32_t offset, | |
cb00ea35 | 356 | uint32_t count) |
1da177e4 LT |
357 | { |
358 | struct udf_sb_info *sbi = UDF_SB(sb); | |
97e961fd | 359 | struct udf_part_map *partmap; |
1da177e4 | 360 | uint32_t start, end; |
ff116fc8 | 361 | uint32_t elen; |
5ca4e4be | 362 | struct kernel_lb_addr eloc; |
ff116fc8 | 363 | struct extent_position oepos, epos; |
1da177e4 LT |
364 | int8_t etype; |
365 | int i; | |
48d6d8ff | 366 | struct udf_inode_info *iinfo; |
1da177e4 | 367 | |
1e7933de | 368 | mutex_lock(&sbi->s_alloc_mutex); |
97e961fd | 369 | partmap = &sbi->s_partmaps[bloc->partitionReferenceNum]; |
69ecbbed DC |
370 | if (bloc->logicalBlockNum + count < count || |
371 | (bloc->logicalBlockNum + count) > partmap->s_partition_len) { | |
28de7948 | 372 | udf_debug("%d < %d || %d + %d > %d\n", |
a983f368 JP |
373 | bloc->logicalBlockNum, 0, |
374 | bloc->logicalBlockNum, count, | |
97e961fd | 375 | partmap->s_partition_len); |
1da177e4 LT |
376 | goto error_return; |
377 | } | |
378 | ||
48d6d8ff | 379 | iinfo = UDF_I(table); |
146bca72 | 380 | udf_add_free_space(sb, sbi->s_partition, count); |
1da177e4 | 381 | |
97e961fd PE |
382 | start = bloc->logicalBlockNum + offset; |
383 | end = bloc->logicalBlockNum + offset + count - 1; | |
1da177e4 | 384 | |
ff116fc8 | 385 | epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); |
1da177e4 | 386 | elen = 0; |
48d6d8ff | 387 | epos.block = oepos.block = iinfo->i_location; |
ff116fc8 | 388 | epos.bh = oepos.bh = NULL; |
1da177e4 | 389 | |
28de7948 CG |
390 | while (count && |
391 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
4b11111a MS |
392 | if (((eloc.logicalBlockNum + |
393 | (elen >> sb->s_blocksize_bits)) == start)) { | |
394 | if ((0x3FFFFFFF - elen) < | |
395 | (count << sb->s_blocksize_bits)) { | |
396 | uint32_t tmp = ((0x3FFFFFFF - elen) >> | |
397 | sb->s_blocksize_bits); | |
398 | count -= tmp; | |
399 | start += tmp; | |
400 | elen = (etype << 30) | | |
401 | (0x40000000 - sb->s_blocksize); | |
cb00ea35 | 402 | } else { |
4b11111a MS |
403 | elen = (etype << 30) | |
404 | (elen + | |
405 | (count << sb->s_blocksize_bits)); | |
1da177e4 LT |
406 | start += count; |
407 | count = 0; | |
408 | } | |
97e961fd | 409 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
cb00ea35 | 410 | } else if (eloc.logicalBlockNum == (end + 1)) { |
4b11111a MS |
411 | if ((0x3FFFFFFF - elen) < |
412 | (count << sb->s_blocksize_bits)) { | |
413 | uint32_t tmp = ((0x3FFFFFFF - elen) >> | |
414 | sb->s_blocksize_bits); | |
415 | count -= tmp; | |
416 | end -= tmp; | |
417 | eloc.logicalBlockNum -= tmp; | |
418 | elen = (etype << 30) | | |
419 | (0x40000000 - sb->s_blocksize); | |
cb00ea35 | 420 | } else { |
1da177e4 | 421 | eloc.logicalBlockNum = start; |
4b11111a MS |
422 | elen = (etype << 30) | |
423 | (elen + | |
424 | (count << sb->s_blocksize_bits)); | |
1da177e4 LT |
425 | end -= count; |
426 | count = 0; | |
427 | } | |
97e961fd | 428 | udf_write_aext(table, &oepos, &eloc, elen, 1); |
1da177e4 LT |
429 | } |
430 | ||
cb00ea35 | 431 | if (epos.bh != oepos.bh) { |
1da177e4 | 432 | i = -1; |
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; | |
5ca4e4be PE |
458 | struct short_ad *sad = NULL; |
459 | struct long_ad *lad = NULL; | |
1da177e4 LT |
460 | struct allocExtDesc *aed; |
461 | ||
462 | eloc.logicalBlockNum = start; | |
28de7948 CG |
463 | elen = EXT_RECORDED_ALLOCATED | |
464 | (count << sb->s_blocksize_bits); | |
1da177e4 | 465 | |
48d6d8ff | 466 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 467 | adsize = sizeof(struct short_ad); |
48d6d8ff | 468 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 469 | adsize = sizeof(struct long_ad); |
48d6d8ff | 470 | else { |
3bf25cb4 JK |
471 | brelse(oepos.bh); |
472 | brelse(epos.bh); | |
1da177e4 LT |
473 | goto error_return; |
474 | } | |
475 | ||
cb00ea35 | 476 | if (epos.offset + (2 * adsize) > sb->s_blocksize) { |
391e8bbd | 477 | unsigned char *sptr, *dptr; |
1da177e4 | 478 | int loffset; |
cb00ea35 | 479 | |
3bf25cb4 | 480 | brelse(oepos.bh); |
ff116fc8 | 481 | oepos = epos; |
1da177e4 LT |
482 | |
483 | /* Steal a block from the extent being free'd */ | |
ff116fc8 | 484 | epos.block.logicalBlockNum = eloc.logicalBlockNum; |
cb00ea35 | 485 | eloc.logicalBlockNum++; |
1da177e4 LT |
486 | elen -= sb->s_blocksize; |
487 | ||
4b11111a | 488 | epos.bh = udf_tread(sb, |
97e961fd | 489 | udf_get_lb_pblock(sb, &epos.block, 0)); |
4b11111a | 490 | if (!epos.bh) { |
3bf25cb4 | 491 | brelse(oepos.bh); |
1da177e4 LT |
492 | goto error_return; |
493 | } | |
ff116fc8 | 494 | aed = (struct allocExtDesc *)(epos.bh->b_data); |
4b11111a MS |
495 | aed->previousAllocExtLocation = |
496 | cpu_to_le32(oepos.block.logicalBlockNum); | |
cb00ea35 | 497 | if (epos.offset + adsize > sb->s_blocksize) { |
ff116fc8 | 498 | loffset = epos.offset; |
1da177e4 | 499 | aed->lengthAllocDescs = cpu_to_le32(adsize); |
48d6d8ff | 500 | sptr = iinfo->i_ext.i_data + epos.offset |
c0b34438 | 501 | - adsize; |
4b11111a MS |
502 | dptr = epos.bh->b_data + |
503 | sizeof(struct allocExtDesc); | |
1da177e4 | 504 | memcpy(dptr, sptr, adsize); |
4b11111a MS |
505 | epos.offset = sizeof(struct allocExtDesc) + |
506 | adsize; | |
cb00ea35 | 507 | } else { |
ff116fc8 | 508 | loffset = epos.offset + adsize; |
1da177e4 | 509 | aed->lengthAllocDescs = cpu_to_le32(0); |
cb00ea35 | 510 | if (oepos.bh) { |
f5cc15da | 511 | sptr = oepos.bh->b_data + epos.offset; |
4b11111a MS |
512 | aed = (struct allocExtDesc *) |
513 | oepos.bh->b_data; | |
c2104fda | 514 | le32_add_cpu(&aed->lengthAllocDescs, |
515 | adsize); | |
cb00ea35 | 516 | } else { |
48d6d8ff | 517 | sptr = iinfo->i_ext.i_data + |
c0b34438 | 518 | epos.offset; |
48d6d8ff | 519 | iinfo->i_lenAlloc += adsize; |
1da177e4 LT |
520 | mark_inode_dirty(table); |
521 | } | |
f5cc15da | 522 | epos.offset = sizeof(struct allocExtDesc); |
1da177e4 | 523 | } |
6c79e987 | 524 | if (sbi->s_udfrev >= 0x0200) |
4b11111a MS |
525 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
526 | 3, 1, epos.block.logicalBlockNum, | |
5ca4e4be | 527 | sizeof(struct tag)); |
1da177e4 | 528 | else |
4b11111a MS |
529 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
530 | 2, 1, epos.block.logicalBlockNum, | |
5ca4e4be | 531 | sizeof(struct tag)); |
28de7948 | 532 | |
48d6d8ff | 533 | switch (iinfo->i_alloc_type) { |
4b11111a | 534 | case ICBTAG_FLAG_AD_SHORT: |
5ca4e4be | 535 | sad = (struct short_ad *)sptr; |
4b11111a MS |
536 | sad->extLength = cpu_to_le32( |
537 | EXT_NEXT_EXTENT_ALLOCDECS | | |
538 | sb->s_blocksize); | |
539 | sad->extPosition = | |
540 | cpu_to_le32(epos.block.logicalBlockNum); | |
541 | break; | |
542 | case ICBTAG_FLAG_AD_LONG: | |
5ca4e4be | 543 | lad = (struct long_ad *)sptr; |
4b11111a MS |
544 | lad->extLength = cpu_to_le32( |
545 | EXT_NEXT_EXTENT_ALLOCDECS | | |
546 | sb->s_blocksize); | |
547 | lad->extLocation = | |
548 | cpu_to_lelb(epos.block); | |
549 | break; | |
1da177e4 | 550 | } |
cb00ea35 | 551 | if (oepos.bh) { |
ff116fc8 JK |
552 | udf_update_tag(oepos.bh->b_data, loffset); |
553 | mark_buffer_dirty(oepos.bh); | |
28de7948 | 554 | } else { |
1da177e4 | 555 | mark_inode_dirty(table); |
28de7948 | 556 | } |
1da177e4 LT |
557 | } |
558 | ||
4b11111a MS |
559 | /* It's possible that stealing the block emptied the extent */ |
560 | if (elen) { | |
97e961fd | 561 | udf_write_aext(table, &epos, &eloc, elen, 1); |
1da177e4 | 562 | |
cb00ea35 | 563 | if (!epos.bh) { |
48d6d8ff | 564 | iinfo->i_lenAlloc += adsize; |
1da177e4 | 565 | mark_inode_dirty(table); |
cb00ea35 | 566 | } else { |
ff116fc8 | 567 | aed = (struct allocExtDesc *)epos.bh->b_data; |
c2104fda | 568 | le32_add_cpu(&aed->lengthAllocDescs, adsize); |
ff116fc8 JK |
569 | udf_update_tag(epos.bh->b_data, epos.offset); |
570 | mark_buffer_dirty(epos.bh); | |
1da177e4 LT |
571 | } |
572 | } | |
573 | } | |
574 | ||
3bf25cb4 JK |
575 | brelse(epos.bh); |
576 | brelse(oepos.bh); | |
1da177e4 | 577 | |
28de7948 | 578 | error_return: |
1e7933de | 579 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
580 | return; |
581 | } | |
582 | ||
cb00ea35 CG |
583 | static int udf_table_prealloc_blocks(struct super_block *sb, |
584 | struct inode *inode, | |
585 | struct inode *table, uint16_t partition, | |
586 | uint32_t first_block, uint32_t block_count) | |
1da177e4 LT |
587 | { |
588 | struct udf_sb_info *sbi = UDF_SB(sb); | |
589 | int alloc_count = 0; | |
ff116fc8 | 590 | uint32_t elen, adsize; |
5ca4e4be | 591 | struct kernel_lb_addr eloc; |
ff116fc8 | 592 | struct extent_position epos; |
1da177e4 | 593 | int8_t etype = -1; |
48d6d8ff | 594 | struct udf_inode_info *iinfo; |
1da177e4 | 595 | |
3391faa4 | 596 | if (first_block >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
597 | return 0; |
598 | ||
48d6d8ff MS |
599 | iinfo = UDF_I(table); |
600 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | |
5ca4e4be | 601 | adsize = sizeof(struct short_ad); |
48d6d8ff | 602 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 603 | adsize = sizeof(struct long_ad); |
1da177e4 LT |
604 | else |
605 | return 0; | |
606 | ||
1e7933de | 607 | mutex_lock(&sbi->s_alloc_mutex); |
ff116fc8 | 608 | epos.offset = sizeof(struct unallocSpaceEntry); |
48d6d8ff | 609 | epos.block = iinfo->i_location; |
ff116fc8 | 610 | epos.bh = NULL; |
1da177e4 LT |
611 | eloc.logicalBlockNum = 0xFFFFFFFF; |
612 | ||
28de7948 CG |
613 | while (first_block != eloc.logicalBlockNum && |
614 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
1da177e4 | 615 | udf_debug("eloc=%d, elen=%d, first_block=%d\n", |
cb00ea35 | 616 | eloc.logicalBlockNum, elen, first_block); |
28de7948 | 617 | ; /* empty loop body */ |
1da177e4 LT |
618 | } |
619 | ||
cb00ea35 | 620 | if (first_block == eloc.logicalBlockNum) { |
ff116fc8 | 621 | epos.offset -= adsize; |
1da177e4 LT |
622 | |
623 | alloc_count = (elen >> sb->s_blocksize_bits); | |
36350462 | 624 | if (alloc_count > block_count) { |
1da177e4 LT |
625 | alloc_count = block_count; |
626 | eloc.logicalBlockNum += alloc_count; | |
627 | elen -= (alloc_count << sb->s_blocksize_bits); | |
97e961fd | 628 | udf_write_aext(table, &epos, &eloc, |
4b11111a MS |
629 | (etype << 30) | elen, 1); |
630 | } else | |
631 | udf_delete_aext(table, epos, eloc, | |
632 | (etype << 30) | elen); | |
28de7948 | 633 | } else { |
1da177e4 | 634 | alloc_count = 0; |
28de7948 | 635 | } |
1da177e4 | 636 | |
3bf25cb4 | 637 | brelse(epos.bh); |
1da177e4 | 638 | |
146bca72 JK |
639 | if (alloc_count) |
640 | udf_add_free_space(sb, partition, -alloc_count); | |
1e7933de | 641 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
642 | return alloc_count; |
643 | } | |
644 | ||
cb00ea35 CG |
645 | static int udf_table_new_block(struct super_block *sb, |
646 | struct inode *inode, | |
647 | struct inode *table, uint16_t partition, | |
648 | uint32_t goal, int *err) | |
1da177e4 LT |
649 | { |
650 | struct udf_sb_info *sbi = UDF_SB(sb); | |
651 | uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; | |
652 | uint32_t newblock = 0, adsize; | |
ff116fc8 | 653 | uint32_t elen, goal_elen = 0; |
5ca4e4be | 654 | struct kernel_lb_addr eloc, uninitialized_var(goal_eloc); |
ff116fc8 | 655 | struct extent_position epos, goal_epos; |
1da177e4 | 656 | int8_t etype; |
48d6d8ff | 657 | struct udf_inode_info *iinfo = UDF_I(table); |
1da177e4 LT |
658 | |
659 | *err = -ENOSPC; | |
660 | ||
48d6d8ff | 661 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 662 | adsize = sizeof(struct short_ad); |
48d6d8ff | 663 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 664 | adsize = sizeof(struct long_ad); |
1da177e4 LT |
665 | else |
666 | return newblock; | |
667 | ||
1e7933de | 668 | mutex_lock(&sbi->s_alloc_mutex); |
3391faa4 | 669 | if (goal >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
670 | goal = 0; |
671 | ||
4b11111a MS |
672 | /* We search for the closest matching block to goal. If we find |
673 | a exact hit, we stop. Otherwise we keep going till we run out | |
674 | of extents. We store the buffer_head, bloc, and extoffset | |
675 | of the current closest match and use that when we are done. | |
cb00ea35 | 676 | */ |
ff116fc8 | 677 | epos.offset = sizeof(struct unallocSpaceEntry); |
48d6d8ff | 678 | epos.block = iinfo->i_location; |
ff116fc8 | 679 | epos.bh = goal_epos.bh = NULL; |
1da177e4 | 680 | |
28de7948 CG |
681 | while (spread && |
682 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
cb00ea35 | 683 | if (goal >= eloc.logicalBlockNum) { |
4b11111a MS |
684 | if (goal < eloc.logicalBlockNum + |
685 | (elen >> sb->s_blocksize_bits)) | |
1da177e4 LT |
686 | nspread = 0; |
687 | else | |
688 | nspread = goal - eloc.logicalBlockNum - | |
28de7948 CG |
689 | (elen >> sb->s_blocksize_bits); |
690 | } else { | |
1da177e4 | 691 | nspread = eloc.logicalBlockNum - goal; |
28de7948 | 692 | } |
1da177e4 | 693 | |
cb00ea35 | 694 | if (nspread < spread) { |
1da177e4 | 695 | spread = nspread; |
cb00ea35 | 696 | if (goal_epos.bh != epos.bh) { |
3bf25cb4 | 697 | brelse(goal_epos.bh); |
ff116fc8 | 698 | goal_epos.bh = epos.bh; |
3bf25cb4 | 699 | get_bh(goal_epos.bh); |
1da177e4 | 700 | } |
ff116fc8 JK |
701 | goal_epos.block = epos.block; |
702 | goal_epos.offset = epos.offset - adsize; | |
1da177e4 LT |
703 | goal_eloc = eloc; |
704 | goal_elen = (etype << 30) | elen; | |
705 | } | |
706 | } | |
707 | ||
3bf25cb4 | 708 | brelse(epos.bh); |
1da177e4 | 709 | |
cb00ea35 | 710 | if (spread == 0xFFFFFFFF) { |
3bf25cb4 | 711 | brelse(goal_epos.bh); |
1e7933de | 712 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
713 | return 0; |
714 | } | |
715 | ||
716 | /* Only allocate blocks from the beginning of the extent. | |
717 | That way, we only delete (empty) extents, never have to insert an | |
718 | extent because of splitting */ | |
719 | /* This works, but very poorly.... */ | |
720 | ||
721 | newblock = goal_eloc.logicalBlockNum; | |
cb00ea35 | 722 | goal_eloc.logicalBlockNum++; |
1da177e4 | 723 | goal_elen -= sb->s_blocksize; |
1da177e4 LT |
724 | |
725 | if (goal_elen) | |
97e961fd | 726 | udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1); |
1da177e4 | 727 | else |
ff116fc8 | 728 | udf_delete_aext(table, goal_epos, goal_eloc, goal_elen); |
3bf25cb4 | 729 | brelse(goal_epos.bh); |
1da177e4 | 730 | |
146bca72 | 731 | udf_add_free_space(sb, partition, -1); |
1da177e4 | 732 | |
1e7933de | 733 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
734 | *err = 0; |
735 | return newblock; | |
736 | } | |
737 | ||
97e961fd PE |
738 | void udf_free_blocks(struct super_block *sb, struct inode *inode, |
739 | struct kernel_lb_addr *bloc, uint32_t offset, | |
740 | uint32_t count) | |
1da177e4 | 741 | { |
97e961fd | 742 | uint16_t partition = bloc->partitionReferenceNum; |
6c79e987 | 743 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
1da177e4 | 744 | |
6c79e987 | 745 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { |
e650b94a JK |
746 | udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap, |
747 | bloc, offset, count); | |
6c79e987 | 748 | } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { |
e650b94a JK |
749 | udf_table_free_blocks(sb, inode, map->s_uspace.s_table, |
750 | bloc, offset, count); | |
6c79e987 | 751 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) { |
e650b94a JK |
752 | udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap, |
753 | bloc, offset, count); | |
6c79e987 | 754 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) { |
e650b94a JK |
755 | udf_table_free_blocks(sb, inode, map->s_fspace.s_table, |
756 | bloc, offset, count); | |
28de7948 | 757 | } |
1da177e4 LT |
758 | } |
759 | ||
cb00ea35 CG |
760 | inline int udf_prealloc_blocks(struct super_block *sb, |
761 | struct inode *inode, | |
762 | uint16_t partition, uint32_t first_block, | |
763 | uint32_t block_count) | |
1da177e4 | 764 | { |
6c79e987 MS |
765 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
766 | ||
4b11111a | 767 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
1da177e4 | 768 | return udf_bitmap_prealloc_blocks(sb, inode, |
6c79e987 | 769 | map->s_uspace.s_bitmap, |
4b11111a MS |
770 | partition, first_block, |
771 | block_count); | |
772 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) | |
1da177e4 | 773 | return udf_table_prealloc_blocks(sb, inode, |
6c79e987 | 774 | map->s_uspace.s_table, |
4b11111a MS |
775 | partition, first_block, |
776 | block_count); | |
777 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) | |
1da177e4 | 778 | return udf_bitmap_prealloc_blocks(sb, inode, |
6c79e987 | 779 | map->s_fspace.s_bitmap, |
4b11111a MS |
780 | partition, first_block, |
781 | block_count); | |
782 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) | |
1da177e4 | 783 | return udf_table_prealloc_blocks(sb, inode, |
6c79e987 | 784 | map->s_fspace.s_table, |
4b11111a MS |
785 | partition, first_block, |
786 | block_count); | |
787 | else | |
1da177e4 LT |
788 | return 0; |
789 | } | |
790 | ||
cb00ea35 CG |
791 | inline int udf_new_block(struct super_block *sb, |
792 | struct inode *inode, | |
793 | uint16_t partition, uint32_t goal, int *err) | |
1da177e4 | 794 | { |
6c79e987 | 795 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
3bf25cb4 | 796 | |
4b11111a MS |
797 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
798 | return udf_bitmap_new_block(sb, inode, | |
6c79e987 | 799 | map->s_uspace.s_bitmap, |
28de7948 | 800 | partition, goal, err); |
4b11111a | 801 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
1da177e4 | 802 | return udf_table_new_block(sb, inode, |
6c79e987 | 803 | map->s_uspace.s_table, |
28de7948 | 804 | partition, goal, err); |
4b11111a | 805 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) |
1da177e4 | 806 | return udf_bitmap_new_block(sb, inode, |
6c79e987 | 807 | map->s_fspace.s_bitmap, |
28de7948 | 808 | partition, goal, err); |
4b11111a | 809 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) |
1da177e4 | 810 | return udf_table_new_block(sb, inode, |
6c79e987 | 811 | map->s_fspace.s_table, |
28de7948 | 812 | partition, goal, err); |
4b11111a | 813 | else { |
1da177e4 LT |
814 | *err = -EIO; |
815 | return 0; | |
816 | } | |
817 | } |