blob: 6d6a96b4e73fe20ba15edc1213564613cb201bbd [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/bitops.h>
25
26#include "udf_i.h"
27#include "udf_sb.h"
28
Akinobu Mita9ad1e1e2011-03-23 16:42:11 -070029#define udf_clear_bit __test_and_clear_bit_le
30#define udf_set_bit __test_and_set_bit_le
31#define udf_test_bit test_bit_le
32#define udf_find_next_one_bit find_next_bit_le
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070034static int read_block_bitmap(struct super_block *sb,
35 struct udf_bitmap *bitmap, unsigned int block,
36 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 struct buffer_head *bh = NULL;
39 int retval = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020040 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080043 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Pekka Enberg97e961f2008-10-15 12:29:03 +020045 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080046 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 bitmap->s_block_bitmap[bitmap_nr] = bh;
50 return retval;
51}
52
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053static int __load_block_bitmap(struct super_block *sb,
54 struct udf_bitmap *bitmap,
55 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 int retval = 0;
58 int nr_groups = bitmap->s_nr_groups;
59
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070060 if (block_group >= nr_groups) {
Joe Perchesa983f362011-10-10 01:08:07 -070061 udf_debug("block_group (%d) > nr_groups (%d)\n",
62 block_group, nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
Fabian Frederick6fbaad82015-03-10 21:44:33 +010065 if (bitmap->s_block_bitmap[block_group])
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return block_group;
Fabian Frederick6fbaad82015-03-10 21:44:33 +010067
68 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
69 if (retval < 0)
70 return retval;
71
72 return block_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070075static inline int load_block_bitmap(struct super_block *sb,
76 struct udf_bitmap *bitmap,
77 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int slot;
80
81 slot = __load_block_bitmap(sb, bitmap, block_group);
82
83 if (slot < 0)
84 return slot;
85
86 if (!bitmap->s_block_bitmap[slot])
87 return -EIO;
88
89 return slot;
90}
91
Jan Kara146bca72009-03-16 18:27:37 +010092static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
Marcin Slusarz742ba022008-02-08 04:20:40 -080093{
Jan Kara146bca72009-03-16 18:27:37 +010094 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -080095 struct logicalVolIntegrityDesc *lvid;
96
Jan Kara146bca72009-03-16 18:27:37 +010097 if (!sbi->s_lvid_bh)
98 return;
Marcin Slusarz742ba022008-02-08 04:20:40 -080099
100 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100101 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
Jan Kara146bca72009-03-16 18:27:37 +0100102 udf_updated_lvid(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800103}
104
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700105static void udf_bitmap_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700106 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200107 struct kernel_lb_addr *bloc,
108 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700109 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700112 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200113 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned long block;
115 unsigned long block_group;
116 unsigned long bit;
117 unsigned long i;
118 int bitmap_nr;
119 unsigned long overflow;
120
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800121 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200122 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300123 if (bloc->logicalBlockNum + count < count ||
124 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700125 udf_debug("%d < %d || %d + %d > %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700126 bloc->logicalBlockNum, 0,
127 bloc->logicalBlockNum, count,
128 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto error_return;
130 }
131
Pekka Enberg97e961f2008-10-15 12:29:03 +0200132 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800133 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800135 do {
136 overflow = 0;
137 block_group = block >> (sb->s_blocksize_bits + 3);
138 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800140 /*
141 * Check to see if we are freeing blocks across a group boundary.
142 */
143 if (bit + count > (sb->s_blocksize << 3)) {
144 overflow = bit + count - (sb->s_blocksize << 3);
145 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800147 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
148 if (bitmap_nr < 0)
149 goto error_return;
150
151 bh = bitmap->s_block_bitmap[bitmap_nr];
152 for (i = 0; i < count; i++) {
153 if (udf_set_bit(bit + i, bh->b_data)) {
154 udf_debug("bit %ld already set\n", bit + i);
155 udf_debug("byte=%2x\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700156 ((char *)bh->b_data)[(bit + i) >> 3]);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800157 }
158 }
Jan Kara7abc2e42010-10-20 22:32:02 +0200159 udf_add_free_space(sb, sbi->s_partition, count);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800160 mark_buffer_dirty(bh);
161 if (overflow) {
162 block += count;
163 count = overflow;
164 }
165 } while (overflow);
166
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700167error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800168 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700171static int udf_bitmap_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700172 struct udf_bitmap *bitmap,
173 uint16_t partition, uint32_t first_block,
174 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct udf_sb_info *sbi = UDF_SB(sb);
177 int alloc_count = 0;
178 int bit, block, block_group, group_start;
179 int nr_groups, bitmap_nr;
180 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800181 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800183 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800184 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200185 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out;
187
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800188 if (first_block + block_count > part_len)
189 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800191 do {
192 nr_groups = udf_compute_nr_groups(sb, partition);
193 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
194 block_group = block >> (sb->s_blocksize_bits + 3);
195 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800197 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
198 if (bitmap_nr < 0)
199 goto out;
200 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800202 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800204 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Jan Kara36350462010-05-19 16:28:56 +0200205 if (!udf_clear_bit(bit, bh->b_data))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800206 goto out;
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800207 block_count--;
208 alloc_count++;
209 bit++;
210 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800212 mark_buffer_dirty(bh);
213 } while (block_count > 0);
214
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700215out:
Jan Kara146bca72009-03-16 18:27:37 +0100216 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800217 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return alloc_count;
219}
220
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700221static int udf_bitmap_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700222 struct udf_bitmap *bitmap, uint16_t partition,
223 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int end_goal, nr_groups, bitmap_nr, i;
228 struct buffer_head *bh = NULL;
229 char *ptr;
230 int newblock = 0;
231
232 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800233 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700235repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200236 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goal = 0;
238
239 nr_groups = bitmap->s_nr_groups;
240 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
241 block_group = block >> (sb->s_blocksize_bits + 3);
242 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
243
244 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
245 if (bitmap_nr < 0)
246 goto error_return;
247 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700248 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
249 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700251 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700253 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 end_goal = (bit + 63) & ~63;
257 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
258 if (bit < end_goal)
259 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700260
Marcin Slusarz4b111112008-02-08 04:20:36 -0800261 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
262 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700264 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 bit = newbit;
266 goto search_back;
267 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700268
Marcin Slusarz4b111112008-02-08 04:20:36 -0800269 newbit = udf_find_next_one_bit(bh->b_data,
270 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700271 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 bit = newbit;
273 goto got_block;
274 }
275 }
276
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700277 for (i = 0; i < (nr_groups * 2); i++) {
278 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (block_group >= nr_groups)
280 block_group = 0;
281 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
282
283 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
284 if (bitmap_nr < 0)
285 goto error_return;
286 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700287 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700288 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
289 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700290 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 bit = (ptr - ((char *)bh->b_data)) << 3;
292 break;
293 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 } else {
Dirk Behme6f644e52011-02-22 14:04:19 -0500295 bit = udf_find_next_one_bit(bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700296 sb->s_blocksize << 3,
297 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (bit < sb->s_blocksize << 3)
299 break;
300 }
301 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700302 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800303 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return newblock;
305 }
306 if (bit < sb->s_blocksize << 3)
307 goto search_back;
308 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800309 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
310 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700311 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800312 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314 }
315
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700316search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800317 i = 0;
318 while (i < 7 && bit > (group_start << 3) &&
319 udf_test_bit(bit - 1, bh->b_data)) {
320 ++i;
321 --bit;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700324got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700326 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 udf_debug("bit already cleared for block %d\n", bit);
330 goto repeat;
331 }
332
333 mark_buffer_dirty(bh);
334
Jan Kara146bca72009-03-16 18:27:37 +0100335 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800336 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *err = 0;
338 return newblock;
339
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700340error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800342 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346static void udf_table_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200348 struct kernel_lb_addr *bloc,
349 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700350 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200353 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700355 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200356 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700357 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800359 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800361 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200362 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300363 if (bloc->logicalBlockNum + count < count ||
364 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700365 udf_debug("%d < %d || %d + %d > %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700366 bloc->logicalBlockNum, 0,
367 bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200368 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto error_return;
370 }
371
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800372 iinfo = UDF_I(table);
Jan Kara146bca72009-03-16 18:27:37 +0100373 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Pekka Enberg97e961f2008-10-15 12:29:03 +0200375 start = bloc->logicalBlockNum + offset;
376 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jan Karaff116fc2007-05-08 00:35:14 -0700378 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800380 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700381 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700383 while (count &&
384 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800385 if (((eloc.logicalBlockNum +
386 (elen >> sb->s_blocksize_bits)) == start)) {
387 if ((0x3FFFFFFF - elen) <
388 (count << sb->s_blocksize_bits)) {
389 uint32_t tmp = ((0x3FFFFFFF - elen) >>
390 sb->s_blocksize_bits);
391 count -= tmp;
392 start += tmp;
393 elen = (etype << 30) |
394 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700395 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800396 elen = (etype << 30) |
397 (elen +
398 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 start += count;
400 count = 0;
401 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200402 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700403 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800404 if ((0x3FFFFFFF - elen) <
405 (count << sb->s_blocksize_bits)) {
406 uint32_t tmp = ((0x3FFFFFFF - elen) >>
407 sb->s_blocksize_bits);
408 count -= tmp;
409 end -= tmp;
410 eloc.logicalBlockNum -= tmp;
411 elen = (etype << 30) |
412 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700413 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800415 elen = (etype << 30) |
416 (elen +
417 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 end -= count;
419 count = 0;
420 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200421 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700425 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700426 brelse(oepos.bh);
427 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700428 oepos.bh = epos.bh;
429 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700430 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700431 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700435 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800437 * NOTE: we CANNOT use udf_add_aext here, as it can try to
438 * allocate a new block, and since we hold the super block
439 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700440 *
441 * We copy the behavior of udf_add_aext, but instead of
442 * trying to allocate a new block close to the existing one,
443 * we just steal a block from the extent we are trying to add.
444 *
445 * It would be nice if the blocks were close together, but it
446 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200450 struct short_ad *sad = NULL;
451 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct allocExtDesc *aed;
453
454 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700455 elen = EXT_RECORDED_ALLOCATED |
456 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800458 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200459 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800460 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200461 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800462 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700463 brelse(oepos.bh);
464 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto error_return;
466 }
467
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500469 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471
Jan Kara3bf25cb2007-05-08 00:35:16 -0700472 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700473 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700476 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700477 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 elen -= sb->s_blocksize;
479
Marcin Slusarz4b111112008-02-08 04:20:36 -0800480 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200481 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800482 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700483 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto error_return;
485 }
Jan Karaff116fc2007-05-08 00:35:14 -0700486 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800487 aed->previousAllocExtLocation =
488 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700489 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700490 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800492 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800493 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800494 dptr = epos.bh->b_data +
495 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800497 epos.offset = sizeof(struct allocExtDesc) +
498 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700499 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700500 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700502 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700503 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800504 aed = (struct allocExtDesc *)
505 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100506 le32_add_cpu(&aed->lengthAllocDescs,
507 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700508 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800509 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800510 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800511 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 mark_inode_dirty(table);
513 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700514 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800516 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800517 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
518 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200519 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800521 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
522 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200523 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700524
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800525 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800526 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200527 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800528 sad->extLength = cpu_to_le32(
529 EXT_NEXT_EXTENT_ALLOCDECS |
530 sb->s_blocksize);
531 sad->extPosition =
532 cpu_to_le32(epos.block.logicalBlockNum);
533 break;
534 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200535 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800536 lad->extLength = cpu_to_le32(
537 EXT_NEXT_EXTENT_ALLOCDECS |
538 sb->s_blocksize);
539 lad->extLocation =
540 cpu_to_lelb(epos.block);
541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700543 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700544 udf_update_tag(oepos.bh->b_data, loffset);
545 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
Marcin Slusarz4b111112008-02-08 04:20:36 -0800551 /* It's possible that stealing the block emptied the extent */
552 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200553 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800556 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700558 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700559 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100560 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700561 udf_update_tag(epos.bh->b_data, epos.offset);
562 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 }
565 }
566
Jan Kara3bf25cb2007-05-08 00:35:16 -0700567 brelse(epos.bh);
568 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700570error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800571 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
573}
574
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575static int udf_table_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700576 struct inode *table, uint16_t partition,
577 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct udf_sb_info *sbi = UDF_SB(sb);
580 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700581 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200582 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700583 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800585 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Roel Kluin3391faa2009-06-22 23:12:29 +0200587 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 0;
589
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800590 iinfo = UDF_I(table);
591 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200592 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800593 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200594 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 else
596 return 0;
597
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800598 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700599 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800600 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700601 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 eloc.logicalBlockNum = 0xFFFFFFFF;
603
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700604 while (first_block != eloc.logicalBlockNum &&
605 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700607 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700608 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700612 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Kara36350462010-05-19 16:28:56 +0200615 if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 alloc_count = block_count;
617 eloc.logicalBlockNum += alloc_count;
618 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200619 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800620 (etype << 30) | elen, 1);
621 } else
622 udf_delete_aext(table, epos, eloc,
623 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700624 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jan Kara3bf25cb2007-05-08 00:35:16 -0700628 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jan Kara146bca72009-03-16 18:27:37 +0100630 if (alloc_count)
631 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800632 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return alloc_count;
634}
635
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636static int udf_table_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700637 struct inode *table, uint16_t partition,
638 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct udf_sb_info *sbi = UDF_SB(sb);
641 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
642 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700643 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200644 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700645 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800647 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 *err = -ENOSPC;
650
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800651 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200652 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800653 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200654 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 else
656 return newblock;
657
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800658 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200659 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goal = 0;
661
Marcin Slusarz4b111112008-02-08 04:20:36 -0800662 /* We search for the closest matching block to goal. If we find
663 a exact hit, we stop. Otherwise we keep going till we run out
664 of extents. We store the buffer_head, bloc, and extoffset
665 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700666 */
Jan Karaff116fc2007-05-08 00:35:14 -0700667 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800668 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700669 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700671 while (spread &&
672 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700673 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800674 if (goal < eloc.logicalBlockNum +
675 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 nspread = 0;
677 else
678 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700679 (elen >> sb->s_blocksize_bits);
680 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700684 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700686 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700687 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700688 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700689 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Jan Karaff116fc2007-05-08 00:35:14 -0700691 goal_epos.block = epos.block;
692 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goal_eloc = eloc;
694 goal_elen = (etype << 30) | elen;
695 }
696 }
697
Jan Kara3bf25cb2007-05-08 00:35:16 -0700698 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700700 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700701 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800702 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704 }
705
706 /* Only allocate blocks from the beginning of the extent.
707 That way, we only delete (empty) extents, never have to insert an
708 extent because of splitting */
709 /* This works, but very poorly.... */
710
711 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goal_elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200716 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 else
Jan Karaff116fc2007-05-08 00:35:14 -0700718 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700719 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jan Kara146bca72009-03-16 18:27:37 +0100721 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800723 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 *err = 0;
725 return newblock;
726}
727
Pekka Enberg97e961f2008-10-15 12:29:03 +0200728void udf_free_blocks(struct super_block *sb, struct inode *inode,
729 struct kernel_lb_addr *bloc, uint32_t offset,
730 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200732 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800733 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800735 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100736 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100737 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800738 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100739 udf_table_free_blocks(sb, map->s_uspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100740 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800741 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100742 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100743 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800744 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100745 udf_table_free_blocks(sb, map->s_fspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100746 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700747 }
Jan Karafd4287d2012-02-16 13:00:14 +0100748
749 if (inode) {
750 inode_sub_bytes(inode,
751 ((sector_t)count) << sb->s_blocksize_bits);
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700755inline int udf_prealloc_blocks(struct super_block *sb,
756 struct inode *inode,
757 uint16_t partition, uint32_t first_block,
758 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800760 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Fabian Frederick1be440d2015-03-15 10:54:23 +0100761 int allocated;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800762
Marcin Slusarz4b111112008-02-08 04:20:36 -0800763 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100764 allocated = udf_bitmap_prealloc_blocks(sb,
765 map->s_uspace.s_bitmap,
766 partition, first_block,
767 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800768 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100769 allocated = udf_table_prealloc_blocks(sb,
770 map->s_uspace.s_table,
771 partition, first_block,
772 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800773 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100774 allocated = udf_bitmap_prealloc_blocks(sb,
775 map->s_fspace.s_bitmap,
776 partition, first_block,
777 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800778 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100779 allocated = udf_table_prealloc_blocks(sb,
780 map->s_fspace.s_table,
781 partition, first_block,
782 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800783 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
Jan Karafd4287d2012-02-16 13:00:14 +0100785
786 if (inode && allocated > 0)
787 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
788 return allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791inline int udf_new_block(struct super_block *sb,
792 struct inode *inode,
793 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800795 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Karafd4287d2012-02-16 13:00:14 +0100796 int block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700797
Marcin Slusarz4b111112008-02-08 04:20:36 -0800798 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100799 block = udf_bitmap_new_block(sb,
800 map->s_uspace.s_bitmap,
801 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800802 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100803 block = udf_table_new_block(sb,
804 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700805 partition, goal, err);
Jan Karafd4287d2012-02-16 13:00:14 +0100806 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
807 block = udf_bitmap_new_block(sb,
808 map->s_fspace.s_bitmap,
809 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800810 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100811 block = udf_table_new_block(sb,
812 map->s_fspace.s_table,
813 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800814 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 *err = -EIO;
816 return 0;
817 }
Jan Karafd4287d2012-02-16 13:00:14 +0100818 if (inode && block)
819 inode_add_bytes(inode, sb->s_blocksize);
820 return block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}