| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved. | 
| Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 |  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved. | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 |  * | 
 | 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 Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 |  * of the GNU General Public License version 2. | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 |  */ | 
 | 9 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 10 | #include <linux/slab.h> | 
 | 11 | #include <linux/spinlock.h> | 
 | 12 | #include <linux/completion.h> | 
 | 13 | #include <linux/buffer_head.h> | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 14 | #include <linux/gfs2_ondisk.h> | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 15 | #include <linux/crc32.h> | 
| Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 16 | #include <linux/lm_interface.h> | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 17 |  | 
 | 18 | #include "gfs2.h" | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include "incore.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 20 | #include "bmap.h" | 
 | 21 | #include "glock.h" | 
 | 22 | #include "inode.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 23 | #include "meta_io.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "quota.h" | 
 | 25 | #include "rgrp.h" | 
 | 26 | #include "trans.h" | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 27 | #include "dir.h" | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "util.h" | 
| Steven Whitehouse | ba7f729 | 2006-07-26 11:27:10 -0400 | [diff] [blame] | 29 | #include "ops_address.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 |  | 
 | 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 |  */ | 
 | 35 | struct metapath { | 
 | 36 | 	__u16 mp_list[GFS2_MAX_META_HEIGHT]; | 
 | 37 | }; | 
 | 38 |  | 
 | 39 | typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh, | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 40 | 			     struct buffer_head *bh, __be64 *top, | 
 | 41 | 			     __be64 *bottom, unsigned int height, | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 42 | 			     void *data); | 
 | 43 |  | 
 | 44 | struct strip_mine { | 
 | 45 | 	int sm_first; | 
 | 46 | 	unsigned int sm_height; | 
 | 47 | }; | 
 | 48 |  | 
 | 49 | /** | 
| Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 50 |  * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page | 
 | 51 |  * @ip: the inode | 
 | 52 |  * @dibh: the dinode buffer | 
 | 53 |  * @block: the block number that was allocated | 
 | 54 |  * @private: any locked page held by the caller process | 
 | 55 |  * | 
 | 56 |  * Returns: errno | 
 | 57 |  */ | 
 | 58 |  | 
 | 59 | static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh, | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 60 | 			       u64 block, struct page *page) | 
| Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 61 | { | 
 | 62 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
 | 63 | 	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 Whitehouse | 75d3b81 | 2006-09-04 11:41:31 -0400 | [diff] [blame] | 96 | 	if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) | 
| Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 97 | 		gfs2_trans_add_bh(ip->i_gl, bh, 0); | 
 | 98 | 	mark_buffer_dirty(bh); | 
 | 99 |  | 
 | 100 | 	if (release) { | 
 | 101 | 		unlock_page(page); | 
 | 102 | 		page_cache_release(page); | 
 | 103 | 	} | 
 | 104 |  | 
 | 105 | 	return 0; | 
 | 106 | } | 
 | 107 |  | 
 | 108 | /** | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 109 |  * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big | 
 | 110 |  * @ip: The GFS2 inode to unstuff | 
 | 111 |  * @unstuffer: the routine that handles unstuffing a non-zero length file | 
 | 112 |  * @private: private data for the unstuffer | 
 | 113 |  * | 
 | 114 |  * This routine unstuffs a dinode and returns it to a "normal" state such | 
 | 115 |  * that the height can be grown in the traditional way. | 
 | 116 |  * | 
 | 117 |  * Returns: errno | 
 | 118 |  */ | 
 | 119 |  | 
| Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 120 | int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | { | 
 | 122 | 	struct buffer_head *bh, *dibh; | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 123 | 	struct gfs2_dinode *di; | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 124 | 	u64 block = 0; | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 125 | 	int isdir = gfs2_is_dir(ip); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 126 | 	int error; | 
 | 127 |  | 
 | 128 | 	down_write(&ip->i_rw_mutex); | 
 | 129 |  | 
 | 130 | 	error = gfs2_meta_inode_buffer(ip, &dibh); | 
 | 131 | 	if (error) | 
 | 132 | 		goto out; | 
| Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 133 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 134 | 	if (ip->i_di.di_size) { | 
 | 135 | 		/* Get a free block, fill it with the stuffed data, | 
 | 136 | 		   and write it out to disk */ | 
 | 137 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 138 | 		if (isdir) { | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 139 | 			block = gfs2_alloc_meta(ip); | 
 | 140 |  | 
| Steven Whitehouse | 61e085a | 2006-04-24 10:07:13 -0400 | [diff] [blame] | 141 | 			error = gfs2_dir_get_new_buffer(ip, block, &bh); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 142 | 			if (error) | 
 | 143 | 				goto out_brelse; | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 144 | 			gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header), | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 145 | 					      dibh, sizeof(struct gfs2_dinode)); | 
 | 146 | 			brelse(bh); | 
 | 147 | 		} else { | 
 | 148 | 			block = gfs2_alloc_data(ip); | 
 | 149 |  | 
| Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 150 | 			error = gfs2_unstuffer_page(ip, dibh, block, page); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 151 | 			if (error) | 
 | 152 | 				goto out_brelse; | 
 | 153 | 		} | 
 | 154 | 	} | 
 | 155 |  | 
 | 156 | 	/*  Set up the pointer to the new block  */ | 
 | 157 |  | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 158 | 	gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 159 | 	di = (struct gfs2_dinode *)dibh->b_data; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 160 | 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | 
 | 161 |  | 
 | 162 | 	if (ip->i_di.di_size) { | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 163 | 		*(__be64 *)(di + 1) = cpu_to_be64(block); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 164 | 		ip->i_di.di_blocks++; | 
| Steven Whitehouse | 9e2dbda | 2006-11-08 15:45:46 -0500 | [diff] [blame] | 165 | 		gfs2_set_inode_blocks(&ip->i_inode); | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 166 | 		di->di_blocks = cpu_to_be64(ip->i_di.di_blocks); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 167 | 	} | 
 | 168 |  | 
 | 169 | 	ip->i_di.di_height = 1; | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 170 | 	di->di_height = cpu_to_be16(1); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 171 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 172 | out_brelse: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 173 | 	brelse(dibh); | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 174 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 175 | 	up_write(&ip->i_rw_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 176 | 	return error; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | /** | 
 | 180 |  * calc_tree_height - Calculate the height of a metadata tree | 
 | 181 |  * @ip: The GFS2 inode | 
 | 182 |  * @size: The proposed size of the file | 
 | 183 |  * | 
 | 184 |  * Work out how tall a metadata tree needs to be in order to accommodate a | 
 | 185 |  * file of a particular size. If size is less than the current size of | 
 | 186 |  * the inode, then the current size of the inode is used instead of the | 
 | 187 |  * supplied one. | 
 | 188 |  * | 
 | 189 |  * Returns: the height the tree should be | 
 | 190 |  */ | 
 | 191 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 192 | static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 193 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 194 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 195 | 	u64 *arr; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 196 | 	unsigned int max, height; | 
 | 197 |  | 
 | 198 | 	if (ip->i_di.di_size > size) | 
 | 199 | 		size = ip->i_di.di_size; | 
 | 200 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 201 | 	if (gfs2_is_dir(ip)) { | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 202 | 		arr = sdp->sd_jheightsize; | 
 | 203 | 		max = sdp->sd_max_jheight; | 
 | 204 | 	} else { | 
 | 205 | 		arr = sdp->sd_heightsize; | 
 | 206 | 		max = sdp->sd_max_height; | 
 | 207 | 	} | 
 | 208 |  | 
 | 209 | 	for (height = 0; height < max; height++) | 
 | 210 | 		if (arr[height] >= size) | 
 | 211 | 			break; | 
 | 212 |  | 
 | 213 | 	return height; | 
 | 214 | } | 
 | 215 |  | 
 | 216 | /** | 
 | 217 |  * build_height - Build a metadata tree of the requested height | 
 | 218 |  * @ip: The GFS2 inode | 
 | 219 |  * @height: The height to build to | 
 | 220 |  * | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 221 |  * | 
 | 222 |  * Returns: errno | 
 | 223 |  */ | 
 | 224 |  | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 225 | static int build_height(struct inode *inode, unsigned height) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 226 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 227 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 228 | 	unsigned new_height = height - ip->i_di.di_height; | 
 | 229 | 	struct buffer_head *dibh; | 
 | 230 | 	struct buffer_head *blocks[GFS2_MAX_META_HEIGHT]; | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 231 | 	struct gfs2_dinode *di; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 232 | 	int error; | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 233 | 	__be64 *bp; | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 234 | 	u64 bn; | 
 | 235 | 	unsigned n; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 |  | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 237 | 	if (height <= ip->i_di.di_height) | 
 | 238 | 		return 0; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 239 |  | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 240 | 	error = gfs2_meta_inode_buffer(ip, &dibh); | 
 | 241 | 	if (error) | 
 | 242 | 		return error; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 243 |  | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 244 | 	for(n = 0; n < new_height; n++) { | 
 | 245 | 		bn = gfs2_alloc_meta(ip); | 
 | 246 | 		blocks[n] = gfs2_meta_new(ip->i_gl, bn); | 
 | 247 | 		gfs2_trans_add_bh(ip->i_gl, blocks[n], 1); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 248 | 	} | 
| Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 249 |  | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 250 | 	n = 0; | 
 | 251 | 	bn = blocks[0]->b_blocknr; | 
 | 252 | 	if (new_height > 1) { | 
 | 253 | 		for(; n < new_height-1; n++) { | 
 | 254 | 			gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, | 
 | 255 | 					  GFS2_FORMAT_IN); | 
 | 256 | 			gfs2_buffer_clear_tail(blocks[n], | 
 | 257 | 					       sizeof(struct gfs2_meta_header)); | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 258 | 			bp = (__be64 *)(blocks[n]->b_data + | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 259 | 				     sizeof(struct gfs2_meta_header)); | 
 | 260 | 			*bp = cpu_to_be64(blocks[n+1]->b_blocknr); | 
 | 261 | 			brelse(blocks[n]); | 
 | 262 | 			blocks[n] = NULL; | 
 | 263 | 		} | 
 | 264 | 	} | 
 | 265 | 	gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN); | 
 | 266 | 	gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header), | 
 | 267 | 			      dibh, sizeof(struct gfs2_dinode)); | 
 | 268 | 	brelse(blocks[n]); | 
 | 269 | 	gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 270 | 	di = (struct gfs2_dinode *)dibh->b_data; | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 271 | 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 272 | 	*(__be64 *)(di + 1) = cpu_to_be64(bn); | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 273 | 	ip->i_di.di_height += new_height; | 
 | 274 | 	ip->i_di.di_blocks += new_height; | 
| Steven Whitehouse | 9e2dbda | 2006-11-08 15:45:46 -0500 | [diff] [blame] | 275 | 	gfs2_set_inode_blocks(&ip->i_inode); | 
| Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 276 | 	di->di_height = cpu_to_be16(ip->i_di.di_height); | 
 | 277 | 	di->di_blocks = cpu_to_be64(ip->i_di.di_blocks); | 
| Steven Whitehouse | e90c01e | 2006-05-12 12:09:15 -0400 | [diff] [blame] | 278 | 	brelse(dibh); | 
 | 279 | 	return error; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 280 | } | 
 | 281 |  | 
 | 282 | /** | 
 | 283 |  * find_metapath - Find path through the metadata tree | 
 | 284 |  * @ip: The inode pointer | 
 | 285 |  * @mp: The metapath to return the result in | 
 | 286 |  * @block: The disk block to look up | 
 | 287 |  * | 
 | 288 |  *   This routine returns a struct metapath structure that defines a path | 
 | 289 |  *   through the metadata of inode "ip" to get to block "block". | 
 | 290 |  * | 
 | 291 |  *   Example: | 
 | 292 |  *   Given:  "ip" is a height 3 file, "offset" is 101342453, and this is a | 
 | 293 |  *   filesystem with a blocksize of 4096. | 
 | 294 |  * | 
 | 295 |  *   find_metapath() would return a struct metapath structure set to: | 
 | 296 |  *   mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48, | 
 | 297 |  *   and mp_list[2] = 165. | 
 | 298 |  * | 
 | 299 |  *   That means that in order to get to the block containing the byte at | 
 | 300 |  *   offset 101342453, we would load the indirect block pointed to by pointer | 
 | 301 |  *   0 in the dinode.  We would then load the indirect block pointed to by | 
 | 302 |  *   pointer 48 in that indirect block.  We would then load the data block | 
 | 303 |  *   pointed to by pointer 165 in that indirect block. | 
 | 304 |  * | 
 | 305 |  *             ---------------------------------------- | 
 | 306 |  *             | Dinode |                             | | 
 | 307 |  *             |        |                            4| | 
 | 308 |  *             |        |0 1 2 3 4 5                 9| | 
 | 309 |  *             |        |                            6| | 
 | 310 |  *             ---------------------------------------- | 
 | 311 |  *                       | | 
 | 312 |  *                       | | 
 | 313 |  *                       V | 
 | 314 |  *             ---------------------------------------- | 
 | 315 |  *             | Indirect Block                       | | 
 | 316 |  *             |                                     5| | 
 | 317 |  *             |            4 4 4 4 4 5 5            1| | 
 | 318 |  *             |0           5 6 7 8 9 0 1            2| | 
 | 319 |  *             ---------------------------------------- | 
 | 320 |  *                                | | 
 | 321 |  *                                | | 
 | 322 |  *                                V | 
 | 323 |  *             ---------------------------------------- | 
 | 324 |  *             | Indirect Block                       | | 
 | 325 |  *             |                         1 1 1 1 1   5| | 
 | 326 |  *             |                         6 6 6 6 6   1| | 
 | 327 |  *             |0                        3 4 5 6 7   2| | 
 | 328 |  *             ---------------------------------------- | 
 | 329 |  *                                           | | 
 | 330 |  *                                           | | 
 | 331 |  *                                           V | 
 | 332 |  *             ---------------------------------------- | 
 | 333 |  *             | Data block containing offset         | | 
 | 334 |  *             |            101342453                 | | 
 | 335 |  *             |                                      | | 
 | 336 |  *             |                                      | | 
 | 337 |  *             ---------------------------------------- | 
 | 338 |  * | 
 | 339 |  */ | 
 | 340 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 341 | static void find_metapath(struct gfs2_inode *ip, u64 block, | 
| Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 342 | 			  struct metapath *mp) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 343 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 344 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 345 | 	u64 b = block; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 346 | 	unsigned int i; | 
 | 347 |  | 
 | 348 | 	for (i = ip->i_di.di_height; i--;) | 
