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 | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 55 | enum ocfs2_contig_type { |
| 56 | CONTIG_NONE = 0, |
| 57 | CONTIG_LEFT, |
| 58 | CONTIG_RIGHT, |
| 59 | CONTIG_LEFTRIGHT, |
| 60 | }; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 61 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 62 | static enum ocfs2_contig_type |
| 63 | ocfs2_extent_rec_contig(struct super_block *sb, |
| 64 | struct ocfs2_extent_rec *ext, |
| 65 | struct ocfs2_extent_rec *insert_rec); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Operations for a specific extent tree type. |
| 68 | * |
| 69 | * To implement an on-disk btree (extent tree) type in ocfs2, add |
| 70 | * an ocfs2_extent_tree_operations structure and the matching |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 71 | * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 72 | * for the allocation portion of the extent tree. |
| 73 | */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 74 | struct ocfs2_extent_tree_operations { |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 75 | /* |
| 76 | * last_eb_blk is the block number of the right most leaf extent |
| 77 | * block. Most on-disk structures containing an extent tree store |
| 78 | * this value for fast access. The ->eo_set_last_eb_blk() and |
| 79 | * ->eo_get_last_eb_blk() operations access this value. They are |
| 80 | * both required. |
| 81 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 82 | void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et, |
| 83 | u64 blkno); |
| 84 | u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * The on-disk structure usually keeps track of how many total |
| 88 | * clusters are stored in this extent tree. This function updates |
| 89 | * that value. new_clusters is the delta, and must be |
| 90 | * added to the total. Required. |
| 91 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 92 | void (*eo_update_clusters)(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 93 | u32 new_clusters); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 96 | * If this extent tree is supported by an extent map, insert |
| 97 | * a record into the map. |
| 98 | */ |
| 99 | void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et, |
| 100 | struct ocfs2_extent_rec *rec); |
| 101 | |
| 102 | /* |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 103 | * If this extent tree is supported by an extent map, truncate the |
| 104 | * map to clusters, |
| 105 | */ |
| 106 | void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et, |
| 107 | u32 clusters); |
| 108 | |
| 109 | /* |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 110 | * If ->eo_insert_check() exists, it is called before rec is |
| 111 | * inserted into the extent tree. It is optional. |
| 112 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 113 | int (*eo_insert_check)(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 114 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 115 | int (*eo_sanity_check)(struct ocfs2_extent_tree *et); |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 116 | |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 117 | /* |
| 118 | * -------------------------------------------------------------- |
| 119 | * The remaining are internal to ocfs2_extent_tree and don't have |
| 120 | * accessor functions |
| 121 | */ |
| 122 | |
| 123 | /* |
| 124 | * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. |
| 125 | * It is required. |
| 126 | */ |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 127 | void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if |
| 131 | * it exists. If it does not, et->et_max_leaf_clusters is set |
| 132 | * to 0 (unlimited). Optional. |
| 133 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 134 | void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * ->eo_extent_contig test whether the 2 ocfs2_extent_rec |
| 138 | * are contiguous or not. Optional. Don't need to set it if use |
| 139 | * ocfs2_extent_rec as the tree leaf. |
| 140 | */ |
| 141 | enum ocfs2_contig_type |
| 142 | (*eo_extent_contig)(struct ocfs2_extent_tree *et, |
| 143 | struct ocfs2_extent_rec *ext, |
| 144 | struct ocfs2_extent_rec *insert_rec); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 145 | }; |
| 146 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check |
| 150 | * in the methods. |
| 151 | */ |
| 152 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); |
| 153 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 154 | u64 blkno); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 155 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 156 | u32 clusters); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 157 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 158 | struct ocfs2_extent_rec *rec); |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 159 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 160 | u32 clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 161 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 162 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 163 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 164 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); |
| 165 | static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { |
| 166 | .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, |
| 167 | .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, |
| 168 | .eo_update_clusters = ocfs2_dinode_update_clusters, |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 169 | .eo_extent_map_insert = ocfs2_dinode_extent_map_insert, |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 170 | .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 171 | .eo_insert_check = ocfs2_dinode_insert_check, |
| 172 | .eo_sanity_check = ocfs2_dinode_sanity_check, |
| 173 | .eo_fill_root_el = ocfs2_dinode_fill_root_el, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 177 | u64 blkno) |
| 178 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 179 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 180 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 181 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 182 | di->i_last_eb_blk = cpu_to_le64(blkno); |
| 183 | } |
| 184 | |
| 185 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 186 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 187 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 188 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 189 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 190 | return le64_to_cpu(di->i_last_eb_blk); |
| 191 | } |
| 192 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 193 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 194 | u32 clusters) |
| 195 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 196 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 197 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 198 | |
| 199 | le32_add_cpu(&di->i_clusters, clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 200 | spin_lock(&oi->ip_lock); |
| 201 | oi->ip_clusters = le32_to_cpu(di->i_clusters); |
| 202 | spin_unlock(&oi->ip_lock); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 205 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 206 | struct ocfs2_extent_rec *rec) |
| 207 | { |
| 208 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 209 | |
| 210 | ocfs2_extent_map_insert_rec(inode, rec); |
| 211 | } |
| 212 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 213 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 214 | u32 clusters) |
| 215 | { |
| 216 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 217 | |
| 218 | ocfs2_extent_map_trunc(inode, clusters); |
| 219 | } |
| 220 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 221 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 222 | struct ocfs2_extent_rec *rec) |
| 223 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 224 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
| 225 | struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 226 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 227 | BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 228 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 229 | (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 230 | "Device %s, asking for sparse allocation: inode %llu, " |
| 231 | "cpos %u, clusters %u\n", |
| 232 | osb->dev_str, |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 233 | (unsigned long long)oi->ip_blkno, |
| 234 | rec->e_cpos, oi->ip_clusters); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 239 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 240 | { |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 241 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 242 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 243 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 244 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 245 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 246 | return 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 249 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) |
| 250 | { |
| 251 | struct ocfs2_dinode *di = et->et_object; |
| 252 | |
| 253 | et->et_root_el = &di->id2.i_list; |
| 254 | } |
| 255 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 256 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 257 | static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) |
| 258 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 259 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 260 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 261 | et->et_root_el = &vb->vb_xv->xr_list; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 264 | static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 265 | u64 blkno) |
| 266 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 267 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 268 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 269 | vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 273 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 274 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 275 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 276 | return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 279 | static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 280 | u32 clusters) |
| 281 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 282 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 283 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 284 | le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 287 | static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 288 | .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, |
| 289 | .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, |
| 290 | .eo_update_clusters = ocfs2_xattr_value_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 291 | .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 292 | }; |
| 293 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 294 | static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 295 | { |
| 296 | struct ocfs2_xattr_block *xb = et->et_object; |
| 297 | |
| 298 | et->et_root_el = &xb->xb_attrs.xb_root.xt_list; |
| 299 | } |
| 300 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 301 | 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] | 302 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 303 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 304 | et->et_max_leaf_clusters = |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 305 | ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 308 | static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 309 | u64 blkno) |
| 310 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 311 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 312 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 313 | |
| 314 | xt->xt_last_eb_blk = cpu_to_le64(blkno); |
| 315 | } |
| 316 | |
| 317 | static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 318 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 319 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 320 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 321 | |
| 322 | return le64_to_cpu(xt->xt_last_eb_blk); |
| 323 | } |
| 324 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 325 | static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 326 | u32 clusters) |
| 327 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 328 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 329 | |
| 330 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); |
| 331 | } |
| 332 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 333 | static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 334 | .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, |
| 335 | .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, |
| 336 | .eo_update_clusters = ocfs2_xattr_tree_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 337 | .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 338 | .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 339 | }; |
| 340 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 341 | static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 342 | u64 blkno) |
| 343 | { |
| 344 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 345 | |
| 346 | dx_root->dr_last_eb_blk = cpu_to_le64(blkno); |
| 347 | } |
| 348 | |
| 349 | static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 350 | { |
| 351 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 352 | |
| 353 | return le64_to_cpu(dx_root->dr_last_eb_blk); |
| 354 | } |
| 355 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 356 | static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 357 | u32 clusters) |
| 358 | { |
| 359 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 360 | |
| 361 | le32_add_cpu(&dx_root->dr_clusters, clusters); |
| 362 | } |
| 363 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 364 | static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 365 | { |
| 366 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 367 | |
| 368 | BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) |
| 374 | { |
| 375 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 376 | |
| 377 | et->et_root_el = &dx_root->dr_list; |
| 378 | } |
| 379 | |
| 380 | static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { |
| 381 | .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, |
| 382 | .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, |
| 383 | .eo_update_clusters = ocfs2_dx_root_update_clusters, |
| 384 | .eo_sanity_check = ocfs2_dx_root_sanity_check, |
| 385 | .eo_fill_root_el = ocfs2_dx_root_fill_root_el, |
| 386 | }; |
| 387 | |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame^] | 388 | static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 389 | { |
| 390 | struct ocfs2_refcount_block *rb = et->et_object; |
| 391 | |
| 392 | et->et_root_el = &rb->rf_list; |
| 393 | } |
| 394 | |
| 395 | static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 396 | u64 blkno) |
| 397 | { |
| 398 | struct ocfs2_refcount_block *rb = et->et_object; |
| 399 | |
| 400 | rb->rf_last_eb_blk = cpu_to_le64(blkno); |
| 401 | } |
| 402 | |
| 403 | static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 404 | { |
| 405 | struct ocfs2_refcount_block *rb = et->et_object; |
| 406 | |
| 407 | return le64_to_cpu(rb->rf_last_eb_blk); |
| 408 | } |
| 409 | |
| 410 | static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et, |
| 411 | u32 clusters) |
| 412 | { |
| 413 | struct ocfs2_refcount_block *rb = et->et_object; |
| 414 | |
| 415 | le32_add_cpu(&rb->rf_clusters, clusters); |
| 416 | } |
| 417 | |
| 418 | static enum ocfs2_contig_type |
| 419 | ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et, |
| 420 | struct ocfs2_extent_rec *ext, |
| 421 | struct ocfs2_extent_rec *insert_rec) |
| 422 | { |
| 423 | return CONTIG_NONE; |
| 424 | } |
| 425 | |
| 426 | static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = { |
| 427 | .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk, |
| 428 | .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk, |
| 429 | .eo_update_clusters = ocfs2_refcount_tree_update_clusters, |
| 430 | .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el, |
| 431 | .eo_extent_contig = ocfs2_refcount_tree_extent_contig, |
| 432 | }; |
| 433 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 434 | static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 435 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 436 | struct buffer_head *bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 437 | ocfs2_journal_access_func access, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 438 | void *obj, |
| 439 | struct ocfs2_extent_tree_operations *ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 440 | { |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 441 | et->et_ops = ops; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 442 | et->et_root_bh = bh; |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 443 | et->et_ci = ci; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 444 | et->et_root_journal_access = access; |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 445 | if (!obj) |
| 446 | obj = (void *)bh->b_data; |
| 447 | et->et_object = obj; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 448 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 449 | et->et_ops->eo_fill_root_el(et); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 450 | if (!et->et_ops->eo_fill_max_leaf_clusters) |
| 451 | et->et_max_leaf_clusters = 0; |
| 452 | else |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 453 | et->et_ops->eo_fill_max_leaf_clusters(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 454 | } |
| 455 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 456 | void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 457 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 458 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 459 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 460 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 461 | NULL, &ocfs2_dinode_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 464 | void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 465 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 466 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 467 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 468 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 469 | NULL, &ocfs2_xattr_tree_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 472 | void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 473 | struct ocfs2_caching_info *ci, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 474 | struct ocfs2_xattr_value_buf *vb) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 475 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 476 | __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 477 | &ocfs2_xattr_value_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 478 | } |
| 479 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 480 | void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 481 | struct ocfs2_caching_info *ci, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 482 | struct buffer_head *bh) |
| 483 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 484 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 485 | NULL, &ocfs2_dx_root_et_ops); |
| 486 | } |
| 487 | |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame^] | 488 | void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et, |
| 489 | struct ocfs2_caching_info *ci, |
| 490 | struct buffer_head *bh) |
| 491 | { |
| 492 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb, |
| 493 | NULL, &ocfs2_refcount_tree_et_ops); |
| 494 | } |
| 495 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 496 | static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 497 | u64 new_last_eb_blk) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 498 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 499 | 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] | 500 | } |
| 501 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 502 | 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] | 503 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 504 | return et->et_ops->eo_get_last_eb_blk(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 505 | } |
| 506 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 507 | static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 508 | u32 clusters) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 509 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 510 | et->et_ops->eo_update_clusters(et, clusters); |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 513 | static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et, |
| 514 | struct ocfs2_extent_rec *rec) |
| 515 | { |
| 516 | if (et->et_ops->eo_extent_map_insert) |
| 517 | et->et_ops->eo_extent_map_insert(et, rec); |
| 518 | } |
| 519 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 520 | static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 521 | u32 clusters) |
| 522 | { |
| 523 | if (et->et_ops->eo_extent_map_truncate) |
| 524 | et->et_ops->eo_extent_map_truncate(et, clusters); |
| 525 | } |
| 526 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 527 | static inline int ocfs2_et_root_journal_access(handle_t *handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 528 | struct ocfs2_extent_tree *et, |
| 529 | int type) |
| 530 | { |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 531 | 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] | 532 | type); |
| 533 | } |
| 534 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 535 | static inline enum ocfs2_contig_type |
| 536 | ocfs2_et_extent_contig(struct ocfs2_extent_tree *et, |
| 537 | struct ocfs2_extent_rec *rec, |
| 538 | struct ocfs2_extent_rec *insert_rec) |
| 539 | { |
| 540 | if (et->et_ops->eo_extent_contig) |
| 541 | return et->et_ops->eo_extent_contig(et, rec, insert_rec); |
| 542 | |
| 543 | return ocfs2_extent_rec_contig( |
| 544 | ocfs2_metadata_cache_get_super(et->et_ci), |
| 545 | rec, insert_rec); |
| 546 | } |
| 547 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 548 | static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 549 | struct ocfs2_extent_rec *rec) |
| 550 | { |
| 551 | int ret = 0; |
| 552 | |
| 553 | if (et->et_ops->eo_insert_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 554 | ret = et->et_ops->eo_insert_check(et, rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 555 | return ret; |
| 556 | } |
| 557 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 558 | static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 559 | { |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 560 | int ret = 0; |
| 561 | |
| 562 | if (et->et_ops->eo_sanity_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 563 | ret = et->et_ops->eo_sanity_check(et); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 564 | return ret; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 565 | } |
| 566 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 567 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 568 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 569 | struct ocfs2_extent_block *eb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 570 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 571 | /* |
| 572 | * Structures which describe a path through a btree, and functions to |
| 573 | * manipulate them. |
| 574 | * |
| 575 | * The idea here is to be as generic as possible with the tree |
| 576 | * manipulation code. |
| 577 | */ |
| 578 | struct ocfs2_path_item { |
| 579 | struct buffer_head *bh; |
| 580 | struct ocfs2_extent_list *el; |
| 581 | }; |
| 582 | |
| 583 | #define OCFS2_MAX_PATH_DEPTH 5 |
| 584 | |
| 585 | struct ocfs2_path { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 586 | int p_tree_depth; |
| 587 | ocfs2_journal_access_func p_root_access; |
| 588 | struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 589 | }; |
| 590 | |
| 591 | #define path_root_bh(_path) ((_path)->p_node[0].bh) |
| 592 | #define path_root_el(_path) ((_path)->p_node[0].el) |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 593 | #define path_root_access(_path)((_path)->p_root_access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 594 | #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) |
| 595 | #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) |
| 596 | #define path_num_items(_path) ((_path)->p_tree_depth + 1) |
| 597 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 598 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 599 | struct ocfs2_path *path, u32 cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 600 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 601 | struct ocfs2_extent_tree *et, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 602 | struct ocfs2_path *path, |
| 603 | struct ocfs2_extent_rec *insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 604 | /* |
| 605 | * Reset the actual path elements so that we can re-use the structure |
| 606 | * to build another path. Generally, this involves freeing the buffer |
| 607 | * heads. |
| 608 | */ |
| 609 | static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
| 610 | { |
| 611 | int i, start = 0, depth = 0; |
| 612 | struct ocfs2_path_item *node; |
| 613 | |
| 614 | if (keep_root) |
| 615 | start = 1; |
| 616 | |
| 617 | for(i = start; i < path_num_items(path); i++) { |
| 618 | node = &path->p_node[i]; |
| 619 | |
| 620 | brelse(node->bh); |
| 621 | node->bh = NULL; |
| 622 | node->el = NULL; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Tree depth may change during truncate, or insert. If we're |
| 627 | * keeping the root extent list, then make sure that our path |
| 628 | * structure reflects the proper depth. |
| 629 | */ |
| 630 | if (keep_root) |
| 631 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 632 | else |
| 633 | path_root_access(path) = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 634 | |
| 635 | path->p_tree_depth = depth; |
| 636 | } |
| 637 | |
| 638 | static void ocfs2_free_path(struct ocfs2_path *path) |
| 639 | { |
| 640 | if (path) { |
| 641 | ocfs2_reinit_path(path, 0); |
| 642 | kfree(path); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /* |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 647 | * All the elements of src into dest. After this call, src could be freed |
| 648 | * without affecting dest. |
| 649 | * |
| 650 | * Both paths should have the same root. Any non-root elements of dest |
| 651 | * will be freed. |
| 652 | */ |
| 653 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 654 | { |
| 655 | int i; |
| 656 | |
| 657 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 658 | BUG_ON(path_root_el(dest) != path_root_el(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 659 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 660 | |
| 661 | ocfs2_reinit_path(dest, 1); |
| 662 | |
| 663 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 664 | dest->p_node[i].bh = src->p_node[i].bh; |
| 665 | dest->p_node[i].el = src->p_node[i].el; |
| 666 | |
| 667 | if (dest->p_node[i].bh) |
| 668 | get_bh(dest->p_node[i].bh); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 673 | * Make the *dest path the same as src and re-initialize src path to |
| 674 | * have a root only. |
| 675 | */ |
| 676 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 677 | { |
| 678 | int i; |
| 679 | |
| 680 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 681 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 682 | |
| 683 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 684 | brelse(dest->p_node[i].bh); |
| 685 | |
| 686 | dest->p_node[i].bh = src->p_node[i].bh; |
| 687 | dest->p_node[i].el = src->p_node[i].el; |
| 688 | |
| 689 | src->p_node[i].bh = NULL; |
| 690 | src->p_node[i].el = NULL; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Insert an extent block at given index. |
| 696 | * |
| 697 | * This will not take an additional reference on eb_bh. |
| 698 | */ |
| 699 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, |
| 700 | struct buffer_head *eb_bh) |
| 701 | { |
| 702 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; |
| 703 | |
| 704 | /* |
| 705 | * Right now, no root bh is an extent block, so this helps |
| 706 | * catch code errors with dinode trees. The assertion can be |
| 707 | * safely removed if we ever need to insert extent block |
| 708 | * structures at the root. |
| 709 | */ |
| 710 | BUG_ON(index == 0); |
| 711 | |
| 712 | path->p_node[index].bh = eb_bh; |
| 713 | path->p_node[index].el = &eb->h_list; |
| 714 | } |
| 715 | |
| 716 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 717 | struct ocfs2_extent_list *root_el, |
| 718 | ocfs2_journal_access_func access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 719 | { |
| 720 | struct ocfs2_path *path; |
| 721 | |
| 722 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
| 723 | |
| 724 | path = kzalloc(sizeof(*path), GFP_NOFS); |
| 725 | if (path) { |
| 726 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); |
| 727 | get_bh(root_bh); |
| 728 | path_root_bh(path) = root_bh; |
| 729 | path_root_el(path) = root_el; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 730 | path_root_access(path) = access; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return path; |
| 734 | } |
| 735 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 736 | static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) |
| 737 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 738 | return ocfs2_new_path(path_root_bh(path), path_root_el(path), |
| 739 | path_root_access(path)); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et) |
| 743 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 744 | return ocfs2_new_path(et->et_root_bh, et->et_root_el, |
| 745 | et->et_root_journal_access); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Journal the buffer at depth idx. All idx>0 are extent_blocks, |
| 750 | * otherwise it's the root_access function. |
| 751 | * |
| 752 | * I don't like the way this function's name looks next to |
| 753 | * ocfs2_journal_access_path(), but I don't have a better one. |
| 754 | */ |
| 755 | static int ocfs2_path_bh_journal_access(handle_t *handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 756 | struct ocfs2_caching_info *ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 757 | struct ocfs2_path *path, |
| 758 | int idx) |
| 759 | { |
| 760 | ocfs2_journal_access_func access = path_root_access(path); |
| 761 | |
| 762 | if (!access) |
| 763 | access = ocfs2_journal_access; |
| 764 | |
| 765 | if (idx) |
| 766 | access = ocfs2_journal_access_eb; |
| 767 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 768 | return access(handle, ci, path->p_node[idx].bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 769 | OCFS2_JOURNAL_ACCESS_WRITE); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 772 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 773 | * Convenience function to journal all components in a path. |
| 774 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 775 | static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, |
| 776 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 777 | struct ocfs2_path *path) |
| 778 | { |
| 779 | int i, ret = 0; |
| 780 | |
| 781 | if (!path) |
| 782 | goto out; |
| 783 | |
| 784 | for(i = 0; i < path_num_items(path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 785 | ret = ocfs2_path_bh_journal_access(handle, ci, path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 786 | if (ret < 0) { |
| 787 | mlog_errno(ret); |
| 788 | goto out; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | out: |
| 793 | return ret; |
| 794 | } |
| 795 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 796 | /* |
| 797 | * Return the index of the extent record which contains cluster #v_cluster. |
| 798 | * -1 is returned if it was not found. |
| 799 | * |
| 800 | * Should work fine on interior and exterior nodes. |
| 801 | */ |
| 802 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) |
| 803 | { |
| 804 | int ret = -1; |
| 805 | int i; |
| 806 | struct ocfs2_extent_rec *rec; |
| 807 | u32 rec_end, rec_start, clusters; |
| 808 | |
| 809 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 810 | rec = &el->l_recs[i]; |
| 811 | |
| 812 | rec_start = le32_to_cpu(rec->e_cpos); |
| 813 | clusters = ocfs2_rec_clusters(el, rec); |
| 814 | |
| 815 | rec_end = rec_start + clusters; |
| 816 | |
| 817 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 818 | ret = i; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | return ret; |
| 824 | } |
| 825 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 826 | /* |
| 827 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 828 | * ocfs2_extent_rec_contig only work properly against leaf nodes! |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 829 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 830 | static int ocfs2_block_extent_contig(struct super_block *sb, |
| 831 | struct ocfs2_extent_rec *ext, |
| 832 | u64 blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 833 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 834 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
| 835 | |
| 836 | blk_end += ocfs2_clusters_to_blocks(sb, |
| 837 | le16_to_cpu(ext->e_leaf_clusters)); |
| 838 | |
| 839 | return blkno == blk_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 842 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
| 843 | struct ocfs2_extent_rec *right) |
| 844 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 845 | u32 left_range; |
| 846 | |
| 847 | left_range = le32_to_cpu(left->e_cpos) + |
| 848 | le16_to_cpu(left->e_leaf_clusters); |
| 849 | |
| 850 | return (left_range == le32_to_cpu(right->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | static enum ocfs2_contig_type |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 854 | ocfs2_extent_rec_contig(struct super_block *sb, |
| 855 | struct ocfs2_extent_rec *ext, |
| 856 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 857 | { |
| 858 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); |
| 859 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 860 | /* |
| 861 | * Refuse to coalesce extent records with different flag |
| 862 | * fields - we don't want to mix unwritten extents with user |
| 863 | * data. |
| 864 | */ |
| 865 | if (ext->e_flags != insert_rec->e_flags) |
| 866 | return CONTIG_NONE; |
| 867 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 868 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 869 | ocfs2_block_extent_contig(sb, ext, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 870 | return CONTIG_RIGHT; |
| 871 | |
| 872 | blkno = le64_to_cpu(ext->e_blkno); |
| 873 | if (ocfs2_extents_adjacent(insert_rec, ext) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 874 | ocfs2_block_extent_contig(sb, insert_rec, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 875 | return CONTIG_LEFT; |
| 876 | |
| 877 | return CONTIG_NONE; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * NOTE: We can have pretty much any combination of contiguousness and |
| 882 | * appending. |
| 883 | * |
| 884 | * The usefulness of APPEND_TAIL is more in that it lets us know that |
| 885 | * we'll have to update the path to that leaf. |
| 886 | */ |
| 887 | enum ocfs2_append_type { |
| 888 | APPEND_NONE = 0, |
| 889 | APPEND_TAIL, |
| 890 | }; |
| 891 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 892 | enum ocfs2_split_type { |
| 893 | SPLIT_NONE = 0, |
| 894 | SPLIT_LEFT, |
| 895 | SPLIT_RIGHT, |
| 896 | }; |
| 897 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 898 | struct ocfs2_insert_type { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 899 | enum ocfs2_split_type ins_split; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 900 | enum ocfs2_append_type ins_appending; |
| 901 | enum ocfs2_contig_type ins_contig; |
| 902 | int ins_contig_index; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 903 | int ins_tree_depth; |
| 904 | }; |
| 905 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 906 | struct ocfs2_merge_ctxt { |
| 907 | enum ocfs2_contig_type c_contig_type; |
| 908 | int c_has_empty_extent; |
| 909 | int c_split_covers_rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 910 | }; |
| 911 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 912 | static int ocfs2_validate_extent_block(struct super_block *sb, |
| 913 | struct buffer_head *bh) |
| 914 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 915 | int rc; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 916 | struct ocfs2_extent_block *eb = |
| 917 | (struct ocfs2_extent_block *)bh->b_data; |
| 918 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 919 | mlog(0, "Validating extent block %llu\n", |
| 920 | (unsigned long long)bh->b_blocknr); |
| 921 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 922 | BUG_ON(!buffer_uptodate(bh)); |
| 923 | |
| 924 | /* |
| 925 | * If the ecc fails, we return the error but otherwise |
| 926 | * leave the filesystem running. We know any error is |
| 927 | * local to this block. |
| 928 | */ |
| 929 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 930 | if (rc) { |
| 931 | mlog(ML_ERROR, "Checksum failed for extent block %llu\n", |
| 932 | (unsigned long long)bh->b_blocknr); |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 933 | return rc; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 934 | } |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 935 | |
| 936 | /* |
| 937 | * Errors after here are fatal. |
| 938 | */ |
| 939 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 940 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 941 | ocfs2_error(sb, |
| 942 | "Extent block #%llu has bad signature %.*s", |
| 943 | (unsigned long long)bh->b_blocknr, 7, |
| 944 | eb->h_signature); |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | |
| 948 | if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { |
| 949 | ocfs2_error(sb, |
| 950 | "Extent block #%llu has an invalid h_blkno " |
| 951 | "of %llu", |
| 952 | (unsigned long long)bh->b_blocknr, |
| 953 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
| 957 | if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 958 | ocfs2_error(sb, |
| 959 | "Extent block #%llu has an invalid " |
| 960 | "h_fs_generation of #%u", |
| 961 | (unsigned long long)bh->b_blocknr, |
| 962 | le32_to_cpu(eb->h_fs_generation)); |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 969 | 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] | 970 | struct buffer_head **bh) |
| 971 | { |
| 972 | int rc; |
| 973 | struct buffer_head *tmp = *bh; |
| 974 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 975 | rc = ocfs2_read_block(ci, eb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 976 | ocfs2_validate_extent_block); |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 977 | |
| 978 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 979 | if (!rc && !*bh) |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 980 | *bh = tmp; |
| 981 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 982 | return rc; |
| 983 | } |
| 984 | |
| 985 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 986 | /* |
| 987 | * How many free extents have we got before we need more meta data? |
| 988 | */ |
| 989 | int ocfs2_num_free_extents(struct ocfs2_super *osb, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 990 | struct ocfs2_extent_tree *et) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 991 | { |
| 992 | int retval; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 993 | struct ocfs2_extent_list *el = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 994 | struct ocfs2_extent_block *eb; |
| 995 | struct buffer_head *eb_bh = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 996 | u64 last_eb_blk = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 997 | |
| 998 | mlog_entry_void(); |
| 999 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1000 | el = et->et_root_el; |
| 1001 | last_eb_blk = ocfs2_et_get_last_eb_blk(et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1002 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1003 | if (last_eb_blk) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1004 | retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, |
| 1005 | &eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1006 | if (retval < 0) { |
| 1007 | mlog_errno(retval); |
| 1008 | goto bail; |
| 1009 | } |
| 1010 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1011 | el = &eb->h_list; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1012 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1013 | |
| 1014 | BUG_ON(el->l_tree_depth != 0); |
| 1015 | |
| 1016 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); |
| 1017 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1018 | brelse(eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1019 | |
| 1020 | mlog_exit(retval); |
| 1021 | return retval; |
| 1022 | } |
| 1023 | |
| 1024 | /* expects array to already be allocated |
| 1025 | * |
| 1026 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and |
| 1027 | * l_count for you |
| 1028 | */ |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1029 | static int ocfs2_create_new_meta_bhs(handle_t *handle, |
| 1030 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1031 | int wanted, |
| 1032 | struct ocfs2_alloc_context *meta_ac, |
| 1033 | struct buffer_head *bhs[]) |
| 1034 | { |
| 1035 | int count, status, i; |
| 1036 | u16 suballoc_bit_start; |
| 1037 | u32 num_got; |
| 1038 | u64 first_blkno; |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1039 | struct ocfs2_super *osb = |
| 1040 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1041 | struct ocfs2_extent_block *eb; |
| 1042 | |
| 1043 | mlog_entry_void(); |
| 1044 | |
| 1045 | count = 0; |
| 1046 | while (count < wanted) { |
| 1047 | status = ocfs2_claim_metadata(osb, |
| 1048 | handle, |
| 1049 | meta_ac, |
| 1050 | wanted - count, |
| 1051 | &suballoc_bit_start, |
| 1052 | &num_got, |
| 1053 | &first_blkno); |
| 1054 | if (status < 0) { |
| 1055 | mlog_errno(status); |
| 1056 | goto bail; |
| 1057 | } |
| 1058 | |
| 1059 | for(i = count; i < (num_got + count); i++) { |
| 1060 | bhs[i] = sb_getblk(osb->sb, first_blkno); |
| 1061 | if (bhs[i] == NULL) { |
| 1062 | status = -EIO; |
| 1063 | mlog_errno(status); |
| 1064 | goto bail; |
| 1065 | } |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1066 | ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1067 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1068 | status = ocfs2_journal_access_eb(handle, et->et_ci, |
| 1069 | bhs[i], |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1070 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1071 | if (status < 0) { |
| 1072 | mlog_errno(status); |
| 1073 | goto bail; |
| 1074 | } |
| 1075 | |
| 1076 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); |
| 1077 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; |
| 1078 | /* Ok, setup the minimal stuff here. */ |
| 1079 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); |
| 1080 | eb->h_blkno = cpu_to_le64(first_blkno); |
| 1081 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1082 | eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1083 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 1084 | eb->h_list.l_count = |
| 1085 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 1086 | |
| 1087 | suballoc_bit_start++; |
| 1088 | first_blkno++; |
| 1089 | |
| 1090 | /* We'll also be dirtied by the caller, so |
| 1091 | * this isn't absolutely necessary. */ |
| 1092 | status = ocfs2_journal_dirty(handle, bhs[i]); |
| 1093 | if (status < 0) { |
| 1094 | mlog_errno(status); |
| 1095 | goto bail; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | count += num_got; |
| 1100 | } |
| 1101 | |
| 1102 | status = 0; |
| 1103 | bail: |
| 1104 | if (status < 0) { |
| 1105 | for(i = 0; i < wanted; i++) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1106 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1107 | bhs[i] = NULL; |
| 1108 | } |
| 1109 | } |
| 1110 | mlog_exit(status); |
| 1111 | return status; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1115 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 1116 | * |
| 1117 | * Returns the sum of the rightmost extent rec logical offset and |
| 1118 | * cluster count. |
| 1119 | * |
| 1120 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 1121 | * value should be populated into the leftmost new branch records. |
| 1122 | * |
| 1123 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 1124 | * value for the new topmost tree record. |
| 1125 | */ |
| 1126 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 1127 | { |
| 1128 | int i; |
| 1129 | |
| 1130 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1131 | |
| 1132 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1133 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1137 | * Change range of the branches in the right most path according to the leaf |
| 1138 | * extent block's rightmost record. |
| 1139 | */ |
| 1140 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1141 | struct ocfs2_extent_tree *et) |
| 1142 | { |
| 1143 | int status; |
| 1144 | struct ocfs2_path *path = NULL; |
| 1145 | struct ocfs2_extent_list *el; |
| 1146 | struct ocfs2_extent_rec *rec; |
| 1147 | |
| 1148 | path = ocfs2_new_path_from_et(et); |
| 1149 | if (!path) { |
| 1150 | status = -ENOMEM; |
| 1151 | return status; |
| 1152 | } |
| 1153 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1154 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1155 | if (status < 0) { |
| 1156 | mlog_errno(status); |
| 1157 | goto out; |
| 1158 | } |
| 1159 | |
| 1160 | status = ocfs2_extend_trans(handle, path_num_items(path) + |
| 1161 | handle->h_buffer_credits); |
| 1162 | if (status < 0) { |
| 1163 | mlog_errno(status); |
| 1164 | goto out; |
| 1165 | } |
| 1166 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1167 | status = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1168 | if (status < 0) { |
| 1169 | mlog_errno(status); |
| 1170 | goto out; |
| 1171 | } |
| 1172 | |
| 1173 | el = path_leaf_el(path); |
| 1174 | rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; |
| 1175 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1176 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1177 | |
| 1178 | out: |
| 1179 | ocfs2_free_path(path); |
| 1180 | return status; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1184 | * 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] | 1185 | * 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] | 1186 | * structure. |
| 1187 | * |
| 1188 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1189 | * for the new last extent block. |
| 1190 | * |
| 1191 | * 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] | 1192 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1193 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1194 | static int ocfs2_add_branch(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1195 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1196 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1197 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1198 | struct ocfs2_alloc_context *meta_ac) |
| 1199 | { |
| 1200 | int status, new_blocks, i; |
| 1201 | u64 next_blkno, new_last_eb_blk; |
| 1202 | struct buffer_head *bh; |
| 1203 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1204 | struct ocfs2_extent_block *eb; |
| 1205 | struct ocfs2_extent_list *eb_el; |
| 1206 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1207 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1208 | |
| 1209 | mlog_entry_void(); |
| 1210 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1211 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1212 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1213 | if (eb_bh) { |
| 1214 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1215 | el = &eb->h_list; |
| 1216 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1217 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1218 | |
| 1219 | /* we never add a branch to a leaf. */ |
| 1220 | BUG_ON(!el->l_tree_depth); |
| 1221 | |
| 1222 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1223 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1224 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1225 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1226 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1227 | |
| 1228 | /* |
| 1229 | * If there is a gap before the root end and the real end |
| 1230 | * of the righmost leaf block, we need to remove the gap |
| 1231 | * between new_cpos and root_end first so that the tree |
| 1232 | * is consistent after we add a new branch(it will start |
| 1233 | * from new_cpos). |
| 1234 | */ |
| 1235 | if (root_end > new_cpos) { |
| 1236 | mlog(0, "adjust the cluster end from %u to %u\n", |
| 1237 | root_end, new_cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1238 | status = ocfs2_adjust_rightmost_branch(handle, et); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1239 | if (status) { |
| 1240 | mlog_errno(status); |
| 1241 | goto bail; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1245 | /* allocate the number of new eb blocks we need */ |
| 1246 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1247 | GFP_KERNEL); |
| 1248 | if (!new_eb_bhs) { |
| 1249 | status = -ENOMEM; |
| 1250 | mlog_errno(status); |
| 1251 | goto bail; |
| 1252 | } |
| 1253 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1254 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1255 | meta_ac, new_eb_bhs); |
| 1256 | if (status < 0) { |
| 1257 | mlog_errno(status); |
| 1258 | goto bail; |
| 1259 | } |
| 1260 | |
| 1261 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1262 | * linked with the rest of the tree. |
| 1263 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1264 | * |
| 1265 | * when we leave the loop, new_last_eb_blk will point to the |
| 1266 | * newest leaf, and next_blkno will point to the topmost extent |
| 1267 | * block. */ |
| 1268 | next_blkno = new_last_eb_blk = 0; |
| 1269 | for(i = 0; i < new_blocks; i++) { |
| 1270 | bh = new_eb_bhs[i]; |
| 1271 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1272 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1273 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1274 | eb_el = &eb->h_list; |
| 1275 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1276 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1277 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1278 | if (status < 0) { |
| 1279 | mlog_errno(status); |
| 1280 | goto bail; |
| 1281 | } |
| 1282 | |
| 1283 | eb->h_next_leaf_blk = 0; |
| 1284 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1285 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1286 | /* |
| 1287 | * This actually counts as an empty extent as |
| 1288 | * c_clusters == 0 |
| 1289 | */ |
| 1290 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1291 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1292 | /* |
| 1293 | * eb_el isn't always an interior node, but even leaf |
| 1294 | * nodes want a zero'd flags and reserved field so |
| 1295 | * this gets the whole 32 bits regardless of use. |
| 1296 | */ |
| 1297 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1298 | if (!eb_el->l_tree_depth) |
| 1299 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1300 | |
| 1301 | status = ocfs2_journal_dirty(handle, bh); |
| 1302 | if (status < 0) { |
| 1303 | mlog_errno(status); |
| 1304 | goto bail; |
| 1305 | } |
| 1306 | |
| 1307 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1308 | } |
| 1309 | |
| 1310 | /* This is a bit hairy. We want to update up to three blocks |
| 1311 | * here without leaving any of them in an inconsistent state |
| 1312 | * in case of error. We don't have to worry about |
| 1313 | * journal_dirty erroring as it won't unless we've aborted the |
| 1314 | * handle (in which case we would never be here) so reserving |
| 1315 | * the write with journal_access is all we need to do. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1316 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1317 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1318 | if (status < 0) { |
| 1319 | mlog_errno(status); |
| 1320 | goto bail; |
| 1321 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1322 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1323 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1324 | if (status < 0) { |
| 1325 | mlog_errno(status); |
| 1326 | goto bail; |
| 1327 | } |
| 1328 | if (eb_bh) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1329 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1330 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1331 | if (status < 0) { |
| 1332 | mlog_errno(status); |
| 1333 | goto bail; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1338 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1339 | i = le16_to_cpu(el->l_next_free_rec); |
| 1340 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1341 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1342 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1343 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1344 | |
| 1345 | /* fe needs a new last extent block pointer, as does the |
| 1346 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1347 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1348 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1349 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1350 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1351 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1352 | status = ocfs2_journal_dirty(handle, *last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1353 | if (status < 0) |
| 1354 | mlog_errno(status); |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1355 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1356 | if (status < 0) |
| 1357 | mlog_errno(status); |
| 1358 | if (eb_bh) { |
| 1359 | status = ocfs2_journal_dirty(handle, eb_bh); |
| 1360 | if (status < 0) |
| 1361 | mlog_errno(status); |
| 1362 | } |
| 1363 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1364 | /* |
| 1365 | * Some callers want to track the rightmost leaf so pass it |
| 1366 | * back here. |
| 1367 | */ |
| 1368 | brelse(*last_eb_bh); |
| 1369 | get_bh(new_eb_bhs[0]); |
| 1370 | *last_eb_bh = new_eb_bhs[0]; |
| 1371 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1372 | status = 0; |
| 1373 | bail: |
| 1374 | if (new_eb_bhs) { |
| 1375 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1376 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1377 | kfree(new_eb_bhs); |
| 1378 | } |
| 1379 | |
| 1380 | mlog_exit(status); |
| 1381 | return status; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * adds another level to the allocation tree. |
| 1386 | * returns back the new extent block so you can add a branch to it |
| 1387 | * after this call. |
| 1388 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1389 | static int ocfs2_shift_tree_depth(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1390 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1391 | struct ocfs2_alloc_context *meta_ac, |
| 1392 | struct buffer_head **ret_new_eb_bh) |
| 1393 | { |
| 1394 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1395 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1396 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1397 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1398 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1399 | struct ocfs2_extent_list *eb_el; |
| 1400 | |
| 1401 | mlog_entry_void(); |
| 1402 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1403 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1404 | &new_eb_bh); |
| 1405 | if (status < 0) { |
| 1406 | mlog_errno(status); |
| 1407 | goto bail; |
| 1408 | } |
| 1409 | |
| 1410 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1411 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1412 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1413 | |
| 1414 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1415 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1416 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1417 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1418 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1419 | if (status < 0) { |
| 1420 | mlog_errno(status); |
| 1421 | goto bail; |
| 1422 | } |
| 1423 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1424 | /* copy the root extent list data into the new extent block */ |
| 1425 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1426 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1427 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1428 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1429 | |
| 1430 | status = ocfs2_journal_dirty(handle, new_eb_bh); |
| 1431 | if (status < 0) { |
| 1432 | mlog_errno(status); |
| 1433 | goto bail; |
| 1434 | } |
| 1435 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1436 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1437 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1438 | if (status < 0) { |
| 1439 | mlog_errno(status); |
| 1440 | goto bail; |
| 1441 | } |
| 1442 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1443 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1444 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1445 | /* update root_bh now */ |
| 1446 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1447 | root_el->l_recs[0].e_cpos = 0; |
| 1448 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1449 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1450 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1451 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1452 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1453 | |
| 1454 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1455 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1456 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1457 | 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] | 1458 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1459 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1460 | if (status < 0) { |
| 1461 | mlog_errno(status); |
| 1462 | goto bail; |
| 1463 | } |
| 1464 | |
| 1465 | *ret_new_eb_bh = new_eb_bh; |
| 1466 | new_eb_bh = NULL; |
| 1467 | status = 0; |
| 1468 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1469 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1470 | |
| 1471 | mlog_exit(status); |
| 1472 | return status; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1476 | * Should only be called when there is no space left in any of the |
| 1477 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1478 | * non-leaf extent block with room for new records. There are three |
| 1479 | * valid results of this search: |
| 1480 | * |
| 1481 | * 1) a lowest extent block is found, then we pass it back in |
| 1482 | * *lowest_eb_bh and return '0' |
| 1483 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1484 | * 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] | 1485 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1486 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1487 | * 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] | 1488 | * which case we return > 0 |
| 1489 | * |
| 1490 | * return status < 0 indicates an error. |
| 1491 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1492 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1493 | struct buffer_head **target_bh) |
| 1494 | { |
| 1495 | int status = 0, i; |
| 1496 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1497 | struct ocfs2_extent_block *eb; |
| 1498 | struct ocfs2_extent_list *el; |
| 1499 | struct buffer_head *bh = NULL; |
| 1500 | struct buffer_head *lowest_bh = NULL; |
| 1501 | |
| 1502 | mlog_entry_void(); |
| 1503 | |
| 1504 | *target_bh = NULL; |
| 1505 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1506 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1507 | |
| 1508 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1509 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1510 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1511 | "Owner %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1512 | "extent list (next_free_rec == 0)", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1513 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1514 | status = -EIO; |
| 1515 | goto bail; |
| 1516 | } |
| 1517 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1518 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1519 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1520 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1521 | "Owner %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1522 | "list where extent # %d has no physical " |
| 1523 | "block start", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1524 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1525 | status = -EIO; |
| 1526 | goto bail; |
| 1527 | } |
| 1528 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1529 | brelse(bh); |
| 1530 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1531 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1532 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1533 | if (status < 0) { |
| 1534 | mlog_errno(status); |
| 1535 | goto bail; |
| 1536 | } |
| 1537 | |
| 1538 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1539 | el = &eb->h_list; |
| 1540 | |
| 1541 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1542 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1543 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1544 | lowest_bh = bh; |
| 1545 | get_bh(lowest_bh); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /* If we didn't find one and the fe doesn't have any room, |
| 1550 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1551 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1552 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1553 | status = 1; |
| 1554 | |
| 1555 | *target_bh = lowest_bh; |
| 1556 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1557 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1558 | |
| 1559 | mlog_exit(status); |
| 1560 | return status; |
| 1561 | } |
| 1562 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1563 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1564 | * Grow a b-tree so that it has more records. |
| 1565 | * |
| 1566 | * We might shift the tree depth in which case existing paths should |
| 1567 | * be considered invalid. |
| 1568 | * |
| 1569 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1570 | * |
| 1571 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1572 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1573 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 1574 | int *final_depth, struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1575 | struct ocfs2_alloc_context *meta_ac) |
| 1576 | { |
| 1577 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1578 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1579 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1580 | struct buffer_head *bh = NULL; |
| 1581 | |
| 1582 | BUG_ON(meta_ac == NULL); |
| 1583 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1584 | shift = ocfs2_find_branch_target(et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1585 | if (shift < 0) { |
| 1586 | ret = shift; |
| 1587 | mlog_errno(ret); |
| 1588 | goto out; |
| 1589 | } |
| 1590 | |
| 1591 | /* We traveled all the way to the bottom of the allocation tree |
| 1592 | * and didn't find room for any more extents - we need to add |
| 1593 | * another tree level */ |
| 1594 | if (shift) { |
| 1595 | BUG_ON(bh); |
| 1596 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 1597 | |
| 1598 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1599 | * the new extent block (so we can pass that to |
| 1600 | * ocfs2_add_branch). */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1601 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1602 | if (ret < 0) { |
| 1603 | mlog_errno(ret); |
| 1604 | goto out; |
| 1605 | } |
| 1606 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1607 | if (depth == 1) { |
| 1608 | /* |
| 1609 | * Special case: we have room now if we shifted from |
| 1610 | * tree_depth 0, so no more work needs to be done. |
| 1611 | * |
| 1612 | * We won't be calling add_branch, so pass |
| 1613 | * back *last_eb_bh as the new leaf. At depth |
| 1614 | * zero, it should always be null so there's |
| 1615 | * no reason to brelse. |
| 1616 | */ |
| 1617 | BUG_ON(*last_eb_bh); |
| 1618 | get_bh(bh); |
| 1619 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1620 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1621 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1625 | * the new data. */ |
| 1626 | mlog(0, "add branch. bh = %p\n", bh); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1627 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1628 | meta_ac); |
| 1629 | if (ret < 0) { |
| 1630 | mlog_errno(ret); |
| 1631 | goto out; |
| 1632 | } |
| 1633 | |
| 1634 | out: |
| 1635 | if (final_depth) |
| 1636 | *final_depth = depth; |
| 1637 | brelse(bh); |
| 1638 | return ret; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1642 | * This function will discard the rightmost extent record. |
| 1643 | */ |
| 1644 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1645 | { |
| 1646 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1647 | int count = le16_to_cpu(el->l_count); |
| 1648 | unsigned int num_bytes; |
| 1649 | |
| 1650 | BUG_ON(!next_free); |
| 1651 | /* This will cause us to go off the end of our extent list. */ |
| 1652 | BUG_ON(next_free >= count); |
| 1653 | |
| 1654 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1655 | |
| 1656 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1657 | } |
| 1658 | |
| 1659 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1660 | struct ocfs2_extent_rec *insert_rec) |
| 1661 | { |
| 1662 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1663 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1664 | struct ocfs2_extent_rec *rec; |
| 1665 | |
| 1666 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1667 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1668 | |
| 1669 | BUG_ON(!next_free); |
| 1670 | |
| 1671 | /* 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] | 1672 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1673 | |
| 1674 | /* |
| 1675 | * The easiest way to approach this is to just remove the |
| 1676 | * empty extent and temporarily decrement next_free. |
| 1677 | */ |
| 1678 | if (has_empty) { |
| 1679 | /* |
| 1680 | * If next_free was 1 (only an empty extent), this |
| 1681 | * loop won't execute, which is fine. We still want |
| 1682 | * the decrement above to happen. |
| 1683 | */ |
| 1684 | for(i = 0; i < (next_free - 1); i++) |
| 1685 | el->l_recs[i] = el->l_recs[i+1]; |
| 1686 | |
| 1687 | next_free--; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Figure out what the new record index should be. |
| 1692 | */ |
| 1693 | for(i = 0; i < next_free; i++) { |
| 1694 | rec = &el->l_recs[i]; |
| 1695 | |
| 1696 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1697 | break; |
| 1698 | } |
| 1699 | insert_index = i; |
| 1700 | |
| 1701 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1702 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1703 | |
| 1704 | BUG_ON(insert_index < 0); |
| 1705 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1706 | BUG_ON(insert_index > next_free); |
| 1707 | |
| 1708 | /* |
| 1709 | * No need to memmove if we're just adding to the tail. |
| 1710 | */ |
| 1711 | if (insert_index != next_free) { |
| 1712 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1713 | |
| 1714 | num_bytes = next_free - insert_index; |
| 1715 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1716 | memmove(&el->l_recs[insert_index + 1], |
| 1717 | &el->l_recs[insert_index], |
| 1718 | num_bytes); |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Either we had an empty extent, and need to re-increment or |
| 1723 | * there was no empty extent on a non full rightmost leaf node, |
| 1724 | * in which case we still need to increment. |
| 1725 | */ |
| 1726 | next_free++; |
| 1727 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1728 | /* |
| 1729 | * Make sure none of the math above just messed up our tree. |
| 1730 | */ |
| 1731 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1732 | |
| 1733 | el->l_recs[insert_index] = *insert_rec; |
| 1734 | |
| 1735 | } |
| 1736 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1737 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1738 | { |
| 1739 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1740 | |
| 1741 | BUG_ON(num_recs == 0); |
| 1742 | |
| 1743 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1744 | num_recs--; |
| 1745 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1746 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1747 | memset(&el->l_recs[num_recs], 0, |
| 1748 | sizeof(struct ocfs2_extent_rec)); |
| 1749 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1750 | } |
| 1751 | } |
| 1752 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1753 | /* |
| 1754 | * Create an empty extent record . |
| 1755 | * |
| 1756 | * l_next_free_rec may be updated. |
| 1757 | * |
| 1758 | * If an empty extent already exists do nothing. |
| 1759 | */ |
| 1760 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1761 | { |
| 1762 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1763 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1764 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1765 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1766 | if (next_free == 0) |
| 1767 | goto set_and_inc; |
| 1768 | |
| 1769 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1770 | return; |
| 1771 | |
| 1772 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1773 | "Asked to create an empty extent in a full list:\n" |
| 1774 | "count = %u, tree depth = %u", |
| 1775 | le16_to_cpu(el->l_count), |
| 1776 | le16_to_cpu(el->l_tree_depth)); |
| 1777 | |
| 1778 | ocfs2_shift_records_right(el); |
| 1779 | |
| 1780 | set_and_inc: |
| 1781 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1782 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1787 | * the lowest level tree node which contains a path to both leafs. This |
| 1788 | * resulting set of information can be used to form a complete "subtree" |
| 1789 | * |
| 1790 | * This function is passed two full paths from the dinode down to a |
| 1791 | * pair of adjacent leaves. It's task is to figure out which path |
| 1792 | * index contains the subtree root - this can be the root index itself |
| 1793 | * in a worst-case rotation. |
| 1794 | * |
| 1795 | * The array index of the subtree root is passed back. |
| 1796 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1797 | static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1798 | struct ocfs2_path *left, |
| 1799 | struct ocfs2_path *right) |
| 1800 | { |
| 1801 | int i = 0; |
| 1802 | |
| 1803 | /* |
| 1804 | * Check that the caller passed in two paths from the same tree. |
| 1805 | */ |
| 1806 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1807 | |
| 1808 | do { |
| 1809 | i++; |
| 1810 | |
| 1811 | /* |
| 1812 | * The caller didn't pass two adjacent paths. |
| 1813 | */ |
| 1814 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1815 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1816 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1817 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1818 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1819 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1820 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1821 | } while (left->p_node[i].bh->b_blocknr == |
| 1822 | right->p_node[i].bh->b_blocknr); |
| 1823 | |
| 1824 | return i - 1; |
| 1825 | } |
| 1826 | |
| 1827 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1828 | |
| 1829 | /* |
| 1830 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1831 | * |
| 1832 | * This code can be called with a cpos larger than the tree, in which |
| 1833 | * case it will return the rightmost path. |
| 1834 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1835 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1836 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1837 | path_insert_t *func, void *data) |
| 1838 | { |
| 1839 | int i, ret = 0; |
| 1840 | u32 range; |
| 1841 | u64 blkno; |
| 1842 | struct buffer_head *bh = NULL; |
| 1843 | struct ocfs2_extent_block *eb; |
| 1844 | struct ocfs2_extent_list *el; |
| 1845 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1846 | |
| 1847 | el = root_el; |
| 1848 | while (el->l_tree_depth) { |
| 1849 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1850 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1851 | "Owner %llu has empty extent list at " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1852 | "depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1853 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1854 | le16_to_cpu(el->l_tree_depth)); |
| 1855 | ret = -EROFS; |
| 1856 | goto out; |
| 1857 | |
| 1858 | } |
| 1859 | |
| 1860 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1861 | rec = &el->l_recs[i]; |
| 1862 | |
| 1863 | /* |
| 1864 | * In the case that cpos is off the allocation |
| 1865 | * tree, this should just wind up returning the |
| 1866 | * rightmost record. |
| 1867 | */ |
| 1868 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1869 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1870 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1871 | break; |
| 1872 | } |
| 1873 | |
| 1874 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1875 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1876 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1877 | "Owner %llu has bad blkno in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1878 | "at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1879 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1880 | le16_to_cpu(el->l_tree_depth), i); |
| 1881 | ret = -EROFS; |
| 1882 | goto out; |
| 1883 | } |
| 1884 | |
| 1885 | brelse(bh); |
| 1886 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1887 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1888 | if (ret) { |
| 1889 | mlog_errno(ret); |
| 1890 | goto out; |
| 1891 | } |
| 1892 | |
| 1893 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1894 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1895 | |
| 1896 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1897 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1898 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1899 | "Owner %llu has bad count in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1900 | "at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1901 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1902 | (unsigned long long)bh->b_blocknr, |
| 1903 | le16_to_cpu(el->l_next_free_rec), |
| 1904 | le16_to_cpu(el->l_count)); |
| 1905 | ret = -EROFS; |
| 1906 | goto out; |
| 1907 | } |
| 1908 | |
| 1909 | if (func) |
| 1910 | func(data, bh); |
| 1911 | } |
| 1912 | |
| 1913 | out: |
| 1914 | /* |
| 1915 | * Catch any trailing bh that the loop didn't handle. |
| 1916 | */ |
| 1917 | brelse(bh); |
| 1918 | |
| 1919 | return ret; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * Given an initialized path (that is, it has a valid root extent |
| 1924 | * list), this function will traverse the btree in search of the path |
| 1925 | * which would contain cpos. |
| 1926 | * |
| 1927 | * The path traveled is recorded in the path structure. |
| 1928 | * |
| 1929 | * Note that this will not do any comparisons on leaf node extent |
| 1930 | * records, so it will work fine in the case that we just added a tree |
| 1931 | * branch. |
| 1932 | */ |
| 1933 | struct find_path_data { |
| 1934 | int index; |
| 1935 | struct ocfs2_path *path; |
| 1936 | }; |
| 1937 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1938 | { |
| 1939 | struct find_path_data *fp = data; |
| 1940 | |
| 1941 | get_bh(bh); |
| 1942 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1943 | fp->index++; |
| 1944 | } |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1945 | static int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1946 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1947 | { |
| 1948 | struct find_path_data data; |
| 1949 | |
| 1950 | data.index = 1; |
| 1951 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1952 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1953 | find_path_ins, &data); |
| 1954 | } |
| 1955 | |
| 1956 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1957 | { |
| 1958 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1959 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1960 | struct buffer_head **ret = data; |
| 1961 | |
| 1962 | /* We want to retain only the leaf block. */ |
| 1963 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1964 | get_bh(bh); |
| 1965 | *ret = bh; |
| 1966 | } |
| 1967 | } |
| 1968 | /* |
| 1969 | * Find the leaf block in the tree which would contain cpos. No |
| 1970 | * checking of the actual leaf is done. |
| 1971 | * |
| 1972 | * Some paths want to call this instead of allocating a path structure |
| 1973 | * and calling ocfs2_find_path(). |
| 1974 | * |
| 1975 | * This function doesn't handle non btree extent lists. |
| 1976 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1977 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1978 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1979 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1980 | { |
| 1981 | int ret; |
| 1982 | struct buffer_head *bh = NULL; |
| 1983 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1984 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1985 | if (ret) { |
| 1986 | mlog_errno(ret); |
| 1987 | goto out; |
| 1988 | } |
| 1989 | |
| 1990 | *leaf_bh = bh; |
| 1991 | out: |
| 1992 | return ret; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1997 | * |
| 1998 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1999 | * we need to fix up the extent records above the changes to reflect |
| 2000 | * the new changes. |
| 2001 | * |
| 2002 | * left_rec: the record on the left. |
| 2003 | * left_child_el: is the child list pointed to by left_rec |
| 2004 | * right_rec: the record to the right of left_rec |
| 2005 | * right_child_el: is the child list pointed to by right_rec |
| 2006 | * |
| 2007 | * By definition, this only works on interior nodes. |
| 2008 | */ |
| 2009 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 2010 | struct ocfs2_extent_list *left_child_el, |
| 2011 | struct ocfs2_extent_rec *right_rec, |
| 2012 | struct ocfs2_extent_list *right_child_el) |
| 2013 | { |
| 2014 | u32 left_clusters, right_end; |
| 2015 | |
| 2016 | /* |
| 2017 | * Interior nodes never have holes. Their cpos is the cpos of |
| 2018 | * the leftmost record in their child list. Their cluster |
| 2019 | * count covers the full theoretical range of their child list |
| 2020 | * - the range between their cpos and the cpos of the record |
| 2021 | * immediately to their right. |
| 2022 | */ |
| 2023 | 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] | 2024 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 2025 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2026 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 2027 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 2028 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2029 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2030 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2031 | |
| 2032 | /* |
| 2033 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2034 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2035 | * updating e_cpos to keep the same highest cluster count. |
| 2036 | */ |
| 2037 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2038 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2039 | |
| 2040 | right_rec->e_cpos = left_rec->e_cpos; |
| 2041 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 2042 | |
| 2043 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2044 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * Adjust the adjacent root node records involved in a |
| 2049 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 2050 | * find it's index in the root list. |
| 2051 | */ |
| 2052 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 2053 | struct ocfs2_extent_list *left_el, |
| 2054 | struct ocfs2_extent_list *right_el, |
| 2055 | u64 left_el_blkno) |
| 2056 | { |
| 2057 | int i; |
| 2058 | |
| 2059 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 2060 | le16_to_cpu(left_el->l_tree_depth)); |
| 2061 | |
| 2062 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 2063 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 2064 | break; |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * The path walking code should have never returned a root and |
| 2069 | * two paths which are not adjacent. |
| 2070 | */ |
| 2071 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 2072 | |
| 2073 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 2074 | &root_el->l_recs[i + 1], right_el); |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * We've changed a leaf block (in right_path) and need to reflect that |
| 2079 | * change back up the subtree. |
| 2080 | * |
| 2081 | * This happens in multiple places: |
| 2082 | * - When we've moved an extent record from the left path leaf to the right |
| 2083 | * path leaf to make room for an empty extent in the left path leaf. |
| 2084 | * - When our insert into the right path leaf is at the leftmost edge |
| 2085 | * and requires an update of the path immediately to it's left. This |
| 2086 | * 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] | 2087 | * - When we've adjusted the last extent record in the left path leaf and the |
| 2088 | * 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] | 2089 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2090 | static void ocfs2_complete_edge_insert(handle_t *handle, |
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, idx; |
| 2096 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 2097 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 2098 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2099 | |
| 2100 | /* |
| 2101 | * Update the counts and position values within all the |
| 2102 | * interior nodes to reflect the leaf rotation we just did. |
| 2103 | * |
| 2104 | * The root node is handled below the loop. |
| 2105 | * |
| 2106 | * We begin the loop with right_el and left_el pointing to the |
| 2107 | * leaf lists and work our way up. |
| 2108 | * |
| 2109 | * NOTE: within this loop, left_el and right_el always refer |
| 2110 | * to the *child* lists. |
| 2111 | */ |
| 2112 | left_el = path_leaf_el(left_path); |
| 2113 | right_el = path_leaf_el(right_path); |
| 2114 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 2115 | mlog(0, "Adjust records at index %u\n", i); |
| 2116 | |
| 2117 | /* |
| 2118 | * One nice property of knowing that all of these |
| 2119 | * nodes are below the root is that we only deal with |
| 2120 | * the leftmost right node record and the rightmost |
| 2121 | * left node record. |
| 2122 | */ |
| 2123 | el = left_path->p_node[i].el; |
| 2124 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2125 | left_rec = &el->l_recs[idx]; |
| 2126 | |
| 2127 | el = right_path->p_node[i].el; |
| 2128 | right_rec = &el->l_recs[0]; |
| 2129 | |
| 2130 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 2131 | right_el); |
| 2132 | |
| 2133 | ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2134 | if (ret) |
| 2135 | mlog_errno(ret); |
| 2136 | |
| 2137 | ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
| 2138 | if (ret) |
| 2139 | mlog_errno(ret); |
| 2140 | |
| 2141 | /* |
| 2142 | * Setup our list pointers now so that the current |
| 2143 | * parents become children in the next iteration. |
| 2144 | */ |
| 2145 | left_el = left_path->p_node[i].el; |
| 2146 | right_el = right_path->p_node[i].el; |
| 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * At the root node, adjust the two adjacent records which |
| 2151 | * begin our path to the leaves. |
| 2152 | */ |
| 2153 | |
| 2154 | el = left_path->p_node[subtree_index].el; |
| 2155 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2156 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2157 | |
| 2158 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2159 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2160 | |
| 2161 | root_bh = left_path->p_node[subtree_index].bh; |
| 2162 | |
| 2163 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 2164 | if (ret) |
| 2165 | mlog_errno(ret); |
| 2166 | } |
| 2167 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2168 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2169 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2170 | struct ocfs2_path *left_path, |
| 2171 | struct ocfs2_path *right_path, |
| 2172 | int subtree_index) |
| 2173 | { |
| 2174 | int ret, i; |
| 2175 | struct buffer_head *right_leaf_bh; |
| 2176 | struct buffer_head *left_leaf_bh = NULL; |
| 2177 | struct buffer_head *root_bh; |
| 2178 | struct ocfs2_extent_list *right_el, *left_el; |
| 2179 | struct ocfs2_extent_rec move_rec; |
| 2180 | |
| 2181 | left_leaf_bh = path_leaf_bh(left_path); |
| 2182 | left_el = path_leaf_el(left_path); |
| 2183 | |
| 2184 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2185 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2186 | "Inode %llu has non-full interior leaf node %llu" |
| 2187 | "(next free = %u)", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2188 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2189 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2190 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2191 | return -EROFS; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * This extent block may already have an empty record, so we |
| 2196 | * return early if so. |
| 2197 | */ |
| 2198 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2199 | return 0; |
| 2200 | |
| 2201 | root_bh = left_path->p_node[subtree_index].bh; |
| 2202 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2203 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2204 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2205 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2206 | if (ret) { |
| 2207 | mlog_errno(ret); |
| 2208 | goto out; |
| 2209 | } |
| 2210 | |
| 2211 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2212 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2213 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2214 | if (ret) { |
| 2215 | mlog_errno(ret); |
| 2216 | goto out; |
| 2217 | } |
| 2218 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2219 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2220 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2221 | if (ret) { |
| 2222 | mlog_errno(ret); |
| 2223 | goto out; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | right_leaf_bh = path_leaf_bh(right_path); |
| 2228 | right_el = path_leaf_el(right_path); |
| 2229 | |
| 2230 | /* This is a code error, not a disk corruption. */ |
| 2231 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2232 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2233 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2234 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2235 | |
| 2236 | ocfs2_create_empty_extent(right_el); |
| 2237 | |
| 2238 | ret = ocfs2_journal_dirty(handle, right_leaf_bh); |
| 2239 | if (ret) { |
| 2240 | mlog_errno(ret); |
| 2241 | goto out; |
| 2242 | } |
| 2243 | |
| 2244 | /* Do the copy now. */ |
| 2245 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2246 | move_rec = left_el->l_recs[i]; |
| 2247 | right_el->l_recs[0] = move_rec; |
| 2248 | |
| 2249 | /* |
| 2250 | * Clear out the record we just copied and shift everything |
| 2251 | * over, leaving an empty extent in the left leaf. |
| 2252 | * |
| 2253 | * We temporarily subtract from next_free_rec so that the |
| 2254 | * shift will lose the tail record (which is now defunct). |
| 2255 | */ |
| 2256 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2257 | ocfs2_shift_records_right(left_el); |
| 2258 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2259 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2260 | |
| 2261 | ret = ocfs2_journal_dirty(handle, left_leaf_bh); |
| 2262 | if (ret) { |
| 2263 | mlog_errno(ret); |
| 2264 | goto out; |
| 2265 | } |
| 2266 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2267 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2268 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2269 | |
| 2270 | out: |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * Given a full path, determine what cpos value would return us a path |
| 2276 | * containing the leaf immediately to the left of the current one. |
| 2277 | * |
| 2278 | * Will return zero if the path passed in is already the leftmost path. |
| 2279 | */ |
| 2280 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2281 | struct ocfs2_path *path, u32 *cpos) |
| 2282 | { |
| 2283 | int i, j, ret = 0; |
| 2284 | u64 blkno; |
| 2285 | struct ocfs2_extent_list *el; |
| 2286 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2287 | BUG_ON(path->p_tree_depth == 0); |
| 2288 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2289 | *cpos = 0; |
| 2290 | |
| 2291 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2292 | |
| 2293 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2294 | i = path->p_tree_depth - 1; |
| 2295 | while (i >= 0) { |
| 2296 | el = path->p_node[i].el; |
| 2297 | |
| 2298 | /* |
| 2299 | * Find the extent record just before the one in our |
| 2300 | * path. |
| 2301 | */ |
| 2302 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2303 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2304 | if (j == 0) { |
| 2305 | if (i == 0) { |
| 2306 | /* |
| 2307 | * We've determined that the |
| 2308 | * path specified is already |
| 2309 | * the leftmost one - return a |
| 2310 | * cpos of zero. |
| 2311 | */ |
| 2312 | goto out; |
| 2313 | } |
| 2314 | /* |
| 2315 | * The leftmost record points to our |
| 2316 | * leaf - we need to travel up the |
| 2317 | * tree one level. |
| 2318 | */ |
| 2319 | goto next_node; |
| 2320 | } |
| 2321 | |
| 2322 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2323 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2324 | &el->l_recs[j - 1]); |
| 2325 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2326 | goto out; |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | * If we got here, we never found a valid node where |
| 2332 | * the tree indicated one should be. |
| 2333 | */ |
| 2334 | ocfs2_error(sb, |
| 2335 | "Invalid extent tree at extent block %llu\n", |
| 2336 | (unsigned long long)blkno); |
| 2337 | ret = -EROFS; |
| 2338 | goto out; |
| 2339 | |
| 2340 | next_node: |
| 2341 | blkno = path->p_node[i].bh->b_blocknr; |
| 2342 | i--; |
| 2343 | } |
| 2344 | |
| 2345 | out: |
| 2346 | return ret; |
| 2347 | } |
| 2348 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2349 | /* |
| 2350 | * Extend the transaction by enough credits to complete the rotation, |
| 2351 | * and still leave at least the original number of credits allocated |
| 2352 | * to this transaction. |
| 2353 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2354 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2355 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2356 | struct ocfs2_path *path) |
| 2357 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2358 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2359 | |
| 2360 | if (handle->h_buffer_credits < credits) |
| 2361 | return ocfs2_extend_trans(handle, credits); |
| 2362 | |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
| 2366 | /* |
| 2367 | * Trap the case where we're inserting into the theoretical range past |
| 2368 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2369 | * whose cpos is less than ours into the right leaf. |
| 2370 | * |
| 2371 | * It's only necessary to look at the rightmost record of the left |
| 2372 | * leaf because the logic that calls us should ensure that the |
| 2373 | * theoretical ranges in the path components above the leaves are |
| 2374 | * correct. |
| 2375 | */ |
| 2376 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2377 | u32 insert_cpos) |
| 2378 | { |
| 2379 | struct ocfs2_extent_list *left_el; |
| 2380 | struct ocfs2_extent_rec *rec; |
| 2381 | int next_free; |
| 2382 | |
| 2383 | left_el = path_leaf_el(left_path); |
| 2384 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2385 | rec = &left_el->l_recs[next_free - 1]; |
| 2386 | |
| 2387 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2388 | return 1; |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2392 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2393 | { |
| 2394 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2395 | unsigned int range; |
| 2396 | struct ocfs2_extent_rec *rec; |
| 2397 | |
| 2398 | if (next_free == 0) |
| 2399 | return 0; |
| 2400 | |
| 2401 | rec = &el->l_recs[0]; |
| 2402 | if (ocfs2_is_empty_extent(rec)) { |
| 2403 | /* Empty list. */ |
| 2404 | if (next_free == 1) |
| 2405 | return 0; |
| 2406 | rec = &el->l_recs[1]; |
| 2407 | } |
| 2408 | |
| 2409 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2410 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2411 | return 1; |
| 2412 | return 0; |
| 2413 | } |
| 2414 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2415 | /* |
| 2416 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2417 | * |
| 2418 | * The path to the rightmost leaf should be passed in. |
| 2419 | * |
| 2420 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2421 | * |
| 2422 | * Upon succesful return from this function: |
| 2423 | * |
| 2424 | * - The 'right_path' array will contain a path to the leaf block |
| 2425 | * whose range contains e_cpos. |
| 2426 | * - That leaf block will have a single empty extent in list index 0. |
| 2427 | * - In the case that the rotation requires a post-insert update, |
| 2428 | * *ret_left_path will contain a valid path which can be passed to |
| 2429 | * ocfs2_insert_path(). |
| 2430 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2431 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2432 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2433 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2434 | u32 insert_cpos, |
| 2435 | struct ocfs2_path *right_path, |
| 2436 | struct ocfs2_path **ret_left_path) |
| 2437 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2438 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2439 | u32 cpos; |
| 2440 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2441 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2442 | |
| 2443 | *ret_left_path = NULL; |
| 2444 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2445 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2446 | if (!left_path) { |
| 2447 | ret = -ENOMEM; |
| 2448 | mlog_errno(ret); |
| 2449 | goto out; |
| 2450 | } |
| 2451 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2452 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2453 | if (ret) { |
| 2454 | mlog_errno(ret); |
| 2455 | goto out; |
| 2456 | } |
| 2457 | |
| 2458 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 2459 | |
| 2460 | /* |
| 2461 | * What we want to do here is: |
| 2462 | * |
| 2463 | * 1) Start with the rightmost path. |
| 2464 | * |
| 2465 | * 2) Determine a path to the leaf block directly to the left |
| 2466 | * of that leaf. |
| 2467 | * |
| 2468 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2469 | * which contains a path to both leaves. |
| 2470 | * |
| 2471 | * 4) Rotate the subtree. |
| 2472 | * |
| 2473 | * 5) Find the next subtree by considering the left path to be |
| 2474 | * the new right path. |
| 2475 | * |
| 2476 | * The check at the top of this while loop also accepts |
| 2477 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2478 | * value to get us the left path - insert_cpos might very well |
| 2479 | * be filling that hole. |
| 2480 | * |
| 2481 | * Stop at a cpos of '0' because we either started at the |
| 2482 | * leftmost branch (i.e., a tree with one branch and a |
| 2483 | * rotation inside of it), or we've gone as far as we can in |
| 2484 | * rotating subtrees. |
| 2485 | */ |
| 2486 | while (cpos && insert_cpos <= cpos) { |
| 2487 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 2488 | insert_cpos, cpos); |
| 2489 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2490 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2491 | if (ret) { |
| 2492 | mlog_errno(ret); |
| 2493 | goto out; |
| 2494 | } |
| 2495 | |
| 2496 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2497 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2498 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2499 | "(left path cpos %u) results in two identical " |
| 2500 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2501 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2502 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2503 | (unsigned long long) |
| 2504 | path_leaf_bh(left_path)->b_blocknr); |
| 2505 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2506 | if (split == SPLIT_NONE && |
| 2507 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2508 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2509 | |
| 2510 | /* |
| 2511 | * We've rotated the tree as much as we |
| 2512 | * should. The rest is up to |
| 2513 | * ocfs2_insert_path() to complete, after the |
| 2514 | * record insertion. We indicate this |
| 2515 | * situation by returning the left path. |
| 2516 | * |
| 2517 | * The reason we don't adjust the records here |
| 2518 | * before the record insert is that an error |
| 2519 | * later might break the rule where a parent |
| 2520 | * record e_cpos will reflect the actual |
| 2521 | * e_cpos of the 1st nonempty record of the |
| 2522 | * child list. |
| 2523 | */ |
| 2524 | *ret_left_path = left_path; |
| 2525 | goto out_ret_path; |
| 2526 | } |
| 2527 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2528 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2529 | |
| 2530 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2531 | start, |
| 2532 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 2533 | right_path->p_tree_depth); |
| 2534 | |
| 2535 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2536 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2537 | if (ret) { |
| 2538 | mlog_errno(ret); |
| 2539 | goto out; |
| 2540 | } |
| 2541 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2542 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2543 | right_path, start); |
| 2544 | if (ret) { |
| 2545 | mlog_errno(ret); |
| 2546 | goto out; |
| 2547 | } |
| 2548 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2549 | if (split != SPLIT_NONE && |
| 2550 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2551 | insert_cpos)) { |
| 2552 | /* |
| 2553 | * A rotate moves the rightmost left leaf |
| 2554 | * record over to the leftmost right leaf |
| 2555 | * slot. If we're doing an extent split |
| 2556 | * instead of a real insert, then we have to |
| 2557 | * check that the extent to be split wasn't |
| 2558 | * just moved over. If it was, then we can |
| 2559 | * exit here, passing left_path back - |
| 2560 | * ocfs2_split_extent() is smart enough to |
| 2561 | * search both leaves. |
| 2562 | */ |
| 2563 | *ret_left_path = left_path; |
| 2564 | goto out_ret_path; |
| 2565 | } |
| 2566 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2567 | /* |
| 2568 | * There is no need to re-read the next right path |
| 2569 | * as we know that it'll be our current left |
| 2570 | * path. Optimize by copying values instead. |
| 2571 | */ |
| 2572 | ocfs2_mv_path(right_path, left_path); |
| 2573 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2574 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2575 | if (ret) { |
| 2576 | mlog_errno(ret); |
| 2577 | goto out; |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | out: |
| 2582 | ocfs2_free_path(left_path); |
| 2583 | |
| 2584 | out_ret_path: |
| 2585 | return ret; |
| 2586 | } |
| 2587 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2588 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2589 | struct ocfs2_extent_tree *et, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2590 | int subtree_index, struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2591 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2592 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2593 | struct ocfs2_extent_rec *rec; |
| 2594 | struct ocfs2_extent_list *el; |
| 2595 | struct ocfs2_extent_block *eb; |
| 2596 | u32 range; |
| 2597 | |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2598 | /* |
| 2599 | * In normal tree rotation process, we will never touch the |
| 2600 | * tree branch above subtree_index and ocfs2_extend_rotate_transaction |
| 2601 | * doesn't reserve the credits for them either. |
| 2602 | * |
| 2603 | * But we do have a special case here which will update the rightmost |
| 2604 | * records for all the bh in the path. |
| 2605 | * So we have to allocate extra credits and access them. |
| 2606 | */ |
| 2607 | ret = ocfs2_extend_trans(handle, |
| 2608 | handle->h_buffer_credits + subtree_index); |
| 2609 | if (ret) { |
| 2610 | mlog_errno(ret); |
| 2611 | goto out; |
| 2612 | } |
| 2613 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2614 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2615 | if (ret) { |
| 2616 | mlog_errno(ret); |
| 2617 | goto out; |
| 2618 | } |
| 2619 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2620 | /* Path should always be rightmost. */ |
| 2621 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2622 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2623 | |
| 2624 | el = &eb->h_list; |
| 2625 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2626 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2627 | rec = &el->l_recs[idx]; |
| 2628 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2629 | |
| 2630 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2631 | el = path->p_node[i].el; |
| 2632 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2633 | rec = &el->l_recs[idx]; |
| 2634 | |
| 2635 | rec->e_int_clusters = cpu_to_le32(range); |
| 2636 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2637 | |
| 2638 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2639 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2640 | out: |
| 2641 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2644 | static void ocfs2_unlink_path(handle_t *handle, |
| 2645 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2646 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2647 | struct ocfs2_path *path, int unlink_start) |
| 2648 | { |
| 2649 | int ret, i; |
| 2650 | struct ocfs2_extent_block *eb; |
| 2651 | struct ocfs2_extent_list *el; |
| 2652 | struct buffer_head *bh; |
| 2653 | |
| 2654 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2655 | bh = path->p_node[i].bh; |
| 2656 | |
| 2657 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2658 | /* |
| 2659 | * Not all nodes might have had their final count |
| 2660 | * decremented by the caller - handle this here. |
| 2661 | */ |
| 2662 | el = &eb->h_list; |
| 2663 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2664 | mlog(ML_ERROR, |
| 2665 | "Inode %llu, attempted to remove extent block " |
| 2666 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2667 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2668 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2669 | le16_to_cpu(el->l_next_free_rec)); |
| 2670 | |
| 2671 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2672 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2673 | continue; |
| 2674 | } |
| 2675 | |
| 2676 | el->l_next_free_rec = 0; |
| 2677 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2678 | |
| 2679 | ocfs2_journal_dirty(handle, bh); |
| 2680 | |
| 2681 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2682 | if (ret) |
| 2683 | mlog_errno(ret); |
| 2684 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2685 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2686 | } |
| 2687 | } |
| 2688 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2689 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2690 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2691 | struct ocfs2_path *left_path, |
| 2692 | struct ocfs2_path *right_path, |
| 2693 | int subtree_index, |
| 2694 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2695 | { |
| 2696 | int i; |
| 2697 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2698 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2699 | struct ocfs2_extent_list *el; |
| 2700 | struct ocfs2_extent_block *eb; |
| 2701 | |
| 2702 | el = path_leaf_el(left_path); |
| 2703 | |
| 2704 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2705 | |
| 2706 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2707 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2708 | break; |
| 2709 | |
| 2710 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2711 | |
| 2712 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2713 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2714 | |
| 2715 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2716 | eb->h_next_leaf_blk = 0; |
| 2717 | |
| 2718 | ocfs2_journal_dirty(handle, root_bh); |
| 2719 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2720 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2721 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2722 | subtree_index + 1); |
| 2723 | } |
| 2724 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2725 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
| 2726 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2727 | struct ocfs2_path *left_path, |
| 2728 | struct ocfs2_path *right_path, |
| 2729 | int subtree_index, |
| 2730 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2731 | int *deleted) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2732 | { |
| 2733 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2734 | 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] | 2735 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2736 | struct ocfs2_extent_block *eb; |
| 2737 | |
| 2738 | *deleted = 0; |
| 2739 | |
| 2740 | right_leaf_el = path_leaf_el(right_path); |
| 2741 | left_leaf_el = path_leaf_el(left_path); |
| 2742 | root_bh = left_path->p_node[subtree_index].bh; |
| 2743 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2744 | |
| 2745 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2746 | return 0; |
| 2747 | |
| 2748 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2749 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2750 | /* |
| 2751 | * It's legal for us to proceed if the right leaf is |
| 2752 | * the rightmost one and it has an empty extent. There |
| 2753 | * are two cases to handle - whether the leaf will be |
| 2754 | * empty after removal or not. If the leaf isn't empty |
| 2755 | * then just remove the empty extent up front. The |
| 2756 | * next block will handle empty leaves by flagging |
| 2757 | * them for unlink. |
| 2758 | * |
| 2759 | * Non rightmost leaves will throw -EAGAIN and the |
| 2760 | * caller can manually move the subtree and retry. |
| 2761 | */ |
| 2762 | |
| 2763 | if (eb->h_next_leaf_blk != 0ULL) |
| 2764 | return -EAGAIN; |
| 2765 | |
| 2766 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2767 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2768 | path_leaf_bh(right_path), |
| 2769 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2770 | if (ret) { |
| 2771 | mlog_errno(ret); |
| 2772 | goto out; |
| 2773 | } |
| 2774 | |
| 2775 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2776 | } else |
| 2777 | right_has_empty = 1; |
| 2778 | } |
| 2779 | |
| 2780 | if (eb->h_next_leaf_blk == 0ULL && |
| 2781 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2782 | /* |
| 2783 | * We have to update i_last_eb_blk during the meta |
| 2784 | * data delete. |
| 2785 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2786 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2787 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2788 | if (ret) { |
| 2789 | mlog_errno(ret); |
| 2790 | goto out; |
| 2791 | } |
| 2792 | |
| 2793 | del_right_subtree = 1; |
| 2794 | } |
| 2795 | |
| 2796 | /* |
| 2797 | * Getting here with an empty extent in the right path implies |
| 2798 | * that it's the rightmost path and will be deleted. |
| 2799 | */ |
| 2800 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2801 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2802 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2803 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2804 | if (ret) { |
| 2805 | mlog_errno(ret); |
| 2806 | goto out; |
| 2807 | } |
| 2808 | |
| 2809 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2810 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2811 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2812 | if (ret) { |
| 2813 | mlog_errno(ret); |
| 2814 | goto out; |
| 2815 | } |
| 2816 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2817 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2818 | left_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2819 | if (ret) { |
| 2820 | mlog_errno(ret); |
| 2821 | goto out; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | if (!right_has_empty) { |
| 2826 | /* |
| 2827 | * Only do this if we're moving a real |
| 2828 | * record. Otherwise, the action is delayed until |
| 2829 | * after removal of the right path in which case we |
| 2830 | * can do a simple shift to remove the empty extent. |
| 2831 | */ |
| 2832 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2833 | memset(&right_leaf_el->l_recs[0], 0, |
| 2834 | sizeof(struct ocfs2_extent_rec)); |
| 2835 | } |
| 2836 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2837 | /* |
| 2838 | * Move recs over to get rid of empty extent, decrease |
| 2839 | * next_free. This is allowed to remove the last |
| 2840 | * extent in our leaf (setting l_next_free_rec to |
| 2841 | * zero) - the delete code below won't care. |
| 2842 | */ |
| 2843 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2844 | } |
| 2845 | |
| 2846 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2847 | if (ret) |
| 2848 | mlog_errno(ret); |
| 2849 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 2850 | if (ret) |
| 2851 | mlog_errno(ret); |
| 2852 | |
| 2853 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2854 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2855 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2856 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2857 | left_path); |
| 2858 | if (ret) { |
| 2859 | mlog_errno(ret); |
| 2860 | goto out; |
| 2861 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2862 | |
| 2863 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2864 | 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] | 2865 | |
| 2866 | /* |
| 2867 | * Removal of the extent in the left leaf was skipped |
| 2868 | * above so we could delete the right path |
| 2869 | * 1st. |
| 2870 | */ |
| 2871 | if (right_has_empty) |
| 2872 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2873 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2874 | ret = ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2875 | if (ret) |
| 2876 | mlog_errno(ret); |
| 2877 | |
| 2878 | *deleted = 1; |
| 2879 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2880 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2881 | subtree_index); |
| 2882 | |
| 2883 | out: |
| 2884 | return ret; |
| 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * Given a full path, determine what cpos value would return us a path |
| 2889 | * containing the leaf immediately to the right of the current one. |
| 2890 | * |
| 2891 | * Will return zero if the path passed in is already the rightmost path. |
| 2892 | * |
| 2893 | * This looks similar, but is subtly different to |
| 2894 | * ocfs2_find_cpos_for_left_leaf(). |
| 2895 | */ |
| 2896 | static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2897 | struct ocfs2_path *path, u32 *cpos) |
| 2898 | { |
| 2899 | int i, j, ret = 0; |
| 2900 | u64 blkno; |
| 2901 | struct ocfs2_extent_list *el; |
| 2902 | |
| 2903 | *cpos = 0; |
| 2904 | |
| 2905 | if (path->p_tree_depth == 0) |
| 2906 | return 0; |
| 2907 | |
| 2908 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2909 | |
| 2910 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2911 | i = path->p_tree_depth - 1; |
| 2912 | while (i >= 0) { |
| 2913 | int next_free; |
| 2914 | |
| 2915 | el = path->p_node[i].el; |
| 2916 | |
| 2917 | /* |
| 2918 | * Find the extent record just after the one in our |
| 2919 | * path. |
| 2920 | */ |
| 2921 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2922 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2923 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2924 | if (j == (next_free - 1)) { |
| 2925 | if (i == 0) { |
| 2926 | /* |
| 2927 | * We've determined that the |
| 2928 | * path specified is already |
| 2929 | * the rightmost one - return a |
| 2930 | * cpos of zero. |
| 2931 | */ |
| 2932 | goto out; |
| 2933 | } |
| 2934 | /* |
| 2935 | * The rightmost record points to our |
| 2936 | * leaf - we need to travel up the |
| 2937 | * tree one level. |
| 2938 | */ |
| 2939 | goto next_node; |
| 2940 | } |
| 2941 | |
| 2942 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2943 | goto out; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | /* |
| 2948 | * If we got here, we never found a valid node where |
| 2949 | * the tree indicated one should be. |
| 2950 | */ |
| 2951 | ocfs2_error(sb, |
| 2952 | "Invalid extent tree at extent block %llu\n", |
| 2953 | (unsigned long long)blkno); |
| 2954 | ret = -EROFS; |
| 2955 | goto out; |
| 2956 | |
| 2957 | next_node: |
| 2958 | blkno = path->p_node[i].bh->b_blocknr; |
| 2959 | i--; |
| 2960 | } |
| 2961 | |
| 2962 | out: |
| 2963 | return ret; |
| 2964 | } |
| 2965 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2966 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
| 2967 | struct ocfs2_extent_tree *et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2968 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2969 | { |
| 2970 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2971 | struct buffer_head *bh = path_leaf_bh(path); |
| 2972 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2973 | |
| 2974 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2975 | return 0; |
| 2976 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2977 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2978 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2979 | if (ret) { |
| 2980 | mlog_errno(ret); |
| 2981 | goto out; |
| 2982 | } |
| 2983 | |
| 2984 | ocfs2_remove_empty_extent(el); |
| 2985 | |
| 2986 | ret = ocfs2_journal_dirty(handle, bh); |
| 2987 | if (ret) |
| 2988 | mlog_errno(ret); |
| 2989 | |
| 2990 | out: |
| 2991 | return ret; |
| 2992 | } |
| 2993 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2994 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
| 2995 | struct ocfs2_extent_tree *et, |
| 2996 | int orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2997 | struct ocfs2_path *path, |
| 2998 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2999 | struct ocfs2_path **empty_extent_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3000 | { |
| 3001 | int ret, subtree_root, deleted; |
| 3002 | u32 right_cpos; |
| 3003 | struct ocfs2_path *left_path = NULL; |
| 3004 | struct ocfs2_path *right_path = NULL; |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3005 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3006 | |
| 3007 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 3008 | |
| 3009 | *empty_extent_path = NULL; |
| 3010 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3011 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3012 | if (ret) { |
| 3013 | mlog_errno(ret); |
| 3014 | goto out; |
| 3015 | } |
| 3016 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3017 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3018 | if (!left_path) { |
| 3019 | ret = -ENOMEM; |
| 3020 | mlog_errno(ret); |
| 3021 | goto out; |
| 3022 | } |
| 3023 | |
| 3024 | ocfs2_cp_path(left_path, path); |
| 3025 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3026 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3027 | if (!right_path) { |
| 3028 | ret = -ENOMEM; |
| 3029 | mlog_errno(ret); |
| 3030 | goto out; |
| 3031 | } |
| 3032 | |
| 3033 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3034 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3035 | if (ret) { |
| 3036 | mlog_errno(ret); |
| 3037 | goto out; |
| 3038 | } |
| 3039 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3040 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3041 | right_path); |
| 3042 | |
| 3043 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 3044 | subtree_root, |
| 3045 | (unsigned long long) |
| 3046 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 3047 | right_path->p_tree_depth); |
| 3048 | |
| 3049 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 3050 | orig_credits, left_path); |
| 3051 | if (ret) { |
| 3052 | mlog_errno(ret); |
| 3053 | goto out; |
| 3054 | } |
| 3055 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 3056 | /* |
| 3057 | * Caller might still want to make changes to the |
| 3058 | * tree root, so re-add it to the journal here. |
| 3059 | */ |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3060 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3061 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 3062 | if (ret) { |
| 3063 | mlog_errno(ret); |
| 3064 | goto out; |
| 3065 | } |
| 3066 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 3067 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3068 | right_path, subtree_root, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 3069 | dealloc, &deleted); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3070 | if (ret == -EAGAIN) { |
| 3071 | /* |
| 3072 | * The rotation has to temporarily stop due to |
| 3073 | * the right subtree having an empty |
| 3074 | * extent. Pass it back to the caller for a |
| 3075 | * fixup. |
| 3076 | */ |
| 3077 | *empty_extent_path = right_path; |
| 3078 | right_path = NULL; |
| 3079 | goto out; |
| 3080 | } |
| 3081 | if (ret) { |
| 3082 | mlog_errno(ret); |
| 3083 | goto out; |
| 3084 | } |
| 3085 | |
| 3086 | /* |
| 3087 | * The subtree rotate might have removed records on |
| 3088 | * the rightmost edge. If so, then rotation is |
| 3089 | * complete. |
| 3090 | */ |
| 3091 | if (deleted) |
| 3092 | break; |
| 3093 | |
| 3094 | ocfs2_mv_path(left_path, right_path); |
| 3095 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3096 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3097 | &right_cpos); |
| 3098 | if (ret) { |
| 3099 | mlog_errno(ret); |
| 3100 | goto out; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | out: |
| 3105 | ocfs2_free_path(right_path); |
| 3106 | ocfs2_free_path(left_path); |
| 3107 | |
| 3108 | return ret; |
| 3109 | } |
| 3110 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3111 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
| 3112 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3113 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3114 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3115 | { |
| 3116 | int ret, subtree_index; |
| 3117 | u32 cpos; |
| 3118 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3119 | struct ocfs2_extent_block *eb; |
| 3120 | struct ocfs2_extent_list *el; |
| 3121 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3122 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 3123 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3124 | if (ret) |
| 3125 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3126 | /* |
| 3127 | * There's two ways we handle this depending on |
| 3128 | * whether path is the only existing one. |
| 3129 | */ |
| 3130 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3131 | handle->h_buffer_credits, |
| 3132 | path); |
| 3133 | if (ret) { |
| 3134 | mlog_errno(ret); |
| 3135 | goto out; |
| 3136 | } |
| 3137 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3138 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3139 | if (ret) { |
| 3140 | mlog_errno(ret); |
| 3141 | goto out; |
| 3142 | } |
| 3143 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3144 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3145 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3146 | if (ret) { |
| 3147 | mlog_errno(ret); |
| 3148 | goto out; |
| 3149 | } |
| 3150 | |
| 3151 | if (cpos) { |
| 3152 | /* |
| 3153 | * We have a path to the left of this one - it needs |
| 3154 | * an update too. |
| 3155 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3156 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3157 | if (!left_path) { |
| 3158 | ret = -ENOMEM; |
| 3159 | mlog_errno(ret); |
| 3160 | goto out; |
| 3161 | } |
| 3162 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3163 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3164 | if (ret) { |
| 3165 | mlog_errno(ret); |
| 3166 | goto out; |
| 3167 | } |
| 3168 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3169 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3170 | if (ret) { |
| 3171 | mlog_errno(ret); |
| 3172 | goto out; |
| 3173 | } |
| 3174 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3175 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3176 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3177 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3178 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 3179 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3180 | left_path); |
| 3181 | if (ret) { |
| 3182 | mlog_errno(ret); |
| 3183 | goto out; |
| 3184 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3185 | |
| 3186 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3187 | 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] | 3188 | } else { |
| 3189 | /* |
| 3190 | * 'path' is also the leftmost path which |
| 3191 | * means it must be the only one. This gets |
| 3192 | * handled differently because we want to |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3193 | * revert the root back to having extents |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3194 | * in-line. |
| 3195 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3196 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3197 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3198 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3199 | el->l_tree_depth = 0; |
| 3200 | el->l_next_free_rec = 0; |
| 3201 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3202 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3203 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3207 | |
| 3208 | out: |
| 3209 | ocfs2_free_path(left_path); |
| 3210 | return ret; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * Left rotation of btree records. |
| 3215 | * |
| 3216 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3217 | * rotation. We start at some non-rightmost path containing an empty |
| 3218 | * extent in the leaf block. The code works its way to the rightmost |
| 3219 | * path by rotating records to the left in every subtree. |
| 3220 | * |
| 3221 | * This is used by any code which reduces the number of extent records |
| 3222 | * in a leaf. After removal, an empty record should be placed in the |
| 3223 | * leftmost list position. |
| 3224 | * |
| 3225 | * This won't handle a length update of the rightmost path records if |
| 3226 | * the rightmost tree leaf record is removed so the caller is |
| 3227 | * responsible for detecting and correcting that. |
| 3228 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3229 | static int ocfs2_rotate_tree_left(handle_t *handle, |
| 3230 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3231 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3232 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3233 | { |
| 3234 | int ret, orig_credits = handle->h_buffer_credits; |
| 3235 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3236 | struct ocfs2_extent_block *eb; |
| 3237 | struct ocfs2_extent_list *el; |
| 3238 | |
| 3239 | el = path_leaf_el(path); |
| 3240 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3241 | return 0; |
| 3242 | |
| 3243 | if (path->p_tree_depth == 0) { |
| 3244 | rightmost_no_delete: |
| 3245 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3246 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3247 | * it up front. |
| 3248 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3249 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3250 | if (ret) |
| 3251 | mlog_errno(ret); |
| 3252 | goto out; |
| 3253 | } |
| 3254 | |
| 3255 | /* |
| 3256 | * Handle rightmost branch now. There's several cases: |
| 3257 | * 1) simple rotation leaving records in there. That's trivial. |
| 3258 | * 2) rotation requiring a branch delete - there's no more |
| 3259 | * records left. Two cases of this: |
| 3260 | * a) There are branches to the left. |
| 3261 | * b) This is also the leftmost (the only) branch. |
| 3262 | * |
| 3263 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3264 | * 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] | 3265 | * 2b) we need to bring the root back to inline extents. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3266 | */ |
| 3267 | |
| 3268 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3269 | el = &eb->h_list; |
| 3270 | if (eb->h_next_leaf_blk == 0) { |
| 3271 | /* |
| 3272 | * This gets a bit tricky if we're going to delete the |
| 3273 | * rightmost path. Get the other cases out of the way |
| 3274 | * 1st. |
| 3275 | */ |
| 3276 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3277 | goto rightmost_no_delete; |
| 3278 | |
| 3279 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3280 | ret = -EIO; |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3281 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3282 | "Owner %llu has empty extent block at %llu", |
| 3283 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3284 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3285 | goto out; |
| 3286 | } |
| 3287 | |
| 3288 | /* |
| 3289 | * XXX: The caller can not trust "path" any more after |
| 3290 | * this as it will have been deleted. What do we do? |
| 3291 | * |
| 3292 | * In theory the rotate-for-merge code will never get |
| 3293 | * here because it'll always ask for a rotate in a |
| 3294 | * nonempty list. |
| 3295 | */ |
| 3296 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3297 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
| 3298 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3299 | if (ret) |
| 3300 | mlog_errno(ret); |
| 3301 | goto out; |
| 3302 | } |
| 3303 | |
| 3304 | /* |
| 3305 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3306 | * and restarting from there. |
| 3307 | */ |
| 3308 | try_rotate: |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3309 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
| 3310 | dealloc, &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3311 | if (ret && ret != -EAGAIN) { |
| 3312 | mlog_errno(ret); |
| 3313 | goto out; |
| 3314 | } |
| 3315 | |
| 3316 | while (ret == -EAGAIN) { |
| 3317 | tmp_path = restart_path; |
| 3318 | restart_path = NULL; |
| 3319 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3320 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3321 | tmp_path, dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3322 | &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3323 | if (ret && ret != -EAGAIN) { |
| 3324 | mlog_errno(ret); |
| 3325 | goto out; |
| 3326 | } |
| 3327 | |
| 3328 | ocfs2_free_path(tmp_path); |
| 3329 | tmp_path = NULL; |
| 3330 | |
| 3331 | if (ret == 0) |
| 3332 | goto try_rotate; |
| 3333 | } |
| 3334 | |
| 3335 | out: |
| 3336 | ocfs2_free_path(tmp_path); |
| 3337 | ocfs2_free_path(restart_path); |
| 3338 | return ret; |
| 3339 | } |
| 3340 | |
| 3341 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3342 | int index) |
| 3343 | { |
| 3344 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3345 | unsigned int size; |
| 3346 | |
| 3347 | if (rec->e_leaf_clusters == 0) { |
| 3348 | /* |
| 3349 | * We consumed all of the merged-from record. An empty |
| 3350 | * extent cannot exist anywhere but the 1st array |
| 3351 | * position, so move things over if the merged-from |
| 3352 | * record doesn't occupy that position. |
| 3353 | * |
| 3354 | * This creates a new empty extent so the caller |
| 3355 | * should be smart enough to have removed any existing |
| 3356 | * ones. |
| 3357 | */ |
| 3358 | if (index > 0) { |
| 3359 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3360 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3361 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * Always memset - the caller doesn't check whether it |
| 3366 | * created an empty extent, so there could be junk in |
| 3367 | * the other fields. |
| 3368 | */ |
| 3369 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3370 | } |
| 3371 | } |
| 3372 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3373 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3374 | struct ocfs2_path *left_path, |
| 3375 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3376 | { |
| 3377 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3378 | u32 right_cpos; |
| 3379 | struct ocfs2_path *right_path = NULL; |
| 3380 | struct ocfs2_extent_list *left_el; |
| 3381 | |
| 3382 | *ret_right_path = NULL; |
| 3383 | |
| 3384 | /* This function shouldn't be called for non-trees. */ |
| 3385 | BUG_ON(left_path->p_tree_depth == 0); |
| 3386 | |
| 3387 | left_el = path_leaf_el(left_path); |
| 3388 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3389 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3390 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3391 | left_path, &right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3392 | if (ret) { |
| 3393 | mlog_errno(ret); |
| 3394 | goto out; |
| 3395 | } |
| 3396 | |
| 3397 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3398 | BUG_ON(right_cpos == 0); |
| 3399 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3400 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3401 | if (!right_path) { |
| 3402 | ret = -ENOMEM; |
| 3403 | mlog_errno(ret); |
| 3404 | goto out; |
| 3405 | } |
| 3406 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3407 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3408 | if (ret) { |
| 3409 | mlog_errno(ret); |
| 3410 | goto out; |
| 3411 | } |
| 3412 | |
| 3413 | *ret_right_path = right_path; |
| 3414 | out: |
| 3415 | if (ret) |
| 3416 | ocfs2_free_path(right_path); |
| 3417 | return ret; |
| 3418 | } |
| 3419 | |
| 3420 | /* |
| 3421 | * Remove split_rec clusters from the record at index and merge them |
| 3422 | * onto the beginning of the record "next" to it. |
| 3423 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3424 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3425 | * next extent block. |
| 3426 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3427 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3428 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3429 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3430 | struct ocfs2_extent_rec *split_rec, |
| 3431 | int index) |
| 3432 | { |
| 3433 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3434 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3435 | struct ocfs2_extent_rec *left_rec; |
| 3436 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3437 | struct ocfs2_extent_list *right_el; |
| 3438 | struct ocfs2_path *right_path = NULL; |
| 3439 | int subtree_index = 0; |
| 3440 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3441 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3442 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3443 | |
| 3444 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3445 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3446 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3447 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3448 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3449 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3450 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3451 | if (ret) { |
| 3452 | mlog_errno(ret); |
| 3453 | goto out; |
| 3454 | } |
| 3455 | |
| 3456 | right_el = path_leaf_el(right_path); |
| 3457 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3458 | BUG_ON(next_free <= 0); |
| 3459 | right_rec = &right_el->l_recs[0]; |
| 3460 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3461 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3462 | right_rec = &right_el->l_recs[1]; |
| 3463 | } |
| 3464 | |
| 3465 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3466 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3467 | le32_to_cpu(right_rec->e_cpos)); |
| 3468 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3469 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3470 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3471 | |
| 3472 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3473 | handle->h_buffer_credits, |
| 3474 | right_path); |
| 3475 | if (ret) { |
| 3476 | mlog_errno(ret); |
| 3477 | goto out; |
| 3478 | } |
| 3479 | |
| 3480 | root_bh = left_path->p_node[subtree_index].bh; |
| 3481 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3482 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3483 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3484 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3485 | if (ret) { |
| 3486 | mlog_errno(ret); |
| 3487 | goto out; |
| 3488 | } |
| 3489 | |
| 3490 | for (i = subtree_index + 1; |
| 3491 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3492 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3493 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3494 | if (ret) { |
| 3495 | mlog_errno(ret); |
| 3496 | goto out; |
| 3497 | } |
| 3498 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3499 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3500 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3501 | if (ret) { |
| 3502 | mlog_errno(ret); |
| 3503 | goto out; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | } else { |
| 3508 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3509 | right_rec = &el->l_recs[index + 1]; |
| 3510 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3511 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3512 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3513 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3514 | if (ret) { |
| 3515 | mlog_errno(ret); |
| 3516 | goto out; |
| 3517 | } |
| 3518 | |
| 3519 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3520 | |
| 3521 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3522 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3523 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3524 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3525 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3526 | |
| 3527 | ocfs2_cleanup_merge(el, index); |
| 3528 | |
| 3529 | ret = ocfs2_journal_dirty(handle, bh); |
| 3530 | if (ret) |
| 3531 | mlog_errno(ret); |
| 3532 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3533 | if (right_path) { |
| 3534 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 3535 | if (ret) |
| 3536 | mlog_errno(ret); |
| 3537 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3538 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3539 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3540 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3541 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3542 | if (right_path) |
| 3543 | ocfs2_free_path(right_path); |
| 3544 | return ret; |
| 3545 | } |
| 3546 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3547 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3548 | struct ocfs2_path *right_path, |
| 3549 | struct ocfs2_path **ret_left_path) |
| 3550 | { |
| 3551 | int ret; |
| 3552 | u32 left_cpos; |
| 3553 | struct ocfs2_path *left_path = NULL; |
| 3554 | |
| 3555 | *ret_left_path = NULL; |
| 3556 | |
| 3557 | /* This function shouldn't be called for non-trees. */ |
| 3558 | BUG_ON(right_path->p_tree_depth == 0); |
| 3559 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3560 | 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] | 3561 | right_path, &left_cpos); |
| 3562 | if (ret) { |
| 3563 | mlog_errno(ret); |
| 3564 | goto out; |
| 3565 | } |
| 3566 | |
| 3567 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3568 | BUG_ON(left_cpos == 0); |
| 3569 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3570 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3571 | if (!left_path) { |
| 3572 | ret = -ENOMEM; |
| 3573 | mlog_errno(ret); |
| 3574 | goto out; |
| 3575 | } |
| 3576 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3577 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3578 | if (ret) { |
| 3579 | mlog_errno(ret); |
| 3580 | goto out; |
| 3581 | } |
| 3582 | |
| 3583 | *ret_left_path = left_path; |
| 3584 | out: |
| 3585 | if (ret) |
| 3586 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3587 | return ret; |
| 3588 | } |
| 3589 | |
| 3590 | /* |
| 3591 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3592 | * onto the tail of the record "before" it. |
| 3593 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3594 | * |
| 3595 | * For index == 0, the "before" means the last record of the previous |
| 3596 | * extent block. And there is also a situation that we may need to |
| 3597 | * remove the rightmost leaf extent block in the right_path and change |
| 3598 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3599 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3600 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3601 | handle_t *handle, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3602 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3603 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3604 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3605 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3606 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3607 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3608 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3609 | struct ocfs2_extent_rec *left_rec; |
| 3610 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3611 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3612 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3613 | struct buffer_head *root_bh = NULL; |
| 3614 | struct ocfs2_path *left_path = NULL; |
| 3615 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3616 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3617 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3618 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3619 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3620 | if (index == 0) { |
| 3621 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3622 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3623 | if (ret) { |
| 3624 | mlog_errno(ret); |
| 3625 | goto out; |
| 3626 | } |
| 3627 | |
| 3628 | left_el = path_leaf_el(left_path); |
| 3629 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3630 | le16_to_cpu(left_el->l_count)); |
| 3631 | |
| 3632 | left_rec = &left_el->l_recs[ |
| 3633 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3634 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3635 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3636 | le32_to_cpu(split_rec->e_cpos)); |
| 3637 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3638 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3639 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3640 | |
| 3641 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3642 | handle->h_buffer_credits, |
| 3643 | left_path); |
| 3644 | if (ret) { |
| 3645 | mlog_errno(ret); |
| 3646 | goto out; |
| 3647 | } |
| 3648 | |
| 3649 | root_bh = left_path->p_node[subtree_index].bh; |
| 3650 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3651 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3652 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3653 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3654 | if (ret) { |
| 3655 | mlog_errno(ret); |
| 3656 | goto out; |
| 3657 | } |
| 3658 | |
| 3659 | for (i = subtree_index + 1; |
| 3660 | i < path_num_items(right_path); i++) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3661 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3662 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3663 | if (ret) { |
| 3664 | mlog_errno(ret); |
| 3665 | goto out; |
| 3666 | } |
| 3667 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3668 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3669 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3670 | if (ret) { |
| 3671 | mlog_errno(ret); |
| 3672 | goto out; |
| 3673 | } |
| 3674 | } |
| 3675 | } else { |
| 3676 | left_rec = &el->l_recs[index - 1]; |
| 3677 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3678 | has_empty_extent = 1; |
| 3679 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3680 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3681 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3682 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3683 | if (ret) { |
| 3684 | mlog_errno(ret); |
| 3685 | goto out; |
| 3686 | } |
| 3687 | |
| 3688 | if (has_empty_extent && index == 1) { |
| 3689 | /* |
| 3690 | * The easy case - we can just plop the record right in. |
| 3691 | */ |
| 3692 | *left_rec = *split_rec; |
| 3693 | |
| 3694 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3695 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3696 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3697 | |
| 3698 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3699 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3700 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3701 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3702 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3703 | |
| 3704 | ocfs2_cleanup_merge(el, index); |
| 3705 | |
| 3706 | ret = ocfs2_journal_dirty(handle, bh); |
| 3707 | if (ret) |
| 3708 | mlog_errno(ret); |
| 3709 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3710 | if (left_path) { |
| 3711 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 3712 | if (ret) |
| 3713 | mlog_errno(ret); |
| 3714 | |
| 3715 | /* |
| 3716 | * In the situation that the right_rec is empty and the extent |
| 3717 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3718 | * it and we need to delete the right extent block. |
| 3719 | */ |
| 3720 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3721 | le16_to_cpu(el->l_next_free_rec) == 1) { |
| 3722 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3723 | ret = ocfs2_remove_rightmost_path(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3724 | right_path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3725 | dealloc); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3726 | if (ret) { |
| 3727 | mlog_errno(ret); |
| 3728 | goto out; |
| 3729 | } |
| 3730 | |
| 3731 | /* Now the rightmost extent block has been deleted. |
| 3732 | * So we use the new rightmost path. |
| 3733 | */ |
| 3734 | ocfs2_mv_path(right_path, left_path); |
| 3735 | left_path = NULL; |
| 3736 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3737 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3738 | right_path, subtree_index); |
| 3739 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3740 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3741 | if (left_path) |
| 3742 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3743 | return ret; |
| 3744 | } |
| 3745 | |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3746 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
| 3747 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3748 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3749 | int split_index, |
| 3750 | struct ocfs2_extent_rec *split_rec, |
| 3751 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3752 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3753 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3754 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3755 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3756 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3757 | |
| 3758 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3759 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3760 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 3761 | /* |
| 3762 | * The merge code will need to create an empty |
| 3763 | * extent to take the place of the newly |
| 3764 | * emptied slot. Remove any pre-existing empty |
| 3765 | * extents - having more than one in a leaf is |
| 3766 | * illegal. |
| 3767 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3768 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3769 | if (ret) { |
| 3770 | mlog_errno(ret); |
| 3771 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3772 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3773 | split_index--; |
| 3774 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3778 | /* |
| 3779 | * Left-right contig implies this. |
| 3780 | */ |
| 3781 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3782 | |
| 3783 | /* |
| 3784 | * Since the leftright insert always covers the entire |
| 3785 | * extent, this call will delete the insert record |
| 3786 | * entirely, resulting in an empty extent record added to |
| 3787 | * the extent block. |
| 3788 | * |
| 3789 | * Since the adding of an empty extent shifts |
| 3790 | * everything back to the right, there's no need to |
| 3791 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3792 | * |
| 3793 | * When the split_index is zero, we need to merge it to the |
| 3794 | * prevoius extent block. It is more efficient and easier |
| 3795 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3796 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3797 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3798 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3799 | if (ret) { |
| 3800 | mlog_errno(ret); |
| 3801 | goto out; |
| 3802 | } |
| 3803 | |
| 3804 | /* |
| 3805 | * We can only get this from logic error above. |
| 3806 | */ |
| 3807 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3808 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3809 | /* The merge left us with an empty extent, remove it. */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3810 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3811 | if (ret) { |
| 3812 | mlog_errno(ret); |
| 3813 | goto out; |
| 3814 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3815 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3816 | rec = &el->l_recs[split_index]; |
| 3817 | |
| 3818 | /* |
| 3819 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3820 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3821 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3822 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
| 3823 | dealloc, split_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3824 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3825 | if (ret) { |
| 3826 | mlog_errno(ret); |
| 3827 | goto out; |
| 3828 | } |
| 3829 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3830 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3831 | /* |
| 3832 | * Error from this last rotate is not critical, so |
| 3833 | * print but don't bubble it up. |
| 3834 | */ |
| 3835 | if (ret) |
| 3836 | mlog_errno(ret); |
| 3837 | ret = 0; |
| 3838 | } else { |
| 3839 | /* |
| 3840 | * Merge a record to the left or right. |
| 3841 | * |
| 3842 | * 'contig_type' is relative to the existing record, |
| 3843 | * so for example, if we're "right contig", it's to |
| 3844 | * the record on the left (hence the left merge). |
| 3845 | */ |
| 3846 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3847 | ret = ocfs2_merge_rec_left(path, handle, et, |
| 3848 | split_rec, dealloc, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3849 | split_index); |
| 3850 | if (ret) { |
| 3851 | mlog_errno(ret); |
| 3852 | goto out; |
| 3853 | } |
| 3854 | } else { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3855 | ret = ocfs2_merge_rec_right(path, handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3856 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3857 | split_index); |
| 3858 | if (ret) { |
| 3859 | mlog_errno(ret); |
| 3860 | goto out; |
| 3861 | } |
| 3862 | } |
| 3863 | |
| 3864 | if (ctxt->c_split_covers_rec) { |
| 3865 | /* |
| 3866 | * The merge may have left an empty extent in |
| 3867 | * our leaf. Try to rotate it away. |
| 3868 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3869 | ret = ocfs2_rotate_tree_left(handle, et, path, |
| 3870 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3871 | if (ret) |
| 3872 | mlog_errno(ret); |
| 3873 | ret = 0; |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | out: |
| 3878 | return ret; |
| 3879 | } |
| 3880 | |
| 3881 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3882 | enum ocfs2_split_type split, |
| 3883 | struct ocfs2_extent_rec *rec, |
| 3884 | struct ocfs2_extent_rec *split_rec) |
| 3885 | { |
| 3886 | u64 len_blocks; |
| 3887 | |
| 3888 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3889 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3890 | |
| 3891 | if (split == SPLIT_LEFT) { |
| 3892 | /* |
| 3893 | * Region is on the left edge of the existing |
| 3894 | * record. |
| 3895 | */ |
| 3896 | le32_add_cpu(&rec->e_cpos, |
| 3897 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3898 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3899 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3900 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3901 | } else { |
| 3902 | /* |
| 3903 | * Region is on the right edge of the existing |
| 3904 | * record. |
| 3905 | */ |
| 3906 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3907 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3908 | } |
| 3909 | } |
| 3910 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3911 | /* |
| 3912 | * Do the final bits of extent record insertion at the target leaf |
| 3913 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3914 | * that the tree above has been prepared. |
| 3915 | */ |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3916 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et, |
| 3917 | struct ocfs2_extent_rec *insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3918 | struct ocfs2_extent_list *el, |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3919 | struct ocfs2_insert_type *insert) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3920 | { |
| 3921 | int i = insert->ins_contig_index; |
| 3922 | unsigned int range; |
| 3923 | struct ocfs2_extent_rec *rec; |
| 3924 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3925 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3926 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3927 | if (insert->ins_split != SPLIT_NONE) { |
| 3928 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3929 | BUG_ON(i == -1); |
| 3930 | rec = &el->l_recs[i]; |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3931 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3932 | insert->ins_split, rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3933 | insert_rec); |
| 3934 | goto rotate; |
| 3935 | } |
| 3936 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3937 | /* |
| 3938 | * Contiguous insert - either left or right. |
| 3939 | */ |
| 3940 | if (insert->ins_contig != CONTIG_NONE) { |
| 3941 | rec = &el->l_recs[i]; |
| 3942 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3943 | rec->e_blkno = insert_rec->e_blkno; |
| 3944 | rec->e_cpos = insert_rec->e_cpos; |
| 3945 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3946 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3947 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3948 | return; |
| 3949 | } |
| 3950 | |
| 3951 | /* |
| 3952 | * Handle insert into an empty leaf. |
| 3953 | */ |
| 3954 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3955 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3956 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3957 | el->l_recs[0] = *insert_rec; |
| 3958 | el->l_next_free_rec = cpu_to_le16(1); |
| 3959 | return; |
| 3960 | } |
| 3961 | |
| 3962 | /* |
| 3963 | * Appending insert. |
| 3964 | */ |
| 3965 | if (insert->ins_appending == APPEND_TAIL) { |
| 3966 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3967 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3968 | range = le32_to_cpu(rec->e_cpos) |
| 3969 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3970 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3971 | |
| 3972 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3973 | le16_to_cpu(el->l_count), |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3974 | "owner %llu, depth %u, count %u, next free %u, " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3975 | "rec.cpos %u, rec.clusters %u, " |
| 3976 | "insert.cpos %u, insert.clusters %u\n", |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3977 | ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3978 | le16_to_cpu(el->l_tree_depth), |
| 3979 | le16_to_cpu(el->l_count), |
| 3980 | le16_to_cpu(el->l_next_free_rec), |
| 3981 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3982 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3983 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3984 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3985 | i++; |
| 3986 | el->l_recs[i] = *insert_rec; |
| 3987 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3988 | return; |
| 3989 | } |
| 3990 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3991 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3992 | /* |
| 3993 | * Ok, we have to rotate. |
| 3994 | * |
| 3995 | * At this point, it is safe to assume that inserting into an |
| 3996 | * empty leaf and appending to a leaf have both been handled |
| 3997 | * above. |
| 3998 | * |
| 3999 | * This leaf needs to have space, either by the empty 1st |
| 4000 | * extent record, or by virtue of an l_next_rec < l_count. |
| 4001 | */ |
| 4002 | ocfs2_rotate_leaf(el, insert_rec); |
| 4003 | } |
| 4004 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4005 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 4006 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4007 | struct ocfs2_path *path, |
| 4008 | struct ocfs2_extent_rec *insert_rec) |
| 4009 | { |
| 4010 | int ret, i, next_free; |
| 4011 | struct buffer_head *bh; |
| 4012 | struct ocfs2_extent_list *el; |
| 4013 | struct ocfs2_extent_rec *rec; |
| 4014 | |
| 4015 | /* |
| 4016 | * Update everything except the leaf block. |
| 4017 | */ |
| 4018 | for (i = 0; i < path->p_tree_depth; i++) { |
| 4019 | bh = path->p_node[i].bh; |
| 4020 | el = path->p_node[i].el; |
| 4021 | |
| 4022 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 4023 | if (next_free == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4024 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4025 | "Owner %llu has a bad extent list", |
| 4026 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4027 | ret = -EIO; |
| 4028 | return; |
| 4029 | } |
| 4030 | |
| 4031 | rec = &el->l_recs[next_free - 1]; |
| 4032 | |
| 4033 | rec->e_int_clusters = insert_rec->e_cpos; |
| 4034 | le32_add_cpu(&rec->e_int_clusters, |
| 4035 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 4036 | le32_add_cpu(&rec->e_int_clusters, |
| 4037 | -le32_to_cpu(rec->e_cpos)); |
| 4038 | |
| 4039 | ret = ocfs2_journal_dirty(handle, bh); |
| 4040 | if (ret) |
| 4041 | mlog_errno(ret); |
| 4042 | |
| 4043 | } |
| 4044 | } |
| 4045 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4046 | static int ocfs2_append_rec_to_path(handle_t *handle, |
| 4047 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4048 | struct ocfs2_extent_rec *insert_rec, |
| 4049 | struct ocfs2_path *right_path, |
| 4050 | struct ocfs2_path **ret_left_path) |
| 4051 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4052 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4053 | struct ocfs2_extent_list *el; |
| 4054 | struct ocfs2_path *left_path = NULL; |
| 4055 | |
| 4056 | *ret_left_path = NULL; |
| 4057 | |
| 4058 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4059 | * This shouldn't happen for non-trees. The extent rec cluster |
| 4060 | * count manipulation below only works for interior nodes. |
| 4061 | */ |
| 4062 | BUG_ON(right_path->p_tree_depth == 0); |
| 4063 | |
| 4064 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4065 | * If our appending insert is at the leftmost edge of a leaf, |
| 4066 | * then we might need to update the rightmost records of the |
| 4067 | * neighboring path. |
| 4068 | */ |
| 4069 | el = path_leaf_el(right_path); |
| 4070 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 4071 | if (next_free == 0 || |
| 4072 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 4073 | u32 left_cpos; |
| 4074 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4075 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4076 | right_path, &left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4077 | if (ret) { |
| 4078 | mlog_errno(ret); |
| 4079 | goto out; |
| 4080 | } |
| 4081 | |
| 4082 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 4083 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 4084 | left_cpos); |
| 4085 | |
| 4086 | /* |
| 4087 | * No need to worry if the append is already in the |
| 4088 | * leftmost leaf. |
| 4089 | */ |
| 4090 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4091 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4092 | if (!left_path) { |
| 4093 | ret = -ENOMEM; |
| 4094 | mlog_errno(ret); |
| 4095 | goto out; |
| 4096 | } |
| 4097 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4098 | ret = ocfs2_find_path(et->et_ci, left_path, |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4099 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4100 | if (ret) { |
| 4101 | mlog_errno(ret); |
| 4102 | goto out; |
| 4103 | } |
| 4104 | |
| 4105 | /* |
| 4106 | * ocfs2_insert_path() will pass the left_path to the |
| 4107 | * journal for us. |
| 4108 | */ |
| 4109 | } |
| 4110 | } |
| 4111 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4112 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4113 | if (ret) { |
| 4114 | mlog_errno(ret); |
| 4115 | goto out; |
| 4116 | } |
| 4117 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4118 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4119 | |
| 4120 | *ret_left_path = left_path; |
| 4121 | ret = 0; |
| 4122 | out: |
| 4123 | if (ret != 0) |
| 4124 | ocfs2_free_path(left_path); |
| 4125 | |
| 4126 | return ret; |
| 4127 | } |
| 4128 | |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4129 | static void ocfs2_split_record(struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4130 | struct ocfs2_path *left_path, |
| 4131 | struct ocfs2_path *right_path, |
| 4132 | struct ocfs2_extent_rec *split_rec, |
| 4133 | enum ocfs2_split_type split) |
| 4134 | { |
| 4135 | int index; |
| 4136 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4137 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4138 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4139 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4140 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4141 | if (left_path) |
| 4142 | left_el = path_leaf_el(left_path); |
| 4143 | |
| 4144 | el = right_el; |
| 4145 | insert_el = right_el; |
| 4146 | index = ocfs2_search_extent_list(el, cpos); |
| 4147 | if (index != -1) { |
| 4148 | if (index == 0 && left_path) { |
| 4149 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4150 | |
| 4151 | /* |
| 4152 | * This typically means that the record |
| 4153 | * started in the left path but moved to the |
| 4154 | * right as a result of rotation. We either |
| 4155 | * move the existing record to the left, or we |
| 4156 | * do the later insert there. |
| 4157 | * |
| 4158 | * In this case, the left path should always |
| 4159 | * exist as the rotate code will have passed |
| 4160 | * it back for a post-insert update. |
| 4161 | */ |
| 4162 | |
| 4163 | if (split == SPLIT_LEFT) { |
| 4164 | /* |
| 4165 | * It's a left split. Since we know |
| 4166 | * that the rotate code gave us an |
| 4167 | * empty extent in the left path, we |
| 4168 | * can just do the insert there. |
| 4169 | */ |
| 4170 | insert_el = left_el; |
| 4171 | } else { |
| 4172 | /* |
| 4173 | * Right split - we have to move the |
| 4174 | * existing record over to the left |
| 4175 | * leaf. The insert will be into the |
| 4176 | * newly created empty extent in the |
| 4177 | * right leaf. |
| 4178 | */ |
| 4179 | tmprec = &right_el->l_recs[index]; |
| 4180 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4181 | el = left_el; |
| 4182 | |
| 4183 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4184 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4185 | BUG_ON(index == -1); |
| 4186 | } |
| 4187 | } |
| 4188 | } else { |
| 4189 | BUG_ON(!left_path); |
| 4190 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4191 | /* |
| 4192 | * Left path is easy - we can just allow the insert to |
| 4193 | * happen. |
| 4194 | */ |
| 4195 | el = left_el; |
| 4196 | insert_el = left_el; |
| 4197 | index = ocfs2_search_extent_list(el, cpos); |
| 4198 | BUG_ON(index == -1); |
| 4199 | } |
| 4200 | |
| 4201 | rec = &el->l_recs[index]; |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4202 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4203 | split, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4204 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4205 | } |
| 4206 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4207 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4208 | * This function only does inserts on an allocation b-tree. For tree |
| 4209 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4210 | * |
| 4211 | * right_path is the path we want to do the actual insert |
| 4212 | * in. left_path should only be passed in if we need to update that |
| 4213 | * portion of the tree after an edge insert. |
| 4214 | */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4215 | static int ocfs2_insert_path(handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4216 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4217 | struct ocfs2_path *left_path, |
| 4218 | struct ocfs2_path *right_path, |
| 4219 | struct ocfs2_extent_rec *insert_rec, |
| 4220 | struct ocfs2_insert_type *insert) |
| 4221 | { |
| 4222 | int ret, subtree_index; |
| 4223 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4224 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4225 | if (left_path) { |
| 4226 | int credits = handle->h_buffer_credits; |
| 4227 | |
| 4228 | /* |
| 4229 | * There's a chance that left_path got passed back to |
| 4230 | * us without being accounted for in the |
| 4231 | * journal. Extend our transaction here to be sure we |
| 4232 | * can change those blocks. |
| 4233 | */ |
| 4234 | credits += left_path->p_tree_depth; |
| 4235 | |
| 4236 | ret = ocfs2_extend_trans(handle, credits); |
| 4237 | if (ret < 0) { |
| 4238 | mlog_errno(ret); |
| 4239 | goto out; |
| 4240 | } |
| 4241 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4242 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4243 | if (ret < 0) { |
| 4244 | mlog_errno(ret); |
| 4245 | goto out; |
| 4246 | } |
| 4247 | } |
| 4248 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4249 | /* |
| 4250 | * Pass both paths to the journal. The majority of inserts |
| 4251 | * will be touching all components anyway. |
| 4252 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4253 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4254 | if (ret < 0) { |
| 4255 | mlog_errno(ret); |
| 4256 | goto out; |
| 4257 | } |
| 4258 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4259 | if (insert->ins_split != SPLIT_NONE) { |
| 4260 | /* |
| 4261 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4262 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4263 | * function sort it all out. |
| 4264 | */ |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4265 | ocfs2_split_record(et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4266 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4267 | |
| 4268 | /* |
| 4269 | * Split might have modified either leaf and we don't |
| 4270 | * have a guarantee that the later edge insert will |
| 4271 | * dirty this for us. |
| 4272 | */ |
| 4273 | if (left_path) |
| 4274 | ret = ocfs2_journal_dirty(handle, |
| 4275 | path_leaf_bh(left_path)); |
| 4276 | if (ret) |
| 4277 | mlog_errno(ret); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4278 | } else |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4279 | ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path), |
| 4280 | insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4281 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4282 | ret = ocfs2_journal_dirty(handle, leaf_bh); |
| 4283 | if (ret) |
| 4284 | mlog_errno(ret); |
| 4285 | |
| 4286 | if (left_path) { |
| 4287 | /* |
| 4288 | * The rotate code has indicated that we need to fix |
| 4289 | * up portions of the tree after the insert. |
| 4290 | * |
| 4291 | * XXX: Should we extend the transaction here? |
| 4292 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4293 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4294 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4295 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4296 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | ret = 0; |
| 4300 | out: |
| 4301 | return ret; |
| 4302 | } |
| 4303 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4304 | static int ocfs2_do_insert_extent(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4305 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4306 | struct ocfs2_extent_rec *insert_rec, |
| 4307 | struct ocfs2_insert_type *type) |
| 4308 | { |
| 4309 | int ret, rotate = 0; |
| 4310 | u32 cpos; |
| 4311 | struct ocfs2_path *right_path = NULL; |
| 4312 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4313 | struct ocfs2_extent_list *el; |
| 4314 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4315 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4316 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4317 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4318 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4319 | if (ret) { |
| 4320 | mlog_errno(ret); |
| 4321 | goto out; |
| 4322 | } |
| 4323 | |
| 4324 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4325 | ocfs2_insert_at_leaf(et, insert_rec, el, type); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4326 | goto out_update_clusters; |
| 4327 | } |
| 4328 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4329 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4330 | if (!right_path) { |
| 4331 | ret = -ENOMEM; |
| 4332 | mlog_errno(ret); |
| 4333 | goto out; |
| 4334 | } |
| 4335 | |
| 4336 | /* |
| 4337 | * Determine the path to start with. Rotations need the |
| 4338 | * rightmost path, everything else can go directly to the |
| 4339 | * target leaf. |
| 4340 | */ |
| 4341 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4342 | if (type->ins_appending == APPEND_NONE && |
| 4343 | type->ins_contig == CONTIG_NONE) { |
| 4344 | rotate = 1; |
| 4345 | cpos = UINT_MAX; |
| 4346 | } |
| 4347 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4348 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4349 | if (ret) { |
| 4350 | mlog_errno(ret); |
| 4351 | goto out; |
| 4352 | } |
| 4353 | |
| 4354 | /* |
| 4355 | * Rotations and appends need special treatment - they modify |
| 4356 | * parts of the tree's above them. |
| 4357 | * |
| 4358 | * Both might pass back a path immediate to the left of the |
| 4359 | * one being inserted to. This will be cause |
| 4360 | * ocfs2_insert_path() to modify the rightmost records of |
| 4361 | * left_path to account for an edge insert. |
| 4362 | * |
| 4363 | * XXX: When modifying this code, keep in mind that an insert |
| 4364 | * can wind up skipping both of these two special cases... |
| 4365 | */ |
| 4366 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4367 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4368 | le32_to_cpu(insert_rec->e_cpos), |
| 4369 | right_path, &left_path); |
| 4370 | if (ret) { |
| 4371 | mlog_errno(ret); |
| 4372 | goto out; |
| 4373 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4374 | |
| 4375 | /* |
| 4376 | * ocfs2_rotate_tree_right() might have extended the |
| 4377 | * transaction without re-journaling our tree root. |
| 4378 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4379 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4380 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4381 | if (ret) { |
| 4382 | mlog_errno(ret); |
| 4383 | goto out; |
| 4384 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4385 | } else if (type->ins_appending == APPEND_TAIL |
| 4386 | && type->ins_contig != CONTIG_LEFT) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4387 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4388 | right_path, &left_path); |
| 4389 | if (ret) { |
| 4390 | mlog_errno(ret); |
| 4391 | goto out; |
| 4392 | } |
| 4393 | } |
| 4394 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4395 | ret = ocfs2_insert_path(handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4396 | insert_rec, type); |
| 4397 | if (ret) { |
| 4398 | mlog_errno(ret); |
| 4399 | goto out; |
| 4400 | } |
| 4401 | |
| 4402 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4403 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4404 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4405 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4406 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4407 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4408 | if (ret) |
| 4409 | mlog_errno(ret); |
| 4410 | |
| 4411 | out: |
| 4412 | ocfs2_free_path(left_path); |
| 4413 | ocfs2_free_path(right_path); |
| 4414 | |
| 4415 | return ret; |
| 4416 | } |
| 4417 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4418 | static enum ocfs2_contig_type |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4419 | ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et, |
| 4420 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4421 | struct ocfs2_extent_list *el, int index, |
| 4422 | struct ocfs2_extent_rec *split_rec) |
| 4423 | { |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4424 | int status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4425 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4426 | u32 left_cpos, right_cpos; |
| 4427 | struct ocfs2_extent_rec *rec = NULL; |
| 4428 | struct ocfs2_extent_list *new_el; |
| 4429 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4430 | struct buffer_head *bh; |
| 4431 | struct ocfs2_extent_block *eb; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4432 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4433 | |
| 4434 | if (index > 0) { |
| 4435 | rec = &el->l_recs[index - 1]; |
| 4436 | } else if (path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4437 | status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4438 | if (status) |
| 4439 | goto out; |
| 4440 | |
| 4441 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4442 | left_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4443 | if (!left_path) |
| 4444 | goto out; |
| 4445 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4446 | status = ocfs2_find_path(et->et_ci, left_path, |
| 4447 | left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4448 | if (status) |
| 4449 | goto out; |
| 4450 | |
| 4451 | new_el = path_leaf_el(left_path); |
| 4452 | |
| 4453 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4454 | le16_to_cpu(new_el->l_count)) { |
| 4455 | bh = path_leaf_bh(left_path); |
| 4456 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4457 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4458 | "Extent block #%llu has an " |
| 4459 | "invalid l_next_free_rec of " |
| 4460 | "%d. It should have " |
| 4461 | "matched the l_count of %d", |
| 4462 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4463 | le16_to_cpu(new_el->l_next_free_rec), |
| 4464 | le16_to_cpu(new_el->l_count)); |
| 4465 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4466 | goto out; |
| 4467 | } |
| 4468 | rec = &new_el->l_recs[ |
| 4469 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4470 | } |
| 4471 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4472 | |
| 4473 | /* |
| 4474 | * We're careful to check for an empty extent record here - |
| 4475 | * the merge code will know what to do if it sees one. |
| 4476 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4477 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4478 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4479 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4480 | ret = CONTIG_RIGHT; |
| 4481 | } else { |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4482 | ret = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4483 | } |
| 4484 | } |
| 4485 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4486 | rec = NULL; |
| 4487 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4488 | rec = &el->l_recs[index + 1]; |
| 4489 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4490 | path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4491 | status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4492 | if (status) |
| 4493 | goto out; |
| 4494 | |
| 4495 | if (right_cpos == 0) |
| 4496 | goto out; |
| 4497 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4498 | right_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4499 | if (!right_path) |
| 4500 | goto out; |
| 4501 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4502 | status = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4503 | if (status) |
| 4504 | goto out; |
| 4505 | |
| 4506 | new_el = path_leaf_el(right_path); |
| 4507 | rec = &new_el->l_recs[0]; |
| 4508 | if (ocfs2_is_empty_extent(rec)) { |
| 4509 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4510 | bh = path_leaf_bh(right_path); |
| 4511 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4512 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4513 | "Extent block #%llu has an " |
| 4514 | "invalid l_next_free_rec of %d", |
| 4515 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4516 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4517 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4518 | goto out; |
| 4519 | } |
| 4520 | rec = &new_el->l_recs[1]; |
| 4521 | } |
| 4522 | } |
| 4523 | |
| 4524 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4525 | enum ocfs2_contig_type contig_type; |
| 4526 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4527 | contig_type = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4528 | |
| 4529 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4530 | ret = CONTIG_LEFTRIGHT; |
| 4531 | else if (ret == CONTIG_NONE) |
| 4532 | ret = contig_type; |
| 4533 | } |
| 4534 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4535 | out: |
| 4536 | if (left_path) |
| 4537 | ocfs2_free_path(left_path); |
| 4538 | if (right_path) |
| 4539 | ocfs2_free_path(right_path); |
| 4540 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4541 | return ret; |
| 4542 | } |
| 4543 | |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4544 | static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4545 | struct ocfs2_insert_type *insert, |
| 4546 | struct ocfs2_extent_list *el, |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4547 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4548 | { |
| 4549 | int i; |
| 4550 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4551 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4552 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4553 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4554 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4555 | contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i], |
| 4556 | insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4557 | if (contig_type != CONTIG_NONE) { |
| 4558 | insert->ins_contig_index = i; |
| 4559 | break; |
| 4560 | } |
| 4561 | } |
| 4562 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4563 | |
| 4564 | if (insert->ins_contig != CONTIG_NONE) { |
| 4565 | struct ocfs2_extent_rec *rec = |
| 4566 | &el->l_recs[insert->ins_contig_index]; |
| 4567 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4568 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4569 | |
| 4570 | /* |
| 4571 | * Caller might want us to limit the size of extents, don't |
| 4572 | * calculate contiguousness if we might exceed that limit. |
| 4573 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4574 | if (et->et_max_leaf_clusters && |
| 4575 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4576 | insert->ins_contig = CONTIG_NONE; |
| 4577 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | /* |
| 4581 | * This should only be called against the righmost leaf extent list. |
| 4582 | * |
| 4583 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4584 | * insert at the tail of the rightmost leaf. |
| 4585 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4586 | * This should also work against the root extent list for tree's with 0 |
| 4587 | * 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] | 4588 | * then the logic here makes sense. |
| 4589 | */ |
| 4590 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4591 | struct ocfs2_extent_list *el, |
| 4592 | struct ocfs2_extent_rec *insert_rec) |
| 4593 | { |
| 4594 | int i; |
| 4595 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4596 | struct ocfs2_extent_rec *rec; |
| 4597 | |
| 4598 | insert->ins_appending = APPEND_NONE; |
| 4599 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4600 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4601 | |
| 4602 | if (!el->l_next_free_rec) |
| 4603 | goto set_tail_append; |
| 4604 | |
| 4605 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4606 | /* Were all records empty? */ |
| 4607 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4608 | goto set_tail_append; |
| 4609 | } |
| 4610 | |
| 4611 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4612 | rec = &el->l_recs[i]; |
| 4613 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4614 | if (cpos >= |
| 4615 | (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] | 4616 | goto set_tail_append; |
| 4617 | |
| 4618 | return; |
| 4619 | |
| 4620 | set_tail_append: |
| 4621 | insert->ins_appending = APPEND_TAIL; |
| 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * Helper function called at the begining of an insert. |
| 4626 | * |
| 4627 | * This computes a few things that are commonly used in the process of |
| 4628 | * inserting into the btree: |
| 4629 | * - Whether the new extent is contiguous with an existing one. |
| 4630 | * - The current tree depth. |
| 4631 | * - Whether the insert is an appending one. |
| 4632 | * - The total # of free records in the tree. |
| 4633 | * |
| 4634 | * All of the information is stored on the ocfs2_insert_type |
| 4635 | * structure. |
| 4636 | */ |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4637 | static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4638 | struct buffer_head **last_eb_bh, |
| 4639 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4640 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4641 | struct ocfs2_insert_type *insert) |
| 4642 | { |
| 4643 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4644 | struct ocfs2_extent_block *eb; |
| 4645 | struct ocfs2_extent_list *el; |
| 4646 | struct ocfs2_path *path = NULL; |
| 4647 | struct buffer_head *bh = NULL; |
| 4648 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4649 | insert->ins_split = SPLIT_NONE; |
| 4650 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4651 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4652 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4653 | |
| 4654 | if (el->l_tree_depth) { |
| 4655 | /* |
| 4656 | * If we have tree depth, we read in the |
| 4657 | * rightmost extent block ahead of time as |
| 4658 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4659 | * may want it later. |
| 4660 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4661 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4662 | ocfs2_et_get_last_eb_blk(et), |
| 4663 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4664 | if (ret) { |
| 4665 | mlog_exit(ret); |
| 4666 | goto out; |
| 4667 | } |
| 4668 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4669 | el = &eb->h_list; |
| 4670 | } |
| 4671 | |
| 4672 | /* |
| 4673 | * Unless we have a contiguous insert, we'll need to know if |
| 4674 | * there is room left in our allocation tree for another |
| 4675 | * extent record. |
| 4676 | * |
| 4677 | * XXX: This test is simplistic, we can search for empty |
| 4678 | * extent records too. |
| 4679 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4680 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4681 | le16_to_cpu(el->l_next_free_rec); |
| 4682 | |
| 4683 | if (!insert->ins_tree_depth) { |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4684 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4685 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4686 | return 0; |
| 4687 | } |
| 4688 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4689 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4690 | if (!path) { |
| 4691 | ret = -ENOMEM; |
| 4692 | mlog_errno(ret); |
| 4693 | goto out; |
| 4694 | } |
| 4695 | |
| 4696 | /* |
| 4697 | * In the case that we're inserting past what the tree |
| 4698 | * currently accounts for, ocfs2_find_path() will return for |
| 4699 | * us the rightmost tree path. This is accounted for below in |
| 4700 | * the appending code. |
| 4701 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4702 | 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] | 4703 | if (ret) { |
| 4704 | mlog_errno(ret); |
| 4705 | goto out; |
| 4706 | } |
| 4707 | |
| 4708 | el = path_leaf_el(path); |
| 4709 | |
| 4710 | /* |
| 4711 | * Now that we have the path, there's two things we want to determine: |
| 4712 | * 1) Contiguousness (also set contig_index if this is so) |
| 4713 | * |
| 4714 | * 2) Are we doing an append? We can trivially break this up |
| 4715 | * into two types of appends: simple record append, or a |
| 4716 | * rotate inside the tail leaf. |
| 4717 | */ |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4718 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4719 | |
| 4720 | /* |
| 4721 | * The insert code isn't quite ready to deal with all cases of |
| 4722 | * left contiguousness. Specifically, if it's an insert into |
| 4723 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4724 | * 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] | 4725 | * left. For now, just catch that case and fool the layers |
| 4726 | * above us. This works just fine for tree_depth == 0, which |
| 4727 | * is why we allow that above. |
| 4728 | */ |
| 4729 | if (insert->ins_contig == CONTIG_LEFT && |
| 4730 | insert->ins_contig_index == 0) |
| 4731 | insert->ins_contig = CONTIG_NONE; |
| 4732 | |
| 4733 | /* |
| 4734 | * Ok, so we can simply compare against last_eb to figure out |
| 4735 | * whether the path doesn't exist. This will only happen in |
| 4736 | * the case that we're doing a tail append, so maybe we can |
| 4737 | * take advantage of that information somehow. |
| 4738 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4739 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4740 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4741 | /* |
| 4742 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4743 | * tree path. This might be an appending insert. There are |
| 4744 | * two cases: |
| 4745 | * 1) We're doing a true append at the tail: |
| 4746 | * -This might even be off the end of the leaf |
| 4747 | * 2) We're "appending" by rotating in the tail |
| 4748 | */ |
| 4749 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4750 | } |
| 4751 | |
| 4752 | out: |
| 4753 | ocfs2_free_path(path); |
| 4754 | |
| 4755 | if (ret == 0) |
| 4756 | *last_eb_bh = bh; |
| 4757 | else |
| 4758 | brelse(bh); |
| 4759 | return ret; |
| 4760 | } |
| 4761 | |
| 4762 | /* |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4763 | * Insert an extent into a btree. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4764 | * |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4765 | * The caller needs to update the owning btree's cluster count. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4766 | */ |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4767 | int ocfs2_insert_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4768 | struct ocfs2_extent_tree *et, |
| 4769 | u32 cpos, |
| 4770 | u64 start_blk, |
| 4771 | u32 new_clusters, |
| 4772 | u8 flags, |
| 4773 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4774 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4775 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4776 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4777 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4778 | struct ocfs2_insert_type insert = {0, }; |
| 4779 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4780 | |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4781 | mlog(0, "add %u clusters at position %u to owner %llu\n", |
| 4782 | new_clusters, cpos, |
| 4783 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4784 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4785 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4786 | rec.e_cpos = cpu_to_le32(cpos); |
| 4787 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4788 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4789 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4790 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4791 | if (status) { |
| 4792 | mlog_errno(status); |
| 4793 | goto bail; |
| 4794 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4795 | |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4796 | status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4797 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4798 | if (status < 0) { |
| 4799 | mlog_errno(status); |
| 4800 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4801 | } |
| 4802 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4803 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 4804 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 4805 | "Insert.tree_depth: %d\n", |
| 4806 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4807 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4808 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4809 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4810 | status = ocfs2_grow_tree(handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4811 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4812 | meta_ac); |
| 4813 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4814 | mlog_errno(status); |
| 4815 | goto bail; |
| 4816 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4817 | } |
| 4818 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4819 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4820 | status = ocfs2_do_insert_extent(handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4821 | if (status < 0) |
| 4822 | mlog_errno(status); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 4823 | else |
| 4824 | ocfs2_et_extent_map_insert(et, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4825 | |
| 4826 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4827 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4828 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4829 | mlog_exit(status); |
| 4830 | return status; |
| 4831 | } |
| 4832 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4833 | /* |
| 4834 | * Allcate and add clusters into the extent b-tree. |
| 4835 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4836 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4837 | * it is not limited to the file storage. Any extent tree can use this |
| 4838 | * function if it implements the proper ocfs2_extent_tree. |
| 4839 | */ |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4840 | int ocfs2_add_clusters_in_btree(handle_t *handle, |
| 4841 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4842 | u32 *logical_offset, |
| 4843 | u32 clusters_to_add, |
| 4844 | int mark_unwritten, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4845 | struct ocfs2_alloc_context *data_ac, |
| 4846 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4847 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4848 | { |
| 4849 | int status = 0; |
| 4850 | int free_extents; |
| 4851 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4852 | u32 bit_off, num_bits; |
| 4853 | u64 block; |
| 4854 | u8 flags = 0; |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4855 | struct ocfs2_super *osb = |
| 4856 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4857 | |
| 4858 | BUG_ON(!clusters_to_add); |
| 4859 | |
| 4860 | if (mark_unwritten) |
| 4861 | flags = OCFS2_EXT_UNWRITTEN; |
| 4862 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4863 | free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4864 | if (free_extents < 0) { |
| 4865 | status = free_extents; |
| 4866 | mlog_errno(status); |
| 4867 | goto leave; |
| 4868 | } |
| 4869 | |
| 4870 | /* there are two cases which could cause us to EAGAIN in the |
| 4871 | * we-need-more-metadata case: |
| 4872 | * 1) we haven't reserved *any* |
| 4873 | * 2) we are so fragmented, we've needed to add metadata too |
| 4874 | * many times. */ |
| 4875 | if (!free_extents && !meta_ac) { |
| 4876 | mlog(0, "we haven't reserved any metadata!\n"); |
| 4877 | status = -EAGAIN; |
| 4878 | reason = RESTART_META; |
| 4879 | goto leave; |
| 4880 | } else if ((!free_extents) |
| 4881 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4882 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4883 | mlog(0, "filesystem is really fragmented...\n"); |
| 4884 | status = -EAGAIN; |
| 4885 | reason = RESTART_META; |
| 4886 | goto leave; |
| 4887 | } |
| 4888 | |
| 4889 | status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 4890 | clusters_to_add, &bit_off, &num_bits); |
| 4891 | if (status < 0) { |
| 4892 | if (status != -ENOSPC) |
| 4893 | mlog_errno(status); |
| 4894 | goto leave; |
| 4895 | } |
| 4896 | |
| 4897 | BUG_ON(num_bits > clusters_to_add); |
| 4898 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4899 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4900 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4901 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4902 | if (status < 0) { |
| 4903 | mlog_errno(status); |
| 4904 | goto leave; |
| 4905 | } |
| 4906 | |
| 4907 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4908 | mlog(0, "Allocating %u clusters at block %u for owner %llu\n", |
| 4909 | num_bits, bit_off, |
| 4910 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4911 | status = ocfs2_insert_extent(handle, et, *logical_offset, block, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4912 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4913 | if (status < 0) { |
| 4914 | mlog_errno(status); |
| 4915 | goto leave; |
| 4916 | } |
| 4917 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4918 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4919 | if (status < 0) { |
| 4920 | mlog_errno(status); |
| 4921 | goto leave; |
| 4922 | } |
| 4923 | |
| 4924 | clusters_to_add -= num_bits; |
| 4925 | *logical_offset += num_bits; |
| 4926 | |
| 4927 | if (clusters_to_add) { |
| 4928 | mlog(0, "need to alloc once more, wanted = %u\n", |
| 4929 | clusters_to_add); |
| 4930 | status = -EAGAIN; |
| 4931 | reason = RESTART_TRANS; |
| 4932 | } |
| 4933 | |
| 4934 | leave: |
| 4935 | mlog_exit(status); |
| 4936 | if (reason_ret) |
| 4937 | *reason_ret = reason; |
| 4938 | return status; |
| 4939 | } |
| 4940 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4941 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4942 | struct ocfs2_extent_rec *split_rec, |
| 4943 | u32 cpos, |
| 4944 | struct ocfs2_extent_rec *rec) |
| 4945 | { |
| 4946 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4947 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4948 | |
| 4949 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4950 | |
| 4951 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4952 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4953 | |
| 4954 | split_rec->e_blkno = rec->e_blkno; |
| 4955 | le64_add_cpu(&split_rec->e_blkno, |
| 4956 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4957 | |
| 4958 | split_rec->e_flags = rec->e_flags; |
| 4959 | } |
| 4960 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4961 | static int ocfs2_split_and_insert(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4962 | struct ocfs2_extent_tree *et, |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4963 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4964 | struct buffer_head **last_eb_bh, |
| 4965 | int split_index, |
| 4966 | struct ocfs2_extent_rec *orig_split_rec, |
| 4967 | struct ocfs2_alloc_context *meta_ac) |
| 4968 | { |
| 4969 | int ret = 0, depth; |
| 4970 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4971 | struct ocfs2_extent_rec tmprec; |
| 4972 | struct ocfs2_extent_list *rightmost_el; |
| 4973 | struct ocfs2_extent_rec rec; |
| 4974 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4975 | struct ocfs2_insert_type insert; |
| 4976 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4977 | |
| 4978 | leftright: |
| 4979 | /* |
| 4980 | * Store a copy of the record on the stack - it might move |
| 4981 | * around as the tree is manipulated below. |
| 4982 | */ |
| 4983 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4984 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4985 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4986 | |
| 4987 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4988 | if (depth) { |
| 4989 | BUG_ON(!(*last_eb_bh)); |
| 4990 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4991 | rightmost_el = &eb->h_list; |
| 4992 | } |
| 4993 | |
| 4994 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4995 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4996 | ret = ocfs2_grow_tree(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4997 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4998 | if (ret) { |
| 4999 | mlog_errno(ret); |
| 5000 | goto out; |
| 5001 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5005 | insert.ins_appending = APPEND_NONE; |
| 5006 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5007 | insert.ins_tree_depth = depth; |
| 5008 | |
| 5009 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 5010 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 5011 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 5012 | le16_to_cpu(rec.e_leaf_clusters); |
| 5013 | |
| 5014 | if (split_rec.e_cpos == rec.e_cpos) { |
| 5015 | insert.ins_split = SPLIT_LEFT; |
| 5016 | } else if (insert_range == rec_range) { |
| 5017 | insert.ins_split = SPLIT_RIGHT; |
| 5018 | } else { |
| 5019 | /* |
| 5020 | * Left/right split. We fake this as a right split |
| 5021 | * first and then make a second pass as a left split. |
| 5022 | */ |
| 5023 | insert.ins_split = SPLIT_RIGHT; |
| 5024 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 5025 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5026 | &tmprec, insert_range, &rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5027 | |
| 5028 | split_rec = tmprec; |
| 5029 | |
| 5030 | BUG_ON(do_leftright); |
| 5031 | do_leftright = 1; |
| 5032 | } |
| 5033 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5034 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5035 | if (ret) { |
| 5036 | mlog_errno(ret); |
| 5037 | goto out; |
| 5038 | } |
| 5039 | |
| 5040 | if (do_leftright == 1) { |
| 5041 | u32 cpos; |
| 5042 | struct ocfs2_extent_list *el; |
| 5043 | |
| 5044 | do_leftright++; |
| 5045 | split_rec = *orig_split_rec; |
| 5046 | |
| 5047 | ocfs2_reinit_path(path, 1); |
| 5048 | |
| 5049 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5050 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5051 | if (ret) { |
| 5052 | mlog_errno(ret); |
| 5053 | goto out; |
| 5054 | } |
| 5055 | |
| 5056 | el = path_leaf_el(path); |
| 5057 | split_index = ocfs2_search_extent_list(el, cpos); |
| 5058 | goto leftright; |
| 5059 | } |
| 5060 | out: |
| 5061 | |
| 5062 | return ret; |
| 5063 | } |
| 5064 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5065 | static int ocfs2_replace_extent_rec(handle_t *handle, |
| 5066 | struct ocfs2_extent_tree *et, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5067 | struct ocfs2_path *path, |
| 5068 | struct ocfs2_extent_list *el, |
| 5069 | int split_index, |
| 5070 | struct ocfs2_extent_rec *split_rec) |
| 5071 | { |
| 5072 | int ret; |
| 5073 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5074 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5075 | path_num_items(path) - 1); |
| 5076 | if (ret) { |
| 5077 | mlog_errno(ret); |
| 5078 | goto out; |
| 5079 | } |
| 5080 | |
| 5081 | el->l_recs[split_index] = *split_rec; |
| 5082 | |
| 5083 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5084 | out: |
| 5085 | return ret; |
| 5086 | } |
| 5087 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5088 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5089 | * Split part or all of the extent record at split_index in the leaf |
| 5090 | * pointed to by path. Merge with the contiguous extent record if needed. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5091 | * |
| 5092 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 5093 | * |
| 5094 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 5095 | * of the tree is required. All other cases will degrade into a less |
| 5096 | * optimal tree layout. |
| 5097 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5098 | * last_eb_bh should be the rightmost leaf block for any extent |
| 5099 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 5100 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5101 | * |
| 5102 | * This code is optimized for readability - several passes might be |
| 5103 | * made over certain portions of the tree. All of those blocks will |
| 5104 | * have been brought into cache (and pinned via the journal), so the |
| 5105 | * extra overhead is not expressed in terms of disk reads. |
| 5106 | */ |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5107 | static int __ocfs2_split_extent(handle_t *handle, |
| 5108 | struct ocfs2_extent_tree *et, |
| 5109 | struct ocfs2_path *path, |
| 5110 | int split_index, |
| 5111 | struct ocfs2_extent_rec *split_rec, |
| 5112 | struct ocfs2_alloc_context *meta_ac, |
| 5113 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5114 | { |
| 5115 | int ret = 0; |
| 5116 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 5117 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5118 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 5119 | struct ocfs2_merge_ctxt ctxt; |
| 5120 | struct ocfs2_extent_list *rightmost_el; |
| 5121 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5122 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5123 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5124 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5125 | ret = -EIO; |
| 5126 | mlog_errno(ret); |
| 5127 | goto out; |
| 5128 | } |
| 5129 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 5130 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5131 | split_index, |
| 5132 | split_rec); |
| 5133 | |
| 5134 | /* |
| 5135 | * The core merge / split code wants to know how much room is |
Joel Becker | a1cf076 | 2009-02-13 03:45:49 -0800 | [diff] [blame] | 5136 | * left in this allocation tree, so we pass the |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5137 | * rightmost extent list. |
| 5138 | */ |
| 5139 | if (path->p_tree_depth) { |
| 5140 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5141 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5142 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5143 | ocfs2_et_get_last_eb_blk(et), |
| 5144 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5145 | if (ret) { |
| 5146 | mlog_exit(ret); |
| 5147 | goto out; |
| 5148 | } |
| 5149 | |
| 5150 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5151 | rightmost_el = &eb->h_list; |
| 5152 | } else |
| 5153 | rightmost_el = path_root_el(path); |
| 5154 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5155 | if (rec->e_cpos == split_rec->e_cpos && |
| 5156 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5157 | ctxt.c_split_covers_rec = 1; |
| 5158 | else |
| 5159 | ctxt.c_split_covers_rec = 0; |
| 5160 | |
| 5161 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5162 | |
Mark Fasheh | 015452b | 2007-09-12 10:21:22 -0700 | [diff] [blame] | 5163 | mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", |
| 5164 | split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, |
| 5165 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5166 | |
| 5167 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5168 | if (ctxt.c_split_covers_rec) |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5169 | ret = ocfs2_replace_extent_rec(handle, et, path, el, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5170 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5171 | else |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 5172 | ret = ocfs2_split_and_insert(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5173 | &last_eb_bh, split_index, |
| 5174 | split_rec, meta_ac); |
| 5175 | if (ret) |
| 5176 | mlog_errno(ret); |
| 5177 | } else { |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5178 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5179 | split_index, split_rec, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5180 | dealloc, &ctxt); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5181 | if (ret) |
| 5182 | mlog_errno(ret); |
| 5183 | } |
| 5184 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5185 | out: |
| 5186 | brelse(last_eb_bh); |
| 5187 | return ret; |
| 5188 | } |
| 5189 | |
| 5190 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5191 | * Change the flags of the already-existing extent at cpos for len clusters. |
| 5192 | * |
| 5193 | * new_flags: the flags we want to set. |
| 5194 | * clear_flags: the flags we want to clear. |
| 5195 | * phys: the new physical offset we want this new extent starts from. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5196 | * |
| 5197 | * If the existing extent is larger than the request, initiate a |
| 5198 | * split. An attempt will be made at merging with adjacent extents. |
| 5199 | * |
| 5200 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5201 | */ |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5202 | static int ocfs2_change_extent_flag(handle_t *handle, |
| 5203 | struct ocfs2_extent_tree *et, |
| 5204 | u32 cpos, u32 len, u32 phys, |
| 5205 | struct ocfs2_alloc_context *meta_ac, |
| 5206 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 5207 | int new_flags, int clear_flags) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5208 | { |
| 5209 | int ret, index; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5210 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
| 5211 | u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5212 | struct ocfs2_extent_rec split_rec; |
| 5213 | struct ocfs2_path *left_path = NULL; |
| 5214 | struct ocfs2_extent_list *el; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5215 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5216 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5217 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5218 | if (!left_path) { |
| 5219 | ret = -ENOMEM; |
| 5220 | mlog_errno(ret); |
| 5221 | goto out; |
| 5222 | } |
| 5223 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5224 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5225 | if (ret) { |
| 5226 | mlog_errno(ret); |
| 5227 | goto out; |
| 5228 | } |
| 5229 | el = path_leaf_el(left_path); |
| 5230 | |
| 5231 | index = ocfs2_search_extent_list(el, cpos); |
| 5232 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5233 | ocfs2_error(sb, |
| 5234 | "Owner %llu has an extent at cpos %u which can no " |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5235 | "longer be found.\n", |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5236 | (unsigned long long) |
| 5237 | ocfs2_metadata_cache_owner(et->et_ci), cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5238 | ret = -EROFS; |
| 5239 | goto out; |
| 5240 | } |
| 5241 | |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5242 | ret = -EIO; |
| 5243 | rec = &el->l_recs[index]; |
| 5244 | if (new_flags && (rec->e_flags & new_flags)) { |
| 5245 | mlog(ML_ERROR, "Owner %llu tried to set %d flags on an " |
| 5246 | "extent that already had them", |
| 5247 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5248 | new_flags); |
| 5249 | goto out; |
| 5250 | } |
| 5251 | |
| 5252 | if (clear_flags && !(rec->e_flags & clear_flags)) { |
| 5253 | mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an " |
| 5254 | "extent that didn't have them", |
| 5255 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5256 | clear_flags); |
| 5257 | goto out; |
| 5258 | } |
| 5259 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5260 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5261 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5262 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5263 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5264 | split_rec.e_flags = rec->e_flags; |
| 5265 | if (new_flags) |
| 5266 | split_rec.e_flags |= new_flags; |
| 5267 | if (clear_flags) |
| 5268 | split_rec.e_flags &= ~clear_flags; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5269 | |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5270 | ret = __ocfs2_split_extent(handle, et, left_path, |
| 5271 | index, &split_rec, meta_ac, |
| 5272 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5273 | if (ret) |
| 5274 | mlog_errno(ret); |
| 5275 | |
| 5276 | out: |
| 5277 | ocfs2_free_path(left_path); |
| 5278 | return ret; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5279 | |
| 5280 | } |
| 5281 | |
| 5282 | /* |
| 5283 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5284 | * This removes the unwritten extent flag. |
| 5285 | * |
| 5286 | * If the existing extent is larger than the request, initiate a |
| 5287 | * split. An attempt will be made at merging with adjacent extents. |
| 5288 | * |
| 5289 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5290 | */ |
| 5291 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5292 | struct ocfs2_extent_tree *et, |
| 5293 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5294 | struct ocfs2_alloc_context *meta_ac, |
| 5295 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5296 | { |
| 5297 | int ret; |
| 5298 | |
| 5299 | mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n", |
| 5300 | inode->i_ino, cpos, len, phys); |
| 5301 | |
| 5302 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 5303 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 5304 | "that are being written to, but the feature bit " |
| 5305 | "is not set in the super block.", |
| 5306 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5307 | ret = -EROFS; |
| 5308 | goto out; |
| 5309 | } |
| 5310 | |
| 5311 | /* |
| 5312 | * XXX: This should be fixed up so that we just re-insert the |
| 5313 | * next extent records. |
| 5314 | */ |
| 5315 | ocfs2_et_extent_map_truncate(et, 0); |
| 5316 | |
| 5317 | ret = ocfs2_change_extent_flag(handle, et, cpos, |
| 5318 | len, phys, meta_ac, dealloc, |
| 5319 | 0, OCFS2_EXT_UNWRITTEN); |
| 5320 | if (ret) |
| 5321 | mlog_errno(ret); |
| 5322 | |
| 5323 | out: |
| 5324 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5327 | static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 5328 | struct ocfs2_path *path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5329 | int index, u32 new_range, |
| 5330 | struct ocfs2_alloc_context *meta_ac) |
| 5331 | { |
| 5332 | int ret, depth, credits = handle->h_buffer_credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5333 | struct buffer_head *last_eb_bh = NULL; |
| 5334 | struct ocfs2_extent_block *eb; |
| 5335 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5336 | struct ocfs2_extent_rec split_rec; |
| 5337 | struct ocfs2_extent_rec *rec; |
| 5338 | struct ocfs2_insert_type insert; |
| 5339 | |
| 5340 | /* |
| 5341 | * Setup the record to split before we grow the tree. |
| 5342 | */ |
| 5343 | el = path_leaf_el(path); |
| 5344 | rec = &el->l_recs[index]; |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5345 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5346 | &split_rec, new_range, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5347 | |
| 5348 | depth = path->p_tree_depth; |
| 5349 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5350 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5351 | ocfs2_et_get_last_eb_blk(et), |
| 5352 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5353 | if (ret < 0) { |
| 5354 | mlog_errno(ret); |
| 5355 | goto out; |
| 5356 | } |
| 5357 | |
| 5358 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5359 | rightmost_el = &eb->h_list; |
| 5360 | } else |
| 5361 | rightmost_el = path_leaf_el(path); |
| 5362 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 5363 | credits += path->p_tree_depth + |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 5364 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5365 | ret = ocfs2_extend_trans(handle, credits); |
| 5366 | if (ret) { |
| 5367 | mlog_errno(ret); |
| 5368 | goto out; |
| 5369 | } |
| 5370 | |
| 5371 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5372 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5373 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5374 | meta_ac); |
| 5375 | if (ret) { |
| 5376 | mlog_errno(ret); |
| 5377 | goto out; |
| 5378 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5382 | insert.ins_appending = APPEND_NONE; |
| 5383 | insert.ins_contig = CONTIG_NONE; |
| 5384 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5385 | insert.ins_tree_depth = depth; |
| 5386 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5387 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5388 | if (ret) |
| 5389 | mlog_errno(ret); |
| 5390 | |
| 5391 | out: |
| 5392 | brelse(last_eb_bh); |
| 5393 | return ret; |
| 5394 | } |
| 5395 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5396 | static int ocfs2_truncate_rec(handle_t *handle, |
| 5397 | struct ocfs2_extent_tree *et, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5398 | struct ocfs2_path *path, int index, |
| 5399 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5400 | u32 cpos, u32 len) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5401 | { |
| 5402 | int ret; |
| 5403 | u32 left_cpos, rec_range, trunc_range; |
| 5404 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5405 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5406 | struct ocfs2_path *left_path = NULL; |
| 5407 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5408 | struct ocfs2_extent_rec *rec; |
| 5409 | struct ocfs2_extent_block *eb; |
| 5410 | |
| 5411 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5412 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5413 | if (ret) { |
| 5414 | mlog_errno(ret); |
| 5415 | goto out; |
| 5416 | } |
| 5417 | |
| 5418 | index--; |
| 5419 | } |
| 5420 | |
| 5421 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5422 | path->p_tree_depth) { |
| 5423 | /* |
| 5424 | * Check whether this is the rightmost tree record. If |
| 5425 | * we remove all of this record or part of its right |
| 5426 | * edge then an update of the record lengths above it |
| 5427 | * will be required. |
| 5428 | */ |
| 5429 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5430 | if (eb->h_next_leaf_blk == 0) |
| 5431 | is_rightmost_tree_rec = 1; |
| 5432 | } |
| 5433 | |
| 5434 | rec = &el->l_recs[index]; |
| 5435 | if (index == 0 && path->p_tree_depth && |
| 5436 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5437 | /* |
| 5438 | * Changing the leftmost offset (via partial or whole |
| 5439 | * record truncate) of an interior (or rightmost) path |
| 5440 | * means we have to update the subtree that is formed |
| 5441 | * by this leaf and the one to it's left. |
| 5442 | * |
| 5443 | * There are two cases we can skip: |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5444 | * 1) Path is the leftmost one in our btree. |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5445 | * 2) The leaf is rightmost and will be empty after |
| 5446 | * we remove the extent record - the rotate code |
| 5447 | * knows how to update the newly formed edge. |
| 5448 | */ |
| 5449 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5450 | ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5451 | if (ret) { |
| 5452 | mlog_errno(ret); |
| 5453 | goto out; |
| 5454 | } |
| 5455 | |
| 5456 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5457 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5458 | if (!left_path) { |
| 5459 | ret = -ENOMEM; |
| 5460 | mlog_errno(ret); |
| 5461 | goto out; |
| 5462 | } |
| 5463 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5464 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5465 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5466 | if (ret) { |
| 5467 | mlog_errno(ret); |
| 5468 | goto out; |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5474 | handle->h_buffer_credits, |
| 5475 | path); |
| 5476 | if (ret) { |
| 5477 | mlog_errno(ret); |
| 5478 | goto out; |
| 5479 | } |
| 5480 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5481 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5482 | if (ret) { |
| 5483 | mlog_errno(ret); |
| 5484 | goto out; |
| 5485 | } |
| 5486 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5487 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5488 | if (ret) { |
| 5489 | mlog_errno(ret); |
| 5490 | goto out; |
| 5491 | } |
| 5492 | |
| 5493 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5494 | trunc_range = cpos + len; |
| 5495 | |
| 5496 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5497 | int next_free; |
| 5498 | |
| 5499 | memset(rec, 0, sizeof(*rec)); |
| 5500 | ocfs2_cleanup_merge(el, index); |
| 5501 | wants_rotate = 1; |
| 5502 | |
| 5503 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5504 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5505 | /* |
| 5506 | * We skip the edge update if this path will |
| 5507 | * be deleted by the rotate code. |
| 5508 | */ |
| 5509 | rec = &el->l_recs[next_free - 1]; |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5510 | ocfs2_adjust_rightmost_records(handle, et, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5511 | rec); |
| 5512 | } |
| 5513 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5514 | /* Remove leftmost portion of the record. */ |
| 5515 | le32_add_cpu(&rec->e_cpos, len); |
| 5516 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5517 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5518 | } else if (rec_range == trunc_range) { |
| 5519 | /* Remove rightmost portion of the record */ |
| 5520 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5521 | if (is_rightmost_tree_rec) |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5522 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5523 | } else { |
| 5524 | /* Caller should have trapped this. */ |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5525 | mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) " |
| 5526 | "(%u, %u)\n", |
| 5527 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5528 | le32_to_cpu(rec->e_cpos), |
| 5529 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5530 | BUG(); |
| 5531 | } |
| 5532 | |
| 5533 | if (left_path) { |
| 5534 | int subtree_index; |
| 5535 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5536 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5537 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5538 | subtree_index); |
| 5539 | } |
| 5540 | |
| 5541 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5542 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5543 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5544 | if (ret) { |
| 5545 | mlog_errno(ret); |
| 5546 | goto out; |
| 5547 | } |
| 5548 | |
| 5549 | out: |
| 5550 | ocfs2_free_path(left_path); |
| 5551 | return ret; |
| 5552 | } |
| 5553 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5554 | int ocfs2_remove_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5555 | struct ocfs2_extent_tree *et, |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5556 | u32 cpos, u32 len, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5557 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5558 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5559 | { |
| 5560 | int ret, index; |
| 5561 | u32 rec_range, trunc_range; |
| 5562 | struct ocfs2_extent_rec *rec; |
| 5563 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5564 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5565 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 5566 | /* |
| 5567 | * XXX: Why are we truncating to 0 instead of wherever this |
| 5568 | * affects us? |
| 5569 | */ |
| 5570 | ocfs2_et_extent_map_truncate(et, 0); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5571 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5572 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5573 | if (!path) { |
| 5574 | ret = -ENOMEM; |
| 5575 | mlog_errno(ret); |
| 5576 | goto out; |
| 5577 | } |
| 5578 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5579 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5580 | if (ret) { |
| 5581 | mlog_errno(ret); |
| 5582 | goto out; |
| 5583 | } |
| 5584 | |
| 5585 | el = path_leaf_el(path); |
| 5586 | index = ocfs2_search_extent_list(el, cpos); |
| 5587 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5588 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5589 | "Owner %llu has an extent at cpos %u which can no " |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5590 | "longer be found.\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5591 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5592 | cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5593 | ret = -EROFS; |
| 5594 | goto out; |
| 5595 | } |
| 5596 | |
| 5597 | /* |
| 5598 | * We have 3 cases of extent removal: |
| 5599 | * 1) Range covers the entire extent rec |
| 5600 | * 2) Range begins or ends on one edge of the extent rec |
| 5601 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5602 | * |
| 5603 | * For case 1 we remove the extent rec and left rotate to |
| 5604 | * fill the hole. |
| 5605 | * |
| 5606 | * For case 2 we just shrink the existing extent rec, with a |
| 5607 | * tree update if the shrinking edge is also the edge of an |
| 5608 | * extent block. |
| 5609 | * |
| 5610 | * For case 3 we do a right split to turn the extent rec into |
| 5611 | * something case 2 can handle. |
| 5612 | */ |
| 5613 | rec = &el->l_recs[index]; |
| 5614 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5615 | trunc_range = cpos + len; |
| 5616 | |
| 5617 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5618 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5619 | mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d " |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5620 | "(cpos %u, len %u)\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5621 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5622 | cpos, len, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5623 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 5624 | |
| 5625 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5626 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5627 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5628 | if (ret) { |
| 5629 | mlog_errno(ret); |
| 5630 | goto out; |
| 5631 | } |
| 5632 | } else { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5633 | ret = ocfs2_split_tree(handle, et, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5634 | trunc_range, meta_ac); |
| 5635 | if (ret) { |
| 5636 | mlog_errno(ret); |
| 5637 | goto out; |
| 5638 | } |
| 5639 | |
| 5640 | /* |
| 5641 | * The split could have manipulated the tree enough to |
| 5642 | * move the record location, so we have to look for it again. |
| 5643 | */ |
| 5644 | ocfs2_reinit_path(path, 1); |
| 5645 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5646 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5647 | if (ret) { |
| 5648 | mlog_errno(ret); |
| 5649 | goto out; |
| 5650 | } |
| 5651 | |
| 5652 | el = path_leaf_el(path); |
| 5653 | index = ocfs2_search_extent_list(el, cpos); |
| 5654 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5655 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5656 | "Owner %llu: split at cpos %u lost record.", |
| 5657 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5658 | cpos); |
| 5659 | ret = -EROFS; |
| 5660 | goto out; |
| 5661 | } |
| 5662 | |
| 5663 | /* |
| 5664 | * Double check our values here. If anything is fishy, |
| 5665 | * it's easier to catch it at the top level. |
| 5666 | */ |
| 5667 | rec = &el->l_recs[index]; |
| 5668 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5669 | ocfs2_rec_clusters(el, rec); |
| 5670 | if (rec_range != trunc_range) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5671 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5672 | "Owner %llu: error after split at cpos %u" |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5673 | "trunc len %u, existing record is (%u,%u)", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5674 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5675 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5676 | ocfs2_rec_clusters(el, rec)); |
| 5677 | ret = -EROFS; |
| 5678 | goto out; |
| 5679 | } |
| 5680 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5681 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5682 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5683 | if (ret) { |
| 5684 | mlog_errno(ret); |
| 5685 | goto out; |
| 5686 | } |
| 5687 | } |
| 5688 | |
| 5689 | out: |
| 5690 | ocfs2_free_path(path); |
| 5691 | return ret; |
| 5692 | } |
| 5693 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5694 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5695 | struct ocfs2_extent_tree *et, |
| 5696 | u32 cpos, u32 phys_cpos, u32 len, |
| 5697 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5698 | { |
| 5699 | int ret; |
| 5700 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5701 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5702 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5703 | handle_t *handle; |
| 5704 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5705 | |
| 5706 | ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); |
| 5707 | if (ret) { |
| 5708 | mlog_errno(ret); |
| 5709 | return ret; |
| 5710 | } |
| 5711 | |
| 5712 | mutex_lock(&tl_inode->i_mutex); |
| 5713 | |
| 5714 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5715 | ret = __ocfs2_flush_truncate_log(osb); |
| 5716 | if (ret < 0) { |
| 5717 | mlog_errno(ret); |
| 5718 | goto out; |
| 5719 | } |
| 5720 | } |
| 5721 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5722 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5723 | if (IS_ERR(handle)) { |
| 5724 | ret = PTR_ERR(handle); |
| 5725 | mlog_errno(ret); |
| 5726 | goto out; |
| 5727 | } |
| 5728 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5729 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5730 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5731 | if (ret) { |
| 5732 | mlog_errno(ret); |
| 5733 | goto out; |
| 5734 | } |
| 5735 | |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5736 | vfs_dq_free_space_nodirty(inode, |
| 5737 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5738 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5739 | ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5740 | if (ret) { |
| 5741 | mlog_errno(ret); |
| 5742 | goto out_commit; |
| 5743 | } |
| 5744 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5745 | ocfs2_et_update_clusters(et, -len); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5746 | |
| 5747 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
| 5748 | if (ret) { |
| 5749 | mlog_errno(ret); |
| 5750 | goto out_commit; |
| 5751 | } |
| 5752 | |
| 5753 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 5754 | if (ret) |
| 5755 | mlog_errno(ret); |
| 5756 | |
| 5757 | out_commit: |
| 5758 | ocfs2_commit_trans(osb, handle); |
| 5759 | out: |
| 5760 | mutex_unlock(&tl_inode->i_mutex); |
| 5761 | |
| 5762 | if (meta_ac) |
| 5763 | ocfs2_free_alloc_context(meta_ac); |
| 5764 | |
| 5765 | return ret; |
| 5766 | } |
| 5767 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5768 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5769 | { |
| 5770 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5771 | struct ocfs2_dinode *di; |
| 5772 | struct ocfs2_truncate_log *tl; |
| 5773 | |
| 5774 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5775 | tl = &di->id2.i_dealloc; |
| 5776 | |
| 5777 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5778 | "slot %d, invalid truncate log parameters: used = " |
| 5779 | "%u, count = %u\n", osb->slot_num, |
| 5780 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5781 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5782 | } |
| 5783 | |
| 5784 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5785 | unsigned int new_start) |
| 5786 | { |
| 5787 | unsigned int tail_index; |
| 5788 | unsigned int current_tail; |
| 5789 | |
| 5790 | /* No records, nothing to coalesce */ |
| 5791 | if (!le16_to_cpu(tl->tl_used)) |
| 5792 | return 0; |
| 5793 | |
| 5794 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5795 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5796 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5797 | |
| 5798 | return current_tail == new_start; |
| 5799 | } |
| 5800 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5801 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5802 | handle_t *handle, |
| 5803 | u64 start_blk, |
| 5804 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5805 | { |
| 5806 | int status, index; |
| 5807 | unsigned int start_cluster, tl_count; |
| 5808 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5809 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5810 | struct ocfs2_dinode *di; |
| 5811 | struct ocfs2_truncate_log *tl; |
| 5812 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5813 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 5814 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5815 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5816 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5817 | |
| 5818 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5819 | |
| 5820 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5821 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5822 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5823 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5824 | * corruption is a code bug */ |
| 5825 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5826 | |
| 5827 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5828 | tl_count = le16_to_cpu(tl->tl_count); |
| 5829 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5830 | tl_count == 0, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5831 | "Truncate record count on #%llu invalid " |
| 5832 | "wanted %u, actual %u\n", |
| 5833 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5834 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5835 | le16_to_cpu(tl->tl_count)); |
| 5836 | |
| 5837 | /* Caller should have known to flush before calling us. */ |
| 5838 | index = le16_to_cpu(tl->tl_used); |
| 5839 | if (index >= tl_count) { |
| 5840 | status = -ENOSPC; |
| 5841 | mlog_errno(status); |
| 5842 | goto bail; |
| 5843 | } |
| 5844 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5845 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5846 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5847 | if (status < 0) { |
| 5848 | mlog_errno(status); |
| 5849 | goto bail; |
| 5850 | } |
| 5851 | |
| 5852 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5853 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 5854 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5855 | |
| 5856 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5857 | /* |
| 5858 | * Move index back to the record we are coalescing with. |
| 5859 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5860 | */ |
| 5861 | index--; |
| 5862 | |
| 5863 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 5864 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 5865 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5866 | num_clusters); |
| 5867 | } else { |
| 5868 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5869 | tl->tl_used = cpu_to_le16(index + 1); |
| 5870 | } |
| 5871 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5872 | |
| 5873 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5874 | if (status < 0) { |
| 5875 | mlog_errno(status); |
| 5876 | goto bail; |
| 5877 | } |
| 5878 | |
| 5879 | bail: |
| 5880 | mlog_exit(status); |
| 5881 | return status; |
| 5882 | } |
| 5883 | |
| 5884 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5885 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5886 | struct inode *data_alloc_inode, |
| 5887 | struct buffer_head *data_alloc_bh) |
| 5888 | { |
| 5889 | int status = 0; |
| 5890 | int i; |
| 5891 | unsigned int num_clusters; |
| 5892 | u64 start_blk; |
| 5893 | struct ocfs2_truncate_rec rec; |
| 5894 | struct ocfs2_dinode *di; |
| 5895 | struct ocfs2_truncate_log *tl; |
| 5896 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5897 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5898 | |
| 5899 | mlog_entry_void(); |
| 5900 | |
| 5901 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5902 | tl = &di->id2.i_dealloc; |
| 5903 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5904 | while (i >= 0) { |
| 5905 | /* Caller has given us at least enough credits to |
| 5906 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5907 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5908 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5909 | if (status < 0) { |
| 5910 | mlog_errno(status); |
| 5911 | goto bail; |
| 5912 | } |
| 5913 | |
| 5914 | tl->tl_used = cpu_to_le16(i); |
| 5915 | |
| 5916 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5917 | if (status < 0) { |
| 5918 | mlog_errno(status); |
| 5919 | goto bail; |
| 5920 | } |
| 5921 | |
| 5922 | /* TODO: Perhaps we can calculate the bulk of the |
| 5923 | * credits up front rather than extending like |
| 5924 | * this. */ |
| 5925 | status = ocfs2_extend_trans(handle, |
| 5926 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5927 | if (status < 0) { |
| 5928 | mlog_errno(status); |
| 5929 | goto bail; |
| 5930 | } |
| 5931 | |
| 5932 | rec = tl->tl_recs[i]; |
| 5933 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5934 | le32_to_cpu(rec.t_start)); |
| 5935 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5936 | |
| 5937 | /* if start_blk is not set, we ignore the record as |
| 5938 | * invalid. */ |
| 5939 | if (start_blk) { |
| 5940 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 5941 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 5942 | |
| 5943 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5944 | data_alloc_bh, start_blk, |
| 5945 | num_clusters); |
| 5946 | if (status < 0) { |
| 5947 | mlog_errno(status); |
| 5948 | goto bail; |
| 5949 | } |
| 5950 | } |
| 5951 | i--; |
| 5952 | } |
| 5953 | |
| 5954 | bail: |
| 5955 | mlog_exit(status); |
| 5956 | return status; |
| 5957 | } |
| 5958 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5959 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5960 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5961 | { |
| 5962 | int status; |
| 5963 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5964 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5965 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5966 | struct inode *data_alloc_inode = NULL; |
| 5967 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5968 | struct buffer_head *data_alloc_bh = NULL; |
| 5969 | struct ocfs2_dinode *di; |
| 5970 | struct ocfs2_truncate_log *tl; |
| 5971 | |
| 5972 | mlog_entry_void(); |
| 5973 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5974 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5975 | |
| 5976 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5977 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5978 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5979 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5980 | * corruption is a code bug */ |
| 5981 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5982 | |
| 5983 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5984 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5985 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 5986 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5987 | if (!num_to_flush) { |
| 5988 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5989 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 5993 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 5994 | OCFS2_INVALID_SLOT); |
| 5995 | if (!data_alloc_inode) { |
| 5996 | status = -EINVAL; |
| 5997 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5998 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5999 | } |
| 6000 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6001 | mutex_lock(&data_alloc_inode->i_mutex); |
| 6002 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6003 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6004 | if (status < 0) { |
| 6005 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6006 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6007 | } |
| 6008 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6009 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6010 | if (IS_ERR(handle)) { |
| 6011 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6012 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6013 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6014 | } |
| 6015 | |
| 6016 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 6017 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6018 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6019 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6020 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6021 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6022 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6023 | out_unlock: |
| 6024 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6025 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6026 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6027 | out_mutex: |
| 6028 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 6029 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6030 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6031 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6032 | mlog_exit(status); |
| 6033 | return status; |
| 6034 | } |
| 6035 | |
| 6036 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 6037 | { |
| 6038 | int status; |
| 6039 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6040 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6041 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6042 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6043 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6044 | |
| 6045 | return status; |
| 6046 | } |
| 6047 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6048 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6049 | { |
| 6050 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6051 | struct ocfs2_super *osb = |
| 6052 | container_of(work, struct ocfs2_super, |
| 6053 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6054 | |
| 6055 | mlog_entry_void(); |
| 6056 | |
| 6057 | status = ocfs2_flush_truncate_log(osb); |
| 6058 | if (status < 0) |
| 6059 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 6060 | else |
| 6061 | ocfs2_init_inode_steal_slot(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6062 | |
| 6063 | mlog_exit(status); |
| 6064 | } |
| 6065 | |
| 6066 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 6067 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 6068 | int cancel) |
| 6069 | { |
| 6070 | if (osb->osb_tl_inode) { |
| 6071 | /* We want to push off log flushes while truncates are |
| 6072 | * still running. */ |
| 6073 | if (cancel) |
| 6074 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6075 | |
| 6076 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 6077 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 6078 | } |
| 6079 | } |
| 6080 | |
| 6081 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 6082 | int slot_num, |
| 6083 | struct inode **tl_inode, |
| 6084 | struct buffer_head **tl_bh) |
| 6085 | { |
| 6086 | int status; |
| 6087 | struct inode *inode = NULL; |
| 6088 | struct buffer_head *bh = NULL; |
| 6089 | |
| 6090 | inode = ocfs2_get_system_file_inode(osb, |
| 6091 | TRUNCATE_LOG_SYSTEM_INODE, |
| 6092 | slot_num); |
| 6093 | if (!inode) { |
| 6094 | status = -EINVAL; |
| 6095 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 6096 | goto bail; |
| 6097 | } |
| 6098 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 6099 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6100 | if (status < 0) { |
| 6101 | iput(inode); |
| 6102 | mlog_errno(status); |
| 6103 | goto bail; |
| 6104 | } |
| 6105 | |
| 6106 | *tl_inode = inode; |
| 6107 | *tl_bh = bh; |
| 6108 | bail: |
| 6109 | mlog_exit(status); |
| 6110 | return status; |
| 6111 | } |
| 6112 | |
| 6113 | /* called during the 1st stage of node recovery. we stamp a clean |
| 6114 | * truncate log and pass back a copy for processing later. if the |
| 6115 | * truncate log does not require processing, a *tl_copy is set to |
| 6116 | * NULL. */ |
| 6117 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 6118 | int slot_num, |
| 6119 | struct ocfs2_dinode **tl_copy) |
| 6120 | { |
| 6121 | int status; |
| 6122 | struct inode *tl_inode = NULL; |
| 6123 | struct buffer_head *tl_bh = NULL; |
| 6124 | struct ocfs2_dinode *di; |
| 6125 | struct ocfs2_truncate_log *tl; |
| 6126 | |
| 6127 | *tl_copy = NULL; |
| 6128 | |
| 6129 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 6130 | |
| 6131 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 6132 | if (status < 0) { |
| 6133 | mlog_errno(status); |
| 6134 | goto bail; |
| 6135 | } |
| 6136 | |
| 6137 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6138 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 6139 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 6140 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 6141 | * so any corruption is a code bug */ |
| 6142 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 6143 | |
| 6144 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6145 | if (le16_to_cpu(tl->tl_used)) { |
| 6146 | mlog(0, "We'll have %u logs to recover\n", |
| 6147 | le16_to_cpu(tl->tl_used)); |
| 6148 | |
| 6149 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 6150 | if (!(*tl_copy)) { |
| 6151 | status = -ENOMEM; |
| 6152 | mlog_errno(status); |
| 6153 | goto bail; |
| 6154 | } |
| 6155 | |
| 6156 | /* Assuming the write-out below goes well, this copy |
| 6157 | * will be passed back to recovery for processing. */ |
| 6158 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 6159 | |
| 6160 | /* All we need to do to clear the truncate log is set |
| 6161 | * tl_used. */ |
| 6162 | tl->tl_used = 0; |
| 6163 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6164 | 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] | 6165 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6166 | if (status < 0) { |
| 6167 | mlog_errno(status); |
| 6168 | goto bail; |
| 6169 | } |
| 6170 | } |
| 6171 | |
| 6172 | bail: |
| 6173 | if (tl_inode) |
| 6174 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6175 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6176 | |
| 6177 | if (status < 0 && (*tl_copy)) { |
| 6178 | kfree(*tl_copy); |
| 6179 | *tl_copy = NULL; |
| 6180 | } |
| 6181 | |
| 6182 | mlog_exit(status); |
| 6183 | return status; |
| 6184 | } |
| 6185 | |
| 6186 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6187 | struct ocfs2_dinode *tl_copy) |
| 6188 | { |
| 6189 | int status = 0; |
| 6190 | int i; |
| 6191 | unsigned int clusters, num_recs, start_cluster; |
| 6192 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6193 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6194 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6195 | struct ocfs2_truncate_log *tl; |
| 6196 | |
| 6197 | mlog_entry_void(); |
| 6198 | |
| 6199 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6200 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6201 | return -EINVAL; |
| 6202 | } |
| 6203 | |
| 6204 | tl = &tl_copy->id2.i_dealloc; |
| 6205 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 6206 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6207 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6208 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6209 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6210 | for(i = 0; i < num_recs; i++) { |
| 6211 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6212 | status = __ocfs2_flush_truncate_log(osb); |
| 6213 | if (status < 0) { |
| 6214 | mlog_errno(status); |
| 6215 | goto bail_up; |
| 6216 | } |
| 6217 | } |
| 6218 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6219 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6220 | if (IS_ERR(handle)) { |
| 6221 | status = PTR_ERR(handle); |
| 6222 | mlog_errno(status); |
| 6223 | goto bail_up; |
| 6224 | } |
| 6225 | |
| 6226 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6227 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6228 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6229 | |
| 6230 | status = ocfs2_truncate_log_append(osb, handle, |
| 6231 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6232 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6233 | if (status < 0) { |
| 6234 | mlog_errno(status); |
| 6235 | goto bail_up; |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6240 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6241 | |
| 6242 | mlog_exit(status); |
| 6243 | return status; |
| 6244 | } |
| 6245 | |
| 6246 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6247 | { |
| 6248 | int status; |
| 6249 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6250 | |
| 6251 | mlog_entry_void(); |
| 6252 | |
| 6253 | if (tl_inode) { |
| 6254 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6255 | flush_workqueue(ocfs2_wq); |
| 6256 | |
| 6257 | status = ocfs2_flush_truncate_log(osb); |
| 6258 | if (status < 0) |
| 6259 | mlog_errno(status); |
| 6260 | |
| 6261 | brelse(osb->osb_tl_bh); |
| 6262 | iput(osb->osb_tl_inode); |
| 6263 | } |
| 6264 | |
| 6265 | mlog_exit_void(); |
| 6266 | } |
| 6267 | |
| 6268 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6269 | { |
| 6270 | int status; |
| 6271 | struct inode *tl_inode = NULL; |
| 6272 | struct buffer_head *tl_bh = NULL; |
| 6273 | |
| 6274 | mlog_entry_void(); |
| 6275 | |
| 6276 | status = ocfs2_get_truncate_log_info(osb, |
| 6277 | osb->slot_num, |
| 6278 | &tl_inode, |
| 6279 | &tl_bh); |
| 6280 | if (status < 0) |
| 6281 | mlog_errno(status); |
| 6282 | |
| 6283 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6284 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6285 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6286 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6287 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6288 | osb->osb_tl_bh = tl_bh; |
| 6289 | osb->osb_tl_inode = tl_inode; |
| 6290 | |
| 6291 | mlog_exit(status); |
| 6292 | return status; |
| 6293 | } |
| 6294 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6295 | /* |
| 6296 | * Delayed de-allocation of suballocator blocks. |
| 6297 | * |
| 6298 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6299 | * |
| 6300 | * The locking for this can get extremely complicated, especially when |
| 6301 | * the suballocator inodes to delete from aren't known until deep |
| 6302 | * within an unrelated codepath. |
| 6303 | * |
| 6304 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6305 | * btree could have been grown by any number of nodes each allocating |
| 6306 | * out of their own suballoc inode. |
| 6307 | * |
| 6308 | * These structures allow the delay of block de-allocation until a |
| 6309 | * later time, when locking of multiple cluster inodes won't cause |
| 6310 | * deadlock. |
| 6311 | */ |
| 6312 | |
| 6313 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6314 | * Describe a single bit freed from a suballocator. For the block |
| 6315 | * suballocators, it represents one block. For the global cluster |
| 6316 | * allocator, it represents some clusters and free_bit indicates |
| 6317 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6318 | */ |
| 6319 | struct ocfs2_cached_block_free { |
| 6320 | struct ocfs2_cached_block_free *free_next; |
| 6321 | u64 free_blk; |
| 6322 | unsigned int free_bit; |
| 6323 | }; |
| 6324 | |
| 6325 | struct ocfs2_per_slot_free_list { |
| 6326 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6327 | int f_inode_type; |
| 6328 | int f_slot; |
| 6329 | struct ocfs2_cached_block_free *f_first; |
| 6330 | }; |
| 6331 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6332 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6333 | int sysfile_type, |
| 6334 | int slot, |
| 6335 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6336 | { |
| 6337 | int ret; |
| 6338 | u64 bg_blkno; |
| 6339 | handle_t *handle; |
| 6340 | struct inode *inode; |
| 6341 | struct buffer_head *di_bh = NULL; |
| 6342 | struct ocfs2_cached_block_free *tmp; |
| 6343 | |
| 6344 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6345 | if (!inode) { |
| 6346 | ret = -EINVAL; |
| 6347 | mlog_errno(ret); |
| 6348 | goto out; |
| 6349 | } |
| 6350 | |
| 6351 | mutex_lock(&inode->i_mutex); |
| 6352 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6353 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6354 | if (ret) { |
| 6355 | mlog_errno(ret); |
| 6356 | goto out_mutex; |
| 6357 | } |
| 6358 | |
| 6359 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6360 | if (IS_ERR(handle)) { |
| 6361 | ret = PTR_ERR(handle); |
| 6362 | mlog_errno(ret); |
| 6363 | goto out_unlock; |
| 6364 | } |
| 6365 | |
| 6366 | while (head) { |
| 6367 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6368 | head->free_bit); |
| 6369 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 6370 | head->free_bit, (unsigned long long)head->free_blk); |
| 6371 | |
| 6372 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6373 | head->free_bit, bg_blkno, 1); |
| 6374 | if (ret) { |
| 6375 | mlog_errno(ret); |
| 6376 | goto out_journal; |
| 6377 | } |
| 6378 | |
| 6379 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 6380 | if (ret) { |
| 6381 | mlog_errno(ret); |
| 6382 | goto out_journal; |
| 6383 | } |
| 6384 | |
| 6385 | tmp = head; |
| 6386 | head = head->free_next; |
| 6387 | kfree(tmp); |
| 6388 | } |
| 6389 | |
| 6390 | out_journal: |
| 6391 | ocfs2_commit_trans(osb, handle); |
| 6392 | |
| 6393 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6394 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6395 | brelse(di_bh); |
| 6396 | out_mutex: |
| 6397 | mutex_unlock(&inode->i_mutex); |
| 6398 | iput(inode); |
| 6399 | out: |
| 6400 | while(head) { |
| 6401 | /* Premature exit may have left some dangling items. */ |
| 6402 | tmp = head; |
| 6403 | head = head->free_next; |
| 6404 | kfree(tmp); |
| 6405 | } |
| 6406 | |
| 6407 | return ret; |
| 6408 | } |
| 6409 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6410 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6411 | u64 blkno, unsigned int bit) |
| 6412 | { |
| 6413 | int ret = 0; |
| 6414 | struct ocfs2_cached_block_free *item; |
| 6415 | |
| 6416 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6417 | if (item == NULL) { |
| 6418 | ret = -ENOMEM; |
| 6419 | mlog_errno(ret); |
| 6420 | return ret; |
| 6421 | } |
| 6422 | |
| 6423 | mlog(0, "Insert clusters: (bit %u, blk %llu)\n", |
| 6424 | bit, (unsigned long long)blkno); |
| 6425 | |
| 6426 | item->free_blk = blkno; |
| 6427 | item->free_bit = bit; |
| 6428 | item->free_next = ctxt->c_global_allocator; |
| 6429 | |
| 6430 | ctxt->c_global_allocator = item; |
| 6431 | return ret; |
| 6432 | } |
| 6433 | |
| 6434 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6435 | struct ocfs2_cached_block_free *head) |
| 6436 | { |
| 6437 | struct ocfs2_cached_block_free *tmp; |
| 6438 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6439 | handle_t *handle; |
| 6440 | int ret = 0; |
| 6441 | |
| 6442 | mutex_lock(&tl_inode->i_mutex); |
| 6443 | |
| 6444 | while (head) { |
| 6445 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6446 | ret = __ocfs2_flush_truncate_log(osb); |
| 6447 | if (ret < 0) { |
| 6448 | mlog_errno(ret); |
| 6449 | break; |
| 6450 | } |
| 6451 | } |
| 6452 | |
| 6453 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6454 | if (IS_ERR(handle)) { |
| 6455 | ret = PTR_ERR(handle); |
| 6456 | mlog_errno(ret); |
| 6457 | break; |
| 6458 | } |
| 6459 | |
| 6460 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6461 | head->free_bit); |
| 6462 | |
| 6463 | ocfs2_commit_trans(osb, handle); |
| 6464 | tmp = head; |
| 6465 | head = head->free_next; |
| 6466 | kfree(tmp); |
| 6467 | |
| 6468 | if (ret < 0) { |
| 6469 | mlog_errno(ret); |
| 6470 | break; |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | mutex_unlock(&tl_inode->i_mutex); |
| 6475 | |
| 6476 | while (head) { |
| 6477 | /* Premature exit may have left some dangling items. */ |
| 6478 | tmp = head; |
| 6479 | head = head->free_next; |
| 6480 | kfree(tmp); |
| 6481 | } |
| 6482 | |
| 6483 | return ret; |
| 6484 | } |
| 6485 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6486 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6487 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6488 | { |
| 6489 | int ret = 0, ret2; |
| 6490 | struct ocfs2_per_slot_free_list *fl; |
| 6491 | |
| 6492 | if (!ctxt) |
| 6493 | return 0; |
| 6494 | |
| 6495 | while (ctxt->c_first_suballocator) { |
| 6496 | fl = ctxt->c_first_suballocator; |
| 6497 | |
| 6498 | if (fl->f_first) { |
| 6499 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 6500 | fl->f_inode_type, fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6501 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6502 | fl->f_inode_type, |
| 6503 | fl->f_slot, |
| 6504 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6505 | if (ret2) |
| 6506 | mlog_errno(ret2); |
| 6507 | if (!ret) |
| 6508 | ret = ret2; |
| 6509 | } |
| 6510 | |
| 6511 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6512 | kfree(fl); |
| 6513 | } |
| 6514 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6515 | if (ctxt->c_global_allocator) { |
| 6516 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6517 | ctxt->c_global_allocator); |
| 6518 | if (ret2) |
| 6519 | mlog_errno(ret2); |
| 6520 | if (!ret) |
| 6521 | ret = ret2; |
| 6522 | |
| 6523 | ctxt->c_global_allocator = NULL; |
| 6524 | } |
| 6525 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6526 | return ret; |
| 6527 | } |
| 6528 | |
| 6529 | static struct ocfs2_per_slot_free_list * |
| 6530 | ocfs2_find_per_slot_free_list(int type, |
| 6531 | int slot, |
| 6532 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6533 | { |
| 6534 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6535 | |
| 6536 | while (fl) { |
| 6537 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6538 | return fl; |
| 6539 | |
| 6540 | fl = fl->f_next_suballocator; |
| 6541 | } |
| 6542 | |
| 6543 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6544 | if (fl) { |
| 6545 | fl->f_inode_type = type; |
| 6546 | fl->f_slot = slot; |
| 6547 | fl->f_first = NULL; |
| 6548 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6549 | |
| 6550 | ctxt->c_first_suballocator = fl; |
| 6551 | } |
| 6552 | return fl; |
| 6553 | } |
| 6554 | |
| 6555 | static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6556 | int type, int slot, u64 blkno, |
| 6557 | unsigned int bit) |
| 6558 | { |
| 6559 | int ret; |
| 6560 | struct ocfs2_per_slot_free_list *fl; |
| 6561 | struct ocfs2_cached_block_free *item; |
| 6562 | |
| 6563 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6564 | if (fl == NULL) { |
| 6565 | ret = -ENOMEM; |
| 6566 | mlog_errno(ret); |
| 6567 | goto out; |
| 6568 | } |
| 6569 | |
| 6570 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6571 | if (item == NULL) { |
| 6572 | ret = -ENOMEM; |
| 6573 | mlog_errno(ret); |
| 6574 | goto out; |
| 6575 | } |
| 6576 | |
| 6577 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 6578 | type, slot, bit, (unsigned long long)blkno); |
| 6579 | |
| 6580 | item->free_blk = blkno; |
| 6581 | item->free_bit = bit; |
| 6582 | item->free_next = fl->f_first; |
| 6583 | |
| 6584 | fl->f_first = item; |
| 6585 | |
| 6586 | ret = 0; |
| 6587 | out: |
| 6588 | return ret; |
| 6589 | } |
| 6590 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6591 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6592 | struct ocfs2_extent_block *eb) |
| 6593 | { |
| 6594 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6595 | le16_to_cpu(eb->h_suballoc_slot), |
| 6596 | le64_to_cpu(eb->h_blkno), |
| 6597 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6598 | } |
| 6599 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6600 | /* This function will figure out whether the currently last extent |
| 6601 | * block will be deleted, and if it will, what the new last extent |
| 6602 | * block will be so we can update his h_next_leaf_blk field, as well |
| 6603 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6604 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6605 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6606 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6607 | struct buffer_head **new_last_eb) |
| 6608 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6609 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6610 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6611 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6612 | struct ocfs2_extent_block *eb; |
| 6613 | struct ocfs2_extent_list *el; |
| 6614 | struct buffer_head *bh = NULL; |
| 6615 | |
| 6616 | *new_last_eb = NULL; |
| 6617 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6618 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6619 | if (!path->p_tree_depth) |
| 6620 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6621 | |
| 6622 | /* trunc to zero special case - this makes tree_depth = 0 |
| 6623 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6624 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6625 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6626 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6627 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6628 | BUG_ON(!el->l_next_free_rec); |
| 6629 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6630 | /* |
| 6631 | * Make sure that this extent list will actually be empty |
| 6632 | * after we clear away the data. We can shortcut out if |
| 6633 | * there's more than one non-empty extent in the |
| 6634 | * list. Otherwise, a check of the remaining extent is |
| 6635 | * necessary. |
| 6636 | */ |
| 6637 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 6638 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6639 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6640 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6641 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6642 | |
| 6643 | /* We may have a valid extent in index 1, check it. */ |
| 6644 | if (next_free == 2) |
| 6645 | rec = &el->l_recs[1]; |
| 6646 | |
| 6647 | /* |
| 6648 | * Fall through - no more nonempty extents, so we want |
| 6649 | * to delete this leaf. |
| 6650 | */ |
| 6651 | } else { |
| 6652 | if (next_free > 1) |
| 6653 | goto out; |
| 6654 | |
| 6655 | rec = &el->l_recs[0]; |
| 6656 | } |
| 6657 | |
| 6658 | if (rec) { |
| 6659 | /* |
| 6660 | * Check it we'll only be trimming off the end of this |
| 6661 | * cluster. |
| 6662 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6663 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6664 | goto out; |
| 6665 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6666 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6667 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 6668 | if (ret) { |
| 6669 | mlog_errno(ret); |
| 6670 | goto out; |
| 6671 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6672 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 6673 | 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] | 6674 | if (ret) { |
| 6675 | mlog_errno(ret); |
| 6676 | goto out; |
| 6677 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6678 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6679 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 6680 | el = &eb->h_list; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 6681 | |
| 6682 | /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). |
| 6683 | * Any corruption is a code bug. */ |
| 6684 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6685 | |
| 6686 | *new_last_eb = bh; |
| 6687 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6688 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 6689 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 6690 | out: |
| 6691 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6692 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6693 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6694 | } |
| 6695 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6696 | /* |
| 6697 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 6698 | * during truncate. |
| 6699 | * |
| 6700 | * The caller needs to: |
| 6701 | * - start journaling of each path component. |
| 6702 | * - compute and fully set up any new last ext block |
| 6703 | */ |
| 6704 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 6705 | handle_t *handle, struct ocfs2_truncate_context *tc, |
| 6706 | u32 clusters_to_del, u64 *delete_start) |
| 6707 | { |
| 6708 | int ret, i, index = path->p_tree_depth; |
| 6709 | u32 new_edge = 0; |
| 6710 | u64 deleted_eb = 0; |
| 6711 | struct buffer_head *bh; |
| 6712 | struct ocfs2_extent_list *el; |
| 6713 | struct ocfs2_extent_rec *rec; |
| 6714 | |
| 6715 | *delete_start = 0; |
| 6716 | |
| 6717 | while (index >= 0) { |
| 6718 | bh = path->p_node[index].bh; |
| 6719 | el = path->p_node[index].el; |
| 6720 | |
| 6721 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 6722 | index, (unsigned long long)bh->b_blocknr); |
| 6723 | |
| 6724 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 6725 | |
| 6726 | if (index != |
| 6727 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 6728 | ocfs2_error(inode->i_sb, |
| 6729 | "Inode %lu has invalid ext. block %llu", |
| 6730 | inode->i_ino, |
| 6731 | (unsigned long long)bh->b_blocknr); |
| 6732 | ret = -EROFS; |
| 6733 | goto out; |
| 6734 | } |
| 6735 | |
| 6736 | find_tail_record: |
| 6737 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 6738 | rec = &el->l_recs[i]; |
| 6739 | |
| 6740 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 6741 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6742 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6743 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6744 | le16_to_cpu(el->l_next_free_rec)); |
| 6745 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6746 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6747 | |
| 6748 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 6749 | /* |
| 6750 | * If the leaf block contains a single empty |
| 6751 | * extent and no records, we can just remove |
| 6752 | * the block. |
| 6753 | */ |
| 6754 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 6755 | memset(rec, 0, |
| 6756 | sizeof(struct ocfs2_extent_rec)); |
| 6757 | el->l_next_free_rec = cpu_to_le16(0); |
| 6758 | |
| 6759 | goto delete; |
| 6760 | } |
| 6761 | |
| 6762 | /* |
| 6763 | * Remove any empty extents by shifting things |
| 6764 | * left. That should make life much easier on |
| 6765 | * the code below. This condition is rare |
| 6766 | * enough that we shouldn't see a performance |
| 6767 | * hit. |
| 6768 | */ |
| 6769 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 6770 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6771 | |
| 6772 | for(i = 0; |
| 6773 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 6774 | el->l_recs[i] = el->l_recs[i + 1]; |
| 6775 | |
| 6776 | memset(&el->l_recs[i], 0, |
| 6777 | sizeof(struct ocfs2_extent_rec)); |
| 6778 | |
| 6779 | /* |
| 6780 | * We've modified our extent list. The |
| 6781 | * simplest way to handle this change |
| 6782 | * is to being the search from the |
| 6783 | * start again. |
| 6784 | */ |
| 6785 | goto find_tail_record; |
| 6786 | } |
| 6787 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6788 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6789 | |
| 6790 | /* |
| 6791 | * We'll use "new_edge" on our way back up the |
| 6792 | * tree to know what our rightmost cpos is. |
| 6793 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6794 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6795 | new_edge += le32_to_cpu(rec->e_cpos); |
| 6796 | |
| 6797 | /* |
| 6798 | * The caller will use this to delete data blocks. |
| 6799 | */ |
| 6800 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 6801 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6802 | le16_to_cpu(rec->e_leaf_clusters)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6803 | |
| 6804 | /* |
| 6805 | * If it's now empty, remove this record. |
| 6806 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6807 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6808 | memset(rec, 0, |
| 6809 | sizeof(struct ocfs2_extent_rec)); |
| 6810 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6811 | } |
| 6812 | } else { |
| 6813 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 6814 | memset(rec, 0, |
| 6815 | sizeof(struct ocfs2_extent_rec)); |
| 6816 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6817 | |
| 6818 | goto delete; |
| 6819 | } |
| 6820 | |
| 6821 | /* Can this actually happen? */ |
| 6822 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 6823 | goto delete; |
| 6824 | |
| 6825 | /* |
| 6826 | * We never actually deleted any clusters |
| 6827 | * because our leaf was empty. There's no |
| 6828 | * reason to adjust the rightmost edge then. |
| 6829 | */ |
| 6830 | if (new_edge == 0) |
| 6831 | goto delete; |
| 6832 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6833 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 6834 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6835 | -le32_to_cpu(rec->e_cpos)); |
| 6836 | |
| 6837 | /* |
| 6838 | * A deleted child record should have been |
| 6839 | * caught above. |
| 6840 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6841 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | delete: |
| 6845 | ret = ocfs2_journal_dirty(handle, bh); |
| 6846 | if (ret) { |
| 6847 | mlog_errno(ret); |
| 6848 | goto out; |
| 6849 | } |
| 6850 | |
| 6851 | mlog(0, "extent list container %llu, after: record %d: " |
| 6852 | "(%u, %u, %llu), next = %u.\n", |
| 6853 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6854 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6855 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6856 | le16_to_cpu(el->l_next_free_rec)); |
| 6857 | |
| 6858 | /* |
| 6859 | * We must be careful to only attempt delete of an |
| 6860 | * extent block (and not the root inode block). |
| 6861 | */ |
| 6862 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 6863 | struct ocfs2_extent_block *eb = |
| 6864 | (struct ocfs2_extent_block *)bh->b_data; |
| 6865 | |
| 6866 | /* |
| 6867 | * Save this for use when processing the |
| 6868 | * parent block. |
| 6869 | */ |
| 6870 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 6871 | |
| 6872 | mlog(0, "deleting this extent block.\n"); |
| 6873 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6874 | ocfs2_remove_from_cache(INODE_CACHE(inode), bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6875 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6876 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6877 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 6878 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 6879 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6880 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 6881 | /* An error here is not fatal. */ |
| 6882 | if (ret < 0) |
| 6883 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6884 | } else { |
| 6885 | deleted_eb = 0; |
| 6886 | } |
| 6887 | |
| 6888 | index--; |
| 6889 | } |
| 6890 | |
| 6891 | ret = 0; |
| 6892 | out: |
| 6893 | return ret; |
| 6894 | } |
| 6895 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6896 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 6897 | unsigned int clusters_to_del, |
| 6898 | struct inode *inode, |
| 6899 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6900 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6901 | struct ocfs2_truncate_context *tc, |
| 6902 | struct ocfs2_path *path) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6903 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6904 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6905 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6906 | struct ocfs2_extent_block *last_eb = NULL; |
| 6907 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6908 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6909 | u64 delete_blk = 0; |
| 6910 | |
| 6911 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6912 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6913 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6914 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6915 | if (status < 0) { |
| 6916 | mlog_errno(status); |
| 6917 | goto bail; |
| 6918 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6919 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6920 | /* |
| 6921 | * Each component will be touched, so we might as well journal |
| 6922 | * here to avoid having to handle errors later. |
| 6923 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6924 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6925 | if (status < 0) { |
| 6926 | mlog_errno(status); |
| 6927 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6928 | } |
| 6929 | |
| 6930 | if (last_eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6931 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6932 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6933 | if (status < 0) { |
| 6934 | mlog_errno(status); |
| 6935 | goto bail; |
| 6936 | } |
| 6937 | |
| 6938 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6939 | } |
| 6940 | |
| 6941 | el = &(fe->id2.i_list); |
| 6942 | |
| 6943 | /* |
| 6944 | * Lower levels depend on this never happening, but it's best |
| 6945 | * to check it up here before changing the tree. |
| 6946 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6947 | 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] | 6948 | ocfs2_error(inode->i_sb, |
| 6949 | "Inode %lu has an empty extent record, depth %u\n", |
| 6950 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6951 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6952 | goto bail; |
| 6953 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6954 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6955 | vfs_dq_free_space_nodirty(inode, |
| 6956 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6957 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 6958 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 6959 | clusters_to_del; |
| 6960 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 6961 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 6962 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6963 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6964 | status = ocfs2_trim_tree(inode, path, handle, tc, |
| 6965 | clusters_to_del, &delete_blk); |
| 6966 | if (status) { |
| 6967 | mlog_errno(status); |
| 6968 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6969 | } |
| 6970 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6971 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6972 | /* trunc to zero is a special case. */ |
| 6973 | el->l_tree_depth = 0; |
| 6974 | fe->i_last_eb_blk = 0; |
| 6975 | } else if (last_eb) |
| 6976 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 6977 | |
| 6978 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 6979 | if (status < 0) { |
| 6980 | mlog_errno(status); |
| 6981 | goto bail; |
| 6982 | } |
| 6983 | |
| 6984 | if (last_eb) { |
| 6985 | /* If there will be a new last extent block, then by |
| 6986 | * definition, there cannot be any leaves to the right of |
| 6987 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6988 | last_eb->h_next_leaf_blk = 0; |
| 6989 | status = ocfs2_journal_dirty(handle, last_eb_bh); |
| 6990 | if (status < 0) { |
| 6991 | mlog_errno(status); |
| 6992 | goto bail; |
| 6993 | } |
| 6994 | } |
| 6995 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6996 | if (delete_blk) { |
| 6997 | status = ocfs2_truncate_log_append(osb, handle, delete_blk, |
| 6998 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6999 | if (status < 0) { |
| 7000 | mlog_errno(status); |
| 7001 | goto bail; |
| 7002 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7003 | } |
| 7004 | status = 0; |
| 7005 | bail: |
Tao Ma | 60e2ec4 | 2009-08-12 14:42:47 +0800 | [diff] [blame] | 7006 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7007 | mlog_exit(status); |
| 7008 | return status; |
| 7009 | } |
| 7010 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 7011 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7012 | { |
| 7013 | set_buffer_uptodate(bh); |
| 7014 | mark_buffer_dirty(bh); |
| 7015 | return 0; |
| 7016 | } |
| 7017 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7018 | static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 7019 | unsigned int from, unsigned int to, |
| 7020 | struct page *page, int zero, u64 *phys) |
| 7021 | { |
| 7022 | int ret, partial = 0; |
| 7023 | |
| 7024 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 7025 | if (ret) |
| 7026 | mlog_errno(ret); |
| 7027 | |
| 7028 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 7029 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7030 | |
| 7031 | /* |
| 7032 | * Need to set the buffers we zero'd into uptodate |
| 7033 | * here if they aren't - ocfs2_map_page_blocks() |
| 7034 | * might've skipped some |
| 7035 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 7036 | ret = walk_page_buffers(handle, page_buffers(page), |
| 7037 | from, to, &partial, |
| 7038 | ocfs2_zero_func); |
| 7039 | if (ret < 0) |
| 7040 | mlog_errno(ret); |
| 7041 | else if (ocfs2_should_order_data(inode)) { |
| 7042 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7043 | if (ret < 0) |
| 7044 | mlog_errno(ret); |
| 7045 | } |
| 7046 | |
| 7047 | if (!partial) |
| 7048 | SetPageUptodate(page); |
| 7049 | |
| 7050 | flush_dcache_page(page); |
| 7051 | } |
| 7052 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7053 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 7054 | loff_t end, struct page **pages, |
| 7055 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7056 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7057 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7058 | struct page *page; |
| 7059 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 7060 | struct super_block *sb = inode->i_sb; |
| 7061 | |
| 7062 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 7063 | |
| 7064 | if (numpages == 0) |
| 7065 | goto out; |
| 7066 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7067 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7068 | for(i = 0; i < numpages; i++) { |
| 7069 | page = pages[i]; |
| 7070 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7071 | from = start & (PAGE_CACHE_SIZE - 1); |
| 7072 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 7073 | to = end & (PAGE_CACHE_SIZE - 1); |
| 7074 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7075 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 7076 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 7077 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7078 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 7079 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7080 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7081 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7082 | } |
| 7083 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7084 | if (pages) |
| 7085 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7086 | } |
| 7087 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7088 | 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] | 7089 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7090 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7091 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7092 | struct super_block *sb = inode->i_sb; |
| 7093 | struct address_space *mapping = inode->i_mapping; |
| 7094 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7095 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7096 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7097 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7098 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7099 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 7100 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 7101 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7102 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7103 | last_page_bytes = PAGE_ALIGN(end); |
| 7104 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7105 | do { |
| 7106 | pages[numpages] = grab_cache_page(mapping, index); |
| 7107 | if (!pages[numpages]) { |
| 7108 | ret = -ENOMEM; |
| 7109 | mlog_errno(ret); |
| 7110 | goto out; |
| 7111 | } |
| 7112 | |
| 7113 | numpages++; |
| 7114 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7115 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7116 | |
| 7117 | out: |
| 7118 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7119 | if (pages) |
| 7120 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7121 | numpages = 0; |
| 7122 | } |
| 7123 | |
| 7124 | *num = numpages; |
| 7125 | |
| 7126 | return ret; |
| 7127 | } |
| 7128 | |
| 7129 | /* |
| 7130 | * Zero the area past i_size but still within an allocated |
| 7131 | * cluster. This avoids exposing nonzero data on subsequent file |
| 7132 | * extends. |
| 7133 | * |
| 7134 | * We need to call this before i_size is updated on the inode because |
| 7135 | * otherwise block_write_full_page() will skip writeout of pages past |
| 7136 | * i_size. The new_i_size parameter is passed for this reason. |
| 7137 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7138 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 7139 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7140 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7141 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7142 | struct page **pages = NULL; |
| 7143 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7144 | unsigned int ext_flags; |
| 7145 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7146 | |
| 7147 | /* |
| 7148 | * File systems which don't support sparse files zero on every |
| 7149 | * extend. |
| 7150 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7151 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7152 | return 0; |
| 7153 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7154 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7155 | sizeof(struct page *), GFP_NOFS); |
| 7156 | if (pages == NULL) { |
| 7157 | ret = -ENOMEM; |
| 7158 | mlog_errno(ret); |
| 7159 | goto out; |
| 7160 | } |
| 7161 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7162 | if (range_start == range_end) |
| 7163 | goto out; |
| 7164 | |
| 7165 | ret = ocfs2_extent_map_get_blocks(inode, |
| 7166 | range_start >> sb->s_blocksize_bits, |
| 7167 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7168 | if (ret) { |
| 7169 | mlog_errno(ret); |
| 7170 | goto out; |
| 7171 | } |
| 7172 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7173 | /* |
| 7174 | * Tail is a hole, or is marked unwritten. In either case, we |
| 7175 | * can count on read and write to return/push zero's. |
| 7176 | */ |
| 7177 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7178 | goto out; |
| 7179 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7180 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 7181 | &numpages); |
| 7182 | if (ret) { |
| 7183 | mlog_errno(ret); |
| 7184 | goto out; |
| 7185 | } |
| 7186 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7187 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 7188 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7189 | |
| 7190 | /* |
| 7191 | * Initiate writeout of the pages we zero'd here. We don't |
| 7192 | * wait on them - the truncate_inode_pages() call later will |
| 7193 | * do that for us. |
| 7194 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7195 | ret = do_sync_mapping_range(inode->i_mapping, range_start, |
| 7196 | range_end - 1, SYNC_FILE_RANGE_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7197 | if (ret) |
| 7198 | mlog_errno(ret); |
| 7199 | |
| 7200 | out: |
| 7201 | if (pages) |
| 7202 | kfree(pages); |
| 7203 | |
| 7204 | return ret; |
| 7205 | } |
| 7206 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7207 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 7208 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7209 | { |
| 7210 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7211 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7212 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7213 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 7214 | memset(&di->id2, 0, blocksize - |
| 7215 | offsetof(struct ocfs2_dinode, id2) - |
| 7216 | xattrsize); |
| 7217 | else |
| 7218 | memset(&di->id2, 0, blocksize - |
| 7219 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7220 | } |
| 7221 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7222 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 7223 | struct ocfs2_dinode *di) |
| 7224 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7225 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7226 | di->id2.i_list.l_tree_depth = 0; |
| 7227 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7228 | di->id2.i_list.l_count = cpu_to_le16( |
| 7229 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7230 | } |
| 7231 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7232 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 7233 | { |
| 7234 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7235 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7236 | |
| 7237 | spin_lock(&oi->ip_lock); |
| 7238 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 7239 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7240 | spin_unlock(&oi->ip_lock); |
| 7241 | |
| 7242 | /* |
| 7243 | * We clear the entire i_data structure here so that all |
| 7244 | * fields can be properly initialized. |
| 7245 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7246 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7247 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7248 | idata->id_count = cpu_to_le16( |
| 7249 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 7253 | struct buffer_head *di_bh) |
| 7254 | { |
| 7255 | int ret, i, has_data, num_pages = 0; |
| 7256 | handle_t *handle; |
| 7257 | u64 uninitialized_var(block); |
| 7258 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7259 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7260 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7261 | struct ocfs2_alloc_context *data_ac = NULL; |
| 7262 | struct page **pages = NULL; |
| 7263 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7264 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7265 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7266 | |
| 7267 | has_data = i_size_read(inode) ? 1 : 0; |
| 7268 | |
| 7269 | if (has_data) { |
| 7270 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 7271 | sizeof(struct page *), GFP_NOFS); |
| 7272 | if (pages == NULL) { |
| 7273 | ret = -ENOMEM; |
| 7274 | mlog_errno(ret); |
| 7275 | goto out; |
| 7276 | } |
| 7277 | |
| 7278 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 7279 | if (ret) { |
| 7280 | mlog_errno(ret); |
| 7281 | goto out; |
| 7282 | } |
| 7283 | } |
| 7284 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7285 | handle = ocfs2_start_trans(osb, |
| 7286 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7287 | if (IS_ERR(handle)) { |
| 7288 | ret = PTR_ERR(handle); |
| 7289 | mlog_errno(ret); |
| 7290 | goto out_unlock; |
| 7291 | } |
| 7292 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7293 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7294 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7295 | if (ret) { |
| 7296 | mlog_errno(ret); |
| 7297 | goto out_commit; |
| 7298 | } |
| 7299 | |
| 7300 | if (has_data) { |
| 7301 | u32 bit_off, num; |
| 7302 | unsigned int page_end; |
| 7303 | u64 phys; |
| 7304 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7305 | if (vfs_dq_alloc_space_nodirty(inode, |
| 7306 | ocfs2_clusters_to_bytes(osb->sb, 1))) { |
| 7307 | ret = -EDQUOT; |
| 7308 | goto out_commit; |
| 7309 | } |
| 7310 | did_quota = 1; |
| 7311 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7312 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 7313 | &num); |
| 7314 | if (ret) { |
| 7315 | mlog_errno(ret); |
| 7316 | goto out_commit; |
| 7317 | } |
| 7318 | |
| 7319 | /* |
| 7320 | * Save two copies, one for insert, and one that can |
| 7321 | * be changed by ocfs2_map_and_dirty_page() below. |
| 7322 | */ |
| 7323 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 7324 | |
| 7325 | /* |
| 7326 | * Non sparse file systems zero on extend, so no need |
| 7327 | * to do that now. |
| 7328 | */ |
| 7329 | if (!ocfs2_sparse_alloc(osb) && |
| 7330 | PAGE_CACHE_SIZE < osb->s_clustersize) |
| 7331 | end = PAGE_CACHE_SIZE; |
| 7332 | |
| 7333 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 7334 | if (ret) { |
| 7335 | mlog_errno(ret); |
| 7336 | goto out_commit; |
| 7337 | } |
| 7338 | |
| 7339 | /* |
| 7340 | * This should populate the 1st page for us and mark |
| 7341 | * it up to date. |
| 7342 | */ |
| 7343 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 7344 | if (ret) { |
| 7345 | mlog_errno(ret); |
| 7346 | goto out_commit; |
| 7347 | } |
| 7348 | |
| 7349 | page_end = PAGE_CACHE_SIZE; |
| 7350 | if (PAGE_CACHE_SIZE > osb->s_clustersize) |
| 7351 | page_end = osb->s_clustersize; |
| 7352 | |
| 7353 | for (i = 0; i < num_pages; i++) |
| 7354 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 7355 | pages[i], i > 0, &phys); |
| 7356 | } |
| 7357 | |
| 7358 | spin_lock(&oi->ip_lock); |
| 7359 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 7360 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7361 | spin_unlock(&oi->ip_lock); |
| 7362 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7363 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7364 | |
| 7365 | ocfs2_journal_dirty(handle, di_bh); |
| 7366 | |
| 7367 | if (has_data) { |
| 7368 | /* |
| 7369 | * An error at this point should be extremely rare. If |
| 7370 | * this proves to be false, we could always re-build |
| 7371 | * the in-inode data from our pages. |
| 7372 | */ |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 7373 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 7374 | ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7375 | if (ret) { |
| 7376 | mlog_errno(ret); |
| 7377 | goto out_commit; |
| 7378 | } |
| 7379 | |
| 7380 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7381 | } |
| 7382 | |
| 7383 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7384 | if (ret < 0 && did_quota) |
| 7385 | vfs_dq_free_space_nodirty(inode, |
| 7386 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7387 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7388 | ocfs2_commit_trans(osb, handle); |
| 7389 | |
| 7390 | out_unlock: |
| 7391 | if (data_ac) |
| 7392 | ocfs2_free_alloc_context(data_ac); |
| 7393 | |
| 7394 | out: |
| 7395 | if (pages) { |
| 7396 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7397 | kfree(pages); |
| 7398 | } |
| 7399 | |
| 7400 | return ret; |
| 7401 | } |
| 7402 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7403 | /* |
| 7404 | * It is expected, that by the time you call this function, |
| 7405 | * inode->i_size and fe->i_size have been adjusted. |
| 7406 | * |
| 7407 | * WARNING: This will kfree the truncate context |
| 7408 | */ |
| 7409 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7410 | struct inode *inode, |
| 7411 | struct buffer_head *fe_bh, |
| 7412 | struct ocfs2_truncate_context *tc) |
| 7413 | { |
| 7414 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7415 | u32 clusters_to_del, new_highest_cpos, range; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7416 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 7417 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7418 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7419 | struct ocfs2_path *path = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 7420 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7421 | |
| 7422 | mlog_entry_void(); |
| 7423 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7424 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7425 | i_size_read(inode)); |
| 7426 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7427 | path = ocfs2_new_path(fe_bh, &di->id2.i_list, |
| 7428 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7429 | if (!path) { |
| 7430 | status = -ENOMEM; |
| 7431 | mlog_errno(status); |
| 7432 | goto bail; |
| 7433 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7434 | |
| 7435 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7436 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7437 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7438 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7439 | * Check that we still have allocation to delete. |
| 7440 | */ |
| 7441 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7442 | status = 0; |
| 7443 | goto bail; |
| 7444 | } |
| 7445 | |
| 7446 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7447 | * Truncate always works against the rightmost tree branch. |
| 7448 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7449 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7450 | if (status) { |
| 7451 | mlog_errno(status); |
| 7452 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7453 | } |
| 7454 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7455 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 7456 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 7457 | |
| 7458 | /* |
| 7459 | * By now, el will point to the extent list on the bottom most |
| 7460 | * portion of this tree. Only the tail record is considered in |
| 7461 | * each pass. |
| 7462 | * |
| 7463 | * We handle the following cases, in order: |
| 7464 | * - empty extent: delete the remaining branch |
| 7465 | * - remove the entire record |
| 7466 | * - remove a partial record |
| 7467 | * - no record needs to be removed (truncate has completed) |
| 7468 | */ |
| 7469 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7470 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7471 | ocfs2_error(inode->i_sb, |
| 7472 | "Inode %llu has empty extent block at %llu\n", |
| 7473 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7474 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7475 | status = -EROFS; |
| 7476 | goto bail; |
| 7477 | } |
| 7478 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7479 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7480 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7481 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7482 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 7483 | clusters_to_del = 0; |
| 7484 | } 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] | 7485 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7486 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7487 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7488 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7489 | new_highest_cpos; |
| 7490 | } else { |
| 7491 | status = 0; |
| 7492 | goto bail; |
| 7493 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7494 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7495 | mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", |
| 7496 | clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7497 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7498 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7499 | tl_sem = 1; |
| 7500 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 7501 | * record is free for use. If there isn't any, we flush to get |
| 7502 | * an empty truncate log. */ |
| 7503 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 7504 | status = __ocfs2_flush_truncate_log(osb); |
| 7505 | if (status < 0) { |
| 7506 | mlog_errno(status); |
| 7507 | goto bail; |
| 7508 | } |
| 7509 | } |
| 7510 | |
| 7511 | credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7512 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 7513 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 7514 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7515 | if (IS_ERR(handle)) { |
| 7516 | status = PTR_ERR(handle); |
| 7517 | handle = NULL; |
| 7518 | mlog_errno(status); |
| 7519 | goto bail; |
| 7520 | } |
| 7521 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7522 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
| 7523 | tc, path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7524 | if (status < 0) { |
| 7525 | mlog_errno(status); |
| 7526 | goto bail; |
| 7527 | } |
| 7528 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7529 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7530 | tl_sem = 0; |
| 7531 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7532 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7533 | handle = NULL; |
| 7534 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7535 | ocfs2_reinit_path(path, 1); |
| 7536 | |
| 7537 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7538 | * The check above will catch the case where we've truncated |
| 7539 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7540 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7541 | goto start; |
| 7542 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7543 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7544 | |
| 7545 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7546 | |
| 7547 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7548 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7549 | |
| 7550 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7551 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7552 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7553 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 7554 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7555 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7556 | |
| 7557 | /* This will drop the ext_alloc cluster lock for us */ |
| 7558 | ocfs2_free_truncate_context(tc); |
| 7559 | |
| 7560 | mlog_exit(status); |
| 7561 | return status; |
| 7562 | } |
| 7563 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7564 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7565 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7566 | */ |
| 7567 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 7568 | struct inode *inode, |
| 7569 | struct buffer_head *fe_bh, |
| 7570 | struct ocfs2_truncate_context **tc) |
| 7571 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7572 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7573 | unsigned int new_i_clusters; |
| 7574 | struct ocfs2_dinode *fe; |
| 7575 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7576 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7577 | |
| 7578 | mlog_entry_void(); |
| 7579 | |
| 7580 | *tc = NULL; |
| 7581 | |
| 7582 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 7583 | i_size_read(inode)); |
| 7584 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 7585 | |
| 7586 | 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] | 7587 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 7588 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7589 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 7590 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7591 | if (!(*tc)) { |
| 7592 | status = -ENOMEM; |
| 7593 | mlog_errno(status); |
| 7594 | goto bail; |
| 7595 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7596 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7597 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7598 | if (fe->id2.i_list.l_tree_depth) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 7599 | status = ocfs2_read_extent_block(INODE_CACHE(inode), |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 7600 | le64_to_cpu(fe->i_last_eb_blk), |
| 7601 | &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7602 | if (status < 0) { |
| 7603 | mlog_errno(status); |
| 7604 | goto bail; |
| 7605 | } |
| 7606 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7607 | } |
| 7608 | |
| 7609 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 7610 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7611 | status = 0; |
| 7612 | bail: |
| 7613 | if (status < 0) { |
| 7614 | if (*tc) |
| 7615 | ocfs2_free_truncate_context(*tc); |
| 7616 | *tc = NULL; |
| 7617 | } |
| 7618 | mlog_exit_void(); |
| 7619 | return status; |
| 7620 | } |
| 7621 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7622 | /* |
| 7623 | * 'start' is inclusive, 'end' is not. |
| 7624 | */ |
| 7625 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7626 | unsigned int start, unsigned int end, int trunc) |
| 7627 | { |
| 7628 | int ret; |
| 7629 | unsigned int numbytes; |
| 7630 | handle_t *handle; |
| 7631 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7632 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7633 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7634 | |
| 7635 | if (end > i_size_read(inode)) |
| 7636 | end = i_size_read(inode); |
| 7637 | |
| 7638 | BUG_ON(start >= end); |
| 7639 | |
| 7640 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7641 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7642 | !ocfs2_supports_inline_data(osb)) { |
| 7643 | ocfs2_error(inode->i_sb, |
| 7644 | "Inline data flags for inode %llu don't agree! " |
| 7645 | "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
| 7646 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7647 | le16_to_cpu(di->i_dyn_features), |
| 7648 | OCFS2_I(inode)->ip_dyn_features, |
| 7649 | osb->s_feature_incompat); |
| 7650 | ret = -EROFS; |
| 7651 | goto out; |
| 7652 | } |
| 7653 | |
| 7654 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7655 | if (IS_ERR(handle)) { |
| 7656 | ret = PTR_ERR(handle); |
| 7657 | mlog_errno(ret); |
| 7658 | goto out; |
| 7659 | } |
| 7660 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7661 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7662 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7663 | if (ret) { |
| 7664 | mlog_errno(ret); |
| 7665 | goto out_commit; |
| 7666 | } |
| 7667 | |
| 7668 | numbytes = end - start; |
| 7669 | memset(idata->id_data + start, 0, numbytes); |
| 7670 | |
| 7671 | /* |
| 7672 | * No need to worry about the data page here - it's been |
| 7673 | * truncated already and inline data doesn't need it for |
| 7674 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7675 | * later. |
| 7676 | */ |
| 7677 | if (trunc) { |
| 7678 | i_size_write(inode, start); |
| 7679 | di->i_size = cpu_to_le64(start); |
| 7680 | } |
| 7681 | |
| 7682 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7683 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 7684 | |
| 7685 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7686 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7687 | |
| 7688 | ocfs2_journal_dirty(handle, di_bh); |
| 7689 | |
| 7690 | out_commit: |
| 7691 | ocfs2_commit_trans(osb, handle); |
| 7692 | |
| 7693 | out: |
| 7694 | return ret; |
| 7695 | } |
| 7696 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7697 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 7698 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7699 | /* |
| 7700 | * The caller is responsible for completing deallocation |
| 7701 | * before freeing the context. |
| 7702 | */ |
| 7703 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 7704 | mlog(ML_NOTICE, |
| 7705 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7706 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 7707 | brelse(tc->tc_last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7708 | |
| 7709 | kfree(tc); |
| 7710 | } |