move_extents.c 27 KB

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