blob: 8fd42ae026dd64b03be4b6d51d8538936fcd7f80 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/spinlock.h>
11#include <linux/completion.h>
12#include <linux/buffer_head.h>
Benjamin Marzinski64dd1532011-09-12 18:15:24 -050013#include <linux/blkdev.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050014#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050015#include <linux/crc32.h>
Bob Peterson39743202017-02-16 10:27:16 -050016#include <linux/iomap.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "bmap.h"
21#include "glock.h"
22#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "quota.h"
25#include "rgrp.h"
Steven Whitehouse45138992013-01-28 09:30:07 +000026#include "log.h"
Bob Peterson4c16c362011-02-23 16:11:33 -050027#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000029#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010031#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
33/* This doesn't need to be that large as max 64 bit pointers in a 4k
34 * block is 512, so __u16 is fine for that. It saves stack space to
35 * keep it small.
36 */
37struct metapath {
Steven Whitehousedbac6712008-01-29 09:12:55 +000038 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +000039 __u16 mp_list[GFS2_MAX_META_HEIGHT];
Bob Peterson5f8bd442016-10-28 14:29:29 -050040 int mp_fheight; /* find_metapath height */
41 int mp_aheight; /* actual height (lookup height) */
David Teiglandb3b94fa2006-01-16 16:50:04 +000042};
43
David Teiglandb3b94fa2006-01-16 16:50:04 +000044/**
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040045 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
46 * @ip: the inode
47 * @dibh: the dinode buffer
48 * @block: the block number that was allocated
Steven Whitehouseff8f33c2010-08-11 09:37:53 +010049 * @page: The (optional) page. This is looked up if @page is NULL
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040050 *
51 * Returns: errno
52 */
53
54static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
Steven Whitehousecd915492006-09-04 12:49:07 -040055 u64 block, struct page *page)
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040056{
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040057 struct inode *inode = &ip->i_inode;
58 struct buffer_head *bh;
59 int release = 0;
60
61 if (!page || page->index) {
Bob Peterson220cca22012-03-19 15:25:50 -040062 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040063 if (!page)
64 return -ENOMEM;
65 release = 1;
66 }
67
68 if (!PageUptodate(page)) {
69 void *kaddr = kmap(page);
Steven Whitehouse602c89d2010-03-25 14:32:43 +000070 u64 dsize = i_size_read(inode);
71
72 if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
73 dsize = dibh->b_size - sizeof(struct gfs2_dinode);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040074
Steven Whitehouse602c89d2010-03-25 14:32:43 +000075 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030076 memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040077 kunmap(page);
78
79 SetPageUptodate(page);
80 }
81
82 if (!page_has_buffers(page))
Fabian Frederick47a9a522016-08-02 12:05:27 -050083 create_empty_buffers(page, BIT(inode->i_blkbits),
84 BIT(BH_Uptodate));
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040085
86 bh = page_buffers(page);
87
88 if (!buffer_mapped(bh))
89 map_bh(bh, inode->i_sb, block);
90
91 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010092 if (!gfs2_is_jdata(ip))
93 mark_buffer_dirty(bh);
Steven Whitehousebf36a712007-10-17 08:35:19 +010094 if (!gfs2_is_writeback(ip))
Steven Whitehouse350a9b02012-12-14 12:36:02 +000095 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040096
97 if (release) {
98 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 put_page(page);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400100 }
101
102 return 0;
103}
104
105/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
107 * @ip: The GFS2 inode to unstuff
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100108 * @page: The (optional) page. This is looked up if the @page is NULL
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 *
110 * This routine unstuffs a dinode and returns it to a "normal" state such
111 * that the height can be grown in the traditional way.
112 *
113 * Returns: errno
114 */
115
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400116int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117{
118 struct buffer_head *bh, *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400119 struct gfs2_dinode *di;
Steven Whitehousecd915492006-09-04 12:49:07 -0400120 u64 block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000121 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 int error;
123
124 down_write(&ip->i_rw_mutex);
125
126 error = gfs2_meta_inode_buffer(ip, &dibh);
127 if (error)
128 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400129
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100130 if (i_size_read(&ip->i_inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 /* Get a free block, fill it with the stuffed data,
132 and write it out to disk */
133
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000134 unsigned int n = 1;
Bob Peterson6e87ed02011-11-18 10:58:32 -0500135 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100136 if (error)
137 goto out_brelse;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000138 if (isdir) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000139 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400140 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 if (error)
142 goto out_brelse;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400143 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 dibh, sizeof(struct gfs2_dinode));
145 brelse(bh);
146 } else {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400147 error = gfs2_unstuffer_page(ip, dibh, block, page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 if (error)
149 goto out_brelse;
150 }
151 }
152
153 /* Set up the pointer to the new block */
154
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000155 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400156 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
158
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100159 if (i_size_read(&ip->i_inode)) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400160 *(__be64 *)(di + 1) = cpu_to_be64(block);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000161 gfs2_add_inode_blocks(&ip->i_inode, 1);
162 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 }
164
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000165 ip->i_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400166 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167
Steven Whitehousea91ea692006-09-04 12:04:26 -0400168out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400170out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 return error;
173}
174
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175
176/**
177 * find_metapath - Find path through the metadata tree
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000178 * @sdp: The superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 * @mp: The metapath to return the result in
180 * @block: The disk block to look up
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000181 * @height: The pre-calculated height of the metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 *
183 * This routine returns a struct metapath structure that defines a path
184 * through the metadata of inode "ip" to get to block "block".
185 *
186 * Example:
187 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
188 * filesystem with a blocksize of 4096.
189 *
190 * find_metapath() would return a struct metapath structure set to:
191 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
192 * 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
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000236static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
237 struct metapath *mp, unsigned int height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 unsigned int i;
240
Bob Peterson5f8bd442016-10-28 14:29:29 -0500241 mp->mp_fheight = height;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000242 for (i = height; i--;)
Bob Peterson7eabb772008-01-28 11:24:35 -0600243 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244}
245
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500246static inline unsigned int metapath_branch_start(const struct metapath *mp)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000247{
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500248 if (mp->mp_list[0] == 0)
249 return 2;
250 return 1;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000251}
252
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253/**
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500254 * metaptr1 - Return the first possible metadata pointer in a metapath buffer
Bob Petersond552a2b2017-02-06 08:28:32 -0500255 * @height: The metadata height (0 = dinode)
256 * @mp: The metapath
257 */
258static 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/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267 * metapointer - Return pointer to start of metadata in a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 * @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
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000276static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277{
Bob Petersond552a2b2017-02-06 08:28:32 -0500278 __be64 *p = metaptr1(height, mp);
279 return p + mp->mp_list[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280}
281
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +0100282static void gfs2_metapath_ra(struct gfs2_glock *gl, __be64 *start, __be64 *end)
Steven Whitehouseb99b98d2011-09-21 11:05:16 +0100283{
Steven Whitehouseb99b98d2011-09-21 11:05:16 +0100284 const __be64 *t;
285
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +0100286 for (t = start; t < end; t++) {
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +0100287 struct buffer_head *rabh;
288
Steven Whitehouseb99b98d2011-09-21 11:05:16 +0100289 if (!*t)
290 continue;
291
292 rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
293 if (trylock_buffer(rabh)) {
294 if (!buffer_uptodate(rabh)) {
295 rabh->b_end_io = end_buffer_read_sync;
Coly Lie477b242017-07-21 07:48:22 -0500296 submit_bh(REQ_OP_READ,
297 REQ_RAHEAD | REQ_META | REQ_PRIO,
298 rabh);
Steven Whitehouseb99b98d2011-09-21 11:05:16 +0100299 continue;
300 }
301 unlock_buffer(rabh);
302 }
303 brelse(rabh);
304 }
305}
306
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100307static int __fillup_metapath(struct gfs2_inode *ip, struct metapath *mp,
308 unsigned int x, unsigned int h)
Bob Petersond552a2b2017-02-06 08:28:32 -0500309{
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100310 for (; x < h; x++) {
311 __be64 *ptr = metapointer(x, mp);
312 u64 dblock = be64_to_cpu(*ptr);
313 int ret;
Bob Petersond552a2b2017-02-06 08:28:32 -0500314
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100315 if (!dblock)
316 break;
317 ret = gfs2_meta_indirect_buffer(ip, x + 1, dblock, &mp->mp_bh[x + 1]);
318 if (ret)
319 return ret;
320 }
321 mp->mp_aheight = x + 1;
322 return 0;
Bob Petersond552a2b2017-02-06 08:28:32 -0500323}
324
325/**
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000326 * lookup_metapath - Walk the metadata tree to a specific point
327 * @ip: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 * @mp: The metapath
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000330 * Assumes that the inode's buffer has already been looked up and
331 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
332 * by find_metapath().
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000334 * If this function encounters part of the tree which has not been
335 * allocated, it returns the current height of the tree at the point
336 * at which it found the unallocated block. Blocks which are found are
337 * added to the mp->mp_bh[] list.
338 *
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100339 * Returns: error
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 */
341
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000342static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343{
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100344 return __fillup_metapath(ip, mp, 0, ip->i_height - 1);
Steven Whitehousedbac6712008-01-29 09:12:55 +0000345}
346
Bob Petersond552a2b2017-02-06 08:28:32 -0500347/**
348 * fillup_metapath - fill up buffers for the metadata path to a specific height
349 * @ip: The inode
350 * @mp: The metapath
351 * @h: The height to which it should be mapped
352 *
353 * Similar to lookup_metapath, but does lookups for a range of heights
354 *
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +0100355 * Returns: error or the number of buffers filled
Bob Petersond552a2b2017-02-06 08:28:32 -0500356 */
357
358static int fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, int h)
359{
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100360 unsigned int x = 0;
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +0100361 int ret;
Bob Petersond552a2b2017-02-06 08:28:32 -0500362
363 if (h) {
364 /* find the first buffer we need to look up. */
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100365 for (x = h - 1; x > 0; x--) {
366 if (mp->mp_bh[x])
367 break;
Bob Petersond552a2b2017-02-06 08:28:32 -0500368 }
369 }
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +0100370 ret = __fillup_metapath(ip, mp, x, h);
371 if (ret)
372 return ret;
373 return mp->mp_aheight - x - 1;
Bob Petersond552a2b2017-02-06 08:28:32 -0500374}
375
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000376static inline void release_metapath(struct metapath *mp)
Steven Whitehousedbac6712008-01-29 09:12:55 +0000377{
378 int i;
379
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000380 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
381 if (mp->mp_bh[i] == NULL)
382 break;
383 brelse(mp->mp_bh[i]);
384 }
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000385}
386
Steven Whitehouse30cbf182008-02-08 13:18:11 +0000387/**
388 * gfs2_extent_length - Returns length of an extent of blocks
389 * @start: Start of the buffer
390 * @len: Length of the buffer in bytes
391 * @ptr: Current position in the buffer
392 * @limit: Max extent length to return (0 = unlimited)
393 * @eob: Set to 1 if we hit "end of block"
394 *
395 * If the first block is zero (unallocated) it will return the number of
396 * unallocated blocks in the extent, otherwise it will return the number
397 * of contiguous blocks in the extent.
398 *
399 * Returns: The length of the extent (minimum of one block)
400 */
401
Bob Petersonb6507382014-08-06 09:08:36 -0400402static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, size_t limit, int *eob)
Steven Whitehouse30cbf182008-02-08 13:18:11 +0000403{
404 const __be64 *end = (start + len);
405 const __be64 *first = ptr;
406 u64 d = be64_to_cpu(*ptr);
407
408 *eob = 0;
409 do {
410 ptr++;
411 if (ptr >= end)
412 break;
413 if (limit && --limit == 0)
414 break;
415 if (d)
416 d++;
417 } while(be64_to_cpu(*ptr) == d);
418 if (ptr >= end)
419 *eob = 1;
420 return (ptr - first);
421}
422
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000423static inline void bmap_lock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400424{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400425 if (create)
426 down_write(&ip->i_rw_mutex);
427 else
428 down_read(&ip->i_rw_mutex);
429}
430
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000431static inline void bmap_unlock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400432{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 if (create)
434 up_write(&ip->i_rw_mutex);
435 else
436 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400437}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000439static inline __be64 *gfs2_indirect_init(struct metapath *mp,
440 struct gfs2_glock *gl, unsigned int i,
441 unsigned offset, u64 bn)
442{
443 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
444 ((i > 1) ? sizeof(struct gfs2_meta_header) :
445 sizeof(struct gfs2_dinode)));
446 BUG_ON(i < 1);
447 BUG_ON(mp->mp_bh[i] != NULL);
448 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000449 gfs2_trans_add_meta(gl, mp->mp_bh[i]);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000450 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
451 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
452 ptr += offset;
453 *ptr = cpu_to_be64(bn);
454 return ptr;
455}
456
457enum alloc_state {
458 ALLOC_DATA = 0,
459 ALLOC_GROW_DEPTH = 1,
460 ALLOC_GROW_HEIGHT = 2,
461 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
462};
463
Bob Petersond552a2b2017-02-06 08:28:32 -0500464static inline unsigned int hptrs(struct gfs2_sbd *sdp, const unsigned int hgt)
465{
466 if (hgt)
467 return sdp->sd_inptrs;
468 return sdp->sd_diptrs;
469}
470
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000471/**
472 * gfs2_bmap_alloc - Build a metadata tree of the requested height
473 * @inode: The GFS2 inode
474 * @lblock: The logical starting block of the extent
475 * @bh_map: This is used to return the mapping details
Bob Peterson5f8bd442016-10-28 14:29:29 -0500476 * @zero_new: True if newly allocated blocks should be zeroed
477 * @mp: The metapath, with proper height information calculated
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000478 * @maxlen: The max number of data blocks to alloc
Bob Peterson5f8bd442016-10-28 14:29:29 -0500479 * @dblock: Pointer to return the resulting new block
480 * @dblks: Pointer to return the number of blocks allocated
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000481 *
482 * In this routine we may have to alloc:
483 * i) Indirect blocks to grow the metadata tree height
484 * ii) Indirect blocks to fill in lower part of the metadata tree
485 * iii) Data blocks
486 *
487 * The function is in two parts. The first part works out the total
488 * number of blocks which we need. The second part does the actual
489 * allocation asking for an extent at a time (if enough contiguous free
490 * blocks are available, there will only be one request per bmap call)
491 * and uses the state machine to initialise the blocks in order.
492 *
493 * Returns: errno on error
494 */
495
Bob Peterson39743202017-02-16 10:27:16 -0500496static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
497 unsigned flags, struct metapath *mp)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000498{
499 struct gfs2_inode *ip = GFS2_I(inode);
500 struct gfs2_sbd *sdp = GFS2_SB(inode);
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500501 struct super_block *sb = sdp->sd_vfs;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000502 struct buffer_head *dibh = mp->mp_bh[0];
Bob Peterson5f8bd442016-10-28 14:29:29 -0500503 u64 bn;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500504 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
Bob Peterson39743202017-02-16 10:27:16 -0500505 unsigned dblks = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000506 unsigned ptrs_per_blk;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500507 const unsigned end_of_metadata = mp->mp_fheight - 1;
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500508 int ret;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000509 enum alloc_state state;
510 __be64 *ptr;
511 __be64 zero_bn = 0;
Bob Peterson39743202017-02-16 10:27:16 -0500512 size_t maxlen = iomap->length >> inode->i_blkbits;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000513
Bob Peterson5f8bd442016-10-28 14:29:29 -0500514 BUG_ON(mp->mp_aheight < 1);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000515 BUG_ON(dibh == NULL);
516
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000517 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000518
Bob Peterson5f8bd442016-10-28 14:29:29 -0500519 if (mp->mp_fheight == mp->mp_aheight) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000520 struct buffer_head *bh;
Bob Peterson39743202017-02-16 10:27:16 -0500521 int eob;
522
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000523 /* Bottom indirect block exists, find unalloced extent size */
524 ptr = metapointer(end_of_metadata, mp);
525 bh = mp->mp_bh[end_of_metadata];
Bob Peterson39743202017-02-16 10:27:16 -0500526 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr,
527 maxlen, &eob);
528 BUG_ON(dblks < 1);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000529 state = ALLOC_DATA;
530 } else {
531 /* Need to allocate indirect blocks */
Bob Peterson5f8bd442016-10-28 14:29:29 -0500532 ptrs_per_blk = mp->mp_fheight > 1 ? sdp->sd_inptrs :
533 sdp->sd_diptrs;
Bob Peterson39743202017-02-16 10:27:16 -0500534 dblks = min(maxlen, (size_t)(ptrs_per_blk -
535 mp->mp_list[end_of_metadata]));
Bob Peterson5f8bd442016-10-28 14:29:29 -0500536 if (mp->mp_fheight == ip->i_height) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000537 /* Writing into existing tree, extend tree down */
Bob Peterson5f8bd442016-10-28 14:29:29 -0500538 iblks = mp->mp_fheight - mp->mp_aheight;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000539 state = ALLOC_GROW_DEPTH;
540 } else {
541 /* Building up tree height */
542 state = ALLOC_GROW_HEIGHT;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500543 iblks = mp->mp_fheight - ip->i_height;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500544 branch_start = metapath_branch_start(mp);
Bob Peterson5f8bd442016-10-28 14:29:29 -0500545 iblks += (mp->mp_fheight - branch_start);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000546 }
547 }
548
549 /* start of the second part of the function (state machine) */
550
Bob Peterson39743202017-02-16 10:27:16 -0500551 blks = dblks + iblks;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500552 i = mp->mp_aheight;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000553 do {
Steven Whitehouse09010972009-05-20 10:48:47 +0100554 int error;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000555 n = blks - alloced;
Bob Peterson6e87ed02011-11-18 10:58:32 -0500556 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100557 if (error)
558 return error;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000559 alloced += n;
560 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
561 gfs2_trans_add_unrevoke(sdp, bn, n);
562 switch (state) {
563 /* Growing height of tree */
564 case ALLOC_GROW_HEIGHT:
565 if (i == 1) {
566 ptr = (__be64 *)(dibh->b_data +
567 sizeof(struct gfs2_dinode));
568 zero_bn = *ptr;
569 }
Bob Peterson5f8bd442016-10-28 14:29:29 -0500570 for (; i - 1 < mp->mp_fheight - ip->i_height && n > 0;
571 i++, n--)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000572 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
Bob Peterson5f8bd442016-10-28 14:29:29 -0500573 if (i - 1 == mp->mp_fheight - ip->i_height) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000574 i--;
575 gfs2_buffer_copy_tail(mp->mp_bh[i],
576 sizeof(struct gfs2_meta_header),
577 dibh, sizeof(struct gfs2_dinode));
578 gfs2_buffer_clear_tail(dibh,
579 sizeof(struct gfs2_dinode) +
580 sizeof(__be64));
581 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
582 sizeof(struct gfs2_meta_header));
583 *ptr = zero_bn;
584 state = ALLOC_GROW_DEPTH;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500585 for(i = branch_start; i < mp->mp_fheight; i++) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000586 if (mp->mp_bh[i] == NULL)
587 break;
588 brelse(mp->mp_bh[i]);
589 mp->mp_bh[i] = NULL;
590 }
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500591 i = branch_start;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000592 }
593 if (n == 0)
594 break;
595 /* Branching from existing tree */
596 case ALLOC_GROW_DEPTH:
Bob Peterson5f8bd442016-10-28 14:29:29 -0500597 if (i > 1 && i < mp->mp_fheight)
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000598 gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
Bob Peterson5f8bd442016-10-28 14:29:29 -0500599 for (; i < mp->mp_fheight && n > 0; i++, n--)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000600 gfs2_indirect_init(mp, ip->i_gl, i,
601 mp->mp_list[i-1], bn++);
Bob Peterson5f8bd442016-10-28 14:29:29 -0500602 if (i == mp->mp_fheight)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000603 state = ALLOC_DATA;
604 if (n == 0)
605 break;
606 /* Tree complete, adding data blocks */
607 case ALLOC_DATA:
Bob Peterson39743202017-02-16 10:27:16 -0500608 BUG_ON(n > dblks);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000609 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000610 gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
Bob Peterson39743202017-02-16 10:27:16 -0500611 dblks = n;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000612 ptr = metapointer(end_of_metadata, mp);
Bob Peterson39743202017-02-16 10:27:16 -0500613 iomap->addr = bn << inode->i_blkbits;
614 iomap->flags |= IOMAP_F_NEW;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000615 while (n-- > 0)
616 *ptr++ = cpu_to_be64(bn++);
Bob Peterson39743202017-02-16 10:27:16 -0500617 if (flags & IOMAP_ZERO) {
618 ret = sb_issue_zeroout(sb, iomap->addr >> inode->i_blkbits,
619 dblks, GFP_NOFS);
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500620 if (ret) {
621 fs_err(sdp,
622 "Failed to zero data buffers\n");
Bob Peterson39743202017-02-16 10:27:16 -0500623 flags &= ~IOMAP_ZERO;
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500624 }
625 }
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000626 break;
627 }
Bob Peterson39743202017-02-16 10:27:16 -0500628 } while (iomap->addr == IOMAP_NULL_ADDR);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000629
Bob Peterson39743202017-02-16 10:27:16 -0500630 iomap->length = (u64)dblks << inode->i_blkbits;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500631 ip->i_height = mp->mp_fheight;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000632 gfs2_add_inode_blocks(&ip->i_inode, alloced);
633 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000634 return 0;
635}
636
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500637/**
Bob Peterson39743202017-02-16 10:27:16 -0500638 * hole_size - figure out the size of a hole
639 * @inode: The inode
640 * @lblock: The logical starting block number
641 * @mp: The metapath
642 *
643 * Returns: The hole size in bytes
644 *
645 */
646static u64 hole_size(struct inode *inode, sector_t lblock, struct metapath *mp)
647{
648 struct gfs2_inode *ip = GFS2_I(inode);
649 struct gfs2_sbd *sdp = GFS2_SB(inode);
650 struct metapath mp_eof;
651 u64 factor = 1;
652 int hgt;
653 u64 holesz = 0;
654 const __be64 *first, *end, *ptr;
655 const struct buffer_head *bh;
656 u64 lblock_stop = (i_size_read(inode) - 1) >> inode->i_blkbits;
657 int zeroptrs;
658 bool done = false;
659
660 /* Get another metapath, to the very last byte */
661 find_metapath(sdp, lblock_stop, &mp_eof, ip->i_height);
662 for (hgt = ip->i_height - 1; hgt >= 0 && !done; hgt--) {
663 bh = mp->mp_bh[hgt];
664 if (bh) {
665 zeroptrs = 0;
666 first = metapointer(hgt, mp);
667 end = (const __be64 *)(bh->b_data + bh->b_size);
668
669 for (ptr = first; ptr < end; ptr++) {
670 if (*ptr) {
671 done = true;
672 break;
673 } else {
674 zeroptrs++;
675 }
676 }
677 } else {
678 zeroptrs = sdp->sd_inptrs;
679 }
680 if (factor * zeroptrs >= lblock_stop - lblock + 1) {
681 holesz = lblock_stop - lblock + 1;
682 break;
683 }
684 holesz += factor * zeroptrs;
685
686 factor *= sdp->sd_inptrs;
687 if (hgt && (mp->mp_list[hgt - 1] < mp_eof.mp_list[hgt - 1]))
688 (mp->mp_list[hgt - 1])++;
689 }
690 return holesz << inode->i_blkbits;
691}
692
693static void gfs2_stuffed_iomap(struct inode *inode, struct iomap *iomap)
694{
695 struct gfs2_inode *ip = GFS2_I(inode);
696
697 iomap->addr = (ip->i_no_addr << inode->i_blkbits) +
698 sizeof(struct gfs2_dinode);
699 iomap->offset = 0;
700 iomap->length = i_size_read(inode);
701 iomap->type = IOMAP_MAPPED;
702 iomap->flags = IOMAP_F_DATA_INLINE;
703}
704
705/**
706 * gfs2_iomap_begin - Map blocks from an inode to disk blocks
707 * @inode: The inode
708 * @pos: Starting position in bytes
709 * @length: Length to map, in bytes
710 * @flags: iomap flags
711 * @iomap: The iomap structure
712 *
713 * Returns: errno
714 */
715int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
716 unsigned flags, struct iomap *iomap)
717{
718 struct gfs2_inode *ip = GFS2_I(inode);
719 struct gfs2_sbd *sdp = GFS2_SB(inode);
720 struct metapath mp = { .mp_aheight = 1, };
721 unsigned int factor = sdp->sd_sb.sb_bsize;
722 const u64 *arr = sdp->sd_heightsize;
723 __be64 *ptr;
724 sector_t lblock;
725 sector_t lend;
726 int ret;
727 int eob;
728 unsigned int len;
729 struct buffer_head *bh;
730 u8 height;
731
732 trace_gfs2_iomap_start(ip, pos, length, flags);
733 if (!length) {
734 ret = -EINVAL;
735 goto out;
736 }
737
738 if ((flags & IOMAP_REPORT) && gfs2_is_stuffed(ip)) {
739 gfs2_stuffed_iomap(inode, iomap);
740 if (pos >= iomap->length)
741 return -ENOENT;
742 ret = 0;
743 goto out;
744 }
745
746 lblock = pos >> inode->i_blkbits;
747 lend = (pos + length + sdp->sd_sb.sb_bsize - 1) >> inode->i_blkbits;
748
749 iomap->offset = lblock << inode->i_blkbits;
750 iomap->addr = IOMAP_NULL_ADDR;
751 iomap->type = IOMAP_HOLE;
752 iomap->length = (u64)(lend - lblock) << inode->i_blkbits;
753 iomap->flags = IOMAP_F_MERGED;
754 bmap_lock(ip, 0);
755
756 /*
757 * Directory data blocks have a struct gfs2_meta_header header, so the
758 * remaining size is smaller than the filesystem block size. Logical
759 * block numbers for directories are in units of this remaining size!
760 */
761 if (gfs2_is_dir(ip)) {
762 factor = sdp->sd_jbsize;
763 arr = sdp->sd_jheightsize;
764 }
765
766 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
767 if (ret)
768 goto out_release;
769
770 height = ip->i_height;
771 while ((lblock + 1) * factor > arr[height])
772 height++;
773 find_metapath(sdp, lblock, &mp, height);
774 if (height > ip->i_height || gfs2_is_stuffed(ip))
775 goto do_alloc;
776
777 ret = lookup_metapath(ip, &mp);
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +0100778 if (ret)
Bob Peterson39743202017-02-16 10:27:16 -0500779 goto out_release;
780
781 if (mp.mp_aheight != ip->i_height)
782 goto do_alloc;
783
784 ptr = metapointer(ip->i_height - 1, &mp);
785 if (*ptr == 0)
786 goto do_alloc;
787
788 iomap->type = IOMAP_MAPPED;
789 iomap->addr = be64_to_cpu(*ptr) << inode->i_blkbits;
790
791 bh = mp.mp_bh[ip->i_height - 1];
792 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, lend - lblock, &eob);
793 if (eob)
794 iomap->flags |= IOMAP_F_BOUNDARY;
795 iomap->length = (u64)len << inode->i_blkbits;
796
797 ret = 0;
798
799out_release:
800 release_metapath(&mp);
801 bmap_unlock(ip, 0);
802out:
803 trace_gfs2_iomap_end(ip, iomap, ret);
804 return ret;
805
806do_alloc:
807 if (!(flags & IOMAP_WRITE)) {
808 if (pos >= i_size_read(inode)) {
809 ret = -ENOENT;
810 goto out_release;
811 }
812 ret = 0;
813 iomap->length = hole_size(inode, lblock, &mp);
814 goto out_release;
815 }
816
817 ret = gfs2_iomap_alloc(inode, iomap, flags, &mp);
818 goto out_release;
819}
820
821/**
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500822 * gfs2_block_map - Map a block from an inode to a disk block
823 * @inode: The inode
824 * @lblock: The logical block number
825 * @bh_map: The bh to be mapped
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000826 * @create: True if its ok to alloc blocks to satify the request
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500827 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000828 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
829 * read of metadata will be required before the next block can be
830 * mapped. Sets buffer_new() if new blocks were allocated.
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500831 *
832 * Returns: errno
833 */
834
Bob Petersone9e1ef22007-12-10 14:13:27 -0600835int gfs2_block_map(struct inode *inode, sector_t lblock,
836 struct buffer_head *bh_map, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400837{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500838 struct gfs2_inode *ip = GFS2_I(inode);
Bob Peterson39743202017-02-16 10:27:16 -0500839 struct iomap iomap;
840 int ret, flags = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000841
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500842 clear_buffer_mapped(bh_map);
843 clear_buffer_new(bh_map);
844 clear_buffer_boundary(bh_map);
Steven Whitehouse63997772009-06-12 08:49:20 +0100845 trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500846
Bob Peterson39743202017-02-16 10:27:16 -0500847 if (create)
848 flags |= IOMAP_WRITE;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500849 if (buffer_zeronew(bh_map))
Bob Peterson39743202017-02-16 10:27:16 -0500850 flags |= IOMAP_ZERO;
851 ret = gfs2_iomap_begin(inode, (loff_t)lblock << inode->i_blkbits,
852 bh_map->b_size, flags, &iomap);
853 if (ret) {
854 if (!create && ret == -ENOENT) {
855 /* Return unmapped buffer beyond the end of file. */
856 ret = 0;
857 }
858 goto out;
Bob Peterson5f8bd442016-10-28 14:29:29 -0500859 }
Bob Peterson39743202017-02-16 10:27:16 -0500860
861 if (iomap.length > bh_map->b_size) {
862 iomap.length = bh_map->b_size;
863 iomap.flags &= ~IOMAP_F_BOUNDARY;
864 }
865 if (iomap.addr != IOMAP_NULL_ADDR)
866 map_bh(bh_map, inode->i_sb, iomap.addr >> inode->i_blkbits);
867 bh_map->b_size = iomap.length;
868 if (iomap.flags & IOMAP_F_BOUNDARY)
869 set_buffer_boundary(bh_map);
870 if (iomap.flags & IOMAP_F_NEW)
871 set_buffer_new(bh_map);
872
873out:
874 trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
875 return ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400876}
877
Steven Whitehouse941e6d72008-01-28 08:47:38 +0000878/*
879 * Deprecated: do not use in new code
880 */
Steven Whitehousefd88de562006-05-05 16:59:11 -0400881int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
882{
Steven Whitehouse23591252006-10-13 17:25:45 -0400883 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400884 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400885 int create = *new;
886
887 BUG_ON(!extlen);
888 BUG_ON(!dblock);
889 BUG_ON(!new);
890
Fabian Frederick47a9a522016-08-02 12:05:27 -0500891 bh.b_size = BIT(inode->i_blkbits + (create ? 0 : 5));
Bob Petersone9e1ef22007-12-10 14:13:27 -0600892 ret = gfs2_block_map(inode, lblock, &bh, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400893 *extlen = bh.b_size >> inode->i_blkbits;
894 *dblock = bh.b_blocknr;
895 if (buffer_new(&bh))
896 *new = 1;
897 else
898 *new = 0;
899 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900}
901
902/**
Andreas Gruenbacherbdba0d52017-12-13 20:10:38 +0100903 * gfs2_block_zero_range - Deal with zeroing out data
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400904 *
905 * This is partly borrowed from ext3.
906 */
Andreas Gruenbacherbdba0d52017-12-13 20:10:38 +0100907static int gfs2_block_zero_range(struct inode *inode, loff_t from,
908 unsigned int length)
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400909{
Andreas Gruenbacherbdba0d52017-12-13 20:10:38 +0100910 struct address_space *mapping = inode->i_mapping;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400911 struct gfs2_inode *ip = GFS2_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300912 unsigned long index = from >> PAGE_SHIFT;
913 unsigned offset = from & (PAGE_SIZE-1);
Andreas Gruenbacherbdba0d52017-12-13 20:10:38 +0100914 unsigned blocksize, iblock, pos;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400915 struct buffer_head *bh;
916 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400917 int err;
918
Bob Peterson220cca22012-03-19 15:25:50 -0400919 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400920 if (!page)
921 return 0;
922
923 blocksize = inode->i_sb->s_blocksize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300924 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400925
926 if (!page_has_buffers(page))
927 create_empty_buffers(page, blocksize, 0);
928
929 /* Find the buffer that contains "offset" */
930 bh = page_buffers(page);
931 pos = blocksize;
932 while (offset >= pos) {
933 bh = bh->b_this_page;
934 iblock++;
935 pos += blocksize;
936 }
937
938 err = 0;
939
940 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600941 gfs2_block_map(inode, iblock, bh, 0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400942 /* unmapped? It's a hole - nothing to do */
943 if (!buffer_mapped(bh))
944 goto unlock;
945 }
946
947 /* Ok, it's mapped. Make sure it's up-to-date */
948 if (PageUptodate(page))
949 set_buffer_uptodate(bh);
950
951 if (!buffer_uptodate(bh)) {
952 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -0500953 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400954 wait_on_buffer(bh);
955 /* Uhhuh. Read error. Complain and punt. */
956 if (!buffer_uptodate(bh))
957 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -0400958 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400959 }
960
Steven Whitehousebf36a712007-10-17 08:35:19 +0100961 if (!gfs2_is_writeback(ip))
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000962 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400963
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800964 zero_user(page, offset, length);
Steven Whitehouse40bc9a22009-06-10 09:09:40 +0100965 mark_buffer_dirty(bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400966unlock:
967 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300968 put_page(page);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400969 return err;
970}
971
Fabian Frederickc62baf62014-05-14 18:32:31 +0200972#define GFS2_JTRUNC_REVOKES 8192
973
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000974/**
975 * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
976 * @inode: The inode being truncated
977 * @oldsize: The original (larger) size
978 * @newsize: The new smaller size
979 *
980 * With jdata files, we have to journal a revoke for each block which is
981 * truncated. As a result, we need to split this into separate transactions
982 * if the number of pages being truncated gets too large.
983 */
984
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000985static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
986{
987 struct gfs2_sbd *sdp = GFS2_SB(inode);
988 u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
989 u64 chunk;
990 int error;
991
992 while (oldsize != newsize) {
Andreas Gruenbachere7fdf002017-12-12 16:47:20 +0100993 struct gfs2_trans *tr;
994 unsigned int offs;
995
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000996 chunk = oldsize - newsize;
997 if (chunk > max_chunk)
998 chunk = max_chunk;
Andreas Gruenbachere7fdf002017-12-12 16:47:20 +0100999
1000 offs = oldsize & ~PAGE_MASK;
1001 if (offs && chunk > PAGE_SIZE)
1002 chunk = offs + ((chunk - offs) & PAGE_MASK);
1003
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001004 truncate_pagecache(inode, oldsize - chunk);
Steven Whitehousefa731fc2012-11-13 09:50:28 +00001005 oldsize -= chunk;
Andreas Gruenbachere7fdf002017-12-12 16:47:20 +01001006
1007 tr = current->journal_info;
1008 if (!test_bit(TR_TOUCHED, &tr->tr_flags))
1009 continue;
1010
Steven Whitehousefa731fc2012-11-13 09:50:28 +00001011 gfs2_trans_end(sdp);
1012 error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
1013 if (error)
1014 return error;
1015 }
1016
1017 return 0;
1018}
1019
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001020static int trunc_start(struct inode *inode, u64 newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021{
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001022 struct gfs2_inode *ip = GFS2_I(inode);
1023 struct gfs2_sbd *sdp = GFS2_SB(inode);
Andreas Gruenbacher80990f42017-12-12 16:42:01 +01001024 struct buffer_head *dibh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 int journaled = gfs2_is_jdata(ip);
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001026 u64 oldsize = inode->i_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 int error;
1028
Steven Whitehousefa731fc2012-11-13 09:50:28 +00001029 if (journaled)
1030 error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
1031 else
1032 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 if (error)
1034 return error;
1035
1036 error = gfs2_meta_inode_buffer(ip, &dibh);
1037 if (error)
1038 goto out;
1039
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001040 gfs2_trans_add_meta(ip->i_gl, dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001042 if (gfs2_is_stuffed(ip)) {
1043 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
1044 } else {
Andreas Gruenbacherbdba0d52017-12-13 20:10:38 +01001045 unsigned int blocksize = i_blocksize(inode);
1046 unsigned int offs = newsize & (blocksize - 1);
1047 if (offs) {
1048 error = gfs2_block_zero_range(inode, newsize,
1049 blocksize - offs);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001050 if (error)
Andreas Gruenbacher80990f42017-12-12 16:42:01 +01001051 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 }
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001053 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054 }
1055
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001056 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001057 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001058 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059
Steven Whitehousefa731fc2012-11-13 09:50:28 +00001060 if (journaled)
1061 error = gfs2_journaled_truncate(inode, oldsize, newsize);
1062 else
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001063 truncate_pagecache(inode, newsize);
Steven Whitehousefa731fc2012-11-13 09:50:28 +00001064
Steven Whitehousea91ea692006-09-04 12:04:26 -04001065out:
Andreas Gruenbacher80990f42017-12-12 16:42:01 +01001066 brelse(dibh);
1067 if (current->journal_info)
1068 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 return error;
1070}
1071
Bob Petersond552a2b2017-02-06 08:28:32 -05001072/**
1073 * sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein
1074 * @ip: inode
1075 * @rg_gh: holder of resource group glock
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001076 * @bh: buffer head to sweep
1077 * @start: starting point in bh
1078 * @end: end point in bh
1079 * @meta: true if bh points to metadata (rather than data)
Bob Petersond552a2b2017-02-06 08:28:32 -05001080 * @btotal: place to keep count of total blocks freed
Bob Petersond552a2b2017-02-06 08:28:32 -05001081 *
1082 * We sweep a metadata buffer (provided by the metapath) for blocks we need to
1083 * free, and free them all. However, we do it one rgrp at a time. If this
1084 * block has references to multiple rgrps, we break it into individual
1085 * transactions. This allows other processes to use the rgrps while we're
1086 * focused on a single one, for better concurrency / performance.
1087 * At every transaction boundary, we rewrite the inode into the journal.
1088 * That way the bitmaps are kept consistent with the inode and we can recover
1089 * if we're interrupted by power-outages.
1090 *
1091 * Returns: 0, or return code if an error occurred.
1092 * *btotal has the total number of blocks freed
1093 */
1094static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001095 struct buffer_head *bh, __be64 *start, __be64 *end,
1096 bool meta, u32 *btotal)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097{
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001098 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Petersond552a2b2017-02-06 08:28:32 -05001099 struct gfs2_rgrpd *rgd;
1100 struct gfs2_trans *tr;
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001101 __be64 *p;
Bob Petersond552a2b2017-02-06 08:28:32 -05001102 int blks_outside_rgrp;
1103 u64 bn, bstart, isize_blks;
1104 s64 blen; /* needs to be s64 or gfs2_add_inode_blocks breaks */
Bob Petersond552a2b2017-02-06 08:28:32 -05001105 int ret = 0;
1106 bool buf_in_tr = false; /* buffer was added to transaction */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107
Bob Petersond552a2b2017-02-06 08:28:32 -05001108more_rgrps:
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001109 rgd = NULL;
1110 if (gfs2_holder_initialized(rd_gh)) {
1111 rgd = gfs2_glock2rgrp(rd_gh->gh_gl);
1112 gfs2_assert_withdraw(sdp,
1113 gfs2_glock_is_locked_by_me(rd_gh->gh_gl));
1114 }
Bob Petersond552a2b2017-02-06 08:28:32 -05001115 blks_outside_rgrp = 0;
1116 bstart = 0;
1117 blen = 0;
Bob Petersond552a2b2017-02-06 08:28:32 -05001118
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001119 for (p = start; p < end; p++) {
Bob Petersond552a2b2017-02-06 08:28:32 -05001120 if (!*p)
1121 continue;
1122 bn = be64_to_cpu(*p);
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001123
1124 if (rgd) {
1125 if (!rgrp_contains_block(rgd, bn)) {
1126 blks_outside_rgrp++;
1127 continue;
1128 }
Bob Petersond552a2b2017-02-06 08:28:32 -05001129 } else {
Steven Whitehouse90bcab92017-12-22 13:13:07 +01001130 rgd = gfs2_blk2rgrpd(sdp, bn, true);
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001131 if (unlikely(!rgd)) {
1132 ret = -EIO;
1133 goto out;
1134 }
Bob Petersond552a2b2017-02-06 08:28:32 -05001135 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1136 0, rd_gh);
1137 if (ret)
1138 goto out;
1139
1140 /* Must be done with the rgrp glock held: */
1141 if (gfs2_rs_active(&ip->i_res) &&
1142 rgd == ip->i_res.rs_rbm.rgd)
1143 gfs2_rs_deltree(&ip->i_res);
1144 }
1145
Bob Petersond552a2b2017-02-06 08:28:32 -05001146 /* The size of our transactions will be unknown until we
1147 actually process all the metadata blocks that relate to
1148 the rgrp. So we estimate. We know it can't be more than
1149 the dinode's i_blocks and we don't want to exceed the
1150 journal flush threshold, sd_log_thresh2. */
1151 if (current->journal_info == NULL) {
1152 unsigned int jblocks_rqsted, revokes;
1153
1154 jblocks_rqsted = rgd->rd_length + RES_DINODE +
1155 RES_INDIRECT;
1156 isize_blks = gfs2_get_inode_blocks(&ip->i_inode);
1157 if (isize_blks > atomic_read(&sdp->sd_log_thresh2))
1158 jblocks_rqsted +=
1159 atomic_read(&sdp->sd_log_thresh2);
1160 else
1161 jblocks_rqsted += isize_blks;
1162 revokes = jblocks_rqsted;
1163 if (meta)
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001164 revokes += end - start;
Bob Petersond552a2b2017-02-06 08:28:32 -05001165 else if (ip->i_depth)
1166 revokes += sdp->sd_inptrs;
1167 ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes);
1168 if (ret)
1169 goto out_unlock;
1170 down_write(&ip->i_rw_mutex);
1171 }
1172 /* check if we will exceed the transaction blocks requested */
1173 tr = current->journal_info;
1174 if (tr->tr_num_buf_new + RES_STATFS +
1175 RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) {
1176 /* We set blks_outside_rgrp to ensure the loop will
1177 be repeated for the same rgrp, but with a new
1178 transaction. */
1179 blks_outside_rgrp++;
1180 /* This next part is tricky. If the buffer was added
1181 to the transaction, we've already set some block
1182 pointers to 0, so we better follow through and free
1183 them, or we will introduce corruption (so break).
1184 This may be impossible, or at least rare, but I
1185 decided to cover the case regardless.
1186
1187 If the buffer was not added to the transaction
1188 (this call), doing so would exceed our transaction
1189 size, so we need to end the transaction and start a
1190 new one (so goto). */
1191
1192 if (buf_in_tr)
1193 break;
1194 goto out_unlock;
1195 }
1196
1197 gfs2_trans_add_meta(ip->i_gl, bh);
1198 buf_in_tr = true;
1199 *p = 0;
1200 if (bstart + blen == bn) {
1201 blen++;
1202 continue;
1203 }
1204 if (bstart) {
1205 __gfs2_free_blocks(ip, bstart, (u32)blen, meta);
1206 (*btotal) += blen;
1207 gfs2_add_inode_blocks(&ip->i_inode, -blen);
1208 }
1209 bstart = bn;
1210 blen = 1;
1211 }
1212 if (bstart) {
1213 __gfs2_free_blocks(ip, bstart, (u32)blen, meta);
1214 (*btotal) += blen;
1215 gfs2_add_inode_blocks(&ip->i_inode, -blen);
1216 }
1217out_unlock:
1218 if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks
1219 outside the rgrp we just processed,
1220 do it all over again. */
1221 if (current->journal_info) {
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001222 struct buffer_head *dibh;
1223
1224 ret = gfs2_meta_inode_buffer(ip, &dibh);
1225 if (ret)
1226 goto out;
Bob Petersond552a2b2017-02-06 08:28:32 -05001227
1228 /* Every transaction boundary, we rewrite the dinode
1229 to keep its di_blocks current in case of failure. */
1230 ip->i_inode.i_mtime = ip->i_inode.i_ctime =
Stephen Rothwellb32c8c72017-05-08 15:59:34 -07001231 current_time(&ip->i_inode);
Bob Petersond552a2b2017-02-06 08:28:32 -05001232 gfs2_trans_add_meta(ip->i_gl, dibh);
1233 gfs2_dinode_out(ip, dibh->b_data);
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001234 brelse(dibh);
Bob Petersond552a2b2017-02-06 08:28:32 -05001235 up_write(&ip->i_rw_mutex);
1236 gfs2_trans_end(sdp);
1237 }
1238 gfs2_glock_dq_uninit(rd_gh);
1239 cond_resched();
1240 goto more_rgrps;
1241 }
1242out:
1243 return ret;
1244}
1245
1246/**
1247 * find_nonnull_ptr - find a non-null pointer given a metapath and height
1248 * assumes the metapath is valid (with buffers) out to height h
1249 * @mp: starting metapath
1250 * @h: desired height to search
1251 *
1252 * Returns: true if a non-null pointer was found in the metapath buffer
1253 * false if all remaining pointers are NULL in the buffer
1254 */
1255static bool find_nonnull_ptr(struct gfs2_sbd *sdp, struct metapath *mp,
1256 unsigned int h)
1257{
1258 __be64 *ptr;
1259 unsigned int ptrs = hptrs(sdp, h) - 1;
1260
1261 while (true) {
1262 ptr = metapointer(h, mp);
Bob Petersonc4a9d182017-08-30 09:26:09 -05001263 if (*ptr) { /* if we have a non-null pointer */
1264 /* Now zero the metapath after the current height. */
1265 h++;
1266 if (h < GFS2_MAX_META_HEIGHT)
1267 memset(&mp->mp_list[h], 0,
1268 (GFS2_MAX_META_HEIGHT - h) *
1269 sizeof(mp->mp_list[0]));
Bob Petersond552a2b2017-02-06 08:28:32 -05001270 return true;
Bob Petersonc4a9d182017-08-30 09:26:09 -05001271 }
Bob Petersond552a2b2017-02-06 08:28:32 -05001272
1273 if (mp->mp_list[h] < ptrs)
1274 mp->mp_list[h]++;
1275 else
1276 return false; /* no more pointers in this buffer */
1277 }
1278}
1279
1280enum dealloc_states {
1281 DEALLOC_MP_FULL = 0, /* Strip a metapath with all buffers read in */
1282 DEALLOC_MP_LOWER = 1, /* lower the metapath strip height */
1283 DEALLOC_FILL_MP = 2, /* Fill in the metapath to the given height. */
1284 DEALLOC_DONE = 3, /* process complete */
1285};
1286
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001287static bool mp_eq_to_hgt(struct metapath *mp, __u16 *list, unsigned int h)
Bob Petersonc4a9d182017-08-30 09:26:09 -05001288{
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001289 if (memcmp(mp->mp_list, list, h * sizeof(mp->mp_list[0])))
Bob Petersonc4a9d182017-08-30 09:26:09 -05001290 return false;
1291 return true;
1292}
1293
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001294static inline void
1295metapointer_range(struct metapath *mp, int height,
1296 __u16 *start_list, unsigned int start_aligned,
1297 __be64 **start, __be64 **end)
1298{
1299 struct buffer_head *bh = mp->mp_bh[height];
1300 __be64 *first;
1301
1302 first = metaptr1(height, mp);
1303 *start = first;
1304 if (mp_eq_to_hgt(mp, start_list, height)) {
1305 bool keep_start = height < start_aligned;
1306 *start = first + start_list[height] + keep_start;
1307 }
1308 *end = (__be64 *)(bh->b_data + bh->b_size);
1309}
1310
Bob Petersond552a2b2017-02-06 08:28:32 -05001311/**
1312 * trunc_dealloc - truncate a file down to a desired size
1313 * @ip: inode to truncate
1314 * @newsize: The desired size of the file
1315 *
1316 * This function truncates a file to newsize. It works from the
1317 * bottom up, and from the right to the left. In other words, it strips off
1318 * the highest layer (data) before stripping any of the metadata. Doing it
1319 * this way is best in case the operation is interrupted by power failure, etc.
1320 * The dinode is rewritten in every transaction to guarantee integrity.
1321 */
1322static int trunc_dealloc(struct gfs2_inode *ip, u64 newsize)
1323{
1324 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1325 struct metapath mp;
1326 struct buffer_head *dibh, *bh;
1327 struct gfs2_holder rd_gh;
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001328 unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift;
1329 u64 lblock = (newsize + (1 << bsize_shift) - 1) >> bsize_shift;
1330 __u16 start_list[GFS2_MAX_META_HEIGHT]; /* new beginning of truncation */
1331 unsigned int start_aligned;
Bob Petersond552a2b2017-02-06 08:28:32 -05001332 unsigned int strip_h = ip->i_height - 1;
1333 u32 btotal = 0;
1334 int ret, state;
1335 int mp_h; /* metapath buffers are read in to this height */
Bob Petersond552a2b2017-02-06 08:28:32 -05001336 u64 prev_bnr = 0;
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001337 __be64 *start, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338
Bob Petersond552a2b2017-02-06 08:28:32 -05001339 memset(&mp, 0, sizeof(mp));
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001340 find_metapath(sdp, lblock, &mp, ip->i_height);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001341
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001342 memcpy(start_list, mp.mp_list, sizeof(start_list));
1343
1344 /*
1345 * Set start_aligned to the metadata height up to which the truncate
1346 * point is aligned to the metadata tree (i.e., the truncate point is a
1347 * multiple of the granularity at the height above). This determines
1348 * at which heights an additional meta pointer needs to be preserved:
1349 * an additional meta pointer is needed at a given height if
1350 * height < start_aligned.
1351 */
1352 for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) {
1353 if (start_list[mp_h])
1354 break;
1355 }
1356 start_aligned = mp_h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357
Bob Petersond552a2b2017-02-06 08:28:32 -05001358 ret = gfs2_meta_inode_buffer(ip, &dibh);
1359 if (ret)
1360 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001361
Bob Petersond552a2b2017-02-06 08:28:32 -05001362 mp.mp_bh[0] = dibh;
1363 ret = lookup_metapath(ip, &mp);
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +01001364 if (ret)
1365 goto out_metapath;
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +01001366
1367 /* issue read-ahead on metadata */
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001368 for (mp_h = 0; mp_h < mp.mp_aheight - 1; mp_h++) {
1369 metapointer_range(&mp, mp_h, start_list, start_aligned,
1370 &start, &end);
1371 gfs2_metapath_ra(ip->i_gl, start, end);
1372 }
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +01001373
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +01001374 if (mp.mp_aheight == ip->i_height)
Bob Petersond552a2b2017-02-06 08:28:32 -05001375 state = DEALLOC_MP_FULL; /* We have a complete metapath */
1376 else
1377 state = DEALLOC_FILL_MP; /* deal with partial metapath */
1378
1379 ret = gfs2_rindex_update(sdp);
1380 if (ret)
1381 goto out_metapath;
1382
1383 ret = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1384 if (ret)
1385 goto out_metapath;
1386 gfs2_holder_mark_uninitialized(&rd_gh);
1387
1388 mp_h = strip_h;
1389
1390 while (state != DEALLOC_DONE) {
1391 switch (state) {
1392 /* Truncate a full metapath at the given strip height.
1393 * Note that strip_h == mp_h in order to be in this state. */
1394 case DEALLOC_MP_FULL:
Bob Petersond552a2b2017-02-06 08:28:32 -05001395 bh = mp.mp_bh[mp_h];
1396 gfs2_assert_withdraw(sdp, bh);
1397 if (gfs2_assert_withdraw(sdp,
1398 prev_bnr != bh->b_blocknr)) {
1399 printk(KERN_EMERG "GFS2: fsid=%s:inode %llu, "
1400 "block:%llu, i_h:%u, s_h:%u, mp_h:%u\n",
1401 sdp->sd_fsname,
1402 (unsigned long long)ip->i_no_addr,
1403 prev_bnr, ip->i_height, strip_h, mp_h);
1404 }
1405 prev_bnr = bh->b_blocknr;
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001406
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001407 if (gfs2_metatype_check(sdp, bh,
1408 (mp_h ? GFS2_METATYPE_IN :
1409 GFS2_METATYPE_DI))) {
1410 ret = -EIO;
1411 goto out;
1412 }
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001413
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001414 metapointer_range(&mp, mp_h, start_list, start_aligned,
1415 &start, &end);
1416 ret = sweep_bh_for_rgrps(ip, &rd_gh, mp.mp_bh[mp_h],
1417 start, end,
1418 mp_h != ip->i_height - 1,
1419 &btotal);
1420
Bob Petersond552a2b2017-02-06 08:28:32 -05001421 /* If we hit an error or just swept dinode buffer,
1422 just exit. */
1423 if (ret || !mp_h) {
1424 state = DEALLOC_DONE;
1425 break;
1426 }
1427 state = DEALLOC_MP_LOWER;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428 break;
Bob Petersond552a2b2017-02-06 08:28:32 -05001429
1430 /* lower the metapath strip height */
1431 case DEALLOC_MP_LOWER:
1432 /* We're done with the current buffer, so release it,
1433 unless it's the dinode buffer. Then back up to the
1434 previous pointer. */
1435 if (mp_h) {
1436 brelse(mp.mp_bh[mp_h]);
1437 mp.mp_bh[mp_h] = NULL;
1438 }
1439 /* If we can't get any lower in height, we've stripped
1440 off all we can. Next step is to back up and start
1441 stripping the previous level of metadata. */
1442 if (mp_h == 0) {
1443 strip_h--;
Andreas Gruenbachercb7f0902017-12-04 16:06:17 +01001444 memcpy(mp.mp_list, start_list, sizeof(start_list));
Bob Petersond552a2b2017-02-06 08:28:32 -05001445 mp_h = strip_h;
1446 state = DEALLOC_FILL_MP;
1447 break;
1448 }
1449 mp.mp_list[mp_h] = 0;
1450 mp_h--; /* search one metadata height down */
1451 if (mp.mp_list[mp_h] >= hptrs(sdp, mp_h) - 1)
1452 break; /* loop around in the same state */
1453 mp.mp_list[mp_h]++;
1454 /* Here we've found a part of the metapath that is not
1455 * allocated. We need to search at that height for the
1456 * next non-null pointer. */
1457 if (find_nonnull_ptr(sdp, &mp, mp_h)) {
1458 state = DEALLOC_FILL_MP;
1459 mp_h++;
1460 }
1461 /* No more non-null pointers at this height. Back up
1462 to the previous height and try again. */
1463 break; /* loop around in the same state */
1464
1465 /* Fill the metapath with buffers to the given height. */
1466 case DEALLOC_FILL_MP:
1467 /* Fill the buffers out to the current height. */
1468 ret = fillup_metapath(ip, &mp, mp_h);
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +01001469 if (ret < 0)
Bob Petersond552a2b2017-02-06 08:28:32 -05001470 goto out;
1471
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +01001472 /* issue read-ahead on metadata */
1473 if (mp.mp_aheight > 1) {
Andreas Gruenbacher5cf26b12017-12-11 12:49:55 +01001474 for (; ret > 1; ret--) {
1475 metapointer_range(&mp, mp.mp_aheight - ret,
1476 start_list, start_aligned,
1477 &start, &end);
1478 gfs2_metapath_ra(ip->i_gl, start, end);
1479 }
Andreas Gruenbacherc3ce5aa2017-12-08 21:11:39 +01001480 }
1481
Bob Petersond552a2b2017-02-06 08:28:32 -05001482 /* If buffers found for the entire strip height */
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +01001483 if (mp.mp_aheight - 1 == strip_h) {
Bob Petersond552a2b2017-02-06 08:28:32 -05001484 state = DEALLOC_MP_FULL;
1485 break;
1486 }
Andreas Gruenbachere8b43fe2017-12-08 17:01:57 +01001487 if (mp.mp_aheight < ip->i_height) /* We have a partial height */
1488 mp_h = mp.mp_aheight - 1;
Bob Petersond552a2b2017-02-06 08:28:32 -05001489
1490 /* If we find a non-null block pointer, crawl a bit
1491 higher up in the metapath and try again, otherwise
1492 we need to look lower for a new starting point. */
1493 if (find_nonnull_ptr(sdp, &mp, mp_h))
1494 mp_h++;
1495 else
1496 state = DEALLOC_MP_LOWER;
1497 break;
1498 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001499 }
1500
Bob Petersond552a2b2017-02-06 08:28:32 -05001501 if (btotal) {
1502 if (current->journal_info == NULL) {
1503 ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS +
1504 RES_QUOTA, 0);
1505 if (ret)
1506 goto out;
1507 down_write(&ip->i_rw_mutex);
1508 }
1509 gfs2_statfs_change(sdp, 0, +btotal, 0);
1510 gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
1511 ip->i_inode.i_gid);
Stephen Rothwellb32c8c72017-05-08 15:59:34 -07001512 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Bob Petersond552a2b2017-02-06 08:28:32 -05001513 gfs2_trans_add_meta(ip->i_gl, dibh);
1514 gfs2_dinode_out(ip, dibh->b_data);
1515 up_write(&ip->i_rw_mutex);
1516 gfs2_trans_end(sdp);
1517 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001518
Bob Petersond552a2b2017-02-06 08:28:32 -05001519out:
1520 if (gfs2_holder_initialized(&rd_gh))
1521 gfs2_glock_dq_uninit(&rd_gh);
1522 if (current->journal_info) {
1523 up_write(&ip->i_rw_mutex);
1524 gfs2_trans_end(sdp);
1525 cond_resched();
1526 }
1527 gfs2_quota_unhold(ip);
1528out_metapath:
1529 release_metapath(&mp);
1530 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531}
1532
1533static int trunc_end(struct gfs2_inode *ip)
1534{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001535 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001536 struct buffer_head *dibh;
1537 int error;
1538
1539 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1540 if (error)
1541 return error;
1542
1543 down_write(&ip->i_rw_mutex);
1544
1545 error = gfs2_meta_inode_buffer(ip, &dibh);
1546 if (error)
1547 goto out;
1548
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001549 if (!i_size_read(&ip->i_inode)) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001550 ip->i_height = 0;
Steven Whitehousece276b02008-02-06 09:25:45 +00001551 ip->i_goal = ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse45138992013-01-28 09:30:07 +00001553 gfs2_ordered_del_inode(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001554 }
Deepa Dinamani078cd822016-09-14 07:48:04 -07001555 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001556 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001557
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001558 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001559 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001560 brelse(dibh);
1561
Steven Whitehousea91ea692006-09-04 12:04:26 -04001562out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001563 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001564 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001565 return error;
1566}
1567
1568/**
1569 * do_shrink - make a file smaller
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001570 * @inode: the inode
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001571 * @newsize: the size to make the file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001572 *
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001573 * Called with an exclusive lock on @inode. The @size must
1574 * be equal to or smaller than the current inode size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001575 *
1576 * Returns: errno
1577 */
1578
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001579static int do_shrink(struct inode *inode, u64 newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001580{
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001581 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001582 int error;
1583
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001584 error = trunc_start(inode, newsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001585 if (error < 0)
1586 return error;
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001587 if (gfs2_is_stuffed(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001588 return 0;
1589
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001590 error = trunc_dealloc(ip, newsize);
1591 if (error == 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001592 error = trunc_end(ip);
1593
1594 return error;
1595}
1596
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001597void gfs2_trim_blocks(struct inode *inode)
Wendy Chenga13b8c52007-08-20 09:29:53 -04001598{
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001599 int ret;
1600
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001601 ret = do_shrink(inode, inode->i_size);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001602 WARN_ON(ret != 0);
1603}
1604
1605/**
1606 * do_grow - Touch and update inode size
1607 * @inode: The inode
1608 * @size: The new size
1609 *
1610 * This function updates the timestamps on the inode and
1611 * may also increase the size of the inode. This function
1612 * must not be called with @size any smaller than the current
1613 * inode size.
1614 *
1615 * Although it is not strictly required to unstuff files here,
1616 * earlier versions of GFS2 have a bug in the stuffed file reading
1617 * code which will result in a buffer overrun if the size is larger
1618 * than the max stuffed file size. In order to prevent this from
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001619 * occurring, such files are unstuffed, but in other cases we can
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001620 * just update the inode size directly.
1621 *
1622 * Returns: 0 on success, or -ve on error
1623 */
1624
1625static int do_grow(struct inode *inode, u64 size)
1626{
1627 struct gfs2_inode *ip = GFS2_I(inode);
1628 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001629 struct gfs2_alloc_parms ap = { .target = 1, };
Wendy Chenga13b8c52007-08-20 09:29:53 -04001630 struct buffer_head *dibh;
1631 int error;
Bob Peterson2f7ee352012-04-12 09:19:30 -04001632 int unstuff = 0;
Wendy Chenga13b8c52007-08-20 09:29:53 -04001633
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001634 if (gfs2_is_stuffed(ip) &&
1635 (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001636 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001637 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001638 return error;
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001639
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001640 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001641 if (error)
1642 goto do_grow_qunlock;
Bob Peterson2f7ee352012-04-12 09:19:30 -04001643 unstuff = 1;
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001644 }
1645
Bob Petersona01aedf2013-06-27 12:47:51 -04001646 error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
1647 (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
1648 0 : RES_QUOTA), 0);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001649 if (error)
1650 goto do_grow_release;
1651
Bob Peterson2f7ee352012-04-12 09:19:30 -04001652 if (unstuff) {
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001653 error = gfs2_unstuff_dinode(ip, NULL);
1654 if (error)
1655 goto do_end_trans;
1656 }
Wendy Chenga13b8c52007-08-20 09:29:53 -04001657
1658 error = gfs2_meta_inode_buffer(ip, &dibh);
1659 if (error)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001660 goto do_end_trans;
Wendy Chenga13b8c52007-08-20 09:29:53 -04001661
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001662 i_size_write(inode, size);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001663 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001664 gfs2_trans_add_meta(ip->i_gl, dibh);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001665 gfs2_dinode_out(ip, dibh->b_data);
1666 brelse(dibh);
1667
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001668do_end_trans:
Wendy Chenga13b8c52007-08-20 09:29:53 -04001669 gfs2_trans_end(sdp);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001670do_grow_release:
Bob Peterson2f7ee352012-04-12 09:19:30 -04001671 if (unstuff) {
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001672 gfs2_inplace_release(ip);
1673do_grow_qunlock:
1674 gfs2_quota_unlock(ip);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001675 }
Wendy Chenga13b8c52007-08-20 09:29:53 -04001676 return error;
1677}
1678
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679/**
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001680 * gfs2_setattr_size - make a file a given size
1681 * @inode: the inode
1682 * @newsize: the size to make the file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001683 *
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001684 * The file size can grow, shrink, or stay the same size. This
1685 * is called holding i_mutex and an exclusive glock on the inode
1686 * in question.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001687 *
1688 * Returns: errno
1689 */
1690
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001691int gfs2_setattr_size(struct inode *inode, u64 newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001692{
Steven Whitehouseaf5c2692013-09-27 12:49:33 +01001693 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001694 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001695
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001696 BUG_ON(!S_ISREG(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001697
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001698 ret = inode_newsize_ok(inode, newsize);
1699 if (ret)
1700 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701
Christoph Hellwig562c72a2011-06-24 14:29:45 -04001702 inode_dio_wait(inode);
1703
Bob Petersonb54e9a02015-10-26 10:40:28 -05001704 ret = gfs2_rsqa_alloc(ip);
Bob Petersond2b47cf2013-02-01 12:03:02 -05001705 if (ret)
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001706 goto out;
Bob Petersond2b47cf2013-02-01 12:03:02 -05001707
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001708 if (newsize >= inode->i_size) {
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001709 ret = do_grow(inode, newsize);
1710 goto out;
1711 }
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001712
Andreas Gruenbacher8b5860a2017-12-12 20:49:05 +01001713 ret = do_shrink(inode, newsize);
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001714out:
Bob Petersona097dc7e2015-07-16 08:28:04 -05001715 gfs2_rsqa_delete(ip, NULL);
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001716 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001717}
1718
1719int gfs2_truncatei_resume(struct gfs2_inode *ip)
1720{
1721 int error;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001722 error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001723 if (!error)
1724 error = trunc_end(ip);
1725 return error;
1726}
1727
1728int gfs2_file_dealloc(struct gfs2_inode *ip)
1729{
1730 return trunc_dealloc(ip, 0);
1731}
1732
1733/**
Steven Whitehouseb50f2272014-03-03 13:35:57 +00001734 * gfs2_free_journal_extents - Free cached journal bmap info
1735 * @jd: The journal
1736 *
1737 */
1738
1739void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
1740{
1741 struct gfs2_journal_extent *jext;
1742
1743 while(!list_empty(&jd->extent_list)) {
1744 jext = list_entry(jd->extent_list.next, struct gfs2_journal_extent, list);
1745 list_del(&jext->list);
1746 kfree(jext);
1747 }
1748}
1749
1750/**
1751 * gfs2_add_jextent - Add or merge a new extent to extent cache
1752 * @jd: The journal descriptor
1753 * @lblock: The logical block at start of new extent
Fabian Frederickc62baf62014-05-14 18:32:31 +02001754 * @dblock: The physical block at start of new extent
Steven Whitehouseb50f2272014-03-03 13:35:57 +00001755 * @blocks: Size of extent in fs blocks
1756 *
1757 * Returns: 0 on success or -ENOMEM
1758 */
1759
1760static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
1761{
1762 struct gfs2_journal_extent *jext;
1763
1764 if (!list_empty(&jd->extent_list)) {
1765 jext = list_entry(jd->extent_list.prev, struct gfs2_journal_extent, list);
1766 if ((jext->dblock + jext->blocks) == dblock) {
1767 jext->blocks += blocks;
1768 return 0;
1769 }
1770 }
1771
1772 jext = kzalloc(sizeof(struct gfs2_journal_extent), GFP_NOFS);
1773 if (jext == NULL)
1774 return -ENOMEM;
1775 jext->dblock = dblock;
1776 jext->lblock = lblock;
1777 jext->blocks = blocks;
1778 list_add_tail(&jext->list, &jd->extent_list);
1779 jd->nr_extents++;
1780 return 0;
1781}
1782
1783/**
1784 * gfs2_map_journal_extents - Cache journal bmap info
1785 * @sdp: The super block
1786 * @jd: The journal to map
1787 *
1788 * Create a reusable "extent" mapping from all logical
1789 * blocks to all physical blocks for the given journal. This will save
1790 * us time when writing journal blocks. Most journals will have only one
1791 * extent that maps all their logical blocks. That's because gfs2.mkfs
1792 * arranges the journal blocks sequentially to maximize performance.
1793 * So the extent would map the first block for the entire file length.
1794 * However, gfs2_jadd can happen while file activity is happening, so
1795 * those journals may not be sequential. Less likely is the case where
1796 * the users created their own journals by mounting the metafs and
1797 * laying it out. But it's still possible. These journals might have
1798 * several extents.
1799 *
1800 * Returns: 0 on success, or error on failure
1801 */
1802
1803int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
1804{
1805 u64 lblock = 0;
1806 u64 lblock_stop;
1807 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
1808 struct buffer_head bh;
1809 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1810 u64 size;
1811 int rc;
1812
1813 lblock_stop = i_size_read(jd->jd_inode) >> shift;
1814 size = (lblock_stop - lblock) << shift;
1815 jd->nr_extents = 0;
1816 WARN_ON(!list_empty(&jd->extent_list));
1817
1818 do {
1819 bh.b_state = 0;
1820 bh.b_blocknr = 0;
1821 bh.b_size = size;
1822 rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
1823 if (rc || !buffer_mapped(&bh))
1824 goto fail;
1825 rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
1826 if (rc)
1827 goto fail;
1828 size -= bh.b_size;
1829 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1830 } while(size > 0);
1831
1832 fs_info(sdp, "journal %d mapped with %u extents\n", jd->jd_jid,
1833 jd->nr_extents);
1834 return 0;
1835
1836fail:
1837 fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
1838 rc, jd->jd_jid,
1839 (unsigned long long)(i_size_read(jd->jd_inode) - size),
1840 jd->nr_extents);
1841 fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
1842 rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
1843 bh.b_state, (unsigned long long)bh.b_size);
1844 gfs2_free_journal_extents(jd);
1845 return rc;
1846}
1847
1848/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001849 * gfs2_write_alloc_required - figure out if a write will require an allocation
1850 * @ip: the file being written to
1851 * @offset: the offset to write to
1852 * @len: the number of bytes being written
David Teiglandb3b94fa2006-01-16 16:50:04 +00001853 *
Bob Peterson461cb412010-06-24 19:21:20 -04001854 * Returns: 1 if an alloc is required, 0 otherwise
David Teiglandb3b94fa2006-01-16 16:50:04 +00001855 */
1856
Steven Whitehousecd915492006-09-04 12:49:07 -04001857int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
Bob Peterson461cb412010-06-24 19:21:20 -04001858 unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001859{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001860 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001861 struct buffer_head bh;
1862 unsigned int shift;
1863 u64 lblock, lblock_stop, size;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001864 u64 end_of_file;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001865
David Teiglandb3b94fa2006-01-16 16:50:04 +00001866 if (!len)
1867 return 0;
1868
1869 if (gfs2_is_stuffed(ip)) {
1870 if (offset + len >
1871 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Bob Peterson461cb412010-06-24 19:21:20 -04001872 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001873 return 0;
1874 }
1875
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001876 shift = sdp->sd_sb.sb_bsize_shift;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001877 BUG_ON(gfs2_is_dir(ip));
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001878 end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001879 lblock = offset >> shift;
1880 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1881 if (lblock_stop > end_of_file)
Bob Peterson461cb412010-06-24 19:21:20 -04001882 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001883
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001884 size = (lblock_stop - lblock) << shift;
1885 do {
1886 bh.b_state = 0;
1887 bh.b_size = size;
1888 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1889 if (!buffer_mapped(&bh))
Bob Peterson461cb412010-06-24 19:21:20 -04001890 return 1;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001891 size -= bh.b_size;
1892 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1893 } while(size > 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001894
1895 return 0;
1896}
1897