Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * move_extents.c
0004  *
0005  * Copyright (C) 2011 Oracle.  All rights reserved.
0006  */
0007 #include <linux/fs.h>
0008 #include <linux/types.h>
0009 #include <linux/mount.h>
0010 #include <linux/swap.h>
0011 
0012 #include <cluster/masklog.h>
0013 
0014 #include "ocfs2.h"
0015 #include "ocfs2_ioctl.h"
0016 
0017 #include "alloc.h"
0018 #include "localalloc.h"
0019 #include "aops.h"
0020 #include "dlmglue.h"
0021 #include "extent_map.h"
0022 #include "inode.h"
0023 #include "journal.h"
0024 #include "suballoc.h"
0025 #include "uptodate.h"
0026 #include "super.h"
0027 #include "dir.h"
0028 #include "buffer_head_io.h"
0029 #include "sysfile.h"
0030 #include "refcounttree.h"
0031 #include "move_extents.h"
0032 
0033 struct ocfs2_move_extents_context {
0034     struct inode *inode;
0035     struct file *file;
0036     int auto_defrag;
0037     int partial;
0038     int credits;
0039     u32 new_phys_cpos;
0040     u32 clusters_moved;
0041     u64 refcount_loc;
0042     struct ocfs2_move_extents *range;
0043     struct ocfs2_extent_tree et;
0044     struct ocfs2_alloc_context *meta_ac;
0045     struct ocfs2_alloc_context *data_ac;
0046     struct ocfs2_cached_dealloc_ctxt dealloc;
0047 };
0048 
0049 static int __ocfs2_move_extent(handle_t *handle,
0050                    struct ocfs2_move_extents_context *context,
0051                    u32 cpos, u32 len, u32 p_cpos, u32 new_p_cpos,
0052                    int ext_flags)
0053 {
0054     int ret = 0, index;
0055     struct inode *inode = context->inode;
0056     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0057     struct ocfs2_extent_rec *rec, replace_rec;
0058     struct ocfs2_path *path = NULL;
0059     struct ocfs2_extent_list *el;
0060     u64 ino = ocfs2_metadata_cache_owner(context->et.et_ci);
0061     u64 old_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cpos);
0062 
0063     ret = ocfs2_duplicate_clusters_by_page(handle, inode, cpos,
0064                            p_cpos, new_p_cpos, len);
0065     if (ret) {
0066         mlog_errno(ret);
0067         goto out;
0068     }
0069 
0070     memset(&replace_rec, 0, sizeof(replace_rec));
0071     replace_rec.e_cpos = cpu_to_le32(cpos);
0072     replace_rec.e_leaf_clusters = cpu_to_le16(len);
0073     replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
0074                                    new_p_cpos));
0075 
0076     path = ocfs2_new_path_from_et(&context->et);
0077     if (!path) {
0078         ret = -ENOMEM;
0079         mlog_errno(ret);
0080         goto out;
0081     }
0082 
0083     ret = ocfs2_find_path(INODE_CACHE(inode), path, cpos);
0084     if (ret) {
0085         mlog_errno(ret);
0086         goto out;
0087     }
0088 
0089     el = path_leaf_el(path);
0090 
0091     index = ocfs2_search_extent_list(el, cpos);
0092     if (index == -1) {
0093         ret = ocfs2_error(inode->i_sb,
0094                   "Inode %llu has an extent at cpos %u which can no longer be found\n",
0095                   (unsigned long long)ino, cpos);
0096         goto out;
0097     }
0098 
0099     rec = &el->l_recs[index];
0100 
0101     BUG_ON(ext_flags != rec->e_flags);
0102     /*
0103      * after moving/defraging to new location, the extent is not going
0104      * to be refcounted anymore.
0105      */
0106     replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED;
0107 
0108     ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
0109                       context->et.et_root_bh,
0110                       OCFS2_JOURNAL_ACCESS_WRITE);
0111     if (ret) {
0112         mlog_errno(ret);
0113         goto out;
0114     }
0115 
0116     ret = ocfs2_split_extent(handle, &context->et, path, index,
0117                  &replace_rec, context->meta_ac,
0118                  &context->dealloc);
0119     if (ret) {
0120         mlog_errno(ret);
0121         goto out;
0122     }
0123 
0124     ocfs2_journal_dirty(handle, context->et.et_root_bh);
0125 
0126     context->new_phys_cpos = new_p_cpos;
0127 
0128     /*
0129      * need I to append truncate log for old clusters?
0130      */
0131     if (old_blkno) {
0132         if (ext_flags & OCFS2_EXT_REFCOUNTED)
0133             ret = ocfs2_decrease_refcount(inode, handle,
0134                     ocfs2_blocks_to_clusters(osb->sb,
0135                                  old_blkno),
0136                     len, context->meta_ac,
0137                     &context->dealloc, 1);
0138         else
0139             ret = ocfs2_truncate_log_append(osb, handle,
0140                             old_blkno, len);
0141     }
0142 
0143     ocfs2_update_inode_fsync_trans(handle, inode, 0);
0144 out:
0145     ocfs2_free_path(path);
0146     return ret;
0147 }
0148 
0149 /*
0150  * lock allocator, and reserve appropriate number of bits for
0151  * meta blocks.
0152  */
0153 static int ocfs2_lock_meta_allocator_move_extents(struct inode *inode,
0154                     struct ocfs2_extent_tree *et,
0155                     u32 clusters_to_move,
0156                     u32 extents_to_split,
0157                     struct ocfs2_alloc_context **meta_ac,
0158                     int extra_blocks,
0159                     int *credits)
0160 {
0161     int ret, num_free_extents;
0162     unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move;
0163     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0164 
0165     num_free_extents = ocfs2_num_free_extents(et);
0166     if (num_free_extents < 0) {
0167         ret = num_free_extents;
0168         mlog_errno(ret);
0169         goto out;
0170     }
0171 
0172     if (!num_free_extents ||
0173         (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
0174         extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
0175 
0176     ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac);
0177     if (ret) {
0178         mlog_errno(ret);
0179         goto out;
0180     }
0181 
0182 
0183     *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el);
0184 
0185     mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
0186          extra_blocks, clusters_to_move, *credits);
0187 out:
0188     if (ret) {
0189         if (*meta_ac) {
0190             ocfs2_free_alloc_context(*meta_ac);
0191             *meta_ac = NULL;
0192         }
0193     }
0194 
0195     return ret;
0196 }
0197 
0198 /*
0199  * Using one journal handle to guarantee the data consistency in case
0200  * crash happens anywhere.
0201  *
0202  *  XXX: defrag can end up with finishing partial extent as requested,
0203  * due to not enough contiguous clusters can be found in allocator.
0204  */
0205 static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
0206                    u32 cpos, u32 phys_cpos, u32 *len, int ext_flags)
0207 {
0208     int ret, credits = 0, extra_blocks = 0, partial = context->partial;
0209     handle_t *handle;
0210     struct inode *inode = context->inode;
0211     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0212     struct inode *tl_inode = osb->osb_tl_inode;
0213     struct ocfs2_refcount_tree *ref_tree = NULL;
0214     u32 new_phys_cpos, new_len;
0215     u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
0216     int need_free = 0;
0217 
0218     if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) {
0219         BUG_ON(!ocfs2_is_refcount_inode(inode));
0220         BUG_ON(!context->refcount_loc);
0221 
0222         ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
0223                            &ref_tree, NULL);
0224         if (ret) {
0225             mlog_errno(ret);
0226             return ret;
0227         }
0228 
0229         ret = ocfs2_prepare_refcount_change_for_del(inode,
0230                             context->refcount_loc,
0231                             phys_blkno,
0232                             *len,
0233                             &credits,
0234                             &extra_blocks);
0235         if (ret) {
0236             mlog_errno(ret);
0237             goto out;
0238         }
0239     }
0240 
0241     ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et,
0242                         *len, 1,
0243                         &context->meta_ac,
0244                         extra_blocks, &credits);
0245     if (ret) {
0246         mlog_errno(ret);
0247         goto out;
0248     }
0249 
0250     /*
0251      * should be using allocation reservation strategy there?
0252      *
0253      * if (context->data_ac)
0254      *  context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
0255      */
0256 
0257     inode_lock(tl_inode);
0258 
0259     if (ocfs2_truncate_log_needs_flush(osb)) {
0260         ret = __ocfs2_flush_truncate_log(osb);
0261         if (ret < 0) {
0262             mlog_errno(ret);
0263             goto out_unlock_mutex;
0264         }
0265     }
0266 
0267     /*
0268      * Make sure ocfs2_reserve_cluster is called after
0269      * __ocfs2_flush_truncate_log, otherwise, dead lock may happen.
0270      *
0271      * If ocfs2_reserve_cluster is called
0272      * before __ocfs2_flush_truncate_log, dead lock on global bitmap
0273      * may happen.
0274      *
0275      */
0276     ret = ocfs2_reserve_clusters(osb, *len, &context->data_ac);
0277     if (ret) {
0278         mlog_errno(ret);
0279         goto out_unlock_mutex;
0280     }
0281 
0282     handle = ocfs2_start_trans(osb, credits);
0283     if (IS_ERR(handle)) {
0284         ret = PTR_ERR(handle);
0285         mlog_errno(ret);
0286         goto out_unlock_mutex;
0287     }
0288 
0289     ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len,
0290                      &new_phys_cpos, &new_len);
0291     if (ret) {
0292         mlog_errno(ret);
0293         goto out_commit;
0294     }
0295 
0296     /*
0297      * allowing partial extent moving is kind of 'pros and cons', it makes
0298      * whole defragmentation less likely to fail, on the contrary, the bad
0299      * thing is it may make the fs even more fragmented after moving, let
0300      * userspace make a good decision here.
0301      */
0302     if (new_len != *len) {
0303         mlog(0, "len_claimed: %u, len: %u\n", new_len, *len);
0304         if (!partial) {
0305             context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
0306             ret = -ENOSPC;
0307             need_free = 1;
0308             goto out_commit;
0309         }
0310     }
0311 
0312     mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
0313          phys_cpos, new_phys_cpos);
0314 
0315     ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos,
0316                   new_phys_cpos, ext_flags);
0317     if (ret)
0318         mlog_errno(ret);
0319 
0320     if (partial && (new_len != *len))
0321         *len = new_len;
0322 
0323     /*
0324      * Here we should write the new page out first if we are
0325      * in write-back mode.
0326      */
0327     ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len);
0328     if (ret)
0329         mlog_errno(ret);
0330 
0331 out_commit:
0332     if (need_free && context->data_ac) {
0333         struct ocfs2_alloc_context *data_ac = context->data_ac;
0334 
0335         if (context->data_ac->ac_which == OCFS2_AC_USE_LOCAL)
0336             ocfs2_free_local_alloc_bits(osb, handle, data_ac,
0337                     new_phys_cpos, new_len);
0338         else
0339             ocfs2_free_clusters(handle,
0340                     data_ac->ac_inode,
0341                     data_ac->ac_bh,
0342                     ocfs2_clusters_to_blocks(osb->sb, new_phys_cpos),
0343                     new_len);
0344     }
0345 
0346     ocfs2_commit_trans(osb, handle);
0347 
0348 out_unlock_mutex:
0349     inode_unlock(tl_inode);
0350 
0351     if (context->data_ac) {
0352         ocfs2_free_alloc_context(context->data_ac);
0353         context->data_ac = NULL;
0354     }
0355 
0356     if (context->meta_ac) {
0357         ocfs2_free_alloc_context(context->meta_ac);
0358         context->meta_ac = NULL;
0359     }
0360 
0361 out:
0362     if (ref_tree)
0363         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
0364 
0365     return ret;
0366 }
0367 
0368 /*
0369  * find the victim alloc group, where #blkno fits.
0370  */
0371 static int ocfs2_find_victim_alloc_group(struct inode *inode,
0372                      u64 vict_blkno,
0373                      int type, int slot,
0374                      int *vict_bit,
0375                      struct buffer_head **ret_bh)
0376 {
0377     int ret, i, bits_per_unit = 0;
0378     u64 blkno;
0379     char namebuf[40];
0380 
0381     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0382     struct buffer_head *ac_bh = NULL, *gd_bh = NULL;
0383     struct ocfs2_chain_list *cl;
0384     struct ocfs2_chain_rec *rec;
0385     struct ocfs2_dinode *ac_dinode;
0386     struct ocfs2_group_desc *bg;
0387 
0388     ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot);
0389     ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
0390                      strlen(namebuf), &blkno);
0391     if (ret) {
0392         ret = -ENOENT;
0393         goto out;
0394     }
0395 
0396     ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh);
0397     if (ret) {
0398         mlog_errno(ret);
0399         goto out;
0400     }
0401 
0402     ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data;
0403     cl = &(ac_dinode->id2.i_chain);
0404     rec = &(cl->cl_recs[0]);
0405 
0406     if (type == GLOBAL_BITMAP_SYSTEM_INODE)
0407         bits_per_unit = osb->s_clustersize_bits -
0408                     inode->i_sb->s_blocksize_bits;
0409     /*
0410      * 'vict_blkno' was out of the valid range.
0411      */
0412     if ((vict_blkno < le64_to_cpu(rec->c_blkno)) ||
0413         (vict_blkno >= ((u64)le32_to_cpu(ac_dinode->id1.bitmap1.i_total) <<
0414                 bits_per_unit))) {
0415         ret = -EINVAL;
0416         goto out;
0417     }
0418 
0419     for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
0420 
0421         rec = &(cl->cl_recs[i]);
0422         if (!rec)
0423             continue;
0424 
0425         bg = NULL;
0426 
0427         do {
0428             if (!bg)
0429                 blkno = le64_to_cpu(rec->c_blkno);
0430             else
0431                 blkno = le64_to_cpu(bg->bg_next_group);
0432 
0433             if (gd_bh) {
0434                 brelse(gd_bh);
0435                 gd_bh = NULL;
0436             }
0437 
0438             ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh);
0439             if (ret) {
0440                 mlog_errno(ret);
0441                 goto out;
0442             }
0443 
0444             bg = (struct ocfs2_group_desc *)gd_bh->b_data;
0445 
0446             if (vict_blkno < (le64_to_cpu(bg->bg_blkno) +
0447                         le16_to_cpu(bg->bg_bits))) {
0448 
0449                 *ret_bh = gd_bh;
0450                 *vict_bit = (vict_blkno - blkno) >>
0451                             bits_per_unit;
0452                 mlog(0, "find the victim group: #%llu, "
0453                      "total_bits: %u, vict_bit: %u\n",
0454                      blkno, le16_to_cpu(bg->bg_bits),
0455                      *vict_bit);
0456                 goto out;
0457             }
0458 
0459         } while (le64_to_cpu(bg->bg_next_group));
0460     }
0461 
0462     ret = -EINVAL;
0463 out:
0464     brelse(ac_bh);
0465 
0466     /*
0467      * caller has to release the gd_bh properly.
0468      */
0469     return ret;
0470 }
0471 
0472 /*
0473  * XXX: helper to validate and adjust moving goal.
0474  */
0475 static int ocfs2_validate_and_adjust_move_goal(struct inode *inode,
0476                            struct ocfs2_move_extents *range)
0477 {
0478     int ret, goal_bit = 0;
0479 
0480     struct buffer_head *gd_bh = NULL;
0481     struct ocfs2_group_desc *bg;
0482     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0483     int c_to_b = 1 << (osb->s_clustersize_bits -
0484                     inode->i_sb->s_blocksize_bits);
0485 
0486     /*
0487      * make goal become cluster aligned.
0488      */
0489     range->me_goal = ocfs2_block_to_cluster_start(inode->i_sb,
0490                               range->me_goal);
0491     /*
0492      * validate goal sits within global_bitmap, and return the victim
0493      * group desc
0494      */
0495     ret = ocfs2_find_victim_alloc_group(inode, range->me_goal,
0496                         GLOBAL_BITMAP_SYSTEM_INODE,
0497                         OCFS2_INVALID_SLOT,
0498                         &goal_bit, &gd_bh);
0499     if (ret)
0500         goto out;
0501 
0502     bg = (struct ocfs2_group_desc *)gd_bh->b_data;
0503 
0504     /*
0505      * moving goal is not allowd to start with a group desc blok(#0 blk)
0506      * let's compromise to the latter cluster.
0507      */
0508     if (range->me_goal == le64_to_cpu(bg->bg_blkno))
0509         range->me_goal += c_to_b;
0510 
0511     /*
0512      * movement is not gonna cross two groups.
0513      */
0514     if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize <
0515                                 range->me_len) {
0516         ret = -EINVAL;
0517         goto out;
0518     }
0519     /*
0520      * more exact validations/adjustments will be performed later during
0521      * moving operation for each extent range.
0522      */
0523     mlog(0, "extents get ready to be moved to #%llu block\n",
0524          range->me_goal);
0525 
0526 out:
0527     brelse(gd_bh);
0528 
0529     return ret;
0530 }
0531 
0532 static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh,
0533                     int *goal_bit, u32 move_len, u32 max_hop,
0534                     u32 *phys_cpos)
0535 {
0536     int i, used, last_free_bits = 0, base_bit = *goal_bit;
0537     struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
0538     u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
0539                          le64_to_cpu(gd->bg_blkno));
0540 
0541     for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) {
0542 
0543         used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap);
0544         if (used) {
0545             /*
0546              * we even tried searching the free chunk by jumping
0547              * a 'max_hop' distance, but still failed.
0548              */
0549             if ((i - base_bit) > max_hop) {
0550                 *phys_cpos = 0;
0551                 break;
0552             }
0553 
0554             if (last_free_bits)
0555                 last_free_bits = 0;
0556 
0557             continue;
0558         } else
0559             last_free_bits++;
0560 
0561         if (last_free_bits == move_len) {
0562             *goal_bit = i;
0563             *phys_cpos = base_cpos + i;
0564             break;
0565         }
0566     }
0567 
0568     mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
0569 }
0570 
0571 static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
0572                  u32 cpos, u32 phys_cpos, u32 *new_phys_cpos,
0573                  u32 len, int ext_flags)
0574 {
0575     int ret, credits = 0, extra_blocks = 0, goal_bit = 0;
0576     handle_t *handle;
0577     struct inode *inode = context->inode;
0578     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0579     struct inode *tl_inode = osb->osb_tl_inode;
0580     struct inode *gb_inode = NULL;
0581     struct buffer_head *gb_bh = NULL;
0582     struct buffer_head *gd_bh = NULL;
0583     struct ocfs2_group_desc *gd;
0584     struct ocfs2_refcount_tree *ref_tree = NULL;
0585     u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb,
0586                             context->range->me_threshold);
0587     u64 phys_blkno, new_phys_blkno;
0588 
0589     phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
0590 
0591     if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
0592         BUG_ON(!ocfs2_is_refcount_inode(inode));
0593         BUG_ON(!context->refcount_loc);
0594 
0595         ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
0596                            &ref_tree, NULL);
0597         if (ret) {
0598             mlog_errno(ret);
0599             return ret;
0600         }
0601 
0602         ret = ocfs2_prepare_refcount_change_for_del(inode,
0603                             context->refcount_loc,
0604                             phys_blkno,
0605                             len,
0606                             &credits,
0607                             &extra_blocks);
0608         if (ret) {
0609             mlog_errno(ret);
0610             goto out;
0611         }
0612     }
0613 
0614     ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et,
0615                         len, 1,
0616                         &context->meta_ac,
0617                         extra_blocks, &credits);
0618     if (ret) {
0619         mlog_errno(ret);
0620         goto out;
0621     }
0622 
0623     /*
0624      * need to count 2 extra credits for global_bitmap inode and
0625      * group descriptor.
0626      */
0627     credits += OCFS2_INODE_UPDATE_CREDITS + 1;
0628 
0629     /*
0630      * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
0631      * logic, while we still need to lock the global_bitmap.
0632      */
0633     gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
0634                            OCFS2_INVALID_SLOT);
0635     if (!gb_inode) {
0636         mlog(ML_ERROR, "unable to get global_bitmap inode\n");
0637         ret = -EIO;
0638         goto out;
0639     }
0640 
0641     inode_lock(gb_inode);
0642 
0643     ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
0644     if (ret) {
0645         mlog_errno(ret);
0646         goto out_unlock_gb_mutex;
0647     }
0648 
0649     inode_lock(tl_inode);
0650 
0651     handle = ocfs2_start_trans(osb, credits);
0652     if (IS_ERR(handle)) {
0653         ret = PTR_ERR(handle);
0654         mlog_errno(ret);
0655         goto out_unlock_tl_inode;
0656     }
0657 
0658     new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
0659     ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno,
0660                         GLOBAL_BITMAP_SYSTEM_INODE,
0661                         OCFS2_INVALID_SLOT,
0662                         &goal_bit, &gd_bh);
0663     if (ret) {
0664         mlog_errno(ret);
0665         goto out_commit;
0666     }
0667 
0668     /*
0669      * probe the victim cluster group to find a proper
0670      * region to fit wanted movement, it even will perfrom
0671      * a best-effort attempt by compromising to a threshold
0672      * around the goal.
0673      */
0674     ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop,
0675                 new_phys_cpos);
0676     if (!*new_phys_cpos) {
0677         ret = -ENOSPC;
0678         goto out_commit;
0679     }
0680 
0681     ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
0682                   *new_phys_cpos, ext_flags);
0683     if (ret) {
0684         mlog_errno(ret);
0685         goto out_commit;
0686     }
0687 
0688     gd = (struct ocfs2_group_desc *)gd_bh->b_data;
0689     ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len,
0690                            le16_to_cpu(gd->bg_chain));
0691     if (ret) {
0692         mlog_errno(ret);
0693         goto out_commit;
0694     }
0695 
0696     ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
0697                      goal_bit, len);
0698     if (ret) {
0699         ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len,
0700                            le16_to_cpu(gd->bg_chain));
0701         mlog_errno(ret);
0702     }
0703 
0704     /*
0705      * Here we should write the new page out first if we are
0706      * in write-back mode.
0707      */
0708     ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len);
0709     if (ret)
0710         mlog_errno(ret);
0711 
0712 out_commit:
0713     ocfs2_commit_trans(osb, handle);
0714     brelse(gd_bh);
0715 
0716 out_unlock_tl_inode:
0717     inode_unlock(tl_inode);
0718 
0719     ocfs2_inode_unlock(gb_inode, 1);
0720 out_unlock_gb_mutex:
0721     inode_unlock(gb_inode);
0722     brelse(gb_bh);
0723     iput(gb_inode);
0724 
0725 out:
0726     if (context->meta_ac) {
0727         ocfs2_free_alloc_context(context->meta_ac);
0728         context->meta_ac = NULL;
0729     }
0730 
0731     if (ref_tree)
0732         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
0733 
0734     return ret;
0735 }
0736 
0737 /*
0738  * Helper to calculate the defraging length in one run according to threshold.
0739  */
0740 static void ocfs2_calc_extent_defrag_len(u32 *alloc_size, u32 *len_defraged,
0741                      u32 threshold, int *skip)
0742 {
0743     if ((*alloc_size + *len_defraged) < threshold) {
0744         /*
0745          * proceed defragmentation until we meet the thresh
0746          */
0747         *len_defraged += *alloc_size;
0748     } else if (*len_defraged == 0) {
0749         /*
0750          * XXX: skip a large extent.
0751          */
0752         *skip = 1;
0753     } else {
0754         /*
0755          * split this extent to coalesce with former pieces as
0756          * to reach the threshold.
0757          *
0758          * we're done here with one cycle of defragmentation
0759          * in a size of 'thresh', resetting 'len_defraged'
0760          * forces a new defragmentation.
0761          */
0762         *alloc_size = threshold - *len_defraged;
0763         *len_defraged = 0;
0764     }
0765 }
0766 
0767 static int __ocfs2_move_extents_range(struct buffer_head *di_bh,
0768                 struct ocfs2_move_extents_context *context)
0769 {
0770     int ret = 0, flags, do_defrag, skip = 0;
0771     u32 cpos, phys_cpos, move_start, len_to_move, alloc_size;
0772     u32 len_defraged = 0, defrag_thresh = 0, new_phys_cpos = 0;
0773 
0774     struct inode *inode = context->inode;
0775     struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
0776     struct ocfs2_move_extents *range = context->range;
0777     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0778 
0779     if ((i_size_read(inode) == 0) || (range->me_len == 0))
0780         return 0;
0781 
0782     if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
0783         return 0;
0784 
0785     context->refcount_loc = le64_to_cpu(di->i_refcount_loc);
0786 
0787     ocfs2_init_dinode_extent_tree(&context->et, INODE_CACHE(inode), di_bh);
0788     ocfs2_init_dealloc_ctxt(&context->dealloc);
0789 
0790     /*
0791      * TO-DO XXX:
0792      *
0793      * - xattr extents.
0794      */
0795 
0796     do_defrag = context->auto_defrag;
0797 
0798     /*
0799      * extents moving happens in unit of clusters, for the sake
0800      * of simplicity, we may ignore two clusters where 'byte_start'
0801      * and 'byte_start + len' were within.
0802      */
0803     move_start = ocfs2_clusters_for_bytes(osb->sb, range->me_start);
0804     len_to_move = (range->me_start + range->me_len) >>
0805                         osb->s_clustersize_bits;
0806     if (len_to_move >= move_start)
0807         len_to_move -= move_start;
0808     else
0809         len_to_move = 0;
0810 
0811     if (do_defrag) {
0812         defrag_thresh = range->me_threshold >> osb->s_clustersize_bits;
0813         if (defrag_thresh <= 1)
0814             goto done;
0815     } else
0816         new_phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
0817                              range->me_goal);
0818 
0819     mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u, "
0820          "thresh: %u\n",
0821          (unsigned long long)OCFS2_I(inode)->ip_blkno,
0822          (unsigned long long)range->me_start,
0823          (unsigned long long)range->me_len,
0824          move_start, len_to_move, defrag_thresh);
0825 
0826     cpos = move_start;
0827     while (len_to_move) {
0828         ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &alloc_size,
0829                      &flags);
0830         if (ret) {
0831             mlog_errno(ret);
0832             goto out;
0833         }
0834 
0835         if (alloc_size > len_to_move)
0836             alloc_size = len_to_move;
0837 
0838         /*
0839          * XXX: how to deal with a hole:
0840          *
0841          * - skip the hole of course
0842          * - force a new defragmentation
0843          */
0844         if (!phys_cpos) {
0845             if (do_defrag)
0846                 len_defraged = 0;
0847 
0848             goto next;
0849         }
0850 
0851         if (do_defrag) {
0852             ocfs2_calc_extent_defrag_len(&alloc_size, &len_defraged,
0853                              defrag_thresh, &skip);
0854             /*
0855              * skip large extents
0856              */
0857             if (skip) {
0858                 skip = 0;
0859                 goto next;
0860             }
0861 
0862             mlog(0, "#Defrag: cpos: %u, phys_cpos: %u, "
0863                  "alloc_size: %u, len_defraged: %u\n",
0864                  cpos, phys_cpos, alloc_size, len_defraged);
0865 
0866             ret = ocfs2_defrag_extent(context, cpos, phys_cpos,
0867                           &alloc_size, flags);
0868         } else {
0869             ret = ocfs2_move_extent(context, cpos, phys_cpos,
0870                         &new_phys_cpos, alloc_size,
0871                         flags);
0872 
0873             new_phys_cpos += alloc_size;
0874         }
0875 
0876         if (ret < 0) {
0877             mlog_errno(ret);
0878             goto out;
0879         }
0880 
0881         context->clusters_moved += alloc_size;
0882 next:
0883         cpos += alloc_size;
0884         len_to_move -= alloc_size;
0885     }
0886 
0887 done:
0888     range->me_flags |= OCFS2_MOVE_EXT_FL_COMPLETE;
0889 
0890 out:
0891     range->me_moved_len = ocfs2_clusters_to_bytes(osb->sb,
0892                               context->clusters_moved);
0893     range->me_new_offset = ocfs2_clusters_to_bytes(osb->sb,
0894                                context->new_phys_cpos);
0895 
0896     ocfs2_schedule_truncate_log_flush(osb, 1);
0897     ocfs2_run_deallocs(osb, &context->dealloc);
0898 
0899     return ret;
0900 }
0901 
0902 static int ocfs2_move_extents(struct ocfs2_move_extents_context *context)
0903 {
0904     int status;
0905     handle_t *handle;
0906     struct inode *inode = context->inode;
0907     struct ocfs2_dinode *di;
0908     struct buffer_head *di_bh = NULL;
0909     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0910 
0911     if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
0912         return -EROFS;
0913 
0914     inode_lock(inode);
0915 
0916     /*
0917      * This prevents concurrent writes from other nodes
0918      */
0919     status = ocfs2_rw_lock(inode, 1);
0920     if (status) {
0921         mlog_errno(status);
0922         goto out;
0923     }
0924 
0925     status = ocfs2_inode_lock(inode, &di_bh, 1);
0926     if (status) {
0927         mlog_errno(status);
0928         goto out_rw_unlock;
0929     }
0930 
0931     /*
0932      * rememer ip_xattr_sem also needs to be held if necessary
0933      */
0934     down_write(&OCFS2_I(inode)->ip_alloc_sem);
0935 
0936     status = __ocfs2_move_extents_range(di_bh, context);
0937 
0938     up_write(&OCFS2_I(inode)->ip_alloc_sem);
0939     if (status) {
0940         mlog_errno(status);
0941         goto out_inode_unlock;
0942     }
0943 
0944     /*
0945      * We update ctime for these changes
0946      */
0947     handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
0948     if (IS_ERR(handle)) {
0949         status = PTR_ERR(handle);
0950         mlog_errno(status);
0951         goto out_inode_unlock;
0952     }
0953 
0954     status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
0955                      OCFS2_JOURNAL_ACCESS_WRITE);
0956     if (status) {
0957         mlog_errno(status);
0958         goto out_commit;
0959     }
0960 
0961     di = (struct ocfs2_dinode *)di_bh->b_data;
0962     inode->i_ctime = current_time(inode);
0963     di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
0964     di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
0965     ocfs2_update_inode_fsync_trans(handle, inode, 0);
0966 
0967     ocfs2_journal_dirty(handle, di_bh);
0968 
0969 out_commit:
0970     ocfs2_commit_trans(osb, handle);
0971 
0972 out_inode_unlock:
0973     brelse(di_bh);
0974     ocfs2_inode_unlock(inode, 1);
0975 out_rw_unlock:
0976     ocfs2_rw_unlock(inode, 1);
0977 out:
0978     inode_unlock(inode);
0979 
0980     return status;
0981 }
0982 
0983 int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp)
0984 {
0985     int status;
0986 
0987     struct inode *inode = file_inode(filp);
0988     struct ocfs2_move_extents range;
0989     struct ocfs2_move_extents_context *context;
0990 
0991     if (!argp)
0992         return -EINVAL;
0993 
0994     status = mnt_want_write_file(filp);
0995     if (status)
0996         return status;
0997 
0998     if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) {
0999         status = -EPERM;
1000         goto out_drop;
1001     }
1002 
1003     if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1004         status = -EPERM;
1005         goto out_drop;
1006     }
1007 
1008     context = kzalloc(sizeof(struct ocfs2_move_extents_context), GFP_NOFS);
1009     if (!context) {
1010         status = -ENOMEM;
1011         mlog_errno(status);
1012         goto out_drop;
1013     }
1014 
1015     context->inode = inode;
1016     context->file = filp;
1017 
1018     if (copy_from_user(&range, argp, sizeof(range))) {
1019         status = -EFAULT;
1020         goto out_free;
1021     }
1022 
1023     if (range.me_start > i_size_read(inode)) {
1024         status = -EINVAL;
1025         goto out_free;
1026     }
1027 
1028     if (range.me_start + range.me_len > i_size_read(inode))
1029             range.me_len = i_size_read(inode) - range.me_start;
1030 
1031     context->range = &range;
1032 
1033     if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) {
1034         context->auto_defrag = 1;
1035         /*
1036          * ok, the default theshold for the defragmentation
1037          * is 1M, since our maximum clustersize was 1M also.
1038          * any thought?
1039          */
1040         if (!range.me_threshold)
1041             range.me_threshold = 1024 * 1024;
1042 
1043         if (range.me_threshold > i_size_read(inode))
1044             range.me_threshold = i_size_read(inode);
1045 
1046         if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG)
1047             context->partial = 1;
1048     } else {
1049         /*
1050          * first best-effort attempt to validate and adjust the goal
1051          * (physical address in block), while it can't guarantee later
1052          * operation can succeed all the time since global_bitmap may
1053          * change a bit over time.
1054          */
1055 
1056         status = ocfs2_validate_and_adjust_move_goal(inode, &range);
1057         if (status)
1058             goto out_copy;
1059     }
1060 
1061     status = ocfs2_move_extents(context);
1062     if (status)
1063         mlog_errno(status);
1064 out_copy:
1065     /*
1066      * movement/defragmentation may end up being partially completed,
1067      * that's the reason why we need to return userspace the finished
1068      * length and new_offset even if failure happens somewhere.
1069      */
1070     if (copy_to_user(argp, &range, sizeof(range)))
1071         status = -EFAULT;
1072 
1073 out_free:
1074     kfree(context);
1075 out_drop:
1076     mnt_drop_write_file(filp);
1077 
1078     return status;
1079 }