transaction.c 66 KB

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