transaction.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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. #include "dev-replace.h"
  33. #define BTRFS_ROOT_TRANS_TAG 0
  34. static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
  35. [TRANS_STATE_RUNNING] = 0U,
  36. [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE |
  37. __TRANS_START),
  38. [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE |
  39. __TRANS_START |
  40. __TRANS_ATTACH),
  41. [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE |
  42. __TRANS_START |
  43. __TRANS_ATTACH |
  44. __TRANS_JOIN),
  45. [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE |
  46. __TRANS_START |
  47. __TRANS_ATTACH |
  48. __TRANS_JOIN |
  49. __TRANS_JOIN_NOLOCK),
  50. [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE |
  51. __TRANS_START |
  52. __TRANS_ATTACH |
  53. __TRANS_JOIN |
  54. __TRANS_JOIN_NOLOCK),
  55. };
  56. static void put_transaction(struct btrfs_transaction *transaction)
  57. {
  58. WARN_ON(atomic_read(&transaction->use_count) == 0);
  59. if (atomic_dec_and_test(&transaction->use_count)) {
  60. BUG_ON(!list_empty(&transaction->list));
  61. WARN_ON(transaction->delayed_refs.root.rb_node);
  62. while (!list_empty(&transaction->pending_chunks)) {
  63. struct extent_map *em;
  64. em = list_first_entry(&transaction->pending_chunks,
  65. struct extent_map, list);
  66. list_del_init(&em->list);
  67. free_extent_map(em);
  68. }
  69. kmem_cache_free(btrfs_transaction_cachep, transaction);
  70. }
  71. }
  72. static noinline void switch_commit_root(struct btrfs_root *root)
  73. {
  74. free_extent_buffer(root->commit_root);
  75. root->commit_root = btrfs_root_node(root);
  76. }
  77. static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
  78. unsigned int type)
  79. {
  80. if (type & TRANS_EXTWRITERS)
  81. atomic_inc(&trans->num_extwriters);
  82. }
  83. static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
  84. unsigned int type)
  85. {
  86. if (type & TRANS_EXTWRITERS)
  87. atomic_dec(&trans->num_extwriters);
  88. }
  89. static inline void extwriter_counter_init(struct btrfs_transaction *trans,
  90. unsigned int type)
  91. {
  92. atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
  93. }
  94. static inline int extwriter_counter_read(struct btrfs_transaction *trans)
  95. {
  96. return atomic_read(&trans->num_extwriters);
  97. }
  98. /*
  99. * either allocate a new transaction or hop into the existing one
  100. */
  101. static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
  102. {
  103. struct btrfs_transaction *cur_trans;
  104. struct btrfs_fs_info *fs_info = root->fs_info;
  105. spin_lock(&fs_info->trans_lock);
  106. loop:
  107. /* The file system has been taken offline. No new transactions. */
  108. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  109. spin_unlock(&fs_info->trans_lock);
  110. return -EROFS;
  111. }
  112. cur_trans = fs_info->running_transaction;
  113. if (cur_trans) {
  114. if (cur_trans->aborted) {
  115. spin_unlock(&fs_info->trans_lock);
  116. return cur_trans->aborted;
  117. }
  118. if (btrfs_blocked_trans_types[cur_trans->state] & type) {
  119. spin_unlock(&fs_info->trans_lock);
  120. return -EBUSY;
  121. }
  122. atomic_inc(&cur_trans->use_count);
  123. atomic_inc(&cur_trans->num_writers);
  124. extwriter_counter_inc(cur_trans, type);
  125. spin_unlock(&fs_info->trans_lock);
  126. return 0;
  127. }
  128. spin_unlock(&fs_info->trans_lock);
  129. /*
  130. * If we are ATTACH, we just want to catch the current transaction,
  131. * and commit it. If there is no transaction, just return ENOENT.
  132. */
  133. if (type == TRANS_ATTACH)
  134. return -ENOENT;
  135. /*
  136. * JOIN_NOLOCK only happens during the transaction commit, so
  137. * it is impossible that ->running_transaction is NULL
  138. */
  139. BUG_ON(type == TRANS_JOIN_NOLOCK);
  140. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
  141. if (!cur_trans)
  142. return -ENOMEM;
  143. spin_lock(&fs_info->trans_lock);
  144. if (fs_info->running_transaction) {
  145. /*
  146. * someone started a transaction after we unlocked. Make sure
  147. * to redo the checks above
  148. */
  149. kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  150. goto loop;
  151. } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  152. spin_unlock(&fs_info->trans_lock);
  153. kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  154. return -EROFS;
  155. }
  156. atomic_set(&cur_trans->num_writers, 1);
  157. extwriter_counter_init(cur_trans, type);
  158. init_waitqueue_head(&cur_trans->writer_wait);
  159. init_waitqueue_head(&cur_trans->commit_wait);
  160. cur_trans->state = TRANS_STATE_RUNNING;
  161. /*
  162. * One for this trans handle, one so it will live on until we
  163. * commit the transaction.
  164. */
  165. atomic_set(&cur_trans->use_count, 2);
  166. cur_trans->start_time = get_seconds();
  167. cur_trans->delayed_refs.root = RB_ROOT;
  168. cur_trans->delayed_refs.num_entries = 0;
  169. cur_trans->delayed_refs.num_heads_ready = 0;
  170. cur_trans->delayed_refs.num_heads = 0;
  171. cur_trans->delayed_refs.flushing = 0;
  172. cur_trans->delayed_refs.run_delayed_start = 0;
  173. /*
  174. * although the tree mod log is per file system and not per transaction,
  175. * the log must never go across transaction boundaries.
  176. */
  177. smp_mb();
  178. if (!list_empty(&fs_info->tree_mod_seq_list))
  179. WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
  180. "creating a fresh transaction\n");
  181. if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
  182. WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
  183. "creating a fresh transaction\n");
  184. atomic64_set(&fs_info->tree_mod_seq, 0);
  185. spin_lock_init(&cur_trans->delayed_refs.lock);
  186. atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
  187. atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
  188. init_waitqueue_head(&cur_trans->delayed_refs.wait);
  189. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  190. INIT_LIST_HEAD(&cur_trans->ordered_operations);
  191. INIT_LIST_HEAD(&cur_trans->pending_chunks);
  192. list_add_tail(&cur_trans->list, &fs_info->trans_list);
  193. extent_io_tree_init(&cur_trans->dirty_pages,
  194. fs_info->btree_inode->i_mapping);
  195. fs_info->generation++;
  196. cur_trans->transid = fs_info->generation;
  197. fs_info->running_transaction = cur_trans;
  198. cur_trans->aborted = 0;
  199. spin_unlock(&fs_info->trans_lock);
  200. return 0;
  201. }
  202. /*
  203. * this does all the record keeping required to make sure that a reference
  204. * counted root is properly recorded in a given transaction. This is required
  205. * to make sure the old root from before we joined the transaction is deleted
  206. * when the transaction commits
  207. */
  208. static int record_root_in_trans(struct btrfs_trans_handle *trans,
  209. struct btrfs_root *root)
  210. {
  211. if (root->ref_cows && root->last_trans < trans->transid) {
  212. WARN_ON(root == root->fs_info->extent_root);
  213. WARN_ON(root->commit_root != root->node);
  214. /*
  215. * see below for in_trans_setup usage rules
  216. * we have the reloc mutex held now, so there
  217. * is only one writer in this function
  218. */
  219. root->in_trans_setup = 1;
  220. /* make sure readers find in_trans_setup before
  221. * they find our root->last_trans update
  222. */
  223. smp_wmb();
  224. spin_lock(&root->fs_info->fs_roots_radix_lock);
  225. if (root->last_trans == trans->transid) {
  226. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  227. return 0;
  228. }
  229. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  230. (unsigned long)root->root_key.objectid,
  231. BTRFS_ROOT_TRANS_TAG);
  232. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  233. root->last_trans = trans->transid;
  234. /* this is pretty tricky. We don't want to
  235. * take the relocation lock in btrfs_record_root_in_trans
  236. * unless we're really doing the first setup for this root in
  237. * this transaction.
  238. *
  239. * Normally we'd use root->last_trans as a flag to decide
  240. * if we want to take the expensive mutex.
  241. *
  242. * But, we have to set root->last_trans before we
  243. * init the relocation root, otherwise, we trip over warnings
  244. * in ctree.c. The solution used here is to flag ourselves
  245. * with root->in_trans_setup. When this is 1, we're still
  246. * fixing up the reloc trees and everyone must wait.
  247. *
  248. * When this is zero, they can trust root->last_trans and fly
  249. * through btrfs_record_root_in_trans without having to take the
  250. * lock. smp_wmb() makes sure that all the writes above are
  251. * done before we pop in the zero below
  252. */
  253. btrfs_init_reloc_root(trans, root);
  254. smp_wmb();
  255. root->in_trans_setup = 0;
  256. }
  257. return 0;
  258. }
  259. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  260. struct btrfs_root *root)
  261. {
  262. if (!root->ref_cows)
  263. return 0;
  264. /*
  265. * see record_root_in_trans for comments about in_trans_setup usage
  266. * and barriers
  267. */
  268. smp_rmb();
  269. if (root->last_trans == trans->transid &&
  270. !root->in_trans_setup)
  271. return 0;
  272. mutex_lock(&root->fs_info->reloc_mutex);
  273. record_root_in_trans(trans, root);
  274. mutex_unlock(&root->fs_info->reloc_mutex);
  275. return 0;
  276. }
  277. static inline int is_transaction_blocked(struct btrfs_transaction *trans)
  278. {
  279. return (trans->state >= TRANS_STATE_BLOCKED &&
  280. trans->state < TRANS_STATE_UNBLOCKED &&
  281. !trans->aborted);
  282. }
  283. /* wait for commit against the current transaction to become unblocked
  284. * when this is done, it is safe to start a new transaction, but the current
  285. * transaction might not be fully on disk.
  286. */
  287. static void wait_current_trans(struct btrfs_root *root)
  288. {
  289. struct btrfs_transaction *cur_trans;
  290. spin_lock(&root->fs_info->trans_lock);
  291. cur_trans = root->fs_info->running_transaction;
  292. if (cur_trans && is_transaction_blocked(cur_trans)) {
  293. atomic_inc(&cur_trans->use_count);
  294. spin_unlock(&root->fs_info->trans_lock);
  295. wait_event(root->fs_info->transaction_wait,
  296. cur_trans->state >= TRANS_STATE_UNBLOCKED ||
  297. cur_trans->aborted);
  298. put_transaction(cur_trans);
  299. } else {
  300. spin_unlock(&root->fs_info->trans_lock);
  301. }
  302. }
  303. static int may_wait_transaction(struct btrfs_root *root, int type)
  304. {
  305. if (root->fs_info->log_root_recovering)
  306. return 0;
  307. if (type == TRANS_USERSPACE)
  308. return 1;
  309. if (type == TRANS_START &&
  310. !atomic_read(&root->fs_info->open_ioctl_trans))
  311. return 1;
  312. return 0;
  313. }
  314. static struct btrfs_trans_handle *
  315. start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
  316. enum btrfs_reserve_flush_enum flush)
  317. {
  318. struct btrfs_trans_handle *h;
  319. struct btrfs_transaction *cur_trans;
  320. u64 num_bytes = 0;
  321. int ret;
  322. u64 qgroup_reserved = 0;
  323. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  324. return ERR_PTR(-EROFS);
  325. if (current->journal_info) {
  326. WARN_ON(type & TRANS_EXTWRITERS);
  327. h = current->journal_info;
  328. h->use_count++;
  329. WARN_ON(h->use_count > 2);
  330. h->orig_rsv = h->block_rsv;
  331. h->block_rsv = NULL;
  332. goto got_it;
  333. }
  334. /*
  335. * Do the reservation before we join the transaction so we can do all
  336. * the appropriate flushing if need be.
  337. */
  338. if (num_items > 0 && root != root->fs_info->chunk_root) {
  339. if (root->fs_info->quota_enabled &&
  340. is_fstree(root->root_key.objectid)) {
  341. qgroup_reserved = num_items * root->leafsize;
  342. ret = btrfs_qgroup_reserve(root, qgroup_reserved);
  343. if (ret)
  344. return ERR_PTR(ret);
  345. }
  346. num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
  347. ret = btrfs_block_rsv_add(root,
  348. &root->fs_info->trans_block_rsv,
  349. num_bytes, flush);
  350. if (ret)
  351. goto reserve_fail;
  352. }
  353. again:
  354. h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  355. if (!h) {
  356. ret = -ENOMEM;
  357. goto alloc_fail;
  358. }
  359. /*
  360. * If we are JOIN_NOLOCK we're already committing a transaction and
  361. * waiting on this guy, so we don't need to do the sb_start_intwrite
  362. * because we're already holding a ref. We need this because we could
  363. * have raced in and did an fsync() on a file which can kick a commit
  364. * and then we deadlock with somebody doing a freeze.
  365. *
  366. * If we are ATTACH, it means we just want to catch the current
  367. * transaction and commit it, so we needn't do sb_start_intwrite().
  368. */
  369. if (type & __TRANS_FREEZABLE)
  370. sb_start_intwrite(root->fs_info->sb);
  371. if (may_wait_transaction(root, type))
  372. wait_current_trans(root);
  373. do {
  374. ret = join_transaction(root, type);
  375. if (ret == -EBUSY) {
  376. wait_current_trans(root);
  377. if (unlikely(type == TRANS_ATTACH))
  378. ret = -ENOENT;
  379. }
  380. } while (ret == -EBUSY);
  381. if (ret < 0) {
  382. /* We must get the transaction if we are JOIN_NOLOCK. */
  383. BUG_ON(type == TRANS_JOIN_NOLOCK);
  384. goto join_fail;
  385. }
  386. cur_trans = root->fs_info->running_transaction;
  387. h->transid = cur_trans->transid;
  388. h->transaction = cur_trans;
  389. h->blocks_used = 0;
  390. h->bytes_reserved = 0;
  391. h->root = root;
  392. h->delayed_ref_updates = 0;
  393. h->use_count = 1;
  394. h->adding_csums = 0;
  395. h->block_rsv = NULL;
  396. h->orig_rsv = NULL;
  397. h->aborted = 0;
  398. h->qgroup_reserved = 0;
  399. h->delayed_ref_elem.seq = 0;
  400. h->type = type;
  401. h->allocating_chunk = false;
  402. INIT_LIST_HEAD(&h->qgroup_ref_list);
  403. INIT_LIST_HEAD(&h->new_bgs);
  404. smp_mb();
  405. if (cur_trans->state >= TRANS_STATE_BLOCKED &&
  406. may_wait_transaction(root, type)) {
  407. btrfs_commit_transaction(h, root);
  408. goto again;
  409. }
  410. if (num_bytes) {
  411. trace_btrfs_space_reservation(root->fs_info, "transaction",
  412. h->transid, num_bytes, 1);
  413. h->block_rsv = &root->fs_info->trans_block_rsv;
  414. h->bytes_reserved = num_bytes;
  415. }
  416. h->qgroup_reserved = qgroup_reserved;
  417. got_it:
  418. btrfs_record_root_in_trans(h, root);
  419. if (!current->journal_info && type != TRANS_USERSPACE)
  420. current->journal_info = h;
  421. return h;
  422. join_fail:
  423. if (type & __TRANS_FREEZABLE)
  424. sb_end_intwrite(root->fs_info->sb);
  425. kmem_cache_free(btrfs_trans_handle_cachep, h);
  426. alloc_fail:
  427. if (num_bytes)
  428. btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
  429. num_bytes);
  430. reserve_fail:
  431. if (qgroup_reserved)
  432. btrfs_qgroup_free(root, qgroup_reserved);
  433. return ERR_PTR(ret);
  434. }
  435. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  436. int num_items)
  437. {
  438. return start_transaction(root, num_items, TRANS_START,
  439. BTRFS_RESERVE_FLUSH_ALL);
  440. }
  441. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  442. struct btrfs_root *root, int num_items)
  443. {
  444. return start_transaction(root, num_items, TRANS_START,
  445. BTRFS_RESERVE_FLUSH_LIMIT);
  446. }
  447. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  448. {
  449. return start_transaction(root, 0, TRANS_JOIN, 0);
  450. }
  451. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
  452. {
  453. return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
  454. }
  455. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
  456. {
  457. return start_transaction(root, 0, TRANS_USERSPACE, 0);
  458. }
  459. /*
  460. * btrfs_attach_transaction() - catch the running transaction
  461. *
  462. * It is used when we want to commit the current the transaction, but
  463. * don't want to start a new one.
  464. *
  465. * Note: If this function return -ENOENT, it just means there is no
  466. * running transaction. But it is possible that the inactive transaction
  467. * is still in the memory, not fully on disk. If you hope there is no
  468. * inactive transaction in the fs when -ENOENT is returned, you should
  469. * invoke
  470. * btrfs_attach_transaction_barrier()
  471. */
  472. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  473. {
  474. return start_transaction(root, 0, TRANS_ATTACH, 0);
  475. }
  476. /*
  477. * btrfs_attach_transaction_barrier() - catch the running transaction
  478. *
  479. * It is similar to the above function, the differentia is this one
  480. * will wait for all the inactive transactions until they fully
  481. * complete.
  482. */
  483. struct btrfs_trans_handle *
  484. btrfs_attach_transaction_barrier(struct btrfs_root *root)
  485. {
  486. struct btrfs_trans_handle *trans;
  487. trans = start_transaction(root, 0, TRANS_ATTACH, 0);
  488. if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
  489. btrfs_wait_for_commit(root, 0);
  490. return trans;
  491. }
  492. /* wait for a transaction commit to be fully complete */
  493. static noinline void wait_for_commit(struct btrfs_root *root,
  494. struct btrfs_transaction *commit)
  495. {
  496. wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
  497. }
  498. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
  499. {
  500. struct btrfs_transaction *cur_trans = NULL, *t;
  501. int ret = 0;
  502. if (transid) {
  503. if (transid <= root->fs_info->last_trans_committed)
  504. goto out;
  505. ret = -EINVAL;
  506. /* find specified transaction */
  507. spin_lock(&root->fs_info->trans_lock);
  508. list_for_each_entry(t, &root->fs_info->trans_list, list) {
  509. if (t->transid == transid) {
  510. cur_trans = t;
  511. atomic_inc(&cur_trans->use_count);
  512. ret = 0;
  513. break;
  514. }
  515. if (t->transid > transid) {
  516. ret = 0;
  517. break;
  518. }
  519. }
  520. spin_unlock(&root->fs_info->trans_lock);
  521. /* The specified transaction doesn't exist */
  522. if (!cur_trans)
  523. goto out;
  524. } else {
  525. /* find newest transaction that is committing | committed */
  526. spin_lock(&root->fs_info->trans_lock);
  527. list_for_each_entry_reverse(t, &root->fs_info->trans_list,
  528. list) {
  529. if (t->state >= TRANS_STATE_COMMIT_START) {
  530. if (t->state == TRANS_STATE_COMPLETED)
  531. break;
  532. cur_trans = t;
  533. atomic_inc(&cur_trans->use_count);
  534. break;
  535. }
  536. }
  537. spin_unlock(&root->fs_info->trans_lock);
  538. if (!cur_trans)
  539. goto out; /* nothing committing|committed */
  540. }
  541. wait_for_commit(root, cur_trans);
  542. put_transaction(cur_trans);
  543. out:
  544. return ret;
  545. }
  546. void btrfs_throttle(struct btrfs_root *root)
  547. {
  548. if (!atomic_read(&root->fs_info->open_ioctl_trans))
  549. wait_current_trans(root);
  550. }
  551. static int should_end_transaction(struct btrfs_trans_handle *trans,
  552. struct btrfs_root *root)
  553. {
  554. if (root->fs_info->global_block_rsv.space_info->full &&
  555. btrfs_should_throttle_delayed_refs(trans, root))
  556. return 1;
  557. return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
  558. }
  559. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  560. struct btrfs_root *root)
  561. {
  562. struct btrfs_transaction *cur_trans = trans->transaction;
  563. int updates;
  564. int err;
  565. smp_mb();
  566. if (cur_trans->state >= TRANS_STATE_BLOCKED ||
  567. cur_trans->delayed_refs.flushing)
  568. return 1;
  569. updates = trans->delayed_ref_updates;
  570. trans->delayed_ref_updates = 0;
  571. if (updates) {
  572. err = btrfs_run_delayed_refs(trans, root, updates);
  573. if (err) /* Error code will also eval true */
  574. return err;
  575. }
  576. return should_end_transaction(trans, root);
  577. }
  578. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  579. struct btrfs_root *root, int throttle)
  580. {
  581. struct btrfs_transaction *cur_trans = trans->transaction;
  582. struct btrfs_fs_info *info = root->fs_info;
  583. unsigned long cur = trans->delayed_ref_updates;
  584. int lock = (trans->type != TRANS_JOIN_NOLOCK);
  585. int err = 0;
  586. if (--trans->use_count) {
  587. trans->block_rsv = trans->orig_rsv;
  588. return 0;
  589. }
  590. /*
  591. * do the qgroup accounting as early as possible
  592. */
  593. err = btrfs_delayed_refs_qgroup_accounting(trans, info);
  594. btrfs_trans_release_metadata(trans, root);
  595. trans->block_rsv = NULL;
  596. if (trans->qgroup_reserved) {
  597. /*
  598. * the same root has to be passed here between start_transaction
  599. * and end_transaction. Subvolume quota depends on this.
  600. */
  601. btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
  602. trans->qgroup_reserved = 0;
  603. }
  604. if (!list_empty(&trans->new_bgs))
  605. btrfs_create_pending_block_groups(trans, root);
  606. trans->delayed_ref_updates = 0;
  607. if (btrfs_should_throttle_delayed_refs(trans, root)) {
  608. cur = max_t(unsigned long, cur, 1);
  609. trans->delayed_ref_updates = 0;
  610. btrfs_run_delayed_refs(trans, root, cur);
  611. }
  612. btrfs_trans_release_metadata(trans, root);
  613. trans->block_rsv = NULL;
  614. if (!list_empty(&trans->new_bgs))
  615. btrfs_create_pending_block_groups(trans, root);
  616. if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
  617. should_end_transaction(trans, root) &&
  618. ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
  619. spin_lock(&info->trans_lock);
  620. if (cur_trans->state == TRANS_STATE_RUNNING)
  621. cur_trans->state = TRANS_STATE_BLOCKED;
  622. spin_unlock(&info->trans_lock);
  623. }
  624. if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
  625. if (throttle) {
  626. /*
  627. * We may race with somebody else here so end up having
  628. * to call end_transaction on ourselves again, so inc
  629. * our use_count.
  630. */
  631. trans->use_count++;
  632. return btrfs_commit_transaction(trans, root);
  633. } else {
  634. wake_up_process(info->transaction_kthread);
  635. }
  636. }
  637. if (trans->type & __TRANS_FREEZABLE)
  638. sb_end_intwrite(root->fs_info->sb);
  639. WARN_ON(cur_trans != info->running_transaction);
  640. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  641. atomic_dec(&cur_trans->num_writers);
  642. extwriter_counter_dec(cur_trans, trans->type);
  643. smp_mb();
  644. if (waitqueue_active(&cur_trans->writer_wait))
  645. wake_up(&cur_trans->writer_wait);
  646. put_transaction(cur_trans);
  647. if (current->journal_info == trans)
  648. current->journal_info = NULL;
  649. if (throttle)
  650. btrfs_run_delayed_iputs(root);
  651. if (trans->aborted ||
  652. test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  653. err = -EIO;
  654. assert_qgroups_uptodate(trans);
  655. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  656. return err;
  657. }
  658. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  659. struct btrfs_root *root)
  660. {
  661. return __btrfs_end_transaction(trans, root, 0);
  662. }
  663. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  664. struct btrfs_root *root)
  665. {
  666. return __btrfs_end_transaction(trans, root, 1);
  667. }
  668. int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
  669. struct btrfs_root *root)
  670. {
  671. return __btrfs_end_transaction(trans, root, 1);
  672. }
  673. /*
  674. * when btree blocks are allocated, they have some corresponding bits set for
  675. * them in one of two extent_io trees. This is used to make sure all of
  676. * those extents are sent to disk but does not wait on them
  677. */
  678. int btrfs_write_marked_extents(struct btrfs_root *root,
  679. struct extent_io_tree *dirty_pages, int mark)
  680. {
  681. int err = 0;
  682. int werr = 0;
  683. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  684. struct extent_state *cached_state = NULL;
  685. u64 start = 0;
  686. u64 end;
  687. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  688. mark, &cached_state)) {
  689. convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
  690. mark, &cached_state, GFP_NOFS);
  691. cached_state = NULL;
  692. err = filemap_fdatawrite_range(mapping, start, end);
  693. if (err)
  694. werr = err;
  695. cond_resched();
  696. start = end + 1;
  697. }
  698. if (err)
  699. werr = err;
  700. return werr;
  701. }
  702. /*
  703. * when btree blocks are allocated, they have some corresponding bits set for
  704. * them in one of two extent_io trees. This is used to make sure all of
  705. * those extents are on disk for transaction or log commit. We wait
  706. * on all the pages and clear them from the dirty pages state tree
  707. */
  708. int btrfs_wait_marked_extents(struct btrfs_root *root,
  709. struct extent_io_tree *dirty_pages, int mark)
  710. {
  711. int err = 0;
  712. int werr = 0;
  713. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  714. struct extent_state *cached_state = NULL;
  715. u64 start = 0;
  716. u64 end;
  717. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  718. EXTENT_NEED_WAIT, &cached_state)) {
  719. clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
  720. 0, 0, &cached_state, GFP_NOFS);
  721. err = filemap_fdatawait_range(mapping, start, end);
  722. if (err)
  723. werr = err;
  724. cond_resched();
  725. start = end + 1;
  726. }
  727. if (err)
  728. werr = err;
  729. return werr;
  730. }
  731. /*
  732. * when btree blocks are allocated, they have some corresponding bits set for
  733. * them in one of two extent_io trees. This is used to make sure all of
  734. * those extents are on disk for transaction or log commit
  735. */
  736. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  737. struct extent_io_tree *dirty_pages, int mark)
  738. {
  739. int ret;
  740. int ret2;
  741. struct blk_plug plug;
  742. blk_start_plug(&plug);
  743. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  744. blk_finish_plug(&plug);
  745. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  746. if (ret)
  747. return ret;
  748. if (ret2)
  749. return ret2;
  750. return 0;
  751. }
  752. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  753. struct btrfs_root *root)
  754. {
  755. if (!trans || !trans->transaction) {
  756. struct inode *btree_inode;
  757. btree_inode = root->fs_info->btree_inode;
  758. return filemap_write_and_wait(btree_inode->i_mapping);
  759. }
  760. return btrfs_write_and_wait_marked_extents(root,
  761. &trans->transaction->dirty_pages,
  762. EXTENT_DIRTY);
  763. }
  764. /*
  765. * this is used to update the root pointer in the tree of tree roots.
  766. *
  767. * But, in the case of the extent allocation tree, updating the root
  768. * pointer may allocate blocks which may change the root of the extent
  769. * allocation tree.
  770. *
  771. * So, this loops and repeats and makes sure the cowonly root didn't
  772. * change while the root pointer was being updated in the metadata.
  773. */
  774. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  775. struct btrfs_root *root)
  776. {
  777. int ret;
  778. u64 old_root_bytenr;
  779. u64 old_root_used;
  780. struct btrfs_root *tree_root = root->fs_info->tree_root;
  781. old_root_used = btrfs_root_used(&root->root_item);
  782. btrfs_write_dirty_block_groups(trans, root);
  783. while (1) {
  784. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  785. if (old_root_bytenr == root->node->start &&
  786. old_root_used == btrfs_root_used(&root->root_item))
  787. break;
  788. btrfs_set_root_node(&root->root_item, root->node);
  789. ret = btrfs_update_root(trans, tree_root,
  790. &root->root_key,
  791. &root->root_item);
  792. if (ret)
  793. return ret;
  794. old_root_used = btrfs_root_used(&root->root_item);
  795. ret = btrfs_write_dirty_block_groups(trans, root);
  796. if (ret)
  797. return ret;
  798. }
  799. if (root != root->fs_info->extent_root)
  800. switch_commit_root(root);
  801. return 0;
  802. }
  803. /*
  804. * update all the cowonly tree roots on disk
  805. *
  806. * The error handling in this function may not be obvious. Any of the
  807. * failures will cause the file system to go offline. We still need
  808. * to clean up the delayed refs.
  809. */
  810. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  811. struct btrfs_root *root)
  812. {
  813. struct btrfs_fs_info *fs_info = root->fs_info;
  814. struct list_head *next;
  815. struct extent_buffer *eb;
  816. int ret;
  817. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  818. if (ret)
  819. return ret;
  820. eb = btrfs_lock_root_node(fs_info->tree_root);
  821. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  822. 0, &eb);
  823. btrfs_tree_unlock(eb);
  824. free_extent_buffer(eb);
  825. if (ret)
  826. return ret;
  827. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  828. if (ret)
  829. return ret;
  830. ret = btrfs_run_dev_stats(trans, root->fs_info);
  831. WARN_ON(ret);
  832. ret = btrfs_run_dev_replace(trans, root->fs_info);
  833. WARN_ON(ret);
  834. ret = btrfs_run_qgroups(trans, root->fs_info);
  835. BUG_ON(ret);
  836. /* run_qgroups might have added some more refs */
  837. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  838. BUG_ON(ret);
  839. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  840. next = fs_info->dirty_cowonly_roots.next;
  841. list_del_init(next);
  842. root = list_entry(next, struct btrfs_root, dirty_list);
  843. ret = update_cowonly_root(trans, root);
  844. if (ret)
  845. return ret;
  846. }
  847. down_write(&fs_info->extent_commit_sem);
  848. switch_commit_root(fs_info->extent_root);
  849. up_write(&fs_info->extent_commit_sem);
  850. btrfs_after_dev_replace_commit(fs_info);
  851. return 0;
  852. }
  853. /*
  854. * dead roots are old snapshots that need to be deleted. This allocates
  855. * a dirty root struct and adds it into the list of dead roots that need to
  856. * be deleted
  857. */
  858. void btrfs_add_dead_root(struct btrfs_root *root)
  859. {
  860. spin_lock(&root->fs_info->trans_lock);
  861. if (list_empty(&root->root_list))
  862. list_add_tail(&root->root_list, &root->fs_info->dead_roots);
  863. spin_unlock(&root->fs_info->trans_lock);
  864. }
  865. /*
  866. * update all the cowonly tree roots on disk
  867. */
  868. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  869. struct btrfs_root *root)
  870. {
  871. struct btrfs_root *gang[8];
  872. struct btrfs_fs_info *fs_info = root->fs_info;
  873. int i;
  874. int ret;
  875. int err = 0;
  876. spin_lock(&fs_info->fs_roots_radix_lock);
  877. while (1) {
  878. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  879. (void **)gang, 0,
  880. ARRAY_SIZE(gang),
  881. BTRFS_ROOT_TRANS_TAG);
  882. if (ret == 0)
  883. break;
  884. for (i = 0; i < ret; i++) {
  885. root = gang[i];
  886. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  887. (unsigned long)root->root_key.objectid,
  888. BTRFS_ROOT_TRANS_TAG);
  889. spin_unlock(&fs_info->fs_roots_radix_lock);
  890. btrfs_free_log(trans, root);
  891. btrfs_update_reloc_root(trans, root);
  892. btrfs_orphan_commit_root(trans, root);
  893. btrfs_save_ino_cache(root, trans);
  894. /* see comments in should_cow_block() */
  895. root->force_cow = 0;
  896. smp_wmb();
  897. if (root->commit_root != root->node) {
  898. mutex_lock(&root->fs_commit_mutex);
  899. switch_commit_root(root);
  900. btrfs_unpin_free_ino(root);
  901. mutex_unlock(&root->fs_commit_mutex);
  902. btrfs_set_root_node(&root->root_item,
  903. root->node);
  904. }
  905. err = btrfs_update_root(trans, fs_info->tree_root,
  906. &root->root_key,
  907. &root->root_item);
  908. spin_lock(&fs_info->fs_roots_radix_lock);
  909. if (err)
  910. break;
  911. }
  912. }
  913. spin_unlock(&fs_info->fs_roots_radix_lock);
  914. return err;
  915. }
  916. /*
  917. * defrag a given btree.
  918. * Every leaf in the btree is read and defragged.
  919. */
  920. int btrfs_defrag_root(struct btrfs_root *root)
  921. {
  922. struct btrfs_fs_info *info = root->fs_info;
  923. struct btrfs_trans_handle *trans;
  924. int ret;
  925. if (xchg(&root->defrag_running, 1))
  926. return 0;
  927. while (1) {
  928. trans = btrfs_start_transaction(root, 0);
  929. if (IS_ERR(trans))
  930. return PTR_ERR(trans);
  931. ret = btrfs_defrag_leaves(trans, root);
  932. btrfs_end_transaction(trans, root);
  933. btrfs_btree_balance_dirty(info->tree_root);
  934. cond_resched();
  935. if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
  936. break;
  937. if (btrfs_defrag_cancelled(root->fs_info)) {
  938. printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
  939. ret = -EAGAIN;
  940. break;
  941. }
  942. }
  943. root->defrag_running = 0;
  944. return ret;
  945. }
  946. /*
  947. * new snapshots need to be created at a very specific time in the
  948. * transaction commit. This does the actual creation.
  949. *
  950. * Note:
  951. * If the error which may affect the commitment of the current transaction
  952. * happens, we should return the error number. If the error which just affect
  953. * the creation of the pending snapshots, just return 0.
  954. */
  955. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  956. struct btrfs_fs_info *fs_info,
  957. struct btrfs_pending_snapshot *pending)
  958. {
  959. struct btrfs_key key;
  960. struct btrfs_root_item *new_root_item;
  961. struct btrfs_root *tree_root = fs_info->tree_root;
  962. struct btrfs_root *root = pending->root;
  963. struct btrfs_root *parent_root;
  964. struct btrfs_block_rsv *rsv;
  965. struct inode *parent_inode;
  966. struct btrfs_path *path;
  967. struct btrfs_dir_item *dir_item;
  968. struct dentry *dentry;
  969. struct extent_buffer *tmp;
  970. struct extent_buffer *old;
  971. struct timespec cur_time = CURRENT_TIME;
  972. int ret = 0;
  973. u64 to_reserve = 0;
  974. u64 index = 0;
  975. u64 objectid;
  976. u64 root_flags;
  977. uuid_le new_uuid;
  978. path = btrfs_alloc_path();
  979. if (!path) {
  980. pending->error = -ENOMEM;
  981. return 0;
  982. }
  983. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  984. if (!new_root_item) {
  985. pending->error = -ENOMEM;
  986. goto root_item_alloc_fail;
  987. }
  988. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  989. if (pending->error)
  990. goto no_free_objectid;
  991. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  992. if (to_reserve > 0) {
  993. pending->error = btrfs_block_rsv_add(root,
  994. &pending->block_rsv,
  995. to_reserve,
  996. BTRFS_RESERVE_NO_FLUSH);
  997. if (pending->error)
  998. goto no_free_objectid;
  999. }
  1000. pending->error = btrfs_qgroup_inherit(trans, fs_info,
  1001. root->root_key.objectid,
  1002. objectid, pending->inherit);
  1003. if (pending->error)
  1004. goto no_free_objectid;
  1005. key.objectid = objectid;
  1006. key.offset = (u64)-1;
  1007. key.type = BTRFS_ROOT_ITEM_KEY;
  1008. rsv = trans->block_rsv;
  1009. trans->block_rsv = &pending->block_rsv;
  1010. trans->bytes_reserved = trans->block_rsv->reserved;
  1011. dentry = pending->dentry;
  1012. parent_inode = pending->dir;
  1013. parent_root = BTRFS_I(parent_inode)->root;
  1014. record_root_in_trans(trans, parent_root);
  1015. /*
  1016. * insert the directory item
  1017. */
  1018. ret = btrfs_set_inode_index(parent_inode, &index);
  1019. BUG_ON(ret); /* -ENOMEM */
  1020. /* check if there is a file/dir which has the same name. */
  1021. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1022. btrfs_ino(parent_inode),
  1023. dentry->d_name.name,
  1024. dentry->d_name.len, 0);
  1025. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1026. pending->error = -EEXIST;
  1027. goto dir_item_existed;
  1028. } else if (IS_ERR(dir_item)) {
  1029. ret = PTR_ERR(dir_item);
  1030. btrfs_abort_transaction(trans, root, ret);
  1031. goto fail;
  1032. }
  1033. btrfs_release_path(path);
  1034. /*
  1035. * pull in the delayed directory update
  1036. * and the delayed inode item
  1037. * otherwise we corrupt the FS during
  1038. * snapshot
  1039. */
  1040. ret = btrfs_run_delayed_items(trans, root);
  1041. if (ret) { /* Transaction aborted */
  1042. btrfs_abort_transaction(trans, root, ret);
  1043. goto fail;
  1044. }
  1045. record_root_in_trans(trans, root);
  1046. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1047. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1048. btrfs_check_and_init_root_item(new_root_item);
  1049. root_flags = btrfs_root_flags(new_root_item);
  1050. if (pending->readonly)
  1051. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1052. else
  1053. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1054. btrfs_set_root_flags(new_root_item, root_flags);
  1055. btrfs_set_root_generation_v2(new_root_item,
  1056. trans->transid);
  1057. uuid_le_gen(&new_uuid);
  1058. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  1059. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1060. BTRFS_UUID_SIZE);
  1061. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1062. memset(new_root_item->received_uuid, 0,
  1063. sizeof(new_root_item->received_uuid));
  1064. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1065. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1066. btrfs_set_root_stransid(new_root_item, 0);
  1067. btrfs_set_root_rtransid(new_root_item, 0);
  1068. }
  1069. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1070. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1071. btrfs_set_root_otransid(new_root_item, trans->transid);
  1072. old = btrfs_lock_root_node(root);
  1073. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  1074. if (ret) {
  1075. btrfs_tree_unlock(old);
  1076. free_extent_buffer(old);
  1077. btrfs_abort_transaction(trans, root, ret);
  1078. goto fail;
  1079. }
  1080. btrfs_set_lock_blocking(old);
  1081. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1082. /* clean up in any case */
  1083. btrfs_tree_unlock(old);
  1084. free_extent_buffer(old);
  1085. if (ret) {
  1086. btrfs_abort_transaction(trans, root, ret);
  1087. goto fail;
  1088. }
  1089. /* see comments in should_cow_block() */
  1090. root->force_cow = 1;
  1091. smp_wmb();
  1092. btrfs_set_root_node(new_root_item, tmp);
  1093. /* record when the snapshot was created in key.offset */
  1094. key.offset = trans->transid;
  1095. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1096. btrfs_tree_unlock(tmp);
  1097. free_extent_buffer(tmp);
  1098. if (ret) {
  1099. btrfs_abort_transaction(trans, root, ret);
  1100. goto fail;
  1101. }
  1102. /*
  1103. * insert root back/forward references
  1104. */
  1105. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  1106. parent_root->root_key.objectid,
  1107. btrfs_ino(parent_inode), index,
  1108. dentry->d_name.name, dentry->d_name.len);
  1109. if (ret) {
  1110. btrfs_abort_transaction(trans, root, ret);
  1111. goto fail;
  1112. }
  1113. key.offset = (u64)-1;
  1114. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  1115. if (IS_ERR(pending->snap)) {
  1116. ret = PTR_ERR(pending->snap);
  1117. btrfs_abort_transaction(trans, root, ret);
  1118. goto fail;
  1119. }
  1120. ret = btrfs_reloc_post_snapshot(trans, pending);
  1121. if (ret) {
  1122. btrfs_abort_transaction(trans, root, ret);
  1123. goto fail;
  1124. }
  1125. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1126. if (ret) {
  1127. btrfs_abort_transaction(trans, root, ret);
  1128. goto fail;
  1129. }
  1130. ret = btrfs_insert_dir_item(trans, parent_root,
  1131. dentry->d_name.name, dentry->d_name.len,
  1132. parent_inode, &key,
  1133. BTRFS_FT_DIR, index);
  1134. /* We have check then name at the beginning, so it is impossible. */
  1135. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1136. if (ret) {
  1137. btrfs_abort_transaction(trans, root, ret);
  1138. goto fail;
  1139. }
  1140. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  1141. dentry->d_name.len * 2);
  1142. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  1143. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1144. if (ret)
  1145. btrfs_abort_transaction(trans, root, ret);
  1146. fail:
  1147. pending->error = ret;
  1148. dir_item_existed:
  1149. trans->block_rsv = rsv;
  1150. trans->bytes_reserved = 0;
  1151. no_free_objectid:
  1152. kfree(new_root_item);
  1153. root_item_alloc_fail:
  1154. btrfs_free_path(path);
  1155. return ret;
  1156. }
  1157. /*
  1158. * create all the snapshots we've scheduled for creation
  1159. */
  1160. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  1161. struct btrfs_fs_info *fs_info)
  1162. {
  1163. struct btrfs_pending_snapshot *pending, *next;
  1164. struct list_head *head = &trans->transaction->pending_snapshots;
  1165. int ret = 0;
  1166. list_for_each_entry_safe(pending, next, head, list) {
  1167. list_del(&pending->list);
  1168. ret = create_pending_snapshot(trans, fs_info, pending);
  1169. if (ret)
  1170. break;
  1171. }
  1172. return ret;
  1173. }
  1174. static void update_super_roots(struct btrfs_root *root)
  1175. {
  1176. struct btrfs_root_item *root_item;
  1177. struct btrfs_super_block *super;
  1178. super = root->fs_info->super_copy;
  1179. root_item = &root->fs_info->chunk_root->root_item;
  1180. super->chunk_root = root_item->bytenr;
  1181. super->chunk_root_generation = root_item->generation;
  1182. super->chunk_root_level = root_item->level;
  1183. root_item = &root->fs_info->tree_root->root_item;
  1184. super->root = root_item->bytenr;
  1185. super->generation = root_item->generation;
  1186. super->root_level = root_item->level;
  1187. if (btrfs_test_opt(root, SPACE_CACHE))
  1188. super->cache_generation = root_item->generation;
  1189. }
  1190. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1191. {
  1192. struct btrfs_transaction *trans;
  1193. int ret = 0;
  1194. spin_lock(&info->trans_lock);
  1195. trans = info->running_transaction;
  1196. if (trans)
  1197. ret = (trans->state >= TRANS_STATE_COMMIT_START);
  1198. spin_unlock(&info->trans_lock);
  1199. return ret;
  1200. }
  1201. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1202. {
  1203. struct btrfs_transaction *trans;
  1204. int ret = 0;
  1205. spin_lock(&info->trans_lock);
  1206. trans = info->running_transaction;
  1207. if (trans)
  1208. ret = is_transaction_blocked(trans);
  1209. spin_unlock(&info->trans_lock);
  1210. return ret;
  1211. }
  1212. /*
  1213. * wait for the current transaction commit to start and block subsequent
  1214. * transaction joins
  1215. */
  1216. static void wait_current_trans_commit_start(struct btrfs_root *root,
  1217. struct btrfs_transaction *trans)
  1218. {
  1219. wait_event(root->fs_info->transaction_blocked_wait,
  1220. trans->state >= TRANS_STATE_COMMIT_START ||
  1221. trans->aborted);
  1222. }
  1223. /*
  1224. * wait for the current transaction to start and then become unblocked.
  1225. * caller holds ref.
  1226. */
  1227. static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  1228. struct btrfs_transaction *trans)
  1229. {
  1230. wait_event(root->fs_info->transaction_wait,
  1231. trans->state >= TRANS_STATE_UNBLOCKED ||
  1232. trans->aborted);
  1233. }
  1234. /*
  1235. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1236. * returns, any subsequent transaction will not be allowed to join.
  1237. */
  1238. struct btrfs_async_commit {
  1239. struct btrfs_trans_handle *newtrans;
  1240. struct btrfs_root *root;
  1241. struct work_struct work;
  1242. };
  1243. static void do_async_commit(struct work_struct *work)
  1244. {
  1245. struct btrfs_async_commit *ac =
  1246. container_of(work, struct btrfs_async_commit, work);
  1247. /*
  1248. * We've got freeze protection passed with the transaction.
  1249. * Tell lockdep about it.
  1250. */
  1251. if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
  1252. rwsem_acquire_read(
  1253. &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
  1254. 0, 1, _THIS_IP_);
  1255. current->journal_info = ac->newtrans;
  1256. btrfs_commit_transaction(ac->newtrans, ac->root);
  1257. kfree(ac);
  1258. }
  1259. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1260. struct btrfs_root *root,
  1261. int wait_for_unblock)
  1262. {
  1263. struct btrfs_async_commit *ac;
  1264. struct btrfs_transaction *cur_trans;
  1265. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1266. if (!ac)
  1267. return -ENOMEM;
  1268. INIT_WORK(&ac->work, do_async_commit);
  1269. ac->root = root;
  1270. ac->newtrans = btrfs_join_transaction(root);
  1271. if (IS_ERR(ac->newtrans)) {
  1272. int err = PTR_ERR(ac->newtrans);
  1273. kfree(ac);
  1274. return err;
  1275. }
  1276. /* take transaction reference */
  1277. cur_trans = trans->transaction;
  1278. atomic_inc(&cur_trans->use_count);
  1279. btrfs_end_transaction(trans, root);
  1280. /*
  1281. * Tell lockdep we've released the freeze rwsem, since the
  1282. * async commit thread will be the one to unlock it.
  1283. */
  1284. if (trans->type < TRANS_JOIN_NOLOCK)
  1285. rwsem_release(
  1286. &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
  1287. 1, _THIS_IP_);
  1288. schedule_work(&ac->work);
  1289. /* wait for transaction to start and unblock */
  1290. if (wait_for_unblock)
  1291. wait_current_trans_commit_start_and_unblock(root, cur_trans);
  1292. else
  1293. wait_current_trans_commit_start(root, cur_trans);
  1294. if (current->journal_info == trans)
  1295. current->journal_info = NULL;
  1296. put_transaction(cur_trans);
  1297. return 0;
  1298. }
  1299. static void cleanup_transaction(struct btrfs_trans_handle *trans,
  1300. struct btrfs_root *root, int err)
  1301. {
  1302. struct btrfs_transaction *cur_trans = trans->transaction;
  1303. DEFINE_WAIT(wait);
  1304. WARN_ON(trans->use_count > 1);
  1305. btrfs_abort_transaction(trans, root, err);
  1306. spin_lock(&root->fs_info->trans_lock);
  1307. /*
  1308. * If the transaction is removed from the list, it means this
  1309. * transaction has been committed successfully, so it is impossible
  1310. * to call the cleanup function.
  1311. */
  1312. BUG_ON(list_empty(&cur_trans->list));
  1313. list_del_init(&cur_trans->list);
  1314. if (cur_trans == root->fs_info->running_transaction) {
  1315. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1316. spin_unlock(&root->fs_info->trans_lock);
  1317. wait_event(cur_trans->writer_wait,
  1318. atomic_read(&cur_trans->num_writers) == 1);
  1319. spin_lock(&root->fs_info->trans_lock);
  1320. }
  1321. spin_unlock(&root->fs_info->trans_lock);
  1322. btrfs_cleanup_one_transaction(trans->transaction, root);
  1323. spin_lock(&root->fs_info->trans_lock);
  1324. if (cur_trans == root->fs_info->running_transaction)
  1325. root->fs_info->running_transaction = NULL;
  1326. spin_unlock(&root->fs_info->trans_lock);
  1327. put_transaction(cur_trans);
  1328. put_transaction(cur_trans);
  1329. trace_btrfs_transaction_commit(root);
  1330. btrfs_scrub_continue(root);
  1331. if (current->journal_info == trans)
  1332. current->journal_info = NULL;
  1333. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1334. }
  1335. static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
  1336. struct btrfs_root *root)
  1337. {
  1338. int ret;
  1339. ret = btrfs_run_delayed_items(trans, root);
  1340. if (ret)
  1341. return ret;
  1342. /*
  1343. * running the delayed items may have added new refs. account
  1344. * them now so that they hinder processing of more delayed refs
  1345. * as little as possible.
  1346. */
  1347. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  1348. /*
  1349. * rename don't use btrfs_join_transaction, so, once we
  1350. * set the transaction to blocked above, we aren't going
  1351. * to get any new ordered operations. We can safely run
  1352. * it here and no for sure that nothing new will be added
  1353. * to the list
  1354. */
  1355. ret = btrfs_run_ordered_operations(trans, root, 1);
  1356. return ret;
  1357. }
  1358. static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  1359. {
  1360. if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
  1361. return btrfs_start_all_delalloc_inodes(fs_info, 1);
  1362. return 0;
  1363. }
  1364. static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  1365. {
  1366. if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
  1367. btrfs_wait_all_ordered_extents(fs_info, 1);
  1368. }
  1369. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  1370. struct btrfs_root *root)
  1371. {
  1372. struct btrfs_transaction *cur_trans = trans->transaction;
  1373. struct btrfs_transaction *prev_trans = NULL;
  1374. int ret;
  1375. ret = btrfs_run_ordered_operations(trans, root, 0);
  1376. if (ret) {
  1377. btrfs_abort_transaction(trans, root, ret);
  1378. btrfs_end_transaction(trans, root);
  1379. return ret;
  1380. }
  1381. /* Stop the commit early if ->aborted is set */
  1382. if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  1383. ret = cur_trans->aborted;
  1384. btrfs_end_transaction(trans, root);
  1385. return ret;
  1386. }
  1387. /* make a pass through all the delayed refs we have so far
  1388. * any runnings procs may add more while we are here
  1389. */
  1390. ret = btrfs_run_delayed_refs(trans, root, 0);
  1391. if (ret) {
  1392. btrfs_end_transaction(trans, root);
  1393. return ret;
  1394. }
  1395. btrfs_trans_release_metadata(trans, root);
  1396. trans->block_rsv = NULL;
  1397. if (trans->qgroup_reserved) {
  1398. btrfs_qgroup_free(root, trans->qgroup_reserved);
  1399. trans->qgroup_reserved = 0;
  1400. }
  1401. cur_trans = trans->transaction;
  1402. /*
  1403. * set the flushing flag so procs in this transaction have to
  1404. * start sending their work down.
  1405. */
  1406. cur_trans->delayed_refs.flushing = 1;
  1407. smp_wmb();
  1408. if (!list_empty(&trans->new_bgs))
  1409. btrfs_create_pending_block_groups(trans, root);
  1410. ret = btrfs_run_delayed_refs(trans, root, 0);
  1411. if (ret) {
  1412. btrfs_end_transaction(trans, root);
  1413. return ret;
  1414. }
  1415. spin_lock(&root->fs_info->trans_lock);
  1416. if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  1417. spin_unlock(&root->fs_info->trans_lock);
  1418. atomic_inc(&cur_trans->use_count);
  1419. ret = btrfs_end_transaction(trans, root);
  1420. wait_for_commit(root, cur_trans);
  1421. put_transaction(cur_trans);
  1422. return ret;
  1423. }
  1424. cur_trans->state = TRANS_STATE_COMMIT_START;
  1425. wake_up(&root->fs_info->transaction_blocked_wait);
  1426. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  1427. prev_trans = list_entry(cur_trans->list.prev,
  1428. struct btrfs_transaction, list);
  1429. if (prev_trans->state != TRANS_STATE_COMPLETED) {
  1430. atomic_inc(&prev_trans->use_count);
  1431. spin_unlock(&root->fs_info->trans_lock);
  1432. wait_for_commit(root, prev_trans);
  1433. put_transaction(prev_trans);
  1434. } else {
  1435. spin_unlock(&root->fs_info->trans_lock);
  1436. }
  1437. } else {
  1438. spin_unlock(&root->fs_info->trans_lock);
  1439. }
  1440. extwriter_counter_dec(cur_trans, trans->type);
  1441. ret = btrfs_start_delalloc_flush(root->fs_info);
  1442. if (ret)
  1443. goto cleanup_transaction;
  1444. ret = btrfs_flush_all_pending_stuffs(trans, root);
  1445. if (ret)
  1446. goto cleanup_transaction;
  1447. wait_event(cur_trans->writer_wait,
  1448. extwriter_counter_read(cur_trans) == 0);
  1449. /* some pending stuffs might be added after the previous flush. */
  1450. ret = btrfs_flush_all_pending_stuffs(trans, root);
  1451. if (ret)
  1452. goto cleanup_transaction;
  1453. btrfs_wait_delalloc_flush(root->fs_info);
  1454. /*
  1455. * Ok now we need to make sure to block out any other joins while we
  1456. * commit the transaction. We could have started a join before setting
  1457. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  1458. */
  1459. spin_lock(&root->fs_info->trans_lock);
  1460. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1461. spin_unlock(&root->fs_info->trans_lock);
  1462. wait_event(cur_trans->writer_wait,
  1463. atomic_read(&cur_trans->num_writers) == 1);
  1464. /* ->aborted might be set after the previous check, so check it */
  1465. if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  1466. ret = cur_trans->aborted;
  1467. goto cleanup_transaction;
  1468. }
  1469. /*
  1470. * the reloc mutex makes sure that we stop
  1471. * the balancing code from coming in and moving
  1472. * extents around in the middle of the commit
  1473. */
  1474. mutex_lock(&root->fs_info->reloc_mutex);
  1475. /*
  1476. * We needn't worry about the delayed items because we will
  1477. * deal with them in create_pending_snapshot(), which is the
  1478. * core function of the snapshot creation.
  1479. */
  1480. ret = create_pending_snapshots(trans, root->fs_info);
  1481. if (ret) {
  1482. mutex_unlock(&root->fs_info->reloc_mutex);
  1483. goto cleanup_transaction;
  1484. }
  1485. /*
  1486. * We insert the dir indexes of the snapshots and update the inode
  1487. * of the snapshots' parents after the snapshot creation, so there
  1488. * are some delayed items which are not dealt with. Now deal with
  1489. * them.
  1490. *
  1491. * We needn't worry that this operation will corrupt the snapshots,
  1492. * because all the tree which are snapshoted will be forced to COW
  1493. * the nodes and leaves.
  1494. */
  1495. ret = btrfs_run_delayed_items(trans, root);
  1496. if (ret) {
  1497. mutex_unlock(&root->fs_info->reloc_mutex);
  1498. goto cleanup_transaction;
  1499. }
  1500. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1501. if (ret) {
  1502. mutex_unlock(&root->fs_info->reloc_mutex);
  1503. goto cleanup_transaction;
  1504. }
  1505. /*
  1506. * make sure none of the code above managed to slip in a
  1507. * delayed item
  1508. */
  1509. btrfs_assert_delayed_root_empty(root);
  1510. WARN_ON(cur_trans != trans->transaction);
  1511. btrfs_scrub_pause(root);
  1512. /* btrfs_commit_tree_roots is responsible for getting the
  1513. * various roots consistent with each other. Every pointer
  1514. * in the tree of tree roots has to point to the most up to date
  1515. * root for every subvolume and other tree. So, we have to keep
  1516. * the tree logging code from jumping in and changing any
  1517. * of the trees.
  1518. *
  1519. * At this point in the commit, there can't be any tree-log
  1520. * writers, but a little lower down we drop the trans mutex
  1521. * and let new people in. By holding the tree_log_mutex
  1522. * from now until after the super is written, we avoid races
  1523. * with the tree-log code.
  1524. */
  1525. mutex_lock(&root->fs_info->tree_log_mutex);
  1526. ret = commit_fs_roots(trans, root);
  1527. if (ret) {
  1528. mutex_unlock(&root->fs_info->tree_log_mutex);
  1529. mutex_unlock(&root->fs_info->reloc_mutex);
  1530. goto cleanup_transaction;
  1531. }
  1532. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1533. * safe to free the root of tree log roots
  1534. */
  1535. btrfs_free_log_root_tree(trans, root->fs_info);
  1536. ret = commit_cowonly_roots(trans, root);
  1537. if (ret) {
  1538. mutex_unlock(&root->fs_info->tree_log_mutex);
  1539. mutex_unlock(&root->fs_info->reloc_mutex);
  1540. goto cleanup_transaction;
  1541. }
  1542. /*
  1543. * The tasks which save the space cache and inode cache may also
  1544. * update ->aborted, check it.
  1545. */
  1546. if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  1547. ret = cur_trans->aborted;
  1548. mutex_unlock(&root->fs_info->tree_log_mutex);
  1549. mutex_unlock(&root->fs_info->reloc_mutex);
  1550. goto cleanup_transaction;
  1551. }
  1552. btrfs_prepare_extent_commit(trans, root);
  1553. cur_trans = root->fs_info->running_transaction;
  1554. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  1555. root->fs_info->tree_root->node);
  1556. switch_commit_root(root->fs_info->tree_root);
  1557. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  1558. root->fs_info->chunk_root->node);
  1559. switch_commit_root(root->fs_info->chunk_root);
  1560. assert_qgroups_uptodate(trans);
  1561. update_super_roots(root);
  1562. if (!root->fs_info->log_root_recovering) {
  1563. btrfs_set_super_log_root(root->fs_info->super_copy, 0);
  1564. btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
  1565. }
  1566. memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
  1567. sizeof(*root->fs_info->super_copy));
  1568. spin_lock(&root->fs_info->trans_lock);
  1569. cur_trans->state = TRANS_STATE_UNBLOCKED;
  1570. root->fs_info->running_transaction = NULL;
  1571. spin_unlock(&root->fs_info->trans_lock);
  1572. mutex_unlock(&root->fs_info->reloc_mutex);
  1573. wake_up(&root->fs_info->transaction_wait);
  1574. ret = btrfs_write_and_wait_transaction(trans, root);
  1575. if (ret) {
  1576. btrfs_error(root->fs_info, ret,
  1577. "Error while writing out transaction");
  1578. mutex_unlock(&root->fs_info->tree_log_mutex);
  1579. goto cleanup_transaction;
  1580. }
  1581. ret = write_ctree_super(trans, root, 0);
  1582. if (ret) {
  1583. mutex_unlock(&root->fs_info->tree_log_mutex);
  1584. goto cleanup_transaction;
  1585. }
  1586. /*
  1587. * the super is written, we can safely allow the tree-loggers
  1588. * to go about their business
  1589. */
  1590. mutex_unlock(&root->fs_info->tree_log_mutex);
  1591. btrfs_finish_extent_commit(trans, root);
  1592. root->fs_info->last_trans_committed = cur_trans->transid;
  1593. /*
  1594. * We needn't acquire the lock here because there is no other task
  1595. * which can change it.
  1596. */
  1597. cur_trans->state = TRANS_STATE_COMPLETED;
  1598. wake_up(&cur_trans->commit_wait);
  1599. spin_lock(&root->fs_info->trans_lock);
  1600. list_del_init(&cur_trans->list);
  1601. spin_unlock(&root->fs_info->trans_lock);
  1602. put_transaction(cur_trans);
  1603. put_transaction(cur_trans);
  1604. if (trans->type & __TRANS_FREEZABLE)
  1605. sb_end_intwrite(root->fs_info->sb);
  1606. trace_btrfs_transaction_commit(root);
  1607. btrfs_scrub_continue(root);
  1608. if (current->journal_info == trans)
  1609. current->journal_info = NULL;
  1610. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1611. if (current != root->fs_info->transaction_kthread)
  1612. btrfs_run_delayed_iputs(root);
  1613. return ret;
  1614. cleanup_transaction:
  1615. btrfs_trans_release_metadata(trans, root);
  1616. trans->block_rsv = NULL;
  1617. if (trans->qgroup_reserved) {
  1618. btrfs_qgroup_free(root, trans->qgroup_reserved);
  1619. trans->qgroup_reserved = 0;
  1620. }
  1621. btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
  1622. if (current->journal_info == trans)
  1623. current->journal_info = NULL;
  1624. cleanup_transaction(trans, root, ret);
  1625. return ret;
  1626. }
  1627. /*
  1628. * return < 0 if error
  1629. * 0 if there are no more dead_roots at the time of call
  1630. * 1 there are more to be processed, call me again
  1631. *
  1632. * The return value indicates there are certainly more snapshots to delete, but
  1633. * if there comes a new one during processing, it may return 0. We don't mind,
  1634. * because btrfs_commit_super will poke cleaner thread and it will process it a
  1635. * few seconds later.
  1636. */
  1637. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
  1638. {
  1639. int ret;
  1640. struct btrfs_fs_info *fs_info = root->fs_info;
  1641. spin_lock(&fs_info->trans_lock);
  1642. if (list_empty(&fs_info->dead_roots)) {
  1643. spin_unlock(&fs_info->trans_lock);
  1644. return 0;
  1645. }
  1646. root = list_first_entry(&fs_info->dead_roots,
  1647. struct btrfs_root, root_list);
  1648. list_del_init(&root->root_list);
  1649. spin_unlock(&fs_info->trans_lock);
  1650. pr_debug("btrfs: cleaner removing %llu\n",
  1651. (unsigned long long)root->objectid);
  1652. btrfs_kill_all_delayed_nodes(root);
  1653. if (btrfs_header_backref_rev(root->node) <
  1654. BTRFS_MIXED_BACKREF_REV)
  1655. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  1656. else
  1657. ret = btrfs_drop_snapshot(root, NULL, 1, 0);
  1658. /*
  1659. * If we encounter a transaction abort during snapshot cleaning, we
  1660. * don't want to crash here
  1661. */
  1662. return (ret < 0) ? 0 : 1;
  1663. }