transaction.c 63 KB

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