transaction.c 55 KB

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