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> |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 32 | #include <linux/blkdev.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 33 | #include <linux/sched/signal.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 34 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 35 | #include <cluster/masklog.h> |
| 36 | |
| 37 | #include "ocfs2.h" |
| 38 | |
| 39 | #include "alloc.h" |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 40 | #include "aops.h" |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 41 | #include "blockcheck.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 42 | #include "dlmglue.h" |
| 43 | #include "extent_map.h" |
| 44 | #include "inode.h" |
| 45 | #include "journal.h" |
| 46 | #include "localalloc.h" |
| 47 | #include "suballoc.h" |
| 48 | #include "sysfile.h" |
| 49 | #include "file.h" |
| 50 | #include "super.h" |
| 51 | #include "uptodate.h" |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 52 | #include "xattr.h" |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 53 | #include "refcounttree.h" |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 54 | #include "ocfs2_trace.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 55 | |
| 56 | #include "buffer_head_io.h" |
| 57 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 58 | enum ocfs2_contig_type { |
| 59 | CONTIG_NONE = 0, |
| 60 | CONTIG_LEFT, |
| 61 | CONTIG_RIGHT, |
| 62 | CONTIG_LEFTRIGHT, |
| 63 | }; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 64 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 65 | static enum ocfs2_contig_type |
| 66 | ocfs2_extent_rec_contig(struct super_block *sb, |
| 67 | struct ocfs2_extent_rec *ext, |
| 68 | struct ocfs2_extent_rec *insert_rec); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Operations for a specific extent tree type. |
| 71 | * |
| 72 | * To implement an on-disk btree (extent tree) type in ocfs2, add |
| 73 | * an ocfs2_extent_tree_operations structure and the matching |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 74 | * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 75 | * for the allocation portion of the extent tree. |
| 76 | */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 77 | struct ocfs2_extent_tree_operations { |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 78 | /* |
| 79 | * last_eb_blk is the block number of the right most leaf extent |
| 80 | * block. Most on-disk structures containing an extent tree store |
| 81 | * this value for fast access. The ->eo_set_last_eb_blk() and |
| 82 | * ->eo_get_last_eb_blk() operations access this value. They are |
| 83 | * both required. |
| 84 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 85 | void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et, |
| 86 | u64 blkno); |
| 87 | u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * The on-disk structure usually keeps track of how many total |
| 91 | * clusters are stored in this extent tree. This function updates |
| 92 | * that value. new_clusters is the delta, and must be |
| 93 | * added to the total. Required. |
| 94 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 95 | void (*eo_update_clusters)(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 96 | u32 new_clusters); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 99 | * If this extent tree is supported by an extent map, insert |
| 100 | * a record into the map. |
| 101 | */ |
| 102 | void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et, |
| 103 | struct ocfs2_extent_rec *rec); |
| 104 | |
| 105 | /* |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 106 | * If this extent tree is supported by an extent map, truncate the |
| 107 | * map to clusters, |
| 108 | */ |
| 109 | void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et, |
| 110 | u32 clusters); |
| 111 | |
| 112 | /* |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 113 | * If ->eo_insert_check() exists, it is called before rec is |
| 114 | * inserted into the extent tree. It is optional. |
| 115 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 116 | int (*eo_insert_check)(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 117 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 118 | int (*eo_sanity_check)(struct ocfs2_extent_tree *et); |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 119 | |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 120 | /* |
| 121 | * -------------------------------------------------------------- |
| 122 | * The remaining are internal to ocfs2_extent_tree and don't have |
| 123 | * accessor functions |
| 124 | */ |
| 125 | |
| 126 | /* |
| 127 | * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. |
| 128 | * It is required. |
| 129 | */ |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 130 | void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); |
Joel Becker | 1625f8a | 2008-08-21 17:11:10 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if |
| 134 | * it exists. If it does not, et->et_max_leaf_clusters is set |
| 135 | * to 0 (unlimited). Optional. |
| 136 | */ |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 137 | void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * ->eo_extent_contig test whether the 2 ocfs2_extent_rec |
| 141 | * are contiguous or not. Optional. Don't need to set it if use |
| 142 | * ocfs2_extent_rec as the tree leaf. |
| 143 | */ |
| 144 | enum ocfs2_contig_type |
| 145 | (*eo_extent_contig)(struct ocfs2_extent_tree *et, |
| 146 | struct ocfs2_extent_rec *ext, |
| 147 | struct ocfs2_extent_rec *insert_rec); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 148 | }; |
| 149 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check |
| 153 | * in the methods. |
| 154 | */ |
| 155 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); |
| 156 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 157 | u64 blkno); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 158 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 159 | u32 clusters); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 160 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 161 | struct ocfs2_extent_rec *rec); |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 162 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 163 | u32 clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 164 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 165 | struct ocfs2_extent_rec *rec); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 166 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 167 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); |
Julia Lawall | 9e62dc0 | 2016-01-14 15:17:21 -0800 | [diff] [blame] | 168 | static const struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 169 | .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, |
| 170 | .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, |
| 171 | .eo_update_clusters = ocfs2_dinode_update_clusters, |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 172 | .eo_extent_map_insert = ocfs2_dinode_extent_map_insert, |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 173 | .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 174 | .eo_insert_check = ocfs2_dinode_insert_check, |
| 175 | .eo_sanity_check = ocfs2_dinode_sanity_check, |
| 176 | .eo_fill_root_el = ocfs2_dinode_fill_root_el, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 180 | u64 blkno) |
| 181 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 182 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 183 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 184 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 185 | di->i_last_eb_blk = cpu_to_le64(blkno); |
| 186 | } |
| 187 | |
| 188 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 189 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 190 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 191 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 192 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 193 | return le64_to_cpu(di->i_last_eb_blk); |
| 194 | } |
| 195 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 196 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 197 | u32 clusters) |
| 198 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 199 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 200 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 201 | |
| 202 | le32_add_cpu(&di->i_clusters, clusters); |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 203 | spin_lock(&oi->ip_lock); |
| 204 | oi->ip_clusters = le32_to_cpu(di->i_clusters); |
| 205 | spin_unlock(&oi->ip_lock); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 208 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
| 209 | struct ocfs2_extent_rec *rec) |
| 210 | { |
| 211 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 212 | |
| 213 | ocfs2_extent_map_insert_rec(inode, rec); |
| 214 | } |
| 215 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 216 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 217 | u32 clusters) |
| 218 | { |
| 219 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; |
| 220 | |
| 221 | ocfs2_extent_map_trunc(inode, clusters); |
| 222 | } |
| 223 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 224 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 225 | struct ocfs2_extent_rec *rec) |
| 226 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 227 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
| 228 | struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 229 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 230 | BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 231 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 232 | (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 233 | "Device %s, asking for sparse allocation: inode %llu, " |
| 234 | "cpos %u, clusters %u\n", |
| 235 | osb->dev_str, |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 236 | (unsigned long long)oi->ip_blkno, |
| 237 | rec->e_cpos, oi->ip_clusters); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 242 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 243 | { |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 244 | struct ocfs2_dinode *di = et->et_object; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 245 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 246 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 247 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 248 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 249 | return 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 250 | } |
| 251 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 252 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) |
| 253 | { |
| 254 | struct ocfs2_dinode *di = et->et_object; |
| 255 | |
| 256 | et->et_root_el = &di->id2.i_list; |
| 257 | } |
| 258 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 259 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 260 | static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) |
| 261 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 262 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 263 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 264 | et->et_root_el = &vb->vb_xv->xr_list; |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 267 | static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 268 | u64 blkno) |
| 269 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 270 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 271 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 272 | vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 276 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 277 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 278 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 279 | return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 280 | } |
| 281 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 282 | static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 283 | u32 clusters) |
| 284 | { |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 285 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 286 | |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 287 | le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Julia Lawall | 9e62dc0 | 2016-01-14 15:17:21 -0800 | [diff] [blame] | 290 | static const struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 291 | .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, |
| 292 | .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, |
| 293 | .eo_update_clusters = ocfs2_xattr_value_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 294 | .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 295 | }; |
| 296 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 297 | static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 298 | { |
| 299 | struct ocfs2_xattr_block *xb = et->et_object; |
| 300 | |
| 301 | et->et_root_el = &xb->xb_attrs.xb_root.xt_list; |
| 302 | } |
| 303 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 304 | 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] | 305 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 306 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 307 | et->et_max_leaf_clusters = |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 308 | ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 311 | static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 312 | u64 blkno) |
| 313 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 314 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 315 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 316 | |
| 317 | xt->xt_last_eb_blk = cpu_to_le64(blkno); |
| 318 | } |
| 319 | |
| 320 | static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 321 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 322 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 323 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
| 324 | |
| 325 | return le64_to_cpu(xt->xt_last_eb_blk); |
| 326 | } |
| 327 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 328 | static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 329 | u32 clusters) |
| 330 | { |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 331 | struct ocfs2_xattr_block *xb = et->et_object; |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 332 | |
| 333 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); |
| 334 | } |
| 335 | |
Julia Lawall | 9e62dc0 | 2016-01-14 15:17:21 -0800 | [diff] [blame] | 336 | static const struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 337 | .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, |
| 338 | .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, |
| 339 | .eo_update_clusters = ocfs2_xattr_tree_update_clusters, |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 340 | .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 341 | .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, |
Tao Ma | ba49261 | 2008-08-18 17:38:49 +0800 | [diff] [blame] | 342 | }; |
| 343 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 344 | static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 345 | u64 blkno) |
| 346 | { |
| 347 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 348 | |
| 349 | dx_root->dr_last_eb_blk = cpu_to_le64(blkno); |
| 350 | } |
| 351 | |
| 352 | static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 353 | { |
| 354 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 355 | |
| 356 | return le64_to_cpu(dx_root->dr_last_eb_blk); |
| 357 | } |
| 358 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 359 | static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 360 | u32 clusters) |
| 361 | { |
| 362 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 363 | |
| 364 | le32_add_cpu(&dx_root->dr_clusters, clusters); |
| 365 | } |
| 366 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 367 | static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 368 | { |
| 369 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 370 | |
| 371 | BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) |
| 377 | { |
| 378 | struct ocfs2_dx_root_block *dx_root = et->et_object; |
| 379 | |
| 380 | et->et_root_el = &dx_root->dr_list; |
| 381 | } |
| 382 | |
Julia Lawall | 9e62dc0 | 2016-01-14 15:17:21 -0800 | [diff] [blame] | 383 | static const struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 384 | .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, |
| 385 | .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, |
| 386 | .eo_update_clusters = ocfs2_dx_root_update_clusters, |
| 387 | .eo_sanity_check = ocfs2_dx_root_sanity_check, |
| 388 | .eo_fill_root_el = ocfs2_dx_root_fill_root_el, |
| 389 | }; |
| 390 | |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame] | 391 | static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et) |
| 392 | { |
| 393 | struct ocfs2_refcount_block *rb = et->et_object; |
| 394 | |
| 395 | et->et_root_el = &rb->rf_list; |
| 396 | } |
| 397 | |
| 398 | static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 399 | u64 blkno) |
| 400 | { |
| 401 | struct ocfs2_refcount_block *rb = et->et_object; |
| 402 | |
| 403 | rb->rf_last_eb_blk = cpu_to_le64(blkno); |
| 404 | } |
| 405 | |
| 406 | static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) |
| 407 | { |
| 408 | struct ocfs2_refcount_block *rb = et->et_object; |
| 409 | |
| 410 | return le64_to_cpu(rb->rf_last_eb_blk); |
| 411 | } |
| 412 | |
| 413 | static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et, |
| 414 | u32 clusters) |
| 415 | { |
| 416 | struct ocfs2_refcount_block *rb = et->et_object; |
| 417 | |
| 418 | le32_add_cpu(&rb->rf_clusters, clusters); |
| 419 | } |
| 420 | |
| 421 | static enum ocfs2_contig_type |
| 422 | ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et, |
| 423 | struct ocfs2_extent_rec *ext, |
| 424 | struct ocfs2_extent_rec *insert_rec) |
| 425 | { |
| 426 | return CONTIG_NONE; |
| 427 | } |
| 428 | |
Julia Lawall | 9e62dc0 | 2016-01-14 15:17:21 -0800 | [diff] [blame] | 429 | static const struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = { |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame] | 430 | .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk, |
| 431 | .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk, |
| 432 | .eo_update_clusters = ocfs2_refcount_tree_update_clusters, |
| 433 | .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el, |
| 434 | .eo_extent_contig = ocfs2_refcount_tree_extent_contig, |
| 435 | }; |
| 436 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 437 | static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 438 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 439 | struct buffer_head *bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 440 | ocfs2_journal_access_func access, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 441 | void *obj, |
Julia Lawall | 9e62dc0 | 2016-01-14 15:17:21 -0800 | [diff] [blame] | 442 | const struct ocfs2_extent_tree_operations *ops) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 443 | { |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 444 | et->et_ops = ops; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 445 | et->et_root_bh = bh; |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 446 | et->et_ci = ci; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 447 | et->et_root_journal_access = access; |
Joel Becker | ea5efa1 | 2008-08-20 16:57:27 -0700 | [diff] [blame] | 448 | if (!obj) |
| 449 | obj = (void *)bh->b_data; |
| 450 | et->et_object = obj; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 451 | |
Joel Becker | 0ce1010 | 2008-08-20 17:19:50 -0700 | [diff] [blame] | 452 | et->et_ops->eo_fill_root_el(et); |
Joel Becker | 943cced | 2008-08-20 17:31:10 -0700 | [diff] [blame] | 453 | if (!et->et_ops->eo_fill_max_leaf_clusters) |
| 454 | et->et_max_leaf_clusters = 0; |
| 455 | else |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 456 | et->et_ops->eo_fill_max_leaf_clusters(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 457 | } |
| 458 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 459 | void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 460 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 461 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 462 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 463 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 464 | NULL, &ocfs2_dinode_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 467 | void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 468 | struct ocfs2_caching_info *ci, |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 469 | struct buffer_head *bh) |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 470 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 471 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 472 | NULL, &ocfs2_xattr_tree_et_ops); |
Joel Becker | 1a09f55 | 2008-08-20 17:44:24 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 475 | void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 476 | struct ocfs2_caching_info *ci, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 477 | struct ocfs2_xattr_value_buf *vb) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 478 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 479 | __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] | 480 | &ocfs2_xattr_value_et_ops); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 481 | } |
| 482 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 483 | void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 484 | struct ocfs2_caching_info *ci, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 485 | struct buffer_head *bh) |
| 486 | { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 487 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 488 | NULL, &ocfs2_dx_root_et_ops); |
| 489 | } |
| 490 | |
Tao Ma | fe92441 | 2009-08-18 11:22:25 +0800 | [diff] [blame] | 491 | void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et, |
| 492 | struct ocfs2_caching_info *ci, |
| 493 | struct buffer_head *bh) |
| 494 | { |
| 495 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb, |
| 496 | NULL, &ocfs2_refcount_tree_et_ops); |
| 497 | } |
| 498 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 499 | static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, |
| 500 | u64 new_last_eb_blk) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 501 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 502 | 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] | 503 | } |
| 504 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 505 | 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] | 506 | { |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 507 | return et->et_ops->eo_get_last_eb_blk(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 508 | } |
| 509 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 510 | static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 511 | u32 clusters) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 512 | { |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 513 | et->et_ops->eo_update_clusters(et, clusters); |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 516 | static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et, |
| 517 | struct ocfs2_extent_rec *rec) |
| 518 | { |
| 519 | if (et->et_ops->eo_extent_map_insert) |
| 520 | et->et_ops->eo_extent_map_insert(et, rec); |
| 521 | } |
| 522 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 523 | static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et, |
| 524 | u32 clusters) |
| 525 | { |
| 526 | if (et->et_ops->eo_extent_map_truncate) |
| 527 | et->et_ops->eo_extent_map_truncate(et, clusters); |
| 528 | } |
| 529 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 530 | static inline int ocfs2_et_root_journal_access(handle_t *handle, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 531 | struct ocfs2_extent_tree *et, |
| 532 | int type) |
| 533 | { |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 534 | 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] | 535 | type); |
| 536 | } |
| 537 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 538 | static inline enum ocfs2_contig_type |
| 539 | ocfs2_et_extent_contig(struct ocfs2_extent_tree *et, |
| 540 | struct ocfs2_extent_rec *rec, |
| 541 | struct ocfs2_extent_rec *insert_rec) |
| 542 | { |
| 543 | if (et->et_ops->eo_extent_contig) |
| 544 | return et->et_ops->eo_extent_contig(et, rec, insert_rec); |
| 545 | |
| 546 | return ocfs2_extent_rec_contig( |
| 547 | ocfs2_metadata_cache_get_super(et->et_ci), |
| 548 | rec, insert_rec); |
| 549 | } |
| 550 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 551 | static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 552 | struct ocfs2_extent_rec *rec) |
| 553 | { |
| 554 | int ret = 0; |
| 555 | |
| 556 | if (et->et_ops->eo_insert_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 557 | ret = et->et_ops->eo_insert_check(et, rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 558 | return ret; |
| 559 | } |
| 560 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 561 | static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 562 | { |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 563 | int ret = 0; |
| 564 | |
| 565 | if (et->et_ops->eo_sanity_check) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 566 | ret = et->et_ops->eo_sanity_check(et); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 567 | return ret; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 568 | } |
| 569 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 570 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 571 | struct ocfs2_extent_block *eb); |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 572 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 573 | struct ocfs2_extent_tree *et, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 574 | struct ocfs2_path *path, |
| 575 | struct ocfs2_extent_rec *insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 576 | /* |
| 577 | * Reset the actual path elements so that we can re-use the structure |
| 578 | * to build another path. Generally, this involves freeing the buffer |
| 579 | * heads. |
| 580 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 581 | void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 582 | { |
| 583 | int i, start = 0, depth = 0; |
| 584 | struct ocfs2_path_item *node; |
| 585 | |
| 586 | if (keep_root) |
| 587 | start = 1; |
| 588 | |
| 589 | for(i = start; i < path_num_items(path); i++) { |
| 590 | node = &path->p_node[i]; |
| 591 | |
| 592 | brelse(node->bh); |
| 593 | node->bh = NULL; |
| 594 | node->el = NULL; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Tree depth may change during truncate, or insert. If we're |
| 599 | * keeping the root extent list, then make sure that our path |
| 600 | * structure reflects the proper depth. |
| 601 | */ |
| 602 | if (keep_root) |
| 603 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 604 | else |
| 605 | path_root_access(path) = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 606 | |
| 607 | path->p_tree_depth = depth; |
| 608 | } |
| 609 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 610 | void ocfs2_free_path(struct ocfs2_path *path) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 611 | { |
| 612 | if (path) { |
| 613 | ocfs2_reinit_path(path, 0); |
| 614 | kfree(path); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 619 | * All the elements of src into dest. After this call, src could be freed |
| 620 | * without affecting dest. |
| 621 | * |
| 622 | * Both paths should have the same root. Any non-root elements of dest |
| 623 | * will be freed. |
| 624 | */ |
| 625 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 626 | { |
| 627 | int i; |
| 628 | |
| 629 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 630 | BUG_ON(path_root_el(dest) != path_root_el(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 631 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 632 | |
| 633 | ocfs2_reinit_path(dest, 1); |
| 634 | |
| 635 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 636 | dest->p_node[i].bh = src->p_node[i].bh; |
| 637 | dest->p_node[i].el = src->p_node[i].el; |
| 638 | |
| 639 | if (dest->p_node[i].bh) |
| 640 | get_bh(dest->p_node[i].bh); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 645 | * Make the *dest path the same as src and re-initialize src path to |
| 646 | * have a root only. |
| 647 | */ |
| 648 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 649 | { |
| 650 | int i; |
| 651 | |
| 652 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 653 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 654 | |
| 655 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 656 | brelse(dest->p_node[i].bh); |
| 657 | |
| 658 | dest->p_node[i].bh = src->p_node[i].bh; |
| 659 | dest->p_node[i].el = src->p_node[i].el; |
| 660 | |
| 661 | src->p_node[i].bh = NULL; |
| 662 | src->p_node[i].el = NULL; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Insert an extent block at given index. |
| 668 | * |
| 669 | * This will not take an additional reference on eb_bh. |
| 670 | */ |
| 671 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, |
| 672 | struct buffer_head *eb_bh) |
| 673 | { |
| 674 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; |
| 675 | |
| 676 | /* |
| 677 | * Right now, no root bh is an extent block, so this helps |
| 678 | * catch code errors with dinode trees. The assertion can be |
| 679 | * safely removed if we ever need to insert extent block |
| 680 | * structures at the root. |
| 681 | */ |
| 682 | BUG_ON(index == 0); |
| 683 | |
| 684 | path->p_node[index].bh = eb_bh; |
| 685 | path->p_node[index].el = &eb->h_list; |
| 686 | } |
| 687 | |
| 688 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 689 | struct ocfs2_extent_list *root_el, |
| 690 | ocfs2_journal_access_func access) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 691 | { |
| 692 | struct ocfs2_path *path; |
| 693 | |
| 694 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
| 695 | |
| 696 | path = kzalloc(sizeof(*path), GFP_NOFS); |
| 697 | if (path) { |
| 698 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); |
| 699 | get_bh(root_bh); |
| 700 | path_root_bh(path) = root_bh; |
| 701 | path_root_el(path) = root_el; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 702 | path_root_access(path) = access; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | return path; |
| 706 | } |
| 707 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 708 | struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 709 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 710 | return ocfs2_new_path(path_root_bh(path), path_root_el(path), |
| 711 | path_root_access(path)); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 714 | 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] | 715 | { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 716 | return ocfs2_new_path(et->et_root_bh, et->et_root_el, |
| 717 | et->et_root_journal_access); |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Journal the buffer at depth idx. All idx>0 are extent_blocks, |
| 722 | * otherwise it's the root_access function. |
| 723 | * |
| 724 | * I don't like the way this function's name looks next to |
| 725 | * ocfs2_journal_access_path(), but I don't have a better one. |
| 726 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 727 | int ocfs2_path_bh_journal_access(handle_t *handle, |
| 728 | struct ocfs2_caching_info *ci, |
| 729 | struct ocfs2_path *path, |
| 730 | int idx) |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 731 | { |
| 732 | ocfs2_journal_access_func access = path_root_access(path); |
| 733 | |
| 734 | if (!access) |
| 735 | access = ocfs2_journal_access; |
| 736 | |
| 737 | if (idx) |
| 738 | access = ocfs2_journal_access_eb; |
| 739 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 740 | return access(handle, ci, path->p_node[idx].bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 741 | OCFS2_JOURNAL_ACCESS_WRITE); |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 744 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 745 | * Convenience function to journal all components in a path. |
| 746 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 747 | int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, |
| 748 | handle_t *handle, |
| 749 | struct ocfs2_path *path) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 750 | { |
| 751 | int i, ret = 0; |
| 752 | |
| 753 | if (!path) |
| 754 | goto out; |
| 755 | |
| 756 | for(i = 0; i < path_num_items(path); i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 757 | ret = ocfs2_path_bh_journal_access(handle, ci, path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 758 | if (ret < 0) { |
| 759 | mlog_errno(ret); |
| 760 | goto out; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | out: |
| 765 | return ret; |
| 766 | } |
| 767 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Return the index of the extent record which contains cluster #v_cluster. |
| 770 | * -1 is returned if it was not found. |
| 771 | * |
| 772 | * Should work fine on interior and exterior nodes. |
| 773 | */ |
| 774 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) |
| 775 | { |
| 776 | int ret = -1; |
| 777 | int i; |
| 778 | struct ocfs2_extent_rec *rec; |
| 779 | u32 rec_end, rec_start, clusters; |
| 780 | |
| 781 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 782 | rec = &el->l_recs[i]; |
| 783 | |
| 784 | rec_start = le32_to_cpu(rec->e_cpos); |
| 785 | clusters = ocfs2_rec_clusters(el, rec); |
| 786 | |
| 787 | rec_end = rec_start + clusters; |
| 788 | |
| 789 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 790 | ret = i; |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | return ret; |
| 796 | } |
| 797 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 798 | /* |
| 799 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 800 | * ocfs2_extent_rec_contig only work properly against leaf nodes! |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 801 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 802 | static int ocfs2_block_extent_contig(struct super_block *sb, |
| 803 | struct ocfs2_extent_rec *ext, |
| 804 | u64 blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 805 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 806 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
| 807 | |
| 808 | blk_end += ocfs2_clusters_to_blocks(sb, |
| 809 | le16_to_cpu(ext->e_leaf_clusters)); |
| 810 | |
| 811 | return blkno == blk_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 814 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
| 815 | struct ocfs2_extent_rec *right) |
| 816 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 817 | u32 left_range; |
| 818 | |
| 819 | left_range = le32_to_cpu(left->e_cpos) + |
| 820 | le16_to_cpu(left->e_leaf_clusters); |
| 821 | |
| 822 | return (left_range == le32_to_cpu(right->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static enum ocfs2_contig_type |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 826 | ocfs2_extent_rec_contig(struct super_block *sb, |
| 827 | struct ocfs2_extent_rec *ext, |
| 828 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 829 | { |
| 830 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); |
| 831 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 832 | /* |
| 833 | * Refuse to coalesce extent records with different flag |
| 834 | * fields - we don't want to mix unwritten extents with user |
| 835 | * data. |
| 836 | */ |
| 837 | if (ext->e_flags != insert_rec->e_flags) |
| 838 | return CONTIG_NONE; |
| 839 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 840 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 841 | ocfs2_block_extent_contig(sb, ext, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 842 | return CONTIG_RIGHT; |
| 843 | |
| 844 | blkno = le64_to_cpu(ext->e_blkno); |
| 845 | if (ocfs2_extents_adjacent(insert_rec, ext) && |
Joel Becker | b4a1765 | 2009-02-13 03:07:09 -0800 | [diff] [blame] | 846 | ocfs2_block_extent_contig(sb, insert_rec, blkno)) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 847 | return CONTIG_LEFT; |
| 848 | |
| 849 | return CONTIG_NONE; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * NOTE: We can have pretty much any combination of contiguousness and |
| 854 | * appending. |
| 855 | * |
| 856 | * The usefulness of APPEND_TAIL is more in that it lets us know that |
| 857 | * we'll have to update the path to that leaf. |
| 858 | */ |
| 859 | enum ocfs2_append_type { |
| 860 | APPEND_NONE = 0, |
| 861 | APPEND_TAIL, |
| 862 | }; |
| 863 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 864 | enum ocfs2_split_type { |
| 865 | SPLIT_NONE = 0, |
| 866 | SPLIT_LEFT, |
| 867 | SPLIT_RIGHT, |
| 868 | }; |
| 869 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 870 | struct ocfs2_insert_type { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 871 | enum ocfs2_split_type ins_split; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 872 | enum ocfs2_append_type ins_appending; |
| 873 | enum ocfs2_contig_type ins_contig; |
| 874 | int ins_contig_index; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 875 | int ins_tree_depth; |
| 876 | }; |
| 877 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 878 | struct ocfs2_merge_ctxt { |
| 879 | enum ocfs2_contig_type c_contig_type; |
| 880 | int c_has_empty_extent; |
| 881 | int c_split_covers_rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 882 | }; |
| 883 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 884 | static int ocfs2_validate_extent_block(struct super_block *sb, |
| 885 | struct buffer_head *bh) |
| 886 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 887 | int rc; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 888 | struct ocfs2_extent_block *eb = |
| 889 | (struct ocfs2_extent_block *)bh->b_data; |
| 890 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 891 | trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr); |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 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)) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 912 | rc = ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 913 | "Extent block #%llu has bad signature %.*s\n", |
| 914 | (unsigned long long)bh->b_blocknr, 7, |
| 915 | eb->h_signature); |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 916 | goto bail; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 920 | rc = ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 921 | "Extent block #%llu has an invalid h_blkno of %llu\n", |
| 922 | (unsigned long long)bh->b_blocknr, |
| 923 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 924 | goto bail; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 928 | rc = ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 929 | "Extent block #%llu has an invalid h_fs_generation of #%u\n", |
| 930 | (unsigned long long)bh->b_blocknr, |
| 931 | le32_to_cpu(eb->h_fs_generation)); |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 932 | goto bail; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 933 | } |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 934 | bail: |
| 935 | return rc; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 938 | 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] | 939 | struct buffer_head **bh) |
| 940 | { |
| 941 | int rc; |
| 942 | struct buffer_head *tmp = *bh; |
| 943 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 944 | rc = ocfs2_read_block(ci, eb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 945 | ocfs2_validate_extent_block); |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 946 | |
| 947 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 948 | if (!rc && !*bh) |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 949 | *bh = tmp; |
| 950 | |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 951 | return rc; |
| 952 | } |
| 953 | |
| 954 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 955 | /* |
| 956 | * How many free extents have we got before we need more meta data? |
| 957 | */ |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 958 | int ocfs2_num_free_extents(struct ocfs2_extent_tree *et) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 959 | { |
| 960 | int retval; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 961 | struct ocfs2_extent_list *el = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 962 | struct ocfs2_extent_block *eb; |
| 963 | struct buffer_head *eb_bh = NULL; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 964 | u64 last_eb_blk = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 965 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 966 | el = et->et_root_el; |
| 967 | last_eb_blk = ocfs2_et_get_last_eb_blk(et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 968 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 969 | if (last_eb_blk) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 970 | retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, |
| 971 | &eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 972 | if (retval < 0) { |
| 973 | mlog_errno(retval); |
| 974 | goto bail; |
| 975 | } |
| 976 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 977 | el = &eb->h_list; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 978 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 979 | |
| 980 | BUG_ON(el->l_tree_depth != 0); |
| 981 | |
| 982 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); |
| 983 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 984 | brelse(eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 985 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 986 | trace_ocfs2_num_free_extents(retval); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 987 | return retval; |
| 988 | } |
| 989 | |
| 990 | /* expects array to already be allocated |
| 991 | * |
| 992 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and |
| 993 | * l_count for you |
| 994 | */ |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 995 | static int ocfs2_create_new_meta_bhs(handle_t *handle, |
| 996 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 997 | int wanted, |
| 998 | struct ocfs2_alloc_context *meta_ac, |
| 999 | struct buffer_head *bhs[]) |
| 1000 | { |
| 1001 | int count, status, i; |
| 1002 | u16 suballoc_bit_start; |
| 1003 | u32 num_got; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1004 | u64 suballoc_loc, first_blkno; |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1005 | struct ocfs2_super *osb = |
| 1006 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1007 | struct ocfs2_extent_block *eb; |
| 1008 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1009 | count = 0; |
| 1010 | while (count < wanted) { |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 1011 | status = ocfs2_claim_metadata(handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1012 | meta_ac, |
| 1013 | wanted - count, |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1014 | &suballoc_loc, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1015 | &suballoc_bit_start, |
| 1016 | &num_got, |
| 1017 | &first_blkno); |
| 1018 | if (status < 0) { |
| 1019 | mlog_errno(status); |
| 1020 | goto bail; |
| 1021 | } |
| 1022 | |
| 1023 | for(i = count; i < (num_got + count); i++) { |
| 1024 | bhs[i] = sb_getblk(osb->sb, first_blkno); |
| 1025 | if (bhs[i] == NULL) { |
Rui Xiang | 7391a29 | 2013-11-12 15:06:54 -0800 | [diff] [blame] | 1026 | status = -ENOMEM; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1027 | mlog_errno(status); |
| 1028 | goto bail; |
| 1029 | } |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1030 | ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1031 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1032 | status = ocfs2_journal_access_eb(handle, et->et_ci, |
| 1033 | bhs[i], |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1034 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1035 | if (status < 0) { |
| 1036 | mlog_errno(status); |
| 1037 | goto bail; |
| 1038 | } |
| 1039 | |
| 1040 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); |
| 1041 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; |
| 1042 | /* Ok, setup the minimal stuff here. */ |
| 1043 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); |
| 1044 | eb->h_blkno = cpu_to_le64(first_blkno); |
| 1045 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1046 | eb->h_suballoc_slot = |
| 1047 | cpu_to_le16(meta_ac->ac_alloc_slot); |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1048 | eb->h_suballoc_loc = cpu_to_le64(suballoc_loc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1049 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 1050 | eb->h_list.l_count = |
| 1051 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 1052 | |
| 1053 | suballoc_bit_start++; |
| 1054 | first_blkno++; |
| 1055 | |
| 1056 | /* We'll also be dirtied by the caller, so |
| 1057 | * this isn't absolutely necessary. */ |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1058 | ocfs2_journal_dirty(handle, bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | count += num_got; |
| 1062 | } |
| 1063 | |
| 1064 | status = 0; |
| 1065 | bail: |
| 1066 | if (status < 0) { |
| 1067 | for(i = 0; i < wanted; i++) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1068 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1069 | bhs[i] = NULL; |
| 1070 | } |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1071 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1072 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1073 | return status; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1077 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 1078 | * |
| 1079 | * Returns the sum of the rightmost extent rec logical offset and |
| 1080 | * cluster count. |
| 1081 | * |
| 1082 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 1083 | * value should be populated into the leftmost new branch records. |
| 1084 | * |
| 1085 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 1086 | * value for the new topmost tree record. |
| 1087 | */ |
| 1088 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 1089 | { |
| 1090 | int i; |
| 1091 | |
| 1092 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1093 | |
| 1094 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1095 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /* |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1099 | * Change range of the branches in the right most path according to the leaf |
| 1100 | * extent block's rightmost record. |
| 1101 | */ |
| 1102 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1103 | struct ocfs2_extent_tree *et) |
| 1104 | { |
| 1105 | int status; |
| 1106 | struct ocfs2_path *path = NULL; |
| 1107 | struct ocfs2_extent_list *el; |
| 1108 | struct ocfs2_extent_rec *rec; |
| 1109 | |
| 1110 | path = ocfs2_new_path_from_et(et); |
| 1111 | if (!path) { |
| 1112 | status = -ENOMEM; |
| 1113 | return status; |
| 1114 | } |
| 1115 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1116 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1117 | if (status < 0) { |
| 1118 | mlog_errno(status); |
| 1119 | goto out; |
| 1120 | } |
| 1121 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 1122 | status = ocfs2_extend_trans(handle, path_num_items(path)); |
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 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1128 | status = ocfs2_journal_access_path(et->et_ci, handle, 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 | |
| 1134 | el = path_leaf_el(path); |
Al Viro | 3a251f0 | 2012-04-13 12:22:00 -0400 | [diff] [blame] | 1135 | rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1]; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1136 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1137 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1138 | |
| 1139 | out: |
| 1140 | ocfs2_free_path(path); |
| 1141 | return status; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1145 | * 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] | 1146 | * 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] | 1147 | * structure. |
| 1148 | * |
| 1149 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 1150 | * for the new last extent block. |
| 1151 | * |
| 1152 | * 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] | 1153 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1154 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1155 | static int ocfs2_add_branch(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1156 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1157 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1158 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1159 | struct ocfs2_alloc_context *meta_ac) |
| 1160 | { |
| 1161 | int status, new_blocks, i; |
| 1162 | u64 next_blkno, new_last_eb_blk; |
| 1163 | struct buffer_head *bh; |
| 1164 | struct buffer_head **new_eb_bhs = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1165 | struct ocfs2_extent_block *eb; |
| 1166 | struct ocfs2_extent_list *eb_el; |
| 1167 | struct ocfs2_extent_list *el; |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1168 | u32 new_cpos, root_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1169 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1170 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1171 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1172 | if (eb_bh) { |
| 1173 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 1174 | el = &eb->h_list; |
| 1175 | } else |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1176 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1177 | |
| 1178 | /* we never add a branch to a leaf. */ |
| 1179 | BUG_ON(!el->l_tree_depth); |
| 1180 | |
| 1181 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 1182 | |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1183 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
| 1184 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 1185 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); |
| 1186 | |
| 1187 | /* |
| 1188 | * If there is a gap before the root end and the real end |
| 1189 | * of the righmost leaf block, we need to remove the gap |
| 1190 | * between new_cpos and root_end first so that the tree |
| 1191 | * is consistent after we add a new branch(it will start |
| 1192 | * from new_cpos). |
| 1193 | */ |
| 1194 | if (root_end > new_cpos) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 1195 | trace_ocfs2_adjust_rightmost_branch( |
| 1196 | (unsigned long long) |
| 1197 | ocfs2_metadata_cache_owner(et->et_ci), |
| 1198 | root_end, new_cpos); |
| 1199 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1200 | status = ocfs2_adjust_rightmost_branch(handle, et); |
Tao Ma | 6b791bc | 2009-06-12 14:18:36 +0800 | [diff] [blame] | 1201 | if (status) { |
| 1202 | mlog_errno(status); |
| 1203 | goto bail; |
| 1204 | } |
| 1205 | } |
| 1206 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1207 | /* allocate the number of new eb blocks we need */ |
| 1208 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 1209 | GFP_KERNEL); |
| 1210 | if (!new_eb_bhs) { |
| 1211 | status = -ENOMEM; |
| 1212 | mlog_errno(status); |
| 1213 | goto bail; |
| 1214 | } |
| 1215 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1216 | status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1217 | meta_ac, new_eb_bhs); |
| 1218 | if (status < 0) { |
| 1219 | mlog_errno(status); |
| 1220 | goto bail; |
| 1221 | } |
| 1222 | |
| 1223 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 1224 | * linked with the rest of the tree. |
| 1225 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 1226 | * |
| 1227 | * when we leave the loop, new_last_eb_blk will point to the |
| 1228 | * newest leaf, and next_blkno will point to the topmost extent |
| 1229 | * block. */ |
| 1230 | next_blkno = new_last_eb_blk = 0; |
| 1231 | for(i = 0; i < new_blocks; i++) { |
| 1232 | bh = new_eb_bhs[i]; |
| 1233 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1234 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1235 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1236 | eb_el = &eb->h_list; |
| 1237 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1238 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1239 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1240 | if (status < 0) { |
| 1241 | mlog_errno(status); |
| 1242 | goto bail; |
| 1243 | } |
| 1244 | |
| 1245 | eb->h_next_leaf_blk = 0; |
| 1246 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 1247 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1248 | /* |
| 1249 | * This actually counts as an empty extent as |
| 1250 | * c_clusters == 0 |
| 1251 | */ |
| 1252 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1253 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1254 | /* |
| 1255 | * eb_el isn't always an interior node, but even leaf |
| 1256 | * nodes want a zero'd flags and reserved field so |
| 1257 | * this gets the whole 32 bits regardless of use. |
| 1258 | */ |
| 1259 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1260 | if (!eb_el->l_tree_depth) |
| 1261 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 1262 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1263 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1264 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 1265 | } |
| 1266 | |
| 1267 | /* This is a bit hairy. We want to update up to three blocks |
| 1268 | * here without leaving any of them in an inconsistent state |
| 1269 | * in case of error. We don't have to worry about |
| 1270 | * journal_dirty erroring as it won't unless we've aborted the |
| 1271 | * handle (in which case we would never be here) so reserving |
| 1272 | * the write with journal_access is all we need to do. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1273 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1274 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1275 | if (status < 0) { |
| 1276 | mlog_errno(status); |
| 1277 | goto bail; |
| 1278 | } |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1279 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1280 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1281 | if (status < 0) { |
| 1282 | mlog_errno(status); |
| 1283 | goto bail; |
| 1284 | } |
| 1285 | if (eb_bh) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1286 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1287 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1288 | if (status < 0) { |
| 1289 | mlog_errno(status); |
| 1290 | goto bail; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /* Link the new branch into the rest of the tree (el will |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1295 | * either be on the root_bh, or the extent block passed in. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1296 | i = le16_to_cpu(el->l_next_free_rec); |
| 1297 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1298 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1299 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1300 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1301 | |
| 1302 | /* fe needs a new last extent block pointer, as does the |
| 1303 | * next_leaf on the previously last-extent-block. */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1304 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1305 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1306 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1307 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 1308 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1309 | ocfs2_journal_dirty(handle, *last_eb_bh); |
| 1310 | ocfs2_journal_dirty(handle, et->et_root_bh); |
| 1311 | if (eb_bh) |
| 1312 | ocfs2_journal_dirty(handle, eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1313 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1314 | /* |
| 1315 | * Some callers want to track the rightmost leaf so pass it |
| 1316 | * back here. |
| 1317 | */ |
| 1318 | brelse(*last_eb_bh); |
| 1319 | get_bh(new_eb_bhs[0]); |
| 1320 | *last_eb_bh = new_eb_bhs[0]; |
| 1321 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1322 | status = 0; |
| 1323 | bail: |
| 1324 | if (new_eb_bhs) { |
| 1325 | for (i = 0; i < new_blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1326 | brelse(new_eb_bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1327 | kfree(new_eb_bhs); |
| 1328 | } |
| 1329 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1330 | return status; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * adds another level to the allocation tree. |
| 1335 | * returns back the new extent block so you can add a branch to it |
| 1336 | * after this call. |
| 1337 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1338 | static int ocfs2_shift_tree_depth(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1339 | struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1340 | struct ocfs2_alloc_context *meta_ac, |
| 1341 | struct buffer_head **ret_new_eb_bh) |
| 1342 | { |
| 1343 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1344 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1345 | struct buffer_head *new_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1346 | struct ocfs2_extent_block *eb; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1347 | struct ocfs2_extent_list *root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1348 | struct ocfs2_extent_list *eb_el; |
| 1349 | |
Joel Becker | 42a5a7a | 2009-02-12 18:49:19 -0800 | [diff] [blame] | 1350 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1351 | &new_eb_bh); |
| 1352 | if (status < 0) { |
| 1353 | mlog_errno(status); |
| 1354 | goto bail; |
| 1355 | } |
| 1356 | |
| 1357 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 1358 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
| 1359 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1360 | |
| 1361 | eb_el = &eb->h_list; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1362 | root_el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1363 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1364 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1365 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1366 | if (status < 0) { |
| 1367 | mlog_errno(status); |
| 1368 | goto bail; |
| 1369 | } |
| 1370 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1371 | /* copy the root extent list data into the new extent block */ |
| 1372 | eb_el->l_tree_depth = root_el->l_tree_depth; |
| 1373 | eb_el->l_next_free_rec = root_el->l_next_free_rec; |
| 1374 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1375 | eb_el->l_recs[i] = root_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1376 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1377 | ocfs2_journal_dirty(handle, new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1378 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 1379 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1380 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1381 | if (status < 0) { |
| 1382 | mlog_errno(status); |
| 1383 | goto bail; |
| 1384 | } |
| 1385 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1386 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 1387 | |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1388 | /* update root_bh now */ |
| 1389 | le16_add_cpu(&root_el->l_tree_depth, 1); |
| 1390 | root_el->l_recs[0].e_cpos = 0; |
| 1391 | root_el->l_recs[0].e_blkno = eb->h_blkno; |
| 1392 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 1393 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 1394 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 1395 | root_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1396 | |
| 1397 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 1398 | * becomes the allocated extent block */ |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1399 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 1400 | 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] | 1401 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1402 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1403 | |
| 1404 | *ret_new_eb_bh = new_eb_bh; |
| 1405 | new_eb_bh = NULL; |
| 1406 | status = 0; |
| 1407 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1408 | brelse(new_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1409 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1410 | return status; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1414 | * Should only be called when there is no space left in any of the |
| 1415 | * leaf nodes. What we want to do is find the lowest tree depth |
| 1416 | * non-leaf extent block with room for new records. There are three |
| 1417 | * valid results of this search: |
| 1418 | * |
| 1419 | * 1) a lowest extent block is found, then we pass it back in |
| 1420 | * *lowest_eb_bh and return '0' |
| 1421 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1422 | * 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] | 1423 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 1424 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1425 | * 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] | 1426 | * which case we return > 0 |
| 1427 | * |
| 1428 | * return status < 0 indicates an error. |
| 1429 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1430 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1431 | struct buffer_head **target_bh) |
| 1432 | { |
| 1433 | int status = 0, i; |
| 1434 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1435 | struct ocfs2_extent_block *eb; |
| 1436 | struct ocfs2_extent_list *el; |
| 1437 | struct buffer_head *bh = NULL; |
| 1438 | struct buffer_head *lowest_bh = NULL; |
| 1439 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1440 | *target_bh = NULL; |
| 1441 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1442 | el = et->et_root_el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1443 | |
| 1444 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 1445 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1446 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1447 | "Owner %llu has empty extent list (next_free_rec == 0)\n", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1448 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1449 | status = -EIO; |
| 1450 | goto bail; |
| 1451 | } |
| 1452 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1453 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1454 | if (!blkno) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1455 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1456 | "Owner %llu has extent list where extent # %d has no physical block start\n", |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1457 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1458 | status = -EIO; |
| 1459 | goto bail; |
| 1460 | } |
| 1461 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1462 | brelse(bh); |
| 1463 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1464 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 1465 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1466 | if (status < 0) { |
| 1467 | mlog_errno(status); |
| 1468 | goto bail; |
| 1469 | } |
| 1470 | |
| 1471 | eb = (struct ocfs2_extent_block *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1472 | el = &eb->h_list; |
| 1473 | |
| 1474 | if (le16_to_cpu(el->l_next_free_rec) < |
| 1475 | le16_to_cpu(el->l_count)) { |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1476 | brelse(lowest_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1477 | lowest_bh = bh; |
| 1478 | get_bh(lowest_bh); |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /* If we didn't find one and the fe doesn't have any room, |
| 1483 | * then return '1' */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1484 | el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1485 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1486 | status = 1; |
| 1487 | |
| 1488 | *target_bh = lowest_bh; |
| 1489 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1490 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1491 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1492 | return status; |
| 1493 | } |
| 1494 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1495 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1496 | * Grow a b-tree so that it has more records. |
| 1497 | * |
| 1498 | * We might shift the tree depth in which case existing paths should |
| 1499 | * be considered invalid. |
| 1500 | * |
| 1501 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1502 | * |
| 1503 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1504 | */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1505 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 1506 | int *final_depth, struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1507 | struct ocfs2_alloc_context *meta_ac) |
| 1508 | { |
| 1509 | int ret, shift; |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 1510 | struct ocfs2_extent_list *el = et->et_root_el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 1511 | int depth = le16_to_cpu(el->l_tree_depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1512 | struct buffer_head *bh = NULL; |
| 1513 | |
| 1514 | BUG_ON(meta_ac == NULL); |
| 1515 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1516 | shift = ocfs2_find_branch_target(et, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1517 | if (shift < 0) { |
| 1518 | ret = shift; |
| 1519 | mlog_errno(ret); |
| 1520 | goto out; |
| 1521 | } |
| 1522 | |
| 1523 | /* We traveled all the way to the bottom of the allocation tree |
| 1524 | * and didn't find room for any more extents - we need to add |
| 1525 | * another tree level */ |
| 1526 | if (shift) { |
| 1527 | BUG_ON(bh); |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 1528 | trace_ocfs2_grow_tree( |
| 1529 | (unsigned long long) |
| 1530 | ocfs2_metadata_cache_owner(et->et_ci), |
| 1531 | depth); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1532 | |
| 1533 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 1534 | * the new extent block (so we can pass that to |
| 1535 | * ocfs2_add_branch). */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1536 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1537 | if (ret < 0) { |
| 1538 | mlog_errno(ret); |
| 1539 | goto out; |
| 1540 | } |
| 1541 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1542 | if (depth == 1) { |
| 1543 | /* |
| 1544 | * Special case: we have room now if we shifted from |
| 1545 | * tree_depth 0, so no more work needs to be done. |
| 1546 | * |
| 1547 | * We won't be calling add_branch, so pass |
| 1548 | * back *last_eb_bh as the new leaf. At depth |
| 1549 | * zero, it should always be null so there's |
| 1550 | * no reason to brelse. |
| 1551 | */ |
| 1552 | BUG_ON(*last_eb_bh); |
| 1553 | get_bh(bh); |
| 1554 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1555 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1556 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* call ocfs2_add_branch to add the final part of the tree with |
| 1560 | * the new data. */ |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 1561 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 1562 | meta_ac); |
| 1563 | if (ret < 0) { |
| 1564 | mlog_errno(ret); |
| 1565 | goto out; |
| 1566 | } |
| 1567 | |
| 1568 | out: |
| 1569 | if (final_depth) |
| 1570 | *final_depth = depth; |
| 1571 | brelse(bh); |
| 1572 | return ret; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1576 | * This function will discard the rightmost extent record. |
| 1577 | */ |
| 1578 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1579 | { |
| 1580 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1581 | int count = le16_to_cpu(el->l_count); |
| 1582 | unsigned int num_bytes; |
| 1583 | |
| 1584 | BUG_ON(!next_free); |
| 1585 | /* This will cause us to go off the end of our extent list. */ |
| 1586 | BUG_ON(next_free >= count); |
| 1587 | |
| 1588 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1589 | |
| 1590 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1591 | } |
| 1592 | |
| 1593 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1594 | struct ocfs2_extent_rec *insert_rec) |
| 1595 | { |
| 1596 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1597 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1598 | struct ocfs2_extent_rec *rec; |
| 1599 | |
| 1600 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1601 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1602 | |
| 1603 | BUG_ON(!next_free); |
| 1604 | |
| 1605 | /* 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] | 1606 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1607 | |
| 1608 | /* |
| 1609 | * The easiest way to approach this is to just remove the |
| 1610 | * empty extent and temporarily decrement next_free. |
| 1611 | */ |
| 1612 | if (has_empty) { |
| 1613 | /* |
| 1614 | * If next_free was 1 (only an empty extent), this |
| 1615 | * loop won't execute, which is fine. We still want |
| 1616 | * the decrement above to happen. |
| 1617 | */ |
| 1618 | for(i = 0; i < (next_free - 1); i++) |
| 1619 | el->l_recs[i] = el->l_recs[i+1]; |
| 1620 | |
| 1621 | next_free--; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * Figure out what the new record index should be. |
| 1626 | */ |
| 1627 | for(i = 0; i < next_free; i++) { |
| 1628 | rec = &el->l_recs[i]; |
| 1629 | |
| 1630 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1631 | break; |
| 1632 | } |
| 1633 | insert_index = i; |
| 1634 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 1635 | trace_ocfs2_rotate_leaf(insert_cpos, insert_index, |
| 1636 | has_empty, next_free, |
| 1637 | le16_to_cpu(el->l_count)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1638 | |
| 1639 | BUG_ON(insert_index < 0); |
| 1640 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1641 | BUG_ON(insert_index > next_free); |
| 1642 | |
| 1643 | /* |
| 1644 | * No need to memmove if we're just adding to the tail. |
| 1645 | */ |
| 1646 | if (insert_index != next_free) { |
| 1647 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1648 | |
| 1649 | num_bytes = next_free - insert_index; |
| 1650 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1651 | memmove(&el->l_recs[insert_index + 1], |
| 1652 | &el->l_recs[insert_index], |
| 1653 | num_bytes); |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * Either we had an empty extent, and need to re-increment or |
| 1658 | * there was no empty extent on a non full rightmost leaf node, |
| 1659 | * in which case we still need to increment. |
| 1660 | */ |
| 1661 | next_free++; |
| 1662 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1663 | /* |
| 1664 | * Make sure none of the math above just messed up our tree. |
| 1665 | */ |
| 1666 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1667 | |
| 1668 | el->l_recs[insert_index] = *insert_rec; |
| 1669 | |
| 1670 | } |
| 1671 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1672 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1673 | { |
| 1674 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1675 | |
| 1676 | BUG_ON(num_recs == 0); |
| 1677 | |
| 1678 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1679 | num_recs--; |
| 1680 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1681 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1682 | memset(&el->l_recs[num_recs], 0, |
| 1683 | sizeof(struct ocfs2_extent_rec)); |
| 1684 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1685 | } |
| 1686 | } |
| 1687 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1688 | /* |
| 1689 | * Create an empty extent record . |
| 1690 | * |
| 1691 | * l_next_free_rec may be updated. |
| 1692 | * |
| 1693 | * If an empty extent already exists do nothing. |
| 1694 | */ |
| 1695 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1696 | { |
| 1697 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1698 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1699 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1700 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1701 | if (next_free == 0) |
| 1702 | goto set_and_inc; |
| 1703 | |
| 1704 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1705 | return; |
| 1706 | |
| 1707 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1708 | "Asked to create an empty extent in a full list:\n" |
| 1709 | "count = %u, tree depth = %u", |
| 1710 | le16_to_cpu(el->l_count), |
| 1711 | le16_to_cpu(el->l_tree_depth)); |
| 1712 | |
| 1713 | ocfs2_shift_records_right(el); |
| 1714 | |
| 1715 | set_and_inc: |
| 1716 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1717 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1722 | * the lowest level tree node which contains a path to both leafs. This |
| 1723 | * resulting set of information can be used to form a complete "subtree" |
| 1724 | * |
| 1725 | * This function is passed two full paths from the dinode down to a |
| 1726 | * pair of adjacent leaves. It's task is to figure out which path |
| 1727 | * index contains the subtree root - this can be the root index itself |
| 1728 | * in a worst-case rotation. |
| 1729 | * |
| 1730 | * The array index of the subtree root is passed back. |
| 1731 | */ |
Tao Ma | 38a04e4 | 2009-11-30 14:32:19 +0800 | [diff] [blame] | 1732 | int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
| 1733 | struct ocfs2_path *left, |
| 1734 | struct ocfs2_path *right) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1735 | { |
| 1736 | int i = 0; |
| 1737 | |
| 1738 | /* |
| 1739 | * Check that the caller passed in two paths from the same tree. |
| 1740 | */ |
| 1741 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1742 | |
| 1743 | do { |
| 1744 | i++; |
| 1745 | |
| 1746 | /* |
| 1747 | * The caller didn't pass two adjacent paths. |
| 1748 | */ |
| 1749 | mlog_bug_on_msg(i > left->p_tree_depth, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1750 | "Owner %llu, left depth %u, right depth %u\n" |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1751 | "left leaf blk %llu, right leaf blk %llu\n", |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 1752 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 1753 | left->p_tree_depth, right->p_tree_depth, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1754 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1755 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1756 | } while (left->p_node[i].bh->b_blocknr == |
| 1757 | right->p_node[i].bh->b_blocknr); |
| 1758 | |
| 1759 | return i - 1; |
| 1760 | } |
| 1761 | |
| 1762 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1763 | |
| 1764 | /* |
| 1765 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1766 | * |
| 1767 | * This code can be called with a cpos larger than the tree, in which |
| 1768 | * case it will return the rightmost path. |
| 1769 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1770 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1771 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1772 | path_insert_t *func, void *data) |
| 1773 | { |
| 1774 | int i, ret = 0; |
| 1775 | u32 range; |
| 1776 | u64 blkno; |
| 1777 | struct buffer_head *bh = NULL; |
| 1778 | struct ocfs2_extent_block *eb; |
| 1779 | struct ocfs2_extent_list *el; |
| 1780 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1781 | |
| 1782 | el = root_el; |
| 1783 | while (el->l_tree_depth) { |
| 1784 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1785 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1786 | "Owner %llu has empty extent list at depth %u\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1787 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1788 | le16_to_cpu(el->l_tree_depth)); |
| 1789 | ret = -EROFS; |
| 1790 | goto out; |
| 1791 | |
| 1792 | } |
| 1793 | |
| 1794 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1795 | rec = &el->l_recs[i]; |
| 1796 | |
| 1797 | /* |
| 1798 | * In the case that cpos is off the allocation |
| 1799 | * tree, this should just wind up returning the |
| 1800 | * rightmost record. |
| 1801 | */ |
| 1802 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1803 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1804 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1809 | if (blkno == 0) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1810 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1811 | "Owner %llu has bad blkno in extent list at depth %u (index %d)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1812 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1813 | le16_to_cpu(el->l_tree_depth), i); |
| 1814 | ret = -EROFS; |
| 1815 | goto out; |
| 1816 | } |
| 1817 | |
| 1818 | brelse(bh); |
| 1819 | bh = NULL; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1820 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1821 | if (ret) { |
| 1822 | mlog_errno(ret); |
| 1823 | goto out; |
| 1824 | } |
| 1825 | |
| 1826 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1827 | el = &eb->h_list; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1828 | |
| 1829 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1830 | le16_to_cpu(el->l_count)) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1831 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1832 | "Owner %llu has bad count in extent list at block %llu (next free=%u, count=%u)\n", |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1833 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1834 | (unsigned long long)bh->b_blocknr, |
| 1835 | le16_to_cpu(el->l_next_free_rec), |
| 1836 | le16_to_cpu(el->l_count)); |
| 1837 | ret = -EROFS; |
| 1838 | goto out; |
| 1839 | } |
| 1840 | |
| 1841 | if (func) |
| 1842 | func(data, bh); |
| 1843 | } |
| 1844 | |
| 1845 | out: |
| 1846 | /* |
| 1847 | * Catch any trailing bh that the loop didn't handle. |
| 1848 | */ |
| 1849 | brelse(bh); |
| 1850 | |
| 1851 | return ret; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Given an initialized path (that is, it has a valid root extent |
| 1856 | * list), this function will traverse the btree in search of the path |
| 1857 | * which would contain cpos. |
| 1858 | * |
| 1859 | * The path traveled is recorded in the path structure. |
| 1860 | * |
| 1861 | * Note that this will not do any comparisons on leaf node extent |
| 1862 | * records, so it will work fine in the case that we just added a tree |
| 1863 | * branch. |
| 1864 | */ |
| 1865 | struct find_path_data { |
| 1866 | int index; |
| 1867 | struct ocfs2_path *path; |
| 1868 | }; |
| 1869 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1870 | { |
| 1871 | struct find_path_data *fp = data; |
| 1872 | |
| 1873 | get_bh(bh); |
| 1874 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1875 | fp->index++; |
| 1876 | } |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 1877 | int ocfs2_find_path(struct ocfs2_caching_info *ci, |
| 1878 | struct ocfs2_path *path, u32 cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1879 | { |
| 1880 | struct find_path_data data; |
| 1881 | |
| 1882 | data.index = 1; |
| 1883 | data.path = path; |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1884 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1885 | find_path_ins, &data); |
| 1886 | } |
| 1887 | |
| 1888 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1889 | { |
| 1890 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1891 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1892 | struct buffer_head **ret = data; |
| 1893 | |
| 1894 | /* We want to retain only the leaf block. */ |
| 1895 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1896 | get_bh(bh); |
| 1897 | *ret = bh; |
| 1898 | } |
| 1899 | } |
| 1900 | /* |
| 1901 | * Find the leaf block in the tree which would contain cpos. No |
| 1902 | * checking of the actual leaf is done. |
| 1903 | * |
| 1904 | * Some paths want to call this instead of allocating a path structure |
| 1905 | * and calling ocfs2_find_path(). |
| 1906 | * |
| 1907 | * This function doesn't handle non btree extent lists. |
| 1908 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1909 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
| 1910 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1911 | struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1912 | { |
| 1913 | int ret; |
| 1914 | struct buffer_head *bh = NULL; |
| 1915 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 1916 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1917 | if (ret) { |
| 1918 | mlog_errno(ret); |
| 1919 | goto out; |
| 1920 | } |
| 1921 | |
| 1922 | *leaf_bh = bh; |
| 1923 | out: |
| 1924 | return ret; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1929 | * |
| 1930 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1931 | * we need to fix up the extent records above the changes to reflect |
| 1932 | * the new changes. |
| 1933 | * |
| 1934 | * left_rec: the record on the left. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1935 | * right_rec: the record to the right of left_rec |
| 1936 | * right_child_el: is the child list pointed to by right_rec |
| 1937 | * |
| 1938 | * By definition, this only works on interior nodes. |
| 1939 | */ |
| 1940 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1941 | struct ocfs2_extent_rec *right_rec, |
| 1942 | struct ocfs2_extent_list *right_child_el) |
| 1943 | { |
| 1944 | u32 left_clusters, right_end; |
| 1945 | |
| 1946 | /* |
| 1947 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1948 | * the leftmost record in their child list. Their cluster |
| 1949 | * count covers the full theoretical range of their child list |
| 1950 | * - the range between their cpos and the cpos of the record |
| 1951 | * immediately to their right. |
| 1952 | */ |
| 1953 | 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] | 1954 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
| 1955 | BUG_ON(right_child_el->l_tree_depth); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1956 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1957 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1958 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1959 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1960 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1961 | |
| 1962 | /* |
| 1963 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1964 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1965 | * updating e_cpos to keep the same highest cluster count. |
| 1966 | */ |
| 1967 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1968 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1969 | |
| 1970 | right_rec->e_cpos = left_rec->e_cpos; |
| 1971 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 1972 | |
| 1973 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1974 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | /* |
| 1978 | * Adjust the adjacent root node records involved in a |
| 1979 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 1980 | * find it's index in the root list. |
| 1981 | */ |
| 1982 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 1983 | struct ocfs2_extent_list *left_el, |
| 1984 | struct ocfs2_extent_list *right_el, |
| 1985 | u64 left_el_blkno) |
| 1986 | { |
| 1987 | int i; |
| 1988 | |
| 1989 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 1990 | le16_to_cpu(left_el->l_tree_depth)); |
| 1991 | |
| 1992 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 1993 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 1994 | break; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * The path walking code should have never returned a root and |
| 1999 | * two paths which are not adjacent. |
| 2000 | */ |
| 2001 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 2002 | |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 2003 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2004 | &root_el->l_recs[i + 1], right_el); |
| 2005 | } |
| 2006 | |
| 2007 | /* |
| 2008 | * We've changed a leaf block (in right_path) and need to reflect that |
| 2009 | * change back up the subtree. |
| 2010 | * |
| 2011 | * This happens in multiple places: |
| 2012 | * - When we've moved an extent record from the left path leaf to the right |
| 2013 | * path leaf to make room for an empty extent in the left path leaf. |
| 2014 | * - When our insert into the right path leaf is at the leftmost edge |
| 2015 | * and requires an update of the path immediately to it's left. This |
| 2016 | * 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] | 2017 | * - When we've adjusted the last extent record in the left path leaf and the |
| 2018 | * 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] | 2019 | */ |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2020 | static void ocfs2_complete_edge_insert(handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2021 | struct ocfs2_path *left_path, |
| 2022 | struct ocfs2_path *right_path, |
| 2023 | int subtree_index) |
| 2024 | { |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2025 | int i, idx; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2026 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 2027 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 2028 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2029 | |
| 2030 | /* |
| 2031 | * Update the counts and position values within all the |
| 2032 | * interior nodes to reflect the leaf rotation we just did. |
| 2033 | * |
| 2034 | * The root node is handled below the loop. |
| 2035 | * |
| 2036 | * We begin the loop with right_el and left_el pointing to the |
| 2037 | * leaf lists and work our way up. |
| 2038 | * |
| 2039 | * NOTE: within this loop, left_el and right_el always refer |
| 2040 | * to the *child* lists. |
| 2041 | */ |
| 2042 | left_el = path_leaf_el(left_path); |
| 2043 | right_el = path_leaf_el(right_path); |
| 2044 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 2045 | trace_ocfs2_complete_edge_insert(i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2046 | |
| 2047 | /* |
| 2048 | * One nice property of knowing that all of these |
| 2049 | * nodes are below the root is that we only deal with |
| 2050 | * the leftmost right node record and the rightmost |
| 2051 | * left node record. |
| 2052 | */ |
| 2053 | el = left_path->p_node[i].el; |
| 2054 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2055 | left_rec = &el->l_recs[idx]; |
| 2056 | |
| 2057 | el = right_path->p_node[i].el; |
| 2058 | right_rec = &el->l_recs[0]; |
| 2059 | |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 2060 | ocfs2_adjust_adjacent_records(left_rec, right_rec, right_el); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2061 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2062 | ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 2063 | ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2064 | |
| 2065 | /* |
| 2066 | * Setup our list pointers now so that the current |
| 2067 | * parents become children in the next iteration. |
| 2068 | */ |
| 2069 | left_el = left_path->p_node[i].el; |
| 2070 | right_el = right_path->p_node[i].el; |
| 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * At the root node, adjust the two adjacent records which |
| 2075 | * begin our path to the leaves. |
| 2076 | */ |
| 2077 | |
| 2078 | el = left_path->p_node[subtree_index].el; |
| 2079 | left_el = left_path->p_node[subtree_index + 1].el; |
| 2080 | right_el = right_path->p_node[subtree_index + 1].el; |
| 2081 | |
| 2082 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 2083 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 2084 | |
| 2085 | root_bh = left_path->p_node[subtree_index].bh; |
| 2086 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2087 | ocfs2_journal_dirty(handle, root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2090 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
| 2091 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2092 | struct ocfs2_path *left_path, |
| 2093 | struct ocfs2_path *right_path, |
| 2094 | int subtree_index) |
| 2095 | { |
| 2096 | int ret, i; |
| 2097 | struct buffer_head *right_leaf_bh; |
| 2098 | struct buffer_head *left_leaf_bh = NULL; |
| 2099 | struct buffer_head *root_bh; |
| 2100 | struct ocfs2_extent_list *right_el, *left_el; |
| 2101 | struct ocfs2_extent_rec move_rec; |
| 2102 | |
| 2103 | left_leaf_bh = path_leaf_bh(left_path); |
| 2104 | left_el = path_leaf_el(left_path); |
| 2105 | |
| 2106 | if (left_el->l_next_free_rec != left_el->l_count) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2107 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 2108 | "Inode %llu has non-full interior leaf node %llu (next free = %u)\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2109 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2110 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 2111 | le16_to_cpu(left_el->l_next_free_rec)); |
| 2112 | return -EROFS; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * This extent block may already have an empty record, so we |
| 2117 | * return early if so. |
| 2118 | */ |
| 2119 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 2120 | return 0; |
| 2121 | |
| 2122 | root_bh = left_path->p_node[subtree_index].bh; |
| 2123 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2124 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2125 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2126 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2127 | if (ret) { |
| 2128 | mlog_errno(ret); |
| 2129 | goto out; |
| 2130 | } |
| 2131 | |
| 2132 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2133 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2134 | right_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2135 | if (ret) { |
| 2136 | mlog_errno(ret); |
| 2137 | goto out; |
| 2138 | } |
| 2139 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2140 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2141 | left_path, i); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2142 | if (ret) { |
| 2143 | mlog_errno(ret); |
| 2144 | goto out; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | right_leaf_bh = path_leaf_bh(right_path); |
| 2149 | right_el = path_leaf_el(right_path); |
| 2150 | |
| 2151 | /* This is a code error, not a disk corruption. */ |
| 2152 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 2153 | "because rightmost leaf block %llu is empty\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2154 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2155 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 2156 | |
| 2157 | ocfs2_create_empty_extent(right_el); |
| 2158 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2159 | ocfs2_journal_dirty(handle, right_leaf_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2160 | |
| 2161 | /* Do the copy now. */ |
| 2162 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 2163 | move_rec = left_el->l_recs[i]; |
| 2164 | right_el->l_recs[0] = move_rec; |
| 2165 | |
| 2166 | /* |
| 2167 | * Clear out the record we just copied and shift everything |
| 2168 | * over, leaving an empty extent in the left leaf. |
| 2169 | * |
| 2170 | * We temporarily subtract from next_free_rec so that the |
| 2171 | * shift will lose the tail record (which is now defunct). |
| 2172 | */ |
| 2173 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 2174 | ocfs2_shift_records_right(left_el); |
| 2175 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2176 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 2177 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2178 | ocfs2_journal_dirty(handle, left_leaf_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2179 | |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2180 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 2181 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2182 | |
| 2183 | out: |
| 2184 | return ret; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * Given a full path, determine what cpos value would return us a path |
| 2189 | * containing the leaf immediately to the left of the current one. |
| 2190 | * |
| 2191 | * Will return zero if the path passed in is already the leftmost path. |
| 2192 | */ |
Tristan Ye | ee149a7 | 2010-05-11 17:54:44 +0800 | [diff] [blame] | 2193 | int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 2194 | struct ocfs2_path *path, u32 *cpos) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2195 | { |
| 2196 | int i, j, ret = 0; |
| 2197 | u64 blkno; |
| 2198 | struct ocfs2_extent_list *el; |
| 2199 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2200 | BUG_ON(path->p_tree_depth == 0); |
| 2201 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2202 | *cpos = 0; |
| 2203 | |
| 2204 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2205 | |
| 2206 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2207 | i = path->p_tree_depth - 1; |
| 2208 | while (i >= 0) { |
| 2209 | el = path->p_node[i].el; |
| 2210 | |
| 2211 | /* |
| 2212 | * Find the extent record just before the one in our |
| 2213 | * path. |
| 2214 | */ |
| 2215 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2216 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2217 | if (j == 0) { |
| 2218 | if (i == 0) { |
| 2219 | /* |
| 2220 | * We've determined that the |
| 2221 | * path specified is already |
| 2222 | * the leftmost one - return a |
| 2223 | * cpos of zero. |
| 2224 | */ |
| 2225 | goto out; |
| 2226 | } |
| 2227 | /* |
| 2228 | * The leftmost record points to our |
| 2229 | * leaf - we need to travel up the |
| 2230 | * tree one level. |
| 2231 | */ |
| 2232 | goto next_node; |
| 2233 | } |
| 2234 | |
| 2235 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2236 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 2237 | &el->l_recs[j - 1]); |
| 2238 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2239 | goto out; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | /* |
| 2244 | * If we got here, we never found a valid node where |
| 2245 | * the tree indicated one should be. |
| 2246 | */ |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 2247 | ocfs2_error(sb, "Invalid extent tree at extent block %llu\n", |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2248 | (unsigned long long)blkno); |
| 2249 | ret = -EROFS; |
| 2250 | goto out; |
| 2251 | |
| 2252 | next_node: |
| 2253 | blkno = path->p_node[i].bh->b_blocknr; |
| 2254 | i--; |
| 2255 | } |
| 2256 | |
| 2257 | out: |
| 2258 | return ret; |
| 2259 | } |
| 2260 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2261 | /* |
| 2262 | * Extend the transaction by enough credits to complete the rotation, |
| 2263 | * and still leave at least the original number of credits allocated |
| 2264 | * to this transaction. |
| 2265 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2266 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2267 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2268 | struct ocfs2_path *path) |
| 2269 | { |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 2270 | int ret = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2271 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2272 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 2273 | if (handle->h_buffer_credits < credits) |
Tao Ma | c18b812 | 2009-08-18 11:44:07 +0800 | [diff] [blame] | 2274 | ret = ocfs2_extend_trans(handle, |
| 2275 | credits - handle->h_buffer_credits); |
Tao Ma | c18b812 | 2009-08-18 11:44:07 +0800 | [diff] [blame] | 2276 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 2277 | return ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | * Trap the case where we're inserting into the theoretical range past |
| 2282 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 2283 | * whose cpos is less than ours into the right leaf. |
| 2284 | * |
| 2285 | * It's only necessary to look at the rightmost record of the left |
| 2286 | * leaf because the logic that calls us should ensure that the |
| 2287 | * theoretical ranges in the path components above the leaves are |
| 2288 | * correct. |
| 2289 | */ |
| 2290 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 2291 | u32 insert_cpos) |
| 2292 | { |
| 2293 | struct ocfs2_extent_list *left_el; |
| 2294 | struct ocfs2_extent_rec *rec; |
| 2295 | int next_free; |
| 2296 | |
| 2297 | left_el = path_leaf_el(left_path); |
| 2298 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 2299 | rec = &left_el->l_recs[next_free - 1]; |
| 2300 | |
| 2301 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 2302 | return 1; |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2306 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 2307 | { |
| 2308 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 2309 | unsigned int range; |
| 2310 | struct ocfs2_extent_rec *rec; |
| 2311 | |
| 2312 | if (next_free == 0) |
| 2313 | return 0; |
| 2314 | |
| 2315 | rec = &el->l_recs[0]; |
| 2316 | if (ocfs2_is_empty_extent(rec)) { |
| 2317 | /* Empty list. */ |
| 2318 | if (next_free == 1) |
| 2319 | return 0; |
| 2320 | rec = &el->l_recs[1]; |
| 2321 | } |
| 2322 | |
| 2323 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2324 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 2325 | return 1; |
| 2326 | return 0; |
| 2327 | } |
| 2328 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2329 | /* |
| 2330 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 2331 | * |
| 2332 | * The path to the rightmost leaf should be passed in. |
| 2333 | * |
| 2334 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 2335 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2336 | * Upon successful return from this function: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2337 | * |
| 2338 | * - The 'right_path' array will contain a path to the leaf block |
| 2339 | * whose range contains e_cpos. |
| 2340 | * - That leaf block will have a single empty extent in list index 0. |
| 2341 | * - In the case that the rotation requires a post-insert update, |
| 2342 | * *ret_left_path will contain a valid path which can be passed to |
| 2343 | * ocfs2_insert_path(). |
| 2344 | */ |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 2345 | static int ocfs2_rotate_tree_right(handle_t *handle, |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2346 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2347 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2348 | u32 insert_cpos, |
| 2349 | struct ocfs2_path *right_path, |
| 2350 | struct ocfs2_path **ret_left_path) |
| 2351 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2352 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2353 | u32 cpos; |
| 2354 | struct ocfs2_path *left_path = NULL; |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2355 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2356 | |
| 2357 | *ret_left_path = NULL; |
| 2358 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2359 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2360 | if (!left_path) { |
| 2361 | ret = -ENOMEM; |
| 2362 | mlog_errno(ret); |
| 2363 | goto out; |
| 2364 | } |
| 2365 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2366 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2367 | if (ret) { |
| 2368 | mlog_errno(ret); |
| 2369 | goto out; |
| 2370 | } |
| 2371 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 2372 | trace_ocfs2_rotate_tree_right( |
| 2373 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2374 | insert_cpos, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2375 | |
| 2376 | /* |
| 2377 | * What we want to do here is: |
| 2378 | * |
| 2379 | * 1) Start with the rightmost path. |
| 2380 | * |
| 2381 | * 2) Determine a path to the leaf block directly to the left |
| 2382 | * of that leaf. |
| 2383 | * |
| 2384 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 2385 | * which contains a path to both leaves. |
| 2386 | * |
| 2387 | * 4) Rotate the subtree. |
| 2388 | * |
| 2389 | * 5) Find the next subtree by considering the left path to be |
| 2390 | * the new right path. |
| 2391 | * |
| 2392 | * The check at the top of this while loop also accepts |
| 2393 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 2394 | * value to get us the left path - insert_cpos might very well |
| 2395 | * be filling that hole. |
| 2396 | * |
| 2397 | * Stop at a cpos of '0' because we either started at the |
| 2398 | * leftmost branch (i.e., a tree with one branch and a |
| 2399 | * rotation inside of it), or we've gone as far as we can in |
| 2400 | * rotating subtrees. |
| 2401 | */ |
| 2402 | while (cpos && insert_cpos <= cpos) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 2403 | trace_ocfs2_rotate_tree_right( |
| 2404 | (unsigned long long) |
| 2405 | ocfs2_metadata_cache_owner(et->et_ci), |
| 2406 | insert_cpos, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2407 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2408 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2409 | if (ret) { |
| 2410 | mlog_errno(ret); |
| 2411 | goto out; |
| 2412 | } |
| 2413 | |
| 2414 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 2415 | path_leaf_bh(right_path), |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2416 | "Owner %llu: error during insert of %u " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2417 | "(left path cpos %u) results in two identical " |
| 2418 | "paths ending at %llu\n", |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2419 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 2420 | insert_cpos, cpos, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2421 | (unsigned long long) |
| 2422 | path_leaf_bh(left_path)->b_blocknr); |
| 2423 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2424 | if (split == SPLIT_NONE && |
| 2425 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2426 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2427 | |
| 2428 | /* |
| 2429 | * We've rotated the tree as much as we |
| 2430 | * should. The rest is up to |
| 2431 | * ocfs2_insert_path() to complete, after the |
| 2432 | * record insertion. We indicate this |
| 2433 | * situation by returning the left path. |
| 2434 | * |
| 2435 | * The reason we don't adjust the records here |
| 2436 | * before the record insert is that an error |
| 2437 | * later might break the rule where a parent |
| 2438 | * record e_cpos will reflect the actual |
| 2439 | * e_cpos of the 1st nonempty record of the |
| 2440 | * child list. |
| 2441 | */ |
| 2442 | *ret_left_path = left_path; |
| 2443 | goto out_ret_path; |
| 2444 | } |
| 2445 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2446 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2447 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 2448 | trace_ocfs2_rotate_subtree(start, |
| 2449 | (unsigned long long) |
| 2450 | right_path->p_node[start].bh->b_blocknr, |
| 2451 | right_path->p_tree_depth); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2452 | |
| 2453 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2454 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2455 | if (ret) { |
| 2456 | mlog_errno(ret); |
| 2457 | goto out; |
| 2458 | } |
| 2459 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2460 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2461 | right_path, start); |
| 2462 | if (ret) { |
| 2463 | mlog_errno(ret); |
| 2464 | goto out; |
| 2465 | } |
| 2466 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2467 | if (split != SPLIT_NONE && |
| 2468 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 2469 | insert_cpos)) { |
| 2470 | /* |
| 2471 | * A rotate moves the rightmost left leaf |
| 2472 | * record over to the leftmost right leaf |
| 2473 | * slot. If we're doing an extent split |
| 2474 | * instead of a real insert, then we have to |
| 2475 | * check that the extent to be split wasn't |
| 2476 | * just moved over. If it was, then we can |
| 2477 | * exit here, passing left_path back - |
| 2478 | * ocfs2_split_extent() is smart enough to |
| 2479 | * search both leaves. |
| 2480 | */ |
| 2481 | *ret_left_path = left_path; |
| 2482 | goto out_ret_path; |
| 2483 | } |
| 2484 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2485 | /* |
| 2486 | * There is no need to re-read the next right path |
| 2487 | * as we know that it'll be our current left |
| 2488 | * path. Optimize by copying values instead. |
| 2489 | */ |
| 2490 | ocfs2_mv_path(right_path, left_path); |
| 2491 | |
Joel Becker | 5c601ab | 2009-02-12 19:10:13 -0800 | [diff] [blame] | 2492 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2493 | if (ret) { |
| 2494 | mlog_errno(ret); |
| 2495 | goto out; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | out: |
| 2500 | ocfs2_free_path(left_path); |
| 2501 | |
| 2502 | out_ret_path: |
| 2503 | return ret; |
| 2504 | } |
| 2505 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2506 | static int ocfs2_update_edge_lengths(handle_t *handle, |
| 2507 | struct ocfs2_extent_tree *et, |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 2508 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2509 | { |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2510 | int i, idx, ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2511 | struct ocfs2_extent_rec *rec; |
| 2512 | struct ocfs2_extent_list *el; |
| 2513 | struct ocfs2_extent_block *eb; |
| 2514 | u32 range; |
| 2515 | |
Joel Becker | 09106ba | 2009-02-12 19:43:57 -0800 | [diff] [blame] | 2516 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2517 | if (ret) { |
| 2518 | mlog_errno(ret); |
| 2519 | goto out; |
| 2520 | } |
| 2521 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2522 | /* Path should always be rightmost. */ |
| 2523 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2524 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 2525 | |
| 2526 | el = &eb->h_list; |
| 2527 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 2528 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2529 | rec = &el->l_recs[idx]; |
| 2530 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 2531 | |
| 2532 | for (i = 0; i < path->p_tree_depth; i++) { |
| 2533 | el = path->p_node[i].el; |
| 2534 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 2535 | rec = &el->l_recs[idx]; |
| 2536 | |
| 2537 | rec->e_int_clusters = cpu_to_le32(range); |
| 2538 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 2539 | |
| 2540 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 2541 | } |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2542 | out: |
| 2543 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2546 | static void ocfs2_unlink_path(handle_t *handle, |
| 2547 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2548 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2549 | struct ocfs2_path *path, int unlink_start) |
| 2550 | { |
| 2551 | int ret, i; |
| 2552 | struct ocfs2_extent_block *eb; |
| 2553 | struct ocfs2_extent_list *el; |
| 2554 | struct buffer_head *bh; |
| 2555 | |
| 2556 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 2557 | bh = path->p_node[i].bh; |
| 2558 | |
| 2559 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 2560 | /* |
| 2561 | * Not all nodes might have had their final count |
| 2562 | * decremented by the caller - handle this here. |
| 2563 | */ |
| 2564 | el = &eb->h_list; |
| 2565 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2566 | mlog(ML_ERROR, |
| 2567 | "Inode %llu, attempted to remove extent block " |
| 2568 | "%llu with %u records\n", |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2569 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2570 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2571 | le16_to_cpu(el->l_next_free_rec)); |
| 2572 | |
| 2573 | ocfs2_journal_dirty(handle, bh); |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2574 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2575 | continue; |
| 2576 | } |
| 2577 | |
| 2578 | el->l_next_free_rec = 0; |
| 2579 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2580 | |
| 2581 | ocfs2_journal_dirty(handle, bh); |
| 2582 | |
| 2583 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2584 | if (ret) |
| 2585 | mlog_errno(ret); |
| 2586 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2587 | ocfs2_remove_from_cache(et->et_ci, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2591 | static void ocfs2_unlink_subtree(handle_t *handle, |
| 2592 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2593 | struct ocfs2_path *left_path, |
| 2594 | struct ocfs2_path *right_path, |
| 2595 | int subtree_index, |
| 2596 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2597 | { |
| 2598 | int i; |
| 2599 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2600 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2601 | struct ocfs2_extent_list *el; |
| 2602 | struct ocfs2_extent_block *eb; |
| 2603 | |
| 2604 | el = path_leaf_el(left_path); |
| 2605 | |
| 2606 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2607 | |
| 2608 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2609 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2610 | break; |
| 2611 | |
| 2612 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2613 | |
| 2614 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2615 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2616 | |
| 2617 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2618 | eb->h_next_leaf_blk = 0; |
| 2619 | |
| 2620 | ocfs2_journal_dirty(handle, root_bh); |
| 2621 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2622 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2623 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2624 | subtree_index + 1); |
| 2625 | } |
| 2626 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2627 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
| 2628 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2629 | struct ocfs2_path *left_path, |
| 2630 | struct ocfs2_path *right_path, |
| 2631 | int subtree_index, |
| 2632 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2633 | int *deleted) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2634 | { |
| 2635 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2636 | 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] | 2637 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2638 | struct ocfs2_extent_block *eb; |
| 2639 | |
| 2640 | *deleted = 0; |
| 2641 | |
| 2642 | right_leaf_el = path_leaf_el(right_path); |
| 2643 | left_leaf_el = path_leaf_el(left_path); |
| 2644 | root_bh = left_path->p_node[subtree_index].bh; |
| 2645 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2646 | |
| 2647 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2648 | return 0; |
| 2649 | |
| 2650 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2651 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2652 | /* |
| 2653 | * It's legal for us to proceed if the right leaf is |
| 2654 | * the rightmost one and it has an empty extent. There |
| 2655 | * are two cases to handle - whether the leaf will be |
| 2656 | * empty after removal or not. If the leaf isn't empty |
| 2657 | * then just remove the empty extent up front. The |
| 2658 | * next block will handle empty leaves by flagging |
| 2659 | * them for unlink. |
| 2660 | * |
| 2661 | * Non rightmost leaves will throw -EAGAIN and the |
| 2662 | * caller can manually move the subtree and retry. |
| 2663 | */ |
| 2664 | |
| 2665 | if (eb->h_next_leaf_blk != 0ULL) |
| 2666 | return -EAGAIN; |
| 2667 | |
| 2668 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2669 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2670 | path_leaf_bh(right_path), |
| 2671 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2672 | if (ret) { |
| 2673 | mlog_errno(ret); |
| 2674 | goto out; |
| 2675 | } |
| 2676 | |
| 2677 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2678 | } else |
| 2679 | right_has_empty = 1; |
| 2680 | } |
| 2681 | |
| 2682 | if (eb->h_next_leaf_blk == 0ULL && |
| 2683 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2684 | /* |
| 2685 | * We have to update i_last_eb_blk during the meta |
| 2686 | * data delete. |
| 2687 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 2688 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2689 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2690 | if (ret) { |
| 2691 | mlog_errno(ret); |
| 2692 | goto out; |
| 2693 | } |
| 2694 | |
| 2695 | del_right_subtree = 1; |
| 2696 | } |
| 2697 | |
| 2698 | /* |
| 2699 | * Getting here with an empty extent in the right path implies |
| 2700 | * that it's the rightmost path and will be deleted. |
| 2701 | */ |
| 2702 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2703 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2704 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2705 | subtree_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2706 | if (ret) { |
| 2707 | mlog_errno(ret); |
| 2708 | goto out; |
| 2709 | } |
| 2710 | |
| 2711 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2712 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2713 | right_path, i); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2714 | if (ret) { |
| 2715 | mlog_errno(ret); |
| 2716 | goto out; |
| 2717 | } |
| 2718 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2719 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2720 | left_path, i); |
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 | |
| 2727 | if (!right_has_empty) { |
| 2728 | /* |
| 2729 | * Only do this if we're moving a real |
| 2730 | * record. Otherwise, the action is delayed until |
| 2731 | * after removal of the right path in which case we |
| 2732 | * can do a simple shift to remove the empty extent. |
| 2733 | */ |
| 2734 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2735 | memset(&right_leaf_el->l_recs[0], 0, |
| 2736 | sizeof(struct ocfs2_extent_rec)); |
| 2737 | } |
| 2738 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2739 | /* |
| 2740 | * Move recs over to get rid of empty extent, decrease |
| 2741 | * next_free. This is allowed to remove the last |
| 2742 | * extent in our leaf (setting l_next_free_rec to |
| 2743 | * zero) - the delete code below won't care. |
| 2744 | */ |
| 2745 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2746 | } |
| 2747 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2748 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2749 | ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2750 | |
| 2751 | if (del_right_subtree) { |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 2752 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2753 | subtree_index, dealloc); |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 2754 | ret = ocfs2_update_edge_lengths(handle, et, left_path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 2755 | if (ret) { |
| 2756 | mlog_errno(ret); |
| 2757 | goto out; |
| 2758 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2759 | |
| 2760 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 2761 | 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] | 2762 | |
| 2763 | /* |
| 2764 | * Removal of the extent in the left leaf was skipped |
| 2765 | * above so we could delete the right path |
| 2766 | * 1st. |
| 2767 | */ |
| 2768 | if (right_has_empty) |
| 2769 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2770 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2771 | ocfs2_journal_dirty(handle, et_root_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2772 | |
| 2773 | *deleted = 1; |
| 2774 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 2775 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2776 | subtree_index); |
| 2777 | |
| 2778 | out: |
| 2779 | return ret; |
| 2780 | } |
| 2781 | |
| 2782 | /* |
| 2783 | * Given a full path, determine what cpos value would return us a path |
| 2784 | * containing the leaf immediately to the right of the current one. |
| 2785 | * |
| 2786 | * Will return zero if the path passed in is already the rightmost path. |
| 2787 | * |
| 2788 | * This looks similar, but is subtly different to |
| 2789 | * ocfs2_find_cpos_for_left_leaf(). |
| 2790 | */ |
Tao Ma | 38a04e4 | 2009-11-30 14:32:19 +0800 | [diff] [blame] | 2791 | int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2792 | struct ocfs2_path *path, u32 *cpos) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2793 | { |
| 2794 | int i, j, ret = 0; |
| 2795 | u64 blkno; |
| 2796 | struct ocfs2_extent_list *el; |
| 2797 | |
| 2798 | *cpos = 0; |
| 2799 | |
| 2800 | if (path->p_tree_depth == 0) |
| 2801 | return 0; |
| 2802 | |
| 2803 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2804 | |
| 2805 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2806 | i = path->p_tree_depth - 1; |
| 2807 | while (i >= 0) { |
| 2808 | int next_free; |
| 2809 | |
| 2810 | el = path->p_node[i].el; |
| 2811 | |
| 2812 | /* |
| 2813 | * Find the extent record just after the one in our |
| 2814 | * path. |
| 2815 | */ |
| 2816 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2817 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2818 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2819 | if (j == (next_free - 1)) { |
| 2820 | if (i == 0) { |
| 2821 | /* |
| 2822 | * We've determined that the |
| 2823 | * path specified is already |
| 2824 | * the rightmost one - return a |
| 2825 | * cpos of zero. |
| 2826 | */ |
| 2827 | goto out; |
| 2828 | } |
| 2829 | /* |
| 2830 | * The rightmost record points to our |
| 2831 | * leaf - we need to travel up the |
| 2832 | * tree one level. |
| 2833 | */ |
| 2834 | goto next_node; |
| 2835 | } |
| 2836 | |
| 2837 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2838 | goto out; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | /* |
| 2843 | * If we got here, we never found a valid node where |
| 2844 | * the tree indicated one should be. |
| 2845 | */ |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 2846 | ocfs2_error(sb, "Invalid extent tree at extent block %llu\n", |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2847 | (unsigned long long)blkno); |
| 2848 | ret = -EROFS; |
| 2849 | goto out; |
| 2850 | |
| 2851 | next_node: |
| 2852 | blkno = path->p_node[i].bh->b_blocknr; |
| 2853 | i--; |
| 2854 | } |
| 2855 | |
| 2856 | out: |
| 2857 | return ret; |
| 2858 | } |
| 2859 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2860 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
| 2861 | struct ocfs2_extent_tree *et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2862 | struct ocfs2_path *path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2863 | { |
| 2864 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2865 | struct buffer_head *bh = path_leaf_bh(path); |
| 2866 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2867 | |
| 2868 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2869 | return 0; |
| 2870 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 2871 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2872 | path_num_items(path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2873 | if (ret) { |
| 2874 | mlog_errno(ret); |
| 2875 | goto out; |
| 2876 | } |
| 2877 | |
| 2878 | ocfs2_remove_empty_extent(el); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2879 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2880 | |
| 2881 | out: |
| 2882 | return ret; |
| 2883 | } |
| 2884 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2885 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
| 2886 | struct ocfs2_extent_tree *et, |
| 2887 | int orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2888 | struct ocfs2_path *path, |
| 2889 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2890 | struct ocfs2_path **empty_extent_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2891 | { |
| 2892 | int ret, subtree_root, deleted; |
| 2893 | u32 right_cpos; |
| 2894 | struct ocfs2_path *left_path = NULL; |
| 2895 | struct ocfs2_path *right_path = NULL; |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2896 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2897 | |
Xue jiufei | 099768b | 2015-06-24 16:55:09 -0700 | [diff] [blame] | 2898 | if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))) |
| 2899 | return 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2900 | |
| 2901 | *empty_extent_path = NULL; |
| 2902 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2903 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2904 | if (ret) { |
| 2905 | mlog_errno(ret); |
| 2906 | goto out; |
| 2907 | } |
| 2908 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2909 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2910 | if (!left_path) { |
| 2911 | ret = -ENOMEM; |
| 2912 | mlog_errno(ret); |
| 2913 | goto out; |
| 2914 | } |
| 2915 | |
| 2916 | ocfs2_cp_path(left_path, path); |
| 2917 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 2918 | right_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2919 | if (!right_path) { |
| 2920 | ret = -ENOMEM; |
| 2921 | mlog_errno(ret); |
| 2922 | goto out; |
| 2923 | } |
| 2924 | |
| 2925 | while (right_cpos) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 2926 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2927 | if (ret) { |
| 2928 | mlog_errno(ret); |
| 2929 | goto out; |
| 2930 | } |
| 2931 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 2932 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2933 | right_path); |
| 2934 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 2935 | trace_ocfs2_rotate_subtree(subtree_root, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2936 | (unsigned long long) |
| 2937 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 2938 | right_path->p_tree_depth); |
| 2939 | |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 2940 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2941 | orig_credits, left_path); |
| 2942 | if (ret) { |
| 2943 | mlog_errno(ret); |
| 2944 | goto out; |
| 2945 | } |
| 2946 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2947 | /* |
| 2948 | * Caller might still want to make changes to the |
| 2949 | * tree root, so re-add it to the journal here. |
| 2950 | */ |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2951 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2952 | left_path, 0); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 2953 | if (ret) { |
| 2954 | mlog_errno(ret); |
| 2955 | goto out; |
| 2956 | } |
| 2957 | |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2958 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2959 | right_path, subtree_root, |
Joel Becker | 1e2dd63 | 2009-02-12 19:45:28 -0800 | [diff] [blame] | 2960 | dealloc, &deleted); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2961 | if (ret == -EAGAIN) { |
| 2962 | /* |
| 2963 | * The rotation has to temporarily stop due to |
| 2964 | * the right subtree having an empty |
| 2965 | * extent. Pass it back to the caller for a |
| 2966 | * fixup. |
| 2967 | */ |
| 2968 | *empty_extent_path = right_path; |
| 2969 | right_path = NULL; |
| 2970 | goto out; |
| 2971 | } |
| 2972 | if (ret) { |
| 2973 | mlog_errno(ret); |
| 2974 | goto out; |
| 2975 | } |
| 2976 | |
| 2977 | /* |
| 2978 | * The subtree rotate might have removed records on |
| 2979 | * the rightmost edge. If so, then rotation is |
| 2980 | * complete. |
| 2981 | */ |
| 2982 | if (deleted) |
| 2983 | break; |
| 2984 | |
| 2985 | ocfs2_mv_path(left_path, right_path); |
| 2986 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 2987 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2988 | &right_cpos); |
| 2989 | if (ret) { |
| 2990 | mlog_errno(ret); |
| 2991 | goto out; |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | out: |
| 2996 | ocfs2_free_path(right_path); |
| 2997 | ocfs2_free_path(left_path); |
| 2998 | |
| 2999 | return ret; |
| 3000 | } |
| 3001 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3002 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
| 3003 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3004 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3005 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3006 | { |
| 3007 | int ret, subtree_index; |
| 3008 | u32 cpos; |
| 3009 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3010 | struct ocfs2_extent_block *eb; |
| 3011 | struct ocfs2_extent_list *el; |
| 3012 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 3013 | ret = ocfs2_et_sanity_check(et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3014 | if (ret) |
| 3015 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3016 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3017 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3018 | if (ret) { |
| 3019 | mlog_errno(ret); |
| 3020 | goto out; |
| 3021 | } |
| 3022 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3023 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3024 | path, &cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3025 | if (ret) { |
| 3026 | mlog_errno(ret); |
| 3027 | goto out; |
| 3028 | } |
| 3029 | |
| 3030 | if (cpos) { |
| 3031 | /* |
| 3032 | * We have a path to the left of this one - it needs |
| 3033 | * an update too. |
| 3034 | */ |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3035 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3036 | if (!left_path) { |
| 3037 | ret = -ENOMEM; |
| 3038 | mlog_errno(ret); |
| 3039 | goto out; |
| 3040 | } |
| 3041 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3042 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3043 | if (ret) { |
| 3044 | mlog_errno(ret); |
| 3045 | goto out; |
| 3046 | } |
| 3047 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 3048 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3049 | if (ret) { |
| 3050 | mlog_errno(ret); |
| 3051 | goto out; |
| 3052 | } |
| 3053 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3054 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3055 | |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3056 | ocfs2_unlink_subtree(handle, et, left_path, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3057 | subtree_index, dealloc); |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 3058 | ret = ocfs2_update_edge_lengths(handle, et, left_path); |
Tao Ma | 3c5e106 | 2009-07-21 15:42:05 +0800 | [diff] [blame] | 3059 | if (ret) { |
| 3060 | mlog_errno(ret); |
| 3061 | goto out; |
| 3062 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3063 | |
| 3064 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3065 | 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] | 3066 | } else { |
| 3067 | /* |
| 3068 | * 'path' is also the leftmost path which |
| 3069 | * means it must be the only one. This gets |
| 3070 | * handled differently because we want to |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3071 | * revert the root back to having extents |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3072 | * in-line. |
| 3073 | */ |
Joel Becker | 6641b0c | 2009-02-12 18:57:52 -0800 | [diff] [blame] | 3074 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3075 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 3076 | el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3077 | el->l_tree_depth = 0; |
| 3078 | el->l_next_free_rec = 0; |
| 3079 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3080 | |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 3081 | ocfs2_et_set_last_eb_blk(et, 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 3085 | |
| 3086 | out: |
| 3087 | ocfs2_free_path(left_path); |
| 3088 | return ret; |
| 3089 | } |
| 3090 | |
Xue jiufei | d0c97d5 | 2015-09-04 15:44:48 -0700 | [diff] [blame] | 3091 | static int ocfs2_remove_rightmost_empty_extent(struct ocfs2_super *osb, |
| 3092 | struct ocfs2_extent_tree *et, |
| 3093 | struct ocfs2_path *path, |
| 3094 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 3095 | { |
| 3096 | handle_t *handle; |
| 3097 | int ret; |
| 3098 | int credits = path->p_tree_depth * 2 + 1; |
| 3099 | |
| 3100 | handle = ocfs2_start_trans(osb, credits); |
| 3101 | if (IS_ERR(handle)) { |
| 3102 | ret = PTR_ERR(handle); |
| 3103 | mlog_errno(ret); |
| 3104 | return ret; |
| 3105 | } |
| 3106 | |
| 3107 | ret = ocfs2_remove_rightmost_path(handle, et, path, dealloc); |
| 3108 | if (ret) |
| 3109 | mlog_errno(ret); |
| 3110 | |
| 3111 | ocfs2_commit_trans(osb, handle); |
| 3112 | return ret; |
| 3113 | } |
| 3114 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3115 | /* |
| 3116 | * Left rotation of btree records. |
| 3117 | * |
| 3118 | * In many ways, this is (unsurprisingly) the opposite of right |
| 3119 | * rotation. We start at some non-rightmost path containing an empty |
| 3120 | * extent in the leaf block. The code works its way to the rightmost |
| 3121 | * path by rotating records to the left in every subtree. |
| 3122 | * |
| 3123 | * This is used by any code which reduces the number of extent records |
| 3124 | * in a leaf. After removal, an empty record should be placed in the |
| 3125 | * leftmost list position. |
| 3126 | * |
| 3127 | * This won't handle a length update of the rightmost path records if |
| 3128 | * the rightmost tree leaf record is removed so the caller is |
| 3129 | * responsible for detecting and correcting that. |
| 3130 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3131 | static int ocfs2_rotate_tree_left(handle_t *handle, |
| 3132 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3133 | struct ocfs2_path *path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3134 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3135 | { |
| 3136 | int ret, orig_credits = handle->h_buffer_credits; |
| 3137 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 3138 | struct ocfs2_extent_block *eb; |
| 3139 | struct ocfs2_extent_list *el; |
| 3140 | |
| 3141 | el = path_leaf_el(path); |
| 3142 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3143 | return 0; |
| 3144 | |
| 3145 | if (path->p_tree_depth == 0) { |
| 3146 | rightmost_no_delete: |
| 3147 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3148 | * Inline extents. This is trivially handled, so do |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3149 | * it up front. |
| 3150 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3151 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3152 | if (ret) |
| 3153 | mlog_errno(ret); |
| 3154 | goto out; |
| 3155 | } |
| 3156 | |
| 3157 | /* |
| 3158 | * Handle rightmost branch now. There's several cases: |
| 3159 | * 1) simple rotation leaving records in there. That's trivial. |
| 3160 | * 2) rotation requiring a branch delete - there's no more |
| 3161 | * records left. Two cases of this: |
| 3162 | * a) There are branches to the left. |
| 3163 | * b) This is also the leftmost (the only) branch. |
| 3164 | * |
| 3165 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 3166 | * 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] | 3167 | * 2b) we need to bring the root back to inline extents. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3168 | */ |
| 3169 | |
| 3170 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 3171 | el = &eb->h_list; |
| 3172 | if (eb->h_next_leaf_blk == 0) { |
| 3173 | /* |
| 3174 | * This gets a bit tricky if we're going to delete the |
| 3175 | * rightmost path. Get the other cases out of the way |
| 3176 | * 1st. |
| 3177 | */ |
| 3178 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 3179 | goto rightmost_no_delete; |
| 3180 | |
| 3181 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 3182 | ret = -EIO; |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3183 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 3184 | "Owner %llu has empty extent block at %llu\n", |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3185 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3186 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 3187 | goto out; |
| 3188 | } |
| 3189 | |
| 3190 | /* |
| 3191 | * XXX: The caller can not trust "path" any more after |
| 3192 | * this as it will have been deleted. What do we do? |
| 3193 | * |
| 3194 | * In theory the rotate-for-merge code will never get |
| 3195 | * here because it'll always ask for a rotate in a |
| 3196 | * nonempty list. |
| 3197 | */ |
| 3198 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3199 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
| 3200 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3201 | if (ret) |
| 3202 | mlog_errno(ret); |
| 3203 | goto out; |
| 3204 | } |
| 3205 | |
| 3206 | /* |
| 3207 | * Now we can loop, remembering the path we get from -EAGAIN |
| 3208 | * and restarting from there. |
| 3209 | */ |
| 3210 | try_rotate: |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3211 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
| 3212 | dealloc, &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3213 | if (ret && ret != -EAGAIN) { |
| 3214 | mlog_errno(ret); |
| 3215 | goto out; |
| 3216 | } |
| 3217 | |
| 3218 | while (ret == -EAGAIN) { |
| 3219 | tmp_path = restart_path; |
| 3220 | restart_path = NULL; |
| 3221 | |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3222 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3223 | tmp_path, dealloc, |
Joel Becker | e46f74d | 2009-02-12 19:47:43 -0800 | [diff] [blame] | 3224 | &restart_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3225 | if (ret && ret != -EAGAIN) { |
| 3226 | mlog_errno(ret); |
| 3227 | goto out; |
| 3228 | } |
| 3229 | |
| 3230 | ocfs2_free_path(tmp_path); |
| 3231 | tmp_path = NULL; |
| 3232 | |
| 3233 | if (ret == 0) |
| 3234 | goto try_rotate; |
| 3235 | } |
| 3236 | |
| 3237 | out: |
| 3238 | ocfs2_free_path(tmp_path); |
| 3239 | ocfs2_free_path(restart_path); |
| 3240 | return ret; |
| 3241 | } |
| 3242 | |
| 3243 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 3244 | int index) |
| 3245 | { |
| 3246 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 3247 | unsigned int size; |
| 3248 | |
| 3249 | if (rec->e_leaf_clusters == 0) { |
| 3250 | /* |
| 3251 | * We consumed all of the merged-from record. An empty |
| 3252 | * extent cannot exist anywhere but the 1st array |
| 3253 | * position, so move things over if the merged-from |
| 3254 | * record doesn't occupy that position. |
| 3255 | * |
| 3256 | * This creates a new empty extent so the caller |
| 3257 | * should be smart enough to have removed any existing |
| 3258 | * ones. |
| 3259 | */ |
| 3260 | if (index > 0) { |
| 3261 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3262 | size = index * sizeof(struct ocfs2_extent_rec); |
| 3263 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 3264 | } |
| 3265 | |
| 3266 | /* |
| 3267 | * Always memset - the caller doesn't check whether it |
| 3268 | * created an empty extent, so there could be junk in |
| 3269 | * the other fields. |
| 3270 | */ |
| 3271 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 3272 | } |
| 3273 | } |
| 3274 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3275 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3276 | struct ocfs2_path *left_path, |
| 3277 | struct ocfs2_path **ret_right_path) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3278 | { |
| 3279 | int ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3280 | u32 right_cpos; |
| 3281 | struct ocfs2_path *right_path = NULL; |
| 3282 | struct ocfs2_extent_list *left_el; |
| 3283 | |
| 3284 | *ret_right_path = NULL; |
| 3285 | |
| 3286 | /* This function shouldn't be called for non-trees. */ |
| 3287 | BUG_ON(left_path->p_tree_depth == 0); |
| 3288 | |
| 3289 | left_el = path_leaf_el(left_path); |
| 3290 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); |
| 3291 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3292 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3293 | left_path, &right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3294 | if (ret) { |
| 3295 | mlog_errno(ret); |
| 3296 | goto out; |
| 3297 | } |
| 3298 | |
| 3299 | /* This function shouldn't be called for the rightmost leaf. */ |
| 3300 | BUG_ON(right_cpos == 0); |
| 3301 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3302 | right_path = ocfs2_new_path_from_path(left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3303 | if (!right_path) { |
| 3304 | ret = -ENOMEM; |
| 3305 | mlog_errno(ret); |
| 3306 | goto out; |
| 3307 | } |
| 3308 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3309 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3310 | if (ret) { |
| 3311 | mlog_errno(ret); |
| 3312 | goto out; |
| 3313 | } |
| 3314 | |
| 3315 | *ret_right_path = right_path; |
| 3316 | out: |
| 3317 | if (ret) |
| 3318 | ocfs2_free_path(right_path); |
| 3319 | return ret; |
| 3320 | } |
| 3321 | |
| 3322 | /* |
| 3323 | * Remove split_rec clusters from the record at index and merge them |
| 3324 | * onto the beginning of the record "next" to it. |
| 3325 | * For index < l_count - 1, the next means the extent rec at index + 1. |
| 3326 | * For index == l_count - 1, the "next" means the 1st extent rec of the |
| 3327 | * next extent block. |
| 3328 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3329 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3330 | handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3331 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3332 | struct ocfs2_extent_rec *split_rec, |
| 3333 | int index) |
| 3334 | { |
| 3335 | int ret, next_free, i; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3336 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3337 | struct ocfs2_extent_rec *left_rec; |
| 3338 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3339 | struct ocfs2_extent_list *right_el; |
| 3340 | struct ocfs2_path *right_path = NULL; |
| 3341 | int subtree_index = 0; |
| 3342 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 3343 | struct buffer_head *bh = path_leaf_bh(left_path); |
| 3344 | struct buffer_head *root_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3345 | |
| 3346 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3347 | left_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3348 | |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3349 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3350 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
| 3351 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3352 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3353 | if (ret) { |
| 3354 | mlog_errno(ret); |
Markus Elfring | 629a3b5 | 2015-04-14 15:42:53 -0700 | [diff] [blame] | 3355 | return ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | right_el = path_leaf_el(right_path); |
| 3359 | next_free = le16_to_cpu(right_el->l_next_free_rec); |
| 3360 | BUG_ON(next_free <= 0); |
| 3361 | right_rec = &right_el->l_recs[0]; |
| 3362 | if (ocfs2_is_empty_extent(right_rec)) { |
Al Viro | 9d8df6a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 3363 | BUG_ON(next_free <= 1); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3364 | right_rec = &right_el->l_recs[1]; |
| 3365 | } |
| 3366 | |
| 3367 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3368 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3369 | le32_to_cpu(right_rec->e_cpos)); |
| 3370 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3371 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3372 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3373 | |
| 3374 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3375 | handle->h_buffer_credits, |
| 3376 | right_path); |
| 3377 | if (ret) { |
| 3378 | mlog_errno(ret); |
| 3379 | goto out; |
| 3380 | } |
| 3381 | |
| 3382 | root_bh = left_path->p_node[subtree_index].bh; |
| 3383 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3384 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3385 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3386 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3387 | if (ret) { |
| 3388 | mlog_errno(ret); |
| 3389 | goto out; |
| 3390 | } |
| 3391 | |
| 3392 | for (i = subtree_index + 1; |
| 3393 | i < path_num_items(right_path); i++) { |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3394 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3395 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3396 | if (ret) { |
| 3397 | mlog_errno(ret); |
| 3398 | goto out; |
| 3399 | } |
| 3400 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3401 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3402 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3403 | if (ret) { |
| 3404 | mlog_errno(ret); |
| 3405 | goto out; |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | } else { |
| 3410 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); |
| 3411 | right_rec = &el->l_recs[index + 1]; |
| 3412 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3413 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3414 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3415 | path_num_items(left_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3416 | if (ret) { |
| 3417 | mlog_errno(ret); |
| 3418 | goto out; |
| 3419 | } |
| 3420 | |
| 3421 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 3422 | |
| 3423 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 3424 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3425 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3426 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3427 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 3428 | |
| 3429 | ocfs2_cleanup_merge(el, index); |
| 3430 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3431 | ocfs2_journal_dirty(handle, bh); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3432 | if (right_path) { |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3433 | ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3434 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 3435 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3436 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3437 | out: |
Markus Elfring | fd90d4df | 2015-04-14 15:42:42 -0700 | [diff] [blame] | 3438 | ocfs2_free_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3439 | return ret; |
| 3440 | } |
| 3441 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3442 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3443 | struct ocfs2_path *right_path, |
| 3444 | struct ocfs2_path **ret_left_path) |
| 3445 | { |
| 3446 | int ret; |
| 3447 | u32 left_cpos; |
| 3448 | struct ocfs2_path *left_path = NULL; |
| 3449 | |
| 3450 | *ret_left_path = NULL; |
| 3451 | |
| 3452 | /* This function shouldn't be called for non-trees. */ |
| 3453 | BUG_ON(right_path->p_tree_depth == 0); |
| 3454 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3455 | 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] | 3456 | right_path, &left_cpos); |
| 3457 | if (ret) { |
| 3458 | mlog_errno(ret); |
| 3459 | goto out; |
| 3460 | } |
| 3461 | |
| 3462 | /* This function shouldn't be called for the leftmost leaf. */ |
| 3463 | BUG_ON(left_cpos == 0); |
| 3464 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 3465 | left_path = ocfs2_new_path_from_path(right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3466 | if (!left_path) { |
| 3467 | ret = -ENOMEM; |
| 3468 | mlog_errno(ret); |
| 3469 | goto out; |
| 3470 | } |
| 3471 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3472 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3473 | if (ret) { |
| 3474 | mlog_errno(ret); |
| 3475 | goto out; |
| 3476 | } |
| 3477 | |
| 3478 | *ret_left_path = left_path; |
| 3479 | out: |
| 3480 | if (ret) |
| 3481 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3482 | return ret; |
| 3483 | } |
| 3484 | |
| 3485 | /* |
| 3486 | * Remove split_rec clusters from the record at index and merge them |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3487 | * onto the tail of the record "before" it. |
| 3488 | * For index > 0, the "before" means the extent rec at index - 1. |
| 3489 | * |
| 3490 | * For index == 0, the "before" means the last record of the previous |
| 3491 | * extent block. And there is also a situation that we may need to |
| 3492 | * remove the rightmost leaf extent block in the right_path and change |
| 3493 | * the right path to indicate the new rightmost path. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3494 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3495 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3496 | handle_t *handle, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3497 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3498 | struct ocfs2_extent_rec *split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3499 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 3500 | int index) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3501 | { |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3502 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3503 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 3504 | struct ocfs2_extent_rec *left_rec; |
| 3505 | struct ocfs2_extent_rec *right_rec; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3506 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
| 3507 | struct buffer_head *bh = path_leaf_bh(right_path); |
| 3508 | struct buffer_head *root_bh = NULL; |
| 3509 | struct ocfs2_path *left_path = NULL; |
| 3510 | struct ocfs2_extent_list *left_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3511 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3512 | BUG_ON(index < 0); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3513 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3514 | right_rec = &el->l_recs[index]; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3515 | if (index == 0) { |
| 3516 | /* we meet with a cross extent block merge. */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3517 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3518 | if (ret) { |
| 3519 | mlog_errno(ret); |
Markus Elfring | 992ef6e | 2015-04-14 15:42:51 -0700 | [diff] [blame] | 3520 | return ret; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | left_el = path_leaf_el(left_path); |
| 3524 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != |
| 3525 | le16_to_cpu(left_el->l_count)); |
| 3526 | |
| 3527 | left_rec = &left_el->l_recs[ |
| 3528 | le16_to_cpu(left_el->l_next_free_rec) - 1]; |
| 3529 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + |
| 3530 | le16_to_cpu(left_rec->e_leaf_clusters) != |
| 3531 | le32_to_cpu(split_rec->e_cpos)); |
| 3532 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3533 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
| 3534 | right_path); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3535 | |
| 3536 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, |
| 3537 | handle->h_buffer_credits, |
| 3538 | left_path); |
| 3539 | if (ret) { |
| 3540 | mlog_errno(ret); |
| 3541 | goto out; |
| 3542 | } |
| 3543 | |
| 3544 | root_bh = left_path->p_node[subtree_index].bh; |
| 3545 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 3546 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3547 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3548 | subtree_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3549 | if (ret) { |
| 3550 | mlog_errno(ret); |
| 3551 | goto out; |
| 3552 | } |
| 3553 | |
| 3554 | for (i = subtree_index + 1; |
| 3555 | i < path_num_items(right_path); i++) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3556 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3557 | right_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3558 | if (ret) { |
| 3559 | mlog_errno(ret); |
| 3560 | goto out; |
| 3561 | } |
| 3562 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3563 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3564 | left_path, i); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3565 | if (ret) { |
| 3566 | mlog_errno(ret); |
| 3567 | goto out; |
| 3568 | } |
| 3569 | } |
| 3570 | } else { |
| 3571 | left_rec = &el->l_recs[index - 1]; |
| 3572 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 3573 | has_empty_extent = 1; |
| 3574 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3575 | |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3576 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
Tao Ma | 9047bea | 2009-01-05 14:45:24 +0800 | [diff] [blame] | 3577 | path_num_items(right_path) - 1); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3578 | if (ret) { |
| 3579 | mlog_errno(ret); |
| 3580 | goto out; |
| 3581 | } |
| 3582 | |
| 3583 | if (has_empty_extent && index == 1) { |
| 3584 | /* |
| 3585 | * The easy case - we can just plop the record right in. |
| 3586 | */ |
| 3587 | *left_rec = *split_rec; |
| 3588 | |
| 3589 | has_empty_extent = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3590 | } else |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3591 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3592 | |
| 3593 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 3594 | le64_add_cpu(&right_rec->e_blkno, |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3595 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3596 | split_clusters)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3597 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 3598 | |
| 3599 | ocfs2_cleanup_merge(el, index); |
| 3600 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3601 | ocfs2_journal_dirty(handle, bh); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3602 | if (left_path) { |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3603 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3604 | |
| 3605 | /* |
| 3606 | * In the situation that the right_rec is empty and the extent |
| 3607 | * block is empty also, ocfs2_complete_edge_insert can't handle |
| 3608 | * it and we need to delete the right extent block. |
| 3609 | */ |
| 3610 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && |
| 3611 | le16_to_cpu(el->l_next_free_rec) == 1) { |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 3612 | /* extend credit for ocfs2_remove_rightmost_path */ |
| 3613 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3614 | handle->h_buffer_credits, |
| 3615 | right_path); |
| 3616 | if (ret) { |
| 3617 | mlog_errno(ret); |
| 3618 | goto out; |
| 3619 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3620 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3621 | ret = ocfs2_remove_rightmost_path(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 3622 | right_path, |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3623 | dealloc); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3624 | if (ret) { |
| 3625 | mlog_errno(ret); |
| 3626 | goto out; |
| 3627 | } |
| 3628 | |
| 3629 | /* Now the rightmost extent block has been deleted. |
| 3630 | * So we use the new rightmost path. |
| 3631 | */ |
| 3632 | ocfs2_mv_path(right_path, left_path); |
| 3633 | left_path = NULL; |
| 3634 | } else |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 3635 | ocfs2_complete_edge_insert(handle, left_path, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3636 | right_path, subtree_index); |
| 3637 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3638 | out: |
Markus Elfring | fd90d4df | 2015-04-14 15:42:42 -0700 | [diff] [blame] | 3639 | ocfs2_free_path(left_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3640 | return ret; |
| 3641 | } |
| 3642 | |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3643 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
| 3644 | struct ocfs2_extent_tree *et, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3645 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3646 | int split_index, |
| 3647 | struct ocfs2_extent_rec *split_rec, |
| 3648 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 3649 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3650 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3651 | int ret = 0; |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3652 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3653 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3654 | |
| 3655 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 3656 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3657 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 3658 | /* extend credit for ocfs2_remove_rightmost_path */ |
| 3659 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3660 | handle->h_buffer_credits, |
| 3661 | path); |
| 3662 | if (ret) { |
| 3663 | mlog_errno(ret); |
| 3664 | goto out; |
| 3665 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3666 | /* |
| 3667 | * The merge code will need to create an empty |
| 3668 | * extent to take the place of the newly |
| 3669 | * emptied slot. Remove any pre-existing empty |
| 3670 | * extents - having more than one in a leaf is |
| 3671 | * illegal. |
| 3672 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3673 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3674 | if (ret) { |
| 3675 | mlog_errno(ret); |
| 3676 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3677 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame] | 3678 | split_index--; |
| 3679 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 3683 | /* |
| 3684 | * Left-right contig implies this. |
| 3685 | */ |
| 3686 | BUG_ON(!ctxt->c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3687 | |
| 3688 | /* |
| 3689 | * Since the leftright insert always covers the entire |
| 3690 | * extent, this call will delete the insert record |
| 3691 | * entirely, resulting in an empty extent record added to |
| 3692 | * the extent block. |
| 3693 | * |
| 3694 | * Since the adding of an empty extent shifts |
| 3695 | * everything back to the right, there's no need to |
| 3696 | * update split_index here. |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3697 | * |
| 3698 | * When the split_index is zero, we need to merge it to the |
| 3699 | * prevoius extent block. It is more efficient and easier |
| 3700 | * if we do merge_right first and merge_left later. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3701 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3702 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3703 | split_index); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3704 | if (ret) { |
| 3705 | mlog_errno(ret); |
| 3706 | goto out; |
| 3707 | } |
| 3708 | |
| 3709 | /* |
| 3710 | * We can only get this from logic error above. |
| 3711 | */ |
| 3712 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3713 | |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 3714 | /* extend credit for ocfs2_remove_rightmost_path */ |
| 3715 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3716 | handle->h_buffer_credits, |
| 3717 | path); |
| 3718 | if (ret) { |
| 3719 | mlog_errno(ret); |
| 3720 | goto out; |
| 3721 | } |
| 3722 | |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3723 | /* The merge left us with an empty extent, remove it. */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3724 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3725 | if (ret) { |
| 3726 | mlog_errno(ret); |
| 3727 | goto out; |
| 3728 | } |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3729 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3730 | rec = &el->l_recs[split_index]; |
| 3731 | |
| 3732 | /* |
| 3733 | * Note that we don't pass split_rec here on purpose - |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3734 | * we've merged it into the rec already. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3735 | */ |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3736 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
| 3737 | dealloc, split_index); |
Tao Ma | 677b975 | 2008-01-30 14:21:05 +0800 | [diff] [blame] | 3738 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3739 | if (ret) { |
| 3740 | mlog_errno(ret); |
| 3741 | goto out; |
| 3742 | } |
| 3743 | |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 3744 | /* extend credit for ocfs2_remove_rightmost_path */ |
| 3745 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3746 | handle->h_buffer_credits, |
| 3747 | path); |
| 3748 | if (ret) { |
| 3749 | mlog_errno(ret); |
| 3750 | goto out; |
| 3751 | } |
| 3752 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3753 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3754 | /* |
| 3755 | * Error from this last rotate is not critical, so |
| 3756 | * print but don't bubble it up. |
| 3757 | */ |
| 3758 | if (ret) |
| 3759 | mlog_errno(ret); |
| 3760 | ret = 0; |
| 3761 | } else { |
| 3762 | /* |
| 3763 | * Merge a record to the left or right. |
| 3764 | * |
| 3765 | * 'contig_type' is relative to the existing record, |
| 3766 | * so for example, if we're "right contig", it's to |
| 3767 | * the record on the left (hence the left merge). |
| 3768 | */ |
| 3769 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3770 | ret = ocfs2_merge_rec_left(path, handle, et, |
| 3771 | split_rec, dealloc, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3772 | split_index); |
| 3773 | if (ret) { |
| 3774 | mlog_errno(ret); |
| 3775 | goto out; |
| 3776 | } |
| 3777 | } else { |
Joel Becker | 4fe82c3 | 2009-02-13 02:16:08 -0800 | [diff] [blame] | 3778 | ret = ocfs2_merge_rec_right(path, handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 3779 | et, split_rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3780 | split_index); |
| 3781 | if (ret) { |
| 3782 | mlog_errno(ret); |
| 3783 | goto out; |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | if (ctxt->c_split_covers_rec) { |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 3788 | /* extend credit for ocfs2_remove_rightmost_path */ |
| 3789 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 3790 | handle->h_buffer_credits, |
| 3791 | path); |
| 3792 | if (ret) { |
| 3793 | mlog_errno(ret); |
| 3794 | ret = 0; |
| 3795 | goto out; |
| 3796 | } |
| 3797 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3798 | /* |
| 3799 | * The merge may have left an empty extent in |
| 3800 | * our leaf. Try to rotate it away. |
| 3801 | */ |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 3802 | ret = ocfs2_rotate_tree_left(handle, et, path, |
| 3803 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3804 | if (ret) |
| 3805 | mlog_errno(ret); |
| 3806 | ret = 0; |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | out: |
| 3811 | return ret; |
| 3812 | } |
| 3813 | |
| 3814 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 3815 | enum ocfs2_split_type split, |
| 3816 | struct ocfs2_extent_rec *rec, |
| 3817 | struct ocfs2_extent_rec *split_rec) |
| 3818 | { |
| 3819 | u64 len_blocks; |
| 3820 | |
| 3821 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 3822 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3823 | |
| 3824 | if (split == SPLIT_LEFT) { |
| 3825 | /* |
| 3826 | * Region is on the left edge of the existing |
| 3827 | * record. |
| 3828 | */ |
| 3829 | le32_add_cpu(&rec->e_cpos, |
| 3830 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3831 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 3832 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3833 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3834 | } else { |
| 3835 | /* |
| 3836 | * Region is on the right edge of the existing |
| 3837 | * record. |
| 3838 | */ |
| 3839 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3840 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 3841 | } |
| 3842 | } |
| 3843 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3844 | /* |
| 3845 | * Do the final bits of extent record insertion at the target leaf |
| 3846 | * list. If this leaf is part of an allocation tree, it is assumed |
| 3847 | * that the tree above has been prepared. |
| 3848 | */ |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3849 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et, |
| 3850 | struct ocfs2_extent_rec *insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3851 | struct ocfs2_extent_list *el, |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3852 | struct ocfs2_insert_type *insert) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3853 | { |
| 3854 | int i = insert->ins_contig_index; |
| 3855 | unsigned int range; |
| 3856 | struct ocfs2_extent_rec *rec; |
| 3857 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3858 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3859 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3860 | if (insert->ins_split != SPLIT_NONE) { |
| 3861 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 3862 | BUG_ON(i == -1); |
| 3863 | rec = &el->l_recs[i]; |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3864 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 3865 | insert->ins_split, rec, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3866 | insert_rec); |
| 3867 | goto rotate; |
| 3868 | } |
| 3869 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3870 | /* |
| 3871 | * Contiguous insert - either left or right. |
| 3872 | */ |
| 3873 | if (insert->ins_contig != CONTIG_NONE) { |
| 3874 | rec = &el->l_recs[i]; |
| 3875 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3876 | rec->e_blkno = insert_rec->e_blkno; |
| 3877 | rec->e_cpos = insert_rec->e_cpos; |
| 3878 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3879 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3880 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3881 | return; |
| 3882 | } |
| 3883 | |
| 3884 | /* |
| 3885 | * Handle insert into an empty leaf. |
| 3886 | */ |
| 3887 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3888 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3889 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3890 | el->l_recs[0] = *insert_rec; |
| 3891 | el->l_next_free_rec = cpu_to_le16(1); |
| 3892 | return; |
| 3893 | } |
| 3894 | |
| 3895 | /* |
| 3896 | * Appending insert. |
| 3897 | */ |
| 3898 | if (insert->ins_appending == APPEND_TAIL) { |
| 3899 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3900 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3901 | range = le32_to_cpu(rec->e_cpos) |
| 3902 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3903 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3904 | |
| 3905 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3906 | le16_to_cpu(el->l_count), |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3907 | "owner %llu, depth %u, count %u, next free %u, " |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3908 | "rec.cpos %u, rec.clusters %u, " |
| 3909 | "insert.cpos %u, insert.clusters %u\n", |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 3910 | ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3911 | le16_to_cpu(el->l_tree_depth), |
| 3912 | le16_to_cpu(el->l_count), |
| 3913 | le16_to_cpu(el->l_next_free_rec), |
| 3914 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3915 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3916 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3917 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3918 | i++; |
| 3919 | el->l_recs[i] = *insert_rec; |
| 3920 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3921 | return; |
| 3922 | } |
| 3923 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3924 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3925 | /* |
| 3926 | * Ok, we have to rotate. |
| 3927 | * |
| 3928 | * At this point, it is safe to assume that inserting into an |
| 3929 | * empty leaf and appending to a leaf have both been handled |
| 3930 | * above. |
| 3931 | * |
| 3932 | * This leaf needs to have space, either by the empty 1st |
| 3933 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3934 | */ |
| 3935 | ocfs2_rotate_leaf(el, insert_rec); |
| 3936 | } |
| 3937 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3938 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
| 3939 | struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3940 | struct ocfs2_path *path, |
| 3941 | struct ocfs2_extent_rec *insert_rec) |
| 3942 | { |
| 3943 | int ret, i, next_free; |
| 3944 | struct buffer_head *bh; |
| 3945 | struct ocfs2_extent_list *el; |
| 3946 | struct ocfs2_extent_rec *rec; |
| 3947 | |
| 3948 | /* |
| 3949 | * Update everything except the leaf block. |
| 3950 | */ |
| 3951 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3952 | bh = path->p_node[i].bh; |
| 3953 | el = path->p_node[i].el; |
| 3954 | |
| 3955 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3956 | if (next_free == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3957 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 3958 | "Owner %llu has a bad extent list\n", |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3959 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3960 | ret = -EIO; |
| 3961 | return; |
| 3962 | } |
| 3963 | |
| 3964 | rec = &el->l_recs[next_free - 1]; |
| 3965 | |
| 3966 | rec->e_int_clusters = insert_rec->e_cpos; |
| 3967 | le32_add_cpu(&rec->e_int_clusters, |
| 3968 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 3969 | le32_add_cpu(&rec->e_int_clusters, |
| 3970 | -le32_to_cpu(rec->e_cpos)); |
| 3971 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3972 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 3976 | static int ocfs2_append_rec_to_path(handle_t *handle, |
| 3977 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3978 | struct ocfs2_extent_rec *insert_rec, |
| 3979 | struct ocfs2_path *right_path, |
| 3980 | struct ocfs2_path **ret_left_path) |
| 3981 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3982 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3983 | struct ocfs2_extent_list *el; |
| 3984 | struct ocfs2_path *left_path = NULL; |
| 3985 | |
| 3986 | *ret_left_path = NULL; |
| 3987 | |
| 3988 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3989 | * This shouldn't happen for non-trees. The extent rec cluster |
| 3990 | * count manipulation below only works for interior nodes. |
| 3991 | */ |
| 3992 | BUG_ON(right_path->p_tree_depth == 0); |
| 3993 | |
| 3994 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3995 | * If our appending insert is at the leftmost edge of a leaf, |
| 3996 | * then we might need to update the rightmost records of the |
| 3997 | * neighboring path. |
| 3998 | */ |
| 3999 | el = path_leaf_el(right_path); |
| 4000 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 4001 | if (next_free == 0 || |
| 4002 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 4003 | u32 left_cpos; |
| 4004 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4005 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4006 | right_path, &left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4007 | if (ret) { |
| 4008 | mlog_errno(ret); |
| 4009 | goto out; |
| 4010 | } |
| 4011 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4012 | trace_ocfs2_append_rec_to_path( |
| 4013 | (unsigned long long) |
| 4014 | ocfs2_metadata_cache_owner(et->et_ci), |
| 4015 | le32_to_cpu(insert_rec->e_cpos), |
| 4016 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4017 | |
| 4018 | /* |
| 4019 | * No need to worry if the append is already in the |
| 4020 | * leftmost leaf. |
| 4021 | */ |
| 4022 | if (left_cpos) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4023 | left_path = ocfs2_new_path_from_path(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4024 | if (!left_path) { |
| 4025 | ret = -ENOMEM; |
| 4026 | mlog_errno(ret); |
| 4027 | goto out; |
| 4028 | } |
| 4029 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4030 | ret = ocfs2_find_path(et->et_ci, left_path, |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4031 | left_cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4032 | if (ret) { |
| 4033 | mlog_errno(ret); |
| 4034 | goto out; |
| 4035 | } |
| 4036 | |
| 4037 | /* |
| 4038 | * ocfs2_insert_path() will pass the left_path to the |
| 4039 | * journal for us. |
| 4040 | */ |
| 4041 | } |
| 4042 | } |
| 4043 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4044 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4045 | if (ret) { |
| 4046 | mlog_errno(ret); |
| 4047 | goto out; |
| 4048 | } |
| 4049 | |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4050 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4051 | |
| 4052 | *ret_left_path = left_path; |
| 4053 | ret = 0; |
| 4054 | out: |
| 4055 | if (ret != 0) |
| 4056 | ocfs2_free_path(left_path); |
| 4057 | |
| 4058 | return ret; |
| 4059 | } |
| 4060 | |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4061 | static void ocfs2_split_record(struct ocfs2_extent_tree *et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4062 | struct ocfs2_path *left_path, |
| 4063 | struct ocfs2_path *right_path, |
| 4064 | struct ocfs2_extent_rec *split_rec, |
| 4065 | enum ocfs2_split_type split) |
| 4066 | { |
| 4067 | int index; |
| 4068 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 4069 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 4070 | struct ocfs2_extent_rec *rec, *tmprec; |
| 4071 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 4072 | right_el = path_leaf_el(right_path); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4073 | if (left_path) |
| 4074 | left_el = path_leaf_el(left_path); |
| 4075 | |
| 4076 | el = right_el; |
| 4077 | insert_el = right_el; |
| 4078 | index = ocfs2_search_extent_list(el, cpos); |
| 4079 | if (index != -1) { |
| 4080 | if (index == 0 && left_path) { |
| 4081 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 4082 | |
| 4083 | /* |
| 4084 | * This typically means that the record |
| 4085 | * started in the left path but moved to the |
| 4086 | * right as a result of rotation. We either |
| 4087 | * move the existing record to the left, or we |
| 4088 | * do the later insert there. |
| 4089 | * |
| 4090 | * In this case, the left path should always |
| 4091 | * exist as the rotate code will have passed |
| 4092 | * it back for a post-insert update. |
| 4093 | */ |
| 4094 | |
| 4095 | if (split == SPLIT_LEFT) { |
| 4096 | /* |
| 4097 | * It's a left split. Since we know |
| 4098 | * that the rotate code gave us an |
| 4099 | * empty extent in the left path, we |
| 4100 | * can just do the insert there. |
| 4101 | */ |
| 4102 | insert_el = left_el; |
| 4103 | } else { |
| 4104 | /* |
| 4105 | * Right split - we have to move the |
| 4106 | * existing record over to the left |
| 4107 | * leaf. The insert will be into the |
| 4108 | * newly created empty extent in the |
| 4109 | * right leaf. |
| 4110 | */ |
| 4111 | tmprec = &right_el->l_recs[index]; |
| 4112 | ocfs2_rotate_leaf(left_el, tmprec); |
| 4113 | el = left_el; |
| 4114 | |
| 4115 | memset(tmprec, 0, sizeof(*tmprec)); |
| 4116 | index = ocfs2_search_extent_list(left_el, cpos); |
| 4117 | BUG_ON(index == -1); |
| 4118 | } |
| 4119 | } |
| 4120 | } else { |
| 4121 | BUG_ON(!left_path); |
| 4122 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 4123 | /* |
| 4124 | * Left path is easy - we can just allow the insert to |
| 4125 | * happen. |
| 4126 | */ |
| 4127 | el = left_el; |
| 4128 | insert_el = left_el; |
| 4129 | index = ocfs2_search_extent_list(el, cpos); |
| 4130 | BUG_ON(index == -1); |
| 4131 | } |
| 4132 | |
| 4133 | rec = &el->l_recs[index]; |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4134 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4135 | split, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4136 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 4137 | } |
| 4138 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4139 | /* |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4140 | * This function only does inserts on an allocation b-tree. For tree |
| 4141 | * depth = 0, ocfs2_insert_at_leaf() is called directly. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4142 | * |
| 4143 | * right_path is the path we want to do the actual insert |
| 4144 | * in. left_path should only be passed in if we need to update that |
| 4145 | * portion of the tree after an edge insert. |
| 4146 | */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4147 | static int ocfs2_insert_path(handle_t *handle, |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4148 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4149 | struct ocfs2_path *left_path, |
| 4150 | struct ocfs2_path *right_path, |
| 4151 | struct ocfs2_extent_rec *insert_rec, |
| 4152 | struct ocfs2_insert_type *insert) |
| 4153 | { |
| 4154 | int ret, subtree_index; |
| 4155 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4156 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4157 | if (left_path) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4158 | /* |
| 4159 | * There's a chance that left_path got passed back to |
| 4160 | * us without being accounted for in the |
| 4161 | * journal. Extend our transaction here to be sure we |
| 4162 | * can change those blocks. |
| 4163 | */ |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 4164 | ret = ocfs2_extend_trans(handle, left_path->p_tree_depth); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4165 | if (ret < 0) { |
| 4166 | mlog_errno(ret); |
| 4167 | goto out; |
| 4168 | } |
| 4169 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4170 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4171 | if (ret < 0) { |
| 4172 | mlog_errno(ret); |
| 4173 | goto out; |
| 4174 | } |
| 4175 | } |
| 4176 | |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4177 | /* |
| 4178 | * Pass both paths to the journal. The majority of inserts |
| 4179 | * will be touching all components anyway. |
| 4180 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4181 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4182 | if (ret < 0) { |
| 4183 | mlog_errno(ret); |
| 4184 | goto out; |
| 4185 | } |
| 4186 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4187 | if (insert->ins_split != SPLIT_NONE) { |
| 4188 | /* |
| 4189 | * We could call ocfs2_insert_at_leaf() for some types |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 4190 | * of splits, but it's easier to just let one separate |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4191 | * function sort it all out. |
| 4192 | */ |
Joel Becker | c38e52b | 2009-02-13 02:56:23 -0800 | [diff] [blame] | 4193 | ocfs2_split_record(et, left_path, right_path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4194 | insert_rec, insert->ins_split); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4195 | |
| 4196 | /* |
| 4197 | * Split might have modified either leaf and we don't |
| 4198 | * have a guarantee that the later edge insert will |
| 4199 | * dirty this for us. |
| 4200 | */ |
| 4201 | if (left_path) |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4202 | ocfs2_journal_dirty(handle, |
| 4203 | path_leaf_bh(left_path)); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4204 | } else |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4205 | ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path), |
| 4206 | insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4207 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4208 | ocfs2_journal_dirty(handle, leaf_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4209 | |
| 4210 | if (left_path) { |
| 4211 | /* |
| 4212 | * The rotate code has indicated that we need to fix |
| 4213 | * up portions of the tree after the insert. |
| 4214 | * |
| 4215 | * XXX: Should we extend the transaction here? |
| 4216 | */ |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 4217 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4218 | right_path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 4219 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
| 4220 | subtree_index); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | ret = 0; |
| 4224 | out: |
| 4225 | return ret; |
| 4226 | } |
| 4227 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4228 | static int ocfs2_do_insert_extent(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4229 | struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4230 | struct ocfs2_extent_rec *insert_rec, |
| 4231 | struct ocfs2_insert_type *type) |
| 4232 | { |
| 4233 | int ret, rotate = 0; |
| 4234 | u32 cpos; |
| 4235 | struct ocfs2_path *right_path = NULL; |
| 4236 | struct ocfs2_path *left_path = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4237 | struct ocfs2_extent_list *el; |
| 4238 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4239 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4240 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4241 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4242 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4243 | if (ret) { |
| 4244 | mlog_errno(ret); |
| 4245 | goto out; |
| 4246 | } |
| 4247 | |
| 4248 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
Joel Becker | d562862 | 2009-02-13 02:54:36 -0800 | [diff] [blame] | 4249 | ocfs2_insert_at_leaf(et, insert_rec, el, type); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4250 | goto out_update_clusters; |
| 4251 | } |
| 4252 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4253 | right_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4254 | if (!right_path) { |
| 4255 | ret = -ENOMEM; |
| 4256 | mlog_errno(ret); |
| 4257 | goto out; |
| 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | * Determine the path to start with. Rotations need the |
| 4262 | * rightmost path, everything else can go directly to the |
| 4263 | * target leaf. |
| 4264 | */ |
| 4265 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4266 | if (type->ins_appending == APPEND_NONE && |
| 4267 | type->ins_contig == CONTIG_NONE) { |
| 4268 | rotate = 1; |
| 4269 | cpos = UINT_MAX; |
| 4270 | } |
| 4271 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4272 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4273 | if (ret) { |
| 4274 | mlog_errno(ret); |
| 4275 | goto out; |
| 4276 | } |
| 4277 | |
| 4278 | /* |
| 4279 | * Rotations and appends need special treatment - they modify |
| 4280 | * parts of the tree's above them. |
| 4281 | * |
| 4282 | * Both might pass back a path immediate to the left of the |
| 4283 | * one being inserted to. This will be cause |
| 4284 | * ocfs2_insert_path() to modify the rightmost records of |
| 4285 | * left_path to account for an edge insert. |
| 4286 | * |
| 4287 | * XXX: When modifying this code, keep in mind that an insert |
| 4288 | * can wind up skipping both of these two special cases... |
| 4289 | */ |
| 4290 | if (rotate) { |
Joel Becker | 1bbf0b8 | 2009-02-12 19:42:08 -0800 | [diff] [blame] | 4291 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4292 | le32_to_cpu(insert_rec->e_cpos), |
| 4293 | right_path, &left_path); |
| 4294 | if (ret) { |
| 4295 | mlog_errno(ret); |
| 4296 | goto out; |
| 4297 | } |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4298 | |
| 4299 | /* |
| 4300 | * ocfs2_rotate_tree_right() might have extended the |
| 4301 | * transaction without re-journaling our tree root. |
| 4302 | */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4303 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4304 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 4305 | if (ret) { |
| 4306 | mlog_errno(ret); |
| 4307 | goto out; |
| 4308 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4309 | } else if (type->ins_appending == APPEND_TAIL |
| 4310 | && type->ins_contig != CONTIG_LEFT) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4311 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4312 | right_path, &left_path); |
| 4313 | if (ret) { |
| 4314 | mlog_errno(ret); |
| 4315 | goto out; |
| 4316 | } |
| 4317 | } |
| 4318 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4319 | ret = ocfs2_insert_path(handle, et, left_path, right_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4320 | insert_rec, type); |
| 4321 | if (ret) { |
| 4322 | mlog_errno(ret); |
| 4323 | goto out; |
| 4324 | } |
| 4325 | |
| 4326 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4327 | if (type->ins_split == SPLIT_NONE) |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4328 | ocfs2_et_update_clusters(et, |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4329 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4330 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4331 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4332 | |
| 4333 | out: |
| 4334 | ocfs2_free_path(left_path); |
| 4335 | ocfs2_free_path(right_path); |
| 4336 | |
| 4337 | return ret; |
| 4338 | } |
| 4339 | |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4340 | static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et, |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4341 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4342 | struct ocfs2_extent_list *el, int index, |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4343 | struct ocfs2_extent_rec *split_rec, |
| 4344 | struct ocfs2_merge_ctxt *ctxt) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4345 | { |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4346 | int status = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4347 | enum ocfs2_contig_type ret = CONTIG_NONE; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4348 | u32 left_cpos, right_cpos; |
| 4349 | struct ocfs2_extent_rec *rec = NULL; |
| 4350 | struct ocfs2_extent_list *new_el; |
| 4351 | struct ocfs2_path *left_path = NULL, *right_path = NULL; |
| 4352 | struct buffer_head *bh; |
| 4353 | struct ocfs2_extent_block *eb; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4354 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4355 | |
| 4356 | if (index > 0) { |
| 4357 | rec = &el->l_recs[index - 1]; |
| 4358 | } else if (path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4359 | status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4360 | if (status) |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4361 | goto exit; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4362 | |
| 4363 | if (left_cpos != 0) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4364 | left_path = ocfs2_new_path_from_path(path); |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4365 | if (!left_path) { |
| 4366 | status = -ENOMEM; |
| 4367 | mlog_errno(status); |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4368 | goto exit; |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4369 | } |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4370 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4371 | status = ocfs2_find_path(et->et_ci, left_path, |
| 4372 | left_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4373 | if (status) |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4374 | goto free_left_path; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4375 | |
| 4376 | new_el = path_leaf_el(left_path); |
| 4377 | |
| 4378 | if (le16_to_cpu(new_el->l_next_free_rec) != |
| 4379 | le16_to_cpu(new_el->l_count)) { |
| 4380 | bh = path_leaf_bh(left_path); |
| 4381 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4382 | ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 4383 | "Extent block #%llu has an invalid l_next_free_rec of %d. It should have matched the l_count of %d\n", |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4384 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4385 | le16_to_cpu(new_el->l_next_free_rec), |
| 4386 | le16_to_cpu(new_el->l_count)); |
| 4387 | status = -EINVAL; |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4388 | goto free_left_path; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4389 | } |
| 4390 | rec = &new_el->l_recs[ |
| 4391 | le16_to_cpu(new_el->l_next_free_rec) - 1]; |
| 4392 | } |
| 4393 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4394 | |
| 4395 | /* |
| 4396 | * We're careful to check for an empty extent record here - |
| 4397 | * the merge code will know what to do if it sees one. |
| 4398 | */ |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4399 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4400 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 4401 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 4402 | ret = CONTIG_RIGHT; |
| 4403 | } else { |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4404 | ret = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4408 | rec = NULL; |
| 4409 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) |
| 4410 | rec = &el->l_recs[index + 1]; |
| 4411 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && |
| 4412 | path->p_tree_depth > 0) { |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4413 | status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4414 | if (status) |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4415 | goto free_left_path; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4416 | |
| 4417 | if (right_cpos == 0) |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4418 | goto free_left_path; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4419 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4420 | right_path = ocfs2_new_path_from_path(path); |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4421 | if (!right_path) { |
| 4422 | status = -ENOMEM; |
| 4423 | mlog_errno(status); |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4424 | goto free_left_path; |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4425 | } |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4426 | |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4427 | status = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4428 | if (status) |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4429 | goto free_right_path; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4430 | |
| 4431 | new_el = path_leaf_el(right_path); |
| 4432 | rec = &new_el->l_recs[0]; |
| 4433 | if (ocfs2_is_empty_extent(rec)) { |
| 4434 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { |
| 4435 | bh = path_leaf_bh(right_path); |
| 4436 | eb = (struct ocfs2_extent_block *)bh->b_data; |
Joel Becker | a297029 | 2009-02-13 03:09:54 -0800 | [diff] [blame] | 4437 | ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 4438 | "Extent block #%llu has an invalid l_next_free_rec of %d\n", |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4439 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 4440 | le16_to_cpu(new_el->l_next_free_rec)); |
| 4441 | status = -EINVAL; |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4442 | goto free_right_path; |
Tao Ma | ad5a4d7 | 2008-01-30 14:21:32 +0800 | [diff] [blame] | 4443 | } |
| 4444 | rec = &new_el->l_recs[1]; |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | if (rec) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4449 | enum ocfs2_contig_type contig_type; |
| 4450 | |
Tao Ma | 853a3a1 | 2009-08-18 11:22:18 +0800 | [diff] [blame] | 4451 | contig_type = ocfs2_et_extent_contig(et, rec, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4452 | |
| 4453 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 4454 | ret = CONTIG_LEFTRIGHT; |
| 4455 | else if (ret == CONTIG_NONE) |
| 4456 | ret = contig_type; |
| 4457 | } |
| 4458 | |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4459 | free_right_path: |
Markus Elfring | fd90d4df | 2015-04-14 15:42:42 -0700 | [diff] [blame] | 4460 | ocfs2_free_path(right_path); |
Markus Elfring | 06a269cc | 2015-04-14 15:42:48 -0700 | [diff] [blame] | 4461 | free_left_path: |
| 4462 | ocfs2_free_path(left_path); |
| 4463 | exit: |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 4464 | if (status == 0) |
| 4465 | ctxt->c_contig_type = ret; |
| 4466 | |
| 4467 | return status; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4468 | } |
| 4469 | |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4470 | static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4471 | struct ocfs2_insert_type *insert, |
| 4472 | struct ocfs2_extent_list *el, |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4473 | struct ocfs2_extent_rec *insert_rec) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4474 | { |
| 4475 | int i; |
| 4476 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 4477 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4478 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 4479 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4480 | 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] | 4481 | contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i], |
| 4482 | insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4483 | if (contig_type != CONTIG_NONE) { |
| 4484 | insert->ins_contig_index = i; |
| 4485 | break; |
| 4486 | } |
| 4487 | } |
| 4488 | insert->ins_contig = contig_type; |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4489 | |
| 4490 | if (insert->ins_contig != CONTIG_NONE) { |
| 4491 | struct ocfs2_extent_rec *rec = |
| 4492 | &el->l_recs[insert->ins_contig_index]; |
| 4493 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + |
| 4494 | le16_to_cpu(insert_rec->e_leaf_clusters); |
| 4495 | |
| 4496 | /* |
| 4497 | * Caller might want us to limit the size of extents, don't |
| 4498 | * calculate contiguousness if we might exceed that limit. |
| 4499 | */ |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4500 | if (et->et_max_leaf_clusters && |
| 4501 | (len > et->et_max_leaf_clusters)) |
Tao Ma | ca12b7c | 2008-08-18 17:38:52 +0800 | [diff] [blame] | 4502 | insert->ins_contig = CONTIG_NONE; |
| 4503 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | /* |
| 4507 | * This should only be called against the righmost leaf extent list. |
| 4508 | * |
| 4509 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 4510 | * insert at the tail of the rightmost leaf. |
| 4511 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4512 | * This should also work against the root extent list for tree's with 0 |
| 4513 | * 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] | 4514 | * then the logic here makes sense. |
| 4515 | */ |
| 4516 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 4517 | struct ocfs2_extent_list *el, |
| 4518 | struct ocfs2_extent_rec *insert_rec) |
| 4519 | { |
| 4520 | int i; |
| 4521 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 4522 | struct ocfs2_extent_rec *rec; |
| 4523 | |
| 4524 | insert->ins_appending = APPEND_NONE; |
| 4525 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4526 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4527 | |
| 4528 | if (!el->l_next_free_rec) |
| 4529 | goto set_tail_append; |
| 4530 | |
| 4531 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 4532 | /* Were all records empty? */ |
| 4533 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 4534 | goto set_tail_append; |
| 4535 | } |
| 4536 | |
| 4537 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 4538 | rec = &el->l_recs[i]; |
| 4539 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4540 | if (cpos >= |
| 4541 | (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] | 4542 | goto set_tail_append; |
| 4543 | |
| 4544 | return; |
| 4545 | |
| 4546 | set_tail_append: |
| 4547 | insert->ins_appending = APPEND_TAIL; |
| 4548 | } |
| 4549 | |
| 4550 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4551 | * Helper function called at the beginning of an insert. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4552 | * |
| 4553 | * This computes a few things that are commonly used in the process of |
| 4554 | * inserting into the btree: |
| 4555 | * - Whether the new extent is contiguous with an existing one. |
| 4556 | * - The current tree depth. |
| 4557 | * - Whether the insert is an appending one. |
| 4558 | * - The total # of free records in the tree. |
| 4559 | * |
| 4560 | * All of the information is stored on the ocfs2_insert_type |
| 4561 | * structure. |
| 4562 | */ |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4563 | static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4564 | struct buffer_head **last_eb_bh, |
| 4565 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4566 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4567 | struct ocfs2_insert_type *insert) |
| 4568 | { |
| 4569 | int ret; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4570 | struct ocfs2_extent_block *eb; |
| 4571 | struct ocfs2_extent_list *el; |
| 4572 | struct ocfs2_path *path = NULL; |
| 4573 | struct buffer_head *bh = NULL; |
| 4574 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4575 | insert->ins_split = SPLIT_NONE; |
| 4576 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4577 | el = et->et_root_el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4578 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 4579 | |
| 4580 | if (el->l_tree_depth) { |
| 4581 | /* |
| 4582 | * If we have tree depth, we read in the |
| 4583 | * rightmost extent block ahead of time as |
| 4584 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 4585 | * may want it later. |
| 4586 | */ |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 4587 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 4588 | ocfs2_et_get_last_eb_blk(et), |
| 4589 | &bh); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4590 | if (ret) { |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 4591 | mlog_errno(ret); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4592 | goto out; |
| 4593 | } |
| 4594 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 4595 | el = &eb->h_list; |
| 4596 | } |
| 4597 | |
| 4598 | /* |
| 4599 | * Unless we have a contiguous insert, we'll need to know if |
| 4600 | * there is room left in our allocation tree for another |
| 4601 | * extent record. |
| 4602 | * |
| 4603 | * XXX: This test is simplistic, we can search for empty |
| 4604 | * extent records too. |
| 4605 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4606 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4607 | le16_to_cpu(el->l_next_free_rec); |
| 4608 | |
| 4609 | if (!insert->ins_tree_depth) { |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4610 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4611 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4612 | return 0; |
| 4613 | } |
| 4614 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 4615 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4616 | if (!path) { |
| 4617 | ret = -ENOMEM; |
| 4618 | mlog_errno(ret); |
| 4619 | goto out; |
| 4620 | } |
| 4621 | |
| 4622 | /* |
| 4623 | * In the case that we're inserting past what the tree |
| 4624 | * currently accounts for, ocfs2_find_path() will return for |
| 4625 | * us the rightmost tree path. This is accounted for below in |
| 4626 | * the appending code. |
| 4627 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4628 | 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] | 4629 | if (ret) { |
| 4630 | mlog_errno(ret); |
| 4631 | goto out; |
| 4632 | } |
| 4633 | |
| 4634 | el = path_leaf_el(path); |
| 4635 | |
| 4636 | /* |
| 4637 | * Now that we have the path, there's two things we want to determine: |
| 4638 | * 1) Contiguousness (also set contig_index if this is so) |
| 4639 | * |
| 4640 | * 2) Are we doing an append? We can trivially break this up |
| 4641 | * into two types of appends: simple record append, or a |
| 4642 | * rotate inside the tail leaf. |
| 4643 | */ |
Joel Becker | 1ef61b3 | 2009-02-13 03:12:33 -0800 | [diff] [blame] | 4644 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4645 | |
| 4646 | /* |
| 4647 | * The insert code isn't quite ready to deal with all cases of |
| 4648 | * left contiguousness. Specifically, if it's an insert into |
| 4649 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4650 | * 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] | 4651 | * left. For now, just catch that case and fool the layers |
| 4652 | * above us. This works just fine for tree_depth == 0, which |
| 4653 | * is why we allow that above. |
| 4654 | */ |
| 4655 | if (insert->ins_contig == CONTIG_LEFT && |
| 4656 | insert->ins_contig_index == 0) |
| 4657 | insert->ins_contig = CONTIG_NONE; |
| 4658 | |
| 4659 | /* |
| 4660 | * Ok, so we can simply compare against last_eb to figure out |
| 4661 | * whether the path doesn't exist. This will only happen in |
| 4662 | * the case that we're doing a tail append, so maybe we can |
| 4663 | * take advantage of that information somehow. |
| 4664 | */ |
Joel Becker | 35dc0aa | 2008-08-20 16:25:06 -0700 | [diff] [blame] | 4665 | if (ocfs2_et_get_last_eb_blk(et) == |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4666 | path_leaf_bh(path)->b_blocknr) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4667 | /* |
| 4668 | * Ok, ocfs2_find_path() returned us the rightmost |
| 4669 | * tree path. This might be an appending insert. There are |
| 4670 | * two cases: |
| 4671 | * 1) We're doing a true append at the tail: |
| 4672 | * -This might even be off the end of the leaf |
| 4673 | * 2) We're "appending" by rotating in the tail |
| 4674 | */ |
| 4675 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 4676 | } |
| 4677 | |
| 4678 | out: |
| 4679 | ocfs2_free_path(path); |
| 4680 | |
| 4681 | if (ret == 0) |
| 4682 | *last_eb_bh = bh; |
| 4683 | else |
| 4684 | brelse(bh); |
| 4685 | return ret; |
| 4686 | } |
| 4687 | |
| 4688 | /* |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4689 | * Insert an extent into a btree. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4690 | * |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4691 | * The caller needs to update the owning btree's cluster count. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4692 | */ |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4693 | int ocfs2_insert_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4694 | struct ocfs2_extent_tree *et, |
| 4695 | u32 cpos, |
| 4696 | u64 start_blk, |
| 4697 | u32 new_clusters, |
| 4698 | u8 flags, |
| 4699 | struct ocfs2_alloc_context *meta_ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4700 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4701 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4702 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4703 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4704 | struct ocfs2_insert_type insert = {0, }; |
| 4705 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4706 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4707 | trace_ocfs2_insert_extent_start( |
| 4708 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 4709 | cpos, new_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4710 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4711 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4712 | rec.e_cpos = cpu_to_le32(cpos); |
| 4713 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 4714 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 4715 | rec.e_flags = flags; |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 4716 | status = ocfs2_et_insert_check(et, &rec); |
Joel Becker | 1e61ee7 | 2008-08-20 18:32:45 -0700 | [diff] [blame] | 4717 | if (status) { |
| 4718 | mlog_errno(status); |
| 4719 | goto bail; |
| 4720 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4721 | |
Joel Becker | 627961b | 2009-02-13 03:14:38 -0800 | [diff] [blame] | 4722 | status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4723 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4724 | if (status < 0) { |
| 4725 | mlog_errno(status); |
| 4726 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4727 | } |
| 4728 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4729 | trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig, |
| 4730 | insert.ins_contig_index, free_records, |
| 4731 | insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4732 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 4733 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4734 | status = ocfs2_grow_tree(handle, et, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4735 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 4736 | meta_ac); |
| 4737 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4738 | mlog_errno(status); |
| 4739 | goto bail; |
| 4740 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4741 | } |
| 4742 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 4743 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4744 | status = ocfs2_do_insert_extent(handle, et, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4745 | if (status < 0) |
| 4746 | mlog_errno(status); |
Joel Becker | 92ba470 | 2009-02-13 03:18:34 -0800 | [diff] [blame] | 4747 | else |
| 4748 | ocfs2_et_extent_map_insert(et, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4749 | |
| 4750 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4751 | brelse(last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4752 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 4753 | return status; |
| 4754 | } |
| 4755 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4756 | /* |
| 4757 | * Allcate and add clusters into the extent b-tree. |
| 4758 | * The new clusters(clusters_to_add) will be inserted at logical_offset. |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4759 | * The extent b-tree's root is specified by et, and |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4760 | * it is not limited to the file storage. Any extent tree can use this |
| 4761 | * function if it implements the proper ocfs2_extent_tree. |
| 4762 | */ |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4763 | int ocfs2_add_clusters_in_btree(handle_t *handle, |
| 4764 | struct ocfs2_extent_tree *et, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4765 | u32 *logical_offset, |
| 4766 | u32 clusters_to_add, |
| 4767 | int mark_unwritten, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4768 | struct ocfs2_alloc_context *data_ac, |
| 4769 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4770 | enum ocfs2_alloc_restarted *reason_ret) |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4771 | { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4772 | int status = 0, err = 0; |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 4773 | int need_free = 0; |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4774 | int free_extents; |
| 4775 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 4776 | u32 bit_off, num_bits; |
| 4777 | u64 block; |
| 4778 | u8 flags = 0; |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 4779 | struct ocfs2_super *osb = |
| 4780 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4781 | |
| 4782 | BUG_ON(!clusters_to_add); |
| 4783 | |
| 4784 | if (mark_unwritten) |
| 4785 | flags = OCFS2_EXT_UNWRITTEN; |
| 4786 | |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 4787 | free_extents = ocfs2_num_free_extents(et); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4788 | if (free_extents < 0) { |
| 4789 | status = free_extents; |
| 4790 | mlog_errno(status); |
| 4791 | goto leave; |
| 4792 | } |
| 4793 | |
| 4794 | /* there are two cases which could cause us to EAGAIN in the |
| 4795 | * we-need-more-metadata case: |
| 4796 | * 1) we haven't reserved *any* |
| 4797 | * 2) we are so fragmented, we've needed to add metadata too |
| 4798 | * many times. */ |
| 4799 | if (!free_extents && !meta_ac) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4800 | err = -1; |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4801 | status = -EAGAIN; |
| 4802 | reason = RESTART_META; |
| 4803 | goto leave; |
| 4804 | } else if ((!free_extents) |
| 4805 | && (ocfs2_alloc_context_bits_left(meta_ac) |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4806 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4807 | err = -2; |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4808 | status = -EAGAIN; |
| 4809 | reason = RESTART_META; |
| 4810 | goto leave; |
| 4811 | } |
| 4812 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 4813 | status = __ocfs2_claim_clusters(handle, data_ac, 1, |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4814 | clusters_to_add, &bit_off, &num_bits); |
| 4815 | if (status < 0) { |
| 4816 | if (status != -ENOSPC) |
| 4817 | mlog_errno(status); |
| 4818 | goto leave; |
| 4819 | } |
| 4820 | |
| 4821 | BUG_ON(num_bits > clusters_to_add); |
| 4822 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4823 | /* reserve our write early -- insert_extent may update the tree root */ |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 4824 | status = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 4825 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4826 | if (status < 0) { |
| 4827 | mlog_errno(status); |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 4828 | need_free = 1; |
| 4829 | goto bail; |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4833 | trace_ocfs2_add_clusters_in_btree( |
| 4834 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 4835 | bit_off, num_bits); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4836 | status = ocfs2_insert_extent(handle, et, *logical_offset, block, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4837 | num_bits, flags, meta_ac); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4838 | if (status < 0) { |
| 4839 | mlog_errno(status); |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 4840 | need_free = 1; |
| 4841 | goto bail; |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4842 | } |
| 4843 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 4844 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4845 | |
| 4846 | clusters_to_add -= num_bits; |
| 4847 | *logical_offset += num_bits; |
| 4848 | |
| 4849 | if (clusters_to_add) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4850 | err = clusters_to_add; |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4851 | status = -EAGAIN; |
| 4852 | reason = RESTART_TRANS; |
| 4853 | } |
| 4854 | |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 4855 | bail: |
| 4856 | if (need_free) { |
| 4857 | if (data_ac->ac_which == OCFS2_AC_USE_LOCAL) |
| 4858 | ocfs2_free_local_alloc_bits(osb, handle, data_ac, |
| 4859 | bit_off, num_bits); |
| 4860 | else |
| 4861 | ocfs2_free_clusters(handle, |
| 4862 | data_ac->ac_inode, |
| 4863 | data_ac->ac_bh, |
| 4864 | ocfs2_clusters_to_blocks(osb->sb, bit_off), |
| 4865 | num_bits); |
| 4866 | } |
| 4867 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4868 | leave: |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4869 | if (reason_ret) |
| 4870 | *reason_ret = reason; |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 4871 | trace_ocfs2_add_clusters_in_btree_ret(status, reason, err); |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 4872 | return status; |
| 4873 | } |
| 4874 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4875 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 4876 | struct ocfs2_extent_rec *split_rec, |
| 4877 | u32 cpos, |
| 4878 | struct ocfs2_extent_rec *rec) |
| 4879 | { |
| 4880 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 4881 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 4882 | |
| 4883 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4884 | |
| 4885 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 4886 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 4887 | |
| 4888 | split_rec->e_blkno = rec->e_blkno; |
| 4889 | le64_add_cpu(&split_rec->e_blkno, |
| 4890 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 4891 | |
| 4892 | split_rec->e_flags = rec->e_flags; |
| 4893 | } |
| 4894 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4895 | static int ocfs2_split_and_insert(handle_t *handle, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4896 | struct ocfs2_extent_tree *et, |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4897 | struct ocfs2_path *path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4898 | struct buffer_head **last_eb_bh, |
| 4899 | int split_index, |
| 4900 | struct ocfs2_extent_rec *orig_split_rec, |
| 4901 | struct ocfs2_alloc_context *meta_ac) |
| 4902 | { |
| 4903 | int ret = 0, depth; |
| 4904 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 4905 | struct ocfs2_extent_rec tmprec; |
| 4906 | struct ocfs2_extent_list *rightmost_el; |
| 4907 | struct ocfs2_extent_rec rec; |
| 4908 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 4909 | struct ocfs2_insert_type insert; |
| 4910 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4911 | |
| 4912 | leftright: |
| 4913 | /* |
| 4914 | * Store a copy of the record on the stack - it might move |
| 4915 | * around as the tree is manipulated below. |
| 4916 | */ |
| 4917 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 4918 | |
Joel Becker | ce1d9ea | 2008-08-20 16:30:07 -0700 | [diff] [blame] | 4919 | rightmost_el = et->et_root_el; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4920 | |
| 4921 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 4922 | if (depth) { |
| 4923 | BUG_ON(!(*last_eb_bh)); |
| 4924 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 4925 | rightmost_el = &eb->h_list; |
| 4926 | } |
| 4927 | |
| 4928 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4929 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 4930 | ret = ocfs2_grow_tree(handle, et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 4931 | &depth, last_eb_bh, meta_ac); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4932 | if (ret) { |
| 4933 | mlog_errno(ret); |
| 4934 | goto out; |
| 4935 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4939 | insert.ins_appending = APPEND_NONE; |
| 4940 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4941 | insert.ins_tree_depth = depth; |
| 4942 | |
| 4943 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 4944 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 4945 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 4946 | le16_to_cpu(rec.e_leaf_clusters); |
| 4947 | |
| 4948 | if (split_rec.e_cpos == rec.e_cpos) { |
| 4949 | insert.ins_split = SPLIT_LEFT; |
| 4950 | } else if (insert_range == rec_range) { |
| 4951 | insert.ins_split = SPLIT_RIGHT; |
| 4952 | } else { |
| 4953 | /* |
| 4954 | * Left/right split. We fake this as a right split |
| 4955 | * first and then make a second pass as a left split. |
| 4956 | */ |
| 4957 | insert.ins_split = SPLIT_RIGHT; |
| 4958 | |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 4959 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 4960 | &tmprec, insert_range, &rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4961 | |
| 4962 | split_rec = tmprec; |
| 4963 | |
| 4964 | BUG_ON(do_leftright); |
| 4965 | do_leftright = 1; |
| 4966 | } |
| 4967 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 4968 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4969 | if (ret) { |
| 4970 | mlog_errno(ret); |
| 4971 | goto out; |
| 4972 | } |
| 4973 | |
| 4974 | if (do_leftright == 1) { |
| 4975 | u32 cpos; |
| 4976 | struct ocfs2_extent_list *el; |
| 4977 | |
| 4978 | do_leftright++; |
| 4979 | split_rec = *orig_split_rec; |
| 4980 | |
| 4981 | ocfs2_reinit_path(path, 1); |
| 4982 | |
| 4983 | cpos = le32_to_cpu(split_rec.e_cpos); |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 4984 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 4985 | if (ret) { |
| 4986 | mlog_errno(ret); |
| 4987 | goto out; |
| 4988 | } |
| 4989 | |
| 4990 | el = path_leaf_el(path); |
| 4991 | split_index = ocfs2_search_extent_list(el, cpos); |
Yingtai Xie | 981035b | 2014-08-06 16:03:54 -0700 | [diff] [blame] | 4992 | if (split_index == -1) { |
| 4993 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 4994 | "Owner %llu has an extent at cpos %u which can no longer be found\n", |
| 4995 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 4996 | cpos); |
Yingtai Xie | 981035b | 2014-08-06 16:03:54 -0700 | [diff] [blame] | 4997 | ret = -EROFS; |
| 4998 | goto out; |
| 4999 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5000 | goto leftright; |
| 5001 | } |
| 5002 | out: |
| 5003 | |
| 5004 | return ret; |
| 5005 | } |
| 5006 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5007 | static int ocfs2_replace_extent_rec(handle_t *handle, |
| 5008 | struct ocfs2_extent_tree *et, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5009 | struct ocfs2_path *path, |
| 5010 | struct ocfs2_extent_list *el, |
| 5011 | int split_index, |
| 5012 | struct ocfs2_extent_rec *split_rec) |
| 5013 | { |
| 5014 | int ret; |
| 5015 | |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5016 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5017 | path_num_items(path) - 1); |
| 5018 | if (ret) { |
| 5019 | mlog_errno(ret); |
| 5020 | goto out; |
| 5021 | } |
| 5022 | |
| 5023 | el->l_recs[split_index] = *split_rec; |
| 5024 | |
| 5025 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5026 | out: |
| 5027 | return ret; |
| 5028 | } |
| 5029 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5030 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5031 | * Split part or all of the extent record at split_index in the leaf |
| 5032 | * pointed to by path. Merge with the contiguous extent record if needed. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5033 | * |
| 5034 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 5035 | * |
| 5036 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 5037 | * of the tree is required. All other cases will degrade into a less |
| 5038 | * optimal tree layout. |
| 5039 | * |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5040 | * last_eb_bh should be the rightmost leaf block for any extent |
| 5041 | * btree. Since a split may grow the tree or a merge might shrink it, |
| 5042 | * the caller cannot trust the contents of that buffer after this call. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5043 | * |
| 5044 | * This code is optimized for readability - several passes might be |
| 5045 | * made over certain portions of the tree. All of those blocks will |
| 5046 | * have been brought into cache (and pinned via the journal), so the |
| 5047 | * extra overhead is not expressed in terms of disk reads. |
| 5048 | */ |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 5049 | int ocfs2_split_extent(handle_t *handle, |
| 5050 | struct ocfs2_extent_tree *et, |
| 5051 | struct ocfs2_path *path, |
| 5052 | int split_index, |
| 5053 | struct ocfs2_extent_rec *split_rec, |
| 5054 | struct ocfs2_alloc_context *meta_ac, |
| 5055 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5056 | { |
| 5057 | int ret = 0; |
| 5058 | struct ocfs2_extent_list *el = path_leaf_el(path); |
Mark Fasheh | e8aed34 | 2007-12-03 16:43:01 -0800 | [diff] [blame] | 5059 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5060 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 5061 | struct ocfs2_merge_ctxt ctxt; |
| 5062 | struct ocfs2_extent_list *rightmost_el; |
| 5063 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5064 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 5065 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 5066 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 5067 | ret = -EIO; |
| 5068 | mlog_errno(ret); |
| 5069 | goto out; |
| 5070 | } |
| 5071 | |
Xue jiufei | 9f99ad0 | 2015-06-24 16:55:07 -0700 | [diff] [blame] | 5072 | ret = ocfs2_figure_merge_contig_type(et, path, el, |
| 5073 | split_index, |
| 5074 | split_rec, |
| 5075 | &ctxt); |
| 5076 | if (ret) { |
| 5077 | mlog_errno(ret); |
| 5078 | goto out; |
| 5079 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5080 | |
| 5081 | /* |
| 5082 | * The core merge / split code wants to know how much room is |
Joel Becker | a1cf076 | 2009-02-13 03:45:49 -0800 | [diff] [blame] | 5083 | * left in this allocation tree, so we pass the |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5084 | * rightmost extent list. |
| 5085 | */ |
| 5086 | if (path->p_tree_depth) { |
| 5087 | struct ocfs2_extent_block *eb; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5088 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5089 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5090 | ocfs2_et_get_last_eb_blk(et), |
| 5091 | &last_eb_bh); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5092 | if (ret) { |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 5093 | mlog_errno(ret); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5094 | goto out; |
| 5095 | } |
| 5096 | |
| 5097 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5098 | rightmost_el = &eb->h_list; |
| 5099 | } else |
| 5100 | rightmost_el = path_root_el(path); |
| 5101 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5102 | if (rec->e_cpos == split_rec->e_cpos && |
| 5103 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 5104 | ctxt.c_split_covers_rec = 1; |
| 5105 | else |
| 5106 | ctxt.c_split_covers_rec = 0; |
| 5107 | |
| 5108 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 5109 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 5110 | trace_ocfs2_split_extent(split_index, ctxt.c_contig_type, |
| 5111 | ctxt.c_has_empty_extent, |
| 5112 | ctxt.c_split_covers_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5113 | |
| 5114 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 5115 | if (ctxt.c_split_covers_rec) |
Joel Becker | f3868d0 | 2009-02-17 19:46:04 -0800 | [diff] [blame] | 5116 | ret = ocfs2_replace_extent_rec(handle, et, path, el, |
Tao Ma | 47be12e | 2009-01-09 07:32:48 +0800 | [diff] [blame] | 5117 | split_index, split_rec); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5118 | else |
Joel Becker | d231129 | 2009-02-13 03:43:22 -0800 | [diff] [blame] | 5119 | ret = ocfs2_split_and_insert(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5120 | &last_eb_bh, split_index, |
| 5121 | split_rec, meta_ac); |
| 5122 | if (ret) |
| 5123 | mlog_errno(ret); |
| 5124 | } else { |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5125 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5126 | split_index, split_rec, |
Joel Becker | c495dd2 | 2009-02-13 02:19:11 -0800 | [diff] [blame] | 5127 | dealloc, &ctxt); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5128 | if (ret) |
| 5129 | mlog_errno(ret); |
| 5130 | } |
| 5131 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5132 | out: |
| 5133 | brelse(last_eb_bh); |
| 5134 | return ret; |
| 5135 | } |
| 5136 | |
| 5137 | /* |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5138 | * Change the flags of the already-existing extent at cpos for len clusters. |
| 5139 | * |
| 5140 | * new_flags: the flags we want to set. |
| 5141 | * clear_flags: the flags we want to clear. |
| 5142 | * phys: the new physical offset we want this new extent starts from. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5143 | * |
| 5144 | * If the existing extent is larger than the request, initiate a |
| 5145 | * split. An attempt will be made at merging with adjacent extents. |
| 5146 | * |
| 5147 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5148 | */ |
Tao Ma | 1aa75fe | 2009-08-18 11:28:39 +0800 | [diff] [blame] | 5149 | int ocfs2_change_extent_flag(handle_t *handle, |
| 5150 | struct ocfs2_extent_tree *et, |
| 5151 | u32 cpos, u32 len, u32 phys, |
| 5152 | struct ocfs2_alloc_context *meta_ac, |
| 5153 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 5154 | int new_flags, int clear_flags) |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5155 | { |
| 5156 | int ret, index; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5157 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
| 5158 | u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5159 | struct ocfs2_extent_rec split_rec; |
| 5160 | struct ocfs2_path *left_path = NULL; |
| 5161 | struct ocfs2_extent_list *el; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5162 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5163 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5164 | left_path = ocfs2_new_path_from_et(et); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5165 | if (!left_path) { |
| 5166 | ret = -ENOMEM; |
| 5167 | mlog_errno(ret); |
| 5168 | goto out; |
| 5169 | } |
| 5170 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5171 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5172 | if (ret) { |
| 5173 | mlog_errno(ret); |
| 5174 | goto out; |
| 5175 | } |
| 5176 | el = path_leaf_el(left_path); |
| 5177 | |
| 5178 | index = ocfs2_search_extent_list(el, cpos); |
Yingtai Xie | 981035b | 2014-08-06 16:03:54 -0700 | [diff] [blame] | 5179 | if (index == -1) { |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5180 | ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 5181 | "Owner %llu has an extent at cpos %u which can no longer be found\n", |
| 5182 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5183 | cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5184 | ret = -EROFS; |
| 5185 | goto out; |
| 5186 | } |
| 5187 | |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5188 | ret = -EIO; |
| 5189 | rec = &el->l_recs[index]; |
| 5190 | if (new_flags && (rec->e_flags & new_flags)) { |
| 5191 | mlog(ML_ERROR, "Owner %llu tried to set %d flags on an " |
Darrick J. Wong | 86544fb | 2016-11-09 14:13:09 -0800 | [diff] [blame] | 5192 | "extent that already had them\n", |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5193 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5194 | new_flags); |
| 5195 | goto out; |
| 5196 | } |
| 5197 | |
| 5198 | if (clear_flags && !(rec->e_flags & clear_flags)) { |
| 5199 | mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an " |
Darrick J. Wong | 86544fb | 2016-11-09 14:13:09 -0800 | [diff] [blame] | 5200 | "extent that didn't have them\n", |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5201 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5202 | clear_flags); |
| 5203 | goto out; |
| 5204 | } |
| 5205 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5206 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 5207 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 5208 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 5209 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5210 | split_rec.e_flags = rec->e_flags; |
| 5211 | if (new_flags) |
| 5212 | split_rec.e_flags |= new_flags; |
| 5213 | if (clear_flags) |
| 5214 | split_rec.e_flags &= ~clear_flags; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5215 | |
Tao Ma | e2e9f60 | 2009-08-18 11:22:34 +0800 | [diff] [blame] | 5216 | ret = ocfs2_split_extent(handle, et, left_path, |
| 5217 | index, &split_rec, meta_ac, |
| 5218 | dealloc); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5219 | if (ret) |
| 5220 | mlog_errno(ret); |
| 5221 | |
| 5222 | out: |
| 5223 | ocfs2_free_path(left_path); |
| 5224 | return ret; |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5225 | |
| 5226 | } |
| 5227 | |
| 5228 | /* |
| 5229 | * Mark the already-existing extent at cpos as written for len clusters. |
| 5230 | * This removes the unwritten extent flag. |
| 5231 | * |
| 5232 | * If the existing extent is larger than the request, initiate a |
| 5233 | * split. An attempt will be made at merging with adjacent extents. |
| 5234 | * |
| 5235 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 5236 | */ |
| 5237 | int ocfs2_mark_extent_written(struct inode *inode, |
| 5238 | struct ocfs2_extent_tree *et, |
| 5239 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 5240 | struct ocfs2_alloc_context *meta_ac, |
| 5241 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5242 | { |
| 5243 | int ret; |
| 5244 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 5245 | trace_ocfs2_mark_extent_written( |
| 5246 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5247 | cpos, len, phys); |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5248 | |
| 5249 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 5250 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents that are being written to, but the feature bit is not set in the super block\n", |
Tao Ma | 555936b | 2009-08-18 11:22:21 +0800 | [diff] [blame] | 5251 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 5252 | ret = -EROFS; |
| 5253 | goto out; |
| 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * XXX: This should be fixed up so that we just re-insert the |
| 5258 | * next extent records. |
| 5259 | */ |
| 5260 | ocfs2_et_extent_map_truncate(et, 0); |
| 5261 | |
| 5262 | ret = ocfs2_change_extent_flag(handle, et, cpos, |
| 5263 | len, phys, meta_ac, dealloc, |
| 5264 | 0, OCFS2_EXT_UNWRITTEN); |
| 5265 | if (ret) |
| 5266 | mlog_errno(ret); |
| 5267 | |
| 5268 | out: |
| 5269 | return ret; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 5270 | } |
| 5271 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5272 | static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
| 5273 | struct ocfs2_path *path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5274 | int index, u32 new_range, |
| 5275 | struct ocfs2_alloc_context *meta_ac) |
| 5276 | { |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 5277 | int ret, depth, credits; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5278 | struct buffer_head *last_eb_bh = NULL; |
| 5279 | struct ocfs2_extent_block *eb; |
| 5280 | struct ocfs2_extent_list *rightmost_el, *el; |
| 5281 | struct ocfs2_extent_rec split_rec; |
| 5282 | struct ocfs2_extent_rec *rec; |
| 5283 | struct ocfs2_insert_type insert; |
| 5284 | |
| 5285 | /* |
| 5286 | * Setup the record to split before we grow the tree. |
| 5287 | */ |
| 5288 | el = path_leaf_el(path); |
| 5289 | rec = &el->l_recs[index]; |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5290 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
| 5291 | &split_rec, new_range, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5292 | |
| 5293 | depth = path->p_tree_depth; |
| 5294 | if (depth > 0) { |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 5295 | ret = ocfs2_read_extent_block(et->et_ci, |
Joel Becker | 5e96581 | 2008-11-13 14:49:16 -0800 | [diff] [blame] | 5296 | ocfs2_et_get_last_eb_blk(et), |
| 5297 | &last_eb_bh); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5298 | if (ret < 0) { |
| 5299 | mlog_errno(ret); |
| 5300 | goto out; |
| 5301 | } |
| 5302 | |
| 5303 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5304 | rightmost_el = &eb->h_list; |
| 5305 | } else |
| 5306 | rightmost_el = path_leaf_el(path); |
| 5307 | |
Tao Ma | c901fb0 | 2010-04-26 14:34:57 +0800 | [diff] [blame] | 5308 | credits = path->p_tree_depth + |
| 5309 | ocfs2_extend_meta_needed(et->et_root_el); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5310 | ret = ocfs2_extend_trans(handle, credits); |
| 5311 | if (ret) { |
| 5312 | mlog_errno(ret); |
| 5313 | goto out; |
| 5314 | } |
| 5315 | |
| 5316 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 5317 | le16_to_cpu(rightmost_el->l_count)) { |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5318 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5319 | meta_ac); |
| 5320 | if (ret) { |
| 5321 | mlog_errno(ret); |
| 5322 | goto out; |
| 5323 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 5327 | insert.ins_appending = APPEND_NONE; |
| 5328 | insert.ins_contig = CONTIG_NONE; |
| 5329 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5330 | insert.ins_tree_depth = depth; |
| 5331 | |
Joel Becker | 3505bec | 2009-02-13 02:57:58 -0800 | [diff] [blame] | 5332 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5333 | if (ret) |
| 5334 | mlog_errno(ret); |
| 5335 | |
| 5336 | out: |
| 5337 | brelse(last_eb_bh); |
| 5338 | return ret; |
| 5339 | } |
| 5340 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5341 | static int ocfs2_truncate_rec(handle_t *handle, |
| 5342 | struct ocfs2_extent_tree *et, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5343 | struct ocfs2_path *path, int index, |
| 5344 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5345 | u32 cpos, u32 len) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5346 | { |
| 5347 | int ret; |
| 5348 | u32 left_cpos, rec_range, trunc_range; |
piaojun | c14688e | 2016-05-19 17:09:47 -0700 | [diff] [blame] | 5349 | int is_rightmost_tree_rec = 0; |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5350 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5351 | struct ocfs2_path *left_path = NULL; |
| 5352 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 5353 | struct ocfs2_extent_rec *rec; |
| 5354 | struct ocfs2_extent_block *eb; |
| 5355 | |
| 5356 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
Xue jiufei | 1721598 | 2016-03-25 14:21:41 -0700 | [diff] [blame] | 5357 | /* extend credit for ocfs2_remove_rightmost_path */ |
| 5358 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5359 | handle->h_buffer_credits, |
| 5360 | path); |
| 5361 | if (ret) { |
| 5362 | mlog_errno(ret); |
| 5363 | goto out; |
| 5364 | } |
| 5365 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5366 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5367 | if (ret) { |
| 5368 | mlog_errno(ret); |
| 5369 | goto out; |
| 5370 | } |
| 5371 | |
| 5372 | index--; |
| 5373 | } |
| 5374 | |
| 5375 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 5376 | path->p_tree_depth) { |
| 5377 | /* |
| 5378 | * Check whether this is the rightmost tree record. If |
| 5379 | * we remove all of this record or part of its right |
| 5380 | * edge then an update of the record lengths above it |
| 5381 | * will be required. |
| 5382 | */ |
| 5383 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 5384 | if (eb->h_next_leaf_blk == 0) |
| 5385 | is_rightmost_tree_rec = 1; |
| 5386 | } |
| 5387 | |
| 5388 | rec = &el->l_recs[index]; |
| 5389 | if (index == 0 && path->p_tree_depth && |
| 5390 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 5391 | /* |
| 5392 | * Changing the leftmost offset (via partial or whole |
| 5393 | * record truncate) of an interior (or rightmost) path |
| 5394 | * means we have to update the subtree that is formed |
| 5395 | * by this leaf and the one to it's left. |
| 5396 | * |
| 5397 | * There are two cases we can skip: |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5398 | * 1) Path is the leftmost one in our btree. |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5399 | * 2) The leaf is rightmost and will be empty after |
| 5400 | * we remove the extent record - the rotate code |
| 5401 | * knows how to update the newly formed edge. |
| 5402 | */ |
| 5403 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5404 | ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5405 | if (ret) { |
| 5406 | mlog_errno(ret); |
| 5407 | goto out; |
| 5408 | } |
| 5409 | |
| 5410 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5411 | left_path = ocfs2_new_path_from_path(path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5412 | if (!left_path) { |
| 5413 | ret = -ENOMEM; |
| 5414 | mlog_errno(ret); |
| 5415 | goto out; |
| 5416 | } |
| 5417 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5418 | ret = ocfs2_find_path(et->et_ci, left_path, |
| 5419 | left_cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5420 | if (ret) { |
| 5421 | mlog_errno(ret); |
| 5422 | goto out; |
| 5423 | } |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 5428 | handle->h_buffer_credits, |
| 5429 | path); |
| 5430 | if (ret) { |
| 5431 | mlog_errno(ret); |
| 5432 | goto out; |
| 5433 | } |
| 5434 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5435 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5436 | if (ret) { |
| 5437 | mlog_errno(ret); |
| 5438 | goto out; |
| 5439 | } |
| 5440 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5441 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5442 | if (ret) { |
| 5443 | mlog_errno(ret); |
| 5444 | goto out; |
| 5445 | } |
| 5446 | |
| 5447 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5448 | trunc_range = cpos + len; |
| 5449 | |
| 5450 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 5451 | int next_free; |
| 5452 | |
| 5453 | memset(rec, 0, sizeof(*rec)); |
| 5454 | ocfs2_cleanup_merge(el, index); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5455 | |
| 5456 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5457 | if (is_rightmost_tree_rec && next_free > 1) { |
| 5458 | /* |
| 5459 | * We skip the edge update if this path will |
| 5460 | * be deleted by the rotate code. |
| 5461 | */ |
| 5462 | rec = &el->l_recs[next_free - 1]; |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5463 | ocfs2_adjust_rightmost_records(handle, et, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5464 | rec); |
| 5465 | } |
| 5466 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 5467 | /* Remove leftmost portion of the record. */ |
| 5468 | le32_add_cpu(&rec->e_cpos, len); |
| 5469 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 5470 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5471 | } else if (rec_range == trunc_range) { |
| 5472 | /* Remove rightmost portion of the record */ |
| 5473 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 5474 | if (is_rightmost_tree_rec) |
Joel Becker | d401dc1 | 2009-02-13 02:24:10 -0800 | [diff] [blame] | 5475 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5476 | } else { |
| 5477 | /* Caller should have trapped this. */ |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5478 | mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) " |
| 5479 | "(%u, %u)\n", |
| 5480 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5481 | le32_to_cpu(rec->e_cpos), |
| 5482 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 5483 | BUG(); |
| 5484 | } |
| 5485 | |
| 5486 | if (left_path) { |
| 5487 | int subtree_index; |
| 5488 | |
Joel Becker | 7dc0280 | 2009-02-12 19:20:13 -0800 | [diff] [blame] | 5489 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
Joel Becker | 4619c73 | 2009-02-12 19:02:36 -0800 | [diff] [blame] | 5490 | ocfs2_complete_edge_insert(handle, left_path, path, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5491 | subtree_index); |
| 5492 | } |
| 5493 | |
| 5494 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 5495 | |
Joel Becker | 70f18c0 | 2009-02-13 02:09:31 -0800 | [diff] [blame] | 5496 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5497 | if (ret) { |
| 5498 | mlog_errno(ret); |
| 5499 | goto out; |
| 5500 | } |
| 5501 | |
| 5502 | out: |
| 5503 | ocfs2_free_path(left_path); |
| 5504 | return ret; |
| 5505 | } |
| 5506 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5507 | int ocfs2_remove_extent(handle_t *handle, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5508 | struct ocfs2_extent_tree *et, |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5509 | u32 cpos, u32 len, |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5510 | struct ocfs2_alloc_context *meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5511 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5512 | { |
| 5513 | int ret, index; |
| 5514 | u32 rec_range, trunc_range; |
| 5515 | struct ocfs2_extent_rec *rec; |
| 5516 | struct ocfs2_extent_list *el; |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 5517 | struct ocfs2_path *path = NULL; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5518 | |
Joel Becker | 4c911ee | 2009-02-13 02:50:12 -0800 | [diff] [blame] | 5519 | /* |
| 5520 | * XXX: Why are we truncating to 0 instead of wherever this |
| 5521 | * affects us? |
| 5522 | */ |
| 5523 | ocfs2_et_extent_map_truncate(et, 0); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5524 | |
Joel Becker | ffdd7a5 | 2008-10-17 22:32:01 -0700 | [diff] [blame] | 5525 | path = ocfs2_new_path_from_et(et); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5526 | if (!path) { |
| 5527 | ret = -ENOMEM; |
| 5528 | mlog_errno(ret); |
| 5529 | goto out; |
| 5530 | } |
| 5531 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5532 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5533 | if (ret) { |
| 5534 | mlog_errno(ret); |
| 5535 | goto out; |
| 5536 | } |
| 5537 | |
| 5538 | el = path_leaf_el(path); |
| 5539 | index = ocfs2_search_extent_list(el, cpos); |
Yingtai Xie | 981035b | 2014-08-06 16:03:54 -0700 | [diff] [blame] | 5540 | if (index == -1) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5541 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 5542 | "Owner %llu has an extent at cpos %u which can no longer be found\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5543 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5544 | cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5545 | ret = -EROFS; |
| 5546 | goto out; |
| 5547 | } |
| 5548 | |
| 5549 | /* |
| 5550 | * We have 3 cases of extent removal: |
| 5551 | * 1) Range covers the entire extent rec |
| 5552 | * 2) Range begins or ends on one edge of the extent rec |
| 5553 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 5554 | * |
| 5555 | * For case 1 we remove the extent rec and left rotate to |
| 5556 | * fill the hole. |
| 5557 | * |
| 5558 | * For case 2 we just shrink the existing extent rec, with a |
| 5559 | * tree update if the shrinking edge is also the edge of an |
| 5560 | * extent block. |
| 5561 | * |
| 5562 | * For case 3 we do a right split to turn the extent rec into |
| 5563 | * something case 2 can handle. |
| 5564 | */ |
| 5565 | rec = &el->l_recs[index]; |
| 5566 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 5567 | trunc_range = cpos + len; |
| 5568 | |
| 5569 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 5570 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 5571 | trace_ocfs2_remove_extent( |
| 5572 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
| 5573 | cpos, len, index, le32_to_cpu(rec->e_cpos), |
| 5574 | ocfs2_rec_clusters(el, rec)); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5575 | |
| 5576 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
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 | } else { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5584 | ret = ocfs2_split_tree(handle, et, path, index, |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5585 | trunc_range, meta_ac); |
| 5586 | if (ret) { |
| 5587 | mlog_errno(ret); |
| 5588 | goto out; |
| 5589 | } |
| 5590 | |
| 5591 | /* |
| 5592 | * The split could have manipulated the tree enough to |
| 5593 | * move the record location, so we have to look for it again. |
| 5594 | */ |
| 5595 | ocfs2_reinit_path(path, 1); |
| 5596 | |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 5597 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5598 | if (ret) { |
| 5599 | mlog_errno(ret); |
| 5600 | goto out; |
| 5601 | } |
| 5602 | |
| 5603 | el = path_leaf_el(path); |
| 5604 | index = ocfs2_search_extent_list(el, cpos); |
Yingtai Xie | 981035b | 2014-08-06 16:03:54 -0700 | [diff] [blame] | 5605 | if (index == -1) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5606 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 5607 | "Owner %llu: split at cpos %u lost record\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5608 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5609 | cpos); |
| 5610 | ret = -EROFS; |
| 5611 | goto out; |
| 5612 | } |
| 5613 | |
| 5614 | /* |
| 5615 | * Double check our values here. If anything is fishy, |
| 5616 | * it's easier to catch it at the top level. |
| 5617 | */ |
| 5618 | rec = &el->l_recs[index]; |
| 5619 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 5620 | ocfs2_rec_clusters(el, rec); |
| 5621 | if (rec_range != trunc_range) { |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5622 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 5623 | "Owner %llu: error after split at cpos %u trunc len %u, existing record is (%u,%u)\n", |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5624 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5625 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 5626 | ocfs2_rec_clusters(el, rec)); |
| 5627 | ret = -EROFS; |
| 5628 | goto out; |
| 5629 | } |
| 5630 | |
Joel Becker | 043beeb | 2009-02-13 02:42:30 -0800 | [diff] [blame] | 5631 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
| 5632 | cpos, len); |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 5633 | if (ret) { |
| 5634 | mlog_errno(ret); |
| 5635 | goto out; |
| 5636 | } |
| 5637 | } |
| 5638 | |
| 5639 | out: |
| 5640 | ocfs2_free_path(path); |
| 5641 | return ret; |
| 5642 | } |
| 5643 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5644 | /* |
| 5645 | * ocfs2_reserve_blocks_for_rec_trunc() would look basically the |
| 5646 | * same as ocfs2_lock_alloctors(), except for it accepts a blocks |
| 5647 | * number to reserve some extra blocks, and it only handles meta |
| 5648 | * data allocations. |
| 5649 | * |
| 5650 | * Currently, only ocfs2_remove_btree_range() uses it for truncating |
| 5651 | * and punching holes. |
| 5652 | */ |
| 5653 | static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode, |
| 5654 | struct ocfs2_extent_tree *et, |
| 5655 | u32 extents_to_split, |
| 5656 | struct ocfs2_alloc_context **ac, |
| 5657 | int extra_blocks) |
| 5658 | { |
| 5659 | int ret = 0, num_free_extents; |
| 5660 | unsigned int max_recs_needed = 2 * extents_to_split; |
| 5661 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5662 | |
| 5663 | *ac = NULL; |
| 5664 | |
Jun Piao | 964f14a | 2017-09-06 16:19:11 -0700 | [diff] [blame^] | 5665 | num_free_extents = ocfs2_num_free_extents(et); |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5666 | if (num_free_extents < 0) { |
| 5667 | ret = num_free_extents; |
| 5668 | mlog_errno(ret); |
| 5669 | goto out; |
| 5670 | } |
| 5671 | |
| 5672 | if (!num_free_extents || |
| 5673 | (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) |
| 5674 | extra_blocks += ocfs2_extend_meta_needed(et->et_root_el); |
| 5675 | |
| 5676 | if (extra_blocks) { |
| 5677 | ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac); |
| 5678 | if (ret < 0) { |
| 5679 | if (ret != -ENOSPC) |
| 5680 | mlog_errno(ret); |
| 5681 | goto out; |
| 5682 | } |
| 5683 | } |
| 5684 | |
| 5685 | out: |
| 5686 | if (ret) { |
| 5687 | if (*ac) { |
| 5688 | ocfs2_free_alloc_context(*ac); |
| 5689 | *ac = NULL; |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | return ret; |
| 5694 | } |
| 5695 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5696 | int ocfs2_remove_btree_range(struct inode *inode, |
| 5697 | struct ocfs2_extent_tree *et, |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5698 | u32 cpos, u32 phys_cpos, u32 len, int flags, |
| 5699 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 5700 | u64 refcount_loc, bool refcount_tree_locked) |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5701 | { |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5702 | int ret, credits = 0, extra_blocks = 0; |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5703 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 5704 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5705 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5706 | handle_t *handle; |
| 5707 | struct ocfs2_alloc_context *meta_ac = NULL; |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5708 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5709 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5710 | if ((flags & OCFS2_EXT_REFCOUNTED) && len) { |
Darrick J. Wong | 84e4008 | 2016-11-09 14:13:09 -0800 | [diff] [blame] | 5711 | BUG_ON(!ocfs2_is_refcount_inode(inode)); |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5712 | |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 5713 | if (!refcount_tree_locked) { |
| 5714 | ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1, |
| 5715 | &ref_tree, NULL); |
| 5716 | if (ret) { |
| 5717 | mlog_errno(ret); |
| 5718 | goto bail; |
| 5719 | } |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | ret = ocfs2_prepare_refcount_change_for_del(inode, |
| 5723 | refcount_loc, |
| 5724 | phys_blkno, |
| 5725 | len, |
| 5726 | &credits, |
| 5727 | &extra_blocks); |
| 5728 | if (ret < 0) { |
| 5729 | mlog_errno(ret); |
Joseph Qi | 33add0e | 2013-07-03 15:00:52 -0700 | [diff] [blame] | 5730 | goto bail; |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac, |
| 5735 | extra_blocks); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5736 | if (ret) { |
| 5737 | mlog_errno(ret); |
Joseph Qi | 33add0e | 2013-07-03 15:00:52 -0700 | [diff] [blame] | 5738 | goto bail; |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5739 | } |
| 5740 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5741 | inode_lock(tl_inode); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5742 | |
| 5743 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5744 | ret = __ocfs2_flush_truncate_log(osb); |
| 5745 | if (ret < 0) { |
| 5746 | mlog_errno(ret); |
| 5747 | goto out; |
| 5748 | } |
| 5749 | } |
| 5750 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5751 | handle = ocfs2_start_trans(osb, |
| 5752 | ocfs2_remove_extent_credits(osb->sb) + credits); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5753 | if (IS_ERR(handle)) { |
| 5754 | ret = PTR_ERR(handle); |
| 5755 | mlog_errno(ret); |
| 5756 | goto out; |
| 5757 | } |
| 5758 | |
Joel Becker | d9a0a1f | 2009-02-12 17:32:34 -0800 | [diff] [blame] | 5759 | ret = ocfs2_et_root_journal_access(handle, et, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5760 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5761 | if (ret) { |
| 5762 | mlog_errno(ret); |
Wengang Wang | b8a0ae5 | 2011-10-12 15:22:15 +0800 | [diff] [blame] | 5763 | goto out_commit; |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5764 | } |
| 5765 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 5766 | dquot_free_space_nodirty(inode, |
Mark Fasheh | fd4ef23 | 2009-01-29 15:06:21 -0800 | [diff] [blame] | 5767 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
| 5768 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5769 | ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5770 | if (ret) { |
| 5771 | mlog_errno(ret); |
| 5772 | goto out_commit; |
| 5773 | } |
| 5774 | |
Joel Becker | 6136ca5 | 2009-02-12 19:32:43 -0800 | [diff] [blame] | 5775 | ocfs2_et_update_clusters(et, -len); |
Darrick J. Wong | 6fdb702 | 2014-04-03 14:47:08 -0700 | [diff] [blame] | 5776 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5777 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 5778 | ocfs2_journal_dirty(handle, et->et_root_bh); |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5779 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5780 | if (phys_blkno) { |
| 5781 | if (flags & OCFS2_EXT_REFCOUNTED) |
| 5782 | ret = ocfs2_decrease_refcount(inode, handle, |
| 5783 | ocfs2_blocks_to_clusters(osb->sb, |
| 5784 | phys_blkno), |
| 5785 | len, meta_ac, |
| 5786 | dealloc, 1); |
| 5787 | else |
| 5788 | ret = ocfs2_truncate_log_append(osb, handle, |
| 5789 | phys_blkno, len); |
| 5790 | if (ret) |
| 5791 | mlog_errno(ret); |
| 5792 | |
| 5793 | } |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5794 | |
| 5795 | out_commit: |
| 5796 | ocfs2_commit_trans(osb, handle); |
| 5797 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5798 | inode_unlock(tl_inode); |
Joseph Qi | 33add0e | 2013-07-03 15:00:52 -0700 | [diff] [blame] | 5799 | bail: |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5800 | if (meta_ac) |
| 5801 | ocfs2_free_alloc_context(meta_ac); |
| 5802 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 5803 | if (ref_tree) |
| 5804 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
| 5805 | |
Mark Fasheh | fecc011 | 2008-11-12 15:16:38 -0800 | [diff] [blame] | 5806 | return ret; |
| 5807 | } |
| 5808 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5809 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5810 | { |
| 5811 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5812 | struct ocfs2_dinode *di; |
| 5813 | struct ocfs2_truncate_log *tl; |
| 5814 | |
| 5815 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5816 | tl = &di->id2.i_dealloc; |
| 5817 | |
| 5818 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 5819 | "slot %d, invalid truncate log parameters: used = " |
| 5820 | "%u, count = %u\n", osb->slot_num, |
| 5821 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 5822 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 5823 | } |
| 5824 | |
| 5825 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 5826 | unsigned int new_start) |
| 5827 | { |
| 5828 | unsigned int tail_index; |
| 5829 | unsigned int current_tail; |
| 5830 | |
| 5831 | /* No records, nothing to coalesce */ |
| 5832 | if (!le16_to_cpu(tl->tl_used)) |
| 5833 | return 0; |
| 5834 | |
| 5835 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 5836 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 5837 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 5838 | |
| 5839 | return current_tail == new_start; |
| 5840 | } |
| 5841 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5842 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 5843 | handle_t *handle, |
| 5844 | u64 start_blk, |
| 5845 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5846 | { |
| 5847 | int status, index; |
| 5848 | unsigned int start_cluster, tl_count; |
| 5849 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5850 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5851 | struct ocfs2_dinode *di; |
| 5852 | struct ocfs2_truncate_log *tl; |
| 5853 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5854 | BUG_ON(inode_trylock(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5855 | |
| 5856 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 5857 | |
| 5858 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5859 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 5860 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 5861 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 5862 | * corruption is a code bug */ |
| 5863 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 5864 | |
| 5865 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5866 | tl_count = le16_to_cpu(tl->tl_count); |
| 5867 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 5868 | tl_count == 0, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 5869 | "Truncate record count on #%llu invalid " |
| 5870 | "wanted %u, actual %u\n", |
| 5871 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5872 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 5873 | le16_to_cpu(tl->tl_count)); |
| 5874 | |
| 5875 | /* Caller should have known to flush before calling us. */ |
| 5876 | index = le16_to_cpu(tl->tl_used); |
| 5877 | if (index >= tl_count) { |
| 5878 | status = -ENOSPC; |
| 5879 | mlog_errno(status); |
| 5880 | goto bail; |
| 5881 | } |
| 5882 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5883 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5884 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5885 | if (status < 0) { |
| 5886 | mlog_errno(status); |
| 5887 | goto bail; |
| 5888 | } |
| 5889 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 5890 | trace_ocfs2_truncate_log_append( |
| 5891 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index, |
| 5892 | start_cluster, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5893 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 5894 | /* |
| 5895 | * Move index back to the record we are coalescing with. |
| 5896 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 5897 | */ |
| 5898 | index--; |
| 5899 | |
| 5900 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 5901 | trace_ocfs2_truncate_log_append( |
| 5902 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
| 5903 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 5904 | num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5905 | } else { |
| 5906 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 5907 | tl->tl_used = cpu_to_le16(index + 1); |
| 5908 | } |
| 5909 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 5910 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 5911 | ocfs2_journal_dirty(handle, tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5912 | |
Tao Ma | 50308d8 | 2010-11-04 15:14:11 +0800 | [diff] [blame] | 5913 | osb->truncated_clusters += num_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5914 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5915 | return status; |
| 5916 | } |
| 5917 | |
| 5918 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5919 | struct inode *data_alloc_inode, |
| 5920 | struct buffer_head *data_alloc_bh) |
| 5921 | { |
| 5922 | int status = 0; |
| 5923 | int i; |
| 5924 | unsigned int num_clusters; |
| 5925 | u64 start_blk; |
| 5926 | struct ocfs2_truncate_rec rec; |
| 5927 | struct ocfs2_dinode *di; |
| 5928 | struct ocfs2_truncate_log *tl; |
| 5929 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5930 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
Junxiao Bi | 2b0ad00 | 2016-09-19 14:44:21 -0700 | [diff] [blame] | 5931 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5932 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5933 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 5934 | tl = &di->id2.i_dealloc; |
| 5935 | i = le16_to_cpu(tl->tl_used) - 1; |
| 5936 | while (i >= 0) { |
Junxiao Bi | 2b0ad00 | 2016-09-19 14:44:21 -0700 | [diff] [blame] | 5937 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 5938 | if (IS_ERR(handle)) { |
| 5939 | status = PTR_ERR(handle); |
| 5940 | mlog_errno(status); |
| 5941 | goto bail; |
| 5942 | } |
| 5943 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5944 | /* Caller has given us at least enough credits to |
| 5945 | * update the truncate log dinode */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5946 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 5947 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5948 | if (status < 0) { |
| 5949 | mlog_errno(status); |
| 5950 | goto bail; |
| 5951 | } |
| 5952 | |
| 5953 | tl->tl_used = cpu_to_le16(i); |
| 5954 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 5955 | ocfs2_journal_dirty(handle, tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5956 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5957 | rec = tl->tl_recs[i]; |
| 5958 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 5959 | le32_to_cpu(rec.t_start)); |
| 5960 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 5961 | |
| 5962 | /* if start_blk is not set, we ignore the record as |
| 5963 | * invalid. */ |
| 5964 | if (start_blk) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 5965 | trace_ocfs2_replay_truncate_records( |
| 5966 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
| 5967 | i, le32_to_cpu(rec.t_start), num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5968 | |
| 5969 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 5970 | data_alloc_bh, start_blk, |
| 5971 | num_clusters); |
| 5972 | if (status < 0) { |
| 5973 | mlog_errno(status); |
| 5974 | goto bail; |
| 5975 | } |
| 5976 | } |
Xue jiufei | 102c259 | 2016-03-25 14:21:44 -0700 | [diff] [blame] | 5977 | |
Junxiao Bi | 2b0ad00 | 2016-09-19 14:44:21 -0700 | [diff] [blame] | 5978 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5979 | i--; |
| 5980 | } |
| 5981 | |
Tao Ma | 50308d8 | 2010-11-04 15:14:11 +0800 | [diff] [blame] | 5982 | osb->truncated_clusters = 0; |
| 5983 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5984 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5985 | return status; |
| 5986 | } |
| 5987 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5988 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 5989 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5990 | { |
| 5991 | int status; |
| 5992 | unsigned int num_to_flush; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5993 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5994 | struct inode *data_alloc_inode = NULL; |
| 5995 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 5996 | struct buffer_head *data_alloc_bh = NULL; |
| 5997 | struct ocfs2_dinode *di; |
| 5998 | struct ocfs2_truncate_log *tl; |
| 5999 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6000 | BUG_ON(inode_trylock(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6001 | |
| 6002 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6003 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 6004 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
| 6005 | * by the underlying call to ocfs2_read_inode_block(), so any |
| 6006 | * corruption is a code bug */ |
| 6007 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 6008 | |
| 6009 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6010 | num_to_flush = le16_to_cpu(tl->tl_used); |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6011 | trace_ocfs2_flush_truncate_log( |
| 6012 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
| 6013 | num_to_flush); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6014 | if (!num_to_flush) { |
| 6015 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6016 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6017 | } |
| 6018 | |
| 6019 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 6020 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 6021 | OCFS2_INVALID_SLOT); |
| 6022 | if (!data_alloc_inode) { |
| 6023 | status = -EINVAL; |
| 6024 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6025 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6026 | } |
| 6027 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6028 | inode_lock(data_alloc_inode); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6029 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6030 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6031 | if (status < 0) { |
| 6032 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6033 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6034 | } |
| 6035 | |
Junxiao Bi | 2b0ad00 | 2016-09-19 14:44:21 -0700 | [diff] [blame] | 6036 | status = ocfs2_replay_truncate_records(osb, data_alloc_inode, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6037 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6038 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6039 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6040 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6041 | brelse(data_alloc_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6042 | ocfs2_inode_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6043 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6044 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6045 | inode_unlock(data_alloc_inode); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6046 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6047 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 6048 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6049 | return status; |
| 6050 | } |
| 6051 | |
| 6052 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 6053 | { |
| 6054 | int status; |
| 6055 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6056 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6057 | inode_lock(tl_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6058 | status = __ocfs2_flush_truncate_log(osb); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6059 | inode_unlock(tl_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6060 | |
| 6061 | return status; |
| 6062 | } |
| 6063 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6064 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6065 | { |
| 6066 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6067 | struct ocfs2_super *osb = |
| 6068 | container_of(work, struct ocfs2_super, |
| 6069 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6070 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6071 | status = ocfs2_flush_truncate_log(osb); |
| 6072 | if (status < 0) |
| 6073 | mlog_errno(status); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 6074 | else |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 6075 | ocfs2_init_steal_slots(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6076 | } |
| 6077 | |
| 6078 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 6079 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 6080 | int cancel) |
| 6081 | { |
Xue jiufei | a9e9aca | 2014-06-04 16:06:13 -0700 | [diff] [blame] | 6082 | if (osb->osb_tl_inode && |
| 6083 | atomic_read(&osb->osb_tl_disable) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6084 | /* We want to push off log flushes while truncates are |
| 6085 | * still running. */ |
| 6086 | if (cancel) |
| 6087 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 6088 | |
jiangyiwen | 35ddf78 | 2016-03-25 14:21:32 -0700 | [diff] [blame] | 6089 | queue_delayed_work(osb->ocfs2_wq, &osb->osb_truncate_log_wq, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6090 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 6091 | } |
| 6092 | } |
| 6093 | |
Eric Ren | 2070ad1 | 2016-08-02 14:02:10 -0700 | [diff] [blame] | 6094 | /* |
| 6095 | * Try to flush truncate logs if we can free enough clusters from it. |
| 6096 | * As for return value, "< 0" means error, "0" no space and "1" means |
| 6097 | * we have freed enough spaces and let the caller try to allocate again. |
| 6098 | */ |
| 6099 | int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb, |
| 6100 | unsigned int needed) |
| 6101 | { |
| 6102 | tid_t target; |
| 6103 | int ret = 0; |
| 6104 | unsigned int truncated_clusters; |
| 6105 | |
| 6106 | inode_lock(osb->osb_tl_inode); |
| 6107 | truncated_clusters = osb->truncated_clusters; |
| 6108 | inode_unlock(osb->osb_tl_inode); |
| 6109 | |
| 6110 | /* |
| 6111 | * Check whether we can succeed in allocating if we free |
| 6112 | * the truncate log. |
| 6113 | */ |
| 6114 | if (truncated_clusters < needed) |
| 6115 | goto out; |
| 6116 | |
| 6117 | ret = ocfs2_flush_truncate_log(osb); |
| 6118 | if (ret) { |
| 6119 | mlog_errno(ret); |
| 6120 | goto out; |
| 6121 | } |
| 6122 | |
| 6123 | if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) { |
| 6124 | jbd2_log_wait_commit(osb->journal->j_journal, target); |
| 6125 | ret = 1; |
| 6126 | } |
| 6127 | out: |
| 6128 | return ret; |
| 6129 | } |
| 6130 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6131 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 6132 | int slot_num, |
| 6133 | struct inode **tl_inode, |
| 6134 | struct buffer_head **tl_bh) |
| 6135 | { |
| 6136 | int status; |
| 6137 | struct inode *inode = NULL; |
| 6138 | struct buffer_head *bh = NULL; |
| 6139 | |
| 6140 | inode = ocfs2_get_system_file_inode(osb, |
| 6141 | TRUNCATE_LOG_SYSTEM_INODE, |
| 6142 | slot_num); |
| 6143 | if (!inode) { |
| 6144 | status = -EINVAL; |
| 6145 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 6146 | goto bail; |
| 6147 | } |
| 6148 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 6149 | status = ocfs2_read_inode_block(inode, &bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6150 | if (status < 0) { |
| 6151 | iput(inode); |
| 6152 | mlog_errno(status); |
| 6153 | goto bail; |
| 6154 | } |
| 6155 | |
| 6156 | *tl_inode = inode; |
| 6157 | *tl_bh = bh; |
| 6158 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6159 | return status; |
| 6160 | } |
| 6161 | |
| 6162 | /* called during the 1st stage of node recovery. we stamp a clean |
| 6163 | * truncate log and pass back a copy for processing later. if the |
| 6164 | * truncate log does not require processing, a *tl_copy is set to |
| 6165 | * NULL. */ |
| 6166 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 6167 | int slot_num, |
| 6168 | struct ocfs2_dinode **tl_copy) |
| 6169 | { |
| 6170 | int status; |
| 6171 | struct inode *tl_inode = NULL; |
| 6172 | struct buffer_head *tl_bh = NULL; |
| 6173 | struct ocfs2_dinode *di; |
| 6174 | struct ocfs2_truncate_log *tl; |
| 6175 | |
| 6176 | *tl_copy = NULL; |
| 6177 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6178 | trace_ocfs2_begin_truncate_log_recovery(slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6179 | |
| 6180 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 6181 | if (status < 0) { |
| 6182 | mlog_errno(status); |
| 6183 | goto bail; |
| 6184 | } |
| 6185 | |
| 6186 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6187 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 6188 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
| 6189 | * validated by the underlying call to ocfs2_read_inode_block(), |
| 6190 | * so any corruption is a code bug */ |
| 6191 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
| 6192 | |
| 6193 | tl = &di->id2.i_dealloc; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6194 | if (le16_to_cpu(tl->tl_used)) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6195 | trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6196 | |
| 6197 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 6198 | if (!(*tl_copy)) { |
| 6199 | status = -ENOMEM; |
| 6200 | mlog_errno(status); |
| 6201 | goto bail; |
| 6202 | } |
| 6203 | |
| 6204 | /* Assuming the write-out below goes well, this copy |
| 6205 | * will be passed back to recovery for processing. */ |
| 6206 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 6207 | |
| 6208 | /* All we need to do to clear the truncate log is set |
| 6209 | * tl_used. */ |
| 6210 | tl->tl_used = 0; |
| 6211 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6212 | 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] | 6213 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6214 | if (status < 0) { |
| 6215 | mlog_errno(status); |
| 6216 | goto bail; |
| 6217 | } |
| 6218 | } |
| 6219 | |
| 6220 | bail: |
Joseph Qi | 72865d9 | 2016-01-14 15:17:27 -0800 | [diff] [blame] | 6221 | iput(tl_inode); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 6222 | brelse(tl_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6223 | |
Joseph Qi | 4635929 | 2015-09-04 15:44:54 -0700 | [diff] [blame] | 6224 | if (status < 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6225 | kfree(*tl_copy); |
| 6226 | *tl_copy = NULL; |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 6227 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6228 | } |
| 6229 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6230 | return status; |
| 6231 | } |
| 6232 | |
| 6233 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 6234 | struct ocfs2_dinode *tl_copy) |
| 6235 | { |
| 6236 | int status = 0; |
| 6237 | int i; |
| 6238 | unsigned int clusters, num_recs, start_cluster; |
| 6239 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 6240 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6241 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6242 | struct ocfs2_truncate_log *tl; |
| 6243 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6244 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 6245 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 6246 | return -EINVAL; |
| 6247 | } |
| 6248 | |
| 6249 | tl = &tl_copy->id2.i_dealloc; |
| 6250 | num_recs = le16_to_cpu(tl->tl_used); |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6251 | trace_ocfs2_complete_truncate_log_recovery( |
| 6252 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno), |
| 6253 | num_recs); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6254 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6255 | inode_lock(tl_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6256 | for(i = 0; i < num_recs; i++) { |
| 6257 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6258 | status = __ocfs2_flush_truncate_log(osb); |
| 6259 | if (status < 0) { |
| 6260 | mlog_errno(status); |
| 6261 | goto bail_up; |
| 6262 | } |
| 6263 | } |
| 6264 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 6265 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6266 | if (IS_ERR(handle)) { |
| 6267 | status = PTR_ERR(handle); |
| 6268 | mlog_errno(status); |
| 6269 | goto bail_up; |
| 6270 | } |
| 6271 | |
| 6272 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 6273 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 6274 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 6275 | |
| 6276 | status = ocfs2_truncate_log_append(osb, handle, |
| 6277 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 6278 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6279 | if (status < 0) { |
| 6280 | mlog_errno(status); |
| 6281 | goto bail_up; |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | bail_up: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6286 | inode_unlock(tl_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6287 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6288 | return status; |
| 6289 | } |
| 6290 | |
| 6291 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 6292 | { |
| 6293 | int status; |
| 6294 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6295 | |
Xue jiufei | a9e9aca | 2014-06-04 16:06:13 -0700 | [diff] [blame] | 6296 | atomic_set(&osb->osb_tl_disable, 1); |
| 6297 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6298 | if (tl_inode) { |
| 6299 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
jiangyiwen | 35ddf78 | 2016-03-25 14:21:32 -0700 | [diff] [blame] | 6300 | flush_workqueue(osb->ocfs2_wq); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6301 | |
| 6302 | status = ocfs2_flush_truncate_log(osb); |
| 6303 | if (status < 0) |
| 6304 | mlog_errno(status); |
| 6305 | |
| 6306 | brelse(osb->osb_tl_bh); |
| 6307 | iput(osb->osb_tl_inode); |
| 6308 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 6312 | { |
| 6313 | int status; |
| 6314 | struct inode *tl_inode = NULL; |
| 6315 | struct buffer_head *tl_bh = NULL; |
| 6316 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6317 | status = ocfs2_get_truncate_log_info(osb, |
| 6318 | osb->slot_num, |
| 6319 | &tl_inode, |
| 6320 | &tl_bh); |
| 6321 | if (status < 0) |
| 6322 | mlog_errno(status); |
| 6323 | |
| 6324 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 6325 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 6326 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 6327 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 6328 | ocfs2_truncate_log_worker); |
Xue jiufei | a9e9aca | 2014-06-04 16:06:13 -0700 | [diff] [blame] | 6329 | atomic_set(&osb->osb_tl_disable, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6330 | osb->osb_tl_bh = tl_bh; |
| 6331 | osb->osb_tl_inode = tl_inode; |
| 6332 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6333 | return status; |
| 6334 | } |
| 6335 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6336 | /* |
| 6337 | * Delayed de-allocation of suballocator blocks. |
| 6338 | * |
| 6339 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 6340 | * |
| 6341 | * The locking for this can get extremely complicated, especially when |
| 6342 | * the suballocator inodes to delete from aren't known until deep |
| 6343 | * within an unrelated codepath. |
| 6344 | * |
| 6345 | * ocfs2_extent_block structures are a good example of this - an inode |
| 6346 | * btree could have been grown by any number of nodes each allocating |
| 6347 | * out of their own suballoc inode. |
| 6348 | * |
| 6349 | * These structures allow the delay of block de-allocation until a |
| 6350 | * later time, when locking of multiple cluster inodes won't cause |
| 6351 | * deadlock. |
| 6352 | */ |
| 6353 | |
| 6354 | /* |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6355 | * Describe a single bit freed from a suballocator. For the block |
| 6356 | * suballocators, it represents one block. For the global cluster |
| 6357 | * allocator, it represents some clusters and free_bit indicates |
| 6358 | * clusters number. |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6359 | */ |
| 6360 | struct ocfs2_cached_block_free { |
| 6361 | struct ocfs2_cached_block_free *free_next; |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6362 | u64 free_bg; |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6363 | u64 free_blk; |
| 6364 | unsigned int free_bit; |
| 6365 | }; |
| 6366 | |
| 6367 | struct ocfs2_per_slot_free_list { |
| 6368 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 6369 | int f_inode_type; |
| 6370 | int f_slot; |
| 6371 | struct ocfs2_cached_block_free *f_first; |
| 6372 | }; |
| 6373 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6374 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
| 6375 | int sysfile_type, |
| 6376 | int slot, |
| 6377 | struct ocfs2_cached_block_free *head) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6378 | { |
| 6379 | int ret; |
| 6380 | u64 bg_blkno; |
| 6381 | handle_t *handle; |
| 6382 | struct inode *inode; |
| 6383 | struct buffer_head *di_bh = NULL; |
| 6384 | struct ocfs2_cached_block_free *tmp; |
| 6385 | |
| 6386 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 6387 | if (!inode) { |
| 6388 | ret = -EINVAL; |
| 6389 | mlog_errno(ret); |
| 6390 | goto out; |
| 6391 | } |
| 6392 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6393 | inode_lock(inode); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6394 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6395 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6396 | if (ret) { |
| 6397 | mlog_errno(ret); |
| 6398 | goto out_mutex; |
| 6399 | } |
| 6400 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6401 | while (head) { |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6402 | if (head->free_bg) |
| 6403 | bg_blkno = head->free_bg; |
| 6404 | else |
| 6405 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 6406 | head->free_bit); |
Junxiao Bi | d5bf141 | 2016-09-19 14:44:24 -0700 | [diff] [blame] | 6407 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 6408 | if (IS_ERR(handle)) { |
| 6409 | ret = PTR_ERR(handle); |
| 6410 | mlog_errno(ret); |
| 6411 | goto out_unlock; |
| 6412 | } |
| 6413 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6414 | trace_ocfs2_free_cached_blocks( |
| 6415 | (unsigned long long)head->free_blk, head->free_bit); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6416 | |
| 6417 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 6418 | head->free_bit, bg_blkno, 1); |
Junxiao Bi | d5bf141 | 2016-09-19 14:44:24 -0700 | [diff] [blame] | 6419 | if (ret) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6420 | mlog_errno(ret); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6421 | |
Junxiao Bi | d5bf141 | 2016-09-19 14:44:24 -0700 | [diff] [blame] | 6422 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6423 | |
| 6424 | tmp = head; |
| 6425 | head = head->free_next; |
| 6426 | kfree(tmp); |
| 6427 | } |
| 6428 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6429 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 6430 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6431 | brelse(di_bh); |
| 6432 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6433 | inode_unlock(inode); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6434 | iput(inode); |
| 6435 | out: |
| 6436 | while(head) { |
| 6437 | /* Premature exit may have left some dangling items. */ |
| 6438 | tmp = head; |
| 6439 | head = head->free_next; |
| 6440 | kfree(tmp); |
| 6441 | } |
| 6442 | |
| 6443 | return ret; |
| 6444 | } |
| 6445 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6446 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6447 | u64 blkno, unsigned int bit) |
| 6448 | { |
| 6449 | int ret = 0; |
| 6450 | struct ocfs2_cached_block_free *item; |
| 6451 | |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6452 | item = kzalloc(sizeof(*item), GFP_NOFS); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6453 | if (item == NULL) { |
| 6454 | ret = -ENOMEM; |
| 6455 | mlog_errno(ret); |
| 6456 | return ret; |
| 6457 | } |
| 6458 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6459 | trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6460 | |
| 6461 | item->free_blk = blkno; |
| 6462 | item->free_bit = bit; |
| 6463 | item->free_next = ctxt->c_global_allocator; |
| 6464 | |
| 6465 | ctxt->c_global_allocator = item; |
| 6466 | return ret; |
| 6467 | } |
| 6468 | |
| 6469 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, |
| 6470 | struct ocfs2_cached_block_free *head) |
| 6471 | { |
| 6472 | struct ocfs2_cached_block_free *tmp; |
| 6473 | struct inode *tl_inode = osb->osb_tl_inode; |
| 6474 | handle_t *handle; |
| 6475 | int ret = 0; |
| 6476 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6477 | inode_lock(tl_inode); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6478 | |
| 6479 | while (head) { |
| 6480 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 6481 | ret = __ocfs2_flush_truncate_log(osb); |
| 6482 | if (ret < 0) { |
| 6483 | mlog_errno(ret); |
| 6484 | break; |
| 6485 | } |
| 6486 | } |
| 6487 | |
| 6488 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
| 6489 | if (IS_ERR(handle)) { |
| 6490 | ret = PTR_ERR(handle); |
| 6491 | mlog_errno(ret); |
| 6492 | break; |
| 6493 | } |
| 6494 | |
| 6495 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, |
| 6496 | head->free_bit); |
| 6497 | |
| 6498 | ocfs2_commit_trans(osb, handle); |
| 6499 | tmp = head; |
| 6500 | head = head->free_next; |
| 6501 | kfree(tmp); |
| 6502 | |
| 6503 | if (ret < 0) { |
| 6504 | mlog_errno(ret); |
| 6505 | break; |
| 6506 | } |
| 6507 | } |
| 6508 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 6509 | inode_unlock(tl_inode); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6510 | |
| 6511 | while (head) { |
| 6512 | /* Premature exit may have left some dangling items. */ |
| 6513 | tmp = head; |
| 6514 | head = head->free_next; |
| 6515 | kfree(tmp); |
| 6516 | } |
| 6517 | |
| 6518 | return ret; |
| 6519 | } |
| 6520 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6521 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 6522 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6523 | { |
| 6524 | int ret = 0, ret2; |
| 6525 | struct ocfs2_per_slot_free_list *fl; |
| 6526 | |
| 6527 | if (!ctxt) |
| 6528 | return 0; |
| 6529 | |
| 6530 | while (ctxt->c_first_suballocator) { |
| 6531 | fl = ctxt->c_first_suballocator; |
| 6532 | |
| 6533 | if (fl->f_first) { |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6534 | trace_ocfs2_run_deallocs(fl->f_inode_type, |
| 6535 | fl->f_slot); |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6536 | ret2 = ocfs2_free_cached_blocks(osb, |
| 6537 | fl->f_inode_type, |
| 6538 | fl->f_slot, |
| 6539 | fl->f_first); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6540 | if (ret2) |
| 6541 | mlog_errno(ret2); |
| 6542 | if (!ret) |
| 6543 | ret = ret2; |
| 6544 | } |
| 6545 | |
| 6546 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 6547 | kfree(fl); |
| 6548 | } |
| 6549 | |
Tao Ma | 2891d29 | 2008-11-12 08:26:58 +0800 | [diff] [blame] | 6550 | if (ctxt->c_global_allocator) { |
| 6551 | ret2 = ocfs2_free_cached_clusters(osb, |
| 6552 | ctxt->c_global_allocator); |
| 6553 | if (ret2) |
| 6554 | mlog_errno(ret2); |
| 6555 | if (!ret) |
| 6556 | ret = ret2; |
| 6557 | |
| 6558 | ctxt->c_global_allocator = NULL; |
| 6559 | } |
| 6560 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6561 | return ret; |
| 6562 | } |
| 6563 | |
| 6564 | static struct ocfs2_per_slot_free_list * |
| 6565 | ocfs2_find_per_slot_free_list(int type, |
| 6566 | int slot, |
| 6567 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 6568 | { |
| 6569 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 6570 | |
| 6571 | while (fl) { |
| 6572 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 6573 | return fl; |
| 6574 | |
| 6575 | fl = fl->f_next_suballocator; |
| 6576 | } |
| 6577 | |
| 6578 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 6579 | if (fl) { |
| 6580 | fl->f_inode_type = type; |
| 6581 | fl->f_slot = slot; |
| 6582 | fl->f_first = NULL; |
| 6583 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 6584 | |
| 6585 | ctxt->c_first_suballocator = fl; |
| 6586 | } |
| 6587 | return fl; |
| 6588 | } |
| 6589 | |
Tao Ma | 1823cb0 | 2009-08-18 11:24:49 +0800 | [diff] [blame] | 6590 | int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6591 | int type, int slot, u64 suballoc, |
| 6592 | u64 blkno, unsigned int bit) |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6593 | { |
| 6594 | int ret; |
| 6595 | struct ocfs2_per_slot_free_list *fl; |
| 6596 | struct ocfs2_cached_block_free *item; |
| 6597 | |
| 6598 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 6599 | if (fl == NULL) { |
| 6600 | ret = -ENOMEM; |
| 6601 | mlog_errno(ret); |
| 6602 | goto out; |
| 6603 | } |
| 6604 | |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6605 | item = kzalloc(sizeof(*item), GFP_NOFS); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6606 | if (item == NULL) { |
| 6607 | ret = -ENOMEM; |
| 6608 | mlog_errno(ret); |
| 6609 | goto out; |
| 6610 | } |
| 6611 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 6612 | trace_ocfs2_cache_block_dealloc(type, slot, |
| 6613 | (unsigned long long)suballoc, |
| 6614 | (unsigned long long)blkno, bit); |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6615 | |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6616 | item->free_bg = suballoc; |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 6617 | item->free_blk = blkno; |
| 6618 | item->free_bit = bit; |
| 6619 | item->free_next = fl->f_first; |
| 6620 | |
| 6621 | fl->f_first = item; |
| 6622 | |
| 6623 | ret = 0; |
| 6624 | out: |
| 6625 | return ret; |
| 6626 | } |
| 6627 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6628 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 6629 | struct ocfs2_extent_block *eb) |
| 6630 | { |
| 6631 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 6632 | le16_to_cpu(eb->h_suballoc_slot), |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 6633 | le64_to_cpu(eb->h_suballoc_loc), |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6634 | le64_to_cpu(eb->h_blkno), |
| 6635 | le16_to_cpu(eb->h_suballoc_bit)); |
| 6636 | } |
| 6637 | |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6638 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6639 | { |
| 6640 | set_buffer_uptodate(bh); |
| 6641 | mark_buffer_dirty(bh); |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 6645 | void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
| 6646 | unsigned int from, unsigned int to, |
| 6647 | struct page *page, int zero, u64 *phys) |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6648 | { |
| 6649 | int ret, partial = 0; |
| 6650 | |
| 6651 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); |
| 6652 | if (ret) |
| 6653 | mlog_errno(ret); |
| 6654 | |
| 6655 | if (zero) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 6656 | zero_user_segment(page, from, to); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6657 | |
| 6658 | /* |
| 6659 | * Need to set the buffers we zero'd into uptodate |
| 6660 | * here if they aren't - ocfs2_map_page_blocks() |
| 6661 | * might've skipped some |
| 6662 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 6663 | ret = walk_page_buffers(handle, page_buffers(page), |
| 6664 | from, to, &partial, |
| 6665 | ocfs2_zero_func); |
| 6666 | if (ret < 0) |
| 6667 | mlog_errno(ret); |
| 6668 | else if (ocfs2_should_order_data(inode)) { |
| 6669 | ret = ocfs2_jbd2_file_inode(handle, inode); |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6670 | if (ret < 0) |
| 6671 | mlog_errno(ret); |
| 6672 | } |
| 6673 | |
| 6674 | if (!partial) |
| 6675 | SetPageUptodate(page); |
| 6676 | |
| 6677 | flush_dcache_page(page); |
| 6678 | } |
| 6679 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6680 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 6681 | loff_t end, struct page **pages, |
| 6682 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6683 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6684 | int i; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6685 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6686 | unsigned int from, to = PAGE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6687 | struct super_block *sb = inode->i_sb; |
| 6688 | |
| 6689 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 6690 | |
| 6691 | if (numpages == 0) |
| 6692 | goto out; |
| 6693 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6694 | to = PAGE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6695 | for(i = 0; i < numpages; i++) { |
| 6696 | page = pages[i]; |
| 6697 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6698 | from = start & (PAGE_SIZE - 1); |
| 6699 | if ((end >> PAGE_SHIFT) == page->index) |
| 6700 | to = end & (PAGE_SIZE - 1); |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6701 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6702 | BUG_ON(from > PAGE_SIZE); |
| 6703 | BUG_ON(to > PAGE_SIZE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6704 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6705 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
| 6706 | &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6707 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6708 | start = (page->index + 1) << PAGE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6709 | } |
| 6710 | out: |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6711 | if (pages) |
| 6712 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6713 | } |
| 6714 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 6715 | int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end, |
| 6716 | struct page **pages, int *num) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6717 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6718 | int numpages, ret = 0; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6719 | struct address_space *mapping = inode->i_mapping; |
| 6720 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6721 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6722 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6723 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6724 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6725 | numpages = 0; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6726 | last_page_bytes = PAGE_ALIGN(end); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6727 | index = start >> PAGE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6728 | do { |
Jan Kara | 9b4c0ff | 2010-08-24 14:28:03 +0200 | [diff] [blame] | 6729 | pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6730 | if (!pages[numpages]) { |
| 6731 | ret = -ENOMEM; |
| 6732 | mlog_errno(ret); |
| 6733 | goto out; |
| 6734 | } |
| 6735 | |
| 6736 | numpages++; |
| 6737 | index++; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6738 | } while (index < (last_page_bytes >> PAGE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6739 | |
| 6740 | out: |
| 6741 | if (ret != 0) { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6742 | if (pages) |
| 6743 | ocfs2_unlock_and_free_pages(pages, numpages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6744 | numpages = 0; |
| 6745 | } |
| 6746 | |
| 6747 | *num = numpages; |
| 6748 | |
| 6749 | return ret; |
| 6750 | } |
| 6751 | |
Tao Ma | 6f70fa5 | 2009-08-25 08:05:12 +0800 | [diff] [blame] | 6752 | static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, |
| 6753 | struct page **pages, int *num) |
| 6754 | { |
| 6755 | struct super_block *sb = inode->i_sb; |
| 6756 | |
| 6757 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 6758 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 6759 | |
| 6760 | return ocfs2_grab_pages(inode, start, end, pages, num); |
| 6761 | } |
| 6762 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6763 | /* |
| 6764 | * Zero the area past i_size but still within an allocated |
| 6765 | * cluster. This avoids exposing nonzero data on subsequent file |
| 6766 | * extends. |
| 6767 | * |
| 6768 | * We need to call this before i_size is updated on the inode because |
| 6769 | * otherwise block_write_full_page() will skip writeout of pages past |
| 6770 | * i_size. The new_i_size parameter is passed for this reason. |
| 6771 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6772 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 6773 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6774 | { |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6775 | int ret = 0, numpages; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6776 | struct page **pages = NULL; |
| 6777 | u64 phys; |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6778 | unsigned int ext_flags; |
| 6779 | struct super_block *sb = inode->i_sb; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6780 | |
| 6781 | /* |
| 6782 | * File systems which don't support sparse files zero on every |
| 6783 | * extend. |
| 6784 | */ |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6785 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6786 | return 0; |
| 6787 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6788 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6789 | sizeof(struct page *), GFP_NOFS); |
| 6790 | if (pages == NULL) { |
| 6791 | ret = -ENOMEM; |
| 6792 | mlog_errno(ret); |
| 6793 | goto out; |
| 6794 | } |
| 6795 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6796 | if (range_start == range_end) |
| 6797 | goto out; |
| 6798 | |
| 6799 | ret = ocfs2_extent_map_get_blocks(inode, |
| 6800 | range_start >> sb->s_blocksize_bits, |
| 6801 | &phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6802 | if (ret) { |
| 6803 | mlog_errno(ret); |
| 6804 | goto out; |
| 6805 | } |
| 6806 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6807 | /* |
| 6808 | * Tail is a hole, or is marked unwritten. In either case, we |
| 6809 | * can count on read and write to return/push zero's. |
| 6810 | */ |
| 6811 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6812 | goto out; |
| 6813 | |
Mark Fasheh | 1d410a6 | 2007-09-07 14:20:45 -0700 | [diff] [blame] | 6814 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 6815 | &numpages); |
| 6816 | if (ret) { |
| 6817 | mlog_errno(ret); |
| 6818 | goto out; |
| 6819 | } |
| 6820 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 6821 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 6822 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6823 | |
| 6824 | /* |
| 6825 | * Initiate writeout of the pages we zero'd here. We don't |
| 6826 | * wait on them - the truncate_inode_pages() call later will |
| 6827 | * do that for us. |
| 6828 | */ |
Christoph Hellwig | 2cfd30a | 2009-09-23 15:04:02 +0200 | [diff] [blame] | 6829 | ret = filemap_fdatawrite_range(inode->i_mapping, range_start, |
| 6830 | range_end - 1); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6831 | if (ret) |
| 6832 | mlog_errno(ret); |
| 6833 | |
| 6834 | out: |
Tim Gardner | d787ab0 | 2013-02-21 16:42:44 -0800 | [diff] [blame] | 6835 | kfree(pages); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 6836 | |
| 6837 | return ret; |
| 6838 | } |
| 6839 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6840 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
| 6841 | struct ocfs2_dinode *di) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6842 | { |
| 6843 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6844 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6845 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6846 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
| 6847 | memset(&di->id2, 0, blocksize - |
| 6848 | offsetof(struct ocfs2_dinode, id2) - |
| 6849 | xattrsize); |
| 6850 | else |
| 6851 | memset(&di->id2, 0, blocksize - |
| 6852 | offsetof(struct ocfs2_dinode, id2)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6853 | } |
| 6854 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 6855 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
| 6856 | struct ocfs2_dinode *di) |
| 6857 | { |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6858 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 6859 | di->id2.i_list.l_tree_depth = 0; |
| 6860 | di->id2.i_list.l_next_free_rec = 0; |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6861 | di->id2.i_list.l_count = cpu_to_le16( |
| 6862 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 6863 | } |
| 6864 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6865 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
| 6866 | { |
| 6867 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 6868 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 6869 | |
| 6870 | spin_lock(&oi->ip_lock); |
| 6871 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; |
| 6872 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 6873 | spin_unlock(&oi->ip_lock); |
| 6874 | |
| 6875 | /* |
| 6876 | * We clear the entire i_data structure here so that all |
| 6877 | * fields can be properly initialized. |
| 6878 | */ |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6879 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6880 | |
Tiger Yang | fdd7770 | 2008-08-18 17:08:55 +0800 | [diff] [blame] | 6881 | idata->id_count = cpu_to_le16( |
| 6882 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, |
| 6886 | struct buffer_head *di_bh) |
| 6887 | { |
| 6888 | int ret, i, has_data, num_pages = 0; |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 6889 | int need_free = 0; |
| 6890 | u32 bit_off, num; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6891 | handle_t *handle; |
| 6892 | u64 uninitialized_var(block); |
| 6893 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 6894 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 6895 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6896 | struct ocfs2_alloc_context *data_ac = NULL; |
| 6897 | struct page **pages = NULL; |
| 6898 | loff_t end = osb->s_clustersize; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 6899 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6900 | int did_quota = 0; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6901 | |
| 6902 | has_data = i_size_read(inode) ? 1 : 0; |
| 6903 | |
| 6904 | if (has_data) { |
| 6905 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), |
| 6906 | sizeof(struct page *), GFP_NOFS); |
| 6907 | if (pages == NULL) { |
| 6908 | ret = -ENOMEM; |
| 6909 | mlog_errno(ret); |
Markus Elfring | 3cc79b7 | 2015-04-14 15:42:45 -0700 | [diff] [blame] | 6910 | return ret; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 6914 | if (ret) { |
| 6915 | mlog_errno(ret); |
Markus Elfring | 3cc79b7 | 2015-04-14 15:42:45 -0700 | [diff] [blame] | 6916 | goto free_pages; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6920 | handle = ocfs2_start_trans(osb, |
| 6921 | ocfs2_inline_to_extents_credits(osb->sb)); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6922 | if (IS_ERR(handle)) { |
| 6923 | ret = PTR_ERR(handle); |
| 6924 | mlog_errno(ret); |
alex chen | 15eba0f | 2015-02-10 14:09:02 -0800 | [diff] [blame] | 6925 | goto out; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6926 | } |
| 6927 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 6928 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 6929 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6930 | if (ret) { |
| 6931 | mlog_errno(ret); |
| 6932 | goto out_commit; |
| 6933 | } |
| 6934 | |
| 6935 | if (has_data) { |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6936 | unsigned int page_end; |
| 6937 | u64 phys; |
| 6938 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 6939 | ret = dquot_alloc_space_nodirty(inode, |
| 6940 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 6941 | if (ret) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6942 | goto out_commit; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 6943 | did_quota = 1; |
| 6944 | |
Mark Fasheh | 4fe370a | 2009-12-07 13:15:40 -0800 | [diff] [blame] | 6945 | data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv; |
| 6946 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 6947 | ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6948 | &num); |
| 6949 | if (ret) { |
| 6950 | mlog_errno(ret); |
| 6951 | goto out_commit; |
| 6952 | } |
| 6953 | |
| 6954 | /* |
| 6955 | * Save two copies, one for insert, and one that can |
| 6956 | * be changed by ocfs2_map_and_dirty_page() below. |
| 6957 | */ |
| 6958 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 6959 | |
| 6960 | /* |
| 6961 | * Non sparse file systems zero on extend, so no need |
| 6962 | * to do that now. |
| 6963 | */ |
| 6964 | if (!ocfs2_sparse_alloc(osb) && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6965 | PAGE_SIZE < osb->s_clustersize) |
| 6966 | end = PAGE_SIZE; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6967 | |
| 6968 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); |
| 6969 | if (ret) { |
| 6970 | mlog_errno(ret); |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 6971 | need_free = 1; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6972 | goto out_commit; |
| 6973 | } |
| 6974 | |
| 6975 | /* |
| 6976 | * This should populate the 1st page for us and mark |
| 6977 | * it up to date. |
| 6978 | */ |
| 6979 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); |
| 6980 | if (ret) { |
| 6981 | mlog_errno(ret); |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 6982 | need_free = 1; |
alex chen | 15eba0f | 2015-02-10 14:09:02 -0800 | [diff] [blame] | 6983 | goto out_unlock; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6984 | } |
| 6985 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6986 | page_end = PAGE_SIZE; |
| 6987 | if (PAGE_SIZE > osb->s_clustersize) |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 6988 | page_end = osb->s_clustersize; |
| 6989 | |
| 6990 | for (i = 0; i < num_pages; i++) |
| 6991 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, |
| 6992 | pages[i], i > 0, &phys); |
| 6993 | } |
| 6994 | |
| 6995 | spin_lock(&oi->ip_lock); |
| 6996 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 6997 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 6998 | spin_unlock(&oi->ip_lock); |
| 6999 | |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 7000 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 7001 | ocfs2_dinode_new_extent_list(inode, di); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7002 | |
| 7003 | ocfs2_journal_dirty(handle, di_bh); |
| 7004 | |
| 7005 | if (has_data) { |
| 7006 | /* |
| 7007 | * An error at this point should be extremely rare. If |
| 7008 | * this proves to be false, we could always re-build |
| 7009 | * the in-inode data from our pages. |
| 7010 | */ |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 7011 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 7012 | ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7013 | if (ret) { |
| 7014 | mlog_errno(ret); |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 7015 | need_free = 1; |
alex chen | 15eba0f | 2015-02-10 14:09:02 -0800 | [diff] [blame] | 7016 | goto out_unlock; |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7017 | } |
| 7018 | |
| 7019 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 7020 | } |
| 7021 | |
alex chen | 15eba0f | 2015-02-10 14:09:02 -0800 | [diff] [blame] | 7022 | out_unlock: |
| 7023 | if (pages) |
| 7024 | ocfs2_unlock_and_free_pages(pages, num_pages); |
| 7025 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7026 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7027 | if (ret < 0 && did_quota) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 7028 | dquot_free_space_nodirty(inode, |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 7029 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 7030 | |
Zongxun Wang | fb951eb | 2014-02-06 12:04:20 -0800 | [diff] [blame] | 7031 | if (need_free) { |
| 7032 | if (data_ac->ac_which == OCFS2_AC_USE_LOCAL) |
| 7033 | ocfs2_free_local_alloc_bits(osb, handle, data_ac, |
| 7034 | bit_off, num); |
| 7035 | else |
| 7036 | ocfs2_free_clusters(handle, |
| 7037 | data_ac->ac_inode, |
| 7038 | data_ac->ac_bh, |
| 7039 | ocfs2_clusters_to_blocks(osb->sb, bit_off), |
| 7040 | num); |
| 7041 | } |
| 7042 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7043 | ocfs2_commit_trans(osb, handle); |
| 7044 | |
alex chen | 15eba0f | 2015-02-10 14:09:02 -0800 | [diff] [blame] | 7045 | out: |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7046 | if (data_ac) |
| 7047 | ocfs2_free_alloc_context(data_ac); |
Markus Elfring | 3cc79b7 | 2015-04-14 15:42:45 -0700 | [diff] [blame] | 7048 | free_pages: |
Markus Elfring | fd90d4df | 2015-04-14 15:42:42 -0700 | [diff] [blame] | 7049 | kfree(pages); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7050 | return ret; |
| 7051 | } |
| 7052 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7053 | /* |
| 7054 | * It is expected, that by the time you call this function, |
| 7055 | * inode->i_size and fe->i_size have been adjusted. |
| 7056 | * |
| 7057 | * WARNING: This will kfree the truncate context |
| 7058 | */ |
| 7059 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 7060 | struct inode *inode, |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7061 | struct buffer_head *di_bh) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7062 | { |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7063 | int status = 0, i, flags = 0; |
| 7064 | u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff; |
Tao Ma | bcbbb24 | 2009-08-18 11:29:12 +0800 | [diff] [blame] | 7065 | u64 blkno = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7066 | struct ocfs2_extent_list *el; |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7067 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7068 | struct ocfs2_path *path = NULL; |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7069 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7070 | struct ocfs2_extent_list *root_el = &(di->id2.i_list); |
| 7071 | u64 refcount_loc = le64_to_cpu(di->i_refcount_loc); |
| 7072 | struct ocfs2_extent_tree et; |
| 7073 | struct ocfs2_cached_dealloc_ctxt dealloc; |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 7074 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7075 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7076 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
| 7077 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 7078 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7079 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7080 | i_size_read(inode)); |
| 7081 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7082 | path = ocfs2_new_path(di_bh, &di->id2.i_list, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7083 | ocfs2_journal_access_di); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7084 | if (!path) { |
| 7085 | status = -ENOMEM; |
| 7086 | mlog_errno(status); |
| 7087 | goto bail; |
| 7088 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 7089 | |
| 7090 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 7091 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7092 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7093 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7094 | * Check that we still have allocation to delete. |
| 7095 | */ |
| 7096 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 7097 | status = 0; |
| 7098 | goto bail; |
| 7099 | } |
| 7100 | |
| 7101 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7102 | * Truncate always works against the rightmost tree branch. |
| 7103 | */ |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 7104 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7105 | if (status) { |
| 7106 | mlog_errno(status); |
| 7107 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7108 | } |
| 7109 | |
Tao Ma | a09d09b | 2011-02-22 08:21:10 +0800 | [diff] [blame] | 7110 | trace_ocfs2_commit_truncate( |
| 7111 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7112 | new_highest_cpos, |
| 7113 | OCFS2_I(inode)->ip_clusters, |
| 7114 | path->p_tree_depth); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7115 | |
| 7116 | /* |
| 7117 | * By now, el will point to the extent list on the bottom most |
| 7118 | * portion of this tree. Only the tail record is considered in |
| 7119 | * each pass. |
| 7120 | * |
| 7121 | * We handle the following cases, in order: |
| 7122 | * - empty extent: delete the remaining branch |
| 7123 | * - remove the entire record |
| 7124 | * - remove a partial record |
| 7125 | * - no record needs to be removed (truncate has completed) |
| 7126 | */ |
| 7127 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7128 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 7129 | ocfs2_error(inode->i_sb, |
| 7130 | "Inode %llu has empty extent block at %llu\n", |
| 7131 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7132 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 7133 | status = -EROFS; |
| 7134 | goto bail; |
| 7135 | } |
| 7136 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7137 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7138 | rec = &el->l_recs[i]; |
| 7139 | flags = rec->e_flags; |
| 7140 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 7141 | |
| 7142 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 7143 | /* |
| 7144 | * Lower levels depend on this never happening, but it's best |
| 7145 | * to check it up here before changing the tree. |
| 7146 | */ |
| 7147 | if (root_el->l_tree_depth && rec->e_int_clusters == 0) { |
Xue jiufei | d0c97d5 | 2015-09-04 15:44:48 -0700 | [diff] [blame] | 7148 | mlog(ML_ERROR, "Inode %lu has an empty " |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7149 | "extent record, depth %u\n", inode->i_ino, |
| 7150 | le16_to_cpu(root_el->l_tree_depth)); |
Xue jiufei | d0c97d5 | 2015-09-04 15:44:48 -0700 | [diff] [blame] | 7151 | status = ocfs2_remove_rightmost_empty_extent(osb, |
| 7152 | &et, path, &dealloc); |
| 7153 | if (status) { |
| 7154 | mlog_errno(status); |
| 7155 | goto bail; |
| 7156 | } |
| 7157 | |
| 7158 | ocfs2_reinit_path(path, 1); |
| 7159 | goto start; |
| 7160 | } else { |
| 7161 | trunc_cpos = le32_to_cpu(rec->e_cpos); |
| 7162 | trunc_len = 0; |
| 7163 | blkno = 0; |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7164 | } |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7165 | } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) { |
| 7166 | /* |
| 7167 | * Truncate entire record. |
| 7168 | */ |
| 7169 | trunc_cpos = le32_to_cpu(rec->e_cpos); |
| 7170 | trunc_len = ocfs2_rec_clusters(el, rec); |
| 7171 | blkno = le64_to_cpu(rec->e_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7172 | } else if (range > new_highest_cpos) { |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7173 | /* |
| 7174 | * Partial truncate. it also should be |
| 7175 | * the last truncate we're doing. |
| 7176 | */ |
| 7177 | trunc_cpos = new_highest_cpos; |
| 7178 | trunc_len = range - new_highest_cpos; |
| 7179 | coff = new_highest_cpos - le32_to_cpu(rec->e_cpos); |
| 7180 | blkno = le64_to_cpu(rec->e_blkno) + |
| 7181 | ocfs2_clusters_to_blocks(inode->i_sb, coff); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7182 | } else { |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7183 | /* |
| 7184 | * Truncate completed, leave happily. |
| 7185 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7186 | status = 0; |
| 7187 | goto bail; |
| 7188 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7189 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7190 | phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7191 | |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 7192 | if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) { |
| 7193 | status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1, |
| 7194 | &ref_tree, NULL); |
| 7195 | if (status) { |
| 7196 | mlog_errno(status); |
| 7197 | goto bail; |
| 7198 | } |
| 7199 | } |
| 7200 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7201 | status = ocfs2_remove_btree_range(inode, &et, trunc_cpos, |
| 7202 | phys_cpos, trunc_len, flags, &dealloc, |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 7203 | refcount_loc, true); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7204 | if (status < 0) { |
| 7205 | mlog_errno(status); |
| 7206 | goto bail; |
| 7207 | } |
| 7208 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7209 | ocfs2_reinit_path(path, 1); |
| 7210 | |
| 7211 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7212 | * The check above will catch the case where we've truncated |
| 7213 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7214 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 7215 | goto start; |
| 7216 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7217 | bail: |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 7218 | if (ref_tree) |
| 7219 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7220 | |
| 7221 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 7222 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 7223 | ocfs2_run_deallocs(osb, &dealloc); |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 7224 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 7225 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7226 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7227 | return status; |
| 7228 | } |
| 7229 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 7230 | /* |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7231 | * 'start' is inclusive, 'end' is not. |
| 7232 | */ |
| 7233 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, |
| 7234 | unsigned int start, unsigned int end, int trunc) |
| 7235 | { |
| 7236 | int ret; |
| 7237 | unsigned int numbytes; |
| 7238 | handle_t *handle; |
| 7239 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 7240 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 7241 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 7242 | |
| 7243 | if (end > i_size_read(inode)) |
| 7244 | end = i_size_read(inode); |
| 7245 | |
Younger Liu | d62e74b | 2014-02-10 14:25:51 -0800 | [diff] [blame] | 7246 | BUG_ON(start > end); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7247 | |
| 7248 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || |
| 7249 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || |
| 7250 | !ocfs2_supports_inline_data(osb)) { |
| 7251 | ocfs2_error(inode->i_sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 7252 | "Inline data flags for inode %llu don't agree! Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7253 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 7254 | le16_to_cpu(di->i_dyn_features), |
| 7255 | OCFS2_I(inode)->ip_dyn_features, |
| 7256 | osb->s_feature_incompat); |
| 7257 | ret = -EROFS; |
| 7258 | goto out; |
| 7259 | } |
| 7260 | |
| 7261 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 7262 | if (IS_ERR(handle)) { |
| 7263 | ret = PTR_ERR(handle); |
| 7264 | mlog_errno(ret); |
| 7265 | goto out; |
| 7266 | } |
| 7267 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 7268 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 7269 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7270 | if (ret) { |
| 7271 | mlog_errno(ret); |
| 7272 | goto out_commit; |
| 7273 | } |
| 7274 | |
| 7275 | numbytes = end - start; |
| 7276 | memset(idata->id_data + start, 0, numbytes); |
| 7277 | |
| 7278 | /* |
| 7279 | * No need to worry about the data page here - it's been |
| 7280 | * truncated already and inline data doesn't need it for |
| 7281 | * pushing zero's to disk, so we'll let readpage pick it up |
| 7282 | * later. |
| 7283 | */ |
| 7284 | if (trunc) { |
| 7285 | i_size_write(inode, start); |
| 7286 | di->i_size = cpu_to_le64(start); |
| 7287 | } |
| 7288 | |
| 7289 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 7290 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7291 | |
| 7292 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 7293 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 7294 | |
Darrick J. Wong | 6fdb702 | 2014-04-03 14:47:08 -0700 | [diff] [blame] | 7295 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 7296 | ocfs2_journal_dirty(handle, di_bh); |
| 7297 | |
| 7298 | out_commit: |
| 7299 | ocfs2_commit_trans(osb, handle); |
| 7300 | |
| 7301 | out: |
| 7302 | return ret; |
| 7303 | } |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7304 | |
| 7305 | static int ocfs2_trim_extent(struct super_block *sb, |
| 7306 | struct ocfs2_group_desc *gd, |
| 7307 | u32 start, u32 count) |
| 7308 | { |
| 7309 | u64 discard, bcount; |
| 7310 | |
| 7311 | bcount = ocfs2_clusters_to_blocks(sb, count); |
| 7312 | discard = le64_to_cpu(gd->bg_blkno) + |
| 7313 | ocfs2_clusters_to_blocks(sb, start); |
| 7314 | |
Tao Ma | 10fca35 | 2011-05-23 10:36:45 +0800 | [diff] [blame] | 7315 | trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount); |
| 7316 | |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7317 | return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0); |
| 7318 | } |
| 7319 | |
| 7320 | static int ocfs2_trim_group(struct super_block *sb, |
| 7321 | struct ocfs2_group_desc *gd, |
| 7322 | u32 start, u32 max, u32 minbits) |
| 7323 | { |
| 7324 | int ret = 0, count = 0, next; |
| 7325 | void *bitmap = gd->bg_bitmap; |
| 7326 | |
| 7327 | if (le16_to_cpu(gd->bg_free_bits_count) < minbits) |
| 7328 | return 0; |
| 7329 | |
Tao Ma | 10fca35 | 2011-05-23 10:36:45 +0800 | [diff] [blame] | 7330 | trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 7331 | start, max, minbits); |
| 7332 | |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7333 | while (start < max) { |
| 7334 | start = ocfs2_find_next_zero_bit(bitmap, max, start); |
| 7335 | if (start >= max) |
| 7336 | break; |
| 7337 | next = ocfs2_find_next_bit(bitmap, max, start); |
| 7338 | |
| 7339 | if ((next - start) >= minbits) { |
| 7340 | ret = ocfs2_trim_extent(sb, gd, |
| 7341 | start, next - start); |
| 7342 | if (ret < 0) { |
| 7343 | mlog_errno(ret); |
| 7344 | break; |
| 7345 | } |
| 7346 | count += next - start; |
| 7347 | } |
| 7348 | start = next + 1; |
| 7349 | |
| 7350 | if (fatal_signal_pending(current)) { |
| 7351 | count = -ERESTARTSYS; |
| 7352 | break; |
| 7353 | } |
| 7354 | |
| 7355 | if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits) |
| 7356 | break; |
| 7357 | } |
| 7358 | |
| 7359 | if (ret < 0) |
| 7360 | count = ret; |
| 7361 | |
| 7362 | return count; |
| 7363 | } |
| 7364 | |
| 7365 | int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range) |
| 7366 | { |
| 7367 | struct ocfs2_super *osb = OCFS2_SB(sb); |
| 7368 | u64 start, len, trimmed, first_group, last_group, group; |
| 7369 | int ret, cnt; |
| 7370 | u32 first_bit, last_bit, minlen; |
| 7371 | struct buffer_head *main_bm_bh = NULL; |
| 7372 | struct inode *main_bm_inode = NULL; |
| 7373 | struct buffer_head *gd_bh = NULL; |
| 7374 | struct ocfs2_dinode *main_bm; |
| 7375 | struct ocfs2_group_desc *gd = NULL; |
| 7376 | |
| 7377 | start = range->start >> osb->s_clustersize_bits; |
| 7378 | len = range->len >> osb->s_clustersize_bits; |
| 7379 | minlen = range->minlen >> osb->s_clustersize_bits; |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7380 | |
Jie Liu | aa89762 | 2014-01-21 15:48:35 -0800 | [diff] [blame] | 7381 | if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize) |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7382 | return -EINVAL; |
| 7383 | |
| 7384 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 7385 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 7386 | OCFS2_INVALID_SLOT); |
| 7387 | if (!main_bm_inode) { |
| 7388 | ret = -EIO; |
| 7389 | mlog_errno(ret); |
| 7390 | goto out; |
| 7391 | } |
| 7392 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 7393 | inode_lock(main_bm_inode); |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7394 | |
| 7395 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0); |
| 7396 | if (ret < 0) { |
| 7397 | mlog_errno(ret); |
| 7398 | goto out_mutex; |
| 7399 | } |
| 7400 | main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 7401 | |
| 7402 | if (start >= le32_to_cpu(main_bm->i_clusters)) { |
| 7403 | ret = -EINVAL; |
| 7404 | goto out_unlock; |
| 7405 | } |
| 7406 | |
Jie Liu | aa89762 | 2014-01-21 15:48:35 -0800 | [diff] [blame] | 7407 | len = range->len >> osb->s_clustersize_bits; |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7408 | if (start + len > le32_to_cpu(main_bm->i_clusters)) |
| 7409 | len = le32_to_cpu(main_bm->i_clusters) - start; |
| 7410 | |
Tao Ma | 10fca35 | 2011-05-23 10:36:45 +0800 | [diff] [blame] | 7411 | trace_ocfs2_trim_fs(start, len, minlen); |
| 7412 | |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7413 | /* Determine first and last group to examine based on start and len */ |
| 7414 | first_group = ocfs2_which_cluster_group(main_bm_inode, start); |
| 7415 | if (first_group == osb->first_cluster_group_blkno) |
| 7416 | first_bit = start; |
| 7417 | else |
| 7418 | first_bit = start - ocfs2_blocks_to_clusters(sb, first_group); |
| 7419 | last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1); |
| 7420 | last_bit = osb->bitmap_cpg; |
| 7421 | |
Jie Liu | aa89762 | 2014-01-21 15:48:35 -0800 | [diff] [blame] | 7422 | trimmed = 0; |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7423 | for (group = first_group; group <= last_group;) { |
| 7424 | if (first_bit + len >= osb->bitmap_cpg) |
| 7425 | last_bit = osb->bitmap_cpg; |
| 7426 | else |
| 7427 | last_bit = first_bit + len; |
| 7428 | |
| 7429 | ret = ocfs2_read_group_descriptor(main_bm_inode, |
| 7430 | main_bm, group, |
| 7431 | &gd_bh); |
| 7432 | if (ret < 0) { |
| 7433 | mlog_errno(ret); |
| 7434 | break; |
| 7435 | } |
| 7436 | |
| 7437 | gd = (struct ocfs2_group_desc *)gd_bh->b_data; |
| 7438 | cnt = ocfs2_trim_group(sb, gd, first_bit, last_bit, minlen); |
| 7439 | brelse(gd_bh); |
| 7440 | gd_bh = NULL; |
| 7441 | if (cnt < 0) { |
| 7442 | ret = cnt; |
| 7443 | mlog_errno(ret); |
| 7444 | break; |
| 7445 | } |
| 7446 | |
| 7447 | trimmed += cnt; |
| 7448 | len -= osb->bitmap_cpg - first_bit; |
| 7449 | first_bit = 0; |
| 7450 | if (group == osb->first_cluster_group_blkno) |
| 7451 | group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg); |
| 7452 | else |
| 7453 | group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg); |
| 7454 | } |
| 7455 | range->len = trimmed * sb->s_blocksize; |
| 7456 | out_unlock: |
| 7457 | ocfs2_inode_unlock(main_bm_inode, 0); |
| 7458 | brelse(main_bm_bh); |
| 7459 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 7460 | inode_unlock(main_bm_inode); |
Tao Ma | e80de36 | 2011-05-23 10:36:43 +0800 | [diff] [blame] | 7461 | iput(main_bm_inode); |
| 7462 | out: |
| 7463 | return ret; |
| 7464 | } |