xref: /openbmc/linux/fs/hfsplus/extents.c (revision d7bdb996)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/hfsplus/extents.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2001
51da177e4SLinus Torvalds  * Brad Boyer (flar@allandria.com)
61da177e4SLinus Torvalds  * (C) 2003 Ardis Technologies <roman@ardistech.com>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Handling of Extents both in catalog and extents overflow trees
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/fs.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include "hfsplus_fs.h"
161da177e4SLinus Torvalds #include "hfsplus_raw.h"
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /* Compare two extents keys, returns 0 on same, pos/neg for difference */
192179d372SDavid Elliott int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
202179d372SDavid Elliott 			const hfsplus_btree_key *k2)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds 	__be32 k1id, k2id;
231da177e4SLinus Torvalds 	__be32 k1s, k2s;
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds 	k1id = k1->ext.cnid;
261da177e4SLinus Torvalds 	k2id = k2->ext.cnid;
271da177e4SLinus Torvalds 	if (k1id != k2id)
281da177e4SLinus Torvalds 		return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 	if (k1->ext.fork_type != k2->ext.fork_type)
311da177e4SLinus Torvalds 		return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	k1s = k1->ext.start_block;
341da177e4SLinus Torvalds 	k2s = k2->ext.start_block;
351da177e4SLinus Torvalds 	if (k1s == k2s)
361da177e4SLinus Torvalds 		return 0;
371da177e4SLinus Torvalds 	return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
381da177e4SLinus Torvalds }
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid,
411da177e4SLinus Torvalds 				  u32 block, u8 type)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
441da177e4SLinus Torvalds 	key->ext.cnid = cpu_to_be32(cnid);
451da177e4SLinus Torvalds 	key->ext.start_block = cpu_to_be32(block);
461da177e4SLinus Torvalds 	key->ext.fork_type = type;
471da177e4SLinus Torvalds 	key->ext.pad = 0;
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	int i;
531da177e4SLinus Torvalds 	u32 count;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	for (i = 0; i < 8; ext++, i++) {
561da177e4SLinus Torvalds 		count = be32_to_cpu(ext->block_count);
571da177e4SLinus Torvalds 		if (off < count)
581da177e4SLinus Torvalds 			return be32_to_cpu(ext->start_block) + off;
591da177e4SLinus Torvalds 		off -= count;
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds 	/* panic? */
621da177e4SLinus Torvalds 	return 0;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	int i;
681da177e4SLinus Torvalds 	u32 count = 0;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	for (i = 0; i < 8; ext++, i++)
711da177e4SLinus Torvalds 		count += be32_to_cpu(ext->block_count);
721da177e4SLinus Torvalds 	return count;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	int i;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	ext += 7;
801da177e4SLinus Torvalds 	for (i = 0; i < 7; ext--, i++)
811da177e4SLinus Torvalds 		if (ext->block_count)
821da177e4SLinus Torvalds 			break;
831da177e4SLinus Torvalds 	return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
86d7a475d0SAlexey Khoroshilov static int __hfsplus_ext_write_extent(struct inode *inode,
872753cc28SAnton Salikhmetov 		struct hfs_find_data *fd)
881da177e4SLinus Torvalds {
896af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
901da177e4SLinus Torvalds 	int res;
911da177e4SLinus Torvalds 
927fcc99f4SChristoph Hellwig 	WARN_ON(!mutex_is_locked(&hip->extents_lock));
937fcc99f4SChristoph Hellwig 
946af502deSChristoph Hellwig 	hfsplus_ext_build_key(fd->search_key, inode->i_ino, hip->cached_start,
956af502deSChristoph Hellwig 			      HFSPLUS_IS_RSRC(inode) ?
966af502deSChristoph Hellwig 				HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
976af502deSChristoph Hellwig 
98324ef39aSVyacheslav Dubeyko 	res = hfs_brec_find(fd, hfs_find_rec_by_key);
99b33b7921SChristoph Hellwig 	if (hip->extent_state & HFSPLUS_EXT_NEW) {
1001da177e4SLinus Torvalds 		if (res != -ENOENT)
101d7a475d0SAlexey Khoroshilov 			return res;
1026af502deSChristoph Hellwig 		hfs_brec_insert(fd, hip->cached_extents,
1036af502deSChristoph Hellwig 				sizeof(hfsplus_extent_rec));
104b33b7921SChristoph Hellwig 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
1051da177e4SLinus Torvalds 	} else {
1061da177e4SLinus Torvalds 		if (res)
107d7a475d0SAlexey Khoroshilov 			return res;
1086af502deSChristoph Hellwig 		hfs_bnode_write(fd->bnode, hip->cached_extents,
1096af502deSChristoph Hellwig 				fd->entryoffset, fd->entrylength);
110b33b7921SChristoph Hellwig 		hip->extent_state &= ~HFSPLUS_EXT_DIRTY;
1111da177e4SLinus Torvalds 	}
112e3494705SChristoph Hellwig 
113e3494705SChristoph Hellwig 	/*
114e3494705SChristoph Hellwig 	 * We can't just use hfsplus_mark_inode_dirty here, because we
115e3494705SChristoph Hellwig 	 * also get called from hfsplus_write_inode, which should not
116e3494705SChristoph Hellwig 	 * redirty the inode.  Instead the callers have to be careful
117e3494705SChristoph Hellwig 	 * to explicily mark the inode dirty, too.
118e3494705SChristoph Hellwig 	 */
119e3494705SChristoph Hellwig 	set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags);
120d7a475d0SAlexey Khoroshilov 
121d7a475d0SAlexey Khoroshilov 	return 0;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124dd7f3d54SAlexey Khoroshilov static int hfsplus_ext_write_extent_locked(struct inode *inode)
1251da177e4SLinus Torvalds {
126d7a475d0SAlexey Khoroshilov 	int res = 0;
127dd7f3d54SAlexey Khoroshilov 
128b33b7921SChristoph Hellwig 	if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
1291da177e4SLinus Torvalds 		struct hfs_find_data fd;
1301da177e4SLinus Torvalds 
131dd7f3d54SAlexey Khoroshilov 		res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
132dd7f3d54SAlexey Khoroshilov 		if (res)
133dd7f3d54SAlexey Khoroshilov 			return res;
134d7a475d0SAlexey Khoroshilov 		res = __hfsplus_ext_write_extent(inode, &fd);
1351da177e4SLinus Torvalds 		hfs_find_exit(&fd);
1361da177e4SLinus Torvalds 	}
137d7a475d0SAlexey Khoroshilov 	return res;
1385bd9d99dSAlexey Khoroshilov }
1391da177e4SLinus Torvalds 
140dd7f3d54SAlexey Khoroshilov int hfsplus_ext_write_extent(struct inode *inode)
1417fcc99f4SChristoph Hellwig {
142dd7f3d54SAlexey Khoroshilov 	int res;
143dd7f3d54SAlexey Khoroshilov 
1447fcc99f4SChristoph Hellwig 	mutex_lock(&HFSPLUS_I(inode)->extents_lock);
145dd7f3d54SAlexey Khoroshilov 	res = hfsplus_ext_write_extent_locked(inode);
1467fcc99f4SChristoph Hellwig 	mutex_unlock(&HFSPLUS_I(inode)->extents_lock);
147dd7f3d54SAlexey Khoroshilov 
148dd7f3d54SAlexey Khoroshilov 	return res;
1497fcc99f4SChristoph Hellwig }
1507fcc99f4SChristoph Hellwig 
1511da177e4SLinus Torvalds static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
1521da177e4SLinus Torvalds 					    struct hfsplus_extent *extent,
1531da177e4SLinus Torvalds 					    u32 cnid, u32 block, u8 type)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	int res;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	hfsplus_ext_build_key(fd->search_key, cnid, block, type);
1581da177e4SLinus Torvalds 	fd->key->ext.cnid = 0;
159324ef39aSVyacheslav Dubeyko 	res = hfs_brec_find(fd, hfs_find_rec_by_key);
1601da177e4SLinus Torvalds 	if (res && res != -ENOENT)
1611da177e4SLinus Torvalds 		return res;
1621da177e4SLinus Torvalds 	if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
1631da177e4SLinus Torvalds 	    fd->key->ext.fork_type != fd->search_key->ext.fork_type)
1641da177e4SLinus Torvalds 		return -ENOENT;
1651da177e4SLinus Torvalds 	if (fd->entrylength != sizeof(hfsplus_extent_rec))
1661da177e4SLinus Torvalds 		return -EIO;
1672753cc28SAnton Salikhmetov 	hfs_bnode_read(fd->bnode, extent, fd->entryoffset,
1682753cc28SAnton Salikhmetov 		sizeof(hfsplus_extent_rec));
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1722753cc28SAnton Salikhmetov static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd,
1732753cc28SAnton Salikhmetov 		struct inode *inode, u32 block)
1741da177e4SLinus Torvalds {
1756af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
1761da177e4SLinus Torvalds 	int res;
1771da177e4SLinus Torvalds 
1787fcc99f4SChristoph Hellwig 	WARN_ON(!mutex_is_locked(&hip->extents_lock));
1797fcc99f4SChristoph Hellwig 
180d7a475d0SAlexey Khoroshilov 	if (hip->extent_state & HFSPLUS_EXT_DIRTY) {
181d7a475d0SAlexey Khoroshilov 		res = __hfsplus_ext_write_extent(inode, fd);
182d7a475d0SAlexey Khoroshilov 		if (res)
183d7a475d0SAlexey Khoroshilov 			return res;
184d7a475d0SAlexey Khoroshilov 	}
1851da177e4SLinus Torvalds 
1866af502deSChristoph Hellwig 	res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino,
1876af502deSChristoph Hellwig 					block, HFSPLUS_IS_RSRC(inode) ?
1886af502deSChristoph Hellwig 						HFSPLUS_TYPE_RSRC :
1896af502deSChristoph Hellwig 						HFSPLUS_TYPE_DATA);
1901da177e4SLinus Torvalds 	if (!res) {
1916af502deSChristoph Hellwig 		hip->cached_start = be32_to_cpu(fd->key->ext.start_block);
1922753cc28SAnton Salikhmetov 		hip->cached_blocks =
1932753cc28SAnton Salikhmetov 			hfsplus_ext_block_count(hip->cached_extents);
1941da177e4SLinus Torvalds 	} else {
1956af502deSChristoph Hellwig 		hip->cached_start = hip->cached_blocks = 0;
196b33b7921SChristoph Hellwig 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 	return res;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
2021da177e4SLinus Torvalds {
2036af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
2041da177e4SLinus Torvalds 	struct hfs_find_data fd;
2051da177e4SLinus Torvalds 	int res;
2061da177e4SLinus Torvalds 
2076af502deSChristoph Hellwig 	if (block >= hip->cached_start &&
2086af502deSChristoph Hellwig 	    block < hip->cached_start + hip->cached_blocks)
2091da177e4SLinus Torvalds 		return 0;
2101da177e4SLinus Torvalds 
2115bd9d99dSAlexey Khoroshilov 	res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
2125bd9d99dSAlexey Khoroshilov 	if (!res) {
2131da177e4SLinus Torvalds 		res = __hfsplus_ext_cache_extent(&fd, inode, block);
2141da177e4SLinus Torvalds 		hfs_find_exit(&fd);
2155bd9d99dSAlexey Khoroshilov 	}
2161da177e4SLinus Torvalds 	return res;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds /* Get a block at iblock for inode, possibly allocating if create */
2201da177e4SLinus Torvalds int hfsplus_get_block(struct inode *inode, sector_t iblock,
2211da177e4SLinus Torvalds 		      struct buffer_head *bh_result, int create)
2221da177e4SLinus Torvalds {
223dd73a01aSChristoph Hellwig 	struct super_block *sb = inode->i_sb;
224dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
2256af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
2261da177e4SLinus Torvalds 	int res = -EIO;
2271da177e4SLinus Torvalds 	u32 ablock, dblock, mask;
228bf1a1b31SChristoph Hellwig 	sector_t sector;
229e3494705SChristoph Hellwig 	int was_dirty = 0;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/* Convert inode block to disk allocation block */
232dd73a01aSChristoph Hellwig 	ablock = iblock >> sbi->fs_shift;
2331da177e4SLinus Torvalds 
2346af502deSChristoph Hellwig 	if (iblock >= hip->fs_blocks) {
2356af502deSChristoph Hellwig 		if (iblock > hip->fs_blocks || !create)
2361da177e4SLinus Torvalds 			return -EIO;
2376af502deSChristoph Hellwig 		if (ablock >= hip->alloc_blocks) {
2381da177e4SLinus Torvalds 			res = hfsplus_file_extend(inode);
2391da177e4SLinus Torvalds 			if (res)
2401da177e4SLinus Torvalds 				return res;
2411da177e4SLinus Torvalds 		}
2421da177e4SLinus Torvalds 	} else
2431da177e4SLinus Torvalds 		create = 0;
2441da177e4SLinus Torvalds 
2456af502deSChristoph Hellwig 	if (ablock < hip->first_blocks) {
2466af502deSChristoph Hellwig 		dblock = hfsplus_ext_find_block(hip->first_extents, ablock);
2471da177e4SLinus Torvalds 		goto done;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 
250248736c2SEric Sesterhenn 	if (inode->i_ino == HFSPLUS_EXT_CNID)
251248736c2SEric Sesterhenn 		return -EIO;
252248736c2SEric Sesterhenn 
2536af502deSChristoph Hellwig 	mutex_lock(&hip->extents_lock);
254e3494705SChristoph Hellwig 
255e3494705SChristoph Hellwig 	/*
256e3494705SChristoph Hellwig 	 * hfsplus_ext_read_extent will write out a cached extent into
257e3494705SChristoph Hellwig 	 * the extents btree.  In that case we may have to mark the inode
258e3494705SChristoph Hellwig 	 * dirty even for a pure read of an extent here.
259e3494705SChristoph Hellwig 	 */
260e3494705SChristoph Hellwig 	was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
2611da177e4SLinus Torvalds 	res = hfsplus_ext_read_extent(inode, ablock);
262e3494705SChristoph Hellwig 	if (res) {
2636af502deSChristoph Hellwig 		mutex_unlock(&hip->extents_lock);
2641da177e4SLinus Torvalds 		return -EIO;
2651da177e4SLinus Torvalds 	}
266e3494705SChristoph Hellwig 	dblock = hfsplus_ext_find_block(hip->cached_extents,
267e3494705SChristoph Hellwig 					ablock - hip->cached_start);
2686af502deSChristoph Hellwig 	mutex_unlock(&hip->extents_lock);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds done:
271c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "get_block(%lu): %llu - %u\n",
2722753cc28SAnton Salikhmetov 		inode->i_ino, (long long)iblock, dblock);
273bf1a1b31SChristoph Hellwig 
274dd73a01aSChristoph Hellwig 	mask = (1 << sbi->fs_shift) - 1;
275bf1a1b31SChristoph Hellwig 	sector = ((sector_t)dblock << sbi->fs_shift) +
276bf1a1b31SChristoph Hellwig 		  sbi->blockoffset + (iblock & mask);
277bf1a1b31SChristoph Hellwig 	map_bh(bh_result, sb, sector);
278bf1a1b31SChristoph Hellwig 
2791da177e4SLinus Torvalds 	if (create) {
2801da177e4SLinus Torvalds 		set_buffer_new(bh_result);
2816af502deSChristoph Hellwig 		hip->phys_size += sb->s_blocksize;
2826af502deSChristoph Hellwig 		hip->fs_blocks++;
2831da177e4SLinus Torvalds 		inode_add_bytes(inode, sb->s_blocksize);
2841da177e4SLinus Torvalds 	}
285e3494705SChristoph Hellwig 	if (create || was_dirty)
286e3494705SChristoph Hellwig 		mark_inode_dirty(inode);
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static void hfsplus_dump_extent(struct hfsplus_extent *extent)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	int i;
2931da177e4SLinus Torvalds 
294c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "   ");
2951da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
296c2b3e1f7SJoe Perches 		hfs_dbg_cont(EXTENT, " %u:%u",
297c2b3e1f7SJoe Perches 			     be32_to_cpu(extent[i].start_block),
2981da177e4SLinus Torvalds 			     be32_to_cpu(extent[i].block_count));
299c2b3e1f7SJoe Perches 	hfs_dbg_cont(EXTENT, "\n");
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
3031da177e4SLinus Torvalds 			      u32 alloc_block, u32 block_count)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	u32 count, start;
3061da177e4SLinus Torvalds 	int i;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	hfsplus_dump_extent(extent);
3091da177e4SLinus Torvalds 	for (i = 0; i < 8; extent++, i++) {
3101da177e4SLinus Torvalds 		count = be32_to_cpu(extent->block_count);
3111da177e4SLinus Torvalds 		if (offset == count) {
3121da177e4SLinus Torvalds 			start = be32_to_cpu(extent->start_block);
3131da177e4SLinus Torvalds 			if (alloc_block != start + count) {
3141da177e4SLinus Torvalds 				if (++i >= 8)
3151da177e4SLinus Torvalds 					return -ENOSPC;
3161da177e4SLinus Torvalds 				extent++;
3171da177e4SLinus Torvalds 				extent->start_block = cpu_to_be32(alloc_block);
3181da177e4SLinus Torvalds 			} else
3191da177e4SLinus Torvalds 				block_count += count;
3201da177e4SLinus Torvalds 			extent->block_count = cpu_to_be32(block_count);
3211da177e4SLinus Torvalds 			return 0;
3221da177e4SLinus Torvalds 		} else if (offset < count)
3231da177e4SLinus Torvalds 			break;
3241da177e4SLinus Torvalds 		offset -= count;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 	/* panic? */
3271da177e4SLinus Torvalds 	return -EIO;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds static int hfsplus_free_extents(struct super_block *sb,
3311da177e4SLinus Torvalds 				struct hfsplus_extent *extent,
3321da177e4SLinus Torvalds 				u32 offset, u32 block_nr)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	u32 count, start;
3351da177e4SLinus Torvalds 	int i;
3361b243fd3SVyacheslav Dubeyko 	int err = 0;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	hfsplus_dump_extent(extent);
3391da177e4SLinus Torvalds 	for (i = 0; i < 8; extent++, i++) {
3401da177e4SLinus Torvalds 		count = be32_to_cpu(extent->block_count);
3411da177e4SLinus Torvalds 		if (offset == count)
3421da177e4SLinus Torvalds 			goto found;
3431da177e4SLinus Torvalds 		else if (offset < count)
3441da177e4SLinus Torvalds 			break;
3451da177e4SLinus Torvalds 		offset -= count;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	/* panic? */
3481da177e4SLinus Torvalds 	return -EIO;
3491da177e4SLinus Torvalds found:
3501da177e4SLinus Torvalds 	for (;;) {
3511da177e4SLinus Torvalds 		start = be32_to_cpu(extent->start_block);
3521da177e4SLinus Torvalds 		if (count <= block_nr) {
3531b243fd3SVyacheslav Dubeyko 			err = hfsplus_block_free(sb, start, count);
3541b243fd3SVyacheslav Dubeyko 			if (err) {
355d6142673SJoe Perches 				pr_err("can't free extent\n");
356c2b3e1f7SJoe Perches 				hfs_dbg(EXTENT, " start: %u count: %u\n",
3571b243fd3SVyacheslav Dubeyko 					start, count);
3581b243fd3SVyacheslav Dubeyko 			}
3591da177e4SLinus Torvalds 			extent->block_count = 0;
3601da177e4SLinus Torvalds 			extent->start_block = 0;
3611da177e4SLinus Torvalds 			block_nr -= count;
3621da177e4SLinus Torvalds 		} else {
3631da177e4SLinus Torvalds 			count -= block_nr;
3641b243fd3SVyacheslav Dubeyko 			err = hfsplus_block_free(sb, start + count, block_nr);
3651b243fd3SVyacheslav Dubeyko 			if (err) {
366d6142673SJoe Perches 				pr_err("can't free extent\n");
367c2b3e1f7SJoe Perches 				hfs_dbg(EXTENT, " start: %u count: %u\n",
3681b243fd3SVyacheslav Dubeyko 					start, count);
3691b243fd3SVyacheslav Dubeyko 			}
3701da177e4SLinus Torvalds 			extent->block_count = cpu_to_be32(count);
3711da177e4SLinus Torvalds 			block_nr = 0;
3721da177e4SLinus Torvalds 		}
3731b243fd3SVyacheslav Dubeyko 		if (!block_nr || !i) {
3741b243fd3SVyacheslav Dubeyko 			/*
3751b243fd3SVyacheslav Dubeyko 			 * Try to free all extents and
3761b243fd3SVyacheslav Dubeyko 			 * return only last error
3771b243fd3SVyacheslav Dubeyko 			 */
3781b243fd3SVyacheslav Dubeyko 			return err;
3791b243fd3SVyacheslav Dubeyko 		}
3801da177e4SLinus Torvalds 		i--;
3811da177e4SLinus Torvalds 		extent--;
3821da177e4SLinus Torvalds 		count = be32_to_cpu(extent->block_count);
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3862753cc28SAnton Salikhmetov int hfsplus_free_fork(struct super_block *sb, u32 cnid,
3872753cc28SAnton Salikhmetov 		struct hfsplus_fork_raw *fork, int type)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	struct hfs_find_data fd;
3901da177e4SLinus Torvalds 	hfsplus_extent_rec ext_entry;
3911da177e4SLinus Torvalds 	u32 total_blocks, blocks, start;
3921da177e4SLinus Torvalds 	int res, i;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	total_blocks = be32_to_cpu(fork->total_blocks);
3951da177e4SLinus Torvalds 	if (!total_blocks)
3961da177e4SLinus Torvalds 		return 0;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	blocks = 0;
3991da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
4001da177e4SLinus Torvalds 		blocks += be32_to_cpu(fork->extents[i].block_count);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
4031da177e4SLinus Torvalds 	if (res)
4041da177e4SLinus Torvalds 		return res;
4051da177e4SLinus Torvalds 	if (total_blocks == blocks)
4061da177e4SLinus Torvalds 		return 0;
4071da177e4SLinus Torvalds 
4085bd9d99dSAlexey Khoroshilov 	res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
4095bd9d99dSAlexey Khoroshilov 	if (res)
4105bd9d99dSAlexey Khoroshilov 		return res;
4111da177e4SLinus Torvalds 	do {
4121da177e4SLinus Torvalds 		res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
4131da177e4SLinus Torvalds 						total_blocks, type);
4141da177e4SLinus Torvalds 		if (res)
4151da177e4SLinus Torvalds 			break;
4161da177e4SLinus Torvalds 		start = be32_to_cpu(fd.key->ext.start_block);
4171da177e4SLinus Torvalds 		hfsplus_free_extents(sb, ext_entry,
4181da177e4SLinus Torvalds 				     total_blocks - start,
4191da177e4SLinus Torvalds 				     total_blocks);
4201da177e4SLinus Torvalds 		hfs_brec_remove(&fd);
4211da177e4SLinus Torvalds 		total_blocks = start;
4221da177e4SLinus Torvalds 	} while (total_blocks > blocks);
4231da177e4SLinus Torvalds 	hfs_find_exit(&fd);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	return res;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds int hfsplus_file_extend(struct inode *inode)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
431dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
4326af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
4331da177e4SLinus Torvalds 	u32 start, len, goal;
4341da177e4SLinus Torvalds 	int res;
4351da177e4SLinus Torvalds 
4361065348dSChristoph Hellwig 	if (sbi->alloc_file->i_size * 8 <
4371065348dSChristoph Hellwig 	    sbi->total_blocks - sbi->free_blocks + 8) {
43821f2296aSAnton Salikhmetov 		/* extend alloc file */
439d6142673SJoe Perches 		pr_err("extend alloc file! "
440b2837fcfSAnton Salikhmetov 				"(%llu,%u,%u)\n",
441dd73a01aSChristoph Hellwig 			sbi->alloc_file->i_size * 8,
442dd73a01aSChristoph Hellwig 			sbi->total_blocks, sbi->free_blocks);
4431da177e4SLinus Torvalds 		return -ENOSPC;
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4466af502deSChristoph Hellwig 	mutex_lock(&hip->extents_lock);
4476af502deSChristoph Hellwig 	if (hip->alloc_blocks == hip->first_blocks)
4486af502deSChristoph Hellwig 		goal = hfsplus_ext_lastblock(hip->first_extents);
4491da177e4SLinus Torvalds 	else {
4506af502deSChristoph Hellwig 		res = hfsplus_ext_read_extent(inode, hip->alloc_blocks);
4511da177e4SLinus Torvalds 		if (res)
4521da177e4SLinus Torvalds 			goto out;
4536af502deSChristoph Hellwig 		goal = hfsplus_ext_lastblock(hip->cached_extents);
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 
4566af502deSChristoph Hellwig 	len = hip->clump_blocks;
457dd73a01aSChristoph Hellwig 	start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len);
458dd73a01aSChristoph Hellwig 	if (start >= sbi->total_blocks) {
4591da177e4SLinus Torvalds 		start = hfsplus_block_allocate(sb, goal, 0, &len);
4601da177e4SLinus Torvalds 		if (start >= goal) {
4611da177e4SLinus Torvalds 			res = -ENOSPC;
4621da177e4SLinus Torvalds 			goto out;
4631da177e4SLinus Torvalds 		}
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 
466c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
4676af502deSChristoph Hellwig 
4686af502deSChristoph Hellwig 	if (hip->alloc_blocks <= hip->first_blocks) {
4696af502deSChristoph Hellwig 		if (!hip->first_blocks) {
470c2b3e1f7SJoe Perches 			hfs_dbg(EXTENT, "first extents\n");
4711da177e4SLinus Torvalds 			/* no extents yet */
4726af502deSChristoph Hellwig 			hip->first_extents[0].start_block = cpu_to_be32(start);
4736af502deSChristoph Hellwig 			hip->first_extents[0].block_count = cpu_to_be32(len);
4741da177e4SLinus Torvalds 			res = 0;
4751da177e4SLinus Torvalds 		} else {
4761da177e4SLinus Torvalds 			/* try to append to extents in inode */
4776af502deSChristoph Hellwig 			res = hfsplus_add_extent(hip->first_extents,
4786af502deSChristoph Hellwig 						 hip->alloc_blocks,
4791da177e4SLinus Torvalds 						 start, len);
4801da177e4SLinus Torvalds 			if (res == -ENOSPC)
4811da177e4SLinus Torvalds 				goto insert_extent;
4821da177e4SLinus Torvalds 		}
4831da177e4SLinus Torvalds 		if (!res) {
4846af502deSChristoph Hellwig 			hfsplus_dump_extent(hip->first_extents);
4856af502deSChristoph Hellwig 			hip->first_blocks += len;
4861da177e4SLinus Torvalds 		}
4871da177e4SLinus Torvalds 	} else {
4886af502deSChristoph Hellwig 		res = hfsplus_add_extent(hip->cached_extents,
4896af502deSChristoph Hellwig 					 hip->alloc_blocks - hip->cached_start,
4901da177e4SLinus Torvalds 					 start, len);
4911da177e4SLinus Torvalds 		if (!res) {
4926af502deSChristoph Hellwig 			hfsplus_dump_extent(hip->cached_extents);
493b33b7921SChristoph Hellwig 			hip->extent_state |= HFSPLUS_EXT_DIRTY;
4946af502deSChristoph Hellwig 			hip->cached_blocks += len;
4951da177e4SLinus Torvalds 		} else if (res == -ENOSPC)
4961da177e4SLinus Torvalds 			goto insert_extent;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds out:
4991da177e4SLinus Torvalds 	if (!res) {
5006af502deSChristoph Hellwig 		hip->alloc_blocks += len;
501d7bdb996SSougata Santra 		mutex_unlock(&hip->extents_lock);
502e3494705SChristoph Hellwig 		hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
503d7bdb996SSougata Santra 		return 0;
5041da177e4SLinus Torvalds 	}
505d7bdb996SSougata Santra 	mutex_unlock(&hip->extents_lock);
5061da177e4SLinus Torvalds 	return res;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds insert_extent:
509c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "insert new extent\n");
510dd7f3d54SAlexey Khoroshilov 	res = hfsplus_ext_write_extent_locked(inode);
511dd7f3d54SAlexey Khoroshilov 	if (res)
512dd7f3d54SAlexey Khoroshilov 		goto out;
5131da177e4SLinus Torvalds 
5146af502deSChristoph Hellwig 	memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
5156af502deSChristoph Hellwig 	hip->cached_extents[0].start_block = cpu_to_be32(start);
5166af502deSChristoph Hellwig 	hip->cached_extents[0].block_count = cpu_to_be32(len);
5176af502deSChristoph Hellwig 	hfsplus_dump_extent(hip->cached_extents);
518b33b7921SChristoph Hellwig 	hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW;
5196af502deSChristoph Hellwig 	hip->cached_start = hip->alloc_blocks;
5206af502deSChristoph Hellwig 	hip->cached_blocks = len;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	res = 0;
5231da177e4SLinus Torvalds 	goto out;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds void hfsplus_file_truncate(struct inode *inode)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
5296af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
5301da177e4SLinus Torvalds 	struct hfs_find_data fd;
5311da177e4SLinus Torvalds 	u32 alloc_cnt, blk_cnt, start;
5321da177e4SLinus Torvalds 	int res;
5331da177e4SLinus Torvalds 
534c2b3e1f7SJoe Perches 	hfs_dbg(INODE, "truncate: %lu, %llu -> %llu\n",
535c2b3e1f7SJoe Perches 		inode->i_ino, (long long)hip->phys_size, inode->i_size);
5366af502deSChristoph Hellwig 
5376af502deSChristoph Hellwig 	if (inode->i_size > hip->phys_size) {
5381da177e4SLinus Torvalds 		struct address_space *mapping = inode->i_mapping;
5391da177e4SLinus Torvalds 		struct page *page;
5407c0efc62SNick Piggin 		void *fsdata;
54112f267a2SVyacheslav Dubeyko 		loff_t size = inode->i_size;
5421da177e4SLinus Torvalds 
5437c0efc62SNick Piggin 		res = pagecache_write_begin(NULL, mapping, size, 0,
5447c0efc62SNick Piggin 						AOP_FLAG_UNINTERRUPTIBLE,
5457c0efc62SNick Piggin 						&page, &fsdata);
5461da177e4SLinus Torvalds 		if (res)
5477c0efc62SNick Piggin 			return;
5482753cc28SAnton Salikhmetov 		res = pagecache_write_end(NULL, mapping, size,
5492753cc28SAnton Salikhmetov 			0, 0, page, fsdata);
5507c0efc62SNick Piggin 		if (res < 0)
5517c0efc62SNick Piggin 			return;
5521da177e4SLinus Torvalds 		mark_inode_dirty(inode);
5531da177e4SLinus Torvalds 		return;
5546af502deSChristoph Hellwig 	} else if (inode->i_size == hip->phys_size)
555f76d28d2SRoman Zippel 		return;
556f76d28d2SRoman Zippel 
557dd73a01aSChristoph Hellwig 	blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >>
558dd73a01aSChristoph Hellwig 			HFSPLUS_SB(sb)->alloc_blksz_shift;
5591da177e4SLinus Torvalds 
5606af502deSChristoph Hellwig 	mutex_lock(&hip->extents_lock);
561d7bdb996SSougata Santra 
562d7bdb996SSougata Santra 	alloc_cnt = hip->alloc_blocks;
563d7bdb996SSougata Santra 	if (blk_cnt == alloc_cnt)
564d7bdb996SSougata Santra 		goto out_unlock;
565d7bdb996SSougata Santra 
5665bd9d99dSAlexey Khoroshilov 	res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
5675bd9d99dSAlexey Khoroshilov 	if (res) {
5685bd9d99dSAlexey Khoroshilov 		mutex_unlock(&hip->extents_lock);
5695bd9d99dSAlexey Khoroshilov 		/* XXX: We lack error handling of hfsplus_file_truncate() */
5705bd9d99dSAlexey Khoroshilov 		return;
5715bd9d99dSAlexey Khoroshilov 	}
5721da177e4SLinus Torvalds 	while (1) {
5736af502deSChristoph Hellwig 		if (alloc_cnt == hip->first_blocks) {
5746af502deSChristoph Hellwig 			hfsplus_free_extents(sb, hip->first_extents,
5751da177e4SLinus Torvalds 					     alloc_cnt, alloc_cnt - blk_cnt);
5766af502deSChristoph Hellwig 			hfsplus_dump_extent(hip->first_extents);
5776af502deSChristoph Hellwig 			hip->first_blocks = blk_cnt;
5781da177e4SLinus Torvalds 			break;
5791da177e4SLinus Torvalds 		}
5801da177e4SLinus Torvalds 		res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
5811da177e4SLinus Torvalds 		if (res)
5821da177e4SLinus Torvalds 			break;
5836af502deSChristoph Hellwig 		start = hip->cached_start;
5846af502deSChristoph Hellwig 		hfsplus_free_extents(sb, hip->cached_extents,
5851da177e4SLinus Torvalds 				     alloc_cnt - start, alloc_cnt - blk_cnt);
5866af502deSChristoph Hellwig 		hfsplus_dump_extent(hip->cached_extents);
5871da177e4SLinus Torvalds 		if (blk_cnt > start) {
588b33b7921SChristoph Hellwig 			hip->extent_state |= HFSPLUS_EXT_DIRTY;
5891da177e4SLinus Torvalds 			break;
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 		alloc_cnt = start;
5926af502deSChristoph Hellwig 		hip->cached_start = hip->cached_blocks = 0;
593b33b7921SChristoph Hellwig 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
5941da177e4SLinus Torvalds 		hfs_brec_remove(&fd);
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	hfs_find_exit(&fd);
5971da177e4SLinus Torvalds 
5986af502deSChristoph Hellwig 	hip->alloc_blocks = blk_cnt;
599d7bdb996SSougata Santra out_unlock:
600d7bdb996SSougata Santra 	mutex_unlock(&hip->extents_lock);
6016af502deSChristoph Hellwig 	hip->phys_size = inode->i_size;
6022753cc28SAnton Salikhmetov 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
6032753cc28SAnton Salikhmetov 		sb->s_blocksize_bits;
6046af502deSChristoph Hellwig 	inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
605e3494705SChristoph Hellwig 	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
6061da177e4SLinus Torvalds }
607