blob: 78cf7bd7f604ad84278d93e5a631b0ec8ac76fb0 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070040
41#include <asm/uaccess.h>
42#include <asm/page.h>
43
Mingming Caof7f4bcc2006-10-11 01:20:59 -070044EXPORT_SYMBOL(jbd2_journal_start);
45EXPORT_SYMBOL(jbd2_journal_restart);
46EXPORT_SYMBOL(jbd2_journal_extend);
47EXPORT_SYMBOL(jbd2_journal_stop);
48EXPORT_SYMBOL(jbd2_journal_lock_updates);
49EXPORT_SYMBOL(jbd2_journal_unlock_updates);
50EXPORT_SYMBOL(jbd2_journal_get_write_access);
51EXPORT_SYMBOL(jbd2_journal_get_create_access);
52EXPORT_SYMBOL(jbd2_journal_get_undo_access);
53EXPORT_SYMBOL(jbd2_journal_dirty_data);
54EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
55EXPORT_SYMBOL(jbd2_journal_release_buffer);
56EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070057#if 0
58EXPORT_SYMBOL(journal_sync_buffer);
59#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070060EXPORT_SYMBOL(jbd2_journal_flush);
61EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_init_dev);
64EXPORT_SYMBOL(jbd2_journal_init_inode);
65EXPORT_SYMBOL(jbd2_journal_update_format);
66EXPORT_SYMBOL(jbd2_journal_check_used_features);
67EXPORT_SYMBOL(jbd2_journal_check_available_features);
68EXPORT_SYMBOL(jbd2_journal_set_features);
69EXPORT_SYMBOL(jbd2_journal_create);
70EXPORT_SYMBOL(jbd2_journal_load);
71EXPORT_SYMBOL(jbd2_journal_destroy);
72EXPORT_SYMBOL(jbd2_journal_update_superblock);
73EXPORT_SYMBOL(jbd2_journal_abort);
74EXPORT_SYMBOL(jbd2_journal_errno);
75EXPORT_SYMBOL(jbd2_journal_ack_err);
76EXPORT_SYMBOL(jbd2_journal_clear_err);
77EXPORT_SYMBOL(jbd2_log_wait_commit);
78EXPORT_SYMBOL(jbd2_journal_start_commit);
79EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
80EXPORT_SYMBOL(jbd2_journal_wipe);
81EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
82EXPORT_SYMBOL(jbd2_journal_invalidatepage);
83EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
84EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040085EXPORT_SYMBOL(jbd2_journal_file_inode);
86EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
87EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
88EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070089
90static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
91static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070092
93/*
94 * Helper function used to manage commit timeouts
95 */
96
97static void commit_timeout(unsigned long __data)
98{
99 struct task_struct * p = (struct task_struct *) __data;
100
101 wake_up_process(p);
102}
103
104/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700105 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700106 * journal.
107 *
108 * This kernel thread is responsible for two things:
109 *
110 * 1) COMMIT: Every so often we need to commit the current state of the
111 * filesystem to disk. The journal thread is responsible for writing
112 * all of the metadata buffers to disk.
113 *
114 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
115 * of the data in that part of the log has been rewritten elsewhere on
116 * the disk. Flushing these old buffers to reclaim space in the log is
117 * known as checkpointing, and this thread is responsible for that job.
118 */
119
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700120static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700121{
122 journal_t *journal = arg;
123 transaction_t *transaction;
124
125 /*
126 * Set up an interval timer which can be used to trigger a commit wakeup
127 * after the commit interval expires
128 */
129 setup_timer(&journal->j_commit_timer, commit_timeout,
130 (unsigned long)current);
131
132 /* Record that the journal thread is running */
133 journal->j_task = current;
134 wake_up(&journal->j_wait_done_commit);
135
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700136 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700137 journal->j_commit_interval / HZ);
138
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);
295
296 /*
297 * The buffer really shouldn't be locked: only the current committing
298 * transaction is allowed to write it, so nobody else is allowed
299 * to do any IO.
300 *
301 * akpm: except if we're journalling data, and write() output is
302 * also part of a shared mapping, and another thread has
303 * decided to launch a writepage() against this buffer.
304 */
305 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
306
307 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
308
309 /*
310 * If a new transaction has already done a buffer copy-out, then
311 * we use that version of the data for the commit.
312 */
313 jbd_lock_bh_state(bh_in);
314repeat:
315 if (jh_in->b_frozen_data) {
316 done_copy_out = 1;
317 new_page = virt_to_page(jh_in->b_frozen_data);
318 new_offset = offset_in_page(jh_in->b_frozen_data);
319 } else {
320 new_page = jh2bh(jh_in)->b_page;
321 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
322 }
323
324 mapped_data = kmap_atomic(new_page, KM_USER0);
325 /*
326 * Check for escaping
327 */
328 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700329 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700330 need_copy_out = 1;
331 do_escape = 1;
332 }
333 kunmap_atomic(mapped_data, KM_USER0);
334
335 /*
336 * Do we need to do a data copy?
337 */
338 if (need_copy_out && !done_copy_out) {
339 char *tmp;
340
341 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400342 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343 jbd_lock_bh_state(bh_in);
344 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400345 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700346 goto repeat;
347 }
348
349 jh_in->b_frozen_data = tmp;
350 mapped_data = kmap_atomic(new_page, KM_USER0);
351 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
352 kunmap_atomic(mapped_data, KM_USER0);
353
354 new_page = virt_to_page(tmp);
355 new_offset = offset_in_page(tmp);
356 done_copy_out = 1;
357 }
358
359 /*
360 * Did we need to do an escaping? Now we've done all the
361 * copying, we can finally do so.
362 */
363 if (do_escape) {
364 mapped_data = kmap_atomic(new_page, KM_USER0);
365 *((unsigned int *)(mapped_data + new_offset)) = 0;
366 kunmap_atomic(mapped_data, KM_USER0);
367 }
368
369 /* keep subsequent assertions sane */
370 new_bh->b_state = 0;
371 init_buffer(new_bh, NULL, NULL);
372 atomic_set(&new_bh->b_count, 1);
373 jbd_unlock_bh_state(bh_in);
374
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700375 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700376
377 set_bh_page(new_bh, new_page, new_offset);
378 new_jh->b_transaction = NULL;
379 new_bh->b_size = jh2bh(jh_in)->b_size;
380 new_bh->b_bdev = transaction->t_journal->j_dev;
381 new_bh->b_blocknr = blocknr;
382 set_buffer_mapped(new_bh);
383 set_buffer_dirty(new_bh);
384
385 *jh_out = new_jh;
386
387 /*
388 * The to-be-written buffer needs to get moved to the io queue,
389 * and the original buffer whose contents we are shadowing or
390 * copying is moved to the transaction's shadow queue.
391 */
392 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700393 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700395 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700396
397 return do_escape | (done_copy_out << 1);
398}
399
400/*
401 * Allocation code for the journal file. Manage the space left in the
402 * journal, so that we can begin checkpointing when appropriate.
403 */
404
405/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700406 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700407 *
408 * Called with the journal already locked.
409 *
410 * Called under j_state_lock
411 */
412
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700413int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700414{
415 int left = journal->j_free;
416
417 assert_spin_locked(&journal->j_state_lock);
418
419 /*
420 * Be pessimistic here about the number of those free blocks which
421 * might be required for log descriptor control blocks.
422 */
423
424#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
425
426 left -= MIN_LOG_RESERVED_BLOCKS;
427
428 if (left <= 0)
429 return 0;
430 left -= (left >> 3);
431 return left;
432}
433
434/*
435 * Called under j_state_lock. Returns true if a transaction was started.
436 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700437int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700438{
439 /*
440 * Are we already doing a recent enough commit?
441 */
442 if (!tid_geq(journal->j_commit_request, target)) {
443 /*
444 * We want a new commit: OK, mark the request and wakup the
445 * commit thread. We do _not_ do the commit ourselves.
446 */
447
448 journal->j_commit_request = target;
449 jbd_debug(1, "JBD: requesting commit %d/%d\n",
450 journal->j_commit_request,
451 journal->j_commit_sequence);
452 wake_up(&journal->j_wait_commit);
453 return 1;
454 }
455 return 0;
456}
457
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700458int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459{
460 int ret;
461
462 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700463 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700464 spin_unlock(&journal->j_state_lock);
465 return ret;
466}
467
468/*
469 * Force and wait upon a commit if the calling process is not within
470 * transaction. This is used for forcing out undo-protected data which contains
471 * bitmaps, when the fs is running out of space.
472 *
473 * We can only force the running transaction if we don't have an active handle;
474 * otherwise, we will deadlock.
475 *
476 * Returns true if a transaction was started.
477 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700478int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700479{
480 transaction_t *transaction = NULL;
481 tid_t tid;
482
483 spin_lock(&journal->j_state_lock);
484 if (journal->j_running_transaction && !current->journal_info) {
485 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700486 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700487 } else if (journal->j_committing_transaction)
488 transaction = journal->j_committing_transaction;
489
490 if (!transaction) {
491 spin_unlock(&journal->j_state_lock);
492 return 0; /* Nothing to retry */
493 }
494
495 tid = transaction->t_tid;
496 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700497 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700498 return 1;
499}
500
501/*
502 * Start a commit of the current running transaction (if any). Returns true
503 * if a transaction was started, and fills its tid in at *ptid
504 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700505int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700506{
507 int ret = 0;
508
509 spin_lock(&journal->j_state_lock);
510 if (journal->j_running_transaction) {
511 tid_t tid = journal->j_running_transaction->t_tid;
512
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700513 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700514 if (ret && ptid)
515 *ptid = tid;
516 } else if (journal->j_committing_transaction && ptid) {
517 /*
518 * If ext3_write_super() recently started a commit, then we
519 * have to wait for completion of that transaction
520 */
521 *ptid = journal->j_committing_transaction->t_tid;
522 ret = 1;
523 }
524 spin_unlock(&journal->j_state_lock);
525 return ret;
526}
527
528/*
529 * Wait for a specified commit to complete.
530 * The caller may not hold the journal lock.
531 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700532int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700533{
534 int err = 0;
535
Jose R. Santose23291b2007-07-18 08:57:06 -0400536#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700537 spin_lock(&journal->j_state_lock);
538 if (!tid_geq(journal->j_commit_request, tid)) {
539 printk(KERN_EMERG
540 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400541 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 }
543 spin_unlock(&journal->j_state_lock);
544#endif
545 spin_lock(&journal->j_state_lock);
546 while (tid_gt(tid, journal->j_commit_sequence)) {
547 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
548 tid, journal->j_commit_sequence);
549 wake_up(&journal->j_wait_commit);
550 spin_unlock(&journal->j_state_lock);
551 wait_event(journal->j_wait_done_commit,
552 !tid_gt(tid, journal->j_commit_sequence));
553 spin_lock(&journal->j_state_lock);
554 }
555 spin_unlock(&journal->j_state_lock);
556
557 if (unlikely(is_journal_aborted(journal))) {
558 printk(KERN_EMERG "journal commit I/O error\n");
559 err = -EIO;
560 }
561 return err;
562}
563
564/*
565 * Log buffer allocation routines:
566 */
567
Mingming Cao18eba7a2006-10-11 01:21:13 -0700568int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700569{
570 unsigned long blocknr;
571
572 spin_lock(&journal->j_state_lock);
573 J_ASSERT(journal->j_free > 1);
574
575 blocknr = journal->j_head;
576 journal->j_head++;
577 journal->j_free--;
578 if (journal->j_head == journal->j_last)
579 journal->j_head = journal->j_first;
580 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700581 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700582}
583
584/*
585 * Conversion of logical to physical block numbers for the journal
586 *
587 * On external journals the journal blocks are identity-mapped, so
588 * this is a no-op. If needed, we can use j_blk_offset - everything is
589 * ready.
590 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700591int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700592 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700593{
594 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700595 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700596
597 if (journal->j_inode) {
598 ret = bmap(journal->j_inode, blocknr);
599 if (ret)
600 *retp = ret;
601 else {
602 char b[BDEVNAME_SIZE];
603
604 printk(KERN_ALERT "%s: journal block not found "
605 "at offset %lu on %s\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400606 __func__,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700607 blocknr,
608 bdevname(journal->j_dev, b));
609 err = -EIO;
610 __journal_abort_soft(journal, err);
611 }
612 } else {
613 *retp = blocknr; /* +journal->j_blk_offset */
614 }
615 return err;
616}
617
618/*
619 * We play buffer_head aliasing tricks to write data/metadata blocks to
620 * the journal without copying their contents, but for journal
621 * descriptor blocks we do need to generate bona fide buffers.
622 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700623 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700624 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
625 * But we don't bother doing that, so there will be coherency problems with
626 * mmaps of blockdevs which hold live JBD-controlled filesystems.
627 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700628struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700629{
630 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700631 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700632 int err;
633
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700634 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700635
636 if (err)
637 return NULL;
638
639 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
640 lock_buffer(bh);
641 memset(bh->b_data, 0, journal->j_blocksize);
642 set_buffer_uptodate(bh);
643 unlock_buffer(bh);
644 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700645 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646}
647
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500648struct jbd2_stats_proc_session {
649 journal_t *journal;
650 struct transaction_stats_s *stats;
651 int start;
652 int max;
653};
654
655static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
656 struct transaction_stats_s *ts,
657 int first)
658{
659 if (ts == s->stats + s->max)
660 ts = s->stats;
661 if (!first && ts == s->stats + s->start)
662 return NULL;
663 while (ts->ts_type == 0) {
664 ts++;
665 if (ts == s->stats + s->max)
666 ts = s->stats;
667 if (ts == s->stats + s->start)
668 return NULL;
669 }
670 return ts;
671
672}
673
674static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
675{
676 struct jbd2_stats_proc_session *s = seq->private;
677 struct transaction_stats_s *ts;
678 int l = *pos;
679
680 if (l == 0)
681 return SEQ_START_TOKEN;
682 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
683 if (!ts)
684 return NULL;
685 l--;
686 while (l) {
687 ts = jbd2_history_skip_empty(s, ++ts, 0);
688 if (!ts)
689 break;
690 l--;
691 }
692 return ts;
693}
694
695static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
696{
697 struct jbd2_stats_proc_session *s = seq->private;
698 struct transaction_stats_s *ts = v;
699
700 ++*pos;
701 if (v == SEQ_START_TOKEN)
702 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
703 else
704 return jbd2_history_skip_empty(s, ++ts, 0);
705}
706
707static int jbd2_seq_history_show(struct seq_file *seq, void *v)
708{
709 struct transaction_stats_s *ts = v;
710 if (v == SEQ_START_TOKEN) {
711 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
712 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
713 "wait", "run", "lock", "flush", "log", "hndls",
714 "block", "inlog", "ctime", "write", "drop",
715 "close");
716 return 0;
717 }
718 if (ts->ts_type == JBD2_STATS_RUN)
719 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
720 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
721 jiffies_to_msecs(ts->u.run.rs_wait),
722 jiffies_to_msecs(ts->u.run.rs_running),
723 jiffies_to_msecs(ts->u.run.rs_locked),
724 jiffies_to_msecs(ts->u.run.rs_flushing),
725 jiffies_to_msecs(ts->u.run.rs_logging),
726 ts->u.run.rs_handle_count,
727 ts->u.run.rs_blocks,
728 ts->u.run.rs_blocks_logged);
729 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
730 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
731 "C", ts->ts_tid, " ",
732 jiffies_to_msecs(ts->u.chp.cs_chp_time),
733 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
734 ts->u.chp.cs_forced_to_close);
735 else
736 J_ASSERT(0);
737 return 0;
738}
739
740static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
741{
742}
743
744static struct seq_operations jbd2_seq_history_ops = {
745 .start = jbd2_seq_history_start,
746 .next = jbd2_seq_history_next,
747 .stop = jbd2_seq_history_stop,
748 .show = jbd2_seq_history_show,
749};
750
751static int jbd2_seq_history_open(struct inode *inode, struct file *file)
752{
753 journal_t *journal = PDE(inode)->data;
754 struct jbd2_stats_proc_session *s;
755 int rc, size;
756
757 s = kmalloc(sizeof(*s), GFP_KERNEL);
758 if (s == NULL)
759 return -ENOMEM;
760 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
761 s->stats = kmalloc(size, GFP_KERNEL);
762 if (s->stats == NULL) {
763 kfree(s);
764 return -ENOMEM;
765 }
766 spin_lock(&journal->j_history_lock);
767 memcpy(s->stats, journal->j_history, size);
768 s->max = journal->j_history_max;
769 s->start = journal->j_history_cur % s->max;
770 spin_unlock(&journal->j_history_lock);
771
772 rc = seq_open(file, &jbd2_seq_history_ops);
773 if (rc == 0) {
774 struct seq_file *m = file->private_data;
775 m->private = s;
776 } else {
777 kfree(s->stats);
778 kfree(s);
779 }
780 return rc;
781
782}
783
784static int jbd2_seq_history_release(struct inode *inode, struct file *file)
785{
786 struct seq_file *seq = file->private_data;
787 struct jbd2_stats_proc_session *s = seq->private;
788
789 kfree(s->stats);
790 kfree(s);
791 return seq_release(inode, file);
792}
793
794static struct file_operations jbd2_seq_history_fops = {
795 .owner = THIS_MODULE,
796 .open = jbd2_seq_history_open,
797 .read = seq_read,
798 .llseek = seq_lseek,
799 .release = jbd2_seq_history_release,
800};
801
802static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
803{
804 return *pos ? NULL : SEQ_START_TOKEN;
805}
806
807static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
808{
809 return NULL;
810}
811
812static int jbd2_seq_info_show(struct seq_file *seq, void *v)
813{
814 struct jbd2_stats_proc_session *s = seq->private;
815
816 if (v != SEQ_START_TOKEN)
817 return 0;
818 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
819 s->stats->ts_tid,
820 s->journal->j_max_transaction_buffers);
821 if (s->stats->ts_tid == 0)
822 return 0;
823 seq_printf(seq, "average: \n %ums waiting for transaction\n",
824 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
825 seq_printf(seq, " %ums running transaction\n",
826 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
827 seq_printf(seq, " %ums transaction was being locked\n",
828 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
829 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
830 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
831 seq_printf(seq, " %ums logging transaction\n",
832 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
833 seq_printf(seq, " %lu handles per transaction\n",
834 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
835 seq_printf(seq, " %lu blocks per transaction\n",
836 s->stats->u.run.rs_blocks / s->stats->ts_tid);
837 seq_printf(seq, " %lu logged blocks per transaction\n",
838 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
839 return 0;
840}
841
842static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
843{
844}
845
846static struct seq_operations jbd2_seq_info_ops = {
847 .start = jbd2_seq_info_start,
848 .next = jbd2_seq_info_next,
849 .stop = jbd2_seq_info_stop,
850 .show = jbd2_seq_info_show,
851};
852
853static int jbd2_seq_info_open(struct inode *inode, struct file *file)
854{
855 journal_t *journal = PDE(inode)->data;
856 struct jbd2_stats_proc_session *s;
857 int rc, size;
858
859 s = kmalloc(sizeof(*s), GFP_KERNEL);
860 if (s == NULL)
861 return -ENOMEM;
862 size = sizeof(struct transaction_stats_s);
863 s->stats = kmalloc(size, GFP_KERNEL);
864 if (s->stats == NULL) {
865 kfree(s);
866 return -ENOMEM;
867 }
868 spin_lock(&journal->j_history_lock);
869 memcpy(s->stats, &journal->j_stats, size);
870 s->journal = journal;
871 spin_unlock(&journal->j_history_lock);
872
873 rc = seq_open(file, &jbd2_seq_info_ops);
874 if (rc == 0) {
875 struct seq_file *m = file->private_data;
876 m->private = s;
877 } else {
878 kfree(s->stats);
879 kfree(s);
880 }
881 return rc;
882
883}
884
885static int jbd2_seq_info_release(struct inode *inode, struct file *file)
886{
887 struct seq_file *seq = file->private_data;
888 struct jbd2_stats_proc_session *s = seq->private;
889 kfree(s->stats);
890 kfree(s);
891 return seq_release(inode, file);
892}
893
894static struct file_operations jbd2_seq_info_fops = {
895 .owner = THIS_MODULE,
896 .open = jbd2_seq_info_open,
897 .read = seq_read,
898 .llseek = seq_lseek,
899 .release = jbd2_seq_info_release,
900};
901
902static struct proc_dir_entry *proc_jbd2_stats;
903
904static void jbd2_stats_proc_init(journal_t *journal)
905{
906 char name[BDEVNAME_SIZE];
907
Jean Delvaref36f21e2008-05-12 14:02:33 -0700908 bdevname(journal->j_dev, name);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500909 journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats);
910 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700911 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
912 &jbd2_seq_history_fops, journal);
913 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
914 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500915 }
916}
917
918static void jbd2_stats_proc_exit(journal_t *journal)
919{
920 char name[BDEVNAME_SIZE];
921
Jean Delvaref36f21e2008-05-12 14:02:33 -0700922 bdevname(journal->j_dev, name);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500923 remove_proc_entry("info", journal->j_proc_entry);
924 remove_proc_entry("history", journal->j_proc_entry);
925 remove_proc_entry(name, proc_jbd2_stats);
926}
927
928static void journal_init_stats(journal_t *journal)
929{
930 int size;
931
932 if (!proc_jbd2_stats)
933 return;
934
935 journal->j_history_max = 100;
936 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
937 journal->j_history = kzalloc(size, GFP_KERNEL);
938 if (!journal->j_history) {
939 journal->j_history_max = 0;
940 return;
941 }
942 spin_lock_init(&journal->j_history_lock);
943}
944
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945/*
946 * Management for journal control blocks: functions to create and
947 * destroy journal_t structures, and to initialise and read existing
948 * journal blocks from disk. */
949
950/* First: create and setup a journal_t object in memory. We initialise
951 * very few fields yet: that has to wait until we have created the
952 * journal structures from from scratch, or loaded them from disk. */
953
954static journal_t * journal_init_common (void)
955{
956 journal_t *journal;
957 int err;
958
Mingming Caod802ffa2007-10-16 18:38:25 -0400959 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700960 if (!journal)
961 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700962
963 init_waitqueue_head(&journal->j_wait_transaction_locked);
964 init_waitqueue_head(&journal->j_wait_logspace);
965 init_waitqueue_head(&journal->j_wait_done_commit);
966 init_waitqueue_head(&journal->j_wait_checkpoint);
967 init_waitqueue_head(&journal->j_wait_commit);
968 init_waitqueue_head(&journal->j_wait_updates);
969 mutex_init(&journal->j_barrier);
970 mutex_init(&journal->j_checkpoint_mutex);
971 spin_lock_init(&journal->j_revoke_lock);
972 spin_lock_init(&journal->j_list_lock);
973 spin_lock_init(&journal->j_state_lock);
974
Mingming Caocd02ff02007-10-16 18:38:25 -0400975 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700976
977 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700978 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700979
980 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700981 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700982 if (err) {
983 kfree(journal);
984 goto fail;
985 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500986
987 journal_init_stats(journal);
988
Dave Kleikamp470decc2006-10-11 01:20:57 -0700989 return journal;
990fail:
991 return NULL;
992}
993
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700994/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700995 *
996 * Create a journal structure assigned some fixed set of disk blocks to
997 * the journal. We don't actually touch those disk blocks yet, but we
998 * need to set up all of the mapping information to tell the journaling
999 * system where the journal blocks are.
1000 *
1001 */
1002
1003/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001004 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001005 * @bdev: Block device on which to create the journal
1006 * @fs_dev: Device which hold journalled filesystem for this journal.
1007 * @start: Block nr Start of journal.
1008 * @len: Length of the journal in blocks.
1009 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001010 *
1011 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001012 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001013 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001014 * range of blocks on an arbitrary block device.
1015 *
1016 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001017journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001018 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001019 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001020{
1021 journal_t *journal = journal_init_common();
1022 struct buffer_head *bh;
1023 int n;
1024
1025 if (!journal)
1026 return NULL;
1027
1028 /* journal descriptor can store up to n blocks -bzzz */
1029 journal->j_blocksize = blocksize;
1030 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1031 journal->j_wbufsize = n;
1032 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1033 if (!journal->j_wbuf) {
1034 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001035 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001036 kfree(journal);
1037 journal = NULL;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001038 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001039 }
1040 journal->j_dev = bdev;
1041 journal->j_fs_dev = fs_dev;
1042 journal->j_blk_offset = start;
1043 journal->j_maxlen = len;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001044 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001045
1046 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1047 J_ASSERT(bh != NULL);
1048 journal->j_sb_buffer = bh;
1049 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001050out:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051 return journal;
1052}
1053
1054/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001055 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001056 * @inode: An inode to create the journal in
1057 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001058 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001059 * the journal. The inode must exist already, must support bmap() and
1060 * must have all data blocks preallocated.
1061 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001062journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063{
1064 struct buffer_head *bh;
1065 journal_t *journal = journal_init_common();
1066 int err;
1067 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001068 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001069
1070 if (!journal)
1071 return NULL;
1072
1073 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1074 journal->j_inode = inode;
1075 jbd_debug(1,
1076 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1077 journal, inode->i_sb->s_id, inode->i_ino,
1078 (long long) inode->i_size,
1079 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1080
1081 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1082 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001083 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001084
1085 /* journal descriptor can store up to n blocks -bzzz */
1086 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1087 journal->j_wbufsize = n;
1088 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1089 if (!journal->j_wbuf) {
1090 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001091 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001092 kfree(journal);
1093 return NULL;
1094 }
1095
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001096 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001097 /* If that failed, give up */
1098 if (err) {
1099 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001100 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101 kfree(journal);
1102 return NULL;
1103 }
1104
1105 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1106 J_ASSERT(bh != NULL);
1107 journal->j_sb_buffer = bh;
1108 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1109
1110 return journal;
1111}
1112
1113/*
1114 * If the journal init or create aborts, we need to mark the journal
1115 * superblock as being NULL to prevent the journal destroy from writing
1116 * back a bogus superblock.
1117 */
1118static void journal_fail_superblock (journal_t *journal)
1119{
1120 struct buffer_head *bh = journal->j_sb_buffer;
1121 brelse(bh);
1122 journal->j_sb_buffer = NULL;
1123}
1124
1125/*
1126 * Given a journal_t structure, initialise the various fields for
1127 * startup of a new journaling session. We use this both when creating
1128 * a journal, and after recovering an old journal to reset it for
1129 * subsequent use.
1130 */
1131
1132static int journal_reset(journal_t *journal)
1133{
1134 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001135 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001136
1137 first = be32_to_cpu(sb->s_first);
1138 last = be32_to_cpu(sb->s_maxlen);
1139
1140 journal->j_first = first;
1141 journal->j_last = last;
1142
1143 journal->j_head = first;
1144 journal->j_tail = first;
1145 journal->j_free = last - first;
1146
1147 journal->j_tail_sequence = journal->j_transaction_sequence;
1148 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1149 journal->j_commit_request = journal->j_commit_sequence;
1150
1151 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1152
1153 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001154 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001155 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001156}
1157
1158/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001159 * int jbd2_journal_create() - Initialise the new journal file
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160 * @journal: Journal to create. This structure must have been initialised
1161 *
1162 * Given a journal_t structure which tells us which disk blocks we can
1163 * use, create a new journal superblock and initialise all of the
1164 * journal fields from scratch.
1165 **/
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001166int jbd2_journal_create(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001167{
Mingming Cao18eba7a2006-10-11 01:21:13 -07001168 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169 struct buffer_head *bh;
1170 journal_superblock_t *sb;
1171 int i, err;
1172
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001173 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001174 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1175 journal->j_maxlen);
1176 journal_fail_superblock(journal);
1177 return -EINVAL;
1178 }
1179
1180 if (journal->j_inode == NULL) {
1181 /*
1182 * We don't know what block to start at!
1183 */
1184 printk(KERN_EMERG
1185 "%s: creation of journal on external device!\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001186 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001187 BUG();
1188 }
1189
1190 /* Zero out the entire journal on disk. We cannot afford to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001191 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001192 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1193 for (i = 0; i < journal->j_maxlen; i++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001194 err = jbd2_journal_bmap(journal, i, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001195 if (err)
1196 return err;
1197 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1198 lock_buffer(bh);
1199 memset (bh->b_data, 0, journal->j_blocksize);
1200 BUFFER_TRACE(bh, "marking dirty");
1201 mark_buffer_dirty(bh);
1202 BUFFER_TRACE(bh, "marking uptodate");
1203 set_buffer_uptodate(bh);
1204 unlock_buffer(bh);
1205 __brelse(bh);
1206 }
1207
1208 sync_blockdev(journal->j_dev);
1209 jbd_debug(1, "JBD: journal cleared.\n");
1210
1211 /* OK, fill in the initial static fields in the new superblock */
1212 sb = journal->j_superblock;
1213
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001214 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1215 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001216
1217 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1218 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1219 sb->s_first = cpu_to_be32(1);
1220
1221 journal->j_transaction_sequence = 1;
1222
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001223 journal->j_flags &= ~JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001224 journal->j_format_version = 2;
1225
1226 return journal_reset(journal);
1227}
1228
1229/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001230 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 * @journal: The journal to update.
1232 * @wait: Set to '0' if you don't want to wait for IO completion.
1233 *
1234 * Update a journal's dynamic superblock fields and write it to disk,
1235 * optionally waiting for the IO to complete.
1236 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001237void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238{
1239 journal_superblock_t *sb = journal->j_superblock;
1240 struct buffer_head *bh = journal->j_sb_buffer;
1241
1242 /*
1243 * As a special case, if the on-disk copy is already marked as needing
1244 * no recovery (s_start == 0) and there are no outstanding transactions
1245 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001246 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001247 * attempting a write to a potential-readonly device.
1248 */
1249 if (sb->s_start == 0 && journal->j_tail_sequence ==
1250 journal->j_transaction_sequence) {
1251 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1252 "(start %ld, seq %d, errno %d)\n",
1253 journal->j_tail, journal->j_tail_sequence,
1254 journal->j_errno);
1255 goto out;
1256 }
1257
1258 spin_lock(&journal->j_state_lock);
1259 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1260 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1261
1262 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1263 sb->s_start = cpu_to_be32(journal->j_tail);
1264 sb->s_errno = cpu_to_be32(journal->j_errno);
1265 spin_unlock(&journal->j_state_lock);
1266
1267 BUFFER_TRACE(bh, "marking dirty");
1268 mark_buffer_dirty(bh);
1269 if (wait)
1270 sync_dirty_buffer(bh);
1271 else
1272 ll_rw_block(SWRITE, 1, &bh);
1273
1274out:
1275 /* If we have just flushed the log (by marking s_start==0), then
1276 * any future commit will have to be careful to update the
1277 * superblock again to re-record the true start of the log. */
1278
1279 spin_lock(&journal->j_state_lock);
1280 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001281 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001282 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001283 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001284 spin_unlock(&journal->j_state_lock);
1285}
1286
1287/*
1288 * Read the superblock for a given journal, performing initial
1289 * validation of the format.
1290 */
1291
1292static int journal_get_superblock(journal_t *journal)
1293{
1294 struct buffer_head *bh;
1295 journal_superblock_t *sb;
1296 int err = -EIO;
1297
1298 bh = journal->j_sb_buffer;
1299
1300 J_ASSERT(bh != NULL);
1301 if (!buffer_uptodate(bh)) {
1302 ll_rw_block(READ, 1, &bh);
1303 wait_on_buffer(bh);
1304 if (!buffer_uptodate(bh)) {
1305 printk (KERN_ERR
1306 "JBD: IO error reading journal superblock\n");
1307 goto out;
1308 }
1309 }
1310
1311 sb = journal->j_superblock;
1312
1313 err = -EINVAL;
1314
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001315 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001316 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1317 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1318 goto out;
1319 }
1320
1321 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001322 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001323 journal->j_format_version = 1;
1324 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001325 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001326 journal->j_format_version = 2;
1327 break;
1328 default:
1329 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1330 goto out;
1331 }
1332
1333 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1334 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1335 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1336 printk (KERN_WARNING "JBD: journal file too short\n");
1337 goto out;
1338 }
1339
1340 return 0;
1341
1342out:
1343 journal_fail_superblock(journal);
1344 return err;
1345}
1346
1347/*
1348 * Load the on-disk journal superblock and read the key fields into the
1349 * journal_t.
1350 */
1351
1352static int load_superblock(journal_t *journal)
1353{
1354 int err;
1355 journal_superblock_t *sb;
1356
1357 err = journal_get_superblock(journal);
1358 if (err)
1359 return err;
1360
1361 sb = journal->j_superblock;
1362
1363 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1364 journal->j_tail = be32_to_cpu(sb->s_start);
1365 journal->j_first = be32_to_cpu(sb->s_first);
1366 journal->j_last = be32_to_cpu(sb->s_maxlen);
1367 journal->j_errno = be32_to_cpu(sb->s_errno);
1368
1369 return 0;
1370}
1371
1372
1373/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001374 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001375 * @journal: Journal to act on.
1376 *
1377 * Given a journal_t structure which tells us which disk blocks contain
1378 * a journal, read the journal from disk to initialise the in-memory
1379 * structures.
1380 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001381int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001382{
1383 int err;
1384 journal_superblock_t *sb;
1385
1386 err = load_superblock(journal);
1387 if (err)
1388 return err;
1389
1390 sb = journal->j_superblock;
1391 /* If this is a V2 superblock, then we have to check the
1392 * features flags on it. */
1393
1394 if (journal->j_format_version >= 2) {
1395 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001396 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001397 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001398 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001399 printk (KERN_WARNING
1400 "JBD: Unrecognised features on journal\n");
1401 return -EINVAL;
1402 }
1403 }
1404
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 /* Let the recovery code check whether it needs to recover any
1406 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001407 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 goto recovery_error;
1409
1410 /* OK, we've finished with the dynamic journal bits:
1411 * reinitialise the dynamic contents of the superblock in memory
1412 * and reset them on disk. */
1413 if (journal_reset(journal))
1414 goto recovery_error;
1415
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001416 journal->j_flags &= ~JBD2_ABORT;
1417 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001418 return 0;
1419
1420recovery_error:
1421 printk (KERN_WARNING "JBD: recovery failed\n");
1422 return -EIO;
1423}
1424
1425/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001426 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 * @journal: Journal to act on.
1428 *
1429 * Release a journal_t structure once it is no longer in use by the
1430 * journaled object.
1431 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001432void jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001433{
1434 /* 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);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001447 jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001448 spin_lock(&journal->j_list_lock);
1449 }
1450
1451 J_ASSERT(journal->j_running_transaction == NULL);
1452 J_ASSERT(journal->j_committing_transaction == NULL);
1453 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1454 spin_unlock(&journal->j_list_lock);
1455
1456 /* We can now mark the journal as empty. */
1457 journal->j_tail = 0;
1458 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1459 if (journal->j_sb_buffer) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001460 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001461 brelse(journal->j_sb_buffer);
1462 }
1463
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001464 if (journal->j_proc_entry)
1465 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001466 if (journal->j_inode)
1467 iput(journal->j_inode);
1468 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001469 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001470 kfree(journal->j_wbuf);
1471 kfree(journal);
1472}
1473
1474
1475/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001476 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 * @journal: Journal to check.
1478 * @compat: bitmask of compatible features
1479 * @ro: bitmask of features that force read-only mount
1480 * @incompat: bitmask of incompatible features
1481 *
1482 * Check whether the journal uses all of a given set of
1483 * features. Return true (non-zero) if it does.
1484 **/
1485
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001486int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487 unsigned long ro, unsigned long incompat)
1488{
1489 journal_superblock_t *sb;
1490
1491 if (!compat && !ro && !incompat)
1492 return 1;
1493 if (journal->j_format_version == 1)
1494 return 0;
1495
1496 sb = journal->j_superblock;
1497
1498 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1499 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1500 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1501 return 1;
1502
1503 return 0;
1504}
1505
1506/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001507 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001508 * @journal: Journal to check.
1509 * @compat: bitmask of compatible features
1510 * @ro: bitmask of features that force read-only mount
1511 * @incompat: bitmask of incompatible features
1512 *
1513 * Check whether the journaling code supports the use of
1514 * all of a given set of features on this journal. Return true
1515 * (non-zero) if it can. */
1516
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001517int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001518 unsigned long ro, unsigned long incompat)
1519{
1520 journal_superblock_t *sb;
1521
1522 if (!compat && !ro && !incompat)
1523 return 1;
1524
1525 sb = journal->j_superblock;
1526
1527 /* We can support any known requested features iff the
1528 * superblock is in version 2. Otherwise we fail to support any
1529 * extended sb features. */
1530
1531 if (journal->j_format_version != 2)
1532 return 0;
1533
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001534 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1535 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1536 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001537 return 1;
1538
1539 return 0;
1540}
1541
1542/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001543 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001544 * @journal: Journal to act on.
1545 * @compat: bitmask of compatible features
1546 * @ro: bitmask of features that force read-only mount
1547 * @incompat: bitmask of incompatible features
1548 *
1549 * Mark a given journal feature as present on the
1550 * superblock. Returns true if the requested features could be set.
1551 *
1552 */
1553
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001554int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001555 unsigned long ro, unsigned long incompat)
1556{
1557 journal_superblock_t *sb;
1558
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001559 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001560 return 1;
1561
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001562 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001563 return 0;
1564
1565 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1566 compat, ro, incompat);
1567
1568 sb = journal->j_superblock;
1569
1570 sb->s_feature_compat |= cpu_to_be32(compat);
1571 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1572 sb->s_feature_incompat |= cpu_to_be32(incompat);
1573
1574 return 1;
1575}
1576
Girish Shilamkar818d2762008-01-28 23:58:27 -05001577/*
1578 * jbd2_journal_clear_features () - Clear a given journal feature in the
1579 * superblock
1580 * @journal: Journal to act on.
1581 * @compat: bitmask of compatible features
1582 * @ro: bitmask of features that force read-only mount
1583 * @incompat: bitmask of incompatible features
1584 *
1585 * Clear a given journal feature as present on the
1586 * superblock.
1587 */
1588void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1589 unsigned long ro, unsigned long incompat)
1590{
1591 journal_superblock_t *sb;
1592
1593 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1594 compat, ro, incompat);
1595
1596 sb = journal->j_superblock;
1597
1598 sb->s_feature_compat &= ~cpu_to_be32(compat);
1599 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1600 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1601}
1602EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001603
1604/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001605 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001606 * @journal: Journal to act on.
1607 *
1608 * Given an initialised but unloaded journal struct, poke about in the
1609 * on-disk structure to update it to the most recent supported version.
1610 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001611int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001612{
1613 journal_superblock_t *sb;
1614 int err;
1615
1616 err = journal_get_superblock(journal);
1617 if (err)
1618 return err;
1619
1620 sb = journal->j_superblock;
1621
1622 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001623 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001625 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001626 return journal_convert_superblock_v1(journal, sb);
1627 default:
1628 break;
1629 }
1630 return -EINVAL;
1631}
1632
1633static int journal_convert_superblock_v1(journal_t *journal,
1634 journal_superblock_t *sb)
1635{
1636 int offset, blocksize;
1637 struct buffer_head *bh;
1638
1639 printk(KERN_WARNING
1640 "JBD: Converting superblock from version 1 to 2.\n");
1641
1642 /* Pre-initialise new fields to zero */
1643 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1644 blocksize = be32_to_cpu(sb->s_blocksize);
1645 memset(&sb->s_feature_compat, 0, blocksize-offset);
1646
1647 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001648 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001649 journal->j_format_version = 2;
1650
1651 bh = journal->j_sb_buffer;
1652 BUFFER_TRACE(bh, "marking dirty");
1653 mark_buffer_dirty(bh);
1654 sync_dirty_buffer(bh);
1655 return 0;
1656}
1657
1658
1659/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001660 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001661 * @journal: Journal to act on.
1662 *
1663 * Flush all data for a given journal to disk and empty the journal.
1664 * Filesystems can use this when remounting readonly to ensure that
1665 * recovery does not need to happen on remount.
1666 */
1667
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001668int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001669{
1670 int err = 0;
1671 transaction_t *transaction = NULL;
1672 unsigned long old_tail;
1673
1674 spin_lock(&journal->j_state_lock);
1675
1676 /* Force everything buffered to the log... */
1677 if (journal->j_running_transaction) {
1678 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680 } else if (journal->j_committing_transaction)
1681 transaction = journal->j_committing_transaction;
1682
1683 /* Wait for the log commit to complete... */
1684 if (transaction) {
1685 tid_t tid = transaction->t_tid;
1686
1687 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001688 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001689 } else {
1690 spin_unlock(&journal->j_state_lock);
1691 }
1692
1693 /* ...and flush everything in the log out to disk. */
1694 spin_lock(&journal->j_list_lock);
1695 while (!err && journal->j_checkpoint_transactions != NULL) {
1696 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001697 err = jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001698 spin_lock(&journal->j_list_lock);
1699 }
1700 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001701 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001702
1703 /* Finally, mark the journal as really needing no recovery.
1704 * This sets s_start==0 in the underlying superblock, which is
1705 * the magic code for a fully-recovered superblock. Any future
1706 * commits of data to the journal will restore the current
1707 * s_start value. */
1708 spin_lock(&journal->j_state_lock);
1709 old_tail = journal->j_tail;
1710 journal->j_tail = 0;
1711 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001712 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713 spin_lock(&journal->j_state_lock);
1714 journal->j_tail = old_tail;
1715
1716 J_ASSERT(!journal->j_running_transaction);
1717 J_ASSERT(!journal->j_committing_transaction);
1718 J_ASSERT(!journal->j_checkpoint_transactions);
1719 J_ASSERT(journal->j_head == journal->j_tail);
1720 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1721 spin_unlock(&journal->j_state_lock);
1722 return err;
1723}
1724
1725/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001726 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001727 * @journal: Journal to act on.
1728 * @write: flag (see below)
1729 *
1730 * Wipe out all of the contents of a journal, safely. This will produce
1731 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001732 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001733 *
1734 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1735 * we merely suppress recovery.
1736 */
1737
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001738int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001739{
1740 journal_superblock_t *sb;
1741 int err = 0;
1742
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001743 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001744
1745 err = load_superblock(journal);
1746 if (err)
1747 return err;
1748
1749 sb = journal->j_superblock;
1750
1751 if (!journal->j_tail)
1752 goto no_recovery;
1753
1754 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1755 write ? "Clearing" : "Ignoring");
1756
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001757 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001758 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001759 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760
1761 no_recovery:
1762 return err;
1763}
1764
1765/*
1766 * journal_dev_name: format a character string to describe on what
1767 * device this journal is present.
1768 */
1769
1770static const char *journal_dev_name(journal_t *journal, char *buffer)
1771{
1772 struct block_device *bdev;
1773
1774 if (journal->j_inode)
1775 bdev = journal->j_inode->i_sb->s_bdev;
1776 else
1777 bdev = journal->j_dev;
1778
1779 return bdevname(bdev, buffer);
1780}
1781
1782/*
1783 * Journal abort has very specific semantics, which we describe
1784 * for journal abort.
1785 *
1786 * Two internal function, which provide abort to te jbd layer
1787 * itself are here.
1788 */
1789
1790/*
1791 * Quick version for internal journal use (doesn't lock the journal).
1792 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1793 * and don't attempt to make any other journal updates.
1794 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796{
1797 transaction_t *transaction;
1798 char b[BDEVNAME_SIZE];
1799
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001800 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001801 return;
1802
1803 printk(KERN_ERR "Aborting journal on device %s.\n",
1804 journal_dev_name(journal, b));
1805
1806 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001807 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808 transaction = journal->j_running_transaction;
1809 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001810 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 spin_unlock(&journal->j_state_lock);
1812}
1813
1814/* Soft abort: record the abort error status in the journal superblock,
1815 * but don't do any other IO. */
1816static void __journal_abort_soft (journal_t *journal, int errno)
1817{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001818 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001819 return;
1820
1821 if (!journal->j_errno)
1822 journal->j_errno = errno;
1823
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001824 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001825
1826 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001827 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828}
1829
1830/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001831 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001832 * @journal: the journal to shutdown.
1833 * @errno: an error number to record in the journal indicating
1834 * the reason for the shutdown.
1835 *
1836 * Perform a complete, immediate shutdown of the ENTIRE
1837 * journal (not of a single transaction). This operation cannot be
1838 * undone without closing and reopening the journal.
1839 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001840 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1842 * mode.
1843 *
1844 * Journal abort has very specific semantics. Any existing dirty,
1845 * unjournaled buffers in the main filesystem will still be written to
1846 * disk by bdflush, but the journaling mechanism will be suspended
1847 * immediately and no further transaction commits will be honoured.
1848 *
1849 * Any dirty, journaled buffers will be written back to disk without
1850 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1851 * filesystem, but we _do_ attempt to leave as much data as possible
1852 * behind for fsck to use for cleanup.
1853 *
1854 * Any attempt to get a new transaction handle on a journal which is in
1855 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001856 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001857 * entered abort state during the update.
1858 *
1859 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001860 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001861 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001862 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001863 * which will be recorded (if possible) in the journal superblock. This
1864 * allows a client to record failure conditions in the middle of a
1865 * transaction without having to complete the transaction to record the
1866 * failure to disk. ext3_error, for example, now uses this
1867 * functionality.
1868 *
1869 * Errors which originate from within the journaling layer will NOT
1870 * supply an errno; a null errno implies that absolutely no further
1871 * writes are done to the journal (unless there are any already in
1872 * progress).
1873 *
1874 */
1875
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001876void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001877{
1878 __journal_abort_soft(journal, errno);
1879}
1880
1881/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001882 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883 * @journal: journal to examine.
1884 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001885 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001886 * time the journal was mounted - if the journal was stopped
1887 * without calling abort this will be 0.
1888 *
1889 * If the journal has been aborted on this mount time -EROFS will
1890 * be returned.
1891 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001892int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893{
1894 int err;
1895
1896 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001897 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001898 err = -EROFS;
1899 else
1900 err = journal->j_errno;
1901 spin_unlock(&journal->j_state_lock);
1902 return err;
1903}
1904
1905/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001906 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907 * @journal: journal to act on.
1908 *
1909 * An error must be cleared or Acked to take a FS out of readonly
1910 * mode.
1911 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001912int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913{
1914 int err = 0;
1915
1916 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001917 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001918 err = -EROFS;
1919 else
1920 journal->j_errno = 0;
1921 spin_unlock(&journal->j_state_lock);
1922 return err;
1923}
1924
1925/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001926 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001927 * @journal: journal to act on.
1928 *
1929 * An error must be cleared or Acked to take a FS out of readonly
1930 * mode.
1931 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001932void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001933{
1934 spin_lock(&journal->j_state_lock);
1935 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001936 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001937 spin_unlock(&journal->j_state_lock);
1938}
1939
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001940int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001941{
1942 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1943}
1944
1945/*
Zach Brownb517bea2006-10-11 01:21:08 -07001946 * helper functions to deal with 32 or 64bit block numbers.
1947 */
1948size_t journal_tag_bytes(journal_t *journal)
1949{
1950 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001951 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001952 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001953 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001954}
1955
1956/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 * Journal_head storage management
1958 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001959static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001960#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1962#endif
1963
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001964static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001965{
1966 int retval;
1967
Al Viro1076d172008-03-29 03:07:18 +00001968 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001969 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001970 sizeof(struct journal_head),
1971 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001972 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001973 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001974 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001975 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001976 retval = -ENOMEM;
1977 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1978 }
1979 return retval;
1980}
1981
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001982static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001983{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001984 if (jbd2_journal_head_cache) {
1985 kmem_cache_destroy(jbd2_journal_head_cache);
1986 jbd2_journal_head_cache = NULL;
1987 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001988}
1989
1990/*
1991 * journal_head splicing and dicing
1992 */
1993static struct journal_head *journal_alloc_journal_head(void)
1994{
1995 struct journal_head *ret;
1996 static unsigned long last_warning;
1997
Jose R. Santose23291b2007-07-18 08:57:06 -04001998#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 atomic_inc(&nr_journal_heads);
2000#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002001 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002002 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002003 jbd_debug(1, "out of memory for journal_head\n");
2004 if (time_after(jiffies, last_warning + 5*HZ)) {
2005 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002006 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007 last_warning = jiffies;
2008 }
Al Viro1076d172008-03-29 03:07:18 +00002009 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002010 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002011 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 }
2013 }
2014 return ret;
2015}
2016
2017static void journal_free_journal_head(struct journal_head *jh)
2018{
Jose R. Santose23291b2007-07-18 08:57:06 -04002019#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002020 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002021 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002023 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002024}
2025
2026/*
2027 * A journal_head is attached to a buffer_head whenever JBD has an
2028 * interest in the buffer.
2029 *
2030 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2031 * is set. This bit is tested in core kernel code where we need to take
2032 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2033 * there.
2034 *
2035 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2036 *
2037 * When a buffer has its BH_JBD bit set it is immune from being released by
2038 * core kernel code, mainly via ->b_count.
2039 *
2040 * A journal_head may be detached from its buffer_head when the journal_head's
2041 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002042 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002043 * journal_head can be dropped if needed.
2044 *
2045 * Various places in the kernel want to attach a journal_head to a buffer_head
2046 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002047 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002048 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002049 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002050 *
2051 * So the typical usage would be:
2052 *
2053 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002054 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002055 * ...
2056 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002057 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058 *
2059 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2060 * because it has a non-zero b_transaction.
2061 */
2062
2063/*
2064 * Give a buffer_head a journal_head.
2065 *
2066 * Doesn't need the journal lock.
2067 * May sleep.
2068 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002069struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002070{
2071 struct journal_head *jh;
2072 struct journal_head *new_jh = NULL;
2073
2074repeat:
2075 if (!buffer_jbd(bh)) {
2076 new_jh = journal_alloc_journal_head();
2077 memset(new_jh, 0, sizeof(*new_jh));
2078 }
2079
2080 jbd_lock_bh_journal_head(bh);
2081 if (buffer_jbd(bh)) {
2082 jh = bh2jh(bh);
2083 } else {
2084 J_ASSERT_BH(bh,
2085 (atomic_read(&bh->b_count) > 0) ||
2086 (bh->b_page && bh->b_page->mapping));
2087
2088 if (!new_jh) {
2089 jbd_unlock_bh_journal_head(bh);
2090 goto repeat;
2091 }
2092
2093 jh = new_jh;
2094 new_jh = NULL; /* We consumed it */
2095 set_buffer_jbd(bh);
2096 bh->b_private = jh;
2097 jh->b_bh = bh;
2098 get_bh(bh);
2099 BUFFER_TRACE(bh, "added journal_head");
2100 }
2101 jh->b_jcount++;
2102 jbd_unlock_bh_journal_head(bh);
2103 if (new_jh)
2104 journal_free_journal_head(new_jh);
2105 return bh->b_private;
2106}
2107
2108/*
2109 * Grab a ref against this buffer_head's journal_head. If it ended up not
2110 * having a journal_head, return NULL
2111 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002112struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002113{
2114 struct journal_head *jh = NULL;
2115
2116 jbd_lock_bh_journal_head(bh);
2117 if (buffer_jbd(bh)) {
2118 jh = bh2jh(bh);
2119 jh->b_jcount++;
2120 }
2121 jbd_unlock_bh_journal_head(bh);
2122 return jh;
2123}
2124
2125static void __journal_remove_journal_head(struct buffer_head *bh)
2126{
2127 struct journal_head *jh = bh2jh(bh);
2128
2129 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2130
2131 get_bh(bh);
2132 if (jh->b_jcount == 0) {
2133 if (jh->b_transaction == NULL &&
2134 jh->b_next_transaction == NULL &&
2135 jh->b_cp_transaction == NULL) {
2136 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2137 J_ASSERT_BH(bh, buffer_jbd(bh));
2138 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2139 BUFFER_TRACE(bh, "remove journal_head");
2140 if (jh->b_frozen_data) {
2141 printk(KERN_WARNING "%s: freeing "
2142 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002143 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002144 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002145 }
2146 if (jh->b_committed_data) {
2147 printk(KERN_WARNING "%s: freeing "
2148 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002149 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002150 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002151 }
2152 bh->b_private = NULL;
2153 jh->b_bh = NULL; /* debug, really */
2154 clear_buffer_jbd(bh);
2155 __brelse(bh);
2156 journal_free_journal_head(jh);
2157 } else {
2158 BUFFER_TRACE(bh, "journal_head was locked");
2159 }
2160 }
2161}
2162
2163/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002164 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002165 * and has a zero b_jcount then remove and release its journal_head. If we did
2166 * see that the buffer is not used by any transaction we also "logically"
2167 * decrement ->b_count.
2168 *
2169 * We in fact take an additional increment on ->b_count as a convenience,
2170 * because the caller usually wants to do additional things with the bh
2171 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002172 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002173 * time. Once the caller has run __brelse(), the buffer is eligible for
2174 * reaping by try_to_free_buffers().
2175 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002176void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002177{
2178 jbd_lock_bh_journal_head(bh);
2179 __journal_remove_journal_head(bh);
2180 jbd_unlock_bh_journal_head(bh);
2181}
2182
2183/*
2184 * Drop a reference on the passed journal_head. If it fell to zero then try to
2185 * release the journal_head from the buffer_head.
2186 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002187void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002188{
2189 struct buffer_head *bh = jh2bh(jh);
2190
2191 jbd_lock_bh_journal_head(bh);
2192 J_ASSERT_JH(jh, jh->b_jcount > 0);
2193 --jh->b_jcount;
2194 if (!jh->b_jcount && !jh->b_transaction) {
2195 __journal_remove_journal_head(bh);
2196 __brelse(bh);
2197 }
2198 jbd_unlock_bh_journal_head(bh);
2199}
2200
2201/*
Jan Karac851ed52008-07-11 19:27:31 -04002202 * Initialize jbd inode head
2203 */
2204void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2205{
2206 jinode->i_transaction = NULL;
2207 jinode->i_next_transaction = NULL;
2208 jinode->i_vfs_inode = inode;
2209 jinode->i_flags = 0;
2210 INIT_LIST_HEAD(&jinode->i_list);
2211}
2212
2213/*
2214 * Function to be called before we start removing inode from memory (i.e.,
2215 * clear_inode() is a fine place to be called from). It removes inode from
2216 * transaction's lists.
2217 */
2218void jbd2_journal_release_jbd_inode(journal_t *journal,
2219 struct jbd2_inode *jinode)
2220{
2221 int writeout = 0;
2222
2223 if (!journal)
2224 return;
2225restart:
2226 spin_lock(&journal->j_list_lock);
2227 /* Is commit writing out inode - we have to wait */
2228 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2229 wait_queue_head_t *wq;
2230 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2231 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2232 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2233 spin_unlock(&journal->j_list_lock);
2234 schedule();
2235 finish_wait(wq, &wait.wait);
2236 goto restart;
2237 }
2238
2239 /* Do we need to wait for data writeback? */
2240 if (journal->j_committing_transaction == jinode->i_transaction)
2241 writeout = 1;
2242 if (jinode->i_transaction) {
2243 list_del(&jinode->i_list);
2244 jinode->i_transaction = NULL;
2245 }
2246 spin_unlock(&journal->j_list_lock);
2247}
2248
2249/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002250 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002251 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002252#ifdef CONFIG_JBD2_DEBUG
2253u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002254EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002255
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002256#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257
Jose R. Santos6f38c742007-10-16 18:38:25 -04002258static struct dentry *jbd2_debugfs_dir;
2259static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002260
2261static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002262{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002263 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2264 if (jbd2_debugfs_dir)
2265 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2266 jbd2_debugfs_dir,
2267 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002268}
2269
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002270static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002271{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002272 debugfs_remove(jbd2_debug);
2273 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002274}
2275
2276#else
2277
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002278static void __init jbd2_create_debugfs_entry(void)
2279{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002280}
2281
2282static void __exit jbd2_remove_debugfs_entry(void)
2283{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002284}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002285
2286#endif
2287
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002288#ifdef CONFIG_PROC_FS
2289
2290#define JBD2_STATS_PROC_NAME "fs/jbd2"
2291
2292static void __init jbd2_create_jbd_stats_proc_entry(void)
2293{
2294 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2295}
2296
2297static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2298{
2299 if (proc_jbd2_stats)
2300 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2301}
2302
2303#else
2304
2305#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2306#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2307
2308#endif
2309
Christoph Lametere18b8902006-12-06 20:33:20 -08002310struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002311
2312static int __init journal_init_handle_cache(void)
2313{
Johann Lombardia920e942006-10-11 01:21:00 -07002314 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002315 sizeof(handle_t),
2316 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002317 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002318 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002319 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002320 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2321 return -ENOMEM;
2322 }
2323 return 0;
2324}
2325
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002326static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002327{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002328 if (jbd2_handle_cache)
2329 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002330}
2331
2332/*
2333 * Module startup and shutdown
2334 */
2335
2336static int __init journal_init_caches(void)
2337{
2338 int ret;
2339
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002342 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002343 if (ret == 0)
2344 ret = journal_init_handle_cache();
2345 return ret;
2346}
2347
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002348static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002349{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002350 jbd2_journal_destroy_revoke_caches();
2351 jbd2_journal_destroy_jbd2_journal_head_cache();
2352 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002353}
2354
2355static int __init journal_init(void)
2356{
2357 int ret;
2358
2359 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2360
2361 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002362 if (ret == 0) {
2363 jbd2_create_debugfs_entry();
2364 jbd2_create_jbd_stats_proc_entry();
2365 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002366 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002367 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002368 return ret;
2369}
2370
2371static void __exit journal_exit(void)
2372{
Jose R. Santose23291b2007-07-18 08:57:06 -04002373#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002374 int n = atomic_read(&nr_journal_heads);
2375 if (n)
2376 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2377#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002378 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002379 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002380 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002381}
2382
2383MODULE_LICENSE("GPL");
2384module_init(journal_init);
2385module_exit(journal_exit);
2386