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