blob: eb343008eded0fd86851417f60717650dda96857 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
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>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -050040#include <linux/math64.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070041
42#include <asm/uaccess.h>
43#include <asm/page.h>
44
Mingming Caof7f4bcc2006-10-11 01:20:59 -070045EXPORT_SYMBOL(jbd2_journal_start);
46EXPORT_SYMBOL(jbd2_journal_restart);
47EXPORT_SYMBOL(jbd2_journal_extend);
48EXPORT_SYMBOL(jbd2_journal_stop);
49EXPORT_SYMBOL(jbd2_journal_lock_updates);
50EXPORT_SYMBOL(jbd2_journal_unlock_updates);
51EXPORT_SYMBOL(jbd2_journal_get_write_access);
52EXPORT_SYMBOL(jbd2_journal_get_create_access);
53EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070054EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070055EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
56EXPORT_SYMBOL(jbd2_journal_release_buffer);
57EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070058#if 0
59EXPORT_SYMBOL(journal_sync_buffer);
60#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070061EXPORT_SYMBOL(jbd2_journal_flush);
62EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070063
Mingming Caof7f4bcc2006-10-11 01:20:59 -070064EXPORT_SYMBOL(jbd2_journal_init_dev);
65EXPORT_SYMBOL(jbd2_journal_init_inode);
66EXPORT_SYMBOL(jbd2_journal_update_format);
67EXPORT_SYMBOL(jbd2_journal_check_used_features);
68EXPORT_SYMBOL(jbd2_journal_check_available_features);
69EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070070EXPORT_SYMBOL(jbd2_journal_load);
71EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070072EXPORT_SYMBOL(jbd2_journal_abort);
73EXPORT_SYMBOL(jbd2_journal_errno);
74EXPORT_SYMBOL(jbd2_journal_ack_err);
75EXPORT_SYMBOL(jbd2_journal_clear_err);
76EXPORT_SYMBOL(jbd2_log_wait_commit);
77EXPORT_SYMBOL(jbd2_journal_start_commit);
78EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
79EXPORT_SYMBOL(jbd2_journal_wipe);
80EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
81EXPORT_SYMBOL(jbd2_journal_invalidatepage);
82EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
83EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040084EXPORT_SYMBOL(jbd2_journal_file_inode);
85EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
86EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
87EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070088
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070091
92/*
93 * Helper function used to manage commit timeouts
94 */
95
96static void commit_timeout(unsigned long __data)
97{
98 struct task_struct * p = (struct task_struct *) __data;
99
100 wake_up_process(p);
101}
102
103/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700104 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700105 * journal.
106 *
107 * This kernel thread is responsible for two things:
108 *
109 * 1) COMMIT: Every so often we need to commit the current state of the
110 * filesystem to disk. The journal thread is responsible for writing
111 * all of the metadata buffers to disk.
112 *
113 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
114 * of the data in that part of the log has been rewritten elsewhere on
115 * the disk. Flushing these old buffers to reclaim space in the log is
116 * known as checkpointing, and this thread is responsible for that job.
117 */
118
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700119static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700120{
121 journal_t *journal = arg;
122 transaction_t *transaction;
123
124 /*
125 * Set up an interval timer which can be used to trigger a commit wakeup
126 * after the commit interval expires
127 */
128 setup_timer(&journal->j_commit_timer, commit_timeout,
129 (unsigned long)current);
130
131 /* Record that the journal thread is running */
132 journal->j_task = current;
133 wake_up(&journal->j_wait_done_commit);
134
Theodore Ts'o4a9bf992009-01-03 22:56:44 -0500135 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
136 "commit interval %ld seconds\n", current->pid,
137 journal->j_devname, journal->j_commit_interval / HZ);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700138
139 /*
140 * And now, wait forever for commit wakeup events.
141 */
142 spin_lock(&journal->j_state_lock);
143
144loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700145 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146 goto end_loop;
147
148 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
149 journal->j_commit_sequence, journal->j_commit_request);
150
151 if (journal->j_commit_sequence != journal->j_commit_request) {
152 jbd_debug(1, "OK, requests differ\n");
153 spin_unlock(&journal->j_state_lock);
154 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700155 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700156 spin_lock(&journal->j_state_lock);
157 goto loop;
158 }
159
160 wake_up(&journal->j_wait_done_commit);
161 if (freezing(current)) {
162 /*
163 * The simpler the better. Flushing journal isn't a
164 * good idea, because that depends on threads that may
165 * be already stopped.
166 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700167 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700168 spin_unlock(&journal->j_state_lock);
169 refrigerator();
170 spin_lock(&journal->j_state_lock);
171 } else {
172 /*
173 * We assume on resume that commits are already there,
174 * so we don't sleep
175 */
176 DEFINE_WAIT(wait);
177 int should_sleep = 1;
178
179 prepare_to_wait(&journal->j_wait_commit, &wait,
180 TASK_INTERRUPTIBLE);
181 if (journal->j_commit_sequence != journal->j_commit_request)
182 should_sleep = 0;
183 transaction = journal->j_running_transaction;
184 if (transaction && time_after_eq(jiffies,
185 transaction->t_expires))
186 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700187 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700188 should_sleep = 0;
189 if (should_sleep) {
190 spin_unlock(&journal->j_state_lock);
191 schedule();
192 spin_lock(&journal->j_state_lock);
193 }
194 finish_wait(&journal->j_wait_commit, &wait);
195 }
196
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700197 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198
199 /*
200 * Were we woken up by a commit wakeup event?
201 */
202 transaction = journal->j_running_transaction;
203 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
204 journal->j_commit_request = transaction->t_tid;
205 jbd_debug(1, "woke because of timeout\n");
206 }
207 goto loop;
208
209end_loop:
210 spin_unlock(&journal->j_state_lock);
211 del_timer_sync(&journal->j_commit_timer);
212 journal->j_task = NULL;
213 wake_up(&journal->j_wait_done_commit);
214 jbd_debug(1, "Journal thread exiting.\n");
215 return 0;
216}
217
Pavel Emelianov97f06782007-05-08 00:30:42 -0700218static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700219{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700220 struct task_struct *t;
221
222 t = kthread_run(kjournald2, journal, "kjournald2");
223 if (IS_ERR(t))
224 return PTR_ERR(t);
225
Al Viro1076d172008-03-29 03:07:18 +0000226 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700228}
229
230static void journal_kill_thread(journal_t *journal)
231{
232 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700233 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234
235 while (journal->j_task) {
236 wake_up(&journal->j_wait_commit);
237 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000238 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700239 spin_lock(&journal->j_state_lock);
240 }
241 spin_unlock(&journal->j_state_lock);
242}
243
244/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700245 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246 *
247 * Writes a metadata buffer to a given disk block. The actual IO is not
248 * performed but a new buffer_head is constructed which labels the data
249 * to be written with the correct destination disk block.
250 *
251 * Any magic-number escaping which needs to be done will cause a
252 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700253 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700254 * magic number is only written to the log for descripter blocks. In
255 * this case, we copy the data and replace the first word with 0, and we
256 * return a result code which indicates that this buffer needs to be
257 * marked as an escaped buffer in the corresponding log descriptor
258 * block. The missing word can then be restored when the block is read
259 * during recovery.
260 *
261 * If the source buffer has already been modified by a new transaction
262 * since we took the last commit snapshot, we use the frozen copy of
263 * that data for IO. If we end up using the existing buffer_head's data
264 * for the write, then we *have* to lock the buffer to prevent anyone
265 * else from using and possibly modifying it while the IO is in
266 * progress.
267 *
268 * The function returns a pointer to the buffer_heads to be used for IO.
269 *
270 * We assume that the journal has already been locked in this function.
271 *
272 * Return value:
273 * <0: Error
274 * >=0: Finished OK
275 *
276 * On success:
277 * Bit 0 set == escape performed on the data
278 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 */
280
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700281int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700282 struct journal_head *jh_in,
283 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700284 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700285{
286 int need_copy_out = 0;
287 int done_copy_out = 0;
288 int do_escape = 0;
289 char *mapped_data;
290 struct buffer_head *new_bh;
291 struct journal_head *new_jh;
292 struct page *new_page;
293 unsigned int new_offset;
294 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700295 struct jbd2_buffer_trigger_type *triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700296
297 /*
298 * The buffer really shouldn't be locked: only the current committing
299 * transaction is allowed to write it, so nobody else is allowed
300 * to do any IO.
301 *
302 * akpm: except if we're journalling data, and write() output is
303 * also part of a shared mapping, and another thread has
304 * decided to launch a writepage() against this buffer.
305 */
306 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
307
308 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
309
310 /*
311 * If a new transaction has already done a buffer copy-out, then
312 * we use that version of the data for the commit.
313 */
314 jbd_lock_bh_state(bh_in);
315repeat:
316 if (jh_in->b_frozen_data) {
317 done_copy_out = 1;
318 new_page = virt_to_page(jh_in->b_frozen_data);
319 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700320 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700321 } else {
322 new_page = jh2bh(jh_in)->b_page;
323 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700324 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325 }
326
327 mapped_data = kmap_atomic(new_page, KM_USER0);
328 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700329 * Fire any commit trigger. Do this before checking for escaping,
330 * as the trigger may modify the magic offset. If a copy-out
331 * happens afterwards, it will have the correct data in the buffer.
332 */
333 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
334 triggers);
335
336 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700337 * Check for escaping
338 */
339 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700340 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 need_copy_out = 1;
342 do_escape = 1;
343 }
344 kunmap_atomic(mapped_data, KM_USER0);
345
346 /*
347 * Do we need to do a data copy?
348 */
349 if (need_copy_out && !done_copy_out) {
350 char *tmp;
351
352 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400353 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700354 jbd_lock_bh_state(bh_in);
355 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400356 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700357 goto repeat;
358 }
359
360 jh_in->b_frozen_data = tmp;
361 mapped_data = kmap_atomic(new_page, KM_USER0);
362 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
363 kunmap_atomic(mapped_data, KM_USER0);
364
365 new_page = virt_to_page(tmp);
366 new_offset = offset_in_page(tmp);
367 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700368
369 /*
370 * This isn't strictly necessary, as we're using frozen
371 * data for the escaping, but it keeps consistency with
372 * b_frozen_data usage.
373 */
374 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700375 }
376
377 /*
378 * Did we need to do an escaping? Now we've done all the
379 * copying, we can finally do so.
380 */
381 if (do_escape) {
382 mapped_data = kmap_atomic(new_page, KM_USER0);
383 *((unsigned int *)(mapped_data + new_offset)) = 0;
384 kunmap_atomic(mapped_data, KM_USER0);
385 }
386
387 /* keep subsequent assertions sane */
388 new_bh->b_state = 0;
389 init_buffer(new_bh, NULL, NULL);
390 atomic_set(&new_bh->b_count, 1);
391 jbd_unlock_bh_state(bh_in);
392
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700393 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394
395 set_bh_page(new_bh, new_page, new_offset);
396 new_jh->b_transaction = NULL;
397 new_bh->b_size = jh2bh(jh_in)->b_size;
398 new_bh->b_bdev = transaction->t_journal->j_dev;
399 new_bh->b_blocknr = blocknr;
400 set_buffer_mapped(new_bh);
401 set_buffer_dirty(new_bh);
402
403 *jh_out = new_jh;
404
405 /*
406 * The to-be-written buffer needs to get moved to the io queue,
407 * and the original buffer whose contents we are shadowing or
408 * copying is moved to the transaction's shadow queue.
409 */
410 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700411 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700412 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700413 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700414
415 return do_escape | (done_copy_out << 1);
416}
417
418/*
419 * Allocation code for the journal file. Manage the space left in the
420 * journal, so that we can begin checkpointing when appropriate.
421 */
422
423/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700424 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700425 *
426 * Called with the journal already locked.
427 *
428 * Called under j_state_lock
429 */
430
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700431int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700432{
433 int left = journal->j_free;
434
435 assert_spin_locked(&journal->j_state_lock);
436
437 /*
438 * Be pessimistic here about the number of those free blocks which
439 * might be required for log descriptor control blocks.
440 */
441
442#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
443
444 left -= MIN_LOG_RESERVED_BLOCKS;
445
446 if (left <= 0)
447 return 0;
448 left -= (left >> 3);
449 return left;
450}
451
452/*
453 * Called under j_state_lock. Returns true if a transaction was started.
454 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700455int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456{
457 /*
458 * Are we already doing a recent enough commit?
459 */
460 if (!tid_geq(journal->j_commit_request, target)) {
461 /*
462 * We want a new commit: OK, mark the request and wakup the
463 * commit thread. We do _not_ do the commit ourselves.
464 */
465
466 journal->j_commit_request = target;
467 jbd_debug(1, "JBD: requesting commit %d/%d\n",
468 journal->j_commit_request,
469 journal->j_commit_sequence);
470 wake_up(&journal->j_wait_commit);
471 return 1;
472 }
473 return 0;
474}
475
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477{
478 int ret;
479
480 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700481 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700482 spin_unlock(&journal->j_state_lock);
483 return ret;
484}
485
486/*
487 * Force and wait upon a commit if the calling process is not within
488 * transaction. This is used for forcing out undo-protected data which contains
489 * bitmaps, when the fs is running out of space.
490 *
491 * We can only force the running transaction if we don't have an active handle;
492 * otherwise, we will deadlock.
493 *
494 * Returns true if a transaction was started.
495 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700496int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497{
498 transaction_t *transaction = NULL;
499 tid_t tid;
500
501 spin_lock(&journal->j_state_lock);
502 if (journal->j_running_transaction && !current->journal_info) {
503 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700504 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505 } else if (journal->j_committing_transaction)
506 transaction = journal->j_committing_transaction;
507
508 if (!transaction) {
509 spin_unlock(&journal->j_state_lock);
510 return 0; /* Nothing to retry */
511 }
512
513 tid = transaction->t_tid;
514 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700515 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516 return 1;
517}
518
519/*
520 * Start a commit of the current running transaction (if any). Returns true
521 * if a transaction was started, and fills its tid in at *ptid
522 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700523int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700524{
525 int ret = 0;
526
527 spin_lock(&journal->j_state_lock);
528 if (journal->j_running_transaction) {
529 tid_t tid = journal->j_running_transaction->t_tid;
530
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700531 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532 if (ret && ptid)
533 *ptid = tid;
534 } else if (journal->j_committing_transaction && ptid) {
535 /*
536 * If ext3_write_super() recently started a commit, then we
537 * have to wait for completion of that transaction
538 */
539 *ptid = journal->j_committing_transaction->t_tid;
540 ret = 1;
541 }
542 spin_unlock(&journal->j_state_lock);
543 return ret;
544}
545
546/*
547 * Wait for a specified commit to complete.
548 * The caller may not hold the journal lock.
549 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700550int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551{
552 int err = 0;
553
Jose R. Santose23291b2007-07-18 08:57:06 -0400554#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555 spin_lock(&journal->j_state_lock);
556 if (!tid_geq(journal->j_commit_request, tid)) {
557 printk(KERN_EMERG
558 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400559 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700560 }
561 spin_unlock(&journal->j_state_lock);
562#endif
563 spin_lock(&journal->j_state_lock);
564 while (tid_gt(tid, journal->j_commit_sequence)) {
565 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
566 tid, journal->j_commit_sequence);
567 wake_up(&journal->j_wait_commit);
568 spin_unlock(&journal->j_state_lock);
569 wait_event(journal->j_wait_done_commit,
570 !tid_gt(tid, journal->j_commit_sequence));
571 spin_lock(&journal->j_state_lock);
572 }
573 spin_unlock(&journal->j_state_lock);
574
575 if (unlikely(is_journal_aborted(journal))) {
576 printk(KERN_EMERG "journal commit I/O error\n");
577 err = -EIO;
578 }
579 return err;
580}
581
582/*
583 * Log buffer allocation routines:
584 */
585
Mingming Cao18eba7a2006-10-11 01:21:13 -0700586int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587{
588 unsigned long blocknr;
589
590 spin_lock(&journal->j_state_lock);
591 J_ASSERT(journal->j_free > 1);
592
593 blocknr = journal->j_head;
594 journal->j_head++;
595 journal->j_free--;
596 if (journal->j_head == journal->j_last)
597 journal->j_head = journal->j_first;
598 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700599 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700600}
601
602/*
603 * Conversion of logical to physical block numbers for the journal
604 *
605 * On external journals the journal blocks are identity-mapped, so
606 * this is a no-op. If needed, we can use j_blk_offset - everything is
607 * ready.
608 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700609int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700610 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700611{
612 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700613 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614
615 if (journal->j_inode) {
616 ret = bmap(journal->j_inode, blocknr);
617 if (ret)
618 *retp = ret;
619 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700620 printk(KERN_ALERT "%s: journal block not found "
621 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400622 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623 err = -EIO;
624 __journal_abort_soft(journal, err);
625 }
626 } else {
627 *retp = blocknr; /* +journal->j_blk_offset */
628 }
629 return err;
630}
631
632/*
633 * We play buffer_head aliasing tricks to write data/metadata blocks to
634 * the journal without copying their contents, but for journal
635 * descriptor blocks we do need to generate bona fide buffers.
636 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700637 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700638 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
639 * But we don't bother doing that, so there will be coherency problems with
640 * mmaps of blockdevs which hold live JBD-controlled filesystems.
641 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700642struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700643{
644 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700645 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646 int err;
647
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700648 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700649
650 if (err)
651 return NULL;
652
653 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500654 if (!bh)
655 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700656 lock_buffer(bh);
657 memset(bh->b_data, 0, journal->j_blocksize);
658 set_buffer_uptodate(bh);
659 unlock_buffer(bh);
660 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700661 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700662}
663
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500664struct jbd2_stats_proc_session {
665 journal_t *journal;
666 struct transaction_stats_s *stats;
667 int start;
668 int max;
669};
670
671static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
672 struct transaction_stats_s *ts,
673 int first)
674{
675 if (ts == s->stats + s->max)
676 ts = s->stats;
677 if (!first && ts == s->stats + s->start)
678 return NULL;
679 while (ts->ts_type == 0) {
680 ts++;
681 if (ts == s->stats + s->max)
682 ts = s->stats;
683 if (ts == s->stats + s->start)
684 return NULL;
685 }
686 return ts;
687
688}
689
690static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
691{
692 struct jbd2_stats_proc_session *s = seq->private;
693 struct transaction_stats_s *ts;
694 int l = *pos;
695
696 if (l == 0)
697 return SEQ_START_TOKEN;
698 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
699 if (!ts)
700 return NULL;
701 l--;
702 while (l) {
703 ts = jbd2_history_skip_empty(s, ++ts, 0);
704 if (!ts)
705 break;
706 l--;
707 }
708 return ts;
709}
710
711static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
712{
713 struct jbd2_stats_proc_session *s = seq->private;
714 struct transaction_stats_s *ts = v;
715
716 ++*pos;
717 if (v == SEQ_START_TOKEN)
718 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
719 else
720 return jbd2_history_skip_empty(s, ++ts, 0);
721}
722
723static int jbd2_seq_history_show(struct seq_file *seq, void *v)
724{
725 struct transaction_stats_s *ts = v;
726 if (v == SEQ_START_TOKEN) {
727 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
728 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
729 "wait", "run", "lock", "flush", "log", "hndls",
730 "block", "inlog", "ctime", "write", "drop",
731 "close");
732 return 0;
733 }
734 if (ts->ts_type == JBD2_STATS_RUN)
735 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
736 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
737 jiffies_to_msecs(ts->u.run.rs_wait),
738 jiffies_to_msecs(ts->u.run.rs_running),
739 jiffies_to_msecs(ts->u.run.rs_locked),
740 jiffies_to_msecs(ts->u.run.rs_flushing),
741 jiffies_to_msecs(ts->u.run.rs_logging),
742 ts->u.run.rs_handle_count,
743 ts->u.run.rs_blocks,
744 ts->u.run.rs_blocks_logged);
745 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
746 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
747 "C", ts->ts_tid, " ",
748 jiffies_to_msecs(ts->u.chp.cs_chp_time),
749 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
750 ts->u.chp.cs_forced_to_close);
751 else
752 J_ASSERT(0);
753 return 0;
754}
755
756static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
757{
758}
759
760static struct seq_operations jbd2_seq_history_ops = {
761 .start = jbd2_seq_history_start,
762 .next = jbd2_seq_history_next,
763 .stop = jbd2_seq_history_stop,
764 .show = jbd2_seq_history_show,
765};
766
767static int jbd2_seq_history_open(struct inode *inode, struct file *file)
768{
769 journal_t *journal = PDE(inode)->data;
770 struct jbd2_stats_proc_session *s;
771 int rc, size;
772
773 s = kmalloc(sizeof(*s), GFP_KERNEL);
774 if (s == NULL)
775 return -ENOMEM;
776 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
777 s->stats = kmalloc(size, GFP_KERNEL);
778 if (s->stats == NULL) {
779 kfree(s);
780 return -ENOMEM;
781 }
782 spin_lock(&journal->j_history_lock);
783 memcpy(s->stats, journal->j_history, size);
784 s->max = journal->j_history_max;
785 s->start = journal->j_history_cur % s->max;
786 spin_unlock(&journal->j_history_lock);
787
788 rc = seq_open(file, &jbd2_seq_history_ops);
789 if (rc == 0) {
790 struct seq_file *m = file->private_data;
791 m->private = s;
792 } else {
793 kfree(s->stats);
794 kfree(s);
795 }
796 return rc;
797
798}
799
800static int jbd2_seq_history_release(struct inode *inode, struct file *file)
801{
802 struct seq_file *seq = file->private_data;
803 struct jbd2_stats_proc_session *s = seq->private;
804
805 kfree(s->stats);
806 kfree(s);
807 return seq_release(inode, file);
808}
809
810static struct file_operations jbd2_seq_history_fops = {
811 .owner = THIS_MODULE,
812 .open = jbd2_seq_history_open,
813 .read = seq_read,
814 .llseek = seq_lseek,
815 .release = jbd2_seq_history_release,
816};
817
818static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
819{
820 return *pos ? NULL : SEQ_START_TOKEN;
821}
822
823static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
824{
825 return NULL;
826}
827
828static int jbd2_seq_info_show(struct seq_file *seq, void *v)
829{
830 struct jbd2_stats_proc_session *s = seq->private;
831
832 if (v != SEQ_START_TOKEN)
833 return 0;
834 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
835 s->stats->ts_tid,
836 s->journal->j_max_transaction_buffers);
837 if (s->stats->ts_tid == 0)
838 return 0;
839 seq_printf(seq, "average: \n %ums waiting for transaction\n",
840 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
841 seq_printf(seq, " %ums running transaction\n",
842 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
843 seq_printf(seq, " %ums transaction was being locked\n",
844 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
845 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
846 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
847 seq_printf(seq, " %ums logging transaction\n",
848 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500849 seq_printf(seq, " %lluus average transaction commit time\n",
850 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500851 seq_printf(seq, " %lu handles per transaction\n",
852 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
853 seq_printf(seq, " %lu blocks per transaction\n",
854 s->stats->u.run.rs_blocks / s->stats->ts_tid);
855 seq_printf(seq, " %lu logged blocks per transaction\n",
856 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
857 return 0;
858}
859
860static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
861{
862}
863
864static struct seq_operations jbd2_seq_info_ops = {
865 .start = jbd2_seq_info_start,
866 .next = jbd2_seq_info_next,
867 .stop = jbd2_seq_info_stop,
868 .show = jbd2_seq_info_show,
869};
870
871static int jbd2_seq_info_open(struct inode *inode, struct file *file)
872{
873 journal_t *journal = PDE(inode)->data;
874 struct jbd2_stats_proc_session *s;
875 int rc, size;
876
877 s = kmalloc(sizeof(*s), GFP_KERNEL);
878 if (s == NULL)
879 return -ENOMEM;
880 size = sizeof(struct transaction_stats_s);
881 s->stats = kmalloc(size, GFP_KERNEL);
882 if (s->stats == NULL) {
883 kfree(s);
884 return -ENOMEM;
885 }
886 spin_lock(&journal->j_history_lock);
887 memcpy(s->stats, &journal->j_stats, size);
888 s->journal = journal;
889 spin_unlock(&journal->j_history_lock);
890
891 rc = seq_open(file, &jbd2_seq_info_ops);
892 if (rc == 0) {
893 struct seq_file *m = file->private_data;
894 m->private = s;
895 } else {
896 kfree(s->stats);
897 kfree(s);
898 }
899 return rc;
900
901}
902
903static int jbd2_seq_info_release(struct inode *inode, struct file *file)
904{
905 struct seq_file *seq = file->private_data;
906 struct jbd2_stats_proc_session *s = seq->private;
907 kfree(s->stats);
908 kfree(s);
909 return seq_release(inode, file);
910}
911
912static struct file_operations jbd2_seq_info_fops = {
913 .owner = THIS_MODULE,
914 .open = jbd2_seq_info_open,
915 .read = seq_read,
916 .llseek = seq_lseek,
917 .release = jbd2_seq_info_release,
918};
919
920static struct proc_dir_entry *proc_jbd2_stats;
921
922static void jbd2_stats_proc_init(journal_t *journal)
923{
Theodore Ts'o05496762008-09-16 14:36:17 -0400924 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500925 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700926 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
927 &jbd2_seq_history_fops, journal);
928 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
929 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500930 }
931}
932
933static void jbd2_stats_proc_exit(journal_t *journal)
934{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500935 remove_proc_entry("info", journal->j_proc_entry);
936 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400937 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500938}
939
940static void journal_init_stats(journal_t *journal)
941{
942 int size;
943
944 if (!proc_jbd2_stats)
945 return;
946
947 journal->j_history_max = 100;
948 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
949 journal->j_history = kzalloc(size, GFP_KERNEL);
950 if (!journal->j_history) {
951 journal->j_history_max = 0;
952 return;
953 }
954 spin_lock_init(&journal->j_history_lock);
955}
956
Dave Kleikamp470decc2006-10-11 01:20:57 -0700957/*
958 * Management for journal control blocks: functions to create and
959 * destroy journal_t structures, and to initialise and read existing
960 * journal blocks from disk. */
961
962/* First: create and setup a journal_t object in memory. We initialise
963 * very few fields yet: that has to wait until we have created the
964 * journal structures from from scratch, or loaded them from disk. */
965
966static journal_t * journal_init_common (void)
967{
968 journal_t *journal;
969 int err;
970
Mingming Caod802ffa2007-10-16 18:38:25 -0400971 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700972 if (!journal)
973 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700974
975 init_waitqueue_head(&journal->j_wait_transaction_locked);
976 init_waitqueue_head(&journal->j_wait_logspace);
977 init_waitqueue_head(&journal->j_wait_done_commit);
978 init_waitqueue_head(&journal->j_wait_checkpoint);
979 init_waitqueue_head(&journal->j_wait_commit);
980 init_waitqueue_head(&journal->j_wait_updates);
981 mutex_init(&journal->j_barrier);
982 mutex_init(&journal->j_checkpoint_mutex);
983 spin_lock_init(&journal->j_revoke_lock);
984 spin_lock_init(&journal->j_list_lock);
985 spin_lock_init(&journal->j_state_lock);
986
Mingming Caocd02ff02007-10-16 18:38:25 -0400987 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500988 journal->j_min_batch_time = 0;
989 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700990
991 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700992 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700993
994 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700995 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700996 if (err) {
997 kfree(journal);
998 goto fail;
999 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001000
1001 journal_init_stats(journal);
1002
Dave Kleikamp470decc2006-10-11 01:20:57 -07001003 return journal;
1004fail:
1005 return NULL;
1006}
1007
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001008/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001009 *
1010 * Create a journal structure assigned some fixed set of disk blocks to
1011 * the journal. We don't actually touch those disk blocks yet, but we
1012 * need to set up all of the mapping information to tell the journaling
1013 * system where the journal blocks are.
1014 *
1015 */
1016
1017/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001018 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001019 * @bdev: Block device on which to create the journal
1020 * @fs_dev: Device which hold journalled filesystem for this journal.
1021 * @start: Block nr Start of journal.
1022 * @len: Length of the journal in blocks.
1023 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001024 *
1025 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001026 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001027 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001028 * range of blocks on an arbitrary block device.
1029 *
1030 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001031journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001032 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001033 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034{
1035 journal_t *journal = journal_init_common();
1036 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001037 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001038 int n;
1039
1040 if (!journal)
1041 return NULL;
1042
1043 /* journal descriptor can store up to n blocks -bzzz */
1044 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -05001045 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1047 journal->j_wbufsize = n;
1048 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1049 if (!journal->j_wbuf) {
1050 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001051 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001052 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001053 }
1054 journal->j_dev = bdev;
1055 journal->j_fs_dev = fs_dev;
1056 journal->j_blk_offset = start;
1057 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001058 bdevname(journal->j_dev, journal->j_devname);
1059 p = journal->j_devname;
1060 while ((p = strchr(p, '/')))
1061 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -07001062
1063 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001064 if (!bh) {
1065 printk(KERN_ERR
1066 "%s: Cannot get buffer for journal superblock\n",
1067 __func__);
1068 goto out_err;
1069 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070 journal->j_sb_buffer = bh;
1071 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001072
Dave Kleikamp470decc2006-10-11 01:20:57 -07001073 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001074out_err:
1075 jbd2_stats_proc_exit(journal);
1076 kfree(journal);
1077 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001078}
1079
1080/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001081 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001082 * @inode: An inode to create the journal in
1083 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001084 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001085 * the journal. The inode must exist already, must support bmap() and
1086 * must have all data blocks preallocated.
1087 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001088journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001089{
1090 struct buffer_head *bh;
1091 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001092 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001093 int err;
1094 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001095 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001096
1097 if (!journal)
1098 return NULL;
1099
1100 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1101 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001102 bdevname(journal->j_dev, journal->j_devname);
1103 p = journal->j_devname;
1104 while ((p = strchr(p, '/')))
1105 *p = '!';
1106 p = journal->j_devname + strlen(journal->j_devname);
1107 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001108 jbd_debug(1,
1109 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1110 journal, inode->i_sb->s_id, inode->i_ino,
1111 (long long) inode->i_size,
1112 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1113
1114 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1115 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001116 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117
1118 /* journal descriptor can store up to n blocks -bzzz */
1119 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1120 journal->j_wbufsize = n;
1121 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1122 if (!journal->j_wbuf) {
1123 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001124 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001125 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001126 }
1127
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001128 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001129 /* If that failed, give up */
1130 if (err) {
1131 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001132 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001133 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001134 }
1135
1136 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001137 if (!bh) {
1138 printk(KERN_ERR
1139 "%s: Cannot get buffer for journal superblock\n",
1140 __func__);
1141 goto out_err;
1142 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143 journal->j_sb_buffer = bh;
1144 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1145
1146 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001147out_err:
1148 jbd2_stats_proc_exit(journal);
1149 kfree(journal);
1150 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001151}
1152
1153/*
1154 * If the journal init or create aborts, we need to mark the journal
1155 * superblock as being NULL to prevent the journal destroy from writing
1156 * back a bogus superblock.
1157 */
1158static void journal_fail_superblock (journal_t *journal)
1159{
1160 struct buffer_head *bh = journal->j_sb_buffer;
1161 brelse(bh);
1162 journal->j_sb_buffer = NULL;
1163}
1164
1165/*
1166 * Given a journal_t structure, initialise the various fields for
1167 * startup of a new journaling session. We use this both when creating
1168 * a journal, and after recovering an old journal to reset it for
1169 * subsequent use.
1170 */
1171
1172static int journal_reset(journal_t *journal)
1173{
1174 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001175 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001176
1177 first = be32_to_cpu(sb->s_first);
1178 last = be32_to_cpu(sb->s_maxlen);
1179
1180 journal->j_first = first;
1181 journal->j_last = last;
1182
1183 journal->j_head = first;
1184 journal->j_tail = first;
1185 journal->j_free = last - first;
1186
1187 journal->j_tail_sequence = journal->j_transaction_sequence;
1188 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1189 journal->j_commit_request = journal->j_commit_sequence;
1190
1191 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1192
1193 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001194 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001195 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001196}
1197
1198/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001199 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001200 * @journal: The journal to update.
1201 * @wait: Set to '0' if you don't want to wait for IO completion.
1202 *
1203 * Update a journal's dynamic superblock fields and write it to disk,
1204 * optionally waiting for the IO to complete.
1205 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001206void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001207{
1208 journal_superblock_t *sb = journal->j_superblock;
1209 struct buffer_head *bh = journal->j_sb_buffer;
1210
1211 /*
1212 * As a special case, if the on-disk copy is already marked as needing
1213 * no recovery (s_start == 0) and there are no outstanding transactions
1214 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001215 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001216 * attempting a write to a potential-readonly device.
1217 */
1218 if (sb->s_start == 0 && journal->j_tail_sequence ==
1219 journal->j_transaction_sequence) {
1220 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1221 "(start %ld, seq %d, errno %d)\n",
1222 journal->j_tail, journal->j_tail_sequence,
1223 journal->j_errno);
1224 goto out;
1225 }
1226
Theodore Ts'o914258b2008-10-06 21:35:40 -04001227 if (buffer_write_io_error(bh)) {
1228 /*
1229 * Oh, dear. A previous attempt to write the journal
1230 * superblock failed. This could happen because the
1231 * USB device was yanked out. Or it could happen to
1232 * be a transient write error and maybe the block will
1233 * be remapped. Nothing we can do but to retry the
1234 * write and hope for the best.
1235 */
1236 printk(KERN_ERR "JBD2: previous I/O error detected "
1237 "for journal superblock update for %s.\n",
1238 journal->j_devname);
1239 clear_buffer_write_io_error(bh);
1240 set_buffer_uptodate(bh);
1241 }
1242
Dave Kleikamp470decc2006-10-11 01:20:57 -07001243 spin_lock(&journal->j_state_lock);
1244 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1245 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1246
1247 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1248 sb->s_start = cpu_to_be32(journal->j_tail);
1249 sb->s_errno = cpu_to_be32(journal->j_errno);
1250 spin_unlock(&journal->j_state_lock);
1251
1252 BUFFER_TRACE(bh, "marking dirty");
1253 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001254 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001255 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001256 if (buffer_write_io_error(bh)) {
1257 printk(KERN_ERR "JBD2: I/O error detected "
1258 "when updating journal superblock for %s.\n",
1259 journal->j_devname);
1260 clear_buffer_write_io_error(bh);
1261 set_buffer_uptodate(bh);
1262 }
1263 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001264 ll_rw_block(SWRITE, 1, &bh);
1265
1266out:
1267 /* If we have just flushed the log (by marking s_start==0), then
1268 * any future commit will have to be careful to update the
1269 * superblock again to re-record the true start of the log. */
1270
1271 spin_lock(&journal->j_state_lock);
1272 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001273 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001274 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001275 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001276 spin_unlock(&journal->j_state_lock);
1277}
1278
1279/*
1280 * Read the superblock for a given journal, performing initial
1281 * validation of the format.
1282 */
1283
1284static int journal_get_superblock(journal_t *journal)
1285{
1286 struct buffer_head *bh;
1287 journal_superblock_t *sb;
1288 int err = -EIO;
1289
1290 bh = journal->j_sb_buffer;
1291
1292 J_ASSERT(bh != NULL);
1293 if (!buffer_uptodate(bh)) {
1294 ll_rw_block(READ, 1, &bh);
1295 wait_on_buffer(bh);
1296 if (!buffer_uptodate(bh)) {
1297 printk (KERN_ERR
1298 "JBD: IO error reading journal superblock\n");
1299 goto out;
1300 }
1301 }
1302
1303 sb = journal->j_superblock;
1304
1305 err = -EINVAL;
1306
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001307 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001308 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1309 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1310 goto out;
1311 }
1312
1313 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001314 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001315 journal->j_format_version = 1;
1316 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001317 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001318 journal->j_format_version = 2;
1319 break;
1320 default:
1321 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1322 goto out;
1323 }
1324
1325 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1326 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1327 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1328 printk (KERN_WARNING "JBD: journal file too short\n");
1329 goto out;
1330 }
1331
1332 return 0;
1333
1334out:
1335 journal_fail_superblock(journal);
1336 return err;
1337}
1338
1339/*
1340 * Load the on-disk journal superblock and read the key fields into the
1341 * journal_t.
1342 */
1343
1344static int load_superblock(journal_t *journal)
1345{
1346 int err;
1347 journal_superblock_t *sb;
1348
1349 err = journal_get_superblock(journal);
1350 if (err)
1351 return err;
1352
1353 sb = journal->j_superblock;
1354
1355 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1356 journal->j_tail = be32_to_cpu(sb->s_start);
1357 journal->j_first = be32_to_cpu(sb->s_first);
1358 journal->j_last = be32_to_cpu(sb->s_maxlen);
1359 journal->j_errno = be32_to_cpu(sb->s_errno);
1360
1361 return 0;
1362}
1363
1364
1365/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001366 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001367 * @journal: Journal to act on.
1368 *
1369 * Given a journal_t structure which tells us which disk blocks contain
1370 * a journal, read the journal from disk to initialise the in-memory
1371 * structures.
1372 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001373int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001374{
1375 int err;
1376 journal_superblock_t *sb;
1377
1378 err = load_superblock(journal);
1379 if (err)
1380 return err;
1381
1382 sb = journal->j_superblock;
1383 /* If this is a V2 superblock, then we have to check the
1384 * features flags on it. */
1385
1386 if (journal->j_format_version >= 2) {
1387 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001388 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001389 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001390 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391 printk (KERN_WARNING
1392 "JBD: Unrecognised features on journal\n");
1393 return -EINVAL;
1394 }
1395 }
1396
Dave Kleikamp470decc2006-10-11 01:20:57 -07001397 /* Let the recovery code check whether it needs to recover any
1398 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001399 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 goto recovery_error;
1401
1402 /* OK, we've finished with the dynamic journal bits:
1403 * reinitialise the dynamic contents of the superblock in memory
1404 * and reset them on disk. */
1405 if (journal_reset(journal))
1406 goto recovery_error;
1407
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001408 journal->j_flags &= ~JBD2_ABORT;
1409 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001410 return 0;
1411
1412recovery_error:
1413 printk (KERN_WARNING "JBD: recovery failed\n");
1414 return -EIO;
1415}
1416
1417/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001418 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001419 * @journal: Journal to act on.
1420 *
1421 * Release a journal_t structure once it is no longer in use by the
1422 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001423 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001424 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001425int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001426{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001427 int err = 0;
1428
Dave Kleikamp470decc2006-10-11 01:20:57 -07001429 /* Wait for the commit thread to wake up and die. */
1430 journal_kill_thread(journal);
1431
1432 /* Force a final log commit */
1433 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001434 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435
1436 /* Force any old transactions to disk */
1437
1438 /* Totally anal locking here... */
1439 spin_lock(&journal->j_list_lock);
1440 while (journal->j_checkpoint_transactions != NULL) {
1441 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001442 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001443 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001444 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001445 spin_lock(&journal->j_list_lock);
1446 }
1447
1448 J_ASSERT(journal->j_running_transaction == NULL);
1449 J_ASSERT(journal->j_committing_transaction == NULL);
1450 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1451 spin_unlock(&journal->j_list_lock);
1452
Dave Kleikamp470decc2006-10-11 01:20:57 -07001453 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001454 if (!is_journal_aborted(journal)) {
1455 /* We can now mark the journal as empty. */
1456 journal->j_tail = 0;
1457 journal->j_tail_sequence =
1458 ++journal->j_transaction_sequence;
1459 jbd2_journal_update_superblock(journal, 1);
1460 } else {
1461 err = -EIO;
1462 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001463 brelse(journal->j_sb_buffer);
1464 }
1465
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001466 if (journal->j_proc_entry)
1467 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001468 if (journal->j_inode)
1469 iput(journal->j_inode);
1470 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001471 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 kfree(journal->j_wbuf);
1473 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001474
1475 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001476}
1477
1478
1479/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001480 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481 * @journal: Journal to check.
1482 * @compat: bitmask of compatible features
1483 * @ro: bitmask of features that force read-only mount
1484 * @incompat: bitmask of incompatible features
1485 *
1486 * Check whether the journal uses all of a given set of
1487 * features. Return true (non-zero) if it does.
1488 **/
1489
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001490int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001491 unsigned long ro, unsigned long incompat)
1492{
1493 journal_superblock_t *sb;
1494
1495 if (!compat && !ro && !incompat)
1496 return 1;
1497 if (journal->j_format_version == 1)
1498 return 0;
1499
1500 sb = journal->j_superblock;
1501
1502 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1503 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1504 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1505 return 1;
1506
1507 return 0;
1508}
1509
1510/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001511 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001512 * @journal: Journal to check.
1513 * @compat: bitmask of compatible features
1514 * @ro: bitmask of features that force read-only mount
1515 * @incompat: bitmask of incompatible features
1516 *
1517 * Check whether the journaling code supports the use of
1518 * all of a given set of features on this journal. Return true
1519 * (non-zero) if it can. */
1520
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001521int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001522 unsigned long ro, unsigned long incompat)
1523{
1524 journal_superblock_t *sb;
1525
1526 if (!compat && !ro && !incompat)
1527 return 1;
1528
1529 sb = journal->j_superblock;
1530
1531 /* We can support any known requested features iff the
1532 * superblock is in version 2. Otherwise we fail to support any
1533 * extended sb features. */
1534
1535 if (journal->j_format_version != 2)
1536 return 0;
1537
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001538 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1539 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1540 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001541 return 1;
1542
1543 return 0;
1544}
1545
1546/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001547 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001548 * @journal: Journal to act on.
1549 * @compat: bitmask of compatible features
1550 * @ro: bitmask of features that force read-only mount
1551 * @incompat: bitmask of incompatible features
1552 *
1553 * Mark a given journal feature as present on the
1554 * superblock. Returns true if the requested features could be set.
1555 *
1556 */
1557
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001558int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559 unsigned long ro, unsigned long incompat)
1560{
1561 journal_superblock_t *sb;
1562
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001563 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 return 1;
1565
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001566 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001567 return 0;
1568
1569 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1570 compat, ro, incompat);
1571
1572 sb = journal->j_superblock;
1573
1574 sb->s_feature_compat |= cpu_to_be32(compat);
1575 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1576 sb->s_feature_incompat |= cpu_to_be32(incompat);
1577
1578 return 1;
1579}
1580
Girish Shilamkar818d2762008-01-28 23:58:27 -05001581/*
1582 * jbd2_journal_clear_features () - Clear a given journal feature in the
1583 * superblock
1584 * @journal: Journal to act on.
1585 * @compat: bitmask of compatible features
1586 * @ro: bitmask of features that force read-only mount
1587 * @incompat: bitmask of incompatible features
1588 *
1589 * Clear a given journal feature as present on the
1590 * superblock.
1591 */
1592void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1593 unsigned long ro, unsigned long incompat)
1594{
1595 journal_superblock_t *sb;
1596
1597 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1598 compat, ro, incompat);
1599
1600 sb = journal->j_superblock;
1601
1602 sb->s_feature_compat &= ~cpu_to_be32(compat);
1603 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1604 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1605}
1606EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001607
1608/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001609 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001610 * @journal: Journal to act on.
1611 *
1612 * Given an initialised but unloaded journal struct, poke about in the
1613 * on-disk structure to update it to the most recent supported version.
1614 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001615int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001616{
1617 journal_superblock_t *sb;
1618 int err;
1619
1620 err = journal_get_superblock(journal);
1621 if (err)
1622 return err;
1623
1624 sb = journal->j_superblock;
1625
1626 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001627 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001628 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001629 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630 return journal_convert_superblock_v1(journal, sb);
1631 default:
1632 break;
1633 }
1634 return -EINVAL;
1635}
1636
1637static int journal_convert_superblock_v1(journal_t *journal,
1638 journal_superblock_t *sb)
1639{
1640 int offset, blocksize;
1641 struct buffer_head *bh;
1642
1643 printk(KERN_WARNING
1644 "JBD: Converting superblock from version 1 to 2.\n");
1645
1646 /* Pre-initialise new fields to zero */
1647 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1648 blocksize = be32_to_cpu(sb->s_blocksize);
1649 memset(&sb->s_feature_compat, 0, blocksize-offset);
1650
1651 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001652 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 journal->j_format_version = 2;
1654
1655 bh = journal->j_sb_buffer;
1656 BUFFER_TRACE(bh, "marking dirty");
1657 mark_buffer_dirty(bh);
1658 sync_dirty_buffer(bh);
1659 return 0;
1660}
1661
1662
1663/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001664 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 * @journal: Journal to act on.
1666 *
1667 * Flush all data for a given journal to disk and empty the journal.
1668 * Filesystems can use this when remounting readonly to ensure that
1669 * recovery does not need to happen on remount.
1670 */
1671
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001672int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001673{
1674 int err = 0;
1675 transaction_t *transaction = NULL;
1676 unsigned long old_tail;
1677
1678 spin_lock(&journal->j_state_lock);
1679
1680 /* Force everything buffered to the log... */
1681 if (journal->j_running_transaction) {
1682 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001683 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001684 } else if (journal->j_committing_transaction)
1685 transaction = journal->j_committing_transaction;
1686
1687 /* Wait for the log commit to complete... */
1688 if (transaction) {
1689 tid_t tid = transaction->t_tid;
1690
1691 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693 } else {
1694 spin_unlock(&journal->j_state_lock);
1695 }
1696
1697 /* ...and flush everything in the log out to disk. */
1698 spin_lock(&journal->j_list_lock);
1699 while (!err && journal->j_checkpoint_transactions != NULL) {
1700 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001701 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001702 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001703 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 spin_lock(&journal->j_list_lock);
1705 }
1706 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001707
1708 if (is_journal_aborted(journal))
1709 return -EIO;
1710
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001711 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001712
1713 /* Finally, mark the journal as really needing no recovery.
1714 * This sets s_start==0 in the underlying superblock, which is
1715 * the magic code for a fully-recovered superblock. Any future
1716 * commits of data to the journal will restore the current
1717 * s_start value. */
1718 spin_lock(&journal->j_state_lock);
1719 old_tail = journal->j_tail;
1720 journal->j_tail = 0;
1721 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001722 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001723 spin_lock(&journal->j_state_lock);
1724 journal->j_tail = old_tail;
1725
1726 J_ASSERT(!journal->j_running_transaction);
1727 J_ASSERT(!journal->j_committing_transaction);
1728 J_ASSERT(!journal->j_checkpoint_transactions);
1729 J_ASSERT(journal->j_head == journal->j_tail);
1730 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1731 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001732 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001733}
1734
1735/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001736 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001737 * @journal: Journal to act on.
1738 * @write: flag (see below)
1739 *
1740 * Wipe out all of the contents of a journal, safely. This will produce
1741 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001742 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001743 *
1744 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1745 * we merely suppress recovery.
1746 */
1747
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001748int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749{
1750 journal_superblock_t *sb;
1751 int err = 0;
1752
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001753 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754
1755 err = load_superblock(journal);
1756 if (err)
1757 return err;
1758
1759 sb = journal->j_superblock;
1760
1761 if (!journal->j_tail)
1762 goto no_recovery;
1763
1764 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1765 write ? "Clearing" : "Ignoring");
1766
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001767 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001768 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001769 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001770
1771 no_recovery:
1772 return err;
1773}
1774
1775/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776 * Journal abort has very specific semantics, which we describe
1777 * for journal abort.
1778 *
1779 * Two internal function, which provide abort to te jbd layer
1780 * itself are here.
1781 */
1782
1783/*
1784 * Quick version for internal journal use (doesn't lock the journal).
1785 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1786 * and don't attempt to make any other journal updates.
1787 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001788void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001789{
1790 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001792 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 return;
1794
1795 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001796 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001797
1798 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001799 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001800 transaction = journal->j_running_transaction;
1801 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001802 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001803 spin_unlock(&journal->j_state_lock);
1804}
1805
1806/* Soft abort: record the abort error status in the journal superblock,
1807 * but don't do any other IO. */
1808static void __journal_abort_soft (journal_t *journal, int errno)
1809{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001810 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 return;
1812
1813 if (!journal->j_errno)
1814 journal->j_errno = errno;
1815
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001816 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001817
1818 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001819 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820}
1821
1822/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001823 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001824 * @journal: the journal to shutdown.
1825 * @errno: an error number to record in the journal indicating
1826 * the reason for the shutdown.
1827 *
1828 * Perform a complete, immediate shutdown of the ENTIRE
1829 * journal (not of a single transaction). This operation cannot be
1830 * undone without closing and reopening the journal.
1831 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001832 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001833 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1834 * mode.
1835 *
1836 * Journal abort has very specific semantics. Any existing dirty,
1837 * unjournaled buffers in the main filesystem will still be written to
1838 * disk by bdflush, but the journaling mechanism will be suspended
1839 * immediately and no further transaction commits will be honoured.
1840 *
1841 * Any dirty, journaled buffers will be written back to disk without
1842 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1843 * filesystem, but we _do_ attempt to leave as much data as possible
1844 * behind for fsck to use for cleanup.
1845 *
1846 * Any attempt to get a new transaction handle on a journal which is in
1847 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001848 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849 * entered abort state during the update.
1850 *
1851 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001852 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001854 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855 * which will be recorded (if possible) in the journal superblock. This
1856 * allows a client to record failure conditions in the middle of a
1857 * transaction without having to complete the transaction to record the
1858 * failure to disk. ext3_error, for example, now uses this
1859 * functionality.
1860 *
1861 * Errors which originate from within the journaling layer will NOT
1862 * supply an errno; a null errno implies that absolutely no further
1863 * writes are done to the journal (unless there are any already in
1864 * progress).
1865 *
1866 */
1867
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001868void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001869{
1870 __journal_abort_soft(journal, errno);
1871}
1872
1873/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001874 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001875 * @journal: journal to examine.
1876 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001877 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001878 * time the journal was mounted - if the journal was stopped
1879 * without calling abort this will be 0.
1880 *
1881 * If the journal has been aborted on this mount time -EROFS will
1882 * be returned.
1883 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001884int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001885{
1886 int err;
1887
1888 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001889 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890 err = -EROFS;
1891 else
1892 err = journal->j_errno;
1893 spin_unlock(&journal->j_state_lock);
1894 return err;
1895}
1896
1897/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001898 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899 * @journal: journal to act on.
1900 *
1901 * An error must be cleared or Acked to take a FS out of readonly
1902 * mode.
1903 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001904int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001905{
1906 int err = 0;
1907
1908 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 err = -EROFS;
1911 else
1912 journal->j_errno = 0;
1913 spin_unlock(&journal->j_state_lock);
1914 return err;
1915}
1916
1917/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 * @journal: journal to act on.
1920 *
1921 * An error must be cleared or Acked to take a FS out of readonly
1922 * mode.
1923 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001924void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925{
1926 spin_lock(&journal->j_state_lock);
1927 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001928 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001929 spin_unlock(&journal->j_state_lock);
1930}
1931
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001932int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001933{
1934 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1935}
1936
1937/*
Zach Brownb517bea2006-10-11 01:21:08 -07001938 * helper functions to deal with 32 or 64bit block numbers.
1939 */
1940size_t journal_tag_bytes(journal_t *journal)
1941{
1942 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001943 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001944 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001945 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001946}
1947
1948/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001949 * Journal_head storage management
1950 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001951static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001952#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001953static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1954#endif
1955
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001956static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957{
1958 int retval;
1959
Al Viro1076d172008-03-29 03:07:18 +00001960 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001961 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962 sizeof(struct journal_head),
1963 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001964 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001965 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001966 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001967 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968 retval = -ENOMEM;
1969 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1970 }
1971 return retval;
1972}
1973
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001974static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001975{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001976 if (jbd2_journal_head_cache) {
1977 kmem_cache_destroy(jbd2_journal_head_cache);
1978 jbd2_journal_head_cache = NULL;
1979 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001980}
1981
1982/*
1983 * journal_head splicing and dicing
1984 */
1985static struct journal_head *journal_alloc_journal_head(void)
1986{
1987 struct journal_head *ret;
1988 static unsigned long last_warning;
1989
Jose R. Santose23291b2007-07-18 08:57:06 -04001990#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991 atomic_inc(&nr_journal_heads);
1992#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001993 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001994 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001995 jbd_debug(1, "out of memory for journal_head\n");
1996 if (time_after(jiffies, last_warning + 5*HZ)) {
1997 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001998 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 last_warning = jiffies;
2000 }
Al Viro1076d172008-03-29 03:07:18 +00002001 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002002 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002003 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 }
2005 }
2006 return ret;
2007}
2008
2009static void journal_free_journal_head(struct journal_head *jh)
2010{
Jose R. Santose23291b2007-07-18 08:57:06 -04002011#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002013 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002014#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002015 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016}
2017
2018/*
2019 * A journal_head is attached to a buffer_head whenever JBD has an
2020 * interest in the buffer.
2021 *
2022 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2023 * is set. This bit is tested in core kernel code where we need to take
2024 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2025 * there.
2026 *
2027 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2028 *
2029 * When a buffer has its BH_JBD bit set it is immune from being released by
2030 * core kernel code, mainly via ->b_count.
2031 *
2032 * A journal_head may be detached from its buffer_head when the journal_head's
2033 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002034 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002035 * journal_head can be dropped if needed.
2036 *
2037 * Various places in the kernel want to attach a journal_head to a buffer_head
2038 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002039 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002040 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 *
2043 * So the typical usage would be:
2044 *
2045 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002046 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002047 * ...
2048 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002049 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002050 *
2051 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2052 * because it has a non-zero b_transaction.
2053 */
2054
2055/*
2056 * Give a buffer_head a journal_head.
2057 *
2058 * Doesn't need the journal lock.
2059 * May sleep.
2060 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002061struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002062{
2063 struct journal_head *jh;
2064 struct journal_head *new_jh = NULL;
2065
2066repeat:
2067 if (!buffer_jbd(bh)) {
2068 new_jh = journal_alloc_journal_head();
2069 memset(new_jh, 0, sizeof(*new_jh));
2070 }
2071
2072 jbd_lock_bh_journal_head(bh);
2073 if (buffer_jbd(bh)) {
2074 jh = bh2jh(bh);
2075 } else {
2076 J_ASSERT_BH(bh,
2077 (atomic_read(&bh->b_count) > 0) ||
2078 (bh->b_page && bh->b_page->mapping));
2079
2080 if (!new_jh) {
2081 jbd_unlock_bh_journal_head(bh);
2082 goto repeat;
2083 }
2084
2085 jh = new_jh;
2086 new_jh = NULL; /* We consumed it */
2087 set_buffer_jbd(bh);
2088 bh->b_private = jh;
2089 jh->b_bh = bh;
2090 get_bh(bh);
2091 BUFFER_TRACE(bh, "added journal_head");
2092 }
2093 jh->b_jcount++;
2094 jbd_unlock_bh_journal_head(bh);
2095 if (new_jh)
2096 journal_free_journal_head(new_jh);
2097 return bh->b_private;
2098}
2099
2100/*
2101 * Grab a ref against this buffer_head's journal_head. If it ended up not
2102 * having a journal_head, return NULL
2103 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002104struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002105{
2106 struct journal_head *jh = NULL;
2107
2108 jbd_lock_bh_journal_head(bh);
2109 if (buffer_jbd(bh)) {
2110 jh = bh2jh(bh);
2111 jh->b_jcount++;
2112 }
2113 jbd_unlock_bh_journal_head(bh);
2114 return jh;
2115}
2116
2117static void __journal_remove_journal_head(struct buffer_head *bh)
2118{
2119 struct journal_head *jh = bh2jh(bh);
2120
2121 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2122
2123 get_bh(bh);
2124 if (jh->b_jcount == 0) {
2125 if (jh->b_transaction == NULL &&
2126 jh->b_next_transaction == NULL &&
2127 jh->b_cp_transaction == NULL) {
2128 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2129 J_ASSERT_BH(bh, buffer_jbd(bh));
2130 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2131 BUFFER_TRACE(bh, "remove journal_head");
2132 if (jh->b_frozen_data) {
2133 printk(KERN_WARNING "%s: freeing "
2134 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002135 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002136 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002137 }
2138 if (jh->b_committed_data) {
2139 printk(KERN_WARNING "%s: freeing "
2140 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002141 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002142 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002143 }
2144 bh->b_private = NULL;
2145 jh->b_bh = NULL; /* debug, really */
2146 clear_buffer_jbd(bh);
2147 __brelse(bh);
2148 journal_free_journal_head(jh);
2149 } else {
2150 BUFFER_TRACE(bh, "journal_head was locked");
2151 }
2152 }
2153}
2154
2155/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002156 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002157 * and has a zero b_jcount then remove and release its journal_head. If we did
2158 * see that the buffer is not used by any transaction we also "logically"
2159 * decrement ->b_count.
2160 *
2161 * We in fact take an additional increment on ->b_count as a convenience,
2162 * because the caller usually wants to do additional things with the bh
2163 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002164 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002165 * time. Once the caller has run __brelse(), the buffer is eligible for
2166 * reaping by try_to_free_buffers().
2167 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002168void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002169{
2170 jbd_lock_bh_journal_head(bh);
2171 __journal_remove_journal_head(bh);
2172 jbd_unlock_bh_journal_head(bh);
2173}
2174
2175/*
2176 * Drop a reference on the passed journal_head. If it fell to zero then try to
2177 * release the journal_head from the buffer_head.
2178 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002179void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002180{
2181 struct buffer_head *bh = jh2bh(jh);
2182
2183 jbd_lock_bh_journal_head(bh);
2184 J_ASSERT_JH(jh, jh->b_jcount > 0);
2185 --jh->b_jcount;
2186 if (!jh->b_jcount && !jh->b_transaction) {
2187 __journal_remove_journal_head(bh);
2188 __brelse(bh);
2189 }
2190 jbd_unlock_bh_journal_head(bh);
2191}
2192
2193/*
Jan Karac851ed52008-07-11 19:27:31 -04002194 * Initialize jbd inode head
2195 */
2196void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2197{
2198 jinode->i_transaction = NULL;
2199 jinode->i_next_transaction = NULL;
2200 jinode->i_vfs_inode = inode;
2201 jinode->i_flags = 0;
2202 INIT_LIST_HEAD(&jinode->i_list);
2203}
2204
2205/*
2206 * Function to be called before we start removing inode from memory (i.e.,
2207 * clear_inode() is a fine place to be called from). It removes inode from
2208 * transaction's lists.
2209 */
2210void jbd2_journal_release_jbd_inode(journal_t *journal,
2211 struct jbd2_inode *jinode)
2212{
2213 int writeout = 0;
2214
2215 if (!journal)
2216 return;
2217restart:
2218 spin_lock(&journal->j_list_lock);
2219 /* Is commit writing out inode - we have to wait */
2220 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2221 wait_queue_head_t *wq;
2222 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2223 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2224 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2225 spin_unlock(&journal->j_list_lock);
2226 schedule();
2227 finish_wait(wq, &wait.wait);
2228 goto restart;
2229 }
2230
2231 /* Do we need to wait for data writeback? */
2232 if (journal->j_committing_transaction == jinode->i_transaction)
2233 writeout = 1;
2234 if (jinode->i_transaction) {
2235 list_del(&jinode->i_list);
2236 jinode->i_transaction = NULL;
2237 }
2238 spin_unlock(&journal->j_list_lock);
2239}
2240
2241/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002242 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002243 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002244#ifdef CONFIG_JBD2_DEBUG
2245u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002246EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002247
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002248#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002249
Jose R. Santos6f38c742007-10-16 18:38:25 -04002250static struct dentry *jbd2_debugfs_dir;
2251static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002252
2253static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002255 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2256 if (jbd2_debugfs_dir)
2257 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2258 jbd2_debugfs_dir,
2259 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002260}
2261
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002262static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002263{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002264 debugfs_remove(jbd2_debug);
2265 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002266}
2267
2268#else
2269
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002270static void __init jbd2_create_debugfs_entry(void)
2271{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002272}
2273
2274static void __exit jbd2_remove_debugfs_entry(void)
2275{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002276}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002277
2278#endif
2279
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002280#ifdef CONFIG_PROC_FS
2281
2282#define JBD2_STATS_PROC_NAME "fs/jbd2"
2283
2284static void __init jbd2_create_jbd_stats_proc_entry(void)
2285{
2286 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2287}
2288
2289static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2290{
2291 if (proc_jbd2_stats)
2292 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2293}
2294
2295#else
2296
2297#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2298#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2299
2300#endif
2301
Christoph Lametere18b8902006-12-06 20:33:20 -08002302struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002303
2304static int __init journal_init_handle_cache(void)
2305{
Johann Lombardia920e942006-10-11 01:21:00 -07002306 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002307 sizeof(handle_t),
2308 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002309 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002310 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002311 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002312 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2313 return -ENOMEM;
2314 }
2315 return 0;
2316}
2317
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002318static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002319{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002320 if (jbd2_handle_cache)
2321 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002322}
2323
2324/*
2325 * Module startup and shutdown
2326 */
2327
2328static int __init journal_init_caches(void)
2329{
2330 int ret;
2331
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002332 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002333 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002334 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002335 if (ret == 0)
2336 ret = journal_init_handle_cache();
2337 return ret;
2338}
2339
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002342 jbd2_journal_destroy_revoke_caches();
2343 jbd2_journal_destroy_jbd2_journal_head_cache();
2344 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002345}
2346
2347static int __init journal_init(void)
2348{
2349 int ret;
2350
2351 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2352
2353 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002354 if (ret == 0) {
2355 jbd2_create_debugfs_entry();
2356 jbd2_create_jbd_stats_proc_entry();
2357 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002358 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002359 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360 return ret;
2361}
2362
2363static void __exit journal_exit(void)
2364{
Jose R. Santose23291b2007-07-18 08:57:06 -04002365#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002366 int n = atomic_read(&nr_journal_heads);
2367 if (n)
2368 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2369#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002370 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002371 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002372 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002373}
2374
2375MODULE_LICENSE("GPL");
2376module_init(journal_init);
2377module_exit(journal_exit);
2378