xref: /openbmc/linux/fs/hfsplus/extents.c (revision d9efb07d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/hfsplus/extents.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2001
61da177e4SLinus Torvalds  * Brad Boyer (flar@allandria.com)
71da177e4SLinus Torvalds  * (C) 2003 Ardis Technologies <roman@ardistech.com>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Handling of Extents both in catalog and extents overflow trees
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/pagemap.h>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include "hfsplus_fs.h"
171da177e4SLinus Torvalds #include "hfsplus_raw.h"
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds /* Compare two extents keys, returns 0 on same, pos/neg for difference */
hfsplus_ext_cmp_key(const hfsplus_btree_key * k1,const hfsplus_btree_key * k2)202179d372SDavid Elliott int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
212179d372SDavid Elliott 			const hfsplus_btree_key *k2)
221da177e4SLinus Torvalds {
231da177e4SLinus Torvalds 	__be32 k1id, k2id;
241da177e4SLinus Torvalds 	__be32 k1s, k2s;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds 	k1id = k1->ext.cnid;
271da177e4SLinus Torvalds 	k2id = k2->ext.cnid;
281da177e4SLinus Torvalds 	if (k1id != k2id)
291da177e4SLinus Torvalds 		return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	if (k1->ext.fork_type != k2->ext.fork_type)
321da177e4SLinus Torvalds 		return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	k1s = k1->ext.start_block;
351da177e4SLinus Torvalds 	k2s = k2->ext.start_block;
361da177e4SLinus Torvalds 	if (k1s == k2s)
371da177e4SLinus Torvalds 		return 0;
381da177e4SLinus Torvalds 	return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
hfsplus_ext_build_key(hfsplus_btree_key * key,u32 cnid,u32 block,u8 type)411da177e4SLinus Torvalds static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid,
421da177e4SLinus Torvalds 				  u32 block, u8 type)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
451da177e4SLinus Torvalds 	key->ext.cnid = cpu_to_be32(cnid);
461da177e4SLinus Torvalds 	key->ext.start_block = cpu_to_be32(block);
471da177e4SLinus Torvalds 	key->ext.fork_type = type;
481da177e4SLinus Torvalds 	key->ext.pad = 0;
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
hfsplus_ext_find_block(struct hfsplus_extent * ext,u32 off)511da177e4SLinus Torvalds static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	int i;
541da177e4SLinus Torvalds 	u32 count;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	for (i = 0; i < 8; ext++, i++) {
571da177e4SLinus Torvalds 		count = be32_to_cpu(ext->block_count);
581da177e4SLinus Torvalds 		if (off < count)
591da177e4SLinus Torvalds 			return be32_to_cpu(ext->start_block) + off;
601da177e4SLinus Torvalds 		off -= count;
611da177e4SLinus Torvalds 	}
621da177e4SLinus Torvalds 	/* panic? */
631da177e4SLinus Torvalds 	return 0;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
hfsplus_ext_block_count(struct hfsplus_extent * ext)661da177e4SLinus Torvalds static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	int i;
691da177e4SLinus Torvalds 	u32 count = 0;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	for (i = 0; i < 8; ext++, i++)
721da177e4SLinus Torvalds 		count += be32_to_cpu(ext->block_count);
731da177e4SLinus Torvalds 	return count;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
hfsplus_ext_lastblock(struct hfsplus_extent * ext)761da177e4SLinus Torvalds static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	int i;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	ext += 7;
811da177e4SLinus Torvalds 	for (i = 0; i < 7; ext--, i++)
821da177e4SLinus Torvalds 		if (ext->block_count)
831da177e4SLinus Torvalds 			break;
841da177e4SLinus Torvalds 	return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
__hfsplus_ext_write_extent(struct inode * inode,struct hfs_find_data * fd)87d7a475d0SAlexey Khoroshilov static int __hfsplus_ext_write_extent(struct inode *inode,
882753cc28SAnton Salikhmetov 		struct hfs_find_data *fd)
891da177e4SLinus Torvalds {
906af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
911da177e4SLinus Torvalds 	int res;
921da177e4SLinus Torvalds 
937fcc99f4SChristoph Hellwig 	WARN_ON(!mutex_is_locked(&hip->extents_lock));
947fcc99f4SChristoph Hellwig 
956af502deSChristoph Hellwig 	hfsplus_ext_build_key(fd->search_key, inode->i_ino, hip->cached_start,
966af502deSChristoph Hellwig 			      HFSPLUS_IS_RSRC(inode) ?
976af502deSChristoph Hellwig 				HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
986af502deSChristoph Hellwig 
99324ef39aSVyacheslav Dubeyko 	res = hfs_brec_find(fd, hfs_find_rec_by_key);
100b33b7921SChristoph Hellwig 	if (hip->extent_state & HFSPLUS_EXT_NEW) {
1011da177e4SLinus Torvalds 		if (res != -ENOENT)
102d7a475d0SAlexey Khoroshilov 			return res;
103d92915c3SErnesto A. Fernández 		/* Fail early and avoid ENOSPC during the btree operation */
104d92915c3SErnesto A. Fernández 		res = hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
105d92915c3SErnesto A. Fernández 		if (res)
106d92915c3SErnesto A. Fernández 			return res;
1076af502deSChristoph Hellwig 		hfs_brec_insert(fd, hip->cached_extents,
1086af502deSChristoph Hellwig 				sizeof(hfsplus_extent_rec));
109b33b7921SChristoph Hellwig 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
1101da177e4SLinus Torvalds 	} else {
1111da177e4SLinus Torvalds 		if (res)
112d7a475d0SAlexey Khoroshilov 			return res;
1136af502deSChristoph Hellwig 		hfs_bnode_write(fd->bnode, hip->cached_extents,
1146af502deSChristoph Hellwig 				fd->entryoffset, fd->entrylength);
115b33b7921SChristoph Hellwig 		hip->extent_state &= ~HFSPLUS_EXT_DIRTY;
1161da177e4SLinus Torvalds 	}
117e3494705SChristoph Hellwig 
118e3494705SChristoph Hellwig 	/*
119e3494705SChristoph Hellwig 	 * We can't just use hfsplus_mark_inode_dirty here, because we
120e3494705SChristoph Hellwig 	 * also get called from hfsplus_write_inode, which should not
121e3494705SChristoph Hellwig 	 * redirty the inode.  Instead the callers have to be careful
122e3494705SChristoph Hellwig 	 * to explicily mark the inode dirty, too.
123e3494705SChristoph Hellwig 	 */
124e3494705SChristoph Hellwig 	set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags);
125d7a475d0SAlexey Khoroshilov 
126d7a475d0SAlexey Khoroshilov 	return 0;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
hfsplus_ext_write_extent_locked(struct inode * inode)129dd7f3d54SAlexey Khoroshilov static int hfsplus_ext_write_extent_locked(struct inode *inode)
1301da177e4SLinus Torvalds {
131d7a475d0SAlexey Khoroshilov 	int res = 0;
132dd7f3d54SAlexey Khoroshilov 
133b33b7921SChristoph Hellwig 	if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
1341da177e4SLinus Torvalds 		struct hfs_find_data fd;
1351da177e4SLinus Torvalds 
136dd7f3d54SAlexey Khoroshilov 		res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
137dd7f3d54SAlexey Khoroshilov 		if (res)
138dd7f3d54SAlexey Khoroshilov 			return res;
139d7a475d0SAlexey Khoroshilov 		res = __hfsplus_ext_write_extent(inode, &fd);
1401da177e4SLinus Torvalds 		hfs_find_exit(&fd);
1411da177e4SLinus Torvalds 	}
142d7a475d0SAlexey Khoroshilov 	return res;
1435bd9d99dSAlexey Khoroshilov }
1441da177e4SLinus Torvalds 
hfsplus_ext_write_extent(struct inode * inode)145dd7f3d54SAlexey Khoroshilov int hfsplus_ext_write_extent(struct inode *inode)
1467fcc99f4SChristoph Hellwig {
147dd7f3d54SAlexey Khoroshilov 	int res;
148dd7f3d54SAlexey Khoroshilov 
1497fcc99f4SChristoph Hellwig 	mutex_lock(&HFSPLUS_I(inode)->extents_lock);
150dd7f3d54SAlexey Khoroshilov 	res = hfsplus_ext_write_extent_locked(inode);
1517fcc99f4SChristoph Hellwig 	mutex_unlock(&HFSPLUS_I(inode)->extents_lock);
152dd7f3d54SAlexey Khoroshilov 
153dd7f3d54SAlexey Khoroshilov 	return res;
1547fcc99f4SChristoph Hellwig }
1557fcc99f4SChristoph Hellwig 
__hfsplus_ext_read_extent(struct hfs_find_data * fd,struct hfsplus_extent * extent,u32 cnid,u32 block,u8 type)1561da177e4SLinus Torvalds static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
1571da177e4SLinus Torvalds 					    struct hfsplus_extent *extent,
1581da177e4SLinus Torvalds 					    u32 cnid, u32 block, u8 type)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	int res;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	hfsplus_ext_build_key(fd->search_key, cnid, block, type);
1631da177e4SLinus Torvalds 	fd->key->ext.cnid = 0;
164324ef39aSVyacheslav Dubeyko 	res = hfs_brec_find(fd, hfs_find_rec_by_key);
1651da177e4SLinus Torvalds 	if (res && res != -ENOENT)
1661da177e4SLinus Torvalds 		return res;
1671da177e4SLinus Torvalds 	if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
1681da177e4SLinus Torvalds 	    fd->key->ext.fork_type != fd->search_key->ext.fork_type)
1691da177e4SLinus Torvalds 		return -ENOENT;
1701da177e4SLinus Torvalds 	if (fd->entrylength != sizeof(hfsplus_extent_rec))
1711da177e4SLinus Torvalds 		return -EIO;
1722753cc28SAnton Salikhmetov 	hfs_bnode_read(fd->bnode, extent, fd->entryoffset,
1732753cc28SAnton Salikhmetov 		sizeof(hfsplus_extent_rec));
1741da177e4SLinus Torvalds 	return 0;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
__hfsplus_ext_cache_extent(struct hfs_find_data * fd,struct inode * inode,u32 block)1772753cc28SAnton Salikhmetov static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd,
1782753cc28SAnton Salikhmetov 		struct inode *inode, u32 block)
1791da177e4SLinus Torvalds {
1806af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
1811da177e4SLinus Torvalds 	int res;
1821da177e4SLinus Torvalds 
1837fcc99f4SChristoph Hellwig 	WARN_ON(!mutex_is_locked(&hip->extents_lock));
1847fcc99f4SChristoph Hellwig 
185d7a475d0SAlexey Khoroshilov 	if (hip->extent_state & HFSPLUS_EXT_DIRTY) {
186d7a475d0SAlexey Khoroshilov 		res = __hfsplus_ext_write_extent(inode, fd);
187d7a475d0SAlexey Khoroshilov 		if (res)
188d7a475d0SAlexey Khoroshilov 			return res;
189d7a475d0SAlexey Khoroshilov 	}
1901da177e4SLinus Torvalds 
1916af502deSChristoph Hellwig 	res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino,
1926af502deSChristoph Hellwig 					block, HFSPLUS_IS_RSRC(inode) ?
1936af502deSChristoph Hellwig 						HFSPLUS_TYPE_RSRC :
1946af502deSChristoph Hellwig 						HFSPLUS_TYPE_DATA);
1951da177e4SLinus Torvalds 	if (!res) {
1966af502deSChristoph Hellwig 		hip->cached_start = be32_to_cpu(fd->key->ext.start_block);
1972753cc28SAnton Salikhmetov 		hip->cached_blocks =
1982753cc28SAnton Salikhmetov 			hfsplus_ext_block_count(hip->cached_extents);
1991da177e4SLinus Torvalds 	} else {
2006af502deSChristoph Hellwig 		hip->cached_start = hip->cached_blocks = 0;
201b33b7921SChristoph Hellwig 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 	return res;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
hfsplus_ext_read_extent(struct inode * inode,u32 block)2061da177e4SLinus Torvalds static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
2071da177e4SLinus Torvalds {
2086af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
2091da177e4SLinus Torvalds 	struct hfs_find_data fd;
2101da177e4SLinus Torvalds 	int res;
2111da177e4SLinus Torvalds 
2126af502deSChristoph Hellwig 	if (block >= hip->cached_start &&
2136af502deSChristoph Hellwig 	    block < hip->cached_start + hip->cached_blocks)
2141da177e4SLinus Torvalds 		return 0;
2151da177e4SLinus Torvalds 
2165bd9d99dSAlexey Khoroshilov 	res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
2175bd9d99dSAlexey Khoroshilov 	if (!res) {
2181da177e4SLinus Torvalds 		res = __hfsplus_ext_cache_extent(&fd, inode, block);
2191da177e4SLinus Torvalds 		hfs_find_exit(&fd);
2205bd9d99dSAlexey Khoroshilov 	}
2211da177e4SLinus Torvalds 	return res;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /* Get a block at iblock for inode, possibly allocating if create */
hfsplus_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)2251da177e4SLinus Torvalds int hfsplus_get_block(struct inode *inode, sector_t iblock,
2261da177e4SLinus Torvalds 		      struct buffer_head *bh_result, int create)
2271da177e4SLinus Torvalds {
228dd73a01aSChristoph Hellwig 	struct super_block *sb = inode->i_sb;
229dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
2306af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
2311da177e4SLinus Torvalds 	int res = -EIO;
2321da177e4SLinus Torvalds 	u32 ablock, dblock, mask;
233bf1a1b31SChristoph Hellwig 	sector_t sector;
234e3494705SChristoph Hellwig 	int was_dirty = 0;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* Convert inode block to disk allocation block */
237dd73a01aSChristoph Hellwig 	ablock = iblock >> sbi->fs_shift;
2381da177e4SLinus Torvalds 
2396af502deSChristoph Hellwig 	if (iblock >= hip->fs_blocks) {
240839c3a6aSErnesto A. Fernández 		if (!create)
241839c3a6aSErnesto A. Fernández 			return 0;
242839c3a6aSErnesto A. Fernández 		if (iblock > hip->fs_blocks)
2431da177e4SLinus Torvalds 			return -EIO;
2446af502deSChristoph Hellwig 		if (ablock >= hip->alloc_blocks) {
2452cd282a1SSergei Antonov 			res = hfsplus_file_extend(inode, false);
2461da177e4SLinus Torvalds 			if (res)
2471da177e4SLinus Torvalds 				return res;
2481da177e4SLinus Torvalds 		}
2491da177e4SLinus Torvalds 	} else
2501da177e4SLinus Torvalds 		create = 0;
2511da177e4SLinus Torvalds 
2526af502deSChristoph Hellwig 	if (ablock < hip->first_blocks) {
2536af502deSChristoph Hellwig 		dblock = hfsplus_ext_find_block(hip->first_extents, ablock);
2541da177e4SLinus Torvalds 		goto done;
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 
257248736c2SEric Sesterhenn 	if (inode->i_ino == HFSPLUS_EXT_CNID)
258248736c2SEric Sesterhenn 		return -EIO;
259248736c2SEric Sesterhenn 
2606af502deSChristoph Hellwig 	mutex_lock(&hip->extents_lock);
261e3494705SChristoph Hellwig 
262e3494705SChristoph Hellwig 	/*
263e3494705SChristoph Hellwig 	 * hfsplus_ext_read_extent will write out a cached extent into
264e3494705SChristoph Hellwig 	 * the extents btree.  In that case we may have to mark the inode
265e3494705SChristoph Hellwig 	 * dirty even for a pure read of an extent here.
266e3494705SChristoph Hellwig 	 */
267e3494705SChristoph Hellwig 	was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
2681da177e4SLinus Torvalds 	res = hfsplus_ext_read_extent(inode, ablock);
269e3494705SChristoph Hellwig 	if (res) {
2706af502deSChristoph Hellwig 		mutex_unlock(&hip->extents_lock);
2711da177e4SLinus Torvalds 		return -EIO;
2721da177e4SLinus Torvalds 	}
273e3494705SChristoph Hellwig 	dblock = hfsplus_ext_find_block(hip->cached_extents,
274e3494705SChristoph Hellwig 					ablock - hip->cached_start);
2756af502deSChristoph Hellwig 	mutex_unlock(&hip->extents_lock);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds done:
278c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "get_block(%lu): %llu - %u\n",
2792753cc28SAnton Salikhmetov 		inode->i_ino, (long long)iblock, dblock);
280bf1a1b31SChristoph Hellwig 
281dd73a01aSChristoph Hellwig 	mask = (1 << sbi->fs_shift) - 1;
282bf1a1b31SChristoph Hellwig 	sector = ((sector_t)dblock << sbi->fs_shift) +
283bf1a1b31SChristoph Hellwig 		  sbi->blockoffset + (iblock & mask);
284bf1a1b31SChristoph Hellwig 	map_bh(bh_result, sb, sector);
285bf1a1b31SChristoph Hellwig 
2861da177e4SLinus Torvalds 	if (create) {
2871da177e4SLinus Torvalds 		set_buffer_new(bh_result);
2886af502deSChristoph Hellwig 		hip->phys_size += sb->s_blocksize;
2896af502deSChristoph Hellwig 		hip->fs_blocks++;
2901da177e4SLinus Torvalds 		inode_add_bytes(inode, sb->s_blocksize);
2911da177e4SLinus Torvalds 	}
292e3494705SChristoph Hellwig 	if (create || was_dirty)
293e3494705SChristoph Hellwig 		mark_inode_dirty(inode);
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
hfsplus_dump_extent(struct hfsplus_extent * extent)2971da177e4SLinus Torvalds static void hfsplus_dump_extent(struct hfsplus_extent *extent)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	int i;
3001da177e4SLinus Torvalds 
301c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "   ");
3021da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
303c2b3e1f7SJoe Perches 		hfs_dbg_cont(EXTENT, " %u:%u",
304c2b3e1f7SJoe Perches 			     be32_to_cpu(extent[i].start_block),
3051da177e4SLinus Torvalds 			     be32_to_cpu(extent[i].block_count));
306c2b3e1f7SJoe Perches 	hfs_dbg_cont(EXTENT, "\n");
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
hfsplus_add_extent(struct hfsplus_extent * extent,u32 offset,u32 alloc_block,u32 block_count)3091da177e4SLinus Torvalds static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
3101da177e4SLinus Torvalds 			      u32 alloc_block, u32 block_count)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	u32 count, start;
3131da177e4SLinus Torvalds 	int i;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	hfsplus_dump_extent(extent);
3161da177e4SLinus Torvalds 	for (i = 0; i < 8; extent++, i++) {
3171da177e4SLinus Torvalds 		count = be32_to_cpu(extent->block_count);
3181da177e4SLinus Torvalds 		if (offset == count) {
3191da177e4SLinus Torvalds 			start = be32_to_cpu(extent->start_block);
3201da177e4SLinus Torvalds 			if (alloc_block != start + count) {
3211da177e4SLinus Torvalds 				if (++i >= 8)
3221da177e4SLinus Torvalds 					return -ENOSPC;
3231da177e4SLinus Torvalds 				extent++;
3241da177e4SLinus Torvalds 				extent->start_block = cpu_to_be32(alloc_block);
3251da177e4SLinus Torvalds 			} else
3261da177e4SLinus Torvalds 				block_count += count;
3271da177e4SLinus Torvalds 			extent->block_count = cpu_to_be32(block_count);
3281da177e4SLinus Torvalds 			return 0;
3291da177e4SLinus Torvalds 		} else if (offset < count)
3301da177e4SLinus Torvalds 			break;
3311da177e4SLinus Torvalds 		offset -= count;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	/* panic? */
3341da177e4SLinus Torvalds 	return -EIO;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
hfsplus_free_extents(struct super_block * sb,struct hfsplus_extent * extent,u32 offset,u32 block_nr)3371da177e4SLinus Torvalds static int hfsplus_free_extents(struct super_block *sb,
3381da177e4SLinus Torvalds 				struct hfsplus_extent *extent,
3391da177e4SLinus Torvalds 				u32 offset, u32 block_nr)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	u32 count, start;
3421da177e4SLinus Torvalds 	int i;
3431b243fd3SVyacheslav Dubeyko 	int err = 0;
3441da177e4SLinus Torvalds 
34531651c60SErnesto A. Fernández 	/* Mapping the allocation file may lock the extent tree */
34631651c60SErnesto A. Fernández 	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
34731651c60SErnesto A. Fernández 
3481da177e4SLinus Torvalds 	hfsplus_dump_extent(extent);
3491da177e4SLinus Torvalds 	for (i = 0; i < 8; extent++, i++) {
3501da177e4SLinus Torvalds 		count = be32_to_cpu(extent->block_count);
3511da177e4SLinus Torvalds 		if (offset == count)
3521da177e4SLinus Torvalds 			goto found;
3531da177e4SLinus Torvalds 		else if (offset < count)
3541da177e4SLinus Torvalds 			break;
3551da177e4SLinus Torvalds 		offset -= count;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 	/* panic? */
3581da177e4SLinus Torvalds 	return -EIO;
3591da177e4SLinus Torvalds found:
3601da177e4SLinus Torvalds 	for (;;) {
3611da177e4SLinus Torvalds 		start = be32_to_cpu(extent->start_block);
3621da177e4SLinus Torvalds 		if (count <= block_nr) {
3631b243fd3SVyacheslav Dubeyko 			err = hfsplus_block_free(sb, start, count);
3641b243fd3SVyacheslav Dubeyko 			if (err) {
365d6142673SJoe Perches 				pr_err("can't free extent\n");
366c2b3e1f7SJoe Perches 				hfs_dbg(EXTENT, " start: %u count: %u\n",
3671b243fd3SVyacheslav Dubeyko 					start, count);
3681b243fd3SVyacheslav Dubeyko 			}
3691da177e4SLinus Torvalds 			extent->block_count = 0;
3701da177e4SLinus Torvalds 			extent->start_block = 0;
3711da177e4SLinus Torvalds 			block_nr -= count;
3721da177e4SLinus Torvalds 		} else {
3731da177e4SLinus Torvalds 			count -= block_nr;
3741b243fd3SVyacheslav Dubeyko 			err = hfsplus_block_free(sb, start + count, block_nr);
3751b243fd3SVyacheslav Dubeyko 			if (err) {
376d6142673SJoe Perches 				pr_err("can't free extent\n");
377c2b3e1f7SJoe Perches 				hfs_dbg(EXTENT, " start: %u count: %u\n",
3781b243fd3SVyacheslav Dubeyko 					start, count);
3791b243fd3SVyacheslav Dubeyko 			}
3801da177e4SLinus Torvalds 			extent->block_count = cpu_to_be32(count);
3811da177e4SLinus Torvalds 			block_nr = 0;
3821da177e4SLinus Torvalds 		}
3831b243fd3SVyacheslav Dubeyko 		if (!block_nr || !i) {
3841b243fd3SVyacheslav Dubeyko 			/*
3851b243fd3SVyacheslav Dubeyko 			 * Try to free all extents and
3861b243fd3SVyacheslav Dubeyko 			 * return only last error
3871b243fd3SVyacheslav Dubeyko 			 */
3881b243fd3SVyacheslav Dubeyko 			return err;
3891b243fd3SVyacheslav Dubeyko 		}
3901da177e4SLinus Torvalds 		i--;
3911da177e4SLinus Torvalds 		extent--;
3921da177e4SLinus Torvalds 		count = be32_to_cpu(extent->block_count);
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
hfsplus_free_fork(struct super_block * sb,u32 cnid,struct hfsplus_fork_raw * fork,int type)3962753cc28SAnton Salikhmetov int hfsplus_free_fork(struct super_block *sb, u32 cnid,
3972753cc28SAnton Salikhmetov 		struct hfsplus_fork_raw *fork, int type)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct hfs_find_data fd;
4001da177e4SLinus Torvalds 	hfsplus_extent_rec ext_entry;
4011da177e4SLinus Torvalds 	u32 total_blocks, blocks, start;
4021da177e4SLinus Torvalds 	int res, i;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	total_blocks = be32_to_cpu(fork->total_blocks);
4051da177e4SLinus Torvalds 	if (!total_blocks)
4061da177e4SLinus Torvalds 		return 0;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	blocks = 0;
4091da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
4101da177e4SLinus Torvalds 		blocks += be32_to_cpu(fork->extents[i].block_count);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
4131da177e4SLinus Torvalds 	if (res)
4141da177e4SLinus Torvalds 		return res;
4151da177e4SLinus Torvalds 	if (total_blocks == blocks)
4161da177e4SLinus Torvalds 		return 0;
4171da177e4SLinus Torvalds 
4185bd9d99dSAlexey Khoroshilov 	res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
4195bd9d99dSAlexey Khoroshilov 	if (res)
4205bd9d99dSAlexey Khoroshilov 		return res;
4211da177e4SLinus Torvalds 	do {
4221da177e4SLinus Torvalds 		res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
4231da177e4SLinus Torvalds 						total_blocks, type);
4241da177e4SLinus Torvalds 		if (res)
4251da177e4SLinus Torvalds 			break;
4261da177e4SLinus Torvalds 		start = be32_to_cpu(fd.key->ext.start_block);
4271da177e4SLinus Torvalds 		hfs_brec_remove(&fd);
42831651c60SErnesto A. Fernández 
42931651c60SErnesto A. Fernández 		mutex_unlock(&fd.tree->tree_lock);
43031651c60SErnesto A. Fernández 		hfsplus_free_extents(sb, ext_entry, total_blocks - start,
43131651c60SErnesto A. Fernández 				     total_blocks);
4321da177e4SLinus Torvalds 		total_blocks = start;
43331651c60SErnesto A. Fernández 		mutex_lock(&fd.tree->tree_lock);
4341da177e4SLinus Torvalds 	} while (total_blocks > blocks);
4351da177e4SLinus Torvalds 	hfs_find_exit(&fd);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	return res;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
hfsplus_file_extend(struct inode * inode,bool zeroout)4402cd282a1SSergei Antonov int hfsplus_file_extend(struct inode *inode, bool zeroout)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
443dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
4446af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
4451da177e4SLinus Torvalds 	u32 start, len, goal;
4461da177e4SLinus Torvalds 	int res;
4471da177e4SLinus Torvalds 
4481065348dSChristoph Hellwig 	if (sbi->alloc_file->i_size * 8 <
4491065348dSChristoph Hellwig 	    sbi->total_blocks - sbi->free_blocks + 8) {
45021f2296aSAnton Salikhmetov 		/* extend alloc file */
451*d9efb07dSColin Ian King 		pr_err_ratelimited("extend alloc file! (%llu,%u,%u)\n",
452dd73a01aSChristoph Hellwig 				   sbi->alloc_file->i_size * 8,
453dd73a01aSChristoph Hellwig 				   sbi->total_blocks, sbi->free_blocks);
4541da177e4SLinus Torvalds 		return -ENOSPC;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 
4576af502deSChristoph Hellwig 	mutex_lock(&hip->extents_lock);
4586af502deSChristoph Hellwig 	if (hip->alloc_blocks == hip->first_blocks)
4596af502deSChristoph Hellwig 		goal = hfsplus_ext_lastblock(hip->first_extents);
4601da177e4SLinus Torvalds 	else {
4616af502deSChristoph Hellwig 		res = hfsplus_ext_read_extent(inode, hip->alloc_blocks);
4621da177e4SLinus Torvalds 		if (res)
4631da177e4SLinus Torvalds 			goto out;
4646af502deSChristoph Hellwig 		goal = hfsplus_ext_lastblock(hip->cached_extents);
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
4676af502deSChristoph Hellwig 	len = hip->clump_blocks;
468dd73a01aSChristoph Hellwig 	start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len);
469dd73a01aSChristoph Hellwig 	if (start >= sbi->total_blocks) {
4701da177e4SLinus Torvalds 		start = hfsplus_block_allocate(sb, goal, 0, &len);
4711da177e4SLinus Torvalds 		if (start >= goal) {
4721da177e4SLinus Torvalds 			res = -ENOSPC;
4731da177e4SLinus Torvalds 			goto out;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4772cd282a1SSergei Antonov 	if (zeroout) {
4782cd282a1SSergei Antonov 		res = sb_issue_zeroout(sb, start, len, GFP_NOFS);
4792cd282a1SSergei Antonov 		if (res)
4802cd282a1SSergei Antonov 			goto out;
4812cd282a1SSergei Antonov 	}
4822cd282a1SSergei Antonov 
483c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
4846af502deSChristoph Hellwig 
4856af502deSChristoph Hellwig 	if (hip->alloc_blocks <= hip->first_blocks) {
4866af502deSChristoph Hellwig 		if (!hip->first_blocks) {
487c2b3e1f7SJoe Perches 			hfs_dbg(EXTENT, "first extents\n");
4881da177e4SLinus Torvalds 			/* no extents yet */
4896af502deSChristoph Hellwig 			hip->first_extents[0].start_block = cpu_to_be32(start);
4906af502deSChristoph Hellwig 			hip->first_extents[0].block_count = cpu_to_be32(len);
4911da177e4SLinus Torvalds 			res = 0;
4921da177e4SLinus Torvalds 		} else {
4931da177e4SLinus Torvalds 			/* try to append to extents in inode */
4946af502deSChristoph Hellwig 			res = hfsplus_add_extent(hip->first_extents,
4956af502deSChristoph Hellwig 						 hip->alloc_blocks,
4961da177e4SLinus Torvalds 						 start, len);
4971da177e4SLinus Torvalds 			if (res == -ENOSPC)
4981da177e4SLinus Torvalds 				goto insert_extent;
4991da177e4SLinus Torvalds 		}
5001da177e4SLinus Torvalds 		if (!res) {
5016af502deSChristoph Hellwig 			hfsplus_dump_extent(hip->first_extents);
5026af502deSChristoph Hellwig 			hip->first_blocks += len;
5031da177e4SLinus Torvalds 		}
5041da177e4SLinus Torvalds 	} else {
5056af502deSChristoph Hellwig 		res = hfsplus_add_extent(hip->cached_extents,
5066af502deSChristoph Hellwig 					 hip->alloc_blocks - hip->cached_start,
5071da177e4SLinus Torvalds 					 start, len);
5081da177e4SLinus Torvalds 		if (!res) {
5096af502deSChristoph Hellwig 			hfsplus_dump_extent(hip->cached_extents);
510b33b7921SChristoph Hellwig 			hip->extent_state |= HFSPLUS_EXT_DIRTY;
5116af502deSChristoph Hellwig 			hip->cached_blocks += len;
5121da177e4SLinus Torvalds 		} else if (res == -ENOSPC)
5131da177e4SLinus Torvalds 			goto insert_extent;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds out:
5161da177e4SLinus Torvalds 	if (!res) {
5176af502deSChristoph Hellwig 		hip->alloc_blocks += len;
518d7bdb996SSougata Santra 		mutex_unlock(&hip->extents_lock);
519e3494705SChristoph Hellwig 		hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
520d7bdb996SSougata Santra 		return 0;
5211da177e4SLinus Torvalds 	}
522d7bdb996SSougata Santra 	mutex_unlock(&hip->extents_lock);
5231da177e4SLinus Torvalds 	return res;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds insert_extent:
526c2b3e1f7SJoe Perches 	hfs_dbg(EXTENT, "insert new extent\n");
527dd7f3d54SAlexey Khoroshilov 	res = hfsplus_ext_write_extent_locked(inode);
528dd7f3d54SAlexey Khoroshilov 	if (res)
529dd7f3d54SAlexey Khoroshilov 		goto out;
5301da177e4SLinus Torvalds 
5316af502deSChristoph Hellwig 	memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
5326af502deSChristoph Hellwig 	hip->cached_extents[0].start_block = cpu_to_be32(start);
5336af502deSChristoph Hellwig 	hip->cached_extents[0].block_count = cpu_to_be32(len);
5346af502deSChristoph Hellwig 	hfsplus_dump_extent(hip->cached_extents);
535b33b7921SChristoph Hellwig 	hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW;
5366af502deSChristoph Hellwig 	hip->cached_start = hip->alloc_blocks;
5376af502deSChristoph Hellwig 	hip->cached_blocks = len;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	res = 0;
5401da177e4SLinus Torvalds 	goto out;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
hfsplus_file_truncate(struct inode * inode)5431da177e4SLinus Torvalds void hfsplus_file_truncate(struct inode *inode)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
5466af502deSChristoph Hellwig 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
5471da177e4SLinus Torvalds 	struct hfs_find_data fd;
5481da177e4SLinus Torvalds 	u32 alloc_cnt, blk_cnt, start;
5491da177e4SLinus Torvalds 	int res;
5501da177e4SLinus Torvalds 
551c2b3e1f7SJoe Perches 	hfs_dbg(INODE, "truncate: %lu, %llu -> %llu\n",
552c2b3e1f7SJoe Perches 		inode->i_ino, (long long)hip->phys_size, inode->i_size);
5536af502deSChristoph Hellwig 
5546af502deSChristoph Hellwig 	if (inode->i_size > hip->phys_size) {
5551da177e4SLinus Torvalds 		struct address_space *mapping = inode->i_mapping;
5561da177e4SLinus Torvalds 		struct page *page;
55774b7046dSAlexander Potapenko 		void *fsdata = NULL;
55812f267a2SVyacheslav Dubeyko 		loff_t size = inode->i_size;
5591da177e4SLinus Torvalds 
56007a31f72SMatthew Wilcox (Oracle) 		res = hfsplus_write_begin(NULL, mapping, size, 0,
5617c0efc62SNick Piggin 					  &page, &fsdata);
5621da177e4SLinus Torvalds 		if (res)
5637c0efc62SNick Piggin 			return;
56407a31f72SMatthew Wilcox (Oracle) 		res = generic_write_end(NULL, mapping, size, 0, 0,
56507a31f72SMatthew Wilcox (Oracle) 					page, fsdata);
5667c0efc62SNick Piggin 		if (res < 0)
5677c0efc62SNick Piggin 			return;
5681da177e4SLinus Torvalds 		mark_inode_dirty(inode);
5691da177e4SLinus Torvalds 		return;
5706af502deSChristoph Hellwig 	} else if (inode->i_size == hip->phys_size)
571f76d28d2SRoman Zippel 		return;
572f76d28d2SRoman Zippel 
573dd73a01aSChristoph Hellwig 	blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >>
574dd73a01aSChristoph Hellwig 			HFSPLUS_SB(sb)->alloc_blksz_shift;
5751da177e4SLinus Torvalds 
5766af502deSChristoph Hellwig 	mutex_lock(&hip->extents_lock);
577d7bdb996SSougata Santra 
578d7bdb996SSougata Santra 	alloc_cnt = hip->alloc_blocks;
579d7bdb996SSougata Santra 	if (blk_cnt == alloc_cnt)
580d7bdb996SSougata Santra 		goto out_unlock;
581d7bdb996SSougata Santra 
5825bd9d99dSAlexey Khoroshilov 	res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
5835bd9d99dSAlexey Khoroshilov 	if (res) {
5845bd9d99dSAlexey Khoroshilov 		mutex_unlock(&hip->extents_lock);
5855bd9d99dSAlexey Khoroshilov 		/* XXX: We lack error handling of hfsplus_file_truncate() */
5865bd9d99dSAlexey Khoroshilov 		return;
5875bd9d99dSAlexey Khoroshilov 	}
5881da177e4SLinus Torvalds 	while (1) {
5896af502deSChristoph Hellwig 		if (alloc_cnt == hip->first_blocks) {
59031651c60SErnesto A. Fernández 			mutex_unlock(&fd.tree->tree_lock);
5916af502deSChristoph Hellwig 			hfsplus_free_extents(sb, hip->first_extents,
5921da177e4SLinus Torvalds 					     alloc_cnt, alloc_cnt - blk_cnt);
5936af502deSChristoph Hellwig 			hfsplus_dump_extent(hip->first_extents);
5946af502deSChristoph Hellwig 			hip->first_blocks = blk_cnt;
59531651c60SErnesto A. Fernández 			mutex_lock(&fd.tree->tree_lock);
5961da177e4SLinus Torvalds 			break;
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 		res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
5991da177e4SLinus Torvalds 		if (res)
6001da177e4SLinus Torvalds 			break;
60131651c60SErnesto A. Fernández 
6026af502deSChristoph Hellwig 		start = hip->cached_start;
603c3187cf3SJouni Roivas 		if (blk_cnt <= start)
604c3187cf3SJouni Roivas 			hfs_brec_remove(&fd);
605c3187cf3SJouni Roivas 		mutex_unlock(&fd.tree->tree_lock);
6066af502deSChristoph Hellwig 		hfsplus_free_extents(sb, hip->cached_extents,
6071da177e4SLinus Torvalds 				     alloc_cnt - start, alloc_cnt - blk_cnt);
6086af502deSChristoph Hellwig 		hfsplus_dump_extent(hip->cached_extents);
609c3187cf3SJouni Roivas 		mutex_lock(&fd.tree->tree_lock);
6101da177e4SLinus Torvalds 		if (blk_cnt > start) {
611b33b7921SChristoph Hellwig 			hip->extent_state |= HFSPLUS_EXT_DIRTY;
6121da177e4SLinus Torvalds 			break;
6131da177e4SLinus Torvalds 		}
6141da177e4SLinus Torvalds 		alloc_cnt = start;
6156af502deSChristoph Hellwig 		hip->cached_start = hip->cached_blocks = 0;
616b33b7921SChristoph Hellwig 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 	hfs_find_exit(&fd);
6191da177e4SLinus Torvalds 
6206af502deSChristoph Hellwig 	hip->alloc_blocks = blk_cnt;
621d7bdb996SSougata Santra out_unlock:
622d7bdb996SSougata Santra 	mutex_unlock(&hip->extents_lock);
6236af502deSChristoph Hellwig 	hip->phys_size = inode->i_size;
6242753cc28SAnton Salikhmetov 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
6252753cc28SAnton Salikhmetov 		sb->s_blocksize_bits;
6266af502deSChristoph Hellwig 	inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
627e3494705SChristoph Hellwig 	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
6281da177e4SLinus Torvalds }
629