blob: 19626e2491c4656b56fe672d3a9f3f8a09143c66 [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];
128 if (bloc->logicalBlockNum < 0 ||
129 (bloc->logicalBlockNum + count) >
130 partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700131 udf_debug("%d < %d || %d + %d > %d\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +0200132 bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
133 count, partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto error_return;
135 }
136
Pekka Enberg97e961f2008-10-15 12:29:03 +0200137 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800138 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800140 do {
141 overflow = 0;
142 block_group = block >> (sb->s_blocksize_bits + 3);
143 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800145 /*
146 * Check to see if we are freeing blocks across a group boundary.
147 */
148 if (bit + count > (sb->s_blocksize << 3)) {
149 overflow = bit + count - (sb->s_blocksize << 3);
150 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800152 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
153 if (bitmap_nr < 0)
154 goto error_return;
155
156 bh = bitmap->s_block_bitmap[bitmap_nr];
157 for (i = 0; i < count; i++) {
158 if (udf_set_bit(bit + i, bh->b_data)) {
159 udf_debug("bit %ld already set\n", bit + i);
160 udf_debug("byte=%2x\n",
161 ((char *)bh->b_data)[(bit + i) >> 3]);
162 } else {
163 if (inode)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500164 dquot_free_block(inode, 1);
Jan Kara146bca72009-03-16 18:27:37 +0100165 udf_add_free_space(sb, sbi->s_partition, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800166 }
167 }
168 mark_buffer_dirty(bh);
169 if (overflow) {
170 block += count;
171 count = overflow;
172 }
173 } while (overflow);
174
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700175error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800176 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700179static int udf_bitmap_prealloc_blocks(struct super_block *sb,
180 struct inode *inode,
181 struct udf_bitmap *bitmap,
182 uint16_t partition, uint32_t first_block,
183 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct udf_sb_info *sbi = UDF_SB(sb);
186 int alloc_count = 0;
187 int bit, block, block_group, group_start;
188 int nr_groups, bitmap_nr;
189 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800190 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800192 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800193 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200194 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800197 if (first_block + block_count > part_len)
198 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800200 do {
201 nr_groups = udf_compute_nr_groups(sb, partition);
202 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
203 block_group = block >> (sb->s_blocksize_bits + 3);
204 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800206 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
207 if (bitmap_nr < 0)
208 goto out;
209 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800211 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800213 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
214 if (!udf_test_bit(bit, bh->b_data))
215 goto out;
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500216 else if (dquot_prealloc_block(inode, 1))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800217 goto out;
218 else if (!udf_clear_bit(bit, bh->b_data)) {
219 udf_debug("bit already cleared for block %d\n", bit);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500220 dquot_free_block(inode, 1);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800221 goto out;
222 }
223 block_count--;
224 alloc_count++;
225 bit++;
226 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800228 mark_buffer_dirty(bh);
229 } while (block_count > 0);
230
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700231out:
Jan Kara146bca72009-03-16 18:27:37 +0100232 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800233 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return alloc_count;
235}
236
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700237static int udf_bitmap_new_block(struct super_block *sb,
238 struct inode *inode,
239 struct udf_bitmap *bitmap, uint16_t partition,
240 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700243 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int end_goal, nr_groups, bitmap_nr, i;
245 struct buffer_head *bh = NULL;
246 char *ptr;
247 int newblock = 0;
248
249 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800250 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700252repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200253 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goal = 0;
255
256 nr_groups = bitmap->s_nr_groups;
257 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
258 block_group = block >> (sb->s_blocksize_bits + 3);
259 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
260
261 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
262 if (bitmap_nr < 0)
263 goto error_return;
264 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700265 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
266 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700268 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700270 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 end_goal = (bit + 63) & ~63;
274 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
275 if (bit < end_goal)
276 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700277
Marcin Slusarz4b111112008-02-08 04:20:36 -0800278 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
279 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700281 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 bit = newbit;
283 goto search_back;
284 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700285
Marcin Slusarz4b111112008-02-08 04:20:36 -0800286 newbit = udf_find_next_one_bit(bh->b_data,
287 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 bit = newbit;
290 goto got_block;
291 }
292 }
293
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 for (i = 0; i < (nr_groups * 2); i++) {
295 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (block_group >= nr_groups)
297 block_group = 0;
298 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
299
300 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
301 if (bitmap_nr < 0)
302 goto error_return;
303 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700304 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700305 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
306 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700307 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 bit = (ptr - ((char *)bh->b_data)) << 3;
309 break;
310 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700311 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700312 bit = udf_find_next_one_bit((char *)bh->b_data,
313 sb->s_blocksize << 3,
314 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (bit < sb->s_blocksize << 3)
316 break;
317 }
318 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700319 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800320 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return newblock;
322 }
323 if (bit < sb->s_blocksize << 3)
324 goto search_back;
325 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800326 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
327 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800329 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331 }
332
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700333search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800334 i = 0;
335 while (i < 7 && bit > (group_start << 3) &&
336 udf_test_bit(bit - 1, bh->b_data)) {
337 ++i;
338 --bit;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700341got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /*
344 * Check quota for allocation of this block.
345 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500346 if (inode) {
347 int ret = dquot_alloc_block(inode, 1);
348
349 if (ret) {
350 mutex_unlock(&sbi->s_alloc_mutex);
351 *err = ret;
352 return 0;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700357 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700359 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 udf_debug("bit already cleared for block %d\n", bit);
361 goto repeat;
362 }
363
364 mark_buffer_dirty(bh);
365
Jan Kara146bca72009-03-16 18:27:37 +0100366 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800367 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *err = 0;
369 return newblock;
370
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700371error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800373 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700377static void udf_table_free_blocks(struct super_block *sb,
378 struct inode *inode,
379 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200380 struct kernel_lb_addr *bloc,
381 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700382 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200385 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700387 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200388 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700389 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int8_t etype;
391 int i;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800392 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800394 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200395 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
396 if (bloc->logicalBlockNum < 0 ||
397 (bloc->logicalBlockNum + count) >
398 partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700399 udf_debug("%d < %d || %d + %d > %d\n",
Jan Kara1fefd082009-11-30 19:47:10 +0100400 bloc->logicalBlockNum, 0, bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200401 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto error_return;
403 }
404
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800405 iinfo = UDF_I(table);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800406 /* We do this up front - There are some error conditions that
407 could occure, but.. oh well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (inode)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500409 dquot_free_block(inode, count);
Jan Kara146bca72009-03-16 18:27:37 +0100410 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Pekka Enberg97e961f2008-10-15 12:29:03 +0200412 start = bloc->logicalBlockNum + offset;
413 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jan Karaff116fc2007-05-08 00:35:14 -0700415 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800417 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700418 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700420 while (count &&
421 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 if (((eloc.logicalBlockNum +
423 (elen >> sb->s_blocksize_bits)) == start)) {
424 if ((0x3FFFFFFF - elen) <
425 (count << sb->s_blocksize_bits)) {
426 uint32_t tmp = ((0x3FFFFFFF - elen) >>
427 sb->s_blocksize_bits);
428 count -= tmp;
429 start += tmp;
430 elen = (etype << 30) |
431 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700432 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800433 elen = (etype << 30) |
434 (elen +
435 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 start += count;
437 count = 0;
438 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200439 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700440 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800441 if ((0x3FFFFFFF - elen) <
442 (count << sb->s_blocksize_bits)) {
443 uint32_t tmp = ((0x3FFFFFFF - elen) >>
444 sb->s_blocksize_bits);
445 count -= tmp;
446 end -= tmp;
447 eloc.logicalBlockNum -= tmp;
448 elen = (etype << 30) |
449 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700450 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800452 elen = (etype << 30) |
453 (elen +
454 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 end -= count;
456 count = 0;
457 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200458 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700461 if (epos.bh != oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 i = -1;
Jan Karaff116fc2007-05-08 00:35:14 -0700463 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700464 brelse(oepos.bh);
465 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700466 oepos.bh = epos.bh;
467 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700468 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700469 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700473 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700474 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800475 * NOTE: we CANNOT use udf_add_aext here, as it can try to
476 * allocate a new block, and since we hold the super block
477 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700478 *
479 * We copy the behavior of udf_add_aext, but instead of
480 * trying to allocate a new block close to the existing one,
481 * we just steal a block from the extent we are trying to add.
482 *
483 * It would be nice if the blocks were close together, but it
484 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200488 struct short_ad *sad = NULL;
489 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct allocExtDesc *aed;
491
492 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700493 elen = EXT_RECORDED_ALLOCATED |
494 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800496 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200497 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800498 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200499 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800500 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700501 brelse(oepos.bh);
502 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto error_return;
504 }
505
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700506 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500507 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700509
Jan Kara3bf25cb2007-05-08 00:35:16 -0700510 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700511 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700514 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700515 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 elen -= sb->s_blocksize;
517
Marcin Slusarz4b111112008-02-08 04:20:36 -0800518 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200519 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800520 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700521 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto error_return;
523 }
Jan Karaff116fc2007-05-08 00:35:14 -0700524 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800525 aed->previousAllocExtLocation =
526 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700527 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700528 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800530 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800531 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800532 dptr = epos.bh->b_data +
533 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800535 epos.offset = sizeof(struct allocExtDesc) +
536 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700537 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700538 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700540 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700541 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800542 aed = (struct allocExtDesc *)
543 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100544 le32_add_cpu(&aed->lengthAllocDescs,
545 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700546 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800547 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800548 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800549 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 mark_inode_dirty(table);
551 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700552 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800554 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800555 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
556 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200557 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800559 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
560 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200561 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700562
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800563 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800564 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200565 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800566 sad->extLength = cpu_to_le32(
567 EXT_NEXT_EXTENT_ALLOCDECS |
568 sb->s_blocksize);
569 sad->extPosition =
570 cpu_to_le32(epos.block.logicalBlockNum);
571 break;
572 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200573 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800574 lad->extLength = cpu_to_le32(
575 EXT_NEXT_EXTENT_ALLOCDECS |
576 sb->s_blocksize);
577 lad->extLocation =
578 cpu_to_lelb(epos.block);
579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700581 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700582 udf_update_tag(oepos.bh->b_data, loffset);
583 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700584 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Marcin Slusarz4b111112008-02-08 04:20:36 -0800589 /* It's possible that stealing the block emptied the extent */
590 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200591 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800594 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700596 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700597 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100598 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700599 udf_update_tag(epos.bh->b_data, epos.offset);
600 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 }
603 }
604
Jan Kara3bf25cb2007-05-08 00:35:16 -0700605 brelse(epos.bh);
606 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700608error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800609 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
611}
612
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613static int udf_table_prealloc_blocks(struct super_block *sb,
614 struct inode *inode,
615 struct inode *table, uint16_t partition,
616 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct udf_sb_info *sbi = UDF_SB(sb);
619 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700620 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200621 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700622 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800624 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Roel Kluin3391faa2009-06-22 23:12:29 +0200626 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800629 iinfo = UDF_I(table);
630 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200631 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800632 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200633 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 else
635 return 0;
636
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800637 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700638 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800639 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700640 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 eloc.logicalBlockNum = 0xFFFFFFFF;
642
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700643 while (first_block != eloc.logicalBlockNum &&
644 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700646 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700647 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700650 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700651 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 alloc_count = (elen >> sb->s_blocksize_bits);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500654 if (inode && dquot_prealloc_block(inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800655 alloc_count > block_count ? block_count : alloc_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 alloc_count = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800657 else if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 alloc_count = block_count;
659 eloc.logicalBlockNum += alloc_count;
660 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200661 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800662 (etype << 30) | elen, 1);
663 } else
664 udf_delete_aext(table, epos, eloc,
665 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700666 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jan Kara3bf25cb2007-05-08 00:35:16 -0700670 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jan Kara146bca72009-03-16 18:27:37 +0100672 if (alloc_count)
673 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800674 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return alloc_count;
676}
677
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700678static int udf_table_new_block(struct super_block *sb,
679 struct inode *inode,
680 struct inode *table, uint16_t partition,
681 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct udf_sb_info *sbi = UDF_SB(sb);
684 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
685 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700686 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200687 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700688 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800690 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 *err = -ENOSPC;
693
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800694 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200695 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800696 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200697 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 else
699 return newblock;
700
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800701 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200702 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goal = 0;
704
Marcin Slusarz4b111112008-02-08 04:20:36 -0800705 /* We search for the closest matching block to goal. If we find
706 a exact hit, we stop. Otherwise we keep going till we run out
707 of extents. We store the buffer_head, bloc, and extoffset
708 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700709 */
Jan Karaff116fc2007-05-08 00:35:14 -0700710 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800711 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700712 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700714 while (spread &&
715 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700716 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800717 if (goal < eloc.logicalBlockNum +
718 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 nspread = 0;
720 else
721 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700722 (elen >> sb->s_blocksize_bits);
723 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700727 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700729 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700730 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700731 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700732 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Jan Karaff116fc2007-05-08 00:35:14 -0700734 goal_epos.block = epos.block;
735 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goal_eloc = eloc;
737 goal_elen = (etype << 30) | elen;
738 }
739 }
740
Jan Kara3bf25cb2007-05-08 00:35:16 -0700741 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700743 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700744 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800745 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747 }
748
749 /* Only allocate blocks from the beginning of the extent.
750 That way, we only delete (empty) extents, never have to insert an
751 extent because of splitting */
752 /* This works, but very poorly.... */
753
754 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700755 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 goal_elen -= sb->s_blocksize;
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500757 if (inode) {
758 *err = dquot_alloc_block(inode, 1);
759 if (*err) {
760 brelse(goal_epos.bh);
761 mutex_unlock(&sbi->s_alloc_mutex);
762 return 0;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200767 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 else
Jan Karaff116fc2007-05-08 00:35:14 -0700769 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700770 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Jan Kara146bca72009-03-16 18:27:37 +0100772 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800774 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *err = 0;
776 return newblock;
777}
778
Pekka Enberg97e961f2008-10-15 12:29:03 +0200779void udf_free_blocks(struct super_block *sb, struct inode *inode,
780 struct kernel_lb_addr *bloc, uint32_t offset,
781 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200783 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800784 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800786 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100787 udf_bitmap_free_blocks(sb, inode, map->s_uspace.s_bitmap,
788 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800789 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100790 udf_table_free_blocks(sb, inode, map->s_uspace.s_table,
791 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800792 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karae650b942008-12-01 13:06:10 +0100793 udf_bitmap_free_blocks(sb, inode, map->s_fspace.s_bitmap,
794 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800795 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karae650b942008-12-01 13:06:10 +0100796 udf_table_free_blocks(sb, inode, map->s_fspace.s_table,
797 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801inline int udf_prealloc_blocks(struct super_block *sb,
802 struct inode *inode,
803 uint16_t partition, uint32_t first_block,
804 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800806 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
807
Marcin Slusarz4b111112008-02-08 04:20:36 -0800808 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800810 map->s_uspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 partition, first_block,
812 block_count);
813 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800815 map->s_uspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800816 partition, first_block,
817 block_count);
818 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return udf_bitmap_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800820 map->s_fspace.s_bitmap,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 partition, first_block,
822 block_count);
823 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return udf_table_prealloc_blocks(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800825 map->s_fspace.s_table,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800826 partition, first_block,
827 block_count);
828 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700832inline int udf_new_block(struct super_block *sb,
833 struct inode *inode,
834 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800836 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Kara3bf25cb2007-05-08 00:35:16 -0700837
Marcin Slusarz4b111112008-02-08 04:20:36 -0800838 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
839 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 map->s_uspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700841 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800842 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800844 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700845 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800846 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return udf_bitmap_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800848 map->s_fspace.s_bitmap,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700849 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800850 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return udf_table_new_block(sb, inode,
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800852 map->s_fspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700853 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800854 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 *err = -EIO;
856 return 0;
857 }
858}