transaction.c 55 KB

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