blob: 783de118de9235583bda263db9c9ce665333c4c3 [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);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070053EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
54EXPORT_SYMBOL(jbd2_journal_release_buffer);
55EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070056#if 0
57EXPORT_SYMBOL(journal_sync_buffer);
58#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070059EXPORT_SYMBOL(jbd2_journal_flush);
60EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070061
Mingming Caof7f4bcc2006-10-11 01:20:59 -070062EXPORT_SYMBOL(jbd2_journal_init_dev);
63EXPORT_SYMBOL(jbd2_journal_init_inode);
64EXPORT_SYMBOL(jbd2_journal_update_format);
65EXPORT_SYMBOL(jbd2_journal_check_used_features);
66EXPORT_SYMBOL(jbd2_journal_check_available_features);
67EXPORT_SYMBOL(jbd2_journal_set_features);
68EXPORT_SYMBOL(jbd2_journal_create);
69EXPORT_SYMBOL(jbd2_journal_load);
70EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070071EXPORT_SYMBOL(jbd2_journal_abort);
72EXPORT_SYMBOL(jbd2_journal_errno);
73EXPORT_SYMBOL(jbd2_journal_ack_err);
74EXPORT_SYMBOL(jbd2_journal_clear_err);
75EXPORT_SYMBOL(jbd2_log_wait_commit);
76EXPORT_SYMBOL(jbd2_journal_start_commit);
77EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
78EXPORT_SYMBOL(jbd2_journal_wipe);
79EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
80EXPORT_SYMBOL(jbd2_journal_invalidatepage);
81EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
82EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040083EXPORT_SYMBOL(jbd2_journal_file_inode);
84EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
85EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
86EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070087
88static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
89static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070090
91/*
92 * Helper function used to manage commit timeouts
93 */
94
95static void commit_timeout(unsigned long __data)
96{
97 struct task_struct * p = (struct task_struct *) __data;
98
99 wake_up_process(p);
100}
101
102/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700103 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700104 * journal.
105 *
106 * This kernel thread is responsible for two things:
107 *
108 * 1) COMMIT: Every so often we need to commit the current state of the
109 * filesystem to disk. The journal thread is responsible for writing
110 * all of the metadata buffers to disk.
111 *
112 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
113 * of the data in that part of the log has been rewritten elsewhere on
114 * the disk. Flushing these old buffers to reclaim space in the log is
115 * known as checkpointing, and this thread is responsible for that job.
116 */
117
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700118static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700119{
120 journal_t *journal = arg;
121 transaction_t *transaction;
122
123 /*
124 * Set up an interval timer which can be used to trigger a commit wakeup
125 * after the commit interval expires
126 */
127 setup_timer(&journal->j_commit_timer, commit_timeout,
128 (unsigned long)current);
129
130 /* Record that the journal thread is running */
131 journal->j_task = current;
132 wake_up(&journal->j_wait_done_commit);
133
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700134 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700135 journal->j_commit_interval / HZ);
136
137 /*
138 * And now, wait forever for commit wakeup events.
139 */
140 spin_lock(&journal->j_state_lock);
141
142loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700143 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700144 goto end_loop;
145
146 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
147 journal->j_commit_sequence, journal->j_commit_request);
148
149 if (journal->j_commit_sequence != journal->j_commit_request) {
150 jbd_debug(1, "OK, requests differ\n");
151 spin_unlock(&journal->j_state_lock);
152 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700153 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700154 spin_lock(&journal->j_state_lock);
155 goto loop;
156 }
157
158 wake_up(&journal->j_wait_done_commit);
159 if (freezing(current)) {
160 /*
161 * The simpler the better. Flushing journal isn't a
162 * good idea, because that depends on threads that may
163 * be already stopped.
164 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700165 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700166 spin_unlock(&journal->j_state_lock);
167 refrigerator();
168 spin_lock(&journal->j_state_lock);
169 } else {
170 /*
171 * We assume on resume that commits are already there,
172 * so we don't sleep
173 */
174 DEFINE_WAIT(wait);
175 int should_sleep = 1;
176
177 prepare_to_wait(&journal->j_wait_commit, &wait,
178 TASK_INTERRUPTIBLE);
179 if (journal->j_commit_sequence != journal->j_commit_request)
180 should_sleep = 0;
181 transaction = journal->j_running_transaction;
182 if (transaction && time_after_eq(jiffies,
183 transaction->t_expires))
184 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700185 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700186 should_sleep = 0;
187 if (should_sleep) {
188 spin_unlock(&journal->j_state_lock);
189 schedule();
190 spin_lock(&journal->j_state_lock);
191 }
192 finish_wait(&journal->j_wait_commit, &wait);
193 }
194
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700195 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700196
197 /*
198 * Were we woken up by a commit wakeup event?
199 */
200 transaction = journal->j_running_transaction;
201 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
202 journal->j_commit_request = transaction->t_tid;
203 jbd_debug(1, "woke because of timeout\n");
204 }
205 goto loop;
206
207end_loop:
208 spin_unlock(&journal->j_state_lock);
209 del_timer_sync(&journal->j_commit_timer);
210 journal->j_task = NULL;
211 wake_up(&journal->j_wait_done_commit);
212 jbd_debug(1, "Journal thread exiting.\n");
213 return 0;
214}
215
Pavel Emelianov97f06782007-05-08 00:30:42 -0700216static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700217{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700218 struct task_struct *t;
219
220 t = kthread_run(kjournald2, journal, "kjournald2");
221 if (IS_ERR(t))
222 return PTR_ERR(t);
223
Al Viro1076d172008-03-29 03:07:18 +0000224 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700225 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700226}
227
228static void journal_kill_thread(journal_t *journal)
229{
230 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700231 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232
233 while (journal->j_task) {
234 wake_up(&journal->j_wait_commit);
235 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000236 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700237 spin_lock(&journal->j_state_lock);
238 }
239 spin_unlock(&journal->j_state_lock);
240}
241
242/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700243 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244 *
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
248 *
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700251 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
257 * during recovery.
258 *
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
264 * progress.
265 *
266 * The function returns a pointer to the buffer_heads to be used for IO.
267 *
268 * We assume that the journal has already been locked in this function.
269 *
270 * Return value:
271 * <0: Error
272 * >=0: Finished OK
273 *
274 * On success:
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
277 */
278
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700279int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700280 struct journal_head *jh_in,
281 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700282 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700283{
284 int need_copy_out = 0;
285 int done_copy_out = 0;
286 int do_escape = 0;
287 char *mapped_data;
288 struct buffer_head *new_bh;
289 struct journal_head *new_jh;
290 struct page *new_page;
291 unsigned int new_offset;
292 struct buffer_head *bh_in = jh2bh(jh_in);
293
294 /*
295 * The buffer really shouldn't be locked: only the current committing
296 * transaction is allowed to write it, so nobody else is allowed
297 * to do any IO.
298 *
299 * akpm: except if we're journalling data, and write() output is
300 * also part of a shared mapping, and another thread has
301 * decided to launch a writepage() against this buffer.
302 */
303 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
304
305 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
306
307 /*
308 * If a new transaction has already done a buffer copy-out, then
309 * we use that version of the data for the commit.
310 */
311 jbd_lock_bh_state(bh_in);
312repeat:
313 if (jh_in->b_frozen_data) {
314 done_copy_out = 1;
315 new_page = virt_to_page(jh_in->b_frozen_data);
316 new_offset = offset_in_page(jh_in->b_frozen_data);
317 } else {
318 new_page = jh2bh(jh_in)->b_page;
319 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
320 }
321
322 mapped_data = kmap_atomic(new_page, KM_USER0);
323 /*
324 * Check for escaping
325 */
326 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700327 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700328 need_copy_out = 1;
329 do_escape = 1;
330 }
331 kunmap_atomic(mapped_data, KM_USER0);
332
333 /*
334 * Do we need to do a data copy?
335 */
336 if (need_copy_out && !done_copy_out) {
337 char *tmp;
338
339 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400340 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 jbd_lock_bh_state(bh_in);
342 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400343 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700344 goto repeat;
345 }
346
347 jh_in->b_frozen_data = tmp;
348 mapped_data = kmap_atomic(new_page, KM_USER0);
349 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
350 kunmap_atomic(mapped_data, KM_USER0);
351
352 new_page = virt_to_page(tmp);
353 new_offset = offset_in_page(tmp);
354 done_copy_out = 1;
355 }
356
357 /*
358 * Did we need to do an escaping? Now we've done all the
359 * copying, we can finally do so.
360 */
361 if (do_escape) {
362 mapped_data = kmap_atomic(new_page, KM_USER0);
363 *((unsigned int *)(mapped_data + new_offset)) = 0;
364 kunmap_atomic(mapped_data, KM_USER0);
365 }
366
367 /* keep subsequent assertions sane */
368 new_bh->b_state = 0;
369 init_buffer(new_bh, NULL, NULL);
370 atomic_set(&new_bh->b_count, 1);
371 jbd_unlock_bh_state(bh_in);
372
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700373 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700374
375 set_bh_page(new_bh, new_page, new_offset);
376 new_jh->b_transaction = NULL;
377 new_bh->b_size = jh2bh(jh_in)->b_size;
378 new_bh->b_bdev = transaction->t_journal->j_dev;
379 new_bh->b_blocknr = blocknr;
380 set_buffer_mapped(new_bh);
381 set_buffer_dirty(new_bh);
382
383 *jh_out = new_jh;
384
385 /*
386 * The to-be-written buffer needs to get moved to the io queue,
387 * and the original buffer whose contents we are shadowing or
388 * copying is moved to the transaction's shadow queue.
389 */
390 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700391 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700392 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700393 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394
395 return do_escape | (done_copy_out << 1);
396}
397
398/*
399 * Allocation code for the journal file. Manage the space left in the
400 * journal, so that we can begin checkpointing when appropriate.
401 */
402
403/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700404 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700405 *
406 * Called with the journal already locked.
407 *
408 * Called under j_state_lock
409 */
410
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700411int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700412{
413 int left = journal->j_free;
414
415 assert_spin_locked(&journal->j_state_lock);
416
417 /*
418 * Be pessimistic here about the number of those free blocks which
419 * might be required for log descriptor control blocks.
420 */
421
422#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
423
424 left -= MIN_LOG_RESERVED_BLOCKS;
425
426 if (left <= 0)
427 return 0;
428 left -= (left >> 3);
429 return left;
430}
431
432/*
433 * Called under j_state_lock. Returns true if a transaction was started.
434 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700435int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700436{
437 /*
438 * Are we already doing a recent enough commit?
439 */
440 if (!tid_geq(journal->j_commit_request, target)) {
441 /*
442 * We want a new commit: OK, mark the request and wakup the
443 * commit thread. We do _not_ do the commit ourselves.
444 */
445
446 journal->j_commit_request = target;
447 jbd_debug(1, "JBD: requesting commit %d/%d\n",
448 journal->j_commit_request,
449 journal->j_commit_sequence);
450 wake_up(&journal->j_wait_commit);
451 return 1;
452 }
453 return 0;
454}
455
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700456int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457{
458 int ret;
459
460 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700461 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700462 spin_unlock(&journal->j_state_lock);
463 return ret;
464}
465
466/*
467 * Force and wait upon a commit if the calling process is not within
468 * transaction. This is used for forcing out undo-protected data which contains
469 * bitmaps, when the fs is running out of space.
470 *
471 * We can only force the running transaction if we don't have an active handle;
472 * otherwise, we will deadlock.
473 *
474 * Returns true if a transaction was started.
475 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477{
478 transaction_t *transaction = NULL;
479 tid_t tid;
480
481 spin_lock(&journal->j_state_lock);
482 if (journal->j_running_transaction && !current->journal_info) {
483 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700484 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485 } else if (journal->j_committing_transaction)
486 transaction = journal->j_committing_transaction;
487
488 if (!transaction) {
489 spin_unlock(&journal->j_state_lock);
490 return 0; /* Nothing to retry */
491 }
492
493 tid = transaction->t_tid;
494 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700495 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496 return 1;
497}
498
499/*
500 * Start a commit of the current running transaction (if any). Returns true
501 * if a transaction was started, and fills its tid in at *ptid
502 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504{
505 int ret = 0;
506
507 spin_lock(&journal->j_state_lock);
508 if (journal->j_running_transaction) {
509 tid_t tid = journal->j_running_transaction->t_tid;
510
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700511 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512 if (ret && ptid)
513 *ptid = tid;
514 } else if (journal->j_committing_transaction && ptid) {
515 /*
516 * If ext3_write_super() recently started a commit, then we
517 * have to wait for completion of that transaction
518 */
519 *ptid = journal->j_committing_transaction->t_tid;
520 ret = 1;
521 }
522 spin_unlock(&journal->j_state_lock);
523 return ret;
524}
525
526/*
527 * Wait for a specified commit to complete.
528 * The caller may not hold the journal lock.
529 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700530int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531{
532 int err = 0;
533
Jose R. Santose23291b2007-07-18 08:57:06 -0400534#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700535 spin_lock(&journal->j_state_lock);
536 if (!tid_geq(journal->j_commit_request, tid)) {
537 printk(KERN_EMERG
538 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400539 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540 }
541 spin_unlock(&journal->j_state_lock);
542#endif
543 spin_lock(&journal->j_state_lock);
544 while (tid_gt(tid, journal->j_commit_sequence)) {
545 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
546 tid, journal->j_commit_sequence);
547 wake_up(&journal->j_wait_commit);
548 spin_unlock(&journal->j_state_lock);
549 wait_event(journal->j_wait_done_commit,
550 !tid_gt(tid, journal->j_commit_sequence));
551 spin_lock(&journal->j_state_lock);
552 }
553 spin_unlock(&journal->j_state_lock);
554
555 if (unlikely(is_journal_aborted(journal))) {
556 printk(KERN_EMERG "journal commit I/O error\n");
557 err = -EIO;
558 }
559 return err;
560}
561
562/*
563 * Log buffer allocation routines:
564 */
565
Mingming Cao18eba7a2006-10-11 01:21:13 -0700566int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700567{
568 unsigned long blocknr;
569
570 spin_lock(&journal->j_state_lock);
571 J_ASSERT(journal->j_free > 1);
572
573 blocknr = journal->j_head;
574 journal->j_head++;
575 journal->j_free--;
576 if (journal->j_head == journal->j_last)
577 journal->j_head = journal->j_first;
578 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700579 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700580}
581
582/*
583 * Conversion of logical to physical block numbers for the journal
584 *
585 * On external journals the journal blocks are identity-mapped, so
586 * this is a no-op. If needed, we can use j_blk_offset - everything is
587 * ready.
588 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700589int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700590 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700591{
592 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700593 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594
595 if (journal->j_inode) {
596 ret = bmap(journal->j_inode, blocknr);
597 if (ret)
598 *retp = ret;
599 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700600 printk(KERN_ALERT "%s: journal block not found "
601 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400602 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700603 err = -EIO;
604 __journal_abort_soft(journal, err);
605 }
606 } else {
607 *retp = blocknr; /* +journal->j_blk_offset */
608 }
609 return err;
610}
611
612/*
613 * We play buffer_head aliasing tricks to write data/metadata blocks to
614 * the journal without copying their contents, but for journal
615 * descriptor blocks we do need to generate bona fide buffers.
616 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700617 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700618 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
619 * But we don't bother doing that, so there will be coherency problems with
620 * mmaps of blockdevs which hold live JBD-controlled filesystems.
621 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700622struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623{
624 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700625 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700626 int err;
627
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700628 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700629
630 if (err)
631 return NULL;
632
633 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
634 lock_buffer(bh);
635 memset(bh->b_data, 0, journal->j_blocksize);
636 set_buffer_uptodate(bh);
637 unlock_buffer(bh);
638 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700639 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700640}
641
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500642struct jbd2_stats_proc_session {
643 journal_t *journal;
644 struct transaction_stats_s *stats;
645 int start;
646 int max;
647};
648
649static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
650 struct transaction_stats_s *ts,
651 int first)
652{
653 if (ts == s->stats + s->max)
654 ts = s->stats;
655 if (!first && ts == s->stats + s->start)
656 return NULL;
657 while (ts->ts_type == 0) {
658 ts++;
659 if (ts == s->stats + s->max)
660 ts = s->stats;
661 if (ts == s->stats + s->start)
662 return NULL;
663 }
664 return ts;
665
666}
667
668static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
669{
670 struct jbd2_stats_proc_session *s = seq->private;
671 struct transaction_stats_s *ts;
672 int l = *pos;
673
674 if (l == 0)
675 return SEQ_START_TOKEN;
676 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
677 if (!ts)
678 return NULL;
679 l--;
680 while (l) {
681 ts = jbd2_history_skip_empty(s, ++ts, 0);
682 if (!ts)
683 break;
684 l--;
685 }
686 return ts;
687}
688
689static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
690{
691 struct jbd2_stats_proc_session *s = seq->private;
692 struct transaction_stats_s *ts = v;
693
694 ++*pos;
695 if (v == SEQ_START_TOKEN)
696 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
697 else
698 return jbd2_history_skip_empty(s, ++ts, 0);
699}
700
701static int jbd2_seq_history_show(struct seq_file *seq, void *v)
702{
703 struct transaction_stats_s *ts = v;
704 if (v == SEQ_START_TOKEN) {
705 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
706 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
707 "wait", "run", "lock", "flush", "log", "hndls",
708 "block", "inlog", "ctime", "write", "drop",
709 "close");
710 return 0;
711 }
712 if (ts->ts_type == JBD2_STATS_RUN)
713 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
714 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
715 jiffies_to_msecs(ts->u.run.rs_wait),
716 jiffies_to_msecs(ts->u.run.rs_running),
717 jiffies_to_msecs(ts->u.run.rs_locked),
718 jiffies_to_msecs(ts->u.run.rs_flushing),
719 jiffies_to_msecs(ts->u.run.rs_logging),
720 ts->u.run.rs_handle_count,
721 ts->u.run.rs_blocks,
722 ts->u.run.rs_blocks_logged);
723 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
724 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
725 "C", ts->ts_tid, " ",
726 jiffies_to_msecs(ts->u.chp.cs_chp_time),
727 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
728 ts->u.chp.cs_forced_to_close);
729 else
730 J_ASSERT(0);
731 return 0;
732}
733
734static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
735{
736}
737
738static struct seq_operations jbd2_seq_history_ops = {
739 .start = jbd2_seq_history_start,
740 .next = jbd2_seq_history_next,
741 .stop = jbd2_seq_history_stop,
742 .show = jbd2_seq_history_show,
743};
744
745static int jbd2_seq_history_open(struct inode *inode, struct file *file)
746{
747 journal_t *journal = PDE(inode)->data;
748 struct jbd2_stats_proc_session *s;
749 int rc, size;
750
751 s = kmalloc(sizeof(*s), GFP_KERNEL);
752 if (s == NULL)
753 return -ENOMEM;
754 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
755 s->stats = kmalloc(size, GFP_KERNEL);
756 if (s->stats == NULL) {
757 kfree(s);
758 return -ENOMEM;
759 }
760 spin_lock(&journal->j_history_lock);
761 memcpy(s->stats, journal->j_history, size);
762 s->max = journal->j_history_max;
763 s->start = journal->j_history_cur % s->max;
764 spin_unlock(&journal->j_history_lock);
765
766 rc = seq_open(file, &jbd2_seq_history_ops);
767 if (rc == 0) {
768 struct seq_file *m = file->private_data;
769 m->private = s;
770 } else {
771 kfree(s->stats);
772 kfree(s);
773 }
774 return rc;
775
776}
777
778static int jbd2_seq_history_release(struct inode *inode, struct file *file)
779{
780 struct seq_file *seq = file->private_data;
781 struct jbd2_stats_proc_session *s = seq->private;
782
783 kfree(s->stats);
784 kfree(s);
785 return seq_release(inode, file);
786}
787
788static struct file_operations jbd2_seq_history_fops = {
789 .owner = THIS_MODULE,
790 .open = jbd2_seq_history_open,
791 .read = seq_read,
792 .llseek = seq_lseek,
793 .release = jbd2_seq_history_release,
794};
795
796static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
797{
798 return *pos ? NULL : SEQ_START_TOKEN;
799}
800
801static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
802{
803 return NULL;
804}
805
806static int jbd2_seq_info_show(struct seq_file *seq, void *v)
807{
808 struct jbd2_stats_proc_session *s = seq->private;
809
810 if (v != SEQ_START_TOKEN)
811 return 0;
812 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
813 s->stats->ts_tid,
814 s->journal->j_max_transaction_buffers);
815 if (s->stats->ts_tid == 0)
816 return 0;
817 seq_printf(seq, "average: \n %ums waiting for transaction\n",
818 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
819 seq_printf(seq, " %ums running transaction\n",
820 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
821 seq_printf(seq, " %ums transaction was being locked\n",
822 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
823 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
824 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
825 seq_printf(seq, " %ums logging transaction\n",
826 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
827 seq_printf(seq, " %lu handles per transaction\n",
828 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
829 seq_printf(seq, " %lu blocks per transaction\n",
830 s->stats->u.run.rs_blocks / s->stats->ts_tid);
831 seq_printf(seq, " %lu logged blocks per transaction\n",
832 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
833 return 0;
834}
835
836static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
837{
838}
839
840static struct seq_operations jbd2_seq_info_ops = {
841 .start = jbd2_seq_info_start,
842 .next = jbd2_seq_info_next,
843 .stop = jbd2_seq_info_stop,
844 .show = jbd2_seq_info_show,
845};
846
847static int jbd2_seq_info_open(struct inode *inode, struct file *file)
848{
849 journal_t *journal = PDE(inode)->data;
850 struct jbd2_stats_proc_session *s;
851 int rc, size;
852
853 s = kmalloc(sizeof(*s), GFP_KERNEL);
854 if (s == NULL)
855 return -ENOMEM;
856 size = sizeof(struct transaction_stats_s);
857 s->stats = kmalloc(size, GFP_KERNEL);
858 if (s->stats == NULL) {
859 kfree(s);
860 return -ENOMEM;
861 }
862 spin_lock(&journal->j_history_lock);
863 memcpy(s->stats, &journal->j_stats, size);
864 s->journal = journal;
865 spin_unlock(&journal->j_history_lock);
866
867 rc = seq_open(file, &jbd2_seq_info_ops);
868 if (rc == 0) {
869 struct seq_file *m = file->private_data;
870 m->private = s;
871 } else {
872 kfree(s->stats);
873 kfree(s);
874 }
875 return rc;
876
877}
878
879static int jbd2_seq_info_release(struct inode *inode, struct file *file)
880{
881 struct seq_file *seq = file->private_data;
882 struct jbd2_stats_proc_session *s = seq->private;
883 kfree(s->stats);
884 kfree(s);
885 return seq_release(inode, file);
886}
887
888static struct file_operations jbd2_seq_info_fops = {
889 .owner = THIS_MODULE,
890 .open = jbd2_seq_info_open,
891 .read = seq_read,
892 .llseek = seq_lseek,
893 .release = jbd2_seq_info_release,
894};
895
896static struct proc_dir_entry *proc_jbd2_stats;
897
898static void jbd2_stats_proc_init(journal_t *journal)
899{
Theodore Ts'o05496762008-09-16 14:36:17 -0400900 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500901 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700902 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
903 &jbd2_seq_history_fops, journal);
904 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
905 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500906 }
907}
908
909static void jbd2_stats_proc_exit(journal_t *journal)
910{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500911 remove_proc_entry("info", journal->j_proc_entry);
912 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400913 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500914}
915
916static void journal_init_stats(journal_t *journal)
917{
918 int size;
919
920 if (!proc_jbd2_stats)
921 return;
922
923 journal->j_history_max = 100;
924 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
925 journal->j_history = kzalloc(size, GFP_KERNEL);
926 if (!journal->j_history) {
927 journal->j_history_max = 0;
928 return;
929 }
930 spin_lock_init(&journal->j_history_lock);
931}
932
Dave Kleikamp470decc2006-10-11 01:20:57 -0700933/*
934 * Management for journal control blocks: functions to create and
935 * destroy journal_t structures, and to initialise and read existing
936 * journal blocks from disk. */
937
938/* First: create and setup a journal_t object in memory. We initialise
939 * very few fields yet: that has to wait until we have created the
940 * journal structures from from scratch, or loaded them from disk. */
941
942static journal_t * journal_init_common (void)
943{
944 journal_t *journal;
945 int err;
946
Mingming Caod802ffa2007-10-16 18:38:25 -0400947 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700948 if (!journal)
949 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700950
951 init_waitqueue_head(&journal->j_wait_transaction_locked);
952 init_waitqueue_head(&journal->j_wait_logspace);
953 init_waitqueue_head(&journal->j_wait_done_commit);
954 init_waitqueue_head(&journal->j_wait_checkpoint);
955 init_waitqueue_head(&journal->j_wait_commit);
956 init_waitqueue_head(&journal->j_wait_updates);
957 mutex_init(&journal->j_barrier);
958 mutex_init(&journal->j_checkpoint_mutex);
959 spin_lock_init(&journal->j_revoke_lock);
960 spin_lock_init(&journal->j_list_lock);
961 spin_lock_init(&journal->j_state_lock);
962
Mingming Caocd02ff02007-10-16 18:38:25 -0400963 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700964
965 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700966 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700967
968 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700969 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700970 if (err) {
971 kfree(journal);
972 goto fail;
973 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500974
975 journal_init_stats(journal);
976
Dave Kleikamp470decc2006-10-11 01:20:57 -0700977 return journal;
978fail:
979 return NULL;
980}
981
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700982/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700983 *
984 * Create a journal structure assigned some fixed set of disk blocks to
985 * the journal. We don't actually touch those disk blocks yet, but we
986 * need to set up all of the mapping information to tell the journaling
987 * system where the journal blocks are.
988 *
989 */
990
991/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400992 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700993 * @bdev: Block device on which to create the journal
994 * @fs_dev: Device which hold journalled filesystem for this journal.
995 * @start: Block nr Start of journal.
996 * @len: Length of the journal in blocks.
997 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400998 *
999 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001000 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001001 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002 * range of blocks on an arbitrary block device.
1003 *
1004 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001005journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001006 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001007 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001008{
1009 journal_t *journal = journal_init_common();
1010 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001011 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001012 int n;
1013
1014 if (!journal)
1015 return NULL;
1016
1017 /* journal descriptor can store up to n blocks -bzzz */
1018 journal->j_blocksize = blocksize;
1019 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1020 journal->j_wbufsize = n;
1021 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1022 if (!journal->j_wbuf) {
1023 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001024 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001025 kfree(journal);
1026 journal = NULL;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001027 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001028 }
1029 journal->j_dev = bdev;
1030 journal->j_fs_dev = fs_dev;
1031 journal->j_blk_offset = start;
1032 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001033 bdevname(journal->j_dev, journal->j_devname);
1034 p = journal->j_devname;
1035 while ((p = strchr(p, '/')))
1036 *p = '!';
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001037 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001038
1039 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1040 J_ASSERT(bh != NULL);
1041 journal->j_sb_buffer = bh;
1042 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001043out:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001044 return journal;
1045}
1046
1047/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001048 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049 * @inode: An inode to create the journal in
1050 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001051 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001052 * the journal. The inode must exist already, must support bmap() and
1053 * must have all data blocks preallocated.
1054 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001055journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001056{
1057 struct buffer_head *bh;
1058 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001059 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001060 int err;
1061 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001062 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063
1064 if (!journal)
1065 return NULL;
1066
1067 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1068 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001069 bdevname(journal->j_dev, journal->j_devname);
1070 p = journal->j_devname;
1071 while ((p = strchr(p, '/')))
1072 *p = '!';
1073 p = journal->j_devname + strlen(journal->j_devname);
1074 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001075 jbd_debug(1,
1076 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1077 journal, inode->i_sb->s_id, inode->i_ino,
1078 (long long) inode->i_size,
1079 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1080
1081 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1082 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001083 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001084
1085 /* journal descriptor can store up to n blocks -bzzz */
1086 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1087 journal->j_wbufsize = n;
1088 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1089 if (!journal->j_wbuf) {
1090 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001091 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001092 kfree(journal);
1093 return NULL;
1094 }
1095
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001096 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001097 /* If that failed, give up */
1098 if (err) {
1099 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001100 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101 kfree(journal);
1102 return NULL;
1103 }
1104
1105 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1106 J_ASSERT(bh != NULL);
1107 journal->j_sb_buffer = bh;
1108 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1109
1110 return journal;
1111}
1112
1113/*
1114 * If the journal init or create aborts, we need to mark the journal
1115 * superblock as being NULL to prevent the journal destroy from writing
1116 * back a bogus superblock.
1117 */
1118static void journal_fail_superblock (journal_t *journal)
1119{
1120 struct buffer_head *bh = journal->j_sb_buffer;
1121 brelse(bh);
1122 journal->j_sb_buffer = NULL;
1123}
1124
1125/*
1126 * Given a journal_t structure, initialise the various fields for
1127 * startup of a new journaling session. We use this both when creating
1128 * a journal, and after recovering an old journal to reset it for
1129 * subsequent use.
1130 */
1131
1132static int journal_reset(journal_t *journal)
1133{
1134 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001135 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001136
1137 first = be32_to_cpu(sb->s_first);
1138 last = be32_to_cpu(sb->s_maxlen);
1139
1140 journal->j_first = first;
1141 journal->j_last = last;
1142
1143 journal->j_head = first;
1144 journal->j_tail = first;
1145 journal->j_free = last - first;
1146
1147 journal->j_tail_sequence = journal->j_transaction_sequence;
1148 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1149 journal->j_commit_request = journal->j_commit_sequence;
1150
1151 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1152
1153 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001154 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001155 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001156}
1157
1158/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001159 * int jbd2_journal_create() - Initialise the new journal file
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160 * @journal: Journal to create. This structure must have been initialised
1161 *
1162 * Given a journal_t structure which tells us which disk blocks we can
1163 * use, create a new journal superblock and initialise all of the
1164 * journal fields from scratch.
1165 **/
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001166int jbd2_journal_create(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001167{
Mingming Cao18eba7a2006-10-11 01:21:13 -07001168 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169 struct buffer_head *bh;
1170 journal_superblock_t *sb;
1171 int i, err;
1172
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001173 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001174 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1175 journal->j_maxlen);
1176 journal_fail_superblock(journal);
1177 return -EINVAL;
1178 }
1179
1180 if (journal->j_inode == NULL) {
1181 /*
1182 * We don't know what block to start at!
1183 */
1184 printk(KERN_EMERG
1185 "%s: creation of journal on external device!\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001186 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001187 BUG();
1188 }
1189
1190 /* Zero out the entire journal on disk. We cannot afford to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001191 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001192 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1193 for (i = 0; i < journal->j_maxlen; i++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001194 err = jbd2_journal_bmap(journal, i, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001195 if (err)
1196 return err;
1197 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1198 lock_buffer(bh);
1199 memset (bh->b_data, 0, journal->j_blocksize);
1200 BUFFER_TRACE(bh, "marking dirty");
1201 mark_buffer_dirty(bh);
1202 BUFFER_TRACE(bh, "marking uptodate");
1203 set_buffer_uptodate(bh);
1204 unlock_buffer(bh);
1205 __brelse(bh);
1206 }
1207
1208 sync_blockdev(journal->j_dev);
1209 jbd_debug(1, "JBD: journal cleared.\n");
1210
1211 /* OK, fill in the initial static fields in the new superblock */
1212 sb = journal->j_superblock;
1213
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001214 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1215 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001216
1217 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1218 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1219 sb->s_first = cpu_to_be32(1);
1220
1221 journal->j_transaction_sequence = 1;
1222
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001223 journal->j_flags &= ~JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001224 journal->j_format_version = 2;
1225
1226 return journal_reset(journal);
1227}
1228
1229/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001230 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 * @journal: The journal to update.
1232 * @wait: Set to '0' if you don't want to wait for IO completion.
1233 *
1234 * Update a journal's dynamic superblock fields and write it to disk,
1235 * optionally waiting for the IO to complete.
1236 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001237void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238{
1239 journal_superblock_t *sb = journal->j_superblock;
1240 struct buffer_head *bh = journal->j_sb_buffer;
1241
1242 /*
1243 * As a special case, if the on-disk copy is already marked as needing
1244 * no recovery (s_start == 0) and there are no outstanding transactions
1245 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001246 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001247 * attempting a write to a potential-readonly device.
1248 */
1249 if (sb->s_start == 0 && journal->j_tail_sequence ==
1250 journal->j_transaction_sequence) {
1251 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1252 "(start %ld, seq %d, errno %d)\n",
1253 journal->j_tail, journal->j_tail_sequence,
1254 journal->j_errno);
1255 goto out;
1256 }
1257
Theodore Ts'o914258b2008-10-06 21:35:40 -04001258 if (buffer_write_io_error(bh)) {
1259 /*
1260 * Oh, dear. A previous attempt to write the journal
1261 * superblock failed. This could happen because the
1262 * USB device was yanked out. Or it could happen to
1263 * be a transient write error and maybe the block will
1264 * be remapped. Nothing we can do but to retry the
1265 * write and hope for the best.
1266 */
1267 printk(KERN_ERR "JBD2: previous I/O error detected "
1268 "for journal superblock update for %s.\n",
1269 journal->j_devname);
1270 clear_buffer_write_io_error(bh);
1271 set_buffer_uptodate(bh);
1272 }
1273
Dave Kleikamp470decc2006-10-11 01:20:57 -07001274 spin_lock(&journal->j_state_lock);
1275 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1276 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1277
1278 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1279 sb->s_start = cpu_to_be32(journal->j_tail);
1280 sb->s_errno = cpu_to_be32(journal->j_errno);
1281 spin_unlock(&journal->j_state_lock);
1282
1283 BUFFER_TRACE(bh, "marking dirty");
1284 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001285 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001286 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001287 if (buffer_write_io_error(bh)) {
1288 printk(KERN_ERR "JBD2: I/O error detected "
1289 "when updating journal superblock for %s.\n",
1290 journal->j_devname);
1291 clear_buffer_write_io_error(bh);
1292 set_buffer_uptodate(bh);
1293 }
1294 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001295 ll_rw_block(SWRITE, 1, &bh);
1296
1297out:
1298 /* If we have just flushed the log (by marking s_start==0), then
1299 * any future commit will have to be careful to update the
1300 * superblock again to re-record the true start of the log. */
1301
1302 spin_lock(&journal->j_state_lock);
1303 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001304 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001305 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001306 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001307 spin_unlock(&journal->j_state_lock);
1308}
1309
1310/*
1311 * Read the superblock for a given journal, performing initial
1312 * validation of the format.
1313 */
1314
1315static int journal_get_superblock(journal_t *journal)
1316{
1317 struct buffer_head *bh;
1318 journal_superblock_t *sb;
1319 int err = -EIO;
1320
1321 bh = journal->j_sb_buffer;
1322
1323 J_ASSERT(bh != NULL);
1324 if (!buffer_uptodate(bh)) {
1325 ll_rw_block(READ, 1, &bh);
1326 wait_on_buffer(bh);
1327 if (!buffer_uptodate(bh)) {
1328 printk (KERN_ERR
1329 "JBD: IO error reading journal superblock\n");
1330 goto out;
1331 }
1332 }
1333
1334 sb = journal->j_superblock;
1335
1336 err = -EINVAL;
1337
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001338 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001339 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1340 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1341 goto out;
1342 }
1343
1344 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001345 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001346 journal->j_format_version = 1;
1347 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001348 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001349 journal->j_format_version = 2;
1350 break;
1351 default:
1352 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1353 goto out;
1354 }
1355
1356 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1357 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1358 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1359 printk (KERN_WARNING "JBD: journal file too short\n");
1360 goto out;
1361 }
1362
1363 return 0;
1364
1365out:
1366 journal_fail_superblock(journal);
1367 return err;
1368}
1369
1370/*
1371 * Load the on-disk journal superblock and read the key fields into the
1372 * journal_t.
1373 */
1374
1375static int load_superblock(journal_t *journal)
1376{
1377 int err;
1378 journal_superblock_t *sb;
1379
1380 err = journal_get_superblock(journal);
1381 if (err)
1382 return err;
1383
1384 sb = journal->j_superblock;
1385
1386 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1387 journal->j_tail = be32_to_cpu(sb->s_start);
1388 journal->j_first = be32_to_cpu(sb->s_first);
1389 journal->j_last = be32_to_cpu(sb->s_maxlen);
1390 journal->j_errno = be32_to_cpu(sb->s_errno);
1391
1392 return 0;
1393}
1394
1395
1396/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001397 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001398 * @journal: Journal to act on.
1399 *
1400 * Given a journal_t structure which tells us which disk blocks contain
1401 * a journal, read the journal from disk to initialise the in-memory
1402 * structures.
1403 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001404int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405{
1406 int err;
1407 journal_superblock_t *sb;
1408
1409 err = load_superblock(journal);
1410 if (err)
1411 return err;
1412
1413 sb = journal->j_superblock;
1414 /* If this is a V2 superblock, then we have to check the
1415 * features flags on it. */
1416
1417 if (journal->j_format_version >= 2) {
1418 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001419 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001420 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001421 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001422 printk (KERN_WARNING
1423 "JBD: Unrecognised features on journal\n");
1424 return -EINVAL;
1425 }
1426 }
1427
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428 /* Let the recovery code check whether it needs to recover any
1429 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001430 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001431 goto recovery_error;
1432
1433 /* OK, we've finished with the dynamic journal bits:
1434 * reinitialise the dynamic contents of the superblock in memory
1435 * and reset them on disk. */
1436 if (journal_reset(journal))
1437 goto recovery_error;
1438
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001439 journal->j_flags &= ~JBD2_ABORT;
1440 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 return 0;
1442
1443recovery_error:
1444 printk (KERN_WARNING "JBD: recovery failed\n");
1445 return -EIO;
1446}
1447
1448/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001449 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001450 * @journal: Journal to act on.
1451 *
1452 * Release a journal_t structure once it is no longer in use by the
1453 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001454 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001455 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001456int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001457{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001458 int err = 0;
1459
Dave Kleikamp470decc2006-10-11 01:20:57 -07001460 /* Wait for the commit thread to wake up and die. */
1461 journal_kill_thread(journal);
1462
1463 /* Force a final log commit */
1464 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001465 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001466
1467 /* Force any old transactions to disk */
1468
1469 /* Totally anal locking here... */
1470 spin_lock(&journal->j_list_lock);
1471 while (journal->j_checkpoint_transactions != NULL) {
1472 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001473 jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001474 spin_lock(&journal->j_list_lock);
1475 }
1476
1477 J_ASSERT(journal->j_running_transaction == NULL);
1478 J_ASSERT(journal->j_committing_transaction == NULL);
1479 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1480 spin_unlock(&journal->j_list_lock);
1481
Dave Kleikamp470decc2006-10-11 01:20:57 -07001482 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001483 if (!is_journal_aborted(journal)) {
1484 /* We can now mark the journal as empty. */
1485 journal->j_tail = 0;
1486 journal->j_tail_sequence =
1487 ++journal->j_transaction_sequence;
1488 jbd2_journal_update_superblock(journal, 1);
1489 } else {
1490 err = -EIO;
1491 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001492 brelse(journal->j_sb_buffer);
1493 }
1494
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001495 if (journal->j_proc_entry)
1496 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001497 if (journal->j_inode)
1498 iput(journal->j_inode);
1499 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001500 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001501 kfree(journal->j_wbuf);
1502 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001503
1504 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001505}
1506
1507
1508/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001509 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001510 * @journal: Journal to check.
1511 * @compat: bitmask of compatible features
1512 * @ro: bitmask of features that force read-only mount
1513 * @incompat: bitmask of incompatible features
1514 *
1515 * Check whether the journal uses all of a given set of
1516 * features. Return true (non-zero) if it does.
1517 **/
1518
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001519int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001520 unsigned long ro, unsigned long incompat)
1521{
1522 journal_superblock_t *sb;
1523
1524 if (!compat && !ro && !incompat)
1525 return 1;
1526 if (journal->j_format_version == 1)
1527 return 0;
1528
1529 sb = journal->j_superblock;
1530
1531 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1532 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1533 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1534 return 1;
1535
1536 return 0;
1537}
1538
1539/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001540 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001541 * @journal: Journal to check.
1542 * @compat: bitmask of compatible features
1543 * @ro: bitmask of features that force read-only mount
1544 * @incompat: bitmask of incompatible features
1545 *
1546 * Check whether the journaling code supports the use of
1547 * all of a given set of features on this journal. Return true
1548 * (non-zero) if it can. */
1549
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001550int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001551 unsigned long ro, unsigned long incompat)
1552{
1553 journal_superblock_t *sb;
1554
1555 if (!compat && !ro && !incompat)
1556 return 1;
1557
1558 sb = journal->j_superblock;
1559
1560 /* We can support any known requested features iff the
1561 * superblock is in version 2. Otherwise we fail to support any
1562 * extended sb features. */
1563
1564 if (journal->j_format_version != 2)
1565 return 0;
1566
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1568 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1569 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 return 1;
1571
1572 return 0;
1573}
1574
1575/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001576 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001577 * @journal: Journal to act on.
1578 * @compat: bitmask of compatible features
1579 * @ro: bitmask of features that force read-only mount
1580 * @incompat: bitmask of incompatible features
1581 *
1582 * Mark a given journal feature as present on the
1583 * superblock. Returns true if the requested features could be set.
1584 *
1585 */
1586
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001587int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001588 unsigned long ro, unsigned long incompat)
1589{
1590 journal_superblock_t *sb;
1591
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001592 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593 return 1;
1594
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001595 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596 return 0;
1597
1598 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1599 compat, ro, incompat);
1600
1601 sb = journal->j_superblock;
1602
1603 sb->s_feature_compat |= cpu_to_be32(compat);
1604 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1605 sb->s_feature_incompat |= cpu_to_be32(incompat);
1606
1607 return 1;
1608}
1609
Girish Shilamkar818d2762008-01-28 23:58:27 -05001610/*
1611 * jbd2_journal_clear_features () - Clear a given journal feature in the
1612 * superblock
1613 * @journal: Journal to act on.
1614 * @compat: bitmask of compatible features
1615 * @ro: bitmask of features that force read-only mount
1616 * @incompat: bitmask of incompatible features
1617 *
1618 * Clear a given journal feature as present on the
1619 * superblock.
1620 */
1621void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1622 unsigned long ro, unsigned long incompat)
1623{
1624 journal_superblock_t *sb;
1625
1626 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1627 compat, ro, incompat);
1628
1629 sb = journal->j_superblock;
1630
1631 sb->s_feature_compat &= ~cpu_to_be32(compat);
1632 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1633 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1634}
1635EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001636
1637/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 * @journal: Journal to act on.
1640 *
1641 * Given an initialised but unloaded journal struct, poke about in the
1642 * on-disk structure to update it to the most recent supported version.
1643 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001644int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645{
1646 journal_superblock_t *sb;
1647 int err;
1648
1649 err = journal_get_superblock(journal);
1650 if (err)
1651 return err;
1652
1653 sb = journal->j_superblock;
1654
1655 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001656 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001657 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001658 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001659 return journal_convert_superblock_v1(journal, sb);
1660 default:
1661 break;
1662 }
1663 return -EINVAL;
1664}
1665
1666static int journal_convert_superblock_v1(journal_t *journal,
1667 journal_superblock_t *sb)
1668{
1669 int offset, blocksize;
1670 struct buffer_head *bh;
1671
1672 printk(KERN_WARNING
1673 "JBD: Converting superblock from version 1 to 2.\n");
1674
1675 /* Pre-initialise new fields to zero */
1676 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1677 blocksize = be32_to_cpu(sb->s_blocksize);
1678 memset(&sb->s_feature_compat, 0, blocksize-offset);
1679
1680 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001681 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001682 journal->j_format_version = 2;
1683
1684 bh = journal->j_sb_buffer;
1685 BUFFER_TRACE(bh, "marking dirty");
1686 mark_buffer_dirty(bh);
1687 sync_dirty_buffer(bh);
1688 return 0;
1689}
1690
1691
1692/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001693 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001694 * @journal: Journal to act on.
1695 *
1696 * Flush all data for a given journal to disk and empty the journal.
1697 * Filesystems can use this when remounting readonly to ensure that
1698 * recovery does not need to happen on remount.
1699 */
1700
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001701int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001702{
1703 int err = 0;
1704 transaction_t *transaction = NULL;
1705 unsigned long old_tail;
1706
1707 spin_lock(&journal->j_state_lock);
1708
1709 /* Force everything buffered to the log... */
1710 if (journal->j_running_transaction) {
1711 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001712 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713 } else if (journal->j_committing_transaction)
1714 transaction = journal->j_committing_transaction;
1715
1716 /* Wait for the log commit to complete... */
1717 if (transaction) {
1718 tid_t tid = transaction->t_tid;
1719
1720 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 } else {
1723 spin_unlock(&journal->j_state_lock);
1724 }
1725
1726 /* ...and flush everything in the log out to disk. */
1727 spin_lock(&journal->j_list_lock);
1728 while (!err && journal->j_checkpoint_transactions != NULL) {
1729 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001730 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001732 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001733 spin_lock(&journal->j_list_lock);
1734 }
1735 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001736
1737 if (is_journal_aborted(journal))
1738 return -EIO;
1739
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001740 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001741
1742 /* Finally, mark the journal as really needing no recovery.
1743 * This sets s_start==0 in the underlying superblock, which is
1744 * the magic code for a fully-recovered superblock. Any future
1745 * commits of data to the journal will restore the current
1746 * s_start value. */
1747 spin_lock(&journal->j_state_lock);
1748 old_tail = journal->j_tail;
1749 journal->j_tail = 0;
1750 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001751 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752 spin_lock(&journal->j_state_lock);
1753 journal->j_tail = old_tail;
1754
1755 J_ASSERT(!journal->j_running_transaction);
1756 J_ASSERT(!journal->j_committing_transaction);
1757 J_ASSERT(!journal->j_checkpoint_transactions);
1758 J_ASSERT(journal->j_head == journal->j_tail);
1759 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1760 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001761 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001762}
1763
1764/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001765 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766 * @journal: Journal to act on.
1767 * @write: flag (see below)
1768 *
1769 * Wipe out all of the contents of a journal, safely. This will produce
1770 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001771 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001772 *
1773 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1774 * we merely suppress recovery.
1775 */
1776
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001777int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001778{
1779 journal_superblock_t *sb;
1780 int err = 0;
1781
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001782 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001783
1784 err = load_superblock(journal);
1785 if (err)
1786 return err;
1787
1788 sb = journal->j_superblock;
1789
1790 if (!journal->j_tail)
1791 goto no_recovery;
1792
1793 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1794 write ? "Clearing" : "Ignoring");
1795
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001796 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001797 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001798 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001799
1800 no_recovery:
1801 return err;
1802}
1803
1804/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805 * Journal abort has very specific semantics, which we describe
1806 * for journal abort.
1807 *
1808 * Two internal function, which provide abort to te jbd layer
1809 * itself are here.
1810 */
1811
1812/*
1813 * Quick version for internal journal use (doesn't lock the journal).
1814 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1815 * and don't attempt to make any other journal updates.
1816 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001817void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001818{
1819 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001821 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822 return;
1823
1824 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001825 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001826
1827 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001828 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001829 transaction = journal->j_running_transaction;
1830 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001831 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001832 spin_unlock(&journal->j_state_lock);
1833}
1834
1835/* Soft abort: record the abort error status in the journal superblock,
1836 * but don't do any other IO. */
1837static void __journal_abort_soft (journal_t *journal, int errno)
1838{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001839 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001840 return;
1841
1842 if (!journal->j_errno)
1843 journal->j_errno = errno;
1844
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001845 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001846
1847 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001848 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849}
1850
1851/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001852 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853 * @journal: the journal to shutdown.
1854 * @errno: an error number to record in the journal indicating
1855 * the reason for the shutdown.
1856 *
1857 * Perform a complete, immediate shutdown of the ENTIRE
1858 * journal (not of a single transaction). This operation cannot be
1859 * undone without closing and reopening the journal.
1860 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001861 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1863 * mode.
1864 *
1865 * Journal abort has very specific semantics. Any existing dirty,
1866 * unjournaled buffers in the main filesystem will still be written to
1867 * disk by bdflush, but the journaling mechanism will be suspended
1868 * immediately and no further transaction commits will be honoured.
1869 *
1870 * Any dirty, journaled buffers will be written back to disk without
1871 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1872 * filesystem, but we _do_ attempt to leave as much data as possible
1873 * behind for fsck to use for cleanup.
1874 *
1875 * Any attempt to get a new transaction handle on a journal which is in
1876 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001877 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001878 * entered abort state during the update.
1879 *
1880 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001881 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001882 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001883 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001884 * which will be recorded (if possible) in the journal superblock. This
1885 * allows a client to record failure conditions in the middle of a
1886 * transaction without having to complete the transaction to record the
1887 * failure to disk. ext3_error, for example, now uses this
1888 * functionality.
1889 *
1890 * Errors which originate from within the journaling layer will NOT
1891 * supply an errno; a null errno implies that absolutely no further
1892 * writes are done to the journal (unless there are any already in
1893 * progress).
1894 *
1895 */
1896
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001897void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001898{
1899 __journal_abort_soft(journal, errno);
1900}
1901
1902/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001903 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001904 * @journal: journal to examine.
1905 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001906 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907 * time the journal was mounted - if the journal was stopped
1908 * without calling abort this will be 0.
1909 *
1910 * If the journal has been aborted on this mount time -EROFS will
1911 * be returned.
1912 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001913int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001914{
1915 int err;
1916
1917 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 err = -EROFS;
1920 else
1921 err = journal->j_errno;
1922 spin_unlock(&journal->j_state_lock);
1923 return err;
1924}
1925
1926/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001927 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928 * @journal: journal to act on.
1929 *
1930 * An error must be cleared or Acked to take a FS out of readonly
1931 * mode.
1932 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001933int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934{
1935 int err = 0;
1936
1937 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001938 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001939 err = -EROFS;
1940 else
1941 journal->j_errno = 0;
1942 spin_unlock(&journal->j_state_lock);
1943 return err;
1944}
1945
1946/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001947 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001948 * @journal: journal to act on.
1949 *
1950 * An error must be cleared or Acked to take a FS out of readonly
1951 * mode.
1952 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001953void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001954{
1955 spin_lock(&journal->j_state_lock);
1956 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001957 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001958 spin_unlock(&journal->j_state_lock);
1959}
1960
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001961int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962{
1963 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1964}
1965
1966/*
Zach Brownb517bea2006-10-11 01:21:08 -07001967 * helper functions to deal with 32 or 64bit block numbers.
1968 */
1969size_t journal_tag_bytes(journal_t *journal)
1970{
1971 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001972 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001973 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001974 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001975}
1976
1977/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001978 * Journal_head storage management
1979 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001980static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001981#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001982static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1983#endif
1984
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001985static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001986{
1987 int retval;
1988
Al Viro1076d172008-03-29 03:07:18 +00001989 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001990 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991 sizeof(struct journal_head),
1992 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001993 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001994 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001995 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001996 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 retval = -ENOMEM;
1998 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1999 }
2000 return retval;
2001}
2002
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002003static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002005 if (jbd2_journal_head_cache) {
2006 kmem_cache_destroy(jbd2_journal_head_cache);
2007 jbd2_journal_head_cache = NULL;
2008 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002009}
2010
2011/*
2012 * journal_head splicing and dicing
2013 */
2014static struct journal_head *journal_alloc_journal_head(void)
2015{
2016 struct journal_head *ret;
2017 static unsigned long last_warning;
2018
Jose R. Santose23291b2007-07-18 08:57:06 -04002019#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002020 atomic_inc(&nr_journal_heads);
2021#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002022 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002023 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002024 jbd_debug(1, "out of memory for journal_head\n");
2025 if (time_after(jiffies, last_warning + 5*HZ)) {
2026 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002027 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028 last_warning = jiffies;
2029 }
Al Viro1076d172008-03-29 03:07:18 +00002030 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002031 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002032 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033 }
2034 }
2035 return ret;
2036}
2037
2038static void journal_free_journal_head(struct journal_head *jh)
2039{
Jose R. Santose23291b2007-07-18 08:57:06 -04002040#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002041 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002042 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002043#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002044 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002045}
2046
2047/*
2048 * A journal_head is attached to a buffer_head whenever JBD has an
2049 * interest in the buffer.
2050 *
2051 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2052 * is set. This bit is tested in core kernel code where we need to take
2053 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2054 * there.
2055 *
2056 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2057 *
2058 * When a buffer has its BH_JBD bit set it is immune from being released by
2059 * core kernel code, mainly via ->b_count.
2060 *
2061 * A journal_head may be detached from its buffer_head when the journal_head's
2062 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002063 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064 * journal_head can be dropped if needed.
2065 *
2066 * Various places in the kernel want to attach a journal_head to a buffer_head
2067 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002068 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002069 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002070 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002071 *
2072 * So the typical usage would be:
2073 *
2074 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002075 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002076 * ...
2077 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002078 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002079 *
2080 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2081 * because it has a non-zero b_transaction.
2082 */
2083
2084/*
2085 * Give a buffer_head a journal_head.
2086 *
2087 * Doesn't need the journal lock.
2088 * May sleep.
2089 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002090struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002091{
2092 struct journal_head *jh;
2093 struct journal_head *new_jh = NULL;
2094
2095repeat:
2096 if (!buffer_jbd(bh)) {
2097 new_jh = journal_alloc_journal_head();
2098 memset(new_jh, 0, sizeof(*new_jh));
2099 }
2100
2101 jbd_lock_bh_journal_head(bh);
2102 if (buffer_jbd(bh)) {
2103 jh = bh2jh(bh);
2104 } else {
2105 J_ASSERT_BH(bh,
2106 (atomic_read(&bh->b_count) > 0) ||
2107 (bh->b_page && bh->b_page->mapping));
2108
2109 if (!new_jh) {
2110 jbd_unlock_bh_journal_head(bh);
2111 goto repeat;
2112 }
2113
2114 jh = new_jh;
2115 new_jh = NULL; /* We consumed it */
2116 set_buffer_jbd(bh);
2117 bh->b_private = jh;
2118 jh->b_bh = bh;
2119 get_bh(bh);
2120 BUFFER_TRACE(bh, "added journal_head");
2121 }
2122 jh->b_jcount++;
2123 jbd_unlock_bh_journal_head(bh);
2124 if (new_jh)
2125 journal_free_journal_head(new_jh);
2126 return bh->b_private;
2127}
2128
2129/*
2130 * Grab a ref against this buffer_head's journal_head. If it ended up not
2131 * having a journal_head, return NULL
2132 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002133struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002134{
2135 struct journal_head *jh = NULL;
2136
2137 jbd_lock_bh_journal_head(bh);
2138 if (buffer_jbd(bh)) {
2139 jh = bh2jh(bh);
2140 jh->b_jcount++;
2141 }
2142 jbd_unlock_bh_journal_head(bh);
2143 return jh;
2144}
2145
2146static void __journal_remove_journal_head(struct buffer_head *bh)
2147{
2148 struct journal_head *jh = bh2jh(bh);
2149
2150 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2151
2152 get_bh(bh);
2153 if (jh->b_jcount == 0) {
2154 if (jh->b_transaction == NULL &&
2155 jh->b_next_transaction == NULL &&
2156 jh->b_cp_transaction == NULL) {
2157 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2158 J_ASSERT_BH(bh, buffer_jbd(bh));
2159 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2160 BUFFER_TRACE(bh, "remove journal_head");
2161 if (jh->b_frozen_data) {
2162 printk(KERN_WARNING "%s: freeing "
2163 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002164 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002165 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002166 }
2167 if (jh->b_committed_data) {
2168 printk(KERN_WARNING "%s: freeing "
2169 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002170 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002171 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002172 }
2173 bh->b_private = NULL;
2174 jh->b_bh = NULL; /* debug, really */
2175 clear_buffer_jbd(bh);
2176 __brelse(bh);
2177 journal_free_journal_head(jh);
2178 } else {
2179 BUFFER_TRACE(bh, "journal_head was locked");
2180 }
2181 }
2182}
2183
2184/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002185 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002186 * and has a zero b_jcount then remove and release its journal_head. If we did
2187 * see that the buffer is not used by any transaction we also "logically"
2188 * decrement ->b_count.
2189 *
2190 * We in fact take an additional increment on ->b_count as a convenience,
2191 * because the caller usually wants to do additional things with the bh
2192 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002193 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002194 * time. Once the caller has run __brelse(), the buffer is eligible for
2195 * reaping by try_to_free_buffers().
2196 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002197void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002198{
2199 jbd_lock_bh_journal_head(bh);
2200 __journal_remove_journal_head(bh);
2201 jbd_unlock_bh_journal_head(bh);
2202}
2203
2204/*
2205 * Drop a reference on the passed journal_head. If it fell to zero then try to
2206 * release the journal_head from the buffer_head.
2207 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002208void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002209{
2210 struct buffer_head *bh = jh2bh(jh);
2211
2212 jbd_lock_bh_journal_head(bh);
2213 J_ASSERT_JH(jh, jh->b_jcount > 0);
2214 --jh->b_jcount;
2215 if (!jh->b_jcount && !jh->b_transaction) {
2216 __journal_remove_journal_head(bh);
2217 __brelse(bh);
2218 }
2219 jbd_unlock_bh_journal_head(bh);
2220}
2221
2222/*
Jan Karac851ed52008-07-11 19:27:31 -04002223 * Initialize jbd inode head
2224 */
2225void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2226{
2227 jinode->i_transaction = NULL;
2228 jinode->i_next_transaction = NULL;
2229 jinode->i_vfs_inode = inode;
2230 jinode->i_flags = 0;
2231 INIT_LIST_HEAD(&jinode->i_list);
2232}
2233
2234/*
2235 * Function to be called before we start removing inode from memory (i.e.,
2236 * clear_inode() is a fine place to be called from). It removes inode from
2237 * transaction's lists.
2238 */
2239void jbd2_journal_release_jbd_inode(journal_t *journal,
2240 struct jbd2_inode *jinode)
2241{
2242 int writeout = 0;
2243
2244 if (!journal)
2245 return;
2246restart:
2247 spin_lock(&journal->j_list_lock);
2248 /* Is commit writing out inode - we have to wait */
2249 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2250 wait_queue_head_t *wq;
2251 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2252 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2253 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2254 spin_unlock(&journal->j_list_lock);
2255 schedule();
2256 finish_wait(wq, &wait.wait);
2257 goto restart;
2258 }
2259
2260 /* Do we need to wait for data writeback? */
2261 if (journal->j_committing_transaction == jinode->i_transaction)
2262 writeout = 1;
2263 if (jinode->i_transaction) {
2264 list_del(&jinode->i_list);
2265 jinode->i_transaction = NULL;
2266 }
2267 spin_unlock(&journal->j_list_lock);
2268}
2269
2270/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002271 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002272 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002273#ifdef CONFIG_JBD2_DEBUG
2274u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002275EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002276
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002277#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002278
Jose R. Santos6f38c742007-10-16 18:38:25 -04002279static struct dentry *jbd2_debugfs_dir;
2280static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002281
2282static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002283{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002284 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2285 if (jbd2_debugfs_dir)
2286 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2287 jbd2_debugfs_dir,
2288 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002289}
2290
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002291static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002292{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002293 debugfs_remove(jbd2_debug);
2294 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002295}
2296
2297#else
2298
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002299static void __init jbd2_create_debugfs_entry(void)
2300{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002301}
2302
2303static void __exit jbd2_remove_debugfs_entry(void)
2304{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002305}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002306
2307#endif
2308
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002309#ifdef CONFIG_PROC_FS
2310
2311#define JBD2_STATS_PROC_NAME "fs/jbd2"
2312
2313static void __init jbd2_create_jbd_stats_proc_entry(void)
2314{
2315 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2316}
2317
2318static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2319{
2320 if (proc_jbd2_stats)
2321 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2322}
2323
2324#else
2325
2326#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2327#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2328
2329#endif
2330
Christoph Lametere18b8902006-12-06 20:33:20 -08002331struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002332
2333static int __init journal_init_handle_cache(void)
2334{
Johann Lombardia920e942006-10-11 01:21:00 -07002335 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002336 sizeof(handle_t),
2337 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002338 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002339 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2342 return -ENOMEM;
2343 }
2344 return 0;
2345}
2346
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002347static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002348{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002349 if (jbd2_handle_cache)
2350 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002351}
2352
2353/*
2354 * Module startup and shutdown
2355 */
2356
2357static int __init journal_init_caches(void)
2358{
2359 int ret;
2360
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002361 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002363 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002364 if (ret == 0)
2365 ret = journal_init_handle_cache();
2366 return ret;
2367}
2368
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002369static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002370{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002371 jbd2_journal_destroy_revoke_caches();
2372 jbd2_journal_destroy_jbd2_journal_head_cache();
2373 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002374}
2375
2376static int __init journal_init(void)
2377{
2378 int ret;
2379
2380 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2381
2382 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002383 if (ret == 0) {
2384 jbd2_create_debugfs_entry();
2385 jbd2_create_jbd_stats_proc_entry();
2386 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002387 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002388 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002389 return ret;
2390}
2391
2392static void __exit journal_exit(void)
2393{
Jose R. Santose23291b2007-07-18 08:57:06 -04002394#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002395 int n = atomic_read(&nr_journal_heads);
2396 if (n)
2397 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2398#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002399 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002400 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002401 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002402}
2403
2404MODULE_LICENSE("GPL");
2405module_init(journal_init);
2406module_exit(journal_exit);
2407