transaction.c 41 KB

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