| Steven Whitehouse | c266871 | 2006-09-04 13:55:48 -0400 | [diff] [blame] | 349 | 		mp->mp_list[i] = do_div(b, sdp->sd_inptrs); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 350 |  | 
 | 351 | } | 
 | 352 |  | 
 | 353 | /** | 
 | 354 |  * metapointer - Return pointer to start of metadata in a buffer | 
 | 355 |  * @bh: The buffer | 
 | 356 |  * @height: The metadata height (0 = dinode) | 
 | 357 |  * @mp: The metapath | 
 | 358 |  * | 
 | 359 |  * Return a pointer to the block number of the next height of the metadata | 
 | 360 |  * tree given a buffer containing the pointer to the current height of the | 
 | 361 |  * metadata tree. | 
 | 362 |  */ | 
 | 363 |  | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 364 | static inline __be64 *metapointer(struct buffer_head *bh, int *boundary, | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 365 | 			       unsigned int height, const struct metapath *mp) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 | { | 
 | 367 | 	unsigned int head_size = (height > 0) ? | 
 | 368 | 		sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode); | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 369 | 	__be64 *ptr; | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 370 | 	*boundary = 0; | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 371 | 	ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height]; | 
 | 372 | 	if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size)) | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 373 | 		*boundary = 1; | 
 | 374 | 	return ptr; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 375 | } | 
 | 376 |  | 
 | 377 | /** | 
 | 378 |  * lookup_block - Get the next metadata block in metadata tree | 
 | 379 |  * @ip: The GFS2 inode | 
 | 380 |  * @bh: Buffer containing the pointers to metadata blocks | 
 | 381 |  * @height: The height of the tree (0 = dinode) | 
 | 382 |  * @mp: The metapath | 
 | 383 |  * @create: Non-zero if we may create a new meatdata block | 
 | 384 |  * @new: Used to indicate if we did create a new metadata block | 
 | 385 |  * @block: the returned disk block number | 
 | 386 |  * | 
 | 387 |  * Given a metatree, complete to a particular height, checks to see if the next | 
 | 388 |  * height of the tree exists. If not the next height of the tree is created. | 
 | 389 |  * The block number of the next height of the metadata tree is returned. | 
 | 390 |  * | 
 | 391 |  */ | 
 | 392 |  | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 393 | static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh, | 
 | 394 | 			unsigned int height, struct metapath *mp, int create, | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 395 | 			int *new, u64 *block) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 396 | { | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 397 | 	int boundary; | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 398 | 	__be64 *ptr = metapointer(bh, &boundary, height, mp); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 399 |  | 
 | 400 | 	if (*ptr) { | 
 | 401 | 		*block = be64_to_cpu(*ptr); | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 402 | 		return boundary; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 403 | 	} | 
 | 404 |  | 
 | 405 | 	*block = 0; | 
 | 406 |  | 
 | 407 | 	if (!create) | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 408 | 		return 0; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 409 |  | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 410 | 	if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip)) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 411 | 		*block = gfs2_alloc_data(ip); | 
 | 412 | 	else | 
 | 413 | 		*block = gfs2_alloc_meta(ip); | 
 | 414 |  | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 415 | 	gfs2_trans_add_bh(ip->i_gl, bh, 1); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 416 |  | 
 | 417 | 	*ptr = cpu_to_be64(*block); | 
 | 418 | 	ip->i_di.di_blocks++; | 
| Steven Whitehouse | 9e2dbda | 2006-11-08 15:45:46 -0500 | [diff] [blame] | 419 | 	gfs2_set_inode_blocks(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 420 |  | 
 | 421 | 	*new = 1; | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 422 | 	return 0; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | } | 
 | 424 |  | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 425 | static inline void bmap_lock(struct inode *inode, int create) | 
 | 426 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 427 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 428 | 	if (create) | 
 | 429 | 		down_write(&ip->i_rw_mutex); | 
 | 430 | 	else | 
 | 431 | 		down_read(&ip->i_rw_mutex); | 
 | 432 | } | 
 | 433 |  | 
 | 434 | static inline void bmap_unlock(struct inode *inode, int create) | 
 | 435 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 436 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 437 | 	if (create) | 
 | 438 | 		up_write(&ip->i_rw_mutex); | 
 | 439 | 	else | 
 | 440 | 		up_read(&ip->i_rw_mutex); | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 441 | } | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 442 |  | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 443 | /** | 
 | 444 |  * gfs2_block_map - Map a block from an inode to a disk block | 
 | 445 |  * @inode: The inode | 
 | 446 |  * @lblock: The logical block number | 
 | 447 |  * @bh_map: The bh to be mapped | 
 | 448 |  * | 
 | 449 |  * Find the block number on the current device which corresponds to an | 
 | 450 |  * inode's block. If the block had to be created, "new" will be set. | 
 | 451 |  * | 
 | 452 |  * Returns: errno | 
 | 453 |  */ | 
 | 454 |  | 
| Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 455 | int gfs2_block_map(struct inode *inode, u64 lblock, int create, | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 456 | 		   struct buffer_head *bh_map) | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 457 | { | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 458 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
 | 459 | 	struct gfs2_sbd *sdp = GFS2_SB(inode); | 
 | 460 | 	struct buffer_head *bh; | 
 | 461 | 	unsigned int bsize; | 
 | 462 | 	unsigned int height; | 
 | 463 | 	unsigned int end_of_metadata; | 
 | 464 | 	unsigned int x; | 
 | 465 | 	int error = 0; | 
 | 466 | 	int new = 0; | 
 | 467 | 	u64 dblock = 0; | 
 | 468 | 	int boundary; | 
 | 469 | 	unsigned int maxlen = bh_map->b_size >> inode->i_blkbits; | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 470 | 	struct metapath mp; | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 471 | 	u64 size; | 
 | 472 |  | 
 | 473 | 	BUG_ON(maxlen == 0); | 
 | 474 |  | 
 | 475 | 	if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip))) | 
 | 476 | 		return 0; | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 477 |  | 
 | 478 | 	bmap_lock(inode, create); | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 479 | 	clear_buffer_mapped(bh_map); | 
 | 480 | 	clear_buffer_new(bh_map); | 
 | 481 | 	clear_buffer_boundary(bh_map); | 
 | 482 | 	bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize; | 
 | 483 | 	size = (lblock + 1) * bsize; | 
 | 484 |  | 
 | 485 | 	if (size > ip->i_di.di_size) { | 
 | 486 | 		height = calc_tree_height(ip, size); | 
 | 487 | 		if (ip->i_di.di_height < height) { | 
 | 488 | 			if (!create) | 
 | 489 | 				goto out_ok; | 
 | 490 | 	 | 
 | 491 | 			error = build_height(inode, height); | 
 | 492 | 			if (error) | 
 | 493 | 				goto out_fail; | 
 | 494 | 		} | 
 | 495 | 	} | 
 | 496 |  | 
 | 497 | 	find_metapath(ip, lblock, &mp); | 
 | 498 | 	end_of_metadata = ip->i_di.di_height - 1; | 
 | 499 | 	error = gfs2_meta_inode_buffer(ip, &bh); | 
 | 500 | 	if (error) | 
 | 501 | 		goto out_fail; | 
 | 502 |  | 
 | 503 | 	for (x = 0; x < end_of_metadata; x++) { | 
 | 504 | 		lookup_block(ip, bh, x, &mp, create, &new, &dblock); | 
 | 505 | 		brelse(bh); | 
 | 506 | 		if (!dblock) | 
 | 507 | 			goto out_ok; | 
 | 508 |  | 
 | 509 | 		error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh); | 
 | 510 | 		if (error) | 
 | 511 | 			goto out_fail; | 
 | 512 | 	} | 
 | 513 |  | 
 | 514 | 	boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock); | 
 | 515 | 	if (dblock) { | 
 | 516 | 		map_bh(bh_map, inode->i_sb, dblock); | 
 | 517 | 		if (boundary) | 
 | 518 | 			set_buffer_boundary(bh_map); | 
 | 519 | 		if (new) { | 
 | 520 | 			struct buffer_head *dibh; | 
 | 521 | 			error = gfs2_meta_inode_buffer(ip, &dibh); | 
 | 522 | 			if (!error) { | 
 | 523 | 				gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
 | 524 | 				gfs2_dinode_out(ip, dibh->b_data); | 
 | 525 | 				brelse(dibh); | 
 | 526 | 			} | 
 | 527 | 			set_buffer_new(bh_map); | 
 | 528 | 			goto out_brelse; | 
 | 529 | 		} | 
 | 530 | 		while(--maxlen && !buffer_boundary(bh_map)) { | 
 | 531 | 			u64 eblock; | 
 | 532 |  | 
 | 533 | 			mp.mp_list[end_of_metadata]++; | 
 | 534 | 			boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock); | 
 | 535 | 			if (eblock != ++dblock) | 
 | 536 | 				break; | 
 | 537 | 			bh_map->b_size += (1 << inode->i_blkbits); | 
 | 538 | 			if (boundary) | 
 | 539 | 				set_buffer_boundary(bh_map); | 
 | 540 | 		} | 
 | 541 | 	} | 
 | 542 | out_brelse: | 
 | 543 | 	brelse(bh); | 
 | 544 | out_ok: | 
 | 545 | 	error = 0; | 
 | 546 | out_fail: | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 547 | 	bmap_unlock(inode, create); | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 548 | 	return error; | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 549 | } | 
 | 550 |  | 
 | 551 | int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen) | 
 | 552 | { | 
| Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 553 | 	struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 }; | 
| Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 554 | 	int ret; | 
| Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 555 | 	int create = *new; | 
 | 556 |  | 
 | 557 | 	BUG_ON(!extlen); | 
 | 558 | 	BUG_ON(!dblock); | 
 | 559 | 	BUG_ON(!new); | 
 | 560 |  | 
| Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 561 | 	bh.b_size = 1 << (inode->i_blkbits + 5); | 
| Steven Whitehouse | 4cf1ed8 | 2006-11-15 15:21:06 -0500 | [diff] [blame] | 562 | 	ret = gfs2_block_map(inode, lblock, create, &bh); | 
| Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 563 | 	*extlen = bh.b_size >> inode->i_blkbits; | 
 | 564 | 	*dblock = bh.b_blocknr; | 
 | 565 | 	if (buffer_new(&bh)) | 
 | 566 | 		*new = 1; | 
 | 567 | 	else | 
 | 568 | 		*new = 0; | 
 | 569 | 	return ret; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 570 | } | 
 | 571 |  | 
 | 572 | /** | 
 | 573 |  * recursive_scan - recursively scan through the end of a file | 
 | 574 |  * @ip: the inode | 
 | 575 |  * @dibh: the dinode buffer | 
 | 576 |  * @mp: the path through the metadata to the point to start | 
 | 577 |  * @height: the height the recursion is at | 
 | 578 |  * @block: the indirect block to look at | 
 | 579 |  * @first: 1 if this is the first block | 
 | 580 |  * @bc: the call to make for each piece of metadata | 
 | 581 |  * @data: data opaque to this function to pass to @bc | 
 | 582 |  * | 
 | 583 |  * When this is first called @height and @block should be zero and | 
 | 584 |  * @first should be 1. | 
 | 585 |  * | 
 | 586 |  * Returns: errno | 
 | 587 |  */ | 
 | 588 |  | 
 | 589 | static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh, | 
 | 590 | 			  struct metapath *mp, unsigned int height, | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 591 | 			  u64 block, int first, block_call_t bc, | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 592 | 			  void *data) | 
 | 593 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 594 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 595 | 	struct buffer_head *bh = NULL; | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 596 | 	__be64 *top, *bottom; | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 597 | 	u64 bn; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 598 | 	int error; | 
 | 599 | 	int mh_size = sizeof(struct gfs2_meta_header); | 
 | 600 |  | 
 | 601 | 	if (!height) { | 
 | 602 | 		error = gfs2_meta_inode_buffer(ip, &bh); | 
 | 603 | 		if (error) | 
 | 604 | 			return error; | 
 | 605 | 		dibh = bh; | 
 | 606 |  | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 607 | 		top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0]; | 
 | 608 | 		bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 609 | 	} else { | 
 | 610 | 		error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh); | 
 | 611 | 		if (error) | 
 | 612 | 			return error; | 
 | 613 |  | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 614 | 		top = (__be64 *)(bh->b_data + mh_size) + | 
