blob: 651e5320bb4f9112f87dcc2681df86131f85471f [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/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.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"
26#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000027#include "dir.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
Steven Whitehouseba7f7292006-07-26 11:27:10 -040029#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
31/* This doesn't need to be that large as max 64 bit pointers in a 4k
32 * block is 512, so __u16 is fine for that. It saves stack space to
33 * keep it small.
34 */
35struct metapath {
Steven Whitehousedbac6712008-01-29 09:12:55 +000036 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
David Teiglandb3b94fa2006-01-16 16:50:04 +000037 __u16 mp_list[GFS2_MAX_META_HEIGHT];
38};
39
40typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -040041 struct buffer_head *bh, __be64 *top,
42 __be64 *bottom, unsigned int height,
David Teiglandb3b94fa2006-01-16 16:50:04 +000043 void *data);
44
45struct strip_mine {
46 int sm_first;
47 unsigned int sm_height;
48};
49
50/**
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040051 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
52 * @ip: the inode
53 * @dibh: the dinode buffer
54 * @block: the block number that was allocated
55 * @private: any locked page held by the caller process
56 *
57 * Returns: errno
58 */
59
60static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
Steven Whitehousecd915492006-09-04 12:49:07 -040061 u64 block, struct page *page)
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040062{
Steven Whitehousef25ef0c2006-07-26 10:51:20 -040063 struct inode *inode = &ip->i_inode;
64 struct buffer_head *bh;
65 int release = 0;
66
67 if (!page || page->index) {
68 page = grab_cache_page(inode->i_mapping, 0);
69 if (!page)
70 return -ENOMEM;
71 release = 1;
72 }
73
74 if (!PageUptodate(page)) {
75 void *kaddr = kmap(page);
76
77 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
78 ip->i_di.di_size);
79 memset(kaddr + ip->i_di.di_size, 0,
80 PAGE_CACHE_SIZE - ip->i_di.di_size);
81 kunmap(page);
82
83 SetPageUptodate(page);
84 }
85
86 if (!page_has_buffers(page))
87 create_empty_buffers(page, 1 << inode->i_blkbits,
88 (1 << BH_Uptodate));
89
90 bh = page_buffers(page);
91
92 if (!buffer_mapped(bh))
93 map_bh(bh, inode->i_sb, block);
94
95 set_buffer_uptodate(bh);
Steven Whitehouseeaf96522007-08-27 09:49:37 +010096 if (!gfs2_is_jdata(ip))
97 mark_buffer_dirty(bh);
Steven Whitehousebf36a712007-10-17 08:35:19 +010098 if (!gfs2_is_writeback(ip))
Bob Peterson84754872007-09-02 10:55:29 +010099 gfs2_trans_add_bh(ip->i_gl, bh, 0);
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400100
101 if (release) {
102 unlock_page(page);
103 page_cache_release(page);
104 }
105
106 return 0;
107}
108
109/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
111 * @ip: The GFS2 inode to unstuff
112 * @unstuffer: the routine that handles unstuffing a non-zero length file
113 * @private: private data for the unstuffer
114 *
115 * This routine unstuffs a dinode and returns it to a "normal" state such
116 * that the height can be grown in the traditional way.
117 *
118 * Returns: errno
119 */
120
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400121int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122{
123 struct buffer_head *bh, *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400124 struct gfs2_dinode *di;
Steven Whitehousecd915492006-09-04 12:49:07 -0400125 u64 block = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000126 int isdir = gfs2_is_dir(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 int error;
128
129 down_write(&ip->i_rw_mutex);
130
131 error = gfs2_meta_inode_buffer(ip, &dibh);
132 if (error)
133 goto out;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400134
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 if (ip->i_di.di_size) {
136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
138
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000139 if (isdir) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 block = gfs2_alloc_meta(ip);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000141 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400142 error = gfs2_dir_get_new_buffer(ip, block, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 if (error)
144 goto out_brelse;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400145 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 dibh, sizeof(struct gfs2_dinode));
147 brelse(bh);
148 } else {
149 block = gfs2_alloc_data(ip);
150
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400151 error = gfs2_unstuffer_page(ip, dibh, block, page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 if (error)
153 goto out_brelse;
154 }
155 }
156
157 /* Set up the pointer to the new block */
158
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000159 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400160 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
162
163 if (ip->i_di.di_size) {
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400164 *(__be64 *)(di + 1) = cpu_to_be64(block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500166 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400167 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 }
169
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000170 ip->i_height = 1;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400171 di->di_height = cpu_to_be16(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
Steven Whitehousea91ea692006-09-04 12:04:26 -0400173out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400175out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 return error;
178}
179
180/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 * build_height - Build a metadata tree of the requested height
182 * @ip: The GFS2 inode
183 * @height: The height to build to
184 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 *
186 * Returns: errno
187 */
188
Steven Whitehousedbac6712008-01-29 09:12:55 +0000189static int build_height(struct inode *inode, struct metapath *mp, unsigned height)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400191 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000192 unsigned new_height = height - ip->i_height;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400193 struct buffer_head *dibh;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400194 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 int error;
Al Virob44b84d2006-10-14 10:46:30 -0400196 __be64 *bp;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400197 u64 bn;
198 unsigned n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000200 if (height <= ip->i_height)
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400201 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400203 error = gfs2_meta_inode_buffer(ip, &dibh);
204 if (error)
205 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400207 for(n = 0; n < new_height; n++) {
208 bn = gfs2_alloc_meta(ip);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000209 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehousedbac6712008-01-29 09:12:55 +0000210 mp->mp_bh[n] = gfs2_meta_new(ip->i_gl, bn);
211 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[n], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400213
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400214 n = 0;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000215 bn = mp->mp_bh[0]->b_blocknr;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400216 if (new_height > 1) {
217 for(; n < new_height-1; n++) {
Steven Whitehousedbac6712008-01-29 09:12:55 +0000218 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN,
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400219 GFS2_FORMAT_IN);
Steven Whitehousedbac6712008-01-29 09:12:55 +0000220 gfs2_buffer_clear_tail(mp->mp_bh[n],
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400221 sizeof(struct gfs2_meta_header));
Steven Whitehousedbac6712008-01-29 09:12:55 +0000222 bp = (__be64 *)(mp->mp_bh[n]->b_data +
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400223 sizeof(struct gfs2_meta_header));
Steven Whitehousedbac6712008-01-29 09:12:55 +0000224 *bp = cpu_to_be64(mp->mp_bh[n+1]->b_blocknr);
225 brelse(mp->mp_bh[n]);
226 mp->mp_bh[n] = NULL;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400227 }
228 }
Steven Whitehousedbac6712008-01-29 09:12:55 +0000229 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
230 gfs2_buffer_copy_tail(mp->mp_bh[n], sizeof(struct gfs2_meta_header),
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400231 dibh, sizeof(struct gfs2_dinode));
Steven Whitehousedbac6712008-01-29 09:12:55 +0000232 brelse(mp->mp_bh[n]);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400233 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400234 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400235 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400236 *(__be64 *)(di + 1) = cpu_to_be64(bn);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000237 ip->i_height += new_height;
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400238 ip->i_di.di_blocks += new_height;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500239 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000240 di->di_height = cpu_to_be16(ip->i_height);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400241 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
Steven Whitehousee90c01e2006-05-12 12:09:15 -0400242 brelse(dibh);
243 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244}
245
246/**
247 * find_metapath - Find path through the metadata tree
248 * @ip: The inode pointer
249 * @mp: The metapath to return the result in
250 * @block: The disk block to look up
251 *
252 * This routine returns a struct metapath structure that defines a path
253 * through the metadata of inode "ip" to get to block "block".
254 *
255 * Example:
256 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
257 * filesystem with a blocksize of 4096.
258 *
259 * find_metapath() would return a struct metapath structure set to:
260 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
261 * and mp_list[2] = 165.
262 *
263 * That means that in order to get to the block containing the byte at
264 * offset 101342453, we would load the indirect block pointed to by pointer
265 * 0 in the dinode. We would then load the indirect block pointed to by
266 * pointer 48 in that indirect block. We would then load the data block
267 * pointed to by pointer 165 in that indirect block.
268 *
269 * ----------------------------------------
270 * | Dinode | |
271 * | | 4|
272 * | |0 1 2 3 4 5 9|
273 * | | 6|
274 * ----------------------------------------
275 * |
276 * |
277 * V
278 * ----------------------------------------
279 * | Indirect Block |
280 * | 5|
281 * | 4 4 4 4 4 5 5 1|
282 * |0 5 6 7 8 9 0 1 2|
283 * ----------------------------------------
284 * |
285 * |
286 * V
287 * ----------------------------------------
288 * | Indirect Block |
289 * | 1 1 1 1 1 5|
290 * | 6 6 6 6 6 1|
291 * |0 3 4 5 6 7 2|
292 * ----------------------------------------
293 * |
294 * |
295 * V
296 * ----------------------------------------
297 * | Data block containing offset |
298 * | 101342453 |
299 * | |
300 * | |
301 * ----------------------------------------
302 *
303 */
304
Steven Whitehousecd915492006-09-04 12:49:07 -0400305static void find_metapath(struct gfs2_inode *ip, u64 block,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500306 struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400308 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 unsigned int i;
310
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000311 for (i = ip->i_height; i--;)
Bob Peterson7eabb772008-01-28 11:24:35 -0600312 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313
314}
315
316/**
317 * metapointer - Return pointer to start of metadata in a buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 * @height: The metadata height (0 = dinode)
319 * @mp: The metapath
320 *
321 * Return a pointer to the block number of the next height of the metadata
322 * tree given a buffer containing the pointer to the current height of the
323 * metadata tree.
324 */
325
Steven Whitehousedbac6712008-01-29 09:12:55 +0000326static inline __be64 *metapointer(int *boundary, unsigned int height,
327 const struct metapath *mp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328{
Steven Whitehousedbac6712008-01-29 09:12:55 +0000329 struct buffer_head *bh = mp->mp_bh[height];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 unsigned int head_size = (height > 0) ?
331 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
Al Virob44b84d2006-10-14 10:46:30 -0400332 __be64 *ptr;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400333 *boundary = 0;
Al Virob44b84d2006-10-14 10:46:30 -0400334 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
335 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400336 *boundary = 1;
337 return ptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338}
339
340/**
341 * lookup_block - Get the next metadata block in metadata tree
342 * @ip: The GFS2 inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343 * @height: The height of the tree (0 = dinode)
344 * @mp: The metapath
345 * @create: Non-zero if we may create a new meatdata block
346 * @new: Used to indicate if we did create a new metadata block
347 * @block: the returned disk block number
348 *
349 * Given a metatree, complete to a particular height, checks to see if the next
350 * height of the tree exists. If not the next height of the tree is created.
351 * The block number of the next height of the metadata tree is returned.
352 *
353 */
354
Steven Whitehousedbac6712008-01-29 09:12:55 +0000355static int lookup_block(struct gfs2_inode *ip, unsigned int height,
356 struct metapath *mp, int create,
Steven Whitehousecd915492006-09-04 12:49:07 -0400357 int *new, u64 *block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358{
Steven Whitehousefd88de562006-05-05 16:59:11 -0400359 int boundary;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000360 __be64 *ptr = metapointer(&boundary, height, mp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361
362 if (*ptr) {
363 *block = be64_to_cpu(*ptr);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400364 return boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365 }
366
367 *block = 0;
368
369 if (!create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400370 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000372 if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 *block = gfs2_alloc_data(ip);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000374 else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375 *block = gfs2_alloc_meta(ip);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000376 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), *block, 1);
377 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378
Steven Whitehousedbac6712008-01-29 09:12:55 +0000379 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380
381 *ptr = cpu_to_be64(*block);
382 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500383 gfs2_set_inode_blocks(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384
385 *new = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400386 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387}
388
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000389static int lookup_metapath(struct inode *inode, struct metapath *mp,
Steven Whitehousedbac6712008-01-29 09:12:55 +0000390 int create, int *new, u64 *dblock)
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000391{
Steven Whitehousedbac6712008-01-29 09:12:55 +0000392 struct buffer_head *bh;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000393 struct gfs2_inode *ip = GFS2_I(inode);
394 unsigned int end_of_metadata = ip->i_height - 1;
395 unsigned int x;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000396 int ret = gfs2_meta_inode_buffer(ip, &bh);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000397 if (ret)
398 return ret;
399
Steven Whitehousedbac6712008-01-29 09:12:55 +0000400 mp->mp_bh[0] = bh;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000401
402 for (x = 0; x < end_of_metadata; x++) {
Steven Whitehousedbac6712008-01-29 09:12:55 +0000403 lookup_block(ip, x, mp, create, new, dblock);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000404 if (!dblock)
405 return 0;
406
Steven Whitehousedbac6712008-01-29 09:12:55 +0000407 ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000408 if (ret)
409 return ret;
410 }
411
Steven Whitehousedbac6712008-01-29 09:12:55 +0000412 return lookup_block(ip, end_of_metadata, mp, create, new, dblock);
413}
414
415static void release_metapath(struct metapath *mp)
416{
417 int i;
418
419 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++)
420 if (mp->mp_bh[i])
421 brelse(mp->mp_bh[i]);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000422}
423
Steven Whitehousefd88de562006-05-05 16:59:11 -0400424static inline void bmap_lock(struct inode *inode, int create)
425{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400426 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400427 if (create)
428 down_write(&ip->i_rw_mutex);
429 else
430 down_read(&ip->i_rw_mutex);
431}
432
433static inline void bmap_unlock(struct inode *inode, int create)
434{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400435 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 if (create)
437 up_write(&ip->i_rw_mutex);
438 else
439 up_read(&ip->i_rw_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400440}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500442/**
443 * gfs2_block_map - Map a block from an inode to a disk block
444 * @inode: The inode
445 * @lblock: The logical block number
446 * @bh_map: The bh to be mapped
447 *
448 * Find the block number on the current device which corresponds to an
449 * inode's block. If the block had to be created, "new" will be set.
450 *
451 * Returns: errno
452 */
453
Bob Petersone9e1ef22007-12-10 14:13:27 -0600454int gfs2_block_map(struct inode *inode, sector_t lblock,
455 struct buffer_head *bh_map, int create)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400456{
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500457 struct gfs2_inode *ip = GFS2_I(inode);
458 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000459 unsigned int bsize = sdp->sd_sb.sb_bsize;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500460 int error = 0;
461 int new = 0;
462 u64 dblock = 0;
463 int boundary;
464 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400465 struct metapath mp;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500466 u64 size;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000467 const u64 *arr = sdp->sd_heightsize;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500468 BUG_ON(maxlen == 0);
469
470 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
471 return 0;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400472
Steven Whitehousedbac6712008-01-29 09:12:55 +0000473 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
Steven Whitehousefd88de562006-05-05 16:59:11 -0400474 bmap_lock(inode, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500475 clear_buffer_mapped(bh_map);
476 clear_buffer_new(bh_map);
477 clear_buffer_boundary(bh_map);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000478 if (gfs2_is_dir(ip)) {
479 bsize = sdp->sd_jbsize;
480 arr = sdp->sd_jheightsize;
481 }
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500482 size = (lblock + 1) * bsize;
483
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000484 if (size > arr[ip->i_height]) {
485 u8 height = ip->i_height;
486 if (!create)
487 goto out_ok;
488 while (size > arr[height])
489 height++;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000490 error = build_height(inode, &mp, height);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000491 if (error)
492 goto out_fail;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500493 }
494
495 find_metapath(ip, lblock, &mp);
Steven Whitehousedbac6712008-01-29 09:12:55 +0000496 error = lookup_metapath(inode, &mp, create, &new, &dblock);
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000497 if (error < 0)
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500498 goto out_fail;
Steven Whitehouse11707ea2008-01-28 15:10:29 +0000499 boundary = error;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500500
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500501 if (dblock) {
502 map_bh(bh_map, inode->i_sb, dblock);
503 if (boundary)
504 set_buffer_boundary(bh_map);
505 if (new) {
Steven Whitehousedbac6712008-01-29 09:12:55 +0000506 gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1);
507 gfs2_dinode_out(ip, mp.mp_bh[0]->b_data);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500508 set_buffer_new(bh_map);
Steven Whitehousedbac6712008-01-29 09:12:55 +0000509 goto out_ok;
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500510 }
511 while(--maxlen && !buffer_boundary(bh_map)) {
512 u64 eblock;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000513 mp.mp_list[ip->i_height - 1]++;
514 boundary = lookup_block(ip, ip->i_height - 1, &mp, 0, &new, &eblock);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500515 if (eblock != ++dblock)
516 break;
517 bh_map->b_size += (1 << inode->i_blkbits);
518 if (boundary)
519 set_buffer_boundary(bh_map);
520 }
521 }
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500522out_ok:
523 error = 0;
524out_fail:
Steven Whitehousedbac6712008-01-29 09:12:55 +0000525 release_metapath(&mp);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400526 bmap_unlock(inode, create);
Steven Whitehouse4cf1ed82006-11-15 15:21:06 -0500527 return error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400528}
529
Steven Whitehouse941e6d72008-01-28 08:47:38 +0000530/*
531 * Deprecated: do not use in new code
532 */
Steven Whitehousefd88de562006-05-05 16:59:11 -0400533int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
534{
Steven Whitehouse23591252006-10-13 17:25:45 -0400535 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400536 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400537 int create = *new;
538
539 BUG_ON(!extlen);
540 BUG_ON(!dblock);
541 BUG_ON(!new);
542
Steven Whitehouse23591252006-10-13 17:25:45 -0400543 bh.b_size = 1 << (inode->i_blkbits + 5);
Bob Petersone9e1ef22007-12-10 14:13:27 -0600544 ret = gfs2_block_map(inode, lblock, &bh, create);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400545 *extlen = bh.b_size >> inode->i_blkbits;
546 *dblock = bh.b_blocknr;
547 if (buffer_new(&bh))
548 *new = 1;
549 else
550 *new = 0;
551 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552}
553
554/**
555 * recursive_scan - recursively scan through the end of a file
556 * @ip: the inode
557 * @dibh: the dinode buffer
558 * @mp: the path through the metadata to the point to start
559 * @height: the height the recursion is at
560 * @block: the indirect block to look at
561 * @first: 1 if this is the first block
562 * @bc: the call to make for each piece of metadata
563 * @data: data opaque to this function to pass to @bc
564 *
565 * When this is first called @height and @block should be zero and
566 * @first should be 1.
567 *
568 * Returns: errno
569 */
570
571static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
572 struct metapath *mp, unsigned int height,
Steven Whitehousecd915492006-09-04 12:49:07 -0400573 u64 block, int first, block_call_t bc,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 void *data)
575{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400576 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 struct buffer_head *bh = NULL;
Al Virob44b84d2006-10-14 10:46:30 -0400578 __be64 *top, *bottom;
Steven Whitehousecd915492006-09-04 12:49:07 -0400579 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 int error;
581 int mh_size = sizeof(struct gfs2_meta_header);
582
583 if (!height) {
584 error = gfs2_meta_inode_buffer(ip, &bh);
585 if (error)
586 return error;
587 dibh = bh;
588
Al Virob44b84d2006-10-14 10:46:30 -0400589 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
590 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 } else {
592 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
593 if (error)
594 return error;
595
Al Virob44b84d2006-10-14 10:46:30 -0400596 top = (__be64 *)(bh->b_data + mh_size) +
Jan Engelhardtc53921242006-09-05 14:30:40 +0200597 (first ? mp->mp_list[height] : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598
Al Virob44b84d2006-10-14 10:46:30 -0400599 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 }
601
602 error = bc(ip, dibh, bh, top, bottom, height, data);
603 if (error)
604 goto out;
605
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000606 if (height < ip->i_height - 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 for (; top < bottom; top++, first = 0) {
608 if (!*top)
609 continue;
610
611 bn = be64_to_cpu(*top);
612
613 error = recursive_scan(ip, dibh, mp, height + 1, bn,
614 first, bc, data);
615 if (error)
616 break;
617 }
618
Steven Whitehousea91ea692006-09-04 12:04:26 -0400619out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 return error;
622}
623
624/**
625 * do_strip - Look for a layer a particular layer of the file and strip it off
626 * @ip: the inode
627 * @dibh: the dinode buffer
628 * @bh: A buffer of pointers
629 * @top: The first pointer in the buffer
630 * @bottom: One more than the last pointer
631 * @height: the height this buffer is at
632 * @data: a pointer to a struct strip_mine
633 *
634 * Returns: errno
635 */
636
637static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
Al Virob44b84d2006-10-14 10:46:30 -0400638 struct buffer_head *bh, __be64 *top, __be64 *bottom,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 unsigned int height, void *data)
640{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400641 struct strip_mine *sm = data;
642 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 struct gfs2_rgrp_list rlist;
Steven Whitehousecd915492006-09-04 12:49:07 -0400644 u64 bn, bstart;
645 u32 blen;
Al Virob44b84d2006-10-14 10:46:30 -0400646 __be64 *p;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647 unsigned int rg_blocks = 0;
648 int metadata;
649 unsigned int revokes = 0;
650 int x;
651 int error;
652
653 if (!*top)
654 sm->sm_first = 0;
655
656 if (height != sm->sm_height)
657 return 0;
658
659 if (sm->sm_first) {
660 top++;
661 sm->sm_first = 0;
662 }
663
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000664 metadata = (height != ip->i_height - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 if (metadata)
666 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
667
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000668 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 if (error)
670 return error;
671
672 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
673 bstart = 0;
674 blen = 0;
675
676 for (p = top; p < bottom; p++) {
677 if (!*p)
678 continue;
679
680 bn = be64_to_cpu(*p);
681
682 if (bstart + blen == bn)
683 blen++;
684 else {
685 if (bstart)
686 gfs2_rlist_add(sdp, &rlist, bstart);
687
688 bstart = bn;
689 blen = 1;
690 }
691 }
692
693 if (bstart)
694 gfs2_rlist_add(sdp, &rlist, bstart);
695 else
696 goto out; /* Nothing to do */
697
Bob Petersonfe6c9912008-01-28 11:13:02 -0600698 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699
700 for (x = 0; x < rlist.rl_rgrps; x++) {
701 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500702 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100703 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 }
705
706 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
707 if (error)
708 goto out_rlist;
709
710 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
711 RES_INDIRECT + RES_STATFS + RES_QUOTA,
712 revokes);
713 if (error)
714 goto out_rg_gunlock;
715
716 down_write(&ip->i_rw_mutex);
717
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000718 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
719 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720
721 bstart = 0;
722 blen = 0;
723
724 for (p = top; p < bottom; p++) {
725 if (!*p)
726 continue;
727
728 bn = be64_to_cpu(*p);
729
730 if (bstart + blen == bn)
731 blen++;
732 else {
733 if (bstart) {
734 if (metadata)
735 gfs2_free_meta(ip, bstart, blen);
736 else
737 gfs2_free_data(ip, bstart, blen);
738 }
739
740 bstart = bn;
741 blen = 1;
742 }
743
744 *p = 0;
745 if (!ip->i_di.di_blocks)
746 gfs2_consist_inode(ip);
747 ip->i_di.di_blocks--;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500748 gfs2_set_inode_blocks(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 }
750 if (bstart) {
751 if (metadata)
752 gfs2_free_meta(ip, bstart, blen);
753 else
754 gfs2_free_data(ip, bstart, blen);
755 }
756
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100757 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500759 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760
761 up_write(&ip->i_rw_mutex);
762
763 gfs2_trans_end(sdp);
764
Steven Whitehousea91ea692006-09-04 12:04:26 -0400765out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400767out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400769out:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000770 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771 return error;
772}
773
774/**
775 * do_grow - Make a file look bigger than it is
776 * @ip: the inode
777 * @size: the size to set the file to
778 *
779 * Called with an exclusive lock on @ip.
780 *
781 * Returns: errno
782 */
783
Steven Whitehousecd915492006-09-04 12:49:07 -0400784static int do_grow(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400786 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 struct gfs2_alloc *al;
788 struct buffer_head *dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 int error;
790
791 al = gfs2_alloc_get(ip);
792
793 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
794 if (error)
795 goto out;
796
Steven Whitehouse2933f922006-11-01 13:23:29 -0500797 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 if (error)
799 goto out_gunlock_q;
800
801 al->al_requested = sdp->sd_max_height + RES_DATA;
802
803 error = gfs2_inplace_reserve(ip);
804 if (error)
805 goto out_gunlock_q;
806
807 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100808 sdp->sd_max_height + al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
810 if (error)
811 goto out_ipres;
812
813 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000814 const u64 *arr = sdp->sd_heightsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400816 error = gfs2_unstuff_dinode(ip, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 if (error)
818 goto out_end_trans;
819 }
820
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000821 down_write(&ip->i_rw_mutex);
822 if (size > arr[ip->i_height]) {
Steven Whitehousedbac6712008-01-29 09:12:55 +0000823 struct metapath mp;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000824 u8 height = ip->i_height;
825 while(size > arr[height])
826 height++;
Steven Whitehousedbac6712008-01-29 09:12:55 +0000827 error = build_height(&ip->i_inode, &mp, height);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 }
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000829 up_write(&ip->i_rw_mutex);
830 if (error)
831 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 }
833
834 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100835 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836
837 error = gfs2_meta_inode_buffer(ip, &dibh);
838 if (error)
839 goto out_end_trans;
840
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000841 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500842 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 brelse(dibh);
844
Steven Whitehousea91ea692006-09-04 12:04:26 -0400845out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400847out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400849out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400851out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 return error;
854}
855
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400856
857/**
858 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
859 *
860 * This is partly borrowed from ext3.
861 */
862static int gfs2_block_truncate_page(struct address_space *mapping)
863{
864 struct inode *inode = mapping->host;
865 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400866 loff_t from = inode->i_size;
867 unsigned long index = from >> PAGE_CACHE_SHIFT;
868 unsigned offset = from & (PAGE_CACHE_SIZE-1);
869 unsigned blocksize, iblock, length, pos;
870 struct buffer_head *bh;
871 struct page *page;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400872 int err;
873
874 page = grab_cache_page(mapping, index);
875 if (!page)
876 return 0;
877
878 blocksize = inode->i_sb->s_blocksize;
879 length = blocksize - (offset & (blocksize - 1));
880 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
881
882 if (!page_has_buffers(page))
883 create_empty_buffers(page, blocksize, 0);
884
885 /* Find the buffer that contains "offset" */
886 bh = page_buffers(page);
887 pos = blocksize;
888 while (offset >= pos) {
889 bh = bh->b_this_page;
890 iblock++;
891 pos += blocksize;
892 }
893
894 err = 0;
895
896 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600897 gfs2_block_map(inode, iblock, bh, 0);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400898 /* unmapped? It's a hole - nothing to do */
899 if (!buffer_mapped(bh))
900 goto unlock;
901 }
902
903 /* Ok, it's mapped. Make sure it's up-to-date */
904 if (PageUptodate(page))
905 set_buffer_uptodate(bh);
906
907 if (!buffer_uptodate(bh)) {
908 err = -EIO;
909 ll_rw_block(READ, 1, &bh);
910 wait_on_buffer(bh);
911 /* Uhhuh. Read error. Complain and punt. */
912 if (!buffer_uptodate(bh))
913 goto unlock;
S. Wendy Cheng1875f2f2007-06-25 21:14:31 -0400914 err = 0;
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400915 }
916
Steven Whitehousebf36a712007-10-17 08:35:19 +0100917 if (!gfs2_is_writeback(ip))
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400918 gfs2_trans_add_bh(ip->i_gl, bh, 0);
919
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800920 zero_user(page, offset, length);
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400921
922unlock:
923 unlock_page(page);
924 page_cache_release(page);
925 return err;
926}
927
Steven Whitehousecd915492006-09-04 12:49:07 -0400928static int trunc_start(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400930 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 struct buffer_head *dibh;
932 int journaled = gfs2_is_jdata(ip);
933 int error;
934
935 error = gfs2_trans_begin(sdp,
Jan Engelhardtc53921242006-09-05 14:30:40 +0200936 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 if (error)
938 return error;
939
940 error = gfs2_meta_inode_buffer(ip, &dibh);
941 if (error)
942 goto out;
943
944 if (gfs2_is_stuffed(ip)) {
945 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100946 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000947 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500948 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
950 error = 1;
951
952 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400953 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400954 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955
956 if (!error) {
957 ip->i_di.di_size = size;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100958 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000960 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500961 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962 }
963 }
964
965 brelse(dibh);
966
Steven Whitehousea91ea692006-09-04 12:04:26 -0400967out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969 return error;
970}
971
Steven Whitehousecd915492006-09-04 12:49:07 -0400972static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973{
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000974 unsigned int height = ip->i_height;
Steven Whitehousecd915492006-09-04 12:49:07 -0400975 u64 lblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 struct metapath mp;
977 int error;
978
979 if (!size)
980 lblock = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000981 else
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400982 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983
984 find_metapath(ip, lblock, &mp);
985 gfs2_alloc_get(ip);
986
987 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
988 if (error)
989 goto out;
990
991 while (height--) {
992 struct strip_mine sm;
993 sm.sm_first = !!size;
994 sm.sm_height = height;
995
996 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
997 if (error)
998 break;
999 }
1000
1001 gfs2_quota_unhold(ip);
1002
Steven Whitehousea91ea692006-09-04 12:04:26 -04001003out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 gfs2_alloc_put(ip);
1005 return error;
1006}
1007
1008static int trunc_end(struct gfs2_inode *ip)
1009{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001010 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 struct buffer_head *dibh;
1012 int error;
1013
1014 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1015 if (error)
1016 return error;
1017
1018 down_write(&ip->i_rw_mutex);
1019
1020 error = gfs2_meta_inode_buffer(ip, &dibh);
1021 if (error)
1022 goto out;
1023
1024 if (!ip->i_di.di_size) {
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001025 ip->i_height = 0;
Steven Whitehousece276b02008-02-06 09:25:45 +00001026 ip->i_goal = ip->i_no_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1028 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001029 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1031
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001032 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001033 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 brelse(dibh);
1035
Steven Whitehousea91ea692006-09-04 12:04:26 -04001036out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 up_write(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 return error;
1040}
1041
1042/**
1043 * do_shrink - make a file smaller
1044 * @ip: the inode
1045 * @size: the size to make the file
1046 * @truncator: function to truncate the last partial block
1047 *
1048 * Called with an exclusive lock on @ip.
1049 *
1050 * Returns: errno
1051 */
1052
Steven Whitehousecd915492006-09-04 12:49:07 -04001053static int do_shrink(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054{
1055 int error;
1056
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001057 error = trunc_start(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 if (error < 0)
1059 return error;
1060 if (error > 0)
1061 return 0;
1062
1063 error = trunc_dealloc(ip, size);
1064 if (!error)
1065 error = trunc_end(ip);
1066
1067 return error;
1068}
1069
Wendy Chenga13b8c52007-08-20 09:29:53 -04001070static int do_touch(struct gfs2_inode *ip, u64 size)
1071{
1072 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1073 struct buffer_head *dibh;
1074 int error;
1075
1076 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1077 if (error)
1078 return error;
1079
1080 down_write(&ip->i_rw_mutex);
1081
1082 error = gfs2_meta_inode_buffer(ip, &dibh);
1083 if (error)
1084 goto do_touch_out;
1085
1086 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1087 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1088 gfs2_dinode_out(ip, dibh->b_data);
1089 brelse(dibh);
1090
1091do_touch_out:
1092 up_write(&ip->i_rw_mutex);
1093 gfs2_trans_end(sdp);
1094 return error;
1095}
1096
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097/**
Steven Whitehouse666a2c52006-01-18 10:29:04 +00001098 * gfs2_truncatei - make a file a given size
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 * @ip: the inode
1100 * @size: the size to make the file
1101 * @truncator: function to truncate the last partial block
1102 *
1103 * The file size can grow, shrink, or stay the same size.
1104 *
1105 * Returns: errno
1106 */
1107
Steven Whitehousecd915492006-09-04 12:49:07 -04001108int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109{
1110 int error;
1111
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001112 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 return -EINVAL;
1114
1115 if (size > ip->i_di.di_size)
1116 error = do_grow(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001117 else if (size < ip->i_di.di_size)
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001118 error = do_shrink(ip, size);
Wendy Chenga13b8c52007-08-20 09:29:53 -04001119 else
1120 /* update time stamps */
1121 error = do_touch(ip, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122
1123 return error;
1124}
1125
1126int gfs2_truncatei_resume(struct gfs2_inode *ip)
1127{
1128 int error;
1129 error = trunc_dealloc(ip, ip->i_di.di_size);
1130 if (!error)
1131 error = trunc_end(ip);
1132 return error;
1133}
1134
1135int gfs2_file_dealloc(struct gfs2_inode *ip)
1136{
1137 return trunc_dealloc(ip, 0);
1138}
1139
1140/**
1141 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1142 * @ip: the file
1143 * @len: the number of bytes to be written to the file
1144 * @data_blocks: returns the number of data blocks required
1145 * @ind_blocks: returns the number of indirect blocks required
1146 *
1147 */
1148
1149void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1150 unsigned int *data_blocks, unsigned int *ind_blocks)
1151{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001152 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153 unsigned int tmp;
1154
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001155 if (gfs2_is_dir(ip)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001156 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1158 } else {
1159 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1160 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1161 }
1162
1163 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001164 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 *ind_blocks += tmp;
1166 }
1167}
1168
1169/**
1170 * gfs2_write_alloc_required - figure out if a write will require an allocation
1171 * @ip: the file being written to
1172 * @offset: the offset to write to
1173 * @len: the number of bytes being written
1174 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1175 *
1176 * Returns: errno
1177 */
1178
Steven Whitehousecd915492006-09-04 12:49:07 -04001179int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 unsigned int len, int *alloc_required)
1181{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001182 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001183 struct buffer_head bh;
1184 unsigned int shift;
1185 u64 lblock, lblock_stop, size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186
1187 *alloc_required = 0;
1188
1189 if (!len)
1190 return 0;
1191
1192 if (gfs2_is_stuffed(ip)) {
1193 if (offset + len >
1194 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1195 *alloc_required = 1;
1196 return 0;
1197 }
1198
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001199 *alloc_required = 1;
1200 shift = sdp->sd_sb.sb_bsize_shift;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +00001201 if (gfs2_is_dir(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001202 unsigned int bsize = sdp->sd_jbsize;
1203 lblock = offset;
1204 do_div(lblock, bsize);
1205 lblock_stop = offset + len + bsize - 1;
1206 do_div(lblock_stop, bsize);
1207 } else {
Steven Whitehouse1af53572008-01-16 14:24:05 +00001208 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 lblock = offset >> shift;
1210 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001211 if (lblock_stop > end_of_file)
Bob Peterson05220532008-01-11 13:44:50 -06001212 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 }
1214
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001215 size = (lblock_stop - lblock) << shift;
1216 do {
1217 bh.b_state = 0;
1218 bh.b_size = size;
1219 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1220 if (!buffer_mapped(&bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221 return 0;
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001222 size -= bh.b_size;
1223 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1224 } while(size > 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225
Steven Whitehouse941e6d72008-01-28 08:47:38 +00001226 *alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 return 0;
1228}
1229