xref: /linux/fs/gfs2/bmap.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5  */
6 
7 #include <linux/spinlock.h>
8 #include <linux/completion.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/gfs2_ondisk.h>
12 #include <linux/crc32.h>
13 #include <linux/iomap.h>
14 #include <linux/ktime.h>
15 
16 #include "gfs2.h"
17 #include "incore.h"
18 #include "bmap.h"
19 #include "glock.h"
20 #include "inode.h"
21 #include "meta_io.h"
22 #include "quota.h"
23 #include "rgrp.h"
24 #include "log.h"
25 #include "super.h"
26 #include "trans.h"
27 #include "dir.h"
28 #include "util.h"
29 #include "aops.h"
30 #include "trace_gfs2.h"
31 
32 /* This doesn't need to be that large as max 64 bit pointers in a 4k
33  * block is 512, so __u16 is fine for that. It saves stack space to
34  * keep it small.
35  */
36 struct metapath {
37 	struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
38 	__u16 mp_list[GFS2_MAX_META_HEIGHT];
39 	int mp_fheight; /* find_metapath height */
40 	int mp_aheight; /* actual height (lookup height) */
41 };
42 
43 static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length);
44 
45 /**
46  * gfs2_unstuffer_folio - unstuff a stuffed inode into a block cached by a folio
47  * @ip: the inode
48  * @dibh: the dinode buffer
49  * @block: the block number that was allocated
50  * @folio: The folio.
51  *
52  * Returns: errno
53  */
54 static int gfs2_unstuffer_folio(struct gfs2_inode *ip, struct buffer_head *dibh,
55 			       u64 block, struct folio *folio)
56 {
57 	struct inode *inode = &ip->i_inode;
58 
59 	if (!folio_test_uptodate(folio)) {
60 		void *kaddr = kmap_local_folio(folio, 0);
61 		u64 dsize = i_size_read(inode);
62 
63 		memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
64 		memset(kaddr + dsize, 0, folio_size(folio) - dsize);
65 		kunmap_local(kaddr);
66 
67 		folio_mark_uptodate(folio);
68 	}
69 
70 	if (gfs2_is_jdata(ip)) {
71 		struct buffer_head *bh = folio_buffers(folio);
72 
73 		if (!bh)
74 			bh = create_empty_buffers(folio,
75 				BIT(inode->i_blkbits), BIT(BH_Uptodate));
76 
77 		if (!buffer_mapped(bh))
78 			map_bh(bh, inode->i_sb, block);
79 
80 		set_buffer_uptodate(bh);
81 		gfs2_trans_add_data(ip->i_gl, bh);
82 	} else {
83 		folio_mark_dirty(folio);
84 		gfs2_ordered_add_inode(ip);
85 	}
86 
87 	return 0;
88 }
89 
90 static int __gfs2_unstuff_inode(struct gfs2_inode *ip, struct folio *folio)
91 {
92 	struct buffer_head *bh, *dibh;
93 	struct gfs2_dinode *di;
94 	u64 block = 0;
95 	int isdir = gfs2_is_dir(ip);
96 	int error;
97 
98 	error = gfs2_meta_inode_buffer(ip, &dibh);
99 	if (error)
100 		return error;
101 
102 	if (i_size_read(&ip->i_inode)) {
103 		/* Get a free block, fill it with the stuffed data,
104 		   and write it out to disk */
105 
106 		unsigned int n = 1;
107 		error = gfs2_alloc_blocks(ip, &block, &n, 0);
108 		if (error)
109 			goto out_brelse;
110 		if (isdir) {
111 			gfs2_trans_remove_revoke(GFS2_SB(&ip->i_inode), block, 1);
112 			error = gfs2_dir_get_new_buffer(ip, block, &bh);
113 			if (error)
114 				goto out_brelse;
115 			gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
116 					      dibh, sizeof(struct gfs2_dinode));
117 			brelse(bh);
118 		} else {
119 			error = gfs2_unstuffer_folio(ip, dibh, block, folio);
120 			if (error)
121 				goto out_brelse;
122 		}
123 	}
124 
125 	/*  Set up the pointer to the new block  */
126 
127 	gfs2_trans_add_meta(ip->i_gl, dibh);
128 	di = (struct gfs2_dinode *)dibh->b_data;
129 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
130 
131 	if (i_size_read(&ip->i_inode)) {
132 		*(__be64 *)(di + 1) = cpu_to_be64(block);
133 		gfs2_add_inode_blocks(&ip->i_inode, 1);
134 		di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
135 	}
136 
137 	ip->i_height = 1;
138 	di->di_height = cpu_to_be16(1);
139 
140 out_brelse:
141 	brelse(dibh);
142 	return error;
143 }
144 
145 /**
146  * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
147  * @ip: The GFS2 inode to unstuff
148  *
149  * This routine unstuffs a dinode and returns it to a "normal" state such
150  * that the height can be grown in the traditional way.
151  *
152  * Returns: errno
153  */
154 
155 int gfs2_unstuff_dinode(struct gfs2_inode *ip)
156 {
157 	struct inode *inode = &ip->i_inode;
158 	struct folio *folio;
159 	int error;
160 
161 	down_write(&ip->i_rw_mutex);
162 	folio = filemap_grab_folio(inode->i_mapping, 0);
163 	error = PTR_ERR(folio);
164 	if (IS_ERR(folio))
165 		goto out;
166 	error = __gfs2_unstuff_inode(ip, folio);
167 	folio_unlock(folio);
168 	folio_put(folio);
169 out:
170 	up_write(&ip->i_rw_mutex);
171 	return error;
172 }
173 
174 /**
175  * find_metapath - Find path through the metadata tree
176  * @sdp: The superblock
177  * @block: The disk block to look up
178  * @mp: The metapath to return the result in
179  * @height: The pre-calculated height of the metadata tree
180  *
181  *   This routine returns a struct metapath structure that defines a path
182  *   through the metadata of inode "ip" to get to block "block".
183  *
184  *   Example:
185  *   Given:  "ip" is a height 3 file, "offset" is 101342453, and this is a
186  *   filesystem with a blocksize of 4096.
187  *
188  *   find_metapath() would return a struct metapath structure set to:
189  *   mp_fheight = 3, mp_list[0] = 0, mp_list[1] = 48, and mp_list[2] = 165.
190  *
191  *   That means that in order to get to the block containing the byte at
192  *   offset 101342453, we would load the indirect block pointed to by pointer
193  *   0 in the dinode.  We would then load the indirect block pointed to by
194  *   pointer 48 in that indirect block.  We would then load the data block
195  *   pointed to by pointer 165 in that indirect block.
196  *
197  *             ----------------------------------------
198  *             | Dinode |                             |
199  *             |        |                            4|
200  *             |        |0 1 2 3 4 5                 9|
201  *             |        |                            6|
202  *             ----------------------------------------
203  *                       |
204  *                       |
205  *                       V
206  *             ----------------------------------------
207  *             | Indirect Block                       |
208  *             |                                     5|
209  *             |            4 4 4 4 4 5 5            1|
210  *             |0           5 6 7 8 9 0 1            2|
211  *             ----------------------------------------
212  *                                |
213  *                                |
214  *                                V
215  *             ----------------------------------------
216  *             | Indirect Block                       |
217  *             |                         1 1 1 1 1   5|
218  *             |                         6 6 6 6 6   1|
219  *             |0                        3 4 5 6 7   2|
220  *             ----------------------------------------
221  *                                           |
222  *                                           |
223  *                                           V
224  *             ----------------------------------------
225  *             | Data block containing offset         |
226  *             |            101342453                 |
227  *             |                                      |
228  *             |                                      |
229  *             ----------------------------------------
230  *
231  */
232 
233 static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
234 			  struct metapath *mp, unsigned int height)
235 {
236 	unsigned int i;
237 
238 	mp->mp_fheight = height;
239 	for (i = height; i--;)
240 		mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
241 }
242 
243 static inline unsigned int metapath_branch_start(const struct metapath *mp)
244 {
245 	if (mp->mp_list[0] == 0)
246 		return 2;
247 	return 1;
248 }
249 
250 /**
251  * metaptr1 - Return the first possible metadata pointer in a metapath buffer
252  * @height: The metadata height (0 = dinode)
253  * @mp: The metapath
254  */
255 static inline __be64 *metaptr1(unsigned int height, const struct metapath *mp)
256 {
257 	struct buffer_head *bh = mp->mp_bh[height];
258 	if (height == 0)
259 		return ((__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)));
260 	return ((__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)));
261 }
262 
263 /**
264  * metapointer - Return pointer to start of metadata in a buffer
265  * @height: The metadata height (0 = dinode)
266  * @mp: The metapath
267  *
268  * Return a pointer to the block number of the next height of the metadata
269  * tree given a buffer containing the pointer to the current height of the
270  * metadata tree.
271  */
272 
273 static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
274 {
275 	__be64 *p = metaptr1(height, mp);
276 	return p + mp->mp_list[height];
277 }
278 
279 static inline const __be64 *metaend(unsigned int height, const struct metapath *mp)
280 {
281 	const struct buffer_head *bh = mp->mp_bh[height];
282 	return (const __be64 *)(bh->b_data + bh->b_size);
283 }
284 
285 static void clone_metapath(struct metapath *clone, struct metapath *mp)
286 {
287 	unsigned int hgt;
288 
289 	*clone = *mp;
290 	for (hgt = 0; hgt < mp->mp_aheight; hgt++)
291 		get_bh(clone->mp_bh[hgt]);
292 }
293 
294 static void gfs2_metapath_ra(struct gfs2_glock *gl, __be64 *start, __be64 *end)
295 {
296 	const __be64 *t;
297 
298 	for (t = start; t < end; t++) {
299 		struct buffer_head *rabh;
300 
301 		if (!*t)
302 			continue;
303 
304 		rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
305 		if (trylock_buffer(rabh)) {
306 			if (!buffer_uptodate(rabh)) {
307 				bh_submit(rabh,
308 					REQ_OP_READ | REQ_RAHEAD | REQ_META |
309 					REQ_PRIO,
310 					bh_end_read);
311 			} else {
312 				unlock_buffer(rabh);
313 			}
314 		}
315 		put_bh(rabh);
316 	}
317 }
318 
319 static inline struct buffer_head *
320 metapath_dibh(struct metapath *mp)
321 {
322 	return mp->mp_bh[0];
323 }
324 
325 static int __fillup_metapath(struct gfs2_inode *ip, struct metapath *mp,
326 			     unsigned int x, unsigned int h)
327 {
328 	for (; x < h; x++) {
329 		__be64 *ptr = metapointer(x, mp);
330 		u64 dblock = be64_to_cpu(*ptr);
331 		int ret;
332 
333 		if (!dblock)
334 			break;
335 		ret = gfs2_meta_buffer(ip, GFS2_METATYPE_IN, dblock, &mp->mp_bh[x + 1]);
336 		if (ret)
337 			return ret;
338 	}
339 	mp->mp_aheight = x + 1;
340 	return 0;
341 }
342 
343 /**
344  * lookup_metapath - Walk the metadata tree to a specific point
345  * @ip: The inode
346  * @mp: The metapath
347  *
348  * Assumes that the inode's buffer has already been looked up and
349  * hooked onto mp->mp_bh[0] and that the metapath has been initialised
350  * by find_metapath().
351  *
352  * If this function encounters part of the tree which has not been
353  * allocated, it returns the current height of the tree at the point
354  * at which it found the unallocated block. Blocks which are found are
355  * added to the mp->mp_bh[] list.
356  *
357  * Returns: error
358  */
359 
360 static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
361 {
362 	return __fillup_metapath(ip, mp, 0, ip->i_height - 1);
363 }
364 
365 /**
366  * fillup_metapath - fill up buffers for the metadata path to a specific height
367  * @ip: The inode
368  * @mp: The metapath
369  * @h: The height to which it should be mapped
370  *
371  * Similar to lookup_metapath, but does lookups for a range of heights
372  *
373  * Returns: error or the number of buffers filled
374  */
375 
376 static int fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, int h)
377 {
378 	unsigned int x = 0;
379 	int ret;
380 
381 	if (h) {
382 		/* find the first buffer we need to look up. */
383 		for (x = h - 1; x > 0; x--) {
384 			if (mp->mp_bh[x])
385 				break;
386 		}
387 	}
388 	ret = __fillup_metapath(ip, mp, x, h);
389 	if (ret)
390 		return ret;
391 	return mp->mp_aheight - x - 1;
392 }
393 
394 static sector_t metapath_to_block(struct gfs2_sbd *sdp, struct metapath *mp)
395 {
396 	sector_t factor = 1, block = 0;
397 	int hgt;
398 
399 	for (hgt = mp->mp_fheight - 1; hgt >= 0; hgt--) {
400 		if (hgt < mp->mp_aheight)
401 			block += mp->mp_list[hgt] * factor;
402 		factor *= sdp->sd_inptrs;
403 	}
404 	return block;
405 }
406 
407 static void release_metapath(struct metapath *mp)
408 {
409 	int i;
410 
411 	for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
412 		if (mp->mp_bh[i] == NULL)
413 			break;
414 		brelse(mp->mp_bh[i]);
415 		mp->mp_bh[i] = NULL;
416 	}
417 }
418 
419 /**
420  * gfs2_extent_length - Returns length of an extent of blocks
421  * @bh: The metadata block
422  * @ptr: Current position in @bh
423  * @eob: Set to 1 if we hit "end of block"
424  *
425  * Returns: The length of the extent (minimum of one block)
426  */
427 
428 static inline unsigned int gfs2_extent_length(struct buffer_head *bh, __be64 *ptr, int *eob)
429 {
430 	const __be64 *end = (__be64 *)(bh->b_data + bh->b_size);
431 	const __be64 *first = ptr;
432 	u64 d = be64_to_cpu(*ptr);
433 
434 	*eob = 0;
435 	do {
436 		ptr++;
437 		if (ptr >= end)
438 			break;
439 		d++;
440 	} while(be64_to_cpu(*ptr) == d);
441 	if (ptr >= end)
442 		*eob = 1;
443 	return ptr - first;
444 }
445 
446 enum walker_status { WALK_STOP, WALK_FOLLOW, WALK_CONTINUE };
447 
448 /*
449  * gfs2_metadata_walker - walk an indirect block
450  * @mp: Metapath to indirect block
451  * @ptrs: Number of pointers to look at
452  *
453  * When returning WALK_FOLLOW, the walker must update @mp to point at the right
454  * indirect block to follow.
455  */
456 typedef enum walker_status (*gfs2_metadata_walker)(struct metapath *mp,
457 						   unsigned int ptrs);
458 
459 /*
460  * gfs2_walk_metadata - walk a tree of indirect blocks
461  * @inode: The inode
462  * @mp: Starting point of walk
463  * @max_len: Maximum number of blocks to walk
464  * @walker: Called during the walk
465  *
466  * Returns 1 if the walk was stopped by @walker, 0 if we went past @max_len or
467  * past the end of metadata, and a negative error code otherwise.
468  */
469 
470 static int gfs2_walk_metadata(struct inode *inode, struct metapath *mp,
471 		u64 max_len, gfs2_metadata_walker walker)
472 {
473 	struct gfs2_inode *ip = GFS2_I(inode);
474 	struct gfs2_sbd *sdp = GFS2_SB(inode);
475 	u64 factor = 1;
476 	unsigned int hgt;
477 	int ret;
478 
479 	/*
480 	 * The walk starts in the lowest allocated indirect block, which may be
481 	 * before the position indicated by @mp.  Adjust @max_len accordingly
482 	 * to avoid a short walk.
483 	 */
484 	for (hgt = mp->mp_fheight - 1; hgt >= mp->mp_aheight; hgt--) {
485 		max_len += mp->mp_list[hgt] * factor;
486 		mp->mp_list[hgt] = 0;
487 		factor *= sdp->sd_inptrs;
488 	}
489 
490 	for (;;) {
491 		u16 start = mp->mp_list[hgt];
492 		enum walker_status status;
493 		unsigned int ptrs;
494 		u64 len;
495 
496 		/* Walk indirect block. */
497 		ptrs = (hgt >= 1 ? sdp->sd_inptrs : sdp->sd_diptrs) - start;
498 		len = ptrs * factor;
499 		if (len > max_len)
500 			ptrs = DIV_ROUND_UP_ULL(max_len, factor);
501 		status = walker(mp, ptrs);
502 		switch (status) {
503 		case WALK_STOP:
504 			return 1;
505 		case WALK_FOLLOW:
506 			BUG_ON(mp->mp_aheight == mp->mp_fheight);
507 			ptrs = mp->mp_list[hgt] - start;
508 			len = ptrs * factor;
509 			break;
510 		case WALK_CONTINUE:
511 			break;
512 		}
513 		if (len >= max_len)
514 			break;
515 		max_len -= len;
516 		if (status == WALK_FOLLOW)
517 			goto fill_up_metapath;
518 
519 lower_metapath:
520 		/* Decrease height of metapath. */
521 		brelse(mp->mp_bh[hgt]);
522 		mp->mp_bh[hgt] = NULL;
523 		mp->mp_list[hgt] = 0;
524 		if (!hgt)
525 			break;
526 		hgt--;
527 		factor *= sdp->sd_inptrs;
528 
529 		/* Advance in metadata tree. */
530 		(mp->mp_list[hgt])++;
531 		if (hgt) {
532 			if (mp->mp_list[hgt] >= sdp->sd_inptrs)
533 				goto lower_metapath;
534 		} else {
535 			if (mp->mp_list[hgt] >= sdp->sd_diptrs)
536 				break;
537 		}
538 
539 fill_up_metapath:
540 		/* Increase height of metapath. */
541 		ret = fillup_metapath(ip, mp, ip->i_height - 1);
542 		if (ret < 0)
543 			return ret;
544 		hgt += ret;
545 		for (; ret; ret--)
546 			do_div(factor, sdp->sd_inptrs);
547 		mp->mp_aheight = hgt + 1;
548 	}
549 	return 0;
550 }
551 
552 static enum walker_status gfs2_hole_walker(struct metapath *mp,
553 					   unsigned int ptrs)
554 {
555 	const __be64 *start, *ptr, *end;
556 	unsigned int hgt;
557 
558 	hgt = mp->mp_aheight - 1;
559 	start = metapointer(hgt, mp);
560 	end = start + ptrs;
561 
562 	for (ptr = start; ptr < end; ptr++) {
563 		if (*ptr) {
564 			mp->mp_list[hgt] += ptr - start;
565 			if (mp->mp_aheight == mp->mp_fheight)
566 				return WALK_STOP;
567 			return WALK_FOLLOW;
568 		}
569 	}
570 	return WALK_CONTINUE;
571 }
572 
573 /**
574  * gfs2_hole_size - figure out the size of a hole
575  * @inode: The inode
576  * @lblock: The logical starting block number
577  * @len: How far to look (in blocks)
578  * @mp: The metapath at lblock
579  * @iomap: The iomap to store the hole size in
580  *
581  * This function modifies @mp.
582  *
583  * Returns: errno on error
584  */
585 static int gfs2_hole_size(struct inode *inode, sector_t lblock, u64 len,
586 			  struct metapath *mp, struct iomap *iomap)
587 {
588 	struct metapath clone;
589 	u64 hole_size;
590 	int ret;
591 
592 	clone_metapath(&clone, mp);
593 	ret = gfs2_walk_metadata(inode, &clone, len, gfs2_hole_walker);
594 	if (ret < 0)
595 		goto out;
596 
597 	if (ret == 1)
598 		hole_size = metapath_to_block(GFS2_SB(inode), &clone) - lblock;
599 	else
600 		hole_size = len;
601 	iomap->length = hole_size << inode->i_blkbits;
602 	ret = 0;
603 
604 out:
605 	release_metapath(&clone);
606 	return ret;
607 }
608 
609 static inline void gfs2_indirect_init(struct metapath *mp,
610 				      struct gfs2_glock *gl, unsigned int i,
611 				      unsigned offset, u64 bn)
612 {
613 	__be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
614 		       ((i > 1) ? sizeof(struct gfs2_meta_header) :
615 				 sizeof(struct gfs2_dinode)));
616 	BUG_ON(i < 1);
617 	BUG_ON(mp->mp_bh[i] != NULL);
618 	mp->mp_bh[i] = gfs2_meta_new(gl, bn);
619 	gfs2_trans_add_meta(gl, mp->mp_bh[i]);
620 	gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
621 	gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
622 	ptr += offset;
623 	*ptr = cpu_to_be64(bn);
624 }
625 
626 enum alloc_state {
627 	ALLOC_DATA = 0,
628 	ALLOC_GROW_DEPTH = 1,
629 	ALLOC_GROW_HEIGHT = 2,
630 	/* ALLOC_UNSTUFF = 3,   TBD and rather complicated */
631 };
632 
633 /**
634  * __gfs2_iomap_alloc - Build a metadata tree of the requested height
635  * @inode: The GFS2 inode
636  * @iomap: The iomap structure
637  * @mp: The metapath, with proper height information calculated
638  *
639  * In this routine we may have to alloc:
640  *   i) Indirect blocks to grow the metadata tree height
641  *  ii) Indirect blocks to fill in lower part of the metadata tree
642  * iii) Data blocks
643  *
644  * This function is called after __gfs2_iomap_get, which works out the
645  * total number of blocks which we need via gfs2_alloc_size.
646  *
647  * We then do the actual allocation asking for an extent at a time (if
648  * enough contiguous free blocks are available, there will only be one
649  * allocation request per call) and uses the state machine to initialise
650  * the blocks in order.
651  *
652  * Right now, this function will allocate at most one indirect block
653  * worth of data -- with a default block size of 4K, that's slightly
654  * less than 2M.  If this limitation is ever removed to allow huge
655  * allocations, we would probably still want to limit the iomap size we
656  * return to avoid stalling other tasks during huge writes; the next
657  * iomap iteration would then find the blocks already allocated.
658  *
659  * Returns: errno on error
660  */
661 
662 static int __gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
663 			      struct metapath *mp)
664 {
665 	struct gfs2_inode *ip = GFS2_I(inode);
666 	struct gfs2_sbd *sdp = GFS2_SB(inode);
667 	struct buffer_head *dibh = metapath_dibh(mp);
668 	u64 bn;
669 	unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
670 	size_t dblks = iomap->length >> inode->i_blkbits;
671 	const unsigned end_of_metadata = mp->mp_fheight - 1;
672 	int ret;
673 	enum alloc_state state;
674 	__be64 *ptr;
675 	__be64 zero_bn = 0;
676 
677 	BUG_ON(mp->mp_aheight < 1);
678 	BUG_ON(dibh == NULL);
679 	BUG_ON(dblks < 1);
680 
681 	gfs2_trans_add_meta(ip->i_gl, dibh);
682 
683 	down_write(&ip->i_rw_mutex);
684 
685 	if (mp->mp_fheight == mp->mp_aheight) {
686 		/* Bottom indirect block exists */
687 		state = ALLOC_DATA;
688 	} else {
689 		/* Need to allocate indirect blocks */
690 		if (mp->mp_fheight == ip->i_height) {
691 			/* Writing into existing tree, extend tree down */
692 			iblks = mp->mp_fheight - mp->mp_aheight;
693 			state = ALLOC_GROW_DEPTH;
694 		} else {
695 			/* Building up tree height */
696 			state = ALLOC_GROW_HEIGHT;
697 			iblks = mp->mp_fheight - ip->i_height;
698 			branch_start = metapath_branch_start(mp);
699 			iblks += (mp->mp_fheight - branch_start);
700 		}
701 	}
702 
703 	/* start of the second part of the function (state machine) */
704 
705 	blks = dblks + iblks;
706 	i = mp->mp_aheight;
707 	do {
708 		n = blks - alloced;
709 		ret = gfs2_alloc_blocks(ip, &bn, &n, 0);
710 		if (ret)
711 			goto out;
712 		alloced += n;
713 		if (state != ALLOC_DATA || gfs2_is_jdata(ip))
714 			gfs2_trans_remove_revoke(sdp, bn, n);
715 		switch (state) {
716 		/* Growing height of tree */
717 		case ALLOC_GROW_HEIGHT:
718 			if (i == 1) {
719 				ptr = (__be64 *)(dibh->b_data +
720 						 sizeof(struct gfs2_dinode));
721 				zero_bn = *ptr;
722 			}
723 			for (; i - 1 < mp->mp_fheight - ip->i_height && n > 0;
724 			     i++, n--)
725 				gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
726 			if (i - 1 == mp->mp_fheight - ip->i_height) {
727 				i--;
728 				gfs2_buffer_copy_tail(mp->mp_bh[i],
729 						sizeof(struct gfs2_meta_header),
730 						dibh, sizeof(struct gfs2_dinode));
731 				gfs2_buffer_clear_tail(dibh,
732 						sizeof(struct gfs2_dinode) +
733 						sizeof(__be64));
734 				ptr = (__be64 *)(mp->mp_bh[i]->b_data +
735 					sizeof(struct gfs2_meta_header));
736 				*ptr = zero_bn;
737 				state = ALLOC_GROW_DEPTH;
738 				for(i = branch_start; i < mp->mp_fheight; i++) {
739 					if (mp->mp_bh[i] == NULL)
740 						break;
741 					brelse(mp->mp_bh[i]);
742 					mp->mp_bh[i] = NULL;
743 				}
744 				i = branch_start;
745 			}
746 			if (n == 0)
747 				break;
748 			fallthrough;	/* To branching from existing tree */
749 		case ALLOC_GROW_DEPTH:
750 			if (i > 1 && i < mp->mp_fheight)
751 				gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
752 			for (; i < mp->mp_fheight && n > 0; i++, n--)
753 				gfs2_indirect_init(mp, ip->i_gl, i,
754 						   mp->mp_list[i-1], bn++);
755 			if (i == mp->mp_fheight)
756 				state = ALLOC_DATA;
757 			if (n == 0)
758 				break;
759 			fallthrough;	/* To tree complete, adding data blocks */
760 		case ALLOC_DATA:
761 			BUG_ON(n > dblks);
762 			BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
763 			gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
764 			dblks = n;
765 			ptr = metapointer(end_of_metadata, mp);
766 			iomap->addr = bn << inode->i_blkbits;
767 			iomap->flags |= IOMAP_F_MERGED | IOMAP_F_NEW;
768 			while (n-- > 0)
769 				*ptr++ = cpu_to_be64(bn++);
770 			break;
771 		}
772 	} while (iomap->addr == IOMAP_NULL_ADDR);
773 
774 	iomap->type = IOMAP_MAPPED;
775 	iomap->length = (u64)dblks << inode->i_blkbits;
776 	ip->i_height = mp->mp_fheight;
777 	gfs2_add_inode_blocks(&ip->i_inode, alloced);
778 	gfs2_dinode_out(ip, dibh->b_data);
779 out:
780 	up_write(&ip->i_rw_mutex);
781 	return ret;
782 }
783 
784 #define IOMAP_F_GFS2_BOUNDARY IOMAP_F_PRIVATE
785 
786 /**
787  * gfs2_alloc_size - Compute the maximum allocation size
788  * @inode: The inode
789  * @mp: The metapath
790  * @size: Requested size in blocks
791  *
792  * Compute the maximum size of the next allocation at @mp.
793  *
794  * Returns: size in blocks
795  */
796 static u64 gfs2_alloc_size(struct inode *inode, struct metapath *mp, u64 size)
797 {
798 	struct gfs2_inode *ip = GFS2_I(inode);
799 	struct gfs2_sbd *sdp = GFS2_SB(inode);
800 	const __be64 *first, *ptr, *end;
801 
802 	/*
803 	 * For writes to stuffed files, this function is called twice via
804 	 * __gfs2_iomap_get, before and after unstuffing. The size we return the
805 	 * first time needs to be large enough to get the reservation and
806 	 * allocation sizes right.  The size we return the second time must
807 	 * be exact or else __gfs2_iomap_alloc won't do the right thing.
808 	 */
809 
810 	if (gfs2_is_stuffed(ip) || mp->mp_fheight != mp->mp_aheight) {
811 		unsigned int maxsize = mp->mp_fheight > 1 ?
812 			sdp->sd_inptrs : sdp->sd_diptrs;
813 		maxsize -= mp->mp_list[mp->mp_fheight - 1];
814 		if (size > maxsize)
815 			size = maxsize;
816 		return size;
817 	}
818 
819 	first = metapointer(ip->i_height - 1, mp);
820 	end = metaend(ip->i_height - 1, mp);
821 	if (end - first > size)
822 		end = first + size;
823 	for (ptr = first; ptr < end; ptr++) {
824 		if (*ptr)
825 			break;
826 	}
827 	return ptr - first;
828 }
829 
830 /**
831  * __gfs2_iomap_get - Map blocks from an inode to disk blocks
832  * @inode: The inode
833  * @pos: Starting position in bytes
834  * @length: Length to map, in bytes
835  * @flags: iomap flags
836  * @iomap: The iomap structure
837  * @mp: The metapath
838  *
839  * Returns: errno
840  */
841 static int __gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
842 			    unsigned flags, struct iomap *iomap,
843 			    struct metapath *mp)
844 {
845 	struct gfs2_inode *ip = GFS2_I(inode);
846 	struct gfs2_sbd *sdp = GFS2_SB(inode);
847 	loff_t size = i_size_read(inode);
848 	__be64 *ptr;
849 	sector_t lblock;
850 	sector_t lblock_stop;
851 	int ret;
852 	int eob;
853 	u64 len;
854 	struct buffer_head *dibh = NULL, *bh;
855 	u8 height;
856 
857 	if (!length)
858 		return -EINVAL;
859 
860 	down_read(&ip->i_rw_mutex);
861 
862 	ret = gfs2_meta_inode_buffer(ip, &dibh);
863 	if (ret)
864 		goto unlock;
865 	mp->mp_bh[0] = dibh;
866 
867 	if (gfs2_is_stuffed(ip)) {
868 		if (flags & IOMAP_WRITE) {
869 			loff_t max_size = gfs2_max_stuffed_size(ip);
870 
871 			if (pos + length > max_size)
872 				goto unstuff;
873 			iomap->length = max_size;
874 		} else {
875 			if (pos >= size) {
876 				if (flags & IOMAP_REPORT) {
877 					ret = -ENOENT;
878 					goto unlock;
879 				} else {
880 					iomap->offset = pos;
881 					iomap->length = length;
882 					goto hole_found;
883 				}
884 			}
885 			iomap->length = size;
886 		}
887 		iomap->addr = (ip->i_no_addr << inode->i_blkbits) +
888 			      sizeof(struct gfs2_dinode);
889 		iomap->type = IOMAP_INLINE;
890 		iomap->inline_data = dibh->b_data + sizeof(struct gfs2_dinode);
891 		goto out;
892 	}
893 
894 unstuff:
895 	lblock = pos >> inode->i_blkbits;
896 	iomap->offset = lblock << inode->i_blkbits;
897 	lblock_stop = (pos + length - 1) >> inode->i_blkbits;
898 	len = lblock_stop - lblock + 1;
899 	iomap->length = len << inode->i_blkbits;
900 
901 	height = ip->i_height;
902 	while ((lblock + 1) * sdp->sd_sb.sb_bsize > sdp->sd_heightsize[height])
903 		height++;
904 	find_metapath(sdp, lblock, mp, height);
905 	if (height > ip->i_height || gfs2_is_stuffed(ip))
906 		goto do_alloc;
907 
908 	ret = lookup_metapath(ip, mp);
909 	if (ret)
910 		goto unlock;
911 
912 	if (mp->mp_aheight != ip->i_height)
913 		goto do_alloc;
914 
915 	ptr = metapointer(ip->i_height - 1, mp);
916 	if (*ptr == 0)
917 		goto do_alloc;
918 
919 	bh = mp->mp_bh[ip->i_height - 1];
920 	len = gfs2_extent_length(bh, ptr, &eob);
921 
922 	iomap->addr = be64_to_cpu(*ptr) << inode->i_blkbits;
923 	iomap->length = len << inode->i_blkbits;
924 	iomap->type = IOMAP_MAPPED;
925 	iomap->flags |= IOMAP_F_MERGED;
926 	if (eob)
927 		iomap->flags |= IOMAP_F_GFS2_BOUNDARY;
928 
929 out:
930 	iomap->bdev = inode->i_sb->s_bdev;
931 unlock:
932 	up_read(&ip->i_rw_mutex);
933 	return ret;
934 
935 do_alloc:
936 	if (flags & IOMAP_REPORT) {
937 		if (pos >= size)
938 			ret = -ENOENT;
939 		else if (height == ip->i_height)
940 			ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
941 		else
942 			iomap->length = size - iomap->offset;
943 	} else if (flags & IOMAP_WRITE) {
944 		u64 alloc_size;
945 
946 		if (flags & IOMAP_DIRECT)
947 			goto out;  /* (see gfs2_file_direct_write) */
948 
949 		len = gfs2_alloc_size(inode, mp, len);
950 		alloc_size = len << inode->i_blkbits;
951 		if (alloc_size < iomap->length)
952 			iomap->length = alloc_size;
953 	} else {
954 		if (pos < size && height == ip->i_height)
955 			ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
956 	}
957 hole_found:
958 	iomap->addr = IOMAP_NULL_ADDR;
959 	iomap->type = IOMAP_HOLE;
960 	goto out;
961 }
962 
963 static struct folio *
964 gfs2_iomap_get_folio(struct iomap_iter *iter, loff_t pos, unsigned len)
965 {
966 	struct inode *inode = iter->inode;
967 	struct gfs2_inode *ip = GFS2_I(inode);
968 	unsigned int blockmask = i_blocksize(inode) - 1;
969 	struct gfs2_sbd *sdp = GFS2_SB(inode);
970 	unsigned int blocks;
971 	struct folio *folio;
972 	int status;
973 
974 	if (!gfs2_is_jdata(ip) && !gfs2_is_stuffed(ip))
975 		return iomap_get_folio(iter, pos, len);
976 
977 	blocks = ((pos & blockmask) + len + blockmask) >> inode->i_blkbits;
978 	status = gfs2_trans_begin(sdp, RES_DINODE + blocks, 0);
979 	if (status)
980 		return ERR_PTR(status);
981 
982 	folio = iomap_get_folio(iter, pos, len);
983 	if (IS_ERR(folio))
984 		gfs2_trans_end(sdp);
985 	return folio;
986 }
987 
988 static void gfs2_iomap_put_folio(struct inode *inode, loff_t pos,
989 				 unsigned copied, struct folio *folio)
990 {
991 	struct gfs2_trans *tr = current->journal_info;
992 	struct gfs2_inode *ip = GFS2_I(inode);
993 	struct gfs2_sbd *sdp = GFS2_SB(inode);
994 
995 	if (gfs2_is_jdata(ip) && !gfs2_is_stuffed(ip))
996 		gfs2_trans_add_databufs(ip->i_gl, folio,
997 					offset_in_folio(folio, pos),
998 					copied);
999 
1000 	folio_unlock(folio);
1001 	folio_put(folio);
1002 
1003 	if (gfs2_is_jdata(ip) || gfs2_is_stuffed(ip)) {
1004 		if (tr->tr_num_buf_new)
1005 			__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1006 		gfs2_trans_end(sdp);
1007 	}
1008 }
1009 
1010 const struct iomap_write_ops gfs2_iomap_write_ops = {
1011 	.get_folio = gfs2_iomap_get_folio,
1012 	.put_folio = gfs2_iomap_put_folio,
1013 };
1014 
1015 static int gfs2_iomap_begin_write(struct inode *inode, loff_t pos,
1016 				  loff_t length, unsigned flags,
1017 				  struct iomap *iomap,
1018 				  struct metapath *mp)
1019 {
1020 	struct gfs2_inode *ip = GFS2_I(inode);
1021 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1022 	bool unstuff;
1023 	int ret;
1024 
1025 	unstuff = gfs2_is_stuffed(ip) &&
1026 		  pos + length > gfs2_max_stuffed_size(ip);
1027 
1028 	if (unstuff || iomap->type == IOMAP_HOLE) {
1029 		unsigned int data_blocks, ind_blocks;
1030 		struct gfs2_alloc_parms ap = {};
1031 		unsigned int rblocks;
1032 		struct gfs2_trans *tr;
1033 
1034 		gfs2_write_calc_reserv(ip, iomap->length, &data_blocks,
1035 				       &ind_blocks);
1036 		ap.target = data_blocks + ind_blocks;
1037 		ret = gfs2_quota_lock_check(ip, &ap);
1038 		if (ret)
1039 			return ret;
1040 
1041 		ret = gfs2_inplace_reserve(ip, &ap);
1042 		if (ret)
1043 			goto out_qunlock;
1044 
1045 		rblocks = RES_DINODE + ind_blocks;
1046 		if (gfs2_is_jdata(ip))
1047 			rblocks += data_blocks;
1048 		if (ind_blocks || data_blocks)
1049 			rblocks += RES_STATFS + RES_QUOTA;
1050 		if (inode == sdp->sd_rindex)
1051 			rblocks += 2 * RES_STATFS;
1052 		rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
1053 
1054 		ret = gfs2_trans_begin(sdp, rblocks,
1055 				       iomap->length >> inode->i_blkbits);
1056 		if (ret)
1057 			goto out_trans_fail;
1058 
1059 		if (unstuff) {
1060 			ret = gfs2_unstuff_dinode(ip);
1061 			if (ret)
1062 				goto out_trans_end;
1063 			release_metapath(mp);
1064 			ret = __gfs2_iomap_get(inode, iomap->offset,
1065 					       iomap->length, flags, iomap, mp);
1066 			if (ret)
1067 				goto out_trans_end;
1068 		}
1069 
1070 		if (iomap->type == IOMAP_HOLE) {
1071 			ret = __gfs2_iomap_alloc(inode, iomap, mp);
1072 			if (ret) {
1073 				gfs2_trans_end(sdp);
1074 				gfs2_inplace_release(ip);
1075 				punch_hole(ip, iomap->offset, iomap->length);
1076 				goto out_qunlock;
1077 			}
1078 		}
1079 
1080 		tr = current->journal_info;
1081 		if (tr->tr_num_buf_new)
1082 			__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1083 
1084 		gfs2_trans_end(sdp);
1085 	}
1086 
1087 	return 0;
1088 
1089 out_trans_end:
1090 	gfs2_trans_end(sdp);
1091 out_trans_fail:
1092 	gfs2_inplace_release(ip);
1093 out_qunlock:
1094 	gfs2_quota_unlock(ip);
1095 	return ret;
1096 }
1097 
1098 static int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
1099 			    unsigned flags, struct iomap *iomap,
1100 			    struct iomap *srcmap)
1101 {
1102 	struct gfs2_inode *ip = GFS2_I(inode);
1103 	struct metapath mp = { .mp_aheight = 1, };
1104 	int ret;
1105 
1106 	if (gfs2_is_jdata(ip))
1107 		iomap->flags |= IOMAP_F_BUFFER_HEAD;
1108 
1109 	trace_gfs2_iomap_start(ip, pos, length, flags);
1110 	ret = __gfs2_iomap_get(inode, pos, length, flags, iomap, &mp);
1111 	if (ret)
1112 		goto out_unlock;
1113 
1114 	switch(flags & (IOMAP_WRITE | IOMAP_ZERO)) {
1115 	case IOMAP_WRITE:
1116 		if (flags & IOMAP_DIRECT) {
1117 			/*
1118 			 * Silently fall back to buffered I/O for stuffed files
1119 			 * or if we've got a hole (see gfs2_file_direct_write).
1120 			 */
1121 			if (iomap->type != IOMAP_MAPPED)
1122 				ret = -ENOTBLK;
1123 			goto out_unlock;
1124 		}
1125 		break;
1126 	case IOMAP_ZERO:
1127 		if (iomap->type == IOMAP_HOLE)
1128 			goto out_unlock;
1129 		break;
1130 	default:
1131 		goto out;
1132 	}
1133 
1134 	ret = gfs2_iomap_begin_write(inode, pos, length, flags, iomap, &mp);
1135 	if (ret)
1136 		goto out_unlock;
1137 
1138 out:
1139 	if (iomap->type == IOMAP_INLINE) {
1140 		iomap->private = metapath_dibh(&mp);
1141 		get_bh(iomap->private);
1142 	}
1143 
1144 out_unlock:
1145 	release_metapath(&mp);
1146 	trace_gfs2_iomap_end(ip, iomap, ret);
1147 	return ret;
1148 }
1149 
1150 static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length,
1151 			  ssize_t written, unsigned flags, struct iomap *iomap)
1152 {
1153 	struct gfs2_inode *ip = GFS2_I(inode);
1154 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1155 
1156 	if (iomap->private)
1157 		brelse(iomap->private);
1158 
1159 	switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
1160 	case IOMAP_WRITE:
1161 		if (flags & IOMAP_DIRECT)
1162 			return 0;
1163 		break;
1164 	case IOMAP_ZERO:
1165 		 if (iomap->type == IOMAP_HOLE)
1166 			 return 0;
1167 		 break;
1168 	default:
1169 		 return 0;
1170 	}
1171 
1172 	if (!gfs2_is_stuffed(ip))
1173 		gfs2_ordered_add_inode(ip);
1174 
1175 	if (inode == sdp->sd_rindex)
1176 		adjust_fs_space(inode);
1177 
1178 	gfs2_inplace_release(ip);
1179 
1180 	if (ip->i_qadata && ip->i_qadata->qa_qd_num)
1181 		gfs2_quota_unlock(ip);
1182 
1183 	if (length != written && (iomap->flags & IOMAP_F_NEW)) {
1184 		/* Deallocate blocks that were just allocated. */
1185 		loff_t hstart = round_up(pos + written, i_blocksize(inode));
1186 		loff_t hend = iomap->offset + iomap->length;
1187 
1188 		if (hstart < hend) {
1189 			truncate_pagecache_range(inode, hstart, hend - 1);
1190 			punch_hole(ip, hstart, hend - hstart);
1191 		}
1192 	}
1193 
1194 	if (unlikely(!written))
1195 		return 0;
1196 
1197 	if (iomap->flags & IOMAP_F_SIZE_CHANGED)
1198 		mark_inode_dirty(inode);
1199 	set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
1200 	return 0;
1201 }
1202 
1203 static DEFINE_IOMAP_ITER_NEXT_END(gfs2_iomap_next, gfs2_iomap_begin,
1204 				  gfs2_iomap_end);
1205 
1206 const struct iomap_ops gfs2_iomap_ops = {
1207 	.iomap_next = gfs2_iomap_next,
1208 };
1209 
1210 /**
1211  * gfs2_block_map - Map one or more blocks of an inode to a disk block
1212  * @inode: The inode
1213  * @lblock: The logical block number
1214  * @bh_map: The bh to be mapped
1215  * @create: True if its ok to alloc blocks to satify the request
1216  *
1217  * The size of the requested mapping is defined in bh_map->b_size.
1218  *
1219  * Clears buffer_mapped(bh_map) and leaves bh_map->b_size unchanged
1220  * when @lblock is not mapped.  Sets buffer_mapped(bh_map) and
1221  * bh_map->b_size to indicate the size of the mapping when @lblock and
1222  * successive blocks are mapped, up to the requested size.
1223  *
1224  * Sets buffer_boundary() if a read of metadata will be required
1225  * before the next block can be mapped. Sets buffer_new() if new
1226  * blocks were allocated.
1227  *
1228  * Returns: errno
1229  */
1230 
1231 int gfs2_block_map(struct inode *inode, sector_t lblock,
1232 		   struct buffer_head *bh_map, int create)
1233 {
1234 	struct gfs2_inode *ip = GFS2_I(inode);
1235 	loff_t pos = (loff_t)lblock << inode->i_blkbits;
1236 	loff_t length = bh_map->b_size;
1237 	struct iomap iomap = { };
1238 	int ret;
1239 
1240 	clear_buffer_mapped(bh_map);
1241 	clear_buffer_new(bh_map);
1242 	clear_buffer_boundary(bh_map);
1243 	trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
1244 
1245 	if (!create)
1246 		ret = gfs2_iomap_get(inode, pos, length, &iomap);
1247 	else
1248 		ret = gfs2_iomap_alloc(inode, pos, length, &iomap);
1249 	if (ret)
1250 		goto out;
1251 
1252 	if (iomap.length > bh_map->b_size) {
1253 		iomap.length = bh_map->b_size;
1254 		iomap.flags &= ~IOMAP_F_GFS2_BOUNDARY;
1255 	}
1256 	if (iomap.addr != IOMAP_NULL_ADDR)
1257 		map_bh(bh_map, inode->i_sb, iomap.addr >> inode->i_blkbits);
1258 	bh_map->b_size = iomap.length;
1259 	if (iomap.flags & IOMAP_F_GFS2_BOUNDARY)
1260 		set_buffer_boundary(bh_map);
1261 	if (iomap.flags & IOMAP_F_NEW)
1262 		set_buffer_new(bh_map);
1263 
1264 out:
1265 	trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
1266 	return ret;
1267 }
1268 
1269 int gfs2_get_extent(struct inode *inode, u64 lblock, u64 *dblock,
1270 		    unsigned int *extlen)
1271 {
1272 	unsigned int blkbits = inode->i_blkbits;
1273 	struct iomap iomap = { };
1274 	unsigned int len;
1275 	int ret;
1276 
1277 	ret = gfs2_iomap_get(inode, lblock << blkbits, *extlen << blkbits,
1278 			     &iomap);
1279 	if (ret)
1280 		return ret;
1281 	if (iomap.type != IOMAP_MAPPED)
1282 		return -EIO;
1283 	*dblock = iomap.addr >> blkbits;
1284 	len = iomap.length >> blkbits;
1285 	if (len < *extlen)
1286 		*extlen = len;
1287 	return 0;
1288 }
1289 
1290 int gfs2_alloc_extent(struct inode *inode, u64 lblock, u64 *dblock,
1291 		      unsigned int *extlen, bool *new)
1292 {
1293 	unsigned int blkbits = inode->i_blkbits;
1294 	struct iomap iomap = { };
1295 	unsigned int len;
1296 	int ret;
1297 
1298 	ret = gfs2_iomap_alloc(inode, lblock << blkbits, *extlen << blkbits,
1299 			       &iomap);
1300 	if (ret)
1301 		return ret;
1302 	if (iomap.type != IOMAP_MAPPED)
1303 		return -EIO;
1304 	*dblock = iomap.addr >> blkbits;
1305 	len = iomap.length >> blkbits;
1306 	if (len < *extlen)
1307 		*extlen = len;
1308 	*new = iomap.flags & IOMAP_F_NEW;
1309 	return 0;
1310 }
1311 
1312 /*
1313  * NOTE: Never call gfs2_block_zero_range with an open transaction because it
1314  * uses iomap write to perform its actions, which begin their own transactions
1315  * (iomap_begin, get_folio, etc.)
1316  */
1317 static int gfs2_block_zero_range(struct inode *inode, loff_t from, loff_t length)
1318 {
1319 	BUG_ON(current->journal_info);
1320 	if (from >= inode->i_size)
1321 		return 0;
1322 	length = min(length, inode->i_size - from);
1323 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
1324 			&gfs2_iomap_write_ops, NULL);
1325 }
1326 
1327 int gfs2_clear_beyond_eof(struct inode *inode, loff_t end)
1328 {
1329 	loff_t isize = i_size_read(inode);
1330 	unsigned int len = isize & ~PAGE_MASK;
1331 
1332 	if (!len || isize >= end)
1333 		return 0;
1334 	len = PAGE_SIZE - len;
1335 	if (end - isize < len)
1336 		len = end - isize;
1337 	return gfs2_block_zero_range(inode, isize, len);
1338 }
1339 
1340 #define GFS2_JTRUNC_REVOKES 8192
1341 
1342 /**
1343  * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
1344  * @inode: The inode being truncated
1345  * @oldsize: The original (larger) size
1346  * @newsize: The new smaller size
1347  *
1348  * With jdata files, we have to journal a revoke for each block which is
1349  * truncated. As a result, we need to split this into separate transactions
1350  * if the number of pages being truncated gets too large.
1351  */
1352 
1353 static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
1354 {
1355 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1356 	u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
1357 	u64 chunk;
1358 	int error;
1359 
1360 	while (oldsize != newsize) {
1361 		struct gfs2_trans *tr;
1362 		unsigned int offs;
1363 
1364 		chunk = oldsize - newsize;
1365 		if (chunk > max_chunk)
1366 			chunk = max_chunk;
1367 
1368 		offs = oldsize & ~PAGE_MASK;
1369 		if (offs && chunk > PAGE_SIZE)
1370 			chunk = offs + ((chunk - offs) & PAGE_MASK);
1371 
1372 		truncate_pagecache(inode, oldsize - chunk);
1373 		oldsize -= chunk;
1374 
1375 		tr = current->journal_info;
1376 		if (!test_bit(TR_TOUCHED, &tr->tr_flags))
1377 			continue;
1378 
1379 		gfs2_trans_end(sdp);
1380 		error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
1381 		if (error)
1382 			return error;
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 static int trunc_start(struct inode *inode, u64 newsize)
1389 {
1390 	struct gfs2_inode *ip = GFS2_I(inode);
1391 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1392 	struct buffer_head *dibh = NULL;
1393 	int journaled = gfs2_is_jdata(ip);
1394 	u64 oldsize = inode->i_size;
1395 	int error;
1396 
1397 	if (!gfs2_is_stuffed(ip)) {
1398 		unsigned int blocksize = i_blocksize(inode);
1399 		unsigned int offs = newsize & (blocksize - 1);
1400 		if (offs) {
1401 			error = gfs2_block_zero_range(inode, newsize,
1402 						      blocksize - offs);
1403 			if (error)
1404 				return error;
1405 		}
1406 	}
1407 	if (journaled)
1408 		error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
1409 	else
1410 		error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1411 	if (error)
1412 		return error;
1413 
1414 	error = gfs2_meta_inode_buffer(ip, &dibh);
1415 	if (error)
1416 		goto out;
1417 
1418 	gfs2_trans_add_meta(ip->i_gl, dibh);
1419 
1420 	if (gfs2_is_stuffed(ip))
1421 		gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
1422 	else
1423 		ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
1424 
1425 	i_size_write(inode, newsize);
1426 	inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode));
1427 	gfs2_dinode_out(ip, dibh->b_data);
1428 
1429 	if (journaled)
1430 		error = gfs2_journaled_truncate(inode, oldsize, newsize);
1431 	else
1432 		truncate_pagecache(inode, newsize);
1433 
1434 out:
1435 	brelse(dibh);
1436 	if (current->journal_info)
1437 		gfs2_trans_end(sdp);
1438 	return error;
1439 }
1440 
1441 int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
1442 		   struct iomap *iomap)
1443 {
1444 	struct metapath mp = { .mp_aheight = 1, };
1445 	int ret;
1446 
1447 	ret = __gfs2_iomap_get(inode, pos, length, 0, iomap, &mp);
1448 	release_metapath(&mp);
1449 	return ret;
1450 }
1451 
1452 int gfs2_iomap_alloc(struct inode *inode, loff_t pos, loff_t length,
1453 		     struct iomap *iomap)
1454 {
1455 	struct metapath mp = { .mp_aheight = 1, };
1456 	int ret;
1457 
1458 	ret = __gfs2_iomap_get(inode, pos, length, IOMAP_WRITE, iomap, &mp);
1459 	if (!ret && iomap->type == IOMAP_HOLE)
1460 		ret = __gfs2_iomap_alloc(inode, iomap, &mp);
1461 	release_metapath(&mp);
1462 	return ret;
1463 }
1464 
1465 /**
1466  * sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein
1467  * @ip: inode
1468  * @rd_gh: holder of resource group glock
1469  * @bh: buffer head to sweep
1470  * @start: starting point in bh
1471  * @end: end point in bh
1472  * @meta: true if bh points to metadata (rather than data)
1473  * @btotal: place to keep count of total blocks freed
1474  *
1475  * We sweep a metadata buffer (provided by the metapath) for blocks we need to
1476  * free, and free them all. However, we do it one rgrp at a time. If this
1477  * block has references to multiple rgrps, we break it into individual
1478  * transactions. This allows other processes to use the rgrps while we're
1479  * focused on a single one, for better concurrency / performance.
1480  * At every transaction boundary, we rewrite the inode into the journal.
1481  * That way the bitmaps are kept consistent with the inode and we can recover
1482  * if we're interrupted by power-outages.
1483  *
1484  * Returns: 0, or return code if an error occurred.
1485  *          *btotal has the total number of blocks freed
1486  */
1487 static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,
1488 			      struct buffer_head *bh, __be64 *start, __be64 *end,
1489 			      bool meta, u32 *btotal)
1490 {
1491 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1492 	struct gfs2_rgrpd *rgd;
1493 	struct gfs2_trans *tr;
1494 	__be64 *p;
1495 	int blks_outside_rgrp;
1496 	u64 bn, bstart, isize_blks;
1497 	s64 blen; /* needs to be s64 or gfs2_add_inode_blocks breaks */
1498 	int ret = 0;
1499 	bool buf_in_tr = false; /* buffer was added to transaction */
1500 
1501 more_rgrps:
1502 	rgd = NULL;
1503 	if (gfs2_holder_initialized(rd_gh)) {
1504 		rgd = gfs2_glock2rgrp(rd_gh->gh_gl);
1505 		gfs2_assert_withdraw(sdp,
1506 			     gfs2_glock_is_locked_by_me(rd_gh->gh_gl));
1507 	}
1508 	blks_outside_rgrp = 0;
1509 	bstart = 0;
1510 	blen = 0;
1511 
1512 	for (p = start; p < end; p++) {
1513 		if (!*p)
1514 			continue;
1515 		bn = be64_to_cpu(*p);
1516 
1517 		if (rgd) {
1518 			if (!rgrp_contains_block(rgd, bn)) {
1519 				blks_outside_rgrp++;
1520 				continue;
1521 			}
1522 		} else {
1523 			rgd = gfs2_blk2rgrpd(sdp, bn, true);
1524 			if (unlikely(!rgd)) {
1525 				ret = -EIO;
1526 				goto out;
1527 			}
1528 			ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1529 						 LM_FLAG_NODE_SCOPE, rd_gh);
1530 			if (ret)
1531 				goto out;
1532 
1533 			/* Must be done with the rgrp glock held: */
1534 			if (gfs2_rs_active(&ip->i_res) &&
1535 			    rgd == ip->i_res.rs_rgd)
1536 				gfs2_rs_deltree(&ip->i_res);
1537 		}
1538 
1539 		/* The size of our transactions will be unknown until we
1540 		   actually process all the metadata blocks that relate to
1541 		   the rgrp. So we estimate. We know it can't be more than
1542 		   the dinode's i_blocks and we don't want to exceed the
1543 		   journal flush threshold, sd_log_thresh2. */
1544 		if (current->journal_info == NULL) {
1545 			unsigned int jblocks_rqsted, revokes;
1546 
1547 			jblocks_rqsted = rgd->rd_length + RES_DINODE +
1548 				RES_INDIRECT;
1549 			isize_blks = gfs2_get_inode_blocks(&ip->i_inode);
1550 			if (isize_blks > atomic_read(&sdp->sd_log_thresh2))
1551 				jblocks_rqsted +=
1552 					atomic_read(&sdp->sd_log_thresh2);
1553 			else
1554 				jblocks_rqsted += isize_blks;
1555 			revokes = jblocks_rqsted;
1556 			if (meta)
1557 				revokes += end - start;
1558 			else if (ip->i_diskflags & GFS2_DIF_EXHASH)
1559 				revokes += sdp->sd_inptrs;
1560 			ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes);
1561 			if (ret)
1562 				goto out_unlock;
1563 			down_write(&ip->i_rw_mutex);
1564 		}
1565 		/* check if we will exceed the transaction blocks requested */
1566 		tr = current->journal_info;
1567 		if (tr->tr_num_buf_new + RES_STATFS +
1568 		    RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) {
1569 			/* We set blks_outside_rgrp to ensure the loop will
1570 			   be repeated for the same rgrp, but with a new
1571 			   transaction. */
1572 			blks_outside_rgrp++;
1573 			/* This next part is tricky. If the buffer was added
1574 			   to the transaction, we've already set some block
1575 			   pointers to 0, so we better follow through and free
1576 			   them, or we will introduce corruption (so break).
1577 			   This may be impossible, or at least rare, but I
1578 			   decided to cover the case regardless.
1579 
1580 			   If the buffer was not added to the transaction
1581 			   (this call), doing so would exceed our transaction
1582 			   size, so we need to end the transaction and start a
1583 			   new one (so goto). */
1584 
1585 			if (buf_in_tr)
1586 				break;
1587 			goto out_unlock;
1588 		}
1589 
1590 		gfs2_trans_add_meta(ip->i_gl, bh);
1591 		buf_in_tr = true;
1592 		*p = 0;
1593 		if (bstart + blen == bn) {
1594 			blen++;
1595 			continue;
1596 		}
1597 		if (bstart) {
1598 			__gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta);
1599 			(*btotal) += blen;
1600 			gfs2_add_inode_blocks(&ip->i_inode, -blen);
1601 		}
1602 		bstart = bn;
1603 		blen = 1;
1604 	}
1605 	if (bstart) {
1606 		__gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta);
1607 		(*btotal) += blen;
1608 		gfs2_add_inode_blocks(&ip->i_inode, -blen);
1609 	}
1610 out_unlock:
1611 	if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks
1612 					    outside the rgrp we just processed,
1613 					    do it all over again. */
1614 		if (current->journal_info) {
1615 			struct buffer_head *dibh;
1616 
1617 			ret = gfs2_meta_inode_buffer(ip, &dibh);
1618 			if (ret)
1619 				goto out;
1620 
1621 			/* Every transaction boundary, we rewrite the dinode
1622 			   to keep its di_blocks current in case of failure. */
1623 			inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode));
1624 			gfs2_trans_add_meta(ip->i_gl, dibh);
1625 			gfs2_dinode_out(ip, dibh->b_data);
1626 			brelse(dibh);
1627 			up_write(&ip->i_rw_mutex);
1628 			gfs2_trans_end(sdp);
1629 			buf_in_tr = false;
1630 		}
1631 		gfs2_glock_dq_uninit(rd_gh);
1632 		cond_resched();
1633 		goto more_rgrps;
1634 	}
1635 out:
1636 	return ret;
1637 }
1638 
1639 static bool mp_eq_to_hgt(struct metapath *mp, __u16 *list, unsigned int h)
1640 {
1641 	if (memcmp(mp->mp_list, list, h * sizeof(mp->mp_list[0])))
1642 		return false;
1643 	return true;
1644 }
1645 
1646 /**
1647  * find_nonnull_ptr - find a non-null pointer given a metapath and height
1648  * @sdp: The superblock
1649  * @mp: starting metapath
1650  * @h: desired height to search
1651  * @end_list: See punch_hole().
1652  * @end_aligned: See punch_hole().
1653  *
1654  * Assumes the metapath is valid (with buffers) out to height h.
1655  * Returns: true if a non-null pointer was found in the metapath buffer
1656  *          false if all remaining pointers are NULL in the buffer
1657  */
1658 static bool find_nonnull_ptr(struct gfs2_sbd *sdp, struct metapath *mp,
1659 			     unsigned int h,
1660 			     __u16 *end_list, unsigned int end_aligned)
1661 {
1662 	struct buffer_head *bh = mp->mp_bh[h];
1663 	__be64 *first, *ptr, *end;
1664 
1665 	first = metaptr1(h, mp);
1666 	ptr = first + mp->mp_list[h];
1667 	end = (__be64 *)(bh->b_data + bh->b_size);
1668 	if (end_list && mp_eq_to_hgt(mp, end_list, h)) {
1669 		bool keep_end = h < end_aligned;
1670 		end = first + end_list[h] + keep_end;
1671 	}
1672 
1673 	while (ptr < end) {
1674 		if (*ptr) { /* if we have a non-null pointer */
1675 			mp->mp_list[h] = ptr - first;
1676 			h++;
1677 			if (h < GFS2_MAX_META_HEIGHT)
1678 				mp->mp_list[h] = 0;
1679 			return true;
1680 		}
1681 		ptr++;
1682 	}
1683 	return false;
1684 }
1685 
1686 enum dealloc_states {
1687 	DEALLOC_MP_FULL = 0,    /* Strip a metapath with all buffers read in */
1688 	DEALLOC_MP_LOWER = 1,   /* lower the metapath strip height */
1689 	DEALLOC_FILL_MP = 2,  /* Fill in the metapath to the given height. */
1690 	DEALLOC_DONE = 3,       /* process complete */
1691 };
1692 
1693 static inline void
1694 metapointer_range(struct metapath *mp, int height,
1695 		  __u16 *start_list, unsigned int start_aligned,
1696 		  __u16 *end_list, unsigned int end_aligned,
1697 		  __be64 **start, __be64 **end)
1698 {
1699 	struct buffer_head *bh = mp->mp_bh[height];
1700 	__be64 *first;
1701 
1702 	first = metaptr1(height, mp);
1703 	*start = first;
1704 	if (mp_eq_to_hgt(mp, start_list, height)) {
1705 		bool keep_start = height < start_aligned;
1706 		*start = first + start_list[height] + keep_start;
1707 	}
1708 	*end = (__be64 *)(bh->b_data + bh->b_size);
1709 	if (end_list && mp_eq_to_hgt(mp, end_list, height)) {
1710 		bool keep_end = height < end_aligned;
1711 		*end = first + end_list[height] + keep_end;
1712 	}
1713 }
1714 
1715 static inline bool walk_done(struct gfs2_sbd *sdp,
1716 			     struct metapath *mp, int height,
1717 			     __u16 *end_list, unsigned int end_aligned)
1718 {
1719 	__u16 end;
1720 
1721 	if (end_list) {
1722 		bool keep_end = height < end_aligned;
1723 		if (!mp_eq_to_hgt(mp, end_list, height))
1724 			return false;
1725 		end = end_list[height] + keep_end;
1726 	} else
1727 		end = (height > 0) ? sdp->sd_inptrs : sdp->sd_diptrs;
1728 	return mp->mp_list[height] >= end;
1729 }
1730 
1731 /**
1732  * punch_hole - deallocate blocks in a file
1733  * @ip: inode to truncate
1734  * @offset: the start of the hole
1735  * @length: the size of the hole (or 0 for truncate)
1736  *
1737  * Punch a hole into a file or truncate a file at a given position.  This
1738  * function operates in whole blocks (@offset and @length are rounded
1739  * accordingly); partially filled blocks must be cleared otherwise.
1740  *
1741  * This function works from the bottom up, and from the right to the left. In
1742  * other words, it strips off the highest layer (data) before stripping any of
1743  * the metadata. Doing it this way is best in case the operation is interrupted
1744  * by power failure, etc.  The dinode is rewritten in every transaction to
1745  * guarantee integrity.
1746  */
1747 static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length)
1748 {
1749 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1750 	u64 maxsize = sdp->sd_heightsize[ip->i_height];
1751 	struct metapath mp = {};
1752 	struct buffer_head *dibh, *bh;
1753 	struct gfs2_holder rd_gh;
1754 	unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift;
1755 	unsigned int bsize = 1 << bsize_shift;
1756 	u64 lblock = (offset + bsize - 1) >> bsize_shift;
1757 	__u16 start_list[GFS2_MAX_META_HEIGHT];
1758 	__u16 __end_list[GFS2_MAX_META_HEIGHT], *end_list = NULL;
1759 	unsigned int start_aligned, end_aligned;
1760 	unsigned int strip_h = ip->i_height - 1;
1761 	u32 btotal = 0;
1762 	int ret, state;
1763 	int mp_h; /* metapath buffers are read in to this height */
1764 	u64 prev_bnr = 0;
1765 	__be64 *start, *end;
1766 
1767 	if (offset + bsize - 1 >= maxsize) {
1768 		/*
1769 		 * The starting point lies beyond the allocated metadata;
1770 		 * there are no blocks to deallocate.
1771 		 */
1772 		return 0;
1773 	}
1774 
1775 	/*
1776 	 * The start position of the hole is defined by lblock, start_list, and
1777 	 * start_aligned.  The end position of the hole is defined by lend,
1778 	 * end_list, and end_aligned.
1779 	 *
1780 	 * start_aligned and end_aligned define down to which height the start
1781 	 * and end positions are aligned to the metadata tree (i.e., the
1782 	 * position is a multiple of the metadata granularity at the height
1783 	 * above).  This determines at which heights additional meta pointers
1784 	 * needs to be preserved for the remaining data.
1785 	 */
1786 
1787 	if (length) {
1788 		u64 end_offset = offset + length;
1789 		u64 lend;
1790 
1791 		/*
1792 		 * Clip the end at the maximum file size for the given height:
1793 		 * that's how far the metadata goes; files bigger than that
1794 		 * will have additional layers of indirection.
1795 		 */
1796 		if (end_offset > maxsize)
1797 			end_offset = maxsize;
1798 		lend = end_offset >> bsize_shift;
1799 
1800 		if (lblock >= lend)
1801 			return 0;
1802 
1803 		find_metapath(sdp, lend, &mp, ip->i_height);
1804 		end_list = __end_list;
1805 		memcpy(end_list, mp.mp_list, sizeof(mp.mp_list));
1806 
1807 		for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) {
1808 			if (end_list[mp_h])
1809 				break;
1810 		}
1811 		end_aligned = mp_h;
1812 	}
1813 
1814 	find_metapath(sdp, lblock, &mp, ip->i_height);
1815 	memcpy(start_list, mp.mp_list, sizeof(start_list));
1816 
1817 	for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) {
1818 		if (start_list[mp_h])
1819 			break;
1820 	}
1821 	start_aligned = mp_h;
1822 
1823 	ret = gfs2_meta_inode_buffer(ip, &dibh);
1824 	if (ret)
1825 		return ret;
1826 
1827 	mp.mp_bh[0] = dibh;
1828 	ret = lookup_metapath(ip, &mp);
1829 	if (ret)
1830 		goto out_metapath;
1831 
1832 	/* issue read-ahead on metadata */
1833 	for (mp_h = 0; mp_h < mp.mp_aheight - 1; mp_h++) {
1834 		metapointer_range(&mp, mp_h, start_list, start_aligned,
1835 				  end_list, end_aligned, &start, &end);
1836 		gfs2_metapath_ra(ip->i_gl, start, end);
1837 	}
1838 
1839 	if (mp.mp_aheight == ip->i_height)
1840 		state = DEALLOC_MP_FULL; /* We have a complete metapath */
1841 	else
1842 		state = DEALLOC_FILL_MP; /* deal with partial metapath */
1843 
1844 	ret = gfs2_rindex_update(sdp);
1845 	if (ret)
1846 		goto out_metapath;
1847 
1848 	ret = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1849 	if (ret)
1850 		goto out_metapath;
1851 	gfs2_holder_mark_uninitialized(&rd_gh);
1852 
1853 	mp_h = strip_h;
1854 
1855 	while (state != DEALLOC_DONE) {
1856 		switch (state) {
1857 		/* Truncate a full metapath at the given strip height.
1858 		 * Note that strip_h == mp_h in order to be in this state. */
1859 		case DEALLOC_MP_FULL:
1860 			bh = mp.mp_bh[mp_h];
1861 			gfs2_assert_withdraw(sdp, bh);
1862 			if (gfs2_assert_withdraw(sdp,
1863 						 prev_bnr != bh->b_blocknr)) {
1864 				fs_emerg(sdp, "inode %llu, block:%llu, i_h:%u, "
1865 					 "s_h:%u, mp_h:%u\n",
1866 				       (unsigned long long)ip->i_no_addr,
1867 				       prev_bnr, ip->i_height, strip_h, mp_h);
1868 			}
1869 			prev_bnr = bh->b_blocknr;
1870 
1871 			if (gfs2_metatype_check(sdp, bh,
1872 						(mp_h ? GFS2_METATYPE_IN :
1873 							GFS2_METATYPE_DI))) {
1874 				ret = -EIO;
1875 				goto out;
1876 			}
1877 
1878 			/*
1879 			 * Below, passing end_aligned as 0 gives us the
1880 			 * metapointer range excluding the end point: the end
1881 			 * point is the first metapath we must not deallocate!
1882 			 */
1883 
1884 			metapointer_range(&mp, mp_h, start_list, start_aligned,
1885 					  end_list, 0 /* end_aligned */,
1886 					  &start, &end);
1887 			ret = sweep_bh_for_rgrps(ip, &rd_gh, mp.mp_bh[mp_h],
1888 						 start, end,
1889 						 mp_h != ip->i_height - 1,
1890 						 &btotal);
1891 
1892 			/* If we hit an error or just swept dinode buffer,
1893 			   just exit. */
1894 			if (ret || !mp_h) {
1895 				state = DEALLOC_DONE;
1896 				break;
1897 			}
1898 			state = DEALLOC_MP_LOWER;
1899 			break;
1900 
1901 		/* lower the metapath strip height */
1902 		case DEALLOC_MP_LOWER:
1903 			/* We're done with the current buffer, so release it,
1904 			   unless it's the dinode buffer. Then back up to the
1905 			   previous pointer. */
1906 			if (mp_h) {
1907 				brelse(mp.mp_bh[mp_h]);
1908 				mp.mp_bh[mp_h] = NULL;
1909 			}
1910 			/* If we can't get any lower in height, we've stripped
1911 			   off all we can. Next step is to back up and start
1912 			   stripping the previous level of metadata. */
1913 			if (mp_h == 0) {
1914 				strip_h--;
1915 				memcpy(mp.mp_list, start_list, sizeof(start_list));
1916 				mp_h = strip_h;
1917 				state = DEALLOC_FILL_MP;
1918 				break;
1919 			}
1920 			mp.mp_list[mp_h] = 0;
1921 			mp_h--; /* search one metadata height down */
1922 			mp.mp_list[mp_h]++;
1923 			if (walk_done(sdp, &mp, mp_h, end_list, end_aligned))
1924 				break;
1925 			/* Here we've found a part of the metapath that is not
1926 			 * allocated. We need to search at that height for the
1927 			 * next non-null pointer. */
1928 			if (find_nonnull_ptr(sdp, &mp, mp_h, end_list, end_aligned)) {
1929 				state = DEALLOC_FILL_MP;
1930 				mp_h++;
1931 			}
1932 			/* No more non-null pointers at this height. Back up
1933 			   to the previous height and try again. */
1934 			break; /* loop around in the same state */
1935 
1936 		/* Fill the metapath with buffers to the given height. */
1937 		case DEALLOC_FILL_MP:
1938 			/* Fill the buffers out to the current height. */
1939 			ret = fillup_metapath(ip, &mp, mp_h);
1940 			if (ret < 0)
1941 				goto out;
1942 
1943 			/* On the first pass, issue read-ahead on metadata. */
1944 			if (mp.mp_aheight > 1 && strip_h == ip->i_height - 1) {
1945 				unsigned int height = mp.mp_aheight - 1;
1946 
1947 				/* No read-ahead for data blocks. */
1948 				if (mp.mp_aheight - 1 == strip_h)
1949 					height--;
1950 
1951 				for (; height >= mp.mp_aheight - ret; height--) {
1952 					metapointer_range(&mp, height,
1953 							  start_list, start_aligned,
1954 							  end_list, end_aligned,
1955 							  &start, &end);
1956 					gfs2_metapath_ra(ip->i_gl, start, end);
1957 				}
1958 			}
1959 
1960 			/* If buffers found for the entire strip height */
1961 			if (mp.mp_aheight - 1 == strip_h) {
1962 				state = DEALLOC_MP_FULL;
1963 				break;
1964 			}
1965 			if (mp.mp_aheight < ip->i_height) /* We have a partial height */
1966 				mp_h = mp.mp_aheight - 1;
1967 
1968 			/* If we find a non-null block pointer, crawl a bit
1969 			   higher up in the metapath and try again, otherwise
1970 			   we need to look lower for a new starting point. */
1971 			if (find_nonnull_ptr(sdp, &mp, mp_h, end_list, end_aligned))
1972 				mp_h++;
1973 			else
1974 				state = DEALLOC_MP_LOWER;
1975 			break;
1976 		}
1977 	}
1978 
1979 	if (btotal) {
1980 		if (current->journal_info == NULL) {
1981 			ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS +
1982 					       RES_QUOTA, 0);
1983 			if (ret)
1984 				goto out;
1985 			down_write(&ip->i_rw_mutex);
1986 		}
1987 		gfs2_statfs_change(sdp, 0, +btotal, 0);
1988 		gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
1989 				  ip->i_inode.i_gid);
1990 		inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode));
1991 		gfs2_trans_add_meta(ip->i_gl, dibh);
1992 		gfs2_dinode_out(ip, dibh->b_data);
1993 		up_write(&ip->i_rw_mutex);
1994 		gfs2_trans_end(sdp);
1995 	}
1996 
1997 out:
1998 	if (gfs2_holder_initialized(&rd_gh))
1999 		gfs2_glock_dq_uninit(&rd_gh);
2000 	if (current->journal_info) {
2001 		up_write(&ip->i_rw_mutex);
2002 		gfs2_trans_end(sdp);
2003 		cond_resched();
2004 	}
2005 	gfs2_quota_unhold(ip);
2006 out_metapath:
2007 	release_metapath(&mp);
2008 	return ret;
2009 }
2010 
2011 static int trunc_end(struct gfs2_inode *ip)
2012 {
2013 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2014 	struct buffer_head *dibh;
2015 	int error;
2016 
2017 	error = gfs2_trans_begin(sdp, RES_DINODE, 0);
2018 	if (error)
2019 		return error;
2020 
2021 	down_write(&ip->i_rw_mutex);
2022 
2023 	error = gfs2_meta_inode_buffer(ip, &dibh);
2024 	if (error)
2025 		goto out;
2026 
2027 	if (!i_size_read(&ip->i_inode)) {
2028 		ip->i_height = 0;
2029 		ip->i_goal = ip->i_no_addr;
2030 		gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
2031 		gfs2_ordered_del_inode(ip);
2032 	}
2033 	inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode));
2034 	ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
2035 
2036 	gfs2_trans_add_meta(ip->i_gl, dibh);
2037 	gfs2_dinode_out(ip, dibh->b_data);
2038 	brelse(dibh);
2039 
2040 out:
2041 	up_write(&ip->i_rw_mutex);
2042 	gfs2_trans_end(sdp);
2043 	return error;
2044 }
2045 
2046 /**
2047  * do_shrink - make a file smaller
2048  * @inode: the inode
2049  * @newsize: the size to make the file
2050  *
2051  * Called with an exclusive lock on @inode. The @size must
2052  * be equal to or smaller than the current inode size.
2053  *
2054  * Returns: errno
2055  */
2056 
2057 static int do_shrink(struct inode *inode, u64 newsize)
2058 {
2059 	struct gfs2_inode *ip = GFS2_I(inode);
2060 	int error;
2061 
2062 	error = trunc_start(inode, newsize);
2063 	if (error < 0)
2064 		return error;
2065 	if (gfs2_is_stuffed(ip))
2066 		return 0;
2067 
2068 	error = punch_hole(ip, newsize, 0);
2069 	if (error == 0)
2070 		error = trunc_end(ip);
2071 
2072 	return error;
2073 }
2074 
2075 /**
2076  * do_grow - Touch and update inode size
2077  * @inode: The inode
2078  * @size: The new size
2079  *
2080  * This function updates the timestamps on the inode and
2081  * may also increase the size of the inode. This function
2082  * must not be called with @size any smaller than the current
2083  * inode size.
2084  *
2085  * Although it is not strictly required to unstuff files here,
2086  * earlier versions of GFS2 have a bug in the stuffed file reading
2087  * code which will result in a buffer overrun if the size is larger
2088  * than the max stuffed file size. In order to prevent this from
2089  * occurring, such files are unstuffed, but in other cases we can
2090  * just update the inode size directly.
2091  *
2092  * Returns: 0 on success, or -ve on error
2093  */
2094 
2095 static int do_grow(struct inode *inode, u64 size)
2096 {
2097 	struct gfs2_inode *ip = GFS2_I(inode);
2098 	struct gfs2_sbd *sdp = GFS2_SB(inode);
2099 	struct gfs2_alloc_parms ap = { .target = 1, };
2100 	struct buffer_head *dibh;
2101 	int error;
2102 	int unstuff = 0;
2103 
2104 	if (gfs2_is_stuffed(ip) && size > gfs2_max_stuffed_size(ip)) {
2105 		error = gfs2_quota_lock_check(ip, &ap);
2106 		if (error)
2107 			return error;
2108 
2109 		error = gfs2_inplace_reserve(ip, &ap);
2110 		if (error)
2111 			goto do_grow_qunlock;
2112 		unstuff = 1;
2113 	}
2114 
2115 	if (!unstuff) {
2116 		error = gfs2_clear_beyond_eof(inode, size);
2117 		if (error)
2118 			goto do_grow_qunlock;
2119 	}
2120 
2121 	error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
2122 				 (unstuff &&
2123 				  gfs2_is_jdata(ip) ? RES_JDATA : 0) +
2124 				 (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
2125 				  0 : RES_QUOTA), 0);
2126 	if (error)
2127 		goto do_grow_release;
2128 
2129 	if (unstuff) {
2130 		error = gfs2_unstuff_dinode(ip);
2131 		if (error)
2132 			goto do_end_trans;
2133 	}
2134 
2135 	error = gfs2_meta_inode_buffer(ip, &dibh);
2136 	if (error)
2137 		goto do_end_trans;
2138 
2139 	truncate_setsize(inode, size);
2140 	inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode));
2141 	gfs2_trans_add_meta(ip->i_gl, dibh);
2142 	gfs2_dinode_out(ip, dibh->b_data);
2143 	brelse(dibh);
2144 
2145 do_end_trans:
2146 	gfs2_trans_end(sdp);
2147 do_grow_release:
2148 	if (unstuff) {
2149 		gfs2_inplace_release(ip);
2150 do_grow_qunlock:
2151 		gfs2_quota_unlock(ip);
2152 	}
2153 	return error;
2154 }
2155 
2156 /**
2157  * gfs2_setattr_size - make a file a given size
2158  * @inode: the inode
2159  * @newsize: the size to make the file
2160  *
2161  * The file size can grow, shrink, or stay the same size. This
2162  * is called holding i_rwsem and an exclusive glock on the inode
2163  * in question.
2164  *
2165  * Returns: errno
2166  */
2167 
2168 int gfs2_setattr_size(struct inode *inode, u64 newsize)
2169 {
2170 	struct gfs2_inode *ip = GFS2_I(inode);
2171 	int ret;
2172 
2173 	BUG_ON(!S_ISREG(inode->i_mode));
2174 
2175 	ret = inode_newsize_ok(inode, newsize);
2176 	if (ret)
2177 		return ret;
2178 
2179 	inode_dio_wait(inode);
2180 
2181 	ret = gfs2_qa_get(ip);
2182 	if (ret)
2183 		goto out;
2184 
2185 	if (newsize >= inode->i_size) {
2186 		ret = do_grow(inode, newsize);
2187 		goto out;
2188 	}
2189 
2190 	ret = do_shrink(inode, newsize);
2191 out:
2192 	gfs2_rs_delete(ip);
2193 	gfs2_qa_put(ip);
2194 	return ret;
2195 }
2196 
2197 int gfs2_truncatei_resume(struct gfs2_inode *ip)
2198 {
2199 	int error;
2200 	error = punch_hole(ip, i_size_read(&ip->i_inode), 0);
2201 	if (!error)
2202 		error = trunc_end(ip);
2203 	return error;
2204 }
2205 
2206 int gfs2_file_dealloc(struct gfs2_inode *ip)
2207 {
2208 	return punch_hole(ip, 0, 0);
2209 }
2210 
2211 /**
2212  * gfs2_free_journal_extents - Free cached journal bmap info
2213  * @jd: The journal
2214  *
2215  */
2216 
2217 void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
2218 {
2219 	struct gfs2_journal_extent *jext;
2220 
2221 	while(!list_empty(&jd->extent_list)) {
2222 		jext = list_first_entry(&jd->extent_list, struct gfs2_journal_extent, list);
2223 		list_del(&jext->list);
2224 		kfree(jext);
2225 	}
2226 }
2227 
2228 /**
2229  * gfs2_add_jextent - Add or merge a new extent to extent cache
2230  * @jd: The journal descriptor
2231  * @lblock: The logical block at start of new extent
2232  * @dblock: The physical block at start of new extent
2233  * @blocks: Size of extent in fs blocks
2234  *
2235  * Returns: 0 on success or -ENOMEM
2236  */
2237 
2238 static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
2239 {
2240 	struct gfs2_journal_extent *jext;
2241 
2242 	if (!list_empty(&jd->extent_list)) {
2243 		jext = list_last_entry(&jd->extent_list, struct gfs2_journal_extent, list);
2244 		if ((jext->dblock + jext->blocks) == dblock) {
2245 			jext->blocks += blocks;
2246 			return 0;
2247 		}
2248 	}
2249 
2250 	jext = kzalloc_obj(struct gfs2_journal_extent, GFP_NOFS);
2251 	if (jext == NULL)
2252 		return -ENOMEM;
2253 	jext->dblock = dblock;
2254 	jext->lblock = lblock;
2255 	jext->blocks = blocks;
2256 	list_add_tail(&jext->list, &jd->extent_list);
2257 	jd->nr_extents++;
2258 	return 0;
2259 }
2260 
2261 /**
2262  * gfs2_map_journal_extents - Cache journal bmap info
2263  * @sdp: The super block
2264  * @jd: The journal to map
2265  *
2266  * Create a reusable "extent" mapping from all logical
2267  * blocks to all physical blocks for the given journal.  This will save
2268  * us time when writing journal blocks.  Most journals will have only one
2269  * extent that maps all their logical blocks.  That's because gfs2.mkfs
2270  * arranges the journal blocks sequentially to maximize performance.
2271  * So the extent would map the first block for the entire file length.
2272  * However, gfs2_jadd can happen while file activity is happening, so
2273  * those journals may not be sequential.  Less likely is the case where
2274  * the users created their own journals by mounting the metafs and
2275  * laying it out.  But it's still possible.  These journals might have
2276  * several extents.
2277  *
2278  * Returns: 0 on success, or error on failure
2279  */
2280 
2281 int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
2282 {
2283 	u64 lblock = 0;
2284 	u64 lblock_stop;
2285 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
2286 	struct buffer_head bh;
2287 	unsigned int shift = sdp->sd_sb.sb_bsize_shift;
2288 	u64 size;
2289 	int rc;
2290 	ktime_t start, end;
2291 
2292 	start = ktime_get();
2293 	lblock_stop = i_size_read(jd->jd_inode) >> shift;
2294 	size = (lblock_stop - lblock) << shift;
2295 	jd->nr_extents = 0;
2296 	WARN_ON(!list_empty(&jd->extent_list));
2297 
2298 	do {
2299 		bh.b_state = 0;
2300 		bh.b_blocknr = 0;
2301 		bh.b_size = size;
2302 		rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
2303 		if (rc || !buffer_mapped(&bh))
2304 			goto fail;
2305 		rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
2306 		if (rc)
2307 			goto fail;
2308 		size -= bh.b_size;
2309 		lblock += (bh.b_size >> ip->i_inode.i_blkbits);
2310 	} while(size > 0);
2311 
2312 	end = ktime_get();
2313 	fs_info(sdp, "journal %d mapped with %u extents in %lldms\n", jd->jd_jid,
2314 		jd->nr_extents, ktime_ms_delta(end, start));
2315 	return 0;
2316 
2317 fail:
2318 	fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
2319 		rc, jd->jd_jid,
2320 		(unsigned long long)(i_size_read(jd->jd_inode) - size),
2321 		jd->nr_extents);
2322 	fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
2323 		rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
2324 		bh.b_state, (unsigned long long)bh.b_size);
2325 	gfs2_free_journal_extents(jd);
2326 	return rc;
2327 }
2328 
2329 /**
2330  * gfs2_write_alloc_required - figure out if a write will require an allocation
2331  * @ip: the file being written to
2332  * @offset: the offset to write to
2333  * @len: the number of bytes being written
2334  *
2335  * Returns: 1 if an alloc is required, 0 otherwise
2336  */
2337 
2338 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
2339 			      unsigned int len)
2340 {
2341 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2342 	struct buffer_head bh;
2343 	unsigned int shift;
2344 	u64 lblock, lblock_stop, size;
2345 	u64 end_of_file;
2346 
2347 	if (!len)
2348 		return 0;
2349 
2350 	if (gfs2_is_stuffed(ip)) {
2351 		if (offset + len > gfs2_max_stuffed_size(ip))
2352 			return 1;
2353 		return 0;
2354 	}
2355 
2356 	shift = sdp->sd_sb.sb_bsize_shift;
2357 	BUG_ON(gfs2_is_dir(ip));
2358 	end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
2359 	lblock = offset >> shift;
2360 	lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
2361 	if (lblock_stop > end_of_file && ip != GFS2_I(sdp->sd_rindex))
2362 		return 1;
2363 
2364 	size = (lblock_stop - lblock) << shift;
2365 	do {
2366 		bh.b_state = 0;
2367 		bh.b_size = size;
2368 		gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
2369 		if (!buffer_mapped(&bh))
2370 			return 1;
2371 		size -= bh.b_size;
2372 		lblock += (bh.b_size >> ip->i_inode.i_blkbits);
2373 	} while(size > 0);
2374 
2375 	return 0;
2376 }
2377 
2378 static int stuffed_zero_range(struct inode *inode, loff_t offset, loff_t length)
2379 {
2380 	struct gfs2_inode *ip = GFS2_I(inode);
2381 	struct buffer_head *dibh;
2382 	int error;
2383 
2384 	if (offset >= inode->i_size)
2385 		return 0;
2386 	if (offset + length > inode->i_size)
2387 		length = inode->i_size - offset;
2388 
2389 	error = gfs2_meta_inode_buffer(ip, &dibh);
2390 	if (error)
2391 		return error;
2392 	gfs2_trans_add_meta(ip->i_gl, dibh);
2393 	memset(dibh->b_data + sizeof(struct gfs2_dinode) + offset, 0,
2394 	       length);
2395 	brelse(dibh);
2396 	return 0;
2397 }
2398 
2399 static int gfs2_journaled_truncate_range(struct inode *inode, loff_t offset,
2400 					 loff_t length)
2401 {
2402 	struct gfs2_sbd *sdp = GFS2_SB(inode);
2403 	loff_t max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
2404 	int error;
2405 
2406 	while (length) {
2407 		struct gfs2_trans *tr;
2408 		loff_t chunk;
2409 		unsigned int offs;
2410 
2411 		chunk = length;
2412 		if (chunk > max_chunk)
2413 			chunk = max_chunk;
2414 
2415 		offs = offset & ~PAGE_MASK;
2416 		if (offs && chunk > PAGE_SIZE)
2417 			chunk = offs + ((chunk - offs) & PAGE_MASK);
2418 
2419 		truncate_pagecache_range(inode, offset, chunk);
2420 		offset += chunk;
2421 		length -= chunk;
2422 
2423 		tr = current->journal_info;
2424 		if (!test_bit(TR_TOUCHED, &tr->tr_flags))
2425 			continue;
2426 
2427 		gfs2_trans_end(sdp);
2428 		error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
2429 		if (error)
2430 			return error;
2431 	}
2432 	return 0;
2433 }
2434 
2435 int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length)
2436 {
2437 	struct inode *inode = file_inode(file);
2438 	struct gfs2_inode *ip = GFS2_I(inode);
2439 	struct gfs2_sbd *sdp = GFS2_SB(inode);
2440 	unsigned int blocksize = i_blocksize(inode);
2441 	loff_t start, end;
2442 	int error;
2443 
2444 	if (!gfs2_is_stuffed(ip)) {
2445 		unsigned int start_off, end_len;
2446 
2447 		start_off = offset & (blocksize - 1);
2448 		end_len = (offset + length) & (blocksize - 1);
2449 		if (start_off) {
2450 			unsigned int len = length;
2451 			if (length > blocksize - start_off)
2452 				len = blocksize - start_off;
2453 			error = gfs2_block_zero_range(inode, offset, len);
2454 			if (error)
2455 				goto out;
2456 			if (start_off + length < blocksize)
2457 				end_len = 0;
2458 		}
2459 		if (end_len) {
2460 			error = gfs2_block_zero_range(inode,
2461 				offset + length - end_len, end_len);
2462 			if (error)
2463 				goto out;
2464 		}
2465 	}
2466 
2467 	start = round_down(offset, blocksize);
2468 	end = round_up(offset + length, blocksize) - 1;
2469 	error = filemap_write_and_wait_range(inode->i_mapping, start, end);
2470 	if (error)
2471 		return error;
2472 
2473 	if (gfs2_is_jdata(ip))
2474 		error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_JDATA,
2475 					 GFS2_JTRUNC_REVOKES);
2476 	else
2477 		error = gfs2_trans_begin(sdp, RES_DINODE, 0);
2478 	if (error)
2479 		return error;
2480 
2481 	if (gfs2_is_stuffed(ip)) {
2482 		error = stuffed_zero_range(inode, offset, length);
2483 		if (error)
2484 			goto out;
2485 	}
2486 
2487 	if (gfs2_is_jdata(ip)) {
2488 		BUG_ON(!current->journal_info);
2489 		gfs2_journaled_truncate_range(inode, offset, length);
2490 	} else
2491 		truncate_pagecache_range(inode, offset, offset + length - 1);
2492 
2493 	file_update_time(file);
2494 	mark_inode_dirty(inode);
2495 
2496 	if (current->journal_info)
2497 		gfs2_trans_end(sdp);
2498 
2499 	if (!gfs2_is_stuffed(ip))
2500 		error = punch_hole(ip, offset, length);
2501 
2502 out:
2503 	if (current->journal_info)
2504 		gfs2_trans_end(sdp);
2505 	return error;
2506 }
2507 
2508 static ssize_t gfs2_writeback_range(struct iomap_writepage_ctx *wpc,
2509 		struct folio *folio, u64 offset, unsigned int len, u64 end_pos)
2510 {
2511 	if (WARN_ON_ONCE(gfs2_is_stuffed(GFS2_I(wpc->inode))))
2512 		return -EIO;
2513 
2514 	if (offset < wpc->iomap.offset ||
2515 	    offset >= wpc->iomap.offset + wpc->iomap.length) {
2516 		int ret;
2517 
2518 		memset(&wpc->iomap, 0, sizeof(wpc->iomap));
2519 		ret = gfs2_iomap_get(wpc->inode, offset, INT_MAX, &wpc->iomap);
2520 		if (ret)
2521 			return ret;
2522 	}
2523 
2524 	return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
2525 }
2526 
2527 const struct iomap_writeback_ops gfs2_writeback_ops = {
2528 	.writeback_range	= gfs2_writeback_range,
2529 	.writeback_submit	= iomap_ioend_writeback_submit,
2530 };
2531