Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * alloc.c |
| 5 | * |
| 6 | * Extent allocs and frees |
| 7 | * |
| 8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public |
| 21 | * License along with this program; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 021110-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/highmem.h> |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 30 | #include <linux/swap.h> |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 31 | #include <linux/quotaops.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 32 | |
| 33 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC |
| 34 | #include <cluster/masklog.h> |
| 35 | |
| 36 | #include "ocfs2.h" |
| 37 | |
| 38 | #include "alloc.h" |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 39 | #include "aops.h" |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 40 | #include "blockcheck.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 41 | #include "dlmglue.h" |
| 42 | #include "extent_map.h" |
| 43 | #include "inode.h" |
| 44 | #include "journal.h" |
| 45 | #include "localalloc.h" |
| 46 | #include "suballoc.h" |
| 47 | #include "sysfile.h" |
| 48 | #include "file.h" |
| 49 | #include "super.h" |
| 50 | #include "uptodate.h" |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 51 | #include "xattr.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 52 | |
| 53 | #include "buffer_head_io.h" |
| 54 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 55 | |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Operations for a specific extent tree type. |
| 58 | * |
| 59 | * To implement an on-disk btree (extent tree) type in ocfs2, add |
| 60 | * an ocfs2_extent_tree_operations structure and the matching |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 61 | * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 62 | * for the allocation portion of the extent tree. |
| 63 | */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 64 | struct ocfs2_extent_tree_operations { |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 65 | /* |
| 66 | * last_eb_blk is the block number of the right most leaf extent |
| 67 | * block. Most on-disk structures containing an extent tree store |
| 68 | * this value for fast access. The ->eo_set_last_eb_blk() and |
| 69 | * ->eo_get_last_eb_blk() operations access this value. They are |
| 70 | * both required. |
| 71 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 72 | void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et, |
| 73 | u64 blkno); |
| 74 | u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * The on-disk structure usually keeps track of how many total |
| 78 | * clusters are stored in this extent tree. This function updates |
| 79 | * that value. new_clusters is the delta, and must be |
| 80 | * added to the total. Required. |
| 81 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 82 | void (*eo_update_clusters)(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 83 | u32 new_clusters); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * If ->eo_insert_check() exists, it is called before rec is |
| 87 | * inserted into the extent tree. It is optional. |
| 88 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 89 | int (*eo_insert_check)(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 90 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 91 | int (*eo_sanity_check)(struct ocfs2_extent_tree *et); |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 92 | |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 93 | /* |
| 94 | * -------------------------------------------------------------- |
| 95 | * The remaining are internal to ocfs2_extent_tree and don't have |
| 96 | * accessor functions |
| 97 | */ |
| 98 | |
| 99 | /* |
| 100 | * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. |
| 101 | * It is required. |
| 102 | */ |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 103 | void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if |
| 107 | * it exists. If it does not, et->et_max_leaf_clusters is set |
| 108 | * to 0 (unlimited). Optional. |
| 109 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 110 | void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 111 | }; |
| 112 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check |
| 116 | * in the methods. |
| 117 | */ |
| 118 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); |
| 119 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 120 | u64 blkno); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 121 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 122 | u32 clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 123 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 124 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 125 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 126 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); |
| 127 | static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { |
| 128 | .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, |
| 129 | .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, |
| 130 | .eo_update_clusters = ocfs2_dinode_update_clusters, |
| 131 | .eo_insert_check = ocfs2_dinode_insert_check, |
| 132 | .eo_sanity_check = ocfs2_dinode_sanity_check, |
| 133 | .eo_fill_root_el = ocfs2_dinode_fill_root_el, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 137 | u64 blkno) |
| 138 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 139 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 140 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 141 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 142 | di->i_last_eb_blk = cpu_to_le64(blkno); |
| 143 | } |
| 144 | |
| 145 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 146 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 147 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 148 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 149 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 150 | return le64_to_cpu(di->i_last_eb_blk); |
| 151 | } |
| 152 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 153 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 154 | u32 clusters) |
| 155 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 156 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 157 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 158 | |
| 159 | le32_add_cpu(&di->i_clusters, clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 160 | spin_lock(&oi->ip_lock); |
| 161 | oi->ip_clusters = le32_to_cpu(di->i_clusters); |
| 162 | spin_unlock(&oi->ip_lock); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 165 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 166 | struct ocfs2_extent_rec *rec) |
| 167 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 168 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
| 169 | struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 170 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 171 | BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 172 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 173 | (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 174 | "Device %s, asking for sparse allocation: inode %llu, " |
| 175 | "cpos %u, clusters %u\n", |
| 176 | osb->dev_str, |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 177 | (unsigned long long)oi->ip_blkno, |
| 178 | rec->e_cpos, oi->ip_clusters); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 183 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 184 | { |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 185 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 186 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 187 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 188 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 189 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 190 | return 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 193 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) |
| 194 | { |
| 195 | struct ocfs2_dinode *di = et->et_object; |
| 196 | |
| 197 | et->et_root_el = &di->id2.i_list; |
| 198 | } |
| 199 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 200 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 201 | static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) |
| 202 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 203 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 204 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 205 | et->et_root_el = &vb->vb_xv->xr_list; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 208 | static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 209 | u64 blkno) |
| 210 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 211 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 212 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 213 | vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 217 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 218 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 219 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 220 | return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 223 | static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 224 | u32 clusters) |
| 225 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 226 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 227 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 228 | le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 231 | static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 232 | .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, |
| 233 | .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, |
| 234 | .eo_update_clusters = ocfs2_xattr_value_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 235 | .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 236 | }; |
| 237 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 238 | static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 239 | { |
| 240 | struct ocfs2_xattr_block *xb = et->et_object; |
| 241 | |
| 242 | et->et_root_el = &xb->xb_attrs.xb_root.xt_list; |
| 243 | } |
| 244 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 245 | static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et) |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 246 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 247 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 248 | et->et_max_leaf_clusters = |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 249 | ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 252 | static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 253 | u64 blkno) |
| 254 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 255 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 256 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 257 | |
| 258 | xt->xt_last_eb_blk = cpu_to_le64(blkno); |
| 259 | } |
| 260 | |
| 261 | static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 262 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 263 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 264 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 265 | |
| 266 | return le64_to_cpu(xt->xt_last_eb_blk); |
| 267 | } |
| 268 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 269 | static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 270 | u32 clusters) |
| 271 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 272 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 273 | |
| 274 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); |
| 275 | } |
| 276 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 277 | static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 278 | .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, |
| 279 | .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, |
| 280 | .eo_update_clusters = ocfs2_xattr_tree_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 281 | .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 282 | .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 283 | }; |
| 284 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 285 | static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 286 | u64 blkno) |
| 287 | { |
| 288 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 289 | |
| 290 | dx_root->dr_last_eb_blk = cpu_to_le64(blkno); |
| 291 | } |
| 292 | |
| 293 | static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 294 | { |
| 295 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 296 | |
| 297 | return le64_to_cpu(dx_root->dr_last_eb_blk); |
| 298 | } |
| 299 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 300 | static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 301 | u32 clusters) |
| 302 | { |
| 303 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 304 | |
| 305 | le32_add_cpu(&dx_root->dr_clusters, clusters); |
| 306 | } |
| 307 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 308 | static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 309 | { |
| 310 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 311 | |
| 312 | BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) |
| 318 | { |
| 319 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 320 | |
| 321 | et->et_root_el = &dx_root->dr_list; |
| 322 | } |
| 323 | |
| 324 | static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { |
| 325 | .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, |
| 326 | .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, |
| 327 | .eo_update_clusters = ocfs2_dx_root_update_clusters, |
| 328 | .eo_sanity_check = ocfs2_dx_root_sanity_check, |
| 329 | .eo_fill_root_el = ocfs2_dx_root_fill_root_el, |
| 330 | }; |
| 331 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 332 | static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, |
| 333 | struct inode *inode, |
| 334 | struct buffer_head *bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 335 | ocfs2_journal_access_func access, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 336 | void *obj, |
| 337 | struct ocfs2_extent_tree_operations *ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 338 | { |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 339 | et->et_ops = ops; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 340 | et->et_root_bh = bh; |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 341 | et->et_ci = INODE_CACHE(inode); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 342 | et->et_root_journal_access = access; |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 343 | if (!obj) |
| 344 | obj = (void *)bh->b_data; |
| 345 | et->et_object = obj; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 346 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 347 | et->et_ops->eo_fill_root_el(et); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 348 | if (!et->et_ops->eo_fill_max_leaf_clusters) |
| 349 | et->et_max_leaf_clusters = 0; |
| 350 | else |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 351 | et->et_ops->eo_fill_max_leaf_clusters(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 352 | } |
| 353 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 354 | void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, |
| 355 | struct inode *inode, |
| 356 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 357 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 358 | __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di, |
| 359 | NULL, &ocfs2_dinode_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 362 | void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 363 | struct inode *inode, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 364 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 365 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 366 | __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb, |
| 367 | NULL, &ocfs2_xattr_tree_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 370 | void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, |
| 371 | struct inode *inode, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 372 | struct ocfs2_xattr_value_buf *vb) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 373 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 374 | __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 375 | &ocfs2_xattr_value_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 376 | } |
| 377 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 378 | void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, |
| 379 | struct inode *inode, |
| 380 | struct buffer_head *bh) |
| 381 | { |
| 382 | __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr, |
| 383 | NULL, &ocfs2_dx_root_et_ops); |
| 384 | } |
| 385 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 386 | static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 387 | u64 new_last_eb_blk) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 388 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 389 | et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 390 | } |
| 391 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 392 | static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 393 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 394 | return et->et_ops->eo_get_last_eb_blk(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 397 | static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 398 | u32 clusters) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 399 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 400 | et->et_ops->eo_update_clusters(et, clusters); |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 403 | static inline int ocfs2_et_root_journal_access(handle_t *handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 404 | struct ocfs2_extent_tree *et, |
| 405 | int type) |
| 406 | { |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 407 | return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 408 | type); |
| 409 | } |
| 410 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 411 | static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 412 | struct ocfs2_extent_rec *rec) |
| 413 | { |
| 414 | int ret = 0; |
| 415 | |
| 416 | if (et->et_ops->eo_insert_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 417 | ret = et->et_ops->eo_insert_check(et, rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 418 | return ret; |
| 419 | } |
| 420 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 421 | static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 422 | { |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 423 | int ret = 0; |
| 424 | |
| 425 | if (et->et_ops->eo_sanity_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 426 | ret = et->et_ops->eo_sanity_check(et); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 427 | return ret; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 428 | } |
| 429 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 430 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 431 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 432 | struct ocfs2_extent_block *eb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 433 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 434 | /* |
| 435 | * Structures which describe a path through a btree, and functions to |
| 436 | * manipulate them. |
| 437 | * |
| 438 | * The idea here is to be as generic as possible with the tree |
| 439 | * manipulation code. |
| 440 | */ |
| 441 | struct ocfs2_path_item { |
| 442 | struct buffer_head *bh; |
| 443 | struct ocfs2_extent_list *el; |
| 444 | }; |
| 445 | |
| 446 | #define OCFS2_MAX_PATH_DEPTH 5 |
| 447 | |
| 448 | struct ocfs2_path { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 449 | int p_tree_depth; |
| 450 | ocfs2_journal_access_func p_root_access; |
| 451 | struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | #define path_root_bh(_path) ((_path)->p_node[0].bh) |
| 455 | #define path_root_el(_path) ((_path)->p_node[0].el) |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 456 | #define path_root_access(_path)((_path)->p_root_access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 457 | #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) |
| 458 | #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) |
| 459 | #define path_num_items(_path) ((_path)->p_tree_depth + 1) |
| 460 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 461 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 462 | struct ocfs2_path *path, u32 cpos); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 463 | static void ocfs2_adjust_rightmost_records(struct inode *inode, |
| 464 | handle_t *handle, |
| 465 | struct ocfs2_path *path, |
| 466 | struct ocfs2_extent_rec *insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 467 | /* |
| 468 | * Reset the actual path elements so that we can re-use the structure |
| 469 | * to build another path. Generally, this involves freeing the buffer |
| 470 | * heads. |
| 471 | */ |
| 472 | static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
| 473 | { |
| 474 | int i, start = 0, depth = 0; |
| 475 | struct ocfs2_path_item *node; |
| 476 | |
| 477 | if (keep_root) |
| 478 | start = 1; |
| 479 | |
| 480 | for(i = start; i < path_num_items(path); i++) { |
| 481 | node = &path->p_node[i]; |
| 482 | |
| 483 | brelse(node->bh); |
| 484 | node->bh = NULL; |
| 485 | node->el = NULL; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Tree depth may change during truncate, or insert. If we're |
| 490 | * keeping the root extent list, then make sure that our path |
| 491 | * structure reflects the proper depth. |
| 492 | */ |
| 493 | if (keep_root) |
| 494 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 495 | else |
| 496 | path_root_access(path) = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 497 | |
| 498 | path->p_tree_depth = depth; |
| 499 | } |
| 500 | |
| 501 | static void ocfs2_free_path(struct ocfs2_path *path) |
| 502 | { |
| 503 | if (path) { |
| 504 | ocfs2_reinit_path(path, 0); |
| 505 | kfree(path); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 510 | * All the elements of src into dest. After this call, src could be freed |
| 511 | * without affecting dest. |
| 512 | * |
| 513 | * Both paths should have the same root. Any non-root elements of dest |
| 514 | * will be freed. |
| 515 | */ |
| 516 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 517 | { |
| 518 | int i; |
| 519 | |
| 520 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 521 | BUG_ON(path_root_el(dest) != path_root_el(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 522 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 523 | |
| 524 | ocfs2_reinit_path(dest, 1); |
| 525 | |
| 526 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 527 | dest->p_node[i].bh = src->p_node[i].bh; |
| 528 | dest->p_node[i].el = src->p_node[i].el; |
| 529 | |
| 530 | if (dest->p_node[i].bh) |
| 531 | get_bh(dest->p_node[i].bh); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 536 | * Make the *dest path the same as src and re-initialize src path to |
| 537 | * have a root only. |
| 538 | */ |
| 539 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 540 | { |
| 541 | int i; |
| 542 | |
| 543 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 544 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 545 | |
| 546 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 547 | brelse(dest->p_node[i].bh); |
| 548 | |
| 549 | dest->p_node[i].bh = src->p_node[i].bh; |
| 550 | dest->p_node[i].el = src->p_node[i].el; |
| 551 | |
| 552 | src->p_node[i].bh = NULL; |
| 553 | src->p_node[i].el = NULL; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Insert an extent block at given index. |
| 559 | * |
| 560 | * This will not take an additional reference on eb_bh. |
| 561 | */ |
| 562 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, |
| 563 | struct buffer_head *eb_bh) |
| 564 | { |
| 565 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; |
| 566 | |
| 567 | /* |
| 568 | * Right now, no root bh is an extent block, so this helps |
| 569 | * catch code errors with dinode trees. The assertion can be |
| 570 | * safely removed if we ever need to insert extent block |
| 571 | * structures at the root. |
| 572 | */ |
| 573 | BUG_ON(index == 0); |
| 574 | |
| 575 | path->p_node[index].bh = eb_bh; |
| 576 | path->p_node[index].el = &eb->h_list; |
| 577 | } |
| 578 | |
| 579 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 580 | struct ocfs2_extent_list *root_el, |
| 581 | ocfs2_journal_access_func access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 582 | { |
| 583 | struct ocfs2_path *path; |
| 584 | |
| 585 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
| 586 | |
| 587 | path = kzalloc(sizeof(*path), GFP_NOFS); |
| 588 | if (path) { |
| 589 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); |
| 590 | get_bh(root_bh); |
| 591 | path_root_bh(path) = root_bh; |
| 592 | path_root_el(path) = root_el; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 593 | path_root_access(path) = access; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return path; |
| 597 | } |
| 598 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 599 | static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) |
| 600 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 601 | return ocfs2_new_path(path_root_bh(path), path_root_el(path), |
| 602 | path_root_access(path)); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et) |
| 606 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 607 | return ocfs2_new_path(et->et_root_bh, et->et_root_el, |
| 608 | et->et_root_journal_access); |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Journal the buffer at depth idx. All idx>0 are extent_blocks, |
| 613 | * otherwise it's the root_access function. |
| 614 | * |
| 615 | * I don't like the way this function's name looks next to |
| 616 | * ocfs2_journal_access_path(), but I don't have a better one. |
| 617 | */ |
| 618 | static int ocfs2_path_bh_journal_access(handle_t *handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 619 | struct ocfs2_caching_info *ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 620 | struct ocfs2_path *path, |
| 621 | int idx) |
| 622 | { |
| 623 | ocfs2_journal_access_func access = path_root_access(path); |
| 624 | |
| 625 | if (!access) |
| 626 | access = ocfs2_journal_access; |
| 627 | |
| 628 | if (idx) |
| 629 | access = ocfs2_journal_access_eb; |
| 630 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 631 | return access(handle, ci, path->p_node[idx].bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 632 | OCFS2_JOURNAL_ACCESS_WRITE); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 635 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 636 | * Convenience function to journal all components in a path. |
| 637 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 638 | static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, |
| 639 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 640 | struct ocfs2_path *path) |
| 641 | { |
| 642 | int i, ret = 0; |
| 643 | |
| 644 | if (!path) |
| 645 | goto out; |
| 646 | |
| 647 | for(i = 0; i < path_num_items(path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 648 | ret = ocfs2_path_bh_journal_access(handle, ci, path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 649 | if (ret < 0) { |
| 650 | mlog_errno(ret); |
| 651 | goto out; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | out: |
| 656 | return ret; |
| 657 | } |
| 658 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 659 | /* |
| 660 | * Return the index of the extent record which contains cluster #v_cluster. |
| 661 | * -1 is returned if it was not found. |
| 662 | * |
| 663 | * Should work fine on interior and exterior nodes. |
| 664 | */ |
| 665 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) |
| 666 | { |
| 667 | int ret = -1; |
| 668 | int i; |
| 669 | struct ocfs2_extent_rec *rec; |
| 670 | u32 rec_end, rec_start, clusters; |
| 671 | |
| 672 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 673 | rec = &el->l_recs[i]; |
| 674 | |
| 675 | rec_start = le32_to_cpu(rec->e_cpos); |
| 676 | clusters = ocfs2_rec_clusters(el, rec); |
| 677 | |
| 678 | rec_end = rec_start + clusters; |
| 679 | |
| 680 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 681 | ret = i; |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return ret; |
| 687 | } |
| 688 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 689 | enum ocfs2_contig_type { |
| 690 | CONTIG_NONE = 0, |
| 691 | CONTIG_LEFT, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 692 | CONTIG_RIGHT, |
| 693 | CONTIG_LEFTRIGHT, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 694 | }; |
| 695 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and |
| 699 | * ocfs2_extent_contig only work properly against leaf nodes! |
| 700 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 701 | static int ocfs2_block_extent_contig(struct super_block *sb, |
| 702 | struct ocfs2_extent_rec *ext, |
| 703 | u64 blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 704 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 705 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
| 706 | |
| 707 | blk_end += ocfs2_clusters_to_blocks(sb, |
| 708 | le16_to_cpu(ext->e_leaf_clusters)); |
| 709 | |
| 710 | return blkno == blk_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 713 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
| 714 | struct ocfs2_extent_rec *right) |
| 715 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 716 | u32 left_range; |
| 717 | |
| 718 | left_range = le32_to_cpu(left->e_cpos) + |
| 719 | le16_to_cpu(left->e_leaf_clusters); |
| 720 | |
| 721 | return (left_range == le32_to_cpu(right->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static enum ocfs2_contig_type |
| 725 | ocfs2_extent_contig(struct inode *inode, |
| 726 | struct ocfs2_extent_rec *ext, |
| 727 | struct ocfs2_extent_rec *insert_rec) |
| 728 | { |
| 729 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); |
| 730 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Refuse to coalesce extent records with different flag |
| 733 | * fields - we don't want to mix unwritten extents with user |
| 734 | * data. |
| 735 | */ |
| 736 | if (ext->e_flags != insert_rec->e_flags) |
| 737 | return CONTIG_NONE; |
| 738 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 739 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
| 740 | ocfs2_block_extent_contig(inode->i_sb, ext, blkno)) |
| 741 | return CONTIG_RIGHT; |
| 742 | |
| 743 | blkno = le64_to_cpu(ext->e_blkno); |
| 744 | if (ocfs2_extents_adjacent(insert_rec, ext) && |
| 745 | ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno)) |
| 746 | return CONTIG_LEFT; |
| 747 | |
| 748 | return CONTIG_NONE; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * NOTE: We can have pretty much any combination of contiguousness and |
| 753 | * appending. |
| 754 | * |
| 755 | * The usefulness of APPEND_TAIL is more in that it lets us know that |
| 756 | * we'll have to update the path to that leaf. |
| 757 | */ |
| 758 | enum ocfs2_append_type { |
| 759 | APPEND_NONE = 0, |
| 760 | APPEND_TAIL, |
| 761 | }; |
| 762 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 763 | enum ocfs2_split_type { |
| 764 | SPLIT_NONE = 0, |
| 765 | SPLIT_LEFT, |
| 766 | SPLIT_RIGHT, |
| 767 | }; |
| 768 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 769 | struct ocfs2_insert_type { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 770 | enum ocfs2_split_type ins_split; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 771 | enum ocfs2_append_type ins_appending; |
| 772 | enum ocfs2_contig_type ins_contig; |
| 773 | int ins_contig_index; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 774 | int ins_tree_depth; |
| 775 | }; |
| 776 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 777 | struct ocfs2_merge_ctxt { |
| 778 | enum ocfs2_contig_type c_contig_type; |
| 779 | int c_has_empty_extent; |
| 780 | int c_split_covers_rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 781 | }; |
| 782 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 783 | static int ocfs2_validate_extent_block(struct super_block *sb, |
| 784 | struct buffer_head *bh) |
| 785 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 786 | int rc; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 787 | struct ocfs2_extent_block *eb = |
| 788 | (struct ocfs2_extent_block *)bh->b_data; |
| 789 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 790 | mlog(0, "Validating extent block %llu\n", |
| 791 | (unsigned long long)bh->b_blocknr); |
| 792 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 793 | BUG_ON(!buffer_uptodate(bh)); |
| 794 | |
| 795 | /* |
| 796 | * If the ecc fails, we return the error but otherwise |
| 797 | * leave the filesystem running. We know any error is |
| 798 | * local to this block. |
| 799 | */ |
| 800 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 801 | if (rc) { |
| 802 | mlog(ML_ERROR, "Checksum failed for extent block %llu\n", |
| 803 | (unsigned long long)bh->b_blocknr); |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 804 | return rc; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 805 | } |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * Errors after here are fatal. |
| 809 | */ |
| 810 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 811 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 812 | ocfs2_error(sb, |
| 813 | "Extent block #%llu has bad signature %.*s", |
| 814 | (unsigned long long)bh->b_blocknr, 7, |
| 815 | eb->h_signature); |
| 816 | return -EINVAL; |
| 817 | } |
| 818 | |
| 819 | if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { |
| 820 | ocfs2_error(sb, |
| 821 | "Extent block #%llu has an invalid h_blkno " |
| 822 | "of %llu", |
| 823 | (unsigned long long)bh->b_blocknr, |
| 824 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 825 | return -EINVAL; |
| 826 | } |
| 827 | |
| 828 | if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 829 | ocfs2_error(sb, |
| 830 | "Extent block #%llu has an invalid " |
| 831 | "h_fs_generation of #%u", |
| 832 | (unsigned long long)bh->b_blocknr, |
| 833 | le32_to_cpu(eb->h_fs_generation)); |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 840 | int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 841 | struct buffer_head **bh) |
| 842 | { |
| 843 | int rc; |
| 844 | struct buffer_head *tmp = *bh; |
| 845 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 846 | rc = ocfs2_read_block(ci, eb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 847 | ocfs2_validate_extent_block); |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 848 | |
| 849 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 850 | if (!rc && !*bh) |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 851 | *bh = tmp; |
| 852 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 853 | return rc; |
| 854 | } |
| 855 | |
| 856 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 857 | /* |
| 858 | * How many free extents have we got before we need more meta data? |
| 859 | */ |
| 860 | int ocfs2_num_free_extents(struct ocfs2_super *osb, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 861 | struct ocfs2_extent_tree *et) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 862 | { |
| 863 | int retval; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 864 | struct ocfs2_extent_list *el = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 865 | struct ocfs2_extent_block *eb; |
| 866 | struct buffer_head *eb_bh = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 867 | u64 last_eb_blk = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 868 | |
| 869 | mlog_entry_void(); |
| 870 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 871 | el = et->et_root_el; |
| 872 | last_eb_blk = ocfs2_et_get_last_eb_blk(et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 873 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 874 | if (last_eb_blk) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 875 | retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, |
| 876 | &eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 877 | if (retval < 0) { |
| 878 | mlog_errno(retval); |
| 879 | goto bail; |
| 880 | } |
| 881 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 882 | el = &eb->h_list; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 883 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 884 | |
| 885 | BUG_ON(el->l_tree_depth != 0); |
| 886 | |
| 887 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); |
| 888 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 889 | brelse(eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 890 | |
| 891 | mlog_exit(retval); |
| 892 | return retval; |
| 893 | } |
| 894 | |
| 895 | /* expects array to already be allocated |
| 896 | * |
| 897 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and |
| 898 | * l_count for you |
| 899 | */ |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 900 | static int ocfs2_create_new_meta_bhs(handle_t *handle, |
| 901 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 902 | int wanted, |
| 903 | struct ocfs2_alloc_context *meta_ac, |
| 904 | struct buffer_head *bhs[]) |
| 905 | { |
| 906 | int count, status, i; |
| 907 | u16 suballoc_bit_start; |
| 908 | u32 num_got; |
| 909 | u64 first_blkno; |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 910 | struct ocfs2_super *osb = |
| 911 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 912 | struct ocfs2_extent_block *eb; |
| 913 | |
| 914 | mlog_entry_void(); |
| 915 | |
| 916 | count = 0; |
| 917 | while (count < wanted) { |
| 918 | status = ocfs2_claim_metadata(osb, |
| 919 | handle, |
| 920 | meta_ac, |
| 921 | wanted - count, |
| 922 | &suballoc_bit_start, |
| 923 | &num_got, |
| 924 | &first_blkno); |
| 925 | if (status < 0) { |
| 926 | mlog_errno(status); |
| 927 | goto bail; |
| 928 | } |
| 929 | |
| 930 | for(i = count; i < (num_got + count); i++) { |
| 931 | bhs[i] = sb_getblk(osb->sb, first_blkno); |
| 932 | if (bhs[i] == NULL) { |
| 933 | status = -EIO; |
| 934 | mlog_errno(status); |
| 935 | goto bail; |
| 936 | } |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 937 | ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 938 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 939 | status = ocfs2_journal_access_eb(handle, et->et_ci, |
| 940 | bhs[i], |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 941 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 942 | if (status < 0) { |
| 943 | mlog_errno(status); |
| 944 | goto bail; |
| 945 | } |
| 946 | |
| 947 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); |
| 948 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; |
| 949 | /* Ok, setup the minimal stuff here. */ |
| 950 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); |
| 951 | eb->h_blkno = cpu_to_le64(first_blkno); |
| 952 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 953 | eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 954 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 955 | eb->h_list.l_count = |
| 956 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 957 | |
| 958 | suballoc_bit_start++; |
| 959 | first_blkno++; |
| 960 | |
| 961 | /* We'll also be dirtied by the caller, so |
| 962 | * this isn't absolutely necessary. */ |
| 963 | status = ocfs2_journal_dirty(handle, bhs[i]); |
| 964 | if (status < 0) { |
| 965 | mlog_errno(status); |
| 966 | goto bail; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | count += num_got; |
| 971 | } |
| 972 | |
| 973 | status = 0; |
| 974 | bail: |
| 975 | if (status < 0) { |
| 976 | for(i = 0; i < wanted; i++) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 977 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 978 | bhs[i] = NULL; |
| 979 | } |
| 980 | } |
| 981 | mlog_exit(status); |
| 982 | return status; |
| 983 | } |
| 984 | |
| 985 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 986 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 987 | * |
| 988 | * Returns the sum of the rightmost extent rec logical offset and |
| 989 | * cluster count. |
| 990 | * |
| 991 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 992 | * value should be populated into the leftmost new branch records. |
| 993 | * |
| 994 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 995 | * value for the new topmost tree record. |
| 996 | */ |
| 997 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 998 | { |
| 999 | int i; |
| 1000 | |
| 1001 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1002 | |
| 1003 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1004 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1008 | * Change range of the branches in the right most path according to the leaf |
| 1009 | * extent block's rightmost record. |
| 1010 | */ |
| 1011 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, |
| 1012 | struct inode *inode, |
| 1013 | struct ocfs2_extent_tree *et) |
| 1014 | { |
| 1015 | int status; |
| 1016 | struct ocfs2_path *path = NULL; |
| 1017 | struct ocfs2_extent_list *el; |
| 1018 | struct ocfs2_extent_rec *rec; |
| 1019 | |
| 1020 | path = ocfs2_new_path_from_et(et); |
| 1021 | if (!path) { |
| 1022 | status = -ENOMEM; |
| 1023 | return status; |
| 1024 | } |
| 1025 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1026 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1027 | if (status < 0) { |
| 1028 | mlog_errno(status); |
| 1029 | goto out; |
| 1030 | } |
| 1031 | |
| 1032 | status = ocfs2_extend_trans(handle, path_num_items(path) + |
| 1033 | handle->h_buffer_credits); |
| 1034 | if (status < 0) { |
| 1035 | mlog_errno(status); |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1039 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1040 | if (status < 0) { |
| 1041 | mlog_errno(status); |
| 1042 | goto out; |
| 1043 | } |
| 1044 | |
| 1045 | el = path_leaf_el(path); |
| 1046 | rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; |
| 1047 | |
| 1048 | ocfs2_adjust_rightmost_records(inode, handle, path, rec); |
| 1049 | |
| 1050 | out: |
| 1051 | ocfs2_free_path(path); |
| 1052 | return status; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1056 | * Add an entire tree branch to our inode. eb_bh is the extent block |
| 1057 | * to start at, if we don't want to start the branch at the dinode |
| 1058 | * structure. |
| 1059 | * |
| 1060 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1061 | * for the new last extent block. |
| 1062 | * |
| 1063 | * the new branch will be 'empty' in the sense that every block will |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1064 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1065 | */ |
| 1066 | static int ocfs2_add_branch(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1067 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1068 | struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1069 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1070 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1071 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1072 | struct ocfs2_alloc_context *meta_ac) |
| 1073 | { |
| 1074 | int status, new_blocks, i; |
| 1075 | u64 next_blkno, new_last_eb_blk; |
| 1076 | struct buffer_head *bh; |
| 1077 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1078 | struct ocfs2_extent_block *eb; |
| 1079 | struct ocfs2_extent_list *eb_el; |
| 1080 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1081 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1082 | |
| 1083 | mlog_entry_void(); |
| 1084 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1085 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1086 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1087 | if (eb_bh) { |
| 1088 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1089 | el = &eb->h_list; |
| 1090 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1091 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1092 | |
| 1093 | /* we never add a branch to a leaf. */ |
| 1094 | BUG_ON(!el->l_tree_depth); |
| 1095 | |
| 1096 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1097 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1098 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1099 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1100 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1101 | |
| 1102 | /* |
| 1103 | * If there is a gap before the root end and the real end |
| 1104 | * of the righmost leaf block, we need to remove the gap |
| 1105 | * between new_cpos and root_end first so that the tree |
| 1106 | * is consistent after we add a new branch(it will start |
| 1107 | * from new_cpos). |
| 1108 | */ |
| 1109 | if (root_end > new_cpos) { |
| 1110 | mlog(0, "adjust the cluster end from %u to %u\n", |
| 1111 | root_end, new_cpos); |
| 1112 | status = ocfs2_adjust_rightmost_branch(handle, inode, et); |
| 1113 | if (status) { |
| 1114 | mlog_errno(status); |
| 1115 | goto bail; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1119 | /* allocate the number of new eb blocks we need */ |
| 1120 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1121 | GFP_KERNEL); |
| 1122 | if (!new_eb_bhs) { |
| 1123 | status = -ENOMEM; |
| 1124 | mlog_errno(status); |
| 1125 | goto bail; |
| 1126 | } |
| 1127 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1128 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1129 | meta_ac, new_eb_bhs); |
| 1130 | if (status < 0) { |
| 1131 | mlog_errno(status); |
| 1132 | goto bail; |
| 1133 | } |
| 1134 | |
| 1135 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1136 | * linked with the rest of the tree. |
| 1137 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1138 | * |
| 1139 | * when we leave the loop, new_last_eb_blk will point to the |
| 1140 | * newest leaf, and next_blkno will point to the topmost extent |
| 1141 | * block. */ |
| 1142 | next_blkno = new_last_eb_blk = 0; |
| 1143 | for(i = 0; i < new_blocks; i++) { |
| 1144 | bh = new_eb_bhs[i]; |
| 1145 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1146 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1147 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1148 | eb_el = &eb->h_list; |
| 1149 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1150 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1151 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1152 | if (status < 0) { |
| 1153 | mlog_errno(status); |
| 1154 | goto bail; |
| 1155 | } |
| 1156 | |
| 1157 | eb->h_next_leaf_blk = 0; |
| 1158 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1159 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1160 | /* |
| 1161 | * This actually counts as an empty extent as |
| 1162 | * c_clusters == 0 |
| 1163 | */ |
| 1164 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1165 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1166 | /* |
| 1167 | * eb_el isn't always an interior node, but even leaf |
| 1168 | * nodes want a zero'd flags and reserved field so |
| 1169 | * this gets the whole 32 bits regardless of use. |
| 1170 | */ |
| 1171 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1172 | if (!eb_el->l_tree_depth) |
| 1173 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1174 | |
| 1175 | status = ocfs2_journal_dirty(handle, bh); |
| 1176 | if (status < 0) { |
| 1177 | mlog_errno(status); |
| 1178 | goto bail; |
| 1179 | } |
| 1180 | |
| 1181 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1182 | } |
| 1183 | |
| 1184 | /* This is a bit hairy. We want to update up to three blocks |
| 1185 | * here without leaving any of them in an inconsistent state |
| 1186 | * in case of error. We don't have to worry about |
| 1187 | * journal_dirty erroring as it won't unless we've aborted the |
| 1188 | * handle (in which case we would never be here) so reserving |
| 1189 | * the write with journal_access is all we need to do. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1190 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1191 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1192 | if (status < 0) { |
| 1193 | mlog_errno(status); |
| 1194 | goto bail; |
| 1195 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1196 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1197 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1198 | if (status < 0) { |
| 1199 | mlog_errno(status); |
| 1200 | goto bail; |
| 1201 | } |
| 1202 | if (eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1203 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1204 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1205 | if (status < 0) { |
| 1206 | mlog_errno(status); |
| 1207 | goto bail; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1212 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1213 | i = le16_to_cpu(el->l_next_free_rec); |
| 1214 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1215 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1216 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1217 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1218 | |
| 1219 | /* fe needs a new last extent block pointer, as does the |
| 1220 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1221 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1222 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1223 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1224 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1225 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1226 | status = ocfs2_journal_dirty(handle, *last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1227 | if (status < 0) |
| 1228 | mlog_errno(status); |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1229 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1230 | if (status < 0) |
| 1231 | mlog_errno(status); |
| 1232 | if (eb_bh) { |
| 1233 | status = ocfs2_journal_dirty(handle, eb_bh); |
| 1234 | if (status < 0) |
| 1235 | mlog_errno(status); |
| 1236 | } |
| 1237 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1238 | /* |
| 1239 | * Some callers want to track the rightmost leaf so pass it |
| 1240 | * back here. |
| 1241 | */ |
| 1242 | brelse(*last_eb_bh); |
| 1243 | get_bh(new_eb_bhs[0]); |
| 1244 | *last_eb_bh = new_eb_bhs[0]; |
| 1245 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1246 | status = 0; |
| 1247 | bail: |
| 1248 | if (new_eb_bhs) { |
| 1249 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1250 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1251 | kfree(new_eb_bhs); |
| 1252 | } |
| 1253 | |
| 1254 | mlog_exit(status); |
| 1255 | return status; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * adds another level to the allocation tree. |
| 1260 | * returns back the new extent block so you can add a branch to it |
| 1261 | * after this call. |
| 1262 | */ |
| 1263 | static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1264 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1265 | struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1266 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1267 | struct ocfs2_alloc_context *meta_ac, |
| 1268 | struct buffer_head **ret_new_eb_bh) |
| 1269 | { |
| 1270 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1271 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1272 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1273 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1274 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1275 | struct ocfs2_extent_list *eb_el; |
| 1276 | |
| 1277 | mlog_entry_void(); |
| 1278 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1279 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1280 | &new_eb_bh); |
| 1281 | if (status < 0) { |
| 1282 | mlog_errno(status); |
| 1283 | goto bail; |
| 1284 | } |
| 1285 | |
| 1286 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1287 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1288 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1289 | |
| 1290 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1291 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1292 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1293 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1294 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1295 | if (status < 0) { |
| 1296 | mlog_errno(status); |
| 1297 | goto bail; |
| 1298 | } |
| 1299 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1300 | /* copy the root extent list data into the new extent block */ |
| 1301 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1302 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1303 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1304 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1305 | |
| 1306 | status = ocfs2_journal_dirty(handle, new_eb_bh); |
| 1307 | if (status < 0) { |
| 1308 | mlog_errno(status); |
| 1309 | goto bail; |
| 1310 | } |
| 1311 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1312 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1313 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1314 | if (status < 0) { |
| 1315 | mlog_errno(status); |
| 1316 | goto bail; |
| 1317 | } |
| 1318 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1319 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1320 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1321 | /* update root_bh now */ |
| 1322 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1323 | root_el->l_recs[0].e_cpos = 0; |
| 1324 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1325 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1326 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1327 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1328 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1329 | |
| 1330 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1331 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1332 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1333 | ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1334 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1335 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1336 | if (status < 0) { |
| 1337 | mlog_errno(status); |
| 1338 | goto bail; |
| 1339 | } |
| 1340 | |
| 1341 | *ret_new_eb_bh = new_eb_bh; |
| 1342 | new_eb_bh = NULL; |
| 1343 | status = 0; |
| 1344 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1345 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1346 | |
| 1347 | mlog_exit(status); |
| 1348 | return status; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1352 | * Should only be called when there is no space left in any of the |
| 1353 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1354 | * non-leaf extent block with room for new records. There are three |
| 1355 | * valid results of this search: |
| 1356 | * |
| 1357 | * 1) a lowest extent block is found, then we pass it back in |
| 1358 | * *lowest_eb_bh and return '0' |
| 1359 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1360 | * 2) the search fails to find anything, but the root_el has room. We |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1361 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1362 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1363 | * 3) the search fails to find anything AND the root_el is full, in |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1364 | * which case we return > 0 |
| 1365 | * |
| 1366 | * return status < 0 indicates an error. |
| 1367 | */ |
| 1368 | static int ocfs2_find_branch_target(struct ocfs2_super *osb, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1369 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1370 | struct buffer_head **target_bh) |
| 1371 | { |
| 1372 | int status = 0, i; |
| 1373 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1374 | struct ocfs2_extent_block *eb; |
| 1375 | struct ocfs2_extent_list *el; |
| 1376 | struct buffer_head *bh = NULL; |
| 1377 | struct buffer_head *lowest_bh = NULL; |
| 1378 | |
| 1379 | mlog_entry_void(); |
| 1380 | |
| 1381 | *target_bh = NULL; |
| 1382 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1383 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1384 | |
| 1385 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1386 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1387 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1388 | "Owner %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1389 | "extent list (next_free_rec == 0)", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1390 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1391 | status = -EIO; |
| 1392 | goto bail; |
| 1393 | } |
| 1394 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1395 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1396 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1397 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1398 | "Owner %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1399 | "list where extent # %d has no physical " |
| 1400 | "block start", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1401 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1402 | status = -EIO; |
| 1403 | goto bail; |
| 1404 | } |
| 1405 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1406 | brelse(bh); |
| 1407 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1408 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1409 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1410 | if (status < 0) { |
| 1411 | mlog_errno(status); |
| 1412 | goto bail; |
| 1413 | } |
| 1414 | |
| 1415 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1416 | el = &eb->h_list; |
| 1417 | |
| 1418 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1419 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1420 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1421 | lowest_bh = bh; |
| 1422 | get_bh(lowest_bh); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | /* If we didn't find one and the fe doesn't have any room, |
| 1427 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1428 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1429 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1430 | status = 1; |
| 1431 | |
| 1432 | *target_bh = lowest_bh; |
| 1433 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1434 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1435 | |
| 1436 | mlog_exit(status); |
| 1437 | return status; |
| 1438 | } |
| 1439 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1440 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1441 | * Grow a b-tree so that it has more records. |
| 1442 | * |
| 1443 | * We might shift the tree depth in which case existing paths should |
| 1444 | * be considered invalid. |
| 1445 | * |
| 1446 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1447 | * |
| 1448 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1449 | */ |
| 1450 | static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1451 | struct ocfs2_extent_tree *et, int *final_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1452 | struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1453 | struct ocfs2_alloc_context *meta_ac) |
| 1454 | { |
| 1455 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1456 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1457 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1458 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1459 | struct buffer_head *bh = NULL; |
| 1460 | |
| 1461 | BUG_ON(meta_ac == NULL); |
| 1462 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1463 | shift = ocfs2_find_branch_target(osb, et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1464 | if (shift < 0) { |
| 1465 | ret = shift; |
| 1466 | mlog_errno(ret); |
| 1467 | goto out; |
| 1468 | } |
| 1469 | |
| 1470 | /* We traveled all the way to the bottom of the allocation tree |
| 1471 | * and didn't find room for any more extents - we need to add |
| 1472 | * another tree level */ |
| 1473 | if (shift) { |
| 1474 | BUG_ON(bh); |
| 1475 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 1476 | |
| 1477 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1478 | * the new extent block (so we can pass that to |
| 1479 | * ocfs2_add_branch). */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1480 | ret = ocfs2_shift_tree_depth(osb, handle, inode, et, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1481 | meta_ac, &bh); |
| 1482 | if (ret < 0) { |
| 1483 | mlog_errno(ret); |
| 1484 | goto out; |
| 1485 | } |
| 1486 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1487 | if (depth == 1) { |
| 1488 | /* |
| 1489 | * Special case: we have room now if we shifted from |
| 1490 | * tree_depth 0, so no more work needs to be done. |
| 1491 | * |
| 1492 | * We won't be calling add_branch, so pass |
| 1493 | * back *last_eb_bh as the new leaf. At depth |
| 1494 | * zero, it should always be null so there's |
| 1495 | * no reason to brelse. |
| 1496 | */ |
| 1497 | BUG_ON(*last_eb_bh); |
| 1498 | get_bh(bh); |
| 1499 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1500 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1501 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1505 | * the new data. */ |
| 1506 | mlog(0, "add branch. bh = %p\n", bh); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1507 | ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1508 | meta_ac); |
| 1509 | if (ret < 0) { |
| 1510 | mlog_errno(ret); |
| 1511 | goto out; |
| 1512 | } |
| 1513 | |
| 1514 | out: |
| 1515 | if (final_depth) |
| 1516 | *final_depth = depth; |
| 1517 | brelse(bh); |
| 1518 | return ret; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1522 | * This function will discard the rightmost extent record. |
| 1523 | */ |
| 1524 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1525 | { |
| 1526 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1527 | int count = le16_to_cpu(el->l_count); |
| 1528 | unsigned int num_bytes; |
| 1529 | |
| 1530 | BUG_ON(!next_free); |
| 1531 | /* This will cause us to go off the end of our extent list. */ |
| 1532 | BUG_ON(next_free >= count); |
| 1533 | |
| 1534 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1535 | |
| 1536 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1537 | } |
| 1538 | |
| 1539 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1540 | struct ocfs2_extent_rec *insert_rec) |
| 1541 | { |
| 1542 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1543 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1544 | struct ocfs2_extent_rec *rec; |
| 1545 | |
| 1546 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1547 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1548 | |
| 1549 | BUG_ON(!next_free); |
| 1550 | |
| 1551 | /* The tree code before us didn't allow enough room in the leaf. */ |
Julia Lawall | b1f3550 | 2008-03-04 15:21:05 -0800 | [diff] [blame] | 1552 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1553 | |
| 1554 | /* |
| 1555 | * The easiest way to approach this is to just remove the |
| 1556 | * empty extent and temporarily decrement next_free. |
| 1557 | */ |
| 1558 | if (has_empty) { |
| 1559 | /* |
| 1560 | * If next_free was 1 (only an empty extent), this |
| 1561 | * loop won't execute, which is fine. We still want |
| 1562 | * the decrement above to happen. |
| 1563 | */ |
| 1564 | for(i = 0; i < (next_free - 1); i++) |
| 1565 | el->l_recs[i] = el->l_recs[i+1]; |
| 1566 | |
| 1567 | next_free--; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * Figure out what the new record index should be. |
| 1572 | */ |
| 1573 | for(i = 0; i < next_free; i++) { |
| 1574 | rec = &el->l_recs[i]; |
| 1575 | |
| 1576 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1577 | break; |
| 1578 | } |
| 1579 | insert_index = i; |
| 1580 | |
| 1581 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1582 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1583 | |
| 1584 | BUG_ON(insert_index < 0); |
| 1585 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1586 | BUG_ON(insert_index > next_free); |
| 1587 | |
| 1588 | /* |
| 1589 | * No need to memmove if we're just adding to the tail. |
| 1590 | */ |
| 1591 | if (insert_index != next_free) { |
| 1592 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1593 | |
| 1594 | num_bytes = next_free - insert_index; |
| 1595 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1596 | memmove(&el->l_recs[insert_index + 1], |
| 1597 | &el->l_recs[insert_index], |
| 1598 | num_bytes); |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * Either we had an empty extent, and need to re-increment or |
| 1603 | * there was no empty extent on a non full rightmost leaf node, |
| 1604 | * in which case we still need to increment. |
| 1605 | */ |
| 1606 | next_free++; |
| 1607 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1608 | /* |
| 1609 | * Make sure none of the math above just messed up our tree. |
| 1610 | */ |
| 1611 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1612 | |
| 1613 | el->l_recs[insert_index] = *insert_rec; |
| 1614 | |
| 1615 | } |
| 1616 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1617 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1618 | { |
| 1619 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1620 | |
| 1621 | BUG_ON(num_recs == 0); |
| 1622 | |
| 1623 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1624 | num_recs--; |
| 1625 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1626 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1627 | memset(&el->l_recs[num_recs], 0, |
| 1628 | sizeof(struct ocfs2_extent_rec)); |
| 1629 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1630 | } |
| 1631 | } |
| 1632 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1633 | /* |
| 1634 | * Create an empty extent record . |
| 1635 | * |
| 1636 | * l_next_free_rec may be updated. |
| 1637 | * |
| 1638 | * If an empty extent already exists do nothing. |
| 1639 | */ |
| 1640 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1641 | { |
| 1642 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1643 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1644 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1645 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1646 | if (next_free == 0) |
| 1647 | goto set_and_inc; |
| 1648 | |
| 1649 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1650 | return; |
| 1651 | |
| 1652 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1653 | "Asked to create an empty extent in a full list:\n" |
| 1654 | "count = %u, tree depth = %u", |
| 1655 | le16_to_cpu(el->l_count), |
| 1656 | le16_to_cpu(el->l_tree_depth)); |
| 1657 | |
| 1658 | ocfs2_shift_records_right(el); |
| 1659 | |
| 1660 | set_and_inc: |
| 1661 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1662 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1667 | * the lowest level tree node which contains a path to both leafs. This |
| 1668 | * resulting set of information can be used to form a complete "subtree" |
| 1669 | * |
| 1670 | * This function is passed two full paths from the dinode down to a |
| 1671 | * pair of adjacent leaves. It's task is to figure out which path |
| 1672 | * index contains the subtree root - this can be the root index itself |
| 1673 | * in a worst-case rotation. |
| 1674 | * |
| 1675 | * The array index of the subtree root is passed back. |
| 1676 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1677 | static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1678 | struct ocfs2_path *left, |
| 1679 | struct ocfs2_path *right) |
| 1680 | { |
| 1681 | int i = 0; |
| 1682 | |
| 1683 | /* |
| 1684 | * Check that the caller passed in two paths from the same tree. |
| 1685 | */ |
| 1686 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1687 | |
| 1688 | do { |
| 1689 | i++; |
| 1690 | |
| 1691 | /* |
| 1692 | * The caller didn't pass two adjacent paths. |
| 1693 | */ |
| 1694 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1695 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1696 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1697 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1698 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1699 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1700 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1701 | } while (left->p_node[i].bh->b_blocknr == |
| 1702 | right->p_node[i].bh->b_blocknr); |
| 1703 | |
| 1704 | return i - 1; |
| 1705 | } |
| 1706 | |
| 1707 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1708 | |
| 1709 | /* |
| 1710 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1711 | * |
| 1712 | * This code can be called with a cpos larger than the tree, in which |
| 1713 | * case it will return the rightmost path. |
| 1714 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1715 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1716 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1717 | path_insert_t *func, void *data) |
| 1718 | { |
| 1719 | int i, ret = 0; |
| 1720 | u32 range; |
| 1721 | u64 blkno; |
| 1722 | struct buffer_head *bh = NULL; |
| 1723 | struct ocfs2_extent_block *eb; |
| 1724 | struct ocfs2_extent_list *el; |
| 1725 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1726 | |
| 1727 | el = root_el; |
| 1728 | while (el->l_tree_depth) { |
| 1729 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1730 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1731 | "Owner %llu has empty extent list at " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1732 | "depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1733 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1734 | le16_to_cpu(el->l_tree_depth)); |
| 1735 | ret = -EROFS; |
| 1736 | goto out; |
| 1737 | |
| 1738 | } |
| 1739 | |
| 1740 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1741 | rec = &el->l_recs[i]; |
| 1742 | |
| 1743 | /* |
| 1744 | * In the case that cpos is off the allocation |
| 1745 | * tree, this should just wind up returning the |
| 1746 | * rightmost record. |
| 1747 | */ |
| 1748 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1749 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1750 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1751 | break; |
| 1752 | } |
| 1753 | |
| 1754 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1755 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1756 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1757 | "Owner %llu has bad blkno in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1758 | "at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1759 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1760 | le16_to_cpu(el->l_tree_depth), i); |
| 1761 | ret = -EROFS; |
| 1762 | goto out; |
| 1763 | } |
| 1764 | |
| 1765 | brelse(bh); |
| 1766 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1767 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1768 | if (ret) { |
| 1769 | mlog_errno(ret); |
| 1770 | goto out; |
| 1771 | } |
| 1772 | |
| 1773 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1774 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1775 | |
| 1776 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1777 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1778 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1779 | "Owner %llu has bad count in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1780 | "at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1781 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1782 | (unsigned long long)bh->b_blocknr, |
| 1783 | le16_to_cpu(el->l_next_free_rec), |
| 1784 | le16_to_cpu(el->l_count)); |
| 1785 | ret = -EROFS; |
| 1786 | goto out; |
| 1787 | } |
| 1788 | |
| 1789 | if (func) |
| 1790 | func(data, bh); |
| 1791 | } |
| 1792 | |
| 1793 | out: |
| 1794 | /* |
| 1795 | * Catch any trailing bh that the loop didn't handle. |
| 1796 | */ |
| 1797 | brelse(bh); |
| 1798 | |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * Given an initialized path (that is, it has a valid root extent |
| 1804 | * list), this function will traverse the btree in search of the path |
| 1805 | * which would contain cpos. |
| 1806 | * |
| 1807 | * The path traveled is recorded in the path structure. |
| 1808 | * |
| 1809 | * Note that this will not do any comparisons on leaf node extent |
| 1810 | * records, so it will work fine in the case that we just added a tree |
| 1811 | * branch. |
| 1812 | */ |
| 1813 | struct find_path_data { |
| 1814 | int index; |
| 1815 | struct ocfs2_path *path; |
| 1816 | }; |
| 1817 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1818 | { |
| 1819 | struct find_path_data *fp = data; |
| 1820 | |
| 1821 | get_bh(bh); |
| 1822 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1823 | fp->index++; |
| 1824 | } |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1825 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1826 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1827 | { |
| 1828 | struct find_path_data data; |
| 1829 | |
| 1830 | data.index = 1; |
| 1831 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1832 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1833 | find_path_ins, &data); |
| 1834 | } |
| 1835 | |
| 1836 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1837 | { |
| 1838 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1839 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1840 | struct buffer_head **ret = data; |
| 1841 | |
| 1842 | /* We want to retain only the leaf block. */ |
| 1843 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1844 | get_bh(bh); |
| 1845 | *ret = bh; |
| 1846 | } |
| 1847 | } |
| 1848 | /* |
| 1849 | * Find the leaf block in the tree which would contain cpos. No |
| 1850 | * checking of the actual leaf is done. |
| 1851 | * |
| 1852 | * Some paths want to call this instead of allocating a path structure |
| 1853 | * and calling ocfs2_find_path(). |
| 1854 | * |
| 1855 | * This function doesn't handle non btree extent lists. |
| 1856 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1857 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1858 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1859 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1860 | { |
| 1861 | int ret; |
| 1862 | struct buffer_head *bh = NULL; |
| 1863 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1864 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1865 | if (ret) { |
| 1866 | mlog_errno(ret); |
| 1867 | goto out; |
| 1868 | } |
| 1869 | |
| 1870 | *leaf_bh = bh; |
| 1871 | out: |
| 1872 | return ret; |
| 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1877 | * |
| 1878 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1879 | * we need to fix up the extent records above the changes to reflect |
| 1880 | * the new changes. |
| 1881 | * |
| 1882 | * left_rec: the record on the left. |
| 1883 | * left_child_el: is the child list pointed to by left_rec |
| 1884 | * right_rec: the record to the right of left_rec |
| 1885 | * right_child_el: is the child list pointed to by right_rec |
| 1886 | * |
| 1887 | * By definition, this only works on interior nodes. |
| 1888 | */ |
| 1889 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 1890 | struct ocfs2_extent_list *left_child_el, |
| 1891 | struct ocfs2_extent_rec *right_rec, |
| 1892 | struct ocfs2_extent_list *right_child_el) |
| 1893 | { |
| 1894 | u32 left_clusters, right_end; |
| 1895 | |
| 1896 | /* |
| 1897 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1898 | * the leftmost record in their child list. Their cluster |
| 1899 | * count covers the full theoretical range of their child list |
| 1900 | * - the range between their cpos and the cpos of the record |
| 1901 | * immediately to their right. |
| 1902 | */ |
| 1903 | left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos); |
Tao Ma | 82e1264 | 2009-07-23 08:12:58 +0800 | [diff] [blame] | 1904 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 1905 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1906 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1907 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1908 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1909 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1910 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1914 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1915 | * updating e_cpos to keep the same highest cluster count. |
| 1916 | */ |
| 1917 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1918 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1919 | |
| 1920 | right_rec->e_cpos = left_rec->e_cpos; |
| 1921 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 1922 | |
| 1923 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1924 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * Adjust the adjacent root node records involved in a |
| 1929 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 1930 | * find it's index in the root list. |
| 1931 | */ |
| 1932 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 1933 | struct ocfs2_extent_list *left_el, |
| 1934 | struct ocfs2_extent_list *right_el, |
| 1935 | u64 left_el_blkno) |
| 1936 | { |
| 1937 | int i; |
| 1938 | |
| 1939 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 1940 | le16_to_cpu(left_el->l_tree_depth)); |
| 1941 | |
| 1942 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 1943 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 1944 | break; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * The path walking code should have never returned a root and |
| 1949 | * two paths which are not adjacent. |
| 1950 | */ |
| 1951 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 1952 | |
| 1953 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 1954 | &root_el->l_recs[i + 1], right_el); |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * We've changed a leaf block (in right_path) and need to reflect that |
| 1959 | * change back up the subtree. |
| 1960 | * |
| 1961 | * This happens in multiple places: |
| 1962 | * - When we've moved an extent record from the left path leaf to the right |
| 1963 | * path leaf to make room for an empty extent in the left path leaf. |
| 1964 | * - When our insert into the right path leaf is at the leftmost edge |
| 1965 | * and requires an update of the path immediately to it's left. This |
| 1966 | * can occur at the end of some types of rotation and appending inserts. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 1967 | * - When we've adjusted the last extent record in the left path leaf and the |
| 1968 | * 1st extent record in the right path leaf during cross extent block merge. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1969 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 1970 | static void ocfs2_complete_edge_insert(handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1971 | struct ocfs2_path *left_path, |
| 1972 | struct ocfs2_path *right_path, |
| 1973 | int subtree_index) |
| 1974 | { |
| 1975 | int ret, i, idx; |
| 1976 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 1977 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 1978 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 1979 | |
| 1980 | /* |
| 1981 | * Update the counts and position values within all the |
| 1982 | * interior nodes to reflect the leaf rotation we just did. |
| 1983 | * |
| 1984 | * The root node is handled below the loop. |
| 1985 | * |
| 1986 | * We begin the loop with right_el and left_el pointing to the |
| 1987 | * leaf lists and work our way up. |
| 1988 | * |
| 1989 | * NOTE: within this loop, left_el and right_el always refer |
| 1990 | * to the *child* lists. |
| 1991 | */ |
| 1992 | left_el = path_leaf_el(left_path); |
| 1993 | right_el = path_leaf_el(right_path); |
| 1994 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 1995 | mlog(0, "Adjust records at index %u\n", i); |
| 1996 | |
| 1997 | /* |
| 1998 | * One nice property of knowing that all of these |
| 1999 | * nodes are below the root is that we only deal with |
| 2000 | * the leftmost right node record and the rightmost |
| 2001 | * left node record. |
| 2002 | */ |
| 2003 | el = left_path->p_node[i].el; |
| 2004 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2005 | left_rec = &el->l_recs[idx]; |
| 2006 | |
| 2007 | el = right_path->p_node[i].el; |
| 2008 | right_rec = &el->l_recs[0]; |
| 2009 | |
| 2010 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 2011 | right_el); |
| 2012 | |
| 2013 | ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2014 | if (ret) |
| 2015 | mlog_errno(ret); |
| 2016 | |
| 2017 | ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
| 2018 | if (ret) |
| 2019 | mlog_errno(ret); |
| 2020 | |
| 2021 | /* |
| 2022 | * Setup our list pointers now so that the current |
| 2023 | * parents become children in the next iteration. |
| 2024 | */ |
| 2025 | left_el = left_path->p_node[i].el; |
| 2026 | right_el = right_path->p_node[i].el; |
| 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * At the root node, adjust the two adjacent records which |
| 2031 | * begin our path to the leaves. |
| 2032 | */ |
| 2033 | |
| 2034 | el = left_path->p_node[subtree_index].el; |
| 2035 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2036 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2037 | |
| 2038 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2039 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2040 | |
| 2041 | root_bh = left_path->p_node[subtree_index].bh; |
| 2042 | |
| 2043 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 2044 | if (ret) |
| 2045 | mlog_errno(ret); |
| 2046 | } |
| 2047 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2048 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2049 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2050 | struct ocfs2_path *left_path, |
| 2051 | struct ocfs2_path *right_path, |
| 2052 | int subtree_index) |
| 2053 | { |
| 2054 | int ret, i; |
| 2055 | struct buffer_head *right_leaf_bh; |
| 2056 | struct buffer_head *left_leaf_bh = NULL; |
| 2057 | struct buffer_head *root_bh; |
| 2058 | struct ocfs2_extent_list *right_el, *left_el; |
| 2059 | struct ocfs2_extent_rec move_rec; |
| 2060 | |
| 2061 | left_leaf_bh = path_leaf_bh(left_path); |
| 2062 | left_el = path_leaf_el(left_path); |
| 2063 | |
| 2064 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2065 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2066 | "Inode %llu has non-full interior leaf node %llu" |
| 2067 | "(next free = %u)", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2068 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2069 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2070 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2071 | return -EROFS; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * This extent block may already have an empty record, so we |
| 2076 | * return early if so. |
| 2077 | */ |
| 2078 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2079 | return 0; |
| 2080 | |
| 2081 | root_bh = left_path->p_node[subtree_index].bh; |
| 2082 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2083 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2084 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2085 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2086 | if (ret) { |
| 2087 | mlog_errno(ret); |
| 2088 | goto out; |
| 2089 | } |
| 2090 | |
| 2091 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2092 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2093 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2094 | if (ret) { |
| 2095 | mlog_errno(ret); |
| 2096 | goto out; |
| 2097 | } |
| 2098 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2099 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2100 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2101 | if (ret) { |
| 2102 | mlog_errno(ret); |
| 2103 | goto out; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | right_leaf_bh = path_leaf_bh(right_path); |
| 2108 | right_el = path_leaf_el(right_path); |
| 2109 | |
| 2110 | /* This is a code error, not a disk corruption. */ |
| 2111 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2112 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2113 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2114 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2115 | |
| 2116 | ocfs2_create_empty_extent(right_el); |
| 2117 | |
| 2118 | ret = ocfs2_journal_dirty(handle, right_leaf_bh); |
| 2119 | if (ret) { |
| 2120 | mlog_errno(ret); |
| 2121 | goto out; |
| 2122 | } |
| 2123 | |
| 2124 | /* Do the copy now. */ |
| 2125 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2126 | move_rec = left_el->l_recs[i]; |
| 2127 | right_el->l_recs[0] = move_rec; |
| 2128 | |
| 2129 | /* |
| 2130 | * Clear out the record we just copied and shift everything |
| 2131 | * over, leaving an empty extent in the left leaf. |
| 2132 | * |
| 2133 | * We temporarily subtract from next_free_rec so that the |
| 2134 | * shift will lose the tail record (which is now defunct). |
| 2135 | */ |
| 2136 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2137 | ocfs2_shift_records_right(left_el); |
| 2138 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2139 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2140 | |
| 2141 | ret = ocfs2_journal_dirty(handle, left_leaf_bh); |
| 2142 | if (ret) { |
| 2143 | mlog_errno(ret); |
| 2144 | goto out; |
| 2145 | } |
| 2146 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2147 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2148 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2149 | |
| 2150 | out: |
| 2151 | return ret; |
| 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | * Given a full path, determine what cpos value would return us a path |
| 2156 | * containing the leaf immediately to the left of the current one. |
| 2157 | * |
| 2158 | * Will return zero if the path passed in is already the leftmost path. |
| 2159 | */ |
| 2160 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2161 | struct ocfs2_path *path, u32 *cpos) |
| 2162 | { |
| 2163 | int i, j, ret = 0; |
| 2164 | u64 blkno; |
| 2165 | struct ocfs2_extent_list *el; |
| 2166 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2167 | BUG_ON(path->p_tree_depth == 0); |
| 2168 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2169 | *cpos = 0; |
| 2170 | |
| 2171 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2172 | |
| 2173 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2174 | i = path->p_tree_depth - 1; |
| 2175 | while (i >= 0) { |
| 2176 | el = path->p_node[i].el; |
| 2177 | |
| 2178 | /* |
| 2179 | * Find the extent record just before the one in our |
| 2180 | * path. |
| 2181 | */ |
| 2182 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2183 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2184 | if (j == 0) { |
| 2185 | if (i == 0) { |
| 2186 | /* |
| 2187 | * We've determined that the |
| 2188 | * path specified is already |
| 2189 | * the leftmost one - return a |
| 2190 | * cpos of zero. |
| 2191 | */ |
| 2192 | goto out; |
| 2193 | } |
| 2194 | /* |
| 2195 | * The leftmost record points to our |
| 2196 | * leaf - we need to travel up the |
| 2197 | * tree one level. |
| 2198 | */ |
| 2199 | goto next_node; |
| 2200 | } |
| 2201 | |
| 2202 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2203 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2204 | &el->l_recs[j - 1]); |
| 2205 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2206 | goto out; |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | /* |
| 2211 | * If we got here, we never found a valid node where |
| 2212 | * the tree indicated one should be. |
| 2213 | */ |
| 2214 | ocfs2_error(sb, |
| 2215 | "Invalid extent tree at extent block %llu\n", |
| 2216 | (unsigned long long)blkno); |
| 2217 | ret = -EROFS; |
| 2218 | goto out; |
| 2219 | |
| 2220 | next_node: |
| 2221 | blkno = path->p_node[i].bh->b_blocknr; |
| 2222 | i--; |
| 2223 | } |
| 2224 | |
| 2225 | out: |
| 2226 | return ret; |
| 2227 | } |
| 2228 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2229 | /* |
| 2230 | * Extend the transaction by enough credits to complete the rotation, |
| 2231 | * and still leave at least the original number of credits allocated |
| 2232 | * to this transaction. |
| 2233 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2234 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2235 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2236 | struct ocfs2_path *path) |
| 2237 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2238 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2239 | |
| 2240 | if (handle->h_buffer_credits < credits) |
| 2241 | return ocfs2_extend_trans(handle, credits); |
| 2242 | |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * Trap the case where we're inserting into the theoretical range past |
| 2248 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2249 | * whose cpos is less than ours into the right leaf. |
| 2250 | * |
| 2251 | * It's only necessary to look at the rightmost record of the left |
| 2252 | * leaf because the logic that calls us should ensure that the |
| 2253 | * theoretical ranges in the path components above the leaves are |
| 2254 | * correct. |
| 2255 | */ |
| 2256 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2257 | u32 insert_cpos) |
| 2258 | { |
| 2259 | struct ocfs2_extent_list *left_el; |
| 2260 | struct ocfs2_extent_rec *rec; |
| 2261 | int next_free; |
| 2262 | |
| 2263 | left_el = path_leaf_el(left_path); |
| 2264 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2265 | rec = &left_el->l_recs[next_free - 1]; |
| 2266 | |
| 2267 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2268 | return 1; |
| 2269 | return 0; |
| 2270 | } |
| 2271 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2272 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2273 | { |
| 2274 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2275 | unsigned int range; |
| 2276 | struct ocfs2_extent_rec *rec; |
| 2277 | |
| 2278 | if (next_free == 0) |
| 2279 | return 0; |
| 2280 | |
| 2281 | rec = &el->l_recs[0]; |
| 2282 | if (ocfs2_is_empty_extent(rec)) { |
| 2283 | /* Empty list. */ |
| 2284 | if (next_free == 1) |
| 2285 | return 0; |
| 2286 | rec = &el->l_recs[1]; |
| 2287 | } |
| 2288 | |
| 2289 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2290 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2291 | return 1; |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2295 | /* |
| 2296 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2297 | * |
| 2298 | * The path to the rightmost leaf should be passed in. |
| 2299 | * |
| 2300 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2301 | * |
| 2302 | * Upon succesful return from this function: |
| 2303 | * |
| 2304 | * - The 'right_path' array will contain a path to the leaf block |
| 2305 | * whose range contains e_cpos. |
| 2306 | * - That leaf block will have a single empty extent in list index 0. |
| 2307 | * - In the case that the rotation requires a post-insert update, |
| 2308 | * *ret_left_path will contain a valid path which can be passed to |
| 2309 | * ocfs2_insert_path(). |
| 2310 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2311 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2312 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2313 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2314 | u32 insert_cpos, |
| 2315 | struct ocfs2_path *right_path, |
| 2316 | struct ocfs2_path **ret_left_path) |
| 2317 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2318 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2319 | u32 cpos; |
| 2320 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2321 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2322 | |
| 2323 | *ret_left_path = NULL; |
| 2324 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2325 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2326 | if (!left_path) { |
| 2327 | ret = -ENOMEM; |
| 2328 | mlog_errno(ret); |
| 2329 | goto out; |
| 2330 | } |
| 2331 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2332 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2333 | if (ret) { |
| 2334 | mlog_errno(ret); |
| 2335 | goto out; |
| 2336 | } |
| 2337 | |
| 2338 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 2339 | |
| 2340 | /* |
| 2341 | * What we want to do here is: |
| 2342 | * |
| 2343 | * 1) Start with the rightmost path. |
| 2344 | * |
| 2345 | * 2) Determine a path to the leaf block directly to the left |
| 2346 | * of that leaf. |
| 2347 | * |
| 2348 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2349 | * which contains a path to both leaves. |
| 2350 | * |
| 2351 | * 4) Rotate the subtree. |
| 2352 | * |
| 2353 | * 5) Find the next subtree by considering the left path to be |
| 2354 | * the new right path. |
| 2355 | * |
| 2356 | * The check at the top of this while loop also accepts |
| 2357 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2358 | * value to get us the left path - insert_cpos might very well |
| 2359 | * be filling that hole. |
| 2360 | * |
| 2361 | * Stop at a cpos of '0' because we either started at the |
| 2362 | * leftmost branch (i.e., a tree with one branch and a |
| 2363 | * rotation inside of it), or we've gone as far as we can in |
| 2364 | * rotating subtrees. |
| 2365 | */ |
| 2366 | while (cpos && insert_cpos <= cpos) { |
| 2367 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 2368 | insert_cpos, cpos); |
| 2369 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2370 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2371 | if (ret) { |
| 2372 | mlog_errno(ret); |
| 2373 | goto out; |
| 2374 | } |
| 2375 | |
| 2376 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2377 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2378 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2379 | "(left path cpos %u) results in two identical " |
| 2380 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2381 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2382 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2383 | (unsigned long long) |
| 2384 | path_leaf_bh(left_path)->b_blocknr); |
| 2385 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2386 | if (split == SPLIT_NONE && |
| 2387 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2388 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2389 | |
| 2390 | /* |
| 2391 | * We've rotated the tree as much as we |
| 2392 | * should. The rest is up to |
| 2393 | * ocfs2_insert_path() to complete, after the |
| 2394 | * record insertion. We indicate this |
| 2395 | * situation by returning the left path. |
| 2396 | * |
| 2397 | * The reason we don't adjust the records here |
| 2398 | * before the record insert is that an error |
| 2399 | * later might break the rule where a parent |
| 2400 | * record e_cpos will reflect the actual |
| 2401 | * e_cpos of the 1st nonempty record of the |
| 2402 | * child list. |
| 2403 | */ |
| 2404 | *ret_left_path = left_path; |
| 2405 | goto out_ret_path; |
| 2406 | } |
| 2407 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2408 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2409 | |
| 2410 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2411 | start, |
| 2412 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 2413 | right_path->p_tree_depth); |
| 2414 | |
| 2415 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2416 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2417 | if (ret) { |
| 2418 | mlog_errno(ret); |
| 2419 | goto out; |
| 2420 | } |
| 2421 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2422 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2423 | right_path, start); |
| 2424 | if (ret) { |
| 2425 | mlog_errno(ret); |
| 2426 | goto out; |
| 2427 | } |
| 2428 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2429 | if (split != SPLIT_NONE && |
| 2430 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2431 | insert_cpos)) { |
| 2432 | /* |
| 2433 | * A rotate moves the rightmost left leaf |
| 2434 | * record over to the leftmost right leaf |
| 2435 | * slot. If we're doing an extent split |
| 2436 | * instead of a real insert, then we have to |
| 2437 | * check that the extent to be split wasn't |
| 2438 | * just moved over. If it was, then we can |
| 2439 | * exit here, passing left_path back - |
| 2440 | * ocfs2_split_extent() is smart enough to |
| 2441 | * search both leaves. |
| 2442 | */ |
| 2443 | *ret_left_path = left_path; |
| 2444 | goto out_ret_path; |
| 2445 | } |
| 2446 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2447 | /* |
| 2448 | * There is no need to re-read the next right path |
| 2449 | * as we know that it'll be our current left |
| 2450 | * path. Optimize by copying values instead. |
| 2451 | */ |
| 2452 | ocfs2_mv_path(right_path, left_path); |
| 2453 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2454 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2455 | if (ret) { |
| 2456 | mlog_errno(ret); |
| 2457 | goto out; |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | out: |
| 2462 | ocfs2_free_path(left_path); |
| 2463 | |
| 2464 | out_ret_path: |
| 2465 | return ret; |
| 2466 | } |
| 2467 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame^] | 2468 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2469 | struct ocfs2_extent_tree *et, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2470 | int subtree_index, struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2471 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2472 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2473 | struct ocfs2_extent_rec *rec; |
| 2474 | struct ocfs2_extent_list *el; |
| 2475 | struct ocfs2_extent_block *eb; |
| 2476 | u32 range; |
| 2477 | |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2478 | /* |
| 2479 | * In normal tree rotation process, we will never touch the |
| 2480 | * tree branch above subtree_index and ocfs2_extend_rotate_transaction |
| 2481 | * doesn't reserve the credits for them either. |
| 2482 | * |
| 2483 | * But we do have a special case here which will update the rightmost |
| 2484 | * records for all the bh in the path. |
| 2485 | * So we have to allocate extra credits and access them. |
| 2486 | */ |
| 2487 | ret = ocfs2_extend_trans(handle, |
| 2488 | handle->h_buffer_credits + subtree_index); |
| 2489 | if (ret) { |
| 2490 | mlog_errno(ret); |
| 2491 | goto out; |
| 2492 | } |
| 2493 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame^] | 2494 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2495 | if (ret) { |
| 2496 | mlog_errno(ret); |
| 2497 | goto out; |
| 2498 | } |
| 2499 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2500 | /* Path should always be rightmost. */ |
| 2501 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2502 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2503 | |
| 2504 | el = &eb->h_list; |
| 2505 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2506 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2507 | rec = &el->l_recs[idx]; |
| 2508 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2509 | |
| 2510 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2511 | el = path->p_node[i].el; |
| 2512 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2513 | rec = &el->l_recs[idx]; |
| 2514 | |
| 2515 | rec->e_int_clusters = cpu_to_le32(range); |
| 2516 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2517 | |
| 2518 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2519 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2520 | out: |
| 2521 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2524 | static void ocfs2_unlink_path(handle_t *handle, |
| 2525 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2526 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2527 | struct ocfs2_path *path, int unlink_start) |
| 2528 | { |
| 2529 | int ret, i; |
| 2530 | struct ocfs2_extent_block *eb; |
| 2531 | struct ocfs2_extent_list *el; |
| 2532 | struct buffer_head *bh; |
| 2533 | |
| 2534 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2535 | bh = path->p_node[i].bh; |
| 2536 | |
| 2537 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2538 | /* |
| 2539 | * Not all nodes might have had their final count |
| 2540 | * decremented by the caller - handle this here. |
| 2541 | */ |
| 2542 | el = &eb->h_list; |
| 2543 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2544 | mlog(ML_ERROR, |
| 2545 | "Inode %llu, attempted to remove extent block " |
| 2546 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2547 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2548 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2549 | le16_to_cpu(el->l_next_free_rec)); |
| 2550 | |
| 2551 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2552 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2553 | continue; |
| 2554 | } |
| 2555 | |
| 2556 | el->l_next_free_rec = 0; |
| 2557 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2558 | |
| 2559 | ocfs2_journal_dirty(handle, bh); |
| 2560 | |
| 2561 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2562 | if (ret) |
| 2563 | mlog_errno(ret); |
| 2564 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2565 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2569 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2570 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2571 | struct ocfs2_path *left_path, |
| 2572 | struct ocfs2_path *right_path, |
| 2573 | int subtree_index, |
| 2574 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2575 | { |
| 2576 | int i; |
| 2577 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2578 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2579 | struct ocfs2_extent_list *el; |
| 2580 | struct ocfs2_extent_block *eb; |
| 2581 | |
| 2582 | el = path_leaf_el(left_path); |
| 2583 | |
| 2584 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2585 | |
| 2586 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2587 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2588 | break; |
| 2589 | |
| 2590 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2591 | |
| 2592 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2593 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2594 | |
| 2595 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2596 | eb->h_next_leaf_blk = 0; |
| 2597 | |
| 2598 | ocfs2_journal_dirty(handle, root_bh); |
| 2599 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2600 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2601 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2602 | subtree_index + 1); |
| 2603 | } |
| 2604 | |
| 2605 | static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, |
| 2606 | struct ocfs2_path *left_path, |
| 2607 | struct ocfs2_path *right_path, |
| 2608 | int subtree_index, |
| 2609 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2610 | int *deleted, |
| 2611 | struct ocfs2_extent_tree *et) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2612 | { |
| 2613 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2614 | struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2615 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2616 | struct ocfs2_extent_block *eb; |
| 2617 | |
| 2618 | *deleted = 0; |
| 2619 | |
| 2620 | right_leaf_el = path_leaf_el(right_path); |
| 2621 | left_leaf_el = path_leaf_el(left_path); |
| 2622 | root_bh = left_path->p_node[subtree_index].bh; |
| 2623 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2624 | |
| 2625 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2626 | return 0; |
| 2627 | |
| 2628 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2629 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2630 | /* |
| 2631 | * It's legal for us to proceed if the right leaf is |
| 2632 | * the rightmost one and it has an empty extent. There |
| 2633 | * are two cases to handle - whether the leaf will be |
| 2634 | * empty after removal or not. If the leaf isn't empty |
| 2635 | * then just remove the empty extent up front. The |
| 2636 | * next block will handle empty leaves by flagging |
| 2637 | * them for unlink. |
| 2638 | * |
| 2639 | * Non rightmost leaves will throw -EAGAIN and the |
| 2640 | * caller can manually move the subtree and retry. |
| 2641 | */ |
| 2642 | |
| 2643 | if (eb->h_next_leaf_blk != 0ULL) |
| 2644 | return -EAGAIN; |
| 2645 | |
| 2646 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2647 | ret = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2648 | path_leaf_bh(right_path), |
| 2649 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2650 | if (ret) { |
| 2651 | mlog_errno(ret); |
| 2652 | goto out; |
| 2653 | } |
| 2654 | |
| 2655 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2656 | } else |
| 2657 | right_has_empty = 1; |
| 2658 | } |
| 2659 | |
| 2660 | if (eb->h_next_leaf_blk == 0ULL && |
| 2661 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2662 | /* |
| 2663 | * We have to update i_last_eb_blk during the meta |
| 2664 | * data delete. |
| 2665 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2666 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2667 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2668 | if (ret) { |
| 2669 | mlog_errno(ret); |
| 2670 | goto out; |
| 2671 | } |
| 2672 | |
| 2673 | del_right_subtree = 1; |
| 2674 | } |
| 2675 | |
| 2676 | /* |
| 2677 | * Getting here with an empty extent in the right path implies |
| 2678 | * that it's the rightmost path and will be deleted. |
| 2679 | */ |
| 2680 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2681 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2682 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2683 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2684 | if (ret) { |
| 2685 | mlog_errno(ret); |
| 2686 | goto out; |
| 2687 | } |
| 2688 | |
| 2689 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2690 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2691 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2692 | if (ret) { |
| 2693 | mlog_errno(ret); |
| 2694 | goto out; |
| 2695 | } |
| 2696 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2697 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2698 | left_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2699 | if (ret) { |
| 2700 | mlog_errno(ret); |
| 2701 | goto out; |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | if (!right_has_empty) { |
| 2706 | /* |
| 2707 | * Only do this if we're moving a real |
| 2708 | * record. Otherwise, the action is delayed until |
| 2709 | * after removal of the right path in which case we |
| 2710 | * can do a simple shift to remove the empty extent. |
| 2711 | */ |
| 2712 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2713 | memset(&right_leaf_el->l_recs[0], 0, |
| 2714 | sizeof(struct ocfs2_extent_rec)); |
| 2715 | } |
| 2716 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2717 | /* |
| 2718 | * Move recs over to get rid of empty extent, decrease |
| 2719 | * next_free. This is allowed to remove the last |
| 2720 | * extent in our leaf (setting l_next_free_rec to |
| 2721 | * zero) - the delete code below won't care. |
| 2722 | */ |
| 2723 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2724 | } |
| 2725 | |
| 2726 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2727 | if (ret) |
| 2728 | mlog_errno(ret); |
| 2729 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 2730 | if (ret) |
| 2731 | mlog_errno(ret); |
| 2732 | |
| 2733 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2734 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2735 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame^] | 2736 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2737 | left_path); |
| 2738 | if (ret) { |
| 2739 | mlog_errno(ret); |
| 2740 | goto out; |
| 2741 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2742 | |
| 2743 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2744 | ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2745 | |
| 2746 | /* |
| 2747 | * Removal of the extent in the left leaf was skipped |
| 2748 | * above so we could delete the right path |
| 2749 | * 1st. |
| 2750 | */ |
| 2751 | if (right_has_empty) |
| 2752 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2753 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2754 | ret = ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2755 | if (ret) |
| 2756 | mlog_errno(ret); |
| 2757 | |
| 2758 | *deleted = 1; |
| 2759 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2760 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2761 | subtree_index); |
| 2762 | |
| 2763 | out: |
| 2764 | return ret; |
| 2765 | } |
| 2766 | |
| 2767 | /* |
| 2768 | * Given a full path, determine what cpos value would return us a path |
| 2769 | * containing the leaf immediately to the right of the current one. |
| 2770 | * |
| 2771 | * Will return zero if the path passed in is already the rightmost path. |
| 2772 | * |
| 2773 | * This looks similar, but is subtly different to |
| 2774 | * ocfs2_find_cpos_for_left_leaf(). |
| 2775 | */ |
| 2776 | static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2777 | struct ocfs2_path *path, u32 *cpos) |
| 2778 | { |
| 2779 | int i, j, ret = 0; |
| 2780 | u64 blkno; |
| 2781 | struct ocfs2_extent_list *el; |
| 2782 | |
| 2783 | *cpos = 0; |
| 2784 | |
| 2785 | if (path->p_tree_depth == 0) |
| 2786 | return 0; |
| 2787 | |
| 2788 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2789 | |
| 2790 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2791 | i = path->p_tree_depth - 1; |
| 2792 | while (i >= 0) { |
| 2793 | int next_free; |
| 2794 | |
| 2795 | el = path->p_node[i].el; |
| 2796 | |
| 2797 | /* |
| 2798 | * Find the extent record just after the one in our |
| 2799 | * path. |
| 2800 | */ |
| 2801 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2802 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2803 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2804 | if (j == (next_free - 1)) { |
| 2805 | if (i == 0) { |
| 2806 | /* |
| 2807 | * We've determined that the |
| 2808 | * path specified is already |
| 2809 | * the rightmost one - return a |
| 2810 | * cpos of zero. |
| 2811 | */ |
| 2812 | goto out; |
| 2813 | } |
| 2814 | /* |
| 2815 | * The rightmost record points to our |
| 2816 | * leaf - we need to travel up the |
| 2817 | * tree one level. |
| 2818 | */ |
| 2819 | goto next_node; |
| 2820 | } |
| 2821 | |
| 2822 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2823 | goto out; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | /* |
| 2828 | * If we got here, we never found a valid node where |
| 2829 | * the tree indicated one should be. |
| 2830 | */ |
| 2831 | ocfs2_error(sb, |
| 2832 | "Invalid extent tree at extent block %llu\n", |
| 2833 | (unsigned long long)blkno); |
| 2834 | ret = -EROFS; |
| 2835 | goto out; |
| 2836 | |
| 2837 | next_node: |
| 2838 | blkno = path->p_node[i].bh->b_blocknr; |
| 2839 | i--; |
| 2840 | } |
| 2841 | |
| 2842 | out: |
| 2843 | return ret; |
| 2844 | } |
| 2845 | |
| 2846 | static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode, |
| 2847 | handle_t *handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2848 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2849 | { |
| 2850 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2851 | struct buffer_head *bh = path_leaf_bh(path); |
| 2852 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2853 | |
| 2854 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2855 | return 0; |
| 2856 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2857 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2858 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2859 | if (ret) { |
| 2860 | mlog_errno(ret); |
| 2861 | goto out; |
| 2862 | } |
| 2863 | |
| 2864 | ocfs2_remove_empty_extent(el); |
| 2865 | |
| 2866 | ret = ocfs2_journal_dirty(handle, bh); |
| 2867 | if (ret) |
| 2868 | mlog_errno(ret); |
| 2869 | |
| 2870 | out: |
| 2871 | return ret; |
| 2872 | } |
| 2873 | |
| 2874 | static int __ocfs2_rotate_tree_left(struct inode *inode, |
| 2875 | handle_t *handle, int orig_credits, |
| 2876 | struct ocfs2_path *path, |
| 2877 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2878 | struct ocfs2_path **empty_extent_path, |
| 2879 | struct ocfs2_extent_tree *et) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2880 | { |
| 2881 | int ret, subtree_root, deleted; |
| 2882 | u32 right_cpos; |
| 2883 | struct ocfs2_path *left_path = NULL; |
| 2884 | struct ocfs2_path *right_path = NULL; |
| 2885 | |
| 2886 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 2887 | |
| 2888 | *empty_extent_path = NULL; |
| 2889 | |
| 2890 | ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path, |
| 2891 | &right_cpos); |
| 2892 | if (ret) { |
| 2893 | mlog_errno(ret); |
| 2894 | goto out; |
| 2895 | } |
| 2896 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2897 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2898 | if (!left_path) { |
| 2899 | ret = -ENOMEM; |
| 2900 | mlog_errno(ret); |
| 2901 | goto out; |
| 2902 | } |
| 2903 | |
| 2904 | ocfs2_cp_path(left_path, path); |
| 2905 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2906 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2907 | if (!right_path) { |
| 2908 | ret = -ENOMEM; |
| 2909 | mlog_errno(ret); |
| 2910 | goto out; |
| 2911 | } |
| 2912 | |
| 2913 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 2914 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2915 | if (ret) { |
| 2916 | mlog_errno(ret); |
| 2917 | goto out; |
| 2918 | } |
| 2919 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2920 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2921 | right_path); |
| 2922 | |
| 2923 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2924 | subtree_root, |
| 2925 | (unsigned long long) |
| 2926 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 2927 | right_path->p_tree_depth); |
| 2928 | |
| 2929 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 2930 | orig_credits, left_path); |
| 2931 | if (ret) { |
| 2932 | mlog_errno(ret); |
| 2933 | goto out; |
| 2934 | } |
| 2935 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2936 | /* |
| 2937 | * Caller might still want to make changes to the |
| 2938 | * tree root, so re-add it to the journal here. |
| 2939 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2940 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2941 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2942 | if (ret) { |
| 2943 | mlog_errno(ret); |
| 2944 | goto out; |
| 2945 | } |
| 2946 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2947 | ret = ocfs2_rotate_subtree_left(inode, handle, left_path, |
| 2948 | right_path, subtree_root, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2949 | dealloc, &deleted, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2950 | if (ret == -EAGAIN) { |
| 2951 | /* |
| 2952 | * The rotation has to temporarily stop due to |
| 2953 | * the right subtree having an empty |
| 2954 | * extent. Pass it back to the caller for a |
| 2955 | * fixup. |
| 2956 | */ |
| 2957 | *empty_extent_path = right_path; |
| 2958 | right_path = NULL; |
| 2959 | goto out; |
| 2960 | } |
| 2961 | if (ret) { |
| 2962 | mlog_errno(ret); |
| 2963 | goto out; |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * The subtree rotate might have removed records on |
| 2968 | * the rightmost edge. If so, then rotation is |
| 2969 | * complete. |
| 2970 | */ |
| 2971 | if (deleted) |
| 2972 | break; |
| 2973 | |
| 2974 | ocfs2_mv_path(left_path, right_path); |
| 2975 | |
| 2976 | ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path, |
| 2977 | &right_cpos); |
| 2978 | if (ret) { |
| 2979 | mlog_errno(ret); |
| 2980 | goto out; |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | out: |
| 2985 | ocfs2_free_path(right_path); |
| 2986 | ocfs2_free_path(left_path); |
| 2987 | |
| 2988 | return ret; |
| 2989 | } |
| 2990 | |
| 2991 | static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2992 | struct ocfs2_path *path, |
| 2993 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2994 | struct ocfs2_extent_tree *et) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2995 | { |
| 2996 | int ret, subtree_index; |
| 2997 | u32 cpos; |
| 2998 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2999 | struct ocfs2_extent_block *eb; |
| 3000 | struct ocfs2_extent_list *el; |
| 3001 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3002 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 3003 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3004 | if (ret) |
| 3005 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3006 | /* |
| 3007 | * There's two ways we handle this depending on |
| 3008 | * whether path is the only existing one. |
| 3009 | */ |
| 3010 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3011 | handle->h_buffer_credits, |
| 3012 | path); |
| 3013 | if (ret) { |
| 3014 | mlog_errno(ret); |
| 3015 | goto out; |
| 3016 | } |
| 3017 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3018 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3019 | if (ret) { |
| 3020 | mlog_errno(ret); |
| 3021 | goto out; |
| 3022 | } |
| 3023 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3024 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3025 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3026 | if (ret) { |
| 3027 | mlog_errno(ret); |
| 3028 | goto out; |
| 3029 | } |
| 3030 | |
| 3031 | if (cpos) { |
| 3032 | /* |
| 3033 | * We have a path to the left of this one - it needs |
| 3034 | * an update too. |
| 3035 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3036 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3037 | if (!left_path) { |
| 3038 | ret = -ENOMEM; |
| 3039 | mlog_errno(ret); |
| 3040 | goto out; |
| 3041 | } |
| 3042 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3043 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3044 | if (ret) { |
| 3045 | mlog_errno(ret); |
| 3046 | goto out; |
| 3047 | } |
| 3048 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3049 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3050 | if (ret) { |
| 3051 | mlog_errno(ret); |
| 3052 | goto out; |
| 3053 | } |
| 3054 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3055 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3056 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3057 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3058 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame^] | 3059 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3060 | left_path); |
| 3061 | if (ret) { |
| 3062 | mlog_errno(ret); |
| 3063 | goto out; |
| 3064 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3065 | |
| 3066 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3067 | ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3068 | } else { |
| 3069 | /* |
| 3070 | * 'path' is also the leftmost path which |
| 3071 | * means it must be the only one. This gets |
| 3072 | * handled differently because we want to |
| 3073 | * revert the inode back to having extents |
| 3074 | * in-line. |
| 3075 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3076 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3077 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3078 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3079 | el->l_tree_depth = 0; |
| 3080 | el->l_next_free_rec = 0; |
| 3081 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3082 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3083 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3087 | |
| 3088 | out: |
| 3089 | ocfs2_free_path(left_path); |
| 3090 | return ret; |
| 3091 | } |
| 3092 | |
| 3093 | /* |
| 3094 | * Left rotation of btree records. |
| 3095 | * |
| 3096 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3097 | * rotation. We start at some non-rightmost path containing an empty |
| 3098 | * extent in the leaf block. The code works its way to the rightmost |
| 3099 | * path by rotating records to the left in every subtree. |
| 3100 | * |
| 3101 | * This is used by any code which reduces the number of extent records |
| 3102 | * in a leaf. After removal, an empty record should be placed in the |
| 3103 | * leftmost list position. |
| 3104 | * |
| 3105 | * This won't handle a length update of the rightmost path records if |
| 3106 | * the rightmost tree leaf record is removed so the caller is |
| 3107 | * responsible for detecting and correcting that. |
| 3108 | */ |
| 3109 | static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, |
| 3110 | struct ocfs2_path *path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3111 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3112 | struct ocfs2_extent_tree *et) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3113 | { |
| 3114 | int ret, orig_credits = handle->h_buffer_credits; |
| 3115 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3116 | struct ocfs2_extent_block *eb; |
| 3117 | struct ocfs2_extent_list *el; |
| 3118 | |
| 3119 | el = path_leaf_el(path); |
| 3120 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3121 | return 0; |
| 3122 | |
| 3123 | if (path->p_tree_depth == 0) { |
| 3124 | rightmost_no_delete: |
| 3125 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3126 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3127 | * it up front. |
| 3128 | */ |
| 3129 | ret = ocfs2_rotate_rightmost_leaf_left(inode, handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3130 | path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3131 | if (ret) |
| 3132 | mlog_errno(ret); |
| 3133 | goto out; |
| 3134 | } |
| 3135 | |
| 3136 | /* |
| 3137 | * Handle rightmost branch now. There's several cases: |
| 3138 | * 1) simple rotation leaving records in there. That's trivial. |
| 3139 | * 2) rotation requiring a branch delete - there's no more |
| 3140 | * records left. Two cases of this: |
| 3141 | * a) There are branches to the left. |
| 3142 | * b) This is also the leftmost (the only) branch. |
| 3143 | * |
| 3144 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3145 | * 2a) we need the left branch so that we can update it with the unlink |
| 3146 | * 2b) we need to bring the inode back to inline extents. |
| 3147 | */ |
| 3148 | |
| 3149 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3150 | el = &eb->h_list; |
| 3151 | if (eb->h_next_leaf_blk == 0) { |
| 3152 | /* |
| 3153 | * This gets a bit tricky if we're going to delete the |
| 3154 | * rightmost path. Get the other cases out of the way |
| 3155 | * 1st. |
| 3156 | */ |
| 3157 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3158 | goto rightmost_no_delete; |
| 3159 | |
| 3160 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3161 | ret = -EIO; |
| 3162 | ocfs2_error(inode->i_sb, |
| 3163 | "Inode %llu has empty extent block at %llu", |
| 3164 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 3165 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3166 | goto out; |
| 3167 | } |
| 3168 | |
| 3169 | /* |
| 3170 | * XXX: The caller can not trust "path" any more after |
| 3171 | * this as it will have been deleted. What do we do? |
| 3172 | * |
| 3173 | * In theory the rotate-for-merge code will never get |
| 3174 | * here because it'll always ask for a rotate in a |
| 3175 | * nonempty list. |
| 3176 | */ |
| 3177 | |
| 3178 | ret = ocfs2_remove_rightmost_path(inode, handle, path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3179 | dealloc, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3180 | if (ret) |
| 3181 | mlog_errno(ret); |
| 3182 | goto out; |
| 3183 | } |
| 3184 | |
| 3185 | /* |
| 3186 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3187 | * and restarting from there. |
| 3188 | */ |
| 3189 | try_rotate: |
| 3190 | ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3191 | dealloc, &restart_path, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3192 | if (ret && ret != -EAGAIN) { |
| 3193 | mlog_errno(ret); |
| 3194 | goto out; |
| 3195 | } |
| 3196 | |
| 3197 | while (ret == -EAGAIN) { |
| 3198 | tmp_path = restart_path; |
| 3199 | restart_path = NULL; |
| 3200 | |
| 3201 | ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, |
| 3202 | tmp_path, dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3203 | &restart_path, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3204 | if (ret && ret != -EAGAIN) { |
| 3205 | mlog_errno(ret); |
| 3206 | goto out; |
| 3207 | } |
| 3208 | |
| 3209 | ocfs2_free_path(tmp_path); |
| 3210 | tmp_path = NULL; |
| 3211 | |
| 3212 | if (ret == 0) |
| 3213 | goto try_rotate; |
| 3214 | } |
| 3215 | |
| 3216 | out: |
| 3217 | ocfs2_free_path(tmp_path); |
| 3218 | ocfs2_free_path(restart_path); |
| 3219 | return ret; |
| 3220 | } |
| 3221 | |
| 3222 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3223 | int index) |
| 3224 | { |
| 3225 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3226 | unsigned int size; |
| 3227 | |
| 3228 | if (rec->e_leaf_clusters == 0) { |
| 3229 | /* |
| 3230 | * We consumed all of the merged-from record. An empty |
| 3231 | * extent cannot exist anywhere but the 1st array |
| 3232 | * position, so move things over if the merged-from |
| 3233 | * record doesn't occupy that position. |
| 3234 | * |
| 3235 | * This creates a new empty extent so the caller |
| 3236 | * should be smart enough to have removed any existing |
| 3237 | * ones. |
| 3238 | */ |
| 3239 | if (index > 0) { |
| 3240 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3241 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3242 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3243 | } |
| 3244 | |
| 3245 | /* |
| 3246 | * Always memset - the caller doesn't check whether it |
| 3247 | * created an empty extent, so there could be junk in |
| 3248 | * the other fields. |
| 3249 | */ |
| 3250 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3251 | } |
| 3252 | } |
| 3253 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3254 | static int ocfs2_get_right_path(struct inode *inode, |
| 3255 | struct ocfs2_path *left_path, |
| 3256 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3257 | { |
| 3258 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3259 | u32 right_cpos; |
| 3260 | struct ocfs2_path *right_path = NULL; |
| 3261 | struct ocfs2_extent_list *left_el; |
| 3262 | |
| 3263 | *ret_right_path = NULL; |
| 3264 | |
| 3265 | /* This function shouldn't be called for non-trees. */ |
| 3266 | BUG_ON(left_path->p_tree_depth == 0); |
| 3267 | |
| 3268 | left_el = path_leaf_el(left_path); |
| 3269 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3270 | |
| 3271 | ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path, |
| 3272 | &right_cpos); |
| 3273 | if (ret) { |
| 3274 | mlog_errno(ret); |
| 3275 | goto out; |
| 3276 | } |
| 3277 | |
| 3278 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3279 | BUG_ON(right_cpos == 0); |
| 3280 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3281 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3282 | if (!right_path) { |
| 3283 | ret = -ENOMEM; |
| 3284 | mlog_errno(ret); |
| 3285 | goto out; |
| 3286 | } |
| 3287 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3288 | ret = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3289 | if (ret) { |
| 3290 | mlog_errno(ret); |
| 3291 | goto out; |
| 3292 | } |
| 3293 | |
| 3294 | *ret_right_path = right_path; |
| 3295 | out: |
| 3296 | if (ret) |
| 3297 | ocfs2_free_path(right_path); |
| 3298 | return ret; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * Remove split_rec clusters from the record at index and merge them |
| 3303 | * onto the beginning of the record "next" to it. |
| 3304 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3305 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3306 | * next extent block. |
| 3307 | */ |
| 3308 | static int ocfs2_merge_rec_right(struct inode *inode, |
| 3309 | struct ocfs2_path *left_path, |
| 3310 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3311 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3312 | struct ocfs2_extent_rec *split_rec, |
| 3313 | int index) |
| 3314 | { |
| 3315 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3316 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3317 | struct ocfs2_extent_rec *left_rec; |
| 3318 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3319 | struct ocfs2_extent_list *right_el; |
| 3320 | struct ocfs2_path *right_path = NULL; |
| 3321 | int subtree_index = 0; |
| 3322 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3323 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3324 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3325 | |
| 3326 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3327 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3328 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3329 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3330 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3331 | /* we meet with a cross extent block merge. */ |
| 3332 | ret = ocfs2_get_right_path(inode, left_path, &right_path); |
| 3333 | if (ret) { |
| 3334 | mlog_errno(ret); |
| 3335 | goto out; |
| 3336 | } |
| 3337 | |
| 3338 | right_el = path_leaf_el(right_path); |
| 3339 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3340 | BUG_ON(next_free <= 0); |
| 3341 | right_rec = &right_el->l_recs[0]; |
| 3342 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3343 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3344 | right_rec = &right_el->l_recs[1]; |
| 3345 | } |
| 3346 | |
| 3347 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3348 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3349 | le32_to_cpu(right_rec->e_cpos)); |
| 3350 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3351 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3352 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3353 | |
| 3354 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3355 | handle->h_buffer_credits, |
| 3356 | right_path); |
| 3357 | if (ret) { |
| 3358 | mlog_errno(ret); |
| 3359 | goto out; |
| 3360 | } |
| 3361 | |
| 3362 | root_bh = left_path->p_node[subtree_index].bh; |
| 3363 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3364 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3365 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3366 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3367 | if (ret) { |
| 3368 | mlog_errno(ret); |
| 3369 | goto out; |
| 3370 | } |
| 3371 | |
| 3372 | for (i = subtree_index + 1; |
| 3373 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3374 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3375 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3376 | if (ret) { |
| 3377 | mlog_errno(ret); |
| 3378 | goto out; |
| 3379 | } |
| 3380 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3381 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3382 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3383 | if (ret) { |
| 3384 | mlog_errno(ret); |
| 3385 | goto out; |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | } else { |
| 3390 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3391 | right_rec = &el->l_recs[index + 1]; |
| 3392 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3393 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3394 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3395 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3396 | if (ret) { |
| 3397 | mlog_errno(ret); |
| 3398 | goto out; |
| 3399 | } |
| 3400 | |
| 3401 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3402 | |
| 3403 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3404 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3405 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3406 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3407 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3408 | |
| 3409 | ocfs2_cleanup_merge(el, index); |
| 3410 | |
| 3411 | ret = ocfs2_journal_dirty(handle, bh); |
| 3412 | if (ret) |
| 3413 | mlog_errno(ret); |
| 3414 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3415 | if (right_path) { |
| 3416 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 3417 | if (ret) |
| 3418 | mlog_errno(ret); |
| 3419 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3420 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3421 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3422 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3423 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3424 | if (right_path) |
| 3425 | ocfs2_free_path(right_path); |
| 3426 | return ret; |
| 3427 | } |
| 3428 | |
| 3429 | static int ocfs2_get_left_path(struct inode *inode, |
| 3430 | struct ocfs2_path *right_path, |
| 3431 | struct ocfs2_path **ret_left_path) |
| 3432 | { |
| 3433 | int ret; |
| 3434 | u32 left_cpos; |
| 3435 | struct ocfs2_path *left_path = NULL; |
| 3436 | |
| 3437 | *ret_left_path = NULL; |
| 3438 | |
| 3439 | /* This function shouldn't be called for non-trees. */ |
| 3440 | BUG_ON(right_path->p_tree_depth == 0); |
| 3441 | |
| 3442 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, |
| 3443 | right_path, &left_cpos); |
| 3444 | if (ret) { |
| 3445 | mlog_errno(ret); |
| 3446 | goto out; |
| 3447 | } |
| 3448 | |
| 3449 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3450 | BUG_ON(left_cpos == 0); |
| 3451 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3452 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3453 | if (!left_path) { |
| 3454 | ret = -ENOMEM; |
| 3455 | mlog_errno(ret); |
| 3456 | goto out; |
| 3457 | } |
| 3458 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3459 | ret = ocfs2_find_path(INODE_CACHE(inode), left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3460 | if (ret) { |
| 3461 | mlog_errno(ret); |
| 3462 | goto out; |
| 3463 | } |
| 3464 | |
| 3465 | *ret_left_path = left_path; |
| 3466 | out: |
| 3467 | if (ret) |
| 3468 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3469 | return ret; |
| 3470 | } |
| 3471 | |
| 3472 | /* |
| 3473 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3474 | * onto the tail of the record "before" it. |
| 3475 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3476 | * |
| 3477 | * For index == 0, the "before" means the last record of the previous |
| 3478 | * extent block. And there is also a situation that we may need to |
| 3479 | * remove the rightmost leaf extent block in the right_path and change |
| 3480 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3481 | */ |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3482 | static int ocfs2_merge_rec_left(struct inode *inode, |
| 3483 | struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3484 | handle_t *handle, |
| 3485 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3486 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3487 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3488 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3489 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3490 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3491 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3492 | struct ocfs2_extent_rec *left_rec; |
| 3493 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3494 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3495 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3496 | struct buffer_head *root_bh = NULL; |
| 3497 | struct ocfs2_path *left_path = NULL; |
| 3498 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3499 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3500 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3501 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3502 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3503 | if (index == 0) { |
| 3504 | /* we meet with a cross extent block merge. */ |
| 3505 | ret = ocfs2_get_left_path(inode, right_path, &left_path); |
| 3506 | if (ret) { |
| 3507 | mlog_errno(ret); |
| 3508 | goto out; |
| 3509 | } |
| 3510 | |
| 3511 | left_el = path_leaf_el(left_path); |
| 3512 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3513 | le16_to_cpu(left_el->l_count)); |
| 3514 | |
| 3515 | left_rec = &left_el->l_recs[ |
| 3516 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3517 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3518 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3519 | le32_to_cpu(split_rec->e_cpos)); |
| 3520 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3521 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3522 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3523 | |
| 3524 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3525 | handle->h_buffer_credits, |
| 3526 | left_path); |
| 3527 | if (ret) { |
| 3528 | mlog_errno(ret); |
| 3529 | goto out; |
| 3530 | } |
| 3531 | |
| 3532 | root_bh = left_path->p_node[subtree_index].bh; |
| 3533 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3534 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3535 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3536 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3537 | if (ret) { |
| 3538 | mlog_errno(ret); |
| 3539 | goto out; |
| 3540 | } |
| 3541 | |
| 3542 | for (i = subtree_index + 1; |
| 3543 | i < path_num_items(right_path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3544 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3545 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3546 | if (ret) { |
| 3547 | mlog_errno(ret); |
| 3548 | goto out; |
| 3549 | } |
| 3550 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3551 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3552 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3553 | if (ret) { |
| 3554 | mlog_errno(ret); |
| 3555 | goto out; |
| 3556 | } |
| 3557 | } |
| 3558 | } else { |
| 3559 | left_rec = &el->l_recs[index - 1]; |
| 3560 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3561 | has_empty_extent = 1; |
| 3562 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3563 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3564 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3565 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3566 | if (ret) { |
| 3567 | mlog_errno(ret); |
| 3568 | goto out; |
| 3569 | } |
| 3570 | |
| 3571 | if (has_empty_extent && index == 1) { |
| 3572 | /* |
| 3573 | * The easy case - we can just plop the record right in. |
| 3574 | */ |
| 3575 | *left_rec = *split_rec; |
| 3576 | |
| 3577 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3578 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3579 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3580 | |
| 3581 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3582 | le64_add_cpu(&right_rec->e_blkno, |
| 3583 | ocfs2_clusters_to_blocks(inode->i_sb, split_clusters)); |
| 3584 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3585 | |
| 3586 | ocfs2_cleanup_merge(el, index); |
| 3587 | |
| 3588 | ret = ocfs2_journal_dirty(handle, bh); |
| 3589 | if (ret) |
| 3590 | mlog_errno(ret); |
| 3591 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3592 | if (left_path) { |
| 3593 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 3594 | if (ret) |
| 3595 | mlog_errno(ret); |
| 3596 | |
| 3597 | /* |
| 3598 | * In the situation that the right_rec is empty and the extent |
| 3599 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3600 | * it and we need to delete the right extent block. |
| 3601 | */ |
| 3602 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3603 | le16_to_cpu(el->l_next_free_rec) == 1) { |
| 3604 | |
| 3605 | ret = ocfs2_remove_rightmost_path(inode, handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3606 | right_path, |
| 3607 | dealloc, et); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3608 | if (ret) { |
| 3609 | mlog_errno(ret); |
| 3610 | goto out; |
| 3611 | } |
| 3612 | |
| 3613 | /* Now the rightmost extent block has been deleted. |
| 3614 | * So we use the new rightmost path. |
| 3615 | */ |
| 3616 | ocfs2_mv_path(right_path, left_path); |
| 3617 | left_path = NULL; |
| 3618 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3619 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3620 | right_path, subtree_index); |
| 3621 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3622 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3623 | if (left_path) |
| 3624 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3625 | return ret; |
| 3626 | } |
| 3627 | |
| 3628 | static int ocfs2_try_to_merge_extent(struct inode *inode, |
| 3629 | handle_t *handle, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3630 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3631 | int split_index, |
| 3632 | struct ocfs2_extent_rec *split_rec, |
| 3633 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3634 | struct ocfs2_merge_ctxt *ctxt, |
| 3635 | struct ocfs2_extent_tree *et) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3636 | |
| 3637 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3638 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3639 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3640 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3641 | |
| 3642 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3643 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3644 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 3645 | /* |
| 3646 | * The merge code will need to create an empty |
| 3647 | * extent to take the place of the newly |
| 3648 | * emptied slot. Remove any pre-existing empty |
| 3649 | * extents - having more than one in a leaf is |
| 3650 | * illegal. |
| 3651 | */ |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3652 | ret = ocfs2_rotate_tree_left(inode, handle, path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3653 | dealloc, et); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3654 | if (ret) { |
| 3655 | mlog_errno(ret); |
| 3656 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3657 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3658 | split_index--; |
| 3659 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3663 | /* |
| 3664 | * Left-right contig implies this. |
| 3665 | */ |
| 3666 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3667 | |
| 3668 | /* |
| 3669 | * Since the leftright insert always covers the entire |
| 3670 | * extent, this call will delete the insert record |
| 3671 | * entirely, resulting in an empty extent record added to |
| 3672 | * the extent block. |
| 3673 | * |
| 3674 | * Since the adding of an empty extent shifts |
| 3675 | * everything back to the right, there's no need to |
| 3676 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3677 | * |
| 3678 | * When the split_index is zero, we need to merge it to the |
| 3679 | * prevoius extent block. It is more efficient and easier |
| 3680 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3681 | */ |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3682 | ret = ocfs2_merge_rec_right(inode, path, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3683 | handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3684 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3685 | if (ret) { |
| 3686 | mlog_errno(ret); |
| 3687 | goto out; |
| 3688 | } |
| 3689 | |
| 3690 | /* |
| 3691 | * We can only get this from logic error above. |
| 3692 | */ |
| 3693 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3694 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3695 | /* The merge left us with an empty extent, remove it. */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3696 | ret = ocfs2_rotate_tree_left(inode, handle, path, |
| 3697 | dealloc, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3698 | if (ret) { |
| 3699 | mlog_errno(ret); |
| 3700 | goto out; |
| 3701 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3702 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3703 | rec = &el->l_recs[split_index]; |
| 3704 | |
| 3705 | /* |
| 3706 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3707 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3708 | */ |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3709 | ret = ocfs2_merge_rec_left(inode, path, |
| 3710 | handle, rec, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3711 | dealloc, et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3712 | split_index); |
| 3713 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3714 | if (ret) { |
| 3715 | mlog_errno(ret); |
| 3716 | goto out; |
| 3717 | } |
| 3718 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3719 | ret = ocfs2_rotate_tree_left(inode, handle, path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3720 | dealloc, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3721 | /* |
| 3722 | * Error from this last rotate is not critical, so |
| 3723 | * print but don't bubble it up. |
| 3724 | */ |
| 3725 | if (ret) |
| 3726 | mlog_errno(ret); |
| 3727 | ret = 0; |
| 3728 | } else { |
| 3729 | /* |
| 3730 | * Merge a record to the left or right. |
| 3731 | * |
| 3732 | * 'contig_type' is relative to the existing record, |
| 3733 | * so for example, if we're "right contig", it's to |
| 3734 | * the record on the left (hence the left merge). |
| 3735 | */ |
| 3736 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
| 3737 | ret = ocfs2_merge_rec_left(inode, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3738 | path, |
| 3739 | handle, split_rec, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3740 | dealloc, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3741 | split_index); |
| 3742 | if (ret) { |
| 3743 | mlog_errno(ret); |
| 3744 | goto out; |
| 3745 | } |
| 3746 | } else { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3747 | ret = ocfs2_merge_rec_right(inode, path, handle, |
| 3748 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3749 | split_index); |
| 3750 | if (ret) { |
| 3751 | mlog_errno(ret); |
| 3752 | goto out; |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | if (ctxt->c_split_covers_rec) { |
| 3757 | /* |
| 3758 | * The merge may have left an empty extent in |
| 3759 | * our leaf. Try to rotate it away. |
| 3760 | */ |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3761 | ret = ocfs2_rotate_tree_left(inode, handle, path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3762 | dealloc, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3763 | if (ret) |
| 3764 | mlog_errno(ret); |
| 3765 | ret = 0; |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | out: |
| 3770 | return ret; |
| 3771 | } |
| 3772 | |
| 3773 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3774 | enum ocfs2_split_type split, |
| 3775 | struct ocfs2_extent_rec *rec, |
| 3776 | struct ocfs2_extent_rec *split_rec) |
| 3777 | { |
| 3778 | u64 len_blocks; |
| 3779 | |
| 3780 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3781 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3782 | |
| 3783 | if (split == SPLIT_LEFT) { |
| 3784 | /* |
| 3785 | * Region is on the left edge of the existing |
| 3786 | * record. |
| 3787 | */ |
| 3788 | le32_add_cpu(&rec->e_cpos, |
| 3789 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3790 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3791 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3792 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3793 | } else { |
| 3794 | /* |
| 3795 | * Region is on the right edge of the existing |
| 3796 | * record. |
| 3797 | */ |
| 3798 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3799 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3800 | } |
| 3801 | } |
| 3802 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3803 | /* |
| 3804 | * Do the final bits of extent record insertion at the target leaf |
| 3805 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3806 | * that the tree above has been prepared. |
| 3807 | */ |
| 3808 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec, |
| 3809 | struct ocfs2_extent_list *el, |
| 3810 | struct ocfs2_insert_type *insert, |
| 3811 | struct inode *inode) |
| 3812 | { |
| 3813 | int i = insert->ins_contig_index; |
| 3814 | unsigned int range; |
| 3815 | struct ocfs2_extent_rec *rec; |
| 3816 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3817 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3818 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3819 | if (insert->ins_split != SPLIT_NONE) { |
| 3820 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3821 | BUG_ON(i == -1); |
| 3822 | rec = &el->l_recs[i]; |
| 3823 | ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec, |
| 3824 | insert_rec); |
| 3825 | goto rotate; |
| 3826 | } |
| 3827 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3828 | /* |
| 3829 | * Contiguous insert - either left or right. |
| 3830 | */ |
| 3831 | if (insert->ins_contig != CONTIG_NONE) { |
| 3832 | rec = &el->l_recs[i]; |
| 3833 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3834 | rec->e_blkno = insert_rec->e_blkno; |
| 3835 | rec->e_cpos = insert_rec->e_cpos; |
| 3836 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3837 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3838 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3839 | return; |
| 3840 | } |
| 3841 | |
| 3842 | /* |
| 3843 | * Handle insert into an empty leaf. |
| 3844 | */ |
| 3845 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3846 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3847 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3848 | el->l_recs[0] = *insert_rec; |
| 3849 | el->l_next_free_rec = cpu_to_le16(1); |
| 3850 | return; |
| 3851 | } |
| 3852 | |
| 3853 | /* |
| 3854 | * Appending insert. |
| 3855 | */ |
| 3856 | if (insert->ins_appending == APPEND_TAIL) { |
| 3857 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3858 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3859 | range = le32_to_cpu(rec->e_cpos) |
| 3860 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3861 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3862 | |
| 3863 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3864 | le16_to_cpu(el->l_count), |
| 3865 | "inode %lu, depth %u, count %u, next free %u, " |
| 3866 | "rec.cpos %u, rec.clusters %u, " |
| 3867 | "insert.cpos %u, insert.clusters %u\n", |
| 3868 | inode->i_ino, |
| 3869 | le16_to_cpu(el->l_tree_depth), |
| 3870 | le16_to_cpu(el->l_count), |
| 3871 | le16_to_cpu(el->l_next_free_rec), |
| 3872 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3873 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3874 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3875 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3876 | i++; |
| 3877 | el->l_recs[i] = *insert_rec; |
| 3878 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3879 | return; |
| 3880 | } |
| 3881 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3882 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3883 | /* |
| 3884 | * Ok, we have to rotate. |
| 3885 | * |
| 3886 | * At this point, it is safe to assume that inserting into an |
| 3887 | * empty leaf and appending to a leaf have both been handled |
| 3888 | * above. |
| 3889 | * |
| 3890 | * This leaf needs to have space, either by the empty 1st |
| 3891 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3892 | */ |
| 3893 | ocfs2_rotate_leaf(el, insert_rec); |
| 3894 | } |
| 3895 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3896 | static void ocfs2_adjust_rightmost_records(struct inode *inode, |
| 3897 | handle_t *handle, |
| 3898 | struct ocfs2_path *path, |
| 3899 | struct ocfs2_extent_rec *insert_rec) |
| 3900 | { |
| 3901 | int ret, i, next_free; |
| 3902 | struct buffer_head *bh; |
| 3903 | struct ocfs2_extent_list *el; |
| 3904 | struct ocfs2_extent_rec *rec; |
| 3905 | |
| 3906 | /* |
| 3907 | * Update everything except the leaf block. |
| 3908 | */ |
| 3909 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3910 | bh = path->p_node[i].bh; |
| 3911 | el = path->p_node[i].el; |
| 3912 | |
| 3913 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3914 | if (next_free == 0) { |
| 3915 | ocfs2_error(inode->i_sb, |
| 3916 | "Dinode %llu has a bad extent list", |
| 3917 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 3918 | ret = -EIO; |
| 3919 | return; |
| 3920 | } |
| 3921 | |
| 3922 | rec = &el->l_recs[next_free - 1]; |
| 3923 | |
| 3924 | rec->e_int_clusters = insert_rec->e_cpos; |
| 3925 | le32_add_cpu(&rec->e_int_clusters, |
| 3926 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 3927 | le32_add_cpu(&rec->e_int_clusters, |
| 3928 | -le32_to_cpu(rec->e_cpos)); |
| 3929 | |
| 3930 | ret = ocfs2_journal_dirty(handle, bh); |
| 3931 | if (ret) |
| 3932 | mlog_errno(ret); |
| 3933 | |
| 3934 | } |
| 3935 | } |
| 3936 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3937 | static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle, |
| 3938 | struct ocfs2_extent_rec *insert_rec, |
| 3939 | struct ocfs2_path *right_path, |
| 3940 | struct ocfs2_path **ret_left_path) |
| 3941 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3942 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3943 | struct ocfs2_extent_list *el; |
| 3944 | struct ocfs2_path *left_path = NULL; |
| 3945 | |
| 3946 | *ret_left_path = NULL; |
| 3947 | |
| 3948 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3949 | * This shouldn't happen for non-trees. The extent rec cluster |
| 3950 | * count manipulation below only works for interior nodes. |
| 3951 | */ |
| 3952 | BUG_ON(right_path->p_tree_depth == 0); |
| 3953 | |
| 3954 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3955 | * If our appending insert is at the leftmost edge of a leaf, |
| 3956 | * then we might need to update the rightmost records of the |
| 3957 | * neighboring path. |
| 3958 | */ |
| 3959 | el = path_leaf_el(right_path); |
| 3960 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3961 | if (next_free == 0 || |
| 3962 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3963 | u32 left_cpos; |
| 3964 | |
| 3965 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, |
| 3966 | &left_cpos); |
| 3967 | if (ret) { |
| 3968 | mlog_errno(ret); |
| 3969 | goto out; |
| 3970 | } |
| 3971 | |
| 3972 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 3973 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 3974 | left_cpos); |
| 3975 | |
| 3976 | /* |
| 3977 | * No need to worry if the append is already in the |
| 3978 | * leftmost leaf. |
| 3979 | */ |
| 3980 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3981 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3982 | if (!left_path) { |
| 3983 | ret = -ENOMEM; |
| 3984 | mlog_errno(ret); |
| 3985 | goto out; |
| 3986 | } |
| 3987 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3988 | ret = ocfs2_find_path(INODE_CACHE(inode), left_path, |
| 3989 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3990 | if (ret) { |
| 3991 | mlog_errno(ret); |
| 3992 | goto out; |
| 3993 | } |
| 3994 | |
| 3995 | /* |
| 3996 | * ocfs2_insert_path() will pass the left_path to the |
| 3997 | * journal for us. |
| 3998 | */ |
| 3999 | } |
| 4000 | } |
| 4001 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4002 | ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4003 | if (ret) { |
| 4004 | mlog_errno(ret); |
| 4005 | goto out; |
| 4006 | } |
| 4007 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4008 | ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4009 | |
| 4010 | *ret_left_path = left_path; |
| 4011 | ret = 0; |
| 4012 | out: |
| 4013 | if (ret != 0) |
| 4014 | ocfs2_free_path(left_path); |
| 4015 | |
| 4016 | return ret; |
| 4017 | } |
| 4018 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4019 | static void ocfs2_split_record(struct inode *inode, |
| 4020 | struct ocfs2_path *left_path, |
| 4021 | struct ocfs2_path *right_path, |
| 4022 | struct ocfs2_extent_rec *split_rec, |
| 4023 | enum ocfs2_split_type split) |
| 4024 | { |
| 4025 | int index; |
| 4026 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4027 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4028 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4029 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4030 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4031 | if (left_path) |
| 4032 | left_el = path_leaf_el(left_path); |
| 4033 | |
| 4034 | el = right_el; |
| 4035 | insert_el = right_el; |
| 4036 | index = ocfs2_search_extent_list(el, cpos); |
| 4037 | if (index != -1) { |
| 4038 | if (index == 0 && left_path) { |
| 4039 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4040 | |
| 4041 | /* |
| 4042 | * This typically means that the record |
| 4043 | * started in the left path but moved to the |
| 4044 | * right as a result of rotation. We either |
| 4045 | * move the existing record to the left, or we |
| 4046 | * do the later insert there. |
| 4047 | * |
| 4048 | * In this case, the left path should always |
| 4049 | * exist as the rotate code will have passed |
| 4050 | * it back for a post-insert update. |
| 4051 | */ |
| 4052 | |
| 4053 | if (split == SPLIT_LEFT) { |
| 4054 | /* |
| 4055 | * It's a left split. Since we know |
| 4056 | * that the rotate code gave us an |
| 4057 | * empty extent in the left path, we |
| 4058 | * can just do the insert there. |
| 4059 | */ |
| 4060 | insert_el = left_el; |
| 4061 | } else { |
| 4062 | /* |
| 4063 | * Right split - we have to move the |
| 4064 | * existing record over to the left |
| 4065 | * leaf. The insert will be into the |
| 4066 | * newly created empty extent in the |
| 4067 | * right leaf. |
| 4068 | */ |
| 4069 | tmprec = &right_el->l_recs[index]; |
| 4070 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4071 | el = left_el; |
| 4072 | |
| 4073 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4074 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4075 | BUG_ON(index == -1); |
| 4076 | } |
| 4077 | } |
| 4078 | } else { |
| 4079 | BUG_ON(!left_path); |
| 4080 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4081 | /* |
| 4082 | * Left path is easy - we can just allow the insert to |
| 4083 | * happen. |
| 4084 | */ |
| 4085 | el = left_el; |
| 4086 | insert_el = left_el; |
| 4087 | index = ocfs2_search_extent_list(el, cpos); |
| 4088 | BUG_ON(index == -1); |
| 4089 | } |
| 4090 | |
| 4091 | rec = &el->l_recs[index]; |
| 4092 | ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec); |
| 4093 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4094 | } |
| 4095 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4096 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4097 | * This function only does inserts on an allocation b-tree. For tree |
| 4098 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4099 | * |
| 4100 | * right_path is the path we want to do the actual insert |
| 4101 | * in. left_path should only be passed in if we need to update that |
| 4102 | * portion of the tree after an edge insert. |
| 4103 | */ |
| 4104 | static int ocfs2_insert_path(struct inode *inode, |
| 4105 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4106 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4107 | struct ocfs2_path *left_path, |
| 4108 | struct ocfs2_path *right_path, |
| 4109 | struct ocfs2_extent_rec *insert_rec, |
| 4110 | struct ocfs2_insert_type *insert) |
| 4111 | { |
| 4112 | int ret, subtree_index; |
| 4113 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4114 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4115 | if (left_path) { |
| 4116 | int credits = handle->h_buffer_credits; |
| 4117 | |
| 4118 | /* |
| 4119 | * There's a chance that left_path got passed back to |
| 4120 | * us without being accounted for in the |
| 4121 | * journal. Extend our transaction here to be sure we |
| 4122 | * can change those blocks. |
| 4123 | */ |
| 4124 | credits += left_path->p_tree_depth; |
| 4125 | |
| 4126 | ret = ocfs2_extend_trans(handle, credits); |
| 4127 | if (ret < 0) { |
| 4128 | mlog_errno(ret); |
| 4129 | goto out; |
| 4130 | } |
| 4131 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4132 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4133 | if (ret < 0) { |
| 4134 | mlog_errno(ret); |
| 4135 | goto out; |
| 4136 | } |
| 4137 | } |
| 4138 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4139 | /* |
| 4140 | * Pass both paths to the journal. The majority of inserts |
| 4141 | * will be touching all components anyway. |
| 4142 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4143 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4144 | if (ret < 0) { |
| 4145 | mlog_errno(ret); |
| 4146 | goto out; |
| 4147 | } |
| 4148 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4149 | if (insert->ins_split != SPLIT_NONE) { |
| 4150 | /* |
| 4151 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4152 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4153 | * function sort it all out. |
| 4154 | */ |
| 4155 | ocfs2_split_record(inode, left_path, right_path, |
| 4156 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4157 | |
| 4158 | /* |
| 4159 | * Split might have modified either leaf and we don't |
| 4160 | * have a guarantee that the later edge insert will |
| 4161 | * dirty this for us. |
| 4162 | */ |
| 4163 | if (left_path) |
| 4164 | ret = ocfs2_journal_dirty(handle, |
| 4165 | path_leaf_bh(left_path)); |
| 4166 | if (ret) |
| 4167 | mlog_errno(ret); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4168 | } else |
| 4169 | ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path), |
| 4170 | insert, inode); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4171 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4172 | ret = ocfs2_journal_dirty(handle, leaf_bh); |
| 4173 | if (ret) |
| 4174 | mlog_errno(ret); |
| 4175 | |
| 4176 | if (left_path) { |
| 4177 | /* |
| 4178 | * The rotate code has indicated that we need to fix |
| 4179 | * up portions of the tree after the insert. |
| 4180 | * |
| 4181 | * XXX: Should we extend the transaction here? |
| 4182 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4183 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4184 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4185 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4186 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | ret = 0; |
| 4190 | out: |
| 4191 | return ret; |
| 4192 | } |
| 4193 | |
| 4194 | static int ocfs2_do_insert_extent(struct inode *inode, |
| 4195 | handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4196 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4197 | struct ocfs2_extent_rec *insert_rec, |
| 4198 | struct ocfs2_insert_type *type) |
| 4199 | { |
| 4200 | int ret, rotate = 0; |
| 4201 | u32 cpos; |
| 4202 | struct ocfs2_path *right_path = NULL; |
| 4203 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4204 | struct ocfs2_extent_list *el; |
| 4205 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4206 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4207 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4208 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4209 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4210 | if (ret) { |
| 4211 | mlog_errno(ret); |
| 4212 | goto out; |
| 4213 | } |
| 4214 | |
| 4215 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 4216 | ocfs2_insert_at_leaf(insert_rec, el, type, inode); |
| 4217 | goto out_update_clusters; |
| 4218 | } |
| 4219 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4220 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4221 | if (!right_path) { |
| 4222 | ret = -ENOMEM; |
| 4223 | mlog_errno(ret); |
| 4224 | goto out; |
| 4225 | } |
| 4226 | |
| 4227 | /* |
| 4228 | * Determine the path to start with. Rotations need the |
| 4229 | * rightmost path, everything else can go directly to the |
| 4230 | * target leaf. |
| 4231 | */ |
| 4232 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4233 | if (type->ins_appending == APPEND_NONE && |
| 4234 | type->ins_contig == CONTIG_NONE) { |
| 4235 | rotate = 1; |
| 4236 | cpos = UINT_MAX; |
| 4237 | } |
| 4238 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4239 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4240 | if (ret) { |
| 4241 | mlog_errno(ret); |
| 4242 | goto out; |
| 4243 | } |
| 4244 | |
| 4245 | /* |
| 4246 | * Rotations and appends need special treatment - they modify |
| 4247 | * parts of the tree's above them. |
| 4248 | * |
| 4249 | * Both might pass back a path immediate to the left of the |
| 4250 | * one being inserted to. This will be cause |
| 4251 | * ocfs2_insert_path() to modify the rightmost records of |
| 4252 | * left_path to account for an edge insert. |
| 4253 | * |
| 4254 | * XXX: When modifying this code, keep in mind that an insert |
| 4255 | * can wind up skipping both of these two special cases... |
| 4256 | */ |
| 4257 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4258 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4259 | le32_to_cpu(insert_rec->e_cpos), |
| 4260 | right_path, &left_path); |
| 4261 | if (ret) { |
| 4262 | mlog_errno(ret); |
| 4263 | goto out; |
| 4264 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4265 | |
| 4266 | /* |
| 4267 | * ocfs2_rotate_tree_right() might have extended the |
| 4268 | * transaction without re-journaling our tree root. |
| 4269 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4270 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4271 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4272 | if (ret) { |
| 4273 | mlog_errno(ret); |
| 4274 | goto out; |
| 4275 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4276 | } else if (type->ins_appending == APPEND_TAIL |
| 4277 | && type->ins_contig != CONTIG_LEFT) { |
| 4278 | ret = ocfs2_append_rec_to_path(inode, handle, insert_rec, |
| 4279 | right_path, &left_path); |
| 4280 | if (ret) { |
| 4281 | mlog_errno(ret); |
| 4282 | goto out; |
| 4283 | } |
| 4284 | } |
| 4285 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4286 | ret = ocfs2_insert_path(inode, handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4287 | insert_rec, type); |
| 4288 | if (ret) { |
| 4289 | mlog_errno(ret); |
| 4290 | goto out; |
| 4291 | } |
| 4292 | |
| 4293 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4294 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4295 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4296 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4297 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4298 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4299 | if (ret) |
| 4300 | mlog_errno(ret); |
| 4301 | |
| 4302 | out: |
| 4303 | ocfs2_free_path(left_path); |
| 4304 | ocfs2_free_path(right_path); |
| 4305 | |
| 4306 | return ret; |
| 4307 | } |
| 4308 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4309 | static enum ocfs2_contig_type |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4310 | ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4311 | struct ocfs2_extent_list *el, int index, |
| 4312 | struct ocfs2_extent_rec *split_rec) |
| 4313 | { |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4314 | int status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4315 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4316 | u32 left_cpos, right_cpos; |
| 4317 | struct ocfs2_extent_rec *rec = NULL; |
| 4318 | struct ocfs2_extent_list *new_el; |
| 4319 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4320 | struct buffer_head *bh; |
| 4321 | struct ocfs2_extent_block *eb; |
| 4322 | |
| 4323 | if (index > 0) { |
| 4324 | rec = &el->l_recs[index - 1]; |
| 4325 | } else if (path->p_tree_depth > 0) { |
| 4326 | status = ocfs2_find_cpos_for_left_leaf(inode->i_sb, |
| 4327 | path, &left_cpos); |
| 4328 | if (status) |
| 4329 | goto out; |
| 4330 | |
| 4331 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4332 | left_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4333 | if (!left_path) |
| 4334 | goto out; |
| 4335 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4336 | status = ocfs2_find_path(INODE_CACHE(inode), |
| 4337 | left_path, left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4338 | if (status) |
| 4339 | goto out; |
| 4340 | |
| 4341 | new_el = path_leaf_el(left_path); |
| 4342 | |
| 4343 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4344 | le16_to_cpu(new_el->l_count)) { |
| 4345 | bh = path_leaf_bh(left_path); |
| 4346 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4347 | ocfs2_error(inode->i_sb, |
| 4348 | "Extent block #%llu has an " |
| 4349 | "invalid l_next_free_rec of " |
| 4350 | "%d. It should have " |
| 4351 | "matched the l_count of %d", |
| 4352 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4353 | le16_to_cpu(new_el->l_next_free_rec), |
| 4354 | le16_to_cpu(new_el->l_count)); |
| 4355 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4356 | goto out; |
| 4357 | } |
| 4358 | rec = &new_el->l_recs[ |
| 4359 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4360 | } |
| 4361 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4362 | |
| 4363 | /* |
| 4364 | * We're careful to check for an empty extent record here - |
| 4365 | * the merge code will know what to do if it sees one. |
| 4366 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4367 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4368 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4369 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4370 | ret = CONTIG_RIGHT; |
| 4371 | } else { |
| 4372 | ret = ocfs2_extent_contig(inode, rec, split_rec); |
| 4373 | } |
| 4374 | } |
| 4375 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4376 | rec = NULL; |
| 4377 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4378 | rec = &el->l_recs[index + 1]; |
| 4379 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4380 | path->p_tree_depth > 0) { |
| 4381 | status = ocfs2_find_cpos_for_right_leaf(inode->i_sb, |
| 4382 | path, &right_cpos); |
| 4383 | if (status) |
| 4384 | goto out; |
| 4385 | |
| 4386 | if (right_cpos == 0) |
| 4387 | goto out; |
| 4388 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4389 | right_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4390 | if (!right_path) |
| 4391 | goto out; |
| 4392 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4393 | status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4394 | if (status) |
| 4395 | goto out; |
| 4396 | |
| 4397 | new_el = path_leaf_el(right_path); |
| 4398 | rec = &new_el->l_recs[0]; |
| 4399 | if (ocfs2_is_empty_extent(rec)) { |
| 4400 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4401 | bh = path_leaf_bh(right_path); |
| 4402 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4403 | ocfs2_error(inode->i_sb, |
| 4404 | "Extent block #%llu has an " |
| 4405 | "invalid l_next_free_rec of %d", |
| 4406 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4407 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4408 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4409 | goto out; |
| 4410 | } |
| 4411 | rec = &new_el->l_recs[1]; |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4416 | enum ocfs2_contig_type contig_type; |
| 4417 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4418 | contig_type = ocfs2_extent_contig(inode, rec, split_rec); |
| 4419 | |
| 4420 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4421 | ret = CONTIG_LEFTRIGHT; |
| 4422 | else if (ret == CONTIG_NONE) |
| 4423 | ret = contig_type; |
| 4424 | } |
| 4425 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4426 | out: |
| 4427 | if (left_path) |
| 4428 | ocfs2_free_path(left_path); |
| 4429 | if (right_path) |
| 4430 | ocfs2_free_path(right_path); |
| 4431 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4432 | return ret; |
| 4433 | } |
| 4434 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4435 | static void ocfs2_figure_contig_type(struct inode *inode, |
| 4436 | struct ocfs2_insert_type *insert, |
| 4437 | struct ocfs2_extent_list *el, |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4438 | struct ocfs2_extent_rec *insert_rec, |
| 4439 | struct ocfs2_extent_tree *et) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4440 | { |
| 4441 | int i; |
| 4442 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4443 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4444 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4445 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4446 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 4447 | contig_type = ocfs2_extent_contig(inode, &el->l_recs[i], |
| 4448 | insert_rec); |
| 4449 | if (contig_type != CONTIG_NONE) { |
| 4450 | insert->ins_contig_index = i; |
| 4451 | break; |
| 4452 | } |
| 4453 | } |
| 4454 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4455 | |
| 4456 | if (insert->ins_contig != CONTIG_NONE) { |
| 4457 | struct ocfs2_extent_rec *rec = |
| 4458 | &el->l_recs[insert->ins_contig_index]; |
| 4459 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4460 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4461 | |
| 4462 | /* |
| 4463 | * Caller might want us to limit the size of extents, don't |
| 4464 | * calculate contiguousness if we might exceed that limit. |
| 4465 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4466 | if (et->et_max_leaf_clusters && |
| 4467 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4468 | insert->ins_contig = CONTIG_NONE; |
| 4469 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | /* |
| 4473 | * This should only be called against the righmost leaf extent list. |
| 4474 | * |
| 4475 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4476 | * insert at the tail of the rightmost leaf. |
| 4477 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4478 | * This should also work against the root extent list for tree's with 0 |
| 4479 | * depth. If we consider the root extent list to be the rightmost leaf node |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4480 | * then the logic here makes sense. |
| 4481 | */ |
| 4482 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4483 | struct ocfs2_extent_list *el, |
| 4484 | struct ocfs2_extent_rec *insert_rec) |
| 4485 | { |
| 4486 | int i; |
| 4487 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4488 | struct ocfs2_extent_rec *rec; |
| 4489 | |
| 4490 | insert->ins_appending = APPEND_NONE; |
| 4491 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4492 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4493 | |
| 4494 | if (!el->l_next_free_rec) |
| 4495 | goto set_tail_append; |
| 4496 | |
| 4497 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4498 | /* Were all records empty? */ |
| 4499 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4500 | goto set_tail_append; |
| 4501 | } |
| 4502 | |
| 4503 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4504 | rec = &el->l_recs[i]; |
| 4505 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4506 | if (cpos >= |
| 4507 | (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters))) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4508 | goto set_tail_append; |
| 4509 | |
| 4510 | return; |
| 4511 | |
| 4512 | set_tail_append: |
| 4513 | insert->ins_appending = APPEND_TAIL; |
| 4514 | } |
| 4515 | |
| 4516 | /* |
| 4517 | * Helper function called at the begining of an insert. |
| 4518 | * |
| 4519 | * This computes a few things that are commonly used in the process of |
| 4520 | * inserting into the btree: |
| 4521 | * - Whether the new extent is contiguous with an existing one. |
| 4522 | * - The current tree depth. |
| 4523 | * - Whether the insert is an appending one. |
| 4524 | * - The total # of free records in the tree. |
| 4525 | * |
| 4526 | * All of the information is stored on the ocfs2_insert_type |
| 4527 | * structure. |
| 4528 | */ |
| 4529 | static int ocfs2_figure_insert_type(struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4530 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4531 | struct buffer_head **last_eb_bh, |
| 4532 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4533 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4534 | struct ocfs2_insert_type *insert) |
| 4535 | { |
| 4536 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4537 | struct ocfs2_extent_block *eb; |
| 4538 | struct ocfs2_extent_list *el; |
| 4539 | struct ocfs2_path *path = NULL; |
| 4540 | struct buffer_head *bh = NULL; |
| 4541 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4542 | insert->ins_split = SPLIT_NONE; |
| 4543 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4544 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4545 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4546 | |
| 4547 | if (el->l_tree_depth) { |
| 4548 | /* |
| 4549 | * If we have tree depth, we read in the |
| 4550 | * rightmost extent block ahead of time as |
| 4551 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4552 | * may want it later. |
| 4553 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4554 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4555 | ocfs2_et_get_last_eb_blk(et), |
| 4556 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4557 | if (ret) { |
| 4558 | mlog_exit(ret); |
| 4559 | goto out; |
| 4560 | } |
| 4561 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4562 | el = &eb->h_list; |
| 4563 | } |
| 4564 | |
| 4565 | /* |
| 4566 | * Unless we have a contiguous insert, we'll need to know if |
| 4567 | * there is room left in our allocation tree for another |
| 4568 | * extent record. |
| 4569 | * |
| 4570 | * XXX: This test is simplistic, we can search for empty |
| 4571 | * extent records too. |
| 4572 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4573 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4574 | le16_to_cpu(el->l_next_free_rec); |
| 4575 | |
| 4576 | if (!insert->ins_tree_depth) { |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4577 | ocfs2_figure_contig_type(inode, insert, el, insert_rec, et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4578 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4579 | return 0; |
| 4580 | } |
| 4581 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4582 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4583 | if (!path) { |
| 4584 | ret = -ENOMEM; |
| 4585 | mlog_errno(ret); |
| 4586 | goto out; |
| 4587 | } |
| 4588 | |
| 4589 | /* |
| 4590 | * In the case that we're inserting past what the tree |
| 4591 | * currently accounts for, ocfs2_find_path() will return for |
| 4592 | * us the rightmost tree path. This is accounted for below in |
| 4593 | * the appending code. |
| 4594 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4595 | ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4596 | if (ret) { |
| 4597 | mlog_errno(ret); |
| 4598 | goto out; |
| 4599 | } |
| 4600 | |
| 4601 | el = path_leaf_el(path); |
| 4602 | |
| 4603 | /* |
| 4604 | * Now that we have the path, there's two things we want to determine: |
| 4605 | * 1) Contiguousness (also set contig_index if this is so) |
| 4606 | * |
| 4607 | * 2) Are we doing an append? We can trivially break this up |
| 4608 | * into two types of appends: simple record append, or a |
| 4609 | * rotate inside the tail leaf. |
| 4610 | */ |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4611 | ocfs2_figure_contig_type(inode, insert, el, insert_rec, et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4612 | |
| 4613 | /* |
| 4614 | * The insert code isn't quite ready to deal with all cases of |
| 4615 | * left contiguousness. Specifically, if it's an insert into |
| 4616 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4617 | * cluster count on the last record of the path directly to it's |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4618 | * left. For now, just catch that case and fool the layers |
| 4619 | * above us. This works just fine for tree_depth == 0, which |
| 4620 | * is why we allow that above. |
| 4621 | */ |
| 4622 | if (insert->ins_contig == CONTIG_LEFT && |
| 4623 | insert->ins_contig_index == 0) |
| 4624 | insert->ins_contig = CONTIG_NONE; |
| 4625 | |
| 4626 | /* |
| 4627 | * Ok, so we can simply compare against last_eb to figure out |
| 4628 | * whether the path doesn't exist. This will only happen in |
| 4629 | * the case that we're doing a tail append, so maybe we can |
| 4630 | * take advantage of that information somehow. |
| 4631 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4632 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4633 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4634 | /* |
| 4635 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4636 | * tree path. This might be an appending insert. There are |
| 4637 | * two cases: |
| 4638 | * 1) We're doing a true append at the tail: |
| 4639 | * -This might even be off the end of the leaf |
| 4640 | * 2) We're "appending" by rotating in the tail |
| 4641 | */ |
| 4642 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4643 | } |
| 4644 | |
| 4645 | out: |
| 4646 | ocfs2_free_path(path); |
| 4647 | |
| 4648 | if (ret == 0) |
| 4649 | *last_eb_bh = bh; |
| 4650 | else |
| 4651 | brelse(bh); |
| 4652 | return ret; |
| 4653 | } |
| 4654 | |
| 4655 | /* |
| 4656 | * Insert an extent into an inode btree. |
| 4657 | * |
| 4658 | * The caller needs to update fe->i_clusters |
| 4659 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4660 | int ocfs2_insert_extent(struct ocfs2_super *osb, |
| 4661 | handle_t *handle, |
| 4662 | struct inode *inode, |
| 4663 | struct ocfs2_extent_tree *et, |
| 4664 | u32 cpos, |
| 4665 | u64 start_blk, |
| 4666 | u32 new_clusters, |
| 4667 | u8 flags, |
| 4668 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4669 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4670 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4671 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4672 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4673 | struct ocfs2_insert_type insert = {0, }; |
| 4674 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4675 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4676 | mlog(0, "add %u clusters at position %u to inode %llu\n", |
| 4677 | new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4678 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4679 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4680 | rec.e_cpos = cpu_to_le32(cpos); |
| 4681 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4682 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4683 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4684 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4685 | if (status) { |
| 4686 | mlog_errno(status); |
| 4687 | goto bail; |
| 4688 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4689 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4690 | status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4691 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4692 | if (status < 0) { |
| 4693 | mlog_errno(status); |
| 4694 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4695 | } |
| 4696 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4697 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 4698 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 4699 | "Insert.tree_depth: %d\n", |
| 4700 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4701 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4702 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4703 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4704 | status = ocfs2_grow_tree(inode, handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4705 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4706 | meta_ac); |
| 4707 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4708 | mlog_errno(status); |
| 4709 | goto bail; |
| 4710 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4713 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4714 | status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4715 | if (status < 0) |
| 4716 | mlog_errno(status); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4717 | else if (et->et_ops == &ocfs2_dinode_et_ops) |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 4718 | ocfs2_extent_map_insert_rec(inode, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4719 | |
| 4720 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4721 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4722 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4723 | mlog_exit(status); |
| 4724 | return status; |
| 4725 | } |
| 4726 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4727 | /* |
| 4728 | * Allcate and add clusters into the extent b-tree. |
| 4729 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4730 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4731 | * it is not limited to the file storage. Any extent tree can use this |
| 4732 | * function if it implements the proper ocfs2_extent_tree. |
| 4733 | */ |
| 4734 | int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, |
| 4735 | struct inode *inode, |
| 4736 | u32 *logical_offset, |
| 4737 | u32 clusters_to_add, |
| 4738 | int mark_unwritten, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4739 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4740 | handle_t *handle, |
| 4741 | struct ocfs2_alloc_context *data_ac, |
| 4742 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4743 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4744 | { |
| 4745 | int status = 0; |
| 4746 | int free_extents; |
| 4747 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4748 | u32 bit_off, num_bits; |
| 4749 | u64 block; |
| 4750 | u8 flags = 0; |
| 4751 | |
| 4752 | BUG_ON(!clusters_to_add); |
| 4753 | |
| 4754 | if (mark_unwritten) |
| 4755 | flags = OCFS2_EXT_UNWRITTEN; |
| 4756 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4757 | free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4758 | if (free_extents < 0) { |
| 4759 | status = free_extents; |
| 4760 | mlog_errno(status); |
| 4761 | goto leave; |
| 4762 | } |
| 4763 | |
| 4764 | /* there are two cases which could cause us to EAGAIN in the |
| 4765 | * we-need-more-metadata case: |
| 4766 | * 1) we haven't reserved *any* |
| 4767 | * 2) we are so fragmented, we've needed to add metadata too |
| 4768 | * many times. */ |
| 4769 | if (!free_extents && !meta_ac) { |
| 4770 | mlog(0, "we haven't reserved any metadata!\n"); |
| 4771 | status = -EAGAIN; |
| 4772 | reason = RESTART_META; |
| 4773 | goto leave; |
| 4774 | } else if ((!free_extents) |
| 4775 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4776 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4777 | mlog(0, "filesystem is really fragmented...\n"); |
| 4778 | status = -EAGAIN; |
| 4779 | reason = RESTART_META; |
| 4780 | goto leave; |
| 4781 | } |
| 4782 | |
| 4783 | status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 4784 | clusters_to_add, &bit_off, &num_bits); |
| 4785 | if (status < 0) { |
| 4786 | if (status != -ENOSPC) |
| 4787 | mlog_errno(status); |
| 4788 | goto leave; |
| 4789 | } |
| 4790 | |
| 4791 | BUG_ON(num_bits > clusters_to_add); |
| 4792 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4793 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4794 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4795 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4796 | if (status < 0) { |
| 4797 | mlog_errno(status); |
| 4798 | goto leave; |
| 4799 | } |
| 4800 | |
| 4801 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
| 4802 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
| 4803 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4804 | status = ocfs2_insert_extent(osb, handle, inode, et, |
| 4805 | *logical_offset, block, |
| 4806 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4807 | if (status < 0) { |
| 4808 | mlog_errno(status); |
| 4809 | goto leave; |
| 4810 | } |
| 4811 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4812 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4813 | if (status < 0) { |
| 4814 | mlog_errno(status); |
| 4815 | goto leave; |
| 4816 | } |
| 4817 | |
| 4818 | clusters_to_add -= num_bits; |
| 4819 | *logical_offset += num_bits; |
| 4820 | |
| 4821 | if (clusters_to_add) { |
| 4822 | mlog(0, "need to alloc once more, wanted = %u\n", |
| 4823 | clusters_to_add); |
| 4824 | status = -EAGAIN; |
| 4825 | reason = RESTART_TRANS; |
| 4826 | } |
| 4827 | |
| 4828 | leave: |
| 4829 | mlog_exit(status); |
| 4830 | if (reason_ret) |
| 4831 | *reason_ret = reason; |
| 4832 | return status; |
| 4833 | } |
| 4834 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4835 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4836 | struct ocfs2_extent_rec *split_rec, |
| 4837 | u32 cpos, |
| 4838 | struct ocfs2_extent_rec *rec) |
| 4839 | { |
| 4840 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4841 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4842 | |
| 4843 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4844 | |
| 4845 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4846 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4847 | |
| 4848 | split_rec->e_blkno = rec->e_blkno; |
| 4849 | le64_add_cpu(&split_rec->e_blkno, |
| 4850 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4851 | |
| 4852 | split_rec->e_flags = rec->e_flags; |
| 4853 | } |
| 4854 | |
| 4855 | static int ocfs2_split_and_insert(struct inode *inode, |
| 4856 | handle_t *handle, |
| 4857 | struct ocfs2_path *path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4858 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4859 | struct buffer_head **last_eb_bh, |
| 4860 | int split_index, |
| 4861 | struct ocfs2_extent_rec *orig_split_rec, |
| 4862 | struct ocfs2_alloc_context *meta_ac) |
| 4863 | { |
| 4864 | int ret = 0, depth; |
| 4865 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4866 | struct ocfs2_extent_rec tmprec; |
| 4867 | struct ocfs2_extent_list *rightmost_el; |
| 4868 | struct ocfs2_extent_rec rec; |
| 4869 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4870 | struct ocfs2_insert_type insert; |
| 4871 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4872 | |
| 4873 | leftright: |
| 4874 | /* |
| 4875 | * Store a copy of the record on the stack - it might move |
| 4876 | * around as the tree is manipulated below. |
| 4877 | */ |
| 4878 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4879 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4880 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4881 | |
| 4882 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4883 | if (depth) { |
| 4884 | BUG_ON(!(*last_eb_bh)); |
| 4885 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4886 | rightmost_el = &eb->h_list; |
| 4887 | } |
| 4888 | |
| 4889 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4890 | le16_to_cpu(rightmost_el->l_count)) { |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4891 | ret = ocfs2_grow_tree(inode, handle, et, |
| 4892 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4893 | if (ret) { |
| 4894 | mlog_errno(ret); |
| 4895 | goto out; |
| 4896 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4900 | insert.ins_appending = APPEND_NONE; |
| 4901 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4902 | insert.ins_tree_depth = depth; |
| 4903 | |
| 4904 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 4905 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 4906 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 4907 | le16_to_cpu(rec.e_leaf_clusters); |
| 4908 | |
| 4909 | if (split_rec.e_cpos == rec.e_cpos) { |
| 4910 | insert.ins_split = SPLIT_LEFT; |
| 4911 | } else if (insert_range == rec_range) { |
| 4912 | insert.ins_split = SPLIT_RIGHT; |
| 4913 | } else { |
| 4914 | /* |
| 4915 | * Left/right split. We fake this as a right split |
| 4916 | * first and then make a second pass as a left split. |
| 4917 | */ |
| 4918 | insert.ins_split = SPLIT_RIGHT; |
| 4919 | |
| 4920 | ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range, |
| 4921 | &rec); |
| 4922 | |
| 4923 | split_rec = tmprec; |
| 4924 | |
| 4925 | BUG_ON(do_leftright); |
| 4926 | do_leftright = 1; |
| 4927 | } |
| 4928 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4929 | ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4930 | if (ret) { |
| 4931 | mlog_errno(ret); |
| 4932 | goto out; |
| 4933 | } |
| 4934 | |
| 4935 | if (do_leftright == 1) { |
| 4936 | u32 cpos; |
| 4937 | struct ocfs2_extent_list *el; |
| 4938 | |
| 4939 | do_leftright++; |
| 4940 | split_rec = *orig_split_rec; |
| 4941 | |
| 4942 | ocfs2_reinit_path(path, 1); |
| 4943 | |
| 4944 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4945 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4946 | if (ret) { |
| 4947 | mlog_errno(ret); |
| 4948 | goto out; |
| 4949 | } |
| 4950 | |
| 4951 | el = path_leaf_el(path); |
| 4952 | split_index = ocfs2_search_extent_list(el, cpos); |
| 4953 | goto leftright; |
| 4954 | } |
| 4955 | out: |
| 4956 | |
| 4957 | return ret; |
| 4958 | } |
| 4959 | |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4960 | static int ocfs2_replace_extent_rec(struct inode *inode, |
| 4961 | handle_t *handle, |
| 4962 | struct ocfs2_path *path, |
| 4963 | struct ocfs2_extent_list *el, |
| 4964 | int split_index, |
| 4965 | struct ocfs2_extent_rec *split_rec) |
| 4966 | { |
| 4967 | int ret; |
| 4968 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4969 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4970 | path_num_items(path) - 1); |
| 4971 | if (ret) { |
| 4972 | mlog_errno(ret); |
| 4973 | goto out; |
| 4974 | } |
| 4975 | |
| 4976 | el->l_recs[split_index] = *split_rec; |
| 4977 | |
| 4978 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 4979 | out: |
| 4980 | return ret; |
| 4981 | } |
| 4982 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4983 | /* |
| 4984 | * Mark part or all of the extent record at split_index in the leaf |
| 4985 | * pointed to by path as written. This removes the unwritten |
| 4986 | * extent flag. |
| 4987 | * |
| 4988 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 4989 | * |
| 4990 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 4991 | * of the tree is required. All other cases will degrade into a less |
| 4992 | * optimal tree layout. |
| 4993 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4994 | * last_eb_bh should be the rightmost leaf block for any extent |
| 4995 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 4996 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4997 | * |
| 4998 | * This code is optimized for readability - several passes might be |
| 4999 | * made over certain portions of the tree. All of those blocks will |
| 5000 | * have been brought into cache (and pinned via the journal), so the |
| 5001 | * extra overhead is not expressed in terms of disk reads. |
| 5002 | */ |
| 5003 | static int __ocfs2_mark_extent_written(struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5004 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5005 | handle_t *handle, |
| 5006 | struct ocfs2_path *path, |
| 5007 | int split_index, |
| 5008 | struct ocfs2_extent_rec *split_rec, |
| 5009 | struct ocfs2_alloc_context *meta_ac, |
| 5010 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5011 | { |
| 5012 | int ret = 0; |
| 5013 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 5014 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5015 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 5016 | struct ocfs2_merge_ctxt ctxt; |
| 5017 | struct ocfs2_extent_list *rightmost_el; |
| 5018 | |
Roel Kluin | 3cf0c50 | 2007-10-27 00:20:36 +0200 | [diff] [blame] | 5019 | if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5020 | ret = -EIO; |
| 5021 | mlog_errno(ret); |
| 5022 | goto out; |
| 5023 | } |
| 5024 | |
| 5025 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5026 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5027 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5028 | ret = -EIO; |
| 5029 | mlog_errno(ret); |
| 5030 | goto out; |
| 5031 | } |
| 5032 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 5033 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5034 | split_index, |
| 5035 | split_rec); |
| 5036 | |
| 5037 | /* |
| 5038 | * The core merge / split code wants to know how much room is |
| 5039 | * left in this inodes allocation tree, so we pass the |
| 5040 | * rightmost extent list. |
| 5041 | */ |
| 5042 | if (path->p_tree_depth) { |
| 5043 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5044 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5045 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5046 | ocfs2_et_get_last_eb_blk(et), |
| 5047 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5048 | if (ret) { |
| 5049 | mlog_exit(ret); |
| 5050 | goto out; |
| 5051 | } |
| 5052 | |
| 5053 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5054 | rightmost_el = &eb->h_list; |
| 5055 | } else |
| 5056 | rightmost_el = path_root_el(path); |
| 5057 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5058 | if (rec->e_cpos == split_rec->e_cpos && |
| 5059 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5060 | ctxt.c_split_covers_rec = 1; |
| 5061 | else |
| 5062 | ctxt.c_split_covers_rec = 0; |
| 5063 | |
| 5064 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5065 | |
Mark Fasheh | 015452b | 2007-09-12 10:21:22 -0700 | [diff] [blame] | 5066 | mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", |
| 5067 | split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, |
| 5068 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5069 | |
| 5070 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5071 | if (ctxt.c_split_covers_rec) |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5072 | ret = ocfs2_replace_extent_rec(inode, handle, |
| 5073 | path, el, |
| 5074 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5075 | else |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5076 | ret = ocfs2_split_and_insert(inode, handle, path, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5077 | &last_eb_bh, split_index, |
| 5078 | split_rec, meta_ac); |
| 5079 | if (ret) |
| 5080 | mlog_errno(ret); |
| 5081 | } else { |
| 5082 | ret = ocfs2_try_to_merge_extent(inode, handle, path, |
| 5083 | split_index, split_rec, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5084 | dealloc, &ctxt, et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5085 | if (ret) |
| 5086 | mlog_errno(ret); |
| 5087 | } |
| 5088 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5089 | out: |
| 5090 | brelse(last_eb_bh); |
| 5091 | return ret; |
| 5092 | } |
| 5093 | |
| 5094 | /* |
| 5095 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5096 | * |
| 5097 | * If the existing extent is larger than the request, initiate a |
| 5098 | * split. An attempt will be made at merging with adjacent extents. |
| 5099 | * |
| 5100 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5101 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5102 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5103 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5104 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5105 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5106 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5107 | { |
| 5108 | int ret, index; |
| 5109 | u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); |
| 5110 | struct ocfs2_extent_rec split_rec; |
| 5111 | struct ocfs2_path *left_path = NULL; |
| 5112 | struct ocfs2_extent_list *el; |
| 5113 | |
| 5114 | mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", |
| 5115 | inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); |
| 5116 | |
| 5117 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 5118 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 5119 | "that are being written to, but the feature bit " |
| 5120 | "is not set in the super block.", |
| 5121 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5122 | ret = -EROFS; |
| 5123 | goto out; |
| 5124 | } |
| 5125 | |
| 5126 | /* |
| 5127 | * XXX: This should be fixed up so that we just re-insert the |
| 5128 | * next extent records. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5129 | * |
| 5130 | * XXX: This is a hack on the extent tree, maybe it should be |
| 5131 | * an op? |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5132 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5133 | if (et->et_ops == &ocfs2_dinode_et_ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5134 | ocfs2_extent_map_trunc(inode, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5135 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5136 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5137 | if (!left_path) { |
| 5138 | ret = -ENOMEM; |
| 5139 | mlog_errno(ret); |
| 5140 | goto out; |
| 5141 | } |
| 5142 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5143 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5144 | if (ret) { |
| 5145 | mlog_errno(ret); |
| 5146 | goto out; |
| 5147 | } |
| 5148 | el = path_leaf_el(left_path); |
| 5149 | |
| 5150 | index = ocfs2_search_extent_list(el, cpos); |
| 5151 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5152 | ocfs2_error(inode->i_sb, |
| 5153 | "Inode %llu has an extent at cpos %u which can no " |
| 5154 | "longer be found.\n", |
| 5155 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 5156 | ret = -EROFS; |
| 5157 | goto out; |
| 5158 | } |
| 5159 | |
| 5160 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5161 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5162 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5163 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
| 5164 | split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; |
| 5165 | split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; |
| 5166 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5167 | ret = __ocfs2_mark_extent_written(inode, et, handle, left_path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5168 | index, &split_rec, meta_ac, |
| 5169 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5170 | if (ret) |
| 5171 | mlog_errno(ret); |
| 5172 | |
| 5173 | out: |
| 5174 | ocfs2_free_path(left_path); |
| 5175 | return ret; |
| 5176 | } |
| 5177 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5178 | static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5179 | handle_t *handle, struct ocfs2_path *path, |
| 5180 | int index, u32 new_range, |
| 5181 | struct ocfs2_alloc_context *meta_ac) |
| 5182 | { |
| 5183 | int ret, depth, credits = handle->h_buffer_credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5184 | struct buffer_head *last_eb_bh = NULL; |
| 5185 | struct ocfs2_extent_block *eb; |
| 5186 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5187 | struct ocfs2_extent_rec split_rec; |
| 5188 | struct ocfs2_extent_rec *rec; |
| 5189 | struct ocfs2_insert_type insert; |
| 5190 | |
| 5191 | /* |
| 5192 | * Setup the record to split before we grow the tree. |
| 5193 | */ |
| 5194 | el = path_leaf_el(path); |
| 5195 | rec = &el->l_recs[index]; |
| 5196 | ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec); |
| 5197 | |
| 5198 | depth = path->p_tree_depth; |
| 5199 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5200 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5201 | ocfs2_et_get_last_eb_blk(et), |
| 5202 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5203 | if (ret < 0) { |
| 5204 | mlog_errno(ret); |
| 5205 | goto out; |
| 5206 | } |
| 5207 | |
| 5208 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5209 | rightmost_el = &eb->h_list; |
| 5210 | } else |
| 5211 | rightmost_el = path_leaf_el(path); |
| 5212 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 5213 | credits += path->p_tree_depth + |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 5214 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5215 | ret = ocfs2_extend_trans(handle, credits); |
| 5216 | if (ret) { |
| 5217 | mlog_errno(ret); |
| 5218 | goto out; |
| 5219 | } |
| 5220 | |
| 5221 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5222 | le16_to_cpu(rightmost_el->l_count)) { |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5223 | ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5224 | meta_ac); |
| 5225 | if (ret) { |
| 5226 | mlog_errno(ret); |
| 5227 | goto out; |
| 5228 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5232 | insert.ins_appending = APPEND_NONE; |
| 5233 | insert.ins_contig = CONTIG_NONE; |
| 5234 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5235 | insert.ins_tree_depth = depth; |
| 5236 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5237 | ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5238 | if (ret) |
| 5239 | mlog_errno(ret); |
| 5240 | |
| 5241 | out: |
| 5242 | brelse(last_eb_bh); |
| 5243 | return ret; |
| 5244 | } |
| 5245 | |
| 5246 | static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, |
| 5247 | struct ocfs2_path *path, int index, |
| 5248 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5249 | u32 cpos, u32 len, |
| 5250 | struct ocfs2_extent_tree *et) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5251 | { |
| 5252 | int ret; |
| 5253 | u32 left_cpos, rec_range, trunc_range; |
| 5254 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
| 5255 | struct super_block *sb = inode->i_sb; |
| 5256 | struct ocfs2_path *left_path = NULL; |
| 5257 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5258 | struct ocfs2_extent_rec *rec; |
| 5259 | struct ocfs2_extent_block *eb; |
| 5260 | |
| 5261 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5262 | ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5263 | if (ret) { |
| 5264 | mlog_errno(ret); |
| 5265 | goto out; |
| 5266 | } |
| 5267 | |
| 5268 | index--; |
| 5269 | } |
| 5270 | |
| 5271 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5272 | path->p_tree_depth) { |
| 5273 | /* |
| 5274 | * Check whether this is the rightmost tree record. If |
| 5275 | * we remove all of this record or part of its right |
| 5276 | * edge then an update of the record lengths above it |
| 5277 | * will be required. |
| 5278 | */ |
| 5279 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5280 | if (eb->h_next_leaf_blk == 0) |
| 5281 | is_rightmost_tree_rec = 1; |
| 5282 | } |
| 5283 | |
| 5284 | rec = &el->l_recs[index]; |
| 5285 | if (index == 0 && path->p_tree_depth && |
| 5286 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5287 | /* |
| 5288 | * Changing the leftmost offset (via partial or whole |
| 5289 | * record truncate) of an interior (or rightmost) path |
| 5290 | * means we have to update the subtree that is formed |
| 5291 | * by this leaf and the one to it's left. |
| 5292 | * |
| 5293 | * There are two cases we can skip: |
| 5294 | * 1) Path is the leftmost one in our inode tree. |
| 5295 | * 2) The leaf is rightmost and will be empty after |
| 5296 | * we remove the extent record - the rotate code |
| 5297 | * knows how to update the newly formed edge. |
| 5298 | */ |
| 5299 | |
| 5300 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, |
| 5301 | &left_cpos); |
| 5302 | if (ret) { |
| 5303 | mlog_errno(ret); |
| 5304 | goto out; |
| 5305 | } |
| 5306 | |
| 5307 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5308 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5309 | if (!left_path) { |
| 5310 | ret = -ENOMEM; |
| 5311 | mlog_errno(ret); |
| 5312 | goto out; |
| 5313 | } |
| 5314 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5315 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5316 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5317 | if (ret) { |
| 5318 | mlog_errno(ret); |
| 5319 | goto out; |
| 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | |
| 5324 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5325 | handle->h_buffer_credits, |
| 5326 | path); |
| 5327 | if (ret) { |
| 5328 | mlog_errno(ret); |
| 5329 | goto out; |
| 5330 | } |
| 5331 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5332 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5333 | if (ret) { |
| 5334 | mlog_errno(ret); |
| 5335 | goto out; |
| 5336 | } |
| 5337 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5338 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5339 | if (ret) { |
| 5340 | mlog_errno(ret); |
| 5341 | goto out; |
| 5342 | } |
| 5343 | |
| 5344 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5345 | trunc_range = cpos + len; |
| 5346 | |
| 5347 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5348 | int next_free; |
| 5349 | |
| 5350 | memset(rec, 0, sizeof(*rec)); |
| 5351 | ocfs2_cleanup_merge(el, index); |
| 5352 | wants_rotate = 1; |
| 5353 | |
| 5354 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5355 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5356 | /* |
| 5357 | * We skip the edge update if this path will |
| 5358 | * be deleted by the rotate code. |
| 5359 | */ |
| 5360 | rec = &el->l_recs[next_free - 1]; |
| 5361 | ocfs2_adjust_rightmost_records(inode, handle, path, |
| 5362 | rec); |
| 5363 | } |
| 5364 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5365 | /* Remove leftmost portion of the record. */ |
| 5366 | le32_add_cpu(&rec->e_cpos, len); |
| 5367 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5368 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5369 | } else if (rec_range == trunc_range) { |
| 5370 | /* Remove rightmost portion of the record */ |
| 5371 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5372 | if (is_rightmost_tree_rec) |
| 5373 | ocfs2_adjust_rightmost_records(inode, handle, path, rec); |
| 5374 | } else { |
| 5375 | /* Caller should have trapped this. */ |
| 5376 | mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) " |
| 5377 | "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5378 | le32_to_cpu(rec->e_cpos), |
| 5379 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5380 | BUG(); |
| 5381 | } |
| 5382 | |
| 5383 | if (left_path) { |
| 5384 | int subtree_index; |
| 5385 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5386 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5387 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5388 | subtree_index); |
| 5389 | } |
| 5390 | |
| 5391 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5392 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5393 | ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5394 | if (ret) { |
| 5395 | mlog_errno(ret); |
| 5396 | goto out; |
| 5397 | } |
| 5398 | |
| 5399 | out: |
| 5400 | ocfs2_free_path(left_path); |
| 5401 | return ret; |
| 5402 | } |
| 5403 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5404 | int ocfs2_remove_extent(struct inode *inode, |
| 5405 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5406 | u32 cpos, u32 len, handle_t *handle, |
| 5407 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5408 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5409 | { |
| 5410 | int ret, index; |
| 5411 | u32 rec_range, trunc_range; |
| 5412 | struct ocfs2_extent_rec *rec; |
| 5413 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5414 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5415 | |
| 5416 | ocfs2_extent_map_trunc(inode, 0); |
| 5417 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5418 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5419 | if (!path) { |
| 5420 | ret = -ENOMEM; |
| 5421 | mlog_errno(ret); |
| 5422 | goto out; |
| 5423 | } |
| 5424 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5425 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5426 | if (ret) { |
| 5427 | mlog_errno(ret); |
| 5428 | goto out; |
| 5429 | } |
| 5430 | |
| 5431 | el = path_leaf_el(path); |
| 5432 | index = ocfs2_search_extent_list(el, cpos); |
| 5433 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5434 | ocfs2_error(inode->i_sb, |
| 5435 | "Inode %llu has an extent at cpos %u which can no " |
| 5436 | "longer be found.\n", |
| 5437 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 5438 | ret = -EROFS; |
| 5439 | goto out; |
| 5440 | } |
| 5441 | |
| 5442 | /* |
| 5443 | * We have 3 cases of extent removal: |
| 5444 | * 1) Range covers the entire extent rec |
| 5445 | * 2) Range begins or ends on one edge of the extent rec |
| 5446 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5447 | * |
| 5448 | * For case 1 we remove the extent rec and left rotate to |
| 5449 | * fill the hole. |
| 5450 | * |
| 5451 | * For case 2 we just shrink the existing extent rec, with a |
| 5452 | * tree update if the shrinking edge is also the edge of an |
| 5453 | * extent block. |
| 5454 | * |
| 5455 | * For case 3 we do a right split to turn the extent rec into |
| 5456 | * something case 2 can handle. |
| 5457 | */ |
| 5458 | rec = &el->l_recs[index]; |
| 5459 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5460 | trunc_range = cpos + len; |
| 5461 | |
| 5462 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5463 | |
| 5464 | mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d " |
| 5465 | "(cpos %u, len %u)\n", |
| 5466 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index, |
| 5467 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 5468 | |
| 5469 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
| 5470 | ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5471 | cpos, len, et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5472 | if (ret) { |
| 5473 | mlog_errno(ret); |
| 5474 | goto out; |
| 5475 | } |
| 5476 | } else { |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5477 | ret = ocfs2_split_tree(inode, et, handle, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5478 | trunc_range, meta_ac); |
| 5479 | if (ret) { |
| 5480 | mlog_errno(ret); |
| 5481 | goto out; |
| 5482 | } |
| 5483 | |
| 5484 | /* |
| 5485 | * The split could have manipulated the tree enough to |
| 5486 | * move the record location, so we have to look for it again. |
| 5487 | */ |
| 5488 | ocfs2_reinit_path(path, 1); |
| 5489 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5490 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5491 | if (ret) { |
| 5492 | mlog_errno(ret); |
| 5493 | goto out; |
| 5494 | } |
| 5495 | |
| 5496 | el = path_leaf_el(path); |
| 5497 | index = ocfs2_search_extent_list(el, cpos); |
| 5498 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5499 | ocfs2_error(inode->i_sb, |
| 5500 | "Inode %llu: split at cpos %u lost record.", |
| 5501 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5502 | cpos); |
| 5503 | ret = -EROFS; |
| 5504 | goto out; |
| 5505 | } |
| 5506 | |
| 5507 | /* |
| 5508 | * Double check our values here. If anything is fishy, |
| 5509 | * it's easier to catch it at the top level. |
| 5510 | */ |
| 5511 | rec = &el->l_recs[index]; |
| 5512 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5513 | ocfs2_rec_clusters(el, rec); |
| 5514 | if (rec_range != trunc_range) { |
| 5515 | ocfs2_error(inode->i_sb, |
| 5516 | "Inode %llu: error after split at cpos %u" |
| 5517 | "trunc len %u, existing record is (%u,%u)", |
| 5518 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5519 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5520 | ocfs2_rec_clusters(el, rec)); |
| 5521 | ret = -EROFS; |
| 5522 | goto out; |
| 5523 | } |
| 5524 | |
| 5525 | ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5526 | cpos, len, et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5527 | if (ret) { |
| 5528 | mlog_errno(ret); |
| 5529 | goto out; |
| 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | out: |
| 5534 | ocfs2_free_path(path); |
| 5535 | return ret; |
| 5536 | } |
| 5537 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5538 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5539 | struct ocfs2_extent_tree *et, |
| 5540 | u32 cpos, u32 phys_cpos, u32 len, |
| 5541 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5542 | { |
| 5543 | int ret; |
| 5544 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5545 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5546 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5547 | handle_t *handle; |
| 5548 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5549 | |
| 5550 | ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); |
| 5551 | if (ret) { |
| 5552 | mlog_errno(ret); |
| 5553 | return ret; |
| 5554 | } |
| 5555 | |
| 5556 | mutex_lock(&tl_inode->i_mutex); |
| 5557 | |
| 5558 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5559 | ret = __ocfs2_flush_truncate_log(osb); |
| 5560 | if (ret < 0) { |
| 5561 | mlog_errno(ret); |
| 5562 | goto out; |
| 5563 | } |
| 5564 | } |
| 5565 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5566 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5567 | if (IS_ERR(handle)) { |
| 5568 | ret = PTR_ERR(handle); |
| 5569 | mlog_errno(ret); |
| 5570 | goto out; |
| 5571 | } |
| 5572 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5573 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5574 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5575 | if (ret) { |
| 5576 | mlog_errno(ret); |
| 5577 | goto out; |
| 5578 | } |
| 5579 | |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5580 | vfs_dq_free_space_nodirty(inode, |
| 5581 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5582 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5583 | ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac, |
| 5584 | dealloc); |
| 5585 | if (ret) { |
| 5586 | mlog_errno(ret); |
| 5587 | goto out_commit; |
| 5588 | } |
| 5589 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5590 | ocfs2_et_update_clusters(et, -len); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5591 | |
| 5592 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
| 5593 | if (ret) { |
| 5594 | mlog_errno(ret); |
| 5595 | goto out_commit; |
| 5596 | } |
| 5597 | |
| 5598 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 5599 | if (ret) |
| 5600 | mlog_errno(ret); |
| 5601 | |
| 5602 | out_commit: |
| 5603 | ocfs2_commit_trans(osb, handle); |
| 5604 | out: |
| 5605 | mutex_unlock(&tl_inode->i_mutex); |
| 5606 | |
| 5607 | if (meta_ac) |
| 5608 | ocfs2_free_alloc_context(meta_ac); |
| 5609 | |
| 5610 | return ret; |
| 5611 | } |
| 5612 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5613 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5614 | { |
| 5615 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5616 | struct ocfs2_dinode *di; |
| 5617 | struct ocfs2_truncate_log *tl; |
| 5618 | |
| 5619 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5620 | tl = &di->id2.i_dealloc; |
| 5621 | |
| 5622 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5623 | "slot %d, invalid truncate log parameters: used = " |
| 5624 | "%u, count = %u\n", osb->slot_num, |
| 5625 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5626 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5627 | } |
| 5628 | |
| 5629 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5630 | unsigned int new_start) |
| 5631 | { |
| 5632 | unsigned int tail_index; |
| 5633 | unsigned int current_tail; |
| 5634 | |
| 5635 | /* No records, nothing to coalesce */ |
| 5636 | if (!le16_to_cpu(tl->tl_used)) |
| 5637 | return 0; |
| 5638 | |
| 5639 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5640 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5641 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5642 | |
| 5643 | return current_tail == new_start; |
| 5644 | } |
| 5645 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5646 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5647 | handle_t *handle, |
| 5648 | u64 start_blk, |
| 5649 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5650 | { |
| 5651 | int status, index; |
| 5652 | unsigned int start_cluster, tl_count; |
| 5653 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5654 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5655 | struct ocfs2_dinode *di; |
| 5656 | struct ocfs2_truncate_log *tl; |
| 5657 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5658 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 5659 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5660 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5661 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5662 | |
| 5663 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5664 | |
| 5665 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5666 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5667 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5668 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5669 | * corruption is a code bug */ |
| 5670 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5671 | |
| 5672 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5673 | tl_count = le16_to_cpu(tl->tl_count); |
| 5674 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5675 | tl_count == 0, |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5676 | "Truncate record count on #%llu invalid " |
| 5677 | "wanted %u, actual %u\n", |
| 5678 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5679 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5680 | le16_to_cpu(tl->tl_count)); |
| 5681 | |
| 5682 | /* Caller should have known to flush before calling us. */ |
| 5683 | index = le16_to_cpu(tl->tl_used); |
| 5684 | if (index >= tl_count) { |
| 5685 | status = -ENOSPC; |
| 5686 | mlog_errno(status); |
| 5687 | goto bail; |
| 5688 | } |
| 5689 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5690 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5691 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5692 | if (status < 0) { |
| 5693 | mlog_errno(status); |
| 5694 | goto bail; |
| 5695 | } |
| 5696 | |
| 5697 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5698 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 5699 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5700 | |
| 5701 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5702 | /* |
| 5703 | * Move index back to the record we are coalescing with. |
| 5704 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5705 | */ |
| 5706 | index--; |
| 5707 | |
| 5708 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 5709 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 5710 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5711 | num_clusters); |
| 5712 | } else { |
| 5713 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5714 | tl->tl_used = cpu_to_le16(index + 1); |
| 5715 | } |
| 5716 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5717 | |
| 5718 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5719 | if (status < 0) { |
| 5720 | mlog_errno(status); |
| 5721 | goto bail; |
| 5722 | } |
| 5723 | |
| 5724 | bail: |
| 5725 | mlog_exit(status); |
| 5726 | return status; |
| 5727 | } |
| 5728 | |
| 5729 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5730 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5731 | struct inode *data_alloc_inode, |
| 5732 | struct buffer_head *data_alloc_bh) |
| 5733 | { |
| 5734 | int status = 0; |
| 5735 | int i; |
| 5736 | unsigned int num_clusters; |
| 5737 | u64 start_blk; |
| 5738 | struct ocfs2_truncate_rec rec; |
| 5739 | struct ocfs2_dinode *di; |
| 5740 | struct ocfs2_truncate_log *tl; |
| 5741 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5742 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5743 | |
| 5744 | mlog_entry_void(); |
| 5745 | |
| 5746 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5747 | tl = &di->id2.i_dealloc; |
| 5748 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5749 | while (i >= 0) { |
| 5750 | /* Caller has given us at least enough credits to |
| 5751 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5752 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5753 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5754 | if (status < 0) { |
| 5755 | mlog_errno(status); |
| 5756 | goto bail; |
| 5757 | } |
| 5758 | |
| 5759 | tl->tl_used = cpu_to_le16(i); |
| 5760 | |
| 5761 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5762 | if (status < 0) { |
| 5763 | mlog_errno(status); |
| 5764 | goto bail; |
| 5765 | } |
| 5766 | |
| 5767 | /* TODO: Perhaps we can calculate the bulk of the |
| 5768 | * credits up front rather than extending like |
| 5769 | * this. */ |
| 5770 | status = ocfs2_extend_trans(handle, |
| 5771 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5772 | if (status < 0) { |
| 5773 | mlog_errno(status); |
| 5774 | goto bail; |
| 5775 | } |
| 5776 | |
| 5777 | rec = tl->tl_recs[i]; |
| 5778 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5779 | le32_to_cpu(rec.t_start)); |
| 5780 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5781 | |
| 5782 | /* if start_blk is not set, we ignore the record as |
| 5783 | * invalid. */ |
| 5784 | if (start_blk) { |
| 5785 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 5786 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 5787 | |
| 5788 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5789 | data_alloc_bh, start_blk, |
| 5790 | num_clusters); |
| 5791 | if (status < 0) { |
| 5792 | mlog_errno(status); |
| 5793 | goto bail; |
| 5794 | } |
| 5795 | } |
| 5796 | i--; |
| 5797 | } |
| 5798 | |
| 5799 | bail: |
| 5800 | mlog_exit(status); |
| 5801 | return status; |
| 5802 | } |
| 5803 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5804 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5805 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5806 | { |
| 5807 | int status; |
| 5808 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5809 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5810 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5811 | struct inode *data_alloc_inode = NULL; |
| 5812 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5813 | struct buffer_head *data_alloc_bh = NULL; |
| 5814 | struct ocfs2_dinode *di; |
| 5815 | struct ocfs2_truncate_log *tl; |
| 5816 | |
| 5817 | mlog_entry_void(); |
| 5818 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5819 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5820 | |
| 5821 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5822 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5823 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5824 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5825 | * corruption is a code bug */ |
| 5826 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5827 | |
| 5828 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5829 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5830 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 5831 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5832 | if (!num_to_flush) { |
| 5833 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5834 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5835 | } |
| 5836 | |
| 5837 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 5838 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 5839 | OCFS2_INVALID_SLOT); |
| 5840 | if (!data_alloc_inode) { |
| 5841 | status = -EINVAL; |
| 5842 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5843 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5844 | } |
| 5845 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5846 | mutex_lock(&data_alloc_inode->i_mutex); |
| 5847 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5848 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5849 | if (status < 0) { |
| 5850 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5851 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5852 | } |
| 5853 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 5854 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5855 | if (IS_ERR(handle)) { |
| 5856 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5857 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5858 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 5862 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5863 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5864 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5865 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5866 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5867 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5868 | out_unlock: |
| 5869 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5870 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5871 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5872 | out_mutex: |
| 5873 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 5874 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5875 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5876 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5877 | mlog_exit(status); |
| 5878 | return status; |
| 5879 | } |
| 5880 | |
| 5881 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 5882 | { |
| 5883 | int status; |
| 5884 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5885 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5886 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5887 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5888 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5889 | |
| 5890 | return status; |
| 5891 | } |
| 5892 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5893 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5894 | { |
| 5895 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5896 | struct ocfs2_super *osb = |
| 5897 | container_of(work, struct ocfs2_super, |
| 5898 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5899 | |
| 5900 | mlog_entry_void(); |
| 5901 | |
| 5902 | status = ocfs2_flush_truncate_log(osb); |
| 5903 | if (status < 0) |
| 5904 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 5905 | else |
| 5906 | ocfs2_init_inode_steal_slot(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5907 | |
| 5908 | mlog_exit(status); |
| 5909 | } |
| 5910 | |
| 5911 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 5912 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 5913 | int cancel) |
| 5914 | { |
| 5915 | if (osb->osb_tl_inode) { |
| 5916 | /* We want to push off log flushes while truncates are |
| 5917 | * still running. */ |
| 5918 | if (cancel) |
| 5919 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 5920 | |
| 5921 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 5922 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 5927 | int slot_num, |
| 5928 | struct inode **tl_inode, |
| 5929 | struct buffer_head **tl_bh) |
| 5930 | { |
| 5931 | int status; |
| 5932 | struct inode *inode = NULL; |
| 5933 | struct buffer_head *bh = NULL; |
| 5934 | |
| 5935 | inode = ocfs2_get_system_file_inode(osb, |
| 5936 | TRUNCATE_LOG_SYSTEM_INODE, |
| 5937 | slot_num); |
| 5938 | if (!inode) { |
| 5939 | status = -EINVAL; |
| 5940 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 5941 | goto bail; |
| 5942 | } |
| 5943 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 5944 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5945 | if (status < 0) { |
| 5946 | iput(inode); |
| 5947 | mlog_errno(status); |
| 5948 | goto bail; |
| 5949 | } |
| 5950 | |
| 5951 | *tl_inode = inode; |
| 5952 | *tl_bh = bh; |
| 5953 | bail: |
| 5954 | mlog_exit(status); |
| 5955 | return status; |
| 5956 | } |
| 5957 | |
| 5958 | /* called during the 1st stage of node recovery. we stamp a clean |
| 5959 | * truncate log and pass back a copy for processing later. if the |
| 5960 | * truncate log does not require processing, a *tl_copy is set to |
| 5961 | * NULL. */ |
| 5962 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 5963 | int slot_num, |
| 5964 | struct ocfs2_dinode **tl_copy) |
| 5965 | { |
| 5966 | int status; |
| 5967 | struct inode *tl_inode = NULL; |
| 5968 | struct buffer_head *tl_bh = NULL; |
| 5969 | struct ocfs2_dinode *di; |
| 5970 | struct ocfs2_truncate_log *tl; |
| 5971 | |
| 5972 | *tl_copy = NULL; |
| 5973 | |
| 5974 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 5975 | |
| 5976 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 5977 | if (status < 0) { |
| 5978 | mlog_errno(status); |
| 5979 | goto bail; |
| 5980 | } |
| 5981 | |
| 5982 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5983 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5984 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 5985 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 5986 | * so any corruption is a code bug */ |
| 5987 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5988 | |
| 5989 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5990 | if (le16_to_cpu(tl->tl_used)) { |
| 5991 | mlog(0, "We'll have %u logs to recover\n", |
| 5992 | le16_to_cpu(tl->tl_used)); |
| 5993 | |
| 5994 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 5995 | if (!(*tl_copy)) { |
| 5996 | status = -ENOMEM; |
| 5997 | mlog_errno(status); |
| 5998 | goto bail; |
| 5999 | } |
| 6000 | |
| 6001 | /* Assuming the write-out below goes well, this copy |
| 6002 | * will be passed back to recovery for processing. */ |
| 6003 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 6004 | |
| 6005 | /* All we need to do to clear the truncate log is set |
| 6006 | * tl_used. */ |
| 6007 | tl->tl_used = 0; |
| 6008 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6009 | ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6010 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6011 | if (status < 0) { |
| 6012 | mlog_errno(status); |
| 6013 | goto bail; |
| 6014 | } |
| 6015 | } |
| 6016 | |
| 6017 | bail: |
| 6018 | if (tl_inode) |
| 6019 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6020 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6021 | |
| 6022 | if (status < 0 && (*tl_copy)) { |
| 6023 | kfree(*tl_copy); |
| 6024 | *tl_copy = NULL; |
| 6025 | } |
| 6026 | |
| 6027 | mlog_exit(status); |
| 6028 | return status; |
| 6029 | } |
| 6030 | |
| 6031 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6032 | struct ocfs2_dinode *tl_copy) |
| 6033 | { |
| 6034 | int status = 0; |
| 6035 | int i; |
| 6036 | unsigned int clusters, num_recs, start_cluster; |
| 6037 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6038 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6039 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6040 | struct ocfs2_truncate_log *tl; |
| 6041 | |
| 6042 | mlog_entry_void(); |
| 6043 | |
| 6044 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6045 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6046 | return -EINVAL; |
| 6047 | } |
| 6048 | |
| 6049 | tl = &tl_copy->id2.i_dealloc; |
| 6050 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 6051 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6052 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6053 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6054 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6055 | for(i = 0; i < num_recs; i++) { |
| 6056 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6057 | status = __ocfs2_flush_truncate_log(osb); |
| 6058 | if (status < 0) { |
| 6059 | mlog_errno(status); |
| 6060 | goto bail_up; |
| 6061 | } |
| 6062 | } |
| 6063 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6064 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6065 | if (IS_ERR(handle)) { |
| 6066 | status = PTR_ERR(handle); |
| 6067 | mlog_errno(status); |
| 6068 | goto bail_up; |
| 6069 | } |
| 6070 | |
| 6071 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6072 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6073 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6074 | |
| 6075 | status = ocfs2_truncate_log_append(osb, handle, |
| 6076 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6077 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6078 | if (status < 0) { |
| 6079 | mlog_errno(status); |
| 6080 | goto bail_up; |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6085 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6086 | |
| 6087 | mlog_exit(status); |
| 6088 | return status; |
| 6089 | } |
| 6090 | |
| 6091 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6092 | { |
| 6093 | int status; |
| 6094 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6095 | |
| 6096 | mlog_entry_void(); |
| 6097 | |
| 6098 | if (tl_inode) { |
| 6099 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6100 | flush_workqueue(ocfs2_wq); |
| 6101 | |
| 6102 | status = ocfs2_flush_truncate_log(osb); |
| 6103 | if (status < 0) |
| 6104 | mlog_errno(status); |
| 6105 | |
| 6106 | brelse(osb->osb_tl_bh); |
| 6107 | iput(osb->osb_tl_inode); |
| 6108 | } |
| 6109 | |
| 6110 | mlog_exit_void(); |
| 6111 | } |
| 6112 | |
| 6113 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6114 | { |
| 6115 | int status; |
| 6116 | struct inode *tl_inode = NULL; |
| 6117 | struct buffer_head *tl_bh = NULL; |
| 6118 | |
| 6119 | mlog_entry_void(); |
| 6120 | |
| 6121 | status = ocfs2_get_truncate_log_info(osb, |
| 6122 | osb->slot_num, |
| 6123 | &tl_inode, |
| 6124 | &tl_bh); |
| 6125 | if (status < 0) |
| 6126 | mlog_errno(status); |
| 6127 | |
| 6128 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6129 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6130 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6131 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6132 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6133 | osb->osb_tl_bh = tl_bh; |
| 6134 | osb->osb_tl_inode = tl_inode; |
| 6135 | |
| 6136 | mlog_exit(status); |
| 6137 | return status; |
| 6138 | } |
| 6139 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6140 | /* |
| 6141 | * Delayed de-allocation of suballocator blocks. |
| 6142 | * |
| 6143 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6144 | * |
| 6145 | * The locking for this can get extremely complicated, especially when |
| 6146 | * the suballocator inodes to delete from aren't known until deep |
| 6147 | * within an unrelated codepath. |
| 6148 | * |
| 6149 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6150 | * btree could have been grown by any number of nodes each allocating |
| 6151 | * out of their own suballoc inode. |
| 6152 | * |
| 6153 | * These structures allow the delay of block de-allocation until a |
| 6154 | * later time, when locking of multiple cluster inodes won't cause |
| 6155 | * deadlock. |
| 6156 | */ |
| 6157 | |
| 6158 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6159 | * Describe a single bit freed from a suballocator. For the block |
| 6160 | * suballocators, it represents one block. For the global cluster |
| 6161 | * allocator, it represents some clusters and free_bit indicates |
| 6162 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6163 | */ |
| 6164 | struct ocfs2_cached_block_free { |
| 6165 | struct ocfs2_cached_block_free *free_next; |
| 6166 | u64 free_blk; |
| 6167 | unsigned int free_bit; |
| 6168 | }; |
| 6169 | |
| 6170 | struct ocfs2_per_slot_free_list { |
| 6171 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6172 | int f_inode_type; |
| 6173 | int f_slot; |
| 6174 | struct ocfs2_cached_block_free *f_first; |
| 6175 | }; |
| 6176 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6177 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6178 | int sysfile_type, |
| 6179 | int slot, |
| 6180 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6181 | { |
| 6182 | int ret; |
| 6183 | u64 bg_blkno; |
| 6184 | handle_t *handle; |
| 6185 | struct inode *inode; |
| 6186 | struct buffer_head *di_bh = NULL; |
| 6187 | struct ocfs2_cached_block_free *tmp; |
| 6188 | |
| 6189 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6190 | if (!inode) { |
| 6191 | ret = -EINVAL; |
| 6192 | mlog_errno(ret); |
| 6193 | goto out; |
| 6194 | } |
| 6195 | |
| 6196 | mutex_lock(&inode->i_mutex); |
| 6197 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6198 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6199 | if (ret) { |
| 6200 | mlog_errno(ret); |
| 6201 | goto out_mutex; |
| 6202 | } |
| 6203 | |
| 6204 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6205 | if (IS_ERR(handle)) { |
| 6206 | ret = PTR_ERR(handle); |
| 6207 | mlog_errno(ret); |
| 6208 | goto out_unlock; |
| 6209 | } |
| 6210 | |
| 6211 | while (head) { |
| 6212 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6213 | head->free_bit); |
| 6214 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 6215 | head->free_bit, (unsigned long long)head->free_blk); |
| 6216 | |
| 6217 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6218 | head->free_bit, bg_blkno, 1); |
| 6219 | if (ret) { |
| 6220 | mlog_errno(ret); |
| 6221 | goto out_journal; |
| 6222 | } |
| 6223 | |
| 6224 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 6225 | if (ret) { |
| 6226 | mlog_errno(ret); |
| 6227 | goto out_journal; |
| 6228 | } |
| 6229 | |
| 6230 | tmp = head; |
| 6231 | head = head->free_next; |
| 6232 | kfree(tmp); |
| 6233 | } |
| 6234 | |
| 6235 | out_journal: |
| 6236 | ocfs2_commit_trans(osb, handle); |
| 6237 | |
| 6238 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6239 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6240 | brelse(di_bh); |
| 6241 | out_mutex: |
| 6242 | mutex_unlock(&inode->i_mutex); |
| 6243 | iput(inode); |
| 6244 | out: |
| 6245 | while(head) { |
| 6246 | /* Premature exit may have left some dangling items. */ |
| 6247 | tmp = head; |
| 6248 | head = head->free_next; |
| 6249 | kfree(tmp); |
| 6250 | } |
| 6251 | |
| 6252 | return ret; |
| 6253 | } |
| 6254 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6255 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6256 | u64 blkno, unsigned int bit) |
| 6257 | { |
| 6258 | int ret = 0; |
| 6259 | struct ocfs2_cached_block_free *item; |
| 6260 | |
| 6261 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6262 | if (item == NULL) { |
| 6263 | ret = -ENOMEM; |
| 6264 | mlog_errno(ret); |
| 6265 | return ret; |
| 6266 | } |
| 6267 | |
| 6268 | mlog(0, "Insert clusters: (bit %u, blk %llu)\n", |
| 6269 | bit, (unsigned long long)blkno); |
| 6270 | |
| 6271 | item->free_blk = blkno; |
| 6272 | item->free_bit = bit; |
| 6273 | item->free_next = ctxt->c_global_allocator; |
| 6274 | |
| 6275 | ctxt->c_global_allocator = item; |
| 6276 | return ret; |
| 6277 | } |
| 6278 | |
| 6279 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6280 | struct ocfs2_cached_block_free *head) |
| 6281 | { |
| 6282 | struct ocfs2_cached_block_free *tmp; |
| 6283 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6284 | handle_t *handle; |
| 6285 | int ret = 0; |
| 6286 | |
| 6287 | mutex_lock(&tl_inode->i_mutex); |
| 6288 | |
| 6289 | while (head) { |
| 6290 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6291 | ret = __ocfs2_flush_truncate_log(osb); |
| 6292 | if (ret < 0) { |
| 6293 | mlog_errno(ret); |
| 6294 | break; |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6299 | if (IS_ERR(handle)) { |
| 6300 | ret = PTR_ERR(handle); |
| 6301 | mlog_errno(ret); |
| 6302 | break; |
| 6303 | } |
| 6304 | |
| 6305 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6306 | head->free_bit); |
| 6307 | |
| 6308 | ocfs2_commit_trans(osb, handle); |
| 6309 | tmp = head; |
| 6310 | head = head->free_next; |
| 6311 | kfree(tmp); |
| 6312 | |
| 6313 | if (ret < 0) { |
| 6314 | mlog_errno(ret); |
| 6315 | break; |
| 6316 | } |
| 6317 | } |
| 6318 | |
| 6319 | mutex_unlock(&tl_inode->i_mutex); |
| 6320 | |
| 6321 | while (head) { |
| 6322 | /* Premature exit may have left some dangling items. */ |
| 6323 | tmp = head; |
| 6324 | head = head->free_next; |
| 6325 | kfree(tmp); |
| 6326 | } |
| 6327 | |
| 6328 | return ret; |
| 6329 | } |
| 6330 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6331 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6332 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6333 | { |
| 6334 | int ret = 0, ret2; |
| 6335 | struct ocfs2_per_slot_free_list *fl; |
| 6336 | |
| 6337 | if (!ctxt) |
| 6338 | return 0; |
| 6339 | |
| 6340 | while (ctxt->c_first_suballocator) { |
| 6341 | fl = ctxt->c_first_suballocator; |
| 6342 | |
| 6343 | if (fl->f_first) { |
| 6344 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 6345 | fl->f_inode_type, fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6346 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6347 | fl->f_inode_type, |
| 6348 | fl->f_slot, |
| 6349 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6350 | if (ret2) |
| 6351 | mlog_errno(ret2); |
| 6352 | if (!ret) |
| 6353 | ret = ret2; |
| 6354 | } |
| 6355 | |
| 6356 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6357 | kfree(fl); |
| 6358 | } |
| 6359 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6360 | if (ctxt->c_global_allocator) { |
| 6361 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6362 | ctxt->c_global_allocator); |
| 6363 | if (ret2) |
| 6364 | mlog_errno(ret2); |
| 6365 | if (!ret) |
| 6366 | ret = ret2; |
| 6367 | |
| 6368 | ctxt->c_global_allocator = NULL; |
| 6369 | } |
| 6370 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6371 | return ret; |
| 6372 | } |
| 6373 | |
| 6374 | static struct ocfs2_per_slot_free_list * |
| 6375 | ocfs2_find_per_slot_free_list(int type, |
| 6376 | int slot, |
| 6377 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6378 | { |
| 6379 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6380 | |
| 6381 | while (fl) { |
| 6382 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6383 | return fl; |
| 6384 | |
| 6385 | fl = fl->f_next_suballocator; |
| 6386 | } |
| 6387 | |
| 6388 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6389 | if (fl) { |
| 6390 | fl->f_inode_type = type; |
| 6391 | fl->f_slot = slot; |
| 6392 | fl->f_first = NULL; |
| 6393 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6394 | |
| 6395 | ctxt->c_first_suballocator = fl; |
| 6396 | } |
| 6397 | return fl; |
| 6398 | } |
| 6399 | |
| 6400 | static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6401 | int type, int slot, u64 blkno, |
| 6402 | unsigned int bit) |
| 6403 | { |
| 6404 | int ret; |
| 6405 | struct ocfs2_per_slot_free_list *fl; |
| 6406 | struct ocfs2_cached_block_free *item; |
| 6407 | |
| 6408 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6409 | if (fl == NULL) { |
| 6410 | ret = -ENOMEM; |
| 6411 | mlog_errno(ret); |
| 6412 | goto out; |
| 6413 | } |
| 6414 | |
| 6415 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6416 | if (item == NULL) { |
| 6417 | ret = -ENOMEM; |
| 6418 | mlog_errno(ret); |
| 6419 | goto out; |
| 6420 | } |
| 6421 | |
| 6422 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 6423 | type, slot, bit, (unsigned long long)blkno); |
| 6424 | |
| 6425 | item->free_blk = blkno; |
| 6426 | item->free_bit = bit; |
| 6427 | item->free_next = fl->f_first; |
| 6428 | |
| 6429 | fl->f_first = item; |
| 6430 | |
| 6431 | ret = 0; |
| 6432 | out: |
| 6433 | return ret; |
| 6434 | } |
| 6435 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6436 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6437 | struct ocfs2_extent_block *eb) |
| 6438 | { |
| 6439 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6440 | le16_to_cpu(eb->h_suballoc_slot), |
| 6441 | le64_to_cpu(eb->h_blkno), |
| 6442 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6443 | } |
| 6444 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6445 | /* This function will figure out whether the currently last extent |
| 6446 | * block will be deleted, and if it will, what the new last extent |
| 6447 | * block will be so we can update his h_next_leaf_blk field, as well |
| 6448 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6449 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6450 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6451 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6452 | struct buffer_head **new_last_eb) |
| 6453 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6454 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6455 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6456 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6457 | struct ocfs2_extent_block *eb; |
| 6458 | struct ocfs2_extent_list *el; |
| 6459 | struct buffer_head *bh = NULL; |
| 6460 | |
| 6461 | *new_last_eb = NULL; |
| 6462 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6463 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6464 | if (!path->p_tree_depth) |
| 6465 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6466 | |
| 6467 | /* trunc to zero special case - this makes tree_depth = 0 |
| 6468 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6469 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6470 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6471 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6472 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6473 | BUG_ON(!el->l_next_free_rec); |
| 6474 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6475 | /* |
| 6476 | * Make sure that this extent list will actually be empty |
| 6477 | * after we clear away the data. We can shortcut out if |
| 6478 | * there's more than one non-empty extent in the |
| 6479 | * list. Otherwise, a check of the remaining extent is |
| 6480 | * necessary. |
| 6481 | */ |
| 6482 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 6483 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6484 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6485 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6486 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6487 | |
| 6488 | /* We may have a valid extent in index 1, check it. */ |
| 6489 | if (next_free == 2) |
| 6490 | rec = &el->l_recs[1]; |
| 6491 | |
| 6492 | /* |
| 6493 | * Fall through - no more nonempty extents, so we want |
| 6494 | * to delete this leaf. |
| 6495 | */ |
| 6496 | } else { |
| 6497 | if (next_free > 1) |
| 6498 | goto out; |
| 6499 | |
| 6500 | rec = &el->l_recs[0]; |
| 6501 | } |
| 6502 | |
| 6503 | if (rec) { |
| 6504 | /* |
| 6505 | * Check it we'll only be trimming off the end of this |
| 6506 | * cluster. |
| 6507 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6508 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6509 | goto out; |
| 6510 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6511 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6512 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 6513 | if (ret) { |
| 6514 | mlog_errno(ret); |
| 6515 | goto out; |
| 6516 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6517 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 6518 | ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6519 | if (ret) { |
| 6520 | mlog_errno(ret); |
| 6521 | goto out; |
| 6522 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6523 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6524 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 6525 | el = &eb->h_list; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 6526 | |
| 6527 | /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). |
| 6528 | * Any corruption is a code bug. */ |
| 6529 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6530 | |
| 6531 | *new_last_eb = bh; |
| 6532 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6533 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 6534 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 6535 | out: |
| 6536 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6537 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6538 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6539 | } |
| 6540 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6541 | /* |
| 6542 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 6543 | * during truncate. |
| 6544 | * |
| 6545 | * The caller needs to: |
| 6546 | * - start journaling of each path component. |
| 6547 | * - compute and fully set up any new last ext block |
| 6548 | */ |
| 6549 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 6550 | handle_t *handle, struct ocfs2_truncate_context *tc, |
| 6551 | u32 clusters_to_del, u64 *delete_start) |
| 6552 | { |
| 6553 | int ret, i, index = path->p_tree_depth; |
| 6554 | u32 new_edge = 0; |
| 6555 | u64 deleted_eb = 0; |
| 6556 | struct buffer_head *bh; |
| 6557 | struct ocfs2_extent_list *el; |
| 6558 | struct ocfs2_extent_rec *rec; |
| 6559 | |
| 6560 | *delete_start = 0; |
| 6561 | |
| 6562 | while (index >= 0) { |
| 6563 | bh = path->p_node[index].bh; |
| 6564 | el = path->p_node[index].el; |
| 6565 | |
| 6566 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 6567 | index, (unsigned long long)bh->b_blocknr); |
| 6568 | |
| 6569 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 6570 | |
| 6571 | if (index != |
| 6572 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 6573 | ocfs2_error(inode->i_sb, |
| 6574 | "Inode %lu has invalid ext. block %llu", |
| 6575 | inode->i_ino, |
| 6576 | (unsigned long long)bh->b_blocknr); |
| 6577 | ret = -EROFS; |
| 6578 | goto out; |
| 6579 | } |
| 6580 | |
| 6581 | find_tail_record: |
| 6582 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 6583 | rec = &el->l_recs[i]; |
| 6584 | |
| 6585 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 6586 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6587 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6588 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6589 | le16_to_cpu(el->l_next_free_rec)); |
| 6590 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6591 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6592 | |
| 6593 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 6594 | /* |
| 6595 | * If the leaf block contains a single empty |
| 6596 | * extent and no records, we can just remove |
| 6597 | * the block. |
| 6598 | */ |
| 6599 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 6600 | memset(rec, 0, |
| 6601 | sizeof(struct ocfs2_extent_rec)); |
| 6602 | el->l_next_free_rec = cpu_to_le16(0); |
| 6603 | |
| 6604 | goto delete; |
| 6605 | } |
| 6606 | |
| 6607 | /* |
| 6608 | * Remove any empty extents by shifting things |
| 6609 | * left. That should make life much easier on |
| 6610 | * the code below. This condition is rare |
| 6611 | * enough that we shouldn't see a performance |
| 6612 | * hit. |
| 6613 | */ |
| 6614 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 6615 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6616 | |
| 6617 | for(i = 0; |
| 6618 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 6619 | el->l_recs[i] = el->l_recs[i + 1]; |
| 6620 | |
| 6621 | memset(&el->l_recs[i], 0, |
| 6622 | sizeof(struct ocfs2_extent_rec)); |
| 6623 | |
| 6624 | /* |
| 6625 | * We've modified our extent list. The |
| 6626 | * simplest way to handle this change |
| 6627 | * is to being the search from the |
| 6628 | * start again. |
| 6629 | */ |
| 6630 | goto find_tail_record; |
| 6631 | } |
| 6632 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6633 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6634 | |
| 6635 | /* |
| 6636 | * We'll use "new_edge" on our way back up the |
| 6637 | * tree to know what our rightmost cpos is. |
| 6638 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6639 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6640 | new_edge += le32_to_cpu(rec->e_cpos); |
| 6641 | |
| 6642 | /* |
| 6643 | * The caller will use this to delete data blocks. |
| 6644 | */ |
| 6645 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 6646 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6647 | le16_to_cpu(rec->e_leaf_clusters)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6648 | |
| 6649 | /* |
| 6650 | * If it's now empty, remove this record. |
| 6651 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6652 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6653 | memset(rec, 0, |
| 6654 | sizeof(struct ocfs2_extent_rec)); |
| 6655 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6656 | } |
| 6657 | } else { |
| 6658 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 6659 | memset(rec, 0, |
| 6660 | sizeof(struct ocfs2_extent_rec)); |
| 6661 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6662 | |
| 6663 | goto delete; |
| 6664 | } |
| 6665 | |
| 6666 | /* Can this actually happen? */ |
| 6667 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 6668 | goto delete; |
| 6669 | |
| 6670 | /* |
| 6671 | * We never actually deleted any clusters |
| 6672 | * because our leaf was empty. There's no |
| 6673 | * reason to adjust the rightmost edge then. |
| 6674 | */ |
| 6675 | if (new_edge == 0) |
| 6676 | goto delete; |
| 6677 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6678 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 6679 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6680 | -le32_to_cpu(rec->e_cpos)); |
| 6681 | |
| 6682 | /* |
| 6683 | * A deleted child record should have been |
| 6684 | * caught above. |
| 6685 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6686 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6687 | } |
| 6688 | |
| 6689 | delete: |
| 6690 | ret = ocfs2_journal_dirty(handle, bh); |
| 6691 | if (ret) { |
| 6692 | mlog_errno(ret); |
| 6693 | goto out; |
| 6694 | } |
| 6695 | |
| 6696 | mlog(0, "extent list container %llu, after: record %d: " |
| 6697 | "(%u, %u, %llu), next = %u.\n", |
| 6698 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6699 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6700 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6701 | le16_to_cpu(el->l_next_free_rec)); |
| 6702 | |
| 6703 | /* |
| 6704 | * We must be careful to only attempt delete of an |
| 6705 | * extent block (and not the root inode block). |
| 6706 | */ |
| 6707 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 6708 | struct ocfs2_extent_block *eb = |
| 6709 | (struct ocfs2_extent_block *)bh->b_data; |
| 6710 | |
| 6711 | /* |
| 6712 | * Save this for use when processing the |
| 6713 | * parent block. |
| 6714 | */ |
| 6715 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 6716 | |
| 6717 | mlog(0, "deleting this extent block.\n"); |
| 6718 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6719 | ocfs2_remove_from_cache(INODE_CACHE(inode), bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6720 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6721 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6722 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 6723 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 6724 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6725 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 6726 | /* An error here is not fatal. */ |
| 6727 | if (ret < 0) |
| 6728 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6729 | } else { |
| 6730 | deleted_eb = 0; |
| 6731 | } |
| 6732 | |
| 6733 | index--; |
| 6734 | } |
| 6735 | |
| 6736 | ret = 0; |
| 6737 | out: |
| 6738 | return ret; |
| 6739 | } |
| 6740 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6741 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 6742 | unsigned int clusters_to_del, |
| 6743 | struct inode *inode, |
| 6744 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6745 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6746 | struct ocfs2_truncate_context *tc, |
| 6747 | struct ocfs2_path *path) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6748 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6749 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6750 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6751 | struct ocfs2_extent_block *last_eb = NULL; |
| 6752 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6753 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6754 | u64 delete_blk = 0; |
| 6755 | |
| 6756 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6757 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6758 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6759 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6760 | if (status < 0) { |
| 6761 | mlog_errno(status); |
| 6762 | goto bail; |
| 6763 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6764 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6765 | /* |
| 6766 | * Each component will be touched, so we might as well journal |
| 6767 | * here to avoid having to handle errors later. |
| 6768 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6769 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6770 | if (status < 0) { |
| 6771 | mlog_errno(status); |
| 6772 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | if (last_eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6776 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6777 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6778 | if (status < 0) { |
| 6779 | mlog_errno(status); |
| 6780 | goto bail; |
| 6781 | } |
| 6782 | |
| 6783 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6784 | } |
| 6785 | |
| 6786 | el = &(fe->id2.i_list); |
| 6787 | |
| 6788 | /* |
| 6789 | * Lower levels depend on this never happening, but it's best |
| 6790 | * to check it up here before changing the tree. |
| 6791 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6792 | if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6793 | ocfs2_error(inode->i_sb, |
| 6794 | "Inode %lu has an empty extent record, depth %u\n", |
| 6795 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6796 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6797 | goto bail; |
| 6798 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6799 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6800 | vfs_dq_free_space_nodirty(inode, |
| 6801 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6802 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 6803 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 6804 | clusters_to_del; |
| 6805 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 6806 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 6807 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6808 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6809 | status = ocfs2_trim_tree(inode, path, handle, tc, |
| 6810 | clusters_to_del, &delete_blk); |
| 6811 | if (status) { |
| 6812 | mlog_errno(status); |
| 6813 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6814 | } |
| 6815 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6816 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6817 | /* trunc to zero is a special case. */ |
| 6818 | el->l_tree_depth = 0; |
| 6819 | fe->i_last_eb_blk = 0; |
| 6820 | } else if (last_eb) |
| 6821 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 6822 | |
| 6823 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 6824 | if (status < 0) { |
| 6825 | mlog_errno(status); |
| 6826 | goto bail; |
| 6827 | } |
| 6828 | |
| 6829 | if (last_eb) { |
| 6830 | /* If there will be a new last extent block, then by |
| 6831 | * definition, there cannot be any leaves to the right of |
| 6832 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6833 | last_eb->h_next_leaf_blk = 0; |
| 6834 | status = ocfs2_journal_dirty(handle, last_eb_bh); |
| 6835 | if (status < 0) { |
| 6836 | mlog_errno(status); |
| 6837 | goto bail; |
| 6838 | } |
| 6839 | } |
| 6840 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6841 | if (delete_blk) { |
| 6842 | status = ocfs2_truncate_log_append(osb, handle, delete_blk, |
| 6843 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6844 | if (status < 0) { |
| 6845 | mlog_errno(status); |
| 6846 | goto bail; |
| 6847 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6848 | } |
| 6849 | status = 0; |
| 6850 | bail: |
Tao Ma | 60e2ec4 | 2009-08-12 14:42:47 +0800 | [diff] [blame] | 6851 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6852 | mlog_exit(status); |
| 6853 | return status; |
| 6854 | } |
| 6855 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6856 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6857 | { |
| 6858 | set_buffer_uptodate(bh); |
| 6859 | mark_buffer_dirty(bh); |
| 6860 | return 0; |
| 6861 | } |
| 6862 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6863 | static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 6864 | unsigned int from, unsigned int to, |
| 6865 | struct page *page, int zero, u64 *phys) |
| 6866 | { |
| 6867 | int ret, partial = 0; |
| 6868 | |
| 6869 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 6870 | if (ret) |
| 6871 | mlog_errno(ret); |
| 6872 | |
| 6873 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 6874 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6875 | |
| 6876 | /* |
| 6877 | * Need to set the buffers we zero'd into uptodate |
| 6878 | * here if they aren't - ocfs2_map_page_blocks() |
| 6879 | * might've skipped some |
| 6880 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6881 | ret = walk_page_buffers(handle, page_buffers(page), |
| 6882 | from, to, &partial, |
| 6883 | ocfs2_zero_func); |
| 6884 | if (ret < 0) |
| 6885 | mlog_errno(ret); |
| 6886 | else if (ocfs2_should_order_data(inode)) { |
| 6887 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6888 | if (ret < 0) |
| 6889 | mlog_errno(ret); |
| 6890 | } |
| 6891 | |
| 6892 | if (!partial) |
| 6893 | SetPageUptodate(page); |
| 6894 | |
| 6895 | flush_dcache_page(page); |
| 6896 | } |
| 6897 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6898 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 6899 | loff_t end, struct page **pages, |
| 6900 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6901 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6902 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6903 | struct page *page; |
| 6904 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 6905 | struct super_block *sb = inode->i_sb; |
| 6906 | |
| 6907 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 6908 | |
| 6909 | if (numpages == 0) |
| 6910 | goto out; |
| 6911 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6912 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6913 | for(i = 0; i < numpages; i++) { |
| 6914 | page = pages[i]; |
| 6915 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6916 | from = start & (PAGE_CACHE_SIZE - 1); |
| 6917 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 6918 | to = end & (PAGE_CACHE_SIZE - 1); |
| 6919 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6920 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 6921 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 6922 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6923 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 6924 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6925 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6926 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6927 | } |
| 6928 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6929 | if (pages) |
| 6930 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6931 | } |
| 6932 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6933 | static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6934 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6935 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6936 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6937 | struct super_block *sb = inode->i_sb; |
| 6938 | struct address_space *mapping = inode->i_mapping; |
| 6939 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6940 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6941 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6942 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6943 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6944 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 6945 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 6946 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6947 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6948 | last_page_bytes = PAGE_ALIGN(end); |
| 6949 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6950 | do { |
| 6951 | pages[numpages] = grab_cache_page(mapping, index); |
| 6952 | if (!pages[numpages]) { |
| 6953 | ret = -ENOMEM; |
| 6954 | mlog_errno(ret); |
| 6955 | goto out; |
| 6956 | } |
| 6957 | |
| 6958 | numpages++; |
| 6959 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6960 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6961 | |
| 6962 | out: |
| 6963 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6964 | if (pages) |
| 6965 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6966 | numpages = 0; |
| 6967 | } |
| 6968 | |
| 6969 | *num = numpages; |
| 6970 | |
| 6971 | return ret; |
| 6972 | } |
| 6973 | |
| 6974 | /* |
| 6975 | * Zero the area past i_size but still within an allocated |
| 6976 | * cluster. This avoids exposing nonzero data on subsequent file |
| 6977 | * extends. |
| 6978 | * |
| 6979 | * We need to call this before i_size is updated on the inode because |
| 6980 | * otherwise block_write_full_page() will skip writeout of pages past |
| 6981 | * i_size. The new_i_size parameter is passed for this reason. |
| 6982 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6983 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 6984 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6985 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6986 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6987 | struct page **pages = NULL; |
| 6988 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6989 | unsigned int ext_flags; |
| 6990 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6991 | |
| 6992 | /* |
| 6993 | * File systems which don't support sparse files zero on every |
| 6994 | * extend. |
| 6995 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6996 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6997 | return 0; |
| 6998 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6999 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7000 | sizeof(struct page *), GFP_NOFS); |
| 7001 | if (pages == NULL) { |
| 7002 | ret = -ENOMEM; |
| 7003 | mlog_errno(ret); |
| 7004 | goto out; |
| 7005 | } |
| 7006 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7007 | if (range_start == range_end) |
| 7008 | goto out; |
| 7009 | |
| 7010 | ret = ocfs2_extent_map_get_blocks(inode, |
| 7011 | range_start >> sb->s_blocksize_bits, |
| 7012 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7013 | if (ret) { |
| 7014 | mlog_errno(ret); |
| 7015 | goto out; |
| 7016 | } |
| 7017 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7018 | /* |
| 7019 | * Tail is a hole, or is marked unwritten. In either case, we |
| 7020 | * can count on read and write to return/push zero's. |
| 7021 | */ |
| 7022 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7023 | goto out; |
| 7024 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7025 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 7026 | &numpages); |
| 7027 | if (ret) { |
| 7028 | mlog_errno(ret); |
| 7029 | goto out; |
| 7030 | } |
| 7031 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7032 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 7033 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7034 | |
| 7035 | /* |
| 7036 | * Initiate writeout of the pages we zero'd here. We don't |
| 7037 | * wait on them - the truncate_inode_pages() call later will |
| 7038 | * do that for us. |
| 7039 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7040 | ret = do_sync_mapping_range(inode->i_mapping, range_start, |
| 7041 | range_end - 1, SYNC_FILE_RANGE_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7042 | if (ret) |
| 7043 | mlog_errno(ret); |
| 7044 | |
| 7045 | out: |
| 7046 | if (pages) |
| 7047 | kfree(pages); |
| 7048 | |
| 7049 | return ret; |
| 7050 | } |
| 7051 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7052 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 7053 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7054 | { |
| 7055 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7056 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7057 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7058 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 7059 | memset(&di->id2, 0, blocksize - |
| 7060 | offsetof(struct ocfs2_dinode, id2) - |
| 7061 | xattrsize); |
| 7062 | else |
| 7063 | memset(&di->id2, 0, blocksize - |
| 7064 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7065 | } |
| 7066 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7067 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 7068 | struct ocfs2_dinode *di) |
| 7069 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7070 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7071 | di->id2.i_list.l_tree_depth = 0; |
| 7072 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7073 | di->id2.i_list.l_count = cpu_to_le16( |
| 7074 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7075 | } |
| 7076 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7077 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 7078 | { |
| 7079 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7080 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7081 | |
| 7082 | spin_lock(&oi->ip_lock); |
| 7083 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 7084 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7085 | spin_unlock(&oi->ip_lock); |
| 7086 | |
| 7087 | /* |
| 7088 | * We clear the entire i_data structure here so that all |
| 7089 | * fields can be properly initialized. |
| 7090 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7091 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7092 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7093 | idata->id_count = cpu_to_le16( |
| 7094 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 7098 | struct buffer_head *di_bh) |
| 7099 | { |
| 7100 | int ret, i, has_data, num_pages = 0; |
| 7101 | handle_t *handle; |
| 7102 | u64 uninitialized_var(block); |
| 7103 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7104 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7105 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7106 | struct ocfs2_alloc_context *data_ac = NULL; |
| 7107 | struct page **pages = NULL; |
| 7108 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7109 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7110 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7111 | |
| 7112 | has_data = i_size_read(inode) ? 1 : 0; |
| 7113 | |
| 7114 | if (has_data) { |
| 7115 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 7116 | sizeof(struct page *), GFP_NOFS); |
| 7117 | if (pages == NULL) { |
| 7118 | ret = -ENOMEM; |
| 7119 | mlog_errno(ret); |
| 7120 | goto out; |
| 7121 | } |
| 7122 | |
| 7123 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 7124 | if (ret) { |
| 7125 | mlog_errno(ret); |
| 7126 | goto out; |
| 7127 | } |
| 7128 | } |
| 7129 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7130 | handle = ocfs2_start_trans(osb, |
| 7131 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7132 | if (IS_ERR(handle)) { |
| 7133 | ret = PTR_ERR(handle); |
| 7134 | mlog_errno(ret); |
| 7135 | goto out_unlock; |
| 7136 | } |
| 7137 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7138 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7139 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7140 | if (ret) { |
| 7141 | mlog_errno(ret); |
| 7142 | goto out_commit; |
| 7143 | } |
| 7144 | |
| 7145 | if (has_data) { |
| 7146 | u32 bit_off, num; |
| 7147 | unsigned int page_end; |
| 7148 | u64 phys; |
| 7149 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7150 | if (vfs_dq_alloc_space_nodirty(inode, |
| 7151 | ocfs2_clusters_to_bytes(osb->sb, 1))) { |
| 7152 | ret = -EDQUOT; |
| 7153 | goto out_commit; |
| 7154 | } |
| 7155 | did_quota = 1; |
| 7156 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7157 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 7158 | &num); |
| 7159 | if (ret) { |
| 7160 | mlog_errno(ret); |
| 7161 | goto out_commit; |
| 7162 | } |
| 7163 | |
| 7164 | /* |
| 7165 | * Save two copies, one for insert, and one that can |
| 7166 | * be changed by ocfs2_map_and_dirty_page() below. |
| 7167 | */ |
| 7168 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 7169 | |
| 7170 | /* |
| 7171 | * Non sparse file systems zero on extend, so no need |
| 7172 | * to do that now. |
| 7173 | */ |
| 7174 | if (!ocfs2_sparse_alloc(osb) && |
| 7175 | PAGE_CACHE_SIZE < osb->s_clustersize) |
| 7176 | end = PAGE_CACHE_SIZE; |
| 7177 | |
| 7178 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 7179 | if (ret) { |
| 7180 | mlog_errno(ret); |
| 7181 | goto out_commit; |
| 7182 | } |
| 7183 | |
| 7184 | /* |
| 7185 | * This should populate the 1st page for us and mark |
| 7186 | * it up to date. |
| 7187 | */ |
| 7188 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 7189 | if (ret) { |
| 7190 | mlog_errno(ret); |
| 7191 | goto out_commit; |
| 7192 | } |
| 7193 | |
| 7194 | page_end = PAGE_CACHE_SIZE; |
| 7195 | if (PAGE_CACHE_SIZE > osb->s_clustersize) |
| 7196 | page_end = osb->s_clustersize; |
| 7197 | |
| 7198 | for (i = 0; i < num_pages; i++) |
| 7199 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 7200 | pages[i], i > 0, &phys); |
| 7201 | } |
| 7202 | |
| 7203 | spin_lock(&oi->ip_lock); |
| 7204 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 7205 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7206 | spin_unlock(&oi->ip_lock); |
| 7207 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7208 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7209 | |
| 7210 | ocfs2_journal_dirty(handle, di_bh); |
| 7211 | |
| 7212 | if (has_data) { |
| 7213 | /* |
| 7214 | * An error at this point should be extremely rare. If |
| 7215 | * this proves to be false, we could always re-build |
| 7216 | * the in-inode data from our pages. |
| 7217 | */ |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 7218 | ocfs2_init_dinode_extent_tree(&et, inode, di_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7219 | ret = ocfs2_insert_extent(osb, handle, inode, &et, |
| 7220 | 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7221 | if (ret) { |
| 7222 | mlog_errno(ret); |
| 7223 | goto out_commit; |
| 7224 | } |
| 7225 | |
| 7226 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7227 | } |
| 7228 | |
| 7229 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7230 | if (ret < 0 && did_quota) |
| 7231 | vfs_dq_free_space_nodirty(inode, |
| 7232 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7233 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7234 | ocfs2_commit_trans(osb, handle); |
| 7235 | |
| 7236 | out_unlock: |
| 7237 | if (data_ac) |
| 7238 | ocfs2_free_alloc_context(data_ac); |
| 7239 | |
| 7240 | out: |
| 7241 | if (pages) { |
| 7242 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7243 | kfree(pages); |
| 7244 | } |
| 7245 | |
| 7246 | return ret; |
| 7247 | } |
| 7248 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7249 | /* |
| 7250 | * It is expected, that by the time you call this function, |
| 7251 | * inode->i_size and fe->i_size have been adjusted. |
| 7252 | * |
| 7253 | * WARNING: This will kfree the truncate context |
| 7254 | */ |
| 7255 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7256 | struct inode *inode, |
| 7257 | struct buffer_head *fe_bh, |
| 7258 | struct ocfs2_truncate_context *tc) |
| 7259 | { |
| 7260 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7261 | u32 clusters_to_del, new_highest_cpos, range; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7262 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 7263 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7264 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7265 | struct ocfs2_path *path = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 7266 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7267 | |
| 7268 | mlog_entry_void(); |
| 7269 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7270 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7271 | i_size_read(inode)); |
| 7272 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7273 | path = ocfs2_new_path(fe_bh, &di->id2.i_list, |
| 7274 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7275 | if (!path) { |
| 7276 | status = -ENOMEM; |
| 7277 | mlog_errno(status); |
| 7278 | goto bail; |
| 7279 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7280 | |
| 7281 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7282 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7283 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7284 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7285 | * Check that we still have allocation to delete. |
| 7286 | */ |
| 7287 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7288 | status = 0; |
| 7289 | goto bail; |
| 7290 | } |
| 7291 | |
| 7292 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7293 | * Truncate always works against the rightmost tree branch. |
| 7294 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7295 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7296 | if (status) { |
| 7297 | mlog_errno(status); |
| 7298 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7299 | } |
| 7300 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7301 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 7302 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 7303 | |
| 7304 | /* |
| 7305 | * By now, el will point to the extent list on the bottom most |
| 7306 | * portion of this tree. Only the tail record is considered in |
| 7307 | * each pass. |
| 7308 | * |
| 7309 | * We handle the following cases, in order: |
| 7310 | * - empty extent: delete the remaining branch |
| 7311 | * - remove the entire record |
| 7312 | * - remove a partial record |
| 7313 | * - no record needs to be removed (truncate has completed) |
| 7314 | */ |
| 7315 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7316 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7317 | ocfs2_error(inode->i_sb, |
| 7318 | "Inode %llu has empty extent block at %llu\n", |
| 7319 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7320 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7321 | status = -EROFS; |
| 7322 | goto bail; |
| 7323 | } |
| 7324 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7325 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7326 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7327 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7328 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 7329 | clusters_to_del = 0; |
| 7330 | } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7331 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7332 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7333 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7334 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7335 | new_highest_cpos; |
| 7336 | } else { |
| 7337 | status = 0; |
| 7338 | goto bail; |
| 7339 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7340 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7341 | mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", |
| 7342 | clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7343 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7344 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7345 | tl_sem = 1; |
| 7346 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 7347 | * record is free for use. If there isn't any, we flush to get |
| 7348 | * an empty truncate log. */ |
| 7349 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 7350 | status = __ocfs2_flush_truncate_log(osb); |
| 7351 | if (status < 0) { |
| 7352 | mlog_errno(status); |
| 7353 | goto bail; |
| 7354 | } |
| 7355 | } |
| 7356 | |
| 7357 | credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7358 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 7359 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 7360 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7361 | if (IS_ERR(handle)) { |
| 7362 | status = PTR_ERR(handle); |
| 7363 | handle = NULL; |
| 7364 | mlog_errno(status); |
| 7365 | goto bail; |
| 7366 | } |
| 7367 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7368 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
| 7369 | tc, path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7370 | if (status < 0) { |
| 7371 | mlog_errno(status); |
| 7372 | goto bail; |
| 7373 | } |
| 7374 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7375 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7376 | tl_sem = 0; |
| 7377 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7378 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7379 | handle = NULL; |
| 7380 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7381 | ocfs2_reinit_path(path, 1); |
| 7382 | |
| 7383 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7384 | * The check above will catch the case where we've truncated |
| 7385 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7386 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7387 | goto start; |
| 7388 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7389 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7390 | |
| 7391 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7392 | |
| 7393 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7394 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7395 | |
| 7396 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7397 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7398 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7399 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 7400 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7401 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7402 | |
| 7403 | /* This will drop the ext_alloc cluster lock for us */ |
| 7404 | ocfs2_free_truncate_context(tc); |
| 7405 | |
| 7406 | mlog_exit(status); |
| 7407 | return status; |
| 7408 | } |
| 7409 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7410 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7411 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7412 | */ |
| 7413 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 7414 | struct inode *inode, |
| 7415 | struct buffer_head *fe_bh, |
| 7416 | struct ocfs2_truncate_context **tc) |
| 7417 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7418 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7419 | unsigned int new_i_clusters; |
| 7420 | struct ocfs2_dinode *fe; |
| 7421 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7422 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7423 | |
| 7424 | mlog_entry_void(); |
| 7425 | |
| 7426 | *tc = NULL; |
| 7427 | |
| 7428 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 7429 | i_size_read(inode)); |
| 7430 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 7431 | |
| 7432 | mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size =" |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 7433 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 7434 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7435 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 7436 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7437 | if (!(*tc)) { |
| 7438 | status = -ENOMEM; |
| 7439 | mlog_errno(status); |
| 7440 | goto bail; |
| 7441 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7442 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7443 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7444 | if (fe->id2.i_list.l_tree_depth) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 7445 | status = ocfs2_read_extent_block(INODE_CACHE(inode), |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 7446 | le64_to_cpu(fe->i_last_eb_blk), |
| 7447 | &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7448 | if (status < 0) { |
| 7449 | mlog_errno(status); |
| 7450 | goto bail; |
| 7451 | } |
| 7452 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7453 | } |
| 7454 | |
| 7455 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 7456 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7457 | status = 0; |
| 7458 | bail: |
| 7459 | if (status < 0) { |
| 7460 | if (*tc) |
| 7461 | ocfs2_free_truncate_context(*tc); |
| 7462 | *tc = NULL; |
| 7463 | } |
| 7464 | mlog_exit_void(); |
| 7465 | return status; |
| 7466 | } |
| 7467 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7468 | /* |
| 7469 | * 'start' is inclusive, 'end' is not. |
| 7470 | */ |
| 7471 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7472 | unsigned int start, unsigned int end, int trunc) |
| 7473 | { |
| 7474 | int ret; |
| 7475 | unsigned int numbytes; |
| 7476 | handle_t *handle; |
| 7477 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7478 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7479 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7480 | |
| 7481 | if (end > i_size_read(inode)) |
| 7482 | end = i_size_read(inode); |
| 7483 | |
| 7484 | BUG_ON(start >= end); |
| 7485 | |
| 7486 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7487 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7488 | !ocfs2_supports_inline_data(osb)) { |
| 7489 | ocfs2_error(inode->i_sb, |
| 7490 | "Inline data flags for inode %llu don't agree! " |
| 7491 | "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
| 7492 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7493 | le16_to_cpu(di->i_dyn_features), |
| 7494 | OCFS2_I(inode)->ip_dyn_features, |
| 7495 | osb->s_feature_incompat); |
| 7496 | ret = -EROFS; |
| 7497 | goto out; |
| 7498 | } |
| 7499 | |
| 7500 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7501 | if (IS_ERR(handle)) { |
| 7502 | ret = PTR_ERR(handle); |
| 7503 | mlog_errno(ret); |
| 7504 | goto out; |
| 7505 | } |
| 7506 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7507 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7508 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7509 | if (ret) { |
| 7510 | mlog_errno(ret); |
| 7511 | goto out_commit; |
| 7512 | } |
| 7513 | |
| 7514 | numbytes = end - start; |
| 7515 | memset(idata->id_data + start, 0, numbytes); |
| 7516 | |
| 7517 | /* |
| 7518 | * No need to worry about the data page here - it's been |
| 7519 | * truncated already and inline data doesn't need it for |
| 7520 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7521 | * later. |
| 7522 | */ |
| 7523 | if (trunc) { |
| 7524 | i_size_write(inode, start); |
| 7525 | di->i_size = cpu_to_le64(start); |
| 7526 | } |
| 7527 | |
| 7528 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7529 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 7530 | |
| 7531 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7532 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7533 | |
| 7534 | ocfs2_journal_dirty(handle, di_bh); |
| 7535 | |
| 7536 | out_commit: |
| 7537 | ocfs2_commit_trans(osb, handle); |
| 7538 | |
| 7539 | out: |
| 7540 | return ret; |
| 7541 | } |
| 7542 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7543 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 7544 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7545 | /* |
| 7546 | * The caller is responsible for completing deallocation |
| 7547 | * before freeing the context. |
| 7548 | */ |
| 7549 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 7550 | mlog(ML_NOTICE, |
| 7551 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7552 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 7553 | brelse(tc->tc_last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7554 | |
| 7555 | kfree(tc); |
| 7556 | } |