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