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 | /* |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame^] | 86 | * If this extent tree is supported by an extent map, insert |
| 87 | * a record into the map. |
| 88 | */ |
| 89 | void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et, |
| 90 | struct ocfs2_extent_rec *rec); |
| 91 | |
| 92 | /* |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 93 | * If this extent tree is supported by an extent map, truncate the |
| 94 | * map to clusters, |
| 95 | */ |
| 96 | void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et, |
| 97 | u32 clusters); |
| 98 | |
| 99 | /* |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 100 | * If ->eo_insert_check() exists, it is called before rec is |
| 101 | * inserted into the extent tree. It is optional. |
| 102 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 103 | int (*eo_insert_check)(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 104 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 105 | int (*eo_sanity_check)(struct ocfs2_extent_tree *et); |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 106 | |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 107 | /* |
| 108 | * -------------------------------------------------------------- |
| 109 | * The remaining are internal to ocfs2_extent_tree and don't have |
| 110 | * accessor functions |
| 111 | */ |
| 112 | |
| 113 | /* |
| 114 | * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. |
| 115 | * It is required. |
| 116 | */ |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 117 | void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if |
| 121 | * it exists. If it does not, et->et_max_leaf_clusters is set |
| 122 | * to 0 (unlimited). Optional. |
| 123 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 124 | void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 125 | }; |
| 126 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check |
| 130 | * in the methods. |
| 131 | */ |
| 132 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); |
| 133 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 134 | u64 blkno); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 135 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 136 | u32 clusters); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame^] | 137 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 138 | struct ocfs2_extent_rec *rec); |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 139 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 140 | u32 clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 141 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 142 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 143 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 144 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); |
| 145 | static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { |
| 146 | .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, |
| 147 | .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, |
| 148 | .eo_update_clusters = ocfs2_dinode_update_clusters, |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame^] | 149 | .eo_extent_map_insert = ocfs2_dinode_extent_map_insert, |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 150 | .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 151 | .eo_insert_check = ocfs2_dinode_insert_check, |
| 152 | .eo_sanity_check = ocfs2_dinode_sanity_check, |
| 153 | .eo_fill_root_el = ocfs2_dinode_fill_root_el, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 157 | u64 blkno) |
| 158 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 159 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 160 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 161 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 162 | di->i_last_eb_blk = cpu_to_le64(blkno); |
| 163 | } |
| 164 | |
| 165 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 166 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 167 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 168 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 169 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 170 | return le64_to_cpu(di->i_last_eb_blk); |
| 171 | } |
| 172 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 173 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 174 | u32 clusters) |
| 175 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 176 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 177 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 178 | |
| 179 | le32_add_cpu(&di->i_clusters, clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 180 | spin_lock(&oi->ip_lock); |
| 181 | oi->ip_clusters = le32_to_cpu(di->i_clusters); |
| 182 | spin_unlock(&oi->ip_lock); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 183 | } |
| 184 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame^] | 185 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 186 | struct ocfs2_extent_rec *rec) |
| 187 | { |
| 188 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 189 | |
| 190 | ocfs2_extent_map_insert_rec(inode, rec); |
| 191 | } |
| 192 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 193 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 194 | u32 clusters) |
| 195 | { |
| 196 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 197 | |
| 198 | ocfs2_extent_map_trunc(inode, clusters); |
| 199 | } |
| 200 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 201 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 202 | struct ocfs2_extent_rec *rec) |
| 203 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 204 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
| 205 | struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 206 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 207 | BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 208 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 209 | (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 210 | "Device %s, asking for sparse allocation: inode %llu, " |
| 211 | "cpos %u, clusters %u\n", |
| 212 | osb->dev_str, |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 213 | (unsigned long long)oi->ip_blkno, |
| 214 | rec->e_cpos, oi->ip_clusters); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 219 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 220 | { |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 221 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 222 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 223 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 224 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 225 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 226 | return 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 229 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) |
| 230 | { |
| 231 | struct ocfs2_dinode *di = et->et_object; |
| 232 | |
| 233 | et->et_root_el = &di->id2.i_list; |
| 234 | } |
| 235 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 236 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 237 | static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) |
| 238 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 239 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 240 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 241 | et->et_root_el = &vb->vb_xv->xr_list; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 244 | static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 245 | u64 blkno) |
| 246 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 247 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 248 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 249 | vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 253 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 254 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 255 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 256 | return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 257 | } |
| 258 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 259 | static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 260 | u32 clusters) |
| 261 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 262 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 263 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 264 | le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 267 | static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 268 | .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, |
| 269 | .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, |
| 270 | .eo_update_clusters = ocfs2_xattr_value_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 271 | .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 272 | }; |
| 273 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 274 | static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 275 | { |
| 276 | struct ocfs2_xattr_block *xb = et->et_object; |
| 277 | |
| 278 | et->et_root_el = &xb->xb_attrs.xb_root.xt_list; |
| 279 | } |
| 280 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 281 | 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] | 282 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 283 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 284 | et->et_max_leaf_clusters = |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 285 | ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 288 | static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 289 | u64 blkno) |
| 290 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 291 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 292 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 293 | |
| 294 | xt->xt_last_eb_blk = cpu_to_le64(blkno); |
| 295 | } |
| 296 | |
| 297 | static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 298 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 299 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 300 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 301 | |
| 302 | return le64_to_cpu(xt->xt_last_eb_blk); |
| 303 | } |
| 304 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 305 | static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 306 | u32 clusters) |
| 307 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 308 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 309 | |
| 310 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); |
| 311 | } |
| 312 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 313 | static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 314 | .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, |
| 315 | .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, |
| 316 | .eo_update_clusters = ocfs2_xattr_tree_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 317 | .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 318 | .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 319 | }; |
| 320 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 321 | static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 322 | u64 blkno) |
| 323 | { |
| 324 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 325 | |
| 326 | dx_root->dr_last_eb_blk = cpu_to_le64(blkno); |
| 327 | } |
| 328 | |
| 329 | static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 330 | { |
| 331 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 332 | |
| 333 | return le64_to_cpu(dx_root->dr_last_eb_blk); |
| 334 | } |
| 335 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 336 | static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 337 | u32 clusters) |
| 338 | { |
| 339 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 340 | |
| 341 | le32_add_cpu(&dx_root->dr_clusters, clusters); |
| 342 | } |
| 343 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 344 | static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 345 | { |
| 346 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 347 | |
| 348 | BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) |
| 354 | { |
| 355 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 356 | |
| 357 | et->et_root_el = &dx_root->dr_list; |
| 358 | } |
| 359 | |
| 360 | static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { |
| 361 | .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, |
| 362 | .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, |
| 363 | .eo_update_clusters = ocfs2_dx_root_update_clusters, |
| 364 | .eo_sanity_check = ocfs2_dx_root_sanity_check, |
| 365 | .eo_fill_root_el = ocfs2_dx_root_fill_root_el, |
| 366 | }; |
| 367 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 368 | static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, |
| 369 | struct inode *inode, |
| 370 | struct buffer_head *bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 371 | ocfs2_journal_access_func access, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 372 | void *obj, |
| 373 | struct ocfs2_extent_tree_operations *ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 374 | { |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 375 | et->et_ops = ops; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 376 | et->et_root_bh = bh; |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 377 | et->et_ci = INODE_CACHE(inode); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 378 | et->et_root_journal_access = access; |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 379 | if (!obj) |
| 380 | obj = (void *)bh->b_data; |
| 381 | et->et_object = obj; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 382 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 383 | et->et_ops->eo_fill_root_el(et); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 384 | if (!et->et_ops->eo_fill_max_leaf_clusters) |
| 385 | et->et_max_leaf_clusters = 0; |
| 386 | else |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 387 | et->et_ops->eo_fill_max_leaf_clusters(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 388 | } |
| 389 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 390 | void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, |
| 391 | struct inode *inode, |
| 392 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 393 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 394 | __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di, |
| 395 | NULL, &ocfs2_dinode_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 398 | void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 399 | struct inode *inode, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 400 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 401 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 402 | __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb, |
| 403 | NULL, &ocfs2_xattr_tree_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 406 | void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, |
| 407 | struct inode *inode, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 408 | struct ocfs2_xattr_value_buf *vb) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 409 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 410 | __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] | 411 | &ocfs2_xattr_value_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 412 | } |
| 413 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 414 | void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, |
| 415 | struct inode *inode, |
| 416 | struct buffer_head *bh) |
| 417 | { |
| 418 | __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr, |
| 419 | NULL, &ocfs2_dx_root_et_ops); |
| 420 | } |
| 421 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 422 | static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 423 | u64 new_last_eb_blk) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 424 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 425 | 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] | 426 | } |
| 427 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 428 | 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] | 429 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 430 | return et->et_ops->eo_get_last_eb_blk(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 431 | } |
| 432 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 433 | static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 434 | u32 clusters) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 435 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 436 | et->et_ops->eo_update_clusters(et, clusters); |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame^] | 439 | static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et, |
| 440 | struct ocfs2_extent_rec *rec) |
| 441 | { |
| 442 | if (et->et_ops->eo_extent_map_insert) |
| 443 | et->et_ops->eo_extent_map_insert(et, rec); |
| 444 | } |
| 445 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 446 | static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 447 | u32 clusters) |
| 448 | { |
| 449 | if (et->et_ops->eo_extent_map_truncate) |
| 450 | et->et_ops->eo_extent_map_truncate(et, clusters); |
| 451 | } |
| 452 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 453 | static inline int ocfs2_et_root_journal_access(handle_t *handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 454 | struct ocfs2_extent_tree *et, |
| 455 | int type) |
| 456 | { |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 457 | 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] | 458 | type); |
| 459 | } |
| 460 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 461 | static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 462 | struct ocfs2_extent_rec *rec) |
| 463 | { |
| 464 | int ret = 0; |
| 465 | |
| 466 | if (et->et_ops->eo_insert_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 467 | ret = et->et_ops->eo_insert_check(et, rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 468 | return ret; |
| 469 | } |
| 470 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 471 | static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 472 | { |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 473 | int ret = 0; |
| 474 | |
| 475 | if (et->et_ops->eo_sanity_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 476 | ret = et->et_ops->eo_sanity_check(et); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 477 | return ret; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 478 | } |
| 479 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 480 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 481 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 482 | struct ocfs2_extent_block *eb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 483 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 484 | /* |
| 485 | * Structures which describe a path through a btree, and functions to |
| 486 | * manipulate them. |
| 487 | * |
| 488 | * The idea here is to be as generic as possible with the tree |
| 489 | * manipulation code. |
| 490 | */ |
| 491 | struct ocfs2_path_item { |
| 492 | struct buffer_head *bh; |
| 493 | struct ocfs2_extent_list *el; |
| 494 | }; |
| 495 | |
| 496 | #define OCFS2_MAX_PATH_DEPTH 5 |
| 497 | |
| 498 | struct ocfs2_path { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 499 | int p_tree_depth; |
| 500 | ocfs2_journal_access_func p_root_access; |
| 501 | struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | #define path_root_bh(_path) ((_path)->p_node[0].bh) |
| 505 | #define path_root_el(_path) ((_path)->p_node[0].el) |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 506 | #define path_root_access(_path)((_path)->p_root_access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 507 | #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) |
| 508 | #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) |
| 509 | #define path_num_items(_path) ((_path)->p_tree_depth + 1) |
| 510 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 511 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 512 | struct ocfs2_path *path, u32 cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 513 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 514 | struct ocfs2_extent_tree *et, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 515 | struct ocfs2_path *path, |
| 516 | struct ocfs2_extent_rec *insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 517 | /* |
| 518 | * Reset the actual path elements so that we can re-use the structure |
| 519 | * to build another path. Generally, this involves freeing the buffer |
| 520 | * heads. |
| 521 | */ |
| 522 | static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
| 523 | { |
| 524 | int i, start = 0, depth = 0; |
| 525 | struct ocfs2_path_item *node; |
| 526 | |
| 527 | if (keep_root) |
| 528 | start = 1; |
| 529 | |
| 530 | for(i = start; i < path_num_items(path); i++) { |
| 531 | node = &path->p_node[i]; |
| 532 | |
| 533 | brelse(node->bh); |
| 534 | node->bh = NULL; |
| 535 | node->el = NULL; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Tree depth may change during truncate, or insert. If we're |
| 540 | * keeping the root extent list, then make sure that our path |
| 541 | * structure reflects the proper depth. |
| 542 | */ |
| 543 | if (keep_root) |
| 544 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 545 | else |
| 546 | path_root_access(path) = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 547 | |
| 548 | path->p_tree_depth = depth; |
| 549 | } |
| 550 | |
| 551 | static void ocfs2_free_path(struct ocfs2_path *path) |
| 552 | { |
| 553 | if (path) { |
| 554 | ocfs2_reinit_path(path, 0); |
| 555 | kfree(path); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 560 | * All the elements of src into dest. After this call, src could be freed |
| 561 | * without affecting dest. |
| 562 | * |
| 563 | * Both paths should have the same root. Any non-root elements of dest |
| 564 | * will be freed. |
| 565 | */ |
| 566 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 567 | { |
| 568 | int i; |
| 569 | |
| 570 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 571 | BUG_ON(path_root_el(dest) != path_root_el(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 572 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 573 | |
| 574 | ocfs2_reinit_path(dest, 1); |
| 575 | |
| 576 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 577 | dest->p_node[i].bh = src->p_node[i].bh; |
| 578 | dest->p_node[i].el = src->p_node[i].el; |
| 579 | |
| 580 | if (dest->p_node[i].bh) |
| 581 | get_bh(dest->p_node[i].bh); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 586 | * Make the *dest path the same as src and re-initialize src path to |
| 587 | * have a root only. |
| 588 | */ |
| 589 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 590 | { |
| 591 | int i; |
| 592 | |
| 593 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 594 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 595 | |
| 596 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 597 | brelse(dest->p_node[i].bh); |
| 598 | |
| 599 | dest->p_node[i].bh = src->p_node[i].bh; |
| 600 | dest->p_node[i].el = src->p_node[i].el; |
| 601 | |
| 602 | src->p_node[i].bh = NULL; |
| 603 | src->p_node[i].el = NULL; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Insert an extent block at given index. |
| 609 | * |
| 610 | * This will not take an additional reference on eb_bh. |
| 611 | */ |
| 612 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, |
| 613 | struct buffer_head *eb_bh) |
| 614 | { |
| 615 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; |
| 616 | |
| 617 | /* |
| 618 | * Right now, no root bh is an extent block, so this helps |
| 619 | * catch code errors with dinode trees. The assertion can be |
| 620 | * safely removed if we ever need to insert extent block |
| 621 | * structures at the root. |
| 622 | */ |
| 623 | BUG_ON(index == 0); |
| 624 | |
| 625 | path->p_node[index].bh = eb_bh; |
| 626 | path->p_node[index].el = &eb->h_list; |
| 627 | } |
| 628 | |
| 629 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 630 | struct ocfs2_extent_list *root_el, |
| 631 | ocfs2_journal_access_func access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 632 | { |
| 633 | struct ocfs2_path *path; |
| 634 | |
| 635 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
| 636 | |
| 637 | path = kzalloc(sizeof(*path), GFP_NOFS); |
| 638 | if (path) { |
| 639 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); |
| 640 | get_bh(root_bh); |
| 641 | path_root_bh(path) = root_bh; |
| 642 | path_root_el(path) = root_el; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 643 | path_root_access(path) = access; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | return path; |
| 647 | } |
| 648 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 649 | static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) |
| 650 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 651 | return ocfs2_new_path(path_root_bh(path), path_root_el(path), |
| 652 | path_root_access(path)); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et) |
| 656 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 657 | return ocfs2_new_path(et->et_root_bh, et->et_root_el, |
| 658 | et->et_root_journal_access); |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Journal the buffer at depth idx. All idx>0 are extent_blocks, |
| 663 | * otherwise it's the root_access function. |
| 664 | * |
| 665 | * I don't like the way this function's name looks next to |
| 666 | * ocfs2_journal_access_path(), but I don't have a better one. |
| 667 | */ |
| 668 | static int ocfs2_path_bh_journal_access(handle_t *handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 669 | struct ocfs2_caching_info *ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 670 | struct ocfs2_path *path, |
| 671 | int idx) |
| 672 | { |
| 673 | ocfs2_journal_access_func access = path_root_access(path); |
| 674 | |
| 675 | if (!access) |
| 676 | access = ocfs2_journal_access; |
| 677 | |
| 678 | if (idx) |
| 679 | access = ocfs2_journal_access_eb; |
| 680 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 681 | return access(handle, ci, path->p_node[idx].bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 682 | OCFS2_JOURNAL_ACCESS_WRITE); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 685 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 686 | * Convenience function to journal all components in a path. |
| 687 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 688 | static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, |
| 689 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 690 | struct ocfs2_path *path) |
| 691 | { |
| 692 | int i, ret = 0; |
| 693 | |
| 694 | if (!path) |
| 695 | goto out; |
| 696 | |
| 697 | for(i = 0; i < path_num_items(path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 698 | ret = ocfs2_path_bh_journal_access(handle, ci, path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 699 | if (ret < 0) { |
| 700 | mlog_errno(ret); |
| 701 | goto out; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | out: |
| 706 | return ret; |
| 707 | } |
| 708 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 709 | /* |
| 710 | * Return the index of the extent record which contains cluster #v_cluster. |
| 711 | * -1 is returned if it was not found. |
| 712 | * |
| 713 | * Should work fine on interior and exterior nodes. |
| 714 | */ |
| 715 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) |
| 716 | { |
| 717 | int ret = -1; |
| 718 | int i; |
| 719 | struct ocfs2_extent_rec *rec; |
| 720 | u32 rec_end, rec_start, clusters; |
| 721 | |
| 722 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 723 | rec = &el->l_recs[i]; |
| 724 | |
| 725 | rec_start = le32_to_cpu(rec->e_cpos); |
| 726 | clusters = ocfs2_rec_clusters(el, rec); |
| 727 | |
| 728 | rec_end = rec_start + clusters; |
| 729 | |
| 730 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 731 | ret = i; |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 739 | enum ocfs2_contig_type { |
| 740 | CONTIG_NONE = 0, |
| 741 | CONTIG_LEFT, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 742 | CONTIG_RIGHT, |
| 743 | CONTIG_LEFTRIGHT, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 744 | }; |
| 745 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and |
| 749 | * ocfs2_extent_contig only work properly against leaf nodes! |
| 750 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 751 | static int ocfs2_block_extent_contig(struct super_block *sb, |
| 752 | struct ocfs2_extent_rec *ext, |
| 753 | u64 blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 754 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 755 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
| 756 | |
| 757 | blk_end += ocfs2_clusters_to_blocks(sb, |
| 758 | le16_to_cpu(ext->e_leaf_clusters)); |
| 759 | |
| 760 | return blkno == blk_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 763 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
| 764 | struct ocfs2_extent_rec *right) |
| 765 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 766 | u32 left_range; |
| 767 | |
| 768 | left_range = le32_to_cpu(left->e_cpos) + |
| 769 | le16_to_cpu(left->e_leaf_clusters); |
| 770 | |
| 771 | return (left_range == le32_to_cpu(right->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static enum ocfs2_contig_type |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 775 | ocfs2_extent_contig(struct super_block *sb, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 776 | struct ocfs2_extent_rec *ext, |
| 777 | struct ocfs2_extent_rec *insert_rec) |
| 778 | { |
| 779 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); |
| 780 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 781 | /* |
| 782 | * Refuse to coalesce extent records with different flag |
| 783 | * fields - we don't want to mix unwritten extents with user |
| 784 | * data. |
| 785 | */ |
| 786 | if (ext->e_flags != insert_rec->e_flags) |
| 787 | return CONTIG_NONE; |
| 788 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 789 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 790 | ocfs2_block_extent_contig(sb, ext, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 791 | return CONTIG_RIGHT; |
| 792 | |
| 793 | blkno = le64_to_cpu(ext->e_blkno); |
| 794 | if (ocfs2_extents_adjacent(insert_rec, ext) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 795 | ocfs2_block_extent_contig(sb, insert_rec, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 796 | return CONTIG_LEFT; |
| 797 | |
| 798 | return CONTIG_NONE; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * NOTE: We can have pretty much any combination of contiguousness and |
| 803 | * appending. |
| 804 | * |
| 805 | * The usefulness of APPEND_TAIL is more in that it lets us know that |
| 806 | * we'll have to update the path to that leaf. |
| 807 | */ |
| 808 | enum ocfs2_append_type { |
| 809 | APPEND_NONE = 0, |
| 810 | APPEND_TAIL, |
| 811 | }; |
| 812 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 813 | enum ocfs2_split_type { |
| 814 | SPLIT_NONE = 0, |
| 815 | SPLIT_LEFT, |
| 816 | SPLIT_RIGHT, |
| 817 | }; |
| 818 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 819 | struct ocfs2_insert_type { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 820 | enum ocfs2_split_type ins_split; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 821 | enum ocfs2_append_type ins_appending; |
| 822 | enum ocfs2_contig_type ins_contig; |
| 823 | int ins_contig_index; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 824 | int ins_tree_depth; |
| 825 | }; |
| 826 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 827 | struct ocfs2_merge_ctxt { |
| 828 | enum ocfs2_contig_type c_contig_type; |
| 829 | int c_has_empty_extent; |
| 830 | int c_split_covers_rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 831 | }; |
| 832 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 833 | static int ocfs2_validate_extent_block(struct super_block *sb, |
| 834 | struct buffer_head *bh) |
| 835 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 836 | int rc; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 837 | struct ocfs2_extent_block *eb = |
| 838 | (struct ocfs2_extent_block *)bh->b_data; |
| 839 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 840 | mlog(0, "Validating extent block %llu\n", |
| 841 | (unsigned long long)bh->b_blocknr); |
| 842 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 843 | BUG_ON(!buffer_uptodate(bh)); |
| 844 | |
| 845 | /* |
| 846 | * If the ecc fails, we return the error but otherwise |
| 847 | * leave the filesystem running. We know any error is |
| 848 | * local to this block. |
| 849 | */ |
| 850 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 851 | if (rc) { |
| 852 | mlog(ML_ERROR, "Checksum failed for extent block %llu\n", |
| 853 | (unsigned long long)bh->b_blocknr); |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 854 | return rc; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 855 | } |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 856 | |
| 857 | /* |
| 858 | * Errors after here are fatal. |
| 859 | */ |
| 860 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 861 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 862 | ocfs2_error(sb, |
| 863 | "Extent block #%llu has bad signature %.*s", |
| 864 | (unsigned long long)bh->b_blocknr, 7, |
| 865 | eb->h_signature); |
| 866 | return -EINVAL; |
| 867 | } |
| 868 | |
| 869 | if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { |
| 870 | ocfs2_error(sb, |
| 871 | "Extent block #%llu has an invalid h_blkno " |
| 872 | "of %llu", |
| 873 | (unsigned long long)bh->b_blocknr, |
| 874 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | |
| 878 | if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 879 | ocfs2_error(sb, |
| 880 | "Extent block #%llu has an invalid " |
| 881 | "h_fs_generation of #%u", |
| 882 | (unsigned long long)bh->b_blocknr, |
| 883 | le32_to_cpu(eb->h_fs_generation)); |
| 884 | return -EINVAL; |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 890 | 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] | 891 | struct buffer_head **bh) |
| 892 | { |
| 893 | int rc; |
| 894 | struct buffer_head *tmp = *bh; |
| 895 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 896 | rc = ocfs2_read_block(ci, eb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 897 | ocfs2_validate_extent_block); |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 898 | |
| 899 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 900 | if (!rc && !*bh) |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 901 | *bh = tmp; |
| 902 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 903 | return rc; |
| 904 | } |
| 905 | |
| 906 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 907 | /* |
| 908 | * How many free extents have we got before we need more meta data? |
| 909 | */ |
| 910 | int ocfs2_num_free_extents(struct ocfs2_super *osb, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 911 | struct ocfs2_extent_tree *et) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 912 | { |
| 913 | int retval; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 914 | struct ocfs2_extent_list *el = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 915 | struct ocfs2_extent_block *eb; |
| 916 | struct buffer_head *eb_bh = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 917 | u64 last_eb_blk = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 918 | |
| 919 | mlog_entry_void(); |
| 920 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 921 | el = et->et_root_el; |
| 922 | last_eb_blk = ocfs2_et_get_last_eb_blk(et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 923 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 924 | if (last_eb_blk) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 925 | retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, |
| 926 | &eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 927 | if (retval < 0) { |
| 928 | mlog_errno(retval); |
| 929 | goto bail; |
| 930 | } |
| 931 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 932 | el = &eb->h_list; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 933 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 934 | |
| 935 | BUG_ON(el->l_tree_depth != 0); |
| 936 | |
| 937 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); |
| 938 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 939 | brelse(eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 940 | |
| 941 | mlog_exit(retval); |
| 942 | return retval; |
| 943 | } |
| 944 | |
| 945 | /* expects array to already be allocated |
| 946 | * |
| 947 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and |
| 948 | * l_count for you |
| 949 | */ |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 950 | static int ocfs2_create_new_meta_bhs(handle_t *handle, |
| 951 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 952 | int wanted, |
| 953 | struct ocfs2_alloc_context *meta_ac, |
| 954 | struct buffer_head *bhs[]) |
| 955 | { |
| 956 | int count, status, i; |
| 957 | u16 suballoc_bit_start; |
| 958 | u32 num_got; |
| 959 | u64 first_blkno; |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 960 | struct ocfs2_super *osb = |
| 961 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 962 | struct ocfs2_extent_block *eb; |
| 963 | |
| 964 | mlog_entry_void(); |
| 965 | |
| 966 | count = 0; |
| 967 | while (count < wanted) { |
| 968 | status = ocfs2_claim_metadata(osb, |
| 969 | handle, |
| 970 | meta_ac, |
| 971 | wanted - count, |
| 972 | &suballoc_bit_start, |
| 973 | &num_got, |
| 974 | &first_blkno); |
| 975 | if (status < 0) { |
| 976 | mlog_errno(status); |
| 977 | goto bail; |
| 978 | } |
| 979 | |
| 980 | for(i = count; i < (num_got + count); i++) { |
| 981 | bhs[i] = sb_getblk(osb->sb, first_blkno); |
| 982 | if (bhs[i] == NULL) { |
| 983 | status = -EIO; |
| 984 | mlog_errno(status); |
| 985 | goto bail; |
| 986 | } |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 987 | ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 988 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 989 | status = ocfs2_journal_access_eb(handle, et->et_ci, |
| 990 | bhs[i], |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 991 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 992 | if (status < 0) { |
| 993 | mlog_errno(status); |
| 994 | goto bail; |
| 995 | } |
| 996 | |
| 997 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); |
| 998 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; |
| 999 | /* Ok, setup the minimal stuff here. */ |
| 1000 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); |
| 1001 | eb->h_blkno = cpu_to_le64(first_blkno); |
| 1002 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1003 | eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1004 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 1005 | eb->h_list.l_count = |
| 1006 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 1007 | |
| 1008 | suballoc_bit_start++; |
| 1009 | first_blkno++; |
| 1010 | |
| 1011 | /* We'll also be dirtied by the caller, so |
| 1012 | * this isn't absolutely necessary. */ |
| 1013 | status = ocfs2_journal_dirty(handle, bhs[i]); |
| 1014 | if (status < 0) { |
| 1015 | mlog_errno(status); |
| 1016 | goto bail; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | count += num_got; |
| 1021 | } |
| 1022 | |
| 1023 | status = 0; |
| 1024 | bail: |
| 1025 | if (status < 0) { |
| 1026 | for(i = 0; i < wanted; i++) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1027 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1028 | bhs[i] = NULL; |
| 1029 | } |
| 1030 | } |
| 1031 | mlog_exit(status); |
| 1032 | return status; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1036 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 1037 | * |
| 1038 | * Returns the sum of the rightmost extent rec logical offset and |
| 1039 | * cluster count. |
| 1040 | * |
| 1041 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 1042 | * value should be populated into the leftmost new branch records. |
| 1043 | * |
| 1044 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 1045 | * value for the new topmost tree record. |
| 1046 | */ |
| 1047 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 1048 | { |
| 1049 | int i; |
| 1050 | |
| 1051 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1052 | |
| 1053 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1054 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /* |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1058 | * Change range of the branches in the right most path according to the leaf |
| 1059 | * extent block's rightmost record. |
| 1060 | */ |
| 1061 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1062 | struct ocfs2_extent_tree *et) |
| 1063 | { |
| 1064 | int status; |
| 1065 | struct ocfs2_path *path = NULL; |
| 1066 | struct ocfs2_extent_list *el; |
| 1067 | struct ocfs2_extent_rec *rec; |
| 1068 | |
| 1069 | path = ocfs2_new_path_from_et(et); |
| 1070 | if (!path) { |
| 1071 | status = -ENOMEM; |
| 1072 | return status; |
| 1073 | } |
| 1074 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1075 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1076 | if (status < 0) { |
| 1077 | mlog_errno(status); |
| 1078 | goto out; |
| 1079 | } |
| 1080 | |
| 1081 | status = ocfs2_extend_trans(handle, path_num_items(path) + |
| 1082 | handle->h_buffer_credits); |
| 1083 | if (status < 0) { |
| 1084 | mlog_errno(status); |
| 1085 | goto out; |
| 1086 | } |
| 1087 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1088 | status = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1089 | if (status < 0) { |
| 1090 | mlog_errno(status); |
| 1091 | goto out; |
| 1092 | } |
| 1093 | |
| 1094 | el = path_leaf_el(path); |
| 1095 | rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; |
| 1096 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1097 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1098 | |
| 1099 | out: |
| 1100 | ocfs2_free_path(path); |
| 1101 | return status; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1105 | * 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] | 1106 | * 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] | 1107 | * structure. |
| 1108 | * |
| 1109 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1110 | * for the new last extent block. |
| 1111 | * |
| 1112 | * 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] | 1113 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1114 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1115 | static int ocfs2_add_branch(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1116 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1117 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1118 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1119 | struct ocfs2_alloc_context *meta_ac) |
| 1120 | { |
| 1121 | int status, new_blocks, i; |
| 1122 | u64 next_blkno, new_last_eb_blk; |
| 1123 | struct buffer_head *bh; |
| 1124 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1125 | struct ocfs2_extent_block *eb; |
| 1126 | struct ocfs2_extent_list *eb_el; |
| 1127 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1128 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1129 | |
| 1130 | mlog_entry_void(); |
| 1131 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1132 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1133 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1134 | if (eb_bh) { |
| 1135 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1136 | el = &eb->h_list; |
| 1137 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1138 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1139 | |
| 1140 | /* we never add a branch to a leaf. */ |
| 1141 | BUG_ON(!el->l_tree_depth); |
| 1142 | |
| 1143 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1144 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1145 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1146 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1147 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1148 | |
| 1149 | /* |
| 1150 | * If there is a gap before the root end and the real end |
| 1151 | * of the righmost leaf block, we need to remove the gap |
| 1152 | * between new_cpos and root_end first so that the tree |
| 1153 | * is consistent after we add a new branch(it will start |
| 1154 | * from new_cpos). |
| 1155 | */ |
| 1156 | if (root_end > new_cpos) { |
| 1157 | mlog(0, "adjust the cluster end from %u to %u\n", |
| 1158 | root_end, new_cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1159 | status = ocfs2_adjust_rightmost_branch(handle, et); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1160 | if (status) { |
| 1161 | mlog_errno(status); |
| 1162 | goto bail; |
| 1163 | } |
| 1164 | } |
| 1165 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1166 | /* allocate the number of new eb blocks we need */ |
| 1167 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1168 | GFP_KERNEL); |
| 1169 | if (!new_eb_bhs) { |
| 1170 | status = -ENOMEM; |
| 1171 | mlog_errno(status); |
| 1172 | goto bail; |
| 1173 | } |
| 1174 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1175 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1176 | meta_ac, new_eb_bhs); |
| 1177 | if (status < 0) { |
| 1178 | mlog_errno(status); |
| 1179 | goto bail; |
| 1180 | } |
| 1181 | |
| 1182 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1183 | * linked with the rest of the tree. |
| 1184 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1185 | * |
| 1186 | * when we leave the loop, new_last_eb_blk will point to the |
| 1187 | * newest leaf, and next_blkno will point to the topmost extent |
| 1188 | * block. */ |
| 1189 | next_blkno = new_last_eb_blk = 0; |
| 1190 | for(i = 0; i < new_blocks; i++) { |
| 1191 | bh = new_eb_bhs[i]; |
| 1192 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1193 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1194 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1195 | eb_el = &eb->h_list; |
| 1196 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1197 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1198 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1199 | if (status < 0) { |
| 1200 | mlog_errno(status); |
| 1201 | goto bail; |
| 1202 | } |
| 1203 | |
| 1204 | eb->h_next_leaf_blk = 0; |
| 1205 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1206 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1207 | /* |
| 1208 | * This actually counts as an empty extent as |
| 1209 | * c_clusters == 0 |
| 1210 | */ |
| 1211 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1212 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1213 | /* |
| 1214 | * eb_el isn't always an interior node, but even leaf |
| 1215 | * nodes want a zero'd flags and reserved field so |
| 1216 | * this gets the whole 32 bits regardless of use. |
| 1217 | */ |
| 1218 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1219 | if (!eb_el->l_tree_depth) |
| 1220 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1221 | |
| 1222 | status = ocfs2_journal_dirty(handle, bh); |
| 1223 | if (status < 0) { |
| 1224 | mlog_errno(status); |
| 1225 | goto bail; |
| 1226 | } |
| 1227 | |
| 1228 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1229 | } |
| 1230 | |
| 1231 | /* This is a bit hairy. We want to update up to three blocks |
| 1232 | * here without leaving any of them in an inconsistent state |
| 1233 | * in case of error. We don't have to worry about |
| 1234 | * journal_dirty erroring as it won't unless we've aborted the |
| 1235 | * handle (in which case we would never be here) so reserving |
| 1236 | * the write with journal_access is all we need to do. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1237 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1238 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1239 | if (status < 0) { |
| 1240 | mlog_errno(status); |
| 1241 | goto bail; |
| 1242 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1243 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1244 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1245 | if (status < 0) { |
| 1246 | mlog_errno(status); |
| 1247 | goto bail; |
| 1248 | } |
| 1249 | if (eb_bh) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1250 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1251 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1252 | if (status < 0) { |
| 1253 | mlog_errno(status); |
| 1254 | goto bail; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1259 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1260 | i = le16_to_cpu(el->l_next_free_rec); |
| 1261 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1262 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1263 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1264 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1265 | |
| 1266 | /* fe needs a new last extent block pointer, as does the |
| 1267 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1268 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1269 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1270 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1271 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1272 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1273 | status = ocfs2_journal_dirty(handle, *last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1274 | if (status < 0) |
| 1275 | mlog_errno(status); |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1276 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1277 | if (status < 0) |
| 1278 | mlog_errno(status); |
| 1279 | if (eb_bh) { |
| 1280 | status = ocfs2_journal_dirty(handle, eb_bh); |
| 1281 | if (status < 0) |
| 1282 | mlog_errno(status); |
| 1283 | } |
| 1284 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1285 | /* |
| 1286 | * Some callers want to track the rightmost leaf so pass it |
| 1287 | * back here. |
| 1288 | */ |
| 1289 | brelse(*last_eb_bh); |
| 1290 | get_bh(new_eb_bhs[0]); |
| 1291 | *last_eb_bh = new_eb_bhs[0]; |
| 1292 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1293 | status = 0; |
| 1294 | bail: |
| 1295 | if (new_eb_bhs) { |
| 1296 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1297 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1298 | kfree(new_eb_bhs); |
| 1299 | } |
| 1300 | |
| 1301 | mlog_exit(status); |
| 1302 | return status; |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * adds another level to the allocation tree. |
| 1307 | * returns back the new extent block so you can add a branch to it |
| 1308 | * after this call. |
| 1309 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1310 | static int ocfs2_shift_tree_depth(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1311 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1312 | struct ocfs2_alloc_context *meta_ac, |
| 1313 | struct buffer_head **ret_new_eb_bh) |
| 1314 | { |
| 1315 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1316 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1317 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1318 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1319 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1320 | struct ocfs2_extent_list *eb_el; |
| 1321 | |
| 1322 | mlog_entry_void(); |
| 1323 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1324 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1325 | &new_eb_bh); |
| 1326 | if (status < 0) { |
| 1327 | mlog_errno(status); |
| 1328 | goto bail; |
| 1329 | } |
| 1330 | |
| 1331 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1332 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1333 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1334 | |
| 1335 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1336 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1337 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1338 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1339 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1340 | if (status < 0) { |
| 1341 | mlog_errno(status); |
| 1342 | goto bail; |
| 1343 | } |
| 1344 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1345 | /* copy the root extent list data into the new extent block */ |
| 1346 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1347 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1348 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1349 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1350 | |
| 1351 | status = ocfs2_journal_dirty(handle, new_eb_bh); |
| 1352 | if (status < 0) { |
| 1353 | mlog_errno(status); |
| 1354 | goto bail; |
| 1355 | } |
| 1356 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1357 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1358 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1359 | if (status < 0) { |
| 1360 | mlog_errno(status); |
| 1361 | goto bail; |
| 1362 | } |
| 1363 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1364 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1365 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1366 | /* update root_bh now */ |
| 1367 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1368 | root_el->l_recs[0].e_cpos = 0; |
| 1369 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1370 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1371 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1372 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1373 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1374 | |
| 1375 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1376 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1377 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1378 | 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] | 1379 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1380 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1381 | if (status < 0) { |
| 1382 | mlog_errno(status); |
| 1383 | goto bail; |
| 1384 | } |
| 1385 | |
| 1386 | *ret_new_eb_bh = new_eb_bh; |
| 1387 | new_eb_bh = NULL; |
| 1388 | status = 0; |
| 1389 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1390 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1391 | |
| 1392 | mlog_exit(status); |
| 1393 | return status; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1397 | * Should only be called when there is no space left in any of the |
| 1398 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1399 | * non-leaf extent block with room for new records. There are three |
| 1400 | * valid results of this search: |
| 1401 | * |
| 1402 | * 1) a lowest extent block is found, then we pass it back in |
| 1403 | * *lowest_eb_bh and return '0' |
| 1404 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1405 | * 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] | 1406 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1407 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1408 | * 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] | 1409 | * which case we return > 0 |
| 1410 | * |
| 1411 | * return status < 0 indicates an error. |
| 1412 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1413 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1414 | struct buffer_head **target_bh) |
| 1415 | { |
| 1416 | int status = 0, i; |
| 1417 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1418 | struct ocfs2_extent_block *eb; |
| 1419 | struct ocfs2_extent_list *el; |
| 1420 | struct buffer_head *bh = NULL; |
| 1421 | struct buffer_head *lowest_bh = NULL; |
| 1422 | |
| 1423 | mlog_entry_void(); |
| 1424 | |
| 1425 | *target_bh = NULL; |
| 1426 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1427 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1428 | |
| 1429 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1430 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1431 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1432 | "Owner %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1433 | "extent list (next_free_rec == 0)", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1434 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1435 | status = -EIO; |
| 1436 | goto bail; |
| 1437 | } |
| 1438 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1439 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1440 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1441 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1442 | "Owner %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1443 | "list where extent # %d has no physical " |
| 1444 | "block start", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1445 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1446 | status = -EIO; |
| 1447 | goto bail; |
| 1448 | } |
| 1449 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1450 | brelse(bh); |
| 1451 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1452 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1453 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1454 | if (status < 0) { |
| 1455 | mlog_errno(status); |
| 1456 | goto bail; |
| 1457 | } |
| 1458 | |
| 1459 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1460 | el = &eb->h_list; |
| 1461 | |
| 1462 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1463 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1464 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1465 | lowest_bh = bh; |
| 1466 | get_bh(lowest_bh); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* If we didn't find one and the fe doesn't have any room, |
| 1471 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1472 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1473 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1474 | status = 1; |
| 1475 | |
| 1476 | *target_bh = lowest_bh; |
| 1477 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1478 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1479 | |
| 1480 | mlog_exit(status); |
| 1481 | return status; |
| 1482 | } |
| 1483 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1484 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1485 | * Grow a b-tree so that it has more records. |
| 1486 | * |
| 1487 | * We might shift the tree depth in which case existing paths should |
| 1488 | * be considered invalid. |
| 1489 | * |
| 1490 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1491 | * |
| 1492 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1493 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1494 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 1495 | int *final_depth, struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1496 | struct ocfs2_alloc_context *meta_ac) |
| 1497 | { |
| 1498 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1499 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1500 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1501 | struct buffer_head *bh = NULL; |
| 1502 | |
| 1503 | BUG_ON(meta_ac == NULL); |
| 1504 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1505 | shift = ocfs2_find_branch_target(et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1506 | if (shift < 0) { |
| 1507 | ret = shift; |
| 1508 | mlog_errno(ret); |
| 1509 | goto out; |
| 1510 | } |
| 1511 | |
| 1512 | /* We traveled all the way to the bottom of the allocation tree |
| 1513 | * and didn't find room for any more extents - we need to add |
| 1514 | * another tree level */ |
| 1515 | if (shift) { |
| 1516 | BUG_ON(bh); |
| 1517 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 1518 | |
| 1519 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1520 | * the new extent block (so we can pass that to |
| 1521 | * ocfs2_add_branch). */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1522 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1523 | if (ret < 0) { |
| 1524 | mlog_errno(ret); |
| 1525 | goto out; |
| 1526 | } |
| 1527 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1528 | if (depth == 1) { |
| 1529 | /* |
| 1530 | * Special case: we have room now if we shifted from |
| 1531 | * tree_depth 0, so no more work needs to be done. |
| 1532 | * |
| 1533 | * We won't be calling add_branch, so pass |
| 1534 | * back *last_eb_bh as the new leaf. At depth |
| 1535 | * zero, it should always be null so there's |
| 1536 | * no reason to brelse. |
| 1537 | */ |
| 1538 | BUG_ON(*last_eb_bh); |
| 1539 | get_bh(bh); |
| 1540 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1541 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1542 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1546 | * the new data. */ |
| 1547 | mlog(0, "add branch. bh = %p\n", bh); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1548 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1549 | meta_ac); |
| 1550 | if (ret < 0) { |
| 1551 | mlog_errno(ret); |
| 1552 | goto out; |
| 1553 | } |
| 1554 | |
| 1555 | out: |
| 1556 | if (final_depth) |
| 1557 | *final_depth = depth; |
| 1558 | brelse(bh); |
| 1559 | return ret; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1563 | * This function will discard the rightmost extent record. |
| 1564 | */ |
| 1565 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1566 | { |
| 1567 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1568 | int count = le16_to_cpu(el->l_count); |
| 1569 | unsigned int num_bytes; |
| 1570 | |
| 1571 | BUG_ON(!next_free); |
| 1572 | /* This will cause us to go off the end of our extent list. */ |
| 1573 | BUG_ON(next_free >= count); |
| 1574 | |
| 1575 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1576 | |
| 1577 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1578 | } |
| 1579 | |
| 1580 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1581 | struct ocfs2_extent_rec *insert_rec) |
| 1582 | { |
| 1583 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1584 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1585 | struct ocfs2_extent_rec *rec; |
| 1586 | |
| 1587 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1588 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1589 | |
| 1590 | BUG_ON(!next_free); |
| 1591 | |
| 1592 | /* 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] | 1593 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1594 | |
| 1595 | /* |
| 1596 | * The easiest way to approach this is to just remove the |
| 1597 | * empty extent and temporarily decrement next_free. |
| 1598 | */ |
| 1599 | if (has_empty) { |
| 1600 | /* |
| 1601 | * If next_free was 1 (only an empty extent), this |
| 1602 | * loop won't execute, which is fine. We still want |
| 1603 | * the decrement above to happen. |
| 1604 | */ |
| 1605 | for(i = 0; i < (next_free - 1); i++) |
| 1606 | el->l_recs[i] = el->l_recs[i+1]; |
| 1607 | |
| 1608 | next_free--; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Figure out what the new record index should be. |
| 1613 | */ |
| 1614 | for(i = 0; i < next_free; i++) { |
| 1615 | rec = &el->l_recs[i]; |
| 1616 | |
| 1617 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1618 | break; |
| 1619 | } |
| 1620 | insert_index = i; |
| 1621 | |
| 1622 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1623 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1624 | |
| 1625 | BUG_ON(insert_index < 0); |
| 1626 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1627 | BUG_ON(insert_index > next_free); |
| 1628 | |
| 1629 | /* |
| 1630 | * No need to memmove if we're just adding to the tail. |
| 1631 | */ |
| 1632 | if (insert_index != next_free) { |
| 1633 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1634 | |
| 1635 | num_bytes = next_free - insert_index; |
| 1636 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1637 | memmove(&el->l_recs[insert_index + 1], |
| 1638 | &el->l_recs[insert_index], |
| 1639 | num_bytes); |
| 1640 | } |
| 1641 | |
| 1642 | /* |
| 1643 | * Either we had an empty extent, and need to re-increment or |
| 1644 | * there was no empty extent on a non full rightmost leaf node, |
| 1645 | * in which case we still need to increment. |
| 1646 | */ |
| 1647 | next_free++; |
| 1648 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1649 | /* |
| 1650 | * Make sure none of the math above just messed up our tree. |
| 1651 | */ |
| 1652 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1653 | |
| 1654 | el->l_recs[insert_index] = *insert_rec; |
| 1655 | |
| 1656 | } |
| 1657 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1658 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1659 | { |
| 1660 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1661 | |
| 1662 | BUG_ON(num_recs == 0); |
| 1663 | |
| 1664 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1665 | num_recs--; |
| 1666 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1667 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1668 | memset(&el->l_recs[num_recs], 0, |
| 1669 | sizeof(struct ocfs2_extent_rec)); |
| 1670 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1671 | } |
| 1672 | } |
| 1673 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1674 | /* |
| 1675 | * Create an empty extent record . |
| 1676 | * |
| 1677 | * l_next_free_rec may be updated. |
| 1678 | * |
| 1679 | * If an empty extent already exists do nothing. |
| 1680 | */ |
| 1681 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1682 | { |
| 1683 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1684 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1685 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1686 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1687 | if (next_free == 0) |
| 1688 | goto set_and_inc; |
| 1689 | |
| 1690 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1691 | return; |
| 1692 | |
| 1693 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1694 | "Asked to create an empty extent in a full list:\n" |
| 1695 | "count = %u, tree depth = %u", |
| 1696 | le16_to_cpu(el->l_count), |
| 1697 | le16_to_cpu(el->l_tree_depth)); |
| 1698 | |
| 1699 | ocfs2_shift_records_right(el); |
| 1700 | |
| 1701 | set_and_inc: |
| 1702 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1703 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1708 | * the lowest level tree node which contains a path to both leafs. This |
| 1709 | * resulting set of information can be used to form a complete "subtree" |
| 1710 | * |
| 1711 | * This function is passed two full paths from the dinode down to a |
| 1712 | * pair of adjacent leaves. It's task is to figure out which path |
| 1713 | * index contains the subtree root - this can be the root index itself |
| 1714 | * in a worst-case rotation. |
| 1715 | * |
| 1716 | * The array index of the subtree root is passed back. |
| 1717 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1718 | static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1719 | struct ocfs2_path *left, |
| 1720 | struct ocfs2_path *right) |
| 1721 | { |
| 1722 | int i = 0; |
| 1723 | |
| 1724 | /* |
| 1725 | * Check that the caller passed in two paths from the same tree. |
| 1726 | */ |
| 1727 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1728 | |
| 1729 | do { |
| 1730 | i++; |
| 1731 | |
| 1732 | /* |
| 1733 | * The caller didn't pass two adjacent paths. |
| 1734 | */ |
| 1735 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1736 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1737 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1738 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1739 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1740 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1741 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1742 | } while (left->p_node[i].bh->b_blocknr == |
| 1743 | right->p_node[i].bh->b_blocknr); |
| 1744 | |
| 1745 | return i - 1; |
| 1746 | } |
| 1747 | |
| 1748 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1749 | |
| 1750 | /* |
| 1751 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1752 | * |
| 1753 | * This code can be called with a cpos larger than the tree, in which |
| 1754 | * case it will return the rightmost path. |
| 1755 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1756 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1757 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1758 | path_insert_t *func, void *data) |
| 1759 | { |
| 1760 | int i, ret = 0; |
| 1761 | u32 range; |
| 1762 | u64 blkno; |
| 1763 | struct buffer_head *bh = NULL; |
| 1764 | struct ocfs2_extent_block *eb; |
| 1765 | struct ocfs2_extent_list *el; |
| 1766 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1767 | |
| 1768 | el = root_el; |
| 1769 | while (el->l_tree_depth) { |
| 1770 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1771 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1772 | "Owner %llu has empty extent list at " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1773 | "depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1774 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1775 | le16_to_cpu(el->l_tree_depth)); |
| 1776 | ret = -EROFS; |
| 1777 | goto out; |
| 1778 | |
| 1779 | } |
| 1780 | |
| 1781 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1782 | rec = &el->l_recs[i]; |
| 1783 | |
| 1784 | /* |
| 1785 | * In the case that cpos is off the allocation |
| 1786 | * tree, this should just wind up returning the |
| 1787 | * rightmost record. |
| 1788 | */ |
| 1789 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1790 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1791 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1792 | break; |
| 1793 | } |
| 1794 | |
| 1795 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1796 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1797 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1798 | "Owner %llu has bad blkno in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1799 | "at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1800 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1801 | le16_to_cpu(el->l_tree_depth), i); |
| 1802 | ret = -EROFS; |
| 1803 | goto out; |
| 1804 | } |
| 1805 | |
| 1806 | brelse(bh); |
| 1807 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1808 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1809 | if (ret) { |
| 1810 | mlog_errno(ret); |
| 1811 | goto out; |
| 1812 | } |
| 1813 | |
| 1814 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1815 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1816 | |
| 1817 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1818 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1819 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1820 | "Owner %llu has bad count in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1821 | "at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1822 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1823 | (unsigned long long)bh->b_blocknr, |
| 1824 | le16_to_cpu(el->l_next_free_rec), |
| 1825 | le16_to_cpu(el->l_count)); |
| 1826 | ret = -EROFS; |
| 1827 | goto out; |
| 1828 | } |
| 1829 | |
| 1830 | if (func) |
| 1831 | func(data, bh); |
| 1832 | } |
| 1833 | |
| 1834 | out: |
| 1835 | /* |
| 1836 | * Catch any trailing bh that the loop didn't handle. |
| 1837 | */ |
| 1838 | brelse(bh); |
| 1839 | |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Given an initialized path (that is, it has a valid root extent |
| 1845 | * list), this function will traverse the btree in search of the path |
| 1846 | * which would contain cpos. |
| 1847 | * |
| 1848 | * The path traveled is recorded in the path structure. |
| 1849 | * |
| 1850 | * Note that this will not do any comparisons on leaf node extent |
| 1851 | * records, so it will work fine in the case that we just added a tree |
| 1852 | * branch. |
| 1853 | */ |
| 1854 | struct find_path_data { |
| 1855 | int index; |
| 1856 | struct ocfs2_path *path; |
| 1857 | }; |
| 1858 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1859 | { |
| 1860 | struct find_path_data *fp = data; |
| 1861 | |
| 1862 | get_bh(bh); |
| 1863 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1864 | fp->index++; |
| 1865 | } |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1866 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1867 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1868 | { |
| 1869 | struct find_path_data data; |
| 1870 | |
| 1871 | data.index = 1; |
| 1872 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1873 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1874 | find_path_ins, &data); |
| 1875 | } |
| 1876 | |
| 1877 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1878 | { |
| 1879 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1880 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1881 | struct buffer_head **ret = data; |
| 1882 | |
| 1883 | /* We want to retain only the leaf block. */ |
| 1884 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1885 | get_bh(bh); |
| 1886 | *ret = bh; |
| 1887 | } |
| 1888 | } |
| 1889 | /* |
| 1890 | * Find the leaf block in the tree which would contain cpos. No |
| 1891 | * checking of the actual leaf is done. |
| 1892 | * |
| 1893 | * Some paths want to call this instead of allocating a path structure |
| 1894 | * and calling ocfs2_find_path(). |
| 1895 | * |
| 1896 | * This function doesn't handle non btree extent lists. |
| 1897 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1898 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1899 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1900 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1901 | { |
| 1902 | int ret; |
| 1903 | struct buffer_head *bh = NULL; |
| 1904 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1905 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1906 | if (ret) { |
| 1907 | mlog_errno(ret); |
| 1908 | goto out; |
| 1909 | } |
| 1910 | |
| 1911 | *leaf_bh = bh; |
| 1912 | out: |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | /* |
| 1917 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1918 | * |
| 1919 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1920 | * we need to fix up the extent records above the changes to reflect |
| 1921 | * the new changes. |
| 1922 | * |
| 1923 | * left_rec: the record on the left. |
| 1924 | * left_child_el: is the child list pointed to by left_rec |
| 1925 | * right_rec: the record to the right of left_rec |
| 1926 | * right_child_el: is the child list pointed to by right_rec |
| 1927 | * |
| 1928 | * By definition, this only works on interior nodes. |
| 1929 | */ |
| 1930 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 1931 | struct ocfs2_extent_list *left_child_el, |
| 1932 | struct ocfs2_extent_rec *right_rec, |
| 1933 | struct ocfs2_extent_list *right_child_el) |
| 1934 | { |
| 1935 | u32 left_clusters, right_end; |
| 1936 | |
| 1937 | /* |
| 1938 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1939 | * the leftmost record in their child list. Their cluster |
| 1940 | * count covers the full theoretical range of their child list |
| 1941 | * - the range between their cpos and the cpos of the record |
| 1942 | * immediately to their right. |
| 1943 | */ |
| 1944 | 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] | 1945 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 1946 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1947 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1948 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1949 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1950 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1951 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1952 | |
| 1953 | /* |
| 1954 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1955 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1956 | * updating e_cpos to keep the same highest cluster count. |
| 1957 | */ |
| 1958 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1959 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1960 | |
| 1961 | right_rec->e_cpos = left_rec->e_cpos; |
| 1962 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 1963 | |
| 1964 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1965 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * Adjust the adjacent root node records involved in a |
| 1970 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 1971 | * find it's index in the root list. |
| 1972 | */ |
| 1973 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 1974 | struct ocfs2_extent_list *left_el, |
| 1975 | struct ocfs2_extent_list *right_el, |
| 1976 | u64 left_el_blkno) |
| 1977 | { |
| 1978 | int i; |
| 1979 | |
| 1980 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 1981 | le16_to_cpu(left_el->l_tree_depth)); |
| 1982 | |
| 1983 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 1984 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 1985 | break; |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * The path walking code should have never returned a root and |
| 1990 | * two paths which are not adjacent. |
| 1991 | */ |
| 1992 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 1993 | |
| 1994 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 1995 | &root_el->l_recs[i + 1], right_el); |
| 1996 | } |
| 1997 | |
| 1998 | /* |
| 1999 | * We've changed a leaf block (in right_path) and need to reflect that |
| 2000 | * change back up the subtree. |
| 2001 | * |
| 2002 | * This happens in multiple places: |
| 2003 | * - When we've moved an extent record from the left path leaf to the right |
| 2004 | * path leaf to make room for an empty extent in the left path leaf. |
| 2005 | * - When our insert into the right path leaf is at the leftmost edge |
| 2006 | * and requires an update of the path immediately to it's left. This |
| 2007 | * 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] | 2008 | * - When we've adjusted the last extent record in the left path leaf and the |
| 2009 | * 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] | 2010 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2011 | static void ocfs2_complete_edge_insert(handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2012 | struct ocfs2_path *left_path, |
| 2013 | struct ocfs2_path *right_path, |
| 2014 | int subtree_index) |
| 2015 | { |
| 2016 | int ret, i, idx; |
| 2017 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 2018 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 2019 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2020 | |
| 2021 | /* |
| 2022 | * Update the counts and position values within all the |
| 2023 | * interior nodes to reflect the leaf rotation we just did. |
| 2024 | * |
| 2025 | * The root node is handled below the loop. |
| 2026 | * |
| 2027 | * We begin the loop with right_el and left_el pointing to the |
| 2028 | * leaf lists and work our way up. |
| 2029 | * |
| 2030 | * NOTE: within this loop, left_el and right_el always refer |
| 2031 | * to the *child* lists. |
| 2032 | */ |
| 2033 | left_el = path_leaf_el(left_path); |
| 2034 | right_el = path_leaf_el(right_path); |
| 2035 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 2036 | mlog(0, "Adjust records at index %u\n", i); |
| 2037 | |
| 2038 | /* |
| 2039 | * One nice property of knowing that all of these |
| 2040 | * nodes are below the root is that we only deal with |
| 2041 | * the leftmost right node record and the rightmost |
| 2042 | * left node record. |
| 2043 | */ |
| 2044 | el = left_path->p_node[i].el; |
| 2045 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2046 | left_rec = &el->l_recs[idx]; |
| 2047 | |
| 2048 | el = right_path->p_node[i].el; |
| 2049 | right_rec = &el->l_recs[0]; |
| 2050 | |
| 2051 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 2052 | right_el); |
| 2053 | |
| 2054 | ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2055 | if (ret) |
| 2056 | mlog_errno(ret); |
| 2057 | |
| 2058 | ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
| 2059 | if (ret) |
| 2060 | mlog_errno(ret); |
| 2061 | |
| 2062 | /* |
| 2063 | * Setup our list pointers now so that the current |
| 2064 | * parents become children in the next iteration. |
| 2065 | */ |
| 2066 | left_el = left_path->p_node[i].el; |
| 2067 | right_el = right_path->p_node[i].el; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * At the root node, adjust the two adjacent records which |
| 2072 | * begin our path to the leaves. |
| 2073 | */ |
| 2074 | |
| 2075 | el = left_path->p_node[subtree_index].el; |
| 2076 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2077 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2078 | |
| 2079 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2080 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2081 | |
| 2082 | root_bh = left_path->p_node[subtree_index].bh; |
| 2083 | |
| 2084 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 2085 | if (ret) |
| 2086 | mlog_errno(ret); |
| 2087 | } |
| 2088 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2089 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2090 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2091 | struct ocfs2_path *left_path, |
| 2092 | struct ocfs2_path *right_path, |
| 2093 | int subtree_index) |
| 2094 | { |
| 2095 | int ret, i; |
| 2096 | struct buffer_head *right_leaf_bh; |
| 2097 | struct buffer_head *left_leaf_bh = NULL; |
| 2098 | struct buffer_head *root_bh; |
| 2099 | struct ocfs2_extent_list *right_el, *left_el; |
| 2100 | struct ocfs2_extent_rec move_rec; |
| 2101 | |
| 2102 | left_leaf_bh = path_leaf_bh(left_path); |
| 2103 | left_el = path_leaf_el(left_path); |
| 2104 | |
| 2105 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2106 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2107 | "Inode %llu has non-full interior leaf node %llu" |
| 2108 | "(next free = %u)", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2109 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2110 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2111 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2112 | return -EROFS; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * This extent block may already have an empty record, so we |
| 2117 | * return early if so. |
| 2118 | */ |
| 2119 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2120 | return 0; |
| 2121 | |
| 2122 | root_bh = left_path->p_node[subtree_index].bh; |
| 2123 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2124 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2125 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2126 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2127 | if (ret) { |
| 2128 | mlog_errno(ret); |
| 2129 | goto out; |
| 2130 | } |
| 2131 | |
| 2132 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2133 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2134 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2135 | if (ret) { |
| 2136 | mlog_errno(ret); |
| 2137 | goto out; |
| 2138 | } |
| 2139 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2140 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2141 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2142 | if (ret) { |
| 2143 | mlog_errno(ret); |
| 2144 | goto out; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | right_leaf_bh = path_leaf_bh(right_path); |
| 2149 | right_el = path_leaf_el(right_path); |
| 2150 | |
| 2151 | /* This is a code error, not a disk corruption. */ |
| 2152 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2153 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2154 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2155 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2156 | |
| 2157 | ocfs2_create_empty_extent(right_el); |
| 2158 | |
| 2159 | ret = ocfs2_journal_dirty(handle, right_leaf_bh); |
| 2160 | if (ret) { |
| 2161 | mlog_errno(ret); |
| 2162 | goto out; |
| 2163 | } |
| 2164 | |
| 2165 | /* Do the copy now. */ |
| 2166 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2167 | move_rec = left_el->l_recs[i]; |
| 2168 | right_el->l_recs[0] = move_rec; |
| 2169 | |
| 2170 | /* |
| 2171 | * Clear out the record we just copied and shift everything |
| 2172 | * over, leaving an empty extent in the left leaf. |
| 2173 | * |
| 2174 | * We temporarily subtract from next_free_rec so that the |
| 2175 | * shift will lose the tail record (which is now defunct). |
| 2176 | */ |
| 2177 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2178 | ocfs2_shift_records_right(left_el); |
| 2179 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2180 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2181 | |
| 2182 | ret = ocfs2_journal_dirty(handle, left_leaf_bh); |
| 2183 | if (ret) { |
| 2184 | mlog_errno(ret); |
| 2185 | goto out; |
| 2186 | } |
| 2187 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2188 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2189 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2190 | |
| 2191 | out: |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Given a full path, determine what cpos value would return us a path |
| 2197 | * containing the leaf immediately to the left of the current one. |
| 2198 | * |
| 2199 | * Will return zero if the path passed in is already the leftmost path. |
| 2200 | */ |
| 2201 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2202 | struct ocfs2_path *path, u32 *cpos) |
| 2203 | { |
| 2204 | int i, j, ret = 0; |
| 2205 | u64 blkno; |
| 2206 | struct ocfs2_extent_list *el; |
| 2207 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2208 | BUG_ON(path->p_tree_depth == 0); |
| 2209 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2210 | *cpos = 0; |
| 2211 | |
| 2212 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2213 | |
| 2214 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2215 | i = path->p_tree_depth - 1; |
| 2216 | while (i >= 0) { |
| 2217 | el = path->p_node[i].el; |
| 2218 | |
| 2219 | /* |
| 2220 | * Find the extent record just before the one in our |
| 2221 | * path. |
| 2222 | */ |
| 2223 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2224 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2225 | if (j == 0) { |
| 2226 | if (i == 0) { |
| 2227 | /* |
| 2228 | * We've determined that the |
| 2229 | * path specified is already |
| 2230 | * the leftmost one - return a |
| 2231 | * cpos of zero. |
| 2232 | */ |
| 2233 | goto out; |
| 2234 | } |
| 2235 | /* |
| 2236 | * The leftmost record points to our |
| 2237 | * leaf - we need to travel up the |
| 2238 | * tree one level. |
| 2239 | */ |
| 2240 | goto next_node; |
| 2241 | } |
| 2242 | |
| 2243 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2244 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2245 | &el->l_recs[j - 1]); |
| 2246 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2247 | goto out; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /* |
| 2252 | * If we got here, we never found a valid node where |
| 2253 | * the tree indicated one should be. |
| 2254 | */ |
| 2255 | ocfs2_error(sb, |
| 2256 | "Invalid extent tree at extent block %llu\n", |
| 2257 | (unsigned long long)blkno); |
| 2258 | ret = -EROFS; |
| 2259 | goto out; |
| 2260 | |
| 2261 | next_node: |
| 2262 | blkno = path->p_node[i].bh->b_blocknr; |
| 2263 | i--; |
| 2264 | } |
| 2265 | |
| 2266 | out: |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2270 | /* |
| 2271 | * Extend the transaction by enough credits to complete the rotation, |
| 2272 | * and still leave at least the original number of credits allocated |
| 2273 | * to this transaction. |
| 2274 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2275 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2276 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2277 | struct ocfs2_path *path) |
| 2278 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2279 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2280 | |
| 2281 | if (handle->h_buffer_credits < credits) |
| 2282 | return ocfs2_extend_trans(handle, credits); |
| 2283 | |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | /* |
| 2288 | * Trap the case where we're inserting into the theoretical range past |
| 2289 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2290 | * whose cpos is less than ours into the right leaf. |
| 2291 | * |
| 2292 | * It's only necessary to look at the rightmost record of the left |
| 2293 | * leaf because the logic that calls us should ensure that the |
| 2294 | * theoretical ranges in the path components above the leaves are |
| 2295 | * correct. |
| 2296 | */ |
| 2297 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2298 | u32 insert_cpos) |
| 2299 | { |
| 2300 | struct ocfs2_extent_list *left_el; |
| 2301 | struct ocfs2_extent_rec *rec; |
| 2302 | int next_free; |
| 2303 | |
| 2304 | left_el = path_leaf_el(left_path); |
| 2305 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2306 | rec = &left_el->l_recs[next_free - 1]; |
| 2307 | |
| 2308 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2309 | return 1; |
| 2310 | return 0; |
| 2311 | } |
| 2312 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2313 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2314 | { |
| 2315 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2316 | unsigned int range; |
| 2317 | struct ocfs2_extent_rec *rec; |
| 2318 | |
| 2319 | if (next_free == 0) |
| 2320 | return 0; |
| 2321 | |
| 2322 | rec = &el->l_recs[0]; |
| 2323 | if (ocfs2_is_empty_extent(rec)) { |
| 2324 | /* Empty list. */ |
| 2325 | if (next_free == 1) |
| 2326 | return 0; |
| 2327 | rec = &el->l_recs[1]; |
| 2328 | } |
| 2329 | |
| 2330 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2331 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2332 | return 1; |
| 2333 | return 0; |
| 2334 | } |
| 2335 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2336 | /* |
| 2337 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2338 | * |
| 2339 | * The path to the rightmost leaf should be passed in. |
| 2340 | * |
| 2341 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2342 | * |
| 2343 | * Upon succesful return from this function: |
| 2344 | * |
| 2345 | * - The 'right_path' array will contain a path to the leaf block |
| 2346 | * whose range contains e_cpos. |
| 2347 | * - That leaf block will have a single empty extent in list index 0. |
| 2348 | * - In the case that the rotation requires a post-insert update, |
| 2349 | * *ret_left_path will contain a valid path which can be passed to |
| 2350 | * ocfs2_insert_path(). |
| 2351 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2352 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2353 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2354 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2355 | u32 insert_cpos, |
| 2356 | struct ocfs2_path *right_path, |
| 2357 | struct ocfs2_path **ret_left_path) |
| 2358 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2359 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2360 | u32 cpos; |
| 2361 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2362 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2363 | |
| 2364 | *ret_left_path = NULL; |
| 2365 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2366 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2367 | if (!left_path) { |
| 2368 | ret = -ENOMEM; |
| 2369 | mlog_errno(ret); |
| 2370 | goto out; |
| 2371 | } |
| 2372 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2373 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2374 | if (ret) { |
| 2375 | mlog_errno(ret); |
| 2376 | goto out; |
| 2377 | } |
| 2378 | |
| 2379 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 2380 | |
| 2381 | /* |
| 2382 | * What we want to do here is: |
| 2383 | * |
| 2384 | * 1) Start with the rightmost path. |
| 2385 | * |
| 2386 | * 2) Determine a path to the leaf block directly to the left |
| 2387 | * of that leaf. |
| 2388 | * |
| 2389 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2390 | * which contains a path to both leaves. |
| 2391 | * |
| 2392 | * 4) Rotate the subtree. |
| 2393 | * |
| 2394 | * 5) Find the next subtree by considering the left path to be |
| 2395 | * the new right path. |
| 2396 | * |
| 2397 | * The check at the top of this while loop also accepts |
| 2398 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2399 | * value to get us the left path - insert_cpos might very well |
| 2400 | * be filling that hole. |
| 2401 | * |
| 2402 | * Stop at a cpos of '0' because we either started at the |
| 2403 | * leftmost branch (i.e., a tree with one branch and a |
| 2404 | * rotation inside of it), or we've gone as far as we can in |
| 2405 | * rotating subtrees. |
| 2406 | */ |
| 2407 | while (cpos && insert_cpos <= cpos) { |
| 2408 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 2409 | insert_cpos, cpos); |
| 2410 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2411 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2412 | if (ret) { |
| 2413 | mlog_errno(ret); |
| 2414 | goto out; |
| 2415 | } |
| 2416 | |
| 2417 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2418 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2419 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2420 | "(left path cpos %u) results in two identical " |
| 2421 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2422 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2423 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2424 | (unsigned long long) |
| 2425 | path_leaf_bh(left_path)->b_blocknr); |
| 2426 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2427 | if (split == SPLIT_NONE && |
| 2428 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2429 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2430 | |
| 2431 | /* |
| 2432 | * We've rotated the tree as much as we |
| 2433 | * should. The rest is up to |
| 2434 | * ocfs2_insert_path() to complete, after the |
| 2435 | * record insertion. We indicate this |
| 2436 | * situation by returning the left path. |
| 2437 | * |
| 2438 | * The reason we don't adjust the records here |
| 2439 | * before the record insert is that an error |
| 2440 | * later might break the rule where a parent |
| 2441 | * record e_cpos will reflect the actual |
| 2442 | * e_cpos of the 1st nonempty record of the |
| 2443 | * child list. |
| 2444 | */ |
| 2445 | *ret_left_path = left_path; |
| 2446 | goto out_ret_path; |
| 2447 | } |
| 2448 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2449 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2450 | |
| 2451 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2452 | start, |
| 2453 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 2454 | right_path->p_tree_depth); |
| 2455 | |
| 2456 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2457 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2458 | if (ret) { |
| 2459 | mlog_errno(ret); |
| 2460 | goto out; |
| 2461 | } |
| 2462 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2463 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2464 | right_path, start); |
| 2465 | if (ret) { |
| 2466 | mlog_errno(ret); |
| 2467 | goto out; |
| 2468 | } |
| 2469 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2470 | if (split != SPLIT_NONE && |
| 2471 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2472 | insert_cpos)) { |
| 2473 | /* |
| 2474 | * A rotate moves the rightmost left leaf |
| 2475 | * record over to the leftmost right leaf |
| 2476 | * slot. If we're doing an extent split |
| 2477 | * instead of a real insert, then we have to |
| 2478 | * check that the extent to be split wasn't |
| 2479 | * just moved over. If it was, then we can |
| 2480 | * exit here, passing left_path back - |
| 2481 | * ocfs2_split_extent() is smart enough to |
| 2482 | * search both leaves. |
| 2483 | */ |
| 2484 | *ret_left_path = left_path; |
| 2485 | goto out_ret_path; |
| 2486 | } |
| 2487 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2488 | /* |
| 2489 | * There is no need to re-read the next right path |
| 2490 | * as we know that it'll be our current left |
| 2491 | * path. Optimize by copying values instead. |
| 2492 | */ |
| 2493 | ocfs2_mv_path(right_path, left_path); |
| 2494 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2495 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2496 | if (ret) { |
| 2497 | mlog_errno(ret); |
| 2498 | goto out; |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | out: |
| 2503 | ocfs2_free_path(left_path); |
| 2504 | |
| 2505 | out_ret_path: |
| 2506 | return ret; |
| 2507 | } |
| 2508 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2509 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2510 | struct ocfs2_extent_tree *et, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2511 | int subtree_index, struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2512 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2513 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2514 | struct ocfs2_extent_rec *rec; |
| 2515 | struct ocfs2_extent_list *el; |
| 2516 | struct ocfs2_extent_block *eb; |
| 2517 | u32 range; |
| 2518 | |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2519 | /* |
| 2520 | * In normal tree rotation process, we will never touch the |
| 2521 | * tree branch above subtree_index and ocfs2_extend_rotate_transaction |
| 2522 | * doesn't reserve the credits for them either. |
| 2523 | * |
| 2524 | * But we do have a special case here which will update the rightmost |
| 2525 | * records for all the bh in the path. |
| 2526 | * So we have to allocate extra credits and access them. |
| 2527 | */ |
| 2528 | ret = ocfs2_extend_trans(handle, |
| 2529 | handle->h_buffer_credits + subtree_index); |
| 2530 | if (ret) { |
| 2531 | mlog_errno(ret); |
| 2532 | goto out; |
| 2533 | } |
| 2534 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2535 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2536 | if (ret) { |
| 2537 | mlog_errno(ret); |
| 2538 | goto out; |
| 2539 | } |
| 2540 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2541 | /* Path should always be rightmost. */ |
| 2542 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2543 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2544 | |
| 2545 | el = &eb->h_list; |
| 2546 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2547 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2548 | rec = &el->l_recs[idx]; |
| 2549 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2550 | |
| 2551 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2552 | el = path->p_node[i].el; |
| 2553 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2554 | rec = &el->l_recs[idx]; |
| 2555 | |
| 2556 | rec->e_int_clusters = cpu_to_le32(range); |
| 2557 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2558 | |
| 2559 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2560 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2561 | out: |
| 2562 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2563 | } |
| 2564 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2565 | static void ocfs2_unlink_path(handle_t *handle, |
| 2566 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2567 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2568 | struct ocfs2_path *path, int unlink_start) |
| 2569 | { |
| 2570 | int ret, i; |
| 2571 | struct ocfs2_extent_block *eb; |
| 2572 | struct ocfs2_extent_list *el; |
| 2573 | struct buffer_head *bh; |
| 2574 | |
| 2575 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2576 | bh = path->p_node[i].bh; |
| 2577 | |
| 2578 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2579 | /* |
| 2580 | * Not all nodes might have had their final count |
| 2581 | * decremented by the caller - handle this here. |
| 2582 | */ |
| 2583 | el = &eb->h_list; |
| 2584 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2585 | mlog(ML_ERROR, |
| 2586 | "Inode %llu, attempted to remove extent block " |
| 2587 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2588 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2589 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2590 | le16_to_cpu(el->l_next_free_rec)); |
| 2591 | |
| 2592 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2593 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2594 | continue; |
| 2595 | } |
| 2596 | |
| 2597 | el->l_next_free_rec = 0; |
| 2598 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2599 | |
| 2600 | ocfs2_journal_dirty(handle, bh); |
| 2601 | |
| 2602 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2603 | if (ret) |
| 2604 | mlog_errno(ret); |
| 2605 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2606 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2607 | } |
| 2608 | } |
| 2609 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2610 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2611 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2612 | struct ocfs2_path *left_path, |
| 2613 | struct ocfs2_path *right_path, |
| 2614 | int subtree_index, |
| 2615 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2616 | { |
| 2617 | int i; |
| 2618 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2619 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2620 | struct ocfs2_extent_list *el; |
| 2621 | struct ocfs2_extent_block *eb; |
| 2622 | |
| 2623 | el = path_leaf_el(left_path); |
| 2624 | |
| 2625 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2626 | |
| 2627 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2628 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2629 | break; |
| 2630 | |
| 2631 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2632 | |
| 2633 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2634 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2635 | |
| 2636 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2637 | eb->h_next_leaf_blk = 0; |
| 2638 | |
| 2639 | ocfs2_journal_dirty(handle, root_bh); |
| 2640 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2641 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2642 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2643 | subtree_index + 1); |
| 2644 | } |
| 2645 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2646 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
| 2647 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2648 | struct ocfs2_path *left_path, |
| 2649 | struct ocfs2_path *right_path, |
| 2650 | int subtree_index, |
| 2651 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2652 | int *deleted) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2653 | { |
| 2654 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2655 | 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] | 2656 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2657 | struct ocfs2_extent_block *eb; |
| 2658 | |
| 2659 | *deleted = 0; |
| 2660 | |
| 2661 | right_leaf_el = path_leaf_el(right_path); |
| 2662 | left_leaf_el = path_leaf_el(left_path); |
| 2663 | root_bh = left_path->p_node[subtree_index].bh; |
| 2664 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2665 | |
| 2666 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2667 | return 0; |
| 2668 | |
| 2669 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2670 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2671 | /* |
| 2672 | * It's legal for us to proceed if the right leaf is |
| 2673 | * the rightmost one and it has an empty extent. There |
| 2674 | * are two cases to handle - whether the leaf will be |
| 2675 | * empty after removal or not. If the leaf isn't empty |
| 2676 | * then just remove the empty extent up front. The |
| 2677 | * next block will handle empty leaves by flagging |
| 2678 | * them for unlink. |
| 2679 | * |
| 2680 | * Non rightmost leaves will throw -EAGAIN and the |
| 2681 | * caller can manually move the subtree and retry. |
| 2682 | */ |
| 2683 | |
| 2684 | if (eb->h_next_leaf_blk != 0ULL) |
| 2685 | return -EAGAIN; |
| 2686 | |
| 2687 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2688 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2689 | path_leaf_bh(right_path), |
| 2690 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2691 | if (ret) { |
| 2692 | mlog_errno(ret); |
| 2693 | goto out; |
| 2694 | } |
| 2695 | |
| 2696 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2697 | } else |
| 2698 | right_has_empty = 1; |
| 2699 | } |
| 2700 | |
| 2701 | if (eb->h_next_leaf_blk == 0ULL && |
| 2702 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2703 | /* |
| 2704 | * We have to update i_last_eb_blk during the meta |
| 2705 | * data delete. |
| 2706 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2707 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2708 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2709 | if (ret) { |
| 2710 | mlog_errno(ret); |
| 2711 | goto out; |
| 2712 | } |
| 2713 | |
| 2714 | del_right_subtree = 1; |
| 2715 | } |
| 2716 | |
| 2717 | /* |
| 2718 | * Getting here with an empty extent in the right path implies |
| 2719 | * that it's the rightmost path and will be deleted. |
| 2720 | */ |
| 2721 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2722 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2723 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2724 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2725 | if (ret) { |
| 2726 | mlog_errno(ret); |
| 2727 | goto out; |
| 2728 | } |
| 2729 | |
| 2730 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2731 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2732 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2733 | if (ret) { |
| 2734 | mlog_errno(ret); |
| 2735 | goto out; |
| 2736 | } |
| 2737 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2738 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2739 | left_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2740 | if (ret) { |
| 2741 | mlog_errno(ret); |
| 2742 | goto out; |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | if (!right_has_empty) { |
| 2747 | /* |
| 2748 | * Only do this if we're moving a real |
| 2749 | * record. Otherwise, the action is delayed until |
| 2750 | * after removal of the right path in which case we |
| 2751 | * can do a simple shift to remove the empty extent. |
| 2752 | */ |
| 2753 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2754 | memset(&right_leaf_el->l_recs[0], 0, |
| 2755 | sizeof(struct ocfs2_extent_rec)); |
| 2756 | } |
| 2757 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2758 | /* |
| 2759 | * Move recs over to get rid of empty extent, decrease |
| 2760 | * next_free. This is allowed to remove the last |
| 2761 | * extent in our leaf (setting l_next_free_rec to |
| 2762 | * zero) - the delete code below won't care. |
| 2763 | */ |
| 2764 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2765 | } |
| 2766 | |
| 2767 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2768 | if (ret) |
| 2769 | mlog_errno(ret); |
| 2770 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 2771 | if (ret) |
| 2772 | mlog_errno(ret); |
| 2773 | |
| 2774 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2775 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2776 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2777 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2778 | left_path); |
| 2779 | if (ret) { |
| 2780 | mlog_errno(ret); |
| 2781 | goto out; |
| 2782 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2783 | |
| 2784 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2785 | 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] | 2786 | |
| 2787 | /* |
| 2788 | * Removal of the extent in the left leaf was skipped |
| 2789 | * above so we could delete the right path |
| 2790 | * 1st. |
| 2791 | */ |
| 2792 | if (right_has_empty) |
| 2793 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2794 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2795 | ret = ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2796 | if (ret) |
| 2797 | mlog_errno(ret); |
| 2798 | |
| 2799 | *deleted = 1; |
| 2800 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2801 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2802 | subtree_index); |
| 2803 | |
| 2804 | out: |
| 2805 | return ret; |
| 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * Given a full path, determine what cpos value would return us a path |
| 2810 | * containing the leaf immediately to the right of the current one. |
| 2811 | * |
| 2812 | * Will return zero if the path passed in is already the rightmost path. |
| 2813 | * |
| 2814 | * This looks similar, but is subtly different to |
| 2815 | * ocfs2_find_cpos_for_left_leaf(). |
| 2816 | */ |
| 2817 | static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2818 | struct ocfs2_path *path, u32 *cpos) |
| 2819 | { |
| 2820 | int i, j, ret = 0; |
| 2821 | u64 blkno; |
| 2822 | struct ocfs2_extent_list *el; |
| 2823 | |
| 2824 | *cpos = 0; |
| 2825 | |
| 2826 | if (path->p_tree_depth == 0) |
| 2827 | return 0; |
| 2828 | |
| 2829 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2830 | |
| 2831 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2832 | i = path->p_tree_depth - 1; |
| 2833 | while (i >= 0) { |
| 2834 | int next_free; |
| 2835 | |
| 2836 | el = path->p_node[i].el; |
| 2837 | |
| 2838 | /* |
| 2839 | * Find the extent record just after the one in our |
| 2840 | * path. |
| 2841 | */ |
| 2842 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2843 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2844 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2845 | if (j == (next_free - 1)) { |
| 2846 | if (i == 0) { |
| 2847 | /* |
| 2848 | * We've determined that the |
| 2849 | * path specified is already |
| 2850 | * the rightmost one - return a |
| 2851 | * cpos of zero. |
| 2852 | */ |
| 2853 | goto out; |
| 2854 | } |
| 2855 | /* |
| 2856 | * The rightmost record points to our |
| 2857 | * leaf - we need to travel up the |
| 2858 | * tree one level. |
| 2859 | */ |
| 2860 | goto next_node; |
| 2861 | } |
| 2862 | |
| 2863 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2864 | goto out; |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | /* |
| 2869 | * If we got here, we never found a valid node where |
| 2870 | * the tree indicated one should be. |
| 2871 | */ |
| 2872 | ocfs2_error(sb, |
| 2873 | "Invalid extent tree at extent block %llu\n", |
| 2874 | (unsigned long long)blkno); |
| 2875 | ret = -EROFS; |
| 2876 | goto out; |
| 2877 | |
| 2878 | next_node: |
| 2879 | blkno = path->p_node[i].bh->b_blocknr; |
| 2880 | i--; |
| 2881 | } |
| 2882 | |
| 2883 | out: |
| 2884 | return ret; |
| 2885 | } |
| 2886 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2887 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
| 2888 | struct ocfs2_extent_tree *et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2889 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2890 | { |
| 2891 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2892 | struct buffer_head *bh = path_leaf_bh(path); |
| 2893 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2894 | |
| 2895 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2896 | return 0; |
| 2897 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2898 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2899 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2900 | if (ret) { |
| 2901 | mlog_errno(ret); |
| 2902 | goto out; |
| 2903 | } |
| 2904 | |
| 2905 | ocfs2_remove_empty_extent(el); |
| 2906 | |
| 2907 | ret = ocfs2_journal_dirty(handle, bh); |
| 2908 | if (ret) |
| 2909 | mlog_errno(ret); |
| 2910 | |
| 2911 | out: |
| 2912 | return ret; |
| 2913 | } |
| 2914 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2915 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
| 2916 | struct ocfs2_extent_tree *et, |
| 2917 | int orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2918 | struct ocfs2_path *path, |
| 2919 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2920 | struct ocfs2_path **empty_extent_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2921 | { |
| 2922 | int ret, subtree_root, deleted; |
| 2923 | u32 right_cpos; |
| 2924 | struct ocfs2_path *left_path = NULL; |
| 2925 | struct ocfs2_path *right_path = NULL; |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2926 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2927 | |
| 2928 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 2929 | |
| 2930 | *empty_extent_path = NULL; |
| 2931 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2932 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2933 | if (ret) { |
| 2934 | mlog_errno(ret); |
| 2935 | goto out; |
| 2936 | } |
| 2937 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2938 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2939 | if (!left_path) { |
| 2940 | ret = -ENOMEM; |
| 2941 | mlog_errno(ret); |
| 2942 | goto out; |
| 2943 | } |
| 2944 | |
| 2945 | ocfs2_cp_path(left_path, path); |
| 2946 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2947 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2948 | if (!right_path) { |
| 2949 | ret = -ENOMEM; |
| 2950 | mlog_errno(ret); |
| 2951 | goto out; |
| 2952 | } |
| 2953 | |
| 2954 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 2955 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2956 | if (ret) { |
| 2957 | mlog_errno(ret); |
| 2958 | goto out; |
| 2959 | } |
| 2960 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2961 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2962 | right_path); |
| 2963 | |
| 2964 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2965 | subtree_root, |
| 2966 | (unsigned long long) |
| 2967 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 2968 | right_path->p_tree_depth); |
| 2969 | |
| 2970 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 2971 | orig_credits, left_path); |
| 2972 | if (ret) { |
| 2973 | mlog_errno(ret); |
| 2974 | goto out; |
| 2975 | } |
| 2976 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2977 | /* |
| 2978 | * Caller might still want to make changes to the |
| 2979 | * tree root, so re-add it to the journal here. |
| 2980 | */ |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2981 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2982 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2983 | if (ret) { |
| 2984 | mlog_errno(ret); |
| 2985 | goto out; |
| 2986 | } |
| 2987 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2988 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2989 | right_path, subtree_root, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2990 | dealloc, &deleted); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2991 | if (ret == -EAGAIN) { |
| 2992 | /* |
| 2993 | * The rotation has to temporarily stop due to |
| 2994 | * the right subtree having an empty |
| 2995 | * extent. Pass it back to the caller for a |
| 2996 | * fixup. |
| 2997 | */ |
| 2998 | *empty_extent_path = right_path; |
| 2999 | right_path = NULL; |
| 3000 | goto out; |
| 3001 | } |
| 3002 | if (ret) { |
| 3003 | mlog_errno(ret); |
| 3004 | goto out; |
| 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * The subtree rotate might have removed records on |
| 3009 | * the rightmost edge. If so, then rotation is |
| 3010 | * complete. |
| 3011 | */ |
| 3012 | if (deleted) |
| 3013 | break; |
| 3014 | |
| 3015 | ocfs2_mv_path(left_path, right_path); |
| 3016 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3017 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3018 | &right_cpos); |
| 3019 | if (ret) { |
| 3020 | mlog_errno(ret); |
| 3021 | goto out; |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | out: |
| 3026 | ocfs2_free_path(right_path); |
| 3027 | ocfs2_free_path(left_path); |
| 3028 | |
| 3029 | return ret; |
| 3030 | } |
| 3031 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3032 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
| 3033 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3034 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3035 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3036 | { |
| 3037 | int ret, subtree_index; |
| 3038 | u32 cpos; |
| 3039 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3040 | struct ocfs2_extent_block *eb; |
| 3041 | struct ocfs2_extent_list *el; |
| 3042 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3043 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 3044 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3045 | if (ret) |
| 3046 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3047 | /* |
| 3048 | * There's two ways we handle this depending on |
| 3049 | * whether path is the only existing one. |
| 3050 | */ |
| 3051 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3052 | handle->h_buffer_credits, |
| 3053 | path); |
| 3054 | if (ret) { |
| 3055 | mlog_errno(ret); |
| 3056 | goto out; |
| 3057 | } |
| 3058 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3059 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3060 | if (ret) { |
| 3061 | mlog_errno(ret); |
| 3062 | goto out; |
| 3063 | } |
| 3064 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3065 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3066 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3067 | if (ret) { |
| 3068 | mlog_errno(ret); |
| 3069 | goto out; |
| 3070 | } |
| 3071 | |
| 3072 | if (cpos) { |
| 3073 | /* |
| 3074 | * We have a path to the left of this one - it needs |
| 3075 | * an update too. |
| 3076 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3077 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3078 | if (!left_path) { |
| 3079 | ret = -ENOMEM; |
| 3080 | mlog_errno(ret); |
| 3081 | goto out; |
| 3082 | } |
| 3083 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3084 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3085 | if (ret) { |
| 3086 | mlog_errno(ret); |
| 3087 | goto out; |
| 3088 | } |
| 3089 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3090 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3091 | if (ret) { |
| 3092 | mlog_errno(ret); |
| 3093 | goto out; |
| 3094 | } |
| 3095 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3096 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3097 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3098 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3099 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 3100 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3101 | left_path); |
| 3102 | if (ret) { |
| 3103 | mlog_errno(ret); |
| 3104 | goto out; |
| 3105 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3106 | |
| 3107 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3108 | 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] | 3109 | } else { |
| 3110 | /* |
| 3111 | * 'path' is also the leftmost path which |
| 3112 | * means it must be the only one. This gets |
| 3113 | * handled differently because we want to |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3114 | * revert the root back to having extents |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3115 | * in-line. |
| 3116 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3117 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3118 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3119 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3120 | el->l_tree_depth = 0; |
| 3121 | el->l_next_free_rec = 0; |
| 3122 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3123 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3124 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3128 | |
| 3129 | out: |
| 3130 | ocfs2_free_path(left_path); |
| 3131 | return ret; |
| 3132 | } |
| 3133 | |
| 3134 | /* |
| 3135 | * Left rotation of btree records. |
| 3136 | * |
| 3137 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3138 | * rotation. We start at some non-rightmost path containing an empty |
| 3139 | * extent in the leaf block. The code works its way to the rightmost |
| 3140 | * path by rotating records to the left in every subtree. |
| 3141 | * |
| 3142 | * This is used by any code which reduces the number of extent records |
| 3143 | * in a leaf. After removal, an empty record should be placed in the |
| 3144 | * leftmost list position. |
| 3145 | * |
| 3146 | * This won't handle a length update of the rightmost path records if |
| 3147 | * the rightmost tree leaf record is removed so the caller is |
| 3148 | * responsible for detecting and correcting that. |
| 3149 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3150 | static int ocfs2_rotate_tree_left(handle_t *handle, |
| 3151 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3152 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3153 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3154 | { |
| 3155 | int ret, orig_credits = handle->h_buffer_credits; |
| 3156 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3157 | struct ocfs2_extent_block *eb; |
| 3158 | struct ocfs2_extent_list *el; |
| 3159 | |
| 3160 | el = path_leaf_el(path); |
| 3161 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3162 | return 0; |
| 3163 | |
| 3164 | if (path->p_tree_depth == 0) { |
| 3165 | rightmost_no_delete: |
| 3166 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3167 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3168 | * it up front. |
| 3169 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3170 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3171 | if (ret) |
| 3172 | mlog_errno(ret); |
| 3173 | goto out; |
| 3174 | } |
| 3175 | |
| 3176 | /* |
| 3177 | * Handle rightmost branch now. There's several cases: |
| 3178 | * 1) simple rotation leaving records in there. That's trivial. |
| 3179 | * 2) rotation requiring a branch delete - there's no more |
| 3180 | * records left. Two cases of this: |
| 3181 | * a) There are branches to the left. |
| 3182 | * b) This is also the leftmost (the only) branch. |
| 3183 | * |
| 3184 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3185 | * 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] | 3186 | * 2b) we need to bring the root back to inline extents. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3187 | */ |
| 3188 | |
| 3189 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3190 | el = &eb->h_list; |
| 3191 | if (eb->h_next_leaf_blk == 0) { |
| 3192 | /* |
| 3193 | * This gets a bit tricky if we're going to delete the |
| 3194 | * rightmost path. Get the other cases out of the way |
| 3195 | * 1st. |
| 3196 | */ |
| 3197 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3198 | goto rightmost_no_delete; |
| 3199 | |
| 3200 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3201 | ret = -EIO; |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3202 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3203 | "Owner %llu has empty extent block at %llu", |
| 3204 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3205 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3206 | goto out; |
| 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * XXX: The caller can not trust "path" any more after |
| 3211 | * this as it will have been deleted. What do we do? |
| 3212 | * |
| 3213 | * In theory the rotate-for-merge code will never get |
| 3214 | * here because it'll always ask for a rotate in a |
| 3215 | * nonempty list. |
| 3216 | */ |
| 3217 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3218 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
| 3219 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3220 | if (ret) |
| 3221 | mlog_errno(ret); |
| 3222 | goto out; |
| 3223 | } |
| 3224 | |
| 3225 | /* |
| 3226 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3227 | * and restarting from there. |
| 3228 | */ |
| 3229 | try_rotate: |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3230 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
| 3231 | dealloc, &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3232 | if (ret && ret != -EAGAIN) { |
| 3233 | mlog_errno(ret); |
| 3234 | goto out; |
| 3235 | } |
| 3236 | |
| 3237 | while (ret == -EAGAIN) { |
| 3238 | tmp_path = restart_path; |
| 3239 | restart_path = NULL; |
| 3240 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3241 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3242 | tmp_path, dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3243 | &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3244 | if (ret && ret != -EAGAIN) { |
| 3245 | mlog_errno(ret); |
| 3246 | goto out; |
| 3247 | } |
| 3248 | |
| 3249 | ocfs2_free_path(tmp_path); |
| 3250 | tmp_path = NULL; |
| 3251 | |
| 3252 | if (ret == 0) |
| 3253 | goto try_rotate; |
| 3254 | } |
| 3255 | |
| 3256 | out: |
| 3257 | ocfs2_free_path(tmp_path); |
| 3258 | ocfs2_free_path(restart_path); |
| 3259 | return ret; |
| 3260 | } |
| 3261 | |
| 3262 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3263 | int index) |
| 3264 | { |
| 3265 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3266 | unsigned int size; |
| 3267 | |
| 3268 | if (rec->e_leaf_clusters == 0) { |
| 3269 | /* |
| 3270 | * We consumed all of the merged-from record. An empty |
| 3271 | * extent cannot exist anywhere but the 1st array |
| 3272 | * position, so move things over if the merged-from |
| 3273 | * record doesn't occupy that position. |
| 3274 | * |
| 3275 | * This creates a new empty extent so the caller |
| 3276 | * should be smart enough to have removed any existing |
| 3277 | * ones. |
| 3278 | */ |
| 3279 | if (index > 0) { |
| 3280 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3281 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3282 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3283 | } |
| 3284 | |
| 3285 | /* |
| 3286 | * Always memset - the caller doesn't check whether it |
| 3287 | * created an empty extent, so there could be junk in |
| 3288 | * the other fields. |
| 3289 | */ |
| 3290 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3291 | } |
| 3292 | } |
| 3293 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3294 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3295 | struct ocfs2_path *left_path, |
| 3296 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3297 | { |
| 3298 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3299 | u32 right_cpos; |
| 3300 | struct ocfs2_path *right_path = NULL; |
| 3301 | struct ocfs2_extent_list *left_el; |
| 3302 | |
| 3303 | *ret_right_path = NULL; |
| 3304 | |
| 3305 | /* This function shouldn't be called for non-trees. */ |
| 3306 | BUG_ON(left_path->p_tree_depth == 0); |
| 3307 | |
| 3308 | left_el = path_leaf_el(left_path); |
| 3309 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3310 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3311 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3312 | left_path, &right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3313 | if (ret) { |
| 3314 | mlog_errno(ret); |
| 3315 | goto out; |
| 3316 | } |
| 3317 | |
| 3318 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3319 | BUG_ON(right_cpos == 0); |
| 3320 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3321 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3322 | if (!right_path) { |
| 3323 | ret = -ENOMEM; |
| 3324 | mlog_errno(ret); |
| 3325 | goto out; |
| 3326 | } |
| 3327 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3328 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3329 | if (ret) { |
| 3330 | mlog_errno(ret); |
| 3331 | goto out; |
| 3332 | } |
| 3333 | |
| 3334 | *ret_right_path = right_path; |
| 3335 | out: |
| 3336 | if (ret) |
| 3337 | ocfs2_free_path(right_path); |
| 3338 | return ret; |
| 3339 | } |
| 3340 | |
| 3341 | /* |
| 3342 | * Remove split_rec clusters from the record at index and merge them |
| 3343 | * onto the beginning of the record "next" to it. |
| 3344 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3345 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3346 | * next extent block. |
| 3347 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3348 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3349 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3350 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3351 | struct ocfs2_extent_rec *split_rec, |
| 3352 | int index) |
| 3353 | { |
| 3354 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3355 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3356 | struct ocfs2_extent_rec *left_rec; |
| 3357 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3358 | struct ocfs2_extent_list *right_el; |
| 3359 | struct ocfs2_path *right_path = NULL; |
| 3360 | int subtree_index = 0; |
| 3361 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3362 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3363 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3364 | |
| 3365 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3366 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3367 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3368 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3369 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3370 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3371 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
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 | right_el = path_leaf_el(right_path); |
| 3378 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3379 | BUG_ON(next_free <= 0); |
| 3380 | right_rec = &right_el->l_recs[0]; |
| 3381 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3382 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3383 | right_rec = &right_el->l_recs[1]; |
| 3384 | } |
| 3385 | |
| 3386 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3387 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3388 | le32_to_cpu(right_rec->e_cpos)); |
| 3389 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3390 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3391 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3392 | |
| 3393 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3394 | handle->h_buffer_credits, |
| 3395 | right_path); |
| 3396 | if (ret) { |
| 3397 | mlog_errno(ret); |
| 3398 | goto out; |
| 3399 | } |
| 3400 | |
| 3401 | root_bh = left_path->p_node[subtree_index].bh; |
| 3402 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3403 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3404 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3405 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3406 | if (ret) { |
| 3407 | mlog_errno(ret); |
| 3408 | goto out; |
| 3409 | } |
| 3410 | |
| 3411 | for (i = subtree_index + 1; |
| 3412 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3413 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3414 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3415 | if (ret) { |
| 3416 | mlog_errno(ret); |
| 3417 | goto out; |
| 3418 | } |
| 3419 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3420 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3421 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3422 | if (ret) { |
| 3423 | mlog_errno(ret); |
| 3424 | goto out; |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | } else { |
| 3429 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3430 | right_rec = &el->l_recs[index + 1]; |
| 3431 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3432 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3433 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3434 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3435 | if (ret) { |
| 3436 | mlog_errno(ret); |
| 3437 | goto out; |
| 3438 | } |
| 3439 | |
| 3440 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3441 | |
| 3442 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3443 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3444 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3445 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3446 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3447 | |
| 3448 | ocfs2_cleanup_merge(el, index); |
| 3449 | |
| 3450 | ret = ocfs2_journal_dirty(handle, bh); |
| 3451 | if (ret) |
| 3452 | mlog_errno(ret); |
| 3453 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3454 | if (right_path) { |
| 3455 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 3456 | if (ret) |
| 3457 | mlog_errno(ret); |
| 3458 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3459 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3460 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3461 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3462 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3463 | if (right_path) |
| 3464 | ocfs2_free_path(right_path); |
| 3465 | return ret; |
| 3466 | } |
| 3467 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3468 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3469 | struct ocfs2_path *right_path, |
| 3470 | struct ocfs2_path **ret_left_path) |
| 3471 | { |
| 3472 | int ret; |
| 3473 | u32 left_cpos; |
| 3474 | struct ocfs2_path *left_path = NULL; |
| 3475 | |
| 3476 | *ret_left_path = NULL; |
| 3477 | |
| 3478 | /* This function shouldn't be called for non-trees. */ |
| 3479 | BUG_ON(right_path->p_tree_depth == 0); |
| 3480 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3481 | 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] | 3482 | right_path, &left_cpos); |
| 3483 | if (ret) { |
| 3484 | mlog_errno(ret); |
| 3485 | goto out; |
| 3486 | } |
| 3487 | |
| 3488 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3489 | BUG_ON(left_cpos == 0); |
| 3490 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3491 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3492 | if (!left_path) { |
| 3493 | ret = -ENOMEM; |
| 3494 | mlog_errno(ret); |
| 3495 | goto out; |
| 3496 | } |
| 3497 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3498 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3499 | if (ret) { |
| 3500 | mlog_errno(ret); |
| 3501 | goto out; |
| 3502 | } |
| 3503 | |
| 3504 | *ret_left_path = left_path; |
| 3505 | out: |
| 3506 | if (ret) |
| 3507 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3508 | return ret; |
| 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3513 | * onto the tail of the record "before" it. |
| 3514 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3515 | * |
| 3516 | * For index == 0, the "before" means the last record of the previous |
| 3517 | * extent block. And there is also a situation that we may need to |
| 3518 | * remove the rightmost leaf extent block in the right_path and change |
| 3519 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3520 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3521 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3522 | handle_t *handle, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3523 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3524 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3525 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3526 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3527 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3528 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3529 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3530 | struct ocfs2_extent_rec *left_rec; |
| 3531 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3532 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3533 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3534 | struct buffer_head *root_bh = NULL; |
| 3535 | struct ocfs2_path *left_path = NULL; |
| 3536 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3537 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3538 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3539 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3540 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3541 | if (index == 0) { |
| 3542 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3543 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3544 | if (ret) { |
| 3545 | mlog_errno(ret); |
| 3546 | goto out; |
| 3547 | } |
| 3548 | |
| 3549 | left_el = path_leaf_el(left_path); |
| 3550 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3551 | le16_to_cpu(left_el->l_count)); |
| 3552 | |
| 3553 | left_rec = &left_el->l_recs[ |
| 3554 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3555 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3556 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3557 | le32_to_cpu(split_rec->e_cpos)); |
| 3558 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3559 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3560 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3561 | |
| 3562 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3563 | handle->h_buffer_credits, |
| 3564 | left_path); |
| 3565 | if (ret) { |
| 3566 | mlog_errno(ret); |
| 3567 | goto out; |
| 3568 | } |
| 3569 | |
| 3570 | root_bh = left_path->p_node[subtree_index].bh; |
| 3571 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3572 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3573 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3574 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3575 | if (ret) { |
| 3576 | mlog_errno(ret); |
| 3577 | goto out; |
| 3578 | } |
| 3579 | |
| 3580 | for (i = subtree_index + 1; |
| 3581 | i < path_num_items(right_path); i++) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3582 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3583 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3584 | if (ret) { |
| 3585 | mlog_errno(ret); |
| 3586 | goto out; |
| 3587 | } |
| 3588 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3589 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3590 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3591 | if (ret) { |
| 3592 | mlog_errno(ret); |
| 3593 | goto out; |
| 3594 | } |
| 3595 | } |
| 3596 | } else { |
| 3597 | left_rec = &el->l_recs[index - 1]; |
| 3598 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3599 | has_empty_extent = 1; |
| 3600 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3601 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3602 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3603 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3604 | if (ret) { |
| 3605 | mlog_errno(ret); |
| 3606 | goto out; |
| 3607 | } |
| 3608 | |
| 3609 | if (has_empty_extent && index == 1) { |
| 3610 | /* |
| 3611 | * The easy case - we can just plop the record right in. |
| 3612 | */ |
| 3613 | *left_rec = *split_rec; |
| 3614 | |
| 3615 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3616 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3617 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3618 | |
| 3619 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3620 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3621 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3622 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3623 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3624 | |
| 3625 | ocfs2_cleanup_merge(el, index); |
| 3626 | |
| 3627 | ret = ocfs2_journal_dirty(handle, bh); |
| 3628 | if (ret) |
| 3629 | mlog_errno(ret); |
| 3630 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3631 | if (left_path) { |
| 3632 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 3633 | if (ret) |
| 3634 | mlog_errno(ret); |
| 3635 | |
| 3636 | /* |
| 3637 | * In the situation that the right_rec is empty and the extent |
| 3638 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3639 | * it and we need to delete the right extent block. |
| 3640 | */ |
| 3641 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3642 | le16_to_cpu(el->l_next_free_rec) == 1) { |
| 3643 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3644 | ret = ocfs2_remove_rightmost_path(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3645 | right_path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3646 | dealloc); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3647 | if (ret) { |
| 3648 | mlog_errno(ret); |
| 3649 | goto out; |
| 3650 | } |
| 3651 | |
| 3652 | /* Now the rightmost extent block has been deleted. |
| 3653 | * So we use the new rightmost path. |
| 3654 | */ |
| 3655 | ocfs2_mv_path(right_path, left_path); |
| 3656 | left_path = NULL; |
| 3657 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3658 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3659 | right_path, subtree_index); |
| 3660 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3661 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3662 | if (left_path) |
| 3663 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3664 | return ret; |
| 3665 | } |
| 3666 | |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3667 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
| 3668 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3669 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3670 | int split_index, |
| 3671 | struct ocfs2_extent_rec *split_rec, |
| 3672 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3673 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3674 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3675 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3676 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3677 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3678 | |
| 3679 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3680 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3681 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 3682 | /* |
| 3683 | * The merge code will need to create an empty |
| 3684 | * extent to take the place of the newly |
| 3685 | * emptied slot. Remove any pre-existing empty |
| 3686 | * extents - having more than one in a leaf is |
| 3687 | * illegal. |
| 3688 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3689 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3690 | if (ret) { |
| 3691 | mlog_errno(ret); |
| 3692 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3693 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3694 | split_index--; |
| 3695 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3699 | /* |
| 3700 | * Left-right contig implies this. |
| 3701 | */ |
| 3702 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3703 | |
| 3704 | /* |
| 3705 | * Since the leftright insert always covers the entire |
| 3706 | * extent, this call will delete the insert record |
| 3707 | * entirely, resulting in an empty extent record added to |
| 3708 | * the extent block. |
| 3709 | * |
| 3710 | * Since the adding of an empty extent shifts |
| 3711 | * everything back to the right, there's no need to |
| 3712 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3713 | * |
| 3714 | * When the split_index is zero, we need to merge it to the |
| 3715 | * prevoius extent block. It is more efficient and easier |
| 3716 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3717 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3718 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3719 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3720 | if (ret) { |
| 3721 | mlog_errno(ret); |
| 3722 | goto out; |
| 3723 | } |
| 3724 | |
| 3725 | /* |
| 3726 | * We can only get this from logic error above. |
| 3727 | */ |
| 3728 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3729 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3730 | /* The merge left us with an empty extent, remove it. */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3731 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3732 | if (ret) { |
| 3733 | mlog_errno(ret); |
| 3734 | goto out; |
| 3735 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3736 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3737 | rec = &el->l_recs[split_index]; |
| 3738 | |
| 3739 | /* |
| 3740 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3741 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3742 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3743 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
| 3744 | dealloc, split_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3745 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3746 | if (ret) { |
| 3747 | mlog_errno(ret); |
| 3748 | goto out; |
| 3749 | } |
| 3750 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3751 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3752 | /* |
| 3753 | * Error from this last rotate is not critical, so |
| 3754 | * print but don't bubble it up. |
| 3755 | */ |
| 3756 | if (ret) |
| 3757 | mlog_errno(ret); |
| 3758 | ret = 0; |
| 3759 | } else { |
| 3760 | /* |
| 3761 | * Merge a record to the left or right. |
| 3762 | * |
| 3763 | * 'contig_type' is relative to the existing record, |
| 3764 | * so for example, if we're "right contig", it's to |
| 3765 | * the record on the left (hence the left merge). |
| 3766 | */ |
| 3767 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3768 | ret = ocfs2_merge_rec_left(path, handle, et, |
| 3769 | split_rec, dealloc, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3770 | split_index); |
| 3771 | if (ret) { |
| 3772 | mlog_errno(ret); |
| 3773 | goto out; |
| 3774 | } |
| 3775 | } else { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3776 | ret = ocfs2_merge_rec_right(path, handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3777 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3778 | split_index); |
| 3779 | if (ret) { |
| 3780 | mlog_errno(ret); |
| 3781 | goto out; |
| 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | if (ctxt->c_split_covers_rec) { |
| 3786 | /* |
| 3787 | * The merge may have left an empty extent in |
| 3788 | * our leaf. Try to rotate it away. |
| 3789 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3790 | ret = ocfs2_rotate_tree_left(handle, et, path, |
| 3791 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3792 | if (ret) |
| 3793 | mlog_errno(ret); |
| 3794 | ret = 0; |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | out: |
| 3799 | return ret; |
| 3800 | } |
| 3801 | |
| 3802 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3803 | enum ocfs2_split_type split, |
| 3804 | struct ocfs2_extent_rec *rec, |
| 3805 | struct ocfs2_extent_rec *split_rec) |
| 3806 | { |
| 3807 | u64 len_blocks; |
| 3808 | |
| 3809 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3810 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3811 | |
| 3812 | if (split == SPLIT_LEFT) { |
| 3813 | /* |
| 3814 | * Region is on the left edge of the existing |
| 3815 | * record. |
| 3816 | */ |
| 3817 | le32_add_cpu(&rec->e_cpos, |
| 3818 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3819 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3820 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3821 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3822 | } else { |
| 3823 | /* |
| 3824 | * Region is on the right edge of the existing |
| 3825 | * record. |
| 3826 | */ |
| 3827 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3828 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3829 | } |
| 3830 | } |
| 3831 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3832 | /* |
| 3833 | * Do the final bits of extent record insertion at the target leaf |
| 3834 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3835 | * that the tree above has been prepared. |
| 3836 | */ |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3837 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et, |
| 3838 | struct ocfs2_extent_rec *insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3839 | struct ocfs2_extent_list *el, |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3840 | struct ocfs2_insert_type *insert) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3841 | { |
| 3842 | int i = insert->ins_contig_index; |
| 3843 | unsigned int range; |
| 3844 | struct ocfs2_extent_rec *rec; |
| 3845 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3846 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3847 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3848 | if (insert->ins_split != SPLIT_NONE) { |
| 3849 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3850 | BUG_ON(i == -1); |
| 3851 | rec = &el->l_recs[i]; |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3852 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3853 | insert->ins_split, rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3854 | insert_rec); |
| 3855 | goto rotate; |
| 3856 | } |
| 3857 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3858 | /* |
| 3859 | * Contiguous insert - either left or right. |
| 3860 | */ |
| 3861 | if (insert->ins_contig != CONTIG_NONE) { |
| 3862 | rec = &el->l_recs[i]; |
| 3863 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3864 | rec->e_blkno = insert_rec->e_blkno; |
| 3865 | rec->e_cpos = insert_rec->e_cpos; |
| 3866 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3867 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3868 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3869 | return; |
| 3870 | } |
| 3871 | |
| 3872 | /* |
| 3873 | * Handle insert into an empty leaf. |
| 3874 | */ |
| 3875 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3876 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3877 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3878 | el->l_recs[0] = *insert_rec; |
| 3879 | el->l_next_free_rec = cpu_to_le16(1); |
| 3880 | return; |
| 3881 | } |
| 3882 | |
| 3883 | /* |
| 3884 | * Appending insert. |
| 3885 | */ |
| 3886 | if (insert->ins_appending == APPEND_TAIL) { |
| 3887 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3888 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3889 | range = le32_to_cpu(rec->e_cpos) |
| 3890 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3891 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3892 | |
| 3893 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3894 | le16_to_cpu(el->l_count), |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3895 | "owner %llu, depth %u, count %u, next free %u, " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3896 | "rec.cpos %u, rec.clusters %u, " |
| 3897 | "insert.cpos %u, insert.clusters %u\n", |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3898 | ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3899 | le16_to_cpu(el->l_tree_depth), |
| 3900 | le16_to_cpu(el->l_count), |
| 3901 | le16_to_cpu(el->l_next_free_rec), |
| 3902 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3903 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3904 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3905 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3906 | i++; |
| 3907 | el->l_recs[i] = *insert_rec; |
| 3908 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3909 | return; |
| 3910 | } |
| 3911 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3912 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3913 | /* |
| 3914 | * Ok, we have to rotate. |
| 3915 | * |
| 3916 | * At this point, it is safe to assume that inserting into an |
| 3917 | * empty leaf and appending to a leaf have both been handled |
| 3918 | * above. |
| 3919 | * |
| 3920 | * This leaf needs to have space, either by the empty 1st |
| 3921 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3922 | */ |
| 3923 | ocfs2_rotate_leaf(el, insert_rec); |
| 3924 | } |
| 3925 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3926 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 3927 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3928 | struct ocfs2_path *path, |
| 3929 | struct ocfs2_extent_rec *insert_rec) |
| 3930 | { |
| 3931 | int ret, i, next_free; |
| 3932 | struct buffer_head *bh; |
| 3933 | struct ocfs2_extent_list *el; |
| 3934 | struct ocfs2_extent_rec *rec; |
| 3935 | |
| 3936 | /* |
| 3937 | * Update everything except the leaf block. |
| 3938 | */ |
| 3939 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3940 | bh = path->p_node[i].bh; |
| 3941 | el = path->p_node[i].el; |
| 3942 | |
| 3943 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3944 | if (next_free == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3945 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3946 | "Owner %llu has a bad extent list", |
| 3947 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3948 | ret = -EIO; |
| 3949 | return; |
| 3950 | } |
| 3951 | |
| 3952 | rec = &el->l_recs[next_free - 1]; |
| 3953 | |
| 3954 | rec->e_int_clusters = insert_rec->e_cpos; |
| 3955 | le32_add_cpu(&rec->e_int_clusters, |
| 3956 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 3957 | le32_add_cpu(&rec->e_int_clusters, |
| 3958 | -le32_to_cpu(rec->e_cpos)); |
| 3959 | |
| 3960 | ret = ocfs2_journal_dirty(handle, bh); |
| 3961 | if (ret) |
| 3962 | mlog_errno(ret); |
| 3963 | |
| 3964 | } |
| 3965 | } |
| 3966 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3967 | static int ocfs2_append_rec_to_path(handle_t *handle, |
| 3968 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3969 | struct ocfs2_extent_rec *insert_rec, |
| 3970 | struct ocfs2_path *right_path, |
| 3971 | struct ocfs2_path **ret_left_path) |
| 3972 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3973 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3974 | struct ocfs2_extent_list *el; |
| 3975 | struct ocfs2_path *left_path = NULL; |
| 3976 | |
| 3977 | *ret_left_path = NULL; |
| 3978 | |
| 3979 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3980 | * This shouldn't happen for non-trees. The extent rec cluster |
| 3981 | * count manipulation below only works for interior nodes. |
| 3982 | */ |
| 3983 | BUG_ON(right_path->p_tree_depth == 0); |
| 3984 | |
| 3985 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3986 | * If our appending insert is at the leftmost edge of a leaf, |
| 3987 | * then we might need to update the rightmost records of the |
| 3988 | * neighboring path. |
| 3989 | */ |
| 3990 | el = path_leaf_el(right_path); |
| 3991 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3992 | if (next_free == 0 || |
| 3993 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3994 | u32 left_cpos; |
| 3995 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3996 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3997 | right_path, &left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3998 | if (ret) { |
| 3999 | mlog_errno(ret); |
| 4000 | goto out; |
| 4001 | } |
| 4002 | |
| 4003 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 4004 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 4005 | left_cpos); |
| 4006 | |
| 4007 | /* |
| 4008 | * No need to worry if the append is already in the |
| 4009 | * leftmost leaf. |
| 4010 | */ |
| 4011 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4012 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4013 | if (!left_path) { |
| 4014 | ret = -ENOMEM; |
| 4015 | mlog_errno(ret); |
| 4016 | goto out; |
| 4017 | } |
| 4018 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4019 | ret = ocfs2_find_path(et->et_ci, left_path, |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4020 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4021 | if (ret) { |
| 4022 | mlog_errno(ret); |
| 4023 | goto out; |
| 4024 | } |
| 4025 | |
| 4026 | /* |
| 4027 | * ocfs2_insert_path() will pass the left_path to the |
| 4028 | * journal for us. |
| 4029 | */ |
| 4030 | } |
| 4031 | } |
| 4032 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4033 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4034 | if (ret) { |
| 4035 | mlog_errno(ret); |
| 4036 | goto out; |
| 4037 | } |
| 4038 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4039 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4040 | |
| 4041 | *ret_left_path = left_path; |
| 4042 | ret = 0; |
| 4043 | out: |
| 4044 | if (ret != 0) |
| 4045 | ocfs2_free_path(left_path); |
| 4046 | |
| 4047 | return ret; |
| 4048 | } |
| 4049 | |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4050 | static void ocfs2_split_record(struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4051 | struct ocfs2_path *left_path, |
| 4052 | struct ocfs2_path *right_path, |
| 4053 | struct ocfs2_extent_rec *split_rec, |
| 4054 | enum ocfs2_split_type split) |
| 4055 | { |
| 4056 | int index; |
| 4057 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4058 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4059 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4060 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4061 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4062 | if (left_path) |
| 4063 | left_el = path_leaf_el(left_path); |
| 4064 | |
| 4065 | el = right_el; |
| 4066 | insert_el = right_el; |
| 4067 | index = ocfs2_search_extent_list(el, cpos); |
| 4068 | if (index != -1) { |
| 4069 | if (index == 0 && left_path) { |
| 4070 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4071 | |
| 4072 | /* |
| 4073 | * This typically means that the record |
| 4074 | * started in the left path but moved to the |
| 4075 | * right as a result of rotation. We either |
| 4076 | * move the existing record to the left, or we |
| 4077 | * do the later insert there. |
| 4078 | * |
| 4079 | * In this case, the left path should always |
| 4080 | * exist as the rotate code will have passed |
| 4081 | * it back for a post-insert update. |
| 4082 | */ |
| 4083 | |
| 4084 | if (split == SPLIT_LEFT) { |
| 4085 | /* |
| 4086 | * It's a left split. Since we know |
| 4087 | * that the rotate code gave us an |
| 4088 | * empty extent in the left path, we |
| 4089 | * can just do the insert there. |
| 4090 | */ |
| 4091 | insert_el = left_el; |
| 4092 | } else { |
| 4093 | /* |
| 4094 | * Right split - we have to move the |
| 4095 | * existing record over to the left |
| 4096 | * leaf. The insert will be into the |
| 4097 | * newly created empty extent in the |
| 4098 | * right leaf. |
| 4099 | */ |
| 4100 | tmprec = &right_el->l_recs[index]; |
| 4101 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4102 | el = left_el; |
| 4103 | |
| 4104 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4105 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4106 | BUG_ON(index == -1); |
| 4107 | } |
| 4108 | } |
| 4109 | } else { |
| 4110 | BUG_ON(!left_path); |
| 4111 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4112 | /* |
| 4113 | * Left path is easy - we can just allow the insert to |
| 4114 | * happen. |
| 4115 | */ |
| 4116 | el = left_el; |
| 4117 | insert_el = left_el; |
| 4118 | index = ocfs2_search_extent_list(el, cpos); |
| 4119 | BUG_ON(index == -1); |
| 4120 | } |
| 4121 | |
| 4122 | rec = &el->l_recs[index]; |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4123 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4124 | split, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4125 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4126 | } |
| 4127 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4128 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4129 | * This function only does inserts on an allocation b-tree. For tree |
| 4130 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4131 | * |
| 4132 | * right_path is the path we want to do the actual insert |
| 4133 | * in. left_path should only be passed in if we need to update that |
| 4134 | * portion of the tree after an edge insert. |
| 4135 | */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4136 | static int ocfs2_insert_path(handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4137 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4138 | struct ocfs2_path *left_path, |
| 4139 | struct ocfs2_path *right_path, |
| 4140 | struct ocfs2_extent_rec *insert_rec, |
| 4141 | struct ocfs2_insert_type *insert) |
| 4142 | { |
| 4143 | int ret, subtree_index; |
| 4144 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4145 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4146 | if (left_path) { |
| 4147 | int credits = handle->h_buffer_credits; |
| 4148 | |
| 4149 | /* |
| 4150 | * There's a chance that left_path got passed back to |
| 4151 | * us without being accounted for in the |
| 4152 | * journal. Extend our transaction here to be sure we |
| 4153 | * can change those blocks. |
| 4154 | */ |
| 4155 | credits += left_path->p_tree_depth; |
| 4156 | |
| 4157 | ret = ocfs2_extend_trans(handle, credits); |
| 4158 | if (ret < 0) { |
| 4159 | mlog_errno(ret); |
| 4160 | goto out; |
| 4161 | } |
| 4162 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4163 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4164 | if (ret < 0) { |
| 4165 | mlog_errno(ret); |
| 4166 | goto out; |
| 4167 | } |
| 4168 | } |
| 4169 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4170 | /* |
| 4171 | * Pass both paths to the journal. The majority of inserts |
| 4172 | * will be touching all components anyway. |
| 4173 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4174 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4175 | if (ret < 0) { |
| 4176 | mlog_errno(ret); |
| 4177 | goto out; |
| 4178 | } |
| 4179 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4180 | if (insert->ins_split != SPLIT_NONE) { |
| 4181 | /* |
| 4182 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4183 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4184 | * function sort it all out. |
| 4185 | */ |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4186 | ocfs2_split_record(et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4187 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4188 | |
| 4189 | /* |
| 4190 | * Split might have modified either leaf and we don't |
| 4191 | * have a guarantee that the later edge insert will |
| 4192 | * dirty this for us. |
| 4193 | */ |
| 4194 | if (left_path) |
| 4195 | ret = ocfs2_journal_dirty(handle, |
| 4196 | path_leaf_bh(left_path)); |
| 4197 | if (ret) |
| 4198 | mlog_errno(ret); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4199 | } else |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4200 | ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path), |
| 4201 | insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4202 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4203 | ret = ocfs2_journal_dirty(handle, leaf_bh); |
| 4204 | if (ret) |
| 4205 | mlog_errno(ret); |
| 4206 | |
| 4207 | if (left_path) { |
| 4208 | /* |
| 4209 | * The rotate code has indicated that we need to fix |
| 4210 | * up portions of the tree after the insert. |
| 4211 | * |
| 4212 | * XXX: Should we extend the transaction here? |
| 4213 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4214 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4215 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4216 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4217 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | ret = 0; |
| 4221 | out: |
| 4222 | return ret; |
| 4223 | } |
| 4224 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4225 | static int ocfs2_do_insert_extent(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4226 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4227 | struct ocfs2_extent_rec *insert_rec, |
| 4228 | struct ocfs2_insert_type *type) |
| 4229 | { |
| 4230 | int ret, rotate = 0; |
| 4231 | u32 cpos; |
| 4232 | struct ocfs2_path *right_path = NULL; |
| 4233 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4234 | struct ocfs2_extent_list *el; |
| 4235 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4236 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4237 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4238 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4239 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4240 | if (ret) { |
| 4241 | mlog_errno(ret); |
| 4242 | goto out; |
| 4243 | } |
| 4244 | |
| 4245 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4246 | ocfs2_insert_at_leaf(et, insert_rec, el, type); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4247 | goto out_update_clusters; |
| 4248 | } |
| 4249 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4250 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4251 | if (!right_path) { |
| 4252 | ret = -ENOMEM; |
| 4253 | mlog_errno(ret); |
| 4254 | goto out; |
| 4255 | } |
| 4256 | |
| 4257 | /* |
| 4258 | * Determine the path to start with. Rotations need the |
| 4259 | * rightmost path, everything else can go directly to the |
| 4260 | * target leaf. |
| 4261 | */ |
| 4262 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4263 | if (type->ins_appending == APPEND_NONE && |
| 4264 | type->ins_contig == CONTIG_NONE) { |
| 4265 | rotate = 1; |
| 4266 | cpos = UINT_MAX; |
| 4267 | } |
| 4268 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4269 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4270 | if (ret) { |
| 4271 | mlog_errno(ret); |
| 4272 | goto out; |
| 4273 | } |
| 4274 | |
| 4275 | /* |
| 4276 | * Rotations and appends need special treatment - they modify |
| 4277 | * parts of the tree's above them. |
| 4278 | * |
| 4279 | * Both might pass back a path immediate to the left of the |
| 4280 | * one being inserted to. This will be cause |
| 4281 | * ocfs2_insert_path() to modify the rightmost records of |
| 4282 | * left_path to account for an edge insert. |
| 4283 | * |
| 4284 | * XXX: When modifying this code, keep in mind that an insert |
| 4285 | * can wind up skipping both of these two special cases... |
| 4286 | */ |
| 4287 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4288 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4289 | le32_to_cpu(insert_rec->e_cpos), |
| 4290 | right_path, &left_path); |
| 4291 | if (ret) { |
| 4292 | mlog_errno(ret); |
| 4293 | goto out; |
| 4294 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4295 | |
| 4296 | /* |
| 4297 | * ocfs2_rotate_tree_right() might have extended the |
| 4298 | * transaction without re-journaling our tree root. |
| 4299 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4300 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4301 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4302 | if (ret) { |
| 4303 | mlog_errno(ret); |
| 4304 | goto out; |
| 4305 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4306 | } else if (type->ins_appending == APPEND_TAIL |
| 4307 | && type->ins_contig != CONTIG_LEFT) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4308 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4309 | right_path, &left_path); |
| 4310 | if (ret) { |
| 4311 | mlog_errno(ret); |
| 4312 | goto out; |
| 4313 | } |
| 4314 | } |
| 4315 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4316 | ret = ocfs2_insert_path(handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4317 | insert_rec, type); |
| 4318 | if (ret) { |
| 4319 | mlog_errno(ret); |
| 4320 | goto out; |
| 4321 | } |
| 4322 | |
| 4323 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4324 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4325 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4326 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4327 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4328 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4329 | if (ret) |
| 4330 | mlog_errno(ret); |
| 4331 | |
| 4332 | out: |
| 4333 | ocfs2_free_path(left_path); |
| 4334 | ocfs2_free_path(right_path); |
| 4335 | |
| 4336 | return ret; |
| 4337 | } |
| 4338 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4339 | static enum ocfs2_contig_type |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4340 | ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et, |
| 4341 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4342 | struct ocfs2_extent_list *el, int index, |
| 4343 | struct ocfs2_extent_rec *split_rec) |
| 4344 | { |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4345 | int status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4346 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4347 | u32 left_cpos, right_cpos; |
| 4348 | struct ocfs2_extent_rec *rec = NULL; |
| 4349 | struct ocfs2_extent_list *new_el; |
| 4350 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4351 | struct buffer_head *bh; |
| 4352 | struct ocfs2_extent_block *eb; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4353 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4354 | |
| 4355 | if (index > 0) { |
| 4356 | rec = &el->l_recs[index - 1]; |
| 4357 | } else if (path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4358 | status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4359 | if (status) |
| 4360 | goto out; |
| 4361 | |
| 4362 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4363 | left_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4364 | if (!left_path) |
| 4365 | goto out; |
| 4366 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4367 | status = ocfs2_find_path(et->et_ci, left_path, |
| 4368 | left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4369 | if (status) |
| 4370 | goto out; |
| 4371 | |
| 4372 | new_el = path_leaf_el(left_path); |
| 4373 | |
| 4374 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4375 | le16_to_cpu(new_el->l_count)) { |
| 4376 | bh = path_leaf_bh(left_path); |
| 4377 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4378 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4379 | "Extent block #%llu has an " |
| 4380 | "invalid l_next_free_rec of " |
| 4381 | "%d. It should have " |
| 4382 | "matched the l_count of %d", |
| 4383 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4384 | le16_to_cpu(new_el->l_next_free_rec), |
| 4385 | le16_to_cpu(new_el->l_count)); |
| 4386 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4387 | goto out; |
| 4388 | } |
| 4389 | rec = &new_el->l_recs[ |
| 4390 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4391 | } |
| 4392 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4393 | |
| 4394 | /* |
| 4395 | * We're careful to check for an empty extent record here - |
| 4396 | * the merge code will know what to do if it sees one. |
| 4397 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4398 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4399 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4400 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4401 | ret = CONTIG_RIGHT; |
| 4402 | } else { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4403 | ret = ocfs2_extent_contig(sb, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4407 | rec = NULL; |
| 4408 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4409 | rec = &el->l_recs[index + 1]; |
| 4410 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4411 | path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4412 | status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4413 | if (status) |
| 4414 | goto out; |
| 4415 | |
| 4416 | if (right_cpos == 0) |
| 4417 | goto out; |
| 4418 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4419 | right_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4420 | if (!right_path) |
| 4421 | goto out; |
| 4422 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4423 | status = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4424 | if (status) |
| 4425 | goto out; |
| 4426 | |
| 4427 | new_el = path_leaf_el(right_path); |
| 4428 | rec = &new_el->l_recs[0]; |
| 4429 | if (ocfs2_is_empty_extent(rec)) { |
| 4430 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4431 | bh = path_leaf_bh(right_path); |
| 4432 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4433 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4434 | "Extent block #%llu has an " |
| 4435 | "invalid l_next_free_rec of %d", |
| 4436 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4437 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4438 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4439 | goto out; |
| 4440 | } |
| 4441 | rec = &new_el->l_recs[1]; |
| 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4446 | enum ocfs2_contig_type contig_type; |
| 4447 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4448 | contig_type = ocfs2_extent_contig(sb, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4449 | |
| 4450 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4451 | ret = CONTIG_LEFTRIGHT; |
| 4452 | else if (ret == CONTIG_NONE) |
| 4453 | ret = contig_type; |
| 4454 | } |
| 4455 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4456 | out: |
| 4457 | if (left_path) |
| 4458 | ocfs2_free_path(left_path); |
| 4459 | if (right_path) |
| 4460 | ocfs2_free_path(right_path); |
| 4461 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4462 | return ret; |
| 4463 | } |
| 4464 | |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4465 | static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4466 | struct ocfs2_insert_type *insert, |
| 4467 | struct ocfs2_extent_list *el, |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4468 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4469 | { |
| 4470 | int i; |
| 4471 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4472 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4473 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4474 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4475 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4476 | contig_type = ocfs2_extent_contig(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4477 | &el->l_recs[i], insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4478 | if (contig_type != CONTIG_NONE) { |
| 4479 | insert->ins_contig_index = i; |
| 4480 | break; |
| 4481 | } |
| 4482 | } |
| 4483 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4484 | |
| 4485 | if (insert->ins_contig != CONTIG_NONE) { |
| 4486 | struct ocfs2_extent_rec *rec = |
| 4487 | &el->l_recs[insert->ins_contig_index]; |
| 4488 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4489 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4490 | |
| 4491 | /* |
| 4492 | * Caller might want us to limit the size of extents, don't |
| 4493 | * calculate contiguousness if we might exceed that limit. |
| 4494 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4495 | if (et->et_max_leaf_clusters && |
| 4496 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4497 | insert->ins_contig = CONTIG_NONE; |
| 4498 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | /* |
| 4502 | * This should only be called against the righmost leaf extent list. |
| 4503 | * |
| 4504 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4505 | * insert at the tail of the rightmost leaf. |
| 4506 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4507 | * This should also work against the root extent list for tree's with 0 |
| 4508 | * 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] | 4509 | * then the logic here makes sense. |
| 4510 | */ |
| 4511 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4512 | struct ocfs2_extent_list *el, |
| 4513 | struct ocfs2_extent_rec *insert_rec) |
| 4514 | { |
| 4515 | int i; |
| 4516 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4517 | struct ocfs2_extent_rec *rec; |
| 4518 | |
| 4519 | insert->ins_appending = APPEND_NONE; |
| 4520 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4521 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4522 | |
| 4523 | if (!el->l_next_free_rec) |
| 4524 | goto set_tail_append; |
| 4525 | |
| 4526 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4527 | /* Were all records empty? */ |
| 4528 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4529 | goto set_tail_append; |
| 4530 | } |
| 4531 | |
| 4532 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4533 | rec = &el->l_recs[i]; |
| 4534 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4535 | if (cpos >= |
| 4536 | (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] | 4537 | goto set_tail_append; |
| 4538 | |
| 4539 | return; |
| 4540 | |
| 4541 | set_tail_append: |
| 4542 | insert->ins_appending = APPEND_TAIL; |
| 4543 | } |
| 4544 | |
| 4545 | /* |
| 4546 | * Helper function called at the begining of an insert. |
| 4547 | * |
| 4548 | * This computes a few things that are commonly used in the process of |
| 4549 | * inserting into the btree: |
| 4550 | * - Whether the new extent is contiguous with an existing one. |
| 4551 | * - The current tree depth. |
| 4552 | * - Whether the insert is an appending one. |
| 4553 | * - The total # of free records in the tree. |
| 4554 | * |
| 4555 | * All of the information is stored on the ocfs2_insert_type |
| 4556 | * structure. |
| 4557 | */ |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4558 | static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4559 | struct buffer_head **last_eb_bh, |
| 4560 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4561 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4562 | struct ocfs2_insert_type *insert) |
| 4563 | { |
| 4564 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4565 | struct ocfs2_extent_block *eb; |
| 4566 | struct ocfs2_extent_list *el; |
| 4567 | struct ocfs2_path *path = NULL; |
| 4568 | struct buffer_head *bh = NULL; |
| 4569 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4570 | insert->ins_split = SPLIT_NONE; |
| 4571 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4572 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4573 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4574 | |
| 4575 | if (el->l_tree_depth) { |
| 4576 | /* |
| 4577 | * If we have tree depth, we read in the |
| 4578 | * rightmost extent block ahead of time as |
| 4579 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4580 | * may want it later. |
| 4581 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4582 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4583 | ocfs2_et_get_last_eb_blk(et), |
| 4584 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4585 | if (ret) { |
| 4586 | mlog_exit(ret); |
| 4587 | goto out; |
| 4588 | } |
| 4589 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4590 | el = &eb->h_list; |
| 4591 | } |
| 4592 | |
| 4593 | /* |
| 4594 | * Unless we have a contiguous insert, we'll need to know if |
| 4595 | * there is room left in our allocation tree for another |
| 4596 | * extent record. |
| 4597 | * |
| 4598 | * XXX: This test is simplistic, we can search for empty |
| 4599 | * extent records too. |
| 4600 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4601 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4602 | le16_to_cpu(el->l_next_free_rec); |
| 4603 | |
| 4604 | if (!insert->ins_tree_depth) { |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4605 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4606 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4607 | return 0; |
| 4608 | } |
| 4609 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4610 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4611 | if (!path) { |
| 4612 | ret = -ENOMEM; |
| 4613 | mlog_errno(ret); |
| 4614 | goto out; |
| 4615 | } |
| 4616 | |
| 4617 | /* |
| 4618 | * In the case that we're inserting past what the tree |
| 4619 | * currently accounts for, ocfs2_find_path() will return for |
| 4620 | * us the rightmost tree path. This is accounted for below in |
| 4621 | * the appending code. |
| 4622 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4623 | 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] | 4624 | if (ret) { |
| 4625 | mlog_errno(ret); |
| 4626 | goto out; |
| 4627 | } |
| 4628 | |
| 4629 | el = path_leaf_el(path); |
| 4630 | |
| 4631 | /* |
| 4632 | * Now that we have the path, there's two things we want to determine: |
| 4633 | * 1) Contiguousness (also set contig_index if this is so) |
| 4634 | * |
| 4635 | * 2) Are we doing an append? We can trivially break this up |
| 4636 | * into two types of appends: simple record append, or a |
| 4637 | * rotate inside the tail leaf. |
| 4638 | */ |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4639 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4640 | |
| 4641 | /* |
| 4642 | * The insert code isn't quite ready to deal with all cases of |
| 4643 | * left contiguousness. Specifically, if it's an insert into |
| 4644 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4645 | * 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] | 4646 | * left. For now, just catch that case and fool the layers |
| 4647 | * above us. This works just fine for tree_depth == 0, which |
| 4648 | * is why we allow that above. |
| 4649 | */ |
| 4650 | if (insert->ins_contig == CONTIG_LEFT && |
| 4651 | insert->ins_contig_index == 0) |
| 4652 | insert->ins_contig = CONTIG_NONE; |
| 4653 | |
| 4654 | /* |
| 4655 | * Ok, so we can simply compare against last_eb to figure out |
| 4656 | * whether the path doesn't exist. This will only happen in |
| 4657 | * the case that we're doing a tail append, so maybe we can |
| 4658 | * take advantage of that information somehow. |
| 4659 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4660 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4661 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4662 | /* |
| 4663 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4664 | * tree path. This might be an appending insert. There are |
| 4665 | * two cases: |
| 4666 | * 1) We're doing a true append at the tail: |
| 4667 | * -This might even be off the end of the leaf |
| 4668 | * 2) We're "appending" by rotating in the tail |
| 4669 | */ |
| 4670 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4671 | } |
| 4672 | |
| 4673 | out: |
| 4674 | ocfs2_free_path(path); |
| 4675 | |
| 4676 | if (ret == 0) |
| 4677 | *last_eb_bh = bh; |
| 4678 | else |
| 4679 | brelse(bh); |
| 4680 | return ret; |
| 4681 | } |
| 4682 | |
| 4683 | /* |
| 4684 | * Insert an extent into an inode btree. |
| 4685 | * |
| 4686 | * The caller needs to update fe->i_clusters |
| 4687 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4688 | int ocfs2_insert_extent(struct ocfs2_super *osb, |
| 4689 | handle_t *handle, |
| 4690 | struct inode *inode, |
| 4691 | struct ocfs2_extent_tree *et, |
| 4692 | u32 cpos, |
| 4693 | u64 start_blk, |
| 4694 | u32 new_clusters, |
| 4695 | u8 flags, |
| 4696 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4697 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4698 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4699 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4700 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4701 | struct ocfs2_insert_type insert = {0, }; |
| 4702 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4703 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4704 | mlog(0, "add %u clusters at position %u to inode %llu\n", |
| 4705 | new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4706 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4707 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4708 | rec.e_cpos = cpu_to_le32(cpos); |
| 4709 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4710 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4711 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4712 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4713 | if (status) { |
| 4714 | mlog_errno(status); |
| 4715 | goto bail; |
| 4716 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4717 | |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4718 | status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4719 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4720 | if (status < 0) { |
| 4721 | mlog_errno(status); |
| 4722 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4723 | } |
| 4724 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4725 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 4726 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 4727 | "Insert.tree_depth: %d\n", |
| 4728 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4729 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4730 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4731 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4732 | status = ocfs2_grow_tree(handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4733 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4734 | meta_ac); |
| 4735 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4736 | mlog_errno(status); |
| 4737 | goto bail; |
| 4738 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4739 | } |
| 4740 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4741 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4742 | status = ocfs2_do_insert_extent(handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4743 | if (status < 0) |
| 4744 | mlog_errno(status); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame^] | 4745 | else |
| 4746 | ocfs2_et_extent_map_insert(et, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4747 | |
| 4748 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4749 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4750 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4751 | mlog_exit(status); |
| 4752 | return status; |
| 4753 | } |
| 4754 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4755 | /* |
| 4756 | * Allcate and add clusters into the extent b-tree. |
| 4757 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4758 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4759 | * it is not limited to the file storage. Any extent tree can use this |
| 4760 | * function if it implements the proper ocfs2_extent_tree. |
| 4761 | */ |
| 4762 | int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, |
| 4763 | struct inode *inode, |
| 4764 | u32 *logical_offset, |
| 4765 | u32 clusters_to_add, |
| 4766 | int mark_unwritten, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4767 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4768 | handle_t *handle, |
| 4769 | struct ocfs2_alloc_context *data_ac, |
| 4770 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4771 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4772 | { |
| 4773 | int status = 0; |
| 4774 | int free_extents; |
| 4775 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4776 | u32 bit_off, num_bits; |
| 4777 | u64 block; |
| 4778 | u8 flags = 0; |
| 4779 | |
| 4780 | BUG_ON(!clusters_to_add); |
| 4781 | |
| 4782 | if (mark_unwritten) |
| 4783 | flags = OCFS2_EXT_UNWRITTEN; |
| 4784 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4785 | free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4786 | if (free_extents < 0) { |
| 4787 | status = free_extents; |
| 4788 | mlog_errno(status); |
| 4789 | goto leave; |
| 4790 | } |
| 4791 | |
| 4792 | /* there are two cases which could cause us to EAGAIN in the |
| 4793 | * we-need-more-metadata case: |
| 4794 | * 1) we haven't reserved *any* |
| 4795 | * 2) we are so fragmented, we've needed to add metadata too |
| 4796 | * many times. */ |
| 4797 | if (!free_extents && !meta_ac) { |
| 4798 | mlog(0, "we haven't reserved any metadata!\n"); |
| 4799 | status = -EAGAIN; |
| 4800 | reason = RESTART_META; |
| 4801 | goto leave; |
| 4802 | } else if ((!free_extents) |
| 4803 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4804 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4805 | mlog(0, "filesystem is really fragmented...\n"); |
| 4806 | status = -EAGAIN; |
| 4807 | reason = RESTART_META; |
| 4808 | goto leave; |
| 4809 | } |
| 4810 | |
| 4811 | status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 4812 | clusters_to_add, &bit_off, &num_bits); |
| 4813 | if (status < 0) { |
| 4814 | if (status != -ENOSPC) |
| 4815 | mlog_errno(status); |
| 4816 | goto leave; |
| 4817 | } |
| 4818 | |
| 4819 | BUG_ON(num_bits > clusters_to_add); |
| 4820 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4821 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4822 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4823 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4824 | if (status < 0) { |
| 4825 | mlog_errno(status); |
| 4826 | goto leave; |
| 4827 | } |
| 4828 | |
| 4829 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
| 4830 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
| 4831 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4832 | status = ocfs2_insert_extent(osb, handle, inode, et, |
| 4833 | *logical_offset, block, |
| 4834 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4835 | if (status < 0) { |
| 4836 | mlog_errno(status); |
| 4837 | goto leave; |
| 4838 | } |
| 4839 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4840 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4841 | if (status < 0) { |
| 4842 | mlog_errno(status); |
| 4843 | goto leave; |
| 4844 | } |
| 4845 | |
| 4846 | clusters_to_add -= num_bits; |
| 4847 | *logical_offset += num_bits; |
| 4848 | |
| 4849 | if (clusters_to_add) { |
| 4850 | mlog(0, "need to alloc once more, wanted = %u\n", |
| 4851 | clusters_to_add); |
| 4852 | status = -EAGAIN; |
| 4853 | reason = RESTART_TRANS; |
| 4854 | } |
| 4855 | |
| 4856 | leave: |
| 4857 | mlog_exit(status); |
| 4858 | if (reason_ret) |
| 4859 | *reason_ret = reason; |
| 4860 | return status; |
| 4861 | } |
| 4862 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4863 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4864 | struct ocfs2_extent_rec *split_rec, |
| 4865 | u32 cpos, |
| 4866 | struct ocfs2_extent_rec *rec) |
| 4867 | { |
| 4868 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4869 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4870 | |
| 4871 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4872 | |
| 4873 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4874 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4875 | |
| 4876 | split_rec->e_blkno = rec->e_blkno; |
| 4877 | le64_add_cpu(&split_rec->e_blkno, |
| 4878 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4879 | |
| 4880 | split_rec->e_flags = rec->e_flags; |
| 4881 | } |
| 4882 | |
| 4883 | static int ocfs2_split_and_insert(struct inode *inode, |
| 4884 | handle_t *handle, |
| 4885 | struct ocfs2_path *path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4886 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4887 | struct buffer_head **last_eb_bh, |
| 4888 | int split_index, |
| 4889 | struct ocfs2_extent_rec *orig_split_rec, |
| 4890 | struct ocfs2_alloc_context *meta_ac) |
| 4891 | { |
| 4892 | int ret = 0, depth; |
| 4893 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4894 | struct ocfs2_extent_rec tmprec; |
| 4895 | struct ocfs2_extent_list *rightmost_el; |
| 4896 | struct ocfs2_extent_rec rec; |
| 4897 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4898 | struct ocfs2_insert_type insert; |
| 4899 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4900 | |
| 4901 | leftright: |
| 4902 | /* |
| 4903 | * Store a copy of the record on the stack - it might move |
| 4904 | * around as the tree is manipulated below. |
| 4905 | */ |
| 4906 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4907 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4908 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4909 | |
| 4910 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4911 | if (depth) { |
| 4912 | BUG_ON(!(*last_eb_bh)); |
| 4913 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4914 | rightmost_el = &eb->h_list; |
| 4915 | } |
| 4916 | |
| 4917 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4918 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4919 | ret = ocfs2_grow_tree(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4920 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4921 | if (ret) { |
| 4922 | mlog_errno(ret); |
| 4923 | goto out; |
| 4924 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4925 | } |
| 4926 | |
| 4927 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4928 | insert.ins_appending = APPEND_NONE; |
| 4929 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4930 | insert.ins_tree_depth = depth; |
| 4931 | |
| 4932 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 4933 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 4934 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 4935 | le16_to_cpu(rec.e_leaf_clusters); |
| 4936 | |
| 4937 | if (split_rec.e_cpos == rec.e_cpos) { |
| 4938 | insert.ins_split = SPLIT_LEFT; |
| 4939 | } else if (insert_range == rec_range) { |
| 4940 | insert.ins_split = SPLIT_RIGHT; |
| 4941 | } else { |
| 4942 | /* |
| 4943 | * Left/right split. We fake this as a right split |
| 4944 | * first and then make a second pass as a left split. |
| 4945 | */ |
| 4946 | insert.ins_split = SPLIT_RIGHT; |
| 4947 | |
| 4948 | ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range, |
| 4949 | &rec); |
| 4950 | |
| 4951 | split_rec = tmprec; |
| 4952 | |
| 4953 | BUG_ON(do_leftright); |
| 4954 | do_leftright = 1; |
| 4955 | } |
| 4956 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4957 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4958 | if (ret) { |
| 4959 | mlog_errno(ret); |
| 4960 | goto out; |
| 4961 | } |
| 4962 | |
| 4963 | if (do_leftright == 1) { |
| 4964 | u32 cpos; |
| 4965 | struct ocfs2_extent_list *el; |
| 4966 | |
| 4967 | do_leftright++; |
| 4968 | split_rec = *orig_split_rec; |
| 4969 | |
| 4970 | ocfs2_reinit_path(path, 1); |
| 4971 | |
| 4972 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4973 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4974 | if (ret) { |
| 4975 | mlog_errno(ret); |
| 4976 | goto out; |
| 4977 | } |
| 4978 | |
| 4979 | el = path_leaf_el(path); |
| 4980 | split_index = ocfs2_search_extent_list(el, cpos); |
| 4981 | goto leftright; |
| 4982 | } |
| 4983 | out: |
| 4984 | |
| 4985 | return ret; |
| 4986 | } |
| 4987 | |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4988 | static int ocfs2_replace_extent_rec(struct inode *inode, |
| 4989 | handle_t *handle, |
| 4990 | struct ocfs2_path *path, |
| 4991 | struct ocfs2_extent_list *el, |
| 4992 | int split_index, |
| 4993 | struct ocfs2_extent_rec *split_rec) |
| 4994 | { |
| 4995 | int ret; |
| 4996 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4997 | ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4998 | path_num_items(path) - 1); |
| 4999 | if (ret) { |
| 5000 | mlog_errno(ret); |
| 5001 | goto out; |
| 5002 | } |
| 5003 | |
| 5004 | el->l_recs[split_index] = *split_rec; |
| 5005 | |
| 5006 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5007 | out: |
| 5008 | return ret; |
| 5009 | } |
| 5010 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5011 | /* |
| 5012 | * Mark part or all of the extent record at split_index in the leaf |
| 5013 | * pointed to by path as written. This removes the unwritten |
| 5014 | * extent flag. |
| 5015 | * |
| 5016 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 5017 | * |
| 5018 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 5019 | * of the tree is required. All other cases will degrade into a less |
| 5020 | * optimal tree layout. |
| 5021 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5022 | * last_eb_bh should be the rightmost leaf block for any extent |
| 5023 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 5024 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5025 | * |
| 5026 | * This code is optimized for readability - several passes might be |
| 5027 | * made over certain portions of the tree. All of those blocks will |
| 5028 | * have been brought into cache (and pinned via the journal), so the |
| 5029 | * extra overhead is not expressed in terms of disk reads. |
| 5030 | */ |
| 5031 | static int __ocfs2_mark_extent_written(struct inode *inode, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5032 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5033 | handle_t *handle, |
| 5034 | struct ocfs2_path *path, |
| 5035 | int split_index, |
| 5036 | struct ocfs2_extent_rec *split_rec, |
| 5037 | struct ocfs2_alloc_context *meta_ac, |
| 5038 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5039 | { |
| 5040 | int ret = 0; |
| 5041 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 5042 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5043 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 5044 | struct ocfs2_merge_ctxt ctxt; |
| 5045 | struct ocfs2_extent_list *rightmost_el; |
| 5046 | |
Roel Kluin | 3cf0c50 | 2007-10-27 00:20:36 +0200 | [diff] [blame] | 5047 | if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5048 | ret = -EIO; |
| 5049 | mlog_errno(ret); |
| 5050 | goto out; |
| 5051 | } |
| 5052 | |
| 5053 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5054 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5055 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5056 | ret = -EIO; |
| 5057 | mlog_errno(ret); |
| 5058 | goto out; |
| 5059 | } |
| 5060 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 5061 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5062 | split_index, |
| 5063 | split_rec); |
| 5064 | |
| 5065 | /* |
| 5066 | * The core merge / split code wants to know how much room is |
| 5067 | * left in this inodes allocation tree, so we pass the |
| 5068 | * rightmost extent list. |
| 5069 | */ |
| 5070 | if (path->p_tree_depth) { |
| 5071 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5072 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5073 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5074 | ocfs2_et_get_last_eb_blk(et), |
| 5075 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5076 | if (ret) { |
| 5077 | mlog_exit(ret); |
| 5078 | goto out; |
| 5079 | } |
| 5080 | |
| 5081 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5082 | rightmost_el = &eb->h_list; |
| 5083 | } else |
| 5084 | rightmost_el = path_root_el(path); |
| 5085 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5086 | if (rec->e_cpos == split_rec->e_cpos && |
| 5087 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5088 | ctxt.c_split_covers_rec = 1; |
| 5089 | else |
| 5090 | ctxt.c_split_covers_rec = 0; |
| 5091 | |
| 5092 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5093 | |
Mark Fasheh | 015452b | 2007-09-12 10:21:22 -0700 | [diff] [blame] | 5094 | mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", |
| 5095 | split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, |
| 5096 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5097 | |
| 5098 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5099 | if (ctxt.c_split_covers_rec) |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5100 | ret = ocfs2_replace_extent_rec(inode, handle, |
| 5101 | path, el, |
| 5102 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5103 | else |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5104 | ret = ocfs2_split_and_insert(inode, handle, path, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5105 | &last_eb_bh, split_index, |
| 5106 | split_rec, meta_ac); |
| 5107 | if (ret) |
| 5108 | mlog_errno(ret); |
| 5109 | } else { |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5110 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5111 | split_index, split_rec, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5112 | dealloc, &ctxt); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5113 | if (ret) |
| 5114 | mlog_errno(ret); |
| 5115 | } |
| 5116 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5117 | out: |
| 5118 | brelse(last_eb_bh); |
| 5119 | return ret; |
| 5120 | } |
| 5121 | |
| 5122 | /* |
| 5123 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5124 | * |
| 5125 | * If the existing extent is larger than the request, initiate a |
| 5126 | * split. An attempt will be made at merging with adjacent extents. |
| 5127 | * |
| 5128 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5129 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5130 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5131 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5132 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5133 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5134 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5135 | { |
| 5136 | int ret, index; |
| 5137 | u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); |
| 5138 | struct ocfs2_extent_rec split_rec; |
| 5139 | struct ocfs2_path *left_path = NULL; |
| 5140 | struct ocfs2_extent_list *el; |
| 5141 | |
| 5142 | mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", |
| 5143 | inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); |
| 5144 | |
| 5145 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 5146 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 5147 | "that are being written to, but the feature bit " |
| 5148 | "is not set in the super block.", |
| 5149 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5150 | ret = -EROFS; |
| 5151 | goto out; |
| 5152 | } |
| 5153 | |
| 5154 | /* |
| 5155 | * XXX: This should be fixed up so that we just re-insert the |
| 5156 | * next extent records. |
| 5157 | */ |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 5158 | ocfs2_et_extent_map_truncate(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5159 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5160 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5161 | if (!left_path) { |
| 5162 | ret = -ENOMEM; |
| 5163 | mlog_errno(ret); |
| 5164 | goto out; |
| 5165 | } |
| 5166 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5167 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5168 | if (ret) { |
| 5169 | mlog_errno(ret); |
| 5170 | goto out; |
| 5171 | } |
| 5172 | el = path_leaf_el(left_path); |
| 5173 | |
| 5174 | index = ocfs2_search_extent_list(el, cpos); |
| 5175 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5176 | ocfs2_error(inode->i_sb, |
| 5177 | "Inode %llu has an extent at cpos %u which can no " |
| 5178 | "longer be found.\n", |
| 5179 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 5180 | ret = -EROFS; |
| 5181 | goto out; |
| 5182 | } |
| 5183 | |
| 5184 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5185 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5186 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5187 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
| 5188 | split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; |
| 5189 | split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; |
| 5190 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5191 | ret = __ocfs2_mark_extent_written(inode, et, handle, left_path, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5192 | index, &split_rec, meta_ac, |
| 5193 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5194 | if (ret) |
| 5195 | mlog_errno(ret); |
| 5196 | |
| 5197 | out: |
| 5198 | ocfs2_free_path(left_path); |
| 5199 | return ret; |
| 5200 | } |
| 5201 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5202 | 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] | 5203 | handle_t *handle, struct ocfs2_path *path, |
| 5204 | int index, u32 new_range, |
| 5205 | struct ocfs2_alloc_context *meta_ac) |
| 5206 | { |
| 5207 | int ret, depth, credits = handle->h_buffer_credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5208 | struct buffer_head *last_eb_bh = NULL; |
| 5209 | struct ocfs2_extent_block *eb; |
| 5210 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5211 | struct ocfs2_extent_rec split_rec; |
| 5212 | struct ocfs2_extent_rec *rec; |
| 5213 | struct ocfs2_insert_type insert; |
| 5214 | |
| 5215 | /* |
| 5216 | * Setup the record to split before we grow the tree. |
| 5217 | */ |
| 5218 | el = path_leaf_el(path); |
| 5219 | rec = &el->l_recs[index]; |
| 5220 | ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec); |
| 5221 | |
| 5222 | depth = path->p_tree_depth; |
| 5223 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5224 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5225 | ocfs2_et_get_last_eb_blk(et), |
| 5226 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5227 | if (ret < 0) { |
| 5228 | mlog_errno(ret); |
| 5229 | goto out; |
| 5230 | } |
| 5231 | |
| 5232 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5233 | rightmost_el = &eb->h_list; |
| 5234 | } else |
| 5235 | rightmost_el = path_leaf_el(path); |
| 5236 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 5237 | credits += path->p_tree_depth + |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 5238 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5239 | ret = ocfs2_extend_trans(handle, credits); |
| 5240 | if (ret) { |
| 5241 | mlog_errno(ret); |
| 5242 | goto out; |
| 5243 | } |
| 5244 | |
| 5245 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5246 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5247 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5248 | meta_ac); |
| 5249 | if (ret) { |
| 5250 | mlog_errno(ret); |
| 5251 | goto out; |
| 5252 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5256 | insert.ins_appending = APPEND_NONE; |
| 5257 | insert.ins_contig = CONTIG_NONE; |
| 5258 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5259 | insert.ins_tree_depth = depth; |
| 5260 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5261 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5262 | if (ret) |
| 5263 | mlog_errno(ret); |
| 5264 | |
| 5265 | out: |
| 5266 | brelse(last_eb_bh); |
| 5267 | return ret; |
| 5268 | } |
| 5269 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5270 | static int ocfs2_truncate_rec(handle_t *handle, |
| 5271 | struct ocfs2_extent_tree *et, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5272 | struct ocfs2_path *path, int index, |
| 5273 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5274 | u32 cpos, u32 len) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5275 | { |
| 5276 | int ret; |
| 5277 | u32 left_cpos, rec_range, trunc_range; |
| 5278 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5279 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5280 | struct ocfs2_path *left_path = NULL; |
| 5281 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5282 | struct ocfs2_extent_rec *rec; |
| 5283 | struct ocfs2_extent_block *eb; |
| 5284 | |
| 5285 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5286 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5287 | if (ret) { |
| 5288 | mlog_errno(ret); |
| 5289 | goto out; |
| 5290 | } |
| 5291 | |
| 5292 | index--; |
| 5293 | } |
| 5294 | |
| 5295 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5296 | path->p_tree_depth) { |
| 5297 | /* |
| 5298 | * Check whether this is the rightmost tree record. If |
| 5299 | * we remove all of this record or part of its right |
| 5300 | * edge then an update of the record lengths above it |
| 5301 | * will be required. |
| 5302 | */ |
| 5303 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5304 | if (eb->h_next_leaf_blk == 0) |
| 5305 | is_rightmost_tree_rec = 1; |
| 5306 | } |
| 5307 | |
| 5308 | rec = &el->l_recs[index]; |
| 5309 | if (index == 0 && path->p_tree_depth && |
| 5310 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5311 | /* |
| 5312 | * Changing the leftmost offset (via partial or whole |
| 5313 | * record truncate) of an interior (or rightmost) path |
| 5314 | * means we have to update the subtree that is formed |
| 5315 | * by this leaf and the one to it's left. |
| 5316 | * |
| 5317 | * There are two cases we can skip: |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5318 | * 1) Path is the leftmost one in our btree. |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5319 | * 2) The leaf is rightmost and will be empty after |
| 5320 | * we remove the extent record - the rotate code |
| 5321 | * knows how to update the newly formed edge. |
| 5322 | */ |
| 5323 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5324 | ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5325 | if (ret) { |
| 5326 | mlog_errno(ret); |
| 5327 | goto out; |
| 5328 | } |
| 5329 | |
| 5330 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5331 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5332 | if (!left_path) { |
| 5333 | ret = -ENOMEM; |
| 5334 | mlog_errno(ret); |
| 5335 | goto out; |
| 5336 | } |
| 5337 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5338 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5339 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5340 | if (ret) { |
| 5341 | mlog_errno(ret); |
| 5342 | goto out; |
| 5343 | } |
| 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5348 | handle->h_buffer_credits, |
| 5349 | path); |
| 5350 | if (ret) { |
| 5351 | mlog_errno(ret); |
| 5352 | goto out; |
| 5353 | } |
| 5354 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5355 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5356 | if (ret) { |
| 5357 | mlog_errno(ret); |
| 5358 | goto out; |
| 5359 | } |
| 5360 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5361 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5362 | if (ret) { |
| 5363 | mlog_errno(ret); |
| 5364 | goto out; |
| 5365 | } |
| 5366 | |
| 5367 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5368 | trunc_range = cpos + len; |
| 5369 | |
| 5370 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5371 | int next_free; |
| 5372 | |
| 5373 | memset(rec, 0, sizeof(*rec)); |
| 5374 | ocfs2_cleanup_merge(el, index); |
| 5375 | wants_rotate = 1; |
| 5376 | |
| 5377 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5378 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5379 | /* |
| 5380 | * We skip the edge update if this path will |
| 5381 | * be deleted by the rotate code. |
| 5382 | */ |
| 5383 | rec = &el->l_recs[next_free - 1]; |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5384 | ocfs2_adjust_rightmost_records(handle, et, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5385 | rec); |
| 5386 | } |
| 5387 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5388 | /* Remove leftmost portion of the record. */ |
| 5389 | le32_add_cpu(&rec->e_cpos, len); |
| 5390 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5391 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5392 | } else if (rec_range == trunc_range) { |
| 5393 | /* Remove rightmost portion of the record */ |
| 5394 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5395 | if (is_rightmost_tree_rec) |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5396 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5397 | } else { |
| 5398 | /* Caller should have trapped this. */ |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5399 | mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) " |
| 5400 | "(%u, %u)\n", |
| 5401 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5402 | le32_to_cpu(rec->e_cpos), |
| 5403 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5404 | BUG(); |
| 5405 | } |
| 5406 | |
| 5407 | if (left_path) { |
| 5408 | int subtree_index; |
| 5409 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5410 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5411 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5412 | subtree_index); |
| 5413 | } |
| 5414 | |
| 5415 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5416 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5417 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5418 | if (ret) { |
| 5419 | mlog_errno(ret); |
| 5420 | goto out; |
| 5421 | } |
| 5422 | |
| 5423 | out: |
| 5424 | ocfs2_free_path(left_path); |
| 5425 | return ret; |
| 5426 | } |
| 5427 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5428 | int ocfs2_remove_extent(struct inode *inode, |
| 5429 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5430 | u32 cpos, u32 len, handle_t *handle, |
| 5431 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5432 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5433 | { |
| 5434 | int ret, index; |
| 5435 | u32 rec_range, trunc_range; |
| 5436 | struct ocfs2_extent_rec *rec; |
| 5437 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5438 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5439 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 5440 | /* |
| 5441 | * XXX: Why are we truncating to 0 instead of wherever this |
| 5442 | * affects us? |
| 5443 | */ |
| 5444 | ocfs2_et_extent_map_truncate(et, 0); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5445 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5446 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5447 | if (!path) { |
| 5448 | ret = -ENOMEM; |
| 5449 | mlog_errno(ret); |
| 5450 | goto out; |
| 5451 | } |
| 5452 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5453 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5454 | if (ret) { |
| 5455 | mlog_errno(ret); |
| 5456 | goto out; |
| 5457 | } |
| 5458 | |
| 5459 | el = path_leaf_el(path); |
| 5460 | index = ocfs2_search_extent_list(el, cpos); |
| 5461 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5462 | ocfs2_error(inode->i_sb, |
| 5463 | "Inode %llu has an extent at cpos %u which can no " |
| 5464 | "longer be found.\n", |
| 5465 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 5466 | ret = -EROFS; |
| 5467 | goto out; |
| 5468 | } |
| 5469 | |
| 5470 | /* |
| 5471 | * We have 3 cases of extent removal: |
| 5472 | * 1) Range covers the entire extent rec |
| 5473 | * 2) Range begins or ends on one edge of the extent rec |
| 5474 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5475 | * |
| 5476 | * For case 1 we remove the extent rec and left rotate to |
| 5477 | * fill the hole. |
| 5478 | * |
| 5479 | * For case 2 we just shrink the existing extent rec, with a |
| 5480 | * tree update if the shrinking edge is also the edge of an |
| 5481 | * extent block. |
| 5482 | * |
| 5483 | * For case 3 we do a right split to turn the extent rec into |
| 5484 | * something case 2 can handle. |
| 5485 | */ |
| 5486 | rec = &el->l_recs[index]; |
| 5487 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5488 | trunc_range = cpos + len; |
| 5489 | |
| 5490 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5491 | |
| 5492 | mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d " |
| 5493 | "(cpos %u, len %u)\n", |
| 5494 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index, |
| 5495 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 5496 | |
| 5497 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5498 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5499 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5500 | if (ret) { |
| 5501 | mlog_errno(ret); |
| 5502 | goto out; |
| 5503 | } |
| 5504 | } else { |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5505 | ret = ocfs2_split_tree(inode, et, handle, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5506 | trunc_range, meta_ac); |
| 5507 | if (ret) { |
| 5508 | mlog_errno(ret); |
| 5509 | goto out; |
| 5510 | } |
| 5511 | |
| 5512 | /* |
| 5513 | * The split could have manipulated the tree enough to |
| 5514 | * move the record location, so we have to look for it again. |
| 5515 | */ |
| 5516 | ocfs2_reinit_path(path, 1); |
| 5517 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5518 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5519 | if (ret) { |
| 5520 | mlog_errno(ret); |
| 5521 | goto out; |
| 5522 | } |
| 5523 | |
| 5524 | el = path_leaf_el(path); |
| 5525 | index = ocfs2_search_extent_list(el, cpos); |
| 5526 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 5527 | ocfs2_error(inode->i_sb, |
| 5528 | "Inode %llu: split at cpos %u lost record.", |
| 5529 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5530 | cpos); |
| 5531 | ret = -EROFS; |
| 5532 | goto out; |
| 5533 | } |
| 5534 | |
| 5535 | /* |
| 5536 | * Double check our values here. If anything is fishy, |
| 5537 | * it's easier to catch it at the top level. |
| 5538 | */ |
| 5539 | rec = &el->l_recs[index]; |
| 5540 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5541 | ocfs2_rec_clusters(el, rec); |
| 5542 | if (rec_range != trunc_range) { |
| 5543 | ocfs2_error(inode->i_sb, |
| 5544 | "Inode %llu: error after split at cpos %u" |
| 5545 | "trunc len %u, existing record is (%u,%u)", |
| 5546 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5547 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5548 | ocfs2_rec_clusters(el, rec)); |
| 5549 | ret = -EROFS; |
| 5550 | goto out; |
| 5551 | } |
| 5552 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5553 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5554 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5555 | if (ret) { |
| 5556 | mlog_errno(ret); |
| 5557 | goto out; |
| 5558 | } |
| 5559 | } |
| 5560 | |
| 5561 | out: |
| 5562 | ocfs2_free_path(path); |
| 5563 | return ret; |
| 5564 | } |
| 5565 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5566 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5567 | struct ocfs2_extent_tree *et, |
| 5568 | u32 cpos, u32 phys_cpos, u32 len, |
| 5569 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5570 | { |
| 5571 | int ret; |
| 5572 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5573 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5574 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5575 | handle_t *handle; |
| 5576 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5577 | |
| 5578 | ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); |
| 5579 | if (ret) { |
| 5580 | mlog_errno(ret); |
| 5581 | return ret; |
| 5582 | } |
| 5583 | |
| 5584 | mutex_lock(&tl_inode->i_mutex); |
| 5585 | |
| 5586 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5587 | ret = __ocfs2_flush_truncate_log(osb); |
| 5588 | if (ret < 0) { |
| 5589 | mlog_errno(ret); |
| 5590 | goto out; |
| 5591 | } |
| 5592 | } |
| 5593 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5594 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5595 | if (IS_ERR(handle)) { |
| 5596 | ret = PTR_ERR(handle); |
| 5597 | mlog_errno(ret); |
| 5598 | goto out; |
| 5599 | } |
| 5600 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5601 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5602 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5603 | if (ret) { |
| 5604 | mlog_errno(ret); |
| 5605 | goto out; |
| 5606 | } |
| 5607 | |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5608 | vfs_dq_free_space_nodirty(inode, |
| 5609 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5610 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5611 | ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac, |
| 5612 | dealloc); |
| 5613 | if (ret) { |
| 5614 | mlog_errno(ret); |
| 5615 | goto out_commit; |
| 5616 | } |
| 5617 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5618 | ocfs2_et_update_clusters(et, -len); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5619 | |
| 5620 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
| 5621 | if (ret) { |
| 5622 | mlog_errno(ret); |
| 5623 | goto out_commit; |
| 5624 | } |
| 5625 | |
| 5626 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 5627 | if (ret) |
| 5628 | mlog_errno(ret); |
| 5629 | |
| 5630 | out_commit: |
| 5631 | ocfs2_commit_trans(osb, handle); |
| 5632 | out: |
| 5633 | mutex_unlock(&tl_inode->i_mutex); |
| 5634 | |
| 5635 | if (meta_ac) |
| 5636 | ocfs2_free_alloc_context(meta_ac); |
| 5637 | |
| 5638 | return ret; |
| 5639 | } |
| 5640 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5641 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5642 | { |
| 5643 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5644 | struct ocfs2_dinode *di; |
| 5645 | struct ocfs2_truncate_log *tl; |
| 5646 | |
| 5647 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5648 | tl = &di->id2.i_dealloc; |
| 5649 | |
| 5650 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5651 | "slot %d, invalid truncate log parameters: used = " |
| 5652 | "%u, count = %u\n", osb->slot_num, |
| 5653 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5654 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5655 | } |
| 5656 | |
| 5657 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5658 | unsigned int new_start) |
| 5659 | { |
| 5660 | unsigned int tail_index; |
| 5661 | unsigned int current_tail; |
| 5662 | |
| 5663 | /* No records, nothing to coalesce */ |
| 5664 | if (!le16_to_cpu(tl->tl_used)) |
| 5665 | return 0; |
| 5666 | |
| 5667 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5668 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5669 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5670 | |
| 5671 | return current_tail == new_start; |
| 5672 | } |
| 5673 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5674 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5675 | handle_t *handle, |
| 5676 | u64 start_blk, |
| 5677 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5678 | { |
| 5679 | int status, index; |
| 5680 | unsigned int start_cluster, tl_count; |
| 5681 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5682 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5683 | struct ocfs2_dinode *di; |
| 5684 | struct ocfs2_truncate_log *tl; |
| 5685 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5686 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 5687 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5688 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5689 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5690 | |
| 5691 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5692 | |
| 5693 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5694 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5695 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5696 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5697 | * corruption is a code bug */ |
| 5698 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5699 | |
| 5700 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5701 | tl_count = le16_to_cpu(tl->tl_count); |
| 5702 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5703 | tl_count == 0, |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5704 | "Truncate record count on #%llu invalid " |
| 5705 | "wanted %u, actual %u\n", |
| 5706 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5707 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5708 | le16_to_cpu(tl->tl_count)); |
| 5709 | |
| 5710 | /* Caller should have known to flush before calling us. */ |
| 5711 | index = le16_to_cpu(tl->tl_used); |
| 5712 | if (index >= tl_count) { |
| 5713 | status = -ENOSPC; |
| 5714 | mlog_errno(status); |
| 5715 | goto bail; |
| 5716 | } |
| 5717 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5718 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5719 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5720 | if (status < 0) { |
| 5721 | mlog_errno(status); |
| 5722 | goto bail; |
| 5723 | } |
| 5724 | |
| 5725 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5726 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 5727 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5728 | |
| 5729 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5730 | /* |
| 5731 | * Move index back to the record we are coalescing with. |
| 5732 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5733 | */ |
| 5734 | index--; |
| 5735 | |
| 5736 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 5737 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 5738 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5739 | num_clusters); |
| 5740 | } else { |
| 5741 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5742 | tl->tl_used = cpu_to_le16(index + 1); |
| 5743 | } |
| 5744 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5745 | |
| 5746 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5747 | if (status < 0) { |
| 5748 | mlog_errno(status); |
| 5749 | goto bail; |
| 5750 | } |
| 5751 | |
| 5752 | bail: |
| 5753 | mlog_exit(status); |
| 5754 | return status; |
| 5755 | } |
| 5756 | |
| 5757 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5758 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5759 | struct inode *data_alloc_inode, |
| 5760 | struct buffer_head *data_alloc_bh) |
| 5761 | { |
| 5762 | int status = 0; |
| 5763 | int i; |
| 5764 | unsigned int num_clusters; |
| 5765 | u64 start_blk; |
| 5766 | struct ocfs2_truncate_rec rec; |
| 5767 | struct ocfs2_dinode *di; |
| 5768 | struct ocfs2_truncate_log *tl; |
| 5769 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5770 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5771 | |
| 5772 | mlog_entry_void(); |
| 5773 | |
| 5774 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5775 | tl = &di->id2.i_dealloc; |
| 5776 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5777 | while (i >= 0) { |
| 5778 | /* Caller has given us at least enough credits to |
| 5779 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5780 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5781 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5782 | if (status < 0) { |
| 5783 | mlog_errno(status); |
| 5784 | goto bail; |
| 5785 | } |
| 5786 | |
| 5787 | tl->tl_used = cpu_to_le16(i); |
| 5788 | |
| 5789 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5790 | if (status < 0) { |
| 5791 | mlog_errno(status); |
| 5792 | goto bail; |
| 5793 | } |
| 5794 | |
| 5795 | /* TODO: Perhaps we can calculate the bulk of the |
| 5796 | * credits up front rather than extending like |
| 5797 | * this. */ |
| 5798 | status = ocfs2_extend_trans(handle, |
| 5799 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5800 | if (status < 0) { |
| 5801 | mlog_errno(status); |
| 5802 | goto bail; |
| 5803 | } |
| 5804 | |
| 5805 | rec = tl->tl_recs[i]; |
| 5806 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5807 | le32_to_cpu(rec.t_start)); |
| 5808 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5809 | |
| 5810 | /* if start_blk is not set, we ignore the record as |
| 5811 | * invalid. */ |
| 5812 | if (start_blk) { |
| 5813 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 5814 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 5815 | |
| 5816 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5817 | data_alloc_bh, start_blk, |
| 5818 | num_clusters); |
| 5819 | if (status < 0) { |
| 5820 | mlog_errno(status); |
| 5821 | goto bail; |
| 5822 | } |
| 5823 | } |
| 5824 | i--; |
| 5825 | } |
| 5826 | |
| 5827 | bail: |
| 5828 | mlog_exit(status); |
| 5829 | return status; |
| 5830 | } |
| 5831 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5832 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5833 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5834 | { |
| 5835 | int status; |
| 5836 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5837 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5838 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5839 | struct inode *data_alloc_inode = NULL; |
| 5840 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5841 | struct buffer_head *data_alloc_bh = NULL; |
| 5842 | struct ocfs2_dinode *di; |
| 5843 | struct ocfs2_truncate_log *tl; |
| 5844 | |
| 5845 | mlog_entry_void(); |
| 5846 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5847 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5848 | |
| 5849 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5850 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5851 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5852 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5853 | * corruption is a code bug */ |
| 5854 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5855 | |
| 5856 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5857 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5858 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 5859 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5860 | if (!num_to_flush) { |
| 5861 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5862 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 5866 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 5867 | OCFS2_INVALID_SLOT); |
| 5868 | if (!data_alloc_inode) { |
| 5869 | status = -EINVAL; |
| 5870 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5871 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5872 | } |
| 5873 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5874 | mutex_lock(&data_alloc_inode->i_mutex); |
| 5875 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5876 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5877 | if (status < 0) { |
| 5878 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5879 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5880 | } |
| 5881 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 5882 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5883 | if (IS_ERR(handle)) { |
| 5884 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5885 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5886 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 5890 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5891 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5892 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5893 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5894 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5895 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5896 | out_unlock: |
| 5897 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5898 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5899 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5900 | out_mutex: |
| 5901 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 5902 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5903 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5904 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5905 | mlog_exit(status); |
| 5906 | return status; |
| 5907 | } |
| 5908 | |
| 5909 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 5910 | { |
| 5911 | int status; |
| 5912 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5913 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5914 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5915 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5916 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5917 | |
| 5918 | return status; |
| 5919 | } |
| 5920 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5921 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5922 | { |
| 5923 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5924 | struct ocfs2_super *osb = |
| 5925 | container_of(work, struct ocfs2_super, |
| 5926 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5927 | |
| 5928 | mlog_entry_void(); |
| 5929 | |
| 5930 | status = ocfs2_flush_truncate_log(osb); |
| 5931 | if (status < 0) |
| 5932 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 5933 | else |
| 5934 | ocfs2_init_inode_steal_slot(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5935 | |
| 5936 | mlog_exit(status); |
| 5937 | } |
| 5938 | |
| 5939 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 5940 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 5941 | int cancel) |
| 5942 | { |
| 5943 | if (osb->osb_tl_inode) { |
| 5944 | /* We want to push off log flushes while truncates are |
| 5945 | * still running. */ |
| 5946 | if (cancel) |
| 5947 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 5948 | |
| 5949 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 5950 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 5951 | } |
| 5952 | } |
| 5953 | |
| 5954 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 5955 | int slot_num, |
| 5956 | struct inode **tl_inode, |
| 5957 | struct buffer_head **tl_bh) |
| 5958 | { |
| 5959 | int status; |
| 5960 | struct inode *inode = NULL; |
| 5961 | struct buffer_head *bh = NULL; |
| 5962 | |
| 5963 | inode = ocfs2_get_system_file_inode(osb, |
| 5964 | TRUNCATE_LOG_SYSTEM_INODE, |
| 5965 | slot_num); |
| 5966 | if (!inode) { |
| 5967 | status = -EINVAL; |
| 5968 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 5969 | goto bail; |
| 5970 | } |
| 5971 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 5972 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5973 | if (status < 0) { |
| 5974 | iput(inode); |
| 5975 | mlog_errno(status); |
| 5976 | goto bail; |
| 5977 | } |
| 5978 | |
| 5979 | *tl_inode = inode; |
| 5980 | *tl_bh = bh; |
| 5981 | bail: |
| 5982 | mlog_exit(status); |
| 5983 | return status; |
| 5984 | } |
| 5985 | |
| 5986 | /* called during the 1st stage of node recovery. we stamp a clean |
| 5987 | * truncate log and pass back a copy for processing later. if the |
| 5988 | * truncate log does not require processing, a *tl_copy is set to |
| 5989 | * NULL. */ |
| 5990 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 5991 | int slot_num, |
| 5992 | struct ocfs2_dinode **tl_copy) |
| 5993 | { |
| 5994 | int status; |
| 5995 | struct inode *tl_inode = NULL; |
| 5996 | struct buffer_head *tl_bh = NULL; |
| 5997 | struct ocfs2_dinode *di; |
| 5998 | struct ocfs2_truncate_log *tl; |
| 5999 | |
| 6000 | *tl_copy = NULL; |
| 6001 | |
| 6002 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 6003 | |
| 6004 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 6005 | if (status < 0) { |
| 6006 | mlog_errno(status); |
| 6007 | goto bail; |
| 6008 | } |
| 6009 | |
| 6010 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6011 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 6012 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 6013 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 6014 | * so any corruption is a code bug */ |
| 6015 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 6016 | |
| 6017 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6018 | if (le16_to_cpu(tl->tl_used)) { |
| 6019 | mlog(0, "We'll have %u logs to recover\n", |
| 6020 | le16_to_cpu(tl->tl_used)); |
| 6021 | |
| 6022 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 6023 | if (!(*tl_copy)) { |
| 6024 | status = -ENOMEM; |
| 6025 | mlog_errno(status); |
| 6026 | goto bail; |
| 6027 | } |
| 6028 | |
| 6029 | /* Assuming the write-out below goes well, this copy |
| 6030 | * will be passed back to recovery for processing. */ |
| 6031 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 6032 | |
| 6033 | /* All we need to do to clear the truncate log is set |
| 6034 | * tl_used. */ |
| 6035 | tl->tl_used = 0; |
| 6036 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6037 | 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] | 6038 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6039 | if (status < 0) { |
| 6040 | mlog_errno(status); |
| 6041 | goto bail; |
| 6042 | } |
| 6043 | } |
| 6044 | |
| 6045 | bail: |
| 6046 | if (tl_inode) |
| 6047 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6048 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6049 | |
| 6050 | if (status < 0 && (*tl_copy)) { |
| 6051 | kfree(*tl_copy); |
| 6052 | *tl_copy = NULL; |
| 6053 | } |
| 6054 | |
| 6055 | mlog_exit(status); |
| 6056 | return status; |
| 6057 | } |
| 6058 | |
| 6059 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6060 | struct ocfs2_dinode *tl_copy) |
| 6061 | { |
| 6062 | int status = 0; |
| 6063 | int i; |
| 6064 | unsigned int clusters, num_recs, start_cluster; |
| 6065 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6066 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6067 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6068 | struct ocfs2_truncate_log *tl; |
| 6069 | |
| 6070 | mlog_entry_void(); |
| 6071 | |
| 6072 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6073 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6074 | return -EINVAL; |
| 6075 | } |
| 6076 | |
| 6077 | tl = &tl_copy->id2.i_dealloc; |
| 6078 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 6079 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6080 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6081 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6082 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6083 | for(i = 0; i < num_recs; i++) { |
| 6084 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6085 | status = __ocfs2_flush_truncate_log(osb); |
| 6086 | if (status < 0) { |
| 6087 | mlog_errno(status); |
| 6088 | goto bail_up; |
| 6089 | } |
| 6090 | } |
| 6091 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6092 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6093 | if (IS_ERR(handle)) { |
| 6094 | status = PTR_ERR(handle); |
| 6095 | mlog_errno(status); |
| 6096 | goto bail_up; |
| 6097 | } |
| 6098 | |
| 6099 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6100 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6101 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6102 | |
| 6103 | status = ocfs2_truncate_log_append(osb, handle, |
| 6104 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6105 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6106 | if (status < 0) { |
| 6107 | mlog_errno(status); |
| 6108 | goto bail_up; |
| 6109 | } |
| 6110 | } |
| 6111 | |
| 6112 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6113 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6114 | |
| 6115 | mlog_exit(status); |
| 6116 | return status; |
| 6117 | } |
| 6118 | |
| 6119 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6120 | { |
| 6121 | int status; |
| 6122 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6123 | |
| 6124 | mlog_entry_void(); |
| 6125 | |
| 6126 | if (tl_inode) { |
| 6127 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6128 | flush_workqueue(ocfs2_wq); |
| 6129 | |
| 6130 | status = ocfs2_flush_truncate_log(osb); |
| 6131 | if (status < 0) |
| 6132 | mlog_errno(status); |
| 6133 | |
| 6134 | brelse(osb->osb_tl_bh); |
| 6135 | iput(osb->osb_tl_inode); |
| 6136 | } |
| 6137 | |
| 6138 | mlog_exit_void(); |
| 6139 | } |
| 6140 | |
| 6141 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6142 | { |
| 6143 | int status; |
| 6144 | struct inode *tl_inode = NULL; |
| 6145 | struct buffer_head *tl_bh = NULL; |
| 6146 | |
| 6147 | mlog_entry_void(); |
| 6148 | |
| 6149 | status = ocfs2_get_truncate_log_info(osb, |
| 6150 | osb->slot_num, |
| 6151 | &tl_inode, |
| 6152 | &tl_bh); |
| 6153 | if (status < 0) |
| 6154 | mlog_errno(status); |
| 6155 | |
| 6156 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6157 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6158 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6159 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6160 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6161 | osb->osb_tl_bh = tl_bh; |
| 6162 | osb->osb_tl_inode = tl_inode; |
| 6163 | |
| 6164 | mlog_exit(status); |
| 6165 | return status; |
| 6166 | } |
| 6167 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6168 | /* |
| 6169 | * Delayed de-allocation of suballocator blocks. |
| 6170 | * |
| 6171 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6172 | * |
| 6173 | * The locking for this can get extremely complicated, especially when |
| 6174 | * the suballocator inodes to delete from aren't known until deep |
| 6175 | * within an unrelated codepath. |
| 6176 | * |
| 6177 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6178 | * btree could have been grown by any number of nodes each allocating |
| 6179 | * out of their own suballoc inode. |
| 6180 | * |
| 6181 | * These structures allow the delay of block de-allocation until a |
| 6182 | * later time, when locking of multiple cluster inodes won't cause |
| 6183 | * deadlock. |
| 6184 | */ |
| 6185 | |
| 6186 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6187 | * Describe a single bit freed from a suballocator. For the block |
| 6188 | * suballocators, it represents one block. For the global cluster |
| 6189 | * allocator, it represents some clusters and free_bit indicates |
| 6190 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6191 | */ |
| 6192 | struct ocfs2_cached_block_free { |
| 6193 | struct ocfs2_cached_block_free *free_next; |
| 6194 | u64 free_blk; |
| 6195 | unsigned int free_bit; |
| 6196 | }; |
| 6197 | |
| 6198 | struct ocfs2_per_slot_free_list { |
| 6199 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6200 | int f_inode_type; |
| 6201 | int f_slot; |
| 6202 | struct ocfs2_cached_block_free *f_first; |
| 6203 | }; |
| 6204 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6205 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6206 | int sysfile_type, |
| 6207 | int slot, |
| 6208 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6209 | { |
| 6210 | int ret; |
| 6211 | u64 bg_blkno; |
| 6212 | handle_t *handle; |
| 6213 | struct inode *inode; |
| 6214 | struct buffer_head *di_bh = NULL; |
| 6215 | struct ocfs2_cached_block_free *tmp; |
| 6216 | |
| 6217 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6218 | if (!inode) { |
| 6219 | ret = -EINVAL; |
| 6220 | mlog_errno(ret); |
| 6221 | goto out; |
| 6222 | } |
| 6223 | |
| 6224 | mutex_lock(&inode->i_mutex); |
| 6225 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6226 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6227 | if (ret) { |
| 6228 | mlog_errno(ret); |
| 6229 | goto out_mutex; |
| 6230 | } |
| 6231 | |
| 6232 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6233 | if (IS_ERR(handle)) { |
| 6234 | ret = PTR_ERR(handle); |
| 6235 | mlog_errno(ret); |
| 6236 | goto out_unlock; |
| 6237 | } |
| 6238 | |
| 6239 | while (head) { |
| 6240 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6241 | head->free_bit); |
| 6242 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 6243 | head->free_bit, (unsigned long long)head->free_blk); |
| 6244 | |
| 6245 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6246 | head->free_bit, bg_blkno, 1); |
| 6247 | if (ret) { |
| 6248 | mlog_errno(ret); |
| 6249 | goto out_journal; |
| 6250 | } |
| 6251 | |
| 6252 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 6253 | if (ret) { |
| 6254 | mlog_errno(ret); |
| 6255 | goto out_journal; |
| 6256 | } |
| 6257 | |
| 6258 | tmp = head; |
| 6259 | head = head->free_next; |
| 6260 | kfree(tmp); |
| 6261 | } |
| 6262 | |
| 6263 | out_journal: |
| 6264 | ocfs2_commit_trans(osb, handle); |
| 6265 | |
| 6266 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6267 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6268 | brelse(di_bh); |
| 6269 | out_mutex: |
| 6270 | mutex_unlock(&inode->i_mutex); |
| 6271 | iput(inode); |
| 6272 | out: |
| 6273 | while(head) { |
| 6274 | /* Premature exit may have left some dangling items. */ |
| 6275 | tmp = head; |
| 6276 | head = head->free_next; |
| 6277 | kfree(tmp); |
| 6278 | } |
| 6279 | |
| 6280 | return ret; |
| 6281 | } |
| 6282 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6283 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6284 | u64 blkno, unsigned int bit) |
| 6285 | { |
| 6286 | int ret = 0; |
| 6287 | struct ocfs2_cached_block_free *item; |
| 6288 | |
| 6289 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6290 | if (item == NULL) { |
| 6291 | ret = -ENOMEM; |
| 6292 | mlog_errno(ret); |
| 6293 | return ret; |
| 6294 | } |
| 6295 | |
| 6296 | mlog(0, "Insert clusters: (bit %u, blk %llu)\n", |
| 6297 | bit, (unsigned long long)blkno); |
| 6298 | |
| 6299 | item->free_blk = blkno; |
| 6300 | item->free_bit = bit; |
| 6301 | item->free_next = ctxt->c_global_allocator; |
| 6302 | |
| 6303 | ctxt->c_global_allocator = item; |
| 6304 | return ret; |
| 6305 | } |
| 6306 | |
| 6307 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6308 | struct ocfs2_cached_block_free *head) |
| 6309 | { |
| 6310 | struct ocfs2_cached_block_free *tmp; |
| 6311 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6312 | handle_t *handle; |
| 6313 | int ret = 0; |
| 6314 | |
| 6315 | mutex_lock(&tl_inode->i_mutex); |
| 6316 | |
| 6317 | while (head) { |
| 6318 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6319 | ret = __ocfs2_flush_truncate_log(osb); |
| 6320 | if (ret < 0) { |
| 6321 | mlog_errno(ret); |
| 6322 | break; |
| 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6327 | if (IS_ERR(handle)) { |
| 6328 | ret = PTR_ERR(handle); |
| 6329 | mlog_errno(ret); |
| 6330 | break; |
| 6331 | } |
| 6332 | |
| 6333 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6334 | head->free_bit); |
| 6335 | |
| 6336 | ocfs2_commit_trans(osb, handle); |
| 6337 | tmp = head; |
| 6338 | head = head->free_next; |
| 6339 | kfree(tmp); |
| 6340 | |
| 6341 | if (ret < 0) { |
| 6342 | mlog_errno(ret); |
| 6343 | break; |
| 6344 | } |
| 6345 | } |
| 6346 | |
| 6347 | mutex_unlock(&tl_inode->i_mutex); |
| 6348 | |
| 6349 | while (head) { |
| 6350 | /* Premature exit may have left some dangling items. */ |
| 6351 | tmp = head; |
| 6352 | head = head->free_next; |
| 6353 | kfree(tmp); |
| 6354 | } |
| 6355 | |
| 6356 | return ret; |
| 6357 | } |
| 6358 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6359 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6360 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6361 | { |
| 6362 | int ret = 0, ret2; |
| 6363 | struct ocfs2_per_slot_free_list *fl; |
| 6364 | |
| 6365 | if (!ctxt) |
| 6366 | return 0; |
| 6367 | |
| 6368 | while (ctxt->c_first_suballocator) { |
| 6369 | fl = ctxt->c_first_suballocator; |
| 6370 | |
| 6371 | if (fl->f_first) { |
| 6372 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 6373 | fl->f_inode_type, fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6374 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6375 | fl->f_inode_type, |
| 6376 | fl->f_slot, |
| 6377 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6378 | if (ret2) |
| 6379 | mlog_errno(ret2); |
| 6380 | if (!ret) |
| 6381 | ret = ret2; |
| 6382 | } |
| 6383 | |
| 6384 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6385 | kfree(fl); |
| 6386 | } |
| 6387 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6388 | if (ctxt->c_global_allocator) { |
| 6389 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6390 | ctxt->c_global_allocator); |
| 6391 | if (ret2) |
| 6392 | mlog_errno(ret2); |
| 6393 | if (!ret) |
| 6394 | ret = ret2; |
| 6395 | |
| 6396 | ctxt->c_global_allocator = NULL; |
| 6397 | } |
| 6398 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6399 | return ret; |
| 6400 | } |
| 6401 | |
| 6402 | static struct ocfs2_per_slot_free_list * |
| 6403 | ocfs2_find_per_slot_free_list(int type, |
| 6404 | int slot, |
| 6405 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6406 | { |
| 6407 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6408 | |
| 6409 | while (fl) { |
| 6410 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6411 | return fl; |
| 6412 | |
| 6413 | fl = fl->f_next_suballocator; |
| 6414 | } |
| 6415 | |
| 6416 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6417 | if (fl) { |
| 6418 | fl->f_inode_type = type; |
| 6419 | fl->f_slot = slot; |
| 6420 | fl->f_first = NULL; |
| 6421 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6422 | |
| 6423 | ctxt->c_first_suballocator = fl; |
| 6424 | } |
| 6425 | return fl; |
| 6426 | } |
| 6427 | |
| 6428 | static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6429 | int type, int slot, u64 blkno, |
| 6430 | unsigned int bit) |
| 6431 | { |
| 6432 | int ret; |
| 6433 | struct ocfs2_per_slot_free_list *fl; |
| 6434 | struct ocfs2_cached_block_free *item; |
| 6435 | |
| 6436 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6437 | if (fl == NULL) { |
| 6438 | ret = -ENOMEM; |
| 6439 | mlog_errno(ret); |
| 6440 | goto out; |
| 6441 | } |
| 6442 | |
| 6443 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6444 | if (item == NULL) { |
| 6445 | ret = -ENOMEM; |
| 6446 | mlog_errno(ret); |
| 6447 | goto out; |
| 6448 | } |
| 6449 | |
| 6450 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 6451 | type, slot, bit, (unsigned long long)blkno); |
| 6452 | |
| 6453 | item->free_blk = blkno; |
| 6454 | item->free_bit = bit; |
| 6455 | item->free_next = fl->f_first; |
| 6456 | |
| 6457 | fl->f_first = item; |
| 6458 | |
| 6459 | ret = 0; |
| 6460 | out: |
| 6461 | return ret; |
| 6462 | } |
| 6463 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6464 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6465 | struct ocfs2_extent_block *eb) |
| 6466 | { |
| 6467 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6468 | le16_to_cpu(eb->h_suballoc_slot), |
| 6469 | le64_to_cpu(eb->h_blkno), |
| 6470 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6471 | } |
| 6472 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6473 | /* This function will figure out whether the currently last extent |
| 6474 | * block will be deleted, and if it will, what the new last extent |
| 6475 | * block will be so we can update his h_next_leaf_blk field, as well |
| 6476 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6477 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6478 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6479 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6480 | struct buffer_head **new_last_eb) |
| 6481 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6482 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6483 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6484 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6485 | struct ocfs2_extent_block *eb; |
| 6486 | struct ocfs2_extent_list *el; |
| 6487 | struct buffer_head *bh = NULL; |
| 6488 | |
| 6489 | *new_last_eb = NULL; |
| 6490 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6491 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6492 | if (!path->p_tree_depth) |
| 6493 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6494 | |
| 6495 | /* trunc to zero special case - this makes tree_depth = 0 |
| 6496 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6497 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6498 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6499 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6500 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6501 | BUG_ON(!el->l_next_free_rec); |
| 6502 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6503 | /* |
| 6504 | * Make sure that this extent list will actually be empty |
| 6505 | * after we clear away the data. We can shortcut out if |
| 6506 | * there's more than one non-empty extent in the |
| 6507 | * list. Otherwise, a check of the remaining extent is |
| 6508 | * necessary. |
| 6509 | */ |
| 6510 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 6511 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6512 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6513 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6514 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6515 | |
| 6516 | /* We may have a valid extent in index 1, check it. */ |
| 6517 | if (next_free == 2) |
| 6518 | rec = &el->l_recs[1]; |
| 6519 | |
| 6520 | /* |
| 6521 | * Fall through - no more nonempty extents, so we want |
| 6522 | * to delete this leaf. |
| 6523 | */ |
| 6524 | } else { |
| 6525 | if (next_free > 1) |
| 6526 | goto out; |
| 6527 | |
| 6528 | rec = &el->l_recs[0]; |
| 6529 | } |
| 6530 | |
| 6531 | if (rec) { |
| 6532 | /* |
| 6533 | * Check it we'll only be trimming off the end of this |
| 6534 | * cluster. |
| 6535 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6536 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6537 | goto out; |
| 6538 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6539 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6540 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 6541 | if (ret) { |
| 6542 | mlog_errno(ret); |
| 6543 | goto out; |
| 6544 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6545 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 6546 | 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] | 6547 | if (ret) { |
| 6548 | mlog_errno(ret); |
| 6549 | goto out; |
| 6550 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6551 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6552 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 6553 | el = &eb->h_list; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 6554 | |
| 6555 | /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). |
| 6556 | * Any corruption is a code bug. */ |
| 6557 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6558 | |
| 6559 | *new_last_eb = bh; |
| 6560 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6561 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 6562 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 6563 | out: |
| 6564 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6565 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6566 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6567 | } |
| 6568 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6569 | /* |
| 6570 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 6571 | * during truncate. |
| 6572 | * |
| 6573 | * The caller needs to: |
| 6574 | * - start journaling of each path component. |
| 6575 | * - compute and fully set up any new last ext block |
| 6576 | */ |
| 6577 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 6578 | handle_t *handle, struct ocfs2_truncate_context *tc, |
| 6579 | u32 clusters_to_del, u64 *delete_start) |
| 6580 | { |
| 6581 | int ret, i, index = path->p_tree_depth; |
| 6582 | u32 new_edge = 0; |
| 6583 | u64 deleted_eb = 0; |
| 6584 | struct buffer_head *bh; |
| 6585 | struct ocfs2_extent_list *el; |
| 6586 | struct ocfs2_extent_rec *rec; |
| 6587 | |
| 6588 | *delete_start = 0; |
| 6589 | |
| 6590 | while (index >= 0) { |
| 6591 | bh = path->p_node[index].bh; |
| 6592 | el = path->p_node[index].el; |
| 6593 | |
| 6594 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 6595 | index, (unsigned long long)bh->b_blocknr); |
| 6596 | |
| 6597 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 6598 | |
| 6599 | if (index != |
| 6600 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 6601 | ocfs2_error(inode->i_sb, |
| 6602 | "Inode %lu has invalid ext. block %llu", |
| 6603 | inode->i_ino, |
| 6604 | (unsigned long long)bh->b_blocknr); |
| 6605 | ret = -EROFS; |
| 6606 | goto out; |
| 6607 | } |
| 6608 | |
| 6609 | find_tail_record: |
| 6610 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 6611 | rec = &el->l_recs[i]; |
| 6612 | |
| 6613 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 6614 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6615 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6616 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6617 | le16_to_cpu(el->l_next_free_rec)); |
| 6618 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6619 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6620 | |
| 6621 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 6622 | /* |
| 6623 | * If the leaf block contains a single empty |
| 6624 | * extent and no records, we can just remove |
| 6625 | * the block. |
| 6626 | */ |
| 6627 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 6628 | memset(rec, 0, |
| 6629 | sizeof(struct ocfs2_extent_rec)); |
| 6630 | el->l_next_free_rec = cpu_to_le16(0); |
| 6631 | |
| 6632 | goto delete; |
| 6633 | } |
| 6634 | |
| 6635 | /* |
| 6636 | * Remove any empty extents by shifting things |
| 6637 | * left. That should make life much easier on |
| 6638 | * the code below. This condition is rare |
| 6639 | * enough that we shouldn't see a performance |
| 6640 | * hit. |
| 6641 | */ |
| 6642 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 6643 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6644 | |
| 6645 | for(i = 0; |
| 6646 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 6647 | el->l_recs[i] = el->l_recs[i + 1]; |
| 6648 | |
| 6649 | memset(&el->l_recs[i], 0, |
| 6650 | sizeof(struct ocfs2_extent_rec)); |
| 6651 | |
| 6652 | /* |
| 6653 | * We've modified our extent list. The |
| 6654 | * simplest way to handle this change |
| 6655 | * is to being the search from the |
| 6656 | * start again. |
| 6657 | */ |
| 6658 | goto find_tail_record; |
| 6659 | } |
| 6660 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6661 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6662 | |
| 6663 | /* |
| 6664 | * We'll use "new_edge" on our way back up the |
| 6665 | * tree to know what our rightmost cpos is. |
| 6666 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6667 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6668 | new_edge += le32_to_cpu(rec->e_cpos); |
| 6669 | |
| 6670 | /* |
| 6671 | * The caller will use this to delete data blocks. |
| 6672 | */ |
| 6673 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 6674 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6675 | le16_to_cpu(rec->e_leaf_clusters)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6676 | |
| 6677 | /* |
| 6678 | * If it's now empty, remove this record. |
| 6679 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6680 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6681 | memset(rec, 0, |
| 6682 | sizeof(struct ocfs2_extent_rec)); |
| 6683 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6684 | } |
| 6685 | } else { |
| 6686 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 6687 | memset(rec, 0, |
| 6688 | sizeof(struct ocfs2_extent_rec)); |
| 6689 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6690 | |
| 6691 | goto delete; |
| 6692 | } |
| 6693 | |
| 6694 | /* Can this actually happen? */ |
| 6695 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 6696 | goto delete; |
| 6697 | |
| 6698 | /* |
| 6699 | * We never actually deleted any clusters |
| 6700 | * because our leaf was empty. There's no |
| 6701 | * reason to adjust the rightmost edge then. |
| 6702 | */ |
| 6703 | if (new_edge == 0) |
| 6704 | goto delete; |
| 6705 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6706 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 6707 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6708 | -le32_to_cpu(rec->e_cpos)); |
| 6709 | |
| 6710 | /* |
| 6711 | * A deleted child record should have been |
| 6712 | * caught above. |
| 6713 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6714 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | delete: |
| 6718 | ret = ocfs2_journal_dirty(handle, bh); |
| 6719 | if (ret) { |
| 6720 | mlog_errno(ret); |
| 6721 | goto out; |
| 6722 | } |
| 6723 | |
| 6724 | mlog(0, "extent list container %llu, after: record %d: " |
| 6725 | "(%u, %u, %llu), next = %u.\n", |
| 6726 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6727 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6728 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6729 | le16_to_cpu(el->l_next_free_rec)); |
| 6730 | |
| 6731 | /* |
| 6732 | * We must be careful to only attempt delete of an |
| 6733 | * extent block (and not the root inode block). |
| 6734 | */ |
| 6735 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 6736 | struct ocfs2_extent_block *eb = |
| 6737 | (struct ocfs2_extent_block *)bh->b_data; |
| 6738 | |
| 6739 | /* |
| 6740 | * Save this for use when processing the |
| 6741 | * parent block. |
| 6742 | */ |
| 6743 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 6744 | |
| 6745 | mlog(0, "deleting this extent block.\n"); |
| 6746 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6747 | ocfs2_remove_from_cache(INODE_CACHE(inode), bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6748 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6749 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6750 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 6751 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 6752 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6753 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 6754 | /* An error here is not fatal. */ |
| 6755 | if (ret < 0) |
| 6756 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6757 | } else { |
| 6758 | deleted_eb = 0; |
| 6759 | } |
| 6760 | |
| 6761 | index--; |
| 6762 | } |
| 6763 | |
| 6764 | ret = 0; |
| 6765 | out: |
| 6766 | return ret; |
| 6767 | } |
| 6768 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6769 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 6770 | unsigned int clusters_to_del, |
| 6771 | struct inode *inode, |
| 6772 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6773 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6774 | struct ocfs2_truncate_context *tc, |
| 6775 | struct ocfs2_path *path) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6776 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6777 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6778 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6779 | struct ocfs2_extent_block *last_eb = NULL; |
| 6780 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6781 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6782 | u64 delete_blk = 0; |
| 6783 | |
| 6784 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6785 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6786 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6787 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6788 | if (status < 0) { |
| 6789 | mlog_errno(status); |
| 6790 | goto bail; |
| 6791 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6792 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6793 | /* |
| 6794 | * Each component will be touched, so we might as well journal |
| 6795 | * here to avoid having to handle errors later. |
| 6796 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6797 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6798 | if (status < 0) { |
| 6799 | mlog_errno(status); |
| 6800 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | if (last_eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6804 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6805 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6806 | if (status < 0) { |
| 6807 | mlog_errno(status); |
| 6808 | goto bail; |
| 6809 | } |
| 6810 | |
| 6811 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6812 | } |
| 6813 | |
| 6814 | el = &(fe->id2.i_list); |
| 6815 | |
| 6816 | /* |
| 6817 | * Lower levels depend on this never happening, but it's best |
| 6818 | * to check it up here before changing the tree. |
| 6819 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6820 | 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] | 6821 | ocfs2_error(inode->i_sb, |
| 6822 | "Inode %lu has an empty extent record, depth %u\n", |
| 6823 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6824 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6825 | goto bail; |
| 6826 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6827 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6828 | vfs_dq_free_space_nodirty(inode, |
| 6829 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6830 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 6831 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 6832 | clusters_to_del; |
| 6833 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 6834 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 6835 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6836 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6837 | status = ocfs2_trim_tree(inode, path, handle, tc, |
| 6838 | clusters_to_del, &delete_blk); |
| 6839 | if (status) { |
| 6840 | mlog_errno(status); |
| 6841 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6842 | } |
| 6843 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6844 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6845 | /* trunc to zero is a special case. */ |
| 6846 | el->l_tree_depth = 0; |
| 6847 | fe->i_last_eb_blk = 0; |
| 6848 | } else if (last_eb) |
| 6849 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 6850 | |
| 6851 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 6852 | if (status < 0) { |
| 6853 | mlog_errno(status); |
| 6854 | goto bail; |
| 6855 | } |
| 6856 | |
| 6857 | if (last_eb) { |
| 6858 | /* If there will be a new last extent block, then by |
| 6859 | * definition, there cannot be any leaves to the right of |
| 6860 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6861 | last_eb->h_next_leaf_blk = 0; |
| 6862 | status = ocfs2_journal_dirty(handle, last_eb_bh); |
| 6863 | if (status < 0) { |
| 6864 | mlog_errno(status); |
| 6865 | goto bail; |
| 6866 | } |
| 6867 | } |
| 6868 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6869 | if (delete_blk) { |
| 6870 | status = ocfs2_truncate_log_append(osb, handle, delete_blk, |
| 6871 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6872 | if (status < 0) { |
| 6873 | mlog_errno(status); |
| 6874 | goto bail; |
| 6875 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6876 | } |
| 6877 | status = 0; |
| 6878 | bail: |
Tao Ma | 60e2ec4 | 2009-08-12 14:42:47 +0800 | [diff] [blame] | 6879 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6880 | mlog_exit(status); |
| 6881 | return status; |
| 6882 | } |
| 6883 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6884 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6885 | { |
| 6886 | set_buffer_uptodate(bh); |
| 6887 | mark_buffer_dirty(bh); |
| 6888 | return 0; |
| 6889 | } |
| 6890 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6891 | static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 6892 | unsigned int from, unsigned int to, |
| 6893 | struct page *page, int zero, u64 *phys) |
| 6894 | { |
| 6895 | int ret, partial = 0; |
| 6896 | |
| 6897 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 6898 | if (ret) |
| 6899 | mlog_errno(ret); |
| 6900 | |
| 6901 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 6902 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6903 | |
| 6904 | /* |
| 6905 | * Need to set the buffers we zero'd into uptodate |
| 6906 | * here if they aren't - ocfs2_map_page_blocks() |
| 6907 | * might've skipped some |
| 6908 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6909 | ret = walk_page_buffers(handle, page_buffers(page), |
| 6910 | from, to, &partial, |
| 6911 | ocfs2_zero_func); |
| 6912 | if (ret < 0) |
| 6913 | mlog_errno(ret); |
| 6914 | else if (ocfs2_should_order_data(inode)) { |
| 6915 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6916 | if (ret < 0) |
| 6917 | mlog_errno(ret); |
| 6918 | } |
| 6919 | |
| 6920 | if (!partial) |
| 6921 | SetPageUptodate(page); |
| 6922 | |
| 6923 | flush_dcache_page(page); |
| 6924 | } |
| 6925 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6926 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 6927 | loff_t end, struct page **pages, |
| 6928 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6929 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6930 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6931 | struct page *page; |
| 6932 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 6933 | struct super_block *sb = inode->i_sb; |
| 6934 | |
| 6935 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 6936 | |
| 6937 | if (numpages == 0) |
| 6938 | goto out; |
| 6939 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6940 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6941 | for(i = 0; i < numpages; i++) { |
| 6942 | page = pages[i]; |
| 6943 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6944 | from = start & (PAGE_CACHE_SIZE - 1); |
| 6945 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 6946 | to = end & (PAGE_CACHE_SIZE - 1); |
| 6947 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6948 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 6949 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 6950 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6951 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 6952 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6953 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6954 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6955 | } |
| 6956 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6957 | if (pages) |
| 6958 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6959 | } |
| 6960 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6961 | 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] | 6962 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6963 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6964 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6965 | struct super_block *sb = inode->i_sb; |
| 6966 | struct address_space *mapping = inode->i_mapping; |
| 6967 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6968 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6969 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6970 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6971 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6972 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 6973 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 6974 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6975 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6976 | last_page_bytes = PAGE_ALIGN(end); |
| 6977 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6978 | do { |
| 6979 | pages[numpages] = grab_cache_page(mapping, index); |
| 6980 | if (!pages[numpages]) { |
| 6981 | ret = -ENOMEM; |
| 6982 | mlog_errno(ret); |
| 6983 | goto out; |
| 6984 | } |
| 6985 | |
| 6986 | numpages++; |
| 6987 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6988 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6989 | |
| 6990 | out: |
| 6991 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6992 | if (pages) |
| 6993 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6994 | numpages = 0; |
| 6995 | } |
| 6996 | |
| 6997 | *num = numpages; |
| 6998 | |
| 6999 | return ret; |
| 7000 | } |
| 7001 | |
| 7002 | /* |
| 7003 | * Zero the area past i_size but still within an allocated |
| 7004 | * cluster. This avoids exposing nonzero data on subsequent file |
| 7005 | * extends. |
| 7006 | * |
| 7007 | * We need to call this before i_size is updated on the inode because |
| 7008 | * otherwise block_write_full_page() will skip writeout of pages past |
| 7009 | * i_size. The new_i_size parameter is passed for this reason. |
| 7010 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7011 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 7012 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7013 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7014 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7015 | struct page **pages = NULL; |
| 7016 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7017 | unsigned int ext_flags; |
| 7018 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7019 | |
| 7020 | /* |
| 7021 | * File systems which don't support sparse files zero on every |
| 7022 | * extend. |
| 7023 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7024 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7025 | return 0; |
| 7026 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7027 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7028 | sizeof(struct page *), GFP_NOFS); |
| 7029 | if (pages == NULL) { |
| 7030 | ret = -ENOMEM; |
| 7031 | mlog_errno(ret); |
| 7032 | goto out; |
| 7033 | } |
| 7034 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7035 | if (range_start == range_end) |
| 7036 | goto out; |
| 7037 | |
| 7038 | ret = ocfs2_extent_map_get_blocks(inode, |
| 7039 | range_start >> sb->s_blocksize_bits, |
| 7040 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7041 | if (ret) { |
| 7042 | mlog_errno(ret); |
| 7043 | goto out; |
| 7044 | } |
| 7045 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7046 | /* |
| 7047 | * Tail is a hole, or is marked unwritten. In either case, we |
| 7048 | * can count on read and write to return/push zero's. |
| 7049 | */ |
| 7050 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7051 | goto out; |
| 7052 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7053 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 7054 | &numpages); |
| 7055 | if (ret) { |
| 7056 | mlog_errno(ret); |
| 7057 | goto out; |
| 7058 | } |
| 7059 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7060 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 7061 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7062 | |
| 7063 | /* |
| 7064 | * Initiate writeout of the pages we zero'd here. We don't |
| 7065 | * wait on them - the truncate_inode_pages() call later will |
| 7066 | * do that for us. |
| 7067 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7068 | ret = do_sync_mapping_range(inode->i_mapping, range_start, |
| 7069 | range_end - 1, SYNC_FILE_RANGE_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7070 | if (ret) |
| 7071 | mlog_errno(ret); |
| 7072 | |
| 7073 | out: |
| 7074 | if (pages) |
| 7075 | kfree(pages); |
| 7076 | |
| 7077 | return ret; |
| 7078 | } |
| 7079 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7080 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 7081 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7082 | { |
| 7083 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7084 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7085 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7086 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 7087 | memset(&di->id2, 0, blocksize - |
| 7088 | offsetof(struct ocfs2_dinode, id2) - |
| 7089 | xattrsize); |
| 7090 | else |
| 7091 | memset(&di->id2, 0, blocksize - |
| 7092 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7093 | } |
| 7094 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7095 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 7096 | struct ocfs2_dinode *di) |
| 7097 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7098 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7099 | di->id2.i_list.l_tree_depth = 0; |
| 7100 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7101 | di->id2.i_list.l_count = cpu_to_le16( |
| 7102 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7103 | } |
| 7104 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7105 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 7106 | { |
| 7107 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7108 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7109 | |
| 7110 | spin_lock(&oi->ip_lock); |
| 7111 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 7112 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7113 | spin_unlock(&oi->ip_lock); |
| 7114 | |
| 7115 | /* |
| 7116 | * We clear the entire i_data structure here so that all |
| 7117 | * fields can be properly initialized. |
| 7118 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7119 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7120 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7121 | idata->id_count = cpu_to_le16( |
| 7122 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7123 | } |
| 7124 | |
| 7125 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 7126 | struct buffer_head *di_bh) |
| 7127 | { |
| 7128 | int ret, i, has_data, num_pages = 0; |
| 7129 | handle_t *handle; |
| 7130 | u64 uninitialized_var(block); |
| 7131 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7132 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7133 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7134 | struct ocfs2_alloc_context *data_ac = NULL; |
| 7135 | struct page **pages = NULL; |
| 7136 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7137 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7138 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7139 | |
| 7140 | has_data = i_size_read(inode) ? 1 : 0; |
| 7141 | |
| 7142 | if (has_data) { |
| 7143 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 7144 | sizeof(struct page *), GFP_NOFS); |
| 7145 | if (pages == NULL) { |
| 7146 | ret = -ENOMEM; |
| 7147 | mlog_errno(ret); |
| 7148 | goto out; |
| 7149 | } |
| 7150 | |
| 7151 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 7152 | if (ret) { |
| 7153 | mlog_errno(ret); |
| 7154 | goto out; |
| 7155 | } |
| 7156 | } |
| 7157 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7158 | handle = ocfs2_start_trans(osb, |
| 7159 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7160 | if (IS_ERR(handle)) { |
| 7161 | ret = PTR_ERR(handle); |
| 7162 | mlog_errno(ret); |
| 7163 | goto out_unlock; |
| 7164 | } |
| 7165 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7166 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7167 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7168 | if (ret) { |
| 7169 | mlog_errno(ret); |
| 7170 | goto out_commit; |
| 7171 | } |
| 7172 | |
| 7173 | if (has_data) { |
| 7174 | u32 bit_off, num; |
| 7175 | unsigned int page_end; |
| 7176 | u64 phys; |
| 7177 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7178 | if (vfs_dq_alloc_space_nodirty(inode, |
| 7179 | ocfs2_clusters_to_bytes(osb->sb, 1))) { |
| 7180 | ret = -EDQUOT; |
| 7181 | goto out_commit; |
| 7182 | } |
| 7183 | did_quota = 1; |
| 7184 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7185 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 7186 | &num); |
| 7187 | if (ret) { |
| 7188 | mlog_errno(ret); |
| 7189 | goto out_commit; |
| 7190 | } |
| 7191 | |
| 7192 | /* |
| 7193 | * Save two copies, one for insert, and one that can |
| 7194 | * be changed by ocfs2_map_and_dirty_page() below. |
| 7195 | */ |
| 7196 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 7197 | |
| 7198 | /* |
| 7199 | * Non sparse file systems zero on extend, so no need |
| 7200 | * to do that now. |
| 7201 | */ |
| 7202 | if (!ocfs2_sparse_alloc(osb) && |
| 7203 | PAGE_CACHE_SIZE < osb->s_clustersize) |
| 7204 | end = PAGE_CACHE_SIZE; |
| 7205 | |
| 7206 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 7207 | if (ret) { |
| 7208 | mlog_errno(ret); |
| 7209 | goto out_commit; |
| 7210 | } |
| 7211 | |
| 7212 | /* |
| 7213 | * This should populate the 1st page for us and mark |
| 7214 | * it up to date. |
| 7215 | */ |
| 7216 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 7217 | if (ret) { |
| 7218 | mlog_errno(ret); |
| 7219 | goto out_commit; |
| 7220 | } |
| 7221 | |
| 7222 | page_end = PAGE_CACHE_SIZE; |
| 7223 | if (PAGE_CACHE_SIZE > osb->s_clustersize) |
| 7224 | page_end = osb->s_clustersize; |
| 7225 | |
| 7226 | for (i = 0; i < num_pages; i++) |
| 7227 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 7228 | pages[i], i > 0, &phys); |
| 7229 | } |
| 7230 | |
| 7231 | spin_lock(&oi->ip_lock); |
| 7232 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 7233 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7234 | spin_unlock(&oi->ip_lock); |
| 7235 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7236 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7237 | |
| 7238 | ocfs2_journal_dirty(handle, di_bh); |
| 7239 | |
| 7240 | if (has_data) { |
| 7241 | /* |
| 7242 | * An error at this point should be extremely rare. If |
| 7243 | * this proves to be false, we could always re-build |
| 7244 | * the in-inode data from our pages. |
| 7245 | */ |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 7246 | ocfs2_init_dinode_extent_tree(&et, inode, di_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7247 | ret = ocfs2_insert_extent(osb, handle, inode, &et, |
| 7248 | 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7249 | if (ret) { |
| 7250 | mlog_errno(ret); |
| 7251 | goto out_commit; |
| 7252 | } |
| 7253 | |
| 7254 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7255 | } |
| 7256 | |
| 7257 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7258 | if (ret < 0 && did_quota) |
| 7259 | vfs_dq_free_space_nodirty(inode, |
| 7260 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7261 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7262 | ocfs2_commit_trans(osb, handle); |
| 7263 | |
| 7264 | out_unlock: |
| 7265 | if (data_ac) |
| 7266 | ocfs2_free_alloc_context(data_ac); |
| 7267 | |
| 7268 | out: |
| 7269 | if (pages) { |
| 7270 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7271 | kfree(pages); |
| 7272 | } |
| 7273 | |
| 7274 | return ret; |
| 7275 | } |
| 7276 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7277 | /* |
| 7278 | * It is expected, that by the time you call this function, |
| 7279 | * inode->i_size and fe->i_size have been adjusted. |
| 7280 | * |
| 7281 | * WARNING: This will kfree the truncate context |
| 7282 | */ |
| 7283 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7284 | struct inode *inode, |
| 7285 | struct buffer_head *fe_bh, |
| 7286 | struct ocfs2_truncate_context *tc) |
| 7287 | { |
| 7288 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7289 | u32 clusters_to_del, new_highest_cpos, range; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7290 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 7291 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7292 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7293 | struct ocfs2_path *path = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 7294 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7295 | |
| 7296 | mlog_entry_void(); |
| 7297 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7298 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7299 | i_size_read(inode)); |
| 7300 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7301 | path = ocfs2_new_path(fe_bh, &di->id2.i_list, |
| 7302 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7303 | if (!path) { |
| 7304 | status = -ENOMEM; |
| 7305 | mlog_errno(status); |
| 7306 | goto bail; |
| 7307 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7308 | |
| 7309 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7310 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7311 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7312 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7313 | * Check that we still have allocation to delete. |
| 7314 | */ |
| 7315 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7316 | status = 0; |
| 7317 | goto bail; |
| 7318 | } |
| 7319 | |
| 7320 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7321 | * Truncate always works against the rightmost tree branch. |
| 7322 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7323 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7324 | if (status) { |
| 7325 | mlog_errno(status); |
| 7326 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7327 | } |
| 7328 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7329 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 7330 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 7331 | |
| 7332 | /* |
| 7333 | * By now, el will point to the extent list on the bottom most |
| 7334 | * portion of this tree. Only the tail record is considered in |
| 7335 | * each pass. |
| 7336 | * |
| 7337 | * We handle the following cases, in order: |
| 7338 | * - empty extent: delete the remaining branch |
| 7339 | * - remove the entire record |
| 7340 | * - remove a partial record |
| 7341 | * - no record needs to be removed (truncate has completed) |
| 7342 | */ |
| 7343 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7344 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7345 | ocfs2_error(inode->i_sb, |
| 7346 | "Inode %llu has empty extent block at %llu\n", |
| 7347 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7348 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7349 | status = -EROFS; |
| 7350 | goto bail; |
| 7351 | } |
| 7352 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7353 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7354 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7355 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7356 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 7357 | clusters_to_del = 0; |
| 7358 | } 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] | 7359 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7360 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7361 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7362 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7363 | new_highest_cpos; |
| 7364 | } else { |
| 7365 | status = 0; |
| 7366 | goto bail; |
| 7367 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7368 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7369 | mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", |
| 7370 | clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7371 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7372 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7373 | tl_sem = 1; |
| 7374 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 7375 | * record is free for use. If there isn't any, we flush to get |
| 7376 | * an empty truncate log. */ |
| 7377 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 7378 | status = __ocfs2_flush_truncate_log(osb); |
| 7379 | if (status < 0) { |
| 7380 | mlog_errno(status); |
| 7381 | goto bail; |
| 7382 | } |
| 7383 | } |
| 7384 | |
| 7385 | credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7386 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 7387 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 7388 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7389 | if (IS_ERR(handle)) { |
| 7390 | status = PTR_ERR(handle); |
| 7391 | handle = NULL; |
| 7392 | mlog_errno(status); |
| 7393 | goto bail; |
| 7394 | } |
| 7395 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7396 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
| 7397 | tc, path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7398 | if (status < 0) { |
| 7399 | mlog_errno(status); |
| 7400 | goto bail; |
| 7401 | } |
| 7402 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7403 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7404 | tl_sem = 0; |
| 7405 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7406 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7407 | handle = NULL; |
| 7408 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7409 | ocfs2_reinit_path(path, 1); |
| 7410 | |
| 7411 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7412 | * The check above will catch the case where we've truncated |
| 7413 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7414 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7415 | goto start; |
| 7416 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7417 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7418 | |
| 7419 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7420 | |
| 7421 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7422 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7423 | |
| 7424 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7425 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7426 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7427 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 7428 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7429 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7430 | |
| 7431 | /* This will drop the ext_alloc cluster lock for us */ |
| 7432 | ocfs2_free_truncate_context(tc); |
| 7433 | |
| 7434 | mlog_exit(status); |
| 7435 | return status; |
| 7436 | } |
| 7437 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7438 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7439 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7440 | */ |
| 7441 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 7442 | struct inode *inode, |
| 7443 | struct buffer_head *fe_bh, |
| 7444 | struct ocfs2_truncate_context **tc) |
| 7445 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7446 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7447 | unsigned int new_i_clusters; |
| 7448 | struct ocfs2_dinode *fe; |
| 7449 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7450 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7451 | |
| 7452 | mlog_entry_void(); |
| 7453 | |
| 7454 | *tc = NULL; |
| 7455 | |
| 7456 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 7457 | i_size_read(inode)); |
| 7458 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 7459 | |
| 7460 | 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] | 7461 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 7462 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7463 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 7464 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7465 | if (!(*tc)) { |
| 7466 | status = -ENOMEM; |
| 7467 | mlog_errno(status); |
| 7468 | goto bail; |
| 7469 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7470 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7471 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7472 | if (fe->id2.i_list.l_tree_depth) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 7473 | status = ocfs2_read_extent_block(INODE_CACHE(inode), |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 7474 | le64_to_cpu(fe->i_last_eb_blk), |
| 7475 | &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7476 | if (status < 0) { |
| 7477 | mlog_errno(status); |
| 7478 | goto bail; |
| 7479 | } |
| 7480 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7481 | } |
| 7482 | |
| 7483 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 7484 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7485 | status = 0; |
| 7486 | bail: |
| 7487 | if (status < 0) { |
| 7488 | if (*tc) |
| 7489 | ocfs2_free_truncate_context(*tc); |
| 7490 | *tc = NULL; |
| 7491 | } |
| 7492 | mlog_exit_void(); |
| 7493 | return status; |
| 7494 | } |
| 7495 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7496 | /* |
| 7497 | * 'start' is inclusive, 'end' is not. |
| 7498 | */ |
| 7499 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7500 | unsigned int start, unsigned int end, int trunc) |
| 7501 | { |
| 7502 | int ret; |
| 7503 | unsigned int numbytes; |
| 7504 | handle_t *handle; |
| 7505 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7506 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7507 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7508 | |
| 7509 | if (end > i_size_read(inode)) |
| 7510 | end = i_size_read(inode); |
| 7511 | |
| 7512 | BUG_ON(start >= end); |
| 7513 | |
| 7514 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7515 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7516 | !ocfs2_supports_inline_data(osb)) { |
| 7517 | ocfs2_error(inode->i_sb, |
| 7518 | "Inline data flags for inode %llu don't agree! " |
| 7519 | "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
| 7520 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7521 | le16_to_cpu(di->i_dyn_features), |
| 7522 | OCFS2_I(inode)->ip_dyn_features, |
| 7523 | osb->s_feature_incompat); |
| 7524 | ret = -EROFS; |
| 7525 | goto out; |
| 7526 | } |
| 7527 | |
| 7528 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7529 | if (IS_ERR(handle)) { |
| 7530 | ret = PTR_ERR(handle); |
| 7531 | mlog_errno(ret); |
| 7532 | goto out; |
| 7533 | } |
| 7534 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7535 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7536 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7537 | if (ret) { |
| 7538 | mlog_errno(ret); |
| 7539 | goto out_commit; |
| 7540 | } |
| 7541 | |
| 7542 | numbytes = end - start; |
| 7543 | memset(idata->id_data + start, 0, numbytes); |
| 7544 | |
| 7545 | /* |
| 7546 | * No need to worry about the data page here - it's been |
| 7547 | * truncated already and inline data doesn't need it for |
| 7548 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7549 | * later. |
| 7550 | */ |
| 7551 | if (trunc) { |
| 7552 | i_size_write(inode, start); |
| 7553 | di->i_size = cpu_to_le64(start); |
| 7554 | } |
| 7555 | |
| 7556 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7557 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 7558 | |
| 7559 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7560 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7561 | |
| 7562 | ocfs2_journal_dirty(handle, di_bh); |
| 7563 | |
| 7564 | out_commit: |
| 7565 | ocfs2_commit_trans(osb, handle); |
| 7566 | |
| 7567 | out: |
| 7568 | return ret; |
| 7569 | } |
| 7570 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7571 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 7572 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7573 | /* |
| 7574 | * The caller is responsible for completing deallocation |
| 7575 | * before freeing the context. |
| 7576 | */ |
| 7577 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 7578 | mlog(ML_NOTICE, |
| 7579 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7580 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 7581 | brelse(tc->tc_last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7582 | |
| 7583 | kfree(tc); |
| 7584 | } |