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