transaction.c 66 KB

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