transaction.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/writeback.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/uuid.h>
  12. #include "misc.h"
  13. #include "ctree.h"
  14. #include "disk-io.h"
  15. #include "transaction.h"
  16. #include "locking.h"
  17. #include "tree-log.h"
  18. #include "inode-map.h"
  19. #include "volumes.h"
  20. #include "dev-replace.h"
  21. #include "qgroup.h"
  22. #include "block-group.h"
  23. #include "space-info.h"
  24. #define BTRFS_ROOT_TRANS_TAG 0
  25. /*
  26. * Transaction states and transitions
  27. *
  28. * No running transaction (fs tree blocks are not modified)
  29. * |
  30. * | To next stage:
  31. * | Call start_transaction() variants. Except btrfs_join_transaction_nostart().
  32. * V
  33. * Transaction N [[TRANS_STATE_RUNNING]]
  34. * |
  35. * | New trans handles can be attached to transaction N by calling all
  36. * | start_transaction() variants.
  37. * |
  38. * | To next stage:
  39. * | Call btrfs_commit_transaction() on any trans handle attached to
  40. * | transaction N
  41. * V
  42. * Transaction N [[TRANS_STATE_COMMIT_START]]
  43. * |
  44. * | Will wait for previous running transaction to completely finish if there
  45. * | is one
  46. * |
  47. * | Then one of the following happes:
  48. * | - Wait for all other trans handle holders to release.
  49. * | The btrfs_commit_transaction() caller will do the commit work.
  50. * | - Wait for current transaction to be committed by others.
  51. * | Other btrfs_commit_transaction() caller will do the commit work.
  52. * |
  53. * | At this stage, only btrfs_join_transaction*() variants can attach
  54. * | to this running transaction.
  55. * | All other variants will wait for current one to finish and attach to
  56. * | transaction N+1.
  57. * |
  58. * | To next stage:
  59. * | Caller is chosen to commit transaction N, and all other trans handle
  60. * | haven been released.
  61. * V
  62. * Transaction N [[TRANS_STATE_COMMIT_DOING]]
  63. * |
  64. * | The heavy lifting transaction work is started.
  65. * | From running delayed refs (modifying extent tree) to creating pending
  66. * | snapshots, running qgroups.
  67. * | In short, modify supporting trees to reflect modifications of subvolume
  68. * | trees.
  69. * |
  70. * | At this stage, all start_transaction() calls will wait for this
  71. * | transaction to finish and attach to transaction N+1.
  72. * |
  73. * | To next stage:
  74. * | Until all supporting trees are updated.
  75. * V
  76. * Transaction N [[TRANS_STATE_UNBLOCKED]]
  77. * | Transaction N+1
  78. * | All needed trees are modified, thus we only [[TRANS_STATE_RUNNING]]
  79. * | need to write them back to disk and update |
  80. * | super blocks. |
  81. * | |
  82. * | At this stage, new transaction is allowed to |
  83. * | start. |
  84. * | All new start_transaction() calls will be |
  85. * | attached to transid N+1. |
  86. * | |
  87. * | To next stage: |
  88. * | Until all tree blocks are super blocks are |
  89. * | written to block devices |
  90. * V |
  91. * Transaction N [[TRANS_STATE_COMPLETED]] V
  92. * All tree blocks and super blocks are written. Transaction N+1
  93. * This transaction is finished and all its [[TRANS_STATE_COMMIT_START]]
  94. * data structures will be cleaned up. | Life goes on
  95. */
  96. static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
  97. [TRANS_STATE_RUNNING] = 0U,
  98. [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH),
  99. [TRANS_STATE_COMMIT_DOING] = (__TRANS_START |
  100. __TRANS_ATTACH |
  101. __TRANS_JOIN |
  102. __TRANS_JOIN_NOSTART),
  103. [TRANS_STATE_UNBLOCKED] = (__TRANS_START |
  104. __TRANS_ATTACH |
  105. __TRANS_JOIN |
  106. __TRANS_JOIN_NOLOCK |
  107. __TRANS_JOIN_NOSTART),
  108. [TRANS_STATE_COMPLETED] = (__TRANS_START |
  109. __TRANS_ATTACH |
  110. __TRANS_JOIN |
  111. __TRANS_JOIN_NOLOCK |
  112. __TRANS_JOIN_NOSTART),
  113. };
  114. void btrfs_put_transaction(struct btrfs_transaction *transaction)
  115. {
  116. WARN_ON(refcount_read(&transaction->use_count) == 0);
  117. if (refcount_dec_and_test(&transaction->use_count)) {
  118. BUG_ON(!list_empty(&transaction->list));
  119. WARN_ON(!RB_EMPTY_ROOT(
  120. &transaction->delayed_refs.href_root.rb_root));
  121. WARN_ON(!RB_EMPTY_ROOT(
  122. &transaction->delayed_refs.dirty_extent_root));
  123. if (transaction->delayed_refs.pending_csums)
  124. btrfs_err(transaction->fs_info,
  125. "pending csums is %llu",
  126. transaction->delayed_refs.pending_csums);
  127. /*
  128. * If any block groups are found in ->deleted_bgs then it's
  129. * because the transaction was aborted and a commit did not
  130. * happen (things failed before writing the new superblock
  131. * and calling btrfs_finish_extent_commit()), so we can not
  132. * discard the physical locations of the block groups.
  133. */
  134. while (!list_empty(&transaction->deleted_bgs)) {
  135. struct btrfs_block_group *cache;
  136. cache = list_first_entry(&transaction->deleted_bgs,
  137. struct btrfs_block_group,
  138. bg_list);
  139. list_del_init(&cache->bg_list);
  140. btrfs_unfreeze_block_group(cache);
  141. btrfs_put_block_group(cache);
  142. }
  143. WARN_ON(!list_empty(&transaction->dev_update_list));
  144. kfree(transaction);
  145. }
  146. }
  147. static noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
  148. {
  149. struct btrfs_transaction *cur_trans = trans->transaction;
  150. struct btrfs_fs_info *fs_info = trans->fs_info;
  151. struct btrfs_root *root, *tmp;
  152. struct btrfs_caching_control *caching_ctl, *next;
  153. down_write(&fs_info->commit_root_sem);
  154. list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
  155. dirty_list) {
  156. list_del_init(&root->dirty_list);
  157. free_extent_buffer(root->commit_root);
  158. root->commit_root = btrfs_root_node(root);
  159. if (is_fstree(root->root_key.objectid))
  160. btrfs_unpin_free_ino(root);
  161. extent_io_tree_release(&root->dirty_log_pages);
  162. btrfs_qgroup_clean_swapped_blocks(root);
  163. }
  164. /* We can free old roots now. */
  165. spin_lock(&cur_trans->dropped_roots_lock);
  166. while (!list_empty(&cur_trans->dropped_roots)) {
  167. root = list_first_entry(&cur_trans->dropped_roots,
  168. struct btrfs_root, root_list);
  169. list_del_init(&root->root_list);
  170. spin_unlock(&cur_trans->dropped_roots_lock);
  171. btrfs_free_log(trans, root);
  172. btrfs_drop_and_free_fs_root(fs_info, root);
  173. spin_lock(&cur_trans->dropped_roots_lock);
  174. }
  175. spin_unlock(&cur_trans->dropped_roots_lock);
  176. /*
  177. * We have to update the last_byte_to_unpin under the commit_root_sem,
  178. * at the same time we swap out the commit roots.
  179. *
  180. * This is because we must have a real view of the last spot the caching
  181. * kthreads were while caching. Consider the following views of the
  182. * extent tree for a block group
  183. *
  184. * commit root
  185. * +----+----+----+----+----+----+----+
  186. * |\\\\| |\\\\|\\\\| |\\\\|\\\\|
  187. * +----+----+----+----+----+----+----+
  188. * 0 1 2 3 4 5 6 7
  189. *
  190. * new commit root
  191. * +----+----+----+----+----+----+----+
  192. * | | | |\\\\| | |\\\\|
  193. * +----+----+----+----+----+----+----+
  194. * 0 1 2 3 4 5 6 7
  195. *
  196. * If the cache_ctl->progress was at 3, then we are only allowed to
  197. * unpin [0,1) and [2,3], because the caching thread has already
  198. * processed those extents. We are not allowed to unpin [5,6), because
  199. * the caching thread will re-start it's search from 3, and thus find
  200. * the hole from [4,6) to add to the free space cache.
  201. */
  202. list_for_each_entry_safe(caching_ctl, next,
  203. &fs_info->caching_block_groups, list) {
  204. struct btrfs_block_group *cache = caching_ctl->block_group;
  205. if (btrfs_block_group_done(cache)) {
  206. cache->last_byte_to_unpin = (u64)-1;
  207. list_del_init(&caching_ctl->list);
  208. btrfs_put_caching_control(caching_ctl);
  209. } else {
  210. cache->last_byte_to_unpin = caching_ctl->progress;
  211. }
  212. }
  213. up_write(&fs_info->commit_root_sem);
  214. }
  215. static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
  216. unsigned int type)
  217. {
  218. if (type & TRANS_EXTWRITERS)
  219. atomic_inc(&trans->num_extwriters);
  220. }
  221. static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
  222. unsigned int type)
  223. {
  224. if (type & TRANS_EXTWRITERS)
  225. atomic_dec(&trans->num_extwriters);
  226. }
  227. static inline void extwriter_counter_init(struct btrfs_transaction *trans,
  228. unsigned int type)
  229. {
  230. atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
  231. }
  232. static inline int extwriter_counter_read(struct btrfs_transaction *trans)
  233. {
  234. return atomic_read(&trans->num_extwriters);
  235. }
  236. /*
  237. * To be called after all the new block groups attached to the transaction
  238. * handle have been created (btrfs_create_pending_block_groups()).
  239. */
  240. void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
  241. {
  242. struct btrfs_fs_info *fs_info = trans->fs_info;
  243. if (!trans->chunk_bytes_reserved)
  244. return;
  245. WARN_ON_ONCE(!list_empty(&trans->new_bgs));
  246. btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
  247. trans->chunk_bytes_reserved, NULL);
  248. trans->chunk_bytes_reserved = 0;
  249. }
  250. /*
  251. * either allocate a new transaction or hop into the existing one
  252. */
  253. static noinline int join_transaction(struct btrfs_fs_info *fs_info,
  254. unsigned int type)
  255. {
  256. struct btrfs_transaction *cur_trans;
  257. spin_lock(&fs_info->trans_lock);
  258. loop:
  259. /* The file system has been taken offline. No new transactions. */
  260. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  261. spin_unlock(&fs_info->trans_lock);
  262. return -EROFS;
  263. }
  264. cur_trans = fs_info->running_transaction;
  265. if (cur_trans) {
  266. if (TRANS_ABORTED(cur_trans)) {
  267. spin_unlock(&fs_info->trans_lock);
  268. return cur_trans->aborted;
  269. }
  270. if (btrfs_blocked_trans_types[cur_trans->state] & type) {
  271. spin_unlock(&fs_info->trans_lock);
  272. return -EBUSY;
  273. }
  274. refcount_inc(&cur_trans->use_count);
  275. atomic_inc(&cur_trans->num_writers);
  276. extwriter_counter_inc(cur_trans, type);
  277. spin_unlock(&fs_info->trans_lock);
  278. return 0;
  279. }
  280. spin_unlock(&fs_info->trans_lock);
  281. /*
  282. * If we are ATTACH, we just want to catch the current transaction,
  283. * and commit it. If there is no transaction, just return ENOENT.
  284. */
  285. if (type == TRANS_ATTACH)
  286. return -ENOENT;
  287. /*
  288. * JOIN_NOLOCK only happens during the transaction commit, so
  289. * it is impossible that ->running_transaction is NULL
  290. */
  291. BUG_ON(type == TRANS_JOIN_NOLOCK);
  292. cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
  293. if (!cur_trans)
  294. return -ENOMEM;
  295. spin_lock(&fs_info->trans_lock);
  296. if (fs_info->running_transaction) {
  297. /*
  298. * someone started a transaction after we unlocked. Make sure
  299. * to redo the checks above
  300. */
  301. kfree(cur_trans);
  302. goto loop;
  303. } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  304. spin_unlock(&fs_info->trans_lock);
  305. kfree(cur_trans);
  306. return -EROFS;
  307. }
  308. cur_trans->fs_info = fs_info;
  309. atomic_set(&cur_trans->pending_ordered, 0);
  310. init_waitqueue_head(&cur_trans->pending_wait);
  311. atomic_set(&cur_trans->num_writers, 1);
  312. extwriter_counter_init(cur_trans, type);
  313. init_waitqueue_head(&cur_trans->writer_wait);
  314. init_waitqueue_head(&cur_trans->commit_wait);
  315. cur_trans->state = TRANS_STATE_RUNNING;
  316. /*
  317. * One for this trans handle, one so it will live on until we
  318. * commit the transaction.
  319. */
  320. refcount_set(&cur_trans->use_count, 2);
  321. cur_trans->flags = 0;
  322. cur_trans->start_time = ktime_get_seconds();
  323. memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
  324. cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
  325. cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
  326. atomic_set(&cur_trans->delayed_refs.num_entries, 0);
  327. /*
  328. * although the tree mod log is per file system and not per transaction,
  329. * the log must never go across transaction boundaries.
  330. */
  331. smp_mb();
  332. if (!list_empty(&fs_info->tree_mod_seq_list))
  333. WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
  334. if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
  335. WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
  336. atomic64_set(&fs_info->tree_mod_seq, 0);
  337. spin_lock_init(&cur_trans->delayed_refs.lock);
  338. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  339. INIT_LIST_HEAD(&cur_trans->dev_update_list);
  340. INIT_LIST_HEAD(&cur_trans->switch_commits);
  341. INIT_LIST_HEAD(&cur_trans->dirty_bgs);
  342. INIT_LIST_HEAD(&cur_trans->io_bgs);
  343. INIT_LIST_HEAD(&cur_trans->dropped_roots);
  344. mutex_init(&cur_trans->cache_write_mutex);
  345. spin_lock_init(&cur_trans->dirty_bgs_lock);
  346. INIT_LIST_HEAD(&cur_trans->deleted_bgs);
  347. spin_lock_init(&cur_trans->dropped_roots_lock);
  348. list_add_tail(&cur_trans->list, &fs_info->trans_list);
  349. extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
  350. IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode);
  351. extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
  352. IO_TREE_FS_PINNED_EXTENTS, NULL);
  353. fs_info->generation++;
  354. cur_trans->transid = fs_info->generation;
  355. fs_info->running_transaction = cur_trans;
  356. cur_trans->aborted = 0;
  357. spin_unlock(&fs_info->trans_lock);
  358. return 0;
  359. }
  360. /*
  361. * This does all the record keeping required to make sure that a shareable root
  362. * is properly recorded in a given transaction. This is required to make sure
  363. * the old root from before we joined the transaction is deleted when the
  364. * transaction commits.
  365. */
  366. static int record_root_in_trans(struct btrfs_trans_handle *trans,
  367. struct btrfs_root *root,
  368. int force)
  369. {
  370. struct btrfs_fs_info *fs_info = root->fs_info;
  371. if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
  372. root->last_trans < trans->transid) || force) {
  373. WARN_ON(root == fs_info->extent_root);
  374. WARN_ON(!force && root->commit_root != root->node);
  375. /*
  376. * see below for IN_TRANS_SETUP usage rules
  377. * we have the reloc mutex held now, so there
  378. * is only one writer in this function
  379. */
  380. set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  381. /* make sure readers find IN_TRANS_SETUP before
  382. * they find our root->last_trans update
  383. */
  384. smp_wmb();
  385. spin_lock(&fs_info->fs_roots_radix_lock);
  386. if (root->last_trans == trans->transid && !force) {
  387. spin_unlock(&fs_info->fs_roots_radix_lock);
  388. return 0;
  389. }
  390. radix_tree_tag_set(&fs_info->fs_roots_radix,
  391. (unsigned long)root->root_key.objectid,
  392. BTRFS_ROOT_TRANS_TAG);
  393. spin_unlock(&fs_info->fs_roots_radix_lock);
  394. root->last_trans = trans->transid;
  395. /* this is pretty tricky. We don't want to
  396. * take the relocation lock in btrfs_record_root_in_trans
  397. * unless we're really doing the first setup for this root in
  398. * this transaction.
  399. *
  400. * Normally we'd use root->last_trans as a flag to decide
  401. * if we want to take the expensive mutex.
  402. *
  403. * But, we have to set root->last_trans before we
  404. * init the relocation root, otherwise, we trip over warnings
  405. * in ctree.c. The solution used here is to flag ourselves
  406. * with root IN_TRANS_SETUP. When this is 1, we're still
  407. * fixing up the reloc trees and everyone must wait.
  408. *
  409. * When this is zero, they can trust root->last_trans and fly
  410. * through btrfs_record_root_in_trans without having to take the
  411. * lock. smp_wmb() makes sure that all the writes above are
  412. * done before we pop in the zero below
  413. */
  414. btrfs_init_reloc_root(trans, root);
  415. smp_mb__before_atomic();
  416. clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  417. }
  418. return 0;
  419. }
  420. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  421. struct btrfs_root *root)
  422. {
  423. struct btrfs_fs_info *fs_info = root->fs_info;
  424. struct btrfs_transaction *cur_trans = trans->transaction;
  425. /* Add ourselves to the transaction dropped list */
  426. spin_lock(&cur_trans->dropped_roots_lock);
  427. list_add_tail(&root->root_list, &cur_trans->dropped_roots);
  428. spin_unlock(&cur_trans->dropped_roots_lock);
  429. /* Make sure we don't try to update the root at commit time */
  430. spin_lock(&fs_info->fs_roots_radix_lock);
  431. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  432. (unsigned long)root->root_key.objectid,
  433. BTRFS_ROOT_TRANS_TAG);
  434. spin_unlock(&fs_info->fs_roots_radix_lock);
  435. }
  436. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  437. struct btrfs_root *root)
  438. {
  439. struct btrfs_fs_info *fs_info = root->fs_info;
  440. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  441. return 0;
  442. /*
  443. * see record_root_in_trans for comments about IN_TRANS_SETUP usage
  444. * and barriers
  445. */
  446. smp_rmb();
  447. if (root->last_trans == trans->transid &&
  448. !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
  449. return 0;
  450. mutex_lock(&fs_info->reloc_mutex);
  451. record_root_in_trans(trans, root, 0);
  452. mutex_unlock(&fs_info->reloc_mutex);
  453. return 0;
  454. }
  455. static inline int is_transaction_blocked(struct btrfs_transaction *trans)
  456. {
  457. return (trans->state >= TRANS_STATE_COMMIT_START &&
  458. trans->state < TRANS_STATE_UNBLOCKED &&
  459. !TRANS_ABORTED(trans));
  460. }
  461. /* wait for commit against the current transaction to become unblocked
  462. * when this is done, it is safe to start a new transaction, but the current
  463. * transaction might not be fully on disk.
  464. */
  465. static void wait_current_trans(struct btrfs_fs_info *fs_info)
  466. {
  467. struct btrfs_transaction *cur_trans;
  468. spin_lock(&fs_info->trans_lock);
  469. cur_trans = fs_info->running_transaction;
  470. if (cur_trans && is_transaction_blocked(cur_trans)) {
  471. refcount_inc(&cur_trans->use_count);
  472. spin_unlock(&fs_info->trans_lock);
  473. wait_event(fs_info->transaction_wait,
  474. cur_trans->state >= TRANS_STATE_UNBLOCKED ||
  475. TRANS_ABORTED(cur_trans));
  476. btrfs_put_transaction(cur_trans);
  477. } else {
  478. spin_unlock(&fs_info->trans_lock);
  479. }
  480. }
  481. static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
  482. {
  483. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
  484. return 0;
  485. if (type == TRANS_START)
  486. return 1;
  487. return 0;
  488. }
  489. static inline bool need_reserve_reloc_root(struct btrfs_root *root)
  490. {
  491. struct btrfs_fs_info *fs_info = root->fs_info;
  492. if (!fs_info->reloc_ctl ||
  493. !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
  494. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  495. root->reloc_root)
  496. return false;
  497. return true;
  498. }
  499. static struct btrfs_trans_handle *
  500. start_transaction(struct btrfs_root *root, unsigned int num_items,
  501. unsigned int type, enum btrfs_reserve_flush_enum flush,
  502. bool enforce_qgroups)
  503. {
  504. struct btrfs_fs_info *fs_info = root->fs_info;
  505. struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
  506. struct btrfs_trans_handle *h;
  507. struct btrfs_transaction *cur_trans;
  508. u64 num_bytes = 0;
  509. u64 qgroup_reserved = 0;
  510. bool reloc_reserved = false;
  511. bool do_chunk_alloc = false;
  512. int ret;
  513. /* Send isn't supposed to start transactions. */
  514. ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
  515. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  516. return ERR_PTR(-EROFS);
  517. if (current->journal_info) {
  518. WARN_ON(type & TRANS_EXTWRITERS);
  519. h = current->journal_info;
  520. refcount_inc(&h->use_count);
  521. WARN_ON(refcount_read(&h->use_count) > 2);
  522. h->orig_rsv = h->block_rsv;
  523. h->block_rsv = NULL;
  524. goto got_it;
  525. }
  526. /*
  527. * Do the reservation before we join the transaction so we can do all
  528. * the appropriate flushing if need be.
  529. */
  530. if (num_items && root != fs_info->chunk_root) {
  531. struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
  532. u64 delayed_refs_bytes = 0;
  533. qgroup_reserved = num_items * fs_info->nodesize;
  534. ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
  535. enforce_qgroups);
  536. if (ret)
  537. return ERR_PTR(ret);
  538. /*
  539. * We want to reserve all the bytes we may need all at once, so
  540. * we only do 1 enospc flushing cycle per transaction start. We
  541. * accomplish this by simply assuming we'll do 2 x num_items
  542. * worth of delayed refs updates in this trans handle, and
  543. * refill that amount for whatever is missing in the reserve.
  544. */
  545. num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
  546. if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  547. delayed_refs_rsv->full == 0) {
  548. delayed_refs_bytes = num_bytes;
  549. num_bytes <<= 1;
  550. }
  551. /*
  552. * Do the reservation for the relocation root creation
  553. */
  554. if (need_reserve_reloc_root(root)) {
  555. num_bytes += fs_info->nodesize;
  556. reloc_reserved = true;
  557. }
  558. ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush);
  559. if (ret)
  560. goto reserve_fail;
  561. if (delayed_refs_bytes) {
  562. btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv,
  563. delayed_refs_bytes);
  564. num_bytes -= delayed_refs_bytes;
  565. }
  566. if (rsv->space_info->force_alloc)
  567. do_chunk_alloc = true;
  568. } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
  569. !delayed_refs_rsv->full) {
  570. /*
  571. * Some people call with btrfs_start_transaction(root, 0)
  572. * because they can be throttled, but have some other mechanism
  573. * for reserving space. We still want these guys to refill the
  574. * delayed block_rsv so just add 1 items worth of reservation
  575. * here.
  576. */
  577. ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
  578. if (ret)
  579. goto reserve_fail;
  580. }
  581. again:
  582. h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
  583. if (!h) {
  584. ret = -ENOMEM;
  585. goto alloc_fail;
  586. }
  587. /*
  588. * If we are JOIN_NOLOCK we're already committing a transaction and
  589. * waiting on this guy, so we don't need to do the sb_start_intwrite
  590. * because we're already holding a ref. We need this because we could
  591. * have raced in and did an fsync() on a file which can kick a commit
  592. * and then we deadlock with somebody doing a freeze.
  593. *
  594. * If we are ATTACH, it means we just want to catch the current
  595. * transaction and commit it, so we needn't do sb_start_intwrite().
  596. */
  597. if (type & __TRANS_FREEZABLE)
  598. sb_start_intwrite(fs_info->sb);
  599. if (may_wait_transaction(fs_info, type))
  600. wait_current_trans(fs_info);
  601. do {
  602. ret = join_transaction(fs_info, type);
  603. if (ret == -EBUSY) {
  604. wait_current_trans(fs_info);
  605. if (unlikely(type == TRANS_ATTACH ||
  606. type == TRANS_JOIN_NOSTART))
  607. ret = -ENOENT;
  608. }
  609. } while (ret == -EBUSY);
  610. if (ret < 0)
  611. goto join_fail;
  612. cur_trans = fs_info->running_transaction;
  613. h->transid = cur_trans->transid;
  614. h->transaction = cur_trans;
  615. h->root = root;
  616. refcount_set(&h->use_count, 1);
  617. h->fs_info = root->fs_info;
  618. h->type = type;
  619. h->can_flush_pending_bgs = true;
  620. INIT_LIST_HEAD(&h->new_bgs);
  621. smp_mb();
  622. if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
  623. may_wait_transaction(fs_info, type)) {
  624. current->journal_info = h;
  625. btrfs_commit_transaction(h);
  626. goto again;
  627. }
  628. if (num_bytes) {
  629. trace_btrfs_space_reservation(fs_info, "transaction",
  630. h->transid, num_bytes, 1);
  631. h->block_rsv = &fs_info->trans_block_rsv;
  632. h->bytes_reserved = num_bytes;
  633. h->reloc_reserved = reloc_reserved;
  634. }
  635. got_it:
  636. if (!current->journal_info)
  637. current->journal_info = h;
  638. /*
  639. * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
  640. * ALLOC_FORCE the first run through, and then we won't allocate for
  641. * anybody else who races in later. We don't care about the return
  642. * value here.
  643. */
  644. if (do_chunk_alloc && num_bytes) {
  645. u64 flags = h->block_rsv->space_info->flags;
  646. btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
  647. CHUNK_ALLOC_NO_FORCE);
  648. }
  649. /*
  650. * btrfs_record_root_in_trans() needs to alloc new extents, and may
  651. * call btrfs_join_transaction() while we're also starting a
  652. * transaction.
  653. *
  654. * Thus it need to be called after current->journal_info initialized,
  655. * or we can deadlock.
  656. */
  657. btrfs_record_root_in_trans(h, root);
  658. return h;
  659. join_fail:
  660. if (type & __TRANS_FREEZABLE)
  661. sb_end_intwrite(fs_info->sb);
  662. kmem_cache_free(btrfs_trans_handle_cachep, h);
  663. alloc_fail:
  664. if (num_bytes)
  665. btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
  666. num_bytes, NULL);
  667. reserve_fail:
  668. btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
  669. return ERR_PTR(ret);
  670. }
  671. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  672. unsigned int num_items)
  673. {
  674. return start_transaction(root, num_items, TRANS_START,
  675. BTRFS_RESERVE_FLUSH_ALL, true);
  676. }
  677. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  678. struct btrfs_root *root,
  679. unsigned int num_items)
  680. {
  681. return start_transaction(root, num_items, TRANS_START,
  682. BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
  683. }
  684. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  685. {
  686. return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
  687. true);
  688. }
  689. struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
  690. {
  691. return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
  692. BTRFS_RESERVE_NO_FLUSH, true);
  693. }
  694. /*
  695. * Similar to regular join but it never starts a transaction when none is
  696. * running or after waiting for the current one to finish.
  697. */
  698. struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
  699. {
  700. return start_transaction(root, 0, TRANS_JOIN_NOSTART,
  701. BTRFS_RESERVE_NO_FLUSH, true);
  702. }
  703. /*
  704. * btrfs_attach_transaction() - catch the running transaction
  705. *
  706. * It is used when we want to commit the current the transaction, but
  707. * don't want to start a new one.
  708. *
  709. * Note: If this function return -ENOENT, it just means there is no
  710. * running transaction. But it is possible that the inactive transaction
  711. * is still in the memory, not fully on disk. If you hope there is no
  712. * inactive transaction in the fs when -ENOENT is returned, you should
  713. * invoke
  714. * btrfs_attach_transaction_barrier()
  715. */
  716. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  717. {
  718. return start_transaction(root, 0, TRANS_ATTACH,
  719. BTRFS_RESERVE_NO_FLUSH, true);
  720. }
  721. /*
  722. * btrfs_attach_transaction_barrier() - catch the running transaction
  723. *
  724. * It is similar to the above function, the difference is this one
  725. * will wait for all the inactive transactions until they fully
  726. * complete.
  727. */
  728. struct btrfs_trans_handle *
  729. btrfs_attach_transaction_barrier(struct btrfs_root *root)
  730. {
  731. struct btrfs_trans_handle *trans;
  732. trans = start_transaction(root, 0, TRANS_ATTACH,
  733. BTRFS_RESERVE_NO_FLUSH, true);
  734. if (trans == ERR_PTR(-ENOENT))
  735. btrfs_wait_for_commit(root->fs_info, 0);
  736. return trans;
  737. }
  738. /* wait for a transaction commit to be fully complete */
  739. static noinline void wait_for_commit(struct btrfs_transaction *commit)
  740. {
  741. wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
  742. }
  743. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
  744. {
  745. struct btrfs_transaction *cur_trans = NULL, *t;
  746. int ret = 0;
  747. if (transid) {
  748. if (transid <= fs_info->last_trans_committed)
  749. goto out;
  750. /* find specified transaction */
  751. spin_lock(&fs_info->trans_lock);
  752. list_for_each_entry(t, &fs_info->trans_list, list) {
  753. if (t->transid == transid) {
  754. cur_trans = t;
  755. refcount_inc(&cur_trans->use_count);
  756. ret = 0;
  757. break;
  758. }
  759. if (t->transid > transid) {
  760. ret = 0;
  761. break;
  762. }
  763. }
  764. spin_unlock(&fs_info->trans_lock);
  765. /*
  766. * The specified transaction doesn't exist, or we
  767. * raced with btrfs_commit_transaction
  768. */
  769. if (!cur_trans) {
  770. if (transid > fs_info->last_trans_committed)
  771. ret = -EINVAL;
  772. goto out;
  773. }
  774. } else {
  775. /* find newest transaction that is committing | committed */
  776. spin_lock(&fs_info->trans_lock);
  777. list_for_each_entry_reverse(t, &fs_info->trans_list,
  778. list) {
  779. if (t->state >= TRANS_STATE_COMMIT_START) {
  780. if (t->state == TRANS_STATE_COMPLETED)
  781. break;
  782. cur_trans = t;
  783. refcount_inc(&cur_trans->use_count);
  784. break;
  785. }
  786. }
  787. spin_unlock(&fs_info->trans_lock);
  788. if (!cur_trans)
  789. goto out; /* nothing committing|committed */
  790. }
  791. wait_for_commit(cur_trans);
  792. btrfs_put_transaction(cur_trans);
  793. out:
  794. return ret;
  795. }
  796. void btrfs_throttle(struct btrfs_fs_info *fs_info)
  797. {
  798. wait_current_trans(fs_info);
  799. }
  800. static int should_end_transaction(struct btrfs_trans_handle *trans)
  801. {
  802. struct btrfs_fs_info *fs_info = trans->fs_info;
  803. if (btrfs_check_space_for_delayed_refs(fs_info))
  804. return 1;
  805. return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
  806. }
  807. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
  808. {
  809. struct btrfs_transaction *cur_trans = trans->transaction;
  810. smp_mb();
  811. if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
  812. cur_trans->delayed_refs.flushing)
  813. return 1;
  814. return should_end_transaction(trans);
  815. }
  816. static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
  817. {
  818. struct btrfs_fs_info *fs_info = trans->fs_info;
  819. if (!trans->block_rsv) {
  820. ASSERT(!trans->bytes_reserved);
  821. return;
  822. }
  823. if (!trans->bytes_reserved)
  824. return;
  825. ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
  826. trace_btrfs_space_reservation(fs_info, "transaction",
  827. trans->transid, trans->bytes_reserved, 0);
  828. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  829. trans->bytes_reserved, NULL);
  830. trans->bytes_reserved = 0;
  831. }
  832. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  833. int throttle)
  834. {
  835. struct btrfs_fs_info *info = trans->fs_info;
  836. struct btrfs_transaction *cur_trans = trans->transaction;
  837. int err = 0;
  838. if (refcount_read(&trans->use_count) > 1) {
  839. refcount_dec(&trans->use_count);
  840. trans->block_rsv = trans->orig_rsv;
  841. return 0;
  842. }
  843. btrfs_trans_release_metadata(trans);
  844. trans->block_rsv = NULL;
  845. btrfs_create_pending_block_groups(trans);
  846. btrfs_trans_release_chunk_metadata(trans);
  847. if (trans->type & __TRANS_FREEZABLE)
  848. sb_end_intwrite(info->sb);
  849. WARN_ON(cur_trans != info->running_transaction);
  850. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  851. atomic_dec(&cur_trans->num_writers);
  852. extwriter_counter_dec(cur_trans, trans->type);
  853. cond_wake_up(&cur_trans->writer_wait);
  854. btrfs_put_transaction(cur_trans);
  855. if (current->journal_info == trans)
  856. current->journal_info = NULL;
  857. if (throttle)
  858. btrfs_run_delayed_iputs(info);
  859. if (TRANS_ABORTED(trans) ||
  860. test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
  861. wake_up_process(info->transaction_kthread);
  862. if (TRANS_ABORTED(trans))
  863. err = trans->aborted;
  864. else
  865. err = -EROFS;
  866. }
  867. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  868. return err;
  869. }
  870. int btrfs_end_transaction(struct btrfs_trans_handle *trans)
  871. {
  872. return __btrfs_end_transaction(trans, 0);
  873. }
  874. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
  875. {
  876. return __btrfs_end_transaction(trans, 1);
  877. }
  878. /*
  879. * when btree blocks are allocated, they have some corresponding bits set for
  880. * them in one of two extent_io trees. This is used to make sure all of
  881. * those extents are sent to disk but does not wait on them
  882. */
  883. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  884. struct extent_io_tree *dirty_pages, int mark)
  885. {
  886. int err = 0;
  887. int werr = 0;
  888. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  889. struct extent_state *cached_state = NULL;
  890. u64 start = 0;
  891. u64 end;
  892. atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  893. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  894. mark, &cached_state)) {
  895. bool wait_writeback = false;
  896. err = convert_extent_bit(dirty_pages, start, end,
  897. EXTENT_NEED_WAIT,
  898. mark, &cached_state);
  899. /*
  900. * convert_extent_bit can return -ENOMEM, which is most of the
  901. * time a temporary error. So when it happens, ignore the error
  902. * and wait for writeback of this range to finish - because we
  903. * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  904. * to __btrfs_wait_marked_extents() would not know that
  905. * writeback for this range started and therefore wouldn't
  906. * wait for it to finish - we don't want to commit a
  907. * superblock that points to btree nodes/leafs for which
  908. * writeback hasn't finished yet (and without errors).
  909. * We cleanup any entries left in the io tree when committing
  910. * the transaction (through extent_io_tree_release()).
  911. */
  912. if (err == -ENOMEM) {
  913. err = 0;
  914. wait_writeback = true;
  915. }
  916. if (!err)
  917. err = filemap_fdatawrite_range(mapping, start, end);
  918. if (err)
  919. werr = err;
  920. else if (wait_writeback)
  921. werr = filemap_fdatawait_range(mapping, start, end);
  922. free_extent_state(cached_state);
  923. cached_state = NULL;
  924. cond_resched();
  925. start = end + 1;
  926. }
  927. atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  928. return werr;
  929. }
  930. /*
  931. * when btree blocks are allocated, they have some corresponding bits set for
  932. * them in one of two extent_io trees. This is used to make sure all of
  933. * those extents are on disk for transaction or log commit. We wait
  934. * on all the pages and clear them from the dirty pages state tree
  935. */
  936. static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
  937. struct extent_io_tree *dirty_pages)
  938. {
  939. int err = 0;
  940. int werr = 0;
  941. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  942. struct extent_state *cached_state = NULL;
  943. u64 start = 0;
  944. u64 end;
  945. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  946. EXTENT_NEED_WAIT, &cached_state)) {
  947. /*
  948. * Ignore -ENOMEM errors returned by clear_extent_bit().
  949. * When committing the transaction, we'll remove any entries
  950. * left in the io tree. For a log commit, we don't remove them
  951. * after committing the log because the tree can be accessed
  952. * concurrently - we do it only at transaction commit time when
  953. * it's safe to do it (through extent_io_tree_release()).
  954. */
  955. err = clear_extent_bit(dirty_pages, start, end,
  956. EXTENT_NEED_WAIT, 0, 0, &cached_state);
  957. if (err == -ENOMEM)
  958. err = 0;
  959. if (!err)
  960. err = filemap_fdatawait_range(mapping, start, end);
  961. if (err)
  962. werr = err;
  963. free_extent_state(cached_state);
  964. cached_state = NULL;
  965. cond_resched();
  966. start = end + 1;
  967. }
  968. if (err)
  969. werr = err;
  970. return werr;
  971. }
  972. static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  973. struct extent_io_tree *dirty_pages)
  974. {
  975. bool errors = false;
  976. int err;
  977. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  978. if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
  979. errors = true;
  980. if (errors && !err)
  981. err = -EIO;
  982. return err;
  983. }
  984. int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
  985. {
  986. struct btrfs_fs_info *fs_info = log_root->fs_info;
  987. struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
  988. bool errors = false;
  989. int err;
  990. ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
  991. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  992. if ((mark & EXTENT_DIRTY) &&
  993. test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
  994. errors = true;
  995. if ((mark & EXTENT_NEW) &&
  996. test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
  997. errors = true;
  998. if (errors && !err)
  999. err = -EIO;
  1000. return err;
  1001. }
  1002. /*
  1003. * When btree blocks are allocated the corresponding extents are marked dirty.
  1004. * This function ensures such extents are persisted on disk for transaction or
  1005. * log commit.
  1006. *
  1007. * @trans: transaction whose dirty pages we'd like to write
  1008. */
  1009. static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
  1010. {
  1011. int ret;
  1012. int ret2;
  1013. struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
  1014. struct btrfs_fs_info *fs_info = trans->fs_info;
  1015. struct blk_plug plug;
  1016. blk_start_plug(&plug);
  1017. ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
  1018. blk_finish_plug(&plug);
  1019. ret2 = btrfs_wait_extents(fs_info, dirty_pages);
  1020. extent_io_tree_release(&trans->transaction->dirty_pages);
  1021. if (ret)
  1022. return ret;
  1023. else if (ret2)
  1024. return ret2;
  1025. else
  1026. return 0;
  1027. }
  1028. /*
  1029. * this is used to update the root pointer in the tree of tree roots.
  1030. *
  1031. * But, in the case of the extent allocation tree, updating the root
  1032. * pointer may allocate blocks which may change the root of the extent
  1033. * allocation tree.
  1034. *
  1035. * So, this loops and repeats and makes sure the cowonly root didn't
  1036. * change while the root pointer was being updated in the metadata.
  1037. */
  1038. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  1039. struct btrfs_root *root)
  1040. {
  1041. int ret;
  1042. u64 old_root_bytenr;
  1043. u64 old_root_used;
  1044. struct btrfs_fs_info *fs_info = root->fs_info;
  1045. struct btrfs_root *tree_root = fs_info->tree_root;
  1046. old_root_used = btrfs_root_used(&root->root_item);
  1047. while (1) {
  1048. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  1049. if (old_root_bytenr == root->node->start &&
  1050. old_root_used == btrfs_root_used(&root->root_item))
  1051. break;
  1052. btrfs_set_root_node(&root->root_item, root->node);
  1053. ret = btrfs_update_root(trans, tree_root,
  1054. &root->root_key,
  1055. &root->root_item);
  1056. if (ret)
  1057. return ret;
  1058. old_root_used = btrfs_root_used(&root->root_item);
  1059. }
  1060. return 0;
  1061. }
  1062. /*
  1063. * update all the cowonly tree roots on disk
  1064. *
  1065. * The error handling in this function may not be obvious. Any of the
  1066. * failures will cause the file system to go offline. We still need
  1067. * to clean up the delayed refs.
  1068. */
  1069. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
  1070. {
  1071. struct btrfs_fs_info *fs_info = trans->fs_info;
  1072. struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
  1073. struct list_head *io_bgs = &trans->transaction->io_bgs;
  1074. struct list_head *next;
  1075. struct extent_buffer *eb;
  1076. int ret;
  1077. eb = btrfs_lock_root_node(fs_info->tree_root);
  1078. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  1079. 0, &eb, BTRFS_NESTING_COW);
  1080. btrfs_tree_unlock(eb);
  1081. free_extent_buffer(eb);
  1082. if (ret)
  1083. return ret;
  1084. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1085. if (ret)
  1086. return ret;
  1087. ret = btrfs_run_dev_stats(trans);
  1088. if (ret)
  1089. return ret;
  1090. ret = btrfs_run_dev_replace(trans);
  1091. if (ret)
  1092. return ret;
  1093. ret = btrfs_run_qgroups(trans);
  1094. if (ret)
  1095. return ret;
  1096. ret = btrfs_setup_space_cache(trans);
  1097. if (ret)
  1098. return ret;
  1099. /* run_qgroups might have added some more refs */
  1100. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1101. if (ret)
  1102. return ret;
  1103. again:
  1104. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  1105. struct btrfs_root *root;
  1106. next = fs_info->dirty_cowonly_roots.next;
  1107. list_del_init(next);
  1108. root = list_entry(next, struct btrfs_root, dirty_list);
  1109. clear_bit(BTRFS_ROOT_DIRTY, &root->state);
  1110. if (root != fs_info->extent_root)
  1111. list_add_tail(&root->dirty_list,
  1112. &trans->transaction->switch_commits);
  1113. ret = update_cowonly_root(trans, root);
  1114. if (ret)
  1115. return ret;
  1116. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1117. if (ret)
  1118. return ret;
  1119. }
  1120. while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
  1121. ret = btrfs_write_dirty_block_groups(trans);
  1122. if (ret)
  1123. return ret;
  1124. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1125. if (ret)
  1126. return ret;
  1127. }
  1128. if (!list_empty(&fs_info->dirty_cowonly_roots))
  1129. goto again;
  1130. list_add_tail(&fs_info->extent_root->dirty_list,
  1131. &trans->transaction->switch_commits);
  1132. /* Update dev-replace pointer once everything is committed */
  1133. fs_info->dev_replace.committed_cursor_left =
  1134. fs_info->dev_replace.cursor_left_last_write_of_item;
  1135. return 0;
  1136. }
  1137. /*
  1138. * dead roots are old snapshots that need to be deleted. This allocates
  1139. * a dirty root struct and adds it into the list of dead roots that need to
  1140. * be deleted
  1141. */
  1142. void btrfs_add_dead_root(struct btrfs_root *root)
  1143. {
  1144. struct btrfs_fs_info *fs_info = root->fs_info;
  1145. spin_lock(&fs_info->trans_lock);
  1146. if (list_empty(&root->root_list)) {
  1147. btrfs_grab_root(root);
  1148. list_add_tail(&root->root_list, &fs_info->dead_roots);
  1149. }
  1150. spin_unlock(&fs_info->trans_lock);
  1151. }
  1152. /*
  1153. * update all the cowonly tree roots on disk
  1154. */
  1155. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
  1156. {
  1157. struct btrfs_fs_info *fs_info = trans->fs_info;
  1158. struct btrfs_root *gang[8];
  1159. int i;
  1160. int ret;
  1161. spin_lock(&fs_info->fs_roots_radix_lock);
  1162. while (1) {
  1163. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  1164. (void **)gang, 0,
  1165. ARRAY_SIZE(gang),
  1166. BTRFS_ROOT_TRANS_TAG);
  1167. if (ret == 0)
  1168. break;
  1169. for (i = 0; i < ret; i++) {
  1170. struct btrfs_root *root = gang[i];
  1171. int ret2;
  1172. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  1173. (unsigned long)root->root_key.objectid,
  1174. BTRFS_ROOT_TRANS_TAG);
  1175. spin_unlock(&fs_info->fs_roots_radix_lock);
  1176. btrfs_free_log(trans, root);
  1177. btrfs_update_reloc_root(trans, root);
  1178. btrfs_save_ino_cache(root, trans);
  1179. /* see comments in should_cow_block() */
  1180. clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1181. smp_mb__after_atomic();
  1182. if (root->commit_root != root->node) {
  1183. list_add_tail(&root->dirty_list,
  1184. &trans->transaction->switch_commits);
  1185. btrfs_set_root_node(&root->root_item,
  1186. root->node);
  1187. }
  1188. ret2 = btrfs_update_root(trans, fs_info->tree_root,
  1189. &root->root_key,
  1190. &root->root_item);
  1191. if (ret2)
  1192. return ret2;
  1193. spin_lock(&fs_info->fs_roots_radix_lock);
  1194. btrfs_qgroup_free_meta_all_pertrans(root);
  1195. }
  1196. }
  1197. spin_unlock(&fs_info->fs_roots_radix_lock);
  1198. return 0;
  1199. }
  1200. /*
  1201. * defrag a given btree.
  1202. * Every leaf in the btree is read and defragged.
  1203. */
  1204. int btrfs_defrag_root(struct btrfs_root *root)
  1205. {
  1206. struct btrfs_fs_info *info = root->fs_info;
  1207. struct btrfs_trans_handle *trans;
  1208. int ret;
  1209. if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
  1210. return 0;
  1211. while (1) {
  1212. trans = btrfs_start_transaction(root, 0);
  1213. if (IS_ERR(trans)) {
  1214. ret = PTR_ERR(trans);
  1215. break;
  1216. }
  1217. ret = btrfs_defrag_leaves(trans, root);
  1218. btrfs_end_transaction(trans);
  1219. btrfs_btree_balance_dirty(info);
  1220. cond_resched();
  1221. if (btrfs_fs_closing(info) || ret != -EAGAIN)
  1222. break;
  1223. if (btrfs_defrag_cancelled(info)) {
  1224. btrfs_debug(info, "defrag_root cancelled");
  1225. ret = -EAGAIN;
  1226. break;
  1227. }
  1228. }
  1229. clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
  1230. return ret;
  1231. }
  1232. /*
  1233. * Do all special snapshot related qgroup dirty hack.
  1234. *
  1235. * Will do all needed qgroup inherit and dirty hack like switch commit
  1236. * roots inside one transaction and write all btree into disk, to make
  1237. * qgroup works.
  1238. */
  1239. static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  1240. struct btrfs_root *src,
  1241. struct btrfs_root *parent,
  1242. struct btrfs_qgroup_inherit *inherit,
  1243. u64 dst_objectid)
  1244. {
  1245. struct btrfs_fs_info *fs_info = src->fs_info;
  1246. int ret;
  1247. /*
  1248. * Save some performance in the case that qgroups are not
  1249. * enabled. If this check races with the ioctl, rescan will
  1250. * kick in anyway.
  1251. */
  1252. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1253. return 0;
  1254. /*
  1255. * Ensure dirty @src will be committed. Or, after coming
  1256. * commit_fs_roots() and switch_commit_roots(), any dirty but not
  1257. * recorded root will never be updated again, causing an outdated root
  1258. * item.
  1259. */
  1260. record_root_in_trans(trans, src, 1);
  1261. /*
  1262. * We are going to commit transaction, see btrfs_commit_transaction()
  1263. * comment for reason locking tree_log_mutex
  1264. */
  1265. mutex_lock(&fs_info->tree_log_mutex);
  1266. ret = commit_fs_roots(trans);
  1267. if (ret)
  1268. goto out;
  1269. ret = btrfs_qgroup_account_extents(trans);
  1270. if (ret < 0)
  1271. goto out;
  1272. /* Now qgroup are all updated, we can inherit it to new qgroups */
  1273. ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
  1274. inherit);
  1275. if (ret < 0)
  1276. goto out;
  1277. /*
  1278. * Now we do a simplified commit transaction, which will:
  1279. * 1) commit all subvolume and extent tree
  1280. * To ensure all subvolume and extent tree have a valid
  1281. * commit_root to accounting later insert_dir_item()
  1282. * 2) write all btree blocks onto disk
  1283. * This is to make sure later btree modification will be cowed
  1284. * Or commit_root can be populated and cause wrong qgroup numbers
  1285. * In this simplified commit, we don't really care about other trees
  1286. * like chunk and root tree, as they won't affect qgroup.
  1287. * And we don't write super to avoid half committed status.
  1288. */
  1289. ret = commit_cowonly_roots(trans);
  1290. if (ret)
  1291. goto out;
  1292. switch_commit_roots(trans);
  1293. ret = btrfs_write_and_wait_transaction(trans);
  1294. if (ret)
  1295. btrfs_handle_fs_error(fs_info, ret,
  1296. "Error while writing out transaction for qgroup");
  1297. out:
  1298. mutex_unlock(&fs_info->tree_log_mutex);
  1299. /*
  1300. * Force parent root to be updated, as we recorded it before so its
  1301. * last_trans == cur_transid.
  1302. * Or it won't be committed again onto disk after later
  1303. * insert_dir_item()
  1304. */
  1305. if (!ret)
  1306. record_root_in_trans(trans, parent, 1);
  1307. return ret;
  1308. }
  1309. /*
  1310. * new snapshots need to be created at a very specific time in the
  1311. * transaction commit. This does the actual creation.
  1312. *
  1313. * Note:
  1314. * If the error which may affect the commitment of the current transaction
  1315. * happens, we should return the error number. If the error which just affect
  1316. * the creation of the pending snapshots, just return 0.
  1317. */
  1318. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  1319. struct btrfs_pending_snapshot *pending)
  1320. {
  1321. struct btrfs_fs_info *fs_info = trans->fs_info;
  1322. struct btrfs_key key;
  1323. struct btrfs_root_item *new_root_item;
  1324. struct btrfs_root *tree_root = fs_info->tree_root;
  1325. struct btrfs_root *root = pending->root;
  1326. struct btrfs_root *parent_root;
  1327. struct btrfs_block_rsv *rsv;
  1328. struct inode *parent_inode;
  1329. struct btrfs_path *path;
  1330. struct btrfs_dir_item *dir_item;
  1331. struct dentry *dentry;
  1332. struct extent_buffer *tmp;
  1333. struct extent_buffer *old;
  1334. struct timespec64 cur_time;
  1335. int ret = 0;
  1336. u64 to_reserve = 0;
  1337. u64 index = 0;
  1338. u64 objectid;
  1339. u64 root_flags;
  1340. ASSERT(pending->path);
  1341. path = pending->path;
  1342. ASSERT(pending->root_item);
  1343. new_root_item = pending->root_item;
  1344. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  1345. if (pending->error)
  1346. goto no_free_objectid;
  1347. /*
  1348. * Make qgroup to skip current new snapshot's qgroupid, as it is
  1349. * accounted by later btrfs_qgroup_inherit().
  1350. */
  1351. btrfs_set_skip_qgroup(trans, objectid);
  1352. btrfs_reloc_pre_snapshot(pending, &to_reserve);
  1353. if (to_reserve > 0) {
  1354. pending->error = btrfs_block_rsv_add(root,
  1355. &pending->block_rsv,
  1356. to_reserve,
  1357. BTRFS_RESERVE_NO_FLUSH);
  1358. if (pending->error)
  1359. goto clear_skip_qgroup;
  1360. }
  1361. key.objectid = objectid;
  1362. key.offset = (u64)-1;
  1363. key.type = BTRFS_ROOT_ITEM_KEY;
  1364. rsv = trans->block_rsv;
  1365. trans->block_rsv = &pending->block_rsv;
  1366. trans->bytes_reserved = trans->block_rsv->reserved;
  1367. trace_btrfs_space_reservation(fs_info, "transaction",
  1368. trans->transid,
  1369. trans->bytes_reserved, 1);
  1370. dentry = pending->dentry;
  1371. parent_inode = pending->dir;
  1372. parent_root = BTRFS_I(parent_inode)->root;
  1373. record_root_in_trans(trans, parent_root, 0);
  1374. cur_time = current_time(parent_inode);
  1375. /*
  1376. * insert the directory item
  1377. */
  1378. ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
  1379. BUG_ON(ret); /* -ENOMEM */
  1380. /* check if there is a file/dir which has the same name. */
  1381. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1382. btrfs_ino(BTRFS_I(parent_inode)),
  1383. dentry->d_name.name,
  1384. dentry->d_name.len, 0);
  1385. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1386. pending->error = -EEXIST;
  1387. goto dir_item_existed;
  1388. } else if (IS_ERR(dir_item)) {
  1389. ret = PTR_ERR(dir_item);
  1390. btrfs_abort_transaction(trans, ret);
  1391. goto fail;
  1392. }
  1393. btrfs_release_path(path);
  1394. /*
  1395. * pull in the delayed directory update
  1396. * and the delayed inode item
  1397. * otherwise we corrupt the FS during
  1398. * snapshot
  1399. */
  1400. ret = btrfs_run_delayed_items(trans);
  1401. if (ret) { /* Transaction aborted */
  1402. btrfs_abort_transaction(trans, ret);
  1403. goto fail;
  1404. }
  1405. record_root_in_trans(trans, root, 0);
  1406. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1407. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1408. btrfs_check_and_init_root_item(new_root_item);
  1409. root_flags = btrfs_root_flags(new_root_item);
  1410. if (pending->readonly)
  1411. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1412. else
  1413. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1414. btrfs_set_root_flags(new_root_item, root_flags);
  1415. btrfs_set_root_generation_v2(new_root_item,
  1416. trans->transid);
  1417. generate_random_guid(new_root_item->uuid);
  1418. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1419. BTRFS_UUID_SIZE);
  1420. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1421. memset(new_root_item->received_uuid, 0,
  1422. sizeof(new_root_item->received_uuid));
  1423. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1424. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1425. btrfs_set_root_stransid(new_root_item, 0);
  1426. btrfs_set_root_rtransid(new_root_item, 0);
  1427. }
  1428. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1429. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1430. btrfs_set_root_otransid(new_root_item, trans->transid);
  1431. old = btrfs_lock_root_node(root);
  1432. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
  1433. BTRFS_NESTING_COW);
  1434. if (ret) {
  1435. btrfs_tree_unlock(old);
  1436. free_extent_buffer(old);
  1437. btrfs_abort_transaction(trans, ret);
  1438. goto fail;
  1439. }
  1440. btrfs_set_lock_blocking_write(old);
  1441. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1442. /* clean up in any case */
  1443. btrfs_tree_unlock(old);
  1444. free_extent_buffer(old);
  1445. if (ret) {
  1446. btrfs_abort_transaction(trans, ret);
  1447. goto fail;
  1448. }
  1449. /* see comments in should_cow_block() */
  1450. set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1451. smp_wmb();
  1452. btrfs_set_root_node(new_root_item, tmp);
  1453. /* record when the snapshot was created in key.offset */
  1454. key.offset = trans->transid;
  1455. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1456. btrfs_tree_unlock(tmp);
  1457. free_extent_buffer(tmp);
  1458. if (ret) {
  1459. btrfs_abort_transaction(trans, ret);
  1460. goto fail;
  1461. }
  1462. /*
  1463. * insert root back/forward references
  1464. */
  1465. ret = btrfs_add_root_ref(trans, objectid,
  1466. parent_root->root_key.objectid,
  1467. btrfs_ino(BTRFS_I(parent_inode)), index,
  1468. dentry->d_name.name, dentry->d_name.len);
  1469. if (ret) {
  1470. btrfs_abort_transaction(trans, ret);
  1471. goto fail;
  1472. }
  1473. key.offset = (u64)-1;
  1474. pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev);
  1475. if (IS_ERR(pending->snap)) {
  1476. ret = PTR_ERR(pending->snap);
  1477. pending->snap = NULL;
  1478. btrfs_abort_transaction(trans, ret);
  1479. goto fail;
  1480. }
  1481. ret = btrfs_reloc_post_snapshot(trans, pending);
  1482. if (ret) {
  1483. btrfs_abort_transaction(trans, ret);
  1484. goto fail;
  1485. }
  1486. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1487. if (ret) {
  1488. btrfs_abort_transaction(trans, ret);
  1489. goto fail;
  1490. }
  1491. /*
  1492. * Do special qgroup accounting for snapshot, as we do some qgroup
  1493. * snapshot hack to do fast snapshot.
  1494. * To co-operate with that hack, we do hack again.
  1495. * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  1496. */
  1497. ret = qgroup_account_snapshot(trans, root, parent_root,
  1498. pending->inherit, objectid);
  1499. if (ret < 0)
  1500. goto fail;
  1501. ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
  1502. dentry->d_name.len, BTRFS_I(parent_inode),
  1503. &key, BTRFS_FT_DIR, index);
  1504. /* We have check then name at the beginning, so it is impossible. */
  1505. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1506. if (ret) {
  1507. btrfs_abort_transaction(trans, ret);
  1508. goto fail;
  1509. }
  1510. btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
  1511. dentry->d_name.len * 2);
  1512. parent_inode->i_mtime = parent_inode->i_ctime =
  1513. current_time(parent_inode);
  1514. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1515. if (ret) {
  1516. btrfs_abort_transaction(trans, ret);
  1517. goto fail;
  1518. }
  1519. ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
  1520. BTRFS_UUID_KEY_SUBVOL,
  1521. objectid);
  1522. if (ret) {
  1523. btrfs_abort_transaction(trans, ret);
  1524. goto fail;
  1525. }
  1526. if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  1527. ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
  1528. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1529. objectid);
  1530. if (ret && ret != -EEXIST) {
  1531. btrfs_abort_transaction(trans, ret);
  1532. goto fail;
  1533. }
  1534. }
  1535. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1536. if (ret) {
  1537. btrfs_abort_transaction(trans, ret);
  1538. goto fail;
  1539. }
  1540. fail:
  1541. pending->error = ret;
  1542. dir_item_existed:
  1543. trans->block_rsv = rsv;
  1544. trans->bytes_reserved = 0;
  1545. clear_skip_qgroup:
  1546. btrfs_clear_skip_qgroup(trans);
  1547. no_free_objectid:
  1548. kfree(new_root_item);
  1549. pending->root_item = NULL;
  1550. btrfs_free_path(path);
  1551. pending->path = NULL;
  1552. return ret;
  1553. }
  1554. /*
  1555. * create all the snapshots we've scheduled for creation
  1556. */
  1557. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
  1558. {
  1559. struct btrfs_pending_snapshot *pending, *next;
  1560. struct list_head *head = &trans->transaction->pending_snapshots;
  1561. int ret = 0;
  1562. list_for_each_entry_safe(pending, next, head, list) {
  1563. list_del(&pending->list);
  1564. ret = create_pending_snapshot(trans, pending);
  1565. if (ret)
  1566. break;
  1567. }
  1568. return ret;
  1569. }
  1570. static void update_super_roots(struct btrfs_fs_info *fs_info)
  1571. {
  1572. struct btrfs_root_item *root_item;
  1573. struct btrfs_super_block *super;
  1574. super = fs_info->super_copy;
  1575. root_item = &fs_info->chunk_root->root_item;
  1576. super->chunk_root = root_item->bytenr;
  1577. super->chunk_root_generation = root_item->generation;
  1578. super->chunk_root_level = root_item->level;
  1579. root_item = &fs_info->tree_root->root_item;
  1580. super->root = root_item->bytenr;
  1581. super->generation = root_item->generation;
  1582. super->root_level = root_item->level;
  1583. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  1584. super->cache_generation = root_item->generation;
  1585. if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
  1586. super->uuid_tree_generation = root_item->generation;
  1587. }
  1588. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1589. {
  1590. struct btrfs_transaction *trans;
  1591. int ret = 0;
  1592. spin_lock(&info->trans_lock);
  1593. trans = info->running_transaction;
  1594. if (trans)
  1595. ret = (trans->state >= TRANS_STATE_COMMIT_START);
  1596. spin_unlock(&info->trans_lock);
  1597. return ret;
  1598. }
  1599. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1600. {
  1601. struct btrfs_transaction *trans;
  1602. int ret = 0;
  1603. spin_lock(&info->trans_lock);
  1604. trans = info->running_transaction;
  1605. if (trans)
  1606. ret = is_transaction_blocked(trans);
  1607. spin_unlock(&info->trans_lock);
  1608. return ret;
  1609. }
  1610. /*
  1611. * wait for the current transaction commit to start and block subsequent
  1612. * transaction joins
  1613. */
  1614. static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
  1615. struct btrfs_transaction *trans)
  1616. {
  1617. wait_event(fs_info->transaction_blocked_wait,
  1618. trans->state >= TRANS_STATE_COMMIT_START ||
  1619. TRANS_ABORTED(trans));
  1620. }
  1621. /*
  1622. * wait for the current transaction to start and then become unblocked.
  1623. * caller holds ref.
  1624. */
  1625. static void wait_current_trans_commit_start_and_unblock(
  1626. struct btrfs_fs_info *fs_info,
  1627. struct btrfs_transaction *trans)
  1628. {
  1629. wait_event(fs_info->transaction_wait,
  1630. trans->state >= TRANS_STATE_UNBLOCKED ||
  1631. TRANS_ABORTED(trans));
  1632. }
  1633. /*
  1634. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1635. * returns, any subsequent transaction will not be allowed to join.
  1636. */
  1637. struct btrfs_async_commit {
  1638. struct btrfs_trans_handle *newtrans;
  1639. struct work_struct work;
  1640. };
  1641. static void do_async_commit(struct work_struct *work)
  1642. {
  1643. struct btrfs_async_commit *ac =
  1644. container_of(work, struct btrfs_async_commit, work);
  1645. /*
  1646. * We've got freeze protection passed with the transaction.
  1647. * Tell lockdep about it.
  1648. */
  1649. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1650. __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
  1651. current->journal_info = ac->newtrans;
  1652. btrfs_commit_transaction(ac->newtrans);
  1653. kfree(ac);
  1654. }
  1655. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1656. int wait_for_unblock)
  1657. {
  1658. struct btrfs_fs_info *fs_info = trans->fs_info;
  1659. struct btrfs_async_commit *ac;
  1660. struct btrfs_transaction *cur_trans;
  1661. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1662. if (!ac)
  1663. return -ENOMEM;
  1664. INIT_WORK(&ac->work, do_async_commit);
  1665. ac->newtrans = btrfs_join_transaction(trans->root);
  1666. if (IS_ERR(ac->newtrans)) {
  1667. int err = PTR_ERR(ac->newtrans);
  1668. kfree(ac);
  1669. return err;
  1670. }
  1671. /* take transaction reference */
  1672. cur_trans = trans->transaction;
  1673. refcount_inc(&cur_trans->use_count);
  1674. btrfs_end_transaction(trans);
  1675. /*
  1676. * Tell lockdep we've released the freeze rwsem, since the
  1677. * async commit thread will be the one to unlock it.
  1678. */
  1679. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1680. __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
  1681. schedule_work(&ac->work);
  1682. /* wait for transaction to start and unblock */
  1683. if (wait_for_unblock)
  1684. wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
  1685. else
  1686. wait_current_trans_commit_start(fs_info, cur_trans);
  1687. if (current->journal_info == trans)
  1688. current->journal_info = NULL;
  1689. btrfs_put_transaction(cur_trans);
  1690. return 0;
  1691. }
  1692. static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
  1693. {
  1694. struct btrfs_fs_info *fs_info = trans->fs_info;
  1695. struct btrfs_transaction *cur_trans = trans->transaction;
  1696. WARN_ON(refcount_read(&trans->use_count) > 1);
  1697. btrfs_abort_transaction(trans, err);
  1698. spin_lock(&fs_info->trans_lock);
  1699. /*
  1700. * If the transaction is removed from the list, it means this
  1701. * transaction has been committed successfully, so it is impossible
  1702. * to call the cleanup function.
  1703. */
  1704. BUG_ON(list_empty(&cur_trans->list));
  1705. if (cur_trans == fs_info->running_transaction) {
  1706. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1707. spin_unlock(&fs_info->trans_lock);
  1708. wait_event(cur_trans->writer_wait,
  1709. atomic_read(&cur_trans->num_writers) == 1);
  1710. spin_lock(&fs_info->trans_lock);
  1711. }
  1712. /*
  1713. * Now that we know no one else is still using the transaction we can
  1714. * remove the transaction from the list of transactions. This avoids
  1715. * the transaction kthread from cleaning up the transaction while some
  1716. * other task is still using it, which could result in a use-after-free
  1717. * on things like log trees, as it forces the transaction kthread to
  1718. * wait for this transaction to be cleaned up by us.
  1719. */
  1720. list_del_init(&cur_trans->list);
  1721. spin_unlock(&fs_info->trans_lock);
  1722. btrfs_cleanup_one_transaction(trans->transaction, fs_info);
  1723. spin_lock(&fs_info->trans_lock);
  1724. if (cur_trans == fs_info->running_transaction)
  1725. fs_info->running_transaction = NULL;
  1726. spin_unlock(&fs_info->trans_lock);
  1727. if (trans->type & __TRANS_FREEZABLE)
  1728. sb_end_intwrite(fs_info->sb);
  1729. btrfs_put_transaction(cur_trans);
  1730. btrfs_put_transaction(cur_trans);
  1731. trace_btrfs_transaction_commit(trans->root);
  1732. if (current->journal_info == trans)
  1733. current->journal_info = NULL;
  1734. btrfs_scrub_cancel(fs_info);
  1735. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1736. }
  1737. /*
  1738. * Release reserved delayed ref space of all pending block groups of the
  1739. * transaction and remove them from the list
  1740. */
  1741. static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
  1742. {
  1743. struct btrfs_fs_info *fs_info = trans->fs_info;
  1744. struct btrfs_block_group *block_group, *tmp;
  1745. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
  1746. btrfs_delayed_refs_rsv_release(fs_info, 1);
  1747. list_del_init(&block_group->bg_list);
  1748. }
  1749. }
  1750. static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
  1751. {
  1752. struct btrfs_fs_info *fs_info = trans->fs_info;
  1753. /*
  1754. * We use writeback_inodes_sb here because if we used
  1755. * btrfs_start_delalloc_roots we would deadlock with fs freeze.
  1756. * Currently are holding the fs freeze lock, if we do an async flush
  1757. * we'll do btrfs_join_transaction() and deadlock because we need to
  1758. * wait for the fs freeze lock. Using the direct flushing we benefit
  1759. * from already being in a transaction and our join_transaction doesn't
  1760. * have to re-take the fs freeze lock.
  1761. */
  1762. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
  1763. writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
  1764. } else {
  1765. struct btrfs_pending_snapshot *pending;
  1766. struct list_head *head = &trans->transaction->pending_snapshots;
  1767. /*
  1768. * Flush dellaloc for any root that is going to be snapshotted.
  1769. * This is done to avoid a corrupted version of files, in the
  1770. * snapshots, that had both buffered and direct IO writes (even
  1771. * if they were done sequentially) due to an unordered update of
  1772. * the inode's size on disk.
  1773. */
  1774. list_for_each_entry(pending, head, list) {
  1775. int ret;
  1776. ret = btrfs_start_delalloc_snapshot(pending->root);
  1777. if (ret)
  1778. return ret;
  1779. }
  1780. }
  1781. return 0;
  1782. }
  1783. static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
  1784. {
  1785. struct btrfs_fs_info *fs_info = trans->fs_info;
  1786. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
  1787. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
  1788. } else {
  1789. struct btrfs_pending_snapshot *pending;
  1790. struct list_head *head = &trans->transaction->pending_snapshots;
  1791. /*
  1792. * Wait for any dellaloc that we started previously for the roots
  1793. * that are going to be snapshotted. This is to avoid a corrupted
  1794. * version of files in the snapshots that had both buffered and
  1795. * direct IO writes (even if they were done sequentially).
  1796. */
  1797. list_for_each_entry(pending, head, list)
  1798. btrfs_wait_ordered_extents(pending->root,
  1799. U64_MAX, 0, U64_MAX);
  1800. }
  1801. }
  1802. int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  1803. {
  1804. struct btrfs_fs_info *fs_info = trans->fs_info;
  1805. struct btrfs_transaction *cur_trans = trans->transaction;
  1806. struct btrfs_transaction *prev_trans = NULL;
  1807. int ret;
  1808. ASSERT(refcount_read(&trans->use_count) == 1);
  1809. /*
  1810. * Some places just start a transaction to commit it. We need to make
  1811. * sure that if this commit fails that the abort code actually marks the
  1812. * transaction as failed, so set trans->dirty to make the abort code do
  1813. * the right thing.
  1814. */
  1815. trans->dirty = true;
  1816. /* Stop the commit early if ->aborted is set */
  1817. if (TRANS_ABORTED(cur_trans)) {
  1818. ret = cur_trans->aborted;
  1819. btrfs_end_transaction(trans);
  1820. return ret;
  1821. }
  1822. btrfs_trans_release_metadata(trans);
  1823. trans->block_rsv = NULL;
  1824. /* make a pass through all the delayed refs we have so far
  1825. * any runnings procs may add more while we are here
  1826. */
  1827. ret = btrfs_run_delayed_refs(trans, 0);
  1828. if (ret) {
  1829. btrfs_end_transaction(trans);
  1830. return ret;
  1831. }
  1832. cur_trans = trans->transaction;
  1833. /*
  1834. * set the flushing flag so procs in this transaction have to
  1835. * start sending their work down.
  1836. */
  1837. cur_trans->delayed_refs.flushing = 1;
  1838. smp_wmb();
  1839. btrfs_create_pending_block_groups(trans);
  1840. ret = btrfs_run_delayed_refs(trans, 0);
  1841. if (ret) {
  1842. btrfs_end_transaction(trans);
  1843. return ret;
  1844. }
  1845. if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
  1846. int run_it = 0;
  1847. /* this mutex is also taken before trying to set
  1848. * block groups readonly. We need to make sure
  1849. * that nobody has set a block group readonly
  1850. * after a extents from that block group have been
  1851. * allocated for cache files. btrfs_set_block_group_ro
  1852. * will wait for the transaction to commit if it
  1853. * finds BTRFS_TRANS_DIRTY_BG_RUN set.
  1854. *
  1855. * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  1856. * only one process starts all the block group IO. It wouldn't
  1857. * hurt to have more than one go through, but there's no
  1858. * real advantage to it either.
  1859. */
  1860. mutex_lock(&fs_info->ro_block_group_mutex);
  1861. if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  1862. &cur_trans->flags))
  1863. run_it = 1;
  1864. mutex_unlock(&fs_info->ro_block_group_mutex);
  1865. if (run_it) {
  1866. ret = btrfs_start_dirty_block_groups(trans);
  1867. if (ret) {
  1868. btrfs_end_transaction(trans);
  1869. return ret;
  1870. }
  1871. }
  1872. }
  1873. spin_lock(&fs_info->trans_lock);
  1874. if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  1875. spin_unlock(&fs_info->trans_lock);
  1876. refcount_inc(&cur_trans->use_count);
  1877. ret = btrfs_end_transaction(trans);
  1878. wait_for_commit(cur_trans);
  1879. if (TRANS_ABORTED(cur_trans))
  1880. ret = cur_trans->aborted;
  1881. btrfs_put_transaction(cur_trans);
  1882. return ret;
  1883. }
  1884. cur_trans->state = TRANS_STATE_COMMIT_START;
  1885. wake_up(&fs_info->transaction_blocked_wait);
  1886. if (cur_trans->list.prev != &fs_info->trans_list) {
  1887. prev_trans = list_entry(cur_trans->list.prev,
  1888. struct btrfs_transaction, list);
  1889. if (prev_trans->state != TRANS_STATE_COMPLETED) {
  1890. refcount_inc(&prev_trans->use_count);
  1891. spin_unlock(&fs_info->trans_lock);
  1892. wait_for_commit(prev_trans);
  1893. ret = READ_ONCE(prev_trans->aborted);
  1894. btrfs_put_transaction(prev_trans);
  1895. if (ret)
  1896. goto cleanup_transaction;
  1897. } else {
  1898. spin_unlock(&fs_info->trans_lock);
  1899. }
  1900. } else {
  1901. spin_unlock(&fs_info->trans_lock);
  1902. /*
  1903. * The previous transaction was aborted and was already removed
  1904. * from the list of transactions at fs_info->trans_list. So we
  1905. * abort to prevent writing a new superblock that reflects a
  1906. * corrupt state (pointing to trees with unwritten nodes/leafs).
  1907. */
  1908. if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
  1909. ret = -EROFS;
  1910. goto cleanup_transaction;
  1911. }
  1912. }
  1913. extwriter_counter_dec(cur_trans, trans->type);
  1914. ret = btrfs_start_delalloc_flush(trans);
  1915. if (ret)
  1916. goto cleanup_transaction;
  1917. ret = btrfs_run_delayed_items(trans);
  1918. if (ret)
  1919. goto cleanup_transaction;
  1920. wait_event(cur_trans->writer_wait,
  1921. extwriter_counter_read(cur_trans) == 0);
  1922. /* some pending stuffs might be added after the previous flush. */
  1923. ret = btrfs_run_delayed_items(trans);
  1924. if (ret)
  1925. goto cleanup_transaction;
  1926. btrfs_wait_delalloc_flush(trans);
  1927. /*
  1928. * Wait for all ordered extents started by a fast fsync that joined this
  1929. * transaction. Otherwise if this transaction commits before the ordered
  1930. * extents complete we lose logged data after a power failure.
  1931. */
  1932. wait_event(cur_trans->pending_wait,
  1933. atomic_read(&cur_trans->pending_ordered) == 0);
  1934. btrfs_scrub_pause(fs_info);
  1935. /*
  1936. * Ok now we need to make sure to block out any other joins while we
  1937. * commit the transaction. We could have started a join before setting
  1938. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  1939. */
  1940. spin_lock(&fs_info->trans_lock);
  1941. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1942. spin_unlock(&fs_info->trans_lock);
  1943. wait_event(cur_trans->writer_wait,
  1944. atomic_read(&cur_trans->num_writers) == 1);
  1945. if (TRANS_ABORTED(cur_trans)) {
  1946. ret = cur_trans->aborted;
  1947. goto scrub_continue;
  1948. }
  1949. /*
  1950. * the reloc mutex makes sure that we stop
  1951. * the balancing code from coming in and moving
  1952. * extents around in the middle of the commit
  1953. */
  1954. mutex_lock(&fs_info->reloc_mutex);
  1955. /*
  1956. * We needn't worry about the delayed items because we will
  1957. * deal with them in create_pending_snapshot(), which is the
  1958. * core function of the snapshot creation.
  1959. */
  1960. ret = create_pending_snapshots(trans);
  1961. if (ret)
  1962. goto unlock_reloc;
  1963. /*
  1964. * We insert the dir indexes of the snapshots and update the inode
  1965. * of the snapshots' parents after the snapshot creation, so there
  1966. * are some delayed items which are not dealt with. Now deal with
  1967. * them.
  1968. *
  1969. * We needn't worry that this operation will corrupt the snapshots,
  1970. * because all the tree which are snapshoted will be forced to COW
  1971. * the nodes and leaves.
  1972. */
  1973. ret = btrfs_run_delayed_items(trans);
  1974. if (ret)
  1975. goto unlock_reloc;
  1976. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1977. if (ret)
  1978. goto unlock_reloc;
  1979. /*
  1980. * make sure none of the code above managed to slip in a
  1981. * delayed item
  1982. */
  1983. btrfs_assert_delayed_root_empty(fs_info);
  1984. WARN_ON(cur_trans != trans->transaction);
  1985. /* btrfs_commit_tree_roots is responsible for getting the
  1986. * various roots consistent with each other. Every pointer
  1987. * in the tree of tree roots has to point to the most up to date
  1988. * root for every subvolume and other tree. So, we have to keep
  1989. * the tree logging code from jumping in and changing any
  1990. * of the trees.
  1991. *
  1992. * At this point in the commit, there can't be any tree-log
  1993. * writers, but a little lower down we drop the trans mutex
  1994. * and let new people in. By holding the tree_log_mutex
  1995. * from now until after the super is written, we avoid races
  1996. * with the tree-log code.
  1997. */
  1998. mutex_lock(&fs_info->tree_log_mutex);
  1999. ret = commit_fs_roots(trans);
  2000. if (ret)
  2001. goto unlock_tree_log;
  2002. /*
  2003. * Since the transaction is done, we can apply the pending changes
  2004. * before the next transaction.
  2005. */
  2006. btrfs_apply_pending_changes(fs_info);
  2007. /* commit_fs_roots gets rid of all the tree log roots, it is now
  2008. * safe to free the root of tree log roots
  2009. */
  2010. btrfs_free_log_root_tree(trans, fs_info);
  2011. /*
  2012. * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
  2013. * new delayed refs. Must handle them or qgroup can be wrong.
  2014. */
  2015. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  2016. if (ret)
  2017. goto unlock_tree_log;
  2018. /*
  2019. * Since fs roots are all committed, we can get a quite accurate
  2020. * new_roots. So let's do quota accounting.
  2021. */
  2022. ret = btrfs_qgroup_account_extents(trans);
  2023. if (ret < 0)
  2024. goto unlock_tree_log;
  2025. ret = commit_cowonly_roots(trans);
  2026. if (ret)
  2027. goto unlock_tree_log;
  2028. /*
  2029. * The tasks which save the space cache and inode cache may also
  2030. * update ->aborted, check it.
  2031. */
  2032. if (TRANS_ABORTED(cur_trans)) {
  2033. ret = cur_trans->aborted;
  2034. goto unlock_tree_log;
  2035. }
  2036. cur_trans = fs_info->running_transaction;
  2037. btrfs_set_root_node(&fs_info->tree_root->root_item,
  2038. fs_info->tree_root->node);
  2039. list_add_tail(&fs_info->tree_root->dirty_list,
  2040. &cur_trans->switch_commits);
  2041. btrfs_set_root_node(&fs_info->chunk_root->root_item,
  2042. fs_info->chunk_root->node);
  2043. list_add_tail(&fs_info->chunk_root->dirty_list,
  2044. &cur_trans->switch_commits);
  2045. switch_commit_roots(trans);
  2046. ASSERT(list_empty(&cur_trans->dirty_bgs));
  2047. ASSERT(list_empty(&cur_trans->io_bgs));
  2048. update_super_roots(fs_info);
  2049. btrfs_set_super_log_root(fs_info->super_copy, 0);
  2050. btrfs_set_super_log_root_level(fs_info->super_copy, 0);
  2051. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  2052. sizeof(*fs_info->super_copy));
  2053. btrfs_commit_device_sizes(cur_trans);
  2054. clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  2055. clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  2056. btrfs_trans_release_chunk_metadata(trans);
  2057. spin_lock(&fs_info->trans_lock);
  2058. cur_trans->state = TRANS_STATE_UNBLOCKED;
  2059. fs_info->running_transaction = NULL;
  2060. spin_unlock(&fs_info->trans_lock);
  2061. mutex_unlock(&fs_info->reloc_mutex);
  2062. wake_up(&fs_info->transaction_wait);
  2063. ret = btrfs_write_and_wait_transaction(trans);
  2064. if (ret) {
  2065. btrfs_handle_fs_error(fs_info, ret,
  2066. "Error while writing out transaction");
  2067. /*
  2068. * reloc_mutex has been unlocked, tree_log_mutex is still held
  2069. * but we can't jump to unlock_tree_log causing double unlock
  2070. */
  2071. mutex_unlock(&fs_info->tree_log_mutex);
  2072. goto scrub_continue;
  2073. }
  2074. ret = write_all_supers(fs_info, 0);
  2075. /*
  2076. * the super is written, we can safely allow the tree-loggers
  2077. * to go about their business
  2078. */
  2079. mutex_unlock(&fs_info->tree_log_mutex);
  2080. if (ret)
  2081. goto scrub_continue;
  2082. btrfs_finish_extent_commit(trans);
  2083. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
  2084. btrfs_clear_space_info_full(fs_info);
  2085. fs_info->last_trans_committed = cur_trans->transid;
  2086. /*
  2087. * We needn't acquire the lock here because there is no other task
  2088. * which can change it.
  2089. */
  2090. cur_trans->state = TRANS_STATE_COMPLETED;
  2091. wake_up(&cur_trans->commit_wait);
  2092. spin_lock(&fs_info->trans_lock);
  2093. list_del_init(&cur_trans->list);
  2094. spin_unlock(&fs_info->trans_lock);
  2095. btrfs_put_transaction(cur_trans);
  2096. btrfs_put_transaction(cur_trans);
  2097. if (trans->type & __TRANS_FREEZABLE)
  2098. sb_end_intwrite(fs_info->sb);
  2099. trace_btrfs_transaction_commit(trans->root);
  2100. btrfs_scrub_continue(fs_info);
  2101. if (current->journal_info == trans)
  2102. current->journal_info = NULL;
  2103. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  2104. return ret;
  2105. unlock_tree_log:
  2106. mutex_unlock(&fs_info->tree_log_mutex);
  2107. unlock_reloc:
  2108. mutex_unlock(&fs_info->reloc_mutex);
  2109. scrub_continue:
  2110. btrfs_scrub_continue(fs_info);
  2111. cleanup_transaction:
  2112. btrfs_trans_release_metadata(trans);
  2113. btrfs_cleanup_pending_block_groups(trans);
  2114. btrfs_trans_release_chunk_metadata(trans);
  2115. trans->block_rsv = NULL;
  2116. btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
  2117. if (current->journal_info == trans)
  2118. current->journal_info = NULL;
  2119. cleanup_transaction(trans, ret);
  2120. return ret;
  2121. }
  2122. /*
  2123. * return < 0 if error
  2124. * 0 if there are no more dead_roots at the time of call
  2125. * 1 there are more to be processed, call me again
  2126. *
  2127. * The return value indicates there are certainly more snapshots to delete, but
  2128. * if there comes a new one during processing, it may return 0. We don't mind,
  2129. * because btrfs_commit_super will poke cleaner thread and it will process it a
  2130. * few seconds later.
  2131. */
  2132. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
  2133. {
  2134. int ret;
  2135. struct btrfs_fs_info *fs_info = root->fs_info;
  2136. spin_lock(&fs_info->trans_lock);
  2137. if (list_empty(&fs_info->dead_roots)) {
  2138. spin_unlock(&fs_info->trans_lock);
  2139. return 0;
  2140. }
  2141. root = list_first_entry(&fs_info->dead_roots,
  2142. struct btrfs_root, root_list);
  2143. list_del_init(&root->root_list);
  2144. spin_unlock(&fs_info->trans_lock);
  2145. btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
  2146. btrfs_kill_all_delayed_nodes(root);
  2147. if (root->ino_cache_inode) {
  2148. iput(root->ino_cache_inode);
  2149. root->ino_cache_inode = NULL;
  2150. }
  2151. if (btrfs_header_backref_rev(root->node) <
  2152. BTRFS_MIXED_BACKREF_REV)
  2153. ret = btrfs_drop_snapshot(root, 0, 0);
  2154. else
  2155. ret = btrfs_drop_snapshot(root, 1, 0);
  2156. btrfs_put_root(root);
  2157. return (ret < 0) ? 0 : 1;
  2158. }
  2159. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
  2160. {
  2161. unsigned long prev;
  2162. unsigned long bit;
  2163. prev = xchg(&fs_info->pending_changes, 0);
  2164. if (!prev)
  2165. return;
  2166. bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
  2167. if (prev & bit)
  2168. btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2169. prev &= ~bit;
  2170. bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
  2171. if (prev & bit)
  2172. btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2173. prev &= ~bit;
  2174. bit = 1 << BTRFS_PENDING_COMMIT;
  2175. if (prev & bit)
  2176. btrfs_debug(fs_info, "pending commit done");
  2177. prev &= ~bit;
  2178. if (prev)
  2179. btrfs_warn(fs_info,
  2180. "unknown pending changes left 0x%lx, ignoring", prev);
  2181. }