transaction.c 40 KB

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