blob: e2ff180173a207bda4804edfe002333bcab4181e [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
Jan Kara146bca72009-03-16 18:27:37 +0100143static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
Marcin Slusarz742ba022008-02-08 04:20:40 -0800144{
Jan Kara146bca72009-03-16 18:27:37 +0100145 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800146 struct logicalVolIntegrityDesc *lvid;
147
Jan Kara146bca72009-03-16 18:27:37 +0100148 if (!sbi->s_lvid_bh)
149 return;
Marcin Slusarz742ba022008-02-08 04:20:40 -0800150
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);
Jan Kara146bca72009-03-16 18:27:37 +0100153 udf_updated_lvid(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800154}
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)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500211 dquot_free_block(inode, 1);
Jan Kara146bca72009-03-16 18:27:37 +0100212 udf_add_free_space(sb, sbi->s_partition, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800213 }
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:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800223 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226static int udf_bitmap_prealloc_blocks(struct super_block *sb,
227 struct inode *inode,
228 struct udf_bitmap *bitmap,
229 uint16_t partition, uint32_t first_block,
230 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct udf_sb_info *sbi = UDF_SB(sb);
233 int alloc_count = 0;
234 int bit, block, block_group, group_start;
235 int nr_groups, bitmap_nr;
236 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800237 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800239 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800240 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200241 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out;
243
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800244 if (first_block + block_count > part_len)
245 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800247 do {
248 nr_groups = udf_compute_nr_groups(sb, partition);
249 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
250 block_group = block >> (sb->s_blocksize_bits + 3);
251 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800253 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
254 if (bitmap_nr < 0)
255 goto out;
256 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800258 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800260 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
261 if (!udf_test_bit(bit, bh->b_data))
262 goto out;
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500263 else if (dquot_prealloc_block(inode, 1))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800264 goto out;
265 else if (!udf_clear_bit(bit, bh->b_data)) {
266 udf_debug("bit already cleared for block %d\n", bit);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500267 dquot_free_block(inode, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800268 goto out;
269 }
270 block_count--;
271 alloc_count++;
272 bit++;
273 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800275 mark_buffer_dirty(bh);
276 } while (block_count > 0);
277
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700278out:
Jan Kara146bca72009-03-16 18:27:37 +0100279 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800280 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return alloc_count;
282}
283
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700284static int udf_bitmap_new_block(struct super_block *sb,
285 struct inode *inode,
286 struct udf_bitmap *bitmap, uint16_t partition,
287 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700290 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int end_goal, nr_groups, bitmap_nr, i;
292 struct buffer_head *bh = NULL;
293 char *ptr;
294 int newblock = 0;
295
296 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800297 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700299repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200300 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goal = 0;
302
303 nr_groups = bitmap->s_nr_groups;
304 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
305 block_group = block >> (sb->s_blocksize_bits + 3);
306 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
307
308 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
309 if (bitmap_nr < 0)
310 goto error_return;
311 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700312 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
313 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700315 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700317 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 end_goal = (bit + 63) & ~63;
321 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
322 if (bit < end_goal)
323 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700324
Marcin Slusarz4b111112008-02-08 04:20:36 -0800325 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
326 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 bit = newbit;
330 goto search_back;
331 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332
Marcin Slusarz4b111112008-02-08 04:20:36 -0800333 newbit = udf_find_next_one_bit(bh->b_data,
334 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 bit = newbit;
337 goto got_block;
338 }
339 }
340
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341 for (i = 0; i < (nr_groups * 2); i++) {
342 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (block_group >= nr_groups)
344 block_group = 0;
345 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
346
347 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
348 if (bitmap_nr < 0)
349 goto error_return;
350 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700351 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700352 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
353 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700354 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 bit = (ptr - ((char *)bh->b_data)) << 3;
356 break;
357 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700358 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700359 bit = udf_find_next_one_bit((char *)bh->b_data,
360 sb->s_blocksize << 3,
361 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (bit < sb->s_blocksize << 3)
363 break;
364 }
365 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700366 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800367 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return newblock;
369 }
370 if (bit < sb->s_blocksize << 3)
371 goto search_back;
372 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800373 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
374 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700375 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800376 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
378 }
379
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700380search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800381 i = 0;
382 while (i < 7 && bit > (group_start << 3) &&
383 udf_test_bit(bit - 1, bh->b_data)) {
384 ++i;
385 --bit;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700388got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * Check quota for allocation of this block.
392 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500393 if (inode) {
394 int ret = dquot_alloc_block(inode, 1);
395
396 if (ret) {
397 mutex_unlock(&sbi->s_alloc_mutex);
398 *err = ret;
399 return 0;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700404 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700406 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 udf_debug("bit already cleared for block %d\n", bit);
408 goto repeat;
409 }
410
411 mark_buffer_dirty(bh);
412
Jan Kara146bca72009-03-16 18:27:37 +0100413 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800414 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *err = 0;
416 return newblock;
417
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700418error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800420 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424static void udf_table_free_blocks(struct super_block *sb,
425 struct inode *inode,
426 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200427 struct kernel_lb_addr *bloc,
428 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700429 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200432 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700434 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200435 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700436 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int8_t etype;
438 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800439 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800441 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200442 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
443 if (bloc->logicalBlockNum < 0 ||
444 (bloc->logicalBlockNum + count) >
445 partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700446 udf_debug("%d < %d || %d + %d > %d\n",
Jan Kara1fefd082009-11-30 19:47:10 +0100447 bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200448 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto error_return;
450 }
451
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800452 iinfo = UDF_I(table);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800453 /* We do this up front - There are some error conditions that
454 could occure, but.. oh well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (inode)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500456 dquot_free_block(inode, count);
Jan Kara146bca72009-03-16 18:27:37 +0100457 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Pekka Enberg97e961f2008-10-15 12:29:03 +0200459 start = bloc->logicalBlockNum + offset;
460 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jan Karaff116fc2007-05-08 00:35:14 -0700462 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800464 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700465 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700467 while (count &&
468 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800469 if (((eloc.logicalBlockNum +
470 (elen >> sb->s_blocksize_bits)) == start)) {
471 if ((0x3FFFFFFF - elen) <
472 (count << sb->s_blocksize_bits)) {
473 uint32_t tmp = ((0x3FFFFFFF - elen) >>
474 sb->s_blocksize_bits);
475 count -= tmp;
476 start += tmp;
477 elen = (etype << 30) |
478 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700479 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800480 elen = (etype << 30) |
481 (elen +
482 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 start += count;
484 count = 0;
485 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200486 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700487 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800488 if ((0x3FFFFFFF - elen) <
489 (count << sb->s_blocksize_bits)) {
490 uint32_t tmp = ((0x3FFFFFFF - elen) >>
491 sb->s_blocksize_bits);
492 count -= tmp;
493 end -= tmp;
494 eloc.logicalBlockNum -= tmp;
495 elen = (etype << 30) |
496 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700497 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800499 elen = (etype << 30) |
500 (elen +
501 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 end -= count;
503 count = 0;
504 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200505 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700508 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700510 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700511 brelse(oepos.bh);
512 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700513 oepos.bh = epos.bh;
514 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700515 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700516 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700520 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700521 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800522 * NOTE: we CANNOT use udf_add_aext here, as it can try to
523 * allocate a new block, and since we hold the super block
524 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700525 *
526 * We copy the behavior of udf_add_aext, but instead of
527 * trying to allocate a new block close to the existing one,
528 * we just steal a block from the extent we are trying to add.
529 *
530 * It would be nice if the blocks were close together, but it
531 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200535 struct short_ad *sad = NULL;
536 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct allocExtDesc *aed;
538
539 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700540 elen = EXT_RECORDED_ALLOCATED |
541 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800543 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200544 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800545 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200546 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800547 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700548 brelse(oepos.bh);
549 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto error_return;
551 }
552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 char *sptr, *dptr;
555 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556
Jan Kara3bf25cb2007-05-08 00:35:16 -0700557 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700558 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700561 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700562 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 elen -= sb->s_blocksize;
564
Marcin Slusarz4b111112008-02-08 04:20:36 -0800565 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200566 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800567 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700568 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto error_return;
570 }
Jan Karaff116fc2007-05-08 00:35:14 -0700571 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800572 aed->previousAllocExtLocation =
573 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700574 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700575 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800577 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800578 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800579 dptr = epos.bh->b_data +
580 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800582 epos.offset = sizeof(struct allocExtDesc) +
583 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700584 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700585 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700588 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800589 aed = (struct allocExtDesc *)
590 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100591 le32_add_cpu(&aed->lengthAllocDescs,
592 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800594 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800595 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800596 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 mark_inode_dirty(table);
598 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700599 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800601 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800602 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
603 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200604 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800606 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
607 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200608 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700609
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800610 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800611 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200612 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800613 sad->extLength = cpu_to_le32(
614 EXT_NEXT_EXTENT_ALLOCDECS |
615 sb->s_blocksize);
616 sad->extPosition =
617 cpu_to_le32(epos.block.logicalBlockNum);
618 break;
619 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200620 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800621 lad->extLength = cpu_to_le32(
622 EXT_NEXT_EXTENT_ALLOCDECS |
623 sb->s_blocksize);
624 lad->extLocation =
625 cpu_to_lelb(epos.block);
626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700628 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700629 udf_update_tag(oepos.bh->b_data, loffset);
630 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
Marcin Slusarz4b111112008-02-08 04:20:36 -0800636 /* It's possible that stealing the block emptied the extent */
637 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200638 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800641 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700644 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100645 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700646 udf_update_tag(epos.bh->b_data, epos.offset);
647 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 }
650 }
651
Jan Kara3bf25cb2007-05-08 00:35:16 -0700652 brelse(epos.bh);
653 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700655error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800656 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return;
658}
659
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700660static int udf_table_prealloc_blocks(struct super_block *sb,
661 struct inode *inode,
662 struct inode *table, uint16_t partition,
663 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct udf_sb_info *sbi = UDF_SB(sb);
666 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700667 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200668 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700669 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800671 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Roel Kluin3391faa2009-06-22 23:12:29 +0200673 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800676 iinfo = UDF_I(table);
677 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200678 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800679 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200680 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 else
682 return 0;
683
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800684 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700685 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800686 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700687 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 eloc.logicalBlockNum = 0xFFFFFFFF;
689
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700690 while (first_block != eloc.logicalBlockNum &&
691 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700693 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700694 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700697 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700698 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 alloc_count = (elen >> sb->s_blocksize_bits);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500701 if (inode && dquot_prealloc_block(inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800702 alloc_count > block_count ? block_count : alloc_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 alloc_count = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800704 else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 alloc_count = block_count;
706 eloc.logicalBlockNum += alloc_count;
707 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200708 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800709 (etype << 30) | elen, 1);
710 } else
711 udf_delete_aext(table, epos, eloc,
712 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700713 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jan Kara3bf25cb2007-05-08 00:35:16 -0700717 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jan Kara146bca72009-03-16 18:27:37 +0100719 if (alloc_count)
720 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800721 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return alloc_count;
723}
724
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700725static int udf_table_new_block(struct super_block *sb,
726 struct inode *inode,
727 struct inode *table, uint16_t partition,
728 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct udf_sb_info *sbi = UDF_SB(sb);
731 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
732 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700733 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200734 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700735 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800737 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 *err = -ENOSPC;
740
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800741 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200742 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800743 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200744 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 else
746 return newblock;
747
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800748 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200749 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goal = 0;
751
Marcin Slusarz4b111112008-02-08 04:20:36 -0800752 /* We search for the closest matching block to goal. If we find
753 a exact hit, we stop. Otherwise we keep going till we run out
754 of extents. We store the buffer_head, bloc, and extoffset
755 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756 */
Jan Karaff116fc2007-05-08 00:35:14 -0700757 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800758 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700759 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700761 while (spread &&
762 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800764 if (goal < eloc.logicalBlockNum +
765 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 nspread = 0;
767 else
768 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700769 (elen >> sb->s_blocksize_bits);
770 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700776 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700777 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700778 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700779 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Jan Karaff116fc2007-05-08 00:35:14 -0700781 goal_epos.block = epos.block;
782 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goal_eloc = eloc;
784 goal_elen = (etype << 30) | elen;
785 }
786 }
787
Jan Kara3bf25cb2007-05-08 00:35:16 -0700788 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700790 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700791 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800792 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return 0;
794 }
795
796 /* Only allocate blocks from the beginning of the extent.
797 That way, we only delete (empty) extents, never have to insert an
798 extent because of splitting */
799 /* This works, but very poorly.... */
800
801 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700802 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goal_elen -= sb->s_blocksize;
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500804 if (inode) {
805 *err = dquot_alloc_block(inode, 1);
806 if (*err) {
807 brelse(goal_epos.bh);
808 mutex_unlock(&sbi->s_alloc_mutex);
809 return 0;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200814 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 else
Jan Karaff116fc2007-05-08 00:35:14 -0700816 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700817 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jan Kara146bca72009-03-16 18:27:37 +0100819 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800821 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *err = 0;
823 return newblock;
824}
825
Pekka Enberg97e961f2008-10-15 12:29:03 +0200826void udf_free_blocks(struct super_block *sb, struct inode *inode,
827 struct kernel_lb_addr *bloc, uint32_t offset,
828 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200830 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800831 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800833 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100834 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
835 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800836 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100837 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
838 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800839 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100840 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
841 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100843 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
844 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848inline int udf_prealloc_blocks(struct super_block *sb,
849 struct inode *inode,
850 uint16_t partition, uint32_t first_block,
851 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800853 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
854
Marcin Slusarz4b111112008-02-08 04:20:36 -0800855 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800857 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800858 partition, first_block,
859 block_count);
860 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800862 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800863 partition, first_block,
864 block_count);
865 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800867 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800868 partition, first_block,
869 block_count);
870 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800872 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800873 partition, first_block,
874 block_count);
875 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877}
878
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700879inline int udf_new_block(struct super_block *sb,
880 struct inode *inode,
881 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800883 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700884
Marcin Slusarz4b111112008-02-08 04:20:36 -0800885 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
886 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800887 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700888 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800889 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800891 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700892 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800893 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800895 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700896 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800897 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800899 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700900 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800901 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 *err = -EIO;
903 return 0;
904 }
905}