transaction.c 66 KB

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