blob: c60f378b0f7670dec45afd81cd576cb030a0a324 [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>
31#include <linux/smp_lock.h>
32#include <linux/init.h>
33#include <linux/mm.h>
34#include <linux/suspend.h>
35#include <linux/pagemap.h>
36#include <linux/kthread.h>
37#include <linux/poison.h>
38#include <linux/proc_fs.h>
39
40#include <asm/uaccess.h>
41#include <asm/page.h>
42
Mingming Caof7f4bcc2006-10-11 01:20:59 -070043EXPORT_SYMBOL(jbd2_journal_start);
44EXPORT_SYMBOL(jbd2_journal_restart);
45EXPORT_SYMBOL(jbd2_journal_extend);
46EXPORT_SYMBOL(jbd2_journal_stop);
47EXPORT_SYMBOL(jbd2_journal_lock_updates);
48EXPORT_SYMBOL(jbd2_journal_unlock_updates);
49EXPORT_SYMBOL(jbd2_journal_get_write_access);
50EXPORT_SYMBOL(jbd2_journal_get_create_access);
51EXPORT_SYMBOL(jbd2_journal_get_undo_access);
52EXPORT_SYMBOL(jbd2_journal_dirty_data);
53EXPORT_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);
71EXPORT_SYMBOL(jbd2_journal_update_superblock);
72EXPORT_SYMBOL(jbd2_journal_abort);
73EXPORT_SYMBOL(jbd2_journal_errno);
74EXPORT_SYMBOL(jbd2_journal_ack_err);
75EXPORT_SYMBOL(jbd2_journal_clear_err);
76EXPORT_SYMBOL(jbd2_log_wait_commit);
77EXPORT_SYMBOL(jbd2_journal_start_commit);
78EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
79EXPORT_SYMBOL(jbd2_journal_wipe);
80EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
81EXPORT_SYMBOL(jbd2_journal_invalidatepage);
82EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
83EXPORT_SYMBOL(jbd2_journal_force_commit);
Dave Kleikamp470decc2006-10-11 01:20:57 -070084
85static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
86static void __journal_abort_soft (journal_t *journal, int errno);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070087static int jbd2_journal_create_jbd_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -070088
89/*
90 * Helper function used to manage commit timeouts
91 */
92
93static void commit_timeout(unsigned long __data)
94{
95 struct task_struct * p = (struct task_struct *) __data;
96
97 wake_up_process(p);
98}
99
100/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700101 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700102 * journal.
103 *
104 * This kernel thread is responsible for two things:
105 *
106 * 1) COMMIT: Every so often we need to commit the current state of the
107 * filesystem to disk. The journal thread is responsible for writing
108 * all of the metadata buffers to disk.
109 *
110 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
111 * of the data in that part of the log has been rewritten elsewhere on
112 * the disk. Flushing these old buffers to reclaim space in the log is
113 * known as checkpointing, and this thread is responsible for that job.
114 */
115
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700116static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700117{
118 journal_t *journal = arg;
119 transaction_t *transaction;
120
121 /*
122 * Set up an interval timer which can be used to trigger a commit wakeup
123 * after the commit interval expires
124 */
125 setup_timer(&journal->j_commit_timer, commit_timeout,
126 (unsigned long)current);
127
128 /* Record that the journal thread is running */
129 journal->j_task = current;
130 wake_up(&journal->j_wait_done_commit);
131
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700132 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700133 journal->j_commit_interval / HZ);
134
135 /*
136 * And now, wait forever for commit wakeup events.
137 */
138 spin_lock(&journal->j_state_lock);
139
140loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700141 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142 goto end_loop;
143
144 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
145 journal->j_commit_sequence, journal->j_commit_request);
146
147 if (journal->j_commit_sequence != journal->j_commit_request) {
148 jbd_debug(1, "OK, requests differ\n");
149 spin_unlock(&journal->j_state_lock);
150 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700151 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700152 spin_lock(&journal->j_state_lock);
153 goto loop;
154 }
155
156 wake_up(&journal->j_wait_done_commit);
157 if (freezing(current)) {
158 /*
159 * The simpler the better. Flushing journal isn't a
160 * good idea, because that depends on threads that may
161 * be already stopped.
162 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700163 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700164 spin_unlock(&journal->j_state_lock);
165 refrigerator();
166 spin_lock(&journal->j_state_lock);
167 } else {
168 /*
169 * We assume on resume that commits are already there,
170 * so we don't sleep
171 */
172 DEFINE_WAIT(wait);
173 int should_sleep = 1;
174
175 prepare_to_wait(&journal->j_wait_commit, &wait,
176 TASK_INTERRUPTIBLE);
177 if (journal->j_commit_sequence != journal->j_commit_request)
178 should_sleep = 0;
179 transaction = journal->j_running_transaction;
180 if (transaction && time_after_eq(jiffies,
181 transaction->t_expires))
182 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700183 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700184 should_sleep = 0;
185 if (should_sleep) {
186 spin_unlock(&journal->j_state_lock);
187 schedule();
188 spin_lock(&journal->j_state_lock);
189 }
190 finish_wait(&journal->j_wait_commit, &wait);
191 }
192
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700193 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700194
195 /*
196 * Were we woken up by a commit wakeup event?
197 */
198 transaction = journal->j_running_transaction;
199 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
200 journal->j_commit_request = transaction->t_tid;
201 jbd_debug(1, "woke because of timeout\n");
202 }
203 goto loop;
204
205end_loop:
206 spin_unlock(&journal->j_state_lock);
207 del_timer_sync(&journal->j_commit_timer);
208 journal->j_task = NULL;
209 wake_up(&journal->j_wait_done_commit);
210 jbd_debug(1, "Journal thread exiting.\n");
211 return 0;
212}
213
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700214static void jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700215{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700216 kthread_run(kjournald2, journal, "kjournald2");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700217 wait_event(journal->j_wait_done_commit, journal->j_task != 0);
218}
219
220static void journal_kill_thread(journal_t *journal)
221{
222 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700223 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700224
225 while (journal->j_task) {
226 wake_up(&journal->j_wait_commit);
227 spin_unlock(&journal->j_state_lock);
228 wait_event(journal->j_wait_done_commit, journal->j_task == 0);
229 spin_lock(&journal->j_state_lock);
230 }
231 spin_unlock(&journal->j_state_lock);
232}
233
234/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700235 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700236 *
237 * Writes a metadata buffer to a given disk block. The actual IO is not
238 * performed but a new buffer_head is constructed which labels the data
239 * to be written with the correct destination disk block.
240 *
241 * Any magic-number escaping which needs to be done will cause a
242 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700243 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244 * magic number is only written to the log for descripter blocks. In
245 * this case, we copy the data and replace the first word with 0, and we
246 * return a result code which indicates that this buffer needs to be
247 * marked as an escaped buffer in the corresponding log descriptor
248 * block. The missing word can then be restored when the block is read
249 * during recovery.
250 *
251 * If the source buffer has already been modified by a new transaction
252 * since we took the last commit snapshot, we use the frozen copy of
253 * that data for IO. If we end up using the existing buffer_head's data
254 * for the write, then we *have* to lock the buffer to prevent anyone
255 * else from using and possibly modifying it while the IO is in
256 * progress.
257 *
258 * The function returns a pointer to the buffer_heads to be used for IO.
259 *
260 * We assume that the journal has already been locked in this function.
261 *
262 * Return value:
263 * <0: Error
264 * >=0: Finished OK
265 *
266 * On success:
267 * Bit 0 set == escape performed on the data
268 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
269 */
270
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700271int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700272 struct journal_head *jh_in,
273 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700274 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700275{
276 int need_copy_out = 0;
277 int done_copy_out = 0;
278 int do_escape = 0;
279 char *mapped_data;
280 struct buffer_head *new_bh;
281 struct journal_head *new_jh;
282 struct page *new_page;
283 unsigned int new_offset;
284 struct buffer_head *bh_in = jh2bh(jh_in);
285
286 /*
287 * The buffer really shouldn't be locked: only the current committing
288 * transaction is allowed to write it, so nobody else is allowed
289 * to do any IO.
290 *
291 * akpm: except if we're journalling data, and write() output is
292 * also part of a shared mapping, and another thread has
293 * decided to launch a writepage() against this buffer.
294 */
295 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
296
297 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
298
299 /*
300 * If a new transaction has already done a buffer copy-out, then
301 * we use that version of the data for the commit.
302 */
303 jbd_lock_bh_state(bh_in);
304repeat:
305 if (jh_in->b_frozen_data) {
306 done_copy_out = 1;
307 new_page = virt_to_page(jh_in->b_frozen_data);
308 new_offset = offset_in_page(jh_in->b_frozen_data);
309 } else {
310 new_page = jh2bh(jh_in)->b_page;
311 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
312 }
313
314 mapped_data = kmap_atomic(new_page, KM_USER0);
315 /*
316 * Check for escaping
317 */
318 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700319 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700320 need_copy_out = 1;
321 do_escape = 1;
322 }
323 kunmap_atomic(mapped_data, KM_USER0);
324
325 /*
326 * Do we need to do a data copy?
327 */
328 if (need_copy_out && !done_copy_out) {
329 char *tmp;
330
331 jbd_unlock_bh_state(bh_in);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700332 tmp = jbd2_slab_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700333 jbd_lock_bh_state(bh_in);
334 if (jh_in->b_frozen_data) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700335 jbd2_slab_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700336 goto repeat;
337 }
338
339 jh_in->b_frozen_data = tmp;
340 mapped_data = kmap_atomic(new_page, KM_USER0);
341 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
342 kunmap_atomic(mapped_data, KM_USER0);
343
344 new_page = virt_to_page(tmp);
345 new_offset = offset_in_page(tmp);
346 done_copy_out = 1;
347 }
348
349 /*
350 * Did we need to do an escaping? Now we've done all the
351 * copying, we can finally do so.
352 */
353 if (do_escape) {
354 mapped_data = kmap_atomic(new_page, KM_USER0);
355 *((unsigned int *)(mapped_data + new_offset)) = 0;
356 kunmap_atomic(mapped_data, KM_USER0);
357 }
358
359 /* keep subsequent assertions sane */
360 new_bh->b_state = 0;
361 init_buffer(new_bh, NULL, NULL);
362 atomic_set(&new_bh->b_count, 1);
363 jbd_unlock_bh_state(bh_in);
364
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700365 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700366
367 set_bh_page(new_bh, new_page, new_offset);
368 new_jh->b_transaction = NULL;
369 new_bh->b_size = jh2bh(jh_in)->b_size;
370 new_bh->b_bdev = transaction->t_journal->j_dev;
371 new_bh->b_blocknr = blocknr;
372 set_buffer_mapped(new_bh);
373 set_buffer_dirty(new_bh);
374
375 *jh_out = new_jh;
376
377 /*
378 * The to-be-written buffer needs to get moved to the io queue,
379 * and the original buffer whose contents we are shadowing or
380 * copying is moved to the transaction's shadow queue.
381 */
382 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700383 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700384 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700385 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700386
387 return do_escape | (done_copy_out << 1);
388}
389
390/*
391 * Allocation code for the journal file. Manage the space left in the
392 * journal, so that we can begin checkpointing when appropriate.
393 */
394
395/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700396 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700397 *
398 * Called with the journal already locked.
399 *
400 * Called under j_state_lock
401 */
402
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700403int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404{
405 int left = journal->j_free;
406
407 assert_spin_locked(&journal->j_state_lock);
408
409 /*
410 * Be pessimistic here about the number of those free blocks which
411 * might be required for log descriptor control blocks.
412 */
413
414#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
415
416 left -= MIN_LOG_RESERVED_BLOCKS;
417
418 if (left <= 0)
419 return 0;
420 left -= (left >> 3);
421 return left;
422}
423
424/*
425 * Called under j_state_lock. Returns true if a transaction was started.
426 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700427int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700428{
429 /*
430 * Are we already doing a recent enough commit?
431 */
432 if (!tid_geq(journal->j_commit_request, target)) {
433 /*
434 * We want a new commit: OK, mark the request and wakup the
435 * commit thread. We do _not_ do the commit ourselves.
436 */
437
438 journal->j_commit_request = target;
439 jbd_debug(1, "JBD: requesting commit %d/%d\n",
440 journal->j_commit_request,
441 journal->j_commit_sequence);
442 wake_up(&journal->j_wait_commit);
443 return 1;
444 }
445 return 0;
446}
447
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700448int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700449{
450 int ret;
451
452 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700453 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700454 spin_unlock(&journal->j_state_lock);
455 return ret;
456}
457
458/*
459 * Force and wait upon a commit if the calling process is not within
460 * transaction. This is used for forcing out undo-protected data which contains
461 * bitmaps, when the fs is running out of space.
462 *
463 * We can only force the running transaction if we don't have an active handle;
464 * otherwise, we will deadlock.
465 *
466 * Returns true if a transaction was started.
467 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700468int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700469{
470 transaction_t *transaction = NULL;
471 tid_t tid;
472
473 spin_lock(&journal->j_state_lock);
474 if (journal->j_running_transaction && !current->journal_info) {
475 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477 } else if (journal->j_committing_transaction)
478 transaction = journal->j_committing_transaction;
479
480 if (!transaction) {
481 spin_unlock(&journal->j_state_lock);
482 return 0; /* Nothing to retry */
483 }
484
485 tid = transaction->t_tid;
486 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700487 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700488 return 1;
489}
490
491/*
492 * Start a commit of the current running transaction (if any). Returns true
493 * if a transaction was started, and fills its tid in at *ptid
494 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700495int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496{
497 int ret = 0;
498
499 spin_lock(&journal->j_state_lock);
500 if (journal->j_running_transaction) {
501 tid_t tid = journal->j_running_transaction->t_tid;
502
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504 if (ret && ptid)
505 *ptid = tid;
506 } else if (journal->j_committing_transaction && ptid) {
507 /*
508 * If ext3_write_super() recently started a commit, then we
509 * have to wait for completion of that transaction
510 */
511 *ptid = journal->j_committing_transaction->t_tid;
512 ret = 1;
513 }
514 spin_unlock(&journal->j_state_lock);
515 return ret;
516}
517
518/*
519 * Wait for a specified commit to complete.
520 * The caller may not hold the journal lock.
521 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700522int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523{
524 int err = 0;
525
526#ifdef CONFIG_JBD_DEBUG
527 spin_lock(&journal->j_state_lock);
528 if (!tid_geq(journal->j_commit_request, tid)) {
529 printk(KERN_EMERG
530 "%s: error: j_commit_request=%d, tid=%d\n",
531 __FUNCTION__, journal->j_commit_request, tid);
532 }
533 spin_unlock(&journal->j_state_lock);
534#endif
535 spin_lock(&journal->j_state_lock);
536 while (tid_gt(tid, journal->j_commit_sequence)) {
537 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
538 tid, journal->j_commit_sequence);
539 wake_up(&journal->j_wait_commit);
540 spin_unlock(&journal->j_state_lock);
541 wait_event(journal->j_wait_done_commit,
542 !tid_gt(tid, journal->j_commit_sequence));
543 spin_lock(&journal->j_state_lock);
544 }
545 spin_unlock(&journal->j_state_lock);
546
547 if (unlikely(is_journal_aborted(journal))) {
548 printk(KERN_EMERG "journal commit I/O error\n");
549 err = -EIO;
550 }
551 return err;
552}
553
554/*
555 * Log buffer allocation routines:
556 */
557
Mingming Cao18eba7a2006-10-11 01:21:13 -0700558int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559{
560 unsigned long blocknr;
561
562 spin_lock(&journal->j_state_lock);
563 J_ASSERT(journal->j_free > 1);
564
565 blocknr = journal->j_head;
566 journal->j_head++;
567 journal->j_free--;
568 if (journal->j_head == journal->j_last)
569 journal->j_head = journal->j_first;
570 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700571 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700572}
573
574/*
575 * Conversion of logical to physical block numbers for the journal
576 *
577 * On external journals the journal blocks are identity-mapped, so
578 * this is a no-op. If needed, we can use j_blk_offset - everything is
579 * ready.
580 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700581int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700582 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700583{
584 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700585 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586
587 if (journal->j_inode) {
588 ret = bmap(journal->j_inode, blocknr);
589 if (ret)
590 *retp = ret;
591 else {
592 char b[BDEVNAME_SIZE];
593
594 printk(KERN_ALERT "%s: journal block not found "
595 "at offset %lu on %s\n",
596 __FUNCTION__,
597 blocknr,
598 bdevname(journal->j_dev, b));
599 err = -EIO;
600 __journal_abort_soft(journal, err);
601 }
602 } else {
603 *retp = blocknr; /* +journal->j_blk_offset */
604 }
605 return err;
606}
607
608/*
609 * We play buffer_head aliasing tricks to write data/metadata blocks to
610 * the journal without copying their contents, but for journal
611 * descriptor blocks we do need to generate bona fide buffers.
612 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700613 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
615 * But we don't bother doing that, so there will be coherency problems with
616 * mmaps of blockdevs which hold live JBD-controlled filesystems.
617 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700618struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619{
620 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700621 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622 int err;
623
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700624 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625
626 if (err)
627 return NULL;
628
629 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
630 lock_buffer(bh);
631 memset(bh->b_data, 0, journal->j_blocksize);
632 set_buffer_uptodate(bh);
633 unlock_buffer(bh);
634 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700635 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700636}
637
638/*
639 * Management for journal control blocks: functions to create and
640 * destroy journal_t structures, and to initialise and read existing
641 * journal blocks from disk. */
642
643/* First: create and setup a journal_t object in memory. We initialise
644 * very few fields yet: that has to wait until we have created the
645 * journal structures from from scratch, or loaded them from disk. */
646
647static journal_t * journal_init_common (void)
648{
649 journal_t *journal;
650 int err;
651
652 journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL);
653 if (!journal)
654 goto fail;
655 memset(journal, 0, sizeof(*journal));
656
657 init_waitqueue_head(&journal->j_wait_transaction_locked);
658 init_waitqueue_head(&journal->j_wait_logspace);
659 init_waitqueue_head(&journal->j_wait_done_commit);
660 init_waitqueue_head(&journal->j_wait_checkpoint);
661 init_waitqueue_head(&journal->j_wait_commit);
662 init_waitqueue_head(&journal->j_wait_updates);
663 mutex_init(&journal->j_barrier);
664 mutex_init(&journal->j_checkpoint_mutex);
665 spin_lock_init(&journal->j_revoke_lock);
666 spin_lock_init(&journal->j_list_lock);
667 spin_lock_init(&journal->j_state_lock);
668
669 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
670
671 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700672 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700673
674 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700675 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700676 if (err) {
677 kfree(journal);
678 goto fail;
679 }
680 return journal;
681fail:
682 return NULL;
683}
684
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700685/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700686 *
687 * Create a journal structure assigned some fixed set of disk blocks to
688 * the journal. We don't actually touch those disk blocks yet, but we
689 * need to set up all of the mapping information to tell the journaling
690 * system where the journal blocks are.
691 *
692 */
693
694/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700695 * journal_t * jbd2_journal_init_dev() - creates an initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700696 * @bdev: Block device on which to create the journal
697 * @fs_dev: Device which hold journalled filesystem for this journal.
698 * @start: Block nr Start of journal.
699 * @len: Length of the journal in blocks.
700 * @blocksize: blocksize of journalling device
701 * @returns: a newly created journal_t *
702 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700703 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700704 * range of blocks on an arbitrary block device.
705 *
706 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700707journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700708 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700709 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700710{
711 journal_t *journal = journal_init_common();
712 struct buffer_head *bh;
713 int n;
714
715 if (!journal)
716 return NULL;
717
718 /* journal descriptor can store up to n blocks -bzzz */
719 journal->j_blocksize = blocksize;
720 n = journal->j_blocksize / sizeof(journal_block_tag_t);
721 journal->j_wbufsize = n;
722 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
723 if (!journal->j_wbuf) {
724 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
725 __FUNCTION__);
726 kfree(journal);
727 journal = NULL;
Dave Kleikamp5eb30792006-10-17 00:09:35 -0700728 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700729 }
730 journal->j_dev = bdev;
731 journal->j_fs_dev = fs_dev;
732 journal->j_blk_offset = start;
733 journal->j_maxlen = len;
734
735 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
736 J_ASSERT(bh != NULL);
737 journal->j_sb_buffer = bh;
738 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Dave Kleikamp5eb30792006-10-17 00:09:35 -0700739out:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700740 return journal;
741}
742
743/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700744 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700745 * @inode: An inode to create the journal in
746 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700747 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700748 * the journal. The inode must exist already, must support bmap() and
749 * must have all data blocks preallocated.
750 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700751journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700752{
753 struct buffer_head *bh;
754 journal_t *journal = journal_init_common();
755 int err;
756 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700757 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700758
759 if (!journal)
760 return NULL;
761
762 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
763 journal->j_inode = inode;
764 jbd_debug(1,
765 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
766 journal, inode->i_sb->s_id, inode->i_ino,
767 (long long) inode->i_size,
768 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
769
770 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
771 journal->j_blocksize = inode->i_sb->s_blocksize;
772
773 /* journal descriptor can store up to n blocks -bzzz */
774 n = journal->j_blocksize / sizeof(journal_block_tag_t);
775 journal->j_wbufsize = n;
776 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
777 if (!journal->j_wbuf) {
778 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
779 __FUNCTION__);
780 kfree(journal);
781 return NULL;
782 }
783
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700784 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700785 /* If that failed, give up */
786 if (err) {
787 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
788 __FUNCTION__);
789 kfree(journal);
790 return NULL;
791 }
792
793 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
794 J_ASSERT(bh != NULL);
795 journal->j_sb_buffer = bh;
796 journal->j_superblock = (journal_superblock_t *)bh->b_data;
797
798 return journal;
799}
800
801/*
802 * If the journal init or create aborts, we need to mark the journal
803 * superblock as being NULL to prevent the journal destroy from writing
804 * back a bogus superblock.
805 */
806static void journal_fail_superblock (journal_t *journal)
807{
808 struct buffer_head *bh = journal->j_sb_buffer;
809 brelse(bh);
810 journal->j_sb_buffer = NULL;
811}
812
813/*
814 * Given a journal_t structure, initialise the various fields for
815 * startup of a new journaling session. We use this both when creating
816 * a journal, and after recovering an old journal to reset it for
817 * subsequent use.
818 */
819
820static int journal_reset(journal_t *journal)
821{
822 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700823 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700824
825 first = be32_to_cpu(sb->s_first);
826 last = be32_to_cpu(sb->s_maxlen);
827
828 journal->j_first = first;
829 journal->j_last = last;
830
831 journal->j_head = first;
832 journal->j_tail = first;
833 journal->j_free = last - first;
834
835 journal->j_tail_sequence = journal->j_transaction_sequence;
836 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
837 journal->j_commit_request = journal->j_commit_sequence;
838
839 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
840
841 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700842 jbd2_journal_update_superblock(journal, 1);
843 jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700844 return 0;
845}
846
847/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700848 * int jbd2_journal_create() - Initialise the new journal file
Dave Kleikamp470decc2006-10-11 01:20:57 -0700849 * @journal: Journal to create. This structure must have been initialised
850 *
851 * Given a journal_t structure which tells us which disk blocks we can
852 * use, create a new journal superblock and initialise all of the
853 * journal fields from scratch.
854 **/
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700855int jbd2_journal_create(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700856{
Mingming Cao18eba7a2006-10-11 01:21:13 -0700857 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700858 struct buffer_head *bh;
859 journal_superblock_t *sb;
860 int i, err;
861
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700862 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700863 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
864 journal->j_maxlen);
865 journal_fail_superblock(journal);
866 return -EINVAL;
867 }
868
869 if (journal->j_inode == NULL) {
870 /*
871 * We don't know what block to start at!
872 */
873 printk(KERN_EMERG
874 "%s: creation of journal on external device!\n",
875 __FUNCTION__);
876 BUG();
877 }
878
879 /* Zero out the entire journal on disk. We cannot afford to
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700880 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700881 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
882 for (i = 0; i < journal->j_maxlen; i++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700883 err = jbd2_journal_bmap(journal, i, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700884 if (err)
885 return err;
886 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
887 lock_buffer(bh);
888 memset (bh->b_data, 0, journal->j_blocksize);
889 BUFFER_TRACE(bh, "marking dirty");
890 mark_buffer_dirty(bh);
891 BUFFER_TRACE(bh, "marking uptodate");
892 set_buffer_uptodate(bh);
893 unlock_buffer(bh);
894 __brelse(bh);
895 }
896
897 sync_blockdev(journal->j_dev);
898 jbd_debug(1, "JBD: journal cleared.\n");
899
900 /* OK, fill in the initial static fields in the new superblock */
901 sb = journal->j_superblock;
902
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700903 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
904 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700905
906 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
907 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
908 sb->s_first = cpu_to_be32(1);
909
910 journal->j_transaction_sequence = 1;
911
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700912 journal->j_flags &= ~JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700913 journal->j_format_version = 2;
914
915 return journal_reset(journal);
916}
917
918/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700919 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700920 * @journal: The journal to update.
921 * @wait: Set to '0' if you don't want to wait for IO completion.
922 *
923 * Update a journal's dynamic superblock fields and write it to disk,
924 * optionally waiting for the IO to complete.
925 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700926void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700927{
928 journal_superblock_t *sb = journal->j_superblock;
929 struct buffer_head *bh = journal->j_sb_buffer;
930
931 /*
932 * As a special case, if the on-disk copy is already marked as needing
933 * no recovery (s_start == 0) and there are no outstanding transactions
934 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700935 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -0700936 * attempting a write to a potential-readonly device.
937 */
938 if (sb->s_start == 0 && journal->j_tail_sequence ==
939 journal->j_transaction_sequence) {
940 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
941 "(start %ld, seq %d, errno %d)\n",
942 journal->j_tail, journal->j_tail_sequence,
943 journal->j_errno);
944 goto out;
945 }
946
947 spin_lock(&journal->j_state_lock);
948 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
949 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
950
951 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
952 sb->s_start = cpu_to_be32(journal->j_tail);
953 sb->s_errno = cpu_to_be32(journal->j_errno);
954 spin_unlock(&journal->j_state_lock);
955
956 BUFFER_TRACE(bh, "marking dirty");
957 mark_buffer_dirty(bh);
958 if (wait)
959 sync_dirty_buffer(bh);
960 else
961 ll_rw_block(SWRITE, 1, &bh);
962
963out:
964 /* If we have just flushed the log (by marking s_start==0), then
965 * any future commit will have to be careful to update the
966 * superblock again to re-record the true start of the log. */
967
968 spin_lock(&journal->j_state_lock);
969 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700970 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700971 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700972 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700973 spin_unlock(&journal->j_state_lock);
974}
975
976/*
977 * Read the superblock for a given journal, performing initial
978 * validation of the format.
979 */
980
981static int journal_get_superblock(journal_t *journal)
982{
983 struct buffer_head *bh;
984 journal_superblock_t *sb;
985 int err = -EIO;
986
987 bh = journal->j_sb_buffer;
988
989 J_ASSERT(bh != NULL);
990 if (!buffer_uptodate(bh)) {
991 ll_rw_block(READ, 1, &bh);
992 wait_on_buffer(bh);
993 if (!buffer_uptodate(bh)) {
994 printk (KERN_ERR
995 "JBD: IO error reading journal superblock\n");
996 goto out;
997 }
998 }
999
1000 sb = journal->j_superblock;
1001
1002 err = -EINVAL;
1003
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001004 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001005 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1006 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1007 goto out;
1008 }
1009
1010 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001011 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001012 journal->j_format_version = 1;
1013 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001014 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015 journal->j_format_version = 2;
1016 break;
1017 default:
1018 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1019 goto out;
1020 }
1021
1022 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1023 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1024 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1025 printk (KERN_WARNING "JBD: journal file too short\n");
1026 goto out;
1027 }
1028
1029 return 0;
1030
1031out:
1032 journal_fail_superblock(journal);
1033 return err;
1034}
1035
1036/*
1037 * Load the on-disk journal superblock and read the key fields into the
1038 * journal_t.
1039 */
1040
1041static int load_superblock(journal_t *journal)
1042{
1043 int err;
1044 journal_superblock_t *sb;
1045
1046 err = journal_get_superblock(journal);
1047 if (err)
1048 return err;
1049
1050 sb = journal->j_superblock;
1051
1052 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1053 journal->j_tail = be32_to_cpu(sb->s_start);
1054 journal->j_first = be32_to_cpu(sb->s_first);
1055 journal->j_last = be32_to_cpu(sb->s_maxlen);
1056 journal->j_errno = be32_to_cpu(sb->s_errno);
1057
1058 return 0;
1059}
1060
1061
1062/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001063 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001064 * @journal: Journal to act on.
1065 *
1066 * Given a journal_t structure which tells us which disk blocks contain
1067 * a journal, read the journal from disk to initialise the in-memory
1068 * structures.
1069 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001070int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001071{
1072 int err;
1073 journal_superblock_t *sb;
1074
1075 err = load_superblock(journal);
1076 if (err)
1077 return err;
1078
1079 sb = journal->j_superblock;
1080 /* If this is a V2 superblock, then we have to check the
1081 * features flags on it. */
1082
1083 if (journal->j_format_version >= 2) {
1084 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001085 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001086 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001087 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001088 printk (KERN_WARNING
1089 "JBD: Unrecognised features on journal\n");
1090 return -EINVAL;
1091 }
1092 }
1093
1094 /*
1095 * Create a slab for this blocksize
1096 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001097 err = jbd2_journal_create_jbd_slab(be32_to_cpu(sb->s_blocksize));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098 if (err)
1099 return err;
1100
1101 /* Let the recovery code check whether it needs to recover any
1102 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001103 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104 goto recovery_error;
1105
1106 /* OK, we've finished with the dynamic journal bits:
1107 * reinitialise the dynamic contents of the superblock in memory
1108 * and reset them on disk. */
1109 if (journal_reset(journal))
1110 goto recovery_error;
1111
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001112 journal->j_flags &= ~JBD2_ABORT;
1113 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001114 return 0;
1115
1116recovery_error:
1117 printk (KERN_WARNING "JBD: recovery failed\n");
1118 return -EIO;
1119}
1120
1121/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001122 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001123 * @journal: Journal to act on.
1124 *
1125 * Release a journal_t structure once it is no longer in use by the
1126 * journaled object.
1127 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001128void jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001129{
1130 /* Wait for the commit thread to wake up and die. */
1131 journal_kill_thread(journal);
1132
1133 /* Force a final log commit */
1134 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001135 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001136
1137 /* Force any old transactions to disk */
1138
1139 /* Totally anal locking here... */
1140 spin_lock(&journal->j_list_lock);
1141 while (journal->j_checkpoint_transactions != NULL) {
1142 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001143 jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001144 spin_lock(&journal->j_list_lock);
1145 }
1146
1147 J_ASSERT(journal->j_running_transaction == NULL);
1148 J_ASSERT(journal->j_committing_transaction == NULL);
1149 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1150 spin_unlock(&journal->j_list_lock);
1151
1152 /* We can now mark the journal as empty. */
1153 journal->j_tail = 0;
1154 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1155 if (journal->j_sb_buffer) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001156 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001157 brelse(journal->j_sb_buffer);
1158 }
1159
1160 if (journal->j_inode)
1161 iput(journal->j_inode);
1162 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001163 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001164 kfree(journal->j_wbuf);
1165 kfree(journal);
1166}
1167
1168
1169/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001170 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001171 * @journal: Journal to check.
1172 * @compat: bitmask of compatible features
1173 * @ro: bitmask of features that force read-only mount
1174 * @incompat: bitmask of incompatible features
1175 *
1176 * Check whether the journal uses all of a given set of
1177 * features. Return true (non-zero) if it does.
1178 **/
1179
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 unsigned long ro, unsigned long incompat)
1182{
1183 journal_superblock_t *sb;
1184
1185 if (!compat && !ro && !incompat)
1186 return 1;
1187 if (journal->j_format_version == 1)
1188 return 0;
1189
1190 sb = journal->j_superblock;
1191
1192 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1193 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1194 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1195 return 1;
1196
1197 return 0;
1198}
1199
1200/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001201 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001202 * @journal: Journal to check.
1203 * @compat: bitmask of compatible features
1204 * @ro: bitmask of features that force read-only mount
1205 * @incompat: bitmask of incompatible features
1206 *
1207 * Check whether the journaling code supports the use of
1208 * all of a given set of features on this journal. Return true
1209 * (non-zero) if it can. */
1210
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001211int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001212 unsigned long ro, unsigned long incompat)
1213{
1214 journal_superblock_t *sb;
1215
1216 if (!compat && !ro && !incompat)
1217 return 1;
1218
1219 sb = journal->j_superblock;
1220
1221 /* We can support any known requested features iff the
1222 * superblock is in version 2. Otherwise we fail to support any
1223 * extended sb features. */
1224
1225 if (journal->j_format_version != 2)
1226 return 0;
1227
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001228 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1229 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1230 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 return 1;
1232
1233 return 0;
1234}
1235
1236/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001237 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238 * @journal: Journal to act on.
1239 * @compat: bitmask of compatible features
1240 * @ro: bitmask of features that force read-only mount
1241 * @incompat: bitmask of incompatible features
1242 *
1243 * Mark a given journal feature as present on the
1244 * superblock. Returns true if the requested features could be set.
1245 *
1246 */
1247
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001248int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001249 unsigned long ro, unsigned long incompat)
1250{
1251 journal_superblock_t *sb;
1252
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001253 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 return 1;
1255
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001256 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 return 0;
1258
1259 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1260 compat, ro, incompat);
1261
1262 sb = journal->j_superblock;
1263
1264 sb->s_feature_compat |= cpu_to_be32(compat);
1265 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1266 sb->s_feature_incompat |= cpu_to_be32(incompat);
1267
1268 return 1;
1269}
1270
1271
1272/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001273 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001274 * @journal: Journal to act on.
1275 *
1276 * Given an initialised but unloaded journal struct, poke about in the
1277 * on-disk structure to update it to the most recent supported version.
1278 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001279int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001280{
1281 journal_superblock_t *sb;
1282 int err;
1283
1284 err = journal_get_superblock(journal);
1285 if (err)
1286 return err;
1287
1288 sb = journal->j_superblock;
1289
1290 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001291 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001292 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001293 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294 return journal_convert_superblock_v1(journal, sb);
1295 default:
1296 break;
1297 }
1298 return -EINVAL;
1299}
1300
1301static int journal_convert_superblock_v1(journal_t *journal,
1302 journal_superblock_t *sb)
1303{
1304 int offset, blocksize;
1305 struct buffer_head *bh;
1306
1307 printk(KERN_WARNING
1308 "JBD: Converting superblock from version 1 to 2.\n");
1309
1310 /* Pre-initialise new fields to zero */
1311 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1312 blocksize = be32_to_cpu(sb->s_blocksize);
1313 memset(&sb->s_feature_compat, 0, blocksize-offset);
1314
1315 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001316 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001317 journal->j_format_version = 2;
1318
1319 bh = journal->j_sb_buffer;
1320 BUFFER_TRACE(bh, "marking dirty");
1321 mark_buffer_dirty(bh);
1322 sync_dirty_buffer(bh);
1323 return 0;
1324}
1325
1326
1327/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001328 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001329 * @journal: Journal to act on.
1330 *
1331 * Flush all data for a given journal to disk and empty the journal.
1332 * Filesystems can use this when remounting readonly to ensure that
1333 * recovery does not need to happen on remount.
1334 */
1335
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001336int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001337{
1338 int err = 0;
1339 transaction_t *transaction = NULL;
1340 unsigned long old_tail;
1341
1342 spin_lock(&journal->j_state_lock);
1343
1344 /* Force everything buffered to the log... */
1345 if (journal->j_running_transaction) {
1346 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001347 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001348 } else if (journal->j_committing_transaction)
1349 transaction = journal->j_committing_transaction;
1350
1351 /* Wait for the log commit to complete... */
1352 if (transaction) {
1353 tid_t tid = transaction->t_tid;
1354
1355 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001356 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001357 } else {
1358 spin_unlock(&journal->j_state_lock);
1359 }
1360
1361 /* ...and flush everything in the log out to disk. */
1362 spin_lock(&journal->j_list_lock);
1363 while (!err && journal->j_checkpoint_transactions != NULL) {
1364 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001365 err = jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001366 spin_lock(&journal->j_list_lock);
1367 }
1368 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001369 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370
1371 /* Finally, mark the journal as really needing no recovery.
1372 * This sets s_start==0 in the underlying superblock, which is
1373 * the magic code for a fully-recovered superblock. Any future
1374 * commits of data to the journal will restore the current
1375 * s_start value. */
1376 spin_lock(&journal->j_state_lock);
1377 old_tail = journal->j_tail;
1378 journal->j_tail = 0;
1379 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001380 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001381 spin_lock(&journal->j_state_lock);
1382 journal->j_tail = old_tail;
1383
1384 J_ASSERT(!journal->j_running_transaction);
1385 J_ASSERT(!journal->j_committing_transaction);
1386 J_ASSERT(!journal->j_checkpoint_transactions);
1387 J_ASSERT(journal->j_head == journal->j_tail);
1388 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1389 spin_unlock(&journal->j_state_lock);
1390 return err;
1391}
1392
1393/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001394 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395 * @journal: Journal to act on.
1396 * @write: flag (see below)
1397 *
1398 * Wipe out all of the contents of a journal, safely. This will produce
1399 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001400 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001401 *
1402 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1403 * we merely suppress recovery.
1404 */
1405
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001406int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001407{
1408 journal_superblock_t *sb;
1409 int err = 0;
1410
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001411 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001412
1413 err = load_superblock(journal);
1414 if (err)
1415 return err;
1416
1417 sb = journal->j_superblock;
1418
1419 if (!journal->j_tail)
1420 goto no_recovery;
1421
1422 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1423 write ? "Clearing" : "Ignoring");
1424
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001425 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001426 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001427 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428
1429 no_recovery:
1430 return err;
1431}
1432
1433/*
1434 * journal_dev_name: format a character string to describe on what
1435 * device this journal is present.
1436 */
1437
1438static const char *journal_dev_name(journal_t *journal, char *buffer)
1439{
1440 struct block_device *bdev;
1441
1442 if (journal->j_inode)
1443 bdev = journal->j_inode->i_sb->s_bdev;
1444 else
1445 bdev = journal->j_dev;
1446
1447 return bdevname(bdev, buffer);
1448}
1449
1450/*
1451 * Journal abort has very specific semantics, which we describe
1452 * for journal abort.
1453 *
1454 * Two internal function, which provide abort to te jbd layer
1455 * itself are here.
1456 */
1457
1458/*
1459 * Quick version for internal journal use (doesn't lock the journal).
1460 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1461 * and don't attempt to make any other journal updates.
1462 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001463void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001464{
1465 transaction_t *transaction;
1466 char b[BDEVNAME_SIZE];
1467
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001468 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001469 return;
1470
1471 printk(KERN_ERR "Aborting journal on device %s.\n",
1472 journal_dev_name(journal, b));
1473
1474 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001475 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001476 transaction = journal->j_running_transaction;
1477 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001478 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001479 spin_unlock(&journal->j_state_lock);
1480}
1481
1482/* Soft abort: record the abort error status in the journal superblock,
1483 * but don't do any other IO. */
1484static void __journal_abort_soft (journal_t *journal, int errno)
1485{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001486 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487 return;
1488
1489 if (!journal->j_errno)
1490 journal->j_errno = errno;
1491
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001492 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001493
1494 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001495 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001496}
1497
1498/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001499 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500 * @journal: the journal to shutdown.
1501 * @errno: an error number to record in the journal indicating
1502 * the reason for the shutdown.
1503 *
1504 * Perform a complete, immediate shutdown of the ENTIRE
1505 * journal (not of a single transaction). This operation cannot be
1506 * undone without closing and reopening the journal.
1507 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001508 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001509 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1510 * mode.
1511 *
1512 * Journal abort has very specific semantics. Any existing dirty,
1513 * unjournaled buffers in the main filesystem will still be written to
1514 * disk by bdflush, but the journaling mechanism will be suspended
1515 * immediately and no further transaction commits will be honoured.
1516 *
1517 * Any dirty, journaled buffers will be written back to disk without
1518 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1519 * filesystem, but we _do_ attempt to leave as much data as possible
1520 * behind for fsck to use for cleanup.
1521 *
1522 * Any attempt to get a new transaction handle on a journal which is in
1523 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001524 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001525 * entered abort state during the update.
1526 *
1527 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001528 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001529 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001530 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001531 * which will be recorded (if possible) in the journal superblock. This
1532 * allows a client to record failure conditions in the middle of a
1533 * transaction without having to complete the transaction to record the
1534 * failure to disk. ext3_error, for example, now uses this
1535 * functionality.
1536 *
1537 * Errors which originate from within the journaling layer will NOT
1538 * supply an errno; a null errno implies that absolutely no further
1539 * writes are done to the journal (unless there are any already in
1540 * progress).
1541 *
1542 */
1543
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001544void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001545{
1546 __journal_abort_soft(journal, errno);
1547}
1548
1549/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001550 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001551 * @journal: journal to examine.
1552 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001553 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001554 * time the journal was mounted - if the journal was stopped
1555 * without calling abort this will be 0.
1556 *
1557 * If the journal has been aborted on this mount time -EROFS will
1558 * be returned.
1559 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001560int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001561{
1562 int err;
1563
1564 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001565 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001566 err = -EROFS;
1567 else
1568 err = journal->j_errno;
1569 spin_unlock(&journal->j_state_lock);
1570 return err;
1571}
1572
1573/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001574 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001575 * @journal: journal to act on.
1576 *
1577 * An error must be cleared or Acked to take a FS out of readonly
1578 * mode.
1579 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001580int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001581{
1582 int err = 0;
1583
1584 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001585 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001586 err = -EROFS;
1587 else
1588 journal->j_errno = 0;
1589 spin_unlock(&journal->j_state_lock);
1590 return err;
1591}
1592
1593/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001594 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001595 * @journal: journal to act on.
1596 *
1597 * An error must be cleared or Acked to take a FS out of readonly
1598 * mode.
1599 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601{
1602 spin_lock(&journal->j_state_lock);
1603 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001604 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001605 spin_unlock(&journal->j_state_lock);
1606}
1607
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001608int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609{
1610 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1611}
1612
1613/*
Zach Brownb517bea2006-10-11 01:21:08 -07001614 * helper functions to deal with 32 or 64bit block numbers.
1615 */
1616size_t journal_tag_bytes(journal_t *journal)
1617{
1618 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1619 return JBD_TAG_SIZE64;
1620 else
1621 return JBD_TAG_SIZE32;
1622}
1623
1624/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001625 * Simple support for retrying memory allocations. Introduced to help to
1626 * debug different VM deadlock avoidance strategies.
1627 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001628void * __jbd2_kmalloc (const char *where, size_t size, gfp_t flags, int retry)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001629{
1630 return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0));
1631}
1632
1633/*
1634 * jbd slab management: create 1k, 2k, 4k, 8k slabs as needed
1635 * and allocate frozen and commit buffers from these slabs.
1636 *
1637 * Reason for doing this is to avoid, SLAB_DEBUG - since it could
1638 * cause bh to cross page boundary.
1639 */
1640
1641#define JBD_MAX_SLABS 5
1642#define JBD_SLAB_INDEX(size) (size >> 11)
1643
1644static kmem_cache_t *jbd_slab[JBD_MAX_SLABS];
1645static const char *jbd_slab_names[JBD_MAX_SLABS] = {
Johann Lombardia920e942006-10-11 01:21:00 -07001646 "jbd2_1k", "jbd2_2k", "jbd2_4k", NULL, "jbd2_8k"
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647};
1648
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001649static void jbd2_journal_destroy_jbd_slabs(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650{
1651 int i;
1652
1653 for (i = 0; i < JBD_MAX_SLABS; i++) {
1654 if (jbd_slab[i])
1655 kmem_cache_destroy(jbd_slab[i]);
1656 jbd_slab[i] = NULL;
1657 }
1658}
1659
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001660static int jbd2_journal_create_jbd_slab(size_t slab_size)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001661{
1662 int i = JBD_SLAB_INDEX(slab_size);
1663
1664 BUG_ON(i >= JBD_MAX_SLABS);
1665
1666 /*
1667 * Check if we already have a slab created for this size
1668 */
1669 if (jbd_slab[i])
1670 return 0;
1671
1672 /*
1673 * Create a slab and force alignment to be same as slabsize -
1674 * this will make sure that allocations won't cross the page
1675 * boundary.
1676 */
1677 jbd_slab[i] = kmem_cache_create(jbd_slab_names[i],
1678 slab_size, slab_size, 0, NULL, NULL);
1679 if (!jbd_slab[i]) {
1680 printk(KERN_EMERG "JBD: no memory for jbd_slab cache\n");
1681 return -ENOMEM;
1682 }
1683 return 0;
1684}
1685
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001686void * jbd2_slab_alloc(size_t size, gfp_t flags)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001687{
1688 int idx;
1689
1690 idx = JBD_SLAB_INDEX(size);
1691 BUG_ON(jbd_slab[idx] == NULL);
1692 return kmem_cache_alloc(jbd_slab[idx], flags | __GFP_NOFAIL);
1693}
1694
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001695void jbd2_slab_free(void *ptr, size_t size)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001696{
1697 int idx;
1698
1699 idx = JBD_SLAB_INDEX(size);
1700 BUG_ON(jbd_slab[idx] == NULL);
1701 kmem_cache_free(jbd_slab[idx], ptr);
1702}
1703
1704/*
1705 * Journal_head storage management
1706 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001707static kmem_cache_t *jbd2_journal_head_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001708#ifdef CONFIG_JBD_DEBUG
1709static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1710#endif
1711
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001712static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713{
1714 int retval;
1715
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001716 J_ASSERT(jbd2_journal_head_cache == 0);
Johann Lombardia920e942006-10-11 01:21:00 -07001717 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718 sizeof(struct journal_head),
1719 0, /* offset */
1720 0, /* flags */
1721 NULL, /* ctor */
1722 NULL); /* dtor */
1723 retval = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001724 if (jbd2_journal_head_cache == 0) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001725 retval = -ENOMEM;
1726 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1727 }
1728 return retval;
1729}
1730
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001733 J_ASSERT(jbd2_journal_head_cache != NULL);
1734 kmem_cache_destroy(jbd2_journal_head_cache);
1735 jbd2_journal_head_cache = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001736}
1737
1738/*
1739 * journal_head splicing and dicing
1740 */
1741static struct journal_head *journal_alloc_journal_head(void)
1742{
1743 struct journal_head *ret;
1744 static unsigned long last_warning;
1745
1746#ifdef CONFIG_JBD_DEBUG
1747 atomic_inc(&nr_journal_heads);
1748#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001749 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750 if (ret == 0) {
1751 jbd_debug(1, "out of memory for journal_head\n");
1752 if (time_after(jiffies, last_warning + 5*HZ)) {
1753 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1754 __FUNCTION__);
1755 last_warning = jiffies;
1756 }
1757 while (ret == 0) {
1758 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001759 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760 }
1761 }
1762 return ret;
1763}
1764
1765static void journal_free_journal_head(struct journal_head *jh)
1766{
1767#ifdef CONFIG_JBD_DEBUG
1768 atomic_dec(&nr_journal_heads);
1769 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1770#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001771 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001772}
1773
1774/*
1775 * A journal_head is attached to a buffer_head whenever JBD has an
1776 * interest in the buffer.
1777 *
1778 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1779 * is set. This bit is tested in core kernel code where we need to take
1780 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1781 * there.
1782 *
1783 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1784 *
1785 * When a buffer has its BH_JBD bit set it is immune from being released by
1786 * core kernel code, mainly via ->b_count.
1787 *
1788 * A journal_head may be detached from its buffer_head when the journal_head's
1789 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001790 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 * journal_head can be dropped if needed.
1792 *
1793 * Various places in the kernel want to attach a journal_head to a buffer_head
1794 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001797 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 *
1799 * So the typical usage would be:
1800 *
1801 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001802 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001803 * ...
1804 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001805 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806 *
1807 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1808 * because it has a non-zero b_transaction.
1809 */
1810
1811/*
1812 * Give a buffer_head a journal_head.
1813 *
1814 * Doesn't need the journal lock.
1815 * May sleep.
1816 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001817struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001818{
1819 struct journal_head *jh;
1820 struct journal_head *new_jh = NULL;
1821
1822repeat:
1823 if (!buffer_jbd(bh)) {
1824 new_jh = journal_alloc_journal_head();
1825 memset(new_jh, 0, sizeof(*new_jh));
1826 }
1827
1828 jbd_lock_bh_journal_head(bh);
1829 if (buffer_jbd(bh)) {
1830 jh = bh2jh(bh);
1831 } else {
1832 J_ASSERT_BH(bh,
1833 (atomic_read(&bh->b_count) > 0) ||
1834 (bh->b_page && bh->b_page->mapping));
1835
1836 if (!new_jh) {
1837 jbd_unlock_bh_journal_head(bh);
1838 goto repeat;
1839 }
1840
1841 jh = new_jh;
1842 new_jh = NULL; /* We consumed it */
1843 set_buffer_jbd(bh);
1844 bh->b_private = jh;
1845 jh->b_bh = bh;
1846 get_bh(bh);
1847 BUFFER_TRACE(bh, "added journal_head");
1848 }
1849 jh->b_jcount++;
1850 jbd_unlock_bh_journal_head(bh);
1851 if (new_jh)
1852 journal_free_journal_head(new_jh);
1853 return bh->b_private;
1854}
1855
1856/*
1857 * Grab a ref against this buffer_head's journal_head. If it ended up not
1858 * having a journal_head, return NULL
1859 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001860struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001861{
1862 struct journal_head *jh = NULL;
1863
1864 jbd_lock_bh_journal_head(bh);
1865 if (buffer_jbd(bh)) {
1866 jh = bh2jh(bh);
1867 jh->b_jcount++;
1868 }
1869 jbd_unlock_bh_journal_head(bh);
1870 return jh;
1871}
1872
1873static void __journal_remove_journal_head(struct buffer_head *bh)
1874{
1875 struct journal_head *jh = bh2jh(bh);
1876
1877 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1878
1879 get_bh(bh);
1880 if (jh->b_jcount == 0) {
1881 if (jh->b_transaction == NULL &&
1882 jh->b_next_transaction == NULL &&
1883 jh->b_cp_transaction == NULL) {
1884 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1885 J_ASSERT_BH(bh, buffer_jbd(bh));
1886 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1887 BUFFER_TRACE(bh, "remove journal_head");
1888 if (jh->b_frozen_data) {
1889 printk(KERN_WARNING "%s: freeing "
1890 "b_frozen_data\n",
1891 __FUNCTION__);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001892 jbd2_slab_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893 }
1894 if (jh->b_committed_data) {
1895 printk(KERN_WARNING "%s: freeing "
1896 "b_committed_data\n",
1897 __FUNCTION__);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001898 jbd2_slab_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899 }
1900 bh->b_private = NULL;
1901 jh->b_bh = NULL; /* debug, really */
1902 clear_buffer_jbd(bh);
1903 __brelse(bh);
1904 journal_free_journal_head(jh);
1905 } else {
1906 BUFFER_TRACE(bh, "journal_head was locked");
1907 }
1908 }
1909}
1910
1911/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001912 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 * and has a zero b_jcount then remove and release its journal_head. If we did
1914 * see that the buffer is not used by any transaction we also "logically"
1915 * decrement ->b_count.
1916 *
1917 * We in fact take an additional increment on ->b_count as a convenience,
1918 * because the caller usually wants to do additional things with the bh
1919 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001920 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07001921 * time. Once the caller has run __brelse(), the buffer is eligible for
1922 * reaping by try_to_free_buffers().
1923 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001924void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925{
1926 jbd_lock_bh_journal_head(bh);
1927 __journal_remove_journal_head(bh);
1928 jbd_unlock_bh_journal_head(bh);
1929}
1930
1931/*
1932 * Drop a reference on the passed journal_head. If it fell to zero then try to
1933 * release the journal_head from the buffer_head.
1934 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001935void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936{
1937 struct buffer_head *bh = jh2bh(jh);
1938
1939 jbd_lock_bh_journal_head(bh);
1940 J_ASSERT_JH(jh, jh->b_jcount > 0);
1941 --jh->b_jcount;
1942 if (!jh->b_jcount && !jh->b_transaction) {
1943 __journal_remove_journal_head(bh);
1944 __brelse(bh);
1945 }
1946 jbd_unlock_bh_journal_head(bh);
1947}
1948
1949/*
1950 * /proc tunables
1951 */
1952#if defined(CONFIG_JBD_DEBUG)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001953int jbd2_journal_enable_debug;
1954EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955#endif
1956
1957#if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
1958
1959static struct proc_dir_entry *proc_jbd_debug;
1960
1961static int read_jbd_debug(char *page, char **start, off_t off,
1962 int count, int *eof, void *data)
1963{
1964 int ret;
1965
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966 ret = sprintf(page + off, "%d\n", jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 *eof = 1;
1968 return ret;
1969}
1970
1971static int write_jbd_debug(struct file *file, const char __user *buffer,
1972 unsigned long count, void *data)
1973{
1974 char buf[32];
1975
1976 if (count > ARRAY_SIZE(buf) - 1)
1977 count = ARRAY_SIZE(buf) - 1;
1978 if (copy_from_user(buf, buffer, count))
1979 return -EFAULT;
1980 buf[ARRAY_SIZE(buf) - 1] = '\0';
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001981 jbd2_journal_enable_debug = simple_strtoul(buf, NULL, 10);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001982 return count;
1983}
1984
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001985#define JBD_PROC_NAME "sys/fs/jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07001986
1987static void __init create_jbd_proc_entry(void)
1988{
1989 proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL);
1990 if (proc_jbd_debug) {
1991 /* Why is this so hard? */
1992 proc_jbd_debug->read_proc = read_jbd_debug;
1993 proc_jbd_debug->write_proc = write_jbd_debug;
1994 }
1995}
1996
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001997static void __exit jbd2_remove_jbd_proc_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001998{
1999 if (proc_jbd_debug)
2000 remove_proc_entry(JBD_PROC_NAME, NULL);
2001}
2002
2003#else
2004
2005#define create_jbd_proc_entry() do {} while (0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002006#define jbd2_remove_jbd_proc_entry() do {} while (0)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007
2008#endif
2009
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002010kmem_cache_t *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011
2012static int __init journal_init_handle_cache(void)
2013{
Johann Lombardia920e942006-10-11 01:21:00 -07002014 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002015 sizeof(handle_t),
2016 0, /* offset */
2017 0, /* flags */
2018 NULL, /* ctor */
2019 NULL); /* dtor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002020 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2022 return -ENOMEM;
2023 }
2024 return 0;
2025}
2026
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002029 if (jbd2_handle_cache)
2030 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002031}
2032
2033/*
2034 * Module startup and shutdown
2035 */
2036
2037static int __init journal_init_caches(void)
2038{
2039 int ret;
2040
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002043 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 if (ret == 0)
2045 ret = journal_init_handle_cache();
2046 return ret;
2047}
2048
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002049static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002050{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051 jbd2_journal_destroy_revoke_caches();
2052 jbd2_journal_destroy_jbd2_journal_head_cache();
2053 jbd2_journal_destroy_handle_cache();
2054 jbd2_journal_destroy_jbd_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002055}
2056
2057static int __init journal_init(void)
2058{
2059 int ret;
2060
2061 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2062
2063 ret = journal_init_caches();
2064 if (ret != 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002065 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002066 create_jbd_proc_entry();
2067 return ret;
2068}
2069
2070static void __exit journal_exit(void)
2071{
2072#ifdef CONFIG_JBD_DEBUG
2073 int n = atomic_read(&nr_journal_heads);
2074 if (n)
2075 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2076#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002077 jbd2_remove_jbd_proc_entry();
2078 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002079}
2080
2081MODULE_LICENSE("GPL");
2082module_init(journal_init);
2083module_exit(journal_exit);
2084