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); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 463 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 464 | struct ocfs2_extent_tree *et, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 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, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1012 | struct ocfs2_extent_tree *et) |
| 1013 | { |
| 1014 | int status; |
| 1015 | struct ocfs2_path *path = NULL; |
| 1016 | struct ocfs2_extent_list *el; |
| 1017 | struct ocfs2_extent_rec *rec; |
| 1018 | |
| 1019 | path = ocfs2_new_path_from_et(et); |
| 1020 | if (!path) { |
| 1021 | status = -ENOMEM; |
| 1022 | return status; |
| 1023 | } |
| 1024 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1025 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1026 | if (status < 0) { |
| 1027 | mlog_errno(status); |
| 1028 | goto out; |
| 1029 | } |
| 1030 | |
| 1031 | status = ocfs2_extend_trans(handle, path_num_items(path) + |
| 1032 | handle->h_buffer_credits); |
| 1033 | if (status < 0) { |
| 1034 | mlog_errno(status); |
| 1035 | goto out; |
| 1036 | } |
| 1037 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1038 | status = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1039 | if (status < 0) { |
| 1040 | mlog_errno(status); |
| 1041 | goto out; |
| 1042 | } |
| 1043 | |
| 1044 | el = path_leaf_el(path); |
| 1045 | rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; |
| 1046 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1047 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1048 | |
| 1049 | out: |
| 1050 | ocfs2_free_path(path); |
| 1051 | return status; |
| 1052 | } |
| 1053 | |
| 1054 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1055 | * Add an entire tree branch to our inode. eb_bh is the extent block |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1056 | * to start at, if we don't want to start the branch at the root |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1057 | * structure. |
| 1058 | * |
| 1059 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1060 | * for the new last extent block. |
| 1061 | * |
| 1062 | * 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] | 1063 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1064 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1065 | static int ocfs2_add_branch(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1066 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1067 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1068 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1069 | struct ocfs2_alloc_context *meta_ac) |
| 1070 | { |
| 1071 | int status, new_blocks, i; |
| 1072 | u64 next_blkno, new_last_eb_blk; |
| 1073 | struct buffer_head *bh; |
| 1074 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1075 | struct ocfs2_extent_block *eb; |
| 1076 | struct ocfs2_extent_list *eb_el; |
| 1077 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1078 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1079 | |
| 1080 | mlog_entry_void(); |
| 1081 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1082 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1083 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1084 | if (eb_bh) { |
| 1085 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1086 | el = &eb->h_list; |
| 1087 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1088 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1089 | |
| 1090 | /* we never add a branch to a leaf. */ |
| 1091 | BUG_ON(!el->l_tree_depth); |
| 1092 | |
| 1093 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1094 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1095 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1096 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1097 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1098 | |
| 1099 | /* |
| 1100 | * If there is a gap before the root end and the real end |
| 1101 | * of the righmost leaf block, we need to remove the gap |
| 1102 | * between new_cpos and root_end first so that the tree |
| 1103 | * is consistent after we add a new branch(it will start |
| 1104 | * from new_cpos). |
| 1105 | */ |
| 1106 | if (root_end > new_cpos) { |
| 1107 | mlog(0, "adjust the cluster end from %u to %u\n", |
| 1108 | root_end, new_cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1109 | status = ocfs2_adjust_rightmost_branch(handle, et); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1110 | if (status) { |
| 1111 | mlog_errno(status); |
| 1112 | goto bail; |
| 1113 | } |
| 1114 | } |
| 1115 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1116 | /* allocate the number of new eb blocks we need */ |
| 1117 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1118 | GFP_KERNEL); |
| 1119 | if (!new_eb_bhs) { |
| 1120 | status = -ENOMEM; |
| 1121 | mlog_errno(status); |
| 1122 | goto bail; |
| 1123 | } |
| 1124 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1125 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1126 | meta_ac, new_eb_bhs); |
| 1127 | if (status < 0) { |
| 1128 | mlog_errno(status); |
| 1129 | goto bail; |
| 1130 | } |
| 1131 | |
| 1132 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1133 | * linked with the rest of the tree. |
| 1134 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1135 | * |
| 1136 | * when we leave the loop, new_last_eb_blk will point to the |
| 1137 | * newest leaf, and next_blkno will point to the topmost extent |
| 1138 | * block. */ |
| 1139 | next_blkno = new_last_eb_blk = 0; |
| 1140 | for(i = 0; i < new_blocks; i++) { |
| 1141 | bh = new_eb_bhs[i]; |
| 1142 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1143 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1144 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1145 | eb_el = &eb->h_list; |
| 1146 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1147 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1148 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1149 | if (status < 0) { |
| 1150 | mlog_errno(status); |
| 1151 | goto bail; |
| 1152 | } |
| 1153 | |
| 1154 | eb->h_next_leaf_blk = 0; |
| 1155 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1156 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1157 | /* |
| 1158 | * This actually counts as an empty extent as |
| 1159 | * c_clusters == 0 |
| 1160 | */ |
| 1161 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1162 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1163 | /* |
| 1164 | * eb_el isn't always an interior node, but even leaf |
| 1165 | * nodes want a zero'd flags and reserved field so |
| 1166 | * this gets the whole 32 bits regardless of use. |
| 1167 | */ |
| 1168 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1169 | if (!eb_el->l_tree_depth) |
| 1170 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1171 | |
| 1172 | status = ocfs2_journal_dirty(handle, bh); |
| 1173 | if (status < 0) { |
| 1174 | mlog_errno(status); |
| 1175 | goto bail; |
| 1176 | } |
| 1177 | |
| 1178 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1179 | } |
| 1180 | |
| 1181 | /* This is a bit hairy. We want to update up to three blocks |
| 1182 | * here without leaving any of them in an inconsistent state |
| 1183 | * in case of error. We don't have to worry about |
| 1184 | * journal_dirty erroring as it won't unless we've aborted the |
| 1185 | * handle (in which case we would never be here) so reserving |
| 1186 | * the write with journal_access is all we need to do. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1187 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1188 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1189 | if (status < 0) { |
| 1190 | mlog_errno(status); |
| 1191 | goto bail; |
| 1192 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1193 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1194 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1195 | if (status < 0) { |
| 1196 | mlog_errno(status); |
| 1197 | goto bail; |
| 1198 | } |
| 1199 | if (eb_bh) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1200 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1201 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1202 | if (status < 0) { |
| 1203 | mlog_errno(status); |
| 1204 | goto bail; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1209 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1210 | i = le16_to_cpu(el->l_next_free_rec); |
| 1211 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1212 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1213 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1214 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1215 | |
| 1216 | /* fe needs a new last extent block pointer, as does the |
| 1217 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1218 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1219 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1220 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1221 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1222 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1223 | status = ocfs2_journal_dirty(handle, *last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1224 | if (status < 0) |
| 1225 | mlog_errno(status); |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1226 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1227 | if (status < 0) |
| 1228 | mlog_errno(status); |
| 1229 | if (eb_bh) { |
| 1230 | status = ocfs2_journal_dirty(handle, eb_bh); |
| 1231 | if (status < 0) |
| 1232 | mlog_errno(status); |
| 1233 | } |
| 1234 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1235 | /* |
| 1236 | * Some callers want to track the rightmost leaf so pass it |
| 1237 | * back here. |
| 1238 | */ |
| 1239 | brelse(*last_eb_bh); |
| 1240 | get_bh(new_eb_bhs[0]); |
| 1241 | *last_eb_bh = new_eb_bhs[0]; |
| 1242 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1243 | status = 0; |
| 1244 | bail: |
| 1245 | if (new_eb_bhs) { |
| 1246 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1247 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1248 | kfree(new_eb_bhs); |
| 1249 | } |
| 1250 | |
| 1251 | mlog_exit(status); |
| 1252 | return status; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * adds another level to the allocation tree. |
| 1257 | * returns back the new extent block so you can add a branch to it |
| 1258 | * after this call. |
| 1259 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1260 | static int ocfs2_shift_tree_depth(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1261 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1262 | struct ocfs2_alloc_context *meta_ac, |
| 1263 | struct buffer_head **ret_new_eb_bh) |
| 1264 | { |
| 1265 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1266 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1267 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1268 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1269 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1270 | struct ocfs2_extent_list *eb_el; |
| 1271 | |
| 1272 | mlog_entry_void(); |
| 1273 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1274 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1275 | &new_eb_bh); |
| 1276 | if (status < 0) { |
| 1277 | mlog_errno(status); |
| 1278 | goto bail; |
| 1279 | } |
| 1280 | |
| 1281 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1282 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1283 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1284 | |
| 1285 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1286 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1287 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1288 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1289 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1290 | if (status < 0) { |
| 1291 | mlog_errno(status); |
| 1292 | goto bail; |
| 1293 | } |
| 1294 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1295 | /* copy the root extent list data into the new extent block */ |
| 1296 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1297 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1298 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1299 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1300 | |
| 1301 | status = ocfs2_journal_dirty(handle, new_eb_bh); |
| 1302 | if (status < 0) { |
| 1303 | mlog_errno(status); |
| 1304 | goto bail; |
| 1305 | } |
| 1306 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1307 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1308 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1309 | if (status < 0) { |
| 1310 | mlog_errno(status); |
| 1311 | goto bail; |
| 1312 | } |
| 1313 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1314 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1315 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1316 | /* update root_bh now */ |
| 1317 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1318 | root_el->l_recs[0].e_cpos = 0; |
| 1319 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1320 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1321 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1322 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1323 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1324 | |
| 1325 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1326 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1327 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1328 | 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] | 1329 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1330 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1331 | if (status < 0) { |
| 1332 | mlog_errno(status); |
| 1333 | goto bail; |
| 1334 | } |
| 1335 | |
| 1336 | *ret_new_eb_bh = new_eb_bh; |
| 1337 | new_eb_bh = NULL; |
| 1338 | status = 0; |
| 1339 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1340 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1341 | |
| 1342 | mlog_exit(status); |
| 1343 | return status; |
| 1344 | } |
| 1345 | |
| 1346 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1347 | * Should only be called when there is no space left in any of the |
| 1348 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1349 | * non-leaf extent block with room for new records. There are three |
| 1350 | * valid results of this search: |
| 1351 | * |
| 1352 | * 1) a lowest extent block is found, then we pass it back in |
| 1353 | * *lowest_eb_bh and return '0' |
| 1354 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1355 | * 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] | 1356 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1357 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1358 | * 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] | 1359 | * which case we return > 0 |
| 1360 | * |
| 1361 | * return status < 0 indicates an error. |
| 1362 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1363 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1364 | struct buffer_head **target_bh) |
| 1365 | { |
| 1366 | int status = 0, i; |
| 1367 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1368 | struct ocfs2_extent_block *eb; |
| 1369 | struct ocfs2_extent_list *el; |
| 1370 | struct buffer_head *bh = NULL; |
| 1371 | struct buffer_head *lowest_bh = NULL; |
| 1372 | |
| 1373 | mlog_entry_void(); |
| 1374 | |
| 1375 | *target_bh = NULL; |
| 1376 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1377 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1378 | |
| 1379 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1380 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1381 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1382 | "Owner %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1383 | "extent list (next_free_rec == 0)", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1384 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1385 | status = -EIO; |
| 1386 | goto bail; |
| 1387 | } |
| 1388 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1389 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1390 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1391 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1392 | "Owner %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1393 | "list where extent # %d has no physical " |
| 1394 | "block start", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1395 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1396 | status = -EIO; |
| 1397 | goto bail; |
| 1398 | } |
| 1399 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1400 | brelse(bh); |
| 1401 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1402 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1403 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1404 | if (status < 0) { |
| 1405 | mlog_errno(status); |
| 1406 | goto bail; |
| 1407 | } |
| 1408 | |
| 1409 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1410 | el = &eb->h_list; |
| 1411 | |
| 1412 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1413 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1414 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1415 | lowest_bh = bh; |
| 1416 | get_bh(lowest_bh); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | /* If we didn't find one and the fe doesn't have any room, |
| 1421 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1422 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1423 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1424 | status = 1; |
| 1425 | |
| 1426 | *target_bh = lowest_bh; |
| 1427 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1428 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1429 | |
| 1430 | mlog_exit(status); |
| 1431 | return status; |
| 1432 | } |
| 1433 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1434 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1435 | * Grow a b-tree so that it has more records. |
| 1436 | * |
| 1437 | * We might shift the tree depth in which case existing paths should |
| 1438 | * be considered invalid. |
| 1439 | * |
| 1440 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1441 | * |
| 1442 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1443 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1444 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 1445 | int *final_depth, struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1446 | struct ocfs2_alloc_context *meta_ac) |
| 1447 | { |
| 1448 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1449 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1450 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1451 | struct buffer_head *bh = NULL; |
| 1452 | |
| 1453 | BUG_ON(meta_ac == NULL); |
| 1454 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1455 | shift = ocfs2_find_branch_target(et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1456 | if (shift < 0) { |
| 1457 | ret = shift; |
| 1458 | mlog_errno(ret); |
| 1459 | goto out; |
| 1460 | } |
| 1461 | |
| 1462 | /* We traveled all the way to the bottom of the allocation tree |
| 1463 | * and didn't find room for any more extents - we need to add |
| 1464 | * another tree level */ |
| 1465 | if (shift) { |
| 1466 | BUG_ON(bh); |
| 1467 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 1468 | |
| 1469 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1470 | * the new extent block (so we can pass that to |
| 1471 | * ocfs2_add_branch). */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1472 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1473 | if (ret < 0) { |
| 1474 | mlog_errno(ret); |
| 1475 | goto out; |
| 1476 | } |
| 1477 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1478 | if (depth == 1) { |
| 1479 | /* |
| 1480 | * Special case: we have room now if we shifted from |
| 1481 | * tree_depth 0, so no more work needs to be done. |
| 1482 | * |
| 1483 | * We won't be calling add_branch, so pass |
| 1484 | * back *last_eb_bh as the new leaf. At depth |
| 1485 | * zero, it should always be null so there's |
| 1486 | * no reason to brelse. |
| 1487 | */ |
| 1488 | BUG_ON(*last_eb_bh); |
| 1489 | get_bh(bh); |
| 1490 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1491 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1492 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1496 | * the new data. */ |
| 1497 | mlog(0, "add branch. bh = %p\n", bh); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 1498 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1499 | meta_ac); |
| 1500 | if (ret < 0) { |
| 1501 | mlog_errno(ret); |
| 1502 | goto out; |
| 1503 | } |
| 1504 | |
| 1505 | out: |
| 1506 | if (final_depth) |
| 1507 | *final_depth = depth; |
| 1508 | brelse(bh); |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
| 1512 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1513 | * This function will discard the rightmost extent record. |
| 1514 | */ |
| 1515 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1516 | { |
| 1517 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1518 | int count = le16_to_cpu(el->l_count); |
| 1519 | unsigned int num_bytes; |
| 1520 | |
| 1521 | BUG_ON(!next_free); |
| 1522 | /* This will cause us to go off the end of our extent list. */ |
| 1523 | BUG_ON(next_free >= count); |
| 1524 | |
| 1525 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1526 | |
| 1527 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1528 | } |
| 1529 | |
| 1530 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1531 | struct ocfs2_extent_rec *insert_rec) |
| 1532 | { |
| 1533 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1534 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1535 | struct ocfs2_extent_rec *rec; |
| 1536 | |
| 1537 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1538 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1539 | |
| 1540 | BUG_ON(!next_free); |
| 1541 | |
| 1542 | /* 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] | 1543 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1544 | |
| 1545 | /* |
| 1546 | * The easiest way to approach this is to just remove the |
| 1547 | * empty extent and temporarily decrement next_free. |
| 1548 | */ |
| 1549 | if (has_empty) { |
| 1550 | /* |
| 1551 | * If next_free was 1 (only an empty extent), this |
| 1552 | * loop won't execute, which is fine. We still want |
| 1553 | * the decrement above to happen. |
| 1554 | */ |
| 1555 | for(i = 0; i < (next_free - 1); i++) |
| 1556 | el->l_recs[i] = el->l_recs[i+1]; |
| 1557 | |
| 1558 | next_free--; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Figure out what the new record index should be. |
| 1563 | */ |
| 1564 | for(i = 0; i < next_free; i++) { |
| 1565 | rec = &el->l_recs[i]; |
| 1566 | |
| 1567 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1568 | break; |
| 1569 | } |
| 1570 | insert_index = i; |
| 1571 | |
| 1572 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1573 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1574 | |
| 1575 | BUG_ON(insert_index < 0); |
| 1576 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1577 | BUG_ON(insert_index > next_free); |
| 1578 | |
| 1579 | /* |
| 1580 | * No need to memmove if we're just adding to the tail. |
| 1581 | */ |
| 1582 | if (insert_index != next_free) { |
| 1583 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1584 | |
| 1585 | num_bytes = next_free - insert_index; |
| 1586 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1587 | memmove(&el->l_recs[insert_index + 1], |
| 1588 | &el->l_recs[insert_index], |
| 1589 | num_bytes); |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * Either we had an empty extent, and need to re-increment or |
| 1594 | * there was no empty extent on a non full rightmost leaf node, |
| 1595 | * in which case we still need to increment. |
| 1596 | */ |
| 1597 | next_free++; |
| 1598 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1599 | /* |
| 1600 | * Make sure none of the math above just messed up our tree. |
| 1601 | */ |
| 1602 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1603 | |
| 1604 | el->l_recs[insert_index] = *insert_rec; |
| 1605 | |
| 1606 | } |
| 1607 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1608 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1609 | { |
| 1610 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1611 | |
| 1612 | BUG_ON(num_recs == 0); |
| 1613 | |
| 1614 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1615 | num_recs--; |
| 1616 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1617 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1618 | memset(&el->l_recs[num_recs], 0, |
| 1619 | sizeof(struct ocfs2_extent_rec)); |
| 1620 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1621 | } |
| 1622 | } |
| 1623 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1624 | /* |
| 1625 | * Create an empty extent record . |
| 1626 | * |
| 1627 | * l_next_free_rec may be updated. |
| 1628 | * |
| 1629 | * If an empty extent already exists do nothing. |
| 1630 | */ |
| 1631 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1632 | { |
| 1633 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1634 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1635 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1636 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1637 | if (next_free == 0) |
| 1638 | goto set_and_inc; |
| 1639 | |
| 1640 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1641 | return; |
| 1642 | |
| 1643 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1644 | "Asked to create an empty extent in a full list:\n" |
| 1645 | "count = %u, tree depth = %u", |
| 1646 | le16_to_cpu(el->l_count), |
| 1647 | le16_to_cpu(el->l_tree_depth)); |
| 1648 | |
| 1649 | ocfs2_shift_records_right(el); |
| 1650 | |
| 1651 | set_and_inc: |
| 1652 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1653 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1658 | * the lowest level tree node which contains a path to both leafs. This |
| 1659 | * resulting set of information can be used to form a complete "subtree" |
| 1660 | * |
| 1661 | * This function is passed two full paths from the dinode down to a |
| 1662 | * pair of adjacent leaves. It's task is to figure out which path |
| 1663 | * index contains the subtree root - this can be the root index itself |
| 1664 | * in a worst-case rotation. |
| 1665 | * |
| 1666 | * The array index of the subtree root is passed back. |
| 1667 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1668 | static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1669 | struct ocfs2_path *left, |
| 1670 | struct ocfs2_path *right) |
| 1671 | { |
| 1672 | int i = 0; |
| 1673 | |
| 1674 | /* |
| 1675 | * Check that the caller passed in two paths from the same tree. |
| 1676 | */ |
| 1677 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1678 | |
| 1679 | do { |
| 1680 | i++; |
| 1681 | |
| 1682 | /* |
| 1683 | * The caller didn't pass two adjacent paths. |
| 1684 | */ |
| 1685 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1686 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1687 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1688 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1689 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1690 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1691 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1692 | } while (left->p_node[i].bh->b_blocknr == |
| 1693 | right->p_node[i].bh->b_blocknr); |
| 1694 | |
| 1695 | return i - 1; |
| 1696 | } |
| 1697 | |
| 1698 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1699 | |
| 1700 | /* |
| 1701 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1702 | * |
| 1703 | * This code can be called with a cpos larger than the tree, in which |
| 1704 | * case it will return the rightmost path. |
| 1705 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1706 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1707 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1708 | path_insert_t *func, void *data) |
| 1709 | { |
| 1710 | int i, ret = 0; |
| 1711 | u32 range; |
| 1712 | u64 blkno; |
| 1713 | struct buffer_head *bh = NULL; |
| 1714 | struct ocfs2_extent_block *eb; |
| 1715 | struct ocfs2_extent_list *el; |
| 1716 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1717 | |
| 1718 | el = root_el; |
| 1719 | while (el->l_tree_depth) { |
| 1720 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1721 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1722 | "Owner %llu has empty extent list at " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1723 | "depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1724 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1725 | le16_to_cpu(el->l_tree_depth)); |
| 1726 | ret = -EROFS; |
| 1727 | goto out; |
| 1728 | |
| 1729 | } |
| 1730 | |
| 1731 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1732 | rec = &el->l_recs[i]; |
| 1733 | |
| 1734 | /* |
| 1735 | * In the case that cpos is off the allocation |
| 1736 | * tree, this should just wind up returning the |
| 1737 | * rightmost record. |
| 1738 | */ |
| 1739 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1740 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1741 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1742 | break; |
| 1743 | } |
| 1744 | |
| 1745 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1746 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1747 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1748 | "Owner %llu has bad blkno in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1749 | "at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1750 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1751 | le16_to_cpu(el->l_tree_depth), i); |
| 1752 | ret = -EROFS; |
| 1753 | goto out; |
| 1754 | } |
| 1755 | |
| 1756 | brelse(bh); |
| 1757 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1758 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1759 | if (ret) { |
| 1760 | mlog_errno(ret); |
| 1761 | goto out; |
| 1762 | } |
| 1763 | |
| 1764 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1765 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1766 | |
| 1767 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1768 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1769 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1770 | "Owner %llu has bad count in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1771 | "at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1772 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1773 | (unsigned long long)bh->b_blocknr, |
| 1774 | le16_to_cpu(el->l_next_free_rec), |
| 1775 | le16_to_cpu(el->l_count)); |
| 1776 | ret = -EROFS; |
| 1777 | goto out; |
| 1778 | } |
| 1779 | |
| 1780 | if (func) |
| 1781 | func(data, bh); |
| 1782 | } |
| 1783 | |
| 1784 | out: |
| 1785 | /* |
| 1786 | * Catch any trailing bh that the loop didn't handle. |
| 1787 | */ |
| 1788 | brelse(bh); |
| 1789 | |
| 1790 | return ret; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | * Given an initialized path (that is, it has a valid root extent |
| 1795 | * list), this function will traverse the btree in search of the path |
| 1796 | * which would contain cpos. |
| 1797 | * |
| 1798 | * The path traveled is recorded in the path structure. |
| 1799 | * |
| 1800 | * Note that this will not do any comparisons on leaf node extent |
| 1801 | * records, so it will work fine in the case that we just added a tree |
| 1802 | * branch. |
| 1803 | */ |
| 1804 | struct find_path_data { |
| 1805 | int index; |
| 1806 | struct ocfs2_path *path; |
| 1807 | }; |
| 1808 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1809 | { |
| 1810 | struct find_path_data *fp = data; |
| 1811 | |
| 1812 | get_bh(bh); |
| 1813 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1814 | fp->index++; |
| 1815 | } |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1816 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1817 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1818 | { |
| 1819 | struct find_path_data data; |
| 1820 | |
| 1821 | data.index = 1; |
| 1822 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1823 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1824 | find_path_ins, &data); |
| 1825 | } |
| 1826 | |
| 1827 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1828 | { |
| 1829 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1830 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1831 | struct buffer_head **ret = data; |
| 1832 | |
| 1833 | /* We want to retain only the leaf block. */ |
| 1834 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1835 | get_bh(bh); |
| 1836 | *ret = bh; |
| 1837 | } |
| 1838 | } |
| 1839 | /* |
| 1840 | * Find the leaf block in the tree which would contain cpos. No |
| 1841 | * checking of the actual leaf is done. |
| 1842 | * |
| 1843 | * Some paths want to call this instead of allocating a path structure |
| 1844 | * and calling ocfs2_find_path(). |
| 1845 | * |
| 1846 | * This function doesn't handle non btree extent lists. |
| 1847 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1848 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1849 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1850 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1851 | { |
| 1852 | int ret; |
| 1853 | struct buffer_head *bh = NULL; |
| 1854 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1855 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1856 | if (ret) { |
| 1857 | mlog_errno(ret); |
| 1858 | goto out; |
| 1859 | } |
| 1860 | |
| 1861 | *leaf_bh = bh; |
| 1862 | out: |
| 1863 | return ret; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1868 | * |
| 1869 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1870 | * we need to fix up the extent records above the changes to reflect |
| 1871 | * the new changes. |
| 1872 | * |
| 1873 | * left_rec: the record on the left. |
| 1874 | * left_child_el: is the child list pointed to by left_rec |
| 1875 | * right_rec: the record to the right of left_rec |
| 1876 | * right_child_el: is the child list pointed to by right_rec |
| 1877 | * |
| 1878 | * By definition, this only works on interior nodes. |
| 1879 | */ |
| 1880 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 1881 | struct ocfs2_extent_list *left_child_el, |
| 1882 | struct ocfs2_extent_rec *right_rec, |
| 1883 | struct ocfs2_extent_list *right_child_el) |
| 1884 | { |
| 1885 | u32 left_clusters, right_end; |
| 1886 | |
| 1887 | /* |
| 1888 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1889 | * the leftmost record in their child list. Their cluster |
| 1890 | * count covers the full theoretical range of their child list |
| 1891 | * - the range between their cpos and the cpos of the record |
| 1892 | * immediately to their right. |
| 1893 | */ |
| 1894 | 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] | 1895 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 1896 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1897 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1898 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1899 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1900 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1901 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1902 | |
| 1903 | /* |
| 1904 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1905 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1906 | * updating e_cpos to keep the same highest cluster count. |
| 1907 | */ |
| 1908 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1909 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1910 | |
| 1911 | right_rec->e_cpos = left_rec->e_cpos; |
| 1912 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 1913 | |
| 1914 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1915 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * Adjust the adjacent root node records involved in a |
| 1920 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 1921 | * find it's index in the root list. |
| 1922 | */ |
| 1923 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 1924 | struct ocfs2_extent_list *left_el, |
| 1925 | struct ocfs2_extent_list *right_el, |
| 1926 | u64 left_el_blkno) |
| 1927 | { |
| 1928 | int i; |
| 1929 | |
| 1930 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 1931 | le16_to_cpu(left_el->l_tree_depth)); |
| 1932 | |
| 1933 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 1934 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 1935 | break; |
| 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | * The path walking code should have never returned a root and |
| 1940 | * two paths which are not adjacent. |
| 1941 | */ |
| 1942 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 1943 | |
| 1944 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 1945 | &root_el->l_recs[i + 1], right_el); |
| 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * We've changed a leaf block (in right_path) and need to reflect that |
| 1950 | * change back up the subtree. |
| 1951 | * |
| 1952 | * This happens in multiple places: |
| 1953 | * - When we've moved an extent record from the left path leaf to the right |
| 1954 | * path leaf to make room for an empty extent in the left path leaf. |
| 1955 | * - When our insert into the right path leaf is at the leftmost edge |
| 1956 | * and requires an update of the path immediately to it's left. This |
| 1957 | * 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] | 1958 | * - When we've adjusted the last extent record in the left path leaf and the |
| 1959 | * 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] | 1960 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 1961 | static void ocfs2_complete_edge_insert(handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1962 | struct ocfs2_path *left_path, |
| 1963 | struct ocfs2_path *right_path, |
| 1964 | int subtree_index) |
| 1965 | { |
| 1966 | int ret, i, idx; |
| 1967 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 1968 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 1969 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 1970 | |
| 1971 | /* |
| 1972 | * Update the counts and position values within all the |
| 1973 | * interior nodes to reflect the leaf rotation we just did. |
| 1974 | * |
| 1975 | * The root node is handled below the loop. |
| 1976 | * |
| 1977 | * We begin the loop with right_el and left_el pointing to the |
| 1978 | * leaf lists and work our way up. |
| 1979 | * |
| 1980 | * NOTE: within this loop, left_el and right_el always refer |
| 1981 | * to the *child* lists. |
| 1982 | */ |
| 1983 | left_el = path_leaf_el(left_path); |
| 1984 | right_el = path_leaf_el(right_path); |
| 1985 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 1986 | mlog(0, "Adjust records at index %u\n", i); |
| 1987 | |
| 1988 | /* |
| 1989 | * One nice property of knowing that all of these |
| 1990 | * nodes are below the root is that we only deal with |
| 1991 | * the leftmost right node record and the rightmost |
| 1992 | * left node record. |
| 1993 | */ |
| 1994 | el = left_path->p_node[i].el; |
| 1995 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 1996 | left_rec = &el->l_recs[idx]; |
| 1997 | |
| 1998 | el = right_path->p_node[i].el; |
| 1999 | right_rec = &el->l_recs[0]; |
| 2000 | |
| 2001 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 2002 | right_el); |
| 2003 | |
| 2004 | ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2005 | if (ret) |
| 2006 | mlog_errno(ret); |
| 2007 | |
| 2008 | ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
| 2009 | if (ret) |
| 2010 | mlog_errno(ret); |
| 2011 | |
| 2012 | /* |
| 2013 | * Setup our list pointers now so that the current |
| 2014 | * parents become children in the next iteration. |
| 2015 | */ |
| 2016 | left_el = left_path->p_node[i].el; |
| 2017 | right_el = right_path->p_node[i].el; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * At the root node, adjust the two adjacent records which |
| 2022 | * begin our path to the leaves. |
| 2023 | */ |
| 2024 | |
| 2025 | el = left_path->p_node[subtree_index].el; |
| 2026 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2027 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2028 | |
| 2029 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2030 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2031 | |
| 2032 | root_bh = left_path->p_node[subtree_index].bh; |
| 2033 | |
| 2034 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 2035 | if (ret) |
| 2036 | mlog_errno(ret); |
| 2037 | } |
| 2038 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2039 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2040 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2041 | struct ocfs2_path *left_path, |
| 2042 | struct ocfs2_path *right_path, |
| 2043 | int subtree_index) |
| 2044 | { |
| 2045 | int ret, i; |
| 2046 | struct buffer_head *right_leaf_bh; |
| 2047 | struct buffer_head *left_leaf_bh = NULL; |
| 2048 | struct buffer_head *root_bh; |
| 2049 | struct ocfs2_extent_list *right_el, *left_el; |
| 2050 | struct ocfs2_extent_rec move_rec; |
| 2051 | |
| 2052 | left_leaf_bh = path_leaf_bh(left_path); |
| 2053 | left_el = path_leaf_el(left_path); |
| 2054 | |
| 2055 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2056 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2057 | "Inode %llu has non-full interior leaf node %llu" |
| 2058 | "(next free = %u)", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2059 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2060 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2061 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2062 | return -EROFS; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * This extent block may already have an empty record, so we |
| 2067 | * return early if so. |
| 2068 | */ |
| 2069 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2070 | return 0; |
| 2071 | |
| 2072 | root_bh = left_path->p_node[subtree_index].bh; |
| 2073 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2074 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2075 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2076 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2077 | if (ret) { |
| 2078 | mlog_errno(ret); |
| 2079 | goto out; |
| 2080 | } |
| 2081 | |
| 2082 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2083 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2084 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2085 | if (ret) { |
| 2086 | mlog_errno(ret); |
| 2087 | goto out; |
| 2088 | } |
| 2089 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2090 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2091 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2092 | if (ret) { |
| 2093 | mlog_errno(ret); |
| 2094 | goto out; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | right_leaf_bh = path_leaf_bh(right_path); |
| 2099 | right_el = path_leaf_el(right_path); |
| 2100 | |
| 2101 | /* This is a code error, not a disk corruption. */ |
| 2102 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2103 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2104 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2105 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2106 | |
| 2107 | ocfs2_create_empty_extent(right_el); |
| 2108 | |
| 2109 | ret = ocfs2_journal_dirty(handle, right_leaf_bh); |
| 2110 | if (ret) { |
| 2111 | mlog_errno(ret); |
| 2112 | goto out; |
| 2113 | } |
| 2114 | |
| 2115 | /* Do the copy now. */ |
| 2116 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2117 | move_rec = left_el->l_recs[i]; |
| 2118 | right_el->l_recs[0] = move_rec; |
| 2119 | |
| 2120 | /* |
| 2121 | * Clear out the record we just copied and shift everything |
| 2122 | * over, leaving an empty extent in the left leaf. |
| 2123 | * |
| 2124 | * We temporarily subtract from next_free_rec so that the |
| 2125 | * shift will lose the tail record (which is now defunct). |
| 2126 | */ |
| 2127 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2128 | ocfs2_shift_records_right(left_el); |
| 2129 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2130 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2131 | |
| 2132 | ret = ocfs2_journal_dirty(handle, left_leaf_bh); |
| 2133 | if (ret) { |
| 2134 | mlog_errno(ret); |
| 2135 | goto out; |
| 2136 | } |
| 2137 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2138 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2139 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2140 | |
| 2141 | out: |
| 2142 | return ret; |
| 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * Given a full path, determine what cpos value would return us a path |
| 2147 | * containing the leaf immediately to the left of the current one. |
| 2148 | * |
| 2149 | * Will return zero if the path passed in is already the leftmost path. |
| 2150 | */ |
| 2151 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2152 | struct ocfs2_path *path, u32 *cpos) |
| 2153 | { |
| 2154 | int i, j, ret = 0; |
| 2155 | u64 blkno; |
| 2156 | struct ocfs2_extent_list *el; |
| 2157 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2158 | BUG_ON(path->p_tree_depth == 0); |
| 2159 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2160 | *cpos = 0; |
| 2161 | |
| 2162 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2163 | |
| 2164 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2165 | i = path->p_tree_depth - 1; |
| 2166 | while (i >= 0) { |
| 2167 | el = path->p_node[i].el; |
| 2168 | |
| 2169 | /* |
| 2170 | * Find the extent record just before the one in our |
| 2171 | * path. |
| 2172 | */ |
| 2173 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2174 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2175 | if (j == 0) { |
| 2176 | if (i == 0) { |
| 2177 | /* |
| 2178 | * We've determined that the |
| 2179 | * path specified is already |
| 2180 | * the leftmost one - return a |
| 2181 | * cpos of zero. |
| 2182 | */ |
| 2183 | goto out; |
| 2184 | } |
| 2185 | /* |
| 2186 | * The leftmost record points to our |
| 2187 | * leaf - we need to travel up the |
| 2188 | * tree one level. |
| 2189 | */ |
| 2190 | goto next_node; |
| 2191 | } |
| 2192 | |
| 2193 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2194 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2195 | &el->l_recs[j - 1]); |
| 2196 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2197 | goto out; |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | /* |
| 2202 | * If we got here, we never found a valid node where |
| 2203 | * the tree indicated one should be. |
| 2204 | */ |
| 2205 | ocfs2_error(sb, |
| 2206 | "Invalid extent tree at extent block %llu\n", |
| 2207 | (unsigned long long)blkno); |
| 2208 | ret = -EROFS; |
| 2209 | goto out; |
| 2210 | |
| 2211 | next_node: |
| 2212 | blkno = path->p_node[i].bh->b_blocknr; |
| 2213 | i--; |
| 2214 | } |
| 2215 | |
| 2216 | out: |
| 2217 | return ret; |
| 2218 | } |
| 2219 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2220 | /* |
| 2221 | * Extend the transaction by enough credits to complete the rotation, |
| 2222 | * and still leave at least the original number of credits allocated |
| 2223 | * to this transaction. |
| 2224 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2225 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2226 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2227 | struct ocfs2_path *path) |
| 2228 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2229 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2230 | |
| 2231 | if (handle->h_buffer_credits < credits) |
| 2232 | return ocfs2_extend_trans(handle, credits); |
| 2233 | |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * Trap the case where we're inserting into the theoretical range past |
| 2239 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2240 | * whose cpos is less than ours into the right leaf. |
| 2241 | * |
| 2242 | * It's only necessary to look at the rightmost record of the left |
| 2243 | * leaf because the logic that calls us should ensure that the |
| 2244 | * theoretical ranges in the path components above the leaves are |
| 2245 | * correct. |
| 2246 | */ |
| 2247 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2248 | u32 insert_cpos) |
| 2249 | { |
| 2250 | struct ocfs2_extent_list *left_el; |
| 2251 | struct ocfs2_extent_rec *rec; |
| 2252 | int next_free; |
| 2253 | |
| 2254 | left_el = path_leaf_el(left_path); |
| 2255 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2256 | rec = &left_el->l_recs[next_free - 1]; |
| 2257 | |
| 2258 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2259 | return 1; |
| 2260 | return 0; |
| 2261 | } |
| 2262 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2263 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2264 | { |
| 2265 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2266 | unsigned int range; |
| 2267 | struct ocfs2_extent_rec *rec; |
| 2268 | |
| 2269 | if (next_free == 0) |
| 2270 | return 0; |
| 2271 | |
| 2272 | rec = &el->l_recs[0]; |
| 2273 | if (ocfs2_is_empty_extent(rec)) { |
| 2274 | /* Empty list. */ |
| 2275 | if (next_free == 1) |
| 2276 | return 0; |
| 2277 | rec = &el->l_recs[1]; |
| 2278 | } |
| 2279 | |
| 2280 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2281 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2282 | return 1; |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2286 | /* |
| 2287 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2288 | * |
| 2289 | * The path to the rightmost leaf should be passed in. |
| 2290 | * |
| 2291 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2292 | * |
| 2293 | * Upon succesful return from this function: |
| 2294 | * |
| 2295 | * - The 'right_path' array will contain a path to the leaf block |
| 2296 | * whose range contains e_cpos. |
| 2297 | * - That leaf block will have a single empty extent in list index 0. |
| 2298 | * - In the case that the rotation requires a post-insert update, |
| 2299 | * *ret_left_path will contain a valid path which can be passed to |
| 2300 | * ocfs2_insert_path(). |
| 2301 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2302 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2303 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2304 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2305 | u32 insert_cpos, |
| 2306 | struct ocfs2_path *right_path, |
| 2307 | struct ocfs2_path **ret_left_path) |
| 2308 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2309 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2310 | u32 cpos; |
| 2311 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2312 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2313 | |
| 2314 | *ret_left_path = NULL; |
| 2315 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2316 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2317 | if (!left_path) { |
| 2318 | ret = -ENOMEM; |
| 2319 | mlog_errno(ret); |
| 2320 | goto out; |
| 2321 | } |
| 2322 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2323 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2324 | if (ret) { |
| 2325 | mlog_errno(ret); |
| 2326 | goto out; |
| 2327 | } |
| 2328 | |
| 2329 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 2330 | |
| 2331 | /* |
| 2332 | * What we want to do here is: |
| 2333 | * |
| 2334 | * 1) Start with the rightmost path. |
| 2335 | * |
| 2336 | * 2) Determine a path to the leaf block directly to the left |
| 2337 | * of that leaf. |
| 2338 | * |
| 2339 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2340 | * which contains a path to both leaves. |
| 2341 | * |
| 2342 | * 4) Rotate the subtree. |
| 2343 | * |
| 2344 | * 5) Find the next subtree by considering the left path to be |
| 2345 | * the new right path. |
| 2346 | * |
| 2347 | * The check at the top of this while loop also accepts |
| 2348 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2349 | * value to get us the left path - insert_cpos might very well |
| 2350 | * be filling that hole. |
| 2351 | * |
| 2352 | * Stop at a cpos of '0' because we either started at the |
| 2353 | * leftmost branch (i.e., a tree with one branch and a |
| 2354 | * rotation inside of it), or we've gone as far as we can in |
| 2355 | * rotating subtrees. |
| 2356 | */ |
| 2357 | while (cpos && insert_cpos <= cpos) { |
| 2358 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 2359 | insert_cpos, cpos); |
| 2360 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2361 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2362 | if (ret) { |
| 2363 | mlog_errno(ret); |
| 2364 | goto out; |
| 2365 | } |
| 2366 | |
| 2367 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2368 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2369 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2370 | "(left path cpos %u) results in two identical " |
| 2371 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2372 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2373 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2374 | (unsigned long long) |
| 2375 | path_leaf_bh(left_path)->b_blocknr); |
| 2376 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2377 | if (split == SPLIT_NONE && |
| 2378 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2379 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2380 | |
| 2381 | /* |
| 2382 | * We've rotated the tree as much as we |
| 2383 | * should. The rest is up to |
| 2384 | * ocfs2_insert_path() to complete, after the |
| 2385 | * record insertion. We indicate this |
| 2386 | * situation by returning the left path. |
| 2387 | * |
| 2388 | * The reason we don't adjust the records here |
| 2389 | * before the record insert is that an error |
| 2390 | * later might break the rule where a parent |
| 2391 | * record e_cpos will reflect the actual |
| 2392 | * e_cpos of the 1st nonempty record of the |
| 2393 | * child list. |
| 2394 | */ |
| 2395 | *ret_left_path = left_path; |
| 2396 | goto out_ret_path; |
| 2397 | } |
| 2398 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2399 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2400 | |
| 2401 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2402 | start, |
| 2403 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 2404 | right_path->p_tree_depth); |
| 2405 | |
| 2406 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2407 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2408 | if (ret) { |
| 2409 | mlog_errno(ret); |
| 2410 | goto out; |
| 2411 | } |
| 2412 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2413 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2414 | right_path, start); |
| 2415 | if (ret) { |
| 2416 | mlog_errno(ret); |
| 2417 | goto out; |
| 2418 | } |
| 2419 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2420 | if (split != SPLIT_NONE && |
| 2421 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2422 | insert_cpos)) { |
| 2423 | /* |
| 2424 | * A rotate moves the rightmost left leaf |
| 2425 | * record over to the leftmost right leaf |
| 2426 | * slot. If we're doing an extent split |
| 2427 | * instead of a real insert, then we have to |
| 2428 | * check that the extent to be split wasn't |
| 2429 | * just moved over. If it was, then we can |
| 2430 | * exit here, passing left_path back - |
| 2431 | * ocfs2_split_extent() is smart enough to |
| 2432 | * search both leaves. |
| 2433 | */ |
| 2434 | *ret_left_path = left_path; |
| 2435 | goto out_ret_path; |
| 2436 | } |
| 2437 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2438 | /* |
| 2439 | * There is no need to re-read the next right path |
| 2440 | * as we know that it'll be our current left |
| 2441 | * path. Optimize by copying values instead. |
| 2442 | */ |
| 2443 | ocfs2_mv_path(right_path, left_path); |
| 2444 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2445 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2446 | if (ret) { |
| 2447 | mlog_errno(ret); |
| 2448 | goto out; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | out: |
| 2453 | ocfs2_free_path(left_path); |
| 2454 | |
| 2455 | out_ret_path: |
| 2456 | return ret; |
| 2457 | } |
| 2458 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2459 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2460 | struct ocfs2_extent_tree *et, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2461 | int subtree_index, struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2462 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2463 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2464 | struct ocfs2_extent_rec *rec; |
| 2465 | struct ocfs2_extent_list *el; |
| 2466 | struct ocfs2_extent_block *eb; |
| 2467 | u32 range; |
| 2468 | |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2469 | /* |
| 2470 | * In normal tree rotation process, we will never touch the |
| 2471 | * tree branch above subtree_index and ocfs2_extend_rotate_transaction |
| 2472 | * doesn't reserve the credits for them either. |
| 2473 | * |
| 2474 | * But we do have a special case here which will update the rightmost |
| 2475 | * records for all the bh in the path. |
| 2476 | * So we have to allocate extra credits and access them. |
| 2477 | */ |
| 2478 | ret = ocfs2_extend_trans(handle, |
| 2479 | handle->h_buffer_credits + subtree_index); |
| 2480 | if (ret) { |
| 2481 | mlog_errno(ret); |
| 2482 | goto out; |
| 2483 | } |
| 2484 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2485 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2486 | if (ret) { |
| 2487 | mlog_errno(ret); |
| 2488 | goto out; |
| 2489 | } |
| 2490 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2491 | /* Path should always be rightmost. */ |
| 2492 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2493 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2494 | |
| 2495 | el = &eb->h_list; |
| 2496 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2497 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2498 | rec = &el->l_recs[idx]; |
| 2499 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2500 | |
| 2501 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2502 | el = path->p_node[i].el; |
| 2503 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2504 | rec = &el->l_recs[idx]; |
| 2505 | |
| 2506 | rec->e_int_clusters = cpu_to_le32(range); |
| 2507 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2508 | |
| 2509 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2510 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2511 | out: |
| 2512 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2515 | static void ocfs2_unlink_path(handle_t *handle, |
| 2516 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2517 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2518 | struct ocfs2_path *path, int unlink_start) |
| 2519 | { |
| 2520 | int ret, i; |
| 2521 | struct ocfs2_extent_block *eb; |
| 2522 | struct ocfs2_extent_list *el; |
| 2523 | struct buffer_head *bh; |
| 2524 | |
| 2525 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2526 | bh = path->p_node[i].bh; |
| 2527 | |
| 2528 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2529 | /* |
| 2530 | * Not all nodes might have had their final count |
| 2531 | * decremented by the caller - handle this here. |
| 2532 | */ |
| 2533 | el = &eb->h_list; |
| 2534 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2535 | mlog(ML_ERROR, |
| 2536 | "Inode %llu, attempted to remove extent block " |
| 2537 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2538 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2539 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2540 | le16_to_cpu(el->l_next_free_rec)); |
| 2541 | |
| 2542 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2543 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2544 | continue; |
| 2545 | } |
| 2546 | |
| 2547 | el->l_next_free_rec = 0; |
| 2548 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2549 | |
| 2550 | ocfs2_journal_dirty(handle, bh); |
| 2551 | |
| 2552 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2553 | if (ret) |
| 2554 | mlog_errno(ret); |
| 2555 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2556 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2560 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2561 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2562 | struct ocfs2_path *left_path, |
| 2563 | struct ocfs2_path *right_path, |
| 2564 | int subtree_index, |
| 2565 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2566 | { |
| 2567 | int i; |
| 2568 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2569 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2570 | struct ocfs2_extent_list *el; |
| 2571 | struct ocfs2_extent_block *eb; |
| 2572 | |
| 2573 | el = path_leaf_el(left_path); |
| 2574 | |
| 2575 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2576 | |
| 2577 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2578 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2579 | break; |
| 2580 | |
| 2581 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2582 | |
| 2583 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2584 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2585 | |
| 2586 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2587 | eb->h_next_leaf_blk = 0; |
| 2588 | |
| 2589 | ocfs2_journal_dirty(handle, root_bh); |
| 2590 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2591 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2592 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2593 | subtree_index + 1); |
| 2594 | } |
| 2595 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2596 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
| 2597 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2598 | struct ocfs2_path *left_path, |
| 2599 | struct ocfs2_path *right_path, |
| 2600 | int subtree_index, |
| 2601 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2602 | int *deleted) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2603 | { |
| 2604 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2605 | 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] | 2606 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2607 | struct ocfs2_extent_block *eb; |
| 2608 | |
| 2609 | *deleted = 0; |
| 2610 | |
| 2611 | right_leaf_el = path_leaf_el(right_path); |
| 2612 | left_leaf_el = path_leaf_el(left_path); |
| 2613 | root_bh = left_path->p_node[subtree_index].bh; |
| 2614 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2615 | |
| 2616 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2617 | return 0; |
| 2618 | |
| 2619 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2620 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2621 | /* |
| 2622 | * It's legal for us to proceed if the right leaf is |
| 2623 | * the rightmost one and it has an empty extent. There |
| 2624 | * are two cases to handle - whether the leaf will be |
| 2625 | * empty after removal or not. If the leaf isn't empty |
| 2626 | * then just remove the empty extent up front. The |
| 2627 | * next block will handle empty leaves by flagging |
| 2628 | * them for unlink. |
| 2629 | * |
| 2630 | * Non rightmost leaves will throw -EAGAIN and the |
| 2631 | * caller can manually move the subtree and retry. |
| 2632 | */ |
| 2633 | |
| 2634 | if (eb->h_next_leaf_blk != 0ULL) |
| 2635 | return -EAGAIN; |
| 2636 | |
| 2637 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2638 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2639 | path_leaf_bh(right_path), |
| 2640 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2641 | if (ret) { |
| 2642 | mlog_errno(ret); |
| 2643 | goto out; |
| 2644 | } |
| 2645 | |
| 2646 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2647 | } else |
| 2648 | right_has_empty = 1; |
| 2649 | } |
| 2650 | |
| 2651 | if (eb->h_next_leaf_blk == 0ULL && |
| 2652 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2653 | /* |
| 2654 | * We have to update i_last_eb_blk during the meta |
| 2655 | * data delete. |
| 2656 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2657 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2658 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2659 | if (ret) { |
| 2660 | mlog_errno(ret); |
| 2661 | goto out; |
| 2662 | } |
| 2663 | |
| 2664 | del_right_subtree = 1; |
| 2665 | } |
| 2666 | |
| 2667 | /* |
| 2668 | * Getting here with an empty extent in the right path implies |
| 2669 | * that it's the rightmost path and will be deleted. |
| 2670 | */ |
| 2671 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2672 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2673 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2674 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2675 | if (ret) { |
| 2676 | mlog_errno(ret); |
| 2677 | goto out; |
| 2678 | } |
| 2679 | |
| 2680 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2681 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2682 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2683 | if (ret) { |
| 2684 | mlog_errno(ret); |
| 2685 | goto out; |
| 2686 | } |
| 2687 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2688 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2689 | left_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2690 | if (ret) { |
| 2691 | mlog_errno(ret); |
| 2692 | goto out; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | if (!right_has_empty) { |
| 2697 | /* |
| 2698 | * Only do this if we're moving a real |
| 2699 | * record. Otherwise, the action is delayed until |
| 2700 | * after removal of the right path in which case we |
| 2701 | * can do a simple shift to remove the empty extent. |
| 2702 | */ |
| 2703 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2704 | memset(&right_leaf_el->l_recs[0], 0, |
| 2705 | sizeof(struct ocfs2_extent_rec)); |
| 2706 | } |
| 2707 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2708 | /* |
| 2709 | * Move recs over to get rid of empty extent, decrease |
| 2710 | * next_free. This is allowed to remove the last |
| 2711 | * extent in our leaf (setting l_next_free_rec to |
| 2712 | * zero) - the delete code below won't care. |
| 2713 | */ |
| 2714 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2715 | } |
| 2716 | |
| 2717 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2718 | if (ret) |
| 2719 | mlog_errno(ret); |
| 2720 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 2721 | if (ret) |
| 2722 | mlog_errno(ret); |
| 2723 | |
| 2724 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2725 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2726 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2727 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2728 | left_path); |
| 2729 | if (ret) { |
| 2730 | mlog_errno(ret); |
| 2731 | goto out; |
| 2732 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2733 | |
| 2734 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2735 | 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] | 2736 | |
| 2737 | /* |
| 2738 | * Removal of the extent in the left leaf was skipped |
| 2739 | * above so we could delete the right path |
| 2740 | * 1st. |
| 2741 | */ |
| 2742 | if (right_has_empty) |
| 2743 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2744 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2745 | ret = ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2746 | if (ret) |
| 2747 | mlog_errno(ret); |
| 2748 | |
| 2749 | *deleted = 1; |
| 2750 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2751 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2752 | subtree_index); |
| 2753 | |
| 2754 | out: |
| 2755 | return ret; |
| 2756 | } |
| 2757 | |
| 2758 | /* |
| 2759 | * Given a full path, determine what cpos value would return us a path |
| 2760 | * containing the leaf immediately to the right of the current one. |
| 2761 | * |
| 2762 | * Will return zero if the path passed in is already the rightmost path. |
| 2763 | * |
| 2764 | * This looks similar, but is subtly different to |
| 2765 | * ocfs2_find_cpos_for_left_leaf(). |
| 2766 | */ |
| 2767 | static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2768 | struct ocfs2_path *path, u32 *cpos) |
| 2769 | { |
| 2770 | int i, j, ret = 0; |
| 2771 | u64 blkno; |
| 2772 | struct ocfs2_extent_list *el; |
| 2773 | |
| 2774 | *cpos = 0; |
| 2775 | |
| 2776 | if (path->p_tree_depth == 0) |
| 2777 | return 0; |
| 2778 | |
| 2779 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2780 | |
| 2781 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2782 | i = path->p_tree_depth - 1; |
| 2783 | while (i >= 0) { |
| 2784 | int next_free; |
| 2785 | |
| 2786 | el = path->p_node[i].el; |
| 2787 | |
| 2788 | /* |
| 2789 | * Find the extent record just after the one in our |
| 2790 | * path. |
| 2791 | */ |
| 2792 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2793 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2794 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2795 | if (j == (next_free - 1)) { |
| 2796 | if (i == 0) { |
| 2797 | /* |
| 2798 | * We've determined that the |
| 2799 | * path specified is already |
| 2800 | * the rightmost one - return a |
| 2801 | * cpos of zero. |
| 2802 | */ |
| 2803 | goto out; |
| 2804 | } |
| 2805 | /* |
| 2806 | * The rightmost record points to our |
| 2807 | * leaf - we need to travel up the |
| 2808 | * tree one level. |
| 2809 | */ |
| 2810 | goto next_node; |
| 2811 | } |
| 2812 | |
| 2813 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2814 | goto out; |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | /* |
| 2819 | * If we got here, we never found a valid node where |
| 2820 | * the tree indicated one should be. |
| 2821 | */ |
| 2822 | ocfs2_error(sb, |
| 2823 | "Invalid extent tree at extent block %llu\n", |
| 2824 | (unsigned long long)blkno); |
| 2825 | ret = -EROFS; |
| 2826 | goto out; |
| 2827 | |
| 2828 | next_node: |
| 2829 | blkno = path->p_node[i].bh->b_blocknr; |
| 2830 | i--; |
| 2831 | } |
| 2832 | |
| 2833 | out: |
| 2834 | return ret; |
| 2835 | } |
| 2836 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2837 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
| 2838 | struct ocfs2_extent_tree *et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2839 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2840 | { |
| 2841 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2842 | struct buffer_head *bh = path_leaf_bh(path); |
| 2843 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2844 | |
| 2845 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2846 | return 0; |
| 2847 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2848 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2849 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2850 | if (ret) { |
| 2851 | mlog_errno(ret); |
| 2852 | goto out; |
| 2853 | } |
| 2854 | |
| 2855 | ocfs2_remove_empty_extent(el); |
| 2856 | |
| 2857 | ret = ocfs2_journal_dirty(handle, bh); |
| 2858 | if (ret) |
| 2859 | mlog_errno(ret); |
| 2860 | |
| 2861 | out: |
| 2862 | return ret; |
| 2863 | } |
| 2864 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2865 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
| 2866 | struct ocfs2_extent_tree *et, |
| 2867 | int orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2868 | struct ocfs2_path *path, |
| 2869 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2870 | struct ocfs2_path **empty_extent_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2871 | { |
| 2872 | int ret, subtree_root, deleted; |
| 2873 | u32 right_cpos; |
| 2874 | struct ocfs2_path *left_path = NULL; |
| 2875 | struct ocfs2_path *right_path = NULL; |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2876 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2877 | |
| 2878 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 2879 | |
| 2880 | *empty_extent_path = NULL; |
| 2881 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2882 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2883 | if (ret) { |
| 2884 | mlog_errno(ret); |
| 2885 | goto out; |
| 2886 | } |
| 2887 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2888 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2889 | if (!left_path) { |
| 2890 | ret = -ENOMEM; |
| 2891 | mlog_errno(ret); |
| 2892 | goto out; |
| 2893 | } |
| 2894 | |
| 2895 | ocfs2_cp_path(left_path, path); |
| 2896 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2897 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2898 | if (!right_path) { |
| 2899 | ret = -ENOMEM; |
| 2900 | mlog_errno(ret); |
| 2901 | goto out; |
| 2902 | } |
| 2903 | |
| 2904 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 2905 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2906 | if (ret) { |
| 2907 | mlog_errno(ret); |
| 2908 | goto out; |
| 2909 | } |
| 2910 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2911 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2912 | right_path); |
| 2913 | |
| 2914 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2915 | subtree_root, |
| 2916 | (unsigned long long) |
| 2917 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 2918 | right_path->p_tree_depth); |
| 2919 | |
| 2920 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 2921 | orig_credits, left_path); |
| 2922 | if (ret) { |
| 2923 | mlog_errno(ret); |
| 2924 | goto out; |
| 2925 | } |
| 2926 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2927 | /* |
| 2928 | * Caller might still want to make changes to the |
| 2929 | * tree root, so re-add it to the journal here. |
| 2930 | */ |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2931 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2932 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2933 | if (ret) { |
| 2934 | mlog_errno(ret); |
| 2935 | goto out; |
| 2936 | } |
| 2937 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2938 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2939 | right_path, subtree_root, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2940 | dealloc, &deleted); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2941 | if (ret == -EAGAIN) { |
| 2942 | /* |
| 2943 | * The rotation has to temporarily stop due to |
| 2944 | * the right subtree having an empty |
| 2945 | * extent. Pass it back to the caller for a |
| 2946 | * fixup. |
| 2947 | */ |
| 2948 | *empty_extent_path = right_path; |
| 2949 | right_path = NULL; |
| 2950 | goto out; |
| 2951 | } |
| 2952 | if (ret) { |
| 2953 | mlog_errno(ret); |
| 2954 | goto out; |
| 2955 | } |
| 2956 | |
| 2957 | /* |
| 2958 | * The subtree rotate might have removed records on |
| 2959 | * the rightmost edge. If so, then rotation is |
| 2960 | * complete. |
| 2961 | */ |
| 2962 | if (deleted) |
| 2963 | break; |
| 2964 | |
| 2965 | ocfs2_mv_path(left_path, right_path); |
| 2966 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2967 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2968 | &right_cpos); |
| 2969 | if (ret) { |
| 2970 | mlog_errno(ret); |
| 2971 | goto out; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | out: |
| 2976 | ocfs2_free_path(right_path); |
| 2977 | ocfs2_free_path(left_path); |
| 2978 | |
| 2979 | return ret; |
| 2980 | } |
| 2981 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2982 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
| 2983 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2984 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2985 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2986 | { |
| 2987 | int ret, subtree_index; |
| 2988 | u32 cpos; |
| 2989 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2990 | struct ocfs2_extent_block *eb; |
| 2991 | struct ocfs2_extent_list *el; |
| 2992 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2993 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 2994 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2995 | if (ret) |
| 2996 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2997 | /* |
| 2998 | * There's two ways we handle this depending on |
| 2999 | * whether path is the only existing one. |
| 3000 | */ |
| 3001 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3002 | handle->h_buffer_credits, |
| 3003 | path); |
| 3004 | if (ret) { |
| 3005 | mlog_errno(ret); |
| 3006 | goto out; |
| 3007 | } |
| 3008 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3009 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3010 | if (ret) { |
| 3011 | mlog_errno(ret); |
| 3012 | goto out; |
| 3013 | } |
| 3014 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3015 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3016 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3017 | if (ret) { |
| 3018 | mlog_errno(ret); |
| 3019 | goto out; |
| 3020 | } |
| 3021 | |
| 3022 | if (cpos) { |
| 3023 | /* |
| 3024 | * We have a path to the left of this one - it needs |
| 3025 | * an update too. |
| 3026 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3027 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3028 | if (!left_path) { |
| 3029 | ret = -ENOMEM; |
| 3030 | mlog_errno(ret); |
| 3031 | goto out; |
| 3032 | } |
| 3033 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3034 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3035 | if (ret) { |
| 3036 | mlog_errno(ret); |
| 3037 | goto out; |
| 3038 | } |
| 3039 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3040 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3041 | if (ret) { |
| 3042 | mlog_errno(ret); |
| 3043 | goto out; |
| 3044 | } |
| 3045 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3046 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3047 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3048 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3049 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 3050 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3051 | left_path); |
| 3052 | if (ret) { |
| 3053 | mlog_errno(ret); |
| 3054 | goto out; |
| 3055 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3056 | |
| 3057 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3058 | 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] | 3059 | } else { |
| 3060 | /* |
| 3061 | * 'path' is also the leftmost path which |
| 3062 | * means it must be the only one. This gets |
| 3063 | * handled differently because we want to |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3064 | * revert the root back to having extents |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3065 | * in-line. |
| 3066 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3067 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3068 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3069 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3070 | el->l_tree_depth = 0; |
| 3071 | el->l_next_free_rec = 0; |
| 3072 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3073 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3074 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3078 | |
| 3079 | out: |
| 3080 | ocfs2_free_path(left_path); |
| 3081 | return ret; |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * Left rotation of btree records. |
| 3086 | * |
| 3087 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3088 | * rotation. We start at some non-rightmost path containing an empty |
| 3089 | * extent in the leaf block. The code works its way to the rightmost |
| 3090 | * path by rotating records to the left in every subtree. |
| 3091 | * |
| 3092 | * This is used by any code which reduces the number of extent records |
| 3093 | * in a leaf. After removal, an empty record should be placed in the |
| 3094 | * leftmost list position. |
| 3095 | * |
| 3096 | * This won't handle a length update of the rightmost path records if |
| 3097 | * the rightmost tree leaf record is removed so the caller is |
| 3098 | * responsible for detecting and correcting that. |
| 3099 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3100 | static int ocfs2_rotate_tree_left(handle_t *handle, |
| 3101 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3102 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3103 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3104 | { |
| 3105 | int ret, orig_credits = handle->h_buffer_credits; |
| 3106 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3107 | struct ocfs2_extent_block *eb; |
| 3108 | struct ocfs2_extent_list *el; |
| 3109 | |
| 3110 | el = path_leaf_el(path); |
| 3111 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3112 | return 0; |
| 3113 | |
| 3114 | if (path->p_tree_depth == 0) { |
| 3115 | rightmost_no_delete: |
| 3116 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3117 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3118 | * it up front. |
| 3119 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3120 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3121 | if (ret) |
| 3122 | mlog_errno(ret); |
| 3123 | goto out; |
| 3124 | } |
| 3125 | |
| 3126 | /* |
| 3127 | * Handle rightmost branch now. There's several cases: |
| 3128 | * 1) simple rotation leaving records in there. That's trivial. |
| 3129 | * 2) rotation requiring a branch delete - there's no more |
| 3130 | * records left. Two cases of this: |
| 3131 | * a) There are branches to the left. |
| 3132 | * b) This is also the leftmost (the only) branch. |
| 3133 | * |
| 3134 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3135 | * 2a) we need the left branch so that we can update it with the unlink |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3136 | * 2b) we need to bring the root back to inline extents. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3137 | */ |
| 3138 | |
| 3139 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3140 | el = &eb->h_list; |
| 3141 | if (eb->h_next_leaf_blk == 0) { |
| 3142 | /* |
| 3143 | * This gets a bit tricky if we're going to delete the |
| 3144 | * rightmost path. Get the other cases out of the way |
| 3145 | * 1st. |
| 3146 | */ |
| 3147 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3148 | goto rightmost_no_delete; |
| 3149 | |
| 3150 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3151 | ret = -EIO; |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3152 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3153 | "Owner %llu has empty extent block at %llu", |
| 3154 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3155 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3156 | goto out; |
| 3157 | } |
| 3158 | |
| 3159 | /* |
| 3160 | * XXX: The caller can not trust "path" any more after |
| 3161 | * this as it will have been deleted. What do we do? |
| 3162 | * |
| 3163 | * In theory the rotate-for-merge code will never get |
| 3164 | * here because it'll always ask for a rotate in a |
| 3165 | * nonempty list. |
| 3166 | */ |
| 3167 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3168 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
| 3169 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3170 | if (ret) |
| 3171 | mlog_errno(ret); |
| 3172 | goto out; |
| 3173 | } |
| 3174 | |
| 3175 | /* |
| 3176 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3177 | * and restarting from there. |
| 3178 | */ |
| 3179 | try_rotate: |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3180 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
| 3181 | dealloc, &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3182 | if (ret && ret != -EAGAIN) { |
| 3183 | mlog_errno(ret); |
| 3184 | goto out; |
| 3185 | } |
| 3186 | |
| 3187 | while (ret == -EAGAIN) { |
| 3188 | tmp_path = restart_path; |
| 3189 | restart_path = NULL; |
| 3190 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3191 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3192 | tmp_path, dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3193 | &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3194 | if (ret && ret != -EAGAIN) { |
| 3195 | mlog_errno(ret); |
| 3196 | goto out; |
| 3197 | } |
| 3198 | |
| 3199 | ocfs2_free_path(tmp_path); |
| 3200 | tmp_path = NULL; |
| 3201 | |
| 3202 | if (ret == 0) |
| 3203 | goto try_rotate; |
| 3204 | } |
| 3205 | |
| 3206 | out: |
| 3207 | ocfs2_free_path(tmp_path); |
| 3208 | ocfs2_free_path(restart_path); |
| 3209 | return ret; |
| 3210 | } |
| 3211 | |
| 3212 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3213 | int index) |
| 3214 | { |
| 3215 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3216 | unsigned int size; |
| 3217 | |
| 3218 | if (rec->e_leaf_clusters == 0) { |
| 3219 | /* |
| 3220 | * We consumed all of the merged-from record. An empty |
| 3221 | * extent cannot exist anywhere but the 1st array |
| 3222 | * position, so move things over if the merged-from |
| 3223 | * record doesn't occupy that position. |
| 3224 | * |
| 3225 | * This creates a new empty extent so the caller |
| 3226 | * should be smart enough to have removed any existing |
| 3227 | * ones. |
| 3228 | */ |
| 3229 | if (index > 0) { |
| 3230 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3231 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3232 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3233 | } |
| 3234 | |
| 3235 | /* |
| 3236 | * Always memset - the caller doesn't check whether it |
| 3237 | * created an empty extent, so there could be junk in |
| 3238 | * the other fields. |
| 3239 | */ |
| 3240 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3241 | } |
| 3242 | } |
| 3243 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3244 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3245 | struct ocfs2_path *left_path, |
| 3246 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3247 | { |
| 3248 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3249 | u32 right_cpos; |
| 3250 | struct ocfs2_path *right_path = NULL; |
| 3251 | struct ocfs2_extent_list *left_el; |
| 3252 | |
| 3253 | *ret_right_path = NULL; |
| 3254 | |
| 3255 | /* This function shouldn't be called for non-trees. */ |
| 3256 | BUG_ON(left_path->p_tree_depth == 0); |
| 3257 | |
| 3258 | left_el = path_leaf_el(left_path); |
| 3259 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3260 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3261 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3262 | left_path, &right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3263 | if (ret) { |
| 3264 | mlog_errno(ret); |
| 3265 | goto out; |
| 3266 | } |
| 3267 | |
| 3268 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3269 | BUG_ON(right_cpos == 0); |
| 3270 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3271 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3272 | if (!right_path) { |
| 3273 | ret = -ENOMEM; |
| 3274 | mlog_errno(ret); |
| 3275 | goto out; |
| 3276 | } |
| 3277 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3278 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3279 | if (ret) { |
| 3280 | mlog_errno(ret); |
| 3281 | goto out; |
| 3282 | } |
| 3283 | |
| 3284 | *ret_right_path = right_path; |
| 3285 | out: |
| 3286 | if (ret) |
| 3287 | ocfs2_free_path(right_path); |
| 3288 | return ret; |
| 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * Remove split_rec clusters from the record at index and merge them |
| 3293 | * onto the beginning of the record "next" to it. |
| 3294 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3295 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3296 | * next extent block. |
| 3297 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3298 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3299 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3300 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3301 | struct ocfs2_extent_rec *split_rec, |
| 3302 | int index) |
| 3303 | { |
| 3304 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3305 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3306 | struct ocfs2_extent_rec *left_rec; |
| 3307 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3308 | struct ocfs2_extent_list *right_el; |
| 3309 | struct ocfs2_path *right_path = NULL; |
| 3310 | int subtree_index = 0; |
| 3311 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3312 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3313 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3314 | |
| 3315 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3316 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3317 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3318 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3319 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3320 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3321 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3322 | if (ret) { |
| 3323 | mlog_errno(ret); |
| 3324 | goto out; |
| 3325 | } |
| 3326 | |
| 3327 | right_el = path_leaf_el(right_path); |
| 3328 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3329 | BUG_ON(next_free <= 0); |
| 3330 | right_rec = &right_el->l_recs[0]; |
| 3331 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3332 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3333 | right_rec = &right_el->l_recs[1]; |
| 3334 | } |
| 3335 | |
| 3336 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3337 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3338 | le32_to_cpu(right_rec->e_cpos)); |
| 3339 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3340 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3341 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3342 | |
| 3343 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3344 | handle->h_buffer_credits, |
| 3345 | right_path); |
| 3346 | if (ret) { |
| 3347 | mlog_errno(ret); |
| 3348 | goto out; |
| 3349 | } |
| 3350 | |
| 3351 | root_bh = left_path->p_node[subtree_index].bh; |
| 3352 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3353 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3354 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3355 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3356 | if (ret) { |
| 3357 | mlog_errno(ret); |
| 3358 | goto out; |
| 3359 | } |
| 3360 | |
| 3361 | for (i = subtree_index + 1; |
| 3362 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3363 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3364 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3365 | if (ret) { |
| 3366 | mlog_errno(ret); |
| 3367 | goto out; |
| 3368 | } |
| 3369 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3370 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3371 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3372 | if (ret) { |
| 3373 | mlog_errno(ret); |
| 3374 | goto out; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | } else { |
| 3379 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3380 | right_rec = &el->l_recs[index + 1]; |
| 3381 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3382 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3383 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3384 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3385 | if (ret) { |
| 3386 | mlog_errno(ret); |
| 3387 | goto out; |
| 3388 | } |
| 3389 | |
| 3390 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3391 | |
| 3392 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3393 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3394 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3395 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3396 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3397 | |
| 3398 | ocfs2_cleanup_merge(el, index); |
| 3399 | |
| 3400 | ret = ocfs2_journal_dirty(handle, bh); |
| 3401 | if (ret) |
| 3402 | mlog_errno(ret); |
| 3403 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3404 | if (right_path) { |
| 3405 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 3406 | if (ret) |
| 3407 | mlog_errno(ret); |
| 3408 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3409 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3410 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3411 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3412 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3413 | if (right_path) |
| 3414 | ocfs2_free_path(right_path); |
| 3415 | return ret; |
| 3416 | } |
| 3417 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3418 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3419 | struct ocfs2_path *right_path, |
| 3420 | struct ocfs2_path **ret_left_path) |
| 3421 | { |
| 3422 | int ret; |
| 3423 | u32 left_cpos; |
| 3424 | struct ocfs2_path *left_path = NULL; |
| 3425 | |
| 3426 | *ret_left_path = NULL; |
| 3427 | |
| 3428 | /* This function shouldn't be called for non-trees. */ |
| 3429 | BUG_ON(right_path->p_tree_depth == 0); |
| 3430 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3431 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3432 | right_path, &left_cpos); |
| 3433 | if (ret) { |
| 3434 | mlog_errno(ret); |
| 3435 | goto out; |
| 3436 | } |
| 3437 | |
| 3438 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3439 | BUG_ON(left_cpos == 0); |
| 3440 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3441 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3442 | if (!left_path) { |
| 3443 | ret = -ENOMEM; |
| 3444 | mlog_errno(ret); |
| 3445 | goto out; |
| 3446 | } |
| 3447 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3448 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3449 | if (ret) { |
| 3450 | mlog_errno(ret); |
| 3451 | goto out; |
| 3452 | } |
| 3453 | |
| 3454 | *ret_left_path = left_path; |
| 3455 | out: |
| 3456 | if (ret) |
| 3457 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3458 | return ret; |
| 3459 | } |
| 3460 | |
| 3461 | /* |
| 3462 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3463 | * onto the tail of the record "before" it. |
| 3464 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3465 | * |
| 3466 | * For index == 0, the "before" means the last record of the previous |
| 3467 | * extent block. And there is also a situation that we may need to |
| 3468 | * remove the rightmost leaf extent block in the right_path and change |
| 3469 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3470 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3471 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3472 | handle_t *handle, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3473 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3474 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3475 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3476 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3477 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3478 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3479 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3480 | struct ocfs2_extent_rec *left_rec; |
| 3481 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3482 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3483 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3484 | struct buffer_head *root_bh = NULL; |
| 3485 | struct ocfs2_path *left_path = NULL; |
| 3486 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3487 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3488 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3489 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3490 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3491 | if (index == 0) { |
| 3492 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3493 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3494 | if (ret) { |
| 3495 | mlog_errno(ret); |
| 3496 | goto out; |
| 3497 | } |
| 3498 | |
| 3499 | left_el = path_leaf_el(left_path); |
| 3500 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3501 | le16_to_cpu(left_el->l_count)); |
| 3502 | |
| 3503 | left_rec = &left_el->l_recs[ |
| 3504 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3505 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3506 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3507 | le32_to_cpu(split_rec->e_cpos)); |
| 3508 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3509 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3510 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3511 | |
| 3512 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3513 | handle->h_buffer_credits, |
| 3514 | left_path); |
| 3515 | if (ret) { |
| 3516 | mlog_errno(ret); |
| 3517 | goto out; |
| 3518 | } |
| 3519 | |
| 3520 | root_bh = left_path->p_node[subtree_index].bh; |
| 3521 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3522 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3523 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3524 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3525 | if (ret) { |
| 3526 | mlog_errno(ret); |
| 3527 | goto out; |
| 3528 | } |
| 3529 | |
| 3530 | for (i = subtree_index + 1; |
| 3531 | i < path_num_items(right_path); i++) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3532 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3533 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3534 | if (ret) { |
| 3535 | mlog_errno(ret); |
| 3536 | goto out; |
| 3537 | } |
| 3538 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3539 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3540 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3541 | if (ret) { |
| 3542 | mlog_errno(ret); |
| 3543 | goto out; |
| 3544 | } |
| 3545 | } |
| 3546 | } else { |
| 3547 | left_rec = &el->l_recs[index - 1]; |
| 3548 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3549 | has_empty_extent = 1; |
| 3550 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3551 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3552 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3553 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3554 | if (ret) { |
| 3555 | mlog_errno(ret); |
| 3556 | goto out; |
| 3557 | } |
| 3558 | |
| 3559 | if (has_empty_extent && index == 1) { |
| 3560 | /* |
| 3561 | * The easy case - we can just plop the record right in. |
| 3562 | */ |
| 3563 | *left_rec = *split_rec; |
| 3564 | |
| 3565 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3566 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3567 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3568 | |
| 3569 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3570 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3571 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3572 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3573 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3574 | |
| 3575 | ocfs2_cleanup_merge(el, index); |
| 3576 | |
| 3577 | ret = ocfs2_journal_dirty(handle, bh); |
| 3578 | if (ret) |
| 3579 | mlog_errno(ret); |
| 3580 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3581 | if (left_path) { |
| 3582 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 3583 | if (ret) |
| 3584 | mlog_errno(ret); |
| 3585 | |
| 3586 | /* |
| 3587 | * In the situation that the right_rec is empty and the extent |
| 3588 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3589 | * it and we need to delete the right extent block. |
| 3590 | */ |
| 3591 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3592 | le16_to_cpu(el->l_next_free_rec) == 1) { |
| 3593 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3594 | ret = ocfs2_remove_rightmost_path(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3595 | right_path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3596 | dealloc); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3597 | if (ret) { |
| 3598 | mlog_errno(ret); |
| 3599 | goto out; |
| 3600 | } |
| 3601 | |
| 3602 | /* Now the rightmost extent block has been deleted. |
| 3603 | * So we use the new rightmost path. |
| 3604 | */ |
| 3605 | ocfs2_mv_path(right_path, left_path); |
| 3606 | left_path = NULL; |
| 3607 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3608 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3609 | right_path, subtree_index); |
| 3610 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3611 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3612 | if (left_path) |
| 3613 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3614 | return ret; |
| 3615 | } |
| 3616 | |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3617 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
| 3618 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3619 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3620 | int split_index, |
| 3621 | struct ocfs2_extent_rec *split_rec, |
| 3622 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3623 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3624 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3625 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3626 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3627 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3628 | |
| 3629 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3630 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3631 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 3632 | /* |
| 3633 | * The merge code will need to create an empty |
| 3634 | * extent to take the place of the newly |
| 3635 | * emptied slot. Remove any pre-existing empty |
| 3636 | * extents - having more than one in a leaf is |
| 3637 | * illegal. |
| 3638 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3639 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3640 | if (ret) { |
| 3641 | mlog_errno(ret); |
| 3642 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3643 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3644 | split_index--; |
| 3645 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3649 | /* |
| 3650 | * Left-right contig implies this. |
| 3651 | */ |
| 3652 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3653 | |
| 3654 | /* |
| 3655 | * Since the leftright insert always covers the entire |
| 3656 | * extent, this call will delete the insert record |
| 3657 | * entirely, resulting in an empty extent record added to |
| 3658 | * the extent block. |
| 3659 | * |
| 3660 | * Since the adding of an empty extent shifts |
| 3661 | * everything back to the right, there's no need to |
| 3662 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3663 | * |
| 3664 | * When the split_index is zero, we need to merge it to the |
| 3665 | * prevoius extent block. It is more efficient and easier |
| 3666 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3667 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3668 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3669 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3670 | if (ret) { |
| 3671 | mlog_errno(ret); |
| 3672 | goto out; |
| 3673 | } |
| 3674 | |
| 3675 | /* |
| 3676 | * We can only get this from logic error above. |
| 3677 | */ |
| 3678 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3679 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3680 | /* The merge left us with an empty extent, remove it. */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3681 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3682 | if (ret) { |
| 3683 | mlog_errno(ret); |
| 3684 | goto out; |
| 3685 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3686 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3687 | rec = &el->l_recs[split_index]; |
| 3688 | |
| 3689 | /* |
| 3690 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3691 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3692 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3693 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
| 3694 | dealloc, split_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3695 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3696 | if (ret) { |
| 3697 | mlog_errno(ret); |
| 3698 | goto out; |
| 3699 | } |
| 3700 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3701 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3702 | /* |
| 3703 | * Error from this last rotate is not critical, so |
| 3704 | * print but don't bubble it up. |
| 3705 | */ |
| 3706 | if (ret) |
| 3707 | mlog_errno(ret); |
| 3708 | ret = 0; |
| 3709 | } else { |
| 3710 | /* |
| 3711 | * Merge a record to the left or right. |
| 3712 | * |
| 3713 | * 'contig_type' is relative to the existing record, |
| 3714 | * so for example, if we're "right contig", it's to |
| 3715 | * the record on the left (hence the left merge). |
| 3716 | */ |
| 3717 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3718 | ret = ocfs2_merge_rec_left(path, handle, et, |
| 3719 | split_rec, dealloc, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3720 | split_index); |
| 3721 | if (ret) { |
| 3722 | mlog_errno(ret); |
| 3723 | goto out; |
| 3724 | } |
| 3725 | } else { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3726 | ret = ocfs2_merge_rec_right(path, handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3727 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3728 | split_index); |
| 3729 | if (ret) { |
| 3730 | mlog_errno(ret); |
| 3731 | goto out; |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | if (ctxt->c_split_covers_rec) { |
| 3736 | /* |
| 3737 | * The merge may have left an empty extent in |
| 3738 | * our leaf. Try to rotate it away. |
| 3739 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3740 | ret = ocfs2_rotate_tree_left(handle, et, path, |
| 3741 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3742 | if (ret) |
| 3743 | mlog_errno(ret); |
| 3744 | ret = 0; |
| 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | out: |
| 3749 | return ret; |
| 3750 | } |
| 3751 | |
| 3752 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3753 | enum ocfs2_split_type split, |
| 3754 | struct ocfs2_extent_rec *rec, |
| 3755 | struct ocfs2_extent_rec *split_rec) |
| 3756 | { |
| 3757 | u64 len_blocks; |
| 3758 | |
| 3759 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3760 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3761 | |
| 3762 | if (split == SPLIT_LEFT) { |
| 3763 | /* |
| 3764 | * Region is on the left edge of the existing |
| 3765 | * record. |
| 3766 | */ |
| 3767 | le32_add_cpu(&rec->e_cpos, |
| 3768 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3769 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3770 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3771 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3772 | } else { |
| 3773 | /* |
| 3774 | * Region is on the right edge of the existing |
| 3775 | * record. |
| 3776 | */ |
| 3777 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3778 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3779 | } |
| 3780 | } |
| 3781 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3782 | /* |
| 3783 | * Do the final bits of extent record insertion at the target leaf |
| 3784 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3785 | * that the tree above has been prepared. |
| 3786 | */ |
| 3787 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec, |
| 3788 | struct ocfs2_extent_list *el, |
| 3789 | struct ocfs2_insert_type *insert, |
| 3790 | struct inode *inode) |
| 3791 | { |
| 3792 | int i = insert->ins_contig_index; |
| 3793 | unsigned int range; |
| 3794 | struct ocfs2_extent_rec *rec; |
| 3795 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3796 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3797 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3798 | if (insert->ins_split != SPLIT_NONE) { |
| 3799 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3800 | BUG_ON(i == -1); |
| 3801 | rec = &el->l_recs[i]; |
| 3802 | ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec, |
| 3803 | insert_rec); |
| 3804 | goto rotate; |
| 3805 | } |
| 3806 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3807 | /* |
| 3808 | * Contiguous insert - either left or right. |
| 3809 | */ |
| 3810 | if (insert->ins_contig != CONTIG_NONE) { |
| 3811 | rec = &el->l_recs[i]; |
| 3812 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3813 | rec->e_blkno = insert_rec->e_blkno; |
| 3814 | rec->e_cpos = insert_rec->e_cpos; |
| 3815 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3816 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3817 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3818 | return; |
| 3819 | } |
| 3820 | |
| 3821 | /* |
| 3822 | * Handle insert into an empty leaf. |
| 3823 | */ |
| 3824 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3825 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3826 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3827 | el->l_recs[0] = *insert_rec; |
| 3828 | el->l_next_free_rec = cpu_to_le16(1); |
| 3829 | return; |
| 3830 | } |
| 3831 | |
| 3832 | /* |
| 3833 | * Appending insert. |
| 3834 | */ |
| 3835 | if (insert->ins_appending == APPEND_TAIL) { |
| 3836 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3837 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3838 | range = le32_to_cpu(rec->e_cpos) |
| 3839 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3840 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3841 | |
| 3842 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3843 | le16_to_cpu(el->l_count), |
| 3844 | "inode %lu, depth %u, count %u, next free %u, " |
| 3845 | "rec.cpos %u, rec.clusters %u, " |
| 3846 | "insert.cpos %u, insert.clusters %u\n", |
| 3847 | inode->i_ino, |
| 3848 | le16_to_cpu(el->l_tree_depth), |
| 3849 | le16_to_cpu(el->l_count), |
| 3850 | le16_to_cpu(el->l_next_free_rec), |
| 3851 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3852 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3853 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3854 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3855 | i++; |
| 3856 | el->l_recs[i] = *insert_rec; |
| 3857 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3858 | return; |
| 3859 | } |
| 3860 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3861 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3862 | /* |
| 3863 | * Ok, we have to rotate. |
| 3864 | * |
| 3865 | * At this point, it is safe to assume that inserting into an |
| 3866 | * empty leaf and appending to a leaf have both been handled |
| 3867 | * above. |
| 3868 | * |
| 3869 | * This leaf needs to have space, either by the empty 1st |
| 3870 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3871 | */ |
| 3872 | ocfs2_rotate_leaf(el, insert_rec); |
| 3873 | } |
| 3874 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3875 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 3876 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3877 | struct ocfs2_path *path, |
| 3878 | struct ocfs2_extent_rec *insert_rec) |
| 3879 | { |
| 3880 | int ret, i, next_free; |
| 3881 | struct buffer_head *bh; |
| 3882 | struct ocfs2_extent_list *el; |
| 3883 | struct ocfs2_extent_rec *rec; |
| 3884 | |
| 3885 | /* |
| 3886 | * Update everything except the leaf block. |
| 3887 | */ |
| 3888 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3889 | bh = path->p_node[i].bh; |
| 3890 | el = path->p_node[i].el; |
| 3891 | |
| 3892 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3893 | if (next_free == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3894 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3895 | "Owner %llu has a bad extent list", |
| 3896 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3897 | ret = -EIO; |
| 3898 | return; |
| 3899 | } |
| 3900 | |
| 3901 | rec = &el->l_recs[next_free - 1]; |
| 3902 | |
| 3903 | rec->e_int_clusters = insert_rec->e_cpos; |
| 3904 | le32_add_cpu(&rec->e_int_clusters, |
| 3905 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 3906 | le32_add_cpu(&rec->e_int_clusters, |
| 3907 | -le32_to_cpu(rec->e_cpos)); |
| 3908 | |
| 3909 | ret = ocfs2_journal_dirty(handle, bh); |
| 3910 | if (ret) |
| 3911 | mlog_errno(ret); |
| 3912 | |
| 3913 | } |
| 3914 | } |
| 3915 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3916 | static int ocfs2_append_rec_to_path(handle_t *handle, |
| 3917 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3918 | struct ocfs2_extent_rec *insert_rec, |
| 3919 | struct ocfs2_path *right_path, |
| 3920 | struct ocfs2_path **ret_left_path) |
| 3921 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3922 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3923 | struct ocfs2_extent_list *el; |
| 3924 | struct ocfs2_path *left_path = NULL; |
| 3925 | |
| 3926 | *ret_left_path = NULL; |
| 3927 | |
| 3928 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3929 | * This shouldn't happen for non-trees. The extent rec cluster |
| 3930 | * count manipulation below only works for interior nodes. |
| 3931 | */ |
| 3932 | BUG_ON(right_path->p_tree_depth == 0); |
| 3933 | |
| 3934 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3935 | * If our appending insert is at the leftmost edge of a leaf, |
| 3936 | * then we might need to update the rightmost records of the |
| 3937 | * neighboring path. |
| 3938 | */ |
| 3939 | el = path_leaf_el(right_path); |
| 3940 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3941 | if (next_free == 0 || |
| 3942 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3943 | u32 left_cpos; |
| 3944 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3945 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3946 | right_path, &left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3947 | if (ret) { |
| 3948 | mlog_errno(ret); |
| 3949 | goto out; |
| 3950 | } |
| 3951 | |
| 3952 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 3953 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 3954 | left_cpos); |
| 3955 | |
| 3956 | /* |
| 3957 | * No need to worry if the append is already in the |
| 3958 | * leftmost leaf. |
| 3959 | */ |
| 3960 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3961 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3962 | if (!left_path) { |
| 3963 | ret = -ENOMEM; |
| 3964 | mlog_errno(ret); |
| 3965 | goto out; |
| 3966 | } |
| 3967 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3968 | ret = ocfs2_find_path(et->et_ci, left_path, |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3969 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3970 | if (ret) { |
| 3971 | mlog_errno(ret); |
| 3972 | goto out; |
| 3973 | } |
| 3974 | |
| 3975 | /* |
| 3976 | * ocfs2_insert_path() will pass the left_path to the |
| 3977 | * journal for us. |
| 3978 | */ |
| 3979 | } |
| 3980 | } |
| 3981 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3982 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3983 | if (ret) { |
| 3984 | mlog_errno(ret); |
| 3985 | goto out; |
| 3986 | } |
| 3987 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 3988 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3989 | |
| 3990 | *ret_left_path = left_path; |
| 3991 | ret = 0; |
| 3992 | out: |
| 3993 | if (ret != 0) |
| 3994 | ocfs2_free_path(left_path); |
| 3995 | |
| 3996 | return ret; |
| 3997 | } |
| 3998 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3999 | static void ocfs2_split_record(struct inode *inode, |
| 4000 | struct ocfs2_path *left_path, |
| 4001 | struct ocfs2_path *right_path, |
| 4002 | struct ocfs2_extent_rec *split_rec, |
| 4003 | enum ocfs2_split_type split) |
| 4004 | { |
| 4005 | int index; |
| 4006 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4007 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4008 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4009 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4010 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4011 | if (left_path) |
| 4012 | left_el = path_leaf_el(left_path); |
| 4013 | |
| 4014 | el = right_el; |
| 4015 | insert_el = right_el; |
| 4016 | index = ocfs2_search_extent_list(el, cpos); |
| 4017 | if (index != -1) { |
| 4018 | if (index == 0 && left_path) { |
| 4019 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4020 | |
| 4021 | /* |
| 4022 | * This typically means that the record |
| 4023 | * started in the left path but moved to the |
| 4024 | * right as a result of rotation. We either |
| 4025 | * move the existing record to the left, or we |
| 4026 | * do the later insert there. |
| 4027 | * |
| 4028 | * In this case, the left path should always |
| 4029 | * exist as the rotate code will have passed |
| 4030 | * it back for a post-insert update. |
| 4031 | */ |
| 4032 | |
| 4033 | if (split == SPLIT_LEFT) { |
| 4034 | /* |
| 4035 | * It's a left split. Since we know |
| 4036 | * that the rotate code gave us an |
| 4037 | * empty extent in the left path, we |
| 4038 | * can just do the insert there. |
| 4039 | */ |
| 4040 | insert_el = left_el; |
| 4041 | } else { |
| 4042 | /* |
| 4043 | * Right split - we have to move the |
| 4044 | * existing record over to the left |
| 4045 | * leaf. The insert will be into the |
| 4046 | * newly created empty extent in the |
| 4047 | * right leaf. |
| 4048 | */ |
| 4049 | tmprec = &right_el->l_recs[index]; |
| 4050 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4051 | el = left_el; |
| 4052 | |
| 4053 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4054 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4055 | BUG_ON(index == -1); |
| 4056 | } |
| 4057 | } |
| 4058 | } else { |
| 4059 | BUG_ON(!left_path); |
| 4060 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4061 | /* |
| 4062 | * Left path is easy - we can just allow the insert to |
| 4063 | * happen. |
| 4064 | */ |
| 4065 | el = left_el; |
| 4066 | insert_el = left_el; |
| 4067 | index = ocfs2_search_extent_list(el, cpos); |
| 4068 | BUG_ON(index == -1); |
| 4069 | } |
| 4070 | |
| 4071 | rec = &el->l_recs[index]; |
| 4072 | ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec); |
| 4073 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4074 | } |
| 4075 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4076 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4077 | * This function only does inserts on an allocation b-tree. For tree |
| 4078 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4079 | * |
| 4080 | * right_path is the path we want to do the actual insert |
| 4081 | * in. left_path should only be passed in if we need to update that |
| 4082 | * portion of the tree after an edge insert. |
| 4083 | */ |
| 4084 | static int ocfs2_insert_path(struct inode *inode, |
| 4085 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4086 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4087 | struct ocfs2_path *left_path, |
| 4088 | struct ocfs2_path *right_path, |
| 4089 | struct ocfs2_extent_rec *insert_rec, |
| 4090 | struct ocfs2_insert_type *insert) |
| 4091 | { |
| 4092 | int ret, subtree_index; |
| 4093 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4094 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4095 | if (left_path) { |
| 4096 | int credits = handle->h_buffer_credits; |
| 4097 | |
| 4098 | /* |
| 4099 | * There's a chance that left_path got passed back to |
| 4100 | * us without being accounted for in the |
| 4101 | * journal. Extend our transaction here to be sure we |
| 4102 | * can change those blocks. |
| 4103 | */ |
| 4104 | credits += left_path->p_tree_depth; |
| 4105 | |
| 4106 | ret = ocfs2_extend_trans(handle, credits); |
| 4107 | if (ret < 0) { |
| 4108 | mlog_errno(ret); |
| 4109 | goto out; |
| 4110 | } |
| 4111 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4112 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4113 | if (ret < 0) { |
| 4114 | mlog_errno(ret); |
| 4115 | goto out; |
| 4116 | } |
| 4117 | } |
| 4118 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4119 | /* |
| 4120 | * Pass both paths to the journal. The majority of inserts |
| 4121 | * will be touching all components anyway. |
| 4122 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4123 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4124 | if (ret < 0) { |
| 4125 | mlog_errno(ret); |
| 4126 | goto out; |
| 4127 | } |
| 4128 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4129 | if (insert->ins_split != SPLIT_NONE) { |
| 4130 | /* |
| 4131 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4132 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4133 | * function sort it all out. |
| 4134 | */ |
| 4135 | ocfs2_split_record(inode, left_path, right_path, |
| 4136 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4137 | |
| 4138 | /* |
| 4139 | * Split might have modified either leaf and we don't |
| 4140 | * have a guarantee that the later edge insert will |
| 4141 | * dirty this for us. |
| 4142 | */ |
| 4143 | if (left_path) |
| 4144 | ret = ocfs2_journal_dirty(handle, |
| 4145 | path_leaf_bh(left_path)); |
| 4146 | if (ret) |
| 4147 | mlog_errno(ret); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4148 | } else |
| 4149 | ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path), |
| 4150 | insert, inode); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4151 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4152 | ret = ocfs2_journal_dirty(handle, leaf_bh); |
| 4153 | if (ret) |
| 4154 | mlog_errno(ret); |
| 4155 | |
| 4156 | if (left_path) { |
| 4157 | /* |
| 4158 | * The rotate code has indicated that we need to fix |
| 4159 | * up portions of the tree after the insert. |
| 4160 | * |
| 4161 | * XXX: Should we extend the transaction here? |
| 4162 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4163 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4164 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4165 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4166 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | ret = 0; |
| 4170 | out: |
| 4171 | return ret; |
| 4172 | } |
| 4173 | |
| 4174 | static int ocfs2_do_insert_extent(struct inode *inode, |
| 4175 | handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4176 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4177 | struct ocfs2_extent_rec *insert_rec, |
| 4178 | struct ocfs2_insert_type *type) |
| 4179 | { |
| 4180 | int ret, rotate = 0; |
| 4181 | u32 cpos; |
| 4182 | struct ocfs2_path *right_path = NULL; |
| 4183 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4184 | struct ocfs2_extent_list *el; |
| 4185 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4186 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4187 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4188 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4189 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4190 | if (ret) { |
| 4191 | mlog_errno(ret); |
| 4192 | goto out; |
| 4193 | } |
| 4194 | |
| 4195 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 4196 | ocfs2_insert_at_leaf(insert_rec, el, type, inode); |
| 4197 | goto out_update_clusters; |
| 4198 | } |
| 4199 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4200 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4201 | if (!right_path) { |
| 4202 | ret = -ENOMEM; |
| 4203 | mlog_errno(ret); |
| 4204 | goto out; |
| 4205 | } |
| 4206 | |
| 4207 | /* |
| 4208 | * Determine the path to start with. Rotations need the |
| 4209 | * rightmost path, everything else can go directly to the |
| 4210 | * target leaf. |
| 4211 | */ |
| 4212 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4213 | if (type->ins_appending == APPEND_NONE && |
| 4214 | type->ins_contig == CONTIG_NONE) { |
| 4215 | rotate = 1; |
| 4216 | cpos = UINT_MAX; |
| 4217 | } |
| 4218 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4219 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4220 | if (ret) { |
| 4221 | mlog_errno(ret); |
| 4222 | goto out; |
| 4223 | } |
| 4224 | |
| 4225 | /* |
| 4226 | * Rotations and appends need special treatment - they modify |
| 4227 | * parts of the tree's above them. |
| 4228 | * |
| 4229 | * Both might pass back a path immediate to the left of the |
| 4230 | * one being inserted to. This will be cause |
| 4231 | * ocfs2_insert_path() to modify the rightmost records of |
| 4232 | * left_path to account for an edge insert. |
| 4233 | * |
| 4234 | * XXX: When modifying this code, keep in mind that an insert |
| 4235 | * can wind up skipping both of these two special cases... |
| 4236 | */ |
| 4237 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4238 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4239 | le32_to_cpu(insert_rec->e_cpos), |
| 4240 | right_path, &left_path); |
| 4241 | if (ret) { |
| 4242 | mlog_errno(ret); |
| 4243 | goto out; |
| 4244 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4245 | |
| 4246 | /* |
| 4247 | * ocfs2_rotate_tree_right() might have extended the |
| 4248 | * transaction without re-journaling our tree root. |
| 4249 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4250 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4251 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4252 | if (ret) { |
| 4253 | mlog_errno(ret); |
| 4254 | goto out; |
| 4255 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4256 | } else if (type->ins_appending == APPEND_TAIL |
| 4257 | && type->ins_contig != CONTIG_LEFT) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 4258 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4259 | right_path, &left_path); |
| 4260 | if (ret) { |
| 4261 | mlog_errno(ret); |
| 4262 | goto out; |
| 4263 | } |
| 4264 | } |
| 4265 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4266 | ret = ocfs2_insert_path(inode, handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4267 | insert_rec, type); |
| 4268 | if (ret) { |
| 4269 | mlog_errno(ret); |
| 4270 | goto out; |
| 4271 | } |
| 4272 | |
| 4273 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4274 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4275 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4276 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4277 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4278 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4279 | if (ret) |
| 4280 | mlog_errno(ret); |
| 4281 | |
| 4282 | out: |
| 4283 | ocfs2_free_path(left_path); |
| 4284 | ocfs2_free_path(right_path); |
| 4285 | |
| 4286 | return ret; |
| 4287 | } |
| 4288 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4289 | static enum ocfs2_contig_type |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4290 | ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4291 | struct ocfs2_extent_list *el, int index, |
| 4292 | struct ocfs2_extent_rec *split_rec) |
| 4293 | { |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4294 | int status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4295 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4296 | u32 left_cpos, right_cpos; |
| 4297 | struct ocfs2_extent_rec *rec = NULL; |
| 4298 | struct ocfs2_extent_list *new_el; |
| 4299 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4300 | struct buffer_head *bh; |
| 4301 | struct ocfs2_extent_block *eb; |
| 4302 | |
| 4303 | if (index > 0) { |
| 4304 | rec = &el->l_recs[index - 1]; |
| 4305 | } else if (path->p_tree_depth > 0) { |
| 4306 | status = ocfs2_find_cpos_for_left_leaf(inode->i_sb, |
| 4307 | path, &left_cpos); |
| 4308 | if (status) |
| 4309 | goto out; |
| 4310 | |
| 4311 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4312 | left_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4313 | if (!left_path) |
| 4314 | goto out; |
| 4315 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4316 | status = ocfs2_find_path(INODE_CACHE(inode), |
| 4317 | left_path, left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4318 | if (status) |
| 4319 | goto out; |
| 4320 | |
| 4321 | new_el = path_leaf_el(left_path); |
| 4322 | |
| 4323 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4324 | le16_to_cpu(new_el->l_count)) { |
| 4325 | bh = path_leaf_bh(left_path); |
| 4326 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4327 | ocfs2_error(inode->i_sb, |
| 4328 | "Extent block #%llu has an " |
| 4329 | "invalid l_next_free_rec of " |
| 4330 | "%d. It should have " |
| 4331 | "matched the l_count of %d", |
| 4332 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4333 | le16_to_cpu(new_el->l_next_free_rec), |
| 4334 | le16_to_cpu(new_el->l_count)); |
| 4335 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4336 | goto out; |
| 4337 | } |
| 4338 | rec = &new_el->l_recs[ |
| 4339 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4340 | } |
| 4341 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4342 | |
| 4343 | /* |
| 4344 | * We're careful to check for an empty extent record here - |
| 4345 | * the merge code will know what to do if it sees one. |
| 4346 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4347 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4348 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4349 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4350 | ret = CONTIG_RIGHT; |
| 4351 | } else { |
| 4352 | ret = ocfs2_extent_contig(inode, rec, split_rec); |
| 4353 | } |
| 4354 | } |
| 4355 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4356 | rec = NULL; |
| 4357 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4358 | rec = &el->l_recs[index + 1]; |
| 4359 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4360 | path->p_tree_depth > 0) { |
| 4361 | status = ocfs2_find_cpos_for_right_leaf(inode->i_sb, |
| 4362 | path, &right_cpos); |
| 4363 | if (status) |
| 4364 | goto out; |
| 4365 | |
| 4366 | if (right_cpos == 0) |
| 4367 | goto out; |
| 4368 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4369 | right_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4370 | if (!right_path) |
| 4371 | goto out; |
| 4372 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4373 | status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4374 | if (status) |
| 4375 | goto out; |
| 4376 | |
| 4377 | new_el = path_leaf_el(right_path); |
| 4378 | rec = &new_el->l_recs[0]; |
| 4379 | if (ocfs2_is_empty_extent(rec)) { |
| 4380 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4381 | bh = path_leaf_bh(right_path); |
| 4382 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4383 | ocfs2_error(inode->i_sb, |
| 4384 | "Extent block #%llu has an " |
| 4385 | "invalid l_next_free_rec of %d", |
| 4386 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4387 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4388 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4389 | goto out; |
| 4390 | } |
| 4391 | rec = &new_el->l_recs[1]; |
| 4392 | } |
| 4393 | } |
| 4394 | |
| 4395 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4396 | enum ocfs2_contig_type contig_type; |
| 4397 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4398 | contig_type = ocfs2_extent_contig(inode, rec, split_rec); |
| 4399 | |
| 4400 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4401 | ret = CONTIG_LEFTRIGHT; |
| 4402 | else if (ret == CONTIG_NONE) |
| 4403 | ret = contig_type; |
| 4404 | } |
| 4405 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4406 | out: |
| 4407 | if (left_path) |
| 4408 | ocfs2_free_path(left_path); |
| 4409 | if (right_path) |
| 4410 | ocfs2_free_path(right_path); |
| 4411 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4412 | return ret; |
| 4413 | } |
| 4414 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4415 | static void ocfs2_figure_contig_type(struct inode *inode, |
| 4416 | struct ocfs2_insert_type *insert, |
| 4417 | struct ocfs2_extent_list *el, |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4418 | struct ocfs2_extent_rec *insert_rec, |
| 4419 | struct ocfs2_extent_tree *et) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4420 | { |
| 4421 | int i; |
| 4422 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4423 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4424 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4425 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4426 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 4427 | contig_type = ocfs2_extent_contig(inode, &el->l_recs[i], |
| 4428 | insert_rec); |
| 4429 | if (contig_type != CONTIG_NONE) { |
| 4430 | insert->ins_contig_index = i; |
| 4431 | break; |
| 4432 | } |
| 4433 | } |
| 4434 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4435 | |
| 4436 | if (insert->ins_contig != CONTIG_NONE) { |
| 4437 | struct ocfs2_extent_rec *rec = |
| 4438 | &el->l_recs[insert->ins_contig_index]; |
| 4439 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4440 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4441 | |
| 4442 | /* |
| 4443 | * Caller might want us to limit the size of extents, don't |
| 4444 | * calculate contiguousness if we might exceed that limit. |
| 4445 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4446 | if (et->et_max_leaf_clusters && |
| 4447 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4448 | insert->ins_contig = CONTIG_NONE; |
| 4449 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | /* |
| 4453 | * This should only be called against the righmost leaf extent list. |
| 4454 | * |
| 4455 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4456 | * insert at the tail of the rightmost leaf. |
| 4457 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4458 | * This should also work against the root extent list for tree's with 0 |
| 4459 | * 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] | 4460 | * then the logic here makes sense. |
| 4461 | */ |
| 4462 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4463 | struct ocfs2_extent_list *el, |
| 4464 | struct ocfs2_extent_rec *insert_rec) |
| 4465 | { |
| 4466 | int i; |
| 4467 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4468 | struct ocfs2_extent_rec *rec; |
| 4469 | |
| 4470 | insert->ins_appending = APPEND_NONE; |
| 4471 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4472 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4473 | |
| 4474 | if (!el->l_next_free_rec) |
| 4475 | goto set_tail_append; |
| 4476 | |
| 4477 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4478 | /* Were all records empty? */ |
| 4479 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4480 | goto set_tail_append; |
| 4481 | } |
| 4482 | |
| 4483 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4484 | rec = &el->l_recs[i]; |
| 4485 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4486 | if (cpos >= |
| 4487 | (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] | 4488 | goto set_tail_append; |
| 4489 | |
| 4490 | return; |
| 4491 | |
| 4492 | set_tail_append: |
| 4493 | insert->ins_appending = APPEND_TAIL; |
| 4494 | } |
| 4495 | |
| 4496 | /* |
| 4497 | * Helper function called at the begining of an insert. |
| 4498 | * |
| 4499 | * This computes a few things that are commonly used in the process of |
| 4500 | * inserting into the btree: |
| 4501 | * - Whether the new extent is contiguous with an existing one. |
| 4502 | * - The current tree depth. |
| 4503 | * - Whether the insert is an appending one. |
| 4504 | * - The total # of free records in the tree. |
| 4505 | * |
| 4506 | * All of the information is stored on the ocfs2_insert_type |
| 4507 | * structure. |
| 4508 | */ |
| 4509 | static int ocfs2_figure_insert_type(struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4510 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4511 | struct buffer_head **last_eb_bh, |
| 4512 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4513 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4514 | struct ocfs2_insert_type *insert) |
| 4515 | { |
| 4516 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4517 | struct ocfs2_extent_block *eb; |
| 4518 | struct ocfs2_extent_list *el; |
| 4519 | struct ocfs2_path *path = NULL; |
| 4520 | struct buffer_head *bh = NULL; |
| 4521 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4522 | insert->ins_split = SPLIT_NONE; |
| 4523 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4524 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4525 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4526 | |
| 4527 | if (el->l_tree_depth) { |
| 4528 | /* |
| 4529 | * If we have tree depth, we read in the |
| 4530 | * rightmost extent block ahead of time as |
| 4531 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4532 | * may want it later. |
| 4533 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4534 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4535 | ocfs2_et_get_last_eb_blk(et), |
| 4536 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4537 | if (ret) { |
| 4538 | mlog_exit(ret); |
| 4539 | goto out; |
| 4540 | } |
| 4541 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4542 | el = &eb->h_list; |
| 4543 | } |
| 4544 | |
| 4545 | /* |
| 4546 | * Unless we have a contiguous insert, we'll need to know if |
| 4547 | * there is room left in our allocation tree for another |
| 4548 | * extent record. |
| 4549 | * |
| 4550 | * XXX: This test is simplistic, we can search for empty |
| 4551 | * extent records too. |
| 4552 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4553 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4554 | le16_to_cpu(el->l_next_free_rec); |
| 4555 | |
| 4556 | if (!insert->ins_tree_depth) { |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4557 | ocfs2_figure_contig_type(inode, insert, el, insert_rec, et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4558 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4559 | return 0; |
| 4560 | } |
| 4561 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4562 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4563 | if (!path) { |
| 4564 | ret = -ENOMEM; |
| 4565 | mlog_errno(ret); |
| 4566 | goto out; |
| 4567 | } |
| 4568 | |
| 4569 | /* |
| 4570 | * In the case that we're inserting past what the tree |
| 4571 | * currently accounts for, ocfs2_find_path() will return for |
| 4572 | * us the rightmost tree path. This is accounted for below in |
| 4573 | * the appending code. |
| 4574 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4575 | 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] | 4576 | if (ret) { |
| 4577 | mlog_errno(ret); |
| 4578 | goto out; |
| 4579 | } |
| 4580 | |
| 4581 | el = path_leaf_el(path); |
| 4582 | |
| 4583 | /* |
| 4584 | * Now that we have the path, there's two things we want to determine: |
| 4585 | * 1) Contiguousness (also set contig_index if this is so) |
| 4586 | * |
| 4587 | * 2) Are we doing an append? We can trivially break this up |
| 4588 | * into two types of appends: simple record append, or a |
| 4589 | * rotate inside the tail leaf. |
| 4590 | */ |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4591 | ocfs2_figure_contig_type(inode, insert, el, insert_rec, et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4592 | |
| 4593 | /* |
| 4594 | * The insert code isn't quite ready to deal with all cases of |
| 4595 | * left contiguousness. Specifically, if it's an insert into |
| 4596 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4597 | * 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] | 4598 | * left. For now, just catch that case and fool the layers |
| 4599 | * above us. This works just fine for tree_depth == 0, which |
| 4600 | * is why we allow that above. |
| 4601 | */ |
| 4602 | if (insert->ins_contig == CONTIG_LEFT && |
| 4603 | insert->ins_contig_index == 0) |
| 4604 | insert->ins_contig = CONTIG_NONE; |
| 4605 | |
| 4606 | /* |
| 4607 | * Ok, so we can simply compare against last_eb to figure out |
| 4608 | * whether the path doesn't exist. This will only happen in |
| 4609 | * the case that we're doing a tail append, so maybe we can |
| 4610 | * take advantage of that information somehow. |
| 4611 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4612 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4613 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4614 | /* |
| 4615 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4616 | * tree path. This might be an appending insert. There are |
| 4617 | * two cases: |
| 4618 | * 1) We're doing a true append at the tail: |
| 4619 | * -This might even be off the end of the leaf |
| 4620 | * 2) We're "appending" by rotating in the tail |
| 4621 | */ |
| 4622 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4623 | } |
| 4624 | |
| 4625 | out: |
| 4626 | ocfs2_free_path(path); |
| 4627 | |
| 4628 | if (ret == 0) |
| 4629 | *last_eb_bh = bh; |
| 4630 | else |
| 4631 | brelse(bh); |
| 4632 | return ret; |
| 4633 | } |
| 4634 | |
| 4635 | /* |
| 4636 | * Insert an extent into an inode btree. |
| 4637 | * |
| 4638 | * The caller needs to update fe->i_clusters |
| 4639 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4640 | int ocfs2_insert_extent(struct ocfs2_super *osb, |
| 4641 | handle_t *handle, |
| 4642 | struct inode *inode, |
| 4643 | struct ocfs2_extent_tree *et, |
| 4644 | u32 cpos, |
| 4645 | u64 start_blk, |
| 4646 | u32 new_clusters, |
| 4647 | u8 flags, |
| 4648 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4649 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4650 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4651 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4652 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4653 | struct ocfs2_insert_type insert = {0, }; |
| 4654 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4655 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4656 | mlog(0, "add %u clusters at position %u to inode %llu\n", |
| 4657 | new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4658 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4659 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4660 | rec.e_cpos = cpu_to_le32(cpos); |
| 4661 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4662 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4663 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4664 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4665 | if (status) { |
| 4666 | mlog_errno(status); |
| 4667 | goto bail; |
| 4668 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4669 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4670 | status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4671 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4672 | if (status < 0) { |
| 4673 | mlog_errno(status); |
| 4674 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4675 | } |
| 4676 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4677 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 4678 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 4679 | "Insert.tree_depth: %d\n", |
| 4680 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4681 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4682 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4683 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 4684 | status = ocfs2_grow_tree(handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4685 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4686 | meta_ac); |
| 4687 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4688 | mlog_errno(status); |
| 4689 | goto bail; |
| 4690 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4691 | } |
| 4692 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4693 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4694 | status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4695 | if (status < 0) |
| 4696 | mlog_errno(status); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4697 | else if (et->et_ops == &ocfs2_dinode_et_ops) |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 4698 | ocfs2_extent_map_insert_rec(inode, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4699 | |
| 4700 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4701 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4702 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4703 | mlog_exit(status); |
| 4704 | return status; |
| 4705 | } |
| 4706 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4707 | /* |
| 4708 | * Allcate and add clusters into the extent b-tree. |
| 4709 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4710 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4711 | * it is not limited to the file storage. Any extent tree can use this |
| 4712 | * function if it implements the proper ocfs2_extent_tree. |
| 4713 | */ |
| 4714 | int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, |
| 4715 | struct inode *inode, |
| 4716 | u32 *logical_offset, |
| 4717 | u32 clusters_to_add, |
| 4718 | int mark_unwritten, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4719 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4720 | handle_t *handle, |
| 4721 | struct ocfs2_alloc_context *data_ac, |
| 4722 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4723 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4724 | { |
| 4725 | int status = 0; |
| 4726 | int free_extents; |
| 4727 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4728 | u32 bit_off, num_bits; |
| 4729 | u64 block; |
| 4730 | u8 flags = 0; |
| 4731 | |
| 4732 | BUG_ON(!clusters_to_add); |
| 4733 | |
| 4734 | if (mark_unwritten) |
| 4735 | flags = OCFS2_EXT_UNWRITTEN; |
| 4736 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4737 | free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4738 | if (free_extents < 0) { |
| 4739 | status = free_extents; |
| 4740 | mlog_errno(status); |
| 4741 | goto leave; |
| 4742 | } |
| 4743 | |
| 4744 | /* there are two cases which could cause us to EAGAIN in the |
| 4745 | * we-need-more-metadata case: |
| 4746 | * 1) we haven't reserved *any* |
| 4747 | * 2) we are so fragmented, we've needed to add metadata too |
| 4748 | * many times. */ |
| 4749 | if (!free_extents && !meta_ac) { |
| 4750 | mlog(0, "we haven't reserved any metadata!\n"); |
| 4751 | status = -EAGAIN; |
| 4752 | reason = RESTART_META; |
| 4753 | goto leave; |
| 4754 | } else if ((!free_extents) |
| 4755 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4756 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4757 | mlog(0, "filesystem is really fragmented...\n"); |
| 4758 | status = -EAGAIN; |
| 4759 | reason = RESTART_META; |
| 4760 | goto leave; |
| 4761 | } |
| 4762 | |
| 4763 | status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 4764 | clusters_to_add, &bit_off, &num_bits); |
| 4765 | if (status < 0) { |
| 4766 | if (status != -ENOSPC) |
| 4767 | mlog_errno(status); |
| 4768 | goto leave; |
| 4769 | } |
| 4770 | |
| 4771 | BUG_ON(num_bits > clusters_to_add); |
| 4772 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4773 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4774 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4775 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4776 | if (status < 0) { |
| 4777 | mlog_errno(status); |
| 4778 | goto leave; |
| 4779 | } |
| 4780 | |
| 4781 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
| 4782 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
| 4783 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4784 | status = ocfs2_insert_extent(osb, handle, inode, et, |
| 4785 | *logical_offset, block, |
| 4786 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4787 | if (status < 0) { |
| 4788 | mlog_errno(status); |
| 4789 | goto leave; |
| 4790 | } |
| 4791 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4792 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4793 | if (status < 0) { |
| 4794 | mlog_errno(status); |
| 4795 | goto leave; |
| 4796 | } |
| 4797 | |
| 4798 | clusters_to_add -= num_bits; |
| 4799 | *logical_offset += num_bits; |
| 4800 | |
| 4801 | if (clusters_to_add) { |
| 4802 | mlog(0, "need to alloc once more, wanted = %u\n", |
| 4803 | clusters_to_add); |
| 4804 | status = -EAGAIN; |
| 4805 | reason = RESTART_TRANS; |
| 4806 | } |
| 4807 | |
| 4808 | leave: |
| 4809 | mlog_exit(status); |
| 4810 | if (reason_ret) |
| 4811 | *reason_ret = reason; |
| 4812 | return status; |
| 4813 | } |
| 4814 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4815 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4816 | struct ocfs2_extent_rec *split_rec, |
| 4817 | u32 cpos, |
| 4818 | struct ocfs2_extent_rec *rec) |
| 4819 | { |
| 4820 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4821 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4822 | |
| 4823 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4824 | |
| 4825 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4826 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4827 | |
| 4828 | split_rec->e_blkno = rec->e_blkno; |
| 4829 | le64_add_cpu(&split_rec->e_blkno, |
| 4830 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4831 | |
| 4832 | split_rec->e_flags = rec->e_flags; |
| 4833 | } |
| 4834 | |
| 4835 | static int ocfs2_split_and_insert(struct inode *inode, |
| 4836 | handle_t *handle, |
| 4837 | struct ocfs2_path *path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4838 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4839 | struct buffer_head **last_eb_bh, |
| 4840 | int split_index, |
| 4841 | struct ocfs2_extent_rec *orig_split_rec, |
| 4842 | struct ocfs2_alloc_context *meta_ac) |
| 4843 | { |
| 4844 | int ret = 0, depth; |
| 4845 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4846 | struct ocfs2_extent_rec tmprec; |
| 4847 | struct ocfs2_extent_list *rightmost_el; |
| 4848 | struct ocfs2_extent_rec rec; |
| 4849 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4850 | struct ocfs2_insert_type insert; |
| 4851 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4852 | |
| 4853 | leftright: |
| 4854 | /* |
| 4855 | * Store a copy of the record on the stack - it might move |
| 4856 | * around as the tree is manipulated below. |
| 4857 | */ |
| 4858 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4859 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4860 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4861 | |
| 4862 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4863 | if (depth) { |
| 4864 | BUG_ON(!(*last_eb_bh)); |
| 4865 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4866 | rightmost_el = &eb->h_list; |
| 4867 | } |
| 4868 | |
| 4869 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4870 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 4871 | ret = ocfs2_grow_tree(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4872 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4873 | if (ret) { |
| 4874 | mlog_errno(ret); |
| 4875 | goto out; |
| 4876 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4880 | insert.ins_appending = APPEND_NONE; |
| 4881 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4882 | insert.ins_tree_depth = depth; |
| 4883 | |
| 4884 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 4885 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 4886 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 4887 | le16_to_cpu(rec.e_leaf_clusters); |
| 4888 | |
| 4889 | if (split_rec.e_cpos == rec.e_cpos) { |
| 4890 | insert.ins_split = SPLIT_LEFT; |
| 4891 | } else if (insert_range == rec_range) { |
| 4892 | insert.ins_split = SPLIT_RIGHT; |
| 4893 | } else { |
| 4894 | /* |
| 4895 | * Left/right split. We fake this as a right split |
| 4896 | * first and then make a second pass as a left split. |
| 4897 | */ |
| 4898 | insert.ins_split = SPLIT_RIGHT; |
| 4899 | |
| 4900 | ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range, |
| 4901 | &rec); |
| 4902 | |
| 4903 | split_rec = tmprec; |
| 4904 | |
| 4905 | BUG_ON(do_leftright); |
| 4906 | do_leftright = 1; |
| 4907 | } |
| 4908 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4909 | ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4910 | if (ret) { |
| 4911 | mlog_errno(ret); |
| 4912 | goto out; |
| 4913 | } |
| 4914 | |
| 4915 | if (do_leftright == 1) { |
| 4916 | u32 cpos; |
| 4917 | struct ocfs2_extent_list *el; |
| 4918 | |
| 4919 | do_leftright++; |
| 4920 | split_rec = *orig_split_rec; |
| 4921 | |
| 4922 | ocfs2_reinit_path(path, 1); |
| 4923 | |
| 4924 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4925 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4926 | if (ret) { |
| 4927 | mlog_errno(ret); |
| 4928 | goto out; |
| 4929 | } |
| 4930 | |
| 4931 | el = path_leaf_el(path); |
| 4932 | split_index = ocfs2_search_extent_list(el, cpos); |
| 4933 | goto leftright; |
| 4934 | } |
| 4935 | out: |
| 4936 | |
| 4937 | return ret; |
| 4938 | } |
| 4939 | |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4940 | static int ocfs2_replace_extent_rec(struct inode *inode, |
| 4941 | handle_t *handle, |
| 4942 | struct ocfs2_path *path, |
| 4943 | struct ocfs2_extent_list *el, |
| 4944 | int split_index, |
| 4945 | struct ocfs2_extent_rec *split_rec) |
| 4946 | { |
| 4947 | int ret; |
| 4948 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4949 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4950 | path_num_items(path) - 1); |
| 4951 | if (ret) { |
| 4952 | mlog_errno(ret); |
| 4953 | goto out; |
| 4954 | } |
| 4955 | |
| 4956 | el->l_recs[split_index] = *split_rec; |
| 4957 | |
| 4958 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 4959 | out: |
| 4960 | return ret; |
| 4961 | } |
| 4962 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4963 | /* |
| 4964 | * Mark part or all of the extent record at split_index in the leaf |
| 4965 | * pointed to by path as written. This removes the unwritten |
| 4966 | * extent flag. |
| 4967 | * |
| 4968 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 4969 | * |
| 4970 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 4971 | * of the tree is required. All other cases will degrade into a less |
| 4972 | * optimal tree layout. |
| 4973 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4974 | * last_eb_bh should be the rightmost leaf block for any extent |
| 4975 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 4976 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4977 | * |
| 4978 | * This code is optimized for readability - several passes might be |
| 4979 | * made over certain portions of the tree. All of those blocks will |
| 4980 | * have been brought into cache (and pinned via the journal), so the |
| 4981 | * extra overhead is not expressed in terms of disk reads. |
| 4982 | */ |
| 4983 | static int __ocfs2_mark_extent_written(struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4984 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4985 | handle_t *handle, |
| 4986 | struct ocfs2_path *path, |
| 4987 | int split_index, |
| 4988 | struct ocfs2_extent_rec *split_rec, |
| 4989 | struct ocfs2_alloc_context *meta_ac, |
| 4990 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 4991 | { |
| 4992 | int ret = 0; |
| 4993 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4994 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4995 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 4996 | struct ocfs2_merge_ctxt ctxt; |
| 4997 | struct ocfs2_extent_list *rightmost_el; |
| 4998 | |
Roel Kluin | 3cf0c50 | 2007-10-27 00:20:36 +0200 | [diff] [blame] | 4999 | if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5000 | ret = -EIO; |
| 5001 | mlog_errno(ret); |
| 5002 | goto out; |
| 5003 | } |
| 5004 | |
| 5005 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5006 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5007 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5008 | ret = -EIO; |
| 5009 | mlog_errno(ret); |
| 5010 | goto out; |
| 5011 | } |
| 5012 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 5013 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5014 | split_index, |
| 5015 | split_rec); |
| 5016 | |
| 5017 | /* |
| 5018 | * The core merge / split code wants to know how much room is |
| 5019 | * left in this inodes allocation tree, so we pass the |
| 5020 | * rightmost extent list. |
| 5021 | */ |
| 5022 | if (path->p_tree_depth) { |
| 5023 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5024 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5025 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5026 | ocfs2_et_get_last_eb_blk(et), |
| 5027 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5028 | if (ret) { |
| 5029 | mlog_exit(ret); |
| 5030 | goto out; |
| 5031 | } |
| 5032 | |
| 5033 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5034 | rightmost_el = &eb->h_list; |
| 5035 | } else |
| 5036 | rightmost_el = path_root_el(path); |
| 5037 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5038 | if (rec->e_cpos == split_rec->e_cpos && |
| 5039 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5040 | ctxt.c_split_covers_rec = 1; |
| 5041 | else |
| 5042 | ctxt.c_split_covers_rec = 0; |
| 5043 | |
| 5044 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5045 | |
Mark Fasheh | 015452b | 2007-09-12 10:21:22 -0700 | [diff] [blame] | 5046 | mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", |
| 5047 | split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, |
| 5048 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5049 | |
| 5050 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5051 | if (ctxt.c_split_covers_rec) |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5052 | ret = ocfs2_replace_extent_rec(inode, handle, |
| 5053 | path, el, |
| 5054 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5055 | else |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5056 | ret = ocfs2_split_and_insert(inode, handle, path, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5057 | &last_eb_bh, split_index, |
| 5058 | split_rec, meta_ac); |
| 5059 | if (ret) |
| 5060 | mlog_errno(ret); |
| 5061 | } else { |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5062 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5063 | split_index, split_rec, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5064 | dealloc, &ctxt); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5065 | if (ret) |
| 5066 | mlog_errno(ret); |
| 5067 | } |
| 5068 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5069 | out: |
| 5070 | brelse(last_eb_bh); |
| 5071 | return ret; |
| 5072 | } |
| 5073 | |
| 5074 | /* |
| 5075 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5076 | * |
| 5077 | * If the existing extent is larger than the request, initiate a |
| 5078 | * split. An attempt will be made at merging with adjacent extents. |
| 5079 | * |
| 5080 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5081 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5082 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5083 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5084 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5085 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5086 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5087 | { |
| 5088 | int ret, index; |
| 5089 | u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); |
| 5090 | struct ocfs2_extent_rec split_rec; |
| 5091 | struct ocfs2_path *left_path = NULL; |
| 5092 | struct ocfs2_extent_list *el; |
| 5093 | |
| 5094 | mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", |
| 5095 | inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); |
| 5096 | |
| 5097 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 5098 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 5099 | "that are being written to, but the feature bit " |
| 5100 | "is not set in the super block.", |
| 5101 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5102 | ret = -EROFS; |
| 5103 | goto out; |
| 5104 | } |
| 5105 | |
| 5106 | /* |
| 5107 | * XXX: This should be fixed up so that we just re-insert the |
| 5108 | * next extent records. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5109 | * |
| 5110 | * XXX: This is a hack on the extent tree, maybe it should be |
| 5111 | * an op? |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5112 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5113 | if (et->et_ops == &ocfs2_dinode_et_ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5114 | ocfs2_extent_map_trunc(inode, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5115 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5116 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5117 | if (!left_path) { |
| 5118 | ret = -ENOMEM; |
| 5119 | mlog_errno(ret); |
| 5120 | goto out; |
| 5121 | } |
| 5122 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5123 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5124 | if (ret) { |
| 5125 | mlog_errno(ret); |
| 5126 | goto out; |
| 5127 | } |
| 5128 | el = path_leaf_el(left_path); |
| 5129 | |
| 5130 | index = ocfs2_search_extent_list(el, cpos); |
| 5131 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5132 | ocfs2_error(inode->i_sb, |
| 5133 | "Inode %llu has an extent at cpos %u which can no " |
| 5134 | "longer be found.\n", |
| 5135 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 5136 | ret = -EROFS; |
| 5137 | goto out; |
| 5138 | } |
| 5139 | |
| 5140 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5141 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5142 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5143 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
| 5144 | split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; |
| 5145 | split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; |
| 5146 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5147 | ret = __ocfs2_mark_extent_written(inode, et, handle, left_path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5148 | index, &split_rec, meta_ac, |
| 5149 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5150 | if (ret) |
| 5151 | mlog_errno(ret); |
| 5152 | |
| 5153 | out: |
| 5154 | ocfs2_free_path(left_path); |
| 5155 | return ret; |
| 5156 | } |
| 5157 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5158 | 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] | 5159 | handle_t *handle, struct ocfs2_path *path, |
| 5160 | int index, u32 new_range, |
| 5161 | struct ocfs2_alloc_context *meta_ac) |
| 5162 | { |
| 5163 | int ret, depth, credits = handle->h_buffer_credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5164 | struct buffer_head *last_eb_bh = NULL; |
| 5165 | struct ocfs2_extent_block *eb; |
| 5166 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5167 | struct ocfs2_extent_rec split_rec; |
| 5168 | struct ocfs2_extent_rec *rec; |
| 5169 | struct ocfs2_insert_type insert; |
| 5170 | |
| 5171 | /* |
| 5172 | * Setup the record to split before we grow the tree. |
| 5173 | */ |
| 5174 | el = path_leaf_el(path); |
| 5175 | rec = &el->l_recs[index]; |
| 5176 | ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec); |
| 5177 | |
| 5178 | depth = path->p_tree_depth; |
| 5179 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5180 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5181 | ocfs2_et_get_last_eb_blk(et), |
| 5182 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5183 | if (ret < 0) { |
| 5184 | mlog_errno(ret); |
| 5185 | goto out; |
| 5186 | } |
| 5187 | |
| 5188 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5189 | rightmost_el = &eb->h_list; |
| 5190 | } else |
| 5191 | rightmost_el = path_leaf_el(path); |
| 5192 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 5193 | credits += path->p_tree_depth + |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 5194 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5195 | ret = ocfs2_extend_trans(handle, credits); |
| 5196 | if (ret) { |
| 5197 | mlog_errno(ret); |
| 5198 | goto out; |
| 5199 | } |
| 5200 | |
| 5201 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5202 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 5203 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5204 | meta_ac); |
| 5205 | if (ret) { |
| 5206 | mlog_errno(ret); |
| 5207 | goto out; |
| 5208 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5212 | insert.ins_appending = APPEND_NONE; |
| 5213 | insert.ins_contig = CONTIG_NONE; |
| 5214 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5215 | insert.ins_tree_depth = depth; |
| 5216 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5217 | ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5218 | if (ret) |
| 5219 | mlog_errno(ret); |
| 5220 | |
| 5221 | out: |
| 5222 | brelse(last_eb_bh); |
| 5223 | return ret; |
| 5224 | } |
| 5225 | |
| 5226 | static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, |
| 5227 | struct ocfs2_path *path, int index, |
| 5228 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5229 | u32 cpos, u32 len, |
| 5230 | struct ocfs2_extent_tree *et) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5231 | { |
| 5232 | int ret; |
| 5233 | u32 left_cpos, rec_range, trunc_range; |
| 5234 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
| 5235 | struct super_block *sb = inode->i_sb; |
| 5236 | struct ocfs2_path *left_path = NULL; |
| 5237 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5238 | struct ocfs2_extent_rec *rec; |
| 5239 | struct ocfs2_extent_block *eb; |
| 5240 | |
| 5241 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5242 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5243 | if (ret) { |
| 5244 | mlog_errno(ret); |
| 5245 | goto out; |
| 5246 | } |
| 5247 | |
| 5248 | index--; |
| 5249 | } |
| 5250 | |
| 5251 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5252 | path->p_tree_depth) { |
| 5253 | /* |
| 5254 | * Check whether this is the rightmost tree record. If |
| 5255 | * we remove all of this record or part of its right |
| 5256 | * edge then an update of the record lengths above it |
| 5257 | * will be required. |
| 5258 | */ |
| 5259 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5260 | if (eb->h_next_leaf_blk == 0) |
| 5261 | is_rightmost_tree_rec = 1; |
| 5262 | } |
| 5263 | |
| 5264 | rec = &el->l_recs[index]; |
| 5265 | if (index == 0 && path->p_tree_depth && |
| 5266 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5267 | /* |
| 5268 | * Changing the leftmost offset (via partial or whole |
| 5269 | * record truncate) of an interior (or rightmost) path |
| 5270 | * means we have to update the subtree that is formed |
| 5271 | * by this leaf and the one to it's left. |
| 5272 | * |
| 5273 | * There are two cases we can skip: |
| 5274 | * 1) Path is the leftmost one in our inode tree. |
| 5275 | * 2) The leaf is rightmost and will be empty after |
| 5276 | * we remove the extent record - the rotate code |
| 5277 | * knows how to update the newly formed edge. |
| 5278 | */ |
| 5279 | |
| 5280 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, |
| 5281 | &left_cpos); |
| 5282 | if (ret) { |
| 5283 | mlog_errno(ret); |
| 5284 | goto out; |
| 5285 | } |
| 5286 | |
| 5287 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5288 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5289 | if (!left_path) { |
| 5290 | ret = -ENOMEM; |
| 5291 | mlog_errno(ret); |
| 5292 | goto out; |
| 5293 | } |
| 5294 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5295 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5296 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5297 | if (ret) { |
| 5298 | mlog_errno(ret); |
| 5299 | goto out; |
| 5300 | } |
| 5301 | } |
| 5302 | } |
| 5303 | |
| 5304 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5305 | handle->h_buffer_credits, |
| 5306 | path); |
| 5307 | if (ret) { |
| 5308 | mlog_errno(ret); |
| 5309 | goto out; |
| 5310 | } |
| 5311 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5312 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5313 | if (ret) { |
| 5314 | mlog_errno(ret); |
| 5315 | goto out; |
| 5316 | } |
| 5317 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5318 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5319 | if (ret) { |
| 5320 | mlog_errno(ret); |
| 5321 | goto out; |
| 5322 | } |
| 5323 | |
| 5324 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5325 | trunc_range = cpos + len; |
| 5326 | |
| 5327 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5328 | int next_free; |
| 5329 | |
| 5330 | memset(rec, 0, sizeof(*rec)); |
| 5331 | ocfs2_cleanup_merge(el, index); |
| 5332 | wants_rotate = 1; |
| 5333 | |
| 5334 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5335 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5336 | /* |
| 5337 | * We skip the edge update if this path will |
| 5338 | * be deleted by the rotate code. |
| 5339 | */ |
| 5340 | rec = &el->l_recs[next_free - 1]; |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 5341 | ocfs2_adjust_rightmost_records(handle, et, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5342 | rec); |
| 5343 | } |
| 5344 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5345 | /* Remove leftmost portion of the record. */ |
| 5346 | le32_add_cpu(&rec->e_cpos, len); |
| 5347 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5348 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5349 | } else if (rec_range == trunc_range) { |
| 5350 | /* Remove rightmost portion of the record */ |
| 5351 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5352 | if (is_rightmost_tree_rec) |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame^] | 5353 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5354 | } else { |
| 5355 | /* Caller should have trapped this. */ |
| 5356 | mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) " |
| 5357 | "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5358 | le32_to_cpu(rec->e_cpos), |
| 5359 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5360 | BUG(); |
| 5361 | } |
| 5362 | |
| 5363 | if (left_path) { |
| 5364 | int subtree_index; |
| 5365 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5366 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5367 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5368 | subtree_index); |
| 5369 | } |
| 5370 | |
| 5371 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5372 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5373 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5374 | if (ret) { |
| 5375 | mlog_errno(ret); |
| 5376 | goto out; |
| 5377 | } |
| 5378 | |
| 5379 | out: |
| 5380 | ocfs2_free_path(left_path); |
| 5381 | return ret; |
| 5382 | } |
| 5383 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5384 | int ocfs2_remove_extent(struct inode *inode, |
| 5385 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5386 | u32 cpos, u32 len, handle_t *handle, |
| 5387 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5388 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5389 | { |
| 5390 | int ret, index; |
| 5391 | u32 rec_range, trunc_range; |
| 5392 | struct ocfs2_extent_rec *rec; |
| 5393 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5394 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5395 | |
| 5396 | ocfs2_extent_map_trunc(inode, 0); |
| 5397 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5398 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5399 | if (!path) { |
| 5400 | ret = -ENOMEM; |
| 5401 | mlog_errno(ret); |
| 5402 | goto out; |
| 5403 | } |
| 5404 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5405 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5406 | if (ret) { |
| 5407 | mlog_errno(ret); |
| 5408 | goto out; |
| 5409 | } |
| 5410 | |
| 5411 | el = path_leaf_el(path); |
| 5412 | index = ocfs2_search_extent_list(el, cpos); |
| 5413 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5414 | ocfs2_error(inode->i_sb, |
| 5415 | "Inode %llu has an extent at cpos %u which can no " |
| 5416 | "longer be found.\n", |
| 5417 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 5418 | ret = -EROFS; |
| 5419 | goto out; |
| 5420 | } |
| 5421 | |
| 5422 | /* |
| 5423 | * We have 3 cases of extent removal: |
| 5424 | * 1) Range covers the entire extent rec |
| 5425 | * 2) Range begins or ends on one edge of the extent rec |
| 5426 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5427 | * |
| 5428 | * For case 1 we remove the extent rec and left rotate to |
| 5429 | * fill the hole. |
| 5430 | * |
| 5431 | * For case 2 we just shrink the existing extent rec, with a |
| 5432 | * tree update if the shrinking edge is also the edge of an |
| 5433 | * extent block. |
| 5434 | * |
| 5435 | * For case 3 we do a right split to turn the extent rec into |
| 5436 | * something case 2 can handle. |
| 5437 | */ |
| 5438 | rec = &el->l_recs[index]; |
| 5439 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5440 | trunc_range = cpos + len; |
| 5441 | |
| 5442 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5443 | |
| 5444 | mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d " |
| 5445 | "(cpos %u, len %u)\n", |
| 5446 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index, |
| 5447 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 5448 | |
| 5449 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
| 5450 | ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5451 | cpos, len, et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5452 | if (ret) { |
| 5453 | mlog_errno(ret); |
| 5454 | goto out; |
| 5455 | } |
| 5456 | } else { |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5457 | ret = ocfs2_split_tree(inode, et, handle, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5458 | trunc_range, meta_ac); |
| 5459 | if (ret) { |
| 5460 | mlog_errno(ret); |
| 5461 | goto out; |
| 5462 | } |
| 5463 | |
| 5464 | /* |
| 5465 | * The split could have manipulated the tree enough to |
| 5466 | * move the record location, so we have to look for it again. |
| 5467 | */ |
| 5468 | ocfs2_reinit_path(path, 1); |
| 5469 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5470 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5471 | if (ret) { |
| 5472 | mlog_errno(ret); |
| 5473 | goto out; |
| 5474 | } |
| 5475 | |
| 5476 | el = path_leaf_el(path); |
| 5477 | index = ocfs2_search_extent_list(el, cpos); |
| 5478 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5479 | ocfs2_error(inode->i_sb, |
| 5480 | "Inode %llu: split at cpos %u lost record.", |
| 5481 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5482 | cpos); |
| 5483 | ret = -EROFS; |
| 5484 | goto out; |
| 5485 | } |
| 5486 | |
| 5487 | /* |
| 5488 | * Double check our values here. If anything is fishy, |
| 5489 | * it's easier to catch it at the top level. |
| 5490 | */ |
| 5491 | rec = &el->l_recs[index]; |
| 5492 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5493 | ocfs2_rec_clusters(el, rec); |
| 5494 | if (rec_range != trunc_range) { |
| 5495 | ocfs2_error(inode->i_sb, |
| 5496 | "Inode %llu: error after split at cpos %u" |
| 5497 | "trunc len %u, existing record is (%u,%u)", |
| 5498 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5499 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5500 | ocfs2_rec_clusters(el, rec)); |
| 5501 | ret = -EROFS; |
| 5502 | goto out; |
| 5503 | } |
| 5504 | |
| 5505 | ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5506 | cpos, len, et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5507 | if (ret) { |
| 5508 | mlog_errno(ret); |
| 5509 | goto out; |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | out: |
| 5514 | ocfs2_free_path(path); |
| 5515 | return ret; |
| 5516 | } |
| 5517 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5518 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5519 | struct ocfs2_extent_tree *et, |
| 5520 | u32 cpos, u32 phys_cpos, u32 len, |
| 5521 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5522 | { |
| 5523 | int ret; |
| 5524 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5525 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5526 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5527 | handle_t *handle; |
| 5528 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5529 | |
| 5530 | ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); |
| 5531 | if (ret) { |
| 5532 | mlog_errno(ret); |
| 5533 | return ret; |
| 5534 | } |
| 5535 | |
| 5536 | mutex_lock(&tl_inode->i_mutex); |
| 5537 | |
| 5538 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5539 | ret = __ocfs2_flush_truncate_log(osb); |
| 5540 | if (ret < 0) { |
| 5541 | mlog_errno(ret); |
| 5542 | goto out; |
| 5543 | } |
| 5544 | } |
| 5545 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5546 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5547 | if (IS_ERR(handle)) { |
| 5548 | ret = PTR_ERR(handle); |
| 5549 | mlog_errno(ret); |
| 5550 | goto out; |
| 5551 | } |
| 5552 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5553 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5554 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5555 | if (ret) { |
| 5556 | mlog_errno(ret); |
| 5557 | goto out; |
| 5558 | } |
| 5559 | |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5560 | vfs_dq_free_space_nodirty(inode, |
| 5561 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5562 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5563 | ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac, |
| 5564 | dealloc); |
| 5565 | if (ret) { |
| 5566 | mlog_errno(ret); |
| 5567 | goto out_commit; |
| 5568 | } |
| 5569 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5570 | ocfs2_et_update_clusters(et, -len); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5571 | |
| 5572 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
| 5573 | if (ret) { |
| 5574 | mlog_errno(ret); |
| 5575 | goto out_commit; |
| 5576 | } |
| 5577 | |
| 5578 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 5579 | if (ret) |
| 5580 | mlog_errno(ret); |
| 5581 | |
| 5582 | out_commit: |
| 5583 | ocfs2_commit_trans(osb, handle); |
| 5584 | out: |
| 5585 | mutex_unlock(&tl_inode->i_mutex); |
| 5586 | |
| 5587 | if (meta_ac) |
| 5588 | ocfs2_free_alloc_context(meta_ac); |
| 5589 | |
| 5590 | return ret; |
| 5591 | } |
| 5592 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5593 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5594 | { |
| 5595 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5596 | struct ocfs2_dinode *di; |
| 5597 | struct ocfs2_truncate_log *tl; |
| 5598 | |
| 5599 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5600 | tl = &di->id2.i_dealloc; |
| 5601 | |
| 5602 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5603 | "slot %d, invalid truncate log parameters: used = " |
| 5604 | "%u, count = %u\n", osb->slot_num, |
| 5605 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5606 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5607 | } |
| 5608 | |
| 5609 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5610 | unsigned int new_start) |
| 5611 | { |
| 5612 | unsigned int tail_index; |
| 5613 | unsigned int current_tail; |
| 5614 | |
| 5615 | /* No records, nothing to coalesce */ |
| 5616 | if (!le16_to_cpu(tl->tl_used)) |
| 5617 | return 0; |
| 5618 | |
| 5619 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5620 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5621 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5622 | |
| 5623 | return current_tail == new_start; |
| 5624 | } |
| 5625 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5626 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5627 | handle_t *handle, |
| 5628 | u64 start_blk, |
| 5629 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5630 | { |
| 5631 | int status, index; |
| 5632 | unsigned int start_cluster, tl_count; |
| 5633 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5634 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5635 | struct ocfs2_dinode *di; |
| 5636 | struct ocfs2_truncate_log *tl; |
| 5637 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5638 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 5639 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5640 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5641 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5642 | |
| 5643 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5644 | |
| 5645 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5646 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5647 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5648 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5649 | * corruption is a code bug */ |
| 5650 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5651 | |
| 5652 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5653 | tl_count = le16_to_cpu(tl->tl_count); |
| 5654 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5655 | tl_count == 0, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5656 | "Truncate record count on #%llu invalid " |
| 5657 | "wanted %u, actual %u\n", |
| 5658 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5659 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5660 | le16_to_cpu(tl->tl_count)); |
| 5661 | |
| 5662 | /* Caller should have known to flush before calling us. */ |
| 5663 | index = le16_to_cpu(tl->tl_used); |
| 5664 | if (index >= tl_count) { |
| 5665 | status = -ENOSPC; |
| 5666 | mlog_errno(status); |
| 5667 | goto bail; |
| 5668 | } |
| 5669 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5670 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5671 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5672 | if (status < 0) { |
| 5673 | mlog_errno(status); |
| 5674 | goto bail; |
| 5675 | } |
| 5676 | |
| 5677 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5678 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 5679 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5680 | |
| 5681 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5682 | /* |
| 5683 | * Move index back to the record we are coalescing with. |
| 5684 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5685 | */ |
| 5686 | index--; |
| 5687 | |
| 5688 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 5689 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 5690 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5691 | num_clusters); |
| 5692 | } else { |
| 5693 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5694 | tl->tl_used = cpu_to_le16(index + 1); |
| 5695 | } |
| 5696 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5697 | |
| 5698 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5699 | if (status < 0) { |
| 5700 | mlog_errno(status); |
| 5701 | goto bail; |
| 5702 | } |
| 5703 | |
| 5704 | bail: |
| 5705 | mlog_exit(status); |
| 5706 | return status; |
| 5707 | } |
| 5708 | |
| 5709 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5710 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5711 | struct inode *data_alloc_inode, |
| 5712 | struct buffer_head *data_alloc_bh) |
| 5713 | { |
| 5714 | int status = 0; |
| 5715 | int i; |
| 5716 | unsigned int num_clusters; |
| 5717 | u64 start_blk; |
| 5718 | struct ocfs2_truncate_rec rec; |
| 5719 | struct ocfs2_dinode *di; |
| 5720 | struct ocfs2_truncate_log *tl; |
| 5721 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5722 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5723 | |
| 5724 | mlog_entry_void(); |
| 5725 | |
| 5726 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5727 | tl = &di->id2.i_dealloc; |
| 5728 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5729 | while (i >= 0) { |
| 5730 | /* Caller has given us at least enough credits to |
| 5731 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5732 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5733 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5734 | if (status < 0) { |
| 5735 | mlog_errno(status); |
| 5736 | goto bail; |
| 5737 | } |
| 5738 | |
| 5739 | tl->tl_used = cpu_to_le16(i); |
| 5740 | |
| 5741 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5742 | if (status < 0) { |
| 5743 | mlog_errno(status); |
| 5744 | goto bail; |
| 5745 | } |
| 5746 | |
| 5747 | /* TODO: Perhaps we can calculate the bulk of the |
| 5748 | * credits up front rather than extending like |
| 5749 | * this. */ |
| 5750 | status = ocfs2_extend_trans(handle, |
| 5751 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5752 | if (status < 0) { |
| 5753 | mlog_errno(status); |
| 5754 | goto bail; |
| 5755 | } |
| 5756 | |
| 5757 | rec = tl->tl_recs[i]; |
| 5758 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5759 | le32_to_cpu(rec.t_start)); |
| 5760 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5761 | |
| 5762 | /* if start_blk is not set, we ignore the record as |
| 5763 | * invalid. */ |
| 5764 | if (start_blk) { |
| 5765 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 5766 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 5767 | |
| 5768 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5769 | data_alloc_bh, start_blk, |
| 5770 | num_clusters); |
| 5771 | if (status < 0) { |
| 5772 | mlog_errno(status); |
| 5773 | goto bail; |
| 5774 | } |
| 5775 | } |
| 5776 | i--; |
| 5777 | } |
| 5778 | |
| 5779 | bail: |
| 5780 | mlog_exit(status); |
| 5781 | return status; |
| 5782 | } |
| 5783 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5784 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5785 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5786 | { |
| 5787 | int status; |
| 5788 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5789 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5790 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5791 | struct inode *data_alloc_inode = NULL; |
| 5792 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5793 | struct buffer_head *data_alloc_bh = NULL; |
| 5794 | struct ocfs2_dinode *di; |
| 5795 | struct ocfs2_truncate_log *tl; |
| 5796 | |
| 5797 | mlog_entry_void(); |
| 5798 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5799 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5800 | |
| 5801 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5802 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5803 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5804 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5805 | * corruption is a code bug */ |
| 5806 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5807 | |
| 5808 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5809 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5810 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 5811 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5812 | if (!num_to_flush) { |
| 5813 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5814 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 5818 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 5819 | OCFS2_INVALID_SLOT); |
| 5820 | if (!data_alloc_inode) { |
| 5821 | status = -EINVAL; |
| 5822 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5823 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5824 | } |
| 5825 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5826 | mutex_lock(&data_alloc_inode->i_mutex); |
| 5827 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5828 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5829 | if (status < 0) { |
| 5830 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5831 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5832 | } |
| 5833 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 5834 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5835 | if (IS_ERR(handle)) { |
| 5836 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5837 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5838 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 5842 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5843 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5844 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5845 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5846 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5847 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5848 | out_unlock: |
| 5849 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5850 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5851 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5852 | out_mutex: |
| 5853 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 5854 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5855 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5856 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5857 | mlog_exit(status); |
| 5858 | return status; |
| 5859 | } |
| 5860 | |
| 5861 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 5862 | { |
| 5863 | int status; |
| 5864 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5865 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5866 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5867 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5868 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5869 | |
| 5870 | return status; |
| 5871 | } |
| 5872 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5873 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5874 | { |
| 5875 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5876 | struct ocfs2_super *osb = |
| 5877 | container_of(work, struct ocfs2_super, |
| 5878 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5879 | |
| 5880 | mlog_entry_void(); |
| 5881 | |
| 5882 | status = ocfs2_flush_truncate_log(osb); |
| 5883 | if (status < 0) |
| 5884 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 5885 | else |
| 5886 | ocfs2_init_inode_steal_slot(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5887 | |
| 5888 | mlog_exit(status); |
| 5889 | } |
| 5890 | |
| 5891 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 5892 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 5893 | int cancel) |
| 5894 | { |
| 5895 | if (osb->osb_tl_inode) { |
| 5896 | /* We want to push off log flushes while truncates are |
| 5897 | * still running. */ |
| 5898 | if (cancel) |
| 5899 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 5900 | |
| 5901 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 5902 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 5903 | } |
| 5904 | } |
| 5905 | |
| 5906 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 5907 | int slot_num, |
| 5908 | struct inode **tl_inode, |
| 5909 | struct buffer_head **tl_bh) |
| 5910 | { |
| 5911 | int status; |
| 5912 | struct inode *inode = NULL; |
| 5913 | struct buffer_head *bh = NULL; |
| 5914 | |
| 5915 | inode = ocfs2_get_system_file_inode(osb, |
| 5916 | TRUNCATE_LOG_SYSTEM_INODE, |
| 5917 | slot_num); |
| 5918 | if (!inode) { |
| 5919 | status = -EINVAL; |
| 5920 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 5921 | goto bail; |
| 5922 | } |
| 5923 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 5924 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5925 | if (status < 0) { |
| 5926 | iput(inode); |
| 5927 | mlog_errno(status); |
| 5928 | goto bail; |
| 5929 | } |
| 5930 | |
| 5931 | *tl_inode = inode; |
| 5932 | *tl_bh = bh; |
| 5933 | bail: |
| 5934 | mlog_exit(status); |
| 5935 | return status; |
| 5936 | } |
| 5937 | |
| 5938 | /* called during the 1st stage of node recovery. we stamp a clean |
| 5939 | * truncate log and pass back a copy for processing later. if the |
| 5940 | * truncate log does not require processing, a *tl_copy is set to |
| 5941 | * NULL. */ |
| 5942 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 5943 | int slot_num, |
| 5944 | struct ocfs2_dinode **tl_copy) |
| 5945 | { |
| 5946 | int status; |
| 5947 | struct inode *tl_inode = NULL; |
| 5948 | struct buffer_head *tl_bh = NULL; |
| 5949 | struct ocfs2_dinode *di; |
| 5950 | struct ocfs2_truncate_log *tl; |
| 5951 | |
| 5952 | *tl_copy = NULL; |
| 5953 | |
| 5954 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 5955 | |
| 5956 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 5957 | if (status < 0) { |
| 5958 | mlog_errno(status); |
| 5959 | goto bail; |
| 5960 | } |
| 5961 | |
| 5962 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5963 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5964 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 5965 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 5966 | * so any corruption is a code bug */ |
| 5967 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5968 | |
| 5969 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5970 | if (le16_to_cpu(tl->tl_used)) { |
| 5971 | mlog(0, "We'll have %u logs to recover\n", |
| 5972 | le16_to_cpu(tl->tl_used)); |
| 5973 | |
| 5974 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 5975 | if (!(*tl_copy)) { |
| 5976 | status = -ENOMEM; |
| 5977 | mlog_errno(status); |
| 5978 | goto bail; |
| 5979 | } |
| 5980 | |
| 5981 | /* Assuming the write-out below goes well, this copy |
| 5982 | * will be passed back to recovery for processing. */ |
| 5983 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 5984 | |
| 5985 | /* All we need to do to clear the truncate log is set |
| 5986 | * tl_used. */ |
| 5987 | tl->tl_used = 0; |
| 5988 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5989 | 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] | 5990 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5991 | if (status < 0) { |
| 5992 | mlog_errno(status); |
| 5993 | goto bail; |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | bail: |
| 5998 | if (tl_inode) |
| 5999 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6000 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6001 | |
| 6002 | if (status < 0 && (*tl_copy)) { |
| 6003 | kfree(*tl_copy); |
| 6004 | *tl_copy = NULL; |
| 6005 | } |
| 6006 | |
| 6007 | mlog_exit(status); |
| 6008 | return status; |
| 6009 | } |
| 6010 | |
| 6011 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6012 | struct ocfs2_dinode *tl_copy) |
| 6013 | { |
| 6014 | int status = 0; |
| 6015 | int i; |
| 6016 | unsigned int clusters, num_recs, start_cluster; |
| 6017 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6018 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6019 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6020 | struct ocfs2_truncate_log *tl; |
| 6021 | |
| 6022 | mlog_entry_void(); |
| 6023 | |
| 6024 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6025 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6026 | return -EINVAL; |
| 6027 | } |
| 6028 | |
| 6029 | tl = &tl_copy->id2.i_dealloc; |
| 6030 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 6031 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6032 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6033 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6034 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6035 | for(i = 0; i < num_recs; i++) { |
| 6036 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6037 | status = __ocfs2_flush_truncate_log(osb); |
| 6038 | if (status < 0) { |
| 6039 | mlog_errno(status); |
| 6040 | goto bail_up; |
| 6041 | } |
| 6042 | } |
| 6043 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6044 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6045 | if (IS_ERR(handle)) { |
| 6046 | status = PTR_ERR(handle); |
| 6047 | mlog_errno(status); |
| 6048 | goto bail_up; |
| 6049 | } |
| 6050 | |
| 6051 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6052 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6053 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6054 | |
| 6055 | status = ocfs2_truncate_log_append(osb, handle, |
| 6056 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6057 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6058 | if (status < 0) { |
| 6059 | mlog_errno(status); |
| 6060 | goto bail_up; |
| 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6065 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6066 | |
| 6067 | mlog_exit(status); |
| 6068 | return status; |
| 6069 | } |
| 6070 | |
| 6071 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6072 | { |
| 6073 | int status; |
| 6074 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6075 | |
| 6076 | mlog_entry_void(); |
| 6077 | |
| 6078 | if (tl_inode) { |
| 6079 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6080 | flush_workqueue(ocfs2_wq); |
| 6081 | |
| 6082 | status = ocfs2_flush_truncate_log(osb); |
| 6083 | if (status < 0) |
| 6084 | mlog_errno(status); |
| 6085 | |
| 6086 | brelse(osb->osb_tl_bh); |
| 6087 | iput(osb->osb_tl_inode); |
| 6088 | } |
| 6089 | |
| 6090 | mlog_exit_void(); |
| 6091 | } |
| 6092 | |
| 6093 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6094 | { |
| 6095 | int status; |
| 6096 | struct inode *tl_inode = NULL; |
| 6097 | struct buffer_head *tl_bh = NULL; |
| 6098 | |
| 6099 | mlog_entry_void(); |
| 6100 | |
| 6101 | status = ocfs2_get_truncate_log_info(osb, |
| 6102 | osb->slot_num, |
| 6103 | &tl_inode, |
| 6104 | &tl_bh); |
| 6105 | if (status < 0) |
| 6106 | mlog_errno(status); |
| 6107 | |
| 6108 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6109 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6110 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6111 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6112 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6113 | osb->osb_tl_bh = tl_bh; |
| 6114 | osb->osb_tl_inode = tl_inode; |
| 6115 | |
| 6116 | mlog_exit(status); |
| 6117 | return status; |
| 6118 | } |
| 6119 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6120 | /* |
| 6121 | * Delayed de-allocation of suballocator blocks. |
| 6122 | * |
| 6123 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6124 | * |
| 6125 | * The locking for this can get extremely complicated, especially when |
| 6126 | * the suballocator inodes to delete from aren't known until deep |
| 6127 | * within an unrelated codepath. |
| 6128 | * |
| 6129 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6130 | * btree could have been grown by any number of nodes each allocating |
| 6131 | * out of their own suballoc inode. |
| 6132 | * |
| 6133 | * These structures allow the delay of block de-allocation until a |
| 6134 | * later time, when locking of multiple cluster inodes won't cause |
| 6135 | * deadlock. |
| 6136 | */ |
| 6137 | |
| 6138 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6139 | * Describe a single bit freed from a suballocator. For the block |
| 6140 | * suballocators, it represents one block. For the global cluster |
| 6141 | * allocator, it represents some clusters and free_bit indicates |
| 6142 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6143 | */ |
| 6144 | struct ocfs2_cached_block_free { |
| 6145 | struct ocfs2_cached_block_free *free_next; |
| 6146 | u64 free_blk; |
| 6147 | unsigned int free_bit; |
| 6148 | }; |
| 6149 | |
| 6150 | struct ocfs2_per_slot_free_list { |
| 6151 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6152 | int f_inode_type; |
| 6153 | int f_slot; |
| 6154 | struct ocfs2_cached_block_free *f_first; |
| 6155 | }; |
| 6156 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6157 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6158 | int sysfile_type, |
| 6159 | int slot, |
| 6160 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6161 | { |
| 6162 | int ret; |
| 6163 | u64 bg_blkno; |
| 6164 | handle_t *handle; |
| 6165 | struct inode *inode; |
| 6166 | struct buffer_head *di_bh = NULL; |
| 6167 | struct ocfs2_cached_block_free *tmp; |
| 6168 | |
| 6169 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6170 | if (!inode) { |
| 6171 | ret = -EINVAL; |
| 6172 | mlog_errno(ret); |
| 6173 | goto out; |
| 6174 | } |
| 6175 | |
| 6176 | mutex_lock(&inode->i_mutex); |
| 6177 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6178 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6179 | if (ret) { |
| 6180 | mlog_errno(ret); |
| 6181 | goto out_mutex; |
| 6182 | } |
| 6183 | |
| 6184 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6185 | if (IS_ERR(handle)) { |
| 6186 | ret = PTR_ERR(handle); |
| 6187 | mlog_errno(ret); |
| 6188 | goto out_unlock; |
| 6189 | } |
| 6190 | |
| 6191 | while (head) { |
| 6192 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6193 | head->free_bit); |
| 6194 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 6195 | head->free_bit, (unsigned long long)head->free_blk); |
| 6196 | |
| 6197 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6198 | head->free_bit, bg_blkno, 1); |
| 6199 | if (ret) { |
| 6200 | mlog_errno(ret); |
| 6201 | goto out_journal; |
| 6202 | } |
| 6203 | |
| 6204 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 6205 | if (ret) { |
| 6206 | mlog_errno(ret); |
| 6207 | goto out_journal; |
| 6208 | } |
| 6209 | |
| 6210 | tmp = head; |
| 6211 | head = head->free_next; |
| 6212 | kfree(tmp); |
| 6213 | } |
| 6214 | |
| 6215 | out_journal: |
| 6216 | ocfs2_commit_trans(osb, handle); |
| 6217 | |
| 6218 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6219 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6220 | brelse(di_bh); |
| 6221 | out_mutex: |
| 6222 | mutex_unlock(&inode->i_mutex); |
| 6223 | iput(inode); |
| 6224 | out: |
| 6225 | while(head) { |
| 6226 | /* Premature exit may have left some dangling items. */ |
| 6227 | tmp = head; |
| 6228 | head = head->free_next; |
| 6229 | kfree(tmp); |
| 6230 | } |
| 6231 | |
| 6232 | return ret; |
| 6233 | } |
| 6234 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6235 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6236 | u64 blkno, unsigned int bit) |
| 6237 | { |
| 6238 | int ret = 0; |
| 6239 | struct ocfs2_cached_block_free *item; |
| 6240 | |
| 6241 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6242 | if (item == NULL) { |
| 6243 | ret = -ENOMEM; |
| 6244 | mlog_errno(ret); |
| 6245 | return ret; |
| 6246 | } |
| 6247 | |
| 6248 | mlog(0, "Insert clusters: (bit %u, blk %llu)\n", |
| 6249 | bit, (unsigned long long)blkno); |
| 6250 | |
| 6251 | item->free_blk = blkno; |
| 6252 | item->free_bit = bit; |
| 6253 | item->free_next = ctxt->c_global_allocator; |
| 6254 | |
| 6255 | ctxt->c_global_allocator = item; |
| 6256 | return ret; |
| 6257 | } |
| 6258 | |
| 6259 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6260 | struct ocfs2_cached_block_free *head) |
| 6261 | { |
| 6262 | struct ocfs2_cached_block_free *tmp; |
| 6263 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6264 | handle_t *handle; |
| 6265 | int ret = 0; |
| 6266 | |
| 6267 | mutex_lock(&tl_inode->i_mutex); |
| 6268 | |
| 6269 | while (head) { |
| 6270 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6271 | ret = __ocfs2_flush_truncate_log(osb); |
| 6272 | if (ret < 0) { |
| 6273 | mlog_errno(ret); |
| 6274 | break; |
| 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6279 | if (IS_ERR(handle)) { |
| 6280 | ret = PTR_ERR(handle); |
| 6281 | mlog_errno(ret); |
| 6282 | break; |
| 6283 | } |
| 6284 | |
| 6285 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6286 | head->free_bit); |
| 6287 | |
| 6288 | ocfs2_commit_trans(osb, handle); |
| 6289 | tmp = head; |
| 6290 | head = head->free_next; |
| 6291 | kfree(tmp); |
| 6292 | |
| 6293 | if (ret < 0) { |
| 6294 | mlog_errno(ret); |
| 6295 | break; |
| 6296 | } |
| 6297 | } |
| 6298 | |
| 6299 | mutex_unlock(&tl_inode->i_mutex); |
| 6300 | |
| 6301 | while (head) { |
| 6302 | /* Premature exit may have left some dangling items. */ |
| 6303 | tmp = head; |
| 6304 | head = head->free_next; |
| 6305 | kfree(tmp); |
| 6306 | } |
| 6307 | |
| 6308 | return ret; |
| 6309 | } |
| 6310 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6311 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6312 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6313 | { |
| 6314 | int ret = 0, ret2; |
| 6315 | struct ocfs2_per_slot_free_list *fl; |
| 6316 | |
| 6317 | if (!ctxt) |
| 6318 | return 0; |
| 6319 | |
| 6320 | while (ctxt->c_first_suballocator) { |
| 6321 | fl = ctxt->c_first_suballocator; |
| 6322 | |
| 6323 | if (fl->f_first) { |
| 6324 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 6325 | fl->f_inode_type, fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6326 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6327 | fl->f_inode_type, |
| 6328 | fl->f_slot, |
| 6329 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6330 | if (ret2) |
| 6331 | mlog_errno(ret2); |
| 6332 | if (!ret) |
| 6333 | ret = ret2; |
| 6334 | } |
| 6335 | |
| 6336 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6337 | kfree(fl); |
| 6338 | } |
| 6339 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6340 | if (ctxt->c_global_allocator) { |
| 6341 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6342 | ctxt->c_global_allocator); |
| 6343 | if (ret2) |
| 6344 | mlog_errno(ret2); |
| 6345 | if (!ret) |
| 6346 | ret = ret2; |
| 6347 | |
| 6348 | ctxt->c_global_allocator = NULL; |
| 6349 | } |
| 6350 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6351 | return ret; |
| 6352 | } |
| 6353 | |
| 6354 | static struct ocfs2_per_slot_free_list * |
| 6355 | ocfs2_find_per_slot_free_list(int type, |
| 6356 | int slot, |
| 6357 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6358 | { |
| 6359 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6360 | |
| 6361 | while (fl) { |
| 6362 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6363 | return fl; |
| 6364 | |
| 6365 | fl = fl->f_next_suballocator; |
| 6366 | } |
| 6367 | |
| 6368 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6369 | if (fl) { |
| 6370 | fl->f_inode_type = type; |
| 6371 | fl->f_slot = slot; |
| 6372 | fl->f_first = NULL; |
| 6373 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6374 | |
| 6375 | ctxt->c_first_suballocator = fl; |
| 6376 | } |
| 6377 | return fl; |
| 6378 | } |
| 6379 | |
| 6380 | static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6381 | int type, int slot, u64 blkno, |
| 6382 | unsigned int bit) |
| 6383 | { |
| 6384 | int ret; |
| 6385 | struct ocfs2_per_slot_free_list *fl; |
| 6386 | struct ocfs2_cached_block_free *item; |
| 6387 | |
| 6388 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6389 | if (fl == NULL) { |
| 6390 | ret = -ENOMEM; |
| 6391 | mlog_errno(ret); |
| 6392 | goto out; |
| 6393 | } |
| 6394 | |
| 6395 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6396 | if (item == NULL) { |
| 6397 | ret = -ENOMEM; |
| 6398 | mlog_errno(ret); |
| 6399 | goto out; |
| 6400 | } |
| 6401 | |
| 6402 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 6403 | type, slot, bit, (unsigned long long)blkno); |
| 6404 | |
| 6405 | item->free_blk = blkno; |
| 6406 | item->free_bit = bit; |
| 6407 | item->free_next = fl->f_first; |
| 6408 | |
| 6409 | fl->f_first = item; |
| 6410 | |
| 6411 | ret = 0; |
| 6412 | out: |
| 6413 | return ret; |
| 6414 | } |
| 6415 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6416 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6417 | struct ocfs2_extent_block *eb) |
| 6418 | { |
| 6419 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6420 | le16_to_cpu(eb->h_suballoc_slot), |
| 6421 | le64_to_cpu(eb->h_blkno), |
| 6422 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6423 | } |
| 6424 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6425 | /* This function will figure out whether the currently last extent |
| 6426 | * block will be deleted, and if it will, what the new last extent |
| 6427 | * block will be so we can update his h_next_leaf_blk field, as well |
| 6428 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6429 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6430 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6431 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6432 | struct buffer_head **new_last_eb) |
| 6433 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6434 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6435 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6436 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6437 | struct ocfs2_extent_block *eb; |
| 6438 | struct ocfs2_extent_list *el; |
| 6439 | struct buffer_head *bh = NULL; |
| 6440 | |
| 6441 | *new_last_eb = NULL; |
| 6442 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6443 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6444 | if (!path->p_tree_depth) |
| 6445 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6446 | |
| 6447 | /* trunc to zero special case - this makes tree_depth = 0 |
| 6448 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6449 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6450 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6451 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6452 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6453 | BUG_ON(!el->l_next_free_rec); |
| 6454 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6455 | /* |
| 6456 | * Make sure that this extent list will actually be empty |
| 6457 | * after we clear away the data. We can shortcut out if |
| 6458 | * there's more than one non-empty extent in the |
| 6459 | * list. Otherwise, a check of the remaining extent is |
| 6460 | * necessary. |
| 6461 | */ |
| 6462 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 6463 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6464 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6465 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6466 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6467 | |
| 6468 | /* We may have a valid extent in index 1, check it. */ |
| 6469 | if (next_free == 2) |
| 6470 | rec = &el->l_recs[1]; |
| 6471 | |
| 6472 | /* |
| 6473 | * Fall through - no more nonempty extents, so we want |
| 6474 | * to delete this leaf. |
| 6475 | */ |
| 6476 | } else { |
| 6477 | if (next_free > 1) |
| 6478 | goto out; |
| 6479 | |
| 6480 | rec = &el->l_recs[0]; |
| 6481 | } |
| 6482 | |
| 6483 | if (rec) { |
| 6484 | /* |
| 6485 | * Check it we'll only be trimming off the end of this |
| 6486 | * cluster. |
| 6487 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6488 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6489 | goto out; |
| 6490 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6491 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6492 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 6493 | if (ret) { |
| 6494 | mlog_errno(ret); |
| 6495 | goto out; |
| 6496 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6497 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 6498 | 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] | 6499 | if (ret) { |
| 6500 | mlog_errno(ret); |
| 6501 | goto out; |
| 6502 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6503 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6504 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 6505 | el = &eb->h_list; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 6506 | |
| 6507 | /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). |
| 6508 | * Any corruption is a code bug. */ |
| 6509 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6510 | |
| 6511 | *new_last_eb = bh; |
| 6512 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6513 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 6514 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 6515 | out: |
| 6516 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6517 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6518 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6519 | } |
| 6520 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6521 | /* |
| 6522 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 6523 | * during truncate. |
| 6524 | * |
| 6525 | * The caller needs to: |
| 6526 | * - start journaling of each path component. |
| 6527 | * - compute and fully set up any new last ext block |
| 6528 | */ |
| 6529 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 6530 | handle_t *handle, struct ocfs2_truncate_context *tc, |
| 6531 | u32 clusters_to_del, u64 *delete_start) |
| 6532 | { |
| 6533 | int ret, i, index = path->p_tree_depth; |
| 6534 | u32 new_edge = 0; |
| 6535 | u64 deleted_eb = 0; |
| 6536 | struct buffer_head *bh; |
| 6537 | struct ocfs2_extent_list *el; |
| 6538 | struct ocfs2_extent_rec *rec; |
| 6539 | |
| 6540 | *delete_start = 0; |
| 6541 | |
| 6542 | while (index >= 0) { |
| 6543 | bh = path->p_node[index].bh; |
| 6544 | el = path->p_node[index].el; |
| 6545 | |
| 6546 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 6547 | index, (unsigned long long)bh->b_blocknr); |
| 6548 | |
| 6549 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 6550 | |
| 6551 | if (index != |
| 6552 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 6553 | ocfs2_error(inode->i_sb, |
| 6554 | "Inode %lu has invalid ext. block %llu", |
| 6555 | inode->i_ino, |
| 6556 | (unsigned long long)bh->b_blocknr); |
| 6557 | ret = -EROFS; |
| 6558 | goto out; |
| 6559 | } |
| 6560 | |
| 6561 | find_tail_record: |
| 6562 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 6563 | rec = &el->l_recs[i]; |
| 6564 | |
| 6565 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 6566 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6567 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6568 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6569 | le16_to_cpu(el->l_next_free_rec)); |
| 6570 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6571 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6572 | |
| 6573 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 6574 | /* |
| 6575 | * If the leaf block contains a single empty |
| 6576 | * extent and no records, we can just remove |
| 6577 | * the block. |
| 6578 | */ |
| 6579 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 6580 | memset(rec, 0, |
| 6581 | sizeof(struct ocfs2_extent_rec)); |
| 6582 | el->l_next_free_rec = cpu_to_le16(0); |
| 6583 | |
| 6584 | goto delete; |
| 6585 | } |
| 6586 | |
| 6587 | /* |
| 6588 | * Remove any empty extents by shifting things |
| 6589 | * left. That should make life much easier on |
| 6590 | * the code below. This condition is rare |
| 6591 | * enough that we shouldn't see a performance |
| 6592 | * hit. |
| 6593 | */ |
| 6594 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 6595 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6596 | |
| 6597 | for(i = 0; |
| 6598 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 6599 | el->l_recs[i] = el->l_recs[i + 1]; |
| 6600 | |
| 6601 | memset(&el->l_recs[i], 0, |
| 6602 | sizeof(struct ocfs2_extent_rec)); |
| 6603 | |
| 6604 | /* |
| 6605 | * We've modified our extent list. The |
| 6606 | * simplest way to handle this change |
| 6607 | * is to being the search from the |
| 6608 | * start again. |
| 6609 | */ |
| 6610 | goto find_tail_record; |
| 6611 | } |
| 6612 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6613 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6614 | |
| 6615 | /* |
| 6616 | * We'll use "new_edge" on our way back up the |
| 6617 | * tree to know what our rightmost cpos is. |
| 6618 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6619 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6620 | new_edge += le32_to_cpu(rec->e_cpos); |
| 6621 | |
| 6622 | /* |
| 6623 | * The caller will use this to delete data blocks. |
| 6624 | */ |
| 6625 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 6626 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6627 | le16_to_cpu(rec->e_leaf_clusters)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6628 | |
| 6629 | /* |
| 6630 | * If it's now empty, remove this record. |
| 6631 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6632 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6633 | memset(rec, 0, |
| 6634 | sizeof(struct ocfs2_extent_rec)); |
| 6635 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6636 | } |
| 6637 | } else { |
| 6638 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 6639 | memset(rec, 0, |
| 6640 | sizeof(struct ocfs2_extent_rec)); |
| 6641 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6642 | |
| 6643 | goto delete; |
| 6644 | } |
| 6645 | |
| 6646 | /* Can this actually happen? */ |
| 6647 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 6648 | goto delete; |
| 6649 | |
| 6650 | /* |
| 6651 | * We never actually deleted any clusters |
| 6652 | * because our leaf was empty. There's no |
| 6653 | * reason to adjust the rightmost edge then. |
| 6654 | */ |
| 6655 | if (new_edge == 0) |
| 6656 | goto delete; |
| 6657 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6658 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 6659 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6660 | -le32_to_cpu(rec->e_cpos)); |
| 6661 | |
| 6662 | /* |
| 6663 | * A deleted child record should have been |
| 6664 | * caught above. |
| 6665 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6666 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | delete: |
| 6670 | ret = ocfs2_journal_dirty(handle, bh); |
| 6671 | if (ret) { |
| 6672 | mlog_errno(ret); |
| 6673 | goto out; |
| 6674 | } |
| 6675 | |
| 6676 | mlog(0, "extent list container %llu, after: record %d: " |
| 6677 | "(%u, %u, %llu), next = %u.\n", |
| 6678 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6679 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6680 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6681 | le16_to_cpu(el->l_next_free_rec)); |
| 6682 | |
| 6683 | /* |
| 6684 | * We must be careful to only attempt delete of an |
| 6685 | * extent block (and not the root inode block). |
| 6686 | */ |
| 6687 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 6688 | struct ocfs2_extent_block *eb = |
| 6689 | (struct ocfs2_extent_block *)bh->b_data; |
| 6690 | |
| 6691 | /* |
| 6692 | * Save this for use when processing the |
| 6693 | * parent block. |
| 6694 | */ |
| 6695 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 6696 | |
| 6697 | mlog(0, "deleting this extent block.\n"); |
| 6698 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6699 | ocfs2_remove_from_cache(INODE_CACHE(inode), bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6700 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6701 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6702 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 6703 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 6704 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6705 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 6706 | /* An error here is not fatal. */ |
| 6707 | if (ret < 0) |
| 6708 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6709 | } else { |
| 6710 | deleted_eb = 0; |
| 6711 | } |
| 6712 | |
| 6713 | index--; |
| 6714 | } |
| 6715 | |
| 6716 | ret = 0; |
| 6717 | out: |
| 6718 | return ret; |
| 6719 | } |
| 6720 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6721 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 6722 | unsigned int clusters_to_del, |
| 6723 | struct inode *inode, |
| 6724 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6725 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6726 | struct ocfs2_truncate_context *tc, |
| 6727 | struct ocfs2_path *path) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6728 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6729 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6730 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6731 | struct ocfs2_extent_block *last_eb = NULL; |
| 6732 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6733 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6734 | u64 delete_blk = 0; |
| 6735 | |
| 6736 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6737 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6738 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6739 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6740 | if (status < 0) { |
| 6741 | mlog_errno(status); |
| 6742 | goto bail; |
| 6743 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6744 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6745 | /* |
| 6746 | * Each component will be touched, so we might as well journal |
| 6747 | * here to avoid having to handle errors later. |
| 6748 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6749 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6750 | if (status < 0) { |
| 6751 | mlog_errno(status); |
| 6752 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | if (last_eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6756 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6757 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6758 | if (status < 0) { |
| 6759 | mlog_errno(status); |
| 6760 | goto bail; |
| 6761 | } |
| 6762 | |
| 6763 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6764 | } |
| 6765 | |
| 6766 | el = &(fe->id2.i_list); |
| 6767 | |
| 6768 | /* |
| 6769 | * Lower levels depend on this never happening, but it's best |
| 6770 | * to check it up here before changing the tree. |
| 6771 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6772 | 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] | 6773 | ocfs2_error(inode->i_sb, |
| 6774 | "Inode %lu has an empty extent record, depth %u\n", |
| 6775 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6776 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6777 | goto bail; |
| 6778 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6779 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6780 | vfs_dq_free_space_nodirty(inode, |
| 6781 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6782 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 6783 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 6784 | clusters_to_del; |
| 6785 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 6786 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 6787 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6788 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6789 | status = ocfs2_trim_tree(inode, path, handle, tc, |
| 6790 | clusters_to_del, &delete_blk); |
| 6791 | if (status) { |
| 6792 | mlog_errno(status); |
| 6793 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6794 | } |
| 6795 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6796 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6797 | /* trunc to zero is a special case. */ |
| 6798 | el->l_tree_depth = 0; |
| 6799 | fe->i_last_eb_blk = 0; |
| 6800 | } else if (last_eb) |
| 6801 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 6802 | |
| 6803 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 6804 | if (status < 0) { |
| 6805 | mlog_errno(status); |
| 6806 | goto bail; |
| 6807 | } |
| 6808 | |
| 6809 | if (last_eb) { |
| 6810 | /* If there will be a new last extent block, then by |
| 6811 | * definition, there cannot be any leaves to the right of |
| 6812 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6813 | last_eb->h_next_leaf_blk = 0; |
| 6814 | status = ocfs2_journal_dirty(handle, last_eb_bh); |
| 6815 | if (status < 0) { |
| 6816 | mlog_errno(status); |
| 6817 | goto bail; |
| 6818 | } |
| 6819 | } |
| 6820 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6821 | if (delete_blk) { |
| 6822 | status = ocfs2_truncate_log_append(osb, handle, delete_blk, |
| 6823 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6824 | if (status < 0) { |
| 6825 | mlog_errno(status); |
| 6826 | goto bail; |
| 6827 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6828 | } |
| 6829 | status = 0; |
| 6830 | bail: |
Tao Ma | 60e2ec4 | 2009-08-12 14:42:47 +0800 | [diff] [blame] | 6831 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6832 | mlog_exit(status); |
| 6833 | return status; |
| 6834 | } |
| 6835 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6836 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6837 | { |
| 6838 | set_buffer_uptodate(bh); |
| 6839 | mark_buffer_dirty(bh); |
| 6840 | return 0; |
| 6841 | } |
| 6842 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6843 | static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 6844 | unsigned int from, unsigned int to, |
| 6845 | struct page *page, int zero, u64 *phys) |
| 6846 | { |
| 6847 | int ret, partial = 0; |
| 6848 | |
| 6849 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 6850 | if (ret) |
| 6851 | mlog_errno(ret); |
| 6852 | |
| 6853 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 6854 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6855 | |
| 6856 | /* |
| 6857 | * Need to set the buffers we zero'd into uptodate |
| 6858 | * here if they aren't - ocfs2_map_page_blocks() |
| 6859 | * might've skipped some |
| 6860 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6861 | ret = walk_page_buffers(handle, page_buffers(page), |
| 6862 | from, to, &partial, |
| 6863 | ocfs2_zero_func); |
| 6864 | if (ret < 0) |
| 6865 | mlog_errno(ret); |
| 6866 | else if (ocfs2_should_order_data(inode)) { |
| 6867 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6868 | if (ret < 0) |
| 6869 | mlog_errno(ret); |
| 6870 | } |
| 6871 | |
| 6872 | if (!partial) |
| 6873 | SetPageUptodate(page); |
| 6874 | |
| 6875 | flush_dcache_page(page); |
| 6876 | } |
| 6877 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6878 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 6879 | loff_t end, struct page **pages, |
| 6880 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6881 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6882 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6883 | struct page *page; |
| 6884 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 6885 | struct super_block *sb = inode->i_sb; |
| 6886 | |
| 6887 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 6888 | |
| 6889 | if (numpages == 0) |
| 6890 | goto out; |
| 6891 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6892 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6893 | for(i = 0; i < numpages; i++) { |
| 6894 | page = pages[i]; |
| 6895 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6896 | from = start & (PAGE_CACHE_SIZE - 1); |
| 6897 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 6898 | to = end & (PAGE_CACHE_SIZE - 1); |
| 6899 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6900 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 6901 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 6902 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6903 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 6904 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6905 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6906 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6907 | } |
| 6908 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6909 | if (pages) |
| 6910 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6911 | } |
| 6912 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6913 | 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] | 6914 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6915 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6916 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6917 | struct super_block *sb = inode->i_sb; |
| 6918 | struct address_space *mapping = inode->i_mapping; |
| 6919 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6920 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6921 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6922 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6923 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6924 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 6925 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 6926 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6927 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6928 | last_page_bytes = PAGE_ALIGN(end); |
| 6929 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6930 | do { |
| 6931 | pages[numpages] = grab_cache_page(mapping, index); |
| 6932 | if (!pages[numpages]) { |
| 6933 | ret = -ENOMEM; |
| 6934 | mlog_errno(ret); |
| 6935 | goto out; |
| 6936 | } |
| 6937 | |
| 6938 | numpages++; |
| 6939 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6940 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6941 | |
| 6942 | out: |
| 6943 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6944 | if (pages) |
| 6945 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6946 | numpages = 0; |
| 6947 | } |
| 6948 | |
| 6949 | *num = numpages; |
| 6950 | |
| 6951 | return ret; |
| 6952 | } |
| 6953 | |
| 6954 | /* |
| 6955 | * Zero the area past i_size but still within an allocated |
| 6956 | * cluster. This avoids exposing nonzero data on subsequent file |
| 6957 | * extends. |
| 6958 | * |
| 6959 | * We need to call this before i_size is updated on the inode because |
| 6960 | * otherwise block_write_full_page() will skip writeout of pages past |
| 6961 | * i_size. The new_i_size parameter is passed for this reason. |
| 6962 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6963 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 6964 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6965 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6966 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6967 | struct page **pages = NULL; |
| 6968 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6969 | unsigned int ext_flags; |
| 6970 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6971 | |
| 6972 | /* |
| 6973 | * File systems which don't support sparse files zero on every |
| 6974 | * extend. |
| 6975 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6976 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6977 | return 0; |
| 6978 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6979 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6980 | sizeof(struct page *), GFP_NOFS); |
| 6981 | if (pages == NULL) { |
| 6982 | ret = -ENOMEM; |
| 6983 | mlog_errno(ret); |
| 6984 | goto out; |
| 6985 | } |
| 6986 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6987 | if (range_start == range_end) |
| 6988 | goto out; |
| 6989 | |
| 6990 | ret = ocfs2_extent_map_get_blocks(inode, |
| 6991 | range_start >> sb->s_blocksize_bits, |
| 6992 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6993 | if (ret) { |
| 6994 | mlog_errno(ret); |
| 6995 | goto out; |
| 6996 | } |
| 6997 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6998 | /* |
| 6999 | * Tail is a hole, or is marked unwritten. In either case, we |
| 7000 | * can count on read and write to return/push zero's. |
| 7001 | */ |
| 7002 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7003 | goto out; |
| 7004 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7005 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 7006 | &numpages); |
| 7007 | if (ret) { |
| 7008 | mlog_errno(ret); |
| 7009 | goto out; |
| 7010 | } |
| 7011 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7012 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 7013 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7014 | |
| 7015 | /* |
| 7016 | * Initiate writeout of the pages we zero'd here. We don't |
| 7017 | * wait on them - the truncate_inode_pages() call later will |
| 7018 | * do that for us. |
| 7019 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7020 | ret = do_sync_mapping_range(inode->i_mapping, range_start, |
| 7021 | range_end - 1, SYNC_FILE_RANGE_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7022 | if (ret) |
| 7023 | mlog_errno(ret); |
| 7024 | |
| 7025 | out: |
| 7026 | if (pages) |
| 7027 | kfree(pages); |
| 7028 | |
| 7029 | return ret; |
| 7030 | } |
| 7031 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7032 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 7033 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7034 | { |
| 7035 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7036 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7037 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7038 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 7039 | memset(&di->id2, 0, blocksize - |
| 7040 | offsetof(struct ocfs2_dinode, id2) - |
| 7041 | xattrsize); |
| 7042 | else |
| 7043 | memset(&di->id2, 0, blocksize - |
| 7044 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7045 | } |
| 7046 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7047 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 7048 | struct ocfs2_dinode *di) |
| 7049 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7050 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7051 | di->id2.i_list.l_tree_depth = 0; |
| 7052 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7053 | di->id2.i_list.l_count = cpu_to_le16( |
| 7054 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7055 | } |
| 7056 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7057 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 7058 | { |
| 7059 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7060 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7061 | |
| 7062 | spin_lock(&oi->ip_lock); |
| 7063 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 7064 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7065 | spin_unlock(&oi->ip_lock); |
| 7066 | |
| 7067 | /* |
| 7068 | * We clear the entire i_data structure here so that all |
| 7069 | * fields can be properly initialized. |
| 7070 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7071 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7072 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7073 | idata->id_count = cpu_to_le16( |
| 7074 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 7078 | struct buffer_head *di_bh) |
| 7079 | { |
| 7080 | int ret, i, has_data, num_pages = 0; |
| 7081 | handle_t *handle; |
| 7082 | u64 uninitialized_var(block); |
| 7083 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7084 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7085 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7086 | struct ocfs2_alloc_context *data_ac = NULL; |
| 7087 | struct page **pages = NULL; |
| 7088 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7089 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7090 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7091 | |
| 7092 | has_data = i_size_read(inode) ? 1 : 0; |
| 7093 | |
| 7094 | if (has_data) { |
| 7095 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 7096 | sizeof(struct page *), GFP_NOFS); |
| 7097 | if (pages == NULL) { |
| 7098 | ret = -ENOMEM; |
| 7099 | mlog_errno(ret); |
| 7100 | goto out; |
| 7101 | } |
| 7102 | |
| 7103 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 7104 | if (ret) { |
| 7105 | mlog_errno(ret); |
| 7106 | goto out; |
| 7107 | } |
| 7108 | } |
| 7109 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7110 | handle = ocfs2_start_trans(osb, |
| 7111 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7112 | if (IS_ERR(handle)) { |
| 7113 | ret = PTR_ERR(handle); |
| 7114 | mlog_errno(ret); |
| 7115 | goto out_unlock; |
| 7116 | } |
| 7117 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7118 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7119 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7120 | if (ret) { |
| 7121 | mlog_errno(ret); |
| 7122 | goto out_commit; |
| 7123 | } |
| 7124 | |
| 7125 | if (has_data) { |
| 7126 | u32 bit_off, num; |
| 7127 | unsigned int page_end; |
| 7128 | u64 phys; |
| 7129 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7130 | if (vfs_dq_alloc_space_nodirty(inode, |
| 7131 | ocfs2_clusters_to_bytes(osb->sb, 1))) { |
| 7132 | ret = -EDQUOT; |
| 7133 | goto out_commit; |
| 7134 | } |
| 7135 | did_quota = 1; |
| 7136 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7137 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 7138 | &num); |
| 7139 | if (ret) { |
| 7140 | mlog_errno(ret); |
| 7141 | goto out_commit; |
| 7142 | } |
| 7143 | |
| 7144 | /* |
| 7145 | * Save two copies, one for insert, and one that can |
| 7146 | * be changed by ocfs2_map_and_dirty_page() below. |
| 7147 | */ |
| 7148 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 7149 | |
| 7150 | /* |
| 7151 | * Non sparse file systems zero on extend, so no need |
| 7152 | * to do that now. |
| 7153 | */ |
| 7154 | if (!ocfs2_sparse_alloc(osb) && |
| 7155 | PAGE_CACHE_SIZE < osb->s_clustersize) |
| 7156 | end = PAGE_CACHE_SIZE; |
| 7157 | |
| 7158 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 7159 | if (ret) { |
| 7160 | mlog_errno(ret); |
| 7161 | goto out_commit; |
| 7162 | } |
| 7163 | |
| 7164 | /* |
| 7165 | * This should populate the 1st page for us and mark |
| 7166 | * it up to date. |
| 7167 | */ |
| 7168 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 7169 | if (ret) { |
| 7170 | mlog_errno(ret); |
| 7171 | goto out_commit; |
| 7172 | } |
| 7173 | |
| 7174 | page_end = PAGE_CACHE_SIZE; |
| 7175 | if (PAGE_CACHE_SIZE > osb->s_clustersize) |
| 7176 | page_end = osb->s_clustersize; |
| 7177 | |
| 7178 | for (i = 0; i < num_pages; i++) |
| 7179 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 7180 | pages[i], i > 0, &phys); |
| 7181 | } |
| 7182 | |
| 7183 | spin_lock(&oi->ip_lock); |
| 7184 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 7185 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7186 | spin_unlock(&oi->ip_lock); |
| 7187 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7188 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7189 | |
| 7190 | ocfs2_journal_dirty(handle, di_bh); |
| 7191 | |
| 7192 | if (has_data) { |
| 7193 | /* |
| 7194 | * An error at this point should be extremely rare. If |
| 7195 | * this proves to be false, we could always re-build |
| 7196 | * the in-inode data from our pages. |
| 7197 | */ |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 7198 | ocfs2_init_dinode_extent_tree(&et, inode, di_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7199 | ret = ocfs2_insert_extent(osb, handle, inode, &et, |
| 7200 | 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7201 | if (ret) { |
| 7202 | mlog_errno(ret); |
| 7203 | goto out_commit; |
| 7204 | } |
| 7205 | |
| 7206 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7207 | } |
| 7208 | |
| 7209 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7210 | if (ret < 0 && did_quota) |
| 7211 | vfs_dq_free_space_nodirty(inode, |
| 7212 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7213 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7214 | ocfs2_commit_trans(osb, handle); |
| 7215 | |
| 7216 | out_unlock: |
| 7217 | if (data_ac) |
| 7218 | ocfs2_free_alloc_context(data_ac); |
| 7219 | |
| 7220 | out: |
| 7221 | if (pages) { |
| 7222 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7223 | kfree(pages); |
| 7224 | } |
| 7225 | |
| 7226 | return ret; |
| 7227 | } |
| 7228 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7229 | /* |
| 7230 | * It is expected, that by the time you call this function, |
| 7231 | * inode->i_size and fe->i_size have been adjusted. |
| 7232 | * |
| 7233 | * WARNING: This will kfree the truncate context |
| 7234 | */ |
| 7235 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7236 | struct inode *inode, |
| 7237 | struct buffer_head *fe_bh, |
| 7238 | struct ocfs2_truncate_context *tc) |
| 7239 | { |
| 7240 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7241 | u32 clusters_to_del, new_highest_cpos, range; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7242 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 7243 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7244 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7245 | struct ocfs2_path *path = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 7246 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7247 | |
| 7248 | mlog_entry_void(); |
| 7249 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7250 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7251 | i_size_read(inode)); |
| 7252 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7253 | path = ocfs2_new_path(fe_bh, &di->id2.i_list, |
| 7254 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7255 | if (!path) { |
| 7256 | status = -ENOMEM; |
| 7257 | mlog_errno(status); |
| 7258 | goto bail; |
| 7259 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7260 | |
| 7261 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7262 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7263 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7264 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7265 | * Check that we still have allocation to delete. |
| 7266 | */ |
| 7267 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7268 | status = 0; |
| 7269 | goto bail; |
| 7270 | } |
| 7271 | |
| 7272 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7273 | * Truncate always works against the rightmost tree branch. |
| 7274 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7275 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7276 | if (status) { |
| 7277 | mlog_errno(status); |
| 7278 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7279 | } |
| 7280 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7281 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 7282 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 7283 | |
| 7284 | /* |
| 7285 | * By now, el will point to the extent list on the bottom most |
| 7286 | * portion of this tree. Only the tail record is considered in |
| 7287 | * each pass. |
| 7288 | * |
| 7289 | * We handle the following cases, in order: |
| 7290 | * - empty extent: delete the remaining branch |
| 7291 | * - remove the entire record |
| 7292 | * - remove a partial record |
| 7293 | * - no record needs to be removed (truncate has completed) |
| 7294 | */ |
| 7295 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7296 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7297 | ocfs2_error(inode->i_sb, |
| 7298 | "Inode %llu has empty extent block at %llu\n", |
| 7299 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7300 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7301 | status = -EROFS; |
| 7302 | goto bail; |
| 7303 | } |
| 7304 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7305 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7306 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7307 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7308 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 7309 | clusters_to_del = 0; |
| 7310 | } 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] | 7311 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7312 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7313 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7314 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7315 | new_highest_cpos; |
| 7316 | } else { |
| 7317 | status = 0; |
| 7318 | goto bail; |
| 7319 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7320 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7321 | mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", |
| 7322 | clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7323 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7324 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7325 | tl_sem = 1; |
| 7326 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 7327 | * record is free for use. If there isn't any, we flush to get |
| 7328 | * an empty truncate log. */ |
| 7329 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 7330 | status = __ocfs2_flush_truncate_log(osb); |
| 7331 | if (status < 0) { |
| 7332 | mlog_errno(status); |
| 7333 | goto bail; |
| 7334 | } |
| 7335 | } |
| 7336 | |
| 7337 | credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7338 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 7339 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 7340 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7341 | if (IS_ERR(handle)) { |
| 7342 | status = PTR_ERR(handle); |
| 7343 | handle = NULL; |
| 7344 | mlog_errno(status); |
| 7345 | goto bail; |
| 7346 | } |
| 7347 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7348 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
| 7349 | tc, path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7350 | if (status < 0) { |
| 7351 | mlog_errno(status); |
| 7352 | goto bail; |
| 7353 | } |
| 7354 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7355 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7356 | tl_sem = 0; |
| 7357 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7358 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7359 | handle = NULL; |
| 7360 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7361 | ocfs2_reinit_path(path, 1); |
| 7362 | |
| 7363 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7364 | * The check above will catch the case where we've truncated |
| 7365 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7366 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7367 | goto start; |
| 7368 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7369 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7370 | |
| 7371 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7372 | |
| 7373 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7374 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7375 | |
| 7376 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7377 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7378 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7379 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 7380 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7381 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7382 | |
| 7383 | /* This will drop the ext_alloc cluster lock for us */ |
| 7384 | ocfs2_free_truncate_context(tc); |
| 7385 | |
| 7386 | mlog_exit(status); |
| 7387 | return status; |
| 7388 | } |
| 7389 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7390 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7391 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7392 | */ |
| 7393 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 7394 | struct inode *inode, |
| 7395 | struct buffer_head *fe_bh, |
| 7396 | struct ocfs2_truncate_context **tc) |
| 7397 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7398 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7399 | unsigned int new_i_clusters; |
| 7400 | struct ocfs2_dinode *fe; |
| 7401 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7402 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7403 | |
| 7404 | mlog_entry_void(); |
| 7405 | |
| 7406 | *tc = NULL; |
| 7407 | |
| 7408 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 7409 | i_size_read(inode)); |
| 7410 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 7411 | |
| 7412 | 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] | 7413 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 7414 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7415 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 7416 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7417 | if (!(*tc)) { |
| 7418 | status = -ENOMEM; |
| 7419 | mlog_errno(status); |
| 7420 | goto bail; |
| 7421 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7422 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7423 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7424 | if (fe->id2.i_list.l_tree_depth) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 7425 | status = ocfs2_read_extent_block(INODE_CACHE(inode), |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 7426 | le64_to_cpu(fe->i_last_eb_blk), |
| 7427 | &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7428 | if (status < 0) { |
| 7429 | mlog_errno(status); |
| 7430 | goto bail; |
| 7431 | } |
| 7432 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 7436 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7437 | status = 0; |
| 7438 | bail: |
| 7439 | if (status < 0) { |
| 7440 | if (*tc) |
| 7441 | ocfs2_free_truncate_context(*tc); |
| 7442 | *tc = NULL; |
| 7443 | } |
| 7444 | mlog_exit_void(); |
| 7445 | return status; |
| 7446 | } |
| 7447 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7448 | /* |
| 7449 | * 'start' is inclusive, 'end' is not. |
| 7450 | */ |
| 7451 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7452 | unsigned int start, unsigned int end, int trunc) |
| 7453 | { |
| 7454 | int ret; |
| 7455 | unsigned int numbytes; |
| 7456 | handle_t *handle; |
| 7457 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7458 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7459 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7460 | |
| 7461 | if (end > i_size_read(inode)) |
| 7462 | end = i_size_read(inode); |
| 7463 | |
| 7464 | BUG_ON(start >= end); |
| 7465 | |
| 7466 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7467 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7468 | !ocfs2_supports_inline_data(osb)) { |
| 7469 | ocfs2_error(inode->i_sb, |
| 7470 | "Inline data flags for inode %llu don't agree! " |
| 7471 | "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
| 7472 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7473 | le16_to_cpu(di->i_dyn_features), |
| 7474 | OCFS2_I(inode)->ip_dyn_features, |
| 7475 | osb->s_feature_incompat); |
| 7476 | ret = -EROFS; |
| 7477 | goto out; |
| 7478 | } |
| 7479 | |
| 7480 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7481 | if (IS_ERR(handle)) { |
| 7482 | ret = PTR_ERR(handle); |
| 7483 | mlog_errno(ret); |
| 7484 | goto out; |
| 7485 | } |
| 7486 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7487 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7488 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7489 | if (ret) { |
| 7490 | mlog_errno(ret); |
| 7491 | goto out_commit; |
| 7492 | } |
| 7493 | |
| 7494 | numbytes = end - start; |
| 7495 | memset(idata->id_data + start, 0, numbytes); |
| 7496 | |
| 7497 | /* |
| 7498 | * No need to worry about the data page here - it's been |
| 7499 | * truncated already and inline data doesn't need it for |
| 7500 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7501 | * later. |
| 7502 | */ |
| 7503 | if (trunc) { |
| 7504 | i_size_write(inode, start); |
| 7505 | di->i_size = cpu_to_le64(start); |
| 7506 | } |
| 7507 | |
| 7508 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7509 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 7510 | |
| 7511 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7512 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7513 | |
| 7514 | ocfs2_journal_dirty(handle, di_bh); |
| 7515 | |
| 7516 | out_commit: |
| 7517 | ocfs2_commit_trans(osb, handle); |
| 7518 | |
| 7519 | out: |
| 7520 | return ret; |
| 7521 | } |
| 7522 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7523 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 7524 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7525 | /* |
| 7526 | * The caller is responsible for completing deallocation |
| 7527 | * before freeing the context. |
| 7528 | */ |
| 7529 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 7530 | mlog(ML_NOTICE, |
| 7531 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7532 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 7533 | brelse(tc->tc_last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7534 | |
| 7535 | kfree(tc); |
| 7536 | } |