blob: f6bff9d6f8df8193e6fc13f1a2b1763124d29cb6 [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>
Dave Kleikamp470decc2006-10-11 01:20:57 -070040
41#include <asm/uaccess.h>
42#include <asm/page.h>
43
Mingming Caof7f4bcc2006-10-11 01:20:59 -070044EXPORT_SYMBOL(jbd2_journal_start);
45EXPORT_SYMBOL(jbd2_journal_restart);
46EXPORT_SYMBOL(jbd2_journal_extend);
47EXPORT_SYMBOL(jbd2_journal_stop);
48EXPORT_SYMBOL(jbd2_journal_lock_updates);
49EXPORT_SYMBOL(jbd2_journal_unlock_updates);
50EXPORT_SYMBOL(jbd2_journal_get_write_access);
51EXPORT_SYMBOL(jbd2_journal_get_create_access);
52EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070053EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070054EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
55EXPORT_SYMBOL(jbd2_journal_release_buffer);
56EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070057#if 0
58EXPORT_SYMBOL(journal_sync_buffer);
59#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070060EXPORT_SYMBOL(jbd2_journal_flush);
61EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_init_dev);
64EXPORT_SYMBOL(jbd2_journal_init_inode);
65EXPORT_SYMBOL(jbd2_journal_update_format);
66EXPORT_SYMBOL(jbd2_journal_check_used_features);
67EXPORT_SYMBOL(jbd2_journal_check_available_features);
68EXPORT_SYMBOL(jbd2_journal_set_features);
69EXPORT_SYMBOL(jbd2_journal_create);
70EXPORT_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
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700135 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700136 journal->j_commit_interval / HZ);
137
138 /*
139 * And now, wait forever for commit wakeup events.
140 */
141 spin_lock(&journal->j_state_lock);
142
143loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700144 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700145 goto end_loop;
146
147 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
148 journal->j_commit_sequence, journal->j_commit_request);
149
150 if (journal->j_commit_sequence != journal->j_commit_request) {
151 jbd_debug(1, "OK, requests differ\n");
152 spin_unlock(&journal->j_state_lock);
153 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700154 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700155 spin_lock(&journal->j_state_lock);
156 goto loop;
157 }
158
159 wake_up(&journal->j_wait_done_commit);
160 if (freezing(current)) {
161 /*
162 * The simpler the better. Flushing journal isn't a
163 * good idea, because that depends on threads that may
164 * be already stopped.
165 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700166 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700167 spin_unlock(&journal->j_state_lock);
168 refrigerator();
169 spin_lock(&journal->j_state_lock);
170 } else {
171 /*
172 * We assume on resume that commits are already there,
173 * so we don't sleep
174 */
175 DEFINE_WAIT(wait);
176 int should_sleep = 1;
177
178 prepare_to_wait(&journal->j_wait_commit, &wait,
179 TASK_INTERRUPTIBLE);
180 if (journal->j_commit_sequence != journal->j_commit_request)
181 should_sleep = 0;
182 transaction = journal->j_running_transaction;
183 if (transaction && time_after_eq(jiffies,
184 transaction->t_expires))
185 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700186 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700187 should_sleep = 0;
188 if (should_sleep) {
189 spin_unlock(&journal->j_state_lock);
190 schedule();
191 spin_lock(&journal->j_state_lock);
192 }
193 finish_wait(&journal->j_wait_commit, &wait);
194 }
195
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700196 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197
198 /*
199 * Were we woken up by a commit wakeup event?
200 */
201 transaction = journal->j_running_transaction;
202 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
203 journal->j_commit_request = transaction->t_tid;
204 jbd_debug(1, "woke because of timeout\n");
205 }
206 goto loop;
207
208end_loop:
209 spin_unlock(&journal->j_state_lock);
210 del_timer_sync(&journal->j_commit_timer);
211 journal->j_task = NULL;
212 wake_up(&journal->j_wait_done_commit);
213 jbd_debug(1, "Journal thread exiting.\n");
214 return 0;
215}
216
Pavel Emelianov97f06782007-05-08 00:30:42 -0700217static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700218{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700219 struct task_struct *t;
220
221 t = kthread_run(kjournald2, journal, "kjournald2");
222 if (IS_ERR(t))
223 return PTR_ERR(t);
224
Al Viro1076d172008-03-29 03:07:18 +0000225 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700226 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700227}
228
229static void journal_kill_thread(journal_t *journal)
230{
231 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700232 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700233
234 while (journal->j_task) {
235 wake_up(&journal->j_wait_commit);
236 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000237 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238 spin_lock(&journal->j_state_lock);
239 }
240 spin_unlock(&journal->j_state_lock);
241}
242
243/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700244 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700245 *
246 * Writes a metadata buffer to a given disk block. The actual IO is not
247 * performed but a new buffer_head is constructed which labels the data
248 * to be written with the correct destination disk block.
249 *
250 * Any magic-number escaping which needs to be done will cause a
251 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700252 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700253 * magic number is only written to the log for descripter blocks. In
254 * this case, we copy the data and replace the first word with 0, and we
255 * return a result code which indicates that this buffer needs to be
256 * marked as an escaped buffer in the corresponding log descriptor
257 * block. The missing word can then be restored when the block is read
258 * during recovery.
259 *
260 * If the source buffer has already been modified by a new transaction
261 * since we took the last commit snapshot, we use the frozen copy of
262 * that data for IO. If we end up using the existing buffer_head's data
263 * for the write, then we *have* to lock the buffer to prevent anyone
264 * else from using and possibly modifying it while the IO is in
265 * progress.
266 *
267 * The function returns a pointer to the buffer_heads to be used for IO.
268 *
269 * We assume that the journal has already been locked in this function.
270 *
271 * Return value:
272 * <0: Error
273 * >=0: Finished OK
274 *
275 * On success:
276 * Bit 0 set == escape performed on the data
277 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
278 */
279
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700280int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700281 struct journal_head *jh_in,
282 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700283 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284{
285 int need_copy_out = 0;
286 int done_copy_out = 0;
287 int do_escape = 0;
288 char *mapped_data;
289 struct buffer_head *new_bh;
290 struct journal_head *new_jh;
291 struct page *new_page;
292 unsigned int new_offset;
293 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700294 struct jbd2_buffer_trigger_type *triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700295
296 /*
297 * The buffer really shouldn't be locked: only the current committing
298 * transaction is allowed to write it, so nobody else is allowed
299 * to do any IO.
300 *
301 * akpm: except if we're journalling data, and write() output is
302 * also part of a shared mapping, and another thread has
303 * decided to launch a writepage() against this buffer.
304 */
305 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
306
307 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
308
309 /*
310 * If a new transaction has already done a buffer copy-out, then
311 * we use that version of the data for the commit.
312 */
313 jbd_lock_bh_state(bh_in);
314repeat:
315 if (jh_in->b_frozen_data) {
316 done_copy_out = 1;
317 new_page = virt_to_page(jh_in->b_frozen_data);
318 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700319 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700320 } else {
321 new_page = jh2bh(jh_in)->b_page;
322 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700323 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700324 }
325
326 mapped_data = kmap_atomic(new_page, KM_USER0);
327 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700328 * Fire any commit trigger. Do this before checking for escaping,
329 * as the trigger may modify the magic offset. If a copy-out
330 * happens afterwards, it will have the correct data in the buffer.
331 */
332 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
333 triggers);
334
335 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700336 * Check for escaping
337 */
338 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700339 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700340 need_copy_out = 1;
341 do_escape = 1;
342 }
343 kunmap_atomic(mapped_data, KM_USER0);
344
345 /*
346 * Do we need to do a data copy?
347 */
348 if (need_copy_out && !done_copy_out) {
349 char *tmp;
350
351 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400352 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700353 jbd_lock_bh_state(bh_in);
354 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400355 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700356 goto repeat;
357 }
358
359 jh_in->b_frozen_data = tmp;
360 mapped_data = kmap_atomic(new_page, KM_USER0);
361 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
362 kunmap_atomic(mapped_data, KM_USER0);
363
364 new_page = virt_to_page(tmp);
365 new_offset = offset_in_page(tmp);
366 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700367
368 /*
369 * This isn't strictly necessary, as we're using frozen
370 * data for the escaping, but it keeps consistency with
371 * b_frozen_data usage.
372 */
373 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700374 }
375
376 /*
377 * Did we need to do an escaping? Now we've done all the
378 * copying, we can finally do so.
379 */
380 if (do_escape) {
381 mapped_data = kmap_atomic(new_page, KM_USER0);
382 *((unsigned int *)(mapped_data + new_offset)) = 0;
383 kunmap_atomic(mapped_data, KM_USER0);
384 }
385
386 /* keep subsequent assertions sane */
387 new_bh->b_state = 0;
388 init_buffer(new_bh, NULL, NULL);
389 atomic_set(&new_bh->b_count, 1);
390 jbd_unlock_bh_state(bh_in);
391
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700392 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700393
394 set_bh_page(new_bh, new_page, new_offset);
395 new_jh->b_transaction = NULL;
396 new_bh->b_size = jh2bh(jh_in)->b_size;
397 new_bh->b_bdev = transaction->t_journal->j_dev;
398 new_bh->b_blocknr = blocknr;
399 set_buffer_mapped(new_bh);
400 set_buffer_dirty(new_bh);
401
402 *jh_out = new_jh;
403
404 /*
405 * The to-be-written buffer needs to get moved to the io queue,
406 * and the original buffer whose contents we are shadowing or
407 * copying is moved to the transaction's shadow queue.
408 */
409 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700410 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700411 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700412 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700413
414 return do_escape | (done_copy_out << 1);
415}
416
417/*
418 * Allocation code for the journal file. Manage the space left in the
419 * journal, so that we can begin checkpointing when appropriate.
420 */
421
422/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700423 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700424 *
425 * Called with the journal already locked.
426 *
427 * Called under j_state_lock
428 */
429
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700430int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700431{
432 int left = journal->j_free;
433
434 assert_spin_locked(&journal->j_state_lock);
435
436 /*
437 * Be pessimistic here about the number of those free blocks which
438 * might be required for log descriptor control blocks.
439 */
440
441#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
442
443 left -= MIN_LOG_RESERVED_BLOCKS;
444
445 if (left <= 0)
446 return 0;
447 left -= (left >> 3);
448 return left;
449}
450
451/*
452 * Called under j_state_lock. Returns true if a transaction was started.
453 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700454int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700455{
456 /*
457 * Are we already doing a recent enough commit?
458 */
459 if (!tid_geq(journal->j_commit_request, target)) {
460 /*
461 * We want a new commit: OK, mark the request and wakup the
462 * commit thread. We do _not_ do the commit ourselves.
463 */
464
465 journal->j_commit_request = target;
466 jbd_debug(1, "JBD: requesting commit %d/%d\n",
467 journal->j_commit_request,
468 journal->j_commit_sequence);
469 wake_up(&journal->j_wait_commit);
470 return 1;
471 }
472 return 0;
473}
474
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700475int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700476{
477 int ret;
478
479 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700480 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700481 spin_unlock(&journal->j_state_lock);
482 return ret;
483}
484
485/*
486 * Force and wait upon a commit if the calling process is not within
487 * transaction. This is used for forcing out undo-protected data which contains
488 * bitmaps, when the fs is running out of space.
489 *
490 * We can only force the running transaction if we don't have an active handle;
491 * otherwise, we will deadlock.
492 *
493 * Returns true if a transaction was started.
494 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700495int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496{
497 transaction_t *transaction = NULL;
498 tid_t tid;
499
500 spin_lock(&journal->j_state_lock);
501 if (journal->j_running_transaction && !current->journal_info) {
502 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504 } else if (journal->j_committing_transaction)
505 transaction = journal->j_committing_transaction;
506
507 if (!transaction) {
508 spin_unlock(&journal->j_state_lock);
509 return 0; /* Nothing to retry */
510 }
511
512 tid = transaction->t_tid;
513 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700514 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700515 return 1;
516}
517
518/*
519 * Start a commit of the current running transaction (if any). Returns true
520 * if a transaction was started, and fills its tid in at *ptid
521 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700522int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523{
524 int ret = 0;
525
526 spin_lock(&journal->j_state_lock);
527 if (journal->j_running_transaction) {
528 tid_t tid = journal->j_running_transaction->t_tid;
529
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700530 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531 if (ret && ptid)
532 *ptid = tid;
533 } else if (journal->j_committing_transaction && ptid) {
534 /*
535 * If ext3_write_super() recently started a commit, then we
536 * have to wait for completion of that transaction
537 */
538 *ptid = journal->j_committing_transaction->t_tid;
539 ret = 1;
540 }
541 spin_unlock(&journal->j_state_lock);
542 return ret;
543}
544
545/*
546 * Wait for a specified commit to complete.
547 * The caller may not hold the journal lock.
548 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700549int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700550{
551 int err = 0;
552
Jose R. Santose23291b2007-07-18 08:57:06 -0400553#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700554 spin_lock(&journal->j_state_lock);
555 if (!tid_geq(journal->j_commit_request, tid)) {
556 printk(KERN_EMERG
557 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400558 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559 }
560 spin_unlock(&journal->j_state_lock);
561#endif
562 spin_lock(&journal->j_state_lock);
563 while (tid_gt(tid, journal->j_commit_sequence)) {
564 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
565 tid, journal->j_commit_sequence);
566 wake_up(&journal->j_wait_commit);
567 spin_unlock(&journal->j_state_lock);
568 wait_event(journal->j_wait_done_commit,
569 !tid_gt(tid, journal->j_commit_sequence));
570 spin_lock(&journal->j_state_lock);
571 }
572 spin_unlock(&journal->j_state_lock);
573
574 if (unlikely(is_journal_aborted(journal))) {
575 printk(KERN_EMERG "journal commit I/O error\n");
576 err = -EIO;
577 }
578 return err;
579}
580
581/*
582 * Log buffer allocation routines:
583 */
584
Mingming Cao18eba7a2006-10-11 01:21:13 -0700585int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586{
587 unsigned long blocknr;
588
589 spin_lock(&journal->j_state_lock);
590 J_ASSERT(journal->j_free > 1);
591
592 blocknr = journal->j_head;
593 journal->j_head++;
594 journal->j_free--;
595 if (journal->j_head == journal->j_last)
596 journal->j_head = journal->j_first;
597 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700598 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700599}
600
601/*
602 * Conversion of logical to physical block numbers for the journal
603 *
604 * On external journals the journal blocks are identity-mapped, so
605 * this is a no-op. If needed, we can use j_blk_offset - everything is
606 * ready.
607 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700608int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700609 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700610{
611 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700612 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700613
614 if (journal->j_inode) {
615 ret = bmap(journal->j_inode, blocknr);
616 if (ret)
617 *retp = ret;
618 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619 printk(KERN_ALERT "%s: journal block not found "
620 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400621 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622 err = -EIO;
623 __journal_abort_soft(journal, err);
624 }
625 } else {
626 *retp = blocknr; /* +journal->j_blk_offset */
627 }
628 return err;
629}
630
631/*
632 * We play buffer_head aliasing tricks to write data/metadata blocks to
633 * the journal without copying their contents, but for journal
634 * descriptor blocks we do need to generate bona fide buffers.
635 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700636 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700637 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
638 * But we don't bother doing that, so there will be coherency problems with
639 * mmaps of blockdevs which hold live JBD-controlled filesystems.
640 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700641struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700642{
643 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700644 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645 int err;
646
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700647 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700648
649 if (err)
650 return NULL;
651
652 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
653 lock_buffer(bh);
654 memset(bh->b_data, 0, journal->j_blocksize);
655 set_buffer_uptodate(bh);
656 unlock_buffer(bh);
657 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700658 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700659}
660
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500661struct jbd2_stats_proc_session {
662 journal_t *journal;
663 struct transaction_stats_s *stats;
664 int start;
665 int max;
666};
667
668static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
669 struct transaction_stats_s *ts,
670 int first)
671{
672 if (ts == s->stats + s->max)
673 ts = s->stats;
674 if (!first && ts == s->stats + s->start)
675 return NULL;
676 while (ts->ts_type == 0) {
677 ts++;
678 if (ts == s->stats + s->max)
679 ts = s->stats;
680 if (ts == s->stats + s->start)
681 return NULL;
682 }
683 return ts;
684
685}
686
687static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
688{
689 struct jbd2_stats_proc_session *s = seq->private;
690 struct transaction_stats_s *ts;
691 int l = *pos;
692
693 if (l == 0)
694 return SEQ_START_TOKEN;
695 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
696 if (!ts)
697 return NULL;
698 l--;
699 while (l) {
700 ts = jbd2_history_skip_empty(s, ++ts, 0);
701 if (!ts)
702 break;
703 l--;
704 }
705 return ts;
706}
707
708static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
709{
710 struct jbd2_stats_proc_session *s = seq->private;
711 struct transaction_stats_s *ts = v;
712
713 ++*pos;
714 if (v == SEQ_START_TOKEN)
715 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
716 else
717 return jbd2_history_skip_empty(s, ++ts, 0);
718}
719
720static int jbd2_seq_history_show(struct seq_file *seq, void *v)
721{
722 struct transaction_stats_s *ts = v;
723 if (v == SEQ_START_TOKEN) {
724 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
725 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
726 "wait", "run", "lock", "flush", "log", "hndls",
727 "block", "inlog", "ctime", "write", "drop",
728 "close");
729 return 0;
730 }
731 if (ts->ts_type == JBD2_STATS_RUN)
732 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
733 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
734 jiffies_to_msecs(ts->u.run.rs_wait),
735 jiffies_to_msecs(ts->u.run.rs_running),
736 jiffies_to_msecs(ts->u.run.rs_locked),
737 jiffies_to_msecs(ts->u.run.rs_flushing),
738 jiffies_to_msecs(ts->u.run.rs_logging),
739 ts->u.run.rs_handle_count,
740 ts->u.run.rs_blocks,
741 ts->u.run.rs_blocks_logged);
742 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
743 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
744 "C", ts->ts_tid, " ",
745 jiffies_to_msecs(ts->u.chp.cs_chp_time),
746 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
747 ts->u.chp.cs_forced_to_close);
748 else
749 J_ASSERT(0);
750 return 0;
751}
752
753static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
754{
755}
756
757static struct seq_operations jbd2_seq_history_ops = {
758 .start = jbd2_seq_history_start,
759 .next = jbd2_seq_history_next,
760 .stop = jbd2_seq_history_stop,
761 .show = jbd2_seq_history_show,
762};
763
764static int jbd2_seq_history_open(struct inode *inode, struct file *file)
765{
766 journal_t *journal = PDE(inode)->data;
767 struct jbd2_stats_proc_session *s;
768 int rc, size;
769
770 s = kmalloc(sizeof(*s), GFP_KERNEL);
771 if (s == NULL)
772 return -ENOMEM;
773 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
774 s->stats = kmalloc(size, GFP_KERNEL);
775 if (s->stats == NULL) {
776 kfree(s);
777 return -ENOMEM;
778 }
779 spin_lock(&journal->j_history_lock);
780 memcpy(s->stats, journal->j_history, size);
781 s->max = journal->j_history_max;
782 s->start = journal->j_history_cur % s->max;
783 spin_unlock(&journal->j_history_lock);
784
785 rc = seq_open(file, &jbd2_seq_history_ops);
786 if (rc == 0) {
787 struct seq_file *m = file->private_data;
788 m->private = s;
789 } else {
790 kfree(s->stats);
791 kfree(s);
792 }
793 return rc;
794
795}
796
797static int jbd2_seq_history_release(struct inode *inode, struct file *file)
798{
799 struct seq_file *seq = file->private_data;
800 struct jbd2_stats_proc_session *s = seq->private;
801
802 kfree(s->stats);
803 kfree(s);
804 return seq_release(inode, file);
805}
806
807static struct file_operations jbd2_seq_history_fops = {
808 .owner = THIS_MODULE,
809 .open = jbd2_seq_history_open,
810 .read = seq_read,
811 .llseek = seq_lseek,
812 .release = jbd2_seq_history_release,
813};
814
815static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
816{
817 return *pos ? NULL : SEQ_START_TOKEN;
818}
819
820static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
821{
822 return NULL;
823}
824
825static int jbd2_seq_info_show(struct seq_file *seq, void *v)
826{
827 struct jbd2_stats_proc_session *s = seq->private;
828
829 if (v != SEQ_START_TOKEN)
830 return 0;
831 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
832 s->stats->ts_tid,
833 s->journal->j_max_transaction_buffers);
834 if (s->stats->ts_tid == 0)
835 return 0;
836 seq_printf(seq, "average: \n %ums waiting for transaction\n",
837 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
838 seq_printf(seq, " %ums running transaction\n",
839 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
840 seq_printf(seq, " %ums transaction was being locked\n",
841 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
842 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
843 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
844 seq_printf(seq, " %ums logging transaction\n",
845 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
846 seq_printf(seq, " %lu handles per transaction\n",
847 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
848 seq_printf(seq, " %lu blocks per transaction\n",
849 s->stats->u.run.rs_blocks / s->stats->ts_tid);
850 seq_printf(seq, " %lu logged blocks per transaction\n",
851 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
852 return 0;
853}
854
855static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
856{
857}
858
859static struct seq_operations jbd2_seq_info_ops = {
860 .start = jbd2_seq_info_start,
861 .next = jbd2_seq_info_next,
862 .stop = jbd2_seq_info_stop,
863 .show = jbd2_seq_info_show,
864};
865
866static int jbd2_seq_info_open(struct inode *inode, struct file *file)
867{
868 journal_t *journal = PDE(inode)->data;
869 struct jbd2_stats_proc_session *s;
870 int rc, size;
871
872 s = kmalloc(sizeof(*s), GFP_KERNEL);
873 if (s == NULL)
874 return -ENOMEM;
875 size = sizeof(struct transaction_stats_s);
876 s->stats = kmalloc(size, GFP_KERNEL);
877 if (s->stats == NULL) {
878 kfree(s);
879 return -ENOMEM;
880 }
881 spin_lock(&journal->j_history_lock);
882 memcpy(s->stats, &journal->j_stats, size);
883 s->journal = journal;
884 spin_unlock(&journal->j_history_lock);
885
886 rc = seq_open(file, &jbd2_seq_info_ops);
887 if (rc == 0) {
888 struct seq_file *m = file->private_data;
889 m->private = s;
890 } else {
891 kfree(s->stats);
892 kfree(s);
893 }
894 return rc;
895
896}
897
898static int jbd2_seq_info_release(struct inode *inode, struct file *file)
899{
900 struct seq_file *seq = file->private_data;
901 struct jbd2_stats_proc_session *s = seq->private;
902 kfree(s->stats);
903 kfree(s);
904 return seq_release(inode, file);
905}
906
907static struct file_operations jbd2_seq_info_fops = {
908 .owner = THIS_MODULE,
909 .open = jbd2_seq_info_open,
910 .read = seq_read,
911 .llseek = seq_lseek,
912 .release = jbd2_seq_info_release,
913};
914
915static struct proc_dir_entry *proc_jbd2_stats;
916
917static void jbd2_stats_proc_init(journal_t *journal)
918{
Theodore Ts'o05496762008-09-16 14:36:17 -0400919 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500920 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700921 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
922 &jbd2_seq_history_fops, journal);
923 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
924 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500925 }
926}
927
928static void jbd2_stats_proc_exit(journal_t *journal)
929{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500930 remove_proc_entry("info", journal->j_proc_entry);
931 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400932 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500933}
934
935static void journal_init_stats(journal_t *journal)
936{
937 int size;
938
939 if (!proc_jbd2_stats)
940 return;
941
942 journal->j_history_max = 100;
943 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
944 journal->j_history = kzalloc(size, GFP_KERNEL);
945 if (!journal->j_history) {
946 journal->j_history_max = 0;
947 return;
948 }
949 spin_lock_init(&journal->j_history_lock);
950}
951
Dave Kleikamp470decc2006-10-11 01:20:57 -0700952/*
953 * Management for journal control blocks: functions to create and
954 * destroy journal_t structures, and to initialise and read existing
955 * journal blocks from disk. */
956
957/* First: create and setup a journal_t object in memory. We initialise
958 * very few fields yet: that has to wait until we have created the
959 * journal structures from from scratch, or loaded them from disk. */
960
961static journal_t * journal_init_common (void)
962{
963 journal_t *journal;
964 int err;
965
Mingming Caod802ffa2007-10-16 18:38:25 -0400966 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700967 if (!journal)
968 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700969
970 init_waitqueue_head(&journal->j_wait_transaction_locked);
971 init_waitqueue_head(&journal->j_wait_logspace);
972 init_waitqueue_head(&journal->j_wait_done_commit);
973 init_waitqueue_head(&journal->j_wait_checkpoint);
974 init_waitqueue_head(&journal->j_wait_commit);
975 init_waitqueue_head(&journal->j_wait_updates);
976 mutex_init(&journal->j_barrier);
977 mutex_init(&journal->j_checkpoint_mutex);
978 spin_lock_init(&journal->j_revoke_lock);
979 spin_lock_init(&journal->j_list_lock);
980 spin_lock_init(&journal->j_state_lock);
981
Mingming Caocd02ff02007-10-16 18:38:25 -0400982 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700983
984 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700985 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700986
987 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700988 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700989 if (err) {
990 kfree(journal);
991 goto fail;
992 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500993
994 journal_init_stats(journal);
995
Dave Kleikamp470decc2006-10-11 01:20:57 -0700996 return journal;
997fail:
998 return NULL;
999}
1000
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001001/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002 *
1003 * Create a journal structure assigned some fixed set of disk blocks to
1004 * the journal. We don't actually touch those disk blocks yet, but we
1005 * need to set up all of the mapping information to tell the journaling
1006 * system where the journal blocks are.
1007 *
1008 */
1009
1010/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001011 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001012 * @bdev: Block device on which to create the journal
1013 * @fs_dev: Device which hold journalled filesystem for this journal.
1014 * @start: Block nr Start of journal.
1015 * @len: Length of the journal in blocks.
1016 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001017 *
1018 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001019 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001020 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001021 * range of blocks on an arbitrary block device.
1022 *
1023 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001024journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001025 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001026 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001027{
1028 journal_t *journal = journal_init_common();
1029 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001030 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001031 int n;
1032
1033 if (!journal)
1034 return NULL;
1035
1036 /* journal descriptor can store up to n blocks -bzzz */
1037 journal->j_blocksize = blocksize;
1038 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1039 journal->j_wbufsize = n;
1040 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1041 if (!journal->j_wbuf) {
1042 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001043 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001044 kfree(journal);
1045 journal = NULL;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001046 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001047 }
1048 journal->j_dev = bdev;
1049 journal->j_fs_dev = fs_dev;
1050 journal->j_blk_offset = start;
1051 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001052 bdevname(journal->j_dev, journal->j_devname);
1053 p = journal->j_devname;
1054 while ((p = strchr(p, '/')))
1055 *p = '!';
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001056 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001057
1058 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1059 J_ASSERT(bh != NULL);
1060 journal->j_sb_buffer = bh;
1061 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001062out:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063 return journal;
1064}
1065
1066/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001067 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001068 * @inode: An inode to create the journal in
1069 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001070 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001071 * the journal. The inode must exist already, must support bmap() and
1072 * must have all data blocks preallocated.
1073 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001074journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001075{
1076 struct buffer_head *bh;
1077 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001078 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079 int err;
1080 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001081 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001082
1083 if (!journal)
1084 return NULL;
1085
1086 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1087 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001088 bdevname(journal->j_dev, journal->j_devname);
1089 p = journal->j_devname;
1090 while ((p = strchr(p, '/')))
1091 *p = '!';
1092 p = journal->j_devname + strlen(journal->j_devname);
1093 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094 jbd_debug(1,
1095 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1096 journal, inode->i_sb->s_id, inode->i_ino,
1097 (long long) inode->i_size,
1098 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1099
1100 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1101 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001102 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001103
1104 /* journal descriptor can store up to n blocks -bzzz */
1105 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1106 journal->j_wbufsize = n;
1107 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1108 if (!journal->j_wbuf) {
1109 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001110 __func__);
Sami Liedes24238402008-11-02 19:23:30 -05001111 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001112 kfree(journal);
1113 return NULL;
1114 }
1115
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001116 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117 /* If that failed, give up */
1118 if (err) {
1119 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001120 __func__);
Sami Liedes24238402008-11-02 19:23:30 -05001121 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001122 kfree(journal);
1123 return NULL;
1124 }
1125
1126 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1127 J_ASSERT(bh != NULL);
1128 journal->j_sb_buffer = bh;
1129 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1130
1131 return journal;
1132}
1133
1134/*
1135 * If the journal init or create aborts, we need to mark the journal
1136 * superblock as being NULL to prevent the journal destroy from writing
1137 * back a bogus superblock.
1138 */
1139static void journal_fail_superblock (journal_t *journal)
1140{
1141 struct buffer_head *bh = journal->j_sb_buffer;
1142 brelse(bh);
1143 journal->j_sb_buffer = NULL;
1144}
1145
1146/*
1147 * Given a journal_t structure, initialise the various fields for
1148 * startup of a new journaling session. We use this both when creating
1149 * a journal, and after recovering an old journal to reset it for
1150 * subsequent use.
1151 */
1152
1153static int journal_reset(journal_t *journal)
1154{
1155 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001156 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001157
1158 first = be32_to_cpu(sb->s_first);
1159 last = be32_to_cpu(sb->s_maxlen);
1160
1161 journal->j_first = first;
1162 journal->j_last = last;
1163
1164 journal->j_head = first;
1165 journal->j_tail = first;
1166 journal->j_free = last - first;
1167
1168 journal->j_tail_sequence = journal->j_transaction_sequence;
1169 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1170 journal->j_commit_request = journal->j_commit_sequence;
1171
1172 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1173
1174 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001175 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001176 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001177}
1178
1179/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180 * int jbd2_journal_create() - Initialise the new journal file
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 * @journal: Journal to create. This structure must have been initialised
1182 *
1183 * Given a journal_t structure which tells us which disk blocks we can
1184 * use, create a new journal superblock and initialise all of the
1185 * journal fields from scratch.
1186 **/
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001187int jbd2_journal_create(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001188{
Mingming Cao18eba7a2006-10-11 01:21:13 -07001189 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001190 struct buffer_head *bh;
1191 journal_superblock_t *sb;
1192 int i, err;
1193
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001194 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001195 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1196 journal->j_maxlen);
1197 journal_fail_superblock(journal);
1198 return -EINVAL;
1199 }
1200
1201 if (journal->j_inode == NULL) {
1202 /*
1203 * We don't know what block to start at!
1204 */
1205 printk(KERN_EMERG
1206 "%s: creation of journal on external device!\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001207 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001208 BUG();
1209 }
1210
1211 /* Zero out the entire journal on disk. We cannot afford to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001212 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001213 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1214 for (i = 0; i < journal->j_maxlen; i++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001215 err = jbd2_journal_bmap(journal, i, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001216 if (err)
1217 return err;
1218 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1219 lock_buffer(bh);
1220 memset (bh->b_data, 0, journal->j_blocksize);
1221 BUFFER_TRACE(bh, "marking dirty");
1222 mark_buffer_dirty(bh);
1223 BUFFER_TRACE(bh, "marking uptodate");
1224 set_buffer_uptodate(bh);
1225 unlock_buffer(bh);
1226 __brelse(bh);
1227 }
1228
1229 sync_blockdev(journal->j_dev);
1230 jbd_debug(1, "JBD: journal cleared.\n");
1231
1232 /* OK, fill in the initial static fields in the new superblock */
1233 sb = journal->j_superblock;
1234
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001235 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1236 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001237
1238 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1239 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1240 sb->s_first = cpu_to_be32(1);
1241
1242 journal->j_transaction_sequence = 1;
1243
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001244 journal->j_flags &= ~JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001245 journal->j_format_version = 2;
1246
1247 return journal_reset(journal);
1248}
1249
1250/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001251 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001252 * @journal: The journal to update.
1253 * @wait: Set to '0' if you don't want to wait for IO completion.
1254 *
1255 * Update a journal's dynamic superblock fields and write it to disk,
1256 * optionally waiting for the IO to complete.
1257 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001258void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001259{
1260 journal_superblock_t *sb = journal->j_superblock;
1261 struct buffer_head *bh = journal->j_sb_buffer;
1262
1263 /*
1264 * As a special case, if the on-disk copy is already marked as needing
1265 * no recovery (s_start == 0) and there are no outstanding transactions
1266 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001267 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001268 * attempting a write to a potential-readonly device.
1269 */
1270 if (sb->s_start == 0 && journal->j_tail_sequence ==
1271 journal->j_transaction_sequence) {
1272 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1273 "(start %ld, seq %d, errno %d)\n",
1274 journal->j_tail, journal->j_tail_sequence,
1275 journal->j_errno);
1276 goto out;
1277 }
1278
Theodore Ts'o914258b2008-10-06 21:35:40 -04001279 if (buffer_write_io_error(bh)) {
1280 /*
1281 * Oh, dear. A previous attempt to write the journal
1282 * superblock failed. This could happen because the
1283 * USB device was yanked out. Or it could happen to
1284 * be a transient write error and maybe the block will
1285 * be remapped. Nothing we can do but to retry the
1286 * write and hope for the best.
1287 */
1288 printk(KERN_ERR "JBD2: previous I/O error detected "
1289 "for journal superblock update for %s.\n",
1290 journal->j_devname);
1291 clear_buffer_write_io_error(bh);
1292 set_buffer_uptodate(bh);
1293 }
1294
Dave Kleikamp470decc2006-10-11 01:20:57 -07001295 spin_lock(&journal->j_state_lock);
1296 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1297 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1298
1299 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1300 sb->s_start = cpu_to_be32(journal->j_tail);
1301 sb->s_errno = cpu_to_be32(journal->j_errno);
1302 spin_unlock(&journal->j_state_lock);
1303
1304 BUFFER_TRACE(bh, "marking dirty");
1305 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001306 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001307 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001308 if (buffer_write_io_error(bh)) {
1309 printk(KERN_ERR "JBD2: I/O error detected "
1310 "when updating journal superblock for %s.\n",
1311 journal->j_devname);
1312 clear_buffer_write_io_error(bh);
1313 set_buffer_uptodate(bh);
1314 }
1315 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001316 ll_rw_block(SWRITE, 1, &bh);
1317
1318out:
1319 /* If we have just flushed the log (by marking s_start==0), then
1320 * any future commit will have to be careful to update the
1321 * superblock again to re-record the true start of the log. */
1322
1323 spin_lock(&journal->j_state_lock);
1324 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001325 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001326 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001327 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001328 spin_unlock(&journal->j_state_lock);
1329}
1330
1331/*
1332 * Read the superblock for a given journal, performing initial
1333 * validation of the format.
1334 */
1335
1336static int journal_get_superblock(journal_t *journal)
1337{
1338 struct buffer_head *bh;
1339 journal_superblock_t *sb;
1340 int err = -EIO;
1341
1342 bh = journal->j_sb_buffer;
1343
1344 J_ASSERT(bh != NULL);
1345 if (!buffer_uptodate(bh)) {
1346 ll_rw_block(READ, 1, &bh);
1347 wait_on_buffer(bh);
1348 if (!buffer_uptodate(bh)) {
1349 printk (KERN_ERR
1350 "JBD: IO error reading journal superblock\n");
1351 goto out;
1352 }
1353 }
1354
1355 sb = journal->j_superblock;
1356
1357 err = -EINVAL;
1358
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001359 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001360 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1361 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1362 goto out;
1363 }
1364
1365 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001366 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001367 journal->j_format_version = 1;
1368 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001369 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 journal->j_format_version = 2;
1371 break;
1372 default:
1373 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1374 goto out;
1375 }
1376
1377 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1378 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1379 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1380 printk (KERN_WARNING "JBD: journal file too short\n");
1381 goto out;
1382 }
1383
1384 return 0;
1385
1386out:
1387 journal_fail_superblock(journal);
1388 return err;
1389}
1390
1391/*
1392 * Load the on-disk journal superblock and read the key fields into the
1393 * journal_t.
1394 */
1395
1396static int load_superblock(journal_t *journal)
1397{
1398 int err;
1399 journal_superblock_t *sb;
1400
1401 err = journal_get_superblock(journal);
1402 if (err)
1403 return err;
1404
1405 sb = journal->j_superblock;
1406
1407 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1408 journal->j_tail = be32_to_cpu(sb->s_start);
1409 journal->j_first = be32_to_cpu(sb->s_first);
1410 journal->j_last = be32_to_cpu(sb->s_maxlen);
1411 journal->j_errno = be32_to_cpu(sb->s_errno);
1412
1413 return 0;
1414}
1415
1416
1417/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001418 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001419 * @journal: Journal to act on.
1420 *
1421 * Given a journal_t structure which tells us which disk blocks contain
1422 * a journal, read the journal from disk to initialise the in-memory
1423 * structures.
1424 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001425int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001426{
1427 int err;
1428 journal_superblock_t *sb;
1429
1430 err = load_superblock(journal);
1431 if (err)
1432 return err;
1433
1434 sb = journal->j_superblock;
1435 /* If this is a V2 superblock, then we have to check the
1436 * features flags on it. */
1437
1438 if (journal->j_format_version >= 2) {
1439 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001440 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001442 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443 printk (KERN_WARNING
1444 "JBD: Unrecognised features on journal\n");
1445 return -EINVAL;
1446 }
1447 }
1448
Dave Kleikamp470decc2006-10-11 01:20:57 -07001449 /* Let the recovery code check whether it needs to recover any
1450 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001451 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001452 goto recovery_error;
1453
1454 /* OK, we've finished with the dynamic journal bits:
1455 * reinitialise the dynamic contents of the superblock in memory
1456 * and reset them on disk. */
1457 if (journal_reset(journal))
1458 goto recovery_error;
1459
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001460 journal->j_flags &= ~JBD2_ABORT;
1461 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 return 0;
1463
1464recovery_error:
1465 printk (KERN_WARNING "JBD: recovery failed\n");
1466 return -EIO;
1467}
1468
1469/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001470 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001471 * @journal: Journal to act on.
1472 *
1473 * Release a journal_t structure once it is no longer in use by the
1474 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001475 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001476 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001477int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001478{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001479 int err = 0;
1480
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481 /* Wait for the commit thread to wake up and die. */
1482 journal_kill_thread(journal);
1483
1484 /* Force a final log commit */
1485 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001486 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487
1488 /* Force any old transactions to disk */
1489
1490 /* Totally anal locking here... */
1491 spin_lock(&journal->j_list_lock);
1492 while (journal->j_checkpoint_transactions != NULL) {
1493 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001494 jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001495 spin_lock(&journal->j_list_lock);
1496 }
1497
1498 J_ASSERT(journal->j_running_transaction == NULL);
1499 J_ASSERT(journal->j_committing_transaction == NULL);
1500 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1501 spin_unlock(&journal->j_list_lock);
1502
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001504 if (!is_journal_aborted(journal)) {
1505 /* We can now mark the journal as empty. */
1506 journal->j_tail = 0;
1507 journal->j_tail_sequence =
1508 ++journal->j_transaction_sequence;
1509 jbd2_journal_update_superblock(journal, 1);
1510 } else {
1511 err = -EIO;
1512 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001513 brelse(journal->j_sb_buffer);
1514 }
1515
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001516 if (journal->j_proc_entry)
1517 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001518 if (journal->j_inode)
1519 iput(journal->j_inode);
1520 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001521 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001522 kfree(journal->j_wbuf);
1523 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001524
1525 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001526}
1527
1528
1529/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001530 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001531 * @journal: Journal to check.
1532 * @compat: bitmask of compatible features
1533 * @ro: bitmask of features that force read-only mount
1534 * @incompat: bitmask of incompatible features
1535 *
1536 * Check whether the journal uses all of a given set of
1537 * features. Return true (non-zero) if it does.
1538 **/
1539
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001540int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001541 unsigned long ro, unsigned long incompat)
1542{
1543 journal_superblock_t *sb;
1544
1545 if (!compat && !ro && !incompat)
1546 return 1;
1547 if (journal->j_format_version == 1)
1548 return 0;
1549
1550 sb = journal->j_superblock;
1551
1552 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1553 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1554 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1555 return 1;
1556
1557 return 0;
1558}
1559
1560/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001561 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001562 * @journal: Journal to check.
1563 * @compat: bitmask of compatible features
1564 * @ro: bitmask of features that force read-only mount
1565 * @incompat: bitmask of incompatible features
1566 *
1567 * Check whether the journaling code supports the use of
1568 * all of a given set of features on this journal. Return true
1569 * (non-zero) if it can. */
1570
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001571int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572 unsigned long ro, unsigned long incompat)
1573{
1574 journal_superblock_t *sb;
1575
1576 if (!compat && !ro && !incompat)
1577 return 1;
1578
1579 sb = journal->j_superblock;
1580
1581 /* We can support any known requested features iff the
1582 * superblock is in version 2. Otherwise we fail to support any
1583 * extended sb features. */
1584
1585 if (journal->j_format_version != 2)
1586 return 0;
1587
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001588 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1589 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1590 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001591 return 1;
1592
1593 return 0;
1594}
1595
1596/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001597 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001598 * @journal: Journal to act on.
1599 * @compat: bitmask of compatible features
1600 * @ro: bitmask of features that force read-only mount
1601 * @incompat: bitmask of incompatible features
1602 *
1603 * Mark a given journal feature as present on the
1604 * superblock. Returns true if the requested features could be set.
1605 *
1606 */
1607
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001608int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609 unsigned long ro, unsigned long incompat)
1610{
1611 journal_superblock_t *sb;
1612
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001613 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614 return 1;
1615
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001616 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617 return 0;
1618
1619 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1620 compat, ro, incompat);
1621
1622 sb = journal->j_superblock;
1623
1624 sb->s_feature_compat |= cpu_to_be32(compat);
1625 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1626 sb->s_feature_incompat |= cpu_to_be32(incompat);
1627
1628 return 1;
1629}
1630
Girish Shilamkar818d2762008-01-28 23:58:27 -05001631/*
1632 * jbd2_journal_clear_features () - Clear a given journal feature in the
1633 * superblock
1634 * @journal: Journal to act on.
1635 * @compat: bitmask of compatible features
1636 * @ro: bitmask of features that force read-only mount
1637 * @incompat: bitmask of incompatible features
1638 *
1639 * Clear a given journal feature as present on the
1640 * superblock.
1641 */
1642void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1643 unsigned long ro, unsigned long incompat)
1644{
1645 journal_superblock_t *sb;
1646
1647 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1648 compat, ro, incompat);
1649
1650 sb = journal->j_superblock;
1651
1652 sb->s_feature_compat &= ~cpu_to_be32(compat);
1653 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1654 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1655}
1656EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001657
1658/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001659 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001660 * @journal: Journal to act on.
1661 *
1662 * Given an initialised but unloaded journal struct, poke about in the
1663 * on-disk structure to update it to the most recent supported version.
1664 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001665int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666{
1667 journal_superblock_t *sb;
1668 int err;
1669
1670 err = journal_get_superblock(journal);
1671 if (err)
1672 return err;
1673
1674 sb = journal->j_superblock;
1675
1676 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001677 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001678 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680 return journal_convert_superblock_v1(journal, sb);
1681 default:
1682 break;
1683 }
1684 return -EINVAL;
1685}
1686
1687static int journal_convert_superblock_v1(journal_t *journal,
1688 journal_superblock_t *sb)
1689{
1690 int offset, blocksize;
1691 struct buffer_head *bh;
1692
1693 printk(KERN_WARNING
1694 "JBD: Converting superblock from version 1 to 2.\n");
1695
1696 /* Pre-initialise new fields to zero */
1697 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1698 blocksize = be32_to_cpu(sb->s_blocksize);
1699 memset(&sb->s_feature_compat, 0, blocksize-offset);
1700
1701 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001702 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001703 journal->j_format_version = 2;
1704
1705 bh = journal->j_sb_buffer;
1706 BUFFER_TRACE(bh, "marking dirty");
1707 mark_buffer_dirty(bh);
1708 sync_dirty_buffer(bh);
1709 return 0;
1710}
1711
1712
1713/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001714 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715 * @journal: Journal to act on.
1716 *
1717 * Flush all data for a given journal to disk and empty the journal.
1718 * Filesystems can use this when remounting readonly to ensure that
1719 * recovery does not need to happen on remount.
1720 */
1721
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001722int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001723{
1724 int err = 0;
1725 transaction_t *transaction = NULL;
1726 unsigned long old_tail;
1727
1728 spin_lock(&journal->j_state_lock);
1729
1730 /* Force everything buffered to the log... */
1731 if (journal->j_running_transaction) {
1732 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001733 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001734 } else if (journal->j_committing_transaction)
1735 transaction = journal->j_committing_transaction;
1736
1737 /* Wait for the log commit to complete... */
1738 if (transaction) {
1739 tid_t tid = transaction->t_tid;
1740
1741 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001742 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001743 } else {
1744 spin_unlock(&journal->j_state_lock);
1745 }
1746
1747 /* ...and flush everything in the log out to disk. */
1748 spin_lock(&journal->j_list_lock);
1749 while (!err && journal->j_checkpoint_transactions != NULL) {
1750 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001751 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001752 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001753 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754 spin_lock(&journal->j_list_lock);
1755 }
1756 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001757
1758 if (is_journal_aborted(journal))
1759 return -EIO;
1760
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001761 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001762
1763 /* Finally, mark the journal as really needing no recovery.
1764 * This sets s_start==0 in the underlying superblock, which is
1765 * the magic code for a fully-recovered superblock. Any future
1766 * commits of data to the journal will restore the current
1767 * s_start value. */
1768 spin_lock(&journal->j_state_lock);
1769 old_tail = journal->j_tail;
1770 journal->j_tail = 0;
1771 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001772 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001773 spin_lock(&journal->j_state_lock);
1774 journal->j_tail = old_tail;
1775
1776 J_ASSERT(!journal->j_running_transaction);
1777 J_ASSERT(!journal->j_committing_transaction);
1778 J_ASSERT(!journal->j_checkpoint_transactions);
1779 J_ASSERT(journal->j_head == journal->j_tail);
1780 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1781 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001782 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001783}
1784
1785/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001786 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787 * @journal: Journal to act on.
1788 * @write: flag (see below)
1789 *
1790 * Wipe out all of the contents of a journal, safely. This will produce
1791 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001792 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 *
1794 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1795 * we merely suppress recovery.
1796 */
1797
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001798int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001799{
1800 journal_superblock_t *sb;
1801 int err = 0;
1802
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001803 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804
1805 err = load_superblock(journal);
1806 if (err)
1807 return err;
1808
1809 sb = journal->j_superblock;
1810
1811 if (!journal->j_tail)
1812 goto no_recovery;
1813
1814 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1815 write ? "Clearing" : "Ignoring");
1816
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001817 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001818 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001819 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820
1821 no_recovery:
1822 return err;
1823}
1824
1825/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001826 * Journal abort has very specific semantics, which we describe
1827 * for journal abort.
1828 *
1829 * Two internal function, which provide abort to te jbd layer
1830 * itself are here.
1831 */
1832
1833/*
1834 * Quick version for internal journal use (doesn't lock the journal).
1835 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1836 * and don't attempt to make any other journal updates.
1837 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001838void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001839{
1840 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001842 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001843 return;
1844
1845 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001846 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001847
1848 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001849 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001850 transaction = journal->j_running_transaction;
1851 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001852 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853 spin_unlock(&journal->j_state_lock);
1854}
1855
1856/* Soft abort: record the abort error status in the journal superblock,
1857 * but don't do any other IO. */
1858static void __journal_abort_soft (journal_t *journal, int errno)
1859{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001860 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001861 return;
1862
1863 if (!journal->j_errno)
1864 journal->j_errno = errno;
1865
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001866 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001867
1868 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001869 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001870}
1871
1872/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001873 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001874 * @journal: the journal to shutdown.
1875 * @errno: an error number to record in the journal indicating
1876 * the reason for the shutdown.
1877 *
1878 * Perform a complete, immediate shutdown of the ENTIRE
1879 * journal (not of a single transaction). This operation cannot be
1880 * undone without closing and reopening the journal.
1881 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001882 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1884 * mode.
1885 *
1886 * Journal abort has very specific semantics. Any existing dirty,
1887 * unjournaled buffers in the main filesystem will still be written to
1888 * disk by bdflush, but the journaling mechanism will be suspended
1889 * immediately and no further transaction commits will be honoured.
1890 *
1891 * Any dirty, journaled buffers will be written back to disk without
1892 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1893 * filesystem, but we _do_ attempt to leave as much data as possible
1894 * behind for fsck to use for cleanup.
1895 *
1896 * Any attempt to get a new transaction handle on a journal which is in
1897 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001898 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899 * entered abort state during the update.
1900 *
1901 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001902 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001903 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001904 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001905 * which will be recorded (if possible) in the journal superblock. This
1906 * allows a client to record failure conditions in the middle of a
1907 * transaction without having to complete the transaction to record the
1908 * failure to disk. ext3_error, for example, now uses this
1909 * functionality.
1910 *
1911 * Errors which originate from within the journaling layer will NOT
1912 * supply an errno; a null errno implies that absolutely no further
1913 * writes are done to the journal (unless there are any already in
1914 * progress).
1915 *
1916 */
1917
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919{
1920 __journal_abort_soft(journal, errno);
1921}
1922
1923/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001924 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925 * @journal: journal to examine.
1926 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001927 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928 * time the journal was mounted - if the journal was stopped
1929 * without calling abort this will be 0.
1930 *
1931 * If the journal has been aborted on this mount time -EROFS will
1932 * be returned.
1933 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001934int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001935{
1936 int err;
1937
1938 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001939 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001940 err = -EROFS;
1941 else
1942 err = journal->j_errno;
1943 spin_unlock(&journal->j_state_lock);
1944 return err;
1945}
1946
1947/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001948 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001949 * @journal: journal to act on.
1950 *
1951 * An error must be cleared or Acked to take a FS out of readonly
1952 * mode.
1953 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001954int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955{
1956 int err = 0;
1957
1958 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001959 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 err = -EROFS;
1961 else
1962 journal->j_errno = 0;
1963 spin_unlock(&journal->j_state_lock);
1964 return err;
1965}
1966
1967/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001968 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001969 * @journal: journal to act on.
1970 *
1971 * An error must be cleared or Acked to take a FS out of readonly
1972 * mode.
1973 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001974void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001975{
1976 spin_lock(&journal->j_state_lock);
1977 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001978 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001979 spin_unlock(&journal->j_state_lock);
1980}
1981
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001982int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001983{
1984 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1985}
1986
1987/*
Zach Brownb517bea2006-10-11 01:21:08 -07001988 * helper functions to deal with 32 or 64bit block numbers.
1989 */
1990size_t journal_tag_bytes(journal_t *journal)
1991{
1992 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001993 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001994 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001995 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001996}
1997
1998/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 * Journal_head storage management
2000 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002001static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002002#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002003static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2004#endif
2005
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002006static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007{
2008 int retval;
2009
Al Viro1076d172008-03-29 03:07:18 +00002010 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002011 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 sizeof(struct journal_head),
2013 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002014 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002015 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002017 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002018 retval = -ENOMEM;
2019 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
2020 }
2021 return retval;
2022}
2023
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002024static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002025{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002026 if (jbd2_journal_head_cache) {
2027 kmem_cache_destroy(jbd2_journal_head_cache);
2028 jbd2_journal_head_cache = NULL;
2029 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002030}
2031
2032/*
2033 * journal_head splicing and dicing
2034 */
2035static struct journal_head *journal_alloc_journal_head(void)
2036{
2037 struct journal_head *ret;
2038 static unsigned long last_warning;
2039
Jose R. Santose23291b2007-07-18 08:57:06 -04002040#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002041 atomic_inc(&nr_journal_heads);
2042#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002043 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002044 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002045 jbd_debug(1, "out of memory for journal_head\n");
2046 if (time_after(jiffies, last_warning + 5*HZ)) {
2047 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002048 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049 last_warning = jiffies;
2050 }
Al Viro1076d172008-03-29 03:07:18 +00002051 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054 }
2055 }
2056 return ret;
2057}
2058
2059static void journal_free_journal_head(struct journal_head *jh)
2060{
Jose R. Santose23291b2007-07-18 08:57:06 -04002061#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002062 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002063 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002065 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002066}
2067
2068/*
2069 * A journal_head is attached to a buffer_head whenever JBD has an
2070 * interest in the buffer.
2071 *
2072 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2073 * is set. This bit is tested in core kernel code where we need to take
2074 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2075 * there.
2076 *
2077 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2078 *
2079 * When a buffer has its BH_JBD bit set it is immune from being released by
2080 * core kernel code, mainly via ->b_count.
2081 *
2082 * A journal_head may be detached from its buffer_head when the journal_head's
2083 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002084 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002085 * journal_head can be dropped if needed.
2086 *
2087 * Various places in the kernel want to attach a journal_head to a buffer_head
2088 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002089 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002090 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002091 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002092 *
2093 * So the typical usage would be:
2094 *
2095 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002096 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002097 * ...
2098 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002099 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100 *
2101 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2102 * because it has a non-zero b_transaction.
2103 */
2104
2105/*
2106 * Give a buffer_head a journal_head.
2107 *
2108 * Doesn't need the journal lock.
2109 * May sleep.
2110 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002111struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002112{
2113 struct journal_head *jh;
2114 struct journal_head *new_jh = NULL;
2115
2116repeat:
2117 if (!buffer_jbd(bh)) {
2118 new_jh = journal_alloc_journal_head();
2119 memset(new_jh, 0, sizeof(*new_jh));
2120 }
2121
2122 jbd_lock_bh_journal_head(bh);
2123 if (buffer_jbd(bh)) {
2124 jh = bh2jh(bh);
2125 } else {
2126 J_ASSERT_BH(bh,
2127 (atomic_read(&bh->b_count) > 0) ||
2128 (bh->b_page && bh->b_page->mapping));
2129
2130 if (!new_jh) {
2131 jbd_unlock_bh_journal_head(bh);
2132 goto repeat;
2133 }
2134
2135 jh = new_jh;
2136 new_jh = NULL; /* We consumed it */
2137 set_buffer_jbd(bh);
2138 bh->b_private = jh;
2139 jh->b_bh = bh;
2140 get_bh(bh);
2141 BUFFER_TRACE(bh, "added journal_head");
2142 }
2143 jh->b_jcount++;
2144 jbd_unlock_bh_journal_head(bh);
2145 if (new_jh)
2146 journal_free_journal_head(new_jh);
2147 return bh->b_private;
2148}
2149
2150/*
2151 * Grab a ref against this buffer_head's journal_head. If it ended up not
2152 * having a journal_head, return NULL
2153 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002154struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002155{
2156 struct journal_head *jh = NULL;
2157
2158 jbd_lock_bh_journal_head(bh);
2159 if (buffer_jbd(bh)) {
2160 jh = bh2jh(bh);
2161 jh->b_jcount++;
2162 }
2163 jbd_unlock_bh_journal_head(bh);
2164 return jh;
2165}
2166
2167static void __journal_remove_journal_head(struct buffer_head *bh)
2168{
2169 struct journal_head *jh = bh2jh(bh);
2170
2171 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2172
2173 get_bh(bh);
2174 if (jh->b_jcount == 0) {
2175 if (jh->b_transaction == NULL &&
2176 jh->b_next_transaction == NULL &&
2177 jh->b_cp_transaction == NULL) {
2178 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2179 J_ASSERT_BH(bh, buffer_jbd(bh));
2180 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2181 BUFFER_TRACE(bh, "remove journal_head");
2182 if (jh->b_frozen_data) {
2183 printk(KERN_WARNING "%s: freeing "
2184 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002185 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002186 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002187 }
2188 if (jh->b_committed_data) {
2189 printk(KERN_WARNING "%s: freeing "
2190 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002191 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002192 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002193 }
2194 bh->b_private = NULL;
2195 jh->b_bh = NULL; /* debug, really */
2196 clear_buffer_jbd(bh);
2197 __brelse(bh);
2198 journal_free_journal_head(jh);
2199 } else {
2200 BUFFER_TRACE(bh, "journal_head was locked");
2201 }
2202 }
2203}
2204
2205/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002206 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002207 * and has a zero b_jcount then remove and release its journal_head. If we did
2208 * see that the buffer is not used by any transaction we also "logically"
2209 * decrement ->b_count.
2210 *
2211 * We in fact take an additional increment on ->b_count as a convenience,
2212 * because the caller usually wants to do additional things with the bh
2213 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002214 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002215 * time. Once the caller has run __brelse(), the buffer is eligible for
2216 * reaping by try_to_free_buffers().
2217 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002218void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002219{
2220 jbd_lock_bh_journal_head(bh);
2221 __journal_remove_journal_head(bh);
2222 jbd_unlock_bh_journal_head(bh);
2223}
2224
2225/*
2226 * Drop a reference on the passed journal_head. If it fell to zero then try to
2227 * release the journal_head from the buffer_head.
2228 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002229void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002230{
2231 struct buffer_head *bh = jh2bh(jh);
2232
2233 jbd_lock_bh_journal_head(bh);
2234 J_ASSERT_JH(jh, jh->b_jcount > 0);
2235 --jh->b_jcount;
2236 if (!jh->b_jcount && !jh->b_transaction) {
2237 __journal_remove_journal_head(bh);
2238 __brelse(bh);
2239 }
2240 jbd_unlock_bh_journal_head(bh);
2241}
2242
2243/*
Jan Karac851ed52008-07-11 19:27:31 -04002244 * Initialize jbd inode head
2245 */
2246void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2247{
2248 jinode->i_transaction = NULL;
2249 jinode->i_next_transaction = NULL;
2250 jinode->i_vfs_inode = inode;
2251 jinode->i_flags = 0;
2252 INIT_LIST_HEAD(&jinode->i_list);
2253}
2254
2255/*
2256 * Function to be called before we start removing inode from memory (i.e.,
2257 * clear_inode() is a fine place to be called from). It removes inode from
2258 * transaction's lists.
2259 */
2260void jbd2_journal_release_jbd_inode(journal_t *journal,
2261 struct jbd2_inode *jinode)
2262{
2263 int writeout = 0;
2264
2265 if (!journal)
2266 return;
2267restart:
2268 spin_lock(&journal->j_list_lock);
2269 /* Is commit writing out inode - we have to wait */
2270 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2271 wait_queue_head_t *wq;
2272 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2273 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2274 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2275 spin_unlock(&journal->j_list_lock);
2276 schedule();
2277 finish_wait(wq, &wait.wait);
2278 goto restart;
2279 }
2280
2281 /* Do we need to wait for data writeback? */
2282 if (journal->j_committing_transaction == jinode->i_transaction)
2283 writeout = 1;
2284 if (jinode->i_transaction) {
2285 list_del(&jinode->i_list);
2286 jinode->i_transaction = NULL;
2287 }
2288 spin_unlock(&journal->j_list_lock);
2289}
2290
2291/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002292 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002293 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002294#ifdef CONFIG_JBD2_DEBUG
2295u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002296EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002297
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002298#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002299
Jose R. Santos6f38c742007-10-16 18:38:25 -04002300static struct dentry *jbd2_debugfs_dir;
2301static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002302
2303static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002304{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002305 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2306 if (jbd2_debugfs_dir)
2307 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2308 jbd2_debugfs_dir,
2309 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002310}
2311
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002312static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002313{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002314 debugfs_remove(jbd2_debug);
2315 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002316}
2317
2318#else
2319
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002320static void __init jbd2_create_debugfs_entry(void)
2321{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002322}
2323
2324static void __exit jbd2_remove_debugfs_entry(void)
2325{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002326}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002327
2328#endif
2329
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002330#ifdef CONFIG_PROC_FS
2331
2332#define JBD2_STATS_PROC_NAME "fs/jbd2"
2333
2334static void __init jbd2_create_jbd_stats_proc_entry(void)
2335{
2336 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2337}
2338
2339static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2340{
2341 if (proc_jbd2_stats)
2342 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2343}
2344
2345#else
2346
2347#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2348#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2349
2350#endif
2351
Christoph Lametere18b8902006-12-06 20:33:20 -08002352struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002353
2354static int __init journal_init_handle_cache(void)
2355{
Johann Lombardia920e942006-10-11 01:21:00 -07002356 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002357 sizeof(handle_t),
2358 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002359 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002360 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002361 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2363 return -ENOMEM;
2364 }
2365 return 0;
2366}
2367
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002368static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002369{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002370 if (jbd2_handle_cache)
2371 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002372}
2373
2374/*
2375 * Module startup and shutdown
2376 */
2377
2378static int __init journal_init_caches(void)
2379{
2380 int ret;
2381
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002382 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002383 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002384 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002385 if (ret == 0)
2386 ret = journal_init_handle_cache();
2387 return ret;
2388}
2389
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002390static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002391{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002392 jbd2_journal_destroy_revoke_caches();
2393 jbd2_journal_destroy_jbd2_journal_head_cache();
2394 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002395}
2396
2397static int __init journal_init(void)
2398{
2399 int ret;
2400
2401 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2402
2403 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002404 if (ret == 0) {
2405 jbd2_create_debugfs_entry();
2406 jbd2_create_jbd_stats_proc_entry();
2407 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002408 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002409 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002410 return ret;
2411}
2412
2413static void __exit journal_exit(void)
2414{
Jose R. Santose23291b2007-07-18 08:57:06 -04002415#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002416 int n = atomic_read(&nr_journal_heads);
2417 if (n)
2418 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2419#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002420 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002421 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002422 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002423}
2424
2425MODULE_LICENSE("GPL");
2426module_init(journal_init);
2427module_exit(journal_exit);
2428