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