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