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