blob: 58144102bf253b2244fee8fe44ce1a7c02cfbc79 [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>
Dave Kleikamp470decc2006-10-11 01:20:57 -070041
42#include <asm/uaccess.h>
43#include <asm/page.h>
44
Mingming Caof7f4bcc2006-10-11 01:20:59 -070045EXPORT_SYMBOL(jbd2_journal_start);
46EXPORT_SYMBOL(jbd2_journal_restart);
47EXPORT_SYMBOL(jbd2_journal_extend);
48EXPORT_SYMBOL(jbd2_journal_stop);
49EXPORT_SYMBOL(jbd2_journal_lock_updates);
50EXPORT_SYMBOL(jbd2_journal_unlock_updates);
51EXPORT_SYMBOL(jbd2_journal_get_write_access);
52EXPORT_SYMBOL(jbd2_journal_get_create_access);
53EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070054EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070055EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
56EXPORT_SYMBOL(jbd2_journal_release_buffer);
57EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070058#if 0
59EXPORT_SYMBOL(journal_sync_buffer);
60#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070061EXPORT_SYMBOL(jbd2_journal_flush);
62EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070063
Mingming Caof7f4bcc2006-10-11 01:20:59 -070064EXPORT_SYMBOL(jbd2_journal_init_dev);
65EXPORT_SYMBOL(jbd2_journal_init_inode);
66EXPORT_SYMBOL(jbd2_journal_update_format);
67EXPORT_SYMBOL(jbd2_journal_check_used_features);
68EXPORT_SYMBOL(jbd2_journal_check_available_features);
69EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070070EXPORT_SYMBOL(jbd2_journal_load);
71EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070072EXPORT_SYMBOL(jbd2_journal_abort);
73EXPORT_SYMBOL(jbd2_journal_errno);
74EXPORT_SYMBOL(jbd2_journal_ack_err);
75EXPORT_SYMBOL(jbd2_journal_clear_err);
76EXPORT_SYMBOL(jbd2_log_wait_commit);
77EXPORT_SYMBOL(jbd2_journal_start_commit);
78EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
79EXPORT_SYMBOL(jbd2_journal_wipe);
80EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
81EXPORT_SYMBOL(jbd2_journal_invalidatepage);
82EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
83EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040084EXPORT_SYMBOL(jbd2_journal_file_inode);
85EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
86EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
87EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070088
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070091
92/*
93 * Helper function used to manage commit timeouts
94 */
95
96static void commit_timeout(unsigned long __data)
97{
98 struct task_struct * p = (struct task_struct *) __data;
99
100 wake_up_process(p);
101}
102
103/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700104 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700105 * journal.
106 *
107 * This kernel thread is responsible for two things:
108 *
109 * 1) COMMIT: Every so often we need to commit the current state of the
110 * filesystem to disk. The journal thread is responsible for writing
111 * all of the metadata buffers to disk.
112 *
113 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
114 * of the data in that part of the log has been rewritten elsewhere on
115 * the disk. Flushing these old buffers to reclaim space in the log is
116 * known as checkpointing, and this thread is responsible for that job.
117 */
118
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700119static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700120{
121 journal_t *journal = arg;
122 transaction_t *transaction;
123
124 /*
125 * Set up an interval timer which can be used to trigger a commit wakeup
126 * after the commit interval expires
127 */
128 setup_timer(&journal->j_commit_timer, commit_timeout,
129 (unsigned long)current);
130
131 /* Record that the journal thread is running */
132 journal->j_task = current;
133 wake_up(&journal->j_wait_done_commit);
134
Theodore Ts'o4a9bf992009-01-03 22:56:44 -0500135 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
136 "commit interval %ld seconds\n", current->pid,
137 journal->j_devname, journal->j_commit_interval / HZ);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700138
139 /*
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
222 t = kthread_run(kjournald2, journal, "kjournald2");
223 if (IS_ERR(t))
224 return PTR_ERR(t);
225
Al Viro1076d172008-03-29 03:07:18 +0000226 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700228}
229
230static void journal_kill_thread(journal_t *journal)
231{
232 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700233 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234
235 while (journal->j_task) {
236 wake_up(&journal->j_wait_commit);
237 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000238 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700239 spin_lock(&journal->j_state_lock);
240 }
241 spin_unlock(&journal->j_state_lock);
242}
243
244/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700245 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246 *
247 * Writes a metadata buffer to a given disk block. The actual IO is not
248 * performed but a new buffer_head is constructed which labels the data
249 * to be written with the correct destination disk block.
250 *
251 * Any magic-number escaping which needs to be done will cause a
252 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700253 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700254 * magic number is only written to the log for descripter blocks. In
255 * this case, we copy the data and replace the first word with 0, and we
256 * return a result code which indicates that this buffer needs to be
257 * marked as an escaped buffer in the corresponding log descriptor
258 * block. The missing word can then be restored when the block is read
259 * during recovery.
260 *
261 * If the source buffer has already been modified by a new transaction
262 * since we took the last commit snapshot, we use the frozen copy of
263 * that data for IO. If we end up using the existing buffer_head's data
264 * for the write, then we *have* to lock the buffer to prevent anyone
265 * else from using and possibly modifying it while the IO is in
266 * progress.
267 *
268 * The function returns a pointer to the buffer_heads to be used for IO.
269 *
270 * We assume that the journal has already been locked in this function.
271 *
272 * Return value:
273 * <0: Error
274 * >=0: Finished OK
275 *
276 * On success:
277 * Bit 0 set == escape performed on the data
278 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 */
280
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700281int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700282 struct journal_head *jh_in,
283 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700284 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700285{
286 int need_copy_out = 0;
287 int done_copy_out = 0;
288 int do_escape = 0;
289 char *mapped_data;
290 struct buffer_head *new_bh;
291 struct journal_head *new_jh;
292 struct page *new_page;
293 unsigned int new_offset;
294 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700295 struct jbd2_buffer_trigger_type *triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700296
297 /*
298 * The buffer really shouldn't be locked: only the current committing
299 * transaction is allowed to write it, so nobody else is allowed
300 * to do any IO.
301 *
302 * akpm: except if we're journalling data, and write() output is
303 * also part of a shared mapping, and another thread has
304 * decided to launch a writepage() against this buffer.
305 */
306 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
307
308 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
309
310 /*
311 * If a new transaction has already done a buffer copy-out, then
312 * we use that version of the data for the commit.
313 */
314 jbd_lock_bh_state(bh_in);
315repeat:
316 if (jh_in->b_frozen_data) {
317 done_copy_out = 1;
318 new_page = virt_to_page(jh_in->b_frozen_data);
319 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700320 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700321 } else {
322 new_page = jh2bh(jh_in)->b_page;
323 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700324 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325 }
326
327 mapped_data = kmap_atomic(new_page, KM_USER0);
328 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700329 * Fire any commit trigger. Do this before checking for escaping,
330 * as the trigger may modify the magic offset. If a copy-out
331 * happens afterwards, it will have the correct data in the buffer.
332 */
333 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
334 triggers);
335
336 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700337 * Check for escaping
338 */
339 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700340 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 need_copy_out = 1;
342 do_escape = 1;
343 }
344 kunmap_atomic(mapped_data, KM_USER0);
345
346 /*
347 * Do we need to do a data copy?
348 */
349 if (need_copy_out && !done_copy_out) {
350 char *tmp;
351
352 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400353 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700354 jbd_lock_bh_state(bh_in);
355 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400356 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700357 goto repeat;
358 }
359
360 jh_in->b_frozen_data = tmp;
361 mapped_data = kmap_atomic(new_page, KM_USER0);
362 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
363 kunmap_atomic(mapped_data, KM_USER0);
364
365 new_page = virt_to_page(tmp);
366 new_offset = offset_in_page(tmp);
367 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700368
369 /*
370 * This isn't strictly necessary, as we're using frozen
371 * data for the escaping, but it keeps consistency with
372 * b_frozen_data usage.
373 */
374 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700375 }
376
377 /*
378 * Did we need to do an escaping? Now we've done all the
379 * copying, we can finally do so.
380 */
381 if (do_escape) {
382 mapped_data = kmap_atomic(new_page, KM_USER0);
383 *((unsigned int *)(mapped_data + new_offset)) = 0;
384 kunmap_atomic(mapped_data, KM_USER0);
385 }
386
387 /* keep subsequent assertions sane */
388 new_bh->b_state = 0;
389 init_buffer(new_bh, NULL, NULL);
390 atomic_set(&new_bh->b_count, 1);
391 jbd_unlock_bh_state(bh_in);
392
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700393 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394
395 set_bh_page(new_bh, new_page, new_offset);
396 new_jh->b_transaction = NULL;
397 new_bh->b_size = jh2bh(jh_in)->b_size;
398 new_bh->b_bdev = transaction->t_journal->j_dev;
399 new_bh->b_blocknr = blocknr;
400 set_buffer_mapped(new_bh);
401 set_buffer_dirty(new_bh);
402
403 *jh_out = new_jh;
404
405 /*
406 * The to-be-written buffer needs to get moved to the io queue,
407 * and the original buffer whose contents we are shadowing or
408 * copying is moved to the transaction's shadow queue.
409 */
410 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700411 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700412 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700413 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700414
415 return do_escape | (done_copy_out << 1);
416}
417
418/*
419 * Allocation code for the journal file. Manage the space left in the
420 * journal, so that we can begin checkpointing when appropriate.
421 */
422
423/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700424 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700425 *
426 * Called with the journal already locked.
427 *
428 * Called under j_state_lock
429 */
430
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700431int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700432{
433 int left = journal->j_free;
434
435 assert_spin_locked(&journal->j_state_lock);
436
437 /*
438 * Be pessimistic here about the number of those free blocks which
439 * might be required for log descriptor control blocks.
440 */
441
442#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
443
444 left -= MIN_LOG_RESERVED_BLOCKS;
445
446 if (left <= 0)
447 return 0;
448 left -= (left >> 3);
449 return left;
450}
451
452/*
Jan Karac88ccea2009-02-10 11:27:46 -0500453 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700454 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700455int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456{
457 /*
458 * Are we already doing a recent enough commit?
459 */
460 if (!tid_geq(journal->j_commit_request, target)) {
461 /*
462 * We want a new commit: OK, mark the request and wakup the
463 * commit thread. We do _not_ do the commit ourselves.
464 */
465
466 journal->j_commit_request = target;
467 jbd_debug(1, "JBD: requesting commit %d/%d\n",
468 journal->j_commit_request,
469 journal->j_commit_sequence);
470 wake_up(&journal->j_wait_commit);
471 return 1;
472 }
473 return 0;
474}
475
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477{
478 int ret;
479
480 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700481 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700482 spin_unlock(&journal->j_state_lock);
483 return ret;
484}
485
486/*
487 * Force and wait upon a commit if the calling process is not within
488 * transaction. This is used for forcing out undo-protected data which contains
489 * bitmaps, when the fs is running out of space.
490 *
491 * We can only force the running transaction if we don't have an active handle;
492 * otherwise, we will deadlock.
493 *
494 * Returns true if a transaction was started.
495 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700496int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497{
498 transaction_t *transaction = NULL;
499 tid_t tid;
500
501 spin_lock(&journal->j_state_lock);
502 if (journal->j_running_transaction && !current->journal_info) {
503 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700504 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505 } else if (journal->j_committing_transaction)
506 transaction = journal->j_committing_transaction;
507
508 if (!transaction) {
509 spin_unlock(&journal->j_state_lock);
510 return 0; /* Nothing to retry */
511 }
512
513 tid = transaction->t_tid;
514 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700515 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516 return 1;
517}
518
519/*
520 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500521 * if a transaction is going to be committed (or is currently already
522 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700524int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700525{
526 int ret = 0;
527
528 spin_lock(&journal->j_state_lock);
529 if (journal->j_running_transaction) {
530 tid_t tid = journal->j_running_transaction->t_tid;
531
Jan Karac88ccea2009-02-10 11:27:46 -0500532 __jbd2_log_start_commit(journal, tid);
533 /* There's a running transaction and we've just made sure
534 * it's commit has been scheduled. */
535 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500537 ret = 1;
538 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700539 /*
540 * If ext3_write_super() recently started a commit, then we
541 * have to wait for completion of that transaction
542 */
Jan Karac88ccea2009-02-10 11:27:46 -0500543 if (ptid)
544 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700545 ret = 1;
546 }
547 spin_unlock(&journal->j_state_lock);
548 return ret;
549}
550
551/*
552 * Wait for a specified commit to complete.
553 * The caller may not hold the journal lock.
554 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700555int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700556{
557 int err = 0;
558
Jose R. Santose23291b2007-07-18 08:57:06 -0400559#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700560 spin_lock(&journal->j_state_lock);
561 if (!tid_geq(journal->j_commit_request, tid)) {
562 printk(KERN_EMERG
563 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400564 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700565 }
566 spin_unlock(&journal->j_state_lock);
567#endif
568 spin_lock(&journal->j_state_lock);
569 while (tid_gt(tid, journal->j_commit_sequence)) {
570 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
571 tid, journal->j_commit_sequence);
572 wake_up(&journal->j_wait_commit);
573 spin_unlock(&journal->j_state_lock);
574 wait_event(journal->j_wait_done_commit,
575 !tid_gt(tid, journal->j_commit_sequence));
576 spin_lock(&journal->j_state_lock);
577 }
578 spin_unlock(&journal->j_state_lock);
579
580 if (unlikely(is_journal_aborted(journal))) {
581 printk(KERN_EMERG "journal commit I/O error\n");
582 err = -EIO;
583 }
584 return err;
585}
586
587/*
588 * Log buffer allocation routines:
589 */
590
Mingming Cao18eba7a2006-10-11 01:21:13 -0700591int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592{
593 unsigned long blocknr;
594
595 spin_lock(&journal->j_state_lock);
596 J_ASSERT(journal->j_free > 1);
597
598 blocknr = journal->j_head;
599 journal->j_head++;
600 journal->j_free--;
601 if (journal->j_head == journal->j_last)
602 journal->j_head = journal->j_first;
603 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700604 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700605}
606
607/*
608 * Conversion of logical to physical block numbers for the journal
609 *
610 * On external journals the journal blocks are identity-mapped, so
611 * this is a no-op. If needed, we can use j_blk_offset - everything is
612 * ready.
613 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700614int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700615 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700616{
617 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700618 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619
620 if (journal->j_inode) {
621 ret = bmap(journal->j_inode, blocknr);
622 if (ret)
623 *retp = ret;
624 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625 printk(KERN_ALERT "%s: journal block not found "
626 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400627 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 err = -EIO;
629 __journal_abort_soft(journal, err);
630 }
631 } else {
632 *retp = blocknr; /* +journal->j_blk_offset */
633 }
634 return err;
635}
636
637/*
638 * We play buffer_head aliasing tricks to write data/metadata blocks to
639 * the journal without copying their contents, but for journal
640 * descriptor blocks we do need to generate bona fide buffers.
641 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700642 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700643 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
644 * But we don't bother doing that, so there will be coherency problems with
645 * mmaps of blockdevs which hold live JBD-controlled filesystems.
646 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700647struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700648{
649 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700650 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700651 int err;
652
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700653 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654
655 if (err)
656 return NULL;
657
658 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500659 if (!bh)
660 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700661 lock_buffer(bh);
662 memset(bh->b_data, 0, journal->j_blocksize);
663 set_buffer_uptodate(bh);
664 unlock_buffer(bh);
665 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700666 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700667}
668
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500669struct jbd2_stats_proc_session {
670 journal_t *journal;
671 struct transaction_stats_s *stats;
672 int start;
673 int max;
674};
675
676static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
677 struct transaction_stats_s *ts,
678 int first)
679{
680 if (ts == s->stats + s->max)
681 ts = s->stats;
682 if (!first && ts == s->stats + s->start)
683 return NULL;
684 while (ts->ts_type == 0) {
685 ts++;
686 if (ts == s->stats + s->max)
687 ts = s->stats;
688 if (ts == s->stats + s->start)
689 return NULL;
690 }
691 return ts;
692
693}
694
695static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
696{
697 struct jbd2_stats_proc_session *s = seq->private;
698 struct transaction_stats_s *ts;
699 int l = *pos;
700
701 if (l == 0)
702 return SEQ_START_TOKEN;
703 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
704 if (!ts)
705 return NULL;
706 l--;
707 while (l) {
708 ts = jbd2_history_skip_empty(s, ++ts, 0);
709 if (!ts)
710 break;
711 l--;
712 }
713 return ts;
714}
715
716static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
717{
718 struct jbd2_stats_proc_session *s = seq->private;
719 struct transaction_stats_s *ts = v;
720
721 ++*pos;
722 if (v == SEQ_START_TOKEN)
723 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
724 else
725 return jbd2_history_skip_empty(s, ++ts, 0);
726}
727
728static int jbd2_seq_history_show(struct seq_file *seq, void *v)
729{
730 struct transaction_stats_s *ts = v;
731 if (v == SEQ_START_TOKEN) {
732 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
733 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
734 "wait", "run", "lock", "flush", "log", "hndls",
735 "block", "inlog", "ctime", "write", "drop",
736 "close");
737 return 0;
738 }
739 if (ts->ts_type == JBD2_STATS_RUN)
740 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
741 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
742 jiffies_to_msecs(ts->u.run.rs_wait),
743 jiffies_to_msecs(ts->u.run.rs_running),
744 jiffies_to_msecs(ts->u.run.rs_locked),
745 jiffies_to_msecs(ts->u.run.rs_flushing),
746 jiffies_to_msecs(ts->u.run.rs_logging),
747 ts->u.run.rs_handle_count,
748 ts->u.run.rs_blocks,
749 ts->u.run.rs_blocks_logged);
750 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
751 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
752 "C", ts->ts_tid, " ",
753 jiffies_to_msecs(ts->u.chp.cs_chp_time),
754 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
755 ts->u.chp.cs_forced_to_close);
756 else
757 J_ASSERT(0);
758 return 0;
759}
760
761static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
762{
763}
764
765static struct seq_operations jbd2_seq_history_ops = {
766 .start = jbd2_seq_history_start,
767 .next = jbd2_seq_history_next,
768 .stop = jbd2_seq_history_stop,
769 .show = jbd2_seq_history_show,
770};
771
772static int jbd2_seq_history_open(struct inode *inode, struct file *file)
773{
774 journal_t *journal = PDE(inode)->data;
775 struct jbd2_stats_proc_session *s;
776 int rc, size;
777
778 s = kmalloc(sizeof(*s), GFP_KERNEL);
779 if (s == NULL)
780 return -ENOMEM;
781 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
782 s->stats = kmalloc(size, GFP_KERNEL);
783 if (s->stats == NULL) {
784 kfree(s);
785 return -ENOMEM;
786 }
787 spin_lock(&journal->j_history_lock);
788 memcpy(s->stats, journal->j_history, size);
789 s->max = journal->j_history_max;
790 s->start = journal->j_history_cur % s->max;
791 spin_unlock(&journal->j_history_lock);
792
793 rc = seq_open(file, &jbd2_seq_history_ops);
794 if (rc == 0) {
795 struct seq_file *m = file->private_data;
796 m->private = s;
797 } else {
798 kfree(s->stats);
799 kfree(s);
800 }
801 return rc;
802
803}
804
805static int jbd2_seq_history_release(struct inode *inode, struct file *file)
806{
807 struct seq_file *seq = file->private_data;
808 struct jbd2_stats_proc_session *s = seq->private;
809
810 kfree(s->stats);
811 kfree(s);
812 return seq_release(inode, file);
813}
814
815static struct file_operations jbd2_seq_history_fops = {
816 .owner = THIS_MODULE,
817 .open = jbd2_seq_history_open,
818 .read = seq_read,
819 .llseek = seq_lseek,
820 .release = jbd2_seq_history_release,
821};
822
823static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
824{
825 return *pos ? NULL : SEQ_START_TOKEN;
826}
827
828static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
829{
830 return NULL;
831}
832
833static int jbd2_seq_info_show(struct seq_file *seq, void *v)
834{
835 struct jbd2_stats_proc_session *s = seq->private;
836
837 if (v != SEQ_START_TOKEN)
838 return 0;
839 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
840 s->stats->ts_tid,
841 s->journal->j_max_transaction_buffers);
842 if (s->stats->ts_tid == 0)
843 return 0;
844 seq_printf(seq, "average: \n %ums waiting for transaction\n",
845 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
846 seq_printf(seq, " %ums running transaction\n",
847 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
848 seq_printf(seq, " %ums transaction was being locked\n",
849 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
850 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
851 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
852 seq_printf(seq, " %ums logging transaction\n",
853 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500854 seq_printf(seq, " %lluus average transaction commit time\n",
855 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500856 seq_printf(seq, " %lu handles per transaction\n",
857 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
858 seq_printf(seq, " %lu blocks per transaction\n",
859 s->stats->u.run.rs_blocks / s->stats->ts_tid);
860 seq_printf(seq, " %lu logged blocks per transaction\n",
861 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
862 return 0;
863}
864
865static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
866{
867}
868
869static struct seq_operations jbd2_seq_info_ops = {
870 .start = jbd2_seq_info_start,
871 .next = jbd2_seq_info_next,
872 .stop = jbd2_seq_info_stop,
873 .show = jbd2_seq_info_show,
874};
875
876static int jbd2_seq_info_open(struct inode *inode, struct file *file)
877{
878 journal_t *journal = PDE(inode)->data;
879 struct jbd2_stats_proc_session *s;
880 int rc, size;
881
882 s = kmalloc(sizeof(*s), GFP_KERNEL);
883 if (s == NULL)
884 return -ENOMEM;
885 size = sizeof(struct transaction_stats_s);
886 s->stats = kmalloc(size, GFP_KERNEL);
887 if (s->stats == NULL) {
888 kfree(s);
889 return -ENOMEM;
890 }
891 spin_lock(&journal->j_history_lock);
892 memcpy(s->stats, &journal->j_stats, size);
893 s->journal = journal;
894 spin_unlock(&journal->j_history_lock);
895
896 rc = seq_open(file, &jbd2_seq_info_ops);
897 if (rc == 0) {
898 struct seq_file *m = file->private_data;
899 m->private = s;
900 } else {
901 kfree(s->stats);
902 kfree(s);
903 }
904 return rc;
905
906}
907
908static int jbd2_seq_info_release(struct inode *inode, struct file *file)
909{
910 struct seq_file *seq = file->private_data;
911 struct jbd2_stats_proc_session *s = seq->private;
912 kfree(s->stats);
913 kfree(s);
914 return seq_release(inode, file);
915}
916
917static struct file_operations jbd2_seq_info_fops = {
918 .owner = THIS_MODULE,
919 .open = jbd2_seq_info_open,
920 .read = seq_read,
921 .llseek = seq_lseek,
922 .release = jbd2_seq_info_release,
923};
924
925static struct proc_dir_entry *proc_jbd2_stats;
926
927static void jbd2_stats_proc_init(journal_t *journal)
928{
Theodore Ts'o05496762008-09-16 14:36:17 -0400929 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500930 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700931 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
932 &jbd2_seq_history_fops, journal);
933 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
934 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500935 }
936}
937
938static void jbd2_stats_proc_exit(journal_t *journal)
939{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500940 remove_proc_entry("info", journal->j_proc_entry);
941 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400942 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500943}
944
945static void journal_init_stats(journal_t *journal)
946{
947 int size;
948
949 if (!proc_jbd2_stats)
950 return;
951
952 journal->j_history_max = 100;
953 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
954 journal->j_history = kzalloc(size, GFP_KERNEL);
955 if (!journal->j_history) {
956 journal->j_history_max = 0;
957 return;
958 }
959 spin_lock_init(&journal->j_history_lock);
960}
961
Dave Kleikamp470decc2006-10-11 01:20:57 -0700962/*
963 * Management for journal control blocks: functions to create and
964 * destroy journal_t structures, and to initialise and read existing
965 * journal blocks from disk. */
966
967/* First: create and setup a journal_t object in memory. We initialise
968 * very few fields yet: that has to wait until we have created the
969 * journal structures from from scratch, or loaded them from disk. */
970
971static journal_t * journal_init_common (void)
972{
973 journal_t *journal;
974 int err;
975
Mingming Caod802ffa2007-10-16 18:38:25 -0400976 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700977 if (!journal)
978 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700979
980 init_waitqueue_head(&journal->j_wait_transaction_locked);
981 init_waitqueue_head(&journal->j_wait_logspace);
982 init_waitqueue_head(&journal->j_wait_done_commit);
983 init_waitqueue_head(&journal->j_wait_checkpoint);
984 init_waitqueue_head(&journal->j_wait_commit);
985 init_waitqueue_head(&journal->j_wait_updates);
986 mutex_init(&journal->j_barrier);
987 mutex_init(&journal->j_checkpoint_mutex);
988 spin_lock_init(&journal->j_revoke_lock);
989 spin_lock_init(&journal->j_list_lock);
990 spin_lock_init(&journal->j_state_lock);
991
Mingming Caocd02ff02007-10-16 18:38:25 -0400992 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500993 journal->j_min_batch_time = 0;
994 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700995
996 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700997 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700998
999 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001000 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001001 if (err) {
1002 kfree(journal);
1003 goto fail;
1004 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001005
1006 journal_init_stats(journal);
1007
Dave Kleikamp470decc2006-10-11 01:20:57 -07001008 return journal;
1009fail:
1010 return NULL;
1011}
1012
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001013/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001014 *
1015 * Create a journal structure assigned some fixed set of disk blocks to
1016 * the journal. We don't actually touch those disk blocks yet, but we
1017 * need to set up all of the mapping information to tell the journaling
1018 * system where the journal blocks are.
1019 *
1020 */
1021
1022/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001023 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001024 * @bdev: Block device on which to create the journal
1025 * @fs_dev: Device which hold journalled filesystem for this journal.
1026 * @start: Block nr Start of journal.
1027 * @len: Length of the journal in blocks.
1028 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001029 *
1030 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001031 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001032 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001033 * range of blocks on an arbitrary block device.
1034 *
1035 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001036journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001037 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001038 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001039{
1040 journal_t *journal = journal_init_common();
1041 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001042 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043 int n;
1044
1045 if (!journal)
1046 return NULL;
1047
1048 /* journal descriptor can store up to n blocks -bzzz */
1049 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -05001050 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1052 journal->j_wbufsize = n;
1053 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1054 if (!journal->j_wbuf) {
1055 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001056 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001057 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001058 }
1059 journal->j_dev = bdev;
1060 journal->j_fs_dev = fs_dev;
1061 journal->j_blk_offset = start;
1062 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001063 bdevname(journal->j_dev, journal->j_devname);
1064 p = journal->j_devname;
1065 while ((p = strchr(p, '/')))
1066 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -07001067
1068 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001069 if (!bh) {
1070 printk(KERN_ERR
1071 "%s: Cannot get buffer for journal superblock\n",
1072 __func__);
1073 goto out_err;
1074 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001075 journal->j_sb_buffer = bh;
1076 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001077
Dave Kleikamp470decc2006-10-11 01:20:57 -07001078 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001079out_err:
1080 jbd2_stats_proc_exit(journal);
1081 kfree(journal);
1082 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001083}
1084
1085/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001086 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001087 * @inode: An inode to create the journal in
1088 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001089 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 * the journal. The inode must exist already, must support bmap() and
1091 * must have all data blocks preallocated.
1092 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001093journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094{
1095 struct buffer_head *bh;
1096 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001097 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098 int err;
1099 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001100 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101
1102 if (!journal)
1103 return NULL;
1104
1105 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1106 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001107 bdevname(journal->j_dev, journal->j_devname);
1108 p = journal->j_devname;
1109 while ((p = strchr(p, '/')))
1110 *p = '!';
1111 p = journal->j_devname + strlen(journal->j_devname);
1112 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113 jbd_debug(1,
1114 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1115 journal, inode->i_sb->s_id, inode->i_ino,
1116 (long long) inode->i_size,
1117 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1118
1119 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1120 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001121 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001122
1123 /* journal descriptor can store up to n blocks -bzzz */
1124 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1125 journal->j_wbufsize = n;
1126 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1127 if (!journal->j_wbuf) {
1128 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001129 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001130 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131 }
1132
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001133 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001134 /* If that failed, give up */
1135 if (err) {
1136 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001137 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001138 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001139 }
1140
1141 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001142 if (!bh) {
1143 printk(KERN_ERR
1144 "%s: Cannot get buffer for journal superblock\n",
1145 __func__);
1146 goto out_err;
1147 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001148 journal->j_sb_buffer = bh;
1149 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1150
1151 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001152out_err:
1153 jbd2_stats_proc_exit(journal);
1154 kfree(journal);
1155 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001156}
1157
1158/*
1159 * If the journal init or create aborts, we need to mark the journal
1160 * superblock as being NULL to prevent the journal destroy from writing
1161 * back a bogus superblock.
1162 */
1163static void journal_fail_superblock (journal_t *journal)
1164{
1165 struct buffer_head *bh = journal->j_sb_buffer;
1166 brelse(bh);
1167 journal->j_sb_buffer = NULL;
1168}
1169
1170/*
1171 * Given a journal_t structure, initialise the various fields for
1172 * startup of a new journaling session. We use this both when creating
1173 * a journal, and after recovering an old journal to reset it for
1174 * subsequent use.
1175 */
1176
1177static int journal_reset(journal_t *journal)
1178{
1179 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001180 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181
1182 first = be32_to_cpu(sb->s_first);
1183 last = be32_to_cpu(sb->s_maxlen);
1184
1185 journal->j_first = first;
1186 journal->j_last = last;
1187
1188 journal->j_head = first;
1189 journal->j_tail = first;
1190 journal->j_free = last - first;
1191
1192 journal->j_tail_sequence = journal->j_transaction_sequence;
1193 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1194 journal->j_commit_request = journal->j_commit_sequence;
1195
1196 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1197
1198 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001199 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001200 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001201}
1202
1203/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001204 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001205 * @journal: The journal to update.
1206 * @wait: Set to '0' if you don't want to wait for IO completion.
1207 *
1208 * Update a journal's dynamic superblock fields and write it to disk,
1209 * optionally waiting for the IO to complete.
1210 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001211void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001212{
1213 journal_superblock_t *sb = journal->j_superblock;
1214 struct buffer_head *bh = journal->j_sb_buffer;
1215
1216 /*
1217 * As a special case, if the on-disk copy is already marked as needing
1218 * no recovery (s_start == 0) and there are no outstanding transactions
1219 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001220 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001221 * attempting a write to a potential-readonly device.
1222 */
1223 if (sb->s_start == 0 && journal->j_tail_sequence ==
1224 journal->j_transaction_sequence) {
1225 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1226 "(start %ld, seq %d, errno %d)\n",
1227 journal->j_tail, journal->j_tail_sequence,
1228 journal->j_errno);
1229 goto out;
1230 }
1231
Theodore Ts'o914258b2008-10-06 21:35:40 -04001232 if (buffer_write_io_error(bh)) {
1233 /*
1234 * Oh, dear. A previous attempt to write the journal
1235 * superblock failed. This could happen because the
1236 * USB device was yanked out. Or it could happen to
1237 * be a transient write error and maybe the block will
1238 * be remapped. Nothing we can do but to retry the
1239 * write and hope for the best.
1240 */
1241 printk(KERN_ERR "JBD2: previous I/O error detected "
1242 "for journal superblock update for %s.\n",
1243 journal->j_devname);
1244 clear_buffer_write_io_error(bh);
1245 set_buffer_uptodate(bh);
1246 }
1247
Dave Kleikamp470decc2006-10-11 01:20:57 -07001248 spin_lock(&journal->j_state_lock);
1249 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1250 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1251
1252 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1253 sb->s_start = cpu_to_be32(journal->j_tail);
1254 sb->s_errno = cpu_to_be32(journal->j_errno);
1255 spin_unlock(&journal->j_state_lock);
1256
1257 BUFFER_TRACE(bh, "marking dirty");
1258 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001259 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001260 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001261 if (buffer_write_io_error(bh)) {
1262 printk(KERN_ERR "JBD2: I/O error detected "
1263 "when updating journal superblock for %s.\n",
1264 journal->j_devname);
1265 clear_buffer_write_io_error(bh);
1266 set_buffer_uptodate(bh);
1267 }
1268 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001269 ll_rw_block(SWRITE, 1, &bh);
1270
1271out:
1272 /* If we have just flushed the log (by marking s_start==0), then
1273 * any future commit will have to be careful to update the
1274 * superblock again to re-record the true start of the log. */
1275
1276 spin_lock(&journal->j_state_lock);
1277 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001278 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001279 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001280 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001281 spin_unlock(&journal->j_state_lock);
1282}
1283
1284/*
1285 * Read the superblock for a given journal, performing initial
1286 * validation of the format.
1287 */
1288
1289static int journal_get_superblock(journal_t *journal)
1290{
1291 struct buffer_head *bh;
1292 journal_superblock_t *sb;
1293 int err = -EIO;
1294
1295 bh = journal->j_sb_buffer;
1296
1297 J_ASSERT(bh != NULL);
1298 if (!buffer_uptodate(bh)) {
1299 ll_rw_block(READ, 1, &bh);
1300 wait_on_buffer(bh);
1301 if (!buffer_uptodate(bh)) {
1302 printk (KERN_ERR
1303 "JBD: IO error reading journal superblock\n");
1304 goto out;
1305 }
1306 }
1307
1308 sb = journal->j_superblock;
1309
1310 err = -EINVAL;
1311
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001312 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001313 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1314 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1315 goto out;
1316 }
1317
1318 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001319 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001320 journal->j_format_version = 1;
1321 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001322 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001323 journal->j_format_version = 2;
1324 break;
1325 default:
1326 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1327 goto out;
1328 }
1329
1330 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1331 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1332 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1333 printk (KERN_WARNING "JBD: journal file too short\n");
1334 goto out;
1335 }
1336
1337 return 0;
1338
1339out:
1340 journal_fail_superblock(journal);
1341 return err;
1342}
1343
1344/*
1345 * Load the on-disk journal superblock and read the key fields into the
1346 * journal_t.
1347 */
1348
1349static int load_superblock(journal_t *journal)
1350{
1351 int err;
1352 journal_superblock_t *sb;
1353
1354 err = journal_get_superblock(journal);
1355 if (err)
1356 return err;
1357
1358 sb = journal->j_superblock;
1359
1360 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1361 journal->j_tail = be32_to_cpu(sb->s_start);
1362 journal->j_first = be32_to_cpu(sb->s_first);
1363 journal->j_last = be32_to_cpu(sb->s_maxlen);
1364 journal->j_errno = be32_to_cpu(sb->s_errno);
1365
1366 return 0;
1367}
1368
1369
1370/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001371 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001372 * @journal: Journal to act on.
1373 *
1374 * Given a journal_t structure which tells us which disk blocks contain
1375 * a journal, read the journal from disk to initialise the in-memory
1376 * structures.
1377 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001378int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001379{
1380 int err;
1381 journal_superblock_t *sb;
1382
1383 err = load_superblock(journal);
1384 if (err)
1385 return err;
1386
1387 sb = journal->j_superblock;
1388 /* If this is a V2 superblock, then we have to check the
1389 * features flags on it. */
1390
1391 if (journal->j_format_version >= 2) {
1392 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001393 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001394 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001395 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001396 printk (KERN_WARNING
1397 "JBD: Unrecognised features on journal\n");
1398 return -EINVAL;
1399 }
1400 }
1401
Dave Kleikamp470decc2006-10-11 01:20:57 -07001402 /* Let the recovery code check whether it needs to recover any
1403 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001404 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 goto recovery_error;
1406
1407 /* OK, we've finished with the dynamic journal bits:
1408 * reinitialise the dynamic contents of the superblock in memory
1409 * and reset them on disk. */
1410 if (journal_reset(journal))
1411 goto recovery_error;
1412
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001413 journal->j_flags &= ~JBD2_ABORT;
1414 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001415 return 0;
1416
1417recovery_error:
1418 printk (KERN_WARNING "JBD: recovery failed\n");
1419 return -EIO;
1420}
1421
1422/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001423 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001424 * @journal: Journal to act on.
1425 *
1426 * Release a journal_t structure once it is no longer in use by the
1427 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001428 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001429 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001430int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001431{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001432 int err = 0;
1433
Dave Kleikamp470decc2006-10-11 01:20:57 -07001434 /* Wait for the commit thread to wake up and die. */
1435 journal_kill_thread(journal);
1436
1437 /* Force a final log commit */
1438 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001439 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440
1441 /* Force any old transactions to disk */
1442
1443 /* Totally anal locking here... */
1444 spin_lock(&journal->j_list_lock);
1445 while (journal->j_checkpoint_transactions != NULL) {
1446 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001447 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001448 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001449 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001450 spin_lock(&journal->j_list_lock);
1451 }
1452
1453 J_ASSERT(journal->j_running_transaction == NULL);
1454 J_ASSERT(journal->j_committing_transaction == NULL);
1455 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1456 spin_unlock(&journal->j_list_lock);
1457
Dave Kleikamp470decc2006-10-11 01:20:57 -07001458 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001459 if (!is_journal_aborted(journal)) {
1460 /* We can now mark the journal as empty. */
1461 journal->j_tail = 0;
1462 journal->j_tail_sequence =
1463 ++journal->j_transaction_sequence;
1464 jbd2_journal_update_superblock(journal, 1);
1465 } else {
1466 err = -EIO;
1467 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001468 brelse(journal->j_sb_buffer);
1469 }
1470
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001471 if (journal->j_proc_entry)
1472 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001473 if (journal->j_inode)
1474 iput(journal->j_inode);
1475 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001476 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 kfree(journal->j_wbuf);
1478 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001479
1480 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481}
1482
1483
1484/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001485 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001486 * @journal: Journal to check.
1487 * @compat: bitmask of compatible features
1488 * @ro: bitmask of features that force read-only mount
1489 * @incompat: bitmask of incompatible features
1490 *
1491 * Check whether the journal uses all of a given set of
1492 * features. Return true (non-zero) if it does.
1493 **/
1494
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001495int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001496 unsigned long ro, unsigned long incompat)
1497{
1498 journal_superblock_t *sb;
1499
1500 if (!compat && !ro && !incompat)
1501 return 1;
1502 if (journal->j_format_version == 1)
1503 return 0;
1504
1505 sb = journal->j_superblock;
1506
1507 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1508 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1509 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1510 return 1;
1511
1512 return 0;
1513}
1514
1515/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001516 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001517 * @journal: Journal to check.
1518 * @compat: bitmask of compatible features
1519 * @ro: bitmask of features that force read-only mount
1520 * @incompat: bitmask of incompatible features
1521 *
1522 * Check whether the journaling code supports the use of
1523 * all of a given set of features on this journal. Return true
1524 * (non-zero) if it can. */
1525
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001526int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001527 unsigned long ro, unsigned long incompat)
1528{
1529 journal_superblock_t *sb;
1530
1531 if (!compat && !ro && !incompat)
1532 return 1;
1533
1534 sb = journal->j_superblock;
1535
1536 /* We can support any known requested features iff the
1537 * superblock is in version 2. Otherwise we fail to support any
1538 * extended sb features. */
1539
1540 if (journal->j_format_version != 2)
1541 return 0;
1542
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001543 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1544 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1545 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001546 return 1;
1547
1548 return 0;
1549}
1550
1551/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001552 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001553 * @journal: Journal to act on.
1554 * @compat: bitmask of compatible features
1555 * @ro: bitmask of features that force read-only mount
1556 * @incompat: bitmask of incompatible features
1557 *
1558 * Mark a given journal feature as present on the
1559 * superblock. Returns true if the requested features could be set.
1560 *
1561 */
1562
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001563int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 unsigned long ro, unsigned long incompat)
1565{
1566 journal_superblock_t *sb;
1567
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001568 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001569 return 1;
1570
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001571 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572 return 0;
1573
1574 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1575 compat, ro, incompat);
1576
1577 sb = journal->j_superblock;
1578
1579 sb->s_feature_compat |= cpu_to_be32(compat);
1580 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1581 sb->s_feature_incompat |= cpu_to_be32(incompat);
1582
1583 return 1;
1584}
1585
Girish Shilamkar818d2762008-01-28 23:58:27 -05001586/*
1587 * jbd2_journal_clear_features () - Clear a given journal feature in the
1588 * superblock
1589 * @journal: Journal to act on.
1590 * @compat: bitmask of compatible features
1591 * @ro: bitmask of features that force read-only mount
1592 * @incompat: bitmask of incompatible features
1593 *
1594 * Clear a given journal feature as present on the
1595 * superblock.
1596 */
1597void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1598 unsigned long ro, unsigned long incompat)
1599{
1600 journal_superblock_t *sb;
1601
1602 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1603 compat, ro, incompat);
1604
1605 sb = journal->j_superblock;
1606
1607 sb->s_feature_compat &= ~cpu_to_be32(compat);
1608 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1609 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1610}
1611EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001612
1613/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001614 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001615 * @journal: Journal to act on.
1616 *
1617 * Given an initialised but unloaded journal struct, poke about in the
1618 * on-disk structure to update it to the most recent supported version.
1619 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001620int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001621{
1622 journal_superblock_t *sb;
1623 int err;
1624
1625 err = journal_get_superblock(journal);
1626 if (err)
1627 return err;
1628
1629 sb = journal->j_superblock;
1630
1631 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001634 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001635 return journal_convert_superblock_v1(journal, sb);
1636 default:
1637 break;
1638 }
1639 return -EINVAL;
1640}
1641
1642static int journal_convert_superblock_v1(journal_t *journal,
1643 journal_superblock_t *sb)
1644{
1645 int offset, blocksize;
1646 struct buffer_head *bh;
1647
1648 printk(KERN_WARNING
1649 "JBD: Converting superblock from version 1 to 2.\n");
1650
1651 /* Pre-initialise new fields to zero */
1652 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1653 blocksize = be32_to_cpu(sb->s_blocksize);
1654 memset(&sb->s_feature_compat, 0, blocksize-offset);
1655
1656 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001657 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 journal->j_format_version = 2;
1659
1660 bh = journal->j_sb_buffer;
1661 BUFFER_TRACE(bh, "marking dirty");
1662 mark_buffer_dirty(bh);
1663 sync_dirty_buffer(bh);
1664 return 0;
1665}
1666
1667
1668/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001669 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670 * @journal: Journal to act on.
1671 *
1672 * Flush all data for a given journal to disk and empty the journal.
1673 * Filesystems can use this when remounting readonly to ensure that
1674 * recovery does not need to happen on remount.
1675 */
1676
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001677int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001678{
1679 int err = 0;
1680 transaction_t *transaction = NULL;
1681 unsigned long old_tail;
1682
1683 spin_lock(&journal->j_state_lock);
1684
1685 /* Force everything buffered to the log... */
1686 if (journal->j_running_transaction) {
1687 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001688 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001689 } else if (journal->j_committing_transaction)
1690 transaction = journal->j_committing_transaction;
1691
1692 /* Wait for the log commit to complete... */
1693 if (transaction) {
1694 tid_t tid = transaction->t_tid;
1695
1696 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001697 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001698 } else {
1699 spin_unlock(&journal->j_state_lock);
1700 }
1701
1702 /* ...and flush everything in the log out to disk. */
1703 spin_lock(&journal->j_list_lock);
1704 while (!err && journal->j_checkpoint_transactions != NULL) {
1705 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001706 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001707 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001708 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 spin_lock(&journal->j_list_lock);
1710 }
1711 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001712
1713 if (is_journal_aborted(journal))
1714 return -EIO;
1715
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001716 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001717
1718 /* Finally, mark the journal as really needing no recovery.
1719 * This sets s_start==0 in the underlying superblock, which is
1720 * the magic code for a fully-recovered superblock. Any future
1721 * commits of data to the journal will restore the current
1722 * s_start value. */
1723 spin_lock(&journal->j_state_lock);
1724 old_tail = journal->j_tail;
1725 journal->j_tail = 0;
1726 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001727 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001728 spin_lock(&journal->j_state_lock);
1729 journal->j_tail = old_tail;
1730
1731 J_ASSERT(!journal->j_running_transaction);
1732 J_ASSERT(!journal->j_committing_transaction);
1733 J_ASSERT(!journal->j_checkpoint_transactions);
1734 J_ASSERT(journal->j_head == journal->j_tail);
1735 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1736 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001737 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001738}
1739
1740/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001741 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001742 * @journal: Journal to act on.
1743 * @write: flag (see below)
1744 *
1745 * Wipe out all of the contents of a journal, safely. This will produce
1746 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001747 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001748 *
1749 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1750 * we merely suppress recovery.
1751 */
1752
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001753int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754{
1755 journal_superblock_t *sb;
1756 int err = 0;
1757
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001758 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001759
1760 err = load_superblock(journal);
1761 if (err)
1762 return err;
1763
1764 sb = journal->j_superblock;
1765
1766 if (!journal->j_tail)
1767 goto no_recovery;
1768
1769 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1770 write ? "Clearing" : "Ignoring");
1771
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001772 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001773 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001774 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001775
1776 no_recovery:
1777 return err;
1778}
1779
1780/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001781 * Journal abort has very specific semantics, which we describe
1782 * for journal abort.
1783 *
1784 * Two internal function, which provide abort to te jbd layer
1785 * itself are here.
1786 */
1787
1788/*
1789 * Quick version for internal journal use (doesn't lock the journal).
1790 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1791 * and don't attempt to make any other journal updates.
1792 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001793void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001794{
1795 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001797 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 return;
1799
1800 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001801 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802
1803 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001804 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805 transaction = journal->j_running_transaction;
1806 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001807 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808 spin_unlock(&journal->j_state_lock);
1809}
1810
1811/* Soft abort: record the abort error status in the journal superblock,
1812 * but don't do any other IO. */
1813static void __journal_abort_soft (journal_t *journal, int errno)
1814{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001815 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001816 return;
1817
1818 if (!journal->j_errno)
1819 journal->j_errno = errno;
1820
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001821 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822
1823 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001824 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001825}
1826
1827/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001828 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001829 * @journal: the journal to shutdown.
1830 * @errno: an error number to record in the journal indicating
1831 * the reason for the shutdown.
1832 *
1833 * Perform a complete, immediate shutdown of the ENTIRE
1834 * journal (not of a single transaction). This operation cannot be
1835 * undone without closing and reopening the journal.
1836 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001837 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001838 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1839 * mode.
1840 *
1841 * Journal abort has very specific semantics. Any existing dirty,
1842 * unjournaled buffers in the main filesystem will still be written to
1843 * disk by bdflush, but the journaling mechanism will be suspended
1844 * immediately and no further transaction commits will be honoured.
1845 *
1846 * Any dirty, journaled buffers will be written back to disk without
1847 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1848 * filesystem, but we _do_ attempt to leave as much data as possible
1849 * behind for fsck to use for cleanup.
1850 *
1851 * Any attempt to get a new transaction handle on a journal which is in
1852 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001853 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001854 * entered abort state during the update.
1855 *
1856 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001857 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001859 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001860 * which will be recorded (if possible) in the journal superblock. This
1861 * allows a client to record failure conditions in the middle of a
1862 * transaction without having to complete the transaction to record the
1863 * failure to disk. ext3_error, for example, now uses this
1864 * functionality.
1865 *
1866 * Errors which originate from within the journaling layer will NOT
1867 * supply an errno; a null errno implies that absolutely no further
1868 * writes are done to the journal (unless there are any already in
1869 * progress).
1870 *
1871 */
1872
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001873void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001874{
1875 __journal_abort_soft(journal, errno);
1876}
1877
1878/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001879 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880 * @journal: journal to examine.
1881 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001882 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883 * time the journal was mounted - if the journal was stopped
1884 * without calling abort this will be 0.
1885 *
1886 * If the journal has been aborted on this mount time -EROFS will
1887 * be returned.
1888 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001889int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890{
1891 int err;
1892
1893 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001894 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001895 err = -EROFS;
1896 else
1897 err = journal->j_errno;
1898 spin_unlock(&journal->j_state_lock);
1899 return err;
1900}
1901
1902/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001903 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001904 * @journal: journal to act on.
1905 *
1906 * An error must be cleared or Acked to take a FS out of readonly
1907 * mode.
1908 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910{
1911 int err = 0;
1912
1913 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001914 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001915 err = -EROFS;
1916 else
1917 journal->j_errno = 0;
1918 spin_unlock(&journal->j_state_lock);
1919 return err;
1920}
1921
1922/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001923 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001924 * @journal: journal to act on.
1925 *
1926 * An error must be cleared or Acked to take a FS out of readonly
1927 * mode.
1928 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001929void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001930{
1931 spin_lock(&journal->j_state_lock);
1932 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001933 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934 spin_unlock(&journal->j_state_lock);
1935}
1936
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001937int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001938{
1939 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1940}
1941
1942/*
Zach Brownb517bea2006-10-11 01:21:08 -07001943 * helper functions to deal with 32 or 64bit block numbers.
1944 */
1945size_t journal_tag_bytes(journal_t *journal)
1946{
1947 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001948 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001949 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001950 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001951}
1952
1953/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001954 * Journal_head storage management
1955 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001956static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001957#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001958static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1959#endif
1960
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001961static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962{
1963 int retval;
1964
Al Viro1076d172008-03-29 03:07:18 +00001965 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001966 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 sizeof(struct journal_head),
1968 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001969 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001970 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001972 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973 retval = -ENOMEM;
1974 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1975 }
1976 return retval;
1977}
1978
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001979static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001980{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001981 if (jbd2_journal_head_cache) {
1982 kmem_cache_destroy(jbd2_journal_head_cache);
1983 jbd2_journal_head_cache = NULL;
1984 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001985}
1986
1987/*
1988 * journal_head splicing and dicing
1989 */
1990static struct journal_head *journal_alloc_journal_head(void)
1991{
1992 struct journal_head *ret;
1993 static unsigned long last_warning;
1994
Jose R. Santose23291b2007-07-18 08:57:06 -04001995#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001996 atomic_inc(&nr_journal_heads);
1997#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001998 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001999 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000 jbd_debug(1, "out of memory for journal_head\n");
2001 if (time_after(jiffies, last_warning + 5*HZ)) {
2002 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002003 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 last_warning = jiffies;
2005 }
Al Viro1076d172008-03-29 03:07:18 +00002006 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002008 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002009 }
2010 }
2011 return ret;
2012}
2013
2014static void journal_free_journal_head(struct journal_head *jh)
2015{
Jose R. Santose23291b2007-07-18 08:57:06 -04002016#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002017 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002018 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002019#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002020 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021}
2022
2023/*
2024 * A journal_head is attached to a buffer_head whenever JBD has an
2025 * interest in the buffer.
2026 *
2027 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2028 * is set. This bit is tested in core kernel code where we need to take
2029 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2030 * there.
2031 *
2032 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2033 *
2034 * When a buffer has its BH_JBD bit set it is immune from being released by
2035 * core kernel code, mainly via ->b_count.
2036 *
2037 * A journal_head may be detached from its buffer_head when the journal_head's
2038 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002039 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002040 * journal_head can be dropped if needed.
2041 *
2042 * Various places in the kernel want to attach a journal_head to a buffer_head
2043 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002044 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002045 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002046 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002047 *
2048 * So the typical usage would be:
2049 *
2050 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 * ...
2053 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002054 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002055 *
2056 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2057 * because it has a non-zero b_transaction.
2058 */
2059
2060/*
2061 * Give a buffer_head a journal_head.
2062 *
2063 * Doesn't need the journal lock.
2064 * May sleep.
2065 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002066struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002067{
2068 struct journal_head *jh;
2069 struct journal_head *new_jh = NULL;
2070
2071repeat:
2072 if (!buffer_jbd(bh)) {
2073 new_jh = journal_alloc_journal_head();
2074 memset(new_jh, 0, sizeof(*new_jh));
2075 }
2076
2077 jbd_lock_bh_journal_head(bh);
2078 if (buffer_jbd(bh)) {
2079 jh = bh2jh(bh);
2080 } else {
2081 J_ASSERT_BH(bh,
2082 (atomic_read(&bh->b_count) > 0) ||
2083 (bh->b_page && bh->b_page->mapping));
2084
2085 if (!new_jh) {
2086 jbd_unlock_bh_journal_head(bh);
2087 goto repeat;
2088 }
2089
2090 jh = new_jh;
2091 new_jh = NULL; /* We consumed it */
2092 set_buffer_jbd(bh);
2093 bh->b_private = jh;
2094 jh->b_bh = bh;
2095 get_bh(bh);
2096 BUFFER_TRACE(bh, "added journal_head");
2097 }
2098 jh->b_jcount++;
2099 jbd_unlock_bh_journal_head(bh);
2100 if (new_jh)
2101 journal_free_journal_head(new_jh);
2102 return bh->b_private;
2103}
2104
2105/*
2106 * Grab a ref against this buffer_head's journal_head. If it ended up not
2107 * having a journal_head, return NULL
2108 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002109struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002110{
2111 struct journal_head *jh = NULL;
2112
2113 jbd_lock_bh_journal_head(bh);
2114 if (buffer_jbd(bh)) {
2115 jh = bh2jh(bh);
2116 jh->b_jcount++;
2117 }
2118 jbd_unlock_bh_journal_head(bh);
2119 return jh;
2120}
2121
2122static void __journal_remove_journal_head(struct buffer_head *bh)
2123{
2124 struct journal_head *jh = bh2jh(bh);
2125
2126 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2127
2128 get_bh(bh);
2129 if (jh->b_jcount == 0) {
2130 if (jh->b_transaction == NULL &&
2131 jh->b_next_transaction == NULL &&
2132 jh->b_cp_transaction == NULL) {
2133 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2134 J_ASSERT_BH(bh, buffer_jbd(bh));
2135 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2136 BUFFER_TRACE(bh, "remove journal_head");
2137 if (jh->b_frozen_data) {
2138 printk(KERN_WARNING "%s: freeing "
2139 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002140 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002141 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002142 }
2143 if (jh->b_committed_data) {
2144 printk(KERN_WARNING "%s: freeing "
2145 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002146 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002147 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002148 }
2149 bh->b_private = NULL;
2150 jh->b_bh = NULL; /* debug, really */
2151 clear_buffer_jbd(bh);
2152 __brelse(bh);
2153 journal_free_journal_head(jh);
2154 } else {
2155 BUFFER_TRACE(bh, "journal_head was locked");
2156 }
2157 }
2158}
2159
2160/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002161 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002162 * and has a zero b_jcount then remove and release its journal_head. If we did
2163 * see that the buffer is not used by any transaction we also "logically"
2164 * decrement ->b_count.
2165 *
2166 * We in fact take an additional increment on ->b_count as a convenience,
2167 * because the caller usually wants to do additional things with the bh
2168 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002169 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002170 * time. Once the caller has run __brelse(), the buffer is eligible for
2171 * reaping by try_to_free_buffers().
2172 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002173void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002174{
2175 jbd_lock_bh_journal_head(bh);
2176 __journal_remove_journal_head(bh);
2177 jbd_unlock_bh_journal_head(bh);
2178}
2179
2180/*
2181 * Drop a reference on the passed journal_head. If it fell to zero then try to
2182 * release the journal_head from the buffer_head.
2183 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002184void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002185{
2186 struct buffer_head *bh = jh2bh(jh);
2187
2188 jbd_lock_bh_journal_head(bh);
2189 J_ASSERT_JH(jh, jh->b_jcount > 0);
2190 --jh->b_jcount;
2191 if (!jh->b_jcount && !jh->b_transaction) {
2192 __journal_remove_journal_head(bh);
2193 __brelse(bh);
2194 }
2195 jbd_unlock_bh_journal_head(bh);
2196}
2197
2198/*
Jan Karac851ed52008-07-11 19:27:31 -04002199 * Initialize jbd inode head
2200 */
2201void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2202{
2203 jinode->i_transaction = NULL;
2204 jinode->i_next_transaction = NULL;
2205 jinode->i_vfs_inode = inode;
2206 jinode->i_flags = 0;
2207 INIT_LIST_HEAD(&jinode->i_list);
2208}
2209
2210/*
2211 * Function to be called before we start removing inode from memory (i.e.,
2212 * clear_inode() is a fine place to be called from). It removes inode from
2213 * transaction's lists.
2214 */
2215void jbd2_journal_release_jbd_inode(journal_t *journal,
2216 struct jbd2_inode *jinode)
2217{
2218 int writeout = 0;
2219
2220 if (!journal)
2221 return;
2222restart:
2223 spin_lock(&journal->j_list_lock);
2224 /* Is commit writing out inode - we have to wait */
2225 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2226 wait_queue_head_t *wq;
2227 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2228 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2229 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2230 spin_unlock(&journal->j_list_lock);
2231 schedule();
2232 finish_wait(wq, &wait.wait);
2233 goto restart;
2234 }
2235
2236 /* Do we need to wait for data writeback? */
2237 if (journal->j_committing_transaction == jinode->i_transaction)
2238 writeout = 1;
2239 if (jinode->i_transaction) {
2240 list_del(&jinode->i_list);
2241 jinode->i_transaction = NULL;
2242 }
2243 spin_unlock(&journal->j_list_lock);
2244}
2245
2246/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002247 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002248 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002249#ifdef CONFIG_JBD2_DEBUG
2250u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002251EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002252
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002253#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254
Jose R. Santos6f38c742007-10-16 18:38:25 -04002255static struct dentry *jbd2_debugfs_dir;
2256static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002257
2258static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002259{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002260 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2261 if (jbd2_debugfs_dir)
2262 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2263 jbd2_debugfs_dir,
2264 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002265}
2266
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002267static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002268{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002269 debugfs_remove(jbd2_debug);
2270 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002271}
2272
2273#else
2274
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002275static void __init jbd2_create_debugfs_entry(void)
2276{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002277}
2278
2279static void __exit jbd2_remove_debugfs_entry(void)
2280{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002281}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002282
2283#endif
2284
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002285#ifdef CONFIG_PROC_FS
2286
2287#define JBD2_STATS_PROC_NAME "fs/jbd2"
2288
2289static void __init jbd2_create_jbd_stats_proc_entry(void)
2290{
2291 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2292}
2293
2294static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2295{
2296 if (proc_jbd2_stats)
2297 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2298}
2299
2300#else
2301
2302#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2303#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2304
2305#endif
2306
Christoph Lametere18b8902006-12-06 20:33:20 -08002307struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002308
2309static int __init journal_init_handle_cache(void)
2310{
Johann Lombardia920e942006-10-11 01:21:00 -07002311 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002312 sizeof(handle_t),
2313 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002314 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002315 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002316 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002317 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2318 return -ENOMEM;
2319 }
2320 return 0;
2321}
2322
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002323static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002324{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002325 if (jbd2_handle_cache)
2326 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002327}
2328
2329/*
2330 * Module startup and shutdown
2331 */
2332
2333static int __init journal_init_caches(void)
2334{
2335 int ret;
2336
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002337 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002338 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002339 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002340 if (ret == 0)
2341 ret = journal_init_handle_cache();
2342 return ret;
2343}
2344
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002345static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002346{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002347 jbd2_journal_destroy_revoke_caches();
2348 jbd2_journal_destroy_jbd2_journal_head_cache();
2349 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002350}
2351
2352static int __init journal_init(void)
2353{
2354 int ret;
2355
2356 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2357
2358 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002359 if (ret == 0) {
2360 jbd2_create_debugfs_entry();
2361 jbd2_create_jbd_stats_proc_entry();
2362 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002363 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002364 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002365 return ret;
2366}
2367
2368static void __exit journal_exit(void)
2369{
Jose R. Santose23291b2007-07-18 08:57:06 -04002370#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002371 int n = atomic_read(&nr_journal_heads);
2372 if (n)
2373 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2374#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002375 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002376 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002377 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002378}
2379
2380MODULE_LICENSE("GPL");
2381module_init(journal_init);
2382module_exit(journal_exit);
2383