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