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