transaction.c 61 KB

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