1 // SPDX-License-Identifier: GPL-2.0-only !! 1 /* -*- mode: c; c-basic-offset: 8; -*- 2 /* !! 2 * vim: noexpandtab sw=8 ts=8 sts=0: >> 3 * 3 * refcounttree.c 4 * refcounttree.c 4 * 5 * 5 * Copyright (C) 2009 Oracle. All rights rese 6 * Copyright (C) 2009 Oracle. All rights reserved. >> 7 * >> 8 * This program is free software; you can redistribute it and/or >> 9 * modify it under the terms of the GNU General Public >> 10 * License version 2 as published by the Free Software Foundation. >> 11 * >> 12 * This program is distributed in the hope that it will be useful, >> 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of >> 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >> 15 * General Public License for more details. 6 */ 16 */ 7 17 8 #include <linux/sort.h> 18 #include <linux/sort.h> 9 #include <cluster/masklog.h> 19 #include <cluster/masklog.h> 10 #include "ocfs2.h" 20 #include "ocfs2.h" 11 #include "inode.h" 21 #include "inode.h" 12 #include "alloc.h" 22 #include "alloc.h" 13 #include "suballoc.h" 23 #include "suballoc.h" 14 #include "journal.h" 24 #include "journal.h" 15 #include "uptodate.h" 25 #include "uptodate.h" 16 #include "super.h" 26 #include "super.h" 17 #include "buffer_head_io.h" 27 #include "buffer_head_io.h" 18 #include "blockcheck.h" 28 #include "blockcheck.h" 19 #include "refcounttree.h" 29 #include "refcounttree.h" 20 #include "sysfile.h" 30 #include "sysfile.h" 21 #include "dlmglue.h" 31 #include "dlmglue.h" 22 #include "extent_map.h" 32 #include "extent_map.h" 23 #include "aops.h" 33 #include "aops.h" 24 #include "xattr.h" 34 #include "xattr.h" 25 #include "namei.h" 35 #include "namei.h" 26 #include "ocfs2_trace.h" 36 #include "ocfs2_trace.h" 27 #include "file.h" 37 #include "file.h" 28 #include "symlink.h" << 29 38 30 #include <linux/bio.h> 39 #include <linux/bio.h> 31 #include <linux/blkdev.h> 40 #include <linux/blkdev.h> 32 #include <linux/slab.h> 41 #include <linux/slab.h> 33 #include <linux/writeback.h> 42 #include <linux/writeback.h> 34 #include <linux/pagevec.h> 43 #include <linux/pagevec.h> 35 #include <linux/swap.h> 44 #include <linux/swap.h> 36 #include <linux/security.h> 45 #include <linux/security.h> 37 #include <linux/fsnotify.h> 46 #include <linux/fsnotify.h> 38 #include <linux/quotaops.h> 47 #include <linux/quotaops.h> 39 #include <linux/namei.h> 48 #include <linux/namei.h> 40 #include <linux/mount.h> 49 #include <linux/mount.h> 41 #include <linux/posix_acl.h> 50 #include <linux/posix_acl.h> 42 51 43 struct ocfs2_cow_context { 52 struct ocfs2_cow_context { 44 struct inode *inode; 53 struct inode *inode; 45 u32 cow_start; 54 u32 cow_start; 46 u32 cow_len; 55 u32 cow_len; 47 struct ocfs2_extent_tree data_et; 56 struct ocfs2_extent_tree data_et; 48 struct ocfs2_refcount_tree *ref_tree; 57 struct ocfs2_refcount_tree *ref_tree; 49 struct buffer_head *ref_root_bh; 58 struct buffer_head *ref_root_bh; 50 struct ocfs2_alloc_context *meta_ac; 59 struct ocfs2_alloc_context *meta_ac; 51 struct ocfs2_alloc_context *data_ac; 60 struct ocfs2_alloc_context *data_ac; 52 struct ocfs2_cached_dealloc_ctxt deall 61 struct ocfs2_cached_dealloc_ctxt dealloc; 53 void *cow_object; 62 void *cow_object; 54 struct ocfs2_post_refcount *post_refco 63 struct ocfs2_post_refcount *post_refcount; 55 int extra_credits; 64 int extra_credits; 56 int (*get_clusters)(struct ocfs2_cow_c 65 int (*get_clusters)(struct ocfs2_cow_context *context, 57 u32 v_cluster, u32 66 u32 v_cluster, u32 *p_cluster, 58 u32 *num_clusters, 67 u32 *num_clusters, 59 unsigned int *exte 68 unsigned int *extent_flags); 60 int (*cow_duplicate_clusters)(handle_t 69 int (*cow_duplicate_clusters)(handle_t *handle, 61 struct i 70 struct inode *inode, 62 u32 cpos 71 u32 cpos, u32 old_cluster, 63 u32 new_ 72 u32 new_cluster, u32 new_len); 64 }; 73 }; 65 74 66 static inline struct ocfs2_refcount_tree * 75 static inline struct ocfs2_refcount_tree * 67 cache_info_to_refcount(struct ocfs2_caching_in 76 cache_info_to_refcount(struct ocfs2_caching_info *ci) 68 { 77 { 69 return container_of(ci, struct ocfs2_r 78 return container_of(ci, struct ocfs2_refcount_tree, rf_ci); 70 } 79 } 71 80 72 static int ocfs2_validate_refcount_block(struc 81 static int ocfs2_validate_refcount_block(struct super_block *sb, 73 struc 82 struct buffer_head *bh) 74 { 83 { 75 int rc; 84 int rc; 76 struct ocfs2_refcount_block *rb = 85 struct ocfs2_refcount_block *rb = 77 (struct ocfs2_refcount_block * 86 (struct ocfs2_refcount_block *)bh->b_data; 78 87 79 trace_ocfs2_validate_refcount_block((u 88 trace_ocfs2_validate_refcount_block((unsigned long long)bh->b_blocknr); 80 89 81 BUG_ON(!buffer_uptodate(bh)); 90 BUG_ON(!buffer_uptodate(bh)); 82 91 83 /* 92 /* 84 * If the ecc fails, we return the err 93 * If the ecc fails, we return the error but otherwise 85 * leave the filesystem running. We k 94 * leave the filesystem running. We know any error is 86 * local to this block. 95 * local to this block. 87 */ 96 */ 88 rc = ocfs2_validate_meta_ecc(sb, bh->b 97 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check); 89 if (rc) { 98 if (rc) { 90 mlog(ML_ERROR, "Checksum faile 99 mlog(ML_ERROR, "Checksum failed for refcount block %llu\n", 91 (unsigned long long)bh->b 100 (unsigned long long)bh->b_blocknr); 92 return rc; 101 return rc; 93 } 102 } 94 103 95 104 96 if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb) 105 if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) { 97 rc = ocfs2_error(sb, 106 rc = ocfs2_error(sb, 98 "Refcount blo 107 "Refcount block #%llu has bad signature %.*s\n", 99 (unsigned lon 108 (unsigned long long)bh->b_blocknr, 7, 100 rb->rf_signat 109 rb->rf_signature); 101 goto out; 110 goto out; 102 } 111 } 103 112 104 if (le64_to_cpu(rb->rf_blkno) != bh->b 113 if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) { 105 rc = ocfs2_error(sb, 114 rc = ocfs2_error(sb, 106 "Refcount blo 115 "Refcount block #%llu has an invalid rf_blkno of %llu\n", 107 (unsigned lon 116 (unsigned long long)bh->b_blocknr, 108 (unsigned lon 117 (unsigned long long)le64_to_cpu(rb->rf_blkno)); 109 goto out; 118 goto out; 110 } 119 } 111 120 112 if (le32_to_cpu(rb->rf_fs_generation) 121 if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) { 113 rc = ocfs2_error(sb, 122 rc = ocfs2_error(sb, 114 "Refcount blo 123 "Refcount block #%llu has an invalid rf_fs_generation of #%u\n", 115 (unsigned lon 124 (unsigned long long)bh->b_blocknr, 116 le32_to_cpu(r 125 le32_to_cpu(rb->rf_fs_generation)); 117 goto out; 126 goto out; 118 } 127 } 119 out: 128 out: 120 return rc; 129 return rc; 121 } 130 } 122 131 123 static int ocfs2_read_refcount_block(struct oc 132 static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci, 124 u64 rb_bl 133 u64 rb_blkno, 125 struct bu 134 struct buffer_head **bh) 126 { 135 { 127 int rc; 136 int rc; 128 struct buffer_head *tmp = *bh; 137 struct buffer_head *tmp = *bh; 129 138 130 rc = ocfs2_read_block(ci, rb_blkno, &t 139 rc = ocfs2_read_block(ci, rb_blkno, &tmp, 131 ocfs2_validate_r 140 ocfs2_validate_refcount_block); 132 141 133 /* If ocfs2_read_block() got us a new 142 /* If ocfs2_read_block() got us a new bh, pass it up. */ 134 if (!rc && !*bh) 143 if (!rc && !*bh) 135 *bh = tmp; 144 *bh = tmp; 136 145 137 return rc; 146 return rc; 138 } 147 } 139 148 140 static u64 ocfs2_refcount_cache_owner(struct o 149 static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci) 141 { 150 { 142 struct ocfs2_refcount_tree *rf = cache 151 struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 143 152 144 return rf->rf_blkno; 153 return rf->rf_blkno; 145 } 154 } 146 155 147 static struct super_block * 156 static struct super_block * 148 ocfs2_refcount_cache_get_super(struct ocfs2_ca 157 ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci) 149 { 158 { 150 struct ocfs2_refcount_tree *rf = cache 159 struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 151 160 152 return rf->rf_sb; 161 return rf->rf_sb; 153 } 162 } 154 163 155 static void ocfs2_refcount_cache_lock(struct o 164 static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci) 156 __acquires(&rf->rf_lock) << 157 { 165 { 158 struct ocfs2_refcount_tree *rf = cache 166 struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 159 167 160 spin_lock(&rf->rf_lock); 168 spin_lock(&rf->rf_lock); 161 } 169 } 162 170 163 static void ocfs2_refcount_cache_unlock(struct 171 static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci) 164 __releases(&rf->rf_lock) << 165 { 172 { 166 struct ocfs2_refcount_tree *rf = cache 173 struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 167 174 168 spin_unlock(&rf->rf_lock); 175 spin_unlock(&rf->rf_lock); 169 } 176 } 170 177 171 static void ocfs2_refcount_cache_io_lock(struc 178 static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci) 172 { 179 { 173 struct ocfs2_refcount_tree *rf = cache 180 struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 174 181 175 mutex_lock(&rf->rf_io_mutex); 182 mutex_lock(&rf->rf_io_mutex); 176 } 183 } 177 184 178 static void ocfs2_refcount_cache_io_unlock(str 185 static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci) 179 { 186 { 180 struct ocfs2_refcount_tree *rf = cache 187 struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 181 188 182 mutex_unlock(&rf->rf_io_mutex); 189 mutex_unlock(&rf->rf_io_mutex); 183 } 190 } 184 191 185 static const struct ocfs2_caching_operations o 192 static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = { 186 .co_owner = ocfs2_refcou 193 .co_owner = ocfs2_refcount_cache_owner, 187 .co_get_super = ocfs2_refcou 194 .co_get_super = ocfs2_refcount_cache_get_super, 188 .co_cache_lock = ocfs2_refcou 195 .co_cache_lock = ocfs2_refcount_cache_lock, 189 .co_cache_unlock = ocfs2_refcou 196 .co_cache_unlock = ocfs2_refcount_cache_unlock, 190 .co_io_lock = ocfs2_refcou 197 .co_io_lock = ocfs2_refcount_cache_io_lock, 191 .co_io_unlock = ocfs2_refcou 198 .co_io_unlock = ocfs2_refcount_cache_io_unlock, 192 }; 199 }; 193 200 194 static struct ocfs2_refcount_tree * 201 static struct ocfs2_refcount_tree * 195 ocfs2_find_refcount_tree(struct ocfs2_super *o 202 ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno) 196 { 203 { 197 struct rb_node *n = osb->osb_rf_lock_t 204 struct rb_node *n = osb->osb_rf_lock_tree.rb_node; 198 struct ocfs2_refcount_tree *tree = NUL 205 struct ocfs2_refcount_tree *tree = NULL; 199 206 200 while (n) { 207 while (n) { 201 tree = rb_entry(n, struct ocfs 208 tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node); 202 209 203 if (blkno < tree->rf_blkno) 210 if (blkno < tree->rf_blkno) 204 n = n->rb_left; 211 n = n->rb_left; 205 else if (blkno > tree->rf_blkn 212 else if (blkno > tree->rf_blkno) 206 n = n->rb_right; 213 n = n->rb_right; 207 else 214 else 208 return tree; 215 return tree; 209 } 216 } 210 217 211 return NULL; 218 return NULL; 212 } 219 } 213 220 214 /* osb_lock is already locked. */ 221 /* osb_lock is already locked. */ 215 static void ocfs2_insert_refcount_tree(struct 222 static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb, 216 struct 223 struct ocfs2_refcount_tree *new) 217 { 224 { 218 u64 rf_blkno = new->rf_blkno; 225 u64 rf_blkno = new->rf_blkno; 219 struct rb_node *parent = NULL; 226 struct rb_node *parent = NULL; 220 struct rb_node **p = &osb->osb_rf_lock 227 struct rb_node **p = &osb->osb_rf_lock_tree.rb_node; 221 struct ocfs2_refcount_tree *tmp; 228 struct ocfs2_refcount_tree *tmp; 222 229 223 while (*p) { 230 while (*p) { 224 parent = *p; 231 parent = *p; 225 232 226 tmp = rb_entry(parent, struct 233 tmp = rb_entry(parent, struct ocfs2_refcount_tree, 227 rf_node); 234 rf_node); 228 235 229 if (rf_blkno < tmp->rf_blkno) 236 if (rf_blkno < tmp->rf_blkno) 230 p = &(*p)->rb_left; 237 p = &(*p)->rb_left; 231 else if (rf_blkno > tmp->rf_bl 238 else if (rf_blkno > tmp->rf_blkno) 232 p = &(*p)->rb_right; 239 p = &(*p)->rb_right; 233 else { 240 else { 234 /* This should never h 241 /* This should never happen! */ 235 mlog(ML_ERROR, "Duplic 242 mlog(ML_ERROR, "Duplicate refcount block %llu found!\n", 236 (unsigned long lo 243 (unsigned long long)rf_blkno); 237 BUG(); 244 BUG(); 238 } 245 } 239 } 246 } 240 247 241 rb_link_node(&new->rf_node, parent, p) 248 rb_link_node(&new->rf_node, parent, p); 242 rb_insert_color(&new->rf_node, &osb->o 249 rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree); 243 } 250 } 244 251 245 static void ocfs2_free_refcount_tree(struct oc 252 static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree) 246 { 253 { 247 ocfs2_metadata_cache_exit(&tree->rf_ci 254 ocfs2_metadata_cache_exit(&tree->rf_ci); 248 ocfs2_simple_drop_lockres(OCFS2_SB(tre 255 ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres); 249 ocfs2_lock_res_free(&tree->rf_lockres) 256 ocfs2_lock_res_free(&tree->rf_lockres); 250 kfree(tree); 257 kfree(tree); 251 } 258 } 252 259 253 static inline void 260 static inline void 254 ocfs2_erase_refcount_tree_from_list_no_lock(st 261 ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb, 255 struct 262 struct ocfs2_refcount_tree *tree) 256 { 263 { 257 rb_erase(&tree->rf_node, &osb->osb_rf_ 264 rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree); 258 if (osb->osb_ref_tree_lru && osb->osb_ 265 if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree) 259 osb->osb_ref_tree_lru = NULL; 266 osb->osb_ref_tree_lru = NULL; 260 } 267 } 261 268 262 static void ocfs2_erase_refcount_tree_from_lis 269 static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb, 263 struct 270 struct ocfs2_refcount_tree *tree) 264 { 271 { 265 spin_lock(&osb->osb_lock); 272 spin_lock(&osb->osb_lock); 266 ocfs2_erase_refcount_tree_from_list_no 273 ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree); 267 spin_unlock(&osb->osb_lock); 274 spin_unlock(&osb->osb_lock); 268 } 275 } 269 276 270 static void ocfs2_kref_remove_refcount_tree(st 277 static void ocfs2_kref_remove_refcount_tree(struct kref *kref) 271 { 278 { 272 struct ocfs2_refcount_tree *tree = 279 struct ocfs2_refcount_tree *tree = 273 container_of(kref, struct ocfs 280 container_of(kref, struct ocfs2_refcount_tree, rf_getcnt); 274 281 275 ocfs2_free_refcount_tree(tree); 282 ocfs2_free_refcount_tree(tree); 276 } 283 } 277 284 278 static inline void 285 static inline void 279 ocfs2_refcount_tree_get(struct ocfs2_refcount_ 286 ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree) 280 { 287 { 281 kref_get(&tree->rf_getcnt); 288 kref_get(&tree->rf_getcnt); 282 } 289 } 283 290 284 static inline void 291 static inline void 285 ocfs2_refcount_tree_put(struct ocfs2_refcount_ 292 ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree) 286 { 293 { 287 kref_put(&tree->rf_getcnt, ocfs2_kref_ 294 kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree); 288 } 295 } 289 296 290 static inline void ocfs2_init_refcount_tree_ci 297 static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new, 291 298 struct super_block *sb) 292 { 299 { 293 ocfs2_metadata_cache_init(&new->rf_ci, 300 ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops); 294 mutex_init(&new->rf_io_mutex); 301 mutex_init(&new->rf_io_mutex); 295 new->rf_sb = sb; 302 new->rf_sb = sb; 296 spin_lock_init(&new->rf_lock); 303 spin_lock_init(&new->rf_lock); 297 } 304 } 298 305 299 static inline void ocfs2_init_refcount_tree_lo 306 static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb, 300 struct 307 struct ocfs2_refcount_tree *new, 301 u64 rf 308 u64 rf_blkno, u32 generation) 302 { 309 { 303 init_rwsem(&new->rf_sem); 310 init_rwsem(&new->rf_sem); 304 ocfs2_refcount_lock_res_init(&new->rf_ 311 ocfs2_refcount_lock_res_init(&new->rf_lockres, osb, 305 rf_blkno, 312 rf_blkno, generation); 306 } 313 } 307 314 308 static struct ocfs2_refcount_tree* 315 static struct ocfs2_refcount_tree* 309 ocfs2_allocate_refcount_tree(struct ocfs2_supe 316 ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno) 310 { 317 { 311 struct ocfs2_refcount_tree *new; 318 struct ocfs2_refcount_tree *new; 312 319 313 new = kzalloc(sizeof(struct ocfs2_refc 320 new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS); 314 if (!new) 321 if (!new) 315 return NULL; 322 return NULL; 316 323 317 new->rf_blkno = rf_blkno; 324 new->rf_blkno = rf_blkno; 318 kref_init(&new->rf_getcnt); 325 kref_init(&new->rf_getcnt); 319 ocfs2_init_refcount_tree_ci(new, osb-> 326 ocfs2_init_refcount_tree_ci(new, osb->sb); 320 327 321 return new; 328 return new; 322 } 329 } 323 330 324 static int ocfs2_get_refcount_tree(struct ocfs 331 static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno, 325 struct ocfs 332 struct ocfs2_refcount_tree **ret_tree) 326 { 333 { 327 int ret = 0; 334 int ret = 0; 328 struct ocfs2_refcount_tree *tree, *new 335 struct ocfs2_refcount_tree *tree, *new = NULL; 329 struct buffer_head *ref_root_bh = NULL 336 struct buffer_head *ref_root_bh = NULL; 330 struct ocfs2_refcount_block *ref_rb; 337 struct ocfs2_refcount_block *ref_rb; 331 338 332 spin_lock(&osb->osb_lock); 339 spin_lock(&osb->osb_lock); 333 if (osb->osb_ref_tree_lru && 340 if (osb->osb_ref_tree_lru && 334 osb->osb_ref_tree_lru->rf_blkno == 341 osb->osb_ref_tree_lru->rf_blkno == rf_blkno) 335 tree = osb->osb_ref_tree_lru; 342 tree = osb->osb_ref_tree_lru; 336 else 343 else 337 tree = ocfs2_find_refcount_tre 344 tree = ocfs2_find_refcount_tree(osb, rf_blkno); 338 if (tree) 345 if (tree) 339 goto out; 346 goto out; 340 347 341 spin_unlock(&osb->osb_lock); 348 spin_unlock(&osb->osb_lock); 342 349 343 new = ocfs2_allocate_refcount_tree(osb 350 new = ocfs2_allocate_refcount_tree(osb, rf_blkno); 344 if (!new) { 351 if (!new) { 345 ret = -ENOMEM; 352 ret = -ENOMEM; 346 mlog_errno(ret); 353 mlog_errno(ret); 347 return ret; 354 return ret; 348 } 355 } 349 /* 356 /* 350 * We need the generation to create th 357 * We need the generation to create the refcount tree lock and since 351 * it isn't changed during the tree mo 358 * it isn't changed during the tree modification, we are safe here to 352 * read without protection. 359 * read without protection. 353 * We also have to purge the cache aft 360 * We also have to purge the cache after we create the lock since the 354 * refcount block may have the stale d 361 * refcount block may have the stale data. It can only be trusted when 355 * we hold the refcount lock. 362 * we hold the refcount lock. 356 */ 363 */ 357 ret = ocfs2_read_refcount_block(&new-> 364 ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh); 358 if (ret) { 365 if (ret) { 359 mlog_errno(ret); 366 mlog_errno(ret); 360 ocfs2_metadata_cache_exit(&new 367 ocfs2_metadata_cache_exit(&new->rf_ci); 361 kfree(new); 368 kfree(new); 362 return ret; 369 return ret; 363 } 370 } 364 371 365 ref_rb = (struct ocfs2_refcount_block 372 ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 366 new->rf_generation = le32_to_cpu(ref_r 373 new->rf_generation = le32_to_cpu(ref_rb->rf_generation); 367 ocfs2_init_refcount_tree_lock(osb, new 374 ocfs2_init_refcount_tree_lock(osb, new, rf_blkno, 368 new->rf_ 375 new->rf_generation); 369 ocfs2_metadata_cache_purge(&new->rf_ci 376 ocfs2_metadata_cache_purge(&new->rf_ci); 370 377 371 spin_lock(&osb->osb_lock); 378 spin_lock(&osb->osb_lock); 372 tree = ocfs2_find_refcount_tree(osb, r 379 tree = ocfs2_find_refcount_tree(osb, rf_blkno); 373 if (tree) 380 if (tree) 374 goto out; 381 goto out; 375 382 376 ocfs2_insert_refcount_tree(osb, new); 383 ocfs2_insert_refcount_tree(osb, new); 377 384 378 tree = new; 385 tree = new; 379 new = NULL; 386 new = NULL; 380 387 381 out: 388 out: 382 *ret_tree = tree; 389 *ret_tree = tree; 383 390 384 osb->osb_ref_tree_lru = tree; 391 osb->osb_ref_tree_lru = tree; 385 392 386 spin_unlock(&osb->osb_lock); 393 spin_unlock(&osb->osb_lock); 387 394 388 if (new) 395 if (new) 389 ocfs2_free_refcount_tree(new); 396 ocfs2_free_refcount_tree(new); 390 397 391 brelse(ref_root_bh); 398 brelse(ref_root_bh); 392 return ret; 399 return ret; 393 } 400 } 394 401 395 static int ocfs2_get_refcount_block(struct ino 402 static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno) 396 { 403 { 397 int ret; 404 int ret; 398 struct buffer_head *di_bh = NULL; 405 struct buffer_head *di_bh = NULL; 399 struct ocfs2_dinode *di; 406 struct ocfs2_dinode *di; 400 407 401 ret = ocfs2_read_inode_block(inode, &d 408 ret = ocfs2_read_inode_block(inode, &di_bh); 402 if (ret) { 409 if (ret) { 403 mlog_errno(ret); 410 mlog_errno(ret); 404 goto out; 411 goto out; 405 } 412 } 406 413 407 BUG_ON(!ocfs2_is_refcount_inode(inode) 414 BUG_ON(!ocfs2_is_refcount_inode(inode)); 408 415 409 di = (struct ocfs2_dinode *)di_bh->b_d 416 di = (struct ocfs2_dinode *)di_bh->b_data; 410 *ref_blkno = le64_to_cpu(di->i_refcoun 417 *ref_blkno = le64_to_cpu(di->i_refcount_loc); 411 brelse(di_bh); 418 brelse(di_bh); 412 out: 419 out: 413 return ret; 420 return ret; 414 } 421 } 415 422 416 static int __ocfs2_lock_refcount_tree(struct o 423 static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb, 417 struct o 424 struct ocfs2_refcount_tree *tree, int rw) 418 { 425 { 419 int ret; 426 int ret; 420 427 421 ret = ocfs2_refcount_lock(tree, rw); 428 ret = ocfs2_refcount_lock(tree, rw); 422 if (ret) { 429 if (ret) { 423 mlog_errno(ret); 430 mlog_errno(ret); 424 goto out; 431 goto out; 425 } 432 } 426 433 427 if (rw) 434 if (rw) 428 down_write(&tree->rf_sem); 435 down_write(&tree->rf_sem); 429 else 436 else 430 down_read(&tree->rf_sem); 437 down_read(&tree->rf_sem); 431 438 432 out: 439 out: 433 return ret; 440 return ret; 434 } 441 } 435 442 436 /* 443 /* 437 * Lock the refcount tree pointed by ref_blkno 444 * Lock the refcount tree pointed by ref_blkno and return the tree. 438 * In most case, we lock the tree and read the 445 * In most case, we lock the tree and read the refcount block. 439 * So read it here if the caller really needs 446 * So read it here if the caller really needs it. 440 * 447 * 441 * If the tree has been re-created by other no 448 * If the tree has been re-created by other node, it will free the 442 * old one and re-create it. 449 * old one and re-create it. 443 */ 450 */ 444 int ocfs2_lock_refcount_tree(struct ocfs2_supe 451 int ocfs2_lock_refcount_tree(struct ocfs2_super *osb, 445 u64 ref_blkno, in 452 u64 ref_blkno, int rw, 446 struct ocfs2_refc 453 struct ocfs2_refcount_tree **ret_tree, 447 struct buffer_hea 454 struct buffer_head **ref_bh) 448 { 455 { 449 int ret, delete_tree = 0; 456 int ret, delete_tree = 0; 450 struct ocfs2_refcount_tree *tree = NUL 457 struct ocfs2_refcount_tree *tree = NULL; 451 struct buffer_head *ref_root_bh = NULL 458 struct buffer_head *ref_root_bh = NULL; 452 struct ocfs2_refcount_block *rb; 459 struct ocfs2_refcount_block *rb; 453 460 454 again: 461 again: 455 ret = ocfs2_get_refcount_tree(osb, ref 462 ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree); 456 if (ret) { 463 if (ret) { 457 mlog_errno(ret); 464 mlog_errno(ret); 458 return ret; 465 return ret; 459 } 466 } 460 467 461 ocfs2_refcount_tree_get(tree); 468 ocfs2_refcount_tree_get(tree); 462 469 463 ret = __ocfs2_lock_refcount_tree(osb, 470 ret = __ocfs2_lock_refcount_tree(osb, tree, rw); 464 if (ret) { 471 if (ret) { 465 mlog_errno(ret); 472 mlog_errno(ret); 466 ocfs2_refcount_tree_put(tree); 473 ocfs2_refcount_tree_put(tree); 467 goto out; 474 goto out; 468 } 475 } 469 476 470 ret = ocfs2_read_refcount_block(&tree- 477 ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno, 471 &ref_r 478 &ref_root_bh); 472 if (ret) { 479 if (ret) { 473 mlog_errno(ret); 480 mlog_errno(ret); 474 ocfs2_unlock_refcount_tree(osb 481 ocfs2_unlock_refcount_tree(osb, tree, rw); 475 goto out; 482 goto out; 476 } 483 } 477 484 478 rb = (struct ocfs2_refcount_block *)re 485 rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 479 /* 486 /* 480 * If the refcount block has been free 487 * If the refcount block has been freed and re-created, we may need 481 * to recreate the refcount tree also. 488 * to recreate the refcount tree also. 482 * 489 * 483 * Here we just remove the tree from t 490 * Here we just remove the tree from the rb-tree, and the last 484 * kref holder will unlock and delete 491 * kref holder will unlock and delete this refcount_tree. 485 * Then we goto "again" and ocfs2_get_ 492 * Then we goto "again" and ocfs2_get_refcount_tree will create 486 * the new refcount tree for us. 493 * the new refcount tree for us. 487 */ 494 */ 488 if (tree->rf_generation != le32_to_cpu 495 if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) { 489 if (!tree->rf_removed) { 496 if (!tree->rf_removed) { 490 ocfs2_erase_refcount_t 497 ocfs2_erase_refcount_tree_from_list(osb, tree); 491 tree->rf_removed = 1; 498 tree->rf_removed = 1; 492 delete_tree = 1; 499 delete_tree = 1; 493 } 500 } 494 501 495 ocfs2_unlock_refcount_tree(osb 502 ocfs2_unlock_refcount_tree(osb, tree, rw); 496 /* 503 /* 497 * We get an extra reference w 504 * We get an extra reference when we create the refcount 498 * tree, so another put will d 505 * tree, so another put will destroy it. 499 */ 506 */ 500 if (delete_tree) 507 if (delete_tree) 501 ocfs2_refcount_tree_pu 508 ocfs2_refcount_tree_put(tree); 502 brelse(ref_root_bh); 509 brelse(ref_root_bh); 503 ref_root_bh = NULL; 510 ref_root_bh = NULL; 504 goto again; 511 goto again; 505 } 512 } 506 513 507 *ret_tree = tree; 514 *ret_tree = tree; 508 if (ref_bh) { 515 if (ref_bh) { 509 *ref_bh = ref_root_bh; 516 *ref_bh = ref_root_bh; 510 ref_root_bh = NULL; 517 ref_root_bh = NULL; 511 } 518 } 512 out: 519 out: 513 brelse(ref_root_bh); 520 brelse(ref_root_bh); 514 return ret; 521 return ret; 515 } 522 } 516 523 517 void ocfs2_unlock_refcount_tree(struct ocfs2_s 524 void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb, 518 struct ocfs2_r 525 struct ocfs2_refcount_tree *tree, int rw) 519 { 526 { 520 if (rw) 527 if (rw) 521 up_write(&tree->rf_sem); 528 up_write(&tree->rf_sem); 522 else 529 else 523 up_read(&tree->rf_sem); 530 up_read(&tree->rf_sem); 524 531 525 ocfs2_refcount_unlock(tree, rw); 532 ocfs2_refcount_unlock(tree, rw); 526 ocfs2_refcount_tree_put(tree); 533 ocfs2_refcount_tree_put(tree); 527 } 534 } 528 535 529 void ocfs2_purge_refcount_trees(struct ocfs2_s 536 void ocfs2_purge_refcount_trees(struct ocfs2_super *osb) 530 { 537 { 531 struct rb_node *node; 538 struct rb_node *node; 532 struct ocfs2_refcount_tree *tree; 539 struct ocfs2_refcount_tree *tree; 533 struct rb_root *root = &osb->osb_rf_lo 540 struct rb_root *root = &osb->osb_rf_lock_tree; 534 541 535 while ((node = rb_last(root)) != NULL) 542 while ((node = rb_last(root)) != NULL) { 536 tree = rb_entry(node, struct o 543 tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node); 537 544 538 trace_ocfs2_purge_refcount_tre 545 trace_ocfs2_purge_refcount_trees( 539 (unsigned long 546 (unsigned long long) tree->rf_blkno); 540 547 541 rb_erase(&tree->rf_node, root) 548 rb_erase(&tree->rf_node, root); 542 ocfs2_free_refcount_tree(tree) 549 ocfs2_free_refcount_tree(tree); 543 } 550 } 544 } 551 } 545 552 546 /* 553 /* 547 * Create a refcount tree for an inode. 554 * Create a refcount tree for an inode. 548 * We take for granted that the inode is alrea 555 * We take for granted that the inode is already locked. 549 */ 556 */ 550 static int ocfs2_create_refcount_tree(struct i 557 static int ocfs2_create_refcount_tree(struct inode *inode, 551 struct b 558 struct buffer_head *di_bh) 552 { 559 { 553 int ret; 560 int ret; 554 handle_t *handle = NULL; 561 handle_t *handle = NULL; 555 struct ocfs2_alloc_context *meta_ac = 562 struct ocfs2_alloc_context *meta_ac = NULL; 556 struct ocfs2_dinode *di = (struct ocfs 563 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 557 struct ocfs2_inode_info *oi = OCFS2_I( 564 struct ocfs2_inode_info *oi = OCFS2_I(inode); 558 struct ocfs2_super *osb = OCFS2_SB(ino 565 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 559 struct buffer_head *new_bh = NULL; 566 struct buffer_head *new_bh = NULL; 560 struct ocfs2_refcount_block *rb; 567 struct ocfs2_refcount_block *rb; 561 struct ocfs2_refcount_tree *new_tree = 568 struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL; 562 u16 suballoc_bit_start; 569 u16 suballoc_bit_start; 563 u32 num_got; 570 u32 num_got; 564 u64 suballoc_loc, first_blkno; 571 u64 suballoc_loc, first_blkno; 565 572 566 BUG_ON(ocfs2_is_refcount_inode(inode)) 573 BUG_ON(ocfs2_is_refcount_inode(inode)); 567 574 568 trace_ocfs2_create_refcount_tree( 575 trace_ocfs2_create_refcount_tree( 569 (unsigned long long)oi->ip_blk 576 (unsigned long long)oi->ip_blkno); 570 577 571 ret = ocfs2_reserve_new_metadata_block 578 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); 572 if (ret) { 579 if (ret) { 573 mlog_errno(ret); 580 mlog_errno(ret); 574 goto out; 581 goto out; 575 } 582 } 576 583 577 handle = ocfs2_start_trans(osb, OCFS2_ 584 handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS); 578 if (IS_ERR(handle)) { 585 if (IS_ERR(handle)) { 579 ret = PTR_ERR(handle); 586 ret = PTR_ERR(handle); 580 mlog_errno(ret); 587 mlog_errno(ret); 581 goto out; 588 goto out; 582 } 589 } 583 590 584 ret = ocfs2_journal_access_di(handle, 591 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 585 OCFS2_JO 592 OCFS2_JOURNAL_ACCESS_WRITE); 586 if (ret) { 593 if (ret) { 587 mlog_errno(ret); 594 mlog_errno(ret); 588 goto out_commit; 595 goto out_commit; 589 } 596 } 590 597 591 ret = ocfs2_claim_metadata(handle, met 598 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 592 &suballoc_b 599 &suballoc_bit_start, &num_got, 593 &first_blkn 600 &first_blkno); 594 if (ret) { 601 if (ret) { 595 mlog_errno(ret); 602 mlog_errno(ret); 596 goto out_commit; 603 goto out_commit; 597 } 604 } 598 605 599 new_tree = ocfs2_allocate_refcount_tre 606 new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno); 600 if (!new_tree) { 607 if (!new_tree) { 601 ret = -ENOMEM; 608 ret = -ENOMEM; 602 mlog_errno(ret); 609 mlog_errno(ret); 603 goto out_commit; 610 goto out_commit; 604 } 611 } 605 612 606 new_bh = sb_getblk(inode->i_sb, first_ 613 new_bh = sb_getblk(inode->i_sb, first_blkno); 607 if (!new_bh) { 614 if (!new_bh) { 608 ret = -ENOMEM; 615 ret = -ENOMEM; 609 mlog_errno(ret); 616 mlog_errno(ret); 610 goto out_commit; 617 goto out_commit; 611 } 618 } 612 ocfs2_set_new_buffer_uptodate(&new_tre 619 ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh); 613 620 614 ret = ocfs2_journal_access_rb(handle, 621 ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh, 615 OCFS2_JO 622 OCFS2_JOURNAL_ACCESS_CREATE); 616 if (ret) { 623 if (ret) { 617 mlog_errno(ret); 624 mlog_errno(ret); 618 goto out_commit; 625 goto out_commit; 619 } 626 } 620 627 621 /* Initialize ocfs2_refcount_block. */ 628 /* Initialize ocfs2_refcount_block. */ 622 rb = (struct ocfs2_refcount_block *)ne 629 rb = (struct ocfs2_refcount_block *)new_bh->b_data; 623 memset(rb, 0, inode->i_sb->s_blocksize 630 memset(rb, 0, inode->i_sb->s_blocksize); 624 strcpy((void *)rb, OCFS2_REFCOUNT_BLOC 631 strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 625 rb->rf_suballoc_slot = cpu_to_le16(met 632 rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 626 rb->rf_suballoc_loc = cpu_to_le64(suba 633 rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc); 627 rb->rf_suballoc_bit = cpu_to_le16(suba 634 rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 628 rb->rf_fs_generation = cpu_to_le32(osb 635 rb->rf_fs_generation = cpu_to_le32(osb->fs_generation); 629 rb->rf_blkno = cpu_to_le64(first_blkno 636 rb->rf_blkno = cpu_to_le64(first_blkno); 630 rb->rf_count = cpu_to_le32(1); 637 rb->rf_count = cpu_to_le32(1); 631 rb->rf_records.rl_count = 638 rb->rf_records.rl_count = 632 cpu_to_le16(ocfs2_refc 639 cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb)); 633 spin_lock(&osb->osb_lock); 640 spin_lock(&osb->osb_lock); 634 rb->rf_generation = cpu_to_le32(osb->s !! 641 rb->rf_generation = osb->s_next_generation++; 635 spin_unlock(&osb->osb_lock); 642 spin_unlock(&osb->osb_lock); 636 643 637 ocfs2_journal_dirty(handle, new_bh); 644 ocfs2_journal_dirty(handle, new_bh); 638 645 639 spin_lock(&oi->ip_lock); 646 spin_lock(&oi->ip_lock); 640 oi->ip_dyn_features |= OCFS2_HAS_REFCO 647 oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL; 641 di->i_dyn_features = cpu_to_le16(oi->i 648 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 642 di->i_refcount_loc = cpu_to_le64(first 649 di->i_refcount_loc = cpu_to_le64(first_blkno); 643 spin_unlock(&oi->ip_lock); 650 spin_unlock(&oi->ip_lock); 644 651 645 trace_ocfs2_create_refcount_tree_blkno 652 trace_ocfs2_create_refcount_tree_blkno((unsigned long long)first_blkno); 646 653 647 ocfs2_journal_dirty(handle, di_bh); 654 ocfs2_journal_dirty(handle, di_bh); 648 655 649 /* 656 /* 650 * We have to init the tree lock here 657 * We have to init the tree lock here since it will use 651 * the generation number to create it. 658 * the generation number to create it. 652 */ 659 */ 653 new_tree->rf_generation = le32_to_cpu( 660 new_tree->rf_generation = le32_to_cpu(rb->rf_generation); 654 ocfs2_init_refcount_tree_lock(osb, new 661 ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno, 655 new_tree 662 new_tree->rf_generation); 656 663 657 spin_lock(&osb->osb_lock); 664 spin_lock(&osb->osb_lock); 658 tree = ocfs2_find_refcount_tree(osb, f 665 tree = ocfs2_find_refcount_tree(osb, first_blkno); 659 666 660 /* 667 /* 661 * We've just created a new refcount t 668 * We've just created a new refcount tree in this block. If 662 * we found a refcount tree on the ocf 669 * we found a refcount tree on the ocfs2_super, it must be 663 * one we just deleted. We free the o 670 * one we just deleted. We free the old tree before 664 * inserting the new tree. 671 * inserting the new tree. 665 */ 672 */ 666 BUG_ON(tree && tree->rf_generation == 673 BUG_ON(tree && tree->rf_generation == new_tree->rf_generation); 667 if (tree) 674 if (tree) 668 ocfs2_erase_refcount_tree_from 675 ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree); 669 ocfs2_insert_refcount_tree(osb, new_tr 676 ocfs2_insert_refcount_tree(osb, new_tree); 670 spin_unlock(&osb->osb_lock); 677 spin_unlock(&osb->osb_lock); 671 new_tree = NULL; 678 new_tree = NULL; 672 if (tree) 679 if (tree) 673 ocfs2_refcount_tree_put(tree); 680 ocfs2_refcount_tree_put(tree); 674 681 675 out_commit: 682 out_commit: 676 ocfs2_commit_trans(osb, handle); 683 ocfs2_commit_trans(osb, handle); 677 684 678 out: 685 out: 679 if (new_tree) { 686 if (new_tree) { 680 ocfs2_metadata_cache_exit(&new 687 ocfs2_metadata_cache_exit(&new_tree->rf_ci); 681 kfree(new_tree); 688 kfree(new_tree); 682 } 689 } 683 690 684 brelse(new_bh); 691 brelse(new_bh); 685 if (meta_ac) 692 if (meta_ac) 686 ocfs2_free_alloc_context(meta_ 693 ocfs2_free_alloc_context(meta_ac); 687 694 688 return ret; 695 return ret; 689 } 696 } 690 697 691 static int ocfs2_set_refcount_tree(struct inod 698 static int ocfs2_set_refcount_tree(struct inode *inode, 692 struct buff 699 struct buffer_head *di_bh, 693 u64 refcoun 700 u64 refcount_loc) 694 { 701 { 695 int ret; 702 int ret; 696 handle_t *handle = NULL; 703 handle_t *handle = NULL; 697 struct ocfs2_dinode *di = (struct ocfs 704 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 698 struct ocfs2_inode_info *oi = OCFS2_I( 705 struct ocfs2_inode_info *oi = OCFS2_I(inode); 699 struct ocfs2_super *osb = OCFS2_SB(ino 706 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 700 struct buffer_head *ref_root_bh = NULL 707 struct buffer_head *ref_root_bh = NULL; 701 struct ocfs2_refcount_block *rb; 708 struct ocfs2_refcount_block *rb; 702 struct ocfs2_refcount_tree *ref_tree; 709 struct ocfs2_refcount_tree *ref_tree; 703 710 704 BUG_ON(ocfs2_is_refcount_inode(inode)) 711 BUG_ON(ocfs2_is_refcount_inode(inode)); 705 712 706 ret = ocfs2_lock_refcount_tree(osb, re 713 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1, 707 &ref_tr 714 &ref_tree, &ref_root_bh); 708 if (ret) { 715 if (ret) { 709 mlog_errno(ret); 716 mlog_errno(ret); 710 return ret; 717 return ret; 711 } 718 } 712 719 713 handle = ocfs2_start_trans(osb, OCFS2_ 720 handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS); 714 if (IS_ERR(handle)) { 721 if (IS_ERR(handle)) { 715 ret = PTR_ERR(handle); 722 ret = PTR_ERR(handle); 716 mlog_errno(ret); 723 mlog_errno(ret); 717 goto out; 724 goto out; 718 } 725 } 719 726 720 ret = ocfs2_journal_access_di(handle, 727 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 721 OCFS2_JO 728 OCFS2_JOURNAL_ACCESS_WRITE); 722 if (ret) { 729 if (ret) { 723 mlog_errno(ret); 730 mlog_errno(ret); 724 goto out_commit; 731 goto out_commit; 725 } 732 } 726 733 727 ret = ocfs2_journal_access_rb(handle, 734 ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh, 728 OCFS2_JO 735 OCFS2_JOURNAL_ACCESS_WRITE); 729 if (ret) { 736 if (ret) { 730 mlog_errno(ret); 737 mlog_errno(ret); 731 goto out_commit; 738 goto out_commit; 732 } 739 } 733 740 734 rb = (struct ocfs2_refcount_block *)re 741 rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 735 le32_add_cpu(&rb->rf_count, 1); 742 le32_add_cpu(&rb->rf_count, 1); 736 743 737 ocfs2_journal_dirty(handle, ref_root_b 744 ocfs2_journal_dirty(handle, ref_root_bh); 738 745 739 spin_lock(&oi->ip_lock); 746 spin_lock(&oi->ip_lock); 740 oi->ip_dyn_features |= OCFS2_HAS_REFCO 747 oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL; 741 di->i_dyn_features = cpu_to_le16(oi->i 748 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 742 di->i_refcount_loc = cpu_to_le64(refco 749 di->i_refcount_loc = cpu_to_le64(refcount_loc); 743 spin_unlock(&oi->ip_lock); 750 spin_unlock(&oi->ip_lock); 744 ocfs2_journal_dirty(handle, di_bh); 751 ocfs2_journal_dirty(handle, di_bh); 745 752 746 out_commit: 753 out_commit: 747 ocfs2_commit_trans(osb, handle); 754 ocfs2_commit_trans(osb, handle); 748 out: 755 out: 749 ocfs2_unlock_refcount_tree(osb, ref_tr 756 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 750 brelse(ref_root_bh); 757 brelse(ref_root_bh); 751 758 752 return ret; 759 return ret; 753 } 760 } 754 761 755 int ocfs2_remove_refcount_tree(struct inode *i 762 int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh) 756 { 763 { 757 int ret, delete_tree = 0; 764 int ret, delete_tree = 0; 758 handle_t *handle = NULL; 765 handle_t *handle = NULL; 759 struct ocfs2_dinode *di = (struct ocfs 766 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 760 struct ocfs2_inode_info *oi = OCFS2_I( 767 struct ocfs2_inode_info *oi = OCFS2_I(inode); 761 struct ocfs2_super *osb = OCFS2_SB(ino 768 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 762 struct ocfs2_refcount_block *rb; 769 struct ocfs2_refcount_block *rb; 763 struct inode *alloc_inode = NULL; 770 struct inode *alloc_inode = NULL; 764 struct buffer_head *alloc_bh = NULL; 771 struct buffer_head *alloc_bh = NULL; 765 struct buffer_head *blk_bh = NULL; 772 struct buffer_head *blk_bh = NULL; 766 struct ocfs2_refcount_tree *ref_tree; 773 struct ocfs2_refcount_tree *ref_tree; 767 int credits = OCFS2_REFCOUNT_TREE_REMO 774 int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS; 768 u64 blk = 0, bg_blkno = 0, ref_blkno = 775 u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc); 769 u16 bit = 0; 776 u16 bit = 0; 770 777 771 if (!ocfs2_is_refcount_inode(inode)) 778 if (!ocfs2_is_refcount_inode(inode)) 772 return 0; 779 return 0; 773 780 774 BUG_ON(!ref_blkno); 781 BUG_ON(!ref_blkno); 775 ret = ocfs2_lock_refcount_tree(osb, re 782 ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh); 776 if (ret) { 783 if (ret) { 777 mlog_errno(ret); 784 mlog_errno(ret); 778 return ret; 785 return ret; 779 } 786 } 780 787 781 rb = (struct ocfs2_refcount_block *)bl 788 rb = (struct ocfs2_refcount_block *)blk_bh->b_data; 782 789 783 /* 790 /* 784 * If we are the last user, we need to 791 * If we are the last user, we need to free the block. 785 * So lock the allocator ahead. 792 * So lock the allocator ahead. 786 */ 793 */ 787 if (le32_to_cpu(rb->rf_count) == 1) { 794 if (le32_to_cpu(rb->rf_count) == 1) { 788 blk = le64_to_cpu(rb->rf_blkno 795 blk = le64_to_cpu(rb->rf_blkno); 789 bit = le16_to_cpu(rb->rf_subal 796 bit = le16_to_cpu(rb->rf_suballoc_bit); 790 if (rb->rf_suballoc_loc) 797 if (rb->rf_suballoc_loc) 791 bg_blkno = le64_to_cpu 798 bg_blkno = le64_to_cpu(rb->rf_suballoc_loc); 792 else 799 else 793 bg_blkno = ocfs2_which 800 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 794 801 795 alloc_inode = ocfs2_get_system 802 alloc_inode = ocfs2_get_system_file_inode(osb, 796 EXTENT 803 EXTENT_ALLOC_SYSTEM_INODE, 797 le16_t 804 le16_to_cpu(rb->rf_suballoc_slot)); 798 if (!alloc_inode) { 805 if (!alloc_inode) { 799 ret = -ENOMEM; 806 ret = -ENOMEM; 800 mlog_errno(ret); 807 mlog_errno(ret); 801 goto out; 808 goto out; 802 } 809 } 803 inode_lock(alloc_inode); 810 inode_lock(alloc_inode); 804 811 805 ret = ocfs2_inode_lock(alloc_i 812 ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1); 806 if (ret) { 813 if (ret) { 807 mlog_errno(ret); 814 mlog_errno(ret); 808 goto out_mutex; 815 goto out_mutex; 809 } 816 } 810 817 811 credits += OCFS2_SUBALLOC_FREE 818 credits += OCFS2_SUBALLOC_FREE; 812 } 819 } 813 820 814 handle = ocfs2_start_trans(osb, credit 821 handle = ocfs2_start_trans(osb, credits); 815 if (IS_ERR(handle)) { 822 if (IS_ERR(handle)) { 816 ret = PTR_ERR(handle); 823 ret = PTR_ERR(handle); 817 mlog_errno(ret); 824 mlog_errno(ret); 818 goto out_unlock; 825 goto out_unlock; 819 } 826 } 820 827 821 ret = ocfs2_journal_access_di(handle, 828 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 822 OCFS2_JO 829 OCFS2_JOURNAL_ACCESS_WRITE); 823 if (ret) { 830 if (ret) { 824 mlog_errno(ret); 831 mlog_errno(ret); 825 goto out_commit; 832 goto out_commit; 826 } 833 } 827 834 828 ret = ocfs2_journal_access_rb(handle, 835 ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh, 829 OCFS2_JO 836 OCFS2_JOURNAL_ACCESS_WRITE); 830 if (ret) { 837 if (ret) { 831 mlog_errno(ret); 838 mlog_errno(ret); 832 goto out_commit; 839 goto out_commit; 833 } 840 } 834 841 835 spin_lock(&oi->ip_lock); 842 spin_lock(&oi->ip_lock); 836 oi->ip_dyn_features &= ~OCFS2_HAS_REFC 843 oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL; 837 di->i_dyn_features = cpu_to_le16(oi->i 844 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 838 di->i_refcount_loc = 0; 845 di->i_refcount_loc = 0; 839 spin_unlock(&oi->ip_lock); 846 spin_unlock(&oi->ip_lock); 840 ocfs2_journal_dirty(handle, di_bh); 847 ocfs2_journal_dirty(handle, di_bh); 841 848 842 le32_add_cpu(&rb->rf_count , -1); 849 le32_add_cpu(&rb->rf_count , -1); 843 ocfs2_journal_dirty(handle, blk_bh); 850 ocfs2_journal_dirty(handle, blk_bh); 844 851 845 if (!rb->rf_count) { 852 if (!rb->rf_count) { 846 delete_tree = 1; 853 delete_tree = 1; 847 ocfs2_erase_refcount_tree_from 854 ocfs2_erase_refcount_tree_from_list(osb, ref_tree); 848 ret = ocfs2_free_suballoc_bits 855 ret = ocfs2_free_suballoc_bits(handle, alloc_inode, 849 856 alloc_bh, bit, bg_blkno, 1); 850 if (ret) 857 if (ret) 851 mlog_errno(ret); 858 mlog_errno(ret); 852 } 859 } 853 860 854 out_commit: 861 out_commit: 855 ocfs2_commit_trans(osb, handle); 862 ocfs2_commit_trans(osb, handle); 856 out_unlock: 863 out_unlock: 857 if (alloc_inode) { 864 if (alloc_inode) { 858 ocfs2_inode_unlock(alloc_inode 865 ocfs2_inode_unlock(alloc_inode, 1); 859 brelse(alloc_bh); 866 brelse(alloc_bh); 860 } 867 } 861 out_mutex: 868 out_mutex: 862 if (alloc_inode) { 869 if (alloc_inode) { 863 inode_unlock(alloc_inode); 870 inode_unlock(alloc_inode); 864 iput(alloc_inode); 871 iput(alloc_inode); 865 } 872 } 866 out: 873 out: 867 ocfs2_unlock_refcount_tree(osb, ref_tr 874 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 868 if (delete_tree) 875 if (delete_tree) 869 ocfs2_refcount_tree_put(ref_tr 876 ocfs2_refcount_tree_put(ref_tree); 870 brelse(blk_bh); 877 brelse(blk_bh); 871 878 872 return ret; 879 return ret; 873 } 880 } 874 881 875 static void ocfs2_find_refcount_rec_in_rl(stru 882 static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci, 876 stru 883 struct buffer_head *ref_leaf_bh, 877 u64 884 u64 cpos, unsigned int len, 878 stru 885 struct ocfs2_refcount_rec *ret_rec, 879 int 886 int *index) 880 { 887 { 881 int i = 0; 888 int i = 0; 882 struct ocfs2_refcount_block *rb = 889 struct ocfs2_refcount_block *rb = 883 (struct ocfs2_refcount_block * 890 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 884 struct ocfs2_refcount_rec *rec = NULL; 891 struct ocfs2_refcount_rec *rec = NULL; 885 892 886 for (; i < le16_to_cpu(rb->rf_records. 893 for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) { 887 rec = &rb->rf_records.rl_recs[ 894 rec = &rb->rf_records.rl_recs[i]; 888 895 889 if (le64_to_cpu(rec->r_cpos) + 896 if (le64_to_cpu(rec->r_cpos) + 890 le32_to_cpu(rec->r_cluster 897 le32_to_cpu(rec->r_clusters) <= cpos) 891 continue; 898 continue; 892 else if (le64_to_cpu(rec->r_cp 899 else if (le64_to_cpu(rec->r_cpos) > cpos) 893 break; 900 break; 894 901 895 /* ok, cpos fail in this rec. 902 /* ok, cpos fail in this rec. Just return. */ 896 if (ret_rec) 903 if (ret_rec) 897 *ret_rec = *rec; 904 *ret_rec = *rec; 898 goto out; 905 goto out; 899 } 906 } 900 907 901 if (ret_rec) { 908 if (ret_rec) { 902 /* We meet with a hole here, s 909 /* We meet with a hole here, so fake the rec. */ 903 ret_rec->r_cpos = cpu_to_le64( 910 ret_rec->r_cpos = cpu_to_le64(cpos); 904 ret_rec->r_refcount = 0; 911 ret_rec->r_refcount = 0; 905 if (i < le16_to_cpu(rb->rf_rec 912 if (i < le16_to_cpu(rb->rf_records.rl_used) && 906 le64_to_cpu(rec->r_cpos) < 913 le64_to_cpu(rec->r_cpos) < cpos + len) 907 ret_rec->r_clusters = 914 ret_rec->r_clusters = 908 cpu_to_le32(le 915 cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos); 909 else 916 else 910 ret_rec->r_clusters = 917 ret_rec->r_clusters = cpu_to_le32(len); 911 } 918 } 912 919 913 out: 920 out: 914 *index = i; 921 *index = i; 915 } 922 } 916 923 917 /* 924 /* 918 * Try to remove refcount tree. The mechanism 925 * Try to remove refcount tree. The mechanism is: 919 * 1) Check whether i_clusters == 0, if no, ex 926 * 1) Check whether i_clusters == 0, if no, exit. 920 * 2) check whether we have i_xattr_loc in din 927 * 2) check whether we have i_xattr_loc in dinode. if yes, exit. 921 * 3) Check whether we have inline xattr store 928 * 3) Check whether we have inline xattr stored outside, if yes, exit. 922 * 4) Remove the tree. 929 * 4) Remove the tree. 923 */ 930 */ 924 int ocfs2_try_remove_refcount_tree(struct inod 931 int ocfs2_try_remove_refcount_tree(struct inode *inode, 925 struct buff 932 struct buffer_head *di_bh) 926 { 933 { 927 int ret; 934 int ret; 928 struct ocfs2_inode_info *oi = OCFS2_I( 935 struct ocfs2_inode_info *oi = OCFS2_I(inode); 929 struct ocfs2_dinode *di = (struct ocfs 936 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 930 937 931 down_write(&oi->ip_xattr_sem); 938 down_write(&oi->ip_xattr_sem); 932 down_write(&oi->ip_alloc_sem); 939 down_write(&oi->ip_alloc_sem); 933 940 934 if (oi->ip_clusters) 941 if (oi->ip_clusters) 935 goto out; 942 goto out; 936 943 937 if ((oi->ip_dyn_features & OCFS2_HAS_X 944 if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc) 938 goto out; 945 goto out; 939 946 940 if (oi->ip_dyn_features & OCFS2_INLINE 947 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL && 941 ocfs2_has_inline_xattr_value_outsi 948 ocfs2_has_inline_xattr_value_outside(inode, di)) 942 goto out; 949 goto out; 943 950 944 ret = ocfs2_remove_refcount_tree(inode 951 ret = ocfs2_remove_refcount_tree(inode, di_bh); 945 if (ret) 952 if (ret) 946 mlog_errno(ret); 953 mlog_errno(ret); 947 out: 954 out: 948 up_write(&oi->ip_alloc_sem); 955 up_write(&oi->ip_alloc_sem); 949 up_write(&oi->ip_xattr_sem); 956 up_write(&oi->ip_xattr_sem); 950 return 0; 957 return 0; 951 } 958 } 952 959 953 /* 960 /* 954 * Find the end range for a leaf refcount bloc 961 * Find the end range for a leaf refcount block indicated by 955 * el->l_recs[index].e_blkno. 962 * el->l_recs[index].e_blkno. 956 */ 963 */ 957 static int ocfs2_get_refcount_cpos_end(struct 964 static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci, 958 struct 965 struct buffer_head *ref_root_bh, 959 struct 966 struct ocfs2_extent_block *eb, 960 struct 967 struct ocfs2_extent_list *el, 961 int ind 968 int index, u32 *cpos_end) 962 { 969 { 963 int ret, i, subtree_root; 970 int ret, i, subtree_root; 964 u32 cpos; 971 u32 cpos; 965 u64 blkno; 972 u64 blkno; 966 struct super_block *sb = ocfs2_metadat 973 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 967 struct ocfs2_path *left_path = NULL, * 974 struct ocfs2_path *left_path = NULL, *right_path = NULL; 968 struct ocfs2_extent_tree et; 975 struct ocfs2_extent_tree et; 969 struct ocfs2_extent_list *tmp_el; 976 struct ocfs2_extent_list *tmp_el; 970 977 971 if (index < le16_to_cpu(el->l_next_fre 978 if (index < le16_to_cpu(el->l_next_free_rec) - 1) { 972 /* 979 /* 973 * We have a extent rec after 980 * We have a extent rec after index, so just use the e_cpos 974 * of the next extent rec. 981 * of the next extent rec. 975 */ 982 */ 976 *cpos_end = le32_to_cpu(el->l_ 983 *cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos); 977 return 0; 984 return 0; 978 } 985 } 979 986 980 if (!eb || !eb->h_next_leaf_blk) { !! 987 if (!eb || (eb && !eb->h_next_leaf_blk)) { 981 /* 988 /* 982 * We are the last extent rec, 989 * We are the last extent rec, so any high cpos should 983 * be stored in this leaf refc 990 * be stored in this leaf refcount block. 984 */ 991 */ 985 *cpos_end = UINT_MAX; 992 *cpos_end = UINT_MAX; 986 return 0; 993 return 0; 987 } 994 } 988 995 989 /* 996 /* 990 * If the extent block isn't the last 997 * If the extent block isn't the last one, we have to find 991 * the subtree root between this exten 998 * the subtree root between this extent block and the next 992 * leaf extent block and get the corre 999 * leaf extent block and get the corresponding e_cpos from 993 * the subroot. Otherwise we may corru 1000 * the subroot. Otherwise we may corrupt the b-tree. 994 */ 1001 */ 995 ocfs2_init_refcount_extent_tree(&et, c 1002 ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 996 1003 997 left_path = ocfs2_new_path_from_et(&et 1004 left_path = ocfs2_new_path_from_et(&et); 998 if (!left_path) { 1005 if (!left_path) { 999 ret = -ENOMEM; 1006 ret = -ENOMEM; 1000 mlog_errno(ret); 1007 mlog_errno(ret); 1001 goto out; 1008 goto out; 1002 } 1009 } 1003 1010 1004 cpos = le32_to_cpu(eb->h_list.l_recs[ 1011 cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos); 1005 ret = ocfs2_find_path(ci, left_path, 1012 ret = ocfs2_find_path(ci, left_path, cpos); 1006 if (ret) { 1013 if (ret) { 1007 mlog_errno(ret); 1014 mlog_errno(ret); 1008 goto out; 1015 goto out; 1009 } 1016 } 1010 1017 1011 right_path = ocfs2_new_path_from_path 1018 right_path = ocfs2_new_path_from_path(left_path); 1012 if (!right_path) { 1019 if (!right_path) { 1013 ret = -ENOMEM; 1020 ret = -ENOMEM; 1014 mlog_errno(ret); 1021 mlog_errno(ret); 1015 goto out; 1022 goto out; 1016 } 1023 } 1017 1024 1018 ret = ocfs2_find_cpos_for_right_leaf( 1025 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos); 1019 if (ret) { 1026 if (ret) { 1020 mlog_errno(ret); 1027 mlog_errno(ret); 1021 goto out; 1028 goto out; 1022 } 1029 } 1023 1030 1024 ret = ocfs2_find_path(ci, right_path, 1031 ret = ocfs2_find_path(ci, right_path, cpos); 1025 if (ret) { 1032 if (ret) { 1026 mlog_errno(ret); 1033 mlog_errno(ret); 1027 goto out; 1034 goto out; 1028 } 1035 } 1029 1036 1030 subtree_root = ocfs2_find_subtree_roo 1037 subtree_root = ocfs2_find_subtree_root(&et, left_path, 1031 1038 right_path); 1032 1039 1033 tmp_el = left_path->p_node[subtree_ro 1040 tmp_el = left_path->p_node[subtree_root].el; 1034 blkno = left_path->p_node[subtree_roo 1041 blkno = left_path->p_node[subtree_root+1].bh->b_blocknr; 1035 for (i = 0; i < le16_to_cpu(tmp_el->l 1042 for (i = 0; i < le16_to_cpu(tmp_el->l_next_free_rec); i++) { 1036 if (le64_to_cpu(tmp_el->l_rec 1043 if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) { 1037 *cpos_end = le32_to_c 1044 *cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos); 1038 break; 1045 break; 1039 } 1046 } 1040 } 1047 } 1041 1048 1042 BUG_ON(i == le16_to_cpu(tmp_el->l_nex 1049 BUG_ON(i == le16_to_cpu(tmp_el->l_next_free_rec)); 1043 1050 1044 out: 1051 out: 1045 ocfs2_free_path(left_path); 1052 ocfs2_free_path(left_path); 1046 ocfs2_free_path(right_path); 1053 ocfs2_free_path(right_path); 1047 return ret; 1054 return ret; 1048 } 1055 } 1049 1056 1050 /* 1057 /* 1051 * Given a cpos and len, try to find the refc 1058 * Given a cpos and len, try to find the refcount record which contains cpos. 1052 * 1. If cpos can be found in one refcount re 1059 * 1. If cpos can be found in one refcount record, return the record. 1053 * 2. If cpos can't be found, return a fake r 1060 * 2. If cpos can't be found, return a fake record which start from cpos 1054 * and end at a small value between cpos+l 1061 * and end at a small value between cpos+len and start of the next record. 1055 * This fake record has r_refcount = 0. 1062 * This fake record has r_refcount = 0. 1056 */ 1063 */ 1057 static int ocfs2_get_refcount_rec(struct ocfs 1064 static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci, 1058 struct buff 1065 struct buffer_head *ref_root_bh, 1059 u64 cpos, u 1066 u64 cpos, unsigned int len, 1060 struct ocfs 1067 struct ocfs2_refcount_rec *ret_rec, 1061 int *index, 1068 int *index, 1062 struct buff 1069 struct buffer_head **ret_bh) 1063 { 1070 { 1064 int ret = 0, i, found; 1071 int ret = 0, i, found; 1065 u32 low_cpos, cpos_end; !! 1072 u32 low_cpos, uninitialized_var(cpos_end); 1066 struct ocfs2_extent_list *el; 1073 struct ocfs2_extent_list *el; 1067 struct ocfs2_extent_rec *rec = NULL; 1074 struct ocfs2_extent_rec *rec = NULL; 1068 struct ocfs2_extent_block *eb = NULL; 1075 struct ocfs2_extent_block *eb = NULL; 1069 struct buffer_head *eb_bh = NULL, *re 1076 struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL; 1070 struct super_block *sb = ocfs2_metada 1077 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1071 struct ocfs2_refcount_block *rb = 1078 struct ocfs2_refcount_block *rb = 1072 (struct ocfs2_refcoun 1079 (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1073 1080 1074 if (!(le32_to_cpu(rb->rf_flags) & OCF 1081 if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) { 1075 ocfs2_find_refcount_rec_in_rl 1082 ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len, 1076 1083 ret_rec, index); 1077 *ret_bh = ref_root_bh; 1084 *ret_bh = ref_root_bh; 1078 get_bh(ref_root_bh); 1085 get_bh(ref_root_bh); 1079 return 0; 1086 return 0; 1080 } 1087 } 1081 1088 1082 el = &rb->rf_list; 1089 el = &rb->rf_list; 1083 low_cpos = cpos & OCFS2_32BIT_POS_MAS 1090 low_cpos = cpos & OCFS2_32BIT_POS_MASK; 1084 1091 1085 if (el->l_tree_depth) { 1092 if (el->l_tree_depth) { 1086 ret = ocfs2_find_leaf(ci, el, 1093 ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh); 1087 if (ret) { 1094 if (ret) { 1088 mlog_errno(ret); 1095 mlog_errno(ret); 1089 goto out; 1096 goto out; 1090 } 1097 } 1091 1098 1092 eb = (struct ocfs2_extent_blo 1099 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 1093 el = &eb->h_list; 1100 el = &eb->h_list; 1094 1101 1095 if (el->l_tree_depth) { 1102 if (el->l_tree_depth) { 1096 ret = ocfs2_error(sb, 1103 ret = ocfs2_error(sb, 1097 "re 1104 "refcount tree %llu has non zero tree depth in leaf btree tree block %llu\n", 1098 (un 1105 (unsigned long long)ocfs2_metadata_cache_owner(ci), 1099 (un 1106 (unsigned long long)eb_bh->b_blocknr); 1100 goto out; 1107 goto out; 1101 } 1108 } 1102 } 1109 } 1103 1110 1104 found = 0; 1111 found = 0; 1105 for (i = le16_to_cpu(el->l_next_free_ 1112 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { 1106 rec = &el->l_recs[i]; 1113 rec = &el->l_recs[i]; 1107 1114 1108 if (le32_to_cpu(rec->e_cpos) 1115 if (le32_to_cpu(rec->e_cpos) <= low_cpos) { 1109 found = 1; 1116 found = 1; 1110 break; 1117 break; 1111 } 1118 } 1112 } 1119 } 1113 1120 1114 if (found) { 1121 if (found) { 1115 ret = ocfs2_get_refcount_cpos 1122 ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh, 1116 1123 eb, el, i, &cpos_end); 1117 if (ret) { 1124 if (ret) { 1118 mlog_errno(ret); 1125 mlog_errno(ret); 1119 goto out; 1126 goto out; 1120 } 1127 } 1121 1128 1122 if (cpos_end < low_cpos + len 1129 if (cpos_end < low_cpos + len) 1123 len = cpos_end - low_ 1130 len = cpos_end - low_cpos; 1124 } 1131 } 1125 1132 1126 ret = ocfs2_read_refcount_block(ci, l 1133 ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno), 1127 &ref_ 1134 &ref_leaf_bh); 1128 if (ret) { 1135 if (ret) { 1129 mlog_errno(ret); 1136 mlog_errno(ret); 1130 goto out; 1137 goto out; 1131 } 1138 } 1132 1139 1133 ocfs2_find_refcount_rec_in_rl(ci, ref 1140 ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len, 1134 ret_rec 1141 ret_rec, index); 1135 *ret_bh = ref_leaf_bh; 1142 *ret_bh = ref_leaf_bh; 1136 out: 1143 out: 1137 brelse(eb_bh); 1144 brelse(eb_bh); 1138 return ret; 1145 return ret; 1139 } 1146 } 1140 1147 1141 enum ocfs2_ref_rec_contig { 1148 enum ocfs2_ref_rec_contig { 1142 REF_CONTIG_NONE = 0, 1149 REF_CONTIG_NONE = 0, 1143 REF_CONTIG_LEFT, 1150 REF_CONTIG_LEFT, 1144 REF_CONTIG_RIGHT, 1151 REF_CONTIG_RIGHT, 1145 REF_CONTIG_LEFTRIGHT, 1152 REF_CONTIG_LEFTRIGHT, 1146 }; 1153 }; 1147 1154 1148 static enum ocfs2_ref_rec_contig 1155 static enum ocfs2_ref_rec_contig 1149 ocfs2_refcount_rec_adjacent(struct oc 1156 ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb, 1150 int index 1157 int index) 1151 { 1158 { 1152 if ((rb->rf_records.rl_recs[index].r_ 1159 if ((rb->rf_records.rl_recs[index].r_refcount == 1153 rb->rf_records.rl_recs[index + 1] 1160 rb->rf_records.rl_recs[index + 1].r_refcount) && 1154 (le64_to_cpu(rb->rf_records.rl_re 1161 (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) + 1155 le32_to_cpu(rb->rf_records.rl_rec 1162 le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) == 1156 le64_to_cpu(rb->rf_records.rl_rec 1163 le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos))) 1157 return REF_CONTIG_RIGHT; 1164 return REF_CONTIG_RIGHT; 1158 1165 1159 return REF_CONTIG_NONE; 1166 return REF_CONTIG_NONE; 1160 } 1167 } 1161 1168 1162 static enum ocfs2_ref_rec_contig 1169 static enum ocfs2_ref_rec_contig 1163 ocfs2_refcount_rec_contig(struct ocfs 1170 ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb, 1164 int index) 1171 int index) 1165 { 1172 { 1166 enum ocfs2_ref_rec_contig ret = REF_C 1173 enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE; 1167 1174 1168 if (index < le16_to_cpu(rb->rf_record 1175 if (index < le16_to_cpu(rb->rf_records.rl_used) - 1) 1169 ret = ocfs2_refcount_rec_adja 1176 ret = ocfs2_refcount_rec_adjacent(rb, index); 1170 1177 1171 if (index > 0) { 1178 if (index > 0) { 1172 enum ocfs2_ref_rec_contig tmp 1179 enum ocfs2_ref_rec_contig tmp; 1173 1180 1174 tmp = ocfs2_refcount_rec_adja 1181 tmp = ocfs2_refcount_rec_adjacent(rb, index - 1); 1175 1182 1176 if (tmp == REF_CONTIG_RIGHT) 1183 if (tmp == REF_CONTIG_RIGHT) { 1177 if (ret == REF_CONTIG 1184 if (ret == REF_CONTIG_RIGHT) 1178 ret = REF_CON 1185 ret = REF_CONTIG_LEFTRIGHT; 1179 else 1186 else 1180 ret = REF_CON 1187 ret = REF_CONTIG_LEFT; 1181 } 1188 } 1182 } 1189 } 1183 1190 1184 return ret; 1191 return ret; 1185 } 1192 } 1186 1193 1187 static void ocfs2_rotate_refcount_rec_left(st 1194 static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb, 1188 in 1195 int index) 1189 { 1196 { 1190 BUG_ON(rb->rf_records.rl_recs[index]. 1197 BUG_ON(rb->rf_records.rl_recs[index].r_refcount != 1191 rb->rf_records.rl_recs[index+1 1198 rb->rf_records.rl_recs[index+1].r_refcount); 1192 1199 1193 le32_add_cpu(&rb->rf_records.rl_recs[ 1200 le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters, 1194 le32_to_cpu(rb->rf_recor 1201 le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters)); 1195 1202 1196 if (index < le16_to_cpu(rb->rf_record 1203 if (index < le16_to_cpu(rb->rf_records.rl_used) - 2) 1197 memmove(&rb->rf_records.rl_re 1204 memmove(&rb->rf_records.rl_recs[index + 1], 1198 &rb->rf_records.rl_re 1205 &rb->rf_records.rl_recs[index + 2], 1199 sizeof(struct ocfs2_r 1206 sizeof(struct ocfs2_refcount_rec) * 1200 (le16_to_cpu(rb->rf_r 1207 (le16_to_cpu(rb->rf_records.rl_used) - index - 2)); 1201 1208 1202 memset(&rb->rf_records.rl_recs[le16_t 1209 memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1], 1203 0, sizeof(struct ocfs2_refcoun 1210 0, sizeof(struct ocfs2_refcount_rec)); 1204 le16_add_cpu(&rb->rf_records.rl_used, 1211 le16_add_cpu(&rb->rf_records.rl_used, -1); 1205 } 1212 } 1206 1213 1207 /* 1214 /* 1208 * Merge the refcount rec if we are contiguou 1215 * Merge the refcount rec if we are contiguous with the adjacent recs. 1209 */ 1216 */ 1210 static void ocfs2_refcount_rec_merge(struct o 1217 static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb, 1211 int inde 1218 int index) 1212 { 1219 { 1213 enum ocfs2_ref_rec_contig contig = 1220 enum ocfs2_ref_rec_contig contig = 1214 ocfs2_refcoun 1221 ocfs2_refcount_rec_contig(rb, index); 1215 1222 1216 if (contig == REF_CONTIG_NONE) 1223 if (contig == REF_CONTIG_NONE) 1217 return; 1224 return; 1218 1225 1219 if (contig == REF_CONTIG_LEFT || cont 1226 if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) { 1220 BUG_ON(index == 0); 1227 BUG_ON(index == 0); 1221 index--; 1228 index--; 1222 } 1229 } 1223 1230 1224 ocfs2_rotate_refcount_rec_left(rb, in 1231 ocfs2_rotate_refcount_rec_left(rb, index); 1225 1232 1226 if (contig == REF_CONTIG_LEFTRIGHT) 1233 if (contig == REF_CONTIG_LEFTRIGHT) 1227 ocfs2_rotate_refcount_rec_lef 1234 ocfs2_rotate_refcount_rec_left(rb, index); 1228 } 1235 } 1229 1236 1230 /* 1237 /* 1231 * Change the refcount indexed by "index" in 1238 * Change the refcount indexed by "index" in ref_bh. 1232 * If refcount reaches 0, remove it. 1239 * If refcount reaches 0, remove it. 1233 */ 1240 */ 1234 static int ocfs2_change_refcount_rec(handle_t 1241 static int ocfs2_change_refcount_rec(handle_t *handle, 1235 struct o 1242 struct ocfs2_caching_info *ci, 1236 struct b 1243 struct buffer_head *ref_leaf_bh, 1237 int inde 1244 int index, int merge, int change) 1238 { 1245 { 1239 int ret; 1246 int ret; 1240 struct ocfs2_refcount_block *rb = 1247 struct ocfs2_refcount_block *rb = 1241 (struct ocfs2_refcoun 1248 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1242 struct ocfs2_refcount_list *rl = &rb- 1249 struct ocfs2_refcount_list *rl = &rb->rf_records; 1243 struct ocfs2_refcount_rec *rec = &rl- 1250 struct ocfs2_refcount_rec *rec = &rl->rl_recs[index]; 1244 1251 1245 ret = ocfs2_journal_access_rb(handle, 1252 ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1246 OCFS2_J 1253 OCFS2_JOURNAL_ACCESS_WRITE); 1247 if (ret) { 1254 if (ret) { 1248 mlog_errno(ret); 1255 mlog_errno(ret); 1249 goto out; 1256 goto out; 1250 } 1257 } 1251 1258 1252 trace_ocfs2_change_refcount_rec( 1259 trace_ocfs2_change_refcount_rec( 1253 (unsigned long long)ocfs2_met 1260 (unsigned long long)ocfs2_metadata_cache_owner(ci), 1254 index, le32_to_cpu(rec->r_ref 1261 index, le32_to_cpu(rec->r_refcount), change); 1255 le32_add_cpu(&rec->r_refcount, change 1262 le32_add_cpu(&rec->r_refcount, change); 1256 1263 1257 if (!rec->r_refcount) { 1264 if (!rec->r_refcount) { 1258 if (index != le16_to_cpu(rl-> 1265 if (index != le16_to_cpu(rl->rl_used) - 1) { 1259 memmove(rec, rec + 1, 1266 memmove(rec, rec + 1, 1260 (le16_to_cpu( 1267 (le16_to_cpu(rl->rl_used) - index - 1) * 1261 sizeof(struct 1268 sizeof(struct ocfs2_refcount_rec)); 1262 memset(&rl->rl_recs[l 1269 memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1], 1263 0, sizeof(stru 1270 0, sizeof(struct ocfs2_refcount_rec)); 1264 } 1271 } 1265 1272 1266 le16_add_cpu(&rl->rl_used, -1 1273 le16_add_cpu(&rl->rl_used, -1); 1267 } else if (merge) 1274 } else if (merge) 1268 ocfs2_refcount_rec_merge(rb, 1275 ocfs2_refcount_rec_merge(rb, index); 1269 1276 1270 ocfs2_journal_dirty(handle, ref_leaf_ 1277 ocfs2_journal_dirty(handle, ref_leaf_bh); 1271 out: 1278 out: 1272 return ret; 1279 return ret; 1273 } 1280 } 1274 1281 1275 static int ocfs2_expand_inline_ref_root(handl 1282 static int ocfs2_expand_inline_ref_root(handle_t *handle, 1276 struc 1283 struct ocfs2_caching_info *ci, 1277 struc 1284 struct buffer_head *ref_root_bh, 1278 struc 1285 struct buffer_head **ref_leaf_bh, 1279 struc 1286 struct ocfs2_alloc_context *meta_ac) 1280 { 1287 { 1281 int ret; 1288 int ret; 1282 u16 suballoc_bit_start; 1289 u16 suballoc_bit_start; 1283 u32 num_got; 1290 u32 num_got; 1284 u64 suballoc_loc, blkno; 1291 u64 suballoc_loc, blkno; 1285 struct super_block *sb = ocfs2_metada 1292 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1286 struct buffer_head *new_bh = NULL; 1293 struct buffer_head *new_bh = NULL; 1287 struct ocfs2_refcount_block *new_rb; 1294 struct ocfs2_refcount_block *new_rb; 1288 struct ocfs2_refcount_block *root_rb 1295 struct ocfs2_refcount_block *root_rb = 1289 (struct ocfs2_refcoun 1296 (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1290 1297 1291 ret = ocfs2_journal_access_rb(handle, 1298 ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh, 1292 OCFS2_J 1299 OCFS2_JOURNAL_ACCESS_WRITE); 1293 if (ret) { 1300 if (ret) { 1294 mlog_errno(ret); 1301 mlog_errno(ret); 1295 goto out; 1302 goto out; 1296 } 1303 } 1297 1304 1298 ret = ocfs2_claim_metadata(handle, me 1305 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 1299 &suballoc_ 1306 &suballoc_bit_start, &num_got, 1300 &blkno); 1307 &blkno); 1301 if (ret) { 1308 if (ret) { 1302 mlog_errno(ret); 1309 mlog_errno(ret); 1303 goto out; 1310 goto out; 1304 } 1311 } 1305 1312 1306 new_bh = sb_getblk(sb, blkno); 1313 new_bh = sb_getblk(sb, blkno); 1307 if (new_bh == NULL) { 1314 if (new_bh == NULL) { 1308 ret = -ENOMEM; 1315 ret = -ENOMEM; 1309 mlog_errno(ret); 1316 mlog_errno(ret); 1310 goto out; 1317 goto out; 1311 } 1318 } 1312 ocfs2_set_new_buffer_uptodate(ci, new 1319 ocfs2_set_new_buffer_uptodate(ci, new_bh); 1313 1320 1314 ret = ocfs2_journal_access_rb(handle, 1321 ret = ocfs2_journal_access_rb(handle, ci, new_bh, 1315 OCFS2_J 1322 OCFS2_JOURNAL_ACCESS_CREATE); 1316 if (ret) { 1323 if (ret) { 1317 mlog_errno(ret); 1324 mlog_errno(ret); 1318 goto out; 1325 goto out; 1319 } 1326 } 1320 1327 1321 /* 1328 /* 1322 * Initialize ocfs2_refcount_block. 1329 * Initialize ocfs2_refcount_block. 1323 * It should contain the same informa 1330 * It should contain the same information as the old root. 1324 * so just memcpy it and change the c 1331 * so just memcpy it and change the corresponding field. 1325 */ 1332 */ 1326 memcpy(new_bh->b_data, ref_root_bh->b 1333 memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize); 1327 1334 1328 new_rb = (struct ocfs2_refcount_block 1335 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1329 new_rb->rf_suballoc_slot = cpu_to_le1 1336 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 1330 new_rb->rf_suballoc_loc = cpu_to_le64 1337 new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc); 1331 new_rb->rf_suballoc_bit = cpu_to_le16 1338 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1332 new_rb->rf_blkno = cpu_to_le64(blkno) 1339 new_rb->rf_blkno = cpu_to_le64(blkno); 1333 new_rb->rf_cpos = cpu_to_le32(0); 1340 new_rb->rf_cpos = cpu_to_le32(0); 1334 new_rb->rf_parent = cpu_to_le64(ref_r 1341 new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr); 1335 new_rb->rf_flags = cpu_to_le32(OCFS2_ 1342 new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL); 1336 ocfs2_journal_dirty(handle, new_bh); 1343 ocfs2_journal_dirty(handle, new_bh); 1337 1344 1338 /* Now change the root. */ 1345 /* Now change the root. */ 1339 memset(&root_rb->rf_list, 0, sb->s_bl 1346 memset(&root_rb->rf_list, 0, sb->s_blocksize - 1340 offsetof(struct ocfs2_refcount 1347 offsetof(struct ocfs2_refcount_block, rf_list)); 1341 root_rb->rf_list.l_count = cpu_to_le1 1348 root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb)); 1342 root_rb->rf_clusters = cpu_to_le32(1) 1349 root_rb->rf_clusters = cpu_to_le32(1); 1343 root_rb->rf_list.l_next_free_rec = cp 1350 root_rb->rf_list.l_next_free_rec = cpu_to_le16(1); 1344 root_rb->rf_list.l_recs[0].e_blkno = 1351 root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno); 1345 root_rb->rf_list.l_recs[0].e_leaf_clu 1352 root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); 1346 root_rb->rf_flags = cpu_to_le32(OCFS2 1353 root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL); 1347 1354 1348 ocfs2_journal_dirty(handle, ref_root_ 1355 ocfs2_journal_dirty(handle, ref_root_bh); 1349 1356 1350 trace_ocfs2_expand_inline_ref_root((u 1357 trace_ocfs2_expand_inline_ref_root((unsigned long long)blkno, 1351 le16_to_cpu(new_rb->rf_record 1358 le16_to_cpu(new_rb->rf_records.rl_used)); 1352 1359 1353 *ref_leaf_bh = new_bh; 1360 *ref_leaf_bh = new_bh; 1354 new_bh = NULL; 1361 new_bh = NULL; 1355 out: 1362 out: 1356 brelse(new_bh); 1363 brelse(new_bh); 1357 return ret; 1364 return ret; 1358 } 1365 } 1359 1366 1360 static int ocfs2_refcount_rec_no_intersect(st 1367 static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev, 1361 st 1368 struct ocfs2_refcount_rec *next) 1362 { 1369 { 1363 if (ocfs2_get_ref_rec_low_cpos(prev) 1370 if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <= 1364 ocfs2_get_ref_rec_low_cpos(ne 1371 ocfs2_get_ref_rec_low_cpos(next)) 1365 return 1; 1372 return 1; 1366 1373 1367 return 0; 1374 return 0; 1368 } 1375 } 1369 1376 1370 static int cmp_refcount_rec_by_low_cpos(const 1377 static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b) 1371 { 1378 { 1372 const struct ocfs2_refcount_rec *l = 1379 const struct ocfs2_refcount_rec *l = a, *r = b; 1373 u32 l_cpos = ocfs2_get_ref_rec_low_cp 1380 u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l); 1374 u32 r_cpos = ocfs2_get_ref_rec_low_cp 1381 u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r); 1375 1382 1376 if (l_cpos > r_cpos) 1383 if (l_cpos > r_cpos) 1377 return 1; 1384 return 1; 1378 if (l_cpos < r_cpos) 1385 if (l_cpos < r_cpos) 1379 return -1; 1386 return -1; 1380 return 0; 1387 return 0; 1381 } 1388 } 1382 1389 1383 static int cmp_refcount_rec_by_cpos(const voi 1390 static int cmp_refcount_rec_by_cpos(const void *a, const void *b) 1384 { 1391 { 1385 const struct ocfs2_refcount_rec *l = 1392 const struct ocfs2_refcount_rec *l = a, *r = b; 1386 u64 l_cpos = le64_to_cpu(l->r_cpos); 1393 u64 l_cpos = le64_to_cpu(l->r_cpos); 1387 u64 r_cpos = le64_to_cpu(r->r_cpos); 1394 u64 r_cpos = le64_to_cpu(r->r_cpos); 1388 1395 1389 if (l_cpos > r_cpos) 1396 if (l_cpos > r_cpos) 1390 return 1; 1397 return 1; 1391 if (l_cpos < r_cpos) 1398 if (l_cpos < r_cpos) 1392 return -1; 1399 return -1; 1393 return 0; 1400 return 0; 1394 } 1401 } 1395 1402 >> 1403 static void swap_refcount_rec(void *a, void *b, int size) >> 1404 { >> 1405 struct ocfs2_refcount_rec *l = a, *r = b; >> 1406 >> 1407 swap(*l, *r); >> 1408 } >> 1409 1396 /* 1410 /* 1397 * The refcount cpos are ordered by their 64b 1411 * The refcount cpos are ordered by their 64bit cpos, 1398 * But we will use the low 32 bit to be the e 1412 * But we will use the low 32 bit to be the e_cpos in the b-tree. 1399 * So we need to make sure that this pos isn' 1413 * So we need to make sure that this pos isn't intersected with others. 1400 * 1414 * 1401 * Note: The refcount block is already sorted 1415 * Note: The refcount block is already sorted by their low 32 bit cpos, 1402 * So just try the middle pos first, an 1416 * So just try the middle pos first, and we will exit when we find 1403 * the good position. 1417 * the good position. 1404 */ 1418 */ 1405 static int ocfs2_find_refcount_split_pos(stru 1419 static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl, 1406 u32 1420 u32 *split_pos, int *split_index) 1407 { 1421 { 1408 int num_used = le16_to_cpu(rl->rl_use 1422 int num_used = le16_to_cpu(rl->rl_used); 1409 int delta, middle = num_used / 2; 1423 int delta, middle = num_used / 2; 1410 1424 1411 for (delta = 0; delta < middle; delta 1425 for (delta = 0; delta < middle; delta++) { 1412 /* Let's check delta earlier 1426 /* Let's check delta earlier than middle */ 1413 if (ocfs2_refcount_rec_no_int 1427 if (ocfs2_refcount_rec_no_intersect( 1414 &rl-> 1428 &rl->rl_recs[middle - delta - 1], 1415 &rl-> 1429 &rl->rl_recs[middle - delta])) { 1416 *split_index = middle 1430 *split_index = middle - delta; 1417 break; 1431 break; 1418 } 1432 } 1419 1433 1420 /* For even counts, don't wal 1434 /* For even counts, don't walk off the end */ 1421 if ((middle + delta + 1) == n 1435 if ((middle + delta + 1) == num_used) 1422 continue; 1436 continue; 1423 1437 1424 /* Now try delta past middle 1438 /* Now try delta past middle */ 1425 if (ocfs2_refcount_rec_no_int 1439 if (ocfs2_refcount_rec_no_intersect( 1426 &rl-> 1440 &rl->rl_recs[middle + delta], 1427 &rl-> 1441 &rl->rl_recs[middle + delta + 1])) { 1428 *split_index = middle 1442 *split_index = middle + delta + 1; 1429 break; 1443 break; 1430 } 1444 } 1431 } 1445 } 1432 1446 1433 if (delta >= middle) 1447 if (delta >= middle) 1434 return -ENOSPC; 1448 return -ENOSPC; 1435 1449 1436 *split_pos = ocfs2_get_ref_rec_low_cp 1450 *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]); 1437 return 0; 1451 return 0; 1438 } 1452 } 1439 1453 1440 static int ocfs2_divide_leaf_refcount_block(s 1454 static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh, 1441 s 1455 struct buffer_head *new_bh, 1442 u 1456 u32 *split_cpos) 1443 { 1457 { 1444 int split_index = 0, num_moved, ret; 1458 int split_index = 0, num_moved, ret; 1445 u32 cpos = 0; 1459 u32 cpos = 0; 1446 struct ocfs2_refcount_block *rb = 1460 struct ocfs2_refcount_block *rb = 1447 (struct ocfs2_refcoun 1461 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1448 struct ocfs2_refcount_list *rl = &rb- 1462 struct ocfs2_refcount_list *rl = &rb->rf_records; 1449 struct ocfs2_refcount_block *new_rb = 1463 struct ocfs2_refcount_block *new_rb = 1450 (struct ocfs2_refcoun 1464 (struct ocfs2_refcount_block *)new_bh->b_data; 1451 struct ocfs2_refcount_list *new_rl = 1465 struct ocfs2_refcount_list *new_rl = &new_rb->rf_records; 1452 1466 1453 trace_ocfs2_divide_leaf_refcount_bloc 1467 trace_ocfs2_divide_leaf_refcount_block( 1454 (unsigned long long)ref_leaf_ 1468 (unsigned long long)ref_leaf_bh->b_blocknr, 1455 le16_to_cpu(rl->rl_count), le 1469 le16_to_cpu(rl->rl_count), le16_to_cpu(rl->rl_used)); 1456 1470 1457 /* 1471 /* 1458 * XXX: Improvement later. 1472 * XXX: Improvement later. 1459 * If we know all the high 32 bit cpo 1473 * If we know all the high 32 bit cpos is the same, no need to sort. 1460 * 1474 * 1461 * In order to make the whole process 1475 * In order to make the whole process safe, we do: 1462 * 1. sort the entries by their low 3 1476 * 1. sort the entries by their low 32 bit cpos first so that we can 1463 * find the split cpos easily. 1477 * find the split cpos easily. 1464 * 2. call ocfs2_insert_extent to ins 1478 * 2. call ocfs2_insert_extent to insert the new refcount block. 1465 * 3. move the refcount rec to the ne 1479 * 3. move the refcount rec to the new block. 1466 * 4. sort the entries by their 64 bi 1480 * 4. sort the entries by their 64 bit cpos. 1467 * 5. dirty the new_rb and rb. 1481 * 5. dirty the new_rb and rb. 1468 */ 1482 */ 1469 sort(&rl->rl_recs, le16_to_cpu(rl->rl 1483 sort(&rl->rl_recs, le16_to_cpu(rl->rl_used), 1470 sizeof(struct ocfs2_refcount_rec 1484 sizeof(struct ocfs2_refcount_rec), 1471 cmp_refcount_rec_by_low_cpos, NU !! 1485 cmp_refcount_rec_by_low_cpos, swap_refcount_rec); 1472 1486 1473 ret = ocfs2_find_refcount_split_pos(r 1487 ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index); 1474 if (ret) { 1488 if (ret) { 1475 mlog_errno(ret); 1489 mlog_errno(ret); 1476 return ret; 1490 return ret; 1477 } 1491 } 1478 1492 1479 new_rb->rf_cpos = cpu_to_le32(cpos); 1493 new_rb->rf_cpos = cpu_to_le32(cpos); 1480 1494 1481 /* move refcount records starting fro 1495 /* move refcount records starting from split_index to the new block. */ 1482 num_moved = le16_to_cpu(rl->rl_used) 1496 num_moved = le16_to_cpu(rl->rl_used) - split_index; 1483 memcpy(new_rl->rl_recs, &rl->rl_recs[ 1497 memcpy(new_rl->rl_recs, &rl->rl_recs[split_index], 1484 num_moved * sizeof(struct ocfs 1498 num_moved * sizeof(struct ocfs2_refcount_rec)); 1485 1499 1486 /*ok, remove the entries we just move 1500 /*ok, remove the entries we just moved over to the other block. */ 1487 memset(&rl->rl_recs[split_index], 0, 1501 memset(&rl->rl_recs[split_index], 0, 1488 num_moved * sizeof(struct ocfs 1502 num_moved * sizeof(struct ocfs2_refcount_rec)); 1489 1503 1490 /* change old and new rl_used accordi 1504 /* change old and new rl_used accordingly. */ 1491 le16_add_cpu(&rl->rl_used, -num_moved 1505 le16_add_cpu(&rl->rl_used, -num_moved); 1492 new_rl->rl_used = cpu_to_le16(num_mov 1506 new_rl->rl_used = cpu_to_le16(num_moved); 1493 1507 1494 sort(&rl->rl_recs, le16_to_cpu(rl->rl 1508 sort(&rl->rl_recs, le16_to_cpu(rl->rl_used), 1495 sizeof(struct ocfs2_refcount_rec 1509 sizeof(struct ocfs2_refcount_rec), 1496 cmp_refcount_rec_by_cpos, NULL); !! 1510 cmp_refcount_rec_by_cpos, swap_refcount_rec); 1497 1511 1498 sort(&new_rl->rl_recs, le16_to_cpu(ne 1512 sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used), 1499 sizeof(struct ocfs2_refcount_rec 1513 sizeof(struct ocfs2_refcount_rec), 1500 cmp_refcount_rec_by_cpos, NULL); !! 1514 cmp_refcount_rec_by_cpos, swap_refcount_rec); 1501 1515 1502 *split_cpos = cpos; 1516 *split_cpos = cpos; 1503 return 0; 1517 return 0; 1504 } 1518 } 1505 1519 1506 static int ocfs2_new_leaf_refcount_block(hand 1520 static int ocfs2_new_leaf_refcount_block(handle_t *handle, 1507 stru 1521 struct ocfs2_caching_info *ci, 1508 stru 1522 struct buffer_head *ref_root_bh, 1509 stru 1523 struct buffer_head *ref_leaf_bh, 1510 stru 1524 struct ocfs2_alloc_context *meta_ac) 1511 { 1525 { 1512 int ret; 1526 int ret; 1513 u16 suballoc_bit_start; 1527 u16 suballoc_bit_start; 1514 u32 num_got, new_cpos; 1528 u32 num_got, new_cpos; 1515 u64 suballoc_loc, blkno; 1529 u64 suballoc_loc, blkno; 1516 struct super_block *sb = ocfs2_metada 1530 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1517 struct ocfs2_refcount_block *root_rb 1531 struct ocfs2_refcount_block *root_rb = 1518 (struct ocfs2_refcoun 1532 (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1519 struct buffer_head *new_bh = NULL; 1533 struct buffer_head *new_bh = NULL; 1520 struct ocfs2_refcount_block *new_rb; 1534 struct ocfs2_refcount_block *new_rb; 1521 struct ocfs2_extent_tree ref_et; 1535 struct ocfs2_extent_tree ref_et; 1522 1536 1523 BUG_ON(!(le32_to_cpu(root_rb->rf_flag 1537 BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)); 1524 1538 1525 ret = ocfs2_journal_access_rb(handle, 1539 ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh, 1526 OCFS2_J 1540 OCFS2_JOURNAL_ACCESS_WRITE); 1527 if (ret) { 1541 if (ret) { 1528 mlog_errno(ret); 1542 mlog_errno(ret); 1529 goto out; 1543 goto out; 1530 } 1544 } 1531 1545 1532 ret = ocfs2_journal_access_rb(handle, 1546 ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1533 OCFS2_J 1547 OCFS2_JOURNAL_ACCESS_WRITE); 1534 if (ret) { 1548 if (ret) { 1535 mlog_errno(ret); 1549 mlog_errno(ret); 1536 goto out; 1550 goto out; 1537 } 1551 } 1538 1552 1539 ret = ocfs2_claim_metadata(handle, me 1553 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 1540 &suballoc_ 1554 &suballoc_bit_start, &num_got, 1541 &blkno); 1555 &blkno); 1542 if (ret) { 1556 if (ret) { 1543 mlog_errno(ret); 1557 mlog_errno(ret); 1544 goto out; 1558 goto out; 1545 } 1559 } 1546 1560 1547 new_bh = sb_getblk(sb, blkno); 1561 new_bh = sb_getblk(sb, blkno); 1548 if (new_bh == NULL) { 1562 if (new_bh == NULL) { 1549 ret = -ENOMEM; 1563 ret = -ENOMEM; 1550 mlog_errno(ret); 1564 mlog_errno(ret); 1551 goto out; 1565 goto out; 1552 } 1566 } 1553 ocfs2_set_new_buffer_uptodate(ci, new 1567 ocfs2_set_new_buffer_uptodate(ci, new_bh); 1554 1568 1555 ret = ocfs2_journal_access_rb(handle, 1569 ret = ocfs2_journal_access_rb(handle, ci, new_bh, 1556 OCFS2_J 1570 OCFS2_JOURNAL_ACCESS_CREATE); 1557 if (ret) { 1571 if (ret) { 1558 mlog_errno(ret); 1572 mlog_errno(ret); 1559 goto out; 1573 goto out; 1560 } 1574 } 1561 1575 1562 /* Initialize ocfs2_refcount_block. * 1576 /* Initialize ocfs2_refcount_block. */ 1563 new_rb = (struct ocfs2_refcount_block 1577 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1564 memset(new_rb, 0, sb->s_blocksize); 1578 memset(new_rb, 0, sb->s_blocksize); 1565 strcpy((void *)new_rb, OCFS2_REFCOUNT 1579 strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 1566 new_rb->rf_suballoc_slot = cpu_to_le1 1580 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 1567 new_rb->rf_suballoc_loc = cpu_to_le64 1581 new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc); 1568 new_rb->rf_suballoc_bit = cpu_to_le16 1582 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1569 new_rb->rf_fs_generation = cpu_to_le3 1583 new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 1570 new_rb->rf_blkno = cpu_to_le64(blkno) 1584 new_rb->rf_blkno = cpu_to_le64(blkno); 1571 new_rb->rf_parent = cpu_to_le64(ref_r 1585 new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr); 1572 new_rb->rf_flags = cpu_to_le32(OCFS2_ 1586 new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL); 1573 new_rb->rf_records.rl_count = 1587 new_rb->rf_records.rl_count = 1574 cpu_to_le16(o 1588 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb)); 1575 new_rb->rf_generation = root_rb->rf_g 1589 new_rb->rf_generation = root_rb->rf_generation; 1576 1590 1577 ret = ocfs2_divide_leaf_refcount_bloc 1591 ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos); 1578 if (ret) { 1592 if (ret) { 1579 mlog_errno(ret); 1593 mlog_errno(ret); 1580 goto out; 1594 goto out; 1581 } 1595 } 1582 1596 1583 ocfs2_journal_dirty(handle, ref_leaf_ 1597 ocfs2_journal_dirty(handle, ref_leaf_bh); 1584 ocfs2_journal_dirty(handle, new_bh); 1598 ocfs2_journal_dirty(handle, new_bh); 1585 1599 1586 ocfs2_init_refcount_extent_tree(&ref_ 1600 ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh); 1587 1601 1588 trace_ocfs2_new_leaf_refcount_block( 1602 trace_ocfs2_new_leaf_refcount_block( 1589 (unsigned long long)n 1603 (unsigned long long)new_bh->b_blocknr, new_cpos); 1590 1604 1591 /* Insert the new leaf block with the 1605 /* Insert the new leaf block with the specific offset cpos. */ 1592 ret = ocfs2_insert_extent(handle, &re 1606 ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr, 1593 1, 0, meta_ 1607 1, 0, meta_ac); 1594 if (ret) 1608 if (ret) 1595 mlog_errno(ret); 1609 mlog_errno(ret); 1596 1610 1597 out: 1611 out: 1598 brelse(new_bh); 1612 brelse(new_bh); 1599 return ret; 1613 return ret; 1600 } 1614 } 1601 1615 1602 static int ocfs2_expand_refcount_tree(handle_ 1616 static int ocfs2_expand_refcount_tree(handle_t *handle, 1603 struct 1617 struct ocfs2_caching_info *ci, 1604 struct 1618 struct buffer_head *ref_root_bh, 1605 struct 1619 struct buffer_head *ref_leaf_bh, 1606 struct 1620 struct ocfs2_alloc_context *meta_ac) 1607 { 1621 { 1608 int ret; 1622 int ret; 1609 struct buffer_head *expand_bh = NULL; 1623 struct buffer_head *expand_bh = NULL; 1610 1624 1611 if (ref_root_bh == ref_leaf_bh) { 1625 if (ref_root_bh == ref_leaf_bh) { 1612 /* 1626 /* 1613 * the old root bh hasn't bee 1627 * the old root bh hasn't been expanded to a b-tree, 1614 * so expand it first. 1628 * so expand it first. 1615 */ 1629 */ 1616 ret = ocfs2_expand_inline_ref 1630 ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh, 1617 1631 &expand_bh, meta_ac); 1618 if (ret) { 1632 if (ret) { 1619 mlog_errno(ret); 1633 mlog_errno(ret); 1620 goto out; 1634 goto out; 1621 } 1635 } 1622 } else { 1636 } else { 1623 expand_bh = ref_leaf_bh; 1637 expand_bh = ref_leaf_bh; 1624 get_bh(expand_bh); 1638 get_bh(expand_bh); 1625 } 1639 } 1626 1640 1627 1641 1628 /* Now add a new refcount block into 1642 /* Now add a new refcount block into the tree.*/ 1629 ret = ocfs2_new_leaf_refcount_block(h 1643 ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh, 1630 e 1644 expand_bh, meta_ac); 1631 if (ret) 1645 if (ret) 1632 mlog_errno(ret); 1646 mlog_errno(ret); 1633 out: 1647 out: 1634 brelse(expand_bh); 1648 brelse(expand_bh); 1635 return ret; 1649 return ret; 1636 } 1650 } 1637 1651 1638 /* 1652 /* 1639 * Adjust the extent rec in b-tree representi 1653 * Adjust the extent rec in b-tree representing ref_leaf_bh. 1640 * 1654 * 1641 * Only called when we have inserted a new re 1655 * Only called when we have inserted a new refcount rec at index 0 1642 * which means ocfs2_extent_rec.e_cpos may ne 1656 * which means ocfs2_extent_rec.e_cpos may need some change. 1643 */ 1657 */ 1644 static int ocfs2_adjust_refcount_rec(handle_t 1658 static int ocfs2_adjust_refcount_rec(handle_t *handle, 1645 struct o 1659 struct ocfs2_caching_info *ci, 1646 struct b 1660 struct buffer_head *ref_root_bh, 1647 struct b 1661 struct buffer_head *ref_leaf_bh, 1648 struct o 1662 struct ocfs2_refcount_rec *rec) 1649 { 1663 { 1650 int ret = 0, i; 1664 int ret = 0, i; 1651 u32 new_cpos, old_cpos; 1665 u32 new_cpos, old_cpos; 1652 struct ocfs2_path *path = NULL; 1666 struct ocfs2_path *path = NULL; 1653 struct ocfs2_extent_tree et; 1667 struct ocfs2_extent_tree et; 1654 struct ocfs2_refcount_block *rb = 1668 struct ocfs2_refcount_block *rb = 1655 (struct ocfs2_refcount_block 1669 (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1656 struct ocfs2_extent_list *el; 1670 struct ocfs2_extent_list *el; 1657 1671 1658 if (!(le32_to_cpu(rb->rf_flags) & OCF 1672 if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) 1659 goto out; 1673 goto out; 1660 1674 1661 rb = (struct ocfs2_refcount_block *)r 1675 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1662 old_cpos = le32_to_cpu(rb->rf_cpos); 1676 old_cpos = le32_to_cpu(rb->rf_cpos); 1663 new_cpos = le64_to_cpu(rec->r_cpos) & 1677 new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK; 1664 if (old_cpos <= new_cpos) 1678 if (old_cpos <= new_cpos) 1665 goto out; 1679 goto out; 1666 1680 1667 ocfs2_init_refcount_extent_tree(&et, 1681 ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 1668 1682 1669 path = ocfs2_new_path_from_et(&et); 1683 path = ocfs2_new_path_from_et(&et); 1670 if (!path) { 1684 if (!path) { 1671 ret = -ENOMEM; 1685 ret = -ENOMEM; 1672 mlog_errno(ret); 1686 mlog_errno(ret); 1673 goto out; 1687 goto out; 1674 } 1688 } 1675 1689 1676 ret = ocfs2_find_path(ci, path, old_c 1690 ret = ocfs2_find_path(ci, path, old_cpos); 1677 if (ret) { 1691 if (ret) { 1678 mlog_errno(ret); 1692 mlog_errno(ret); 1679 goto out; 1693 goto out; 1680 } 1694 } 1681 1695 1682 /* 1696 /* 1683 * 2 more credits, one for the leaf r 1697 * 2 more credits, one for the leaf refcount block, one for 1684 * the extent block contains the exte 1698 * the extent block contains the extent rec. 1685 */ 1699 */ 1686 ret = ocfs2_extend_trans(handle, 2); 1700 ret = ocfs2_extend_trans(handle, 2); 1687 if (ret < 0) { 1701 if (ret < 0) { 1688 mlog_errno(ret); 1702 mlog_errno(ret); 1689 goto out; 1703 goto out; 1690 } 1704 } 1691 1705 1692 ret = ocfs2_journal_access_rb(handle, 1706 ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1693 OCFS2_J 1707 OCFS2_JOURNAL_ACCESS_WRITE); 1694 if (ret < 0) { 1708 if (ret < 0) { 1695 mlog_errno(ret); 1709 mlog_errno(ret); 1696 goto out; 1710 goto out; 1697 } 1711 } 1698 1712 1699 ret = ocfs2_journal_access_eb(handle, 1713 ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path), 1700 OCFS2_J 1714 OCFS2_JOURNAL_ACCESS_WRITE); 1701 if (ret < 0) { 1715 if (ret < 0) { 1702 mlog_errno(ret); 1716 mlog_errno(ret); 1703 goto out; 1717 goto out; 1704 } 1718 } 1705 1719 1706 /* change the leaf extent block first 1720 /* change the leaf extent block first. */ 1707 el = path_leaf_el(path); 1721 el = path_leaf_el(path); 1708 1722 1709 for (i = 0; i < le16_to_cpu(el->l_nex 1723 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) 1710 if (le32_to_cpu(el->l_recs[i] 1724 if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos) 1711 break; 1725 break; 1712 1726 1713 BUG_ON(i == le16_to_cpu(el->l_next_fr 1727 BUG_ON(i == le16_to_cpu(el->l_next_free_rec)); 1714 1728 1715 el->l_recs[i].e_cpos = cpu_to_le32(ne 1729 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); 1716 1730 1717 /* change the r_cpos in the leaf bloc 1731 /* change the r_cpos in the leaf block. */ 1718 rb->rf_cpos = cpu_to_le32(new_cpos); 1732 rb->rf_cpos = cpu_to_le32(new_cpos); 1719 1733 1720 ocfs2_journal_dirty(handle, path_leaf 1734 ocfs2_journal_dirty(handle, path_leaf_bh(path)); 1721 ocfs2_journal_dirty(handle, ref_leaf_ 1735 ocfs2_journal_dirty(handle, ref_leaf_bh); 1722 1736 1723 out: 1737 out: 1724 ocfs2_free_path(path); 1738 ocfs2_free_path(path); 1725 return ret; 1739 return ret; 1726 } 1740 } 1727 1741 1728 static int ocfs2_insert_refcount_rec(handle_t 1742 static int ocfs2_insert_refcount_rec(handle_t *handle, 1729 struct o 1743 struct ocfs2_caching_info *ci, 1730 struct b 1744 struct buffer_head *ref_root_bh, 1731 struct b 1745 struct buffer_head *ref_leaf_bh, 1732 struct o 1746 struct ocfs2_refcount_rec *rec, 1733 int inde 1747 int index, int merge, 1734 struct o 1748 struct ocfs2_alloc_context *meta_ac) 1735 { 1749 { 1736 int ret; 1750 int ret; 1737 struct ocfs2_refcount_block *rb = 1751 struct ocfs2_refcount_block *rb = 1738 (struct ocfs2_refcoun 1752 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1739 struct ocfs2_refcount_list *rf_list = 1753 struct ocfs2_refcount_list *rf_list = &rb->rf_records; 1740 struct buffer_head *new_bh = NULL; 1754 struct buffer_head *new_bh = NULL; 1741 1755 1742 BUG_ON(le32_to_cpu(rb->rf_flags) & OC 1756 BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL); 1743 1757 1744 if (rf_list->rl_used == rf_list->rl_c 1758 if (rf_list->rl_used == rf_list->rl_count) { 1745 u64 cpos = le64_to_cpu(rec->r 1759 u64 cpos = le64_to_cpu(rec->r_cpos); 1746 u32 len = le32_to_cpu(rec->r_ 1760 u32 len = le32_to_cpu(rec->r_clusters); 1747 1761 1748 ret = ocfs2_expand_refcount_t 1762 ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh, 1749 1763 ref_leaf_bh, meta_ac); 1750 if (ret) { 1764 if (ret) { 1751 mlog_errno(ret); 1765 mlog_errno(ret); 1752 goto out; 1766 goto out; 1753 } 1767 } 1754 1768 1755 ret = ocfs2_get_refcount_rec( 1769 ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 1756 1770 cpos, len, NULL, &index, 1757 1771 &new_bh); 1758 if (ret) { 1772 if (ret) { 1759 mlog_errno(ret); 1773 mlog_errno(ret); 1760 goto out; 1774 goto out; 1761 } 1775 } 1762 1776 1763 ref_leaf_bh = new_bh; 1777 ref_leaf_bh = new_bh; 1764 rb = (struct ocfs2_refcount_b 1778 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1765 rf_list = &rb->rf_records; 1779 rf_list = &rb->rf_records; 1766 } 1780 } 1767 1781 1768 ret = ocfs2_journal_access_rb(handle, 1782 ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1769 OCFS2_J 1783 OCFS2_JOURNAL_ACCESS_WRITE); 1770 if (ret) { 1784 if (ret) { 1771 mlog_errno(ret); 1785 mlog_errno(ret); 1772 goto out; 1786 goto out; 1773 } 1787 } 1774 1788 1775 if (index < le16_to_cpu(rf_list->rl_u 1789 if (index < le16_to_cpu(rf_list->rl_used)) 1776 memmove(&rf_list->rl_recs[ind 1790 memmove(&rf_list->rl_recs[index + 1], 1777 &rf_list->rl_recs[ind 1791 &rf_list->rl_recs[index], 1778 (le16_to_cpu(rf_list- 1792 (le16_to_cpu(rf_list->rl_used) - index) * 1779 sizeof(struct ocfs2_ 1793 sizeof(struct ocfs2_refcount_rec)); 1780 1794 1781 trace_ocfs2_insert_refcount_rec( 1795 trace_ocfs2_insert_refcount_rec( 1782 (unsigned long long)ref_leaf_ 1796 (unsigned long long)ref_leaf_bh->b_blocknr, index, 1783 (unsigned long long)le64_to_c 1797 (unsigned long long)le64_to_cpu(rec->r_cpos), 1784 le32_to_cpu(rec->r_clusters), 1798 le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount)); 1785 1799 1786 rf_list->rl_recs[index] = *rec; 1800 rf_list->rl_recs[index] = *rec; 1787 1801 1788 le16_add_cpu(&rf_list->rl_used, 1); 1802 le16_add_cpu(&rf_list->rl_used, 1); 1789 1803 1790 if (merge) 1804 if (merge) 1791 ocfs2_refcount_rec_merge(rb, 1805 ocfs2_refcount_rec_merge(rb, index); 1792 1806 1793 ocfs2_journal_dirty(handle, ref_leaf_ 1807 ocfs2_journal_dirty(handle, ref_leaf_bh); 1794 1808 1795 if (index == 0) { 1809 if (index == 0) { 1796 ret = ocfs2_adjust_refcount_r 1810 ret = ocfs2_adjust_refcount_rec(handle, ci, 1797 1811 ref_root_bh, 1798 1812 ref_leaf_bh, rec); 1799 if (ret) 1813 if (ret) 1800 mlog_errno(ret); 1814 mlog_errno(ret); 1801 } 1815 } 1802 out: 1816 out: 1803 brelse(new_bh); 1817 brelse(new_bh); 1804 return ret; 1818 return ret; 1805 } 1819 } 1806 1820 1807 /* 1821 /* 1808 * Split the refcount_rec indexed by "index" 1822 * Split the refcount_rec indexed by "index" in ref_leaf_bh. 1809 * This is much simple than our b-tree code. 1823 * This is much simple than our b-tree code. 1810 * split_rec is the new refcount rec we want 1824 * split_rec is the new refcount rec we want to insert. 1811 * If split_rec->r_refcount > 0, we are chang 1825 * If split_rec->r_refcount > 0, we are changing the refcount(in case we 1812 * increase refcount or decrease a refcount t 1826 * increase refcount or decrease a refcount to non-zero). 1813 * If split_rec->r_refcount == 0, we are punc 1827 * If split_rec->r_refcount == 0, we are punching a hole in current refcount 1814 * rec( in case we decrease a refcount to zer 1828 * rec( in case we decrease a refcount to zero). 1815 */ 1829 */ 1816 static int ocfs2_split_refcount_rec(handle_t 1830 static int ocfs2_split_refcount_rec(handle_t *handle, 1817 struct oc 1831 struct ocfs2_caching_info *ci, 1818 struct bu 1832 struct buffer_head *ref_root_bh, 1819 struct bu 1833 struct buffer_head *ref_leaf_bh, 1820 struct oc 1834 struct ocfs2_refcount_rec *split_rec, 1821 int index 1835 int index, int merge, 1822 struct oc 1836 struct ocfs2_alloc_context *meta_ac, 1823 struct oc 1837 struct ocfs2_cached_dealloc_ctxt *dealloc) 1824 { 1838 { 1825 int ret, recs_need; 1839 int ret, recs_need; 1826 u32 len; 1840 u32 len; 1827 struct ocfs2_refcount_block *rb = 1841 struct ocfs2_refcount_block *rb = 1828 (struct ocfs2_refcoun 1842 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1829 struct ocfs2_refcount_list *rf_list = 1843 struct ocfs2_refcount_list *rf_list = &rb->rf_records; 1830 struct ocfs2_refcount_rec *orig_rec = 1844 struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index]; 1831 struct ocfs2_refcount_rec *tail_rec = 1845 struct ocfs2_refcount_rec *tail_rec = NULL; 1832 struct buffer_head *new_bh = NULL; 1846 struct buffer_head *new_bh = NULL; 1833 1847 1834 BUG_ON(le32_to_cpu(rb->rf_flags) & OC 1848 BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL); 1835 1849 1836 trace_ocfs2_split_refcount_rec(le64_t 1850 trace_ocfs2_split_refcount_rec(le64_to_cpu(orig_rec->r_cpos), 1837 le32_to_cpu(orig_rec->r_clust 1851 le32_to_cpu(orig_rec->r_clusters), 1838 le32_to_cpu(orig_rec->r_refco 1852 le32_to_cpu(orig_rec->r_refcount), 1839 le64_to_cpu(split_rec->r_cpos 1853 le64_to_cpu(split_rec->r_cpos), 1840 le32_to_cpu(split_rec->r_clus 1854 le32_to_cpu(split_rec->r_clusters), 1841 le32_to_cpu(split_rec->r_refc 1855 le32_to_cpu(split_rec->r_refcount)); 1842 1856 1843 /* 1857 /* 1844 * If we just need to split the heade 1858 * If we just need to split the header or tail clusters, 1845 * no more recs are needed, just spli 1859 * no more recs are needed, just split is OK. 1846 * Otherwise we at least need one new 1860 * Otherwise we at least need one new recs. 1847 */ 1861 */ 1848 if (!split_rec->r_refcount && 1862 if (!split_rec->r_refcount && 1849 (split_rec->r_cpos == orig_rec->r 1863 (split_rec->r_cpos == orig_rec->r_cpos || 1850 le64_to_cpu(split_rec->r_cpos) + 1864 le64_to_cpu(split_rec->r_cpos) + 1851 le32_to_cpu(split_rec->r_cluster 1865 le32_to_cpu(split_rec->r_clusters) == 1852 le64_to_cpu(orig_rec->r_cpos) + 1866 le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters))) 1853 recs_need = 0; 1867 recs_need = 0; 1854 else 1868 else 1855 recs_need = 1; 1869 recs_need = 1; 1856 1870 1857 /* 1871 /* 1858 * We need one more rec if we split i 1872 * We need one more rec if we split in the middle and the new rec have 1859 * some refcount in it. 1873 * some refcount in it. 1860 */ 1874 */ 1861 if (split_rec->r_refcount && 1875 if (split_rec->r_refcount && 1862 (split_rec->r_cpos != orig_rec->r 1876 (split_rec->r_cpos != orig_rec->r_cpos && 1863 le64_to_cpu(split_rec->r_cpos) + 1877 le64_to_cpu(split_rec->r_cpos) + 1864 le32_to_cpu(split_rec->r_cluster 1878 le32_to_cpu(split_rec->r_clusters) != 1865 le64_to_cpu(orig_rec->r_cpos) + 1879 le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters))) 1866 recs_need++; 1880 recs_need++; 1867 1881 1868 /* If the leaf block don't have enoug 1882 /* If the leaf block don't have enough record, expand it. */ 1869 if (le16_to_cpu(rf_list->rl_used) + r 1883 if (le16_to_cpu(rf_list->rl_used) + recs_need > 1870 le16 1884 le16_to_cpu(rf_list->rl_count)) { 1871 struct ocfs2_refcount_rec tmp 1885 struct ocfs2_refcount_rec tmp_rec; 1872 u64 cpos = le64_to_cpu(orig_r 1886 u64 cpos = le64_to_cpu(orig_rec->r_cpos); 1873 len = le32_to_cpu(orig_rec->r 1887 len = le32_to_cpu(orig_rec->r_clusters); 1874 ret = ocfs2_expand_refcount_t 1888 ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh, 1875 1889 ref_leaf_bh, meta_ac); 1876 if (ret) { 1890 if (ret) { 1877 mlog_errno(ret); 1891 mlog_errno(ret); 1878 goto out; 1892 goto out; 1879 } 1893 } 1880 1894 1881 /* 1895 /* 1882 * We have to re-get it since 1896 * We have to re-get it since now cpos may be moved to 1883 * another leaf block. 1897 * another leaf block. 1884 */ 1898 */ 1885 ret = ocfs2_get_refcount_rec( 1899 ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 1886 1900 cpos, len, &tmp_rec, &index, 1887 1901 &new_bh); 1888 if (ret) { 1902 if (ret) { 1889 mlog_errno(ret); 1903 mlog_errno(ret); 1890 goto out; 1904 goto out; 1891 } 1905 } 1892 1906 1893 ref_leaf_bh = new_bh; 1907 ref_leaf_bh = new_bh; 1894 rb = (struct ocfs2_refcount_b 1908 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1895 rf_list = &rb->rf_records; 1909 rf_list = &rb->rf_records; 1896 orig_rec = &rf_list->rl_recs[ 1910 orig_rec = &rf_list->rl_recs[index]; 1897 } 1911 } 1898 1912 1899 ret = ocfs2_journal_access_rb(handle, 1913 ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1900 OCFS2_J 1914 OCFS2_JOURNAL_ACCESS_WRITE); 1901 if (ret) { 1915 if (ret) { 1902 mlog_errno(ret); 1916 mlog_errno(ret); 1903 goto out; 1917 goto out; 1904 } 1918 } 1905 1919 1906 /* 1920 /* 1907 * We have calculated out how many ne 1921 * We have calculated out how many new records we need and store 1908 * in recs_need, so spare enough spac 1922 * in recs_need, so spare enough space first by moving the records 1909 * after "index" to the end. 1923 * after "index" to the end. 1910 */ 1924 */ 1911 if (index != le16_to_cpu(rf_list->rl_ 1925 if (index != le16_to_cpu(rf_list->rl_used) - 1) 1912 memmove(&rf_list->rl_recs[ind 1926 memmove(&rf_list->rl_recs[index + 1 + recs_need], 1913 &rf_list->rl_recs[ind 1927 &rf_list->rl_recs[index + 1], 1914 (le16_to_cpu(rf_list- 1928 (le16_to_cpu(rf_list->rl_used) - index - 1) * 1915 sizeof(struct ocfs2_ 1929 sizeof(struct ocfs2_refcount_rec)); 1916 1930 1917 len = (le64_to_cpu(orig_rec->r_cpos) 1931 len = (le64_to_cpu(orig_rec->r_cpos) + 1918 le32_to_cpu(orig_rec->r_cluster 1932 le32_to_cpu(orig_rec->r_clusters)) - 1919 (le64_to_cpu(split_rec->r_cpos) 1933 (le64_to_cpu(split_rec->r_cpos) + 1920 le32_to_cpu(split_rec->r_cluste 1934 le32_to_cpu(split_rec->r_clusters)); 1921 1935 1922 /* 1936 /* 1923 * If we have "len", the we will spli 1937 * If we have "len", the we will split in the tail and move it 1924 * to the end of the space we have ju 1938 * to the end of the space we have just spared. 1925 */ 1939 */ 1926 if (len) { 1940 if (len) { 1927 tail_rec = &rf_list->rl_recs[ 1941 tail_rec = &rf_list->rl_recs[index + recs_need]; 1928 1942 1929 memcpy(tail_rec, orig_rec, si 1943 memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec)); 1930 le64_add_cpu(&tail_rec->r_cpo 1944 le64_add_cpu(&tail_rec->r_cpos, 1931 le32_to_cpu(tail 1945 le32_to_cpu(tail_rec->r_clusters) - len); 1932 tail_rec->r_clusters = cpu_to 1946 tail_rec->r_clusters = cpu_to_le32(len); 1933 } 1947 } 1934 1948 1935 /* 1949 /* 1936 * If the split pos isn't the same as 1950 * If the split pos isn't the same as the original one, we need to 1937 * split in the head. 1951 * split in the head. 1938 * 1952 * 1939 * Note: We have the chance that spli 1953 * Note: We have the chance that split_rec.r_refcount = 0, 1940 * recs_need = 0 and len > 0, which m 1954 * recs_need = 0 and len > 0, which means we just cut the head from 1941 * the orig_rec and in that case we h 1955 * the orig_rec and in that case we have done some modification in 1942 * orig_rec above, so the check for r 1956 * orig_rec above, so the check for r_cpos is faked. 1943 */ 1957 */ 1944 if (split_rec->r_cpos != orig_rec->r_ 1958 if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) { 1945 len = le64_to_cpu(split_rec-> 1959 len = le64_to_cpu(split_rec->r_cpos) - 1946 le64_to_cpu(orig_rec->r 1960 le64_to_cpu(orig_rec->r_cpos); 1947 orig_rec->r_clusters = cpu_to 1961 orig_rec->r_clusters = cpu_to_le32(len); 1948 index++; 1962 index++; 1949 } 1963 } 1950 1964 1951 le16_add_cpu(&rf_list->rl_used, recs_ 1965 le16_add_cpu(&rf_list->rl_used, recs_need); 1952 1966 1953 if (split_rec->r_refcount) { 1967 if (split_rec->r_refcount) { 1954 rf_list->rl_recs[index] = *sp 1968 rf_list->rl_recs[index] = *split_rec; 1955 trace_ocfs2_split_refcount_re 1969 trace_ocfs2_split_refcount_rec_insert( 1956 (unsigned long long)r 1970 (unsigned long long)ref_leaf_bh->b_blocknr, index, 1957 (unsigned long long)l 1971 (unsigned long long)le64_to_cpu(split_rec->r_cpos), 1958 le32_to_cpu(split_rec 1972 le32_to_cpu(split_rec->r_clusters), 1959 le32_to_cpu(split_rec 1973 le32_to_cpu(split_rec->r_refcount)); 1960 1974 1961 if (merge) 1975 if (merge) 1962 ocfs2_refcount_rec_me 1976 ocfs2_refcount_rec_merge(rb, index); 1963 } 1977 } 1964 1978 1965 ocfs2_journal_dirty(handle, ref_leaf_ 1979 ocfs2_journal_dirty(handle, ref_leaf_bh); 1966 1980 1967 out: 1981 out: 1968 brelse(new_bh); 1982 brelse(new_bh); 1969 return ret; 1983 return ret; 1970 } 1984 } 1971 1985 1972 static int __ocfs2_increase_refcount(handle_t 1986 static int __ocfs2_increase_refcount(handle_t *handle, 1973 struct o 1987 struct ocfs2_caching_info *ci, 1974 struct b 1988 struct buffer_head *ref_root_bh, 1975 u64 cpos 1989 u64 cpos, u32 len, int merge, 1976 struct o 1990 struct ocfs2_alloc_context *meta_ac, 1977 struct o 1991 struct ocfs2_cached_dealloc_ctxt *dealloc) 1978 { 1992 { 1979 int ret = 0, index; 1993 int ret = 0, index; 1980 struct buffer_head *ref_leaf_bh = NUL 1994 struct buffer_head *ref_leaf_bh = NULL; 1981 struct ocfs2_refcount_rec rec; 1995 struct ocfs2_refcount_rec rec; 1982 unsigned int set_len = 0; 1996 unsigned int set_len = 0; 1983 1997 1984 trace_ocfs2_increase_refcount_begin( 1998 trace_ocfs2_increase_refcount_begin( 1985 (unsigned long long)ocfs2_metada 1999 (unsigned long long)ocfs2_metadata_cache_owner(ci), 1986 (unsigned long long)cpos, len); 2000 (unsigned long long)cpos, len); 1987 2001 1988 while (len) { 2002 while (len) { 1989 ret = ocfs2_get_refcount_rec( 2003 ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 1990 2004 cpos, len, &rec, &index, 1991 2005 &ref_leaf_bh); 1992 if (ret) { 2006 if (ret) { 1993 mlog_errno(ret); 2007 mlog_errno(ret); 1994 goto out; 2008 goto out; 1995 } 2009 } 1996 2010 1997 set_len = le32_to_cpu(rec.r_c 2011 set_len = le32_to_cpu(rec.r_clusters); 1998 2012 1999 /* 2013 /* 2000 * Here we may meet with 3 si 2014 * Here we may meet with 3 situations: 2001 * 2015 * 2002 * 1. If we find an already e 2016 * 1. If we find an already existing record, and the length 2003 * is the same, cool, we j 2017 * is the same, cool, we just need to increase the r_refcount 2004 * and it is OK. 2018 * and it is OK. 2005 * 2. If we find a hole, just 2019 * 2. If we find a hole, just insert it with r_refcount = 1. 2006 * 3. If we are in the middle 2020 * 3. If we are in the middle of one extent record, split 2007 * it. 2021 * it. 2008 */ 2022 */ 2009 if (rec.r_refcount && le64_to 2023 if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos && 2010 set_len <= len) { 2024 set_len <= len) { 2011 trace_ocfs2_increase_ 2025 trace_ocfs2_increase_refcount_change( 2012 (unsigned lon 2026 (unsigned long long)cpos, set_len, 2013 le32_to_cpu(r 2027 le32_to_cpu(rec.r_refcount)); 2014 ret = ocfs2_change_re 2028 ret = ocfs2_change_refcount_rec(handle, ci, 2015 2029 ref_leaf_bh, index, 2016 2030 merge, 1); 2017 if (ret) { 2031 if (ret) { 2018 mlog_errno(re 2032 mlog_errno(ret); 2019 goto out; 2033 goto out; 2020 } 2034 } 2021 } else if (!rec.r_refcount) { 2035 } else if (!rec.r_refcount) { 2022 rec.r_refcount = cpu_ 2036 rec.r_refcount = cpu_to_le32(1); 2023 2037 2024 trace_ocfs2_increase_ 2038 trace_ocfs2_increase_refcount_insert( 2025 (unsigned long l 2039 (unsigned long long)le64_to_cpu(rec.r_cpos), 2026 set_len); 2040 set_len); 2027 ret = ocfs2_insert_re 2041 ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh, 2028 2042 ref_leaf_bh, 2029 2043 &rec, index, 2030 2044 merge, meta_ac); 2031 if (ret) { 2045 if (ret) { 2032 mlog_errno(re 2046 mlog_errno(ret); 2033 goto out; 2047 goto out; 2034 } 2048 } 2035 } else { 2049 } else { 2036 set_len = min((u64)(c 2050 set_len = min((u64)(cpos + len), 2037 le64_to 2051 le64_to_cpu(rec.r_cpos) + set_len) - cpos; 2038 rec.r_cpos = cpu_to_l 2052 rec.r_cpos = cpu_to_le64(cpos); 2039 rec.r_clusters = cpu_ 2053 rec.r_clusters = cpu_to_le32(set_len); 2040 le32_add_cpu(&rec.r_r 2054 le32_add_cpu(&rec.r_refcount, 1); 2041 2055 2042 trace_ocfs2_increase_ 2056 trace_ocfs2_increase_refcount_split( 2043 (unsigned long l 2057 (unsigned long long)le64_to_cpu(rec.r_cpos), 2044 set_len, le32_to 2058 set_len, le32_to_cpu(rec.r_refcount)); 2045 ret = ocfs2_split_ref 2059 ret = ocfs2_split_refcount_rec(handle, ci, 2046 2060 ref_root_bh, ref_leaf_bh, 2047 2061 &rec, index, merge, 2048 2062 meta_ac, dealloc); 2049 if (ret) { 2063 if (ret) { 2050 mlog_errno(re 2064 mlog_errno(ret); 2051 goto out; 2065 goto out; 2052 } 2066 } 2053 } 2067 } 2054 2068 2055 cpos += set_len; 2069 cpos += set_len; 2056 len -= set_len; 2070 len -= set_len; 2057 brelse(ref_leaf_bh); 2071 brelse(ref_leaf_bh); 2058 ref_leaf_bh = NULL; 2072 ref_leaf_bh = NULL; 2059 } 2073 } 2060 2074 2061 out: 2075 out: 2062 brelse(ref_leaf_bh); 2076 brelse(ref_leaf_bh); 2063 return ret; 2077 return ret; 2064 } 2078 } 2065 2079 2066 static int ocfs2_remove_refcount_extent(handl 2080 static int ocfs2_remove_refcount_extent(handle_t *handle, 2067 struct ocfs2_ 2081 struct ocfs2_caching_info *ci, 2068 struct buffer 2082 struct buffer_head *ref_root_bh, 2069 struct buffer 2083 struct buffer_head *ref_leaf_bh, 2070 struct ocfs2_ 2084 struct ocfs2_alloc_context *meta_ac, 2071 struct ocfs2_ 2085 struct ocfs2_cached_dealloc_ctxt *dealloc) 2072 { 2086 { 2073 int ret; 2087 int ret; 2074 struct super_block *sb = ocfs2_metada 2088 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 2075 struct ocfs2_refcount_block *rb = 2089 struct ocfs2_refcount_block *rb = 2076 (struct ocfs2_refcoun 2090 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 2077 struct ocfs2_extent_tree et; 2091 struct ocfs2_extent_tree et; 2078 2092 2079 BUG_ON(rb->rf_records.rl_used); 2093 BUG_ON(rb->rf_records.rl_used); 2080 2094 2081 trace_ocfs2_remove_refcount_extent( 2095 trace_ocfs2_remove_refcount_extent( 2082 (unsigned long long)ocfs2_met 2096 (unsigned long long)ocfs2_metadata_cache_owner(ci), 2083 (unsigned long long)ref_leaf_ 2097 (unsigned long long)ref_leaf_bh->b_blocknr, 2084 le32_to_cpu(rb->rf_cpos)); 2098 le32_to_cpu(rb->rf_cpos)); 2085 2099 2086 ocfs2_init_refcount_extent_tree(&et, 2100 ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 2087 ret = ocfs2_remove_extent(handle, &et 2101 ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos), 2088 1, meta_ac, 2102 1, meta_ac, dealloc); 2089 if (ret) { 2103 if (ret) { 2090 mlog_errno(ret); 2104 mlog_errno(ret); 2091 goto out; 2105 goto out; 2092 } 2106 } 2093 2107 2094 ocfs2_remove_from_cache(ci, ref_leaf_ 2108 ocfs2_remove_from_cache(ci, ref_leaf_bh); 2095 2109 2096 /* 2110 /* 2097 * add the freed block to the dealloc 2111 * add the freed block to the dealloc so that it will be freed 2098 * when we run dealloc. 2112 * when we run dealloc. 2099 */ 2113 */ 2100 ret = ocfs2_cache_block_dealloc(deall 2114 ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE, 2101 le16_ 2115 le16_to_cpu(rb->rf_suballoc_slot), 2102 le64_ 2116 le64_to_cpu(rb->rf_suballoc_loc), 2103 le64_ 2117 le64_to_cpu(rb->rf_blkno), 2104 le16_ 2118 le16_to_cpu(rb->rf_suballoc_bit)); 2105 if (ret) { 2119 if (ret) { 2106 mlog_errno(ret); 2120 mlog_errno(ret); 2107 goto out; 2121 goto out; 2108 } 2122 } 2109 2123 2110 ret = ocfs2_journal_access_rb(handle, 2124 ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh, 2111 OCFS2_J 2125 OCFS2_JOURNAL_ACCESS_WRITE); 2112 if (ret) { 2126 if (ret) { 2113 mlog_errno(ret); 2127 mlog_errno(ret); 2114 goto out; 2128 goto out; 2115 } 2129 } 2116 2130 2117 rb = (struct ocfs2_refcount_block *)r 2131 rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 2118 2132 2119 le32_add_cpu(&rb->rf_clusters, -1); 2133 le32_add_cpu(&rb->rf_clusters, -1); 2120 2134 2121 /* 2135 /* 2122 * check whether we need to restore t 2136 * check whether we need to restore the root refcount block if 2123 * there is no leaf extent block at a 2137 * there is no leaf extent block at atll. 2124 */ 2138 */ 2125 if (!rb->rf_list.l_next_free_rec) { 2139 if (!rb->rf_list.l_next_free_rec) { 2126 BUG_ON(rb->rf_clusters); 2140 BUG_ON(rb->rf_clusters); 2127 2141 2128 trace_ocfs2_restore_refcount_ 2142 trace_ocfs2_restore_refcount_block( 2129 (unsigned long long)ref_ 2143 (unsigned long long)ref_root_bh->b_blocknr); 2130 2144 2131 rb->rf_flags = 0; 2145 rb->rf_flags = 0; 2132 rb->rf_parent = 0; 2146 rb->rf_parent = 0; 2133 rb->rf_cpos = 0; 2147 rb->rf_cpos = 0; 2134 memset(&rb->rf_records, 0, sb 2148 memset(&rb->rf_records, 0, sb->s_blocksize - 2135 offsetof(struct ocfs2_ 2149 offsetof(struct ocfs2_refcount_block, rf_records)); 2136 rb->rf_records.rl_count = 2150 rb->rf_records.rl_count = 2137 cpu_to_le16(o 2151 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb)); 2138 } 2152 } 2139 2153 2140 ocfs2_journal_dirty(handle, ref_root_ 2154 ocfs2_journal_dirty(handle, ref_root_bh); 2141 2155 2142 out: 2156 out: 2143 return ret; 2157 return ret; 2144 } 2158 } 2145 2159 2146 int ocfs2_increase_refcount(handle_t *handle, 2160 int ocfs2_increase_refcount(handle_t *handle, 2147 struct ocfs2_cach 2161 struct ocfs2_caching_info *ci, 2148 struct buffer_hea 2162 struct buffer_head *ref_root_bh, 2149 u64 cpos, u32 len 2163 u64 cpos, u32 len, 2150 struct ocfs2_allo 2164 struct ocfs2_alloc_context *meta_ac, 2151 struct ocfs2_cach 2165 struct ocfs2_cached_dealloc_ctxt *dealloc) 2152 { 2166 { 2153 return __ocfs2_increase_refcount(hand 2167 return __ocfs2_increase_refcount(handle, ci, ref_root_bh, 2154 cpos 2168 cpos, len, 1, 2155 meta 2169 meta_ac, dealloc); 2156 } 2170 } 2157 2171 2158 static int ocfs2_decrease_refcount_rec(handle 2172 static int ocfs2_decrease_refcount_rec(handle_t *handle, 2159 struct ocfs2_ 2173 struct ocfs2_caching_info *ci, 2160 struct buffer 2174 struct buffer_head *ref_root_bh, 2161 struct buffer 2175 struct buffer_head *ref_leaf_bh, 2162 int index, u6 2176 int index, u64 cpos, unsigned int len, 2163 struct ocfs2_ 2177 struct ocfs2_alloc_context *meta_ac, 2164 struct ocfs2_ 2178 struct ocfs2_cached_dealloc_ctxt *dealloc) 2165 { 2179 { 2166 int ret; 2180 int ret; 2167 struct ocfs2_refcount_block *rb = 2181 struct ocfs2_refcount_block *rb = 2168 (struct ocfs2_refcoun 2182 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 2169 struct ocfs2_refcount_rec *rec = &rb- 2183 struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index]; 2170 2184 2171 BUG_ON(cpos < le64_to_cpu(rec->r_cpos 2185 BUG_ON(cpos < le64_to_cpu(rec->r_cpos)); 2172 BUG_ON(cpos + len > 2186 BUG_ON(cpos + len > 2173 le64_to_cpu(rec->r_cpos) + le3 2187 le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters)); 2174 2188 2175 trace_ocfs2_decrease_refcount_rec( 2189 trace_ocfs2_decrease_refcount_rec( 2176 (unsigned long long)ocfs2_met 2190 (unsigned long long)ocfs2_metadata_cache_owner(ci), 2177 (unsigned long long)cpos, len 2191 (unsigned long long)cpos, len); 2178 2192 2179 if (cpos == le64_to_cpu(rec->r_cpos) 2193 if (cpos == le64_to_cpu(rec->r_cpos) && 2180 len == le32_to_cpu(rec->r_cluster 2194 len == le32_to_cpu(rec->r_clusters)) 2181 ret = ocfs2_change_refcount_r 2195 ret = ocfs2_change_refcount_rec(handle, ci, 2182 2196 ref_leaf_bh, index, 1, -1); 2183 else { 2197 else { 2184 struct ocfs2_refcount_rec spl 2198 struct ocfs2_refcount_rec split = *rec; 2185 split.r_cpos = cpu_to_le64(cp 2199 split.r_cpos = cpu_to_le64(cpos); 2186 split.r_clusters = cpu_to_le3 2200 split.r_clusters = cpu_to_le32(len); 2187 2201 2188 le32_add_cpu(&split.r_refcoun 2202 le32_add_cpu(&split.r_refcount, -1); 2189 2203 2190 ret = ocfs2_split_refcount_re 2204 ret = ocfs2_split_refcount_rec(handle, ci, 2191 2205 ref_root_bh, ref_leaf_bh, 2192 2206 &split, index, 1, 2193 2207 meta_ac, dealloc); 2194 } 2208 } 2195 2209 2196 if (ret) { 2210 if (ret) { 2197 mlog_errno(ret); 2211 mlog_errno(ret); 2198 goto out; 2212 goto out; 2199 } 2213 } 2200 2214 2201 /* Remove the leaf refcount block if 2215 /* Remove the leaf refcount block if it contains no refcount record. */ 2202 if (!rb->rf_records.rl_used && ref_le 2216 if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) { 2203 ret = ocfs2_remove_refcount_e 2217 ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh, 2204 2218 ref_leaf_bh, meta_ac, 2205 2219 dealloc); 2206 if (ret) 2220 if (ret) 2207 mlog_errno(ret); 2221 mlog_errno(ret); 2208 } 2222 } 2209 2223 2210 out: 2224 out: 2211 return ret; 2225 return ret; 2212 } 2226 } 2213 2227 2214 static int __ocfs2_decrease_refcount(handle_t 2228 static int __ocfs2_decrease_refcount(handle_t *handle, 2215 struct o 2229 struct ocfs2_caching_info *ci, 2216 struct b 2230 struct buffer_head *ref_root_bh, 2217 u64 cpos 2231 u64 cpos, u32 len, 2218 struct o 2232 struct ocfs2_alloc_context *meta_ac, 2219 struct o 2233 struct ocfs2_cached_dealloc_ctxt *dealloc, 2220 int dele 2234 int delete) 2221 { 2235 { 2222 int ret = 0, index = 0; 2236 int ret = 0, index = 0; 2223 struct ocfs2_refcount_rec rec; 2237 struct ocfs2_refcount_rec rec; 2224 unsigned int r_count = 0, r_len; 2238 unsigned int r_count = 0, r_len; 2225 struct super_block *sb = ocfs2_metada 2239 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 2226 struct buffer_head *ref_leaf_bh = NUL 2240 struct buffer_head *ref_leaf_bh = NULL; 2227 2241 2228 trace_ocfs2_decrease_refcount( 2242 trace_ocfs2_decrease_refcount( 2229 (unsigned long long)ocfs2_met 2243 (unsigned long long)ocfs2_metadata_cache_owner(ci), 2230 (unsigned long long)cpos, len 2244 (unsigned long long)cpos, len, delete); 2231 2245 2232 while (len) { 2246 while (len) { 2233 ret = ocfs2_get_refcount_rec( 2247 ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 2234 2248 cpos, len, &rec, &index, 2235 2249 &ref_leaf_bh); 2236 if (ret) { 2250 if (ret) { 2237 mlog_errno(ret); 2251 mlog_errno(ret); 2238 goto out; 2252 goto out; 2239 } 2253 } 2240 2254 2241 r_count = le32_to_cpu(rec.r_r 2255 r_count = le32_to_cpu(rec.r_refcount); 2242 BUG_ON(r_count == 0); 2256 BUG_ON(r_count == 0); 2243 if (!delete) 2257 if (!delete) 2244 BUG_ON(r_count > 1); 2258 BUG_ON(r_count > 1); 2245 2259 2246 r_len = min((u64)(cpos + len) 2260 r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) + 2247 le32_to_cpu(rec 2261 le32_to_cpu(rec.r_clusters)) - cpos; 2248 2262 2249 ret = ocfs2_decrease_refcount 2263 ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh, 2250 2264 ref_leaf_bh, index, 2251 2265 cpos, r_len, 2252 2266 meta_ac, dealloc); 2253 if (ret) { 2267 if (ret) { 2254 mlog_errno(ret); 2268 mlog_errno(ret); 2255 goto out; 2269 goto out; 2256 } 2270 } 2257 2271 2258 if (le32_to_cpu(rec.r_refcoun 2272 if (le32_to_cpu(rec.r_refcount) == 1 && delete) { 2259 ret = ocfs2_cache_clu 2273 ret = ocfs2_cache_cluster_dealloc(dealloc, 2260 ocf 2274 ocfs2_clusters_to_blocks(sb, cpos), 2261 2275 r_len); 2262 if (ret) { 2276 if (ret) { 2263 mlog_errno(re 2277 mlog_errno(ret); 2264 goto out; 2278 goto out; 2265 } 2279 } 2266 } 2280 } 2267 2281 2268 cpos += r_len; 2282 cpos += r_len; 2269 len -= r_len; 2283 len -= r_len; 2270 brelse(ref_leaf_bh); 2284 brelse(ref_leaf_bh); 2271 ref_leaf_bh = NULL; 2285 ref_leaf_bh = NULL; 2272 } 2286 } 2273 2287 2274 out: 2288 out: 2275 brelse(ref_leaf_bh); 2289 brelse(ref_leaf_bh); 2276 return ret; 2290 return ret; 2277 } 2291 } 2278 2292 2279 /* Caller must hold refcount tree lock. */ 2293 /* Caller must hold refcount tree lock. */ 2280 int ocfs2_decrease_refcount(struct inode *ino 2294 int ocfs2_decrease_refcount(struct inode *inode, 2281 handle_t *handle, 2295 handle_t *handle, u32 cpos, u32 len, 2282 struct ocfs2_allo 2296 struct ocfs2_alloc_context *meta_ac, 2283 struct ocfs2_cach 2297 struct ocfs2_cached_dealloc_ctxt *dealloc, 2284 int delete) 2298 int delete) 2285 { 2299 { 2286 int ret; 2300 int ret; 2287 u64 ref_blkno; 2301 u64 ref_blkno; 2288 struct buffer_head *ref_root_bh = NUL 2302 struct buffer_head *ref_root_bh = NULL; 2289 struct ocfs2_refcount_tree *tree; 2303 struct ocfs2_refcount_tree *tree; 2290 2304 2291 BUG_ON(!ocfs2_is_refcount_inode(inode 2305 BUG_ON(!ocfs2_is_refcount_inode(inode)); 2292 2306 2293 ret = ocfs2_get_refcount_block(inode, 2307 ret = ocfs2_get_refcount_block(inode, &ref_blkno); 2294 if (ret) { 2308 if (ret) { 2295 mlog_errno(ret); 2309 mlog_errno(ret); 2296 goto out; 2310 goto out; 2297 } 2311 } 2298 2312 2299 ret = ocfs2_get_refcount_tree(OCFS2_S 2313 ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree); 2300 if (ret) { 2314 if (ret) { 2301 mlog_errno(ret); 2315 mlog_errno(ret); 2302 goto out; 2316 goto out; 2303 } 2317 } 2304 2318 2305 ret = ocfs2_read_refcount_block(&tree 2319 ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno, 2306 &ref_ 2320 &ref_root_bh); 2307 if (ret) { 2321 if (ret) { 2308 mlog_errno(ret); 2322 mlog_errno(ret); 2309 goto out; 2323 goto out; 2310 } 2324 } 2311 2325 2312 ret = __ocfs2_decrease_refcount(handl 2326 ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh, 2313 cpos, 2327 cpos, len, meta_ac, dealloc, delete); 2314 if (ret) 2328 if (ret) 2315 mlog_errno(ret); 2329 mlog_errno(ret); 2316 out: 2330 out: 2317 brelse(ref_root_bh); 2331 brelse(ref_root_bh); 2318 return ret; 2332 return ret; 2319 } 2333 } 2320 2334 2321 /* 2335 /* 2322 * Mark the already-existing extent at cpos a 2336 * Mark the already-existing extent at cpos as refcounted for len clusters. 2323 * This adds the refcount extent flag. 2337 * This adds the refcount extent flag. 2324 * 2338 * 2325 * If the existing extent is larger than the 2339 * If the existing extent is larger than the request, initiate a 2326 * split. An attempt will be made at merging 2340 * split. An attempt will be made at merging with adjacent extents. 2327 * 2341 * 2328 * The caller is responsible for passing down 2342 * The caller is responsible for passing down meta_ac if we'll need it. 2329 */ 2343 */ 2330 static int ocfs2_mark_extent_refcounted(struc 2344 static int ocfs2_mark_extent_refcounted(struct inode *inode, 2331 struct ocfs2_ 2345 struct ocfs2_extent_tree *et, 2332 handle_t *han 2346 handle_t *handle, u32 cpos, 2333 u32 len, u32 2347 u32 len, u32 phys, 2334 struct ocfs2_ 2348 struct ocfs2_alloc_context *meta_ac, 2335 struct ocfs2_ 2349 struct ocfs2_cached_dealloc_ctxt *dealloc) 2336 { 2350 { 2337 int ret; 2351 int ret; 2338 2352 2339 trace_ocfs2_mark_extent_refcounted(OC 2353 trace_ocfs2_mark_extent_refcounted(OCFS2_I(inode)->ip_blkno, 2340 cp 2354 cpos, len, phys); 2341 2355 2342 if (!ocfs2_refcount_tree(OCFS2_SB(ino 2356 if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) { 2343 ret = ocfs2_error(inode->i_sb 2357 ret = ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n", 2344 inode->i_in 2358 inode->i_ino); 2345 goto out; 2359 goto out; 2346 } 2360 } 2347 2361 2348 ret = ocfs2_change_extent_flag(handle 2362 ret = ocfs2_change_extent_flag(handle, et, cpos, 2349 len, p 2363 len, phys, meta_ac, dealloc, 2350 OCFS2_ 2364 OCFS2_EXT_REFCOUNTED, 0); 2351 if (ret) 2365 if (ret) 2352 mlog_errno(ret); 2366 mlog_errno(ret); 2353 2367 2354 out: 2368 out: 2355 return ret; 2369 return ret; 2356 } 2370 } 2357 2371 2358 /* 2372 /* 2359 * Given some contiguous physical clusters, c 2373 * Given some contiguous physical clusters, calculate what we need 2360 * for modifying their refcount. 2374 * for modifying their refcount. 2361 */ 2375 */ 2362 static int ocfs2_calc_refcount_meta_credits(s 2376 static int ocfs2_calc_refcount_meta_credits(struct super_block *sb, 2363 s 2377 struct ocfs2_caching_info *ci, 2364 s 2378 struct buffer_head *ref_root_bh, 2365 u 2379 u64 start_cpos, 2366 u 2380 u32 clusters, 2367 i 2381 int *meta_add, 2368 i 2382 int *credits) 2369 { 2383 { 2370 int ret = 0, index, ref_blocks = 0, r 2384 int ret = 0, index, ref_blocks = 0, recs_add = 0; 2371 u64 cpos = start_cpos; 2385 u64 cpos = start_cpos; 2372 struct ocfs2_refcount_block *rb; 2386 struct ocfs2_refcount_block *rb; 2373 struct ocfs2_refcount_rec rec; 2387 struct ocfs2_refcount_rec rec; 2374 struct buffer_head *ref_leaf_bh = NUL 2388 struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL; 2375 u32 len; 2389 u32 len; 2376 2390 2377 while (clusters) { 2391 while (clusters) { 2378 ret = ocfs2_get_refcount_rec( 2392 ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 2379 2393 cpos, clusters, &rec, 2380 2394 &index, &ref_leaf_bh); 2381 if (ret) { 2395 if (ret) { 2382 mlog_errno(ret); 2396 mlog_errno(ret); 2383 goto out; 2397 goto out; 2384 } 2398 } 2385 2399 2386 if (ref_leaf_bh != prev_bh) { 2400 if (ref_leaf_bh != prev_bh) { 2387 /* 2401 /* 2388 * Now we encounter a 2402 * Now we encounter a new leaf block, so calculate 2389 * whether we need to 2403 * whether we need to extend the old leaf. 2390 */ 2404 */ 2391 if (prev_bh) { 2405 if (prev_bh) { 2392 rb = (struct 2406 rb = (struct ocfs2_refcount_block *) 2393 2407 prev_bh->b_data; 2394 2408 2395 if (le16_to_c 2409 if (le16_to_cpu(rb->rf_records.rl_used) + 2396 recs_add 2410 recs_add > 2397 le16_to_c 2411 le16_to_cpu(rb->rf_records.rl_count)) 2398 ref_b 2412 ref_blocks++; 2399 } 2413 } 2400 2414 2401 recs_add = 0; 2415 recs_add = 0; 2402 *credits += 1; 2416 *credits += 1; 2403 brelse(prev_bh); 2417 brelse(prev_bh); 2404 prev_bh = ref_leaf_bh 2418 prev_bh = ref_leaf_bh; 2405 get_bh(prev_bh); 2419 get_bh(prev_bh); 2406 } 2420 } 2407 2421 2408 trace_ocfs2_calc_refcount_met 2422 trace_ocfs2_calc_refcount_meta_credits_iterate( 2409 recs_add, (un 2423 recs_add, (unsigned long long)cpos, clusters, 2410 (unsigned lon 2424 (unsigned long long)le64_to_cpu(rec.r_cpos), 2411 le32_to_cpu(r 2425 le32_to_cpu(rec.r_clusters), 2412 le32_to_cpu(r 2426 le32_to_cpu(rec.r_refcount), index); 2413 2427 2414 len = min((u64)cpos + cluster 2428 len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) + 2415 le32_to_cpu(rec.r_c 2429 le32_to_cpu(rec.r_clusters)) - cpos; 2416 /* 2430 /* 2417 * We record all the records 2431 * We record all the records which will be inserted to the 2418 * same refcount block, so th 2432 * same refcount block, so that we can tell exactly whether 2419 * we need a new refcount blo 2433 * we need a new refcount block or not. 2420 * 2434 * 2421 * If we will insert a new on 2435 * If we will insert a new one, this is easy and only happens 2422 * during adding refcounted f 2436 * during adding refcounted flag to the extent, so we don't 2423 * have a chance of spliting. 2437 * have a chance of spliting. We just need one record. 2424 * 2438 * 2425 * If the refcount rec alread 2439 * If the refcount rec already exists, that would be a little 2426 * complicated. we may have t 2440 * complicated. we may have to: 2427 * 1) split at the beginning 2441 * 1) split at the beginning if the start pos isn't aligned. 2428 * we need 1 more record i 2442 * we need 1 more record in this case. 2429 * 2) split int the end if th 2443 * 2) split int the end if the end pos isn't aligned. 2430 * we need 1 more record i 2444 * we need 1 more record in this case. 2431 * 3) split in the middle bec 2445 * 3) split in the middle because of file system fragmentation. 2432 * we need 2 more records 2446 * we need 2 more records in this case(we can't detect this 2433 * beforehand, so always t 2447 * beforehand, so always think of the worst case). 2434 */ 2448 */ 2435 if (rec.r_refcount) { 2449 if (rec.r_refcount) { 2436 recs_add += 2; 2450 recs_add += 2; 2437 /* Check whether we n 2451 /* Check whether we need a split at the beginning. */ 2438 if (cpos == start_cpo 2452 if (cpos == start_cpos && 2439 cpos != le64_to_c 2453 cpos != le64_to_cpu(rec.r_cpos)) 2440 recs_add++; 2454 recs_add++; 2441 2455 2442 /* Check whether we n 2456 /* Check whether we need a split in the end. */ 2443 if (cpos + clusters < 2457 if (cpos + clusters < le64_to_cpu(rec.r_cpos) + 2444 le32_to_cpu(rec.r 2458 le32_to_cpu(rec.r_clusters)) 2445 recs_add++; 2459 recs_add++; 2446 } else 2460 } else 2447 recs_add++; 2461 recs_add++; 2448 2462 2449 brelse(ref_leaf_bh); 2463 brelse(ref_leaf_bh); 2450 ref_leaf_bh = NULL; 2464 ref_leaf_bh = NULL; 2451 clusters -= len; 2465 clusters -= len; 2452 cpos += len; 2466 cpos += len; 2453 } 2467 } 2454 2468 2455 if (prev_bh) { 2469 if (prev_bh) { 2456 rb = (struct ocfs2_refcount_b 2470 rb = (struct ocfs2_refcount_block *)prev_bh->b_data; 2457 2471 2458 if (le16_to_cpu(rb->rf_record 2472 if (le16_to_cpu(rb->rf_records.rl_used) + recs_add > 2459 le16_to_cpu(rb->rf_record 2473 le16_to_cpu(rb->rf_records.rl_count)) 2460 ref_blocks++; 2474 ref_blocks++; 2461 2475 2462 *credits += 1; 2476 *credits += 1; 2463 } 2477 } 2464 2478 2465 if (!ref_blocks) 2479 if (!ref_blocks) 2466 goto out; 2480 goto out; 2467 2481 2468 *meta_add += ref_blocks; 2482 *meta_add += ref_blocks; 2469 *credits += ref_blocks; 2483 *credits += ref_blocks; 2470 2484 2471 /* 2485 /* 2472 * So we may need ref_blocks to inser 2486 * So we may need ref_blocks to insert into the tree. 2473 * That also means we need to change 2487 * That also means we need to change the b-tree and add that number 2474 * of records since we never merge th 2488 * of records since we never merge them. 2475 * We need one more block for expansi 2489 * We need one more block for expansion since the new created leaf 2476 * block is also full and needs split 2490 * block is also full and needs split. 2477 */ 2491 */ 2478 rb = (struct ocfs2_refcount_block *)r 2492 rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 2479 if (le32_to_cpu(rb->rf_flags) & OCFS2 2493 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) { 2480 struct ocfs2_extent_tree et; 2494 struct ocfs2_extent_tree et; 2481 2495 2482 ocfs2_init_refcount_extent_tr 2496 ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 2483 *meta_add += ocfs2_extend_met 2497 *meta_add += ocfs2_extend_meta_needed(et.et_root_el); 2484 *credits += ocfs2_calc_extend 2498 *credits += ocfs2_calc_extend_credits(sb, 2485 2499 et.et_root_el); 2486 } else { 2500 } else { 2487 *credits += OCFS2_EXPAND_REFC 2501 *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; 2488 *meta_add += 1; 2502 *meta_add += 1; 2489 } 2503 } 2490 2504 2491 out: 2505 out: 2492 2506 2493 trace_ocfs2_calc_refcount_meta_credit 2507 trace_ocfs2_calc_refcount_meta_credits( 2494 (unsigned long long)start_cpo 2508 (unsigned long long)start_cpos, clusters, 2495 *meta_add, *credits); 2509 *meta_add, *credits); 2496 brelse(ref_leaf_bh); 2510 brelse(ref_leaf_bh); 2497 brelse(prev_bh); 2511 brelse(prev_bh); 2498 return ret; 2512 return ret; 2499 } 2513 } 2500 2514 2501 /* 2515 /* 2502 * For refcount tree, we will decrease some c 2516 * For refcount tree, we will decrease some contiguous clusters 2503 * refcount count, so just go through it to s 2517 * refcount count, so just go through it to see how many blocks 2504 * we gonna touch and whether we need to crea 2518 * we gonna touch and whether we need to create new blocks. 2505 * 2519 * 2506 * Normally the refcount blocks store these r 2520 * Normally the refcount blocks store these refcount should be 2507 * contiguous also, so that we can get the nu 2521 * contiguous also, so that we can get the number easily. 2508 * We will at most add split 2 refcount recor 2522 * We will at most add split 2 refcount records and 2 more 2509 * refcount blocks, so just check it in a rou 2523 * refcount blocks, so just check it in a rough way. 2510 * 2524 * 2511 * Caller must hold refcount tree lock. 2525 * Caller must hold refcount tree lock. 2512 */ 2526 */ 2513 int ocfs2_prepare_refcount_change_for_del(str 2527 int ocfs2_prepare_refcount_change_for_del(struct inode *inode, 2514 u64 2528 u64 refcount_loc, 2515 u64 2529 u64 phys_blkno, 2516 u32 2530 u32 clusters, 2517 int 2531 int *credits, 2518 int 2532 int *ref_blocks) 2519 { 2533 { 2520 int ret; 2534 int ret; 2521 struct buffer_head *ref_root_bh = NUL 2535 struct buffer_head *ref_root_bh = NULL; 2522 struct ocfs2_refcount_tree *tree; 2536 struct ocfs2_refcount_tree *tree; 2523 u64 start_cpos = ocfs2_blocks_to_clus 2537 u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno); 2524 2538 2525 if (!ocfs2_refcount_tree(OCFS2_SB(ino 2539 if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) { 2526 ret = ocfs2_error(inode->i_sb 2540 ret = ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n", 2527 inode->i_in 2541 inode->i_ino); 2528 goto out; 2542 goto out; 2529 } 2543 } 2530 2544 2531 BUG_ON(!ocfs2_is_refcount_inode(inode 2545 BUG_ON(!ocfs2_is_refcount_inode(inode)); 2532 2546 2533 ret = ocfs2_get_refcount_tree(OCFS2_S 2547 ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), 2534 refcoun 2548 refcount_loc, &tree); 2535 if (ret) { 2549 if (ret) { 2536 mlog_errno(ret); 2550 mlog_errno(ret); 2537 goto out; 2551 goto out; 2538 } 2552 } 2539 2553 2540 ret = ocfs2_read_refcount_block(&tree 2554 ret = ocfs2_read_refcount_block(&tree->rf_ci, refcount_loc, 2541 &ref_ 2555 &ref_root_bh); 2542 if (ret) { 2556 if (ret) { 2543 mlog_errno(ret); 2557 mlog_errno(ret); 2544 goto out; 2558 goto out; 2545 } 2559 } 2546 2560 2547 ret = ocfs2_calc_refcount_meta_credit 2561 ret = ocfs2_calc_refcount_meta_credits(inode->i_sb, 2548 2562 &tree->rf_ci, 2549 2563 ref_root_bh, 2550 2564 start_cpos, clusters, 2551 2565 ref_blocks, credits); 2552 if (ret) { 2566 if (ret) { 2553 mlog_errno(ret); 2567 mlog_errno(ret); 2554 goto out; 2568 goto out; 2555 } 2569 } 2556 2570 2557 trace_ocfs2_prepare_refcount_change_f 2571 trace_ocfs2_prepare_refcount_change_for_del(*ref_blocks, *credits); 2558 2572 2559 out: 2573 out: 2560 brelse(ref_root_bh); 2574 brelse(ref_root_bh); 2561 return ret; 2575 return ret; 2562 } 2576 } 2563 2577 2564 #define MAX_CONTIG_BYTES 1048576 2578 #define MAX_CONTIG_BYTES 1048576 2565 2579 2566 static inline unsigned int ocfs2_cow_contig_c 2580 static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb) 2567 { 2581 { 2568 return ocfs2_clusters_for_bytes(sb, M 2582 return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES); 2569 } 2583 } 2570 2584 2571 static inline unsigned int ocfs2_cow_contig_m 2585 static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb) 2572 { 2586 { 2573 return ~(ocfs2_cow_contig_clusters(sb 2587 return ~(ocfs2_cow_contig_clusters(sb) - 1); 2574 } 2588 } 2575 2589 2576 /* 2590 /* 2577 * Given an extent that starts at 'start' and 2591 * Given an extent that starts at 'start' and an I/O that starts at 'cpos', 2578 * find an offset (start + (n * contig_cluste 2592 * find an offset (start + (n * contig_clusters)) that is closest to cpos 2579 * while still being less than or equal to it 2593 * while still being less than or equal to it. 2580 * 2594 * 2581 * The goal is to break the extent at a multi 2595 * The goal is to break the extent at a multiple of contig_clusters. 2582 */ 2596 */ 2583 static inline unsigned int ocfs2_cow_align_st 2597 static inline unsigned int ocfs2_cow_align_start(struct super_block *sb, 2584 2598 unsigned int start, 2585 2599 unsigned int cpos) 2586 { 2600 { 2587 BUG_ON(start > cpos); 2601 BUG_ON(start > cpos); 2588 2602 2589 return start + ((cpos - start) & ocfs 2603 return start + ((cpos - start) & ocfs2_cow_contig_mask(sb)); 2590 } 2604 } 2591 2605 2592 /* 2606 /* 2593 * Given a cluster count of len, pad it out s 2607 * Given a cluster count of len, pad it out so that it is a multiple 2594 * of contig_clusters. 2608 * of contig_clusters. 2595 */ 2609 */ 2596 static inline unsigned int ocfs2_cow_align_le 2610 static inline unsigned int ocfs2_cow_align_length(struct super_block *sb, 2597 2611 unsigned int len) 2598 { 2612 { 2599 unsigned int padded = 2613 unsigned int padded = 2600 (len + (ocfs2_cow_contig_clus 2614 (len + (ocfs2_cow_contig_clusters(sb) - 1)) & 2601 ocfs2_cow_contig_mask(sb); 2615 ocfs2_cow_contig_mask(sb); 2602 2616 2603 /* Did we wrap? */ 2617 /* Did we wrap? */ 2604 if (padded < len) 2618 if (padded < len) 2605 padded = UINT_MAX; 2619 padded = UINT_MAX; 2606 2620 2607 return padded; 2621 return padded; 2608 } 2622 } 2609 2623 2610 /* 2624 /* 2611 * Calculate out the start and number of virt !! 2625 * Calculate out the start and number of virtual clusters we need to to CoW. 2612 * 2626 * 2613 * cpos is vitual start cluster position we w 2627 * cpos is vitual start cluster position we want to do CoW in a 2614 * file and write_len is the cluster length. 2628 * file and write_len is the cluster length. 2615 * max_cpos is the place where we want to sto 2629 * max_cpos is the place where we want to stop CoW intentionally. 2616 * 2630 * 2617 * Normal we will start CoW from the beginnin 2631 * Normal we will start CoW from the beginning of extent record cotaining cpos. 2618 * We try to break up extents on boundaries o 2632 * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we 2619 * get good I/O from the resulting extent tre 2633 * get good I/O from the resulting extent tree. 2620 */ 2634 */ 2621 static int ocfs2_refcount_cal_cow_clusters(st 2635 static int ocfs2_refcount_cal_cow_clusters(struct inode *inode, 2622 st 2636 struct ocfs2_extent_list *el, 2623 u3 2637 u32 cpos, 2624 u3 2638 u32 write_len, 2625 u3 2639 u32 max_cpos, 2626 u3 2640 u32 *cow_start, 2627 u3 2641 u32 *cow_len) 2628 { 2642 { 2629 int ret = 0; 2643 int ret = 0; 2630 int tree_height = le16_to_cpu(el->l_t 2644 int tree_height = le16_to_cpu(el->l_tree_depth), i; 2631 struct buffer_head *eb_bh = NULL; 2645 struct buffer_head *eb_bh = NULL; 2632 struct ocfs2_extent_block *eb = NULL; 2646 struct ocfs2_extent_block *eb = NULL; 2633 struct ocfs2_extent_rec *rec; 2647 struct ocfs2_extent_rec *rec; 2634 unsigned int want_clusters, rec_end = 2648 unsigned int want_clusters, rec_end = 0; 2635 int contig_clusters = ocfs2_cow_conti 2649 int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb); 2636 int leaf_clusters; 2650 int leaf_clusters; 2637 2651 2638 BUG_ON(cpos + write_len > max_cpos); 2652 BUG_ON(cpos + write_len > max_cpos); 2639 2653 2640 if (tree_height > 0) { 2654 if (tree_height > 0) { 2641 ret = ocfs2_find_leaf(INODE_C 2655 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh); 2642 if (ret) { 2656 if (ret) { 2643 mlog_errno(ret); 2657 mlog_errno(ret); 2644 goto out; 2658 goto out; 2645 } 2659 } 2646 2660 2647 eb = (struct ocfs2_extent_blo 2661 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 2648 el = &eb->h_list; 2662 el = &eb->h_list; 2649 2663 2650 if (el->l_tree_depth) { 2664 if (el->l_tree_depth) { 2651 ret = ocfs2_error(ino 2665 ret = ocfs2_error(inode->i_sb, 2652 "In 2666 "Inode %lu has non zero tree depth in leaf block %llu\n", 2653 ino 2667 inode->i_ino, 2654 (un 2668 (unsigned long long)eb_bh->b_blocknr); 2655 goto out; 2669 goto out; 2656 } 2670 } 2657 } 2671 } 2658 2672 2659 *cow_len = 0; 2673 *cow_len = 0; 2660 for (i = 0; i < le16_to_cpu(el->l_nex 2674 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 2661 rec = &el->l_recs[i]; 2675 rec = &el->l_recs[i]; 2662 2676 2663 if (ocfs2_is_empty_extent(rec 2677 if (ocfs2_is_empty_extent(rec)) { 2664 mlog_bug_on_msg(i != 2678 mlog_bug_on_msg(i != 0, "Inode %lu has empty record in " 2665 "inde 2679 "index %d\n", inode->i_ino, i); 2666 continue; 2680 continue; 2667 } 2681 } 2668 2682 2669 if (le32_to_cpu(rec->e_cpos) 2683 if (le32_to_cpu(rec->e_cpos) + 2670 le16_to_cpu(rec->e_leaf_c 2684 le16_to_cpu(rec->e_leaf_clusters) <= cpos) 2671 continue; 2685 continue; 2672 2686 2673 if (*cow_len == 0) { 2687 if (*cow_len == 0) { 2674 /* 2688 /* 2675 * We should find a r 2689 * We should find a refcounted record in the 2676 * first pass. 2690 * first pass. 2677 */ 2691 */ 2678 BUG_ON(!(rec->e_flags 2692 BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED)); 2679 *cow_start = le32_to_ 2693 *cow_start = le32_to_cpu(rec->e_cpos); 2680 } 2694 } 2681 2695 2682 /* 2696 /* 2683 * If we encounter a hole, a 2697 * If we encounter a hole, a non-refcounted record or 2684 * pass the max_cpos, stop th 2698 * pass the max_cpos, stop the search. 2685 */ 2699 */ 2686 if ((!(rec->e_flags & OCFS2_E 2700 if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) || 2687 (*cow_len && rec_end != l 2701 (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) || 2688 (max_cpos <= le32_to_cpu( 2702 (max_cpos <= le32_to_cpu(rec->e_cpos))) 2689 break; 2703 break; 2690 2704 2691 leaf_clusters = le16_to_cpu(r 2705 leaf_clusters = le16_to_cpu(rec->e_leaf_clusters); 2692 rec_end = le32_to_cpu(rec->e_ 2706 rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters; 2693 if (rec_end > max_cpos) { 2707 if (rec_end > max_cpos) { 2694 rec_end = max_cpos; 2708 rec_end = max_cpos; 2695 leaf_clusters = rec_e 2709 leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos); 2696 } 2710 } 2697 2711 2698 /* 2712 /* 2699 * How many clusters do we ac 2713 * How many clusters do we actually need from 2700 * this extent? First we see 2714 * this extent? First we see how many we actually 2701 * need to complete the write 2715 * need to complete the write. If that's smaller 2702 * than contig_clusters, we t 2716 * than contig_clusters, we try for contig_clusters. 2703 */ 2717 */ 2704 if (!*cow_len) 2718 if (!*cow_len) 2705 want_clusters = write 2719 want_clusters = write_len; 2706 else 2720 else 2707 want_clusters = (cpos 2721 want_clusters = (cpos + write_len) - 2708 (*cow_start + 2722 (*cow_start + *cow_len); 2709 if (want_clusters < contig_cl 2723 if (want_clusters < contig_clusters) 2710 want_clusters = conti 2724 want_clusters = contig_clusters; 2711 2725 2712 /* 2726 /* 2713 * If the write does not cove 2727 * If the write does not cover the whole extent, we 2714 * need to calculate how we'r 2728 * need to calculate how we're going to split the extent. 2715 * We try to do it on contig_ 2729 * We try to do it on contig_clusters boundaries. 2716 * 2730 * 2717 * Any extent smaller than co 2731 * Any extent smaller than contig_clusters will be 2718 * CoWed in its entirety. 2732 * CoWed in its entirety. 2719 */ 2733 */ 2720 if (leaf_clusters <= contig_c 2734 if (leaf_clusters <= contig_clusters) 2721 *cow_len += leaf_clus 2735 *cow_len += leaf_clusters; 2722 else if (*cow_len || (*cow_st 2736 else if (*cow_len || (*cow_start == cpos)) { 2723 /* 2737 /* 2724 * This extent needs 2738 * This extent needs to be CoW'd from its 2725 * beginning, so all 2739 * beginning, so all we have to do is compute 2726 * how many clusters 2740 * how many clusters to grab. We align 2727 * want_clusters to t 2741 * want_clusters to the edge of contig_clusters 2728 * to get better I/O. 2742 * to get better I/O. 2729 */ 2743 */ 2730 want_clusters = ocfs2 2744 want_clusters = ocfs2_cow_align_length(inode->i_sb, 2731 2745 want_clusters); 2732 2746 2733 if (leaf_clusters < w 2747 if (leaf_clusters < want_clusters) 2734 *cow_len += l 2748 *cow_len += leaf_clusters; 2735 else 2749 else 2736 *cow_len += w 2750 *cow_len += want_clusters; 2737 } else if ((*cow_start + cont 2751 } else if ((*cow_start + contig_clusters) >= 2738 (cpos + write_len) 2752 (cpos + write_len)) { 2739 /* 2753 /* 2740 * Breaking off conti 2754 * Breaking off contig_clusters at the front 2741 * of the extent will 2755 * of the extent will cover our write. That's 2742 * easy. 2756 * easy. 2743 */ 2757 */ 2744 *cow_len = contig_clu 2758 *cow_len = contig_clusters; 2745 } else if ((rec_end - cpos) < 2759 } else if ((rec_end - cpos) <= contig_clusters) { 2746 /* 2760 /* 2747 * Breaking off conti 2761 * Breaking off contig_clusters at the tail of 2748 * this extent will c 2762 * this extent will cover cpos. 2749 */ 2763 */ 2750 *cow_start = rec_end 2764 *cow_start = rec_end - contig_clusters; 2751 *cow_len = contig_clu 2765 *cow_len = contig_clusters; 2752 } else if ((rec_end - cpos) < 2766 } else if ((rec_end - cpos) <= want_clusters) { 2753 /* 2767 /* 2754 * While we can't fit 2768 * While we can't fit the entire write in this 2755 * extent, we know th 2769 * extent, we know that the write goes from cpos 2756 * to the end of the 2770 * to the end of the extent. Break that off. 2757 * We try to break it 2771 * We try to break it at some multiple of 2758 * contig_clusters fr 2772 * contig_clusters from the front of the extent. 2759 * Failing that (ie, 2773 * Failing that (ie, cpos is within 2760 * contig_clusters of 2774 * contig_clusters of the front), we'll CoW the 2761 * entire extent. 2775 * entire extent. 2762 */ 2776 */ 2763 *cow_start = ocfs2_co 2777 *cow_start = ocfs2_cow_align_start(inode->i_sb, 2764 2778 *cow_start, cpos); 2765 *cow_len = rec_end - 2779 *cow_len = rec_end - *cow_start; 2766 } else { 2780 } else { 2767 /* 2781 /* 2768 * Ok, the entire wri 2782 * Ok, the entire write lives in the middle of 2769 * this extent. Let' 2783 * this extent. Let's try to slice the extent up 2770 * nicely. Optimally 2784 * nicely. Optimally, our CoW region starts at 2771 * m*contig_clusters 2785 * m*contig_clusters from the beginning of the 2772 * extent and goes fo 2786 * extent and goes for n*contig_clusters, 2773 * covering the entir 2787 * covering the entire write. 2774 */ 2788 */ 2775 *cow_start = ocfs2_co 2789 *cow_start = ocfs2_cow_align_start(inode->i_sb, 2776 2790 *cow_start, cpos); 2777 2791 2778 want_clusters = (cpos 2792 want_clusters = (cpos + write_len) - *cow_start; 2779 want_clusters = ocfs2 2793 want_clusters = ocfs2_cow_align_length(inode->i_sb, 2780 2794 want_clusters); 2781 if (*cow_start + want 2795 if (*cow_start + want_clusters <= rec_end) 2782 *cow_len = wa 2796 *cow_len = want_clusters; 2783 else 2797 else 2784 *cow_len = re 2798 *cow_len = rec_end - *cow_start; 2785 } 2799 } 2786 2800 2787 /* Have we covered our entire 2801 /* Have we covered our entire write yet? */ 2788 if ((*cow_start + *cow_len) > 2802 if ((*cow_start + *cow_len) >= (cpos + write_len)) 2789 break; 2803 break; 2790 2804 2791 /* 2805 /* 2792 * If we reach the end of the 2806 * If we reach the end of the extent block and don't get enough 2793 * clusters, continue with th 2807 * clusters, continue with the next extent block if possible. 2794 */ 2808 */ 2795 if (i + 1 == le16_to_cpu(el-> 2809 if (i + 1 == le16_to_cpu(el->l_next_free_rec) && 2796 eb && eb->h_next_leaf_blk 2810 eb && eb->h_next_leaf_blk) { 2797 brelse(eb_bh); 2811 brelse(eb_bh); 2798 eb_bh = NULL; 2812 eb_bh = NULL; 2799 2813 2800 ret = ocfs2_read_exte 2814 ret = ocfs2_read_extent_block(INODE_CACHE(inode), 2801 2815 le64_to_cpu(eb->h_next_leaf_blk), 2802 2816 &eb_bh); 2803 if (ret) { 2817 if (ret) { 2804 mlog_errno(re 2818 mlog_errno(ret); 2805 goto out; 2819 goto out; 2806 } 2820 } 2807 2821 2808 eb = (struct ocfs2_ex 2822 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 2809 el = &eb->h_list; 2823 el = &eb->h_list; 2810 i = -1; 2824 i = -1; 2811 } 2825 } 2812 } 2826 } 2813 2827 2814 out: 2828 out: 2815 brelse(eb_bh); 2829 brelse(eb_bh); 2816 return ret; 2830 return ret; 2817 } 2831 } 2818 2832 2819 /* 2833 /* 2820 * Prepare meta_ac, data_ac and calculate cre 2834 * Prepare meta_ac, data_ac and calculate credits when we want to add some 2821 * num_clusters in data_tree "et" and change 2835 * num_clusters in data_tree "et" and change the refcount for the old 2822 * clusters(starting form p_cluster) in the r 2836 * clusters(starting form p_cluster) in the refcount tree. 2823 * 2837 * 2824 * Note: 2838 * Note: 2825 * 1. since we may split the old tree, so we 2839 * 1. since we may split the old tree, so we at most will need num_clusters + 2 2826 * more new leaf records. 2840 * more new leaf records. 2827 * 2. In some case, we may not need to reserv 2841 * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so 2828 * just give data_ac = NULL. 2842 * just give data_ac = NULL. 2829 */ 2843 */ 2830 static int ocfs2_lock_refcount_allocators(str 2844 static int ocfs2_lock_refcount_allocators(struct super_block *sb, 2831 u32 p 2845 u32 p_cluster, u32 num_clusters, 2832 struc 2846 struct ocfs2_extent_tree *et, 2833 struc 2847 struct ocfs2_caching_info *ref_ci, 2834 struc 2848 struct buffer_head *ref_root_bh, 2835 struc 2849 struct ocfs2_alloc_context **meta_ac, 2836 struc 2850 struct ocfs2_alloc_context **data_ac, 2837 int * 2851 int *credits) 2838 { 2852 { 2839 int ret = 0, meta_add = 0; 2853 int ret = 0, meta_add = 0; 2840 int num_free_extents = ocfs2_num_free 2854 int num_free_extents = ocfs2_num_free_extents(et); 2841 2855 2842 if (num_free_extents < 0) { 2856 if (num_free_extents < 0) { 2843 ret = num_free_extents; 2857 ret = num_free_extents; 2844 mlog_errno(ret); 2858 mlog_errno(ret); 2845 goto out; 2859 goto out; 2846 } 2860 } 2847 2861 2848 if (num_free_extents < num_clusters + 2862 if (num_free_extents < num_clusters + 2) 2849 meta_add = 2863 meta_add = 2850 ocfs2_extend_meta_nee 2864 ocfs2_extend_meta_needed(et->et_root_el); 2851 2865 2852 *credits += ocfs2_calc_extend_credits 2866 *credits += ocfs2_calc_extend_credits(sb, et->et_root_el); 2853 2867 2854 ret = ocfs2_calc_refcount_meta_credit 2868 ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh, 2855 2869 p_cluster, num_clusters, 2856 2870 &meta_add, credits); 2857 if (ret) { 2871 if (ret) { 2858 mlog_errno(ret); 2872 mlog_errno(ret); 2859 goto out; 2873 goto out; 2860 } 2874 } 2861 2875 2862 trace_ocfs2_lock_refcount_allocators( 2876 trace_ocfs2_lock_refcount_allocators(meta_add, *credits); 2863 ret = ocfs2_reserve_new_metadata_bloc 2877 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add, 2864 2878 meta_ac); 2865 if (ret) { 2879 if (ret) { 2866 mlog_errno(ret); 2880 mlog_errno(ret); 2867 goto out; 2881 goto out; 2868 } 2882 } 2869 2883 2870 if (data_ac) { 2884 if (data_ac) { 2871 ret = ocfs2_reserve_clusters( 2885 ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters, 2872 2886 data_ac); 2873 if (ret) 2887 if (ret) 2874 mlog_errno(ret); 2888 mlog_errno(ret); 2875 } 2889 } 2876 2890 2877 out: 2891 out: 2878 if (ret) { 2892 if (ret) { 2879 if (*meta_ac) { 2893 if (*meta_ac) { 2880 ocfs2_free_alloc_cont 2894 ocfs2_free_alloc_context(*meta_ac); 2881 *meta_ac = NULL; 2895 *meta_ac = NULL; 2882 } 2896 } 2883 } 2897 } 2884 2898 2885 return ret; 2899 return ret; 2886 } 2900 } 2887 2901 2888 static int ocfs2_clear_cow_buffer(handle_t *h 2902 static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh) 2889 { 2903 { 2890 BUG_ON(buffer_dirty(bh)); 2904 BUG_ON(buffer_dirty(bh)); 2891 2905 2892 clear_buffer_mapped(bh); 2906 clear_buffer_mapped(bh); 2893 2907 2894 return 0; 2908 return 0; 2895 } 2909 } 2896 2910 2897 int ocfs2_duplicate_clusters_by_page(handle_t 2911 int ocfs2_duplicate_clusters_by_page(handle_t *handle, 2898 struct i 2912 struct inode *inode, 2899 u32 cpos 2913 u32 cpos, u32 old_cluster, 2900 u32 new_ 2914 u32 new_cluster, u32 new_len) 2901 { 2915 { 2902 int ret = 0, partial; 2916 int ret = 0, partial; 2903 struct super_block *sb = inode->i_sb; 2917 struct super_block *sb = inode->i_sb; 2904 u64 new_block = ocfs2_clusters_to_blo 2918 u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster); 2905 struct page *page; 2919 struct page *page; 2906 pgoff_t page_index; 2920 pgoff_t page_index; 2907 unsigned int from, to; 2921 unsigned int from, to; 2908 loff_t offset, end, map_end; 2922 loff_t offset, end, map_end; 2909 struct address_space *mapping = inode 2923 struct address_space *mapping = inode->i_mapping; 2910 2924 2911 trace_ocfs2_duplicate_clusters_by_pag 2925 trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster, 2912 2926 new_cluster, new_len); 2913 2927 2914 offset = ((loff_t)cpos) << OCFS2_SB(s 2928 offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits; 2915 end = offset + (new_len << OCFS2_SB(s 2929 end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits); 2916 /* 2930 /* 2917 * We only duplicate pages until we r 2931 * We only duplicate pages until we reach the page contains i_size - 1. 2918 * So trim 'end' to i_size. 2932 * So trim 'end' to i_size. 2919 */ 2933 */ 2920 if (end > i_size_read(inode)) 2934 if (end > i_size_read(inode)) 2921 end = i_size_read(inode); 2935 end = i_size_read(inode); 2922 2936 2923 while (offset < end) { 2937 while (offset < end) { 2924 page_index = offset >> PAGE_S 2938 page_index = offset >> PAGE_SHIFT; 2925 map_end = ((loff_t)page_index 2939 map_end = ((loff_t)page_index + 1) << PAGE_SHIFT; 2926 if (map_end > end) 2940 if (map_end > end) 2927 map_end = end; 2941 map_end = end; 2928 2942 2929 /* from, to is the offset wit 2943 /* from, to is the offset within the page. */ 2930 from = offset & (PAGE_SIZE - 2944 from = offset & (PAGE_SIZE - 1); 2931 to = PAGE_SIZE; 2945 to = PAGE_SIZE; 2932 if (map_end & (PAGE_SIZE - 1) 2946 if (map_end & (PAGE_SIZE - 1)) 2933 to = map_end & (PAGE_ 2947 to = map_end & (PAGE_SIZE - 1); 2934 2948 2935 retry: 2949 retry: 2936 page = find_or_create_page(ma 2950 page = find_or_create_page(mapping, page_index, GFP_NOFS); 2937 if (!page) { 2951 if (!page) { 2938 ret = -ENOMEM; 2952 ret = -ENOMEM; 2939 mlog_errno(ret); 2953 mlog_errno(ret); 2940 break; 2954 break; 2941 } 2955 } 2942 2956 2943 /* 2957 /* 2944 * In case PAGE_SIZE <= CLUST 2958 * In case PAGE_SIZE <= CLUSTER_SIZE, we do not expect a dirty 2945 * page, so write it back. 2959 * page, so write it back. 2946 */ 2960 */ 2947 if (PAGE_SIZE <= OCFS2_SB(sb) 2961 if (PAGE_SIZE <= OCFS2_SB(sb)->s_clustersize) { 2948 if (PageDirty(page)) 2962 if (PageDirty(page)) { 2949 unlock_page(p !! 2963 /* 2950 put_page(page !! 2964 * write_on_page will unlock the page on return 2951 !! 2965 */ 2952 ret = filemap !! 2966 ret = write_one_page(page); 2953 << 2954 goto retry; 2967 goto retry; 2955 } 2968 } 2956 } 2969 } 2957 2970 2958 if (!PageUptodate(page)) { 2971 if (!PageUptodate(page)) { 2959 struct folio *folio = !! 2972 ret = block_read_full_page(page, ocfs2_get_block); 2960 << 2961 ret = block_read_full << 2962 if (ret) { 2973 if (ret) { 2963 mlog_errno(re 2974 mlog_errno(ret); 2964 goto unlock; 2975 goto unlock; 2965 } 2976 } 2966 folio_lock(folio); !! 2977 lock_page(page); 2967 } 2978 } 2968 2979 2969 if (page_has_buffers(page)) { 2980 if (page_has_buffers(page)) { 2970 ret = walk_page_buffe 2981 ret = walk_page_buffers(handle, page_buffers(page), 2971 2982 from, to, &partial, 2972 2983 ocfs2_clear_cow_buffer); 2973 if (ret) { 2984 if (ret) { 2974 mlog_errno(re 2985 mlog_errno(ret); 2975 goto unlock; 2986 goto unlock; 2976 } 2987 } 2977 } 2988 } 2978 2989 2979 ocfs2_map_and_dirty_page(inod 2990 ocfs2_map_and_dirty_page(inode, 2980 hand 2991 handle, from, to, 2981 page 2992 page, 0, &new_block); 2982 mark_page_accessed(page); 2993 mark_page_accessed(page); 2983 unlock: 2994 unlock: 2984 unlock_page(page); 2995 unlock_page(page); 2985 put_page(page); 2996 put_page(page); 2986 page = NULL; 2997 page = NULL; 2987 offset = map_end; 2998 offset = map_end; 2988 if (ret) 2999 if (ret) 2989 break; 3000 break; 2990 } 3001 } 2991 3002 2992 return ret; 3003 return ret; 2993 } 3004 } 2994 3005 2995 int ocfs2_duplicate_clusters_by_jbd(handle_t 3006 int ocfs2_duplicate_clusters_by_jbd(handle_t *handle, 2996 struct in 3007 struct inode *inode, 2997 u32 cpos, 3008 u32 cpos, u32 old_cluster, 2998 u32 new_c 3009 u32 new_cluster, u32 new_len) 2999 { 3010 { 3000 int ret = 0; 3011 int ret = 0; 3001 struct super_block *sb = inode->i_sb; 3012 struct super_block *sb = inode->i_sb; 3002 struct ocfs2_caching_info *ci = INODE 3013 struct ocfs2_caching_info *ci = INODE_CACHE(inode); 3003 int i, blocks = ocfs2_clusters_to_blo 3014 int i, blocks = ocfs2_clusters_to_blocks(sb, new_len); 3004 u64 old_block = ocfs2_clusters_to_blo 3015 u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster); 3005 u64 new_block = ocfs2_clusters_to_blo 3016 u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster); 3006 struct ocfs2_super *osb = OCFS2_SB(sb 3017 struct ocfs2_super *osb = OCFS2_SB(sb); 3007 struct buffer_head *old_bh = NULL; 3018 struct buffer_head *old_bh = NULL; 3008 struct buffer_head *new_bh = NULL; 3019 struct buffer_head *new_bh = NULL; 3009 3020 3010 trace_ocfs2_duplicate_clusters_by_pag 3021 trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster, 3011 3022 new_cluster, new_len); 3012 3023 3013 for (i = 0; i < blocks; i++, old_bloc 3024 for (i = 0; i < blocks; i++, old_block++, new_block++) { 3014 new_bh = sb_getblk(osb->sb, n 3025 new_bh = sb_getblk(osb->sb, new_block); 3015 if (new_bh == NULL) { 3026 if (new_bh == NULL) { 3016 ret = -ENOMEM; 3027 ret = -ENOMEM; 3017 mlog_errno(ret); 3028 mlog_errno(ret); 3018 break; 3029 break; 3019 } 3030 } 3020 3031 3021 ocfs2_set_new_buffer_uptodate 3032 ocfs2_set_new_buffer_uptodate(ci, new_bh); 3022 3033 3023 ret = ocfs2_read_block(ci, ol 3034 ret = ocfs2_read_block(ci, old_block, &old_bh, NULL); 3024 if (ret) { 3035 if (ret) { 3025 mlog_errno(ret); 3036 mlog_errno(ret); 3026 break; 3037 break; 3027 } 3038 } 3028 3039 3029 ret = ocfs2_journal_access(ha 3040 ret = ocfs2_journal_access(handle, ci, new_bh, 3030 OC 3041 OCFS2_JOURNAL_ACCESS_CREATE); 3031 if (ret) { 3042 if (ret) { 3032 mlog_errno(ret); 3043 mlog_errno(ret); 3033 break; 3044 break; 3034 } 3045 } 3035 3046 3036 memcpy(new_bh->b_data, old_bh 3047 memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize); 3037 ocfs2_journal_dirty(handle, n 3048 ocfs2_journal_dirty(handle, new_bh); 3038 3049 3039 brelse(new_bh); 3050 brelse(new_bh); 3040 brelse(old_bh); 3051 brelse(old_bh); 3041 new_bh = NULL; 3052 new_bh = NULL; 3042 old_bh = NULL; 3053 old_bh = NULL; 3043 } 3054 } 3044 3055 3045 brelse(new_bh); 3056 brelse(new_bh); 3046 brelse(old_bh); 3057 brelse(old_bh); 3047 return ret; 3058 return ret; 3048 } 3059 } 3049 3060 3050 static int ocfs2_clear_ext_refcount(handle_t 3061 static int ocfs2_clear_ext_refcount(handle_t *handle, 3051 struct oc 3062 struct ocfs2_extent_tree *et, 3052 u32 cpos, 3063 u32 cpos, u32 p_cluster, u32 len, 3053 unsigned 3064 unsigned int ext_flags, 3054 struct oc 3065 struct ocfs2_alloc_context *meta_ac, 3055 struct oc 3066 struct ocfs2_cached_dealloc_ctxt *dealloc) 3056 { 3067 { 3057 int ret, index; 3068 int ret, index; 3058 struct ocfs2_extent_rec replace_rec; 3069 struct ocfs2_extent_rec replace_rec; 3059 struct ocfs2_path *path = NULL; 3070 struct ocfs2_path *path = NULL; 3060 struct ocfs2_extent_list *el; 3071 struct ocfs2_extent_list *el; 3061 struct super_block *sb = ocfs2_metada 3072 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); 3062 u64 ino = ocfs2_metadata_cache_owner( 3073 u64 ino = ocfs2_metadata_cache_owner(et->et_ci); 3063 3074 3064 trace_ocfs2_clear_ext_refcount((unsig 3075 trace_ocfs2_clear_ext_refcount((unsigned long long)ino, 3065 cpos, 3076 cpos, len, p_cluster, ext_flags); 3066 3077 3067 memset(&replace_rec, 0, sizeof(replac 3078 memset(&replace_rec, 0, sizeof(replace_rec)); 3068 replace_rec.e_cpos = cpu_to_le32(cpos 3079 replace_rec.e_cpos = cpu_to_le32(cpos); 3069 replace_rec.e_leaf_clusters = cpu_to_ 3080 replace_rec.e_leaf_clusters = cpu_to_le16(len); 3070 replace_rec.e_blkno = cpu_to_le64(ocf 3081 replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb, 3071 3082 p_cluster)); 3072 replace_rec.e_flags = ext_flags; 3083 replace_rec.e_flags = ext_flags; 3073 replace_rec.e_flags &= ~OCFS2_EXT_REF 3084 replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED; 3074 3085 3075 path = ocfs2_new_path_from_et(et); 3086 path = ocfs2_new_path_from_et(et); 3076 if (!path) { 3087 if (!path) { 3077 ret = -ENOMEM; 3088 ret = -ENOMEM; 3078 mlog_errno(ret); 3089 mlog_errno(ret); 3079 goto out; 3090 goto out; 3080 } 3091 } 3081 3092 3082 ret = ocfs2_find_path(et->et_ci, path 3093 ret = ocfs2_find_path(et->et_ci, path, cpos); 3083 if (ret) { 3094 if (ret) { 3084 mlog_errno(ret); 3095 mlog_errno(ret); 3085 goto out; 3096 goto out; 3086 } 3097 } 3087 3098 3088 el = path_leaf_el(path); 3099 el = path_leaf_el(path); 3089 3100 3090 index = ocfs2_search_extent_list(el, 3101 index = ocfs2_search_extent_list(el, cpos); 3091 if (index == -1) { 3102 if (index == -1) { 3092 ret = ocfs2_error(sb, 3103 ret = ocfs2_error(sb, 3093 "Inode %llu 3104 "Inode %llu has an extent at cpos %u which can no longer be found\n", 3094 (unsigned l 3105 (unsigned long long)ino, cpos); 3095 goto out; 3106 goto out; 3096 } 3107 } 3097 3108 3098 ret = ocfs2_split_extent(handle, et, 3109 ret = ocfs2_split_extent(handle, et, path, index, 3099 &replace_rec 3110 &replace_rec, meta_ac, dealloc); 3100 if (ret) 3111 if (ret) 3101 mlog_errno(ret); 3112 mlog_errno(ret); 3102 3113 3103 out: 3114 out: 3104 ocfs2_free_path(path); 3115 ocfs2_free_path(path); 3105 return ret; 3116 return ret; 3106 } 3117 } 3107 3118 3108 static int ocfs2_replace_clusters(handle_t *h 3119 static int ocfs2_replace_clusters(handle_t *handle, 3109 struct ocfs 3120 struct ocfs2_cow_context *context, 3110 u32 cpos, u 3121 u32 cpos, u32 old, 3111 u32 new, u3 3122 u32 new, u32 len, 3112 unsigned in 3123 unsigned int ext_flags) 3113 { 3124 { 3114 int ret; 3125 int ret; 3115 struct ocfs2_caching_info *ci = conte 3126 struct ocfs2_caching_info *ci = context->data_et.et_ci; 3116 u64 ino = ocfs2_metadata_cache_owner( 3127 u64 ino = ocfs2_metadata_cache_owner(ci); 3117 3128 3118 trace_ocfs2_replace_clusters((unsigne 3129 trace_ocfs2_replace_clusters((unsigned long long)ino, 3119 cpos, ol 3130 cpos, old, new, len, ext_flags); 3120 3131 3121 /*If the old clusters is unwritten, n 3132 /*If the old clusters is unwritten, no need to duplicate. */ 3122 if (!(ext_flags & OCFS2_EXT_UNWRITTEN 3133 if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) { 3123 ret = context->cow_duplicate_ 3134 ret = context->cow_duplicate_clusters(handle, context->inode, 3124 3135 cpos, old, new, len); 3125 if (ret) { 3136 if (ret) { 3126 mlog_errno(ret); 3137 mlog_errno(ret); 3127 goto out; 3138 goto out; 3128 } 3139 } 3129 } 3140 } 3130 3141 3131 ret = ocfs2_clear_ext_refcount(handle 3142 ret = ocfs2_clear_ext_refcount(handle, &context->data_et, 3132 cpos, 3143 cpos, new, len, ext_flags, 3133 contex 3144 context->meta_ac, &context->dealloc); 3134 if (ret) 3145 if (ret) 3135 mlog_errno(ret); 3146 mlog_errno(ret); 3136 out: 3147 out: 3137 return ret; 3148 return ret; 3138 } 3149 } 3139 3150 3140 int ocfs2_cow_sync_writeback(struct super_blo 3151 int ocfs2_cow_sync_writeback(struct super_block *sb, 3141 struct inode *in 3152 struct inode *inode, 3142 u32 cpos, u32 nu 3153 u32 cpos, u32 num_clusters) 3143 { 3154 { 3144 int ret; !! 3155 int ret = 0; 3145 loff_t start, end; !! 3156 loff_t offset, end, map_end; >> 3157 pgoff_t page_index; >> 3158 struct page *page; 3146 3159 3147 if (ocfs2_should_order_data(inode)) 3160 if (ocfs2_should_order_data(inode)) 3148 return 0; 3161 return 0; 3149 3162 3150 start = ((loff_t)cpos) << OCFS2_SB(sb !! 3163 offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits; 3151 end = start + (num_clusters << OCFS2_ !! 3164 end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits); 3152 3165 3153 ret = filemap_write_and_wait_range(in !! 3166 ret = filemap_fdatawrite_range(inode->i_mapping, 3154 if (ret < 0) !! 3167 offset, end - 1); >> 3168 if (ret < 0) { 3155 mlog_errno(ret); 3169 mlog_errno(ret); >> 3170 return ret; >> 3171 } >> 3172 >> 3173 while (offset < end) { >> 3174 page_index = offset >> PAGE_SHIFT; >> 3175 map_end = ((loff_t)page_index + 1) << PAGE_SHIFT; >> 3176 if (map_end > end) >> 3177 map_end = end; >> 3178 >> 3179 page = find_or_create_page(inode->i_mapping, >> 3180 page_index, GFP_NOFS); >> 3181 BUG_ON(!page); >> 3182 >> 3183 wait_on_page_writeback(page); >> 3184 if (PageError(page)) { >> 3185 ret = -EIO; >> 3186 mlog_errno(ret); >> 3187 } else >> 3188 mark_page_accessed(page); >> 3189 >> 3190 unlock_page(page); >> 3191 put_page(page); >> 3192 page = NULL; >> 3193 offset = map_end; >> 3194 if (ret) >> 3195 break; >> 3196 } 3156 3197 3157 return ret; 3198 return ret; 3158 } 3199 } 3159 3200 3160 static int ocfs2_di_get_clusters(struct ocfs2 3201 static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context, 3161 u32 v_cluste 3202 u32 v_cluster, u32 *p_cluster, 3162 u32 *num_clu 3203 u32 *num_clusters, 3163 unsigned int 3204 unsigned int *extent_flags) 3164 { 3205 { 3165 return ocfs2_get_clusters(context->in 3206 return ocfs2_get_clusters(context->inode, v_cluster, p_cluster, 3166 num_cluster 3207 num_clusters, extent_flags); 3167 } 3208 } 3168 3209 3169 static int ocfs2_make_clusters_writable(struc 3210 static int ocfs2_make_clusters_writable(struct super_block *sb, 3170 struc 3211 struct ocfs2_cow_context *context, 3171 u32 c 3212 u32 cpos, u32 p_cluster, 3172 u32 n 3213 u32 num_clusters, unsigned int e_flags) 3173 { 3214 { 3174 int ret, delete, index, credits = 0; 3215 int ret, delete, index, credits = 0; 3175 u32 new_bit, new_len, orig_num_cluste 3216 u32 new_bit, new_len, orig_num_clusters; 3176 unsigned int set_len; 3217 unsigned int set_len; 3177 struct ocfs2_super *osb = OCFS2_SB(sb 3218 struct ocfs2_super *osb = OCFS2_SB(sb); 3178 handle_t *handle; 3219 handle_t *handle; 3179 struct buffer_head *ref_leaf_bh = NUL 3220 struct buffer_head *ref_leaf_bh = NULL; 3180 struct ocfs2_caching_info *ref_ci = & 3221 struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci; 3181 struct ocfs2_refcount_rec rec; 3222 struct ocfs2_refcount_rec rec; 3182 3223 3183 trace_ocfs2_make_clusters_writable(cp 3224 trace_ocfs2_make_clusters_writable(cpos, p_cluster, 3184 nu 3225 num_clusters, e_flags); 3185 3226 3186 ret = ocfs2_lock_refcount_allocators( 3227 ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters, 3187 3228 &context->data_et, 3188 3229 ref_ci, 3189 3230 context->ref_root_bh, 3190 3231 &context->meta_ac, 3191 3232 &context->data_ac, &credits); 3192 if (ret) { 3233 if (ret) { 3193 mlog_errno(ret); 3234 mlog_errno(ret); 3194 return ret; 3235 return ret; 3195 } 3236 } 3196 3237 3197 if (context->post_refcount) 3238 if (context->post_refcount) 3198 credits += context->post_refc 3239 credits += context->post_refcount->credits; 3199 3240 3200 credits += context->extra_credits; 3241 credits += context->extra_credits; 3201 handle = ocfs2_start_trans(osb, credi 3242 handle = ocfs2_start_trans(osb, credits); 3202 if (IS_ERR(handle)) { 3243 if (IS_ERR(handle)) { 3203 ret = PTR_ERR(handle); 3244 ret = PTR_ERR(handle); 3204 mlog_errno(ret); 3245 mlog_errno(ret); 3205 goto out; 3246 goto out; 3206 } 3247 } 3207 3248 3208 orig_num_clusters = num_clusters; 3249 orig_num_clusters = num_clusters; 3209 3250 3210 while (num_clusters) { 3251 while (num_clusters) { 3211 ret = ocfs2_get_refcount_rec( 3252 ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh, 3212 3253 p_cluster, num_clusters, 3213 3254 &rec, &index, &ref_leaf_bh); 3214 if (ret) { 3255 if (ret) { 3215 mlog_errno(ret); 3256 mlog_errno(ret); 3216 goto out_commit; 3257 goto out_commit; 3217 } 3258 } 3218 3259 3219 BUG_ON(!rec.r_refcount); 3260 BUG_ON(!rec.r_refcount); 3220 set_len = min((u64)p_cluster 3261 set_len = min((u64)p_cluster + num_clusters, 3221 le64_to_cpu(rec 3262 le64_to_cpu(rec.r_cpos) + 3222 le32_to_cpu(rec 3263 le32_to_cpu(rec.r_clusters)) - p_cluster; 3223 3264 3224 /* 3265 /* 3225 * There are many different s 3266 * There are many different situation here. 3226 * 1. If refcount == 1, remov 3267 * 1. If refcount == 1, remove the flag and don't COW. 3227 * 2. If refcount > 1, alloca 3268 * 2. If refcount > 1, allocate clusters. 3228 * Here we may not allocat 3269 * Here we may not allocate r_len once at a time, so continue 3229 * until we reach num_clus 3270 * until we reach num_clusters. 3230 */ 3271 */ 3231 if (le32_to_cpu(rec.r_refcoun 3272 if (le32_to_cpu(rec.r_refcount) == 1) { 3232 delete = 0; 3273 delete = 0; 3233 ret = ocfs2_clear_ext 3274 ret = ocfs2_clear_ext_refcount(handle, 3234 3275 &context->data_et, 3235 3276 cpos, p_cluster, 3236 3277 set_len, e_flags, 3237 3278 context->meta_ac, 3238 3279 &context->dealloc); 3239 if (ret) { 3280 if (ret) { 3240 mlog_errno(re 3281 mlog_errno(ret); 3241 goto out_comm 3282 goto out_commit; 3242 } 3283 } 3243 } else { 3284 } else { 3244 delete = 1; 3285 delete = 1; 3245 3286 3246 ret = __ocfs2_claim_c 3287 ret = __ocfs2_claim_clusters(handle, 3247 3288 context->data_ac, 3248 3289 1, set_len, 3249 3290 &new_bit, &new_len); 3250 if (ret) { 3291 if (ret) { 3251 mlog_errno(re 3292 mlog_errno(ret); 3252 goto out_comm 3293 goto out_commit; 3253 } 3294 } 3254 3295 3255 ret = ocfs2_replace_c 3296 ret = ocfs2_replace_clusters(handle, context, 3256 3297 cpos, p_cluster, new_bit, 3257 3298 new_len, e_flags); 3258 if (ret) { 3299 if (ret) { 3259 mlog_errno(re 3300 mlog_errno(ret); 3260 goto out_comm 3301 goto out_commit; 3261 } 3302 } 3262 set_len = new_len; 3303 set_len = new_len; 3263 } 3304 } 3264 3305 3265 ret = __ocfs2_decrease_refcou 3306 ret = __ocfs2_decrease_refcount(handle, ref_ci, 3266 3307 context->ref_root_bh, 3267 3308 p_cluster, set_len, 3268 3309 context->meta_ac, 3269 3310 &context->dealloc, delete); 3270 if (ret) { 3311 if (ret) { 3271 mlog_errno(ret); 3312 mlog_errno(ret); 3272 goto out_commit; 3313 goto out_commit; 3273 } 3314 } 3274 3315 3275 cpos += set_len; 3316 cpos += set_len; 3276 p_cluster += set_len; 3317 p_cluster += set_len; 3277 num_clusters -= set_len; 3318 num_clusters -= set_len; 3278 brelse(ref_leaf_bh); 3319 brelse(ref_leaf_bh); 3279 ref_leaf_bh = NULL; 3320 ref_leaf_bh = NULL; 3280 } 3321 } 3281 3322 3282 /* handle any post_cow action. */ 3323 /* handle any post_cow action. */ 3283 if (context->post_refcount && context 3324 if (context->post_refcount && context->post_refcount->func) { 3284 ret = context->post_refcount- 3325 ret = context->post_refcount->func(context->inode, handle, 3285 3326 context->post_refcount->para); 3286 if (ret) { 3327 if (ret) { 3287 mlog_errno(ret); 3328 mlog_errno(ret); 3288 goto out_commit; 3329 goto out_commit; 3289 } 3330 } 3290 } 3331 } 3291 3332 3292 /* 3333 /* 3293 * Here we should write the new page 3334 * Here we should write the new page out first if we are 3294 * in write-back mode. 3335 * in write-back mode. 3295 */ 3336 */ 3296 if (context->get_clusters == ocfs2_di 3337 if (context->get_clusters == ocfs2_di_get_clusters) { 3297 ret = ocfs2_cow_sync_writebac 3338 ret = ocfs2_cow_sync_writeback(sb, context->inode, cpos, 3298 3339 orig_num_clusters); 3299 if (ret) 3340 if (ret) 3300 mlog_errno(ret); 3341 mlog_errno(ret); 3301 } 3342 } 3302 3343 3303 out_commit: 3344 out_commit: 3304 ocfs2_commit_trans(osb, handle); 3345 ocfs2_commit_trans(osb, handle); 3305 3346 3306 out: 3347 out: 3307 if (context->data_ac) { 3348 if (context->data_ac) { 3308 ocfs2_free_alloc_context(cont 3349 ocfs2_free_alloc_context(context->data_ac); 3309 context->data_ac = NULL; 3350 context->data_ac = NULL; 3310 } 3351 } 3311 if (context->meta_ac) { 3352 if (context->meta_ac) { 3312 ocfs2_free_alloc_context(cont 3353 ocfs2_free_alloc_context(context->meta_ac); 3313 context->meta_ac = NULL; 3354 context->meta_ac = NULL; 3314 } 3355 } 3315 brelse(ref_leaf_bh); 3356 brelse(ref_leaf_bh); 3316 3357 3317 return ret; 3358 return ret; 3318 } 3359 } 3319 3360 3320 static int ocfs2_replace_cow(struct ocfs2_cow 3361 static int ocfs2_replace_cow(struct ocfs2_cow_context *context) 3321 { 3362 { 3322 int ret = 0; 3363 int ret = 0; 3323 struct inode *inode = context->inode; 3364 struct inode *inode = context->inode; 3324 u32 cow_start = context->cow_start, c 3365 u32 cow_start = context->cow_start, cow_len = context->cow_len; 3325 u32 p_cluster, num_clusters; 3366 u32 p_cluster, num_clusters; 3326 unsigned int ext_flags; 3367 unsigned int ext_flags; 3327 struct ocfs2_super *osb = OCFS2_SB(in 3368 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3328 3369 3329 if (!ocfs2_refcount_tree(osb)) { 3370 if (!ocfs2_refcount_tree(osb)) { 3330 return ocfs2_error(inode->i_s 3371 return ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n", 3331 inode->i_i 3372 inode->i_ino); 3332 } 3373 } 3333 3374 3334 ocfs2_init_dealloc_ctxt(&context->dea 3375 ocfs2_init_dealloc_ctxt(&context->dealloc); 3335 3376 3336 while (cow_len) { 3377 while (cow_len) { 3337 ret = context->get_clusters(c 3378 ret = context->get_clusters(context, cow_start, &p_cluster, 3338 & 3379 &num_clusters, &ext_flags); 3339 if (ret) { 3380 if (ret) { 3340 mlog_errno(ret); 3381 mlog_errno(ret); 3341 break; 3382 break; 3342 } 3383 } 3343 3384 3344 BUG_ON(!(ext_flags & OCFS2_EX 3385 BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED)); 3345 3386 3346 if (cow_len < num_clusters) 3387 if (cow_len < num_clusters) 3347 num_clusters = cow_le 3388 num_clusters = cow_len; 3348 3389 3349 ret = ocfs2_make_clusters_wri 3390 ret = ocfs2_make_clusters_writable(inode->i_sb, context, 3350 3391 cow_start, p_cluster, 3351 3392 num_clusters, ext_flags); 3352 if (ret) { 3393 if (ret) { 3353 mlog_errno(ret); 3394 mlog_errno(ret); 3354 break; 3395 break; 3355 } 3396 } 3356 3397 3357 cow_len -= num_clusters; 3398 cow_len -= num_clusters; 3358 cow_start += num_clusters; 3399 cow_start += num_clusters; 3359 } 3400 } 3360 3401 3361 if (ocfs2_dealloc_has_cluster(&contex 3402 if (ocfs2_dealloc_has_cluster(&context->dealloc)) { 3362 ocfs2_schedule_truncate_log_f 3403 ocfs2_schedule_truncate_log_flush(osb, 1); 3363 ocfs2_run_deallocs(osb, &cont 3404 ocfs2_run_deallocs(osb, &context->dealloc); 3364 } 3405 } 3365 3406 3366 return ret; 3407 return ret; 3367 } 3408 } 3368 3409 3369 /* 3410 /* 3370 * Starting at cpos, try to CoW write_len clu 3411 * Starting at cpos, try to CoW write_len clusters. Don't CoW 3371 * past max_cpos. This will stop when it run 3412 * past max_cpos. This will stop when it runs into a hole or an 3372 * unrefcounted extent. 3413 * unrefcounted extent. 3373 */ 3414 */ 3374 static int ocfs2_refcount_cow_hunk(struct ino 3415 static int ocfs2_refcount_cow_hunk(struct inode *inode, 3375 struct buf 3416 struct buffer_head *di_bh, 3376 u32 cpos, 3417 u32 cpos, u32 write_len, u32 max_cpos) 3377 { 3418 { 3378 int ret; 3419 int ret; 3379 u32 cow_start = 0, cow_len = 0; 3420 u32 cow_start = 0, cow_len = 0; 3380 struct ocfs2_super *osb = OCFS2_SB(in 3421 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3381 struct ocfs2_dinode *di = (struct ocf 3422 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3382 struct buffer_head *ref_root_bh = NUL 3423 struct buffer_head *ref_root_bh = NULL; 3383 struct ocfs2_refcount_tree *ref_tree; 3424 struct ocfs2_refcount_tree *ref_tree; 3384 struct ocfs2_cow_context *context = N 3425 struct ocfs2_cow_context *context = NULL; 3385 3426 3386 BUG_ON(!ocfs2_is_refcount_inode(inode 3427 BUG_ON(!ocfs2_is_refcount_inode(inode)); 3387 3428 3388 ret = ocfs2_refcount_cal_cow_clusters 3429 ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list, 3389 3430 cpos, write_len, max_cpos, 3390 3431 &cow_start, &cow_len); 3391 if (ret) { 3432 if (ret) { 3392 mlog_errno(ret); 3433 mlog_errno(ret); 3393 goto out; 3434 goto out; 3394 } 3435 } 3395 3436 3396 trace_ocfs2_refcount_cow_hunk(OCFS2_I 3437 trace_ocfs2_refcount_cow_hunk(OCFS2_I(inode)->ip_blkno, 3397 cpos, w 3438 cpos, write_len, max_cpos, 3398 cow_sta 3439 cow_start, cow_len); 3399 3440 3400 BUG_ON(cow_len == 0); 3441 BUG_ON(cow_len == 0); 3401 3442 3402 context = kzalloc(sizeof(struct ocfs2 3443 context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS); 3403 if (!context) { 3444 if (!context) { 3404 ret = -ENOMEM; 3445 ret = -ENOMEM; 3405 mlog_errno(ret); 3446 mlog_errno(ret); 3406 goto out; 3447 goto out; 3407 } 3448 } 3408 3449 3409 ret = ocfs2_lock_refcount_tree(osb, l 3450 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), 3410 1, &re 3451 1, &ref_tree, &ref_root_bh); 3411 if (ret) { 3452 if (ret) { 3412 mlog_errno(ret); 3453 mlog_errno(ret); 3413 goto out; 3454 goto out; 3414 } 3455 } 3415 3456 3416 context->inode = inode; 3457 context->inode = inode; 3417 context->cow_start = cow_start; 3458 context->cow_start = cow_start; 3418 context->cow_len = cow_len; 3459 context->cow_len = cow_len; 3419 context->ref_tree = ref_tree; 3460 context->ref_tree = ref_tree; 3420 context->ref_root_bh = ref_root_bh; 3461 context->ref_root_bh = ref_root_bh; 3421 context->cow_duplicate_clusters = ocf 3462 context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page; 3422 context->get_clusters = ocfs2_di_get_ 3463 context->get_clusters = ocfs2_di_get_clusters; 3423 3464 3424 ocfs2_init_dinode_extent_tree(&contex 3465 ocfs2_init_dinode_extent_tree(&context->data_et, 3425 INODE_C 3466 INODE_CACHE(inode), di_bh); 3426 3467 3427 ret = ocfs2_replace_cow(context); 3468 ret = ocfs2_replace_cow(context); 3428 if (ret) 3469 if (ret) 3429 mlog_errno(ret); 3470 mlog_errno(ret); 3430 3471 3431 /* 3472 /* 3432 * truncate the extent map here since 3473 * truncate the extent map here since no matter whether we meet with 3433 * any error during the action, we sh 3474 * any error during the action, we shouldn't trust cached extent map 3434 * any more. 3475 * any more. 3435 */ 3476 */ 3436 ocfs2_extent_map_trunc(inode, cow_sta 3477 ocfs2_extent_map_trunc(inode, cow_start); 3437 3478 3438 ocfs2_unlock_refcount_tree(osb, ref_t 3479 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 3439 brelse(ref_root_bh); 3480 brelse(ref_root_bh); 3440 out: 3481 out: 3441 kfree(context); 3482 kfree(context); 3442 return ret; 3483 return ret; 3443 } 3484 } 3444 3485 3445 /* 3486 /* 3446 * CoW any and all clusters between cpos and 3487 * CoW any and all clusters between cpos and cpos+write_len. 3447 * Don't CoW past max_cpos. If this returns 3488 * Don't CoW past max_cpos. If this returns successfully, all 3448 * clusters between cpos and cpos+write_len a 3489 * clusters between cpos and cpos+write_len are safe to modify. 3449 */ 3490 */ 3450 int ocfs2_refcount_cow(struct inode *inode, 3491 int ocfs2_refcount_cow(struct inode *inode, 3451 struct buffer_head *di 3492 struct buffer_head *di_bh, 3452 u32 cpos, u32 write_le 3493 u32 cpos, u32 write_len, u32 max_cpos) 3453 { 3494 { 3454 int ret = 0; 3495 int ret = 0; 3455 u32 p_cluster, num_clusters; 3496 u32 p_cluster, num_clusters; 3456 unsigned int ext_flags; 3497 unsigned int ext_flags; 3457 3498 3458 while (write_len) { 3499 while (write_len) { 3459 ret = ocfs2_get_clusters(inod 3500 ret = ocfs2_get_clusters(inode, cpos, &p_cluster, 3460 &num 3501 &num_clusters, &ext_flags); 3461 if (ret) { 3502 if (ret) { 3462 mlog_errno(ret); 3503 mlog_errno(ret); 3463 break; 3504 break; 3464 } 3505 } 3465 3506 3466 if (write_len < num_clusters) 3507 if (write_len < num_clusters) 3467 num_clusters = write_ 3508 num_clusters = write_len; 3468 3509 3469 if (ext_flags & OCFS2_EXT_REF 3510 if (ext_flags & OCFS2_EXT_REFCOUNTED) { 3470 ret = ocfs2_refcount_ 3511 ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos, 3471 3512 num_clusters, max_cpos); 3472 if (ret) { 3513 if (ret) { 3473 mlog_errno(re 3514 mlog_errno(ret); 3474 break; 3515 break; 3475 } 3516 } 3476 } 3517 } 3477 3518 3478 write_len -= num_clusters; 3519 write_len -= num_clusters; 3479 cpos += num_clusters; 3520 cpos += num_clusters; 3480 } 3521 } 3481 3522 3482 return ret; 3523 return ret; 3483 } 3524 } 3484 3525 3485 static int ocfs2_xattr_value_get_clusters(str 3526 static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context, 3486 u32 3527 u32 v_cluster, u32 *p_cluster, 3487 u32 3528 u32 *num_clusters, 3488 uns 3529 unsigned int *extent_flags) 3489 { 3530 { 3490 struct inode *inode = context->inode; 3531 struct inode *inode = context->inode; 3491 struct ocfs2_xattr_value_root *xv = c 3532 struct ocfs2_xattr_value_root *xv = context->cow_object; 3492 3533 3493 return ocfs2_xattr_get_clusters(inode 3534 return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster, 3494 num_c 3535 num_clusters, &xv->xr_list, 3495 exten 3536 extent_flags); 3496 } 3537 } 3497 3538 3498 /* 3539 /* 3499 * Given a xattr value root, calculate the mo 3540 * Given a xattr value root, calculate the most meta/credits we need for 3500 * refcount tree change if we truncate it to 3541 * refcount tree change if we truncate it to 0. 3501 */ 3542 */ 3502 int ocfs2_refcounted_xattr_delete_need(struct 3543 int ocfs2_refcounted_xattr_delete_need(struct inode *inode, 3503 struct 3544 struct ocfs2_caching_info *ref_ci, 3504 struct 3545 struct buffer_head *ref_root_bh, 3505 struct 3546 struct ocfs2_xattr_value_root *xv, 3506 int *m 3547 int *meta_add, int *credits) 3507 { 3548 { 3508 int ret = 0, index, ref_blocks = 0; 3549 int ret = 0, index, ref_blocks = 0; 3509 u32 p_cluster, num_clusters; 3550 u32 p_cluster, num_clusters; 3510 u32 cpos = 0, clusters = le32_to_cpu( 3551 u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters); 3511 struct ocfs2_refcount_block *rb; 3552 struct ocfs2_refcount_block *rb; 3512 struct ocfs2_refcount_rec rec; 3553 struct ocfs2_refcount_rec rec; 3513 struct buffer_head *ref_leaf_bh = NUL 3554 struct buffer_head *ref_leaf_bh = NULL; 3514 3555 3515 while (cpos < clusters) { 3556 while (cpos < clusters) { 3516 ret = ocfs2_xattr_get_cluster 3557 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, 3517 3558 &num_clusters, &xv->xr_list, 3518 3559 NULL); 3519 if (ret) { 3560 if (ret) { 3520 mlog_errno(ret); 3561 mlog_errno(ret); 3521 goto out; 3562 goto out; 3522 } 3563 } 3523 3564 3524 cpos += num_clusters; 3565 cpos += num_clusters; 3525 3566 3526 while (num_clusters) { 3567 while (num_clusters) { 3527 ret = ocfs2_get_refco 3568 ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh, 3528 3569 p_cluster, num_clusters, 3529 3570 &rec, &index, 3530 3571 &ref_leaf_bh); 3531 if (ret) { 3572 if (ret) { 3532 mlog_errno(re 3573 mlog_errno(ret); 3533 goto out; 3574 goto out; 3534 } 3575 } 3535 3576 3536 BUG_ON(!rec.r_refcoun 3577 BUG_ON(!rec.r_refcount); 3537 3578 3538 rb = (struct ocfs2_re 3579 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 3539 3580 3540 /* 3581 /* 3541 * We really don't kn 3582 * We really don't know whether the other clusters is in 3542 * this refcount bloc 3583 * this refcount block or not, so just take the worst 3543 * case that all the 3584 * case that all the clusters are in this block and each 3544 * one will split a r 3585 * one will split a refcount rec, so totally we need 3545 * clusters * 2 new r 3586 * clusters * 2 new refcount rec. 3546 */ 3587 */ 3547 if (le16_to_cpu(rb->r 3588 if (le16_to_cpu(rb->rf_records.rl_used) + clusters * 2 > 3548 le16_to_cpu(rb->r 3589 le16_to_cpu(rb->rf_records.rl_count)) 3549 ref_blocks++; 3590 ref_blocks++; 3550 3591 3551 *credits += 1; 3592 *credits += 1; 3552 brelse(ref_leaf_bh); 3593 brelse(ref_leaf_bh); 3553 ref_leaf_bh = NULL; 3594 ref_leaf_bh = NULL; 3554 3595 3555 if (num_clusters <= l 3596 if (num_clusters <= le32_to_cpu(rec.r_clusters)) 3556 break; 3597 break; 3557 else 3598 else 3558 num_clusters 3599 num_clusters -= le32_to_cpu(rec.r_clusters); 3559 p_cluster += num_clus 3600 p_cluster += num_clusters; 3560 } 3601 } 3561 } 3602 } 3562 3603 3563 *meta_add += ref_blocks; 3604 *meta_add += ref_blocks; 3564 if (!ref_blocks) 3605 if (!ref_blocks) 3565 goto out; 3606 goto out; 3566 3607 3567 rb = (struct ocfs2_refcount_block *)r 3608 rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 3568 if (le32_to_cpu(rb->rf_flags) & OCFS2 3609 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) 3569 *credits += OCFS2_EXPAND_REFC 3610 *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; 3570 else { 3611 else { 3571 struct ocfs2_extent_tree et; 3612 struct ocfs2_extent_tree et; 3572 3613 3573 ocfs2_init_refcount_extent_tr 3614 ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh); 3574 *credits += ocfs2_calc_extend 3615 *credits += ocfs2_calc_extend_credits(inode->i_sb, 3575 3616 et.et_root_el); 3576 } 3617 } 3577 3618 3578 out: 3619 out: 3579 brelse(ref_leaf_bh); 3620 brelse(ref_leaf_bh); 3580 return ret; 3621 return ret; 3581 } 3622 } 3582 3623 3583 /* 3624 /* 3584 * Do CoW for xattr. 3625 * Do CoW for xattr. 3585 */ 3626 */ 3586 int ocfs2_refcount_cow_xattr(struct inode *in 3627 int ocfs2_refcount_cow_xattr(struct inode *inode, 3587 struct ocfs2_din 3628 struct ocfs2_dinode *di, 3588 struct ocfs2_xat 3629 struct ocfs2_xattr_value_buf *vb, 3589 struct ocfs2_ref 3630 struct ocfs2_refcount_tree *ref_tree, 3590 struct buffer_he 3631 struct buffer_head *ref_root_bh, 3591 u32 cpos, u32 wr 3632 u32 cpos, u32 write_len, 3592 struct ocfs2_pos 3633 struct ocfs2_post_refcount *post) 3593 { 3634 { 3594 int ret; 3635 int ret; 3595 struct ocfs2_xattr_value_root *xv = v 3636 struct ocfs2_xattr_value_root *xv = vb->vb_xv; 3596 struct ocfs2_cow_context *context = N 3637 struct ocfs2_cow_context *context = NULL; 3597 u32 cow_start, cow_len; 3638 u32 cow_start, cow_len; 3598 3639 3599 BUG_ON(!ocfs2_is_refcount_inode(inode 3640 BUG_ON(!ocfs2_is_refcount_inode(inode)); 3600 3641 3601 ret = ocfs2_refcount_cal_cow_clusters 3642 ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list, 3602 3643 cpos, write_len, UINT_MAX, 3603 3644 &cow_start, &cow_len); 3604 if (ret) { 3645 if (ret) { 3605 mlog_errno(ret); 3646 mlog_errno(ret); 3606 goto out; 3647 goto out; 3607 } 3648 } 3608 3649 3609 BUG_ON(cow_len == 0); 3650 BUG_ON(cow_len == 0); 3610 3651 3611 context = kzalloc(sizeof(struct ocfs2 3652 context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS); 3612 if (!context) { 3653 if (!context) { 3613 ret = -ENOMEM; 3654 ret = -ENOMEM; 3614 mlog_errno(ret); 3655 mlog_errno(ret); 3615 goto out; 3656 goto out; 3616 } 3657 } 3617 3658 3618 context->inode = inode; 3659 context->inode = inode; 3619 context->cow_start = cow_start; 3660 context->cow_start = cow_start; 3620 context->cow_len = cow_len; 3661 context->cow_len = cow_len; 3621 context->ref_tree = ref_tree; 3662 context->ref_tree = ref_tree; 3622 context->ref_root_bh = ref_root_bh; 3663 context->ref_root_bh = ref_root_bh; 3623 context->cow_object = xv; 3664 context->cow_object = xv; 3624 3665 3625 context->cow_duplicate_clusters = ocf 3666 context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd; 3626 /* We need the extra credits for dupl 3667 /* We need the extra credits for duplicate_clusters by jbd. */ 3627 context->extra_credits = 3668 context->extra_credits = 3628 ocfs2_clusters_to_blocks(inod 3669 ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len; 3629 context->get_clusters = ocfs2_xattr_v 3670 context->get_clusters = ocfs2_xattr_value_get_clusters; 3630 context->post_refcount = post; 3671 context->post_refcount = post; 3631 3672 3632 ocfs2_init_xattr_value_extent_tree(&c 3673 ocfs2_init_xattr_value_extent_tree(&context->data_et, 3633 IN 3674 INODE_CACHE(inode), vb); 3634 3675 3635 ret = ocfs2_replace_cow(context); 3676 ret = ocfs2_replace_cow(context); 3636 if (ret) 3677 if (ret) 3637 mlog_errno(ret); 3678 mlog_errno(ret); 3638 3679 3639 out: 3680 out: 3640 kfree(context); 3681 kfree(context); 3641 return ret; 3682 return ret; 3642 } 3683 } 3643 3684 3644 /* 3685 /* 3645 * Insert a new extent into refcount tree and 3686 * Insert a new extent into refcount tree and mark a extent rec 3646 * as refcounted in the dinode tree. 3687 * as refcounted in the dinode tree. 3647 */ 3688 */ 3648 int ocfs2_add_refcount_flag(struct inode *ino 3689 int ocfs2_add_refcount_flag(struct inode *inode, 3649 struct ocfs2_exte 3690 struct ocfs2_extent_tree *data_et, 3650 struct ocfs2_cach 3691 struct ocfs2_caching_info *ref_ci, 3651 struct buffer_hea 3692 struct buffer_head *ref_root_bh, 3652 u32 cpos, u32 p_c 3693 u32 cpos, u32 p_cluster, u32 num_clusters, 3653 struct ocfs2_cach 3694 struct ocfs2_cached_dealloc_ctxt *dealloc, 3654 struct ocfs2_post 3695 struct ocfs2_post_refcount *post) 3655 { 3696 { 3656 int ret; 3697 int ret; 3657 handle_t *handle; 3698 handle_t *handle; 3658 int credits = 1, ref_blocks = 0; 3699 int credits = 1, ref_blocks = 0; 3659 struct ocfs2_super *osb = OCFS2_SB(in 3700 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3660 struct ocfs2_alloc_context *meta_ac = 3701 struct ocfs2_alloc_context *meta_ac = NULL; 3661 3702 3662 /* We need to be able to handle at le 3703 /* We need to be able to handle at least an extent tree split. */ 3663 ref_blocks = ocfs2_extend_meta_needed 3704 ref_blocks = ocfs2_extend_meta_needed(data_et->et_root_el); 3664 3705 3665 ret = ocfs2_calc_refcount_meta_credit 3706 ret = ocfs2_calc_refcount_meta_credits(inode->i_sb, 3666 3707 ref_ci, ref_root_bh, 3667 3708 p_cluster, num_clusters, 3668 3709 &ref_blocks, &credits); 3669 if (ret) { 3710 if (ret) { 3670 mlog_errno(ret); 3711 mlog_errno(ret); 3671 goto out; 3712 goto out; 3672 } 3713 } 3673 3714 3674 trace_ocfs2_add_refcount_flag(ref_blo 3715 trace_ocfs2_add_refcount_flag(ref_blocks, credits); 3675 3716 3676 if (ref_blocks) { 3717 if (ref_blocks) { 3677 ret = ocfs2_reserve_new_metad 3718 ret = ocfs2_reserve_new_metadata_blocks(osb, 3678 3719 ref_blocks, &meta_ac); 3679 if (ret) { 3720 if (ret) { 3680 mlog_errno(ret); 3721 mlog_errno(ret); 3681 goto out; 3722 goto out; 3682 } 3723 } 3683 } 3724 } 3684 3725 3685 if (post) 3726 if (post) 3686 credits += post->credits; 3727 credits += post->credits; 3687 3728 3688 handle = ocfs2_start_trans(osb, credi 3729 handle = ocfs2_start_trans(osb, credits); 3689 if (IS_ERR(handle)) { 3730 if (IS_ERR(handle)) { 3690 ret = PTR_ERR(handle); 3731 ret = PTR_ERR(handle); 3691 mlog_errno(ret); 3732 mlog_errno(ret); 3692 goto out; 3733 goto out; 3693 } 3734 } 3694 3735 3695 ret = ocfs2_mark_extent_refcounted(in 3736 ret = ocfs2_mark_extent_refcounted(inode, data_et, handle, 3696 cp 3737 cpos, num_clusters, p_cluster, 3697 me 3738 meta_ac, dealloc); 3698 if (ret) { 3739 if (ret) { 3699 mlog_errno(ret); 3740 mlog_errno(ret); 3700 goto out_commit; 3741 goto out_commit; 3701 } 3742 } 3702 3743 3703 ret = __ocfs2_increase_refcount(handl 3744 ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh, 3704 p_clu 3745 p_cluster, num_clusters, 0, 3705 meta_ 3746 meta_ac, dealloc); 3706 if (ret) { 3747 if (ret) { 3707 mlog_errno(ret); 3748 mlog_errno(ret); 3708 goto out_commit; 3749 goto out_commit; 3709 } 3750 } 3710 3751 3711 if (post && post->func) { 3752 if (post && post->func) { 3712 ret = post->func(inode, handl 3753 ret = post->func(inode, handle, post->para); 3713 if (ret) 3754 if (ret) 3714 mlog_errno(ret); 3755 mlog_errno(ret); 3715 } 3756 } 3716 3757 3717 out_commit: 3758 out_commit: 3718 ocfs2_commit_trans(osb, handle); 3759 ocfs2_commit_trans(osb, handle); 3719 out: 3760 out: 3720 if (meta_ac) 3761 if (meta_ac) 3721 ocfs2_free_alloc_context(meta 3762 ocfs2_free_alloc_context(meta_ac); 3722 return ret; 3763 return ret; 3723 } 3764 } 3724 3765 3725 static int ocfs2_change_ctime(struct inode *i 3766 static int ocfs2_change_ctime(struct inode *inode, 3726 struct buffer_h 3767 struct buffer_head *di_bh) 3727 { 3768 { 3728 int ret; 3769 int ret; 3729 handle_t *handle; 3770 handle_t *handle; 3730 struct ocfs2_dinode *di = (struct ocf 3771 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3731 3772 3732 handle = ocfs2_start_trans(OCFS2_SB(i 3773 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), 3733 OCFS2_INOD 3774 OCFS2_INODE_UPDATE_CREDITS); 3734 if (IS_ERR(handle)) { 3775 if (IS_ERR(handle)) { 3735 ret = PTR_ERR(handle); 3776 ret = PTR_ERR(handle); 3736 mlog_errno(ret); 3777 mlog_errno(ret); 3737 goto out; 3778 goto out; 3738 } 3779 } 3739 3780 3740 ret = ocfs2_journal_access_di(handle, 3781 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 3741 OCFS2_J 3782 OCFS2_JOURNAL_ACCESS_WRITE); 3742 if (ret) { 3783 if (ret) { 3743 mlog_errno(ret); 3784 mlog_errno(ret); 3744 goto out_commit; 3785 goto out_commit; 3745 } 3786 } 3746 3787 3747 inode_set_ctime_current(inode); !! 3788 inode->i_ctime = current_time(inode); 3748 di->i_ctime = cpu_to_le64(inode_get_c !! 3789 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 3749 di->i_ctime_nsec = cpu_to_le32(inode_ !! 3790 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 3750 3791 3751 ocfs2_journal_dirty(handle, di_bh); 3792 ocfs2_journal_dirty(handle, di_bh); 3752 3793 3753 out_commit: 3794 out_commit: 3754 ocfs2_commit_trans(OCFS2_SB(inode->i_ 3795 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); 3755 out: 3796 out: 3756 return ret; 3797 return ret; 3757 } 3798 } 3758 3799 3759 static int ocfs2_attach_refcount_tree(struct 3800 static int ocfs2_attach_refcount_tree(struct inode *inode, 3760 struct 3801 struct buffer_head *di_bh) 3761 { 3802 { 3762 int ret, data_changed = 0; 3803 int ret, data_changed = 0; 3763 struct buffer_head *ref_root_bh = NUL 3804 struct buffer_head *ref_root_bh = NULL; 3764 struct ocfs2_inode_info *oi = OCFS2_I 3805 struct ocfs2_inode_info *oi = OCFS2_I(inode); 3765 struct ocfs2_dinode *di = (struct ocf 3806 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3766 struct ocfs2_super *osb = OCFS2_SB(in 3807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3767 struct ocfs2_refcount_tree *ref_tree; 3808 struct ocfs2_refcount_tree *ref_tree; 3768 unsigned int ext_flags; 3809 unsigned int ext_flags; 3769 loff_t size; 3810 loff_t size; 3770 u32 cpos, num_clusters, clusters, p_c 3811 u32 cpos, num_clusters, clusters, p_cluster; 3771 struct ocfs2_cached_dealloc_ctxt deal 3812 struct ocfs2_cached_dealloc_ctxt dealloc; 3772 struct ocfs2_extent_tree di_et; 3813 struct ocfs2_extent_tree di_et; 3773 3814 3774 ocfs2_init_dealloc_ctxt(&dealloc); 3815 ocfs2_init_dealloc_ctxt(&dealloc); 3775 3816 3776 if (!ocfs2_is_refcount_inode(inode)) 3817 if (!ocfs2_is_refcount_inode(inode)) { 3777 ret = ocfs2_create_refcount_t 3818 ret = ocfs2_create_refcount_tree(inode, di_bh); 3778 if (ret) { 3819 if (ret) { 3779 mlog_errno(ret); 3820 mlog_errno(ret); 3780 goto out; 3821 goto out; 3781 } 3822 } 3782 } 3823 } 3783 3824 3784 BUG_ON(!di->i_refcount_loc); 3825 BUG_ON(!di->i_refcount_loc); 3785 ret = ocfs2_lock_refcount_tree(osb, 3826 ret = ocfs2_lock_refcount_tree(osb, 3786 le64_t 3827 le64_to_cpu(di->i_refcount_loc), 1, 3787 &ref_t 3828 &ref_tree, &ref_root_bh); 3788 if (ret) { 3829 if (ret) { 3789 mlog_errno(ret); 3830 mlog_errno(ret); 3790 goto out; 3831 goto out; 3791 } 3832 } 3792 3833 3793 if (oi->ip_dyn_features & OCFS2_INLIN 3834 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) 3794 goto attach_xattr; 3835 goto attach_xattr; 3795 3836 3796 ocfs2_init_dinode_extent_tree(&di_et, 3837 ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh); 3797 3838 3798 size = i_size_read(inode); 3839 size = i_size_read(inode); 3799 clusters = ocfs2_clusters_for_bytes(i 3840 clusters = ocfs2_clusters_for_bytes(inode->i_sb, size); 3800 3841 3801 cpos = 0; 3842 cpos = 0; 3802 while (cpos < clusters) { 3843 while (cpos < clusters) { 3803 ret = ocfs2_get_clusters(inod 3844 ret = ocfs2_get_clusters(inode, cpos, &p_cluster, 3804 &num 3845 &num_clusters, &ext_flags); 3805 if (ret) { 3846 if (ret) { 3806 mlog_errno(ret); 3847 mlog_errno(ret); 3807 goto unlock; 3848 goto unlock; 3808 } 3849 } 3809 if (p_cluster && !(ext_flags 3850 if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) { 3810 ret = ocfs2_add_refco 3851 ret = ocfs2_add_refcount_flag(inode, &di_et, 3811 3852 &ref_tree->rf_ci, 3812 3853 ref_root_bh, cpos, 3813 3854 p_cluster, num_clusters, 3814 3855 &dealloc, NULL); 3815 if (ret) { 3856 if (ret) { 3816 mlog_errno(re 3857 mlog_errno(ret); 3817 goto unlock; 3858 goto unlock; 3818 } 3859 } 3819 3860 3820 data_changed = 1; 3861 data_changed = 1; 3821 } 3862 } 3822 cpos += num_clusters; 3863 cpos += num_clusters; 3823 } 3864 } 3824 3865 3825 attach_xattr: 3866 attach_xattr: 3826 if (oi->ip_dyn_features & OCFS2_HAS_X 3867 if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) { 3827 ret = ocfs2_xattr_attach_refc 3868 ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh, 3828 3869 &ref_tree->rf_ci, 3829 3870 ref_root_bh, 3830 3871 &dealloc); 3831 if (ret) { 3872 if (ret) { 3832 mlog_errno(ret); 3873 mlog_errno(ret); 3833 goto unlock; 3874 goto unlock; 3834 } 3875 } 3835 } 3876 } 3836 3877 3837 if (data_changed) { 3878 if (data_changed) { 3838 ret = ocfs2_change_ctime(inod 3879 ret = ocfs2_change_ctime(inode, di_bh); 3839 if (ret) 3880 if (ret) 3840 mlog_errno(ret); 3881 mlog_errno(ret); 3841 } 3882 } 3842 3883 3843 unlock: 3884 unlock: 3844 ocfs2_unlock_refcount_tree(osb, ref_t 3885 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 3845 brelse(ref_root_bh); 3886 brelse(ref_root_bh); 3846 3887 3847 if (!ret && ocfs2_dealloc_has_cluster 3888 if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) { 3848 ocfs2_schedule_truncate_log_f 3889 ocfs2_schedule_truncate_log_flush(osb, 1); 3849 ocfs2_run_deallocs(osb, &deal 3890 ocfs2_run_deallocs(osb, &dealloc); 3850 } 3891 } 3851 out: 3892 out: 3852 /* 3893 /* 3853 * Empty the extent map so that we ma 3894 * Empty the extent map so that we may get the right extent 3854 * record from the disk. 3895 * record from the disk. 3855 */ 3896 */ 3856 ocfs2_extent_map_trunc(inode, 0); 3897 ocfs2_extent_map_trunc(inode, 0); 3857 3898 3858 return ret; 3899 return ret; 3859 } 3900 } 3860 3901 3861 static int ocfs2_add_refcounted_extent(struct 3902 static int ocfs2_add_refcounted_extent(struct inode *inode, 3862 struct ocf 3903 struct ocfs2_extent_tree *et, 3863 struct ocf 3904 struct ocfs2_caching_info *ref_ci, 3864 struct buf 3905 struct buffer_head *ref_root_bh, 3865 u32 cpos, 3906 u32 cpos, u32 p_cluster, u32 num_clusters, 3866 unsigned i 3907 unsigned int ext_flags, 3867 struct ocf 3908 struct ocfs2_cached_dealloc_ctxt *dealloc) 3868 { 3909 { 3869 int ret; 3910 int ret; 3870 handle_t *handle; 3911 handle_t *handle; 3871 int credits = 0; 3912 int credits = 0; 3872 struct ocfs2_super *osb = OCFS2_SB(in 3913 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3873 struct ocfs2_alloc_context *meta_ac = 3914 struct ocfs2_alloc_context *meta_ac = NULL; 3874 3915 3875 ret = ocfs2_lock_refcount_allocators( 3916 ret = ocfs2_lock_refcount_allocators(inode->i_sb, 3876 3917 p_cluster, num_clusters, 3877 3918 et, ref_ci, 3878 3919 ref_root_bh, &meta_ac, 3879 3920 NULL, &credits); 3880 if (ret) { 3921 if (ret) { 3881 mlog_errno(ret); 3922 mlog_errno(ret); 3882 goto out; 3923 goto out; 3883 } 3924 } 3884 3925 3885 handle = ocfs2_start_trans(osb, credi 3926 handle = ocfs2_start_trans(osb, credits); 3886 if (IS_ERR(handle)) { 3927 if (IS_ERR(handle)) { 3887 ret = PTR_ERR(handle); 3928 ret = PTR_ERR(handle); 3888 mlog_errno(ret); 3929 mlog_errno(ret); 3889 goto out; 3930 goto out; 3890 } 3931 } 3891 3932 3892 ret = ocfs2_insert_extent(handle, et, 3933 ret = ocfs2_insert_extent(handle, et, cpos, 3893 ocfs2_clusters_to_blo 3934 ocfs2_clusters_to_blocks(inode->i_sb, p_cluster), 3894 num_clusters, ext_fla 3935 num_clusters, ext_flags, meta_ac); 3895 if (ret) { 3936 if (ret) { 3896 mlog_errno(ret); 3937 mlog_errno(ret); 3897 goto out_commit; 3938 goto out_commit; 3898 } 3939 } 3899 3940 3900 ret = ocfs2_increase_refcount(handle, 3941 ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh, 3901 p_clust 3942 p_cluster, num_clusters, 3902 meta_ac 3943 meta_ac, dealloc); 3903 if (ret) { 3944 if (ret) { 3904 mlog_errno(ret); 3945 mlog_errno(ret); 3905 goto out_commit; 3946 goto out_commit; 3906 } 3947 } 3907 3948 3908 ret = dquot_alloc_space_nodirty(inode 3949 ret = dquot_alloc_space_nodirty(inode, 3909 ocfs2_clusters_to_bytes(osb-> 3950 ocfs2_clusters_to_bytes(osb->sb, num_clusters)); 3910 if (ret) 3951 if (ret) 3911 mlog_errno(ret); 3952 mlog_errno(ret); 3912 3953 3913 out_commit: 3954 out_commit: 3914 ocfs2_commit_trans(osb, handle); 3955 ocfs2_commit_trans(osb, handle); 3915 out: 3956 out: 3916 if (meta_ac) 3957 if (meta_ac) 3917 ocfs2_free_alloc_context(meta 3958 ocfs2_free_alloc_context(meta_ac); 3918 return ret; 3959 return ret; 3919 } 3960 } 3920 3961 3921 static int ocfs2_duplicate_inline_data(struct 3962 static int ocfs2_duplicate_inline_data(struct inode *s_inode, 3922 struct 3963 struct buffer_head *s_bh, 3923 struct 3964 struct inode *t_inode, 3924 struct 3965 struct buffer_head *t_bh) 3925 { 3966 { 3926 int ret; 3967 int ret; 3927 handle_t *handle; 3968 handle_t *handle; 3928 struct ocfs2_super *osb = OCFS2_SB(s_ 3969 struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb); 3929 struct ocfs2_dinode *s_di = (struct o 3970 struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data; 3930 struct ocfs2_dinode *t_di = (struct o 3971 struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data; 3931 3972 3932 BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_fea 3973 BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)); 3933 3974 3934 handle = ocfs2_start_trans(osb, OCFS2 3975 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 3935 if (IS_ERR(handle)) { 3976 if (IS_ERR(handle)) { 3936 ret = PTR_ERR(handle); 3977 ret = PTR_ERR(handle); 3937 mlog_errno(ret); 3978 mlog_errno(ret); 3938 goto out; 3979 goto out; 3939 } 3980 } 3940 3981 3941 ret = ocfs2_journal_access_di(handle, 3982 ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh, 3942 OCFS2_J 3983 OCFS2_JOURNAL_ACCESS_WRITE); 3943 if (ret) { 3984 if (ret) { 3944 mlog_errno(ret); 3985 mlog_errno(ret); 3945 goto out_commit; 3986 goto out_commit; 3946 } 3987 } 3947 3988 3948 t_di->id2.i_data.id_count = s_di->id2 3989 t_di->id2.i_data.id_count = s_di->id2.i_data.id_count; 3949 memcpy(t_di->id2.i_data.id_data, s_di 3990 memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data, 3950 le16_to_cpu(s_di->id2.i_data.i 3991 le16_to_cpu(s_di->id2.i_data.id_count)); 3951 spin_lock(&OCFS2_I(t_inode)->ip_lock) 3992 spin_lock(&OCFS2_I(t_inode)->ip_lock); 3952 OCFS2_I(t_inode)->ip_dyn_features |= 3993 OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL; 3953 t_di->i_dyn_features = cpu_to_le16(OC 3994 t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features); 3954 spin_unlock(&OCFS2_I(t_inode)->ip_loc 3995 spin_unlock(&OCFS2_I(t_inode)->ip_lock); 3955 3996 3956 ocfs2_journal_dirty(handle, t_bh); 3997 ocfs2_journal_dirty(handle, t_bh); 3957 3998 3958 out_commit: 3999 out_commit: 3959 ocfs2_commit_trans(osb, handle); 4000 ocfs2_commit_trans(osb, handle); 3960 out: 4001 out: 3961 return ret; 4002 return ret; 3962 } 4003 } 3963 4004 3964 static int ocfs2_duplicate_extent_list(struct 4005 static int ocfs2_duplicate_extent_list(struct inode *s_inode, 3965 struct inode 4006 struct inode *t_inode, 3966 struct buffer 4007 struct buffer_head *t_bh, 3967 struct ocfs2_ 4008 struct ocfs2_caching_info *ref_ci, 3968 struct buffer 4009 struct buffer_head *ref_root_bh, 3969 struct ocfs2_ 4010 struct ocfs2_cached_dealloc_ctxt *dealloc) 3970 { 4011 { 3971 int ret = 0; 4012 int ret = 0; 3972 u32 p_cluster, num_clusters, clusters 4013 u32 p_cluster, num_clusters, clusters, cpos; 3973 loff_t size; 4014 loff_t size; 3974 unsigned int ext_flags; 4015 unsigned int ext_flags; 3975 struct ocfs2_extent_tree et; 4016 struct ocfs2_extent_tree et; 3976 4017 3977 ocfs2_init_dinode_extent_tree(&et, IN 4018 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh); 3978 4019 3979 size = i_size_read(s_inode); 4020 size = i_size_read(s_inode); 3980 clusters = ocfs2_clusters_for_bytes(s 4021 clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size); 3981 4022 3982 cpos = 0; 4023 cpos = 0; 3983 while (cpos < clusters) { 4024 while (cpos < clusters) { 3984 ret = ocfs2_get_clusters(s_in 4025 ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster, 3985 &num 4026 &num_clusters, &ext_flags); 3986 if (ret) { 4027 if (ret) { 3987 mlog_errno(ret); 4028 mlog_errno(ret); 3988 goto out; 4029 goto out; 3989 } 4030 } 3990 if (p_cluster) { 4031 if (p_cluster) { 3991 ret = ocfs2_add_refco 4032 ret = ocfs2_add_refcounted_extent(t_inode, &et, 3992 4033 ref_ci, ref_root_bh, 3993 4034 cpos, p_cluster, 3994 4035 num_clusters, 3995 4036 ext_flags, 3996 4037 dealloc); 3997 if (ret) { 4038 if (ret) { 3998 mlog_errno(re 4039 mlog_errno(ret); 3999 goto out; 4040 goto out; 4000 } 4041 } 4001 } 4042 } 4002 4043 4003 cpos += num_clusters; 4044 cpos += num_clusters; 4004 } 4045 } 4005 4046 4006 out: 4047 out: 4007 return ret; 4048 return ret; 4008 } 4049 } 4009 4050 4010 /* 4051 /* 4011 * change the new file's attributes to the sr 4052 * change the new file's attributes to the src. 4012 * 4053 * 4013 * reflink creates a snapshot of a file, that 4054 * reflink creates a snapshot of a file, that means the attributes 4014 * must be identical except for three excepti 4055 * must be identical except for three exceptions - nlink, ino, and ctime. 4015 */ 4056 */ 4016 static int ocfs2_complete_reflink(struct inod 4057 static int ocfs2_complete_reflink(struct inode *s_inode, 4017 struct buff 4058 struct buffer_head *s_bh, 4018 struct inod 4059 struct inode *t_inode, 4019 struct buff 4060 struct buffer_head *t_bh, 4020 bool preser 4061 bool preserve) 4021 { 4062 { 4022 int ret; 4063 int ret; 4023 handle_t *handle; 4064 handle_t *handle; 4024 struct ocfs2_dinode *s_di = (struct o 4065 struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data; 4025 struct ocfs2_dinode *di = (struct ocf 4066 struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data; 4026 loff_t size = i_size_read(s_inode); 4067 loff_t size = i_size_read(s_inode); 4027 4068 4028 handle = ocfs2_start_trans(OCFS2_SB(t 4069 handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb), 4029 OCFS2_INOD 4070 OCFS2_INODE_UPDATE_CREDITS); 4030 if (IS_ERR(handle)) { 4071 if (IS_ERR(handle)) { 4031 ret = PTR_ERR(handle); 4072 ret = PTR_ERR(handle); 4032 mlog_errno(ret); 4073 mlog_errno(ret); 4033 return ret; 4074 return ret; 4034 } 4075 } 4035 4076 4036 ret = ocfs2_journal_access_di(handle, 4077 ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh, 4037 OCFS2_J 4078 OCFS2_JOURNAL_ACCESS_WRITE); 4038 if (ret) { 4079 if (ret) { 4039 mlog_errno(ret); 4080 mlog_errno(ret); 4040 goto out_commit; 4081 goto out_commit; 4041 } 4082 } 4042 4083 4043 spin_lock(&OCFS2_I(t_inode)->ip_lock) 4084 spin_lock(&OCFS2_I(t_inode)->ip_lock); 4044 OCFS2_I(t_inode)->ip_clusters = OCFS2 4085 OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters; 4045 OCFS2_I(t_inode)->ip_attr = OCFS2_I(s 4086 OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr; 4046 OCFS2_I(t_inode)->ip_dyn_features = O 4087 OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features; 4047 spin_unlock(&OCFS2_I(t_inode)->ip_loc 4088 spin_unlock(&OCFS2_I(t_inode)->ip_lock); 4048 i_size_write(t_inode, size); 4089 i_size_write(t_inode, size); 4049 t_inode->i_blocks = s_inode->i_blocks 4090 t_inode->i_blocks = s_inode->i_blocks; 4050 4091 4051 di->i_xattr_inline_size = s_di->i_xat 4092 di->i_xattr_inline_size = s_di->i_xattr_inline_size; 4052 di->i_clusters = s_di->i_clusters; 4093 di->i_clusters = s_di->i_clusters; 4053 di->i_size = s_di->i_size; 4094 di->i_size = s_di->i_size; 4054 di->i_dyn_features = s_di->i_dyn_feat 4095 di->i_dyn_features = s_di->i_dyn_features; 4055 di->i_attr = s_di->i_attr; 4096 di->i_attr = s_di->i_attr; 4056 4097 4057 if (preserve) { 4098 if (preserve) { 4058 t_inode->i_uid = s_inode->i_u 4099 t_inode->i_uid = s_inode->i_uid; 4059 t_inode->i_gid = s_inode->i_g 4100 t_inode->i_gid = s_inode->i_gid; 4060 t_inode->i_mode = s_inode->i_ 4101 t_inode->i_mode = s_inode->i_mode; 4061 di->i_uid = s_di->i_uid; 4102 di->i_uid = s_di->i_uid; 4062 di->i_gid = s_di->i_gid; 4103 di->i_gid = s_di->i_gid; 4063 di->i_mode = s_di->i_mode; 4104 di->i_mode = s_di->i_mode; 4064 4105 4065 /* 4106 /* 4066 * update time. 4107 * update time. 4067 * we want mtime to appear id 4108 * we want mtime to appear identical to the source and 4068 * update ctime. 4109 * update ctime. 4069 */ 4110 */ 4070 inode_set_ctime_current(t_ino !! 4111 t_inode->i_ctime = current_time(t_inode); 4071 4112 4072 di->i_ctime = cpu_to_le64(ino !! 4113 di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec); 4073 di->i_ctime_nsec = cpu_to_le3 !! 4114 di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec); 4074 4115 4075 inode_set_mtime_to_ts(t_inode !! 4116 t_inode->i_mtime = s_inode->i_mtime; 4076 di->i_mtime = s_di->i_mtime; 4117 di->i_mtime = s_di->i_mtime; 4077 di->i_mtime_nsec = s_di->i_mt 4118 di->i_mtime_nsec = s_di->i_mtime_nsec; 4078 } 4119 } 4079 4120 4080 ocfs2_journal_dirty(handle, t_bh); 4121 ocfs2_journal_dirty(handle, t_bh); 4081 4122 4082 out_commit: 4123 out_commit: 4083 ocfs2_commit_trans(OCFS2_SB(t_inode-> 4124 ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle); 4084 return ret; 4125 return ret; 4085 } 4126 } 4086 4127 4087 static int ocfs2_create_reflink_node(struct i 4128 static int ocfs2_create_reflink_node(struct inode *s_inode, 4088 struct b 4129 struct buffer_head *s_bh, 4089 struct i 4130 struct inode *t_inode, 4090 struct b 4131 struct buffer_head *t_bh, 4091 bool pre 4132 bool preserve) 4092 { 4133 { 4093 int ret; 4134 int ret; 4094 struct buffer_head *ref_root_bh = NUL 4135 struct buffer_head *ref_root_bh = NULL; 4095 struct ocfs2_cached_dealloc_ctxt deal 4136 struct ocfs2_cached_dealloc_ctxt dealloc; 4096 struct ocfs2_super *osb = OCFS2_SB(s_ 4137 struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb); 4097 struct ocfs2_dinode *di = (struct ocf 4138 struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data; 4098 struct ocfs2_refcount_tree *ref_tree; 4139 struct ocfs2_refcount_tree *ref_tree; 4099 4140 4100 ocfs2_init_dealloc_ctxt(&dealloc); 4141 ocfs2_init_dealloc_ctxt(&dealloc); 4101 4142 4102 ret = ocfs2_set_refcount_tree(t_inode 4143 ret = ocfs2_set_refcount_tree(t_inode, t_bh, 4103 le64_to 4144 le64_to_cpu(di->i_refcount_loc)); 4104 if (ret) { 4145 if (ret) { 4105 mlog_errno(ret); 4146 mlog_errno(ret); 4106 goto out; 4147 goto out; 4107 } 4148 } 4108 4149 4109 if (OCFS2_I(s_inode)->ip_dyn_features 4150 if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 4110 ret = ocfs2_duplicate_inline_ 4151 ret = ocfs2_duplicate_inline_data(s_inode, s_bh, 4111 4152 t_inode, t_bh); 4112 if (ret) 4153 if (ret) 4113 mlog_errno(ret); 4154 mlog_errno(ret); 4114 goto out; 4155 goto out; 4115 } 4156 } 4116 4157 4117 ret = ocfs2_lock_refcount_tree(osb, l 4158 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), 4118 1, &re 4159 1, &ref_tree, &ref_root_bh); 4119 if (ret) { 4160 if (ret) { 4120 mlog_errno(ret); 4161 mlog_errno(ret); 4121 goto out; 4162 goto out; 4122 } 4163 } 4123 4164 4124 ret = ocfs2_duplicate_extent_list(s_i 4165 ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh, 4125 &re 4166 &ref_tree->rf_ci, ref_root_bh, 4126 &de 4167 &dealloc); 4127 if (ret) { 4168 if (ret) { 4128 mlog_errno(ret); 4169 mlog_errno(ret); 4129 goto out_unlock_refcount; 4170 goto out_unlock_refcount; 4130 } 4171 } 4131 4172 4132 out_unlock_refcount: 4173 out_unlock_refcount: 4133 ocfs2_unlock_refcount_tree(osb, ref_t 4174 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 4134 brelse(ref_root_bh); 4175 brelse(ref_root_bh); 4135 out: 4176 out: 4136 if (ocfs2_dealloc_has_cluster(&deallo 4177 if (ocfs2_dealloc_has_cluster(&dealloc)) { 4137 ocfs2_schedule_truncate_log_f 4178 ocfs2_schedule_truncate_log_flush(osb, 1); 4138 ocfs2_run_deallocs(osb, &deal 4179 ocfs2_run_deallocs(osb, &dealloc); 4139 } 4180 } 4140 4181 4141 return ret; 4182 return ret; 4142 } 4183 } 4143 4184 4144 static int __ocfs2_reflink(struct dentry *old 4185 static int __ocfs2_reflink(struct dentry *old_dentry, 4145 struct buffer_head 4186 struct buffer_head *old_bh, 4146 struct inode *new_ 4187 struct inode *new_inode, 4147 bool preserve) 4188 bool preserve) 4148 { 4189 { 4149 int ret; 4190 int ret; 4150 struct inode *inode = d_inode(old_den 4191 struct inode *inode = d_inode(old_dentry); 4151 struct buffer_head *new_bh = NULL; 4192 struct buffer_head *new_bh = NULL; 4152 struct ocfs2_inode_info *oi = OCFS2_I << 4153 4193 4154 if (oi->ip_flags & OCFS2_INODE_SYSTEM !! 4194 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) { 4155 ret = -EINVAL; 4195 ret = -EINVAL; 4156 mlog_errno(ret); 4196 mlog_errno(ret); 4157 goto out; 4197 goto out; 4158 } 4198 } 4159 4199 4160 ret = filemap_fdatawrite(inode->i_map 4200 ret = filemap_fdatawrite(inode->i_mapping); 4161 if (ret) { 4201 if (ret) { 4162 mlog_errno(ret); 4202 mlog_errno(ret); 4163 goto out; 4203 goto out; 4164 } 4204 } 4165 4205 4166 ret = ocfs2_attach_refcount_tree(inod 4206 ret = ocfs2_attach_refcount_tree(inode, old_bh); 4167 if (ret) { 4207 if (ret) { 4168 mlog_errno(ret); 4208 mlog_errno(ret); 4169 goto out; 4209 goto out; 4170 } 4210 } 4171 4211 4172 inode_lock_nested(new_inode, I_MUTEX_ 4212 inode_lock_nested(new_inode, I_MUTEX_CHILD); 4173 ret = ocfs2_inode_lock_nested(new_ino 4213 ret = ocfs2_inode_lock_nested(new_inode, &new_bh, 1, 4174 OI_LS_R 4214 OI_LS_REFLINK_TARGET); 4175 if (ret) { 4215 if (ret) { 4176 mlog_errno(ret); 4216 mlog_errno(ret); 4177 goto out_unlock; 4217 goto out_unlock; 4178 } 4218 } 4179 4219 4180 if ((oi->ip_dyn_features & OCFS2_HAS_ << 4181 (oi->ip_dyn_features & OCFS2_INLI << 4182 /* << 4183 * Adjust extent record count << 4184 * Inline data count had been << 4185 */ << 4186 struct ocfs2_inode_info *new_ << 4187 << 4188 if (!(new_oi->ip_dyn_features << 4189 !(ocfs2_inode_is_fast_sym << 4190 struct ocfs2_dinode * << 4191 struct ocfs2_dinode * << 4192 struct ocfs2_extent_l << 4193 int inline_size = le1 << 4194 << 4195 le16_add_cpu(&el->l_c << 4196 sizeo << 4197 } << 4198 } << 4199 << 4200 ret = ocfs2_create_reflink_node(inode 4220 ret = ocfs2_create_reflink_node(inode, old_bh, 4201 new_i 4221 new_inode, new_bh, preserve); 4202 if (ret) { 4222 if (ret) { 4203 mlog_errno(ret); 4223 mlog_errno(ret); 4204 goto inode_unlock; 4224 goto inode_unlock; 4205 } 4225 } 4206 4226 4207 if (oi->ip_dyn_features & OCFS2_HAS_X !! 4227 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_XATTR_FL) { 4208 ret = ocfs2_reflink_xattrs(in 4228 ret = ocfs2_reflink_xattrs(inode, old_bh, 4209 ne 4229 new_inode, new_bh, 4210 pr 4230 preserve); 4211 if (ret) { 4231 if (ret) { 4212 mlog_errno(ret); 4232 mlog_errno(ret); 4213 goto inode_unlock; 4233 goto inode_unlock; 4214 } 4234 } 4215 } 4235 } 4216 4236 4217 ret = ocfs2_complete_reflink(inode, o 4237 ret = ocfs2_complete_reflink(inode, old_bh, 4218 new_inod 4238 new_inode, new_bh, preserve); 4219 if (ret) 4239 if (ret) 4220 mlog_errno(ret); 4240 mlog_errno(ret); 4221 4241 4222 inode_unlock: 4242 inode_unlock: 4223 ocfs2_inode_unlock(new_inode, 1); 4243 ocfs2_inode_unlock(new_inode, 1); 4224 brelse(new_bh); 4244 brelse(new_bh); 4225 out_unlock: 4245 out_unlock: 4226 inode_unlock(new_inode); 4246 inode_unlock(new_inode); 4227 out: 4247 out: 4228 if (!ret) { 4248 if (!ret) { 4229 ret = filemap_fdatawait(inode 4249 ret = filemap_fdatawait(inode->i_mapping); 4230 if (ret) 4250 if (ret) 4231 mlog_errno(ret); 4251 mlog_errno(ret); 4232 } 4252 } 4233 return ret; 4253 return ret; 4234 } 4254 } 4235 4255 4236 static int ocfs2_reflink(struct dentry *old_d 4256 static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir, 4237 struct dentry *new_d 4257 struct dentry *new_dentry, bool preserve) 4238 { 4258 { 4239 int error, had_lock; 4259 int error, had_lock; 4240 struct inode *inode = d_inode(old_den 4260 struct inode *inode = d_inode(old_dentry); 4241 struct buffer_head *old_bh = NULL; 4261 struct buffer_head *old_bh = NULL; 4242 struct inode *new_orphan_inode = NULL 4262 struct inode *new_orphan_inode = NULL; 4243 struct ocfs2_lock_holder oh; 4263 struct ocfs2_lock_holder oh; 4244 4264 4245 if (!ocfs2_refcount_tree(OCFS2_SB(ino 4265 if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) 4246 return -EOPNOTSUPP; 4266 return -EOPNOTSUPP; 4247 4267 4248 4268 4249 error = ocfs2_create_inode_in_orphan( 4269 error = ocfs2_create_inode_in_orphan(dir, inode->i_mode, 4250 4270 &new_orphan_inode); 4251 if (error) { 4271 if (error) { 4252 mlog_errno(error); 4272 mlog_errno(error); 4253 goto out; 4273 goto out; 4254 } 4274 } 4255 4275 4256 error = ocfs2_rw_lock(inode, 1); 4276 error = ocfs2_rw_lock(inode, 1); 4257 if (error) { 4277 if (error) { 4258 mlog_errno(error); 4278 mlog_errno(error); 4259 goto out; 4279 goto out; 4260 } 4280 } 4261 4281 4262 error = ocfs2_inode_lock(inode, &old_ 4282 error = ocfs2_inode_lock(inode, &old_bh, 1); 4263 if (error) { 4283 if (error) { 4264 mlog_errno(error); 4284 mlog_errno(error); 4265 ocfs2_rw_unlock(inode, 1); 4285 ocfs2_rw_unlock(inode, 1); 4266 goto out; 4286 goto out; 4267 } 4287 } 4268 4288 4269 down_write(&OCFS2_I(inode)->ip_xattr_ 4289 down_write(&OCFS2_I(inode)->ip_xattr_sem); 4270 down_write(&OCFS2_I(inode)->ip_alloc_ 4290 down_write(&OCFS2_I(inode)->ip_alloc_sem); 4271 error = __ocfs2_reflink(old_dentry, o 4291 error = __ocfs2_reflink(old_dentry, old_bh, 4272 new_orphan_in 4292 new_orphan_inode, preserve); 4273 up_write(&OCFS2_I(inode)->ip_alloc_se 4293 up_write(&OCFS2_I(inode)->ip_alloc_sem); 4274 up_write(&OCFS2_I(inode)->ip_xattr_se 4294 up_write(&OCFS2_I(inode)->ip_xattr_sem); 4275 4295 4276 ocfs2_inode_unlock(inode, 1); 4296 ocfs2_inode_unlock(inode, 1); 4277 ocfs2_rw_unlock(inode, 1); 4297 ocfs2_rw_unlock(inode, 1); 4278 brelse(old_bh); 4298 brelse(old_bh); 4279 4299 4280 if (error) { 4300 if (error) { 4281 mlog_errno(error); 4301 mlog_errno(error); 4282 goto out; 4302 goto out; 4283 } 4303 } 4284 4304 4285 had_lock = ocfs2_inode_lock_tracker(n 4305 had_lock = ocfs2_inode_lock_tracker(new_orphan_inode, NULL, 1, 4286 & 4306 &oh); 4287 if (had_lock < 0) { 4307 if (had_lock < 0) { 4288 error = had_lock; 4308 error = had_lock; 4289 mlog_errno(error); 4309 mlog_errno(error); 4290 goto out; 4310 goto out; 4291 } 4311 } 4292 4312 4293 /* If the security isn't preserved, w 4313 /* If the security isn't preserved, we need to re-initialize them. */ 4294 if (!preserve) { 4314 if (!preserve) { 4295 error = ocfs2_init_security_a 4315 error = ocfs2_init_security_and_acl(dir, new_orphan_inode, 4296 4316 &new_dentry->d_name); 4297 if (error) 4317 if (error) 4298 mlog_errno(error); 4318 mlog_errno(error); 4299 } 4319 } 4300 if (!error) { 4320 if (!error) { 4301 error = ocfs2_mv_orphaned_ino 4321 error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode, 4302 4322 new_dentry); 4303 if (error) 4323 if (error) 4304 mlog_errno(error); 4324 mlog_errno(error); 4305 } 4325 } 4306 ocfs2_inode_unlock_tracker(new_orphan 4326 ocfs2_inode_unlock_tracker(new_orphan_inode, 1, &oh, had_lock); 4307 4327 4308 out: 4328 out: 4309 if (new_orphan_inode) { 4329 if (new_orphan_inode) { 4310 /* 4330 /* 4311 * We need to open_unlock the 4331 * We need to open_unlock the inode no matter whether we 4312 * succeed or not, so that ot 4332 * succeed or not, so that other nodes can delete it later. 4313 */ 4333 */ 4314 ocfs2_open_unlock(new_orphan_ 4334 ocfs2_open_unlock(new_orphan_inode); 4315 if (error) 4335 if (error) 4316 iput(new_orphan_inode 4336 iput(new_orphan_inode); 4317 } 4337 } 4318 4338 4319 return error; 4339 return error; 4320 } 4340 } 4321 4341 4322 /* 4342 /* 4323 * Below here are the bits used by OCFS2_IOC_ 4343 * Below here are the bits used by OCFS2_IOC_REFLINK() to fake 4324 * sys_reflink(). This will go away when vfs 4344 * sys_reflink(). This will go away when vfs_reflink() exists in 4325 * fs/namei.c. 4345 * fs/namei.c. 4326 */ 4346 */ 4327 4347 4328 /* copied from may_create in VFS. */ 4348 /* copied from may_create in VFS. */ 4329 static inline int ocfs2_may_create(struct ino 4349 static inline int ocfs2_may_create(struct inode *dir, struct dentry *child) 4330 { 4350 { 4331 if (d_really_is_positive(child)) 4351 if (d_really_is_positive(child)) 4332 return -EEXIST; 4352 return -EEXIST; 4333 if (IS_DEADDIR(dir)) 4353 if (IS_DEADDIR(dir)) 4334 return -ENOENT; 4354 return -ENOENT; 4335 return inode_permission(&nop_mnt_idma !! 4355 return inode_permission(dir, MAY_WRITE | MAY_EXEC); 4336 } 4356 } 4337 4357 4338 /** 4358 /** 4339 * ocfs2_vfs_reflink - Create a reference-cou 4359 * ocfs2_vfs_reflink - Create a reference-counted link 4340 * 4360 * 4341 * @old_dentry: source dentry + inode 4361 * @old_dentry: source dentry + inode 4342 * @dir: directory to create the target 4362 * @dir: directory to create the target 4343 * @new_dentry: target dentry 4363 * @new_dentry: target dentry 4344 * @preserve: if true, preserve all file att 4364 * @preserve: if true, preserve all file attributes 4345 */ 4365 */ 4346 static int ocfs2_vfs_reflink(struct dentry *o 4366 static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir, 4347 struct dentry *n 4367 struct dentry *new_dentry, bool preserve) 4348 { 4368 { 4349 struct inode *inode = d_inode(old_den 4369 struct inode *inode = d_inode(old_dentry); 4350 int error; 4370 int error; 4351 4371 4352 if (!inode) 4372 if (!inode) 4353 return -ENOENT; 4373 return -ENOENT; 4354 4374 4355 error = ocfs2_may_create(dir, new_den 4375 error = ocfs2_may_create(dir, new_dentry); 4356 if (error) 4376 if (error) 4357 return error; 4377 return error; 4358 4378 4359 if (dir->i_sb != inode->i_sb) 4379 if (dir->i_sb != inode->i_sb) 4360 return -EXDEV; 4380 return -EXDEV; 4361 4381 4362 /* 4382 /* 4363 * A reflink to an append-only or imm 4383 * A reflink to an append-only or immutable file cannot be created. 4364 */ 4384 */ 4365 if (IS_APPEND(inode) || IS_IMMUTABLE( 4385 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 4366 return -EPERM; 4386 return -EPERM; 4367 4387 4368 /* Only regular files can be reflinke 4388 /* Only regular files can be reflinked. */ 4369 if (!S_ISREG(inode->i_mode)) 4389 if (!S_ISREG(inode->i_mode)) 4370 return -EPERM; 4390 return -EPERM; 4371 4391 4372 /* 4392 /* 4373 * If the caller wants to preserve ow 4393 * If the caller wants to preserve ownership, they require the 4374 * rights to do so. 4394 * rights to do so. 4375 */ 4395 */ 4376 if (preserve) { 4396 if (preserve) { 4377 if (!uid_eq(current_fsuid(), 4397 if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_CHOWN)) 4378 return -EPERM; 4398 return -EPERM; 4379 if (!in_group_p(inode->i_gid) 4399 if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN)) 4380 return -EPERM; 4400 return -EPERM; 4381 } 4401 } 4382 4402 4383 /* 4403 /* 4384 * If the caller is modifying any asp 4404 * If the caller is modifying any aspect of the attributes, they 4385 * are not creating a snapshot. They 4405 * are not creating a snapshot. They need read permission on the 4386 * file. 4406 * file. 4387 */ 4407 */ 4388 if (!preserve) { 4408 if (!preserve) { 4389 error = inode_permission(&nop !! 4409 error = inode_permission(inode, MAY_READ); 4390 if (error) 4410 if (error) 4391 return error; 4411 return error; 4392 } 4412 } 4393 4413 4394 inode_lock(inode); 4414 inode_lock(inode); 4395 error = dquot_initialize(dir); 4415 error = dquot_initialize(dir); 4396 if (!error) 4416 if (!error) 4397 error = ocfs2_reflink(old_den 4417 error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve); 4398 inode_unlock(inode); 4418 inode_unlock(inode); 4399 if (!error) 4419 if (!error) 4400 fsnotify_create(dir, new_dent 4420 fsnotify_create(dir, new_dentry); 4401 return error; 4421 return error; 4402 } 4422 } 4403 /* 4423 /* 4404 * Most codes are copied from sys_linkat. 4424 * Most codes are copied from sys_linkat. 4405 */ 4425 */ 4406 int ocfs2_reflink_ioctl(struct inode *inode, 4426 int ocfs2_reflink_ioctl(struct inode *inode, 4407 const char __user *ol 4427 const char __user *oldname, 4408 const char __user *ne 4428 const char __user *newname, 4409 bool preserve) 4429 bool preserve) 4410 { 4430 { 4411 struct dentry *new_dentry; 4431 struct dentry *new_dentry; 4412 struct path old_path, new_path; 4432 struct path old_path, new_path; 4413 int error; 4433 int error; 4414 4434 4415 if (!ocfs2_refcount_tree(OCFS2_SB(ino 4435 if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) 4416 return -EOPNOTSUPP; 4436 return -EOPNOTSUPP; 4417 4437 4418 error = user_path_at(AT_FDCWD, oldnam 4438 error = user_path_at(AT_FDCWD, oldname, 0, &old_path); 4419 if (error) { 4439 if (error) { 4420 mlog_errno(error); 4440 mlog_errno(error); 4421 return error; 4441 return error; 4422 } 4442 } 4423 4443 4424 new_dentry = user_path_create(AT_FDCW 4444 new_dentry = user_path_create(AT_FDCWD, newname, &new_path, 0); 4425 error = PTR_ERR(new_dentry); 4445 error = PTR_ERR(new_dentry); 4426 if (IS_ERR(new_dentry)) { 4446 if (IS_ERR(new_dentry)) { 4427 mlog_errno(error); 4447 mlog_errno(error); 4428 goto out; 4448 goto out; 4429 } 4449 } 4430 4450 4431 error = -EXDEV; 4451 error = -EXDEV; 4432 if (old_path.mnt != new_path.mnt) { 4452 if (old_path.mnt != new_path.mnt) { 4433 mlog_errno(error); 4453 mlog_errno(error); 4434 goto out_dput; 4454 goto out_dput; 4435 } 4455 } 4436 4456 4437 error = ocfs2_vfs_reflink(old_path.de 4457 error = ocfs2_vfs_reflink(old_path.dentry, 4438 d_inode(new 4458 d_inode(new_path.dentry), 4439 new_dentry, 4459 new_dentry, preserve); 4440 out_dput: 4460 out_dput: 4441 done_path_create(&new_path, new_dentr 4461 done_path_create(&new_path, new_dentry); 4442 out: 4462 out: 4443 path_put(&old_path); 4463 path_put(&old_path); 4444 4464 4445 return error; 4465 return error; 4446 } 4466 } 4447 4467 4448 /* Update destination inode size, if necessar 4468 /* Update destination inode size, if necessary. */ 4449 int ocfs2_reflink_update_dest(struct inode *d 4469 int ocfs2_reflink_update_dest(struct inode *dest, 4450 struct buffer_h 4470 struct buffer_head *d_bh, 4451 loff_t newlen) 4471 loff_t newlen) 4452 { 4472 { 4453 handle_t *handle; 4473 handle_t *handle; 4454 int ret; 4474 int ret; 4455 4475 4456 dest->i_blocks = ocfs2_inode_sector_c 4476 dest->i_blocks = ocfs2_inode_sector_count(dest); 4457 4477 4458 if (newlen <= i_size_read(dest)) 4478 if (newlen <= i_size_read(dest)) 4459 return 0; 4479 return 0; 4460 4480 4461 handle = ocfs2_start_trans(OCFS2_SB(d 4481 handle = ocfs2_start_trans(OCFS2_SB(dest->i_sb), 4462 OCFS2_INOD 4482 OCFS2_INODE_UPDATE_CREDITS); 4463 if (IS_ERR(handle)) { 4483 if (IS_ERR(handle)) { 4464 ret = PTR_ERR(handle); 4484 ret = PTR_ERR(handle); 4465 mlog_errno(ret); 4485 mlog_errno(ret); 4466 return ret; 4486 return ret; 4467 } 4487 } 4468 4488 4469 /* Extend i_size if needed. */ 4489 /* Extend i_size if needed. */ 4470 spin_lock(&OCFS2_I(dest)->ip_lock); 4490 spin_lock(&OCFS2_I(dest)->ip_lock); 4471 if (newlen > i_size_read(dest)) 4491 if (newlen > i_size_read(dest)) 4472 i_size_write(dest, newlen); 4492 i_size_write(dest, newlen); 4473 spin_unlock(&OCFS2_I(dest)->ip_lock); 4493 spin_unlock(&OCFS2_I(dest)->ip_lock); 4474 inode_set_mtime_to_ts(dest, inode_set !! 4494 dest->i_ctime = dest->i_mtime = current_time(dest); 4475 4495 4476 ret = ocfs2_mark_inode_dirty(handle, 4496 ret = ocfs2_mark_inode_dirty(handle, dest, d_bh); 4477 if (ret) { 4497 if (ret) { 4478 mlog_errno(ret); 4498 mlog_errno(ret); 4479 goto out_commit; 4499 goto out_commit; 4480 } 4500 } 4481 4501 4482 out_commit: 4502 out_commit: 4483 ocfs2_commit_trans(OCFS2_SB(dest->i_s 4503 ocfs2_commit_trans(OCFS2_SB(dest->i_sb), handle); 4484 return ret; 4504 return ret; 4485 } 4505 } 4486 4506 4487 /* Remap the range pos_in:len in s_inode to p 4507 /* Remap the range pos_in:len in s_inode to pos_out:len in t_inode. */ 4488 static loff_t ocfs2_reflink_remap_extent(stru 4508 static loff_t ocfs2_reflink_remap_extent(struct inode *s_inode, 4489 stru 4509 struct buffer_head *s_bh, 4490 loff 4510 loff_t pos_in, 4491 stru 4511 struct inode *t_inode, 4492 stru 4512 struct buffer_head *t_bh, 4493 loff 4513 loff_t pos_out, 4494 loff 4514 loff_t len, 4495 stru 4515 struct ocfs2_cached_dealloc_ctxt *dealloc) 4496 { 4516 { 4497 struct ocfs2_extent_tree s_et; 4517 struct ocfs2_extent_tree s_et; 4498 struct ocfs2_extent_tree t_et; 4518 struct ocfs2_extent_tree t_et; 4499 struct ocfs2_dinode *dis; 4519 struct ocfs2_dinode *dis; 4500 struct buffer_head *ref_root_bh = NUL 4520 struct buffer_head *ref_root_bh = NULL; 4501 struct ocfs2_refcount_tree *ref_tree; 4521 struct ocfs2_refcount_tree *ref_tree; 4502 struct ocfs2_super *osb; 4522 struct ocfs2_super *osb; 4503 loff_t remapped_bytes = 0; 4523 loff_t remapped_bytes = 0; 4504 loff_t pstart, plen; 4524 loff_t pstart, plen; 4505 u32 p_cluster, num_clusters, slast, s 4525 u32 p_cluster, num_clusters, slast, spos, tpos, remapped_clus = 0; 4506 unsigned int ext_flags; 4526 unsigned int ext_flags; 4507 int ret = 0; 4527 int ret = 0; 4508 4528 4509 osb = OCFS2_SB(s_inode->i_sb); 4529 osb = OCFS2_SB(s_inode->i_sb); 4510 dis = (struct ocfs2_dinode *)s_bh->b_ 4530 dis = (struct ocfs2_dinode *)s_bh->b_data; 4511 ocfs2_init_dinode_extent_tree(&s_et, 4531 ocfs2_init_dinode_extent_tree(&s_et, INODE_CACHE(s_inode), s_bh); 4512 ocfs2_init_dinode_extent_tree(&t_et, 4532 ocfs2_init_dinode_extent_tree(&t_et, INODE_CACHE(t_inode), t_bh); 4513 4533 4514 spos = ocfs2_bytes_to_clusters(s_inod 4534 spos = ocfs2_bytes_to_clusters(s_inode->i_sb, pos_in); 4515 tpos = ocfs2_bytes_to_clusters(t_inod 4535 tpos = ocfs2_bytes_to_clusters(t_inode->i_sb, pos_out); 4516 slast = ocfs2_clusters_for_bytes(s_in 4536 slast = ocfs2_clusters_for_bytes(s_inode->i_sb, pos_in + len); 4517 4537 4518 while (spos < slast) { 4538 while (spos < slast) { 4519 if (fatal_signal_pending(curr 4539 if (fatal_signal_pending(current)) { 4520 ret = -EINTR; 4540 ret = -EINTR; 4521 goto out; 4541 goto out; 4522 } 4542 } 4523 4543 4524 /* Look up the extent. */ 4544 /* Look up the extent. */ 4525 ret = ocfs2_get_clusters(s_in 4545 ret = ocfs2_get_clusters(s_inode, spos, &p_cluster, 4526 &num 4546 &num_clusters, &ext_flags); 4527 if (ret) { 4547 if (ret) { 4528 mlog_errno(ret); 4548 mlog_errno(ret); 4529 goto out; 4549 goto out; 4530 } 4550 } 4531 4551 4532 num_clusters = min_t(u32, num 4552 num_clusters = min_t(u32, num_clusters, slast - spos); 4533 4553 4534 /* Punch out the dest range. 4554 /* Punch out the dest range. */ 4535 pstart = ocfs2_clusters_to_by 4555 pstart = ocfs2_clusters_to_bytes(t_inode->i_sb, tpos); 4536 plen = ocfs2_clusters_to_byte 4556 plen = ocfs2_clusters_to_bytes(t_inode->i_sb, num_clusters); 4537 ret = ocfs2_remove_inode_rang 4557 ret = ocfs2_remove_inode_range(t_inode, t_bh, pstart, plen); 4538 if (ret) { 4558 if (ret) { 4539 mlog_errno(ret); 4559 mlog_errno(ret); 4540 goto out; 4560 goto out; 4541 } 4561 } 4542 4562 4543 if (p_cluster == 0) 4563 if (p_cluster == 0) 4544 goto next_loop; 4564 goto next_loop; 4545 4565 4546 /* Lock the refcount btree... 4566 /* Lock the refcount btree... */ 4547 ret = ocfs2_lock_refcount_tre 4567 ret = ocfs2_lock_refcount_tree(osb, 4548 4568 le64_to_cpu(dis->i_refcount_loc), 4549 4569 1, &ref_tree, &ref_root_bh); 4550 if (ret) { 4570 if (ret) { 4551 mlog_errno(ret); 4571 mlog_errno(ret); 4552 goto out; 4572 goto out; 4553 } 4573 } 4554 4574 4555 /* Mark s_inode's extent as r 4575 /* Mark s_inode's extent as refcounted. */ 4556 if (!(ext_flags & OCFS2_EXT_R 4576 if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) { 4557 ret = ocfs2_add_refco 4577 ret = ocfs2_add_refcount_flag(s_inode, &s_et, 4558 4578 &ref_tree->rf_ci, 4559 4579 ref_root_bh, spos, 4560 4580 p_cluster, num_clusters, 4561 4581 dealloc, NULL); 4562 if (ret) { 4582 if (ret) { 4563 mlog_errno(re 4583 mlog_errno(ret); 4564 goto out_unlo 4584 goto out_unlock_refcount; 4565 } 4585 } 4566 } 4586 } 4567 4587 4568 /* Map in the new extent. */ 4588 /* Map in the new extent. */ 4569 ext_flags |= OCFS2_EXT_REFCOU 4589 ext_flags |= OCFS2_EXT_REFCOUNTED; 4570 ret = ocfs2_add_refcounted_ex 4590 ret = ocfs2_add_refcounted_extent(t_inode, &t_et, 4571 4591 &ref_tree->rf_ci, 4572 4592 ref_root_bh, 4573 4593 tpos, p_cluster, 4574 4594 num_clusters, 4575 4595 ext_flags, 4576 4596 dealloc); 4577 if (ret) { 4597 if (ret) { 4578 mlog_errno(ret); 4598 mlog_errno(ret); 4579 goto out_unlock_refco 4599 goto out_unlock_refcount; 4580 } 4600 } 4581 4601 4582 ocfs2_unlock_refcount_tree(os 4602 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 4583 brelse(ref_root_bh); 4603 brelse(ref_root_bh); 4584 next_loop: 4604 next_loop: 4585 spos += num_clusters; 4605 spos += num_clusters; 4586 tpos += num_clusters; 4606 tpos += num_clusters; 4587 remapped_clus += num_clusters 4607 remapped_clus += num_clusters; 4588 } 4608 } 4589 4609 4590 goto out; 4610 goto out; 4591 out_unlock_refcount: 4611 out_unlock_refcount: 4592 ocfs2_unlock_refcount_tree(osb, ref_t 4612 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 4593 brelse(ref_root_bh); 4613 brelse(ref_root_bh); 4594 out: 4614 out: 4595 remapped_bytes = ocfs2_clusters_to_by 4615 remapped_bytes = ocfs2_clusters_to_bytes(t_inode->i_sb, remapped_clus); 4596 remapped_bytes = min_t(loff_t, len, r 4616 remapped_bytes = min_t(loff_t, len, remapped_bytes); 4597 4617 4598 return remapped_bytes > 0 ? remapped_ 4618 return remapped_bytes > 0 ? remapped_bytes : ret; 4599 } 4619 } 4600 4620 4601 /* Set up refcount tree and remap s_inode to 4621 /* Set up refcount tree and remap s_inode to t_inode. */ 4602 loff_t ocfs2_reflink_remap_blocks(struct inod 4622 loff_t ocfs2_reflink_remap_blocks(struct inode *s_inode, 4603 struct buff 4623 struct buffer_head *s_bh, 4604 loff_t pos_ 4624 loff_t pos_in, 4605 struct inod 4625 struct inode *t_inode, 4606 struct buff 4626 struct buffer_head *t_bh, 4607 loff_t pos_ 4627 loff_t pos_out, 4608 loff_t len) 4628 loff_t len) 4609 { 4629 { 4610 struct ocfs2_cached_dealloc_ctxt deal 4630 struct ocfs2_cached_dealloc_ctxt dealloc; 4611 struct ocfs2_super *osb; 4631 struct ocfs2_super *osb; 4612 struct ocfs2_dinode *dis; 4632 struct ocfs2_dinode *dis; 4613 struct ocfs2_dinode *dit; 4633 struct ocfs2_dinode *dit; 4614 loff_t ret; 4634 loff_t ret; 4615 4635 4616 osb = OCFS2_SB(s_inode->i_sb); 4636 osb = OCFS2_SB(s_inode->i_sb); 4617 dis = (struct ocfs2_dinode *)s_bh->b_ 4637 dis = (struct ocfs2_dinode *)s_bh->b_data; 4618 dit = (struct ocfs2_dinode *)t_bh->b_ 4638 dit = (struct ocfs2_dinode *)t_bh->b_data; 4619 ocfs2_init_dealloc_ctxt(&dealloc); 4639 ocfs2_init_dealloc_ctxt(&dealloc); 4620 4640 4621 /* 4641 /* 4622 * If we're reflinking the entire fil 4642 * If we're reflinking the entire file and the source is inline 4623 * data, just copy the contents. 4643 * data, just copy the contents. 4624 */ 4644 */ 4625 if (pos_in == pos_out && pos_in == 0 4645 if (pos_in == pos_out && pos_in == 0 && len == i_size_read(s_inode) && 4626 i_size_read(t_inode) <= len && 4646 i_size_read(t_inode) <= len && 4627 (OCFS2_I(s_inode)->ip_dyn_feature 4647 (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)) { 4628 ret = ocfs2_duplicate_inline_ 4648 ret = ocfs2_duplicate_inline_data(s_inode, s_bh, t_inode, t_bh); 4629 if (ret) 4649 if (ret) 4630 mlog_errno(ret); 4650 mlog_errno(ret); 4631 goto out; 4651 goto out; 4632 } 4652 } 4633 4653 4634 /* 4654 /* 4635 * If both inodes belong to two diffe 4655 * If both inodes belong to two different refcount groups then 4636 * forget it because we don't know ho 4656 * forget it because we don't know how (or want) to go merging 4637 * refcount trees. 4657 * refcount trees. 4638 */ 4658 */ 4639 ret = -EOPNOTSUPP; 4659 ret = -EOPNOTSUPP; 4640 if (ocfs2_is_refcount_inode(s_inode) 4660 if (ocfs2_is_refcount_inode(s_inode) && 4641 ocfs2_is_refcount_inode(t_inode) 4661 ocfs2_is_refcount_inode(t_inode) && 4642 le64_to_cpu(dis->i_refcount_loc) 4662 le64_to_cpu(dis->i_refcount_loc) != 4643 le64_to_cpu(dit->i_refcount_loc)) 4663 le64_to_cpu(dit->i_refcount_loc)) 4644 goto out; 4664 goto out; 4645 4665 4646 /* Neither inode has a refcount tree. 4666 /* Neither inode has a refcount tree. Add one to s_inode. */ 4647 if (!ocfs2_is_refcount_inode(s_inode) 4667 if (!ocfs2_is_refcount_inode(s_inode) && 4648 !ocfs2_is_refcount_inode(t_inode) 4668 !ocfs2_is_refcount_inode(t_inode)) { 4649 ret = ocfs2_create_refcount_t 4669 ret = ocfs2_create_refcount_tree(s_inode, s_bh); 4650 if (ret) { 4670 if (ret) { 4651 mlog_errno(ret); 4671 mlog_errno(ret); 4652 goto out; 4672 goto out; 4653 } 4673 } 4654 } 4674 } 4655 4675 4656 /* Ensure that both inodes end up wit 4676 /* Ensure that both inodes end up with the same refcount tree. */ 4657 if (!ocfs2_is_refcount_inode(s_inode) 4677 if (!ocfs2_is_refcount_inode(s_inode)) { 4658 ret = ocfs2_set_refcount_tree 4678 ret = ocfs2_set_refcount_tree(s_inode, s_bh, 4659 4679 le64_to_cpu(dit->i_refcount_loc)); 4660 if (ret) { 4680 if (ret) { 4661 mlog_errno(ret); 4681 mlog_errno(ret); 4662 goto out; 4682 goto out; 4663 } 4683 } 4664 } 4684 } 4665 if (!ocfs2_is_refcount_inode(t_inode) 4685 if (!ocfs2_is_refcount_inode(t_inode)) { 4666 ret = ocfs2_set_refcount_tree 4686 ret = ocfs2_set_refcount_tree(t_inode, t_bh, 4667 4687 le64_to_cpu(dis->i_refcount_loc)); 4668 if (ret) { 4688 if (ret) { 4669 mlog_errno(ret); 4689 mlog_errno(ret); 4670 goto out; 4690 goto out; 4671 } 4691 } 4672 } 4692 } 4673 4693 4674 /* Turn off inline data in the dest f 4694 /* Turn off inline data in the dest file. */ 4675 if (OCFS2_I(t_inode)->ip_dyn_features 4695 if (OCFS2_I(t_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 4676 ret = ocfs2_convert_inline_da 4696 ret = ocfs2_convert_inline_data_to_extents(t_inode, t_bh); 4677 if (ret) { 4697 if (ret) { 4678 mlog_errno(ret); 4698 mlog_errno(ret); 4679 goto out; 4699 goto out; 4680 } 4700 } 4681 } 4701 } 4682 4702 4683 /* Actually remap extents now. */ 4703 /* Actually remap extents now. */ 4684 ret = ocfs2_reflink_remap_extent(s_in 4704 ret = ocfs2_reflink_remap_extent(s_inode, s_bh, pos_in, t_inode, t_bh, 4685 pos_ 4705 pos_out, len, &dealloc); 4686 if (ret < 0) { 4706 if (ret < 0) { 4687 mlog_errno(ret); 4707 mlog_errno(ret); 4688 goto out; 4708 goto out; 4689 } 4709 } 4690 4710 4691 out: 4711 out: 4692 if (ocfs2_dealloc_has_cluster(&deallo 4712 if (ocfs2_dealloc_has_cluster(&dealloc)) { 4693 ocfs2_schedule_truncate_log_f 4713 ocfs2_schedule_truncate_log_flush(osb, 1); 4694 ocfs2_run_deallocs(osb, &deal 4714 ocfs2_run_deallocs(osb, &dealloc); 4695 } 4715 } 4696 4716 4697 return ret; 4717 return ret; 4698 } 4718 } 4699 4719 4700 /* Lock an inode and grab a bh pointing to th 4720 /* Lock an inode and grab a bh pointing to the inode. */ 4701 int ocfs2_reflink_inodes_lock(struct inode *s 4721 int ocfs2_reflink_inodes_lock(struct inode *s_inode, 4702 struct buffer_h !! 4722 struct buffer_head **bh1, 4703 struct inode *t 4723 struct inode *t_inode, 4704 struct buffer_h !! 4724 struct buffer_head **bh2) 4705 { 4725 { 4706 struct inode *inode1 = s_inode; !! 4726 struct inode *inode1; 4707 struct inode *inode2 = t_inode; !! 4727 struct inode *inode2; 4708 struct ocfs2_inode_info *oi1; 4728 struct ocfs2_inode_info *oi1; 4709 struct ocfs2_inode_info *oi2; 4729 struct ocfs2_inode_info *oi2; 4710 struct buffer_head *bh1 = NULL; << 4711 struct buffer_head *bh2 = NULL; << 4712 bool same_inode = (s_inode == t_inode 4730 bool same_inode = (s_inode == t_inode); 4713 bool need_swap = (inode1->i_ino > ino << 4714 int status; 4731 int status; 4715 4732 4716 /* First grab the VFS and rw locks. * 4733 /* First grab the VFS and rw locks. */ 4717 lock_two_nondirectories(s_inode, t_in 4734 lock_two_nondirectories(s_inode, t_inode); 4718 if (need_swap) !! 4735 inode1 = s_inode; >> 4736 inode2 = t_inode; >> 4737 if (inode1->i_ino > inode2->i_ino) 4719 swap(inode1, inode2); 4738 swap(inode1, inode2); 4720 4739 4721 status = ocfs2_rw_lock(inode1, 1); 4740 status = ocfs2_rw_lock(inode1, 1); 4722 if (status) { 4741 if (status) { 4723 mlog_errno(status); 4742 mlog_errno(status); 4724 goto out_i1; 4743 goto out_i1; 4725 } 4744 } 4726 if (!same_inode) { 4745 if (!same_inode) { 4727 status = ocfs2_rw_lock(inode2 4746 status = ocfs2_rw_lock(inode2, 1); 4728 if (status) { 4747 if (status) { 4729 mlog_errno(status); 4748 mlog_errno(status); 4730 goto out_i2; 4749 goto out_i2; 4731 } 4750 } 4732 } 4751 } 4733 4752 4734 /* Now go for the cluster locks */ 4753 /* Now go for the cluster locks */ 4735 oi1 = OCFS2_I(inode1); 4754 oi1 = OCFS2_I(inode1); 4736 oi2 = OCFS2_I(inode2); 4755 oi2 = OCFS2_I(inode2); 4737 4756 4738 trace_ocfs2_double_lock((unsigned lon 4757 trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno, 4739 (unsigned lon 4758 (unsigned long long)oi2->ip_blkno); 4740 4759 >> 4760 if (*bh1) >> 4761 *bh1 = NULL; >> 4762 if (*bh2) >> 4763 *bh2 = NULL; >> 4764 4741 /* We always want to lock the one wit 4765 /* We always want to lock the one with the lower lockid first. */ 4742 if (oi1->ip_blkno > oi2->ip_blkno) 4766 if (oi1->ip_blkno > oi2->ip_blkno) 4743 mlog_errno(-ENOLCK); 4767 mlog_errno(-ENOLCK); 4744 4768 4745 /* lock id1 */ 4769 /* lock id1 */ 4746 status = ocfs2_inode_lock_nested(inod !! 4770 status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_REFLINK_TARGET); 4747 OI_L << 4748 if (status < 0) { 4771 if (status < 0) { 4749 if (status != -ENOENT) 4772 if (status != -ENOENT) 4750 mlog_errno(status); 4773 mlog_errno(status); 4751 goto out_rw2; 4774 goto out_rw2; 4752 } 4775 } 4753 4776 4754 /* lock id2 */ 4777 /* lock id2 */ 4755 if (!same_inode) { 4778 if (!same_inode) { 4756 status = ocfs2_inode_lock_nes !! 4779 status = ocfs2_inode_lock_nested(inode2, bh2, 1, 4757 4780 OI_LS_REFLINK_TARGET); 4758 if (status < 0) { 4781 if (status < 0) { 4759 if (status != -ENOENT 4782 if (status != -ENOENT) 4760 mlog_errno(st 4783 mlog_errno(status); 4761 goto out_cl1; 4784 goto out_cl1; 4762 } 4785 } 4763 } else { !! 4786 } else 4764 bh2 = bh1; !! 4787 *bh2 = *bh1; 4765 } << 4766 << 4767 /* << 4768 * If we swapped inode order above, w << 4769 * before passing them back to the ca << 4770 */ << 4771 if (need_swap) << 4772 swap(bh1, bh2); << 4773 *bh_s = bh1; << 4774 *bh_t = bh2; << 4775 4788 4776 trace_ocfs2_double_lock_end( 4789 trace_ocfs2_double_lock_end( 4777 (unsigned long long)o 4790 (unsigned long long)oi1->ip_blkno, 4778 (unsigned long long)o 4791 (unsigned long long)oi2->ip_blkno); 4779 4792 4780 return 0; 4793 return 0; 4781 4794 4782 out_cl1: 4795 out_cl1: 4783 ocfs2_inode_unlock(inode1, 1); 4796 ocfs2_inode_unlock(inode1, 1); 4784 brelse(bh1); !! 4797 brelse(*bh1); >> 4798 *bh1 = NULL; 4785 out_rw2: 4799 out_rw2: 4786 ocfs2_rw_unlock(inode2, 1); 4800 ocfs2_rw_unlock(inode2, 1); 4787 out_i2: 4801 out_i2: 4788 ocfs2_rw_unlock(inode1, 1); 4802 ocfs2_rw_unlock(inode1, 1); 4789 out_i1: 4803 out_i1: 4790 unlock_two_nondirectories(s_inode, t_ 4804 unlock_two_nondirectories(s_inode, t_inode); 4791 return status; 4805 return status; 4792 } 4806 } 4793 4807 4794 /* Unlock both inodes and release buffers. */ 4808 /* Unlock both inodes and release buffers. */ 4795 void ocfs2_reflink_inodes_unlock(struct inode 4809 void ocfs2_reflink_inodes_unlock(struct inode *s_inode, 4796 struct buffe 4810 struct buffer_head *s_bh, 4797 struct inode 4811 struct inode *t_inode, 4798 struct buffe 4812 struct buffer_head *t_bh) 4799 { 4813 { 4800 ocfs2_inode_unlock(s_inode, 1); 4814 ocfs2_inode_unlock(s_inode, 1); 4801 ocfs2_rw_unlock(s_inode, 1); 4815 ocfs2_rw_unlock(s_inode, 1); 4802 brelse(s_bh); 4816 brelse(s_bh); 4803 if (s_inode != t_inode) { 4817 if (s_inode != t_inode) { 4804 ocfs2_inode_unlock(t_inode, 1 4818 ocfs2_inode_unlock(t_inode, 1); 4805 ocfs2_rw_unlock(t_inode, 1); 4819 ocfs2_rw_unlock(t_inode, 1); 4806 brelse(t_bh); 4820 brelse(t_bh); 4807 } 4821 } 4808 unlock_two_nondirectories(s_inode, t_ 4822 unlock_two_nondirectories(s_inode, t_inode); 4809 } 4823 } 4810 4824
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.