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