blob: eb7eb6c27bcb7af2692678f12e358286a4598fbe [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070040
41#include <asm/uaccess.h>
42#include <asm/page.h>
43
Mingming Caof7f4bcc2006-10-11 01:20:59 -070044EXPORT_SYMBOL(jbd2_journal_start);
45EXPORT_SYMBOL(jbd2_journal_restart);
46EXPORT_SYMBOL(jbd2_journal_extend);
47EXPORT_SYMBOL(jbd2_journal_stop);
48EXPORT_SYMBOL(jbd2_journal_lock_updates);
49EXPORT_SYMBOL(jbd2_journal_unlock_updates);
50EXPORT_SYMBOL(jbd2_journal_get_write_access);
51EXPORT_SYMBOL(jbd2_journal_get_create_access);
52EXPORT_SYMBOL(jbd2_journal_get_undo_access);
53EXPORT_SYMBOL(jbd2_journal_dirty_data);
54EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
55EXPORT_SYMBOL(jbd2_journal_release_buffer);
56EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070057#if 0
58EXPORT_SYMBOL(journal_sync_buffer);
59#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070060EXPORT_SYMBOL(jbd2_journal_flush);
61EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_init_dev);
64EXPORT_SYMBOL(jbd2_journal_init_inode);
65EXPORT_SYMBOL(jbd2_journal_update_format);
66EXPORT_SYMBOL(jbd2_journal_check_used_features);
67EXPORT_SYMBOL(jbd2_journal_check_available_features);
68EXPORT_SYMBOL(jbd2_journal_set_features);
69EXPORT_SYMBOL(jbd2_journal_create);
70EXPORT_SYMBOL(jbd2_journal_load);
71EXPORT_SYMBOL(jbd2_journal_destroy);
72EXPORT_SYMBOL(jbd2_journal_update_superblock);
73EXPORT_SYMBOL(jbd2_journal_abort);
74EXPORT_SYMBOL(jbd2_journal_errno);
75EXPORT_SYMBOL(jbd2_journal_ack_err);
76EXPORT_SYMBOL(jbd2_journal_clear_err);
77EXPORT_SYMBOL(jbd2_log_wait_commit);
78EXPORT_SYMBOL(jbd2_journal_start_commit);
79EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
80EXPORT_SYMBOL(jbd2_journal_wipe);
81EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
82EXPORT_SYMBOL(jbd2_journal_invalidatepage);
83EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
84EXPORT_SYMBOL(jbd2_journal_force_commit);
Dave Kleikamp470decc2006-10-11 01:20:57 -070085
86static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
87static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070088
89/*
90 * Helper function used to manage commit timeouts
91 */
92
93static void commit_timeout(unsigned long __data)
94{
95 struct task_struct * p = (struct task_struct *) __data;
96
97 wake_up_process(p);
98}
99
100/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700101 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700102 * journal.
103 *
104 * This kernel thread is responsible for two things:
105 *
106 * 1) COMMIT: Every so often we need to commit the current state of the
107 * filesystem to disk. The journal thread is responsible for writing
108 * all of the metadata buffers to disk.
109 *
110 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
111 * of the data in that part of the log has been rewritten elsewhere on
112 * the disk. Flushing these old buffers to reclaim space in the log is
113 * known as checkpointing, and this thread is responsible for that job.
114 */
115
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700116static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700117{
118 journal_t *journal = arg;
119 transaction_t *transaction;
120
121 /*
122 * Set up an interval timer which can be used to trigger a commit wakeup
123 * after the commit interval expires
124 */
125 setup_timer(&journal->j_commit_timer, commit_timeout,
126 (unsigned long)current);
127
128 /* Record that the journal thread is running */
129 journal->j_task = current;
130 wake_up(&journal->j_wait_done_commit);
131
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700132 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700133 journal->j_commit_interval / HZ);
134
135 /*
136 * And now, wait forever for commit wakeup events.
137 */
138 spin_lock(&journal->j_state_lock);
139
140loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700141 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142 goto end_loop;
143
144 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
145 journal->j_commit_sequence, journal->j_commit_request);
146
147 if (journal->j_commit_sequence != journal->j_commit_request) {
148 jbd_debug(1, "OK, requests differ\n");
149 spin_unlock(&journal->j_state_lock);
150 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700151 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700152 spin_lock(&journal->j_state_lock);
153 goto loop;
154 }
155
156 wake_up(&journal->j_wait_done_commit);
157 if (freezing(current)) {
158 /*
159 * The simpler the better. Flushing journal isn't a
160 * good idea, because that depends on threads that may
161 * be already stopped.
162 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700163 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700164 spin_unlock(&journal->j_state_lock);
165 refrigerator();
166 spin_lock(&journal->j_state_lock);
167 } else {
168 /*
169 * We assume on resume that commits are already there,
170 * so we don't sleep
171 */
172 DEFINE_WAIT(wait);
173 int should_sleep = 1;
174
175 prepare_to_wait(&journal->j_wait_commit, &wait,
176 TASK_INTERRUPTIBLE);
177 if (journal->j_commit_sequence != journal->j_commit_request)
178 should_sleep = 0;
179 transaction = journal->j_running_transaction;
180 if (transaction && time_after_eq(jiffies,
181 transaction->t_expires))
182 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700183 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700184 should_sleep = 0;
185 if (should_sleep) {
186 spin_unlock(&journal->j_state_lock);
187 schedule();
188 spin_lock(&journal->j_state_lock);
189 }
190 finish_wait(&journal->j_wait_commit, &wait);
191 }
192
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700193 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700194
195 /*
196 * Were we woken up by a commit wakeup event?
197 */
198 transaction = journal->j_running_transaction;
199 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
200 journal->j_commit_request = transaction->t_tid;
201 jbd_debug(1, "woke because of timeout\n");
202 }
203 goto loop;
204
205end_loop:
206 spin_unlock(&journal->j_state_lock);
207 del_timer_sync(&journal->j_commit_timer);
208 journal->j_task = NULL;
209 wake_up(&journal->j_wait_done_commit);
210 jbd_debug(1, "Journal thread exiting.\n");
211 return 0;
212}
213
Pavel Emelianov97f06782007-05-08 00:30:42 -0700214static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700215{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700216 struct task_struct *t;
217
218 t = kthread_run(kjournald2, journal, "kjournald2");
219 if (IS_ERR(t))
220 return PTR_ERR(t);
221
Al Viro1076d172008-03-29 03:07:18 +0000222 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700223 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700224}
225
226static void journal_kill_thread(journal_t *journal)
227{
228 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700229 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700230
231 while (journal->j_task) {
232 wake_up(&journal->j_wait_commit);
233 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000234 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700235 spin_lock(&journal->j_state_lock);
236 }
237 spin_unlock(&journal->j_state_lock);
238}
239
240/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700241 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700242 *
243 * Writes a metadata buffer to a given disk block. The actual IO is not
244 * performed but a new buffer_head is constructed which labels the data
245 * to be written with the correct destination disk block.
246 *
247 * Any magic-number escaping which needs to be done will cause a
248 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700249 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250 * magic number is only written to the log for descripter blocks. In
251 * this case, we copy the data and replace the first word with 0, and we
252 * return a result code which indicates that this buffer needs to be
253 * marked as an escaped buffer in the corresponding log descriptor
254 * block. The missing word can then be restored when the block is read
255 * during recovery.
256 *
257 * If the source buffer has already been modified by a new transaction
258 * since we took the last commit snapshot, we use the frozen copy of
259 * that data for IO. If we end up using the existing buffer_head's data
260 * for the write, then we *have* to lock the buffer to prevent anyone
261 * else from using and possibly modifying it while the IO is in
262 * progress.
263 *
264 * The function returns a pointer to the buffer_heads to be used for IO.
265 *
266 * We assume that the journal has already been locked in this function.
267 *
268 * Return value:
269 * <0: Error
270 * >=0: Finished OK
271 *
272 * On success:
273 * Bit 0 set == escape performed on the data
274 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
275 */
276
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700277int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700278 struct journal_head *jh_in,
279 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700280 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700281{
282 int need_copy_out = 0;
283 int done_copy_out = 0;
284 int do_escape = 0;
285 char *mapped_data;
286 struct buffer_head *new_bh;
287 struct journal_head *new_jh;
288 struct page *new_page;
289 unsigned int new_offset;
290 struct buffer_head *bh_in = jh2bh(jh_in);
291
292 /*
293 * The buffer really shouldn't be locked: only the current committing
294 * transaction is allowed to write it, so nobody else is allowed
295 * to do any IO.
296 *
297 * akpm: except if we're journalling data, and write() output is
298 * also part of a shared mapping, and another thread has
299 * decided to launch a writepage() against this buffer.
300 */
301 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
302
303 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
304
305 /*
306 * If a new transaction has already done a buffer copy-out, then
307 * we use that version of the data for the commit.
308 */
309 jbd_lock_bh_state(bh_in);
310repeat:
311 if (jh_in->b_frozen_data) {
312 done_copy_out = 1;
313 new_page = virt_to_page(jh_in->b_frozen_data);
314 new_offset = offset_in_page(jh_in->b_frozen_data);
315 } else {
316 new_page = jh2bh(jh_in)->b_page;
317 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
318 }
319
320 mapped_data = kmap_atomic(new_page, KM_USER0);
321 /*
322 * Check for escaping
323 */
324 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700325 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700326 need_copy_out = 1;
327 do_escape = 1;
328 }
329 kunmap_atomic(mapped_data, KM_USER0);
330
331 /*
332 * Do we need to do a data copy?
333 */
334 if (need_copy_out && !done_copy_out) {
335 char *tmp;
336
337 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400338 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700339 jbd_lock_bh_state(bh_in);
340 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400341 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700342 goto repeat;
343 }
344
345 jh_in->b_frozen_data = tmp;
346 mapped_data = kmap_atomic(new_page, KM_USER0);
347 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
348 kunmap_atomic(mapped_data, KM_USER0);
349
350 new_page = virt_to_page(tmp);
351 new_offset = offset_in_page(tmp);
352 done_copy_out = 1;
353 }
354
355 /*
356 * Did we need to do an escaping? Now we've done all the
357 * copying, we can finally do so.
358 */
359 if (do_escape) {
360 mapped_data = kmap_atomic(new_page, KM_USER0);
361 *((unsigned int *)(mapped_data + new_offset)) = 0;
362 kunmap_atomic(mapped_data, KM_USER0);
363 }
364
365 /* keep subsequent assertions sane */
366 new_bh->b_state = 0;
367 init_buffer(new_bh, NULL, NULL);
368 atomic_set(&new_bh->b_count, 1);
369 jbd_unlock_bh_state(bh_in);
370
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700371 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700372
373 set_bh_page(new_bh, new_page, new_offset);
374 new_jh->b_transaction = NULL;
375 new_bh->b_size = jh2bh(jh_in)->b_size;
376 new_bh->b_bdev = transaction->t_journal->j_dev;
377 new_bh->b_blocknr = blocknr;
378 set_buffer_mapped(new_bh);
379 set_buffer_dirty(new_bh);
380
381 *jh_out = new_jh;
382
383 /*
384 * The to-be-written buffer needs to get moved to the io queue,
385 * and the original buffer whose contents we are shadowing or
386 * copying is moved to the transaction's shadow queue.
387 */
388 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700389 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700390 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700391 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700392
393 return do_escape | (done_copy_out << 1);
394}
395
396/*
397 * Allocation code for the journal file. Manage the space left in the
398 * journal, so that we can begin checkpointing when appropriate.
399 */
400
401/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700402 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700403 *
404 * Called with the journal already locked.
405 *
406 * Called under j_state_lock
407 */
408
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700409int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700410{
411 int left = journal->j_free;
412
413 assert_spin_locked(&journal->j_state_lock);
414
415 /*
416 * Be pessimistic here about the number of those free blocks which
417 * might be required for log descriptor control blocks.
418 */
419
420#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
421
422 left -= MIN_LOG_RESERVED_BLOCKS;
423
424 if (left <= 0)
425 return 0;
426 left -= (left >> 3);
427 return left;
428}
429
430/*
431 * Called under j_state_lock. Returns true if a transaction was started.
432 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700433int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700434{
435 /*
436 * Are we already doing a recent enough commit?
437 */
438 if (!tid_geq(journal->j_commit_request, target)) {
439 /*
440 * We want a new commit: OK, mark the request and wakup the
441 * commit thread. We do _not_ do the commit ourselves.
442 */
443
444 journal->j_commit_request = target;
445 jbd_debug(1, "JBD: requesting commit %d/%d\n",
446 journal->j_commit_request,
447 journal->j_commit_sequence);
448 wake_up(&journal->j_wait_commit);
449 return 1;
450 }
451 return 0;
452}
453
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700454int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700455{
456 int ret;
457
458 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700459 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700460 spin_unlock(&journal->j_state_lock);
461 return ret;
462}
463
464/*
465 * Force and wait upon a commit if the calling process is not within
466 * transaction. This is used for forcing out undo-protected data which contains
467 * bitmaps, when the fs is running out of space.
468 *
469 * We can only force the running transaction if we don't have an active handle;
470 * otherwise, we will deadlock.
471 *
472 * Returns true if a transaction was started.
473 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700474int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700475{
476 transaction_t *transaction = NULL;
477 tid_t tid;
478
479 spin_lock(&journal->j_state_lock);
480 if (journal->j_running_transaction && !current->journal_info) {
481 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700482 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700483 } else if (journal->j_committing_transaction)
484 transaction = journal->j_committing_transaction;
485
486 if (!transaction) {
487 spin_unlock(&journal->j_state_lock);
488 return 0; /* Nothing to retry */
489 }
490
491 tid = transaction->t_tid;
492 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700493 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700494 return 1;
495}
496
497/*
498 * Start a commit of the current running transaction (if any). Returns true
499 * if a transaction was started, and fills its tid in at *ptid
500 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700501int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700502{
503 int ret = 0;
504
505 spin_lock(&journal->j_state_lock);
506 if (journal->j_running_transaction) {
507 tid_t tid = journal->j_running_transaction->t_tid;
508
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700509 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700510 if (ret && ptid)
511 *ptid = tid;
512 } else if (journal->j_committing_transaction && ptid) {
513 /*
514 * If ext3_write_super() recently started a commit, then we
515 * have to wait for completion of that transaction
516 */
517 *ptid = journal->j_committing_transaction->t_tid;
518 ret = 1;
519 }
520 spin_unlock(&journal->j_state_lock);
521 return ret;
522}
523
524/*
525 * Wait for a specified commit to complete.
526 * The caller may not hold the journal lock.
527 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700528int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529{
530 int err = 0;
531
Jose R. Santose23291b2007-07-18 08:57:06 -0400532#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700533 spin_lock(&journal->j_state_lock);
534 if (!tid_geq(journal->j_commit_request, tid)) {
535 printk(KERN_EMERG
536 "%s: error: j_commit_request=%d, tid=%d\n",
537 __FUNCTION__, journal->j_commit_request, tid);
538 }
539 spin_unlock(&journal->j_state_lock);
540#endif
541 spin_lock(&journal->j_state_lock);
542 while (tid_gt(tid, journal->j_commit_sequence)) {
543 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
544 tid, journal->j_commit_sequence);
545 wake_up(&journal->j_wait_commit);
546 spin_unlock(&journal->j_state_lock);
547 wait_event(journal->j_wait_done_commit,
548 !tid_gt(tid, journal->j_commit_sequence));
549 spin_lock(&journal->j_state_lock);
550 }
551 spin_unlock(&journal->j_state_lock);
552
553 if (unlikely(is_journal_aborted(journal))) {
554 printk(KERN_EMERG "journal commit I/O error\n");
555 err = -EIO;
556 }
557 return err;
558}
559
560/*
561 * Log buffer allocation routines:
562 */
563
Mingming Cao18eba7a2006-10-11 01:21:13 -0700564int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700565{
566 unsigned long blocknr;
567
568 spin_lock(&journal->j_state_lock);
569 J_ASSERT(journal->j_free > 1);
570
571 blocknr = journal->j_head;
572 journal->j_head++;
573 journal->j_free--;
574 if (journal->j_head == journal->j_last)
575 journal->j_head = journal->j_first;
576 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700577 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700578}
579
580/*
581 * Conversion of logical to physical block numbers for the journal
582 *
583 * On external journals the journal blocks are identity-mapped, so
584 * this is a no-op. If needed, we can use j_blk_offset - everything is
585 * ready.
586 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700587int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700588 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700589{
590 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700591 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592
593 if (journal->j_inode) {
594 ret = bmap(journal->j_inode, blocknr);
595 if (ret)
596 *retp = ret;
597 else {
598 char b[BDEVNAME_SIZE];
599
600 printk(KERN_ALERT "%s: journal block not found "
601 "at offset %lu on %s\n",
602 __FUNCTION__,
603 blocknr,
604 bdevname(journal->j_dev, b));
605 err = -EIO;
606 __journal_abort_soft(journal, err);
607 }
608 } else {
609 *retp = blocknr; /* +journal->j_blk_offset */
610 }
611 return err;
612}
613
614/*
615 * We play buffer_head aliasing tricks to write data/metadata blocks to
616 * the journal without copying their contents, but for journal
617 * descriptor blocks we do need to generate bona fide buffers.
618 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700619 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700620 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
621 * But we don't bother doing that, so there will be coherency problems with
622 * mmaps of blockdevs which hold live JBD-controlled filesystems.
623 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700624struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625{
626 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700627 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 int err;
629
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700630 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700631
632 if (err)
633 return NULL;
634
635 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
636 lock_buffer(bh);
637 memset(bh->b_data, 0, journal->j_blocksize);
638 set_buffer_uptodate(bh);
639 unlock_buffer(bh);
640 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700641 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700642}
643
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500644struct jbd2_stats_proc_session {
645 journal_t *journal;
646 struct transaction_stats_s *stats;
647 int start;
648 int max;
649};
650
651static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
652 struct transaction_stats_s *ts,
653 int first)
654{
655 if (ts == s->stats + s->max)
656 ts = s->stats;
657 if (!first && ts == s->stats + s->start)
658 return NULL;
659 while (ts->ts_type == 0) {
660 ts++;
661 if (ts == s->stats + s->max)
662 ts = s->stats;
663 if (ts == s->stats + s->start)
664 return NULL;
665 }
666 return ts;
667
668}
669
670static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
671{
672 struct jbd2_stats_proc_session *s = seq->private;
673 struct transaction_stats_s *ts;
674 int l = *pos;
675
676 if (l == 0)
677 return SEQ_START_TOKEN;
678 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
679 if (!ts)
680 return NULL;
681 l--;
682 while (l) {
683 ts = jbd2_history_skip_empty(s, ++ts, 0);
684 if (!ts)
685 break;
686 l--;
687 }
688 return ts;
689}
690
691static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
692{
693 struct jbd2_stats_proc_session *s = seq->private;
694 struct transaction_stats_s *ts = v;
695
696 ++*pos;
697 if (v == SEQ_START_TOKEN)
698 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
699 else
700 return jbd2_history_skip_empty(s, ++ts, 0);
701}
702
703static int jbd2_seq_history_show(struct seq_file *seq, void *v)
704{
705 struct transaction_stats_s *ts = v;
706 if (v == SEQ_START_TOKEN) {
707 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
708 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
709 "wait", "run", "lock", "flush", "log", "hndls",
710 "block", "inlog", "ctime", "write", "drop",
711 "close");
712 return 0;
713 }
714 if (ts->ts_type == JBD2_STATS_RUN)
715 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
716 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
717 jiffies_to_msecs(ts->u.run.rs_wait),
718 jiffies_to_msecs(ts->u.run.rs_running),
719 jiffies_to_msecs(ts->u.run.rs_locked),
720 jiffies_to_msecs(ts->u.run.rs_flushing),
721 jiffies_to_msecs(ts->u.run.rs_logging),
722 ts->u.run.rs_handle_count,
723 ts->u.run.rs_blocks,
724 ts->u.run.rs_blocks_logged);
725 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
726 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
727 "C", ts->ts_tid, " ",
728 jiffies_to_msecs(ts->u.chp.cs_chp_time),
729 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
730 ts->u.chp.cs_forced_to_close);
731 else
732 J_ASSERT(0);
733 return 0;
734}
735
736static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
737{
738}
739
740static struct seq_operations jbd2_seq_history_ops = {
741 .start = jbd2_seq_history_start,
742 .next = jbd2_seq_history_next,
743 .stop = jbd2_seq_history_stop,
744 .show = jbd2_seq_history_show,
745};
746
747static int jbd2_seq_history_open(struct inode *inode, struct file *file)
748{
749 journal_t *journal = PDE(inode)->data;
750 struct jbd2_stats_proc_session *s;
751 int rc, size;
752
753 s = kmalloc(sizeof(*s), GFP_KERNEL);
754 if (s == NULL)
755 return -ENOMEM;
756 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
757 s->stats = kmalloc(size, GFP_KERNEL);
758 if (s->stats == NULL) {
759 kfree(s);
760 return -ENOMEM;
761 }
762 spin_lock(&journal->j_history_lock);
763 memcpy(s->stats, journal->j_history, size);
764 s->max = journal->j_history_max;
765 s->start = journal->j_history_cur % s->max;
766 spin_unlock(&journal->j_history_lock);
767
768 rc = seq_open(file, &jbd2_seq_history_ops);
769 if (rc == 0) {
770 struct seq_file *m = file->private_data;
771 m->private = s;
772 } else {
773 kfree(s->stats);
774 kfree(s);
775 }
776 return rc;
777
778}
779
780static int jbd2_seq_history_release(struct inode *inode, struct file *file)
781{
782 struct seq_file *seq = file->private_data;
783 struct jbd2_stats_proc_session *s = seq->private;
784
785 kfree(s->stats);
786 kfree(s);
787 return seq_release(inode, file);
788}
789
790static struct file_operations jbd2_seq_history_fops = {
791 .owner = THIS_MODULE,
792 .open = jbd2_seq_history_open,
793 .read = seq_read,
794 .llseek = seq_lseek,
795 .release = jbd2_seq_history_release,
796};
797
798static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
799{
800 return *pos ? NULL : SEQ_START_TOKEN;
801}
802
803static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
804{
805 return NULL;
806}
807
808static int jbd2_seq_info_show(struct seq_file *seq, void *v)
809{
810 struct jbd2_stats_proc_session *s = seq->private;
811
812 if (v != SEQ_START_TOKEN)
813 return 0;
814 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
815 s->stats->ts_tid,
816 s->journal->j_max_transaction_buffers);
817 if (s->stats->ts_tid == 0)
818 return 0;
819 seq_printf(seq, "average: \n %ums waiting for transaction\n",
820 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
821 seq_printf(seq, " %ums running transaction\n",
822 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
823 seq_printf(seq, " %ums transaction was being locked\n",
824 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
825 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
826 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
827 seq_printf(seq, " %ums logging transaction\n",
828 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
829 seq_printf(seq, " %lu handles per transaction\n",
830 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
831 seq_printf(seq, " %lu blocks per transaction\n",
832 s->stats->u.run.rs_blocks / s->stats->ts_tid);
833 seq_printf(seq, " %lu logged blocks per transaction\n",
834 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
835 return 0;
836}
837
838static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
839{
840}
841
842static struct seq_operations jbd2_seq_info_ops = {
843 .start = jbd2_seq_info_start,
844 .next = jbd2_seq_info_next,
845 .stop = jbd2_seq_info_stop,
846 .show = jbd2_seq_info_show,
847};
848
849static int jbd2_seq_info_open(struct inode *inode, struct file *file)
850{
851 journal_t *journal = PDE(inode)->data;
852 struct jbd2_stats_proc_session *s;
853 int rc, size;
854
855 s = kmalloc(sizeof(*s), GFP_KERNEL);
856 if (s == NULL)
857 return -ENOMEM;
858 size = sizeof(struct transaction_stats_s);
859 s->stats = kmalloc(size, GFP_KERNEL);
860 if (s->stats == NULL) {
861 kfree(s);
862 return -ENOMEM;
863 }
864 spin_lock(&journal->j_history_lock);
865 memcpy(s->stats, &journal->j_stats, size);
866 s->journal = journal;
867 spin_unlock(&journal->j_history_lock);
868
869 rc = seq_open(file, &jbd2_seq_info_ops);
870 if (rc == 0) {
871 struct seq_file *m = file->private_data;
872 m->private = s;
873 } else {
874 kfree(s->stats);
875 kfree(s);
876 }
877 return rc;
878
879}
880
881static int jbd2_seq_info_release(struct inode *inode, struct file *file)
882{
883 struct seq_file *seq = file->private_data;
884 struct jbd2_stats_proc_session *s = seq->private;
885 kfree(s->stats);
886 kfree(s);
887 return seq_release(inode, file);
888}
889
890static struct file_operations jbd2_seq_info_fops = {
891 .owner = THIS_MODULE,
892 .open = jbd2_seq_info_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = jbd2_seq_info_release,
896};
897
898static struct proc_dir_entry *proc_jbd2_stats;
899
900static void jbd2_stats_proc_init(journal_t *journal)
901{
902 char name[BDEVNAME_SIZE];
903
904 snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
905 journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats);
906 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700907 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
908 &jbd2_seq_history_fops, journal);
909 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
910 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500911 }
912}
913
914static void jbd2_stats_proc_exit(journal_t *journal)
915{
916 char name[BDEVNAME_SIZE];
917
918 snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
919 remove_proc_entry("info", journal->j_proc_entry);
920 remove_proc_entry("history", journal->j_proc_entry);
921 remove_proc_entry(name, proc_jbd2_stats);
922}
923
924static void journal_init_stats(journal_t *journal)
925{
926 int size;
927
928 if (!proc_jbd2_stats)
929 return;
930
931 journal->j_history_max = 100;
932 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
933 journal->j_history = kzalloc(size, GFP_KERNEL);
934 if (!journal->j_history) {
935 journal->j_history_max = 0;
936 return;
937 }
938 spin_lock_init(&journal->j_history_lock);
939}
940
Dave Kleikamp470decc2006-10-11 01:20:57 -0700941/*
942 * Management for journal control blocks: functions to create and
943 * destroy journal_t structures, and to initialise and read existing
944 * journal blocks from disk. */
945
946/* First: create and setup a journal_t object in memory. We initialise
947 * very few fields yet: that has to wait until we have created the
948 * journal structures from from scratch, or loaded them from disk. */
949
950static journal_t * journal_init_common (void)
951{
952 journal_t *journal;
953 int err;
954
Mingming Caod802ffa2007-10-16 18:38:25 -0400955 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700956 if (!journal)
957 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700958
959 init_waitqueue_head(&journal->j_wait_transaction_locked);
960 init_waitqueue_head(&journal->j_wait_logspace);
961 init_waitqueue_head(&journal->j_wait_done_commit);
962 init_waitqueue_head(&journal->j_wait_checkpoint);
963 init_waitqueue_head(&journal->j_wait_commit);
964 init_waitqueue_head(&journal->j_wait_updates);
965 mutex_init(&journal->j_barrier);
966 mutex_init(&journal->j_checkpoint_mutex);
967 spin_lock_init(&journal->j_revoke_lock);
968 spin_lock_init(&journal->j_list_lock);
969 spin_lock_init(&journal->j_state_lock);
970
Mingming Caocd02ff02007-10-16 18:38:25 -0400971 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700972
973 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700974 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975
976 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700977 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700978 if (err) {
979 kfree(journal);
980 goto fail;
981 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500982
983 journal_init_stats(journal);
984
Dave Kleikamp470decc2006-10-11 01:20:57 -0700985 return journal;
986fail:
987 return NULL;
988}
989
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700990/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700991 *
992 * Create a journal structure assigned some fixed set of disk blocks to
993 * the journal. We don't actually touch those disk blocks yet, but we
994 * need to set up all of the mapping information to tell the journaling
995 * system where the journal blocks are.
996 *
997 */
998
999/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001000 * journal_t * jbd2_journal_init_dev() - creates an initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001001 * @bdev: Block device on which to create the journal
1002 * @fs_dev: Device which hold journalled filesystem for this journal.
1003 * @start: Block nr Start of journal.
1004 * @len: Length of the journal in blocks.
1005 * @blocksize: blocksize of journalling device
1006 * @returns: a newly created journal_t *
1007 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001008 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001009 * range of blocks on an arbitrary block device.
1010 *
1011 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001012journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001013 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001014 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015{
1016 journal_t *journal = journal_init_common();
1017 struct buffer_head *bh;
1018 int n;
1019
1020 if (!journal)
1021 return NULL;
1022
1023 /* journal descriptor can store up to n blocks -bzzz */
1024 journal->j_blocksize = blocksize;
1025 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1026 journal->j_wbufsize = n;
1027 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1028 if (!journal->j_wbuf) {
1029 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1030 __FUNCTION__);
1031 kfree(journal);
1032 journal = NULL;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001033 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034 }
1035 journal->j_dev = bdev;
1036 journal->j_fs_dev = fs_dev;
1037 journal->j_blk_offset = start;
1038 journal->j_maxlen = len;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001039 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040
1041 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1042 J_ASSERT(bh != NULL);
1043 journal->j_sb_buffer = bh;
1044 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Dave Kleikamp5eb30792006-10-17 00:09:35 -07001045out:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 return journal;
1047}
1048
1049/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001050 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051 * @inode: An inode to create the journal in
1052 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001053 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001054 * the journal. The inode must exist already, must support bmap() and
1055 * must have all data blocks preallocated.
1056 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001057journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001058{
1059 struct buffer_head *bh;
1060 journal_t *journal = journal_init_common();
1061 int err;
1062 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001063 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001064
1065 if (!journal)
1066 return NULL;
1067
1068 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1069 journal->j_inode = inode;
1070 jbd_debug(1,
1071 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1072 journal, inode->i_sb->s_id, inode->i_ino,
1073 (long long) inode->i_size,
1074 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1075
1076 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1077 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001078 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079
1080 /* journal descriptor can store up to n blocks -bzzz */
1081 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1082 journal->j_wbufsize = n;
1083 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1084 if (!journal->j_wbuf) {
1085 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1086 __FUNCTION__);
1087 kfree(journal);
1088 return NULL;
1089 }
1090
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001091 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001092 /* If that failed, give up */
1093 if (err) {
1094 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1095 __FUNCTION__);
1096 kfree(journal);
1097 return NULL;
1098 }
1099
1100 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1101 J_ASSERT(bh != NULL);
1102 journal->j_sb_buffer = bh;
1103 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1104
1105 return journal;
1106}
1107
1108/*
1109 * If the journal init or create aborts, we need to mark the journal
1110 * superblock as being NULL to prevent the journal destroy from writing
1111 * back a bogus superblock.
1112 */
1113static void journal_fail_superblock (journal_t *journal)
1114{
1115 struct buffer_head *bh = journal->j_sb_buffer;
1116 brelse(bh);
1117 journal->j_sb_buffer = NULL;
1118}
1119
1120/*
1121 * Given a journal_t structure, initialise the various fields for
1122 * startup of a new journaling session. We use this both when creating
1123 * a journal, and after recovering an old journal to reset it for
1124 * subsequent use.
1125 */
1126
1127static int journal_reset(journal_t *journal)
1128{
1129 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001130 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131
1132 first = be32_to_cpu(sb->s_first);
1133 last = be32_to_cpu(sb->s_maxlen);
1134
1135 journal->j_first = first;
1136 journal->j_last = last;
1137
1138 journal->j_head = first;
1139 journal->j_tail = first;
1140 journal->j_free = last - first;
1141
1142 journal->j_tail_sequence = journal->j_transaction_sequence;
1143 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1144 journal->j_commit_request = journal->j_commit_sequence;
1145
1146 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1147
1148 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001149 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001150 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001151}
1152
1153/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001154 * int jbd2_journal_create() - Initialise the new journal file
Dave Kleikamp470decc2006-10-11 01:20:57 -07001155 * @journal: Journal to create. This structure must have been initialised
1156 *
1157 * Given a journal_t structure which tells us which disk blocks we can
1158 * use, create a new journal superblock and initialise all of the
1159 * journal fields from scratch.
1160 **/
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001161int jbd2_journal_create(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001162{
Mingming Cao18eba7a2006-10-11 01:21:13 -07001163 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001164 struct buffer_head *bh;
1165 journal_superblock_t *sb;
1166 int i, err;
1167
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001168 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1170 journal->j_maxlen);
1171 journal_fail_superblock(journal);
1172 return -EINVAL;
1173 }
1174
1175 if (journal->j_inode == NULL) {
1176 /*
1177 * We don't know what block to start at!
1178 */
1179 printk(KERN_EMERG
1180 "%s: creation of journal on external device!\n",
1181 __FUNCTION__);
1182 BUG();
1183 }
1184
1185 /* Zero out the entire journal on disk. We cannot afford to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001186 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001187 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1188 for (i = 0; i < journal->j_maxlen; i++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001189 err = jbd2_journal_bmap(journal, i, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001190 if (err)
1191 return err;
1192 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1193 lock_buffer(bh);
1194 memset (bh->b_data, 0, journal->j_blocksize);
1195 BUFFER_TRACE(bh, "marking dirty");
1196 mark_buffer_dirty(bh);
1197 BUFFER_TRACE(bh, "marking uptodate");
1198 set_buffer_uptodate(bh);
1199 unlock_buffer(bh);
1200 __brelse(bh);
1201 }
1202
1203 sync_blockdev(journal->j_dev);
1204 jbd_debug(1, "JBD: journal cleared.\n");
1205
1206 /* OK, fill in the initial static fields in the new superblock */
1207 sb = journal->j_superblock;
1208
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001209 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1210 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001211
1212 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1213 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1214 sb->s_first = cpu_to_be32(1);
1215
1216 journal->j_transaction_sequence = 1;
1217
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001218 journal->j_flags &= ~JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001219 journal->j_format_version = 2;
1220
1221 return journal_reset(journal);
1222}
1223
1224/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001225 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001226 * @journal: The journal to update.
1227 * @wait: Set to '0' if you don't want to wait for IO completion.
1228 *
1229 * Update a journal's dynamic superblock fields and write it to disk,
1230 * optionally waiting for the IO to complete.
1231 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001232void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001233{
1234 journal_superblock_t *sb = journal->j_superblock;
1235 struct buffer_head *bh = journal->j_sb_buffer;
1236
1237 /*
1238 * As a special case, if the on-disk copy is already marked as needing
1239 * no recovery (s_start == 0) and there are no outstanding transactions
1240 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001241 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001242 * attempting a write to a potential-readonly device.
1243 */
1244 if (sb->s_start == 0 && journal->j_tail_sequence ==
1245 journal->j_transaction_sequence) {
1246 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1247 "(start %ld, seq %d, errno %d)\n",
1248 journal->j_tail, journal->j_tail_sequence,
1249 journal->j_errno);
1250 goto out;
1251 }
1252
1253 spin_lock(&journal->j_state_lock);
1254 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1255 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1256
1257 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1258 sb->s_start = cpu_to_be32(journal->j_tail);
1259 sb->s_errno = cpu_to_be32(journal->j_errno);
1260 spin_unlock(&journal->j_state_lock);
1261
1262 BUFFER_TRACE(bh, "marking dirty");
1263 mark_buffer_dirty(bh);
1264 if (wait)
1265 sync_dirty_buffer(bh);
1266 else
1267 ll_rw_block(SWRITE, 1, &bh);
1268
1269out:
1270 /* If we have just flushed the log (by marking s_start==0), then
1271 * any future commit will have to be careful to update the
1272 * superblock again to re-record the true start of the log. */
1273
1274 spin_lock(&journal->j_state_lock);
1275 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001276 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001277 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001278 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001279 spin_unlock(&journal->j_state_lock);
1280}
1281
1282/*
1283 * Read the superblock for a given journal, performing initial
1284 * validation of the format.
1285 */
1286
1287static int journal_get_superblock(journal_t *journal)
1288{
1289 struct buffer_head *bh;
1290 journal_superblock_t *sb;
1291 int err = -EIO;
1292
1293 bh = journal->j_sb_buffer;
1294
1295 J_ASSERT(bh != NULL);
1296 if (!buffer_uptodate(bh)) {
1297 ll_rw_block(READ, 1, &bh);
1298 wait_on_buffer(bh);
1299 if (!buffer_uptodate(bh)) {
1300 printk (KERN_ERR
1301 "JBD: IO error reading journal superblock\n");
1302 goto out;
1303 }
1304 }
1305
1306 sb = journal->j_superblock;
1307
1308 err = -EINVAL;
1309
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001310 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001311 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1312 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1313 goto out;
1314 }
1315
1316 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001317 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001318 journal->j_format_version = 1;
1319 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001320 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001321 journal->j_format_version = 2;
1322 break;
1323 default:
1324 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1325 goto out;
1326 }
1327
1328 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1329 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1330 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1331 printk (KERN_WARNING "JBD: journal file too short\n");
1332 goto out;
1333 }
1334
1335 return 0;
1336
1337out:
1338 journal_fail_superblock(journal);
1339 return err;
1340}
1341
1342/*
1343 * Load the on-disk journal superblock and read the key fields into the
1344 * journal_t.
1345 */
1346
1347static int load_superblock(journal_t *journal)
1348{
1349 int err;
1350 journal_superblock_t *sb;
1351
1352 err = journal_get_superblock(journal);
1353 if (err)
1354 return err;
1355
1356 sb = journal->j_superblock;
1357
1358 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1359 journal->j_tail = be32_to_cpu(sb->s_start);
1360 journal->j_first = be32_to_cpu(sb->s_first);
1361 journal->j_last = be32_to_cpu(sb->s_maxlen);
1362 journal->j_errno = be32_to_cpu(sb->s_errno);
1363
1364 return 0;
1365}
1366
1367
1368/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001369 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 * @journal: Journal to act on.
1371 *
1372 * Given a journal_t structure which tells us which disk blocks contain
1373 * a journal, read the journal from disk to initialise the in-memory
1374 * structures.
1375 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001376int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001377{
1378 int err;
1379 journal_superblock_t *sb;
1380
1381 err = load_superblock(journal);
1382 if (err)
1383 return err;
1384
1385 sb = journal->j_superblock;
1386 /* If this is a V2 superblock, then we have to check the
1387 * features flags on it. */
1388
1389 if (journal->j_format_version >= 2) {
1390 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001391 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001392 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001393 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001394 printk (KERN_WARNING
1395 "JBD: Unrecognised features on journal\n");
1396 return -EINVAL;
1397 }
1398 }
1399
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 /* Let the recovery code check whether it needs to recover any
1401 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001402 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 goto recovery_error;
1404
1405 /* OK, we've finished with the dynamic journal bits:
1406 * reinitialise the dynamic contents of the superblock in memory
1407 * and reset them on disk. */
1408 if (journal_reset(journal))
1409 goto recovery_error;
1410
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001411 journal->j_flags &= ~JBD2_ABORT;
1412 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001413 return 0;
1414
1415recovery_error:
1416 printk (KERN_WARNING "JBD: recovery failed\n");
1417 return -EIO;
1418}
1419
1420/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001421 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001422 * @journal: Journal to act on.
1423 *
1424 * Release a journal_t structure once it is no longer in use by the
1425 * journaled object.
1426 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001427void jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428{
1429 /* Wait for the commit thread to wake up and die. */
1430 journal_kill_thread(journal);
1431
1432 /* Force a final log commit */
1433 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001434 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435
1436 /* Force any old transactions to disk */
1437
1438 /* Totally anal locking here... */
1439 spin_lock(&journal->j_list_lock);
1440 while (journal->j_checkpoint_transactions != NULL) {
1441 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001442 jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443 spin_lock(&journal->j_list_lock);
1444 }
1445
1446 J_ASSERT(journal->j_running_transaction == NULL);
1447 J_ASSERT(journal->j_committing_transaction == NULL);
1448 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1449 spin_unlock(&journal->j_list_lock);
1450
1451 /* We can now mark the journal as empty. */
1452 journal->j_tail = 0;
1453 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1454 if (journal->j_sb_buffer) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001455 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001456 brelse(journal->j_sb_buffer);
1457 }
1458
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001459 if (journal->j_proc_entry)
1460 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001461 if (journal->j_inode)
1462 iput(journal->j_inode);
1463 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001464 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001465 kfree(journal->j_wbuf);
1466 kfree(journal);
1467}
1468
1469
1470/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001471 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 * @journal: Journal to check.
1473 * @compat: bitmask of compatible features
1474 * @ro: bitmask of features that force read-only mount
1475 * @incompat: bitmask of incompatible features
1476 *
1477 * Check whether the journal uses all of a given set of
1478 * features. Return true (non-zero) if it does.
1479 **/
1480
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001481int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001482 unsigned long ro, unsigned long incompat)
1483{
1484 journal_superblock_t *sb;
1485
1486 if (!compat && !ro && !incompat)
1487 return 1;
1488 if (journal->j_format_version == 1)
1489 return 0;
1490
1491 sb = journal->j_superblock;
1492
1493 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1494 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1495 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1496 return 1;
1497
1498 return 0;
1499}
1500
1501/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 * @journal: Journal to check.
1504 * @compat: bitmask of compatible features
1505 * @ro: bitmask of features that force read-only mount
1506 * @incompat: bitmask of incompatible features
1507 *
1508 * Check whether the journaling code supports the use of
1509 * all of a given set of features on this journal. Return true
1510 * (non-zero) if it can. */
1511
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001512int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001513 unsigned long ro, unsigned long incompat)
1514{
1515 journal_superblock_t *sb;
1516
1517 if (!compat && !ro && !incompat)
1518 return 1;
1519
1520 sb = journal->j_superblock;
1521
1522 /* We can support any known requested features iff the
1523 * superblock is in version 2. Otherwise we fail to support any
1524 * extended sb features. */
1525
1526 if (journal->j_format_version != 2)
1527 return 0;
1528
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001529 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1530 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1531 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001532 return 1;
1533
1534 return 0;
1535}
1536
1537/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001538 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001539 * @journal: Journal to act on.
1540 * @compat: bitmask of compatible features
1541 * @ro: bitmask of features that force read-only mount
1542 * @incompat: bitmask of incompatible features
1543 *
1544 * Mark a given journal feature as present on the
1545 * superblock. Returns true if the requested features could be set.
1546 *
1547 */
1548
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001549int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001550 unsigned long ro, unsigned long incompat)
1551{
1552 journal_superblock_t *sb;
1553
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001554 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001555 return 1;
1556
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001557 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001558 return 0;
1559
1560 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1561 compat, ro, incompat);
1562
1563 sb = journal->j_superblock;
1564
1565 sb->s_feature_compat |= cpu_to_be32(compat);
1566 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1567 sb->s_feature_incompat |= cpu_to_be32(incompat);
1568
1569 return 1;
1570}
1571
Girish Shilamkar818d2762008-01-28 23:58:27 -05001572/*
1573 * jbd2_journal_clear_features () - Clear a given journal feature in the
1574 * superblock
1575 * @journal: Journal to act on.
1576 * @compat: bitmask of compatible features
1577 * @ro: bitmask of features that force read-only mount
1578 * @incompat: bitmask of incompatible features
1579 *
1580 * Clear a given journal feature as present on the
1581 * superblock.
1582 */
1583void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1584 unsigned long ro, unsigned long incompat)
1585{
1586 journal_superblock_t *sb;
1587
1588 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1589 compat, ro, incompat);
1590
1591 sb = journal->j_superblock;
1592
1593 sb->s_feature_compat &= ~cpu_to_be32(compat);
1594 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1595 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1596}
1597EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001598
1599/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 * @journal: Journal to act on.
1602 *
1603 * Given an initialised but unloaded journal struct, poke about in the
1604 * on-disk structure to update it to the most recent supported version.
1605 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001606int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001607{
1608 journal_superblock_t *sb;
1609 int err;
1610
1611 err = journal_get_superblock(journal);
1612 if (err)
1613 return err;
1614
1615 sb = journal->j_superblock;
1616
1617 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001618 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001619 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001620 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001621 return journal_convert_superblock_v1(journal, sb);
1622 default:
1623 break;
1624 }
1625 return -EINVAL;
1626}
1627
1628static int journal_convert_superblock_v1(journal_t *journal,
1629 journal_superblock_t *sb)
1630{
1631 int offset, blocksize;
1632 struct buffer_head *bh;
1633
1634 printk(KERN_WARNING
1635 "JBD: Converting superblock from version 1 to 2.\n");
1636
1637 /* Pre-initialise new fields to zero */
1638 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1639 blocksize = be32_to_cpu(sb->s_blocksize);
1640 memset(&sb->s_feature_compat, 0, blocksize-offset);
1641
1642 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001643 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644 journal->j_format_version = 2;
1645
1646 bh = journal->j_sb_buffer;
1647 BUFFER_TRACE(bh, "marking dirty");
1648 mark_buffer_dirty(bh);
1649 sync_dirty_buffer(bh);
1650 return 0;
1651}
1652
1653
1654/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001655 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001656 * @journal: Journal to act on.
1657 *
1658 * Flush all data for a given journal to disk and empty the journal.
1659 * Filesystems can use this when remounting readonly to ensure that
1660 * recovery does not need to happen on remount.
1661 */
1662
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001663int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664{
1665 int err = 0;
1666 transaction_t *transaction = NULL;
1667 unsigned long old_tail;
1668
1669 spin_lock(&journal->j_state_lock);
1670
1671 /* Force everything buffered to the log... */
1672 if (journal->j_running_transaction) {
1673 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001674 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001675 } else if (journal->j_committing_transaction)
1676 transaction = journal->j_committing_transaction;
1677
1678 /* Wait for the log commit to complete... */
1679 if (transaction) {
1680 tid_t tid = transaction->t_tid;
1681
1682 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001683 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001684 } else {
1685 spin_unlock(&journal->j_state_lock);
1686 }
1687
1688 /* ...and flush everything in the log out to disk. */
1689 spin_lock(&journal->j_list_lock);
1690 while (!err && journal->j_checkpoint_transactions != NULL) {
1691 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 err = jbd2_log_do_checkpoint(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693 spin_lock(&journal->j_list_lock);
1694 }
1695 spin_unlock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001696 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001697
1698 /* Finally, mark the journal as really needing no recovery.
1699 * This sets s_start==0 in the underlying superblock, which is
1700 * the magic code for a fully-recovered superblock. Any future
1701 * commits of data to the journal will restore the current
1702 * s_start value. */
1703 spin_lock(&journal->j_state_lock);
1704 old_tail = journal->j_tail;
1705 journal->j_tail = 0;
1706 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001707 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001708 spin_lock(&journal->j_state_lock);
1709 journal->j_tail = old_tail;
1710
1711 J_ASSERT(!journal->j_running_transaction);
1712 J_ASSERT(!journal->j_committing_transaction);
1713 J_ASSERT(!journal->j_checkpoint_transactions);
1714 J_ASSERT(journal->j_head == journal->j_tail);
1715 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1716 spin_unlock(&journal->j_state_lock);
1717 return err;
1718}
1719
1720/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 * @journal: Journal to act on.
1723 * @write: flag (see below)
1724 *
1725 * Wipe out all of the contents of a journal, safely. This will produce
1726 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001727 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001728 *
1729 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1730 * we merely suppress recovery.
1731 */
1732
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001733int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001734{
1735 journal_superblock_t *sb;
1736 int err = 0;
1737
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001738 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001739
1740 err = load_superblock(journal);
1741 if (err)
1742 return err;
1743
1744 sb = journal->j_superblock;
1745
1746 if (!journal->j_tail)
1747 goto no_recovery;
1748
1749 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1750 write ? "Clearing" : "Ignoring");
1751
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001752 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001753 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001754 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001755
1756 no_recovery:
1757 return err;
1758}
1759
1760/*
1761 * journal_dev_name: format a character string to describe on what
1762 * device this journal is present.
1763 */
1764
1765static const char *journal_dev_name(journal_t *journal, char *buffer)
1766{
1767 struct block_device *bdev;
1768
1769 if (journal->j_inode)
1770 bdev = journal->j_inode->i_sb->s_bdev;
1771 else
1772 bdev = journal->j_dev;
1773
1774 return bdevname(bdev, buffer);
1775}
1776
1777/*
1778 * Journal abort has very specific semantics, which we describe
1779 * for journal abort.
1780 *
1781 * Two internal function, which provide abort to te jbd layer
1782 * itself are here.
1783 */
1784
1785/*
1786 * Quick version for internal journal use (doesn't lock the journal).
1787 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1788 * and don't attempt to make any other journal updates.
1789 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001790void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791{
1792 transaction_t *transaction;
1793 char b[BDEVNAME_SIZE];
1794
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 return;
1797
1798 printk(KERN_ERR "Aborting journal on device %s.\n",
1799 journal_dev_name(journal, b));
1800
1801 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001802 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001803 transaction = journal->j_running_transaction;
1804 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001805 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806 spin_unlock(&journal->j_state_lock);
1807}
1808
1809/* Soft abort: record the abort error status in the journal superblock,
1810 * but don't do any other IO. */
1811static void __journal_abort_soft (journal_t *journal, int errno)
1812{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001813 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814 return;
1815
1816 if (!journal->j_errno)
1817 journal->j_errno = errno;
1818
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001819 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820
1821 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001822 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001823}
1824
1825/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001826 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001827 * @journal: the journal to shutdown.
1828 * @errno: an error number to record in the journal indicating
1829 * the reason for the shutdown.
1830 *
1831 * Perform a complete, immediate shutdown of the ENTIRE
1832 * journal (not of a single transaction). This operation cannot be
1833 * undone without closing and reopening the journal.
1834 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001835 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001836 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1837 * mode.
1838 *
1839 * Journal abort has very specific semantics. Any existing dirty,
1840 * unjournaled buffers in the main filesystem will still be written to
1841 * disk by bdflush, but the journaling mechanism will be suspended
1842 * immediately and no further transaction commits will be honoured.
1843 *
1844 * Any dirty, journaled buffers will be written back to disk without
1845 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1846 * filesystem, but we _do_ attempt to leave as much data as possible
1847 * behind for fsck to use for cleanup.
1848 *
1849 * Any attempt to get a new transaction handle on a journal which is in
1850 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001851 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001852 * entered abort state during the update.
1853 *
1854 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001855 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001856 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001857 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858 * which will be recorded (if possible) in the journal superblock. This
1859 * allows a client to record failure conditions in the middle of a
1860 * transaction without having to complete the transaction to record the
1861 * failure to disk. ext3_error, for example, now uses this
1862 * functionality.
1863 *
1864 * Errors which originate from within the journaling layer will NOT
1865 * supply an errno; a null errno implies that absolutely no further
1866 * writes are done to the journal (unless there are any already in
1867 * progress).
1868 *
1869 */
1870
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001871void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001872{
1873 __journal_abort_soft(journal, errno);
1874}
1875
1876/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001877 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001878 * @journal: journal to examine.
1879 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001880 * This is the errno numbet set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001881 * time the journal was mounted - if the journal was stopped
1882 * without calling abort this will be 0.
1883 *
1884 * If the journal has been aborted on this mount time -EROFS will
1885 * be returned.
1886 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001887int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001888{
1889 int err;
1890
1891 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001892 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893 err = -EROFS;
1894 else
1895 err = journal->j_errno;
1896 spin_unlock(&journal->j_state_lock);
1897 return err;
1898}
1899
1900/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001901 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001902 * @journal: journal to act on.
1903 *
1904 * An error must be cleared or Acked to take a FS out of readonly
1905 * mode.
1906 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001907int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001908{
1909 int err = 0;
1910
1911 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001912 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 err = -EROFS;
1914 else
1915 journal->j_errno = 0;
1916 spin_unlock(&journal->j_state_lock);
1917 return err;
1918}
1919
1920/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001921 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001922 * @journal: journal to act on.
1923 *
1924 * An error must be cleared or Acked to take a FS out of readonly
1925 * mode.
1926 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001927void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928{
1929 spin_lock(&journal->j_state_lock);
1930 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001931 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001932 spin_unlock(&journal->j_state_lock);
1933}
1934
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001935int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936{
1937 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1938}
1939
1940/*
Zach Brownb517bea2006-10-11 01:21:08 -07001941 * helper functions to deal with 32 or 64bit block numbers.
1942 */
1943size_t journal_tag_bytes(journal_t *journal)
1944{
1945 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001946 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001947 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001948 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001949}
1950
1951/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001952 * Journal_head storage management
1953 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001954static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001955#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001956static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1957#endif
1958
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001959static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960{
1961 int retval;
1962
Al Viro1076d172008-03-29 03:07:18 +00001963 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001964 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001965 sizeof(struct journal_head),
1966 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001967 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001968 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001969 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001970 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971 retval = -ENOMEM;
1972 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1973 }
1974 return retval;
1975}
1976
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001977static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001978{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001979 J_ASSERT(jbd2_journal_head_cache != NULL);
1980 kmem_cache_destroy(jbd2_journal_head_cache);
1981 jbd2_journal_head_cache = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001982}
1983
1984/*
1985 * journal_head splicing and dicing
1986 */
1987static struct journal_head *journal_alloc_journal_head(void)
1988{
1989 struct journal_head *ret;
1990 static unsigned long last_warning;
1991
Jose R. Santose23291b2007-07-18 08:57:06 -04001992#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001993 atomic_inc(&nr_journal_heads);
1994#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001995 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001996 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 jbd_debug(1, "out of memory for journal_head\n");
1998 if (time_after(jiffies, last_warning + 5*HZ)) {
1999 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2000 __FUNCTION__);
2001 last_warning = jiffies;
2002 }
Al Viro1076d172008-03-29 03:07:18 +00002003 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002005 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002006 }
2007 }
2008 return ret;
2009}
2010
2011static void journal_free_journal_head(struct journal_head *jh)
2012{
Jose R. Santose23291b2007-07-18 08:57:06 -04002013#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002014 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002015 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002017 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002018}
2019
2020/*
2021 * A journal_head is attached to a buffer_head whenever JBD has an
2022 * interest in the buffer.
2023 *
2024 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2025 * is set. This bit is tested in core kernel code where we need to take
2026 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2027 * there.
2028 *
2029 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2030 *
2031 * When a buffer has its BH_JBD bit set it is immune from being released by
2032 * core kernel code, mainly via ->b_count.
2033 *
2034 * A journal_head may be detached from its buffer_head when the journal_head's
2035 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002036 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002037 * journal_head can be dropped if needed.
2038 *
2039 * Various places in the kernel want to attach a journal_head to a buffer_head
2040 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002043 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 *
2045 * So the typical usage would be:
2046 *
2047 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002048 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049 * ...
2050 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 *
2053 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2054 * because it has a non-zero b_transaction.
2055 */
2056
2057/*
2058 * Give a buffer_head a journal_head.
2059 *
2060 * Doesn't need the journal lock.
2061 * May sleep.
2062 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002063struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064{
2065 struct journal_head *jh;
2066 struct journal_head *new_jh = NULL;
2067
2068repeat:
2069 if (!buffer_jbd(bh)) {
2070 new_jh = journal_alloc_journal_head();
2071 memset(new_jh, 0, sizeof(*new_jh));
2072 }
2073
2074 jbd_lock_bh_journal_head(bh);
2075 if (buffer_jbd(bh)) {
2076 jh = bh2jh(bh);
2077 } else {
2078 J_ASSERT_BH(bh,
2079 (atomic_read(&bh->b_count) > 0) ||
2080 (bh->b_page && bh->b_page->mapping));
2081
2082 if (!new_jh) {
2083 jbd_unlock_bh_journal_head(bh);
2084 goto repeat;
2085 }
2086
2087 jh = new_jh;
2088 new_jh = NULL; /* We consumed it */
2089 set_buffer_jbd(bh);
2090 bh->b_private = jh;
2091 jh->b_bh = bh;
2092 get_bh(bh);
2093 BUFFER_TRACE(bh, "added journal_head");
2094 }
2095 jh->b_jcount++;
2096 jbd_unlock_bh_journal_head(bh);
2097 if (new_jh)
2098 journal_free_journal_head(new_jh);
2099 return bh->b_private;
2100}
2101
2102/*
2103 * Grab a ref against this buffer_head's journal_head. If it ended up not
2104 * having a journal_head, return NULL
2105 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002106struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002107{
2108 struct journal_head *jh = NULL;
2109
2110 jbd_lock_bh_journal_head(bh);
2111 if (buffer_jbd(bh)) {
2112 jh = bh2jh(bh);
2113 jh->b_jcount++;
2114 }
2115 jbd_unlock_bh_journal_head(bh);
2116 return jh;
2117}
2118
2119static void __journal_remove_journal_head(struct buffer_head *bh)
2120{
2121 struct journal_head *jh = bh2jh(bh);
2122
2123 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2124
2125 get_bh(bh);
2126 if (jh->b_jcount == 0) {
2127 if (jh->b_transaction == NULL &&
2128 jh->b_next_transaction == NULL &&
2129 jh->b_cp_transaction == NULL) {
2130 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2131 J_ASSERT_BH(bh, buffer_jbd(bh));
2132 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2133 BUFFER_TRACE(bh, "remove journal_head");
2134 if (jh->b_frozen_data) {
2135 printk(KERN_WARNING "%s: freeing "
2136 "b_frozen_data\n",
2137 __FUNCTION__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002138 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002139 }
2140 if (jh->b_committed_data) {
2141 printk(KERN_WARNING "%s: freeing "
2142 "b_committed_data\n",
2143 __FUNCTION__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002144 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002145 }
2146 bh->b_private = NULL;
2147 jh->b_bh = NULL; /* debug, really */
2148 clear_buffer_jbd(bh);
2149 __brelse(bh);
2150 journal_free_journal_head(jh);
2151 } else {
2152 BUFFER_TRACE(bh, "journal_head was locked");
2153 }
2154 }
2155}
2156
2157/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002158 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002159 * and has a zero b_jcount then remove and release its journal_head. If we did
2160 * see that the buffer is not used by any transaction we also "logically"
2161 * decrement ->b_count.
2162 *
2163 * We in fact take an additional increment on ->b_count as a convenience,
2164 * because the caller usually wants to do additional things with the bh
2165 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002166 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002167 * time. Once the caller has run __brelse(), the buffer is eligible for
2168 * reaping by try_to_free_buffers().
2169 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002170void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002171{
2172 jbd_lock_bh_journal_head(bh);
2173 __journal_remove_journal_head(bh);
2174 jbd_unlock_bh_journal_head(bh);
2175}
2176
2177/*
2178 * Drop a reference on the passed journal_head. If it fell to zero then try to
2179 * release the journal_head from the buffer_head.
2180 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002181void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002182{
2183 struct buffer_head *bh = jh2bh(jh);
2184
2185 jbd_lock_bh_journal_head(bh);
2186 J_ASSERT_JH(jh, jh->b_jcount > 0);
2187 --jh->b_jcount;
2188 if (!jh->b_jcount && !jh->b_transaction) {
2189 __journal_remove_journal_head(bh);
2190 __brelse(bh);
2191 }
2192 jbd_unlock_bh_journal_head(bh);
2193}
2194
2195/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002196 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002197 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002198#ifdef CONFIG_JBD2_DEBUG
2199u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002200EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002201
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002202#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002203
Jose R. Santos6f38c742007-10-16 18:38:25 -04002204static struct dentry *jbd2_debugfs_dir;
2205static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002206
2207static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002208{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002209 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2210 if (jbd2_debugfs_dir)
2211 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2212 jbd2_debugfs_dir,
2213 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002214}
2215
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002216static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002217{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002218 debugfs_remove(jbd2_debug);
2219 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002220}
2221
2222#else
2223
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002224static void __init jbd2_create_debugfs_entry(void)
2225{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002226}
2227
2228static void __exit jbd2_remove_debugfs_entry(void)
2229{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002230}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002231
2232#endif
2233
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002234#ifdef CONFIG_PROC_FS
2235
2236#define JBD2_STATS_PROC_NAME "fs/jbd2"
2237
2238static void __init jbd2_create_jbd_stats_proc_entry(void)
2239{
2240 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2241}
2242
2243static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2244{
2245 if (proc_jbd2_stats)
2246 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2247}
2248
2249#else
2250
2251#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2252#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2253
2254#endif
2255
Christoph Lametere18b8902006-12-06 20:33:20 -08002256struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257
2258static int __init journal_init_handle_cache(void)
2259{
Johann Lombardia920e942006-10-11 01:21:00 -07002260 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002261 sizeof(handle_t),
2262 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002263 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002264 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002265 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002266 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2267 return -ENOMEM;
2268 }
2269 return 0;
2270}
2271
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002272static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002273{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002274 if (jbd2_handle_cache)
2275 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002276}
2277
2278/*
2279 * Module startup and shutdown
2280 */
2281
2282static int __init journal_init_caches(void)
2283{
2284 int ret;
2285
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002286 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002287 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002288 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002289 if (ret == 0)
2290 ret = journal_init_handle_cache();
2291 return ret;
2292}
2293
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002294static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002295{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002296 jbd2_journal_destroy_revoke_caches();
2297 jbd2_journal_destroy_jbd2_journal_head_cache();
2298 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002299}
2300
2301static int __init journal_init(void)
2302{
2303 int ret;
2304
2305 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2306
2307 ret = journal_init_caches();
2308 if (ret != 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002309 jbd2_journal_destroy_caches();
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002310 jbd2_create_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002311 jbd2_create_jbd_stats_proc_entry();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002312 return ret;
2313}
2314
2315static void __exit journal_exit(void)
2316{
Jose R. Santose23291b2007-07-18 08:57:06 -04002317#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002318 int n = atomic_read(&nr_journal_heads);
2319 if (n)
2320 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2321#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002322 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002323 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002324 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002325}
2326
2327MODULE_LICENSE("GPL");
2328module_init(journal_init);
2329module_exit(journal_exit);
2330