blob: fcda0fc97b90a14fd53aafbeb15885d85716e3a1 [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) {
Steve Magnanifcbf7632017-10-12 08:48:41 -050061 udf_debug("block_group (%u) > nr_groups (%d)\n",
Joe Perchesa983f362011-10-10 01:08:07 -070062 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) {
Steve Magnanifcbf7632017-10-12 08:48:41 -0500125 udf_debug("%u < %d || %u + %u > %u\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)) {
Steve Magnanifcbf7632017-10-12 08:48:41 -0500154 udf_debug("bit %lu already set\n", bit + i);
Marcin Slusarz4daa1b82008-02-08 04:20:41 -0800155 udf_debug("byte=%2x\n",
Steve Magnanifcbf7632017-10-12 08:48:41 -0500156 ((__u8 *)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
Steve Magnanib490bdd2017-10-12 08:48:40 -0500221static udf_pblk_t 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);
Steve Magnanib490bdd2017-10-12 08:48:40 -0500226 int newbit, bit = 0;
227 udf_pblk_t block;
228 int block_group, group_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int end_goal, nr_groups, bitmap_nr, i;
230 struct buffer_head *bh = NULL;
231 char *ptr;
Steve Magnanib490bdd2017-10-12 08:48:40 -0500232 udf_pblk_t newblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 *err = -ENOSPC;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800235 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237repeat:
Roel Kluin3391faa2009-06-22 23:12:29 +0200238 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goal = 0;
240
241 nr_groups = bitmap->s_nr_groups;
242 block = goal + (sizeof(struct spaceBitmapDesc) << 3);
243 block_group = block >> (sb->s_blocksize_bits + 3);
244 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
245
246 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
247 if (bitmap_nr < 0)
248 goto error_return;
249 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
251 sb->s_blocksize - group_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700253 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 bit = block % (sb->s_blocksize << 3);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700255 if (udf_test_bit(bit, bh->b_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 end_goal = (bit + 63) & ~63;
259 bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
260 if (bit < end_goal)
261 goto got_block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700262
Marcin Slusarz4b111112008-02-08 04:20:36 -0800263 ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
264 sb->s_blocksize - ((bit + 7) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 newbit = (ptr - ((char *)bh->b_data)) << 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700266 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 bit = newbit;
268 goto search_back;
269 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700270
Marcin Slusarz4b111112008-02-08 04:20:36 -0800271 newbit = udf_find_next_one_bit(bh->b_data,
272 sb->s_blocksize << 3, bit);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700273 if (newbit < sb->s_blocksize << 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 bit = newbit;
275 goto got_block;
276 }
277 }
278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 for (i = 0; i < (nr_groups * 2); i++) {
280 block_group++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (block_group >= nr_groups)
282 block_group = 0;
283 group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
284
285 bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
286 if (bitmap_nr < 0)
287 goto error_return;
288 bh = bitmap->s_block_bitmap[bitmap_nr];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 if (i < nr_groups) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700290 ptr = memscan((char *)bh->b_data + group_start, 0xFF,
291 sb->s_blocksize - group_start);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700292 if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 bit = (ptr - ((char *)bh->b_data)) << 3;
294 break;
295 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700296 } else {
Dirk Behme6f644e52011-02-22 14:04:19 -0500297 bit = udf_find_next_one_bit(bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700298 sb->s_blocksize << 3,
299 group_start << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (bit < sb->s_blocksize << 3)
301 break;
302 }
303 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700304 if (i >= (nr_groups * 2)) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800305 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return newblock;
307 }
308 if (bit < sb->s_blocksize << 3)
309 goto search_back;
310 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800311 bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
312 group_start << 3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700313 if (bit >= sb->s_blocksize << 3) {
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800314 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316 }
317
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700318search_back:
Marcin Slusarz4b111112008-02-08 04:20:36 -0800319 i = 0;
320 while (i < 7 && bit > (group_start << 3) &&
321 udf_test_bit(bit - 1, bh->b_data)) {
322 ++i;
323 --bit;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700326got_block:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328 (sizeof(struct spaceBitmapDesc) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700330 if (!udf_clear_bit(bit, bh->b_data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 udf_debug("bit already cleared for block %d\n", bit);
332 goto repeat;
333 }
334
335 mark_buffer_dirty(bh);
336
Jan Kara146bca72009-03-16 18:27:37 +0100337 udf_add_free_space(sb, partition, -1);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800338 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *err = 0;
340 return newblock;
341
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700342error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *err = -EIO;
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800344 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348static void udf_table_free_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700349 struct inode *table,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200350 struct kernel_lb_addr *bloc,
351 uint32_t offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct udf_sb_info *sbi = UDF_SB(sb);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200355 struct udf_part_map *partmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 uint32_t start, end;
Jan Karaff116fc2007-05-08 00:35:14 -0700357 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200358 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700359 struct extent_position oepos, epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800361 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800363 mutex_lock(&sbi->s_alloc_mutex);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200364 partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
Dan Carpenter69ecbbe2010-03-15 11:21:13 +0300365 if (bloc->logicalBlockNum + count < count ||
366 (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
Steve Magnanifcbf7632017-10-12 08:48:41 -0500367 udf_debug("%u < %d || %u + %u > %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700368 bloc->logicalBlockNum, 0,
369 bloc->logicalBlockNum, count,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200370 partmap->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto error_return;
372 }
373
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800374 iinfo = UDF_I(table);
Jan Kara146bca72009-03-16 18:27:37 +0100375 udf_add_free_space(sb, sbi->s_partition, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Pekka Enberg97e961f2008-10-15 12:29:03 +0200377 start = bloc->logicalBlockNum + offset;
378 end = bloc->logicalBlockNum + offset + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jan Karaff116fc2007-05-08 00:35:14 -0700380 epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 elen = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800382 epos.block = oepos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700383 epos.bh = oepos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385 while (count &&
386 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800387 if (((eloc.logicalBlockNum +
388 (elen >> sb->s_blocksize_bits)) == start)) {
389 if ((0x3FFFFFFF - elen) <
390 (count << sb->s_blocksize_bits)) {
391 uint32_t tmp = ((0x3FFFFFFF - elen) >>
392 sb->s_blocksize_bits);
393 count -= tmp;
394 start += tmp;
395 elen = (etype << 30) |
396 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700397 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800398 elen = (etype << 30) |
399 (elen +
400 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 start += count;
402 count = 0;
403 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200404 udf_write_aext(table, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700405 } else if (eloc.logicalBlockNum == (end + 1)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800406 if ((0x3FFFFFFF - elen) <
407 (count << sb->s_blocksize_bits)) {
408 uint32_t tmp = ((0x3FFFFFFF - elen) >>
409 sb->s_blocksize_bits);
410 count -= tmp;
411 end -= tmp;
412 eloc.logicalBlockNum -= tmp;
413 elen = (etype << 30) |
414 (0x40000000 - sb->s_blocksize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700415 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 eloc.logicalBlockNum = start;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800417 elen = (etype << 30) |
418 (elen +
419 (count << sb->s_blocksize_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 end -= count;
421 count = 0;
422 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200423 udf_write_aext(table, &oepos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700426 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700427 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700428 brelse(oepos.bh);
429 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700430 oepos.bh = epos.bh;
431 oepos.offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700432 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700433 oepos.offset = epos.offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700437 if (count) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700438 /*
Marcin Slusarz4b111112008-02-08 04:20:36 -0800439 * NOTE: we CANNOT use udf_add_aext here, as it can try to
440 * allocate a new block, and since we hold the super block
441 * lock already very bad things would happen :)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700442 *
443 * We copy the behavior of udf_add_aext, but instead of
444 * trying to allocate a new block close to the existing one,
445 * we just steal a block from the extent we are trying to add.
446 *
447 * It would be nice if the blocks were close together, but it
448 * isn't required.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 eloc.logicalBlockNum = start;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700454 elen = EXT_RECORDED_ALLOCATED |
455 (count << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800457 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200458 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800459 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200460 adsize = sizeof(struct long_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800461 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700462 brelse(oepos.bh);
463 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto error_return;
465 }
466
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 if (epos.offset + (2 * adsize) > sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Steal a block from the extent being free'd */
Jan Karafcea62b2015-12-23 14:21:13 +0100469 udf_setup_indirect_aext(table, eloc.logicalBlockNum,
470 &epos);
471
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700472 eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Marcin Slusarz4b111112008-02-08 04:20:36 -0800476 /* It's possible that stealing the block emptied the extent */
Jan Karafcea62b2015-12-23 14:21:13 +0100477 if (elen)
478 __udf_add_aext(table, &epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
Jan Kara3bf25cb2007-05-08 00:35:16 -0700481 brelse(epos.bh);
482 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700484error_return:
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800485 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return;
487}
488
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700489static int udf_table_prealloc_blocks(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700490 struct inode *table, uint16_t partition,
491 uint32_t first_block, uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct udf_sb_info *sbi = UDF_SB(sb);
494 int alloc_count = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700495 uint32_t elen, adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200496 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -0700497 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int8_t etype = -1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800499 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Roel Kluin3391faa2009-06-22 23:12:29 +0200501 if (first_block >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800504 iinfo = UDF_I(table);
505 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200506 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800507 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200508 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 else
510 return 0;
511
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800512 mutex_lock(&sbi->s_alloc_mutex);
Jan Karaff116fc2007-05-08 00:35:14 -0700513 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800514 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700515 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 eloc.logicalBlockNum = 0xFFFFFFFF;
517
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700518 while (first_block != eloc.logicalBlockNum &&
519 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Steve Magnanifcbf7632017-10-12 08:48:41 -0500520 udf_debug("eloc=%u, elen=%u, first_block=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 eloc.logicalBlockNum, elen, first_block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700522 ; /* empty loop body */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 if (first_block == eloc.logicalBlockNum) {
Jan Karaff116fc2007-05-08 00:35:14 -0700526 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 alloc_count = (elen >> sb->s_blocksize_bits);
Jan Kara36350462010-05-19 16:28:56 +0200529 if (alloc_count > block_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 alloc_count = block_count;
531 eloc.logicalBlockNum += alloc_count;
532 elen -= (alloc_count << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200533 udf_write_aext(table, &epos, &eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800534 (etype << 30) | elen, 1);
535 } else
Jan Kara6c1e4d02018-06-13 18:04:24 +0200536 udf_delete_aext(table, epos);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700537 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 alloc_count = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jan Kara3bf25cb2007-05-08 00:35:16 -0700541 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jan Kara146bca72009-03-16 18:27:37 +0100543 if (alloc_count)
544 udf_add_free_space(sb, partition, -alloc_count);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800545 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return alloc_count;
547}
548
Steve Magnanib490bdd2017-10-12 08:48:40 -0500549static udf_pblk_t udf_table_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700550 struct inode *table, uint16_t partition,
551 uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct udf_sb_info *sbi = UDF_SB(sb);
554 uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
Steve Magnanib490bdd2017-10-12 08:48:40 -0500555 udf_pblk_t newblock = 0;
556 uint32_t adsize;
Jan Karaff116fc2007-05-08 00:35:14 -0700557 uint32_t elen, goal_elen = 0;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200558 struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
Jan Karaff116fc2007-05-08 00:35:14 -0700559 struct extent_position epos, goal_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800561 struct udf_inode_info *iinfo = UDF_I(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 *err = -ENOSPC;
564
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800565 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200566 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800567 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200568 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 else
570 return newblock;
571
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800572 mutex_lock(&sbi->s_alloc_mutex);
Roel Kluin3391faa2009-06-22 23:12:29 +0200573 if (goal >= sbi->s_partmaps[partition].s_partition_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goal = 0;
575
Marcin Slusarz4b111112008-02-08 04:20:36 -0800576 /* We search for the closest matching block to goal. If we find
577 a exact hit, we stop. Otherwise we keep going till we run out
578 of extents. We store the buffer_head, bloc, and extoffset
579 of the current closest match and use that when we are done.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700580 */
Jan Karaff116fc2007-05-08 00:35:14 -0700581 epos.offset = sizeof(struct unallocSpaceEntry);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800582 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700583 epos.bh = goal_epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700585 while (spread &&
586 (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587 if (goal >= eloc.logicalBlockNum) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800588 if (goal < eloc.logicalBlockNum +
589 (elen >> sb->s_blocksize_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 nspread = 0;
591 else
592 nspread = goal - eloc.logicalBlockNum -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700593 (elen >> sb->s_blocksize_bits);
594 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 nspread = eloc.logicalBlockNum - goal;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 if (nspread < spread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spread = nspread;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700600 if (goal_epos.bh != epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700601 brelse(goal_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700602 goal_epos.bh = epos.bh;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700603 get_bh(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Jan Karaff116fc2007-05-08 00:35:14 -0700605 goal_epos.block = epos.block;
606 goal_epos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goal_eloc = eloc;
608 goal_elen = (etype << 30) | elen;
609 }
610 }
611
Jan Kara3bf25cb2007-05-08 00:35:16 -0700612 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 if (spread == 0xFFFFFFFF) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700615 brelse(goal_epos.bh);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800616 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618 }
619
620 /* Only allocate blocks from the beginning of the extent.
621 That way, we only delete (empty) extents, never have to insert an
622 extent because of splitting */
623 /* This works, but very poorly.... */
624
625 newblock = goal_eloc.logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700626 goal_eloc.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goal_elen -= sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (goal_elen)
Pekka Enberg97e961f2008-10-15 12:29:03 +0200630 udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 else
Jan Kara6c1e4d02018-06-13 18:04:24 +0200632 udf_delete_aext(table, goal_epos);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700633 brelse(goal_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jan Kara146bca72009-03-16 18:27:37 +0100635 udf_add_free_space(sb, partition, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800637 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *err = 0;
639 return newblock;
640}
641
Pekka Enberg97e961f2008-10-15 12:29:03 +0200642void udf_free_blocks(struct super_block *sb, struct inode *inode,
643 struct kernel_lb_addr *bloc, uint32_t offset,
644 uint32_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Pekka Enberg97e961f2008-10-15 12:29:03 +0200646 uint16_t partition = bloc->partitionReferenceNum;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800647 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800649 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100650 udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100651 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800652 } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100653 udf_table_free_blocks(sb, map->s_uspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100654 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800655 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Jan Karafd4287d2012-02-16 13:00:14 +0100656 udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
Jan Karae650b942008-12-01 13:06:10 +0100657 bloc, offset, count);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800658 } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Jan Karafd4287d2012-02-16 13:00:14 +0100659 udf_table_free_blocks(sb, map->s_fspace.s_table,
Jan Karae650b942008-12-01 13:06:10 +0100660 bloc, offset, count);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700661 }
Jan Karafd4287d2012-02-16 13:00:14 +0100662
663 if (inode) {
664 inode_sub_bytes(inode,
665 ((sector_t)count) << sb->s_blocksize_bits);
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700669inline int udf_prealloc_blocks(struct super_block *sb,
670 struct inode *inode,
671 uint16_t partition, uint32_t first_block,
672 uint32_t block_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800674 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Fabian Frederick1be440de2015-03-15 10:54:23 +0100675 int allocated;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800676
Marcin Slusarz4b111112008-02-08 04:20:36 -0800677 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100678 allocated = udf_bitmap_prealloc_blocks(sb,
679 map->s_uspace.s_bitmap,
680 partition, first_block,
681 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800682 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100683 allocated = udf_table_prealloc_blocks(sb,
684 map->s_uspace.s_table,
685 partition, first_block,
686 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800687 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100688 allocated = udf_bitmap_prealloc_blocks(sb,
689 map->s_fspace.s_bitmap,
690 partition, first_block,
691 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800692 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100693 allocated = udf_table_prealloc_blocks(sb,
694 map->s_fspace.s_table,
695 partition, first_block,
696 block_count);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800697 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
Jan Karafd4287d2012-02-16 13:00:14 +0100699
700 if (inode && allocated > 0)
701 inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
702 return allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Steve Magnanib490bdd2017-10-12 08:48:40 -0500705inline udf_pblk_t udf_new_block(struct super_block *sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700706 struct inode *inode,
707 uint16_t partition, uint32_t goal, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800709 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Steve Magnanib490bdd2017-10-12 08:48:40 -0500710 udf_pblk_t block;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700711
Marcin Slusarz4b111112008-02-08 04:20:36 -0800712 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Jan Karafd4287d2012-02-16 13:00:14 +0100713 block = udf_bitmap_new_block(sb,
714 map->s_uspace.s_bitmap,
715 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800716 else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100717 block = udf_table_new_block(sb,
718 map->s_uspace.s_table,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700719 partition, goal, err);
Jan Karafd4287d2012-02-16 13:00:14 +0100720 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
721 block = udf_bitmap_new_block(sb,
722 map->s_fspace.s_bitmap,
723 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800724 else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
Jan Karafd4287d2012-02-16 13:00:14 +0100725 block = udf_table_new_block(sb,
726 map->s_fspace.s_table,
727 partition, goal, err);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800728 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *err = -EIO;
730 return 0;
731 }
Jan Karafd4287d2012-02-16 13:00:14 +0100732 if (inode && block)
733 inode_add_bytes(inode, sb->s_blocksize);
734 return block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}