transaction.c 55 KB

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