transaction.c 43 KB

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