blob: b0ab5219becbd92e34c6676a93716e102e70abe9 [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);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700361 jbd_lock_bh_state(bh_in);
362 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400363 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700364 goto repeat;
365 }
366
367 jh_in->b_frozen_data = tmp;
368 mapped_data = kmap_atomic(new_page, KM_USER0);
369 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
370 kunmap_atomic(mapped_data, KM_USER0);
371
372 new_page = virt_to_page(tmp);
373 new_offset = offset_in_page(tmp);
374 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700375
376 /*
377 * This isn't strictly necessary, as we're using frozen
378 * data for the escaping, but it keeps consistency with
379 * b_frozen_data usage.
380 */
381 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700382 }
383
384 /*
385 * Did we need to do an escaping? Now we've done all the
386 * copying, we can finally do so.
387 */
388 if (do_escape) {
389 mapped_data = kmap_atomic(new_page, KM_USER0);
390 *((unsigned int *)(mapped_data + new_offset)) = 0;
391 kunmap_atomic(mapped_data, KM_USER0);
392 }
393
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394 set_bh_page(new_bh, new_page, new_offset);
395 new_jh->b_transaction = NULL;
396 new_bh->b_size = jh2bh(jh_in)->b_size;
397 new_bh->b_bdev = transaction->t_journal->j_dev;
398 new_bh->b_blocknr = blocknr;
399 set_buffer_mapped(new_bh);
400 set_buffer_dirty(new_bh);
401
402 *jh_out = new_jh;
403
404 /*
405 * The to-be-written buffer needs to get moved to the io queue,
406 * and the original buffer whose contents we are shadowing or
407 * copying is moved to the transaction's shadow queue.
408 */
409 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400410 spin_lock(&journal->j_list_lock);
411 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
412 spin_unlock(&journal->j_list_lock);
413 jbd_unlock_bh_state(bh_in);
414
Dave Kleikamp470decc2006-10-11 01:20:57 -0700415 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700416 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700417
418 return do_escape | (done_copy_out << 1);
419}
420
421/*
422 * Allocation code for the journal file. Manage the space left in the
423 * journal, so that we can begin checkpointing when appropriate.
424 */
425
426/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700427 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700428 *
429 * Called with the journal already locked.
430 *
431 * Called under j_state_lock
432 */
433
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700434int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700435{
436 int left = journal->j_free;
437
438 assert_spin_locked(&journal->j_state_lock);
439
440 /*
441 * Be pessimistic here about the number of those free blocks which
442 * might be required for log descriptor control blocks.
443 */
444
445#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
446
447 left -= MIN_LOG_RESERVED_BLOCKS;
448
449 if (left <= 0)
450 return 0;
451 left -= (left >> 3);
452 return left;
453}
454
455/*
Jan Karac88ccea2009-02-10 11:27:46 -0500456 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700458int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459{
460 /*
461 * Are we already doing a recent enough commit?
462 */
463 if (!tid_geq(journal->j_commit_request, target)) {
464 /*
465 * We want a new commit: OK, mark the request and wakup the
466 * commit thread. We do _not_ do the commit ourselves.
467 */
468
469 journal->j_commit_request = target;
470 jbd_debug(1, "JBD: requesting commit %d/%d\n",
471 journal->j_commit_request,
472 journal->j_commit_sequence);
473 wake_up(&journal->j_wait_commit);
474 return 1;
475 }
476 return 0;
477}
478
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700479int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700480{
481 int ret;
482
483 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700484 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485 spin_unlock(&journal->j_state_lock);
486 return ret;
487}
488
489/*
490 * Force and wait upon a commit if the calling process is not within
491 * transaction. This is used for forcing out undo-protected data which contains
492 * bitmaps, when the fs is running out of space.
493 *
494 * We can only force the running transaction if we don't have an active handle;
495 * otherwise, we will deadlock.
496 *
497 * Returns true if a transaction was started.
498 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700499int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700500{
501 transaction_t *transaction = NULL;
502 tid_t tid;
503
504 spin_lock(&journal->j_state_lock);
505 if (journal->j_running_transaction && !current->journal_info) {
506 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700507 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700508 } else if (journal->j_committing_transaction)
509 transaction = journal->j_committing_transaction;
510
511 if (!transaction) {
512 spin_unlock(&journal->j_state_lock);
513 return 0; /* Nothing to retry */
514 }
515
516 tid = transaction->t_tid;
517 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700518 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700519 return 1;
520}
521
522/*
523 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500524 * if a transaction is going to be committed (or is currently already
525 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700527int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700528{
529 int ret = 0;
530
531 spin_lock(&journal->j_state_lock);
532 if (journal->j_running_transaction) {
533 tid_t tid = journal->j_running_transaction->t_tid;
534
Jan Karac88ccea2009-02-10 11:27:46 -0500535 __jbd2_log_start_commit(journal, tid);
536 /* There's a running transaction and we've just made sure
537 * it's commit has been scheduled. */
538 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700539 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500540 ret = 1;
541 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 /*
543 * If ext3_write_super() recently started a commit, then we
544 * have to wait for completion of that transaction
545 */
Jan Karac88ccea2009-02-10 11:27:46 -0500546 if (ptid)
547 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700548 ret = 1;
549 }
550 spin_unlock(&journal->j_state_lock);
551 return ret;
552}
553
554/*
555 * Wait for a specified commit to complete.
556 * The caller may not hold the journal lock.
557 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700558int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559{
560 int err = 0;
561
Jose R. Santose23291b2007-07-18 08:57:06 -0400562#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700563 spin_lock(&journal->j_state_lock);
564 if (!tid_geq(journal->j_commit_request, tid)) {
565 printk(KERN_EMERG
566 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400567 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568 }
569 spin_unlock(&journal->j_state_lock);
570#endif
571 spin_lock(&journal->j_state_lock);
572 while (tid_gt(tid, journal->j_commit_sequence)) {
573 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
574 tid, journal->j_commit_sequence);
575 wake_up(&journal->j_wait_commit);
576 spin_unlock(&journal->j_state_lock);
577 wait_event(journal->j_wait_done_commit,
578 !tid_gt(tid, journal->j_commit_sequence));
579 spin_lock(&journal->j_state_lock);
580 }
581 spin_unlock(&journal->j_state_lock);
582
583 if (unlikely(is_journal_aborted(journal))) {
584 printk(KERN_EMERG "journal commit I/O error\n");
585 err = -EIO;
586 }
587 return err;
588}
589
590/*
591 * Log buffer allocation routines:
592 */
593
Mingming Cao18eba7a2006-10-11 01:21:13 -0700594int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595{
596 unsigned long blocknr;
597
598 spin_lock(&journal->j_state_lock);
599 J_ASSERT(journal->j_free > 1);
600
601 blocknr = journal->j_head;
602 journal->j_head++;
603 journal->j_free--;
604 if (journal->j_head == journal->j_last)
605 journal->j_head = journal->j_first;
606 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700607 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700608}
609
610/*
611 * Conversion of logical to physical block numbers for the journal
612 *
613 * On external journals the journal blocks are identity-mapped, so
614 * this is a no-op. If needed, we can use j_blk_offset - everything is
615 * ready.
616 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700617int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700618 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619{
620 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700621 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622
623 if (journal->j_inode) {
624 ret = bmap(journal->j_inode, blocknr);
625 if (ret)
626 *retp = ret;
627 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 printk(KERN_ALERT "%s: journal block not found "
629 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400630 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700631 err = -EIO;
632 __journal_abort_soft(journal, err);
633 }
634 } else {
635 *retp = blocknr; /* +journal->j_blk_offset */
636 }
637 return err;
638}
639
640/*
641 * We play buffer_head aliasing tricks to write data/metadata blocks to
642 * the journal without copying their contents, but for journal
643 * descriptor blocks we do need to generate bona fide buffers.
644 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700645 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
647 * But we don't bother doing that, so there will be coherency problems with
648 * mmaps of blockdevs which hold live JBD-controlled filesystems.
649 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700650struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700651{
652 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700653 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654 int err;
655
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700656 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700657
658 if (err)
659 return NULL;
660
661 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500662 if (!bh)
663 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700664 lock_buffer(bh);
665 memset(bh->b_data, 0, journal->j_blocksize);
666 set_buffer_uptodate(bh);
667 unlock_buffer(bh);
668 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700669 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700670}
671
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500672struct jbd2_stats_proc_session {
673 journal_t *journal;
674 struct transaction_stats_s *stats;
675 int start;
676 int max;
677};
678
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500679static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
680{
681 return *pos ? NULL : SEQ_START_TOKEN;
682}
683
684static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
685{
686 return NULL;
687}
688
689static int jbd2_seq_info_show(struct seq_file *seq, void *v)
690{
691 struct jbd2_stats_proc_session *s = seq->private;
692
693 if (v != SEQ_START_TOKEN)
694 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400695 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500696 s->stats->ts_tid,
697 s->journal->j_max_transaction_buffers);
698 if (s->stats->ts_tid == 0)
699 return 0;
700 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400701 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500702 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400703 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500704 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400705 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500706 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400707 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500708 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400709 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500710 seq_printf(seq, " %lluus average transaction commit time\n",
711 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500712 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400713 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500714 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400715 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500716 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400717 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500718 return 0;
719}
720
721static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
722{
723}
724
James Morris88e9d342009-09-22 16:43:43 -0700725static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500726 .start = jbd2_seq_info_start,
727 .next = jbd2_seq_info_next,
728 .stop = jbd2_seq_info_stop,
729 .show = jbd2_seq_info_show,
730};
731
732static int jbd2_seq_info_open(struct inode *inode, struct file *file)
733{
734 journal_t *journal = PDE(inode)->data;
735 struct jbd2_stats_proc_session *s;
736 int rc, size;
737
738 s = kmalloc(sizeof(*s), GFP_KERNEL);
739 if (s == NULL)
740 return -ENOMEM;
741 size = sizeof(struct transaction_stats_s);
742 s->stats = kmalloc(size, GFP_KERNEL);
743 if (s->stats == NULL) {
744 kfree(s);
745 return -ENOMEM;
746 }
747 spin_lock(&journal->j_history_lock);
748 memcpy(s->stats, &journal->j_stats, size);
749 s->journal = journal;
750 spin_unlock(&journal->j_history_lock);
751
752 rc = seq_open(file, &jbd2_seq_info_ops);
753 if (rc == 0) {
754 struct seq_file *m = file->private_data;
755 m->private = s;
756 } else {
757 kfree(s->stats);
758 kfree(s);
759 }
760 return rc;
761
762}
763
764static int jbd2_seq_info_release(struct inode *inode, struct file *file)
765{
766 struct seq_file *seq = file->private_data;
767 struct jbd2_stats_proc_session *s = seq->private;
768 kfree(s->stats);
769 kfree(s);
770 return seq_release(inode, file);
771}
772
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700773static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500774 .owner = THIS_MODULE,
775 .open = jbd2_seq_info_open,
776 .read = seq_read,
777 .llseek = seq_lseek,
778 .release = jbd2_seq_info_release,
779};
780
781static struct proc_dir_entry *proc_jbd2_stats;
782
783static void jbd2_stats_proc_init(journal_t *journal)
784{
Theodore Ts'o05496762008-09-16 14:36:17 -0400785 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500786 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700787 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
788 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500789 }
790}
791
792static void jbd2_stats_proc_exit(journal_t *journal)
793{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500794 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400795 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500796}
797
Dave Kleikamp470decc2006-10-11 01:20:57 -0700798/*
799 * Management for journal control blocks: functions to create and
800 * destroy journal_t structures, and to initialise and read existing
801 * journal blocks from disk. */
802
803/* First: create and setup a journal_t object in memory. We initialise
804 * very few fields yet: that has to wait until we have created the
805 * journal structures from from scratch, or loaded them from disk. */
806
807static journal_t * journal_init_common (void)
808{
809 journal_t *journal;
810 int err;
811
Mingming Caod802ffa2007-10-16 18:38:25 -0400812 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700813 if (!journal)
814 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700815
816 init_waitqueue_head(&journal->j_wait_transaction_locked);
817 init_waitqueue_head(&journal->j_wait_logspace);
818 init_waitqueue_head(&journal->j_wait_done_commit);
819 init_waitqueue_head(&journal->j_wait_checkpoint);
820 init_waitqueue_head(&journal->j_wait_commit);
821 init_waitqueue_head(&journal->j_wait_updates);
822 mutex_init(&journal->j_barrier);
823 mutex_init(&journal->j_checkpoint_mutex);
824 spin_lock_init(&journal->j_revoke_lock);
825 spin_lock_init(&journal->j_list_lock);
826 spin_lock_init(&journal->j_state_lock);
827
Mingming Caocd02ff02007-10-16 18:38:25 -0400828 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500829 journal->j_min_batch_time = 0;
830 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700831
832 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700833 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700834
835 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700836 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700837 if (err) {
838 kfree(journal);
839 goto fail;
840 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500841
Theodore Ts'obf699322009-09-30 00:32:06 -0400842 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500843
Dave Kleikamp470decc2006-10-11 01:20:57 -0700844 return journal;
845fail:
846 return NULL;
847}
848
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700849/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700850 *
851 * Create a journal structure assigned some fixed set of disk blocks to
852 * the journal. We don't actually touch those disk blocks yet, but we
853 * need to set up all of the mapping information to tell the journaling
854 * system where the journal blocks are.
855 *
856 */
857
858/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400859 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700860 * @bdev: Block device on which to create the journal
861 * @fs_dev: Device which hold journalled filesystem for this journal.
862 * @start: Block nr Start of journal.
863 * @len: Length of the journal in blocks.
864 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400865 *
866 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700867 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700868 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700869 * range of blocks on an arbitrary block device.
870 *
871 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700872journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700873 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700874 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700875{
876 journal_t *journal = journal_init_common();
877 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400878 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700879 int n;
880
881 if (!journal)
882 return NULL;
883
884 /* journal descriptor can store up to n blocks -bzzz */
885 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -0500886 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700887 n = journal->j_blocksize / sizeof(journal_block_tag_t);
888 journal->j_wbufsize = n;
889 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
890 if (!journal->j_wbuf) {
891 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400892 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500893 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700894 }
895 journal->j_dev = bdev;
896 journal->j_fs_dev = fs_dev;
897 journal->j_blk_offset = start;
898 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -0400899 bdevname(journal->j_dev, journal->j_devname);
900 p = journal->j_devname;
901 while ((p = strchr(p, '/')))
902 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -0700903
904 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500905 if (!bh) {
906 printk(KERN_ERR
907 "%s: Cannot get buffer for journal superblock\n",
908 __func__);
909 goto out_err;
910 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700911 journal->j_sb_buffer = bh;
912 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500913
Dave Kleikamp470decc2006-10-11 01:20:57 -0700914 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500915out_err:
916 jbd2_stats_proc_exit(journal);
917 kfree(journal);
918 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700919}
920
921/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700922 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700923 * @inode: An inode to create the journal in
924 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700925 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700926 * the journal. The inode must exist already, must support bmap() and
927 * must have all data blocks preallocated.
928 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700929journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700930{
931 struct buffer_head *bh;
932 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400933 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700934 int err;
935 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700936 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700937
938 if (!journal)
939 return NULL;
940
941 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
942 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400943 bdevname(journal->j_dev, journal->j_devname);
944 p = journal->j_devname;
945 while ((p = strchr(p, '/')))
946 *p = '!';
947 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400948 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700949 jbd_debug(1,
950 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
951 journal, inode->i_sb->s_id, inode->i_ino,
952 (long long) inode->i_size,
953 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
954
955 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
956 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500957 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700958
959 /* journal descriptor can store up to n blocks -bzzz */
960 n = journal->j_blocksize / sizeof(journal_block_tag_t);
961 journal->j_wbufsize = n;
962 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
963 if (!journal->j_wbuf) {
964 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400965 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500966 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700967 }
968
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700969 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700970 /* If that failed, give up */
971 if (err) {
972 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400973 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500974 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975 }
976
977 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500978 if (!bh) {
979 printk(KERN_ERR
980 "%s: Cannot get buffer for journal superblock\n",
981 __func__);
982 goto out_err;
983 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700984 journal->j_sb_buffer = bh;
985 journal->j_superblock = (journal_superblock_t *)bh->b_data;
986
987 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500988out_err:
989 jbd2_stats_proc_exit(journal);
990 kfree(journal);
991 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700992}
993
994/*
995 * If the journal init or create aborts, we need to mark the journal
996 * superblock as being NULL to prevent the journal destroy from writing
997 * back a bogus superblock.
998 */
999static void journal_fail_superblock (journal_t *journal)
1000{
1001 struct buffer_head *bh = journal->j_sb_buffer;
1002 brelse(bh);
1003 journal->j_sb_buffer = NULL;
1004}
1005
1006/*
1007 * Given a journal_t structure, initialise the various fields for
1008 * startup of a new journaling session. We use this both when creating
1009 * a journal, and after recovering an old journal to reset it for
1010 * subsequent use.
1011 */
1012
1013static int journal_reset(journal_t *journal)
1014{
1015 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001016 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001017
1018 first = be32_to_cpu(sb->s_first);
1019 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001020 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1021 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1022 first, last);
1023 journal_fail_superblock(journal);
1024 return -EINVAL;
1025 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001026
1027 journal->j_first = first;
1028 journal->j_last = last;
1029
1030 journal->j_head = first;
1031 journal->j_tail = first;
1032 journal->j_free = last - first;
1033
1034 journal->j_tail_sequence = journal->j_transaction_sequence;
1035 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1036 journal->j_commit_request = journal->j_commit_sequence;
1037
1038 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1039
1040 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001041 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001042 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043}
1044
1045/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001046 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001047 * @journal: The journal to update.
1048 * @wait: Set to '0' if you don't want to wait for IO completion.
1049 *
1050 * Update a journal's dynamic superblock fields and write it to disk,
1051 * optionally waiting for the IO to complete.
1052 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001053void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001054{
1055 journal_superblock_t *sb = journal->j_superblock;
1056 struct buffer_head *bh = journal->j_sb_buffer;
1057
1058 /*
1059 * As a special case, if the on-disk copy is already marked as needing
1060 * no recovery (s_start == 0) and there are no outstanding transactions
1061 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001062 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063 * attempting a write to a potential-readonly device.
1064 */
1065 if (sb->s_start == 0 && journal->j_tail_sequence ==
1066 journal->j_transaction_sequence) {
1067 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1068 "(start %ld, seq %d, errno %d)\n",
1069 journal->j_tail, journal->j_tail_sequence,
1070 journal->j_errno);
1071 goto out;
1072 }
1073
Theodore Ts'o914258b2008-10-06 21:35:40 -04001074 if (buffer_write_io_error(bh)) {
1075 /*
1076 * Oh, dear. A previous attempt to write the journal
1077 * superblock failed. This could happen because the
1078 * USB device was yanked out. Or it could happen to
1079 * be a transient write error and maybe the block will
1080 * be remapped. Nothing we can do but to retry the
1081 * write and hope for the best.
1082 */
1083 printk(KERN_ERR "JBD2: previous I/O error detected "
1084 "for journal superblock update for %s.\n",
1085 journal->j_devname);
1086 clear_buffer_write_io_error(bh);
1087 set_buffer_uptodate(bh);
1088 }
1089
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 spin_lock(&journal->j_state_lock);
1091 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1092 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1093
1094 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1095 sb->s_start = cpu_to_be32(journal->j_tail);
1096 sb->s_errno = cpu_to_be32(journal->j_errno);
1097 spin_unlock(&journal->j_state_lock);
1098
1099 BUFFER_TRACE(bh, "marking dirty");
1100 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001101 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001102 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001103 if (buffer_write_io_error(bh)) {
1104 printk(KERN_ERR "JBD2: I/O error detected "
1105 "when updating journal superblock for %s.\n",
1106 journal->j_devname);
1107 clear_buffer_write_io_error(bh);
1108 set_buffer_uptodate(bh);
1109 }
1110 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001111 ll_rw_block(SWRITE, 1, &bh);
1112
1113out:
1114 /* If we have just flushed the log (by marking s_start==0), then
1115 * any future commit will have to be careful to update the
1116 * superblock again to re-record the true start of the log. */
1117
1118 spin_lock(&journal->j_state_lock);
1119 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001120 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001122 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001123 spin_unlock(&journal->j_state_lock);
1124}
1125
1126/*
1127 * Read the superblock for a given journal, performing initial
1128 * validation of the format.
1129 */
1130
1131static int journal_get_superblock(journal_t *journal)
1132{
1133 struct buffer_head *bh;
1134 journal_superblock_t *sb;
1135 int err = -EIO;
1136
1137 bh = journal->j_sb_buffer;
1138
1139 J_ASSERT(bh != NULL);
1140 if (!buffer_uptodate(bh)) {
1141 ll_rw_block(READ, 1, &bh);
1142 wait_on_buffer(bh);
1143 if (!buffer_uptodate(bh)) {
1144 printk (KERN_ERR
1145 "JBD: IO error reading journal superblock\n");
1146 goto out;
1147 }
1148 }
1149
1150 sb = journal->j_superblock;
1151
1152 err = -EINVAL;
1153
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001154 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001155 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1156 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1157 goto out;
1158 }
1159
1160 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001161 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001162 journal->j_format_version = 1;
1163 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001164 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001165 journal->j_format_version = 2;
1166 break;
1167 default:
1168 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1169 goto out;
1170 }
1171
1172 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1173 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1174 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1175 printk (KERN_WARNING "JBD: journal file too short\n");
1176 goto out;
1177 }
1178
1179 return 0;
1180
1181out:
1182 journal_fail_superblock(journal);
1183 return err;
1184}
1185
1186/*
1187 * Load the on-disk journal superblock and read the key fields into the
1188 * journal_t.
1189 */
1190
1191static int load_superblock(journal_t *journal)
1192{
1193 int err;
1194 journal_superblock_t *sb;
1195
1196 err = journal_get_superblock(journal);
1197 if (err)
1198 return err;
1199
1200 sb = journal->j_superblock;
1201
1202 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1203 journal->j_tail = be32_to_cpu(sb->s_start);
1204 journal->j_first = be32_to_cpu(sb->s_first);
1205 journal->j_last = be32_to_cpu(sb->s_maxlen);
1206 journal->j_errno = be32_to_cpu(sb->s_errno);
1207
1208 return 0;
1209}
1210
1211
1212/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001213 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001214 * @journal: Journal to act on.
1215 *
1216 * Given a journal_t structure which tells us which disk blocks contain
1217 * a journal, read the journal from disk to initialise the in-memory
1218 * structures.
1219 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001220int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001221{
1222 int err;
1223 journal_superblock_t *sb;
1224
1225 err = load_superblock(journal);
1226 if (err)
1227 return err;
1228
1229 sb = journal->j_superblock;
1230 /* If this is a V2 superblock, then we have to check the
1231 * features flags on it. */
1232
1233 if (journal->j_format_version >= 2) {
1234 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001235 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001236 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001237 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238 printk (KERN_WARNING
1239 "JBD: Unrecognised features on journal\n");
1240 return -EINVAL;
1241 }
1242 }
1243
Dave Kleikamp470decc2006-10-11 01:20:57 -07001244 /* Let the recovery code check whether it needs to recover any
1245 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001246 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001247 goto recovery_error;
1248
1249 /* OK, we've finished with the dynamic journal bits:
1250 * reinitialise the dynamic contents of the superblock in memory
1251 * and reset them on disk. */
1252 if (journal_reset(journal))
1253 goto recovery_error;
1254
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001255 journal->j_flags &= ~JBD2_ABORT;
1256 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 return 0;
1258
1259recovery_error:
1260 printk (KERN_WARNING "JBD: recovery failed\n");
1261 return -EIO;
1262}
1263
1264/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001265 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001266 * @journal: Journal to act on.
1267 *
1268 * Release a journal_t structure once it is no longer in use by the
1269 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001270 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001271 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001272int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001273{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001274 int err = 0;
1275
Dave Kleikamp470decc2006-10-11 01:20:57 -07001276 /* Wait for the commit thread to wake up and die. */
1277 journal_kill_thread(journal);
1278
1279 /* Force a final log commit */
1280 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001281 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001282
1283 /* Force any old transactions to disk */
1284
1285 /* Totally anal locking here... */
1286 spin_lock(&journal->j_list_lock);
1287 while (journal->j_checkpoint_transactions != NULL) {
1288 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001289 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001290 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001291 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001292 spin_lock(&journal->j_list_lock);
1293 }
1294
1295 J_ASSERT(journal->j_running_transaction == NULL);
1296 J_ASSERT(journal->j_committing_transaction == NULL);
1297 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1298 spin_unlock(&journal->j_list_lock);
1299
Dave Kleikamp470decc2006-10-11 01:20:57 -07001300 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001301 if (!is_journal_aborted(journal)) {
1302 /* We can now mark the journal as empty. */
1303 journal->j_tail = 0;
1304 journal->j_tail_sequence =
1305 ++journal->j_transaction_sequence;
1306 jbd2_journal_update_superblock(journal, 1);
1307 } else {
1308 err = -EIO;
1309 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001310 brelse(journal->j_sb_buffer);
1311 }
1312
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001313 if (journal->j_proc_entry)
1314 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001315 if (journal->j_inode)
1316 iput(journal->j_inode);
1317 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001318 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001319 kfree(journal->j_wbuf);
1320 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001321
1322 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001323}
1324
1325
1326/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001327 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001328 * @journal: Journal to check.
1329 * @compat: bitmask of compatible features
1330 * @ro: bitmask of features that force read-only mount
1331 * @incompat: bitmask of incompatible features
1332 *
1333 * Check whether the journal uses all of a given set of
1334 * features. Return true (non-zero) if it does.
1335 **/
1336
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001337int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001338 unsigned long ro, unsigned long incompat)
1339{
1340 journal_superblock_t *sb;
1341
1342 if (!compat && !ro && !incompat)
1343 return 1;
1344 if (journal->j_format_version == 1)
1345 return 0;
1346
1347 sb = journal->j_superblock;
1348
1349 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1350 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1351 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1352 return 1;
1353
1354 return 0;
1355}
1356
1357/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001358 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001359 * @journal: Journal to check.
1360 * @compat: bitmask of compatible features
1361 * @ro: bitmask of features that force read-only mount
1362 * @incompat: bitmask of incompatible features
1363 *
1364 * Check whether the journaling code supports the use of
1365 * all of a given set of features on this journal. Return true
1366 * (non-zero) if it can. */
1367
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001368int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001369 unsigned long ro, unsigned long incompat)
1370{
1371 journal_superblock_t *sb;
1372
1373 if (!compat && !ro && !incompat)
1374 return 1;
1375
1376 sb = journal->j_superblock;
1377
1378 /* We can support any known requested features iff the
1379 * superblock is in version 2. Otherwise we fail to support any
1380 * extended sb features. */
1381
1382 if (journal->j_format_version != 2)
1383 return 0;
1384
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001385 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1386 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1387 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001388 return 1;
1389
1390 return 0;
1391}
1392
1393/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001394 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395 * @journal: Journal to act on.
1396 * @compat: bitmask of compatible features
1397 * @ro: bitmask of features that force read-only mount
1398 * @incompat: bitmask of incompatible features
1399 *
1400 * Mark a given journal feature as present on the
1401 * superblock. Returns true if the requested features could be set.
1402 *
1403 */
1404
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001405int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 unsigned long ro, unsigned long incompat)
1407{
1408 journal_superblock_t *sb;
1409
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001410 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001411 return 1;
1412
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001413 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001414 return 0;
1415
1416 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1417 compat, ro, incompat);
1418
1419 sb = journal->j_superblock;
1420
1421 sb->s_feature_compat |= cpu_to_be32(compat);
1422 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1423 sb->s_feature_incompat |= cpu_to_be32(incompat);
1424
1425 return 1;
1426}
1427
Girish Shilamkar818d2762008-01-28 23:58:27 -05001428/*
1429 * jbd2_journal_clear_features () - Clear a given journal feature in the
1430 * superblock
1431 * @journal: Journal to act on.
1432 * @compat: bitmask of compatible features
1433 * @ro: bitmask of features that force read-only mount
1434 * @incompat: bitmask of incompatible features
1435 *
1436 * Clear a given journal feature as present on the
1437 * superblock.
1438 */
1439void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1440 unsigned long ro, unsigned long incompat)
1441{
1442 journal_superblock_t *sb;
1443
1444 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1445 compat, ro, incompat);
1446
1447 sb = journal->j_superblock;
1448
1449 sb->s_feature_compat &= ~cpu_to_be32(compat);
1450 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1451 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1452}
1453EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001454
1455/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001456 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001457 * @journal: Journal to act on.
1458 *
1459 * Given an initialised but unloaded journal struct, poke about in the
1460 * on-disk structure to update it to the most recent supported version.
1461 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001462int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001463{
1464 journal_superblock_t *sb;
1465 int err;
1466
1467 err = journal_get_superblock(journal);
1468 if (err)
1469 return err;
1470
1471 sb = journal->j_superblock;
1472
1473 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001474 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001475 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001476 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 return journal_convert_superblock_v1(journal, sb);
1478 default:
1479 break;
1480 }
1481 return -EINVAL;
1482}
1483
1484static int journal_convert_superblock_v1(journal_t *journal,
1485 journal_superblock_t *sb)
1486{
1487 int offset, blocksize;
1488 struct buffer_head *bh;
1489
1490 printk(KERN_WARNING
1491 "JBD: Converting superblock from version 1 to 2.\n");
1492
1493 /* Pre-initialise new fields to zero */
1494 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1495 blocksize = be32_to_cpu(sb->s_blocksize);
1496 memset(&sb->s_feature_compat, 0, blocksize-offset);
1497
1498 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001499 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500 journal->j_format_version = 2;
1501
1502 bh = journal->j_sb_buffer;
1503 BUFFER_TRACE(bh, "marking dirty");
1504 mark_buffer_dirty(bh);
1505 sync_dirty_buffer(bh);
1506 return 0;
1507}
1508
1509
1510/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001511 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001512 * @journal: Journal to act on.
1513 *
1514 * Flush all data for a given journal to disk and empty the journal.
1515 * Filesystems can use this when remounting readonly to ensure that
1516 * recovery does not need to happen on remount.
1517 */
1518
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001519int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001520{
1521 int err = 0;
1522 transaction_t *transaction = NULL;
1523 unsigned long old_tail;
1524
1525 spin_lock(&journal->j_state_lock);
1526
1527 /* Force everything buffered to the log... */
1528 if (journal->j_running_transaction) {
1529 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001530 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001531 } else if (journal->j_committing_transaction)
1532 transaction = journal->j_committing_transaction;
1533
1534 /* Wait for the log commit to complete... */
1535 if (transaction) {
1536 tid_t tid = transaction->t_tid;
1537
1538 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001539 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001540 } else {
1541 spin_unlock(&journal->j_state_lock);
1542 }
1543
1544 /* ...and flush everything in the log out to disk. */
1545 spin_lock(&journal->j_list_lock);
1546 while (!err && journal->j_checkpoint_transactions != NULL) {
1547 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001548 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001549 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001550 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001551 spin_lock(&journal->j_list_lock);
1552 }
1553 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001554
1555 if (is_journal_aborted(journal))
1556 return -EIO;
1557
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001558 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559
1560 /* Finally, mark the journal as really needing no recovery.
1561 * This sets s_start==0 in the underlying superblock, which is
1562 * the magic code for a fully-recovered superblock. Any future
1563 * commits of data to the journal will restore the current
1564 * s_start value. */
1565 spin_lock(&journal->j_state_lock);
1566 old_tail = journal->j_tail;
1567 journal->j_tail = 0;
1568 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001569 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 spin_lock(&journal->j_state_lock);
1571 journal->j_tail = old_tail;
1572
1573 J_ASSERT(!journal->j_running_transaction);
1574 J_ASSERT(!journal->j_committing_transaction);
1575 J_ASSERT(!journal->j_checkpoint_transactions);
1576 J_ASSERT(journal->j_head == journal->j_tail);
1577 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1578 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001579 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001580}
1581
1582/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001583 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001584 * @journal: Journal to act on.
1585 * @write: flag (see below)
1586 *
1587 * Wipe out all of the contents of a journal, safely. This will produce
1588 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001589 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001590 *
1591 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1592 * we merely suppress recovery.
1593 */
1594
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001595int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596{
1597 journal_superblock_t *sb;
1598 int err = 0;
1599
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601
1602 err = load_superblock(journal);
1603 if (err)
1604 return err;
1605
1606 sb = journal->j_superblock;
1607
1608 if (!journal->j_tail)
1609 goto no_recovery;
1610
1611 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1612 write ? "Clearing" : "Ignoring");
1613
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001614 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001615 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001616 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617
1618 no_recovery:
1619 return err;
1620}
1621
1622/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001623 * Journal abort has very specific semantics, which we describe
1624 * for journal abort.
1625 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001626 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627 * itself are here.
1628 */
1629
1630/*
1631 * Quick version for internal journal use (doesn't lock the journal).
1632 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1633 * and don't attempt to make any other journal updates.
1634 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001635void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001636{
1637 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001638
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001639 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001640 return;
1641
1642 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001643 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644
1645 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001646 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647 transaction = journal->j_running_transaction;
1648 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001649 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 spin_unlock(&journal->j_state_lock);
1651}
1652
1653/* Soft abort: record the abort error status in the journal superblock,
1654 * but don't do any other IO. */
1655static void __journal_abort_soft (journal_t *journal, int errno)
1656{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001657 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 return;
1659
1660 if (!journal->j_errno)
1661 journal->j_errno = errno;
1662
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001663 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664
1665 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001666 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001667}
1668
1669/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001670 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671 * @journal: the journal to shutdown.
1672 * @errno: an error number to record in the journal indicating
1673 * the reason for the shutdown.
1674 *
1675 * Perform a complete, immediate shutdown of the ENTIRE
1676 * journal (not of a single transaction). This operation cannot be
1677 * undone without closing and reopening the journal.
1678 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1681 * mode.
1682 *
1683 * Journal abort has very specific semantics. Any existing dirty,
1684 * unjournaled buffers in the main filesystem will still be written to
1685 * disk by bdflush, but the journaling mechanism will be suspended
1686 * immediately and no further transaction commits will be honoured.
1687 *
1688 * Any dirty, journaled buffers will be written back to disk without
1689 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1690 * filesystem, but we _do_ attempt to leave as much data as possible
1691 * behind for fsck to use for cleanup.
1692 *
1693 * Any attempt to get a new transaction handle on a journal which is in
1694 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001695 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001696 * entered abort state during the update.
1697 *
1698 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001699 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001700 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001701 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001702 * which will be recorded (if possible) in the journal superblock. This
1703 * allows a client to record failure conditions in the middle of a
1704 * transaction without having to complete the transaction to record the
1705 * failure to disk. ext3_error, for example, now uses this
1706 * functionality.
1707 *
1708 * Errors which originate from within the journaling layer will NOT
1709 * supply an errno; a null errno implies that absolutely no further
1710 * writes are done to the journal (unless there are any already in
1711 * progress).
1712 *
1713 */
1714
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001715void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001716{
1717 __journal_abort_soft(journal, errno);
1718}
1719
1720/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 * @journal: journal to examine.
1723 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001724 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001725 * time the journal was mounted - if the journal was stopped
1726 * without calling abort this will be 0.
1727 *
1728 * If the journal has been aborted on this mount time -EROFS will
1729 * be returned.
1730 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732{
1733 int err;
1734
1735 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001736 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001737 err = -EROFS;
1738 else
1739 err = journal->j_errno;
1740 spin_unlock(&journal->j_state_lock);
1741 return err;
1742}
1743
1744/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001745 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001746 * @journal: journal to act on.
1747 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001748 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 * mode.
1750 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001751int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752{
1753 int err = 0;
1754
1755 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001756 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001757 err = -EROFS;
1758 else
1759 journal->j_errno = 0;
1760 spin_unlock(&journal->j_state_lock);
1761 return err;
1762}
1763
1764/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001765 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766 * @journal: journal to act on.
1767 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001768 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001769 * mode.
1770 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001771void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001772{
1773 spin_lock(&journal->j_state_lock);
1774 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001775 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776 spin_unlock(&journal->j_state_lock);
1777}
1778
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001779int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001780{
1781 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1782}
1783
1784/*
Zach Brownb517bea2006-10-11 01:21:08 -07001785 * helper functions to deal with 32 or 64bit block numbers.
1786 */
1787size_t journal_tag_bytes(journal_t *journal)
1788{
1789 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001790 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001791 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001792 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001793}
1794
1795/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 * Journal_head storage management
1797 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001798static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001799#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001800static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1801#endif
1802
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001803static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804{
1805 int retval;
1806
Al Viro1076d172008-03-29 03:07:18 +00001807 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001808 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001809 sizeof(struct journal_head),
1810 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001811 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001812 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001813 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001814 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001815 retval = -ENOMEM;
1816 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1817 }
1818 return retval;
1819}
1820
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001821static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001823 if (jbd2_journal_head_cache) {
1824 kmem_cache_destroy(jbd2_journal_head_cache);
1825 jbd2_journal_head_cache = NULL;
1826 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001827}
1828
1829/*
1830 * journal_head splicing and dicing
1831 */
1832static struct journal_head *journal_alloc_journal_head(void)
1833{
1834 struct journal_head *ret;
1835 static unsigned long last_warning;
1836
Jose R. Santose23291b2007-07-18 08:57:06 -04001837#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001838 atomic_inc(&nr_journal_heads);
1839#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001840 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001841 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 jbd_debug(1, "out of memory for journal_head\n");
1843 if (time_after(jiffies, last_warning + 5*HZ)) {
1844 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001845 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001846 last_warning = jiffies;
1847 }
Al Viro1076d172008-03-29 03:07:18 +00001848 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001850 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001851 }
1852 }
1853 return ret;
1854}
1855
1856static void journal_free_journal_head(struct journal_head *jh)
1857{
Jose R. Santose23291b2007-07-18 08:57:06 -04001858#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001859 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04001860 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001861#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001862 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001863}
1864
1865/*
1866 * A journal_head is attached to a buffer_head whenever JBD has an
1867 * interest in the buffer.
1868 *
1869 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1870 * is set. This bit is tested in core kernel code where we need to take
1871 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1872 * there.
1873 *
1874 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1875 *
1876 * When a buffer has its BH_JBD bit set it is immune from being released by
1877 * core kernel code, mainly via ->b_count.
1878 *
1879 * A journal_head may be detached from its buffer_head when the journal_head's
1880 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001881 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001882 * journal_head can be dropped if needed.
1883 *
1884 * Various places in the kernel want to attach a journal_head to a buffer_head
1885 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001886 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001887 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001888 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889 *
1890 * So the typical usage would be:
1891 *
1892 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001893 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001894 * ...
1895 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001896 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001897 *
1898 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1899 * because it has a non-zero b_transaction.
1900 */
1901
1902/*
1903 * Give a buffer_head a journal_head.
1904 *
1905 * Doesn't need the journal lock.
1906 * May sleep.
1907 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001908struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001909{
1910 struct journal_head *jh;
1911 struct journal_head *new_jh = NULL;
1912
1913repeat:
1914 if (!buffer_jbd(bh)) {
1915 new_jh = journal_alloc_journal_head();
1916 memset(new_jh, 0, sizeof(*new_jh));
1917 }
1918
1919 jbd_lock_bh_journal_head(bh);
1920 if (buffer_jbd(bh)) {
1921 jh = bh2jh(bh);
1922 } else {
1923 J_ASSERT_BH(bh,
1924 (atomic_read(&bh->b_count) > 0) ||
1925 (bh->b_page && bh->b_page->mapping));
1926
1927 if (!new_jh) {
1928 jbd_unlock_bh_journal_head(bh);
1929 goto repeat;
1930 }
1931
1932 jh = new_jh;
1933 new_jh = NULL; /* We consumed it */
1934 set_buffer_jbd(bh);
1935 bh->b_private = jh;
1936 jh->b_bh = bh;
1937 get_bh(bh);
1938 BUFFER_TRACE(bh, "added journal_head");
1939 }
1940 jh->b_jcount++;
1941 jbd_unlock_bh_journal_head(bh);
1942 if (new_jh)
1943 journal_free_journal_head(new_jh);
1944 return bh->b_private;
1945}
1946
1947/*
1948 * Grab a ref against this buffer_head's journal_head. If it ended up not
1949 * having a journal_head, return NULL
1950 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001951struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001952{
1953 struct journal_head *jh = NULL;
1954
1955 jbd_lock_bh_journal_head(bh);
1956 if (buffer_jbd(bh)) {
1957 jh = bh2jh(bh);
1958 jh->b_jcount++;
1959 }
1960 jbd_unlock_bh_journal_head(bh);
1961 return jh;
1962}
1963
1964static void __journal_remove_journal_head(struct buffer_head *bh)
1965{
1966 struct journal_head *jh = bh2jh(bh);
1967
1968 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1969
1970 get_bh(bh);
1971 if (jh->b_jcount == 0) {
1972 if (jh->b_transaction == NULL &&
1973 jh->b_next_transaction == NULL &&
1974 jh->b_cp_transaction == NULL) {
1975 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1976 J_ASSERT_BH(bh, buffer_jbd(bh));
1977 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1978 BUFFER_TRACE(bh, "remove journal_head");
1979 if (jh->b_frozen_data) {
1980 printk(KERN_WARNING "%s: freeing "
1981 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001982 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001983 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984 }
1985 if (jh->b_committed_data) {
1986 printk(KERN_WARNING "%s: freeing "
1987 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001988 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001989 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001990 }
1991 bh->b_private = NULL;
1992 jh->b_bh = NULL; /* debug, really */
1993 clear_buffer_jbd(bh);
1994 __brelse(bh);
1995 journal_free_journal_head(jh);
1996 } else {
1997 BUFFER_TRACE(bh, "journal_head was locked");
1998 }
1999 }
2000}
2001
2002/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002003 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 * and has a zero b_jcount then remove and release its journal_head. If we did
2005 * see that the buffer is not used by any transaction we also "logically"
2006 * decrement ->b_count.
2007 *
2008 * We in fact take an additional increment on ->b_count as a convenience,
2009 * because the caller usually wants to do additional things with the bh
2010 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002011 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 * time. Once the caller has run __brelse(), the buffer is eligible for
2013 * reaping by try_to_free_buffers().
2014 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002015void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016{
2017 jbd_lock_bh_journal_head(bh);
2018 __journal_remove_journal_head(bh);
2019 jbd_unlock_bh_journal_head(bh);
2020}
2021
2022/*
2023 * Drop a reference on the passed journal_head. If it fell to zero then try to
2024 * release the journal_head from the buffer_head.
2025 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002026void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002027{
2028 struct buffer_head *bh = jh2bh(jh);
2029
2030 jbd_lock_bh_journal_head(bh);
2031 J_ASSERT_JH(jh, jh->b_jcount > 0);
2032 --jh->b_jcount;
2033 if (!jh->b_jcount && !jh->b_transaction) {
2034 __journal_remove_journal_head(bh);
2035 __brelse(bh);
2036 }
2037 jbd_unlock_bh_journal_head(bh);
2038}
2039
2040/*
Jan Karac851ed52008-07-11 19:27:31 -04002041 * Initialize jbd inode head
2042 */
2043void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2044{
2045 jinode->i_transaction = NULL;
2046 jinode->i_next_transaction = NULL;
2047 jinode->i_vfs_inode = inode;
2048 jinode->i_flags = 0;
2049 INIT_LIST_HEAD(&jinode->i_list);
2050}
2051
2052/*
2053 * Function to be called before we start removing inode from memory (i.e.,
2054 * clear_inode() is a fine place to be called from). It removes inode from
2055 * transaction's lists.
2056 */
2057void jbd2_journal_release_jbd_inode(journal_t *journal,
2058 struct jbd2_inode *jinode)
2059{
2060 int writeout = 0;
2061
2062 if (!journal)
2063 return;
2064restart:
2065 spin_lock(&journal->j_list_lock);
2066 /* Is commit writing out inode - we have to wait */
2067 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2068 wait_queue_head_t *wq;
2069 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2070 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2071 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2072 spin_unlock(&journal->j_list_lock);
2073 schedule();
2074 finish_wait(wq, &wait.wait);
2075 goto restart;
2076 }
2077
2078 /* Do we need to wait for data writeback? */
2079 if (journal->j_committing_transaction == jinode->i_transaction)
2080 writeout = 1;
2081 if (jinode->i_transaction) {
2082 list_del(&jinode->i_list);
2083 jinode->i_transaction = NULL;
2084 }
2085 spin_unlock(&journal->j_list_lock);
2086}
2087
2088/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002089 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002090 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002091#ifdef CONFIG_JBD2_DEBUG
2092u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002093EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002094
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002095#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002096
Jose R. Santos6f38c742007-10-16 18:38:25 -04002097static struct dentry *jbd2_debugfs_dir;
2098static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002099
2100static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002101{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002102 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2103 if (jbd2_debugfs_dir)
2104 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2105 jbd2_debugfs_dir,
2106 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002107}
2108
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002109static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002110{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002111 debugfs_remove(jbd2_debug);
2112 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002113}
2114
2115#else
2116
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002117static void __init jbd2_create_debugfs_entry(void)
2118{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002119}
2120
2121static void __exit jbd2_remove_debugfs_entry(void)
2122{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002123}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002124
2125#endif
2126
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002127#ifdef CONFIG_PROC_FS
2128
2129#define JBD2_STATS_PROC_NAME "fs/jbd2"
2130
2131static void __init jbd2_create_jbd_stats_proc_entry(void)
2132{
2133 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2134}
2135
2136static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2137{
2138 if (proc_jbd2_stats)
2139 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2140}
2141
2142#else
2143
2144#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2145#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2146
2147#endif
2148
Christoph Lametere18b8902006-12-06 20:33:20 -08002149struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002150
2151static int __init journal_init_handle_cache(void)
2152{
Johann Lombardia920e942006-10-11 01:21:00 -07002153 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002154 sizeof(handle_t),
2155 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002156 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002157 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002158 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002159 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2160 return -ENOMEM;
2161 }
2162 return 0;
2163}
2164
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002165static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002166{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002167 if (jbd2_handle_cache)
2168 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002169}
2170
2171/*
2172 * Module startup and shutdown
2173 */
2174
2175static int __init journal_init_caches(void)
2176{
2177 int ret;
2178
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002179 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002180 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002181 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002182 if (ret == 0)
2183 ret = journal_init_handle_cache();
2184 return ret;
2185}
2186
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002187static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002188{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002189 jbd2_journal_destroy_revoke_caches();
2190 jbd2_journal_destroy_jbd2_journal_head_cache();
2191 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002192}
2193
2194static int __init journal_init(void)
2195{
2196 int ret;
2197
2198 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2199
2200 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002201 if (ret == 0) {
2202 jbd2_create_debugfs_entry();
2203 jbd2_create_jbd_stats_proc_entry();
2204 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002205 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002206 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002207 return ret;
2208}
2209
2210static void __exit journal_exit(void)
2211{
Jose R. Santose23291b2007-07-18 08:57:06 -04002212#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002213 int n = atomic_read(&nr_journal_heads);
2214 if (n)
2215 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2216#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002217 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002218 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002219 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002220}
2221
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002222/*
2223 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2224 * tracing infrastructure to map a dev_t to a device name.
2225 *
2226 * The caller should use rcu_read_lock() in order to make sure the
2227 * device name stays valid until its done with it. We use
2228 * rcu_read_lock() as well to make sure we're safe in case the caller
2229 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2230 * nested.
2231 */
2232struct devname_cache {
2233 struct rcu_head rcu;
2234 dev_t device;
2235 char devname[BDEVNAME_SIZE];
2236};
2237#define CACHE_SIZE_BITS 6
2238static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2239static DEFINE_SPINLOCK(devname_cache_lock);
2240
2241static void free_devcache(struct rcu_head *rcu)
2242{
2243 kfree(rcu);
2244}
2245
2246const char *jbd2_dev_to_name(dev_t device)
2247{
2248 int i = hash_32(device, CACHE_SIZE_BITS);
2249 char *ret;
2250 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002251 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002252
2253 rcu_read_lock();
2254 if (devcache[i] && devcache[i]->device == device) {
2255 ret = devcache[i]->devname;
2256 rcu_read_unlock();
2257 return ret;
2258 }
2259 rcu_read_unlock();
2260
Theodore Ts'ob5744802009-06-20 23:34:44 -04002261 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2262 if (!new_dev)
2263 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002264 spin_lock(&devname_cache_lock);
2265 if (devcache[i]) {
2266 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002267 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002268 ret = devcache[i]->devname;
2269 spin_unlock(&devname_cache_lock);
2270 return ret;
2271 }
2272 call_rcu(&devcache[i]->rcu, free_devcache);
2273 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002274 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002275 devcache[i]->device = device;
2276 bd = bdget(device);
2277 if (bd) {
2278 bdevname(bd, devcache[i]->devname);
2279 bdput(bd);
2280 } else
2281 __bdevname(device, devcache[i]->devname);
2282 ret = devcache[i]->devname;
2283 spin_unlock(&devname_cache_lock);
2284 return ret;
2285}
2286EXPORT_SYMBOL(jbd2_dev_to_name);
2287
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288MODULE_LICENSE("GPL");
2289module_init(journal_init);
2290module_exit(journal_exit);
2291