1f2c870e3STao Ma /* -*- mode: c; c-basic-offset: 8; -*- 2f2c870e3STao Ma * vim: noexpandtab sw=8 ts=8 sts=0: 3f2c870e3STao Ma * 4f2c870e3STao Ma * refcounttree.c 5f2c870e3STao Ma * 6f2c870e3STao Ma * Copyright (C) 2009 Oracle. All rights reserved. 7f2c870e3STao Ma * 8f2c870e3STao Ma * This program is free software; you can redistribute it and/or 9f2c870e3STao Ma * modify it under the terms of the GNU General Public 10f2c870e3STao Ma * License version 2 as published by the Free Software Foundation. 11f2c870e3STao Ma * 12f2c870e3STao Ma * This program is distributed in the hope that it will be useful, 13f2c870e3STao Ma * but WITHOUT ANY WARRANTY; without even the implied warranty of 14f2c870e3STao Ma * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15f2c870e3STao Ma * General Public License for more details. 16f2c870e3STao Ma */ 17f2c870e3STao Ma 18e73a819dSTao Ma #include <linux/sort.h> 19f2c870e3STao Ma #include <cluster/masklog.h> 20f2c870e3STao Ma #include "ocfs2.h" 21f2c870e3STao Ma #include "inode.h" 22f2c870e3STao Ma #include "alloc.h" 23f2c870e3STao Ma #include "suballoc.h" 24f2c870e3STao Ma #include "journal.h" 25f2c870e3STao Ma #include "uptodate.h" 26f2c870e3STao Ma #include "super.h" 27f2c870e3STao Ma #include "buffer_head_io.h" 28f2c870e3STao Ma #include "blockcheck.h" 29c732eb16STao Ma #include "refcounttree.h" 308bf396deSTao Ma #include "sysfile.h" 31374a263eSTao Ma #include "dlmglue.h" 32e73a819dSTao Ma #include "extent_map.h" 336f70fa51STao Ma #include "aops.h" 34492a8a33STao Ma #include "xattr.h" 3509bf27a0STao Ma #include "namei.h" 36198aac28STao Ma #include "ocfs2_trace.h" 376f70fa51STao Ma 386f70fa51STao Ma #include <linux/bio.h> 396f70fa51STao Ma #include <linux/blkdev.h> 406f70fa51STao Ma #include <linux/slab.h> 416f70fa51STao Ma #include <linux/writeback.h> 426f70fa51STao Ma #include <linux/pagevec.h> 436f70fa51STao Ma #include <linux/swap.h> 44bd50873dSTao Ma #include <linux/security.h> 45bd50873dSTao Ma #include <linux/fsnotify.h> 46bd50873dSTao Ma #include <linux/quotaops.h> 47bd50873dSTao Ma #include <linux/namei.h> 48bd50873dSTao Ma #include <linux/mount.h> 49702e5bc6SChristoph Hellwig #include <linux/posix_acl.h> 506f70fa51STao Ma 516f70fa51STao Ma struct ocfs2_cow_context { 526f70fa51STao Ma struct inode *inode; 536f70fa51STao Ma u32 cow_start; 546f70fa51STao Ma u32 cow_len; 55913580b4STao Ma struct ocfs2_extent_tree data_et; 56913580b4STao Ma struct ocfs2_refcount_tree *ref_tree; 576f70fa51STao Ma struct buffer_head *ref_root_bh; 586f70fa51STao Ma struct ocfs2_alloc_context *meta_ac; 596f70fa51STao Ma struct ocfs2_alloc_context *data_ac; 606f70fa51STao Ma struct ocfs2_cached_dealloc_ctxt dealloc; 61492a8a33STao Ma void *cow_object; 62492a8a33STao Ma struct ocfs2_post_refcount *post_refcount; 63492a8a33STao Ma int extra_credits; 64913580b4STao Ma int (*get_clusters)(struct ocfs2_cow_context *context, 65913580b4STao Ma u32 v_cluster, u32 *p_cluster, 66913580b4STao Ma u32 *num_clusters, 67913580b4STao Ma unsigned int *extent_flags); 68913580b4STao Ma int (*cow_duplicate_clusters)(handle_t *handle, 69c7dd3392STiger Yang struct inode *inode, 70913580b4STao Ma u32 cpos, u32 old_cluster, 71913580b4STao Ma u32 new_cluster, u32 new_len); 726f70fa51STao Ma }; 73c732eb16STao Ma 74c732eb16STao Ma static inline struct ocfs2_refcount_tree * 75c732eb16STao Ma cache_info_to_refcount(struct ocfs2_caching_info *ci) 76c732eb16STao Ma { 77c732eb16STao Ma return container_of(ci, struct ocfs2_refcount_tree, rf_ci); 78c732eb16STao Ma } 79f2c870e3STao Ma 80f2c870e3STao Ma static int ocfs2_validate_refcount_block(struct super_block *sb, 81f2c870e3STao Ma struct buffer_head *bh) 82f2c870e3STao Ma { 83f2c870e3STao Ma int rc; 84f2c870e3STao Ma struct ocfs2_refcount_block *rb = 85f2c870e3STao Ma (struct ocfs2_refcount_block *)bh->b_data; 86f2c870e3STao Ma 87198aac28STao Ma trace_ocfs2_validate_refcount_block((unsigned long long)bh->b_blocknr); 88f2c870e3STao Ma 89f2c870e3STao Ma BUG_ON(!buffer_uptodate(bh)); 90f2c870e3STao Ma 91f2c870e3STao Ma /* 92f2c870e3STao Ma * If the ecc fails, we return the error but otherwise 93f2c870e3STao Ma * leave the filesystem running. We know any error is 94f2c870e3STao Ma * local to this block. 95f2c870e3STao Ma */ 96f2c870e3STao Ma rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check); 97f2c870e3STao Ma if (rc) { 98f2c870e3STao Ma mlog(ML_ERROR, "Checksum failed for refcount block %llu\n", 99f2c870e3STao Ma (unsigned long long)bh->b_blocknr); 100f2c870e3STao Ma return rc; 101f2c870e3STao Ma } 102f2c870e3STao Ma 103f2c870e3STao Ma 104f2c870e3STao Ma if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) { 105f2c870e3STao Ma ocfs2_error(sb, 106f2c870e3STao Ma "Refcount block #%llu has bad signature %.*s", 107f2c870e3STao Ma (unsigned long long)bh->b_blocknr, 7, 108f2c870e3STao Ma rb->rf_signature); 109f2c870e3STao Ma return -EINVAL; 110f2c870e3STao Ma } 111f2c870e3STao Ma 112f2c870e3STao Ma if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) { 113f2c870e3STao Ma ocfs2_error(sb, 114f2c870e3STao Ma "Refcount block #%llu has an invalid rf_blkno " 115f2c870e3STao Ma "of %llu", 116f2c870e3STao Ma (unsigned long long)bh->b_blocknr, 117f2c870e3STao Ma (unsigned long long)le64_to_cpu(rb->rf_blkno)); 118f2c870e3STao Ma return -EINVAL; 119f2c870e3STao Ma } 120f2c870e3STao Ma 121f2c870e3STao Ma if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) { 122f2c870e3STao Ma ocfs2_error(sb, 123f2c870e3STao Ma "Refcount block #%llu has an invalid " 124f2c870e3STao Ma "rf_fs_generation of #%u", 125f2c870e3STao Ma (unsigned long long)bh->b_blocknr, 126f2c870e3STao Ma le32_to_cpu(rb->rf_fs_generation)); 127f2c870e3STao Ma return -EINVAL; 128f2c870e3STao Ma } 129f2c870e3STao Ma 130f2c870e3STao Ma return 0; 131f2c870e3STao Ma } 132f2c870e3STao Ma 133f2c870e3STao Ma static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci, 134f2c870e3STao Ma u64 rb_blkno, 135f2c870e3STao Ma struct buffer_head **bh) 136f2c870e3STao Ma { 137f2c870e3STao Ma int rc; 138f2c870e3STao Ma struct buffer_head *tmp = *bh; 139f2c870e3STao Ma 140f2c870e3STao Ma rc = ocfs2_read_block(ci, rb_blkno, &tmp, 141f2c870e3STao Ma ocfs2_validate_refcount_block); 142f2c870e3STao Ma 143f2c870e3STao Ma /* If ocfs2_read_block() got us a new bh, pass it up. */ 144f2c870e3STao Ma if (!rc && !*bh) 145f2c870e3STao Ma *bh = tmp; 146f2c870e3STao Ma 147f2c870e3STao Ma return rc; 148f2c870e3STao Ma } 149c732eb16STao Ma 150c732eb16STao Ma static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci) 151c732eb16STao Ma { 152c732eb16STao Ma struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 153c732eb16STao Ma 154c732eb16STao Ma return rf->rf_blkno; 155c732eb16STao Ma } 156c732eb16STao Ma 157c732eb16STao Ma static struct super_block * 158c732eb16STao Ma ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci) 159c732eb16STao Ma { 160c732eb16STao Ma struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 161c732eb16STao Ma 162c732eb16STao Ma return rf->rf_sb; 163c732eb16STao Ma } 164c732eb16STao Ma 165c732eb16STao Ma static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci) 166c732eb16STao Ma { 167c732eb16STao Ma struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 168c732eb16STao Ma 169c732eb16STao Ma spin_lock(&rf->rf_lock); 170c732eb16STao Ma } 171c732eb16STao Ma 172c732eb16STao Ma static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci) 173c732eb16STao Ma { 174c732eb16STao Ma struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 175c732eb16STao Ma 176c732eb16STao Ma spin_unlock(&rf->rf_lock); 177c732eb16STao Ma } 178c732eb16STao Ma 179c732eb16STao Ma static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci) 180c732eb16STao Ma { 181c732eb16STao Ma struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 182c732eb16STao Ma 183c732eb16STao Ma mutex_lock(&rf->rf_io_mutex); 184c732eb16STao Ma } 185c732eb16STao Ma 186c732eb16STao Ma static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci) 187c732eb16STao Ma { 188c732eb16STao Ma struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci); 189c732eb16STao Ma 190c732eb16STao Ma mutex_unlock(&rf->rf_io_mutex); 191c732eb16STao Ma } 192c732eb16STao Ma 193c732eb16STao Ma static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = { 194c732eb16STao Ma .co_owner = ocfs2_refcount_cache_owner, 195c732eb16STao Ma .co_get_super = ocfs2_refcount_cache_get_super, 196c732eb16STao Ma .co_cache_lock = ocfs2_refcount_cache_lock, 197c732eb16STao Ma .co_cache_unlock = ocfs2_refcount_cache_unlock, 198c732eb16STao Ma .co_io_lock = ocfs2_refcount_cache_io_lock, 199c732eb16STao Ma .co_io_unlock = ocfs2_refcount_cache_io_unlock, 200c732eb16STao Ma }; 201374a263eSTao Ma 202374a263eSTao Ma static struct ocfs2_refcount_tree * 203374a263eSTao Ma ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno) 204374a263eSTao Ma { 205374a263eSTao Ma struct rb_node *n = osb->osb_rf_lock_tree.rb_node; 206374a263eSTao Ma struct ocfs2_refcount_tree *tree = NULL; 207374a263eSTao Ma 208374a263eSTao Ma while (n) { 209374a263eSTao Ma tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node); 210374a263eSTao Ma 211374a263eSTao Ma if (blkno < tree->rf_blkno) 212374a263eSTao Ma n = n->rb_left; 213374a263eSTao Ma else if (blkno > tree->rf_blkno) 214374a263eSTao Ma n = n->rb_right; 215374a263eSTao Ma else 216374a263eSTao Ma return tree; 217374a263eSTao Ma } 218374a263eSTao Ma 219374a263eSTao Ma return NULL; 220374a263eSTao Ma } 221374a263eSTao Ma 222374a263eSTao Ma /* osb_lock is already locked. */ 223374a263eSTao Ma static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb, 224374a263eSTao Ma struct ocfs2_refcount_tree *new) 225374a263eSTao Ma { 226374a263eSTao Ma u64 rf_blkno = new->rf_blkno; 227374a263eSTao Ma struct rb_node *parent = NULL; 228374a263eSTao Ma struct rb_node **p = &osb->osb_rf_lock_tree.rb_node; 229374a263eSTao Ma struct ocfs2_refcount_tree *tmp; 230374a263eSTao Ma 231374a263eSTao Ma while (*p) { 232374a263eSTao Ma parent = *p; 233374a263eSTao Ma 234374a263eSTao Ma tmp = rb_entry(parent, struct ocfs2_refcount_tree, 235374a263eSTao Ma rf_node); 236374a263eSTao Ma 237374a263eSTao Ma if (rf_blkno < tmp->rf_blkno) 238374a263eSTao Ma p = &(*p)->rb_left; 239374a263eSTao Ma else if (rf_blkno > tmp->rf_blkno) 240374a263eSTao Ma p = &(*p)->rb_right; 241374a263eSTao Ma else { 242374a263eSTao Ma /* This should never happen! */ 243374a263eSTao Ma mlog(ML_ERROR, "Duplicate refcount block %llu found!\n", 244374a263eSTao Ma (unsigned long long)rf_blkno); 245374a263eSTao Ma BUG(); 246374a263eSTao Ma } 247374a263eSTao Ma } 248374a263eSTao Ma 249374a263eSTao Ma rb_link_node(&new->rf_node, parent, p); 250374a263eSTao Ma rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree); 251374a263eSTao Ma } 252374a263eSTao Ma 253374a263eSTao Ma static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree) 254374a263eSTao Ma { 255374a263eSTao Ma ocfs2_metadata_cache_exit(&tree->rf_ci); 256374a263eSTao Ma ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres); 257374a263eSTao Ma ocfs2_lock_res_free(&tree->rf_lockres); 258374a263eSTao Ma kfree(tree); 259374a263eSTao Ma } 260374a263eSTao Ma 261374a263eSTao Ma static inline void 262374a263eSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb, 263374a263eSTao Ma struct ocfs2_refcount_tree *tree) 264374a263eSTao Ma { 265374a263eSTao Ma rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree); 266374a263eSTao Ma if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree) 267374a263eSTao Ma osb->osb_ref_tree_lru = NULL; 268374a263eSTao Ma } 269374a263eSTao Ma 270374a263eSTao Ma static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb, 271374a263eSTao Ma struct ocfs2_refcount_tree *tree) 272374a263eSTao Ma { 273374a263eSTao Ma spin_lock(&osb->osb_lock); 274374a263eSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree); 275374a263eSTao Ma spin_unlock(&osb->osb_lock); 276374a263eSTao Ma } 277374a263eSTao Ma 27812d4cec9STao Ma static void ocfs2_kref_remove_refcount_tree(struct kref *kref) 279374a263eSTao Ma { 280374a263eSTao Ma struct ocfs2_refcount_tree *tree = 281374a263eSTao Ma container_of(kref, struct ocfs2_refcount_tree, rf_getcnt); 282374a263eSTao Ma 283374a263eSTao Ma ocfs2_free_refcount_tree(tree); 284374a263eSTao Ma } 285374a263eSTao Ma 286374a263eSTao Ma static inline void 287374a263eSTao Ma ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree) 288374a263eSTao Ma { 289374a263eSTao Ma kref_get(&tree->rf_getcnt); 290374a263eSTao Ma } 291374a263eSTao Ma 292374a263eSTao Ma static inline void 293374a263eSTao Ma ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree) 294374a263eSTao Ma { 295374a263eSTao Ma kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree); 296374a263eSTao Ma } 297374a263eSTao Ma 298374a263eSTao Ma static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new, 299374a263eSTao Ma struct super_block *sb) 300374a263eSTao Ma { 301374a263eSTao Ma ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops); 302374a263eSTao Ma mutex_init(&new->rf_io_mutex); 303374a263eSTao Ma new->rf_sb = sb; 304374a263eSTao Ma spin_lock_init(&new->rf_lock); 305374a263eSTao Ma } 306374a263eSTao Ma 307374a263eSTao Ma static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb, 308374a263eSTao Ma struct ocfs2_refcount_tree *new, 309374a263eSTao Ma u64 rf_blkno, u32 generation) 310374a263eSTao Ma { 311374a263eSTao Ma init_rwsem(&new->rf_sem); 312374a263eSTao Ma ocfs2_refcount_lock_res_init(&new->rf_lockres, osb, 313374a263eSTao Ma rf_blkno, generation); 314374a263eSTao Ma } 315374a263eSTao Ma 3168bf396deSTao Ma static struct ocfs2_refcount_tree* 3178bf396deSTao Ma ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno) 3188bf396deSTao Ma { 3198bf396deSTao Ma struct ocfs2_refcount_tree *new; 3208bf396deSTao Ma 3218bf396deSTao Ma new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS); 3228bf396deSTao Ma if (!new) 3238bf396deSTao Ma return NULL; 3248bf396deSTao Ma 3258bf396deSTao Ma new->rf_blkno = rf_blkno; 3268bf396deSTao Ma kref_init(&new->rf_getcnt); 3278bf396deSTao Ma ocfs2_init_refcount_tree_ci(new, osb->sb); 3288bf396deSTao Ma 3298bf396deSTao Ma return new; 3308bf396deSTao Ma } 3318bf396deSTao Ma 332374a263eSTao Ma static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno, 333374a263eSTao Ma struct ocfs2_refcount_tree **ret_tree) 334374a263eSTao Ma { 335374a263eSTao Ma int ret = 0; 336374a263eSTao Ma struct ocfs2_refcount_tree *tree, *new = NULL; 337374a263eSTao Ma struct buffer_head *ref_root_bh = NULL; 338374a263eSTao Ma struct ocfs2_refcount_block *ref_rb; 339374a263eSTao Ma 340374a263eSTao Ma spin_lock(&osb->osb_lock); 341374a263eSTao Ma if (osb->osb_ref_tree_lru && 342374a263eSTao Ma osb->osb_ref_tree_lru->rf_blkno == rf_blkno) 343374a263eSTao Ma tree = osb->osb_ref_tree_lru; 344374a263eSTao Ma else 345374a263eSTao Ma tree = ocfs2_find_refcount_tree(osb, rf_blkno); 346374a263eSTao Ma if (tree) 347374a263eSTao Ma goto out; 348374a263eSTao Ma 349374a263eSTao Ma spin_unlock(&osb->osb_lock); 350374a263eSTao Ma 3518bf396deSTao Ma new = ocfs2_allocate_refcount_tree(osb, rf_blkno); 352374a263eSTao Ma if (!new) { 353374a263eSTao Ma ret = -ENOMEM; 3548bf396deSTao Ma mlog_errno(ret); 355374a263eSTao Ma return ret; 356374a263eSTao Ma } 357374a263eSTao Ma /* 358374a263eSTao Ma * We need the generation to create the refcount tree lock and since 359374a263eSTao Ma * it isn't changed during the tree modification, we are safe here to 360374a263eSTao Ma * read without protection. 361374a263eSTao Ma * We also have to purge the cache after we create the lock since the 362374a263eSTao Ma * refcount block may have the stale data. It can only be trusted when 363374a263eSTao Ma * we hold the refcount lock. 364374a263eSTao Ma */ 365374a263eSTao Ma ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh); 366374a263eSTao Ma if (ret) { 367374a263eSTao Ma mlog_errno(ret); 368374a263eSTao Ma ocfs2_metadata_cache_exit(&new->rf_ci); 369374a263eSTao Ma kfree(new); 370374a263eSTao Ma return ret; 371374a263eSTao Ma } 372374a263eSTao Ma 373374a263eSTao Ma ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 374374a263eSTao Ma new->rf_generation = le32_to_cpu(ref_rb->rf_generation); 375374a263eSTao Ma ocfs2_init_refcount_tree_lock(osb, new, rf_blkno, 376374a263eSTao Ma new->rf_generation); 377374a263eSTao Ma ocfs2_metadata_cache_purge(&new->rf_ci); 378374a263eSTao Ma 379374a263eSTao Ma spin_lock(&osb->osb_lock); 380374a263eSTao Ma tree = ocfs2_find_refcount_tree(osb, rf_blkno); 381374a263eSTao Ma if (tree) 382374a263eSTao Ma goto out; 383374a263eSTao Ma 384374a263eSTao Ma ocfs2_insert_refcount_tree(osb, new); 385374a263eSTao Ma 386374a263eSTao Ma tree = new; 387374a263eSTao Ma new = NULL; 388374a263eSTao Ma 389374a263eSTao Ma out: 390374a263eSTao Ma *ret_tree = tree; 391374a263eSTao Ma 392374a263eSTao Ma osb->osb_ref_tree_lru = tree; 393374a263eSTao Ma 394374a263eSTao Ma spin_unlock(&osb->osb_lock); 395374a263eSTao Ma 396374a263eSTao Ma if (new) 397374a263eSTao Ma ocfs2_free_refcount_tree(new); 398374a263eSTao Ma 399374a263eSTao Ma brelse(ref_root_bh); 400374a263eSTao Ma return ret; 401374a263eSTao Ma } 402374a263eSTao Ma 403374a263eSTao Ma static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno) 404374a263eSTao Ma { 405374a263eSTao Ma int ret; 406374a263eSTao Ma struct buffer_head *di_bh = NULL; 407374a263eSTao Ma struct ocfs2_dinode *di; 408374a263eSTao Ma 409374a263eSTao Ma ret = ocfs2_read_inode_block(inode, &di_bh); 410374a263eSTao Ma if (ret) { 411374a263eSTao Ma mlog_errno(ret); 412374a263eSTao Ma goto out; 413374a263eSTao Ma } 414374a263eSTao Ma 415374a263eSTao Ma BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)); 416374a263eSTao Ma 417374a263eSTao Ma di = (struct ocfs2_dinode *)di_bh->b_data; 418374a263eSTao Ma *ref_blkno = le64_to_cpu(di->i_refcount_loc); 419374a263eSTao Ma brelse(di_bh); 420374a263eSTao Ma out: 421374a263eSTao Ma return ret; 422374a263eSTao Ma } 423374a263eSTao Ma 424374a263eSTao Ma static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb, 425374a263eSTao Ma struct ocfs2_refcount_tree *tree, int rw) 426374a263eSTao Ma { 427374a263eSTao Ma int ret; 428374a263eSTao Ma 429374a263eSTao Ma ret = ocfs2_refcount_lock(tree, rw); 430374a263eSTao Ma if (ret) { 431374a263eSTao Ma mlog_errno(ret); 432374a263eSTao Ma goto out; 433374a263eSTao Ma } 434374a263eSTao Ma 435374a263eSTao Ma if (rw) 436374a263eSTao Ma down_write(&tree->rf_sem); 437374a263eSTao Ma else 438374a263eSTao Ma down_read(&tree->rf_sem); 439374a263eSTao Ma 440374a263eSTao Ma out: 441374a263eSTao Ma return ret; 442374a263eSTao Ma } 443374a263eSTao Ma 444374a263eSTao Ma /* 445374a263eSTao Ma * Lock the refcount tree pointed by ref_blkno and return the tree. 446374a263eSTao Ma * In most case, we lock the tree and read the refcount block. 447374a263eSTao Ma * So read it here if the caller really needs it. 448374a263eSTao Ma * 449374a263eSTao Ma * If the tree has been re-created by other node, it will free the 450374a263eSTao Ma * old one and re-create it. 451374a263eSTao Ma */ 452374a263eSTao Ma int ocfs2_lock_refcount_tree(struct ocfs2_super *osb, 453374a263eSTao Ma u64 ref_blkno, int rw, 454374a263eSTao Ma struct ocfs2_refcount_tree **ret_tree, 455374a263eSTao Ma struct buffer_head **ref_bh) 456374a263eSTao Ma { 457374a263eSTao Ma int ret, delete_tree = 0; 458374a263eSTao Ma struct ocfs2_refcount_tree *tree = NULL; 459374a263eSTao Ma struct buffer_head *ref_root_bh = NULL; 460374a263eSTao Ma struct ocfs2_refcount_block *rb; 461374a263eSTao Ma 462374a263eSTao Ma again: 463374a263eSTao Ma ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree); 464374a263eSTao Ma if (ret) { 465374a263eSTao Ma mlog_errno(ret); 466374a263eSTao Ma return ret; 467374a263eSTao Ma } 468374a263eSTao Ma 469374a263eSTao Ma ocfs2_refcount_tree_get(tree); 470374a263eSTao Ma 471374a263eSTao Ma ret = __ocfs2_lock_refcount_tree(osb, tree, rw); 472374a263eSTao Ma if (ret) { 473374a263eSTao Ma mlog_errno(ret); 474374a263eSTao Ma ocfs2_refcount_tree_put(tree); 475374a263eSTao Ma goto out; 476374a263eSTao Ma } 477374a263eSTao Ma 478374a263eSTao Ma ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno, 479374a263eSTao Ma &ref_root_bh); 480374a263eSTao Ma if (ret) { 481374a263eSTao Ma mlog_errno(ret); 482374a263eSTao Ma ocfs2_unlock_refcount_tree(osb, tree, rw); 483374a263eSTao Ma ocfs2_refcount_tree_put(tree); 484374a263eSTao Ma goto out; 485374a263eSTao Ma } 486374a263eSTao Ma 487374a263eSTao Ma rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 488374a263eSTao Ma /* 489374a263eSTao Ma * If the refcount block has been freed and re-created, we may need 490374a263eSTao Ma * to recreate the refcount tree also. 491374a263eSTao Ma * 492374a263eSTao Ma * Here we just remove the tree from the rb-tree, and the last 493374a263eSTao Ma * kref holder will unlock and delete this refcount_tree. 494374a263eSTao Ma * Then we goto "again" and ocfs2_get_refcount_tree will create 495374a263eSTao Ma * the new refcount tree for us. 496374a263eSTao Ma */ 497374a263eSTao Ma if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) { 498374a263eSTao Ma if (!tree->rf_removed) { 499374a263eSTao Ma ocfs2_erase_refcount_tree_from_list(osb, tree); 500374a263eSTao Ma tree->rf_removed = 1; 501374a263eSTao Ma delete_tree = 1; 502374a263eSTao Ma } 503374a263eSTao Ma 504374a263eSTao Ma ocfs2_unlock_refcount_tree(osb, tree, rw); 505374a263eSTao Ma /* 506374a263eSTao Ma * We get an extra reference when we create the refcount 507374a263eSTao Ma * tree, so another put will destroy it. 508374a263eSTao Ma */ 509374a263eSTao Ma if (delete_tree) 510374a263eSTao Ma ocfs2_refcount_tree_put(tree); 511374a263eSTao Ma brelse(ref_root_bh); 512374a263eSTao Ma ref_root_bh = NULL; 513374a263eSTao Ma goto again; 514374a263eSTao Ma } 515374a263eSTao Ma 516374a263eSTao Ma *ret_tree = tree; 517374a263eSTao Ma if (ref_bh) { 518374a263eSTao Ma *ref_bh = ref_root_bh; 519374a263eSTao Ma ref_root_bh = NULL; 520374a263eSTao Ma } 521374a263eSTao Ma out: 522374a263eSTao Ma brelse(ref_root_bh); 523374a263eSTao Ma return ret; 524374a263eSTao Ma } 525374a263eSTao Ma 526374a263eSTao Ma void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb, 527374a263eSTao Ma struct ocfs2_refcount_tree *tree, int rw) 528374a263eSTao Ma { 529374a263eSTao Ma if (rw) 530374a263eSTao Ma up_write(&tree->rf_sem); 531374a263eSTao Ma else 532374a263eSTao Ma up_read(&tree->rf_sem); 533374a263eSTao Ma 534374a263eSTao Ma ocfs2_refcount_unlock(tree, rw); 535374a263eSTao Ma ocfs2_refcount_tree_put(tree); 536374a263eSTao Ma } 537374a263eSTao Ma 538374a263eSTao Ma void ocfs2_purge_refcount_trees(struct ocfs2_super *osb) 539374a263eSTao Ma { 540374a263eSTao Ma struct rb_node *node; 541374a263eSTao Ma struct ocfs2_refcount_tree *tree; 542374a263eSTao Ma struct rb_root *root = &osb->osb_rf_lock_tree; 543374a263eSTao Ma 544374a263eSTao Ma while ((node = rb_last(root)) != NULL) { 545374a263eSTao Ma tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node); 546374a263eSTao Ma 547198aac28STao Ma trace_ocfs2_purge_refcount_trees( 548374a263eSTao Ma (unsigned long long) tree->rf_blkno); 549374a263eSTao Ma 550374a263eSTao Ma rb_erase(&tree->rf_node, root); 551374a263eSTao Ma ocfs2_free_refcount_tree(tree); 552374a263eSTao Ma } 553374a263eSTao Ma } 5548bf396deSTao Ma 5558bf396deSTao Ma /* 5568bf396deSTao Ma * Create a refcount tree for an inode. 5578bf396deSTao Ma * We take for granted that the inode is already locked. 5588bf396deSTao Ma */ 5598bf396deSTao Ma static int ocfs2_create_refcount_tree(struct inode *inode, 5608bf396deSTao Ma struct buffer_head *di_bh) 5618bf396deSTao Ma { 5628bf396deSTao Ma int ret; 5638bf396deSTao Ma handle_t *handle = NULL; 5648bf396deSTao Ma struct ocfs2_alloc_context *meta_ac = NULL; 5658bf396deSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 5668bf396deSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 5678bf396deSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5688bf396deSTao Ma struct buffer_head *new_bh = NULL; 5698bf396deSTao Ma struct ocfs2_refcount_block *rb; 5708bf396deSTao Ma struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL; 5718bf396deSTao Ma u16 suballoc_bit_start; 5728bf396deSTao Ma u32 num_got; 5732b6cb576SJoel Becker u64 suballoc_loc, first_blkno; 5748bf396deSTao Ma 5758bf396deSTao Ma BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL); 5768bf396deSTao Ma 577198aac28STao Ma trace_ocfs2_create_refcount_tree( 578198aac28STao Ma (unsigned long long)OCFS2_I(inode)->ip_blkno); 5798bf396deSTao Ma 5808bf396deSTao Ma ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); 5818bf396deSTao Ma if (ret) { 5828bf396deSTao Ma mlog_errno(ret); 5838bf396deSTao Ma goto out; 5848bf396deSTao Ma } 5858bf396deSTao Ma 5868bf396deSTao Ma handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS); 5878bf396deSTao Ma if (IS_ERR(handle)) { 5888bf396deSTao Ma ret = PTR_ERR(handle); 5898bf396deSTao Ma mlog_errno(ret); 5908bf396deSTao Ma goto out; 5918bf396deSTao Ma } 5928bf396deSTao Ma 5938bf396deSTao Ma ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 5948bf396deSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 5958bf396deSTao Ma if (ret) { 5968bf396deSTao Ma mlog_errno(ret); 5978bf396deSTao Ma goto out_commit; 5988bf396deSTao Ma } 5998bf396deSTao Ma 6002b6cb576SJoel Becker ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 6018bf396deSTao Ma &suballoc_bit_start, &num_got, 6028bf396deSTao Ma &first_blkno); 6038bf396deSTao Ma if (ret) { 6048bf396deSTao Ma mlog_errno(ret); 6058bf396deSTao Ma goto out_commit; 6068bf396deSTao Ma } 6078bf396deSTao Ma 6088bf396deSTao Ma new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno); 6098bf396deSTao Ma if (!new_tree) { 6108bf396deSTao Ma ret = -ENOMEM; 6118bf396deSTao Ma mlog_errno(ret); 6128bf396deSTao Ma goto out_commit; 6138bf396deSTao Ma } 6148bf396deSTao Ma 6158bf396deSTao Ma new_bh = sb_getblk(inode->i_sb, first_blkno); 61658796207SRui Xiang if (!new_bh) { 61758796207SRui Xiang ret = -ENOMEM; 61858796207SRui Xiang mlog_errno(ret); 61958796207SRui Xiang goto out_commit; 62058796207SRui Xiang } 6218bf396deSTao Ma ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh); 6228bf396deSTao Ma 6238bf396deSTao Ma ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh, 6248bf396deSTao Ma OCFS2_JOURNAL_ACCESS_CREATE); 6258bf396deSTao Ma if (ret) { 6268bf396deSTao Ma mlog_errno(ret); 6278bf396deSTao Ma goto out_commit; 6288bf396deSTao Ma } 6298bf396deSTao Ma 6308bf396deSTao Ma /* Initialize ocfs2_refcount_block. */ 6318bf396deSTao Ma rb = (struct ocfs2_refcount_block *)new_bh->b_data; 6328bf396deSTao Ma memset(rb, 0, inode->i_sb->s_blocksize); 6338bf396deSTao Ma strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 634b89c5428STiger Yang rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 6352b6cb576SJoel Becker rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc); 6368bf396deSTao Ma rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 6378bf396deSTao Ma rb->rf_fs_generation = cpu_to_le32(osb->fs_generation); 6388bf396deSTao Ma rb->rf_blkno = cpu_to_le64(first_blkno); 6398bf396deSTao Ma rb->rf_count = cpu_to_le32(1); 6408bf396deSTao Ma rb->rf_records.rl_count = 6418bf396deSTao Ma cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb)); 6428bf396deSTao Ma spin_lock(&osb->osb_lock); 6438bf396deSTao Ma rb->rf_generation = osb->s_next_generation++; 6448bf396deSTao Ma spin_unlock(&osb->osb_lock); 6458bf396deSTao Ma 6468bf396deSTao Ma ocfs2_journal_dirty(handle, new_bh); 6478bf396deSTao Ma 6488bf396deSTao Ma spin_lock(&oi->ip_lock); 6498bf396deSTao Ma oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL; 6508bf396deSTao Ma di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 6518bf396deSTao Ma di->i_refcount_loc = cpu_to_le64(first_blkno); 6528bf396deSTao Ma spin_unlock(&oi->ip_lock); 6538bf396deSTao Ma 654198aac28STao Ma trace_ocfs2_create_refcount_tree_blkno((unsigned long long)first_blkno); 6558bf396deSTao Ma 6568bf396deSTao Ma ocfs2_journal_dirty(handle, di_bh); 6578bf396deSTao Ma 6588bf396deSTao Ma /* 6598bf396deSTao Ma * We have to init the tree lock here since it will use 6608bf396deSTao Ma * the generation number to create it. 6618bf396deSTao Ma */ 6628bf396deSTao Ma new_tree->rf_generation = le32_to_cpu(rb->rf_generation); 6638bf396deSTao Ma ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno, 6648bf396deSTao Ma new_tree->rf_generation); 6658bf396deSTao Ma 6668bf396deSTao Ma spin_lock(&osb->osb_lock); 6678bf396deSTao Ma tree = ocfs2_find_refcount_tree(osb, first_blkno); 6688bf396deSTao Ma 6698bf396deSTao Ma /* 6708bf396deSTao Ma * We've just created a new refcount tree in this block. If 6718bf396deSTao Ma * we found a refcount tree on the ocfs2_super, it must be 6728bf396deSTao Ma * one we just deleted. We free the old tree before 6738bf396deSTao Ma * inserting the new tree. 6748bf396deSTao Ma */ 6758bf396deSTao Ma BUG_ON(tree && tree->rf_generation == new_tree->rf_generation); 6768bf396deSTao Ma if (tree) 6778bf396deSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree); 6788bf396deSTao Ma ocfs2_insert_refcount_tree(osb, new_tree); 6798bf396deSTao Ma spin_unlock(&osb->osb_lock); 6808bf396deSTao Ma new_tree = NULL; 6818bf396deSTao Ma if (tree) 6828bf396deSTao Ma ocfs2_refcount_tree_put(tree); 6838bf396deSTao Ma 6848bf396deSTao Ma out_commit: 6858bf396deSTao Ma ocfs2_commit_trans(osb, handle); 6868bf396deSTao Ma 6878bf396deSTao Ma out: 6888bf396deSTao Ma if (new_tree) { 6898bf396deSTao Ma ocfs2_metadata_cache_exit(&new_tree->rf_ci); 6908bf396deSTao Ma kfree(new_tree); 6918bf396deSTao Ma } 6928bf396deSTao Ma 6938bf396deSTao Ma brelse(new_bh); 6948bf396deSTao Ma if (meta_ac) 6958bf396deSTao Ma ocfs2_free_alloc_context(meta_ac); 6968bf396deSTao Ma 6978bf396deSTao Ma return ret; 6988bf396deSTao Ma } 6998bf396deSTao Ma 7008bf396deSTao Ma static int ocfs2_set_refcount_tree(struct inode *inode, 7018bf396deSTao Ma struct buffer_head *di_bh, 7028bf396deSTao Ma u64 refcount_loc) 7038bf396deSTao Ma { 7048bf396deSTao Ma int ret; 7058bf396deSTao Ma handle_t *handle = NULL; 7068bf396deSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 7078bf396deSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 7088bf396deSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7098bf396deSTao Ma struct buffer_head *ref_root_bh = NULL; 7108bf396deSTao Ma struct ocfs2_refcount_block *rb; 7118bf396deSTao Ma struct ocfs2_refcount_tree *ref_tree; 7128bf396deSTao Ma 7138bf396deSTao Ma BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL); 7148bf396deSTao Ma 7158bf396deSTao Ma ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1, 7168bf396deSTao Ma &ref_tree, &ref_root_bh); 7178bf396deSTao Ma if (ret) { 7188bf396deSTao Ma mlog_errno(ret); 7198bf396deSTao Ma return ret; 7208bf396deSTao Ma } 7218bf396deSTao Ma 7228bf396deSTao Ma handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS); 7238bf396deSTao Ma if (IS_ERR(handle)) { 7248bf396deSTao Ma ret = PTR_ERR(handle); 7258bf396deSTao Ma mlog_errno(ret); 7268bf396deSTao Ma goto out; 7278bf396deSTao Ma } 7288bf396deSTao Ma 7298bf396deSTao Ma ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 7308bf396deSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 7318bf396deSTao Ma if (ret) { 7328bf396deSTao Ma mlog_errno(ret); 7338bf396deSTao Ma goto out_commit; 7348bf396deSTao Ma } 7358bf396deSTao Ma 7368bf396deSTao Ma ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh, 7378bf396deSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 7388bf396deSTao Ma if (ret) { 7398bf396deSTao Ma mlog_errno(ret); 7408bf396deSTao Ma goto out_commit; 7418bf396deSTao Ma } 7428bf396deSTao Ma 7438bf396deSTao Ma rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 7448bf396deSTao Ma le32_add_cpu(&rb->rf_count, 1); 7458bf396deSTao Ma 7468bf396deSTao Ma ocfs2_journal_dirty(handle, ref_root_bh); 7478bf396deSTao Ma 7488bf396deSTao Ma spin_lock(&oi->ip_lock); 7498bf396deSTao Ma oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL; 7508bf396deSTao Ma di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 7518bf396deSTao Ma di->i_refcount_loc = cpu_to_le64(refcount_loc); 7528bf396deSTao Ma spin_unlock(&oi->ip_lock); 7538bf396deSTao Ma ocfs2_journal_dirty(handle, di_bh); 7548bf396deSTao Ma 7558bf396deSTao Ma out_commit: 7568bf396deSTao Ma ocfs2_commit_trans(osb, handle); 7578bf396deSTao Ma out: 7588bf396deSTao Ma ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 7598bf396deSTao Ma brelse(ref_root_bh); 7608bf396deSTao Ma 7618bf396deSTao Ma return ret; 7628bf396deSTao Ma } 7638bf396deSTao Ma 7648bf396deSTao Ma int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh) 7658bf396deSTao Ma { 7668bf396deSTao Ma int ret, delete_tree = 0; 7678bf396deSTao Ma handle_t *handle = NULL; 7688bf396deSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 7698bf396deSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 7708bf396deSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7718bf396deSTao Ma struct ocfs2_refcount_block *rb; 7728bf396deSTao Ma struct inode *alloc_inode = NULL; 7738bf396deSTao Ma struct buffer_head *alloc_bh = NULL; 7748bf396deSTao Ma struct buffer_head *blk_bh = NULL; 7758bf396deSTao Ma struct ocfs2_refcount_tree *ref_tree; 7768bf396deSTao Ma int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS; 7778bf396deSTao Ma u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc); 7788bf396deSTao Ma u16 bit = 0; 7798bf396deSTao Ma 7808bf396deSTao Ma if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) 7818bf396deSTao Ma return 0; 7828bf396deSTao Ma 7838bf396deSTao Ma BUG_ON(!ref_blkno); 7848bf396deSTao Ma ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh); 7858bf396deSTao Ma if (ret) { 7868bf396deSTao Ma mlog_errno(ret); 7878bf396deSTao Ma return ret; 7888bf396deSTao Ma } 7898bf396deSTao Ma 7908bf396deSTao Ma rb = (struct ocfs2_refcount_block *)blk_bh->b_data; 7918bf396deSTao Ma 7928bf396deSTao Ma /* 7938bf396deSTao Ma * If we are the last user, we need to free the block. 7948bf396deSTao Ma * So lock the allocator ahead. 7958bf396deSTao Ma */ 7968bf396deSTao Ma if (le32_to_cpu(rb->rf_count) == 1) { 7978bf396deSTao Ma blk = le64_to_cpu(rb->rf_blkno); 7988bf396deSTao Ma bit = le16_to_cpu(rb->rf_suballoc_bit); 79974380c47STao Ma if (rb->rf_suballoc_loc) 80074380c47STao Ma bg_blkno = le64_to_cpu(rb->rf_suballoc_loc); 80174380c47STao Ma else 8028bf396deSTao Ma bg_blkno = ocfs2_which_suballoc_group(blk, bit); 8038bf396deSTao Ma 8048bf396deSTao Ma alloc_inode = ocfs2_get_system_file_inode(osb, 8058bf396deSTao Ma EXTENT_ALLOC_SYSTEM_INODE, 8068bf396deSTao Ma le16_to_cpu(rb->rf_suballoc_slot)); 8078bf396deSTao Ma if (!alloc_inode) { 8088bf396deSTao Ma ret = -ENOMEM; 8098bf396deSTao Ma mlog_errno(ret); 8108bf396deSTao Ma goto out; 8118bf396deSTao Ma } 8128bf396deSTao Ma mutex_lock(&alloc_inode->i_mutex); 8138bf396deSTao Ma 8148bf396deSTao Ma ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1); 8158bf396deSTao Ma if (ret) { 8168bf396deSTao Ma mlog_errno(ret); 8178bf396deSTao Ma goto out_mutex; 8188bf396deSTao Ma } 8198bf396deSTao Ma 8208bf396deSTao Ma credits += OCFS2_SUBALLOC_FREE; 8218bf396deSTao Ma } 8228bf396deSTao Ma 8238bf396deSTao Ma handle = ocfs2_start_trans(osb, credits); 8248bf396deSTao Ma if (IS_ERR(handle)) { 8258bf396deSTao Ma ret = PTR_ERR(handle); 8268bf396deSTao Ma mlog_errno(ret); 8278bf396deSTao Ma goto out_unlock; 8288bf396deSTao Ma } 8298bf396deSTao Ma 8308bf396deSTao Ma ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 8318bf396deSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 8328bf396deSTao Ma if (ret) { 8338bf396deSTao Ma mlog_errno(ret); 8348bf396deSTao Ma goto out_commit; 8358bf396deSTao Ma } 8368bf396deSTao Ma 8378bf396deSTao Ma ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh, 8388bf396deSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 8398bf396deSTao Ma if (ret) { 8408bf396deSTao Ma mlog_errno(ret); 8418bf396deSTao Ma goto out_commit; 8428bf396deSTao Ma } 8438bf396deSTao Ma 8448bf396deSTao Ma spin_lock(&oi->ip_lock); 8458bf396deSTao Ma oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL; 8468bf396deSTao Ma di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 8478bf396deSTao Ma di->i_refcount_loc = 0; 8488bf396deSTao Ma spin_unlock(&oi->ip_lock); 8498bf396deSTao Ma ocfs2_journal_dirty(handle, di_bh); 8508bf396deSTao Ma 8518bf396deSTao Ma le32_add_cpu(&rb->rf_count , -1); 8528bf396deSTao Ma ocfs2_journal_dirty(handle, blk_bh); 8538bf396deSTao Ma 8548bf396deSTao Ma if (!rb->rf_count) { 8558bf396deSTao Ma delete_tree = 1; 8568bf396deSTao Ma ocfs2_erase_refcount_tree_from_list(osb, ref_tree); 8578bf396deSTao Ma ret = ocfs2_free_suballoc_bits(handle, alloc_inode, 8588bf396deSTao Ma alloc_bh, bit, bg_blkno, 1); 8598bf396deSTao Ma if (ret) 8608bf396deSTao Ma mlog_errno(ret); 8618bf396deSTao Ma } 8628bf396deSTao Ma 8638bf396deSTao Ma out_commit: 8648bf396deSTao Ma ocfs2_commit_trans(osb, handle); 8658bf396deSTao Ma out_unlock: 8668bf396deSTao Ma if (alloc_inode) { 8678bf396deSTao Ma ocfs2_inode_unlock(alloc_inode, 1); 8688bf396deSTao Ma brelse(alloc_bh); 8698bf396deSTao Ma } 8708bf396deSTao Ma out_mutex: 8718bf396deSTao Ma if (alloc_inode) { 8728bf396deSTao Ma mutex_unlock(&alloc_inode->i_mutex); 8738bf396deSTao Ma iput(alloc_inode); 8748bf396deSTao Ma } 8758bf396deSTao Ma out: 8768bf396deSTao Ma ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 8778bf396deSTao Ma if (delete_tree) 8788bf396deSTao Ma ocfs2_refcount_tree_put(ref_tree); 8798bf396deSTao Ma brelse(blk_bh); 8808bf396deSTao Ma 8818bf396deSTao Ma return ret; 8828bf396deSTao Ma } 883e73a819dSTao Ma 884e73a819dSTao Ma static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci, 885e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 886e73a819dSTao Ma u64 cpos, unsigned int len, 887e73a819dSTao Ma struct ocfs2_refcount_rec *ret_rec, 888e73a819dSTao Ma int *index) 889e73a819dSTao Ma { 890e73a819dSTao Ma int i = 0; 891e73a819dSTao Ma struct ocfs2_refcount_block *rb = 892e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 893e73a819dSTao Ma struct ocfs2_refcount_rec *rec = NULL; 894e73a819dSTao Ma 895e73a819dSTao Ma for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) { 896e73a819dSTao Ma rec = &rb->rf_records.rl_recs[i]; 897e73a819dSTao Ma 898e73a819dSTao Ma if (le64_to_cpu(rec->r_cpos) + 899e73a819dSTao Ma le32_to_cpu(rec->r_clusters) <= cpos) 900e73a819dSTao Ma continue; 901e73a819dSTao Ma else if (le64_to_cpu(rec->r_cpos) > cpos) 902e73a819dSTao Ma break; 903e73a819dSTao Ma 904e73a819dSTao Ma /* ok, cpos fail in this rec. Just return. */ 905e73a819dSTao Ma if (ret_rec) 906e73a819dSTao Ma *ret_rec = *rec; 907e73a819dSTao Ma goto out; 908e73a819dSTao Ma } 909e73a819dSTao Ma 910e73a819dSTao Ma if (ret_rec) { 911e73a819dSTao Ma /* We meet with a hole here, so fake the rec. */ 912e73a819dSTao Ma ret_rec->r_cpos = cpu_to_le64(cpos); 913e73a819dSTao Ma ret_rec->r_refcount = 0; 914e73a819dSTao Ma if (i < le16_to_cpu(rb->rf_records.rl_used) && 915e73a819dSTao Ma le64_to_cpu(rec->r_cpos) < cpos + len) 916e73a819dSTao Ma ret_rec->r_clusters = 917e73a819dSTao Ma cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos); 918e73a819dSTao Ma else 919e73a819dSTao Ma ret_rec->r_clusters = cpu_to_le32(len); 920e73a819dSTao Ma } 921e73a819dSTao Ma 922e73a819dSTao Ma out: 923e73a819dSTao Ma *index = i; 924e73a819dSTao Ma } 925e73a819dSTao Ma 926e73a819dSTao Ma /* 9278b2c0dbaSTao Ma * Try to remove refcount tree. The mechanism is: 9288b2c0dbaSTao Ma * 1) Check whether i_clusters == 0, if no, exit. 9298b2c0dbaSTao Ma * 2) check whether we have i_xattr_loc in dinode. if yes, exit. 9308b2c0dbaSTao Ma * 3) Check whether we have inline xattr stored outside, if yes, exit. 9318b2c0dbaSTao Ma * 4) Remove the tree. 9328b2c0dbaSTao Ma */ 9338b2c0dbaSTao Ma int ocfs2_try_remove_refcount_tree(struct inode *inode, 9348b2c0dbaSTao Ma struct buffer_head *di_bh) 9358b2c0dbaSTao Ma { 9368b2c0dbaSTao Ma int ret; 9378b2c0dbaSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 9388b2c0dbaSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 9398b2c0dbaSTao Ma 9408b2c0dbaSTao Ma down_write(&oi->ip_xattr_sem); 9418b2c0dbaSTao Ma down_write(&oi->ip_alloc_sem); 9428b2c0dbaSTao Ma 9438b2c0dbaSTao Ma if (oi->ip_clusters) 9448b2c0dbaSTao Ma goto out; 9458b2c0dbaSTao Ma 9468b2c0dbaSTao Ma if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc) 9478b2c0dbaSTao Ma goto out; 9488b2c0dbaSTao Ma 9498b2c0dbaSTao Ma if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL && 9508b2c0dbaSTao Ma ocfs2_has_inline_xattr_value_outside(inode, di)) 9518b2c0dbaSTao Ma goto out; 9528b2c0dbaSTao Ma 9538b2c0dbaSTao Ma ret = ocfs2_remove_refcount_tree(inode, di_bh); 9548b2c0dbaSTao Ma if (ret) 9558b2c0dbaSTao Ma mlog_errno(ret); 9568b2c0dbaSTao Ma out: 9578b2c0dbaSTao Ma up_write(&oi->ip_alloc_sem); 9588b2c0dbaSTao Ma up_write(&oi->ip_xattr_sem); 9598b2c0dbaSTao Ma return 0; 9608b2c0dbaSTao Ma } 9618b2c0dbaSTao Ma 9628b2c0dbaSTao Ma /* 96338a04e43STao Ma * Find the end range for a leaf refcount block indicated by 96438a04e43STao Ma * el->l_recs[index].e_blkno. 96538a04e43STao Ma */ 96638a04e43STao Ma static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci, 96738a04e43STao Ma struct buffer_head *ref_root_bh, 96838a04e43STao Ma struct ocfs2_extent_block *eb, 96938a04e43STao Ma struct ocfs2_extent_list *el, 97038a04e43STao Ma int index, u32 *cpos_end) 97138a04e43STao Ma { 97238a04e43STao Ma int ret, i, subtree_root; 97338a04e43STao Ma u32 cpos; 97438a04e43STao Ma u64 blkno; 97538a04e43STao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 97638a04e43STao Ma struct ocfs2_path *left_path = NULL, *right_path = NULL; 97738a04e43STao Ma struct ocfs2_extent_tree et; 97838a04e43STao Ma struct ocfs2_extent_list *tmp_el; 97938a04e43STao Ma 98038a04e43STao Ma if (index < le16_to_cpu(el->l_next_free_rec) - 1) { 98138a04e43STao Ma /* 98238a04e43STao Ma * We have a extent rec after index, so just use the e_cpos 98338a04e43STao Ma * of the next extent rec. 98438a04e43STao Ma */ 98538a04e43STao Ma *cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos); 98638a04e43STao Ma return 0; 98738a04e43STao Ma } 98838a04e43STao Ma 98938a04e43STao Ma if (!eb || (eb && !eb->h_next_leaf_blk)) { 99038a04e43STao Ma /* 99138a04e43STao Ma * We are the last extent rec, so any high cpos should 99238a04e43STao Ma * be stored in this leaf refcount block. 99338a04e43STao Ma */ 99438a04e43STao Ma *cpos_end = UINT_MAX; 99538a04e43STao Ma return 0; 99638a04e43STao Ma } 99738a04e43STao Ma 99838a04e43STao Ma /* 99938a04e43STao Ma * If the extent block isn't the last one, we have to find 100038a04e43STao Ma * the subtree root between this extent block and the next 100138a04e43STao Ma * leaf extent block and get the corresponding e_cpos from 100238a04e43STao Ma * the subroot. Otherwise we may corrupt the b-tree. 100338a04e43STao Ma */ 100438a04e43STao Ma ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 100538a04e43STao Ma 100638a04e43STao Ma left_path = ocfs2_new_path_from_et(&et); 100738a04e43STao Ma if (!left_path) { 100838a04e43STao Ma ret = -ENOMEM; 100938a04e43STao Ma mlog_errno(ret); 101038a04e43STao Ma goto out; 101138a04e43STao Ma } 101238a04e43STao Ma 101338a04e43STao Ma cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos); 101438a04e43STao Ma ret = ocfs2_find_path(ci, left_path, cpos); 101538a04e43STao Ma if (ret) { 101638a04e43STao Ma mlog_errno(ret); 101738a04e43STao Ma goto out; 101838a04e43STao Ma } 101938a04e43STao Ma 102038a04e43STao Ma right_path = ocfs2_new_path_from_path(left_path); 102138a04e43STao Ma if (!right_path) { 102238a04e43STao Ma ret = -ENOMEM; 102338a04e43STao Ma mlog_errno(ret); 102438a04e43STao Ma goto out; 102538a04e43STao Ma } 102638a04e43STao Ma 102738a04e43STao Ma ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos); 102838a04e43STao Ma if (ret) { 102938a04e43STao Ma mlog_errno(ret); 103038a04e43STao Ma goto out; 103138a04e43STao Ma } 103238a04e43STao Ma 103338a04e43STao Ma ret = ocfs2_find_path(ci, right_path, cpos); 103438a04e43STao Ma if (ret) { 103538a04e43STao Ma mlog_errno(ret); 103638a04e43STao Ma goto out; 103738a04e43STao Ma } 103838a04e43STao Ma 103938a04e43STao Ma subtree_root = ocfs2_find_subtree_root(&et, left_path, 104038a04e43STao Ma right_path); 104138a04e43STao Ma 104238a04e43STao Ma tmp_el = left_path->p_node[subtree_root].el; 104338a04e43STao Ma blkno = left_path->p_node[subtree_root+1].bh->b_blocknr; 10443a251f04SAl Viro for (i = 0; i < le16_to_cpu(tmp_el->l_next_free_rec); i++) { 104538a04e43STao Ma if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) { 104638a04e43STao Ma *cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos); 104738a04e43STao Ma break; 104838a04e43STao Ma } 104938a04e43STao Ma } 105038a04e43STao Ma 10513a251f04SAl Viro BUG_ON(i == le16_to_cpu(tmp_el->l_next_free_rec)); 105238a04e43STao Ma 105338a04e43STao Ma out: 105438a04e43STao Ma ocfs2_free_path(left_path); 105538a04e43STao Ma ocfs2_free_path(right_path); 105638a04e43STao Ma return ret; 105738a04e43STao Ma } 105838a04e43STao Ma 105938a04e43STao Ma /* 1060e73a819dSTao Ma * Given a cpos and len, try to find the refcount record which contains cpos. 1061e73a819dSTao Ma * 1. If cpos can be found in one refcount record, return the record. 1062e73a819dSTao Ma * 2. If cpos can't be found, return a fake record which start from cpos 1063e73a819dSTao Ma * and end at a small value between cpos+len and start of the next record. 1064e73a819dSTao Ma * This fake record has r_refcount = 0. 1065e73a819dSTao Ma */ 1066e73a819dSTao Ma static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci, 1067e73a819dSTao Ma struct buffer_head *ref_root_bh, 1068e73a819dSTao Ma u64 cpos, unsigned int len, 1069e73a819dSTao Ma struct ocfs2_refcount_rec *ret_rec, 1070e73a819dSTao Ma int *index, 1071e73a819dSTao Ma struct buffer_head **ret_bh) 1072e73a819dSTao Ma { 1073e73a819dSTao Ma int ret = 0, i, found; 107438a04e43STao Ma u32 low_cpos, uninitialized_var(cpos_end); 1075e73a819dSTao Ma struct ocfs2_extent_list *el; 107638a04e43STao Ma struct ocfs2_extent_rec *rec = NULL; 107738a04e43STao Ma struct ocfs2_extent_block *eb = NULL; 1078e73a819dSTao Ma struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL; 1079e73a819dSTao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1080e73a819dSTao Ma struct ocfs2_refcount_block *rb = 1081e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1082e73a819dSTao Ma 1083e73a819dSTao Ma if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) { 1084e73a819dSTao Ma ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len, 1085e73a819dSTao Ma ret_rec, index); 1086e73a819dSTao Ma *ret_bh = ref_root_bh; 1087e73a819dSTao Ma get_bh(ref_root_bh); 1088e73a819dSTao Ma return 0; 1089e73a819dSTao Ma } 1090e73a819dSTao Ma 1091e73a819dSTao Ma el = &rb->rf_list; 1092e73a819dSTao Ma low_cpos = cpos & OCFS2_32BIT_POS_MASK; 1093e73a819dSTao Ma 1094e73a819dSTao Ma if (el->l_tree_depth) { 1095e73a819dSTao Ma ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh); 1096e73a819dSTao Ma if (ret) { 1097e73a819dSTao Ma mlog_errno(ret); 1098e73a819dSTao Ma goto out; 1099e73a819dSTao Ma } 1100e73a819dSTao Ma 1101e73a819dSTao Ma eb = (struct ocfs2_extent_block *) eb_bh->b_data; 1102e73a819dSTao Ma el = &eb->h_list; 1103e73a819dSTao Ma 1104e73a819dSTao Ma if (el->l_tree_depth) { 1105e73a819dSTao Ma ocfs2_error(sb, 1106e73a819dSTao Ma "refcount tree %llu has non zero tree " 1107e73a819dSTao Ma "depth in leaf btree tree block %llu\n", 1108e73a819dSTao Ma (unsigned long long)ocfs2_metadata_cache_owner(ci), 1109e73a819dSTao Ma (unsigned long long)eb_bh->b_blocknr); 1110e73a819dSTao Ma ret = -EROFS; 1111e73a819dSTao Ma goto out; 1112e73a819dSTao Ma } 1113e73a819dSTao Ma } 1114e73a819dSTao Ma 1115e73a819dSTao Ma found = 0; 1116e73a819dSTao Ma for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { 1117e73a819dSTao Ma rec = &el->l_recs[i]; 1118e73a819dSTao Ma 1119e73a819dSTao Ma if (le32_to_cpu(rec->e_cpos) <= low_cpos) { 1120e73a819dSTao Ma found = 1; 1121e73a819dSTao Ma break; 1122e73a819dSTao Ma } 1123e73a819dSTao Ma } 1124e73a819dSTao Ma 112538a04e43STao Ma if (found) { 112638a04e43STao Ma ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh, 112738a04e43STao Ma eb, el, i, &cpos_end); 112838a04e43STao Ma if (ret) { 112938a04e43STao Ma mlog_errno(ret); 113038a04e43STao Ma goto out; 113138a04e43STao Ma } 1132e73a819dSTao Ma 113338a04e43STao Ma if (cpos_end < low_cpos + len) 113438a04e43STao Ma len = cpos_end - low_cpos; 1135e73a819dSTao Ma } 1136e73a819dSTao Ma 1137e73a819dSTao Ma ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno), 1138e73a819dSTao Ma &ref_leaf_bh); 1139e73a819dSTao Ma if (ret) { 1140e73a819dSTao Ma mlog_errno(ret); 1141e73a819dSTao Ma goto out; 1142e73a819dSTao Ma } 1143e73a819dSTao Ma 1144e73a819dSTao Ma ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len, 1145e73a819dSTao Ma ret_rec, index); 1146e73a819dSTao Ma *ret_bh = ref_leaf_bh; 1147e73a819dSTao Ma out: 1148e73a819dSTao Ma brelse(eb_bh); 1149e73a819dSTao Ma return ret; 1150e73a819dSTao Ma } 1151e73a819dSTao Ma 1152e73a819dSTao Ma enum ocfs2_ref_rec_contig { 1153e73a819dSTao Ma REF_CONTIG_NONE = 0, 1154e73a819dSTao Ma REF_CONTIG_LEFT, 1155e73a819dSTao Ma REF_CONTIG_RIGHT, 1156e73a819dSTao Ma REF_CONTIG_LEFTRIGHT, 1157e73a819dSTao Ma }; 1158e73a819dSTao Ma 1159e73a819dSTao Ma static enum ocfs2_ref_rec_contig 1160e73a819dSTao Ma ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb, 1161e73a819dSTao Ma int index) 1162e73a819dSTao Ma { 1163e73a819dSTao Ma if ((rb->rf_records.rl_recs[index].r_refcount == 1164e73a819dSTao Ma rb->rf_records.rl_recs[index + 1].r_refcount) && 1165e73a819dSTao Ma (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) + 1166e73a819dSTao Ma le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) == 1167e73a819dSTao Ma le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos))) 1168e73a819dSTao Ma return REF_CONTIG_RIGHT; 1169e73a819dSTao Ma 1170e73a819dSTao Ma return REF_CONTIG_NONE; 1171e73a819dSTao Ma } 1172e73a819dSTao Ma 1173e73a819dSTao Ma static enum ocfs2_ref_rec_contig 1174e73a819dSTao Ma ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb, 1175e73a819dSTao Ma int index) 1176e73a819dSTao Ma { 1177e73a819dSTao Ma enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE; 1178e73a819dSTao Ma 1179e73a819dSTao Ma if (index < le16_to_cpu(rb->rf_records.rl_used) - 1) 1180e73a819dSTao Ma ret = ocfs2_refcount_rec_adjacent(rb, index); 1181e73a819dSTao Ma 1182e73a819dSTao Ma if (index > 0) { 1183e73a819dSTao Ma enum ocfs2_ref_rec_contig tmp; 1184e73a819dSTao Ma 1185e73a819dSTao Ma tmp = ocfs2_refcount_rec_adjacent(rb, index - 1); 1186e73a819dSTao Ma 1187e73a819dSTao Ma if (tmp == REF_CONTIG_RIGHT) { 1188e73a819dSTao Ma if (ret == REF_CONTIG_RIGHT) 1189e73a819dSTao Ma ret = REF_CONTIG_LEFTRIGHT; 1190e73a819dSTao Ma else 1191e73a819dSTao Ma ret = REF_CONTIG_LEFT; 1192e73a819dSTao Ma } 1193e73a819dSTao Ma } 1194e73a819dSTao Ma 1195e73a819dSTao Ma return ret; 1196e73a819dSTao Ma } 1197e73a819dSTao Ma 1198e73a819dSTao Ma static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb, 1199e73a819dSTao Ma int index) 1200e73a819dSTao Ma { 1201e73a819dSTao Ma BUG_ON(rb->rf_records.rl_recs[index].r_refcount != 1202e73a819dSTao Ma rb->rf_records.rl_recs[index+1].r_refcount); 1203e73a819dSTao Ma 1204e73a819dSTao Ma le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters, 1205e73a819dSTao Ma le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters)); 1206e73a819dSTao Ma 1207e73a819dSTao Ma if (index < le16_to_cpu(rb->rf_records.rl_used) - 2) 1208e73a819dSTao Ma memmove(&rb->rf_records.rl_recs[index + 1], 1209e73a819dSTao Ma &rb->rf_records.rl_recs[index + 2], 1210e73a819dSTao Ma sizeof(struct ocfs2_refcount_rec) * 1211e73a819dSTao Ma (le16_to_cpu(rb->rf_records.rl_used) - index - 2)); 1212e73a819dSTao Ma 1213e73a819dSTao Ma memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1], 1214e73a819dSTao Ma 0, sizeof(struct ocfs2_refcount_rec)); 1215e73a819dSTao Ma le16_add_cpu(&rb->rf_records.rl_used, -1); 1216e73a819dSTao Ma } 1217e73a819dSTao Ma 1218e73a819dSTao Ma /* 1219e73a819dSTao Ma * Merge the refcount rec if we are contiguous with the adjacent recs. 1220e73a819dSTao Ma */ 1221e73a819dSTao Ma static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb, 1222e73a819dSTao Ma int index) 1223e73a819dSTao Ma { 1224e73a819dSTao Ma enum ocfs2_ref_rec_contig contig = 1225e73a819dSTao Ma ocfs2_refcount_rec_contig(rb, index); 1226e73a819dSTao Ma 1227e73a819dSTao Ma if (contig == REF_CONTIG_NONE) 1228e73a819dSTao Ma return; 1229e73a819dSTao Ma 1230e73a819dSTao Ma if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) { 1231e73a819dSTao Ma BUG_ON(index == 0); 1232e73a819dSTao Ma index--; 1233e73a819dSTao Ma } 1234e73a819dSTao Ma 1235e73a819dSTao Ma ocfs2_rotate_refcount_rec_left(rb, index); 1236e73a819dSTao Ma 1237e73a819dSTao Ma if (contig == REF_CONTIG_LEFTRIGHT) 1238e73a819dSTao Ma ocfs2_rotate_refcount_rec_left(rb, index); 1239e73a819dSTao Ma } 1240e73a819dSTao Ma 12411823cb0bSTao Ma /* 12421823cb0bSTao Ma * Change the refcount indexed by "index" in ref_bh. 12431823cb0bSTao Ma * If refcount reaches 0, remove it. 12441823cb0bSTao Ma */ 1245e73a819dSTao Ma static int ocfs2_change_refcount_rec(handle_t *handle, 1246e73a819dSTao Ma struct ocfs2_caching_info *ci, 1247e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 12487540c1a7STao Ma int index, int merge, int change) 1249e73a819dSTao Ma { 1250e73a819dSTao Ma int ret; 1251e73a819dSTao Ma struct ocfs2_refcount_block *rb = 1252e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 12531823cb0bSTao Ma struct ocfs2_refcount_list *rl = &rb->rf_records; 12541823cb0bSTao Ma struct ocfs2_refcount_rec *rec = &rl->rl_recs[index]; 1255e73a819dSTao Ma 1256e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1257e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1258e73a819dSTao Ma if (ret) { 1259e73a819dSTao Ma mlog_errno(ret); 1260e73a819dSTao Ma goto out; 1261e73a819dSTao Ma } 1262e73a819dSTao Ma 1263198aac28STao Ma trace_ocfs2_change_refcount_rec( 1264198aac28STao Ma (unsigned long long)ocfs2_metadata_cache_owner(ci), 1265198aac28STao Ma index, le32_to_cpu(rec->r_refcount), change); 1266e73a819dSTao Ma le32_add_cpu(&rec->r_refcount, change); 1267e73a819dSTao Ma 12681823cb0bSTao Ma if (!rec->r_refcount) { 12691823cb0bSTao Ma if (index != le16_to_cpu(rl->rl_used) - 1) { 12701823cb0bSTao Ma memmove(rec, rec + 1, 12711823cb0bSTao Ma (le16_to_cpu(rl->rl_used) - index - 1) * 12721823cb0bSTao Ma sizeof(struct ocfs2_refcount_rec)); 12731823cb0bSTao Ma memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1], 12741823cb0bSTao Ma 0, sizeof(struct ocfs2_refcount_rec)); 12751823cb0bSTao Ma } 12761823cb0bSTao Ma 12771823cb0bSTao Ma le16_add_cpu(&rl->rl_used, -1); 12787540c1a7STao Ma } else if (merge) 1279e73a819dSTao Ma ocfs2_refcount_rec_merge(rb, index); 1280e73a819dSTao Ma 1281ec20cec7SJoel Becker ocfs2_journal_dirty(handle, ref_leaf_bh); 1282e73a819dSTao Ma out: 1283e73a819dSTao Ma return ret; 1284e73a819dSTao Ma } 1285e73a819dSTao Ma 1286e73a819dSTao Ma static int ocfs2_expand_inline_ref_root(handle_t *handle, 1287e73a819dSTao Ma struct ocfs2_caching_info *ci, 1288e73a819dSTao Ma struct buffer_head *ref_root_bh, 1289e73a819dSTao Ma struct buffer_head **ref_leaf_bh, 1290e73a819dSTao Ma struct ocfs2_alloc_context *meta_ac) 1291e73a819dSTao Ma { 1292e73a819dSTao Ma int ret; 1293e73a819dSTao Ma u16 suballoc_bit_start; 1294e73a819dSTao Ma u32 num_got; 12952b6cb576SJoel Becker u64 suballoc_loc, blkno; 1296e73a819dSTao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1297e73a819dSTao Ma struct buffer_head *new_bh = NULL; 1298e73a819dSTao Ma struct ocfs2_refcount_block *new_rb; 1299e73a819dSTao Ma struct ocfs2_refcount_block *root_rb = 1300e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1301e73a819dSTao Ma 1302e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh, 1303e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1304e73a819dSTao Ma if (ret) { 1305e73a819dSTao Ma mlog_errno(ret); 1306e73a819dSTao Ma goto out; 1307e73a819dSTao Ma } 1308e73a819dSTao Ma 13092b6cb576SJoel Becker ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 1310e73a819dSTao Ma &suballoc_bit_start, &num_got, 1311e73a819dSTao Ma &blkno); 1312e73a819dSTao Ma if (ret) { 1313e73a819dSTao Ma mlog_errno(ret); 1314e73a819dSTao Ma goto out; 1315e73a819dSTao Ma } 1316e73a819dSTao Ma 1317e73a819dSTao Ma new_bh = sb_getblk(sb, blkno); 1318e73a819dSTao Ma if (new_bh == NULL) { 13197391a294SRui Xiang ret = -ENOMEM; 1320e73a819dSTao Ma mlog_errno(ret); 1321e73a819dSTao Ma goto out; 1322e73a819dSTao Ma } 1323e73a819dSTao Ma ocfs2_set_new_buffer_uptodate(ci, new_bh); 1324e73a819dSTao Ma 1325e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, new_bh, 1326e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_CREATE); 1327e73a819dSTao Ma if (ret) { 1328e73a819dSTao Ma mlog_errno(ret); 1329e73a819dSTao Ma goto out; 1330e73a819dSTao Ma } 1331e73a819dSTao Ma 1332e73a819dSTao Ma /* 1333e73a819dSTao Ma * Initialize ocfs2_refcount_block. 1334e73a819dSTao Ma * It should contain the same information as the old root. 1335e73a819dSTao Ma * so just memcpy it and change the corresponding field. 1336e73a819dSTao Ma */ 1337e73a819dSTao Ma memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize); 1338e73a819dSTao Ma 1339e73a819dSTao Ma new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1340b89c5428STiger Yang new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 13412b6cb576SJoel Becker new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc); 1342e73a819dSTao Ma new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1343e73a819dSTao Ma new_rb->rf_blkno = cpu_to_le64(blkno); 1344e73a819dSTao Ma new_rb->rf_cpos = cpu_to_le32(0); 1345e73a819dSTao Ma new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr); 1346e73a819dSTao Ma new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL); 1347e73a819dSTao Ma ocfs2_journal_dirty(handle, new_bh); 1348e73a819dSTao Ma 1349e73a819dSTao Ma /* Now change the root. */ 1350e73a819dSTao Ma memset(&root_rb->rf_list, 0, sb->s_blocksize - 1351e73a819dSTao Ma offsetof(struct ocfs2_refcount_block, rf_list)); 1352e73a819dSTao Ma root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb)); 1353e73a819dSTao Ma root_rb->rf_clusters = cpu_to_le32(1); 1354e73a819dSTao Ma root_rb->rf_list.l_next_free_rec = cpu_to_le16(1); 1355e73a819dSTao Ma root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno); 1356e73a819dSTao Ma root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); 1357e73a819dSTao Ma root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL); 1358e73a819dSTao Ma 1359e73a819dSTao Ma ocfs2_journal_dirty(handle, ref_root_bh); 1360e73a819dSTao Ma 1361198aac28STao Ma trace_ocfs2_expand_inline_ref_root((unsigned long long)blkno, 1362e73a819dSTao Ma le16_to_cpu(new_rb->rf_records.rl_used)); 1363e73a819dSTao Ma 1364e73a819dSTao Ma *ref_leaf_bh = new_bh; 1365e73a819dSTao Ma new_bh = NULL; 1366e73a819dSTao Ma out: 1367e73a819dSTao Ma brelse(new_bh); 1368e73a819dSTao Ma return ret; 1369e73a819dSTao Ma } 1370e73a819dSTao Ma 1371e73a819dSTao Ma static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev, 1372e73a819dSTao Ma struct ocfs2_refcount_rec *next) 1373e73a819dSTao Ma { 1374e73a819dSTao Ma if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <= 1375e73a819dSTao Ma ocfs2_get_ref_rec_low_cpos(next)) 1376e73a819dSTao Ma return 1; 1377e73a819dSTao Ma 1378e73a819dSTao Ma return 0; 1379e73a819dSTao Ma } 1380e73a819dSTao Ma 1381e73a819dSTao Ma static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b) 1382e73a819dSTao Ma { 1383e73a819dSTao Ma const struct ocfs2_refcount_rec *l = a, *r = b; 1384e73a819dSTao Ma u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l); 1385e73a819dSTao Ma u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r); 1386e73a819dSTao Ma 1387e73a819dSTao Ma if (l_cpos > r_cpos) 1388e73a819dSTao Ma return 1; 1389e73a819dSTao Ma if (l_cpos < r_cpos) 1390e73a819dSTao Ma return -1; 1391e73a819dSTao Ma return 0; 1392e73a819dSTao Ma } 1393e73a819dSTao Ma 1394e73a819dSTao Ma static int cmp_refcount_rec_by_cpos(const void *a, const void *b) 1395e73a819dSTao Ma { 1396e73a819dSTao Ma const struct ocfs2_refcount_rec *l = a, *r = b; 1397e73a819dSTao Ma u64 l_cpos = le64_to_cpu(l->r_cpos); 1398e73a819dSTao Ma u64 r_cpos = le64_to_cpu(r->r_cpos); 1399e73a819dSTao Ma 1400e73a819dSTao Ma if (l_cpos > r_cpos) 1401e73a819dSTao Ma return 1; 1402e73a819dSTao Ma if (l_cpos < r_cpos) 1403e73a819dSTao Ma return -1; 1404e73a819dSTao Ma return 0; 1405e73a819dSTao Ma } 1406e73a819dSTao Ma 1407e73a819dSTao Ma static void swap_refcount_rec(void *a, void *b, int size) 1408e73a819dSTao Ma { 1409a612543fSFabian Frederick struct ocfs2_refcount_rec *l = a, *r = b; 1410e73a819dSTao Ma 1411a612543fSFabian Frederick swap(*l, *r); 1412e73a819dSTao Ma } 1413e73a819dSTao Ma 1414e73a819dSTao Ma /* 1415e73a819dSTao Ma * The refcount cpos are ordered by their 64bit cpos, 1416e73a819dSTao Ma * But we will use the low 32 bit to be the e_cpos in the b-tree. 1417e73a819dSTao Ma * So we need to make sure that this pos isn't intersected with others. 1418e73a819dSTao Ma * 1419e73a819dSTao Ma * Note: The refcount block is already sorted by their low 32 bit cpos, 1420e73a819dSTao Ma * So just try the middle pos first, and we will exit when we find 1421e73a819dSTao Ma * the good position. 1422e73a819dSTao Ma */ 1423e73a819dSTao Ma static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl, 1424e73a819dSTao Ma u32 *split_pos, int *split_index) 1425e73a819dSTao Ma { 1426e73a819dSTao Ma int num_used = le16_to_cpu(rl->rl_used); 1427e73a819dSTao Ma int delta, middle = num_used / 2; 1428e73a819dSTao Ma 1429e73a819dSTao Ma for (delta = 0; delta < middle; delta++) { 1430e73a819dSTao Ma /* Let's check delta earlier than middle */ 1431e73a819dSTao Ma if (ocfs2_refcount_rec_no_intersect( 1432e73a819dSTao Ma &rl->rl_recs[middle - delta - 1], 1433e73a819dSTao Ma &rl->rl_recs[middle - delta])) { 1434e73a819dSTao Ma *split_index = middle - delta; 1435e73a819dSTao Ma break; 1436e73a819dSTao Ma } 1437e73a819dSTao Ma 1438e73a819dSTao Ma /* For even counts, don't walk off the end */ 1439e73a819dSTao Ma if ((middle + delta + 1) == num_used) 1440e73a819dSTao Ma continue; 1441e73a819dSTao Ma 1442e73a819dSTao Ma /* Now try delta past middle */ 1443e73a819dSTao Ma if (ocfs2_refcount_rec_no_intersect( 1444e73a819dSTao Ma &rl->rl_recs[middle + delta], 1445e73a819dSTao Ma &rl->rl_recs[middle + delta + 1])) { 1446e73a819dSTao Ma *split_index = middle + delta + 1; 1447e73a819dSTao Ma break; 1448e73a819dSTao Ma } 1449e73a819dSTao Ma } 1450e73a819dSTao Ma 1451e73a819dSTao Ma if (delta >= middle) 1452e73a819dSTao Ma return -ENOSPC; 1453e73a819dSTao Ma 1454e73a819dSTao Ma *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]); 1455e73a819dSTao Ma return 0; 1456e73a819dSTao Ma } 1457e73a819dSTao Ma 1458e73a819dSTao Ma static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh, 1459e73a819dSTao Ma struct buffer_head *new_bh, 1460e73a819dSTao Ma u32 *split_cpos) 1461e73a819dSTao Ma { 1462e73a819dSTao Ma int split_index = 0, num_moved, ret; 1463e73a819dSTao Ma u32 cpos = 0; 1464e73a819dSTao Ma struct ocfs2_refcount_block *rb = 1465e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1466e73a819dSTao Ma struct ocfs2_refcount_list *rl = &rb->rf_records; 1467e73a819dSTao Ma struct ocfs2_refcount_block *new_rb = 1468e73a819dSTao Ma (struct ocfs2_refcount_block *)new_bh->b_data; 1469e73a819dSTao Ma struct ocfs2_refcount_list *new_rl = &new_rb->rf_records; 1470e73a819dSTao Ma 1471198aac28STao Ma trace_ocfs2_divide_leaf_refcount_block( 1472e73a819dSTao Ma (unsigned long long)ref_leaf_bh->b_blocknr, 147328748b32SAl Viro le16_to_cpu(rl->rl_count), le16_to_cpu(rl->rl_used)); 1474e73a819dSTao Ma 1475e73a819dSTao Ma /* 1476e73a819dSTao Ma * XXX: Improvement later. 1477e73a819dSTao Ma * If we know all the high 32 bit cpos is the same, no need to sort. 1478e73a819dSTao Ma * 1479e73a819dSTao Ma * In order to make the whole process safe, we do: 1480e73a819dSTao Ma * 1. sort the entries by their low 32 bit cpos first so that we can 1481e73a819dSTao Ma * find the split cpos easily. 1482e73a819dSTao Ma * 2. call ocfs2_insert_extent to insert the new refcount block. 1483e73a819dSTao Ma * 3. move the refcount rec to the new block. 1484e73a819dSTao Ma * 4. sort the entries by their 64 bit cpos. 1485e73a819dSTao Ma * 5. dirty the new_rb and rb. 1486e73a819dSTao Ma */ 1487e73a819dSTao Ma sort(&rl->rl_recs, le16_to_cpu(rl->rl_used), 1488e73a819dSTao Ma sizeof(struct ocfs2_refcount_rec), 1489e73a819dSTao Ma cmp_refcount_rec_by_low_cpos, swap_refcount_rec); 1490e73a819dSTao Ma 1491e73a819dSTao Ma ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index); 1492e73a819dSTao Ma if (ret) { 1493e73a819dSTao Ma mlog_errno(ret); 1494e73a819dSTao Ma return ret; 1495e73a819dSTao Ma } 1496e73a819dSTao Ma 1497e73a819dSTao Ma new_rb->rf_cpos = cpu_to_le32(cpos); 1498e73a819dSTao Ma 1499e73a819dSTao Ma /* move refcount records starting from split_index to the new block. */ 1500e73a819dSTao Ma num_moved = le16_to_cpu(rl->rl_used) - split_index; 1501e73a819dSTao Ma memcpy(new_rl->rl_recs, &rl->rl_recs[split_index], 1502e73a819dSTao Ma num_moved * sizeof(struct ocfs2_refcount_rec)); 1503e73a819dSTao Ma 1504e73a819dSTao Ma /*ok, remove the entries we just moved over to the other block. */ 1505e73a819dSTao Ma memset(&rl->rl_recs[split_index], 0, 1506e73a819dSTao Ma num_moved * sizeof(struct ocfs2_refcount_rec)); 1507e73a819dSTao Ma 1508e73a819dSTao Ma /* change old and new rl_used accordingly. */ 1509e73a819dSTao Ma le16_add_cpu(&rl->rl_used, -num_moved); 151012d4cec9STao Ma new_rl->rl_used = cpu_to_le16(num_moved); 1511e73a819dSTao Ma 1512e73a819dSTao Ma sort(&rl->rl_recs, le16_to_cpu(rl->rl_used), 1513e73a819dSTao Ma sizeof(struct ocfs2_refcount_rec), 1514e73a819dSTao Ma cmp_refcount_rec_by_cpos, swap_refcount_rec); 1515e73a819dSTao Ma 1516e73a819dSTao Ma sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used), 1517e73a819dSTao Ma sizeof(struct ocfs2_refcount_rec), 1518e73a819dSTao Ma cmp_refcount_rec_by_cpos, swap_refcount_rec); 1519e73a819dSTao Ma 1520e73a819dSTao Ma *split_cpos = cpos; 1521e73a819dSTao Ma return 0; 1522e73a819dSTao Ma } 1523e73a819dSTao Ma 1524e73a819dSTao Ma static int ocfs2_new_leaf_refcount_block(handle_t *handle, 1525e73a819dSTao Ma struct ocfs2_caching_info *ci, 1526e73a819dSTao Ma struct buffer_head *ref_root_bh, 1527e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 1528e73a819dSTao Ma struct ocfs2_alloc_context *meta_ac) 1529e73a819dSTao Ma { 1530e73a819dSTao Ma int ret; 1531e73a819dSTao Ma u16 suballoc_bit_start; 1532e73a819dSTao Ma u32 num_got, new_cpos; 15332b6cb576SJoel Becker u64 suballoc_loc, blkno; 1534e73a819dSTao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1535e73a819dSTao Ma struct ocfs2_refcount_block *root_rb = 1536e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1537e73a819dSTao Ma struct buffer_head *new_bh = NULL; 1538e73a819dSTao Ma struct ocfs2_refcount_block *new_rb; 1539e73a819dSTao Ma struct ocfs2_extent_tree ref_et; 1540e73a819dSTao Ma 1541e73a819dSTao Ma BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)); 1542e73a819dSTao Ma 1543e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh, 1544e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1545e73a819dSTao Ma if (ret) { 1546e73a819dSTao Ma mlog_errno(ret); 1547e73a819dSTao Ma goto out; 1548e73a819dSTao Ma } 1549e73a819dSTao Ma 1550e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1551e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1552e73a819dSTao Ma if (ret) { 1553e73a819dSTao Ma mlog_errno(ret); 1554e73a819dSTao Ma goto out; 1555e73a819dSTao Ma } 1556e73a819dSTao Ma 15572b6cb576SJoel Becker ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 1558e73a819dSTao Ma &suballoc_bit_start, &num_got, 1559e73a819dSTao Ma &blkno); 1560e73a819dSTao Ma if (ret) { 1561e73a819dSTao Ma mlog_errno(ret); 1562e73a819dSTao Ma goto out; 1563e73a819dSTao Ma } 1564e73a819dSTao Ma 1565e73a819dSTao Ma new_bh = sb_getblk(sb, blkno); 1566e73a819dSTao Ma if (new_bh == NULL) { 15677391a294SRui Xiang ret = -ENOMEM; 1568e73a819dSTao Ma mlog_errno(ret); 1569e73a819dSTao Ma goto out; 1570e73a819dSTao Ma } 1571e73a819dSTao Ma ocfs2_set_new_buffer_uptodate(ci, new_bh); 1572e73a819dSTao Ma 1573e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, new_bh, 1574e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_CREATE); 1575e73a819dSTao Ma if (ret) { 1576e73a819dSTao Ma mlog_errno(ret); 1577e73a819dSTao Ma goto out; 1578e73a819dSTao Ma } 1579e73a819dSTao Ma 1580e73a819dSTao Ma /* Initialize ocfs2_refcount_block. */ 1581e73a819dSTao Ma new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1582e73a819dSTao Ma memset(new_rb, 0, sb->s_blocksize); 1583e73a819dSTao Ma strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 1584b89c5428STiger Yang new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 15852b6cb576SJoel Becker new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc); 1586e73a819dSTao Ma new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1587e73a819dSTao Ma new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 1588e73a819dSTao Ma new_rb->rf_blkno = cpu_to_le64(blkno); 1589e73a819dSTao Ma new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr); 1590e73a819dSTao Ma new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL); 1591e73a819dSTao Ma new_rb->rf_records.rl_count = 1592e73a819dSTao Ma cpu_to_le16(ocfs2_refcount_recs_per_rb(sb)); 1593e73a819dSTao Ma new_rb->rf_generation = root_rb->rf_generation; 1594e73a819dSTao Ma 1595e73a819dSTao Ma ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos); 1596e73a819dSTao Ma if (ret) { 1597e73a819dSTao Ma mlog_errno(ret); 1598e73a819dSTao Ma goto out; 1599e73a819dSTao Ma } 1600e73a819dSTao Ma 1601e73a819dSTao Ma ocfs2_journal_dirty(handle, ref_leaf_bh); 1602e73a819dSTao Ma ocfs2_journal_dirty(handle, new_bh); 1603e73a819dSTao Ma 1604e73a819dSTao Ma ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh); 1605e73a819dSTao Ma 1606198aac28STao Ma trace_ocfs2_new_leaf_refcount_block( 1607e73a819dSTao Ma (unsigned long long)new_bh->b_blocknr, new_cpos); 1608e73a819dSTao Ma 1609e73a819dSTao Ma /* Insert the new leaf block with the specific offset cpos. */ 1610e73a819dSTao Ma ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr, 1611e73a819dSTao Ma 1, 0, meta_ac); 1612e73a819dSTao Ma if (ret) 1613e73a819dSTao Ma mlog_errno(ret); 1614e73a819dSTao Ma 1615e73a819dSTao Ma out: 1616e73a819dSTao Ma brelse(new_bh); 1617e73a819dSTao Ma return ret; 1618e73a819dSTao Ma } 1619e73a819dSTao Ma 1620e73a819dSTao Ma static int ocfs2_expand_refcount_tree(handle_t *handle, 1621e73a819dSTao Ma struct ocfs2_caching_info *ci, 1622e73a819dSTao Ma struct buffer_head *ref_root_bh, 1623e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 1624e73a819dSTao Ma struct ocfs2_alloc_context *meta_ac) 1625e73a819dSTao Ma { 1626e73a819dSTao Ma int ret; 1627e73a819dSTao Ma struct buffer_head *expand_bh = NULL; 1628e73a819dSTao Ma 1629e73a819dSTao Ma if (ref_root_bh == ref_leaf_bh) { 1630e73a819dSTao Ma /* 1631e73a819dSTao Ma * the old root bh hasn't been expanded to a b-tree, 1632e73a819dSTao Ma * so expand it first. 1633e73a819dSTao Ma */ 1634e73a819dSTao Ma ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh, 1635e73a819dSTao Ma &expand_bh, meta_ac); 1636e73a819dSTao Ma if (ret) { 1637e73a819dSTao Ma mlog_errno(ret); 1638e73a819dSTao Ma goto out; 1639e73a819dSTao Ma } 1640e73a819dSTao Ma } else { 1641e73a819dSTao Ma expand_bh = ref_leaf_bh; 1642e73a819dSTao Ma get_bh(expand_bh); 1643e73a819dSTao Ma } 1644e73a819dSTao Ma 1645e73a819dSTao Ma 1646e73a819dSTao Ma /* Now add a new refcount block into the tree.*/ 1647e73a819dSTao Ma ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh, 1648e73a819dSTao Ma expand_bh, meta_ac); 1649e73a819dSTao Ma if (ret) 1650e73a819dSTao Ma mlog_errno(ret); 1651e73a819dSTao Ma out: 1652e73a819dSTao Ma brelse(expand_bh); 1653e73a819dSTao Ma return ret; 1654e73a819dSTao Ma } 1655e73a819dSTao Ma 1656e73a819dSTao Ma /* 1657e73a819dSTao Ma * Adjust the extent rec in b-tree representing ref_leaf_bh. 1658e73a819dSTao Ma * 1659e73a819dSTao Ma * Only called when we have inserted a new refcount rec at index 0 1660e73a819dSTao Ma * which means ocfs2_extent_rec.e_cpos may need some change. 1661e73a819dSTao Ma */ 1662e73a819dSTao Ma static int ocfs2_adjust_refcount_rec(handle_t *handle, 1663e73a819dSTao Ma struct ocfs2_caching_info *ci, 1664e73a819dSTao Ma struct buffer_head *ref_root_bh, 1665e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 1666e73a819dSTao Ma struct ocfs2_refcount_rec *rec) 1667e73a819dSTao Ma { 1668e73a819dSTao Ma int ret = 0, i; 1669e73a819dSTao Ma u32 new_cpos, old_cpos; 1670e73a819dSTao Ma struct ocfs2_path *path = NULL; 1671e73a819dSTao Ma struct ocfs2_extent_tree et; 1672e73a819dSTao Ma struct ocfs2_refcount_block *rb = 1673e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1674e73a819dSTao Ma struct ocfs2_extent_list *el; 1675e73a819dSTao Ma 1676e73a819dSTao Ma if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) 1677e73a819dSTao Ma goto out; 1678e73a819dSTao Ma 1679e73a819dSTao Ma rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1680e73a819dSTao Ma old_cpos = le32_to_cpu(rb->rf_cpos); 1681e73a819dSTao Ma new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK; 1682e73a819dSTao Ma if (old_cpos <= new_cpos) 1683e73a819dSTao Ma goto out; 1684e73a819dSTao Ma 1685e73a819dSTao Ma ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 1686e73a819dSTao Ma 1687e73a819dSTao Ma path = ocfs2_new_path_from_et(&et); 1688e73a819dSTao Ma if (!path) { 1689e73a819dSTao Ma ret = -ENOMEM; 1690e73a819dSTao Ma mlog_errno(ret); 1691e73a819dSTao Ma goto out; 1692e73a819dSTao Ma } 1693e73a819dSTao Ma 1694e73a819dSTao Ma ret = ocfs2_find_path(ci, path, old_cpos); 1695e73a819dSTao Ma if (ret) { 1696e73a819dSTao Ma mlog_errno(ret); 1697e73a819dSTao Ma goto out; 1698e73a819dSTao Ma } 1699e73a819dSTao Ma 1700e73a819dSTao Ma /* 1701e73a819dSTao Ma * 2 more credits, one for the leaf refcount block, one for 1702e73a819dSTao Ma * the extent block contains the extent rec. 1703e73a819dSTao Ma */ 1704c901fb00STao Ma ret = ocfs2_extend_trans(handle, 2); 1705e73a819dSTao Ma if (ret < 0) { 1706e73a819dSTao Ma mlog_errno(ret); 1707e73a819dSTao Ma goto out; 1708e73a819dSTao Ma } 1709e73a819dSTao Ma 1710e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1711e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1712e73a819dSTao Ma if (ret < 0) { 1713e73a819dSTao Ma mlog_errno(ret); 1714e73a819dSTao Ma goto out; 1715e73a819dSTao Ma } 1716e73a819dSTao Ma 1717e73a819dSTao Ma ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path), 1718e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1719e73a819dSTao Ma if (ret < 0) { 1720e73a819dSTao Ma mlog_errno(ret); 1721e73a819dSTao Ma goto out; 1722e73a819dSTao Ma } 1723e73a819dSTao Ma 1724e73a819dSTao Ma /* change the leaf extent block first. */ 1725e73a819dSTao Ma el = path_leaf_el(path); 1726e73a819dSTao Ma 1727e73a819dSTao Ma for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) 1728e73a819dSTao Ma if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos) 1729e73a819dSTao Ma break; 1730e73a819dSTao Ma 1731e73a819dSTao Ma BUG_ON(i == le16_to_cpu(el->l_next_free_rec)); 1732e73a819dSTao Ma 1733e73a819dSTao Ma el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); 1734e73a819dSTao Ma 1735e73a819dSTao Ma /* change the r_cpos in the leaf block. */ 1736e73a819dSTao Ma rb->rf_cpos = cpu_to_le32(new_cpos); 1737e73a819dSTao Ma 1738e73a819dSTao Ma ocfs2_journal_dirty(handle, path_leaf_bh(path)); 1739e73a819dSTao Ma ocfs2_journal_dirty(handle, ref_leaf_bh); 1740e73a819dSTao Ma 1741e73a819dSTao Ma out: 1742e73a819dSTao Ma ocfs2_free_path(path); 1743e73a819dSTao Ma return ret; 1744e73a819dSTao Ma } 1745e73a819dSTao Ma 1746e73a819dSTao Ma static int ocfs2_insert_refcount_rec(handle_t *handle, 1747e73a819dSTao Ma struct ocfs2_caching_info *ci, 1748e73a819dSTao Ma struct buffer_head *ref_root_bh, 1749e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 1750e73a819dSTao Ma struct ocfs2_refcount_rec *rec, 17517540c1a7STao Ma int index, int merge, 1752e73a819dSTao Ma struct ocfs2_alloc_context *meta_ac) 1753e73a819dSTao Ma { 1754e73a819dSTao Ma int ret; 1755e73a819dSTao Ma struct ocfs2_refcount_block *rb = 1756e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1757e73a819dSTao Ma struct ocfs2_refcount_list *rf_list = &rb->rf_records; 1758e73a819dSTao Ma struct buffer_head *new_bh = NULL; 1759e73a819dSTao Ma 1760e73a819dSTao Ma BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL); 1761e73a819dSTao Ma 1762e73a819dSTao Ma if (rf_list->rl_used == rf_list->rl_count) { 1763e73a819dSTao Ma u64 cpos = le64_to_cpu(rec->r_cpos); 1764e73a819dSTao Ma u32 len = le32_to_cpu(rec->r_clusters); 1765e73a819dSTao Ma 1766e73a819dSTao Ma ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh, 1767e73a819dSTao Ma ref_leaf_bh, meta_ac); 1768e73a819dSTao Ma if (ret) { 1769e73a819dSTao Ma mlog_errno(ret); 1770e73a819dSTao Ma goto out; 1771e73a819dSTao Ma } 1772e73a819dSTao Ma 1773e73a819dSTao Ma ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 1774e73a819dSTao Ma cpos, len, NULL, &index, 1775e73a819dSTao Ma &new_bh); 1776e73a819dSTao Ma if (ret) { 1777e73a819dSTao Ma mlog_errno(ret); 1778e73a819dSTao Ma goto out; 1779e73a819dSTao Ma } 1780e73a819dSTao Ma 1781e73a819dSTao Ma ref_leaf_bh = new_bh; 1782e73a819dSTao Ma rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1783e73a819dSTao Ma rf_list = &rb->rf_records; 1784e73a819dSTao Ma } 1785e73a819dSTao Ma 1786e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1787e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1788e73a819dSTao Ma if (ret) { 1789e73a819dSTao Ma mlog_errno(ret); 1790e73a819dSTao Ma goto out; 1791e73a819dSTao Ma } 1792e73a819dSTao Ma 1793e73a819dSTao Ma if (index < le16_to_cpu(rf_list->rl_used)) 1794e73a819dSTao Ma memmove(&rf_list->rl_recs[index + 1], 1795e73a819dSTao Ma &rf_list->rl_recs[index], 1796e73a819dSTao Ma (le16_to_cpu(rf_list->rl_used) - index) * 1797e73a819dSTao Ma sizeof(struct ocfs2_refcount_rec)); 1798e73a819dSTao Ma 1799198aac28STao Ma trace_ocfs2_insert_refcount_rec( 1800198aac28STao Ma (unsigned long long)ref_leaf_bh->b_blocknr, index, 1801e73a819dSTao Ma (unsigned long long)le64_to_cpu(rec->r_cpos), 1802198aac28STao Ma le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount)); 1803e73a819dSTao Ma 1804e73a819dSTao Ma rf_list->rl_recs[index] = *rec; 1805e73a819dSTao Ma 1806e73a819dSTao Ma le16_add_cpu(&rf_list->rl_used, 1); 1807e73a819dSTao Ma 18087540c1a7STao Ma if (merge) 1809e73a819dSTao Ma ocfs2_refcount_rec_merge(rb, index); 1810e73a819dSTao Ma 1811ec20cec7SJoel Becker ocfs2_journal_dirty(handle, ref_leaf_bh); 1812e73a819dSTao Ma 1813e73a819dSTao Ma if (index == 0) { 1814e73a819dSTao Ma ret = ocfs2_adjust_refcount_rec(handle, ci, 1815e73a819dSTao Ma ref_root_bh, 1816e73a819dSTao Ma ref_leaf_bh, rec); 1817e73a819dSTao Ma if (ret) 1818e73a819dSTao Ma mlog_errno(ret); 1819e73a819dSTao Ma } 1820e73a819dSTao Ma out: 1821e73a819dSTao Ma brelse(new_bh); 1822e73a819dSTao Ma return ret; 1823e73a819dSTao Ma } 1824e73a819dSTao Ma 1825e73a819dSTao Ma /* 1826e73a819dSTao Ma * Split the refcount_rec indexed by "index" in ref_leaf_bh. 1827e73a819dSTao Ma * This is much simple than our b-tree code. 1828e73a819dSTao Ma * split_rec is the new refcount rec we want to insert. 1829e73a819dSTao Ma * If split_rec->r_refcount > 0, we are changing the refcount(in case we 1830e73a819dSTao Ma * increase refcount or decrease a refcount to non-zero). 1831e73a819dSTao Ma * If split_rec->r_refcount == 0, we are punching a hole in current refcount 1832e73a819dSTao Ma * rec( in case we decrease a refcount to zero). 1833e73a819dSTao Ma */ 1834e73a819dSTao Ma static int ocfs2_split_refcount_rec(handle_t *handle, 1835e73a819dSTao Ma struct ocfs2_caching_info *ci, 1836e73a819dSTao Ma struct buffer_head *ref_root_bh, 1837e73a819dSTao Ma struct buffer_head *ref_leaf_bh, 1838e73a819dSTao Ma struct ocfs2_refcount_rec *split_rec, 18397540c1a7STao Ma int index, int merge, 1840e73a819dSTao Ma struct ocfs2_alloc_context *meta_ac, 1841e73a819dSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 1842e73a819dSTao Ma { 1843e73a819dSTao Ma int ret, recs_need; 1844e73a819dSTao Ma u32 len; 1845e73a819dSTao Ma struct ocfs2_refcount_block *rb = 1846e73a819dSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1847e73a819dSTao Ma struct ocfs2_refcount_list *rf_list = &rb->rf_records; 1848e73a819dSTao Ma struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index]; 1849e73a819dSTao Ma struct ocfs2_refcount_rec *tail_rec = NULL; 1850e73a819dSTao Ma struct buffer_head *new_bh = NULL; 1851e73a819dSTao Ma 1852e73a819dSTao Ma BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL); 1853e73a819dSTao Ma 1854198aac28STao Ma trace_ocfs2_split_refcount_rec(le64_to_cpu(orig_rec->r_cpos), 1855198aac28STao Ma le32_to_cpu(orig_rec->r_clusters), 1856198aac28STao Ma le32_to_cpu(orig_rec->r_refcount), 1857e73a819dSTao Ma le64_to_cpu(split_rec->r_cpos), 1858198aac28STao Ma le32_to_cpu(split_rec->r_clusters), 1859198aac28STao Ma le32_to_cpu(split_rec->r_refcount)); 1860e73a819dSTao Ma 1861e73a819dSTao Ma /* 1862e73a819dSTao Ma * If we just need to split the header or tail clusters, 1863e73a819dSTao Ma * no more recs are needed, just split is OK. 1864e73a819dSTao Ma * Otherwise we at least need one new recs. 1865e73a819dSTao Ma */ 1866e73a819dSTao Ma if (!split_rec->r_refcount && 1867e73a819dSTao Ma (split_rec->r_cpos == orig_rec->r_cpos || 1868e73a819dSTao Ma le64_to_cpu(split_rec->r_cpos) + 1869e73a819dSTao Ma le32_to_cpu(split_rec->r_clusters) == 1870e73a819dSTao Ma le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters))) 1871e73a819dSTao Ma recs_need = 0; 1872e73a819dSTao Ma else 1873e73a819dSTao Ma recs_need = 1; 1874e73a819dSTao Ma 1875e73a819dSTao Ma /* 1876e73a819dSTao Ma * We need one more rec if we split in the middle and the new rec have 1877e73a819dSTao Ma * some refcount in it. 1878e73a819dSTao Ma */ 1879e73a819dSTao Ma if (split_rec->r_refcount && 1880e73a819dSTao Ma (split_rec->r_cpos != orig_rec->r_cpos && 1881e73a819dSTao Ma le64_to_cpu(split_rec->r_cpos) + 1882e73a819dSTao Ma le32_to_cpu(split_rec->r_clusters) != 1883e73a819dSTao Ma le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters))) 1884e73a819dSTao Ma recs_need++; 1885e73a819dSTao Ma 1886e73a819dSTao Ma /* If the leaf block don't have enough record, expand it. */ 188712d4cec9STao Ma if (le16_to_cpu(rf_list->rl_used) + recs_need > 188812d4cec9STao Ma le16_to_cpu(rf_list->rl_count)) { 1889e73a819dSTao Ma struct ocfs2_refcount_rec tmp_rec; 1890e73a819dSTao Ma u64 cpos = le64_to_cpu(orig_rec->r_cpos); 1891e73a819dSTao Ma len = le32_to_cpu(orig_rec->r_clusters); 1892e73a819dSTao Ma ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh, 1893e73a819dSTao Ma ref_leaf_bh, meta_ac); 1894e73a819dSTao Ma if (ret) { 1895e73a819dSTao Ma mlog_errno(ret); 1896e73a819dSTao Ma goto out; 1897e73a819dSTao Ma } 1898e73a819dSTao Ma 1899e73a819dSTao Ma /* 1900e73a819dSTao Ma * We have to re-get it since now cpos may be moved to 1901e73a819dSTao Ma * another leaf block. 1902e73a819dSTao Ma */ 1903e73a819dSTao Ma ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 1904e73a819dSTao Ma cpos, len, &tmp_rec, &index, 1905e73a819dSTao Ma &new_bh); 1906e73a819dSTao Ma if (ret) { 1907e73a819dSTao Ma mlog_errno(ret); 1908e73a819dSTao Ma goto out; 1909e73a819dSTao Ma } 1910e73a819dSTao Ma 1911e73a819dSTao Ma ref_leaf_bh = new_bh; 1912e73a819dSTao Ma rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 1913e73a819dSTao Ma rf_list = &rb->rf_records; 1914e73a819dSTao Ma orig_rec = &rf_list->rl_recs[index]; 1915e73a819dSTao Ma } 1916e73a819dSTao Ma 1917e73a819dSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh, 1918e73a819dSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 1919e73a819dSTao Ma if (ret) { 1920e73a819dSTao Ma mlog_errno(ret); 1921e73a819dSTao Ma goto out; 1922e73a819dSTao Ma } 1923e73a819dSTao Ma 1924e73a819dSTao Ma /* 1925e73a819dSTao Ma * We have calculated out how many new records we need and store 1926e73a819dSTao Ma * in recs_need, so spare enough space first by moving the records 1927e73a819dSTao Ma * after "index" to the end. 1928e73a819dSTao Ma */ 1929e73a819dSTao Ma if (index != le16_to_cpu(rf_list->rl_used) - 1) 1930e73a819dSTao Ma memmove(&rf_list->rl_recs[index + 1 + recs_need], 1931e73a819dSTao Ma &rf_list->rl_recs[index + 1], 1932e73a819dSTao Ma (le16_to_cpu(rf_list->rl_used) - index - 1) * 1933e73a819dSTao Ma sizeof(struct ocfs2_refcount_rec)); 1934e73a819dSTao Ma 1935e73a819dSTao Ma len = (le64_to_cpu(orig_rec->r_cpos) + 1936e73a819dSTao Ma le32_to_cpu(orig_rec->r_clusters)) - 1937e73a819dSTao Ma (le64_to_cpu(split_rec->r_cpos) + 1938e73a819dSTao Ma le32_to_cpu(split_rec->r_clusters)); 1939e73a819dSTao Ma 1940e73a819dSTao Ma /* 1941e73a819dSTao Ma * If we have "len", the we will split in the tail and move it 1942e73a819dSTao Ma * to the end of the space we have just spared. 1943e73a819dSTao Ma */ 1944e73a819dSTao Ma if (len) { 1945e73a819dSTao Ma tail_rec = &rf_list->rl_recs[index + recs_need]; 1946e73a819dSTao Ma 1947e73a819dSTao Ma memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec)); 1948e73a819dSTao Ma le64_add_cpu(&tail_rec->r_cpos, 1949e73a819dSTao Ma le32_to_cpu(tail_rec->r_clusters) - len); 195012d4cec9STao Ma tail_rec->r_clusters = cpu_to_le32(len); 1951e73a819dSTao Ma } 1952e73a819dSTao Ma 1953e73a819dSTao Ma /* 1954e73a819dSTao Ma * If the split pos isn't the same as the original one, we need to 1955e73a819dSTao Ma * split in the head. 1956e73a819dSTao Ma * 1957e73a819dSTao Ma * Note: We have the chance that split_rec.r_refcount = 0, 1958e73a819dSTao Ma * recs_need = 0 and len > 0, which means we just cut the head from 1959e73a819dSTao Ma * the orig_rec and in that case we have done some modification in 1960e73a819dSTao Ma * orig_rec above, so the check for r_cpos is faked. 1961e73a819dSTao Ma */ 1962e73a819dSTao Ma if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) { 1963e73a819dSTao Ma len = le64_to_cpu(split_rec->r_cpos) - 1964e73a819dSTao Ma le64_to_cpu(orig_rec->r_cpos); 1965e73a819dSTao Ma orig_rec->r_clusters = cpu_to_le32(len); 1966e73a819dSTao Ma index++; 1967e73a819dSTao Ma } 1968e73a819dSTao Ma 1969e73a819dSTao Ma le16_add_cpu(&rf_list->rl_used, recs_need); 1970e73a819dSTao Ma 1971e73a819dSTao Ma if (split_rec->r_refcount) { 1972e73a819dSTao Ma rf_list->rl_recs[index] = *split_rec; 1973198aac28STao Ma trace_ocfs2_split_refcount_rec_insert( 1974198aac28STao Ma (unsigned long long)ref_leaf_bh->b_blocknr, index, 1975e73a819dSTao Ma (unsigned long long)le64_to_cpu(split_rec->r_cpos), 1976e73a819dSTao Ma le32_to_cpu(split_rec->r_clusters), 1977198aac28STao Ma le32_to_cpu(split_rec->r_refcount)); 1978e73a819dSTao Ma 19797540c1a7STao Ma if (merge) 1980e73a819dSTao Ma ocfs2_refcount_rec_merge(rb, index); 1981e73a819dSTao Ma } 1982e73a819dSTao Ma 1983ec20cec7SJoel Becker ocfs2_journal_dirty(handle, ref_leaf_bh); 1984e73a819dSTao Ma 1985e73a819dSTao Ma out: 1986e73a819dSTao Ma brelse(new_bh); 1987e73a819dSTao Ma return ret; 1988e73a819dSTao Ma } 1989e73a819dSTao Ma 19907540c1a7STao Ma static int __ocfs2_increase_refcount(handle_t *handle, 1991e73a819dSTao Ma struct ocfs2_caching_info *ci, 1992e73a819dSTao Ma struct buffer_head *ref_root_bh, 19937540c1a7STao Ma u64 cpos, u32 len, int merge, 1994e73a819dSTao Ma struct ocfs2_alloc_context *meta_ac, 1995e73a819dSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 1996e73a819dSTao Ma { 1997e73a819dSTao Ma int ret = 0, index; 1998e73a819dSTao Ma struct buffer_head *ref_leaf_bh = NULL; 1999e73a819dSTao Ma struct ocfs2_refcount_rec rec; 2000e73a819dSTao Ma unsigned int set_len = 0; 2001e73a819dSTao Ma 2002198aac28STao Ma trace_ocfs2_increase_refcount_begin( 2003e73a819dSTao Ma (unsigned long long)ocfs2_metadata_cache_owner(ci), 2004e73a819dSTao Ma (unsigned long long)cpos, len); 2005e73a819dSTao Ma 2006e73a819dSTao Ma while (len) { 2007e73a819dSTao Ma ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 2008e73a819dSTao Ma cpos, len, &rec, &index, 2009e73a819dSTao Ma &ref_leaf_bh); 2010e73a819dSTao Ma if (ret) { 2011e73a819dSTao Ma mlog_errno(ret); 2012e73a819dSTao Ma goto out; 2013e73a819dSTao Ma } 2014e73a819dSTao Ma 2015e73a819dSTao Ma set_len = le32_to_cpu(rec.r_clusters); 2016e73a819dSTao Ma 2017e73a819dSTao Ma /* 2018e73a819dSTao Ma * Here we may meet with 3 situations: 2019e73a819dSTao Ma * 2020e73a819dSTao Ma * 1. If we find an already existing record, and the length 2021e73a819dSTao Ma * is the same, cool, we just need to increase the r_refcount 2022e73a819dSTao Ma * and it is OK. 2023e73a819dSTao Ma * 2. If we find a hole, just insert it with r_refcount = 1. 2024e73a819dSTao Ma * 3. If we are in the middle of one extent record, split 2025e73a819dSTao Ma * it. 2026e73a819dSTao Ma */ 2027e73a819dSTao Ma if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos && 2028e73a819dSTao Ma set_len <= len) { 2029198aac28STao Ma trace_ocfs2_increase_refcount_change( 2030198aac28STao Ma (unsigned long long)cpos, set_len, 2031e73a819dSTao Ma le32_to_cpu(rec.r_refcount)); 2032e73a819dSTao Ma ret = ocfs2_change_refcount_rec(handle, ci, 20337540c1a7STao Ma ref_leaf_bh, index, 20347540c1a7STao Ma merge, 1); 2035e73a819dSTao Ma if (ret) { 2036e73a819dSTao Ma mlog_errno(ret); 2037e73a819dSTao Ma goto out; 2038e73a819dSTao Ma } 2039e73a819dSTao Ma } else if (!rec.r_refcount) { 2040e73a819dSTao Ma rec.r_refcount = cpu_to_le32(1); 2041e73a819dSTao Ma 2042198aac28STao Ma trace_ocfs2_increase_refcount_insert( 2043e73a819dSTao Ma (unsigned long long)le64_to_cpu(rec.r_cpos), 2044e73a819dSTao Ma set_len); 2045e73a819dSTao Ma ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh, 2046e73a819dSTao Ma ref_leaf_bh, 20477540c1a7STao Ma &rec, index, 20487540c1a7STao Ma merge, meta_ac); 2049e73a819dSTao Ma if (ret) { 2050e73a819dSTao Ma mlog_errno(ret); 2051e73a819dSTao Ma goto out; 2052e73a819dSTao Ma } 2053e73a819dSTao Ma } else { 2054e73a819dSTao Ma set_len = min((u64)(cpos + len), 2055e73a819dSTao Ma le64_to_cpu(rec.r_cpos) + set_len) - cpos; 2056e73a819dSTao Ma rec.r_cpos = cpu_to_le64(cpos); 2057e73a819dSTao Ma rec.r_clusters = cpu_to_le32(set_len); 2058e73a819dSTao Ma le32_add_cpu(&rec.r_refcount, 1); 2059e73a819dSTao Ma 2060198aac28STao Ma trace_ocfs2_increase_refcount_split( 2061e73a819dSTao Ma (unsigned long long)le64_to_cpu(rec.r_cpos), 2062e73a819dSTao Ma set_len, le32_to_cpu(rec.r_refcount)); 2063e73a819dSTao Ma ret = ocfs2_split_refcount_rec(handle, ci, 2064e73a819dSTao Ma ref_root_bh, ref_leaf_bh, 20657540c1a7STao Ma &rec, index, merge, 2066e73a819dSTao Ma meta_ac, dealloc); 2067e73a819dSTao Ma if (ret) { 2068e73a819dSTao Ma mlog_errno(ret); 2069e73a819dSTao Ma goto out; 2070e73a819dSTao Ma } 2071e73a819dSTao Ma } 2072e73a819dSTao Ma 2073e73a819dSTao Ma cpos += set_len; 2074e73a819dSTao Ma len -= set_len; 2075e73a819dSTao Ma brelse(ref_leaf_bh); 2076e73a819dSTao Ma ref_leaf_bh = NULL; 2077e73a819dSTao Ma } 2078e73a819dSTao Ma 2079e73a819dSTao Ma out: 2080e73a819dSTao Ma brelse(ref_leaf_bh); 2081e73a819dSTao Ma return ret; 2082e73a819dSTao Ma } 20831823cb0bSTao Ma 20841823cb0bSTao Ma static int ocfs2_remove_refcount_extent(handle_t *handle, 20851823cb0bSTao Ma struct ocfs2_caching_info *ci, 20861823cb0bSTao Ma struct buffer_head *ref_root_bh, 20871823cb0bSTao Ma struct buffer_head *ref_leaf_bh, 20881823cb0bSTao Ma struct ocfs2_alloc_context *meta_ac, 20891823cb0bSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 20901823cb0bSTao Ma { 20911823cb0bSTao Ma int ret; 20921823cb0bSTao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 20931823cb0bSTao Ma struct ocfs2_refcount_block *rb = 20941823cb0bSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 20951823cb0bSTao Ma struct ocfs2_extent_tree et; 20961823cb0bSTao Ma 20971823cb0bSTao Ma BUG_ON(rb->rf_records.rl_used); 20981823cb0bSTao Ma 2099198aac28STao Ma trace_ocfs2_remove_refcount_extent( 2100198aac28STao Ma (unsigned long long)ocfs2_metadata_cache_owner(ci), 2101198aac28STao Ma (unsigned long long)ref_leaf_bh->b_blocknr, 2102198aac28STao Ma le32_to_cpu(rb->rf_cpos)); 2103198aac28STao Ma 21041823cb0bSTao Ma ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 21051823cb0bSTao Ma ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos), 21061823cb0bSTao Ma 1, meta_ac, dealloc); 21071823cb0bSTao Ma if (ret) { 21081823cb0bSTao Ma mlog_errno(ret); 21091823cb0bSTao Ma goto out; 21101823cb0bSTao Ma } 21111823cb0bSTao Ma 21121823cb0bSTao Ma ocfs2_remove_from_cache(ci, ref_leaf_bh); 21131823cb0bSTao Ma 21141823cb0bSTao Ma /* 21151823cb0bSTao Ma * add the freed block to the dealloc so that it will be freed 21161823cb0bSTao Ma * when we run dealloc. 21171823cb0bSTao Ma */ 21181823cb0bSTao Ma ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE, 21191823cb0bSTao Ma le16_to_cpu(rb->rf_suballoc_slot), 212074380c47STao Ma le64_to_cpu(rb->rf_suballoc_loc), 21211823cb0bSTao Ma le64_to_cpu(rb->rf_blkno), 21221823cb0bSTao Ma le16_to_cpu(rb->rf_suballoc_bit)); 21231823cb0bSTao Ma if (ret) { 21241823cb0bSTao Ma mlog_errno(ret); 21251823cb0bSTao Ma goto out; 21261823cb0bSTao Ma } 21271823cb0bSTao Ma 21281823cb0bSTao Ma ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh, 21291823cb0bSTao Ma OCFS2_JOURNAL_ACCESS_WRITE); 21301823cb0bSTao Ma if (ret) { 21311823cb0bSTao Ma mlog_errno(ret); 21321823cb0bSTao Ma goto out; 21331823cb0bSTao Ma } 21341823cb0bSTao Ma 21351823cb0bSTao Ma rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 21361823cb0bSTao Ma 21371823cb0bSTao Ma le32_add_cpu(&rb->rf_clusters, -1); 21381823cb0bSTao Ma 21391823cb0bSTao Ma /* 21401823cb0bSTao Ma * check whether we need to restore the root refcount block if 21411823cb0bSTao Ma * there is no leaf extent block at atll. 21421823cb0bSTao Ma */ 21431823cb0bSTao Ma if (!rb->rf_list.l_next_free_rec) { 21441823cb0bSTao Ma BUG_ON(rb->rf_clusters); 21451823cb0bSTao Ma 2146198aac28STao Ma trace_ocfs2_restore_refcount_block( 21471823cb0bSTao Ma (unsigned long long)ref_root_bh->b_blocknr); 21481823cb0bSTao Ma 21491823cb0bSTao Ma rb->rf_flags = 0; 21501823cb0bSTao Ma rb->rf_parent = 0; 21511823cb0bSTao Ma rb->rf_cpos = 0; 21521823cb0bSTao Ma memset(&rb->rf_records, 0, sb->s_blocksize - 21531823cb0bSTao Ma offsetof(struct ocfs2_refcount_block, rf_records)); 21541823cb0bSTao Ma rb->rf_records.rl_count = 21551823cb0bSTao Ma cpu_to_le16(ocfs2_refcount_recs_per_rb(sb)); 21561823cb0bSTao Ma } 21571823cb0bSTao Ma 21581823cb0bSTao Ma ocfs2_journal_dirty(handle, ref_root_bh); 21591823cb0bSTao Ma 21601823cb0bSTao Ma out: 21611823cb0bSTao Ma return ret; 21621823cb0bSTao Ma } 21631823cb0bSTao Ma 21647540c1a7STao Ma int ocfs2_increase_refcount(handle_t *handle, 21657540c1a7STao Ma struct ocfs2_caching_info *ci, 21667540c1a7STao Ma struct buffer_head *ref_root_bh, 21677540c1a7STao Ma u64 cpos, u32 len, 21687540c1a7STao Ma struct ocfs2_alloc_context *meta_ac, 21697540c1a7STao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 21707540c1a7STao Ma { 21717540c1a7STao Ma return __ocfs2_increase_refcount(handle, ci, ref_root_bh, 21727540c1a7STao Ma cpos, len, 1, 21737540c1a7STao Ma meta_ac, dealloc); 21747540c1a7STao Ma } 21757540c1a7STao Ma 21761823cb0bSTao Ma static int ocfs2_decrease_refcount_rec(handle_t *handle, 21771823cb0bSTao Ma struct ocfs2_caching_info *ci, 21781823cb0bSTao Ma struct buffer_head *ref_root_bh, 21791823cb0bSTao Ma struct buffer_head *ref_leaf_bh, 21801823cb0bSTao Ma int index, u64 cpos, unsigned int len, 21811823cb0bSTao Ma struct ocfs2_alloc_context *meta_ac, 21821823cb0bSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 21831823cb0bSTao Ma { 21841823cb0bSTao Ma int ret; 21851823cb0bSTao Ma struct ocfs2_refcount_block *rb = 21861823cb0bSTao Ma (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 21871823cb0bSTao Ma struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index]; 21881823cb0bSTao Ma 21891823cb0bSTao Ma BUG_ON(cpos < le64_to_cpu(rec->r_cpos)); 21901823cb0bSTao Ma BUG_ON(cpos + len > 21911823cb0bSTao Ma le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters)); 21921823cb0bSTao Ma 2193198aac28STao Ma trace_ocfs2_decrease_refcount_rec( 2194198aac28STao Ma (unsigned long long)ocfs2_metadata_cache_owner(ci), 2195198aac28STao Ma (unsigned long long)cpos, len); 2196198aac28STao Ma 21971823cb0bSTao Ma if (cpos == le64_to_cpu(rec->r_cpos) && 21981823cb0bSTao Ma len == le32_to_cpu(rec->r_clusters)) 21991823cb0bSTao Ma ret = ocfs2_change_refcount_rec(handle, ci, 22007540c1a7STao Ma ref_leaf_bh, index, 1, -1); 22011823cb0bSTao Ma else { 22021823cb0bSTao Ma struct ocfs2_refcount_rec split = *rec; 22031823cb0bSTao Ma split.r_cpos = cpu_to_le64(cpos); 22041823cb0bSTao Ma split.r_clusters = cpu_to_le32(len); 22051823cb0bSTao Ma 22061823cb0bSTao Ma le32_add_cpu(&split.r_refcount, -1); 22071823cb0bSTao Ma 22081823cb0bSTao Ma ret = ocfs2_split_refcount_rec(handle, ci, 22091823cb0bSTao Ma ref_root_bh, ref_leaf_bh, 22107540c1a7STao Ma &split, index, 1, 22111823cb0bSTao Ma meta_ac, dealloc); 22121823cb0bSTao Ma } 22131823cb0bSTao Ma 22141823cb0bSTao Ma if (ret) { 22151823cb0bSTao Ma mlog_errno(ret); 22161823cb0bSTao Ma goto out; 22171823cb0bSTao Ma } 22181823cb0bSTao Ma 22191823cb0bSTao Ma /* Remove the leaf refcount block if it contains no refcount record. */ 22201823cb0bSTao Ma if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) { 22211823cb0bSTao Ma ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh, 22221823cb0bSTao Ma ref_leaf_bh, meta_ac, 22231823cb0bSTao Ma dealloc); 22241823cb0bSTao Ma if (ret) 22251823cb0bSTao Ma mlog_errno(ret); 22261823cb0bSTao Ma } 22271823cb0bSTao Ma 22281823cb0bSTao Ma out: 22291823cb0bSTao Ma return ret; 22301823cb0bSTao Ma } 22311823cb0bSTao Ma 22321823cb0bSTao Ma static int __ocfs2_decrease_refcount(handle_t *handle, 22331823cb0bSTao Ma struct ocfs2_caching_info *ci, 22341823cb0bSTao Ma struct buffer_head *ref_root_bh, 22351823cb0bSTao Ma u64 cpos, u32 len, 22361823cb0bSTao Ma struct ocfs2_alloc_context *meta_ac, 22376ae23c55STao Ma struct ocfs2_cached_dealloc_ctxt *dealloc, 22386ae23c55STao Ma int delete) 22391823cb0bSTao Ma { 22401823cb0bSTao Ma int ret = 0, index = 0; 22411823cb0bSTao Ma struct ocfs2_refcount_rec rec; 22421823cb0bSTao Ma unsigned int r_count = 0, r_len; 22431823cb0bSTao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 22441823cb0bSTao Ma struct buffer_head *ref_leaf_bh = NULL; 22451823cb0bSTao Ma 2246198aac28STao Ma trace_ocfs2_decrease_refcount( 22471823cb0bSTao Ma (unsigned long long)ocfs2_metadata_cache_owner(ci), 22486ae23c55STao Ma (unsigned long long)cpos, len, delete); 22491823cb0bSTao Ma 22501823cb0bSTao Ma while (len) { 22511823cb0bSTao Ma ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 22521823cb0bSTao Ma cpos, len, &rec, &index, 22531823cb0bSTao Ma &ref_leaf_bh); 22541823cb0bSTao Ma if (ret) { 22551823cb0bSTao Ma mlog_errno(ret); 22561823cb0bSTao Ma goto out; 22571823cb0bSTao Ma } 22581823cb0bSTao Ma 22591823cb0bSTao Ma r_count = le32_to_cpu(rec.r_refcount); 22601823cb0bSTao Ma BUG_ON(r_count == 0); 22616ae23c55STao Ma if (!delete) 22626ae23c55STao Ma BUG_ON(r_count > 1); 22631823cb0bSTao Ma 22641823cb0bSTao Ma r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) + 22651823cb0bSTao Ma le32_to_cpu(rec.r_clusters)) - cpos; 22661823cb0bSTao Ma 22671823cb0bSTao Ma ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh, 22681823cb0bSTao Ma ref_leaf_bh, index, 22691823cb0bSTao Ma cpos, r_len, 22701823cb0bSTao Ma meta_ac, dealloc); 22711823cb0bSTao Ma if (ret) { 22721823cb0bSTao Ma mlog_errno(ret); 22731823cb0bSTao Ma goto out; 22741823cb0bSTao Ma } 22751823cb0bSTao Ma 22766ae23c55STao Ma if (le32_to_cpu(rec.r_refcount) == 1 && delete) { 22771823cb0bSTao Ma ret = ocfs2_cache_cluster_dealloc(dealloc, 22781823cb0bSTao Ma ocfs2_clusters_to_blocks(sb, cpos), 22791823cb0bSTao Ma r_len); 22801823cb0bSTao Ma if (ret) { 22811823cb0bSTao Ma mlog_errno(ret); 22821823cb0bSTao Ma goto out; 22831823cb0bSTao Ma } 22841823cb0bSTao Ma } 22851823cb0bSTao Ma 22861823cb0bSTao Ma cpos += r_len; 22871823cb0bSTao Ma len -= r_len; 22881823cb0bSTao Ma brelse(ref_leaf_bh); 22891823cb0bSTao Ma ref_leaf_bh = NULL; 22901823cb0bSTao Ma } 22911823cb0bSTao Ma 22921823cb0bSTao Ma out: 22931823cb0bSTao Ma brelse(ref_leaf_bh); 22941823cb0bSTao Ma return ret; 22951823cb0bSTao Ma } 22961823cb0bSTao Ma 22971823cb0bSTao Ma /* Caller must hold refcount tree lock. */ 22981823cb0bSTao Ma int ocfs2_decrease_refcount(struct inode *inode, 22991823cb0bSTao Ma handle_t *handle, u32 cpos, u32 len, 23001823cb0bSTao Ma struct ocfs2_alloc_context *meta_ac, 23016ae23c55STao Ma struct ocfs2_cached_dealloc_ctxt *dealloc, 23026ae23c55STao Ma int delete) 23031823cb0bSTao Ma { 23041823cb0bSTao Ma int ret; 23051823cb0bSTao Ma u64 ref_blkno; 23061823cb0bSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 23071823cb0bSTao Ma struct buffer_head *ref_root_bh = NULL; 23081823cb0bSTao Ma struct ocfs2_refcount_tree *tree; 23091823cb0bSTao Ma 23101823cb0bSTao Ma BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)); 23111823cb0bSTao Ma 23121823cb0bSTao Ma ret = ocfs2_get_refcount_block(inode, &ref_blkno); 23131823cb0bSTao Ma if (ret) { 23141823cb0bSTao Ma mlog_errno(ret); 23151823cb0bSTao Ma goto out; 23161823cb0bSTao Ma } 23171823cb0bSTao Ma 23181823cb0bSTao Ma ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree); 23191823cb0bSTao Ma if (ret) { 23201823cb0bSTao Ma mlog_errno(ret); 23211823cb0bSTao Ma goto out; 23221823cb0bSTao Ma } 23231823cb0bSTao Ma 23241823cb0bSTao Ma ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno, 23251823cb0bSTao Ma &ref_root_bh); 23261823cb0bSTao Ma if (ret) { 23271823cb0bSTao Ma mlog_errno(ret); 23281823cb0bSTao Ma goto out; 23291823cb0bSTao Ma } 23301823cb0bSTao Ma 23311823cb0bSTao Ma ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh, 23326ae23c55STao Ma cpos, len, meta_ac, dealloc, delete); 23331823cb0bSTao Ma if (ret) 23341823cb0bSTao Ma mlog_errno(ret); 23351823cb0bSTao Ma out: 23361823cb0bSTao Ma brelse(ref_root_bh); 23371823cb0bSTao Ma return ret; 23381823cb0bSTao Ma } 23391aa75feaSTao Ma 23401aa75feaSTao Ma /* 23411aa75feaSTao Ma * Mark the already-existing extent at cpos as refcounted for len clusters. 23421aa75feaSTao Ma * This adds the refcount extent flag. 23431aa75feaSTao Ma * 23441aa75feaSTao Ma * If the existing extent is larger than the request, initiate a 23451aa75feaSTao Ma * split. An attempt will be made at merging with adjacent extents. 23461aa75feaSTao Ma * 23471aa75feaSTao Ma * The caller is responsible for passing down meta_ac if we'll need it. 23481aa75feaSTao Ma */ 23491aa75feaSTao Ma static int ocfs2_mark_extent_refcounted(struct inode *inode, 23501aa75feaSTao Ma struct ocfs2_extent_tree *et, 23511aa75feaSTao Ma handle_t *handle, u32 cpos, 23521aa75feaSTao Ma u32 len, u32 phys, 23531aa75feaSTao Ma struct ocfs2_alloc_context *meta_ac, 23541aa75feaSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 23551aa75feaSTao Ma { 23561aa75feaSTao Ma int ret; 23571aa75feaSTao Ma 2358198aac28STao Ma trace_ocfs2_mark_extent_refcounted(OCFS2_I(inode)->ip_blkno, 2359198aac28STao Ma cpos, len, phys); 23601aa75feaSTao Ma 23611aa75feaSTao Ma if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) { 23621aa75feaSTao Ma ocfs2_error(inode->i_sb, "Inode %lu want to use refcount " 23631aa75feaSTao Ma "tree, but the feature bit is not set in the " 23641aa75feaSTao Ma "super block.", inode->i_ino); 23651aa75feaSTao Ma ret = -EROFS; 23661aa75feaSTao Ma goto out; 23671aa75feaSTao Ma } 23681aa75feaSTao Ma 23691aa75feaSTao Ma ret = ocfs2_change_extent_flag(handle, et, cpos, 23701aa75feaSTao Ma len, phys, meta_ac, dealloc, 23711aa75feaSTao Ma OCFS2_EXT_REFCOUNTED, 0); 23721aa75feaSTao Ma if (ret) 23731aa75feaSTao Ma mlog_errno(ret); 23741aa75feaSTao Ma 23751aa75feaSTao Ma out: 23761aa75feaSTao Ma return ret; 23771aa75feaSTao Ma } 2378bcbbb24aSTao Ma 2379bcbbb24aSTao Ma /* 2380bcbbb24aSTao Ma * Given some contiguous physical clusters, calculate what we need 2381bcbbb24aSTao Ma * for modifying their refcount. 2382bcbbb24aSTao Ma */ 2383bcbbb24aSTao Ma static int ocfs2_calc_refcount_meta_credits(struct super_block *sb, 2384bcbbb24aSTao Ma struct ocfs2_caching_info *ci, 2385bcbbb24aSTao Ma struct buffer_head *ref_root_bh, 2386bcbbb24aSTao Ma u64 start_cpos, 2387bcbbb24aSTao Ma u32 clusters, 2388bcbbb24aSTao Ma int *meta_add, 2389bcbbb24aSTao Ma int *credits) 2390bcbbb24aSTao Ma { 2391bcbbb24aSTao Ma int ret = 0, index, ref_blocks = 0, recs_add = 0; 2392bcbbb24aSTao Ma u64 cpos = start_cpos; 2393bcbbb24aSTao Ma struct ocfs2_refcount_block *rb; 2394bcbbb24aSTao Ma struct ocfs2_refcount_rec rec; 2395bcbbb24aSTao Ma struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL; 2396bcbbb24aSTao Ma u32 len; 2397bcbbb24aSTao Ma 2398bcbbb24aSTao Ma while (clusters) { 2399bcbbb24aSTao Ma ret = ocfs2_get_refcount_rec(ci, ref_root_bh, 2400bcbbb24aSTao Ma cpos, clusters, &rec, 2401bcbbb24aSTao Ma &index, &ref_leaf_bh); 2402bcbbb24aSTao Ma if (ret) { 2403bcbbb24aSTao Ma mlog_errno(ret); 2404bcbbb24aSTao Ma goto out; 2405bcbbb24aSTao Ma } 2406bcbbb24aSTao Ma 2407bcbbb24aSTao Ma if (ref_leaf_bh != prev_bh) { 2408bcbbb24aSTao Ma /* 2409bcbbb24aSTao Ma * Now we encounter a new leaf block, so calculate 2410bcbbb24aSTao Ma * whether we need to extend the old leaf. 2411bcbbb24aSTao Ma */ 2412bcbbb24aSTao Ma if (prev_bh) { 2413bcbbb24aSTao Ma rb = (struct ocfs2_refcount_block *) 2414bcbbb24aSTao Ma prev_bh->b_data; 2415bcbbb24aSTao Ma 2416e1bf4cc6SAl Viro if (le16_to_cpu(rb->rf_records.rl_used) + 2417bcbbb24aSTao Ma recs_add > 2418bcbbb24aSTao Ma le16_to_cpu(rb->rf_records.rl_count)) 2419bcbbb24aSTao Ma ref_blocks++; 2420bcbbb24aSTao Ma } 2421bcbbb24aSTao Ma 2422bcbbb24aSTao Ma recs_add = 0; 2423bcbbb24aSTao Ma *credits += 1; 2424bcbbb24aSTao Ma brelse(prev_bh); 2425bcbbb24aSTao Ma prev_bh = ref_leaf_bh; 2426bcbbb24aSTao Ma get_bh(prev_bh); 2427bcbbb24aSTao Ma } 2428bcbbb24aSTao Ma 2429198aac28STao Ma trace_ocfs2_calc_refcount_meta_credits_iterate( 2430bcbbb24aSTao Ma recs_add, (unsigned long long)cpos, clusters, 2431bcbbb24aSTao Ma (unsigned long long)le64_to_cpu(rec.r_cpos), 2432bcbbb24aSTao Ma le32_to_cpu(rec.r_clusters), 2433bcbbb24aSTao Ma le32_to_cpu(rec.r_refcount), index); 2434bcbbb24aSTao Ma 2435bcbbb24aSTao Ma len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) + 2436bcbbb24aSTao Ma le32_to_cpu(rec.r_clusters)) - cpos; 2437bcbbb24aSTao Ma /* 2438bcbbb24aSTao Ma * We record all the records which will be inserted to the 2439bcbbb24aSTao Ma * same refcount block, so that we can tell exactly whether 2440bcbbb24aSTao Ma * we need a new refcount block or not. 24418a2e70c4STao Ma * 24428a2e70c4STao Ma * If we will insert a new one, this is easy and only happens 24438a2e70c4STao Ma * during adding refcounted flag to the extent, so we don't 24448a2e70c4STao Ma * have a chance of spliting. We just need one record. 24458a2e70c4STao Ma * 24468a2e70c4STao Ma * If the refcount rec already exists, that would be a little 24478a2e70c4STao Ma * complicated. we may have to: 24488a2e70c4STao Ma * 1) split at the beginning if the start pos isn't aligned. 24498a2e70c4STao Ma * we need 1 more record in this case. 24508a2e70c4STao Ma * 2) split int the end if the end pos isn't aligned. 24518a2e70c4STao Ma * we need 1 more record in this case. 24528a2e70c4STao Ma * 3) split in the middle because of file system fragmentation. 24538a2e70c4STao Ma * we need 2 more records in this case(we can't detect this 24548a2e70c4STao Ma * beforehand, so always think of the worst case). 2455bcbbb24aSTao Ma */ 2456bcbbb24aSTao Ma if (rec.r_refcount) { 24578a2e70c4STao Ma recs_add += 2; 2458bcbbb24aSTao Ma /* Check whether we need a split at the beginning. */ 2459bcbbb24aSTao Ma if (cpos == start_cpos && 2460bcbbb24aSTao Ma cpos != le64_to_cpu(rec.r_cpos)) 2461bcbbb24aSTao Ma recs_add++; 2462bcbbb24aSTao Ma 2463bcbbb24aSTao Ma /* Check whether we need a split in the end. */ 2464bcbbb24aSTao Ma if (cpos + clusters < le64_to_cpu(rec.r_cpos) + 2465bcbbb24aSTao Ma le32_to_cpu(rec.r_clusters)) 2466bcbbb24aSTao Ma recs_add++; 2467bcbbb24aSTao Ma } else 2468bcbbb24aSTao Ma recs_add++; 2469bcbbb24aSTao Ma 2470bcbbb24aSTao Ma brelse(ref_leaf_bh); 2471bcbbb24aSTao Ma ref_leaf_bh = NULL; 2472bcbbb24aSTao Ma clusters -= len; 2473bcbbb24aSTao Ma cpos += len; 2474bcbbb24aSTao Ma } 2475bcbbb24aSTao Ma 2476bcbbb24aSTao Ma if (prev_bh) { 2477bcbbb24aSTao Ma rb = (struct ocfs2_refcount_block *)prev_bh->b_data; 2478bcbbb24aSTao Ma 2479e1bf4cc6SAl Viro if (le16_to_cpu(rb->rf_records.rl_used) + recs_add > 2480bcbbb24aSTao Ma le16_to_cpu(rb->rf_records.rl_count)) 2481bcbbb24aSTao Ma ref_blocks++; 2482bcbbb24aSTao Ma 2483bcbbb24aSTao Ma *credits += 1; 2484bcbbb24aSTao Ma } 2485bcbbb24aSTao Ma 2486bcbbb24aSTao Ma if (!ref_blocks) 2487bcbbb24aSTao Ma goto out; 2488bcbbb24aSTao Ma 2489bcbbb24aSTao Ma *meta_add += ref_blocks; 2490bcbbb24aSTao Ma *credits += ref_blocks; 2491bcbbb24aSTao Ma 2492bcbbb24aSTao Ma /* 2493bcbbb24aSTao Ma * So we may need ref_blocks to insert into the tree. 2494bcbbb24aSTao Ma * That also means we need to change the b-tree and add that number 2495bcbbb24aSTao Ma * of records since we never merge them. 2496bcbbb24aSTao Ma * We need one more block for expansion since the new created leaf 2497bcbbb24aSTao Ma * block is also full and needs split. 2498bcbbb24aSTao Ma */ 2499bcbbb24aSTao Ma rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 2500bcbbb24aSTao Ma if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) { 2501bcbbb24aSTao Ma struct ocfs2_extent_tree et; 2502bcbbb24aSTao Ma 2503bcbbb24aSTao Ma ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh); 2504bcbbb24aSTao Ma *meta_add += ocfs2_extend_meta_needed(et.et_root_el); 2505bcbbb24aSTao Ma *credits += ocfs2_calc_extend_credits(sb, 250606f9da6eSGoldwyn Rodrigues et.et_root_el); 2507bcbbb24aSTao Ma } else { 2508bcbbb24aSTao Ma *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; 2509bcbbb24aSTao Ma *meta_add += 1; 2510bcbbb24aSTao Ma } 2511bcbbb24aSTao Ma 2512bcbbb24aSTao Ma out: 2513198aac28STao Ma 2514198aac28STao Ma trace_ocfs2_calc_refcount_meta_credits( 2515198aac28STao Ma (unsigned long long)start_cpos, clusters, 2516198aac28STao Ma *meta_add, *credits); 2517bcbbb24aSTao Ma brelse(ref_leaf_bh); 2518bcbbb24aSTao Ma brelse(prev_bh); 2519bcbbb24aSTao Ma return ret; 2520bcbbb24aSTao Ma } 2521bcbbb24aSTao Ma 2522bcbbb24aSTao Ma /* 2523bcbbb24aSTao Ma * For refcount tree, we will decrease some contiguous clusters 2524bcbbb24aSTao Ma * refcount count, so just go through it to see how many blocks 2525bcbbb24aSTao Ma * we gonna touch and whether we need to create new blocks. 2526bcbbb24aSTao Ma * 2527bcbbb24aSTao Ma * Normally the refcount blocks store these refcount should be 2528af901ca1SAndré Goddard Rosa * contiguous also, so that we can get the number easily. 252978f94673STristan Ye * We will at most add split 2 refcount records and 2 more 253078f94673STristan Ye * refcount blocks, so just check it in a rough way. 2531bcbbb24aSTao Ma * 2532bcbbb24aSTao Ma * Caller must hold refcount tree lock. 2533bcbbb24aSTao Ma */ 2534bcbbb24aSTao Ma int ocfs2_prepare_refcount_change_for_del(struct inode *inode, 253578f94673STristan Ye u64 refcount_loc, 2536bcbbb24aSTao Ma u64 phys_blkno, 2537bcbbb24aSTao Ma u32 clusters, 2538bcbbb24aSTao Ma int *credits, 253978f94673STristan Ye int *ref_blocks) 2540bcbbb24aSTao Ma { 254178f94673STristan Ye int ret; 2542bcbbb24aSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 2543bcbbb24aSTao Ma struct buffer_head *ref_root_bh = NULL; 2544bcbbb24aSTao Ma struct ocfs2_refcount_tree *tree; 2545bcbbb24aSTao Ma u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno); 2546bcbbb24aSTao Ma 2547bcbbb24aSTao Ma if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) { 2548bcbbb24aSTao Ma ocfs2_error(inode->i_sb, "Inode %lu want to use refcount " 2549bcbbb24aSTao Ma "tree, but the feature bit is not set in the " 2550bcbbb24aSTao Ma "super block.", inode->i_ino); 2551bcbbb24aSTao Ma ret = -EROFS; 2552bcbbb24aSTao Ma goto out; 2553bcbbb24aSTao Ma } 2554bcbbb24aSTao Ma 2555bcbbb24aSTao Ma BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)); 2556bcbbb24aSTao Ma 2557bcbbb24aSTao Ma ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), 255878f94673STristan Ye refcount_loc, &tree); 2559bcbbb24aSTao Ma if (ret) { 2560bcbbb24aSTao Ma mlog_errno(ret); 2561bcbbb24aSTao Ma goto out; 2562bcbbb24aSTao Ma } 2563bcbbb24aSTao Ma 256478f94673STristan Ye ret = ocfs2_read_refcount_block(&tree->rf_ci, refcount_loc, 2565bcbbb24aSTao Ma &ref_root_bh); 2566bcbbb24aSTao Ma if (ret) { 2567bcbbb24aSTao Ma mlog_errno(ret); 2568bcbbb24aSTao Ma goto out; 2569bcbbb24aSTao Ma } 2570bcbbb24aSTao Ma 2571bcbbb24aSTao Ma ret = ocfs2_calc_refcount_meta_credits(inode->i_sb, 2572bcbbb24aSTao Ma &tree->rf_ci, 2573bcbbb24aSTao Ma ref_root_bh, 2574bcbbb24aSTao Ma start_cpos, clusters, 257578f94673STristan Ye ref_blocks, credits); 2576bcbbb24aSTao Ma if (ret) { 2577bcbbb24aSTao Ma mlog_errno(ret); 2578bcbbb24aSTao Ma goto out; 2579bcbbb24aSTao Ma } 2580bcbbb24aSTao Ma 2581198aac28STao Ma trace_ocfs2_prepare_refcount_change_for_del(*ref_blocks, *credits); 2582bcbbb24aSTao Ma 2583bcbbb24aSTao Ma out: 2584bcbbb24aSTao Ma brelse(ref_root_bh); 2585bcbbb24aSTao Ma return ret; 2586bcbbb24aSTao Ma } 25876f70fa51STao Ma 25886f70fa51STao Ma #define MAX_CONTIG_BYTES 1048576 25896f70fa51STao Ma 25906f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb) 25916f70fa51STao Ma { 25926f70fa51STao Ma return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES); 25936f70fa51STao Ma } 25946f70fa51STao Ma 25956f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb) 25966f70fa51STao Ma { 25976f70fa51STao Ma return ~(ocfs2_cow_contig_clusters(sb) - 1); 25986f70fa51STao Ma } 25996f70fa51STao Ma 26006f70fa51STao Ma /* 26016f70fa51STao Ma * Given an extent that starts at 'start' and an I/O that starts at 'cpos', 26026f70fa51STao Ma * find an offset (start + (n * contig_clusters)) that is closest to cpos 26036f70fa51STao Ma * while still being less than or equal to it. 26046f70fa51STao Ma * 26056f70fa51STao Ma * The goal is to break the extent at a multiple of contig_clusters. 26066f70fa51STao Ma */ 26076f70fa51STao Ma static inline unsigned int ocfs2_cow_align_start(struct super_block *sb, 26086f70fa51STao Ma unsigned int start, 26096f70fa51STao Ma unsigned int cpos) 26106f70fa51STao Ma { 26116f70fa51STao Ma BUG_ON(start > cpos); 26126f70fa51STao Ma 26136f70fa51STao Ma return start + ((cpos - start) & ocfs2_cow_contig_mask(sb)); 26146f70fa51STao Ma } 26156f70fa51STao Ma 26166f70fa51STao Ma /* 26176f70fa51STao Ma * Given a cluster count of len, pad it out so that it is a multiple 26186f70fa51STao Ma * of contig_clusters. 26196f70fa51STao Ma */ 26206f70fa51STao Ma static inline unsigned int ocfs2_cow_align_length(struct super_block *sb, 26216f70fa51STao Ma unsigned int len) 26226f70fa51STao Ma { 26236f70fa51STao Ma unsigned int padded = 26246f70fa51STao Ma (len + (ocfs2_cow_contig_clusters(sb) - 1)) & 26256f70fa51STao Ma ocfs2_cow_contig_mask(sb); 26266f70fa51STao Ma 26276f70fa51STao Ma /* Did we wrap? */ 26286f70fa51STao Ma if (padded < len) 26296f70fa51STao Ma padded = UINT_MAX; 26306f70fa51STao Ma 26316f70fa51STao Ma return padded; 26326f70fa51STao Ma } 26336f70fa51STao Ma 26346f70fa51STao Ma /* 26356f70fa51STao Ma * Calculate out the start and number of virtual clusters we need to to CoW. 26366f70fa51STao Ma * 26376f70fa51STao Ma * cpos is vitual start cluster position we want to do CoW in a 26386f70fa51STao Ma * file and write_len is the cluster length. 263937f8a2bfSTao Ma * max_cpos is the place where we want to stop CoW intentionally. 26406f70fa51STao Ma * 26416f70fa51STao Ma * Normal we will start CoW from the beginning of extent record cotaining cpos. 26426f70fa51STao Ma * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we 26436f70fa51STao Ma * get good I/O from the resulting extent tree. 26446f70fa51STao Ma */ 26456f70fa51STao Ma static int ocfs2_refcount_cal_cow_clusters(struct inode *inode, 2646913580b4STao Ma struct ocfs2_extent_list *el, 26476f70fa51STao Ma u32 cpos, 26486f70fa51STao Ma u32 write_len, 264937f8a2bfSTao Ma u32 max_cpos, 26506f70fa51STao Ma u32 *cow_start, 26516f70fa51STao Ma u32 *cow_len) 26526f70fa51STao Ma { 26536f70fa51STao Ma int ret = 0; 26546f70fa51STao Ma int tree_height = le16_to_cpu(el->l_tree_depth), i; 26556f70fa51STao Ma struct buffer_head *eb_bh = NULL; 26566f70fa51STao Ma struct ocfs2_extent_block *eb = NULL; 26576f70fa51STao Ma struct ocfs2_extent_rec *rec; 26586f70fa51STao Ma unsigned int want_clusters, rec_end = 0; 26596f70fa51STao Ma int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb); 26606f70fa51STao Ma int leaf_clusters; 26616f70fa51STao Ma 266237f8a2bfSTao Ma BUG_ON(cpos + write_len > max_cpos); 266337f8a2bfSTao Ma 26646f70fa51STao Ma if (tree_height > 0) { 26656f70fa51STao Ma ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh); 26666f70fa51STao Ma if (ret) { 26676f70fa51STao Ma mlog_errno(ret); 26686f70fa51STao Ma goto out; 26696f70fa51STao Ma } 26706f70fa51STao Ma 26716f70fa51STao Ma eb = (struct ocfs2_extent_block *) eb_bh->b_data; 26726f70fa51STao Ma el = &eb->h_list; 26736f70fa51STao Ma 26746f70fa51STao Ma if (el->l_tree_depth) { 26756f70fa51STao Ma ocfs2_error(inode->i_sb, 26766f70fa51STao Ma "Inode %lu has non zero tree depth in " 26776f70fa51STao Ma "leaf block %llu\n", inode->i_ino, 26786f70fa51STao Ma (unsigned long long)eb_bh->b_blocknr); 26796f70fa51STao Ma ret = -EROFS; 26806f70fa51STao Ma goto out; 26816f70fa51STao Ma } 26826f70fa51STao Ma } 26836f70fa51STao Ma 26846f70fa51STao Ma *cow_len = 0; 26856f70fa51STao Ma for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 26866f70fa51STao Ma rec = &el->l_recs[i]; 26876f70fa51STao Ma 26886f70fa51STao Ma if (ocfs2_is_empty_extent(rec)) { 26896f70fa51STao Ma mlog_bug_on_msg(i != 0, "Inode %lu has empty record in " 26906f70fa51STao Ma "index %d\n", inode->i_ino, i); 26916f70fa51STao Ma continue; 26926f70fa51STao Ma } 26936f70fa51STao Ma 26946f70fa51STao Ma if (le32_to_cpu(rec->e_cpos) + 26956f70fa51STao Ma le16_to_cpu(rec->e_leaf_clusters) <= cpos) 26966f70fa51STao Ma continue; 26976f70fa51STao Ma 26986f70fa51STao Ma if (*cow_len == 0) { 26996f70fa51STao Ma /* 27006f70fa51STao Ma * We should find a refcounted record in the 27016f70fa51STao Ma * first pass. 27026f70fa51STao Ma */ 27036f70fa51STao Ma BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED)); 27046f70fa51STao Ma *cow_start = le32_to_cpu(rec->e_cpos); 27056f70fa51STao Ma } 27066f70fa51STao Ma 27076f70fa51STao Ma /* 270837f8a2bfSTao Ma * If we encounter a hole, a non-refcounted record or 270937f8a2bfSTao Ma * pass the max_cpos, stop the search. 27106f70fa51STao Ma */ 27116f70fa51STao Ma if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) || 271237f8a2bfSTao Ma (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) || 271337f8a2bfSTao Ma (max_cpos <= le32_to_cpu(rec->e_cpos))) 27146f70fa51STao Ma break; 27156f70fa51STao Ma 27166f70fa51STao Ma leaf_clusters = le16_to_cpu(rec->e_leaf_clusters); 27176f70fa51STao Ma rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters; 271837f8a2bfSTao Ma if (rec_end > max_cpos) { 271937f8a2bfSTao Ma rec_end = max_cpos; 272037f8a2bfSTao Ma leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos); 272137f8a2bfSTao Ma } 27226f70fa51STao Ma 27236f70fa51STao Ma /* 27246f70fa51STao Ma * How many clusters do we actually need from 27256f70fa51STao Ma * this extent? First we see how many we actually 27266f70fa51STao Ma * need to complete the write. If that's smaller 27276f70fa51STao Ma * than contig_clusters, we try for contig_clusters. 27286f70fa51STao Ma */ 27296f70fa51STao Ma if (!*cow_len) 27306f70fa51STao Ma want_clusters = write_len; 27316f70fa51STao Ma else 27326f70fa51STao Ma want_clusters = (cpos + write_len) - 27336f70fa51STao Ma (*cow_start + *cow_len); 27346f70fa51STao Ma if (want_clusters < contig_clusters) 27356f70fa51STao Ma want_clusters = contig_clusters; 27366f70fa51STao Ma 27376f70fa51STao Ma /* 27386f70fa51STao Ma * If the write does not cover the whole extent, we 27396f70fa51STao Ma * need to calculate how we're going to split the extent. 27406f70fa51STao Ma * We try to do it on contig_clusters boundaries. 27416f70fa51STao Ma * 27426f70fa51STao Ma * Any extent smaller than contig_clusters will be 27436f70fa51STao Ma * CoWed in its entirety. 27446f70fa51STao Ma */ 27456f70fa51STao Ma if (leaf_clusters <= contig_clusters) 27466f70fa51STao Ma *cow_len += leaf_clusters; 27476f70fa51STao Ma else if (*cow_len || (*cow_start == cpos)) { 27486f70fa51STao Ma /* 27496f70fa51STao Ma * This extent needs to be CoW'd from its 27506f70fa51STao Ma * beginning, so all we have to do is compute 27516f70fa51STao Ma * how many clusters to grab. We align 27526f70fa51STao Ma * want_clusters to the edge of contig_clusters 27536f70fa51STao Ma * to get better I/O. 27546f70fa51STao Ma */ 27556f70fa51STao Ma want_clusters = ocfs2_cow_align_length(inode->i_sb, 27566f70fa51STao Ma want_clusters); 27576f70fa51STao Ma 27586f70fa51STao Ma if (leaf_clusters < want_clusters) 27596f70fa51STao Ma *cow_len += leaf_clusters; 27606f70fa51STao Ma else 27616f70fa51STao Ma *cow_len += want_clusters; 27626f70fa51STao Ma } else if ((*cow_start + contig_clusters) >= 27636f70fa51STao Ma (cpos + write_len)) { 27646f70fa51STao Ma /* 27656f70fa51STao Ma * Breaking off contig_clusters at the front 27666f70fa51STao Ma * of the extent will cover our write. That's 27676f70fa51STao Ma * easy. 27686f70fa51STao Ma */ 27696f70fa51STao Ma *cow_len = contig_clusters; 27706f70fa51STao Ma } else if ((rec_end - cpos) <= contig_clusters) { 27716f70fa51STao Ma /* 27726f70fa51STao Ma * Breaking off contig_clusters at the tail of 27736f70fa51STao Ma * this extent will cover cpos. 27746f70fa51STao Ma */ 27756f70fa51STao Ma *cow_start = rec_end - contig_clusters; 27766f70fa51STao Ma *cow_len = contig_clusters; 27776f70fa51STao Ma } else if ((rec_end - cpos) <= want_clusters) { 27786f70fa51STao Ma /* 27796f70fa51STao Ma * While we can't fit the entire write in this 27806f70fa51STao Ma * extent, we know that the write goes from cpos 27816f70fa51STao Ma * to the end of the extent. Break that off. 27826f70fa51STao Ma * We try to break it at some multiple of 27836f70fa51STao Ma * contig_clusters from the front of the extent. 27846f70fa51STao Ma * Failing that (ie, cpos is within 27856f70fa51STao Ma * contig_clusters of the front), we'll CoW the 27866f70fa51STao Ma * entire extent. 27876f70fa51STao Ma */ 27886f70fa51STao Ma *cow_start = ocfs2_cow_align_start(inode->i_sb, 27896f70fa51STao Ma *cow_start, cpos); 27906f70fa51STao Ma *cow_len = rec_end - *cow_start; 27916f70fa51STao Ma } else { 27926f70fa51STao Ma /* 27936f70fa51STao Ma * Ok, the entire write lives in the middle of 27946f70fa51STao Ma * this extent. Let's try to slice the extent up 27956f70fa51STao Ma * nicely. Optimally, our CoW region starts at 27966f70fa51STao Ma * m*contig_clusters from the beginning of the 27976f70fa51STao Ma * extent and goes for n*contig_clusters, 27986f70fa51STao Ma * covering the entire write. 27996f70fa51STao Ma */ 28006f70fa51STao Ma *cow_start = ocfs2_cow_align_start(inode->i_sb, 28016f70fa51STao Ma *cow_start, cpos); 28026f70fa51STao Ma 28036f70fa51STao Ma want_clusters = (cpos + write_len) - *cow_start; 28046f70fa51STao Ma want_clusters = ocfs2_cow_align_length(inode->i_sb, 28056f70fa51STao Ma want_clusters); 28066f70fa51STao Ma if (*cow_start + want_clusters <= rec_end) 28076f70fa51STao Ma *cow_len = want_clusters; 28086f70fa51STao Ma else 28096f70fa51STao Ma *cow_len = rec_end - *cow_start; 28106f70fa51STao Ma } 28116f70fa51STao Ma 28126f70fa51STao Ma /* Have we covered our entire write yet? */ 28136f70fa51STao Ma if ((*cow_start + *cow_len) >= (cpos + write_len)) 28146f70fa51STao Ma break; 28156f70fa51STao Ma 28166f70fa51STao Ma /* 28176f70fa51STao Ma * If we reach the end of the extent block and don't get enough 28186f70fa51STao Ma * clusters, continue with the next extent block if possible. 28196f70fa51STao Ma */ 28206f70fa51STao Ma if (i + 1 == le16_to_cpu(el->l_next_free_rec) && 28216f70fa51STao Ma eb && eb->h_next_leaf_blk) { 28226f70fa51STao Ma brelse(eb_bh); 28236f70fa51STao Ma eb_bh = NULL; 28246f70fa51STao Ma 28256f70fa51STao Ma ret = ocfs2_read_extent_block(INODE_CACHE(inode), 28266f70fa51STao Ma le64_to_cpu(eb->h_next_leaf_blk), 28276f70fa51STao Ma &eb_bh); 28286f70fa51STao Ma if (ret) { 28296f70fa51STao Ma mlog_errno(ret); 28306f70fa51STao Ma goto out; 28316f70fa51STao Ma } 28326f70fa51STao Ma 28336f70fa51STao Ma eb = (struct ocfs2_extent_block *) eb_bh->b_data; 28346f70fa51STao Ma el = &eb->h_list; 28356f70fa51STao Ma i = -1; 28366f70fa51STao Ma } 28376f70fa51STao Ma } 28386f70fa51STao Ma 28396f70fa51STao Ma out: 28406f70fa51STao Ma brelse(eb_bh); 28416f70fa51STao Ma return ret; 28426f70fa51STao Ma } 28436f70fa51STao Ma 28446f70fa51STao Ma /* 28456f70fa51STao Ma * Prepare meta_ac, data_ac and calculate credits when we want to add some 28466f70fa51STao Ma * num_clusters in data_tree "et" and change the refcount for the old 28476f70fa51STao Ma * clusters(starting form p_cluster) in the refcount tree. 28486f70fa51STao Ma * 28496f70fa51STao Ma * Note: 28506f70fa51STao Ma * 1. since we may split the old tree, so we at most will need num_clusters + 2 28516f70fa51STao Ma * more new leaf records. 28526f70fa51STao Ma * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so 28536f70fa51STao Ma * just give data_ac = NULL. 28546f70fa51STao Ma */ 28556f70fa51STao Ma static int ocfs2_lock_refcount_allocators(struct super_block *sb, 28566f70fa51STao Ma u32 p_cluster, u32 num_clusters, 28576f70fa51STao Ma struct ocfs2_extent_tree *et, 28586f70fa51STao Ma struct ocfs2_caching_info *ref_ci, 28596f70fa51STao Ma struct buffer_head *ref_root_bh, 28606f70fa51STao Ma struct ocfs2_alloc_context **meta_ac, 28616f70fa51STao Ma struct ocfs2_alloc_context **data_ac, 28626f70fa51STao Ma int *credits) 28636f70fa51STao Ma { 28646f70fa51STao Ma int ret = 0, meta_add = 0; 28656f70fa51STao Ma int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et); 28666f70fa51STao Ma 28676f70fa51STao Ma if (num_free_extents < 0) { 28686f70fa51STao Ma ret = num_free_extents; 28696f70fa51STao Ma mlog_errno(ret); 28706f70fa51STao Ma goto out; 28716f70fa51STao Ma } 28726f70fa51STao Ma 28736f70fa51STao Ma if (num_free_extents < num_clusters + 2) 28746f70fa51STao Ma meta_add = 28756f70fa51STao Ma ocfs2_extend_meta_needed(et->et_root_el); 28766f70fa51STao Ma 287706f9da6eSGoldwyn Rodrigues *credits += ocfs2_calc_extend_credits(sb, et->et_root_el); 28786f70fa51STao Ma 28796f70fa51STao Ma ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh, 28806f70fa51STao Ma p_cluster, num_clusters, 28816f70fa51STao Ma &meta_add, credits); 28826f70fa51STao Ma if (ret) { 28836f70fa51STao Ma mlog_errno(ret); 28846f70fa51STao Ma goto out; 28856f70fa51STao Ma } 28866f70fa51STao Ma 2887198aac28STao Ma trace_ocfs2_lock_refcount_allocators(meta_add, *credits); 28886f70fa51STao Ma ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add, 28896f70fa51STao Ma meta_ac); 28906f70fa51STao Ma if (ret) { 28916f70fa51STao Ma mlog_errno(ret); 28926f70fa51STao Ma goto out; 28936f70fa51STao Ma } 28946f70fa51STao Ma 28956f70fa51STao Ma if (data_ac) { 28966f70fa51STao Ma ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters, 28976f70fa51STao Ma data_ac); 28986f70fa51STao Ma if (ret) 28996f70fa51STao Ma mlog_errno(ret); 29006f70fa51STao Ma } 29016f70fa51STao Ma 29026f70fa51STao Ma out: 29036f70fa51STao Ma if (ret) { 29046f70fa51STao Ma if (*meta_ac) { 29056f70fa51STao Ma ocfs2_free_alloc_context(*meta_ac); 29066f70fa51STao Ma *meta_ac = NULL; 29076f70fa51STao Ma } 29086f70fa51STao Ma } 29096f70fa51STao Ma 29106f70fa51STao Ma return ret; 29116f70fa51STao Ma } 29126f70fa51STao Ma 29136f70fa51STao Ma static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh) 29146f70fa51STao Ma { 29156f70fa51STao Ma BUG_ON(buffer_dirty(bh)); 29166f70fa51STao Ma 29176f70fa51STao Ma clear_buffer_mapped(bh); 29186f70fa51STao Ma 29196f70fa51STao Ma return 0; 29206f70fa51STao Ma } 29216f70fa51STao Ma 29223e19a25eSTristan Ye int ocfs2_duplicate_clusters_by_page(handle_t *handle, 2923c7dd3392STiger Yang struct inode *inode, 29246f70fa51STao Ma u32 cpos, u32 old_cluster, 29256f70fa51STao Ma u32 new_cluster, u32 new_len) 29266f70fa51STao Ma { 29276f70fa51STao Ma int ret = 0, partial; 2928c7dd3392STiger Yang struct super_block *sb = inode->i_sb; 29296f70fa51STao Ma u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster); 29306f70fa51STao Ma struct page *page; 29316f70fa51STao Ma pgoff_t page_index; 29326ea4843fSTao Ma unsigned int from, to, readahead_pages; 29336f70fa51STao Ma loff_t offset, end, map_end; 29343e19a25eSTristan Ye struct address_space *mapping = inode->i_mapping; 29356f70fa51STao Ma 2936198aac28STao Ma trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster, 2937198aac28STao Ma new_cluster, new_len); 29386f70fa51STao Ma 29396ea4843fSTao Ma readahead_pages = 29406ea4843fSTao Ma (ocfs2_cow_contig_clusters(sb) << 29416ea4843fSTao Ma OCFS2_SB(sb)->s_clustersize_bits) >> PAGE_CACHE_SHIFT; 29426f70fa51STao Ma offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits; 29436f70fa51STao Ma end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits); 2944f5e27b6dSTao Ma /* 2945f5e27b6dSTao Ma * We only duplicate pages until we reach the page contains i_size - 1. 2946f5e27b6dSTao Ma * So trim 'end' to i_size. 2947f5e27b6dSTao Ma */ 29483e19a25eSTristan Ye if (end > i_size_read(inode)) 29493e19a25eSTristan Ye end = i_size_read(inode); 29506f70fa51STao Ma 29516f70fa51STao Ma while (offset < end) { 29526f70fa51STao Ma page_index = offset >> PAGE_CACHE_SHIFT; 2953d622b89aSTao Ma map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT; 29546f70fa51STao Ma if (map_end > end) 29556f70fa51STao Ma map_end = end; 29566f70fa51STao Ma 29576f70fa51STao Ma /* from, to is the offset within the page. */ 29586f70fa51STao Ma from = offset & (PAGE_CACHE_SIZE - 1); 29596f70fa51STao Ma to = PAGE_CACHE_SIZE; 29606f70fa51STao Ma if (map_end & (PAGE_CACHE_SIZE - 1)) 29616f70fa51STao Ma to = map_end & (PAGE_CACHE_SIZE - 1); 29626f70fa51STao Ma 29639b4c0ff3SJan Kara page = find_or_create_page(mapping, page_index, GFP_NOFS); 296462c61046SGu Zheng if (!page) { 296562c61046SGu Zheng ret = -ENOMEM; 296662c61046SGu Zheng mlog_errno(ret); 296762c61046SGu Zheng break; 296862c61046SGu Zheng } 29696f70fa51STao Ma 29700a1ea437STao Ma /* 29710a1ea437STao Ma * In case PAGE_CACHE_SIZE <= CLUSTER_SIZE, This page 29720a1ea437STao Ma * can't be dirtied before we CoW it out. 29730a1ea437STao Ma */ 29740a1ea437STao Ma if (PAGE_CACHE_SIZE <= OCFS2_SB(sb)->s_clustersize) 29756f70fa51STao Ma BUG_ON(PageDirty(page)); 29766f70fa51STao Ma 29776f70fa51STao Ma if (!PageUptodate(page)) { 29786f70fa51STao Ma ret = block_read_full_page(page, ocfs2_get_block); 29796f70fa51STao Ma if (ret) { 29806f70fa51STao Ma mlog_errno(ret); 29816f70fa51STao Ma goto unlock; 29826f70fa51STao Ma } 29836f70fa51STao Ma lock_page(page); 29846f70fa51STao Ma } 29856f70fa51STao Ma 29866f70fa51STao Ma if (page_has_buffers(page)) { 29876f70fa51STao Ma ret = walk_page_buffers(handle, page_buffers(page), 29886f70fa51STao Ma from, to, &partial, 29896f70fa51STao Ma ocfs2_clear_cow_buffer); 29906f70fa51STao Ma if (ret) { 29916f70fa51STao Ma mlog_errno(ret); 29926f70fa51STao Ma goto unlock; 29936f70fa51STao Ma } 29946f70fa51STao Ma } 29956f70fa51STao Ma 2996c7dd3392STiger Yang ocfs2_map_and_dirty_page(inode, 2997c7dd3392STiger Yang handle, from, to, 29986f70fa51STao Ma page, 0, &new_block); 29996f70fa51STao Ma mark_page_accessed(page); 30006f70fa51STao Ma unlock: 30016f70fa51STao Ma unlock_page(page); 30026f70fa51STao Ma page_cache_release(page); 30036f70fa51STao Ma page = NULL; 30046f70fa51STao Ma offset = map_end; 30056f70fa51STao Ma if (ret) 30066f70fa51STao Ma break; 30076f70fa51STao Ma } 30086f70fa51STao Ma 30096f70fa51STao Ma return ret; 30106f70fa51STao Ma } 30116f70fa51STao Ma 30123e19a25eSTristan Ye int ocfs2_duplicate_clusters_by_jbd(handle_t *handle, 3013c7dd3392STiger Yang struct inode *inode, 3014492a8a33STao Ma u32 cpos, u32 old_cluster, 3015492a8a33STao Ma u32 new_cluster, u32 new_len) 3016492a8a33STao Ma { 3017492a8a33STao Ma int ret = 0; 30183e19a25eSTristan Ye struct super_block *sb = inode->i_sb; 30193e19a25eSTristan Ye struct ocfs2_caching_info *ci = INODE_CACHE(inode); 3020492a8a33STao Ma int i, blocks = ocfs2_clusters_to_blocks(sb, new_len); 3021492a8a33STao Ma u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster); 3022492a8a33STao Ma u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster); 3023492a8a33STao Ma struct ocfs2_super *osb = OCFS2_SB(sb); 3024492a8a33STao Ma struct buffer_head *old_bh = NULL; 3025492a8a33STao Ma struct buffer_head *new_bh = NULL; 3026492a8a33STao Ma 3027198aac28STao Ma trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster, 3028492a8a33STao Ma new_cluster, new_len); 3029492a8a33STao Ma 3030492a8a33STao Ma for (i = 0; i < blocks; i++, old_block++, new_block++) { 3031492a8a33STao Ma new_bh = sb_getblk(osb->sb, new_block); 3032492a8a33STao Ma if (new_bh == NULL) { 30337391a294SRui Xiang ret = -ENOMEM; 3034492a8a33STao Ma mlog_errno(ret); 3035492a8a33STao Ma break; 3036492a8a33STao Ma } 3037492a8a33STao Ma 3038492a8a33STao Ma ocfs2_set_new_buffer_uptodate(ci, new_bh); 3039492a8a33STao Ma 3040492a8a33STao Ma ret = ocfs2_read_block(ci, old_block, &old_bh, NULL); 3041492a8a33STao Ma if (ret) { 3042492a8a33STao Ma mlog_errno(ret); 3043492a8a33STao Ma break; 3044492a8a33STao Ma } 3045492a8a33STao Ma 3046492a8a33STao Ma ret = ocfs2_journal_access(handle, ci, new_bh, 3047492a8a33STao Ma OCFS2_JOURNAL_ACCESS_CREATE); 3048492a8a33STao Ma if (ret) { 3049492a8a33STao Ma mlog_errno(ret); 3050492a8a33STao Ma break; 3051492a8a33STao Ma } 3052492a8a33STao Ma 3053492a8a33STao Ma memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize); 3054ec20cec7SJoel Becker ocfs2_journal_dirty(handle, new_bh); 3055492a8a33STao Ma 3056492a8a33STao Ma brelse(new_bh); 3057492a8a33STao Ma brelse(old_bh); 3058492a8a33STao Ma new_bh = NULL; 3059492a8a33STao Ma old_bh = NULL; 3060492a8a33STao Ma } 3061492a8a33STao Ma 3062492a8a33STao Ma brelse(new_bh); 3063492a8a33STao Ma brelse(old_bh); 3064492a8a33STao Ma return ret; 3065492a8a33STao Ma } 3066492a8a33STao Ma 30676f70fa51STao Ma static int ocfs2_clear_ext_refcount(handle_t *handle, 30686f70fa51STao Ma struct ocfs2_extent_tree *et, 30696f70fa51STao Ma u32 cpos, u32 p_cluster, u32 len, 30706f70fa51STao Ma unsigned int ext_flags, 30716f70fa51STao Ma struct ocfs2_alloc_context *meta_ac, 30726f70fa51STao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 30736f70fa51STao Ma { 30746f70fa51STao Ma int ret, index; 30756f70fa51STao Ma struct ocfs2_extent_rec replace_rec; 30766f70fa51STao Ma struct ocfs2_path *path = NULL; 30776f70fa51STao Ma struct ocfs2_extent_list *el; 30786f70fa51STao Ma struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); 30796f70fa51STao Ma u64 ino = ocfs2_metadata_cache_owner(et->et_ci); 30806f70fa51STao Ma 3081198aac28STao Ma trace_ocfs2_clear_ext_refcount((unsigned long long)ino, 3082198aac28STao Ma cpos, len, p_cluster, ext_flags); 30836f70fa51STao Ma 30846f70fa51STao Ma memset(&replace_rec, 0, sizeof(replace_rec)); 30856f70fa51STao Ma replace_rec.e_cpos = cpu_to_le32(cpos); 30866f70fa51STao Ma replace_rec.e_leaf_clusters = cpu_to_le16(len); 30876f70fa51STao Ma replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb, 30886f70fa51STao Ma p_cluster)); 30896f70fa51STao Ma replace_rec.e_flags = ext_flags; 30906f70fa51STao Ma replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED; 30916f70fa51STao Ma 30926f70fa51STao Ma path = ocfs2_new_path_from_et(et); 30936f70fa51STao Ma if (!path) { 30946f70fa51STao Ma ret = -ENOMEM; 30956f70fa51STao Ma mlog_errno(ret); 30966f70fa51STao Ma goto out; 30976f70fa51STao Ma } 30986f70fa51STao Ma 30996f70fa51STao Ma ret = ocfs2_find_path(et->et_ci, path, cpos); 31006f70fa51STao Ma if (ret) { 31016f70fa51STao Ma mlog_errno(ret); 31026f70fa51STao Ma goto out; 31036f70fa51STao Ma } 31046f70fa51STao Ma 31056f70fa51STao Ma el = path_leaf_el(path); 31066f70fa51STao Ma 31076f70fa51STao Ma index = ocfs2_search_extent_list(el, cpos); 3108981035b4SYingtai Xie if (index == -1) { 31096f70fa51STao Ma ocfs2_error(sb, 31106f70fa51STao Ma "Inode %llu has an extent at cpos %u which can no " 31116f70fa51STao Ma "longer be found.\n", 31126f70fa51STao Ma (unsigned long long)ino, cpos); 31136f70fa51STao Ma ret = -EROFS; 31146f70fa51STao Ma goto out; 31156f70fa51STao Ma } 31166f70fa51STao Ma 31176f70fa51STao Ma ret = ocfs2_split_extent(handle, et, path, index, 31186f70fa51STao Ma &replace_rec, meta_ac, dealloc); 31196f70fa51STao Ma if (ret) 31206f70fa51STao Ma mlog_errno(ret); 31216f70fa51STao Ma 31226f70fa51STao Ma out: 31236f70fa51STao Ma ocfs2_free_path(path); 31246f70fa51STao Ma return ret; 31256f70fa51STao Ma } 31266f70fa51STao Ma 31276f70fa51STao Ma static int ocfs2_replace_clusters(handle_t *handle, 31286f70fa51STao Ma struct ocfs2_cow_context *context, 31296f70fa51STao Ma u32 cpos, u32 old, 31306f70fa51STao Ma u32 new, u32 len, 31316f70fa51STao Ma unsigned int ext_flags) 31326f70fa51STao Ma { 31336f70fa51STao Ma int ret; 3134913580b4STao Ma struct ocfs2_caching_info *ci = context->data_et.et_ci; 31356f70fa51STao Ma u64 ino = ocfs2_metadata_cache_owner(ci); 31366f70fa51STao Ma 3137198aac28STao Ma trace_ocfs2_replace_clusters((unsigned long long)ino, 3138198aac28STao Ma cpos, old, new, len, ext_flags); 31396f70fa51STao Ma 31406f70fa51STao Ma /*If the old clusters is unwritten, no need to duplicate. */ 31416f70fa51STao Ma if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) { 3142c7dd3392STiger Yang ret = context->cow_duplicate_clusters(handle, context->inode, 31433e19a25eSTristan Ye cpos, old, new, len); 31446f70fa51STao Ma if (ret) { 31456f70fa51STao Ma mlog_errno(ret); 31466f70fa51STao Ma goto out; 31476f70fa51STao Ma } 31486f70fa51STao Ma } 31496f70fa51STao Ma 3150913580b4STao Ma ret = ocfs2_clear_ext_refcount(handle, &context->data_et, 31516f70fa51STao Ma cpos, new, len, ext_flags, 31526f70fa51STao Ma context->meta_ac, &context->dealloc); 31536f70fa51STao Ma if (ret) 31546f70fa51STao Ma mlog_errno(ret); 31556f70fa51STao Ma out: 31566f70fa51STao Ma return ret; 31576f70fa51STao Ma } 31586f70fa51STao Ma 31593e19a25eSTristan Ye int ocfs2_cow_sync_writeback(struct super_block *sb, 31603e19a25eSTristan Ye struct inode *inode, 31616f70fa51STao Ma u32 cpos, u32 num_clusters) 31626f70fa51STao Ma { 31636f70fa51STao Ma int ret = 0; 31646f70fa51STao Ma loff_t offset, end, map_end; 31656f70fa51STao Ma pgoff_t page_index; 31666f70fa51STao Ma struct page *page; 31676f70fa51STao Ma 31683e19a25eSTristan Ye if (ocfs2_should_order_data(inode)) 31696f70fa51STao Ma return 0; 31706f70fa51STao Ma 31716f70fa51STao Ma offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits; 31726f70fa51STao Ma end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits); 31736f70fa51STao Ma 31743e19a25eSTristan Ye ret = filemap_fdatawrite_range(inode->i_mapping, 31756f70fa51STao Ma offset, end - 1); 31766f70fa51STao Ma if (ret < 0) { 31776f70fa51STao Ma mlog_errno(ret); 31786f70fa51STao Ma return ret; 31796f70fa51STao Ma } 31806f70fa51STao Ma 31816f70fa51STao Ma while (offset < end) { 31826f70fa51STao Ma page_index = offset >> PAGE_CACHE_SHIFT; 3183d622b89aSTao Ma map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT; 31846f70fa51STao Ma if (map_end > end) 31856f70fa51STao Ma map_end = end; 31866f70fa51STao Ma 31873e19a25eSTristan Ye page = find_or_create_page(inode->i_mapping, 31889b4c0ff3SJan Kara page_index, GFP_NOFS); 31896f70fa51STao Ma BUG_ON(!page); 31906f70fa51STao Ma 31916f70fa51STao Ma wait_on_page_writeback(page); 31926f70fa51STao Ma if (PageError(page)) { 31936f70fa51STao Ma ret = -EIO; 31946f70fa51STao Ma mlog_errno(ret); 31956f70fa51STao Ma } else 31966f70fa51STao Ma mark_page_accessed(page); 31976f70fa51STao Ma 31986f70fa51STao Ma unlock_page(page); 31996f70fa51STao Ma page_cache_release(page); 32006f70fa51STao Ma page = NULL; 32016f70fa51STao Ma offset = map_end; 32026f70fa51STao Ma if (ret) 32036f70fa51STao Ma break; 32046f70fa51STao Ma } 32056f70fa51STao Ma 32066f70fa51STao Ma return ret; 32076f70fa51STao Ma } 32086f70fa51STao Ma 3209913580b4STao Ma static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context, 3210913580b4STao Ma u32 v_cluster, u32 *p_cluster, 3211913580b4STao Ma u32 *num_clusters, 3212913580b4STao Ma unsigned int *extent_flags) 3213913580b4STao Ma { 3214913580b4STao Ma return ocfs2_get_clusters(context->inode, v_cluster, p_cluster, 3215913580b4STao Ma num_clusters, extent_flags); 3216913580b4STao Ma } 3217913580b4STao Ma 32186f70fa51STao Ma static int ocfs2_make_clusters_writable(struct super_block *sb, 32196f70fa51STao Ma struct ocfs2_cow_context *context, 32206f70fa51STao Ma u32 cpos, u32 p_cluster, 32216f70fa51STao Ma u32 num_clusters, unsigned int e_flags) 32226f70fa51STao Ma { 32236ae23c55STao Ma int ret, delete, index, credits = 0; 3224acf3bb00STristan Ye u32 new_bit, new_len, orig_num_clusters; 32256ae23c55STao Ma unsigned int set_len; 32266f70fa51STao Ma struct ocfs2_super *osb = OCFS2_SB(sb); 32276f70fa51STao Ma handle_t *handle; 32286ae23c55STao Ma struct buffer_head *ref_leaf_bh = NULL; 3229913580b4STao Ma struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci; 32306ae23c55STao Ma struct ocfs2_refcount_rec rec; 32316ae23c55STao Ma 3232198aac28STao Ma trace_ocfs2_make_clusters_writable(cpos, p_cluster, 3233198aac28STao Ma num_clusters, e_flags); 32346f70fa51STao Ma 32356f70fa51STao Ma ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters, 3236913580b4STao Ma &context->data_et, 3237913580b4STao Ma ref_ci, 32386f70fa51STao Ma context->ref_root_bh, 32396f70fa51STao Ma &context->meta_ac, 32406f70fa51STao Ma &context->data_ac, &credits); 32416f70fa51STao Ma if (ret) { 32426f70fa51STao Ma mlog_errno(ret); 32436f70fa51STao Ma return ret; 32446f70fa51STao Ma } 32456f70fa51STao Ma 3246492a8a33STao Ma if (context->post_refcount) 3247492a8a33STao Ma credits += context->post_refcount->credits; 3248492a8a33STao Ma 3249492a8a33STao Ma credits += context->extra_credits; 32506f70fa51STao Ma handle = ocfs2_start_trans(osb, credits); 32516f70fa51STao Ma if (IS_ERR(handle)) { 32526f70fa51STao Ma ret = PTR_ERR(handle); 32536f70fa51STao Ma mlog_errno(ret); 32546f70fa51STao Ma goto out; 32556f70fa51STao Ma } 32566f70fa51STao Ma 3257acf3bb00STristan Ye orig_num_clusters = num_clusters; 3258acf3bb00STristan Ye 32596f70fa51STao Ma while (num_clusters) { 3260913580b4STao Ma ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh, 32616ae23c55STao Ma p_cluster, num_clusters, 32626ae23c55STao Ma &rec, &index, &ref_leaf_bh); 32636ae23c55STao Ma if (ret) { 32646ae23c55STao Ma mlog_errno(ret); 32656ae23c55STao Ma goto out_commit; 32666ae23c55STao Ma } 32676ae23c55STao Ma 32686ae23c55STao Ma BUG_ON(!rec.r_refcount); 32696ae23c55STao Ma set_len = min((u64)p_cluster + num_clusters, 32706ae23c55STao Ma le64_to_cpu(rec.r_cpos) + 32716ae23c55STao Ma le32_to_cpu(rec.r_clusters)) - p_cluster; 32726ae23c55STao Ma 32736ae23c55STao Ma /* 32746ae23c55STao Ma * There are many different situation here. 32756ae23c55STao Ma * 1. If refcount == 1, remove the flag and don't COW. 32766ae23c55STao Ma * 2. If refcount > 1, allocate clusters. 32776ae23c55STao Ma * Here we may not allocate r_len once at a time, so continue 32786ae23c55STao Ma * until we reach num_clusters. 32796ae23c55STao Ma */ 32806ae23c55STao Ma if (le32_to_cpu(rec.r_refcount) == 1) { 32816ae23c55STao Ma delete = 0; 3282913580b4STao Ma ret = ocfs2_clear_ext_refcount(handle, 3283913580b4STao Ma &context->data_et, 32846ae23c55STao Ma cpos, p_cluster, 32856ae23c55STao Ma set_len, e_flags, 32866ae23c55STao Ma context->meta_ac, 32876ae23c55STao Ma &context->dealloc); 32886ae23c55STao Ma if (ret) { 32896ae23c55STao Ma mlog_errno(ret); 32906ae23c55STao Ma goto out_commit; 32916ae23c55STao Ma } 32926ae23c55STao Ma } else { 32936ae23c55STao Ma delete = 1; 32946ae23c55STao Ma 32951ed9b777SJoel Becker ret = __ocfs2_claim_clusters(handle, 32966ae23c55STao Ma context->data_ac, 32976ae23c55STao Ma 1, set_len, 32986f70fa51STao Ma &new_bit, &new_len); 32996f70fa51STao Ma if (ret) { 33006f70fa51STao Ma mlog_errno(ret); 33016f70fa51STao Ma goto out_commit; 33026f70fa51STao Ma } 33036f70fa51STao Ma 33046f70fa51STao Ma ret = ocfs2_replace_clusters(handle, context, 33056f70fa51STao Ma cpos, p_cluster, new_bit, 33066f70fa51STao Ma new_len, e_flags); 33076f70fa51STao Ma if (ret) { 33086f70fa51STao Ma mlog_errno(ret); 33096f70fa51STao Ma goto out_commit; 33106f70fa51STao Ma } 33116ae23c55STao Ma set_len = new_len; 33126f70fa51STao Ma } 33136f70fa51STao Ma 3314913580b4STao Ma ret = __ocfs2_decrease_refcount(handle, ref_ci, 33156f70fa51STao Ma context->ref_root_bh, 33166ae23c55STao Ma p_cluster, set_len, 33176f70fa51STao Ma context->meta_ac, 33186ae23c55STao Ma &context->dealloc, delete); 33196f70fa51STao Ma if (ret) { 33206f70fa51STao Ma mlog_errno(ret); 33216f70fa51STao Ma goto out_commit; 33226f70fa51STao Ma } 33236f70fa51STao Ma 33246ae23c55STao Ma cpos += set_len; 33256ae23c55STao Ma p_cluster += set_len; 33266ae23c55STao Ma num_clusters -= set_len; 33276ae23c55STao Ma brelse(ref_leaf_bh); 33286ae23c55STao Ma ref_leaf_bh = NULL; 33296ae23c55STao Ma } 33306ae23c55STao Ma 3331492a8a33STao Ma /* handle any post_cow action. */ 3332492a8a33STao Ma if (context->post_refcount && context->post_refcount->func) { 3333492a8a33STao Ma ret = context->post_refcount->func(context->inode, handle, 3334492a8a33STao Ma context->post_refcount->para); 3335492a8a33STao Ma if (ret) { 3336492a8a33STao Ma mlog_errno(ret); 3337492a8a33STao Ma goto out_commit; 3338492a8a33STao Ma } 3339492a8a33STao Ma } 3340492a8a33STao Ma 33416f70fa51STao Ma /* 33426f70fa51STao Ma * Here we should write the new page out first if we are 33436f70fa51STao Ma * in write-back mode. 33446f70fa51STao Ma */ 3345492a8a33STao Ma if (context->get_clusters == ocfs2_di_get_clusters) { 33463e19a25eSTristan Ye ret = ocfs2_cow_sync_writeback(sb, context->inode, cpos, 3347acf3bb00STristan Ye orig_num_clusters); 33486f70fa51STao Ma if (ret) 33496f70fa51STao Ma mlog_errno(ret); 3350492a8a33STao Ma } 33516f70fa51STao Ma 33526f70fa51STao Ma out_commit: 33536f70fa51STao Ma ocfs2_commit_trans(osb, handle); 33546f70fa51STao Ma 33556f70fa51STao Ma out: 33566f70fa51STao Ma if (context->data_ac) { 33576f70fa51STao Ma ocfs2_free_alloc_context(context->data_ac); 33586f70fa51STao Ma context->data_ac = NULL; 33596f70fa51STao Ma } 33606f70fa51STao Ma if (context->meta_ac) { 33616f70fa51STao Ma ocfs2_free_alloc_context(context->meta_ac); 33626f70fa51STao Ma context->meta_ac = NULL; 33636f70fa51STao Ma } 33646ae23c55STao Ma brelse(ref_leaf_bh); 33656f70fa51STao Ma 33666f70fa51STao Ma return ret; 33676f70fa51STao Ma } 33686f70fa51STao Ma 3369913580b4STao Ma static int ocfs2_replace_cow(struct ocfs2_cow_context *context) 33706f70fa51STao Ma { 33716f70fa51STao Ma int ret = 0; 3372913580b4STao Ma struct inode *inode = context->inode; 3373913580b4STao Ma u32 cow_start = context->cow_start, cow_len = context->cow_len; 3374913580b4STao Ma u32 p_cluster, num_clusters; 33756f70fa51STao Ma unsigned int ext_flags; 33766f70fa51STao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 33776f70fa51STao Ma 33786f70fa51STao Ma if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) { 33796f70fa51STao Ma ocfs2_error(inode->i_sb, "Inode %lu want to use refcount " 33806f70fa51STao Ma "tree, but the feature bit is not set in the " 33816f70fa51STao Ma "super block.", inode->i_ino); 33826f70fa51STao Ma return -EROFS; 33836f70fa51STao Ma } 33846f70fa51STao Ma 33856f70fa51STao Ma ocfs2_init_dealloc_ctxt(&context->dealloc); 33866f70fa51STao Ma 33876f70fa51STao Ma while (cow_len) { 3388913580b4STao Ma ret = context->get_clusters(context, cow_start, &p_cluster, 33896f70fa51STao Ma &num_clusters, &ext_flags); 33906f70fa51STao Ma if (ret) { 33916f70fa51STao Ma mlog_errno(ret); 33926f70fa51STao Ma break; 33936f70fa51STao Ma } 33946f70fa51STao Ma 33956f70fa51STao Ma BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED)); 33966f70fa51STao Ma 33976f70fa51STao Ma if (cow_len < num_clusters) 33986f70fa51STao Ma num_clusters = cow_len; 33996f70fa51STao Ma 34006f70fa51STao Ma ret = ocfs2_make_clusters_writable(inode->i_sb, context, 34016f70fa51STao Ma cow_start, p_cluster, 34026f70fa51STao Ma num_clusters, ext_flags); 34036f70fa51STao Ma if (ret) { 34046f70fa51STao Ma mlog_errno(ret); 34056f70fa51STao Ma break; 34066f70fa51STao Ma } 34076f70fa51STao Ma 34086f70fa51STao Ma cow_len -= num_clusters; 34096f70fa51STao Ma cow_start += num_clusters; 34106f70fa51STao Ma } 34116f70fa51STao Ma 34126f70fa51STao Ma if (ocfs2_dealloc_has_cluster(&context->dealloc)) { 34136f70fa51STao Ma ocfs2_schedule_truncate_log_flush(osb, 1); 34146f70fa51STao Ma ocfs2_run_deallocs(osb, &context->dealloc); 34156f70fa51STao Ma } 34166f70fa51STao Ma 34176f70fa51STao Ma return ret; 34186f70fa51STao Ma } 34196f70fa51STao Ma 34206f70fa51STao Ma /* 342137f8a2bfSTao Ma * Starting at cpos, try to CoW write_len clusters. Don't CoW 342237f8a2bfSTao Ma * past max_cpos. This will stop when it runs into a hole or an 342337f8a2bfSTao Ma * unrefcounted extent. 34246f70fa51STao Ma */ 34256f70fa51STao Ma static int ocfs2_refcount_cow_hunk(struct inode *inode, 34266f70fa51STao Ma struct buffer_head *di_bh, 342737f8a2bfSTao Ma u32 cpos, u32 write_len, u32 max_cpos) 34286f70fa51STao Ma { 34296f70fa51STao Ma int ret; 34306f70fa51STao Ma u32 cow_start = 0, cow_len = 0; 34316f70fa51STao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 34326f70fa51STao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 34336f70fa51STao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 34346f70fa51STao Ma struct buffer_head *ref_root_bh = NULL; 34356f70fa51STao Ma struct ocfs2_refcount_tree *ref_tree; 3436913580b4STao Ma struct ocfs2_cow_context *context = NULL; 34376f70fa51STao Ma 34386f70fa51STao Ma BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)); 34396f70fa51STao Ma 3440913580b4STao Ma ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list, 344137f8a2bfSTao Ma cpos, write_len, max_cpos, 34426f70fa51STao Ma &cow_start, &cow_len); 34436f70fa51STao Ma if (ret) { 34446f70fa51STao Ma mlog_errno(ret); 34456f70fa51STao Ma goto out; 34466f70fa51STao Ma } 344737f8a2bfSTao Ma 3448198aac28STao Ma trace_ocfs2_refcount_cow_hunk(OCFS2_I(inode)->ip_blkno, 3449198aac28STao Ma cpos, write_len, max_cpos, 3450198aac28STao Ma cow_start, cow_len); 34516f70fa51STao Ma 34526f70fa51STao Ma BUG_ON(cow_len == 0); 34536f70fa51STao Ma 3454913580b4STao Ma context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS); 3455913580b4STao Ma if (!context) { 3456913580b4STao Ma ret = -ENOMEM; 3457913580b4STao Ma mlog_errno(ret); 3458913580b4STao Ma goto out; 3459913580b4STao Ma } 3460913580b4STao Ma 34616f70fa51STao Ma ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), 34626f70fa51STao Ma 1, &ref_tree, &ref_root_bh); 34636f70fa51STao Ma if (ret) { 34646f70fa51STao Ma mlog_errno(ret); 34656f70fa51STao Ma goto out; 34666f70fa51STao Ma } 34676f70fa51STao Ma 3468913580b4STao Ma context->inode = inode; 3469913580b4STao Ma context->cow_start = cow_start; 3470913580b4STao Ma context->cow_len = cow_len; 3471913580b4STao Ma context->ref_tree = ref_tree; 3472913580b4STao Ma context->ref_root_bh = ref_root_bh; 3473913580b4STao Ma context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page; 3474913580b4STao Ma context->get_clusters = ocfs2_di_get_clusters; 3475913580b4STao Ma 3476913580b4STao Ma ocfs2_init_dinode_extent_tree(&context->data_et, 3477913580b4STao Ma INODE_CACHE(inode), di_bh); 3478913580b4STao Ma 3479913580b4STao Ma ret = ocfs2_replace_cow(context); 34806f70fa51STao Ma if (ret) 34816f70fa51STao Ma mlog_errno(ret); 34826f70fa51STao Ma 3483913580b4STao Ma /* 3484913580b4STao Ma * truncate the extent map here since no matter whether we meet with 3485913580b4STao Ma * any error during the action, we shouldn't trust cached extent map 3486913580b4STao Ma * any more. 3487913580b4STao Ma */ 3488913580b4STao Ma ocfs2_extent_map_trunc(inode, cow_start); 3489913580b4STao Ma 34906f70fa51STao Ma ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 34916f70fa51STao Ma brelse(ref_root_bh); 34926f70fa51STao Ma out: 3493913580b4STao Ma kfree(context); 34946f70fa51STao Ma return ret; 34956f70fa51STao Ma } 34966f70fa51STao Ma 34976f70fa51STao Ma /* 34986f70fa51STao Ma * CoW any and all clusters between cpos and cpos+write_len. 349937f8a2bfSTao Ma * Don't CoW past max_cpos. If this returns successfully, all 350037f8a2bfSTao Ma * clusters between cpos and cpos+write_len are safe to modify. 35016f70fa51STao Ma */ 35026f70fa51STao Ma int ocfs2_refcount_cow(struct inode *inode, 35036f70fa51STao Ma struct buffer_head *di_bh, 350437f8a2bfSTao Ma u32 cpos, u32 write_len, u32 max_cpos) 35056f70fa51STao Ma { 35066f70fa51STao Ma int ret = 0; 35076f70fa51STao Ma u32 p_cluster, num_clusters; 35086f70fa51STao Ma unsigned int ext_flags; 35096f70fa51STao Ma 35106f70fa51STao Ma while (write_len) { 35116f70fa51STao Ma ret = ocfs2_get_clusters(inode, cpos, &p_cluster, 35126f70fa51STao Ma &num_clusters, &ext_flags); 35136f70fa51STao Ma if (ret) { 35146f70fa51STao Ma mlog_errno(ret); 35156f70fa51STao Ma break; 35166f70fa51STao Ma } 35176f70fa51STao Ma 35186f70fa51STao Ma if (write_len < num_clusters) 35196f70fa51STao Ma num_clusters = write_len; 35206f70fa51STao Ma 35216f70fa51STao Ma if (ext_flags & OCFS2_EXT_REFCOUNTED) { 3522c7dd3392STiger Yang ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos, 352337f8a2bfSTao Ma num_clusters, max_cpos); 35246f70fa51STao Ma if (ret) { 35256f70fa51STao Ma mlog_errno(ret); 35266f70fa51STao Ma break; 35276f70fa51STao Ma } 35286f70fa51STao Ma } 35296f70fa51STao Ma 35306f70fa51STao Ma write_len -= num_clusters; 35316f70fa51STao Ma cpos += num_clusters; 35326f70fa51STao Ma } 35336f70fa51STao Ma 35346f70fa51STao Ma return ret; 35356f70fa51STao Ma } 3536110a045aSTao Ma 3537492a8a33STao Ma static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context, 3538492a8a33STao Ma u32 v_cluster, u32 *p_cluster, 3539492a8a33STao Ma u32 *num_clusters, 3540492a8a33STao Ma unsigned int *extent_flags) 3541492a8a33STao Ma { 3542492a8a33STao Ma struct inode *inode = context->inode; 3543492a8a33STao Ma struct ocfs2_xattr_value_root *xv = context->cow_object; 3544492a8a33STao Ma 3545492a8a33STao Ma return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster, 3546492a8a33STao Ma num_clusters, &xv->xr_list, 3547492a8a33STao Ma extent_flags); 3548492a8a33STao Ma } 3549492a8a33STao Ma 3550492a8a33STao Ma /* 3551492a8a33STao Ma * Given a xattr value root, calculate the most meta/credits we need for 3552492a8a33STao Ma * refcount tree change if we truncate it to 0. 3553492a8a33STao Ma */ 3554492a8a33STao Ma int ocfs2_refcounted_xattr_delete_need(struct inode *inode, 3555492a8a33STao Ma struct ocfs2_caching_info *ref_ci, 3556492a8a33STao Ma struct buffer_head *ref_root_bh, 3557492a8a33STao Ma struct ocfs2_xattr_value_root *xv, 3558492a8a33STao Ma int *meta_add, int *credits) 3559492a8a33STao Ma { 3560492a8a33STao Ma int ret = 0, index, ref_blocks = 0; 3561492a8a33STao Ma u32 p_cluster, num_clusters; 3562492a8a33STao Ma u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters); 3563492a8a33STao Ma struct ocfs2_refcount_block *rb; 3564492a8a33STao Ma struct ocfs2_refcount_rec rec; 3565492a8a33STao Ma struct buffer_head *ref_leaf_bh = NULL; 3566492a8a33STao Ma 3567492a8a33STao Ma while (cpos < clusters) { 3568492a8a33STao Ma ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, 3569492a8a33STao Ma &num_clusters, &xv->xr_list, 3570492a8a33STao Ma NULL); 3571492a8a33STao Ma if (ret) { 3572492a8a33STao Ma mlog_errno(ret); 3573492a8a33STao Ma goto out; 3574492a8a33STao Ma } 3575492a8a33STao Ma 3576492a8a33STao Ma cpos += num_clusters; 3577492a8a33STao Ma 3578492a8a33STao Ma while (num_clusters) { 3579492a8a33STao Ma ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh, 3580492a8a33STao Ma p_cluster, num_clusters, 3581492a8a33STao Ma &rec, &index, 3582492a8a33STao Ma &ref_leaf_bh); 3583492a8a33STao Ma if (ret) { 3584492a8a33STao Ma mlog_errno(ret); 3585492a8a33STao Ma goto out; 3586492a8a33STao Ma } 3587492a8a33STao Ma 3588492a8a33STao Ma BUG_ON(!rec.r_refcount); 3589492a8a33STao Ma 3590492a8a33STao Ma rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data; 3591492a8a33STao Ma 3592492a8a33STao Ma /* 3593492a8a33STao Ma * We really don't know whether the other clusters is in 3594492a8a33STao Ma * this refcount block or not, so just take the worst 3595492a8a33STao Ma * case that all the clusters are in this block and each 3596492a8a33STao Ma * one will split a refcount rec, so totally we need 3597492a8a33STao Ma * clusters * 2 new refcount rec. 3598492a8a33STao Ma */ 3599e1bf4cc6SAl Viro if (le16_to_cpu(rb->rf_records.rl_used) + clusters * 2 > 3600492a8a33STao Ma le16_to_cpu(rb->rf_records.rl_count)) 3601492a8a33STao Ma ref_blocks++; 3602492a8a33STao Ma 3603492a8a33STao Ma *credits += 1; 3604492a8a33STao Ma brelse(ref_leaf_bh); 3605492a8a33STao Ma ref_leaf_bh = NULL; 3606492a8a33STao Ma 3607492a8a33STao Ma if (num_clusters <= le32_to_cpu(rec.r_clusters)) 3608492a8a33STao Ma break; 3609492a8a33STao Ma else 3610492a8a33STao Ma num_clusters -= le32_to_cpu(rec.r_clusters); 3611492a8a33STao Ma p_cluster += num_clusters; 3612492a8a33STao Ma } 3613492a8a33STao Ma } 3614492a8a33STao Ma 3615492a8a33STao Ma *meta_add += ref_blocks; 3616492a8a33STao Ma if (!ref_blocks) 3617492a8a33STao Ma goto out; 3618492a8a33STao Ma 3619492a8a33STao Ma rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 3620492a8a33STao Ma if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) 3621492a8a33STao Ma *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; 3622492a8a33STao Ma else { 3623492a8a33STao Ma struct ocfs2_extent_tree et; 3624492a8a33STao Ma 3625492a8a33STao Ma ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh); 3626492a8a33STao Ma *credits += ocfs2_calc_extend_credits(inode->i_sb, 362706f9da6eSGoldwyn Rodrigues et.et_root_el); 3628492a8a33STao Ma } 3629492a8a33STao Ma 3630492a8a33STao Ma out: 3631492a8a33STao Ma brelse(ref_leaf_bh); 3632492a8a33STao Ma return ret; 3633492a8a33STao Ma } 3634492a8a33STao Ma 3635492a8a33STao Ma /* 3636492a8a33STao Ma * Do CoW for xattr. 3637492a8a33STao Ma */ 3638492a8a33STao Ma int ocfs2_refcount_cow_xattr(struct inode *inode, 3639492a8a33STao Ma struct ocfs2_dinode *di, 3640492a8a33STao Ma struct ocfs2_xattr_value_buf *vb, 3641492a8a33STao Ma struct ocfs2_refcount_tree *ref_tree, 3642492a8a33STao Ma struct buffer_head *ref_root_bh, 3643492a8a33STao Ma u32 cpos, u32 write_len, 3644492a8a33STao Ma struct ocfs2_post_refcount *post) 3645492a8a33STao Ma { 3646492a8a33STao Ma int ret; 3647492a8a33STao Ma struct ocfs2_xattr_value_root *xv = vb->vb_xv; 3648492a8a33STao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 3649492a8a33STao Ma struct ocfs2_cow_context *context = NULL; 3650492a8a33STao Ma u32 cow_start, cow_len; 3651492a8a33STao Ma 3652492a8a33STao Ma BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)); 3653492a8a33STao Ma 3654492a8a33STao Ma ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list, 3655492a8a33STao Ma cpos, write_len, UINT_MAX, 3656492a8a33STao Ma &cow_start, &cow_len); 3657492a8a33STao Ma if (ret) { 3658492a8a33STao Ma mlog_errno(ret); 3659492a8a33STao Ma goto out; 3660492a8a33STao Ma } 3661492a8a33STao Ma 3662492a8a33STao Ma BUG_ON(cow_len == 0); 3663492a8a33STao Ma 3664492a8a33STao Ma context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS); 3665492a8a33STao Ma if (!context) { 3666492a8a33STao Ma ret = -ENOMEM; 3667492a8a33STao Ma mlog_errno(ret); 3668492a8a33STao Ma goto out; 3669492a8a33STao Ma } 3670492a8a33STao Ma 3671492a8a33STao Ma context->inode = inode; 3672492a8a33STao Ma context->cow_start = cow_start; 3673492a8a33STao Ma context->cow_len = cow_len; 3674492a8a33STao Ma context->ref_tree = ref_tree; 36756eab04a8SJustin P. Mattock context->ref_root_bh = ref_root_bh; 3676492a8a33STao Ma context->cow_object = xv; 3677492a8a33STao Ma 3678492a8a33STao Ma context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd; 3679492a8a33STao Ma /* We need the extra credits for duplicate_clusters by jbd. */ 3680492a8a33STao Ma context->extra_credits = 3681492a8a33STao Ma ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len; 3682492a8a33STao Ma context->get_clusters = ocfs2_xattr_value_get_clusters; 3683492a8a33STao Ma context->post_refcount = post; 3684492a8a33STao Ma 3685492a8a33STao Ma ocfs2_init_xattr_value_extent_tree(&context->data_et, 3686492a8a33STao Ma INODE_CACHE(inode), vb); 3687492a8a33STao Ma 3688492a8a33STao Ma ret = ocfs2_replace_cow(context); 3689492a8a33STao Ma if (ret) 3690492a8a33STao Ma mlog_errno(ret); 3691492a8a33STao Ma 3692492a8a33STao Ma out: 3693492a8a33STao Ma kfree(context); 3694492a8a33STao Ma return ret; 3695492a8a33STao Ma } 3696492a8a33STao Ma 3697110a045aSTao Ma /* 3698110a045aSTao Ma * Insert a new extent into refcount tree and mark a extent rec 3699110a045aSTao Ma * as refcounted in the dinode tree. 3700110a045aSTao Ma */ 3701110a045aSTao Ma int ocfs2_add_refcount_flag(struct inode *inode, 3702110a045aSTao Ma struct ocfs2_extent_tree *data_et, 3703110a045aSTao Ma struct ocfs2_caching_info *ref_ci, 3704110a045aSTao Ma struct buffer_head *ref_root_bh, 3705110a045aSTao Ma u32 cpos, u32 p_cluster, u32 num_clusters, 37060129241eSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc, 37070129241eSTao Ma struct ocfs2_post_refcount *post) 3708110a045aSTao Ma { 3709110a045aSTao Ma int ret; 3710110a045aSTao Ma handle_t *handle; 3711110a045aSTao Ma int credits = 1, ref_blocks = 0; 3712110a045aSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3713110a045aSTao Ma struct ocfs2_alloc_context *meta_ac = NULL; 3714110a045aSTao Ma 3715110a045aSTao Ma ret = ocfs2_calc_refcount_meta_credits(inode->i_sb, 3716110a045aSTao Ma ref_ci, ref_root_bh, 3717110a045aSTao Ma p_cluster, num_clusters, 3718110a045aSTao Ma &ref_blocks, &credits); 3719110a045aSTao Ma if (ret) { 3720110a045aSTao Ma mlog_errno(ret); 3721110a045aSTao Ma goto out; 3722110a045aSTao Ma } 3723110a045aSTao Ma 3724198aac28STao Ma trace_ocfs2_add_refcount_flag(ref_blocks, credits); 3725110a045aSTao Ma 3726110a045aSTao Ma if (ref_blocks) { 3727110a045aSTao Ma ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb), 3728110a045aSTao Ma ref_blocks, &meta_ac); 3729110a045aSTao Ma if (ret) { 3730110a045aSTao Ma mlog_errno(ret); 3731110a045aSTao Ma goto out; 3732110a045aSTao Ma } 3733110a045aSTao Ma } 3734110a045aSTao Ma 37350129241eSTao Ma if (post) 37360129241eSTao Ma credits += post->credits; 37370129241eSTao Ma 3738110a045aSTao Ma handle = ocfs2_start_trans(osb, credits); 3739110a045aSTao Ma if (IS_ERR(handle)) { 3740110a045aSTao Ma ret = PTR_ERR(handle); 3741110a045aSTao Ma mlog_errno(ret); 3742110a045aSTao Ma goto out; 3743110a045aSTao Ma } 3744110a045aSTao Ma 3745110a045aSTao Ma ret = ocfs2_mark_extent_refcounted(inode, data_et, handle, 3746110a045aSTao Ma cpos, num_clusters, p_cluster, 3747110a045aSTao Ma meta_ac, dealloc); 3748110a045aSTao Ma if (ret) { 3749110a045aSTao Ma mlog_errno(ret); 3750110a045aSTao Ma goto out_commit; 3751110a045aSTao Ma } 3752110a045aSTao Ma 37537540c1a7STao Ma ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh, 37547540c1a7STao Ma p_cluster, num_clusters, 0, 3755110a045aSTao Ma meta_ac, dealloc); 37560129241eSTao Ma if (ret) { 37570129241eSTao Ma mlog_errno(ret); 37580129241eSTao Ma goto out_commit; 37590129241eSTao Ma } 37600129241eSTao Ma 37610129241eSTao Ma if (post && post->func) { 37620129241eSTao Ma ret = post->func(inode, handle, post->para); 3763110a045aSTao Ma if (ret) 3764110a045aSTao Ma mlog_errno(ret); 37650129241eSTao Ma } 3766110a045aSTao Ma 3767110a045aSTao Ma out_commit: 3768110a045aSTao Ma ocfs2_commit_trans(osb, handle); 3769110a045aSTao Ma out: 3770110a045aSTao Ma if (meta_ac) 3771110a045aSTao Ma ocfs2_free_alloc_context(meta_ac); 3772110a045aSTao Ma return ret; 3773110a045aSTao Ma } 3774110a045aSTao Ma 3775a9063ab9STao Ma static int ocfs2_change_ctime(struct inode *inode, 3776110a045aSTao Ma struct buffer_head *di_bh) 3777110a045aSTao Ma { 3778110a045aSTao Ma int ret; 3779a9063ab9STao Ma handle_t *handle; 3780a9063ab9STao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3781a9063ab9STao Ma 3782a9063ab9STao Ma handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), 3783a9063ab9STao Ma OCFS2_INODE_UPDATE_CREDITS); 3784a9063ab9STao Ma if (IS_ERR(handle)) { 3785a9063ab9STao Ma ret = PTR_ERR(handle); 3786a9063ab9STao Ma mlog_errno(ret); 3787a9063ab9STao Ma goto out; 3788a9063ab9STao Ma } 3789a9063ab9STao Ma 3790a9063ab9STao Ma ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 3791a9063ab9STao Ma OCFS2_JOURNAL_ACCESS_WRITE); 3792a9063ab9STao Ma if (ret) { 3793a9063ab9STao Ma mlog_errno(ret); 3794a9063ab9STao Ma goto out_commit; 3795a9063ab9STao Ma } 3796a9063ab9STao Ma 3797a9063ab9STao Ma inode->i_ctime = CURRENT_TIME; 3798a9063ab9STao Ma di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 3799a9063ab9STao Ma di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 3800a9063ab9STao Ma 3801a9063ab9STao Ma ocfs2_journal_dirty(handle, di_bh); 3802a9063ab9STao Ma 3803a9063ab9STao Ma out_commit: 3804a9063ab9STao Ma ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); 3805a9063ab9STao Ma out: 3806a9063ab9STao Ma return ret; 3807a9063ab9STao Ma } 3808a9063ab9STao Ma 3809a9063ab9STao Ma static int ocfs2_attach_refcount_tree(struct inode *inode, 3810a9063ab9STao Ma struct buffer_head *di_bh) 3811a9063ab9STao Ma { 3812a9063ab9STao Ma int ret, data_changed = 0; 3813110a045aSTao Ma struct buffer_head *ref_root_bh = NULL; 3814110a045aSTao Ma struct ocfs2_inode_info *oi = OCFS2_I(inode); 3815110a045aSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3816110a045aSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3817110a045aSTao Ma struct ocfs2_refcount_tree *ref_tree; 3818110a045aSTao Ma unsigned int ext_flags; 3819110a045aSTao Ma loff_t size; 3820110a045aSTao Ma u32 cpos, num_clusters, clusters, p_cluster; 3821110a045aSTao Ma struct ocfs2_cached_dealloc_ctxt dealloc; 3822110a045aSTao Ma struct ocfs2_extent_tree di_et; 3823110a045aSTao Ma 3824110a045aSTao Ma ocfs2_init_dealloc_ctxt(&dealloc); 3825110a045aSTao Ma 3826110a045aSTao Ma if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) { 3827110a045aSTao Ma ret = ocfs2_create_refcount_tree(inode, di_bh); 3828110a045aSTao Ma if (ret) { 3829110a045aSTao Ma mlog_errno(ret); 3830110a045aSTao Ma goto out; 3831110a045aSTao Ma } 3832110a045aSTao Ma } 3833110a045aSTao Ma 3834110a045aSTao Ma BUG_ON(!di->i_refcount_loc); 3835110a045aSTao Ma ret = ocfs2_lock_refcount_tree(osb, 3836110a045aSTao Ma le64_to_cpu(di->i_refcount_loc), 1, 3837110a045aSTao Ma &ref_tree, &ref_root_bh); 3838110a045aSTao Ma if (ret) { 3839110a045aSTao Ma mlog_errno(ret); 3840110a045aSTao Ma goto out; 3841110a045aSTao Ma } 3842110a045aSTao Ma 38432f48d593STao Ma if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) 38442f48d593STao Ma goto attach_xattr; 38452f48d593STao Ma 3846110a045aSTao Ma ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh); 3847110a045aSTao Ma 3848110a045aSTao Ma size = i_size_read(inode); 3849110a045aSTao Ma clusters = ocfs2_clusters_for_bytes(inode->i_sb, size); 3850110a045aSTao Ma 3851110a045aSTao Ma cpos = 0; 3852110a045aSTao Ma while (cpos < clusters) { 3853110a045aSTao Ma ret = ocfs2_get_clusters(inode, cpos, &p_cluster, 3854110a045aSTao Ma &num_clusters, &ext_flags); 38552b0f6eaeSJoseph Qi if (ret) { 38562b0f6eaeSJoseph Qi mlog_errno(ret); 38572b0f6eaeSJoseph Qi goto unlock; 38582b0f6eaeSJoseph Qi } 3859110a045aSTao Ma if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) { 3860110a045aSTao Ma ret = ocfs2_add_refcount_flag(inode, &di_et, 3861110a045aSTao Ma &ref_tree->rf_ci, 3862110a045aSTao Ma ref_root_bh, cpos, 3863110a045aSTao Ma p_cluster, num_clusters, 38640129241eSTao Ma &dealloc, NULL); 3865110a045aSTao Ma if (ret) { 3866110a045aSTao Ma mlog_errno(ret); 3867a9063ab9STao Ma goto unlock; 3868110a045aSTao Ma } 3869a9063ab9STao Ma 3870a9063ab9STao Ma data_changed = 1; 3871110a045aSTao Ma } 3872110a045aSTao Ma cpos += num_clusters; 3873110a045aSTao Ma } 3874110a045aSTao Ma 38752f48d593STao Ma attach_xattr: 38760129241eSTao Ma if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) { 38770129241eSTao Ma ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh, 38780129241eSTao Ma &ref_tree->rf_ci, 38790129241eSTao Ma ref_root_bh, 38800129241eSTao Ma &dealloc); 38810129241eSTao Ma if (ret) { 38820129241eSTao Ma mlog_errno(ret); 38830129241eSTao Ma goto unlock; 38840129241eSTao Ma } 38850129241eSTao Ma } 38860129241eSTao Ma 3887a9063ab9STao Ma if (data_changed) { 3888a9063ab9STao Ma ret = ocfs2_change_ctime(inode, di_bh); 3889a9063ab9STao Ma if (ret) 3890a9063ab9STao Ma mlog_errno(ret); 3891a9063ab9STao Ma } 3892a9063ab9STao Ma 3893a9063ab9STao Ma unlock: 3894110a045aSTao Ma ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 3895110a045aSTao Ma brelse(ref_root_bh); 3896110a045aSTao Ma 3897110a045aSTao Ma if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) { 3898110a045aSTao Ma ocfs2_schedule_truncate_log_flush(osb, 1); 3899110a045aSTao Ma ocfs2_run_deallocs(osb, &dealloc); 3900110a045aSTao Ma } 3901110a045aSTao Ma out: 3902110a045aSTao Ma /* 3903110a045aSTao Ma * Empty the extent map so that we may get the right extent 3904110a045aSTao Ma * record from the disk. 3905110a045aSTao Ma */ 3906110a045aSTao Ma ocfs2_extent_map_trunc(inode, 0); 3907110a045aSTao Ma 3908110a045aSTao Ma return ret; 3909110a045aSTao Ma } 3910110a045aSTao Ma 3911110a045aSTao Ma static int ocfs2_add_refcounted_extent(struct inode *inode, 3912110a045aSTao Ma struct ocfs2_extent_tree *et, 3913110a045aSTao Ma struct ocfs2_caching_info *ref_ci, 3914110a045aSTao Ma struct buffer_head *ref_root_bh, 3915110a045aSTao Ma u32 cpos, u32 p_cluster, u32 num_clusters, 3916110a045aSTao Ma unsigned int ext_flags, 3917110a045aSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 3918110a045aSTao Ma { 3919110a045aSTao Ma int ret; 3920110a045aSTao Ma handle_t *handle; 3921110a045aSTao Ma int credits = 0; 3922110a045aSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3923110a045aSTao Ma struct ocfs2_alloc_context *meta_ac = NULL; 3924110a045aSTao Ma 3925110a045aSTao Ma ret = ocfs2_lock_refcount_allocators(inode->i_sb, 3926110a045aSTao Ma p_cluster, num_clusters, 3927110a045aSTao Ma et, ref_ci, 3928110a045aSTao Ma ref_root_bh, &meta_ac, 3929110a045aSTao Ma NULL, &credits); 3930110a045aSTao Ma if (ret) { 3931110a045aSTao Ma mlog_errno(ret); 3932110a045aSTao Ma goto out; 3933110a045aSTao Ma } 3934110a045aSTao Ma 3935110a045aSTao Ma handle = ocfs2_start_trans(osb, credits); 3936110a045aSTao Ma if (IS_ERR(handle)) { 3937110a045aSTao Ma ret = PTR_ERR(handle); 3938110a045aSTao Ma mlog_errno(ret); 3939110a045aSTao Ma goto out; 3940110a045aSTao Ma } 3941110a045aSTao Ma 3942110a045aSTao Ma ret = ocfs2_insert_extent(handle, et, cpos, 394312d4cec9STao Ma ocfs2_clusters_to_blocks(inode->i_sb, p_cluster), 3944110a045aSTao Ma num_clusters, ext_flags, meta_ac); 3945110a045aSTao Ma if (ret) { 3946110a045aSTao Ma mlog_errno(ret); 3947110a045aSTao Ma goto out_commit; 3948110a045aSTao Ma } 3949110a045aSTao Ma 39502999d12fSTao Ma ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh, 3951110a045aSTao Ma p_cluster, num_clusters, 3952110a045aSTao Ma meta_ac, dealloc); 3953110a045aSTao Ma if (ret) 3954110a045aSTao Ma mlog_errno(ret); 3955110a045aSTao Ma 3956110a045aSTao Ma out_commit: 3957110a045aSTao Ma ocfs2_commit_trans(osb, handle); 3958110a045aSTao Ma out: 3959110a045aSTao Ma if (meta_ac) 3960110a045aSTao Ma ocfs2_free_alloc_context(meta_ac); 3961110a045aSTao Ma return ret; 3962110a045aSTao Ma } 3963110a045aSTao Ma 39642f48d593STao Ma static int ocfs2_duplicate_inline_data(struct inode *s_inode, 39652f48d593STao Ma struct buffer_head *s_bh, 39662f48d593STao Ma struct inode *t_inode, 39672f48d593STao Ma struct buffer_head *t_bh) 39682f48d593STao Ma { 39692f48d593STao Ma int ret; 39702f48d593STao Ma handle_t *handle; 39712f48d593STao Ma struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb); 39722f48d593STao Ma struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data; 39732f48d593STao Ma struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data; 39742f48d593STao Ma 39752f48d593STao Ma BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)); 39762f48d593STao Ma 39772f48d593STao Ma handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 39782f48d593STao Ma if (IS_ERR(handle)) { 39792f48d593STao Ma ret = PTR_ERR(handle); 39802f48d593STao Ma mlog_errno(ret); 39812f48d593STao Ma goto out; 39822f48d593STao Ma } 39832f48d593STao Ma 39842f48d593STao Ma ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh, 39852f48d593STao Ma OCFS2_JOURNAL_ACCESS_WRITE); 39862f48d593STao Ma if (ret) { 39872f48d593STao Ma mlog_errno(ret); 39882f48d593STao Ma goto out_commit; 39892f48d593STao Ma } 39902f48d593STao Ma 39912f48d593STao Ma t_di->id2.i_data.id_count = s_di->id2.i_data.id_count; 39922f48d593STao Ma memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data, 39932f48d593STao Ma le16_to_cpu(s_di->id2.i_data.id_count)); 39942f48d593STao Ma spin_lock(&OCFS2_I(t_inode)->ip_lock); 39952f48d593STao Ma OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL; 39962f48d593STao Ma t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features); 39972f48d593STao Ma spin_unlock(&OCFS2_I(t_inode)->ip_lock); 39982f48d593STao Ma 39992f48d593STao Ma ocfs2_journal_dirty(handle, t_bh); 40002f48d593STao Ma 40012f48d593STao Ma out_commit: 40022f48d593STao Ma ocfs2_commit_trans(osb, handle); 40032f48d593STao Ma out: 40042f48d593STao Ma return ret; 40052f48d593STao Ma } 40062f48d593STao Ma 4007110a045aSTao Ma static int ocfs2_duplicate_extent_list(struct inode *s_inode, 4008110a045aSTao Ma struct inode *t_inode, 4009110a045aSTao Ma struct buffer_head *t_bh, 4010110a045aSTao Ma struct ocfs2_caching_info *ref_ci, 4011110a045aSTao Ma struct buffer_head *ref_root_bh, 4012110a045aSTao Ma struct ocfs2_cached_dealloc_ctxt *dealloc) 4013110a045aSTao Ma { 4014110a045aSTao Ma int ret = 0; 4015110a045aSTao Ma u32 p_cluster, num_clusters, clusters, cpos; 4016110a045aSTao Ma loff_t size; 4017110a045aSTao Ma unsigned int ext_flags; 4018110a045aSTao Ma struct ocfs2_extent_tree et; 4019110a045aSTao Ma 4020110a045aSTao Ma ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh); 4021110a045aSTao Ma 4022110a045aSTao Ma size = i_size_read(s_inode); 4023110a045aSTao Ma clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size); 4024110a045aSTao Ma 4025110a045aSTao Ma cpos = 0; 4026110a045aSTao Ma while (cpos < clusters) { 4027110a045aSTao Ma ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster, 4028110a045aSTao Ma &num_clusters, &ext_flags); 40292b0f6eaeSJoseph Qi if (ret) { 40302b0f6eaeSJoseph Qi mlog_errno(ret); 40312b0f6eaeSJoseph Qi goto out; 40322b0f6eaeSJoseph Qi } 4033110a045aSTao Ma if (p_cluster) { 4034110a045aSTao Ma ret = ocfs2_add_refcounted_extent(t_inode, &et, 4035110a045aSTao Ma ref_ci, ref_root_bh, 4036110a045aSTao Ma cpos, p_cluster, 4037110a045aSTao Ma num_clusters, 4038110a045aSTao Ma ext_flags, 4039110a045aSTao Ma dealloc); 4040110a045aSTao Ma if (ret) { 4041110a045aSTao Ma mlog_errno(ret); 4042110a045aSTao Ma goto out; 4043110a045aSTao Ma } 4044110a045aSTao Ma } 4045110a045aSTao Ma 4046110a045aSTao Ma cpos += num_clusters; 4047110a045aSTao Ma } 4048110a045aSTao Ma 4049110a045aSTao Ma out: 4050110a045aSTao Ma return ret; 4051110a045aSTao Ma } 4052110a045aSTao Ma 4053a9063ab9STao Ma /* 4054a9063ab9STao Ma * change the new file's attributes to the src. 4055a9063ab9STao Ma * 4056a9063ab9STao Ma * reflink creates a snapshot of a file, that means the attributes 4057a9063ab9STao Ma * must be identical except for three exceptions - nlink, ino, and ctime. 4058a9063ab9STao Ma */ 4059a9063ab9STao Ma static int ocfs2_complete_reflink(struct inode *s_inode, 4060a9063ab9STao Ma struct buffer_head *s_bh, 4061a9063ab9STao Ma struct inode *t_inode, 40620fe9b66cSTao Ma struct buffer_head *t_bh, 40630fe9b66cSTao Ma bool preserve) 4064a9063ab9STao Ma { 4065a9063ab9STao Ma int ret; 4066a9063ab9STao Ma handle_t *handle; 4067a9063ab9STao Ma struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data; 4068a9063ab9STao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data; 4069a9063ab9STao Ma loff_t size = i_size_read(s_inode); 4070a9063ab9STao Ma 4071a9063ab9STao Ma handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb), 4072a9063ab9STao Ma OCFS2_INODE_UPDATE_CREDITS); 4073a9063ab9STao Ma if (IS_ERR(handle)) { 4074a9063ab9STao Ma ret = PTR_ERR(handle); 4075a9063ab9STao Ma mlog_errno(ret); 4076a9063ab9STao Ma return ret; 4077a9063ab9STao Ma } 4078a9063ab9STao Ma 4079a9063ab9STao Ma ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh, 4080a9063ab9STao Ma OCFS2_JOURNAL_ACCESS_WRITE); 4081a9063ab9STao Ma if (ret) { 4082a9063ab9STao Ma mlog_errno(ret); 4083a9063ab9STao Ma goto out_commit; 4084a9063ab9STao Ma } 4085a9063ab9STao Ma 4086a9063ab9STao Ma spin_lock(&OCFS2_I(t_inode)->ip_lock); 4087a9063ab9STao Ma OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters; 4088a9063ab9STao Ma OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr; 4089a9063ab9STao Ma OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features; 4090a9063ab9STao Ma spin_unlock(&OCFS2_I(t_inode)->ip_lock); 4091a9063ab9STao Ma i_size_write(t_inode, size); 40926527f8f8STao Ma t_inode->i_blocks = s_inode->i_blocks; 4093a9063ab9STao Ma 4094a9063ab9STao Ma di->i_xattr_inline_size = s_di->i_xattr_inline_size; 4095a9063ab9STao Ma di->i_clusters = s_di->i_clusters; 4096a9063ab9STao Ma di->i_size = s_di->i_size; 4097a9063ab9STao Ma di->i_dyn_features = s_di->i_dyn_features; 4098a9063ab9STao Ma di->i_attr = s_di->i_attr; 40990fe9b66cSTao Ma 41000fe9b66cSTao Ma if (preserve) { 4101c21a534eSTao Ma t_inode->i_uid = s_inode->i_uid; 4102c21a534eSTao Ma t_inode->i_gid = s_inode->i_gid; 4103c21a534eSTao Ma t_inode->i_mode = s_inode->i_mode; 4104a9063ab9STao Ma di->i_uid = s_di->i_uid; 4105a9063ab9STao Ma di->i_gid = s_di->i_gid; 4106a9063ab9STao Ma di->i_mode = s_di->i_mode; 4107a9063ab9STao Ma 4108a9063ab9STao Ma /* 4109a9063ab9STao Ma * update time. 41100fe9b66cSTao Ma * we want mtime to appear identical to the source and 41110fe9b66cSTao Ma * update ctime. 4112a9063ab9STao Ma */ 4113a9063ab9STao Ma t_inode->i_ctime = CURRENT_TIME; 4114a9063ab9STao Ma 4115a9063ab9STao Ma di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec); 4116a9063ab9STao Ma di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec); 4117a9063ab9STao Ma 4118a9063ab9STao Ma t_inode->i_mtime = s_inode->i_mtime; 4119a9063ab9STao Ma di->i_mtime = s_di->i_mtime; 4120a9063ab9STao Ma di->i_mtime_nsec = s_di->i_mtime_nsec; 41210fe9b66cSTao Ma } 4122a9063ab9STao Ma 4123a9063ab9STao Ma ocfs2_journal_dirty(handle, t_bh); 4124a9063ab9STao Ma 4125a9063ab9STao Ma out_commit: 4126a9063ab9STao Ma ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle); 4127a9063ab9STao Ma return ret; 4128a9063ab9STao Ma } 4129a9063ab9STao Ma 4130110a045aSTao Ma static int ocfs2_create_reflink_node(struct inode *s_inode, 4131110a045aSTao Ma struct buffer_head *s_bh, 4132110a045aSTao Ma struct inode *t_inode, 41330fe9b66cSTao Ma struct buffer_head *t_bh, 41340fe9b66cSTao Ma bool preserve) 4135110a045aSTao Ma { 4136110a045aSTao Ma int ret; 4137110a045aSTao Ma struct buffer_head *ref_root_bh = NULL; 4138110a045aSTao Ma struct ocfs2_cached_dealloc_ctxt dealloc; 4139110a045aSTao Ma struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb); 4140110a045aSTao Ma struct ocfs2_refcount_block *rb; 4141110a045aSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data; 4142110a045aSTao Ma struct ocfs2_refcount_tree *ref_tree; 4143110a045aSTao Ma 4144110a045aSTao Ma ocfs2_init_dealloc_ctxt(&dealloc); 4145110a045aSTao Ma 4146110a045aSTao Ma ret = ocfs2_set_refcount_tree(t_inode, t_bh, 4147110a045aSTao Ma le64_to_cpu(di->i_refcount_loc)); 4148110a045aSTao Ma if (ret) { 4149110a045aSTao Ma mlog_errno(ret); 4150110a045aSTao Ma goto out; 4151110a045aSTao Ma } 4152110a045aSTao Ma 41532f48d593STao Ma if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 41542f48d593STao Ma ret = ocfs2_duplicate_inline_data(s_inode, s_bh, 41552f48d593STao Ma t_inode, t_bh); 41562f48d593STao Ma if (ret) 41572f48d593STao Ma mlog_errno(ret); 41582f48d593STao Ma goto out; 41592f48d593STao Ma } 41602f48d593STao Ma 4161110a045aSTao Ma ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), 4162110a045aSTao Ma 1, &ref_tree, &ref_root_bh); 4163110a045aSTao Ma if (ret) { 4164110a045aSTao Ma mlog_errno(ret); 4165110a045aSTao Ma goto out; 4166110a045aSTao Ma } 4167110a045aSTao Ma rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data; 4168110a045aSTao Ma 4169110a045aSTao Ma ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh, 4170110a045aSTao Ma &ref_tree->rf_ci, ref_root_bh, 4171110a045aSTao Ma &dealloc); 4172a9063ab9STao Ma if (ret) { 4173a9063ab9STao Ma mlog_errno(ret); 4174a9063ab9STao Ma goto out_unlock_refcount; 4175a9063ab9STao Ma } 4176a9063ab9STao Ma 4177a9063ab9STao Ma out_unlock_refcount: 4178110a045aSTao Ma ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 4179110a045aSTao Ma brelse(ref_root_bh); 4180110a045aSTao Ma out: 4181110a045aSTao Ma if (ocfs2_dealloc_has_cluster(&dealloc)) { 4182110a045aSTao Ma ocfs2_schedule_truncate_log_flush(osb, 1); 4183110a045aSTao Ma ocfs2_run_deallocs(osb, &dealloc); 4184110a045aSTao Ma } 4185110a045aSTao Ma 4186110a045aSTao Ma return ret; 4187110a045aSTao Ma } 418809bf27a0STao Ma 418909bf27a0STao Ma static int __ocfs2_reflink(struct dentry *old_dentry, 419009bf27a0STao Ma struct buffer_head *old_bh, 419109bf27a0STao Ma struct inode *new_inode, 419209bf27a0STao Ma bool preserve) 419309bf27a0STao Ma { 419409bf27a0STao Ma int ret; 41952b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 419609bf27a0STao Ma struct buffer_head *new_bh = NULL; 419709bf27a0STao Ma 41985693486bSJoel Becker if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) { 41995693486bSJoel Becker ret = -EINVAL; 42005693486bSJoel Becker mlog_errno(ret); 42015693486bSJoel Becker goto out; 42025693486bSJoel Becker } 42035693486bSJoel Becker 420409bf27a0STao Ma ret = filemap_fdatawrite(inode->i_mapping); 420509bf27a0STao Ma if (ret) { 420609bf27a0STao Ma mlog_errno(ret); 420709bf27a0STao Ma goto out; 420809bf27a0STao Ma } 420909bf27a0STao Ma 421009bf27a0STao Ma ret = ocfs2_attach_refcount_tree(inode, old_bh); 421109bf27a0STao Ma if (ret) { 421209bf27a0STao Ma mlog_errno(ret); 421309bf27a0STao Ma goto out; 421409bf27a0STao Ma } 421509bf27a0STao Ma 421607eaac94STao Ma mutex_lock_nested(&new_inode->i_mutex, I_MUTEX_CHILD); 421707eaac94STao Ma ret = ocfs2_inode_lock_nested(new_inode, &new_bh, 1, 421807eaac94STao Ma OI_LS_REFLINK_TARGET); 421909bf27a0STao Ma if (ret) { 422009bf27a0STao Ma mlog_errno(ret); 422109bf27a0STao Ma goto out_unlock; 422209bf27a0STao Ma } 422309bf27a0STao Ma 422409bf27a0STao Ma ret = ocfs2_create_reflink_node(inode, old_bh, 422509bf27a0STao Ma new_inode, new_bh, preserve); 422609bf27a0STao Ma if (ret) { 422709bf27a0STao Ma mlog_errno(ret); 422809bf27a0STao Ma goto inode_unlock; 422909bf27a0STao Ma } 423009bf27a0STao Ma 423109bf27a0STao Ma if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_XATTR_FL) { 423209bf27a0STao Ma ret = ocfs2_reflink_xattrs(inode, old_bh, 423309bf27a0STao Ma new_inode, new_bh, 423409bf27a0STao Ma preserve); 423587f4b1bbSTao Ma if (ret) { 423687f4b1bbSTao Ma mlog_errno(ret); 423787f4b1bbSTao Ma goto inode_unlock; 423887f4b1bbSTao Ma } 423987f4b1bbSTao Ma } 424087f4b1bbSTao Ma 424187f4b1bbSTao Ma ret = ocfs2_complete_reflink(inode, old_bh, 424287f4b1bbSTao Ma new_inode, new_bh, preserve); 424309bf27a0STao Ma if (ret) 424409bf27a0STao Ma mlog_errno(ret); 424587f4b1bbSTao Ma 424609bf27a0STao Ma inode_unlock: 424709bf27a0STao Ma ocfs2_inode_unlock(new_inode, 1); 424809bf27a0STao Ma brelse(new_bh); 424909bf27a0STao Ma out_unlock: 425009bf27a0STao Ma mutex_unlock(&new_inode->i_mutex); 425109bf27a0STao Ma out: 425209bf27a0STao Ma if (!ret) { 425309bf27a0STao Ma ret = filemap_fdatawait(inode->i_mapping); 425409bf27a0STao Ma if (ret) 425509bf27a0STao Ma mlog_errno(ret); 425609bf27a0STao Ma } 425709bf27a0STao Ma return ret; 425809bf27a0STao Ma } 425909bf27a0STao Ma 426009bf27a0STao Ma static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir, 426109bf27a0STao Ma struct dentry *new_dentry, bool preserve) 426209bf27a0STao Ma { 426309bf27a0STao Ma int error; 42642b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 426509bf27a0STao Ma struct buffer_head *old_bh = NULL; 426609bf27a0STao Ma struct inode *new_orphan_inode = NULL; 4267702e5bc6SChristoph Hellwig struct posix_acl *default_acl, *acl; 4268702e5bc6SChristoph Hellwig umode_t mode; 426909bf27a0STao Ma 427009bf27a0STao Ma if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) 427109bf27a0STao Ma return -EOPNOTSUPP; 427209bf27a0STao Ma 4273702e5bc6SChristoph Hellwig mode = inode->i_mode; 4274702e5bc6SChristoph Hellwig error = posix_acl_create(dir, &mode, &default_acl, &acl); 4275702e5bc6SChristoph Hellwig if (error) { 4276702e5bc6SChristoph Hellwig mlog_errno(error); 4277e073fc58SDan Carpenter return error; 4278702e5bc6SChristoph Hellwig } 4279702e5bc6SChristoph Hellwig 4280702e5bc6SChristoph Hellwig error = ocfs2_create_inode_in_orphan(dir, mode, 428109bf27a0STao Ma &new_orphan_inode); 428209bf27a0STao Ma if (error) { 428309bf27a0STao Ma mlog_errno(error); 428409bf27a0STao Ma goto out; 428509bf27a0STao Ma } 428609bf27a0STao Ma 42878a8ad1c2SWengang Wang error = ocfs2_rw_lock(inode, 1); 42888a8ad1c2SWengang Wang if (error) { 42898a8ad1c2SWengang Wang mlog_errno(error); 42908a8ad1c2SWengang Wang goto out; 42918a8ad1c2SWengang Wang } 42928a8ad1c2SWengang Wang 429309bf27a0STao Ma error = ocfs2_inode_lock(inode, &old_bh, 1); 429409bf27a0STao Ma if (error) { 429509bf27a0STao Ma mlog_errno(error); 42968a8ad1c2SWengang Wang ocfs2_rw_unlock(inode, 1); 429709bf27a0STao Ma goto out; 429809bf27a0STao Ma } 429909bf27a0STao Ma 430009bf27a0STao Ma down_write(&OCFS2_I(inode)->ip_xattr_sem); 430109bf27a0STao Ma down_write(&OCFS2_I(inode)->ip_alloc_sem); 430209bf27a0STao Ma error = __ocfs2_reflink(old_dentry, old_bh, 430309bf27a0STao Ma new_orphan_inode, preserve); 430409bf27a0STao Ma up_write(&OCFS2_I(inode)->ip_alloc_sem); 430509bf27a0STao Ma up_write(&OCFS2_I(inode)->ip_xattr_sem); 430609bf27a0STao Ma 430709bf27a0STao Ma ocfs2_inode_unlock(inode, 1); 43088a8ad1c2SWengang Wang ocfs2_rw_unlock(inode, 1); 430909bf27a0STao Ma brelse(old_bh); 431009bf27a0STao Ma 431109bf27a0STao Ma if (error) { 431209bf27a0STao Ma mlog_errno(error); 431309bf27a0STao Ma goto out; 431409bf27a0STao Ma } 431509bf27a0STao Ma 431609bf27a0STao Ma /* If the security isn't preserved, we need to re-initialize them. */ 431709bf27a0STao Ma if (!preserve) { 43182a7dba39SEric Paris error = ocfs2_init_security_and_acl(dir, new_orphan_inode, 4319702e5bc6SChristoph Hellwig &new_dentry->d_name, 4320702e5bc6SChristoph Hellwig default_acl, acl); 432109bf27a0STao Ma if (error) 432209bf27a0STao Ma mlog_errno(error); 432309bf27a0STao Ma } 432409bf27a0STao Ma out: 4325702e5bc6SChristoph Hellwig if (default_acl) 4326702e5bc6SChristoph Hellwig posix_acl_release(default_acl); 4327702e5bc6SChristoph Hellwig if (acl) 4328702e5bc6SChristoph Hellwig posix_acl_release(acl); 432909bf27a0STao Ma if (!error) { 433009bf27a0STao Ma error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode, 433109bf27a0STao Ma new_dentry); 433209bf27a0STao Ma if (error) 433309bf27a0STao Ma mlog_errno(error); 433409bf27a0STao Ma } 433509bf27a0STao Ma 433609bf27a0STao Ma if (new_orphan_inode) { 433709bf27a0STao Ma /* 433809bf27a0STao Ma * We need to open_unlock the inode no matter whether we 433909bf27a0STao Ma * succeed or not, so that other nodes can delete it later. 434009bf27a0STao Ma */ 434109bf27a0STao Ma ocfs2_open_unlock(new_orphan_inode); 434209bf27a0STao Ma if (error) 434309bf27a0STao Ma iput(new_orphan_inode); 434409bf27a0STao Ma } 434509bf27a0STao Ma 434609bf27a0STao Ma return error; 434709bf27a0STao Ma } 4348bd50873dSTao Ma 4349bd50873dSTao Ma /* 4350bd50873dSTao Ma * Below here are the bits used by OCFS2_IOC_REFLINK() to fake 4351bd50873dSTao Ma * sys_reflink(). This will go away when vfs_reflink() exists in 4352bd50873dSTao Ma * fs/namei.c. 4353bd50873dSTao Ma */ 4354bd50873dSTao Ma 4355bd50873dSTao Ma /* copied from may_create in VFS. */ 4356bd50873dSTao Ma static inline int ocfs2_may_create(struct inode *dir, struct dentry *child) 4357bd50873dSTao Ma { 43582b0143b5SDavid Howells if (d_really_is_positive(child)) 4359bd50873dSTao Ma return -EEXIST; 4360bd50873dSTao Ma if (IS_DEADDIR(dir)) 4361bd50873dSTao Ma return -ENOENT; 4362bd50873dSTao Ma return inode_permission(dir, MAY_WRITE | MAY_EXEC); 4363bd50873dSTao Ma } 4364bd50873dSTao Ma 4365bd50873dSTao Ma /** 4366bd50873dSTao Ma * ocfs2_vfs_reflink - Create a reference-counted link 4367bd50873dSTao Ma * 4368bd50873dSTao Ma * @old_dentry: source dentry + inode 4369bd50873dSTao Ma * @dir: directory to create the target 4370bd50873dSTao Ma * @new_dentry: target dentry 4371bd50873dSTao Ma * @preserve: if true, preserve all file attributes 4372bd50873dSTao Ma */ 437312d4cec9STao Ma static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir, 4374bd50873dSTao Ma struct dentry *new_dentry, bool preserve) 4375bd50873dSTao Ma { 43762b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 4377bd50873dSTao Ma int error; 4378bd50873dSTao Ma 4379bd50873dSTao Ma if (!inode) 4380bd50873dSTao Ma return -ENOENT; 4381bd50873dSTao Ma 4382bd50873dSTao Ma error = ocfs2_may_create(dir, new_dentry); 4383bd50873dSTao Ma if (error) 4384bd50873dSTao Ma return error; 4385bd50873dSTao Ma 4386bd50873dSTao Ma if (dir->i_sb != inode->i_sb) 4387bd50873dSTao Ma return -EXDEV; 4388bd50873dSTao Ma 4389bd50873dSTao Ma /* 4390bd50873dSTao Ma * A reflink to an append-only or immutable file cannot be created. 4391bd50873dSTao Ma */ 4392bd50873dSTao Ma if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 4393bd50873dSTao Ma return -EPERM; 4394bd50873dSTao Ma 4395bd50873dSTao Ma /* Only regular files can be reflinked. */ 4396bd50873dSTao Ma if (!S_ISREG(inode->i_mode)) 4397bd50873dSTao Ma return -EPERM; 4398bd50873dSTao Ma 4399bd50873dSTao Ma /* 4400bd50873dSTao Ma * If the caller wants to preserve ownership, they require the 4401bd50873dSTao Ma * rights to do so. 4402bd50873dSTao Ma */ 4403bd50873dSTao Ma if (preserve) { 4404488c8ef0SEric W. Biederman if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_CHOWN)) 4405bd50873dSTao Ma return -EPERM; 4406bd50873dSTao Ma if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN)) 4407bd50873dSTao Ma return -EPERM; 4408bd50873dSTao Ma } 4409bd50873dSTao Ma 4410bd50873dSTao Ma /* 4411bd50873dSTao Ma * If the caller is modifying any aspect of the attributes, they 4412bd50873dSTao Ma * are not creating a snapshot. They need read permission on the 4413bd50873dSTao Ma * file. 4414bd50873dSTao Ma */ 4415bd50873dSTao Ma if (!preserve) { 4416bd50873dSTao Ma error = inode_permission(inode, MAY_READ); 4417bd50873dSTao Ma if (error) 4418bd50873dSTao Ma return error; 4419bd50873dSTao Ma } 4420bd50873dSTao Ma 4421bd50873dSTao Ma mutex_lock(&inode->i_mutex); 4422871a2931SChristoph Hellwig dquot_initialize(dir); 4423bd50873dSTao Ma error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve); 4424bd50873dSTao Ma mutex_unlock(&inode->i_mutex); 4425bd50873dSTao Ma if (!error) 4426bd50873dSTao Ma fsnotify_create(dir, new_dentry); 4427bd50873dSTao Ma return error; 4428bd50873dSTao Ma } 4429bd50873dSTao Ma /* 4430bd50873dSTao Ma * Most codes are copied from sys_linkat. 4431bd50873dSTao Ma */ 4432bd50873dSTao Ma int ocfs2_reflink_ioctl(struct inode *inode, 4433bd50873dSTao Ma const char __user *oldname, 4434bd50873dSTao Ma const char __user *newname, 4435bd50873dSTao Ma bool preserve) 4436bd50873dSTao Ma { 4437bd50873dSTao Ma struct dentry *new_dentry; 4438dae6ad8fSAl Viro struct path old_path, new_path; 4439bd50873dSTao Ma int error; 4440bd50873dSTao Ma 4441bd50873dSTao Ma if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) 4442bd50873dSTao Ma return -EOPNOTSUPP; 4443bd50873dSTao Ma 4444bd50873dSTao Ma error = user_path_at(AT_FDCWD, oldname, 0, &old_path); 4445bd50873dSTao Ma if (error) { 4446bd50873dSTao Ma mlog_errno(error); 4447bd50873dSTao Ma return error; 4448bd50873dSTao Ma } 4449bd50873dSTao Ma 4450dae6ad8fSAl Viro new_dentry = user_path_create(AT_FDCWD, newname, &new_path, 0); 4451dae6ad8fSAl Viro error = PTR_ERR(new_dentry); 4452dae6ad8fSAl Viro if (IS_ERR(new_dentry)) { 4453bd50873dSTao Ma mlog_errno(error); 4454bd50873dSTao Ma goto out; 4455bd50873dSTao Ma } 4456bd50873dSTao Ma 4457bd50873dSTao Ma error = -EXDEV; 4458dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) { 4459bd50873dSTao Ma mlog_errno(error); 4460dae6ad8fSAl Viro goto out_dput; 4461bd50873dSTao Ma } 4462bd50873dSTao Ma 4463bd50873dSTao Ma error = ocfs2_vfs_reflink(old_path.dentry, 44642b0143b5SDavid Howells d_inode(new_path.dentry), 4465bd50873dSTao Ma new_dentry, preserve); 4466bd50873dSTao Ma out_dput: 4467921a1650SAl Viro done_path_create(&new_path, new_dentry); 4468bd50873dSTao Ma out: 4469bd50873dSTao Ma path_put(&old_path); 4470bd50873dSTao Ma 4471bd50873dSTao Ma return error; 4472bd50873dSTao Ma } 4473