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