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