xref: /linux/fs/hfsplus/extents.c (revision 0dd9ac63ce26ec87b080ca9c3e6efed33c23ace6)
1 /*
2  *  linux/fs/hfsplus/extents.c
3  *
4  * Copyright (C) 2001
5  * Brad Boyer (flar@allandria.com)
6  * (C) 2003 Ardis Technologies <roman@ardistech.com>
7  *
8  * Handling of Extents both in catalog and extents overflow trees
9  */
10 
11 #include <linux/errno.h>
12 #include <linux/fs.h>
13 #include <linux/pagemap.h>
14 
15 #include "hfsplus_fs.h"
16 #include "hfsplus_raw.h"
17 
18 /* Compare two extents keys, returns 0 on same, pos/neg for difference */
19 int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
20 			const hfsplus_btree_key *k2)
21 {
22 	__be32 k1id, k2id;
23 	__be32 k1s, k2s;
24 
25 	k1id = k1->ext.cnid;
26 	k2id = k2->ext.cnid;
27 	if (k1id != k2id)
28 		return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
29 
30 	if (k1->ext.fork_type != k2->ext.fork_type)
31 		return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
32 
33 	k1s = k1->ext.start_block;
34 	k2s = k2->ext.start_block;
35 	if (k1s == k2s)
36 		return 0;
37 	return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
38 }
39 
40 static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid,
41 				  u32 block, u8 type)
42 {
43 	key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
44 	key->ext.cnid = cpu_to_be32(cnid);
45 	key->ext.start_block = cpu_to_be32(block);
46 	key->ext.fork_type = type;
47 	key->ext.pad = 0;
48 }
49 
50 static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
51 {
52 	int i;
53 	u32 count;
54 
55 	for (i = 0; i < 8; ext++, i++) {
56 		count = be32_to_cpu(ext->block_count);
57 		if (off < count)
58 			return be32_to_cpu(ext->start_block) + off;
59 		off -= count;
60 	}
61 	/* panic? */
62 	return 0;
63 }
64 
65 static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
66 {
67 	int i;
68 	u32 count = 0;
69 
70 	for (i = 0; i < 8; ext++, i++)
71 		count += be32_to_cpu(ext->block_count);
72 	return count;
73 }
74 
75 static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
76 {
77 	int i;
78 
79 	ext += 7;
80 	for (i = 0; i < 7; ext--, i++)
81 		if (ext->block_count)
82 			break;
83 	return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
84 }
85 
86 static void __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)
87 {
88 	int res;
89 
90 	hfsplus_ext_build_key(fd->search_key, inode->i_ino, HFSPLUS_I(inode).cached_start,
91 			      HFSPLUS_IS_RSRC(inode) ?  HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
92 	res = hfs_brec_find(fd);
93 	if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_NEW) {
94 		if (res != -ENOENT)
95 			return;
96 		hfs_brec_insert(fd, HFSPLUS_I(inode).cached_extents, sizeof(hfsplus_extent_rec));
97 		HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
98 	} else {
99 		if (res)
100 			return;
101 		hfs_bnode_write(fd->bnode, HFSPLUS_I(inode).cached_extents, fd->entryoffset, fd->entrylength);
102 		HFSPLUS_I(inode).flags &= ~HFSPLUS_FLG_EXT_DIRTY;
103 	}
104 }
105 
106 void hfsplus_ext_write_extent(struct inode *inode)
107 {
108 	if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY) {
109 		struct hfs_find_data fd;
110 
111 		hfs_find_init(HFSPLUS_SB(inode->i_sb).ext_tree, &fd);
112 		__hfsplus_ext_write_extent(inode, &fd);
113 		hfs_find_exit(&fd);
114 	}
115 }
116 
117 static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
118 					    struct hfsplus_extent *extent,
119 					    u32 cnid, u32 block, u8 type)
120 {
121 	int res;
122 
123 	hfsplus_ext_build_key(fd->search_key, cnid, block, type);
124 	fd->key->ext.cnid = 0;
125 	res = hfs_brec_find(fd);
126 	if (res && res != -ENOENT)
127 		return res;
128 	if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
129 	    fd->key->ext.fork_type != fd->search_key->ext.fork_type)
130 		return -ENOENT;
131 	if (fd->entrylength != sizeof(hfsplus_extent_rec))
132 		return -EIO;
133 	hfs_bnode_read(fd->bnode, extent, fd->entryoffset, sizeof(hfsplus_extent_rec));
134 	return 0;
135 }
136 
137 static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, struct inode *inode, u32 block)
138 {
139 	int res;
140 
141 	if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY)
142 		__hfsplus_ext_write_extent(inode, fd);
143 
144 	res = __hfsplus_ext_read_extent(fd, HFSPLUS_I(inode).cached_extents, inode->i_ino,
145 					block, HFSPLUS_IS_RSRC(inode) ? HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
146 	if (!res) {
147 		HFSPLUS_I(inode).cached_start = be32_to_cpu(fd->key->ext.start_block);
148 		HFSPLUS_I(inode).cached_blocks = hfsplus_ext_block_count(HFSPLUS_I(inode).cached_extents);
149 	} else {
150 		HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0;
151 		HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
152 	}
153 	return res;
154 }
155 
156 static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
157 {
158 	struct hfs_find_data fd;
159 	int res;
160 
161 	if (block >= HFSPLUS_I(inode).cached_start &&
162 	    block < HFSPLUS_I(inode).cached_start + HFSPLUS_I(inode).cached_blocks)
163 		return 0;
164 
165 	hfs_find_init(HFSPLUS_SB(inode->i_sb).ext_tree, &fd);
166 	res = __hfsplus_ext_cache_extent(&fd, inode, block);
167 	hfs_find_exit(&fd);
168 	return res;
169 }
170 
171 /* Get a block at iblock for inode, possibly allocating if create */
172 int hfsplus_get_block(struct inode *inode, sector_t iblock,
173 		      struct buffer_head *bh_result, int create)
174 {
175 	struct super_block *sb;
176 	int res = -EIO;
177 	u32 ablock, dblock, mask;
178 	int shift;
179 
180 	sb = inode->i_sb;
181 
182 	/* Convert inode block to disk allocation block */
183 	shift = HFSPLUS_SB(sb).alloc_blksz_shift - sb->s_blocksize_bits;
184 	ablock = iblock >> HFSPLUS_SB(sb).fs_shift;
185 
186 	if (iblock >= HFSPLUS_I(inode).fs_blocks) {
187 		if (iblock > HFSPLUS_I(inode).fs_blocks || !create)
188 			return -EIO;
189 		if (ablock >= HFSPLUS_I(inode).alloc_blocks) {
190 			res = hfsplus_file_extend(inode);
191 			if (res)
192 				return res;
193 		}
194 	} else
195 		create = 0;
196 
197 	if (ablock < HFSPLUS_I(inode).first_blocks) {
198 		dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).first_extents, ablock);
199 		goto done;
200 	}
201 
202 	if (inode->i_ino == HFSPLUS_EXT_CNID)
203 		return -EIO;
204 
205 	mutex_lock(&HFSPLUS_I(inode).extents_lock);
206 	res = hfsplus_ext_read_extent(inode, ablock);
207 	if (!res) {
208 		dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).cached_extents, ablock -
209 					     HFSPLUS_I(inode).cached_start);
210 	} else {
211 		mutex_unlock(&HFSPLUS_I(inode).extents_lock);
212 		return -EIO;
213 	}
214 	mutex_unlock(&HFSPLUS_I(inode).extents_lock);
215 
216 done:
217 	dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n", inode->i_ino, (long long)iblock, dblock);
218 	mask = (1 << HFSPLUS_SB(sb).fs_shift) - 1;
219 	map_bh(bh_result, sb, (dblock << HFSPLUS_SB(sb).fs_shift) + HFSPLUS_SB(sb).blockoffset + (iblock & mask));
220 	if (create) {
221 		set_buffer_new(bh_result);
222 		HFSPLUS_I(inode).phys_size += sb->s_blocksize;
223 		HFSPLUS_I(inode).fs_blocks++;
224 		inode_add_bytes(inode, sb->s_blocksize);
225 		mark_inode_dirty(inode);
226 	}
227 	return 0;
228 }
229 
230 static void hfsplus_dump_extent(struct hfsplus_extent *extent)
231 {
232 	int i;
233 
234 	dprint(DBG_EXTENT, "   ");
235 	for (i = 0; i < 8; i++)
236 		dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block),
237 				 be32_to_cpu(extent[i].block_count));
238 	dprint(DBG_EXTENT, "\n");
239 }
240 
241 static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
242 			      u32 alloc_block, u32 block_count)
243 {
244 	u32 count, start;
245 	int i;
246 
247 	hfsplus_dump_extent(extent);
248 	for (i = 0; i < 8; extent++, i++) {
249 		count = be32_to_cpu(extent->block_count);
250 		if (offset == count) {
251 			start = be32_to_cpu(extent->start_block);
252 			if (alloc_block != start + count) {
253 				if (++i >= 8)
254 					return -ENOSPC;
255 				extent++;
256 				extent->start_block = cpu_to_be32(alloc_block);
257 			} else
258 				block_count += count;
259 			extent->block_count = cpu_to_be32(block_count);
260 			return 0;
261 		} else if (offset < count)
262 			break;
263 		offset -= count;
264 	}
265 	/* panic? */
266 	return -EIO;
267 }
268 
269 static int hfsplus_free_extents(struct super_block *sb,
270 				struct hfsplus_extent *extent,
271 				u32 offset, u32 block_nr)
272 {
273 	u32 count, start;
274 	int i;
275 
276 	hfsplus_dump_extent(extent);
277 	for (i = 0; i < 8; extent++, i++) {
278 		count = be32_to_cpu(extent->block_count);
279 		if (offset == count)
280 			goto found;
281 		else if (offset < count)
282 			break;
283 		offset -= count;
284 	}
285 	/* panic? */
286 	return -EIO;
287 found:
288 	for (;;) {
289 		start = be32_to_cpu(extent->start_block);
290 		if (count <= block_nr) {
291 			hfsplus_block_free(sb, start, count);
292 			extent->block_count = 0;
293 			extent->start_block = 0;
294 			block_nr -= count;
295 		} else {
296 			count -= block_nr;
297 			hfsplus_block_free(sb, start + count, block_nr);
298 			extent->block_count = cpu_to_be32(count);
299 			block_nr = 0;
300 		}
301 		if (!block_nr || !i)
302 			return 0;
303 		i--;
304 		extent--;
305 		count = be32_to_cpu(extent->block_count);
306 	}
307 }
308 
309 int hfsplus_free_fork(struct super_block *sb, u32 cnid, struct hfsplus_fork_raw *fork, int type)
310 {
311 	struct hfs_find_data fd;
312 	hfsplus_extent_rec ext_entry;
313 	u32 total_blocks, blocks, start;
314 	int res, i;
315 
316 	total_blocks = be32_to_cpu(fork->total_blocks);
317 	if (!total_blocks)
318 		return 0;
319 
320 	blocks = 0;
321 	for (i = 0; i < 8; i++)
322 		blocks += be32_to_cpu(fork->extents[i].block_count);
323 
324 	res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
325 	if (res)
326 		return res;
327 	if (total_blocks == blocks)
328 		return 0;
329 
330 	hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
331 	do {
332 		res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
333 						total_blocks, type);
334 		if (res)
335 			break;
336 		start = be32_to_cpu(fd.key->ext.start_block);
337 		hfsplus_free_extents(sb, ext_entry,
338 				     total_blocks - start,
339 				     total_blocks);
340 		hfs_brec_remove(&fd);
341 		total_blocks = start;
342 	} while (total_blocks > blocks);
343 	hfs_find_exit(&fd);
344 
345 	return res;
346 }
347 
348 int hfsplus_file_extend(struct inode *inode)
349 {
350 	struct super_block *sb = inode->i_sb;
351 	u32 start, len, goal;
352 	int res;
353 
354 	if (HFSPLUS_SB(sb).alloc_file->i_size * 8 < HFSPLUS_SB(sb).total_blocks - HFSPLUS_SB(sb).free_blocks + 8) {
355 		// extend alloc file
356 		printk(KERN_ERR "hfs: extend alloc file! (%Lu,%u,%u)\n", HFSPLUS_SB(sb).alloc_file->i_size * 8,
357 			HFSPLUS_SB(sb).total_blocks, HFSPLUS_SB(sb).free_blocks);
358 		return -ENOSPC;
359 	}
360 
361 	mutex_lock(&HFSPLUS_I(inode).extents_lock);
362 	if (HFSPLUS_I(inode).alloc_blocks == HFSPLUS_I(inode).first_blocks)
363 		goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).first_extents);
364 	else {
365 		res = hfsplus_ext_read_extent(inode, HFSPLUS_I(inode).alloc_blocks);
366 		if (res)
367 			goto out;
368 		goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).cached_extents);
369 	}
370 
371 	len = HFSPLUS_I(inode).clump_blocks;
372 	start = hfsplus_block_allocate(sb, HFSPLUS_SB(sb).total_blocks, goal, &len);
373 	if (start >= HFSPLUS_SB(sb).total_blocks) {
374 		start = hfsplus_block_allocate(sb, goal, 0, &len);
375 		if (start >= goal) {
376 			res = -ENOSPC;
377 			goto out;
378 		}
379 	}
380 
381 	dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
382 	if (HFSPLUS_I(inode).alloc_blocks <= HFSPLUS_I(inode).first_blocks) {
383 		if (!HFSPLUS_I(inode).first_blocks) {
384 			dprint(DBG_EXTENT, "first extents\n");
385 			/* no extents yet */
386 			HFSPLUS_I(inode).first_extents[0].start_block = cpu_to_be32(start);
387 			HFSPLUS_I(inode).first_extents[0].block_count = cpu_to_be32(len);
388 			res = 0;
389 		} else {
390 			/* try to append to extents in inode */
391 			res = hfsplus_add_extent(HFSPLUS_I(inode).first_extents,
392 						 HFSPLUS_I(inode).alloc_blocks,
393 						 start, len);
394 			if (res == -ENOSPC)
395 				goto insert_extent;
396 		}
397 		if (!res) {
398 			hfsplus_dump_extent(HFSPLUS_I(inode).first_extents);
399 			HFSPLUS_I(inode).first_blocks += len;
400 		}
401 	} else {
402 		res = hfsplus_add_extent(HFSPLUS_I(inode).cached_extents,
403 					 HFSPLUS_I(inode).alloc_blocks -
404 					 HFSPLUS_I(inode).cached_start,
405 					 start, len);
406 		if (!res) {
407 			hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
408 			HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY;
409 			HFSPLUS_I(inode).cached_blocks += len;
410 		} else if (res == -ENOSPC)
411 			goto insert_extent;
412 	}
413 out:
414 	mutex_unlock(&HFSPLUS_I(inode).extents_lock);
415 	if (!res) {
416 		HFSPLUS_I(inode).alloc_blocks += len;
417 		mark_inode_dirty(inode);
418 	}
419 	return res;
420 
421 insert_extent:
422 	dprint(DBG_EXTENT, "insert new extent\n");
423 	hfsplus_ext_write_extent(inode);
424 
425 	memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
426 	HFSPLUS_I(inode).cached_extents[0].start_block = cpu_to_be32(start);
427 	HFSPLUS_I(inode).cached_extents[0].block_count = cpu_to_be32(len);
428 	hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
429 	HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW;
430 	HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).alloc_blocks;
431 	HFSPLUS_I(inode).cached_blocks = len;
432 
433 	res = 0;
434 	goto out;
435 }
436 
437 void hfsplus_file_truncate(struct inode *inode)
438 {
439 	struct super_block *sb = inode->i_sb;
440 	struct hfs_find_data fd;
441 	u32 alloc_cnt, blk_cnt, start;
442 	int res;
443 
444 	dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", inode->i_ino,
445 	       (long long)HFSPLUS_I(inode).phys_size, inode->i_size);
446 	if (inode->i_size > HFSPLUS_I(inode).phys_size) {
447 		struct address_space *mapping = inode->i_mapping;
448 		struct page *page;
449 		void *fsdata;
450 		u32 size = inode->i_size;
451 		int res;
452 
453 		res = pagecache_write_begin(NULL, mapping, size, 0,
454 						AOP_FLAG_UNINTERRUPTIBLE,
455 						&page, &fsdata);
456 		if (res)
457 			return;
458 		res = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
459 		if (res < 0)
460 			return;
461 		mark_inode_dirty(inode);
462 		return;
463 	} else if (inode->i_size == HFSPLUS_I(inode).phys_size)
464 		return;
465 
466 	blk_cnt = (inode->i_size + HFSPLUS_SB(sb).alloc_blksz - 1) >> HFSPLUS_SB(sb).alloc_blksz_shift;
467 	alloc_cnt = HFSPLUS_I(inode).alloc_blocks;
468 	if (blk_cnt == alloc_cnt)
469 		goto out;
470 
471 	mutex_lock(&HFSPLUS_I(inode).extents_lock);
472 	hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
473 	while (1) {
474 		if (alloc_cnt == HFSPLUS_I(inode).first_blocks) {
475 			hfsplus_free_extents(sb, HFSPLUS_I(inode).first_extents,
476 					     alloc_cnt, alloc_cnt - blk_cnt);
477 			hfsplus_dump_extent(HFSPLUS_I(inode).first_extents);
478 			HFSPLUS_I(inode).first_blocks = blk_cnt;
479 			break;
480 		}
481 		res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
482 		if (res)
483 			break;
484 		start = HFSPLUS_I(inode).cached_start;
485 		hfsplus_free_extents(sb, HFSPLUS_I(inode).cached_extents,
486 				     alloc_cnt - start, alloc_cnt - blk_cnt);
487 		hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
488 		if (blk_cnt > start) {
489 			HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY;
490 			break;
491 		}
492 		alloc_cnt = start;
493 		HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0;
494 		HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
495 		hfs_brec_remove(&fd);
496 	}
497 	hfs_find_exit(&fd);
498 	mutex_unlock(&HFSPLUS_I(inode).extents_lock);
499 
500 	HFSPLUS_I(inode).alloc_blocks = blk_cnt;
501 out:
502 	HFSPLUS_I(inode).phys_size = inode->i_size;
503 	HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
504 	inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
505 	mark_inode_dirty(inode);
506 }
507