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
25 #include <linux/module.h>
26 #include <linux/time.h>
27 #include <linux/fs.h>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/seq_file.h>
40 #include <linux/math64.h>
41 #include <linux/hash.h>
42
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/jbd2.h>
45
46 #include <asm/uaccess.h>
47 #include <asm/page.h>
48
49 EXPORT_SYMBOL(jbd2_journal_start);
50 EXPORT_SYMBOL(jbd2_journal_restart);
51 EXPORT_SYMBOL(jbd2_journal_extend);
52 EXPORT_SYMBOL(jbd2_journal_stop);
53 EXPORT_SYMBOL(jbd2_journal_lock_updates);
54 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
55 EXPORT_SYMBOL(jbd2_journal_get_write_access);
56 EXPORT_SYMBOL(jbd2_journal_get_create_access);
57 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
58 EXPORT_SYMBOL(jbd2_journal_set_triggers);
59 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
60 EXPORT_SYMBOL(jbd2_journal_release_buffer);
61 EXPORT_SYMBOL(jbd2_journal_forget);
62 #if 0
63 EXPORT_SYMBOL(journal_sync_buffer);
64 #endif
65 EXPORT_SYMBOL(jbd2_journal_flush);
66 EXPORT_SYMBOL(jbd2_journal_revoke);
67
68 EXPORT_SYMBOL(jbd2_journal_init_dev);
69 EXPORT_SYMBOL(jbd2_journal_init_inode);
70 EXPORT_SYMBOL(jbd2_journal_update_format);
71 EXPORT_SYMBOL(jbd2_journal_check_used_features);
72 EXPORT_SYMBOL(jbd2_journal_check_available_features);
73 EXPORT_SYMBOL(jbd2_journal_set_features);
74 EXPORT_SYMBOL(jbd2_journal_load);
75 EXPORT_SYMBOL(jbd2_journal_destroy);
76 EXPORT_SYMBOL(jbd2_journal_abort);
77 EXPORT_SYMBOL(jbd2_journal_errno);
78 EXPORT_SYMBOL(jbd2_journal_ack_err);
79 EXPORT_SYMBOL(jbd2_journal_clear_err);
80 EXPORT_SYMBOL(jbd2_log_wait_commit);
81 EXPORT_SYMBOL(jbd2_log_start_commit);
82 EXPORT_SYMBOL(jbd2_journal_start_commit);
83 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
84 EXPORT_SYMBOL(jbd2_journal_wipe);
85 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
86 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
87 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
88 EXPORT_SYMBOL(jbd2_journal_force_commit);
89 EXPORT_SYMBOL(jbd2_journal_file_inode);
90 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
91 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
92 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
93
94 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
95 static void __journal_abort_soft (journal_t *journal, int errno);
96
97 /*
98 * Helper function used to manage commit timeouts
99 */
100
101 static void commit_timeout(unsigned long __data)
102 {
103 struct task_struct * p = (struct task_struct *) __data;
104
105 wake_up_process(p);
106 }
107
108 /*
109 * kjournald2: The main thread function used to manage a logging device
110 * journal.
111 *
112 * This kernel thread is responsible for two things:
113 *
114 * 1) COMMIT: Every so often we need to commit the current state of the
115 * filesystem to disk. The journal thread is responsible for writing
116 * all of the metadata buffers to disk.
117 *
118 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
119 * of the data in that part of the log has been rewritten elsewhere on
120 * the disk. Flushing these old buffers to reclaim space in the log is
121 * known as checkpointing, and this thread is responsible for that job.
122 */
123
124 static int kjournald2(void *arg)
125 {
126 journal_t *journal = arg;
127 transaction_t *transaction;
128
129 /*
130 * Set up an interval timer which can be used to trigger a commit wakeup
131 * after the commit interval expires
132 */
133 setup_timer(&journal->j_commit_timer, commit_timeout,
134 (unsigned long)current);
135
136 /* Record that the journal thread is running */
137 journal->j_task = current;
138 wake_up(&journal->j_wait_done_commit);
139
140 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
141 "commit interval %ld seconds\n", current->pid,
142 journal->j_devname, journal->j_commit_interval / HZ);
143
144 /*
145 * And now, wait forever for commit wakeup events.
146 */
147 spin_lock(&journal->j_state_lock);
148
149 loop:
150 if (journal->j_flags & JBD2_UNMOUNT)
151 goto end_loop;
152
153 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
154 journal->j_commit_sequence, journal->j_commit_request);
155
156 if (journal->j_commit_sequence != journal->j_commit_request) {
157 jbd_debug(1, "OK, requests differ\n");
158 spin_unlock(&journal->j_state_lock);
159 del_timer_sync(&journal->j_commit_timer);
160 jbd2_journal_commit_transaction(journal);
161 spin_lock(&journal->j_state_lock);
162 goto loop;
163 }
164
165 wake_up(&journal->j_wait_done_commit);
166 if (freezing(current)) {
167 /*
168 * The simpler the better. Flushing journal isn't a
169 * good idea, because that depends on threads that may
170 * be already stopped.
171 */
172 jbd_debug(1, "Now suspending kjournald2\n");
173 spin_unlock(&journal->j_state_lock);
174 refrigerator();
175 spin_lock(&journal->j_state_lock);
176 } else {
177 /*
178 * We assume on resume that commits are already there,
179 * so we don't sleep
180 */
181 DEFINE_WAIT(wait);
182 int should_sleep = 1;
183
184 prepare_to_wait(&journal->j_wait_commit, &wait,
185 TASK_INTERRUPTIBLE);
186 if (journal->j_commit_sequence != journal->j_commit_request)
187 should_sleep = 0;
188 transaction = journal->j_running_transaction;
189 if (transaction && time_after_eq(jiffies,
190 transaction->t_expires))
191 should_sleep = 0;
192 if (journal->j_flags & JBD2_UNMOUNT)
193 should_sleep = 0;
194 if (should_sleep) {
195 spin_unlock(&journal->j_state_lock);
196 schedule();
197 spin_lock(&journal->j_state_lock);
198 }
199 finish_wait(&journal->j_wait_commit, &wait);
200 }
201
202 jbd_debug(1, "kjournald2 wakes\n");
203
204 /*
205 * Were we woken up by a commit wakeup event?
206 */
207 transaction = journal->j_running_transaction;
208 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
209 journal->j_commit_request = transaction->t_tid;
210 jbd_debug(1, "woke because of timeout\n");
211 }
212 goto loop;
213
214 end_loop:
215 spin_unlock(&journal->j_state_lock);
216 del_timer_sync(&journal->j_commit_timer);
217 journal->j_task = NULL;
218 wake_up(&journal->j_wait_done_commit);
219 jbd_debug(1, "Journal thread exiting.\n");
220 return 0;
221 }
222
223 static int jbd2_journal_start_thread(journal_t *journal)
224 {
225 struct task_struct *t;
226
227 t = kthread_run(kjournald2, journal, "kjournald2");
228 if (IS_ERR(t))
229 return PTR_ERR(t);
230
231 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
232 return 0;
233 }
234
235 static void journal_kill_thread(journal_t *journal)
236 {
237 spin_lock(&journal->j_state_lock);
238 journal->j_flags |= JBD2_UNMOUNT;
239
240 while (journal->j_task) {
241 wake_up(&journal->j_wait_commit);
242 spin_unlock(&journal->j_state_lock);
243 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
244 spin_lock(&journal->j_state_lock);
245 }
246 spin_unlock(&journal->j_state_lock);
247 }
248
249 /*
250 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
251 *
252 * Writes a metadata buffer to a given disk block. The actual IO is not
253 * performed but a new buffer_head is constructed which labels the data
254 * to be written with the correct destination disk block.
255 *
256 * Any magic-number escaping which needs to be done will cause a
257 * copy-out here. If the buffer happens to start with the
258 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
259 * magic number is only written to the log for descripter blocks. In
260 * this case, we copy the data and replace the first word with 0, and we
261 * return a result code which indicates that this buffer needs to be
262 * marked as an escaped buffer in the corresponding log descriptor
263 * block. The missing word can then be restored when the block is read
264 * during recovery.
265 *
266 * If the source buffer has already been modified by a new transaction
267 * since we took the last commit snapshot, we use the frozen copy of
268 * that data for IO. If we end up using the existing buffer_head's data
269 * for the write, then we *have* to lock the buffer to prevent anyone
270 * else from using and possibly modifying it while the IO is in
271 * progress.
272 *
273 * The function returns a pointer to the buffer_heads to be used for IO.
274 *
275 * We assume that the journal has already been locked in this function.
276 *
277 * Return value:
278 * <0: Error
279 * >=0: Finished OK
280 *
281 * On success:
282 * Bit 0 set == escape performed on the data
283 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
284 */
285
286 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
287 struct journal_head *jh_in,
288 struct journal_head **jh_out,
289 unsigned long long blocknr)
290 {
291 int need_copy_out = 0;
292 int done_copy_out = 0;
293 int do_escape = 0;
294 char *mapped_data;
295 struct buffer_head *new_bh;
296 struct journal_head *new_jh;
297 struct page *new_page;
298 unsigned int new_offset;
299 struct buffer_head *bh_in = jh2bh(jh_in);
300 struct jbd2_buffer_trigger_type *triggers;
301 journal_t *journal = transaction->t_journal;
302
303 /*
304 * The buffer really shouldn't be locked: only the current committing
305 * transaction is allowed to write it, so nobody else is allowed
306 * to do any IO.
307 *
308 * akpm: except if we're journalling data, and write() output is
309 * also part of a shared mapping, and another thread has
310 * decided to launch a writepage() against this buffer.
311 */
312 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
313
314 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
315 /* keep subsequent assertions sane */
316 new_bh->b_state = 0;
317 init_buffer(new_bh, NULL, NULL);
318 atomic_set(&new_bh->b_count, 1);
319 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
320
321 /*
322 * If a new transaction has already done a buffer copy-out, then
323 * we use that version of the data for the commit.
324 */
325 jbd_lock_bh_state(bh_in);
326 repeat:
327 if (jh_in->b_frozen_data) {
328 done_copy_out = 1;
329 new_page = virt_to_page(jh_in->b_frozen_data);
330 new_offset = offset_in_page(jh_in->b_frozen_data);
331 triggers = jh_in->b_frozen_triggers;
332 } else {
333 new_page = jh2bh(jh_in)->b_page;
334 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
335 triggers = jh_in->b_triggers;
336 }
337
338 mapped_data = kmap_atomic(new_page, KM_USER0);
339 /*
340 * Fire any commit trigger. Do this before checking for escaping,
341 * as the trigger may modify the magic offset. If a copy-out
342 * happens afterwards, it will have the correct data in the buffer.
343 */
344 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
345 triggers);
346
347 /*
348 * Check for escaping
349 */
350 if (*((__be32 *)(mapped_data + new_offset)) ==
351 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
352 need_copy_out = 1;
353 do_escape = 1;
354 }
355 kunmap_atomic(mapped_data, KM_USER0);
356
357 /*
358 * Do we need to do a data copy?
359 */
360 if (need_copy_out && !done_copy_out) {
361 char *tmp;
362
363 jbd_unlock_bh_state(bh_in);
364 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
365 if (!tmp) {
366 jbd2_journal_put_journal_head(new_jh);
367 return -ENOMEM;
368 }
369 jbd_lock_bh_state(bh_in);
370 if (jh_in->b_frozen_data) {
371 jbd2_free(tmp, bh_in->b_size);
372 goto repeat;
373 }
374
375 jh_in->b_frozen_data = tmp;
376 mapped_data = kmap_atomic(new_page, KM_USER0);
377 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
378 kunmap_atomic(mapped_data, KM_USER0);
379
380 new_page = virt_to_page(tmp);
381 new_offset = offset_in_page(tmp);
382 done_copy_out = 1;
383
384 /*
385 * This isn't strictly necessary, as we're using frozen
386 * data for the escaping, but it keeps consistency with
387 * b_frozen_data usage.
388 */
389 jh_in->b_frozen_triggers = jh_in->b_triggers;
390 }
391
392 /*
393 * Did we need to do an escaping? Now we've done all the
394 * copying, we can finally do so.
395 */
396 if (do_escape) {
397 mapped_data = kmap_atomic(new_page, KM_USER0);
398 *((unsigned int *)(mapped_data + new_offset)) = 0;
399 kunmap_atomic(mapped_data, KM_USER0);
400 }
401
402 set_bh_page(new_bh, new_page, new_offset);
403 new_jh->b_transaction = NULL;
404 new_bh->b_size = jh2bh(jh_in)->b_size;
405 new_bh->b_bdev = transaction->t_journal->j_dev;
406 new_bh->b_blocknr = blocknr;
407 set_buffer_mapped(new_bh);
408 set_buffer_dirty(new_bh);
409
410 *jh_out = new_jh;
411
412 /*
413 * The to-be-written buffer needs to get moved to the io queue,
414 * and the original buffer whose contents we are shadowing or
415 * copying is moved to the transaction's shadow queue.
416 */
417 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
418 spin_lock(&journal->j_list_lock);
419 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
420 spin_unlock(&journal->j_list_lock);
421 jbd_unlock_bh_state(bh_in);
422
423 JBUFFER_TRACE(new_jh, "file as BJ_IO");
424 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
425
426 return do_escape | (done_copy_out << 1);
427 }
428
429 /*
430 * Allocation code for the journal file. Manage the space left in the
431 * journal, so that we can begin checkpointing when appropriate.
432 */
433
434 /*
435 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
436 *
437 * Called with the journal already locked.
438 *
439 * Called under j_state_lock
440 */
441
442 int __jbd2_log_space_left(journal_t *journal)
443 {
444 int left = journal->j_free;
445
446 assert_spin_locked(&journal->j_state_lock);
447
448 /*
449 * Be pessimistic here about the number of those free blocks which
450 * might be required for log descriptor control blocks.
451 */
452
453 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
454
455 left -= MIN_LOG_RESERVED_BLOCKS;
456
457 if (left <= 0)
458 return 0;
459 left -= (left >> 3);
460 return left;
461 }
462
463 /*
464 * Called under j_state_lock. Returns true if a transaction commit was started.
465 */
466 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
467 {
468 /*
469 * Are we already doing a recent enough commit?
470 */
471 if (!tid_geq(journal->j_commit_request, target)) {
472 /*
473 * We want a new commit: OK, mark the request and wakup the
474 * commit thread. We do _not_ do the commit ourselves.
475 */
476
477 journal->j_commit_request = target;
478 jbd_debug(1, "JBD: requesting commit %d/%d\n",
479 journal->j_commit_request,
480 journal->j_commit_sequence);
481 wake_up(&journal->j_wait_commit);
482 return 1;
483 }
484 return 0;
485 }
486
487 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
488 {
489 int ret;
490
491 spin_lock(&journal->j_state_lock);
492 ret = __jbd2_log_start_commit(journal, tid);
493 spin_unlock(&journal->j_state_lock);
494 return ret;
495 }
496
497 /*
498 * Force and wait upon a commit if the calling process is not within
499 * transaction. This is used for forcing out undo-protected data which contains
500 * bitmaps, when the fs is running out of space.
501 *
502 * We can only force the running transaction if we don't have an active handle;
503 * otherwise, we will deadlock.
504 *
505 * Returns true if a transaction was started.
506 */
507 int jbd2_journal_force_commit_nested(journal_t *journal)
508 {
509 transaction_t *transaction = NULL;
510 tid_t tid;
511
512 spin_lock(&journal->j_state_lock);
513 if (journal->j_running_transaction && !current->journal_info) {
514 transaction = journal->j_running_transaction;
515 __jbd2_log_start_commit(journal, transaction->t_tid);
516 } else if (journal->j_committing_transaction)
517 transaction = journal->j_committing_transaction;
518
519 if (!transaction) {
520 spin_unlock(&journal->j_state_lock);
521 return 0; /* Nothing to retry */
522 }
523
524 tid = transaction->t_tid;
525 spin_unlock(&journal->j_state_lock);
526 jbd2_log_wait_commit(journal, tid);
527 return 1;
528 }
529
530 /*
531 * Start a commit of the current running transaction (if any). Returns true
532 * if a transaction is going to be committed (or is currently already
533 * committing), and fills its tid in at *ptid
534 */
535 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
536 {
537 int ret = 0;
538
539 spin_lock(&journal->j_state_lock);
540 if (journal->j_running_transaction) {
541 tid_t tid = journal->j_running_transaction->t_tid;
542
543 __jbd2_log_start_commit(journal, tid);
544 /* There's a running transaction and we've just made sure
545 * it's commit has been scheduled. */
546 if (ptid)
547 *ptid = tid;
548 ret = 1;
549 } else if (journal->j_committing_transaction) {
550 /*
551 * If ext3_write_super() recently started a commit, then we
552 * have to wait for completion of that transaction
553 */
554 if (ptid)
555 *ptid = journal->j_committing_transaction->t_tid;
556 ret = 1;
557 }
558 spin_unlock(&journal->j_state_lock);
559 return ret;
560 }
561
562 /*
563 * Wait for a specified commit to complete.
564 * The caller may not hold the journal lock.
565 */
566 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
567 {
568 int err = 0;
569
570 #ifdef CONFIG_JBD2_DEBUG
571 spin_lock(&journal->j_state_lock);
572 if (!tid_geq(journal->j_commit_request, tid)) {
573 printk(KERN_EMERG
574 "%s: error: j_commit_request=%d, tid=%d\n",
575 __func__, journal->j_commit_request, tid);
576 }
577 spin_unlock(&journal->j_state_lock);
578 #endif
579 spin_lock(&journal->j_state_lock);
580 while (tid_gt(tid, journal->j_commit_sequence)) {
581 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
582 tid, journal->j_commit_sequence);
583 wake_up(&journal->j_wait_commit);
584 spin_unlock(&journal->j_state_lock);
585 wait_event(journal->j_wait_done_commit,
586 !tid_gt(tid, journal->j_commit_sequence));
587 spin_lock(&journal->j_state_lock);
588 }
589 spin_unlock(&journal->j_state_lock);
590
591 if (unlikely(is_journal_aborted(journal))) {
592 printk(KERN_EMERG "journal commit I/O error\n");
593 err = -EIO;
594 }
595 return err;
596 }
597
598 /*
599 * Log buffer allocation routines:
600 */
601
602 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
603 {
604 unsigned long blocknr;
605
606 spin_lock(&journal->j_state_lock);
607 J_ASSERT(journal->j_free > 1);
608
609 blocknr = journal->j_head;
610 journal->j_head++;
611 journal->j_free--;
612 if (journal->j_head == journal->j_last)
613 journal->j_head = journal->j_first;
614 spin_unlock(&journal->j_state_lock);
615 return jbd2_journal_bmap(journal, blocknr, retp);
616 }
617
618 /*
619 * Conversion of logical to physical block numbers for the journal
620 *
621 * On external journals the journal blocks are identity-mapped, so
622 * this is a no-op. If needed, we can use j_blk_offset - everything is
623 * ready.
624 */
625 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
626 unsigned long long *retp)
627 {
628 int err = 0;
629 unsigned long long ret;
630
631 if (journal->j_inode) {
632 ret = bmap(journal->j_inode, blocknr);
633 if (ret)
634 *retp = ret;
635 else {
636 printk(KERN_ALERT "%s: journal block not found "
637 "at offset %lu on %s\n",
638 __func__, blocknr, journal->j_devname);
639 err = -EIO;
640 __journal_abort_soft(journal, err);
641 }
642 } else {
643 *retp = blocknr; /* +journal->j_blk_offset */
644 }
645 return err;
646 }
647
648 /*
649 * We play buffer_head aliasing tricks to write data/metadata blocks to
650 * the journal without copying their contents, but for journal
651 * descriptor blocks we do need to generate bona fide buffers.
652 *
653 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
654 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
655 * But we don't bother doing that, so there will be coherency problems with
656 * mmaps of blockdevs which hold live JBD-controlled filesystems.
657 */
658 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
659 {
660 struct buffer_head *bh;
661 unsigned long long blocknr;
662 int err;
663
664 err = jbd2_journal_next_log_block(journal, &blocknr);
665
666 if (err)
667 return NULL;
668
669 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
670 if (!bh)
671 return NULL;
672 lock_buffer(bh);
673 memset(bh->b_data, 0, journal->j_blocksize);
674 set_buffer_uptodate(bh);
675 unlock_buffer(bh);
676 BUFFER_TRACE(bh, "return this buffer");
677 return jbd2_journal_add_journal_head(bh);
678 }
679
680 struct jbd2_stats_proc_session {
681 journal_t *journal;
682 struct transaction_stats_s *stats;
683 int start;
684 int max;
685 };
686
687 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
688 struct transaction_stats_s *ts,
689 int first)
690 {
691 if (ts == s->stats + s->max)
692 ts = s->stats;
693 if (!first && ts == s->stats + s->start)
694 return NULL;
695 while (ts->ts_type == 0) {
696 ts++;
697 if (ts == s->stats + s->max)
698 ts = s->stats;
699 if (ts == s->stats + s->start)
700 return NULL;
701 }
702 return ts;
703
704 }
705
706 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
707 {
708 struct jbd2_stats_proc_session *s = seq->private;
709 struct transaction_stats_s *ts;
710 int l = *pos;
711
712 if (l == 0)
713 return SEQ_START_TOKEN;
714 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
715 if (!ts)
716 return NULL;
717 l--;
718 while (l) {
719 ts = jbd2_history_skip_empty(s, ++ts, 0);
720 if (!ts)
721 break;
722 l--;
723 }
724 return ts;
725 }
726
727 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
728 {
729 struct jbd2_stats_proc_session *s = seq->private;
730 struct transaction_stats_s *ts = v;
731
732 ++*pos;
733 if (v == SEQ_START_TOKEN)
734 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
735 else
736 return jbd2_history_skip_empty(s, ++ts, 0);
737 }
738
739 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
740 {
741 struct transaction_stats_s *ts = v;
742 if (v == SEQ_START_TOKEN) {
743 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
744 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
745 "wait", "run", "lock", "flush", "log", "hndls",
746 "block", "inlog", "ctime", "write", "drop",
747 "close");
748 return 0;
749 }
750 if (ts->ts_type == JBD2_STATS_RUN)
751 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
752 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
753 jiffies_to_msecs(ts->u.run.rs_wait),
754 jiffies_to_msecs(ts->u.run.rs_running),
755 jiffies_to_msecs(ts->u.run.rs_locked),
756 jiffies_to_msecs(ts->u.run.rs_flushing),
757 jiffies_to_msecs(ts->u.run.rs_logging),
758 ts->u.run.rs_handle_count,
759 ts->u.run.rs_blocks,
760 ts->u.run.rs_blocks_logged);
761 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
762 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
763 "C", ts->ts_tid, " ",
764 jiffies_to_msecs(ts->u.chp.cs_chp_time),
765 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
766 ts->u.chp.cs_forced_to_close);
767 else
768 J_ASSERT(0);
769 return 0;
770 }
771
772 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
773 {
774 }
775
776 static struct seq_operations jbd2_seq_history_ops = {
777 .start = jbd2_seq_history_start,
778 .next = jbd2_seq_history_next,
779 .stop = jbd2_seq_history_stop,
780 .show = jbd2_seq_history_show,
781 };
782
783 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
784 {
785 journal_t *journal = PDE(inode)->data;
786 struct jbd2_stats_proc_session *s;
787 int rc, size;
788
789 s = kmalloc(sizeof(*s), GFP_KERNEL);
790 if (s == NULL)
791 return -ENOMEM;
792 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
793 s->stats = kmalloc(size, GFP_KERNEL);
794 if (s->stats == NULL) {
795 kfree(s);
796 return -ENOMEM;
797 }
798 spin_lock(&journal->j_history_lock);
799 memcpy(s->stats, journal->j_history, size);
800 s->max = journal->j_history_max;
801 s->start = journal->j_history_cur % s->max;
802 spin_unlock(&journal->j_history_lock);
803
804 rc = seq_open(file, &jbd2_seq_history_ops);
805 if (rc == 0) {
806 struct seq_file *m = file->private_data;
807 m->private = s;
808 } else {
809 kfree(s->stats);
810 kfree(s);
811 }
812 return rc;
813
814 }
815
816 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
817 {
818 struct seq_file *seq = file->private_data;
819 struct jbd2_stats_proc_session *s = seq->private;
820
821 kfree(s->stats);
822 kfree(s);
823 return seq_release(inode, file);
824 }
825
826 static struct file_operations jbd2_seq_history_fops = {
827 .owner = THIS_MODULE,
828 .open = jbd2_seq_history_open,
829 .read = seq_read,
830 .llseek = seq_lseek,
831 .release = jbd2_seq_history_release,
832 };
833
834 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
835 {
836 return *pos ? NULL : SEQ_START_TOKEN;
837 }
838
839 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
840 {
841 return NULL;
842 }
843
844 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
845 {
846 struct jbd2_stats_proc_session *s = seq->private;
847
848 if (v != SEQ_START_TOKEN)
849 return 0;
850 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
851 s->stats->ts_tid,
852 s->journal->j_max_transaction_buffers);
853 if (s->stats->ts_tid == 0)
854 return 0;
855 seq_printf(seq, "average: \n %ums waiting for transaction\n",
856 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
857 seq_printf(seq, " %ums running transaction\n",
858 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
859 seq_printf(seq, " %ums transaction was being locked\n",
860 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
861 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
862 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
863 seq_printf(seq, " %ums logging transaction\n",
864 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
865 seq_printf(seq, " %lluus average transaction commit time\n",
866 div_u64(s->journal->j_average_commit_time, 1000));
867 seq_printf(seq, " %lu handles per transaction\n",
868 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
869 seq_printf(seq, " %lu blocks per transaction\n",
870 s->stats->u.run.rs_blocks / s->stats->ts_tid);
871 seq_printf(seq, " %lu logged blocks per transaction\n",
872 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
873 return 0;
874 }
875
876 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
877 {
878 }
879
880 static struct seq_operations jbd2_seq_info_ops = {
881 .start = jbd2_seq_info_start,
882 .next = jbd2_seq_info_next,
883 .stop = jbd2_seq_info_stop,
884 .show = jbd2_seq_info_show,
885 };
886
887 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
888 {
889 journal_t *journal = PDE(inode)->data;
890 struct jbd2_stats_proc_session *s;
891 int rc, size;
892
893 s = kmalloc(sizeof(*s), GFP_KERNEL);
894 if (s == NULL)
895 return -ENOMEM;
896 size = sizeof(struct transaction_stats_s);
897 s->stats = kmalloc(size, GFP_KERNEL);
898 if (s->stats == NULL) {
899 kfree(s);
900 return -ENOMEM;
901 }
902 spin_lock(&journal->j_history_lock);
903 memcpy(s->stats, &journal->j_stats, size);
904 s->journal = journal;
905 spin_unlock(&journal->j_history_lock);
906
907 rc = seq_open(file, &jbd2_seq_info_ops);
908 if (rc == 0) {
909 struct seq_file *m = file->private_data;
910 m->private = s;
911 } else {
912 kfree(s->stats);
913 kfree(s);
914 }
915 return rc;
916
917 }
918
919 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
920 {
921 struct seq_file *seq = file->private_data;
922 struct jbd2_stats_proc_session *s = seq->private;
923 kfree(s->stats);
924 kfree(s);
925 return seq_release(inode, file);
926 }
927
928 static struct file_operations jbd2_seq_info_fops = {
929 .owner = THIS_MODULE,
930 .open = jbd2_seq_info_open,
931 .read = seq_read,
932 .llseek = seq_lseek,
933 .release = jbd2_seq_info_release,
934 };
935
936 static struct proc_dir_entry *proc_jbd2_stats;
937
938 static void jbd2_stats_proc_init(journal_t *journal)
939 {
940 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
941 if (journal->j_proc_entry) {
942 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
943 &jbd2_seq_history_fops, journal);
944 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
945 &jbd2_seq_info_fops, journal);
946 }
947 }
948
949 static void jbd2_stats_proc_exit(journal_t *journal)
950 {
951 remove_proc_entry("info", journal->j_proc_entry);
952 remove_proc_entry("history", journal->j_proc_entry);
953 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
954 }
955
956 static void journal_init_stats(journal_t *journal)
957 {
958 int size;
959
960 if (!proc_jbd2_stats)
961 return;
962
963 journal->j_history_max = 100;
964 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
965 journal->j_history = kzalloc(size, GFP_KERNEL);
966 if (!journal->j_history) {
967 journal->j_history_max = 0;
968 return;
969 }
970 spin_lock_init(&journal->j_history_lock);
971 }
972
973 /*
974 * Management for journal control blocks: functions to create and
975 * destroy journal_t structures, and to initialise and read existing
976 * journal blocks from disk. */
977
978 /* First: create and setup a journal_t object in memory. We initialise
979 * very few fields yet: that has to wait until we have created the
980 * journal structures from from scratch, or loaded them from disk. */
981
982 static journal_t * journal_init_common (void)
983 {
984 journal_t *journal;
985 int err;
986
987 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
988 if (!journal)
989 goto fail;
990
991 init_waitqueue_head(&journal->j_wait_transaction_locked);
992 init_waitqueue_head(&journal->j_wait_logspace);
993 init_waitqueue_head(&journal->j_wait_done_commit);
994 init_waitqueue_head(&journal->j_wait_checkpoint);
995 init_waitqueue_head(&journal->j_wait_commit);
996 init_waitqueue_head(&journal->j_wait_updates);
997 mutex_init(&journal->j_barrier);
998 mutex_init(&journal->j_checkpoint_mutex);
999 spin_lock_init(&journal->j_revoke_lock);
1000 spin_lock_init(&journal->j_list_lock);
1001 spin_lock_init(&journal->j_state_lock);
1002
1003 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1004 journal->j_min_batch_time = 0;
1005 journal->j_max_batch_time = 15000; /* 15ms */
1006
1007 /* The journal is marked for error until we succeed with recovery! */
1008 journal->j_flags = JBD2_ABORT;
1009
1010 /* Set up a default-sized revoke table for the new mount. */
1011 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1012 if (err) {
1013 kfree(journal);
1014 goto fail;
1015 }
1016
1017 journal_init_stats(journal);
1018
1019 return journal;
1020 fail:
1021 return NULL;
1022 }
1023
1024 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1025 *
1026 * Create a journal structure assigned some fixed set of disk blocks to
1027 * the journal. We don't actually touch those disk blocks yet, but we
1028 * need to set up all of the mapping information to tell the journaling
1029 * system where the journal blocks are.
1030 *
1031 */
1032
1033 /**
1034 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1035 * @bdev: Block device on which to create the journal
1036 * @fs_dev: Device which hold journalled filesystem for this journal.
1037 * @start: Block nr Start of journal.
1038 * @len: Length of the journal in blocks.
1039 * @blocksize: blocksize of journalling device
1040 *
1041 * Returns: a newly created journal_t *
1042 *
1043 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1044 * range of blocks on an arbitrary block device.
1045 *
1046 */
1047 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1048 struct block_device *fs_dev,
1049 unsigned long long start, int len, int blocksize)
1050 {
1051 journal_t *journal = journal_init_common();
1052 struct buffer_head *bh;
1053 char *p;
1054 int n;
1055
1056 if (!journal)
1057 return NULL;
1058
1059 /* journal descriptor can store up to n blocks -bzzz */
1060 journal->j_blocksize = blocksize;
1061 jbd2_stats_proc_init(journal);
1062 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1063 journal->j_wbufsize = n;
1064 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1065 if (!journal->j_wbuf) {
1066 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1067 __func__);
1068 goto out_err;
1069 }
1070 journal->j_dev = bdev;
1071 journal->j_fs_dev = fs_dev;
1072 journal->j_blk_offset = start;
1073 journal->j_maxlen = len;
1074 bdevname(journal->j_dev, journal->j_devname);
1075 p = journal->j_devname;
1076 while ((p = strchr(p, '/')))
1077 *p = '!';
1078
1079 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1080 if (!bh) {
1081 printk(KERN_ERR
1082 "%s: Cannot get buffer for journal superblock\n",
1083 __func__);
1084 goto out_err;
1085 }
1086 journal->j_sb_buffer = bh;
1087 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1088
1089 return journal;
1090 out_err:
1091 jbd2_stats_proc_exit(journal);
1092 kfree(journal);
1093 return NULL;
1094 }
1095
1096 /**
1097 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1098 * @inode: An inode to create the journal in
1099 *
1100 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1101 * the journal. The inode must exist already, must support bmap() and
1102 * must have all data blocks preallocated.
1103 */
1104 journal_t * jbd2_journal_init_inode (struct inode *inode)
1105 {
1106 struct buffer_head *bh;
1107 journal_t *journal = journal_init_common();
1108 char *p;
1109 int err;
1110 int n;
1111 unsigned long long blocknr;
1112
1113 if (!journal)
1114 return NULL;
1115
1116 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1117 journal->j_inode = inode;
1118 bdevname(journal->j_dev, journal->j_devname);
1119 p = journal->j_devname;
1120 while ((p = strchr(p, '/')))
1121 *p = '!';
1122 p = journal->j_devname + strlen(journal->j_devname);
1123 sprintf(p, ":%lu", journal->j_inode->i_ino);
1124 jbd_debug(1,
1125 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1126 journal, inode->i_sb->s_id, inode->i_ino,
1127 (long long) inode->i_size,
1128 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1129
1130 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1131 journal->j_blocksize = inode->i_sb->s_blocksize;
1132 jbd2_stats_proc_init(journal);
1133
1134 /* journal descriptor can store up to n blocks -bzzz */
1135 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1136 journal->j_wbufsize = n;
1137 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1138 if (!journal->j_wbuf) {
1139 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1140 __func__);
1141 goto out_err;
1142 }
1143
1144 err = jbd2_journal_bmap(journal, 0, &blocknr);
1145 /* If that failed, give up */
1146 if (err) {
1147 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1148 __func__);
1149 goto out_err;
1150 }
1151
1152 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1153 if (!bh) {
1154 printk(KERN_ERR
1155 "%s: Cannot get buffer for journal superblock\n",
1156 __func__);
1157 goto out_err;
1158 }
1159 journal->j_sb_buffer = bh;
1160 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1161
1162 return journal;
1163 out_err:
1164 jbd2_stats_proc_exit(journal);
1165 kfree(journal);
1166 return NULL;
1167 }
1168
1169 /*
1170 * If the journal init or create aborts, we need to mark the journal
1171 * superblock as being NULL to prevent the journal destroy from writing
1172 * back a bogus superblock.
1173 */
1174 static void journal_fail_superblock (journal_t *journal)
1175 {
1176 struct buffer_head *bh = journal->j_sb_buffer;
1177 brelse(bh);
1178 journal->j_sb_buffer = NULL;
1179 }
1180
1181 /*
1182 * Given a journal_t structure, initialise the various fields for
1183 * startup of a new journaling session. We use this both when creating
1184 * a journal, and after recovering an old journal to reset it for
1185 * subsequent use.
1186 */
1187
1188 static int journal_reset(journal_t *journal)
1189 {
1190 journal_superblock_t *sb = journal->j_superblock;
1191 unsigned long long first, last;
1192
1193 first = be32_to_cpu(sb->s_first);
1194 last = be32_to_cpu(sb->s_maxlen);
1195 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1196 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1197 first, last);
1198 journal_fail_superblock(journal);
1199 return -EINVAL;
1200 }
1201
1202 journal->j_first = first;
1203 journal->j_last = last;
1204
1205 journal->j_head = first;
1206 journal->j_tail = first;
1207 journal->j_free = last - first;
1208
1209 journal->j_tail_sequence = journal->j_transaction_sequence;
1210 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1211 journal->j_commit_request = journal->j_commit_sequence;
1212
1213 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1214
1215 /* Add the dynamic fields and write it to disk. */
1216 jbd2_journal_update_superblock(journal, 1);
1217 return jbd2_journal_start_thread(journal);
1218 }
1219
1220 /**
1221 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1222 * @journal: The journal to update.
1223 * @wait: Set to '' if you don't want to wait for IO completion.
1224 *
1225 * Update a journal's dynamic superblock fields and write it to disk,
1226 * optionally waiting for the IO to complete.
1227 */
1228 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1229 {
1230 journal_superblock_t *sb = journal->j_superblock;
1231 struct buffer_head *bh = journal->j_sb_buffer;
1232
1233 /*
1234 * As a special case, if the on-disk copy is already marked as needing
1235 * no recovery (s_start == 0) and there are no outstanding transactions
1236 * in the filesystem, then we can safely defer the superblock update
1237 * until the next commit by setting JBD2_FLUSHED. This avoids
1238 * attempting a write to a potential-readonly device.
1239 */
1240 if (sb->s_start == 0 && journal->j_tail_sequence ==
1241 journal->j_transaction_sequence) {
1242 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1243 "(start %ld, seq %d, errno %d)\n",
1244 journal->j_tail, journal->j_tail_sequence,
1245 journal->j_errno);
1246 goto out;
1247 }
1248
1249 if (buffer_write_io_error(bh)) {
1250 /*
1251 * Oh, dear. A previous attempt to write the journal
1252 * superblock failed. This could happen because the
1253 * USB device was yanked out. Or it could happen to
1254 * be a transient write error and maybe the block will
1255 * be remapped. Nothing we can do but to retry the
1256 * write and hope for the best.
1257 */
1258 printk(KERN_ERR "JBD2: previous I/O error detected "
1259 "for journal superblock update for %s.\n",
1260 journal->j_devname);
1261 clear_buffer_write_io_error(bh);
1262 set_buffer_uptodate(bh);
1263 }
1264
1265 spin_lock(&journal->j_state_lock);
1266 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1267 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1268
1269 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1270 sb->s_start = cpu_to_be32(journal->j_tail);
1271 sb->s_errno = cpu_to_be32(journal->j_errno);
1272 spin_unlock(&journal->j_state_lock);
1273
1274 BUFFER_TRACE(bh, "marking dirty");
1275 mark_buffer_dirty(bh);
1276 if (wait) {
1277 sync_dirty_buffer(bh);
1278 if (buffer_write_io_error(bh)) {
1279 printk(KERN_ERR "JBD2: I/O error detected "
1280 "when updating journal superblock for %s.\n",
1281 journal->j_devname);
1282 clear_buffer_write_io_error(bh);
1283 set_buffer_uptodate(bh);
1284 }
1285 } else
1286 ll_rw_block(SWRITE, 1, &bh);
1287
1288 out:
1289 /* If we have just flushed the log (by marking s_start==0), then
1290 * any future commit will have to be careful to update the
1291 * superblock again to re-record the true start of the log. */
1292
1293 spin_lock(&journal->j_state_lock);
1294 if (sb->s_start)
1295 journal->j_flags &= ~JBD2_FLUSHED;
1296 else
1297 journal->j_flags |= JBD2_FLUSHED;
1298 spin_unlock(&journal->j_state_lock);
1299 }
1300
1301 /*
1302 * Read the superblock for a given journal, performing initial
1303 * validation of the format.
1304 */
1305
1306 static int journal_get_superblock(journal_t *journal)
1307 {
1308 struct buffer_head *bh;
1309 journal_superblock_t *sb;
1310 int err = -EIO;
1311
1312 bh = journal->j_sb_buffer;
1313
1314 J_ASSERT(bh != NULL);
1315 if (!buffer_uptodate(bh)) {
1316 ll_rw_block(READ, 1, &bh);
1317 wait_on_buffer(bh);
1318 if (!buffer_uptodate(bh)) {
1319 printk (KERN_ERR
1320 "JBD: IO error reading journal superblock\n");
1321 goto out;
1322 }
1323 }
1324
1325 sb = journal->j_superblock;
1326
1327 err = -EINVAL;
1328
1329 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1330 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1331 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1332 goto out;
1333 }
1334
1335 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1336 case JBD2_SUPERBLOCK_V1:
1337 journal->j_format_version = 1;
1338 break;
1339 case JBD2_SUPERBLOCK_V2:
1340 journal->j_format_version = 2;
1341 break;
1342 default:
1343 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1344 goto out;
1345 }
1346
1347 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1348 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1349 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1350 printk (KERN_WARNING "JBD: journal file too short\n");
1351 goto out;
1352 }
1353
1354 return 0;
1355
1356 out:
1357 journal_fail_superblock(journal);
1358 return err;
1359 }
1360
1361 /*
1362 * Load the on-disk journal superblock and read the key fields into the
1363 * journal_t.
1364 */
1365
1366 static int load_superblock(journal_t *journal)
1367 {
1368 int err;
1369 journal_superblock_t *sb;
1370
1371 err = journal_get_superblock(journal);
1372 if (err)
1373 return err;
1374
1375 sb = journal->j_superblock;
1376
1377 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1378 journal->j_tail = be32_to_cpu(sb->s_start);
1379 journal->j_first = be32_to_cpu(sb->s_first);
1380 journal->j_last = be32_to_cpu(sb->s_maxlen);
1381 journal->j_errno = be32_to_cpu(sb->s_errno);
1382
1383 return 0;
1384 }
1385
1386
1387 /**
1388 * int jbd2_journal_load() - Read journal from disk.
1389 * @journal: Journal to act on.
1390 *
1391 * Given a journal_t structure which tells us which disk blocks contain
1392 * a journal, read the journal from disk to initialise the in-memory
1393 * structures.
1394 */
1395 int jbd2_journal_load(journal_t *journal)
1396 {
1397 int err;
1398 journal_superblock_t *sb;
1399
1400 err = load_superblock(journal);
1401 if (err)
1402 return err;
1403
1404 sb = journal->j_superblock;
1405 /* If this is a V2 superblock, then we have to check the
1406 * features flags on it. */
1407
1408 if (journal->j_format_version >= 2) {
1409 if ((sb->s_feature_ro_compat &
1410 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1411 (sb->s_feature_incompat &
1412 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1413 printk (KERN_WARNING
1414 "JBD: Unrecognised features on journal\n");
1415 return -EINVAL;
1416 }
1417 }
1418
1419 /* Let the recovery code check whether it needs to recover any
1420 * data from the journal. */
1421 if (jbd2_journal_recover(journal))
1422 goto recovery_error;
1423
1424 if (journal->j_failed_commit) {
1425 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1426 "is corrupt.\n", journal->j_failed_commit,
1427 journal->j_devname);
1428 return -EIO;
1429 }
1430
1431 /* OK, we've finished with the dynamic journal bits:
1432 * reinitialise the dynamic contents of the superblock in memory
1433 * and reset them on disk. */
1434 if (journal_reset(journal))
1435 goto recovery_error;
1436
1437 journal->j_flags &= ~JBD2_ABORT;
1438 journal->j_flags |= JBD2_LOADED;
1439 return 0;
1440
1441 recovery_error:
1442 printk (KERN_WARNING "JBD: recovery failed\n");
1443 return -EIO;
1444 }
1445
1446 /**
1447 * void jbd2_journal_destroy() - Release a journal_t structure.
1448 * @journal: Journal to act on.
1449 *
1450 * Release a journal_t structure once it is no longer in use by the
1451 * journaled object.
1452 * Return <0 if we couldn't clean up the journal.
1453 */
1454 int jbd2_journal_destroy(journal_t *journal)
1455 {
1456 int err = 0;
1457
1458 /* Wait for the commit thread to wake up and die. */
1459 journal_kill_thread(journal);
1460
1461 /* Force a final log commit */
1462 if (journal->j_running_transaction)
1463 jbd2_journal_commit_transaction(journal);
1464
1465 /* Force any old transactions to disk */
1466
1467 /* Totally anal locking here... */
1468 spin_lock(&journal->j_list_lock);
1469 while (journal->j_checkpoint_transactions != NULL) {
1470 spin_unlock(&journal->j_list_lock);
1471 mutex_lock(&journal->j_checkpoint_mutex);
1472 jbd2_log_do_checkpoint(journal);
1473 mutex_unlock(&journal->j_checkpoint_mutex);
1474 spin_lock(&journal->j_list_lock);
1475 }
1476
1477 J_ASSERT(journal->j_running_transaction == NULL);
1478 J_ASSERT(journal->j_committing_transaction == NULL);
1479 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1480 spin_unlock(&journal->j_list_lock);
1481
1482 if (journal->j_sb_buffer) {
1483 if (!is_journal_aborted(journal)) {
1484 /* We can now mark the journal as empty. */
1485 journal->j_tail = 0;
1486 journal->j_tail_sequence =
1487 ++journal->j_transaction_sequence;
1488 jbd2_journal_update_superblock(journal, 1);
1489 } else {
1490 err = -EIO;
1491 }
1492 brelse(journal->j_sb_buffer);
1493 }
1494
1495 if (journal->j_proc_entry)
1496 jbd2_stats_proc_exit(journal);
1497 if (journal->j_inode)
1498 iput(journal->j_inode);
1499 if (journal->j_revoke)
1500 jbd2_journal_destroy_revoke(journal);
1501 kfree(journal->j_wbuf);
1502 kfree(journal);
1503
1504 return err;
1505 }
1506
1507
1508 /**
1509 *int jbd2_journal_check_used_features () - Check if features specified are used.
1510 * @journal: Journal to check.
1511 * @compat: bitmask of compatible features
1512 * @ro: bitmask of features that force read-only mount
1513 * @incompat: bitmask of incompatible features
1514 *
1515 * Check whether the journal uses all of a given set of
1516 * features. Return true (non-zero) if it does.
1517 **/
1518
1519 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1520 unsigned long ro, unsigned long incompat)
1521 {
1522 journal_superblock_t *sb;
1523
1524 if (!compat && !ro && !incompat)
1525 return 1;
1526 if (journal->j_format_version == 1)
1527 return 0;
1528
1529 sb = journal->j_superblock;
1530
1531 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1532 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1533 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1534 return 1;
1535
1536 return 0;
1537 }
1538
1539 /**
1540 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1541 * @journal: Journal to check.
1542 * @compat: bitmask of compatible features
1543 * @ro: bitmask of features that force read-only mount
1544 * @incompat: bitmask of incompatible features
1545 *
1546 * Check whether the journaling code supports the use of
1547 * all of a given set of features on this journal. Return true
1548 * (non-zero) if it can. */
1549
1550 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1551 unsigned long ro, unsigned long incompat)
1552 {
1553 journal_superblock_t *sb;
1554
1555 if (!compat && !ro && !incompat)
1556 return 1;
1557
1558 sb = journal->j_superblock;
1559
1560 /* We can support any known requested features iff the
1561 * superblock is in version 2. Otherwise we fail to support any
1562 * extended sb features. */
1563
1564 if (journal->j_format_version != 2)
1565 return 0;
1566
1567 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1568 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1569 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1570 return 1;
1571
1572 return 0;
1573 }
1574
1575 /**
1576 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1577 * @journal: Journal to act on.
1578 * @compat: bitmask of compatible features
1579 * @ro: bitmask of features that force read-only mount
1580 * @incompat: bitmask of incompatible features
1581 *
1582 * Mark a given journal feature as present on the
1583 * superblock. Returns true if the requested features could be set.
1584 *
1585 */
1586
1587 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1588 unsigned long ro, unsigned long incompat)
1589 {
1590 journal_superblock_t *sb;
1591
1592 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1593 return 1;
1594
1595 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1596 return 0;
1597
1598 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1599 compat, ro, incompat);
1600
1601 sb = journal->j_superblock;
1602
1603 sb->s_feature_compat |= cpu_to_be32(compat);
1604 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1605 sb->s_feature_incompat |= cpu_to_be32(incompat);
1606
1607 return 1;
1608 }
1609
1610 /*
1611 * jbd2_journal_clear_features () - Clear a given journal feature in the
1612 * superblock
1613 * @journal: Journal to act on.
1614 * @compat: bitmask of compatible features
1615 * @ro: bitmask of features that force read-only mount
1616 * @incompat: bitmask of incompatible features
1617 *
1618 * Clear a given journal feature as present on the
1619 * superblock.
1620 */
1621 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1622 unsigned long ro, unsigned long incompat)
1623 {
1624 journal_superblock_t *sb;
1625
1626 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1627 compat, ro, incompat);
1628
1629 sb = journal->j_superblock;
1630
1631 sb->s_feature_compat &= ~cpu_to_be32(compat);
1632 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1633 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1634 }
1635 EXPORT_SYMBOL(jbd2_journal_clear_features);
1636
1637 /**
1638 * int jbd2_journal_update_format () - Update on-disk journal structure.
1639 * @journal: Journal to act on.
1640 *
1641 * Given an initialised but unloaded journal struct, poke about in the
1642 * on-disk structure to update it to the most recent supported version.
1643 */
1644 int jbd2_journal_update_format (journal_t *journal)
1645 {
1646 journal_superblock_t *sb;
1647 int err;
1648
1649 err = journal_get_superblock(journal);
1650 if (err)
1651 return err;
1652
1653 sb = journal->j_superblock;
1654
1655 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1656 case JBD2_SUPERBLOCK_V2:
1657 return 0;
1658 case JBD2_SUPERBLOCK_V1:
1659 return journal_convert_superblock_v1(journal, sb);
1660 default:
1661 break;
1662 }
1663 return -EINVAL;
1664 }
1665
1666 static int journal_convert_superblock_v1(journal_t *journal,
1667 journal_superblock_t *sb)
1668 {
1669 int offset, blocksize;
1670 struct buffer_head *bh;
1671
1672 printk(KERN_WARNING
1673 "JBD: Converting superblock from version 1 to 2.\n");
1674
1675 /* Pre-initialise new fields to zero */
1676 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1677 blocksize = be32_to_cpu(sb->s_blocksize);
1678 memset(&sb->s_feature_compat, 0, blocksize-offset);
1679
1680 sb->s_nr_users = cpu_to_be32(1);
1681 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1682 journal->j_format_version = 2;
1683
1684 bh = journal->j_sb_buffer;
1685 BUFFER_TRACE(bh, "marking dirty");
1686 mark_buffer_dirty(bh);
1687 sync_dirty_buffer(bh);
1688 return 0;
1689 }
1690
1691
1692 /**
1693 * int jbd2_journal_flush () - Flush journal
1694 * @journal: Journal to act on.
1695 *
1696 * Flush all data for a given journal to disk and empty the journal.
1697 * Filesystems can use this when remounting readonly to ensure that
1698 * recovery does not need to happen on remount.
1699 */
1700
1701 int jbd2_journal_flush(journal_t *journal)
1702 {
1703 int err = 0;
1704 transaction_t *transaction = NULL;
1705 unsigned long old_tail;
1706
1707 spin_lock(&journal->j_state_lock);
1708
1709 /* Force everything buffered to the log... */
1710 if (journal->j_running_transaction) {
1711 transaction = journal->j_running_transaction;
1712 __jbd2_log_start_commit(journal, transaction->t_tid);
1713 } else if (journal->j_committing_transaction)
1714 transaction = journal->j_committing_transaction;
1715
1716 /* Wait for the log commit to complete... */
1717 if (transaction) {
1718 tid_t tid = transaction->t_tid;
1719
1720 spin_unlock(&journal->j_state_lock);
1721 jbd2_log_wait_commit(journal, tid);
1722 } else {
1723 spin_unlock(&journal->j_state_lock);
1724 }
1725
1726 /* ...and flush everything in the log out to disk. */
1727 spin_lock(&journal->j_list_lock);
1728 while (!err && journal->j_checkpoint_transactions != NULL) {
1729 spin_unlock(&journal->j_list_lock);
1730 mutex_lock(&journal->j_checkpoint_mutex);
1731 err = jbd2_log_do_checkpoint(journal);
1732 mutex_unlock(&journal->j_checkpoint_mutex);
1733 spin_lock(&journal->j_list_lock);
1734 }
1735 spin_unlock(&journal->j_list_lock);
1736
1737 if (is_journal_aborted(journal))
1738 return -EIO;
1739
1740 jbd2_cleanup_journal_tail(journal);
1741
1742 /* Finally, mark the journal as really needing no recovery.
1743 * This sets s_start==0 in the underlying superblock, which is
1744 * the magic code for a fully-recovered superblock. Any future
1745 * commits of data to the journal will restore the current
1746 * s_start value. */
1747 spin_lock(&journal->j_state_lock);
1748 old_tail = journal->j_tail;
1749 journal->j_tail = 0;
1750 spin_unlock(&journal->j_state_lock);
1751 jbd2_journal_update_superblock(journal, 1);
1752 spin_lock(&journal->j_state_lock);
1753 journal->j_tail = old_tail;
1754
1755 J_ASSERT(!journal->j_running_transaction);
1756 J_ASSERT(!journal->j_committing_transaction);
1757 J_ASSERT(!journal->j_checkpoint_transactions);
1758 J_ASSERT(journal->j_head == journal->j_tail);
1759 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1760 spin_unlock(&journal->j_state_lock);
1761 return 0;
1762 }
1763
1764 /**
1765 * int jbd2_journal_wipe() - Wipe journal contents
1766 * @journal: Journal to act on.
1767 * @write: flag (see below)
1768 *
1769 * Wipe out all of the contents of a journal, safely. This will produce
1770 * a warning if the journal contains any valid recovery information.
1771 * Must be called between journal_init_*() and jbd2_journal_load().
1772 *
1773 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1774 * we merely suppress recovery.
1775 */
1776
1777 int jbd2_journal_wipe(journal_t *journal, int write)
1778 {
1779 journal_superblock_t *sb;
1780 int err = 0;
1781
1782 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1783
1784 err = load_superblock(journal);
1785 if (err)
1786 return err;
1787
1788 sb = journal->j_superblock;
1789
1790 if (!journal->j_tail)
1791 goto no_recovery;
1792
1793 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1794 write ? "Clearing" : "Ignoring");
1795
1796 err = jbd2_journal_skip_recovery(journal);
1797 if (write)
1798 jbd2_journal_update_superblock(journal, 1);
1799
1800 no_recovery:
1801 return err;
1802 }
1803
1804 /*
1805 * Journal abort has very specific semantics, which we describe
1806 * for journal abort.
1807 *
1808 * Two internal functions, which provide abort to the jbd layer
1809 * itself are here.
1810 */
1811
1812 /*
1813 * Quick version for internal journal use (doesn't lock the journal).
1814 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1815 * and don't attempt to make any other journal updates.
1816 */
1817 void __jbd2_journal_abort_hard(journal_t *journal)
1818 {
1819 transaction_t *transaction;
1820
1821 if (journal->j_flags & JBD2_ABORT)
1822 return;
1823
1824 printk(KERN_ERR "Aborting journal on device %s.\n",
1825 journal->j_devname);
1826
1827 spin_lock(&journal->j_state_lock);
1828 journal->j_flags |= JBD2_ABORT;
1829 transaction = journal->j_running_transaction;
1830 if (transaction)
1831 __jbd2_log_start_commit(journal, transaction->t_tid);
1832 spin_unlock(&journal->j_state_lock);
1833 }
1834
1835 /* Soft abort: record the abort error status in the journal superblock,
1836 * but don't do any other IO. */
1837 static void __journal_abort_soft (journal_t *journal, int errno)
1838 {
1839 if (journal->j_flags & JBD2_ABORT)
1840 return;
1841
1842 if (!journal->j_errno)
1843 journal->j_errno = errno;
1844
1845 __jbd2_journal_abort_hard(journal);
1846
1847 if (errno)
1848 jbd2_journal_update_superblock(journal, 1);
1849 }
1850
1851 /**
1852 * void jbd2_journal_abort () - Shutdown the journal immediately.
1853 * @journal: the journal to shutdown.
1854 * @errno: an error number to record in the journal indicating
1855 * the reason for the shutdown.
1856 *
1857 * Perform a complete, immediate shutdown of the ENTIRE
1858 * journal (not of a single transaction). This operation cannot be
1859 * undone without closing and reopening the journal.
1860 *
1861 * The jbd2_journal_abort function is intended to support higher level error
1862 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1863 * mode.
1864 *
1865 * Journal abort has very specific semantics. Any existing dirty,
1866 * unjournaled buffers in the main filesystem will still be written to
1867 * disk by bdflush, but the journaling mechanism will be suspended
1868 * immediately and no further transaction commits will be honoured.
1869 *
1870 * Any dirty, journaled buffers will be written back to disk without
1871 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1872 * filesystem, but we _do_ attempt to leave as much data as possible
1873 * behind for fsck to use for cleanup.
1874 *
1875 * Any attempt to get a new transaction handle on a journal which is in
1876 * ABORT state will just result in an -EROFS error return. A
1877 * jbd2_journal_stop on an existing handle will return -EIO if we have
1878 * entered abort state during the update.
1879 *
1880 * Recursive transactions are not disturbed by journal abort until the
1881 * final jbd2_journal_stop, which will receive the -EIO error.
1882 *
1883 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1884 * which will be recorded (if possible) in the journal superblock. This
1885 * allows a client to record failure conditions in the middle of a
1886 * transaction without having to complete the transaction to record the
1887 * failure to disk. ext3_error, for example, now uses this
1888 * functionality.
1889 *
1890 * Errors which originate from within the journaling layer will NOT
1891 * supply an errno; a null errno implies that absolutely no further
1892 * writes are done to the journal (unless there are any already in
1893 * progress).
1894 *
1895 */
1896
1897 void jbd2_journal_abort(journal_t *journal, int errno)
1898 {
1899 __journal_abort_soft(journal, errno);
1900 }
1901
1902 /**
1903 * int jbd2_journal_errno () - returns the journal's error state.
1904 * @journal: journal to examine.
1905 *
1906 * This is the errno number set with jbd2_journal_abort(), the last
1907 * time the journal was mounted - if the journal was stopped
1908 * without calling abort this will be 0.
1909 *
1910 * If the journal has been aborted on this mount time -EROFS will
1911 * be returned.
1912 */
1913 int jbd2_journal_errno(journal_t *journal)
1914 {
1915 int err;
1916
1917 spin_lock(&journal->j_state_lock);
1918 if (journal->j_flags & JBD2_ABORT)
1919 err = -EROFS;
1920 else
1921 err = journal->j_errno;
1922 spin_unlock(&journal->j_state_lock);
1923 return err;
1924 }
1925
1926 /**
1927 * int jbd2_journal_clear_err () - clears the journal's error state
1928 * @journal: journal to act on.
1929 *
1930 * An error must be cleared or acked to take a FS out of readonly
1931 * mode.
1932 */
1933 int jbd2_journal_clear_err(journal_t *journal)
1934 {
1935 int err = 0;
1936
1937 spin_lock(&journal->j_state_lock);
1938 if (journal->j_flags & JBD2_ABORT)
1939 err = -EROFS;
1940 else
1941 journal->j_errno = 0;
1942 spin_unlock(&journal->j_state_lock);
1943 return err;
1944 }
1945
1946 /**
1947 * void jbd2_journal_ack_err() - Ack journal err.
1948 * @journal: journal to act on.
1949 *
1950 * An error must be cleared or acked to take a FS out of readonly
1951 * mode.
1952 */
1953 void jbd2_journal_ack_err(journal_t *journal)
1954 {
1955 spin_lock(&journal->j_state_lock);
1956 if (journal->j_errno)
1957 journal->j_flags |= JBD2_ACK_ERR;
1958 spin_unlock(&journal->j_state_lock);
1959 }
1960
1961 int jbd2_journal_blocks_per_page(struct inode *inode)
1962 {
1963 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1964 }
1965
1966 /*
1967 * helper functions to deal with 32 or 64bit block numbers.
1968 */
1969 size_t journal_tag_bytes(journal_t *journal)
1970 {
1971 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1972 return JBD2_TAG_SIZE64;
1973 else
1974 return JBD2_TAG_SIZE32;
1975 }
1976
1977 /*
1978 * Journal_head storage management
1979 */
1980 static struct kmem_cache *jbd2_journal_head_cache;
1981 #ifdef CONFIG_JBD2_DEBUG
1982 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1983 #endif
1984
1985 static int journal_init_jbd2_journal_head_cache(void)
1986 {
1987 int retval;
1988
1989 J_ASSERT(jbd2_journal_head_cache == NULL);
1990 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1991 sizeof(struct journal_head),
1992 0, /* offset */
1993 SLAB_TEMPORARY, /* flags */
1994 NULL); /* ctor */
1995 retval = 0;
1996 if (!jbd2_journal_head_cache) {
1997 retval = -ENOMEM;
1998 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1999 }
2000 return retval;
2001 }
2002
2003 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
2004 {
2005 if (jbd2_journal_head_cache) {
2006 kmem_cache_destroy(jbd2_journal_head_cache);
2007 jbd2_journal_head_cache = NULL;
2008 }
2009 }
2010
2011 /*
2012 * journal_head splicing and dicing
2013 */
2014 static struct journal_head *journal_alloc_journal_head(void)
2015 {
2016 struct journal_head *ret;
2017 static unsigned long last_warning;
2018
2019 #ifdef CONFIG_JBD2_DEBUG
2020 atomic_inc(&nr_journal_heads);
2021 #endif
2022 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2023 if (!ret) {
2024 jbd_debug(1, "out of memory for journal_head\n");
2025 if (time_after(jiffies, last_warning + 5*HZ)) {
2026 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2027 __func__);
2028 last_warning = jiffies;
2029 }
2030 while (!ret) {
2031 yield();
2032 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2033 }
2034 }
2035 return ret;
2036 }
2037
2038 static void journal_free_journal_head(struct journal_head *jh)
2039 {
2040 #ifdef CONFIG_JBD2_DEBUG
2041 atomic_dec(&nr_journal_heads);
2042 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2043 #endif
2044 kmem_cache_free(jbd2_journal_head_cache, jh);
2045 }
2046
2047 /*
2048 * A journal_head is attached to a buffer_head whenever JBD has an
2049 * interest in the buffer.
2050 *
2051 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2052 * is set. This bit is tested in core kernel code where we need to take
2053 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2054 * there.
2055 *
2056 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2057 *
2058 * When a buffer has its BH_JBD bit set it is immune from being released by
2059 * core kernel code, mainly via ->b_count.
2060 *
2061 * A journal_head may be detached from its buffer_head when the journal_head's
2062 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2063 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2064 * journal_head can be dropped if needed.
2065 *
2066 * Various places in the kernel want to attach a journal_head to a buffer_head
2067 * _before_ attaching the journal_head to a transaction. To protect the
2068 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2069 * journal_head's b_jcount refcount by one. The caller must call
2070 * jbd2_journal_put_journal_head() to undo this.
2071 *
2072 * So the typical usage would be:
2073 *
2074 * (Attach a journal_head if needed. Increments b_jcount)
2075 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2076 * ...
2077 * jh->b_transaction = xxx;
2078 * jbd2_journal_put_journal_head(jh);
2079 *
2080 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2081 * because it has a non-zero b_transaction.
2082 */
2083
2084 /*
2085 * Give a buffer_head a journal_head.
2086 *
2087 * Doesn't need the journal lock.
2088 * May sleep.
2089 */
2090 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2091 {
2092 struct journal_head *jh;
2093 struct journal_head *new_jh = NULL;
2094
2095 repeat:
2096 if (!buffer_jbd(bh)) {
2097 new_jh = journal_alloc_journal_head();
2098 memset(new_jh, 0, sizeof(*new_jh));
2099 }
2100
2101 jbd_lock_bh_journal_head(bh);
2102 if (buffer_jbd(bh)) {
2103 jh = bh2jh(bh);
2104 } else {
2105 J_ASSERT_BH(bh,
2106 (atomic_read(&bh->b_count) > 0) ||
2107 (bh->b_page && bh->b_page->mapping));
2108
2109 if (!new_jh) {
2110 jbd_unlock_bh_journal_head(bh);
2111 goto repeat;
2112 }
2113
2114 jh = new_jh;
2115 new_jh = NULL; /* We consumed it */
2116 set_buffer_jbd(bh);
2117 bh->b_private = jh;
2118 jh->b_bh = bh;
2119 get_bh(bh);
2120 BUFFER_TRACE(bh, "added journal_head");
2121 }
2122 jh->b_jcount++;
2123 jbd_unlock_bh_journal_head(bh);
2124 if (new_jh)
2125 journal_free_journal_head(new_jh);
2126 return bh->b_private;
2127 }
2128
2129 /*
2130 * Grab a ref against this buffer_head's journal_head. If it ended up not
2131 * having a journal_head, return NULL
2132 */
2133 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2134 {
2135 struct journal_head *jh = NULL;
2136
2137 jbd_lock_bh_journal_head(bh);
2138 if (buffer_jbd(bh)) {
2139 jh = bh2jh(bh);
2140 jh->b_jcount++;
2141 }
2142 jbd_unlock_bh_journal_head(bh);
2143 return jh;
2144 }
2145
2146 static void __journal_remove_journal_head(struct buffer_head *bh)
2147 {
2148 struct journal_head *jh = bh2jh(bh);
2149
2150 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2151
2152 get_bh(bh);
2153 if (jh->b_jcount == 0) {
2154 if (jh->b_transaction == NULL &&
2155 jh->b_next_transaction == NULL &&
2156 jh->b_cp_transaction == NULL) {
2157 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2158 J_ASSERT_BH(bh, buffer_jbd(bh));
2159 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2160 BUFFER_TRACE(bh, "remove journal_head");
2161 if (jh->b_frozen_data) {
2162 printk(KERN_WARNING "%s: freeing "
2163 "b_frozen_data\n",
2164 __func__);
2165 jbd2_free(jh->b_frozen_data, bh->b_size);
2166 }
2167 if (jh->b_committed_data) {
2168 printk(KERN_WARNING "%s: freeing "
2169 "b_committed_data\n",
2170 __func__);
2171 jbd2_free(jh->b_committed_data, bh->b_size);
2172 }
2173 bh->b_private = NULL;
2174 jh->b_bh = NULL; /* debug, really */
2175 clear_buffer_jbd(bh);
2176 __brelse(bh);
2177 journal_free_journal_head(jh);
2178 } else {
2179 BUFFER_TRACE(bh, "journal_head was locked");
2180 }
2181 }
2182 }
2183
2184 /*
2185 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2186 * and has a zero b_jcount then remove and release its journal_head. If we did
2187 * see that the buffer is not used by any transaction we also "logically"
2188 * decrement ->b_count.
2189 *
2190 * We in fact take an additional increment on ->b_count as a convenience,
2191 * because the caller usually wants to do additional things with the bh
2192 * after calling here.
2193 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2194 * time. Once the caller has run __brelse(), the buffer is eligible for
2195 * reaping by try_to_free_buffers().
2196 */
2197 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2198 {
2199 jbd_lock_bh_journal_head(bh);
2200 __journal_remove_journal_head(bh);
2201 jbd_unlock_bh_journal_head(bh);
2202 }
2203
2204 /*
2205 * Drop a reference on the passed journal_head. If it fell to zero then try to
2206 * release the journal_head from the buffer_head.
2207 */
2208 void jbd2_journal_put_journal_head(struct journal_head *jh)
2209 {
2210 struct buffer_head *bh = jh2bh(jh);
2211
2212 jbd_lock_bh_journal_head(bh);
2213 J_ASSERT_JH(jh, jh->b_jcount > 0);
2214 --jh->b_jcount;
2215 if (!jh->b_jcount && !jh->b_transaction) {
2216 __journal_remove_journal_head(bh);
2217 __brelse(bh);
2218 }
2219 jbd_unlock_bh_journal_head(bh);
2220 }
2221
2222 /*
2223 * Initialize jbd inode head
2224 */
2225 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2226 {
2227 jinode->i_transaction = NULL;
2228 jinode->i_next_transaction = NULL;
2229 jinode->i_vfs_inode = inode;
2230 jinode->i_flags = 0;
2231 INIT_LIST_HEAD(&jinode->i_list);
2232 }
2233
2234 /*
2235 * Function to be called before we start removing inode from memory (i.e.,
2236 * clear_inode() is a fine place to be called from). It removes inode from
2237 * transaction's lists.
2238 */
2239 void jbd2_journal_release_jbd_inode(journal_t *journal,
2240 struct jbd2_inode *jinode)
2241 {
2242 int writeout = 0;
2243
2244 if (!journal)
2245 return;
2246 restart:
2247 spin_lock(&journal->j_list_lock);
2248 /* Is commit writing out inode - we have to wait */
2249 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2250 wait_queue_head_t *wq;
2251 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2252 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2253 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2254 spin_unlock(&journal->j_list_lock);
2255 schedule();
2256 finish_wait(wq, &wait.wait);
2257 goto restart;
2258 }
2259
2260 /* Do we need to wait for data writeback? */
2261 if (journal->j_committing_transaction == jinode->i_transaction)
2262 writeout = 1;
2263 if (jinode->i_transaction) {
2264 list_del(&jinode->i_list);
2265 jinode->i_transaction = NULL;
2266 }
2267 spin_unlock(&journal->j_list_lock);
2268 }
2269
2270 /*
2271 * debugfs tunables
2272 */
2273 #ifdef CONFIG_JBD2_DEBUG
2274 u8 jbd2_journal_enable_debug __read_mostly;
2275 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2276
2277 #define JBD2_DEBUG_NAME "jbd2-debug"
2278
2279 static struct dentry *jbd2_debugfs_dir;
2280 static struct dentry *jbd2_debug;
2281
2282 static void __init jbd2_create_debugfs_entry(void)
2283 {
2284 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2285 if (jbd2_debugfs_dir)
2286 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2287 jbd2_debugfs_dir,
2288 &jbd2_journal_enable_debug);
2289 }
2290
2291 static void __exit jbd2_remove_debugfs_entry(void)
2292 {
2293 debugfs_remove(jbd2_debug);
2294 debugfs_remove(jbd2_debugfs_dir);
2295 }
2296
2297 #else
2298
2299 static void __init jbd2_create_debugfs_entry(void)
2300 {
2301 }
2302
2303 static void __exit jbd2_remove_debugfs_entry(void)
2304 {
2305 }
2306
2307 #endif
2308
2309 #ifdef CONFIG_PROC_FS
2310
2311 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2312
2313 static void __init jbd2_create_jbd_stats_proc_entry(void)
2314 {
2315 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2316 }
2317
2318 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2319 {
2320 if (proc_jbd2_stats)
2321 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2322 }
2323
2324 #else
2325
2326 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2327 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2328
2329 #endif
2330
2331 struct kmem_cache *jbd2_handle_cache;
2332
2333 static int __init journal_init_handle_cache(void)
2334 {
2335 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2336 sizeof(handle_t),
2337 0, /* offset */
2338 SLAB_TEMPORARY, /* flags */
2339 NULL); /* ctor */
2340 if (jbd2_handle_cache == NULL) {
2341 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2342 return -ENOMEM;
2343 }
2344 return 0;
2345 }
2346
2347 static void jbd2_journal_destroy_handle_cache(void)
2348 {
2349 if (jbd2_handle_cache)
2350 kmem_cache_destroy(jbd2_handle_cache);
2351 }
2352
2353 /*
2354 * Module startup and shutdown
2355 */
2356
2357 static int __init journal_init_caches(void)
2358 {
2359 int ret;
2360
2361 ret = jbd2_journal_init_revoke_caches();
2362 if (ret == 0)
2363 ret = journal_init_jbd2_journal_head_cache();
2364 if (ret == 0)
2365 ret = journal_init_handle_cache();
2366 return ret;
2367 }
2368
2369 static void jbd2_journal_destroy_caches(void)
2370 {
2371 jbd2_journal_destroy_revoke_caches();
2372 jbd2_journal_destroy_jbd2_journal_head_cache();
2373 jbd2_journal_destroy_handle_cache();
2374 }
2375
2376 static int __init journal_init(void)
2377 {
2378 int ret;
2379
2380 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2381
2382 ret = journal_init_caches();
2383 if (ret == 0) {
2384 jbd2_create_debugfs_entry();
2385 jbd2_create_jbd_stats_proc_entry();
2386 } else {
2387 jbd2_journal_destroy_caches();
2388 }
2389 return ret;
2390 }
2391
2392 static void __exit journal_exit(void)
2393 {
2394 #ifdef CONFIG_JBD2_DEBUG
2395 int n = atomic_read(&nr_journal_heads);
2396 if (n)
2397 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2398 #endif
2399 jbd2_remove_debugfs_entry();
2400 jbd2_remove_jbd_stats_proc_entry();
2401 jbd2_journal_destroy_caches();
2402 }
2403
2404 /*
2405 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2406 * tracing infrastructure to map a dev_t to a device name.
2407 *
2408 * The caller should use rcu_read_lock() in order to make sure the
2409 * device name stays valid until its done with it. We use
2410 * rcu_read_lock() as well to make sure we're safe in case the caller
2411 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2412 * nested.
2413 */
2414 struct devname_cache {
2415 struct rcu_head rcu;
2416 dev_t device;
2417 char devname[BDEVNAME_SIZE];
2418 };
2419 #define CACHE_SIZE_BITS 6
2420 static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2421 static DEFINE_SPINLOCK(devname_cache_lock);
2422
2423 static void free_devcache(struct rcu_head *rcu)
2424 {
2425 kfree(rcu);
2426 }
2427
2428 const char *jbd2_dev_to_name(dev_t device)
2429 {
2430 int i = hash_32(device, CACHE_SIZE_BITS);
2431 char *ret;
2432 struct block_device *bd;
2433 static struct devname_cache *new_dev;
2434
2435 rcu_read_lock();
2436 if (devcache[i] && devcache[i]->device == device) {
2437 ret = devcache[i]->devname;
2438 rcu_read_unlock();
2439 return ret;
2440 }
2441 rcu_read_unlock();
2442
2443 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2444 if (!new_dev)
2445 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
2446 spin_lock(&devname_cache_lock);
2447 if (devcache[i]) {
2448 if (devcache[i]->device == device) {
2449 kfree(new_dev);
2450 ret = devcache[i]->devname;
2451 spin_unlock(&devname_cache_lock);
2452 return ret;
2453 }
2454 call_rcu(&devcache[i]->rcu, free_devcache);
2455 }
2456 devcache[i] = new_dev;
2457 devcache[i]->device = device;
2458 bd = bdget(device);
2459 if (bd) {
2460 bdevname(bd, devcache[i]->devname);
2461 bdput(bd);
2462 } else
2463 __bdevname(device, devcache[i]->devname);
2464 ret = devcache[i]->devname;
2465 spin_unlock(&devname_cache_lock);
2466 return ret;
2467 }
2468 EXPORT_SYMBOL(jbd2_dev_to_name);
2469
2470 MODULE_LICENSE("GPL");
2471 module_init(journal_init);
2472 module_exit(journal_exit);
2473
2474
|
This page was automatically generated by the
LXR engine.
|