blob: 8830e2903a349df89ee2a904ca17bed5d61a10e6 [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016
17#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000019#include "bmap.h"
20#include "glock.h"
21#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "quota.h"
24#include "rgrp.h"
Steven Whitehouse45138992013-01-28 09:30:07 +000025#include "log.h"
Bob Peterson4c16c362011-02-23 16:11:33 -050026#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000028#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010030#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
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 */
36struct metapath {
Steven Whitehousedbac6712008-01-29 09:12:55 +000037 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +000038 __u16 mp_list[GFS2_MAX_META_HEIGHT];
39};
40
David Teiglandb3b94fa2006-01-16 16:50:04 +000041/**
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040042 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
43 * @ip: the inode
44 * @dibh: the dinode buffer
45 * @block: the block number that was allocated
Steven Whitehouseff8f33c2010-08-11 09:37:53 +010046 * @page: The (optional) page. This is looked up if @page is NULL
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040047 *
48 * Returns: errno
49 */
50
51static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
Steven Whitehousecd915492006-09-04 12:49:07 -040052 u64 block, struct page *page)
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040053{
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040054 struct inode *inode = &ip->i_inode;
55 struct buffer_head *bh;
56 int release = 0;
57
58 if (!page || page->index) {
Bob Peterson220cca22012-03-19 15:25:50 -040059 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040060 if (!page)
61 return -ENOMEM;
62 release = 1;
63 }
64
65 if (!PageUptodate(page)) {
66 void *kaddr = kmap(page);
Steven Whitehouse602c89d2010-03-25 14:32:43 +000067 u64 dsize = i_size_read(inode);
68
69 if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
70 dsize = dibh->b_size - sizeof(struct gfs2_dinode);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040071
Steven Whitehouse602c89d2010-03-25 14:32:43 +000072 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030073 memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040074 kunmap(page);
75
76 SetPageUptodate(page);
77 }
78
79 if (!page_has_buffers(page))
Fabian Frederick47a9a522016-08-02 12:05:27 -050080 create_empty_buffers(page, BIT(inode->i_blkbits),
81 BIT(BH_Uptodate));
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040082
83 bh = page_buffers(page);
84
85 if (!buffer_mapped(bh))
86 map_bh(bh, inode->i_sb, block);
87
88 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010089 if (!gfs2_is_jdata(ip))
90 mark_buffer_dirty(bh);
Steven Whitehousebf36a712007-10-17 08:35:19 +010091 if (!gfs2_is_writeback(ip))
Steven Whitehouse350a9b02012-12-14 12:36:02 +000092 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040093
94 if (release) {
95 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 put_page(page);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040097 }
98
99 return 0;
100}
101
102/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
104 * @ip: The GFS2 inode to unstuff
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100105 * @page: The (optional) page. This is looked up if the @page is NULL
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106 *
107 * This routine unstuffs a dinode and returns it to a "normal" state such
108 * that the height can be grown in the traditional way.
109 *
110 * Returns: errno
111 */
112
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400113int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114{
115 struct buffer_head *bh, *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400116 struct gfs2_dinode *di;
Steven Whitehousecd915492006-09-04 12:49:07 -0400117 u64 block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000118 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 int error;
120
121 down_write(&ip->i_rw_mutex);
122
123 error = gfs2_meta_inode_buffer(ip, &dibh);
124 if (error)
125 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400126
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100127 if (i_size_read(&ip->i_inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 /* Get a free block, fill it with the stuffed data,
129 and write it out to disk */
130
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000131 unsigned int n = 1;
Bob Peterson6e87ed02011-11-18 10:58:32 -0500132 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100133 if (error)
134 goto out_brelse;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000135 if (isdir) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000136 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400137 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 if (error)
139 goto out_brelse;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400140 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 dibh, sizeof(struct gfs2_dinode));
142 brelse(bh);
143 } else {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400144 error = gfs2_unstuffer_page(ip, dibh, block, page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 if (error)
146 goto out_brelse;
147 }
148 }
149
150 /* Set up the pointer to the new block */
151
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000152 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400153 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
155
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100156 if (i_size_read(&ip->i_inode)) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400157 *(__be64 *)(di + 1) = cpu_to_be64(block);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000158 gfs2_add_inode_blocks(&ip->i_inode, 1);
159 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 }
161
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000162 ip->i_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400163 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164
Steven Whitehousea91ea692006-09-04 12:04:26 -0400165out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400167out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 return error;
170}
171
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
173/**
174 * find_metapath - Find path through the metadata tree
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000175 * @sdp: The superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 * @mp: The metapath to return the result in
177 * @block: The disk block to look up
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000178 * @height: The pre-calculated height of the metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 *
180 * This routine returns a struct metapath structure that defines a path
181 * through the metadata of inode "ip" to get to block "block".
182 *
183 * Example:
184 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
185 * filesystem with a blocksize of 4096.
186 *
187 * find_metapath() would return a struct metapath structure set to:
188 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
189 * and mp_list[2] = 165.
190 *
191 * That means that in order to get to the block containing the byte at
192 * offset 101342453, we would load the indirect block pointed to by pointer
193 * 0 in the dinode. We would then load the indirect block pointed to by
194 * pointer 48 in that indirect block. We would then load the data block
195 * pointed to by pointer 165 in that indirect block.
196 *
197 * ----------------------------------------
198 * | Dinode | |
199 * | | 4|
200 * | |0 1 2 3 4 5 9|
201 * | | 6|
202 * ----------------------------------------
203 * |
204 * |
205 * V
206 * ----------------------------------------
207 * | Indirect Block |
208 * | 5|
209 * | 4 4 4 4 4 5 5 1|
210 * |0 5 6 7 8 9 0 1 2|
211 * ----------------------------------------
212 * |
213 * |
214 * V
215 * ----------------------------------------
216 * | Indirect Block |
217 * | 1 1 1 1 1 5|
218 * | 6 6 6 6 6 1|
219 * |0 3 4 5 6 7 2|
220 * ----------------------------------------
221 * |
222 * |
223 * V
224 * ----------------------------------------
225 * | Data block containing offset |
226 * | 101342453 |
227 * | |
228 * | |
229 * ----------------------------------------
230 *
231 */
232
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000233static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
234 struct metapath *mp, unsigned int height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 unsigned int i;
237
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000238 for (i = height; i--;)
Bob Peterson7eabb772008-01-28 11:24:35 -0600239 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240
241}
242
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500243static inline unsigned int metapath_branch_start(const struct metapath *mp)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000244{
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500245 if (mp->mp_list[0] == 0)
246 return 2;
247 return 1;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000248}
249
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250/**
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500251 * metaptr1 - Return the first possible metadata pointer in a metapath buffer
Bob Petersond552a2b2017-02-06 08:28:32 -0500252 * @height: The metadata height (0 = dinode)
253 * @mp: The metapath
254 */
255static inline __be64 *metaptr1(unsigned int height, const struct metapath *mp)
256{
257 struct buffer_head *bh = mp->mp_bh[height];
258 if (height == 0)
259 return ((__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)));
260 return ((__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)));
261}
262
263/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264 * metapointer - Return pointer to start of metadata in a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265 * @height: The metadata height (0 = dinode)
266 * @mp: The metapath
267 *
268 * Return a pointer to the block number of the next height of the metadata
269 * tree given a buffer containing the pointer to the current height of the
270 * metadata tree.
271 */
272
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000273static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274{
Bob Petersond552a2b2017-02-06 08:28:32 -0500275 __be64 *p = metaptr1(height, mp);
276 return p + mp->mp_list[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277}
278
Steven Whitehouseb99b98d2011-09-21 11:05:16 +0100279static void gfs2_metapath_ra(struct gfs2_glock *gl,
280 const struct buffer_head *bh, const __be64 *pos)
281{
282 struct buffer_head *rabh;
283 const __be64 *endp = (const __be64 *)(bh->b_data + bh->b_size);
284 const __be64 *t;
285
286 for (t = pos; t < endp; t++) {
287 if (!*t)
288 continue;
289
290 rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
291 if (trylock_buffer(rabh)) {
292 if (!buffer_uptodate(rabh)) {
293 rabh->b_end_io = end_buffer_read_sync;
Coly Lie477b242017-07-21 07:48:22 -0500294 submit_bh(REQ_OP_READ,
295 REQ_RAHEAD | REQ_META | REQ_PRIO,
296 rabh);
Steven Whitehouseb99b98d2011-09-21 11:05:16 +0100297 continue;
298 }
299 unlock_buffer(rabh);
300 }
301 brelse(rabh);
302 }
303}
304
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305/**
Bob Petersond552a2b2017-02-06 08:28:32 -0500306 * lookup_mp_height - helper function for lookup_metapath
307 * @ip: the inode
308 * @mp: the metapath
309 * @h: the height which needs looking up
310 */
311static int lookup_mp_height(struct gfs2_inode *ip, struct metapath *mp, int h)
312{
313 __be64 *ptr = metapointer(h, mp);
314 u64 dblock = be64_to_cpu(*ptr);
315
316 if (!dblock)
317 return h + 1;
318
319 return gfs2_meta_indirect_buffer(ip, h + 1, dblock, &mp->mp_bh[h + 1]);
320}
321
322/**
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000323 * lookup_metapath - Walk the metadata tree to a specific point
324 * @ip: The inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 * @mp: The metapath
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000327 * Assumes that the inode's buffer has already been looked up and
328 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
329 * by find_metapath().
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000331 * If this function encounters part of the tree which has not been
332 * allocated, it returns the current height of the tree at the point
333 * at which it found the unallocated block. Blocks which are found are
334 * added to the mp->mp_bh[] list.
335 *
336 * Returns: error or height of metadata tree
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 */
338
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000339static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340{
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000341 unsigned int end_of_metadata = ip->i_height - 1;
342 unsigned int x;
Steven Whitehousee23159d2008-02-12 14:48:39 +0000343 int ret;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000344
345 for (x = 0; x < end_of_metadata; x++) {
Bob Petersond552a2b2017-02-06 08:28:32 -0500346 ret = lookup_mp_height(ip, mp, x);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000347 if (ret)
348 return ret;
349 }
350
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000351 return ip->i_height;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000352}
353
Bob Petersond552a2b2017-02-06 08:28:32 -0500354/**
355 * fillup_metapath - fill up buffers for the metadata path to a specific height
356 * @ip: The inode
357 * @mp: The metapath
358 * @h: The height to which it should be mapped
359 *
360 * Similar to lookup_metapath, but does lookups for a range of heights
361 *
362 * Returns: error or height of metadata tree
363 */
364
365static int fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, int h)
366{
367 unsigned int start_h = h - 1;
368 int ret;
369
370 if (h) {
371 /* find the first buffer we need to look up. */
372 while (start_h > 0 && mp->mp_bh[start_h] == NULL)
373 start_h--;
374 for (; start_h < h; start_h++) {
375 ret = lookup_mp_height(ip, mp, start_h);
376 if (ret)
377 return ret;
378 }
379 }
380 return ip->i_height;
381}
382
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000383static inline void release_metapath(struct metapath *mp)
Steven Whitehousedbac6712008-01-29 09:12:55 +0000384{
385 int i;
386
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000387 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
388 if (mp->mp_bh[i] == NULL)
389 break;
390 brelse(mp->mp_bh[i]);
391 }
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000392}
393
Steven Whitehouse30cbf182008-02-08 13:18:11 +0000394/**
395 * gfs2_extent_length - Returns length of an extent of blocks
396 * @start: Start of the buffer
397 * @len: Length of the buffer in bytes
398 * @ptr: Current position in the buffer
399 * @limit: Max extent length to return (0 = unlimited)
400 * @eob: Set to 1 if we hit "end of block"
401 *
402 * If the first block is zero (unallocated) it will return the number of
403 * unallocated blocks in the extent, otherwise it will return the number
404 * of contiguous blocks in the extent.
405 *
406 * Returns: The length of the extent (minimum of one block)
407 */
408
Bob Petersonb6507382014-08-06 09:08:36 -0400409static 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 +0000410{
411 const __be64 *end = (start + len);
412 const __be64 *first = ptr;
413 u64 d = be64_to_cpu(*ptr);
414
415 *eob = 0;
416 do {
417 ptr++;
418 if (ptr >= end)
419 break;
420 if (limit && --limit == 0)
421 break;
422 if (d)
423 d++;
424 } while(be64_to_cpu(*ptr) == d);
425 if (ptr >= end)
426 *eob = 1;
427 return (ptr - first);
428}
429
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000430static inline void bmap_lock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400431{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400432 if (create)
433 down_write(&ip->i_rw_mutex);
434 else
435 down_read(&ip->i_rw_mutex);
436}
437
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000438static inline void bmap_unlock(struct gfs2_inode *ip, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400439{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 if (create)
441 up_write(&ip->i_rw_mutex);
442 else
443 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400444}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000446static inline __be64 *gfs2_indirect_init(struct metapath *mp,
447 struct gfs2_glock *gl, unsigned int i,
448 unsigned offset, u64 bn)
449{
450 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
451 ((i > 1) ? sizeof(struct gfs2_meta_header) :
452 sizeof(struct gfs2_dinode)));
453 BUG_ON(i < 1);
454 BUG_ON(mp->mp_bh[i] != NULL);
455 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000456 gfs2_trans_add_meta(gl, mp->mp_bh[i]);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000457 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
458 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
459 ptr += offset;
460 *ptr = cpu_to_be64(bn);
461 return ptr;
462}
463
464enum alloc_state {
465 ALLOC_DATA = 0,
466 ALLOC_GROW_DEPTH = 1,
467 ALLOC_GROW_HEIGHT = 2,
468 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
469};
470
Bob Petersond552a2b2017-02-06 08:28:32 -0500471static inline unsigned int hptrs(struct gfs2_sbd *sdp, const unsigned int hgt)
472{
473 if (hgt)
474 return sdp->sd_inptrs;
475 return sdp->sd_diptrs;
476}
477
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000478/**
479 * gfs2_bmap_alloc - Build a metadata tree of the requested height
480 * @inode: The GFS2 inode
481 * @lblock: The logical starting block of the extent
482 * @bh_map: This is used to return the mapping details
483 * @mp: The metapath
484 * @sheight: The starting height (i.e. whats already mapped)
485 * @height: The height to build to
486 * @maxlen: The max number of data blocks to alloc
487 *
488 * In this routine we may have to alloc:
489 * i) Indirect blocks to grow the metadata tree height
490 * ii) Indirect blocks to fill in lower part of the metadata tree
491 * iii) Data blocks
492 *
493 * The function is in two parts. The first part works out the total
494 * number of blocks which we need. The second part does the actual
495 * allocation asking for an extent at a time (if enough contiguous free
496 * blocks are available, there will only be one request per bmap call)
497 * and uses the state machine to initialise the blocks in order.
498 *
499 * Returns: errno on error
500 */
501
502static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
503 struct buffer_head *bh_map, struct metapath *mp,
504 const unsigned int sheight,
505 const unsigned int height,
Bob Petersonb6507382014-08-06 09:08:36 -0400506 const size_t maxlen)
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000507{
508 struct gfs2_inode *ip = GFS2_I(inode);
509 struct gfs2_sbd *sdp = GFS2_SB(inode);
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500510 struct super_block *sb = sdp->sd_vfs;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000511 struct buffer_head *dibh = mp->mp_bh[0];
512 u64 bn, dblock = 0;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500513 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000514 unsigned dblks = 0;
515 unsigned ptrs_per_blk;
516 const unsigned end_of_metadata = height - 1;
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500517 int ret;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000518 int eob = 0;
519 enum alloc_state state;
520 __be64 *ptr;
521 __be64 zero_bn = 0;
522
523 BUG_ON(sheight < 1);
524 BUG_ON(dibh == NULL);
525
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000526 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000527
528 if (height == sheight) {
529 struct buffer_head *bh;
530 /* Bottom indirect block exists, find unalloced extent size */
531 ptr = metapointer(end_of_metadata, mp);
532 bh = mp->mp_bh[end_of_metadata];
533 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
534 &eob);
535 BUG_ON(dblks < 1);
536 state = ALLOC_DATA;
537 } else {
538 /* Need to allocate indirect blocks */
539 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
Bob Petersonb6507382014-08-06 09:08:36 -0400540 dblks = min(maxlen, (size_t)(ptrs_per_blk -
541 mp->mp_list[end_of_metadata]));
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000542 if (height == ip->i_height) {
543 /* Writing into existing tree, extend tree down */
544 iblks = height - sheight;
545 state = ALLOC_GROW_DEPTH;
546 } else {
547 /* Building up tree height */
548 state = ALLOC_GROW_HEIGHT;
549 iblks = height - ip->i_height;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500550 branch_start = metapath_branch_start(mp);
551 iblks += (height - branch_start);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000552 }
553 }
554
555 /* start of the second part of the function (state machine) */
556
557 blks = dblks + iblks;
558 i = sheight;
559 do {
Steven Whitehouse09010972009-05-20 10:48:47 +0100560 int error;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000561 n = blks - alloced;
Bob Peterson6e87ed02011-11-18 10:58:32 -0500562 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100563 if (error)
564 return error;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000565 alloced += n;
566 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
567 gfs2_trans_add_unrevoke(sdp, bn, n);
568 switch (state) {
569 /* Growing height of tree */
570 case ALLOC_GROW_HEIGHT:
571 if (i == 1) {
572 ptr = (__be64 *)(dibh->b_data +
573 sizeof(struct gfs2_dinode));
574 zero_bn = *ptr;
575 }
576 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
577 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
578 if (i - 1 == height - ip->i_height) {
579 i--;
580 gfs2_buffer_copy_tail(mp->mp_bh[i],
581 sizeof(struct gfs2_meta_header),
582 dibh, sizeof(struct gfs2_dinode));
583 gfs2_buffer_clear_tail(dibh,
584 sizeof(struct gfs2_dinode) +
585 sizeof(__be64));
586 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
587 sizeof(struct gfs2_meta_header));
588 *ptr = zero_bn;
589 state = ALLOC_GROW_DEPTH;
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500590 for(i = branch_start; i < height; i++) {
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000591 if (mp->mp_bh[i] == NULL)
592 break;
593 brelse(mp->mp_bh[i]);
594 mp->mp_bh[i] = NULL;
595 }
Benjamin Marzinski5af4e7a2008-06-24 12:53:38 -0500596 i = branch_start;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000597 }
598 if (n == 0)
599 break;
600 /* Branching from existing tree */
601 case ALLOC_GROW_DEPTH:
602 if (i > 1 && i < height)
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000603 gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000604 for (; i < height && n > 0; i++, n--)
605 gfs2_indirect_init(mp, ip->i_gl, i,
606 mp->mp_list[i-1], bn++);
607 if (i == height)
608 state = ALLOC_DATA;
609 if (n == 0)
610 break;
611 /* Tree complete, adding data blocks */
612 case ALLOC_DATA:
613 BUG_ON(n > dblks);
614 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000615 gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000616 dblks = n;
617 ptr = metapointer(end_of_metadata, mp);
618 dblock = bn;
619 while (n-- > 0)
620 *ptr++ = cpu_to_be64(bn++);
Benjamin Marzinski64dd1532011-09-12 18:15:24 -0500621 if (buffer_zeronew(bh_map)) {
622 ret = sb_issue_zeroout(sb, dblock, dblks,
623 GFP_NOFS);
624 if (ret) {
625 fs_err(sdp,
626 "Failed to zero data buffers\n");
627 clear_buffer_zeronew(bh_map);
628 }
629 }
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000630 break;
631 }
Steven Whitehouse07ccb7b2010-02-12 10:10:55 +0000632 } while ((state != ALLOC_DATA) || !dblock);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000633
634 ip->i_height = height;
635 gfs2_add_inode_blocks(&ip->i_inode, alloced);
636 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
637 map_bh(bh_map, inode->i_sb, dblock);
638 bh_map->b_size = dblks << inode->i_blkbits;
639 set_buffer_new(bh_map);
640 return 0;
641}
642
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500643/**
644 * gfs2_block_map - Map a block from an inode to a disk block
645 * @inode: The inode
646 * @lblock: The logical block number
647 * @bh_map: The bh to be mapped
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000648 * @create: True if its ok to alloc blocks to satify the request
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500649 *
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000650 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
651 * read of metadata will be required before the next block can be
652 * mapped. Sets buffer_new() if new blocks were allocated.
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500653 *
654 * Returns: errno
655 */
656
Bob Petersone9e1ef22007-12-10 14:13:27 -0600657int gfs2_block_map(struct inode *inode, sector_t lblock,
658 struct buffer_head *bh_map, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400659{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500660 struct gfs2_inode *ip = GFS2_I(inode);
661 struct gfs2_sbd *sdp = GFS2_SB(inode);
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500662 unsigned int factor = sdp->sd_sb.sb_bsize;
Bob Petersonb6507382014-08-06 09:08:36 -0400663 const size_t maxlen = bh_map->b_size >> inode->i_blkbits;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000664 const u64 *arr = sdp->sd_heightsize;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000665 __be64 *ptr;
666 u64 size;
667 struct metapath mp;
668 int ret;
669 int eob;
670 unsigned int len;
671 struct buffer_head *bh;
672 u8 height;
673
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500674 BUG_ON(maxlen == 0);
675
Bob Petersond552a2b2017-02-06 08:28:32 -0500676 memset(&mp, 0, sizeof(mp));
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000677 bmap_lock(ip, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500678 clear_buffer_mapped(bh_map);
679 clear_buffer_new(bh_map);
680 clear_buffer_boundary(bh_map);
Steven Whitehouse63997772009-06-12 08:49:20 +0100681 trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500682
683 /*
684 * Directory data blocks have a struct gfs2_meta_header header, so the
685 * remaining size is smaller than the filesystem block size. Logical
686 * block numbers for directories are in units of this remaining size!
687 */
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000688 if (gfs2_is_dir(ip)) {
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500689 factor = sdp->sd_jbsize;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000690 arr = sdp->sd_jheightsize;
691 }
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000692
693 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
694 if (ret)
695 goto out;
696
697 height = ip->i_height;
Andreas Gruenbacher20cdc192017-09-22 07:39:54 -0500698 size = (lblock + 1) * factor;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000699 while (size > arr[height])
700 height++;
701 find_metapath(sdp, lblock, &mp, height);
702 ret = 1;
703 if (height > ip->i_height || gfs2_is_stuffed(ip))
704 goto do_alloc;
705 ret = lookup_metapath(ip, &mp);
706 if (ret < 0)
707 goto out;
708 if (ret != ip->i_height)
709 goto do_alloc;
710 ptr = metapointer(ip->i_height - 1, &mp);
711 if (*ptr == 0)
712 goto do_alloc;
713 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
714 bh = mp.mp_bh[ip->i_height - 1];
715 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
716 bh_map->b_size = (len << inode->i_blkbits);
717 if (eob)
718 set_buffer_boundary(bh_map);
719 ret = 0;
720out:
Steven Whitehousedbac6712008-01-29 09:12:55 +0000721 release_metapath(&mp);
Steven Whitehouse63997772009-06-12 08:49:20 +0100722 trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000723 bmap_unlock(ip, create);
724 return ret;
725
726do_alloc:
727 /* All allocations are done here, firstly check create flag */
728 if (!create) {
729 BUG_ON(gfs2_is_stuffed(ip));
730 ret = 0;
731 goto out;
732 }
733
734 /* At this point ret is the tree depth of already allocated blocks */
735 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
736 goto out;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400737}
738
Steven Whitehouse941e6d72008-01-28 08:47:38 +0000739/*
740 * Deprecated: do not use in new code
741 */
Steven Whitehousefd88de562006-05-05 16:59:11 -0400742int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
743{
Steven Whitehouse23591252006-10-13 17:25:45 -0400744 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400745 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400746 int create = *new;
747
748 BUG_ON(!extlen);
749 BUG_ON(!dblock);
750 BUG_ON(!new);
751
Fabian Frederick47a9a522016-08-02 12:05:27 -0500752 bh.b_size = BIT(inode->i_blkbits + (create ? 0 : 5));
Bob Petersone9e1ef22007-12-10 14:13:27 -0600753 ret = gfs2_block_map(inode, lblock, &bh, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400754 *extlen = bh.b_size >> inode->i_blkbits;
755 *dblock = bh.b_blocknr;
756 if (buffer_new(&bh))
757 *new = 1;
758 else
759 *new = 0;
760 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761}
762
763/**
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400764 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
765 *
766 * This is partly borrowed from ext3.
767 */
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100768static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400769{
770 struct inode *inode = mapping->host;
771 struct gfs2_inode *ip = GFS2_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300772 unsigned long index = from >> PAGE_SHIFT;
773 unsigned offset = from & (PAGE_SIZE-1);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400774 unsigned blocksize, iblock, length, pos;
775 struct buffer_head *bh;
776 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400777 int err;
778
Bob Peterson220cca22012-03-19 15:25:50 -0400779 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400780 if (!page)
781 return 0;
782
783 blocksize = inode->i_sb->s_blocksize;
784 length = blocksize - (offset & (blocksize - 1));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300785 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400786
787 if (!page_has_buffers(page))
788 create_empty_buffers(page, blocksize, 0);
789
790 /* Find the buffer that contains "offset" */
791 bh = page_buffers(page);
792 pos = blocksize;
793 while (offset >= pos) {
794 bh = bh->b_this_page;
795 iblock++;
796 pos += blocksize;
797 }
798
799 err = 0;
800
801 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600802 gfs2_block_map(inode, iblock, bh, 0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400803 /* unmapped? It's a hole - nothing to do */
804 if (!buffer_mapped(bh))
805 goto unlock;
806 }
807
808 /* Ok, it's mapped. Make sure it's up-to-date */
809 if (PageUptodate(page))
810 set_buffer_uptodate(bh);
811
812 if (!buffer_uptodate(bh)) {
813 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -0500814 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400815 wait_on_buffer(bh);
816 /* Uhhuh. Read error. Complain and punt. */
817 if (!buffer_uptodate(bh))
818 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -0400819 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400820 }
821
Steven Whitehousebf36a712007-10-17 08:35:19 +0100822 if (!gfs2_is_writeback(ip))
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000823 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400824
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800825 zero_user(page, offset, length);
Steven Whitehouse40bc9a22009-06-10 09:09:40 +0100826 mark_buffer_dirty(bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400827unlock:
828 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300829 put_page(page);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400830 return err;
831}
832
Fabian Frederickc62baf62014-05-14 18:32:31 +0200833#define GFS2_JTRUNC_REVOKES 8192
834
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000835/**
836 * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
837 * @inode: The inode being truncated
838 * @oldsize: The original (larger) size
839 * @newsize: The new smaller size
840 *
841 * With jdata files, we have to journal a revoke for each block which is
842 * truncated. As a result, we need to split this into separate transactions
843 * if the number of pages being truncated gets too large.
844 */
845
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000846static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
847{
848 struct gfs2_sbd *sdp = GFS2_SB(inode);
849 u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
850 u64 chunk;
851 int error;
852
853 while (oldsize != newsize) {
854 chunk = oldsize - newsize;
855 if (chunk > max_chunk)
856 chunk = max_chunk;
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700857 truncate_pagecache(inode, oldsize - chunk);
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000858 oldsize -= chunk;
859 gfs2_trans_end(sdp);
860 error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
861 if (error)
862 return error;
863 }
864
865 return 0;
866}
867
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100868static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869{
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100870 struct gfs2_inode *ip = GFS2_I(inode);
871 struct gfs2_sbd *sdp = GFS2_SB(inode);
872 struct address_space *mapping = inode->i_mapping;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 struct buffer_head *dibh;
874 int journaled = gfs2_is_jdata(ip);
875 int error;
876
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000877 if (journaled)
878 error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
879 else
880 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 if (error)
882 return error;
883
884 error = gfs2_meta_inode_buffer(ip, &dibh);
885 if (error)
886 goto out;
887
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000888 gfs2_trans_add_meta(ip->i_gl, dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100890 if (gfs2_is_stuffed(ip)) {
891 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
892 } else {
893 if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
894 error = gfs2_block_truncate_page(mapping, newsize);
895 if (error)
896 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 }
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100898 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 }
900
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100901 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700902 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100903 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000905 if (journaled)
906 error = gfs2_journaled_truncate(inode, oldsize, newsize);
907 else
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700908 truncate_pagecache(inode, newsize);
Steven Whitehousefa731fc2012-11-13 09:50:28 +0000909
910 if (error) {
911 brelse(dibh);
912 return error;
913 }
914
Steven Whitehouseff8f33c2010-08-11 09:37:53 +0100915out_brelse:
916 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400917out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 return error;
920}
921
Bob Petersond552a2b2017-02-06 08:28:32 -0500922/**
923 * sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein
924 * @ip: inode
925 * @rg_gh: holder of resource group glock
926 * @mp: current metapath fully populated with buffers
927 * @btotal: place to keep count of total blocks freed
928 * @hgt: height we're processing
929 * @first: true if this is the first call to this function for this height
930 *
931 * We sweep a metadata buffer (provided by the metapath) for blocks we need to
932 * free, and free them all. However, we do it one rgrp at a time. If this
933 * block has references to multiple rgrps, we break it into individual
934 * transactions. This allows other processes to use the rgrps while we're
935 * focused on a single one, for better concurrency / performance.
936 * At every transaction boundary, we rewrite the inode into the journal.
937 * That way the bitmaps are kept consistent with the inode and we can recover
938 * if we're interrupted by power-outages.
939 *
940 * Returns: 0, or return code if an error occurred.
941 * *btotal has the total number of blocks freed
942 */
943static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,
944 const struct metapath *mp, u32 *btotal, int hgt,
945 bool preserve1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946{
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000947 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Petersond552a2b2017-02-06 08:28:32 -0500948 struct gfs2_rgrpd *rgd;
949 struct gfs2_trans *tr;
950 struct buffer_head *bh = mp->mp_bh[hgt];
951 __be64 *top, *bottom, *p;
952 int blks_outside_rgrp;
953 u64 bn, bstart, isize_blks;
954 s64 blen; /* needs to be s64 or gfs2_add_inode_blocks breaks */
955 int meta = ((hgt != ip->i_height - 1) ? 1 : 0);
956 int ret = 0;
957 bool buf_in_tr = false; /* buffer was added to transaction */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958
Bob Petersond552a2b2017-02-06 08:28:32 -0500959 if (gfs2_metatype_check(sdp, bh,
960 (hgt ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)))
961 return -EIO;
962
963more_rgrps:
964 blks_outside_rgrp = 0;
965 bstart = 0;
966 blen = 0;
967 top = metapointer(hgt, mp); /* first ptr from metapath */
968 /* If we're keeping some data at the truncation point, we've got to
969 preserve the metadata tree by adding 1 to the starting metapath. */
970 if (preserve1)
971 top++;
972
973 bottom = (__be64 *)(bh->b_data + bh->b_size);
974
975 for (p = top; p < bottom; p++) {
976 if (!*p)
977 continue;
978 bn = be64_to_cpu(*p);
979 if (gfs2_holder_initialized(rd_gh)) {
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500980 rgd = gfs2_glock2rgrp(rd_gh->gh_gl);
Bob Petersond552a2b2017-02-06 08:28:32 -0500981 gfs2_assert_withdraw(sdp,
982 gfs2_glock_is_locked_by_me(rd_gh->gh_gl));
983 } else {
984 rgd = gfs2_blk2rgrpd(sdp, bn, false);
985 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
986 0, rd_gh);
987 if (ret)
988 goto out;
989
990 /* Must be done with the rgrp glock held: */
991 if (gfs2_rs_active(&ip->i_res) &&
992 rgd == ip->i_res.rs_rbm.rgd)
993 gfs2_rs_deltree(&ip->i_res);
994 }
995
996 if (!rgrp_contains_block(rgd, bn)) {
997 blks_outside_rgrp++;
998 continue;
999 }
1000
1001 /* The size of our transactions will be unknown until we
1002 actually process all the metadata blocks that relate to
1003 the rgrp. So we estimate. We know it can't be more than
1004 the dinode's i_blocks and we don't want to exceed the
1005 journal flush threshold, sd_log_thresh2. */
1006 if (current->journal_info == NULL) {
1007 unsigned int jblocks_rqsted, revokes;
1008
1009 jblocks_rqsted = rgd->rd_length + RES_DINODE +
1010 RES_INDIRECT;
1011 isize_blks = gfs2_get_inode_blocks(&ip->i_inode);
1012 if (isize_blks > atomic_read(&sdp->sd_log_thresh2))
1013 jblocks_rqsted +=
1014 atomic_read(&sdp->sd_log_thresh2);
1015 else
1016 jblocks_rqsted += isize_blks;
1017 revokes = jblocks_rqsted;
1018 if (meta)
1019 revokes += hptrs(sdp, hgt);
1020 else if (ip->i_depth)
1021 revokes += sdp->sd_inptrs;
1022 ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes);
1023 if (ret)
1024 goto out_unlock;
1025 down_write(&ip->i_rw_mutex);
1026 }
1027 /* check if we will exceed the transaction blocks requested */
1028 tr = current->journal_info;
1029 if (tr->tr_num_buf_new + RES_STATFS +
1030 RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) {
1031 /* We set blks_outside_rgrp to ensure the loop will
1032 be repeated for the same rgrp, but with a new
1033 transaction. */
1034 blks_outside_rgrp++;
1035 /* This next part is tricky. If the buffer was added
1036 to the transaction, we've already set some block
1037 pointers to 0, so we better follow through and free
1038 them, or we will introduce corruption (so break).
1039 This may be impossible, or at least rare, but I
1040 decided to cover the case regardless.
1041
1042 If the buffer was not added to the transaction
1043 (this call), doing so would exceed our transaction
1044 size, so we need to end the transaction and start a
1045 new one (so goto). */
1046
1047 if (buf_in_tr)
1048 break;
1049 goto out_unlock;
1050 }
1051
1052 gfs2_trans_add_meta(ip->i_gl, bh);
1053 buf_in_tr = true;
1054 *p = 0;
1055 if (bstart + blen == bn) {
1056 blen++;
1057 continue;
1058 }
1059 if (bstart) {
1060 __gfs2_free_blocks(ip, bstart, (u32)blen, meta);
1061 (*btotal) += blen;
1062 gfs2_add_inode_blocks(&ip->i_inode, -blen);
1063 }
1064 bstart = bn;
1065 blen = 1;
1066 }
1067 if (bstart) {
1068 __gfs2_free_blocks(ip, bstart, (u32)blen, meta);
1069 (*btotal) += blen;
1070 gfs2_add_inode_blocks(&ip->i_inode, -blen);
1071 }
1072out_unlock:
1073 if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks
1074 outside the rgrp we just processed,
1075 do it all over again. */
1076 if (current->journal_info) {
1077 struct buffer_head *dibh = mp->mp_bh[0];
1078
1079 /* Every transaction boundary, we rewrite the dinode
1080 to keep its di_blocks current in case of failure. */
1081 ip->i_inode.i_mtime = ip->i_inode.i_ctime =
Stephen Rothwellb32c8c72017-05-08 15:59:34 -07001082 current_time(&ip->i_inode);
Bob Petersond552a2b2017-02-06 08:28:32 -05001083 gfs2_trans_add_meta(ip->i_gl, dibh);
1084 gfs2_dinode_out(ip, dibh->b_data);
1085 up_write(&ip->i_rw_mutex);
1086 gfs2_trans_end(sdp);
1087 }
1088 gfs2_glock_dq_uninit(rd_gh);
1089 cond_resched();
1090 goto more_rgrps;
1091 }
1092out:
1093 return ret;
1094}
1095
1096/**
1097 * find_nonnull_ptr - find a non-null pointer given a metapath and height
1098 * assumes the metapath is valid (with buffers) out to height h
1099 * @mp: starting metapath
1100 * @h: desired height to search
1101 *
1102 * Returns: true if a non-null pointer was found in the metapath buffer
1103 * false if all remaining pointers are NULL in the buffer
1104 */
1105static bool find_nonnull_ptr(struct gfs2_sbd *sdp, struct metapath *mp,
1106 unsigned int h)
1107{
1108 __be64 *ptr;
1109 unsigned int ptrs = hptrs(sdp, h) - 1;
1110
1111 while (true) {
1112 ptr = metapointer(h, mp);
Bob Petersonc4a9d182017-08-30 09:26:09 -05001113 if (*ptr) { /* if we have a non-null pointer */
1114 /* Now zero the metapath after the current height. */
1115 h++;
1116 if (h < GFS2_MAX_META_HEIGHT)
1117 memset(&mp->mp_list[h], 0,
1118 (GFS2_MAX_META_HEIGHT - h) *
1119 sizeof(mp->mp_list[0]));
Bob Petersond552a2b2017-02-06 08:28:32 -05001120 return true;
Bob Petersonc4a9d182017-08-30 09:26:09 -05001121 }
Bob Petersond552a2b2017-02-06 08:28:32 -05001122
1123 if (mp->mp_list[h] < ptrs)
1124 mp->mp_list[h]++;
1125 else
1126 return false; /* no more pointers in this buffer */
1127 }
1128}
1129
1130enum dealloc_states {
1131 DEALLOC_MP_FULL = 0, /* Strip a metapath with all buffers read in */
1132 DEALLOC_MP_LOWER = 1, /* lower the metapath strip height */
1133 DEALLOC_FILL_MP = 2, /* Fill in the metapath to the given height. */
1134 DEALLOC_DONE = 3, /* process complete */
1135};
1136
Bob Petersonc4a9d182017-08-30 09:26:09 -05001137static bool mp_eq_to_hgt(struct metapath *mp, __u16 *nbof, unsigned int h)
1138{
1139 if (memcmp(mp->mp_list, nbof, h * sizeof(mp->mp_list[0])))
1140 return false;
1141 return true;
1142}
1143
Bob Petersond552a2b2017-02-06 08:28:32 -05001144/**
1145 * trunc_dealloc - truncate a file down to a desired size
1146 * @ip: inode to truncate
1147 * @newsize: The desired size of the file
1148 *
1149 * This function truncates a file to newsize. It works from the
1150 * bottom up, and from the right to the left. In other words, it strips off
1151 * the highest layer (data) before stripping any of the metadata. Doing it
1152 * this way is best in case the operation is interrupted by power failure, etc.
1153 * The dinode is rewritten in every transaction to guarantee integrity.
1154 */
1155static int trunc_dealloc(struct gfs2_inode *ip, u64 newsize)
1156{
1157 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1158 struct metapath mp;
1159 struct buffer_head *dibh, *bh;
1160 struct gfs2_holder rd_gh;
1161 u64 lblock;
1162 __u16 nbof[GFS2_MAX_META_HEIGHT]; /* new beginning of truncation */
1163 unsigned int strip_h = ip->i_height - 1;
1164 u32 btotal = 0;
1165 int ret, state;
1166 int mp_h; /* metapath buffers are read in to this height */
1167 sector_t last_ra = 0;
1168 u64 prev_bnr = 0;
1169 bool preserve1; /* need to preserve the first meta pointer? */
1170
1171 if (!newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001173 else
Bob Petersond552a2b2017-02-06 08:28:32 -05001174 lblock = (newsize - 1) >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175
Bob Petersond552a2b2017-02-06 08:28:32 -05001176 memset(&mp, 0, sizeof(mp));
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001177 find_metapath(sdp, lblock, &mp, ip->i_height);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001178
Bob Petersond552a2b2017-02-06 08:28:32 -05001179 memcpy(&nbof, &mp.mp_list, sizeof(nbof));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180
Bob Petersond552a2b2017-02-06 08:28:32 -05001181 ret = gfs2_meta_inode_buffer(ip, &dibh);
1182 if (ret)
1183 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184
Bob Petersond552a2b2017-02-06 08:28:32 -05001185 mp.mp_bh[0] = dibh;
1186 ret = lookup_metapath(ip, &mp);
1187 if (ret == ip->i_height)
1188 state = DEALLOC_MP_FULL; /* We have a complete metapath */
1189 else
1190 state = DEALLOC_FILL_MP; /* deal with partial metapath */
1191
1192 ret = gfs2_rindex_update(sdp);
1193 if (ret)
1194 goto out_metapath;
1195
1196 ret = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1197 if (ret)
1198 goto out_metapath;
1199 gfs2_holder_mark_uninitialized(&rd_gh);
1200
1201 mp_h = strip_h;
1202
1203 while (state != DEALLOC_DONE) {
1204 switch (state) {
1205 /* Truncate a full metapath at the given strip height.
1206 * Note that strip_h == mp_h in order to be in this state. */
1207 case DEALLOC_MP_FULL:
1208 if (mp_h > 0) { /* issue read-ahead on metadata */
1209 __be64 *top;
1210
1211 bh = mp.mp_bh[mp_h - 1];
1212 if (bh->b_blocknr != last_ra) {
1213 last_ra = bh->b_blocknr;
1214 top = metaptr1(mp_h - 1, &mp);
1215 gfs2_metapath_ra(ip->i_gl, bh, top);
1216 }
1217 }
1218 /* If we're truncating to a non-zero size and the mp is
1219 at the beginning of file for the strip height, we
1220 need to preserve the first metadata pointer. */
Bob Petersonc4a9d182017-08-30 09:26:09 -05001221 preserve1 = (newsize && mp_eq_to_hgt(&mp, nbof, mp_h));
Bob Petersond552a2b2017-02-06 08:28:32 -05001222 bh = mp.mp_bh[mp_h];
1223 gfs2_assert_withdraw(sdp, bh);
1224 if (gfs2_assert_withdraw(sdp,
1225 prev_bnr != bh->b_blocknr)) {
1226 printk(KERN_EMERG "GFS2: fsid=%s:inode %llu, "
1227 "block:%llu, i_h:%u, s_h:%u, mp_h:%u\n",
1228 sdp->sd_fsname,
1229 (unsigned long long)ip->i_no_addr,
1230 prev_bnr, ip->i_height, strip_h, mp_h);
1231 }
1232 prev_bnr = bh->b_blocknr;
1233 ret = sweep_bh_for_rgrps(ip, &rd_gh, &mp, &btotal,
1234 mp_h, preserve1);
1235 /* If we hit an error or just swept dinode buffer,
1236 just exit. */
1237 if (ret || !mp_h) {
1238 state = DEALLOC_DONE;
1239 break;
1240 }
1241 state = DEALLOC_MP_LOWER;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001242 break;
Bob Petersond552a2b2017-02-06 08:28:32 -05001243
1244 /* lower the metapath strip height */
1245 case DEALLOC_MP_LOWER:
1246 /* We're done with the current buffer, so release it,
1247 unless it's the dinode buffer. Then back up to the
1248 previous pointer. */
1249 if (mp_h) {
1250 brelse(mp.mp_bh[mp_h]);
1251 mp.mp_bh[mp_h] = NULL;
1252 }
1253 /* If we can't get any lower in height, we've stripped
1254 off all we can. Next step is to back up and start
1255 stripping the previous level of metadata. */
1256 if (mp_h == 0) {
1257 strip_h--;
1258 memcpy(&mp.mp_list, &nbof, sizeof(nbof));
1259 mp_h = strip_h;
1260 state = DEALLOC_FILL_MP;
1261 break;
1262 }
1263 mp.mp_list[mp_h] = 0;
1264 mp_h--; /* search one metadata height down */
1265 if (mp.mp_list[mp_h] >= hptrs(sdp, mp_h) - 1)
1266 break; /* loop around in the same state */
1267 mp.mp_list[mp_h]++;
1268 /* Here we've found a part of the metapath that is not
1269 * allocated. We need to search at that height for the
1270 * next non-null pointer. */
1271 if (find_nonnull_ptr(sdp, &mp, mp_h)) {
1272 state = DEALLOC_FILL_MP;
1273 mp_h++;
1274 }
1275 /* No more non-null pointers at this height. Back up
1276 to the previous height and try again. */
1277 break; /* loop around in the same state */
1278
1279 /* Fill the metapath with buffers to the given height. */
1280 case DEALLOC_FILL_MP:
1281 /* Fill the buffers out to the current height. */
1282 ret = fillup_metapath(ip, &mp, mp_h);
1283 if (ret < 0)
1284 goto out;
1285
1286 /* If buffers found for the entire strip height */
1287 if ((ret == ip->i_height) && (mp_h == strip_h)) {
1288 state = DEALLOC_MP_FULL;
1289 break;
1290 }
1291 if (ret < ip->i_height) /* We have a partial height */
1292 mp_h = ret - 1;
1293
1294 /* If we find a non-null block pointer, crawl a bit
1295 higher up in the metapath and try again, otherwise
1296 we need to look lower for a new starting point. */
1297 if (find_nonnull_ptr(sdp, &mp, mp_h))
1298 mp_h++;
1299 else
1300 state = DEALLOC_MP_LOWER;
1301 break;
1302 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 }
1304
Bob Petersond552a2b2017-02-06 08:28:32 -05001305 if (btotal) {
1306 if (current->journal_info == NULL) {
1307 ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS +
1308 RES_QUOTA, 0);
1309 if (ret)
1310 goto out;
1311 down_write(&ip->i_rw_mutex);
1312 }
1313 gfs2_statfs_change(sdp, 0, +btotal, 0);
1314 gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
1315 ip->i_inode.i_gid);
Stephen Rothwellb32c8c72017-05-08 15:59:34 -07001316 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Bob Petersond552a2b2017-02-06 08:28:32 -05001317 gfs2_trans_add_meta(ip->i_gl, dibh);
1318 gfs2_dinode_out(ip, dibh->b_data);
1319 up_write(&ip->i_rw_mutex);
1320 gfs2_trans_end(sdp);
1321 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322
Bob Petersond552a2b2017-02-06 08:28:32 -05001323out:
1324 if (gfs2_holder_initialized(&rd_gh))
1325 gfs2_glock_dq_uninit(&rd_gh);
1326 if (current->journal_info) {
1327 up_write(&ip->i_rw_mutex);
1328 gfs2_trans_end(sdp);
1329 cond_resched();
1330 }
1331 gfs2_quota_unhold(ip);
1332out_metapath:
1333 release_metapath(&mp);
1334 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335}
1336
1337static int trunc_end(struct gfs2_inode *ip)
1338{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001339 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340 struct buffer_head *dibh;
1341 int error;
1342
1343 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1344 if (error)
1345 return error;
1346
1347 down_write(&ip->i_rw_mutex);
1348
1349 error = gfs2_meta_inode_buffer(ip, &dibh);
1350 if (error)
1351 goto out;
1352
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001353 if (!i_size_read(&ip->i_inode)) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001354 ip->i_height = 0;
Steven Whitehousece276b02008-02-06 09:25:45 +00001355 ip->i_goal = ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse45138992013-01-28 09:30:07 +00001357 gfs2_ordered_del_inode(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358 }
Deepa Dinamani078cd822016-09-14 07:48:04 -07001359 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001360 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001361
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001362 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001363 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001364 brelse(dibh);
1365
Steven Whitehousea91ea692006-09-04 12:04:26 -04001366out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001369 return error;
1370}
1371
1372/**
1373 * do_shrink - make a file smaller
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001374 * @inode: the inode
1375 * @oldsize: the current inode size
1376 * @newsize: the size to make the file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001377 *
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001378 * Called with an exclusive lock on @inode. The @size must
1379 * be equal to or smaller than the current inode size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380 *
1381 * Returns: errno
1382 */
1383
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001384static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001385{
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001386 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 int error;
1388
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001389 error = trunc_start(inode, oldsize, newsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001390 if (error < 0)
1391 return error;
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001392 if (gfs2_is_stuffed(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001393 return 0;
1394
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001395 error = trunc_dealloc(ip, newsize);
1396 if (error == 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 error = trunc_end(ip);
1398
1399 return error;
1400}
1401
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001402void gfs2_trim_blocks(struct inode *inode)
Wendy Chenga13b8c52007-08-20 09:29:53 -04001403{
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001404 u64 size = inode->i_size;
1405 int ret;
1406
1407 ret = do_shrink(inode, size, size);
1408 WARN_ON(ret != 0);
1409}
1410
1411/**
1412 * do_grow - Touch and update inode size
1413 * @inode: The inode
1414 * @size: The new size
1415 *
1416 * This function updates the timestamps on the inode and
1417 * may also increase the size of the inode. This function
1418 * must not be called with @size any smaller than the current
1419 * inode size.
1420 *
1421 * Although it is not strictly required to unstuff files here,
1422 * earlier versions of GFS2 have a bug in the stuffed file reading
1423 * code which will result in a buffer overrun if the size is larger
1424 * than the max stuffed file size. In order to prevent this from
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001425 * occurring, such files are unstuffed, but in other cases we can
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001426 * just update the inode size directly.
1427 *
1428 * Returns: 0 on success, or -ve on error
1429 */
1430
1431static int do_grow(struct inode *inode, u64 size)
1432{
1433 struct gfs2_inode *ip = GFS2_I(inode);
1434 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001435 struct gfs2_alloc_parms ap = { .target = 1, };
Wendy Chenga13b8c52007-08-20 09:29:53 -04001436 struct buffer_head *dibh;
1437 int error;
Bob Peterson2f7ee352012-04-12 09:19:30 -04001438 int unstuff = 0;
Wendy Chenga13b8c52007-08-20 09:29:53 -04001439
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001440 if (gfs2_is_stuffed(ip) &&
1441 (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001442 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001443 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001444 return error;
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001445
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001446 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001447 if (error)
1448 goto do_grow_qunlock;
Bob Peterson2f7ee352012-04-12 09:19:30 -04001449 unstuff = 1;
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001450 }
1451
Bob Petersona01aedf2013-06-27 12:47:51 -04001452 error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
1453 (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
1454 0 : RES_QUOTA), 0);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001455 if (error)
1456 goto do_grow_release;
1457
Bob Peterson2f7ee352012-04-12 09:19:30 -04001458 if (unstuff) {
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001459 error = gfs2_unstuff_dinode(ip, NULL);
1460 if (error)
1461 goto do_end_trans;
1462 }
Wendy Chenga13b8c52007-08-20 09:29:53 -04001463
1464 error = gfs2_meta_inode_buffer(ip, &dibh);
1465 if (error)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001466 goto do_end_trans;
Wendy Chenga13b8c52007-08-20 09:29:53 -04001467
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001468 i_size_write(inode, size);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001469 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001470 gfs2_trans_add_meta(ip->i_gl, dibh);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001471 gfs2_dinode_out(ip, dibh->b_data);
1472 brelse(dibh);
1473
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001474do_end_trans:
Wendy Chenga13b8c52007-08-20 09:29:53 -04001475 gfs2_trans_end(sdp);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001476do_grow_release:
Bob Peterson2f7ee352012-04-12 09:19:30 -04001477 if (unstuff) {
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001478 gfs2_inplace_release(ip);
1479do_grow_qunlock:
1480 gfs2_quota_unlock(ip);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001481 }
Wendy Chenga13b8c52007-08-20 09:29:53 -04001482 return error;
1483}
1484
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485/**
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001486 * gfs2_setattr_size - make a file a given size
1487 * @inode: the inode
1488 * @newsize: the size to make the file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489 *
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001490 * The file size can grow, shrink, or stay the same size. This
1491 * is called holding i_mutex and an exclusive glock on the inode
1492 * in question.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001493 *
1494 * Returns: errno
1495 */
1496
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001497int gfs2_setattr_size(struct inode *inode, u64 newsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498{
Steven Whitehouseaf5c2692013-09-27 12:49:33 +01001499 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001500 int ret;
1501 u64 oldsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001503 BUG_ON(!S_ISREG(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001504
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001505 ret = inode_newsize_ok(inode, newsize);
1506 if (ret)
1507 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001508
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04001509 inode_dio_wait(inode);
1510
Bob Petersonb54e9a02015-10-26 10:40:28 -05001511 ret = gfs2_rsqa_alloc(ip);
Bob Petersond2b47cf2013-02-01 12:03:02 -05001512 if (ret)
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001513 goto out;
Bob Petersond2b47cf2013-02-01 12:03:02 -05001514
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001515 oldsize = inode->i_size;
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001516 if (newsize >= oldsize) {
1517 ret = do_grow(inode, newsize);
1518 goto out;
1519 }
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001520
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001521 ret = do_shrink(inode, oldsize, newsize);
1522out:
Bob Petersona097dc7e2015-07-16 08:28:04 -05001523 gfs2_rsqa_delete(ip, NULL);
Bob Peterson2b3dcf32013-05-28 10:04:44 -04001524 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001525}
1526
1527int gfs2_truncatei_resume(struct gfs2_inode *ip)
1528{
1529 int error;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001530 error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531 if (!error)
1532 error = trunc_end(ip);
1533 return error;
1534}
1535
1536int gfs2_file_dealloc(struct gfs2_inode *ip)
1537{
1538 return trunc_dealloc(ip, 0);
1539}
1540
1541/**
Steven Whitehouseb50f2272014-03-03 13:35:57 +00001542 * gfs2_free_journal_extents - Free cached journal bmap info
1543 * @jd: The journal
1544 *
1545 */
1546
1547void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
1548{
1549 struct gfs2_journal_extent *jext;
1550
1551 while(!list_empty(&jd->extent_list)) {
1552 jext = list_entry(jd->extent_list.next, struct gfs2_journal_extent, list);
1553 list_del(&jext->list);
1554 kfree(jext);
1555 }
1556}
1557
1558/**
1559 * gfs2_add_jextent - Add or merge a new extent to extent cache
1560 * @jd: The journal descriptor
1561 * @lblock: The logical block at start of new extent
Fabian Frederickc62baf62014-05-14 18:32:31 +02001562 * @dblock: The physical block at start of new extent
Steven Whitehouseb50f2272014-03-03 13:35:57 +00001563 * @blocks: Size of extent in fs blocks
1564 *
1565 * Returns: 0 on success or -ENOMEM
1566 */
1567
1568static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
1569{
1570 struct gfs2_journal_extent *jext;
1571
1572 if (!list_empty(&jd->extent_list)) {
1573 jext = list_entry(jd->extent_list.prev, struct gfs2_journal_extent, list);
1574 if ((jext->dblock + jext->blocks) == dblock) {
1575 jext->blocks += blocks;
1576 return 0;
1577 }
1578 }
1579
1580 jext = kzalloc(sizeof(struct gfs2_journal_extent), GFP_NOFS);
1581 if (jext == NULL)
1582 return -ENOMEM;
1583 jext->dblock = dblock;
1584 jext->lblock = lblock;
1585 jext->blocks = blocks;
1586 list_add_tail(&jext->list, &jd->extent_list);
1587 jd->nr_extents++;
1588 return 0;
1589}
1590
1591/**
1592 * gfs2_map_journal_extents - Cache journal bmap info
1593 * @sdp: The super block
1594 * @jd: The journal to map
1595 *
1596 * Create a reusable "extent" mapping from all logical
1597 * blocks to all physical blocks for the given journal. This will save
1598 * us time when writing journal blocks. Most journals will have only one
1599 * extent that maps all their logical blocks. That's because gfs2.mkfs
1600 * arranges the journal blocks sequentially to maximize performance.
1601 * So the extent would map the first block for the entire file length.
1602 * However, gfs2_jadd can happen while file activity is happening, so
1603 * those journals may not be sequential. Less likely is the case where
1604 * the users created their own journals by mounting the metafs and
1605 * laying it out. But it's still possible. These journals might have
1606 * several extents.
1607 *
1608 * Returns: 0 on success, or error on failure
1609 */
1610
1611int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
1612{
1613 u64 lblock = 0;
1614 u64 lblock_stop;
1615 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
1616 struct buffer_head bh;
1617 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1618 u64 size;
1619 int rc;
1620
1621 lblock_stop = i_size_read(jd->jd_inode) >> shift;
1622 size = (lblock_stop - lblock) << shift;
1623 jd->nr_extents = 0;
1624 WARN_ON(!list_empty(&jd->extent_list));
1625
1626 do {
1627 bh.b_state = 0;
1628 bh.b_blocknr = 0;
1629 bh.b_size = size;
1630 rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
1631 if (rc || !buffer_mapped(&bh))
1632 goto fail;
1633 rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
1634 if (rc)
1635 goto fail;
1636 size -= bh.b_size;
1637 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1638 } while(size > 0);
1639
1640 fs_info(sdp, "journal %d mapped with %u extents\n", jd->jd_jid,
1641 jd->nr_extents);
1642 return 0;
1643
1644fail:
1645 fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
1646 rc, jd->jd_jid,
1647 (unsigned long long)(i_size_read(jd->jd_inode) - size),
1648 jd->nr_extents);
1649 fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
1650 rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
1651 bh.b_state, (unsigned long long)bh.b_size);
1652 gfs2_free_journal_extents(jd);
1653 return rc;
1654}
1655
1656/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001657 * gfs2_write_alloc_required - figure out if a write will require an allocation
1658 * @ip: the file being written to
1659 * @offset: the offset to write to
1660 * @len: the number of bytes being written
David Teiglandb3b94fa2006-01-16 16:50:04 +00001661 *
Bob Peterson461cb412010-06-24 19:21:20 -04001662 * Returns: 1 if an alloc is required, 0 otherwise
David Teiglandb3b94fa2006-01-16 16:50:04 +00001663 */
1664
Steven Whitehousecd915492006-09-04 12:49:07 -04001665int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
Bob Peterson461cb412010-06-24 19:21:20 -04001666 unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001667{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001668 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001669 struct buffer_head bh;
1670 unsigned int shift;
1671 u64 lblock, lblock_stop, size;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001672 u64 end_of_file;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001673
David Teiglandb3b94fa2006-01-16 16:50:04 +00001674 if (!len)
1675 return 0;
1676
1677 if (gfs2_is_stuffed(ip)) {
1678 if (offset + len >
1679 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Bob Peterson461cb412010-06-24 19:21:20 -04001680 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001681 return 0;
1682 }
1683
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001684 shift = sdp->sd_sb.sb_bsize_shift;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001685 BUG_ON(gfs2_is_dir(ip));
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001686 end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
Steven Whitehouse7ed122e2008-12-10 10:28:10 +00001687 lblock = offset >> shift;
1688 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1689 if (lblock_stop > end_of_file)
Bob Peterson461cb412010-06-24 19:21:20 -04001690 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001691
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001692 size = (lblock_stop - lblock) << shift;
1693 do {
1694 bh.b_state = 0;
1695 bh.b_size = size;
1696 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1697 if (!buffer_mapped(&bh))
Bob Peterson461cb412010-06-24 19:21:20 -04001698 return 1;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001699 size -= bh.b_size;
1700 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1701 } while(size > 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001702
1703 return 0;
1704}
1705