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 | ||
24 | #include <linux/quotaops.h> | |
25 | #include <linux/buffer_head.h> | |
26 | #include <linux/bitops.h> | |
27 | ||
28 | #include "udf_i.h" | |
29 | #include "udf_sb.h" | |
30 | ||
4b11111a MS |
31 | #define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr) |
32 | #define udf_set_bit(nr, addr) ext2_set_bit(nr, addr) | |
1da177e4 LT |
33 | #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr) |
34 | #define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size) | |
4b11111a MS |
35 | #define udf_find_next_one_bit(addr, size, offset) \ |
36 | find_next_one_bit(addr, size, offset) | |
1da177e4 LT |
37 | |
38 | #define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x) | |
4b11111a MS |
39 | #define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y) |
40 | #define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y)) | |
1da177e4 LT |
41 | #define uintBPL_t uint(BITS_PER_LONG) |
42 | #define uint(x) xuint(x) | |
43 | #define xuint(x) __le ## x | |
44 | ||
cb00ea35 | 45 | static inline int find_next_one_bit(void *addr, int size, int offset) |
1da177e4 | 46 | { |
cb00ea35 CG |
47 | uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG); |
48 | int result = offset & ~(BITS_PER_LONG - 1); | |
1da177e4 LT |
49 | unsigned long tmp; |
50 | ||
51 | if (offset >= size) | |
52 | return size; | |
53 | size -= result; | |
cb00ea35 CG |
54 | offset &= (BITS_PER_LONG - 1); |
55 | if (offset) { | |
1da177e4 LT |
56 | tmp = leBPL_to_cpup(p++); |
57 | tmp &= ~0UL << offset; | |
58 | if (size < BITS_PER_LONG) | |
59 | goto found_first; | |
60 | if (tmp) | |
61 | goto found_middle; | |
62 | size -= BITS_PER_LONG; | |
63 | result += BITS_PER_LONG; | |
64 | } | |
cb00ea35 | 65 | while (size & ~(BITS_PER_LONG - 1)) { |
4b11111a MS |
66 | tmp = leBPL_to_cpup(p++); |
67 | if (tmp) | |
1da177e4 LT |
68 | goto found_middle; |
69 | result += BITS_PER_LONG; | |
70 | size -= BITS_PER_LONG; | |
71 | } | |
72 | if (!size) | |
73 | return result; | |
74 | tmp = leBPL_to_cpup(p); | |
28de7948 | 75 | found_first: |
cb00ea35 | 76 | tmp &= ~0UL >> (BITS_PER_LONG - size); |
28de7948 | 77 | found_middle: |
1da177e4 LT |
78 | return result + ffz(~tmp); |
79 | } | |
80 | ||
81 | #define find_first_one_bit(addr, size)\ | |
82 | find_next_one_bit((addr), (size), 0) | |
83 | ||
cb00ea35 CG |
84 | static int read_block_bitmap(struct super_block *sb, |
85 | struct udf_bitmap *bitmap, unsigned int block, | |
86 | unsigned long bitmap_nr) | |
1da177e4 LT |
87 | { |
88 | struct buffer_head *bh = NULL; | |
89 | int retval = 0; | |
90 | kernel_lb_addr loc; | |
91 | ||
92 | loc.logicalBlockNum = bitmap->s_extPosition; | |
6c79e987 | 93 | loc.partitionReferenceNum = UDF_SB(sb)->s_partition; |
1da177e4 LT |
94 | |
95 | bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block)); | |
4b11111a | 96 | if (!bh) |
1da177e4 | 97 | retval = -EIO; |
4b11111a | 98 | |
1da177e4 LT |
99 | bitmap->s_block_bitmap[bitmap_nr] = bh; |
100 | return retval; | |
101 | } | |
102 | ||
cb00ea35 CG |
103 | static int __load_block_bitmap(struct super_block *sb, |
104 | struct udf_bitmap *bitmap, | |
105 | unsigned int block_group) | |
1da177e4 LT |
106 | { |
107 | int retval = 0; | |
108 | int nr_groups = bitmap->s_nr_groups; | |
109 | ||
cb00ea35 CG |
110 | if (block_group >= nr_groups) { |
111 | udf_debug("block_group (%d) > nr_groups (%d)\n", block_group, | |
112 | nr_groups); | |
1da177e4 LT |
113 | } |
114 | ||
28de7948 | 115 | if (bitmap->s_block_bitmap[block_group]) { |
1da177e4 | 116 | return block_group; |
28de7948 CG |
117 | } else { |
118 | retval = read_block_bitmap(sb, bitmap, block_group, | |
119 | block_group); | |
1da177e4 LT |
120 | if (retval < 0) |
121 | return retval; | |
122 | return block_group; | |
123 | } | |
124 | } | |
125 | ||
cb00ea35 CG |
126 | static inline int load_block_bitmap(struct super_block *sb, |
127 | struct udf_bitmap *bitmap, | |
128 | unsigned int block_group) | |
1da177e4 LT |
129 | { |
130 | int slot; | |
131 | ||
132 | slot = __load_block_bitmap(sb, bitmap, block_group); | |
133 | ||
134 | if (slot < 0) | |
135 | return slot; | |
136 | ||
137 | if (!bitmap->s_block_bitmap[slot]) | |
138 | return -EIO; | |
139 | ||
140 | return slot; | |
141 | } | |
142 | ||
742ba02a MS |
143 | static bool udf_add_free_space(struct udf_sb_info *sbi, |
144 | u16 partition, u32 cnt) | |
145 | { | |
146 | struct logicalVolIntegrityDesc *lvid; | |
147 | ||
148 | if (sbi->s_lvid_bh) | |
149 | return false; | |
150 | ||
151 | lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data; | |
152 | lvid->freeSpaceTable[partition] = cpu_to_le32(le32_to_cpu( | |
153 | lvid->freeSpaceTable[partition]) + cnt); | |
154 | return true; | |
155 | } | |
156 | ||
cb00ea35 CG |
157 | static void udf_bitmap_free_blocks(struct super_block *sb, |
158 | struct inode *inode, | |
159 | struct udf_bitmap *bitmap, | |
160 | kernel_lb_addr bloc, uint32_t offset, | |
161 | uint32_t count) | |
1da177e4 LT |
162 | { |
163 | struct udf_sb_info *sbi = UDF_SB(sb); | |
cb00ea35 | 164 | struct buffer_head *bh = NULL; |
1da177e4 LT |
165 | unsigned long block; |
166 | unsigned long block_group; | |
167 | unsigned long bit; | |
168 | unsigned long i; | |
169 | int bitmap_nr; | |
170 | unsigned long overflow; | |
171 | ||
1e7933de | 172 | mutex_lock(&sbi->s_alloc_mutex); |
1da177e4 | 173 | if (bloc.logicalBlockNum < 0 || |
4b11111a MS |
174 | (bloc.logicalBlockNum + count) > |
175 | sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) { | |
28de7948 CG |
176 | udf_debug("%d < %d || %d + %d > %d\n", |
177 | bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count, | |
4b11111a MS |
178 | sbi->s_partmaps[bloc.partitionReferenceNum]. |
179 | s_partition_len); | |
1da177e4 LT |
180 | goto error_return; |
181 | } | |
182 | ||
4b11111a MS |
183 | block = bloc.logicalBlockNum + offset + |
184 | (sizeof(struct spaceBitmapDesc) << 3); | |
1da177e4 | 185 | |
4daa1b87 MS |
186 | do { |
187 | overflow = 0; | |
188 | block_group = block >> (sb->s_blocksize_bits + 3); | |
189 | bit = block % (sb->s_blocksize << 3); | |
190 | ||
191 | /* | |
192 | * Check to see if we are freeing blocks across a group boundary. | |
193 | */ | |
194 | if (bit + count > (sb->s_blocksize << 3)) { | |
195 | overflow = bit + count - (sb->s_blocksize << 3); | |
196 | count -= overflow; | |
1da177e4 | 197 | } |
4daa1b87 MS |
198 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
199 | if (bitmap_nr < 0) | |
200 | goto error_return; | |
201 | ||
202 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
203 | for (i = 0; i < count; i++) { | |
204 | if (udf_set_bit(bit + i, bh->b_data)) { | |
205 | udf_debug("bit %ld already set\n", bit + i); | |
206 | udf_debug("byte=%2x\n", | |
207 | ((char *)bh->b_data)[(bit + i) >> 3]); | |
208 | } else { | |
209 | if (inode) | |
210 | DQUOT_FREE_BLOCK(inode, 1); | |
211 | udf_add_free_space(sbi, sbi->s_partition, 1); | |
212 | } | |
213 | } | |
214 | mark_buffer_dirty(bh); | |
215 | if (overflow) { | |
216 | block += count; | |
217 | count = overflow; | |
218 | } | |
219 | } while (overflow); | |
220 | ||
28de7948 | 221 | error_return: |
1da177e4 | 222 | sb->s_dirt = 1; |
6c79e987 MS |
223 | if (sbi->s_lvid_bh) |
224 | mark_buffer_dirty(sbi->s_lvid_bh); | |
1e7933de | 225 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
226 | } |
227 | ||
cb00ea35 CG |
228 | static int udf_bitmap_prealloc_blocks(struct super_block *sb, |
229 | struct inode *inode, | |
230 | struct udf_bitmap *bitmap, | |
231 | uint16_t partition, uint32_t first_block, | |
232 | uint32_t block_count) | |
1da177e4 LT |
233 | { |
234 | struct udf_sb_info *sbi = UDF_SB(sb); | |
235 | int alloc_count = 0; | |
236 | int bit, block, block_group, group_start; | |
237 | int nr_groups, bitmap_nr; | |
238 | struct buffer_head *bh; | |
6c79e987 | 239 | __u32 part_len; |
1da177e4 | 240 | |
1e7933de | 241 | mutex_lock(&sbi->s_alloc_mutex); |
6c79e987 MS |
242 | part_len = sbi->s_partmaps[partition].s_partition_len; |
243 | if (first_block < 0 || first_block >= part_len) | |
1da177e4 LT |
244 | goto out; |
245 | ||
6c79e987 MS |
246 | if (first_block + block_count > part_len) |
247 | block_count = part_len - first_block; | |
1da177e4 | 248 | |
4daa1b87 MS |
249 | do { |
250 | nr_groups = udf_compute_nr_groups(sb, partition); | |
251 | block = first_block + (sizeof(struct spaceBitmapDesc) << 3); | |
252 | block_group = block >> (sb->s_blocksize_bits + 3); | |
253 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | |
1da177e4 | 254 | |
4daa1b87 MS |
255 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); |
256 | if (bitmap_nr < 0) | |
257 | goto out; | |
258 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
1da177e4 | 259 | |
4daa1b87 | 260 | bit = block % (sb->s_blocksize << 3); |
1da177e4 | 261 | |
4daa1b87 MS |
262 | while (bit < (sb->s_blocksize << 3) && block_count > 0) { |
263 | if (!udf_test_bit(bit, bh->b_data)) | |
264 | goto out; | |
265 | else if (DQUOT_PREALLOC_BLOCK(inode, 1)) | |
266 | goto out; | |
267 | else if (!udf_clear_bit(bit, bh->b_data)) { | |
268 | udf_debug("bit already cleared for block %d\n", bit); | |
269 | DQUOT_FREE_BLOCK(inode, 1); | |
270 | goto out; | |
271 | } | |
272 | block_count--; | |
273 | alloc_count++; | |
274 | bit++; | |
275 | block++; | |
1da177e4 | 276 | } |
4daa1b87 MS |
277 | mark_buffer_dirty(bh); |
278 | } while (block_count > 0); | |
279 | ||
28de7948 | 280 | out: |
742ba02a | 281 | if (udf_add_free_space(sbi, partition, -alloc_count)) |
6c79e987 | 282 | mark_buffer_dirty(sbi->s_lvid_bh); |
1da177e4 | 283 | sb->s_dirt = 1; |
1e7933de | 284 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
285 | return alloc_count; |
286 | } | |
287 | ||
cb00ea35 CG |
288 | static int udf_bitmap_new_block(struct super_block *sb, |
289 | struct inode *inode, | |
290 | struct udf_bitmap *bitmap, uint16_t partition, | |
291 | uint32_t goal, int *err) | |
1da177e4 LT |
292 | { |
293 | struct udf_sb_info *sbi = UDF_SB(sb); | |
cb00ea35 | 294 | int newbit, bit = 0, block, block_group, group_start; |
1da177e4 LT |
295 | int end_goal, nr_groups, bitmap_nr, i; |
296 | struct buffer_head *bh = NULL; | |
297 | char *ptr; | |
298 | int newblock = 0; | |
299 | ||
300 | *err = -ENOSPC; | |
1e7933de | 301 | mutex_lock(&sbi->s_alloc_mutex); |
1da177e4 | 302 | |
28de7948 | 303 | repeat: |
6c79e987 | 304 | if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
305 | goal = 0; |
306 | ||
307 | nr_groups = bitmap->s_nr_groups; | |
308 | block = goal + (sizeof(struct spaceBitmapDesc) << 3); | |
309 | block_group = block >> (sb->s_blocksize_bits + 3); | |
310 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | |
311 | ||
312 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | |
313 | if (bitmap_nr < 0) | |
314 | goto error_return; | |
315 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
28de7948 CG |
316 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
317 | sb->s_blocksize - group_start); | |
1da177e4 | 318 | |
cb00ea35 | 319 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
1da177e4 | 320 | bit = block % (sb->s_blocksize << 3); |
28de7948 | 321 | if (udf_test_bit(bit, bh->b_data)) |
1da177e4 | 322 | goto got_block; |
28de7948 | 323 | |
1da177e4 LT |
324 | end_goal = (bit + 63) & ~63; |
325 | bit = udf_find_next_one_bit(bh->b_data, end_goal, bit); | |
326 | if (bit < end_goal) | |
327 | goto got_block; | |
28de7948 | 328 | |
4b11111a MS |
329 | ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, |
330 | sb->s_blocksize - ((bit + 7) >> 3)); | |
1da177e4 | 331 | newbit = (ptr - ((char *)bh->b_data)) << 3; |
cb00ea35 | 332 | if (newbit < sb->s_blocksize << 3) { |
1da177e4 LT |
333 | bit = newbit; |
334 | goto search_back; | |
335 | } | |
28de7948 | 336 | |
4b11111a MS |
337 | newbit = udf_find_next_one_bit(bh->b_data, |
338 | sb->s_blocksize << 3, bit); | |
cb00ea35 | 339 | if (newbit < sb->s_blocksize << 3) { |
1da177e4 LT |
340 | bit = newbit; |
341 | goto got_block; | |
342 | } | |
343 | } | |
344 | ||
cb00ea35 CG |
345 | for (i = 0; i < (nr_groups * 2); i++) { |
346 | block_group++; | |
1da177e4 LT |
347 | if (block_group >= nr_groups) |
348 | block_group = 0; | |
349 | group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc); | |
350 | ||
351 | bitmap_nr = load_block_bitmap(sb, bitmap, block_group); | |
352 | if (bitmap_nr < 0) | |
353 | goto error_return; | |
354 | bh = bitmap->s_block_bitmap[bitmap_nr]; | |
cb00ea35 | 355 | if (i < nr_groups) { |
28de7948 CG |
356 | ptr = memscan((char *)bh->b_data + group_start, 0xFF, |
357 | sb->s_blocksize - group_start); | |
cb00ea35 | 358 | if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) { |
1da177e4 LT |
359 | bit = (ptr - ((char *)bh->b_data)) << 3; |
360 | break; | |
361 | } | |
cb00ea35 | 362 | } else { |
28de7948 CG |
363 | bit = udf_find_next_one_bit((char *)bh->b_data, |
364 | sb->s_blocksize << 3, | |
365 | group_start << 3); | |
1da177e4 LT |
366 | if (bit < sb->s_blocksize << 3) |
367 | break; | |
368 | } | |
369 | } | |
cb00ea35 | 370 | if (i >= (nr_groups * 2)) { |
1e7933de | 371 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
372 | return newblock; |
373 | } | |
374 | if (bit < sb->s_blocksize << 3) | |
375 | goto search_back; | |
376 | else | |
4b11111a MS |
377 | bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, |
378 | group_start << 3); | |
cb00ea35 | 379 | if (bit >= sb->s_blocksize << 3) { |
1e7933de | 380 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
381 | return 0; |
382 | } | |
383 | ||
28de7948 | 384 | search_back: |
4b11111a MS |
385 | i = 0; |
386 | while (i < 7 && bit > (group_start << 3) && | |
387 | udf_test_bit(bit - 1, bh->b_data)) { | |
388 | ++i; | |
389 | --bit; | |
390 | } | |
1da177e4 | 391 | |
28de7948 | 392 | got_block: |
1da177e4 LT |
393 | |
394 | /* | |
395 | * Check quota for allocation of this block. | |
396 | */ | |
cb00ea35 | 397 | if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) { |
1e7933de | 398 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
399 | *err = -EDQUOT; |
400 | return 0; | |
401 | } | |
402 | ||
403 | newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) - | |
28de7948 | 404 | (sizeof(struct spaceBitmapDesc) << 3); |
1da177e4 | 405 | |
cb00ea35 | 406 | if (!udf_clear_bit(bit, bh->b_data)) { |
1da177e4 LT |
407 | udf_debug("bit already cleared for block %d\n", bit); |
408 | goto repeat; | |
409 | } | |
410 | ||
411 | mark_buffer_dirty(bh); | |
412 | ||
742ba02a | 413 | if (udf_add_free_space(sbi, partition, -1)) |
6c79e987 | 414 | mark_buffer_dirty(sbi->s_lvid_bh); |
1da177e4 | 415 | sb->s_dirt = 1; |
1e7933de | 416 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
417 | *err = 0; |
418 | return newblock; | |
419 | ||
28de7948 | 420 | error_return: |
1da177e4 | 421 | *err = -EIO; |
1e7933de | 422 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
423 | return 0; |
424 | } | |
425 | ||
cb00ea35 CG |
426 | static void udf_table_free_blocks(struct super_block *sb, |
427 | struct inode *inode, | |
428 | struct inode *table, | |
429 | kernel_lb_addr bloc, uint32_t offset, | |
430 | uint32_t count) | |
1da177e4 LT |
431 | { |
432 | struct udf_sb_info *sbi = UDF_SB(sb); | |
433 | uint32_t start, end; | |
ff116fc8 JK |
434 | uint32_t elen; |
435 | kernel_lb_addr eloc; | |
436 | struct extent_position oepos, epos; | |
1da177e4 LT |
437 | int8_t etype; |
438 | int i; | |
439 | ||
1e7933de | 440 | mutex_lock(&sbi->s_alloc_mutex); |
1da177e4 | 441 | if (bloc.logicalBlockNum < 0 || |
4b11111a MS |
442 | (bloc.logicalBlockNum + count) > |
443 | sbi->s_partmaps[bloc.partitionReferenceNum].s_partition_len) { | |
28de7948 CG |
444 | udf_debug("%d < %d || %d + %d > %d\n", |
445 | bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count, | |
4b11111a MS |
446 | sbi->s_partmaps[bloc.partitionReferenceNum]. |
447 | s_partition_len); | |
1da177e4 LT |
448 | goto error_return; |
449 | } | |
450 | ||
4b11111a MS |
451 | /* We do this up front - There are some error conditions that |
452 | could occure, but.. oh well */ | |
1da177e4 LT |
453 | if (inode) |
454 | DQUOT_FREE_BLOCK(inode, count); | |
742ba02a | 455 | if (udf_add_free_space(sbi, sbi->s_partition, count)) |
6c79e987 | 456 | mark_buffer_dirty(sbi->s_lvid_bh); |
1da177e4 LT |
457 | |
458 | start = bloc.logicalBlockNum + offset; | |
459 | end = bloc.logicalBlockNum + offset + count - 1; | |
460 | ||
ff116fc8 | 461 | epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry); |
1da177e4 | 462 | elen = 0; |
c0b34438 | 463 | epos.block = oepos.block = UDF_I(table)->i_location; |
ff116fc8 | 464 | epos.bh = oepos.bh = NULL; |
1da177e4 | 465 | |
28de7948 CG |
466 | while (count && |
467 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
4b11111a MS |
468 | if (((eloc.logicalBlockNum + |
469 | (elen >> sb->s_blocksize_bits)) == start)) { | |
470 | if ((0x3FFFFFFF - elen) < | |
471 | (count << sb->s_blocksize_bits)) { | |
472 | uint32_t tmp = ((0x3FFFFFFF - elen) >> | |
473 | sb->s_blocksize_bits); | |
474 | count -= tmp; | |
475 | start += tmp; | |
476 | elen = (etype << 30) | | |
477 | (0x40000000 - sb->s_blocksize); | |
cb00ea35 | 478 | } else { |
4b11111a MS |
479 | elen = (etype << 30) | |
480 | (elen + | |
481 | (count << sb->s_blocksize_bits)); | |
1da177e4 LT |
482 | start += count; |
483 | count = 0; | |
484 | } | |
ff116fc8 | 485 | udf_write_aext(table, &oepos, eloc, elen, 1); |
cb00ea35 | 486 | } else if (eloc.logicalBlockNum == (end + 1)) { |
4b11111a MS |
487 | if ((0x3FFFFFFF - elen) < |
488 | (count << sb->s_blocksize_bits)) { | |
489 | uint32_t tmp = ((0x3FFFFFFF - elen) >> | |
490 | sb->s_blocksize_bits); | |
491 | count -= tmp; | |
492 | end -= tmp; | |
493 | eloc.logicalBlockNum -= tmp; | |
494 | elen = (etype << 30) | | |
495 | (0x40000000 - sb->s_blocksize); | |
cb00ea35 | 496 | } else { |
1da177e4 | 497 | eloc.logicalBlockNum = start; |
4b11111a MS |
498 | elen = (etype << 30) | |
499 | (elen + | |
500 | (count << sb->s_blocksize_bits)); | |
1da177e4 LT |
501 | end -= count; |
502 | count = 0; | |
503 | } | |
ff116fc8 | 504 | udf_write_aext(table, &oepos, eloc, elen, 1); |
1da177e4 LT |
505 | } |
506 | ||
cb00ea35 | 507 | if (epos.bh != oepos.bh) { |
1da177e4 | 508 | i = -1; |
ff116fc8 | 509 | oepos.block = epos.block; |
3bf25cb4 JK |
510 | brelse(oepos.bh); |
511 | get_bh(epos.bh); | |
ff116fc8 JK |
512 | oepos.bh = epos.bh; |
513 | oepos.offset = 0; | |
28de7948 | 514 | } else { |
ff116fc8 | 515 | oepos.offset = epos.offset; |
28de7948 | 516 | } |
1da177e4 LT |
517 | } |
518 | ||
cb00ea35 | 519 | if (count) { |
28de7948 | 520 | /* |
4b11111a MS |
521 | * NOTE: we CANNOT use udf_add_aext here, as it can try to |
522 | * allocate a new block, and since we hold the super block | |
523 | * lock already very bad things would happen :) | |
28de7948 CG |
524 | * |
525 | * We copy the behavior of udf_add_aext, but instead of | |
526 | * trying to allocate a new block close to the existing one, | |
527 | * we just steal a block from the extent we are trying to add. | |
528 | * | |
529 | * It would be nice if the blocks were close together, but it | |
530 | * isn't required. | |
cb00ea35 | 531 | */ |
1da177e4 LT |
532 | |
533 | int adsize; | |
534 | short_ad *sad = NULL; | |
535 | long_ad *lad = NULL; | |
536 | struct allocExtDesc *aed; | |
537 | ||
538 | eloc.logicalBlockNum = start; | |
28de7948 CG |
539 | elen = EXT_RECORDED_ALLOCATED | |
540 | (count << sb->s_blocksize_bits); | |
1da177e4 | 541 | |
c0b34438 | 542 | if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) { |
1da177e4 | 543 | adsize = sizeof(short_ad); |
c0b34438 | 544 | } else if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_LONG) { |
1da177e4 | 545 | adsize = sizeof(long_ad); |
28de7948 | 546 | } else { |
3bf25cb4 JK |
547 | brelse(oepos.bh); |
548 | brelse(epos.bh); | |
1da177e4 LT |
549 | goto error_return; |
550 | } | |
551 | ||
cb00ea35 | 552 | if (epos.offset + (2 * adsize) > sb->s_blocksize) { |
1da177e4 LT |
553 | char *sptr, *dptr; |
554 | int loffset; | |
cb00ea35 | 555 | |
3bf25cb4 | 556 | brelse(oepos.bh); |
ff116fc8 | 557 | oepos = epos; |
1da177e4 LT |
558 | |
559 | /* Steal a block from the extent being free'd */ | |
ff116fc8 | 560 | epos.block.logicalBlockNum = eloc.logicalBlockNum; |
cb00ea35 | 561 | eloc.logicalBlockNum++; |
1da177e4 LT |
562 | elen -= sb->s_blocksize; |
563 | ||
4b11111a MS |
564 | epos.bh = udf_tread(sb, |
565 | udf_get_lb_pblock(sb, epos.block, 0)); | |
566 | if (!epos.bh) { | |
3bf25cb4 | 567 | brelse(oepos.bh); |
1da177e4 LT |
568 | goto error_return; |
569 | } | |
ff116fc8 | 570 | aed = (struct allocExtDesc *)(epos.bh->b_data); |
4b11111a MS |
571 | aed->previousAllocExtLocation = |
572 | cpu_to_le32(oepos.block.logicalBlockNum); | |
cb00ea35 | 573 | if (epos.offset + adsize > sb->s_blocksize) { |
ff116fc8 | 574 | loffset = epos.offset; |
1da177e4 | 575 | aed->lengthAllocDescs = cpu_to_le32(adsize); |
c0b34438 MS |
576 | sptr = UDF_I(table)->i_ext.i_data + epos.offset |
577 | - adsize; | |
4b11111a MS |
578 | dptr = epos.bh->b_data + |
579 | sizeof(struct allocExtDesc); | |
1da177e4 | 580 | memcpy(dptr, sptr, adsize); |
4b11111a MS |
581 | epos.offset = sizeof(struct allocExtDesc) + |
582 | adsize; | |
cb00ea35 | 583 | } else { |
ff116fc8 | 584 | loffset = epos.offset + adsize; |
1da177e4 | 585 | aed->lengthAllocDescs = cpu_to_le32(0); |
cb00ea35 | 586 | if (oepos.bh) { |
f5cc15da | 587 | sptr = oepos.bh->b_data + epos.offset; |
4b11111a MS |
588 | aed = (struct allocExtDesc *) |
589 | oepos.bh->b_data; | |
1da177e4 | 590 | aed->lengthAllocDescs = |
4b11111a MS |
591 | cpu_to_le32(le32_to_cpu( |
592 | aed->lengthAllocDescs) + | |
593 | adsize); | |
cb00ea35 | 594 | } else { |
c0b34438 MS |
595 | sptr = UDF_I(table)->i_ext.i_data + |
596 | epos.offset; | |
597 | UDF_I(table)->i_lenAlloc += adsize; | |
1da177e4 LT |
598 | mark_inode_dirty(table); |
599 | } | |
f5cc15da | 600 | epos.offset = sizeof(struct allocExtDesc); |
1da177e4 | 601 | } |
6c79e987 | 602 | if (sbi->s_udfrev >= 0x0200) |
4b11111a MS |
603 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
604 | 3, 1, epos.block.logicalBlockNum, | |
605 | sizeof(tag)); | |
1da177e4 | 606 | else |
4b11111a MS |
607 | udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, |
608 | 2, 1, epos.block.logicalBlockNum, | |
609 | sizeof(tag)); | |
28de7948 | 610 | |
c0b34438 | 611 | switch (UDF_I(table)->i_alloc_type) { |
4b11111a MS |
612 | case ICBTAG_FLAG_AD_SHORT: |
613 | sad = (short_ad *)sptr; | |
614 | sad->extLength = cpu_to_le32( | |
615 | EXT_NEXT_EXTENT_ALLOCDECS | | |
616 | sb->s_blocksize); | |
617 | sad->extPosition = | |
618 | cpu_to_le32(epos.block.logicalBlockNum); | |
619 | break; | |
620 | case ICBTAG_FLAG_AD_LONG: | |
621 | lad = (long_ad *)sptr; | |
622 | lad->extLength = cpu_to_le32( | |
623 | EXT_NEXT_EXTENT_ALLOCDECS | | |
624 | sb->s_blocksize); | |
625 | lad->extLocation = | |
626 | cpu_to_lelb(epos.block); | |
627 | break; | |
1da177e4 | 628 | } |
cb00ea35 | 629 | if (oepos.bh) { |
ff116fc8 JK |
630 | udf_update_tag(oepos.bh->b_data, loffset); |
631 | mark_buffer_dirty(oepos.bh); | |
28de7948 | 632 | } else { |
1da177e4 | 633 | mark_inode_dirty(table); |
28de7948 | 634 | } |
1da177e4 LT |
635 | } |
636 | ||
4b11111a MS |
637 | /* It's possible that stealing the block emptied the extent */ |
638 | if (elen) { | |
ff116fc8 | 639 | udf_write_aext(table, &epos, eloc, elen, 1); |
1da177e4 | 640 | |
cb00ea35 | 641 | if (!epos.bh) { |
c0b34438 | 642 | UDF_I(table)->i_lenAlloc += adsize; |
1da177e4 | 643 | mark_inode_dirty(table); |
cb00ea35 | 644 | } else { |
ff116fc8 | 645 | aed = (struct allocExtDesc *)epos.bh->b_data; |
1da177e4 | 646 | aed->lengthAllocDescs = |
4b11111a MS |
647 | cpu_to_le32(le32_to_cpu( |
648 | aed->lengthAllocDescs) + adsize); | |
ff116fc8 JK |
649 | udf_update_tag(epos.bh->b_data, epos.offset); |
650 | mark_buffer_dirty(epos.bh); | |
1da177e4 LT |
651 | } |
652 | } | |
653 | } | |
654 | ||
3bf25cb4 JK |
655 | brelse(epos.bh); |
656 | brelse(oepos.bh); | |
1da177e4 | 657 | |
28de7948 | 658 | error_return: |
1da177e4 | 659 | sb->s_dirt = 1; |
1e7933de | 660 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
661 | return; |
662 | } | |
663 | ||
cb00ea35 CG |
664 | static int udf_table_prealloc_blocks(struct super_block *sb, |
665 | struct inode *inode, | |
666 | struct inode *table, uint16_t partition, | |
667 | uint32_t first_block, uint32_t block_count) | |
1da177e4 LT |
668 | { |
669 | struct udf_sb_info *sbi = UDF_SB(sb); | |
670 | int alloc_count = 0; | |
ff116fc8 JK |
671 | uint32_t elen, adsize; |
672 | kernel_lb_addr eloc; | |
673 | struct extent_position epos; | |
1da177e4 LT |
674 | int8_t etype = -1; |
675 | ||
4b11111a MS |
676 | if (first_block < 0 || |
677 | first_block >= sbi->s_partmaps[partition].s_partition_len) | |
1da177e4 LT |
678 | return 0; |
679 | ||
c0b34438 | 680 | if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
1da177e4 | 681 | adsize = sizeof(short_ad); |
c0b34438 | 682 | else if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
1da177e4 LT |
683 | adsize = sizeof(long_ad); |
684 | else | |
685 | return 0; | |
686 | ||
1e7933de | 687 | mutex_lock(&sbi->s_alloc_mutex); |
ff116fc8 | 688 | epos.offset = sizeof(struct unallocSpaceEntry); |
c0b34438 | 689 | epos.block = UDF_I(table)->i_location; |
ff116fc8 | 690 | epos.bh = NULL; |
1da177e4 LT |
691 | eloc.logicalBlockNum = 0xFFFFFFFF; |
692 | ||
28de7948 CG |
693 | while (first_block != eloc.logicalBlockNum && |
694 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
1da177e4 | 695 | udf_debug("eloc=%d, elen=%d, first_block=%d\n", |
cb00ea35 | 696 | eloc.logicalBlockNum, elen, first_block); |
28de7948 | 697 | ; /* empty loop body */ |
1da177e4 LT |
698 | } |
699 | ||
cb00ea35 | 700 | if (first_block == eloc.logicalBlockNum) { |
ff116fc8 | 701 | epos.offset -= adsize; |
1da177e4 LT |
702 | |
703 | alloc_count = (elen >> sb->s_blocksize_bits); | |
4b11111a MS |
704 | if (inode && DQUOT_PREALLOC_BLOCK(inode, |
705 | alloc_count > block_count ? block_count : alloc_count)) | |
1da177e4 | 706 | alloc_count = 0; |
4b11111a | 707 | else if (alloc_count > block_count) { |
1da177e4 LT |
708 | alloc_count = block_count; |
709 | eloc.logicalBlockNum += alloc_count; | |
710 | elen -= (alloc_count << sb->s_blocksize_bits); | |
4b11111a MS |
711 | udf_write_aext(table, &epos, eloc, |
712 | (etype << 30) | elen, 1); | |
713 | } else | |
714 | udf_delete_aext(table, epos, eloc, | |
715 | (etype << 30) | elen); | |
28de7948 | 716 | } else { |
1da177e4 | 717 | alloc_count = 0; |
28de7948 | 718 | } |
1da177e4 | 719 | |
3bf25cb4 | 720 | brelse(epos.bh); |
1da177e4 | 721 | |
742ba02a | 722 | if (alloc_count && udf_add_free_space(sbi, partition, -alloc_count)) { |
6c79e987 | 723 | mark_buffer_dirty(sbi->s_lvid_bh); |
1da177e4 LT |
724 | sb->s_dirt = 1; |
725 | } | |
1e7933de | 726 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
727 | return alloc_count; |
728 | } | |
729 | ||
cb00ea35 CG |
730 | static int udf_table_new_block(struct super_block *sb, |
731 | struct inode *inode, | |
732 | struct inode *table, uint16_t partition, | |
733 | uint32_t goal, int *err) | |
1da177e4 LT |
734 | { |
735 | struct udf_sb_info *sbi = UDF_SB(sb); | |
736 | uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF; | |
737 | uint32_t newblock = 0, adsize; | |
ff116fc8 | 738 | uint32_t elen, goal_elen = 0; |
3ad90ec0 | 739 | kernel_lb_addr eloc, uninitialized_var(goal_eloc); |
ff116fc8 | 740 | struct extent_position epos, goal_epos; |
1da177e4 LT |
741 | int8_t etype; |
742 | ||
743 | *err = -ENOSPC; | |
744 | ||
c0b34438 | 745 | if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
1da177e4 | 746 | adsize = sizeof(short_ad); |
c0b34438 | 747 | else if (UDF_I(table)->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
1da177e4 LT |
748 | adsize = sizeof(long_ad); |
749 | else | |
750 | return newblock; | |
751 | ||
1e7933de | 752 | mutex_lock(&sbi->s_alloc_mutex); |
6c79e987 | 753 | if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len) |
1da177e4 LT |
754 | goal = 0; |
755 | ||
4b11111a MS |
756 | /* We search for the closest matching block to goal. If we find |
757 | a exact hit, we stop. Otherwise we keep going till we run out | |
758 | of extents. We store the buffer_head, bloc, and extoffset | |
759 | of the current closest match and use that when we are done. | |
cb00ea35 | 760 | */ |
ff116fc8 | 761 | epos.offset = sizeof(struct unallocSpaceEntry); |
c0b34438 | 762 | epos.block = UDF_I(table)->i_location; |
ff116fc8 | 763 | epos.bh = goal_epos.bh = NULL; |
1da177e4 | 764 | |
28de7948 CG |
765 | while (spread && |
766 | (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) { | |
cb00ea35 | 767 | if (goal >= eloc.logicalBlockNum) { |
4b11111a MS |
768 | if (goal < eloc.logicalBlockNum + |
769 | (elen >> sb->s_blocksize_bits)) | |
1da177e4 LT |
770 | nspread = 0; |
771 | else | |
772 | nspread = goal - eloc.logicalBlockNum - | |
28de7948 CG |
773 | (elen >> sb->s_blocksize_bits); |
774 | } else { | |
1da177e4 | 775 | nspread = eloc.logicalBlockNum - goal; |
28de7948 | 776 | } |
1da177e4 | 777 | |
cb00ea35 | 778 | if (nspread < spread) { |
1da177e4 | 779 | spread = nspread; |
cb00ea35 | 780 | if (goal_epos.bh != epos.bh) { |
3bf25cb4 | 781 | brelse(goal_epos.bh); |
ff116fc8 | 782 | goal_epos.bh = epos.bh; |
3bf25cb4 | 783 | get_bh(goal_epos.bh); |
1da177e4 | 784 | } |
ff116fc8 JK |
785 | goal_epos.block = epos.block; |
786 | goal_epos.offset = epos.offset - adsize; | |
1da177e4 LT |
787 | goal_eloc = eloc; |
788 | goal_elen = (etype << 30) | elen; | |
789 | } | |
790 | } | |
791 | ||
3bf25cb4 | 792 | brelse(epos.bh); |
1da177e4 | 793 | |
cb00ea35 | 794 | if (spread == 0xFFFFFFFF) { |
3bf25cb4 | 795 | brelse(goal_epos.bh); |
1e7933de | 796 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
797 | return 0; |
798 | } | |
799 | ||
800 | /* Only allocate blocks from the beginning of the extent. | |
801 | That way, we only delete (empty) extents, never have to insert an | |
802 | extent because of splitting */ | |
803 | /* This works, but very poorly.... */ | |
804 | ||
805 | newblock = goal_eloc.logicalBlockNum; | |
cb00ea35 | 806 | goal_eloc.logicalBlockNum++; |
1da177e4 LT |
807 | goal_elen -= sb->s_blocksize; |
808 | ||
cb00ea35 | 809 | if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) { |
3bf25cb4 | 810 | brelse(goal_epos.bh); |
1e7933de | 811 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
812 | *err = -EDQUOT; |
813 | return 0; | |
814 | } | |
815 | ||
816 | if (goal_elen) | |
ff116fc8 | 817 | udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1); |
1da177e4 | 818 | else |
ff116fc8 | 819 | udf_delete_aext(table, goal_epos, goal_eloc, goal_elen); |
3bf25cb4 | 820 | brelse(goal_epos.bh); |
1da177e4 | 821 | |
742ba02a | 822 | if (udf_add_free_space(sbi, partition, -1)) |
6c79e987 | 823 | mark_buffer_dirty(sbi->s_lvid_bh); |
1da177e4 LT |
824 | |
825 | sb->s_dirt = 1; | |
1e7933de | 826 | mutex_unlock(&sbi->s_alloc_mutex); |
1da177e4 LT |
827 | *err = 0; |
828 | return newblock; | |
829 | } | |
830 | ||
cb00ea35 CG |
831 | inline void udf_free_blocks(struct super_block *sb, |
832 | struct inode *inode, | |
833 | kernel_lb_addr bloc, uint32_t offset, | |
834 | uint32_t count) | |
1da177e4 LT |
835 | { |
836 | uint16_t partition = bloc.partitionReferenceNum; | |
6c79e987 | 837 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
1da177e4 | 838 | |
6c79e987 | 839 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { |
1da177e4 | 840 | return udf_bitmap_free_blocks(sb, inode, |
6c79e987 | 841 | map->s_uspace.s_bitmap, |
28de7948 | 842 | bloc, offset, count); |
6c79e987 | 843 | } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { |
1da177e4 | 844 | return udf_table_free_blocks(sb, inode, |
6c79e987 | 845 | map->s_uspace.s_table, |
28de7948 | 846 | bloc, offset, count); |
6c79e987 | 847 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) { |
1da177e4 | 848 | return udf_bitmap_free_blocks(sb, inode, |
6c79e987 | 849 | map->s_fspace.s_bitmap, |
28de7948 | 850 | bloc, offset, count); |
6c79e987 | 851 | } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) { |
1da177e4 | 852 | return udf_table_free_blocks(sb, inode, |
6c79e987 | 853 | map->s_fspace.s_table, |
28de7948 CG |
854 | bloc, offset, count); |
855 | } else { | |
1da177e4 | 856 | return; |
28de7948 | 857 | } |
1da177e4 LT |
858 | } |
859 | ||
cb00ea35 CG |
860 | inline int udf_prealloc_blocks(struct super_block *sb, |
861 | struct inode *inode, | |
862 | uint16_t partition, uint32_t first_block, | |
863 | uint32_t block_count) | |
1da177e4 | 864 | { |
6c79e987 MS |
865 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
866 | ||
4b11111a | 867 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
1da177e4 | 868 | return udf_bitmap_prealloc_blocks(sb, inode, |
6c79e987 | 869 | map->s_uspace.s_bitmap, |
4b11111a MS |
870 | partition, first_block, |
871 | block_count); | |
872 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) | |
1da177e4 | 873 | return udf_table_prealloc_blocks(sb, inode, |
6c79e987 | 874 | map->s_uspace.s_table, |
4b11111a MS |
875 | partition, first_block, |
876 | block_count); | |
877 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) | |
1da177e4 | 878 | return udf_bitmap_prealloc_blocks(sb, inode, |
6c79e987 | 879 | map->s_fspace.s_bitmap, |
4b11111a MS |
880 | partition, first_block, |
881 | block_count); | |
882 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) | |
1da177e4 | 883 | return udf_table_prealloc_blocks(sb, inode, |
6c79e987 | 884 | map->s_fspace.s_table, |
4b11111a MS |
885 | partition, first_block, |
886 | block_count); | |
887 | else | |
1da177e4 LT |
888 | return 0; |
889 | } | |
890 | ||
cb00ea35 CG |
891 | inline int udf_new_block(struct super_block *sb, |
892 | struct inode *inode, | |
893 | uint16_t partition, uint32_t goal, int *err) | |
1da177e4 | 894 | { |
6c79e987 | 895 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
3bf25cb4 | 896 | |
4b11111a MS |
897 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
898 | return udf_bitmap_new_block(sb, inode, | |
6c79e987 | 899 | map->s_uspace.s_bitmap, |
28de7948 | 900 | partition, goal, err); |
4b11111a | 901 | else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
1da177e4 | 902 | return udf_table_new_block(sb, inode, |
6c79e987 | 903 | map->s_uspace.s_table, |
28de7948 | 904 | partition, goal, err); |
4b11111a | 905 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) |
1da177e4 | 906 | return udf_bitmap_new_block(sb, inode, |
6c79e987 | 907 | map->s_fspace.s_bitmap, |
28de7948 | 908 | partition, goal, err); |
4b11111a | 909 | else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) |
1da177e4 | 910 | return udf_table_new_block(sb, inode, |
6c79e987 | 911 | map->s_fspace.s_table, |
28de7948 | 912 | partition, goal, err); |
4b11111a | 913 | else { |
1da177e4 LT |
914 | *err = -EIO; |
915 | return 0; | |
916 | } | |
917 | } |