transaction.c 37 KB

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