- pre5:
[davej-history.git] / fs / udf / balloc.c
blob8d7fdfdee8c431080614dc54071677cc71f0f5fd
1 /*
2 * balloc.c
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
7 * CONTACTS
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hootie.lvld.hp.com
12 * COPYRIGHT
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1999-2000 Ben Fennema
19 * (C) 1999 Stelias Computing Inc
21 * HISTORY
23 * 02/24/99 blf Created.
27 #include "udfdecl.h"
28 #include <linux/fs.h>
29 #include <linux/locks.h>
30 #include <linux/quotaops.h>
31 #include <linux/udf_fs.h>
33 #include <asm/bitops.h>
35 #include "udf_i.h"
36 #include "udf_sb.h"
38 #define udf_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
39 #define udf_set_bit(nr,addr) ext2_set_bit(nr,addr)
40 #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
41 #define udf_find_first_one_bit(addr, size) find_first_one_bit(addr, size)
42 #define udf_find_next_one_bit(addr, size, offset) find_next_one_bit(addr, size, offset)
44 #define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
45 #define leNUM_to_cpup(x,y) xleNUM_to_cpup(x,y)
46 #define xleNUM_to_cpup(x,y) (le ## x ## _to_cpup(y))
48 extern inline int find_next_one_bit (void * addr, int size, int offset)
50 unsigned long * p = ((unsigned long *) addr) + (offset / BITS_PER_LONG);
51 unsigned long result = offset & ~(BITS_PER_LONG-1);
52 unsigned long tmp;
54 if (offset >= size)
55 return size;
56 size -= result;
57 offset &= (BITS_PER_LONG-1);
58 if (offset)
60 tmp = leBPL_to_cpup(p++);
61 tmp &= ~0UL << offset;
62 if (size < BITS_PER_LONG)
63 goto found_first;
64 if (tmp)
65 goto found_middle;
66 size -= BITS_PER_LONG;
67 result += BITS_PER_LONG;
69 while (size & ~(BITS_PER_LONG-1))
71 if ((tmp = leBPL_to_cpup(p++)))
72 goto found_middle;
73 result += BITS_PER_LONG;
74 size -= BITS_PER_LONG;
76 if (!size)
77 return result;
78 tmp = leBPL_to_cpup(p);
79 found_first:
80 tmp &= ~0UL >> (BITS_PER_LONG-size);
81 found_middle:
82 return result + ffz(~tmp);
85 #define find_first_one_bit(addr, size)\
86 find_next_one_bit((addr), (size), 0)
88 static int read_block_bitmap(struct super_block * sb, Uint32 bitmap,
89 unsigned int block, unsigned long bitmap_nr)
91 struct buffer_head *bh = NULL;
92 int retval = 0;
93 lb_addr loc;
95 loc.logicalBlockNum = bitmap;
96 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
98 bh = udf_tread(sb, udf_get_lb_pblock(sb, loc, block), sb->s_blocksize);
99 if (!bh)
101 retval = -EIO;
103 UDF_SB_BLOCK_BITMAP_NUMBER(sb, bitmap_nr) = block;
104 UDF_SB_BLOCK_BITMAP(sb, bitmap_nr) = bh;
105 return retval;
108 static int __load_block_bitmap(struct super_block * sb, Uint32 bitmap,
109 unsigned int block_group)
111 int i, j, retval = 0;
112 unsigned long block_bitmap_number;
113 struct buffer_head * block_bitmap = NULL;
114 int nr_groups = (UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb)) +
115 (sizeof(struct SpaceBitmapDesc) << 3) + (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
117 if (block_group >= nr_groups)
119 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group, nr_groups);
122 if (nr_groups <= UDF_MAX_BLOCK_LOADED)
124 if (UDF_SB_BLOCK_BITMAP(sb, block_group))
126 if (UDF_SB_BLOCK_BITMAP_NUMBER(sb, block_group) == block_group)
127 return block_group;
129 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
130 if (retval < 0)
131 return retval;
132 return block_group;
135 for (i=0; i<UDF_SB_LOADED_BLOCK_BITMAPS(sb) &&
136 UDF_SB_BLOCK_BITMAP_NUMBER(sb, i) != block_group; i++)
140 if (i < UDF_SB_LOADED_BLOCK_BITMAPS(sb) &&
141 UDF_SB_BLOCK_BITMAP_NUMBER(sb, i) == block_group)
143 block_bitmap_number = UDF_SB_BLOCK_BITMAP_NUMBER(sb, i);
144 block_bitmap = UDF_SB_BLOCK_BITMAP(sb, i);
145 for (j=i; j>0; j--)
147 UDF_SB_BLOCK_BITMAP_NUMBER(sb, j) = UDF_SB_BLOCK_BITMAP_NUMBER(sb, j-1);
148 UDF_SB_BLOCK_BITMAP(sb, j) = UDF_SB_BLOCK_BITMAP(sb, j-1);
150 UDF_SB_BLOCK_BITMAP_NUMBER(sb, 0) = block_bitmap_number;
151 UDF_SB_BLOCK_BITMAP(sb, 0) = block_bitmap;
153 if (!block_bitmap)
154 retval = read_block_bitmap(sb, bitmap, block_group, 0);
156 else
158 if (UDF_SB_LOADED_BLOCK_BITMAPS(sb) < UDF_MAX_BLOCK_LOADED)
159 UDF_SB_LOADED_BLOCK_BITMAPS(sb) ++;
160 else
161 brelse(UDF_SB_BLOCK_BITMAP(sb, UDF_MAX_BLOCK_LOADED-1));
162 for (j=UDF_SB_LOADED_BLOCK_BITMAPS(sb)-1; j>0; j--)
164 UDF_SB_BLOCK_BITMAP_NUMBER(sb, j) = UDF_SB_BLOCK_BITMAP_NUMBER(sb, j-1);
165 UDF_SB_BLOCK_BITMAP(sb, j) = UDF_SB_BLOCK_BITMAP(sb, j-1);
167 retval = read_block_bitmap(sb, bitmap, block_group, 0);
169 return retval;
172 static inline int load_block_bitmap(struct super_block *sb, Uint32 bitmap,
173 unsigned int block_group)
175 int slot;
176 int nr_groups = (UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb)) +
177 (sizeof(struct SpaceBitmapDesc) << 3) + (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
179 if (UDF_SB_LOADED_BLOCK_BITMAPS(sb) > 0 &&
180 UDF_SB_BLOCK_BITMAP_NUMBER(sb, 0) == block_group &&
181 UDF_SB_BLOCK_BITMAP(sb, block_group))
183 return 0;
185 else if (nr_groups <= UDF_MAX_BLOCK_LOADED &&
186 UDF_SB_BLOCK_BITMAP_NUMBER(sb, block_group) == block_group &&
187 UDF_SB_BLOCK_BITMAP(sb, block_group))
189 slot = block_group;
191 else
193 slot = __load_block_bitmap(sb, bitmap, block_group);
196 if (slot < 0)
197 return slot;
199 if (!UDF_SB_BLOCK_BITMAP(sb, slot))
200 return -EIO;
202 return slot;
205 static void udf_bitmap_free_blocks(const struct inode * inode, Uint32 bitmap,
206 lb_addr bloc, Uint32 offset, Uint32 count)
208 struct buffer_head * bh = NULL;
209 unsigned long block;
210 unsigned long block_group;
211 unsigned long bit;
212 unsigned long i;
213 int bitmap_nr;
214 unsigned long overflow;
215 struct super_block * sb;
217 sb = inode->i_sb;
218 if (!sb)
220 udf_debug("nonexistent device");
221 return;
224 lock_super(sb);
225 if (bloc.logicalBlockNum < 0 ||
226 (bloc.logicalBlockNum + count) > UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum))
228 udf_debug("%d < %d || %d + %d > %d\n",
229 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
230 UDF_SB_PARTLEN(sb, bloc.partitionReferenceNum));
231 goto error_return;
234 block = bloc.logicalBlockNum + offset + (sizeof(struct SpaceBitmapDesc) << 3);
236 do_more:
237 overflow = 0;
238 block_group = block >> (sb->s_blocksize_bits + 3);
239 bit = block % (sb->s_blocksize << 3);
242 * Check to see if we are freeing blocks across a group boundary.
244 if (bit + count > (sb->s_blocksize << 3))
246 overflow = bit + count - (sb->s_blocksize << 3);
247 count -= overflow;
249 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
250 if (bitmap_nr < 0)
251 goto error_return;
253 bh = UDF_SB_BLOCK_BITMAP(sb, bitmap_nr);
254 for (i=0; i < count; i++)
256 if (udf_set_bit(bit + i, bh->b_data))
258 udf_debug("bit %ld already set\n", bit + i);
259 udf_debug("byte=%2x\n", ((char *)bh->b_data)[(bit + i) >> 3]);
261 else
263 DQUOT_FREE_BLOCK(sb, inode, 1);
264 if (UDF_SB_LVIDBH(sb))
266 UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)] =
267 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)])+1);
271 mark_buffer_dirty(bh);
272 if (overflow)
274 block += count;
275 count = overflow;
276 goto do_more;
278 error_return:
279 sb->s_dirt = 1;
280 if (UDF_SB_LVIDBH(sb))
281 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
282 unlock_super(sb);
283 return;
286 static int udf_bitmap_prealloc_blocks(const struct inode * inode, Uint32 bitmap,
287 Uint16 partition, Uint32 first_block, Uint32 block_count)
289 int alloc_count = 0;
290 int bit, block, block_group, group_start;
291 int nr_groups, bitmap_nr;
292 struct buffer_head *bh;
293 struct super_block *sb;
295 sb = inode->i_sb;
296 if (!sb)
298 udf_debug("nonexistent device\n");
299 return 0;
301 lock_super(sb);
303 if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
304 goto out;
306 repeat:
307 nr_groups = (UDF_SB_PARTLEN(sb, partition) +
308 (sizeof(struct SpaceBitmapDesc) << 3) + (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
309 block = first_block + (sizeof(struct SpaceBitmapDesc) << 3);
310 block_group = block >> (sb->s_blocksize_bits + 3);
311 group_start = block_group ? 0 : sizeof(struct SpaceBitmapDesc);
313 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
314 if (bitmap_nr < 0)
315 goto out;
316 bh = UDF_SB_BLOCK_BITMAP(sb, bitmap_nr);
318 bit = block % (sb->s_blocksize << 3);
320 while (bit < (sb->s_blocksize << 3) && block_count > 0)
322 if (!udf_test_bit(bit, bh->b_data))
323 goto out;
324 else if (DQUOT_PREALLOC_BLOCK(sb, inode, 1))
325 goto out;
326 else if (!udf_clear_bit(bit, bh->b_data))
328 udf_debug("bit already cleared for block %d\n", bit);
329 DQUOT_FREE_BLOCK(sb, inode, 1);
330 goto out;
332 block_count --;
333 alloc_count ++;
334 bit ++;
335 block ++;
337 mark_buffer_dirty(bh);
338 if (block_count > 0)
339 goto repeat;
340 out:
341 if (UDF_SB_LVIDBH(sb))
343 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
344 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-alloc_count);
345 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
347 sb->s_dirt = 1;
348 unlock_super(sb);
349 return alloc_count;
352 static int udf_bitmap_new_block(const struct inode * inode, Uint32 bitmap,
353 Uint16 partition, Uint32 goal, int *err)
355 int tmp, newbit, bit=0, block, block_group, group_start;
356 int end_goal, nr_groups, bitmap_nr, i;
357 struct buffer_head *bh = NULL;
358 struct super_block *sb;
359 char *ptr;
360 int newblock = 0;
362 *err = -ENOSPC;
363 sb = inode->i_sb;
364 if (!sb)
366 udf_debug("nonexistent device\n");
367 return newblock;
369 lock_super(sb);
371 repeat:
372 if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
373 goal = 0;
375 nr_groups = (UDF_SB_PARTLEN(sb, partition) +
376 (sizeof(struct SpaceBitmapDesc) << 3) + (sb->s_blocksize * 8) - 1) / (sb->s_blocksize * 8);
377 block = goal + (sizeof(struct SpaceBitmapDesc) << 3);
378 block_group = block >> (sb->s_blocksize_bits + 3);
379 group_start = block_group ? 0 : sizeof(struct SpaceBitmapDesc);
381 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
382 if (bitmap_nr < 0)
383 goto error_return;
384 bh = UDF_SB_BLOCK_BITMAP(sb, bitmap_nr);
385 ptr = memscan((char *)bh->b_data + group_start, 0xFF, sb->s_blocksize - group_start);
387 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize)
389 bit = block % (sb->s_blocksize << 3);
391 if (udf_test_bit(bit, bh->b_data))
393 goto got_block;
395 end_goal = (bit + 63) & ~63;
396 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
397 if (bit < end_goal)
398 goto got_block;
399 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF, sb->s_blocksize - ((bit + 7) >> 3));
400 newbit = (ptr - ((char *)bh->b_data)) << 3;
401 if (newbit < sb->s_blocksize << 3)
403 bit = newbit;
404 goto search_back;
406 newbit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, bit);
407 if (newbit < sb->s_blocksize << 3)
409 bit = newbit;
410 goto got_block;
414 for (i=0; i<(nr_groups*2); i++)
416 block_group ++;
417 if (block_group >= nr_groups)
418 block_group = 0;
419 group_start = block_group ? 0 : sizeof(struct SpaceBitmapDesc);
421 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
422 if (bitmap_nr < 0)
423 goto error_return;
424 bh = UDF_SB_BLOCK_BITMAP(sb, bitmap_nr);
425 if (i < nr_groups)
427 ptr = memscan((char *)bh->b_data + group_start, 0xFF, sb->s_blocksize - group_start);
428 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize)
430 bit = (ptr - ((char *)bh->b_data)) << 3;
431 break;
434 else
436 bit = udf_find_next_one_bit((char *)bh->b_data, sb->s_blocksize << 3, group_start << 3);
437 if (bit < sb->s_blocksize << 3)
438 break;
441 if (i >= (nr_groups*2))
443 unlock_super(sb);
444 return newblock;
446 if (bit < sb->s_blocksize << 3)
447 goto search_back;
448 else
449 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3, group_start << 3);
450 if (bit >= sb->s_blocksize << 3)
452 unlock_super(sb);
453 return 0;
456 search_back:
457 for (i=0; i<7 && bit > (group_start << 3) && udf_test_bit(bit - 1, bh->b_data); i++, bit--);
459 got_block:
462 * Check quota for allocation of this block.
464 if (DQUOT_ALLOC_BLOCK(sb, inode, 1))
466 unlock_super(sb);
467 *err = -EDQUOT;
468 return 0;
471 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
472 (sizeof(struct SpaceBitmapDesc) << 3);
474 tmp = udf_get_pblock(sb, newblock, partition, 0);
475 if (!udf_clear_bit(bit, bh->b_data))
477 udf_debug("bit already cleared for block %d\n", bit);
478 goto repeat;
481 mark_buffer_dirty(bh);
483 if (UDF_SB_LVIDBH(sb))
485 UDF_SB_LVID(sb)->freeSpaceTable[partition] =
486 cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition])-1);
487 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
489 sb->s_dirt = 1;
490 unlock_super(sb);
491 *err = 0;
492 return newblock;
494 error_return:
495 *err = -EIO;
496 unlock_super(sb);
497 return 0;
500 inline void udf_free_blocks(const struct inode * inode, lb_addr bloc,
501 Uint32 offset, Uint32 count)
503 if (UDF_SB_PARTFLAGS(inode->i_sb, bloc.partitionReferenceNum) & UDF_PART_FLAG_UNALLOC_BITMAP)
505 return udf_bitmap_free_blocks(inode,
506 UDF_SB_PARTMAPS(inode->i_sb)[bloc.partitionReferenceNum].s_uspace.bitmap,
507 bloc, offset, count);
509 else if (UDF_SB_PARTFLAGS(inode->i_sb, bloc.partitionReferenceNum) & UDF_PART_FLAG_FREED_BITMAP)
511 return udf_bitmap_free_blocks(inode,
512 UDF_SB_PARTMAPS(inode->i_sb)[bloc.partitionReferenceNum].s_fspace.bitmap,
513 bloc, offset, count);
515 else
516 return;
519 inline int udf_prealloc_blocks(const struct inode * inode, Uint16 partition,
520 Uint32 first_block, Uint32 block_count)
522 if (UDF_SB_PARTFLAGS(inode->i_sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
524 return udf_bitmap_prealloc_blocks(inode,
525 UDF_SB_PARTMAPS(inode->i_sb)[partition].s_uspace.bitmap,
526 partition, first_block, block_count);
528 else if (UDF_SB_PARTFLAGS(inode->i_sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
530 return udf_bitmap_prealloc_blocks(inode,
531 UDF_SB_PARTMAPS(inode->i_sb)[partition].s_fspace.bitmap,
532 partition, first_block, block_count);
534 else
535 return 0;
538 inline int udf_new_block(const struct inode * inode, Uint16 partition,
539 Uint32 goal, int *err)
541 if (UDF_SB_PARTFLAGS(inode->i_sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP)
543 return udf_bitmap_new_block(inode,
544 UDF_SB_PARTMAPS(inode->i_sb)[partition].s_uspace.bitmap,
545 partition, goal, err);
547 else if (UDF_SB_PARTFLAGS(inode->i_sb, partition) & UDF_PART_FLAG_FREED_BITMAP)
549 return udf_bitmap_new_block(inode,
550 UDF_SB_PARTMAPS(inode->i_sb)[partition].s_fspace.bitmap,
551 partition, goal, err);
553 else
555 *err = -EIO;
556 return 0;