| Jan Engelhardt | c539212 | 2006-09-05 14:30:40 +0200 | [diff] [blame] | 615 | 				  (first ? mp->mp_list[height] : 0); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 616 |  | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 617 | 		bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 618 | 	} | 
 | 619 |  | 
 | 620 | 	error = bc(ip, dibh, bh, top, bottom, height, data); | 
 | 621 | 	if (error) | 
 | 622 | 		goto out; | 
 | 623 |  | 
 | 624 | 	if (height < ip->i_di.di_height - 1) | 
 | 625 | 		for (; top < bottom; top++, first = 0) { | 
 | 626 | 			if (!*top) | 
 | 627 | 				continue; | 
 | 628 |  | 
 | 629 | 			bn = be64_to_cpu(*top); | 
 | 630 |  | 
 | 631 | 			error = recursive_scan(ip, dibh, mp, height + 1, bn, | 
 | 632 | 					       first, bc, data); | 
 | 633 | 			if (error) | 
 | 634 | 				break; | 
 | 635 | 		} | 
 | 636 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 637 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 638 | 	brelse(bh); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 639 | 	return error; | 
 | 640 | } | 
 | 641 |  | 
 | 642 | /** | 
 | 643 |  * do_strip - Look for a layer a particular layer of the file and strip it off | 
 | 644 |  * @ip: the inode | 
 | 645 |  * @dibh: the dinode buffer | 
 | 646 |  * @bh: A buffer of pointers | 
 | 647 |  * @top: The first pointer in the buffer | 
 | 648 |  * @bottom: One more than the last pointer | 
 | 649 |  * @height: the height this buffer is at | 
 | 650 |  * @data: a pointer to a struct strip_mine | 
 | 651 |  * | 
 | 652 |  * Returns: errno | 
 | 653 |  */ | 
 | 654 |  | 
 | 655 | static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh, | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 656 | 		    struct buffer_head *bh, __be64 *top, __be64 *bottom, | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 657 | 		    unsigned int height, void *data) | 
 | 658 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 659 | 	struct strip_mine *sm = data; | 
 | 660 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 661 | 	struct gfs2_rgrp_list rlist; | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 662 | 	u64 bn, bstart; | 
 | 663 | 	u32 blen; | 
| Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 664 | 	__be64 *p; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 665 | 	unsigned int rg_blocks = 0; | 
 | 666 | 	int metadata; | 
 | 667 | 	unsigned int revokes = 0; | 
 | 668 | 	int x; | 
 | 669 | 	int error; | 
 | 670 |  | 
 | 671 | 	if (!*top) | 
 | 672 | 		sm->sm_first = 0; | 
 | 673 |  | 
 | 674 | 	if (height != sm->sm_height) | 
 | 675 | 		return 0; | 
 | 676 |  | 
 | 677 | 	if (sm->sm_first) { | 
 | 678 | 		top++; | 
 | 679 | 		sm->sm_first = 0; | 
 | 680 | 	} | 
 | 681 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 682 | 	metadata = (height != ip->i_di.di_height - 1); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 683 | 	if (metadata) | 
 | 684 | 		revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs; | 
 | 685 |  | 
 | 686 | 	error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh); | 
 | 687 | 	if (error) | 
 | 688 | 		return error; | 
 | 689 |  | 
 | 690 | 	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); | 
 | 691 | 	bstart = 0; | 
 | 692 | 	blen = 0; | 
 | 693 |  | 
 | 694 | 	for (p = top; p < bottom; p++) { | 
 | 695 | 		if (!*p) | 
 | 696 | 			continue; | 
 | 697 |  | 
 | 698 | 		bn = be64_to_cpu(*p); | 
 | 699 |  | 
 | 700 | 		if (bstart + blen == bn) | 
 | 701 | 			blen++; | 
 | 702 | 		else { | 
 | 703 | 			if (bstart) | 
 | 704 | 				gfs2_rlist_add(sdp, &rlist, bstart); | 
 | 705 |  | 
 | 706 | 			bstart = bn; | 
 | 707 | 			blen = 1; | 
 | 708 | 		} | 
 | 709 | 	} | 
 | 710 |  | 
 | 711 | 	if (bstart) | 
 | 712 | 		gfs2_rlist_add(sdp, &rlist, bstart); | 
 | 713 | 	else | 
 | 714 | 		goto out; /* Nothing to do */ | 
 | 715 |  | 
 | 716 | 	gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0); | 
 | 717 |  | 
 | 718 | 	for (x = 0; x < rlist.rl_rgrps; x++) { | 
 | 719 | 		struct gfs2_rgrpd *rgd; | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 720 | 		rgd = rlist.rl_ghs[x].gh_gl->gl_object; | 
| Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 721 | 		rg_blocks += rgd->rd_length; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | 	} | 
 | 723 |  | 
 | 724 | 	error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); | 
 | 725 | 	if (error) | 
 | 726 | 		goto out_rlist; | 
 | 727 |  | 
 | 728 | 	error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + | 
 | 729 | 				 RES_INDIRECT + RES_STATFS + RES_QUOTA, | 
 | 730 | 				 revokes); | 
 | 731 | 	if (error) | 
 | 732 | 		goto out_rg_gunlock; | 
 | 733 |  | 
 | 734 | 	down_write(&ip->i_rw_mutex); | 
 | 735 |  | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 736 | 	gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
 | 737 | 	gfs2_trans_add_bh(ip->i_gl, bh, 1); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 738 |  | 
 | 739 | 	bstart = 0; | 
 | 740 | 	blen = 0; | 
 | 741 |  | 
 | 742 | 	for (p = top; p < bottom; p++) { | 
 | 743 | 		if (!*p) | 
 | 744 | 			continue; | 
 | 745 |  | 
 | 746 | 		bn = be64_to_cpu(*p); | 
 | 747 |  | 
 | 748 | 		if (bstart + blen == bn) | 
 | 749 | 			blen++; | 
 | 750 | 		else { | 
 | 751 | 			if (bstart) { | 
 | 752 | 				if (metadata) | 
 | 753 | 					gfs2_free_meta(ip, bstart, blen); | 
 | 754 | 				else | 
 | 755 | 					gfs2_free_data(ip, bstart, blen); | 
 | 756 | 			} | 
 | 757 |  | 
 | 758 | 			bstart = bn; | 
 | 759 | 			blen = 1; | 
 | 760 | 		} | 
 | 761 |  | 
 | 762 | 		*p = 0; | 
 | 763 | 		if (!ip->i_di.di_blocks) | 
 | 764 | 			gfs2_consist_inode(ip); | 
 | 765 | 		ip->i_di.di_blocks--; | 
| Steven Whitehouse | 9e2dbda | 2006-11-08 15:45:46 -0500 | [diff] [blame] | 766 | 		gfs2_set_inode_blocks(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 767 | 	} | 
 | 768 | 	if (bstart) { | 
 | 769 | 		if (metadata) | 
 | 770 | 			gfs2_free_meta(ip, bstart, blen); | 
 | 771 | 		else | 
 | 772 | 			gfs2_free_data(ip, bstart, blen); | 
 | 773 | 	} | 
 | 774 |  | 
| Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 775 | 	ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 776 |  | 
| Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 777 | 	gfs2_dinode_out(ip, dibh->b_data); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 778 |  | 
 | 779 | 	up_write(&ip->i_rw_mutex); | 
 | 780 |  | 
 | 781 | 	gfs2_trans_end(sdp); | 
 | 782 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 783 | out_rg_gunlock: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 784 | 	gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 785 | out_rlist: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 786 | 	gfs2_rlist_free(&rlist); | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 787 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 788 | 	gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 789 | 	return error; | 
 | 790 | } | 
 | 791 |  | 
 | 792 | /** | 
 | 793 |  * do_grow - Make a file look bigger than it is | 
 | 794 |  * @ip: the inode | 
 | 795 |  * @size: the size to set the file to | 
 | 796 |  * | 
 | 797 |  * Called with an exclusive lock on @ip. | 
 | 798 |  * | 
 | 799 |  * Returns: errno | 
 | 800 |  */ | 
 | 801 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 802 | static int do_grow(struct gfs2_inode *ip, u64 size) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 803 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 804 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 805 | 	struct gfs2_alloc *al; | 
 | 806 | 	struct buffer_head *dibh; | 
 | 807 | 	unsigned int h; | 
 | 808 | 	int error; | 
 | 809 |  | 
 | 810 | 	al = gfs2_alloc_get(ip); | 
 | 811 |  | 
 | 812 | 	error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | 
 | 813 | 	if (error) | 
 | 814 | 		goto out; | 
 | 815 |  | 
| Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 816 | 	error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 817 | 	if (error) | 
 | 818 | 		goto out_gunlock_q; | 
 | 819 |  | 
 | 820 | 	al->al_requested = sdp->sd_max_height + RES_DATA; | 
 | 821 |  | 
 | 822 | 	error = gfs2_inplace_reserve(ip); | 
 | 823 | 	if (error) | 
 | 824 | 		goto out_gunlock_q; | 
 | 825 |  | 
 | 826 | 	error = gfs2_trans_begin(sdp, | 
| Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 827 | 			sdp->sd_max_height + al->al_rgd->rd_length + | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 828 | 			RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0); | 
 | 829 | 	if (error) | 
 | 830 | 		goto out_ipres; | 
 | 831 |  | 
 | 832 | 	if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { | 
 | 833 | 		if (gfs2_is_stuffed(ip)) { | 
| Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 834 | 			error = gfs2_unstuff_dinode(ip, NULL); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 835 | 			if (error) | 
 | 836 | 				goto out_end_trans; | 
 | 837 | 		} | 
 | 838 |  | 
 | 839 | 		h = calc_tree_height(ip, size); | 
 | 840 | 		if (ip->i_di.di_height < h) { | 
 | 841 | 			down_write(&ip->i_rw_mutex); | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 842 | 			error = build_height(&ip->i_inode, h); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 843 | 			up_write(&ip->i_rw_mutex); | 
 | 844 | 			if (error) | 
 | 845 | 				goto out_end_trans; | 
 | 846 | 		} | 
 | 847 | 	} | 
 | 848 |  | 
 | 849 | 	ip->i_di.di_size = size; | 
| Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 850 | 	ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 851 |  | 
 | 852 | 	error = gfs2_meta_inode_buffer(ip, &dibh); | 
 | 853 | 	if (error) | 
 | 854 | 		goto out_end_trans; | 
 | 855 |  | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 856 | 	gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
| Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 857 | 	gfs2_dinode_out(ip, dibh->b_data); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 858 | 	brelse(dibh); | 
 | 859 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 860 | out_end_trans: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 861 | 	gfs2_trans_end(sdp); | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 862 | out_ipres: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 863 | 	gfs2_inplace_release(ip); | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 864 | out_gunlock_q: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 865 | 	gfs2_quota_unlock(ip); | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 866 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 867 | 	gfs2_alloc_put(ip); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 868 | 	return error; | 
 | 869 | } | 
 | 870 |  | 
| Steven Whitehouse | ba7f729 | 2006-07-26 11:27:10 -0400 | [diff] [blame] | 871 |  | 
 | 872 | /** | 
 | 873 |  * gfs2_block_truncate_page - Deal with zeroing out data for truncate | 
 | 874 |  * | 
 | 875 |  * This is partly borrowed from ext3. | 
 | 876 |  */ | 
 | 877 | static int gfs2_block_truncate_page(struct address_space *mapping) | 
 | 878 | { | 
 | 879 | 	struct inode *inode = mapping->host; | 
 | 880 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
 | 881 | 	struct gfs2_sbd *sdp = GFS2_SB(inode); | 
 | 882 | 	loff_t from = inode->i_size; | 
 | 883 | 	unsigned long index = from >> PAGE_CACHE_SHIFT; | 
 | 884 | 	unsigned offset = from & (PAGE_CACHE_SIZE-1); | 
 | 885 | 	unsigned blocksize, iblock, length, pos; | 
 | 886 | 	struct buffer_head *bh; | 
 | 887 | 	struct page *page; | 
| Steven Whitehouse | ba7f729 | 2006-07-26 11:27:10 -0400 | [diff] [blame] | 888 | 	int err; | 
 | 889 |  | 
 | 890 | 	page = grab_cache_page(mapping, index); | 
 | 891 | 	if (!page) | 
 | 892 | 		return 0; | 
 | 893 |  | 
 | 894 | 	blocksize = inode->i_sb->s_blocksize; | 
 | 895 | 	length = blocksize - (offset & (blocksize - 1)); | 
 | 896 | 	iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | 
 | 897 |  | 
 | 898 | 	if (!page_has_buffers(page)) | 
 | 899 | 		create_empty_buffers(page, blocksize, 0); | 
 | 900 |  | 
 | 901 | 	/* Find the buffer that contains "offset" */ | 
 | 902 | 	bh = page_buffers(page); | 
 | 903 | 	pos = blocksize; | 
 | 904 | 	while (offset >= pos) { | 
 | 905 | 		bh = bh->b_this_page; | 
 | 906 | 		iblock++; | 
 | 907 | 		pos += blocksize; | 
 | 908 | 	} | 
 | 909 |  | 
 | 910 | 	err = 0; | 
 | 911 |  | 
 | 912 | 	if (!buffer_mapped(bh)) { | 
 | 913 | 		gfs2_get_block(inode, iblock, bh, 0); | 
 | 914 | 		/* unmapped? It's a hole - nothing to do */ | 
 | 915 | 		if (!buffer_mapped(bh)) | 
 | 916 | 			goto unlock; | 
 | 917 | 	} | 
 | 918 |  | 
 | 919 | 	/* Ok, it's mapped. Make sure it's up-to-date */ | 
 | 920 | 	if (PageUptodate(page)) | 
 | 921 | 		set_buffer_uptodate(bh); | 
 | 922 |  | 
 | 923 | 	if (!buffer_uptodate(bh)) { | 
 | 924 | 		err = -EIO; | 
 | 925 | 		ll_rw_block(READ, 1, &bh); | 
 | 926 | 		wait_on_buffer(bh); | 
 | 927 | 		/* Uhhuh. Read error. Complain and punt. */ | 
 | 928 | 		if (!buffer_uptodate(bh)) | 
 | 929 | 			goto unlock; | 
| S. Wendy Cheng | 1875f2f | 2007-06-25 21:14:31 -0400 | [diff] [blame] | 930 | 		err = 0; | 
| Steven Whitehouse | ba7f729 | 2006-07-26 11:27:10 -0400 | [diff] [blame] | 931 | 	} | 
 | 932 |  | 
 | 933 | 	if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) | 
 | 934 | 		gfs2_trans_add_bh(ip->i_gl, bh, 0); | 
 | 935 |  | 
| Nate Diller | 0507ecf | 2007-05-10 22:41:28 -0700 | [diff] [blame] | 936 | 	zero_user_page(page, offset, length, KM_USER0); | 
| Steven Whitehouse | ba7f729 | 2006-07-26 11:27:10 -0400 | [diff] [blame] | 937 |  | 
 | 938 | unlock: | 
 | 939 | 	unlock_page(page); | 
 | 940 | 	page_cache_release(page); | 
 | 941 | 	return err; | 
 | 942 | } | 
 | 943 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 944 | static int trunc_start(struct gfs2_inode *ip, u64 size) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 945 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 946 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 947 | 	struct buffer_head *dibh; | 
 | 948 | 	int journaled = gfs2_is_jdata(ip); | 
 | 949 | 	int error; | 
 | 950 |  | 
 | 951 | 	error = gfs2_trans_begin(sdp, | 
| Jan Engelhardt | c539212 | 2006-09-05 14:30:40 +0200 | [diff] [blame] | 952 | 				 RES_DINODE + (journaled ? RES_JDATA : 0), 0); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 953 | 	if (error) | 
 | 954 | 		return error; | 
 | 955 |  | 
 | 956 | 	error = gfs2_meta_inode_buffer(ip, &dibh); | 
 | 957 | 	if (error) | 
 | 958 | 		goto out; | 
 | 959 |  | 
 | 960 | 	if (gfs2_is_stuffed(ip)) { | 
 | 961 | 		ip->i_di.di_size = size; | 
| Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 962 | 		ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 963 | 		gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
| Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 964 | 		gfs2_dinode_out(ip, dibh->b_data); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 965 | 		gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size); | 
 | 966 | 		error = 1; | 
 | 967 |  | 
 | 968 | 	} else { | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 969 | 		if (size & (u64)(sdp->sd_sb.sb_bsize - 1)) | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 970 | 			error = gfs2_block_truncate_page(ip->i_inode.i_mapping); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 971 |  | 
 | 972 | 		if (!error) { | 
 | 973 | 			ip->i_di.di_size = size; | 
| Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 974 | 			ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 975 | 			ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG; | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 976 | 			gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
| Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 977 | 			gfs2_dinode_out(ip, dibh->b_data); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 978 | 		} | 
 | 979 | 	} | 
 | 980 |  | 
 | 981 | 	brelse(dibh); | 
 | 982 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 983 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 984 | 	gfs2_trans_end(sdp); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 985 | 	return error; | 
 | 986 | } | 
 | 987 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 988 | static int trunc_dealloc(struct gfs2_inode *ip, u64 size) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 989 | { | 
 | 990 | 	unsigned int height = ip->i_di.di_height; | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 991 | 	u64 lblock; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 992 | 	struct metapath mp; | 
 | 993 | 	int error; | 
 | 994 |  | 
 | 995 | 	if (!size) | 
 | 996 | 		lblock = 0; | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 997 | 	else | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 998 | 		lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 999 |  | 
 | 1000 | 	find_metapath(ip, lblock, &mp); | 
 | 1001 | 	gfs2_alloc_get(ip); | 
 | 1002 |  | 
 | 1003 | 	error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | 
 | 1004 | 	if (error) | 
 | 1005 | 		goto out; | 
 | 1006 |  | 
 | 1007 | 	while (height--) { | 
 | 1008 | 		struct strip_mine sm; | 
 | 1009 | 		sm.sm_first = !!size; | 
 | 1010 | 		sm.sm_height = height; | 
 | 1011 |  | 
 | 1012 | 		error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm); | 
 | 1013 | 		if (error) | 
 | 1014 | 			break; | 
 | 1015 | 	} | 
 | 1016 |  | 
 | 1017 | 	gfs2_quota_unhold(ip); | 
 | 1018 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1019 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1020 | 	gfs2_alloc_put(ip); | 
 | 1021 | 	return error; | 
 | 1022 | } | 
 | 1023 |  | 
 | 1024 | static int trunc_end(struct gfs2_inode *ip) | 
 | 1025 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1026 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1027 | 	struct buffer_head *dibh; | 
 | 1028 | 	int error; | 
 | 1029 |  | 
 | 1030 | 	error = gfs2_trans_begin(sdp, RES_DINODE, 0); | 
 | 1031 | 	if (error) | 
 | 1032 | 		return error; | 
 | 1033 |  | 
 | 1034 | 	down_write(&ip->i_rw_mutex); | 
 | 1035 |  | 
 | 1036 | 	error = gfs2_meta_inode_buffer(ip, &dibh); | 
 | 1037 | 	if (error) | 
 | 1038 | 		goto out; | 
 | 1039 |  | 
 | 1040 | 	if (!ip->i_di.di_size) { | 
 | 1041 | 		ip->i_di.di_height = 0; | 
 | 1042 | 		ip->i_di.di_goal_meta = | 
 | 1043 | 			ip->i_di.di_goal_data = | 
| Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1044 | 			ip->i_no_addr; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1045 | 		gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | 
 | 1046 | 	} | 
| Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 1047 | 	ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1048 | 	ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG; | 
 | 1049 |  | 
| Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1050 | 	gfs2_trans_add_bh(ip->i_gl, dibh, 1); | 
| Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1051 | 	gfs2_dinode_out(ip, dibh->b_data); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1052 | 	brelse(dibh); | 
 | 1053 |  | 
| Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1054 | out: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1055 | 	up_write(&ip->i_rw_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1056 | 	gfs2_trans_end(sdp); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1057 | 	return error; | 
 | 1058 | } | 
 | 1059 |  | 
 | 1060 | /** | 
 | 1061 |  * do_shrink - make a file smaller | 
 | 1062 |  * @ip: the inode | 
 | 1063 |  * @size: the size to make the file | 
 | 1064 |  * @truncator: function to truncate the last partial block | 
 | 1065 |  * | 
 | 1066 |  * Called with an exclusive lock on @ip. | 
 | 1067 |  * | 
 | 1068 |  * Returns: errno | 
 | 1069 |  */ | 
 | 1070 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1071 | static int do_shrink(struct gfs2_inode *ip, u64 size) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1072 | { | 
 | 1073 | 	int error; | 
 | 1074 |  | 
| Steven Whitehouse | aa6a85a | 2006-01-24 10:37:06 +0000 | [diff] [blame] | 1075 | 	error = trunc_start(ip, size); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1076 | 	if (error < 0) | 
 | 1077 | 		return error; | 
 | 1078 | 	if (error > 0) | 
 | 1079 | 		return 0; | 
 | 1080 |  | 
 | 1081 | 	error = trunc_dealloc(ip, size); | 
 | 1082 | 	if (!error) | 
 | 1083 | 		error = trunc_end(ip); | 
 | 1084 |  | 
 | 1085 | 	return error; | 
 | 1086 | } | 
 | 1087 |  | 
 | 1088 | /** | 
| Steven Whitehouse | 666a2c5 | 2006-01-18 10:29:04 +0000 | [diff] [blame] | 1089 |  * gfs2_truncatei - make a file a given size | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1090 |  * @ip: the inode | 
 | 1091 |  * @size: the size to make the file | 
 | 1092 |  * @truncator: function to truncate the last partial block | 
 | 1093 |  * | 
 | 1094 |  * The file size can grow, shrink, or stay the same size. | 
 | 1095 |  * | 
 | 1096 |  * Returns: errno | 
 | 1097 |  */ | 
 | 1098 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1099 | int gfs2_truncatei(struct gfs2_inode *ip, u64 size) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1100 | { | 
 | 1101 | 	int error; | 
 | 1102 |  | 
| Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 1103 | 	if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode))) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1104 | 		return -EINVAL; | 
 | 1105 |  | 
 | 1106 | 	if (size > ip->i_di.di_size) | 
 | 1107 | 		error = do_grow(ip, size); | 
 | 1108 | 	else | 
| Steven Whitehouse | aa6a85a | 2006-01-24 10:37:06 +0000 | [diff] [blame] | 1109 | 		error = do_shrink(ip, size); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1110 |  | 
 | 1111 | 	return error; | 
 | 1112 | } | 
 | 1113 |  | 
 | 1114 | int gfs2_truncatei_resume(struct gfs2_inode *ip) | 
 | 1115 | { | 
 | 1116 | 	int error; | 
 | 1117 | 	error = trunc_dealloc(ip, ip->i_di.di_size); | 
 | 1118 | 	if (!error) | 
 | 1119 | 		error = trunc_end(ip); | 
 | 1120 | 	return error; | 
 | 1121 | } | 
 | 1122 |  | 
 | 1123 | int gfs2_file_dealloc(struct gfs2_inode *ip) | 
 | 1124 | { | 
 | 1125 | 	return trunc_dealloc(ip, 0); | 
 | 1126 | } | 
 | 1127 |  | 
 | 1128 | /** | 
 | 1129 |  * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file | 
 | 1130 |  * @ip: the file | 
 | 1131 |  * @len: the number of bytes to be written to the file | 
 | 1132 |  * @data_blocks: returns the number of data blocks required | 
 | 1133 |  * @ind_blocks: returns the number of indirect blocks required | 
 | 1134 |  * | 
 | 1135 |  */ | 
 | 1136 |  | 
 | 1137 | void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len, | 
 | 1138 | 			    unsigned int *data_blocks, unsigned int *ind_blocks) | 
 | 1139 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1140 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1141 | 	unsigned int tmp; | 
 | 1142 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 1143 | 	if (gfs2_is_dir(ip)) { | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1144 | 		*data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1145 | 		*ind_blocks = 3 * (sdp->sd_max_jheight - 1); | 
 | 1146 | 	} else { | 
 | 1147 | 		*data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3; | 
 | 1148 | 		*ind_blocks = 3 * (sdp->sd_max_height - 1); | 
 | 1149 | 	} | 
 | 1150 |  | 
 | 1151 | 	for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) { | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1152 | 		tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1153 | 		*ind_blocks += tmp; | 
 | 1154 | 	} | 
 | 1155 | } | 
 | 1156 |  | 
 | 1157 | /** | 
 | 1158 |  * gfs2_write_alloc_required - figure out if a write will require an allocation | 
 | 1159 |  * @ip: the file being written to | 
 | 1160 |  * @offset: the offset to write to | 
 | 1161 |  * @len: the number of bytes being written | 
 | 1162 |  * @alloc_required: set to 1 if an alloc is required, 0 otherwise | 
 | 1163 |  * | 
 | 1164 |  * Returns: errno | 
 | 1165 |  */ | 
 | 1166 |  | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1167 | int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset, | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1168 | 			      unsigned int len, int *alloc_required) | 
 | 1169 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1170 | 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1171 | 	u64 lblock, lblock_stop, dblock; | 
 | 1172 | 	u32 extlen; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1173 | 	int new = 0; | 
 | 1174 | 	int error = 0; | 
 | 1175 |  | 
 | 1176 | 	*alloc_required = 0; | 
 | 1177 |  | 
 | 1178 | 	if (!len) | 
 | 1179 | 		return 0; | 
 | 1180 |  | 
 | 1181 | 	if (gfs2_is_stuffed(ip)) { | 
 | 1182 | 		if (offset + len > | 
 | 1183 | 		    sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) | 
 | 1184 | 			*alloc_required = 1; | 
 | 1185 | 		return 0; | 
 | 1186 | 	} | 
 | 1187 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 1188 | 	if (gfs2_is_dir(ip)) { | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1189 | 		unsigned int bsize = sdp->sd_jbsize; | 
 | 1190 | 		lblock = offset; | 
 | 1191 | 		do_div(lblock, bsize); | 
 | 1192 | 		lblock_stop = offset + len + bsize - 1; | 
 | 1193 | 		do_div(lblock_stop, bsize); | 
 | 1194 | 	} else { | 
 | 1195 | 		unsigned int shift = sdp->sd_sb.sb_bsize_shift; | 
 | 1196 | 		lblock = offset >> shift; | 
 | 1197 | 		lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; | 
 | 1198 | 	} | 
 | 1199 |  | 
 | 1200 | 	for (; lblock < lblock_stop; lblock += extlen) { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1201 | 		error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1202 | 		if (error) | 
 | 1203 | 			return error; | 
 | 1204 |  | 
 | 1205 | 		if (!dblock) { | 
 | 1206 | 			*alloc_required = 1; | 
 | 1207 | 			return 0; | 
 | 1208 | 		} | 
 | 1209 | 	} | 
 | 1210 |  | 
 | 1211 | 	return 0; | 
 | 1212 | } | 
 | 1213 |  |