transaction.c 48 KB

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