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" |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 52 | #include "refcounttree.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 53 | |
| 54 | #include "buffer_head_io.h" |
| 55 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 56 | enum ocfs2_contig_type { |
| 57 | CONTIG_NONE = 0, |
| 58 | CONTIG_LEFT, |
| 59 | CONTIG_RIGHT, |
| 60 | CONTIG_LEFTRIGHT, |
| 61 | }; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 62 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 63 | static enum ocfs2_contig_type |
| 64 | ocfs2_extent_rec_contig(struct super_block *sb, |
| 65 | struct ocfs2_extent_rec *ext, |
| 66 | struct ocfs2_extent_rec *insert_rec); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 67 | /* |
| 68 | * Operations for a specific extent tree type. |
| 69 | * |
| 70 | * To implement an on-disk btree (extent tree) type in ocfs2, add |
| 71 | * an ocfs2_extent_tree_operations structure and the matching |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 72 | * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 73 | * for the allocation portion of the extent tree. |
| 74 | */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 75 | struct ocfs2_extent_tree_operations { |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 76 | /* |
| 77 | * last_eb_blk is the block number of the right most leaf extent |
| 78 | * block. Most on-disk structures containing an extent tree store |
| 79 | * this value for fast access. The ->eo_set_last_eb_blk() and |
| 80 | * ->eo_get_last_eb_blk() operations access this value. They are |
| 81 | * both required. |
| 82 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 83 | void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et, |
| 84 | u64 blkno); |
| 85 | u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * The on-disk structure usually keeps track of how many total |
| 89 | * clusters are stored in this extent tree. This function updates |
| 90 | * that value. new_clusters is the delta, and must be |
| 91 | * added to the total. Required. |
| 92 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 93 | void (*eo_update_clusters)(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 94 | u32 new_clusters); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 97 | * If this extent tree is supported by an extent map, insert |
| 98 | * a record into the map. |
| 99 | */ |
| 100 | void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et, |
| 101 | struct ocfs2_extent_rec *rec); |
| 102 | |
| 103 | /* |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 104 | * If this extent tree is supported by an extent map, truncate the |
| 105 | * map to clusters, |
| 106 | */ |
| 107 | void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et, |
| 108 | u32 clusters); |
| 109 | |
| 110 | /* |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 111 | * If ->eo_insert_check() exists, it is called before rec is |
| 112 | * inserted into the extent tree. It is optional. |
| 113 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 114 | int (*eo_insert_check)(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 115 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 116 | int (*eo_sanity_check)(struct ocfs2_extent_tree *et); |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 117 | |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 118 | /* |
| 119 | * -------------------------------------------------------------- |
| 120 | * The remaining are internal to ocfs2_extent_tree and don't have |
| 121 | * accessor functions |
| 122 | */ |
| 123 | |
| 124 | /* |
| 125 | * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. |
| 126 | * It is required. |
| 127 | */ |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 128 | void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if |
| 132 | * it exists. If it does not, et->et_max_leaf_clusters is set |
| 133 | * to 0 (unlimited). Optional. |
| 134 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 135 | void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * ->eo_extent_contig test whether the 2 ocfs2_extent_rec |
| 139 | * are contiguous or not. Optional. Don't need to set it if use |
| 140 | * ocfs2_extent_rec as the tree leaf. |
| 141 | */ |
| 142 | enum ocfs2_contig_type |
| 143 | (*eo_extent_contig)(struct ocfs2_extent_tree *et, |
| 144 | struct ocfs2_extent_rec *ext, |
| 145 | struct ocfs2_extent_rec *insert_rec); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 146 | }; |
| 147 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check |
| 151 | * in the methods. |
| 152 | */ |
| 153 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); |
| 154 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 155 | u64 blkno); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 156 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 157 | u32 clusters); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 158 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 159 | struct ocfs2_extent_rec *rec); |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 160 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 161 | u32 clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 162 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 163 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 164 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 165 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); |
| 166 | static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { |
| 167 | .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, |
| 168 | .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, |
| 169 | .eo_update_clusters = ocfs2_dinode_update_clusters, |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 170 | .eo_extent_map_insert = ocfs2_dinode_extent_map_insert, |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 171 | .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 172 | .eo_insert_check = ocfs2_dinode_insert_check, |
| 173 | .eo_sanity_check = ocfs2_dinode_sanity_check, |
| 174 | .eo_fill_root_el = ocfs2_dinode_fill_root_el, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 178 | u64 blkno) |
| 179 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 180 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 181 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 182 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 183 | di->i_last_eb_blk = cpu_to_le64(blkno); |
| 184 | } |
| 185 | |
| 186 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 187 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 188 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 189 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 190 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 191 | return le64_to_cpu(di->i_last_eb_blk); |
| 192 | } |
| 193 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 194 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 195 | u32 clusters) |
| 196 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 197 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 198 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 199 | |
| 200 | le32_add_cpu(&di->i_clusters, clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 201 | spin_lock(&oi->ip_lock); |
| 202 | oi->ip_clusters = le32_to_cpu(di->i_clusters); |
| 203 | spin_unlock(&oi->ip_lock); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 206 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 207 | struct ocfs2_extent_rec *rec) |
| 208 | { |
| 209 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 210 | |
| 211 | ocfs2_extent_map_insert_rec(inode, rec); |
| 212 | } |
| 213 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 214 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 215 | u32 clusters) |
| 216 | { |
| 217 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 218 | |
| 219 | ocfs2_extent_map_trunc(inode, clusters); |
| 220 | } |
| 221 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 222 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 223 | struct ocfs2_extent_rec *rec) |
| 224 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 225 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
| 226 | struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 227 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 228 | BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 229 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 230 | (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 231 | "Device %s, asking for sparse allocation: inode %llu, " |
| 232 | "cpos %u, clusters %u\n", |
| 233 | osb->dev_str, |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 234 | (unsigned long long)oi->ip_blkno, |
| 235 | rec->e_cpos, oi->ip_clusters); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 240 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 241 | { |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 242 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 243 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 244 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 245 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 246 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 247 | return 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 250 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) |
| 251 | { |
| 252 | struct ocfs2_dinode *di = et->et_object; |
| 253 | |
| 254 | et->et_root_el = &di->id2.i_list; |
| 255 | } |
| 256 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 257 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 258 | static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) |
| 259 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 260 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 261 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 262 | et->et_root_el = &vb->vb_xv->xr_list; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 265 | static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 266 | u64 blkno) |
| 267 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 268 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 269 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 270 | vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 274 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 275 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 276 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 277 | return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 278 | } |
| 279 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 280 | static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 281 | u32 clusters) |
| 282 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 283 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 284 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 285 | le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 286 | } |
| 287 | |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 288 | static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 289 | .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, |
| 290 | .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, |
| 291 | .eo_update_clusters = ocfs2_xattr_value_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 292 | .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 293 | }; |
| 294 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 295 | static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 296 | { |
| 297 | struct ocfs2_xattr_block *xb = et->et_object; |
| 298 | |
| 299 | et->et_root_el = &xb->xb_attrs.xb_root.xt_list; |
| 300 | } |
| 301 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 302 | 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] | 303 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 304 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 305 | et->et_max_leaf_clusters = |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 306 | ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 309 | static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 310 | u64 blkno) |
| 311 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 312 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 313 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 314 | |
| 315 | xt->xt_last_eb_blk = cpu_to_le64(blkno); |
| 316 | } |
| 317 | |
| 318 | static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 319 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 320 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 321 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 322 | |
| 323 | return le64_to_cpu(xt->xt_last_eb_blk); |
| 324 | } |
| 325 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 326 | static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 327 | u32 clusters) |
| 328 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 329 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 330 | |
| 331 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); |
| 332 | } |
| 333 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 334 | static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 335 | .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, |
| 336 | .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, |
| 337 | .eo_update_clusters = ocfs2_xattr_tree_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 338 | .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 339 | .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 340 | }; |
| 341 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 342 | static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 343 | u64 blkno) |
| 344 | { |
| 345 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 346 | |
| 347 | dx_root->dr_last_eb_blk = cpu_to_le64(blkno); |
| 348 | } |
| 349 | |
| 350 | static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 351 | { |
| 352 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 353 | |
| 354 | return le64_to_cpu(dx_root->dr_last_eb_blk); |
| 355 | } |
| 356 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 357 | static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 358 | u32 clusters) |
| 359 | { |
| 360 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 361 | |
| 362 | le32_add_cpu(&dx_root->dr_clusters, clusters); |
| 363 | } |
| 364 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 365 | static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 366 | { |
| 367 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 368 | |
| 369 | BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) |
| 375 | { |
| 376 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 377 | |
| 378 | et->et_root_el = &dx_root->dr_list; |
| 379 | } |
| 380 | |
| 381 | static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { |
| 382 | .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, |
| 383 | .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, |
| 384 | .eo_update_clusters = ocfs2_dx_root_update_clusters, |
| 385 | .eo_sanity_check = ocfs2_dx_root_sanity_check, |
| 386 | .eo_fill_root_el = ocfs2_dx_root_fill_root_el, |
| 387 | }; |
| 388 | |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame] | 389 | static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 390 | { |
| 391 | struct ocfs2_refcount_block *rb = et->et_object; |
| 392 | |
| 393 | et->et_root_el = &rb->rf_list; |
| 394 | } |
| 395 | |
| 396 | static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 397 | u64 blkno) |
| 398 | { |
| 399 | struct ocfs2_refcount_block *rb = et->et_object; |
| 400 | |
| 401 | rb->rf_last_eb_blk = cpu_to_le64(blkno); |
| 402 | } |
| 403 | |
| 404 | static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 405 | { |
| 406 | struct ocfs2_refcount_block *rb = et->et_object; |
| 407 | |
| 408 | return le64_to_cpu(rb->rf_last_eb_blk); |
| 409 | } |
| 410 | |
| 411 | static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et, |
| 412 | u32 clusters) |
| 413 | { |
| 414 | struct ocfs2_refcount_block *rb = et->et_object; |
| 415 | |
| 416 | le32_add_cpu(&rb->rf_clusters, clusters); |
| 417 | } |
| 418 | |
| 419 | static enum ocfs2_contig_type |
| 420 | ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et, |
| 421 | struct ocfs2_extent_rec *ext, |
| 422 | struct ocfs2_extent_rec *insert_rec) |
| 423 | { |
| 424 | return CONTIG_NONE; |
| 425 | } |
| 426 | |
| 427 | static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = { |
| 428 | .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk, |
| 429 | .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk, |
| 430 | .eo_update_clusters = ocfs2_refcount_tree_update_clusters, |
| 431 | .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el, |
| 432 | .eo_extent_contig = ocfs2_refcount_tree_extent_contig, |
| 433 | }; |
| 434 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 435 | static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 436 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 437 | struct buffer_head *bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 438 | ocfs2_journal_access_func access, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 439 | void *obj, |
| 440 | struct ocfs2_extent_tree_operations *ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 441 | { |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 442 | et->et_ops = ops; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 443 | et->et_root_bh = bh; |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 444 | et->et_ci = ci; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 445 | et->et_root_journal_access = access; |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 446 | if (!obj) |
| 447 | obj = (void *)bh->b_data; |
| 448 | et->et_object = obj; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 449 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 450 | et->et_ops->eo_fill_root_el(et); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 451 | if (!et->et_ops->eo_fill_max_leaf_clusters) |
| 452 | et->et_max_leaf_clusters = 0; |
| 453 | else |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 454 | et->et_ops->eo_fill_max_leaf_clusters(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 455 | } |
| 456 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 457 | void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 458 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 459 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 460 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 461 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 462 | NULL, &ocfs2_dinode_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 465 | void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 466 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 467 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 468 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 469 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 470 | NULL, &ocfs2_xattr_tree_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 473 | void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 474 | struct ocfs2_caching_info *ci, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 475 | struct ocfs2_xattr_value_buf *vb) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 476 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 477 | __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] | 478 | &ocfs2_xattr_value_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 481 | void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 482 | struct ocfs2_caching_info *ci, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 483 | struct buffer_head *bh) |
| 484 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 485 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 486 | NULL, &ocfs2_dx_root_et_ops); |
| 487 | } |
| 488 | |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame] | 489 | void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et, |
| 490 | struct ocfs2_caching_info *ci, |
| 491 | struct buffer_head *bh) |
| 492 | { |
| 493 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb, |
| 494 | NULL, &ocfs2_refcount_tree_et_ops); |
| 495 | } |
| 496 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 497 | static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 498 | u64 new_last_eb_blk) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 499 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 500 | 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] | 501 | } |
| 502 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 503 | 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] | 504 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 505 | return et->et_ops->eo_get_last_eb_blk(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 506 | } |
| 507 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 508 | static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 509 | u32 clusters) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 510 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 511 | et->et_ops->eo_update_clusters(et, clusters); |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 514 | static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et, |
| 515 | struct ocfs2_extent_rec *rec) |
| 516 | { |
| 517 | if (et->et_ops->eo_extent_map_insert) |
| 518 | et->et_ops->eo_extent_map_insert(et, rec); |
| 519 | } |
| 520 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 521 | static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 522 | u32 clusters) |
| 523 | { |
| 524 | if (et->et_ops->eo_extent_map_truncate) |
| 525 | et->et_ops->eo_extent_map_truncate(et, clusters); |
| 526 | } |
| 527 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 528 | static inline int ocfs2_et_root_journal_access(handle_t *handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 529 | struct ocfs2_extent_tree *et, |
| 530 | int type) |
| 531 | { |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 532 | 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] | 533 | type); |
| 534 | } |
| 535 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 536 | static inline enum ocfs2_contig_type |
| 537 | ocfs2_et_extent_contig(struct ocfs2_extent_tree *et, |
| 538 | struct ocfs2_extent_rec *rec, |
| 539 | struct ocfs2_extent_rec *insert_rec) |
| 540 | { |
| 541 | if (et->et_ops->eo_extent_contig) |
| 542 | return et->et_ops->eo_extent_contig(et, rec, insert_rec); |
| 543 | |
| 544 | return ocfs2_extent_rec_contig( |
| 545 | ocfs2_metadata_cache_get_super(et->et_ci), |
| 546 | rec, insert_rec); |
| 547 | } |
| 548 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 549 | static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 550 | struct ocfs2_extent_rec *rec) |
| 551 | { |
| 552 | int ret = 0; |
| 553 | |
| 554 | if (et->et_ops->eo_insert_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 555 | ret = et->et_ops->eo_insert_check(et, rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 556 | return ret; |
| 557 | } |
| 558 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 559 | static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 560 | { |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 561 | int ret = 0; |
| 562 | |
| 563 | if (et->et_ops->eo_sanity_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 564 | ret = et->et_ops->eo_sanity_check(et); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 565 | return ret; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 568 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 569 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 570 | struct ocfs2_extent_block *eb); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 571 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 572 | struct ocfs2_extent_tree *et, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 573 | struct ocfs2_path *path, |
| 574 | struct ocfs2_extent_rec *insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 575 | /* |
| 576 | * Reset the actual path elements so that we can re-use the structure |
| 577 | * to build another path. Generally, this involves freeing the buffer |
| 578 | * heads. |
| 579 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 580 | void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 581 | { |
| 582 | int i, start = 0, depth = 0; |
| 583 | struct ocfs2_path_item *node; |
| 584 | |
| 585 | if (keep_root) |
| 586 | start = 1; |
| 587 | |
| 588 | for(i = start; i < path_num_items(path); i++) { |
| 589 | node = &path->p_node[i]; |
| 590 | |
| 591 | brelse(node->bh); |
| 592 | node->bh = NULL; |
| 593 | node->el = NULL; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Tree depth may change during truncate, or insert. If we're |
| 598 | * keeping the root extent list, then make sure that our path |
| 599 | * structure reflects the proper depth. |
| 600 | */ |
| 601 | if (keep_root) |
| 602 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 603 | else |
| 604 | path_root_access(path) = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 605 | |
| 606 | path->p_tree_depth = depth; |
| 607 | } |
| 608 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 609 | void ocfs2_free_path(struct ocfs2_path *path) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 610 | { |
| 611 | if (path) { |
| 612 | ocfs2_reinit_path(path, 0); |
| 613 | kfree(path); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 618 | * All the elements of src into dest. After this call, src could be freed |
| 619 | * without affecting dest. |
| 620 | * |
| 621 | * Both paths should have the same root. Any non-root elements of dest |
| 622 | * will be freed. |
| 623 | */ |
| 624 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 625 | { |
| 626 | int i; |
| 627 | |
| 628 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 629 | BUG_ON(path_root_el(dest) != path_root_el(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 630 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 631 | |
| 632 | ocfs2_reinit_path(dest, 1); |
| 633 | |
| 634 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 635 | dest->p_node[i].bh = src->p_node[i].bh; |
| 636 | dest->p_node[i].el = src->p_node[i].el; |
| 637 | |
| 638 | if (dest->p_node[i].bh) |
| 639 | get_bh(dest->p_node[i].bh); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 644 | * Make the *dest path the same as src and re-initialize src path to |
| 645 | * have a root only. |
| 646 | */ |
| 647 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 648 | { |
| 649 | int i; |
| 650 | |
| 651 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 652 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 653 | |
| 654 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 655 | brelse(dest->p_node[i].bh); |
| 656 | |
| 657 | dest->p_node[i].bh = src->p_node[i].bh; |
| 658 | dest->p_node[i].el = src->p_node[i].el; |
| 659 | |
| 660 | src->p_node[i].bh = NULL; |
| 661 | src->p_node[i].el = NULL; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Insert an extent block at given index. |
| 667 | * |
| 668 | * This will not take an additional reference on eb_bh. |
| 669 | */ |
| 670 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, |
| 671 | struct buffer_head *eb_bh) |
| 672 | { |
| 673 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; |
| 674 | |
| 675 | /* |
| 676 | * Right now, no root bh is an extent block, so this helps |
| 677 | * catch code errors with dinode trees. The assertion can be |
| 678 | * safely removed if we ever need to insert extent block |
| 679 | * structures at the root. |
| 680 | */ |
| 681 | BUG_ON(index == 0); |
| 682 | |
| 683 | path->p_node[index].bh = eb_bh; |
| 684 | path->p_node[index].el = &eb->h_list; |
| 685 | } |
| 686 | |
| 687 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 688 | struct ocfs2_extent_list *root_el, |
| 689 | ocfs2_journal_access_func access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 690 | { |
| 691 | struct ocfs2_path *path; |
| 692 | |
| 693 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
| 694 | |
| 695 | path = kzalloc(sizeof(*path), GFP_NOFS); |
| 696 | if (path) { |
| 697 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); |
| 698 | get_bh(root_bh); |
| 699 | path_root_bh(path) = root_bh; |
| 700 | path_root_el(path) = root_el; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 701 | path_root_access(path) = access; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | return path; |
| 705 | } |
| 706 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 707 | struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 708 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 709 | return ocfs2_new_path(path_root_bh(path), path_root_el(path), |
| 710 | path_root_access(path)); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 713 | struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et) |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 714 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 715 | return ocfs2_new_path(et->et_root_bh, et->et_root_el, |
| 716 | et->et_root_journal_access); |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Journal the buffer at depth idx. All idx>0 are extent_blocks, |
| 721 | * otherwise it's the root_access function. |
| 722 | * |
| 723 | * I don't like the way this function's name looks next to |
| 724 | * ocfs2_journal_access_path(), but I don't have a better one. |
| 725 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 726 | int ocfs2_path_bh_journal_access(handle_t *handle, |
| 727 | struct ocfs2_caching_info *ci, |
| 728 | struct ocfs2_path *path, |
| 729 | int idx) |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 730 | { |
| 731 | ocfs2_journal_access_func access = path_root_access(path); |
| 732 | |
| 733 | if (!access) |
| 734 | access = ocfs2_journal_access; |
| 735 | |
| 736 | if (idx) |
| 737 | access = ocfs2_journal_access_eb; |
| 738 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 739 | return access(handle, ci, path->p_node[idx].bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 740 | OCFS2_JOURNAL_ACCESS_WRITE); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 743 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 744 | * Convenience function to journal all components in a path. |
| 745 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 746 | int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, |
| 747 | handle_t *handle, |
| 748 | struct ocfs2_path *path) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 749 | { |
| 750 | int i, ret = 0; |
| 751 | |
| 752 | if (!path) |
| 753 | goto out; |
| 754 | |
| 755 | for(i = 0; i < path_num_items(path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 756 | ret = ocfs2_path_bh_journal_access(handle, ci, path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 757 | if (ret < 0) { |
| 758 | mlog_errno(ret); |
| 759 | goto out; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | out: |
| 764 | return ret; |
| 765 | } |
| 766 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 767 | /* |
| 768 | * Return the index of the extent record which contains cluster #v_cluster. |
| 769 | * -1 is returned if it was not found. |
| 770 | * |
| 771 | * Should work fine on interior and exterior nodes. |
| 772 | */ |
| 773 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) |
| 774 | { |
| 775 | int ret = -1; |
| 776 | int i; |
| 777 | struct ocfs2_extent_rec *rec; |
| 778 | u32 rec_end, rec_start, clusters; |
| 779 | |
| 780 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 781 | rec = &el->l_recs[i]; |
| 782 | |
| 783 | rec_start = le32_to_cpu(rec->e_cpos); |
| 784 | clusters = ocfs2_rec_clusters(el, rec); |
| 785 | |
| 786 | rec_end = rec_start + clusters; |
| 787 | |
| 788 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 789 | ret = i; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return ret; |
| 795 | } |
| 796 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 797 | /* |
| 798 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 799 | * ocfs2_extent_rec_contig only work properly against leaf nodes! |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 800 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 801 | static int ocfs2_block_extent_contig(struct super_block *sb, |
| 802 | struct ocfs2_extent_rec *ext, |
| 803 | u64 blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 804 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 805 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
| 806 | |
| 807 | blk_end += ocfs2_clusters_to_blocks(sb, |
| 808 | le16_to_cpu(ext->e_leaf_clusters)); |
| 809 | |
| 810 | return blkno == blk_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 813 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
| 814 | struct ocfs2_extent_rec *right) |
| 815 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 816 | u32 left_range; |
| 817 | |
| 818 | left_range = le32_to_cpu(left->e_cpos) + |
| 819 | le16_to_cpu(left->e_leaf_clusters); |
| 820 | |
| 821 | return (left_range == le32_to_cpu(right->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static enum ocfs2_contig_type |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 825 | ocfs2_extent_rec_contig(struct super_block *sb, |
| 826 | struct ocfs2_extent_rec *ext, |
| 827 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 828 | { |
| 829 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); |
| 830 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 831 | /* |
| 832 | * Refuse to coalesce extent records with different flag |
| 833 | * fields - we don't want to mix unwritten extents with user |
| 834 | * data. |
| 835 | */ |
| 836 | if (ext->e_flags != insert_rec->e_flags) |
| 837 | return CONTIG_NONE; |
| 838 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 839 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 840 | ocfs2_block_extent_contig(sb, ext, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 841 | return CONTIG_RIGHT; |
| 842 | |
| 843 | blkno = le64_to_cpu(ext->e_blkno); |
| 844 | if (ocfs2_extents_adjacent(insert_rec, ext) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 845 | ocfs2_block_extent_contig(sb, insert_rec, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 846 | return CONTIG_LEFT; |
| 847 | |
| 848 | return CONTIG_NONE; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * NOTE: We can have pretty much any combination of contiguousness and |
| 853 | * appending. |
| 854 | * |
| 855 | * The usefulness of APPEND_TAIL is more in that it lets us know that |
| 856 | * we'll have to update the path to that leaf. |
| 857 | */ |
| 858 | enum ocfs2_append_type { |
| 859 | APPEND_NONE = 0, |
| 860 | APPEND_TAIL, |
| 861 | }; |
| 862 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 863 | enum ocfs2_split_type { |
| 864 | SPLIT_NONE = 0, |
| 865 | SPLIT_LEFT, |
| 866 | SPLIT_RIGHT, |
| 867 | }; |
| 868 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 869 | struct ocfs2_insert_type { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 870 | enum ocfs2_split_type ins_split; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 871 | enum ocfs2_append_type ins_appending; |
| 872 | enum ocfs2_contig_type ins_contig; |
| 873 | int ins_contig_index; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 874 | int ins_tree_depth; |
| 875 | }; |
| 876 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 877 | struct ocfs2_merge_ctxt { |
| 878 | enum ocfs2_contig_type c_contig_type; |
| 879 | int c_has_empty_extent; |
| 880 | int c_split_covers_rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 881 | }; |
| 882 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 883 | static int ocfs2_validate_extent_block(struct super_block *sb, |
| 884 | struct buffer_head *bh) |
| 885 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 886 | int rc; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 887 | struct ocfs2_extent_block *eb = |
| 888 | (struct ocfs2_extent_block *)bh->b_data; |
| 889 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 890 | mlog(0, "Validating extent block %llu\n", |
| 891 | (unsigned long long)bh->b_blocknr); |
| 892 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 893 | BUG_ON(!buffer_uptodate(bh)); |
| 894 | |
| 895 | /* |
| 896 | * If the ecc fails, we return the error but otherwise |
| 897 | * leave the filesystem running. We know any error is |
| 898 | * local to this block. |
| 899 | */ |
| 900 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 901 | if (rc) { |
| 902 | mlog(ML_ERROR, "Checksum failed for extent block %llu\n", |
| 903 | (unsigned long long)bh->b_blocknr); |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 904 | return rc; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 905 | } |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 906 | |
| 907 | /* |
| 908 | * Errors after here are fatal. |
| 909 | */ |
| 910 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 911 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 912 | ocfs2_error(sb, |
| 913 | "Extent block #%llu has bad signature %.*s", |
| 914 | (unsigned long long)bh->b_blocknr, 7, |
| 915 | eb->h_signature); |
| 916 | return -EINVAL; |
| 917 | } |
| 918 | |
| 919 | if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { |
| 920 | ocfs2_error(sb, |
| 921 | "Extent block #%llu has an invalid h_blkno " |
| 922 | "of %llu", |
| 923 | (unsigned long long)bh->b_blocknr, |
| 924 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 925 | return -EINVAL; |
| 926 | } |
| 927 | |
| 928 | if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 929 | ocfs2_error(sb, |
| 930 | "Extent block #%llu has an invalid " |
| 931 | "h_fs_generation of #%u", |
| 932 | (unsigned long long)bh->b_blocknr, |
| 933 | le32_to_cpu(eb->h_fs_generation)); |
| 934 | return -EINVAL; |
| 935 | } |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 940 | 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] | 941 | struct buffer_head **bh) |
| 942 | { |
| 943 | int rc; |
| 944 | struct buffer_head *tmp = *bh; |
| 945 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 946 | rc = ocfs2_read_block(ci, eb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 947 | ocfs2_validate_extent_block); |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 948 | |
| 949 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 950 | if (!rc && !*bh) |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 951 | *bh = tmp; |
| 952 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 953 | return rc; |
| 954 | } |
| 955 | |
| 956 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 957 | /* |
| 958 | * How many free extents have we got before we need more meta data? |
| 959 | */ |
| 960 | int ocfs2_num_free_extents(struct ocfs2_super *osb, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 961 | struct ocfs2_extent_tree *et) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 962 | { |
| 963 | int retval; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 964 | struct ocfs2_extent_list *el = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 965 | struct ocfs2_extent_block *eb; |
| 966 | struct buffer_head *eb_bh = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 967 | u64 last_eb_blk = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 968 | |
| 969 | mlog_entry_void(); |
| 970 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 971 | el = et->et_root_el; |
| 972 | last_eb_blk = ocfs2_et_get_last_eb_blk(et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 973 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 974 | if (last_eb_blk) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 975 | retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, |
| 976 | &eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 977 | if (retval < 0) { |
| 978 | mlog_errno(retval); |
| 979 | goto bail; |
| 980 | } |
| 981 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 982 | el = &eb->h_list; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 983 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 984 | |
| 985 | BUG_ON(el->l_tree_depth != 0); |
| 986 | |
| 987 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); |
| 988 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 989 | brelse(eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 990 | |
| 991 | mlog_exit(retval); |
| 992 | return retval; |
| 993 | } |
| 994 | |
| 995 | /* expects array to already be allocated |
| 996 | * |
| 997 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and |
| 998 | * l_count for you |
| 999 | */ |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1000 | static int ocfs2_create_new_meta_bhs(handle_t *handle, |
| 1001 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1002 | int wanted, |
| 1003 | struct ocfs2_alloc_context *meta_ac, |
| 1004 | struct buffer_head *bhs[]) |
| 1005 | { |
| 1006 | int count, status, i; |
| 1007 | u16 suballoc_bit_start; |
| 1008 | u32 num_got; |
| 1009 | u64 first_blkno; |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1010 | struct ocfs2_super *osb = |
| 1011 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1012 | struct ocfs2_extent_block *eb; |
| 1013 | |
| 1014 | mlog_entry_void(); |
| 1015 | |
| 1016 | count = 0; |
| 1017 | while (count < wanted) { |
| 1018 | status = ocfs2_claim_metadata(osb, |
| 1019 | handle, |
| 1020 | meta_ac, |
| 1021 | wanted - count, |
| 1022 | &suballoc_bit_start, |
| 1023 | &num_got, |
| 1024 | &first_blkno); |
| 1025 | if (status < 0) { |
| 1026 | mlog_errno(status); |
| 1027 | goto bail; |
| 1028 | } |
| 1029 | |
| 1030 | for(i = count; i < (num_got + count); i++) { |
| 1031 | bhs[i] = sb_getblk(osb->sb, first_blkno); |
| 1032 | if (bhs[i] == NULL) { |
| 1033 | status = -EIO; |
| 1034 | mlog_errno(status); |
| 1035 | goto bail; |
| 1036 | } |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1037 | ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1038 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1039 | status = ocfs2_journal_access_eb(handle, et->et_ci, |
| 1040 | bhs[i], |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1041 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1042 | if (status < 0) { |
| 1043 | mlog_errno(status); |
| 1044 | goto bail; |
| 1045 | } |
| 1046 | |
| 1047 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); |
| 1048 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; |
| 1049 | /* Ok, setup the minimal stuff here. */ |
| 1050 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); |
| 1051 | eb->h_blkno = cpu_to_le64(first_blkno); |
| 1052 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1053 | eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1054 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 1055 | eb->h_list.l_count = |
| 1056 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 1057 | |
| 1058 | suballoc_bit_start++; |
| 1059 | first_blkno++; |
| 1060 | |
| 1061 | /* We'll also be dirtied by the caller, so |
| 1062 | * this isn't absolutely necessary. */ |
| 1063 | status = ocfs2_journal_dirty(handle, bhs[i]); |
| 1064 | if (status < 0) { |
| 1065 | mlog_errno(status); |
| 1066 | goto bail; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | count += num_got; |
| 1071 | } |
| 1072 | |
| 1073 | status = 0; |
| 1074 | bail: |
| 1075 | if (status < 0) { |
| 1076 | for(i = 0; i < wanted; i++) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1077 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1078 | bhs[i] = NULL; |
| 1079 | } |
| 1080 | } |
| 1081 | mlog_exit(status); |
| 1082 | return status; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1086 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 1087 | * |
| 1088 | * Returns the sum of the rightmost extent rec logical offset and |
| 1089 | * cluster count. |
| 1090 | * |
| 1091 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 1092 | * value should be populated into the leftmost new branch records. |
| 1093 | * |
| 1094 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 1095 | * value for the new topmost tree record. |
| 1096 | */ |
| 1097 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 1098 | { |
| 1099 | int i; |
| 1100 | |
| 1101 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1102 | |
| 1103 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1104 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1108 | * Change range of the branches in the right most path according to the leaf |
| 1109 | * extent block's rightmost record. |
| 1110 | */ |
| 1111 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1112 | struct ocfs2_extent_tree *et) |
| 1113 | { |
| 1114 | int status; |
| 1115 | struct ocfs2_path *path = NULL; |
| 1116 | struct ocfs2_extent_list *el; |
| 1117 | struct ocfs2_extent_rec *rec; |
| 1118 | |
| 1119 | path = ocfs2_new_path_from_et(et); |
| 1120 | if (!path) { |
| 1121 | status = -ENOMEM; |
| 1122 | return status; |
| 1123 | } |
| 1124 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1125 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1126 | if (status < 0) { |
| 1127 | mlog_errno(status); |
| 1128 | goto out; |
| 1129 | } |
| 1130 | |
| 1131 | status = ocfs2_extend_trans(handle, path_num_items(path) + |
| 1132 | handle->h_buffer_credits); |
| 1133 | if (status < 0) { |
| 1134 | mlog_errno(status); |
| 1135 | goto out; |
| 1136 | } |
| 1137 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1138 | status = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1139 | if (status < 0) { |
| 1140 | mlog_errno(status); |
| 1141 | goto out; |
| 1142 | } |
| 1143 | |
| 1144 | el = path_leaf_el(path); |
| 1145 | rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; |
| 1146 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1147 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1148 | |
| 1149 | out: |
| 1150 | ocfs2_free_path(path); |
| 1151 | return status; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1155 | * 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] | 1156 | * 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] | 1157 | * structure. |
| 1158 | * |
| 1159 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1160 | * for the new last extent block. |
| 1161 | * |
| 1162 | * 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] | 1163 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1164 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1165 | static int ocfs2_add_branch(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1166 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1167 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1168 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1169 | struct ocfs2_alloc_context *meta_ac) |
| 1170 | { |
| 1171 | int status, new_blocks, i; |
| 1172 | u64 next_blkno, new_last_eb_blk; |
| 1173 | struct buffer_head *bh; |
| 1174 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1175 | struct ocfs2_extent_block *eb; |
| 1176 | struct ocfs2_extent_list *eb_el; |
| 1177 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1178 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1179 | |
| 1180 | mlog_entry_void(); |
| 1181 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1182 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1183 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1184 | if (eb_bh) { |
| 1185 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1186 | el = &eb->h_list; |
| 1187 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1188 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1189 | |
| 1190 | /* we never add a branch to a leaf. */ |
| 1191 | BUG_ON(!el->l_tree_depth); |
| 1192 | |
| 1193 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1194 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1195 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1196 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1197 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1198 | |
| 1199 | /* |
| 1200 | * If there is a gap before the root end and the real end |
| 1201 | * of the righmost leaf block, we need to remove the gap |
| 1202 | * between new_cpos and root_end first so that the tree |
| 1203 | * is consistent after we add a new branch(it will start |
| 1204 | * from new_cpos). |
| 1205 | */ |
| 1206 | if (root_end > new_cpos) { |
| 1207 | mlog(0, "adjust the cluster end from %u to %u\n", |
| 1208 | root_end, new_cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1209 | status = ocfs2_adjust_rightmost_branch(handle, et); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1210 | if (status) { |
| 1211 | mlog_errno(status); |
| 1212 | goto bail; |
| 1213 | } |
| 1214 | } |
| 1215 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1216 | /* allocate the number of new eb blocks we need */ |
| 1217 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1218 | GFP_KERNEL); |
| 1219 | if (!new_eb_bhs) { |
| 1220 | status = -ENOMEM; |
| 1221 | mlog_errno(status); |
| 1222 | goto bail; |
| 1223 | } |
| 1224 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1225 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1226 | meta_ac, new_eb_bhs); |
| 1227 | if (status < 0) { |
| 1228 | mlog_errno(status); |
| 1229 | goto bail; |
| 1230 | } |
| 1231 | |
| 1232 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1233 | * linked with the rest of the tree. |
| 1234 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1235 | * |
| 1236 | * when we leave the loop, new_last_eb_blk will point to the |
| 1237 | * newest leaf, and next_blkno will point to the topmost extent |
| 1238 | * block. */ |
| 1239 | next_blkno = new_last_eb_blk = 0; |
| 1240 | for(i = 0; i < new_blocks; i++) { |
| 1241 | bh = new_eb_bhs[i]; |
| 1242 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1243 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1244 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1245 | eb_el = &eb->h_list; |
| 1246 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1247 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1248 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1249 | if (status < 0) { |
| 1250 | mlog_errno(status); |
| 1251 | goto bail; |
| 1252 | } |
| 1253 | |
| 1254 | eb->h_next_leaf_blk = 0; |
| 1255 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1256 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1257 | /* |
| 1258 | * This actually counts as an empty extent as |
| 1259 | * c_clusters == 0 |
| 1260 | */ |
| 1261 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1262 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1263 | /* |
| 1264 | * eb_el isn't always an interior node, but even leaf |
| 1265 | * nodes want a zero'd flags and reserved field so |
| 1266 | * this gets the whole 32 bits regardless of use. |
| 1267 | */ |
| 1268 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1269 | if (!eb_el->l_tree_depth) |
| 1270 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1271 | |
| 1272 | status = ocfs2_journal_dirty(handle, bh); |
| 1273 | if (status < 0) { |
| 1274 | mlog_errno(status); |
| 1275 | goto bail; |
| 1276 | } |
| 1277 | |
| 1278 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1279 | } |
| 1280 | |
| 1281 | /* This is a bit hairy. We want to update up to three blocks |
| 1282 | * here without leaving any of them in an inconsistent state |
| 1283 | * in case of error. We don't have to worry about |
| 1284 | * journal_dirty erroring as it won't unless we've aborted the |
| 1285 | * handle (in which case we would never be here) so reserving |
| 1286 | * the write with journal_access is all we need to do. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1287 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1288 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1289 | if (status < 0) { |
| 1290 | mlog_errno(status); |
| 1291 | goto bail; |
| 1292 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1293 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1294 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1295 | if (status < 0) { |
| 1296 | mlog_errno(status); |
| 1297 | goto bail; |
| 1298 | } |
| 1299 | if (eb_bh) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1300 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1301 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1302 | if (status < 0) { |
| 1303 | mlog_errno(status); |
| 1304 | goto bail; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1309 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1310 | i = le16_to_cpu(el->l_next_free_rec); |
| 1311 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1312 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1313 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1314 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1315 | |
| 1316 | /* fe needs a new last extent block pointer, as does the |
| 1317 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1318 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1319 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1320 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1321 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1322 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1323 | status = ocfs2_journal_dirty(handle, *last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1324 | if (status < 0) |
| 1325 | mlog_errno(status); |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1326 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1327 | if (status < 0) |
| 1328 | mlog_errno(status); |
| 1329 | if (eb_bh) { |
| 1330 | status = ocfs2_journal_dirty(handle, eb_bh); |
| 1331 | if (status < 0) |
| 1332 | mlog_errno(status); |
| 1333 | } |
| 1334 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1335 | /* |
| 1336 | * Some callers want to track the rightmost leaf so pass it |
| 1337 | * back here. |
| 1338 | */ |
| 1339 | brelse(*last_eb_bh); |
| 1340 | get_bh(new_eb_bhs[0]); |
| 1341 | *last_eb_bh = new_eb_bhs[0]; |
| 1342 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1343 | status = 0; |
| 1344 | bail: |
| 1345 | if (new_eb_bhs) { |
| 1346 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1347 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1348 | kfree(new_eb_bhs); |
| 1349 | } |
| 1350 | |
| 1351 | mlog_exit(status); |
| 1352 | return status; |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * adds another level to the allocation tree. |
| 1357 | * returns back the new extent block so you can add a branch to it |
| 1358 | * after this call. |
| 1359 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1360 | static int ocfs2_shift_tree_depth(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1361 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1362 | struct ocfs2_alloc_context *meta_ac, |
| 1363 | struct buffer_head **ret_new_eb_bh) |
| 1364 | { |
| 1365 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1366 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1367 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1368 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1369 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1370 | struct ocfs2_extent_list *eb_el; |
| 1371 | |
| 1372 | mlog_entry_void(); |
| 1373 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1374 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1375 | &new_eb_bh); |
| 1376 | if (status < 0) { |
| 1377 | mlog_errno(status); |
| 1378 | goto bail; |
| 1379 | } |
| 1380 | |
| 1381 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1382 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1383 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1384 | |
| 1385 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1386 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1387 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1388 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1389 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1390 | if (status < 0) { |
| 1391 | mlog_errno(status); |
| 1392 | goto bail; |
| 1393 | } |
| 1394 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1395 | /* copy the root extent list data into the new extent block */ |
| 1396 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1397 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1398 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1399 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1400 | |
| 1401 | status = ocfs2_journal_dirty(handle, new_eb_bh); |
| 1402 | if (status < 0) { |
| 1403 | mlog_errno(status); |
| 1404 | goto bail; |
| 1405 | } |
| 1406 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1407 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1408 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1409 | if (status < 0) { |
| 1410 | mlog_errno(status); |
| 1411 | goto bail; |
| 1412 | } |
| 1413 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1414 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1415 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1416 | /* update root_bh now */ |
| 1417 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1418 | root_el->l_recs[0].e_cpos = 0; |
| 1419 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1420 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1421 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1422 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1423 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1424 | |
| 1425 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1426 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1427 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1428 | 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] | 1429 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1430 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1431 | if (status < 0) { |
| 1432 | mlog_errno(status); |
| 1433 | goto bail; |
| 1434 | } |
| 1435 | |
| 1436 | *ret_new_eb_bh = new_eb_bh; |
| 1437 | new_eb_bh = NULL; |
| 1438 | status = 0; |
| 1439 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1440 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1441 | |
| 1442 | mlog_exit(status); |
| 1443 | return status; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1447 | * Should only be called when there is no space left in any of the |
| 1448 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1449 | * non-leaf extent block with room for new records. There are three |
| 1450 | * valid results of this search: |
| 1451 | * |
| 1452 | * 1) a lowest extent block is found, then we pass it back in |
| 1453 | * *lowest_eb_bh and return '0' |
| 1454 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1455 | * 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] | 1456 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1457 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1458 | * 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] | 1459 | * which case we return > 0 |
| 1460 | * |
| 1461 | * return status < 0 indicates an error. |
| 1462 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1463 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1464 | struct buffer_head **target_bh) |
| 1465 | { |
| 1466 | int status = 0, i; |
| 1467 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1468 | struct ocfs2_extent_block *eb; |
| 1469 | struct ocfs2_extent_list *el; |
| 1470 | struct buffer_head *bh = NULL; |
| 1471 | struct buffer_head *lowest_bh = NULL; |
| 1472 | |
| 1473 | mlog_entry_void(); |
| 1474 | |
| 1475 | *target_bh = NULL; |
| 1476 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1477 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1478 | |
| 1479 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1480 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1481 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1482 | "Owner %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1483 | "extent list (next_free_rec == 0)", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1484 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1485 | status = -EIO; |
| 1486 | goto bail; |
| 1487 | } |
| 1488 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1489 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1490 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1491 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1492 | "Owner %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1493 | "list where extent # %d has no physical " |
| 1494 | "block start", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1495 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1496 | status = -EIO; |
| 1497 | goto bail; |
| 1498 | } |
| 1499 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1500 | brelse(bh); |
| 1501 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1502 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1503 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1504 | if (status < 0) { |
| 1505 | mlog_errno(status); |
| 1506 | goto bail; |
| 1507 | } |
| 1508 | |
| 1509 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1510 | el = &eb->h_list; |
| 1511 | |
| 1512 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1513 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1514 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1515 | lowest_bh = bh; |
| 1516 | get_bh(lowest_bh); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /* If we didn't find one and the fe doesn't have any room, |
| 1521 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1522 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1523 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1524 | status = 1; |
| 1525 | |
| 1526 | *target_bh = lowest_bh; |
| 1527 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1528 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1529 | |
| 1530 | mlog_exit(status); |
| 1531 | return status; |
| 1532 | } |
| 1533 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1534 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1535 | * Grow a b-tree so that it has more records. |
| 1536 | * |
| 1537 | * We might shift the tree depth in which case existing paths should |
| 1538 | * be considered invalid. |
| 1539 | * |
| 1540 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1541 | * |
| 1542 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1543 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1544 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 1545 | int *final_depth, struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1546 | struct ocfs2_alloc_context *meta_ac) |
| 1547 | { |
| 1548 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1549 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1550 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1551 | struct buffer_head *bh = NULL; |
| 1552 | |
| 1553 | BUG_ON(meta_ac == NULL); |
| 1554 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1555 | shift = ocfs2_find_branch_target(et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1556 | if (shift < 0) { |
| 1557 | ret = shift; |
| 1558 | mlog_errno(ret); |
| 1559 | goto out; |
| 1560 | } |
| 1561 | |
| 1562 | /* We traveled all the way to the bottom of the allocation tree |
| 1563 | * and didn't find room for any more extents - we need to add |
| 1564 | * another tree level */ |
| 1565 | if (shift) { |
| 1566 | BUG_ON(bh); |
| 1567 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 1568 | |
| 1569 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1570 | * the new extent block (so we can pass that to |
| 1571 | * ocfs2_add_branch). */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1572 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1573 | if (ret < 0) { |
| 1574 | mlog_errno(ret); |
| 1575 | goto out; |
| 1576 | } |
| 1577 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1578 | if (depth == 1) { |
| 1579 | /* |
| 1580 | * Special case: we have room now if we shifted from |
| 1581 | * tree_depth 0, so no more work needs to be done. |
| 1582 | * |
| 1583 | * We won't be calling add_branch, so pass |
| 1584 | * back *last_eb_bh as the new leaf. At depth |
| 1585 | * zero, it should always be null so there's |
| 1586 | * no reason to brelse. |
| 1587 | */ |
| 1588 | BUG_ON(*last_eb_bh); |
| 1589 | get_bh(bh); |
| 1590 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1591 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1592 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1596 | * the new data. */ |
| 1597 | mlog(0, "add branch. bh = %p\n", bh); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1598 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1599 | meta_ac); |
| 1600 | if (ret < 0) { |
| 1601 | mlog_errno(ret); |
| 1602 | goto out; |
| 1603 | } |
| 1604 | |
| 1605 | out: |
| 1606 | if (final_depth) |
| 1607 | *final_depth = depth; |
| 1608 | brelse(bh); |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
| 1612 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1613 | * This function will discard the rightmost extent record. |
| 1614 | */ |
| 1615 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1616 | { |
| 1617 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1618 | int count = le16_to_cpu(el->l_count); |
| 1619 | unsigned int num_bytes; |
| 1620 | |
| 1621 | BUG_ON(!next_free); |
| 1622 | /* This will cause us to go off the end of our extent list. */ |
| 1623 | BUG_ON(next_free >= count); |
| 1624 | |
| 1625 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1626 | |
| 1627 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1628 | } |
| 1629 | |
| 1630 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1631 | struct ocfs2_extent_rec *insert_rec) |
| 1632 | { |
| 1633 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1634 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1635 | struct ocfs2_extent_rec *rec; |
| 1636 | |
| 1637 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1638 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1639 | |
| 1640 | BUG_ON(!next_free); |
| 1641 | |
| 1642 | /* 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] | 1643 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1644 | |
| 1645 | /* |
| 1646 | * The easiest way to approach this is to just remove the |
| 1647 | * empty extent and temporarily decrement next_free. |
| 1648 | */ |
| 1649 | if (has_empty) { |
| 1650 | /* |
| 1651 | * If next_free was 1 (only an empty extent), this |
| 1652 | * loop won't execute, which is fine. We still want |
| 1653 | * the decrement above to happen. |
| 1654 | */ |
| 1655 | for(i = 0; i < (next_free - 1); i++) |
| 1656 | el->l_recs[i] = el->l_recs[i+1]; |
| 1657 | |
| 1658 | next_free--; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * Figure out what the new record index should be. |
| 1663 | */ |
| 1664 | for(i = 0; i < next_free; i++) { |
| 1665 | rec = &el->l_recs[i]; |
| 1666 | |
| 1667 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1668 | break; |
| 1669 | } |
| 1670 | insert_index = i; |
| 1671 | |
| 1672 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1673 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1674 | |
| 1675 | BUG_ON(insert_index < 0); |
| 1676 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1677 | BUG_ON(insert_index > next_free); |
| 1678 | |
| 1679 | /* |
| 1680 | * No need to memmove if we're just adding to the tail. |
| 1681 | */ |
| 1682 | if (insert_index != next_free) { |
| 1683 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1684 | |
| 1685 | num_bytes = next_free - insert_index; |
| 1686 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1687 | memmove(&el->l_recs[insert_index + 1], |
| 1688 | &el->l_recs[insert_index], |
| 1689 | num_bytes); |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Either we had an empty extent, and need to re-increment or |
| 1694 | * there was no empty extent on a non full rightmost leaf node, |
| 1695 | * in which case we still need to increment. |
| 1696 | */ |
| 1697 | next_free++; |
| 1698 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1699 | /* |
| 1700 | * Make sure none of the math above just messed up our tree. |
| 1701 | */ |
| 1702 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1703 | |
| 1704 | el->l_recs[insert_index] = *insert_rec; |
| 1705 | |
| 1706 | } |
| 1707 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1708 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1709 | { |
| 1710 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1711 | |
| 1712 | BUG_ON(num_recs == 0); |
| 1713 | |
| 1714 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1715 | num_recs--; |
| 1716 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1717 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1718 | memset(&el->l_recs[num_recs], 0, |
| 1719 | sizeof(struct ocfs2_extent_rec)); |
| 1720 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1721 | } |
| 1722 | } |
| 1723 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1724 | /* |
| 1725 | * Create an empty extent record . |
| 1726 | * |
| 1727 | * l_next_free_rec may be updated. |
| 1728 | * |
| 1729 | * If an empty extent already exists do nothing. |
| 1730 | */ |
| 1731 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1732 | { |
| 1733 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1734 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1735 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1736 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1737 | if (next_free == 0) |
| 1738 | goto set_and_inc; |
| 1739 | |
| 1740 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1741 | return; |
| 1742 | |
| 1743 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1744 | "Asked to create an empty extent in a full list:\n" |
| 1745 | "count = %u, tree depth = %u", |
| 1746 | le16_to_cpu(el->l_count), |
| 1747 | le16_to_cpu(el->l_tree_depth)); |
| 1748 | |
| 1749 | ocfs2_shift_records_right(el); |
| 1750 | |
| 1751 | set_and_inc: |
| 1752 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1753 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1754 | } |
| 1755 | |
| 1756 | /* |
| 1757 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1758 | * the lowest level tree node which contains a path to both leafs. This |
| 1759 | * resulting set of information can be used to form a complete "subtree" |
| 1760 | * |
| 1761 | * This function is passed two full paths from the dinode down to a |
| 1762 | * pair of adjacent leaves. It's task is to figure out which path |
| 1763 | * index contains the subtree root - this can be the root index itself |
| 1764 | * in a worst-case rotation. |
| 1765 | * |
| 1766 | * The array index of the subtree root is passed back. |
| 1767 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1768 | static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1769 | struct ocfs2_path *left, |
| 1770 | struct ocfs2_path *right) |
| 1771 | { |
| 1772 | int i = 0; |
| 1773 | |
| 1774 | /* |
| 1775 | * Check that the caller passed in two paths from the same tree. |
| 1776 | */ |
| 1777 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1778 | |
| 1779 | do { |
| 1780 | i++; |
| 1781 | |
| 1782 | /* |
| 1783 | * The caller didn't pass two adjacent paths. |
| 1784 | */ |
| 1785 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1786 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1787 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1788 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1789 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1790 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1791 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1792 | } while (left->p_node[i].bh->b_blocknr == |
| 1793 | right->p_node[i].bh->b_blocknr); |
| 1794 | |
| 1795 | return i - 1; |
| 1796 | } |
| 1797 | |
| 1798 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1799 | |
| 1800 | /* |
| 1801 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1802 | * |
| 1803 | * This code can be called with a cpos larger than the tree, in which |
| 1804 | * case it will return the rightmost path. |
| 1805 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1806 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1807 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1808 | path_insert_t *func, void *data) |
| 1809 | { |
| 1810 | int i, ret = 0; |
| 1811 | u32 range; |
| 1812 | u64 blkno; |
| 1813 | struct buffer_head *bh = NULL; |
| 1814 | struct ocfs2_extent_block *eb; |
| 1815 | struct ocfs2_extent_list *el; |
| 1816 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1817 | |
| 1818 | el = root_el; |
| 1819 | while (el->l_tree_depth) { |
| 1820 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1821 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1822 | "Owner %llu has empty extent list at " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1823 | "depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1824 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1825 | le16_to_cpu(el->l_tree_depth)); |
| 1826 | ret = -EROFS; |
| 1827 | goto out; |
| 1828 | |
| 1829 | } |
| 1830 | |
| 1831 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1832 | rec = &el->l_recs[i]; |
| 1833 | |
| 1834 | /* |
| 1835 | * In the case that cpos is off the allocation |
| 1836 | * tree, this should just wind up returning the |
| 1837 | * rightmost record. |
| 1838 | */ |
| 1839 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1840 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1841 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1842 | break; |
| 1843 | } |
| 1844 | |
| 1845 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1846 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1847 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1848 | "Owner %llu has bad blkno in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1849 | "at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1850 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1851 | le16_to_cpu(el->l_tree_depth), i); |
| 1852 | ret = -EROFS; |
| 1853 | goto out; |
| 1854 | } |
| 1855 | |
| 1856 | brelse(bh); |
| 1857 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1858 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1859 | if (ret) { |
| 1860 | mlog_errno(ret); |
| 1861 | goto out; |
| 1862 | } |
| 1863 | |
| 1864 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1865 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1866 | |
| 1867 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1868 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1869 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1870 | "Owner %llu has bad count in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1871 | "at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1872 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1873 | (unsigned long long)bh->b_blocknr, |
| 1874 | le16_to_cpu(el->l_next_free_rec), |
| 1875 | le16_to_cpu(el->l_count)); |
| 1876 | ret = -EROFS; |
| 1877 | goto out; |
| 1878 | } |
| 1879 | |
| 1880 | if (func) |
| 1881 | func(data, bh); |
| 1882 | } |
| 1883 | |
| 1884 | out: |
| 1885 | /* |
| 1886 | * Catch any trailing bh that the loop didn't handle. |
| 1887 | */ |
| 1888 | brelse(bh); |
| 1889 | |
| 1890 | return ret; |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | * Given an initialized path (that is, it has a valid root extent |
| 1895 | * list), this function will traverse the btree in search of the path |
| 1896 | * which would contain cpos. |
| 1897 | * |
| 1898 | * The path traveled is recorded in the path structure. |
| 1899 | * |
| 1900 | * Note that this will not do any comparisons on leaf node extent |
| 1901 | * records, so it will work fine in the case that we just added a tree |
| 1902 | * branch. |
| 1903 | */ |
| 1904 | struct find_path_data { |
| 1905 | int index; |
| 1906 | struct ocfs2_path *path; |
| 1907 | }; |
| 1908 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1909 | { |
| 1910 | struct find_path_data *fp = data; |
| 1911 | |
| 1912 | get_bh(bh); |
| 1913 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1914 | fp->index++; |
| 1915 | } |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 1916 | int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1917 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1918 | { |
| 1919 | struct find_path_data data; |
| 1920 | |
| 1921 | data.index = 1; |
| 1922 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1923 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1924 | find_path_ins, &data); |
| 1925 | } |
| 1926 | |
| 1927 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1928 | { |
| 1929 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1930 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1931 | struct buffer_head **ret = data; |
| 1932 | |
| 1933 | /* We want to retain only the leaf block. */ |
| 1934 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1935 | get_bh(bh); |
| 1936 | *ret = bh; |
| 1937 | } |
| 1938 | } |
| 1939 | /* |
| 1940 | * Find the leaf block in the tree which would contain cpos. No |
| 1941 | * checking of the actual leaf is done. |
| 1942 | * |
| 1943 | * Some paths want to call this instead of allocating a path structure |
| 1944 | * and calling ocfs2_find_path(). |
| 1945 | * |
| 1946 | * This function doesn't handle non btree extent lists. |
| 1947 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1948 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1949 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1950 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1951 | { |
| 1952 | int ret; |
| 1953 | struct buffer_head *bh = NULL; |
| 1954 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1955 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1956 | if (ret) { |
| 1957 | mlog_errno(ret); |
| 1958 | goto out; |
| 1959 | } |
| 1960 | |
| 1961 | *leaf_bh = bh; |
| 1962 | out: |
| 1963 | return ret; |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1968 | * |
| 1969 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1970 | * we need to fix up the extent records above the changes to reflect |
| 1971 | * the new changes. |
| 1972 | * |
| 1973 | * left_rec: the record on the left. |
| 1974 | * left_child_el: is the child list pointed to by left_rec |
| 1975 | * right_rec: the record to the right of left_rec |
| 1976 | * right_child_el: is the child list pointed to by right_rec |
| 1977 | * |
| 1978 | * By definition, this only works on interior nodes. |
| 1979 | */ |
| 1980 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 1981 | struct ocfs2_extent_list *left_child_el, |
| 1982 | struct ocfs2_extent_rec *right_rec, |
| 1983 | struct ocfs2_extent_list *right_child_el) |
| 1984 | { |
| 1985 | u32 left_clusters, right_end; |
| 1986 | |
| 1987 | /* |
| 1988 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1989 | * the leftmost record in their child list. Their cluster |
| 1990 | * count covers the full theoretical range of their child list |
| 1991 | * - the range between their cpos and the cpos of the record |
| 1992 | * immediately to their right. |
| 1993 | */ |
| 1994 | 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] | 1995 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 1996 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1997 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1998 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1999 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2000 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2001 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2002 | |
| 2003 | /* |
| 2004 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2005 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2006 | * updating e_cpos to keep the same highest cluster count. |
| 2007 | */ |
| 2008 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2009 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2010 | |
| 2011 | right_rec->e_cpos = left_rec->e_cpos; |
| 2012 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 2013 | |
| 2014 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2015 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Adjust the adjacent root node records involved in a |
| 2020 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 2021 | * find it's index in the root list. |
| 2022 | */ |
| 2023 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 2024 | struct ocfs2_extent_list *left_el, |
| 2025 | struct ocfs2_extent_list *right_el, |
| 2026 | u64 left_el_blkno) |
| 2027 | { |
| 2028 | int i; |
| 2029 | |
| 2030 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 2031 | le16_to_cpu(left_el->l_tree_depth)); |
| 2032 | |
| 2033 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 2034 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 2035 | break; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * The path walking code should have never returned a root and |
| 2040 | * two paths which are not adjacent. |
| 2041 | */ |
| 2042 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 2043 | |
| 2044 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 2045 | &root_el->l_recs[i + 1], right_el); |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * We've changed a leaf block (in right_path) and need to reflect that |
| 2050 | * change back up the subtree. |
| 2051 | * |
| 2052 | * This happens in multiple places: |
| 2053 | * - When we've moved an extent record from the left path leaf to the right |
| 2054 | * path leaf to make room for an empty extent in the left path leaf. |
| 2055 | * - When our insert into the right path leaf is at the leftmost edge |
| 2056 | * and requires an update of the path immediately to it's left. This |
| 2057 | * 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] | 2058 | * - When we've adjusted the last extent record in the left path leaf and the |
| 2059 | * 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] | 2060 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2061 | static void ocfs2_complete_edge_insert(handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2062 | struct ocfs2_path *left_path, |
| 2063 | struct ocfs2_path *right_path, |
| 2064 | int subtree_index) |
| 2065 | { |
| 2066 | int ret, i, idx; |
| 2067 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 2068 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 2069 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2070 | |
| 2071 | /* |
| 2072 | * Update the counts and position values within all the |
| 2073 | * interior nodes to reflect the leaf rotation we just did. |
| 2074 | * |
| 2075 | * The root node is handled below the loop. |
| 2076 | * |
| 2077 | * We begin the loop with right_el and left_el pointing to the |
| 2078 | * leaf lists and work our way up. |
| 2079 | * |
| 2080 | * NOTE: within this loop, left_el and right_el always refer |
| 2081 | * to the *child* lists. |
| 2082 | */ |
| 2083 | left_el = path_leaf_el(left_path); |
| 2084 | right_el = path_leaf_el(right_path); |
| 2085 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 2086 | mlog(0, "Adjust records at index %u\n", i); |
| 2087 | |
| 2088 | /* |
| 2089 | * One nice property of knowing that all of these |
| 2090 | * nodes are below the root is that we only deal with |
| 2091 | * the leftmost right node record and the rightmost |
| 2092 | * left node record. |
| 2093 | */ |
| 2094 | el = left_path->p_node[i].el; |
| 2095 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2096 | left_rec = &el->l_recs[idx]; |
| 2097 | |
| 2098 | el = right_path->p_node[i].el; |
| 2099 | right_rec = &el->l_recs[0]; |
| 2100 | |
| 2101 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 2102 | right_el); |
| 2103 | |
| 2104 | ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2105 | if (ret) |
| 2106 | mlog_errno(ret); |
| 2107 | |
| 2108 | ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
| 2109 | if (ret) |
| 2110 | mlog_errno(ret); |
| 2111 | |
| 2112 | /* |
| 2113 | * Setup our list pointers now so that the current |
| 2114 | * parents become children in the next iteration. |
| 2115 | */ |
| 2116 | left_el = left_path->p_node[i].el; |
| 2117 | right_el = right_path->p_node[i].el; |
| 2118 | } |
| 2119 | |
| 2120 | /* |
| 2121 | * At the root node, adjust the two adjacent records which |
| 2122 | * begin our path to the leaves. |
| 2123 | */ |
| 2124 | |
| 2125 | el = left_path->p_node[subtree_index].el; |
| 2126 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2127 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2128 | |
| 2129 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2130 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2131 | |
| 2132 | root_bh = left_path->p_node[subtree_index].bh; |
| 2133 | |
| 2134 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 2135 | if (ret) |
| 2136 | mlog_errno(ret); |
| 2137 | } |
| 2138 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2139 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2140 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2141 | struct ocfs2_path *left_path, |
| 2142 | struct ocfs2_path *right_path, |
| 2143 | int subtree_index) |
| 2144 | { |
| 2145 | int ret, i; |
| 2146 | struct buffer_head *right_leaf_bh; |
| 2147 | struct buffer_head *left_leaf_bh = NULL; |
| 2148 | struct buffer_head *root_bh; |
| 2149 | struct ocfs2_extent_list *right_el, *left_el; |
| 2150 | struct ocfs2_extent_rec move_rec; |
| 2151 | |
| 2152 | left_leaf_bh = path_leaf_bh(left_path); |
| 2153 | left_el = path_leaf_el(left_path); |
| 2154 | |
| 2155 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2156 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2157 | "Inode %llu has non-full interior leaf node %llu" |
| 2158 | "(next free = %u)", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2159 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2160 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2161 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2162 | return -EROFS; |
| 2163 | } |
| 2164 | |
| 2165 | /* |
| 2166 | * This extent block may already have an empty record, so we |
| 2167 | * return early if so. |
| 2168 | */ |
| 2169 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2170 | return 0; |
| 2171 | |
| 2172 | root_bh = left_path->p_node[subtree_index].bh; |
| 2173 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2174 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2175 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2176 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2177 | if (ret) { |
| 2178 | mlog_errno(ret); |
| 2179 | goto out; |
| 2180 | } |
| 2181 | |
| 2182 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2183 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2184 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2185 | if (ret) { |
| 2186 | mlog_errno(ret); |
| 2187 | goto out; |
| 2188 | } |
| 2189 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2190 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2191 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2192 | if (ret) { |
| 2193 | mlog_errno(ret); |
| 2194 | goto out; |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | right_leaf_bh = path_leaf_bh(right_path); |
| 2199 | right_el = path_leaf_el(right_path); |
| 2200 | |
| 2201 | /* This is a code error, not a disk corruption. */ |
| 2202 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2203 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2204 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2205 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2206 | |
| 2207 | ocfs2_create_empty_extent(right_el); |
| 2208 | |
| 2209 | ret = ocfs2_journal_dirty(handle, right_leaf_bh); |
| 2210 | if (ret) { |
| 2211 | mlog_errno(ret); |
| 2212 | goto out; |
| 2213 | } |
| 2214 | |
| 2215 | /* Do the copy now. */ |
| 2216 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2217 | move_rec = left_el->l_recs[i]; |
| 2218 | right_el->l_recs[0] = move_rec; |
| 2219 | |
| 2220 | /* |
| 2221 | * Clear out the record we just copied and shift everything |
| 2222 | * over, leaving an empty extent in the left leaf. |
| 2223 | * |
| 2224 | * We temporarily subtract from next_free_rec so that the |
| 2225 | * shift will lose the tail record (which is now defunct). |
| 2226 | */ |
| 2227 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2228 | ocfs2_shift_records_right(left_el); |
| 2229 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2230 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2231 | |
| 2232 | ret = ocfs2_journal_dirty(handle, left_leaf_bh); |
| 2233 | if (ret) { |
| 2234 | mlog_errno(ret); |
| 2235 | goto out; |
| 2236 | } |
| 2237 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2238 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2239 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2240 | |
| 2241 | out: |
| 2242 | return ret; |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | * Given a full path, determine what cpos value would return us a path |
| 2247 | * containing the leaf immediately to the left of the current one. |
| 2248 | * |
| 2249 | * Will return zero if the path passed in is already the leftmost path. |
| 2250 | */ |
| 2251 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2252 | struct ocfs2_path *path, u32 *cpos) |
| 2253 | { |
| 2254 | int i, j, ret = 0; |
| 2255 | u64 blkno; |
| 2256 | struct ocfs2_extent_list *el; |
| 2257 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2258 | BUG_ON(path->p_tree_depth == 0); |
| 2259 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2260 | *cpos = 0; |
| 2261 | |
| 2262 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2263 | |
| 2264 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2265 | i = path->p_tree_depth - 1; |
| 2266 | while (i >= 0) { |
| 2267 | el = path->p_node[i].el; |
| 2268 | |
| 2269 | /* |
| 2270 | * Find the extent record just before the one in our |
| 2271 | * path. |
| 2272 | */ |
| 2273 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2274 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2275 | if (j == 0) { |
| 2276 | if (i == 0) { |
| 2277 | /* |
| 2278 | * We've determined that the |
| 2279 | * path specified is already |
| 2280 | * the leftmost one - return a |
| 2281 | * cpos of zero. |
| 2282 | */ |
| 2283 | goto out; |
| 2284 | } |
| 2285 | /* |
| 2286 | * The leftmost record points to our |
| 2287 | * leaf - we need to travel up the |
| 2288 | * tree one level. |
| 2289 | */ |
| 2290 | goto next_node; |
| 2291 | } |
| 2292 | |
| 2293 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2294 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2295 | &el->l_recs[j - 1]); |
| 2296 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2297 | goto out; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | /* |
| 2302 | * If we got here, we never found a valid node where |
| 2303 | * the tree indicated one should be. |
| 2304 | */ |
| 2305 | ocfs2_error(sb, |
| 2306 | "Invalid extent tree at extent block %llu\n", |
| 2307 | (unsigned long long)blkno); |
| 2308 | ret = -EROFS; |
| 2309 | goto out; |
| 2310 | |
| 2311 | next_node: |
| 2312 | blkno = path->p_node[i].bh->b_blocknr; |
| 2313 | i--; |
| 2314 | } |
| 2315 | |
| 2316 | out: |
| 2317 | return ret; |
| 2318 | } |
| 2319 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2320 | /* |
| 2321 | * Extend the transaction by enough credits to complete the rotation, |
| 2322 | * and still leave at least the original number of credits allocated |
| 2323 | * to this transaction. |
| 2324 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2325 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2326 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2327 | struct ocfs2_path *path) |
| 2328 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2329 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2330 | |
| 2331 | if (handle->h_buffer_credits < credits) |
| 2332 | return ocfs2_extend_trans(handle, credits); |
| 2333 | |
| 2334 | return 0; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * Trap the case where we're inserting into the theoretical range past |
| 2339 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2340 | * whose cpos is less than ours into the right leaf. |
| 2341 | * |
| 2342 | * It's only necessary to look at the rightmost record of the left |
| 2343 | * leaf because the logic that calls us should ensure that the |
| 2344 | * theoretical ranges in the path components above the leaves are |
| 2345 | * correct. |
| 2346 | */ |
| 2347 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2348 | u32 insert_cpos) |
| 2349 | { |
| 2350 | struct ocfs2_extent_list *left_el; |
| 2351 | struct ocfs2_extent_rec *rec; |
| 2352 | int next_free; |
| 2353 | |
| 2354 | left_el = path_leaf_el(left_path); |
| 2355 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2356 | rec = &left_el->l_recs[next_free - 1]; |
| 2357 | |
| 2358 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2359 | return 1; |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2363 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2364 | { |
| 2365 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2366 | unsigned int range; |
| 2367 | struct ocfs2_extent_rec *rec; |
| 2368 | |
| 2369 | if (next_free == 0) |
| 2370 | return 0; |
| 2371 | |
| 2372 | rec = &el->l_recs[0]; |
| 2373 | if (ocfs2_is_empty_extent(rec)) { |
| 2374 | /* Empty list. */ |
| 2375 | if (next_free == 1) |
| 2376 | return 0; |
| 2377 | rec = &el->l_recs[1]; |
| 2378 | } |
| 2379 | |
| 2380 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2381 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2382 | return 1; |
| 2383 | return 0; |
| 2384 | } |
| 2385 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2386 | /* |
| 2387 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2388 | * |
| 2389 | * The path to the rightmost leaf should be passed in. |
| 2390 | * |
| 2391 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2392 | * |
| 2393 | * Upon succesful return from this function: |
| 2394 | * |
| 2395 | * - The 'right_path' array will contain a path to the leaf block |
| 2396 | * whose range contains e_cpos. |
| 2397 | * - That leaf block will have a single empty extent in list index 0. |
| 2398 | * - In the case that the rotation requires a post-insert update, |
| 2399 | * *ret_left_path will contain a valid path which can be passed to |
| 2400 | * ocfs2_insert_path(). |
| 2401 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2402 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2403 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2404 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2405 | u32 insert_cpos, |
| 2406 | struct ocfs2_path *right_path, |
| 2407 | struct ocfs2_path **ret_left_path) |
| 2408 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2409 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2410 | u32 cpos; |
| 2411 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2412 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2413 | |
| 2414 | *ret_left_path = NULL; |
| 2415 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2416 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2417 | if (!left_path) { |
| 2418 | ret = -ENOMEM; |
| 2419 | mlog_errno(ret); |
| 2420 | goto out; |
| 2421 | } |
| 2422 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2423 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2424 | if (ret) { |
| 2425 | mlog_errno(ret); |
| 2426 | goto out; |
| 2427 | } |
| 2428 | |
| 2429 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 2430 | |
| 2431 | /* |
| 2432 | * What we want to do here is: |
| 2433 | * |
| 2434 | * 1) Start with the rightmost path. |
| 2435 | * |
| 2436 | * 2) Determine a path to the leaf block directly to the left |
| 2437 | * of that leaf. |
| 2438 | * |
| 2439 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2440 | * which contains a path to both leaves. |
| 2441 | * |
| 2442 | * 4) Rotate the subtree. |
| 2443 | * |
| 2444 | * 5) Find the next subtree by considering the left path to be |
| 2445 | * the new right path. |
| 2446 | * |
| 2447 | * The check at the top of this while loop also accepts |
| 2448 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2449 | * value to get us the left path - insert_cpos might very well |
| 2450 | * be filling that hole. |
| 2451 | * |
| 2452 | * Stop at a cpos of '0' because we either started at the |
| 2453 | * leftmost branch (i.e., a tree with one branch and a |
| 2454 | * rotation inside of it), or we've gone as far as we can in |
| 2455 | * rotating subtrees. |
| 2456 | */ |
| 2457 | while (cpos && insert_cpos <= cpos) { |
| 2458 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 2459 | insert_cpos, cpos); |
| 2460 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2461 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2462 | if (ret) { |
| 2463 | mlog_errno(ret); |
| 2464 | goto out; |
| 2465 | } |
| 2466 | |
| 2467 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2468 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2469 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2470 | "(left path cpos %u) results in two identical " |
| 2471 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2472 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2473 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2474 | (unsigned long long) |
| 2475 | path_leaf_bh(left_path)->b_blocknr); |
| 2476 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2477 | if (split == SPLIT_NONE && |
| 2478 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2479 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2480 | |
| 2481 | /* |
| 2482 | * We've rotated the tree as much as we |
| 2483 | * should. The rest is up to |
| 2484 | * ocfs2_insert_path() to complete, after the |
| 2485 | * record insertion. We indicate this |
| 2486 | * situation by returning the left path. |
| 2487 | * |
| 2488 | * The reason we don't adjust the records here |
| 2489 | * before the record insert is that an error |
| 2490 | * later might break the rule where a parent |
| 2491 | * record e_cpos will reflect the actual |
| 2492 | * e_cpos of the 1st nonempty record of the |
| 2493 | * child list. |
| 2494 | */ |
| 2495 | *ret_left_path = left_path; |
| 2496 | goto out_ret_path; |
| 2497 | } |
| 2498 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2499 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2500 | |
| 2501 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2502 | start, |
| 2503 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 2504 | right_path->p_tree_depth); |
| 2505 | |
| 2506 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2507 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2508 | if (ret) { |
| 2509 | mlog_errno(ret); |
| 2510 | goto out; |
| 2511 | } |
| 2512 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2513 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2514 | right_path, start); |
| 2515 | if (ret) { |
| 2516 | mlog_errno(ret); |
| 2517 | goto out; |
| 2518 | } |
| 2519 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2520 | if (split != SPLIT_NONE && |
| 2521 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2522 | insert_cpos)) { |
| 2523 | /* |
| 2524 | * A rotate moves the rightmost left leaf |
| 2525 | * record over to the leftmost right leaf |
| 2526 | * slot. If we're doing an extent split |
| 2527 | * instead of a real insert, then we have to |
| 2528 | * check that the extent to be split wasn't |
| 2529 | * just moved over. If it was, then we can |
| 2530 | * exit here, passing left_path back - |
| 2531 | * ocfs2_split_extent() is smart enough to |
| 2532 | * search both leaves. |
| 2533 | */ |
| 2534 | *ret_left_path = left_path; |
| 2535 | goto out_ret_path; |
| 2536 | } |
| 2537 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2538 | /* |
| 2539 | * There is no need to re-read the next right path |
| 2540 | * as we know that it'll be our current left |
| 2541 | * path. Optimize by copying values instead. |
| 2542 | */ |
| 2543 | ocfs2_mv_path(right_path, left_path); |
| 2544 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2545 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2546 | if (ret) { |
| 2547 | mlog_errno(ret); |
| 2548 | goto out; |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | out: |
| 2553 | ocfs2_free_path(left_path); |
| 2554 | |
| 2555 | out_ret_path: |
| 2556 | return ret; |
| 2557 | } |
| 2558 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2559 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2560 | struct ocfs2_extent_tree *et, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2561 | int subtree_index, struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2562 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2563 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2564 | struct ocfs2_extent_rec *rec; |
| 2565 | struct ocfs2_extent_list *el; |
| 2566 | struct ocfs2_extent_block *eb; |
| 2567 | u32 range; |
| 2568 | |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2569 | /* |
| 2570 | * In normal tree rotation process, we will never touch the |
| 2571 | * tree branch above subtree_index and ocfs2_extend_rotate_transaction |
| 2572 | * doesn't reserve the credits for them either. |
| 2573 | * |
| 2574 | * But we do have a special case here which will update the rightmost |
| 2575 | * records for all the bh in the path. |
| 2576 | * So we have to allocate extra credits and access them. |
| 2577 | */ |
| 2578 | ret = ocfs2_extend_trans(handle, |
| 2579 | handle->h_buffer_credits + subtree_index); |
| 2580 | if (ret) { |
| 2581 | mlog_errno(ret); |
| 2582 | goto out; |
| 2583 | } |
| 2584 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2585 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2586 | if (ret) { |
| 2587 | mlog_errno(ret); |
| 2588 | goto out; |
| 2589 | } |
| 2590 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2591 | /* Path should always be rightmost. */ |
| 2592 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2593 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2594 | |
| 2595 | el = &eb->h_list; |
| 2596 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2597 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2598 | rec = &el->l_recs[idx]; |
| 2599 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2600 | |
| 2601 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2602 | el = path->p_node[i].el; |
| 2603 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2604 | rec = &el->l_recs[idx]; |
| 2605 | |
| 2606 | rec->e_int_clusters = cpu_to_le32(range); |
| 2607 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2608 | |
| 2609 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2610 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2611 | out: |
| 2612 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2615 | static void ocfs2_unlink_path(handle_t *handle, |
| 2616 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2617 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2618 | struct ocfs2_path *path, int unlink_start) |
| 2619 | { |
| 2620 | int ret, i; |
| 2621 | struct ocfs2_extent_block *eb; |
| 2622 | struct ocfs2_extent_list *el; |
| 2623 | struct buffer_head *bh; |
| 2624 | |
| 2625 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2626 | bh = path->p_node[i].bh; |
| 2627 | |
| 2628 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2629 | /* |
| 2630 | * Not all nodes might have had their final count |
| 2631 | * decremented by the caller - handle this here. |
| 2632 | */ |
| 2633 | el = &eb->h_list; |
| 2634 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2635 | mlog(ML_ERROR, |
| 2636 | "Inode %llu, attempted to remove extent block " |
| 2637 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2638 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2639 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2640 | le16_to_cpu(el->l_next_free_rec)); |
| 2641 | |
| 2642 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2643 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2644 | continue; |
| 2645 | } |
| 2646 | |
| 2647 | el->l_next_free_rec = 0; |
| 2648 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2649 | |
| 2650 | ocfs2_journal_dirty(handle, bh); |
| 2651 | |
| 2652 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2653 | if (ret) |
| 2654 | mlog_errno(ret); |
| 2655 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2656 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2660 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2661 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2662 | struct ocfs2_path *left_path, |
| 2663 | struct ocfs2_path *right_path, |
| 2664 | int subtree_index, |
| 2665 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2666 | { |
| 2667 | int i; |
| 2668 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2669 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2670 | struct ocfs2_extent_list *el; |
| 2671 | struct ocfs2_extent_block *eb; |
| 2672 | |
| 2673 | el = path_leaf_el(left_path); |
| 2674 | |
| 2675 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2676 | |
| 2677 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2678 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2679 | break; |
| 2680 | |
| 2681 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2682 | |
| 2683 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2684 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2685 | |
| 2686 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2687 | eb->h_next_leaf_blk = 0; |
| 2688 | |
| 2689 | ocfs2_journal_dirty(handle, root_bh); |
| 2690 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2691 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2692 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2693 | subtree_index + 1); |
| 2694 | } |
| 2695 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2696 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
| 2697 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2698 | struct ocfs2_path *left_path, |
| 2699 | struct ocfs2_path *right_path, |
| 2700 | int subtree_index, |
| 2701 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2702 | int *deleted) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2703 | { |
| 2704 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2705 | 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] | 2706 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2707 | struct ocfs2_extent_block *eb; |
| 2708 | |
| 2709 | *deleted = 0; |
| 2710 | |
| 2711 | right_leaf_el = path_leaf_el(right_path); |
| 2712 | left_leaf_el = path_leaf_el(left_path); |
| 2713 | root_bh = left_path->p_node[subtree_index].bh; |
| 2714 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2715 | |
| 2716 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2717 | return 0; |
| 2718 | |
| 2719 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2720 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2721 | /* |
| 2722 | * It's legal for us to proceed if the right leaf is |
| 2723 | * the rightmost one and it has an empty extent. There |
| 2724 | * are two cases to handle - whether the leaf will be |
| 2725 | * empty after removal or not. If the leaf isn't empty |
| 2726 | * then just remove the empty extent up front. The |
| 2727 | * next block will handle empty leaves by flagging |
| 2728 | * them for unlink. |
| 2729 | * |
| 2730 | * Non rightmost leaves will throw -EAGAIN and the |
| 2731 | * caller can manually move the subtree and retry. |
| 2732 | */ |
| 2733 | |
| 2734 | if (eb->h_next_leaf_blk != 0ULL) |
| 2735 | return -EAGAIN; |
| 2736 | |
| 2737 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2738 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2739 | path_leaf_bh(right_path), |
| 2740 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2741 | if (ret) { |
| 2742 | mlog_errno(ret); |
| 2743 | goto out; |
| 2744 | } |
| 2745 | |
| 2746 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2747 | } else |
| 2748 | right_has_empty = 1; |
| 2749 | } |
| 2750 | |
| 2751 | if (eb->h_next_leaf_blk == 0ULL && |
| 2752 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2753 | /* |
| 2754 | * We have to update i_last_eb_blk during the meta |
| 2755 | * data delete. |
| 2756 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2757 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2758 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2759 | if (ret) { |
| 2760 | mlog_errno(ret); |
| 2761 | goto out; |
| 2762 | } |
| 2763 | |
| 2764 | del_right_subtree = 1; |
| 2765 | } |
| 2766 | |
| 2767 | /* |
| 2768 | * Getting here with an empty extent in the right path implies |
| 2769 | * that it's the rightmost path and will be deleted. |
| 2770 | */ |
| 2771 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2772 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2773 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2774 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2775 | if (ret) { |
| 2776 | mlog_errno(ret); |
| 2777 | goto out; |
| 2778 | } |
| 2779 | |
| 2780 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2781 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2782 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2783 | if (ret) { |
| 2784 | mlog_errno(ret); |
| 2785 | goto out; |
| 2786 | } |
| 2787 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2788 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2789 | left_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2790 | if (ret) { |
| 2791 | mlog_errno(ret); |
| 2792 | goto out; |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | if (!right_has_empty) { |
| 2797 | /* |
| 2798 | * Only do this if we're moving a real |
| 2799 | * record. Otherwise, the action is delayed until |
| 2800 | * after removal of the right path in which case we |
| 2801 | * can do a simple shift to remove the empty extent. |
| 2802 | */ |
| 2803 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2804 | memset(&right_leaf_el->l_recs[0], 0, |
| 2805 | sizeof(struct ocfs2_extent_rec)); |
| 2806 | } |
| 2807 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2808 | /* |
| 2809 | * Move recs over to get rid of empty extent, decrease |
| 2810 | * next_free. This is allowed to remove the last |
| 2811 | * extent in our leaf (setting l_next_free_rec to |
| 2812 | * zero) - the delete code below won't care. |
| 2813 | */ |
| 2814 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2815 | } |
| 2816 | |
| 2817 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2818 | if (ret) |
| 2819 | mlog_errno(ret); |
| 2820 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 2821 | if (ret) |
| 2822 | mlog_errno(ret); |
| 2823 | |
| 2824 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2825 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2826 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2827 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2828 | left_path); |
| 2829 | if (ret) { |
| 2830 | mlog_errno(ret); |
| 2831 | goto out; |
| 2832 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2833 | |
| 2834 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2835 | 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] | 2836 | |
| 2837 | /* |
| 2838 | * Removal of the extent in the left leaf was skipped |
| 2839 | * above so we could delete the right path |
| 2840 | * 1st. |
| 2841 | */ |
| 2842 | if (right_has_empty) |
| 2843 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2844 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2845 | ret = ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2846 | if (ret) |
| 2847 | mlog_errno(ret); |
| 2848 | |
| 2849 | *deleted = 1; |
| 2850 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2851 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2852 | subtree_index); |
| 2853 | |
| 2854 | out: |
| 2855 | return ret; |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * Given a full path, determine what cpos value would return us a path |
| 2860 | * containing the leaf immediately to the right of the current one. |
| 2861 | * |
| 2862 | * Will return zero if the path passed in is already the rightmost path. |
| 2863 | * |
| 2864 | * This looks similar, but is subtly different to |
| 2865 | * ocfs2_find_cpos_for_left_leaf(). |
| 2866 | */ |
| 2867 | static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2868 | struct ocfs2_path *path, u32 *cpos) |
| 2869 | { |
| 2870 | int i, j, ret = 0; |
| 2871 | u64 blkno; |
| 2872 | struct ocfs2_extent_list *el; |
| 2873 | |
| 2874 | *cpos = 0; |
| 2875 | |
| 2876 | if (path->p_tree_depth == 0) |
| 2877 | return 0; |
| 2878 | |
| 2879 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2880 | |
| 2881 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2882 | i = path->p_tree_depth - 1; |
| 2883 | while (i >= 0) { |
| 2884 | int next_free; |
| 2885 | |
| 2886 | el = path->p_node[i].el; |
| 2887 | |
| 2888 | /* |
| 2889 | * Find the extent record just after the one in our |
| 2890 | * path. |
| 2891 | */ |
| 2892 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2893 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2894 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2895 | if (j == (next_free - 1)) { |
| 2896 | if (i == 0) { |
| 2897 | /* |
| 2898 | * We've determined that the |
| 2899 | * path specified is already |
| 2900 | * the rightmost one - return a |
| 2901 | * cpos of zero. |
| 2902 | */ |
| 2903 | goto out; |
| 2904 | } |
| 2905 | /* |
| 2906 | * The rightmost record points to our |
| 2907 | * leaf - we need to travel up the |
| 2908 | * tree one level. |
| 2909 | */ |
| 2910 | goto next_node; |
| 2911 | } |
| 2912 | |
| 2913 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2914 | goto out; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | /* |
| 2919 | * If we got here, we never found a valid node where |
| 2920 | * the tree indicated one should be. |
| 2921 | */ |
| 2922 | ocfs2_error(sb, |
| 2923 | "Invalid extent tree at extent block %llu\n", |
| 2924 | (unsigned long long)blkno); |
| 2925 | ret = -EROFS; |
| 2926 | goto out; |
| 2927 | |
| 2928 | next_node: |
| 2929 | blkno = path->p_node[i].bh->b_blocknr; |
| 2930 | i--; |
| 2931 | } |
| 2932 | |
| 2933 | out: |
| 2934 | return ret; |
| 2935 | } |
| 2936 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2937 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
| 2938 | struct ocfs2_extent_tree *et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2939 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2940 | { |
| 2941 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2942 | struct buffer_head *bh = path_leaf_bh(path); |
| 2943 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2944 | |
| 2945 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2946 | return 0; |
| 2947 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2948 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2949 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2950 | if (ret) { |
| 2951 | mlog_errno(ret); |
| 2952 | goto out; |
| 2953 | } |
| 2954 | |
| 2955 | ocfs2_remove_empty_extent(el); |
| 2956 | |
| 2957 | ret = ocfs2_journal_dirty(handle, bh); |
| 2958 | if (ret) |
| 2959 | mlog_errno(ret); |
| 2960 | |
| 2961 | out: |
| 2962 | return ret; |
| 2963 | } |
| 2964 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2965 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
| 2966 | struct ocfs2_extent_tree *et, |
| 2967 | int orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2968 | struct ocfs2_path *path, |
| 2969 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2970 | struct ocfs2_path **empty_extent_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2971 | { |
| 2972 | int ret, subtree_root, deleted; |
| 2973 | u32 right_cpos; |
| 2974 | struct ocfs2_path *left_path = NULL; |
| 2975 | struct ocfs2_path *right_path = NULL; |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2976 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2977 | |
| 2978 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 2979 | |
| 2980 | *empty_extent_path = NULL; |
| 2981 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2982 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2983 | if (ret) { |
| 2984 | mlog_errno(ret); |
| 2985 | goto out; |
| 2986 | } |
| 2987 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2988 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2989 | if (!left_path) { |
| 2990 | ret = -ENOMEM; |
| 2991 | mlog_errno(ret); |
| 2992 | goto out; |
| 2993 | } |
| 2994 | |
| 2995 | ocfs2_cp_path(left_path, path); |
| 2996 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2997 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2998 | if (!right_path) { |
| 2999 | ret = -ENOMEM; |
| 3000 | mlog_errno(ret); |
| 3001 | goto out; |
| 3002 | } |
| 3003 | |
| 3004 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3005 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3006 | if (ret) { |
| 3007 | mlog_errno(ret); |
| 3008 | goto out; |
| 3009 | } |
| 3010 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3011 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3012 | right_path); |
| 3013 | |
| 3014 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 3015 | subtree_root, |
| 3016 | (unsigned long long) |
| 3017 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 3018 | right_path->p_tree_depth); |
| 3019 | |
| 3020 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 3021 | orig_credits, left_path); |
| 3022 | if (ret) { |
| 3023 | mlog_errno(ret); |
| 3024 | goto out; |
| 3025 | } |
| 3026 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 3027 | /* |
| 3028 | * Caller might still want to make changes to the |
| 3029 | * tree root, so re-add it to the journal here. |
| 3030 | */ |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3031 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3032 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 3033 | if (ret) { |
| 3034 | mlog_errno(ret); |
| 3035 | goto out; |
| 3036 | } |
| 3037 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 3038 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3039 | right_path, subtree_root, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 3040 | dealloc, &deleted); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3041 | if (ret == -EAGAIN) { |
| 3042 | /* |
| 3043 | * The rotation has to temporarily stop due to |
| 3044 | * the right subtree having an empty |
| 3045 | * extent. Pass it back to the caller for a |
| 3046 | * fixup. |
| 3047 | */ |
| 3048 | *empty_extent_path = right_path; |
| 3049 | right_path = NULL; |
| 3050 | goto out; |
| 3051 | } |
| 3052 | if (ret) { |
| 3053 | mlog_errno(ret); |
| 3054 | goto out; |
| 3055 | } |
| 3056 | |
| 3057 | /* |
| 3058 | * The subtree rotate might have removed records on |
| 3059 | * the rightmost edge. If so, then rotation is |
| 3060 | * complete. |
| 3061 | */ |
| 3062 | if (deleted) |
| 3063 | break; |
| 3064 | |
| 3065 | ocfs2_mv_path(left_path, right_path); |
| 3066 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3067 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3068 | &right_cpos); |
| 3069 | if (ret) { |
| 3070 | mlog_errno(ret); |
| 3071 | goto out; |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | out: |
| 3076 | ocfs2_free_path(right_path); |
| 3077 | ocfs2_free_path(left_path); |
| 3078 | |
| 3079 | return ret; |
| 3080 | } |
| 3081 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3082 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
| 3083 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3084 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3085 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3086 | { |
| 3087 | int ret, subtree_index; |
| 3088 | u32 cpos; |
| 3089 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3090 | struct ocfs2_extent_block *eb; |
| 3091 | struct ocfs2_extent_list *el; |
| 3092 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3093 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 3094 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3095 | if (ret) |
| 3096 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3097 | /* |
| 3098 | * There's two ways we handle this depending on |
| 3099 | * whether path is the only existing one. |
| 3100 | */ |
| 3101 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3102 | handle->h_buffer_credits, |
| 3103 | path); |
| 3104 | if (ret) { |
| 3105 | mlog_errno(ret); |
| 3106 | goto out; |
| 3107 | } |
| 3108 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3109 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3110 | if (ret) { |
| 3111 | mlog_errno(ret); |
| 3112 | goto out; |
| 3113 | } |
| 3114 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3115 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3116 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3117 | if (ret) { |
| 3118 | mlog_errno(ret); |
| 3119 | goto out; |
| 3120 | } |
| 3121 | |
| 3122 | if (cpos) { |
| 3123 | /* |
| 3124 | * We have a path to the left of this one - it needs |
| 3125 | * an update too. |
| 3126 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3127 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3128 | if (!left_path) { |
| 3129 | ret = -ENOMEM; |
| 3130 | mlog_errno(ret); |
| 3131 | goto out; |
| 3132 | } |
| 3133 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3134 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3135 | if (ret) { |
| 3136 | mlog_errno(ret); |
| 3137 | goto out; |
| 3138 | } |
| 3139 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3140 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3141 | if (ret) { |
| 3142 | mlog_errno(ret); |
| 3143 | goto out; |
| 3144 | } |
| 3145 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3146 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3147 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3148 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3149 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 3150 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3151 | left_path); |
| 3152 | if (ret) { |
| 3153 | mlog_errno(ret); |
| 3154 | goto out; |
| 3155 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3156 | |
| 3157 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3158 | 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] | 3159 | } else { |
| 3160 | /* |
| 3161 | * 'path' is also the leftmost path which |
| 3162 | * means it must be the only one. This gets |
| 3163 | * handled differently because we want to |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3164 | * revert the root back to having extents |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3165 | * in-line. |
| 3166 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3167 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3168 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3169 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3170 | el->l_tree_depth = 0; |
| 3171 | el->l_next_free_rec = 0; |
| 3172 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3173 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3174 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3178 | |
| 3179 | out: |
| 3180 | ocfs2_free_path(left_path); |
| 3181 | return ret; |
| 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * Left rotation of btree records. |
| 3186 | * |
| 3187 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3188 | * rotation. We start at some non-rightmost path containing an empty |
| 3189 | * extent in the leaf block. The code works its way to the rightmost |
| 3190 | * path by rotating records to the left in every subtree. |
| 3191 | * |
| 3192 | * This is used by any code which reduces the number of extent records |
| 3193 | * in a leaf. After removal, an empty record should be placed in the |
| 3194 | * leftmost list position. |
| 3195 | * |
| 3196 | * This won't handle a length update of the rightmost path records if |
| 3197 | * the rightmost tree leaf record is removed so the caller is |
| 3198 | * responsible for detecting and correcting that. |
| 3199 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3200 | static int ocfs2_rotate_tree_left(handle_t *handle, |
| 3201 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3202 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3203 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3204 | { |
| 3205 | int ret, orig_credits = handle->h_buffer_credits; |
| 3206 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3207 | struct ocfs2_extent_block *eb; |
| 3208 | struct ocfs2_extent_list *el; |
| 3209 | |
| 3210 | el = path_leaf_el(path); |
| 3211 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3212 | return 0; |
| 3213 | |
| 3214 | if (path->p_tree_depth == 0) { |
| 3215 | rightmost_no_delete: |
| 3216 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3217 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3218 | * it up front. |
| 3219 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3220 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3221 | if (ret) |
| 3222 | mlog_errno(ret); |
| 3223 | goto out; |
| 3224 | } |
| 3225 | |
| 3226 | /* |
| 3227 | * Handle rightmost branch now. There's several cases: |
| 3228 | * 1) simple rotation leaving records in there. That's trivial. |
| 3229 | * 2) rotation requiring a branch delete - there's no more |
| 3230 | * records left. Two cases of this: |
| 3231 | * a) There are branches to the left. |
| 3232 | * b) This is also the leftmost (the only) branch. |
| 3233 | * |
| 3234 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3235 | * 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] | 3236 | * 2b) we need to bring the root back to inline extents. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3237 | */ |
| 3238 | |
| 3239 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3240 | el = &eb->h_list; |
| 3241 | if (eb->h_next_leaf_blk == 0) { |
| 3242 | /* |
| 3243 | * This gets a bit tricky if we're going to delete the |
| 3244 | * rightmost path. Get the other cases out of the way |
| 3245 | * 1st. |
| 3246 | */ |
| 3247 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3248 | goto rightmost_no_delete; |
| 3249 | |
| 3250 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3251 | ret = -EIO; |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3252 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3253 | "Owner %llu has empty extent block at %llu", |
| 3254 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3255 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3256 | goto out; |
| 3257 | } |
| 3258 | |
| 3259 | /* |
| 3260 | * XXX: The caller can not trust "path" any more after |
| 3261 | * this as it will have been deleted. What do we do? |
| 3262 | * |
| 3263 | * In theory the rotate-for-merge code will never get |
| 3264 | * here because it'll always ask for a rotate in a |
| 3265 | * nonempty list. |
| 3266 | */ |
| 3267 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3268 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
| 3269 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3270 | if (ret) |
| 3271 | mlog_errno(ret); |
| 3272 | goto out; |
| 3273 | } |
| 3274 | |
| 3275 | /* |
| 3276 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3277 | * and restarting from there. |
| 3278 | */ |
| 3279 | try_rotate: |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3280 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
| 3281 | dealloc, &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3282 | if (ret && ret != -EAGAIN) { |
| 3283 | mlog_errno(ret); |
| 3284 | goto out; |
| 3285 | } |
| 3286 | |
| 3287 | while (ret == -EAGAIN) { |
| 3288 | tmp_path = restart_path; |
| 3289 | restart_path = NULL; |
| 3290 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3291 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3292 | tmp_path, dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3293 | &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3294 | if (ret && ret != -EAGAIN) { |
| 3295 | mlog_errno(ret); |
| 3296 | goto out; |
| 3297 | } |
| 3298 | |
| 3299 | ocfs2_free_path(tmp_path); |
| 3300 | tmp_path = NULL; |
| 3301 | |
| 3302 | if (ret == 0) |
| 3303 | goto try_rotate; |
| 3304 | } |
| 3305 | |
| 3306 | out: |
| 3307 | ocfs2_free_path(tmp_path); |
| 3308 | ocfs2_free_path(restart_path); |
| 3309 | return ret; |
| 3310 | } |
| 3311 | |
| 3312 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3313 | int index) |
| 3314 | { |
| 3315 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3316 | unsigned int size; |
| 3317 | |
| 3318 | if (rec->e_leaf_clusters == 0) { |
| 3319 | /* |
| 3320 | * We consumed all of the merged-from record. An empty |
| 3321 | * extent cannot exist anywhere but the 1st array |
| 3322 | * position, so move things over if the merged-from |
| 3323 | * record doesn't occupy that position. |
| 3324 | * |
| 3325 | * This creates a new empty extent so the caller |
| 3326 | * should be smart enough to have removed any existing |
| 3327 | * ones. |
| 3328 | */ |
| 3329 | if (index > 0) { |
| 3330 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3331 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3332 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3333 | } |
| 3334 | |
| 3335 | /* |
| 3336 | * Always memset - the caller doesn't check whether it |
| 3337 | * created an empty extent, so there could be junk in |
| 3338 | * the other fields. |
| 3339 | */ |
| 3340 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3341 | } |
| 3342 | } |
| 3343 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3344 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3345 | struct ocfs2_path *left_path, |
| 3346 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3347 | { |
| 3348 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3349 | u32 right_cpos; |
| 3350 | struct ocfs2_path *right_path = NULL; |
| 3351 | struct ocfs2_extent_list *left_el; |
| 3352 | |
| 3353 | *ret_right_path = NULL; |
| 3354 | |
| 3355 | /* This function shouldn't be called for non-trees. */ |
| 3356 | BUG_ON(left_path->p_tree_depth == 0); |
| 3357 | |
| 3358 | left_el = path_leaf_el(left_path); |
| 3359 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3360 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3361 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3362 | left_path, &right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3363 | if (ret) { |
| 3364 | mlog_errno(ret); |
| 3365 | goto out; |
| 3366 | } |
| 3367 | |
| 3368 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3369 | BUG_ON(right_cpos == 0); |
| 3370 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3371 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3372 | if (!right_path) { |
| 3373 | ret = -ENOMEM; |
| 3374 | mlog_errno(ret); |
| 3375 | goto out; |
| 3376 | } |
| 3377 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3378 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3379 | if (ret) { |
| 3380 | mlog_errno(ret); |
| 3381 | goto out; |
| 3382 | } |
| 3383 | |
| 3384 | *ret_right_path = right_path; |
| 3385 | out: |
| 3386 | if (ret) |
| 3387 | ocfs2_free_path(right_path); |
| 3388 | return ret; |
| 3389 | } |
| 3390 | |
| 3391 | /* |
| 3392 | * Remove split_rec clusters from the record at index and merge them |
| 3393 | * onto the beginning of the record "next" to it. |
| 3394 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3395 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3396 | * next extent block. |
| 3397 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3398 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3399 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3400 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3401 | struct ocfs2_extent_rec *split_rec, |
| 3402 | int index) |
| 3403 | { |
| 3404 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3405 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3406 | struct ocfs2_extent_rec *left_rec; |
| 3407 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3408 | struct ocfs2_extent_list *right_el; |
| 3409 | struct ocfs2_path *right_path = NULL; |
| 3410 | int subtree_index = 0; |
| 3411 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3412 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3413 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3414 | |
| 3415 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3416 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3417 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3418 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3419 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3420 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3421 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3422 | if (ret) { |
| 3423 | mlog_errno(ret); |
| 3424 | goto out; |
| 3425 | } |
| 3426 | |
| 3427 | right_el = path_leaf_el(right_path); |
| 3428 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3429 | BUG_ON(next_free <= 0); |
| 3430 | right_rec = &right_el->l_recs[0]; |
| 3431 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3432 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3433 | right_rec = &right_el->l_recs[1]; |
| 3434 | } |
| 3435 | |
| 3436 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3437 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3438 | le32_to_cpu(right_rec->e_cpos)); |
| 3439 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3440 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3441 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3442 | |
| 3443 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3444 | handle->h_buffer_credits, |
| 3445 | right_path); |
| 3446 | if (ret) { |
| 3447 | mlog_errno(ret); |
| 3448 | goto out; |
| 3449 | } |
| 3450 | |
| 3451 | root_bh = left_path->p_node[subtree_index].bh; |
| 3452 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3453 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3454 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3455 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3456 | if (ret) { |
| 3457 | mlog_errno(ret); |
| 3458 | goto out; |
| 3459 | } |
| 3460 | |
| 3461 | for (i = subtree_index + 1; |
| 3462 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3463 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3464 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3465 | if (ret) { |
| 3466 | mlog_errno(ret); |
| 3467 | goto out; |
| 3468 | } |
| 3469 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3470 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3471 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3472 | if (ret) { |
| 3473 | mlog_errno(ret); |
| 3474 | goto out; |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | } else { |
| 3479 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3480 | right_rec = &el->l_recs[index + 1]; |
| 3481 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3482 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3483 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3484 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3485 | if (ret) { |
| 3486 | mlog_errno(ret); |
| 3487 | goto out; |
| 3488 | } |
| 3489 | |
| 3490 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3491 | |
| 3492 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3493 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3494 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3495 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3496 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3497 | |
| 3498 | ocfs2_cleanup_merge(el, index); |
| 3499 | |
| 3500 | ret = ocfs2_journal_dirty(handle, bh); |
| 3501 | if (ret) |
| 3502 | mlog_errno(ret); |
| 3503 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3504 | if (right_path) { |
| 3505 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 3506 | if (ret) |
| 3507 | mlog_errno(ret); |
| 3508 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3509 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3510 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3511 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3512 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3513 | if (right_path) |
| 3514 | ocfs2_free_path(right_path); |
| 3515 | return ret; |
| 3516 | } |
| 3517 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3518 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3519 | struct ocfs2_path *right_path, |
| 3520 | struct ocfs2_path **ret_left_path) |
| 3521 | { |
| 3522 | int ret; |
| 3523 | u32 left_cpos; |
| 3524 | struct ocfs2_path *left_path = NULL; |
| 3525 | |
| 3526 | *ret_left_path = NULL; |
| 3527 | |
| 3528 | /* This function shouldn't be called for non-trees. */ |
| 3529 | BUG_ON(right_path->p_tree_depth == 0); |
| 3530 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3531 | 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] | 3532 | right_path, &left_cpos); |
| 3533 | if (ret) { |
| 3534 | mlog_errno(ret); |
| 3535 | goto out; |
| 3536 | } |
| 3537 | |
| 3538 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3539 | BUG_ON(left_cpos == 0); |
| 3540 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3541 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3542 | if (!left_path) { |
| 3543 | ret = -ENOMEM; |
| 3544 | mlog_errno(ret); |
| 3545 | goto out; |
| 3546 | } |
| 3547 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3548 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3549 | if (ret) { |
| 3550 | mlog_errno(ret); |
| 3551 | goto out; |
| 3552 | } |
| 3553 | |
| 3554 | *ret_left_path = left_path; |
| 3555 | out: |
| 3556 | if (ret) |
| 3557 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3558 | return ret; |
| 3559 | } |
| 3560 | |
| 3561 | /* |
| 3562 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3563 | * onto the tail of the record "before" it. |
| 3564 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3565 | * |
| 3566 | * For index == 0, the "before" means the last record of the previous |
| 3567 | * extent block. And there is also a situation that we may need to |
| 3568 | * remove the rightmost leaf extent block in the right_path and change |
| 3569 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3570 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3571 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3572 | handle_t *handle, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3573 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3574 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3575 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3576 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3577 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3578 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3579 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3580 | struct ocfs2_extent_rec *left_rec; |
| 3581 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3582 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3583 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3584 | struct buffer_head *root_bh = NULL; |
| 3585 | struct ocfs2_path *left_path = NULL; |
| 3586 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3587 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3588 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3589 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3590 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3591 | if (index == 0) { |
| 3592 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3593 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3594 | if (ret) { |
| 3595 | mlog_errno(ret); |
| 3596 | goto out; |
| 3597 | } |
| 3598 | |
| 3599 | left_el = path_leaf_el(left_path); |
| 3600 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3601 | le16_to_cpu(left_el->l_count)); |
| 3602 | |
| 3603 | left_rec = &left_el->l_recs[ |
| 3604 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3605 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3606 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3607 | le32_to_cpu(split_rec->e_cpos)); |
| 3608 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3609 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3610 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3611 | |
| 3612 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3613 | handle->h_buffer_credits, |
| 3614 | left_path); |
| 3615 | if (ret) { |
| 3616 | mlog_errno(ret); |
| 3617 | goto out; |
| 3618 | } |
| 3619 | |
| 3620 | root_bh = left_path->p_node[subtree_index].bh; |
| 3621 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3622 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3623 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3624 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3625 | if (ret) { |
| 3626 | mlog_errno(ret); |
| 3627 | goto out; |
| 3628 | } |
| 3629 | |
| 3630 | for (i = subtree_index + 1; |
| 3631 | i < path_num_items(right_path); i++) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3632 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3633 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3634 | if (ret) { |
| 3635 | mlog_errno(ret); |
| 3636 | goto out; |
| 3637 | } |
| 3638 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3639 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3640 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3641 | if (ret) { |
| 3642 | mlog_errno(ret); |
| 3643 | goto out; |
| 3644 | } |
| 3645 | } |
| 3646 | } else { |
| 3647 | left_rec = &el->l_recs[index - 1]; |
| 3648 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3649 | has_empty_extent = 1; |
| 3650 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 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, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3653 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3654 | if (ret) { |
| 3655 | mlog_errno(ret); |
| 3656 | goto out; |
| 3657 | } |
| 3658 | |
| 3659 | if (has_empty_extent && index == 1) { |
| 3660 | /* |
| 3661 | * The easy case - we can just plop the record right in. |
| 3662 | */ |
| 3663 | *left_rec = *split_rec; |
| 3664 | |
| 3665 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3666 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3667 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3668 | |
| 3669 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3670 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3671 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3672 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3673 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3674 | |
| 3675 | ocfs2_cleanup_merge(el, index); |
| 3676 | |
| 3677 | ret = ocfs2_journal_dirty(handle, bh); |
| 3678 | if (ret) |
| 3679 | mlog_errno(ret); |
| 3680 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3681 | if (left_path) { |
| 3682 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 3683 | if (ret) |
| 3684 | mlog_errno(ret); |
| 3685 | |
| 3686 | /* |
| 3687 | * In the situation that the right_rec is empty and the extent |
| 3688 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3689 | * it and we need to delete the right extent block. |
| 3690 | */ |
| 3691 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3692 | le16_to_cpu(el->l_next_free_rec) == 1) { |
| 3693 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3694 | ret = ocfs2_remove_rightmost_path(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3695 | right_path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3696 | dealloc); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3697 | if (ret) { |
| 3698 | mlog_errno(ret); |
| 3699 | goto out; |
| 3700 | } |
| 3701 | |
| 3702 | /* Now the rightmost extent block has been deleted. |
| 3703 | * So we use the new rightmost path. |
| 3704 | */ |
| 3705 | ocfs2_mv_path(right_path, left_path); |
| 3706 | left_path = NULL; |
| 3707 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3708 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3709 | right_path, subtree_index); |
| 3710 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3711 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3712 | if (left_path) |
| 3713 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3714 | return ret; |
| 3715 | } |
| 3716 | |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3717 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
| 3718 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3719 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3720 | int split_index, |
| 3721 | struct ocfs2_extent_rec *split_rec, |
| 3722 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3723 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3724 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3725 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3726 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3727 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3728 | |
| 3729 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3730 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3731 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 3732 | /* |
| 3733 | * The merge code will need to create an empty |
| 3734 | * extent to take the place of the newly |
| 3735 | * emptied slot. Remove any pre-existing empty |
| 3736 | * extents - having more than one in a leaf is |
| 3737 | * illegal. |
| 3738 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3739 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3740 | if (ret) { |
| 3741 | mlog_errno(ret); |
| 3742 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3743 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3744 | split_index--; |
| 3745 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3749 | /* |
| 3750 | * Left-right contig implies this. |
| 3751 | */ |
| 3752 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3753 | |
| 3754 | /* |
| 3755 | * Since the leftright insert always covers the entire |
| 3756 | * extent, this call will delete the insert record |
| 3757 | * entirely, resulting in an empty extent record added to |
| 3758 | * the extent block. |
| 3759 | * |
| 3760 | * Since the adding of an empty extent shifts |
| 3761 | * everything back to the right, there's no need to |
| 3762 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3763 | * |
| 3764 | * When the split_index is zero, we need to merge it to the |
| 3765 | * prevoius extent block. It is more efficient and easier |
| 3766 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3767 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3768 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3769 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3770 | if (ret) { |
| 3771 | mlog_errno(ret); |
| 3772 | goto out; |
| 3773 | } |
| 3774 | |
| 3775 | /* |
| 3776 | * We can only get this from logic error above. |
| 3777 | */ |
| 3778 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3779 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3780 | /* The merge left us with an empty extent, remove it. */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3781 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3782 | if (ret) { |
| 3783 | mlog_errno(ret); |
| 3784 | goto out; |
| 3785 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3786 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3787 | rec = &el->l_recs[split_index]; |
| 3788 | |
| 3789 | /* |
| 3790 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3791 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3792 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3793 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
| 3794 | dealloc, split_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3795 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3796 | if (ret) { |
| 3797 | mlog_errno(ret); |
| 3798 | goto out; |
| 3799 | } |
| 3800 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3801 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3802 | /* |
| 3803 | * Error from this last rotate is not critical, so |
| 3804 | * print but don't bubble it up. |
| 3805 | */ |
| 3806 | if (ret) |
| 3807 | mlog_errno(ret); |
| 3808 | ret = 0; |
| 3809 | } else { |
| 3810 | /* |
| 3811 | * Merge a record to the left or right. |
| 3812 | * |
| 3813 | * 'contig_type' is relative to the existing record, |
| 3814 | * so for example, if we're "right contig", it's to |
| 3815 | * the record on the left (hence the left merge). |
| 3816 | */ |
| 3817 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3818 | ret = ocfs2_merge_rec_left(path, handle, et, |
| 3819 | split_rec, dealloc, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3820 | split_index); |
| 3821 | if (ret) { |
| 3822 | mlog_errno(ret); |
| 3823 | goto out; |
| 3824 | } |
| 3825 | } else { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3826 | ret = ocfs2_merge_rec_right(path, handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3827 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3828 | split_index); |
| 3829 | if (ret) { |
| 3830 | mlog_errno(ret); |
| 3831 | goto out; |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | if (ctxt->c_split_covers_rec) { |
| 3836 | /* |
| 3837 | * The merge may have left an empty extent in |
| 3838 | * our leaf. Try to rotate it away. |
| 3839 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3840 | ret = ocfs2_rotate_tree_left(handle, et, path, |
| 3841 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3842 | if (ret) |
| 3843 | mlog_errno(ret); |
| 3844 | ret = 0; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | out: |
| 3849 | return ret; |
| 3850 | } |
| 3851 | |
| 3852 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3853 | enum ocfs2_split_type split, |
| 3854 | struct ocfs2_extent_rec *rec, |
| 3855 | struct ocfs2_extent_rec *split_rec) |
| 3856 | { |
| 3857 | u64 len_blocks; |
| 3858 | |
| 3859 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3860 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3861 | |
| 3862 | if (split == SPLIT_LEFT) { |
| 3863 | /* |
| 3864 | * Region is on the left edge of the existing |
| 3865 | * record. |
| 3866 | */ |
| 3867 | le32_add_cpu(&rec->e_cpos, |
| 3868 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3869 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3870 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3871 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3872 | } else { |
| 3873 | /* |
| 3874 | * Region is on the right edge of the existing |
| 3875 | * record. |
| 3876 | */ |
| 3877 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3878 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3879 | } |
| 3880 | } |
| 3881 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3882 | /* |
| 3883 | * Do the final bits of extent record insertion at the target leaf |
| 3884 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3885 | * that the tree above has been prepared. |
| 3886 | */ |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3887 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et, |
| 3888 | struct ocfs2_extent_rec *insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3889 | struct ocfs2_extent_list *el, |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3890 | struct ocfs2_insert_type *insert) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3891 | { |
| 3892 | int i = insert->ins_contig_index; |
| 3893 | unsigned int range; |
| 3894 | struct ocfs2_extent_rec *rec; |
| 3895 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3896 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3897 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3898 | if (insert->ins_split != SPLIT_NONE) { |
| 3899 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3900 | BUG_ON(i == -1); |
| 3901 | rec = &el->l_recs[i]; |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3902 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3903 | insert->ins_split, rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3904 | insert_rec); |
| 3905 | goto rotate; |
| 3906 | } |
| 3907 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3908 | /* |
| 3909 | * Contiguous insert - either left or right. |
| 3910 | */ |
| 3911 | if (insert->ins_contig != CONTIG_NONE) { |
| 3912 | rec = &el->l_recs[i]; |
| 3913 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3914 | rec->e_blkno = insert_rec->e_blkno; |
| 3915 | rec->e_cpos = insert_rec->e_cpos; |
| 3916 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3917 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3918 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3919 | return; |
| 3920 | } |
| 3921 | |
| 3922 | /* |
| 3923 | * Handle insert into an empty leaf. |
| 3924 | */ |
| 3925 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3926 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3927 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3928 | el->l_recs[0] = *insert_rec; |
| 3929 | el->l_next_free_rec = cpu_to_le16(1); |
| 3930 | return; |
| 3931 | } |
| 3932 | |
| 3933 | /* |
| 3934 | * Appending insert. |
| 3935 | */ |
| 3936 | if (insert->ins_appending == APPEND_TAIL) { |
| 3937 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3938 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3939 | range = le32_to_cpu(rec->e_cpos) |
| 3940 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3941 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3942 | |
| 3943 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3944 | le16_to_cpu(el->l_count), |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3945 | "owner %llu, depth %u, count %u, next free %u, " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3946 | "rec.cpos %u, rec.clusters %u, " |
| 3947 | "insert.cpos %u, insert.clusters %u\n", |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3948 | ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3949 | le16_to_cpu(el->l_tree_depth), |
| 3950 | le16_to_cpu(el->l_count), |
| 3951 | le16_to_cpu(el->l_next_free_rec), |
| 3952 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3953 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3954 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3955 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3956 | i++; |
| 3957 | el->l_recs[i] = *insert_rec; |
| 3958 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3959 | return; |
| 3960 | } |
| 3961 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3962 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3963 | /* |
| 3964 | * Ok, we have to rotate. |
| 3965 | * |
| 3966 | * At this point, it is safe to assume that inserting into an |
| 3967 | * empty leaf and appending to a leaf have both been handled |
| 3968 | * above. |
| 3969 | * |
| 3970 | * This leaf needs to have space, either by the empty 1st |
| 3971 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3972 | */ |
| 3973 | ocfs2_rotate_leaf(el, insert_rec); |
| 3974 | } |
| 3975 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3976 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 3977 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3978 | struct ocfs2_path *path, |
| 3979 | struct ocfs2_extent_rec *insert_rec) |
| 3980 | { |
| 3981 | int ret, i, next_free; |
| 3982 | struct buffer_head *bh; |
| 3983 | struct ocfs2_extent_list *el; |
| 3984 | struct ocfs2_extent_rec *rec; |
| 3985 | |
| 3986 | /* |
| 3987 | * Update everything except the leaf block. |
| 3988 | */ |
| 3989 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3990 | bh = path->p_node[i].bh; |
| 3991 | el = path->p_node[i].el; |
| 3992 | |
| 3993 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3994 | if (next_free == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3995 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3996 | "Owner %llu has a bad extent list", |
| 3997 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3998 | ret = -EIO; |
| 3999 | return; |
| 4000 | } |
| 4001 | |
| 4002 | rec = &el->l_recs[next_free - 1]; |
| 4003 | |
| 4004 | rec->e_int_clusters = insert_rec->e_cpos; |
| 4005 | le32_add_cpu(&rec->e_int_clusters, |
| 4006 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 4007 | le32_add_cpu(&rec->e_int_clusters, |
| 4008 | -le32_to_cpu(rec->e_cpos)); |
| 4009 | |
| 4010 | ret = ocfs2_journal_dirty(handle, bh); |
| 4011 | if (ret) |
| 4012 | mlog_errno(ret); |
| 4013 | |
| 4014 | } |
| 4015 | } |
| 4016 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4017 | static int ocfs2_append_rec_to_path(handle_t *handle, |
| 4018 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4019 | struct ocfs2_extent_rec *insert_rec, |
| 4020 | struct ocfs2_path *right_path, |
| 4021 | struct ocfs2_path **ret_left_path) |
| 4022 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4023 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4024 | struct ocfs2_extent_list *el; |
| 4025 | struct ocfs2_path *left_path = NULL; |
| 4026 | |
| 4027 | *ret_left_path = NULL; |
| 4028 | |
| 4029 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4030 | * This shouldn't happen for non-trees. The extent rec cluster |
| 4031 | * count manipulation below only works for interior nodes. |
| 4032 | */ |
| 4033 | BUG_ON(right_path->p_tree_depth == 0); |
| 4034 | |
| 4035 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4036 | * If our appending insert is at the leftmost edge of a leaf, |
| 4037 | * then we might need to update the rightmost records of the |
| 4038 | * neighboring path. |
| 4039 | */ |
| 4040 | el = path_leaf_el(right_path); |
| 4041 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 4042 | if (next_free == 0 || |
| 4043 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 4044 | u32 left_cpos; |
| 4045 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4046 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4047 | right_path, &left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4048 | if (ret) { |
| 4049 | mlog_errno(ret); |
| 4050 | goto out; |
| 4051 | } |
| 4052 | |
| 4053 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 4054 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 4055 | left_cpos); |
| 4056 | |
| 4057 | /* |
| 4058 | * No need to worry if the append is already in the |
| 4059 | * leftmost leaf. |
| 4060 | */ |
| 4061 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4062 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4063 | if (!left_path) { |
| 4064 | ret = -ENOMEM; |
| 4065 | mlog_errno(ret); |
| 4066 | goto out; |
| 4067 | } |
| 4068 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4069 | ret = ocfs2_find_path(et->et_ci, left_path, |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4070 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4071 | if (ret) { |
| 4072 | mlog_errno(ret); |
| 4073 | goto out; |
| 4074 | } |
| 4075 | |
| 4076 | /* |
| 4077 | * ocfs2_insert_path() will pass the left_path to the |
| 4078 | * journal for us. |
| 4079 | */ |
| 4080 | } |
| 4081 | } |
| 4082 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4083 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4084 | if (ret) { |
| 4085 | mlog_errno(ret); |
| 4086 | goto out; |
| 4087 | } |
| 4088 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4089 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4090 | |
| 4091 | *ret_left_path = left_path; |
| 4092 | ret = 0; |
| 4093 | out: |
| 4094 | if (ret != 0) |
| 4095 | ocfs2_free_path(left_path); |
| 4096 | |
| 4097 | return ret; |
| 4098 | } |
| 4099 | |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4100 | static void ocfs2_split_record(struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4101 | struct ocfs2_path *left_path, |
| 4102 | struct ocfs2_path *right_path, |
| 4103 | struct ocfs2_extent_rec *split_rec, |
| 4104 | enum ocfs2_split_type split) |
| 4105 | { |
| 4106 | int index; |
| 4107 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4108 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4109 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4110 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4111 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4112 | if (left_path) |
| 4113 | left_el = path_leaf_el(left_path); |
| 4114 | |
| 4115 | el = right_el; |
| 4116 | insert_el = right_el; |
| 4117 | index = ocfs2_search_extent_list(el, cpos); |
| 4118 | if (index != -1) { |
| 4119 | if (index == 0 && left_path) { |
| 4120 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4121 | |
| 4122 | /* |
| 4123 | * This typically means that the record |
| 4124 | * started in the left path but moved to the |
| 4125 | * right as a result of rotation. We either |
| 4126 | * move the existing record to the left, or we |
| 4127 | * do the later insert there. |
| 4128 | * |
| 4129 | * In this case, the left path should always |
| 4130 | * exist as the rotate code will have passed |
| 4131 | * it back for a post-insert update. |
| 4132 | */ |
| 4133 | |
| 4134 | if (split == SPLIT_LEFT) { |
| 4135 | /* |
| 4136 | * It's a left split. Since we know |
| 4137 | * that the rotate code gave us an |
| 4138 | * empty extent in the left path, we |
| 4139 | * can just do the insert there. |
| 4140 | */ |
| 4141 | insert_el = left_el; |
| 4142 | } else { |
| 4143 | /* |
| 4144 | * Right split - we have to move the |
| 4145 | * existing record over to the left |
| 4146 | * leaf. The insert will be into the |
| 4147 | * newly created empty extent in the |
| 4148 | * right leaf. |
| 4149 | */ |
| 4150 | tmprec = &right_el->l_recs[index]; |
| 4151 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4152 | el = left_el; |
| 4153 | |
| 4154 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4155 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4156 | BUG_ON(index == -1); |
| 4157 | } |
| 4158 | } |
| 4159 | } else { |
| 4160 | BUG_ON(!left_path); |
| 4161 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4162 | /* |
| 4163 | * Left path is easy - we can just allow the insert to |
| 4164 | * happen. |
| 4165 | */ |
| 4166 | el = left_el; |
| 4167 | insert_el = left_el; |
| 4168 | index = ocfs2_search_extent_list(el, cpos); |
| 4169 | BUG_ON(index == -1); |
| 4170 | } |
| 4171 | |
| 4172 | rec = &el->l_recs[index]; |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4173 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4174 | split, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4175 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4176 | } |
| 4177 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4178 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4179 | * This function only does inserts on an allocation b-tree. For tree |
| 4180 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4181 | * |
| 4182 | * right_path is the path we want to do the actual insert |
| 4183 | * in. left_path should only be passed in if we need to update that |
| 4184 | * portion of the tree after an edge insert. |
| 4185 | */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4186 | static int ocfs2_insert_path(handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4187 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4188 | struct ocfs2_path *left_path, |
| 4189 | struct ocfs2_path *right_path, |
| 4190 | struct ocfs2_extent_rec *insert_rec, |
| 4191 | struct ocfs2_insert_type *insert) |
| 4192 | { |
| 4193 | int ret, subtree_index; |
| 4194 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4195 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4196 | if (left_path) { |
| 4197 | int credits = handle->h_buffer_credits; |
| 4198 | |
| 4199 | /* |
| 4200 | * There's a chance that left_path got passed back to |
| 4201 | * us without being accounted for in the |
| 4202 | * journal. Extend our transaction here to be sure we |
| 4203 | * can change those blocks. |
| 4204 | */ |
| 4205 | credits += left_path->p_tree_depth; |
| 4206 | |
| 4207 | ret = ocfs2_extend_trans(handle, credits); |
| 4208 | if (ret < 0) { |
| 4209 | mlog_errno(ret); |
| 4210 | goto out; |
| 4211 | } |
| 4212 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4213 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4214 | if (ret < 0) { |
| 4215 | mlog_errno(ret); |
| 4216 | goto out; |
| 4217 | } |
| 4218 | } |
| 4219 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4220 | /* |
| 4221 | * Pass both paths to the journal. The majority of inserts |
| 4222 | * will be touching all components anyway. |
| 4223 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4224 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4225 | if (ret < 0) { |
| 4226 | mlog_errno(ret); |
| 4227 | goto out; |
| 4228 | } |
| 4229 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4230 | if (insert->ins_split != SPLIT_NONE) { |
| 4231 | /* |
| 4232 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4233 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4234 | * function sort it all out. |
| 4235 | */ |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4236 | ocfs2_split_record(et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4237 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4238 | |
| 4239 | /* |
| 4240 | * Split might have modified either leaf and we don't |
| 4241 | * have a guarantee that the later edge insert will |
| 4242 | * dirty this for us. |
| 4243 | */ |
| 4244 | if (left_path) |
| 4245 | ret = ocfs2_journal_dirty(handle, |
| 4246 | path_leaf_bh(left_path)); |
| 4247 | if (ret) |
| 4248 | mlog_errno(ret); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4249 | } else |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4250 | ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path), |
| 4251 | insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4252 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4253 | ret = ocfs2_journal_dirty(handle, leaf_bh); |
| 4254 | if (ret) |
| 4255 | mlog_errno(ret); |
| 4256 | |
| 4257 | if (left_path) { |
| 4258 | /* |
| 4259 | * The rotate code has indicated that we need to fix |
| 4260 | * up portions of the tree after the insert. |
| 4261 | * |
| 4262 | * XXX: Should we extend the transaction here? |
| 4263 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4264 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4265 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4266 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4267 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | ret = 0; |
| 4271 | out: |
| 4272 | return ret; |
| 4273 | } |
| 4274 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4275 | static int ocfs2_do_insert_extent(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4276 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4277 | struct ocfs2_extent_rec *insert_rec, |
| 4278 | struct ocfs2_insert_type *type) |
| 4279 | { |
| 4280 | int ret, rotate = 0; |
| 4281 | u32 cpos; |
| 4282 | struct ocfs2_path *right_path = NULL; |
| 4283 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4284 | struct ocfs2_extent_list *el; |
| 4285 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4286 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4287 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4288 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4289 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4290 | if (ret) { |
| 4291 | mlog_errno(ret); |
| 4292 | goto out; |
| 4293 | } |
| 4294 | |
| 4295 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4296 | ocfs2_insert_at_leaf(et, insert_rec, el, type); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4297 | goto out_update_clusters; |
| 4298 | } |
| 4299 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4300 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4301 | if (!right_path) { |
| 4302 | ret = -ENOMEM; |
| 4303 | mlog_errno(ret); |
| 4304 | goto out; |
| 4305 | } |
| 4306 | |
| 4307 | /* |
| 4308 | * Determine the path to start with. Rotations need the |
| 4309 | * rightmost path, everything else can go directly to the |
| 4310 | * target leaf. |
| 4311 | */ |
| 4312 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4313 | if (type->ins_appending == APPEND_NONE && |
| 4314 | type->ins_contig == CONTIG_NONE) { |
| 4315 | rotate = 1; |
| 4316 | cpos = UINT_MAX; |
| 4317 | } |
| 4318 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4319 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4320 | if (ret) { |
| 4321 | mlog_errno(ret); |
| 4322 | goto out; |
| 4323 | } |
| 4324 | |
| 4325 | /* |
| 4326 | * Rotations and appends need special treatment - they modify |
| 4327 | * parts of the tree's above them. |
| 4328 | * |
| 4329 | * Both might pass back a path immediate to the left of the |
| 4330 | * one being inserted to. This will be cause |
| 4331 | * ocfs2_insert_path() to modify the rightmost records of |
| 4332 | * left_path to account for an edge insert. |
| 4333 | * |
| 4334 | * XXX: When modifying this code, keep in mind that an insert |
| 4335 | * can wind up skipping both of these two special cases... |
| 4336 | */ |
| 4337 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4338 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4339 | le32_to_cpu(insert_rec->e_cpos), |
| 4340 | right_path, &left_path); |
| 4341 | if (ret) { |
| 4342 | mlog_errno(ret); |
| 4343 | goto out; |
| 4344 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4345 | |
| 4346 | /* |
| 4347 | * ocfs2_rotate_tree_right() might have extended the |
| 4348 | * transaction without re-journaling our tree root. |
| 4349 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4350 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4351 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4352 | if (ret) { |
| 4353 | mlog_errno(ret); |
| 4354 | goto out; |
| 4355 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4356 | } else if (type->ins_appending == APPEND_TAIL |
| 4357 | && type->ins_contig != CONTIG_LEFT) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4358 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4359 | right_path, &left_path); |
| 4360 | if (ret) { |
| 4361 | mlog_errno(ret); |
| 4362 | goto out; |
| 4363 | } |
| 4364 | } |
| 4365 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4366 | ret = ocfs2_insert_path(handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4367 | insert_rec, type); |
| 4368 | if (ret) { |
| 4369 | mlog_errno(ret); |
| 4370 | goto out; |
| 4371 | } |
| 4372 | |
| 4373 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4374 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4375 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4376 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4377 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4378 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4379 | if (ret) |
| 4380 | mlog_errno(ret); |
| 4381 | |
| 4382 | out: |
| 4383 | ocfs2_free_path(left_path); |
| 4384 | ocfs2_free_path(right_path); |
| 4385 | |
| 4386 | return ret; |
| 4387 | } |
| 4388 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4389 | static enum ocfs2_contig_type |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4390 | ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et, |
| 4391 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4392 | struct ocfs2_extent_list *el, int index, |
| 4393 | struct ocfs2_extent_rec *split_rec) |
| 4394 | { |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4395 | int status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4396 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4397 | u32 left_cpos, right_cpos; |
| 4398 | struct ocfs2_extent_rec *rec = NULL; |
| 4399 | struct ocfs2_extent_list *new_el; |
| 4400 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4401 | struct buffer_head *bh; |
| 4402 | struct ocfs2_extent_block *eb; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4403 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4404 | |
| 4405 | if (index > 0) { |
| 4406 | rec = &el->l_recs[index - 1]; |
| 4407 | } else if (path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4408 | status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4409 | if (status) |
| 4410 | goto out; |
| 4411 | |
| 4412 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4413 | left_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4414 | if (!left_path) |
| 4415 | goto out; |
| 4416 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4417 | status = ocfs2_find_path(et->et_ci, left_path, |
| 4418 | left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4419 | if (status) |
| 4420 | goto out; |
| 4421 | |
| 4422 | new_el = path_leaf_el(left_path); |
| 4423 | |
| 4424 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4425 | le16_to_cpu(new_el->l_count)) { |
| 4426 | bh = path_leaf_bh(left_path); |
| 4427 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4428 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4429 | "Extent block #%llu has an " |
| 4430 | "invalid l_next_free_rec of " |
| 4431 | "%d. It should have " |
| 4432 | "matched the l_count of %d", |
| 4433 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4434 | le16_to_cpu(new_el->l_next_free_rec), |
| 4435 | le16_to_cpu(new_el->l_count)); |
| 4436 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4437 | goto out; |
| 4438 | } |
| 4439 | rec = &new_el->l_recs[ |
| 4440 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4441 | } |
| 4442 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4443 | |
| 4444 | /* |
| 4445 | * We're careful to check for an empty extent record here - |
| 4446 | * the merge code will know what to do if it sees one. |
| 4447 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4448 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4449 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4450 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4451 | ret = CONTIG_RIGHT; |
| 4452 | } else { |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4453 | ret = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4457 | rec = NULL; |
| 4458 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4459 | rec = &el->l_recs[index + 1]; |
| 4460 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4461 | path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4462 | status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4463 | if (status) |
| 4464 | goto out; |
| 4465 | |
| 4466 | if (right_cpos == 0) |
| 4467 | goto out; |
| 4468 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4469 | right_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4470 | if (!right_path) |
| 4471 | goto out; |
| 4472 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4473 | status = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4474 | if (status) |
| 4475 | goto out; |
| 4476 | |
| 4477 | new_el = path_leaf_el(right_path); |
| 4478 | rec = &new_el->l_recs[0]; |
| 4479 | if (ocfs2_is_empty_extent(rec)) { |
| 4480 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4481 | bh = path_leaf_bh(right_path); |
| 4482 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4483 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4484 | "Extent block #%llu has an " |
| 4485 | "invalid l_next_free_rec of %d", |
| 4486 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4487 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4488 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4489 | goto out; |
| 4490 | } |
| 4491 | rec = &new_el->l_recs[1]; |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4496 | enum ocfs2_contig_type contig_type; |
| 4497 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4498 | contig_type = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4499 | |
| 4500 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4501 | ret = CONTIG_LEFTRIGHT; |
| 4502 | else if (ret == CONTIG_NONE) |
| 4503 | ret = contig_type; |
| 4504 | } |
| 4505 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4506 | out: |
| 4507 | if (left_path) |
| 4508 | ocfs2_free_path(left_path); |
| 4509 | if (right_path) |
| 4510 | ocfs2_free_path(right_path); |
| 4511 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4512 | return ret; |
| 4513 | } |
| 4514 | |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4515 | static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4516 | struct ocfs2_insert_type *insert, |
| 4517 | struct ocfs2_extent_list *el, |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4518 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4519 | { |
| 4520 | int i; |
| 4521 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4522 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4523 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4524 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4525 | 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] | 4526 | contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i], |
| 4527 | insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4528 | if (contig_type != CONTIG_NONE) { |
| 4529 | insert->ins_contig_index = i; |
| 4530 | break; |
| 4531 | } |
| 4532 | } |
| 4533 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4534 | |
| 4535 | if (insert->ins_contig != CONTIG_NONE) { |
| 4536 | struct ocfs2_extent_rec *rec = |
| 4537 | &el->l_recs[insert->ins_contig_index]; |
| 4538 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4539 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4540 | |
| 4541 | /* |
| 4542 | * Caller might want us to limit the size of extents, don't |
| 4543 | * calculate contiguousness if we might exceed that limit. |
| 4544 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4545 | if (et->et_max_leaf_clusters && |
| 4546 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4547 | insert->ins_contig = CONTIG_NONE; |
| 4548 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | /* |
| 4552 | * This should only be called against the righmost leaf extent list. |
| 4553 | * |
| 4554 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4555 | * insert at the tail of the rightmost leaf. |
| 4556 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4557 | * This should also work against the root extent list for tree's with 0 |
| 4558 | * 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] | 4559 | * then the logic here makes sense. |
| 4560 | */ |
| 4561 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4562 | struct ocfs2_extent_list *el, |
| 4563 | struct ocfs2_extent_rec *insert_rec) |
| 4564 | { |
| 4565 | int i; |
| 4566 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4567 | struct ocfs2_extent_rec *rec; |
| 4568 | |
| 4569 | insert->ins_appending = APPEND_NONE; |
| 4570 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4571 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4572 | |
| 4573 | if (!el->l_next_free_rec) |
| 4574 | goto set_tail_append; |
| 4575 | |
| 4576 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4577 | /* Were all records empty? */ |
| 4578 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4579 | goto set_tail_append; |
| 4580 | } |
| 4581 | |
| 4582 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4583 | rec = &el->l_recs[i]; |
| 4584 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4585 | if (cpos >= |
| 4586 | (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] | 4587 | goto set_tail_append; |
| 4588 | |
| 4589 | return; |
| 4590 | |
| 4591 | set_tail_append: |
| 4592 | insert->ins_appending = APPEND_TAIL; |
| 4593 | } |
| 4594 | |
| 4595 | /* |
| 4596 | * Helper function called at the begining of an insert. |
| 4597 | * |
| 4598 | * This computes a few things that are commonly used in the process of |
| 4599 | * inserting into the btree: |
| 4600 | * - Whether the new extent is contiguous with an existing one. |
| 4601 | * - The current tree depth. |
| 4602 | * - Whether the insert is an appending one. |
| 4603 | * - The total # of free records in the tree. |
| 4604 | * |
| 4605 | * All of the information is stored on the ocfs2_insert_type |
| 4606 | * structure. |
| 4607 | */ |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4608 | static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4609 | struct buffer_head **last_eb_bh, |
| 4610 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4611 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4612 | struct ocfs2_insert_type *insert) |
| 4613 | { |
| 4614 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4615 | struct ocfs2_extent_block *eb; |
| 4616 | struct ocfs2_extent_list *el; |
| 4617 | struct ocfs2_path *path = NULL; |
| 4618 | struct buffer_head *bh = NULL; |
| 4619 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4620 | insert->ins_split = SPLIT_NONE; |
| 4621 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4622 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4623 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4624 | |
| 4625 | if (el->l_tree_depth) { |
| 4626 | /* |
| 4627 | * If we have tree depth, we read in the |
| 4628 | * rightmost extent block ahead of time as |
| 4629 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4630 | * may want it later. |
| 4631 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4632 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4633 | ocfs2_et_get_last_eb_blk(et), |
| 4634 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4635 | if (ret) { |
| 4636 | mlog_exit(ret); |
| 4637 | goto out; |
| 4638 | } |
| 4639 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4640 | el = &eb->h_list; |
| 4641 | } |
| 4642 | |
| 4643 | /* |
| 4644 | * Unless we have a contiguous insert, we'll need to know if |
| 4645 | * there is room left in our allocation tree for another |
| 4646 | * extent record. |
| 4647 | * |
| 4648 | * XXX: This test is simplistic, we can search for empty |
| 4649 | * extent records too. |
| 4650 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4651 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4652 | le16_to_cpu(el->l_next_free_rec); |
| 4653 | |
| 4654 | if (!insert->ins_tree_depth) { |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4655 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4656 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4660 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4661 | if (!path) { |
| 4662 | ret = -ENOMEM; |
| 4663 | mlog_errno(ret); |
| 4664 | goto out; |
| 4665 | } |
| 4666 | |
| 4667 | /* |
| 4668 | * In the case that we're inserting past what the tree |
| 4669 | * currently accounts for, ocfs2_find_path() will return for |
| 4670 | * us the rightmost tree path. This is accounted for below in |
| 4671 | * the appending code. |
| 4672 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4673 | 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] | 4674 | if (ret) { |
| 4675 | mlog_errno(ret); |
| 4676 | goto out; |
| 4677 | } |
| 4678 | |
| 4679 | el = path_leaf_el(path); |
| 4680 | |
| 4681 | /* |
| 4682 | * Now that we have the path, there's two things we want to determine: |
| 4683 | * 1) Contiguousness (also set contig_index if this is so) |
| 4684 | * |
| 4685 | * 2) Are we doing an append? We can trivially break this up |
| 4686 | * into two types of appends: simple record append, or a |
| 4687 | * rotate inside the tail leaf. |
| 4688 | */ |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4689 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4690 | |
| 4691 | /* |
| 4692 | * The insert code isn't quite ready to deal with all cases of |
| 4693 | * left contiguousness. Specifically, if it's an insert into |
| 4694 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4695 | * 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] | 4696 | * left. For now, just catch that case and fool the layers |
| 4697 | * above us. This works just fine for tree_depth == 0, which |
| 4698 | * is why we allow that above. |
| 4699 | */ |
| 4700 | if (insert->ins_contig == CONTIG_LEFT && |
| 4701 | insert->ins_contig_index == 0) |
| 4702 | insert->ins_contig = CONTIG_NONE; |
| 4703 | |
| 4704 | /* |
| 4705 | * Ok, so we can simply compare against last_eb to figure out |
| 4706 | * whether the path doesn't exist. This will only happen in |
| 4707 | * the case that we're doing a tail append, so maybe we can |
| 4708 | * take advantage of that information somehow. |
| 4709 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4710 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4711 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4712 | /* |
| 4713 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4714 | * tree path. This might be an appending insert. There are |
| 4715 | * two cases: |
| 4716 | * 1) We're doing a true append at the tail: |
| 4717 | * -This might even be off the end of the leaf |
| 4718 | * 2) We're "appending" by rotating in the tail |
| 4719 | */ |
| 4720 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4721 | } |
| 4722 | |
| 4723 | out: |
| 4724 | ocfs2_free_path(path); |
| 4725 | |
| 4726 | if (ret == 0) |
| 4727 | *last_eb_bh = bh; |
| 4728 | else |
| 4729 | brelse(bh); |
| 4730 | return ret; |
| 4731 | } |
| 4732 | |
| 4733 | /* |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4734 | * Insert an extent into a btree. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4735 | * |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4736 | * The caller needs to update the owning btree's cluster count. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4737 | */ |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4738 | int ocfs2_insert_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4739 | struct ocfs2_extent_tree *et, |
| 4740 | u32 cpos, |
| 4741 | u64 start_blk, |
| 4742 | u32 new_clusters, |
| 4743 | u8 flags, |
| 4744 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4745 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4746 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4747 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4748 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4749 | struct ocfs2_insert_type insert = {0, }; |
| 4750 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4751 | |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4752 | mlog(0, "add %u clusters at position %u to owner %llu\n", |
| 4753 | new_clusters, cpos, |
| 4754 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4755 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4756 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4757 | rec.e_cpos = cpu_to_le32(cpos); |
| 4758 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4759 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4760 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4761 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4762 | if (status) { |
| 4763 | mlog_errno(status); |
| 4764 | goto bail; |
| 4765 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4766 | |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4767 | status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4768 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4769 | if (status < 0) { |
| 4770 | mlog_errno(status); |
| 4771 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4772 | } |
| 4773 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4774 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 4775 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 4776 | "Insert.tree_depth: %d\n", |
| 4777 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4778 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4779 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4780 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4781 | status = ocfs2_grow_tree(handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4782 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4783 | meta_ac); |
| 4784 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4785 | mlog_errno(status); |
| 4786 | goto bail; |
| 4787 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4788 | } |
| 4789 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4790 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4791 | status = ocfs2_do_insert_extent(handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4792 | if (status < 0) |
| 4793 | mlog_errno(status); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 4794 | else |
| 4795 | ocfs2_et_extent_map_insert(et, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4796 | |
| 4797 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4798 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4799 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4800 | mlog_exit(status); |
| 4801 | return status; |
| 4802 | } |
| 4803 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4804 | /* |
| 4805 | * Allcate and add clusters into the extent b-tree. |
| 4806 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4807 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4808 | * it is not limited to the file storage. Any extent tree can use this |
| 4809 | * function if it implements the proper ocfs2_extent_tree. |
| 4810 | */ |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4811 | int ocfs2_add_clusters_in_btree(handle_t *handle, |
| 4812 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4813 | u32 *logical_offset, |
| 4814 | u32 clusters_to_add, |
| 4815 | int mark_unwritten, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4816 | struct ocfs2_alloc_context *data_ac, |
| 4817 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4818 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4819 | { |
| 4820 | int status = 0; |
| 4821 | int free_extents; |
| 4822 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4823 | u32 bit_off, num_bits; |
| 4824 | u64 block; |
| 4825 | u8 flags = 0; |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4826 | struct ocfs2_super *osb = |
| 4827 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4828 | |
| 4829 | BUG_ON(!clusters_to_add); |
| 4830 | |
| 4831 | if (mark_unwritten) |
| 4832 | flags = OCFS2_EXT_UNWRITTEN; |
| 4833 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4834 | free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4835 | if (free_extents < 0) { |
| 4836 | status = free_extents; |
| 4837 | mlog_errno(status); |
| 4838 | goto leave; |
| 4839 | } |
| 4840 | |
| 4841 | /* there are two cases which could cause us to EAGAIN in the |
| 4842 | * we-need-more-metadata case: |
| 4843 | * 1) we haven't reserved *any* |
| 4844 | * 2) we are so fragmented, we've needed to add metadata too |
| 4845 | * many times. */ |
| 4846 | if (!free_extents && !meta_ac) { |
| 4847 | mlog(0, "we haven't reserved any metadata!\n"); |
| 4848 | status = -EAGAIN; |
| 4849 | reason = RESTART_META; |
| 4850 | goto leave; |
| 4851 | } else if ((!free_extents) |
| 4852 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4853 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4854 | mlog(0, "filesystem is really fragmented...\n"); |
| 4855 | status = -EAGAIN; |
| 4856 | reason = RESTART_META; |
| 4857 | goto leave; |
| 4858 | } |
| 4859 | |
| 4860 | status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 4861 | clusters_to_add, &bit_off, &num_bits); |
| 4862 | if (status < 0) { |
| 4863 | if (status != -ENOSPC) |
| 4864 | mlog_errno(status); |
| 4865 | goto leave; |
| 4866 | } |
| 4867 | |
| 4868 | BUG_ON(num_bits > clusters_to_add); |
| 4869 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4870 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4871 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4872 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4873 | if (status < 0) { |
| 4874 | mlog_errno(status); |
| 4875 | goto leave; |
| 4876 | } |
| 4877 | |
| 4878 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4879 | mlog(0, "Allocating %u clusters at block %u for owner %llu\n", |
| 4880 | num_bits, bit_off, |
| 4881 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4882 | status = ocfs2_insert_extent(handle, et, *logical_offset, block, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4883 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4884 | if (status < 0) { |
| 4885 | mlog_errno(status); |
| 4886 | goto leave; |
| 4887 | } |
| 4888 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4889 | status = ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4890 | if (status < 0) { |
| 4891 | mlog_errno(status); |
| 4892 | goto leave; |
| 4893 | } |
| 4894 | |
| 4895 | clusters_to_add -= num_bits; |
| 4896 | *logical_offset += num_bits; |
| 4897 | |
| 4898 | if (clusters_to_add) { |
| 4899 | mlog(0, "need to alloc once more, wanted = %u\n", |
| 4900 | clusters_to_add); |
| 4901 | status = -EAGAIN; |
| 4902 | reason = RESTART_TRANS; |
| 4903 | } |
| 4904 | |
| 4905 | leave: |
| 4906 | mlog_exit(status); |
| 4907 | if (reason_ret) |
| 4908 | *reason_ret = reason; |
| 4909 | return status; |
| 4910 | } |
| 4911 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4912 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4913 | struct ocfs2_extent_rec *split_rec, |
| 4914 | u32 cpos, |
| 4915 | struct ocfs2_extent_rec *rec) |
| 4916 | { |
| 4917 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4918 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4919 | |
| 4920 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4921 | |
| 4922 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4923 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4924 | |
| 4925 | split_rec->e_blkno = rec->e_blkno; |
| 4926 | le64_add_cpu(&split_rec->e_blkno, |
| 4927 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4928 | |
| 4929 | split_rec->e_flags = rec->e_flags; |
| 4930 | } |
| 4931 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4932 | static int ocfs2_split_and_insert(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4933 | struct ocfs2_extent_tree *et, |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4934 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4935 | struct buffer_head **last_eb_bh, |
| 4936 | int split_index, |
| 4937 | struct ocfs2_extent_rec *orig_split_rec, |
| 4938 | struct ocfs2_alloc_context *meta_ac) |
| 4939 | { |
| 4940 | int ret = 0, depth; |
| 4941 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4942 | struct ocfs2_extent_rec tmprec; |
| 4943 | struct ocfs2_extent_list *rightmost_el; |
| 4944 | struct ocfs2_extent_rec rec; |
| 4945 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4946 | struct ocfs2_insert_type insert; |
| 4947 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4948 | |
| 4949 | leftright: |
| 4950 | /* |
| 4951 | * Store a copy of the record on the stack - it might move |
| 4952 | * around as the tree is manipulated below. |
| 4953 | */ |
| 4954 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4955 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4956 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4957 | |
| 4958 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4959 | if (depth) { |
| 4960 | BUG_ON(!(*last_eb_bh)); |
| 4961 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4962 | rightmost_el = &eb->h_list; |
| 4963 | } |
| 4964 | |
| 4965 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4966 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4967 | ret = ocfs2_grow_tree(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4968 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4969 | if (ret) { |
| 4970 | mlog_errno(ret); |
| 4971 | goto out; |
| 4972 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4976 | insert.ins_appending = APPEND_NONE; |
| 4977 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4978 | insert.ins_tree_depth = depth; |
| 4979 | |
| 4980 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 4981 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 4982 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 4983 | le16_to_cpu(rec.e_leaf_clusters); |
| 4984 | |
| 4985 | if (split_rec.e_cpos == rec.e_cpos) { |
| 4986 | insert.ins_split = SPLIT_LEFT; |
| 4987 | } else if (insert_range == rec_range) { |
| 4988 | insert.ins_split = SPLIT_RIGHT; |
| 4989 | } else { |
| 4990 | /* |
| 4991 | * Left/right split. We fake this as a right split |
| 4992 | * first and then make a second pass as a left split. |
| 4993 | */ |
| 4994 | insert.ins_split = SPLIT_RIGHT; |
| 4995 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4996 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4997 | &tmprec, insert_range, &rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4998 | |
| 4999 | split_rec = tmprec; |
| 5000 | |
| 5001 | BUG_ON(do_leftright); |
| 5002 | do_leftright = 1; |
| 5003 | } |
| 5004 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5005 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5006 | if (ret) { |
| 5007 | mlog_errno(ret); |
| 5008 | goto out; |
| 5009 | } |
| 5010 | |
| 5011 | if (do_leftright == 1) { |
| 5012 | u32 cpos; |
| 5013 | struct ocfs2_extent_list *el; |
| 5014 | |
| 5015 | do_leftright++; |
| 5016 | split_rec = *orig_split_rec; |
| 5017 | |
| 5018 | ocfs2_reinit_path(path, 1); |
| 5019 | |
| 5020 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5021 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5022 | if (ret) { |
| 5023 | mlog_errno(ret); |
| 5024 | goto out; |
| 5025 | } |
| 5026 | |
| 5027 | el = path_leaf_el(path); |
| 5028 | split_index = ocfs2_search_extent_list(el, cpos); |
| 5029 | goto leftright; |
| 5030 | } |
| 5031 | out: |
| 5032 | |
| 5033 | return ret; |
| 5034 | } |
| 5035 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5036 | static int ocfs2_replace_extent_rec(handle_t *handle, |
| 5037 | struct ocfs2_extent_tree *et, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5038 | struct ocfs2_path *path, |
| 5039 | struct ocfs2_extent_list *el, |
| 5040 | int split_index, |
| 5041 | struct ocfs2_extent_rec *split_rec) |
| 5042 | { |
| 5043 | int ret; |
| 5044 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5045 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5046 | path_num_items(path) - 1); |
| 5047 | if (ret) { |
| 5048 | mlog_errno(ret); |
| 5049 | goto out; |
| 5050 | } |
| 5051 | |
| 5052 | el->l_recs[split_index] = *split_rec; |
| 5053 | |
| 5054 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5055 | out: |
| 5056 | return ret; |
| 5057 | } |
| 5058 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5059 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5060 | * Split part or all of the extent record at split_index in the leaf |
| 5061 | * pointed to by path. Merge with the contiguous extent record if needed. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5062 | * |
| 5063 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 5064 | * |
| 5065 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 5066 | * of the tree is required. All other cases will degrade into a less |
| 5067 | * optimal tree layout. |
| 5068 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5069 | * last_eb_bh should be the rightmost leaf block for any extent |
| 5070 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 5071 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5072 | * |
| 5073 | * This code is optimized for readability - several passes might be |
| 5074 | * made over certain portions of the tree. All of those blocks will |
| 5075 | * have been brought into cache (and pinned via the journal), so the |
| 5076 | * extra overhead is not expressed in terms of disk reads. |
| 5077 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 5078 | int ocfs2_split_extent(handle_t *handle, |
| 5079 | struct ocfs2_extent_tree *et, |
| 5080 | struct ocfs2_path *path, |
| 5081 | int split_index, |
| 5082 | struct ocfs2_extent_rec *split_rec, |
| 5083 | struct ocfs2_alloc_context *meta_ac, |
| 5084 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5085 | { |
| 5086 | int ret = 0; |
| 5087 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 5088 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5089 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 5090 | struct ocfs2_merge_ctxt ctxt; |
| 5091 | struct ocfs2_extent_list *rightmost_el; |
| 5092 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5093 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5094 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5095 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5096 | ret = -EIO; |
| 5097 | mlog_errno(ret); |
| 5098 | goto out; |
| 5099 | } |
| 5100 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 5101 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5102 | split_index, |
| 5103 | split_rec); |
| 5104 | |
| 5105 | /* |
| 5106 | * The core merge / split code wants to know how much room is |
Joel Becker | a1cf076 | 2009-02-13 03:45:49 -0800 | [diff] [blame] | 5107 | * left in this allocation tree, so we pass the |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5108 | * rightmost extent list. |
| 5109 | */ |
| 5110 | if (path->p_tree_depth) { |
| 5111 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5112 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5113 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5114 | ocfs2_et_get_last_eb_blk(et), |
| 5115 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5116 | if (ret) { |
| 5117 | mlog_exit(ret); |
| 5118 | goto out; |
| 5119 | } |
| 5120 | |
| 5121 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5122 | rightmost_el = &eb->h_list; |
| 5123 | } else |
| 5124 | rightmost_el = path_root_el(path); |
| 5125 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5126 | if (rec->e_cpos == split_rec->e_cpos && |
| 5127 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5128 | ctxt.c_split_covers_rec = 1; |
| 5129 | else |
| 5130 | ctxt.c_split_covers_rec = 0; |
| 5131 | |
| 5132 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5133 | |
Mark Fasheh | 015452b | 2007-09-12 10:21:22 -0700 | [diff] [blame] | 5134 | mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", |
| 5135 | split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, |
| 5136 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5137 | |
| 5138 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5139 | if (ctxt.c_split_covers_rec) |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5140 | ret = ocfs2_replace_extent_rec(handle, et, path, el, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5141 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5142 | else |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 5143 | ret = ocfs2_split_and_insert(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5144 | &last_eb_bh, split_index, |
| 5145 | split_rec, meta_ac); |
| 5146 | if (ret) |
| 5147 | mlog_errno(ret); |
| 5148 | } else { |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5149 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5150 | split_index, split_rec, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5151 | dealloc, &ctxt); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5152 | if (ret) |
| 5153 | mlog_errno(ret); |
| 5154 | } |
| 5155 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5156 | out: |
| 5157 | brelse(last_eb_bh); |
| 5158 | return ret; |
| 5159 | } |
| 5160 | |
| 5161 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5162 | * Change the flags of the already-existing extent at cpos for len clusters. |
| 5163 | * |
| 5164 | * new_flags: the flags we want to set. |
| 5165 | * clear_flags: the flags we want to clear. |
| 5166 | * phys: the new physical offset we want this new extent starts from. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5167 | * |
| 5168 | * If the existing extent is larger than the request, initiate a |
| 5169 | * split. An attempt will be made at merging with adjacent extents. |
| 5170 | * |
| 5171 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5172 | */ |
Tao Ma | 1aa75fe | 2009-08-18 11:28:39 +0800 | [diff] [blame] | 5173 | int ocfs2_change_extent_flag(handle_t *handle, |
| 5174 | struct ocfs2_extent_tree *et, |
| 5175 | u32 cpos, u32 len, u32 phys, |
| 5176 | struct ocfs2_alloc_context *meta_ac, |
| 5177 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 5178 | int new_flags, int clear_flags) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5179 | { |
| 5180 | int ret, index; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5181 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
| 5182 | u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5183 | struct ocfs2_extent_rec split_rec; |
| 5184 | struct ocfs2_path *left_path = NULL; |
| 5185 | struct ocfs2_extent_list *el; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5186 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5187 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5188 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5189 | if (!left_path) { |
| 5190 | ret = -ENOMEM; |
| 5191 | mlog_errno(ret); |
| 5192 | goto out; |
| 5193 | } |
| 5194 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5195 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5196 | if (ret) { |
| 5197 | mlog_errno(ret); |
| 5198 | goto out; |
| 5199 | } |
| 5200 | el = path_leaf_el(left_path); |
| 5201 | |
| 5202 | index = ocfs2_search_extent_list(el, cpos); |
| 5203 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5204 | ocfs2_error(sb, |
| 5205 | "Owner %llu has an extent at cpos %u which can no " |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5206 | "longer be found.\n", |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5207 | (unsigned long long) |
| 5208 | ocfs2_metadata_cache_owner(et->et_ci), cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5209 | ret = -EROFS; |
| 5210 | goto out; |
| 5211 | } |
| 5212 | |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5213 | ret = -EIO; |
| 5214 | rec = &el->l_recs[index]; |
| 5215 | if (new_flags && (rec->e_flags & new_flags)) { |
| 5216 | mlog(ML_ERROR, "Owner %llu tried to set %d flags on an " |
| 5217 | "extent that already had them", |
| 5218 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5219 | new_flags); |
| 5220 | goto out; |
| 5221 | } |
| 5222 | |
| 5223 | if (clear_flags && !(rec->e_flags & clear_flags)) { |
| 5224 | mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an " |
| 5225 | "extent that didn't have them", |
| 5226 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5227 | clear_flags); |
| 5228 | goto out; |
| 5229 | } |
| 5230 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5231 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5232 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5233 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5234 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5235 | split_rec.e_flags = rec->e_flags; |
| 5236 | if (new_flags) |
| 5237 | split_rec.e_flags |= new_flags; |
| 5238 | if (clear_flags) |
| 5239 | split_rec.e_flags &= ~clear_flags; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5240 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 5241 | ret = ocfs2_split_extent(handle, et, left_path, |
| 5242 | index, &split_rec, meta_ac, |
| 5243 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5244 | if (ret) |
| 5245 | mlog_errno(ret); |
| 5246 | |
| 5247 | out: |
| 5248 | ocfs2_free_path(left_path); |
| 5249 | return ret; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5250 | |
| 5251 | } |
| 5252 | |
| 5253 | /* |
| 5254 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5255 | * This removes the unwritten extent flag. |
| 5256 | * |
| 5257 | * If the existing extent is larger than the request, initiate a |
| 5258 | * split. An attempt will be made at merging with adjacent extents. |
| 5259 | * |
| 5260 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5261 | */ |
| 5262 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5263 | struct ocfs2_extent_tree *et, |
| 5264 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5265 | struct ocfs2_alloc_context *meta_ac, |
| 5266 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5267 | { |
| 5268 | int ret; |
| 5269 | |
| 5270 | mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n", |
| 5271 | inode->i_ino, cpos, len, phys); |
| 5272 | |
| 5273 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 5274 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 5275 | "that are being written to, but the feature bit " |
| 5276 | "is not set in the super block.", |
| 5277 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5278 | ret = -EROFS; |
| 5279 | goto out; |
| 5280 | } |
| 5281 | |
| 5282 | /* |
| 5283 | * XXX: This should be fixed up so that we just re-insert the |
| 5284 | * next extent records. |
| 5285 | */ |
| 5286 | ocfs2_et_extent_map_truncate(et, 0); |
| 5287 | |
| 5288 | ret = ocfs2_change_extent_flag(handle, et, cpos, |
| 5289 | len, phys, meta_ac, dealloc, |
| 5290 | 0, OCFS2_EXT_UNWRITTEN); |
| 5291 | if (ret) |
| 5292 | mlog_errno(ret); |
| 5293 | |
| 5294 | out: |
| 5295 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5296 | } |
| 5297 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5298 | static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 5299 | struct ocfs2_path *path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5300 | int index, u32 new_range, |
| 5301 | struct ocfs2_alloc_context *meta_ac) |
| 5302 | { |
| 5303 | int ret, depth, credits = handle->h_buffer_credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5304 | struct buffer_head *last_eb_bh = NULL; |
| 5305 | struct ocfs2_extent_block *eb; |
| 5306 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5307 | struct ocfs2_extent_rec split_rec; |
| 5308 | struct ocfs2_extent_rec *rec; |
| 5309 | struct ocfs2_insert_type insert; |
| 5310 | |
| 5311 | /* |
| 5312 | * Setup the record to split before we grow the tree. |
| 5313 | */ |
| 5314 | el = path_leaf_el(path); |
| 5315 | rec = &el->l_recs[index]; |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5316 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5317 | &split_rec, new_range, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5318 | |
| 5319 | depth = path->p_tree_depth; |
| 5320 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5321 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5322 | ocfs2_et_get_last_eb_blk(et), |
| 5323 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5324 | if (ret < 0) { |
| 5325 | mlog_errno(ret); |
| 5326 | goto out; |
| 5327 | } |
| 5328 | |
| 5329 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5330 | rightmost_el = &eb->h_list; |
| 5331 | } else |
| 5332 | rightmost_el = path_leaf_el(path); |
| 5333 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 5334 | credits += path->p_tree_depth + |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 5335 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5336 | ret = ocfs2_extend_trans(handle, credits); |
| 5337 | if (ret) { |
| 5338 | mlog_errno(ret); |
| 5339 | goto out; |
| 5340 | } |
| 5341 | |
| 5342 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5343 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5344 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5345 | meta_ac); |
| 5346 | if (ret) { |
| 5347 | mlog_errno(ret); |
| 5348 | goto out; |
| 5349 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5353 | insert.ins_appending = APPEND_NONE; |
| 5354 | insert.ins_contig = CONTIG_NONE; |
| 5355 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5356 | insert.ins_tree_depth = depth; |
| 5357 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5358 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5359 | if (ret) |
| 5360 | mlog_errno(ret); |
| 5361 | |
| 5362 | out: |
| 5363 | brelse(last_eb_bh); |
| 5364 | return ret; |
| 5365 | } |
| 5366 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5367 | static int ocfs2_truncate_rec(handle_t *handle, |
| 5368 | struct ocfs2_extent_tree *et, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5369 | struct ocfs2_path *path, int index, |
| 5370 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5371 | u32 cpos, u32 len) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5372 | { |
| 5373 | int ret; |
| 5374 | u32 left_cpos, rec_range, trunc_range; |
| 5375 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5376 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5377 | struct ocfs2_path *left_path = NULL; |
| 5378 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5379 | struct ocfs2_extent_rec *rec; |
| 5380 | struct ocfs2_extent_block *eb; |
| 5381 | |
| 5382 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5383 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5384 | if (ret) { |
| 5385 | mlog_errno(ret); |
| 5386 | goto out; |
| 5387 | } |
| 5388 | |
| 5389 | index--; |
| 5390 | } |
| 5391 | |
| 5392 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5393 | path->p_tree_depth) { |
| 5394 | /* |
| 5395 | * Check whether this is the rightmost tree record. If |
| 5396 | * we remove all of this record or part of its right |
| 5397 | * edge then an update of the record lengths above it |
| 5398 | * will be required. |
| 5399 | */ |
| 5400 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5401 | if (eb->h_next_leaf_blk == 0) |
| 5402 | is_rightmost_tree_rec = 1; |
| 5403 | } |
| 5404 | |
| 5405 | rec = &el->l_recs[index]; |
| 5406 | if (index == 0 && path->p_tree_depth && |
| 5407 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5408 | /* |
| 5409 | * Changing the leftmost offset (via partial or whole |
| 5410 | * record truncate) of an interior (or rightmost) path |
| 5411 | * means we have to update the subtree that is formed |
| 5412 | * by this leaf and the one to it's left. |
| 5413 | * |
| 5414 | * There are two cases we can skip: |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5415 | * 1) Path is the leftmost one in our btree. |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5416 | * 2) The leaf is rightmost and will be empty after |
| 5417 | * we remove the extent record - the rotate code |
| 5418 | * knows how to update the newly formed edge. |
| 5419 | */ |
| 5420 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5421 | ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5422 | if (ret) { |
| 5423 | mlog_errno(ret); |
| 5424 | goto out; |
| 5425 | } |
| 5426 | |
| 5427 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5428 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5429 | if (!left_path) { |
| 5430 | ret = -ENOMEM; |
| 5431 | mlog_errno(ret); |
| 5432 | goto out; |
| 5433 | } |
| 5434 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5435 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5436 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5437 | if (ret) { |
| 5438 | mlog_errno(ret); |
| 5439 | goto out; |
| 5440 | } |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5445 | handle->h_buffer_credits, |
| 5446 | path); |
| 5447 | if (ret) { |
| 5448 | mlog_errno(ret); |
| 5449 | goto out; |
| 5450 | } |
| 5451 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5452 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5453 | if (ret) { |
| 5454 | mlog_errno(ret); |
| 5455 | goto out; |
| 5456 | } |
| 5457 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5458 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5459 | if (ret) { |
| 5460 | mlog_errno(ret); |
| 5461 | goto out; |
| 5462 | } |
| 5463 | |
| 5464 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5465 | trunc_range = cpos + len; |
| 5466 | |
| 5467 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5468 | int next_free; |
| 5469 | |
| 5470 | memset(rec, 0, sizeof(*rec)); |
| 5471 | ocfs2_cleanup_merge(el, index); |
| 5472 | wants_rotate = 1; |
| 5473 | |
| 5474 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5475 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5476 | /* |
| 5477 | * We skip the edge update if this path will |
| 5478 | * be deleted by the rotate code. |
| 5479 | */ |
| 5480 | rec = &el->l_recs[next_free - 1]; |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5481 | ocfs2_adjust_rightmost_records(handle, et, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5482 | rec); |
| 5483 | } |
| 5484 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5485 | /* Remove leftmost portion of the record. */ |
| 5486 | le32_add_cpu(&rec->e_cpos, len); |
| 5487 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5488 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5489 | } else if (rec_range == trunc_range) { |
| 5490 | /* Remove rightmost portion of the record */ |
| 5491 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5492 | if (is_rightmost_tree_rec) |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5493 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5494 | } else { |
| 5495 | /* Caller should have trapped this. */ |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5496 | mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) " |
| 5497 | "(%u, %u)\n", |
| 5498 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5499 | le32_to_cpu(rec->e_cpos), |
| 5500 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5501 | BUG(); |
| 5502 | } |
| 5503 | |
| 5504 | if (left_path) { |
| 5505 | int subtree_index; |
| 5506 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5507 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5508 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5509 | subtree_index); |
| 5510 | } |
| 5511 | |
| 5512 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5513 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5514 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5515 | if (ret) { |
| 5516 | mlog_errno(ret); |
| 5517 | goto out; |
| 5518 | } |
| 5519 | |
| 5520 | out: |
| 5521 | ocfs2_free_path(left_path); |
| 5522 | return ret; |
| 5523 | } |
| 5524 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5525 | int ocfs2_remove_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5526 | struct ocfs2_extent_tree *et, |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5527 | u32 cpos, u32 len, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5528 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5529 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5530 | { |
| 5531 | int ret, index; |
| 5532 | u32 rec_range, trunc_range; |
| 5533 | struct ocfs2_extent_rec *rec; |
| 5534 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5535 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5536 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 5537 | /* |
| 5538 | * XXX: Why are we truncating to 0 instead of wherever this |
| 5539 | * affects us? |
| 5540 | */ |
| 5541 | ocfs2_et_extent_map_truncate(et, 0); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5542 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5543 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5544 | if (!path) { |
| 5545 | ret = -ENOMEM; |
| 5546 | mlog_errno(ret); |
| 5547 | goto out; |
| 5548 | } |
| 5549 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5550 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5551 | if (ret) { |
| 5552 | mlog_errno(ret); |
| 5553 | goto out; |
| 5554 | } |
| 5555 | |
| 5556 | el = path_leaf_el(path); |
| 5557 | index = ocfs2_search_extent_list(el, cpos); |
| 5558 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5559 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5560 | "Owner %llu has an extent at cpos %u which can no " |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5561 | "longer be found.\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5562 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5563 | cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5564 | ret = -EROFS; |
| 5565 | goto out; |
| 5566 | } |
| 5567 | |
| 5568 | /* |
| 5569 | * We have 3 cases of extent removal: |
| 5570 | * 1) Range covers the entire extent rec |
| 5571 | * 2) Range begins or ends on one edge of the extent rec |
| 5572 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5573 | * |
| 5574 | * For case 1 we remove the extent rec and left rotate to |
| 5575 | * fill the hole. |
| 5576 | * |
| 5577 | * For case 2 we just shrink the existing extent rec, with a |
| 5578 | * tree update if the shrinking edge is also the edge of an |
| 5579 | * extent block. |
| 5580 | * |
| 5581 | * For case 3 we do a right split to turn the extent rec into |
| 5582 | * something case 2 can handle. |
| 5583 | */ |
| 5584 | rec = &el->l_recs[index]; |
| 5585 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5586 | trunc_range = cpos + len; |
| 5587 | |
| 5588 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5589 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5590 | mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d " |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5591 | "(cpos %u, len %u)\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5592 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5593 | cpos, len, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5594 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 5595 | |
| 5596 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5597 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5598 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5599 | if (ret) { |
| 5600 | mlog_errno(ret); |
| 5601 | goto out; |
| 5602 | } |
| 5603 | } else { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5604 | ret = ocfs2_split_tree(handle, et, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5605 | trunc_range, meta_ac); |
| 5606 | if (ret) { |
| 5607 | mlog_errno(ret); |
| 5608 | goto out; |
| 5609 | } |
| 5610 | |
| 5611 | /* |
| 5612 | * The split could have manipulated the tree enough to |
| 5613 | * move the record location, so we have to look for it again. |
| 5614 | */ |
| 5615 | ocfs2_reinit_path(path, 1); |
| 5616 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5617 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5618 | if (ret) { |
| 5619 | mlog_errno(ret); |
| 5620 | goto out; |
| 5621 | } |
| 5622 | |
| 5623 | el = path_leaf_el(path); |
| 5624 | index = ocfs2_search_extent_list(el, cpos); |
| 5625 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5626 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5627 | "Owner %llu: split at cpos %u lost record.", |
| 5628 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5629 | cpos); |
| 5630 | ret = -EROFS; |
| 5631 | goto out; |
| 5632 | } |
| 5633 | |
| 5634 | /* |
| 5635 | * Double check our values here. If anything is fishy, |
| 5636 | * it's easier to catch it at the top level. |
| 5637 | */ |
| 5638 | rec = &el->l_recs[index]; |
| 5639 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5640 | ocfs2_rec_clusters(el, rec); |
| 5641 | if (rec_range != trunc_range) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5642 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5643 | "Owner %llu: error after split at cpos %u" |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5644 | "trunc len %u, existing record is (%u,%u)", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5645 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5646 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5647 | ocfs2_rec_clusters(el, rec)); |
| 5648 | ret = -EROFS; |
| 5649 | goto out; |
| 5650 | } |
| 5651 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5652 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5653 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5654 | if (ret) { |
| 5655 | mlog_errno(ret); |
| 5656 | goto out; |
| 5657 | } |
| 5658 | } |
| 5659 | |
| 5660 | out: |
| 5661 | ocfs2_free_path(path); |
| 5662 | return ret; |
| 5663 | } |
| 5664 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5665 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5666 | struct ocfs2_extent_tree *et, |
| 5667 | u32 cpos, u32 phys_cpos, u32 len, |
| 5668 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5669 | { |
| 5670 | int ret; |
| 5671 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5672 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5673 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5674 | handle_t *handle; |
| 5675 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5676 | |
| 5677 | ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); |
| 5678 | if (ret) { |
| 5679 | mlog_errno(ret); |
| 5680 | return ret; |
| 5681 | } |
| 5682 | |
| 5683 | mutex_lock(&tl_inode->i_mutex); |
| 5684 | |
| 5685 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5686 | ret = __ocfs2_flush_truncate_log(osb); |
| 5687 | if (ret < 0) { |
| 5688 | mlog_errno(ret); |
| 5689 | goto out; |
| 5690 | } |
| 5691 | } |
| 5692 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5693 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5694 | if (IS_ERR(handle)) { |
| 5695 | ret = PTR_ERR(handle); |
| 5696 | mlog_errno(ret); |
| 5697 | goto out; |
| 5698 | } |
| 5699 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5700 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5701 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5702 | if (ret) { |
| 5703 | mlog_errno(ret); |
| 5704 | goto out; |
| 5705 | } |
| 5706 | |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5707 | vfs_dq_free_space_nodirty(inode, |
| 5708 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5709 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5710 | ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5711 | if (ret) { |
| 5712 | mlog_errno(ret); |
| 5713 | goto out_commit; |
| 5714 | } |
| 5715 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5716 | ocfs2_et_update_clusters(et, -len); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5717 | |
| 5718 | ret = ocfs2_journal_dirty(handle, et->et_root_bh); |
| 5719 | if (ret) { |
| 5720 | mlog_errno(ret); |
| 5721 | goto out_commit; |
| 5722 | } |
| 5723 | |
| 5724 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 5725 | if (ret) |
| 5726 | mlog_errno(ret); |
| 5727 | |
| 5728 | out_commit: |
| 5729 | ocfs2_commit_trans(osb, handle); |
| 5730 | out: |
| 5731 | mutex_unlock(&tl_inode->i_mutex); |
| 5732 | |
| 5733 | if (meta_ac) |
| 5734 | ocfs2_free_alloc_context(meta_ac); |
| 5735 | |
| 5736 | return ret; |
| 5737 | } |
| 5738 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5739 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5740 | { |
| 5741 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5742 | struct ocfs2_dinode *di; |
| 5743 | struct ocfs2_truncate_log *tl; |
| 5744 | |
| 5745 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5746 | tl = &di->id2.i_dealloc; |
| 5747 | |
| 5748 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5749 | "slot %d, invalid truncate log parameters: used = " |
| 5750 | "%u, count = %u\n", osb->slot_num, |
| 5751 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5752 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5753 | } |
| 5754 | |
| 5755 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5756 | unsigned int new_start) |
| 5757 | { |
| 5758 | unsigned int tail_index; |
| 5759 | unsigned int current_tail; |
| 5760 | |
| 5761 | /* No records, nothing to coalesce */ |
| 5762 | if (!le16_to_cpu(tl->tl_used)) |
| 5763 | return 0; |
| 5764 | |
| 5765 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5766 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5767 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5768 | |
| 5769 | return current_tail == new_start; |
| 5770 | } |
| 5771 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5772 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5773 | handle_t *handle, |
| 5774 | u64 start_blk, |
| 5775 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5776 | { |
| 5777 | int status, index; |
| 5778 | unsigned int start_cluster, tl_count; |
| 5779 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5780 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5781 | struct ocfs2_dinode *di; |
| 5782 | struct ocfs2_truncate_log *tl; |
| 5783 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5784 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 5785 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5786 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5787 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5788 | |
| 5789 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5790 | |
| 5791 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5792 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5793 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5794 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5795 | * corruption is a code bug */ |
| 5796 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5797 | |
| 5798 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5799 | tl_count = le16_to_cpu(tl->tl_count); |
| 5800 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5801 | tl_count == 0, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5802 | "Truncate record count on #%llu invalid " |
| 5803 | "wanted %u, actual %u\n", |
| 5804 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5805 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5806 | le16_to_cpu(tl->tl_count)); |
| 5807 | |
| 5808 | /* Caller should have known to flush before calling us. */ |
| 5809 | index = le16_to_cpu(tl->tl_used); |
| 5810 | if (index >= tl_count) { |
| 5811 | status = -ENOSPC; |
| 5812 | mlog_errno(status); |
| 5813 | goto bail; |
| 5814 | } |
| 5815 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5816 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5817 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5818 | if (status < 0) { |
| 5819 | mlog_errno(status); |
| 5820 | goto bail; |
| 5821 | } |
| 5822 | |
| 5823 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5824 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 5825 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5826 | |
| 5827 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5828 | /* |
| 5829 | * Move index back to the record we are coalescing with. |
| 5830 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5831 | */ |
| 5832 | index--; |
| 5833 | |
| 5834 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 5835 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 5836 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5837 | num_clusters); |
| 5838 | } else { |
| 5839 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5840 | tl->tl_used = cpu_to_le16(index + 1); |
| 5841 | } |
| 5842 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5843 | |
| 5844 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5845 | if (status < 0) { |
| 5846 | mlog_errno(status); |
| 5847 | goto bail; |
| 5848 | } |
| 5849 | |
| 5850 | bail: |
| 5851 | mlog_exit(status); |
| 5852 | return status; |
| 5853 | } |
| 5854 | |
| 5855 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5856 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5857 | struct inode *data_alloc_inode, |
| 5858 | struct buffer_head *data_alloc_bh) |
| 5859 | { |
| 5860 | int status = 0; |
| 5861 | int i; |
| 5862 | unsigned int num_clusters; |
| 5863 | u64 start_blk; |
| 5864 | struct ocfs2_truncate_rec rec; |
| 5865 | struct ocfs2_dinode *di; |
| 5866 | struct ocfs2_truncate_log *tl; |
| 5867 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5868 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5869 | |
| 5870 | mlog_entry_void(); |
| 5871 | |
| 5872 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5873 | tl = &di->id2.i_dealloc; |
| 5874 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5875 | while (i >= 0) { |
| 5876 | /* Caller has given us at least enough credits to |
| 5877 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5878 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5879 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5880 | if (status < 0) { |
| 5881 | mlog_errno(status); |
| 5882 | goto bail; |
| 5883 | } |
| 5884 | |
| 5885 | tl->tl_used = cpu_to_le16(i); |
| 5886 | |
| 5887 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 5888 | if (status < 0) { |
| 5889 | mlog_errno(status); |
| 5890 | goto bail; |
| 5891 | } |
| 5892 | |
| 5893 | /* TODO: Perhaps we can calculate the bulk of the |
| 5894 | * credits up front rather than extending like |
| 5895 | * this. */ |
| 5896 | status = ocfs2_extend_trans(handle, |
| 5897 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5898 | if (status < 0) { |
| 5899 | mlog_errno(status); |
| 5900 | goto bail; |
| 5901 | } |
| 5902 | |
| 5903 | rec = tl->tl_recs[i]; |
| 5904 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5905 | le32_to_cpu(rec.t_start)); |
| 5906 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5907 | |
| 5908 | /* if start_blk is not set, we ignore the record as |
| 5909 | * invalid. */ |
| 5910 | if (start_blk) { |
| 5911 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 5912 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 5913 | |
| 5914 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5915 | data_alloc_bh, start_blk, |
| 5916 | num_clusters); |
| 5917 | if (status < 0) { |
| 5918 | mlog_errno(status); |
| 5919 | goto bail; |
| 5920 | } |
| 5921 | } |
| 5922 | i--; |
| 5923 | } |
| 5924 | |
| 5925 | bail: |
| 5926 | mlog_exit(status); |
| 5927 | return status; |
| 5928 | } |
| 5929 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5930 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5931 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5932 | { |
| 5933 | int status; |
| 5934 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5935 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5936 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5937 | struct inode *data_alloc_inode = NULL; |
| 5938 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5939 | struct buffer_head *data_alloc_bh = NULL; |
| 5940 | struct ocfs2_dinode *di; |
| 5941 | struct ocfs2_truncate_log *tl; |
| 5942 | |
| 5943 | mlog_entry_void(); |
| 5944 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5945 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5946 | |
| 5947 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5948 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5949 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5950 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5951 | * corruption is a code bug */ |
| 5952 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5953 | |
| 5954 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5955 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5956 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 5957 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5958 | if (!num_to_flush) { |
| 5959 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5960 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 5964 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 5965 | OCFS2_INVALID_SLOT); |
| 5966 | if (!data_alloc_inode) { |
| 5967 | status = -EINVAL; |
| 5968 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5969 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5970 | } |
| 5971 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5972 | mutex_lock(&data_alloc_inode->i_mutex); |
| 5973 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5974 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5975 | if (status < 0) { |
| 5976 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5977 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5978 | } |
| 5979 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 5980 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5981 | if (IS_ERR(handle)) { |
| 5982 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5983 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5984 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 5988 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5989 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5990 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5991 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5992 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5993 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5994 | out_unlock: |
| 5995 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5996 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5997 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5998 | out_mutex: |
| 5999 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 6000 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6001 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6002 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6003 | mlog_exit(status); |
| 6004 | return status; |
| 6005 | } |
| 6006 | |
| 6007 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 6008 | { |
| 6009 | int status; |
| 6010 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6011 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6012 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6013 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6014 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6015 | |
| 6016 | return status; |
| 6017 | } |
| 6018 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6019 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6020 | { |
| 6021 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6022 | struct ocfs2_super *osb = |
| 6023 | container_of(work, struct ocfs2_super, |
| 6024 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6025 | |
| 6026 | mlog_entry_void(); |
| 6027 | |
| 6028 | status = ocfs2_flush_truncate_log(osb); |
| 6029 | if (status < 0) |
| 6030 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 6031 | else |
| 6032 | ocfs2_init_inode_steal_slot(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6033 | |
| 6034 | mlog_exit(status); |
| 6035 | } |
| 6036 | |
| 6037 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 6038 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 6039 | int cancel) |
| 6040 | { |
| 6041 | if (osb->osb_tl_inode) { |
| 6042 | /* We want to push off log flushes while truncates are |
| 6043 | * still running. */ |
| 6044 | if (cancel) |
| 6045 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6046 | |
| 6047 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 6048 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 6049 | } |
| 6050 | } |
| 6051 | |
| 6052 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 6053 | int slot_num, |
| 6054 | struct inode **tl_inode, |
| 6055 | struct buffer_head **tl_bh) |
| 6056 | { |
| 6057 | int status; |
| 6058 | struct inode *inode = NULL; |
| 6059 | struct buffer_head *bh = NULL; |
| 6060 | |
| 6061 | inode = ocfs2_get_system_file_inode(osb, |
| 6062 | TRUNCATE_LOG_SYSTEM_INODE, |
| 6063 | slot_num); |
| 6064 | if (!inode) { |
| 6065 | status = -EINVAL; |
| 6066 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 6067 | goto bail; |
| 6068 | } |
| 6069 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 6070 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6071 | if (status < 0) { |
| 6072 | iput(inode); |
| 6073 | mlog_errno(status); |
| 6074 | goto bail; |
| 6075 | } |
| 6076 | |
| 6077 | *tl_inode = inode; |
| 6078 | *tl_bh = bh; |
| 6079 | bail: |
| 6080 | mlog_exit(status); |
| 6081 | return status; |
| 6082 | } |
| 6083 | |
| 6084 | /* called during the 1st stage of node recovery. we stamp a clean |
| 6085 | * truncate log and pass back a copy for processing later. if the |
| 6086 | * truncate log does not require processing, a *tl_copy is set to |
| 6087 | * NULL. */ |
| 6088 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 6089 | int slot_num, |
| 6090 | struct ocfs2_dinode **tl_copy) |
| 6091 | { |
| 6092 | int status; |
| 6093 | struct inode *tl_inode = NULL; |
| 6094 | struct buffer_head *tl_bh = NULL; |
| 6095 | struct ocfs2_dinode *di; |
| 6096 | struct ocfs2_truncate_log *tl; |
| 6097 | |
| 6098 | *tl_copy = NULL; |
| 6099 | |
| 6100 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 6101 | |
| 6102 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 6103 | if (status < 0) { |
| 6104 | mlog_errno(status); |
| 6105 | goto bail; |
| 6106 | } |
| 6107 | |
| 6108 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6109 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 6110 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 6111 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 6112 | * so any corruption is a code bug */ |
| 6113 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 6114 | |
| 6115 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6116 | if (le16_to_cpu(tl->tl_used)) { |
| 6117 | mlog(0, "We'll have %u logs to recover\n", |
| 6118 | le16_to_cpu(tl->tl_used)); |
| 6119 | |
| 6120 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 6121 | if (!(*tl_copy)) { |
| 6122 | status = -ENOMEM; |
| 6123 | mlog_errno(status); |
| 6124 | goto bail; |
| 6125 | } |
| 6126 | |
| 6127 | /* Assuming the write-out below goes well, this copy |
| 6128 | * will be passed back to recovery for processing. */ |
| 6129 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 6130 | |
| 6131 | /* All we need to do to clear the truncate log is set |
| 6132 | * tl_used. */ |
| 6133 | tl->tl_used = 0; |
| 6134 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6135 | 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] | 6136 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6137 | if (status < 0) { |
| 6138 | mlog_errno(status); |
| 6139 | goto bail; |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | bail: |
| 6144 | if (tl_inode) |
| 6145 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6146 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6147 | |
| 6148 | if (status < 0 && (*tl_copy)) { |
| 6149 | kfree(*tl_copy); |
| 6150 | *tl_copy = NULL; |
| 6151 | } |
| 6152 | |
| 6153 | mlog_exit(status); |
| 6154 | return status; |
| 6155 | } |
| 6156 | |
| 6157 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6158 | struct ocfs2_dinode *tl_copy) |
| 6159 | { |
| 6160 | int status = 0; |
| 6161 | int i; |
| 6162 | unsigned int clusters, num_recs, start_cluster; |
| 6163 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6164 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6165 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6166 | struct ocfs2_truncate_log *tl; |
| 6167 | |
| 6168 | mlog_entry_void(); |
| 6169 | |
| 6170 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6171 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6172 | return -EINVAL; |
| 6173 | } |
| 6174 | |
| 6175 | tl = &tl_copy->id2.i_dealloc; |
| 6176 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 6177 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6178 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6179 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6180 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6181 | for(i = 0; i < num_recs; i++) { |
| 6182 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6183 | status = __ocfs2_flush_truncate_log(osb); |
| 6184 | if (status < 0) { |
| 6185 | mlog_errno(status); |
| 6186 | goto bail_up; |
| 6187 | } |
| 6188 | } |
| 6189 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6190 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6191 | if (IS_ERR(handle)) { |
| 6192 | status = PTR_ERR(handle); |
| 6193 | mlog_errno(status); |
| 6194 | goto bail_up; |
| 6195 | } |
| 6196 | |
| 6197 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6198 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6199 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6200 | |
| 6201 | status = ocfs2_truncate_log_append(osb, handle, |
| 6202 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6203 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6204 | if (status < 0) { |
| 6205 | mlog_errno(status); |
| 6206 | goto bail_up; |
| 6207 | } |
| 6208 | } |
| 6209 | |
| 6210 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6211 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6212 | |
| 6213 | mlog_exit(status); |
| 6214 | return status; |
| 6215 | } |
| 6216 | |
| 6217 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6218 | { |
| 6219 | int status; |
| 6220 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6221 | |
| 6222 | mlog_entry_void(); |
| 6223 | |
| 6224 | if (tl_inode) { |
| 6225 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6226 | flush_workqueue(ocfs2_wq); |
| 6227 | |
| 6228 | status = ocfs2_flush_truncate_log(osb); |
| 6229 | if (status < 0) |
| 6230 | mlog_errno(status); |
| 6231 | |
| 6232 | brelse(osb->osb_tl_bh); |
| 6233 | iput(osb->osb_tl_inode); |
| 6234 | } |
| 6235 | |
| 6236 | mlog_exit_void(); |
| 6237 | } |
| 6238 | |
| 6239 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6240 | { |
| 6241 | int status; |
| 6242 | struct inode *tl_inode = NULL; |
| 6243 | struct buffer_head *tl_bh = NULL; |
| 6244 | |
| 6245 | mlog_entry_void(); |
| 6246 | |
| 6247 | status = ocfs2_get_truncate_log_info(osb, |
| 6248 | osb->slot_num, |
| 6249 | &tl_inode, |
| 6250 | &tl_bh); |
| 6251 | if (status < 0) |
| 6252 | mlog_errno(status); |
| 6253 | |
| 6254 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6255 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6256 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6257 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6258 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6259 | osb->osb_tl_bh = tl_bh; |
| 6260 | osb->osb_tl_inode = tl_inode; |
| 6261 | |
| 6262 | mlog_exit(status); |
| 6263 | return status; |
| 6264 | } |
| 6265 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6266 | /* |
| 6267 | * Delayed de-allocation of suballocator blocks. |
| 6268 | * |
| 6269 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6270 | * |
| 6271 | * The locking for this can get extremely complicated, especially when |
| 6272 | * the suballocator inodes to delete from aren't known until deep |
| 6273 | * within an unrelated codepath. |
| 6274 | * |
| 6275 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6276 | * btree could have been grown by any number of nodes each allocating |
| 6277 | * out of their own suballoc inode. |
| 6278 | * |
| 6279 | * These structures allow the delay of block de-allocation until a |
| 6280 | * later time, when locking of multiple cluster inodes won't cause |
| 6281 | * deadlock. |
| 6282 | */ |
| 6283 | |
| 6284 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6285 | * Describe a single bit freed from a suballocator. For the block |
| 6286 | * suballocators, it represents one block. For the global cluster |
| 6287 | * allocator, it represents some clusters and free_bit indicates |
| 6288 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6289 | */ |
| 6290 | struct ocfs2_cached_block_free { |
| 6291 | struct ocfs2_cached_block_free *free_next; |
| 6292 | u64 free_blk; |
| 6293 | unsigned int free_bit; |
| 6294 | }; |
| 6295 | |
| 6296 | struct ocfs2_per_slot_free_list { |
| 6297 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6298 | int f_inode_type; |
| 6299 | int f_slot; |
| 6300 | struct ocfs2_cached_block_free *f_first; |
| 6301 | }; |
| 6302 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6303 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6304 | int sysfile_type, |
| 6305 | int slot, |
| 6306 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6307 | { |
| 6308 | int ret; |
| 6309 | u64 bg_blkno; |
| 6310 | handle_t *handle; |
| 6311 | struct inode *inode; |
| 6312 | struct buffer_head *di_bh = NULL; |
| 6313 | struct ocfs2_cached_block_free *tmp; |
| 6314 | |
| 6315 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6316 | if (!inode) { |
| 6317 | ret = -EINVAL; |
| 6318 | mlog_errno(ret); |
| 6319 | goto out; |
| 6320 | } |
| 6321 | |
| 6322 | mutex_lock(&inode->i_mutex); |
| 6323 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6324 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6325 | if (ret) { |
| 6326 | mlog_errno(ret); |
| 6327 | goto out_mutex; |
| 6328 | } |
| 6329 | |
| 6330 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6331 | if (IS_ERR(handle)) { |
| 6332 | ret = PTR_ERR(handle); |
| 6333 | mlog_errno(ret); |
| 6334 | goto out_unlock; |
| 6335 | } |
| 6336 | |
| 6337 | while (head) { |
| 6338 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6339 | head->free_bit); |
| 6340 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 6341 | head->free_bit, (unsigned long long)head->free_blk); |
| 6342 | |
| 6343 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6344 | head->free_bit, bg_blkno, 1); |
| 6345 | if (ret) { |
| 6346 | mlog_errno(ret); |
| 6347 | goto out_journal; |
| 6348 | } |
| 6349 | |
| 6350 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 6351 | if (ret) { |
| 6352 | mlog_errno(ret); |
| 6353 | goto out_journal; |
| 6354 | } |
| 6355 | |
| 6356 | tmp = head; |
| 6357 | head = head->free_next; |
| 6358 | kfree(tmp); |
| 6359 | } |
| 6360 | |
| 6361 | out_journal: |
| 6362 | ocfs2_commit_trans(osb, handle); |
| 6363 | |
| 6364 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6365 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6366 | brelse(di_bh); |
| 6367 | out_mutex: |
| 6368 | mutex_unlock(&inode->i_mutex); |
| 6369 | iput(inode); |
| 6370 | out: |
| 6371 | while(head) { |
| 6372 | /* Premature exit may have left some dangling items. */ |
| 6373 | tmp = head; |
| 6374 | head = head->free_next; |
| 6375 | kfree(tmp); |
| 6376 | } |
| 6377 | |
| 6378 | return ret; |
| 6379 | } |
| 6380 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6381 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6382 | u64 blkno, unsigned int bit) |
| 6383 | { |
| 6384 | int ret = 0; |
| 6385 | struct ocfs2_cached_block_free *item; |
| 6386 | |
| 6387 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6388 | if (item == NULL) { |
| 6389 | ret = -ENOMEM; |
| 6390 | mlog_errno(ret); |
| 6391 | return ret; |
| 6392 | } |
| 6393 | |
| 6394 | mlog(0, "Insert clusters: (bit %u, blk %llu)\n", |
| 6395 | bit, (unsigned long long)blkno); |
| 6396 | |
| 6397 | item->free_blk = blkno; |
| 6398 | item->free_bit = bit; |
| 6399 | item->free_next = ctxt->c_global_allocator; |
| 6400 | |
| 6401 | ctxt->c_global_allocator = item; |
| 6402 | return ret; |
| 6403 | } |
| 6404 | |
| 6405 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6406 | struct ocfs2_cached_block_free *head) |
| 6407 | { |
| 6408 | struct ocfs2_cached_block_free *tmp; |
| 6409 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6410 | handle_t *handle; |
| 6411 | int ret = 0; |
| 6412 | |
| 6413 | mutex_lock(&tl_inode->i_mutex); |
| 6414 | |
| 6415 | while (head) { |
| 6416 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6417 | ret = __ocfs2_flush_truncate_log(osb); |
| 6418 | if (ret < 0) { |
| 6419 | mlog_errno(ret); |
| 6420 | break; |
| 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6425 | if (IS_ERR(handle)) { |
| 6426 | ret = PTR_ERR(handle); |
| 6427 | mlog_errno(ret); |
| 6428 | break; |
| 6429 | } |
| 6430 | |
| 6431 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6432 | head->free_bit); |
| 6433 | |
| 6434 | ocfs2_commit_trans(osb, handle); |
| 6435 | tmp = head; |
| 6436 | head = head->free_next; |
| 6437 | kfree(tmp); |
| 6438 | |
| 6439 | if (ret < 0) { |
| 6440 | mlog_errno(ret); |
| 6441 | break; |
| 6442 | } |
| 6443 | } |
| 6444 | |
| 6445 | mutex_unlock(&tl_inode->i_mutex); |
| 6446 | |
| 6447 | while (head) { |
| 6448 | /* Premature exit may have left some dangling items. */ |
| 6449 | tmp = head; |
| 6450 | head = head->free_next; |
| 6451 | kfree(tmp); |
| 6452 | } |
| 6453 | |
| 6454 | return ret; |
| 6455 | } |
| 6456 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6457 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6458 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6459 | { |
| 6460 | int ret = 0, ret2; |
| 6461 | struct ocfs2_per_slot_free_list *fl; |
| 6462 | |
| 6463 | if (!ctxt) |
| 6464 | return 0; |
| 6465 | |
| 6466 | while (ctxt->c_first_suballocator) { |
| 6467 | fl = ctxt->c_first_suballocator; |
| 6468 | |
| 6469 | if (fl->f_first) { |
| 6470 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 6471 | fl->f_inode_type, fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6472 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6473 | fl->f_inode_type, |
| 6474 | fl->f_slot, |
| 6475 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6476 | if (ret2) |
| 6477 | mlog_errno(ret2); |
| 6478 | if (!ret) |
| 6479 | ret = ret2; |
| 6480 | } |
| 6481 | |
| 6482 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6483 | kfree(fl); |
| 6484 | } |
| 6485 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6486 | if (ctxt->c_global_allocator) { |
| 6487 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6488 | ctxt->c_global_allocator); |
| 6489 | if (ret2) |
| 6490 | mlog_errno(ret2); |
| 6491 | if (!ret) |
| 6492 | ret = ret2; |
| 6493 | |
| 6494 | ctxt->c_global_allocator = NULL; |
| 6495 | } |
| 6496 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6497 | return ret; |
| 6498 | } |
| 6499 | |
| 6500 | static struct ocfs2_per_slot_free_list * |
| 6501 | ocfs2_find_per_slot_free_list(int type, |
| 6502 | int slot, |
| 6503 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6504 | { |
| 6505 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6506 | |
| 6507 | while (fl) { |
| 6508 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6509 | return fl; |
| 6510 | |
| 6511 | fl = fl->f_next_suballocator; |
| 6512 | } |
| 6513 | |
| 6514 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6515 | if (fl) { |
| 6516 | fl->f_inode_type = type; |
| 6517 | fl->f_slot = slot; |
| 6518 | fl->f_first = NULL; |
| 6519 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6520 | |
| 6521 | ctxt->c_first_suballocator = fl; |
| 6522 | } |
| 6523 | return fl; |
| 6524 | } |
| 6525 | |
Tao Ma | 1823cb0 | 2009-08-18 11:24:49 +0800 | [diff] [blame] | 6526 | int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6527 | int type, int slot, u64 blkno, |
| 6528 | unsigned int bit) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6529 | { |
| 6530 | int ret; |
| 6531 | struct ocfs2_per_slot_free_list *fl; |
| 6532 | struct ocfs2_cached_block_free *item; |
| 6533 | |
| 6534 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6535 | if (fl == NULL) { |
| 6536 | ret = -ENOMEM; |
| 6537 | mlog_errno(ret); |
| 6538 | goto out; |
| 6539 | } |
| 6540 | |
| 6541 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6542 | if (item == NULL) { |
| 6543 | ret = -ENOMEM; |
| 6544 | mlog_errno(ret); |
| 6545 | goto out; |
| 6546 | } |
| 6547 | |
| 6548 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 6549 | type, slot, bit, (unsigned long long)blkno); |
| 6550 | |
| 6551 | item->free_blk = blkno; |
| 6552 | item->free_bit = bit; |
| 6553 | item->free_next = fl->f_first; |
| 6554 | |
| 6555 | fl->f_first = item; |
| 6556 | |
| 6557 | ret = 0; |
| 6558 | out: |
| 6559 | return ret; |
| 6560 | } |
| 6561 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6562 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6563 | struct ocfs2_extent_block *eb) |
| 6564 | { |
| 6565 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6566 | le16_to_cpu(eb->h_suballoc_slot), |
| 6567 | le64_to_cpu(eb->h_blkno), |
| 6568 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6569 | } |
| 6570 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6571 | /* This function will figure out whether the currently last extent |
| 6572 | * block will be deleted, and if it will, what the new last extent |
| 6573 | * block will be so we can update his h_next_leaf_blk field, as well |
| 6574 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6575 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6576 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6577 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6578 | struct buffer_head **new_last_eb) |
| 6579 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6580 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6581 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6582 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6583 | struct ocfs2_extent_block *eb; |
| 6584 | struct ocfs2_extent_list *el; |
| 6585 | struct buffer_head *bh = NULL; |
| 6586 | |
| 6587 | *new_last_eb = NULL; |
| 6588 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6589 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6590 | if (!path->p_tree_depth) |
| 6591 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6592 | |
| 6593 | /* trunc to zero special case - this makes tree_depth = 0 |
| 6594 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6595 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6596 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6597 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6598 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6599 | BUG_ON(!el->l_next_free_rec); |
| 6600 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6601 | /* |
| 6602 | * Make sure that this extent list will actually be empty |
| 6603 | * after we clear away the data. We can shortcut out if |
| 6604 | * there's more than one non-empty extent in the |
| 6605 | * list. Otherwise, a check of the remaining extent is |
| 6606 | * necessary. |
| 6607 | */ |
| 6608 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 6609 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6610 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6611 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6612 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6613 | |
| 6614 | /* We may have a valid extent in index 1, check it. */ |
| 6615 | if (next_free == 2) |
| 6616 | rec = &el->l_recs[1]; |
| 6617 | |
| 6618 | /* |
| 6619 | * Fall through - no more nonempty extents, so we want |
| 6620 | * to delete this leaf. |
| 6621 | */ |
| 6622 | } else { |
| 6623 | if (next_free > 1) |
| 6624 | goto out; |
| 6625 | |
| 6626 | rec = &el->l_recs[0]; |
| 6627 | } |
| 6628 | |
| 6629 | if (rec) { |
| 6630 | /* |
| 6631 | * Check it we'll only be trimming off the end of this |
| 6632 | * cluster. |
| 6633 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6634 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6635 | goto out; |
| 6636 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6637 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6638 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 6639 | if (ret) { |
| 6640 | mlog_errno(ret); |
| 6641 | goto out; |
| 6642 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6643 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 6644 | 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] | 6645 | if (ret) { |
| 6646 | mlog_errno(ret); |
| 6647 | goto out; |
| 6648 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6649 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6650 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 6651 | el = &eb->h_list; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 6652 | |
| 6653 | /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). |
| 6654 | * Any corruption is a code bug. */ |
| 6655 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6656 | |
| 6657 | *new_last_eb = bh; |
| 6658 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6659 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 6660 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 6661 | out: |
| 6662 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6663 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6664 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6665 | } |
| 6666 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6667 | /* |
| 6668 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 6669 | * during truncate. |
| 6670 | * |
| 6671 | * The caller needs to: |
| 6672 | * - start journaling of each path component. |
| 6673 | * - compute and fully set up any new last ext block |
| 6674 | */ |
| 6675 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 6676 | handle_t *handle, struct ocfs2_truncate_context *tc, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6677 | u32 clusters_to_del, u64 *delete_start, u8 *flags) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6678 | { |
| 6679 | int ret, i, index = path->p_tree_depth; |
| 6680 | u32 new_edge = 0; |
| 6681 | u64 deleted_eb = 0; |
| 6682 | struct buffer_head *bh; |
| 6683 | struct ocfs2_extent_list *el; |
| 6684 | struct ocfs2_extent_rec *rec; |
| 6685 | |
| 6686 | *delete_start = 0; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6687 | *flags = 0; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6688 | |
| 6689 | while (index >= 0) { |
| 6690 | bh = path->p_node[index].bh; |
| 6691 | el = path->p_node[index].el; |
| 6692 | |
| 6693 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 6694 | index, (unsigned long long)bh->b_blocknr); |
| 6695 | |
| 6696 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 6697 | |
| 6698 | if (index != |
| 6699 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 6700 | ocfs2_error(inode->i_sb, |
| 6701 | "Inode %lu has invalid ext. block %llu", |
| 6702 | inode->i_ino, |
| 6703 | (unsigned long long)bh->b_blocknr); |
| 6704 | ret = -EROFS; |
| 6705 | goto out; |
| 6706 | } |
| 6707 | |
| 6708 | find_tail_record: |
| 6709 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 6710 | rec = &el->l_recs[i]; |
| 6711 | |
| 6712 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 6713 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6714 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6715 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6716 | le16_to_cpu(el->l_next_free_rec)); |
| 6717 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6718 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6719 | |
| 6720 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 6721 | /* |
| 6722 | * If the leaf block contains a single empty |
| 6723 | * extent and no records, we can just remove |
| 6724 | * the block. |
| 6725 | */ |
| 6726 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 6727 | memset(rec, 0, |
| 6728 | sizeof(struct ocfs2_extent_rec)); |
| 6729 | el->l_next_free_rec = cpu_to_le16(0); |
| 6730 | |
| 6731 | goto delete; |
| 6732 | } |
| 6733 | |
| 6734 | /* |
| 6735 | * Remove any empty extents by shifting things |
| 6736 | * left. That should make life much easier on |
| 6737 | * the code below. This condition is rare |
| 6738 | * enough that we shouldn't see a performance |
| 6739 | * hit. |
| 6740 | */ |
| 6741 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 6742 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6743 | |
| 6744 | for(i = 0; |
| 6745 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 6746 | el->l_recs[i] = el->l_recs[i + 1]; |
| 6747 | |
| 6748 | memset(&el->l_recs[i], 0, |
| 6749 | sizeof(struct ocfs2_extent_rec)); |
| 6750 | |
| 6751 | /* |
| 6752 | * We've modified our extent list. The |
| 6753 | * simplest way to handle this change |
| 6754 | * is to being the search from the |
| 6755 | * start again. |
| 6756 | */ |
| 6757 | goto find_tail_record; |
| 6758 | } |
| 6759 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6760 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6761 | |
| 6762 | /* |
| 6763 | * We'll use "new_edge" on our way back up the |
| 6764 | * tree to know what our rightmost cpos is. |
| 6765 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6766 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6767 | new_edge += le32_to_cpu(rec->e_cpos); |
| 6768 | |
| 6769 | /* |
| 6770 | * The caller will use this to delete data blocks. |
| 6771 | */ |
| 6772 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 6773 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6774 | le16_to_cpu(rec->e_leaf_clusters)); |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6775 | *flags = rec->e_flags; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6776 | |
| 6777 | /* |
| 6778 | * If it's now empty, remove this record. |
| 6779 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6780 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6781 | memset(rec, 0, |
| 6782 | sizeof(struct ocfs2_extent_rec)); |
| 6783 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6784 | } |
| 6785 | } else { |
| 6786 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 6787 | memset(rec, 0, |
| 6788 | sizeof(struct ocfs2_extent_rec)); |
| 6789 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6790 | |
| 6791 | goto delete; |
| 6792 | } |
| 6793 | |
| 6794 | /* Can this actually happen? */ |
| 6795 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 6796 | goto delete; |
| 6797 | |
| 6798 | /* |
| 6799 | * We never actually deleted any clusters |
| 6800 | * because our leaf was empty. There's no |
| 6801 | * reason to adjust the rightmost edge then. |
| 6802 | */ |
| 6803 | if (new_edge == 0) |
| 6804 | goto delete; |
| 6805 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6806 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 6807 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6808 | -le32_to_cpu(rec->e_cpos)); |
| 6809 | |
| 6810 | /* |
| 6811 | * A deleted child record should have been |
| 6812 | * caught above. |
| 6813 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6814 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | delete: |
| 6818 | ret = ocfs2_journal_dirty(handle, bh); |
| 6819 | if (ret) { |
| 6820 | mlog_errno(ret); |
| 6821 | goto out; |
| 6822 | } |
| 6823 | |
| 6824 | mlog(0, "extent list container %llu, after: record %d: " |
| 6825 | "(%u, %u, %llu), next = %u.\n", |
| 6826 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6827 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6828 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6829 | le16_to_cpu(el->l_next_free_rec)); |
| 6830 | |
| 6831 | /* |
| 6832 | * We must be careful to only attempt delete of an |
| 6833 | * extent block (and not the root inode block). |
| 6834 | */ |
| 6835 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 6836 | struct ocfs2_extent_block *eb = |
| 6837 | (struct ocfs2_extent_block *)bh->b_data; |
| 6838 | |
| 6839 | /* |
| 6840 | * Save this for use when processing the |
| 6841 | * parent block. |
| 6842 | */ |
| 6843 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 6844 | |
| 6845 | mlog(0, "deleting this extent block.\n"); |
| 6846 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6847 | ocfs2_remove_from_cache(INODE_CACHE(inode), bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6848 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6849 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6850 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 6851 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 6852 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6853 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 6854 | /* An error here is not fatal. */ |
| 6855 | if (ret < 0) |
| 6856 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6857 | } else { |
| 6858 | deleted_eb = 0; |
| 6859 | } |
| 6860 | |
| 6861 | index--; |
| 6862 | } |
| 6863 | |
| 6864 | ret = 0; |
| 6865 | out: |
| 6866 | return ret; |
| 6867 | } |
| 6868 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6869 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 6870 | unsigned int clusters_to_del, |
| 6871 | struct inode *inode, |
| 6872 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6873 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6874 | struct ocfs2_truncate_context *tc, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6875 | struct ocfs2_path *path, |
| 6876 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6877 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6878 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6879 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6880 | struct ocfs2_extent_block *last_eb = NULL; |
| 6881 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6882 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6883 | u64 delete_blk = 0; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6884 | u8 rec_flags; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6885 | |
| 6886 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6887 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6888 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6889 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6890 | if (status < 0) { |
| 6891 | mlog_errno(status); |
| 6892 | goto bail; |
| 6893 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6894 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6895 | /* |
| 6896 | * Each component will be touched, so we might as well journal |
| 6897 | * here to avoid having to handle errors later. |
| 6898 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6899 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6900 | if (status < 0) { |
| 6901 | mlog_errno(status); |
| 6902 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6903 | } |
| 6904 | |
| 6905 | if (last_eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6906 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6907 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6908 | if (status < 0) { |
| 6909 | mlog_errno(status); |
| 6910 | goto bail; |
| 6911 | } |
| 6912 | |
| 6913 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6914 | } |
| 6915 | |
| 6916 | el = &(fe->id2.i_list); |
| 6917 | |
| 6918 | /* |
| 6919 | * Lower levels depend on this never happening, but it's best |
| 6920 | * to check it up here before changing the tree. |
| 6921 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6922 | 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] | 6923 | ocfs2_error(inode->i_sb, |
| 6924 | "Inode %lu has an empty extent record, depth %u\n", |
| 6925 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6926 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6927 | goto bail; |
| 6928 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6929 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6930 | vfs_dq_free_space_nodirty(inode, |
| 6931 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6932 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 6933 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 6934 | clusters_to_del; |
| 6935 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 6936 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 6937 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6938 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6939 | status = ocfs2_trim_tree(inode, path, handle, tc, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6940 | clusters_to_del, &delete_blk, &rec_flags); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6941 | if (status) { |
| 6942 | mlog_errno(status); |
| 6943 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6944 | } |
| 6945 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6946 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6947 | /* trunc to zero is a special case. */ |
| 6948 | el->l_tree_depth = 0; |
| 6949 | fe->i_last_eb_blk = 0; |
| 6950 | } else if (last_eb) |
| 6951 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 6952 | |
| 6953 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 6954 | if (status < 0) { |
| 6955 | mlog_errno(status); |
| 6956 | goto bail; |
| 6957 | } |
| 6958 | |
| 6959 | if (last_eb) { |
| 6960 | /* If there will be a new last extent block, then by |
| 6961 | * definition, there cannot be any leaves to the right of |
| 6962 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6963 | last_eb->h_next_leaf_blk = 0; |
| 6964 | status = ocfs2_journal_dirty(handle, last_eb_bh); |
| 6965 | if (status < 0) { |
| 6966 | mlog_errno(status); |
| 6967 | goto bail; |
| 6968 | } |
| 6969 | } |
| 6970 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6971 | if (delete_blk) { |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6972 | if (rec_flags & OCFS2_EXT_REFCOUNTED) |
| 6973 | status = ocfs2_decrease_refcount(inode, handle, |
| 6974 | ocfs2_blocks_to_clusters(osb->sb, |
| 6975 | delete_blk), |
| 6976 | clusters_to_del, meta_ac, |
Tao Ma | 6ae23c5 | 2009-08-18 11:30:55 +0800 | [diff] [blame^] | 6977 | &tc->tc_dealloc, 1); |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6978 | else |
| 6979 | status = ocfs2_truncate_log_append(osb, handle, |
| 6980 | delete_blk, |
| 6981 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6982 | if (status < 0) { |
| 6983 | mlog_errno(status); |
| 6984 | goto bail; |
| 6985 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6986 | } |
| 6987 | status = 0; |
| 6988 | bail: |
Tao Ma | 60e2ec4 | 2009-08-12 14:42:47 +0800 | [diff] [blame] | 6989 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6990 | mlog_exit(status); |
| 6991 | return status; |
| 6992 | } |
| 6993 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6994 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6995 | { |
| 6996 | set_buffer_uptodate(bh); |
| 6997 | mark_buffer_dirty(bh); |
| 6998 | return 0; |
| 6999 | } |
| 7000 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 7001 | void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 7002 | unsigned int from, unsigned int to, |
| 7003 | struct page *page, int zero, u64 *phys) |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7004 | { |
| 7005 | int ret, partial = 0; |
| 7006 | |
| 7007 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 7008 | if (ret) |
| 7009 | mlog_errno(ret); |
| 7010 | |
| 7011 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 7012 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7013 | |
| 7014 | /* |
| 7015 | * Need to set the buffers we zero'd into uptodate |
| 7016 | * here if they aren't - ocfs2_map_page_blocks() |
| 7017 | * might've skipped some |
| 7018 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 7019 | ret = walk_page_buffers(handle, page_buffers(page), |
| 7020 | from, to, &partial, |
| 7021 | ocfs2_zero_func); |
| 7022 | if (ret < 0) |
| 7023 | mlog_errno(ret); |
| 7024 | else if (ocfs2_should_order_data(inode)) { |
| 7025 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7026 | if (ret < 0) |
| 7027 | mlog_errno(ret); |
| 7028 | } |
| 7029 | |
| 7030 | if (!partial) |
| 7031 | SetPageUptodate(page); |
| 7032 | |
| 7033 | flush_dcache_page(page); |
| 7034 | } |
| 7035 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7036 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 7037 | loff_t end, struct page **pages, |
| 7038 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7039 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7040 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7041 | struct page *page; |
| 7042 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 7043 | struct super_block *sb = inode->i_sb; |
| 7044 | |
| 7045 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 7046 | |
| 7047 | if (numpages == 0) |
| 7048 | goto out; |
| 7049 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7050 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7051 | for(i = 0; i < numpages; i++) { |
| 7052 | page = pages[i]; |
| 7053 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7054 | from = start & (PAGE_CACHE_SIZE - 1); |
| 7055 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 7056 | to = end & (PAGE_CACHE_SIZE - 1); |
| 7057 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7058 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 7059 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 7060 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7061 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 7062 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7063 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7064 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7065 | } |
| 7066 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7067 | if (pages) |
| 7068 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7069 | } |
| 7070 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 7071 | int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end, |
| 7072 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7073 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7074 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7075 | struct address_space *mapping = inode->i_mapping; |
| 7076 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7077 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7078 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7079 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7080 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7081 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7082 | last_page_bytes = PAGE_ALIGN(end); |
| 7083 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7084 | do { |
| 7085 | pages[numpages] = grab_cache_page(mapping, index); |
| 7086 | if (!pages[numpages]) { |
| 7087 | ret = -ENOMEM; |
| 7088 | mlog_errno(ret); |
| 7089 | goto out; |
| 7090 | } |
| 7091 | |
| 7092 | numpages++; |
| 7093 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7094 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7095 | |
| 7096 | out: |
| 7097 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7098 | if (pages) |
| 7099 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7100 | numpages = 0; |
| 7101 | } |
| 7102 | |
| 7103 | *num = numpages; |
| 7104 | |
| 7105 | return ret; |
| 7106 | } |
| 7107 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 7108 | static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, |
| 7109 | struct page **pages, int *num) |
| 7110 | { |
| 7111 | struct super_block *sb = inode->i_sb; |
| 7112 | |
| 7113 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 7114 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 7115 | |
| 7116 | return ocfs2_grab_pages(inode, start, end, pages, num); |
| 7117 | } |
| 7118 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7119 | /* |
| 7120 | * Zero the area past i_size but still within an allocated |
| 7121 | * cluster. This avoids exposing nonzero data on subsequent file |
| 7122 | * extends. |
| 7123 | * |
| 7124 | * We need to call this before i_size is updated on the inode because |
| 7125 | * otherwise block_write_full_page() will skip writeout of pages past |
| 7126 | * i_size. The new_i_size parameter is passed for this reason. |
| 7127 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7128 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 7129 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7130 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7131 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7132 | struct page **pages = NULL; |
| 7133 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7134 | unsigned int ext_flags; |
| 7135 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7136 | |
| 7137 | /* |
| 7138 | * File systems which don't support sparse files zero on every |
| 7139 | * extend. |
| 7140 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7141 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7142 | return 0; |
| 7143 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7144 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7145 | sizeof(struct page *), GFP_NOFS); |
| 7146 | if (pages == NULL) { |
| 7147 | ret = -ENOMEM; |
| 7148 | mlog_errno(ret); |
| 7149 | goto out; |
| 7150 | } |
| 7151 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7152 | if (range_start == range_end) |
| 7153 | goto out; |
| 7154 | |
| 7155 | ret = ocfs2_extent_map_get_blocks(inode, |
| 7156 | range_start >> sb->s_blocksize_bits, |
| 7157 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7158 | if (ret) { |
| 7159 | mlog_errno(ret); |
| 7160 | goto out; |
| 7161 | } |
| 7162 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7163 | /* |
| 7164 | * Tail is a hole, or is marked unwritten. In either case, we |
| 7165 | * can count on read and write to return/push zero's. |
| 7166 | */ |
| 7167 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7168 | goto out; |
| 7169 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7170 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 7171 | &numpages); |
| 7172 | if (ret) { |
| 7173 | mlog_errno(ret); |
| 7174 | goto out; |
| 7175 | } |
| 7176 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7177 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 7178 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7179 | |
| 7180 | /* |
| 7181 | * Initiate writeout of the pages we zero'd here. We don't |
| 7182 | * wait on them - the truncate_inode_pages() call later will |
| 7183 | * do that for us. |
| 7184 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7185 | ret = do_sync_mapping_range(inode->i_mapping, range_start, |
| 7186 | range_end - 1, SYNC_FILE_RANGE_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7187 | if (ret) |
| 7188 | mlog_errno(ret); |
| 7189 | |
| 7190 | out: |
| 7191 | if (pages) |
| 7192 | kfree(pages); |
| 7193 | |
| 7194 | return ret; |
| 7195 | } |
| 7196 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7197 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 7198 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7199 | { |
| 7200 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7201 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7202 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7203 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 7204 | memset(&di->id2, 0, blocksize - |
| 7205 | offsetof(struct ocfs2_dinode, id2) - |
| 7206 | xattrsize); |
| 7207 | else |
| 7208 | memset(&di->id2, 0, blocksize - |
| 7209 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7210 | } |
| 7211 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7212 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 7213 | struct ocfs2_dinode *di) |
| 7214 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7215 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7216 | di->id2.i_list.l_tree_depth = 0; |
| 7217 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7218 | di->id2.i_list.l_count = cpu_to_le16( |
| 7219 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7220 | } |
| 7221 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7222 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 7223 | { |
| 7224 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7225 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7226 | |
| 7227 | spin_lock(&oi->ip_lock); |
| 7228 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 7229 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7230 | spin_unlock(&oi->ip_lock); |
| 7231 | |
| 7232 | /* |
| 7233 | * We clear the entire i_data structure here so that all |
| 7234 | * fields can be properly initialized. |
| 7235 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7236 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7237 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7238 | idata->id_count = cpu_to_le16( |
| 7239 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7240 | } |
| 7241 | |
| 7242 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 7243 | struct buffer_head *di_bh) |
| 7244 | { |
| 7245 | int ret, i, has_data, num_pages = 0; |
| 7246 | handle_t *handle; |
| 7247 | u64 uninitialized_var(block); |
| 7248 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7249 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7250 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7251 | struct ocfs2_alloc_context *data_ac = NULL; |
| 7252 | struct page **pages = NULL; |
| 7253 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7254 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7255 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7256 | |
| 7257 | has_data = i_size_read(inode) ? 1 : 0; |
| 7258 | |
| 7259 | if (has_data) { |
| 7260 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 7261 | sizeof(struct page *), GFP_NOFS); |
| 7262 | if (pages == NULL) { |
| 7263 | ret = -ENOMEM; |
| 7264 | mlog_errno(ret); |
| 7265 | goto out; |
| 7266 | } |
| 7267 | |
| 7268 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 7269 | if (ret) { |
| 7270 | mlog_errno(ret); |
| 7271 | goto out; |
| 7272 | } |
| 7273 | } |
| 7274 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7275 | handle = ocfs2_start_trans(osb, |
| 7276 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7277 | if (IS_ERR(handle)) { |
| 7278 | ret = PTR_ERR(handle); |
| 7279 | mlog_errno(ret); |
| 7280 | goto out_unlock; |
| 7281 | } |
| 7282 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7283 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7284 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7285 | if (ret) { |
| 7286 | mlog_errno(ret); |
| 7287 | goto out_commit; |
| 7288 | } |
| 7289 | |
| 7290 | if (has_data) { |
| 7291 | u32 bit_off, num; |
| 7292 | unsigned int page_end; |
| 7293 | u64 phys; |
| 7294 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7295 | if (vfs_dq_alloc_space_nodirty(inode, |
| 7296 | ocfs2_clusters_to_bytes(osb->sb, 1))) { |
| 7297 | ret = -EDQUOT; |
| 7298 | goto out_commit; |
| 7299 | } |
| 7300 | did_quota = 1; |
| 7301 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7302 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 7303 | &num); |
| 7304 | if (ret) { |
| 7305 | mlog_errno(ret); |
| 7306 | goto out_commit; |
| 7307 | } |
| 7308 | |
| 7309 | /* |
| 7310 | * Save two copies, one for insert, and one that can |
| 7311 | * be changed by ocfs2_map_and_dirty_page() below. |
| 7312 | */ |
| 7313 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 7314 | |
| 7315 | /* |
| 7316 | * Non sparse file systems zero on extend, so no need |
| 7317 | * to do that now. |
| 7318 | */ |
| 7319 | if (!ocfs2_sparse_alloc(osb) && |
| 7320 | PAGE_CACHE_SIZE < osb->s_clustersize) |
| 7321 | end = PAGE_CACHE_SIZE; |
| 7322 | |
| 7323 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 7324 | if (ret) { |
| 7325 | mlog_errno(ret); |
| 7326 | goto out_commit; |
| 7327 | } |
| 7328 | |
| 7329 | /* |
| 7330 | * This should populate the 1st page for us and mark |
| 7331 | * it up to date. |
| 7332 | */ |
| 7333 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 7334 | if (ret) { |
| 7335 | mlog_errno(ret); |
| 7336 | goto out_commit; |
| 7337 | } |
| 7338 | |
| 7339 | page_end = PAGE_CACHE_SIZE; |
| 7340 | if (PAGE_CACHE_SIZE > osb->s_clustersize) |
| 7341 | page_end = osb->s_clustersize; |
| 7342 | |
| 7343 | for (i = 0; i < num_pages; i++) |
| 7344 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 7345 | pages[i], i > 0, &phys); |
| 7346 | } |
| 7347 | |
| 7348 | spin_lock(&oi->ip_lock); |
| 7349 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 7350 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7351 | spin_unlock(&oi->ip_lock); |
| 7352 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7353 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7354 | |
| 7355 | ocfs2_journal_dirty(handle, di_bh); |
| 7356 | |
| 7357 | if (has_data) { |
| 7358 | /* |
| 7359 | * An error at this point should be extremely rare. If |
| 7360 | * this proves to be false, we could always re-build |
| 7361 | * the in-inode data from our pages. |
| 7362 | */ |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 7363 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 7364 | ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7365 | if (ret) { |
| 7366 | mlog_errno(ret); |
| 7367 | goto out_commit; |
| 7368 | } |
| 7369 | |
| 7370 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7371 | } |
| 7372 | |
| 7373 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7374 | if (ret < 0 && did_quota) |
| 7375 | vfs_dq_free_space_nodirty(inode, |
| 7376 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7377 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7378 | ocfs2_commit_trans(osb, handle); |
| 7379 | |
| 7380 | out_unlock: |
| 7381 | if (data_ac) |
| 7382 | ocfs2_free_alloc_context(data_ac); |
| 7383 | |
| 7384 | out: |
| 7385 | if (pages) { |
| 7386 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7387 | kfree(pages); |
| 7388 | } |
| 7389 | |
| 7390 | return ret; |
| 7391 | } |
| 7392 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7393 | /* |
| 7394 | * It is expected, that by the time you call this function, |
| 7395 | * inode->i_size and fe->i_size have been adjusted. |
| 7396 | * |
| 7397 | * WARNING: This will kfree the truncate context |
| 7398 | */ |
| 7399 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7400 | struct inode *inode, |
| 7401 | struct buffer_head *fe_bh, |
| 7402 | struct ocfs2_truncate_context *tc) |
| 7403 | { |
| 7404 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7405 | u32 clusters_to_del, new_highest_cpos, range; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7406 | u64 blkno = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7407 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 7408 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7409 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7410 | struct ocfs2_path *path = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 7411 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7412 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 7413 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7414 | |
| 7415 | mlog_entry_void(); |
| 7416 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7417 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7418 | i_size_read(inode)); |
| 7419 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7420 | path = ocfs2_new_path(fe_bh, &di->id2.i_list, |
| 7421 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7422 | if (!path) { |
| 7423 | status = -ENOMEM; |
| 7424 | mlog_errno(status); |
| 7425 | goto bail; |
| 7426 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7427 | |
| 7428 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7429 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7430 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7431 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7432 | * Check that we still have allocation to delete. |
| 7433 | */ |
| 7434 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7435 | status = 0; |
| 7436 | goto bail; |
| 7437 | } |
| 7438 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7439 | credits = 0; |
| 7440 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7441 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7442 | * Truncate always works against the rightmost tree branch. |
| 7443 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7444 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7445 | if (status) { |
| 7446 | mlog_errno(status); |
| 7447 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7448 | } |
| 7449 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7450 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 7451 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 7452 | |
| 7453 | /* |
| 7454 | * By now, el will point to the extent list on the bottom most |
| 7455 | * portion of this tree. Only the tail record is considered in |
| 7456 | * each pass. |
| 7457 | * |
| 7458 | * We handle the following cases, in order: |
| 7459 | * - empty extent: delete the remaining branch |
| 7460 | * - remove the entire record |
| 7461 | * - remove a partial record |
| 7462 | * - no record needs to be removed (truncate has completed) |
| 7463 | */ |
| 7464 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7465 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7466 | ocfs2_error(inode->i_sb, |
| 7467 | "Inode %llu has empty extent block at %llu\n", |
| 7468 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7469 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7470 | status = -EROFS; |
| 7471 | goto bail; |
| 7472 | } |
| 7473 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7474 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7475 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7476 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7477 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 7478 | clusters_to_del = 0; |
| 7479 | } 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] | 7480 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7481 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7482 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7483 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7484 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7485 | new_highest_cpos; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7486 | blkno = le64_to_cpu(el->l_recs[i].e_blkno) + |
| 7487 | ocfs2_clusters_to_blocks(inode->i_sb, |
| 7488 | ocfs2_rec_clusters(el, &el->l_recs[i]) - |
| 7489 | clusters_to_del); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 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 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7498 | if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) { |
| 7499 | BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & |
| 7500 | OCFS2_HAS_REFCOUNT_FL)); |
| 7501 | |
| 7502 | status = ocfs2_lock_refcount_tree(osb, |
| 7503 | le64_to_cpu(di->i_refcount_loc), |
| 7504 | 1, &ref_tree, NULL); |
| 7505 | if (status) { |
| 7506 | mlog_errno(status); |
| 7507 | goto bail; |
| 7508 | } |
| 7509 | |
| 7510 | status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh, |
| 7511 | blkno, |
| 7512 | clusters_to_del, |
| 7513 | &credits, |
| 7514 | &meta_ac); |
| 7515 | if (status < 0) { |
| 7516 | mlog_errno(status); |
| 7517 | goto bail; |
| 7518 | } |
| 7519 | } |
| 7520 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7521 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7522 | tl_sem = 1; |
| 7523 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 7524 | * record is free for use. If there isn't any, we flush to get |
| 7525 | * an empty truncate log. */ |
| 7526 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 7527 | status = __ocfs2_flush_truncate_log(osb); |
| 7528 | if (status < 0) { |
| 7529 | mlog_errno(status); |
| 7530 | goto bail; |
| 7531 | } |
| 7532 | } |
| 7533 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7534 | credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7535 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 7536 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 7537 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7538 | if (IS_ERR(handle)) { |
| 7539 | status = PTR_ERR(handle); |
| 7540 | handle = NULL; |
| 7541 | mlog_errno(status); |
| 7542 | goto bail; |
| 7543 | } |
| 7544 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7545 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7546 | tc, path, meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7547 | if (status < 0) { |
| 7548 | mlog_errno(status); |
| 7549 | goto bail; |
| 7550 | } |
| 7551 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7552 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7553 | tl_sem = 0; |
| 7554 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7555 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7556 | handle = NULL; |
| 7557 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7558 | ocfs2_reinit_path(path, 1); |
| 7559 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7560 | if (meta_ac) { |
| 7561 | ocfs2_free_alloc_context(meta_ac); |
| 7562 | meta_ac = NULL; |
| 7563 | } |
| 7564 | |
| 7565 | if (ref_tree) { |
| 7566 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
| 7567 | ref_tree = NULL; |
| 7568 | } |
| 7569 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7570 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7571 | * The check above will catch the case where we've truncated |
| 7572 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7573 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7574 | goto start; |
| 7575 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7576 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7577 | |
| 7578 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7579 | |
| 7580 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7581 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7582 | |
| 7583 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7584 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7585 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7586 | if (meta_ac) |
| 7587 | ocfs2_free_alloc_context(meta_ac); |
| 7588 | |
| 7589 | if (ref_tree) |
| 7590 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
| 7591 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7592 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 7593 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7594 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7595 | |
| 7596 | /* This will drop the ext_alloc cluster lock for us */ |
| 7597 | ocfs2_free_truncate_context(tc); |
| 7598 | |
| 7599 | mlog_exit(status); |
| 7600 | return status; |
| 7601 | } |
| 7602 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7603 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7604 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7605 | */ |
| 7606 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 7607 | struct inode *inode, |
| 7608 | struct buffer_head *fe_bh, |
| 7609 | struct ocfs2_truncate_context **tc) |
| 7610 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7611 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7612 | unsigned int new_i_clusters; |
| 7613 | struct ocfs2_dinode *fe; |
| 7614 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7615 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7616 | |
| 7617 | mlog_entry_void(); |
| 7618 | |
| 7619 | *tc = NULL; |
| 7620 | |
| 7621 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 7622 | i_size_read(inode)); |
| 7623 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 7624 | |
| 7625 | 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] | 7626 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 7627 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7628 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 7629 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7630 | if (!(*tc)) { |
| 7631 | status = -ENOMEM; |
| 7632 | mlog_errno(status); |
| 7633 | goto bail; |
| 7634 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7635 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7636 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7637 | if (fe->id2.i_list.l_tree_depth) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 7638 | status = ocfs2_read_extent_block(INODE_CACHE(inode), |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 7639 | le64_to_cpu(fe->i_last_eb_blk), |
| 7640 | &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7641 | if (status < 0) { |
| 7642 | mlog_errno(status); |
| 7643 | goto bail; |
| 7644 | } |
| 7645 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7646 | } |
| 7647 | |
| 7648 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 7649 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7650 | status = 0; |
| 7651 | bail: |
| 7652 | if (status < 0) { |
| 7653 | if (*tc) |
| 7654 | ocfs2_free_truncate_context(*tc); |
| 7655 | *tc = NULL; |
| 7656 | } |
| 7657 | mlog_exit_void(); |
| 7658 | return status; |
| 7659 | } |
| 7660 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7661 | /* |
| 7662 | * 'start' is inclusive, 'end' is not. |
| 7663 | */ |
| 7664 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7665 | unsigned int start, unsigned int end, int trunc) |
| 7666 | { |
| 7667 | int ret; |
| 7668 | unsigned int numbytes; |
| 7669 | handle_t *handle; |
| 7670 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7671 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7672 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7673 | |
| 7674 | if (end > i_size_read(inode)) |
| 7675 | end = i_size_read(inode); |
| 7676 | |
| 7677 | BUG_ON(start >= end); |
| 7678 | |
| 7679 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7680 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7681 | !ocfs2_supports_inline_data(osb)) { |
| 7682 | ocfs2_error(inode->i_sb, |
| 7683 | "Inline data flags for inode %llu don't agree! " |
| 7684 | "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
| 7685 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7686 | le16_to_cpu(di->i_dyn_features), |
| 7687 | OCFS2_I(inode)->ip_dyn_features, |
| 7688 | osb->s_feature_incompat); |
| 7689 | ret = -EROFS; |
| 7690 | goto out; |
| 7691 | } |
| 7692 | |
| 7693 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7694 | if (IS_ERR(handle)) { |
| 7695 | ret = PTR_ERR(handle); |
| 7696 | mlog_errno(ret); |
| 7697 | goto out; |
| 7698 | } |
| 7699 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7700 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7701 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7702 | if (ret) { |
| 7703 | mlog_errno(ret); |
| 7704 | goto out_commit; |
| 7705 | } |
| 7706 | |
| 7707 | numbytes = end - start; |
| 7708 | memset(idata->id_data + start, 0, numbytes); |
| 7709 | |
| 7710 | /* |
| 7711 | * No need to worry about the data page here - it's been |
| 7712 | * truncated already and inline data doesn't need it for |
| 7713 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7714 | * later. |
| 7715 | */ |
| 7716 | if (trunc) { |
| 7717 | i_size_write(inode, start); |
| 7718 | di->i_size = cpu_to_le64(start); |
| 7719 | } |
| 7720 | |
| 7721 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7722 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 7723 | |
| 7724 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7725 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7726 | |
| 7727 | ocfs2_journal_dirty(handle, di_bh); |
| 7728 | |
| 7729 | out_commit: |
| 7730 | ocfs2_commit_trans(osb, handle); |
| 7731 | |
| 7732 | out: |
| 7733 | return ret; |
| 7734 | } |
| 7735 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7736 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 7737 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7738 | /* |
| 7739 | * The caller is responsible for completing deallocation |
| 7740 | * before freeing the context. |
| 7741 | */ |
| 7742 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 7743 | mlog(ML_NOTICE, |
| 7744 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7745 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 7746 | brelse(tc->tc_last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7747 | |
| 7748 | kfree(tc); |
| 7749 | } |