transaction.c 37 KB

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