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); |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1053 | eb->h_suballoc_slot = |
| 1054 | cpu_to_le16(meta_ac->ac_alloc_slot); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1055 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 1056 | eb->h_list.l_count = |
| 1057 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 1058 | |
| 1059 | suballoc_bit_start++; |
| 1060 | first_blkno++; |
| 1061 | |
| 1062 | /* We'll also be dirtied by the caller, so |
| 1063 | * this isn't absolutely necessary. */ |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1064 | ocfs2_journal_dirty(handle, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | count += num_got; |
| 1068 | } |
| 1069 | |
| 1070 | status = 0; |
| 1071 | bail: |
| 1072 | if (status < 0) { |
| 1073 | for(i = 0; i < wanted; i++) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1074 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1075 | bhs[i] = NULL; |
| 1076 | } |
| 1077 | } |
| 1078 | mlog_exit(status); |
| 1079 | return status; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1083 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 1084 | * |
| 1085 | * Returns the sum of the rightmost extent rec logical offset and |
| 1086 | * cluster count. |
| 1087 | * |
| 1088 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 1089 | * value should be populated into the leftmost new branch records. |
| 1090 | * |
| 1091 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 1092 | * value for the new topmost tree record. |
| 1093 | */ |
| 1094 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 1095 | { |
| 1096 | int i; |
| 1097 | |
| 1098 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1099 | |
| 1100 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1101 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1105 | * Change range of the branches in the right most path according to the leaf |
| 1106 | * extent block's rightmost record. |
| 1107 | */ |
| 1108 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1109 | struct ocfs2_extent_tree *et) |
| 1110 | { |
| 1111 | int status; |
| 1112 | struct ocfs2_path *path = NULL; |
| 1113 | struct ocfs2_extent_list *el; |
| 1114 | struct ocfs2_extent_rec *rec; |
| 1115 | |
| 1116 | path = ocfs2_new_path_from_et(et); |
| 1117 | if (!path) { |
| 1118 | status = -ENOMEM; |
| 1119 | return status; |
| 1120 | } |
| 1121 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1122 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1123 | if (status < 0) { |
| 1124 | mlog_errno(status); |
| 1125 | goto out; |
| 1126 | } |
| 1127 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 1128 | status = ocfs2_extend_trans(handle, path_num_items(path)); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1129 | if (status < 0) { |
| 1130 | mlog_errno(status); |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1134 | status = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1135 | if (status < 0) { |
| 1136 | mlog_errno(status); |
| 1137 | goto out; |
| 1138 | } |
| 1139 | |
| 1140 | el = path_leaf_el(path); |
| 1141 | rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; |
| 1142 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1143 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1144 | |
| 1145 | out: |
| 1146 | ocfs2_free_path(path); |
| 1147 | return status; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1151 | * 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] | 1152 | * 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] | 1153 | * structure. |
| 1154 | * |
| 1155 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1156 | * for the new last extent block. |
| 1157 | * |
| 1158 | * 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] | 1159 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1160 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1161 | static int ocfs2_add_branch(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1162 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1163 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1164 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1165 | struct ocfs2_alloc_context *meta_ac) |
| 1166 | { |
| 1167 | int status, new_blocks, i; |
| 1168 | u64 next_blkno, new_last_eb_blk; |
| 1169 | struct buffer_head *bh; |
| 1170 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1171 | struct ocfs2_extent_block *eb; |
| 1172 | struct ocfs2_extent_list *eb_el; |
| 1173 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1174 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1175 | |
| 1176 | mlog_entry_void(); |
| 1177 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1178 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1179 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1180 | if (eb_bh) { |
| 1181 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1182 | el = &eb->h_list; |
| 1183 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1184 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1185 | |
| 1186 | /* we never add a branch to a leaf. */ |
| 1187 | BUG_ON(!el->l_tree_depth); |
| 1188 | |
| 1189 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1190 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1191 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1192 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1193 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1194 | |
| 1195 | /* |
| 1196 | * If there is a gap before the root end and the real end |
| 1197 | * of the righmost leaf block, we need to remove the gap |
| 1198 | * between new_cpos and root_end first so that the tree |
| 1199 | * is consistent after we add a new branch(it will start |
| 1200 | * from new_cpos). |
| 1201 | */ |
| 1202 | if (root_end > new_cpos) { |
| 1203 | mlog(0, "adjust the cluster end from %u to %u\n", |
| 1204 | root_end, new_cpos); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1205 | status = ocfs2_adjust_rightmost_branch(handle, et); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1206 | if (status) { |
| 1207 | mlog_errno(status); |
| 1208 | goto bail; |
| 1209 | } |
| 1210 | } |
| 1211 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1212 | /* allocate the number of new eb blocks we need */ |
| 1213 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1214 | GFP_KERNEL); |
| 1215 | if (!new_eb_bhs) { |
| 1216 | status = -ENOMEM; |
| 1217 | mlog_errno(status); |
| 1218 | goto bail; |
| 1219 | } |
| 1220 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1221 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1222 | meta_ac, new_eb_bhs); |
| 1223 | if (status < 0) { |
| 1224 | mlog_errno(status); |
| 1225 | goto bail; |
| 1226 | } |
| 1227 | |
| 1228 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1229 | * linked with the rest of the tree. |
| 1230 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1231 | * |
| 1232 | * when we leave the loop, new_last_eb_blk will point to the |
| 1233 | * newest leaf, and next_blkno will point to the topmost extent |
| 1234 | * block. */ |
| 1235 | next_blkno = new_last_eb_blk = 0; |
| 1236 | for(i = 0; i < new_blocks; i++) { |
| 1237 | bh = new_eb_bhs[i]; |
| 1238 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1239 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1240 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1241 | eb_el = &eb->h_list; |
| 1242 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1243 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1244 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1245 | if (status < 0) { |
| 1246 | mlog_errno(status); |
| 1247 | goto bail; |
| 1248 | } |
| 1249 | |
| 1250 | eb->h_next_leaf_blk = 0; |
| 1251 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1252 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1253 | /* |
| 1254 | * This actually counts as an empty extent as |
| 1255 | * c_clusters == 0 |
| 1256 | */ |
| 1257 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1258 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1259 | /* |
| 1260 | * eb_el isn't always an interior node, but even leaf |
| 1261 | * nodes want a zero'd flags and reserved field so |
| 1262 | * this gets the whole 32 bits regardless of use. |
| 1263 | */ |
| 1264 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1265 | if (!eb_el->l_tree_depth) |
| 1266 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1267 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1268 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1269 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1270 | } |
| 1271 | |
| 1272 | /* This is a bit hairy. We want to update up to three blocks |
| 1273 | * here without leaving any of them in an inconsistent state |
| 1274 | * in case of error. We don't have to worry about |
| 1275 | * journal_dirty erroring as it won't unless we've aborted the |
| 1276 | * handle (in which case we would never be here) so reserving |
| 1277 | * the write with journal_access is all we need to do. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1278 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1279 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1280 | if (status < 0) { |
| 1281 | mlog_errno(status); |
| 1282 | goto bail; |
| 1283 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1284 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1285 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1286 | if (status < 0) { |
| 1287 | mlog_errno(status); |
| 1288 | goto bail; |
| 1289 | } |
| 1290 | if (eb_bh) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1291 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1292 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1293 | if (status < 0) { |
| 1294 | mlog_errno(status); |
| 1295 | goto bail; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1300 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1301 | i = le16_to_cpu(el->l_next_free_rec); |
| 1302 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1303 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1304 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1305 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1306 | |
| 1307 | /* fe needs a new last extent block pointer, as does the |
| 1308 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1309 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1310 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1311 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1312 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1313 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1314 | ocfs2_journal_dirty(handle, *last_eb_bh); |
| 1315 | ocfs2_journal_dirty(handle, et->et_root_bh); |
| 1316 | if (eb_bh) |
| 1317 | ocfs2_journal_dirty(handle, eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1318 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1319 | /* |
| 1320 | * Some callers want to track the rightmost leaf so pass it |
| 1321 | * back here. |
| 1322 | */ |
| 1323 | brelse(*last_eb_bh); |
| 1324 | get_bh(new_eb_bhs[0]); |
| 1325 | *last_eb_bh = new_eb_bhs[0]; |
| 1326 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1327 | status = 0; |
| 1328 | bail: |
| 1329 | if (new_eb_bhs) { |
| 1330 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1331 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1332 | kfree(new_eb_bhs); |
| 1333 | } |
| 1334 | |
| 1335 | mlog_exit(status); |
| 1336 | return status; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | * adds another level to the allocation tree. |
| 1341 | * returns back the new extent block so you can add a branch to it |
| 1342 | * after this call. |
| 1343 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1344 | static int ocfs2_shift_tree_depth(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1345 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1346 | struct ocfs2_alloc_context *meta_ac, |
| 1347 | struct buffer_head **ret_new_eb_bh) |
| 1348 | { |
| 1349 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1350 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1351 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1352 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1353 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1354 | struct ocfs2_extent_list *eb_el; |
| 1355 | |
| 1356 | mlog_entry_void(); |
| 1357 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1358 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1359 | &new_eb_bh); |
| 1360 | if (status < 0) { |
| 1361 | mlog_errno(status); |
| 1362 | goto bail; |
| 1363 | } |
| 1364 | |
| 1365 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1366 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1367 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1368 | |
| 1369 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1370 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1371 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1372 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1373 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1374 | if (status < 0) { |
| 1375 | mlog_errno(status); |
| 1376 | goto bail; |
| 1377 | } |
| 1378 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1379 | /* copy the root extent list data into the new extent block */ |
| 1380 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1381 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1382 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1383 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1384 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1385 | ocfs2_journal_dirty(handle, new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1386 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1387 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1388 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1389 | if (status < 0) { |
| 1390 | mlog_errno(status); |
| 1391 | goto bail; |
| 1392 | } |
| 1393 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1394 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1395 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1396 | /* update root_bh now */ |
| 1397 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1398 | root_el->l_recs[0].e_cpos = 0; |
| 1399 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1400 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1401 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1402 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1403 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1404 | |
| 1405 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1406 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1407 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1408 | 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] | 1409 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1410 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1411 | |
| 1412 | *ret_new_eb_bh = new_eb_bh; |
| 1413 | new_eb_bh = NULL; |
| 1414 | status = 0; |
| 1415 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1416 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1417 | |
| 1418 | mlog_exit(status); |
| 1419 | return status; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1423 | * Should only be called when there is no space left in any of the |
| 1424 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1425 | * non-leaf extent block with room for new records. There are three |
| 1426 | * valid results of this search: |
| 1427 | * |
| 1428 | * 1) a lowest extent block is found, then we pass it back in |
| 1429 | * *lowest_eb_bh and return '0' |
| 1430 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1431 | * 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] | 1432 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1433 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1434 | * 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] | 1435 | * which case we return > 0 |
| 1436 | * |
| 1437 | * return status < 0 indicates an error. |
| 1438 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1439 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1440 | struct buffer_head **target_bh) |
| 1441 | { |
| 1442 | int status = 0, i; |
| 1443 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1444 | struct ocfs2_extent_block *eb; |
| 1445 | struct ocfs2_extent_list *el; |
| 1446 | struct buffer_head *bh = NULL; |
| 1447 | struct buffer_head *lowest_bh = NULL; |
| 1448 | |
| 1449 | mlog_entry_void(); |
| 1450 | |
| 1451 | *target_bh = NULL; |
| 1452 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1453 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1454 | |
| 1455 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1456 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1457 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1458 | "Owner %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1459 | "extent list (next_free_rec == 0)", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1460 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1461 | status = -EIO; |
| 1462 | goto bail; |
| 1463 | } |
| 1464 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1465 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1466 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1467 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 1468 | "Owner %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1469 | "list where extent # %d has no physical " |
| 1470 | "block start", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1471 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1472 | status = -EIO; |
| 1473 | goto bail; |
| 1474 | } |
| 1475 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1476 | brelse(bh); |
| 1477 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1478 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1479 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1480 | if (status < 0) { |
| 1481 | mlog_errno(status); |
| 1482 | goto bail; |
| 1483 | } |
| 1484 | |
| 1485 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1486 | el = &eb->h_list; |
| 1487 | |
| 1488 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1489 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1490 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1491 | lowest_bh = bh; |
| 1492 | get_bh(lowest_bh); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | /* If we didn't find one and the fe doesn't have any room, |
| 1497 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1498 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1499 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1500 | status = 1; |
| 1501 | |
| 1502 | *target_bh = lowest_bh; |
| 1503 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1504 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1505 | |
| 1506 | mlog_exit(status); |
| 1507 | return status; |
| 1508 | } |
| 1509 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1510 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1511 | * Grow a b-tree so that it has more records. |
| 1512 | * |
| 1513 | * We might shift the tree depth in which case existing paths should |
| 1514 | * be considered invalid. |
| 1515 | * |
| 1516 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1517 | * |
| 1518 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1519 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1520 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 1521 | int *final_depth, struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1522 | struct ocfs2_alloc_context *meta_ac) |
| 1523 | { |
| 1524 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1525 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1526 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1527 | struct buffer_head *bh = NULL; |
| 1528 | |
| 1529 | BUG_ON(meta_ac == NULL); |
| 1530 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1531 | shift = ocfs2_find_branch_target(et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1532 | if (shift < 0) { |
| 1533 | ret = shift; |
| 1534 | mlog_errno(ret); |
| 1535 | goto out; |
| 1536 | } |
| 1537 | |
| 1538 | /* We traveled all the way to the bottom of the allocation tree |
| 1539 | * and didn't find room for any more extents - we need to add |
| 1540 | * another tree level */ |
| 1541 | if (shift) { |
| 1542 | BUG_ON(bh); |
| 1543 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 1544 | |
| 1545 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1546 | * the new extent block (so we can pass that to |
| 1547 | * ocfs2_add_branch). */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1548 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1549 | if (ret < 0) { |
| 1550 | mlog_errno(ret); |
| 1551 | goto out; |
| 1552 | } |
| 1553 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1554 | if (depth == 1) { |
| 1555 | /* |
| 1556 | * Special case: we have room now if we shifted from |
| 1557 | * tree_depth 0, so no more work needs to be done. |
| 1558 | * |
| 1559 | * We won't be calling add_branch, so pass |
| 1560 | * back *last_eb_bh as the new leaf. At depth |
| 1561 | * zero, it should always be null so there's |
| 1562 | * no reason to brelse. |
| 1563 | */ |
| 1564 | BUG_ON(*last_eb_bh); |
| 1565 | get_bh(bh); |
| 1566 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1567 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1568 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1572 | * the new data. */ |
| 1573 | mlog(0, "add branch. bh = %p\n", bh); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1574 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1575 | meta_ac); |
| 1576 | if (ret < 0) { |
| 1577 | mlog_errno(ret); |
| 1578 | goto out; |
| 1579 | } |
| 1580 | |
| 1581 | out: |
| 1582 | if (final_depth) |
| 1583 | *final_depth = depth; |
| 1584 | brelse(bh); |
| 1585 | return ret; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1589 | * This function will discard the rightmost extent record. |
| 1590 | */ |
| 1591 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1592 | { |
| 1593 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1594 | int count = le16_to_cpu(el->l_count); |
| 1595 | unsigned int num_bytes; |
| 1596 | |
| 1597 | BUG_ON(!next_free); |
| 1598 | /* This will cause us to go off the end of our extent list. */ |
| 1599 | BUG_ON(next_free >= count); |
| 1600 | |
| 1601 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1602 | |
| 1603 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1604 | } |
| 1605 | |
| 1606 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1607 | struct ocfs2_extent_rec *insert_rec) |
| 1608 | { |
| 1609 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1610 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1611 | struct ocfs2_extent_rec *rec; |
| 1612 | |
| 1613 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1614 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1615 | |
| 1616 | BUG_ON(!next_free); |
| 1617 | |
| 1618 | /* 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] | 1619 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1620 | |
| 1621 | /* |
| 1622 | * The easiest way to approach this is to just remove the |
| 1623 | * empty extent and temporarily decrement next_free. |
| 1624 | */ |
| 1625 | if (has_empty) { |
| 1626 | /* |
| 1627 | * If next_free was 1 (only an empty extent), this |
| 1628 | * loop won't execute, which is fine. We still want |
| 1629 | * the decrement above to happen. |
| 1630 | */ |
| 1631 | for(i = 0; i < (next_free - 1); i++) |
| 1632 | el->l_recs[i] = el->l_recs[i+1]; |
| 1633 | |
| 1634 | next_free--; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * Figure out what the new record index should be. |
| 1639 | */ |
| 1640 | for(i = 0; i < next_free; i++) { |
| 1641 | rec = &el->l_recs[i]; |
| 1642 | |
| 1643 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1644 | break; |
| 1645 | } |
| 1646 | insert_index = i; |
| 1647 | |
| 1648 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1649 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1650 | |
| 1651 | BUG_ON(insert_index < 0); |
| 1652 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1653 | BUG_ON(insert_index > next_free); |
| 1654 | |
| 1655 | /* |
| 1656 | * No need to memmove if we're just adding to the tail. |
| 1657 | */ |
| 1658 | if (insert_index != next_free) { |
| 1659 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1660 | |
| 1661 | num_bytes = next_free - insert_index; |
| 1662 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1663 | memmove(&el->l_recs[insert_index + 1], |
| 1664 | &el->l_recs[insert_index], |
| 1665 | num_bytes); |
| 1666 | } |
| 1667 | |
| 1668 | /* |
| 1669 | * Either we had an empty extent, and need to re-increment or |
| 1670 | * there was no empty extent on a non full rightmost leaf node, |
| 1671 | * in which case we still need to increment. |
| 1672 | */ |
| 1673 | next_free++; |
| 1674 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1675 | /* |
| 1676 | * Make sure none of the math above just messed up our tree. |
| 1677 | */ |
| 1678 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1679 | |
| 1680 | el->l_recs[insert_index] = *insert_rec; |
| 1681 | |
| 1682 | } |
| 1683 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1684 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1685 | { |
| 1686 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1687 | |
| 1688 | BUG_ON(num_recs == 0); |
| 1689 | |
| 1690 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1691 | num_recs--; |
| 1692 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1693 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1694 | memset(&el->l_recs[num_recs], 0, |
| 1695 | sizeof(struct ocfs2_extent_rec)); |
| 1696 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1697 | } |
| 1698 | } |
| 1699 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1700 | /* |
| 1701 | * Create an empty extent record . |
| 1702 | * |
| 1703 | * l_next_free_rec may be updated. |
| 1704 | * |
| 1705 | * If an empty extent already exists do nothing. |
| 1706 | */ |
| 1707 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1708 | { |
| 1709 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1710 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1711 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1712 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1713 | if (next_free == 0) |
| 1714 | goto set_and_inc; |
| 1715 | |
| 1716 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1717 | return; |
| 1718 | |
| 1719 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1720 | "Asked to create an empty extent in a full list:\n" |
| 1721 | "count = %u, tree depth = %u", |
| 1722 | le16_to_cpu(el->l_count), |
| 1723 | le16_to_cpu(el->l_tree_depth)); |
| 1724 | |
| 1725 | ocfs2_shift_records_right(el); |
| 1726 | |
| 1727 | set_and_inc: |
| 1728 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1729 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1734 | * the lowest level tree node which contains a path to both leafs. This |
| 1735 | * resulting set of information can be used to form a complete "subtree" |
| 1736 | * |
| 1737 | * This function is passed two full paths from the dinode down to a |
| 1738 | * pair of adjacent leaves. It's task is to figure out which path |
| 1739 | * index contains the subtree root - this can be the root index itself |
| 1740 | * in a worst-case rotation. |
| 1741 | * |
| 1742 | * The array index of the subtree root is passed back. |
| 1743 | */ |
Tao Ma | 38a04e4 | 2009-11-30 14:32:19 +0800 | [diff] [blame] | 1744 | int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
| 1745 | struct ocfs2_path *left, |
| 1746 | struct ocfs2_path *right) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1747 | { |
| 1748 | int i = 0; |
| 1749 | |
| 1750 | /* |
| 1751 | * Check that the caller passed in two paths from the same tree. |
| 1752 | */ |
| 1753 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1754 | |
| 1755 | do { |
| 1756 | i++; |
| 1757 | |
| 1758 | /* |
| 1759 | * The caller didn't pass two adjacent paths. |
| 1760 | */ |
| 1761 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1762 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1763 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1764 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1765 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1766 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1767 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1768 | } while (left->p_node[i].bh->b_blocknr == |
| 1769 | right->p_node[i].bh->b_blocknr); |
| 1770 | |
| 1771 | return i - 1; |
| 1772 | } |
| 1773 | |
| 1774 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1775 | |
| 1776 | /* |
| 1777 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1778 | * |
| 1779 | * This code can be called with a cpos larger than the tree, in which |
| 1780 | * case it will return the rightmost path. |
| 1781 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1782 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1783 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1784 | path_insert_t *func, void *data) |
| 1785 | { |
| 1786 | int i, ret = 0; |
| 1787 | u32 range; |
| 1788 | u64 blkno; |
| 1789 | struct buffer_head *bh = NULL; |
| 1790 | struct ocfs2_extent_block *eb; |
| 1791 | struct ocfs2_extent_list *el; |
| 1792 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1793 | |
| 1794 | el = root_el; |
| 1795 | while (el->l_tree_depth) { |
| 1796 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1797 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1798 | "Owner %llu has empty extent list at " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1799 | "depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1800 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1801 | le16_to_cpu(el->l_tree_depth)); |
| 1802 | ret = -EROFS; |
| 1803 | goto out; |
| 1804 | |
| 1805 | } |
| 1806 | |
| 1807 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1808 | rec = &el->l_recs[i]; |
| 1809 | |
| 1810 | /* |
| 1811 | * In the case that cpos is off the allocation |
| 1812 | * tree, this should just wind up returning the |
| 1813 | * rightmost record. |
| 1814 | */ |
| 1815 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1816 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1817 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1818 | break; |
| 1819 | } |
| 1820 | |
| 1821 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1822 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1823 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1824 | "Owner %llu has bad blkno in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1825 | "at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1826 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1827 | le16_to_cpu(el->l_tree_depth), i); |
| 1828 | ret = -EROFS; |
| 1829 | goto out; |
| 1830 | } |
| 1831 | |
| 1832 | brelse(bh); |
| 1833 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1834 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1835 | if (ret) { |
| 1836 | mlog_errno(ret); |
| 1837 | goto out; |
| 1838 | } |
| 1839 | |
| 1840 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1841 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1842 | |
| 1843 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1844 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1845 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
| 1846 | "Owner %llu has bad count in extent list " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1847 | "at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1848 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1849 | (unsigned long long)bh->b_blocknr, |
| 1850 | le16_to_cpu(el->l_next_free_rec), |
| 1851 | le16_to_cpu(el->l_count)); |
| 1852 | ret = -EROFS; |
| 1853 | goto out; |
| 1854 | } |
| 1855 | |
| 1856 | if (func) |
| 1857 | func(data, bh); |
| 1858 | } |
| 1859 | |
| 1860 | out: |
| 1861 | /* |
| 1862 | * Catch any trailing bh that the loop didn't handle. |
| 1863 | */ |
| 1864 | brelse(bh); |
| 1865 | |
| 1866 | return ret; |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * Given an initialized path (that is, it has a valid root extent |
| 1871 | * list), this function will traverse the btree in search of the path |
| 1872 | * which would contain cpos. |
| 1873 | * |
| 1874 | * The path traveled is recorded in the path structure. |
| 1875 | * |
| 1876 | * Note that this will not do any comparisons on leaf node extent |
| 1877 | * records, so it will work fine in the case that we just added a tree |
| 1878 | * branch. |
| 1879 | */ |
| 1880 | struct find_path_data { |
| 1881 | int index; |
| 1882 | struct ocfs2_path *path; |
| 1883 | }; |
| 1884 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1885 | { |
| 1886 | struct find_path_data *fp = data; |
| 1887 | |
| 1888 | get_bh(bh); |
| 1889 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1890 | fp->index++; |
| 1891 | } |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 1892 | int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1893 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1894 | { |
| 1895 | struct find_path_data data; |
| 1896 | |
| 1897 | data.index = 1; |
| 1898 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1899 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1900 | find_path_ins, &data); |
| 1901 | } |
| 1902 | |
| 1903 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1904 | { |
| 1905 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1906 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1907 | struct buffer_head **ret = data; |
| 1908 | |
| 1909 | /* We want to retain only the leaf block. */ |
| 1910 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1911 | get_bh(bh); |
| 1912 | *ret = bh; |
| 1913 | } |
| 1914 | } |
| 1915 | /* |
| 1916 | * Find the leaf block in the tree which would contain cpos. No |
| 1917 | * checking of the actual leaf is done. |
| 1918 | * |
| 1919 | * Some paths want to call this instead of allocating a path structure |
| 1920 | * and calling ocfs2_find_path(). |
| 1921 | * |
| 1922 | * This function doesn't handle non btree extent lists. |
| 1923 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1924 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1925 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1926 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1927 | { |
| 1928 | int ret; |
| 1929 | struct buffer_head *bh = NULL; |
| 1930 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1931 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1932 | if (ret) { |
| 1933 | mlog_errno(ret); |
| 1934 | goto out; |
| 1935 | } |
| 1936 | |
| 1937 | *leaf_bh = bh; |
| 1938 | out: |
| 1939 | return ret; |
| 1940 | } |
| 1941 | |
| 1942 | /* |
| 1943 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1944 | * |
| 1945 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1946 | * we need to fix up the extent records above the changes to reflect |
| 1947 | * the new changes. |
| 1948 | * |
| 1949 | * left_rec: the record on the left. |
| 1950 | * left_child_el: is the child list pointed to by left_rec |
| 1951 | * right_rec: the record to the right of left_rec |
| 1952 | * right_child_el: is the child list pointed to by right_rec |
| 1953 | * |
| 1954 | * By definition, this only works on interior nodes. |
| 1955 | */ |
| 1956 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 1957 | struct ocfs2_extent_list *left_child_el, |
| 1958 | struct ocfs2_extent_rec *right_rec, |
| 1959 | struct ocfs2_extent_list *right_child_el) |
| 1960 | { |
| 1961 | u32 left_clusters, right_end; |
| 1962 | |
| 1963 | /* |
| 1964 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1965 | * the leftmost record in their child list. Their cluster |
| 1966 | * count covers the full theoretical range of their child list |
| 1967 | * - the range between their cpos and the cpos of the record |
| 1968 | * immediately to their right. |
| 1969 | */ |
| 1970 | 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] | 1971 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 1972 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1973 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1974 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1975 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1976 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1977 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1978 | |
| 1979 | /* |
| 1980 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1981 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1982 | * updating e_cpos to keep the same highest cluster count. |
| 1983 | */ |
| 1984 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1985 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1986 | |
| 1987 | right_rec->e_cpos = left_rec->e_cpos; |
| 1988 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 1989 | |
| 1990 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1991 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | * Adjust the adjacent root node records involved in a |
| 1996 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 1997 | * find it's index in the root list. |
| 1998 | */ |
| 1999 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 2000 | struct ocfs2_extent_list *left_el, |
| 2001 | struct ocfs2_extent_list *right_el, |
| 2002 | u64 left_el_blkno) |
| 2003 | { |
| 2004 | int i; |
| 2005 | |
| 2006 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 2007 | le16_to_cpu(left_el->l_tree_depth)); |
| 2008 | |
| 2009 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 2010 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 2011 | break; |
| 2012 | } |
| 2013 | |
| 2014 | /* |
| 2015 | * The path walking code should have never returned a root and |
| 2016 | * two paths which are not adjacent. |
| 2017 | */ |
| 2018 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 2019 | |
| 2020 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 2021 | &root_el->l_recs[i + 1], right_el); |
| 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * We've changed a leaf block (in right_path) and need to reflect that |
| 2026 | * change back up the subtree. |
| 2027 | * |
| 2028 | * This happens in multiple places: |
| 2029 | * - When we've moved an extent record from the left path leaf to the right |
| 2030 | * path leaf to make room for an empty extent in the left path leaf. |
| 2031 | * - When our insert into the right path leaf is at the leftmost edge |
| 2032 | * and requires an update of the path immediately to it's left. This |
| 2033 | * 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] | 2034 | * - When we've adjusted the last extent record in the left path leaf and the |
| 2035 | * 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] | 2036 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2037 | static void ocfs2_complete_edge_insert(handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2038 | struct ocfs2_path *left_path, |
| 2039 | struct ocfs2_path *right_path, |
| 2040 | int subtree_index) |
| 2041 | { |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2042 | int i, idx; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2043 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 2044 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 2045 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2046 | |
| 2047 | /* |
| 2048 | * Update the counts and position values within all the |
| 2049 | * interior nodes to reflect the leaf rotation we just did. |
| 2050 | * |
| 2051 | * The root node is handled below the loop. |
| 2052 | * |
| 2053 | * We begin the loop with right_el and left_el pointing to the |
| 2054 | * leaf lists and work our way up. |
| 2055 | * |
| 2056 | * NOTE: within this loop, left_el and right_el always refer |
| 2057 | * to the *child* lists. |
| 2058 | */ |
| 2059 | left_el = path_leaf_el(left_path); |
| 2060 | right_el = path_leaf_el(right_path); |
| 2061 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 2062 | mlog(0, "Adjust records at index %u\n", i); |
| 2063 | |
| 2064 | /* |
| 2065 | * One nice property of knowing that all of these |
| 2066 | * nodes are below the root is that we only deal with |
| 2067 | * the leftmost right node record and the rightmost |
| 2068 | * left node record. |
| 2069 | */ |
| 2070 | el = left_path->p_node[i].el; |
| 2071 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2072 | left_rec = &el->l_recs[idx]; |
| 2073 | |
| 2074 | el = right_path->p_node[i].el; |
| 2075 | right_rec = &el->l_recs[0]; |
| 2076 | |
| 2077 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 2078 | right_el); |
| 2079 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2080 | ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2081 | ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2082 | |
| 2083 | /* |
| 2084 | * Setup our list pointers now so that the current |
| 2085 | * parents become children in the next iteration. |
| 2086 | */ |
| 2087 | left_el = left_path->p_node[i].el; |
| 2088 | right_el = right_path->p_node[i].el; |
| 2089 | } |
| 2090 | |
| 2091 | /* |
| 2092 | * At the root node, adjust the two adjacent records which |
| 2093 | * begin our path to the leaves. |
| 2094 | */ |
| 2095 | |
| 2096 | el = left_path->p_node[subtree_index].el; |
| 2097 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2098 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2099 | |
| 2100 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2101 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2102 | |
| 2103 | root_bh = left_path->p_node[subtree_index].bh; |
| 2104 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2105 | ocfs2_journal_dirty(handle, root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2108 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2109 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2110 | struct ocfs2_path *left_path, |
| 2111 | struct ocfs2_path *right_path, |
| 2112 | int subtree_index) |
| 2113 | { |
| 2114 | int ret, i; |
| 2115 | struct buffer_head *right_leaf_bh; |
| 2116 | struct buffer_head *left_leaf_bh = NULL; |
| 2117 | struct buffer_head *root_bh; |
| 2118 | struct ocfs2_extent_list *right_el, *left_el; |
| 2119 | struct ocfs2_extent_rec move_rec; |
| 2120 | |
| 2121 | left_leaf_bh = path_leaf_bh(left_path); |
| 2122 | left_el = path_leaf_el(left_path); |
| 2123 | |
| 2124 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2125 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2126 | "Inode %llu has non-full interior leaf node %llu" |
| 2127 | "(next free = %u)", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2128 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2129 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2130 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2131 | return -EROFS; |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | * This extent block may already have an empty record, so we |
| 2136 | * return early if so. |
| 2137 | */ |
| 2138 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2139 | return 0; |
| 2140 | |
| 2141 | root_bh = left_path->p_node[subtree_index].bh; |
| 2142 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2143 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2144 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2145 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2146 | if (ret) { |
| 2147 | mlog_errno(ret); |
| 2148 | goto out; |
| 2149 | } |
| 2150 | |
| 2151 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2152 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2153 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2154 | if (ret) { |
| 2155 | mlog_errno(ret); |
| 2156 | goto out; |
| 2157 | } |
| 2158 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2159 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2160 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2161 | if (ret) { |
| 2162 | mlog_errno(ret); |
| 2163 | goto out; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | right_leaf_bh = path_leaf_bh(right_path); |
| 2168 | right_el = path_leaf_el(right_path); |
| 2169 | |
| 2170 | /* This is a code error, not a disk corruption. */ |
| 2171 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2172 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2173 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2174 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2175 | |
| 2176 | ocfs2_create_empty_extent(right_el); |
| 2177 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2178 | ocfs2_journal_dirty(handle, right_leaf_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2179 | |
| 2180 | /* Do the copy now. */ |
| 2181 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2182 | move_rec = left_el->l_recs[i]; |
| 2183 | right_el->l_recs[0] = move_rec; |
| 2184 | |
| 2185 | /* |
| 2186 | * Clear out the record we just copied and shift everything |
| 2187 | * over, leaving an empty extent in the left leaf. |
| 2188 | * |
| 2189 | * We temporarily subtract from next_free_rec so that the |
| 2190 | * shift will lose the tail record (which is now defunct). |
| 2191 | */ |
| 2192 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2193 | ocfs2_shift_records_right(left_el); |
| 2194 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2195 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2196 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2197 | ocfs2_journal_dirty(handle, left_leaf_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2198 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2199 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2200 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2201 | |
| 2202 | out: |
| 2203 | return ret; |
| 2204 | } |
| 2205 | |
| 2206 | /* |
| 2207 | * Given a full path, determine what cpos value would return us a path |
| 2208 | * containing the leaf immediately to the left of the current one. |
| 2209 | * |
| 2210 | * Will return zero if the path passed in is already the leftmost path. |
| 2211 | */ |
| 2212 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2213 | struct ocfs2_path *path, u32 *cpos) |
| 2214 | { |
| 2215 | int i, j, ret = 0; |
| 2216 | u64 blkno; |
| 2217 | struct ocfs2_extent_list *el; |
| 2218 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2219 | BUG_ON(path->p_tree_depth == 0); |
| 2220 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2221 | *cpos = 0; |
| 2222 | |
| 2223 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2224 | |
| 2225 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2226 | i = path->p_tree_depth - 1; |
| 2227 | while (i >= 0) { |
| 2228 | el = path->p_node[i].el; |
| 2229 | |
| 2230 | /* |
| 2231 | * Find the extent record just before the one in our |
| 2232 | * path. |
| 2233 | */ |
| 2234 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2235 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2236 | if (j == 0) { |
| 2237 | if (i == 0) { |
| 2238 | /* |
| 2239 | * We've determined that the |
| 2240 | * path specified is already |
| 2241 | * the leftmost one - return a |
| 2242 | * cpos of zero. |
| 2243 | */ |
| 2244 | goto out; |
| 2245 | } |
| 2246 | /* |
| 2247 | * The leftmost record points to our |
| 2248 | * leaf - we need to travel up the |
| 2249 | * tree one level. |
| 2250 | */ |
| 2251 | goto next_node; |
| 2252 | } |
| 2253 | |
| 2254 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2255 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2256 | &el->l_recs[j - 1]); |
| 2257 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2258 | goto out; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | /* |
| 2263 | * If we got here, we never found a valid node where |
| 2264 | * the tree indicated one should be. |
| 2265 | */ |
| 2266 | ocfs2_error(sb, |
| 2267 | "Invalid extent tree at extent block %llu\n", |
| 2268 | (unsigned long long)blkno); |
| 2269 | ret = -EROFS; |
| 2270 | goto out; |
| 2271 | |
| 2272 | next_node: |
| 2273 | blkno = path->p_node[i].bh->b_blocknr; |
| 2274 | i--; |
| 2275 | } |
| 2276 | |
| 2277 | out: |
| 2278 | return ret; |
| 2279 | } |
| 2280 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2281 | /* |
| 2282 | * Extend the transaction by enough credits to complete the rotation, |
| 2283 | * and still leave at least the original number of credits allocated |
| 2284 | * to this transaction. |
| 2285 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2286 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2287 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2288 | struct ocfs2_path *path) |
| 2289 | { |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 2290 | int ret = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2291 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2292 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 2293 | if (handle->h_buffer_credits < credits) |
Tao Ma | c18b812 | 2009-08-18 11:44:07 +0800 | [diff] [blame] | 2294 | ret = ocfs2_extend_trans(handle, |
| 2295 | credits - handle->h_buffer_credits); |
Tao Ma | c18b812 | 2009-08-18 11:44:07 +0800 | [diff] [blame] | 2296 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 2297 | return ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * Trap the case where we're inserting into the theoretical range past |
| 2302 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2303 | * whose cpos is less than ours into the right leaf. |
| 2304 | * |
| 2305 | * It's only necessary to look at the rightmost record of the left |
| 2306 | * leaf because the logic that calls us should ensure that the |
| 2307 | * theoretical ranges in the path components above the leaves are |
| 2308 | * correct. |
| 2309 | */ |
| 2310 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2311 | u32 insert_cpos) |
| 2312 | { |
| 2313 | struct ocfs2_extent_list *left_el; |
| 2314 | struct ocfs2_extent_rec *rec; |
| 2315 | int next_free; |
| 2316 | |
| 2317 | left_el = path_leaf_el(left_path); |
| 2318 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2319 | rec = &left_el->l_recs[next_free - 1]; |
| 2320 | |
| 2321 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2322 | return 1; |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2326 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2327 | { |
| 2328 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2329 | unsigned int range; |
| 2330 | struct ocfs2_extent_rec *rec; |
| 2331 | |
| 2332 | if (next_free == 0) |
| 2333 | return 0; |
| 2334 | |
| 2335 | rec = &el->l_recs[0]; |
| 2336 | if (ocfs2_is_empty_extent(rec)) { |
| 2337 | /* Empty list. */ |
| 2338 | if (next_free == 1) |
| 2339 | return 0; |
| 2340 | rec = &el->l_recs[1]; |
| 2341 | } |
| 2342 | |
| 2343 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2344 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2345 | return 1; |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2349 | /* |
| 2350 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2351 | * |
| 2352 | * The path to the rightmost leaf should be passed in. |
| 2353 | * |
| 2354 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2355 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2356 | * Upon successful return from this function: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2357 | * |
| 2358 | * - The 'right_path' array will contain a path to the leaf block |
| 2359 | * whose range contains e_cpos. |
| 2360 | * - That leaf block will have a single empty extent in list index 0. |
| 2361 | * - In the case that the rotation requires a post-insert update, |
| 2362 | * *ret_left_path will contain a valid path which can be passed to |
| 2363 | * ocfs2_insert_path(). |
| 2364 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2365 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2366 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2367 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2368 | u32 insert_cpos, |
| 2369 | struct ocfs2_path *right_path, |
| 2370 | struct ocfs2_path **ret_left_path) |
| 2371 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2372 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2373 | u32 cpos; |
| 2374 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2375 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2376 | |
| 2377 | *ret_left_path = NULL; |
| 2378 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2379 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2380 | if (!left_path) { |
| 2381 | ret = -ENOMEM; |
| 2382 | mlog_errno(ret); |
| 2383 | goto out; |
| 2384 | } |
| 2385 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2386 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2387 | if (ret) { |
| 2388 | mlog_errno(ret); |
| 2389 | goto out; |
| 2390 | } |
| 2391 | |
| 2392 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 2393 | |
| 2394 | /* |
| 2395 | * What we want to do here is: |
| 2396 | * |
| 2397 | * 1) Start with the rightmost path. |
| 2398 | * |
| 2399 | * 2) Determine a path to the leaf block directly to the left |
| 2400 | * of that leaf. |
| 2401 | * |
| 2402 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2403 | * which contains a path to both leaves. |
| 2404 | * |
| 2405 | * 4) Rotate the subtree. |
| 2406 | * |
| 2407 | * 5) Find the next subtree by considering the left path to be |
| 2408 | * the new right path. |
| 2409 | * |
| 2410 | * The check at the top of this while loop also accepts |
| 2411 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2412 | * value to get us the left path - insert_cpos might very well |
| 2413 | * be filling that hole. |
| 2414 | * |
| 2415 | * Stop at a cpos of '0' because we either started at the |
| 2416 | * leftmost branch (i.e., a tree with one branch and a |
| 2417 | * rotation inside of it), or we've gone as far as we can in |
| 2418 | * rotating subtrees. |
| 2419 | */ |
| 2420 | while (cpos && insert_cpos <= cpos) { |
| 2421 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 2422 | insert_cpos, cpos); |
| 2423 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2424 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2425 | if (ret) { |
| 2426 | mlog_errno(ret); |
| 2427 | goto out; |
| 2428 | } |
| 2429 | |
| 2430 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2431 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2432 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2433 | "(left path cpos %u) results in two identical " |
| 2434 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2435 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2436 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2437 | (unsigned long long) |
| 2438 | path_leaf_bh(left_path)->b_blocknr); |
| 2439 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2440 | if (split == SPLIT_NONE && |
| 2441 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2442 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2443 | |
| 2444 | /* |
| 2445 | * We've rotated the tree as much as we |
| 2446 | * should. The rest is up to |
| 2447 | * ocfs2_insert_path() to complete, after the |
| 2448 | * record insertion. We indicate this |
| 2449 | * situation by returning the left path. |
| 2450 | * |
| 2451 | * The reason we don't adjust the records here |
| 2452 | * before the record insert is that an error |
| 2453 | * later might break the rule where a parent |
| 2454 | * record e_cpos will reflect the actual |
| 2455 | * e_cpos of the 1st nonempty record of the |
| 2456 | * child list. |
| 2457 | */ |
| 2458 | *ret_left_path = left_path; |
| 2459 | goto out_ret_path; |
| 2460 | } |
| 2461 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2462 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2463 | |
| 2464 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2465 | start, |
| 2466 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 2467 | right_path->p_tree_depth); |
| 2468 | |
| 2469 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2470 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2471 | if (ret) { |
| 2472 | mlog_errno(ret); |
| 2473 | goto out; |
| 2474 | } |
| 2475 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2476 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2477 | right_path, start); |
| 2478 | if (ret) { |
| 2479 | mlog_errno(ret); |
| 2480 | goto out; |
| 2481 | } |
| 2482 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2483 | if (split != SPLIT_NONE && |
| 2484 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2485 | insert_cpos)) { |
| 2486 | /* |
| 2487 | * A rotate moves the rightmost left leaf |
| 2488 | * record over to the leftmost right leaf |
| 2489 | * slot. If we're doing an extent split |
| 2490 | * instead of a real insert, then we have to |
| 2491 | * check that the extent to be split wasn't |
| 2492 | * just moved over. If it was, then we can |
| 2493 | * exit here, passing left_path back - |
| 2494 | * ocfs2_split_extent() is smart enough to |
| 2495 | * search both leaves. |
| 2496 | */ |
| 2497 | *ret_left_path = left_path; |
| 2498 | goto out_ret_path; |
| 2499 | } |
| 2500 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2501 | /* |
| 2502 | * There is no need to re-read the next right path |
| 2503 | * as we know that it'll be our current left |
| 2504 | * path. Optimize by copying values instead. |
| 2505 | */ |
| 2506 | ocfs2_mv_path(right_path, left_path); |
| 2507 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2508 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2509 | if (ret) { |
| 2510 | mlog_errno(ret); |
| 2511 | goto out; |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | out: |
| 2516 | ocfs2_free_path(left_path); |
| 2517 | |
| 2518 | out_ret_path: |
| 2519 | return ret; |
| 2520 | } |
| 2521 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2522 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2523 | struct ocfs2_extent_tree *et, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2524 | int subtree_index, struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2525 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2526 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2527 | struct ocfs2_extent_rec *rec; |
| 2528 | struct ocfs2_extent_list *el; |
| 2529 | struct ocfs2_extent_block *eb; |
| 2530 | u32 range; |
| 2531 | |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2532 | /* |
| 2533 | * In normal tree rotation process, we will never touch the |
| 2534 | * tree branch above subtree_index and ocfs2_extend_rotate_transaction |
| 2535 | * doesn't reserve the credits for them either. |
| 2536 | * |
| 2537 | * But we do have a special case here which will update the rightmost |
| 2538 | * records for all the bh in the path. |
| 2539 | * So we have to allocate extra credits and access them. |
| 2540 | */ |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 2541 | ret = ocfs2_extend_trans(handle, subtree_index); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2542 | if (ret) { |
| 2543 | mlog_errno(ret); |
| 2544 | goto out; |
| 2545 | } |
| 2546 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2547 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2548 | if (ret) { |
| 2549 | mlog_errno(ret); |
| 2550 | goto out; |
| 2551 | } |
| 2552 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2553 | /* Path should always be rightmost. */ |
| 2554 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2555 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2556 | |
| 2557 | el = &eb->h_list; |
| 2558 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2559 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2560 | rec = &el->l_recs[idx]; |
| 2561 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2562 | |
| 2563 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2564 | el = path->p_node[i].el; |
| 2565 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2566 | rec = &el->l_recs[idx]; |
| 2567 | |
| 2568 | rec->e_int_clusters = cpu_to_le32(range); |
| 2569 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2570 | |
| 2571 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2572 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2573 | out: |
| 2574 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2575 | } |
| 2576 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2577 | static void ocfs2_unlink_path(handle_t *handle, |
| 2578 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2579 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2580 | struct ocfs2_path *path, int unlink_start) |
| 2581 | { |
| 2582 | int ret, i; |
| 2583 | struct ocfs2_extent_block *eb; |
| 2584 | struct ocfs2_extent_list *el; |
| 2585 | struct buffer_head *bh; |
| 2586 | |
| 2587 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2588 | bh = path->p_node[i].bh; |
| 2589 | |
| 2590 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2591 | /* |
| 2592 | * Not all nodes might have had their final count |
| 2593 | * decremented by the caller - handle this here. |
| 2594 | */ |
| 2595 | el = &eb->h_list; |
| 2596 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2597 | mlog(ML_ERROR, |
| 2598 | "Inode %llu, attempted to remove extent block " |
| 2599 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2600 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2601 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2602 | le16_to_cpu(el->l_next_free_rec)); |
| 2603 | |
| 2604 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2605 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2606 | continue; |
| 2607 | } |
| 2608 | |
| 2609 | el->l_next_free_rec = 0; |
| 2610 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2611 | |
| 2612 | ocfs2_journal_dirty(handle, bh); |
| 2613 | |
| 2614 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2615 | if (ret) |
| 2616 | mlog_errno(ret); |
| 2617 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2618 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2619 | } |
| 2620 | } |
| 2621 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2622 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2623 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2624 | struct ocfs2_path *left_path, |
| 2625 | struct ocfs2_path *right_path, |
| 2626 | int subtree_index, |
| 2627 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2628 | { |
| 2629 | int i; |
| 2630 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2631 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2632 | struct ocfs2_extent_list *el; |
| 2633 | struct ocfs2_extent_block *eb; |
| 2634 | |
| 2635 | el = path_leaf_el(left_path); |
| 2636 | |
| 2637 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2638 | |
| 2639 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2640 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2641 | break; |
| 2642 | |
| 2643 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2644 | |
| 2645 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2646 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2647 | |
| 2648 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2649 | eb->h_next_leaf_blk = 0; |
| 2650 | |
| 2651 | ocfs2_journal_dirty(handle, root_bh); |
| 2652 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2653 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2654 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2655 | subtree_index + 1); |
| 2656 | } |
| 2657 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2658 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
| 2659 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2660 | struct ocfs2_path *left_path, |
| 2661 | struct ocfs2_path *right_path, |
| 2662 | int subtree_index, |
| 2663 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2664 | int *deleted) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2665 | { |
| 2666 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2667 | 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] | 2668 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2669 | struct ocfs2_extent_block *eb; |
| 2670 | |
| 2671 | *deleted = 0; |
| 2672 | |
| 2673 | right_leaf_el = path_leaf_el(right_path); |
| 2674 | left_leaf_el = path_leaf_el(left_path); |
| 2675 | root_bh = left_path->p_node[subtree_index].bh; |
| 2676 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2677 | |
| 2678 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2679 | return 0; |
| 2680 | |
| 2681 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2682 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2683 | /* |
| 2684 | * It's legal for us to proceed if the right leaf is |
| 2685 | * the rightmost one and it has an empty extent. There |
| 2686 | * are two cases to handle - whether the leaf will be |
| 2687 | * empty after removal or not. If the leaf isn't empty |
| 2688 | * then just remove the empty extent up front. The |
| 2689 | * next block will handle empty leaves by flagging |
| 2690 | * them for unlink. |
| 2691 | * |
| 2692 | * Non rightmost leaves will throw -EAGAIN and the |
| 2693 | * caller can manually move the subtree and retry. |
| 2694 | */ |
| 2695 | |
| 2696 | if (eb->h_next_leaf_blk != 0ULL) |
| 2697 | return -EAGAIN; |
| 2698 | |
| 2699 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2700 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2701 | path_leaf_bh(right_path), |
| 2702 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2703 | if (ret) { |
| 2704 | mlog_errno(ret); |
| 2705 | goto out; |
| 2706 | } |
| 2707 | |
| 2708 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2709 | } else |
| 2710 | right_has_empty = 1; |
| 2711 | } |
| 2712 | |
| 2713 | if (eb->h_next_leaf_blk == 0ULL && |
| 2714 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2715 | /* |
| 2716 | * We have to update i_last_eb_blk during the meta |
| 2717 | * data delete. |
| 2718 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2719 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2720 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2721 | if (ret) { |
| 2722 | mlog_errno(ret); |
| 2723 | goto out; |
| 2724 | } |
| 2725 | |
| 2726 | del_right_subtree = 1; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * Getting here with an empty extent in the right path implies |
| 2731 | * that it's the rightmost path and will be deleted. |
| 2732 | */ |
| 2733 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2734 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2735 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2736 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2737 | if (ret) { |
| 2738 | mlog_errno(ret); |
| 2739 | goto out; |
| 2740 | } |
| 2741 | |
| 2742 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2743 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2744 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2745 | if (ret) { |
| 2746 | mlog_errno(ret); |
| 2747 | goto out; |
| 2748 | } |
| 2749 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2750 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2751 | left_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2752 | if (ret) { |
| 2753 | mlog_errno(ret); |
| 2754 | goto out; |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | if (!right_has_empty) { |
| 2759 | /* |
| 2760 | * Only do this if we're moving a real |
| 2761 | * record. Otherwise, the action is delayed until |
| 2762 | * after removal of the right path in which case we |
| 2763 | * can do a simple shift to remove the empty extent. |
| 2764 | */ |
| 2765 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2766 | memset(&right_leaf_el->l_recs[0], 0, |
| 2767 | sizeof(struct ocfs2_extent_rec)); |
| 2768 | } |
| 2769 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2770 | /* |
| 2771 | * Move recs over to get rid of empty extent, decrease |
| 2772 | * next_free. This is allowed to remove the last |
| 2773 | * extent in our leaf (setting l_next_free_rec to |
| 2774 | * zero) - the delete code below won't care. |
| 2775 | */ |
| 2776 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2777 | } |
| 2778 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2779 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2780 | ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2781 | |
| 2782 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2783 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2784 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2785 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2786 | left_path); |
| 2787 | if (ret) { |
| 2788 | mlog_errno(ret); |
| 2789 | goto out; |
| 2790 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2791 | |
| 2792 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2793 | 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] | 2794 | |
| 2795 | /* |
| 2796 | * Removal of the extent in the left leaf was skipped |
| 2797 | * above so we could delete the right path |
| 2798 | * 1st. |
| 2799 | */ |
| 2800 | if (right_has_empty) |
| 2801 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2802 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2803 | ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2804 | |
| 2805 | *deleted = 1; |
| 2806 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2807 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2808 | subtree_index); |
| 2809 | |
| 2810 | out: |
| 2811 | return ret; |
| 2812 | } |
| 2813 | |
| 2814 | /* |
| 2815 | * Given a full path, determine what cpos value would return us a path |
| 2816 | * containing the leaf immediately to the right of the current one. |
| 2817 | * |
| 2818 | * Will return zero if the path passed in is already the rightmost path. |
| 2819 | * |
| 2820 | * This looks similar, but is subtly different to |
| 2821 | * ocfs2_find_cpos_for_left_leaf(). |
| 2822 | */ |
Tao Ma | 38a04e4 | 2009-11-30 14:32:19 +0800 | [diff] [blame] | 2823 | int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2824 | struct ocfs2_path *path, u32 *cpos) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2825 | { |
| 2826 | int i, j, ret = 0; |
| 2827 | u64 blkno; |
| 2828 | struct ocfs2_extent_list *el; |
| 2829 | |
| 2830 | *cpos = 0; |
| 2831 | |
| 2832 | if (path->p_tree_depth == 0) |
| 2833 | return 0; |
| 2834 | |
| 2835 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2836 | |
| 2837 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2838 | i = path->p_tree_depth - 1; |
| 2839 | while (i >= 0) { |
| 2840 | int next_free; |
| 2841 | |
| 2842 | el = path->p_node[i].el; |
| 2843 | |
| 2844 | /* |
| 2845 | * Find the extent record just after the one in our |
| 2846 | * path. |
| 2847 | */ |
| 2848 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2849 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2850 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2851 | if (j == (next_free - 1)) { |
| 2852 | if (i == 0) { |
| 2853 | /* |
| 2854 | * We've determined that the |
| 2855 | * path specified is already |
| 2856 | * the rightmost one - return a |
| 2857 | * cpos of zero. |
| 2858 | */ |
| 2859 | goto out; |
| 2860 | } |
| 2861 | /* |
| 2862 | * The rightmost record points to our |
| 2863 | * leaf - we need to travel up the |
| 2864 | * tree one level. |
| 2865 | */ |
| 2866 | goto next_node; |
| 2867 | } |
| 2868 | |
| 2869 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2870 | goto out; |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | /* |
| 2875 | * If we got here, we never found a valid node where |
| 2876 | * the tree indicated one should be. |
| 2877 | */ |
| 2878 | ocfs2_error(sb, |
| 2879 | "Invalid extent tree at extent block %llu\n", |
| 2880 | (unsigned long long)blkno); |
| 2881 | ret = -EROFS; |
| 2882 | goto out; |
| 2883 | |
| 2884 | next_node: |
| 2885 | blkno = path->p_node[i].bh->b_blocknr; |
| 2886 | i--; |
| 2887 | } |
| 2888 | |
| 2889 | out: |
| 2890 | return ret; |
| 2891 | } |
| 2892 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2893 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
| 2894 | struct ocfs2_extent_tree *et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2895 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2896 | { |
| 2897 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2898 | struct buffer_head *bh = path_leaf_bh(path); |
| 2899 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2900 | |
| 2901 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2902 | return 0; |
| 2903 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2904 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2905 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2906 | if (ret) { |
| 2907 | mlog_errno(ret); |
| 2908 | goto out; |
| 2909 | } |
| 2910 | |
| 2911 | ocfs2_remove_empty_extent(el); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2912 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2913 | |
| 2914 | out: |
| 2915 | return ret; |
| 2916 | } |
| 2917 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2918 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
| 2919 | struct ocfs2_extent_tree *et, |
| 2920 | int orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2921 | struct ocfs2_path *path, |
| 2922 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2923 | struct ocfs2_path **empty_extent_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2924 | { |
| 2925 | int ret, subtree_root, deleted; |
| 2926 | u32 right_cpos; |
| 2927 | struct ocfs2_path *left_path = NULL; |
| 2928 | struct ocfs2_path *right_path = NULL; |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2929 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2930 | |
| 2931 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 2932 | |
| 2933 | *empty_extent_path = NULL; |
| 2934 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2935 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2936 | if (ret) { |
| 2937 | mlog_errno(ret); |
| 2938 | goto out; |
| 2939 | } |
| 2940 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2941 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2942 | if (!left_path) { |
| 2943 | ret = -ENOMEM; |
| 2944 | mlog_errno(ret); |
| 2945 | goto out; |
| 2946 | } |
| 2947 | |
| 2948 | ocfs2_cp_path(left_path, path); |
| 2949 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2950 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2951 | if (!right_path) { |
| 2952 | ret = -ENOMEM; |
| 2953 | mlog_errno(ret); |
| 2954 | goto out; |
| 2955 | } |
| 2956 | |
| 2957 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 2958 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2959 | if (ret) { |
| 2960 | mlog_errno(ret); |
| 2961 | goto out; |
| 2962 | } |
| 2963 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2964 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2965 | right_path); |
| 2966 | |
| 2967 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2968 | subtree_root, |
| 2969 | (unsigned long long) |
| 2970 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 2971 | right_path->p_tree_depth); |
| 2972 | |
| 2973 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 2974 | orig_credits, left_path); |
| 2975 | if (ret) { |
| 2976 | mlog_errno(ret); |
| 2977 | goto out; |
| 2978 | } |
| 2979 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2980 | /* |
| 2981 | * Caller might still want to make changes to the |
| 2982 | * tree root, so re-add it to the journal here. |
| 2983 | */ |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2984 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2985 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2986 | if (ret) { |
| 2987 | mlog_errno(ret); |
| 2988 | goto out; |
| 2989 | } |
| 2990 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2991 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2992 | right_path, subtree_root, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2993 | dealloc, &deleted); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2994 | if (ret == -EAGAIN) { |
| 2995 | /* |
| 2996 | * The rotation has to temporarily stop due to |
| 2997 | * the right subtree having an empty |
| 2998 | * extent. Pass it back to the caller for a |
| 2999 | * fixup. |
| 3000 | */ |
| 3001 | *empty_extent_path = right_path; |
| 3002 | right_path = NULL; |
| 3003 | goto out; |
| 3004 | } |
| 3005 | if (ret) { |
| 3006 | mlog_errno(ret); |
| 3007 | goto out; |
| 3008 | } |
| 3009 | |
| 3010 | /* |
| 3011 | * The subtree rotate might have removed records on |
| 3012 | * the rightmost edge. If so, then rotation is |
| 3013 | * complete. |
| 3014 | */ |
| 3015 | if (deleted) |
| 3016 | break; |
| 3017 | |
| 3018 | ocfs2_mv_path(left_path, right_path); |
| 3019 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3020 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3021 | &right_cpos); |
| 3022 | if (ret) { |
| 3023 | mlog_errno(ret); |
| 3024 | goto out; |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | out: |
| 3029 | ocfs2_free_path(right_path); |
| 3030 | ocfs2_free_path(left_path); |
| 3031 | |
| 3032 | return ret; |
| 3033 | } |
| 3034 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3035 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
| 3036 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3037 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3038 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3039 | { |
| 3040 | int ret, subtree_index; |
| 3041 | u32 cpos; |
| 3042 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3043 | struct ocfs2_extent_block *eb; |
| 3044 | struct ocfs2_extent_list *el; |
| 3045 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3046 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 3047 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3048 | if (ret) |
| 3049 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3050 | /* |
| 3051 | * There's two ways we handle this depending on |
| 3052 | * whether path is the only existing one. |
| 3053 | */ |
| 3054 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3055 | handle->h_buffer_credits, |
| 3056 | path); |
| 3057 | if (ret) { |
| 3058 | mlog_errno(ret); |
| 3059 | goto out; |
| 3060 | } |
| 3061 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3062 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3063 | if (ret) { |
| 3064 | mlog_errno(ret); |
| 3065 | goto out; |
| 3066 | } |
| 3067 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3068 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3069 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3070 | if (ret) { |
| 3071 | mlog_errno(ret); |
| 3072 | goto out; |
| 3073 | } |
| 3074 | |
| 3075 | if (cpos) { |
| 3076 | /* |
| 3077 | * We have a path to the left of this one - it needs |
| 3078 | * an update too. |
| 3079 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3080 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3081 | if (!left_path) { |
| 3082 | ret = -ENOMEM; |
| 3083 | mlog_errno(ret); |
| 3084 | goto out; |
| 3085 | } |
| 3086 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3087 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3088 | if (ret) { |
| 3089 | mlog_errno(ret); |
| 3090 | goto out; |
| 3091 | } |
| 3092 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3093 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3094 | if (ret) { |
| 3095 | mlog_errno(ret); |
| 3096 | goto out; |
| 3097 | } |
| 3098 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3099 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3100 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3101 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3102 | subtree_index, dealloc); |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 3103 | ret = ocfs2_update_edge_lengths(handle, et, subtree_index, |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3104 | left_path); |
| 3105 | if (ret) { |
| 3106 | mlog_errno(ret); |
| 3107 | goto out; |
| 3108 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3109 | |
| 3110 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3111 | 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] | 3112 | } else { |
| 3113 | /* |
| 3114 | * 'path' is also the leftmost path which |
| 3115 | * means it must be the only one. This gets |
| 3116 | * handled differently because we want to |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3117 | * revert the root back to having extents |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3118 | * in-line. |
| 3119 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3120 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3121 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3122 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3123 | el->l_tree_depth = 0; |
| 3124 | el->l_next_free_rec = 0; |
| 3125 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3126 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3127 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3131 | |
| 3132 | out: |
| 3133 | ocfs2_free_path(left_path); |
| 3134 | return ret; |
| 3135 | } |
| 3136 | |
| 3137 | /* |
| 3138 | * Left rotation of btree records. |
| 3139 | * |
| 3140 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3141 | * rotation. We start at some non-rightmost path containing an empty |
| 3142 | * extent in the leaf block. The code works its way to the rightmost |
| 3143 | * path by rotating records to the left in every subtree. |
| 3144 | * |
| 3145 | * This is used by any code which reduces the number of extent records |
| 3146 | * in a leaf. After removal, an empty record should be placed in the |
| 3147 | * leftmost list position. |
| 3148 | * |
| 3149 | * This won't handle a length update of the rightmost path records if |
| 3150 | * the rightmost tree leaf record is removed so the caller is |
| 3151 | * responsible for detecting and correcting that. |
| 3152 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3153 | static int ocfs2_rotate_tree_left(handle_t *handle, |
| 3154 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3155 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3156 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3157 | { |
| 3158 | int ret, orig_credits = handle->h_buffer_credits; |
| 3159 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3160 | struct ocfs2_extent_block *eb; |
| 3161 | struct ocfs2_extent_list *el; |
| 3162 | |
| 3163 | el = path_leaf_el(path); |
| 3164 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3165 | return 0; |
| 3166 | |
| 3167 | if (path->p_tree_depth == 0) { |
| 3168 | rightmost_no_delete: |
| 3169 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3170 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3171 | * it up front. |
| 3172 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3173 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3174 | if (ret) |
| 3175 | mlog_errno(ret); |
| 3176 | goto out; |
| 3177 | } |
| 3178 | |
| 3179 | /* |
| 3180 | * Handle rightmost branch now. There's several cases: |
| 3181 | * 1) simple rotation leaving records in there. That's trivial. |
| 3182 | * 2) rotation requiring a branch delete - there's no more |
| 3183 | * records left. Two cases of this: |
| 3184 | * a) There are branches to the left. |
| 3185 | * b) This is also the leftmost (the only) branch. |
| 3186 | * |
| 3187 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3188 | * 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] | 3189 | * 2b) we need to bring the root back to inline extents. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3190 | */ |
| 3191 | |
| 3192 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3193 | el = &eb->h_list; |
| 3194 | if (eb->h_next_leaf_blk == 0) { |
| 3195 | /* |
| 3196 | * This gets a bit tricky if we're going to delete the |
| 3197 | * rightmost path. Get the other cases out of the way |
| 3198 | * 1st. |
| 3199 | */ |
| 3200 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3201 | goto rightmost_no_delete; |
| 3202 | |
| 3203 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3204 | ret = -EIO; |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3205 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3206 | "Owner %llu has empty extent block at %llu", |
| 3207 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3208 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3209 | goto out; |
| 3210 | } |
| 3211 | |
| 3212 | /* |
| 3213 | * XXX: The caller can not trust "path" any more after |
| 3214 | * this as it will have been deleted. What do we do? |
| 3215 | * |
| 3216 | * In theory the rotate-for-merge code will never get |
| 3217 | * here because it'll always ask for a rotate in a |
| 3218 | * nonempty list. |
| 3219 | */ |
| 3220 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3221 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
| 3222 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3223 | if (ret) |
| 3224 | mlog_errno(ret); |
| 3225 | goto out; |
| 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3230 | * and restarting from there. |
| 3231 | */ |
| 3232 | try_rotate: |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3233 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
| 3234 | dealloc, &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3235 | if (ret && ret != -EAGAIN) { |
| 3236 | mlog_errno(ret); |
| 3237 | goto out; |
| 3238 | } |
| 3239 | |
| 3240 | while (ret == -EAGAIN) { |
| 3241 | tmp_path = restart_path; |
| 3242 | restart_path = NULL; |
| 3243 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3244 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3245 | tmp_path, dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3246 | &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3247 | if (ret && ret != -EAGAIN) { |
| 3248 | mlog_errno(ret); |
| 3249 | goto out; |
| 3250 | } |
| 3251 | |
| 3252 | ocfs2_free_path(tmp_path); |
| 3253 | tmp_path = NULL; |
| 3254 | |
| 3255 | if (ret == 0) |
| 3256 | goto try_rotate; |
| 3257 | } |
| 3258 | |
| 3259 | out: |
| 3260 | ocfs2_free_path(tmp_path); |
| 3261 | ocfs2_free_path(restart_path); |
| 3262 | return ret; |
| 3263 | } |
| 3264 | |
| 3265 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3266 | int index) |
| 3267 | { |
| 3268 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3269 | unsigned int size; |
| 3270 | |
| 3271 | if (rec->e_leaf_clusters == 0) { |
| 3272 | /* |
| 3273 | * We consumed all of the merged-from record. An empty |
| 3274 | * extent cannot exist anywhere but the 1st array |
| 3275 | * position, so move things over if the merged-from |
| 3276 | * record doesn't occupy that position. |
| 3277 | * |
| 3278 | * This creates a new empty extent so the caller |
| 3279 | * should be smart enough to have removed any existing |
| 3280 | * ones. |
| 3281 | */ |
| 3282 | if (index > 0) { |
| 3283 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3284 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3285 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3286 | } |
| 3287 | |
| 3288 | /* |
| 3289 | * Always memset - the caller doesn't check whether it |
| 3290 | * created an empty extent, so there could be junk in |
| 3291 | * the other fields. |
| 3292 | */ |
| 3293 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3294 | } |
| 3295 | } |
| 3296 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3297 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3298 | struct ocfs2_path *left_path, |
| 3299 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3300 | { |
| 3301 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3302 | u32 right_cpos; |
| 3303 | struct ocfs2_path *right_path = NULL; |
| 3304 | struct ocfs2_extent_list *left_el; |
| 3305 | |
| 3306 | *ret_right_path = NULL; |
| 3307 | |
| 3308 | /* This function shouldn't be called for non-trees. */ |
| 3309 | BUG_ON(left_path->p_tree_depth == 0); |
| 3310 | |
| 3311 | left_el = path_leaf_el(left_path); |
| 3312 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3313 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3314 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3315 | left_path, &right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3316 | if (ret) { |
| 3317 | mlog_errno(ret); |
| 3318 | goto out; |
| 3319 | } |
| 3320 | |
| 3321 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3322 | BUG_ON(right_cpos == 0); |
| 3323 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3324 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3325 | if (!right_path) { |
| 3326 | ret = -ENOMEM; |
| 3327 | mlog_errno(ret); |
| 3328 | goto out; |
| 3329 | } |
| 3330 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3331 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3332 | if (ret) { |
| 3333 | mlog_errno(ret); |
| 3334 | goto out; |
| 3335 | } |
| 3336 | |
| 3337 | *ret_right_path = right_path; |
| 3338 | out: |
| 3339 | if (ret) |
| 3340 | ocfs2_free_path(right_path); |
| 3341 | return ret; |
| 3342 | } |
| 3343 | |
| 3344 | /* |
| 3345 | * Remove split_rec clusters from the record at index and merge them |
| 3346 | * onto the beginning of the record "next" to it. |
| 3347 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3348 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3349 | * next extent block. |
| 3350 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3351 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3352 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3353 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3354 | struct ocfs2_extent_rec *split_rec, |
| 3355 | int index) |
| 3356 | { |
| 3357 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3358 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3359 | struct ocfs2_extent_rec *left_rec; |
| 3360 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3361 | struct ocfs2_extent_list *right_el; |
| 3362 | struct ocfs2_path *right_path = NULL; |
| 3363 | int subtree_index = 0; |
| 3364 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3365 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3366 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3367 | |
| 3368 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3369 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3370 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3371 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3372 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3373 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3374 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3375 | if (ret) { |
| 3376 | mlog_errno(ret); |
| 3377 | goto out; |
| 3378 | } |
| 3379 | |
| 3380 | right_el = path_leaf_el(right_path); |
| 3381 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3382 | BUG_ON(next_free <= 0); |
| 3383 | right_rec = &right_el->l_recs[0]; |
| 3384 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3385 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3386 | right_rec = &right_el->l_recs[1]; |
| 3387 | } |
| 3388 | |
| 3389 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3390 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3391 | le32_to_cpu(right_rec->e_cpos)); |
| 3392 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3393 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3394 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3395 | |
| 3396 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3397 | handle->h_buffer_credits, |
| 3398 | right_path); |
| 3399 | if (ret) { |
| 3400 | mlog_errno(ret); |
| 3401 | goto out; |
| 3402 | } |
| 3403 | |
| 3404 | root_bh = left_path->p_node[subtree_index].bh; |
| 3405 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3406 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3407 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3408 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3409 | if (ret) { |
| 3410 | mlog_errno(ret); |
| 3411 | goto out; |
| 3412 | } |
| 3413 | |
| 3414 | for (i = subtree_index + 1; |
| 3415 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3416 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3417 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3418 | if (ret) { |
| 3419 | mlog_errno(ret); |
| 3420 | goto out; |
| 3421 | } |
| 3422 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3423 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3424 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3425 | if (ret) { |
| 3426 | mlog_errno(ret); |
| 3427 | goto out; |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | } else { |
| 3432 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3433 | right_rec = &el->l_recs[index + 1]; |
| 3434 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3435 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3436 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3437 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3438 | if (ret) { |
| 3439 | mlog_errno(ret); |
| 3440 | goto out; |
| 3441 | } |
| 3442 | |
| 3443 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3444 | |
| 3445 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3446 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3447 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3448 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3449 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3450 | |
| 3451 | ocfs2_cleanup_merge(el, index); |
| 3452 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3453 | ocfs2_journal_dirty(handle, bh); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3454 | if (right_path) { |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3455 | ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3456 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3457 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3458 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3459 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3460 | if (right_path) |
| 3461 | ocfs2_free_path(right_path); |
| 3462 | return ret; |
| 3463 | } |
| 3464 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3465 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3466 | struct ocfs2_path *right_path, |
| 3467 | struct ocfs2_path **ret_left_path) |
| 3468 | { |
| 3469 | int ret; |
| 3470 | u32 left_cpos; |
| 3471 | struct ocfs2_path *left_path = NULL; |
| 3472 | |
| 3473 | *ret_left_path = NULL; |
| 3474 | |
| 3475 | /* This function shouldn't be called for non-trees. */ |
| 3476 | BUG_ON(right_path->p_tree_depth == 0); |
| 3477 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3478 | 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] | 3479 | right_path, &left_cpos); |
| 3480 | if (ret) { |
| 3481 | mlog_errno(ret); |
| 3482 | goto out; |
| 3483 | } |
| 3484 | |
| 3485 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3486 | BUG_ON(left_cpos == 0); |
| 3487 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3488 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3489 | if (!left_path) { |
| 3490 | ret = -ENOMEM; |
| 3491 | mlog_errno(ret); |
| 3492 | goto out; |
| 3493 | } |
| 3494 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3495 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3496 | if (ret) { |
| 3497 | mlog_errno(ret); |
| 3498 | goto out; |
| 3499 | } |
| 3500 | |
| 3501 | *ret_left_path = left_path; |
| 3502 | out: |
| 3503 | if (ret) |
| 3504 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3505 | return ret; |
| 3506 | } |
| 3507 | |
| 3508 | /* |
| 3509 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3510 | * onto the tail of the record "before" it. |
| 3511 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3512 | * |
| 3513 | * For index == 0, the "before" means the last record of the previous |
| 3514 | * extent block. And there is also a situation that we may need to |
| 3515 | * remove the rightmost leaf extent block in the right_path and change |
| 3516 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3517 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3518 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3519 | handle_t *handle, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3520 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3521 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3522 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3523 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3524 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3525 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3526 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3527 | struct ocfs2_extent_rec *left_rec; |
| 3528 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3529 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3530 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3531 | struct buffer_head *root_bh = NULL; |
| 3532 | struct ocfs2_path *left_path = NULL; |
| 3533 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3534 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3535 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3536 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3537 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3538 | if (index == 0) { |
| 3539 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3540 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3541 | if (ret) { |
| 3542 | mlog_errno(ret); |
| 3543 | goto out; |
| 3544 | } |
| 3545 | |
| 3546 | left_el = path_leaf_el(left_path); |
| 3547 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3548 | le16_to_cpu(left_el->l_count)); |
| 3549 | |
| 3550 | left_rec = &left_el->l_recs[ |
| 3551 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3552 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3553 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3554 | le32_to_cpu(split_rec->e_cpos)); |
| 3555 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3556 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3557 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3558 | |
| 3559 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3560 | handle->h_buffer_credits, |
| 3561 | left_path); |
| 3562 | if (ret) { |
| 3563 | mlog_errno(ret); |
| 3564 | goto out; |
| 3565 | } |
| 3566 | |
| 3567 | root_bh = left_path->p_node[subtree_index].bh; |
| 3568 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3569 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3570 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3571 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3572 | if (ret) { |
| 3573 | mlog_errno(ret); |
| 3574 | goto out; |
| 3575 | } |
| 3576 | |
| 3577 | for (i = subtree_index + 1; |
| 3578 | i < path_num_items(right_path); i++) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3579 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3580 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3581 | if (ret) { |
| 3582 | mlog_errno(ret); |
| 3583 | goto out; |
| 3584 | } |
| 3585 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3586 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3587 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3588 | if (ret) { |
| 3589 | mlog_errno(ret); |
| 3590 | goto out; |
| 3591 | } |
| 3592 | } |
| 3593 | } else { |
| 3594 | left_rec = &el->l_recs[index - 1]; |
| 3595 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3596 | has_empty_extent = 1; |
| 3597 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3598 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3599 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3600 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3601 | if (ret) { |
| 3602 | mlog_errno(ret); |
| 3603 | goto out; |
| 3604 | } |
| 3605 | |
| 3606 | if (has_empty_extent && index == 1) { |
| 3607 | /* |
| 3608 | * The easy case - we can just plop the record right in. |
| 3609 | */ |
| 3610 | *left_rec = *split_rec; |
| 3611 | |
| 3612 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3613 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3614 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3615 | |
| 3616 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3617 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3618 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3619 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3620 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3621 | |
| 3622 | ocfs2_cleanup_merge(el, index); |
| 3623 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3624 | ocfs2_journal_dirty(handle, bh); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3625 | if (left_path) { |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3626 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3627 | |
| 3628 | /* |
| 3629 | * In the situation that the right_rec is empty and the extent |
| 3630 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3631 | * it and we need to delete the right extent block. |
| 3632 | */ |
| 3633 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3634 | le16_to_cpu(el->l_next_free_rec) == 1) { |
| 3635 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3636 | ret = ocfs2_remove_rightmost_path(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3637 | right_path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3638 | dealloc); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3639 | if (ret) { |
| 3640 | mlog_errno(ret); |
| 3641 | goto out; |
| 3642 | } |
| 3643 | |
| 3644 | /* Now the rightmost extent block has been deleted. |
| 3645 | * So we use the new rightmost path. |
| 3646 | */ |
| 3647 | ocfs2_mv_path(right_path, left_path); |
| 3648 | left_path = NULL; |
| 3649 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3650 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3651 | right_path, subtree_index); |
| 3652 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3653 | out: |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3654 | if (left_path) |
| 3655 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3656 | return ret; |
| 3657 | } |
| 3658 | |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3659 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
| 3660 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3661 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3662 | int split_index, |
| 3663 | struct ocfs2_extent_rec *split_rec, |
| 3664 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3665 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3666 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3667 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3668 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3669 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3670 | |
| 3671 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3672 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3673 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 3674 | /* |
| 3675 | * The merge code will need to create an empty |
| 3676 | * extent to take the place of the newly |
| 3677 | * emptied slot. Remove any pre-existing empty |
| 3678 | * extents - having more than one in a leaf is |
| 3679 | * illegal. |
| 3680 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3681 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3682 | if (ret) { |
| 3683 | mlog_errno(ret); |
| 3684 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3685 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3686 | split_index--; |
| 3687 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3691 | /* |
| 3692 | * Left-right contig implies this. |
| 3693 | */ |
| 3694 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3695 | |
| 3696 | /* |
| 3697 | * Since the leftright insert always covers the entire |
| 3698 | * extent, this call will delete the insert record |
| 3699 | * entirely, resulting in an empty extent record added to |
| 3700 | * the extent block. |
| 3701 | * |
| 3702 | * Since the adding of an empty extent shifts |
| 3703 | * everything back to the right, there's no need to |
| 3704 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3705 | * |
| 3706 | * When the split_index is zero, we need to merge it to the |
| 3707 | * prevoius extent block. It is more efficient and easier |
| 3708 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3709 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3710 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3711 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3712 | if (ret) { |
| 3713 | mlog_errno(ret); |
| 3714 | goto out; |
| 3715 | } |
| 3716 | |
| 3717 | /* |
| 3718 | * We can only get this from logic error above. |
| 3719 | */ |
| 3720 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3721 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3722 | /* The merge left us with an empty extent, remove it. */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3723 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3724 | if (ret) { |
| 3725 | mlog_errno(ret); |
| 3726 | goto out; |
| 3727 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3728 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3729 | rec = &el->l_recs[split_index]; |
| 3730 | |
| 3731 | /* |
| 3732 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3733 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3734 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3735 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
| 3736 | dealloc, split_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3737 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3738 | if (ret) { |
| 3739 | mlog_errno(ret); |
| 3740 | goto out; |
| 3741 | } |
| 3742 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3743 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3744 | /* |
| 3745 | * Error from this last rotate is not critical, so |
| 3746 | * print but don't bubble it up. |
| 3747 | */ |
| 3748 | if (ret) |
| 3749 | mlog_errno(ret); |
| 3750 | ret = 0; |
| 3751 | } else { |
| 3752 | /* |
| 3753 | * Merge a record to the left or right. |
| 3754 | * |
| 3755 | * 'contig_type' is relative to the existing record, |
| 3756 | * so for example, if we're "right contig", it's to |
| 3757 | * the record on the left (hence the left merge). |
| 3758 | */ |
| 3759 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3760 | ret = ocfs2_merge_rec_left(path, handle, et, |
| 3761 | split_rec, dealloc, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3762 | split_index); |
| 3763 | if (ret) { |
| 3764 | mlog_errno(ret); |
| 3765 | goto out; |
| 3766 | } |
| 3767 | } else { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3768 | ret = ocfs2_merge_rec_right(path, handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3769 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3770 | split_index); |
| 3771 | if (ret) { |
| 3772 | mlog_errno(ret); |
| 3773 | goto out; |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | if (ctxt->c_split_covers_rec) { |
| 3778 | /* |
| 3779 | * The merge may have left an empty extent in |
| 3780 | * our leaf. Try to rotate it away. |
| 3781 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3782 | ret = ocfs2_rotate_tree_left(handle, et, path, |
| 3783 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3784 | if (ret) |
| 3785 | mlog_errno(ret); |
| 3786 | ret = 0; |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | out: |
| 3791 | return ret; |
| 3792 | } |
| 3793 | |
| 3794 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3795 | enum ocfs2_split_type split, |
| 3796 | struct ocfs2_extent_rec *rec, |
| 3797 | struct ocfs2_extent_rec *split_rec) |
| 3798 | { |
| 3799 | u64 len_blocks; |
| 3800 | |
| 3801 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3802 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3803 | |
| 3804 | if (split == SPLIT_LEFT) { |
| 3805 | /* |
| 3806 | * Region is on the left edge of the existing |
| 3807 | * record. |
| 3808 | */ |
| 3809 | le32_add_cpu(&rec->e_cpos, |
| 3810 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3811 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3812 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3813 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3814 | } else { |
| 3815 | /* |
| 3816 | * Region is on the right edge of the existing |
| 3817 | * record. |
| 3818 | */ |
| 3819 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3820 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3821 | } |
| 3822 | } |
| 3823 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3824 | /* |
| 3825 | * Do the final bits of extent record insertion at the target leaf |
| 3826 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3827 | * that the tree above has been prepared. |
| 3828 | */ |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3829 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et, |
| 3830 | struct ocfs2_extent_rec *insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3831 | struct ocfs2_extent_list *el, |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3832 | struct ocfs2_insert_type *insert) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3833 | { |
| 3834 | int i = insert->ins_contig_index; |
| 3835 | unsigned int range; |
| 3836 | struct ocfs2_extent_rec *rec; |
| 3837 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3838 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3839 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3840 | if (insert->ins_split != SPLIT_NONE) { |
| 3841 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3842 | BUG_ON(i == -1); |
| 3843 | rec = &el->l_recs[i]; |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3844 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3845 | insert->ins_split, rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3846 | insert_rec); |
| 3847 | goto rotate; |
| 3848 | } |
| 3849 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3850 | /* |
| 3851 | * Contiguous insert - either left or right. |
| 3852 | */ |
| 3853 | if (insert->ins_contig != CONTIG_NONE) { |
| 3854 | rec = &el->l_recs[i]; |
| 3855 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3856 | rec->e_blkno = insert_rec->e_blkno; |
| 3857 | rec->e_cpos = insert_rec->e_cpos; |
| 3858 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3859 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3860 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3861 | return; |
| 3862 | } |
| 3863 | |
| 3864 | /* |
| 3865 | * Handle insert into an empty leaf. |
| 3866 | */ |
| 3867 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3868 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3869 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3870 | el->l_recs[0] = *insert_rec; |
| 3871 | el->l_next_free_rec = cpu_to_le16(1); |
| 3872 | return; |
| 3873 | } |
| 3874 | |
| 3875 | /* |
| 3876 | * Appending insert. |
| 3877 | */ |
| 3878 | if (insert->ins_appending == APPEND_TAIL) { |
| 3879 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3880 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3881 | range = le32_to_cpu(rec->e_cpos) |
| 3882 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3883 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3884 | |
| 3885 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3886 | le16_to_cpu(el->l_count), |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3887 | "owner %llu, depth %u, count %u, next free %u, " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3888 | "rec.cpos %u, rec.clusters %u, " |
| 3889 | "insert.cpos %u, insert.clusters %u\n", |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3890 | ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3891 | le16_to_cpu(el->l_tree_depth), |
| 3892 | le16_to_cpu(el->l_count), |
| 3893 | le16_to_cpu(el->l_next_free_rec), |
| 3894 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3895 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3896 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3897 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3898 | i++; |
| 3899 | el->l_recs[i] = *insert_rec; |
| 3900 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3901 | return; |
| 3902 | } |
| 3903 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3904 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3905 | /* |
| 3906 | * Ok, we have to rotate. |
| 3907 | * |
| 3908 | * At this point, it is safe to assume that inserting into an |
| 3909 | * empty leaf and appending to a leaf have both been handled |
| 3910 | * above. |
| 3911 | * |
| 3912 | * This leaf needs to have space, either by the empty 1st |
| 3913 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3914 | */ |
| 3915 | ocfs2_rotate_leaf(el, insert_rec); |
| 3916 | } |
| 3917 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3918 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 3919 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3920 | struct ocfs2_path *path, |
| 3921 | struct ocfs2_extent_rec *insert_rec) |
| 3922 | { |
| 3923 | int ret, i, next_free; |
| 3924 | struct buffer_head *bh; |
| 3925 | struct ocfs2_extent_list *el; |
| 3926 | struct ocfs2_extent_rec *rec; |
| 3927 | |
| 3928 | /* |
| 3929 | * Update everything except the leaf block. |
| 3930 | */ |
| 3931 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3932 | bh = path->p_node[i].bh; |
| 3933 | el = path->p_node[i].el; |
| 3934 | |
| 3935 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3936 | if (next_free == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3937 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3938 | "Owner %llu has a bad extent list", |
| 3939 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3940 | ret = -EIO; |
| 3941 | return; |
| 3942 | } |
| 3943 | |
| 3944 | rec = &el->l_recs[next_free - 1]; |
| 3945 | |
| 3946 | rec->e_int_clusters = insert_rec->e_cpos; |
| 3947 | le32_add_cpu(&rec->e_int_clusters, |
| 3948 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 3949 | le32_add_cpu(&rec->e_int_clusters, |
| 3950 | -le32_to_cpu(rec->e_cpos)); |
| 3951 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3952 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3953 | } |
| 3954 | } |
| 3955 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3956 | static int ocfs2_append_rec_to_path(handle_t *handle, |
| 3957 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3958 | struct ocfs2_extent_rec *insert_rec, |
| 3959 | struct ocfs2_path *right_path, |
| 3960 | struct ocfs2_path **ret_left_path) |
| 3961 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3962 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3963 | struct ocfs2_extent_list *el; |
| 3964 | struct ocfs2_path *left_path = NULL; |
| 3965 | |
| 3966 | *ret_left_path = NULL; |
| 3967 | |
| 3968 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3969 | * This shouldn't happen for non-trees. The extent rec cluster |
| 3970 | * count manipulation below only works for interior nodes. |
| 3971 | */ |
| 3972 | BUG_ON(right_path->p_tree_depth == 0); |
| 3973 | |
| 3974 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3975 | * If our appending insert is at the leftmost edge of a leaf, |
| 3976 | * then we might need to update the rightmost records of the |
| 3977 | * neighboring path. |
| 3978 | */ |
| 3979 | el = path_leaf_el(right_path); |
| 3980 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3981 | if (next_free == 0 || |
| 3982 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3983 | u32 left_cpos; |
| 3984 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3985 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3986 | right_path, &left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3987 | if (ret) { |
| 3988 | mlog_errno(ret); |
| 3989 | goto out; |
| 3990 | } |
| 3991 | |
| 3992 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 3993 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 3994 | left_cpos); |
| 3995 | |
| 3996 | /* |
| 3997 | * No need to worry if the append is already in the |
| 3998 | * leftmost leaf. |
| 3999 | */ |
| 4000 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4001 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4002 | if (!left_path) { |
| 4003 | ret = -ENOMEM; |
| 4004 | mlog_errno(ret); |
| 4005 | goto out; |
| 4006 | } |
| 4007 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4008 | ret = ocfs2_find_path(et->et_ci, left_path, |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4009 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4010 | if (ret) { |
| 4011 | mlog_errno(ret); |
| 4012 | goto out; |
| 4013 | } |
| 4014 | |
| 4015 | /* |
| 4016 | * ocfs2_insert_path() will pass the left_path to the |
| 4017 | * journal for us. |
| 4018 | */ |
| 4019 | } |
| 4020 | } |
| 4021 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4022 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4023 | if (ret) { |
| 4024 | mlog_errno(ret); |
| 4025 | goto out; |
| 4026 | } |
| 4027 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4028 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4029 | |
| 4030 | *ret_left_path = left_path; |
| 4031 | ret = 0; |
| 4032 | out: |
| 4033 | if (ret != 0) |
| 4034 | ocfs2_free_path(left_path); |
| 4035 | |
| 4036 | return ret; |
| 4037 | } |
| 4038 | |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4039 | static void ocfs2_split_record(struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4040 | struct ocfs2_path *left_path, |
| 4041 | struct ocfs2_path *right_path, |
| 4042 | struct ocfs2_extent_rec *split_rec, |
| 4043 | enum ocfs2_split_type split) |
| 4044 | { |
| 4045 | int index; |
| 4046 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4047 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4048 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4049 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4050 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4051 | if (left_path) |
| 4052 | left_el = path_leaf_el(left_path); |
| 4053 | |
| 4054 | el = right_el; |
| 4055 | insert_el = right_el; |
| 4056 | index = ocfs2_search_extent_list(el, cpos); |
| 4057 | if (index != -1) { |
| 4058 | if (index == 0 && left_path) { |
| 4059 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4060 | |
| 4061 | /* |
| 4062 | * This typically means that the record |
| 4063 | * started in the left path but moved to the |
| 4064 | * right as a result of rotation. We either |
| 4065 | * move the existing record to the left, or we |
| 4066 | * do the later insert there. |
| 4067 | * |
| 4068 | * In this case, the left path should always |
| 4069 | * exist as the rotate code will have passed |
| 4070 | * it back for a post-insert update. |
| 4071 | */ |
| 4072 | |
| 4073 | if (split == SPLIT_LEFT) { |
| 4074 | /* |
| 4075 | * It's a left split. Since we know |
| 4076 | * that the rotate code gave us an |
| 4077 | * empty extent in the left path, we |
| 4078 | * can just do the insert there. |
| 4079 | */ |
| 4080 | insert_el = left_el; |
| 4081 | } else { |
| 4082 | /* |
| 4083 | * Right split - we have to move the |
| 4084 | * existing record over to the left |
| 4085 | * leaf. The insert will be into the |
| 4086 | * newly created empty extent in the |
| 4087 | * right leaf. |
| 4088 | */ |
| 4089 | tmprec = &right_el->l_recs[index]; |
| 4090 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4091 | el = left_el; |
| 4092 | |
| 4093 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4094 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4095 | BUG_ON(index == -1); |
| 4096 | } |
| 4097 | } |
| 4098 | } else { |
| 4099 | BUG_ON(!left_path); |
| 4100 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4101 | /* |
| 4102 | * Left path is easy - we can just allow the insert to |
| 4103 | * happen. |
| 4104 | */ |
| 4105 | el = left_el; |
| 4106 | insert_el = left_el; |
| 4107 | index = ocfs2_search_extent_list(el, cpos); |
| 4108 | BUG_ON(index == -1); |
| 4109 | } |
| 4110 | |
| 4111 | rec = &el->l_recs[index]; |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4112 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4113 | split, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4114 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4115 | } |
| 4116 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4117 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4118 | * This function only does inserts on an allocation b-tree. For tree |
| 4119 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4120 | * |
| 4121 | * right_path is the path we want to do the actual insert |
| 4122 | * in. left_path should only be passed in if we need to update that |
| 4123 | * portion of the tree after an edge insert. |
| 4124 | */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4125 | static int ocfs2_insert_path(handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4126 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4127 | struct ocfs2_path *left_path, |
| 4128 | struct ocfs2_path *right_path, |
| 4129 | struct ocfs2_extent_rec *insert_rec, |
| 4130 | struct ocfs2_insert_type *insert) |
| 4131 | { |
| 4132 | int ret, subtree_index; |
| 4133 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4134 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4135 | if (left_path) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4136 | /* |
| 4137 | * There's a chance that left_path got passed back to |
| 4138 | * us without being accounted for in the |
| 4139 | * journal. Extend our transaction here to be sure we |
| 4140 | * can change those blocks. |
| 4141 | */ |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 4142 | ret = ocfs2_extend_trans(handle, left_path->p_tree_depth); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4143 | if (ret < 0) { |
| 4144 | mlog_errno(ret); |
| 4145 | goto out; |
| 4146 | } |
| 4147 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4148 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4149 | if (ret < 0) { |
| 4150 | mlog_errno(ret); |
| 4151 | goto out; |
| 4152 | } |
| 4153 | } |
| 4154 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4155 | /* |
| 4156 | * Pass both paths to the journal. The majority of inserts |
| 4157 | * will be touching all components anyway. |
| 4158 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4159 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4160 | if (ret < 0) { |
| 4161 | mlog_errno(ret); |
| 4162 | goto out; |
| 4163 | } |
| 4164 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4165 | if (insert->ins_split != SPLIT_NONE) { |
| 4166 | /* |
| 4167 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4168 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4169 | * function sort it all out. |
| 4170 | */ |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4171 | ocfs2_split_record(et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4172 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4173 | |
| 4174 | /* |
| 4175 | * Split might have modified either leaf and we don't |
| 4176 | * have a guarantee that the later edge insert will |
| 4177 | * dirty this for us. |
| 4178 | */ |
| 4179 | if (left_path) |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4180 | ocfs2_journal_dirty(handle, |
| 4181 | path_leaf_bh(left_path)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4182 | } else |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4183 | ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path), |
| 4184 | insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4185 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4186 | ocfs2_journal_dirty(handle, leaf_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4187 | |
| 4188 | if (left_path) { |
| 4189 | /* |
| 4190 | * The rotate code has indicated that we need to fix |
| 4191 | * up portions of the tree after the insert. |
| 4192 | * |
| 4193 | * XXX: Should we extend the transaction here? |
| 4194 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4195 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4196 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4197 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4198 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | ret = 0; |
| 4202 | out: |
| 4203 | return ret; |
| 4204 | } |
| 4205 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4206 | static int ocfs2_do_insert_extent(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4207 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4208 | struct ocfs2_extent_rec *insert_rec, |
| 4209 | struct ocfs2_insert_type *type) |
| 4210 | { |
| 4211 | int ret, rotate = 0; |
| 4212 | u32 cpos; |
| 4213 | struct ocfs2_path *right_path = NULL; |
| 4214 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4215 | struct ocfs2_extent_list *el; |
| 4216 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4217 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4218 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4219 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4220 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4221 | if (ret) { |
| 4222 | mlog_errno(ret); |
| 4223 | goto out; |
| 4224 | } |
| 4225 | |
| 4226 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4227 | ocfs2_insert_at_leaf(et, insert_rec, el, type); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4228 | goto out_update_clusters; |
| 4229 | } |
| 4230 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4231 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4232 | if (!right_path) { |
| 4233 | ret = -ENOMEM; |
| 4234 | mlog_errno(ret); |
| 4235 | goto out; |
| 4236 | } |
| 4237 | |
| 4238 | /* |
| 4239 | * Determine the path to start with. Rotations need the |
| 4240 | * rightmost path, everything else can go directly to the |
| 4241 | * target leaf. |
| 4242 | */ |
| 4243 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4244 | if (type->ins_appending == APPEND_NONE && |
| 4245 | type->ins_contig == CONTIG_NONE) { |
| 4246 | rotate = 1; |
| 4247 | cpos = UINT_MAX; |
| 4248 | } |
| 4249 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4250 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4251 | if (ret) { |
| 4252 | mlog_errno(ret); |
| 4253 | goto out; |
| 4254 | } |
| 4255 | |
| 4256 | /* |
| 4257 | * Rotations and appends need special treatment - they modify |
| 4258 | * parts of the tree's above them. |
| 4259 | * |
| 4260 | * Both might pass back a path immediate to the left of the |
| 4261 | * one being inserted to. This will be cause |
| 4262 | * ocfs2_insert_path() to modify the rightmost records of |
| 4263 | * left_path to account for an edge insert. |
| 4264 | * |
| 4265 | * XXX: When modifying this code, keep in mind that an insert |
| 4266 | * can wind up skipping both of these two special cases... |
| 4267 | */ |
| 4268 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4269 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4270 | le32_to_cpu(insert_rec->e_cpos), |
| 4271 | right_path, &left_path); |
| 4272 | if (ret) { |
| 4273 | mlog_errno(ret); |
| 4274 | goto out; |
| 4275 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4276 | |
| 4277 | /* |
| 4278 | * ocfs2_rotate_tree_right() might have extended the |
| 4279 | * transaction without re-journaling our tree root. |
| 4280 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4281 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4282 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4283 | if (ret) { |
| 4284 | mlog_errno(ret); |
| 4285 | goto out; |
| 4286 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4287 | } else if (type->ins_appending == APPEND_TAIL |
| 4288 | && type->ins_contig != CONTIG_LEFT) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4289 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4290 | right_path, &left_path); |
| 4291 | if (ret) { |
| 4292 | mlog_errno(ret); |
| 4293 | goto out; |
| 4294 | } |
| 4295 | } |
| 4296 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4297 | ret = ocfs2_insert_path(handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4298 | insert_rec, type); |
| 4299 | if (ret) { |
| 4300 | mlog_errno(ret); |
| 4301 | goto out; |
| 4302 | } |
| 4303 | |
| 4304 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4305 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4306 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4307 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4308 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4309 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4310 | |
| 4311 | out: |
| 4312 | ocfs2_free_path(left_path); |
| 4313 | ocfs2_free_path(right_path); |
| 4314 | |
| 4315 | return ret; |
| 4316 | } |
| 4317 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4318 | static enum ocfs2_contig_type |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4319 | ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et, |
| 4320 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4321 | struct ocfs2_extent_list *el, int index, |
| 4322 | struct ocfs2_extent_rec *split_rec) |
| 4323 | { |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4324 | int status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4325 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4326 | u32 left_cpos, right_cpos; |
| 4327 | struct ocfs2_extent_rec *rec = NULL; |
| 4328 | struct ocfs2_extent_list *new_el; |
| 4329 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4330 | struct buffer_head *bh; |
| 4331 | struct ocfs2_extent_block *eb; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4332 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4333 | |
| 4334 | if (index > 0) { |
| 4335 | rec = &el->l_recs[index - 1]; |
| 4336 | } else if (path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4337 | status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4338 | if (status) |
| 4339 | goto out; |
| 4340 | |
| 4341 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4342 | left_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4343 | if (!left_path) |
| 4344 | goto out; |
| 4345 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4346 | status = ocfs2_find_path(et->et_ci, left_path, |
| 4347 | left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4348 | if (status) |
| 4349 | goto out; |
| 4350 | |
| 4351 | new_el = path_leaf_el(left_path); |
| 4352 | |
| 4353 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4354 | le16_to_cpu(new_el->l_count)) { |
| 4355 | bh = path_leaf_bh(left_path); |
| 4356 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4357 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4358 | "Extent block #%llu has an " |
| 4359 | "invalid l_next_free_rec of " |
| 4360 | "%d. It should have " |
| 4361 | "matched the l_count of %d", |
| 4362 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4363 | le16_to_cpu(new_el->l_next_free_rec), |
| 4364 | le16_to_cpu(new_el->l_count)); |
| 4365 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4366 | goto out; |
| 4367 | } |
| 4368 | rec = &new_el->l_recs[ |
| 4369 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4370 | } |
| 4371 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4372 | |
| 4373 | /* |
| 4374 | * We're careful to check for an empty extent record here - |
| 4375 | * the merge code will know what to do if it sees one. |
| 4376 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4377 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4378 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4379 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4380 | ret = CONTIG_RIGHT; |
| 4381 | } else { |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4382 | ret = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4383 | } |
| 4384 | } |
| 4385 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4386 | rec = NULL; |
| 4387 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4388 | rec = &el->l_recs[index + 1]; |
| 4389 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4390 | path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4391 | status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4392 | if (status) |
| 4393 | goto out; |
| 4394 | |
| 4395 | if (right_cpos == 0) |
| 4396 | goto out; |
| 4397 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4398 | right_path = ocfs2_new_path_from_path(path); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4399 | if (!right_path) |
| 4400 | goto out; |
| 4401 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4402 | status = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4403 | if (status) |
| 4404 | goto out; |
| 4405 | |
| 4406 | new_el = path_leaf_el(right_path); |
| 4407 | rec = &new_el->l_recs[0]; |
| 4408 | if (ocfs2_is_empty_extent(rec)) { |
| 4409 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4410 | bh = path_leaf_bh(right_path); |
| 4411 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4412 | ocfs2_error(sb, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4413 | "Extent block #%llu has an " |
| 4414 | "invalid l_next_free_rec of %d", |
| 4415 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4416 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4417 | status = -EINVAL; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4418 | goto out; |
| 4419 | } |
| 4420 | rec = &new_el->l_recs[1]; |
| 4421 | } |
| 4422 | } |
| 4423 | |
| 4424 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4425 | enum ocfs2_contig_type contig_type; |
| 4426 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4427 | contig_type = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4428 | |
| 4429 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4430 | ret = CONTIG_LEFTRIGHT; |
| 4431 | else if (ret == CONTIG_NONE) |
| 4432 | ret = contig_type; |
| 4433 | } |
| 4434 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4435 | out: |
| 4436 | if (left_path) |
| 4437 | ocfs2_free_path(left_path); |
| 4438 | if (right_path) |
| 4439 | ocfs2_free_path(right_path); |
| 4440 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4441 | return ret; |
| 4442 | } |
| 4443 | |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4444 | static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4445 | struct ocfs2_insert_type *insert, |
| 4446 | struct ocfs2_extent_list *el, |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4447 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4448 | { |
| 4449 | int i; |
| 4450 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4451 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4452 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4453 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4454 | 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] | 4455 | contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i], |
| 4456 | insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4457 | if (contig_type != CONTIG_NONE) { |
| 4458 | insert->ins_contig_index = i; |
| 4459 | break; |
| 4460 | } |
| 4461 | } |
| 4462 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4463 | |
| 4464 | if (insert->ins_contig != CONTIG_NONE) { |
| 4465 | struct ocfs2_extent_rec *rec = |
| 4466 | &el->l_recs[insert->ins_contig_index]; |
| 4467 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4468 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4469 | |
| 4470 | /* |
| 4471 | * Caller might want us to limit the size of extents, don't |
| 4472 | * calculate contiguousness if we might exceed that limit. |
| 4473 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4474 | if (et->et_max_leaf_clusters && |
| 4475 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4476 | insert->ins_contig = CONTIG_NONE; |
| 4477 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | /* |
| 4481 | * This should only be called against the righmost leaf extent list. |
| 4482 | * |
| 4483 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4484 | * insert at the tail of the rightmost leaf. |
| 4485 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4486 | * This should also work against the root extent list for tree's with 0 |
| 4487 | * 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] | 4488 | * then the logic here makes sense. |
| 4489 | */ |
| 4490 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4491 | struct ocfs2_extent_list *el, |
| 4492 | struct ocfs2_extent_rec *insert_rec) |
| 4493 | { |
| 4494 | int i; |
| 4495 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4496 | struct ocfs2_extent_rec *rec; |
| 4497 | |
| 4498 | insert->ins_appending = APPEND_NONE; |
| 4499 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4500 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4501 | |
| 4502 | if (!el->l_next_free_rec) |
| 4503 | goto set_tail_append; |
| 4504 | |
| 4505 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4506 | /* Were all records empty? */ |
| 4507 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4508 | goto set_tail_append; |
| 4509 | } |
| 4510 | |
| 4511 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4512 | rec = &el->l_recs[i]; |
| 4513 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4514 | if (cpos >= |
| 4515 | (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] | 4516 | goto set_tail_append; |
| 4517 | |
| 4518 | return; |
| 4519 | |
| 4520 | set_tail_append: |
| 4521 | insert->ins_appending = APPEND_TAIL; |
| 4522 | } |
| 4523 | |
| 4524 | /* |
| 4525 | * Helper function called at the begining of an insert. |
| 4526 | * |
| 4527 | * This computes a few things that are commonly used in the process of |
| 4528 | * inserting into the btree: |
| 4529 | * - Whether the new extent is contiguous with an existing one. |
| 4530 | * - The current tree depth. |
| 4531 | * - Whether the insert is an appending one. |
| 4532 | * - The total # of free records in the tree. |
| 4533 | * |
| 4534 | * All of the information is stored on the ocfs2_insert_type |
| 4535 | * structure. |
| 4536 | */ |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4537 | static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4538 | struct buffer_head **last_eb_bh, |
| 4539 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4540 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4541 | struct ocfs2_insert_type *insert) |
| 4542 | { |
| 4543 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4544 | struct ocfs2_extent_block *eb; |
| 4545 | struct ocfs2_extent_list *el; |
| 4546 | struct ocfs2_path *path = NULL; |
| 4547 | struct buffer_head *bh = NULL; |
| 4548 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4549 | insert->ins_split = SPLIT_NONE; |
| 4550 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4551 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4552 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4553 | |
| 4554 | if (el->l_tree_depth) { |
| 4555 | /* |
| 4556 | * If we have tree depth, we read in the |
| 4557 | * rightmost extent block ahead of time as |
| 4558 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4559 | * may want it later. |
| 4560 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4561 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4562 | ocfs2_et_get_last_eb_blk(et), |
| 4563 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4564 | if (ret) { |
| 4565 | mlog_exit(ret); |
| 4566 | goto out; |
| 4567 | } |
| 4568 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4569 | el = &eb->h_list; |
| 4570 | } |
| 4571 | |
| 4572 | /* |
| 4573 | * Unless we have a contiguous insert, we'll need to know if |
| 4574 | * there is room left in our allocation tree for another |
| 4575 | * extent record. |
| 4576 | * |
| 4577 | * XXX: This test is simplistic, we can search for empty |
| 4578 | * extent records too. |
| 4579 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4580 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4581 | le16_to_cpu(el->l_next_free_rec); |
| 4582 | |
| 4583 | if (!insert->ins_tree_depth) { |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4584 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4585 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4586 | return 0; |
| 4587 | } |
| 4588 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4589 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4590 | if (!path) { |
| 4591 | ret = -ENOMEM; |
| 4592 | mlog_errno(ret); |
| 4593 | goto out; |
| 4594 | } |
| 4595 | |
| 4596 | /* |
| 4597 | * In the case that we're inserting past what the tree |
| 4598 | * currently accounts for, ocfs2_find_path() will return for |
| 4599 | * us the rightmost tree path. This is accounted for below in |
| 4600 | * the appending code. |
| 4601 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4602 | 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] | 4603 | if (ret) { |
| 4604 | mlog_errno(ret); |
| 4605 | goto out; |
| 4606 | } |
| 4607 | |
| 4608 | el = path_leaf_el(path); |
| 4609 | |
| 4610 | /* |
| 4611 | * Now that we have the path, there's two things we want to determine: |
| 4612 | * 1) Contiguousness (also set contig_index if this is so) |
| 4613 | * |
| 4614 | * 2) Are we doing an append? We can trivially break this up |
| 4615 | * into two types of appends: simple record append, or a |
| 4616 | * rotate inside the tail leaf. |
| 4617 | */ |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4618 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4619 | |
| 4620 | /* |
| 4621 | * The insert code isn't quite ready to deal with all cases of |
| 4622 | * left contiguousness. Specifically, if it's an insert into |
| 4623 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4624 | * 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] | 4625 | * left. For now, just catch that case and fool the layers |
| 4626 | * above us. This works just fine for tree_depth == 0, which |
| 4627 | * is why we allow that above. |
| 4628 | */ |
| 4629 | if (insert->ins_contig == CONTIG_LEFT && |
| 4630 | insert->ins_contig_index == 0) |
| 4631 | insert->ins_contig = CONTIG_NONE; |
| 4632 | |
| 4633 | /* |
| 4634 | * Ok, so we can simply compare against last_eb to figure out |
| 4635 | * whether the path doesn't exist. This will only happen in |
| 4636 | * the case that we're doing a tail append, so maybe we can |
| 4637 | * take advantage of that information somehow. |
| 4638 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4639 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4640 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4641 | /* |
| 4642 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4643 | * tree path. This might be an appending insert. There are |
| 4644 | * two cases: |
| 4645 | * 1) We're doing a true append at the tail: |
| 4646 | * -This might even be off the end of the leaf |
| 4647 | * 2) We're "appending" by rotating in the tail |
| 4648 | */ |
| 4649 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4650 | } |
| 4651 | |
| 4652 | out: |
| 4653 | ocfs2_free_path(path); |
| 4654 | |
| 4655 | if (ret == 0) |
| 4656 | *last_eb_bh = bh; |
| 4657 | else |
| 4658 | brelse(bh); |
| 4659 | return ret; |
| 4660 | } |
| 4661 | |
| 4662 | /* |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4663 | * Insert an extent into a btree. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4664 | * |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4665 | * The caller needs to update the owning btree's cluster count. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4666 | */ |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4667 | int ocfs2_insert_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4668 | struct ocfs2_extent_tree *et, |
| 4669 | u32 cpos, |
| 4670 | u64 start_blk, |
| 4671 | u32 new_clusters, |
| 4672 | u8 flags, |
| 4673 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4674 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4675 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4676 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4677 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4678 | struct ocfs2_insert_type insert = {0, }; |
| 4679 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4680 | |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4681 | mlog(0, "add %u clusters at position %u to owner %llu\n", |
| 4682 | new_clusters, cpos, |
| 4683 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4684 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4685 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4686 | rec.e_cpos = cpu_to_le32(cpos); |
| 4687 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4688 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4689 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4690 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4691 | if (status) { |
| 4692 | mlog_errno(status); |
| 4693 | goto bail; |
| 4694 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4695 | |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4696 | status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4697 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4698 | if (status < 0) { |
| 4699 | mlog_errno(status); |
| 4700 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4701 | } |
| 4702 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4703 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 4704 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 4705 | "Insert.tree_depth: %d\n", |
| 4706 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4707 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4708 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4709 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4710 | status = ocfs2_grow_tree(handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4711 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4712 | meta_ac); |
| 4713 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4714 | mlog_errno(status); |
| 4715 | goto bail; |
| 4716 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4717 | } |
| 4718 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4719 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4720 | status = ocfs2_do_insert_extent(handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4721 | if (status < 0) |
| 4722 | mlog_errno(status); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 4723 | else |
| 4724 | ocfs2_et_extent_map_insert(et, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4725 | |
| 4726 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4727 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4728 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4729 | mlog_exit(status); |
| 4730 | return status; |
| 4731 | } |
| 4732 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4733 | /* |
| 4734 | * Allcate and add clusters into the extent b-tree. |
| 4735 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4736 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4737 | * it is not limited to the file storage. Any extent tree can use this |
| 4738 | * function if it implements the proper ocfs2_extent_tree. |
| 4739 | */ |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4740 | int ocfs2_add_clusters_in_btree(handle_t *handle, |
| 4741 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4742 | u32 *logical_offset, |
| 4743 | u32 clusters_to_add, |
| 4744 | int mark_unwritten, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4745 | struct ocfs2_alloc_context *data_ac, |
| 4746 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4747 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4748 | { |
| 4749 | int status = 0; |
| 4750 | int free_extents; |
| 4751 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4752 | u32 bit_off, num_bits; |
| 4753 | u64 block; |
| 4754 | u8 flags = 0; |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4755 | struct ocfs2_super *osb = |
| 4756 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4757 | |
| 4758 | BUG_ON(!clusters_to_add); |
| 4759 | |
| 4760 | if (mark_unwritten) |
| 4761 | flags = OCFS2_EXT_UNWRITTEN; |
| 4762 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4763 | free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4764 | if (free_extents < 0) { |
| 4765 | status = free_extents; |
| 4766 | mlog_errno(status); |
| 4767 | goto leave; |
| 4768 | } |
| 4769 | |
| 4770 | /* there are two cases which could cause us to EAGAIN in the |
| 4771 | * we-need-more-metadata case: |
| 4772 | * 1) we haven't reserved *any* |
| 4773 | * 2) we are so fragmented, we've needed to add metadata too |
| 4774 | * many times. */ |
| 4775 | if (!free_extents && !meta_ac) { |
| 4776 | mlog(0, "we haven't reserved any metadata!\n"); |
| 4777 | status = -EAGAIN; |
| 4778 | reason = RESTART_META; |
| 4779 | goto leave; |
| 4780 | } else if ((!free_extents) |
| 4781 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4782 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4783 | mlog(0, "filesystem is really fragmented...\n"); |
| 4784 | status = -EAGAIN; |
| 4785 | reason = RESTART_META; |
| 4786 | goto leave; |
| 4787 | } |
| 4788 | |
| 4789 | status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 4790 | clusters_to_add, &bit_off, &num_bits); |
| 4791 | if (status < 0) { |
| 4792 | if (status != -ENOSPC) |
| 4793 | mlog_errno(status); |
| 4794 | goto leave; |
| 4795 | } |
| 4796 | |
| 4797 | BUG_ON(num_bits > clusters_to_add); |
| 4798 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4799 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4800 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4801 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4802 | if (status < 0) { |
| 4803 | mlog_errno(status); |
| 4804 | goto leave; |
| 4805 | } |
| 4806 | |
| 4807 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4808 | mlog(0, "Allocating %u clusters at block %u for owner %llu\n", |
| 4809 | num_bits, bit_off, |
| 4810 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4811 | status = ocfs2_insert_extent(handle, et, *logical_offset, block, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4812 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4813 | if (status < 0) { |
| 4814 | mlog_errno(status); |
| 4815 | goto leave; |
| 4816 | } |
| 4817 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4818 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4819 | |
| 4820 | clusters_to_add -= num_bits; |
| 4821 | *logical_offset += num_bits; |
| 4822 | |
| 4823 | if (clusters_to_add) { |
| 4824 | mlog(0, "need to alloc once more, wanted = %u\n", |
| 4825 | clusters_to_add); |
| 4826 | status = -EAGAIN; |
| 4827 | reason = RESTART_TRANS; |
| 4828 | } |
| 4829 | |
| 4830 | leave: |
| 4831 | mlog_exit(status); |
| 4832 | if (reason_ret) |
| 4833 | *reason_ret = reason; |
| 4834 | return status; |
| 4835 | } |
| 4836 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4837 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4838 | struct ocfs2_extent_rec *split_rec, |
| 4839 | u32 cpos, |
| 4840 | struct ocfs2_extent_rec *rec) |
| 4841 | { |
| 4842 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4843 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4844 | |
| 4845 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4846 | |
| 4847 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4848 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4849 | |
| 4850 | split_rec->e_blkno = rec->e_blkno; |
| 4851 | le64_add_cpu(&split_rec->e_blkno, |
| 4852 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4853 | |
| 4854 | split_rec->e_flags = rec->e_flags; |
| 4855 | } |
| 4856 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4857 | static int ocfs2_split_and_insert(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4858 | struct ocfs2_extent_tree *et, |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4859 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4860 | struct buffer_head **last_eb_bh, |
| 4861 | int split_index, |
| 4862 | struct ocfs2_extent_rec *orig_split_rec, |
| 4863 | struct ocfs2_alloc_context *meta_ac) |
| 4864 | { |
| 4865 | int ret = 0, depth; |
| 4866 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4867 | struct ocfs2_extent_rec tmprec; |
| 4868 | struct ocfs2_extent_list *rightmost_el; |
| 4869 | struct ocfs2_extent_rec rec; |
| 4870 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4871 | struct ocfs2_insert_type insert; |
| 4872 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4873 | |
| 4874 | leftright: |
| 4875 | /* |
| 4876 | * Store a copy of the record on the stack - it might move |
| 4877 | * around as the tree is manipulated below. |
| 4878 | */ |
| 4879 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4880 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4881 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4882 | |
| 4883 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4884 | if (depth) { |
| 4885 | BUG_ON(!(*last_eb_bh)); |
| 4886 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4887 | rightmost_el = &eb->h_list; |
| 4888 | } |
| 4889 | |
| 4890 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4891 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4892 | ret = ocfs2_grow_tree(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4893 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4894 | if (ret) { |
| 4895 | mlog_errno(ret); |
| 4896 | goto out; |
| 4897 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4901 | insert.ins_appending = APPEND_NONE; |
| 4902 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4903 | insert.ins_tree_depth = depth; |
| 4904 | |
| 4905 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 4906 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 4907 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 4908 | le16_to_cpu(rec.e_leaf_clusters); |
| 4909 | |
| 4910 | if (split_rec.e_cpos == rec.e_cpos) { |
| 4911 | insert.ins_split = SPLIT_LEFT; |
| 4912 | } else if (insert_range == rec_range) { |
| 4913 | insert.ins_split = SPLIT_RIGHT; |
| 4914 | } else { |
| 4915 | /* |
| 4916 | * Left/right split. We fake this as a right split |
| 4917 | * first and then make a second pass as a left split. |
| 4918 | */ |
| 4919 | insert.ins_split = SPLIT_RIGHT; |
| 4920 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4921 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4922 | &tmprec, insert_range, &rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4923 | |
| 4924 | split_rec = tmprec; |
| 4925 | |
| 4926 | BUG_ON(do_leftright); |
| 4927 | do_leftright = 1; |
| 4928 | } |
| 4929 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4930 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4931 | if (ret) { |
| 4932 | mlog_errno(ret); |
| 4933 | goto out; |
| 4934 | } |
| 4935 | |
| 4936 | if (do_leftright == 1) { |
| 4937 | u32 cpos; |
| 4938 | struct ocfs2_extent_list *el; |
| 4939 | |
| 4940 | do_leftright++; |
| 4941 | split_rec = *orig_split_rec; |
| 4942 | |
| 4943 | ocfs2_reinit_path(path, 1); |
| 4944 | |
| 4945 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4946 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4947 | if (ret) { |
| 4948 | mlog_errno(ret); |
| 4949 | goto out; |
| 4950 | } |
| 4951 | |
| 4952 | el = path_leaf_el(path); |
| 4953 | split_index = ocfs2_search_extent_list(el, cpos); |
| 4954 | goto leftright; |
| 4955 | } |
| 4956 | out: |
| 4957 | |
| 4958 | return ret; |
| 4959 | } |
| 4960 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 4961 | static int ocfs2_replace_extent_rec(handle_t *handle, |
| 4962 | struct ocfs2_extent_tree *et, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4963 | struct ocfs2_path *path, |
| 4964 | struct ocfs2_extent_list *el, |
| 4965 | int split_index, |
| 4966 | struct ocfs2_extent_rec *split_rec) |
| 4967 | { |
| 4968 | int ret; |
| 4969 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 4970 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 4971 | path_num_items(path) - 1); |
| 4972 | if (ret) { |
| 4973 | mlog_errno(ret); |
| 4974 | goto out; |
| 4975 | } |
| 4976 | |
| 4977 | el->l_recs[split_index] = *split_rec; |
| 4978 | |
| 4979 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 4980 | out: |
| 4981 | return ret; |
| 4982 | } |
| 4983 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4984 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 4985 | * Split part or all of the extent record at split_index in the leaf |
| 4986 | * pointed to by path. Merge with the contiguous extent record if needed. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4987 | * |
| 4988 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 4989 | * |
| 4990 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 4991 | * of the tree is required. All other cases will degrade into a less |
| 4992 | * optimal tree layout. |
| 4993 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4994 | * last_eb_bh should be the rightmost leaf block for any extent |
| 4995 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 4996 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4997 | * |
| 4998 | * This code is optimized for readability - several passes might be |
| 4999 | * made over certain portions of the tree. All of those blocks will |
| 5000 | * have been brought into cache (and pinned via the journal), so the |
| 5001 | * extra overhead is not expressed in terms of disk reads. |
| 5002 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 5003 | int ocfs2_split_extent(handle_t *handle, |
| 5004 | struct ocfs2_extent_tree *et, |
| 5005 | struct ocfs2_path *path, |
| 5006 | int split_index, |
| 5007 | struct ocfs2_extent_rec *split_rec, |
| 5008 | struct ocfs2_alloc_context *meta_ac, |
| 5009 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5010 | { |
| 5011 | int ret = 0; |
| 5012 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 5013 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5014 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 5015 | struct ocfs2_merge_ctxt ctxt; |
| 5016 | struct ocfs2_extent_list *rightmost_el; |
| 5017 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5018 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5019 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5020 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5021 | ret = -EIO; |
| 5022 | mlog_errno(ret); |
| 5023 | goto out; |
| 5024 | } |
| 5025 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 5026 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5027 | split_index, |
| 5028 | split_rec); |
| 5029 | |
| 5030 | /* |
| 5031 | * The core merge / split code wants to know how much room is |
Joel Becker | a1cf076 | 2009-02-13 03:45:49 -0800 | [diff] [blame] | 5032 | * left in this allocation tree, so we pass the |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5033 | * rightmost extent list. |
| 5034 | */ |
| 5035 | if (path->p_tree_depth) { |
| 5036 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5037 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5038 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5039 | ocfs2_et_get_last_eb_blk(et), |
| 5040 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5041 | if (ret) { |
| 5042 | mlog_exit(ret); |
| 5043 | goto out; |
| 5044 | } |
| 5045 | |
| 5046 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5047 | rightmost_el = &eb->h_list; |
| 5048 | } else |
| 5049 | rightmost_el = path_root_el(path); |
| 5050 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5051 | if (rec->e_cpos == split_rec->e_cpos && |
| 5052 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5053 | ctxt.c_split_covers_rec = 1; |
| 5054 | else |
| 5055 | ctxt.c_split_covers_rec = 0; |
| 5056 | |
| 5057 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5058 | |
Mark Fasheh | 015452b | 2007-09-12 10:21:22 -0700 | [diff] [blame] | 5059 | mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", |
| 5060 | split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, |
| 5061 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5062 | |
| 5063 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5064 | if (ctxt.c_split_covers_rec) |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5065 | ret = ocfs2_replace_extent_rec(handle, et, path, el, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5066 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5067 | else |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 5068 | ret = ocfs2_split_and_insert(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5069 | &last_eb_bh, split_index, |
| 5070 | split_rec, meta_ac); |
| 5071 | if (ret) |
| 5072 | mlog_errno(ret); |
| 5073 | } else { |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5074 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5075 | split_index, split_rec, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5076 | dealloc, &ctxt); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5077 | if (ret) |
| 5078 | mlog_errno(ret); |
| 5079 | } |
| 5080 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5081 | out: |
| 5082 | brelse(last_eb_bh); |
| 5083 | return ret; |
| 5084 | } |
| 5085 | |
| 5086 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5087 | * Change the flags of the already-existing extent at cpos for len clusters. |
| 5088 | * |
| 5089 | * new_flags: the flags we want to set. |
| 5090 | * clear_flags: the flags we want to clear. |
| 5091 | * phys: the new physical offset we want this new extent starts from. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5092 | * |
| 5093 | * If the existing extent is larger than the request, initiate a |
| 5094 | * split. An attempt will be made at merging with adjacent extents. |
| 5095 | * |
| 5096 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5097 | */ |
Tao Ma | 1aa75fe | 2009-08-18 11:28:39 +0800 | [diff] [blame] | 5098 | int ocfs2_change_extent_flag(handle_t *handle, |
| 5099 | struct ocfs2_extent_tree *et, |
| 5100 | u32 cpos, u32 len, u32 phys, |
| 5101 | struct ocfs2_alloc_context *meta_ac, |
| 5102 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 5103 | int new_flags, int clear_flags) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5104 | { |
| 5105 | int ret, index; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5106 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
| 5107 | u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5108 | struct ocfs2_extent_rec split_rec; |
| 5109 | struct ocfs2_path *left_path = NULL; |
| 5110 | struct ocfs2_extent_list *el; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5111 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5112 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5113 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5114 | if (!left_path) { |
| 5115 | ret = -ENOMEM; |
| 5116 | mlog_errno(ret); |
| 5117 | goto out; |
| 5118 | } |
| 5119 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5120 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5121 | if (ret) { |
| 5122 | mlog_errno(ret); |
| 5123 | goto out; |
| 5124 | } |
| 5125 | el = path_leaf_el(left_path); |
| 5126 | |
| 5127 | index = ocfs2_search_extent_list(el, cpos); |
| 5128 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5129 | ocfs2_error(sb, |
| 5130 | "Owner %llu has an extent at cpos %u which can no " |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5131 | "longer be found.\n", |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5132 | (unsigned long long) |
| 5133 | ocfs2_metadata_cache_owner(et->et_ci), cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5134 | ret = -EROFS; |
| 5135 | goto out; |
| 5136 | } |
| 5137 | |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5138 | ret = -EIO; |
| 5139 | rec = &el->l_recs[index]; |
| 5140 | if (new_flags && (rec->e_flags & new_flags)) { |
| 5141 | mlog(ML_ERROR, "Owner %llu tried to set %d flags on an " |
| 5142 | "extent that already had them", |
| 5143 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5144 | new_flags); |
| 5145 | goto out; |
| 5146 | } |
| 5147 | |
| 5148 | if (clear_flags && !(rec->e_flags & clear_flags)) { |
| 5149 | mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an " |
| 5150 | "extent that didn't have them", |
| 5151 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5152 | clear_flags); |
| 5153 | goto out; |
| 5154 | } |
| 5155 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5156 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5157 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5158 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5159 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5160 | split_rec.e_flags = rec->e_flags; |
| 5161 | if (new_flags) |
| 5162 | split_rec.e_flags |= new_flags; |
| 5163 | if (clear_flags) |
| 5164 | split_rec.e_flags &= ~clear_flags; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5165 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 5166 | ret = ocfs2_split_extent(handle, et, left_path, |
| 5167 | index, &split_rec, meta_ac, |
| 5168 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5169 | if (ret) |
| 5170 | mlog_errno(ret); |
| 5171 | |
| 5172 | out: |
| 5173 | ocfs2_free_path(left_path); |
| 5174 | return ret; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5175 | |
| 5176 | } |
| 5177 | |
| 5178 | /* |
| 5179 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5180 | * This removes the unwritten extent flag. |
| 5181 | * |
| 5182 | * If the existing extent is larger than the request, initiate a |
| 5183 | * split. An attempt will be made at merging with adjacent extents. |
| 5184 | * |
| 5185 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5186 | */ |
| 5187 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5188 | struct ocfs2_extent_tree *et, |
| 5189 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5190 | struct ocfs2_alloc_context *meta_ac, |
| 5191 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5192 | { |
| 5193 | int ret; |
| 5194 | |
| 5195 | mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n", |
| 5196 | inode->i_ino, cpos, len, phys); |
| 5197 | |
| 5198 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 5199 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 5200 | "that are being written to, but the feature bit " |
| 5201 | "is not set in the super block.", |
| 5202 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5203 | ret = -EROFS; |
| 5204 | goto out; |
| 5205 | } |
| 5206 | |
| 5207 | /* |
| 5208 | * XXX: This should be fixed up so that we just re-insert the |
| 5209 | * next extent records. |
| 5210 | */ |
| 5211 | ocfs2_et_extent_map_truncate(et, 0); |
| 5212 | |
| 5213 | ret = ocfs2_change_extent_flag(handle, et, cpos, |
| 5214 | len, phys, meta_ac, dealloc, |
| 5215 | 0, OCFS2_EXT_UNWRITTEN); |
| 5216 | if (ret) |
| 5217 | mlog_errno(ret); |
| 5218 | |
| 5219 | out: |
| 5220 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5221 | } |
| 5222 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5223 | static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 5224 | struct ocfs2_path *path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5225 | int index, u32 new_range, |
| 5226 | struct ocfs2_alloc_context *meta_ac) |
| 5227 | { |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 5228 | int ret, depth, credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5229 | struct buffer_head *last_eb_bh = NULL; |
| 5230 | struct ocfs2_extent_block *eb; |
| 5231 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5232 | struct ocfs2_extent_rec split_rec; |
| 5233 | struct ocfs2_extent_rec *rec; |
| 5234 | struct ocfs2_insert_type insert; |
| 5235 | |
| 5236 | /* |
| 5237 | * Setup the record to split before we grow the tree. |
| 5238 | */ |
| 5239 | el = path_leaf_el(path); |
| 5240 | rec = &el->l_recs[index]; |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5241 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5242 | &split_rec, new_range, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5243 | |
| 5244 | depth = path->p_tree_depth; |
| 5245 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5246 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5247 | ocfs2_et_get_last_eb_blk(et), |
| 5248 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5249 | if (ret < 0) { |
| 5250 | mlog_errno(ret); |
| 5251 | goto out; |
| 5252 | } |
| 5253 | |
| 5254 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5255 | rightmost_el = &eb->h_list; |
| 5256 | } else |
| 5257 | rightmost_el = path_leaf_el(path); |
| 5258 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame^] | 5259 | credits = path->p_tree_depth + |
| 5260 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5261 | ret = ocfs2_extend_trans(handle, credits); |
| 5262 | if (ret) { |
| 5263 | mlog_errno(ret); |
| 5264 | goto out; |
| 5265 | } |
| 5266 | |
| 5267 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5268 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5269 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5270 | meta_ac); |
| 5271 | if (ret) { |
| 5272 | mlog_errno(ret); |
| 5273 | goto out; |
| 5274 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5278 | insert.ins_appending = APPEND_NONE; |
| 5279 | insert.ins_contig = CONTIG_NONE; |
| 5280 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5281 | insert.ins_tree_depth = depth; |
| 5282 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5283 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5284 | if (ret) |
| 5285 | mlog_errno(ret); |
| 5286 | |
| 5287 | out: |
| 5288 | brelse(last_eb_bh); |
| 5289 | return ret; |
| 5290 | } |
| 5291 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5292 | static int ocfs2_truncate_rec(handle_t *handle, |
| 5293 | struct ocfs2_extent_tree *et, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5294 | struct ocfs2_path *path, int index, |
| 5295 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5296 | u32 cpos, u32 len) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5297 | { |
| 5298 | int ret; |
| 5299 | u32 left_cpos, rec_range, trunc_range; |
| 5300 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5301 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5302 | struct ocfs2_path *left_path = NULL; |
| 5303 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5304 | struct ocfs2_extent_rec *rec; |
| 5305 | struct ocfs2_extent_block *eb; |
| 5306 | |
| 5307 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5308 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5309 | if (ret) { |
| 5310 | mlog_errno(ret); |
| 5311 | goto out; |
| 5312 | } |
| 5313 | |
| 5314 | index--; |
| 5315 | } |
| 5316 | |
| 5317 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5318 | path->p_tree_depth) { |
| 5319 | /* |
| 5320 | * Check whether this is the rightmost tree record. If |
| 5321 | * we remove all of this record or part of its right |
| 5322 | * edge then an update of the record lengths above it |
| 5323 | * will be required. |
| 5324 | */ |
| 5325 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5326 | if (eb->h_next_leaf_blk == 0) |
| 5327 | is_rightmost_tree_rec = 1; |
| 5328 | } |
| 5329 | |
| 5330 | rec = &el->l_recs[index]; |
| 5331 | if (index == 0 && path->p_tree_depth && |
| 5332 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5333 | /* |
| 5334 | * Changing the leftmost offset (via partial or whole |
| 5335 | * record truncate) of an interior (or rightmost) path |
| 5336 | * means we have to update the subtree that is formed |
| 5337 | * by this leaf and the one to it's left. |
| 5338 | * |
| 5339 | * There are two cases we can skip: |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5340 | * 1) Path is the leftmost one in our btree. |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5341 | * 2) The leaf is rightmost and will be empty after |
| 5342 | * we remove the extent record - the rotate code |
| 5343 | * knows how to update the newly formed edge. |
| 5344 | */ |
| 5345 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5346 | ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5347 | if (ret) { |
| 5348 | mlog_errno(ret); |
| 5349 | goto out; |
| 5350 | } |
| 5351 | |
| 5352 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5353 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5354 | if (!left_path) { |
| 5355 | ret = -ENOMEM; |
| 5356 | mlog_errno(ret); |
| 5357 | goto out; |
| 5358 | } |
| 5359 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5360 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5361 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5362 | if (ret) { |
| 5363 | mlog_errno(ret); |
| 5364 | goto out; |
| 5365 | } |
| 5366 | } |
| 5367 | } |
| 5368 | |
| 5369 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5370 | handle->h_buffer_credits, |
| 5371 | path); |
| 5372 | if (ret) { |
| 5373 | mlog_errno(ret); |
| 5374 | goto out; |
| 5375 | } |
| 5376 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5377 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5378 | if (ret) { |
| 5379 | mlog_errno(ret); |
| 5380 | goto out; |
| 5381 | } |
| 5382 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5383 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
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 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5390 | trunc_range = cpos + len; |
| 5391 | |
| 5392 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5393 | int next_free; |
| 5394 | |
| 5395 | memset(rec, 0, sizeof(*rec)); |
| 5396 | ocfs2_cleanup_merge(el, index); |
| 5397 | wants_rotate = 1; |
| 5398 | |
| 5399 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5400 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5401 | /* |
| 5402 | * We skip the edge update if this path will |
| 5403 | * be deleted by the rotate code. |
| 5404 | */ |
| 5405 | rec = &el->l_recs[next_free - 1]; |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5406 | ocfs2_adjust_rightmost_records(handle, et, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5407 | rec); |
| 5408 | } |
| 5409 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5410 | /* Remove leftmost portion of the record. */ |
| 5411 | le32_add_cpu(&rec->e_cpos, len); |
| 5412 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5413 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5414 | } else if (rec_range == trunc_range) { |
| 5415 | /* Remove rightmost portion of the record */ |
| 5416 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5417 | if (is_rightmost_tree_rec) |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5418 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5419 | } else { |
| 5420 | /* Caller should have trapped this. */ |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5421 | mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) " |
| 5422 | "(%u, %u)\n", |
| 5423 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5424 | le32_to_cpu(rec->e_cpos), |
| 5425 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5426 | BUG(); |
| 5427 | } |
| 5428 | |
| 5429 | if (left_path) { |
| 5430 | int subtree_index; |
| 5431 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5432 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5433 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5434 | subtree_index); |
| 5435 | } |
| 5436 | |
| 5437 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5438 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5439 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5440 | if (ret) { |
| 5441 | mlog_errno(ret); |
| 5442 | goto out; |
| 5443 | } |
| 5444 | |
| 5445 | out: |
| 5446 | ocfs2_free_path(left_path); |
| 5447 | return ret; |
| 5448 | } |
| 5449 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5450 | int ocfs2_remove_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5451 | struct ocfs2_extent_tree *et, |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5452 | u32 cpos, u32 len, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5453 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5454 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5455 | { |
| 5456 | int ret, index; |
| 5457 | u32 rec_range, trunc_range; |
| 5458 | struct ocfs2_extent_rec *rec; |
| 5459 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5460 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5461 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 5462 | /* |
| 5463 | * XXX: Why are we truncating to 0 instead of wherever this |
| 5464 | * affects us? |
| 5465 | */ |
| 5466 | ocfs2_et_extent_map_truncate(et, 0); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5467 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5468 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5469 | if (!path) { |
| 5470 | ret = -ENOMEM; |
| 5471 | mlog_errno(ret); |
| 5472 | goto out; |
| 5473 | } |
| 5474 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5475 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5476 | if (ret) { |
| 5477 | mlog_errno(ret); |
| 5478 | goto out; |
| 5479 | } |
| 5480 | |
| 5481 | el = path_leaf_el(path); |
| 5482 | index = ocfs2_search_extent_list(el, cpos); |
| 5483 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5484 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5485 | "Owner %llu has an extent at cpos %u which can no " |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5486 | "longer be found.\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5487 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5488 | cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5489 | ret = -EROFS; |
| 5490 | goto out; |
| 5491 | } |
| 5492 | |
| 5493 | /* |
| 5494 | * We have 3 cases of extent removal: |
| 5495 | * 1) Range covers the entire extent rec |
| 5496 | * 2) Range begins or ends on one edge of the extent rec |
| 5497 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5498 | * |
| 5499 | * For case 1 we remove the extent rec and left rotate to |
| 5500 | * fill the hole. |
| 5501 | * |
| 5502 | * For case 2 we just shrink the existing extent rec, with a |
| 5503 | * tree update if the shrinking edge is also the edge of an |
| 5504 | * extent block. |
| 5505 | * |
| 5506 | * For case 3 we do a right split to turn the extent rec into |
| 5507 | * something case 2 can handle. |
| 5508 | */ |
| 5509 | rec = &el->l_recs[index]; |
| 5510 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5511 | trunc_range = cpos + len; |
| 5512 | |
| 5513 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5514 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5515 | mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d " |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5516 | "(cpos %u, len %u)\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5517 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5518 | cpos, len, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5519 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 5520 | |
| 5521 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5522 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5523 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5524 | if (ret) { |
| 5525 | mlog_errno(ret); |
| 5526 | goto out; |
| 5527 | } |
| 5528 | } else { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5529 | ret = ocfs2_split_tree(handle, et, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5530 | trunc_range, meta_ac); |
| 5531 | if (ret) { |
| 5532 | mlog_errno(ret); |
| 5533 | goto out; |
| 5534 | } |
| 5535 | |
| 5536 | /* |
| 5537 | * The split could have manipulated the tree enough to |
| 5538 | * move the record location, so we have to look for it again. |
| 5539 | */ |
| 5540 | ocfs2_reinit_path(path, 1); |
| 5541 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5542 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5543 | if (ret) { |
| 5544 | mlog_errno(ret); |
| 5545 | goto out; |
| 5546 | } |
| 5547 | |
| 5548 | el = path_leaf_el(path); |
| 5549 | index = ocfs2_search_extent_list(el, cpos); |
| 5550 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5551 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5552 | "Owner %llu: split at cpos %u lost record.", |
| 5553 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5554 | cpos); |
| 5555 | ret = -EROFS; |
| 5556 | goto out; |
| 5557 | } |
| 5558 | |
| 5559 | /* |
| 5560 | * Double check our values here. If anything is fishy, |
| 5561 | * it's easier to catch it at the top level. |
| 5562 | */ |
| 5563 | rec = &el->l_recs[index]; |
| 5564 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5565 | ocfs2_rec_clusters(el, rec); |
| 5566 | if (rec_range != trunc_range) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5567 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5568 | "Owner %llu: error after split at cpos %u" |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5569 | "trunc len %u, existing record is (%u,%u)", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5570 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5571 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5572 | ocfs2_rec_clusters(el, rec)); |
| 5573 | ret = -EROFS; |
| 5574 | goto out; |
| 5575 | } |
| 5576 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5577 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5578 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5579 | if (ret) { |
| 5580 | mlog_errno(ret); |
| 5581 | goto out; |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | out: |
| 5586 | ocfs2_free_path(path); |
| 5587 | return ret; |
| 5588 | } |
| 5589 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5590 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5591 | struct ocfs2_extent_tree *et, |
| 5592 | u32 cpos, u32 phys_cpos, u32 len, |
| 5593 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5594 | { |
| 5595 | int ret; |
| 5596 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5597 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5598 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5599 | handle_t *handle; |
| 5600 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5601 | |
| 5602 | ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); |
| 5603 | if (ret) { |
| 5604 | mlog_errno(ret); |
| 5605 | return ret; |
| 5606 | } |
| 5607 | |
| 5608 | mutex_lock(&tl_inode->i_mutex); |
| 5609 | |
| 5610 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5611 | ret = __ocfs2_flush_truncate_log(osb); |
| 5612 | if (ret < 0) { |
| 5613 | mlog_errno(ret); |
| 5614 | goto out; |
| 5615 | } |
| 5616 | } |
| 5617 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5618 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5619 | if (IS_ERR(handle)) { |
| 5620 | ret = PTR_ERR(handle); |
| 5621 | mlog_errno(ret); |
| 5622 | goto out; |
| 5623 | } |
| 5624 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5625 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5626 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5627 | if (ret) { |
| 5628 | mlog_errno(ret); |
| 5629 | goto out; |
| 5630 | } |
| 5631 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 5632 | dquot_free_space_nodirty(inode, |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5633 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5634 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5635 | ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5636 | if (ret) { |
| 5637 | mlog_errno(ret); |
| 5638 | goto out_commit; |
| 5639 | } |
| 5640 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5641 | ocfs2_et_update_clusters(et, -len); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5642 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 5643 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5644 | |
| 5645 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 5646 | if (ret) |
| 5647 | mlog_errno(ret); |
| 5648 | |
| 5649 | out_commit: |
| 5650 | ocfs2_commit_trans(osb, handle); |
| 5651 | out: |
| 5652 | mutex_unlock(&tl_inode->i_mutex); |
| 5653 | |
| 5654 | if (meta_ac) |
| 5655 | ocfs2_free_alloc_context(meta_ac); |
| 5656 | |
| 5657 | return ret; |
| 5658 | } |
| 5659 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5660 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5661 | { |
| 5662 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5663 | struct ocfs2_dinode *di; |
| 5664 | struct ocfs2_truncate_log *tl; |
| 5665 | |
| 5666 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5667 | tl = &di->id2.i_dealloc; |
| 5668 | |
| 5669 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5670 | "slot %d, invalid truncate log parameters: used = " |
| 5671 | "%u, count = %u\n", osb->slot_num, |
| 5672 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5673 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5674 | } |
| 5675 | |
| 5676 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5677 | unsigned int new_start) |
| 5678 | { |
| 5679 | unsigned int tail_index; |
| 5680 | unsigned int current_tail; |
| 5681 | |
| 5682 | /* No records, nothing to coalesce */ |
| 5683 | if (!le16_to_cpu(tl->tl_used)) |
| 5684 | return 0; |
| 5685 | |
| 5686 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5687 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5688 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5689 | |
| 5690 | return current_tail == new_start; |
| 5691 | } |
| 5692 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5693 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5694 | handle_t *handle, |
| 5695 | u64 start_blk, |
| 5696 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5697 | { |
| 5698 | int status, index; |
| 5699 | unsigned int start_cluster, tl_count; |
| 5700 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5701 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5702 | struct ocfs2_dinode *di; |
| 5703 | struct ocfs2_truncate_log *tl; |
| 5704 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5705 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 5706 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5707 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5708 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5709 | |
| 5710 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5711 | |
| 5712 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5713 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5714 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5715 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5716 | * corruption is a code bug */ |
| 5717 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5718 | |
| 5719 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5720 | tl_count = le16_to_cpu(tl->tl_count); |
| 5721 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5722 | tl_count == 0, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5723 | "Truncate record count on #%llu invalid " |
| 5724 | "wanted %u, actual %u\n", |
| 5725 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5726 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5727 | le16_to_cpu(tl->tl_count)); |
| 5728 | |
| 5729 | /* Caller should have known to flush before calling us. */ |
| 5730 | index = le16_to_cpu(tl->tl_used); |
| 5731 | if (index >= tl_count) { |
| 5732 | status = -ENOSPC; |
| 5733 | mlog_errno(status); |
| 5734 | goto bail; |
| 5735 | } |
| 5736 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5737 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5738 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5739 | if (status < 0) { |
| 5740 | mlog_errno(status); |
| 5741 | goto bail; |
| 5742 | } |
| 5743 | |
| 5744 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5745 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 5746 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5747 | |
| 5748 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5749 | /* |
| 5750 | * Move index back to the record we are coalescing with. |
| 5751 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5752 | */ |
| 5753 | index--; |
| 5754 | |
| 5755 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 5756 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 5757 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5758 | num_clusters); |
| 5759 | } else { |
| 5760 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5761 | tl->tl_used = cpu_to_le16(index + 1); |
| 5762 | } |
| 5763 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5764 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 5765 | ocfs2_journal_dirty(handle, tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5766 | |
| 5767 | bail: |
| 5768 | mlog_exit(status); |
| 5769 | return status; |
| 5770 | } |
| 5771 | |
| 5772 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5773 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5774 | struct inode *data_alloc_inode, |
| 5775 | struct buffer_head *data_alloc_bh) |
| 5776 | { |
| 5777 | int status = 0; |
| 5778 | int i; |
| 5779 | unsigned int num_clusters; |
| 5780 | u64 start_blk; |
| 5781 | struct ocfs2_truncate_rec rec; |
| 5782 | struct ocfs2_dinode *di; |
| 5783 | struct ocfs2_truncate_log *tl; |
| 5784 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5785 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5786 | |
| 5787 | mlog_entry_void(); |
| 5788 | |
| 5789 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5790 | tl = &di->id2.i_dealloc; |
| 5791 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5792 | while (i >= 0) { |
| 5793 | /* Caller has given us at least enough credits to |
| 5794 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5795 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5796 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5797 | if (status < 0) { |
| 5798 | mlog_errno(status); |
| 5799 | goto bail; |
| 5800 | } |
| 5801 | |
| 5802 | tl->tl_used = cpu_to_le16(i); |
| 5803 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 5804 | ocfs2_journal_dirty(handle, tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5805 | |
| 5806 | /* TODO: Perhaps we can calculate the bulk of the |
| 5807 | * credits up front rather than extending like |
| 5808 | * this. */ |
| 5809 | status = ocfs2_extend_trans(handle, |
| 5810 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5811 | if (status < 0) { |
| 5812 | mlog_errno(status); |
| 5813 | goto bail; |
| 5814 | } |
| 5815 | |
| 5816 | rec = tl->tl_recs[i]; |
| 5817 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5818 | le32_to_cpu(rec.t_start)); |
| 5819 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5820 | |
| 5821 | /* if start_blk is not set, we ignore the record as |
| 5822 | * invalid. */ |
| 5823 | if (start_blk) { |
| 5824 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 5825 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 5826 | |
| 5827 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5828 | data_alloc_bh, start_blk, |
| 5829 | num_clusters); |
| 5830 | if (status < 0) { |
| 5831 | mlog_errno(status); |
| 5832 | goto bail; |
| 5833 | } |
| 5834 | } |
| 5835 | i--; |
| 5836 | } |
| 5837 | |
| 5838 | bail: |
| 5839 | mlog_exit(status); |
| 5840 | return status; |
| 5841 | } |
| 5842 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5843 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5844 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5845 | { |
| 5846 | int status; |
| 5847 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5848 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5849 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5850 | struct inode *data_alloc_inode = NULL; |
| 5851 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5852 | struct buffer_head *data_alloc_bh = NULL; |
| 5853 | struct ocfs2_dinode *di; |
| 5854 | struct ocfs2_truncate_log *tl; |
| 5855 | |
| 5856 | mlog_entry_void(); |
| 5857 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5858 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5859 | |
| 5860 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5861 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5862 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5863 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5864 | * corruption is a code bug */ |
| 5865 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5866 | |
| 5867 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5868 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5869 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 5870 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5871 | if (!num_to_flush) { |
| 5872 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5873 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 5877 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 5878 | OCFS2_INVALID_SLOT); |
| 5879 | if (!data_alloc_inode) { |
| 5880 | status = -EINVAL; |
| 5881 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5882 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5883 | } |
| 5884 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5885 | mutex_lock(&data_alloc_inode->i_mutex); |
| 5886 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5887 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5888 | if (status < 0) { |
| 5889 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5890 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5891 | } |
| 5892 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 5893 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5894 | if (IS_ERR(handle)) { |
| 5895 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5896 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5897 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 5901 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5902 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5903 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5904 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5905 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5906 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5907 | out_unlock: |
| 5908 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 5909 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5910 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5911 | out_mutex: |
| 5912 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 5913 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5914 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 5915 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5916 | mlog_exit(status); |
| 5917 | return status; |
| 5918 | } |
| 5919 | |
| 5920 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 5921 | { |
| 5922 | int status; |
| 5923 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5924 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5925 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5926 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5927 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5928 | |
| 5929 | return status; |
| 5930 | } |
| 5931 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5932 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5933 | { |
| 5934 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 5935 | struct ocfs2_super *osb = |
| 5936 | container_of(work, struct ocfs2_super, |
| 5937 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5938 | |
| 5939 | mlog_entry_void(); |
| 5940 | |
| 5941 | status = ocfs2_flush_truncate_log(osb); |
| 5942 | if (status < 0) |
| 5943 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 5944 | else |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 5945 | ocfs2_init_steal_slots(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5946 | |
| 5947 | mlog_exit(status); |
| 5948 | } |
| 5949 | |
| 5950 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 5951 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 5952 | int cancel) |
| 5953 | { |
| 5954 | if (osb->osb_tl_inode) { |
| 5955 | /* We want to push off log flushes while truncates are |
| 5956 | * still running. */ |
| 5957 | if (cancel) |
| 5958 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 5959 | |
| 5960 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 5961 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 5966 | int slot_num, |
| 5967 | struct inode **tl_inode, |
| 5968 | struct buffer_head **tl_bh) |
| 5969 | { |
| 5970 | int status; |
| 5971 | struct inode *inode = NULL; |
| 5972 | struct buffer_head *bh = NULL; |
| 5973 | |
| 5974 | inode = ocfs2_get_system_file_inode(osb, |
| 5975 | TRUNCATE_LOG_SYSTEM_INODE, |
| 5976 | slot_num); |
| 5977 | if (!inode) { |
| 5978 | status = -EINVAL; |
| 5979 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 5980 | goto bail; |
| 5981 | } |
| 5982 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 5983 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5984 | if (status < 0) { |
| 5985 | iput(inode); |
| 5986 | mlog_errno(status); |
| 5987 | goto bail; |
| 5988 | } |
| 5989 | |
| 5990 | *tl_inode = inode; |
| 5991 | *tl_bh = bh; |
| 5992 | bail: |
| 5993 | mlog_exit(status); |
| 5994 | return status; |
| 5995 | } |
| 5996 | |
| 5997 | /* called during the 1st stage of node recovery. we stamp a clean |
| 5998 | * truncate log and pass back a copy for processing later. if the |
| 5999 | * truncate log does not require processing, a *tl_copy is set to |
| 6000 | * NULL. */ |
| 6001 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 6002 | int slot_num, |
| 6003 | struct ocfs2_dinode **tl_copy) |
| 6004 | { |
| 6005 | int status; |
| 6006 | struct inode *tl_inode = NULL; |
| 6007 | struct buffer_head *tl_bh = NULL; |
| 6008 | struct ocfs2_dinode *di; |
| 6009 | struct ocfs2_truncate_log *tl; |
| 6010 | |
| 6011 | *tl_copy = NULL; |
| 6012 | |
| 6013 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 6014 | |
| 6015 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 6016 | if (status < 0) { |
| 6017 | mlog_errno(status); |
| 6018 | goto bail; |
| 6019 | } |
| 6020 | |
| 6021 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6022 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 6023 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 6024 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 6025 | * so any corruption is a code bug */ |
| 6026 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 6027 | |
| 6028 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6029 | if (le16_to_cpu(tl->tl_used)) { |
| 6030 | mlog(0, "We'll have %u logs to recover\n", |
| 6031 | le16_to_cpu(tl->tl_used)); |
| 6032 | |
| 6033 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 6034 | if (!(*tl_copy)) { |
| 6035 | status = -ENOMEM; |
| 6036 | mlog_errno(status); |
| 6037 | goto bail; |
| 6038 | } |
| 6039 | |
| 6040 | /* Assuming the write-out below goes well, this copy |
| 6041 | * will be passed back to recovery for processing. */ |
| 6042 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 6043 | |
| 6044 | /* All we need to do to clear the truncate log is set |
| 6045 | * tl_used. */ |
| 6046 | tl->tl_used = 0; |
| 6047 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6048 | 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] | 6049 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6050 | if (status < 0) { |
| 6051 | mlog_errno(status); |
| 6052 | goto bail; |
| 6053 | } |
| 6054 | } |
| 6055 | |
| 6056 | bail: |
| 6057 | if (tl_inode) |
| 6058 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6059 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6060 | |
| 6061 | if (status < 0 && (*tl_copy)) { |
| 6062 | kfree(*tl_copy); |
| 6063 | *tl_copy = NULL; |
| 6064 | } |
| 6065 | |
| 6066 | mlog_exit(status); |
| 6067 | return status; |
| 6068 | } |
| 6069 | |
| 6070 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6071 | struct ocfs2_dinode *tl_copy) |
| 6072 | { |
| 6073 | int status = 0; |
| 6074 | int i; |
| 6075 | unsigned int clusters, num_recs, start_cluster; |
| 6076 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6077 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6078 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6079 | struct ocfs2_truncate_log *tl; |
| 6080 | |
| 6081 | mlog_entry_void(); |
| 6082 | |
| 6083 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6084 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6085 | return -EINVAL; |
| 6086 | } |
| 6087 | |
| 6088 | tl = &tl_copy->id2.i_dealloc; |
| 6089 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 6090 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6091 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6092 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6093 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6094 | for(i = 0; i < num_recs; i++) { |
| 6095 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6096 | status = __ocfs2_flush_truncate_log(osb); |
| 6097 | if (status < 0) { |
| 6098 | mlog_errno(status); |
| 6099 | goto bail_up; |
| 6100 | } |
| 6101 | } |
| 6102 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6103 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6104 | if (IS_ERR(handle)) { |
| 6105 | status = PTR_ERR(handle); |
| 6106 | mlog_errno(status); |
| 6107 | goto bail_up; |
| 6108 | } |
| 6109 | |
| 6110 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6111 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6112 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6113 | |
| 6114 | status = ocfs2_truncate_log_append(osb, handle, |
| 6115 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6116 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6117 | if (status < 0) { |
| 6118 | mlog_errno(status); |
| 6119 | goto bail_up; |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 6124 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6125 | |
| 6126 | mlog_exit(status); |
| 6127 | return status; |
| 6128 | } |
| 6129 | |
| 6130 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6131 | { |
| 6132 | int status; |
| 6133 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6134 | |
| 6135 | mlog_entry_void(); |
| 6136 | |
| 6137 | if (tl_inode) { |
| 6138 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6139 | flush_workqueue(ocfs2_wq); |
| 6140 | |
| 6141 | status = ocfs2_flush_truncate_log(osb); |
| 6142 | if (status < 0) |
| 6143 | mlog_errno(status); |
| 6144 | |
| 6145 | brelse(osb->osb_tl_bh); |
| 6146 | iput(osb->osb_tl_inode); |
| 6147 | } |
| 6148 | |
| 6149 | mlog_exit_void(); |
| 6150 | } |
| 6151 | |
| 6152 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6153 | { |
| 6154 | int status; |
| 6155 | struct inode *tl_inode = NULL; |
| 6156 | struct buffer_head *tl_bh = NULL; |
| 6157 | |
| 6158 | mlog_entry_void(); |
| 6159 | |
| 6160 | status = ocfs2_get_truncate_log_info(osb, |
| 6161 | osb->slot_num, |
| 6162 | &tl_inode, |
| 6163 | &tl_bh); |
| 6164 | if (status < 0) |
| 6165 | mlog_errno(status); |
| 6166 | |
| 6167 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6168 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6169 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6170 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6171 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6172 | osb->osb_tl_bh = tl_bh; |
| 6173 | osb->osb_tl_inode = tl_inode; |
| 6174 | |
| 6175 | mlog_exit(status); |
| 6176 | return status; |
| 6177 | } |
| 6178 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6179 | /* |
| 6180 | * Delayed de-allocation of suballocator blocks. |
| 6181 | * |
| 6182 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6183 | * |
| 6184 | * The locking for this can get extremely complicated, especially when |
| 6185 | * the suballocator inodes to delete from aren't known until deep |
| 6186 | * within an unrelated codepath. |
| 6187 | * |
| 6188 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6189 | * btree could have been grown by any number of nodes each allocating |
| 6190 | * out of their own suballoc inode. |
| 6191 | * |
| 6192 | * These structures allow the delay of block de-allocation until a |
| 6193 | * later time, when locking of multiple cluster inodes won't cause |
| 6194 | * deadlock. |
| 6195 | */ |
| 6196 | |
| 6197 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6198 | * Describe a single bit freed from a suballocator. For the block |
| 6199 | * suballocators, it represents one block. For the global cluster |
| 6200 | * allocator, it represents some clusters and free_bit indicates |
| 6201 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6202 | */ |
| 6203 | struct ocfs2_cached_block_free { |
| 6204 | struct ocfs2_cached_block_free *free_next; |
| 6205 | u64 free_blk; |
| 6206 | unsigned int free_bit; |
| 6207 | }; |
| 6208 | |
| 6209 | struct ocfs2_per_slot_free_list { |
| 6210 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6211 | int f_inode_type; |
| 6212 | int f_slot; |
| 6213 | struct ocfs2_cached_block_free *f_first; |
| 6214 | }; |
| 6215 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6216 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6217 | int sysfile_type, |
| 6218 | int slot, |
| 6219 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6220 | { |
| 6221 | int ret; |
| 6222 | u64 bg_blkno; |
| 6223 | handle_t *handle; |
| 6224 | struct inode *inode; |
| 6225 | struct buffer_head *di_bh = NULL; |
| 6226 | struct ocfs2_cached_block_free *tmp; |
| 6227 | |
| 6228 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6229 | if (!inode) { |
| 6230 | ret = -EINVAL; |
| 6231 | mlog_errno(ret); |
| 6232 | goto out; |
| 6233 | } |
| 6234 | |
| 6235 | mutex_lock(&inode->i_mutex); |
| 6236 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6237 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6238 | if (ret) { |
| 6239 | mlog_errno(ret); |
| 6240 | goto out_mutex; |
| 6241 | } |
| 6242 | |
| 6243 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6244 | if (IS_ERR(handle)) { |
| 6245 | ret = PTR_ERR(handle); |
| 6246 | mlog_errno(ret); |
| 6247 | goto out_unlock; |
| 6248 | } |
| 6249 | |
| 6250 | while (head) { |
| 6251 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6252 | head->free_bit); |
| 6253 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 6254 | head->free_bit, (unsigned long long)head->free_blk); |
| 6255 | |
| 6256 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6257 | head->free_bit, bg_blkno, 1); |
| 6258 | if (ret) { |
| 6259 | mlog_errno(ret); |
| 6260 | goto out_journal; |
| 6261 | } |
| 6262 | |
| 6263 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 6264 | if (ret) { |
| 6265 | mlog_errno(ret); |
| 6266 | goto out_journal; |
| 6267 | } |
| 6268 | |
| 6269 | tmp = head; |
| 6270 | head = head->free_next; |
| 6271 | kfree(tmp); |
| 6272 | } |
| 6273 | |
| 6274 | out_journal: |
| 6275 | ocfs2_commit_trans(osb, handle); |
| 6276 | |
| 6277 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6278 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6279 | brelse(di_bh); |
| 6280 | out_mutex: |
| 6281 | mutex_unlock(&inode->i_mutex); |
| 6282 | iput(inode); |
| 6283 | out: |
| 6284 | while(head) { |
| 6285 | /* Premature exit may have left some dangling items. */ |
| 6286 | tmp = head; |
| 6287 | head = head->free_next; |
| 6288 | kfree(tmp); |
| 6289 | } |
| 6290 | |
| 6291 | return ret; |
| 6292 | } |
| 6293 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6294 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6295 | u64 blkno, unsigned int bit) |
| 6296 | { |
| 6297 | int ret = 0; |
| 6298 | struct ocfs2_cached_block_free *item; |
| 6299 | |
| 6300 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6301 | if (item == NULL) { |
| 6302 | ret = -ENOMEM; |
| 6303 | mlog_errno(ret); |
| 6304 | return ret; |
| 6305 | } |
| 6306 | |
| 6307 | mlog(0, "Insert clusters: (bit %u, blk %llu)\n", |
| 6308 | bit, (unsigned long long)blkno); |
| 6309 | |
| 6310 | item->free_blk = blkno; |
| 6311 | item->free_bit = bit; |
| 6312 | item->free_next = ctxt->c_global_allocator; |
| 6313 | |
| 6314 | ctxt->c_global_allocator = item; |
| 6315 | return ret; |
| 6316 | } |
| 6317 | |
| 6318 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6319 | struct ocfs2_cached_block_free *head) |
| 6320 | { |
| 6321 | struct ocfs2_cached_block_free *tmp; |
| 6322 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6323 | handle_t *handle; |
| 6324 | int ret = 0; |
| 6325 | |
| 6326 | mutex_lock(&tl_inode->i_mutex); |
| 6327 | |
| 6328 | while (head) { |
| 6329 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6330 | ret = __ocfs2_flush_truncate_log(osb); |
| 6331 | if (ret < 0) { |
| 6332 | mlog_errno(ret); |
| 6333 | break; |
| 6334 | } |
| 6335 | } |
| 6336 | |
| 6337 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6338 | if (IS_ERR(handle)) { |
| 6339 | ret = PTR_ERR(handle); |
| 6340 | mlog_errno(ret); |
| 6341 | break; |
| 6342 | } |
| 6343 | |
| 6344 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6345 | head->free_bit); |
| 6346 | |
| 6347 | ocfs2_commit_trans(osb, handle); |
| 6348 | tmp = head; |
| 6349 | head = head->free_next; |
| 6350 | kfree(tmp); |
| 6351 | |
| 6352 | if (ret < 0) { |
| 6353 | mlog_errno(ret); |
| 6354 | break; |
| 6355 | } |
| 6356 | } |
| 6357 | |
| 6358 | mutex_unlock(&tl_inode->i_mutex); |
| 6359 | |
| 6360 | while (head) { |
| 6361 | /* Premature exit may have left some dangling items. */ |
| 6362 | tmp = head; |
| 6363 | head = head->free_next; |
| 6364 | kfree(tmp); |
| 6365 | } |
| 6366 | |
| 6367 | return ret; |
| 6368 | } |
| 6369 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6370 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6371 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6372 | { |
| 6373 | int ret = 0, ret2; |
| 6374 | struct ocfs2_per_slot_free_list *fl; |
| 6375 | |
| 6376 | if (!ctxt) |
| 6377 | return 0; |
| 6378 | |
| 6379 | while (ctxt->c_first_suballocator) { |
| 6380 | fl = ctxt->c_first_suballocator; |
| 6381 | |
| 6382 | if (fl->f_first) { |
| 6383 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 6384 | fl->f_inode_type, fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6385 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6386 | fl->f_inode_type, |
| 6387 | fl->f_slot, |
| 6388 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6389 | if (ret2) |
| 6390 | mlog_errno(ret2); |
| 6391 | if (!ret) |
| 6392 | ret = ret2; |
| 6393 | } |
| 6394 | |
| 6395 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6396 | kfree(fl); |
| 6397 | } |
| 6398 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6399 | if (ctxt->c_global_allocator) { |
| 6400 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6401 | ctxt->c_global_allocator); |
| 6402 | if (ret2) |
| 6403 | mlog_errno(ret2); |
| 6404 | if (!ret) |
| 6405 | ret = ret2; |
| 6406 | |
| 6407 | ctxt->c_global_allocator = NULL; |
| 6408 | } |
| 6409 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6410 | return ret; |
| 6411 | } |
| 6412 | |
| 6413 | static struct ocfs2_per_slot_free_list * |
| 6414 | ocfs2_find_per_slot_free_list(int type, |
| 6415 | int slot, |
| 6416 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6417 | { |
| 6418 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6419 | |
| 6420 | while (fl) { |
| 6421 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6422 | return fl; |
| 6423 | |
| 6424 | fl = fl->f_next_suballocator; |
| 6425 | } |
| 6426 | |
| 6427 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6428 | if (fl) { |
| 6429 | fl->f_inode_type = type; |
| 6430 | fl->f_slot = slot; |
| 6431 | fl->f_first = NULL; |
| 6432 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6433 | |
| 6434 | ctxt->c_first_suballocator = fl; |
| 6435 | } |
| 6436 | return fl; |
| 6437 | } |
| 6438 | |
Tao Ma | 1823cb0 | 2009-08-18 11:24:49 +0800 | [diff] [blame] | 6439 | int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6440 | int type, int slot, u64 blkno, |
| 6441 | unsigned int bit) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6442 | { |
| 6443 | int ret; |
| 6444 | struct ocfs2_per_slot_free_list *fl; |
| 6445 | struct ocfs2_cached_block_free *item; |
| 6446 | |
| 6447 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6448 | if (fl == NULL) { |
| 6449 | ret = -ENOMEM; |
| 6450 | mlog_errno(ret); |
| 6451 | goto out; |
| 6452 | } |
| 6453 | |
| 6454 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 6455 | if (item == NULL) { |
| 6456 | ret = -ENOMEM; |
| 6457 | mlog_errno(ret); |
| 6458 | goto out; |
| 6459 | } |
| 6460 | |
| 6461 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 6462 | type, slot, bit, (unsigned long long)blkno); |
| 6463 | |
| 6464 | item->free_blk = blkno; |
| 6465 | item->free_bit = bit; |
| 6466 | item->free_next = fl->f_first; |
| 6467 | |
| 6468 | fl->f_first = item; |
| 6469 | |
| 6470 | ret = 0; |
| 6471 | out: |
| 6472 | return ret; |
| 6473 | } |
| 6474 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6475 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6476 | struct ocfs2_extent_block *eb) |
| 6477 | { |
| 6478 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6479 | le16_to_cpu(eb->h_suballoc_slot), |
| 6480 | le64_to_cpu(eb->h_blkno), |
| 6481 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6482 | } |
| 6483 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6484 | /* This function will figure out whether the currently last extent |
| 6485 | * block will be deleted, and if it will, what the new last extent |
| 6486 | * block will be so we can update his h_next_leaf_blk field, as well |
| 6487 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6488 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6489 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6490 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6491 | struct buffer_head **new_last_eb) |
| 6492 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6493 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6494 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6495 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6496 | struct ocfs2_extent_block *eb; |
| 6497 | struct ocfs2_extent_list *el; |
| 6498 | struct buffer_head *bh = NULL; |
| 6499 | |
| 6500 | *new_last_eb = NULL; |
| 6501 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6502 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6503 | if (!path->p_tree_depth) |
| 6504 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6505 | |
| 6506 | /* trunc to zero special case - this makes tree_depth = 0 |
| 6507 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6508 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6509 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6510 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6511 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6512 | BUG_ON(!el->l_next_free_rec); |
| 6513 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6514 | /* |
| 6515 | * Make sure that this extent list will actually be empty |
| 6516 | * after we clear away the data. We can shortcut out if |
| 6517 | * there's more than one non-empty extent in the |
| 6518 | * list. Otherwise, a check of the remaining extent is |
| 6519 | * necessary. |
| 6520 | */ |
| 6521 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 6522 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6523 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6524 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6525 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6526 | |
| 6527 | /* We may have a valid extent in index 1, check it. */ |
| 6528 | if (next_free == 2) |
| 6529 | rec = &el->l_recs[1]; |
| 6530 | |
| 6531 | /* |
| 6532 | * Fall through - no more nonempty extents, so we want |
| 6533 | * to delete this leaf. |
| 6534 | */ |
| 6535 | } else { |
| 6536 | if (next_free > 1) |
| 6537 | goto out; |
| 6538 | |
| 6539 | rec = &el->l_recs[0]; |
| 6540 | } |
| 6541 | |
| 6542 | if (rec) { |
| 6543 | /* |
| 6544 | * Check it we'll only be trimming off the end of this |
| 6545 | * cluster. |
| 6546 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6547 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6548 | goto out; |
| 6549 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6550 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6551 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 6552 | if (ret) { |
| 6553 | mlog_errno(ret); |
| 6554 | goto out; |
| 6555 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6556 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 6557 | 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] | 6558 | if (ret) { |
| 6559 | mlog_errno(ret); |
| 6560 | goto out; |
| 6561 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6562 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6563 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 6564 | el = &eb->h_list; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 6565 | |
| 6566 | /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). |
| 6567 | * Any corruption is a code bug. */ |
| 6568 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6569 | |
| 6570 | *new_last_eb = bh; |
| 6571 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6572 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 6573 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 6574 | out: |
| 6575 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6576 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6577 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6578 | } |
| 6579 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6580 | /* |
| 6581 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 6582 | * during truncate. |
| 6583 | * |
| 6584 | * The caller needs to: |
| 6585 | * - start journaling of each path component. |
| 6586 | * - compute and fully set up any new last ext block |
| 6587 | */ |
| 6588 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 6589 | handle_t *handle, struct ocfs2_truncate_context *tc, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6590 | u32 clusters_to_del, u64 *delete_start, u8 *flags) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6591 | { |
| 6592 | int ret, i, index = path->p_tree_depth; |
| 6593 | u32 new_edge = 0; |
| 6594 | u64 deleted_eb = 0; |
| 6595 | struct buffer_head *bh; |
| 6596 | struct ocfs2_extent_list *el; |
| 6597 | struct ocfs2_extent_rec *rec; |
| 6598 | |
| 6599 | *delete_start = 0; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6600 | *flags = 0; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6601 | |
| 6602 | while (index >= 0) { |
| 6603 | bh = path->p_node[index].bh; |
| 6604 | el = path->p_node[index].el; |
| 6605 | |
| 6606 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 6607 | index, (unsigned long long)bh->b_blocknr); |
| 6608 | |
| 6609 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 6610 | |
| 6611 | if (index != |
| 6612 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 6613 | ocfs2_error(inode->i_sb, |
| 6614 | "Inode %lu has invalid ext. block %llu", |
| 6615 | inode->i_ino, |
| 6616 | (unsigned long long)bh->b_blocknr); |
| 6617 | ret = -EROFS; |
| 6618 | goto out; |
| 6619 | } |
| 6620 | |
| 6621 | find_tail_record: |
| 6622 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 6623 | rec = &el->l_recs[i]; |
| 6624 | |
| 6625 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 6626 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6627 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6628 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6629 | le16_to_cpu(el->l_next_free_rec)); |
| 6630 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6631 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6632 | |
| 6633 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 6634 | /* |
| 6635 | * If the leaf block contains a single empty |
| 6636 | * extent and no records, we can just remove |
| 6637 | * the block. |
| 6638 | */ |
| 6639 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 6640 | memset(rec, 0, |
| 6641 | sizeof(struct ocfs2_extent_rec)); |
| 6642 | el->l_next_free_rec = cpu_to_le16(0); |
| 6643 | |
| 6644 | goto delete; |
| 6645 | } |
| 6646 | |
| 6647 | /* |
| 6648 | * Remove any empty extents by shifting things |
| 6649 | * left. That should make life much easier on |
| 6650 | * the code below. This condition is rare |
| 6651 | * enough that we shouldn't see a performance |
| 6652 | * hit. |
| 6653 | */ |
| 6654 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 6655 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6656 | |
| 6657 | for(i = 0; |
| 6658 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 6659 | el->l_recs[i] = el->l_recs[i + 1]; |
| 6660 | |
| 6661 | memset(&el->l_recs[i], 0, |
| 6662 | sizeof(struct ocfs2_extent_rec)); |
| 6663 | |
| 6664 | /* |
| 6665 | * We've modified our extent list. The |
| 6666 | * simplest way to handle this change |
| 6667 | * is to being the search from the |
| 6668 | * start again. |
| 6669 | */ |
| 6670 | goto find_tail_record; |
| 6671 | } |
| 6672 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6673 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6674 | |
| 6675 | /* |
| 6676 | * We'll use "new_edge" on our way back up the |
| 6677 | * tree to know what our rightmost cpos is. |
| 6678 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6679 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6680 | new_edge += le32_to_cpu(rec->e_cpos); |
| 6681 | |
| 6682 | /* |
| 6683 | * The caller will use this to delete data blocks. |
| 6684 | */ |
| 6685 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 6686 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6687 | le16_to_cpu(rec->e_leaf_clusters)); |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6688 | *flags = rec->e_flags; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6689 | |
| 6690 | /* |
| 6691 | * If it's now empty, remove this record. |
| 6692 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6693 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6694 | memset(rec, 0, |
| 6695 | sizeof(struct ocfs2_extent_rec)); |
| 6696 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6697 | } |
| 6698 | } else { |
| 6699 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 6700 | memset(rec, 0, |
| 6701 | sizeof(struct ocfs2_extent_rec)); |
| 6702 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 6703 | |
| 6704 | goto delete; |
| 6705 | } |
| 6706 | |
| 6707 | /* Can this actually happen? */ |
| 6708 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 6709 | goto delete; |
| 6710 | |
| 6711 | /* |
| 6712 | * We never actually deleted any clusters |
| 6713 | * because our leaf was empty. There's no |
| 6714 | * reason to adjust the rightmost edge then. |
| 6715 | */ |
| 6716 | if (new_edge == 0) |
| 6717 | goto delete; |
| 6718 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6719 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 6720 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6721 | -le32_to_cpu(rec->e_cpos)); |
| 6722 | |
| 6723 | /* |
| 6724 | * A deleted child record should have been |
| 6725 | * caught above. |
| 6726 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6727 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6728 | } |
| 6729 | |
| 6730 | delete: |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 6731 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6732 | |
| 6733 | mlog(0, "extent list container %llu, after: record %d: " |
| 6734 | "(%u, %u, %llu), next = %u.\n", |
| 6735 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6736 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6737 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 6738 | le16_to_cpu(el->l_next_free_rec)); |
| 6739 | |
| 6740 | /* |
| 6741 | * We must be careful to only attempt delete of an |
| 6742 | * extent block (and not the root inode block). |
| 6743 | */ |
| 6744 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 6745 | struct ocfs2_extent_block *eb = |
| 6746 | (struct ocfs2_extent_block *)bh->b_data; |
| 6747 | |
| 6748 | /* |
| 6749 | * Save this for use when processing the |
| 6750 | * parent block. |
| 6751 | */ |
| 6752 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 6753 | |
| 6754 | mlog(0, "deleting this extent block.\n"); |
| 6755 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 6756 | ocfs2_remove_from_cache(INODE_CACHE(inode), bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6757 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6758 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6759 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 6760 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 6761 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6762 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 6763 | /* An error here is not fatal. */ |
| 6764 | if (ret < 0) |
| 6765 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6766 | } else { |
| 6767 | deleted_eb = 0; |
| 6768 | } |
| 6769 | |
| 6770 | index--; |
| 6771 | } |
| 6772 | |
| 6773 | ret = 0; |
| 6774 | out: |
| 6775 | return ret; |
| 6776 | } |
| 6777 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6778 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 6779 | unsigned int clusters_to_del, |
| 6780 | struct inode *inode, |
| 6781 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6782 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6783 | struct ocfs2_truncate_context *tc, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6784 | struct ocfs2_path *path, |
| 6785 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6786 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6787 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6788 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6789 | struct ocfs2_extent_block *last_eb = NULL; |
| 6790 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6791 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6792 | u64 delete_blk = 0; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6793 | u8 rec_flags; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6794 | |
| 6795 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6796 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6797 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6798 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6799 | if (status < 0) { |
| 6800 | mlog_errno(status); |
| 6801 | goto bail; |
| 6802 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6803 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6804 | /* |
| 6805 | * Each component will be touched, so we might as well journal |
| 6806 | * here to avoid having to handle errors later. |
| 6807 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6808 | status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6809 | if (status < 0) { |
| 6810 | mlog_errno(status); |
| 6811 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | if (last_eb_bh) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6815 | status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6816 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6817 | if (status < 0) { |
| 6818 | mlog_errno(status); |
| 6819 | goto bail; |
| 6820 | } |
| 6821 | |
| 6822 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6823 | } |
| 6824 | |
| 6825 | el = &(fe->id2.i_list); |
| 6826 | |
| 6827 | /* |
| 6828 | * Lower levels depend on this never happening, but it's best |
| 6829 | * to check it up here before changing the tree. |
| 6830 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 6831 | 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] | 6832 | ocfs2_error(inode->i_sb, |
| 6833 | "Inode %lu has an empty extent record, depth %u\n", |
| 6834 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6835 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6836 | goto bail; |
| 6837 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6838 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 6839 | dquot_free_space_nodirty(inode, |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6840 | ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6841 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 6842 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 6843 | clusters_to_del; |
| 6844 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 6845 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 6846 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6847 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6848 | status = ocfs2_trim_tree(inode, path, handle, tc, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6849 | clusters_to_del, &delete_blk, &rec_flags); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6850 | if (status) { |
| 6851 | mlog_errno(status); |
| 6852 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6853 | } |
| 6854 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 6855 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6856 | /* trunc to zero is a special case. */ |
| 6857 | el->l_tree_depth = 0; |
| 6858 | fe->i_last_eb_blk = 0; |
| 6859 | } else if (last_eb) |
| 6860 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 6861 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 6862 | ocfs2_journal_dirty(handle, fe_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6863 | |
| 6864 | if (last_eb) { |
| 6865 | /* If there will be a new last extent block, then by |
| 6866 | * definition, there cannot be any leaves to the right of |
| 6867 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6868 | last_eb->h_next_leaf_blk = 0; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 6869 | ocfs2_journal_dirty(handle, last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6870 | } |
| 6871 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 6872 | if (delete_blk) { |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6873 | if (rec_flags & OCFS2_EXT_REFCOUNTED) |
| 6874 | status = ocfs2_decrease_refcount(inode, handle, |
| 6875 | ocfs2_blocks_to_clusters(osb->sb, |
| 6876 | delete_blk), |
| 6877 | clusters_to_del, meta_ac, |
Tao Ma | 6ae23c5 | 2009-08-18 11:30:55 +0800 | [diff] [blame] | 6878 | &tc->tc_dealloc, 1); |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 6879 | else |
| 6880 | status = ocfs2_truncate_log_append(osb, handle, |
| 6881 | delete_blk, |
| 6882 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6883 | if (status < 0) { |
| 6884 | mlog_errno(status); |
| 6885 | goto bail; |
| 6886 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6887 | } |
| 6888 | status = 0; |
| 6889 | bail: |
Tao Ma | 60e2ec4 | 2009-08-12 14:42:47 +0800 | [diff] [blame] | 6890 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6891 | mlog_exit(status); |
| 6892 | return status; |
| 6893 | } |
| 6894 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6895 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6896 | { |
| 6897 | set_buffer_uptodate(bh); |
| 6898 | mark_buffer_dirty(bh); |
| 6899 | return 0; |
| 6900 | } |
| 6901 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 6902 | void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 6903 | unsigned int from, unsigned int to, |
| 6904 | struct page *page, int zero, u64 *phys) |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6905 | { |
| 6906 | int ret, partial = 0; |
| 6907 | |
| 6908 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 6909 | if (ret) |
| 6910 | mlog_errno(ret); |
| 6911 | |
| 6912 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 6913 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6914 | |
| 6915 | /* |
| 6916 | * Need to set the buffers we zero'd into uptodate |
| 6917 | * here if they aren't - ocfs2_map_page_blocks() |
| 6918 | * might've skipped some |
| 6919 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6920 | ret = walk_page_buffers(handle, page_buffers(page), |
| 6921 | from, to, &partial, |
| 6922 | ocfs2_zero_func); |
| 6923 | if (ret < 0) |
| 6924 | mlog_errno(ret); |
| 6925 | else if (ocfs2_should_order_data(inode)) { |
| 6926 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6927 | if (ret < 0) |
| 6928 | mlog_errno(ret); |
| 6929 | } |
| 6930 | |
| 6931 | if (!partial) |
| 6932 | SetPageUptodate(page); |
| 6933 | |
| 6934 | flush_dcache_page(page); |
| 6935 | } |
| 6936 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6937 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 6938 | loff_t end, struct page **pages, |
| 6939 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6940 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6941 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6942 | struct page *page; |
| 6943 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 6944 | struct super_block *sb = inode->i_sb; |
| 6945 | |
| 6946 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 6947 | |
| 6948 | if (numpages == 0) |
| 6949 | goto out; |
| 6950 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6951 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6952 | for(i = 0; i < numpages; i++) { |
| 6953 | page = pages[i]; |
| 6954 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6955 | from = start & (PAGE_CACHE_SIZE - 1); |
| 6956 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 6957 | to = end & (PAGE_CACHE_SIZE - 1); |
| 6958 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6959 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 6960 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 6961 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6962 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 6963 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6964 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6965 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6966 | } |
| 6967 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6968 | if (pages) |
| 6969 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6970 | } |
| 6971 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 6972 | int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end, |
| 6973 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6974 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6975 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6976 | struct address_space *mapping = inode->i_mapping; |
| 6977 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6978 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6979 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6980 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6981 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6982 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6983 | last_page_bytes = PAGE_ALIGN(end); |
| 6984 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6985 | do { |
| 6986 | pages[numpages] = grab_cache_page(mapping, index); |
| 6987 | if (!pages[numpages]) { |
| 6988 | ret = -ENOMEM; |
| 6989 | mlog_errno(ret); |
| 6990 | goto out; |
| 6991 | } |
| 6992 | |
| 6993 | numpages++; |
| 6994 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6995 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6996 | |
| 6997 | out: |
| 6998 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6999 | if (pages) |
| 7000 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7001 | numpages = 0; |
| 7002 | } |
| 7003 | |
| 7004 | *num = numpages; |
| 7005 | |
| 7006 | return ret; |
| 7007 | } |
| 7008 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 7009 | static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, |
| 7010 | struct page **pages, int *num) |
| 7011 | { |
| 7012 | struct super_block *sb = inode->i_sb; |
| 7013 | |
| 7014 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 7015 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 7016 | |
| 7017 | return ocfs2_grab_pages(inode, start, end, pages, num); |
| 7018 | } |
| 7019 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7020 | /* |
| 7021 | * Zero the area past i_size but still within an allocated |
| 7022 | * cluster. This avoids exposing nonzero data on subsequent file |
| 7023 | * extends. |
| 7024 | * |
| 7025 | * We need to call this before i_size is updated on the inode because |
| 7026 | * otherwise block_write_full_page() will skip writeout of pages past |
| 7027 | * i_size. The new_i_size parameter is passed for this reason. |
| 7028 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7029 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 7030 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7031 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7032 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7033 | struct page **pages = NULL; |
| 7034 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7035 | unsigned int ext_flags; |
| 7036 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7037 | |
| 7038 | /* |
| 7039 | * File systems which don't support sparse files zero on every |
| 7040 | * extend. |
| 7041 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7042 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7043 | return 0; |
| 7044 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7045 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7046 | sizeof(struct page *), GFP_NOFS); |
| 7047 | if (pages == NULL) { |
| 7048 | ret = -ENOMEM; |
| 7049 | mlog_errno(ret); |
| 7050 | goto out; |
| 7051 | } |
| 7052 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7053 | if (range_start == range_end) |
| 7054 | goto out; |
| 7055 | |
| 7056 | ret = ocfs2_extent_map_get_blocks(inode, |
| 7057 | range_start >> sb->s_blocksize_bits, |
| 7058 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7059 | if (ret) { |
| 7060 | mlog_errno(ret); |
| 7061 | goto out; |
| 7062 | } |
| 7063 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7064 | /* |
| 7065 | * Tail is a hole, or is marked unwritten. In either case, we |
| 7066 | * can count on read and write to return/push zero's. |
| 7067 | */ |
| 7068 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7069 | goto out; |
| 7070 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 7071 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 7072 | &numpages); |
| 7073 | if (ret) { |
| 7074 | mlog_errno(ret); |
| 7075 | goto out; |
| 7076 | } |
| 7077 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 7078 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 7079 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7080 | |
| 7081 | /* |
| 7082 | * Initiate writeout of the pages we zero'd here. We don't |
| 7083 | * wait on them - the truncate_inode_pages() call later will |
| 7084 | * do that for us. |
| 7085 | */ |
Christoph Hellwig | 2cfd30a | 2009-09-23 15:04:02 +0200 | [diff] [blame] | 7086 | ret = filemap_fdatawrite_range(inode->i_mapping, range_start, |
| 7087 | range_end - 1); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 7088 | if (ret) |
| 7089 | mlog_errno(ret); |
| 7090 | |
| 7091 | out: |
| 7092 | if (pages) |
| 7093 | kfree(pages); |
| 7094 | |
| 7095 | return ret; |
| 7096 | } |
| 7097 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7098 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 7099 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7100 | { |
| 7101 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7102 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7103 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7104 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 7105 | memset(&di->id2, 0, blocksize - |
| 7106 | offsetof(struct ocfs2_dinode, id2) - |
| 7107 | xattrsize); |
| 7108 | else |
| 7109 | memset(&di->id2, 0, blocksize - |
| 7110 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7111 | } |
| 7112 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7113 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 7114 | struct ocfs2_dinode *di) |
| 7115 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7116 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7117 | di->id2.i_list.l_tree_depth = 0; |
| 7118 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7119 | di->id2.i_list.l_count = cpu_to_le16( |
| 7120 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7121 | } |
| 7122 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7123 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 7124 | { |
| 7125 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7126 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7127 | |
| 7128 | spin_lock(&oi->ip_lock); |
| 7129 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 7130 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7131 | spin_unlock(&oi->ip_lock); |
| 7132 | |
| 7133 | /* |
| 7134 | * We clear the entire i_data structure here so that all |
| 7135 | * fields can be properly initialized. |
| 7136 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7137 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7138 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 7139 | idata->id_count = cpu_to_le16( |
| 7140 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 7144 | struct buffer_head *di_bh) |
| 7145 | { |
| 7146 | int ret, i, has_data, num_pages = 0; |
| 7147 | handle_t *handle; |
| 7148 | u64 uninitialized_var(block); |
| 7149 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 7150 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7151 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7152 | struct ocfs2_alloc_context *data_ac = NULL; |
| 7153 | struct page **pages = NULL; |
| 7154 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 7155 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7156 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7157 | |
| 7158 | has_data = i_size_read(inode) ? 1 : 0; |
| 7159 | |
| 7160 | if (has_data) { |
| 7161 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 7162 | sizeof(struct page *), GFP_NOFS); |
| 7163 | if (pages == NULL) { |
| 7164 | ret = -ENOMEM; |
| 7165 | mlog_errno(ret); |
| 7166 | goto out; |
| 7167 | } |
| 7168 | |
| 7169 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 7170 | if (ret) { |
| 7171 | mlog_errno(ret); |
| 7172 | goto out; |
| 7173 | } |
| 7174 | } |
| 7175 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7176 | handle = ocfs2_start_trans(osb, |
| 7177 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7178 | if (IS_ERR(handle)) { |
| 7179 | ret = PTR_ERR(handle); |
| 7180 | mlog_errno(ret); |
| 7181 | goto out_unlock; |
| 7182 | } |
| 7183 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7184 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7185 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7186 | if (ret) { |
| 7187 | mlog_errno(ret); |
| 7188 | goto out_commit; |
| 7189 | } |
| 7190 | |
| 7191 | if (has_data) { |
| 7192 | u32 bit_off, num; |
| 7193 | unsigned int page_end; |
| 7194 | u64 phys; |
| 7195 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 7196 | ret = dquot_alloc_space_nodirty(inode, |
| 7197 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7198 | if (ret) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7199 | goto out_commit; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7200 | did_quota = 1; |
| 7201 | |
Mark Fasheh | 4fe370a | 2009-12-07 13:15:40 -0800 | [diff] [blame] | 7202 | data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv; |
| 7203 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7204 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 7205 | &num); |
| 7206 | if (ret) { |
| 7207 | mlog_errno(ret); |
| 7208 | goto out_commit; |
| 7209 | } |
| 7210 | |
| 7211 | /* |
| 7212 | * Save two copies, one for insert, and one that can |
| 7213 | * be changed by ocfs2_map_and_dirty_page() below. |
| 7214 | */ |
| 7215 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 7216 | |
| 7217 | /* |
| 7218 | * Non sparse file systems zero on extend, so no need |
| 7219 | * to do that now. |
| 7220 | */ |
| 7221 | if (!ocfs2_sparse_alloc(osb) && |
| 7222 | PAGE_CACHE_SIZE < osb->s_clustersize) |
| 7223 | end = PAGE_CACHE_SIZE; |
| 7224 | |
| 7225 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 7226 | if (ret) { |
| 7227 | mlog_errno(ret); |
| 7228 | goto out_commit; |
| 7229 | } |
| 7230 | |
| 7231 | /* |
| 7232 | * This should populate the 1st page for us and mark |
| 7233 | * it up to date. |
| 7234 | */ |
| 7235 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 7236 | if (ret) { |
| 7237 | mlog_errno(ret); |
| 7238 | goto out_commit; |
| 7239 | } |
| 7240 | |
| 7241 | page_end = PAGE_CACHE_SIZE; |
| 7242 | if (PAGE_CACHE_SIZE > osb->s_clustersize) |
| 7243 | page_end = osb->s_clustersize; |
| 7244 | |
| 7245 | for (i = 0; i < num_pages; i++) |
| 7246 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 7247 | pages[i], i > 0, &phys); |
| 7248 | } |
| 7249 | |
| 7250 | spin_lock(&oi->ip_lock); |
| 7251 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 7252 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 7253 | spin_unlock(&oi->ip_lock); |
| 7254 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7255 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7256 | |
| 7257 | ocfs2_journal_dirty(handle, di_bh); |
| 7258 | |
| 7259 | if (has_data) { |
| 7260 | /* |
| 7261 | * An error at this point should be extremely rare. If |
| 7262 | * this proves to be false, we could always re-build |
| 7263 | * the in-inode data from our pages. |
| 7264 | */ |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 7265 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 7266 | ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7267 | if (ret) { |
| 7268 | mlog_errno(ret); |
| 7269 | goto out_commit; |
| 7270 | } |
| 7271 | |
| 7272 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7273 | } |
| 7274 | |
| 7275 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7276 | if (ret < 0 && did_quota) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 7277 | dquot_free_space_nodirty(inode, |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7278 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7279 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7280 | ocfs2_commit_trans(osb, handle); |
| 7281 | |
| 7282 | out_unlock: |
| 7283 | if (data_ac) |
| 7284 | ocfs2_free_alloc_context(data_ac); |
| 7285 | |
| 7286 | out: |
| 7287 | if (pages) { |
| 7288 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7289 | kfree(pages); |
| 7290 | } |
| 7291 | |
| 7292 | return ret; |
| 7293 | } |
| 7294 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7295 | /* |
| 7296 | * It is expected, that by the time you call this function, |
| 7297 | * inode->i_size and fe->i_size have been adjusted. |
| 7298 | * |
| 7299 | * WARNING: This will kfree the truncate context |
| 7300 | */ |
| 7301 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7302 | struct inode *inode, |
| 7303 | struct buffer_head *fe_bh, |
| 7304 | struct ocfs2_truncate_context *tc) |
| 7305 | { |
| 7306 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7307 | u32 clusters_to_del, new_highest_cpos, range; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7308 | u64 blkno = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7309 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 7310 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7311 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7312 | struct ocfs2_path *path = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 7313 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7314 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 7315 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7316 | |
| 7317 | mlog_entry_void(); |
| 7318 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7319 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7320 | i_size_read(inode)); |
| 7321 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7322 | path = ocfs2_new_path(fe_bh, &di->id2.i_list, |
| 7323 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7324 | if (!path) { |
| 7325 | status = -ENOMEM; |
| 7326 | mlog_errno(status); |
| 7327 | goto bail; |
| 7328 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7329 | |
| 7330 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7331 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7332 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7333 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7334 | * Check that we still have allocation to delete. |
| 7335 | */ |
| 7336 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7337 | status = 0; |
| 7338 | goto bail; |
| 7339 | } |
| 7340 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7341 | credits = 0; |
| 7342 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7343 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7344 | * Truncate always works against the rightmost tree branch. |
| 7345 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7346 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7347 | if (status) { |
| 7348 | mlog_errno(status); |
| 7349 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7350 | } |
| 7351 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7352 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 7353 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 7354 | |
| 7355 | /* |
| 7356 | * By now, el will point to the extent list on the bottom most |
| 7357 | * portion of this tree. Only the tail record is considered in |
| 7358 | * each pass. |
| 7359 | * |
| 7360 | * We handle the following cases, in order: |
| 7361 | * - empty extent: delete the remaining branch |
| 7362 | * - remove the entire record |
| 7363 | * - remove a partial record |
| 7364 | * - no record needs to be removed (truncate has completed) |
| 7365 | */ |
| 7366 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7367 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7368 | ocfs2_error(inode->i_sb, |
| 7369 | "Inode %llu has empty extent block at %llu\n", |
| 7370 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7371 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7372 | status = -EROFS; |
| 7373 | goto bail; |
| 7374 | } |
| 7375 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7376 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7377 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7378 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7379 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 7380 | clusters_to_del = 0; |
| 7381 | } 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] | 7382 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7383 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7384 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 7385 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7386 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7387 | new_highest_cpos; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7388 | blkno = le64_to_cpu(el->l_recs[i].e_blkno) + |
| 7389 | ocfs2_clusters_to_blocks(inode->i_sb, |
| 7390 | ocfs2_rec_clusters(el, &el->l_recs[i]) - |
| 7391 | clusters_to_del); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7392 | } else { |
| 7393 | status = 0; |
| 7394 | goto bail; |
| 7395 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7396 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7397 | mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", |
| 7398 | clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7399 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7400 | if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) { |
| 7401 | BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & |
| 7402 | OCFS2_HAS_REFCOUNT_FL)); |
| 7403 | |
| 7404 | status = ocfs2_lock_refcount_tree(osb, |
| 7405 | le64_to_cpu(di->i_refcount_loc), |
| 7406 | 1, &ref_tree, NULL); |
| 7407 | if (status) { |
| 7408 | mlog_errno(status); |
| 7409 | goto bail; |
| 7410 | } |
| 7411 | |
| 7412 | status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh, |
| 7413 | blkno, |
| 7414 | clusters_to_del, |
| 7415 | &credits, |
| 7416 | &meta_ac); |
| 7417 | if (status < 0) { |
| 7418 | mlog_errno(status); |
| 7419 | goto bail; |
| 7420 | } |
| 7421 | } |
| 7422 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7423 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7424 | tl_sem = 1; |
| 7425 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 7426 | * record is free for use. If there isn't any, we flush to get |
| 7427 | * an empty truncate log. */ |
| 7428 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 7429 | status = __ocfs2_flush_truncate_log(osb); |
| 7430 | if (status < 0) { |
| 7431 | mlog_errno(status); |
| 7432 | goto bail; |
| 7433 | } |
| 7434 | } |
| 7435 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7436 | credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7437 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 7438 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 7439 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7440 | if (IS_ERR(handle)) { |
| 7441 | status = PTR_ERR(handle); |
| 7442 | handle = NULL; |
| 7443 | mlog_errno(status); |
| 7444 | goto bail; |
| 7445 | } |
| 7446 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7447 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7448 | tc, path, meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7449 | if (status < 0) { |
| 7450 | mlog_errno(status); |
| 7451 | goto bail; |
| 7452 | } |
| 7453 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7454 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7455 | tl_sem = 0; |
| 7456 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7457 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7458 | handle = NULL; |
| 7459 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7460 | ocfs2_reinit_path(path, 1); |
| 7461 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7462 | if (meta_ac) { |
| 7463 | ocfs2_free_alloc_context(meta_ac); |
| 7464 | meta_ac = NULL; |
| 7465 | } |
| 7466 | |
| 7467 | if (ref_tree) { |
| 7468 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
| 7469 | ref_tree = NULL; |
| 7470 | } |
| 7471 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7472 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7473 | * The check above will catch the case where we've truncated |
| 7474 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7475 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7476 | goto start; |
| 7477 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7478 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7479 | |
| 7480 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7481 | |
| 7482 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 7483 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7484 | |
| 7485 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 7486 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7487 | |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7488 | if (meta_ac) |
| 7489 | ocfs2_free_alloc_context(meta_ac); |
| 7490 | |
| 7491 | if (ref_tree) |
| 7492 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
| 7493 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7494 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 7495 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7496 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7497 | |
| 7498 | /* This will drop the ext_alloc cluster lock for us */ |
| 7499 | ocfs2_free_truncate_context(tc); |
| 7500 | |
| 7501 | mlog_exit(status); |
| 7502 | return status; |
| 7503 | } |
| 7504 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7505 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7506 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7507 | */ |
| 7508 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 7509 | struct inode *inode, |
| 7510 | struct buffer_head *fe_bh, |
| 7511 | struct ocfs2_truncate_context **tc) |
| 7512 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7513 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7514 | unsigned int new_i_clusters; |
| 7515 | struct ocfs2_dinode *fe; |
| 7516 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7517 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7518 | |
| 7519 | mlog_entry_void(); |
| 7520 | |
| 7521 | *tc = NULL; |
| 7522 | |
| 7523 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 7524 | i_size_read(inode)); |
| 7525 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 7526 | |
| 7527 | 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] | 7528 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 7529 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7530 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 7531 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7532 | if (!(*tc)) { |
| 7533 | status = -ENOMEM; |
| 7534 | mlog_errno(status); |
| 7535 | goto bail; |
| 7536 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7537 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7538 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7539 | if (fe->id2.i_list.l_tree_depth) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 7540 | status = ocfs2_read_extent_block(INODE_CACHE(inode), |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 7541 | le64_to_cpu(fe->i_last_eb_blk), |
| 7542 | &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7543 | if (status < 0) { |
| 7544 | mlog_errno(status); |
| 7545 | goto bail; |
| 7546 | } |
| 7547 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7548 | } |
| 7549 | |
| 7550 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 7551 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7552 | status = 0; |
| 7553 | bail: |
| 7554 | if (status < 0) { |
| 7555 | if (*tc) |
| 7556 | ocfs2_free_truncate_context(*tc); |
| 7557 | *tc = NULL; |
| 7558 | } |
| 7559 | mlog_exit_void(); |
| 7560 | return status; |
| 7561 | } |
| 7562 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7563 | /* |
| 7564 | * 'start' is inclusive, 'end' is not. |
| 7565 | */ |
| 7566 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7567 | unsigned int start, unsigned int end, int trunc) |
| 7568 | { |
| 7569 | int ret; |
| 7570 | unsigned int numbytes; |
| 7571 | handle_t *handle; |
| 7572 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7573 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7574 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7575 | |
| 7576 | if (end > i_size_read(inode)) |
| 7577 | end = i_size_read(inode); |
| 7578 | |
| 7579 | BUG_ON(start >= end); |
| 7580 | |
| 7581 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7582 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7583 | !ocfs2_supports_inline_data(osb)) { |
| 7584 | ocfs2_error(inode->i_sb, |
| 7585 | "Inline data flags for inode %llu don't agree! " |
| 7586 | "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
| 7587 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7588 | le16_to_cpu(di->i_dyn_features), |
| 7589 | OCFS2_I(inode)->ip_dyn_features, |
| 7590 | osb->s_feature_incompat); |
| 7591 | ret = -EROFS; |
| 7592 | goto out; |
| 7593 | } |
| 7594 | |
| 7595 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7596 | if (IS_ERR(handle)) { |
| 7597 | ret = PTR_ERR(handle); |
| 7598 | mlog_errno(ret); |
| 7599 | goto out; |
| 7600 | } |
| 7601 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7602 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7603 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7604 | if (ret) { |
| 7605 | mlog_errno(ret); |
| 7606 | goto out_commit; |
| 7607 | } |
| 7608 | |
| 7609 | numbytes = end - start; |
| 7610 | memset(idata->id_data + start, 0, numbytes); |
| 7611 | |
| 7612 | /* |
| 7613 | * No need to worry about the data page here - it's been |
| 7614 | * truncated already and inline data doesn't need it for |
| 7615 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7616 | * later. |
| 7617 | */ |
| 7618 | if (trunc) { |
| 7619 | i_size_write(inode, start); |
| 7620 | di->i_size = cpu_to_le64(start); |
| 7621 | } |
| 7622 | |
| 7623 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7624 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 7625 | |
| 7626 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7627 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7628 | |
| 7629 | ocfs2_journal_dirty(handle, di_bh); |
| 7630 | |
| 7631 | out_commit: |
| 7632 | ocfs2_commit_trans(osb, handle); |
| 7633 | |
| 7634 | out: |
| 7635 | return ret; |
| 7636 | } |
| 7637 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7638 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 7639 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7640 | /* |
| 7641 | * The caller is responsible for completing deallocation |
| 7642 | * before freeing the context. |
| 7643 | */ |
| 7644 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 7645 | mlog(ML_NOTICE, |
| 7646 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7647 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 7648 | brelse(tc->tc_last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7649 | |
| 7650 | kfree(tc); |
| 7651 | } |