blob: 18bfd5dab64220d7d11fc90b6efb7c79e807c16a [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -050040#include <linux/math64.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040041#include <linux/hash.h>
42
43#define CREATE_TRACE_POINTS
44#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070045
46#include <asm/uaccess.h>
47#include <asm/page.h>
48
Mingming Caof7f4bcc2006-10-11 01:20:59 -070049EXPORT_SYMBOL(jbd2_journal_start);
50EXPORT_SYMBOL(jbd2_journal_restart);
51EXPORT_SYMBOL(jbd2_journal_extend);
52EXPORT_SYMBOL(jbd2_journal_stop);
53EXPORT_SYMBOL(jbd2_journal_lock_updates);
54EXPORT_SYMBOL(jbd2_journal_unlock_updates);
55EXPORT_SYMBOL(jbd2_journal_get_write_access);
56EXPORT_SYMBOL(jbd2_journal_get_create_access);
57EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070058EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070059EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
60EXPORT_SYMBOL(jbd2_journal_release_buffer);
61EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062#if 0
63EXPORT_SYMBOL(journal_sync_buffer);
64#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070065EXPORT_SYMBOL(jbd2_journal_flush);
66EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070067
Mingming Caof7f4bcc2006-10-11 01:20:59 -070068EXPORT_SYMBOL(jbd2_journal_init_dev);
69EXPORT_SYMBOL(jbd2_journal_init_inode);
70EXPORT_SYMBOL(jbd2_journal_update_format);
71EXPORT_SYMBOL(jbd2_journal_check_used_features);
72EXPORT_SYMBOL(jbd2_journal_check_available_features);
73EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070074EXPORT_SYMBOL(jbd2_journal_load);
75EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070076EXPORT_SYMBOL(jbd2_journal_abort);
77EXPORT_SYMBOL(jbd2_journal_errno);
78EXPORT_SYMBOL(jbd2_journal_ack_err);
79EXPORT_SYMBOL(jbd2_journal_clear_err);
80EXPORT_SYMBOL(jbd2_log_wait_commit);
81EXPORT_SYMBOL(jbd2_journal_start_commit);
82EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
83EXPORT_SYMBOL(jbd2_journal_wipe);
84EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
85EXPORT_SYMBOL(jbd2_journal_invalidatepage);
86EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
87EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040088EXPORT_SYMBOL(jbd2_journal_file_inode);
89EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
90EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
91EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070092
93static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
94static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070095
96/*
97 * Helper function used to manage commit timeouts
98 */
99
100static void commit_timeout(unsigned long __data)
101{
102 struct task_struct * p = (struct task_struct *) __data;
103
104 wake_up_process(p);
105}
106
107/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700108 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700109 * journal.
110 *
111 * This kernel thread is responsible for two things:
112 *
113 * 1) COMMIT: Every so often we need to commit the current state of the
114 * filesystem to disk. The journal thread is responsible for writing
115 * all of the metadata buffers to disk.
116 *
117 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
118 * of the data in that part of the log has been rewritten elsewhere on
119 * the disk. Flushing these old buffers to reclaim space in the log is
120 * known as checkpointing, and this thread is responsible for that job.
121 */
122
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700123static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700124{
125 journal_t *journal = arg;
126 transaction_t *transaction;
127
128 /*
129 * Set up an interval timer which can be used to trigger a commit wakeup
130 * after the commit interval expires
131 */
132 setup_timer(&journal->j_commit_timer, commit_timeout,
133 (unsigned long)current);
134
135 /* Record that the journal thread is running */
136 journal->j_task = current;
137 wake_up(&journal->j_wait_done_commit);
138
Theodore Ts'o4a9bf992009-01-03 22:56:44 -0500139 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
140 "commit interval %ld seconds\n", current->pid,
141 journal->j_devname, journal->j_commit_interval / HZ);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142
143 /*
144 * And now, wait forever for commit wakeup events.
145 */
146 spin_lock(&journal->j_state_lock);
147
148loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700149 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150 goto end_loop;
151
152 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
153 journal->j_commit_sequence, journal->j_commit_request);
154
155 if (journal->j_commit_sequence != journal->j_commit_request) {
156 jbd_debug(1, "OK, requests differ\n");
157 spin_unlock(&journal->j_state_lock);
158 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700159 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 spin_lock(&journal->j_state_lock);
161 goto loop;
162 }
163
164 wake_up(&journal->j_wait_done_commit);
165 if (freezing(current)) {
166 /*
167 * The simpler the better. Flushing journal isn't a
168 * good idea, because that depends on threads that may
169 * be already stopped.
170 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700171 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700172 spin_unlock(&journal->j_state_lock);
173 refrigerator();
174 spin_lock(&journal->j_state_lock);
175 } else {
176 /*
177 * We assume on resume that commits are already there,
178 * so we don't sleep
179 */
180 DEFINE_WAIT(wait);
181 int should_sleep = 1;
182
183 prepare_to_wait(&journal->j_wait_commit, &wait,
184 TASK_INTERRUPTIBLE);
185 if (journal->j_commit_sequence != journal->j_commit_request)
186 should_sleep = 0;
187 transaction = journal->j_running_transaction;
188 if (transaction && time_after_eq(jiffies,
189 transaction->t_expires))
190 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700191 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700192 should_sleep = 0;
193 if (should_sleep) {
194 spin_unlock(&journal->j_state_lock);
195 schedule();
196 spin_lock(&journal->j_state_lock);
197 }
198 finish_wait(&journal->j_wait_commit, &wait);
199 }
200
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700201 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700202
203 /*
204 * Were we woken up by a commit wakeup event?
205 */
206 transaction = journal->j_running_transaction;
207 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
208 journal->j_commit_request = transaction->t_tid;
209 jbd_debug(1, "woke because of timeout\n");
210 }
211 goto loop;
212
213end_loop:
214 spin_unlock(&journal->j_state_lock);
215 del_timer_sync(&journal->j_commit_timer);
216 journal->j_task = NULL;
217 wake_up(&journal->j_wait_done_commit);
218 jbd_debug(1, "Journal thread exiting.\n");
219 return 0;
220}
221
Pavel Emelianov97f06782007-05-08 00:30:42 -0700222static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700223{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700224 struct task_struct *t;
225
226 t = kthread_run(kjournald2, journal, "kjournald2");
227 if (IS_ERR(t))
228 return PTR_ERR(t);
229
Al Viro1076d172008-03-29 03:07:18 +0000230 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700231 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232}
233
234static void journal_kill_thread(journal_t *journal)
235{
236 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700237 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238
239 while (journal->j_task) {
240 wake_up(&journal->j_wait_commit);
241 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000242 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700243 spin_lock(&journal->j_state_lock);
244 }
245 spin_unlock(&journal->j_state_lock);
246}
247
248/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700249 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250 *
251 * Writes a metadata buffer to a given disk block. The actual IO is not
252 * performed but a new buffer_head is constructed which labels the data
253 * to be written with the correct destination disk block.
254 *
255 * Any magic-number escaping which needs to be done will cause a
256 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700257 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258 * magic number is only written to the log for descripter blocks. In
259 * this case, we copy the data and replace the first word with 0, and we
260 * return a result code which indicates that this buffer needs to be
261 * marked as an escaped buffer in the corresponding log descriptor
262 * block. The missing word can then be restored when the block is read
263 * during recovery.
264 *
265 * If the source buffer has already been modified by a new transaction
266 * since we took the last commit snapshot, we use the frozen copy of
267 * that data for IO. If we end up using the existing buffer_head's data
268 * for the write, then we *have* to lock the buffer to prevent anyone
269 * else from using and possibly modifying it while the IO is in
270 * progress.
271 *
272 * The function returns a pointer to the buffer_heads to be used for IO.
273 *
274 * We assume that the journal has already been locked in this function.
275 *
276 * Return value:
277 * <0: Error
278 * >=0: Finished OK
279 *
280 * On success:
281 * Bit 0 set == escape performed on the data
282 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
283 */
284
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700285int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286 struct journal_head *jh_in,
287 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700288 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700289{
290 int need_copy_out = 0;
291 int done_copy_out = 0;
292 int do_escape = 0;
293 char *mapped_data;
294 struct buffer_head *new_bh;
295 struct journal_head *new_jh;
296 struct page *new_page;
297 unsigned int new_offset;
298 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700299 struct jbd2_buffer_trigger_type *triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700300
301 /*
302 * The buffer really shouldn't be locked: only the current committing
303 * transaction is allowed to write it, so nobody else is allowed
304 * to do any IO.
305 *
306 * akpm: except if we're journalling data, and write() output is
307 * also part of a shared mapping, and another thread has
308 * decided to launch a writepage() against this buffer.
309 */
310 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
311
312 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
313
314 /*
315 * If a new transaction has already done a buffer copy-out, then
316 * we use that version of the data for the commit.
317 */
318 jbd_lock_bh_state(bh_in);
319repeat:
320 if (jh_in->b_frozen_data) {
321 done_copy_out = 1;
322 new_page = virt_to_page(jh_in->b_frozen_data);
323 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700324 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325 } else {
326 new_page = jh2bh(jh_in)->b_page;
327 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700328 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700329 }
330
331 mapped_data = kmap_atomic(new_page, KM_USER0);
332 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700333 * Fire any commit trigger. Do this before checking for escaping,
334 * as the trigger may modify the magic offset. If a copy-out
335 * happens afterwards, it will have the correct data in the buffer.
336 */
337 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
338 triggers);
339
340 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 * Check for escaping
342 */
343 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700344 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700345 need_copy_out = 1;
346 do_escape = 1;
347 }
348 kunmap_atomic(mapped_data, KM_USER0);
349
350 /*
351 * Do we need to do a data copy?
352 */
353 if (need_copy_out && !done_copy_out) {
354 char *tmp;
355
356 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400357 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700358 jbd_lock_bh_state(bh_in);
359 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400360 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700361 goto repeat;
362 }
363
364 jh_in->b_frozen_data = tmp;
365 mapped_data = kmap_atomic(new_page, KM_USER0);
366 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
367 kunmap_atomic(mapped_data, KM_USER0);
368
369 new_page = virt_to_page(tmp);
370 new_offset = offset_in_page(tmp);
371 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700372
373 /*
374 * This isn't strictly necessary, as we're using frozen
375 * data for the escaping, but it keeps consistency with
376 * b_frozen_data usage.
377 */
378 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700379 }
380
381 /*
382 * Did we need to do an escaping? Now we've done all the
383 * copying, we can finally do so.
384 */
385 if (do_escape) {
386 mapped_data = kmap_atomic(new_page, KM_USER0);
387 *((unsigned int *)(mapped_data + new_offset)) = 0;
388 kunmap_atomic(mapped_data, KM_USER0);
389 }
390
391 /* keep subsequent assertions sane */
392 new_bh->b_state = 0;
393 init_buffer(new_bh, NULL, NULL);
394 atomic_set(&new_bh->b_count, 1);
395 jbd_unlock_bh_state(bh_in);
396
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700397 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700398
399 set_bh_page(new_bh, new_page, new_offset);
400 new_jh->b_transaction = NULL;
401 new_bh->b_size = jh2bh(jh_in)->b_size;
402 new_bh->b_bdev = transaction->t_journal->j_dev;
403 new_bh->b_blocknr = blocknr;
404 set_buffer_mapped(new_bh);
405 set_buffer_dirty(new_bh);
406
407 *jh_out = new_jh;
408
409 /*
410 * The to-be-written buffer needs to get moved to the io queue,
411 * and the original buffer whose contents we are shadowing or
412 * copying is moved to the transaction's shadow queue.
413 */
414 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700415 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700416 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700417 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700418
419 return do_escape | (done_copy_out << 1);
420}
421
422/*
423 * Allocation code for the journal file. Manage the space left in the
424 * journal, so that we can begin checkpointing when appropriate.
425 */
426
427/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700428 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700429 *
430 * Called with the journal already locked.
431 *
432 * Called under j_state_lock
433 */
434
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700435int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700436{
437 int left = journal->j_free;
438
439 assert_spin_locked(&journal->j_state_lock);
440
441 /*
442 * Be pessimistic here about the number of those free blocks which
443 * might be required for log descriptor control blocks.
444 */
445
446#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
447
448 left -= MIN_LOG_RESERVED_BLOCKS;
449
450 if (left <= 0)
451 return 0;
452 left -= (left >> 3);
453 return left;
454}
455
456/*
Jan Karac88ccea2009-02-10 11:27:46 -0500457 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700458 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700459int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700460{
461 /*
462 * Are we already doing a recent enough commit?
463 */
464 if (!tid_geq(journal->j_commit_request, target)) {
465 /*
466 * We want a new commit: OK, mark the request and wakup the
467 * commit thread. We do _not_ do the commit ourselves.
468 */
469
470 journal->j_commit_request = target;
471 jbd_debug(1, "JBD: requesting commit %d/%d\n",
472 journal->j_commit_request,
473 journal->j_commit_sequence);
474 wake_up(&journal->j_wait_commit);
475 return 1;
476 }
477 return 0;
478}
479
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700480int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700481{
482 int ret;
483
484 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700485 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486 spin_unlock(&journal->j_state_lock);
487 return ret;
488}
489
490/*
491 * Force and wait upon a commit if the calling process is not within
492 * transaction. This is used for forcing out undo-protected data which contains
493 * bitmaps, when the fs is running out of space.
494 *
495 * We can only force the running transaction if we don't have an active handle;
496 * otherwise, we will deadlock.
497 *
498 * Returns true if a transaction was started.
499 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700500int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700501{
502 transaction_t *transaction = NULL;
503 tid_t tid;
504
505 spin_lock(&journal->j_state_lock);
506 if (journal->j_running_transaction && !current->journal_info) {
507 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700508 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700509 } else if (journal->j_committing_transaction)
510 transaction = journal->j_committing_transaction;
511
512 if (!transaction) {
513 spin_unlock(&journal->j_state_lock);
514 return 0; /* Nothing to retry */
515 }
516
517 tid = transaction->t_tid;
518 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700519 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 return 1;
521}
522
523/*
524 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500525 * if a transaction is going to be committed (or is currently already
526 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700528int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529{
530 int ret = 0;
531
532 spin_lock(&journal->j_state_lock);
533 if (journal->j_running_transaction) {
534 tid_t tid = journal->j_running_transaction->t_tid;
535
Jan Karac88ccea2009-02-10 11:27:46 -0500536 __jbd2_log_start_commit(journal, tid);
537 /* There's a running transaction and we've just made sure
538 * it's commit has been scheduled. */
539 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500541 ret = 1;
542 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700543 /*
544 * If ext3_write_super() recently started a commit, then we
545 * have to wait for completion of that transaction
546 */
Jan Karac88ccea2009-02-10 11:27:46 -0500547 if (ptid)
548 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700549 ret = 1;
550 }
551 spin_unlock(&journal->j_state_lock);
552 return ret;
553}
554
555/*
556 * Wait for a specified commit to complete.
557 * The caller may not hold the journal lock.
558 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700559int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700560{
561 int err = 0;
562
Jose R. Santose23291b2007-07-18 08:57:06 -0400563#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564 spin_lock(&journal->j_state_lock);
565 if (!tid_geq(journal->j_commit_request, tid)) {
566 printk(KERN_EMERG
567 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400568 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700569 }
570 spin_unlock(&journal->j_state_lock);
571#endif
572 spin_lock(&journal->j_state_lock);
573 while (tid_gt(tid, journal->j_commit_sequence)) {
574 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
575 tid, journal->j_commit_sequence);
576 wake_up(&journal->j_wait_commit);
577 spin_unlock(&journal->j_state_lock);
578 wait_event(journal->j_wait_done_commit,
579 !tid_gt(tid, journal->j_commit_sequence));
580 spin_lock(&journal->j_state_lock);
581 }
582 spin_unlock(&journal->j_state_lock);
583
584 if (unlikely(is_journal_aborted(journal))) {
585 printk(KERN_EMERG "journal commit I/O error\n");
586 err = -EIO;
587 }
588 return err;
589}
590
591/*
592 * Log buffer allocation routines:
593 */
594
Mingming Cao18eba7a2006-10-11 01:21:13 -0700595int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700596{
597 unsigned long blocknr;
598
599 spin_lock(&journal->j_state_lock);
600 J_ASSERT(journal->j_free > 1);
601
602 blocknr = journal->j_head;
603 journal->j_head++;
604 journal->j_free--;
605 if (journal->j_head == journal->j_last)
606 journal->j_head = journal->j_first;
607 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700608 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700609}
610
611/*
612 * Conversion of logical to physical block numbers for the journal
613 *
614 * On external journals the journal blocks are identity-mapped, so
615 * this is a no-op. If needed, we can use j_blk_offset - everything is
616 * ready.
617 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700618int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700619 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700620{
621 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700622 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623
624 if (journal->j_inode) {
625 ret = bmap(journal->j_inode, blocknr);
626 if (ret)
627 *retp = ret;
628 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700629 printk(KERN_ALERT "%s: journal block not found "
630 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400631 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700632 err = -EIO;
633 __journal_abort_soft(journal, err);
634 }
635 } else {
636 *retp = blocknr; /* +journal->j_blk_offset */
637 }
638 return err;
639}
640
641/*
642 * We play buffer_head aliasing tricks to write data/metadata blocks to
643 * the journal without copying their contents, but for journal
644 * descriptor blocks we do need to generate bona fide buffers.
645 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700646 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
648 * But we don't bother doing that, so there will be coherency problems with
649 * mmaps of blockdevs which hold live JBD-controlled filesystems.
650 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700651struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700652{
653 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700654 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700655 int err;
656
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700657 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700658
659 if (err)
660 return NULL;
661
662 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500663 if (!bh)
664 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700665 lock_buffer(bh);
666 memset(bh->b_data, 0, journal->j_blocksize);
667 set_buffer_uptodate(bh);
668 unlock_buffer(bh);
669 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700670 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671}
672
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500673struct jbd2_stats_proc_session {
674 journal_t *journal;
675 struct transaction_stats_s *stats;
676 int start;
677 int max;
678};
679
680static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
681 struct transaction_stats_s *ts,
682 int first)
683{
684 if (ts == s->stats + s->max)
685 ts = s->stats;
686 if (!first && ts == s->stats + s->start)
687 return NULL;
688 while (ts->ts_type == 0) {
689 ts++;
690 if (ts == s->stats + s->max)
691 ts = s->stats;
692 if (ts == s->stats + s->start)
693 return NULL;
694 }
695 return ts;
696
697}
698
699static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
700{
701 struct jbd2_stats_proc_session *s = seq->private;
702 struct transaction_stats_s *ts;
703 int l = *pos;
704
705 if (l == 0)
706 return SEQ_START_TOKEN;
707 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
708 if (!ts)
709 return NULL;
710 l--;
711 while (l) {
712 ts = jbd2_history_skip_empty(s, ++ts, 0);
713 if (!ts)
714 break;
715 l--;
716 }
717 return ts;
718}
719
720static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
721{
722 struct jbd2_stats_proc_session *s = seq->private;
723 struct transaction_stats_s *ts = v;
724
725 ++*pos;
726 if (v == SEQ_START_TOKEN)
727 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
728 else
729 return jbd2_history_skip_empty(s, ++ts, 0);
730}
731
732static int jbd2_seq_history_show(struct seq_file *seq, void *v)
733{
734 struct transaction_stats_s *ts = v;
735 if (v == SEQ_START_TOKEN) {
736 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
737 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
738 "wait", "run", "lock", "flush", "log", "hndls",
739 "block", "inlog", "ctime", "write", "drop",
740 "close");
741 return 0;
742 }
743 if (ts->ts_type == JBD2_STATS_RUN)
744 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
745 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
746 jiffies_to_msecs(ts->u.run.rs_wait),
747 jiffies_to_msecs(ts->u.run.rs_running),
748 jiffies_to_msecs(ts->u.run.rs_locked),
749 jiffies_to_msecs(ts->u.run.rs_flushing),
750 jiffies_to_msecs(ts->u.run.rs_logging),
751 ts->u.run.rs_handle_count,
752 ts->u.run.rs_blocks,
753 ts->u.run.rs_blocks_logged);
754 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
755 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
756 "C", ts->ts_tid, " ",
757 jiffies_to_msecs(ts->u.chp.cs_chp_time),
758 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
759 ts->u.chp.cs_forced_to_close);
760 else
761 J_ASSERT(0);
762 return 0;
763}
764
765static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
766{
767}
768
769static struct seq_operations jbd2_seq_history_ops = {
770 .start = jbd2_seq_history_start,
771 .next = jbd2_seq_history_next,
772 .stop = jbd2_seq_history_stop,
773 .show = jbd2_seq_history_show,
774};
775
776static int jbd2_seq_history_open(struct inode *inode, struct file *file)
777{
778 journal_t *journal = PDE(inode)->data;
779 struct jbd2_stats_proc_session *s;
780 int rc, size;
781
782 s = kmalloc(sizeof(*s), GFP_KERNEL);
783 if (s == NULL)
784 return -ENOMEM;
785 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
786 s->stats = kmalloc(size, GFP_KERNEL);
787 if (s->stats == NULL) {
788 kfree(s);
789 return -ENOMEM;
790 }
791 spin_lock(&journal->j_history_lock);
792 memcpy(s->stats, journal->j_history, size);
793 s->max = journal->j_history_max;
794 s->start = journal->j_history_cur % s->max;
795 spin_unlock(&journal->j_history_lock);
796
797 rc = seq_open(file, &jbd2_seq_history_ops);
798 if (rc == 0) {
799 struct seq_file *m = file->private_data;
800 m->private = s;
801 } else {
802 kfree(s->stats);
803 kfree(s);
804 }
805 return rc;
806
807}
808
809static int jbd2_seq_history_release(struct inode *inode, struct file *file)
810{
811 struct seq_file *seq = file->private_data;
812 struct jbd2_stats_proc_session *s = seq->private;
813
814 kfree(s->stats);
815 kfree(s);
816 return seq_release(inode, file);
817}
818
819static struct file_operations jbd2_seq_history_fops = {
820 .owner = THIS_MODULE,
821 .open = jbd2_seq_history_open,
822 .read = seq_read,
823 .llseek = seq_lseek,
824 .release = jbd2_seq_history_release,
825};
826
827static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
828{
829 return *pos ? NULL : SEQ_START_TOKEN;
830}
831
832static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
833{
834 return NULL;
835}
836
837static int jbd2_seq_info_show(struct seq_file *seq, void *v)
838{
839 struct jbd2_stats_proc_session *s = seq->private;
840
841 if (v != SEQ_START_TOKEN)
842 return 0;
843 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
844 s->stats->ts_tid,
845 s->journal->j_max_transaction_buffers);
846 if (s->stats->ts_tid == 0)
847 return 0;
848 seq_printf(seq, "average: \n %ums waiting for transaction\n",
849 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
850 seq_printf(seq, " %ums running transaction\n",
851 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
852 seq_printf(seq, " %ums transaction was being locked\n",
853 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
854 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
855 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
856 seq_printf(seq, " %ums logging transaction\n",
857 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500858 seq_printf(seq, " %lluus average transaction commit time\n",
859 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500860 seq_printf(seq, " %lu handles per transaction\n",
861 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
862 seq_printf(seq, " %lu blocks per transaction\n",
863 s->stats->u.run.rs_blocks / s->stats->ts_tid);
864 seq_printf(seq, " %lu logged blocks per transaction\n",
865 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
866 return 0;
867}
868
869static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
870{
871}
872
873static struct seq_operations jbd2_seq_info_ops = {
874 .start = jbd2_seq_info_start,
875 .next = jbd2_seq_info_next,
876 .stop = jbd2_seq_info_stop,
877 .show = jbd2_seq_info_show,
878};
879
880static int jbd2_seq_info_open(struct inode *inode, struct file *file)
881{
882 journal_t *journal = PDE(inode)->data;
883 struct jbd2_stats_proc_session *s;
884 int rc, size;
885
886 s = kmalloc(sizeof(*s), GFP_KERNEL);
887 if (s == NULL)
888 return -ENOMEM;
889 size = sizeof(struct transaction_stats_s);
890 s->stats = kmalloc(size, GFP_KERNEL);
891 if (s->stats == NULL) {
892 kfree(s);
893 return -ENOMEM;
894 }
895 spin_lock(&journal->j_history_lock);
896 memcpy(s->stats, &journal->j_stats, size);
897 s->journal = journal;
898 spin_unlock(&journal->j_history_lock);
899
900 rc = seq_open(file, &jbd2_seq_info_ops);
901 if (rc == 0) {
902 struct seq_file *m = file->private_data;
903 m->private = s;
904 } else {
905 kfree(s->stats);
906 kfree(s);
907 }
908 return rc;
909
910}
911
912static int jbd2_seq_info_release(struct inode *inode, struct file *file)
913{
914 struct seq_file *seq = file->private_data;
915 struct jbd2_stats_proc_session *s = seq->private;
916 kfree(s->stats);
917 kfree(s);
918 return seq_release(inode, file);
919}
920
921static struct file_operations jbd2_seq_info_fops = {
922 .owner = THIS_MODULE,
923 .open = jbd2_seq_info_open,
924 .read = seq_read,
925 .llseek = seq_lseek,
926 .release = jbd2_seq_info_release,
927};
928
929static struct proc_dir_entry *proc_jbd2_stats;
930
931static void jbd2_stats_proc_init(journal_t *journal)
932{
Theodore Ts'o05496762008-09-16 14:36:17 -0400933 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500934 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700935 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
936 &jbd2_seq_history_fops, journal);
937 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
938 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500939 }
940}
941
942static void jbd2_stats_proc_exit(journal_t *journal)
943{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500944 remove_proc_entry("info", journal->j_proc_entry);
945 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400946 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500947}
948
949static void journal_init_stats(journal_t *journal)
950{
951 int size;
952
953 if (!proc_jbd2_stats)
954 return;
955
956 journal->j_history_max = 100;
957 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
958 journal->j_history = kzalloc(size, GFP_KERNEL);
959 if (!journal->j_history) {
960 journal->j_history_max = 0;
961 return;
962 }
963 spin_lock_init(&journal->j_history_lock);
964}
965
Dave Kleikamp470decc2006-10-11 01:20:57 -0700966/*
967 * Management for journal control blocks: functions to create and
968 * destroy journal_t structures, and to initialise and read existing
969 * journal blocks from disk. */
970
971/* First: create and setup a journal_t object in memory. We initialise
972 * very few fields yet: that has to wait until we have created the
973 * journal structures from from scratch, or loaded them from disk. */
974
975static journal_t * journal_init_common (void)
976{
977 journal_t *journal;
978 int err;
979
Mingming Caod802ffa2007-10-16 18:38:25 -0400980 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700981 if (!journal)
982 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700983
984 init_waitqueue_head(&journal->j_wait_transaction_locked);
985 init_waitqueue_head(&journal->j_wait_logspace);
986 init_waitqueue_head(&journal->j_wait_done_commit);
987 init_waitqueue_head(&journal->j_wait_checkpoint);
988 init_waitqueue_head(&journal->j_wait_commit);
989 init_waitqueue_head(&journal->j_wait_updates);
990 mutex_init(&journal->j_barrier);
991 mutex_init(&journal->j_checkpoint_mutex);
992 spin_lock_init(&journal->j_revoke_lock);
993 spin_lock_init(&journal->j_list_lock);
994 spin_lock_init(&journal->j_state_lock);
995
Mingming Caocd02ff02007-10-16 18:38:25 -0400996 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500997 journal->j_min_batch_time = 0;
998 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700999
1000 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001001 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002
1003 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001004 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001005 if (err) {
1006 kfree(journal);
1007 goto fail;
1008 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001009
1010 journal_init_stats(journal);
1011
Dave Kleikamp470decc2006-10-11 01:20:57 -07001012 return journal;
1013fail:
1014 return NULL;
1015}
1016
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001017/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001018 *
1019 * Create a journal structure assigned some fixed set of disk blocks to
1020 * the journal. We don't actually touch those disk blocks yet, but we
1021 * need to set up all of the mapping information to tell the journaling
1022 * system where the journal blocks are.
1023 *
1024 */
1025
1026/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001027 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001028 * @bdev: Block device on which to create the journal
1029 * @fs_dev: Device which hold journalled filesystem for this journal.
1030 * @start: Block nr Start of journal.
1031 * @len: Length of the journal in blocks.
1032 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001033 *
1034 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001035 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001036 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001037 * range of blocks on an arbitrary block device.
1038 *
1039 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001040journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001041 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001042 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043{
1044 journal_t *journal = journal_init_common();
1045 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001046 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001047 int n;
1048
1049 if (!journal)
1050 return NULL;
1051
1052 /* journal descriptor can store up to n blocks -bzzz */
1053 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -05001054 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001055 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1056 journal->j_wbufsize = n;
1057 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1058 if (!journal->j_wbuf) {
1059 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001060 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001061 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001062 }
1063 journal->j_dev = bdev;
1064 journal->j_fs_dev = fs_dev;
1065 journal->j_blk_offset = start;
1066 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001067 bdevname(journal->j_dev, journal->j_devname);
1068 p = journal->j_devname;
1069 while ((p = strchr(p, '/')))
1070 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -07001071
1072 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001073 if (!bh) {
1074 printk(KERN_ERR
1075 "%s: Cannot get buffer for journal superblock\n",
1076 __func__);
1077 goto out_err;
1078 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079 journal->j_sb_buffer = bh;
1080 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001081
Dave Kleikamp470decc2006-10-11 01:20:57 -07001082 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001083out_err:
1084 jbd2_stats_proc_exit(journal);
1085 kfree(journal);
1086 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001087}
1088
1089/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001090 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001091 * @inode: An inode to create the journal in
1092 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001093 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094 * the journal. The inode must exist already, must support bmap() and
1095 * must have all data blocks preallocated.
1096 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001097journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098{
1099 struct buffer_head *bh;
1100 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001101 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001102 int err;
1103 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001104 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001105
1106 if (!journal)
1107 return NULL;
1108
1109 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1110 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001111 bdevname(journal->j_dev, journal->j_devname);
1112 p = journal->j_devname;
1113 while ((p = strchr(p, '/')))
1114 *p = '!';
1115 p = journal->j_devname + strlen(journal->j_devname);
1116 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117 jbd_debug(1,
1118 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1119 journal, inode->i_sb->s_id, inode->i_ino,
1120 (long long) inode->i_size,
1121 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1122
1123 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1124 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001125 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001126
1127 /* journal descriptor can store up to n blocks -bzzz */
1128 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1129 journal->j_wbufsize = n;
1130 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1131 if (!journal->j_wbuf) {
1132 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001133 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001134 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001135 }
1136
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001137 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001138 /* If that failed, give up */
1139 if (err) {
1140 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001141 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001142 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143 }
1144
1145 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001146 if (!bh) {
1147 printk(KERN_ERR
1148 "%s: Cannot get buffer for journal superblock\n",
1149 __func__);
1150 goto out_err;
1151 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001152 journal->j_sb_buffer = bh;
1153 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1154
1155 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001156out_err:
1157 jbd2_stats_proc_exit(journal);
1158 kfree(journal);
1159 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160}
1161
1162/*
1163 * If the journal init or create aborts, we need to mark the journal
1164 * superblock as being NULL to prevent the journal destroy from writing
1165 * back a bogus superblock.
1166 */
1167static void journal_fail_superblock (journal_t *journal)
1168{
1169 struct buffer_head *bh = journal->j_sb_buffer;
1170 brelse(bh);
1171 journal->j_sb_buffer = NULL;
1172}
1173
1174/*
1175 * Given a journal_t structure, initialise the various fields for
1176 * startup of a new journaling session. We use this both when creating
1177 * a journal, and after recovering an old journal to reset it for
1178 * subsequent use.
1179 */
1180
1181static int journal_reset(journal_t *journal)
1182{
1183 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001184 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001185
1186 first = be32_to_cpu(sb->s_first);
1187 last = be32_to_cpu(sb->s_maxlen);
1188
1189 journal->j_first = first;
1190 journal->j_last = last;
1191
1192 journal->j_head = first;
1193 journal->j_tail = first;
1194 journal->j_free = last - first;
1195
1196 journal->j_tail_sequence = journal->j_transaction_sequence;
1197 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1198 journal->j_commit_request = journal->j_commit_sequence;
1199
1200 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1201
1202 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001203 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001204 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001205}
1206
1207/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001208 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001209 * @journal: The journal to update.
1210 * @wait: Set to '0' if you don't want to wait for IO completion.
1211 *
1212 * Update a journal's dynamic superblock fields and write it to disk,
1213 * optionally waiting for the IO to complete.
1214 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001215void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001216{
1217 journal_superblock_t *sb = journal->j_superblock;
1218 struct buffer_head *bh = journal->j_sb_buffer;
1219
1220 /*
1221 * As a special case, if the on-disk copy is already marked as needing
1222 * no recovery (s_start == 0) and there are no outstanding transactions
1223 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001224 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001225 * attempting a write to a potential-readonly device.
1226 */
1227 if (sb->s_start == 0 && journal->j_tail_sequence ==
1228 journal->j_transaction_sequence) {
1229 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1230 "(start %ld, seq %d, errno %d)\n",
1231 journal->j_tail, journal->j_tail_sequence,
1232 journal->j_errno);
1233 goto out;
1234 }
1235
Theodore Ts'o914258b2008-10-06 21:35:40 -04001236 if (buffer_write_io_error(bh)) {
1237 /*
1238 * Oh, dear. A previous attempt to write the journal
1239 * superblock failed. This could happen because the
1240 * USB device was yanked out. Or it could happen to
1241 * be a transient write error and maybe the block will
1242 * be remapped. Nothing we can do but to retry the
1243 * write and hope for the best.
1244 */
1245 printk(KERN_ERR "JBD2: previous I/O error detected "
1246 "for journal superblock update for %s.\n",
1247 journal->j_devname);
1248 clear_buffer_write_io_error(bh);
1249 set_buffer_uptodate(bh);
1250 }
1251
Dave Kleikamp470decc2006-10-11 01:20:57 -07001252 spin_lock(&journal->j_state_lock);
1253 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1254 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1255
1256 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1257 sb->s_start = cpu_to_be32(journal->j_tail);
1258 sb->s_errno = cpu_to_be32(journal->j_errno);
1259 spin_unlock(&journal->j_state_lock);
1260
1261 BUFFER_TRACE(bh, "marking dirty");
1262 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001263 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001264 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001265 if (buffer_write_io_error(bh)) {
1266 printk(KERN_ERR "JBD2: I/O error detected "
1267 "when updating journal superblock for %s.\n",
1268 journal->j_devname);
1269 clear_buffer_write_io_error(bh);
1270 set_buffer_uptodate(bh);
1271 }
1272 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001273 ll_rw_block(SWRITE, 1, &bh);
1274
1275out:
1276 /* If we have just flushed the log (by marking s_start==0), then
1277 * any future commit will have to be careful to update the
1278 * superblock again to re-record the true start of the log. */
1279
1280 spin_lock(&journal->j_state_lock);
1281 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001282 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001283 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001284 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001285 spin_unlock(&journal->j_state_lock);
1286}
1287
1288/*
1289 * Read the superblock for a given journal, performing initial
1290 * validation of the format.
1291 */
1292
1293static int journal_get_superblock(journal_t *journal)
1294{
1295 struct buffer_head *bh;
1296 journal_superblock_t *sb;
1297 int err = -EIO;
1298
1299 bh = journal->j_sb_buffer;
1300
1301 J_ASSERT(bh != NULL);
1302 if (!buffer_uptodate(bh)) {
1303 ll_rw_block(READ, 1, &bh);
1304 wait_on_buffer(bh);
1305 if (!buffer_uptodate(bh)) {
1306 printk (KERN_ERR
1307 "JBD: IO error reading journal superblock\n");
1308 goto out;
1309 }
1310 }
1311
1312 sb = journal->j_superblock;
1313
1314 err = -EINVAL;
1315
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001316 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001317 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1318 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1319 goto out;
1320 }
1321
1322 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001323 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001324 journal->j_format_version = 1;
1325 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001326 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001327 journal->j_format_version = 2;
1328 break;
1329 default:
1330 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1331 goto out;
1332 }
1333
1334 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1335 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1336 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1337 printk (KERN_WARNING "JBD: journal file too short\n");
1338 goto out;
1339 }
1340
1341 return 0;
1342
1343out:
1344 journal_fail_superblock(journal);
1345 return err;
1346}
1347
1348/*
1349 * Load the on-disk journal superblock and read the key fields into the
1350 * journal_t.
1351 */
1352
1353static int load_superblock(journal_t *journal)
1354{
1355 int err;
1356 journal_superblock_t *sb;
1357
1358 err = journal_get_superblock(journal);
1359 if (err)
1360 return err;
1361
1362 sb = journal->j_superblock;
1363
1364 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1365 journal->j_tail = be32_to_cpu(sb->s_start);
1366 journal->j_first = be32_to_cpu(sb->s_first);
1367 journal->j_last = be32_to_cpu(sb->s_maxlen);
1368 journal->j_errno = be32_to_cpu(sb->s_errno);
1369
1370 return 0;
1371}
1372
1373
1374/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001375 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001376 * @journal: Journal to act on.
1377 *
1378 * Given a journal_t structure which tells us which disk blocks contain
1379 * a journal, read the journal from disk to initialise the in-memory
1380 * structures.
1381 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001382int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001383{
1384 int err;
1385 journal_superblock_t *sb;
1386
1387 err = load_superblock(journal);
1388 if (err)
1389 return err;
1390
1391 sb = journal->j_superblock;
1392 /* If this is a V2 superblock, then we have to check the
1393 * features flags on it. */
1394
1395 if (journal->j_format_version >= 2) {
1396 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001397 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001398 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001399 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 printk (KERN_WARNING
1401 "JBD: Unrecognised features on journal\n");
1402 return -EINVAL;
1403 }
1404 }
1405
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 /* Let the recovery code check whether it needs to recover any
1407 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001408 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001409 goto recovery_error;
1410
1411 /* OK, we've finished with the dynamic journal bits:
1412 * reinitialise the dynamic contents of the superblock in memory
1413 * and reset them on disk. */
1414 if (journal_reset(journal))
1415 goto recovery_error;
1416
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001417 journal->j_flags &= ~JBD2_ABORT;
1418 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001419 return 0;
1420
1421recovery_error:
1422 printk (KERN_WARNING "JBD: recovery failed\n");
1423 return -EIO;
1424}
1425
1426/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001427 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428 * @journal: Journal to act on.
1429 *
1430 * Release a journal_t structure once it is no longer in use by the
1431 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001432 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001433 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001434int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001436 int err = 0;
1437
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438 /* Wait for the commit thread to wake up and die. */
1439 journal_kill_thread(journal);
1440
1441 /* Force a final log commit */
1442 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001443 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001444
1445 /* Force any old transactions to disk */
1446
1447 /* Totally anal locking here... */
1448 spin_lock(&journal->j_list_lock);
1449 while (journal->j_checkpoint_transactions != NULL) {
1450 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001451 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001452 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001453 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001454 spin_lock(&journal->j_list_lock);
1455 }
1456
1457 J_ASSERT(journal->j_running_transaction == NULL);
1458 J_ASSERT(journal->j_committing_transaction == NULL);
1459 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1460 spin_unlock(&journal->j_list_lock);
1461
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001463 if (!is_journal_aborted(journal)) {
1464 /* We can now mark the journal as empty. */
1465 journal->j_tail = 0;
1466 journal->j_tail_sequence =
1467 ++journal->j_transaction_sequence;
1468 jbd2_journal_update_superblock(journal, 1);
1469 } else {
1470 err = -EIO;
1471 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 brelse(journal->j_sb_buffer);
1473 }
1474
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001475 if (journal->j_proc_entry)
1476 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 if (journal->j_inode)
1478 iput(journal->j_inode);
1479 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001480 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481 kfree(journal->j_wbuf);
1482 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001483
1484 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001485}
1486
1487
1488/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001489 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001490 * @journal: Journal to check.
1491 * @compat: bitmask of compatible features
1492 * @ro: bitmask of features that force read-only mount
1493 * @incompat: bitmask of incompatible features
1494 *
1495 * Check whether the journal uses all of a given set of
1496 * features. Return true (non-zero) if it does.
1497 **/
1498
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001499int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500 unsigned long ro, unsigned long incompat)
1501{
1502 journal_superblock_t *sb;
1503
1504 if (!compat && !ro && !incompat)
1505 return 1;
1506 if (journal->j_format_version == 1)
1507 return 0;
1508
1509 sb = journal->j_superblock;
1510
1511 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1512 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1513 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1514 return 1;
1515
1516 return 0;
1517}
1518
1519/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001520 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001521 * @journal: Journal to check.
1522 * @compat: bitmask of compatible features
1523 * @ro: bitmask of features that force read-only mount
1524 * @incompat: bitmask of incompatible features
1525 *
1526 * Check whether the journaling code supports the use of
1527 * all of a given set of features on this journal. Return true
1528 * (non-zero) if it can. */
1529
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001530int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001531 unsigned long ro, unsigned long incompat)
1532{
1533 journal_superblock_t *sb;
1534
1535 if (!compat && !ro && !incompat)
1536 return 1;
1537
1538 sb = journal->j_superblock;
1539
1540 /* We can support any known requested features iff the
1541 * superblock is in version 2. Otherwise we fail to support any
1542 * extended sb features. */
1543
1544 if (journal->j_format_version != 2)
1545 return 0;
1546
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001547 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1548 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1549 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001550 return 1;
1551
1552 return 0;
1553}
1554
1555/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001556 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001557 * @journal: Journal to act on.
1558 * @compat: bitmask of compatible features
1559 * @ro: bitmask of features that force read-only mount
1560 * @incompat: bitmask of incompatible features
1561 *
1562 * Mark a given journal feature as present on the
1563 * superblock. Returns true if the requested features could be set.
1564 *
1565 */
1566
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001568 unsigned long ro, unsigned long incompat)
1569{
1570 journal_superblock_t *sb;
1571
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001572 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001573 return 1;
1574
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001575 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001576 return 0;
1577
1578 jbd_debug(1, "Setting new 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
1587 return 1;
1588}
1589
Girish Shilamkar818d2762008-01-28 23:58:27 -05001590/*
1591 * jbd2_journal_clear_features () - Clear a given journal feature in the
1592 * superblock
1593 * @journal: Journal to act on.
1594 * @compat: bitmask of compatible features
1595 * @ro: bitmask of features that force read-only mount
1596 * @incompat: bitmask of incompatible features
1597 *
1598 * Clear a given journal feature as present on the
1599 * superblock.
1600 */
1601void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1602 unsigned long ro, unsigned long incompat)
1603{
1604 journal_superblock_t *sb;
1605
1606 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1607 compat, ro, incompat);
1608
1609 sb = journal->j_superblock;
1610
1611 sb->s_feature_compat &= ~cpu_to_be32(compat);
1612 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1613 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1614}
1615EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001616
1617/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001618 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001619 * @journal: Journal to act on.
1620 *
1621 * Given an initialised but unloaded journal struct, poke about in the
1622 * on-disk structure to update it to the most recent supported version.
1623 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001624int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001625{
1626 journal_superblock_t *sb;
1627 int err;
1628
1629 err = journal_get_superblock(journal);
1630 if (err)
1631 return err;
1632
1633 sb = journal->j_superblock;
1634
1635 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001636 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001637 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 return journal_convert_superblock_v1(journal, sb);
1640 default:
1641 break;
1642 }
1643 return -EINVAL;
1644}
1645
1646static int journal_convert_superblock_v1(journal_t *journal,
1647 journal_superblock_t *sb)
1648{
1649 int offset, blocksize;
1650 struct buffer_head *bh;
1651
1652 printk(KERN_WARNING
1653 "JBD: Converting superblock from version 1 to 2.\n");
1654
1655 /* Pre-initialise new fields to zero */
1656 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1657 blocksize = be32_to_cpu(sb->s_blocksize);
1658 memset(&sb->s_feature_compat, 0, blocksize-offset);
1659
1660 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001661 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001662 journal->j_format_version = 2;
1663
1664 bh = journal->j_sb_buffer;
1665 BUFFER_TRACE(bh, "marking dirty");
1666 mark_buffer_dirty(bh);
1667 sync_dirty_buffer(bh);
1668 return 0;
1669}
1670
1671
1672/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001673 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 * @journal: Journal to act on.
1675 *
1676 * Flush all data for a given journal to disk and empty the journal.
1677 * Filesystems can use this when remounting readonly to ensure that
1678 * recovery does not need to happen on remount.
1679 */
1680
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001681int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001682{
1683 int err = 0;
1684 transaction_t *transaction = NULL;
1685 unsigned long old_tail;
1686
1687 spin_lock(&journal->j_state_lock);
1688
1689 /* Force everything buffered to the log... */
1690 if (journal->j_running_transaction) {
1691 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693 } else if (journal->j_committing_transaction)
1694 transaction = journal->j_committing_transaction;
1695
1696 /* Wait for the log commit to complete... */
1697 if (transaction) {
1698 tid_t tid = transaction->t_tid;
1699
1700 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001701 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001702 } else {
1703 spin_unlock(&journal->j_state_lock);
1704 }
1705
1706 /* ...and flush everything in the log out to disk. */
1707 spin_lock(&journal->j_list_lock);
1708 while (!err && journal->j_checkpoint_transactions != NULL) {
1709 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001710 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001711 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001712 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713 spin_lock(&journal->j_list_lock);
1714 }
1715 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001716
1717 if (is_journal_aborted(journal))
1718 return -EIO;
1719
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001720 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001721
1722 /* Finally, mark the journal as really needing no recovery.
1723 * This sets s_start==0 in the underlying superblock, which is
1724 * the magic code for a fully-recovered superblock. Any future
1725 * commits of data to the journal will restore the current
1726 * s_start value. */
1727 spin_lock(&journal->j_state_lock);
1728 old_tail = journal->j_tail;
1729 journal->j_tail = 0;
1730 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732 spin_lock(&journal->j_state_lock);
1733 journal->j_tail = old_tail;
1734
1735 J_ASSERT(!journal->j_running_transaction);
1736 J_ASSERT(!journal->j_committing_transaction);
1737 J_ASSERT(!journal->j_checkpoint_transactions);
1738 J_ASSERT(journal->j_head == journal->j_tail);
1739 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1740 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001741 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001742}
1743
1744/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001745 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001746 * @journal: Journal to act on.
1747 * @write: flag (see below)
1748 *
1749 * Wipe out all of the contents of a journal, safely. This will produce
1750 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001751 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752 *
1753 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1754 * we merely suppress recovery.
1755 */
1756
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001757int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001758{
1759 journal_superblock_t *sb;
1760 int err = 0;
1761
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001762 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001763
1764 err = load_superblock(journal);
1765 if (err)
1766 return err;
1767
1768 sb = journal->j_superblock;
1769
1770 if (!journal->j_tail)
1771 goto no_recovery;
1772
1773 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1774 write ? "Clearing" : "Ignoring");
1775
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001776 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001777 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001778 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779
1780 no_recovery:
1781 return err;
1782}
1783
1784/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001785 * Journal abort has very specific semantics, which we describe
1786 * for journal abort.
1787 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001788 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001789 * itself are here.
1790 */
1791
1792/*
1793 * Quick version for internal journal use (doesn't lock the journal).
1794 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1795 * and don't attempt to make any other journal updates.
1796 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001797void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798{
1799 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001800
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001801 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802 return;
1803
1804 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001805 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806
1807 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001808 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001809 transaction = journal->j_running_transaction;
1810 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001811 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001812 spin_unlock(&journal->j_state_lock);
1813}
1814
1815/* Soft abort: record the abort error status in the journal superblock,
1816 * but don't do any other IO. */
1817static void __journal_abort_soft (journal_t *journal, int errno)
1818{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001819 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820 return;
1821
1822 if (!journal->j_errno)
1823 journal->j_errno = errno;
1824
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001825 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001826
1827 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001828 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001829}
1830
1831/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001832 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001833 * @journal: the journal to shutdown.
1834 * @errno: an error number to record in the journal indicating
1835 * the reason for the shutdown.
1836 *
1837 * Perform a complete, immediate shutdown of the ENTIRE
1838 * journal (not of a single transaction). This operation cannot be
1839 * undone without closing and reopening the journal.
1840 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001841 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1843 * mode.
1844 *
1845 * Journal abort has very specific semantics. Any existing dirty,
1846 * unjournaled buffers in the main filesystem will still be written to
1847 * disk by bdflush, but the journaling mechanism will be suspended
1848 * immediately and no further transaction commits will be honoured.
1849 *
1850 * Any dirty, journaled buffers will be written back to disk without
1851 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1852 * filesystem, but we _do_ attempt to leave as much data as possible
1853 * behind for fsck to use for cleanup.
1854 *
1855 * Any attempt to get a new transaction handle on a journal which is in
1856 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001857 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858 * entered abort state during the update.
1859 *
1860 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001861 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001863 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001864 * which will be recorded (if possible) in the journal superblock. This
1865 * allows a client to record failure conditions in the middle of a
1866 * transaction without having to complete the transaction to record the
1867 * failure to disk. ext3_error, for example, now uses this
1868 * functionality.
1869 *
1870 * Errors which originate from within the journaling layer will NOT
1871 * supply an errno; a null errno implies that absolutely no further
1872 * writes are done to the journal (unless there are any already in
1873 * progress).
1874 *
1875 */
1876
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001877void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001878{
1879 __journal_abort_soft(journal, errno);
1880}
1881
1882/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001883 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001884 * @journal: journal to examine.
1885 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001886 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001887 * time the journal was mounted - if the journal was stopped
1888 * without calling abort this will be 0.
1889 *
1890 * If the journal has been aborted on this mount time -EROFS will
1891 * be returned.
1892 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001893int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001894{
1895 int err;
1896
1897 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001898 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899 err = -EROFS;
1900 else
1901 err = journal->j_errno;
1902 spin_unlock(&journal->j_state_lock);
1903 return err;
1904}
1905
1906/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001907 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001908 * @journal: journal to act on.
1909 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001910 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001911 * mode.
1912 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001913int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001914{
1915 int err = 0;
1916
1917 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 err = -EROFS;
1920 else
1921 journal->j_errno = 0;
1922 spin_unlock(&journal->j_state_lock);
1923 return err;
1924}
1925
1926/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001927 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928 * @journal: journal to act on.
1929 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001930 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001931 * mode.
1932 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001933void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934{
1935 spin_lock(&journal->j_state_lock);
1936 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001937 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001938 spin_unlock(&journal->j_state_lock);
1939}
1940
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001941int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001942{
1943 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1944}
1945
1946/*
Zach Brownb517bea2006-10-11 01:21:08 -07001947 * helper functions to deal with 32 or 64bit block numbers.
1948 */
1949size_t journal_tag_bytes(journal_t *journal)
1950{
1951 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001952 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001953 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001954 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001955}
1956
1957/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001958 * Journal_head storage management
1959 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001960static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001961#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1963#endif
1964
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001965static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001966{
1967 int retval;
1968
Al Viro1076d172008-03-29 03:07:18 +00001969 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001970 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971 sizeof(struct journal_head),
1972 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001973 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001974 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001975 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001976 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001977 retval = -ENOMEM;
1978 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1979 }
1980 return retval;
1981}
1982
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001983static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001985 if (jbd2_journal_head_cache) {
1986 kmem_cache_destroy(jbd2_journal_head_cache);
1987 jbd2_journal_head_cache = NULL;
1988 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001989}
1990
1991/*
1992 * journal_head splicing and dicing
1993 */
1994static struct journal_head *journal_alloc_journal_head(void)
1995{
1996 struct journal_head *ret;
1997 static unsigned long last_warning;
1998
Jose R. Santose23291b2007-07-18 08:57:06 -04001999#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000 atomic_inc(&nr_journal_heads);
2001#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002002 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002003 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 jbd_debug(1, "out of memory for journal_head\n");
2005 if (time_after(jiffies, last_warning + 5*HZ)) {
2006 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002007 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002008 last_warning = jiffies;
2009 }
Al Viro1076d172008-03-29 03:07:18 +00002010 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002012 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 }
2014 }
2015 return ret;
2016}
2017
2018static void journal_free_journal_head(struct journal_head *jh)
2019{
Jose R. Santose23291b2007-07-18 08:57:06 -04002020#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002022 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002024 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002025}
2026
2027/*
2028 * A journal_head is attached to a buffer_head whenever JBD has an
2029 * interest in the buffer.
2030 *
2031 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2032 * is set. This bit is tested in core kernel code where we need to take
2033 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2034 * there.
2035 *
2036 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2037 *
2038 * When a buffer has its BH_JBD bit set it is immune from being released by
2039 * core kernel code, mainly via ->b_count.
2040 *
2041 * A journal_head may be detached from its buffer_head when the journal_head's
2042 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002043 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 * journal_head can be dropped if needed.
2045 *
2046 * Various places in the kernel want to attach a journal_head to a buffer_head
2047 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002048 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002050 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002051 *
2052 * So the typical usage would be:
2053 *
2054 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002055 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002056 * ...
2057 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002058 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002059 *
2060 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2061 * because it has a non-zero b_transaction.
2062 */
2063
2064/*
2065 * Give a buffer_head a journal_head.
2066 *
2067 * Doesn't need the journal lock.
2068 * May sleep.
2069 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002070struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002071{
2072 struct journal_head *jh;
2073 struct journal_head *new_jh = NULL;
2074
2075repeat:
2076 if (!buffer_jbd(bh)) {
2077 new_jh = journal_alloc_journal_head();
2078 memset(new_jh, 0, sizeof(*new_jh));
2079 }
2080
2081 jbd_lock_bh_journal_head(bh);
2082 if (buffer_jbd(bh)) {
2083 jh = bh2jh(bh);
2084 } else {
2085 J_ASSERT_BH(bh,
2086 (atomic_read(&bh->b_count) > 0) ||
2087 (bh->b_page && bh->b_page->mapping));
2088
2089 if (!new_jh) {
2090 jbd_unlock_bh_journal_head(bh);
2091 goto repeat;
2092 }
2093
2094 jh = new_jh;
2095 new_jh = NULL; /* We consumed it */
2096 set_buffer_jbd(bh);
2097 bh->b_private = jh;
2098 jh->b_bh = bh;
2099 get_bh(bh);
2100 BUFFER_TRACE(bh, "added journal_head");
2101 }
2102 jh->b_jcount++;
2103 jbd_unlock_bh_journal_head(bh);
2104 if (new_jh)
2105 journal_free_journal_head(new_jh);
2106 return bh->b_private;
2107}
2108
2109/*
2110 * Grab a ref against this buffer_head's journal_head. If it ended up not
2111 * having a journal_head, return NULL
2112 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002113struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002114{
2115 struct journal_head *jh = NULL;
2116
2117 jbd_lock_bh_journal_head(bh);
2118 if (buffer_jbd(bh)) {
2119 jh = bh2jh(bh);
2120 jh->b_jcount++;
2121 }
2122 jbd_unlock_bh_journal_head(bh);
2123 return jh;
2124}
2125
2126static void __journal_remove_journal_head(struct buffer_head *bh)
2127{
2128 struct journal_head *jh = bh2jh(bh);
2129
2130 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2131
2132 get_bh(bh);
2133 if (jh->b_jcount == 0) {
2134 if (jh->b_transaction == NULL &&
2135 jh->b_next_transaction == NULL &&
2136 jh->b_cp_transaction == NULL) {
2137 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2138 J_ASSERT_BH(bh, buffer_jbd(bh));
2139 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2140 BUFFER_TRACE(bh, "remove journal_head");
2141 if (jh->b_frozen_data) {
2142 printk(KERN_WARNING "%s: freeing "
2143 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002144 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002145 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002146 }
2147 if (jh->b_committed_data) {
2148 printk(KERN_WARNING "%s: freeing "
2149 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002150 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002151 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002152 }
2153 bh->b_private = NULL;
2154 jh->b_bh = NULL; /* debug, really */
2155 clear_buffer_jbd(bh);
2156 __brelse(bh);
2157 journal_free_journal_head(jh);
2158 } else {
2159 BUFFER_TRACE(bh, "journal_head was locked");
2160 }
2161 }
2162}
2163
2164/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002165 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002166 * and has a zero b_jcount then remove and release its journal_head. If we did
2167 * see that the buffer is not used by any transaction we also "logically"
2168 * decrement ->b_count.
2169 *
2170 * We in fact take an additional increment on ->b_count as a convenience,
2171 * because the caller usually wants to do additional things with the bh
2172 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002173 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002174 * time. Once the caller has run __brelse(), the buffer is eligible for
2175 * reaping by try_to_free_buffers().
2176 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002177void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002178{
2179 jbd_lock_bh_journal_head(bh);
2180 __journal_remove_journal_head(bh);
2181 jbd_unlock_bh_journal_head(bh);
2182}
2183
2184/*
2185 * Drop a reference on the passed journal_head. If it fell to zero then try to
2186 * release the journal_head from the buffer_head.
2187 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002188void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002189{
2190 struct buffer_head *bh = jh2bh(jh);
2191
2192 jbd_lock_bh_journal_head(bh);
2193 J_ASSERT_JH(jh, jh->b_jcount > 0);
2194 --jh->b_jcount;
2195 if (!jh->b_jcount && !jh->b_transaction) {
2196 __journal_remove_journal_head(bh);
2197 __brelse(bh);
2198 }
2199 jbd_unlock_bh_journal_head(bh);
2200}
2201
2202/*
Jan Karac851ed52008-07-11 19:27:31 -04002203 * Initialize jbd inode head
2204 */
2205void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2206{
2207 jinode->i_transaction = NULL;
2208 jinode->i_next_transaction = NULL;
2209 jinode->i_vfs_inode = inode;
2210 jinode->i_flags = 0;
2211 INIT_LIST_HEAD(&jinode->i_list);
2212}
2213
2214/*
2215 * Function to be called before we start removing inode from memory (i.e.,
2216 * clear_inode() is a fine place to be called from). It removes inode from
2217 * transaction's lists.
2218 */
2219void jbd2_journal_release_jbd_inode(journal_t *journal,
2220 struct jbd2_inode *jinode)
2221{
2222 int writeout = 0;
2223
2224 if (!journal)
2225 return;
2226restart:
2227 spin_lock(&journal->j_list_lock);
2228 /* Is commit writing out inode - we have to wait */
2229 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2230 wait_queue_head_t *wq;
2231 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2232 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2233 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2234 spin_unlock(&journal->j_list_lock);
2235 schedule();
2236 finish_wait(wq, &wait.wait);
2237 goto restart;
2238 }
2239
2240 /* Do we need to wait for data writeback? */
2241 if (journal->j_committing_transaction == jinode->i_transaction)
2242 writeout = 1;
2243 if (jinode->i_transaction) {
2244 list_del(&jinode->i_list);
2245 jinode->i_transaction = NULL;
2246 }
2247 spin_unlock(&journal->j_list_lock);
2248}
2249
2250/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002251 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002252 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002253#ifdef CONFIG_JBD2_DEBUG
2254u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002255EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002256
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002257#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002258
Jose R. Santos6f38c742007-10-16 18:38:25 -04002259static struct dentry *jbd2_debugfs_dir;
2260static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002261
2262static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002263{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002264 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2265 if (jbd2_debugfs_dir)
2266 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2267 jbd2_debugfs_dir,
2268 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002269}
2270
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002271static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002272{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002273 debugfs_remove(jbd2_debug);
2274 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002275}
2276
2277#else
2278
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002279static void __init jbd2_create_debugfs_entry(void)
2280{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002281}
2282
2283static void __exit jbd2_remove_debugfs_entry(void)
2284{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002285}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002286
2287#endif
2288
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002289#ifdef CONFIG_PROC_FS
2290
2291#define JBD2_STATS_PROC_NAME "fs/jbd2"
2292
2293static void __init jbd2_create_jbd_stats_proc_entry(void)
2294{
2295 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2296}
2297
2298static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2299{
2300 if (proc_jbd2_stats)
2301 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2302}
2303
2304#else
2305
2306#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2307#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2308
2309#endif
2310
Christoph Lametere18b8902006-12-06 20:33:20 -08002311struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002312
2313static int __init journal_init_handle_cache(void)
2314{
Johann Lombardia920e942006-10-11 01:21:00 -07002315 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002316 sizeof(handle_t),
2317 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002318 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002319 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002320 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002321 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2322 return -ENOMEM;
2323 }
2324 return 0;
2325}
2326
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002327static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002328{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002329 if (jbd2_handle_cache)
2330 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002331}
2332
2333/*
2334 * Module startup and shutdown
2335 */
2336
2337static int __init journal_init_caches(void)
2338{
2339 int ret;
2340
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002341 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002342 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002343 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002344 if (ret == 0)
2345 ret = journal_init_handle_cache();
2346 return ret;
2347}
2348
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002349static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002350{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002351 jbd2_journal_destroy_revoke_caches();
2352 jbd2_journal_destroy_jbd2_journal_head_cache();
2353 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002354}
2355
2356static int __init journal_init(void)
2357{
2358 int ret;
2359
2360 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2361
2362 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002363 if (ret == 0) {
2364 jbd2_create_debugfs_entry();
2365 jbd2_create_jbd_stats_proc_entry();
2366 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002367 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002368 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002369 return ret;
2370}
2371
2372static void __exit journal_exit(void)
2373{
Jose R. Santose23291b2007-07-18 08:57:06 -04002374#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002375 int n = atomic_read(&nr_journal_heads);
2376 if (n)
2377 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2378#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002379 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002380 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002381 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002382}
2383
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002384/*
2385 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2386 * tracing infrastructure to map a dev_t to a device name.
2387 *
2388 * The caller should use rcu_read_lock() in order to make sure the
2389 * device name stays valid until its done with it. We use
2390 * rcu_read_lock() as well to make sure we're safe in case the caller
2391 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2392 * nested.
2393 */
2394struct devname_cache {
2395 struct rcu_head rcu;
2396 dev_t device;
2397 char devname[BDEVNAME_SIZE];
2398};
2399#define CACHE_SIZE_BITS 6
2400static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2401static DEFINE_SPINLOCK(devname_cache_lock);
2402
2403static void free_devcache(struct rcu_head *rcu)
2404{
2405 kfree(rcu);
2406}
2407
2408const char *jbd2_dev_to_name(dev_t device)
2409{
2410 int i = hash_32(device, CACHE_SIZE_BITS);
2411 char *ret;
2412 struct block_device *bd;
2413
2414 rcu_read_lock();
2415 if (devcache[i] && devcache[i]->device == device) {
2416 ret = devcache[i]->devname;
2417 rcu_read_unlock();
2418 return ret;
2419 }
2420 rcu_read_unlock();
2421
2422 spin_lock(&devname_cache_lock);
2423 if (devcache[i]) {
2424 if (devcache[i]->device == device) {
2425 ret = devcache[i]->devname;
2426 spin_unlock(&devname_cache_lock);
2427 return ret;
2428 }
2429 call_rcu(&devcache[i]->rcu, free_devcache);
2430 }
2431 devcache[i] = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2432 if (!devcache[i]) {
2433 spin_unlock(&devname_cache_lock);
2434 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
2435 }
2436 devcache[i]->device = device;
2437 bd = bdget(device);
2438 if (bd) {
2439 bdevname(bd, devcache[i]->devname);
2440 bdput(bd);
2441 } else
2442 __bdevname(device, devcache[i]->devname);
2443 ret = devcache[i]->devname;
2444 spin_unlock(&devname_cache_lock);
2445 return ret;
2446}
2447EXPORT_SYMBOL(jbd2_dev_to_name);
2448
Dave Kleikamp470decc2006-10-11 01:20:57 -07002449MODULE_LICENSE("GPL");
2450module_init(journal_init);
2451module_exit(journal_exit);
2452