PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/jbd2/transaction.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 2225 lines | 1135 code | 248 blank | 842 comment | 224 complexity | aeb233a4b2fc90793f7b349382ba7d3a MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * linux/fs/jbd2/transaction.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
  5. *
  6. * Copyright 1998 Red Hat corp --- All Rights Reserved
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * Generic filesystem transaction handling code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages transactions (compound commits managed by the
  16. * journaling code) and handles (individual atomic operations by the
  17. * filesystem).
  18. */
  19. #include <linux/time.h>
  20. #include <linux/fs.h>
  21. #include <linux/jbd2.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/highmem.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/module.h>
  30. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
  31. static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
  32. /*
  33. * jbd2_get_transaction: obtain a new transaction_t object.
  34. *
  35. * Simply allocate and initialise a new transaction. Create it in
  36. * RUNNING state and add it to the current journal (which should not
  37. * have an existing running transaction: we only make a new transaction
  38. * once we have started to commit the old one).
  39. *
  40. * Preconditions:
  41. * The journal MUST be locked. We don't perform atomic mallocs on the
  42. * new transaction and we can't block without protecting against other
  43. * processes trying to touch the journal while it is in transition.
  44. *
  45. */
  46. static transaction_t *
  47. jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
  48. {
  49. transaction->t_journal = journal;
  50. transaction->t_state = T_RUNNING;
  51. transaction->t_start_time = ktime_get();
  52. transaction->t_tid = journal->j_transaction_sequence++;
  53. transaction->t_expires = jiffies + journal->j_commit_interval;
  54. spin_lock_init(&transaction->t_handle_lock);
  55. atomic_set(&transaction->t_updates, 0);
  56. atomic_set(&transaction->t_outstanding_credits, 0);
  57. atomic_set(&transaction->t_handle_count, 0);
  58. INIT_LIST_HEAD(&transaction->t_inode_list);
  59. INIT_LIST_HEAD(&transaction->t_private_list);
  60. /* Set up the commit timer for the new transaction. */
  61. journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
  62. add_timer(&journal->j_commit_timer);
  63. J_ASSERT(journal->j_running_transaction == NULL);
  64. journal->j_running_transaction = transaction;
  65. transaction->t_max_wait = 0;
  66. transaction->t_start = jiffies;
  67. return transaction;
  68. }
  69. /*
  70. * Handle management.
  71. *
  72. * A handle_t is an object which represents a single atomic update to a
  73. * filesystem, and which tracks all of the modifications which form part
  74. * of that one update.
  75. */
  76. /*
  77. * Update transaction's maximum wait time, if debugging is enabled.
  78. *
  79. * In order for t_max_wait to be reliable, it must be protected by a
  80. * lock. But doing so will mean that start_this_handle() can not be
  81. * run in parallel on SMP systems, which limits our scalability. So
  82. * unless debugging is enabled, we no longer update t_max_wait, which
  83. * means that maximum wait time reported by the jbd2_run_stats
  84. * tracepoint will always be zero.
  85. */
  86. static inline void update_t_max_wait(transaction_t *transaction,
  87. unsigned long ts)
  88. {
  89. #ifdef CONFIG_JBD2_DEBUG
  90. if (jbd2_journal_enable_debug &&
  91. time_after(transaction->t_start, ts)) {
  92. ts = jbd2_time_diff(ts, transaction->t_start);
  93. spin_lock(&transaction->t_handle_lock);
  94. if (ts > transaction->t_max_wait)
  95. transaction->t_max_wait = ts;
  96. spin_unlock(&transaction->t_handle_lock);
  97. }
  98. #endif
  99. }
  100. /*
  101. * start_this_handle: Given a handle, deal with any locking or stalling
  102. * needed to make sure that there is enough journal space for the handle
  103. * to begin. Attach the handle to a transaction and set up the
  104. * transaction's buffer credits.
  105. */
  106. static int start_this_handle(journal_t *journal, handle_t *handle,
  107. int gfp_mask)
  108. {
  109. transaction_t *transaction, *new_transaction = NULL;
  110. tid_t tid;
  111. int needed, need_to_start;
  112. int nblocks = handle->h_buffer_credits;
  113. unsigned long ts = jiffies;
  114. if (nblocks > journal->j_max_transaction_buffers) {
  115. printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
  116. current->comm, nblocks,
  117. journal->j_max_transaction_buffers);
  118. return -ENOSPC;
  119. }
  120. alloc_transaction:
  121. if (!journal->j_running_transaction) {
  122. new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
  123. if (!new_transaction) {
  124. /*
  125. * If __GFP_FS is not present, then we may be
  126. * being called from inside the fs writeback
  127. * layer, so we MUST NOT fail. Since
  128. * __GFP_NOFAIL is going away, we will arrange
  129. * to retry the allocation ourselves.
  130. */
  131. if ((gfp_mask & __GFP_FS) == 0) {
  132. congestion_wait(BLK_RW_ASYNC, HZ/50);
  133. goto alloc_transaction;
  134. }
  135. return -ENOMEM;
  136. }
  137. }
  138. jbd_debug(3, "New handle %p going live.\n", handle);
  139. /*
  140. * We need to hold j_state_lock until t_updates has been incremented,
  141. * for proper journal barrier handling
  142. */
  143. repeat:
  144. read_lock(&journal->j_state_lock);
  145. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  146. if (is_journal_aborted(journal) ||
  147. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  148. read_unlock(&journal->j_state_lock);
  149. kfree(new_transaction);
  150. return -EROFS;
  151. }
  152. /* Wait on the journal's transaction barrier if necessary */
  153. if (journal->j_barrier_count) {
  154. read_unlock(&journal->j_state_lock);
  155. wait_event(journal->j_wait_transaction_locked,
  156. journal->j_barrier_count == 0);
  157. goto repeat;
  158. }
  159. if (!journal->j_running_transaction) {
  160. read_unlock(&journal->j_state_lock);
  161. if (!new_transaction)
  162. goto alloc_transaction;
  163. write_lock(&journal->j_state_lock);
  164. if (!journal->j_running_transaction) {
  165. jbd2_get_transaction(journal, new_transaction);
  166. new_transaction = NULL;
  167. }
  168. write_unlock(&journal->j_state_lock);
  169. goto repeat;
  170. }
  171. transaction = journal->j_running_transaction;
  172. /*
  173. * If the current transaction is locked down for commit, wait for the
  174. * lock to be released.
  175. */
  176. if (transaction->t_state == T_LOCKED) {
  177. DEFINE_WAIT(wait);
  178. prepare_to_wait(&journal->j_wait_transaction_locked,
  179. &wait, TASK_UNINTERRUPTIBLE);
  180. read_unlock(&journal->j_state_lock);
  181. schedule();
  182. finish_wait(&journal->j_wait_transaction_locked, &wait);
  183. goto repeat;
  184. }
  185. /*
  186. * If there is not enough space left in the log to write all potential
  187. * buffers requested by this operation, we need to stall pending a log
  188. * checkpoint to free some more log space.
  189. */
  190. needed = atomic_add_return(nblocks,
  191. &transaction->t_outstanding_credits);
  192. if (needed > journal->j_max_transaction_buffers) {
  193. /*
  194. * If the current transaction is already too large, then start
  195. * to commit it: we can then go back and attach this handle to
  196. * a new transaction.
  197. */
  198. DEFINE_WAIT(wait);
  199. jbd_debug(2, "Handle %p starting new commit...\n", handle);
  200. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  201. prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
  202. TASK_UNINTERRUPTIBLE);
  203. tid = transaction->t_tid;
  204. need_to_start = !tid_geq(journal->j_commit_request, tid);
  205. read_unlock(&journal->j_state_lock);
  206. if (need_to_start)
  207. jbd2_log_start_commit(journal, tid);
  208. schedule();
  209. finish_wait(&journal->j_wait_transaction_locked, &wait);
  210. goto repeat;
  211. }
  212. /*
  213. * The commit code assumes that it can get enough log space
  214. * without forcing a checkpoint. This is *critical* for
  215. * correctness: a checkpoint of a buffer which is also
  216. * associated with a committing transaction creates a deadlock,
  217. * so commit simply cannot force through checkpoints.
  218. *
  219. * We must therefore ensure the necessary space in the journal
  220. * *before* starting to dirty potentially checkpointed buffers
  221. * in the new transaction.
  222. *
  223. * The worst part is, any transaction currently committing can
  224. * reduce the free space arbitrarily. Be careful to account for
  225. * those buffers when checkpointing.
  226. */
  227. /*
  228. * @@@ AKPM: This seems rather over-defensive. We're giving commit
  229. * a _lot_ of headroom: 1/4 of the journal plus the size of
  230. * the committing transaction. Really, we only need to give it
  231. * committing_transaction->t_outstanding_credits plus "enough" for
  232. * the log control blocks.
  233. * Also, this test is inconsistent with the matching one in
  234. * jbd2_journal_extend().
  235. */
  236. if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
  237. jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
  238. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  239. read_unlock(&journal->j_state_lock);
  240. write_lock(&journal->j_state_lock);
  241. if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
  242. __jbd2_log_wait_for_space(journal);
  243. write_unlock(&journal->j_state_lock);
  244. goto repeat;
  245. }
  246. /* OK, account for the buffers that this operation expects to
  247. * use and add the handle to the running transaction.
  248. */
  249. update_t_max_wait(transaction, ts);
  250. handle->h_transaction = transaction;
  251. atomic_inc(&transaction->t_updates);
  252. atomic_inc(&transaction->t_handle_count);
  253. jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
  254. handle, nblocks,
  255. atomic_read(&transaction->t_outstanding_credits),
  256. __jbd2_log_space_left(journal));
  257. read_unlock(&journal->j_state_lock);
  258. lock_map_acquire(&handle->h_lockdep_map);
  259. kfree(new_transaction);
  260. return 0;
  261. }
  262. static struct lock_class_key jbd2_handle_key;
  263. /* Allocate a new handle. This should probably be in a slab... */
  264. static handle_t *new_handle(int nblocks)
  265. {
  266. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  267. if (!handle)
  268. return NULL;
  269. memset(handle, 0, sizeof(*handle));
  270. handle->h_buffer_credits = nblocks;
  271. handle->h_ref = 1;
  272. lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
  273. &jbd2_handle_key, 0);
  274. return handle;
  275. }
  276. /**
  277. * handle_t *jbd2_journal_start() - Obtain a new handle.
  278. * @journal: Journal to start transaction on.
  279. * @nblocks: number of block buffer we might modify
  280. *
  281. * We make sure that the transaction can guarantee at least nblocks of
  282. * modified buffers in the log. We block until the log can guarantee
  283. * that much space.
  284. *
  285. * This function is visible to journal users (like ext3fs), so is not
  286. * called with the journal already locked.
  287. *
  288. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  289. * on failure.
  290. */
  291. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
  292. {
  293. handle_t *handle = journal_current_handle();
  294. int err;
  295. if (!journal)
  296. return ERR_PTR(-EROFS);
  297. if (handle) {
  298. J_ASSERT(handle->h_transaction->t_journal == journal);
  299. handle->h_ref++;
  300. return handle;
  301. }
  302. handle = new_handle(nblocks);
  303. if (!handle)
  304. return ERR_PTR(-ENOMEM);
  305. current->journal_info = handle;
  306. err = start_this_handle(journal, handle, gfp_mask);
  307. if (err < 0) {
  308. jbd2_free_handle(handle);
  309. current->journal_info = NULL;
  310. handle = ERR_PTR(err);
  311. }
  312. return handle;
  313. }
  314. EXPORT_SYMBOL(jbd2__journal_start);
  315. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  316. {
  317. return jbd2__journal_start(journal, nblocks, GFP_NOFS);
  318. }
  319. EXPORT_SYMBOL(jbd2_journal_start);
  320. /**
  321. * int jbd2_journal_extend() - extend buffer credits.
  322. * @handle: handle to 'extend'
  323. * @nblocks: nr blocks to try to extend by.
  324. *
  325. * Some transactions, such as large extends and truncates, can be done
  326. * atomically all at once or in several stages. The operation requests
  327. * a credit for a number of buffer modications in advance, but can
  328. * extend its credit if it needs more.
  329. *
  330. * jbd2_journal_extend tries to give the running handle more buffer credits.
  331. * It does not guarantee that allocation - this is a best-effort only.
  332. * The calling process MUST be able to deal cleanly with a failure to
  333. * extend here.
  334. *
  335. * Return 0 on success, non-zero on failure.
  336. *
  337. * return code < 0 implies an error
  338. * return code > 0 implies normal transaction-full status.
  339. */
  340. int jbd2_journal_extend(handle_t *handle, int nblocks)
  341. {
  342. transaction_t *transaction = handle->h_transaction;
  343. journal_t *journal = transaction->t_journal;
  344. int result;
  345. int wanted;
  346. result = -EIO;
  347. if (is_handle_aborted(handle))
  348. goto out;
  349. result = 1;
  350. read_lock(&journal->j_state_lock);
  351. /* Don't extend a locked-down transaction! */
  352. if (handle->h_transaction->t_state != T_RUNNING) {
  353. jbd_debug(3, "denied handle %p %d blocks: "
  354. "transaction not running\n", handle, nblocks);
  355. goto error_out;
  356. }
  357. spin_lock(&transaction->t_handle_lock);
  358. wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
  359. if (wanted > journal->j_max_transaction_buffers) {
  360. jbd_debug(3, "denied handle %p %d blocks: "
  361. "transaction too large\n", handle, nblocks);
  362. goto unlock;
  363. }
  364. if (wanted > __jbd2_log_space_left(journal)) {
  365. jbd_debug(3, "denied handle %p %d blocks: "
  366. "insufficient log space\n", handle, nblocks);
  367. goto unlock;
  368. }
  369. handle->h_buffer_credits += nblocks;
  370. atomic_add(nblocks, &transaction->t_outstanding_credits);
  371. result = 0;
  372. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  373. unlock:
  374. spin_unlock(&transaction->t_handle_lock);
  375. error_out:
  376. read_unlock(&journal->j_state_lock);
  377. out:
  378. return result;
  379. }
  380. /**
  381. * int jbd2_journal_restart() - restart a handle .
  382. * @handle: handle to restart
  383. * @nblocks: nr credits requested
  384. *
  385. * Restart a handle for a multi-transaction filesystem
  386. * operation.
  387. *
  388. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  389. * to a running handle, a call to jbd2_journal_restart will commit the
  390. * handle's transaction so far and reattach the handle to a new
  391. * transaction capabable of guaranteeing the requested number of
  392. * credits.
  393. */
  394. int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
  395. {
  396. transaction_t *transaction = handle->h_transaction;
  397. journal_t *journal = transaction->t_journal;
  398. tid_t tid;
  399. int need_to_start, ret;
  400. /* If we've had an abort of any type, don't even think about
  401. * actually doing the restart! */
  402. if (is_handle_aborted(handle))
  403. return 0;
  404. /*
  405. * First unlink the handle from its current transaction, and start the
  406. * commit on that.
  407. */
  408. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  409. J_ASSERT(journal_current_handle() == handle);
  410. read_lock(&journal->j_state_lock);
  411. spin_lock(&transaction->t_handle_lock);
  412. atomic_sub(handle->h_buffer_credits,
  413. &transaction->t_outstanding_credits);
  414. if (atomic_dec_and_test(&transaction->t_updates))
  415. wake_up(&journal->j_wait_updates);
  416. spin_unlock(&transaction->t_handle_lock);
  417. jbd_debug(2, "restarting handle %p\n", handle);
  418. tid = transaction->t_tid;
  419. need_to_start = !tid_geq(journal->j_commit_request, tid);
  420. read_unlock(&journal->j_state_lock);
  421. if (need_to_start)
  422. jbd2_log_start_commit(journal, tid);
  423. lock_map_release(&handle->h_lockdep_map);
  424. handle->h_buffer_credits = nblocks;
  425. ret = start_this_handle(journal, handle, gfp_mask);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(jbd2__journal_restart);
  429. int jbd2_journal_restart(handle_t *handle, int nblocks)
  430. {
  431. return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
  432. }
  433. EXPORT_SYMBOL(jbd2_journal_restart);
  434. /**
  435. * void jbd2_journal_lock_updates () - establish a transaction barrier.
  436. * @journal: Journal to establish a barrier on.
  437. *
  438. * This locks out any further updates from being started, and blocks
  439. * until all existing updates have completed, returning only once the
  440. * journal is in a quiescent state with no updates running.
  441. *
  442. * The journal lock should not be held on entry.
  443. */
  444. void jbd2_journal_lock_updates(journal_t *journal)
  445. {
  446. DEFINE_WAIT(wait);
  447. write_lock(&journal->j_state_lock);
  448. ++journal->j_barrier_count;
  449. /* Wait until there are no running updates */
  450. while (1) {
  451. transaction_t *transaction = journal->j_running_transaction;
  452. if (!transaction)
  453. break;
  454. spin_lock(&transaction->t_handle_lock);
  455. if (!atomic_read(&transaction->t_updates)) {
  456. spin_unlock(&transaction->t_handle_lock);
  457. break;
  458. }
  459. prepare_to_wait(&journal->j_wait_updates, &wait,
  460. TASK_UNINTERRUPTIBLE);
  461. spin_unlock(&transaction->t_handle_lock);
  462. write_unlock(&journal->j_state_lock);
  463. schedule();
  464. finish_wait(&journal->j_wait_updates, &wait);
  465. write_lock(&journal->j_state_lock);
  466. }
  467. write_unlock(&journal->j_state_lock);
  468. /*
  469. * We have now established a barrier against other normal updates, but
  470. * we also need to barrier against other jbd2_journal_lock_updates() calls
  471. * to make sure that we serialise special journal-locked operations
  472. * too.
  473. */
  474. mutex_lock(&journal->j_barrier);
  475. }
  476. /**
  477. * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
  478. * @journal: Journal to release the barrier on.
  479. *
  480. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  481. *
  482. * Should be called without the journal lock held.
  483. */
  484. void jbd2_journal_unlock_updates (journal_t *journal)
  485. {
  486. J_ASSERT(journal->j_barrier_count != 0);
  487. mutex_unlock(&journal->j_barrier);
  488. write_lock(&journal->j_state_lock);
  489. --journal->j_barrier_count;
  490. write_unlock(&journal->j_state_lock);
  491. wake_up(&journal->j_wait_transaction_locked);
  492. }
  493. static void warn_dirty_buffer(struct buffer_head *bh)
  494. {
  495. char b[BDEVNAME_SIZE];
  496. printk(KERN_WARNING
  497. "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
  498. "There's a risk of filesystem corruption in case of system "
  499. "crash.\n",
  500. bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
  501. }
  502. /*
  503. * If the buffer is already part of the current transaction, then there
  504. * is nothing we need to do. If it is already part of a prior
  505. * transaction which we are still committing to disk, then we need to
  506. * make sure that we do not overwrite the old copy: we do copy-out to
  507. * preserve the copy going to disk. We also account the buffer against
  508. * the handle's metadata buffer credits (unless the buffer is already
  509. * part of the transaction, that is).
  510. *
  511. */
  512. static int
  513. do_get_write_access(handle_t *handle, struct journal_head *jh,
  514. int force_copy)
  515. {
  516. struct buffer_head *bh;
  517. transaction_t *transaction;
  518. journal_t *journal;
  519. int error;
  520. char *frozen_buffer = NULL;
  521. int need_copy = 0;
  522. if (is_handle_aborted(handle))
  523. return -EROFS;
  524. transaction = handle->h_transaction;
  525. journal = transaction->t_journal;
  526. jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  527. JBUFFER_TRACE(jh, "entry");
  528. repeat:
  529. bh = jh2bh(jh);
  530. /* @@@ Need to check for errors here at some point. */
  531. lock_buffer(bh);
  532. jbd_lock_bh_state(bh);
  533. /* We now hold the buffer lock so it is safe to query the buffer
  534. * state. Is the buffer dirty?
  535. *
  536. * If so, there are two possibilities. The buffer may be
  537. * non-journaled, and undergoing a quite legitimate writeback.
  538. * Otherwise, it is journaled, and we don't expect dirty buffers
  539. * in that state (the buffers should be marked JBD_Dirty
  540. * instead.) So either the IO is being done under our own
  541. * control and this is a bug, or it's a third party IO such as
  542. * dump(8) (which may leave the buffer scheduled for read ---
  543. * ie. locked but not dirty) or tune2fs (which may actually have
  544. * the buffer dirtied, ugh.) */
  545. if (buffer_dirty(bh)) {
  546. /*
  547. * First question: is this buffer already part of the current
  548. * transaction or the existing committing transaction?
  549. */
  550. if (jh->b_transaction) {
  551. J_ASSERT_JH(jh,
  552. jh->b_transaction == transaction ||
  553. jh->b_transaction ==
  554. journal->j_committing_transaction);
  555. if (jh->b_next_transaction)
  556. J_ASSERT_JH(jh, jh->b_next_transaction ==
  557. transaction);
  558. warn_dirty_buffer(bh);
  559. }
  560. /*
  561. * In any case we need to clean the dirty flag and we must
  562. * do it under the buffer lock to be sure we don't race
  563. * with running write-out.
  564. */
  565. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  566. clear_buffer_dirty(bh);
  567. set_buffer_jbddirty(bh);
  568. }
  569. unlock_buffer(bh);
  570. error = -EROFS;
  571. if (is_handle_aborted(handle)) {
  572. jbd_unlock_bh_state(bh);
  573. goto out;
  574. }
  575. error = 0;
  576. /*
  577. * The buffer is already part of this transaction if b_transaction or
  578. * b_next_transaction points to it
  579. */
  580. if (jh->b_transaction == transaction ||
  581. jh->b_next_transaction == transaction)
  582. goto done;
  583. /*
  584. * this is the first time this transaction is touching this buffer,
  585. * reset the modified flag
  586. */
  587. jh->b_modified = 0;
  588. /*
  589. * If there is already a copy-out version of this buffer, then we don't
  590. * need to make another one
  591. */
  592. if (jh->b_frozen_data) {
  593. JBUFFER_TRACE(jh, "has frozen data");
  594. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  595. jh->b_next_transaction = transaction;
  596. goto done;
  597. }
  598. /* Is there data here we need to preserve? */
  599. if (jh->b_transaction && jh->b_transaction != transaction) {
  600. JBUFFER_TRACE(jh, "owned by older transaction");
  601. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  602. J_ASSERT_JH(jh, jh->b_transaction ==
  603. journal->j_committing_transaction);
  604. /* There is one case we have to be very careful about.
  605. * If the committing transaction is currently writing
  606. * this buffer out to disk and has NOT made a copy-out,
  607. * then we cannot modify the buffer contents at all
  608. * right now. The essence of copy-out is that it is the
  609. * extra copy, not the primary copy, which gets
  610. * journaled. If the primary copy is already going to
  611. * disk then we cannot do copy-out here. */
  612. if (jh->b_jlist == BJ_Shadow) {
  613. DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
  614. wait_queue_head_t *wqh;
  615. wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
  616. JBUFFER_TRACE(jh, "on shadow: sleep");
  617. jbd_unlock_bh_state(bh);
  618. /* commit wakes up all shadow buffers after IO */
  619. for ( ; ; ) {
  620. prepare_to_wait(wqh, &wait.wait,
  621. TASK_UNINTERRUPTIBLE);
  622. if (jh->b_jlist != BJ_Shadow)
  623. break;
  624. schedule();
  625. }
  626. finish_wait(wqh, &wait.wait);
  627. goto repeat;
  628. }
  629. /* Only do the copy if the currently-owning transaction
  630. * still needs it. If it is on the Forget list, the
  631. * committing transaction is past that stage. The
  632. * buffer had better remain locked during the kmalloc,
  633. * but that should be true --- we hold the journal lock
  634. * still and the buffer is already on the BUF_JOURNAL
  635. * list so won't be flushed.
  636. *
  637. * Subtle point, though: if this is a get_undo_access,
  638. * then we will be relying on the frozen_data to contain
  639. * the new value of the committed_data record after the
  640. * transaction, so we HAVE to force the frozen_data copy
  641. * in that case. */
  642. if (jh->b_jlist != BJ_Forget || force_copy) {
  643. JBUFFER_TRACE(jh, "generate frozen data");
  644. if (!frozen_buffer) {
  645. JBUFFER_TRACE(jh, "allocate memory for buffer");
  646. jbd_unlock_bh_state(bh);
  647. frozen_buffer =
  648. jbd2_alloc(jh2bh(jh)->b_size,
  649. GFP_NOFS);
  650. if (!frozen_buffer) {
  651. printk(KERN_EMERG
  652. "%s: OOM for frozen_buffer\n",
  653. __func__);
  654. JBUFFER_TRACE(jh, "oom!");
  655. error = -ENOMEM;
  656. jbd_lock_bh_state(bh);
  657. goto done;
  658. }
  659. goto repeat;
  660. }
  661. jh->b_frozen_data = frozen_buffer;
  662. frozen_buffer = NULL;
  663. need_copy = 1;
  664. }
  665. jh->b_next_transaction = transaction;
  666. }
  667. /*
  668. * Finally, if the buffer is not journaled right now, we need to make
  669. * sure it doesn't get written to disk before the caller actually
  670. * commits the new data
  671. */
  672. if (!jh->b_transaction) {
  673. JBUFFER_TRACE(jh, "no transaction");
  674. J_ASSERT_JH(jh, !jh->b_next_transaction);
  675. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  676. spin_lock(&journal->j_list_lock);
  677. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  678. spin_unlock(&journal->j_list_lock);
  679. }
  680. done:
  681. if (need_copy) {
  682. struct page *page;
  683. int offset;
  684. char *source;
  685. J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
  686. "Possible IO failure.\n");
  687. page = jh2bh(jh)->b_page;
  688. offset = offset_in_page(jh2bh(jh)->b_data);
  689. source = kmap_atomic(page, KM_USER0);
  690. /* Fire data frozen trigger just before we copy the data */
  691. jbd2_buffer_frozen_trigger(jh, source + offset,
  692. jh->b_triggers);
  693. memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
  694. kunmap_atomic(source, KM_USER0);
  695. /*
  696. * Now that the frozen data is saved off, we need to store
  697. * any matching triggers.
  698. */
  699. jh->b_frozen_triggers = jh->b_triggers;
  700. }
  701. jbd_unlock_bh_state(bh);
  702. /*
  703. * If we are about to journal a buffer, then any revoke pending on it is
  704. * no longer valid
  705. */
  706. jbd2_journal_cancel_revoke(handle, jh);
  707. out:
  708. if (unlikely(frozen_buffer)) /* It's usually NULL */
  709. jbd2_free(frozen_buffer, bh->b_size);
  710. JBUFFER_TRACE(jh, "exit");
  711. return error;
  712. }
  713. /**
  714. * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
  715. * @handle: transaction to add buffer modifications to
  716. * @bh: bh to be used for metadata writes
  717. *
  718. * Returns an error code or 0 on success.
  719. *
  720. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  721. * because we're write()ing a buffer which is also part of a shared mapping.
  722. */
  723. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  724. {
  725. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  726. int rc;
  727. /* We do not want to get caught playing with fields which the
  728. * log thread also manipulates. Make sure that the buffer
  729. * completes any outstanding IO before proceeding. */
  730. rc = do_get_write_access(handle, jh, 0);
  731. jbd2_journal_put_journal_head(jh);
  732. return rc;
  733. }
  734. /*
  735. * When the user wants to journal a newly created buffer_head
  736. * (ie. getblk() returned a new buffer and we are going to populate it
  737. * manually rather than reading off disk), then we need to keep the
  738. * buffer_head locked until it has been completely filled with new
  739. * data. In this case, we should be able to make the assertion that
  740. * the bh is not already part of an existing transaction.
  741. *
  742. * The buffer should already be locked by the caller by this point.
  743. * There is no lock ranking violation: it was a newly created,
  744. * unlocked buffer beforehand. */
  745. /**
  746. * int jbd2_journal_get_create_access () - notify intent to use newly created bh
  747. * @handle: transaction to new buffer to
  748. * @bh: new buffer.
  749. *
  750. * Call this if you create a new bh.
  751. */
  752. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  753. {
  754. transaction_t *transaction = handle->h_transaction;
  755. journal_t *journal = transaction->t_journal;
  756. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  757. int err;
  758. jbd_debug(5, "journal_head %p\n", jh);
  759. err = -EROFS;
  760. if (is_handle_aborted(handle))
  761. goto out;
  762. err = 0;
  763. JBUFFER_TRACE(jh, "entry");
  764. /*
  765. * The buffer may already belong to this transaction due to pre-zeroing
  766. * in the filesystem's new_block code. It may also be on the previous,
  767. * committing transaction's lists, but it HAS to be in Forget state in
  768. * that case: the transaction must have deleted the buffer for it to be
  769. * reused here.
  770. */
  771. jbd_lock_bh_state(bh);
  772. spin_lock(&journal->j_list_lock);
  773. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  774. jh->b_transaction == NULL ||
  775. (jh->b_transaction == journal->j_committing_transaction &&
  776. jh->b_jlist == BJ_Forget)));
  777. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  778. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  779. if (jh->b_transaction == NULL) {
  780. /*
  781. * Previous jbd2_journal_forget() could have left the buffer
  782. * with jbddirty bit set because it was being committed. When
  783. * the commit finished, we've filed the buffer for
  784. * checkpointing and marked it dirty. Now we are reallocating
  785. * the buffer so the transaction freeing it must have
  786. * committed and so it's safe to clear the dirty bit.
  787. */
  788. clear_buffer_dirty(jh2bh(jh));
  789. /* first access by this transaction */
  790. jh->b_modified = 0;
  791. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  792. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  793. } else if (jh->b_transaction == journal->j_committing_transaction) {
  794. /* first access by this transaction */
  795. jh->b_modified = 0;
  796. JBUFFER_TRACE(jh, "set next transaction");
  797. jh->b_next_transaction = transaction;
  798. }
  799. spin_unlock(&journal->j_list_lock);
  800. jbd_unlock_bh_state(bh);
  801. /*
  802. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  803. * blocks which contain freed but then revoked metadata. We need
  804. * to cancel the revoke in case we end up freeing it yet again
  805. * and the reallocating as data - this would cause a second revoke,
  806. * which hits an assertion error.
  807. */
  808. JBUFFER_TRACE(jh, "cancelling revoke");
  809. jbd2_journal_cancel_revoke(handle, jh);
  810. out:
  811. jbd2_journal_put_journal_head(jh);
  812. return err;
  813. }
  814. /**
  815. * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  816. * non-rewindable consequences
  817. * @handle: transaction
  818. * @bh: buffer to undo
  819. *
  820. * Sometimes there is a need to distinguish between metadata which has
  821. * been committed to disk and that which has not. The ext3fs code uses
  822. * this for freeing and allocating space, we have to make sure that we
  823. * do not reuse freed space until the deallocation has been committed,
  824. * since if we overwrote that space we would make the delete
  825. * un-rewindable in case of a crash.
  826. *
  827. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  828. * buffer for parts of non-rewindable operations such as delete
  829. * operations on the bitmaps. The journaling code must keep a copy of
  830. * the buffer's contents prior to the undo_access call until such time
  831. * as we know that the buffer has definitely been committed to disk.
  832. *
  833. * We never need to know which transaction the committed data is part
  834. * of, buffers touched here are guaranteed to be dirtied later and so
  835. * will be committed to a new transaction in due course, at which point
  836. * we can discard the old committed data pointer.
  837. *
  838. * Returns error number or 0 on success.
  839. */
  840. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  841. {
  842. int err;
  843. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  844. char *committed_data = NULL;
  845. JBUFFER_TRACE(jh, "entry");
  846. /*
  847. * Do this first --- it can drop the journal lock, so we want to
  848. * make sure that obtaining the committed_data is done
  849. * atomically wrt. completion of any outstanding commits.
  850. */
  851. err = do_get_write_access(handle, jh, 1);
  852. if (err)
  853. goto out;
  854. repeat:
  855. if (!jh->b_committed_data) {
  856. committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
  857. if (!committed_data) {
  858. printk(KERN_EMERG "%s: No memory for committed data\n",
  859. __func__);
  860. err = -ENOMEM;
  861. goto out;
  862. }
  863. }
  864. jbd_lock_bh_state(bh);
  865. if (!jh->b_committed_data) {
  866. /* Copy out the current buffer contents into the
  867. * preserved, committed copy. */
  868. JBUFFER_TRACE(jh, "generate b_committed data");
  869. if (!committed_data) {
  870. jbd_unlock_bh_state(bh);
  871. goto repeat;
  872. }
  873. jh->b_committed_data = committed_data;
  874. committed_data = NULL;
  875. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  876. }
  877. jbd_unlock_bh_state(bh);
  878. out:
  879. jbd2_journal_put_journal_head(jh);
  880. if (unlikely(committed_data))
  881. jbd2_free(committed_data, bh->b_size);
  882. return err;
  883. }
  884. /**
  885. * void jbd2_journal_set_triggers() - Add triggers for commit writeout
  886. * @bh: buffer to trigger on
  887. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  888. *
  889. * Set any triggers on this journal_head. This is always safe, because
  890. * triggers for a committing buffer will be saved off, and triggers for
  891. * a running transaction will match the buffer in that transaction.
  892. *
  893. * Call with NULL to clear the triggers.
  894. */
  895. void jbd2_journal_set_triggers(struct buffer_head *bh,
  896. struct jbd2_buffer_trigger_type *type)
  897. {
  898. struct journal_head *jh = bh2jh(bh);
  899. jh->b_triggers = type;
  900. }
  901. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  902. struct jbd2_buffer_trigger_type *triggers)
  903. {
  904. struct buffer_head *bh = jh2bh(jh);
  905. if (!triggers || !triggers->t_frozen)
  906. return;
  907. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  908. }
  909. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  910. struct jbd2_buffer_trigger_type *triggers)
  911. {
  912. if (!triggers || !triggers->t_abort)
  913. return;
  914. triggers->t_abort(triggers, jh2bh(jh));
  915. }
  916. /**
  917. * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  918. * @handle: transaction to add buffer to.
  919. * @bh: buffer to mark
  920. *
  921. * mark dirty metadata which needs to be journaled as part of the current
  922. * transaction.
  923. *
  924. * The buffer is placed on the transaction's metadata list and is marked
  925. * as belonging to the transaction.
  926. *
  927. * Returns error number or 0 on success.
  928. *
  929. * Special care needs to be taken if the buffer already belongs to the
  930. * current committing transaction (in which case we should have frozen
  931. * data present for that commit). In that case, we don't relink the
  932. * buffer: that only gets done when the old transaction finally
  933. * completes its commit.
  934. */
  935. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  936. {
  937. transaction_t *transaction = handle->h_transaction;
  938. journal_t *journal = transaction->t_journal;
  939. struct journal_head *jh = bh2jh(bh);
  940. jbd_debug(5, "journal_head %p\n", jh);
  941. JBUFFER_TRACE(jh, "entry");
  942. if (is_handle_aborted(handle))
  943. goto out;
  944. jbd_lock_bh_state(bh);
  945. if (jh->b_modified == 0) {
  946. /*
  947. * This buffer's got modified and becoming part
  948. * of the transaction. This needs to be done
  949. * once a transaction -bzzz
  950. */
  951. jh->b_modified = 1;
  952. J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
  953. handle->h_buffer_credits--;
  954. }
  955. /*
  956. * fastpath, to avoid expensive locking. If this buffer is already
  957. * on the running transaction's metadata list there is nothing to do.
  958. * Nobody can take it off again because there is a handle open.
  959. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  960. * result in this test being false, so we go in and take the locks.
  961. */
  962. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  963. JBUFFER_TRACE(jh, "fastpath");
  964. J_ASSERT_JH(jh, jh->b_transaction ==
  965. journal->j_running_transaction);
  966. goto out_unlock_bh;
  967. }
  968. set_buffer_jbddirty(bh);
  969. /*
  970. * Metadata already on the current transaction list doesn't
  971. * need to be filed. Metadata on another transaction's list must
  972. * be committing, and will be refiled once the commit completes:
  973. * leave it alone for now.
  974. */
  975. if (jh->b_transaction != transaction) {
  976. JBUFFER_TRACE(jh, "already on other transaction");
  977. J_ASSERT_JH(jh, jh->b_transaction ==
  978. journal->j_committing_transaction);
  979. J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
  980. /* And this case is illegal: we can't reuse another
  981. * transaction's data buffer, ever. */
  982. goto out_unlock_bh;
  983. }
  984. /* That test should have eliminated the following case: */
  985. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  986. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  987. spin_lock(&journal->j_list_lock);
  988. __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
  989. spin_unlock(&journal->j_list_lock);
  990. out_unlock_bh:
  991. jbd_unlock_bh_state(bh);
  992. out:
  993. JBUFFER_TRACE(jh, "exit");
  994. return 0;
  995. }
  996. /*
  997. * jbd2_journal_release_buffer: undo a get_write_access without any buffer
  998. * updates, if the update decided in the end that it didn't need access.
  999. *
  1000. */
  1001. void
  1002. jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
  1003. {
  1004. BUFFER_TRACE(bh, "entry");
  1005. }
  1006. /**
  1007. * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1008. * @handle: transaction handle
  1009. * @bh: bh to 'forget'
  1010. *
  1011. * We can only do the bforget if there are no commits pending against the
  1012. * buffer. If the buffer is dirty in the current running transaction we
  1013. * can safely unlink it.
  1014. *
  1015. * bh may not be a journalled buffer at all - it may be a non-JBD
  1016. * buffer which came off the hashtable. Check for this.
  1017. *
  1018. * Decrements bh->b_count by one.
  1019. *
  1020. * Allow this call even if the handle has aborted --- it may be part of
  1021. * the caller's cleanup after an abort.
  1022. */
  1023. int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
  1024. {
  1025. transaction_t *transaction = handle->h_transaction;
  1026. journal_t *journal = transaction->t_journal;
  1027. struct journal_head *jh;
  1028. int drop_reserve = 0;
  1029. int err = 0;
  1030. int was_modified = 0;
  1031. BUFFER_TRACE(bh, "entry");
  1032. jbd_lock_bh_state(bh);
  1033. spin_lock(&journal->j_list_lock);
  1034. if (!buffer_jbd(bh))
  1035. goto not_jbd;
  1036. jh = bh2jh(bh);
  1037. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1038. * Don't do any jbd operations, and return an error. */
  1039. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1040. "inconsistent data on disk")) {
  1041. err = -EIO;
  1042. goto not_jbd;
  1043. }
  1044. /* keep track of wether or not this transaction modified us */
  1045. was_modified = jh->b_modified;
  1046. /*
  1047. * The buffer's going from the transaction, we must drop
  1048. * all references -bzzz
  1049. */
  1050. jh->b_modified = 0;
  1051. if (jh->b_transaction == handle->h_transaction) {
  1052. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1053. /* If we are forgetting a buffer which is already part
  1054. * of this transaction, then we can just drop it from
  1055. * the transaction immediately. */
  1056. clear_buffer_dirty(bh);
  1057. clear_buffer_jbddirty(bh);
  1058. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1059. /*
  1060. * we only want to drop a reference if this transaction
  1061. * modified the buffer
  1062. */
  1063. if (was_modified)
  1064. drop_reserve = 1;
  1065. /*
  1066. * We are no longer going to journal this buffer.
  1067. * However, the commit of this transaction is still
  1068. * important to the buffer: the delete that we are now
  1069. * processing might obsolete an old log entry, so by
  1070. * committing, we can satisfy the buffer's checkpoint.
  1071. *
  1072. * So, if we have a checkpoint on the buffer, we should
  1073. * now refile the buffer on our BJ_Forget list so that
  1074. * we know to remove the checkpoint after we commit.
  1075. */
  1076. if (jh->b_cp_transaction) {
  1077. __jbd2_journal_temp_unlink_buffer(jh);
  1078. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1079. } else {
  1080. __jbd2_journal_unfile_buffer(jh);
  1081. if (!buffer_jbd(bh)) {
  1082. spin_unlock(&journal->j_list_lock);
  1083. jbd_unlock_bh_state(bh);
  1084. __bforget(bh);
  1085. goto drop;
  1086. }
  1087. }
  1088. } else if (jh->b_transaction) {
  1089. J_ASSERT_JH(jh, (jh->b_transaction ==
  1090. journal->j_committing_transaction));
  1091. /* However, if the buffer is still owned by a prior
  1092. * (committing) transaction, we can't drop it yet... */
  1093. JBUFFER_TRACE(jh, "belongs to older transaction");
  1094. /* ... but we CAN drop it from the new transaction if we
  1095. * have also modified it since the original commit. */
  1096. if (jh->b_next_transaction) {
  1097. J_ASSERT(jh->b_next_transaction == transaction);
  1098. jh->b_next_transaction = NULL;
  1099. /*
  1100. * only drop a reference if this transaction modified
  1101. * the buffer
  1102. */
  1103. if (was_modified)
  1104. drop_reserve = 1;
  1105. }
  1106. }
  1107. not_jbd:
  1108. spin_unlock(&journal->j_list_lock);
  1109. jbd_unlock_bh_state(bh);
  1110. __brelse(bh);
  1111. drop:
  1112. if (drop_reserve) {
  1113. /* no need to reserve log space for this block -bzzz */
  1114. handle->h_buffer_credits++;
  1115. }
  1116. return err;
  1117. }
  1118. /**
  1119. * int jbd2_journal_stop() - complete a transaction
  1120. * @handle: tranaction to complete.
  1121. *
  1122. * All done for a particular handle.
  1123. *
  1124. * There is not much action needed here. We just return any remaining
  1125. * buffer credits to the transaction and remove the handle. The only
  1126. * complication is that we need to start a commit operation if the
  1127. * filesystem is marked for synchronous update.
  1128. *
  1129. * jbd2_journal_stop itself will not usually return an error, but it may
  1130. * do so in unusual circumstances. In particular, expect it to
  1131. * return -EIO if a jbd2_journal_abort has been executed since the
  1132. * transaction began.
  1133. */
  1134. int jbd2_journal_stop(handle_t *handle)
  1135. {
  1136. transaction_t *transaction = handle->h_transaction;
  1137. journal_t *journal = transaction->t_journal;
  1138. int err, wait_for_commit = 0;
  1139. tid_t tid;
  1140. pid_t pid;
  1141. J_ASSERT(journal_current_handle() == handle);
  1142. if (is_handle_aborted(handle))
  1143. err = -EIO;
  1144. else {
  1145. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  1146. err = 0;
  1147. }
  1148. if (--handle->h_ref > 0) {
  1149. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1150. handle->h_ref);
  1151. return err;
  1152. }
  1153. jbd_debug(4, "Handle %p going down\n", handle);
  1154. /*
  1155. * Implement synchronous transaction batching. If the handle
  1156. * was synchronous, don't force a commit immediately. Let's
  1157. * yield and let another thread piggyback onto this
  1158. * transaction. Keep doing that while new threads continue to
  1159. * arrive. It doesn't cost much - we're about to run a commit
  1160. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1161. * operations by 30x or more...
  1162. *
  1163. * We try and optimize the sleep time against what the
  1164. * underlying disk can do, instead of having a static sleep
  1165. * time. This is useful for the case where our storage is so
  1166. * fast that it is more optimal to go ahead and force a flush
  1167. * and wait for the transaction to be committed than it is to
  1168. * wait for an arbitrary amount of time for new writers to
  1169. * join the transaction. We achieve this by measuring how
  1170. * long it takes to commit a transaction, and compare it with
  1171. * how long this transaction has been running, and if run time
  1172. * < commit time then we sleep for the delta and commit. This
  1173. * greatly helps super fast disks that would see slowdowns as
  1174. * more threads started doing fsyncs.
  1175. *
  1176. * But don't do this if this process was the most recent one
  1177. * to perform a synchronous write. We do this to detect the
  1178. * case where a single process is doing a stream of sync
  1179. * writes. No point in waiting for joiners in that case.
  1180. */
  1181. pid = current->pid;
  1182. if (handle->h_sync && journal->j_last_sync_writer != pid) {
  1183. u64 commit_time, trans_time;
  1184. journal->j_last_sync_writer = pid;
  1185. read_lock(&journal->j_state_lock);
  1186. commit_time = journal->j_average_commit_time;
  1187. read_unlock(&journal->j_state_lock);
  1188. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1189. transaction->t_start_time));
  1190. commit_time = max_t(u64, commit_time,
  1191. 1000*journal->j_min_batch_time);
  1192. commit_time = min_t(u64, commit_time,
  1193. 1000*journal->j_max_batch_time);
  1194. if (trans_time < commit_time) {
  1195. ktime_t expires = ktime_add_ns(ktime_get(),
  1196. commit_time);
  1197. set_current_state(TASK_UNINTERRUPTIBLE);
  1198. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1199. }
  1200. }
  1201. if (handle->h_sync)
  1202. transaction->t_synchronous_commit = 1;
  1203. current->journal_info = NULL;
  1204. atomic_sub(handle->h_buffer_credits,
  1205. &transaction->t_outstanding_credits);
  1206. /*
  1207. * If the handle is marked SYNC, we need to set another commit
  1208. * going! We also want to force a commit if the current
  1209. * transaction is occupying too much of the log, or if the
  1210. * transaction is too old now.
  1211. */
  1212. if (handle->h_sync ||
  1213. (atomic_read(&transaction->t_outstanding_credits) >
  1214. journal->j_max_transaction_buffers) ||
  1215. time_after_eq(jiffies, transaction->t_expires)) {
  1216. /* Do this even for aborted journals: an abort still
  1217. * completes the commit thread, it just doesn't write
  1218. * anything to disk. */
  1219. jbd_debug(2, "transaction too old, requesting commit for "
  1220. "handle %p\n", handle);
  1221. /* This is non-blocking */
  1222. jbd2_log_start_commit(journal, transaction->t_tid);
  1223. /*
  1224. * Special case: JBD2_SYNC synchronous updates require us
  1225. * to wait for the commit to complete.
  1226. */
  1227. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1228. wait_for_commit = 1;
  1229. }
  1230. /*
  1231. * Once we drop t_updates, if it goes to zero the transaction
  1232. * could start committing on us and eventually disappear. So
  1233. * once we do this, we must not dereference transaction
  1234. * pointer again.
  1235. */
  1236. tid = transaction->t_tid;
  1237. if (atomic_dec_and_test(&transaction->t_updates)) {
  1238. wake_up(&journal->j_wait_updates);
  1239. if (journal->j_barrier_count)
  1240. wake_up(&journal->j_wait_transaction_locked);
  1241. }
  1242. if (wait_for_commit)
  1243. err = jbd2_log_wait_commit(journal, tid);
  1244. lock_map_release(&handle->h_lockdep_map);
  1245. jbd2_free_handle(handle);
  1246. return err;
  1247. }
  1248. /**
  1249. * int jbd2_journal_force_commit() - force any uncommitted transactions
  1250. * @journal: journal to force
  1251. *
  1252. * For synchronous operations: force any uncommitted transactions
  1253. * to disk. May seem kludgy, but it reuses all the handle batching
  1254. * code in a very simple manner.
  1255. */
  1256. int jbd2_journal_force_commit(journal_t *journal)
  1257. {
  1258. handle_t *handle;
  1259. int ret;
  1260. handle = jbd2_journal_start(journal, 1);
  1261. if (IS_ERR(handle)) {
  1262. ret = PTR_ERR(handle);
  1263. } else {
  1264. handle->h_sync = 1;
  1265. ret = jbd2_journal_stop(handle);
  1266. }
  1267. return ret;
  1268. }
  1269. /*
  1270. *
  1271. * List management code snippets: various functions for manipulating the
  1272. * transaction buffer lists.
  1273. *
  1274. */
  1275. /*
  1276. * Append a buffer to a transaction list, given the transaction's list head
  1277. * pointer.
  1278. *
  1279. * j_list_lock is held.
  1280. *
  1281. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1282. */
  1283. static inline void
  1284. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1285. {
  1286. if (!*list) {
  1287. jh->b_tnext = jh->b_tprev = jh;
  1288. *list = jh;
  1289. } else {
  1290. /* Insert at the tail of the list to preserve order */
  1291. struct journal_head *first = *list, *last = first->b_tprev;
  1292. jh->b_tprev = last;
  1293. jh->b_tnext = first;
  1294. last->b_tnext = first->b_tprev = jh;
  1295. }
  1296. }
  1297. /*
  1298. * Remove a buffer from a transaction list, given the transaction's list
  1299. * head pointer.
  1300. *
  1301. * Called with j_list_lock held, and the journal may not be locked.
  1302. *
  1303. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1304. */
  1305. static inline void
  1306. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1307. {
  1308. if (*list == jh) {
  1309. *list = jh->b_tnext;
  1310. if (*list == jh)
  1311. *list = NULL;
  1312. }
  1313. jh->b_tprev->b_tnext = jh->b_tnext;
  1314. jh->b_tnext->b_tprev = jh->b_tprev;
  1315. }
  1316. /*
  1317. * Remove a buffer from the appropriate transaction list.
  1318. *
  1319. * Note that this function can *change* the value of
  1320. * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
  1321. * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
  1322. * of these pointers, it could go bad. Generally the caller needs to re-read
  1323. * the pointer from the transaction_t.
  1324. *
  1325. * Called under j_list_lock. The journal may not be locked.
  1326. */
  1327. void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1328. {
  1329. struct journal_head **list = NULL;
  1330. transaction_t *transaction;
  1331. struct buffer_head *bh = jh2bh(jh);
  1332. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1333. transaction = jh->b_transaction;
  1334. if (transaction)
  1335. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1336. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1337. if (jh->b_jlist != BJ_None)
  1338. J_ASSERT_JH(jh, transaction != NULL);
  1339. switch (jh->b_jlist) {
  1340. case BJ_None:
  1341. return;
  1342. case BJ_Metadata:
  1343. transaction->t_nr_buffers--;
  1344. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1345. list = &transaction->t_buffers;
  1346. break;
  1347. case BJ_Forget:
  1348. list = &transaction->t_forget;
  1349. break;
  1350. case BJ_IO:
  1351. list = &transaction->t_iobuf_list;
  1352. break;
  1353. case BJ_Shadow:
  1354. list = &transaction->t_shadow_list;
  1355. break;
  1356. case BJ_LogCtl:
  1357. list = &transaction->t_log_list;
  1358. break;
  1359. case BJ_Reserved:
  1360. list = &transaction->t_reserved_list;
  1361. break;
  1362. }
  1363. __blist_del_buffer(list, jh);
  1364. jh->b_jlist = BJ_None;
  1365. if (test_clear_buffer_jbddirty(bh))
  1366. mark_buffer_dirty(bh); /* Expose it to the VM */
  1367. }
  1368. /*
  1369. * Remove buffer from all transactions.
  1370. *
  1371. * Called with bh_state lock and j_list_lock
  1372. *
  1373. * jh and bh may be already freed when this function returns.
  1374. */
  1375. static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1376. {
  1377. __jbd2_journal_temp_unlink_buffer(jh);
  1378. jh->b_transaction = NULL;
  1379. jbd2_journal_put_journal_head(jh);
  1380. }
  1381. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1382. {
  1383. struct buffer_head *bh = jh2bh(jh);
  1384. /* Get reference so that buffer cannot be freed before we unlock it */
  1385. get_bh(bh);
  1386. jbd_lock_bh_state(bh);
  1387. spin_lock(&journal->j_list_lock);
  1388. __jbd2_journal_unfile_buffer(jh);
  1389. spin_unlock(&journal->j_list_lock);
  1390. jbd_unlock_bh_state(bh);
  1391. __brelse(bh);
  1392. }
  1393. /*
  1394. * Called from jbd2_journal_try_to_free_buffers().
  1395. *
  1396. * Called under jbd_lock_bh_state(bh)
  1397. */
  1398. static void
  1399. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1400. {
  1401. struct journal_head *jh;
  1402. jh = bh2jh(bh);
  1403. if (buffer_locked(bh) || buffer_dirty(bh))
  1404. goto out;
  1405. if (jh->b_next_transaction != NULL)
  1406. goto out;
  1407. spin_lock(&journal->j_list_lock);
  1408. if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
  1409. /* written-back checkpointed metadata buffer */
  1410. if (jh->b_jlist == BJ_None) {
  1411. JBUFFER_TRACE(jh, "remove from checkpoint list");
  1412. __jbd2_journal_remove_checkpoint(jh);
  1413. }
  1414. }
  1415. spin_unlock(&journal->j_list_lock);
  1416. out:
  1417. return;
  1418. }
  1419. /**
  1420. * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1421. * @journal: journal for operation
  1422. * @page: to try and free
  1423. * @gfp_mask: we use the mask to detect how hard should we try to release
  1424. * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
  1425. * release the buffers.
  1426. *
  1427. *
  1428. * For all the buffers on this page,
  1429. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1430. * so try_to_free_buffers() can reap them.
  1431. *
  1432. * This function returns non-zero if we wish try_to_free_buffers()
  1433. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1434. * We also do it if the page has locked or dirty buffers and the caller wants
  1435. * us to perform sync or async writeout.
  1436. *
  1437. * This complicates JBD locking somewhat. We aren't protected by the
  1438. * BKL here. We wish to remove the buffer from its committing or
  1439. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1440. *
  1441. * This may *change* the value of transaction_t->t_datalist, so anyone
  1442. * who looks at t_datalist needs to lock against this function.
  1443. *
  1444. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1445. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1446. * will come out of the lock with the buffer dirty, which makes it
  1447. * ineligible for release here.
  1448. *
  1449. * Who else is affected by this? hmm... Really the only contender
  1450. * is do_get_write_access() - it could be looking at the buffer while
  1451. * journal_try_to_free_buffer() is changing its state. But that
  1452. * cannot happen because we never reallocate freed data as metadata
  1453. * while the data is part of a transaction. Yes?
  1454. *
  1455. * Return 0 on failure, 1 on success
  1456. */
  1457. int jbd2_journal_try_to_free_buffers(journal_t *journal,
  1458. struct page *page, gfp_t gfp_mask)
  1459. {
  1460. struct buffer_head *head;
  1461. struct buffer_head *bh;
  1462. int ret = 0;
  1463. J_ASSERT(PageLocked(page));
  1464. head = page_buffers(page);
  1465. bh = head;
  1466. do {
  1467. struct journal_head *jh;
  1468. /*
  1469. * We take our own ref against the journal_head here to avoid
  1470. * having to add tons of locking around each instance of
  1471. * jbd2_journal_put_journal_head().
  1472. */
  1473. jh = jbd2_journal_grab_journal_head(bh);
  1474. if (!jh)
  1475. continue;
  1476. jbd_lock_bh_state(bh);
  1477. __journal_try_to_free_buffer(journal, bh);
  1478. jbd2_journal_put_journal_head(jh);
  1479. jbd_unlock_bh_state(bh);
  1480. if (buffer_jbd(bh))
  1481. goto busy;
  1482. } while ((bh = bh->b_this_page) != head);
  1483. ret = try_to_free_buffers(page);
  1484. busy:
  1485. return ret;
  1486. }
  1487. /*
  1488. * This buffer is no longer needed. If it is on an older transaction's
  1489. * checkpoint list we need to record it on this transaction's forget list
  1490. * to pin this buffer (and hence its checkpointing transaction) down until
  1491. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1492. * release it.
  1493. * Returns non-zero if JBD no longer has an interest in the buffer.
  1494. *
  1495. * Called under j_list_lock.
  1496. *
  1497. * Called under jbd_lock_bh_state(bh).
  1498. */
  1499. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1500. {
  1501. int may_free = 1;
  1502. struct buffer_head *bh = jh2bh(jh);
  1503. if (jh->b_cp_transaction) {
  1504. JBUFFER_TRACE(jh, "on running+cp transaction");
  1505. __jbd2_journal_temp_unlink_buffer(jh);
  1506. /*
  1507. * We don't want to write the buffer anymore, clear the
  1508. * bit so that we don't confuse checks in
  1509. * __journal_file_buffer
  1510. */
  1511. clear_buffer_dirty(bh);
  1512. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1513. may_free = 0;
  1514. } else {
  1515. JBUFFER_TRACE(jh, "on running transaction");
  1516. __jbd2_journal_unfile_buffer(jh);
  1517. }
  1518. return may_free;
  1519. }
  1520. /*
  1521. * jbd2_journal_invalidatepage
  1522. *
  1523. * This code is tricky. It has a number of cases to deal with.
  1524. *
  1525. * There are two invariants which this code relies on:
  1526. *
  1527. * i_size must be updated on disk before we start calling invalidatepage on the
  1528. * data.
  1529. *
  1530. * This is done in ext3 by defining an ext3_setattr method which
  1531. * updates i_size before truncate gets going. By maintaining this
  1532. * invariant, we can be sure that it is safe to throw away any buffers
  1533. * attached to the current transaction: once the transaction commits,
  1534. * we know that the data will not be needed.
  1535. *
  1536. * Note however that we can *not* throw away data belonging to the
  1537. * previous, committing transaction!
  1538. *
  1539. * Any disk blocks which *are* part of the previous, committing
  1540. * transaction (and which therefore cannot be discarded immediately) are
  1541. * not going to be reused in the new running transaction
  1542. *
  1543. * The bitmap committed_data images guarantee this: any block which is
  1544. * allocated in one transaction and removed in the next will be marked
  1545. * as in-use in the committed_data bitmap, so cannot be reused until
  1546. * the next transaction to delete the block commits. This means that
  1547. * leaving committing buffers dirty is quite safe: the disk blocks
  1548. * cannot be reallocated to a different file and so buffer aliasing is
  1549. * not possible.
  1550. *
  1551. *
  1552. * The above applies mainly to ordered data mode. In writeback mode we
  1553. * don't make guarantees about the order in which data hits disk --- in
  1554. * particular we don't guarantee that new dirty data is flushed before
  1555. * transaction commit --- so it is always safe just to discard data
  1556. * immediately in that mode. --sct
  1557. */
  1558. /*
  1559. * The journal_unmap_buffer helper function returns zero if the buffer
  1560. * concerned remains pinned as an anonymous buffer belonging to an older
  1561. * transaction.
  1562. *
  1563. * We're outside-transaction here. Either or both of j_running_transaction
  1564. * and j_committing_transaction may be NULL.
  1565. */
  1566. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
  1567. {
  1568. transaction_t *transaction;
  1569. struct journal_head *jh;
  1570. int may_free = 1;
  1571. int ret;
  1572. BUFFER_TRACE(bh, "entry");
  1573. /*
  1574. * It is safe to proceed here without the j_list_lock because the
  1575. * buffers cannot be stolen by try_to_free_buffers as long as we are
  1576. * holding the page lock. --sct
  1577. */
  1578. if (!buffer_jbd(bh))
  1579. goto zap_buffer_unlocked;
  1580. /* OK, we have data buffer in journaled mode */
  1581. write_lock(&journal->j_state_lock);
  1582. jbd_lock_bh_state(bh);
  1583. spin_lock(&journal->j_list_lock);
  1584. jh = jbd2_journal_grab_journal_head(bh);
  1585. if (!jh)
  1586. goto zap_buffer_no_jh;
  1587. /*
  1588. * We cannot remove the buffer from checkpoint lists until the
  1589. * transaction adding inode to orphan list (let's call it T)
  1590. * is committed. Otherwise if the transaction changing the
  1591. * buffer would be cleaned from the journal before T is
  1592. * committed, a crash will cause that the correct contents of
  1593. * the buffer will be lost. On the other hand we have to
  1594. * clear the buffer dirty bit at latest at the moment when the
  1595. * transaction marking the buffer as freed in the filesystem
  1596. * structures is committed because from that moment on the
  1597. * buffer can be reallocated and used by a different page.
  1598. * Since the block hasn't been freed yet but the inode has
  1599. * already been added to orphan list, it is safe for us to add
  1600. * the buffer to BJ_Forget list of the newest transaction.
  1601. */
  1602. transaction = jh->b_transaction;
  1603. if (transaction == NULL) {
  1604. /* First case: not on any transaction. If it
  1605. * has no checkpoint link, then we can zap it:
  1606. * it's a writeback-mode buffer so we don't care
  1607. * if it hits disk safely. */
  1608. if (!jh->b_cp_transaction) {
  1609. JBUFFER_TRACE(jh, "not on any transaction: zap");
  1610. goto zap_buffer;
  1611. }
  1612. if (!buffer_dirty(bh)) {
  1613. /* bdflush has written it. We can drop it now */
  1614. goto zap_buffer;
  1615. }
  1616. /* OK, it must be in the journal but still not
  1617. * written fully to disk: it's metadata or
  1618. * journaled data... */
  1619. if (journal->j_running_transaction) {
  1620. /* ... and once the current transaction has
  1621. * committed, the buffer won't be needed any
  1622. * longer. */
  1623. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  1624. ret = __dispose_buffer(jh,
  1625. journal->j_running_transaction);
  1626. jbd2_journal_put_journal_head(jh);
  1627. spin_unlock(&journal->j_list_lock);
  1628. jbd_unlock_bh_state(bh);
  1629. write_unlock(&journal->j_state_lock);
  1630. return ret;
  1631. } else {
  1632. /* There is no currently-running transaction. So the
  1633. * orphan record which we wrote for this file must have
  1634. * passed into commit. We must attach this buffer to
  1635. * the committing transaction, if it exists. */
  1636. if (journal->j_committing_transaction) {
  1637. JBUFFER_TRACE(jh, "give to committing trans");
  1638. ret = __dispose_buffer(jh,
  1639. journal->j_committing_transaction);
  1640. jbd2_journal_put_journal_head(jh);
  1641. spin_unlock(&journal->j_list_lock);
  1642. jbd_unlock_bh_state(bh);
  1643. write_unlock(&journal->j_state_lock);
  1644. return ret;
  1645. } else {
  1646. /* The orphan record's transaction has
  1647. * committed. We can cleanse this buffer */
  1648. clear_buffer_jbddirty(bh);
  1649. goto zap_buffer;
  1650. }
  1651. }
  1652. } else if (transaction == journal->j_committing_transaction) {
  1653. JBUFFER_TRACE(jh, "on committing transaction");
  1654. /*
  1655. * The buffer is committing, we simply cannot touch
  1656. * it. So we just set j_next_transaction to the
  1657. * running transaction (if there is one) and mark
  1658. * buffer as freed so that commit code knows it should
  1659. * clear dirty bits when it is done with the buffer.
  1660. */
  1661. set_buffer_freed(bh);
  1662. if (journal->j_running_transaction && buffer_jbddirty(bh))
  1663. jh->b_next_transaction = journal->j_running_transaction;
  1664. jbd2_journal_put_journal_head(jh);
  1665. spin_unlock(&journal->j_list_lock);
  1666. jbd_unlock_bh_state(bh);
  1667. write_unlock(&journal->j_state_lock);
  1668. return 0;
  1669. } else {
  1670. /* Good, the buffer belongs to the running transaction.
  1671. * We are writing our own transaction's data, not any
  1672. * previous one's, so it is safe to throw it away
  1673. * (remember that we expect the filesystem to have set
  1674. * i_size already for this truncate so recovery will not
  1675. * expose the disk blocks we are discarding here.) */
  1676. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  1677. JBUFFER_TRACE(jh, "on running transaction");
  1678. may_free = __dispose_buffer(jh, transaction);
  1679. }
  1680. zap_buffer:
  1681. jbd2_journal_put_journal_head(jh);
  1682. zap_buffer_no_jh:
  1683. spin_unlock(&journal->j_list_lock);
  1684. jbd_unlock_bh_state(bh);
  1685. write_unlock(&journal->j_state_lock);
  1686. zap_buffer_unlocked:
  1687. clear_buffer_dirty(bh);
  1688. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  1689. clear_buffer_mapped(bh);
  1690. clear_buffer_req(bh);
  1691. clear_buffer_new(bh);
  1692. bh->b_bdev = NULL;
  1693. return may_free;
  1694. }
  1695. /**
  1696. * void jbd2_journal_invalidatepage()
  1697. * @journal: journal to use for flush...
  1698. * @page: page to flush
  1699. * @offset: length of page to invalidate.
  1700. *
  1701. * Reap page buffers containing data after offset in page.
  1702. *
  1703. */
  1704. void jbd2_journal_invalidatepage(journal_t *journal,
  1705. struct page *page,
  1706. unsigned long offset)
  1707. {
  1708. struct buffer_head *head, *bh, *next;
  1709. unsigned int curr_off = 0;
  1710. int may_free = 1;
  1711. if (!PageLocked(page))
  1712. BUG();
  1713. if (!page_has_buffers(page))
  1714. return;
  1715. /* We will potentially be playing with lists other than just the
  1716. * data lists (especially for journaled data mode), so be
  1717. * cautious in our locking. */
  1718. head = bh = page_buffers(page);
  1719. do {
  1720. unsigned int next_off = curr_off + bh->b_size;
  1721. next = bh->b_this_page;
  1722. if (offset <= curr_off) {
  1723. /* This block is wholly outside the truncation point */
  1724. lock_buffer(bh);
  1725. may_free &= journal_unmap_buffer(journal, bh);
  1726. unlock_buffer(bh);
  1727. }
  1728. curr_off = next_off;
  1729. bh = next;
  1730. } while (bh != head);
  1731. if (!offset) {
  1732. if (may_free && try_to_free_buffers(page))
  1733. J_ASSERT(!page_has_buffers(page));
  1734. }
  1735. }
  1736. /*
  1737. * File a buffer on the given transaction list.
  1738. */
  1739. void __jbd2_journal_file_buffer(struct journal_head *jh,
  1740. transaction_t *transaction, int jlist)
  1741. {
  1742. struct journal_head **list = NULL;
  1743. int was_dirty = 0;
  1744. struct buffer_head *bh = jh2bh(jh);
  1745. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1746. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1747. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1748. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1749. jh->b_transaction == NULL);
  1750. if (jh->b_transaction && jh->b_jlist == jlist)
  1751. return;
  1752. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  1753. jlist == BJ_Shadow || jlist == BJ_Forget) {
  1754. /*
  1755. * For metadata buffers, we track dirty bit in buffer_jbddirty
  1756. * instead of buffer_dirty. We should not see a dirty bit set
  1757. * here because we clear it in do_get_write_access but e.g.
  1758. * tune2fs can modify the sb and set the dirty bit at any time
  1759. * so we try to gracefully handle that.
  1760. */
  1761. if (buffer_dirty(bh))
  1762. warn_dirty_buffer(bh);
  1763. if (test_clear_buffer_dirty(bh) ||
  1764. test_clear_buffer_jbddirty(bh))
  1765. was_dirty = 1;
  1766. }
  1767. if (jh->b_transaction)
  1768. __jbd2_journal_temp_unlink_buffer(jh);
  1769. else
  1770. jbd2_journal_grab_journal_head(bh);
  1771. jh->b_transaction = transaction;
  1772. switch (jlist) {
  1773. case BJ_None:
  1774. J_ASSERT_JH(jh, !jh->b_committed_data);
  1775. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1776. return;
  1777. case BJ_Metadata:
  1778. transaction->t_nr_buffers++;
  1779. list = &transaction->t_buffers;
  1780. break;
  1781. case BJ_Forget:
  1782. list = &transaction->t_forget;
  1783. break;
  1784. case BJ_IO:
  1785. list = &transaction->t_iobuf_list;
  1786. break;
  1787. case BJ_Shadow:
  1788. list = &transaction->t_shadow_list;
  1789. break;
  1790. case BJ_LogCtl:
  1791. list = &transaction->t_log_list;
  1792. break;
  1793. case BJ_Reserved:
  1794. list = &transaction->t_reserved_list;
  1795. break;
  1796. }
  1797. __blist_add_buffer(list, jh);
  1798. jh->b_jlist = jlist;
  1799. if (was_dirty)
  1800. set_buffer_jbddirty(bh);
  1801. }
  1802. void jbd2_journal_file_buffer(struct journal_head *jh,
  1803. transaction_t *transaction, int jlist)
  1804. {
  1805. jbd_lock_bh_state(jh2bh(jh));
  1806. spin_lock(&transaction->t_journal->j_list_lock);
  1807. __jbd2_journal_file_buffer(jh, transaction, jlist);
  1808. spin_unlock(&transaction->t_journal->j_list_lock);
  1809. jbd_unlock_bh_state(jh2bh(jh));
  1810. }
  1811. /*
  1812. * Remove a buffer from its current buffer list in preparation for
  1813. * dropping it from its current transaction entirely. If the buffer has
  1814. * already started to be used by a subsequent transaction, refile the
  1815. * buffer on that transaction's metadata list.
  1816. *
  1817. * Called under j_list_lock
  1818. * Called under jbd_lock_bh_state(jh2bh(jh))
  1819. *
  1820. * jh and bh may be already free when this function returns
  1821. */
  1822. void __jbd2_journal_refile_buffer(struct journal_head *jh)
  1823. {
  1824. int was_dirty, jlist;
  1825. struct buffer_head *bh = jh2bh(jh);
  1826. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1827. if (jh->b_transaction)
  1828. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  1829. /* If the buffer is now unused, just drop it. */
  1830. if (jh->b_next_transaction == NULL) {
  1831. __jbd2_journal_unfile_buffer(jh);
  1832. return;
  1833. }
  1834. /*
  1835. * It has been modified by a later transaction: add it to the new
  1836. * transaction's metadata list.
  1837. */
  1838. was_dirty = test_clear_buffer_jbddirty(bh);
  1839. __jbd2_journal_temp_unlink_buffer(jh);
  1840. /*
  1841. * We set b_transaction here because b_next_transaction will inherit
  1842. * our jh reference and thus __jbd2_journal_file_buffer() must not
  1843. * take a new one.
  1844. */
  1845. jh->b_transaction = jh->b_next_transaction;
  1846. jh->b_next_transaction = NULL;
  1847. if (buffer_freed(bh))
  1848. jlist = BJ_Forget;
  1849. else if (jh->b_modified)
  1850. jlist = BJ_Metadata;
  1851. else
  1852. jlist = BJ_Reserved;
  1853. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  1854. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  1855. if (was_dirty)
  1856. set_buffer_jbddirty(bh);
  1857. }
  1858. /*
  1859. * __jbd2_journal_refile_buffer() with necessary locking added. We take our
  1860. * bh reference so that we can safely unlock bh.
  1861. *
  1862. * The jh and bh may be freed by this call.
  1863. */
  1864. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  1865. {
  1866. struct buffer_head *bh = jh2bh(jh);
  1867. /* Get reference so that buffer cannot be freed before we unlock it */
  1868. get_bh(bh);
  1869. jbd_lock_bh_state(bh);
  1870. spin_lock(&journal->j_list_lock);
  1871. __jbd2_journal_refile_buffer(jh);
  1872. jbd_unlock_bh_state(bh);
  1873. spin_unlock(&journal->j_list_lock);
  1874. __brelse(bh);
  1875. }
  1876. /*
  1877. * File inode in the inode list of the handle's transaction
  1878. */
  1879. int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
  1880. {
  1881. transaction_t *transaction = handle->h_transaction;
  1882. journal_t *journal = transaction->t_journal;
  1883. if (is_handle_aborted(handle))
  1884. return -EIO;
  1885. jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  1886. transaction->t_tid);
  1887. /*
  1888. * First check whether inode isn't already on the transaction's
  1889. * lists without taking the lock. Note that this check is safe
  1890. * without the lock as we cannot race with somebody removing inode
  1891. * from the transaction. The reason is that we remove inode from the
  1892. * transaction only in journal_release_jbd_inode() and when we commit
  1893. * the transaction. We are guarded from the first case by holding
  1894. * a reference to the inode. We are safe against the second case
  1895. * because if jinode->i_transaction == transaction, commit code
  1896. * cannot touch the transaction because we hold reference to it,
  1897. * and if jinode->i_next_transaction == transaction, commit code
  1898. * will only file the inode where we want it.
  1899. */
  1900. if (jinode->i_transaction == transaction ||
  1901. jinode->i_next_transaction == transaction)
  1902. return 0;
  1903. spin_lock(&journal->j_list_lock);
  1904. if (jinode->i_transaction == transaction ||
  1905. jinode->i_next_transaction == transaction)
  1906. goto done;
  1907. /*
  1908. * We only ever set this variable to 1 so the test is safe. Since
  1909. * t_need_data_flush is likely to be set, we do the test to save some
  1910. * cacheline bouncing
  1911. */
  1912. if (!transaction->t_need_data_flush)
  1913. transaction->t_need_data_flush = 1;
  1914. /* On some different transaction's list - should be
  1915. * the committing one */
  1916. if (jinode->i_transaction) {
  1917. J_ASSERT(jinode->i_next_transaction == NULL);
  1918. J_ASSERT(jinode->i_transaction ==
  1919. journal->j_committing_transaction);
  1920. jinode->i_next_transaction = transaction;
  1921. goto done;
  1922. }
  1923. /* Not on any transaction list... */
  1924. J_ASSERT(!jinode->i_next_transaction);
  1925. jinode->i_transaction = transaction;
  1926. list_add(&jinode->i_list, &transaction->t_inode_list);
  1927. done:
  1928. spin_unlock(&journal->j_list_lock);
  1929. return 0;
  1930. }
  1931. /*
  1932. * File truncate and transaction commit interact with each other in a
  1933. * non-trivial way. If a transaction writing data block A is
  1934. * committing, we cannot discard the data by truncate until we have
  1935. * written them. Otherwise if we crashed after the transaction with
  1936. * write has committed but before the transaction with truncate has
  1937. * committed, we could see stale data in block A. This function is a
  1938. * helper to solve this problem. It starts writeout of the truncated
  1939. * part in case it is in the committing transaction.
  1940. *
  1941. * Filesystem code must call this function when inode is journaled in
  1942. * ordered mode before truncation happens and after the inode has been
  1943. * placed on orphan list with the new inode size. The second condition
  1944. * avoids the race that someone writes new data and we start
  1945. * committing the transaction after this function has been called but
  1946. * before a transaction for truncate is started (and furthermore it
  1947. * allows us to optimize the case where the addition to orphan list
  1948. * happens in the same transaction as write --- we don't have to write
  1949. * any data in such case).
  1950. */
  1951. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  1952. struct jbd2_inode *jinode,
  1953. loff_t new_size)
  1954. {
  1955. transaction_t *inode_trans, *commit_trans;
  1956. int ret = 0;
  1957. /* This is a quick check to avoid locking if not necessary */
  1958. if (!jinode->i_transaction)
  1959. goto out;
  1960. /* Locks are here just to force reading of recent values, it is
  1961. * enough that the transaction was not committing before we started
  1962. * a transaction adding the inode to orphan list */
  1963. read_lock(&journal->j_state_lock);
  1964. commit_trans = journal->j_committing_transaction;
  1965. read_unlock(&journal->j_state_lock);
  1966. spin_lock(&journal->j_list_lock);
  1967. inode_trans = jinode->i_transaction;
  1968. spin_unlock(&journal->j_list_lock);
  1969. if (inode_trans == commit_trans) {
  1970. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  1971. new_size, LLONG_MAX);
  1972. if (ret)
  1973. jbd2_journal_abort(journal, ret);
  1974. }
  1975. out:
  1976. return ret;
  1977. }