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