blob: 58be702cb42d5ccd5621c3b435feac31b483eef3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * balloc.c
3 *
4 * PURPOSE
5 * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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
Marcin Slusarz4b111112008-02-08 04:20:36 -080031#define udf_clear_bit(nr, addr) ext2_clear_bit(nr, addr)
32#define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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)
Marcin Slusarz4b111112008-02-08 04:20:36 -080035#define udf_find_next_one_bit(addr, size, offset) \
36 find_next_one_bit(addr, size, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define leBPL_to_cpup(x) leNUM_to_cpup(BITS_PER_LONG, x)
Marcin Slusarz4b111112008-02-08 04:20:36 -080039#define leNUM_to_cpup(x, y) xleNUM_to_cpup(x, y)
40#define xleNUM_to_cpup(x, y) (le ## x ## _to_cpup(y))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define uintBPL_t uint(BITS_PER_LONG)
42#define uint(x) xuint(x)
43#define xuint(x) __le ## x
44
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070045static inline int find_next_one_bit(void *addr, int size, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070047 uintBPL_t *p = ((uintBPL_t *) addr) + (offset / BITS_PER_LONG);
48 int result = offset & ~(BITS_PER_LONG - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned long tmp;
50
51 if (offset >= size)
52 return size;
53 size -= result;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054 offset &= (BITS_PER_LONG - 1);
55 if (offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 tmp = leBPL_to_cpup(p++);
57 tmp &= ~0UL << offset;
58 if (size < BITS_PER_LONG)
59 goto found_first;
60 if (tmp)
61 goto found_middle;
62 size -= BITS_PER_LONG;
63 result += BITS_PER_LONG;
64 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 while (size & ~(BITS_PER_LONG - 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -080066 tmp = leBPL_to_cpup(p++);
67 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 goto found_middle;
69 result += BITS_PER_LONG;
70 size -= BITS_PER_LONG;
71 }
72 if (!size)
73 return result;
74 tmp = leBPL_to_cpup(p);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070075found_first:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070076 tmp &= ~0UL >> (BITS_PER_LONG - size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070077found_middle:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return result + ffz(~tmp);
79}
80
81#define find_first_one_bit(addr, size)\
82 find_next_one_bit((addr), (size), 0)
83
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084static int read_block_bitmap(struct super_block *sb,
85 struct udf_bitmap *bitmap, unsigned int block,
86 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct buffer_head *bh = NULL;
89 int retval = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020090 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080093 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Pekka Enberg97e961f2008-10-15 12:29:03 +020095 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080096 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 bitmap->s_block_bitmap[bitmap_nr] = bh;
100 return retval;
101}
102
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700103static int __load_block_bitmap(struct super_block *sb,
104 struct udf_bitmap *bitmap,
105 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int retval = 0;
108 int nr_groups = bitmap->s_nr_groups;
109
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 if (block_group >= nr_groups) {
111 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
112 nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700115 if (bitmap->s_block_bitmap[block_group]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return block_group;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700117 } else {
118 retval = read_block_bitmap(sb, bitmap, block_group,
119 block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (retval < 0)
121 return retval;
122 return block_group;
123 }
124}
125
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700126static inline int load_block_bitmap(struct super_block *sb,
127 struct udf_bitmap *bitmap,
128 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 int slot;
131
132 slot = __load_block_bitmap(sb, bitmap, block_group);
133
134 if (slot < 0)
135 return slot;
136
137 if (!bitmap->s_block_bitmap[slot])
138 return -EIO;
139
140 return slot;
141}
142
Marcin Slusarz742ba022008-02-08 04:20:40 -0800143static bool udf_add_free_space(struct udf_sb_info *sbi,
144 u16 partition, u32 cnt)
145{
146 struct logicalVolIntegrityDesc *lvid;
147
Marcin Slusarzcba44352008-02-13 15:03:33 -0800148 if (sbi->s_lvid_bh == NULL)
Marcin Slusarz742ba022008-02-08 04:20:40 -0800149 return false;
150
151 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100152 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800153 return true;
154}
155
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156static void udf_bitmap_free_blocks(struct super_block *sb,
157 struct inode *inode,
158 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200159 struct kernel_lb_addr *bloc,
160 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700161 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200165 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned long block;
167 unsigned long block_group;
168 unsigned long bit;
169 unsigned long i;
170 int bitmap_nr;
171 unsigned long overflow;
172
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800173 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200174 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
175 if (bloc->logicalBlockNum < 0 ||
176 (bloc->logicalBlockNum + count) >
177 partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700178 udf_debug("%d < %d || %d + %d > %d\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +0200179 bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
180 count, partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto error_return;
182 }
183
Pekka Enberg97e961f2008-10-15 12:29:03 +0200184 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800185 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800187 do {
188 overflow = 0;
189 block_group = block >> (sb->s_blocksize_bits + 3);
190 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800192 /*
193 * Check to see if we are freeing blocks across a group boundary.
194 */
195 if (bit + count > (sb->s_blocksize << 3)) {
196 overflow = bit + count - (sb->s_blocksize << 3);
197 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800199 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
200 if (bitmap_nr < 0)
201 goto error_return;
202
203 bh = bitmap->s_block_bitmap[bitmap_nr];
204 for (i = 0; i < count; i++) {
205 if (udf_set_bit(bit + i, bh->b_data)) {
206 udf_debug("bit %ld already set\n", bit + i);
207 udf_debug("byte=%2x\n",
208 ((char *)bh->b_data)[(bit + i) >> 3]);
209 } else {
210 if (inode)
Jan Karabacfb7c2009-01-26 17:20:46 +0100211 vfs_dq_free_block(inode, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800212 udf_add_free_space(sbi, sbi->s_partition, 1);
213 }
214 }
215 mark_buffer_dirty(bh);
216 if (overflow) {
217 block += count;
218 count = overflow;
219 }
220 } while (overflow);
221
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sb->s_dirt = 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800224 if (sbi->s_lvid_bh)
225 mark_buffer_dirty(sbi->s_lvid_bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800226 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700229static int udf_bitmap_prealloc_blocks(struct super_block *sb,
230 struct inode *inode,
231 struct udf_bitmap *bitmap,
232 uint16_t partition, uint32_t first_block,
233 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct udf_sb_info *sbi = UDF_SB(sb);
236 int alloc_count = 0;
237 int bit, block, block_group, group_start;
238 int nr_groups, bitmap_nr;
239 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800240 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800242 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800243 part_len = sbi->s_partmaps[partition].s_partition_len;
244 if (first_block < 0 || first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
246
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800247 if (first_block + block_count > part_len)
248 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800250 do {
251 nr_groups = udf_compute_nr_groups(sb, partition);
252 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
253 block_group = block >> (sb->s_blocksize_bits + 3);
254 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800256 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
257 if (bitmap_nr < 0)
258 goto out;
259 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800261 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800263 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
264 if (!udf_test_bit(bit, bh->b_data))
265 goto out;
Jan Karabacfb7c2009-01-26 17:20:46 +0100266 else if (vfs_dq_prealloc_block(inode, 1))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800267 goto out;
268 else if (!udf_clear_bit(bit, bh->b_data)) {
269 udf_debug("bit already cleared for block %d\n", bit);
Jan Karabacfb7c2009-01-26 17:20:46 +0100270 vfs_dq_free_block(inode, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800271 goto out;
272 }
273 block_count--;
274 alloc_count++;
275 bit++;
276 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800278 mark_buffer_dirty(bh);
279 } while (block_count > 0);
280
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700281out:
Marcin Slusarz742ba022008-02-08 04:20:40 -0800282 if (udf_add_free_space(sbi, partition, -alloc_count))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800283 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800285 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return alloc_count;
287}
288
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289static int udf_bitmap_new_block(struct super_block *sb,
290 struct inode *inode,
291 struct udf_bitmap *bitmap, uint16_t partition,
292 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700295 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int end_goal, nr_groups, bitmap_nr, i;
297 struct buffer_head *bh = NULL;
298 char *ptr;
299 int newblock = 0;
300
301 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800302 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700304repeat:
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800305 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goal = 0;
307
308 nr_groups = bitmap->s_nr_groups;
309 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
310 block_group = block >> (sb->s_blocksize_bits + 3);
311 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
312
313 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
314 if (bitmap_nr < 0)
315 goto error_return;
316 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700317 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
318 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700320 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700322 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 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;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700329
Marcin Slusarz4b111112008-02-08 04:20:36 -0800330 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
331 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700333 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 bit = newbit;
335 goto search_back;
336 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700337
Marcin Slusarz4b111112008-02-08 04:20:36 -0800338 newbit = udf_find_next_one_bit(bh->b_data,
339 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700340 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 bit = newbit;
342 goto got_block;
343 }
344 }
345
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346 for (i = 0; i < (nr_groups * 2); i++) {
347 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 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];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700356 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700357 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
358 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700359 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 bit = (ptr - ((char *)bh->b_data)) << 3;
361 break;
362 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700363 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700364 bit = udf_find_next_one_bit((char *)bh->b_data,
365 sb->s_blocksize << 3,
366 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (bit < sb->s_blocksize << 3)
368 break;
369 }
370 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700371 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800372 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return newblock;
374 }
375 if (bit < sb->s_blocksize << 3)
376 goto search_back;
377 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800378 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
379 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700380 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800381 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383 }
384
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800386 i = 0;
387 while (i < 7 && bit > (group_start << 3) &&
388 udf_test_bit(bit - 1, bh->b_data)) {
389 ++i;
390 --bit;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700393got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * Check quota for allocation of this block.
397 */
Jan Karabacfb7c2009-01-26 17:20:46 +0100398 if (inode && vfs_dq_alloc_block(inode, 1)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800399 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *err = -EDQUOT;
401 return 0;
402 }
403
404 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700407 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 udf_debug("bit already cleared for block %d\n", bit);
409 goto repeat;
410 }
411
412 mark_buffer_dirty(bh);
413
Marcin Slusarz742ba022008-02-08 04:20:40 -0800414 if (udf_add_free_space(sbi, partition, -1))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800415 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800417 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 *err = 0;
419 return newblock;
420
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700421error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800423 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425}
426
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427static void udf_table_free_blocks(struct super_block *sb,
428 struct inode *inode,
429 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200430 struct kernel_lb_addr *bloc,
431 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700432 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200435 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700437 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200438 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700439 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int8_t etype;
441 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800442 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800444 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200445 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
446 if (bloc->logicalBlockNum < 0 ||
447 (bloc->logicalBlockNum + count) >
448 partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700449 udf_debug("%d < %d || %d + %d > %d\n",
450 bloc.logicalBlockNum, 0, bloc.logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200451 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto error_return;
453 }
454
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800455 iinfo = UDF_I(table);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800456 /* We do this up front - There are some error conditions that
457 could occure, but.. oh well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (inode)
Jan Karabacfb7c2009-01-26 17:20:46 +0100459 vfs_dq_free_block(inode, count);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800460 if (udf_add_free_space(sbi, sbi->s_partition, count))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800461 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Pekka Enberg97e961f2008-10-15 12:29:03 +0200463 start = bloc->logicalBlockNum + offset;
464 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Jan Karaff116fc2007-05-08 00:35:14 -0700466 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800468 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700469 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700471 while (count &&
472 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800473 if (((eloc.logicalBlockNum +
474 (elen >> sb->s_blocksize_bits)) == start)) {
475 if ((0x3FFFFFFF - elen) <
476 (count << sb->s_blocksize_bits)) {
477 uint32_t tmp = ((0x3FFFFFFF - elen) >>
478 sb->s_blocksize_bits);
479 count -= tmp;
480 start += tmp;
481 elen = (etype << 30) |
482 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700483 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800484 elen = (etype << 30) |
485 (elen +
486 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 start += count;
488 count = 0;
489 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200490 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700491 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800492 if ((0x3FFFFFFF - elen) <
493 (count << sb->s_blocksize_bits)) {
494 uint32_t tmp = ((0x3FFFFFFF - elen) >>
495 sb->s_blocksize_bits);
496 count -= tmp;
497 end -= tmp;
498 eloc.logicalBlockNum -= tmp;
499 elen = (etype << 30) |
500 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700501 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800503 elen = (etype << 30) |
504 (elen +
505 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 end -= count;
507 count = 0;
508 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200509 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700512 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700514 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700515 brelse(oepos.bh);
516 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700517 oepos.bh = epos.bh;
518 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700519 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700520 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700524 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700525 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800526 * NOTE: we CANNOT use udf_add_aext here, as it can try to
527 * allocate a new block, and since we hold the super block
528 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700529 *
530 * We copy the behavior of udf_add_aext, but instead of
531 * trying to allocate a new block close to the existing one,
532 * we just steal a block from the extent we are trying to add.
533 *
534 * It would be nice if the blocks were close together, but it
535 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200539 struct short_ad *sad = NULL;
540 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct allocExtDesc *aed;
542
543 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700544 elen = EXT_RECORDED_ALLOCATED |
545 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800547 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200548 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800549 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200550 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800551 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700552 brelse(oepos.bh);
553 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto error_return;
555 }
556
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700557 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 char *sptr, *dptr;
559 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700560
Jan Kara3bf25cb2007-05-08 00:35:16 -0700561 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700562 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700565 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700566 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 elen -= sb->s_blocksize;
568
Marcin Slusarz4b111112008-02-08 04:20:36 -0800569 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200570 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800571 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700572 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto error_return;
574 }
Jan Karaff116fc2007-05-08 00:35:14 -0700575 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800576 aed->previousAllocExtLocation =
577 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700578 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700579 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800581 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800582 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800583 dptr = epos.bh->b_data +
584 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800586 epos.offset = sizeof(struct allocExtDesc) +
587 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700588 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700589 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700591 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700592 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800593 aed = (struct allocExtDesc *)
594 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100595 le32_add_cpu(&aed->lengthAllocDescs,
596 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700597 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800598 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800599 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800600 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 mark_inode_dirty(table);
602 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700603 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800605 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800606 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
607 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200608 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800610 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
611 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200612 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700613
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800614 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800615 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200616 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800617 sad->extLength = cpu_to_le32(
618 EXT_NEXT_EXTENT_ALLOCDECS |
619 sb->s_blocksize);
620 sad->extPosition =
621 cpu_to_le32(epos.block.logicalBlockNum);
622 break;
623 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200624 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800625 lad->extLength = cpu_to_le32(
626 EXT_NEXT_EXTENT_ALLOCDECS |
627 sb->s_blocksize);
628 lad->extLocation =
629 cpu_to_lelb(epos.block);
630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700632 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700633 udf_update_tag(oepos.bh->b_data, loffset);
634 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700635 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Marcin Slusarz4b111112008-02-08 04:20:36 -0800640 /* It's possible that stealing the block emptied the extent */
641 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200642 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800645 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700647 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700648 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100649 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700650 udf_update_tag(epos.bh->b_data, epos.offset);
651 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 }
654 }
655
Jan Kara3bf25cb2007-05-08 00:35:16 -0700656 brelse(epos.bh);
657 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700659error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800661 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return;
663}
664
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700665static int udf_table_prealloc_blocks(struct super_block *sb,
666 struct inode *inode,
667 struct inode *table, uint16_t partition,
668 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct udf_sb_info *sbi = UDF_SB(sb);
671 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700672 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200673 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700674 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800676 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Marcin Slusarz4b111112008-02-08 04:20:36 -0800678 if (first_block < 0 ||
679 first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800682 iinfo = UDF_I(table);
683 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200684 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800685 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200686 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 else
688 return 0;
689
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800690 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700691 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800692 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700693 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 eloc.logicalBlockNum = 0xFFFFFFFF;
695
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700696 while (first_block != eloc.logicalBlockNum &&
697 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700699 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700700 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700703 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700704 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Karabacfb7c2009-01-26 17:20:46 +0100707 if (inode && vfs_dq_prealloc_block(inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800708 alloc_count > block_count ? block_count : alloc_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 alloc_count = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800710 else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 alloc_count = block_count;
712 eloc.logicalBlockNum += alloc_count;
713 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200714 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800715 (etype << 30) | elen, 1);
716 } else
717 udf_delete_aext(table, epos, eloc,
718 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700719 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jan Kara3bf25cb2007-05-08 00:35:16 -0700723 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Marcin Slusarz742ba022008-02-08 04:20:40 -0800725 if (alloc_count && udf_add_free_space(sbi, partition, -alloc_count)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800726 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 sb->s_dirt = 1;
728 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800729 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return alloc_count;
731}
732
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700733static int udf_table_new_block(struct super_block *sb,
734 struct inode *inode,
735 struct inode *table, uint16_t partition,
736 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct udf_sb_info *sbi = UDF_SB(sb);
739 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
740 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700741 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200742 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700743 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800745 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 *err = -ENOSPC;
748
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800749 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200750 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800751 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200752 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else
754 return newblock;
755
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800756 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800757 if (goal < 0 || goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goal = 0;
759
Marcin Slusarz4b111112008-02-08 04:20:36 -0800760 /* We search for the closest matching block to goal. If we find
761 a exact hit, we stop. Otherwise we keep going till we run out
762 of extents. We store the buffer_head, bloc, and extoffset
763 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700764 */
Jan Karaff116fc2007-05-08 00:35:14 -0700765 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800766 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700767 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700769 while (spread &&
770 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800772 if (goal < eloc.logicalBlockNum +
773 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 nspread = 0;
775 else
776 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700777 (elen >> sb->s_blocksize_bits);
778 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700782 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700784 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700785 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700786 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700787 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Jan Karaff116fc2007-05-08 00:35:14 -0700789 goal_epos.block = epos.block;
790 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goal_eloc = eloc;
792 goal_elen = (etype << 30) | elen;
793 }
794 }
795
Jan Kara3bf25cb2007-05-08 00:35:16 -0700796 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700799 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800800 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802 }
803
804 /* Only allocate blocks from the beginning of the extent.
805 That way, we only delete (empty) extents, never have to insert an
806 extent because of splitting */
807 /* This works, but very poorly.... */
808
809 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goal_elen -= sb->s_blocksize;
812
Jan Karabacfb7c2009-01-26 17:20:46 +0100813 if (inode && vfs_dq_alloc_block(inode, 1)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700814 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800815 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *err = -EDQUOT;
817 return 0;
818 }
819
820 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200821 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 else
Jan Karaff116fc2007-05-08 00:35:14 -0700823 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700824 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Marcin Slusarz742ba022008-02-08 04:20:40 -0800826 if (udf_add_free_space(sbi, partition, -1))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800827 mark_buffer_dirty(sbi->s_lvid_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 sb->s_dirt = 1;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800830 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *err = 0;
832 return newblock;
833}
834
Pekka Enberg97e961f2008-10-15 12:29:03 +0200835void udf_free_blocks(struct super_block *sb, struct inode *inode,
836 struct kernel_lb_addr *bloc, uint32_t offset,
837 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200839 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100843 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
844 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800845 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100846 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
847 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800848 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100849 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
850 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800851 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100852 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
853 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857inline int udf_prealloc_blocks(struct super_block *sb,
858 struct inode *inode,
859 uint16_t partition, uint32_t first_block,
860 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800862 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
863
Marcin Slusarz4b111112008-02-08 04:20:36 -0800864 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800866 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800867 partition, first_block,
868 block_count);
869 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800871 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800872 partition, first_block,
873 block_count);
874 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800876 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800877 partition, first_block,
878 block_count);
879 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800881 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800882 partition, first_block,
883 block_count);
884 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
886}
887
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700888inline int udf_new_block(struct super_block *sb,
889 struct inode *inode,
890 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800892 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700893
Marcin Slusarz4b111112008-02-08 04:20:36 -0800894 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
895 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800896 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700897 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800898 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800900 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700901 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800902 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800904 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700905 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800906 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800908 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700909 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800910 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *err = -EIO;
912 return 0;
913 }
914}