^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * move_extents.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2011 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <cluster/masklog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "ocfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "ocfs2_ioctl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "localalloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "aops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "dlmglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "extent_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "journal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "suballoc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "uptodate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "super.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "buffer_head_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "sysfile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "refcounttree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "move_extents.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct ocfs2_move_extents_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int auto_defrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u32 new_phys_cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u32 clusters_moved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u64 refcount_loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct ocfs2_move_extents *range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct ocfs2_extent_tree et;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct ocfs2_alloc_context *meta_ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct ocfs2_alloc_context *data_ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct ocfs2_cached_dealloc_ctxt dealloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int __ocfs2_move_extent(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct ocfs2_move_extents_context *context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u32 cpos, u32 len, u32 p_cpos, u32 new_p_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int ext_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int ret = 0, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct inode *inode = context->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct ocfs2_extent_rec *rec, replace_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct ocfs2_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct ocfs2_extent_list *el;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u64 ino = ocfs2_metadata_cache_owner(context->et.et_ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u64 old_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ret = ocfs2_duplicate_clusters_by_page(handle, inode, cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) p_cpos, new_p_cpos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) memset(&replace_rec, 0, sizeof(replace_rec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) replace_rec.e_cpos = cpu_to_le32(cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) replace_rec.e_leaf_clusters = cpu_to_le16(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) new_p_cpos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) path = ocfs2_new_path_from_et(&context->et);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = ocfs2_find_path(INODE_CACHE(inode), path, cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) el = path_leaf_el(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) index = ocfs2_search_extent_list(el, cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (index == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = ocfs2_error(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) "Inode %llu has an extent at cpos %u which can no longer be found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) (unsigned long long)ino, cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) rec = &el->l_recs[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) BUG_ON(ext_flags != rec->e_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * after moving/defraging to new location, the extent is not going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * to be refcounted anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) context->et.et_root_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ret = ocfs2_split_extent(handle, &context->et, path, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) &replace_rec, context->meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) &context->dealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ocfs2_journal_dirty(handle, context->et.et_root_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) context->new_phys_cpos = new_p_cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * need I to append truncate log for old clusters?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (old_blkno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (ext_flags & OCFS2_EXT_REFCOUNTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ret = ocfs2_decrease_refcount(inode, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ocfs2_blocks_to_clusters(osb->sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) old_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) len, context->meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) &context->dealloc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = ocfs2_truncate_log_append(osb, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) old_blkno, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ocfs2_update_inode_fsync_trans(handle, inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ocfs2_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * lock allocator, and reserve appropriate number of bits for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * meta blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int ocfs2_lock_meta_allocator_move_extents(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct ocfs2_extent_tree *et,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u32 clusters_to_move,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u32 extents_to_split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct ocfs2_alloc_context **meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int extra_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int *credits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int ret, num_free_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) num_free_extents = ocfs2_num_free_extents(et);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (num_free_extents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = num_free_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!num_free_extents ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) extra_blocks, clusters_to_move, *credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (*meta_ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ocfs2_free_alloc_context(*meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Using one journal handle to guarantee the data consistency in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * crash happens anywhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * XXX: defrag can end up with finishing partial extent as requested,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * due to not enough contiguous clusters can be found in allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) u32 cpos, u32 phys_cpos, u32 *len, int ext_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int ret, credits = 0, extra_blocks = 0, partial = context->partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct inode *inode = context->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct inode *tl_inode = osb->osb_tl_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct ocfs2_refcount_tree *ref_tree = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) u32 new_phys_cpos, new_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int need_free = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) BUG_ON(!ocfs2_is_refcount_inode(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) BUG_ON(!context->refcount_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) &ref_tree, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = ocfs2_prepare_refcount_change_for_del(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) context->refcount_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) phys_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) &credits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) &extra_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *len, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) &context->meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) extra_blocks, &credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * should be using allocation reservation strategy there?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * if (context->data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) inode_lock(tl_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (ocfs2_truncate_log_needs_flush(osb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ret = __ocfs2_flush_truncate_log(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto out_unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * Make sure ocfs2_reserve_cluster is called after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * __ocfs2_flush_truncate_log, otherwise, dead lock may happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * If ocfs2_reserve_cluster is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * before __ocfs2_flush_truncate_log, dead lock on global bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * may happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ret = ocfs2_reserve_clusters(osb, *len, &context->data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) goto out_unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) handle = ocfs2_start_trans(osb, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto out_unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) &new_phys_cpos, &new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * allowing partial extent moving is kind of 'pros and cons', it makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * whole defragmentation less likely to fail, on the contrary, the bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * thing is it may make the fs even more fragmented after moving, let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * userspace make a good decision here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (new_len != *len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) mlog(0, "len_claimed: %u, len: %u\n", new_len, *len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!partial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) need_free = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) phys_cpos, new_phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) new_phys_cpos, ext_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (partial && (new_len != *len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) *len = new_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * Here we should write the new page out first if we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * in write-back mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (need_free && context->data_ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct ocfs2_alloc_context *data_ac = context->data_ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (context->data_ac->ac_which == OCFS2_AC_USE_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ocfs2_free_local_alloc_bits(osb, handle, data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) new_phys_cpos, new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ocfs2_free_clusters(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) data_ac->ac_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) data_ac->ac_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) ocfs2_clusters_to_blocks(osb->sb, new_phys_cpos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) out_unlock_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) inode_unlock(tl_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (context->data_ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ocfs2_free_alloc_context(context->data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) context->data_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (context->meta_ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ocfs2_free_alloc_context(context->meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) context->meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (ref_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * find the victim alloc group, where #blkno fits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int ocfs2_find_victim_alloc_group(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) u64 vict_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int type, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int *vict_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct buffer_head **ret_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int ret, i, bits_per_unit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) char namebuf[40];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct buffer_head *ac_bh = NULL, *gd_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct ocfs2_chain_list *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct ocfs2_chain_rec *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct ocfs2_dinode *ac_dinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct ocfs2_group_desc *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) strlen(namebuf), &blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) cl = &(ac_dinode->id2.i_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) rec = &(cl->cl_recs[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (type == GLOBAL_BITMAP_SYSTEM_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) bits_per_unit = osb->s_clustersize_bits -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * 'vict_blkno' was out of the valid range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if ((vict_blkno < le64_to_cpu(rec->c_blkno)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) (vict_blkno >= ((u64)le32_to_cpu(ac_dinode->id1.bitmap1.i_total) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) bits_per_unit))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) rec = &(cl->cl_recs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) bg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (!bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) blkno = le64_to_cpu(rec->c_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) blkno = le64_to_cpu(bg->bg_next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (gd_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) brelse(gd_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) gd_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) bg = (struct ocfs2_group_desc *)gd_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (vict_blkno < (le64_to_cpu(bg->bg_blkno) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) le16_to_cpu(bg->bg_bits))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) *ret_bh = gd_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) *vict_bit = (vict_blkno - blkno) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) bits_per_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) mlog(0, "find the victim group: #%llu, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) "total_bits: %u, vict_bit: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) blkno, le16_to_cpu(bg->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) *vict_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) } while (le64_to_cpu(bg->bg_next_group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) brelse(ac_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * caller has to release the gd_bh properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * XXX: helper to validate and adjust moving goal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static int ocfs2_validate_and_adjust_move_goal(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct ocfs2_move_extents *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int ret, goal_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct buffer_head *gd_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct ocfs2_group_desc *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) int c_to_b = 1 << (osb->s_clustersize_bits -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * make goal become cluster aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) range->me_goal = ocfs2_block_to_cluster_start(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) range->me_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * validate goal sits within global_bitmap, and return the victim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * group desc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) ret = ocfs2_find_victim_alloc_group(inode, range->me_goal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) GLOBAL_BITMAP_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) OCFS2_INVALID_SLOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) &goal_bit, &gd_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) bg = (struct ocfs2_group_desc *)gd_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * moving goal is not allowd to start with a group desc blok(#0 blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * let's compromise to the latter cluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (range->me_goal == le64_to_cpu(bg->bg_blkno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) range->me_goal += c_to_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * movement is not gonna cross two groups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) range->me_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * more exact validations/adjustments will be performed later during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * moving operation for each extent range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) mlog(0, "extents get ready to be moved to #%llu block\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) range->me_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) brelse(gd_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) int *goal_bit, u32 move_len, u32 max_hop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) u32 *phys_cpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int i, used, last_free_bits = 0, base_bit = *goal_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) le64_to_cpu(gd->bg_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * we even tried searching the free chunk by jumping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * a 'max_hop' distance, but still failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if ((i - base_bit) > max_hop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) *phys_cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (last_free_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) last_free_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) last_free_bits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (last_free_bits == move_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) *goal_bit = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) *phys_cpos = base_cpos + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) u32 cpos, u32 phys_cpos, u32 *new_phys_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) u32 len, int ext_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) int ret, credits = 0, extra_blocks = 0, goal_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct inode *inode = context->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct inode *tl_inode = osb->osb_tl_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct inode *gb_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct buffer_head *gb_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) struct buffer_head *gd_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) struct ocfs2_group_desc *gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct ocfs2_refcount_tree *ref_tree = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) context->range->me_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) u64 phys_blkno, new_phys_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) BUG_ON(!ocfs2_is_refcount_inode(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) BUG_ON(!context->refcount_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) &ref_tree, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) ret = ocfs2_prepare_refcount_change_for_del(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) context->refcount_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) phys_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) &credits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) &extra_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) len, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) &context->meta_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) extra_blocks, &credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * need to count 2 extra credits for global_bitmap inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * group descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) credits += OCFS2_INODE_UPDATE_CREDITS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * logic, while we still need to lock the global_bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) OCFS2_INVALID_SLOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (!gb_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) mlog(ML_ERROR, "unable to get global_bitmap inode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) inode_lock(gb_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) goto out_unlock_gb_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) inode_lock(tl_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) handle = ocfs2_start_trans(osb, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) goto out_unlock_tl_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) GLOBAL_BITMAP_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) OCFS2_INVALID_SLOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) &goal_bit, &gd_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * probe the victim cluster group to find a proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * region to fit wanted movement, it even will perfrom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * a best-effort attempt by compromising to a threshold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * around the goal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) new_phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (!*new_phys_cpos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) *new_phys_cpos, ext_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) gd = (struct ocfs2_group_desc *)gd_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) le16_to_cpu(gd->bg_chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) goal_bit, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) le16_to_cpu(gd->bg_chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * Here we should write the new page out first if we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * in write-back mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) brelse(gd_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) out_unlock_tl_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) inode_unlock(tl_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) ocfs2_inode_unlock(gb_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) out_unlock_gb_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) inode_unlock(gb_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) brelse(gb_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) iput(gb_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (context->meta_ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ocfs2_free_alloc_context(context->meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) context->meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (ref_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * Helper to calculate the defraging length in one run according to threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) static void ocfs2_calc_extent_defrag_len(u32 *alloc_size, u32 *len_defraged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) u32 threshold, int *skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if ((*alloc_size + *len_defraged) < threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * proceed defragmentation until we meet the thresh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) *len_defraged += *alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) } else if (*len_defraged == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * XXX: skip a large extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) *skip = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * split this extent to coalesce with former pieces as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * to reach the threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * we're done here with one cycle of defragmentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * in a size of 'thresh', resetting 'len_defraged'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * forces a new defragmentation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) *alloc_size = threshold - *len_defraged;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) *len_defraged = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static int __ocfs2_move_extents_range(struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct ocfs2_move_extents_context *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) int ret = 0, flags, do_defrag, skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) u32 cpos, phys_cpos, move_start, len_to_move, alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) u32 len_defraged = 0, defrag_thresh = 0, new_phys_cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) struct inode *inode = context->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) struct ocfs2_move_extents *range = context->range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) if ((i_size_read(inode) == 0) || (range->me_len == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) context->refcount_loc = le64_to_cpu(di->i_refcount_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) ocfs2_init_dinode_extent_tree(&context->et, INODE_CACHE(inode), di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) ocfs2_init_dealloc_ctxt(&context->dealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * TO-DO XXX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * - xattr extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) do_defrag = context->auto_defrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * extents moving happens in unit of clusters, for the sake
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * of simplicity, we may ignore two clusters where 'byte_start'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * and 'byte_start + len' were within.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) move_start = ocfs2_clusters_for_bytes(osb->sb, range->me_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) len_to_move = (range->me_start + range->me_len) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) osb->s_clustersize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (len_to_move >= move_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) len_to_move -= move_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) len_to_move = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (do_defrag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) defrag_thresh = range->me_threshold >> osb->s_clustersize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (defrag_thresh <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) new_phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) range->me_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) "thresh: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) (unsigned long long)OCFS2_I(inode)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) (unsigned long long)range->me_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) (unsigned long long)range->me_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) move_start, len_to_move, defrag_thresh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) cpos = move_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) while (len_to_move) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &alloc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (alloc_size > len_to_move)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) alloc_size = len_to_move;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) * XXX: how to deal with a hole:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * - skip the hole of course
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * - force a new defragmentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (!phys_cpos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (do_defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) len_defraged = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (do_defrag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) ocfs2_calc_extent_defrag_len(&alloc_size, &len_defraged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) defrag_thresh, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * skip large extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) mlog(0, "#Defrag: cpos: %u, phys_cpos: %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) "alloc_size: %u, len_defraged: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) cpos, phys_cpos, alloc_size, len_defraged);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) ret = ocfs2_defrag_extent(context, cpos, phys_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) &alloc_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) ret = ocfs2_move_extent(context, cpos, phys_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) &new_phys_cpos, alloc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) new_phys_cpos += alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) context->clusters_moved += alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) cpos += alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) len_to_move -= alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) range->me_flags |= OCFS2_MOVE_EXT_FL_COMPLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) range->me_moved_len = ocfs2_clusters_to_bytes(osb->sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) context->clusters_moved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) range->me_new_offset = ocfs2_clusters_to_bytes(osb->sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) context->new_phys_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) ocfs2_schedule_truncate_log_flush(osb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) ocfs2_run_deallocs(osb, &context->dealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) static int ocfs2_move_extents(struct ocfs2_move_extents_context *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct inode *inode = context->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) struct ocfs2_dinode *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct buffer_head *di_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * This prevents concurrent writes from other nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) status = ocfs2_rw_lock(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) status = ocfs2_inode_lock(inode, &di_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) goto out_rw_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * rememer ip_xattr_sem also needs to be held if necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) down_write(&OCFS2_I(inode)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) status = __ocfs2_move_extents_range(di_bh, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) up_write(&OCFS2_I(inode)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) goto out_inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * We update ctime for these changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) goto out_inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) goto out_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) di = (struct ocfs2_dinode *)di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) ocfs2_update_inode_fsync_trans(handle, inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) out_commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) out_inode_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) brelse(di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) ocfs2_inode_unlock(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) out_rw_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) ocfs2_rw_unlock(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) struct ocfs2_move_extents range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) struct ocfs2_move_extents_context *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (!argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) status = mnt_want_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) status = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) goto out_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) status = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) goto out_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) context = kzalloc(sizeof(struct ocfs2_move_extents_context), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (!context) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) goto out_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) context->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) context->file = filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (copy_from_user(&range, argp, sizeof(range))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) status = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) if (range.me_start > i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (range.me_start + range.me_len > i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) range.me_len = i_size_read(inode) - range.me_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) context->range = ⦥
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) context->auto_defrag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) * ok, the default theshold for the defragmentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) * is 1M, since our maximum clustersize was 1M also.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) * any thought?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (!range.me_threshold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) range.me_threshold = 1024 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (range.me_threshold > i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) range.me_threshold = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) context->partial = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * first best-effort attempt to validate and adjust the goal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * (physical address in block), while it can't guarantee later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * operation can succeed all the time since global_bitmap may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * change a bit over time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) status = ocfs2_validate_and_adjust_move_goal(inode, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) goto out_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) status = ocfs2_move_extents(context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) out_copy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) * movement/defragmentation may end up being partially completed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * that's the reason why we need to return userspace the finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * length and new_offset even if failure happens somewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (copy_to_user(argp, &range, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) status = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) kfree(context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) out_drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) mnt_drop_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }