transaction.c 62 KB

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