blob: 2932c8f55199768c3887663cd264e1a6e5aabf99 [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>
Theodore Ts'od7cfa462008-12-17 00:20:45 -050043#include <asm/div64.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070044
Mingming Caof7f4bcc2006-10-11 01:20:59 -070045EXPORT_SYMBOL(jbd2_journal_start);
46EXPORT_SYMBOL(jbd2_journal_restart);
47EXPORT_SYMBOL(jbd2_journal_extend);
48EXPORT_SYMBOL(jbd2_journal_stop);
49EXPORT_SYMBOL(jbd2_journal_lock_updates);
50EXPORT_SYMBOL(jbd2_journal_unlock_updates);
51EXPORT_SYMBOL(jbd2_journal_get_write_access);
52EXPORT_SYMBOL(jbd2_journal_get_create_access);
53EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070054EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
55EXPORT_SYMBOL(jbd2_journal_release_buffer);
56EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070057#if 0
58EXPORT_SYMBOL(journal_sync_buffer);
59#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070060EXPORT_SYMBOL(jbd2_journal_flush);
61EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_init_dev);
64EXPORT_SYMBOL(jbd2_journal_init_inode);
65EXPORT_SYMBOL(jbd2_journal_update_format);
66EXPORT_SYMBOL(jbd2_journal_check_used_features);
67EXPORT_SYMBOL(jbd2_journal_check_available_features);
68EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070069EXPORT_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
Theodore Ts'o4a9bf992009-01-03 22:56:44 -0500134 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
135 "commit interval %ld seconds\n", current->pid,
136 journal->j_devname, journal->j_commit_interval / HZ);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700137
138 /*
139 * And now, wait forever for commit wakeup events.
140 */
141 spin_lock(&journal->j_state_lock);
142
143loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700144 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700145 goto end_loop;
146
147 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
148 journal->j_commit_sequence, journal->j_commit_request);
149
150 if (journal->j_commit_sequence != journal->j_commit_request) {
151 jbd_debug(1, "OK, requests differ\n");
152 spin_unlock(&journal->j_state_lock);
153 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700154 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700155 spin_lock(&journal->j_state_lock);
156 goto loop;
157 }
158
159 wake_up(&journal->j_wait_done_commit);
160 if (freezing(current)) {
161 /*
162 * The simpler the better. Flushing journal isn't a
163 * good idea, because that depends on threads that may
164 * be already stopped.
165 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700166 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700167 spin_unlock(&journal->j_state_lock);
168 refrigerator();
169 spin_lock(&journal->j_state_lock);
170 } else {
171 /*
172 * We assume on resume that commits are already there,
173 * so we don't sleep
174 */
175 DEFINE_WAIT(wait);
176 int should_sleep = 1;
177
178 prepare_to_wait(&journal->j_wait_commit, &wait,
179 TASK_INTERRUPTIBLE);
180 if (journal->j_commit_sequence != journal->j_commit_request)
181 should_sleep = 0;
182 transaction = journal->j_running_transaction;
183 if (transaction && time_after_eq(jiffies,
184 transaction->t_expires))
185 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700186 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700187 should_sleep = 0;
188 if (should_sleep) {
189 spin_unlock(&journal->j_state_lock);
190 schedule();
191 spin_lock(&journal->j_state_lock);
192 }
193 finish_wait(&journal->j_wait_commit, &wait);
194 }
195
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700196 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197
198 /*
199 * Were we woken up by a commit wakeup event?
200 */
201 transaction = journal->j_running_transaction;
202 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
203 journal->j_commit_request = transaction->t_tid;
204 jbd_debug(1, "woke because of timeout\n");
205 }
206 goto loop;
207
208end_loop:
209 spin_unlock(&journal->j_state_lock);
210 del_timer_sync(&journal->j_commit_timer);
211 journal->j_task = NULL;
212 wake_up(&journal->j_wait_done_commit);
213 jbd_debug(1, "Journal thread exiting.\n");
214 return 0;
215}
216
Pavel Emelianov97f06782007-05-08 00:30:42 -0700217static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700218{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700219 struct task_struct *t;
220
221 t = kthread_run(kjournald2, journal, "kjournald2");
222 if (IS_ERR(t))
223 return PTR_ERR(t);
224
Al Viro1076d172008-03-29 03:07:18 +0000225 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700226 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700227}
228
229static void journal_kill_thread(journal_t *journal)
230{
231 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700232 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700233
234 while (journal->j_task) {
235 wake_up(&journal->j_wait_commit);
236 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000237 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238 spin_lock(&journal->j_state_lock);
239 }
240 spin_unlock(&journal->j_state_lock);
241}
242
243/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700244 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700245 *
246 * Writes a metadata buffer to a given disk block. The actual IO is not
247 * performed but a new buffer_head is constructed which labels the data
248 * to be written with the correct destination disk block.
249 *
250 * Any magic-number escaping which needs to be done will cause a
251 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700252 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700253 * magic number is only written to the log for descripter blocks. In
254 * this case, we copy the data and replace the first word with 0, and we
255 * return a result code which indicates that this buffer needs to be
256 * marked as an escaped buffer in the corresponding log descriptor
257 * block. The missing word can then be restored when the block is read
258 * during recovery.
259 *
260 * If the source buffer has already been modified by a new transaction
261 * since we took the last commit snapshot, we use the frozen copy of
262 * that data for IO. If we end up using the existing buffer_head's data
263 * for the write, then we *have* to lock the buffer to prevent anyone
264 * else from using and possibly modifying it while the IO is in
265 * progress.
266 *
267 * The function returns a pointer to the buffer_heads to be used for IO.
268 *
269 * We assume that the journal has already been locked in this function.
270 *
271 * Return value:
272 * <0: Error
273 * >=0: Finished OK
274 *
275 * On success:
276 * Bit 0 set == escape performed on the data
277 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
278 */
279
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700280int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700281 struct journal_head *jh_in,
282 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700283 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284{
285 int need_copy_out = 0;
286 int done_copy_out = 0;
287 int do_escape = 0;
288 char *mapped_data;
289 struct buffer_head *new_bh;
290 struct journal_head *new_jh;
291 struct page *new_page;
292 unsigned int new_offset;
293 struct buffer_head *bh_in = jh2bh(jh_in);
294
295 /*
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
298 * to do any IO.
299 *
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
303 */
304 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305
306 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
307
308 /*
309 * If a new transaction has already done a buffer copy-out, then
310 * we use that version of the data for the commit.
311 */
312 jbd_lock_bh_state(bh_in);
313repeat:
314 if (jh_in->b_frozen_data) {
315 done_copy_out = 1;
316 new_page = virt_to_page(jh_in->b_frozen_data);
317 new_offset = offset_in_page(jh_in->b_frozen_data);
318 } else {
319 new_page = jh2bh(jh_in)->b_page;
320 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
321 }
322
323 mapped_data = kmap_atomic(new_page, KM_USER0);
324 /*
325 * Check for escaping
326 */
327 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700328 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700329 need_copy_out = 1;
330 do_escape = 1;
331 }
332 kunmap_atomic(mapped_data, KM_USER0);
333
334 /*
335 * Do we need to do a data copy?
336 */
337 if (need_copy_out && !done_copy_out) {
338 char *tmp;
339
340 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400341 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700342 jbd_lock_bh_state(bh_in);
343 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400344 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700345 goto repeat;
346 }
347
348 jh_in->b_frozen_data = tmp;
349 mapped_data = kmap_atomic(new_page, KM_USER0);
350 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
351 kunmap_atomic(mapped_data, KM_USER0);
352
353 new_page = virt_to_page(tmp);
354 new_offset = offset_in_page(tmp);
355 done_copy_out = 1;
356 }
357
358 /*
359 * Did we need to do an escaping? Now we've done all the
360 * copying, we can finally do so.
361 */
362 if (do_escape) {
363 mapped_data = kmap_atomic(new_page, KM_USER0);
364 *((unsigned int *)(mapped_data + new_offset)) = 0;
365 kunmap_atomic(mapped_data, KM_USER0);
366 }
367
368 /* keep subsequent assertions sane */
369 new_bh->b_state = 0;
370 init_buffer(new_bh, NULL, NULL);
371 atomic_set(&new_bh->b_count, 1);
372 jbd_unlock_bh_state(bh_in);
373
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700374 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700375
376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
383
384 *jh_out = new_jh;
385
386 /*
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
390 */
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700392 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700393 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700394 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700395
396 return do_escape | (done_copy_out << 1);
397}
398
399/*
400 * Allocation code for the journal file. Manage the space left in the
401 * journal, so that we can begin checkpointing when appropriate.
402 */
403
404/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700405 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700406 *
407 * Called with the journal already locked.
408 *
409 * Called under j_state_lock
410 */
411
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700412int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700413{
414 int left = journal->j_free;
415
416 assert_spin_locked(&journal->j_state_lock);
417
418 /*
419 * Be pessimistic here about the number of those free blocks which
420 * might be required for log descriptor control blocks.
421 */
422
423#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424
425 left -= MIN_LOG_RESERVED_BLOCKS;
426
427 if (left <= 0)
428 return 0;
429 left -= (left >> 3);
430 return left;
431}
432
433/*
434 * Called under j_state_lock. Returns true if a transaction was started.
435 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700436int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437{
438 /*
439 * Are we already doing a recent enough commit?
440 */
441 if (!tid_geq(journal->j_commit_request, target)) {
442 /*
443 * We want a new commit: OK, mark the request and wakup the
444 * commit thread. We do _not_ do the commit ourselves.
445 */
446
447 journal->j_commit_request = target;
448 jbd_debug(1, "JBD: requesting commit %d/%d\n",
449 journal->j_commit_request,
450 journal->j_commit_sequence);
451 wake_up(&journal->j_wait_commit);
452 return 1;
453 }
454 return 0;
455}
456
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700457int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700458{
459 int ret;
460
461 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700462 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700463 spin_unlock(&journal->j_state_lock);
464 return ret;
465}
466
467/*
468 * Force and wait upon a commit if the calling process is not within
469 * transaction. This is used for forcing out undo-protected data which contains
470 * bitmaps, when the fs is running out of space.
471 *
472 * We can only force the running transaction if we don't have an active handle;
473 * otherwise, we will deadlock.
474 *
475 * Returns true if a transaction was started.
476 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700477int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478{
479 transaction_t *transaction = NULL;
480 tid_t tid;
481
482 spin_lock(&journal->j_state_lock);
483 if (journal->j_running_transaction && !current->journal_info) {
484 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700485 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486 } else if (journal->j_committing_transaction)
487 transaction = journal->j_committing_transaction;
488
489 if (!transaction) {
490 spin_unlock(&journal->j_state_lock);
491 return 0; /* Nothing to retry */
492 }
493
494 tid = transaction->t_tid;
495 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700496 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497 return 1;
498}
499
500/*
501 * Start a commit of the current running transaction (if any). Returns true
502 * if a transaction was started, and fills its tid in at *ptid
503 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700504int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505{
506 int ret = 0;
507
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction) {
510 tid_t tid = journal->j_running_transaction->t_tid;
511
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700512 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700513 if (ret && ptid)
514 *ptid = tid;
515 } else if (journal->j_committing_transaction && ptid) {
516 /*
517 * If ext3_write_super() recently started a commit, then we
518 * have to wait for completion of that transaction
519 */
520 *ptid = journal->j_committing_transaction->t_tid;
521 ret = 1;
522 }
523 spin_unlock(&journal->j_state_lock);
524 return ret;
525}
526
527/*
528 * Wait for a specified commit to complete.
529 * The caller may not hold the journal lock.
530 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700531int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532{
533 int err = 0;
534
Jose R. Santose23291b2007-07-18 08:57:06 -0400535#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 spin_lock(&journal->j_state_lock);
537 if (!tid_geq(journal->j_commit_request, tid)) {
538 printk(KERN_EMERG
539 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400540 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700541 }
542 spin_unlock(&journal->j_state_lock);
543#endif
544 spin_lock(&journal->j_state_lock);
545 while (tid_gt(tid, journal->j_commit_sequence)) {
546 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
547 tid, journal->j_commit_sequence);
548 wake_up(&journal->j_wait_commit);
549 spin_unlock(&journal->j_state_lock);
550 wait_event(journal->j_wait_done_commit,
551 !tid_gt(tid, journal->j_commit_sequence));
552 spin_lock(&journal->j_state_lock);
553 }
554 spin_unlock(&journal->j_state_lock);
555
556 if (unlikely(is_journal_aborted(journal))) {
557 printk(KERN_EMERG "journal commit I/O error\n");
558 err = -EIO;
559 }
560 return err;
561}
562
563/*
564 * Log buffer allocation routines:
565 */
566
Mingming Cao18eba7a2006-10-11 01:21:13 -0700567int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568{
569 unsigned long blocknr;
570
571 spin_lock(&journal->j_state_lock);
572 J_ASSERT(journal->j_free > 1);
573
574 blocknr = journal->j_head;
575 journal->j_head++;
576 journal->j_free--;
577 if (journal->j_head == journal->j_last)
578 journal->j_head = journal->j_first;
579 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700580 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700581}
582
583/*
584 * Conversion of logical to physical block numbers for the journal
585 *
586 * On external journals the journal blocks are identity-mapped, so
587 * this is a no-op. If needed, we can use j_blk_offset - everything is
588 * ready.
589 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700590int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700591 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592{
593 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700594 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595
596 if (journal->j_inode) {
597 ret = bmap(journal->j_inode, blocknr);
598 if (ret)
599 *retp = ret;
600 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700601 printk(KERN_ALERT "%s: journal block not found "
602 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400603 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604 err = -EIO;
605 __journal_abort_soft(journal, err);
606 }
607 } else {
608 *retp = blocknr; /* +journal->j_blk_offset */
609 }
610 return err;
611}
612
613/*
614 * We play buffer_head aliasing tricks to write data/metadata blocks to
615 * the journal without copying their contents, but for journal
616 * descriptor blocks we do need to generate bona fide buffers.
617 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700618 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
620 * But we don't bother doing that, so there will be coherency problems with
621 * mmaps of blockdevs which hold live JBD-controlled filesystems.
622 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700623struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700624{
625 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700626 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700627 int err;
628
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700629 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700630
631 if (err)
632 return NULL;
633
634 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500635 if (!bh)
636 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700637 lock_buffer(bh);
638 memset(bh->b_data, 0, journal->j_blocksize);
639 set_buffer_uptodate(bh);
640 unlock_buffer(bh);
641 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700642 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700643}
644
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500645struct jbd2_stats_proc_session {
646 journal_t *journal;
647 struct transaction_stats_s *stats;
648 int start;
649 int max;
650};
651
652static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
653 struct transaction_stats_s *ts,
654 int first)
655{
656 if (ts == s->stats + s->max)
657 ts = s->stats;
658 if (!first && ts == s->stats + s->start)
659 return NULL;
660 while (ts->ts_type == 0) {
661 ts++;
662 if (ts == s->stats + s->max)
663 ts = s->stats;
664 if (ts == s->stats + s->start)
665 return NULL;
666 }
667 return ts;
668
669}
670
671static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
672{
673 struct jbd2_stats_proc_session *s = seq->private;
674 struct transaction_stats_s *ts;
675 int l = *pos;
676
677 if (l == 0)
678 return SEQ_START_TOKEN;
679 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
680 if (!ts)
681 return NULL;
682 l--;
683 while (l) {
684 ts = jbd2_history_skip_empty(s, ++ts, 0);
685 if (!ts)
686 break;
687 l--;
688 }
689 return ts;
690}
691
692static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
693{
694 struct jbd2_stats_proc_session *s = seq->private;
695 struct transaction_stats_s *ts = v;
696
697 ++*pos;
698 if (v == SEQ_START_TOKEN)
699 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
700 else
701 return jbd2_history_skip_empty(s, ++ts, 0);
702}
703
704static int jbd2_seq_history_show(struct seq_file *seq, void *v)
705{
706 struct transaction_stats_s *ts = v;
707 if (v == SEQ_START_TOKEN) {
708 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
709 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
710 "wait", "run", "lock", "flush", "log", "hndls",
711 "block", "inlog", "ctime", "write", "drop",
712 "close");
713 return 0;
714 }
715 if (ts->ts_type == JBD2_STATS_RUN)
716 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
717 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
718 jiffies_to_msecs(ts->u.run.rs_wait),
719 jiffies_to_msecs(ts->u.run.rs_running),
720 jiffies_to_msecs(ts->u.run.rs_locked),
721 jiffies_to_msecs(ts->u.run.rs_flushing),
722 jiffies_to_msecs(ts->u.run.rs_logging),
723 ts->u.run.rs_handle_count,
724 ts->u.run.rs_blocks,
725 ts->u.run.rs_blocks_logged);
726 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
727 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
728 "C", ts->ts_tid, " ",
729 jiffies_to_msecs(ts->u.chp.cs_chp_time),
730 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
731 ts->u.chp.cs_forced_to_close);
732 else
733 J_ASSERT(0);
734 return 0;
735}
736
737static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
738{
739}
740
741static struct seq_operations jbd2_seq_history_ops = {
742 .start = jbd2_seq_history_start,
743 .next = jbd2_seq_history_next,
744 .stop = jbd2_seq_history_stop,
745 .show = jbd2_seq_history_show,
746};
747
748static int jbd2_seq_history_open(struct inode *inode, struct file *file)
749{
750 journal_t *journal = PDE(inode)->data;
751 struct jbd2_stats_proc_session *s;
752 int rc, size;
753
754 s = kmalloc(sizeof(*s), GFP_KERNEL);
755 if (s == NULL)
756 return -ENOMEM;
757 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
758 s->stats = kmalloc(size, GFP_KERNEL);
759 if (s->stats == NULL) {
760 kfree(s);
761 return -ENOMEM;
762 }
763 spin_lock(&journal->j_history_lock);
764 memcpy(s->stats, journal->j_history, size);
765 s->max = journal->j_history_max;
766 s->start = journal->j_history_cur % s->max;
767 spin_unlock(&journal->j_history_lock);
768
769 rc = seq_open(file, &jbd2_seq_history_ops);
770 if (rc == 0) {
771 struct seq_file *m = file->private_data;
772 m->private = s;
773 } else {
774 kfree(s->stats);
775 kfree(s);
776 }
777 return rc;
778
779}
780
781static int jbd2_seq_history_release(struct inode *inode, struct file *file)
782{
783 struct seq_file *seq = file->private_data;
784 struct jbd2_stats_proc_session *s = seq->private;
785
786 kfree(s->stats);
787 kfree(s);
788 return seq_release(inode, file);
789}
790
791static struct file_operations jbd2_seq_history_fops = {
792 .owner = THIS_MODULE,
793 .open = jbd2_seq_history_open,
794 .read = seq_read,
795 .llseek = seq_lseek,
796 .release = jbd2_seq_history_release,
797};
798
799static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
800{
801 return *pos ? NULL : SEQ_START_TOKEN;
802}
803
804static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
805{
806 return NULL;
807}
808
809static int jbd2_seq_info_show(struct seq_file *seq, void *v)
810{
811 struct jbd2_stats_proc_session *s = seq->private;
812
813 if (v != SEQ_START_TOKEN)
814 return 0;
815 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
816 s->stats->ts_tid,
817 s->journal->j_max_transaction_buffers);
818 if (s->stats->ts_tid == 0)
819 return 0;
820 seq_printf(seq, "average: \n %ums waiting for transaction\n",
821 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
822 seq_printf(seq, " %ums running transaction\n",
823 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
824 seq_printf(seq, " %ums transaction was being locked\n",
825 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
826 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
827 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
828 seq_printf(seq, " %ums logging transaction\n",
829 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Theodore Ts'od7cfa462008-12-17 00:20:45 -0500830 seq_printf(seq, " %luus average transaction commit time\n",
831 do_div(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500832 seq_printf(seq, " %lu handles per transaction\n",
833 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
834 seq_printf(seq, " %lu blocks per transaction\n",
835 s->stats->u.run.rs_blocks / s->stats->ts_tid);
836 seq_printf(seq, " %lu logged blocks per transaction\n",
837 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
838 return 0;
839}
840
841static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
842{
843}
844
845static struct seq_operations jbd2_seq_info_ops = {
846 .start = jbd2_seq_info_start,
847 .next = jbd2_seq_info_next,
848 .stop = jbd2_seq_info_stop,
849 .show = jbd2_seq_info_show,
850};
851
852static int jbd2_seq_info_open(struct inode *inode, struct file *file)
853{
854 journal_t *journal = PDE(inode)->data;
855 struct jbd2_stats_proc_session *s;
856 int rc, size;
857
858 s = kmalloc(sizeof(*s), GFP_KERNEL);
859 if (s == NULL)
860 return -ENOMEM;
861 size = sizeof(struct transaction_stats_s);
862 s->stats = kmalloc(size, GFP_KERNEL);
863 if (s->stats == NULL) {
864 kfree(s);
865 return -ENOMEM;
866 }
867 spin_lock(&journal->j_history_lock);
868 memcpy(s->stats, &journal->j_stats, size);
869 s->journal = journal;
870 spin_unlock(&journal->j_history_lock);
871
872 rc = seq_open(file, &jbd2_seq_info_ops);
873 if (rc == 0) {
874 struct seq_file *m = file->private_data;
875 m->private = s;
876 } else {
877 kfree(s->stats);
878 kfree(s);
879 }
880 return rc;
881
882}
883
884static int jbd2_seq_info_release(struct inode *inode, struct file *file)
885{
886 struct seq_file *seq = file->private_data;
887 struct jbd2_stats_proc_session *s = seq->private;
888 kfree(s->stats);
889 kfree(s);
890 return seq_release(inode, file);
891}
892
893static struct file_operations jbd2_seq_info_fops = {
894 .owner = THIS_MODULE,
895 .open = jbd2_seq_info_open,
896 .read = seq_read,
897 .llseek = seq_lseek,
898 .release = jbd2_seq_info_release,
899};
900
901static struct proc_dir_entry *proc_jbd2_stats;
902
903static void jbd2_stats_proc_init(journal_t *journal)
904{
Theodore Ts'o05496762008-09-16 14:36:17 -0400905 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500906 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700907 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
908 &jbd2_seq_history_fops, journal);
909 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
910 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500911 }
912}
913
914static void jbd2_stats_proc_exit(journal_t *journal)
915{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500916 remove_proc_entry("info", journal->j_proc_entry);
917 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400918 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500919}
920
921static void journal_init_stats(journal_t *journal)
922{
923 int size;
924
925 if (!proc_jbd2_stats)
926 return;
927
928 journal->j_history_max = 100;
929 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
930 journal->j_history = kzalloc(size, GFP_KERNEL);
931 if (!journal->j_history) {
932 journal->j_history_max = 0;
933 return;
934 }
935 spin_lock_init(&journal->j_history_lock);
936}
937
Dave Kleikamp470decc2006-10-11 01:20:57 -0700938/*
939 * Management for journal control blocks: functions to create and
940 * destroy journal_t structures, and to initialise and read existing
941 * journal blocks from disk. */
942
943/* First: create and setup a journal_t object in memory. We initialise
944 * very few fields yet: that has to wait until we have created the
945 * journal structures from from scratch, or loaded them from disk. */
946
947static journal_t * journal_init_common (void)
948{
949 journal_t *journal;
950 int err;
951
Mingming Caod802ffa2007-10-16 18:38:25 -0400952 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700953 if (!journal)
954 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700955
956 init_waitqueue_head(&journal->j_wait_transaction_locked);
957 init_waitqueue_head(&journal->j_wait_logspace);
958 init_waitqueue_head(&journal->j_wait_done_commit);
959 init_waitqueue_head(&journal->j_wait_checkpoint);
960 init_waitqueue_head(&journal->j_wait_commit);
961 init_waitqueue_head(&journal->j_wait_updates);
962 mutex_init(&journal->j_barrier);
963 mutex_init(&journal->j_checkpoint_mutex);
964 spin_lock_init(&journal->j_revoke_lock);
965 spin_lock_init(&journal->j_list_lock);
966 spin_lock_init(&journal->j_state_lock);
967
Mingming Caocd02ff02007-10-16 18:38:25 -0400968 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500969 journal->j_min_batch_time = 0;
970 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700971
972 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700973 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700974
975 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700976 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700977 if (err) {
978 kfree(journal);
979 goto fail;
980 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500981
982 journal_init_stats(journal);
983
Dave Kleikamp470decc2006-10-11 01:20:57 -0700984 return journal;
985fail:
986 return NULL;
987}
988
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700989/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700990 *
991 * Create a journal structure assigned some fixed set of disk blocks to
992 * the journal. We don't actually touch those disk blocks yet, but we
993 * need to set up all of the mapping information to tell the journaling
994 * system where the journal blocks are.
995 *
996 */
997
998/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400999 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001000 * @bdev: Block device on which to create the journal
1001 * @fs_dev: Device which hold journalled filesystem for this journal.
1002 * @start: Block nr Start of journal.
1003 * @len: Length of the journal in blocks.
1004 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001005 *
1006 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001007 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001008 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001009 * range of blocks on an arbitrary block device.
1010 *
1011 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001012journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001013 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001014 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015{
1016 journal_t *journal = journal_init_common();
1017 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001018 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001019 int n;
1020
1021 if (!journal)
1022 return NULL;
1023
1024 /* journal descriptor can store up to n blocks -bzzz */
1025 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -05001026 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001027 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1028 journal->j_wbufsize = n;
1029 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1030 if (!journal->j_wbuf) {
1031 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001032 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001033 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034 }
1035 journal->j_dev = bdev;
1036 journal->j_fs_dev = fs_dev;
1037 journal->j_blk_offset = start;
1038 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001039 bdevname(journal->j_dev, journal->j_devname);
1040 p = journal->j_devname;
1041 while ((p = strchr(p, '/')))
1042 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043
1044 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001045 if (!bh) {
1046 printk(KERN_ERR
1047 "%s: Cannot get buffer for journal superblock\n",
1048 __func__);
1049 goto out_err;
1050 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051 journal->j_sb_buffer = bh;
1052 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001053
Dave Kleikamp470decc2006-10-11 01:20:57 -07001054 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001055out_err:
1056 jbd2_stats_proc_exit(journal);
1057 kfree(journal);
1058 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001059}
1060
1061/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001062 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063 * @inode: An inode to create the journal in
1064 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001065 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001066 * the journal. The inode must exist already, must support bmap() and
1067 * must have all data blocks preallocated.
1068 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001069journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070{
1071 struct buffer_head *bh;
1072 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001073 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001074 int err;
1075 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001076 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001077
1078 if (!journal)
1079 return NULL;
1080
1081 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1082 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001083 bdevname(journal->j_dev, journal->j_devname);
1084 p = journal->j_devname;
1085 while ((p = strchr(p, '/')))
1086 *p = '!';
1087 p = journal->j_devname + strlen(journal->j_devname);
1088 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001089 jbd_debug(1,
1090 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1091 journal, inode->i_sb->s_id, inode->i_ino,
1092 (long long) inode->i_size,
1093 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1094
1095 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1096 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001097 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098
1099 /* journal descriptor can store up to n blocks -bzzz */
1100 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1101 journal->j_wbufsize = n;
1102 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1103 if (!journal->j_wbuf) {
1104 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001105 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001106 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001107 }
1108
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001109 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001110 /* If that failed, give up */
1111 if (err) {
1112 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001113 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001114 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115 }
1116
1117 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001118 if (!bh) {
1119 printk(KERN_ERR
1120 "%s: Cannot get buffer for journal superblock\n",
1121 __func__);
1122 goto out_err;
1123 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001124 journal->j_sb_buffer = bh;
1125 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1126
1127 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001128out_err:
1129 jbd2_stats_proc_exit(journal);
1130 kfree(journal);
1131 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001132}
1133
1134/*
1135 * If the journal init or create aborts, we need to mark the journal
1136 * superblock as being NULL to prevent the journal destroy from writing
1137 * back a bogus superblock.
1138 */
1139static void journal_fail_superblock (journal_t *journal)
1140{
1141 struct buffer_head *bh = journal->j_sb_buffer;
1142 brelse(bh);
1143 journal->j_sb_buffer = NULL;
1144}
1145
1146/*
1147 * Given a journal_t structure, initialise the various fields for
1148 * startup of a new journaling session. We use this both when creating
1149 * a journal, and after recovering an old journal to reset it for
1150 * subsequent use.
1151 */
1152
1153static int journal_reset(journal_t *journal)
1154{
1155 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001156 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001157
1158 first = be32_to_cpu(sb->s_first);
1159 last = be32_to_cpu(sb->s_maxlen);
1160
1161 journal->j_first = first;
1162 journal->j_last = last;
1163
1164 journal->j_head = first;
1165 journal->j_tail = first;
1166 journal->j_free = last - first;
1167
1168 journal->j_tail_sequence = journal->j_transaction_sequence;
1169 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1170 journal->j_commit_request = journal->j_commit_sequence;
1171
1172 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1173
1174 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001175 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001176 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001177}
1178
1179/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 * @journal: The journal to update.
1182 * @wait: Set to '0' if you don't want to wait for IO completion.
1183 *
1184 * Update a journal's dynamic superblock fields and write it to disk,
1185 * optionally waiting for the IO to complete.
1186 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001187void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001188{
1189 journal_superblock_t *sb = journal->j_superblock;
1190 struct buffer_head *bh = journal->j_sb_buffer;
1191
1192 /*
1193 * As a special case, if the on-disk copy is already marked as needing
1194 * no recovery (s_start == 0) and there are no outstanding transactions
1195 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001196 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001197 * attempting a write to a potential-readonly device.
1198 */
1199 if (sb->s_start == 0 && journal->j_tail_sequence ==
1200 journal->j_transaction_sequence) {
1201 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1202 "(start %ld, seq %d, errno %d)\n",
1203 journal->j_tail, journal->j_tail_sequence,
1204 journal->j_errno);
1205 goto out;
1206 }
1207
Theodore Ts'o914258b2008-10-06 21:35:40 -04001208 if (buffer_write_io_error(bh)) {
1209 /*
1210 * Oh, dear. A previous attempt to write the journal
1211 * superblock failed. This could happen because the
1212 * USB device was yanked out. Or it could happen to
1213 * be a transient write error and maybe the block will
1214 * be remapped. Nothing we can do but to retry the
1215 * write and hope for the best.
1216 */
1217 printk(KERN_ERR "JBD2: previous I/O error detected "
1218 "for journal superblock update for %s.\n",
1219 journal->j_devname);
1220 clear_buffer_write_io_error(bh);
1221 set_buffer_uptodate(bh);
1222 }
1223
Dave Kleikamp470decc2006-10-11 01:20:57 -07001224 spin_lock(&journal->j_state_lock);
1225 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1226 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1227
1228 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1229 sb->s_start = cpu_to_be32(journal->j_tail);
1230 sb->s_errno = cpu_to_be32(journal->j_errno);
1231 spin_unlock(&journal->j_state_lock);
1232
1233 BUFFER_TRACE(bh, "marking dirty");
1234 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001235 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001236 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001237 if (buffer_write_io_error(bh)) {
1238 printk(KERN_ERR "JBD2: I/O error detected "
1239 "when updating journal superblock for %s.\n",
1240 journal->j_devname);
1241 clear_buffer_write_io_error(bh);
1242 set_buffer_uptodate(bh);
1243 }
1244 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001245 ll_rw_block(SWRITE, 1, &bh);
1246
1247out:
1248 /* If we have just flushed the log (by marking s_start==0), then
1249 * any future commit will have to be careful to update the
1250 * superblock again to re-record the true start of the log. */
1251
1252 spin_lock(&journal->j_state_lock);
1253 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001254 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001255 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001256 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 spin_unlock(&journal->j_state_lock);
1258}
1259
1260/*
1261 * Read the superblock for a given journal, performing initial
1262 * validation of the format.
1263 */
1264
1265static int journal_get_superblock(journal_t *journal)
1266{
1267 struct buffer_head *bh;
1268 journal_superblock_t *sb;
1269 int err = -EIO;
1270
1271 bh = journal->j_sb_buffer;
1272
1273 J_ASSERT(bh != NULL);
1274 if (!buffer_uptodate(bh)) {
1275 ll_rw_block(READ, 1, &bh);
1276 wait_on_buffer(bh);
1277 if (!buffer_uptodate(bh)) {
1278 printk (KERN_ERR
1279 "JBD: IO error reading journal superblock\n");
1280 goto out;
1281 }
1282 }
1283
1284 sb = journal->j_superblock;
1285
1286 err = -EINVAL;
1287
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001288 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1290 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1291 goto out;
1292 }
1293
1294 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001295 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001296 journal->j_format_version = 1;
1297 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001298 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001299 journal->j_format_version = 2;
1300 break;
1301 default:
1302 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1303 goto out;
1304 }
1305
1306 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1307 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1308 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1309 printk (KERN_WARNING "JBD: journal file too short\n");
1310 goto out;
1311 }
1312
1313 return 0;
1314
1315out:
1316 journal_fail_superblock(journal);
1317 return err;
1318}
1319
1320/*
1321 * Load the on-disk journal superblock and read the key fields into the
1322 * journal_t.
1323 */
1324
1325static int load_superblock(journal_t *journal)
1326{
1327 int err;
1328 journal_superblock_t *sb;
1329
1330 err = journal_get_superblock(journal);
1331 if (err)
1332 return err;
1333
1334 sb = journal->j_superblock;
1335
1336 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1337 journal->j_tail = be32_to_cpu(sb->s_start);
1338 journal->j_first = be32_to_cpu(sb->s_first);
1339 journal->j_last = be32_to_cpu(sb->s_maxlen);
1340 journal->j_errno = be32_to_cpu(sb->s_errno);
1341
1342 return 0;
1343}
1344
1345
1346/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001347 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001348 * @journal: Journal to act on.
1349 *
1350 * Given a journal_t structure which tells us which disk blocks contain
1351 * a journal, read the journal from disk to initialise the in-memory
1352 * structures.
1353 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001354int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001355{
1356 int err;
1357 journal_superblock_t *sb;
1358
1359 err = load_superblock(journal);
1360 if (err)
1361 return err;
1362
1363 sb = journal->j_superblock;
1364 /* If this is a V2 superblock, then we have to check the
1365 * features flags on it. */
1366
1367 if (journal->j_format_version >= 2) {
1368 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001369 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001371 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001372 printk (KERN_WARNING
1373 "JBD: Unrecognised features on journal\n");
1374 return -EINVAL;
1375 }
1376 }
1377
Dave Kleikamp470decc2006-10-11 01:20:57 -07001378 /* Let the recovery code check whether it needs to recover any
1379 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001380 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001381 goto recovery_error;
1382
1383 /* OK, we've finished with the dynamic journal bits:
1384 * reinitialise the dynamic contents of the superblock in memory
1385 * and reset them on disk. */
1386 if (journal_reset(journal))
1387 goto recovery_error;
1388
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001389 journal->j_flags &= ~JBD2_ABORT;
1390 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391 return 0;
1392
1393recovery_error:
1394 printk (KERN_WARNING "JBD: recovery failed\n");
1395 return -EIO;
1396}
1397
1398/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001399 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 * @journal: Journal to act on.
1401 *
1402 * Release a journal_t structure once it is no longer in use by the
1403 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001404 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001406int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001407{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001408 int err = 0;
1409
Dave Kleikamp470decc2006-10-11 01:20:57 -07001410 /* Wait for the commit thread to wake up and die. */
1411 journal_kill_thread(journal);
1412
1413 /* Force a final log commit */
1414 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001415 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001416
1417 /* Force any old transactions to disk */
1418
1419 /* Totally anal locking here... */
1420 spin_lock(&journal->j_list_lock);
1421 while (journal->j_checkpoint_transactions != NULL) {
1422 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001423 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001424 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001425 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001426 spin_lock(&journal->j_list_lock);
1427 }
1428
1429 J_ASSERT(journal->j_running_transaction == NULL);
1430 J_ASSERT(journal->j_committing_transaction == NULL);
1431 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1432 spin_unlock(&journal->j_list_lock);
1433
Dave Kleikamp470decc2006-10-11 01:20:57 -07001434 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001435 if (!is_journal_aborted(journal)) {
1436 /* We can now mark the journal as empty. */
1437 journal->j_tail = 0;
1438 journal->j_tail_sequence =
1439 ++journal->j_transaction_sequence;
1440 jbd2_journal_update_superblock(journal, 1);
1441 } else {
1442 err = -EIO;
1443 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001444 brelse(journal->j_sb_buffer);
1445 }
1446
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001447 if (journal->j_proc_entry)
1448 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001449 if (journal->j_inode)
1450 iput(journal->j_inode);
1451 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001452 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001453 kfree(journal->j_wbuf);
1454 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001455
1456 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001457}
1458
1459
1460/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001461 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 * @journal: Journal to check.
1463 * @compat: bitmask of compatible features
1464 * @ro: bitmask of features that force read-only mount
1465 * @incompat: bitmask of incompatible features
1466 *
1467 * Check whether the journal uses all of a given set of
1468 * features. Return true (non-zero) if it does.
1469 **/
1470
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001471int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 unsigned long ro, unsigned long incompat)
1473{
1474 journal_superblock_t *sb;
1475
1476 if (!compat && !ro && !incompat)
1477 return 1;
1478 if (journal->j_format_version == 1)
1479 return 0;
1480
1481 sb = journal->j_superblock;
1482
1483 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1484 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1485 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1486 return 1;
1487
1488 return 0;
1489}
1490
1491/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001492 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001493 * @journal: Journal to check.
1494 * @compat: bitmask of compatible features
1495 * @ro: bitmask of features that force read-only mount
1496 * @incompat: bitmask of incompatible features
1497 *
1498 * Check whether the journaling code supports the use of
1499 * all of a given set of features on this journal. Return true
1500 * (non-zero) if it can. */
1501
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 unsigned long ro, unsigned long incompat)
1504{
1505 journal_superblock_t *sb;
1506
1507 if (!compat && !ro && !incompat)
1508 return 1;
1509
1510 sb = journal->j_superblock;
1511
1512 /* We can support any known requested features iff the
1513 * superblock is in version 2. Otherwise we fail to support any
1514 * extended sb features. */
1515
1516 if (journal->j_format_version != 2)
1517 return 0;
1518
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001519 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1520 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1521 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001522 return 1;
1523
1524 return 0;
1525}
1526
1527/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001528 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001529 * @journal: Journal to act on.
1530 * @compat: bitmask of compatible features
1531 * @ro: bitmask of features that force read-only mount
1532 * @incompat: bitmask of incompatible features
1533 *
1534 * Mark a given journal feature as present on the
1535 * superblock. Returns true if the requested features could be set.
1536 *
1537 */
1538
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001539int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001540 unsigned long ro, unsigned long incompat)
1541{
1542 journal_superblock_t *sb;
1543
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001544 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001545 return 1;
1546
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001547 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001548 return 0;
1549
1550 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1551 compat, ro, incompat);
1552
1553 sb = journal->j_superblock;
1554
1555 sb->s_feature_compat |= cpu_to_be32(compat);
1556 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1557 sb->s_feature_incompat |= cpu_to_be32(incompat);
1558
1559 return 1;
1560}
1561
Girish Shilamkar818d2762008-01-28 23:58:27 -05001562/*
1563 * jbd2_journal_clear_features () - Clear a given journal feature in the
1564 * superblock
1565 * @journal: Journal to act on.
1566 * @compat: bitmask of compatible features
1567 * @ro: bitmask of features that force read-only mount
1568 * @incompat: bitmask of incompatible features
1569 *
1570 * Clear a given journal feature as present on the
1571 * superblock.
1572 */
1573void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1574 unsigned long ro, unsigned long incompat)
1575{
1576 journal_superblock_t *sb;
1577
1578 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1579 compat, ro, incompat);
1580
1581 sb = journal->j_superblock;
1582
1583 sb->s_feature_compat &= ~cpu_to_be32(compat);
1584 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1585 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1586}
1587EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001588
1589/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001590 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001591 * @journal: Journal to act on.
1592 *
1593 * Given an initialised but unloaded journal struct, poke about in the
1594 * on-disk structure to update it to the most recent supported version.
1595 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001596int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001597{
1598 journal_superblock_t *sb;
1599 int err;
1600
1601 err = journal_get_superblock(journal);
1602 if (err)
1603 return err;
1604
1605 sb = journal->j_superblock;
1606
1607 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001608 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001610 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001611 return journal_convert_superblock_v1(journal, sb);
1612 default:
1613 break;
1614 }
1615 return -EINVAL;
1616}
1617
1618static int journal_convert_superblock_v1(journal_t *journal,
1619 journal_superblock_t *sb)
1620{
1621 int offset, blocksize;
1622 struct buffer_head *bh;
1623
1624 printk(KERN_WARNING
1625 "JBD: Converting superblock from version 1 to 2.\n");
1626
1627 /* Pre-initialise new fields to zero */
1628 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1629 blocksize = be32_to_cpu(sb->s_blocksize);
1630 memset(&sb->s_feature_compat, 0, blocksize-offset);
1631
1632 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001633 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001634 journal->j_format_version = 2;
1635
1636 bh = journal->j_sb_buffer;
1637 BUFFER_TRACE(bh, "marking dirty");
1638 mark_buffer_dirty(bh);
1639 sync_dirty_buffer(bh);
1640 return 0;
1641}
1642
1643
1644/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001645 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001646 * @journal: Journal to act on.
1647 *
1648 * Flush all data for a given journal to disk and empty the journal.
1649 * Filesystems can use this when remounting readonly to ensure that
1650 * recovery does not need to happen on remount.
1651 */
1652
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001653int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001654{
1655 int err = 0;
1656 transaction_t *transaction = NULL;
1657 unsigned long old_tail;
1658
1659 spin_lock(&journal->j_state_lock);
1660
1661 /* Force everything buffered to the log... */
1662 if (journal->j_running_transaction) {
1663 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001664 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 } else if (journal->j_committing_transaction)
1666 transaction = journal->j_committing_transaction;
1667
1668 /* Wait for the log commit to complete... */
1669 if (transaction) {
1670 tid_t tid = transaction->t_tid;
1671
1672 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001673 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 } else {
1675 spin_unlock(&journal->j_state_lock);
1676 }
1677
1678 /* ...and flush everything in the log out to disk. */
1679 spin_lock(&journal->j_list_lock);
1680 while (!err && journal->j_checkpoint_transactions != NULL) {
1681 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001682 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001683 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001684 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001685 spin_lock(&journal->j_list_lock);
1686 }
1687 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001688
1689 if (is_journal_aborted(journal))
1690 return -EIO;
1691
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693
1694 /* Finally, mark the journal as really needing no recovery.
1695 * This sets s_start==0 in the underlying superblock, which is
1696 * the magic code for a fully-recovered superblock. Any future
1697 * commits of data to the journal will restore the current
1698 * s_start value. */
1699 spin_lock(&journal->j_state_lock);
1700 old_tail = journal->j_tail;
1701 journal->j_tail = 0;
1702 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001703 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 spin_lock(&journal->j_state_lock);
1705 journal->j_tail = old_tail;
1706
1707 J_ASSERT(!journal->j_running_transaction);
1708 J_ASSERT(!journal->j_committing_transaction);
1709 J_ASSERT(!journal->j_checkpoint_transactions);
1710 J_ASSERT(journal->j_head == journal->j_tail);
1711 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1712 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001713 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001714}
1715
1716/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001717 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718 * @journal: Journal to act on.
1719 * @write: flag (see below)
1720 *
1721 * Wipe out all of the contents of a journal, safely. This will produce
1722 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001723 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001724 *
1725 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1726 * we merely suppress recovery.
1727 */
1728
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001729int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001730{
1731 journal_superblock_t *sb;
1732 int err = 0;
1733
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001734 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001735
1736 err = load_superblock(journal);
1737 if (err)
1738 return err;
1739
1740 sb = journal->j_superblock;
1741
1742 if (!journal->j_tail)
1743 goto no_recovery;
1744
1745 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1746 write ? "Clearing" : "Ignoring");
1747
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001748 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001750 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001751
1752 no_recovery:
1753 return err;
1754}
1755
1756/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001757 * Journal abort has very specific semantics, which we describe
1758 * for journal abort.
1759 *
1760 * Two internal function, which provide abort to te jbd layer
1761 * itself are here.
1762 */
1763
1764/*
1765 * Quick version for internal journal use (doesn't lock the journal).
1766 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1767 * and don't attempt to make any other journal updates.
1768 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001769void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001770{
1771 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001772
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001773 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001774 return;
1775
1776 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001777 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001778
1779 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001780 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001781 transaction = journal->j_running_transaction;
1782 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001783 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 spin_unlock(&journal->j_state_lock);
1785}
1786
1787/* Soft abort: record the abort error status in the journal superblock,
1788 * but don't do any other IO. */
1789static void __journal_abort_soft (journal_t *journal, int errno)
1790{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001791 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001792 return;
1793
1794 if (!journal->j_errno)
1795 journal->j_errno = errno;
1796
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001797 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798
1799 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001800 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001801}
1802
1803/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001804 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805 * @journal: the journal to shutdown.
1806 * @errno: an error number to record in the journal indicating
1807 * the reason for the shutdown.
1808 *
1809 * Perform a complete, immediate shutdown of the ENTIRE
1810 * journal (not of a single transaction). This operation cannot be
1811 * undone without closing and reopening the journal.
1812 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001813 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1815 * mode.
1816 *
1817 * Journal abort has very specific semantics. Any existing dirty,
1818 * unjournaled buffers in the main filesystem will still be written to
1819 * disk by bdflush, but the journaling mechanism will be suspended
1820 * immediately and no further transaction commits will be honoured.
1821 *
1822 * Any dirty, journaled buffers will be written back to disk without
1823 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1824 * filesystem, but we _do_ attempt to leave as much data as possible
1825 * behind for fsck to use for cleanup.
1826 *
1827 * Any attempt to get a new transaction handle on a journal which is in
1828 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001829 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001830 * entered abort state during the update.
1831 *
1832 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001833 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001834 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001835 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001836 * which will be recorded (if possible) in the journal superblock. This
1837 * allows a client to record failure conditions in the middle of a
1838 * transaction without having to complete the transaction to record the
1839 * failure to disk. ext3_error, for example, now uses this
1840 * functionality.
1841 *
1842 * Errors which originate from within the journaling layer will NOT
1843 * supply an errno; a null errno implies that absolutely no further
1844 * writes are done to the journal (unless there are any already in
1845 * progress).
1846 *
1847 */
1848
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001849void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001850{
1851 __journal_abort_soft(journal, errno);
1852}
1853
1854/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001855 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001856 * @journal: journal to examine.
1857 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001858 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001859 * time the journal was mounted - if the journal was stopped
1860 * without calling abort this will be 0.
1861 *
1862 * If the journal has been aborted on this mount time -EROFS will
1863 * be returned.
1864 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001865int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866{
1867 int err;
1868
1869 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001870 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001871 err = -EROFS;
1872 else
1873 err = journal->j_errno;
1874 spin_unlock(&journal->j_state_lock);
1875 return err;
1876}
1877
1878/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001879 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880 * @journal: journal to act on.
1881 *
1882 * An error must be cleared or Acked to take a FS out of readonly
1883 * mode.
1884 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001885int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001886{
1887 int err = 0;
1888
1889 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001890 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001891 err = -EROFS;
1892 else
1893 journal->j_errno = 0;
1894 spin_unlock(&journal->j_state_lock);
1895 return err;
1896}
1897
1898/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001899 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001900 * @journal: journal to act on.
1901 *
1902 * An error must be cleared or Acked to take a FS out of readonly
1903 * mode.
1904 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001905void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001906{
1907 spin_lock(&journal->j_state_lock);
1908 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 spin_unlock(&journal->j_state_lock);
1911}
1912
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001913int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001914{
1915 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1916}
1917
1918/*
Zach Brownb517bea2006-10-11 01:21:08 -07001919 * helper functions to deal with 32 or 64bit block numbers.
1920 */
1921size_t journal_tag_bytes(journal_t *journal)
1922{
1923 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001924 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001925 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001926 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001927}
1928
1929/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001930 * Journal_head storage management
1931 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001932static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001933#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1935#endif
1936
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001937static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001938{
1939 int retval;
1940
Al Viro1076d172008-03-29 03:07:18 +00001941 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001942 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001943 sizeof(struct journal_head),
1944 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001945 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001946 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001947 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001948 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001949 retval = -ENOMEM;
1950 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1951 }
1952 return retval;
1953}
1954
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001955static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001956{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001957 if (jbd2_journal_head_cache) {
1958 kmem_cache_destroy(jbd2_journal_head_cache);
1959 jbd2_journal_head_cache = NULL;
1960 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961}
1962
1963/*
1964 * journal_head splicing and dicing
1965 */
1966static struct journal_head *journal_alloc_journal_head(void)
1967{
1968 struct journal_head *ret;
1969 static unsigned long last_warning;
1970
Jose R. Santose23291b2007-07-18 08:57:06 -04001971#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001972 atomic_inc(&nr_journal_heads);
1973#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001974 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001975 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001976 jbd_debug(1, "out of memory for journal_head\n");
1977 if (time_after(jiffies, last_warning + 5*HZ)) {
1978 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001979 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001980 last_warning = jiffies;
1981 }
Al Viro1076d172008-03-29 03:07:18 +00001982 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001983 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001984 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001985 }
1986 }
1987 return ret;
1988}
1989
1990static void journal_free_journal_head(struct journal_head *jh)
1991{
Jose R. Santose23291b2007-07-18 08:57:06 -04001992#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001993 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04001994 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001995#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001996 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997}
1998
1999/*
2000 * A journal_head is attached to a buffer_head whenever JBD has an
2001 * interest in the buffer.
2002 *
2003 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2004 * is set. This bit is tested in core kernel code where we need to take
2005 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2006 * there.
2007 *
2008 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2009 *
2010 * When a buffer has its BH_JBD bit set it is immune from being released by
2011 * core kernel code, mainly via ->b_count.
2012 *
2013 * A journal_head may be detached from its buffer_head when the journal_head's
2014 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002015 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 * journal_head can be dropped if needed.
2017 *
2018 * Various places in the kernel want to attach a journal_head to a buffer_head
2019 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002020 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002022 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023 *
2024 * So the typical usage would be:
2025 *
2026 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028 * ...
2029 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002030 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002031 *
2032 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2033 * because it has a non-zero b_transaction.
2034 */
2035
2036/*
2037 * Give a buffer_head a journal_head.
2038 *
2039 * Doesn't need the journal lock.
2040 * May sleep.
2041 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002042struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002043{
2044 struct journal_head *jh;
2045 struct journal_head *new_jh = NULL;
2046
2047repeat:
2048 if (!buffer_jbd(bh)) {
2049 new_jh = journal_alloc_journal_head();
2050 memset(new_jh, 0, sizeof(*new_jh));
2051 }
2052
2053 jbd_lock_bh_journal_head(bh);
2054 if (buffer_jbd(bh)) {
2055 jh = bh2jh(bh);
2056 } else {
2057 J_ASSERT_BH(bh,
2058 (atomic_read(&bh->b_count) > 0) ||
2059 (bh->b_page && bh->b_page->mapping));
2060
2061 if (!new_jh) {
2062 jbd_unlock_bh_journal_head(bh);
2063 goto repeat;
2064 }
2065
2066 jh = new_jh;
2067 new_jh = NULL; /* We consumed it */
2068 set_buffer_jbd(bh);
2069 bh->b_private = jh;
2070 jh->b_bh = bh;
2071 get_bh(bh);
2072 BUFFER_TRACE(bh, "added journal_head");
2073 }
2074 jh->b_jcount++;
2075 jbd_unlock_bh_journal_head(bh);
2076 if (new_jh)
2077 journal_free_journal_head(new_jh);
2078 return bh->b_private;
2079}
2080
2081/*
2082 * Grab a ref against this buffer_head's journal_head. If it ended up not
2083 * having a journal_head, return NULL
2084 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002085struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002086{
2087 struct journal_head *jh = NULL;
2088
2089 jbd_lock_bh_journal_head(bh);
2090 if (buffer_jbd(bh)) {
2091 jh = bh2jh(bh);
2092 jh->b_jcount++;
2093 }
2094 jbd_unlock_bh_journal_head(bh);
2095 return jh;
2096}
2097
2098static void __journal_remove_journal_head(struct buffer_head *bh)
2099{
2100 struct journal_head *jh = bh2jh(bh);
2101
2102 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2103
2104 get_bh(bh);
2105 if (jh->b_jcount == 0) {
2106 if (jh->b_transaction == NULL &&
2107 jh->b_next_transaction == NULL &&
2108 jh->b_cp_transaction == NULL) {
2109 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2110 J_ASSERT_BH(bh, buffer_jbd(bh));
2111 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2112 BUFFER_TRACE(bh, "remove journal_head");
2113 if (jh->b_frozen_data) {
2114 printk(KERN_WARNING "%s: freeing "
2115 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002116 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002117 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002118 }
2119 if (jh->b_committed_data) {
2120 printk(KERN_WARNING "%s: freeing "
2121 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002122 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002123 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002124 }
2125 bh->b_private = NULL;
2126 jh->b_bh = NULL; /* debug, really */
2127 clear_buffer_jbd(bh);
2128 __brelse(bh);
2129 journal_free_journal_head(jh);
2130 } else {
2131 BUFFER_TRACE(bh, "journal_head was locked");
2132 }
2133 }
2134}
2135
2136/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002137 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002138 * and has a zero b_jcount then remove and release its journal_head. If we did
2139 * see that the buffer is not used by any transaction we also "logically"
2140 * decrement ->b_count.
2141 *
2142 * We in fact take an additional increment on ->b_count as a convenience,
2143 * because the caller usually wants to do additional things with the bh
2144 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002145 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002146 * time. Once the caller has run __brelse(), the buffer is eligible for
2147 * reaping by try_to_free_buffers().
2148 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002149void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002150{
2151 jbd_lock_bh_journal_head(bh);
2152 __journal_remove_journal_head(bh);
2153 jbd_unlock_bh_journal_head(bh);
2154}
2155
2156/*
2157 * Drop a reference on the passed journal_head. If it fell to zero then try to
2158 * release the journal_head from the buffer_head.
2159 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002160void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002161{
2162 struct buffer_head *bh = jh2bh(jh);
2163
2164 jbd_lock_bh_journal_head(bh);
2165 J_ASSERT_JH(jh, jh->b_jcount > 0);
2166 --jh->b_jcount;
2167 if (!jh->b_jcount && !jh->b_transaction) {
2168 __journal_remove_journal_head(bh);
2169 __brelse(bh);
2170 }
2171 jbd_unlock_bh_journal_head(bh);
2172}
2173
2174/*
Jan Karac851ed52008-07-11 19:27:31 -04002175 * Initialize jbd inode head
2176 */
2177void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2178{
2179 jinode->i_transaction = NULL;
2180 jinode->i_next_transaction = NULL;
2181 jinode->i_vfs_inode = inode;
2182 jinode->i_flags = 0;
2183 INIT_LIST_HEAD(&jinode->i_list);
2184}
2185
2186/*
2187 * Function to be called before we start removing inode from memory (i.e.,
2188 * clear_inode() is a fine place to be called from). It removes inode from
2189 * transaction's lists.
2190 */
2191void jbd2_journal_release_jbd_inode(journal_t *journal,
2192 struct jbd2_inode *jinode)
2193{
2194 int writeout = 0;
2195
2196 if (!journal)
2197 return;
2198restart:
2199 spin_lock(&journal->j_list_lock);
2200 /* Is commit writing out inode - we have to wait */
2201 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2202 wait_queue_head_t *wq;
2203 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2204 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2205 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2206 spin_unlock(&journal->j_list_lock);
2207 schedule();
2208 finish_wait(wq, &wait.wait);
2209 goto restart;
2210 }
2211
2212 /* Do we need to wait for data writeback? */
2213 if (journal->j_committing_transaction == jinode->i_transaction)
2214 writeout = 1;
2215 if (jinode->i_transaction) {
2216 list_del(&jinode->i_list);
2217 jinode->i_transaction = NULL;
2218 }
2219 spin_unlock(&journal->j_list_lock);
2220}
2221
2222/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002223 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002224 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002225#ifdef CONFIG_JBD2_DEBUG
2226u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002227EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002228
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002229#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002230
Jose R. Santos6f38c742007-10-16 18:38:25 -04002231static struct dentry *jbd2_debugfs_dir;
2232static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002233
2234static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002235{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002236 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2237 if (jbd2_debugfs_dir)
2238 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2239 jbd2_debugfs_dir,
2240 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002241}
2242
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002243static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002244{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002245 debugfs_remove(jbd2_debug);
2246 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002247}
2248
2249#else
2250
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002251static void __init jbd2_create_debugfs_entry(void)
2252{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002253}
2254
2255static void __exit jbd2_remove_debugfs_entry(void)
2256{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002257}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002258
2259#endif
2260
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002261#ifdef CONFIG_PROC_FS
2262
2263#define JBD2_STATS_PROC_NAME "fs/jbd2"
2264
2265static void __init jbd2_create_jbd_stats_proc_entry(void)
2266{
2267 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2268}
2269
2270static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2271{
2272 if (proc_jbd2_stats)
2273 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2274}
2275
2276#else
2277
2278#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2279#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2280
2281#endif
2282
Christoph Lametere18b8902006-12-06 20:33:20 -08002283struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002284
2285static int __init journal_init_handle_cache(void)
2286{
Johann Lombardia920e942006-10-11 01:21:00 -07002287 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288 sizeof(handle_t),
2289 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002290 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002291 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002292 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002293 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2294 return -ENOMEM;
2295 }
2296 return 0;
2297}
2298
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002299static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002300{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002301 if (jbd2_handle_cache)
2302 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002303}
2304
2305/*
2306 * Module startup and shutdown
2307 */
2308
2309static int __init journal_init_caches(void)
2310{
2311 int ret;
2312
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002313 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002314 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002315 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002316 if (ret == 0)
2317 ret = journal_init_handle_cache();
2318 return ret;
2319}
2320
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002321static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002322{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002323 jbd2_journal_destroy_revoke_caches();
2324 jbd2_journal_destroy_jbd2_journal_head_cache();
2325 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002326}
2327
2328static int __init journal_init(void)
2329{
2330 int ret;
2331
2332 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2333
2334 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002335 if (ret == 0) {
2336 jbd2_create_debugfs_entry();
2337 jbd2_create_jbd_stats_proc_entry();
2338 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002339 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002340 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341 return ret;
2342}
2343
2344static void __exit journal_exit(void)
2345{
Jose R. Santose23291b2007-07-18 08:57:06 -04002346#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002347 int n = atomic_read(&nr_journal_heads);
2348 if (n)
2349 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2350#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002351 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002352 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002353 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002354}
2355
2356MODULE_LICENSE("GPL");
2357module_init(journal_init);
2358module_exit(journal_exit);
2359