blob: 34ef98057202ae7d2750c17c62936dcc2c8b5655 [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>
Theodore Ts'od7cfa462008-12-17 00:20:45 -050043#include <asm/div64.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070044
Mingming Caof7f4bcc2006-10-11 01:20:59 -070045EXPORT_SYMBOL(jbd2_journal_start);
46EXPORT_SYMBOL(jbd2_journal_restart);
47EXPORT_SYMBOL(jbd2_journal_extend);
48EXPORT_SYMBOL(jbd2_journal_stop);
49EXPORT_SYMBOL(jbd2_journal_lock_updates);
50EXPORT_SYMBOL(jbd2_journal_unlock_updates);
51EXPORT_SYMBOL(jbd2_journal_get_write_access);
52EXPORT_SYMBOL(jbd2_journal_get_create_access);
53EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070054EXPORT_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);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070072EXPORT_SYMBOL(jbd2_journal_abort);
73EXPORT_SYMBOL(jbd2_journal_errno);
74EXPORT_SYMBOL(jbd2_journal_ack_err);
75EXPORT_SYMBOL(jbd2_journal_clear_err);
76EXPORT_SYMBOL(jbd2_log_wait_commit);
77EXPORT_SYMBOL(jbd2_journal_start_commit);
78EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
79EXPORT_SYMBOL(jbd2_journal_wipe);
80EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
81EXPORT_SYMBOL(jbd2_journal_invalidatepage);
82EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
83EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040084EXPORT_SYMBOL(jbd2_journal_file_inode);
85EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
86EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
87EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070088
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070091
92/*
93 * Helper function used to manage commit timeouts
94 */
95
96static void commit_timeout(unsigned long __data)
97{
98 struct task_struct * p = (struct task_struct *) __data;
99
100 wake_up_process(p);
101}
102
103/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700104 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700105 * journal.
106 *
107 * This kernel thread is responsible for two things:
108 *
109 * 1) COMMIT: Every so often we need to commit the current state of the
110 * filesystem to disk. The journal thread is responsible for writing
111 * all of the metadata buffers to disk.
112 *
113 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
114 * of the data in that part of the log has been rewritten elsewhere on
115 * the disk. Flushing these old buffers to reclaim space in the log is
116 * known as checkpointing, and this thread is responsible for that job.
117 */
118
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700119static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700120{
121 journal_t *journal = arg;
122 transaction_t *transaction;
123
124 /*
125 * Set up an interval timer which can be used to trigger a commit wakeup
126 * after the commit interval expires
127 */
128 setup_timer(&journal->j_commit_timer, commit_timeout,
129 (unsigned long)current);
130
131 /* Record that the journal thread is running */
132 journal->j_task = current;
133 wake_up(&journal->j_wait_done_commit);
134
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700135 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700136 journal->j_commit_interval / HZ);
137
138 /*
139 * And now, wait forever for commit wakeup events.
140 */
141 spin_lock(&journal->j_state_lock);
142
143loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700144 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700145 goto end_loop;
146
147 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
148 journal->j_commit_sequence, journal->j_commit_request);
149
150 if (journal->j_commit_sequence != journal->j_commit_request) {
151 jbd_debug(1, "OK, requests differ\n");
152 spin_unlock(&journal->j_state_lock);
153 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700154 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700155 spin_lock(&journal->j_state_lock);
156 goto loop;
157 }
158
159 wake_up(&journal->j_wait_done_commit);
160 if (freezing(current)) {
161 /*
162 * The simpler the better. Flushing journal isn't a
163 * good idea, because that depends on threads that may
164 * be already stopped.
165 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700166 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700167 spin_unlock(&journal->j_state_lock);
168 refrigerator();
169 spin_lock(&journal->j_state_lock);
170 } else {
171 /*
172 * We assume on resume that commits are already there,
173 * so we don't sleep
174 */
175 DEFINE_WAIT(wait);
176 int should_sleep = 1;
177
178 prepare_to_wait(&journal->j_wait_commit, &wait,
179 TASK_INTERRUPTIBLE);
180 if (journal->j_commit_sequence != journal->j_commit_request)
181 should_sleep = 0;
182 transaction = journal->j_running_transaction;
183 if (transaction && time_after_eq(jiffies,
184 transaction->t_expires))
185 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700186 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700187 should_sleep = 0;
188 if (should_sleep) {
189 spin_unlock(&journal->j_state_lock);
190 schedule();
191 spin_lock(&journal->j_state_lock);
192 }
193 finish_wait(&journal->j_wait_commit, &wait);
194 }
195
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700196 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197
198 /*
199 * Were we woken up by a commit wakeup event?
200 */
201 transaction = journal->j_running_transaction;
202 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
203 journal->j_commit_request = transaction->t_tid;
204 jbd_debug(1, "woke because of timeout\n");
205 }
206 goto loop;
207
208end_loop:
209 spin_unlock(&journal->j_state_lock);
210 del_timer_sync(&journal->j_commit_timer);
211 journal->j_task = NULL;
212 wake_up(&journal->j_wait_done_commit);
213 jbd_debug(1, "Journal thread exiting.\n");
214 return 0;
215}
216
Pavel Emelianov97f06782007-05-08 00:30:42 -0700217static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700218{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700219 struct task_struct *t;
220
221 t = kthread_run(kjournald2, journal, "kjournald2");
222 if (IS_ERR(t))
223 return PTR_ERR(t);
224
Al Viro1076d172008-03-29 03:07:18 +0000225 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700226 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700227}
228
229static void journal_kill_thread(journal_t *journal)
230{
231 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700232 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700233
234 while (journal->j_task) {
235 wake_up(&journal->j_wait_commit);
236 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000237 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238 spin_lock(&journal->j_state_lock);
239 }
240 spin_unlock(&journal->j_state_lock);
241}
242
243/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700244 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700245 *
246 * Writes a metadata buffer to a given disk block. The actual IO is not
247 * performed but a new buffer_head is constructed which labels the data
248 * to be written with the correct destination disk block.
249 *
250 * Any magic-number escaping which needs to be done will cause a
251 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700252 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700253 * magic number is only written to the log for descripter blocks. In
254 * this case, we copy the data and replace the first word with 0, and we
255 * return a result code which indicates that this buffer needs to be
256 * marked as an escaped buffer in the corresponding log descriptor
257 * block. The missing word can then be restored when the block is read
258 * during recovery.
259 *
260 * If the source buffer has already been modified by a new transaction
261 * since we took the last commit snapshot, we use the frozen copy of
262 * that data for IO. If we end up using the existing buffer_head's data
263 * for the write, then we *have* to lock the buffer to prevent anyone
264 * else from using and possibly modifying it while the IO is in
265 * progress.
266 *
267 * The function returns a pointer to the buffer_heads to be used for IO.
268 *
269 * We assume that the journal has already been locked in this function.
270 *
271 * Return value:
272 * <0: Error
273 * >=0: Finished OK
274 *
275 * On success:
276 * Bit 0 set == escape performed on the data
277 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
278 */
279
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700280int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700281 struct journal_head *jh_in,
282 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700283 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284{
285 int need_copy_out = 0;
286 int done_copy_out = 0;
287 int do_escape = 0;
288 char *mapped_data;
289 struct buffer_head *new_bh;
290 struct journal_head *new_jh;
291 struct page *new_page;
292 unsigned int new_offset;
293 struct buffer_head *bh_in = jh2bh(jh_in);
294
295 /*
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
298 * to do any IO.
299 *
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
303 */
304 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305
306 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
307
308 /*
309 * If a new transaction has already done a buffer copy-out, then
310 * we use that version of the data for the commit.
311 */
312 jbd_lock_bh_state(bh_in);
313repeat:
314 if (jh_in->b_frozen_data) {
315 done_copy_out = 1;
316 new_page = virt_to_page(jh_in->b_frozen_data);
317 new_offset = offset_in_page(jh_in->b_frozen_data);
318 } else {
319 new_page = jh2bh(jh_in)->b_page;
320 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
321 }
322
323 mapped_data = kmap_atomic(new_page, KM_USER0);
324 /*
325 * Check for escaping
326 */
327 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700328 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700329 need_copy_out = 1;
330 do_escape = 1;
331 }
332 kunmap_atomic(mapped_data, KM_USER0);
333
334 /*
335 * Do we need to do a data copy?
336 */
337 if (need_copy_out && !done_copy_out) {
338 char *tmp;
339
340 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400341 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700342 jbd_lock_bh_state(bh_in);
343 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400344 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700345 goto repeat;
346 }
347
348 jh_in->b_frozen_data = tmp;
349 mapped_data = kmap_atomic(new_page, KM_USER0);
350 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
351 kunmap_atomic(mapped_data, KM_USER0);
352
353 new_page = virt_to_page(tmp);
354 new_offset = offset_in_page(tmp);
355 done_copy_out = 1;
356 }
357
358 /*
359 * Did we need to do an escaping? Now we've done all the
360 * copying, we can finally do so.
361 */
362 if (do_escape) {
363 mapped_data = kmap_atomic(new_page, KM_USER0);
364 *((unsigned int *)(mapped_data + new_offset)) = 0;
365 kunmap_atomic(mapped_data, KM_USER0);
366 }
367
368 /* keep subsequent assertions sane */
369 new_bh->b_state = 0;
370 init_buffer(new_bh, NULL, NULL);
371 atomic_set(&new_bh->b_count, 1);
372 jbd_unlock_bh_state(bh_in);
373
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700374 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700375
376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
383
384 *jh_out = new_jh;
385
386 /*
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
390 */
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700392 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700393 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700394 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700395
396 return do_escape | (done_copy_out << 1);
397}
398
399/*
400 * Allocation code for the journal file. Manage the space left in the
401 * journal, so that we can begin checkpointing when appropriate.
402 */
403
404/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700405 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700406 *
407 * Called with the journal already locked.
408 *
409 * Called under j_state_lock
410 */
411
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700412int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700413{
414 int left = journal->j_free;
415
416 assert_spin_locked(&journal->j_state_lock);
417
418 /*
419 * Be pessimistic here about the number of those free blocks which
420 * might be required for log descriptor control blocks.
421 */
422
423#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424
425 left -= MIN_LOG_RESERVED_BLOCKS;
426
427 if (left <= 0)
428 return 0;
429 left -= (left >> 3);
430 return left;
431}
432
433/*
434 * Called under j_state_lock. Returns true if a transaction was started.
435 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700436int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437{
438 /*
439 * Are we already doing a recent enough commit?
440 */
441 if (!tid_geq(journal->j_commit_request, target)) {
442 /*
443 * We want a new commit: OK, mark the request and wakup the
444 * commit thread. We do _not_ do the commit ourselves.
445 */
446
447 journal->j_commit_request = target;
448 jbd_debug(1, "JBD: requesting commit %d/%d\n",
449 journal->j_commit_request,
450 journal->j_commit_sequence);
451 wake_up(&journal->j_wait_commit);
452 return 1;
453 }
454 return 0;
455}
456
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700457int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700458{
459 int ret;
460
461 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700462 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700463 spin_unlock(&journal->j_state_lock);
464 return ret;
465}
466
467/*
468 * Force and wait upon a commit if the calling process is not within
469 * transaction. This is used for forcing out undo-protected data which contains
470 * bitmaps, when the fs is running out of space.
471 *
472 * We can only force the running transaction if we don't have an active handle;
473 * otherwise, we will deadlock.
474 *
475 * Returns true if a transaction was started.
476 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700477int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478{
479 transaction_t *transaction = NULL;
480 tid_t tid;
481
482 spin_lock(&journal->j_state_lock);
483 if (journal->j_running_transaction && !current->journal_info) {
484 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700485 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486 } else if (journal->j_committing_transaction)
487 transaction = journal->j_committing_transaction;
488
489 if (!transaction) {
490 spin_unlock(&journal->j_state_lock);
491 return 0; /* Nothing to retry */
492 }
493
494 tid = transaction->t_tid;
495 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700496 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497 return 1;
498}
499
500/*
501 * Start a commit of the current running transaction (if any). Returns true
502 * if a transaction was started, and fills its tid in at *ptid
503 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700504int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505{
506 int ret = 0;
507
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction) {
510 tid_t tid = journal->j_running_transaction->t_tid;
511
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700512 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700513 if (ret && ptid)
514 *ptid = tid;
515 } else if (journal->j_committing_transaction && ptid) {
516 /*
517 * If ext3_write_super() recently started a commit, then we
518 * have to wait for completion of that transaction
519 */
520 *ptid = journal->j_committing_transaction->t_tid;
521 ret = 1;
522 }
523 spin_unlock(&journal->j_state_lock);
524 return ret;
525}
526
527/*
528 * Wait for a specified commit to complete.
529 * The caller may not hold the journal lock.
530 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700531int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532{
533 int err = 0;
534
Jose R. Santose23291b2007-07-18 08:57:06 -0400535#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 spin_lock(&journal->j_state_lock);
537 if (!tid_geq(journal->j_commit_request, tid)) {
538 printk(KERN_EMERG
539 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400540 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700541 }
542 spin_unlock(&journal->j_state_lock);
543#endif
544 spin_lock(&journal->j_state_lock);
545 while (tid_gt(tid, journal->j_commit_sequence)) {
546 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
547 tid, journal->j_commit_sequence);
548 wake_up(&journal->j_wait_commit);
549 spin_unlock(&journal->j_state_lock);
550 wait_event(journal->j_wait_done_commit,
551 !tid_gt(tid, journal->j_commit_sequence));
552 spin_lock(&journal->j_state_lock);
553 }
554 spin_unlock(&journal->j_state_lock);
555
556 if (unlikely(is_journal_aborted(journal))) {
557 printk(KERN_EMERG "journal commit I/O error\n");
558 err = -EIO;
559 }
560 return err;
561}
562
563/*
564 * Log buffer allocation routines:
565 */
566
Mingming Cao18eba7a2006-10-11 01:21:13 -0700567int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568{
569 unsigned long blocknr;
570
571 spin_lock(&journal->j_state_lock);
572 J_ASSERT(journal->j_free > 1);
573
574 blocknr = journal->j_head;
575 journal->j_head++;
576 journal->j_free--;
577 if (journal->j_head == journal->j_last)
578 journal->j_head = journal->j_first;
579 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700580 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700581}
582
583/*
584 * Conversion of logical to physical block numbers for the journal
585 *
586 * On external journals the journal blocks are identity-mapped, so
587 * this is a no-op. If needed, we can use j_blk_offset - everything is
588 * ready.
589 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700590int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700591 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592{
593 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700594 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595
596 if (journal->j_inode) {
597 ret = bmap(journal->j_inode, blocknr);
598 if (ret)
599 *retp = ret;
600 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700601 printk(KERN_ALERT "%s: journal block not found "
602 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400603 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604 err = -EIO;
605 __journal_abort_soft(journal, err);
606 }
607 } else {
608 *retp = blocknr; /* +journal->j_blk_offset */
609 }
610 return err;
611}
612
613/*
614 * We play buffer_head aliasing tricks to write data/metadata blocks to
615 * the journal without copying their contents, but for journal
616 * descriptor blocks we do need to generate bona fide buffers.
617 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700618 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
620 * But we don't bother doing that, so there will be coherency problems with
621 * mmaps of blockdevs which hold live JBD-controlled filesystems.
622 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700623struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700624{
625 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700626 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700627 int err;
628
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700629 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700630
631 if (err)
632 return NULL;
633
634 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
635 lock_buffer(bh);
636 memset(bh->b_data, 0, journal->j_blocksize);
637 set_buffer_uptodate(bh);
638 unlock_buffer(bh);
639 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700640 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700641}
642
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500643struct jbd2_stats_proc_session {
644 journal_t *journal;
645 struct transaction_stats_s *stats;
646 int start;
647 int max;
648};
649
650static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
651 struct transaction_stats_s *ts,
652 int first)
653{
654 if (ts == s->stats + s->max)
655 ts = s->stats;
656 if (!first && ts == s->stats + s->start)
657 return NULL;
658 while (ts->ts_type == 0) {
659 ts++;
660 if (ts == s->stats + s->max)
661 ts = s->stats;
662 if (ts == s->stats + s->start)
663 return NULL;
664 }
665 return ts;
666
667}
668
669static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
670{
671 struct jbd2_stats_proc_session *s = seq->private;
672 struct transaction_stats_s *ts;
673 int l = *pos;
674
675 if (l == 0)
676 return SEQ_START_TOKEN;
677 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
678 if (!ts)
679 return NULL;
680 l--;
681 while (l) {
682 ts = jbd2_history_skip_empty(s, ++ts, 0);
683 if (!ts)
684 break;
685 l--;
686 }
687 return ts;
688}
689
690static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
691{
692 struct jbd2_stats_proc_session *s = seq->private;
693 struct transaction_stats_s *ts = v;
694
695 ++*pos;
696 if (v == SEQ_START_TOKEN)
697 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
698 else
699 return jbd2_history_skip_empty(s, ++ts, 0);
700}
701
702static int jbd2_seq_history_show(struct seq_file *seq, void *v)
703{
704 struct transaction_stats_s *ts = v;
705 if (v == SEQ_START_TOKEN) {
706 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
707 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
708 "wait", "run", "lock", "flush", "log", "hndls",
709 "block", "inlog", "ctime", "write", "drop",
710 "close");
711 return 0;
712 }
713 if (ts->ts_type == JBD2_STATS_RUN)
714 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
715 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
716 jiffies_to_msecs(ts->u.run.rs_wait),
717 jiffies_to_msecs(ts->u.run.rs_running),
718 jiffies_to_msecs(ts->u.run.rs_locked),
719 jiffies_to_msecs(ts->u.run.rs_flushing),
720 jiffies_to_msecs(ts->u.run.rs_logging),
721 ts->u.run.rs_handle_count,
722 ts->u.run.rs_blocks,
723 ts->u.run.rs_blocks_logged);
724 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
725 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
726 "C", ts->ts_tid, " ",
727 jiffies_to_msecs(ts->u.chp.cs_chp_time),
728 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
729 ts->u.chp.cs_forced_to_close);
730 else
731 J_ASSERT(0);
732 return 0;
733}
734
735static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
736{
737}
738
739static struct seq_operations jbd2_seq_history_ops = {
740 .start = jbd2_seq_history_start,
741 .next = jbd2_seq_history_next,
742 .stop = jbd2_seq_history_stop,
743 .show = jbd2_seq_history_show,
744};
745
746static int jbd2_seq_history_open(struct inode *inode, struct file *file)
747{
748 journal_t *journal = PDE(inode)->data;
749 struct jbd2_stats_proc_session *s;
750 int rc, size;
751
752 s = kmalloc(sizeof(*s), GFP_KERNEL);
753 if (s == NULL)
754 return -ENOMEM;
755 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
756 s->stats = kmalloc(size, GFP_KERNEL);
757 if (s->stats == NULL) {
758 kfree(s);
759 return -ENOMEM;
760 }
761 spin_lock(&journal->j_history_lock);
762 memcpy(s->stats, journal->j_history, size);
763 s->max = journal->j_history_max;
764 s->start = journal->j_history_cur % s->max;
765 spin_unlock(&journal->j_history_lock);
766
767 rc = seq_open(file, &jbd2_seq_history_ops);
768 if (rc == 0) {
769 struct seq_file *m = file->private_data;
770 m->private = s;
771 } else {
772 kfree(s->stats);
773 kfree(s);
774 }
775 return rc;
776
777}
778
779static int jbd2_seq_history_release(struct inode *inode, struct file *file)
780{
781 struct seq_file *seq = file->private_data;
782 struct jbd2_stats_proc_session *s = seq->private;
783
784 kfree(s->stats);
785 kfree(s);
786 return seq_release(inode, file);
787}
788
789static struct file_operations jbd2_seq_history_fops = {
790 .owner = THIS_MODULE,
791 .open = jbd2_seq_history_open,
792 .read = seq_read,
793 .llseek = seq_lseek,
794 .release = jbd2_seq_history_release,
795};
796
797static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
798{
799 return *pos ? NULL : SEQ_START_TOKEN;
800}
801
802static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
803{
804 return NULL;
805}
806
807static int jbd2_seq_info_show(struct seq_file *seq, void *v)
808{
809 struct jbd2_stats_proc_session *s = seq->private;
810
811 if (v != SEQ_START_TOKEN)
812 return 0;
813 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
814 s->stats->ts_tid,
815 s->journal->j_max_transaction_buffers);
816 if (s->stats->ts_tid == 0)
817 return 0;
818 seq_printf(seq, "average: \n %ums waiting for transaction\n",
819 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
820 seq_printf(seq, " %ums running transaction\n",
821 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
822 seq_printf(seq, " %ums transaction was being locked\n",
823 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
824 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
825 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
826 seq_printf(seq, " %ums logging transaction\n",
827 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Theodore Ts'od7cfa462008-12-17 00:20:45 -0500828 seq_printf(seq, " %luus average transaction commit time\n",
829 do_div(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500830 seq_printf(seq, " %lu handles per transaction\n",
831 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
832 seq_printf(seq, " %lu blocks per transaction\n",
833 s->stats->u.run.rs_blocks / s->stats->ts_tid);
834 seq_printf(seq, " %lu logged blocks per transaction\n",
835 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
836 return 0;
837}
838
839static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
840{
841}
842
843static struct seq_operations jbd2_seq_info_ops = {
844 .start = jbd2_seq_info_start,
845 .next = jbd2_seq_info_next,
846 .stop = jbd2_seq_info_stop,
847 .show = jbd2_seq_info_show,
848};
849
850static int jbd2_seq_info_open(struct inode *inode, struct file *file)
851{
852 journal_t *journal = PDE(inode)->data;
853 struct jbd2_stats_proc_session *s;
854 int rc, size;
855
856 s = kmalloc(sizeof(*s), GFP_KERNEL);
857 if (s == NULL)
858 return -ENOMEM;
859 size = sizeof(struct transaction_stats_s);
860 s->stats = kmalloc(size, GFP_KERNEL);
861 if (s->stats == NULL) {
862 kfree(s);
863 return -ENOMEM;
864 }
865 spin_lock(&journal->j_history_lock);
866 memcpy(s->stats, &journal->j_stats, size);
867 s->journal = journal;
868 spin_unlock(&journal->j_history_lock);
869
870 rc = seq_open(file, &jbd2_seq_info_ops);
871 if (rc == 0) {
872 struct seq_file *m = file->private_data;
873 m->private = s;
874 } else {
875 kfree(s->stats);
876 kfree(s);
877 }
878 return rc;
879
880}
881
882static int jbd2_seq_info_release(struct inode *inode, struct file *file)
883{
884 struct seq_file *seq = file->private_data;
885 struct jbd2_stats_proc_session *s = seq->private;
886 kfree(s->stats);
887 kfree(s);
888 return seq_release(inode, file);
889}
890
891static struct file_operations jbd2_seq_info_fops = {
892 .owner = THIS_MODULE,
893 .open = jbd2_seq_info_open,
894 .read = seq_read,
895 .llseek = seq_lseek,
896 .release = jbd2_seq_info_release,
897};
898
899static struct proc_dir_entry *proc_jbd2_stats;
900
901static void jbd2_stats_proc_init(journal_t *journal)
902{
Theodore Ts'o05496762008-09-16 14:36:17 -0400903 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500904 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700905 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
906 &jbd2_seq_history_fops, journal);
907 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
908 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500909 }
910}
911
912static void jbd2_stats_proc_exit(journal_t *journal)
913{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500914 remove_proc_entry("info", journal->j_proc_entry);
915 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400916 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500917}
918
919static void journal_init_stats(journal_t *journal)
920{
921 int size;
922
923 if (!proc_jbd2_stats)
924 return;
925
926 journal->j_history_max = 100;
927 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
928 journal->j_history = kzalloc(size, GFP_KERNEL);
929 if (!journal->j_history) {
930 journal->j_history_max = 0;
931 return;
932 }
933 spin_lock_init(&journal->j_history_lock);
934}
935
Dave Kleikamp470decc2006-10-11 01:20:57 -0700936/*
937 * Management for journal control blocks: functions to create and
938 * destroy journal_t structures, and to initialise and read existing
939 * journal blocks from disk. */
940
941/* First: create and setup a journal_t object in memory. We initialise
942 * very few fields yet: that has to wait until we have created the
943 * journal structures from from scratch, or loaded them from disk. */
944
945static journal_t * journal_init_common (void)
946{
947 journal_t *journal;
948 int err;
949
Mingming Caod802ffa2007-10-16 18:38:25 -0400950 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700951 if (!journal)
952 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700953
954 init_waitqueue_head(&journal->j_wait_transaction_locked);
955 init_waitqueue_head(&journal->j_wait_logspace);
956 init_waitqueue_head(&journal->j_wait_done_commit);
957 init_waitqueue_head(&journal->j_wait_checkpoint);
958 init_waitqueue_head(&journal->j_wait_commit);
959 init_waitqueue_head(&journal->j_wait_updates);
960 mutex_init(&journal->j_barrier);
961 mutex_init(&journal->j_checkpoint_mutex);
962 spin_lock_init(&journal->j_revoke_lock);
963 spin_lock_init(&journal->j_list_lock);
964 spin_lock_init(&journal->j_state_lock);
965
Mingming Caocd02ff02007-10-16 18:38:25 -0400966 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500967 journal->j_min_batch_time = 0;
968 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700969
970 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700971 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700972
973 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700974 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975 if (err) {
976 kfree(journal);
977 goto fail;
978 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500979
980 journal_init_stats(journal);
981
Dave Kleikamp470decc2006-10-11 01:20:57 -0700982 return journal;
983fail:
984 return NULL;
985}
986
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700987/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700988 *
989 * Create a journal structure assigned some fixed set of disk blocks to
990 * the journal. We don't actually touch those disk blocks yet, but we
991 * need to set up all of the mapping information to tell the journaling
992 * system where the journal blocks are.
993 *
994 */
995
996/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400997 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700998 * @bdev: Block device on which to create the journal
999 * @fs_dev: Device which hold journalled filesystem for this journal.
1000 * @start: Block nr Start of journal.
1001 * @len: Length of the journal in blocks.
1002 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001003 *
1004 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001005 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001006 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001007 * range of blocks on an arbitrary block device.
1008 *
1009 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001010journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001011 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001012 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001013{
1014 journal_t *journal = journal_init_common();
1015 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001016 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001017 int n;
1018
1019 if (!journal)
1020 return NULL;
1021
1022 /* journal descriptor can store up to n blocks -bzzz */
1023 journal->j_blocksize = blocksize;
1024 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1025 journal->j_wbufsize = n;
1026 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1027 if (!journal->j_wbuf) {
1028 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001029 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001030 kfree(journal);
1031 journal = NULL;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001032 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001033 }
1034 journal->j_dev = bdev;
1035 journal->j_fs_dev = fs_dev;
1036 journal->j_blk_offset = start;
1037 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001038 bdevname(journal->j_dev, journal->j_devname);
1039 p = journal->j_devname;
1040 while ((p = strchr(p, '/')))
1041 *p = '!';
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001042 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043
1044 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1045 J_ASSERT(bh != NULL);
1046 journal->j_sb_buffer = bh;
1047 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001048out:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049 return journal;
1050}
1051
1052/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001053 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001054 * @inode: An inode to create the journal in
1055 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001056 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001057 * the journal. The inode must exist already, must support bmap() and
1058 * must have all data blocks preallocated.
1059 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001060journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001061{
1062 struct buffer_head *bh;
1063 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001064 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001065 int err;
1066 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001067 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001068
1069 if (!journal)
1070 return NULL;
1071
1072 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1073 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001074 bdevname(journal->j_dev, journal->j_devname);
1075 p = journal->j_devname;
1076 while ((p = strchr(p, '/')))
1077 *p = '!';
1078 p = journal->j_devname + strlen(journal->j_devname);
1079 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001080 jbd_debug(1,
1081 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1082 journal, inode->i_sb->s_id, inode->i_ino,
1083 (long long) inode->i_size,
1084 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1085
1086 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1087 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001088 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001089
1090 /* journal descriptor can store up to n blocks -bzzz */
1091 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1092 journal->j_wbufsize = n;
1093 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1094 if (!journal->j_wbuf) {
1095 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001096 __func__);
Sami Liedes24238402008-11-02 19:23:30 -05001097 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098 kfree(journal);
1099 return NULL;
1100 }
1101
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001102 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001103 /* If that failed, give up */
1104 if (err) {
1105 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001106 __func__);
Sami Liedes24238402008-11-02 19:23:30 -05001107 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001108 kfree(journal);
1109 return NULL;
1110 }
1111
1112 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1113 J_ASSERT(bh != NULL);
1114 journal->j_sb_buffer = bh;
1115 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1116
1117 return journal;
1118}
1119
1120/*
1121 * If the journal init or create aborts, we need to mark the journal
1122 * superblock as being NULL to prevent the journal destroy from writing
1123 * back a bogus superblock.
1124 */
1125static void journal_fail_superblock (journal_t *journal)
1126{
1127 struct buffer_head *bh = journal->j_sb_buffer;
1128 brelse(bh);
1129 journal->j_sb_buffer = NULL;
1130}
1131
1132/*
1133 * Given a journal_t structure, initialise the various fields for
1134 * startup of a new journaling session. We use this both when creating
1135 * a journal, and after recovering an old journal to reset it for
1136 * subsequent use.
1137 */
1138
1139static int journal_reset(journal_t *journal)
1140{
1141 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001142 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143
1144 first = be32_to_cpu(sb->s_first);
1145 last = be32_to_cpu(sb->s_maxlen);
1146
1147 journal->j_first = first;
1148 journal->j_last = last;
1149
1150 journal->j_head = first;
1151 journal->j_tail = first;
1152 journal->j_free = last - first;
1153
1154 journal->j_tail_sequence = journal->j_transaction_sequence;
1155 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1156 journal->j_commit_request = journal->j_commit_sequence;
1157
1158 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1159
1160 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001161 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001162 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001163}
1164
1165/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001166 * int jbd2_journal_create() - Initialise the new journal file
Dave Kleikamp470decc2006-10-11 01:20:57 -07001167 * @journal: Journal to create. This structure must have been initialised
1168 *
1169 * Given a journal_t structure which tells us which disk blocks we can
1170 * use, create a new journal superblock and initialise all of the
1171 * journal fields from scratch.
1172 **/
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001173int jbd2_journal_create(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001174{
Mingming Cao18eba7a2006-10-11 01:21:13 -07001175 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001176 struct buffer_head *bh;
1177 journal_superblock_t *sb;
1178 int i, err;
1179
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1182 journal->j_maxlen);
1183 journal_fail_superblock(journal);
1184 return -EINVAL;
1185 }
1186
1187 if (journal->j_inode == NULL) {
1188 /*
1189 * We don't know what block to start at!
1190 */
1191 printk(KERN_EMERG
1192 "%s: creation of journal on external device!\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001193 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001194 BUG();
1195 }
1196
1197 /* Zero out the entire journal on disk. We cannot afford to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001198 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001199 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1200 for (i = 0; i < journal->j_maxlen; i++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001201 err = jbd2_journal_bmap(journal, i, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001202 if (err)
1203 return err;
1204 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1205 lock_buffer(bh);
1206 memset (bh->b_data, 0, journal->j_blocksize);
1207 BUFFER_TRACE(bh, "marking dirty");
1208 mark_buffer_dirty(bh);
1209 BUFFER_TRACE(bh, "marking uptodate");
1210 set_buffer_uptodate(bh);
1211 unlock_buffer(bh);
1212 __brelse(bh);
1213 }
1214
1215 sync_blockdev(journal->j_dev);
1216 jbd_debug(1, "JBD: journal cleared.\n");
1217
1218 /* OK, fill in the initial static fields in the new superblock */
1219 sb = journal->j_superblock;
1220
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001221 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1222 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001223
1224 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1225 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1226 sb->s_first = cpu_to_be32(1);
1227
1228 journal->j_transaction_sequence = 1;
1229
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001230 journal->j_flags &= ~JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 journal->j_format_version = 2;
1232
1233 return journal_reset(journal);
1234}
1235
1236/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001237 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238 * @journal: The journal to update.
1239 * @wait: Set to '0' if you don't want to wait for IO completion.
1240 *
1241 * Update a journal's dynamic superblock fields and write it to disk,
1242 * optionally waiting for the IO to complete.
1243 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001244void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001245{
1246 journal_superblock_t *sb = journal->j_superblock;
1247 struct buffer_head *bh = journal->j_sb_buffer;
1248
1249 /*
1250 * As a special case, if the on-disk copy is already marked as needing
1251 * no recovery (s_start == 0) and there are no outstanding transactions
1252 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001253 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 * attempting a write to a potential-readonly device.
1255 */
1256 if (sb->s_start == 0 && journal->j_tail_sequence ==
1257 journal->j_transaction_sequence) {
1258 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1259 "(start %ld, seq %d, errno %d)\n",
1260 journal->j_tail, journal->j_tail_sequence,
1261 journal->j_errno);
1262 goto out;
1263 }
1264
Theodore Ts'o914258b2008-10-06 21:35:40 -04001265 if (buffer_write_io_error(bh)) {
1266 /*
1267 * Oh, dear. A previous attempt to write the journal
1268 * superblock failed. This could happen because the
1269 * USB device was yanked out. Or it could happen to
1270 * be a transient write error and maybe the block will
1271 * be remapped. Nothing we can do but to retry the
1272 * write and hope for the best.
1273 */
1274 printk(KERN_ERR "JBD2: previous I/O error detected "
1275 "for journal superblock update for %s.\n",
1276 journal->j_devname);
1277 clear_buffer_write_io_error(bh);
1278 set_buffer_uptodate(bh);
1279 }
1280
Dave Kleikamp470decc2006-10-11 01:20:57 -07001281 spin_lock(&journal->j_state_lock);
1282 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1283 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1284
1285 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1286 sb->s_start = cpu_to_be32(journal->j_tail);
1287 sb->s_errno = cpu_to_be32(journal->j_errno);
1288 spin_unlock(&journal->j_state_lock);
1289
1290 BUFFER_TRACE(bh, "marking dirty");
1291 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001292 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001293 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001294 if (buffer_write_io_error(bh)) {
1295 printk(KERN_ERR "JBD2: I/O error detected "
1296 "when updating journal superblock for %s.\n",
1297 journal->j_devname);
1298 clear_buffer_write_io_error(bh);
1299 set_buffer_uptodate(bh);
1300 }
1301 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001302 ll_rw_block(SWRITE, 1, &bh);
1303
1304out:
1305 /* If we have just flushed the log (by marking s_start==0), then
1306 * any future commit will have to be careful to update the
1307 * superblock again to re-record the true start of the log. */
1308
1309 spin_lock(&journal->j_state_lock);
1310 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001311 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001312 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001313 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001314 spin_unlock(&journal->j_state_lock);
1315}
1316
1317/*
1318 * Read the superblock for a given journal, performing initial
1319 * validation of the format.
1320 */
1321
1322static int journal_get_superblock(journal_t *journal)
1323{
1324 struct buffer_head *bh;
1325 journal_superblock_t *sb;
1326 int err = -EIO;
1327
1328 bh = journal->j_sb_buffer;
1329
1330 J_ASSERT(bh != NULL);
1331 if (!buffer_uptodate(bh)) {
1332 ll_rw_block(READ, 1, &bh);
1333 wait_on_buffer(bh);
1334 if (!buffer_uptodate(bh)) {
1335 printk (KERN_ERR
1336 "JBD: IO error reading journal superblock\n");
1337 goto out;
1338 }
1339 }
1340
1341 sb = journal->j_superblock;
1342
1343 err = -EINVAL;
1344
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001345 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001346 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1347 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1348 goto out;
1349 }
1350
1351 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001352 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001353 journal->j_format_version = 1;
1354 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001355 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001356 journal->j_format_version = 2;
1357 break;
1358 default:
1359 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1360 goto out;
1361 }
1362
1363 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1364 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1365 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1366 printk (KERN_WARNING "JBD: journal file too short\n");
1367 goto out;
1368 }
1369
1370 return 0;
1371
1372out:
1373 journal_fail_superblock(journal);
1374 return err;
1375}
1376
1377/*
1378 * Load the on-disk journal superblock and read the key fields into the
1379 * journal_t.
1380 */
1381
1382static int load_superblock(journal_t *journal)
1383{
1384 int err;
1385 journal_superblock_t *sb;
1386
1387 err = journal_get_superblock(journal);
1388 if (err)
1389 return err;
1390
1391 sb = journal->j_superblock;
1392
1393 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1394 journal->j_tail = be32_to_cpu(sb->s_start);
1395 journal->j_first = be32_to_cpu(sb->s_first);
1396 journal->j_last = be32_to_cpu(sb->s_maxlen);
1397 journal->j_errno = be32_to_cpu(sb->s_errno);
1398
1399 return 0;
1400}
1401
1402
1403/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001404 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 * @journal: Journal to act on.
1406 *
1407 * Given a journal_t structure which tells us which disk blocks contain
1408 * a journal, read the journal from disk to initialise the in-memory
1409 * structures.
1410 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001411int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001412{
1413 int err;
1414 journal_superblock_t *sb;
1415
1416 err = load_superblock(journal);
1417 if (err)
1418 return err;
1419
1420 sb = journal->j_superblock;
1421 /* If this is a V2 superblock, then we have to check the
1422 * features flags on it. */
1423
1424 if (journal->j_format_version >= 2) {
1425 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001426 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001428 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001429 printk (KERN_WARNING
1430 "JBD: Unrecognised features on journal\n");
1431 return -EINVAL;
1432 }
1433 }
1434
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435 /* Let the recovery code check whether it needs to recover any
1436 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001437 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438 goto recovery_error;
1439
1440 /* OK, we've finished with the dynamic journal bits:
1441 * reinitialise the dynamic contents of the superblock in memory
1442 * and reset them on disk. */
1443 if (journal_reset(journal))
1444 goto recovery_error;
1445
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001446 journal->j_flags &= ~JBD2_ABORT;
1447 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001448 return 0;
1449
1450recovery_error:
1451 printk (KERN_WARNING "JBD: recovery failed\n");
1452 return -EIO;
1453}
1454
1455/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001456 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001457 * @journal: Journal to act on.
1458 *
1459 * Release a journal_t structure once it is no longer in use by the
1460 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001461 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001463int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001464{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001465 int err = 0;
1466
Dave Kleikamp470decc2006-10-11 01:20:57 -07001467 /* Wait for the commit thread to wake up and die. */
1468 journal_kill_thread(journal);
1469
1470 /* Force a final log commit */
1471 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001472 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001473
1474 /* Force any old transactions to disk */
1475
1476 /* Totally anal locking here... */
1477 spin_lock(&journal->j_list_lock);
1478 while (journal->j_checkpoint_transactions != NULL) {
1479 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001480 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001481 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001482 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001483 spin_lock(&journal->j_list_lock);
1484 }
1485
1486 J_ASSERT(journal->j_running_transaction == NULL);
1487 J_ASSERT(journal->j_committing_transaction == NULL);
1488 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1489 spin_unlock(&journal->j_list_lock);
1490
Dave Kleikamp470decc2006-10-11 01:20:57 -07001491 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001492 if (!is_journal_aborted(journal)) {
1493 /* We can now mark the journal as empty. */
1494 journal->j_tail = 0;
1495 journal->j_tail_sequence =
1496 ++journal->j_transaction_sequence;
1497 jbd2_journal_update_superblock(journal, 1);
1498 } else {
1499 err = -EIO;
1500 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001501 brelse(journal->j_sb_buffer);
1502 }
1503
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001504 if (journal->j_proc_entry)
1505 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001506 if (journal->j_inode)
1507 iput(journal->j_inode);
1508 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001509 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001510 kfree(journal->j_wbuf);
1511 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001512
1513 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001514}
1515
1516
1517/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001518 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001519 * @journal: Journal to check.
1520 * @compat: bitmask of compatible features
1521 * @ro: bitmask of features that force read-only mount
1522 * @incompat: bitmask of incompatible features
1523 *
1524 * Check whether the journal uses all of a given set of
1525 * features. Return true (non-zero) if it does.
1526 **/
1527
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001528int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001529 unsigned long ro, unsigned long incompat)
1530{
1531 journal_superblock_t *sb;
1532
1533 if (!compat && !ro && !incompat)
1534 return 1;
1535 if (journal->j_format_version == 1)
1536 return 0;
1537
1538 sb = journal->j_superblock;
1539
1540 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1541 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1542 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1543 return 1;
1544
1545 return 0;
1546}
1547
1548/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001549 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001550 * @journal: Journal to check.
1551 * @compat: bitmask of compatible features
1552 * @ro: bitmask of features that force read-only mount
1553 * @incompat: bitmask of incompatible features
1554 *
1555 * Check whether the journaling code supports the use of
1556 * all of a given set of features on this journal. Return true
1557 * (non-zero) if it can. */
1558
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001559int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001560 unsigned long ro, unsigned long incompat)
1561{
1562 journal_superblock_t *sb;
1563
1564 if (!compat && !ro && !incompat)
1565 return 1;
1566
1567 sb = journal->j_superblock;
1568
1569 /* We can support any known requested features iff the
1570 * superblock is in version 2. Otherwise we fail to support any
1571 * extended sb features. */
1572
1573 if (journal->j_format_version != 2)
1574 return 0;
1575
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001576 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1577 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1578 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001579 return 1;
1580
1581 return 0;
1582}
1583
1584/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001585 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001586 * @journal: Journal to act on.
1587 * @compat: bitmask of compatible features
1588 * @ro: bitmask of features that force read-only mount
1589 * @incompat: bitmask of incompatible features
1590 *
1591 * Mark a given journal feature as present on the
1592 * superblock. Returns true if the requested features could be set.
1593 *
1594 */
1595
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001596int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001597 unsigned long ro, unsigned long incompat)
1598{
1599 journal_superblock_t *sb;
1600
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001601 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001602 return 1;
1603
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001604 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001605 return 0;
1606
1607 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1608 compat, ro, incompat);
1609
1610 sb = journal->j_superblock;
1611
1612 sb->s_feature_compat |= cpu_to_be32(compat);
1613 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1614 sb->s_feature_incompat |= cpu_to_be32(incompat);
1615
1616 return 1;
1617}
1618
Girish Shilamkar818d2762008-01-28 23:58:27 -05001619/*
1620 * jbd2_journal_clear_features () - Clear a given journal feature in the
1621 * superblock
1622 * @journal: Journal to act on.
1623 * @compat: bitmask of compatible features
1624 * @ro: bitmask of features that force read-only mount
1625 * @incompat: bitmask of incompatible features
1626 *
1627 * Clear a given journal feature as present on the
1628 * superblock.
1629 */
1630void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1631 unsigned long ro, unsigned long incompat)
1632{
1633 journal_superblock_t *sb;
1634
1635 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1636 compat, ro, incompat);
1637
1638 sb = journal->j_superblock;
1639
1640 sb->s_feature_compat &= ~cpu_to_be32(compat);
1641 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1642 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1643}
1644EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645
1646/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001647 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001648 * @journal: Journal to act on.
1649 *
1650 * Given an initialised but unloaded journal struct, poke about in the
1651 * on-disk structure to update it to the most recent supported version.
1652 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001653int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001654{
1655 journal_superblock_t *sb;
1656 int err;
1657
1658 err = journal_get_superblock(journal);
1659 if (err)
1660 return err;
1661
1662 sb = journal->j_superblock;
1663
1664 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001665 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001667 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001668 return journal_convert_superblock_v1(journal, sb);
1669 default:
1670 break;
1671 }
1672 return -EINVAL;
1673}
1674
1675static int journal_convert_superblock_v1(journal_t *journal,
1676 journal_superblock_t *sb)
1677{
1678 int offset, blocksize;
1679 struct buffer_head *bh;
1680
1681 printk(KERN_WARNING
1682 "JBD: Converting superblock from version 1 to 2.\n");
1683
1684 /* Pre-initialise new fields to zero */
1685 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1686 blocksize = be32_to_cpu(sb->s_blocksize);
1687 memset(&sb->s_feature_compat, 0, blocksize-offset);
1688
1689 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001690 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001691 journal->j_format_version = 2;
1692
1693 bh = journal->j_sb_buffer;
1694 BUFFER_TRACE(bh, "marking dirty");
1695 mark_buffer_dirty(bh);
1696 sync_dirty_buffer(bh);
1697 return 0;
1698}
1699
1700
1701/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001702 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001703 * @journal: Journal to act on.
1704 *
1705 * Flush all data for a given journal to disk and empty the journal.
1706 * Filesystems can use this when remounting readonly to ensure that
1707 * recovery does not need to happen on remount.
1708 */
1709
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001710int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001711{
1712 int err = 0;
1713 transaction_t *transaction = NULL;
1714 unsigned long old_tail;
1715
1716 spin_lock(&journal->j_state_lock);
1717
1718 /* Force everything buffered to the log... */
1719 if (journal->j_running_transaction) {
1720 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 } else if (journal->j_committing_transaction)
1723 transaction = journal->j_committing_transaction;
1724
1725 /* Wait for the log commit to complete... */
1726 if (transaction) {
1727 tid_t tid = transaction->t_tid;
1728
1729 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001730 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001731 } else {
1732 spin_unlock(&journal->j_state_lock);
1733 }
1734
1735 /* ...and flush everything in the log out to disk. */
1736 spin_lock(&journal->j_list_lock);
1737 while (!err && journal->j_checkpoint_transactions != NULL) {
1738 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001739 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001740 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001741 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001742 spin_lock(&journal->j_list_lock);
1743 }
1744 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001745
1746 if (is_journal_aborted(journal))
1747 return -EIO;
1748
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001749 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750
1751 /* Finally, mark the journal as really needing no recovery.
1752 * This sets s_start==0 in the underlying superblock, which is
1753 * the magic code for a fully-recovered superblock. Any future
1754 * commits of data to the journal will restore the current
1755 * s_start value. */
1756 spin_lock(&journal->j_state_lock);
1757 old_tail = journal->j_tail;
1758 journal->j_tail = 0;
1759 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001760 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001761 spin_lock(&journal->j_state_lock);
1762 journal->j_tail = old_tail;
1763
1764 J_ASSERT(!journal->j_running_transaction);
1765 J_ASSERT(!journal->j_committing_transaction);
1766 J_ASSERT(!journal->j_checkpoint_transactions);
1767 J_ASSERT(journal->j_head == journal->j_tail);
1768 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1769 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001770 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001771}
1772
1773/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001774 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001775 * @journal: Journal to act on.
1776 * @write: flag (see below)
1777 *
1778 * Wipe out all of the contents of a journal, safely. This will produce
1779 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001780 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001781 *
1782 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1783 * we merely suppress recovery.
1784 */
1785
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001786int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787{
1788 journal_superblock_t *sb;
1789 int err = 0;
1790
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001791 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001792
1793 err = load_superblock(journal);
1794 if (err)
1795 return err;
1796
1797 sb = journal->j_superblock;
1798
1799 if (!journal->j_tail)
1800 goto no_recovery;
1801
1802 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1803 write ? "Clearing" : "Ignoring");
1804
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001805 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001807 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808
1809 no_recovery:
1810 return err;
1811}
1812
1813/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814 * Journal abort has very specific semantics, which we describe
1815 * for journal abort.
1816 *
1817 * Two internal function, which provide abort to te jbd layer
1818 * itself are here.
1819 */
1820
1821/*
1822 * Quick version for internal journal use (doesn't lock the journal).
1823 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1824 * and don't attempt to make any other journal updates.
1825 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001826void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001827{
1828 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001829
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001830 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001831 return;
1832
1833 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001834 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001835
1836 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001837 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001838 transaction = journal->j_running_transaction;
1839 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001840 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841 spin_unlock(&journal->j_state_lock);
1842}
1843
1844/* Soft abort: record the abort error status in the journal superblock,
1845 * but don't do any other IO. */
1846static void __journal_abort_soft (journal_t *journal, int errno)
1847{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001848 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849 return;
1850
1851 if (!journal->j_errno)
1852 journal->j_errno = errno;
1853
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001854 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855
1856 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001857 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858}
1859
1860/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001861 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 * @journal: the journal to shutdown.
1863 * @errno: an error number to record in the journal indicating
1864 * the reason for the shutdown.
1865 *
1866 * Perform a complete, immediate shutdown of the ENTIRE
1867 * journal (not of a single transaction). This operation cannot be
1868 * undone without closing and reopening the journal.
1869 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001870 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001871 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1872 * mode.
1873 *
1874 * Journal abort has very specific semantics. Any existing dirty,
1875 * unjournaled buffers in the main filesystem will still be written to
1876 * disk by bdflush, but the journaling mechanism will be suspended
1877 * immediately and no further transaction commits will be honoured.
1878 *
1879 * Any dirty, journaled buffers will be written back to disk without
1880 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1881 * filesystem, but we _do_ attempt to leave as much data as possible
1882 * behind for fsck to use for cleanup.
1883 *
1884 * Any attempt to get a new transaction handle on a journal which is in
1885 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001886 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001887 * entered abort state during the update.
1888 *
1889 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001890 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001891 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001892 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893 * which will be recorded (if possible) in the journal superblock. This
1894 * allows a client to record failure conditions in the middle of a
1895 * transaction without having to complete the transaction to record the
1896 * failure to disk. ext3_error, for example, now uses this
1897 * functionality.
1898 *
1899 * Errors which originate from within the journaling layer will NOT
1900 * supply an errno; a null errno implies that absolutely no further
1901 * writes are done to the journal (unless there are any already in
1902 * progress).
1903 *
1904 */
1905
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001906void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907{
1908 __journal_abort_soft(journal, errno);
1909}
1910
1911/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001912 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 * @journal: journal to examine.
1914 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001915 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001916 * time the journal was mounted - if the journal was stopped
1917 * without calling abort this will be 0.
1918 *
1919 * If the journal has been aborted on this mount time -EROFS will
1920 * be returned.
1921 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001922int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001923{
1924 int err;
1925
1926 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001927 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928 err = -EROFS;
1929 else
1930 err = journal->j_errno;
1931 spin_unlock(&journal->j_state_lock);
1932 return err;
1933}
1934
1935/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001936 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001937 * @journal: journal to act on.
1938 *
1939 * An error must be cleared or Acked to take a FS out of readonly
1940 * mode.
1941 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001942int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001943{
1944 int err = 0;
1945
1946 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001947 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001948 err = -EROFS;
1949 else
1950 journal->j_errno = 0;
1951 spin_unlock(&journal->j_state_lock);
1952 return err;
1953}
1954
1955/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001956 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 * @journal: journal to act on.
1958 *
1959 * An error must be cleared or Acked to take a FS out of readonly
1960 * mode.
1961 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001962void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001963{
1964 spin_lock(&journal->j_state_lock);
1965 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 spin_unlock(&journal->j_state_lock);
1968}
1969
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001970int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971{
1972 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1973}
1974
1975/*
Zach Brownb517bea2006-10-11 01:21:08 -07001976 * helper functions to deal with 32 or 64bit block numbers.
1977 */
1978size_t journal_tag_bytes(journal_t *journal)
1979{
1980 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001981 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001982 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001983 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001984}
1985
1986/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001987 * Journal_head storage management
1988 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001989static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001990#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1992#endif
1993
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001994static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001995{
1996 int retval;
1997
Al Viro1076d172008-03-29 03:07:18 +00001998 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001999 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000 sizeof(struct journal_head),
2001 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002002 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002003 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002005 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002006 retval = -ENOMEM;
2007 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
2008 }
2009 return retval;
2010}
2011
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002012static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002014 if (jbd2_journal_head_cache) {
2015 kmem_cache_destroy(jbd2_journal_head_cache);
2016 jbd2_journal_head_cache = NULL;
2017 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002018}
2019
2020/*
2021 * journal_head splicing and dicing
2022 */
2023static struct journal_head *journal_alloc_journal_head(void)
2024{
2025 struct journal_head *ret;
2026 static unsigned long last_warning;
2027
Jose R. Santose23291b2007-07-18 08:57:06 -04002028#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002029 atomic_inc(&nr_journal_heads);
2030#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002031 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002032 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033 jbd_debug(1, "out of memory for journal_head\n");
2034 if (time_after(jiffies, last_warning + 5*HZ)) {
2035 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002036 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002037 last_warning = jiffies;
2038 }
Al Viro1076d172008-03-29 03:07:18 +00002039 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002040 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 }
2043 }
2044 return ret;
2045}
2046
2047static void journal_free_journal_head(struct journal_head *jh)
2048{
Jose R. Santose23291b2007-07-18 08:57:06 -04002049#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002050 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002051 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054}
2055
2056/*
2057 * A journal_head is attached to a buffer_head whenever JBD has an
2058 * interest in the buffer.
2059 *
2060 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2061 * is set. This bit is tested in core kernel code where we need to take
2062 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2063 * there.
2064 *
2065 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2066 *
2067 * When a buffer has its BH_JBD bit set it is immune from being released by
2068 * core kernel code, mainly via ->b_count.
2069 *
2070 * A journal_head may be detached from its buffer_head when the journal_head's
2071 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002072 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002073 * journal_head can be dropped if needed.
2074 *
2075 * Various places in the kernel want to attach a journal_head to a buffer_head
2076 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002077 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002078 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002079 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002080 *
2081 * So the typical usage would be:
2082 *
2083 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002084 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002085 * ...
2086 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002087 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002088 *
2089 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2090 * because it has a non-zero b_transaction.
2091 */
2092
2093/*
2094 * Give a buffer_head a journal_head.
2095 *
2096 * Doesn't need the journal lock.
2097 * May sleep.
2098 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002099struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100{
2101 struct journal_head *jh;
2102 struct journal_head *new_jh = NULL;
2103
2104repeat:
2105 if (!buffer_jbd(bh)) {
2106 new_jh = journal_alloc_journal_head();
2107 memset(new_jh, 0, sizeof(*new_jh));
2108 }
2109
2110 jbd_lock_bh_journal_head(bh);
2111 if (buffer_jbd(bh)) {
2112 jh = bh2jh(bh);
2113 } else {
2114 J_ASSERT_BH(bh,
2115 (atomic_read(&bh->b_count) > 0) ||
2116 (bh->b_page && bh->b_page->mapping));
2117
2118 if (!new_jh) {
2119 jbd_unlock_bh_journal_head(bh);
2120 goto repeat;
2121 }
2122
2123 jh = new_jh;
2124 new_jh = NULL; /* We consumed it */
2125 set_buffer_jbd(bh);
2126 bh->b_private = jh;
2127 jh->b_bh = bh;
2128 get_bh(bh);
2129 BUFFER_TRACE(bh, "added journal_head");
2130 }
2131 jh->b_jcount++;
2132 jbd_unlock_bh_journal_head(bh);
2133 if (new_jh)
2134 journal_free_journal_head(new_jh);
2135 return bh->b_private;
2136}
2137
2138/*
2139 * Grab a ref against this buffer_head's journal_head. If it ended up not
2140 * having a journal_head, return NULL
2141 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002142struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002143{
2144 struct journal_head *jh = NULL;
2145
2146 jbd_lock_bh_journal_head(bh);
2147 if (buffer_jbd(bh)) {
2148 jh = bh2jh(bh);
2149 jh->b_jcount++;
2150 }
2151 jbd_unlock_bh_journal_head(bh);
2152 return jh;
2153}
2154
2155static void __journal_remove_journal_head(struct buffer_head *bh)
2156{
2157 struct journal_head *jh = bh2jh(bh);
2158
2159 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2160
2161 get_bh(bh);
2162 if (jh->b_jcount == 0) {
2163 if (jh->b_transaction == NULL &&
2164 jh->b_next_transaction == NULL &&
2165 jh->b_cp_transaction == NULL) {
2166 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2167 J_ASSERT_BH(bh, buffer_jbd(bh));
2168 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2169 BUFFER_TRACE(bh, "remove journal_head");
2170 if (jh->b_frozen_data) {
2171 printk(KERN_WARNING "%s: freeing "
2172 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002173 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002174 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002175 }
2176 if (jh->b_committed_data) {
2177 printk(KERN_WARNING "%s: freeing "
2178 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002179 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002180 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002181 }
2182 bh->b_private = NULL;
2183 jh->b_bh = NULL; /* debug, really */
2184 clear_buffer_jbd(bh);
2185 __brelse(bh);
2186 journal_free_journal_head(jh);
2187 } else {
2188 BUFFER_TRACE(bh, "journal_head was locked");
2189 }
2190 }
2191}
2192
2193/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002194 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002195 * and has a zero b_jcount then remove and release its journal_head. If we did
2196 * see that the buffer is not used by any transaction we also "logically"
2197 * decrement ->b_count.
2198 *
2199 * We in fact take an additional increment on ->b_count as a convenience,
2200 * because the caller usually wants to do additional things with the bh
2201 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002202 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002203 * time. Once the caller has run __brelse(), the buffer is eligible for
2204 * reaping by try_to_free_buffers().
2205 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002206void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002207{
2208 jbd_lock_bh_journal_head(bh);
2209 __journal_remove_journal_head(bh);
2210 jbd_unlock_bh_journal_head(bh);
2211}
2212
2213/*
2214 * Drop a reference on the passed journal_head. If it fell to zero then try to
2215 * release the journal_head from the buffer_head.
2216 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002217void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002218{
2219 struct buffer_head *bh = jh2bh(jh);
2220
2221 jbd_lock_bh_journal_head(bh);
2222 J_ASSERT_JH(jh, jh->b_jcount > 0);
2223 --jh->b_jcount;
2224 if (!jh->b_jcount && !jh->b_transaction) {
2225 __journal_remove_journal_head(bh);
2226 __brelse(bh);
2227 }
2228 jbd_unlock_bh_journal_head(bh);
2229}
2230
2231/*
Jan Karac851ed52008-07-11 19:27:31 -04002232 * Initialize jbd inode head
2233 */
2234void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2235{
2236 jinode->i_transaction = NULL;
2237 jinode->i_next_transaction = NULL;
2238 jinode->i_vfs_inode = inode;
2239 jinode->i_flags = 0;
2240 INIT_LIST_HEAD(&jinode->i_list);
2241}
2242
2243/*
2244 * Function to be called before we start removing inode from memory (i.e.,
2245 * clear_inode() is a fine place to be called from). It removes inode from
2246 * transaction's lists.
2247 */
2248void jbd2_journal_release_jbd_inode(journal_t *journal,
2249 struct jbd2_inode *jinode)
2250{
2251 int writeout = 0;
2252
2253 if (!journal)
2254 return;
2255restart:
2256 spin_lock(&journal->j_list_lock);
2257 /* Is commit writing out inode - we have to wait */
2258 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2259 wait_queue_head_t *wq;
2260 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2261 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2262 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2263 spin_unlock(&journal->j_list_lock);
2264 schedule();
2265 finish_wait(wq, &wait.wait);
2266 goto restart;
2267 }
2268
2269 /* Do we need to wait for data writeback? */
2270 if (journal->j_committing_transaction == jinode->i_transaction)
2271 writeout = 1;
2272 if (jinode->i_transaction) {
2273 list_del(&jinode->i_list);
2274 jinode->i_transaction = NULL;
2275 }
2276 spin_unlock(&journal->j_list_lock);
2277}
2278
2279/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002280 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002281 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002282#ifdef CONFIG_JBD2_DEBUG
2283u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002284EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002285
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002286#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002287
Jose R. Santos6f38c742007-10-16 18:38:25 -04002288static struct dentry *jbd2_debugfs_dir;
2289static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002290
2291static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002292{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002293 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2294 if (jbd2_debugfs_dir)
2295 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2296 jbd2_debugfs_dir,
2297 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002298}
2299
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002300static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002301{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002302 debugfs_remove(jbd2_debug);
2303 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002304}
2305
2306#else
2307
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002308static void __init jbd2_create_debugfs_entry(void)
2309{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002310}
2311
2312static void __exit jbd2_remove_debugfs_entry(void)
2313{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002314}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002315
2316#endif
2317
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002318#ifdef CONFIG_PROC_FS
2319
2320#define JBD2_STATS_PROC_NAME "fs/jbd2"
2321
2322static void __init jbd2_create_jbd_stats_proc_entry(void)
2323{
2324 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2325}
2326
2327static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2328{
2329 if (proc_jbd2_stats)
2330 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2331}
2332
2333#else
2334
2335#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2336#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2337
2338#endif
2339
Christoph Lametere18b8902006-12-06 20:33:20 -08002340struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341
2342static int __init journal_init_handle_cache(void)
2343{
Johann Lombardia920e942006-10-11 01:21:00 -07002344 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002345 sizeof(handle_t),
2346 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002347 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002348 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002349 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002350 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2351 return -ENOMEM;
2352 }
2353 return 0;
2354}
2355
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002356static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002357{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002358 if (jbd2_handle_cache)
2359 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360}
2361
2362/*
2363 * Module startup and shutdown
2364 */
2365
2366static int __init journal_init_caches(void)
2367{
2368 int ret;
2369
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002370 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002371 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002372 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002373 if (ret == 0)
2374 ret = journal_init_handle_cache();
2375 return ret;
2376}
2377
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002378static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002379{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002380 jbd2_journal_destroy_revoke_caches();
2381 jbd2_journal_destroy_jbd2_journal_head_cache();
2382 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002383}
2384
2385static int __init journal_init(void)
2386{
2387 int ret;
2388
2389 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2390
2391 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002392 if (ret == 0) {
2393 jbd2_create_debugfs_entry();
2394 jbd2_create_jbd_stats_proc_entry();
2395 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002396 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002397 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002398 return ret;
2399}
2400
2401static void __exit journal_exit(void)
2402{
Jose R. Santose23291b2007-07-18 08:57:06 -04002403#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002404 int n = atomic_read(&nr_journal_heads);
2405 if (n)
2406 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2407#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002408 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002409 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002410 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002411}
2412
2413MODULE_LICENSE("GPL");
2414module_init(journal_init);
2415module_exit(journal_exit);
2416