transaction.c 54 KB

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