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