transaction.c 54 KB

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