blob: 3f473faa4660cf31ca86acd1f89c806b95fe9e72 [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
Dave Kleikamp470decc2006-10-11 01:20:57 -0700139 /*
140 * And now, wait forever for commit wakeup events.
141 */
142 spin_lock(&journal->j_state_lock);
143
144loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700145 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146 goto end_loop;
147
148 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
149 journal->j_commit_sequence, journal->j_commit_request);
150
151 if (journal->j_commit_sequence != journal->j_commit_request) {
152 jbd_debug(1, "OK, requests differ\n");
153 spin_unlock(&journal->j_state_lock);
154 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700155 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700156 spin_lock(&journal->j_state_lock);
157 goto loop;
158 }
159
160 wake_up(&journal->j_wait_done_commit);
161 if (freezing(current)) {
162 /*
163 * The simpler the better. Flushing journal isn't a
164 * good idea, because that depends on threads that may
165 * be already stopped.
166 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700167 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700168 spin_unlock(&journal->j_state_lock);
169 refrigerator();
170 spin_lock(&journal->j_state_lock);
171 } else {
172 /*
173 * We assume on resume that commits are already there,
174 * so we don't sleep
175 */
176 DEFINE_WAIT(wait);
177 int should_sleep = 1;
178
179 prepare_to_wait(&journal->j_wait_commit, &wait,
180 TASK_INTERRUPTIBLE);
181 if (journal->j_commit_sequence != journal->j_commit_request)
182 should_sleep = 0;
183 transaction = journal->j_running_transaction;
184 if (transaction && time_after_eq(jiffies,
185 transaction->t_expires))
186 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700187 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700188 should_sleep = 0;
189 if (should_sleep) {
190 spin_unlock(&journal->j_state_lock);
191 schedule();
192 spin_lock(&journal->j_state_lock);
193 }
194 finish_wait(&journal->j_wait_commit, &wait);
195 }
196
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700197 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198
199 /*
200 * Were we woken up by a commit wakeup event?
201 */
202 transaction = journal->j_running_transaction;
203 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
204 journal->j_commit_request = transaction->t_tid;
205 jbd_debug(1, "woke because of timeout\n");
206 }
207 goto loop;
208
209end_loop:
210 spin_unlock(&journal->j_state_lock);
211 del_timer_sync(&journal->j_commit_timer);
212 journal->j_task = NULL;
213 wake_up(&journal->j_wait_done_commit);
214 jbd_debug(1, "Journal thread exiting.\n");
215 return 0;
216}
217
Pavel Emelianov97f06782007-05-08 00:30:42 -0700218static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700219{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700220 struct task_struct *t;
221
Theodore Ts'o90576c02009-09-29 15:51:30 -0400222 t = kthread_run(kjournald2, journal, "jbd2/%s",
223 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700224 if (IS_ERR(t))
225 return PTR_ERR(t);
226
Al Viro1076d172008-03-29 03:07:18 +0000227 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700228 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700229}
230
231static void journal_kill_thread(journal_t *journal)
232{
233 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700234 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700235
236 while (journal->j_task) {
237 wake_up(&journal->j_wait_commit);
238 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000239 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700240 spin_lock(&journal->j_state_lock);
241 }
242 spin_unlock(&journal->j_state_lock);
243}
244
245/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700246 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700247 *
248 * Writes a metadata buffer to a given disk block. The actual IO is not
249 * performed but a new buffer_head is constructed which labels the data
250 * to be written with the correct destination disk block.
251 *
252 * Any magic-number escaping which needs to be done will cause a
253 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700254 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700255 * magic number is only written to the log for descripter blocks. In
256 * this case, we copy the data and replace the first word with 0, and we
257 * return a result code which indicates that this buffer needs to be
258 * marked as an escaped buffer in the corresponding log descriptor
259 * block. The missing word can then be restored when the block is read
260 * during recovery.
261 *
262 * If the source buffer has already been modified by a new transaction
263 * since we took the last commit snapshot, we use the frozen copy of
264 * that data for IO. If we end up using the existing buffer_head's data
265 * for the write, then we *have* to lock the buffer to prevent anyone
266 * else from using and possibly modifying it while the IO is in
267 * progress.
268 *
269 * The function returns a pointer to the buffer_heads to be used for IO.
270 *
271 * We assume that the journal has already been locked in this function.
272 *
273 * Return value:
274 * <0: Error
275 * >=0: Finished OK
276 *
277 * On success:
278 * Bit 0 set == escape performed on the data
279 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
280 */
281
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700282int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700283 struct journal_head *jh_in,
284 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700285 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286{
287 int need_copy_out = 0;
288 int done_copy_out = 0;
289 int do_escape = 0;
290 char *mapped_data;
291 struct buffer_head *new_bh;
292 struct journal_head *new_jh;
293 struct page *new_page;
294 unsigned int new_offset;
295 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700296 struct jbd2_buffer_trigger_type *triggers;
dingdinghua96577c42009-07-13 17:55:35 -0400297 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700298
299 /*
300 * The buffer really shouldn't be locked: only the current committing
301 * transaction is allowed to write it, so nobody else is allowed
302 * to do any IO.
303 *
304 * akpm: except if we're journalling data, and write() output is
305 * also part of a shared mapping, and another thread has
306 * decided to launch a writepage() against this buffer.
307 */
308 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
309
310 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghua96577c42009-07-13 17:55:35 -0400311 /* keep subsequent assertions sane */
312 new_bh->b_state = 0;
313 init_buffer(new_bh, NULL, NULL);
314 atomic_set(&new_bh->b_count, 1);
315 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700316
317 /*
318 * If a new transaction has already done a buffer copy-out, then
319 * we use that version of the data for the commit.
320 */
321 jbd_lock_bh_state(bh_in);
322repeat:
323 if (jh_in->b_frozen_data) {
324 done_copy_out = 1;
325 new_page = virt_to_page(jh_in->b_frozen_data);
326 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700327 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700328 } else {
329 new_page = jh2bh(jh_in)->b_page;
330 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700331 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700332 }
333
334 mapped_data = kmap_atomic(new_page, KM_USER0);
335 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700336 * Fire any commit trigger. Do this before checking for escaping,
337 * as the trigger may modify the magic offset. If a copy-out
338 * happens afterwards, it will have the correct data in the buffer.
339 */
340 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
341 triggers);
342
343 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700344 * Check for escaping
345 */
346 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700347 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700348 need_copy_out = 1;
349 do_escape = 1;
350 }
351 kunmap_atomic(mapped_data, KM_USER0);
352
353 /*
354 * Do we need to do a data copy?
355 */
356 if (need_copy_out && !done_copy_out) {
357 char *tmp;
358
359 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400360 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500361 if (!tmp) {
362 jbd2_journal_put_journal_head(new_jh);
363 return -ENOMEM;
364 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700365 jbd_lock_bh_state(bh_in);
366 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400367 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700368 goto repeat;
369 }
370
371 jh_in->b_frozen_data = tmp;
372 mapped_data = kmap_atomic(new_page, KM_USER0);
373 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
374 kunmap_atomic(mapped_data, KM_USER0);
375
376 new_page = virt_to_page(tmp);
377 new_offset = offset_in_page(tmp);
378 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700379
380 /*
381 * This isn't strictly necessary, as we're using frozen
382 * data for the escaping, but it keeps consistency with
383 * b_frozen_data usage.
384 */
385 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700386 }
387
388 /*
389 * Did we need to do an escaping? Now we've done all the
390 * copying, we can finally do so.
391 */
392 if (do_escape) {
393 mapped_data = kmap_atomic(new_page, KM_USER0);
394 *((unsigned int *)(mapped_data + new_offset)) = 0;
395 kunmap_atomic(mapped_data, KM_USER0);
396 }
397
Dave Kleikamp470decc2006-10-11 01:20:57 -0700398 set_bh_page(new_bh, new_page, new_offset);
399 new_jh->b_transaction = NULL;
400 new_bh->b_size = jh2bh(jh_in)->b_size;
401 new_bh->b_bdev = transaction->t_journal->j_dev;
402 new_bh->b_blocknr = blocknr;
403 set_buffer_mapped(new_bh);
404 set_buffer_dirty(new_bh);
405
406 *jh_out = new_jh;
407
408 /*
409 * The to-be-written buffer needs to get moved to the io queue,
410 * and the original buffer whose contents we are shadowing or
411 * copying is moved to the transaction's shadow queue.
412 */
413 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400414 spin_lock(&journal->j_list_lock);
415 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
416 spin_unlock(&journal->j_list_lock);
417 jbd_unlock_bh_state(bh_in);
418
Dave Kleikamp470decc2006-10-11 01:20:57 -0700419 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700420 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700421
422 return do_escape | (done_copy_out << 1);
423}
424
425/*
426 * Allocation code for the journal file. Manage the space left in the
427 * journal, so that we can begin checkpointing when appropriate.
428 */
429
430/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700431 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700432 *
433 * Called with the journal already locked.
434 *
435 * Called under j_state_lock
436 */
437
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700438int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700439{
440 int left = journal->j_free;
441
442 assert_spin_locked(&journal->j_state_lock);
443
444 /*
445 * Be pessimistic here about the number of those free blocks which
446 * might be required for log descriptor control blocks.
447 */
448
449#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
450
451 left -= MIN_LOG_RESERVED_BLOCKS;
452
453 if (left <= 0)
454 return 0;
455 left -= (left >> 3);
456 return left;
457}
458
459/*
Jan Karac88ccea2009-02-10 11:27:46 -0500460 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700461 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700462int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700463{
464 /*
465 * Are we already doing a recent enough commit?
466 */
467 if (!tid_geq(journal->j_commit_request, target)) {
468 /*
469 * We want a new commit: OK, mark the request and wakup the
470 * commit thread. We do _not_ do the commit ourselves.
471 */
472
473 journal->j_commit_request = target;
474 jbd_debug(1, "JBD: requesting commit %d/%d\n",
475 journal->j_commit_request,
476 journal->j_commit_sequence);
477 wake_up(&journal->j_wait_commit);
478 return 1;
479 }
480 return 0;
481}
482
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700483int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700484{
485 int ret;
486
487 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700488 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700489 spin_unlock(&journal->j_state_lock);
490 return ret;
491}
492
493/*
494 * Force and wait upon a commit if the calling process is not within
495 * transaction. This is used for forcing out undo-protected data which contains
496 * bitmaps, when the fs is running out of space.
497 *
498 * We can only force the running transaction if we don't have an active handle;
499 * otherwise, we will deadlock.
500 *
501 * Returns true if a transaction was started.
502 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504{
505 transaction_t *transaction = NULL;
506 tid_t tid;
507
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction && !current->journal_info) {
510 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700511 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512 } else if (journal->j_committing_transaction)
513 transaction = journal->j_committing_transaction;
514
515 if (!transaction) {
516 spin_unlock(&journal->j_state_lock);
517 return 0; /* Nothing to retry */
518 }
519
520 tid = transaction->t_tid;
521 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700522 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523 return 1;
524}
525
526/*
527 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500528 * if a transaction is going to be committed (or is currently already
529 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700530 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700531int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532{
533 int ret = 0;
534
535 spin_lock(&journal->j_state_lock);
536 if (journal->j_running_transaction) {
537 tid_t tid = journal->j_running_transaction->t_tid;
538
Jan Karac88ccea2009-02-10 11:27:46 -0500539 __jbd2_log_start_commit(journal, tid);
540 /* There's a running transaction and we've just made sure
541 * it's commit has been scheduled. */
542 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700543 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500544 ret = 1;
545 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700546 /*
547 * If ext3_write_super() recently started a commit, then we
548 * have to wait for completion of that transaction
549 */
Jan Karac88ccea2009-02-10 11:27:46 -0500550 if (ptid)
551 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700552 ret = 1;
553 }
554 spin_unlock(&journal->j_state_lock);
555 return ret;
556}
557
558/*
559 * Wait for a specified commit to complete.
560 * The caller may not hold the journal lock.
561 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700562int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700563{
564 int err = 0;
565
Jose R. Santose23291b2007-07-18 08:57:06 -0400566#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700567 spin_lock(&journal->j_state_lock);
568 if (!tid_geq(journal->j_commit_request, tid)) {
569 printk(KERN_EMERG
570 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400571 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700572 }
573 spin_unlock(&journal->j_state_lock);
574#endif
575 spin_lock(&journal->j_state_lock);
576 while (tid_gt(tid, journal->j_commit_sequence)) {
577 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
578 tid, journal->j_commit_sequence);
579 wake_up(&journal->j_wait_commit);
580 spin_unlock(&journal->j_state_lock);
581 wait_event(journal->j_wait_done_commit,
582 !tid_gt(tid, journal->j_commit_sequence));
583 spin_lock(&journal->j_state_lock);
584 }
585 spin_unlock(&journal->j_state_lock);
586
587 if (unlikely(is_journal_aborted(journal))) {
588 printk(KERN_EMERG "journal commit I/O error\n");
589 err = -EIO;
590 }
591 return err;
592}
593
594/*
595 * Log buffer allocation routines:
596 */
597
Mingming Cao18eba7a2006-10-11 01:21:13 -0700598int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700599{
600 unsigned long blocknr;
601
602 spin_lock(&journal->j_state_lock);
603 J_ASSERT(journal->j_free > 1);
604
605 blocknr = journal->j_head;
606 journal->j_head++;
607 journal->j_free--;
608 if (journal->j_head == journal->j_last)
609 journal->j_head = journal->j_first;
610 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700611 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700612}
613
614/*
615 * Conversion of logical to physical block numbers for the journal
616 *
617 * On external journals the journal blocks are identity-mapped, so
618 * this is a no-op. If needed, we can use j_blk_offset - everything is
619 * ready.
620 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700621int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700622 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623{
624 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700625 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700626
627 if (journal->j_inode) {
628 ret = bmap(journal->j_inode, blocknr);
629 if (ret)
630 *retp = ret;
631 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700632 printk(KERN_ALERT "%s: journal block not found "
633 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400634 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700635 err = -EIO;
636 __journal_abort_soft(journal, err);
637 }
638 } else {
639 *retp = blocknr; /* +journal->j_blk_offset */
640 }
641 return err;
642}
643
644/*
645 * We play buffer_head aliasing tricks to write data/metadata blocks to
646 * the journal without copying their contents, but for journal
647 * descriptor blocks we do need to generate bona fide buffers.
648 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700649 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700650 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
651 * But we don't bother doing that, so there will be coherency problems with
652 * mmaps of blockdevs which hold live JBD-controlled filesystems.
653 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700654struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700655{
656 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700657 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700658 int err;
659
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700660 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700661
662 if (err)
663 return NULL;
664
665 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500666 if (!bh)
667 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700668 lock_buffer(bh);
669 memset(bh->b_data, 0, journal->j_blocksize);
670 set_buffer_uptodate(bh);
671 unlock_buffer(bh);
672 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700673 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700674}
675
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500676struct jbd2_stats_proc_session {
677 journal_t *journal;
678 struct transaction_stats_s *stats;
679 int start;
680 int max;
681};
682
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500683static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
684{
685 return *pos ? NULL : SEQ_START_TOKEN;
686}
687
688static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
689{
690 return NULL;
691}
692
693static int jbd2_seq_info_show(struct seq_file *seq, void *v)
694{
695 struct jbd2_stats_proc_session *s = seq->private;
696
697 if (v != SEQ_START_TOKEN)
698 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400699 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500700 s->stats->ts_tid,
701 s->journal->j_max_transaction_buffers);
702 if (s->stats->ts_tid == 0)
703 return 0;
704 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400705 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500706 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400707 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500708 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400709 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500710 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400711 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500712 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400713 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500714 seq_printf(seq, " %lluus average transaction commit time\n",
715 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500716 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400717 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500718 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400719 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500720 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400721 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500722 return 0;
723}
724
725static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
726{
727}
728
James Morris88e9d342009-09-22 16:43:43 -0700729static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500730 .start = jbd2_seq_info_start,
731 .next = jbd2_seq_info_next,
732 .stop = jbd2_seq_info_stop,
733 .show = jbd2_seq_info_show,
734};
735
736static int jbd2_seq_info_open(struct inode *inode, struct file *file)
737{
738 journal_t *journal = PDE(inode)->data;
739 struct jbd2_stats_proc_session *s;
740 int rc, size;
741
742 s = kmalloc(sizeof(*s), GFP_KERNEL);
743 if (s == NULL)
744 return -ENOMEM;
745 size = sizeof(struct transaction_stats_s);
746 s->stats = kmalloc(size, GFP_KERNEL);
747 if (s->stats == NULL) {
748 kfree(s);
749 return -ENOMEM;
750 }
751 spin_lock(&journal->j_history_lock);
752 memcpy(s->stats, &journal->j_stats, size);
753 s->journal = journal;
754 spin_unlock(&journal->j_history_lock);
755
756 rc = seq_open(file, &jbd2_seq_info_ops);
757 if (rc == 0) {
758 struct seq_file *m = file->private_data;
759 m->private = s;
760 } else {
761 kfree(s->stats);
762 kfree(s);
763 }
764 return rc;
765
766}
767
768static int jbd2_seq_info_release(struct inode *inode, struct file *file)
769{
770 struct seq_file *seq = file->private_data;
771 struct jbd2_stats_proc_session *s = seq->private;
772 kfree(s->stats);
773 kfree(s);
774 return seq_release(inode, file);
775}
776
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700777static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500778 .owner = THIS_MODULE,
779 .open = jbd2_seq_info_open,
780 .read = seq_read,
781 .llseek = seq_lseek,
782 .release = jbd2_seq_info_release,
783};
784
785static struct proc_dir_entry *proc_jbd2_stats;
786
787static void jbd2_stats_proc_init(journal_t *journal)
788{
Theodore Ts'o05496762008-09-16 14:36:17 -0400789 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500790 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700791 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
792 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500793 }
794}
795
796static void jbd2_stats_proc_exit(journal_t *journal)
797{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500798 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400799 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500800}
801
Dave Kleikamp470decc2006-10-11 01:20:57 -0700802/*
803 * Management for journal control blocks: functions to create and
804 * destroy journal_t structures, and to initialise and read existing
805 * journal blocks from disk. */
806
807/* First: create and setup a journal_t object in memory. We initialise
808 * very few fields yet: that has to wait until we have created the
809 * journal structures from from scratch, or loaded them from disk. */
810
811static journal_t * journal_init_common (void)
812{
813 journal_t *journal;
814 int err;
815
Mingming Caod802ffa2007-10-16 18:38:25 -0400816 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700817 if (!journal)
818 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700819
820 init_waitqueue_head(&journal->j_wait_transaction_locked);
821 init_waitqueue_head(&journal->j_wait_logspace);
822 init_waitqueue_head(&journal->j_wait_done_commit);
823 init_waitqueue_head(&journal->j_wait_checkpoint);
824 init_waitqueue_head(&journal->j_wait_commit);
825 init_waitqueue_head(&journal->j_wait_updates);
826 mutex_init(&journal->j_barrier);
827 mutex_init(&journal->j_checkpoint_mutex);
828 spin_lock_init(&journal->j_revoke_lock);
829 spin_lock_init(&journal->j_list_lock);
830 spin_lock_init(&journal->j_state_lock);
831
Mingming Caocd02ff02007-10-16 18:38:25 -0400832 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500833 journal->j_min_batch_time = 0;
834 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700835
836 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700837 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700838
839 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700840 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700841 if (err) {
842 kfree(journal);
843 goto fail;
844 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500845
Theodore Ts'obf699322009-09-30 00:32:06 -0400846 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500847
Dave Kleikamp470decc2006-10-11 01:20:57 -0700848 return journal;
849fail:
850 return NULL;
851}
852
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700853/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700854 *
855 * Create a journal structure assigned some fixed set of disk blocks to
856 * the journal. We don't actually touch those disk blocks yet, but we
857 * need to set up all of the mapping information to tell the journaling
858 * system where the journal blocks are.
859 *
860 */
861
862/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400863 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700864 * @bdev: Block device on which to create the journal
865 * @fs_dev: Device which hold journalled filesystem for this journal.
866 * @start: Block nr Start of journal.
867 * @len: Length of the journal in blocks.
868 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400869 *
870 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700871 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700872 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700873 * range of blocks on an arbitrary block device.
874 *
875 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700876journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700877 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700878 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700879{
880 journal_t *journal = journal_init_common();
881 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400882 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700883 int n;
884
885 if (!journal)
886 return NULL;
887
888 /* journal descriptor can store up to n blocks -bzzz */
889 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -0500890 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700891 n = journal->j_blocksize / sizeof(journal_block_tag_t);
892 journal->j_wbufsize = n;
893 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
894 if (!journal->j_wbuf) {
895 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400896 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500897 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700898 }
899 journal->j_dev = bdev;
900 journal->j_fs_dev = fs_dev;
901 journal->j_blk_offset = start;
902 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -0400903 bdevname(journal->j_dev, journal->j_devname);
904 p = journal->j_devname;
905 while ((p = strchr(p, '/')))
906 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -0700907
908 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500909 if (!bh) {
910 printk(KERN_ERR
911 "%s: Cannot get buffer for journal superblock\n",
912 __func__);
913 goto out_err;
914 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700915 journal->j_sb_buffer = bh;
916 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500917
Dave Kleikamp470decc2006-10-11 01:20:57 -0700918 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500919out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800920 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500921 jbd2_stats_proc_exit(journal);
922 kfree(journal);
923 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700924}
925
926/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700927 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700928 * @inode: An inode to create the journal in
929 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700930 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700931 * the journal. The inode must exist already, must support bmap() and
932 * must have all data blocks preallocated.
933 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700934journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700935{
936 struct buffer_head *bh;
937 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400938 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700939 int err;
940 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700941 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700942
943 if (!journal)
944 return NULL;
945
946 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
947 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400948 bdevname(journal->j_dev, journal->j_devname);
949 p = journal->j_devname;
950 while ((p = strchr(p, '/')))
951 *p = '!';
952 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400953 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700954 jbd_debug(1,
955 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
956 journal, inode->i_sb->s_id, inode->i_ino,
957 (long long) inode->i_size,
958 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
959
960 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
961 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500962 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700963
964 /* journal descriptor can store up to n blocks -bzzz */
965 n = journal->j_blocksize / sizeof(journal_block_tag_t);
966 journal->j_wbufsize = n;
967 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
968 if (!journal->j_wbuf) {
969 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400970 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500971 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700972 }
973
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700974 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975 /* If that failed, give up */
976 if (err) {
977 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400978 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500979 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700980 }
981
982 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500983 if (!bh) {
984 printk(KERN_ERR
985 "%s: Cannot get buffer for journal superblock\n",
986 __func__);
987 goto out_err;
988 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700989 journal->j_sb_buffer = bh;
990 journal->j_superblock = (journal_superblock_t *)bh->b_data;
991
992 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500993out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800994 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500995 jbd2_stats_proc_exit(journal);
996 kfree(journal);
997 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700998}
999
1000/*
1001 * If the journal init or create aborts, we need to mark the journal
1002 * superblock as being NULL to prevent the journal destroy from writing
1003 * back a bogus superblock.
1004 */
1005static void journal_fail_superblock (journal_t *journal)
1006{
1007 struct buffer_head *bh = journal->j_sb_buffer;
1008 brelse(bh);
1009 journal->j_sb_buffer = NULL;
1010}
1011
1012/*
1013 * Given a journal_t structure, initialise the various fields for
1014 * startup of a new journaling session. We use this both when creating
1015 * a journal, and after recovering an old journal to reset it for
1016 * subsequent use.
1017 */
1018
1019static int journal_reset(journal_t *journal)
1020{
1021 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001022 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001023
1024 first = be32_to_cpu(sb->s_first);
1025 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001026 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1027 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1028 first, last);
1029 journal_fail_superblock(journal);
1030 return -EINVAL;
1031 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001032
1033 journal->j_first = first;
1034 journal->j_last = last;
1035
1036 journal->j_head = first;
1037 journal->j_tail = first;
1038 journal->j_free = last - first;
1039
1040 journal->j_tail_sequence = journal->j_transaction_sequence;
1041 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1042 journal->j_commit_request = journal->j_commit_sequence;
1043
1044 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1045
1046 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001047 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001048 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049}
1050
1051/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001052 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001053 * @journal: The journal to update.
1054 * @wait: Set to '0' if you don't want to wait for IO completion.
1055 *
1056 * Update a journal's dynamic superblock fields and write it to disk,
1057 * optionally waiting for the IO to complete.
1058 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001059void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001060{
1061 journal_superblock_t *sb = journal->j_superblock;
1062 struct buffer_head *bh = journal->j_sb_buffer;
1063
1064 /*
1065 * As a special case, if the on-disk copy is already marked as needing
1066 * no recovery (s_start == 0) and there are no outstanding transactions
1067 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001068 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001069 * attempting a write to a potential-readonly device.
1070 */
1071 if (sb->s_start == 0 && journal->j_tail_sequence ==
1072 journal->j_transaction_sequence) {
1073 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1074 "(start %ld, seq %d, errno %d)\n",
1075 journal->j_tail, journal->j_tail_sequence,
1076 journal->j_errno);
1077 goto out;
1078 }
1079
Theodore Ts'o914258b2008-10-06 21:35:40 -04001080 if (buffer_write_io_error(bh)) {
1081 /*
1082 * Oh, dear. A previous attempt to write the journal
1083 * superblock failed. This could happen because the
1084 * USB device was yanked out. Or it could happen to
1085 * be a transient write error and maybe the block will
1086 * be remapped. Nothing we can do but to retry the
1087 * write and hope for the best.
1088 */
1089 printk(KERN_ERR "JBD2: previous I/O error detected "
1090 "for journal superblock update for %s.\n",
1091 journal->j_devname);
1092 clear_buffer_write_io_error(bh);
1093 set_buffer_uptodate(bh);
1094 }
1095
Dave Kleikamp470decc2006-10-11 01:20:57 -07001096 spin_lock(&journal->j_state_lock);
1097 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1098 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1099
1100 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1101 sb->s_start = cpu_to_be32(journal->j_tail);
1102 sb->s_errno = cpu_to_be32(journal->j_errno);
1103 spin_unlock(&journal->j_state_lock);
1104
1105 BUFFER_TRACE(bh, "marking dirty");
1106 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001107 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001108 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001109 if (buffer_write_io_error(bh)) {
1110 printk(KERN_ERR "JBD2: I/O error detected "
1111 "when updating journal superblock for %s.\n",
1112 journal->j_devname);
1113 clear_buffer_write_io_error(bh);
1114 set_buffer_uptodate(bh);
1115 }
1116 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117 ll_rw_block(SWRITE, 1, &bh);
1118
1119out:
1120 /* If we have just flushed the log (by marking s_start==0), then
1121 * any future commit will have to be careful to update the
1122 * superblock again to re-record the true start of the log. */
1123
1124 spin_lock(&journal->j_state_lock);
1125 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001126 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001127 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001128 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001129 spin_unlock(&journal->j_state_lock);
1130}
1131
1132/*
1133 * Read the superblock for a given journal, performing initial
1134 * validation of the format.
1135 */
1136
1137static int journal_get_superblock(journal_t *journal)
1138{
1139 struct buffer_head *bh;
1140 journal_superblock_t *sb;
1141 int err = -EIO;
1142
1143 bh = journal->j_sb_buffer;
1144
1145 J_ASSERT(bh != NULL);
1146 if (!buffer_uptodate(bh)) {
1147 ll_rw_block(READ, 1, &bh);
1148 wait_on_buffer(bh);
1149 if (!buffer_uptodate(bh)) {
1150 printk (KERN_ERR
1151 "JBD: IO error reading journal superblock\n");
1152 goto out;
1153 }
1154 }
1155
1156 sb = journal->j_superblock;
1157
1158 err = -EINVAL;
1159
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001160 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001161 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1162 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1163 goto out;
1164 }
1165
1166 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001167 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001168 journal->j_format_version = 1;
1169 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001170 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001171 journal->j_format_version = 2;
1172 break;
1173 default:
1174 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1175 goto out;
1176 }
1177
1178 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1179 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1180 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1181 printk (KERN_WARNING "JBD: journal file too short\n");
1182 goto out;
1183 }
1184
1185 return 0;
1186
1187out:
1188 journal_fail_superblock(journal);
1189 return err;
1190}
1191
1192/*
1193 * Load the on-disk journal superblock and read the key fields into the
1194 * journal_t.
1195 */
1196
1197static int load_superblock(journal_t *journal)
1198{
1199 int err;
1200 journal_superblock_t *sb;
1201
1202 err = journal_get_superblock(journal);
1203 if (err)
1204 return err;
1205
1206 sb = journal->j_superblock;
1207
1208 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1209 journal->j_tail = be32_to_cpu(sb->s_start);
1210 journal->j_first = be32_to_cpu(sb->s_first);
1211 journal->j_last = be32_to_cpu(sb->s_maxlen);
1212 journal->j_errno = be32_to_cpu(sb->s_errno);
1213
1214 return 0;
1215}
1216
1217
1218/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001219 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001220 * @journal: Journal to act on.
1221 *
1222 * Given a journal_t structure which tells us which disk blocks contain
1223 * a journal, read the journal from disk to initialise the in-memory
1224 * structures.
1225 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001226int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001227{
1228 int err;
1229 journal_superblock_t *sb;
1230
1231 err = load_superblock(journal);
1232 if (err)
1233 return err;
1234
1235 sb = journal->j_superblock;
1236 /* If this is a V2 superblock, then we have to check the
1237 * features flags on it. */
1238
1239 if (journal->j_format_version >= 2) {
1240 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001241 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001242 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001243 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001244 printk (KERN_WARNING
1245 "JBD: Unrecognised features on journal\n");
1246 return -EINVAL;
1247 }
1248 }
1249
Dave Kleikamp470decc2006-10-11 01:20:57 -07001250 /* Let the recovery code check whether it needs to recover any
1251 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001252 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001253 goto recovery_error;
1254
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001255 if (journal->j_failed_commit) {
1256 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1257 "is corrupt.\n", journal->j_failed_commit,
1258 journal->j_devname);
1259 return -EIO;
1260 }
1261
Dave Kleikamp470decc2006-10-11 01:20:57 -07001262 /* OK, we've finished with the dynamic journal bits:
1263 * reinitialise the dynamic contents of the superblock in memory
1264 * and reset them on disk. */
1265 if (journal_reset(journal))
1266 goto recovery_error;
1267
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001268 journal->j_flags &= ~JBD2_ABORT;
1269 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001270 return 0;
1271
1272recovery_error:
1273 printk (KERN_WARNING "JBD: recovery failed\n");
1274 return -EIO;
1275}
1276
1277/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001278 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001279 * @journal: Journal to act on.
1280 *
1281 * Release a journal_t structure once it is no longer in use by the
1282 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001283 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001284 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001285int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001286{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001287 int err = 0;
1288
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289 /* Wait for the commit thread to wake up and die. */
1290 journal_kill_thread(journal);
1291
1292 /* Force a final log commit */
1293 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001294 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001295
1296 /* Force any old transactions to disk */
1297
1298 /* Totally anal locking here... */
1299 spin_lock(&journal->j_list_lock);
1300 while (journal->j_checkpoint_transactions != NULL) {
1301 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001302 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001303 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001304 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001305 spin_lock(&journal->j_list_lock);
1306 }
1307
1308 J_ASSERT(journal->j_running_transaction == NULL);
1309 J_ASSERT(journal->j_committing_transaction == NULL);
1310 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1311 spin_unlock(&journal->j_list_lock);
1312
Dave Kleikamp470decc2006-10-11 01:20:57 -07001313 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001314 if (!is_journal_aborted(journal)) {
1315 /* We can now mark the journal as empty. */
1316 journal->j_tail = 0;
1317 journal->j_tail_sequence =
1318 ++journal->j_transaction_sequence;
1319 jbd2_journal_update_superblock(journal, 1);
1320 } else {
1321 err = -EIO;
1322 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001323 brelse(journal->j_sb_buffer);
1324 }
1325
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001326 if (journal->j_proc_entry)
1327 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001328 if (journal->j_inode)
1329 iput(journal->j_inode);
1330 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001331 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001332 kfree(journal->j_wbuf);
1333 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001334
1335 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001336}
1337
1338
1339/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001340 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001341 * @journal: Journal to check.
1342 * @compat: bitmask of compatible features
1343 * @ro: bitmask of features that force read-only mount
1344 * @incompat: bitmask of incompatible features
1345 *
1346 * Check whether the journal uses all of a given set of
1347 * features. Return true (non-zero) if it does.
1348 **/
1349
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001350int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001351 unsigned long ro, unsigned long incompat)
1352{
1353 journal_superblock_t *sb;
1354
1355 if (!compat && !ro && !incompat)
1356 return 1;
1357 if (journal->j_format_version == 1)
1358 return 0;
1359
1360 sb = journal->j_superblock;
1361
1362 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1363 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1364 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1365 return 1;
1366
1367 return 0;
1368}
1369
1370/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001371 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001372 * @journal: Journal to check.
1373 * @compat: bitmask of compatible features
1374 * @ro: bitmask of features that force read-only mount
1375 * @incompat: bitmask of incompatible features
1376 *
1377 * Check whether the journaling code supports the use of
1378 * all of a given set of features on this journal. Return true
1379 * (non-zero) if it can. */
1380
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001381int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001382 unsigned long ro, unsigned long incompat)
1383{
1384 journal_superblock_t *sb;
1385
1386 if (!compat && !ro && !incompat)
1387 return 1;
1388
1389 sb = journal->j_superblock;
1390
1391 /* We can support any known requested features iff the
1392 * superblock is in version 2. Otherwise we fail to support any
1393 * extended sb features. */
1394
1395 if (journal->j_format_version != 2)
1396 return 0;
1397
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001398 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1399 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1400 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001401 return 1;
1402
1403 return 0;
1404}
1405
1406/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001407 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 * @journal: Journal to act on.
1409 * @compat: bitmask of compatible features
1410 * @ro: bitmask of features that force read-only mount
1411 * @incompat: bitmask of incompatible features
1412 *
1413 * Mark a given journal feature as present on the
1414 * superblock. Returns true if the requested features could be set.
1415 *
1416 */
1417
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001418int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001419 unsigned long ro, unsigned long incompat)
1420{
1421 journal_superblock_t *sb;
1422
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001423 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001424 return 1;
1425
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001426 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 return 0;
1428
1429 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1430 compat, ro, incompat);
1431
1432 sb = journal->j_superblock;
1433
1434 sb->s_feature_compat |= cpu_to_be32(compat);
1435 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1436 sb->s_feature_incompat |= cpu_to_be32(incompat);
1437
1438 return 1;
1439}
1440
Girish Shilamkar818d2762008-01-28 23:58:27 -05001441/*
1442 * jbd2_journal_clear_features () - Clear a given journal feature in the
1443 * superblock
1444 * @journal: Journal to act on.
1445 * @compat: bitmask of compatible features
1446 * @ro: bitmask of features that force read-only mount
1447 * @incompat: bitmask of incompatible features
1448 *
1449 * Clear a given journal feature as present on the
1450 * superblock.
1451 */
1452void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1453 unsigned long ro, unsigned long incompat)
1454{
1455 journal_superblock_t *sb;
1456
1457 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1458 compat, ro, incompat);
1459
1460 sb = journal->j_superblock;
1461
1462 sb->s_feature_compat &= ~cpu_to_be32(compat);
1463 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1464 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1465}
1466EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001467
1468/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001469 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001470 * @journal: Journal to act on.
1471 *
1472 * Given an initialised but unloaded journal struct, poke about in the
1473 * on-disk structure to update it to the most recent supported version.
1474 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001475int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001476{
1477 journal_superblock_t *sb;
1478 int err;
1479
1480 err = journal_get_superblock(journal);
1481 if (err)
1482 return err;
1483
1484 sb = journal->j_superblock;
1485
1486 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001487 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001488 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001489 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001490 return journal_convert_superblock_v1(journal, sb);
1491 default:
1492 break;
1493 }
1494 return -EINVAL;
1495}
1496
1497static int journal_convert_superblock_v1(journal_t *journal,
1498 journal_superblock_t *sb)
1499{
1500 int offset, blocksize;
1501 struct buffer_head *bh;
1502
1503 printk(KERN_WARNING
1504 "JBD: Converting superblock from version 1 to 2.\n");
1505
1506 /* Pre-initialise new fields to zero */
1507 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1508 blocksize = be32_to_cpu(sb->s_blocksize);
1509 memset(&sb->s_feature_compat, 0, blocksize-offset);
1510
1511 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001512 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001513 journal->j_format_version = 2;
1514
1515 bh = journal->j_sb_buffer;
1516 BUFFER_TRACE(bh, "marking dirty");
1517 mark_buffer_dirty(bh);
1518 sync_dirty_buffer(bh);
1519 return 0;
1520}
1521
1522
1523/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001524 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001525 * @journal: Journal to act on.
1526 *
1527 * Flush all data for a given journal to disk and empty the journal.
1528 * Filesystems can use this when remounting readonly to ensure that
1529 * recovery does not need to happen on remount.
1530 */
1531
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001532int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001533{
1534 int err = 0;
1535 transaction_t *transaction = NULL;
1536 unsigned long old_tail;
1537
1538 spin_lock(&journal->j_state_lock);
1539
1540 /* Force everything buffered to the log... */
1541 if (journal->j_running_transaction) {
1542 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001543 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001544 } else if (journal->j_committing_transaction)
1545 transaction = journal->j_committing_transaction;
1546
1547 /* Wait for the log commit to complete... */
1548 if (transaction) {
1549 tid_t tid = transaction->t_tid;
1550
1551 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001552 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001553 } else {
1554 spin_unlock(&journal->j_state_lock);
1555 }
1556
1557 /* ...and flush everything in the log out to disk. */
1558 spin_lock(&journal->j_list_lock);
1559 while (!err && journal->j_checkpoint_transactions != NULL) {
1560 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001561 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001562 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001563 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 spin_lock(&journal->j_list_lock);
1565 }
1566 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001567
1568 if (is_journal_aborted(journal))
1569 return -EIO;
1570
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001571 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572
1573 /* Finally, mark the journal as really needing no recovery.
1574 * This sets s_start==0 in the underlying superblock, which is
1575 * the magic code for a fully-recovered superblock. Any future
1576 * commits of data to the journal will restore the current
1577 * s_start value. */
1578 spin_lock(&journal->j_state_lock);
1579 old_tail = journal->j_tail;
1580 journal->j_tail = 0;
1581 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001582 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001583 spin_lock(&journal->j_state_lock);
1584 journal->j_tail = old_tail;
1585
1586 J_ASSERT(!journal->j_running_transaction);
1587 J_ASSERT(!journal->j_committing_transaction);
1588 J_ASSERT(!journal->j_checkpoint_transactions);
1589 J_ASSERT(journal->j_head == journal->j_tail);
1590 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1591 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001592 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593}
1594
1595/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001596 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001597 * @journal: Journal to act on.
1598 * @write: flag (see below)
1599 *
1600 * Wipe out all of the contents of a journal, safely. This will produce
1601 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001602 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001603 *
1604 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1605 * we merely suppress recovery.
1606 */
1607
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001608int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609{
1610 journal_superblock_t *sb;
1611 int err = 0;
1612
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001613 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614
1615 err = load_superblock(journal);
1616 if (err)
1617 return err;
1618
1619 sb = journal->j_superblock;
1620
1621 if (!journal->j_tail)
1622 goto no_recovery;
1623
1624 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1625 write ? "Clearing" : "Ignoring");
1626
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001627 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001628 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001629 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630
1631 no_recovery:
1632 return err;
1633}
1634
1635/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001636 * Journal abort has very specific semantics, which we describe
1637 * for journal abort.
1638 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001639 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001640 * itself are here.
1641 */
1642
1643/*
1644 * Quick version for internal journal use (doesn't lock the journal).
1645 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1646 * and don't attempt to make any other journal updates.
1647 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001648void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001649{
1650 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001651
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001652 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 return;
1654
1655 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001656 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001657
1658 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001659 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001660 transaction = journal->j_running_transaction;
1661 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001662 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001663 spin_unlock(&journal->j_state_lock);
1664}
1665
1666/* Soft abort: record the abort error status in the journal superblock,
1667 * but don't do any other IO. */
1668static void __journal_abort_soft (journal_t *journal, int errno)
1669{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001670 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671 return;
1672
1673 if (!journal->j_errno)
1674 journal->j_errno = errno;
1675
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001676 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001677
1678 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680}
1681
1682/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001683 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001684 * @journal: the journal to shutdown.
1685 * @errno: an error number to record in the journal indicating
1686 * the reason for the shutdown.
1687 *
1688 * Perform a complete, immediate shutdown of the ENTIRE
1689 * journal (not of a single transaction). This operation cannot be
1690 * undone without closing and reopening the journal.
1691 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1694 * mode.
1695 *
1696 * Journal abort has very specific semantics. Any existing dirty,
1697 * unjournaled buffers in the main filesystem will still be written to
1698 * disk by bdflush, but the journaling mechanism will be suspended
1699 * immediately and no further transaction commits will be honoured.
1700 *
1701 * Any dirty, journaled buffers will be written back to disk without
1702 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1703 * filesystem, but we _do_ attempt to leave as much data as possible
1704 * behind for fsck to use for cleanup.
1705 *
1706 * Any attempt to get a new transaction handle on a journal which is in
1707 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001708 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 * entered abort state during the update.
1710 *
1711 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001712 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001714 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715 * which will be recorded (if possible) in the journal superblock. This
1716 * allows a client to record failure conditions in the middle of a
1717 * transaction without having to complete the transaction to record the
1718 * failure to disk. ext3_error, for example, now uses this
1719 * functionality.
1720 *
1721 * Errors which originate from within the journaling layer will NOT
1722 * supply an errno; a null errno implies that absolutely no further
1723 * writes are done to the journal (unless there are any already in
1724 * progress).
1725 *
1726 */
1727
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001728void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729{
1730 __journal_abort_soft(journal, errno);
1731}
1732
1733/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001734 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001735 * @journal: journal to examine.
1736 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001737 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001738 * time the journal was mounted - if the journal was stopped
1739 * without calling abort this will be 0.
1740 *
1741 * If the journal has been aborted on this mount time -EROFS will
1742 * be returned.
1743 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001744int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001745{
1746 int err;
1747
1748 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001749 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750 err = -EROFS;
1751 else
1752 err = journal->j_errno;
1753 spin_unlock(&journal->j_state_lock);
1754 return err;
1755}
1756
1757/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001758 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001759 * @journal: journal to act on.
1760 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001761 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001762 * mode.
1763 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001764int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001765{
1766 int err = 0;
1767
1768 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001769 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001770 err = -EROFS;
1771 else
1772 journal->j_errno = 0;
1773 spin_unlock(&journal->j_state_lock);
1774 return err;
1775}
1776
1777/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001778 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779 * @journal: journal to act on.
1780 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001781 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001782 * mode.
1783 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001784void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001785{
1786 spin_lock(&journal->j_state_lock);
1787 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001788 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001789 spin_unlock(&journal->j_state_lock);
1790}
1791
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001792int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793{
1794 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1795}
1796
1797/*
Zach Brownb517bea2006-10-11 01:21:08 -07001798 * helper functions to deal with 32 or 64bit block numbers.
1799 */
1800size_t journal_tag_bytes(journal_t *journal)
1801{
1802 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001803 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001804 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001805 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001806}
1807
1808/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001809 * Journal_head storage management
1810 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001811static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001812#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001813static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1814#endif
1815
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001816static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001817{
1818 int retval;
1819
Al Viro1076d172008-03-29 03:07:18 +00001820 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001821 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822 sizeof(struct journal_head),
1823 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001824 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001825 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001826 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001827 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828 retval = -ENOMEM;
1829 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1830 }
1831 return retval;
1832}
1833
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001834static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001835{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001836 if (jbd2_journal_head_cache) {
1837 kmem_cache_destroy(jbd2_journal_head_cache);
1838 jbd2_journal_head_cache = NULL;
1839 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001840}
1841
1842/*
1843 * journal_head splicing and dicing
1844 */
1845static struct journal_head *journal_alloc_journal_head(void)
1846{
1847 struct journal_head *ret;
1848 static unsigned long last_warning;
1849
Jose R. Santose23291b2007-07-18 08:57:06 -04001850#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001851 atomic_inc(&nr_journal_heads);
1852#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001853 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001854 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855 jbd_debug(1, "out of memory for journal_head\n");
1856 if (time_after(jiffies, last_warning + 5*HZ)) {
1857 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001858 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001859 last_warning = jiffies;
1860 }
Al Viro1076d172008-03-29 03:07:18 +00001861 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001863 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001864 }
1865 }
1866 return ret;
1867}
1868
1869static void journal_free_journal_head(struct journal_head *jh)
1870{
Jose R. Santose23291b2007-07-18 08:57:06 -04001871#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001872 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04001873 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001874#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001875 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001876}
1877
1878/*
1879 * A journal_head is attached to a buffer_head whenever JBD has an
1880 * interest in the buffer.
1881 *
1882 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1883 * is set. This bit is tested in core kernel code where we need to take
1884 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1885 * there.
1886 *
1887 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1888 *
1889 * When a buffer has its BH_JBD bit set it is immune from being released by
1890 * core kernel code, mainly via ->b_count.
1891 *
1892 * A journal_head may be detached from its buffer_head when the journal_head's
1893 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001894 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001895 * journal_head can be dropped if needed.
1896 *
1897 * Various places in the kernel want to attach a journal_head to a buffer_head
1898 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001899 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001900 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001901 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001902 *
1903 * So the typical usage would be:
1904 *
1905 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001906 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907 * ...
1908 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 *
1911 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1912 * because it has a non-zero b_transaction.
1913 */
1914
1915/*
1916 * Give a buffer_head a journal_head.
1917 *
1918 * Doesn't need the journal lock.
1919 * May sleep.
1920 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001921struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001922{
1923 struct journal_head *jh;
1924 struct journal_head *new_jh = NULL;
1925
1926repeat:
1927 if (!buffer_jbd(bh)) {
1928 new_jh = journal_alloc_journal_head();
1929 memset(new_jh, 0, sizeof(*new_jh));
1930 }
1931
1932 jbd_lock_bh_journal_head(bh);
1933 if (buffer_jbd(bh)) {
1934 jh = bh2jh(bh);
1935 } else {
1936 J_ASSERT_BH(bh,
1937 (atomic_read(&bh->b_count) > 0) ||
1938 (bh->b_page && bh->b_page->mapping));
1939
1940 if (!new_jh) {
1941 jbd_unlock_bh_journal_head(bh);
1942 goto repeat;
1943 }
1944
1945 jh = new_jh;
1946 new_jh = NULL; /* We consumed it */
1947 set_buffer_jbd(bh);
1948 bh->b_private = jh;
1949 jh->b_bh = bh;
1950 get_bh(bh);
1951 BUFFER_TRACE(bh, "added journal_head");
1952 }
1953 jh->b_jcount++;
1954 jbd_unlock_bh_journal_head(bh);
1955 if (new_jh)
1956 journal_free_journal_head(new_jh);
1957 return bh->b_private;
1958}
1959
1960/*
1961 * Grab a ref against this buffer_head's journal_head. If it ended up not
1962 * having a journal_head, return NULL
1963 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001964struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001965{
1966 struct journal_head *jh = NULL;
1967
1968 jbd_lock_bh_journal_head(bh);
1969 if (buffer_jbd(bh)) {
1970 jh = bh2jh(bh);
1971 jh->b_jcount++;
1972 }
1973 jbd_unlock_bh_journal_head(bh);
1974 return jh;
1975}
1976
1977static void __journal_remove_journal_head(struct buffer_head *bh)
1978{
1979 struct journal_head *jh = bh2jh(bh);
1980
1981 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1982
1983 get_bh(bh);
1984 if (jh->b_jcount == 0) {
1985 if (jh->b_transaction == NULL &&
1986 jh->b_next_transaction == NULL &&
1987 jh->b_cp_transaction == NULL) {
1988 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1989 J_ASSERT_BH(bh, buffer_jbd(bh));
1990 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1991 BUFFER_TRACE(bh, "remove journal_head");
1992 if (jh->b_frozen_data) {
1993 printk(KERN_WARNING "%s: freeing "
1994 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001995 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001996 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 }
1998 if (jh->b_committed_data) {
1999 printk(KERN_WARNING "%s: freeing "
2000 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002001 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002002 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002003 }
2004 bh->b_private = NULL;
2005 jh->b_bh = NULL; /* debug, really */
2006 clear_buffer_jbd(bh);
2007 __brelse(bh);
2008 journal_free_journal_head(jh);
2009 } else {
2010 BUFFER_TRACE(bh, "journal_head was locked");
2011 }
2012 }
2013}
2014
2015/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002016 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002017 * and has a zero b_jcount then remove and release its journal_head. If we did
2018 * see that the buffer is not used by any transaction we also "logically"
2019 * decrement ->b_count.
2020 *
2021 * We in fact take an additional increment on ->b_count as a convenience,
2022 * because the caller usually wants to do additional things with the bh
2023 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002024 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002025 * time. Once the caller has run __brelse(), the buffer is eligible for
2026 * reaping by try_to_free_buffers().
2027 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002028void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002029{
2030 jbd_lock_bh_journal_head(bh);
2031 __journal_remove_journal_head(bh);
2032 jbd_unlock_bh_journal_head(bh);
2033}
2034
2035/*
2036 * Drop a reference on the passed journal_head. If it fell to zero then try to
2037 * release the journal_head from the buffer_head.
2038 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002039void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002040{
2041 struct buffer_head *bh = jh2bh(jh);
2042
2043 jbd_lock_bh_journal_head(bh);
2044 J_ASSERT_JH(jh, jh->b_jcount > 0);
2045 --jh->b_jcount;
2046 if (!jh->b_jcount && !jh->b_transaction) {
2047 __journal_remove_journal_head(bh);
2048 __brelse(bh);
2049 }
2050 jbd_unlock_bh_journal_head(bh);
2051}
2052
2053/*
Jan Karac851ed52008-07-11 19:27:31 -04002054 * Initialize jbd inode head
2055 */
2056void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2057{
2058 jinode->i_transaction = NULL;
2059 jinode->i_next_transaction = NULL;
2060 jinode->i_vfs_inode = inode;
2061 jinode->i_flags = 0;
2062 INIT_LIST_HEAD(&jinode->i_list);
2063}
2064
2065/*
2066 * Function to be called before we start removing inode from memory (i.e.,
2067 * clear_inode() is a fine place to be called from). It removes inode from
2068 * transaction's lists.
2069 */
2070void jbd2_journal_release_jbd_inode(journal_t *journal,
2071 struct jbd2_inode *jinode)
2072{
2073 int writeout = 0;
2074
2075 if (!journal)
2076 return;
2077restart:
2078 spin_lock(&journal->j_list_lock);
2079 /* Is commit writing out inode - we have to wait */
2080 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2081 wait_queue_head_t *wq;
2082 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2083 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2084 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2085 spin_unlock(&journal->j_list_lock);
2086 schedule();
2087 finish_wait(wq, &wait.wait);
2088 goto restart;
2089 }
2090
2091 /* Do we need to wait for data writeback? */
2092 if (journal->j_committing_transaction == jinode->i_transaction)
2093 writeout = 1;
2094 if (jinode->i_transaction) {
2095 list_del(&jinode->i_list);
2096 jinode->i_transaction = NULL;
2097 }
2098 spin_unlock(&journal->j_list_lock);
2099}
2100
2101/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002102 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002103 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002104#ifdef CONFIG_JBD2_DEBUG
2105u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002106EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002107
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002108#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002109
Jose R. Santos6f38c742007-10-16 18:38:25 -04002110static struct dentry *jbd2_debugfs_dir;
2111static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002112
2113static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002114{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002115 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2116 if (jbd2_debugfs_dir)
2117 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2118 jbd2_debugfs_dir,
2119 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002120}
2121
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002122static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002123{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002124 debugfs_remove(jbd2_debug);
2125 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002126}
2127
2128#else
2129
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002130static void __init jbd2_create_debugfs_entry(void)
2131{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002132}
2133
2134static void __exit jbd2_remove_debugfs_entry(void)
2135{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002136}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002137
2138#endif
2139
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002140#ifdef CONFIG_PROC_FS
2141
2142#define JBD2_STATS_PROC_NAME "fs/jbd2"
2143
2144static void __init jbd2_create_jbd_stats_proc_entry(void)
2145{
2146 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2147}
2148
2149static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2150{
2151 if (proc_jbd2_stats)
2152 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2153}
2154
2155#else
2156
2157#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2158#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2159
2160#endif
2161
Christoph Lametere18b8902006-12-06 20:33:20 -08002162struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002163
2164static int __init journal_init_handle_cache(void)
2165{
Johann Lombardia920e942006-10-11 01:21:00 -07002166 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002167 sizeof(handle_t),
2168 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002169 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002170 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002171 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002172 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2173 return -ENOMEM;
2174 }
2175 return 0;
2176}
2177
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002178static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002179{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002180 if (jbd2_handle_cache)
2181 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002182}
2183
2184/*
2185 * Module startup and shutdown
2186 */
2187
2188static int __init journal_init_caches(void)
2189{
2190 int ret;
2191
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002192 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002193 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002194 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002195 if (ret == 0)
2196 ret = journal_init_handle_cache();
2197 return ret;
2198}
2199
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002200static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002201{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002202 jbd2_journal_destroy_revoke_caches();
2203 jbd2_journal_destroy_jbd2_journal_head_cache();
2204 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002205}
2206
2207static int __init journal_init(void)
2208{
2209 int ret;
2210
2211 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2212
2213 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002214 if (ret == 0) {
2215 jbd2_create_debugfs_entry();
2216 jbd2_create_jbd_stats_proc_entry();
2217 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002218 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002219 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002220 return ret;
2221}
2222
2223static void __exit journal_exit(void)
2224{
Jose R. Santose23291b2007-07-18 08:57:06 -04002225#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002226 int n = atomic_read(&nr_journal_heads);
2227 if (n)
2228 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2229#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002230 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002231 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002232 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002233}
2234
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002235/*
2236 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2237 * tracing infrastructure to map a dev_t to a device name.
2238 *
2239 * The caller should use rcu_read_lock() in order to make sure the
2240 * device name stays valid until its done with it. We use
2241 * rcu_read_lock() as well to make sure we're safe in case the caller
2242 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2243 * nested.
2244 */
2245struct devname_cache {
2246 struct rcu_head rcu;
2247 dev_t device;
2248 char devname[BDEVNAME_SIZE];
2249};
2250#define CACHE_SIZE_BITS 6
2251static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2252static DEFINE_SPINLOCK(devname_cache_lock);
2253
2254static void free_devcache(struct rcu_head *rcu)
2255{
2256 kfree(rcu);
2257}
2258
2259const char *jbd2_dev_to_name(dev_t device)
2260{
2261 int i = hash_32(device, CACHE_SIZE_BITS);
2262 char *ret;
2263 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002264 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002265
2266 rcu_read_lock();
2267 if (devcache[i] && devcache[i]->device == device) {
2268 ret = devcache[i]->devname;
2269 rcu_read_unlock();
2270 return ret;
2271 }
2272 rcu_read_unlock();
2273
Theodore Ts'ob5744802009-06-20 23:34:44 -04002274 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2275 if (!new_dev)
2276 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002277 spin_lock(&devname_cache_lock);
2278 if (devcache[i]) {
2279 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002280 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002281 ret = devcache[i]->devname;
2282 spin_unlock(&devname_cache_lock);
2283 return ret;
2284 }
2285 call_rcu(&devcache[i]->rcu, free_devcache);
2286 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002287 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002288 devcache[i]->device = device;
2289 bd = bdget(device);
2290 if (bd) {
2291 bdevname(bd, devcache[i]->devname);
2292 bdput(bd);
2293 } else
2294 __bdevname(device, devcache[i]->devname);
2295 ret = devcache[i]->devname;
2296 spin_unlock(&devname_cache_lock);
2297 return ret;
2298}
2299EXPORT_SYMBOL(jbd2_dev_to_name);
2300
Dave Kleikamp470decc2006-10-11 01:20:57 -07002301MODULE_LICENSE("GPL");
2302module_init(journal_init);
2303module_exit(journal_exit);
2304