transaction.c 37 KB

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