/kern_oII/fs/jbd2/journal.c

http://omnia2droid.googlecode.com/ · C · 2455 lines · 1547 code · 323 blank · 585 comment · 239 complexity · 734fca3746447d21bae1ea6c05c60e86 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * linux/fs/jbd2/journal.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 journal-writing code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages journals: areas of disk reserved for logging
  16. * transactional updates. This includes the kernel journaling thread
  17. * which is responsible for scheduling updates to the log.
  18. *
  19. * We do not actually manage the physical storage of the journal in this
  20. * file: that is left to a per-journal policy function, which allows us
  21. * to store the journal within a filesystem-specified area for ext2
  22. * journaling (ext2 can use a reserved inode for storing the log).
  23. */
  24. #include <linux/module.h>
  25. #include <linux/time.h>
  26. #include <linux/fs.h>
  27. #include <linux/jbd2.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/init.h>
  31. #include <linux/mm.h>
  32. #include <linux/freezer.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/kthread.h>
  35. #include <linux/poison.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/math64.h>
  40. #include <linux/hash.h>
  41. #define CREATE_TRACE_POINTS
  42. #include <trace/events/jbd2.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/page.h>
  45. EXPORT_SYMBOL(jbd2_journal_start);
  46. EXPORT_SYMBOL(jbd2_journal_restart);
  47. EXPORT_SYMBOL(jbd2_journal_extend);
  48. EXPORT_SYMBOL(jbd2_journal_stop);
  49. EXPORT_SYMBOL(jbd2_journal_lock_updates);
  50. EXPORT_SYMBOL(jbd2_journal_unlock_updates);
  51. EXPORT_SYMBOL(jbd2_journal_get_write_access);
  52. EXPORT_SYMBOL(jbd2_journal_get_create_access);
  53. EXPORT_SYMBOL(jbd2_journal_get_undo_access);
  54. EXPORT_SYMBOL(jbd2_journal_set_triggers);
  55. EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
  56. EXPORT_SYMBOL(jbd2_journal_release_buffer);
  57. EXPORT_SYMBOL(jbd2_journal_forget);
  58. #if 0
  59. EXPORT_SYMBOL(journal_sync_buffer);
  60. #endif
  61. EXPORT_SYMBOL(jbd2_journal_flush);
  62. EXPORT_SYMBOL(jbd2_journal_revoke);
  63. EXPORT_SYMBOL(jbd2_journal_init_dev);
  64. EXPORT_SYMBOL(jbd2_journal_init_inode);
  65. EXPORT_SYMBOL(jbd2_journal_update_format);
  66. EXPORT_SYMBOL(jbd2_journal_check_used_features);
  67. EXPORT_SYMBOL(jbd2_journal_check_available_features);
  68. EXPORT_SYMBOL(jbd2_journal_set_features);
  69. EXPORT_SYMBOL(jbd2_journal_load);
  70. EXPORT_SYMBOL(jbd2_journal_destroy);
  71. EXPORT_SYMBOL(jbd2_journal_abort);
  72. EXPORT_SYMBOL(jbd2_journal_errno);
  73. EXPORT_SYMBOL(jbd2_journal_ack_err);
  74. EXPORT_SYMBOL(jbd2_journal_clear_err);
  75. EXPORT_SYMBOL(jbd2_log_wait_commit);
  76. EXPORT_SYMBOL(jbd2_journal_start_commit);
  77. EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
  78. EXPORT_SYMBOL(jbd2_journal_wipe);
  79. EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
  80. EXPORT_SYMBOL(jbd2_journal_invalidatepage);
  81. EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
  82. EXPORT_SYMBOL(jbd2_journal_force_commit);
  83. EXPORT_SYMBOL(jbd2_journal_file_inode);
  84. EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
  85. EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
  86. EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
  87. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  88. static void __journal_abort_soft (journal_t *journal, int errno);
  89. /*
  90. * Helper function used to manage commit timeouts
  91. */
  92. static void commit_timeout(unsigned long __data)
  93. {
  94. struct task_struct * p = (struct task_struct *) __data;
  95. wake_up_process(p);
  96. }
  97. /*
  98. * kjournald2: The main thread function used to manage a logging device
  99. * journal.
  100. *
  101. * This kernel thread is responsible for two things:
  102. *
  103. * 1) COMMIT: Every so often we need to commit the current state of the
  104. * filesystem to disk. The journal thread is responsible for writing
  105. * all of the metadata buffers to disk.
  106. *
  107. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  108. * of the data in that part of the log has been rewritten elsewhere on
  109. * the disk. Flushing these old buffers to reclaim space in the log is
  110. * known as checkpointing, and this thread is responsible for that job.
  111. */
  112. static int kjournald2(void *arg)
  113. {
  114. journal_t *journal = arg;
  115. transaction_t *transaction;
  116. /*
  117. * Set up an interval timer which can be used to trigger a commit wakeup
  118. * after the commit interval expires
  119. */
  120. setup_timer(&journal->j_commit_timer, commit_timeout,
  121. (unsigned long)current);
  122. /* Record that the journal thread is running */
  123. journal->j_task = current;
  124. wake_up(&journal->j_wait_done_commit);
  125. printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
  126. "commit interval %ld seconds\n", current->pid,
  127. journal->j_devname, journal->j_commit_interval / HZ);
  128. /*
  129. * And now, wait forever for commit wakeup events.
  130. */
  131. spin_lock(&journal->j_state_lock);
  132. loop:
  133. if (journal->j_flags & JBD2_UNMOUNT)
  134. goto end_loop;
  135. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  136. journal->j_commit_sequence, journal->j_commit_request);
  137. if (journal->j_commit_sequence != journal->j_commit_request) {
  138. jbd_debug(1, "OK, requests differ\n");
  139. spin_unlock(&journal->j_state_lock);
  140. del_timer_sync(&journal->j_commit_timer);
  141. jbd2_journal_commit_transaction(journal);
  142. spin_lock(&journal->j_state_lock);
  143. goto loop;
  144. }
  145. wake_up(&journal->j_wait_done_commit);
  146. if (freezing(current)) {
  147. /*
  148. * The simpler the better. Flushing journal isn't a
  149. * good idea, because that depends on threads that may
  150. * be already stopped.
  151. */
  152. jbd_debug(1, "Now suspending kjournald2\n");
  153. spin_unlock(&journal->j_state_lock);
  154. refrigerator();
  155. spin_lock(&journal->j_state_lock);
  156. } else {
  157. /*
  158. * We assume on resume that commits are already there,
  159. * so we don't sleep
  160. */
  161. DEFINE_WAIT(wait);
  162. int should_sleep = 1;
  163. prepare_to_wait(&journal->j_wait_commit, &wait,
  164. TASK_INTERRUPTIBLE);
  165. if (journal->j_commit_sequence != journal->j_commit_request)
  166. should_sleep = 0;
  167. transaction = journal->j_running_transaction;
  168. if (transaction && time_after_eq(jiffies,
  169. transaction->t_expires))
  170. should_sleep = 0;
  171. if (journal->j_flags & JBD2_UNMOUNT)
  172. should_sleep = 0;
  173. if (should_sleep) {
  174. spin_unlock(&journal->j_state_lock);
  175. schedule();
  176. spin_lock(&journal->j_state_lock);
  177. }
  178. finish_wait(&journal->j_wait_commit, &wait);
  179. }
  180. jbd_debug(1, "kjournald2 wakes\n");
  181. /*
  182. * Were we woken up by a commit wakeup event?
  183. */
  184. transaction = journal->j_running_transaction;
  185. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  186. journal->j_commit_request = transaction->t_tid;
  187. jbd_debug(1, "woke because of timeout\n");
  188. }
  189. goto loop;
  190. end_loop:
  191. spin_unlock(&journal->j_state_lock);
  192. del_timer_sync(&journal->j_commit_timer);
  193. journal->j_task = NULL;
  194. wake_up(&journal->j_wait_done_commit);
  195. jbd_debug(1, "Journal thread exiting.\n");
  196. return 0;
  197. }
  198. static int jbd2_journal_start_thread(journal_t *journal)
  199. {
  200. struct task_struct *t;
  201. t = kthread_run(kjournald2, journal, "kjournald2");
  202. if (IS_ERR(t))
  203. return PTR_ERR(t);
  204. wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
  205. return 0;
  206. }
  207. static void journal_kill_thread(journal_t *journal)
  208. {
  209. spin_lock(&journal->j_state_lock);
  210. journal->j_flags |= JBD2_UNMOUNT;
  211. while (journal->j_task) {
  212. wake_up(&journal->j_wait_commit);
  213. spin_unlock(&journal->j_state_lock);
  214. wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
  215. spin_lock(&journal->j_state_lock);
  216. }
  217. spin_unlock(&journal->j_state_lock);
  218. }
  219. /*
  220. * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
  221. *
  222. * Writes a metadata buffer to a given disk block. The actual IO is not
  223. * performed but a new buffer_head is constructed which labels the data
  224. * to be written with the correct destination disk block.
  225. *
  226. * Any magic-number escaping which needs to be done will cause a
  227. * copy-out here. If the buffer happens to start with the
  228. * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
  229. * magic number is only written to the log for descripter blocks. In
  230. * this case, we copy the data and replace the first word with 0, and we
  231. * return a result code which indicates that this buffer needs to be
  232. * marked as an escaped buffer in the corresponding log descriptor
  233. * block. The missing word can then be restored when the block is read
  234. * during recovery.
  235. *
  236. * If the source buffer has already been modified by a new transaction
  237. * since we took the last commit snapshot, we use the frozen copy of
  238. * that data for IO. If we end up using the existing buffer_head's data
  239. * for the write, then we *have* to lock the buffer to prevent anyone
  240. * else from using and possibly modifying it while the IO is in
  241. * progress.
  242. *
  243. * The function returns a pointer to the buffer_heads to be used for IO.
  244. *
  245. * We assume that the journal has already been locked in this function.
  246. *
  247. * Return value:
  248. * <0: Error
  249. * >=0: Finished OK
  250. *
  251. * On success:
  252. * Bit 0 set == escape performed on the data
  253. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  254. */
  255. int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
  256. struct journal_head *jh_in,
  257. struct journal_head **jh_out,
  258. unsigned long long blocknr)
  259. {
  260. int need_copy_out = 0;
  261. int done_copy_out = 0;
  262. int do_escape = 0;
  263. char *mapped_data;
  264. struct buffer_head *new_bh;
  265. struct journal_head *new_jh;
  266. struct page *new_page;
  267. unsigned int new_offset;
  268. struct buffer_head *bh_in = jh2bh(jh_in);
  269. struct jbd2_buffer_trigger_type *triggers;
  270. journal_t *journal = transaction->t_journal;
  271. /*
  272. * The buffer really shouldn't be locked: only the current committing
  273. * transaction is allowed to write it, so nobody else is allowed
  274. * to do any IO.
  275. *
  276. * akpm: except if we're journalling data, and write() output is
  277. * also part of a shared mapping, and another thread has
  278. * decided to launch a writepage() against this buffer.
  279. */
  280. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  281. new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
  282. /* keep subsequent assertions sane */
  283. new_bh->b_state = 0;
  284. init_buffer(new_bh, NULL, NULL);
  285. atomic_set(&new_bh->b_count, 1);
  286. new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
  287. /*
  288. * If a new transaction has already done a buffer copy-out, then
  289. * we use that version of the data for the commit.
  290. */
  291. jbd_lock_bh_state(bh_in);
  292. repeat:
  293. if (jh_in->b_frozen_data) {
  294. done_copy_out = 1;
  295. new_page = virt_to_page(jh_in->b_frozen_data);
  296. new_offset = offset_in_page(jh_in->b_frozen_data);
  297. triggers = jh_in->b_frozen_triggers;
  298. } else {
  299. new_page = jh2bh(jh_in)->b_page;
  300. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  301. triggers = jh_in->b_triggers;
  302. }
  303. mapped_data = kmap_atomic(new_page, KM_USER0);
  304. /*
  305. * Fire any commit trigger. Do this before checking for escaping,
  306. * as the trigger may modify the magic offset. If a copy-out
  307. * happens afterwards, it will have the correct data in the buffer.
  308. */
  309. jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
  310. triggers);
  311. /*
  312. * Check for escaping
  313. */
  314. if (*((__be32 *)(mapped_data + new_offset)) ==
  315. cpu_to_be32(JBD2_MAGIC_NUMBER)) {
  316. need_copy_out = 1;
  317. do_escape = 1;
  318. }
  319. kunmap_atomic(mapped_data, KM_USER0);
  320. /*
  321. * Do we need to do a data copy?
  322. */
  323. if (need_copy_out && !done_copy_out) {
  324. char *tmp;
  325. jbd_unlock_bh_state(bh_in);
  326. tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
  327. jbd_lock_bh_state(bh_in);
  328. if (jh_in->b_frozen_data) {
  329. jbd2_free(tmp, bh_in->b_size);
  330. goto repeat;
  331. }
  332. jh_in->b_frozen_data = tmp;
  333. mapped_data = kmap_atomic(new_page, KM_USER0);
  334. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  335. kunmap_atomic(mapped_data, KM_USER0);
  336. new_page = virt_to_page(tmp);
  337. new_offset = offset_in_page(tmp);
  338. done_copy_out = 1;
  339. /*
  340. * This isn't strictly necessary, as we're using frozen
  341. * data for the escaping, but it keeps consistency with
  342. * b_frozen_data usage.
  343. */
  344. jh_in->b_frozen_triggers = jh_in->b_triggers;
  345. }
  346. /*
  347. * Did we need to do an escaping? Now we've done all the
  348. * copying, we can finally do so.
  349. */
  350. if (do_escape) {
  351. mapped_data = kmap_atomic(new_page, KM_USER0);
  352. *((unsigned int *)(mapped_data + new_offset)) = 0;
  353. kunmap_atomic(mapped_data, KM_USER0);
  354. }
  355. set_bh_page(new_bh, new_page, new_offset);
  356. new_jh->b_transaction = NULL;
  357. new_bh->b_size = jh2bh(jh_in)->b_size;
  358. new_bh->b_bdev = transaction->t_journal->j_dev;
  359. new_bh->b_blocknr = blocknr;
  360. set_buffer_mapped(new_bh);
  361. set_buffer_dirty(new_bh);
  362. *jh_out = new_jh;
  363. /*
  364. * The to-be-written buffer needs to get moved to the io queue,
  365. * and the original buffer whose contents we are shadowing or
  366. * copying is moved to the transaction's shadow queue.
  367. */
  368. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  369. spin_lock(&journal->j_list_lock);
  370. __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
  371. spin_unlock(&journal->j_list_lock);
  372. jbd_unlock_bh_state(bh_in);
  373. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  374. jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
  375. return do_escape | (done_copy_out << 1);
  376. }
  377. /*
  378. * Allocation code for the journal file. Manage the space left in the
  379. * journal, so that we can begin checkpointing when appropriate.
  380. */
  381. /*
  382. * __jbd2_log_space_left: Return the number of free blocks left in the journal.
  383. *
  384. * Called with the journal already locked.
  385. *
  386. * Called under j_state_lock
  387. */
  388. int __jbd2_log_space_left(journal_t *journal)
  389. {
  390. int left = journal->j_free;
  391. assert_spin_locked(&journal->j_state_lock);
  392. /*
  393. * Be pessimistic here about the number of those free blocks which
  394. * might be required for log descriptor control blocks.
  395. */
  396. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  397. left -= MIN_LOG_RESERVED_BLOCKS;
  398. if (left <= 0)
  399. return 0;
  400. left -= (left >> 3);
  401. return left;
  402. }
  403. /*
  404. * Called under j_state_lock. Returns true if a transaction commit was started.
  405. */
  406. int __jbd2_log_start_commit(journal_t *journal, tid_t target)
  407. {
  408. /*
  409. * Are we already doing a recent enough commit?
  410. */
  411. if (!tid_geq(journal->j_commit_request, target)) {
  412. /*
  413. * We want a new commit: OK, mark the request and wakup the
  414. * commit thread. We do _not_ do the commit ourselves.
  415. */
  416. journal->j_commit_request = target;
  417. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  418. journal->j_commit_request,
  419. journal->j_commit_sequence);
  420. wake_up(&journal->j_wait_commit);
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. int jbd2_log_start_commit(journal_t *journal, tid_t tid)
  426. {
  427. int ret;
  428. spin_lock(&journal->j_state_lock);
  429. ret = __jbd2_log_start_commit(journal, tid);
  430. spin_unlock(&journal->j_state_lock);
  431. return ret;
  432. }
  433. /*
  434. * Force and wait upon a commit if the calling process is not within
  435. * transaction. This is used for forcing out undo-protected data which contains
  436. * bitmaps, when the fs is running out of space.
  437. *
  438. * We can only force the running transaction if we don't have an active handle;
  439. * otherwise, we will deadlock.
  440. *
  441. * Returns true if a transaction was started.
  442. */
  443. int jbd2_journal_force_commit_nested(journal_t *journal)
  444. {
  445. transaction_t *transaction = NULL;
  446. tid_t tid;
  447. spin_lock(&journal->j_state_lock);
  448. if (journal->j_running_transaction && !current->journal_info) {
  449. transaction = journal->j_running_transaction;
  450. __jbd2_log_start_commit(journal, transaction->t_tid);
  451. } else if (journal->j_committing_transaction)
  452. transaction = journal->j_committing_transaction;
  453. if (!transaction) {
  454. spin_unlock(&journal->j_state_lock);
  455. return 0; /* Nothing to retry */
  456. }
  457. tid = transaction->t_tid;
  458. spin_unlock(&journal->j_state_lock);
  459. jbd2_log_wait_commit(journal, tid);
  460. return 1;
  461. }
  462. /*
  463. * Start a commit of the current running transaction (if any). Returns true
  464. * if a transaction is going to be committed (or is currently already
  465. * committing), and fills its tid in at *ptid
  466. */
  467. int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
  468. {
  469. int ret = 0;
  470. spin_lock(&journal->j_state_lock);
  471. if (journal->j_running_transaction) {
  472. tid_t tid = journal->j_running_transaction->t_tid;
  473. __jbd2_log_start_commit(journal, tid);
  474. /* There's a running transaction and we've just made sure
  475. * it's commit has been scheduled. */
  476. if (ptid)
  477. *ptid = tid;
  478. ret = 1;
  479. } else if (journal->j_committing_transaction) {
  480. /*
  481. * If ext3_write_super() recently started a commit, then we
  482. * have to wait for completion of that transaction
  483. */
  484. if (ptid)
  485. *ptid = journal->j_committing_transaction->t_tid;
  486. ret = 1;
  487. }
  488. spin_unlock(&journal->j_state_lock);
  489. return ret;
  490. }
  491. /*
  492. * Wait for a specified commit to complete.
  493. * The caller may not hold the journal lock.
  494. */
  495. int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
  496. {
  497. int err = 0;
  498. #ifdef CONFIG_JBD2_DEBUG
  499. spin_lock(&journal->j_state_lock);
  500. if (!tid_geq(journal->j_commit_request, tid)) {
  501. printk(KERN_EMERG
  502. "%s: error: j_commit_request=%d, tid=%d\n",
  503. __func__, journal->j_commit_request, tid);
  504. }
  505. spin_unlock(&journal->j_state_lock);
  506. #endif
  507. spin_lock(&journal->j_state_lock);
  508. while (tid_gt(tid, journal->j_commit_sequence)) {
  509. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  510. tid, journal->j_commit_sequence);
  511. wake_up(&journal->j_wait_commit);
  512. spin_unlock(&journal->j_state_lock);
  513. wait_event(journal->j_wait_done_commit,
  514. !tid_gt(tid, journal->j_commit_sequence));
  515. spin_lock(&journal->j_state_lock);
  516. }
  517. spin_unlock(&journal->j_state_lock);
  518. if (unlikely(is_journal_aborted(journal))) {
  519. printk(KERN_EMERG "journal commit I/O error\n");
  520. err = -EIO;
  521. }
  522. return err;
  523. }
  524. /*
  525. * Log buffer allocation routines:
  526. */
  527. int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
  528. {
  529. unsigned long blocknr;
  530. spin_lock(&journal->j_state_lock);
  531. J_ASSERT(journal->j_free > 1);
  532. blocknr = journal->j_head;
  533. journal->j_head++;
  534. journal->j_free--;
  535. if (journal->j_head == journal->j_last)
  536. journal->j_head = journal->j_first;
  537. spin_unlock(&journal->j_state_lock);
  538. return jbd2_journal_bmap(journal, blocknr, retp);
  539. }
  540. /*
  541. * Conversion of logical to physical block numbers for the journal
  542. *
  543. * On external journals the journal blocks are identity-mapped, so
  544. * this is a no-op. If needed, we can use j_blk_offset - everything is
  545. * ready.
  546. */
  547. int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
  548. unsigned long long *retp)
  549. {
  550. int err = 0;
  551. unsigned long long ret;
  552. if (journal->j_inode) {
  553. ret = bmap(journal->j_inode, blocknr);
  554. if (ret)
  555. *retp = ret;
  556. else {
  557. printk(KERN_ALERT "%s: journal block not found "
  558. "at offset %lu on %s\n",
  559. __func__, blocknr, journal->j_devname);
  560. err = -EIO;
  561. __journal_abort_soft(journal, err);
  562. }
  563. } else {
  564. *retp = blocknr; /* +journal->j_blk_offset */
  565. }
  566. return err;
  567. }
  568. /*
  569. * We play buffer_head aliasing tricks to write data/metadata blocks to
  570. * the journal without copying their contents, but for journal
  571. * descriptor blocks we do need to generate bona fide buffers.
  572. *
  573. * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
  574. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  575. * But we don't bother doing that, so there will be coherency problems with
  576. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  577. */
  578. struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
  579. {
  580. struct buffer_head *bh;
  581. unsigned long long blocknr;
  582. int err;
  583. err = jbd2_journal_next_log_block(journal, &blocknr);
  584. if (err)
  585. return NULL;
  586. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  587. if (!bh)
  588. return NULL;
  589. lock_buffer(bh);
  590. memset(bh->b_data, 0, journal->j_blocksize);
  591. set_buffer_uptodate(bh);
  592. unlock_buffer(bh);
  593. BUFFER_TRACE(bh, "return this buffer");
  594. return jbd2_journal_add_journal_head(bh);
  595. }
  596. struct jbd2_stats_proc_session {
  597. journal_t *journal;
  598. struct transaction_stats_s *stats;
  599. int start;
  600. int max;
  601. };
  602. static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
  603. struct transaction_stats_s *ts,
  604. int first)
  605. {
  606. if (ts == s->stats + s->max)
  607. ts = s->stats;
  608. if (!first && ts == s->stats + s->start)
  609. return NULL;
  610. while (ts->ts_type == 0) {
  611. ts++;
  612. if (ts == s->stats + s->max)
  613. ts = s->stats;
  614. if (ts == s->stats + s->start)
  615. return NULL;
  616. }
  617. return ts;
  618. }
  619. static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
  620. {
  621. struct jbd2_stats_proc_session *s = seq->private;
  622. struct transaction_stats_s *ts;
  623. int l = *pos;
  624. if (l == 0)
  625. return SEQ_START_TOKEN;
  626. ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
  627. if (!ts)
  628. return NULL;
  629. l--;
  630. while (l) {
  631. ts = jbd2_history_skip_empty(s, ++ts, 0);
  632. if (!ts)
  633. break;
  634. l--;
  635. }
  636. return ts;
  637. }
  638. static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
  639. {
  640. struct jbd2_stats_proc_session *s = seq->private;
  641. struct transaction_stats_s *ts = v;
  642. ++*pos;
  643. if (v == SEQ_START_TOKEN)
  644. return jbd2_history_skip_empty(s, s->stats + s->start, 1);
  645. else
  646. return jbd2_history_skip_empty(s, ++ts, 0);
  647. }
  648. static int jbd2_seq_history_show(struct seq_file *seq, void *v)
  649. {
  650. struct transaction_stats_s *ts = v;
  651. if (v == SEQ_START_TOKEN) {
  652. seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
  653. "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
  654. "wait", "run", "lock", "flush", "log", "hndls",
  655. "block", "inlog", "ctime", "write", "drop",
  656. "close");
  657. return 0;
  658. }
  659. if (ts->ts_type == JBD2_STATS_RUN)
  660. seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
  661. "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
  662. jiffies_to_msecs(ts->u.run.rs_wait),
  663. jiffies_to_msecs(ts->u.run.rs_running),
  664. jiffies_to_msecs(ts->u.run.rs_locked),
  665. jiffies_to_msecs(ts->u.run.rs_flushing),
  666. jiffies_to_msecs(ts->u.run.rs_logging),
  667. ts->u.run.rs_handle_count,
  668. ts->u.run.rs_blocks,
  669. ts->u.run.rs_blocks_logged);
  670. else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
  671. seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
  672. "C", ts->ts_tid, " ",
  673. jiffies_to_msecs(ts->u.chp.cs_chp_time),
  674. ts->u.chp.cs_written, ts->u.chp.cs_dropped,
  675. ts->u.chp.cs_forced_to_close);
  676. else
  677. J_ASSERT(0);
  678. return 0;
  679. }
  680. static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
  681. {
  682. }
  683. static struct seq_operations jbd2_seq_history_ops = {
  684. .start = jbd2_seq_history_start,
  685. .next = jbd2_seq_history_next,
  686. .stop = jbd2_seq_history_stop,
  687. .show = jbd2_seq_history_show,
  688. };
  689. static int jbd2_seq_history_open(struct inode *inode, struct file *file)
  690. {
  691. journal_t *journal = PDE(inode)->data;
  692. struct jbd2_stats_proc_session *s;
  693. int rc, size;
  694. s = kmalloc(sizeof(*s), GFP_KERNEL);
  695. if (s == NULL)
  696. return -ENOMEM;
  697. size = sizeof(struct transaction_stats_s) * journal->j_history_max;
  698. s->stats = kmalloc(size, GFP_KERNEL);
  699. if (s->stats == NULL) {
  700. kfree(s);
  701. return -ENOMEM;
  702. }
  703. spin_lock(&journal->j_history_lock);
  704. memcpy(s->stats, journal->j_history, size);
  705. s->max = journal->j_history_max;
  706. s->start = journal->j_history_cur % s->max;
  707. spin_unlock(&journal->j_history_lock);
  708. rc = seq_open(file, &jbd2_seq_history_ops);
  709. if (rc == 0) {
  710. struct seq_file *m = file->private_data;
  711. m->private = s;
  712. } else {
  713. kfree(s->stats);
  714. kfree(s);
  715. }
  716. return rc;
  717. }
  718. static int jbd2_seq_history_release(struct inode *inode, struct file *file)
  719. {
  720. struct seq_file *seq = file->private_data;
  721. struct jbd2_stats_proc_session *s = seq->private;
  722. kfree(s->stats);
  723. kfree(s);
  724. return seq_release(inode, file);
  725. }
  726. static struct file_operations jbd2_seq_history_fops = {
  727. .owner = THIS_MODULE,
  728. .open = jbd2_seq_history_open,
  729. .read = seq_read,
  730. .llseek = seq_lseek,
  731. .release = jbd2_seq_history_release,
  732. };
  733. static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
  734. {
  735. return *pos ? NULL : SEQ_START_TOKEN;
  736. }
  737. static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
  738. {
  739. return NULL;
  740. }
  741. static int jbd2_seq_info_show(struct seq_file *seq, void *v)
  742. {
  743. struct jbd2_stats_proc_session *s = seq->private;
  744. if (v != SEQ_START_TOKEN)
  745. return 0;
  746. seq_printf(seq, "%lu transaction, each upto %u blocks\n",
  747. s->stats->ts_tid,
  748. s->journal->j_max_transaction_buffers);
  749. if (s->stats->ts_tid == 0)
  750. return 0;
  751. seq_printf(seq, "average: \n %ums waiting for transaction\n",
  752. jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
  753. seq_printf(seq, " %ums running transaction\n",
  754. jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
  755. seq_printf(seq, " %ums transaction was being locked\n",
  756. jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
  757. seq_printf(seq, " %ums flushing data (in ordered mode)\n",
  758. jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
  759. seq_printf(seq, " %ums logging transaction\n",
  760. jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
  761. seq_printf(seq, " %lluus average transaction commit time\n",
  762. div_u64(s->journal->j_average_commit_time, 1000));
  763. seq_printf(seq, " %lu handles per transaction\n",
  764. s->stats->u.run.rs_handle_count / s->stats->ts_tid);
  765. seq_printf(seq, " %lu blocks per transaction\n",
  766. s->stats->u.run.rs_blocks / s->stats->ts_tid);
  767. seq_printf(seq, " %lu logged blocks per transaction\n",
  768. s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
  769. return 0;
  770. }
  771. static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
  772. {
  773. }
  774. static struct seq_operations jbd2_seq_info_ops = {
  775. .start = jbd2_seq_info_start,
  776. .next = jbd2_seq_info_next,
  777. .stop = jbd2_seq_info_stop,
  778. .show = jbd2_seq_info_show,
  779. };
  780. static int jbd2_seq_info_open(struct inode *inode, struct file *file)
  781. {
  782. journal_t *journal = PDE(inode)->data;
  783. struct jbd2_stats_proc_session *s;
  784. int rc, size;
  785. s = kmalloc(sizeof(*s), GFP_KERNEL);
  786. if (s == NULL)
  787. return -ENOMEM;
  788. size = sizeof(struct transaction_stats_s);
  789. s->stats = kmalloc(size, GFP_KERNEL);
  790. if (s->stats == NULL) {
  791. kfree(s);
  792. return -ENOMEM;
  793. }
  794. spin_lock(&journal->j_history_lock);
  795. memcpy(s->stats, &journal->j_stats, size);
  796. s->journal = journal;
  797. spin_unlock(&journal->j_history_lock);
  798. rc = seq_open(file, &jbd2_seq_info_ops);
  799. if (rc == 0) {
  800. struct seq_file *m = file->private_data;
  801. m->private = s;
  802. } else {
  803. kfree(s->stats);
  804. kfree(s);
  805. }
  806. return rc;
  807. }
  808. static int jbd2_seq_info_release(struct inode *inode, struct file *file)
  809. {
  810. struct seq_file *seq = file->private_data;
  811. struct jbd2_stats_proc_session *s = seq->private;
  812. kfree(s->stats);
  813. kfree(s);
  814. return seq_release(inode, file);
  815. }
  816. static struct file_operations jbd2_seq_info_fops = {
  817. .owner = THIS_MODULE,
  818. .open = jbd2_seq_info_open,
  819. .read = seq_read,
  820. .llseek = seq_lseek,
  821. .release = jbd2_seq_info_release,
  822. };
  823. static struct proc_dir_entry *proc_jbd2_stats;
  824. static void jbd2_stats_proc_init(journal_t *journal)
  825. {
  826. journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
  827. if (journal->j_proc_entry) {
  828. proc_create_data("history", S_IRUGO, journal->j_proc_entry,
  829. &jbd2_seq_history_fops, journal);
  830. proc_create_data("info", S_IRUGO, journal->j_proc_entry,
  831. &jbd2_seq_info_fops, journal);
  832. }
  833. }
  834. static void jbd2_stats_proc_exit(journal_t *journal)
  835. {
  836. remove_proc_entry("info", journal->j_proc_entry);
  837. remove_proc_entry("history", journal->j_proc_entry);
  838. remove_proc_entry(journal->j_devname, proc_jbd2_stats);
  839. }
  840. static void journal_init_stats(journal_t *journal)
  841. {
  842. int size;
  843. if (!proc_jbd2_stats)
  844. return;
  845. journal->j_history_max = 100;
  846. size = sizeof(struct transaction_stats_s) * journal->j_history_max;
  847. journal->j_history = kzalloc(size, GFP_KERNEL);
  848. if (!journal->j_history) {
  849. journal->j_history_max = 0;
  850. return;
  851. }
  852. spin_lock_init(&journal->j_history_lock);
  853. }
  854. /*
  855. * Management for journal control blocks: functions to create and
  856. * destroy journal_t structures, and to initialise and read existing
  857. * journal blocks from disk. */
  858. /* First: create and setup a journal_t object in memory. We initialise
  859. * very few fields yet: that has to wait until we have created the
  860. * journal structures from from scratch, or loaded them from disk. */
  861. static journal_t * journal_init_common (void)
  862. {
  863. journal_t *journal;
  864. int err;
  865. journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
  866. if (!journal)
  867. goto fail;
  868. init_waitqueue_head(&journal->j_wait_transaction_locked);
  869. init_waitqueue_head(&journal->j_wait_logspace);
  870. init_waitqueue_head(&journal->j_wait_done_commit);
  871. init_waitqueue_head(&journal->j_wait_checkpoint);
  872. init_waitqueue_head(&journal->j_wait_commit);
  873. init_waitqueue_head(&journal->j_wait_updates);
  874. mutex_init(&journal->j_barrier);
  875. mutex_init(&journal->j_checkpoint_mutex);
  876. spin_lock_init(&journal->j_revoke_lock);
  877. spin_lock_init(&journal->j_list_lock);
  878. spin_lock_init(&journal->j_state_lock);
  879. journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
  880. journal->j_min_batch_time = 0;
  881. journal->j_max_batch_time = 15000; /* 15ms */
  882. /* The journal is marked for error until we succeed with recovery! */
  883. journal->j_flags = JBD2_ABORT;
  884. /* Set up a default-sized revoke table for the new mount. */
  885. err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  886. if (err) {
  887. kfree(journal);
  888. goto fail;
  889. }
  890. journal_init_stats(journal);
  891. return journal;
  892. fail:
  893. return NULL;
  894. }
  895. /* jbd2_journal_init_dev and jbd2_journal_init_inode:
  896. *
  897. * Create a journal structure assigned some fixed set of disk blocks to
  898. * the journal. We don't actually touch those disk blocks yet, but we
  899. * need to set up all of the mapping information to tell the journaling
  900. * system where the journal blocks are.
  901. *
  902. */
  903. /**
  904. * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
  905. * @bdev: Block device on which to create the journal
  906. * @fs_dev: Device which hold journalled filesystem for this journal.
  907. * @start: Block nr Start of journal.
  908. * @len: Length of the journal in blocks.
  909. * @blocksize: blocksize of journalling device
  910. *
  911. * Returns: a newly created journal_t *
  912. *
  913. * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
  914. * range of blocks on an arbitrary block device.
  915. *
  916. */
  917. journal_t * jbd2_journal_init_dev(struct block_device *bdev,
  918. struct block_device *fs_dev,
  919. unsigned long long start, int len, int blocksize)
  920. {
  921. journal_t *journal = journal_init_common();
  922. struct buffer_head *bh;
  923. char *p;
  924. int n;
  925. if (!journal)
  926. return NULL;
  927. /* journal descriptor can store up to n blocks -bzzz */
  928. journal->j_blocksize = blocksize;
  929. jbd2_stats_proc_init(journal);
  930. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  931. journal->j_wbufsize = n;
  932. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  933. if (!journal->j_wbuf) {
  934. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  935. __func__);
  936. goto out_err;
  937. }
  938. journal->j_dev = bdev;
  939. journal->j_fs_dev = fs_dev;
  940. journal->j_blk_offset = start;
  941. journal->j_maxlen = len;
  942. bdevname(journal->j_dev, journal->j_devname);
  943. p = journal->j_devname;
  944. while ((p = strchr(p, '/')))
  945. *p = '!';
  946. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  947. if (!bh) {
  948. printk(KERN_ERR
  949. "%s: Cannot get buffer for journal superblock\n",
  950. __func__);
  951. goto out_err;
  952. }
  953. journal->j_sb_buffer = bh;
  954. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  955. return journal;
  956. out_err:
  957. jbd2_stats_proc_exit(journal);
  958. kfree(journal);
  959. return NULL;
  960. }
  961. /**
  962. * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
  963. * @inode: An inode to create the journal in
  964. *
  965. * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
  966. * the journal. The inode must exist already, must support bmap() and
  967. * must have all data blocks preallocated.
  968. */
  969. journal_t * jbd2_journal_init_inode (struct inode *inode)
  970. {
  971. struct buffer_head *bh;
  972. journal_t *journal = journal_init_common();
  973. char *p;
  974. int err;
  975. int n;
  976. unsigned long long blocknr;
  977. if (!journal)
  978. return NULL;
  979. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  980. journal->j_inode = inode;
  981. bdevname(journal->j_dev, journal->j_devname);
  982. p = journal->j_devname;
  983. while ((p = strchr(p, '/')))
  984. *p = '!';
  985. p = journal->j_devname + strlen(journal->j_devname);
  986. sprintf(p, ":%lu", journal->j_inode->i_ino);
  987. jbd_debug(1,
  988. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  989. journal, inode->i_sb->s_id, inode->i_ino,
  990. (long long) inode->i_size,
  991. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  992. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  993. journal->j_blocksize = inode->i_sb->s_blocksize;
  994. jbd2_stats_proc_init(journal);
  995. /* journal descriptor can store up to n blocks -bzzz */
  996. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  997. journal->j_wbufsize = n;
  998. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  999. if (!journal->j_wbuf) {
  1000. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  1001. __func__);
  1002. goto out_err;
  1003. }
  1004. err = jbd2_journal_bmap(journal, 0, &blocknr);
  1005. /* If that failed, give up */
  1006. if (err) {
  1007. printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
  1008. __func__);
  1009. goto out_err;
  1010. }
  1011. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  1012. if (!bh) {
  1013. printk(KERN_ERR
  1014. "%s: Cannot get buffer for journal superblock\n",
  1015. __func__);
  1016. goto out_err;
  1017. }
  1018. journal->j_sb_buffer = bh;
  1019. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  1020. return journal;
  1021. out_err:
  1022. jbd2_stats_proc_exit(journal);
  1023. kfree(journal);
  1024. return NULL;
  1025. }
  1026. /*
  1027. * If the journal init or create aborts, we need to mark the journal
  1028. * superblock as being NULL to prevent the journal destroy from writing
  1029. * back a bogus superblock.
  1030. */
  1031. static void journal_fail_superblock (journal_t *journal)
  1032. {
  1033. struct buffer_head *bh = journal->j_sb_buffer;
  1034. brelse(bh);
  1035. journal->j_sb_buffer = NULL;
  1036. }
  1037. /*
  1038. * Given a journal_t structure, initialise the various fields for
  1039. * startup of a new journaling session. We use this both when creating
  1040. * a journal, and after recovering an old journal to reset it for
  1041. * subsequent use.
  1042. */
  1043. static int journal_reset(journal_t *journal)
  1044. {
  1045. journal_superblock_t *sb = journal->j_superblock;
  1046. unsigned long long first, last;
  1047. first = be32_to_cpu(sb->s_first);
  1048. last = be32_to_cpu(sb->s_maxlen);
  1049. journal->j_first = first;
  1050. journal->j_last = last;
  1051. journal->j_head = first;
  1052. journal->j_tail = first;
  1053. journal->j_free = last - first;
  1054. journal->j_tail_sequence = journal->j_transaction_sequence;
  1055. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  1056. journal->j_commit_request = journal->j_commit_sequence;
  1057. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  1058. /* Add the dynamic fields and write it to disk. */
  1059. jbd2_journal_update_superblock(journal, 1);
  1060. return jbd2_journal_start_thread(journal);
  1061. }
  1062. /**
  1063. * void jbd2_journal_update_superblock() - Update journal sb on disk.
  1064. * @journal: The journal to update.
  1065. * @wait: Set to '0' if you don't want to wait for IO completion.
  1066. *
  1067. * Update a journal's dynamic superblock fields and write it to disk,
  1068. * optionally waiting for the IO to complete.
  1069. */
  1070. void jbd2_journal_update_superblock(journal_t *journal, int wait)
  1071. {
  1072. journal_superblock_t *sb = journal->j_superblock;
  1073. struct buffer_head *bh = journal->j_sb_buffer;
  1074. /*
  1075. * As a special case, if the on-disk copy is already marked as needing
  1076. * no recovery (s_start == 0) and there are no outstanding transactions
  1077. * in the filesystem, then we can safely defer the superblock update
  1078. * until the next commit by setting JBD2_FLUSHED. This avoids
  1079. * attempting a write to a potential-readonly device.
  1080. */
  1081. if (sb->s_start == 0 && journal->j_tail_sequence ==
  1082. journal->j_transaction_sequence) {
  1083. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  1084. "(start %ld, seq %d, errno %d)\n",
  1085. journal->j_tail, journal->j_tail_sequence,
  1086. journal->j_errno);
  1087. goto out;
  1088. }
  1089. if (buffer_write_io_error(bh)) {
  1090. /*
  1091. * Oh, dear. A previous attempt to write the journal
  1092. * superblock failed. This could happen because the
  1093. * USB device was yanked out. Or it could happen to
  1094. * be a transient write error and maybe the block will
  1095. * be remapped. Nothing we can do but to retry the
  1096. * write and hope for the best.
  1097. */
  1098. printk(KERN_ERR "JBD2: previous I/O error detected "
  1099. "for journal superblock update for %s.\n",
  1100. journal->j_devname);
  1101. clear_buffer_write_io_error(bh);
  1102. set_buffer_uptodate(bh);
  1103. }
  1104. spin_lock(&journal->j_state_lock);
  1105. jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
  1106. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  1107. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  1108. sb->s_start = cpu_to_be32(journal->j_tail);
  1109. sb->s_errno = cpu_to_be32(journal->j_errno);
  1110. spin_unlock(&journal->j_state_lock);
  1111. BUFFER_TRACE(bh, "marking dirty");
  1112. mark_buffer_dirty(bh);
  1113. if (wait) {
  1114. sync_dirty_buffer(bh);
  1115. if (buffer_write_io_error(bh)) {
  1116. printk(KERN_ERR "JBD2: I/O error detected "
  1117. "when updating journal superblock for %s.\n",
  1118. journal->j_devname);
  1119. clear_buffer_write_io_error(bh);
  1120. set_buffer_uptodate(bh);
  1121. }
  1122. } else
  1123. ll_rw_block(SWRITE, 1, &bh);
  1124. out:
  1125. /* If we have just flushed the log (by marking s_start==0), then
  1126. * any future commit will have to be careful to update the
  1127. * superblock again to re-record the true start of the log. */
  1128. spin_lock(&journal->j_state_lock);
  1129. if (sb->s_start)
  1130. journal->j_flags &= ~JBD2_FLUSHED;
  1131. else
  1132. journal->j_flags |= JBD2_FLUSHED;
  1133. spin_unlock(&journal->j_state_lock);
  1134. }
  1135. /*
  1136. * Read the superblock for a given journal, performing initial
  1137. * validation of the format.
  1138. */
  1139. static int journal_get_superblock(journal_t *journal)
  1140. {
  1141. struct buffer_head *bh;
  1142. journal_superblock_t *sb;
  1143. int err = -EIO;
  1144. bh = journal->j_sb_buffer;
  1145. J_ASSERT(bh != NULL);
  1146. if (!buffer_uptodate(bh)) {
  1147. ll_rw_block(READ, 1, &bh);
  1148. wait_on_buffer(bh);
  1149. if (!buffer_uptodate(bh)) {
  1150. printk (KERN_ERR
  1151. "JBD: IO error reading journal superblock\n");
  1152. goto out;
  1153. }
  1154. }
  1155. sb = journal->j_superblock;
  1156. err = -EINVAL;
  1157. if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
  1158. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1159. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  1160. goto out;
  1161. }
  1162. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1163. case JBD2_SUPERBLOCK_V1:
  1164. journal->j_format_version = 1;
  1165. break;
  1166. case JBD2_SUPERBLOCK_V2:
  1167. journal->j_format_version = 2;
  1168. break;
  1169. default:
  1170. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  1171. goto out;
  1172. }
  1173. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1174. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1175. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1176. printk (KERN_WARNING "JBD: journal file too short\n");
  1177. goto out;
  1178. }
  1179. return 0;
  1180. out:
  1181. journal_fail_superblock(journal);
  1182. return err;
  1183. }
  1184. /*
  1185. * Load the on-disk journal superblock and read the key fields into the
  1186. * journal_t.
  1187. */
  1188. static int load_superblock(journal_t *journal)
  1189. {
  1190. int err;
  1191. journal_superblock_t *sb;
  1192. err = journal_get_superblock(journal);
  1193. if (err)
  1194. return err;
  1195. sb = journal->j_superblock;
  1196. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1197. journal->j_tail = be32_to_cpu(sb->s_start);
  1198. journal->j_first = be32_to_cpu(sb->s_first);
  1199. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1200. journal->j_errno = be32_to_cpu(sb->s_errno);
  1201. return 0;
  1202. }
  1203. /**
  1204. * int jbd2_journal_load() - Read journal from disk.
  1205. * @journal: Journal to act on.
  1206. *
  1207. * Given a journal_t structure which tells us which disk blocks contain
  1208. * a journal, read the journal from disk to initialise the in-memory
  1209. * structures.
  1210. */
  1211. int jbd2_journal_load(journal_t *journal)
  1212. {
  1213. int err;
  1214. journal_superblock_t *sb;
  1215. err = load_superblock(journal);
  1216. if (err)
  1217. return err;
  1218. sb = journal->j_superblock;
  1219. /* If this is a V2 superblock, then we have to check the
  1220. * features flags on it. */
  1221. if (journal->j_format_version >= 2) {
  1222. if ((sb->s_feature_ro_compat &
  1223. ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
  1224. (sb->s_feature_incompat &
  1225. ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
  1226. printk (KERN_WARNING
  1227. "JBD: Unrecognised features on journal\n");
  1228. return -EINVAL;
  1229. }
  1230. }
  1231. /* Let the recovery code check whether it needs to recover any
  1232. * data from the journal. */
  1233. if (jbd2_journal_recover(journal))
  1234. goto recovery_error;
  1235. /* OK, we've finished with the dynamic journal bits:
  1236. * reinitialise the dynamic contents of the superblock in memory
  1237. * and reset them on disk. */
  1238. if (journal_reset(journal))
  1239. goto recovery_error;
  1240. journal->j_flags &= ~JBD2_ABORT;
  1241. journal->j_flags |= JBD2_LOADED;
  1242. return 0;
  1243. recovery_error:
  1244. printk (KERN_WARNING "JBD: recovery failed\n");
  1245. return -EIO;
  1246. }
  1247. /**
  1248. * void jbd2_journal_destroy() - Release a journal_t structure.
  1249. * @journal: Journal to act on.
  1250. *
  1251. * Release a journal_t structure once it is no longer in use by the
  1252. * journaled object.
  1253. * Return <0 if we couldn't clean up the journal.
  1254. */
  1255. int jbd2_journal_destroy(journal_t *journal)
  1256. {
  1257. int err = 0;
  1258. /* Wait for the commit thread to wake up and die. */
  1259. journal_kill_thread(journal);
  1260. /* Force a final log commit */
  1261. if (journal->j_running_transaction)
  1262. jbd2_journal_commit_transaction(journal);
  1263. /* Force any old transactions to disk */
  1264. /* Totally anal locking here... */
  1265. spin_lock(&journal->j_list_lock);
  1266. while (journal->j_checkpoint_transactions != NULL) {
  1267. spin_unlock(&journal->j_list_lock);
  1268. mutex_lock(&journal->j_checkpoint_mutex);
  1269. jbd2_log_do_checkpoint(journal);
  1270. mutex_unlock(&journal->j_checkpoint_mutex);
  1271. spin_lock(&journal->j_list_lock);
  1272. }
  1273. J_ASSERT(journal->j_running_transaction == NULL);
  1274. J_ASSERT(journal->j_committing_transaction == NULL);
  1275. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1276. spin_unlock(&journal->j_list_lock);
  1277. if (journal->j_sb_buffer) {
  1278. if (!is_journal_aborted(journal)) {
  1279. /* We can now mark the journal as empty. */
  1280. journal->j_tail = 0;
  1281. journal->j_tail_sequence =
  1282. ++journal->j_transaction_sequence;
  1283. jbd2_journal_update_superblock(journal, 1);
  1284. } else {
  1285. err = -EIO;
  1286. }
  1287. brelse(journal->j_sb_buffer);
  1288. }
  1289. if (journal->j_proc_entry)
  1290. jbd2_stats_proc_exit(journal);
  1291. if (journal->j_inode)
  1292. iput(journal->j_inode);
  1293. if (journal->j_revoke)
  1294. jbd2_journal_destroy_revoke(journal);
  1295. kfree(journal->j_wbuf);
  1296. kfree(journal);
  1297. return err;
  1298. }
  1299. /**
  1300. *int jbd2_journal_check_used_features () - Check if features specified are used.
  1301. * @journal: Journal to check.
  1302. * @compat: bitmask of compatible features
  1303. * @ro: bitmask of features that force read-only mount
  1304. * @incompat: bitmask of incompatible features
  1305. *
  1306. * Check whether the journal uses all of a given set of
  1307. * features. Return true (non-zero) if it does.
  1308. **/
  1309. int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
  1310. unsigned long ro, unsigned long incompat)
  1311. {
  1312. journal_superblock_t *sb;
  1313. if (!compat && !ro && !incompat)
  1314. return 1;
  1315. if (journal->j_format_version == 1)
  1316. return 0;
  1317. sb = journal->j_superblock;
  1318. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1319. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1320. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1321. return 1;
  1322. return 0;
  1323. }
  1324. /**
  1325. * int jbd2_journal_check_available_features() - Check feature set in journalling layer
  1326. * @journal: Journal to check.
  1327. * @compat: bitmask of compatible features
  1328. * @ro: bitmask of features that force read-only mount
  1329. * @incompat: bitmask of incompatible features
  1330. *
  1331. * Check whether the journaling code supports the use of
  1332. * all of a given set of features on this journal. Return true
  1333. * (non-zero) if it can. */
  1334. int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
  1335. unsigned long ro, unsigned long incompat)
  1336. {
  1337. journal_superblock_t *sb;
  1338. if (!compat && !ro && !incompat)
  1339. return 1;
  1340. sb = journal->j_superblock;
  1341. /* We can support any known requested features iff the
  1342. * superblock is in version 2. Otherwise we fail to support any
  1343. * extended sb features. */
  1344. if (journal->j_format_version != 2)
  1345. return 0;
  1346. if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
  1347. (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1348. (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
  1349. return 1;
  1350. return 0;
  1351. }
  1352. /**
  1353. * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
  1354. * @journal: Journal to act on.
  1355. * @compat: bitmask of compatible features
  1356. * @ro: bitmask of features that force read-only mount
  1357. * @incompat: bitmask of incompatible features
  1358. *
  1359. * Mark a given journal feature as present on the
  1360. * superblock. Returns true if the requested features could be set.
  1361. *
  1362. */
  1363. int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
  1364. unsigned long ro, unsigned long incompat)
  1365. {
  1366. journal_superblock_t *sb;
  1367. if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
  1368. return 1;
  1369. if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
  1370. return 0;
  1371. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1372. compat, ro, incompat);
  1373. sb = journal->j_superblock;
  1374. sb->s_feature_compat |= cpu_to_be32(compat);
  1375. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1376. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1377. return 1;
  1378. }
  1379. /*
  1380. * jbd2_journal_clear_features () - Clear a given journal feature in the
  1381. * superblock
  1382. * @journal: Journal to act on.
  1383. * @compat: bitmask of compatible features
  1384. * @ro: bitmask of features that force read-only mount
  1385. * @incompat: bitmask of incompatible features
  1386. *
  1387. * Clear a given journal feature as present on the
  1388. * superblock.
  1389. */
  1390. void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
  1391. unsigned long ro, unsigned long incompat)
  1392. {
  1393. journal_superblock_t *sb;
  1394. jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
  1395. compat, ro, incompat);
  1396. sb = journal->j_superblock;
  1397. sb->s_feature_compat &= ~cpu_to_be32(compat);
  1398. sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
  1399. sb->s_feature_incompat &= ~cpu_to_be32(incompat);
  1400. }
  1401. EXPORT_SYMBOL(jbd2_journal_clear_features);
  1402. /**
  1403. * int jbd2_journal_update_format () - Update on-disk journal structure.
  1404. * @journal: Journal to act on.
  1405. *
  1406. * Given an initialised but unloaded journal struct, poke about in the
  1407. * on-disk structure to update it to the most recent supported version.
  1408. */
  1409. int jbd2_journal_update_format (journal_t *journal)
  1410. {
  1411. journal_superblock_t *sb;
  1412. int err;
  1413. err = journal_get_superblock(journal);
  1414. if (err)
  1415. return err;
  1416. sb = journal->j_superblock;
  1417. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1418. case JBD2_SUPERBLOCK_V2:
  1419. return 0;
  1420. case JBD2_SUPERBLOCK_V1:
  1421. return journal_convert_superblock_v1(journal, sb);
  1422. default:
  1423. break;
  1424. }
  1425. return -EINVAL;
  1426. }
  1427. static int journal_convert_superblock_v1(journal_t *journal,
  1428. journal_superblock_t *sb)
  1429. {
  1430. int offset, blocksize;
  1431. struct buffer_head *bh;
  1432. printk(KERN_WARNING
  1433. "JBD: Converting superblock from version 1 to 2.\n");
  1434. /* Pre-initialise new fields to zero */
  1435. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1436. blocksize = be32_to_cpu(sb->s_blocksize);
  1437. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1438. sb->s_nr_users = cpu_to_be32(1);
  1439. sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
  1440. journal->j_format_version = 2;
  1441. bh = journal->j_sb_buffer;
  1442. BUFFER_TRACE(bh, "marking dirty");
  1443. mark_buffer_dirty(bh);
  1444. sync_dirty_buffer(bh);
  1445. return 0;
  1446. }
  1447. /**
  1448. * int jbd2_journal_flush () - Flush journal
  1449. * @journal: Journal to act on.
  1450. *
  1451. * Flush all data for a given journal to disk and empty the journal.
  1452. * Filesystems can use this when remounting readonly to ensure that
  1453. * recovery does not need to happen on remount.
  1454. */
  1455. int jbd2_journal_flush(journal_t *journal)
  1456. {
  1457. int err = 0;
  1458. transaction_t *transaction = NULL;
  1459. unsigned long old_tail;
  1460. spin_lock(&journal->j_state_lock);
  1461. /* Force everything buffered to the log... */
  1462. if (journal->j_running_transaction) {
  1463. transaction = journal->j_running_transaction;
  1464. __jbd2_log_start_commit(journal, transaction->t_tid);
  1465. } else if (journal->j_committing_transaction)
  1466. transaction = journal->j_committing_transaction;
  1467. /* Wait for the log commit to complete... */
  1468. if (transaction) {
  1469. tid_t tid = transaction->t_tid;
  1470. spin_unlock(&journal->j_state_lock);
  1471. jbd2_log_wait_commit(journal, tid);
  1472. } else {
  1473. spin_unlock(&journal->j_state_lock);
  1474. }
  1475. /* ...and flush everything in the log out to disk. */
  1476. spin_lock(&journal->j_list_lock);
  1477. while (!err && journal->j_checkpoint_transactions != NULL) {
  1478. spin_unlock(&journal->j_list_lock);
  1479. mutex_lock(&journal->j_checkpoint_mutex);
  1480. err = jbd2_log_do_checkpoint(journal);
  1481. mutex_unlock(&journal->j_checkpoint_mutex);
  1482. spin_lock(&journal->j_list_lock);
  1483. }
  1484. spin_unlock(&journal->j_list_lock);
  1485. if (is_journal_aborted(journal))
  1486. return -EIO;
  1487. jbd2_cleanup_journal_tail(journal);
  1488. /* Finally, mark the journal as really needing no recovery.
  1489. * This sets s_start==0 in the underlying superblock, which is
  1490. * the magic code for a fully-recovered superblock. Any future
  1491. * commits of data to the journal will restore the current
  1492. * s_start value. */
  1493. spin_lock(&journal->j_state_lock);
  1494. old_tail = journal->j_tail;
  1495. journal->j_tail = 0;
  1496. spin_unlock(&journal->j_state_lock);
  1497. jbd2_journal_update_superblock(journal, 1);
  1498. spin_lock(&journal->j_state_lock);
  1499. journal->j_tail = old_tail;
  1500. J_ASSERT(!journal->j_running_transaction);
  1501. J_ASSERT(!journal->j_committing_transaction);
  1502. J_ASSERT(!journal->j_checkpoint_transactions);
  1503. J_ASSERT(journal->j_head == journal->j_tail);
  1504. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1505. spin_unlock(&journal->j_state_lock);
  1506. return 0;
  1507. }
  1508. /**
  1509. * int jbd2_journal_wipe() - Wipe journal contents
  1510. * @journal: Journal to act on.
  1511. * @write: flag (see below)
  1512. *
  1513. * Wipe out all of the contents of a journal, safely. This will produce
  1514. * a warning if the journal contains any valid recovery information.
  1515. * Must be called between journal_init_*() and jbd2_journal_load().
  1516. *
  1517. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise…