blob: 0ee257a32e4563af1a350da4631d94c5c4d6511a [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/buffer_head.h>
25#include <linux/bitops.h>
26
27#include "udf_i.h"
28#include "udf_sb.h"
29
Akinobu Mita9ad1e1e2011-03-23 16:42:11 -070030#define udf_clear_bit __test_and_clear_bit_le
31#define udf_set_bit __test_and_set_bit_le
32#define udf_test_bit test_bit_le
33#define udf_find_next_one_bit find_next_bit_le
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070035static int read_block_bitmap(struct super_block *sb,
36 struct udf_bitmap *bitmap, unsigned int block,
37 unsigned long bitmap_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 struct buffer_head *bh = NULL;
40 int retval = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020041 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080044 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Pekka Enberg97e961f2008-10-15 12:29:03 +020046 bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
Marcin Slusarz4b111112008-02-08 04:20:36 -080047 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 retval = -EIO;
Marcin Slusarz4b111112008-02-08 04:20:36 -080049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 bitmap->s_block_bitmap[bitmap_nr] = bh;
51 return retval;
52}
53
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054static int __load_block_bitmap(struct super_block *sb,
55 struct udf_bitmap *bitmap,
56 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 int retval = 0;
59 int nr_groups = bitmap->s_nr_groups;
60
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061 if (block_group >= nr_groups) {
Joe Perchesa983f362011-10-10 01:08:07 -070062 udf_debug("block_group (%d) > nr_groups (%d)\n",
63 block_group, nr_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
Fabian Frederick6fbaad82015-03-10 21:44:33 +010066 if (bitmap->s_block_bitmap[block_group])
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return block_group;
Fabian Frederick6fbaad82015-03-10 21:44:33 +010068
69 retval = read_block_bitmap(sb, bitmap, block_group, block_group);
70 if (retval < 0)
71 return retval;
72
73 return block_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070076static inline int load_block_bitmap(struct super_block *sb,
77 struct udf_bitmap *bitmap,
78 unsigned int block_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 int slot;
81
82 slot = __load_block_bitmap(sb, bitmap, block_group);
83
84 if (slot < 0)
85 return slot;
86
87 if (!bitmap->s_block_bitmap[slot])
88 return -EIO;
89
90 return slot;
91}
92
Jan Kara146bca72009-03-16 18:27:37 +010093static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
Marcin Slusarz742ba022008-02-08 04:20:40 -080094{
Jan Kara146bca72009-03-16 18:27:37 +010095 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -080096 struct logicalVolIntegrityDesc *lvid;
97
Jan Kara146bca72009-03-16 18:27:37 +010098 if (!sbi->s_lvid_bh)
99 return;
Marcin Slusarz742ba022008-02-08 04:20:40 -0800100
101 lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100102 le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
Jan Kara146bca72009-03-16 18:27:37 +0100103 udf_updated_lvid(sb);
Marcin Slusarz742ba022008-02-08 04:20:40 -0800104}
105
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700106static void udf_bitmap_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 struct udf_bitmap *bitmap,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200108 struct kernel_lb_addr *bloc,
109 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 struct buffer_head *bh = NULL;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200114 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned long block;
116 unsigned long block_group;
117 unsigned long bit;
118 unsigned long i;
119 int bitmap_nr;
120 unsigned long overflow;
121
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800122 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200123 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300124 if (bloc->logicalBlockNum + count < count ||
125 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700126 udf_debug("%d < %d || %d + %d > %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700127 bloc->logicalBlockNum, 0,
128 bloc->logicalBlockNum, count,
129 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto error_return;
131 }
132
Pekka Enberg97e961f2008-10-15 12:29:03 +0200133 block = bloc->logicalBlockNum + offset +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800134 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800136 do {
137 overflow = 0;
138 block_group = block >> (sb->s_blocksize_bits + 3);
139 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800141 /*
142 * Check to see if we are freeing blocks across a group boundary.
143 */
144 if (bit + count > (sb->s_blocksize << 3)) {
145 overflow = bit + count - (sb->s_blocksize << 3);
146 count -= overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800148 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
149 if (bitmap_nr < 0)
150 goto error_return;
151
152 bh = bitmap->s_block_bitmap[bitmap_nr];
153 for (i = 0; i < count; i++) {
154 if (udf_set_bit(bit + i, bh->b_data)) {
155 udf_debug("bit %ld already set\n", bit + i);
156 udf_debug("byte=%2x\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700157 ((char *)bh->b_data)[(bit + i) >> 3]);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800158 }
159 }
Jan Kara7abc2e42010-10-20 22:32:02 +0200160 udf_add_free_space(sb, sbi->s_partition, count);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800161 mark_buffer_dirty(bh);
162 if (overflow) {
163 block += count;
164 count = overflow;
165 }
166 } while (overflow);
167
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700168error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800169 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700172static int udf_bitmap_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700173 struct udf_bitmap *bitmap,
174 uint16_t partition, uint32_t first_block,
175 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct udf_sb_info *sbi = UDF_SB(sb);
178 int alloc_count = 0;
179 int bit, block, block_group, group_start;
180 int nr_groups, bitmap_nr;
181 struct buffer_head *bh;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800182 __u32 part_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800184 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800185 part_len = sbi->s_partmaps[partition].s_partition_len;
Roel Kluin3391faa2009-06-22 23:12:29 +0200186 if (first_block >= part_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto out;
188
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800189 if (first_block + block_count > part_len)
190 block_count = part_len - first_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800192 do {
193 nr_groups = udf_compute_nr_groups(sb, partition);
194 block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
195 block_group = block >> (sb->s_blocksize_bits + 3);
196 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800198 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
199 if (bitmap_nr < 0)
200 goto out;
201 bh = bitmap->s_block_bitmap[bitmap_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800203 bit = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800205 while (bit < (sb->s_blocksize << 3) && block_count > 0) {
Jan Kara36350462010-05-19 16:28:56 +0200206 if (!udf_clear_bit(bit, bh->b_data))
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800207 goto out;
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800208 block_count--;
209 alloc_count++;
210 bit++;
211 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800213 mark_buffer_dirty(bh);
214 } while (block_count > 0);
215
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216out:
Jan Kara146bca72009-03-16 18:27:37 +0100217 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800218 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return alloc_count;
220}
221
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700222static int udf_bitmap_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223 struct udf_bitmap *bitmap, uint16_t partition,
224 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700227 int newbit, bit = 0, block, block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int end_goal, nr_groups, bitmap_nr, i;
229 struct buffer_head *bh = NULL;
230 char *ptr;
231 int newblock = 0;
232
233 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800234 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200237 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goal = 0;
239
240 nr_groups = bitmap->s_nr_groups;
241 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
242 block_group = block >> (sb->s_blocksize_bits + 3);
243 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
244
245 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
246 if (bitmap_nr < 0)
247 goto error_return;
248 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700249 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
250 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700252 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700254 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 end_goal = (bit + 63) & ~63;
258 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
259 if (bit < end_goal)
260 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700261
Marcin Slusarz4b111112008-02-08 04:20:36 -0800262 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
263 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700265 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 bit = newbit;
267 goto search_back;
268 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700269
Marcin Slusarz4b111112008-02-08 04:20:36 -0800270 newbit = udf_find_next_one_bit(bh->b_data,
271 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700272 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 bit = newbit;
274 goto got_block;
275 }
276 }
277
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700278 for (i = 0; i < (nr_groups * 2); i++) {
279 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (block_group >= nr_groups)
281 block_group = 0;
282 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
283
284 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
285 if (bitmap_nr < 0)
286 goto error_return;
287 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700289 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
290 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 bit = (ptr - ((char *)bh->b_data)) << 3;
293 break;
294 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700295 } else {
Dirk Behme6f644e52011-02-22 14:04:19 -0500296 bit = udf_find_next_one_bit(bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700297 sb->s_blocksize << 3,
298 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (bit < sb->s_blocksize << 3)
300 break;
301 }
302 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700303 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800304 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return newblock;
306 }
307 if (bit < sb->s_blocksize << 3)
308 goto search_back;
309 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800310 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
311 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700312 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800313 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315 }
316
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700317search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800318 i = 0;
319 while (i < 7 && bit > (group_start << 3) &&
320 udf_test_bit(bit - 1, bh->b_data)) {
321 ++i;
322 --bit;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700325got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700327 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700329 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 udf_debug("bit already cleared for block %d\n", bit);
331 goto repeat;
332 }
333
334 mark_buffer_dirty(bh);
335
Jan Kara146bca72009-03-16 18:27:37 +0100336 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800337 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *err = 0;
339 return newblock;
340
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700341error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800343 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345}
346
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347static void udf_table_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200349 struct kernel_lb_addr *bloc,
350 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700351 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200354 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700356 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200357 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700358 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800360 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800362 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200363 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300364 if (bloc->logicalBlockNum + count < count ||
365 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700366 udf_debug("%d < %d || %d + %d > %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700367 bloc->logicalBlockNum, 0,
368 bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200369 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto error_return;
371 }
372
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800373 iinfo = UDF_I(table);
Jan Kara146bca72009-03-16 18:27:37 +0100374 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Pekka Enberg97e961f2008-10-15 12:29:03 +0200376 start = bloc->logicalBlockNum + offset;
377 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jan Karaff116fc2007-05-08 00:35:14 -0700379 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800381 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700382 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700384 while (count &&
385 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800386 if (((eloc.logicalBlockNum +
387 (elen >> sb->s_blocksize_bits)) == start)) {
388 if ((0x3FFFFFFF - elen) <
389 (count << sb->s_blocksize_bits)) {
390 uint32_t tmp = ((0x3FFFFFFF - elen) >>
391 sb->s_blocksize_bits);
392 count -= tmp;
393 start += tmp;
394 elen = (etype << 30) |
395 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700396 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800397 elen = (etype << 30) |
398 (elen +
399 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 start += count;
401 count = 0;
402 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200403 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700404 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800405 if ((0x3FFFFFFF - elen) <
406 (count << sb->s_blocksize_bits)) {
407 uint32_t tmp = ((0x3FFFFFFF - elen) >>
408 sb->s_blocksize_bits);
409 count -= tmp;
410 end -= tmp;
411 eloc.logicalBlockNum -= tmp;
412 elen = (etype << 30) |
413 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700414 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800416 elen = (etype << 30) |
417 (elen +
418 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 end -= count;
420 count = 0;
421 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200422 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700425 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700426 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700427 brelse(oepos.bh);
428 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700429 oepos.bh = epos.bh;
430 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700431 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700432 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700436 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700437 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800438 * NOTE: we CANNOT use udf_add_aext here, as it can try to
439 * allocate a new block, and since we hold the super block
440 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700441 *
442 * We copy the behavior of udf_add_aext, but instead of
443 * trying to allocate a new block close to the existing one,
444 * we just steal a block from the extent we are trying to add.
445 *
446 * It would be nice if the blocks were close together, but it
447 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200451 struct short_ad *sad = NULL;
452 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct allocExtDesc *aed;
454
455 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700456 elen = EXT_RECORDED_ALLOCATED |
457 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800459 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200460 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800461 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200462 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800463 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700464 brelse(oepos.bh);
465 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto error_return;
467 }
468
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700469 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -0500470 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700472
Jan Kara3bf25cb2007-05-08 00:35:16 -0700473 brelse(oepos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700474 oepos = epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Steal a block from the extent being free'd */
Jan Karaff116fc2007-05-08 00:35:14 -0700477 epos.block.logicalBlockNum = eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700478 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 elen -= sb->s_blocksize;
480
Marcin Slusarz4b111112008-02-08 04:20:36 -0800481 epos.bh = udf_tread(sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200482 udf_get_lb_pblock(sb, &epos.block, 0));
Marcin Slusarz4b111112008-02-08 04:20:36 -0800483 if (!epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700484 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto error_return;
486 }
Jan Karaff116fc2007-05-08 00:35:14 -0700487 aed = (struct allocExtDesc *)(epos.bh->b_data);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800488 aed->previousAllocExtLocation =
489 cpu_to_le32(oepos.block.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700490 if (epos.offset + adsize > sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700491 loffset = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 aed->lengthAllocDescs = cpu_to_le32(adsize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800493 sptr = iinfo->i_ext.i_data + epos.offset
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800494 - adsize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800495 dptr = epos.bh->b_data +
496 sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 memcpy(dptr, sptr, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800498 epos.offset = sizeof(struct allocExtDesc) +
499 adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700500 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700501 loffset = epos.offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 aed->lengthAllocDescs = cpu_to_le32(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700503 if (oepos.bh) {
Jan Karaf5cc15d2007-08-30 23:56:22 -0700504 sptr = oepos.bh->b_data + epos.offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800505 aed = (struct allocExtDesc *)
506 oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100507 le32_add_cpu(&aed->lengthAllocDescs,
508 adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700509 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800510 sptr = iinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800511 epos.offset;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800512 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 mark_inode_dirty(table);
514 }
Jan Karaf5cc15d2007-08-30 23:56:22 -0700515 epos.offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800517 if (sbi->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800518 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
519 3, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200520 sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800522 udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
523 2, 1, epos.block.logicalBlockNum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200524 sizeof(struct tag));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700525
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800526 switch (iinfo->i_alloc_type) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800527 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200528 sad = (struct short_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800529 sad->extLength = cpu_to_le32(
530 EXT_NEXT_EXTENT_ALLOCDECS |
531 sb->s_blocksize);
532 sad->extPosition =
533 cpu_to_le32(epos.block.logicalBlockNum);
534 break;
535 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200536 lad = (struct long_ad *)sptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800537 lad->extLength = cpu_to_le32(
538 EXT_NEXT_EXTENT_ALLOCDECS |
539 sb->s_blocksize);
540 lad->extLocation =
541 cpu_to_lelb(epos.block);
542 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 if (oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700545 udf_update_tag(oepos.bh->b_data, loffset);
546 mark_buffer_dirty(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700547 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 mark_inode_dirty(table);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
Marcin Slusarz4b111112008-02-08 04:20:36 -0800552 /* It's possible that stealing the block emptied the extent */
553 if (elen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200554 udf_write_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800557 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 mark_inode_dirty(table);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700559 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700560 aed = (struct allocExtDesc *)epos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100561 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -0700562 udf_update_tag(epos.bh->b_data, epos.offset);
563 mark_buffer_dirty(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 }
566 }
567
Jan Kara3bf25cb2007-05-08 00:35:16 -0700568 brelse(epos.bh);
569 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700571error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800572 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return;
574}
575
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700576static int udf_table_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700577 struct inode *table, uint16_t partition,
578 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct udf_sb_info *sbi = UDF_SB(sb);
581 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700582 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200583 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700584 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800586 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Roel Kluin3391faa2009-06-22 23:12:29 +0200588 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800591 iinfo = UDF_I(table);
592 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200593 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800594 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200595 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 else
597 return 0;
598
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800599 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700600 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800601 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700602 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 eloc.logicalBlockNum = 0xFFFFFFFF;
604
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700605 while (first_block != eloc.logicalBlockNum &&
606 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 udf_debug("eloc=%d, elen=%d, first_block=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700608 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700609 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700612 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700613 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Kara36350462010-05-19 16:28:56 +0200616 if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 alloc_count = block_count;
618 eloc.logicalBlockNum += alloc_count;
619 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200620 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800621 (etype << 30) | elen, 1);
622 } else
623 udf_delete_aext(table, epos, eloc,
624 (etype << 30) | elen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700625 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jan Kara3bf25cb2007-05-08 00:35:16 -0700629 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jan Kara146bca72009-03-16 18:27:37 +0100631 if (alloc_count)
632 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800633 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return alloc_count;
635}
636
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700637static int udf_table_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700638 struct inode *table, uint16_t partition,
639 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct udf_sb_info *sbi = UDF_SB(sb);
642 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
643 uint32_t newblock = 0, adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700644 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200645 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700646 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800648 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 *err = -ENOSPC;
651
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800652 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200653 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800654 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200655 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 else
657 return newblock;
658
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800659 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200660 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 goal = 0;
662
Marcin Slusarz4b111112008-02-08 04:20:36 -0800663 /* We search for the closest matching block to goal. If we find
664 a exact hit, we stop. Otherwise we keep going till we run out
665 of extents. We store the buffer_head, bloc, and extoffset
666 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700667 */
Jan Karaff116fc2007-05-08 00:35:14 -0700668 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800669 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700670 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700672 while (spread &&
673 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700674 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800675 if (goal < eloc.logicalBlockNum +
676 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 nspread = 0;
678 else
679 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700680 (elen >> sb->s_blocksize_bits);
681 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700685 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700687 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700688 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700689 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700690 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Jan Karaff116fc2007-05-08 00:35:14 -0700692 goal_epos.block = epos.block;
693 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goal_eloc = eloc;
695 goal_elen = (etype << 30) | elen;
696 }
697 }
698
Jan Kara3bf25cb2007-05-08 00:35:16 -0700699 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700701 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700702 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800703 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705 }
706
707 /* Only allocate blocks from the beginning of the extent.
708 That way, we only delete (empty) extents, never have to insert an
709 extent because of splitting */
710 /* This works, but very poorly.... */
711
712 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700713 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goal_elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200717 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 else
Jan Karaff116fc2007-05-08 00:35:14 -0700719 udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700720 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Jan Kara146bca72009-03-16 18:27:37 +0100722 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800724 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *err = 0;
726 return newblock;
727}
728
Pekka Enberg97e961f2008-10-15 12:29:03 +0200729void udf_free_blocks(struct super_block *sb, struct inode *inode,
730 struct kernel_lb_addr *bloc, uint32_t offset,
731 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200733 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800734 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800736 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100737 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100738 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800739 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100740 udf_table_free_blocks(sb, map->s_uspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100741 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800742 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100743 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100744 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800745 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100746 udf_table_free_blocks(sb, map->s_fspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100747 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700748 }
Jan Karafd4287d2012-02-16 13:00:14 +0100749
750 if (inode) {
751 inode_sub_bytes(inode,
752 ((sector_t)count) << sb->s_blocksize_bits);
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756inline int udf_prealloc_blocks(struct super_block *sb,
757 struct inode *inode,
758 uint16_t partition, uint32_t first_block,
759 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800761 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Karafd4287d2012-02-16 13:00:14 +0100762 sector_t allocated;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800763
Marcin Slusarz4b111112008-02-08 04:20:36 -0800764 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100765 allocated = udf_bitmap_prealloc_blocks(sb,
766 map->s_uspace.s_bitmap,
767 partition, first_block,
768 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800769 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100770 allocated = udf_table_prealloc_blocks(sb,
771 map->s_uspace.s_table,
772 partition, first_block,
773 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800774 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100775 allocated = udf_bitmap_prealloc_blocks(sb,
776 map->s_fspace.s_bitmap,
777 partition, first_block,
778 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800779 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100780 allocated = udf_table_prealloc_blocks(sb,
781 map->s_fspace.s_table,
782 partition, first_block,
783 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800784 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
Jan Karafd4287d2012-02-16 13:00:14 +0100786
787 if (inode && allocated > 0)
788 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
789 return allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792inline int udf_new_block(struct super_block *sb,
793 struct inode *inode,
794 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800796 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Jan Karafd4287d2012-02-16 13:00:14 +0100797 int block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700798
Marcin Slusarz4b111112008-02-08 04:20:36 -0800799 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100800 block = udf_bitmap_new_block(sb,
801 map->s_uspace.s_bitmap,
802 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800803 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100804 block = udf_table_new_block(sb,
805 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700806 partition, goal, err);
Jan Karafd4287d2012-02-16 13:00:14 +0100807 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
808 block = udf_bitmap_new_block(sb,
809 map->s_fspace.s_bitmap,
810 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100812 block = udf_table_new_block(sb,
813 map->s_fspace.s_table,
814 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800815 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *err = -EIO;
817 return 0;
818 }
Jan Karafd4287d2012-02-16 13:00:14 +0100819 if (inode && block)
820 inode_add_bytes(inode, sb->s_blocksize);
821 return block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}