transaction.c 37 KB

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