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