transaction.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/writeback.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/blkdev.h>
  24. #include "ctree.h"
  25. #include "disk-io.h"
  26. #include "transaction.h"
  27. #include "locking.h"
  28. #include "tree-log.h"
  29. #define BTRFS_ROOT_TRANS_TAG 0
  30. static noinline void put_transaction(struct btrfs_transaction *transaction)
  31. {
  32. WARN_ON(transaction->use_count == 0);
  33. transaction->use_count--;
  34. if (transaction->use_count == 0) {
  35. list_del_init(&transaction->list);
  36. memset(transaction, 0, sizeof(*transaction));
  37. kmem_cache_free(btrfs_transaction_cachep, transaction);
  38. }
  39. }
  40. static noinline void switch_commit_root(struct btrfs_root *root)
  41. {
  42. free_extent_buffer(root->commit_root);
  43. root->commit_root = btrfs_root_node(root);
  44. }
  45. /*
  46. * either allocate a new transaction or hop into the existing one
  47. */
  48. static noinline int join_transaction(struct btrfs_root *root)
  49. {
  50. struct btrfs_transaction *cur_trans;
  51. cur_trans = root->fs_info->running_transaction;
  52. if (!cur_trans) {
  53. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  54. GFP_NOFS);
  55. if (!cur_trans)
  56. return -ENOMEM;
  57. root->fs_info->generation++;
  58. cur_trans->num_writers = 1;
  59. cur_trans->num_joined = 0;
  60. cur_trans->transid = root->fs_info->generation;
  61. init_waitqueue_head(&cur_trans->writer_wait);
  62. init_waitqueue_head(&cur_trans->commit_wait);
  63. cur_trans->in_commit = 0;
  64. cur_trans->blocked = 0;
  65. cur_trans->use_count = 1;
  66. cur_trans->commit_done = 0;
  67. cur_trans->start_time = get_seconds();
  68. cur_trans->delayed_refs.root = RB_ROOT;
  69. cur_trans->delayed_refs.num_entries = 0;
  70. cur_trans->delayed_refs.num_heads_ready = 0;
  71. cur_trans->delayed_refs.num_heads = 0;
  72. cur_trans->delayed_refs.flushing = 0;
  73. cur_trans->delayed_refs.run_delayed_start = 0;
  74. spin_lock_init(&cur_trans->delayed_refs.lock);
  75. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  76. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  77. extent_io_tree_init(&cur_trans->dirty_pages,
  78. root->fs_info->btree_inode->i_mapping,
  79. GFP_NOFS);
  80. spin_lock(&root->fs_info->new_trans_lock);
  81. root->fs_info->running_transaction = cur_trans;
  82. spin_unlock(&root->fs_info->new_trans_lock);
  83. } else {
  84. cur_trans->num_writers++;
  85. cur_trans->num_joined++;
  86. }
  87. return 0;
  88. }
  89. /*
  90. * this does all the record keeping required to make sure that a reference
  91. * counted root is properly recorded in a given transaction. This is required
  92. * to make sure the old root from before we joined the transaction is deleted
  93. * when the transaction commits
  94. */
  95. static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
  96. struct btrfs_root *root)
  97. {
  98. if (root->ref_cows && root->last_trans < trans->transid) {
  99. WARN_ON(root == root->fs_info->extent_root);
  100. WARN_ON(root->commit_root != root->node);
  101. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  102. (unsigned long)root->root_key.objectid,
  103. BTRFS_ROOT_TRANS_TAG);
  104. root->last_trans = trans->transid;
  105. btrfs_init_reloc_root(trans, root);
  106. }
  107. return 0;
  108. }
  109. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  110. struct btrfs_root *root)
  111. {
  112. if (!root->ref_cows)
  113. return 0;
  114. mutex_lock(&root->fs_info->trans_mutex);
  115. if (root->last_trans == trans->transid) {
  116. mutex_unlock(&root->fs_info->trans_mutex);
  117. return 0;
  118. }
  119. record_root_in_trans(trans, root);
  120. mutex_unlock(&root->fs_info->trans_mutex);
  121. return 0;
  122. }
  123. /* wait for commit against the current transaction to become unblocked
  124. * when this is done, it is safe to start a new transaction, but the current
  125. * transaction might not be fully on disk.
  126. */
  127. static void wait_current_trans(struct btrfs_root *root)
  128. {
  129. struct btrfs_transaction *cur_trans;
  130. cur_trans = root->fs_info->running_transaction;
  131. if (cur_trans && cur_trans->blocked) {
  132. DEFINE_WAIT(wait);
  133. cur_trans->use_count++;
  134. while (1) {
  135. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  136. TASK_UNINTERRUPTIBLE);
  137. if (!cur_trans->blocked)
  138. break;
  139. mutex_unlock(&root->fs_info->trans_mutex);
  140. schedule();
  141. mutex_lock(&root->fs_info->trans_mutex);
  142. }
  143. finish_wait(&root->fs_info->transaction_wait, &wait);
  144. put_transaction(cur_trans);
  145. }
  146. }
  147. enum btrfs_trans_type {
  148. TRANS_START,
  149. TRANS_JOIN,
  150. TRANS_USERSPACE,
  151. TRANS_JOIN_NOLOCK,
  152. };
  153. static int may_wait_transaction(struct btrfs_root *root, int type)
  154. {
  155. if (!root->fs_info->log_root_recovering &&
  156. ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
  157. type == TRANS_USERSPACE))
  158. return 1;
  159. return 0;
  160. }
  161. static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  162. u64 num_items, int type)
  163. {
  164. struct btrfs_trans_handle *h;
  165. struct btrfs_transaction *cur_trans;
  166. int ret;
  167. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  168. return ERR_PTR(-EROFS);
  169. again:
  170. h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  171. if (!h)
  172. return ERR_PTR(-ENOMEM);
  173. if (type != TRANS_JOIN_NOLOCK)
  174. mutex_lock(&root->fs_info->trans_mutex);
  175. if (may_wait_transaction(root, type))
  176. wait_current_trans(root);
  177. ret = join_transaction(root);
  178. if (ret < 0) {
  179. kmem_cache_free(btrfs_trans_handle_cachep, h);
  180. if (type != TRANS_JOIN_NOLOCK)
  181. mutex_unlock(&root->fs_info->trans_mutex);
  182. return ERR_PTR(ret);
  183. }
  184. cur_trans = root->fs_info->running_transaction;
  185. cur_trans->use_count++;
  186. if (type != TRANS_JOIN_NOLOCK)
  187. mutex_unlock(&root->fs_info->trans_mutex);
  188. h->transid = cur_trans->transid;
  189. h->transaction = cur_trans;
  190. h->blocks_used = 0;
  191. h->block_group = 0;
  192. h->bytes_reserved = 0;
  193. h->delayed_ref_updates = 0;
  194. h->block_rsv = NULL;
  195. smp_mb();
  196. if (cur_trans->blocked && may_wait_transaction(root, type)) {
  197. btrfs_commit_transaction(h, root);
  198. goto again;
  199. }
  200. if (num_items > 0) {
  201. ret = btrfs_trans_reserve_metadata(h, root, num_items);
  202. if (ret == -EAGAIN) {
  203. btrfs_commit_transaction(h, root);
  204. goto again;
  205. }
  206. if (ret < 0) {
  207. btrfs_end_transaction(h, root);
  208. return ERR_PTR(ret);
  209. }
  210. }
  211. if (type != TRANS_JOIN_NOLOCK)
  212. mutex_lock(&root->fs_info->trans_mutex);
  213. record_root_in_trans(h, root);
  214. if (type != TRANS_JOIN_NOLOCK)
  215. mutex_unlock(&root->fs_info->trans_mutex);
  216. if (!current->journal_info && type != TRANS_USERSPACE)
  217. current->journal_info = h;
  218. return h;
  219. }
  220. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  221. int num_items)
  222. {
  223. return start_transaction(root, num_items, TRANS_START);
  224. }
  225. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  226. int num_blocks)
  227. {
  228. return start_transaction(root, 0, TRANS_JOIN);
  229. }
  230. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
  231. int num_blocks)
  232. {
  233. return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
  234. }
  235. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  236. int num_blocks)
  237. {
  238. return start_transaction(r, 0, TRANS_USERSPACE);
  239. }
  240. /* wait for a transaction commit to be fully complete */
  241. static noinline int wait_for_commit(struct btrfs_root *root,
  242. struct btrfs_transaction *commit)
  243. {
  244. DEFINE_WAIT(wait);
  245. mutex_lock(&root->fs_info->trans_mutex);
  246. while (!commit->commit_done) {
  247. prepare_to_wait(&commit->commit_wait, &wait,
  248. TASK_UNINTERRUPTIBLE);
  249. if (commit->commit_done)
  250. break;
  251. mutex_unlock(&root->fs_info->trans_mutex);
  252. schedule();
  253. mutex_lock(&root->fs_info->trans_mutex);
  254. }
  255. mutex_unlock(&root->fs_info->trans_mutex);
  256. finish_wait(&commit->commit_wait, &wait);
  257. return 0;
  258. }
  259. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
  260. {
  261. struct btrfs_transaction *cur_trans = NULL, *t;
  262. int ret;
  263. mutex_lock(&root->fs_info->trans_mutex);
  264. ret = 0;
  265. if (transid) {
  266. if (transid <= root->fs_info->last_trans_committed)
  267. goto out_unlock;
  268. /* find specified transaction */
  269. list_for_each_entry(t, &root->fs_info->trans_list, list) {
  270. if (t->transid == transid) {
  271. cur_trans = t;
  272. break;
  273. }
  274. if (t->transid > transid)
  275. break;
  276. }
  277. ret = -EINVAL;
  278. if (!cur_trans)
  279. goto out_unlock; /* bad transid */
  280. } else {
  281. /* find newest transaction that is committing | committed */
  282. list_for_each_entry_reverse(t, &root->fs_info->trans_list,
  283. list) {
  284. if (t->in_commit) {
  285. if (t->commit_done)
  286. goto out_unlock;
  287. cur_trans = t;
  288. break;
  289. }
  290. }
  291. if (!cur_trans)
  292. goto out_unlock; /* nothing committing|committed */
  293. }
  294. cur_trans->use_count++;
  295. mutex_unlock(&root->fs_info->trans_mutex);
  296. wait_for_commit(root, cur_trans);
  297. mutex_lock(&root->fs_info->trans_mutex);
  298. put_transaction(cur_trans);
  299. ret = 0;
  300. out_unlock:
  301. mutex_unlock(&root->fs_info->trans_mutex);
  302. return ret;
  303. }
  304. #if 0
  305. /*
  306. * rate limit against the drop_snapshot code. This helps to slow down new
  307. * operations if the drop_snapshot code isn't able to keep up.
  308. */
  309. static void throttle_on_drops(struct btrfs_root *root)
  310. {
  311. struct btrfs_fs_info *info = root->fs_info;
  312. int harder_count = 0;
  313. harder:
  314. if (atomic_read(&info->throttles)) {
  315. DEFINE_WAIT(wait);
  316. int thr;
  317. thr = atomic_read(&info->throttle_gen);
  318. do {
  319. prepare_to_wait(&info->transaction_throttle,
  320. &wait, TASK_UNINTERRUPTIBLE);
  321. if (!atomic_read(&info->throttles)) {
  322. finish_wait(&info->transaction_throttle, &wait);
  323. break;
  324. }
  325. schedule();
  326. finish_wait(&info->transaction_throttle, &wait);
  327. } while (thr == atomic_read(&info->throttle_gen));
  328. harder_count++;
  329. if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
  330. harder_count < 2)
  331. goto harder;
  332. if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
  333. harder_count < 10)
  334. goto harder;
  335. if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
  336. harder_count < 20)
  337. goto harder;
  338. }
  339. }
  340. #endif
  341. void btrfs_throttle(struct btrfs_root *root)
  342. {
  343. mutex_lock(&root->fs_info->trans_mutex);
  344. if (!root->fs_info->open_ioctl_trans)
  345. wait_current_trans(root);
  346. mutex_unlock(&root->fs_info->trans_mutex);
  347. }
  348. static int should_end_transaction(struct btrfs_trans_handle *trans,
  349. struct btrfs_root *root)
  350. {
  351. int ret;
  352. ret = btrfs_block_rsv_check(trans, root,
  353. &root->fs_info->global_block_rsv, 0, 5);
  354. return ret ? 1 : 0;
  355. }
  356. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  357. struct btrfs_root *root)
  358. {
  359. struct btrfs_transaction *cur_trans = trans->transaction;
  360. int updates;
  361. if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
  362. return 1;
  363. updates = trans->delayed_ref_updates;
  364. trans->delayed_ref_updates = 0;
  365. if (updates)
  366. btrfs_run_delayed_refs(trans, root, updates);
  367. return should_end_transaction(trans, root);
  368. }
  369. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  370. struct btrfs_root *root, int throttle, int lock)
  371. {
  372. struct btrfs_transaction *cur_trans = trans->transaction;
  373. struct btrfs_fs_info *info = root->fs_info;
  374. int count = 0;
  375. while (count < 4) {
  376. unsigned long cur = trans->delayed_ref_updates;
  377. trans->delayed_ref_updates = 0;
  378. if (cur &&
  379. trans->transaction->delayed_refs.num_heads_ready > 64) {
  380. trans->delayed_ref_updates = 0;
  381. /*
  382. * do a full flush if the transaction is trying
  383. * to close
  384. */
  385. if (trans->transaction->delayed_refs.flushing)
  386. cur = 0;
  387. btrfs_run_delayed_refs(trans, root, cur);
  388. } else {
  389. break;
  390. }
  391. count++;
  392. }
  393. btrfs_trans_release_metadata(trans, root);
  394. if (lock && !root->fs_info->open_ioctl_trans &&
  395. should_end_transaction(trans, root))
  396. trans->transaction->blocked = 1;
  397. if (lock && cur_trans->blocked && !cur_trans->in_commit) {
  398. if (throttle)
  399. return btrfs_commit_transaction(trans, root);
  400. else
  401. wake_up_process(info->transaction_kthread);
  402. }
  403. if (lock)
  404. mutex_lock(&info->trans_mutex);
  405. WARN_ON(cur_trans != info->running_transaction);
  406. WARN_ON(cur_trans->num_writers < 1);
  407. cur_trans->num_writers--;
  408. smp_mb();
  409. if (waitqueue_active(&cur_trans->writer_wait))
  410. wake_up(&cur_trans->writer_wait);
  411. put_transaction(cur_trans);
  412. if (lock)
  413. mutex_unlock(&info->trans_mutex);
  414. if (current->journal_info == trans)
  415. current->journal_info = NULL;
  416. memset(trans, 0, sizeof(*trans));
  417. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  418. if (throttle)
  419. btrfs_run_delayed_iputs(root);
  420. return 0;
  421. }
  422. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  423. struct btrfs_root *root)
  424. {
  425. return __btrfs_end_transaction(trans, root, 0, 1);
  426. }
  427. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  428. struct btrfs_root *root)
  429. {
  430. return __btrfs_end_transaction(trans, root, 1, 1);
  431. }
  432. int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
  433. struct btrfs_root *root)
  434. {
  435. return __btrfs_end_transaction(trans, root, 0, 0);
  436. }
  437. /*
  438. * when btree blocks are allocated, they have some corresponding bits set for
  439. * them in one of two extent_io trees. This is used to make sure all of
  440. * those extents are sent to disk but does not wait on them
  441. */
  442. int btrfs_write_marked_extents(struct btrfs_root *root,
  443. struct extent_io_tree *dirty_pages, int mark)
  444. {
  445. int ret;
  446. int err = 0;
  447. int werr = 0;
  448. struct page *page;
  449. struct inode *btree_inode = root->fs_info->btree_inode;
  450. u64 start = 0;
  451. u64 end;
  452. unsigned long index;
  453. while (1) {
  454. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  455. mark);
  456. if (ret)
  457. break;
  458. while (start <= end) {
  459. cond_resched();
  460. index = start >> PAGE_CACHE_SHIFT;
  461. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  462. page = find_get_page(btree_inode->i_mapping, index);
  463. if (!page)
  464. continue;
  465. btree_lock_page_hook(page);
  466. if (!page->mapping) {
  467. unlock_page(page);
  468. page_cache_release(page);
  469. continue;
  470. }
  471. if (PageWriteback(page)) {
  472. if (PageDirty(page))
  473. wait_on_page_writeback(page);
  474. else {
  475. unlock_page(page);
  476. page_cache_release(page);
  477. continue;
  478. }
  479. }
  480. err = write_one_page(page, 0);
  481. if (err)
  482. werr = err;
  483. page_cache_release(page);
  484. }
  485. }
  486. if (err)
  487. werr = err;
  488. return werr;
  489. }
  490. /*
  491. * when btree blocks are allocated, they have some corresponding bits set for
  492. * them in one of two extent_io trees. This is used to make sure all of
  493. * those extents are on disk for transaction or log commit. We wait
  494. * on all the pages and clear them from the dirty pages state tree
  495. */
  496. int btrfs_wait_marked_extents(struct btrfs_root *root,
  497. struct extent_io_tree *dirty_pages, int mark)
  498. {
  499. int ret;
  500. int err = 0;
  501. int werr = 0;
  502. struct page *page;
  503. struct inode *btree_inode = root->fs_info->btree_inode;
  504. u64 start = 0;
  505. u64 end;
  506. unsigned long index;
  507. while (1) {
  508. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  509. mark);
  510. if (ret)
  511. break;
  512. clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
  513. while (start <= end) {
  514. index = start >> PAGE_CACHE_SHIFT;
  515. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  516. page = find_get_page(btree_inode->i_mapping, index);
  517. if (!page)
  518. continue;
  519. if (PageDirty(page)) {
  520. btree_lock_page_hook(page);
  521. wait_on_page_writeback(page);
  522. err = write_one_page(page, 0);
  523. if (err)
  524. werr = err;
  525. }
  526. wait_on_page_writeback(page);
  527. page_cache_release(page);
  528. cond_resched();
  529. }
  530. }
  531. if (err)
  532. werr = err;
  533. return werr;
  534. }
  535. /*
  536. * when btree blocks are allocated, they have some corresponding bits set for
  537. * them in one of two extent_io trees. This is used to make sure all of
  538. * those extents are on disk for transaction or log commit
  539. */
  540. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  541. struct extent_io_tree *dirty_pages, int mark)
  542. {
  543. int ret;
  544. int ret2;
  545. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  546. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  547. return ret || ret2;
  548. }
  549. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  550. struct btrfs_root *root)
  551. {
  552. if (!trans || !trans->transaction) {
  553. struct inode *btree_inode;
  554. btree_inode = root->fs_info->btree_inode;
  555. return filemap_write_and_wait(btree_inode->i_mapping);
  556. }
  557. return btrfs_write_and_wait_marked_extents(root,
  558. &trans->transaction->dirty_pages,
  559. EXTENT_DIRTY);
  560. }
  561. /*
  562. * this is used to update the root pointer in the tree of tree roots.
  563. *
  564. * But, in the case of the extent allocation tree, updating the root
  565. * pointer may allocate blocks which may change the root of the extent
  566. * allocation tree.
  567. *
  568. * So, this loops and repeats and makes sure the cowonly root didn't
  569. * change while the root pointer was being updated in the metadata.
  570. */
  571. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  572. struct btrfs_root *root)
  573. {
  574. int ret;
  575. u64 old_root_bytenr;
  576. u64 old_root_used;
  577. struct btrfs_root *tree_root = root->fs_info->tree_root;
  578. old_root_used = btrfs_root_used(&root->root_item);
  579. btrfs_write_dirty_block_groups(trans, root);
  580. while (1) {
  581. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  582. if (old_root_bytenr == root->node->start &&
  583. old_root_used == btrfs_root_used(&root->root_item))
  584. break;
  585. btrfs_set_root_node(&root->root_item, root->node);
  586. ret = btrfs_update_root(trans, tree_root,
  587. &root->root_key,
  588. &root->root_item);
  589. BUG_ON(ret);
  590. old_root_used = btrfs_root_used(&root->root_item);
  591. ret = btrfs_write_dirty_block_groups(trans, root);
  592. BUG_ON(ret);
  593. }
  594. if (root != root->fs_info->extent_root)
  595. switch_commit_root(root);
  596. return 0;
  597. }
  598. /*
  599. * update all the cowonly tree roots on disk
  600. */
  601. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  602. struct btrfs_root *root)
  603. {
  604. struct btrfs_fs_info *fs_info = root->fs_info;
  605. struct list_head *next;
  606. struct extent_buffer *eb;
  607. int ret;
  608. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  609. BUG_ON(ret);
  610. eb = btrfs_lock_root_node(fs_info->tree_root);
  611. btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
  612. btrfs_tree_unlock(eb);
  613. free_extent_buffer(eb);
  614. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  615. BUG_ON(ret);
  616. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  617. next = fs_info->dirty_cowonly_roots.next;
  618. list_del_init(next);
  619. root = list_entry(next, struct btrfs_root, dirty_list);
  620. update_cowonly_root(trans, root);
  621. }
  622. down_write(&fs_info->extent_commit_sem);
  623. switch_commit_root(fs_info->extent_root);
  624. up_write(&fs_info->extent_commit_sem);
  625. return 0;
  626. }
  627. /*
  628. * dead roots are old snapshots that need to be deleted. This allocates
  629. * a dirty root struct and adds it into the list of dead roots that need to
  630. * be deleted
  631. */
  632. int btrfs_add_dead_root(struct btrfs_root *root)
  633. {
  634. mutex_lock(&root->fs_info->trans_mutex);
  635. list_add(&root->root_list, &root->fs_info->dead_roots);
  636. mutex_unlock(&root->fs_info->trans_mutex);
  637. return 0;
  638. }
  639. /*
  640. * update all the cowonly tree roots on disk
  641. */
  642. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  643. struct btrfs_root *root)
  644. {
  645. struct btrfs_root *gang[8];
  646. struct btrfs_fs_info *fs_info = root->fs_info;
  647. int i;
  648. int ret;
  649. int err = 0;
  650. while (1) {
  651. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  652. (void **)gang, 0,
  653. ARRAY_SIZE(gang),
  654. BTRFS_ROOT_TRANS_TAG);
  655. if (ret == 0)
  656. break;
  657. for (i = 0; i < ret; i++) {
  658. root = gang[i];
  659. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  660. (unsigned long)root->root_key.objectid,
  661. BTRFS_ROOT_TRANS_TAG);
  662. btrfs_free_log(trans, root);
  663. btrfs_update_reloc_root(trans, root);
  664. btrfs_orphan_commit_root(trans, root);
  665. if (root->commit_root != root->node) {
  666. switch_commit_root(root);
  667. btrfs_set_root_node(&root->root_item,
  668. root->node);
  669. }
  670. err = btrfs_update_root(trans, fs_info->tree_root,
  671. &root->root_key,
  672. &root->root_item);
  673. if (err)
  674. break;
  675. }
  676. }
  677. return err;
  678. }
  679. /*
  680. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  681. * otherwise every leaf in the btree is read and defragged.
  682. */
  683. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  684. {
  685. struct btrfs_fs_info *info = root->fs_info;
  686. struct btrfs_trans_handle *trans;
  687. int ret;
  688. unsigned long nr;
  689. if (xchg(&root->defrag_running, 1))
  690. return 0;
  691. while (1) {
  692. trans = btrfs_start_transaction(root, 0);
  693. if (IS_ERR(trans))
  694. return PTR_ERR(trans);
  695. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  696. nr = trans->blocks_used;
  697. btrfs_end_transaction(trans, root);
  698. btrfs_btree_balance_dirty(info->tree_root, nr);
  699. cond_resched();
  700. if (root->fs_info->closing || ret != -EAGAIN)
  701. break;
  702. }
  703. root->defrag_running = 0;
  704. return ret;
  705. }
  706. #if 0
  707. /*
  708. * when dropping snapshots, we generate a ton of delayed refs, and it makes
  709. * sense not to join the transaction while it is trying to flush the current
  710. * queue of delayed refs out.
  711. *
  712. * This is used by the drop snapshot code only
  713. */
  714. static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
  715. {
  716. DEFINE_WAIT(wait);
  717. mutex_lock(&info->trans_mutex);
  718. while (info->running_transaction &&
  719. info->running_transaction->delayed_refs.flushing) {
  720. prepare_to_wait(&info->transaction_wait, &wait,
  721. TASK_UNINTERRUPTIBLE);
  722. mutex_unlock(&info->trans_mutex);
  723. schedule();
  724. mutex_lock(&info->trans_mutex);
  725. finish_wait(&info->transaction_wait, &wait);
  726. }
  727. mutex_unlock(&info->trans_mutex);
  728. return 0;
  729. }
  730. /*
  731. * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
  732. * all of them
  733. */
  734. int btrfs_drop_dead_root(struct btrfs_root *root)
  735. {
  736. struct btrfs_trans_handle *trans;
  737. struct btrfs_root *tree_root = root->fs_info->tree_root;
  738. unsigned long nr;
  739. int ret;
  740. while (1) {
  741. /*
  742. * we don't want to jump in and create a bunch of
  743. * delayed refs if the transaction is starting to close
  744. */
  745. wait_transaction_pre_flush(tree_root->fs_info);
  746. trans = btrfs_start_transaction(tree_root, 1);
  747. /*
  748. * we've joined a transaction, make sure it isn't
  749. * closing right now
  750. */
  751. if (trans->transaction->delayed_refs.flushing) {
  752. btrfs_end_transaction(trans, tree_root);
  753. continue;
  754. }
  755. ret = btrfs_drop_snapshot(trans, root);
  756. if (ret != -EAGAIN)
  757. break;
  758. ret = btrfs_update_root(trans, tree_root,
  759. &root->root_key,
  760. &root->root_item);
  761. if (ret)
  762. break;
  763. nr = trans->blocks_used;
  764. ret = btrfs_end_transaction(trans, tree_root);
  765. BUG_ON(ret);
  766. btrfs_btree_balance_dirty(tree_root, nr);
  767. cond_resched();
  768. }
  769. BUG_ON(ret);
  770. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  771. BUG_ON(ret);
  772. nr = trans->blocks_used;
  773. ret = btrfs_end_transaction(trans, tree_root);
  774. BUG_ON(ret);
  775. free_extent_buffer(root->node);
  776. free_extent_buffer(root->commit_root);
  777. kfree(root);
  778. btrfs_btree_balance_dirty(tree_root, nr);
  779. return ret;
  780. }
  781. #endif
  782. /*
  783. * new snapshots need to be created at a very specific time in the
  784. * transaction commit. This does the actual creation
  785. */
  786. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  787. struct btrfs_fs_info *fs_info,
  788. struct btrfs_pending_snapshot *pending)
  789. {
  790. struct btrfs_key key;
  791. struct btrfs_root_item *new_root_item;
  792. struct btrfs_root *tree_root = fs_info->tree_root;
  793. struct btrfs_root *root = pending->root;
  794. struct btrfs_root *parent_root;
  795. struct inode *parent_inode;
  796. struct dentry *parent;
  797. struct dentry *dentry;
  798. struct extent_buffer *tmp;
  799. struct extent_buffer *old;
  800. int ret;
  801. u64 to_reserve = 0;
  802. u64 index = 0;
  803. u64 objectid;
  804. u64 root_flags;
  805. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  806. if (!new_root_item) {
  807. pending->error = -ENOMEM;
  808. goto fail;
  809. }
  810. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  811. if (ret) {
  812. pending->error = ret;
  813. goto fail;
  814. }
  815. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  816. btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
  817. if (to_reserve > 0) {
  818. ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
  819. to_reserve);
  820. if (ret) {
  821. pending->error = ret;
  822. goto fail;
  823. }
  824. }
  825. key.objectid = objectid;
  826. key.offset = (u64)-1;
  827. key.type = BTRFS_ROOT_ITEM_KEY;
  828. trans->block_rsv = &pending->block_rsv;
  829. dentry = pending->dentry;
  830. parent = dget_parent(dentry);
  831. parent_inode = parent->d_inode;
  832. parent_root = BTRFS_I(parent_inode)->root;
  833. record_root_in_trans(trans, parent_root);
  834. /*
  835. * insert the directory item
  836. */
  837. ret = btrfs_set_inode_index(parent_inode, &index);
  838. BUG_ON(ret);
  839. ret = btrfs_insert_dir_item(trans, parent_root,
  840. dentry->d_name.name, dentry->d_name.len,
  841. parent_inode->i_ino, &key,
  842. BTRFS_FT_DIR, index);
  843. BUG_ON(ret);
  844. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  845. dentry->d_name.len * 2);
  846. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  847. BUG_ON(ret);
  848. record_root_in_trans(trans, root);
  849. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  850. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  851. btrfs_check_and_init_root_item(new_root_item);
  852. root_flags = btrfs_root_flags(new_root_item);
  853. if (pending->readonly)
  854. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  855. else
  856. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  857. btrfs_set_root_flags(new_root_item, root_flags);
  858. old = btrfs_lock_root_node(root);
  859. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  860. btrfs_set_lock_blocking(old);
  861. btrfs_copy_root(trans, root, old, &tmp, objectid);
  862. btrfs_tree_unlock(old);
  863. free_extent_buffer(old);
  864. btrfs_set_root_node(new_root_item, tmp);
  865. /* record when the snapshot was created in key.offset */
  866. key.offset = trans->transid;
  867. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  868. btrfs_tree_unlock(tmp);
  869. free_extent_buffer(tmp);
  870. BUG_ON(ret);
  871. /*
  872. * insert root back/forward references
  873. */
  874. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  875. parent_root->root_key.objectid,
  876. parent_inode->i_ino, index,
  877. dentry->d_name.name, dentry->d_name.len);
  878. BUG_ON(ret);
  879. dput(parent);
  880. key.offset = (u64)-1;
  881. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  882. BUG_ON(IS_ERR(pending->snap));
  883. btrfs_reloc_post_snapshot(trans, pending);
  884. btrfs_orphan_post_snapshot(trans, pending);
  885. fail:
  886. kfree(new_root_item);
  887. btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
  888. return 0;
  889. }
  890. /*
  891. * create all the snapshots we've scheduled for creation
  892. */
  893. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  894. struct btrfs_fs_info *fs_info)
  895. {
  896. struct btrfs_pending_snapshot *pending;
  897. struct list_head *head = &trans->transaction->pending_snapshots;
  898. int ret;
  899. list_for_each_entry(pending, head, list) {
  900. ret = create_pending_snapshot(trans, fs_info, pending);
  901. BUG_ON(ret);
  902. }
  903. return 0;
  904. }
  905. static void update_super_roots(struct btrfs_root *root)
  906. {
  907. struct btrfs_root_item *root_item;
  908. struct btrfs_super_block *super;
  909. super = &root->fs_info->super_copy;
  910. root_item = &root->fs_info->chunk_root->root_item;
  911. super->chunk_root = root_item->bytenr;
  912. super->chunk_root_generation = root_item->generation;
  913. super->chunk_root_level = root_item->level;
  914. root_item = &root->fs_info->tree_root->root_item;
  915. super->root = root_item->bytenr;
  916. super->generation = root_item->generation;
  917. super->root_level = root_item->level;
  918. if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
  919. super->cache_generation = root_item->generation;
  920. }
  921. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  922. {
  923. int ret = 0;
  924. spin_lock(&info->new_trans_lock);
  925. if (info->running_transaction)
  926. ret = info->running_transaction->in_commit;
  927. spin_unlock(&info->new_trans_lock);
  928. return ret;
  929. }
  930. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  931. {
  932. int ret = 0;
  933. spin_lock(&info->new_trans_lock);
  934. if (info->running_transaction)
  935. ret = info->running_transaction->blocked;
  936. spin_unlock(&info->new_trans_lock);
  937. return ret;
  938. }
  939. /*
  940. * wait for the current transaction commit to start and block subsequent
  941. * transaction joins
  942. */
  943. static void wait_current_trans_commit_start(struct btrfs_root *root,
  944. struct btrfs_transaction *trans)
  945. {
  946. DEFINE_WAIT(wait);
  947. if (trans->in_commit)
  948. return;
  949. while (1) {
  950. prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
  951. TASK_UNINTERRUPTIBLE);
  952. if (trans->in_commit) {
  953. finish_wait(&root->fs_info->transaction_blocked_wait,
  954. &wait);
  955. break;
  956. }
  957. mutex_unlock(&root->fs_info->trans_mutex);
  958. schedule();
  959. mutex_lock(&root->fs_info->trans_mutex);
  960. finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
  961. }
  962. }
  963. /*
  964. * wait for the current transaction to start and then become unblocked.
  965. * caller holds ref.
  966. */
  967. static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  968. struct btrfs_transaction *trans)
  969. {
  970. DEFINE_WAIT(wait);
  971. if (trans->commit_done || (trans->in_commit && !trans->blocked))
  972. return;
  973. while (1) {
  974. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  975. TASK_UNINTERRUPTIBLE);
  976. if (trans->commit_done ||
  977. (trans->in_commit && !trans->blocked)) {
  978. finish_wait(&root->fs_info->transaction_wait,
  979. &wait);
  980. break;
  981. }
  982. mutex_unlock(&root->fs_info->trans_mutex);
  983. schedule();
  984. mutex_lock(&root->fs_info->trans_mutex);
  985. finish_wait(&root->fs_info->transaction_wait,
  986. &wait);
  987. }
  988. }
  989. /*
  990. * commit transactions asynchronously. once btrfs_commit_transaction_async
  991. * returns, any subsequent transaction will not be allowed to join.
  992. */
  993. struct btrfs_async_commit {
  994. struct btrfs_trans_handle *newtrans;
  995. struct btrfs_root *root;
  996. struct delayed_work work;
  997. };
  998. static void do_async_commit(struct work_struct *work)
  999. {
  1000. struct btrfs_async_commit *ac =
  1001. container_of(work, struct btrfs_async_commit, work.work);
  1002. btrfs_commit_transaction(ac->newtrans, ac->root);
  1003. kfree(ac);
  1004. }
  1005. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1006. struct btrfs_root *root,
  1007. int wait_for_unblock)
  1008. {
  1009. struct btrfs_async_commit *ac;
  1010. struct btrfs_transaction *cur_trans;
  1011. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1012. if (!ac)
  1013. return -ENOMEM;
  1014. INIT_DELAYED_WORK(&ac->work, do_async_commit);
  1015. ac->root = root;
  1016. ac->newtrans = btrfs_join_transaction(root, 0);
  1017. if (IS_ERR(ac->newtrans)) {
  1018. int err = PTR_ERR(ac->newtrans);
  1019. kfree(ac);
  1020. return err;
  1021. }
  1022. /* take transaction reference */
  1023. mutex_lock(&root->fs_info->trans_mutex);
  1024. cur_trans = trans->transaction;
  1025. cur_trans->use_count++;
  1026. mutex_unlock(&root->fs_info->trans_mutex);
  1027. btrfs_end_transaction(trans, root);
  1028. schedule_delayed_work(&ac->work, 0);
  1029. /* wait for transaction to start and unblock */
  1030. mutex_lock(&root->fs_info->trans_mutex);
  1031. if (wait_for_unblock)
  1032. wait_current_trans_commit_start_and_unblock(root, cur_trans);
  1033. else
  1034. wait_current_trans_commit_start(root, cur_trans);
  1035. put_transaction(cur_trans);
  1036. mutex_unlock(&root->fs_info->trans_mutex);
  1037. return 0;
  1038. }
  1039. /*
  1040. * btrfs_transaction state sequence:
  1041. * in_commit = 0, blocked = 0 (initial)
  1042. * in_commit = 1, blocked = 1
  1043. * blocked = 0
  1044. * commit_done = 1
  1045. */
  1046. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  1047. struct btrfs_root *root)
  1048. {
  1049. unsigned long joined = 0;
  1050. struct btrfs_transaction *cur_trans;
  1051. struct btrfs_transaction *prev_trans = NULL;
  1052. DEFINE_WAIT(wait);
  1053. int ret;
  1054. int should_grow = 0;
  1055. unsigned long now = get_seconds();
  1056. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  1057. btrfs_run_ordered_operations(root, 0);
  1058. /* make a pass through all the delayed refs we have so far
  1059. * any runnings procs may add more while we are here
  1060. */
  1061. ret = btrfs_run_delayed_refs(trans, root, 0);
  1062. BUG_ON(ret);
  1063. btrfs_trans_release_metadata(trans, root);
  1064. cur_trans = trans->transaction;
  1065. /*
  1066. * set the flushing flag so procs in this transaction have to
  1067. * start sending their work down.
  1068. */
  1069. cur_trans->delayed_refs.flushing = 1;
  1070. ret = btrfs_run_delayed_refs(trans, root, 0);
  1071. BUG_ON(ret);
  1072. mutex_lock(&root->fs_info->trans_mutex);
  1073. if (cur_trans->in_commit) {
  1074. cur_trans->use_count++;
  1075. mutex_unlock(&root->fs_info->trans_mutex);
  1076. btrfs_end_transaction(trans, root);
  1077. ret = wait_for_commit(root, cur_trans);
  1078. BUG_ON(ret);
  1079. mutex_lock(&root->fs_info->trans_mutex);
  1080. put_transaction(cur_trans);
  1081. mutex_unlock(&root->fs_info->trans_mutex);
  1082. return 0;
  1083. }
  1084. trans->transaction->in_commit = 1;
  1085. trans->transaction->blocked = 1;
  1086. wake_up(&root->fs_info->transaction_blocked_wait);
  1087. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  1088. prev_trans = list_entry(cur_trans->list.prev,
  1089. struct btrfs_transaction, list);
  1090. if (!prev_trans->commit_done) {
  1091. prev_trans->use_count++;
  1092. mutex_unlock(&root->fs_info->trans_mutex);
  1093. wait_for_commit(root, prev_trans);
  1094. mutex_lock(&root->fs_info->trans_mutex);
  1095. put_transaction(prev_trans);
  1096. }
  1097. }
  1098. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  1099. should_grow = 1;
  1100. do {
  1101. int snap_pending = 0;
  1102. joined = cur_trans->num_joined;
  1103. if (!list_empty(&trans->transaction->pending_snapshots))
  1104. snap_pending = 1;
  1105. WARN_ON(cur_trans != trans->transaction);
  1106. mutex_unlock(&root->fs_info->trans_mutex);
  1107. if (flush_on_commit || snap_pending) {
  1108. btrfs_start_delalloc_inodes(root, 1);
  1109. ret = btrfs_wait_ordered_extents(root, 0, 1);
  1110. BUG_ON(ret);
  1111. }
  1112. /*
  1113. * rename don't use btrfs_join_transaction, so, once we
  1114. * set the transaction to blocked above, we aren't going
  1115. * to get any new ordered operations. We can safely run
  1116. * it here and no for sure that nothing new will be added
  1117. * to the list
  1118. */
  1119. btrfs_run_ordered_operations(root, 1);
  1120. prepare_to_wait(&cur_trans->writer_wait, &wait,
  1121. TASK_UNINTERRUPTIBLE);
  1122. smp_mb();
  1123. if (cur_trans->num_writers > 1)
  1124. schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  1125. else if (should_grow)
  1126. schedule_timeout(1);
  1127. mutex_lock(&root->fs_info->trans_mutex);
  1128. finish_wait(&cur_trans->writer_wait, &wait);
  1129. } while (cur_trans->num_writers > 1 ||
  1130. (should_grow && cur_trans->num_joined != joined));
  1131. ret = create_pending_snapshots(trans, root->fs_info);
  1132. BUG_ON(ret);
  1133. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1134. BUG_ON(ret);
  1135. WARN_ON(cur_trans != trans->transaction);
  1136. /* btrfs_commit_tree_roots is responsible for getting the
  1137. * various roots consistent with each other. Every pointer
  1138. * in the tree of tree roots has to point to the most up to date
  1139. * root for every subvolume and other tree. So, we have to keep
  1140. * the tree logging code from jumping in and changing any
  1141. * of the trees.
  1142. *
  1143. * At this point in the commit, there can't be any tree-log
  1144. * writers, but a little lower down we drop the trans mutex
  1145. * and let new people in. By holding the tree_log_mutex
  1146. * from now until after the super is written, we avoid races
  1147. * with the tree-log code.
  1148. */
  1149. mutex_lock(&root->fs_info->tree_log_mutex);
  1150. ret = commit_fs_roots(trans, root);
  1151. BUG_ON(ret);
  1152. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1153. * safe to free the root of tree log roots
  1154. */
  1155. btrfs_free_log_root_tree(trans, root->fs_info);
  1156. ret = commit_cowonly_roots(trans, root);
  1157. BUG_ON(ret);
  1158. btrfs_prepare_extent_commit(trans, root);
  1159. cur_trans = root->fs_info->running_transaction;
  1160. spin_lock(&root->fs_info->new_trans_lock);
  1161. root->fs_info->running_transaction = NULL;
  1162. spin_unlock(&root->fs_info->new_trans_lock);
  1163. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  1164. root->fs_info->tree_root->node);
  1165. switch_commit_root(root->fs_info->tree_root);
  1166. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  1167. root->fs_info->chunk_root->node);
  1168. switch_commit_root(root->fs_info->chunk_root);
  1169. update_super_roots(root);
  1170. if (!root->fs_info->log_root_recovering) {
  1171. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  1172. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  1173. }
  1174. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  1175. sizeof(root->fs_info->super_copy));
  1176. trans->transaction->blocked = 0;
  1177. wake_up(&root->fs_info->transaction_wait);
  1178. mutex_unlock(&root->fs_info->trans_mutex);
  1179. ret = btrfs_write_and_wait_transaction(trans, root);
  1180. BUG_ON(ret);
  1181. write_ctree_super(trans, root, 0);
  1182. /*
  1183. * the super is written, we can safely allow the tree-loggers
  1184. * to go about their business
  1185. */
  1186. mutex_unlock(&root->fs_info->tree_log_mutex);
  1187. btrfs_finish_extent_commit(trans, root);
  1188. mutex_lock(&root->fs_info->trans_mutex);
  1189. cur_trans->commit_done = 1;
  1190. root->fs_info->last_trans_committed = cur_trans->transid;
  1191. wake_up(&cur_trans->commit_wait);
  1192. put_transaction(cur_trans);
  1193. put_transaction(cur_trans);
  1194. trace_btrfs_transaction_commit(root);
  1195. mutex_unlock(&root->fs_info->trans_mutex);
  1196. if (current->journal_info == trans)
  1197. current->journal_info = NULL;
  1198. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1199. if (current != root->fs_info->transaction_kthread)
  1200. btrfs_run_delayed_iputs(root);
  1201. return ret;
  1202. }
  1203. /*
  1204. * interface function to delete all the snapshots we have scheduled for deletion
  1205. */
  1206. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1207. {
  1208. LIST_HEAD(list);
  1209. struct btrfs_fs_info *fs_info = root->fs_info;
  1210. mutex_lock(&fs_info->trans_mutex);
  1211. list_splice_init(&fs_info->dead_roots, &list);
  1212. mutex_unlock(&fs_info->trans_mutex);
  1213. while (!list_empty(&list)) {
  1214. root = list_entry(list.next, struct btrfs_root, root_list);
  1215. list_del(&root->root_list);
  1216. if (btrfs_header_backref_rev(root->node) <
  1217. BTRFS_MIXED_BACKREF_REV)
  1218. btrfs_drop_snapshot(root, NULL, 0);
  1219. else
  1220. btrfs_drop_snapshot(root, NULL, 1);
  1221. }
  1222. return 0;
  1223. }