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