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