transaction.c 39 KB

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