VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
b24413180f560 (Greg Kroah-Hartman   2017-11-01 15:07:57 +0100   1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   2) /*
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   3)  *  linux/fs/hfsplus/extents.c
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   4)  *
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   5)  * Copyright (C) 2001
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   6)  * Brad Boyer (flar@allandria.com)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   7)  * (C) 2003 Ardis Technologies <roman@ardistech.com>
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   8)  *
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700   9)  * Handling of Extents both in catalog and extents overflow trees
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  10)  */
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  11) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  12) #include <linux/errno.h>
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  13) #include <linux/fs.h>
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  14) #include <linux/pagemap.h>
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  15) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  16) #include "hfsplus_fs.h"
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  17) #include "hfsplus_raw.h"
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  18) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  19) /* Compare two extents keys, returns 0 on same, pos/neg for difference */
2179d372d9f8b (David Elliott        2006-01-18 17:43:08 -0800  20) int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
2179d372d9f8b (David Elliott        2006-01-18 17:43:08 -0800  21) 			const hfsplus_btree_key *k2)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  22) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  23) 	__be32 k1id, k2id;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  24) 	__be32 k1s, k2s;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  25) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  26) 	k1id = k1->ext.cnid;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  27) 	k2id = k2->ext.cnid;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  28) 	if (k1id != k2id)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  29) 		return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  30) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  31) 	if (k1->ext.fork_type != k2->ext.fork_type)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  32) 		return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  33) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  34) 	k1s = k1->ext.start_block;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  35) 	k2s = k2->ext.start_block;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  36) 	if (k1s == k2s)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  37) 		return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  38) 	return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  39) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  40) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  41) static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  42) 				  u32 block, u8 type)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  43) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  44) 	key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  45) 	key->ext.cnid = cpu_to_be32(cnid);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  46) 	key->ext.start_block = cpu_to_be32(block);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  47) 	key->ext.fork_type = type;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  48) 	key->ext.pad = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  49) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  50) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  51) static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  52) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  53) 	int i;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  54) 	u32 count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  55) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  56) 	for (i = 0; i < 8; ext++, i++) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  57) 		count = be32_to_cpu(ext->block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  58) 		if (off < count)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  59) 			return be32_to_cpu(ext->start_block) + off;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  60) 		off -= count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  61) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  62) 	/* panic? */
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  63) 	return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  64) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  65) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  66) static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  67) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  68) 	int i;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  69) 	u32 count = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  70) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  71) 	for (i = 0; i < 8; ext++, i++)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  72) 		count += be32_to_cpu(ext->block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  73) 	return count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  74) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  75) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  76) static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  77) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  78) 	int i;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  79) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  80) 	ext += 7;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  81) 	for (i = 0; i < 7; ext--, i++)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  82) 		if (ext->block_count)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  83) 			break;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  84) 	return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  85) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  86) 
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700  87) static int __hfsplus_ext_write_extent(struct inode *inode,
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200  88) 		struct hfs_find_data *fd)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  89) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200  90) 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  91) 	int res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700  92) 
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200  93) 	WARN_ON(!mutex_is_locked(&hip->extents_lock));
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200  94) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200  95) 	hfsplus_ext_build_key(fd->search_key, inode->i_ino, hip->cached_start,
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200  96) 			      HFSPLUS_IS_RSRC(inode) ?
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200  97) 				HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200  98) 
324ef39a8a4f6 (Vyacheslav Dubeyko   2013-02-27 17:03:04 -0800  99) 	res = hfs_brec_find(fd, hfs_find_rec_by_key);
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 100) 	if (hip->extent_state & HFSPLUS_EXT_NEW) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 101) 		if (res != -ENOENT)
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 102) 			return res;
d92915c35bfaf (Ernesto A. Fernández 2018-10-30 15:06:14 -0700 103) 		/* Fail early and avoid ENOSPC during the btree operation */
d92915c35bfaf (Ernesto A. Fernández 2018-10-30 15:06:14 -0700 104) 		res = hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
d92915c35bfaf (Ernesto A. Fernández 2018-10-30 15:06:14 -0700 105) 		if (res)
d92915c35bfaf (Ernesto A. Fernández 2018-10-30 15:06:14 -0700 106) 			return res;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 107) 		hfs_brec_insert(fd, hip->cached_extents,
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 108) 				sizeof(hfsplus_extent_rec));
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 109) 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 110) 	} else {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 111) 		if (res)
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 112) 			return res;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 113) 		hfs_bnode_write(fd->bnode, hip->cached_extents,
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 114) 				fd->entryoffset, fd->entrylength);
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 115) 		hip->extent_state &= ~HFSPLUS_EXT_DIRTY;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 116) 	}
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 117) 
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 118) 	/*
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 119) 	 * We can't just use hfsplus_mark_inode_dirty here, because we
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 120) 	 * also get called from hfsplus_write_inode, which should not
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 121) 	 * redirty the inode.  Instead the callers have to be careful
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 122) 	 * to explicily mark the inode dirty, too.
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 123) 	 */
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 124) 	set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags);
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 125) 
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 126) 	return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 127) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 128) 
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 129) static int hfsplus_ext_write_extent_locked(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 130) {
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 131) 	int res = 0;
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 132) 
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 133) 	if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 134) 		struct hfs_find_data fd;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 135) 
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 136) 		res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 137) 		if (res)
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 138) 			return res;
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 139) 		res = __hfsplus_ext_write_extent(inode, &fd);
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 140) 		hfs_find_exit(&fd);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 141) 	}
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 142) 	return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 143) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 144) 
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 145) int hfsplus_ext_write_extent(struct inode *inode)
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 146) {
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 147) 	int res;
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 148) 
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 149) 	mutex_lock(&HFSPLUS_I(inode)->extents_lock);
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 150) 	res = hfsplus_ext_write_extent_locked(inode);
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 151) 	mutex_unlock(&HFSPLUS_I(inode)->extents_lock);
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 152) 
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 153) 	return res;
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 154) }
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 155) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 156) static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 157) 					    struct hfsplus_extent *extent,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 158) 					    u32 cnid, u32 block, u8 type)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 159) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 160) 	int res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 161) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 162) 	hfsplus_ext_build_key(fd->search_key, cnid, block, type);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 163) 	fd->key->ext.cnid = 0;
324ef39a8a4f6 (Vyacheslav Dubeyko   2013-02-27 17:03:04 -0800 164) 	res = hfs_brec_find(fd, hfs_find_rec_by_key);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 165) 	if (res && res != -ENOENT)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 166) 		return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 167) 	if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 168) 	    fd->key->ext.fork_type != fd->search_key->ext.fork_type)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 169) 		return -ENOENT;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 170) 	if (fd->entrylength != sizeof(hfsplus_extent_rec))
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 171) 		return -EIO;
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 172) 	hfs_bnode_read(fd->bnode, extent, fd->entryoffset,
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 173) 		sizeof(hfsplus_extent_rec));
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 174) 	return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 175) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 176) 
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 177) static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd,
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 178) 		struct inode *inode, u32 block)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 179) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 180) 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 181) 	int res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 182) 
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 183) 	WARN_ON(!mutex_is_locked(&hip->extents_lock));
7fcc99f4f2ddb (Christoph Hellwig    2010-10-01 05:46:31 +0200 184) 
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 185) 	if (hip->extent_state & HFSPLUS_EXT_DIRTY) {
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 186) 		res = __hfsplus_ext_write_extent(inode, fd);
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 187) 		if (res)
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 188) 			return res;
d7a475d0c4a22 (Alexey Khoroshilov   2013-04-30 15:27:56 -0700 189) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 190) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 191) 	res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino,
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 192) 					block, HFSPLUS_IS_RSRC(inode) ?
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 193) 						HFSPLUS_TYPE_RSRC :
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 194) 						HFSPLUS_TYPE_DATA);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 195) 	if (!res) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 196) 		hip->cached_start = be32_to_cpu(fd->key->ext.start_block);
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 197) 		hip->cached_blocks =
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 198) 			hfsplus_ext_block_count(hip->cached_extents);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 199) 	} else {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 200) 		hip->cached_start = hip->cached_blocks = 0;
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 201) 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 202) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 203) 	return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 204) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 205) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 206) static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 207) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 208) 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 209) 	struct hfs_find_data fd;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 210) 	int res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 211) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 212) 	if (block >= hip->cached_start &&
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 213) 	    block < hip->cached_start + hip->cached_blocks)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 214) 		return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 215) 
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 216) 	res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 217) 	if (!res) {
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 218) 		res = __hfsplus_ext_cache_extent(&fd, inode, block);
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 219) 		hfs_find_exit(&fd);
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 220) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 221) 	return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 222) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 223) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 224) /* Get a block at iblock for inode, possibly allocating if create */
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 225) int hfsplus_get_block(struct inode *inode, sector_t iblock,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 226) 		      struct buffer_head *bh_result, int create)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 227) {
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 228) 	struct super_block *sb = inode->i_sb;
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 229) 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 230) 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 231) 	int res = -EIO;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 232) 	u32 ablock, dblock, mask;
bf1a1b31fa3ea (Christoph Hellwig    2011-02-16 09:34:17 +0100 233) 	sector_t sector;
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 234) 	int was_dirty = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 235) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 236) 	/* Convert inode block to disk allocation block */
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 237) 	ablock = iblock >> sbi->fs_shift;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 238) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 239) 	if (iblock >= hip->fs_blocks) {
839c3a6a5e1fb (Ernesto A. Fernández 2018-10-30 15:06:21 -0700 240) 		if (!create)
839c3a6a5e1fb (Ernesto A. Fernández 2018-10-30 15:06:21 -0700 241) 			return 0;
839c3a6a5e1fb (Ernesto A. Fernández 2018-10-30 15:06:21 -0700 242) 		if (iblock > hip->fs_blocks)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 243) 			return -EIO;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 244) 		if (ablock >= hip->alloc_blocks) {
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 245) 			res = hfsplus_file_extend(inode, false);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 246) 			if (res)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 247) 				return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 248) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 249) 	} else
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 250) 		create = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 251) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 252) 	if (ablock < hip->first_blocks) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 253) 		dblock = hfsplus_ext_find_block(hip->first_extents, ablock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 254) 		goto done;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 255) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 256) 
248736c2a5720 (Eric Sesterhenn      2008-10-18 20:28:02 -0700 257) 	if (inode->i_ino == HFSPLUS_EXT_CNID)
248736c2a5720 (Eric Sesterhenn      2008-10-18 20:28:02 -0700 258) 		return -EIO;
248736c2a5720 (Eric Sesterhenn      2008-10-18 20:28:02 -0700 259) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 260) 	mutex_lock(&hip->extents_lock);
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 261) 
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 262) 	/*
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 263) 	 * hfsplus_ext_read_extent will write out a cached extent into
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 264) 	 * the extents btree.  In that case we may have to mark the inode
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 265) 	 * dirty even for a pure read of an extent here.
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 266) 	 */
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 267) 	was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 268) 	res = hfsplus_ext_read_extent(inode, ablock);
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 269) 	if (res) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 270) 		mutex_unlock(&hip->extents_lock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 271) 		return -EIO;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 272) 	}
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 273) 	dblock = hfsplus_ext_find_block(hip->cached_extents,
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 274) 					ablock - hip->cached_start);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 275) 	mutex_unlock(&hip->extents_lock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 276) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 277) done:
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 278) 	hfs_dbg(EXTENT, "get_block(%lu): %llu - %u\n",
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 279) 		inode->i_ino, (long long)iblock, dblock);
bf1a1b31fa3ea (Christoph Hellwig    2011-02-16 09:34:17 +0100 280) 
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 281) 	mask = (1 << sbi->fs_shift) - 1;
bf1a1b31fa3ea (Christoph Hellwig    2011-02-16 09:34:17 +0100 282) 	sector = ((sector_t)dblock << sbi->fs_shift) +
bf1a1b31fa3ea (Christoph Hellwig    2011-02-16 09:34:17 +0100 283) 		  sbi->blockoffset + (iblock & mask);
bf1a1b31fa3ea (Christoph Hellwig    2011-02-16 09:34:17 +0100 284) 	map_bh(bh_result, sb, sector);
bf1a1b31fa3ea (Christoph Hellwig    2011-02-16 09:34:17 +0100 285) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 286) 	if (create) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 287) 		set_buffer_new(bh_result);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 288) 		hip->phys_size += sb->s_blocksize;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 289) 		hip->fs_blocks++;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 290) 		inode_add_bytes(inode, sb->s_blocksize);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 291) 	}
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 292) 	if (create || was_dirty)
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 293) 		mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 294) 	return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 295) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 296) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 297) static void hfsplus_dump_extent(struct hfsplus_extent *extent)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 298) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 299) 	int i;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 300) 
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 301) 	hfs_dbg(EXTENT, "   ");
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 302) 	for (i = 0; i < 8; i++)
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 303) 		hfs_dbg_cont(EXTENT, " %u:%u",
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 304) 			     be32_to_cpu(extent[i].start_block),
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 305) 			     be32_to_cpu(extent[i].block_count));
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 306) 	hfs_dbg_cont(EXTENT, "\n");
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 307) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 308) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 309) static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 310) 			      u32 alloc_block, u32 block_count)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 311) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 312) 	u32 count, start;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 313) 	int i;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 314) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 315) 	hfsplus_dump_extent(extent);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 316) 	for (i = 0; i < 8; extent++, i++) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 317) 		count = be32_to_cpu(extent->block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 318) 		if (offset == count) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 319) 			start = be32_to_cpu(extent->start_block);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 320) 			if (alloc_block != start + count) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 321) 				if (++i >= 8)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 322) 					return -ENOSPC;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 323) 				extent++;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 324) 				extent->start_block = cpu_to_be32(alloc_block);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 325) 			} else
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 326) 				block_count += count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 327) 			extent->block_count = cpu_to_be32(block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 328) 			return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 329) 		} else if (offset < count)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 330) 			break;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 331) 		offset -= count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 332) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 333) 	/* panic? */
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 334) 	return -EIO;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 335) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 336) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 337) static int hfsplus_free_extents(struct super_block *sb,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 338) 				struct hfsplus_extent *extent,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 339) 				u32 offset, u32 block_nr)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 340) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 341) 	u32 count, start;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 342) 	int i;
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 343) 	int err = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 344) 
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 345) 	/* Mapping the allocation file may lock the extent tree */
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 346) 	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 347) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 348) 	hfsplus_dump_extent(extent);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 349) 	for (i = 0; i < 8; extent++, i++) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 350) 		count = be32_to_cpu(extent->block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 351) 		if (offset == count)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 352) 			goto found;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 353) 		else if (offset < count)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 354) 			break;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 355) 		offset -= count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 356) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 357) 	/* panic? */
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 358) 	return -EIO;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 359) found:
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 360) 	for (;;) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 361) 		start = be32_to_cpu(extent->start_block);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 362) 		if (count <= block_nr) {
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 363) 			err = hfsplus_block_free(sb, start, count);
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 364) 			if (err) {
d614267329f2b (Joe Perches          2013-04-30 15:27:55 -0700 365) 				pr_err("can't free extent\n");
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 366) 				hfs_dbg(EXTENT, " start: %u count: %u\n",
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 367) 					start, count);
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 368) 			}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 369) 			extent->block_count = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 370) 			extent->start_block = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 371) 			block_nr -= count;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 372) 		} else {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 373) 			count -= block_nr;
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 374) 			err = hfsplus_block_free(sb, start + count, block_nr);
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 375) 			if (err) {
d614267329f2b (Joe Perches          2013-04-30 15:27:55 -0700 376) 				pr_err("can't free extent\n");
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 377) 				hfs_dbg(EXTENT, " start: %u count: %u\n",
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 378) 					start, count);
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 379) 			}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 380) 			extent->block_count = cpu_to_be32(count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 381) 			block_nr = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 382) 		}
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 383) 		if (!block_nr || !i) {
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 384) 			/*
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 385) 			 * Try to free all extents and
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 386) 			 * return only last error
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 387) 			 */
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 388) 			return err;
1b243fd39bd60 (Vyacheslav Dubeyko   2012-12-20 15:05:25 -0800 389) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 390) 		i--;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 391) 		extent--;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 392) 		count = be32_to_cpu(extent->block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 393) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 394) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 395) 
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 396) int hfsplus_free_fork(struct super_block *sb, u32 cnid,
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 397) 		struct hfsplus_fork_raw *fork, int type)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 398) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 399) 	struct hfs_find_data fd;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 400) 	hfsplus_extent_rec ext_entry;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 401) 	u32 total_blocks, blocks, start;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 402) 	int res, i;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 403) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 404) 	total_blocks = be32_to_cpu(fork->total_blocks);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 405) 	if (!total_blocks)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 406) 		return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 407) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 408) 	blocks = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 409) 	for (i = 0; i < 8; i++)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 410) 		blocks += be32_to_cpu(fork->extents[i].block_count);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 411) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 412) 	res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 413) 	if (res)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 414) 		return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 415) 	if (total_blocks == blocks)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 416) 		return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 417) 
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 418) 	res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 419) 	if (res)
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 420) 		return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 421) 	do {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 422) 		res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 423) 						total_blocks, type);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 424) 		if (res)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 425) 			break;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 426) 		start = be32_to_cpu(fd.key->ext.start_block);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 427) 		hfs_brec_remove(&fd);
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 428) 
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 429) 		mutex_unlock(&fd.tree->tree_lock);
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 430) 		hfsplus_free_extents(sb, ext_entry, total_blocks - start,
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 431) 				     total_blocks);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 432) 		total_blocks = start;
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 433) 		mutex_lock(&fd.tree->tree_lock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 434) 	} while (total_blocks > blocks);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 435) 	hfs_find_exit(&fd);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 436) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 437) 	return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 438) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 439) 
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 440) int hfsplus_file_extend(struct inode *inode, bool zeroout)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 441) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 442) 	struct super_block *sb = inode->i_sb;
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 443) 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 444) 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 445) 	u32 start, len, goal;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 446) 	int res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 447) 
1065348d472f9 (Christoph Hellwig    2011-02-02 09:40:33 -0700 448) 	if (sbi->alloc_file->i_size * 8 <
1065348d472f9 (Christoph Hellwig    2011-02-02 09:40:33 -0700 449) 	    sbi->total_blocks - sbi->free_blocks + 8) {
21f2296a598c4 (Anton Salikhmetov    2010-12-16 18:08:39 +0200 450) 		/* extend alloc file */
b73f3d0e70b1f (Fabian Frederick     2014-06-06 14:36:31 -0700 451) 		pr_err("extend alloc file! (%llu,%u,%u)\n",
b73f3d0e70b1f (Fabian Frederick     2014-06-06 14:36:31 -0700 452) 		       sbi->alloc_file->i_size * 8,
b73f3d0e70b1f (Fabian Frederick     2014-06-06 14:36:31 -0700 453) 		       sbi->total_blocks, sbi->free_blocks);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 454) 		return -ENOSPC;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 455) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 456) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 457) 	mutex_lock(&hip->extents_lock);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 458) 	if (hip->alloc_blocks == hip->first_blocks)
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 459) 		goal = hfsplus_ext_lastblock(hip->first_extents);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 460) 	else {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 461) 		res = hfsplus_ext_read_extent(inode, hip->alloc_blocks);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 462) 		if (res)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 463) 			goto out;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 464) 		goal = hfsplus_ext_lastblock(hip->cached_extents);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 465) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 466) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 467) 	len = hip->clump_blocks;
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 468) 	start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len);
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 469) 	if (start >= sbi->total_blocks) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 470) 		start = hfsplus_block_allocate(sb, goal, 0, &len);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 471) 		if (start >= goal) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 472) 			res = -ENOSPC;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 473) 			goto out;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 474) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 475) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 476) 
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 477) 	if (zeroout) {
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 478) 		res = sb_issue_zeroout(sb, start, len, GFP_NOFS);
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 479) 		if (res)
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 480) 			goto out;
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 481) 	}
2cd282a1bc6b9 (Sergei Antonov       2014-06-06 14:36:28 -0700 482) 
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 483) 	hfs_dbg(EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 484) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 485) 	if (hip->alloc_blocks <= hip->first_blocks) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 486) 		if (!hip->first_blocks) {
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 487) 			hfs_dbg(EXTENT, "first extents\n");
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 488) 			/* no extents yet */
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 489) 			hip->first_extents[0].start_block = cpu_to_be32(start);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 490) 			hip->first_extents[0].block_count = cpu_to_be32(len);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 491) 			res = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 492) 		} else {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 493) 			/* try to append to extents in inode */
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 494) 			res = hfsplus_add_extent(hip->first_extents,
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 495) 						 hip->alloc_blocks,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 496) 						 start, len);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 497) 			if (res == -ENOSPC)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 498) 				goto insert_extent;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 499) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 500) 		if (!res) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 501) 			hfsplus_dump_extent(hip->first_extents);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 502) 			hip->first_blocks += len;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 503) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 504) 	} else {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 505) 		res = hfsplus_add_extent(hip->cached_extents,
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 506) 					 hip->alloc_blocks - hip->cached_start,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 507) 					 start, len);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 508) 		if (!res) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 509) 			hfsplus_dump_extent(hip->cached_extents);
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 510) 			hip->extent_state |= HFSPLUS_EXT_DIRTY;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 511) 			hip->cached_blocks += len;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 512) 		} else if (res == -ENOSPC)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 513) 			goto insert_extent;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 514) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 515) out:
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 516) 	if (!res) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 517) 		hip->alloc_blocks += len;
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 518) 		mutex_unlock(&hip->extents_lock);
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 519) 		hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 520) 		return 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 521) 	}
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 522) 	mutex_unlock(&hip->extents_lock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 523) 	return res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 524) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 525) insert_extent:
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 526) 	hfs_dbg(EXTENT, "insert new extent\n");
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 527) 	res = hfsplus_ext_write_extent_locked(inode);
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 528) 	if (res)
dd7f3d5458e5c (Alexey Khoroshilov   2011-07-06 02:30:00 +0400 529) 		goto out;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 530) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 531) 	memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 532) 	hip->cached_extents[0].start_block = cpu_to_be32(start);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 533) 	hip->cached_extents[0].block_count = cpu_to_be32(len);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 534) 	hfsplus_dump_extent(hip->cached_extents);
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 535) 	hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 536) 	hip->cached_start = hip->alloc_blocks;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 537) 	hip->cached_blocks = len;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 538) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 539) 	res = 0;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 540) 	goto out;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 541) }
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 542) 
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 543) void hfsplus_file_truncate(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 544) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 545) 	struct super_block *sb = inode->i_sb;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 546) 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 547) 	struct hfs_find_data fd;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 548) 	u32 alloc_cnt, blk_cnt, start;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 549) 	int res;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 550) 
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 551) 	hfs_dbg(INODE, "truncate: %lu, %llu -> %llu\n",
c2b3e1f76e5c9 (Joe Perches          2013-04-30 15:27:54 -0700 552) 		inode->i_ino, (long long)hip->phys_size, inode->i_size);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 553) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 554) 	if (inode->i_size > hip->phys_size) {
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 555) 		struct address_space *mapping = inode->i_mapping;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 556) 		struct page *page;
7c0efc627738c (Nicholas Piggin      2007-10-16 01:25:09 -0700 557) 		void *fsdata;
12f267a20aecf (Vyacheslav Dubeyko   2013-04-17 15:58:33 -0700 558) 		loff_t size = inode->i_size;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 559) 
c718a97514e4d (Tetsuo Handa         2017-05-08 15:58:59 -0700 560) 		res = pagecache_write_begin(NULL, mapping, size, 0, 0,
c718a97514e4d (Tetsuo Handa         2017-05-08 15:58:59 -0700 561) 					    &page, &fsdata);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 562) 		if (res)
7c0efc627738c (Nicholas Piggin      2007-10-16 01:25:09 -0700 563) 			return;
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 564) 		res = pagecache_write_end(NULL, mapping, size,
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 565) 			0, 0, page, fsdata);
7c0efc627738c (Nicholas Piggin      2007-10-16 01:25:09 -0700 566) 		if (res < 0)
7c0efc627738c (Nicholas Piggin      2007-10-16 01:25:09 -0700 567) 			return;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 568) 		mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 569) 		return;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 570) 	} else if (inode->i_size == hip->phys_size)
f76d28d235cf7 (Roman Zippel         2005-08-01 21:11:40 -0700 571) 		return;
f76d28d235cf7 (Roman Zippel         2005-08-01 21:11:40 -0700 572) 
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 573) 	blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >>
dd73a01a30d72 (Christoph Hellwig    2010-10-01 05:42:59 +0200 574) 			HFSPLUS_SB(sb)->alloc_blksz_shift;
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 575) 
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 576) 	mutex_lock(&hip->extents_lock);
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 577) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 578) 	alloc_cnt = hip->alloc_blocks;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 579) 	if (blk_cnt == alloc_cnt)
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 580) 		goto out_unlock;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 581) 
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 582) 	res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 583) 	if (res) {
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 584) 		mutex_unlock(&hip->extents_lock);
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 585) 		/* XXX: We lack error handling of hfsplus_file_truncate() */
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 586) 		return;
5bd9d99d107c5 (Alexey Khoroshilov   2011-07-06 02:29:59 +0400 587) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 588) 	while (1) {
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 589) 		if (alloc_cnt == hip->first_blocks) {
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 590) 			mutex_unlock(&fd.tree->tree_lock);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 591) 			hfsplus_free_extents(sb, hip->first_extents,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 592) 					     alloc_cnt, alloc_cnt - blk_cnt);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 593) 			hfsplus_dump_extent(hip->first_extents);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 594) 			hip->first_blocks = blk_cnt;
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 595) 			mutex_lock(&fd.tree->tree_lock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 596) 			break;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 597) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 598) 		res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 599) 		if (res)
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 600) 			break;
31651c607151f (Ernesto A. Fernández 2018-08-21 21:59:16 -0700 601) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 602) 		start = hip->cached_start;
c3187cf322163 (Jouni Roivas         2021-05-14 17:27:33 -0700 603) 		if (blk_cnt <= start)
c3187cf322163 (Jouni Roivas         2021-05-14 17:27:33 -0700 604) 			hfs_brec_remove(&fd);
c3187cf322163 (Jouni Roivas         2021-05-14 17:27:33 -0700 605) 		mutex_unlock(&fd.tree->tree_lock);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 606) 		hfsplus_free_extents(sb, hip->cached_extents,
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 607) 				     alloc_cnt - start, alloc_cnt - blk_cnt);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 608) 		hfsplus_dump_extent(hip->cached_extents);
c3187cf322163 (Jouni Roivas         2021-05-14 17:27:33 -0700 609) 		mutex_lock(&fd.tree->tree_lock);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 610) 		if (blk_cnt > start) {
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 611) 			hip->extent_state |= HFSPLUS_EXT_DIRTY;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 612) 			break;
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 613) 		}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 614) 		alloc_cnt = start;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 615) 		hip->cached_start = hip->cached_blocks = 0;
b33b7921db14a (Christoph Hellwig    2010-11-23 14:38:13 +0100 616) 		hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 617) 	}
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 618) 	hfs_find_exit(&fd);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 619) 
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 620) 	hip->alloc_blocks = blk_cnt;
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 621) out_unlock:
d7bdb996aef67 (Sougata Santra       2014-04-03 14:50:34 -0700 622) 	mutex_unlock(&hip->extents_lock);
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 623) 	hip->phys_size = inode->i_size;
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 624) 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
2753cc281c9a0 (Anton Salikhmetov    2010-12-16 18:08:38 +0200 625) 		sb->s_blocksize_bits;
6af502de224c3 (Christoph Hellwig    2010-10-01 05:43:31 +0200 626) 	inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
e34947056076c (Christoph Hellwig    2010-11-23 14:38:15 +0100 627) 	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
^1da177e4c3f4 (Linus Torvalds       2005-04-16 15:20:36 -0700 628) }