blob: 9a9378b4eb5ae2c89495b9025d7be8c5aa04ef5b [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)
Marcin Slusarz4b111112008-02-08 04:20:36 -080034#define udf_find_next_one_bit(addr, size, offset) \
Akinobu Mita3a065fc2010-02-23 23:11:13 +090035 ext2_find_next_bit(addr, size, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070037static int read_block_bitmap(struct super_block *sb,
38 struct udf_bitmap *bitmap, unsigned int block,
39 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 struct buffer_head *bh = NULL;
42 int retval = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020043 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080046 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pekka Enberg97e961f2008-10-15 12:29:03 +020048 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080049 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 bitmap->s_block_bitmap[bitmap_nr] = bh;
53 return retval;
54}
55
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070056static int __load_block_bitmap(struct super_block *sb,
57 struct udf_bitmap *bitmap,
58 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int retval = 0;
61 int nr_groups = bitmap->s_nr_groups;
62
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 if (block_group >= nr_groups) {
64 udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
65 nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070068 if (bitmap->s_block_bitmap[block_group]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return block_group;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070070 } else {
71 retval = read_block_bitmap(sb, bitmap, block_group,
72 block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (retval < 0)
74 return retval;
75 return block_group;
76 }
77}
78
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070079static inline int load_block_bitmap(struct super_block *sb,
80 struct udf_bitmap *bitmap,
81 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 int slot;
84
85 slot = __load_block_bitmap(sb, bitmap, block_group);
86
87 if (slot < 0)
88 return slot;
89
90 if (!bitmap->s_block_bitmap[slot])
91 return -EIO;
92
93 return slot;
94}
95
Jan Kara146bca72009-03-16 18:27:37 +010096static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
Marcin Slusarz742ba022008-02-08 04:20:40 -080097{
Jan Kara146bca72009-03-16 18:27:37 +010098 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -080099 struct logicalVolIntegrityDesc *lvid;
100
Jan Kara146bca72009-03-16 18:27:37 +0100101 if (!sbi->s_lvid_bh)
102 return;
Marcin Slusarz742ba022008-02-08 04:20:40 -0800103
104 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100105 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
Jan Kara146bca72009-03-16 18:27:37 +0100106 udf_updated_lvid(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800107}
108
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700109static void udf_bitmap_free_blocks(struct super_block *sb,
110 struct inode *inode,
111 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200112 struct kernel_lb_addr *bloc,
113 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700117 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200118 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned long block;
120 unsigned long block_group;
121 unsigned long bit;
122 unsigned long i;
123 int bitmap_nr;
124 unsigned long overflow;
125
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800126 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200127 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300128 if (bloc->logicalBlockNum + count < count ||
129 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700130 udf_debug("%d < %d || %d + %d > %d\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +0200131 bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
132 count, partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto error_return;
134 }
135
Pekka Enberg97e961f2008-10-15 12:29:03 +0200136 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800137 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800139 do {
140 overflow = 0;
141 block_group = block >> (sb->s_blocksize_bits + 3);
142 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800144 /*
145 * Check to see if we are freeing blocks across a group boundary.
146 */
147 if (bit + count > (sb->s_blocksize << 3)) {
148 overflow = bit + count - (sb->s_blocksize << 3);
149 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800151 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
152 if (bitmap_nr < 0)
153 goto error_return;
154
155 bh = bitmap->s_block_bitmap[bitmap_nr];
156 for (i = 0; i < count; i++) {
157 if (udf_set_bit(bit + i, bh->b_data)) {
158 udf_debug("bit %ld already set\n", bit + i);
159 udf_debug("byte=%2x\n",
160 ((char *)bh->b_data)[(bit + i) >> 3]);
161 } else {
162 if (inode)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500163 dquot_free_block(inode, 1);
Jan Kara146bca72009-03-16 18:27:37 +0100164 udf_add_free_space(sb, sbi->s_partition, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800165 }
166 }
167 mark_buffer_dirty(bh);
168 if (overflow) {
169 block += count;
170 count = overflow;
171 }
172 } while (overflow);
173
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700174error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800175 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700178static int udf_bitmap_prealloc_blocks(struct super_block *sb,
179 struct inode *inode,
180 struct udf_bitmap *bitmap,
181 uint16_t partition, uint32_t first_block,
182 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct udf_sb_info *sbi = UDF_SB(sb);
185 int alloc_count = 0;
186 int bit, block, block_group, group_start;
187 int nr_groups, bitmap_nr;
188 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800189 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800191 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800192 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200193 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto out;
195
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800196 if (first_block + block_count > part_len)
197 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800199 do {
200 nr_groups = udf_compute_nr_groups(sb, partition);
201 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
202 block_group = block >> (sb->s_blocksize_bits + 3);
203 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800205 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
206 if (bitmap_nr < 0)
207 goto out;
208 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800210 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800212 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
213 if (!udf_test_bit(bit, bh->b_data))
214 goto out;
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500215 else if (dquot_prealloc_block(inode, 1))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800216 goto out;
217 else if (!udf_clear_bit(bit, bh->b_data)) {
218 udf_debug("bit already cleared for block %d\n", bit);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500219 dquot_free_block(inode, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800220 goto out;
221 }
222 block_count--;
223 alloc_count++;
224 bit++;
225 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800227 mark_buffer_dirty(bh);
228 } while (block_count > 0);
229
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700230out:
Jan Kara146bca72009-03-16 18:27:37 +0100231 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800232 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return alloc_count;
234}
235
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700236static int udf_bitmap_new_block(struct super_block *sb,
237 struct inode *inode,
238 struct udf_bitmap *bitmap, uint16_t partition,
239 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700242 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int end_goal, nr_groups, bitmap_nr, i;
244 struct buffer_head *bh = NULL;
245 char *ptr;
246 int newblock = 0;
247
248 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800249 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700251repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200252 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goal = 0;
254
255 nr_groups = bitmap->s_nr_groups;
256 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
257 block_group = block >> (sb->s_blocksize_bits + 3);
258 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
259
260 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
261 if (bitmap_nr < 0)
262 goto error_return;
263 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700264 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
265 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700267 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700269 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 end_goal = (bit + 63) & ~63;
273 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
274 if (bit < end_goal)
275 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700276
Marcin Slusarz4b111112008-02-08 04:20:36 -0800277 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
278 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700280 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 bit = newbit;
282 goto search_back;
283 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700284
Marcin Slusarz4b111112008-02-08 04:20:36 -0800285 newbit = udf_find_next_one_bit(bh->b_data,
286 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700287 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 bit = newbit;
289 goto got_block;
290 }
291 }
292
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700293 for (i = 0; i < (nr_groups * 2); i++) {
294 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (block_group >= nr_groups)
296 block_group = 0;
297 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
298
299 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
300 if (bitmap_nr < 0)
301 goto error_return;
302 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700303 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700304 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
305 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700306 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 bit = (ptr - ((char *)bh->b_data)) << 3;
308 break;
309 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700310 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700311 bit = udf_find_next_one_bit((char *)bh->b_data,
312 sb->s_blocksize << 3,
313 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (bit < sb->s_blocksize << 3)
315 break;
316 }
317 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700318 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800319 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return newblock;
321 }
322 if (bit < sb->s_blocksize << 3)
323 goto search_back;
324 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800325 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
326 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700327 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800328 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330 }
331
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800333 i = 0;
334 while (i < 7 && bit > (group_start << 3) &&
335 udf_test_bit(bit - 1, bh->b_data)) {
336 ++i;
337 --bit;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700340got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /*
343 * Check quota for allocation of this block.
344 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500345 if (inode) {
346 int ret = dquot_alloc_block(inode, 1);
347
348 if (ret) {
349 mutex_unlock(&sbi->s_alloc_mutex);
350 *err = ret;
351 return 0;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700356 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700358 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 udf_debug("bit already cleared for block %d\n", bit);
360 goto repeat;
361 }
362
363 mark_buffer_dirty(bh);
364
Jan Kara146bca72009-03-16 18:27:37 +0100365 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800366 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *err = 0;
368 return newblock;
369
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700370error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800372 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700376static void udf_table_free_blocks(struct super_block *sb,
377 struct inode *inode,
378 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200379 struct kernel_lb_addr *bloc,
380 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700381 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200384 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700386 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200387 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700388 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int8_t etype;
390 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800391 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800393 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200394 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300395 if (bloc->logicalBlockNum + count < count ||
396 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700397 udf_debug("%d < %d || %d + %d > %d\n",
Jan Kara1fefd082009-11-30 19:47:10 +0100398 bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200399 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto error_return;
401 }
402
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800403 iinfo = UDF_I(table);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800404 /* We do this up front - There are some error conditions that
405 could occure, but.. oh well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (inode)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500407 dquot_free_block(inode, count);
Jan Kara146bca72009-03-16 18:27:37 +0100408 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Pekka Enberg97e961f2008-10-15 12:29:03 +0200410 start = bloc->logicalBlockNum + offset;
411 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jan Karaff116fc2007-05-08 00:35:14 -0700413 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800415 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700416 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700418 while (count &&
419 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800420 if (((eloc.logicalBlockNum +
421 (elen >> sb->s_blocksize_bits)) == start)) {
422 if ((0x3FFFFFFF - elen) <
423 (count << sb->s_blocksize_bits)) {
424 uint32_t tmp = ((0x3FFFFFFF - elen) >>
425 sb->s_blocksize_bits);
426 count -= tmp;
427 start += tmp;
428 elen = (etype << 30) |
429 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800431 elen = (etype << 30) |
432 (elen +
433 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 start += count;
435 count = 0;
436 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200437 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700438 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800439 if ((0x3FFFFFFF - elen) <
440 (count << sb->s_blocksize_bits)) {
441 uint32_t tmp = ((0x3FFFFFFF - elen) >>
442 sb->s_blocksize_bits);
443 count -= tmp;
444 end -= tmp;
445 eloc.logicalBlockNum -= tmp;
446 elen = (etype << 30) |
447 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700448 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800450 elen = (etype << 30) |
451 (elen +
452 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 end -= count;
454 count = 0;
455 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200456 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700459 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700461 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700462 brelse(oepos.bh);
463 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700464 oepos.bh = epos.bh;
465 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700466 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700467 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700472 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800473 * NOTE: we CANNOT use udf_add_aext here, as it can try to
474 * allocate a new block, and since we hold the super block
475 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700476 *
477 * We copy the behavior of udf_add_aext, but instead of
478 * trying to allocate a new block close to the existing one,
479 * we just steal a block from the extent we are trying to add.
480 *
481 * It would be nice if the blocks were close together, but it
482 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200486 struct short_ad *sad = NULL;
487 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct allocExtDesc *aed;
489
490 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700491 elen = EXT_RECORDED_ALLOCATED |
492 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800494 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200495 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800496 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200497 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800498 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700499 brelse(oepos.bh);
500 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto error_return;
502 }
503
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700504 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500505 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700507
Jan Kara3bf25cb2007-05-08 00:35:16 -0700508 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700509 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700512 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700513 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 elen -= sb->s_blocksize;
515
Marcin Slusarz4b111112008-02-08 04:20:36 -0800516 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200517 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800518 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700519 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto error_return;
521 }
Jan Karaff116fc2007-05-08 00:35:14 -0700522 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800523 aed->previousAllocExtLocation =
524 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700526 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800528 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800529 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800530 dptr = epos.bh->b_data +
531 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800533 epos.offset = sizeof(struct allocExtDesc) +
534 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700535 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700536 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700538 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700539 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800540 aed = (struct allocExtDesc *)
541 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100542 le32_add_cpu(&aed->lengthAllocDescs,
543 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800545 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800546 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800547 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 mark_inode_dirty(table);
549 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700550 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800552 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800553 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
554 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200555 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800557 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
558 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200559 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700560
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800561 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800562 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200563 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800564 sad->extLength = cpu_to_le32(
565 EXT_NEXT_EXTENT_ALLOCDECS |
566 sb->s_blocksize);
567 sad->extPosition =
568 cpu_to_le32(epos.block.logicalBlockNum);
569 break;
570 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200571 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800572 lad->extLength = cpu_to_le32(
573 EXT_NEXT_EXTENT_ALLOCDECS |
574 sb->s_blocksize);
575 lad->extLocation =
576 cpu_to_lelb(epos.block);
577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700579 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700580 udf_update_tag(oepos.bh->b_data, loffset);
581 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700582 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Marcin Slusarz4b111112008-02-08 04:20:36 -0800587 /* It's possible that stealing the block emptied the extent */
588 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200589 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700591 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800592 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700594 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700595 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100596 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700597 udf_update_tag(epos.bh->b_data, epos.offset);
598 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 }
601 }
602
Jan Kara3bf25cb2007-05-08 00:35:16 -0700603 brelse(epos.bh);
604 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700606error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800607 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return;
609}
610
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611static int udf_table_prealloc_blocks(struct super_block *sb,
612 struct inode *inode,
613 struct inode *table, uint16_t partition,
614 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct udf_sb_info *sbi = UDF_SB(sb);
617 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700618 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200619 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700620 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800622 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Roel Kluin3391faa2009-06-22 23:12:29 +0200624 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800627 iinfo = UDF_I(table);
628 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200629 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800630 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200631 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 else
633 return 0;
634
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800635 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700636 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800637 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700638 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 eloc.logicalBlockNum = 0xFFFFFFFF;
640
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700641 while (first_block != eloc.logicalBlockNum &&
642 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700645 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700648 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700649 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 alloc_count = (elen >> sb->s_blocksize_bits);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500652 if (inode && dquot_prealloc_block(inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800653 alloc_count > block_count ? block_count : alloc_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 alloc_count = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800655 else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 alloc_count = block_count;
657 eloc.logicalBlockNum += alloc_count;
658 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200659 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800660 (etype << 30) | elen, 1);
661 } else
662 udf_delete_aext(table, epos, eloc,
663 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700664 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jan Kara3bf25cb2007-05-08 00:35:16 -0700668 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jan Kara146bca72009-03-16 18:27:37 +0100670 if (alloc_count)
671 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800672 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return alloc_count;
674}
675
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700676static int udf_table_new_block(struct super_block *sb,
677 struct inode *inode,
678 struct inode *table, uint16_t partition,
679 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct udf_sb_info *sbi = UDF_SB(sb);
682 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
683 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700684 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200685 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700686 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800688 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 *err = -ENOSPC;
691
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800692 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200693 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800694 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200695 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 else
697 return newblock;
698
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800699 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200700 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goal = 0;
702
Marcin Slusarz4b111112008-02-08 04:20:36 -0800703 /* We search for the closest matching block to goal. If we find
704 a exact hit, we stop. Otherwise we keep going till we run out
705 of extents. We store the buffer_head, bloc, and extoffset
706 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700707 */
Jan Karaff116fc2007-05-08 00:35:14 -0700708 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800709 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700710 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700712 while (spread &&
713 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700714 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800715 if (goal < eloc.logicalBlockNum +
716 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 nspread = 0;
718 else
719 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700720 (elen >> sb->s_blocksize_bits);
721 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700725 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700727 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700728 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700729 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700730 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Jan Karaff116fc2007-05-08 00:35:14 -0700732 goal_epos.block = epos.block;
733 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goal_eloc = eloc;
735 goal_elen = (etype << 30) | elen;
736 }
737 }
738
Jan Kara3bf25cb2007-05-08 00:35:16 -0700739 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700741 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700742 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800743 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745 }
746
747 /* Only allocate blocks from the beginning of the extent.
748 That way, we only delete (empty) extents, never have to insert an
749 extent because of splitting */
750 /* This works, but very poorly.... */
751
752 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700753 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goal_elen -= sb->s_blocksize;
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500755 if (inode) {
756 *err = dquot_alloc_block(inode, 1);
757 if (*err) {
758 brelse(goal_epos.bh);
759 mutex_unlock(&sbi->s_alloc_mutex);
760 return 0;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
764 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200765 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 else
Jan Karaff116fc2007-05-08 00:35:14 -0700767 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700768 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jan Kara146bca72009-03-16 18:27:37 +0100770 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800772 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *err = 0;
774 return newblock;
775}
776
Pekka Enberg97e961f2008-10-15 12:29:03 +0200777void udf_free_blocks(struct super_block *sb, struct inode *inode,
778 struct kernel_lb_addr *bloc, uint32_t offset,
779 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200781 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800782 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800784 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100785 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
786 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800787 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100788 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
789 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800790 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100791 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
792 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800793 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100794 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
795 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700799inline int udf_prealloc_blocks(struct super_block *sb,
800 struct inode *inode,
801 uint16_t partition, uint32_t first_block,
802 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800804 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
805
Marcin Slusarz4b111112008-02-08 04:20:36 -0800806 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800808 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800809 partition, first_block,
810 block_count);
811 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800813 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800814 partition, first_block,
815 block_count);
816 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800818 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800819 partition, first_block,
820 block_count);
821 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800823 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800824 partition, first_block,
825 block_count);
826 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828}
829
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700830inline int udf_new_block(struct super_block *sb,
831 struct inode *inode,
832 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800834 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700835
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
837 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800838 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700839 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800840 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700843 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800844 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800846 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700847 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800848 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800850 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700851 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800852 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 *err = -EIO;
854 return 0;
855 }
856}