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