transaction.c 62 KB

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