blob: 3f1685d7d43bf37a26161bfaeaaeefef3831820b [file] [log] [blame]
Tristan Ye028ba5df2011-05-24 16:42:09 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * move_extents.c
5 *
6 * Copyright (C) 2011 Oracle. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17#include <linux/fs.h>
18#include <linux/types.h>
19#include <linux/mount.h>
20#include <linux/swap.h>
21
22#include <cluster/masklog.h>
23
24#include "ocfs2.h"
25#include "ocfs2_ioctl.h"
26
27#include "alloc.h"
Larry Chen6194ae42018-11-02 15:48:27 -070028#include "localalloc.h"
Tristan Ye028ba5df2011-05-24 16:42:09 +080029#include "aops.h"
30#include "dlmglue.h"
31#include "extent_map.h"
32#include "inode.h"
33#include "journal.h"
34#include "suballoc.h"
35#include "uptodate.h"
36#include "super.h"
37#include "dir.h"
38#include "buffer_head_io.h"
39#include "sysfile.h"
Tristan Ye028ba5df2011-05-24 16:42:09 +080040#include "refcounttree.h"
41#include "move_extents.h"
42
43struct ocfs2_move_extents_context {
44 struct inode *inode;
45 struct file *file;
46 int auto_defrag;
Tristan Ye4dfa66b2011-05-25 14:30:36 +080047 int partial;
Tristan Ye028ba5df2011-05-24 16:42:09 +080048 int credits;
49 u32 new_phys_cpos;
50 u32 clusters_moved;
51 u64 refcount_loc;
52 struct ocfs2_move_extents *range;
53 struct ocfs2_extent_tree et;
54 struct ocfs2_alloc_context *meta_ac;
55 struct ocfs2_alloc_context *data_ac;
56 struct ocfs2_cached_dealloc_ctxt dealloc;
57};
Tristan Yede474ee2011-03-18 14:35:32 +080058
Tristan Ye8f603e52011-03-18 14:35:33 +080059static int __ocfs2_move_extent(handle_t *handle,
60 struct ocfs2_move_extents_context *context,
61 u32 cpos, u32 len, u32 p_cpos, u32 new_p_cpos,
62 int ext_flags)
63{
64 int ret = 0, index;
65 struct inode *inode = context->inode;
66 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
67 struct ocfs2_extent_rec *rec, replace_rec;
68 struct ocfs2_path *path = NULL;
69 struct ocfs2_extent_list *el;
70 u64 ino = ocfs2_metadata_cache_owner(context->et.et_ci);
71 u64 old_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cpos);
72
Tiger Yangc7dd3392013-08-13 16:00:58 -070073 ret = ocfs2_duplicate_clusters_by_page(handle, inode, cpos,
Tristan Ye8f603e52011-03-18 14:35:33 +080074 p_cpos, new_p_cpos, len);
75 if (ret) {
76 mlog_errno(ret);
77 goto out;
78 }
79
80 memset(&replace_rec, 0, sizeof(replace_rec));
81 replace_rec.e_cpos = cpu_to_le32(cpos);
82 replace_rec.e_leaf_clusters = cpu_to_le16(len);
83 replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
84 new_p_cpos));
85
86 path = ocfs2_new_path_from_et(&context->et);
87 if (!path) {
88 ret = -ENOMEM;
89 mlog_errno(ret);
90 goto out;
91 }
92
93 ret = ocfs2_find_path(INODE_CACHE(inode), path, cpos);
94 if (ret) {
95 mlog_errno(ret);
96 goto out;
97 }
98
99 el = path_leaf_el(path);
100
101 index = ocfs2_search_extent_list(el, cpos);
Yingtai Xie981035b2014-08-06 16:03:54 -0700102 if (index == -1) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700103 ret = ocfs2_error(inode->i_sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700104 "Inode %llu has an extent at cpos %u which can no longer be found\n",
105 (unsigned long long)ino, cpos);
Tristan Ye8f603e52011-03-18 14:35:33 +0800106 goto out;
107 }
108
109 rec = &el->l_recs[index];
110
111 BUG_ON(ext_flags != rec->e_flags);
112 /*
113 * after moving/defraging to new location, the extent is not going
114 * to be refcounted anymore.
115 */
116 replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED;
117
118 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
119 context->et.et_root_bh,
120 OCFS2_JOURNAL_ACCESS_WRITE);
121 if (ret) {
122 mlog_errno(ret);
123 goto out;
124 }
125
126 ret = ocfs2_split_extent(handle, &context->et, path, index,
127 &replace_rec, context->meta_ac,
128 &context->dealloc);
129 if (ret) {
130 mlog_errno(ret);
131 goto out;
132 }
133
134 ocfs2_journal_dirty(handle, context->et.et_root_bh);
135
136 context->new_phys_cpos = new_p_cpos;
137
138 /*
139 * need I to append truncate log for old clusters?
140 */
141 if (old_blkno) {
142 if (ext_flags & OCFS2_EXT_REFCOUNTED)
143 ret = ocfs2_decrease_refcount(inode, handle,
144 ocfs2_blocks_to_clusters(osb->sb,
145 old_blkno),
146 len, context->meta_ac,
147 &context->dealloc, 1);
148 else
149 ret = ocfs2_truncate_log_append(osb, handle,
150 old_blkno, len);
151 }
152
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700153 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Tristan Ye8f603e52011-03-18 14:35:33 +0800154out:
Jie Liu4704aa32013-09-11 14:19:53 -0700155 ocfs2_free_path(path);
Tristan Ye8f603e52011-03-18 14:35:33 +0800156 return ret;
157}
158
Tristan Yede474ee2011-03-18 14:35:32 +0800159/*
160 * lock allocators, and reserving appropriate number of bits for
161 * meta blocks and data clusters.
162 *
163 * in some cases, we don't need to reserve clusters, just let data_ac
164 * be NULL.
165 */
166static int ocfs2_lock_allocators_move_extents(struct inode *inode,
167 struct ocfs2_extent_tree *et,
168 u32 clusters_to_move,
169 u32 extents_to_split,
170 struct ocfs2_alloc_context **meta_ac,
171 struct ocfs2_alloc_context **data_ac,
172 int extra_blocks,
173 int *credits)
174{
175 int ret, num_free_extents;
176 unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move;
177 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
178
Jun Piao964f14a2017-09-06 16:19:11 -0700179 num_free_extents = ocfs2_num_free_extents(et);
Tristan Yede474ee2011-03-18 14:35:32 +0800180 if (num_free_extents < 0) {
181 ret = num_free_extents;
182 mlog_errno(ret);
183 goto out;
184 }
185
186 if (!num_free_extents ||
187 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
188 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
189
190 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac);
191 if (ret) {
192 mlog_errno(ret);
193 goto out;
194 }
195
196 if (data_ac) {
197 ret = ocfs2_reserve_clusters(osb, clusters_to_move, data_ac);
198 if (ret) {
199 mlog_errno(ret);
200 goto out;
201 }
202 }
203
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -0800204 *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el);
Tristan Yede474ee2011-03-18 14:35:32 +0800205
206 mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
207 extra_blocks, clusters_to_move, *credits);
208out:
209 if (ret) {
210 if (*meta_ac) {
211 ocfs2_free_alloc_context(*meta_ac);
212 *meta_ac = NULL;
213 }
214 }
215
216 return ret;
217}
Tristan Ye202ee5f2011-03-18 14:35:34 +0800218
219/*
220 * Using one journal handle to guarantee the data consistency in case
221 * crash happens anywhere.
Tristan Yedda54e72011-05-25 14:45:41 +0800222 *
223 * XXX: defrag can end up with finishing partial extent as requested,
224 * due to not enough contiguous clusters can be found in allocator.
Tristan Ye202ee5f2011-03-18 14:35:34 +0800225 */
226static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800227 u32 cpos, u32 phys_cpos, u32 *len, int ext_flags)
Tristan Ye202ee5f2011-03-18 14:35:34 +0800228{
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800229 int ret, credits = 0, extra_blocks = 0, partial = context->partial;
Tristan Ye202ee5f2011-03-18 14:35:34 +0800230 handle_t *handle;
231 struct inode *inode = context->inode;
232 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
233 struct inode *tl_inode = osb->osb_tl_inode;
234 struct ocfs2_refcount_tree *ref_tree = NULL;
235 u32 new_phys_cpos, new_len;
236 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Larry Chen6194ae42018-11-02 15:48:27 -0700237 int need_free = 0;
Tristan Ye202ee5f2011-03-18 14:35:34 +0800238
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800239 if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) {
Darrick J. Wong84e40082016-11-09 14:13:09 -0800240 BUG_ON(!ocfs2_is_refcount_inode(inode));
Tristan Ye202ee5f2011-03-18 14:35:34 +0800241 BUG_ON(!context->refcount_loc);
242
243 ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
244 &ref_tree, NULL);
245 if (ret) {
246 mlog_errno(ret);
247 return ret;
248 }
249
250 ret = ocfs2_prepare_refcount_change_for_del(inode,
251 context->refcount_loc,
252 phys_blkno,
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800253 *len,
Tristan Ye202ee5f2011-03-18 14:35:34 +0800254 &credits,
255 &extra_blocks);
256 if (ret) {
257 mlog_errno(ret);
258 goto out;
259 }
260 }
261
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800262 ret = ocfs2_lock_allocators_move_extents(inode, &context->et, *len, 1,
Tristan Ye202ee5f2011-03-18 14:35:34 +0800263 &context->meta_ac,
264 &context->data_ac,
265 extra_blocks, &credits);
266 if (ret) {
267 mlog_errno(ret);
268 goto out;
269 }
270
271 /*
272 * should be using allocation reservation strategy there?
273 *
274 * if (context->data_ac)
275 * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
276 */
277
Al Viro59551022016-01-22 15:40:57 -0500278 inode_lock(tl_inode);
Tristan Ye202ee5f2011-03-18 14:35:34 +0800279
280 if (ocfs2_truncate_log_needs_flush(osb)) {
281 ret = __ocfs2_flush_truncate_log(osb);
282 if (ret < 0) {
283 mlog_errno(ret);
284 goto out_unlock_mutex;
285 }
286 }
287
288 handle = ocfs2_start_trans(osb, credits);
289 if (IS_ERR(handle)) {
290 ret = PTR_ERR(handle);
291 mlog_errno(ret);
292 goto out_unlock_mutex;
293 }
294
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800295 ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len,
Tristan Ye202ee5f2011-03-18 14:35:34 +0800296 &new_phys_cpos, &new_len);
297 if (ret) {
298 mlog_errno(ret);
299 goto out_commit;
300 }
301
302 /*
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800303 * allowing partial extent moving is kind of 'pros and cons', it makes
304 * whole defragmentation less likely to fail, on the contrary, the bad
305 * thing is it may make the fs even more fragmented after moving, let
306 * userspace make a good decision here.
Tristan Ye202ee5f2011-03-18 14:35:34 +0800307 */
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800308 if (new_len != *len) {
309 mlog(0, "len_claimed: %u, len: %u\n", new_len, *len);
310 if (!partial) {
311 context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
312 ret = -ENOSPC;
Larry Chen6194ae42018-11-02 15:48:27 -0700313 need_free = 1;
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800314 goto out_commit;
315 }
Tristan Ye202ee5f2011-03-18 14:35:34 +0800316 }
317
318 mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
319 phys_cpos, new_phys_cpos);
320
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800321 ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos,
Tristan Ye202ee5f2011-03-18 14:35:34 +0800322 new_phys_cpos, ext_flags);
323 if (ret)
324 mlog_errno(ret);
325
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800326 if (partial && (new_len != *len))
327 *len = new_len;
328
Tristan Ye202ee5f2011-03-18 14:35:34 +0800329 /*
330 * Here we should write the new page out first if we are
331 * in write-back mode.
332 */
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800333 ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len);
Tristan Ye202ee5f2011-03-18 14:35:34 +0800334 if (ret)
335 mlog_errno(ret);
336
337out_commit:
Larry Chen6194ae42018-11-02 15:48:27 -0700338 if (need_free && context->data_ac) {
339 struct ocfs2_alloc_context *data_ac = context->data_ac;
340
341 if (context->data_ac->ac_which == OCFS2_AC_USE_LOCAL)
342 ocfs2_free_local_alloc_bits(osb, handle, data_ac,
343 new_phys_cpos, new_len);
344 else
345 ocfs2_free_clusters(handle,
346 data_ac->ac_inode,
347 data_ac->ac_bh,
348 ocfs2_clusters_to_blocks(osb->sb, new_phys_cpos),
349 new_len);
350 }
351
Tristan Ye202ee5f2011-03-18 14:35:34 +0800352 ocfs2_commit_trans(osb, handle);
353
354out_unlock_mutex:
Al Viro59551022016-01-22 15:40:57 -0500355 inode_unlock(tl_inode);
Tristan Ye202ee5f2011-03-18 14:35:34 +0800356
357 if (context->data_ac) {
358 ocfs2_free_alloc_context(context->data_ac);
359 context->data_ac = NULL;
360 }
361
362 if (context->meta_ac) {
363 ocfs2_free_alloc_context(context->meta_ac);
364 context->meta_ac = NULL;
365 }
366
367out:
368 if (ref_tree)
369 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
370
371 return ret;
372}
Tristan Ye1c06b912011-03-18 14:35:35 +0800373
374/*
375 * find the victim alloc group, where #blkno fits.
376 */
377static int ocfs2_find_victim_alloc_group(struct inode *inode,
378 u64 vict_blkno,
379 int type, int slot,
380 int *vict_bit,
381 struct buffer_head **ret_bh)
382{
Tristan Ye6aea6f52011-05-27 15:19:56 +0800383 int ret, i, bits_per_unit = 0;
Tristan Ye1c06b912011-03-18 14:35:35 +0800384 u64 blkno;
385 char namebuf[40];
386
387 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
388 struct buffer_head *ac_bh = NULL, *gd_bh = NULL;
389 struct ocfs2_chain_list *cl;
390 struct ocfs2_chain_rec *rec;
391 struct ocfs2_dinode *ac_dinode;
392 struct ocfs2_group_desc *bg;
393
394 ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot);
395 ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
396 strlen(namebuf), &blkno);
397 if (ret) {
398 ret = -ENOENT;
399 goto out;
400 }
401
402 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh);
403 if (ret) {
404 mlog_errno(ret);
405 goto out;
406 }
407
408 ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data;
409 cl = &(ac_dinode->id2.i_chain);
410 rec = &(cl->cl_recs[0]);
411
412 if (type == GLOBAL_BITMAP_SYSTEM_INODE)
Tristan Ye6aea6f52011-05-27 15:19:56 +0800413 bits_per_unit = osb->s_clustersize_bits -
414 inode->i_sb->s_blocksize_bits;
Tristan Ye1c06b912011-03-18 14:35:35 +0800415 /*
416 * 'vict_blkno' was out of the valid range.
417 */
418 if ((vict_blkno < le64_to_cpu(rec->c_blkno)) ||
Joseph Qi7fa05c62014-10-09 15:24:56 -0700419 (vict_blkno >= ((u64)le32_to_cpu(ac_dinode->id1.bitmap1.i_total) <<
Tristan Ye6aea6f52011-05-27 15:19:56 +0800420 bits_per_unit))) {
Tristan Ye1c06b912011-03-18 14:35:35 +0800421 ret = -EINVAL;
422 goto out;
423 }
424
425 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
426
427 rec = &(cl->cl_recs[i]);
428 if (!rec)
429 continue;
430
431 bg = NULL;
432
433 do {
434 if (!bg)
435 blkno = le64_to_cpu(rec->c_blkno);
436 else
437 blkno = le64_to_cpu(bg->bg_next_group);
438
439 if (gd_bh) {
440 brelse(gd_bh);
441 gd_bh = NULL;
442 }
443
444 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh);
445 if (ret) {
446 mlog_errno(ret);
447 goto out;
448 }
449
450 bg = (struct ocfs2_group_desc *)gd_bh->b_data;
451
452 if (vict_blkno < (le64_to_cpu(bg->bg_blkno) +
453 le16_to_cpu(bg->bg_bits))) {
454
455 *ret_bh = gd_bh;
Tristan Ye6aea6f52011-05-27 15:19:56 +0800456 *vict_bit = (vict_blkno - blkno) >>
457 bits_per_unit;
Tristan Ye1c06b912011-03-18 14:35:35 +0800458 mlog(0, "find the victim group: #%llu, "
459 "total_bits: %u, vict_bit: %u\n",
460 blkno, le16_to_cpu(bg->bg_bits),
461 *vict_bit);
462 goto out;
463 }
464
465 } while (le64_to_cpu(bg->bg_next_group));
466 }
467
468 ret = -EINVAL;
469out:
470 brelse(ac_bh);
471
472 /*
473 * caller has to release the gd_bh properly.
474 */
475 return ret;
476}
Tristan Ye99e4c752011-03-18 14:35:36 +0800477
478/*
479 * XXX: helper to validate and adjust moving goal.
480 */
481static int ocfs2_validate_and_adjust_move_goal(struct inode *inode,
482 struct ocfs2_move_extents *range)
483{
484 int ret, goal_bit = 0;
485
486 struct buffer_head *gd_bh = NULL;
Dan Carpenter7f4804d2013-04-29 15:05:59 -0700487 struct ocfs2_group_desc *bg;
Tristan Ye99e4c752011-03-18 14:35:36 +0800488 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
489 int c_to_b = 1 << (osb->s_clustersize_bits -
490 inode->i_sb->s_blocksize_bits);
491
492 /*
Tristan Yeea5e1672011-05-27 15:24:14 +0800493 * make goal become cluster aligned.
494 */
495 range->me_goal = ocfs2_block_to_cluster_start(inode->i_sb,
496 range->me_goal);
497 /*
Tristan Ye99e4c752011-03-18 14:35:36 +0800498 * validate goal sits within global_bitmap, and return the victim
499 * group desc
500 */
501 ret = ocfs2_find_victim_alloc_group(inode, range->me_goal,
502 GLOBAL_BITMAP_SYSTEM_INODE,
503 OCFS2_INVALID_SLOT,
504 &goal_bit, &gd_bh);
505 if (ret)
506 goto out;
507
508 bg = (struct ocfs2_group_desc *)gd_bh->b_data;
509
510 /*
Dan Carpenter7f4804d2013-04-29 15:05:59 -0700511 * moving goal is not allowd to start with a group desc blok(#0 blk)
512 * let's compromise to the latter cluster.
513 */
514 if (range->me_goal == le64_to_cpu(bg->bg_blkno))
515 range->me_goal += c_to_b;
516
517 /*
Tristan Ye99e4c752011-03-18 14:35:36 +0800518 * movement is not gonna cross two groups.
519 */
520 if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize <
521 range->me_len) {
522 ret = -EINVAL;
523 goto out;
524 }
525 /*
526 * more exact validations/adjustments will be performed later during
527 * moving operation for each extent range.
528 */
529 mlog(0, "extents get ready to be moved to #%llu block\n",
530 range->me_goal);
531
532out:
533 brelse(gd_bh);
534
535 return ret;
536}
Tristan Yee6b58592011-03-18 14:35:37 +0800537
538static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh,
539 int *goal_bit, u32 move_len, u32 max_hop,
540 u32 *phys_cpos)
541{
542 int i, used, last_free_bits = 0, base_bit = *goal_bit;
543 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
544 u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
545 le64_to_cpu(gd->bg_blkno));
546
547 for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) {
548
549 used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap);
550 if (used) {
551 /*
552 * we even tried searching the free chunk by jumping
553 * a 'max_hop' distance, but still failed.
554 */
555 if ((i - base_bit) > max_hop) {
556 *phys_cpos = 0;
557 break;
558 }
559
560 if (last_free_bits)
561 last_free_bits = 0;
562
563 continue;
564 } else
565 last_free_bits++;
566
567 if (last_free_bits == move_len) {
568 *goal_bit = i;
569 *phys_cpos = base_cpos + i;
570 break;
571 }
572 }
573
574 mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
575}
Tristan Ye8473aa82011-05-24 18:51:41 +0800576
Tristan Yee0847712011-05-24 17:35:19 +0800577static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
578 u32 cpos, u32 phys_cpos, u32 *new_phys_cpos,
579 u32 len, int ext_flags)
580{
581 int ret, credits = 0, extra_blocks = 0, goal_bit = 0;
582 handle_t *handle;
583 struct inode *inode = context->inode;
584 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
585 struct inode *tl_inode = osb->osb_tl_inode;
586 struct inode *gb_inode = NULL;
587 struct buffer_head *gb_bh = NULL;
588 struct buffer_head *gd_bh = NULL;
589 struct ocfs2_group_desc *gd;
590 struct ocfs2_refcount_tree *ref_tree = NULL;
591 u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb,
592 context->range->me_threshold);
593 u64 phys_blkno, new_phys_blkno;
594
595 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
596
597 if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
Darrick J. Wong84e40082016-11-09 14:13:09 -0800598 BUG_ON(!ocfs2_is_refcount_inode(inode));
Tristan Yee0847712011-05-24 17:35:19 +0800599 BUG_ON(!context->refcount_loc);
600
601 ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
602 &ref_tree, NULL);
603 if (ret) {
604 mlog_errno(ret);
605 return ret;
606 }
607
608 ret = ocfs2_prepare_refcount_change_for_del(inode,
609 context->refcount_loc,
610 phys_blkno,
611 len,
612 &credits,
613 &extra_blocks);
614 if (ret) {
615 mlog_errno(ret);
616 goto out;
617 }
618 }
619
620 ret = ocfs2_lock_allocators_move_extents(inode, &context->et, len, 1,
621 &context->meta_ac,
622 NULL, extra_blocks, &credits);
623 if (ret) {
624 mlog_errno(ret);
625 goto out;
626 }
627
628 /*
629 * need to count 2 extra credits for global_bitmap inode and
630 * group descriptor.
631 */
632 credits += OCFS2_INODE_UPDATE_CREDITS + 1;
633
634 /*
635 * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
636 * logic, while we still need to lock the global_bitmap.
637 */
638 gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
639 OCFS2_INVALID_SLOT);
640 if (!gb_inode) {
641 mlog(ML_ERROR, "unable to get global_bitmap inode\n");
642 ret = -EIO;
643 goto out;
644 }
645
Al Viro59551022016-01-22 15:40:57 -0500646 inode_lock(gb_inode);
Tristan Yee0847712011-05-24 17:35:19 +0800647
648 ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
649 if (ret) {
650 mlog_errno(ret);
651 goto out_unlock_gb_mutex;
652 }
653
Al Viro59551022016-01-22 15:40:57 -0500654 inode_lock(tl_inode);
Tristan Yee0847712011-05-24 17:35:19 +0800655
656 handle = ocfs2_start_trans(osb, credits);
657 if (IS_ERR(handle)) {
658 ret = PTR_ERR(handle);
659 mlog_errno(ret);
660 goto out_unlock_tl_inode;
661 }
662
663 new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
664 ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno,
665 GLOBAL_BITMAP_SYSTEM_INODE,
666 OCFS2_INVALID_SLOT,
667 &goal_bit, &gd_bh);
668 if (ret) {
669 mlog_errno(ret);
670 goto out_commit;
671 }
672
673 /*
674 * probe the victim cluster group to find a proper
675 * region to fit wanted movement, it even will perfrom
676 * a best-effort attempt by compromising to a threshold
677 * around the goal.
678 */
679 ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop,
680 new_phys_cpos);
Dan Carpenter3d75be72011-05-29 22:56:31 +0300681 if (!*new_phys_cpos) {
Tristan Yee0847712011-05-24 17:35:19 +0800682 ret = -ENOSPC;
683 goto out_commit;
684 }
685
686 ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
687 *new_phys_cpos, ext_flags);
688 if (ret) {
689 mlog_errno(ret);
690 goto out_commit;
691 }
692
693 gd = (struct ocfs2_group_desc *)gd_bh->b_data;
694 ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len,
695 le16_to_cpu(gd->bg_chain));
696 if (ret) {
697 mlog_errno(ret);
698 goto out_commit;
699 }
700
701 ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
702 goal_bit, len);
Younger Liudb66c712014-04-03 14:47:10 -0700703 if (ret) {
704 ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len,
705 le16_to_cpu(gd->bg_chain));
Tristan Yee0847712011-05-24 17:35:19 +0800706 mlog_errno(ret);
Younger Liudb66c712014-04-03 14:47:10 -0700707 }
Tristan Yee0847712011-05-24 17:35:19 +0800708
709 /*
710 * Here we should write the new page out first if we are
711 * in write-back mode.
712 */
713 ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len);
714 if (ret)
715 mlog_errno(ret);
716
717out_commit:
718 ocfs2_commit_trans(osb, handle);
719 brelse(gd_bh);
720
721out_unlock_tl_inode:
Al Viro59551022016-01-22 15:40:57 -0500722 inode_unlock(tl_inode);
Tristan Yee0847712011-05-24 17:35:19 +0800723
724 ocfs2_inode_unlock(gb_inode, 1);
725out_unlock_gb_mutex:
Al Viro59551022016-01-22 15:40:57 -0500726 inode_unlock(gb_inode);
Tristan Yee0847712011-05-24 17:35:19 +0800727 brelse(gb_bh);
728 iput(gb_inode);
729
730out:
731 if (context->meta_ac) {
732 ocfs2_free_alloc_context(context->meta_ac);
733 context->meta_ac = NULL;
734 }
735
736 if (ref_tree)
737 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
738
739 return ret;
740}
Tristan Yeee16cc02011-03-18 14:35:40 +0800741
742/*
743 * Helper to calculate the defraging length in one run according to threshold.
744 */
745static void ocfs2_calc_extent_defrag_len(u32 *alloc_size, u32 *len_defraged,
746 u32 threshold, int *skip)
747{
748 if ((*alloc_size + *len_defraged) < threshold) {
749 /*
750 * proceed defragmentation until we meet the thresh
751 */
752 *len_defraged += *alloc_size;
753 } else if (*len_defraged == 0) {
754 /*
755 * XXX: skip a large extent.
756 */
757 *skip = 1;
758 } else {
759 /*
760 * split this extent to coalesce with former pieces as
761 * to reach the threshold.
762 *
763 * we're done here with one cycle of defragmentation
764 * in a size of 'thresh', resetting 'len_defraged'
765 * forces a new defragmentation.
766 */
767 *alloc_size = threshold - *len_defraged;
768 *len_defraged = 0;
769 }
770}
Tristan Ye53069d42011-05-25 14:23:43 +0800771
772static int __ocfs2_move_extents_range(struct buffer_head *di_bh,
773 struct ocfs2_move_extents_context *context)
774{
775 int ret = 0, flags, do_defrag, skip = 0;
776 u32 cpos, phys_cpos, move_start, len_to_move, alloc_size;
777 u32 len_defraged = 0, defrag_thresh = 0, new_phys_cpos = 0;
778
779 struct inode *inode = context->inode;
780 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
781 struct ocfs2_move_extents *range = context->range;
782 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
783
Junxiao Bif17c20d2013-09-11 14:19:45 -0700784 if ((i_size_read(inode) == 0) || (range->me_len == 0))
Tristan Ye53069d42011-05-25 14:23:43 +0800785 return 0;
786
787 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
788 return 0;
789
790 context->refcount_loc = le64_to_cpu(di->i_refcount_loc);
791
792 ocfs2_init_dinode_extent_tree(&context->et, INODE_CACHE(inode), di_bh);
793 ocfs2_init_dealloc_ctxt(&context->dealloc);
794
795 /*
796 * TO-DO XXX:
797 *
798 * - xattr extents.
799 */
800
801 do_defrag = context->auto_defrag;
802
803 /*
804 * extents moving happens in unit of clusters, for the sake
805 * of simplicity, we may ignore two clusters where 'byte_start'
806 * and 'byte_start + len' were within.
807 */
808 move_start = ocfs2_clusters_for_bytes(osb->sb, range->me_start);
809 len_to_move = (range->me_start + range->me_len) >>
810 osb->s_clustersize_bits;
811 if (len_to_move >= move_start)
812 len_to_move -= move_start;
813 else
814 len_to_move = 0;
815
Tristan Yedda54e72011-05-25 14:45:41 +0800816 if (do_defrag) {
Tristan Ye53069d42011-05-25 14:23:43 +0800817 defrag_thresh = range->me_threshold >> osb->s_clustersize_bits;
Tristan Yedda54e72011-05-25 14:45:41 +0800818 if (defrag_thresh <= 1)
819 goto done;
820 } else
Tristan Ye53069d42011-05-25 14:23:43 +0800821 new_phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
822 range->me_goal);
823
824 mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u, "
825 "thresh: %u\n",
826 (unsigned long long)OCFS2_I(inode)->ip_blkno,
827 (unsigned long long)range->me_start,
828 (unsigned long long)range->me_len,
829 move_start, len_to_move, defrag_thresh);
830
831 cpos = move_start;
832 while (len_to_move) {
833 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &alloc_size,
834 &flags);
835 if (ret) {
836 mlog_errno(ret);
837 goto out;
838 }
839
840 if (alloc_size > len_to_move)
841 alloc_size = len_to_move;
842
843 /*
844 * XXX: how to deal with a hole:
845 *
846 * - skip the hole of course
847 * - force a new defragmentation
848 */
849 if (!phys_cpos) {
850 if (do_defrag)
851 len_defraged = 0;
852
853 goto next;
854 }
855
856 if (do_defrag) {
857 ocfs2_calc_extent_defrag_len(&alloc_size, &len_defraged,
858 defrag_thresh, &skip);
859 /*
860 * skip large extents
861 */
862 if (skip) {
863 skip = 0;
864 goto next;
865 }
866
867 mlog(0, "#Defrag: cpos: %u, phys_cpos: %u, "
868 "alloc_size: %u, len_defraged: %u\n",
869 cpos, phys_cpos, alloc_size, len_defraged);
870
871 ret = ocfs2_defrag_extent(context, cpos, phys_cpos,
Tristan Ye4dfa66b2011-05-25 14:30:36 +0800872 &alloc_size, flags);
Tristan Ye53069d42011-05-25 14:23:43 +0800873 } else {
874 ret = ocfs2_move_extent(context, cpos, phys_cpos,
875 &new_phys_cpos, alloc_size,
876 flags);
877
878 new_phys_cpos += alloc_size;
879 }
880
881 if (ret < 0) {
882 mlog_errno(ret);
883 goto out;
884 }
885
886 context->clusters_moved += alloc_size;
887next:
888 cpos += alloc_size;
889 len_to_move -= alloc_size;
890 }
891
Tristan Yedda54e72011-05-25 14:45:41 +0800892done:
Tristan Ye53069d42011-05-25 14:23:43 +0800893 range->me_flags |= OCFS2_MOVE_EXT_FL_COMPLETE;
894
895out:
896 range->me_moved_len = ocfs2_clusters_to_bytes(osb->sb,
897 context->clusters_moved);
898 range->me_new_offset = ocfs2_clusters_to_bytes(osb->sb,
899 context->new_phys_cpos);
900
901 ocfs2_schedule_truncate_log_flush(osb, 1);
902 ocfs2_run_deallocs(osb, &context->dealloc);
903
904 return ret;
905}
906
907static int ocfs2_move_extents(struct ocfs2_move_extents_context *context)
908{
909 int status;
910 handle_t *handle;
911 struct inode *inode = context->inode;
912 struct ocfs2_dinode *di;
913 struct buffer_head *di_bh = NULL;
914 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
915
Tristan Ye53069d42011-05-25 14:23:43 +0800916 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
917 return -EROFS;
918
Al Viro59551022016-01-22 15:40:57 -0500919 inode_lock(inode);
Tristan Ye53069d42011-05-25 14:23:43 +0800920
921 /*
922 * This prevents concurrent writes from other nodes
923 */
924 status = ocfs2_rw_lock(inode, 1);
925 if (status) {
926 mlog_errno(status);
927 goto out;
928 }
929
930 status = ocfs2_inode_lock(inode, &di_bh, 1);
931 if (status) {
932 mlog_errno(status);
933 goto out_rw_unlock;
934 }
935
936 /*
937 * rememer ip_xattr_sem also needs to be held if necessary
938 */
939 down_write(&OCFS2_I(inode)->ip_alloc_sem);
940
941 status = __ocfs2_move_extents_range(di_bh, context);
942
943 up_write(&OCFS2_I(inode)->ip_alloc_sem);
944 if (status) {
945 mlog_errno(status);
946 goto out_inode_unlock;
947 }
948
949 /*
950 * We update ctime for these changes
951 */
952 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
953 if (IS_ERR(handle)) {
954 status = PTR_ERR(handle);
955 mlog_errno(status);
956 goto out_inode_unlock;
957 }
958
959 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
960 OCFS2_JOURNAL_ACCESS_WRITE);
961 if (status) {
962 mlog_errno(status);
963 goto out_commit;
964 }
965
966 di = (struct ocfs2_dinode *)di_bh->b_data;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700967 inode->i_ctime = current_time(inode);
Tristan Ye53069d42011-05-25 14:23:43 +0800968 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
969 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700970 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Tristan Ye53069d42011-05-25 14:23:43 +0800971
972 ocfs2_journal_dirty(handle, di_bh);
973
974out_commit:
975 ocfs2_commit_trans(osb, handle);
976
977out_inode_unlock:
978 brelse(di_bh);
979 ocfs2_inode_unlock(inode, 1);
980out_rw_unlock:
981 ocfs2_rw_unlock(inode, 1);
982out:
Al Viro59551022016-01-22 15:40:57 -0500983 inode_unlock(inode);
Tristan Ye53069d42011-05-25 14:23:43 +0800984
985 return status;
986}
987
988int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp)
989{
990 int status;
991
Al Viro496ad9a2013-01-23 17:07:38 -0500992 struct inode *inode = file_inode(filp);
Tristan Ye53069d42011-05-25 14:23:43 +0800993 struct ocfs2_move_extents range;
Dan Carpenter85a258b2013-04-29 15:05:58 -0700994 struct ocfs2_move_extents_context *context;
995
996 if (!argp)
997 return -EINVAL;
Tristan Ye53069d42011-05-25 14:23:43 +0800998
Al Viroa561be72011-11-23 11:57:51 -0500999 status = mnt_want_write_file(filp);
Tristan Ye53069d42011-05-25 14:23:43 +08001000 if (status)
1001 return status;
1002
Younger Liubfbca922013-11-12 15:07:05 -08001003 if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) {
1004 status = -EPERM;
Dan Carpenter85a258b2013-04-29 15:05:58 -07001005 goto out_drop;
Younger Liubfbca922013-11-12 15:07:05 -08001006 }
Tristan Ye53069d42011-05-25 14:23:43 +08001007
1008 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1009 status = -EPERM;
Dan Carpenter85a258b2013-04-29 15:05:58 -07001010 goto out_drop;
Tristan Ye53069d42011-05-25 14:23:43 +08001011 }
1012
1013 context = kzalloc(sizeof(struct ocfs2_move_extents_context), GFP_NOFS);
1014 if (!context) {
1015 status = -ENOMEM;
1016 mlog_errno(status);
Dan Carpenter85a258b2013-04-29 15:05:58 -07001017 goto out_drop;
Tristan Ye53069d42011-05-25 14:23:43 +08001018 }
1019
1020 context->inode = inode;
1021 context->file = filp;
1022
Dan Carpenter85a258b2013-04-29 15:05:58 -07001023 if (copy_from_user(&range, argp, sizeof(range))) {
1024 status = -EFAULT;
1025 goto out_free;
Tristan Ye53069d42011-05-25 14:23:43 +08001026 }
1027
Younger Liubfbca922013-11-12 15:07:05 -08001028 if (range.me_start > i_size_read(inode)) {
1029 status = -EINVAL;
Dan Carpenter85a258b2013-04-29 15:05:58 -07001030 goto out_free;
Younger Liubfbca922013-11-12 15:07:05 -08001031 }
Tristan Ye53069d42011-05-25 14:23:43 +08001032
1033 if (range.me_start + range.me_len > i_size_read(inode))
1034 range.me_len = i_size_read(inode) - range.me_start;
1035
1036 context->range = &range;
1037
1038 if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) {
1039 context->auto_defrag = 1;
Tristan Yedda54e72011-05-25 14:45:41 +08001040 /*
1041 * ok, the default theshold for the defragmentation
1042 * is 1M, since our maximum clustersize was 1M also.
1043 * any thought?
1044 */
Tristan Ye53069d42011-05-25 14:23:43 +08001045 if (!range.me_threshold)
Tristan Ye53069d42011-05-25 14:23:43 +08001046 range.me_threshold = 1024 * 1024;
Tristan Yedda54e72011-05-25 14:45:41 +08001047
1048 if (range.me_threshold > i_size_read(inode))
1049 range.me_threshold = i_size_read(inode);
1050
Tristan Ye4dfa66b2011-05-25 14:30:36 +08001051 if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG)
1052 context->partial = 1;
Tristan Ye53069d42011-05-25 14:23:43 +08001053 } else {
1054 /*
1055 * first best-effort attempt to validate and adjust the goal
1056 * (physical address in block), while it can't guarantee later
1057 * operation can succeed all the time since global_bitmap may
1058 * change a bit over time.
1059 */
1060
1061 status = ocfs2_validate_and_adjust_move_goal(inode, &range);
1062 if (status)
Dan Carpenter85a258b2013-04-29 15:05:58 -07001063 goto out_copy;
Tristan Ye53069d42011-05-25 14:23:43 +08001064 }
1065
1066 status = ocfs2_move_extents(context);
1067 if (status)
1068 mlog_errno(status);
Dan Carpenter85a258b2013-04-29 15:05:58 -07001069out_copy:
Tristan Ye53069d42011-05-25 14:23:43 +08001070 /*
1071 * movement/defragmentation may end up being partially completed,
1072 * that's the reason why we need to return userspace the finished
1073 * length and new_offset even if failure happens somewhere.
1074 */
Dan Carpenter85a258b2013-04-29 15:05:58 -07001075 if (copy_to_user(argp, &range, sizeof(range)))
1076 status = -EFAULT;
Tristan Ye53069d42011-05-25 14:23:43 +08001077
Dan Carpenter85a258b2013-04-29 15:05:58 -07001078out_free:
Tristan Ye53069d42011-05-25 14:23:43 +08001079 kfree(context);
Dan Carpenter85a258b2013-04-29 15:05:58 -07001080out_drop:
Al Viro2a79f172011-12-09 08:06:57 -05001081 mnt_drop_write_file(filp);
Tristan Ye53069d42011-05-25 14:23:43 +08001082
1083 return status;
1084}