blob: e378cb383979a3e8e54234b14aa354e890c54e26 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -050040#include <linux/math64.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040041#include <linux/hash.h>
42
43#define CREATE_TRACE_POINTS
44#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070045
46#include <asm/uaccess.h>
47#include <asm/page.h>
48
Mingming Caof7f4bcc2006-10-11 01:20:59 -070049EXPORT_SYMBOL(jbd2_journal_start);
50EXPORT_SYMBOL(jbd2_journal_restart);
51EXPORT_SYMBOL(jbd2_journal_extend);
52EXPORT_SYMBOL(jbd2_journal_stop);
53EXPORT_SYMBOL(jbd2_journal_lock_updates);
54EXPORT_SYMBOL(jbd2_journal_unlock_updates);
55EXPORT_SYMBOL(jbd2_journal_get_write_access);
56EXPORT_SYMBOL(jbd2_journal_get_create_access);
57EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070058EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070059EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
60EXPORT_SYMBOL(jbd2_journal_release_buffer);
61EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062#if 0
63EXPORT_SYMBOL(journal_sync_buffer);
64#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070065EXPORT_SYMBOL(jbd2_journal_flush);
66EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070067
Mingming Caof7f4bcc2006-10-11 01:20:59 -070068EXPORT_SYMBOL(jbd2_journal_init_dev);
69EXPORT_SYMBOL(jbd2_journal_init_inode);
70EXPORT_SYMBOL(jbd2_journal_update_format);
71EXPORT_SYMBOL(jbd2_journal_check_used_features);
72EXPORT_SYMBOL(jbd2_journal_check_available_features);
73EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070074EXPORT_SYMBOL(jbd2_journal_load);
75EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070076EXPORT_SYMBOL(jbd2_journal_abort);
77EXPORT_SYMBOL(jbd2_journal_errno);
78EXPORT_SYMBOL(jbd2_journal_ack_err);
79EXPORT_SYMBOL(jbd2_journal_clear_err);
80EXPORT_SYMBOL(jbd2_log_wait_commit);
81EXPORT_SYMBOL(jbd2_journal_start_commit);
82EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
83EXPORT_SYMBOL(jbd2_journal_wipe);
84EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
85EXPORT_SYMBOL(jbd2_journal_invalidatepage);
86EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
87EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040088EXPORT_SYMBOL(jbd2_journal_file_inode);
89EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
90EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
91EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070092
93static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
94static void __journal_abort_soft (journal_t *journal, int errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -070095
96/*
97 * Helper function used to manage commit timeouts
98 */
99
100static void commit_timeout(unsigned long __data)
101{
102 struct task_struct * p = (struct task_struct *) __data;
103
104 wake_up_process(p);
105}
106
107/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700108 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700109 * journal.
110 *
111 * This kernel thread is responsible for two things:
112 *
113 * 1) COMMIT: Every so often we need to commit the current state of the
114 * filesystem to disk. The journal thread is responsible for writing
115 * all of the metadata buffers to disk.
116 *
117 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
118 * of the data in that part of the log has been rewritten elsewhere on
119 * the disk. Flushing these old buffers to reclaim space in the log is
120 * known as checkpointing, and this thread is responsible for that job.
121 */
122
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700123static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700124{
125 journal_t *journal = arg;
126 transaction_t *transaction;
127
128 /*
129 * Set up an interval timer which can be used to trigger a commit wakeup
130 * after the commit interval expires
131 */
132 setup_timer(&journal->j_commit_timer, commit_timeout,
133 (unsigned long)current);
134
135 /* Record that the journal thread is running */
136 journal->j_task = current;
137 wake_up(&journal->j_wait_done_commit);
138
Theodore Ts'o4a9bf992009-01-03 22:56:44 -0500139 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
140 "commit interval %ld seconds\n", current->pid,
141 journal->j_devname, journal->j_commit_interval / HZ);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142
143 /*
144 * And now, wait forever for commit wakeup events.
145 */
146 spin_lock(&journal->j_state_lock);
147
148loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700149 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150 goto end_loop;
151
152 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
153 journal->j_commit_sequence, journal->j_commit_request);
154
155 if (journal->j_commit_sequence != journal->j_commit_request) {
156 jbd_debug(1, "OK, requests differ\n");
157 spin_unlock(&journal->j_state_lock);
158 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700159 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 spin_lock(&journal->j_state_lock);
161 goto loop;
162 }
163
164 wake_up(&journal->j_wait_done_commit);
165 if (freezing(current)) {
166 /*
167 * The simpler the better. Flushing journal isn't a
168 * good idea, because that depends on threads that may
169 * be already stopped.
170 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700171 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700172 spin_unlock(&journal->j_state_lock);
173 refrigerator();
174 spin_lock(&journal->j_state_lock);
175 } else {
176 /*
177 * We assume on resume that commits are already there,
178 * so we don't sleep
179 */
180 DEFINE_WAIT(wait);
181 int should_sleep = 1;
182
183 prepare_to_wait(&journal->j_wait_commit, &wait,
184 TASK_INTERRUPTIBLE);
185 if (journal->j_commit_sequence != journal->j_commit_request)
186 should_sleep = 0;
187 transaction = journal->j_running_transaction;
188 if (transaction && time_after_eq(jiffies,
189 transaction->t_expires))
190 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700191 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700192 should_sleep = 0;
193 if (should_sleep) {
194 spin_unlock(&journal->j_state_lock);
195 schedule();
196 spin_lock(&journal->j_state_lock);
197 }
198 finish_wait(&journal->j_wait_commit, &wait);
199 }
200
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700201 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700202
203 /*
204 * Were we woken up by a commit wakeup event?
205 */
206 transaction = journal->j_running_transaction;
207 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
208 journal->j_commit_request = transaction->t_tid;
209 jbd_debug(1, "woke because of timeout\n");
210 }
211 goto loop;
212
213end_loop:
214 spin_unlock(&journal->j_state_lock);
215 del_timer_sync(&journal->j_commit_timer);
216 journal->j_task = NULL;
217 wake_up(&journal->j_wait_done_commit);
218 jbd_debug(1, "Journal thread exiting.\n");
219 return 0;
220}
221
Pavel Emelianov97f06782007-05-08 00:30:42 -0700222static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700223{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700224 struct task_struct *t;
225
226 t = kthread_run(kjournald2, journal, "kjournald2");
227 if (IS_ERR(t))
228 return PTR_ERR(t);
229
Al Viro1076d172008-03-29 03:07:18 +0000230 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700231 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232}
233
234static void journal_kill_thread(journal_t *journal)
235{
236 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700237 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238
239 while (journal->j_task) {
240 wake_up(&journal->j_wait_commit);
241 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000242 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700243 spin_lock(&journal->j_state_lock);
244 }
245 spin_unlock(&journal->j_state_lock);
246}
247
248/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700249 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250 *
251 * Writes a metadata buffer to a given disk block. The actual IO is not
252 * performed but a new buffer_head is constructed which labels the data
253 * to be written with the correct destination disk block.
254 *
255 * Any magic-number escaping which needs to be done will cause a
256 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700257 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258 * magic number is only written to the log for descripter blocks. In
259 * this case, we copy the data and replace the first word with 0, and we
260 * return a result code which indicates that this buffer needs to be
261 * marked as an escaped buffer in the corresponding log descriptor
262 * block. The missing word can then be restored when the block is read
263 * during recovery.
264 *
265 * If the source buffer has already been modified by a new transaction
266 * since we took the last commit snapshot, we use the frozen copy of
267 * that data for IO. If we end up using the existing buffer_head's data
268 * for the write, then we *have* to lock the buffer to prevent anyone
269 * else from using and possibly modifying it while the IO is in
270 * progress.
271 *
272 * The function returns a pointer to the buffer_heads to be used for IO.
273 *
274 * We assume that the journal has already been locked in this function.
275 *
276 * Return value:
277 * <0: Error
278 * >=0: Finished OK
279 *
280 * On success:
281 * Bit 0 set == escape performed on the data
282 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
283 */
284
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700285int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286 struct journal_head *jh_in,
287 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700288 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700289{
290 int need_copy_out = 0;
291 int done_copy_out = 0;
292 int do_escape = 0;
293 char *mapped_data;
294 struct buffer_head *new_bh;
295 struct journal_head *new_jh;
296 struct page *new_page;
297 unsigned int new_offset;
298 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700299 struct jbd2_buffer_trigger_type *triggers;
dingdinghua96577c42009-07-13 17:55:35 -0400300 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700301
302 /*
303 * The buffer really shouldn't be locked: only the current committing
304 * transaction is allowed to write it, so nobody else is allowed
305 * to do any IO.
306 *
307 * akpm: except if we're journalling data, and write() output is
308 * also part of a shared mapping, and another thread has
309 * decided to launch a writepage() against this buffer.
310 */
311 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
312
313 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghua96577c42009-07-13 17:55:35 -0400314 /* keep subsequent assertions sane */
315 new_bh->b_state = 0;
316 init_buffer(new_bh, NULL, NULL);
317 atomic_set(&new_bh->b_count, 1);
318 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700319
320 /*
321 * If a new transaction has already done a buffer copy-out, then
322 * we use that version of the data for the commit.
323 */
324 jbd_lock_bh_state(bh_in);
325repeat:
326 if (jh_in->b_frozen_data) {
327 done_copy_out = 1;
328 new_page = virt_to_page(jh_in->b_frozen_data);
329 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700330 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700331 } else {
332 new_page = jh2bh(jh_in)->b_page;
333 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700334 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700335 }
336
337 mapped_data = kmap_atomic(new_page, KM_USER0);
338 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700339 * Fire any commit trigger. Do this before checking for escaping,
340 * as the trigger may modify the magic offset. If a copy-out
341 * happens afterwards, it will have the correct data in the buffer.
342 */
343 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
344 triggers);
345
346 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700347 * Check for escaping
348 */
349 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700350 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700351 need_copy_out = 1;
352 do_escape = 1;
353 }
354 kunmap_atomic(mapped_data, KM_USER0);
355
356 /*
357 * Do we need to do a data copy?
358 */
359 if (need_copy_out && !done_copy_out) {
360 char *tmp;
361
362 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400363 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700364 jbd_lock_bh_state(bh_in);
365 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400366 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700367 goto repeat;
368 }
369
370 jh_in->b_frozen_data = tmp;
371 mapped_data = kmap_atomic(new_page, KM_USER0);
372 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
373 kunmap_atomic(mapped_data, KM_USER0);
374
375 new_page = virt_to_page(tmp);
376 new_offset = offset_in_page(tmp);
377 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700378
379 /*
380 * This isn't strictly necessary, as we're using frozen
381 * data for the escaping, but it keeps consistency with
382 * b_frozen_data usage.
383 */
384 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700385 }
386
387 /*
388 * Did we need to do an escaping? Now we've done all the
389 * copying, we can finally do so.
390 */
391 if (do_escape) {
392 mapped_data = kmap_atomic(new_page, KM_USER0);
393 *((unsigned int *)(mapped_data + new_offset)) = 0;
394 kunmap_atomic(mapped_data, KM_USER0);
395 }
396
Dave Kleikamp470decc2006-10-11 01:20:57 -0700397 set_bh_page(new_bh, new_page, new_offset);
398 new_jh->b_transaction = NULL;
399 new_bh->b_size = jh2bh(jh_in)->b_size;
400 new_bh->b_bdev = transaction->t_journal->j_dev;
401 new_bh->b_blocknr = blocknr;
402 set_buffer_mapped(new_bh);
403 set_buffer_dirty(new_bh);
404
405 *jh_out = new_jh;
406
407 /*
408 * The to-be-written buffer needs to get moved to the io queue,
409 * and the original buffer whose contents we are shadowing or
410 * copying is moved to the transaction's shadow queue.
411 */
412 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400413 spin_lock(&journal->j_list_lock);
414 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
415 spin_unlock(&journal->j_list_lock);
416 jbd_unlock_bh_state(bh_in);
417
Dave Kleikamp470decc2006-10-11 01:20:57 -0700418 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700419 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420
421 return do_escape | (done_copy_out << 1);
422}
423
424/*
425 * Allocation code for the journal file. Manage the space left in the
426 * journal, so that we can begin checkpointing when appropriate.
427 */
428
429/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700430 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700431 *
432 * Called with the journal already locked.
433 *
434 * Called under j_state_lock
435 */
436
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700437int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700438{
439 int left = journal->j_free;
440
441 assert_spin_locked(&journal->j_state_lock);
442
443 /*
444 * Be pessimistic here about the number of those free blocks which
445 * might be required for log descriptor control blocks.
446 */
447
448#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
449
450 left -= MIN_LOG_RESERVED_BLOCKS;
451
452 if (left <= 0)
453 return 0;
454 left -= (left >> 3);
455 return left;
456}
457
458/*
Jan Karac88ccea2009-02-10 11:27:46 -0500459 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700460 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700461int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700462{
463 /*
464 * Are we already doing a recent enough commit?
465 */
466 if (!tid_geq(journal->j_commit_request, target)) {
467 /*
468 * We want a new commit: OK, mark the request and wakup the
469 * commit thread. We do _not_ do the commit ourselves.
470 */
471
472 journal->j_commit_request = target;
473 jbd_debug(1, "JBD: requesting commit %d/%d\n",
474 journal->j_commit_request,
475 journal->j_commit_sequence);
476 wake_up(&journal->j_wait_commit);
477 return 1;
478 }
479 return 0;
480}
481
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700482int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700483{
484 int ret;
485
486 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700487 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700488 spin_unlock(&journal->j_state_lock);
489 return ret;
490}
491
492/*
493 * Force and wait upon a commit if the calling process is not within
494 * transaction. This is used for forcing out undo-protected data which contains
495 * bitmaps, when the fs is running out of space.
496 *
497 * We can only force the running transaction if we don't have an active handle;
498 * otherwise, we will deadlock.
499 *
500 * Returns true if a transaction was started.
501 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700502int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700503{
504 transaction_t *transaction = NULL;
505 tid_t tid;
506
507 spin_lock(&journal->j_state_lock);
508 if (journal->j_running_transaction && !current->journal_info) {
509 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700510 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700511 } else if (journal->j_committing_transaction)
512 transaction = journal->j_committing_transaction;
513
514 if (!transaction) {
515 spin_unlock(&journal->j_state_lock);
516 return 0; /* Nothing to retry */
517 }
518
519 tid = transaction->t_tid;
520 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700521 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700522 return 1;
523}
524
525/*
526 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500527 * if a transaction is going to be committed (or is currently already
528 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700530int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531{
532 int ret = 0;
533
534 spin_lock(&journal->j_state_lock);
535 if (journal->j_running_transaction) {
536 tid_t tid = journal->j_running_transaction->t_tid;
537
Jan Karac88ccea2009-02-10 11:27:46 -0500538 __jbd2_log_start_commit(journal, tid);
539 /* There's a running transaction and we've just made sure
540 * it's commit has been scheduled. */
541 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500543 ret = 1;
544 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700545 /*
546 * If ext3_write_super() recently started a commit, then we
547 * have to wait for completion of that transaction
548 */
Jan Karac88ccea2009-02-10 11:27:46 -0500549 if (ptid)
550 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551 ret = 1;
552 }
553 spin_unlock(&journal->j_state_lock);
554 return ret;
555}
556
557/*
558 * Wait for a specified commit to complete.
559 * The caller may not hold the journal lock.
560 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700561int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700562{
563 int err = 0;
564
Jose R. Santose23291b2007-07-18 08:57:06 -0400565#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700566 spin_lock(&journal->j_state_lock);
567 if (!tid_geq(journal->j_commit_request, tid)) {
568 printk(KERN_EMERG
569 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400570 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700571 }
572 spin_unlock(&journal->j_state_lock);
573#endif
574 spin_lock(&journal->j_state_lock);
575 while (tid_gt(tid, journal->j_commit_sequence)) {
576 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
577 tid, journal->j_commit_sequence);
578 wake_up(&journal->j_wait_commit);
579 spin_unlock(&journal->j_state_lock);
580 wait_event(journal->j_wait_done_commit,
581 !tid_gt(tid, journal->j_commit_sequence));
582 spin_lock(&journal->j_state_lock);
583 }
584 spin_unlock(&journal->j_state_lock);
585
586 if (unlikely(is_journal_aborted(journal))) {
587 printk(KERN_EMERG "journal commit I/O error\n");
588 err = -EIO;
589 }
590 return err;
591}
592
593/*
594 * Log buffer allocation routines:
595 */
596
Mingming Cao18eba7a2006-10-11 01:21:13 -0700597int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700598{
599 unsigned long blocknr;
600
601 spin_lock(&journal->j_state_lock);
602 J_ASSERT(journal->j_free > 1);
603
604 blocknr = journal->j_head;
605 journal->j_head++;
606 journal->j_free--;
607 if (journal->j_head == journal->j_last)
608 journal->j_head = journal->j_first;
609 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700610 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700611}
612
613/*
614 * Conversion of logical to physical block numbers for the journal
615 *
616 * On external journals the journal blocks are identity-mapped, so
617 * this is a no-op. If needed, we can use j_blk_offset - everything is
618 * ready.
619 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700620int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700621 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622{
623 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700624 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625
626 if (journal->j_inode) {
627 ret = bmap(journal->j_inode, blocknr);
628 if (ret)
629 *retp = ret;
630 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700631 printk(KERN_ALERT "%s: journal block not found "
632 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400633 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700634 err = -EIO;
635 __journal_abort_soft(journal, err);
636 }
637 } else {
638 *retp = blocknr; /* +journal->j_blk_offset */
639 }
640 return err;
641}
642
643/*
644 * We play buffer_head aliasing tricks to write data/metadata blocks to
645 * the journal without copying their contents, but for journal
646 * descriptor blocks we do need to generate bona fide buffers.
647 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700648 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700649 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
650 * But we don't bother doing that, so there will be coherency problems with
651 * mmaps of blockdevs which hold live JBD-controlled filesystems.
652 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700653struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654{
655 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700656 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700657 int err;
658
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700659 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700660
661 if (err)
662 return NULL;
663
664 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500665 if (!bh)
666 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700667 lock_buffer(bh);
668 memset(bh->b_data, 0, journal->j_blocksize);
669 set_buffer_uptodate(bh);
670 unlock_buffer(bh);
671 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700672 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700673}
674
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500675struct jbd2_stats_proc_session {
676 journal_t *journal;
677 struct transaction_stats_s *stats;
678 int start;
679 int max;
680};
681
682static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
683 struct transaction_stats_s *ts,
684 int first)
685{
686 if (ts == s->stats + s->max)
687 ts = s->stats;
688 if (!first && ts == s->stats + s->start)
689 return NULL;
690 while (ts->ts_type == 0) {
691 ts++;
692 if (ts == s->stats + s->max)
693 ts = s->stats;
694 if (ts == s->stats + s->start)
695 return NULL;
696 }
697 return ts;
698
699}
700
701static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
702{
703 struct jbd2_stats_proc_session *s = seq->private;
704 struct transaction_stats_s *ts;
705 int l = *pos;
706
707 if (l == 0)
708 return SEQ_START_TOKEN;
709 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
710 if (!ts)
711 return NULL;
712 l--;
713 while (l) {
714 ts = jbd2_history_skip_empty(s, ++ts, 0);
715 if (!ts)
716 break;
717 l--;
718 }
719 return ts;
720}
721
722static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
723{
724 struct jbd2_stats_proc_session *s = seq->private;
725 struct transaction_stats_s *ts = v;
726
727 ++*pos;
728 if (v == SEQ_START_TOKEN)
729 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
730 else
731 return jbd2_history_skip_empty(s, ++ts, 0);
732}
733
734static int jbd2_seq_history_show(struct seq_file *seq, void *v)
735{
736 struct transaction_stats_s *ts = v;
737 if (v == SEQ_START_TOKEN) {
738 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
739 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
740 "wait", "run", "lock", "flush", "log", "hndls",
741 "block", "inlog", "ctime", "write", "drop",
742 "close");
743 return 0;
744 }
745 if (ts->ts_type == JBD2_STATS_RUN)
746 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
747 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
748 jiffies_to_msecs(ts->u.run.rs_wait),
749 jiffies_to_msecs(ts->u.run.rs_running),
750 jiffies_to_msecs(ts->u.run.rs_locked),
751 jiffies_to_msecs(ts->u.run.rs_flushing),
752 jiffies_to_msecs(ts->u.run.rs_logging),
753 ts->u.run.rs_handle_count,
754 ts->u.run.rs_blocks,
755 ts->u.run.rs_blocks_logged);
756 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
757 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
758 "C", ts->ts_tid, " ",
759 jiffies_to_msecs(ts->u.chp.cs_chp_time),
760 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
761 ts->u.chp.cs_forced_to_close);
762 else
763 J_ASSERT(0);
764 return 0;
765}
766
767static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
768{
769}
770
771static struct seq_operations jbd2_seq_history_ops = {
772 .start = jbd2_seq_history_start,
773 .next = jbd2_seq_history_next,
774 .stop = jbd2_seq_history_stop,
775 .show = jbd2_seq_history_show,
776};
777
778static int jbd2_seq_history_open(struct inode *inode, struct file *file)
779{
780 journal_t *journal = PDE(inode)->data;
781 struct jbd2_stats_proc_session *s;
782 int rc, size;
783
784 s = kmalloc(sizeof(*s), GFP_KERNEL);
785 if (s == NULL)
786 return -ENOMEM;
787 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
788 s->stats = kmalloc(size, GFP_KERNEL);
789 if (s->stats == NULL) {
790 kfree(s);
791 return -ENOMEM;
792 }
793 spin_lock(&journal->j_history_lock);
794 memcpy(s->stats, journal->j_history, size);
795 s->max = journal->j_history_max;
796 s->start = journal->j_history_cur % s->max;
797 spin_unlock(&journal->j_history_lock);
798
799 rc = seq_open(file, &jbd2_seq_history_ops);
800 if (rc == 0) {
801 struct seq_file *m = file->private_data;
802 m->private = s;
803 } else {
804 kfree(s->stats);
805 kfree(s);
806 }
807 return rc;
808
809}
810
811static int jbd2_seq_history_release(struct inode *inode, struct file *file)
812{
813 struct seq_file *seq = file->private_data;
814 struct jbd2_stats_proc_session *s = seq->private;
815
816 kfree(s->stats);
817 kfree(s);
818 return seq_release(inode, file);
819}
820
821static struct file_operations jbd2_seq_history_fops = {
822 .owner = THIS_MODULE,
823 .open = jbd2_seq_history_open,
824 .read = seq_read,
825 .llseek = seq_lseek,
826 .release = jbd2_seq_history_release,
827};
828
829static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
830{
831 return *pos ? NULL : SEQ_START_TOKEN;
832}
833
834static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
835{
836 return NULL;
837}
838
839static int jbd2_seq_info_show(struct seq_file *seq, void *v)
840{
841 struct jbd2_stats_proc_session *s = seq->private;
842
843 if (v != SEQ_START_TOKEN)
844 return 0;
845 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
846 s->stats->ts_tid,
847 s->journal->j_max_transaction_buffers);
848 if (s->stats->ts_tid == 0)
849 return 0;
850 seq_printf(seq, "average: \n %ums waiting for transaction\n",
851 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
852 seq_printf(seq, " %ums running transaction\n",
853 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
854 seq_printf(seq, " %ums transaction was being locked\n",
855 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
856 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
857 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
858 seq_printf(seq, " %ums logging transaction\n",
859 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500860 seq_printf(seq, " %lluus average transaction commit time\n",
861 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500862 seq_printf(seq, " %lu handles per transaction\n",
863 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
864 seq_printf(seq, " %lu blocks per transaction\n",
865 s->stats->u.run.rs_blocks / s->stats->ts_tid);
866 seq_printf(seq, " %lu logged blocks per transaction\n",
867 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
868 return 0;
869}
870
871static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
872{
873}
874
875static struct seq_operations jbd2_seq_info_ops = {
876 .start = jbd2_seq_info_start,
877 .next = jbd2_seq_info_next,
878 .stop = jbd2_seq_info_stop,
879 .show = jbd2_seq_info_show,
880};
881
882static int jbd2_seq_info_open(struct inode *inode, struct file *file)
883{
884 journal_t *journal = PDE(inode)->data;
885 struct jbd2_stats_proc_session *s;
886 int rc, size;
887
888 s = kmalloc(sizeof(*s), GFP_KERNEL);
889 if (s == NULL)
890 return -ENOMEM;
891 size = sizeof(struct transaction_stats_s);
892 s->stats = kmalloc(size, GFP_KERNEL);
893 if (s->stats == NULL) {
894 kfree(s);
895 return -ENOMEM;
896 }
897 spin_lock(&journal->j_history_lock);
898 memcpy(s->stats, &journal->j_stats, size);
899 s->journal = journal;
900 spin_unlock(&journal->j_history_lock);
901
902 rc = seq_open(file, &jbd2_seq_info_ops);
903 if (rc == 0) {
904 struct seq_file *m = file->private_data;
905 m->private = s;
906 } else {
907 kfree(s->stats);
908 kfree(s);
909 }
910 return rc;
911
912}
913
914static int jbd2_seq_info_release(struct inode *inode, struct file *file)
915{
916 struct seq_file *seq = file->private_data;
917 struct jbd2_stats_proc_session *s = seq->private;
918 kfree(s->stats);
919 kfree(s);
920 return seq_release(inode, file);
921}
922
923static struct file_operations jbd2_seq_info_fops = {
924 .owner = THIS_MODULE,
925 .open = jbd2_seq_info_open,
926 .read = seq_read,
927 .llseek = seq_lseek,
928 .release = jbd2_seq_info_release,
929};
930
931static struct proc_dir_entry *proc_jbd2_stats;
932
933static void jbd2_stats_proc_init(journal_t *journal)
934{
Theodore Ts'o05496762008-09-16 14:36:17 -0400935 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500936 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700937 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
938 &jbd2_seq_history_fops, journal);
939 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
940 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500941 }
942}
943
944static void jbd2_stats_proc_exit(journal_t *journal)
945{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500946 remove_proc_entry("info", journal->j_proc_entry);
947 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400948 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500949}
950
951static void journal_init_stats(journal_t *journal)
952{
953 int size;
954
955 if (!proc_jbd2_stats)
956 return;
957
958 journal->j_history_max = 100;
959 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
960 journal->j_history = kzalloc(size, GFP_KERNEL);
961 if (!journal->j_history) {
962 journal->j_history_max = 0;
963 return;
964 }
965 spin_lock_init(&journal->j_history_lock);
966}
967
Dave Kleikamp470decc2006-10-11 01:20:57 -0700968/*
969 * Management for journal control blocks: functions to create and
970 * destroy journal_t structures, and to initialise and read existing
971 * journal blocks from disk. */
972
973/* First: create and setup a journal_t object in memory. We initialise
974 * very few fields yet: that has to wait until we have created the
975 * journal structures from from scratch, or loaded them from disk. */
976
977static journal_t * journal_init_common (void)
978{
979 journal_t *journal;
980 int err;
981
Mingming Caod802ffa2007-10-16 18:38:25 -0400982 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700983 if (!journal)
984 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700985
986 init_waitqueue_head(&journal->j_wait_transaction_locked);
987 init_waitqueue_head(&journal->j_wait_logspace);
988 init_waitqueue_head(&journal->j_wait_done_commit);
989 init_waitqueue_head(&journal->j_wait_checkpoint);
990 init_waitqueue_head(&journal->j_wait_commit);
991 init_waitqueue_head(&journal->j_wait_updates);
992 mutex_init(&journal->j_barrier);
993 mutex_init(&journal->j_checkpoint_mutex);
994 spin_lock_init(&journal->j_revoke_lock);
995 spin_lock_init(&journal->j_list_lock);
996 spin_lock_init(&journal->j_state_lock);
997
Mingming Caocd02ff02007-10-16 18:38:25 -0400998 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500999 journal->j_min_batch_time = 0;
1000 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001001
1002 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001003 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001004
1005 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001006 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001007 if (err) {
1008 kfree(journal);
1009 goto fail;
1010 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001011
1012 journal_init_stats(journal);
1013
Dave Kleikamp470decc2006-10-11 01:20:57 -07001014 return journal;
1015fail:
1016 return NULL;
1017}
1018
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001019/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001020 *
1021 * Create a journal structure assigned some fixed set of disk blocks to
1022 * the journal. We don't actually touch those disk blocks yet, but we
1023 * need to set up all of the mapping information to tell the journaling
1024 * system where the journal blocks are.
1025 *
1026 */
1027
1028/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001029 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001030 * @bdev: Block device on which to create the journal
1031 * @fs_dev: Device which hold journalled filesystem for this journal.
1032 * @start: Block nr Start of journal.
1033 * @len: Length of the journal in blocks.
1034 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001035 *
1036 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001037 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001038 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001039 * range of blocks on an arbitrary block device.
1040 *
1041 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001042journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001044 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001045{
1046 journal_t *journal = journal_init_common();
1047 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001048 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049 int n;
1050
1051 if (!journal)
1052 return NULL;
1053
1054 /* journal descriptor can store up to n blocks -bzzz */
1055 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -05001056 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001057 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1058 journal->j_wbufsize = n;
1059 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1060 if (!journal->j_wbuf) {
1061 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001062 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001063 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001064 }
1065 journal->j_dev = bdev;
1066 journal->j_fs_dev = fs_dev;
1067 journal->j_blk_offset = start;
1068 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001069 bdevname(journal->j_dev, journal->j_devname);
1070 p = journal->j_devname;
1071 while ((p = strchr(p, '/')))
1072 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -07001073
1074 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001075 if (!bh) {
1076 printk(KERN_ERR
1077 "%s: Cannot get buffer for journal superblock\n",
1078 __func__);
1079 goto out_err;
1080 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001081 journal->j_sb_buffer = bh;
1082 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001083
Dave Kleikamp470decc2006-10-11 01:20:57 -07001084 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001085out_err:
1086 jbd2_stats_proc_exit(journal);
1087 kfree(journal);
1088 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001089}
1090
1091/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001092 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001093 * @inode: An inode to create the journal in
1094 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001095 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001096 * the journal. The inode must exist already, must support bmap() and
1097 * must have all data blocks preallocated.
1098 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001099journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001100{
1101 struct buffer_head *bh;
1102 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001103 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104 int err;
1105 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001106 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001107
1108 if (!journal)
1109 return NULL;
1110
1111 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1112 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001113 bdevname(journal->j_dev, journal->j_devname);
1114 p = journal->j_devname;
1115 while ((p = strchr(p, '/')))
1116 *p = '!';
1117 p = journal->j_devname + strlen(journal->j_devname);
1118 sprintf(p, ":%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001119 jbd_debug(1,
1120 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1121 journal, inode->i_sb->s_id, inode->i_ino,
1122 (long long) inode->i_size,
1123 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1124
1125 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1126 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001127 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001128
1129 /* journal descriptor can store up to n blocks -bzzz */
1130 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1131 journal->j_wbufsize = n;
1132 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1133 if (!journal->j_wbuf) {
1134 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001135 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001136 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001137 }
1138
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001139 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001140 /* If that failed, give up */
1141 if (err) {
1142 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001143 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001144 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001145 }
1146
1147 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001148 if (!bh) {
1149 printk(KERN_ERR
1150 "%s: Cannot get buffer for journal superblock\n",
1151 __func__);
1152 goto out_err;
1153 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001154 journal->j_sb_buffer = bh;
1155 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1156
1157 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001158out_err:
1159 jbd2_stats_proc_exit(journal);
1160 kfree(journal);
1161 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001162}
1163
1164/*
1165 * If the journal init or create aborts, we need to mark the journal
1166 * superblock as being NULL to prevent the journal destroy from writing
1167 * back a bogus superblock.
1168 */
1169static void journal_fail_superblock (journal_t *journal)
1170{
1171 struct buffer_head *bh = journal->j_sb_buffer;
1172 brelse(bh);
1173 journal->j_sb_buffer = NULL;
1174}
1175
1176/*
1177 * Given a journal_t structure, initialise the various fields for
1178 * startup of a new journaling session. We use this both when creating
1179 * a journal, and after recovering an old journal to reset it for
1180 * subsequent use.
1181 */
1182
1183static int journal_reset(journal_t *journal)
1184{
1185 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001186 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001187
1188 first = be32_to_cpu(sb->s_first);
1189 last = be32_to_cpu(sb->s_maxlen);
1190
1191 journal->j_first = first;
1192 journal->j_last = last;
1193
1194 journal->j_head = first;
1195 journal->j_tail = first;
1196 journal->j_free = last - first;
1197
1198 journal->j_tail_sequence = journal->j_transaction_sequence;
1199 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1200 journal->j_commit_request = journal->j_commit_sequence;
1201
1202 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1203
1204 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001205 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001206 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001207}
1208
1209/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001210 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001211 * @journal: The journal to update.
1212 * @wait: Set to '0' if you don't want to wait for IO completion.
1213 *
1214 * Update a journal's dynamic superblock fields and write it to disk,
1215 * optionally waiting for the IO to complete.
1216 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001217void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001218{
1219 journal_superblock_t *sb = journal->j_superblock;
1220 struct buffer_head *bh = journal->j_sb_buffer;
1221
1222 /*
1223 * As a special case, if the on-disk copy is already marked as needing
1224 * no recovery (s_start == 0) and there are no outstanding transactions
1225 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001226 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001227 * attempting a write to a potential-readonly device.
1228 */
1229 if (sb->s_start == 0 && journal->j_tail_sequence ==
1230 journal->j_transaction_sequence) {
1231 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1232 "(start %ld, seq %d, errno %d)\n",
1233 journal->j_tail, journal->j_tail_sequence,
1234 journal->j_errno);
1235 goto out;
1236 }
1237
Theodore Ts'o914258b2008-10-06 21:35:40 -04001238 if (buffer_write_io_error(bh)) {
1239 /*
1240 * Oh, dear. A previous attempt to write the journal
1241 * superblock failed. This could happen because the
1242 * USB device was yanked out. Or it could happen to
1243 * be a transient write error and maybe the block will
1244 * be remapped. Nothing we can do but to retry the
1245 * write and hope for the best.
1246 */
1247 printk(KERN_ERR "JBD2: previous I/O error detected "
1248 "for journal superblock update for %s.\n",
1249 journal->j_devname);
1250 clear_buffer_write_io_error(bh);
1251 set_buffer_uptodate(bh);
1252 }
1253
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 spin_lock(&journal->j_state_lock);
1255 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1256 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1257
1258 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1259 sb->s_start = cpu_to_be32(journal->j_tail);
1260 sb->s_errno = cpu_to_be32(journal->j_errno);
1261 spin_unlock(&journal->j_state_lock);
1262
1263 BUFFER_TRACE(bh, "marking dirty");
1264 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001265 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001266 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001267 if (buffer_write_io_error(bh)) {
1268 printk(KERN_ERR "JBD2: I/O error detected "
1269 "when updating journal superblock for %s.\n",
1270 journal->j_devname);
1271 clear_buffer_write_io_error(bh);
1272 set_buffer_uptodate(bh);
1273 }
1274 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001275 ll_rw_block(SWRITE, 1, &bh);
1276
1277out:
1278 /* If we have just flushed the log (by marking s_start==0), then
1279 * any future commit will have to be careful to update the
1280 * superblock again to re-record the true start of the log. */
1281
1282 spin_lock(&journal->j_state_lock);
1283 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001284 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001285 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001286 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001287 spin_unlock(&journal->j_state_lock);
1288}
1289
1290/*
1291 * Read the superblock for a given journal, performing initial
1292 * validation of the format.
1293 */
1294
1295static int journal_get_superblock(journal_t *journal)
1296{
1297 struct buffer_head *bh;
1298 journal_superblock_t *sb;
1299 int err = -EIO;
1300
1301 bh = journal->j_sb_buffer;
1302
1303 J_ASSERT(bh != NULL);
1304 if (!buffer_uptodate(bh)) {
1305 ll_rw_block(READ, 1, &bh);
1306 wait_on_buffer(bh);
1307 if (!buffer_uptodate(bh)) {
1308 printk (KERN_ERR
1309 "JBD: IO error reading journal superblock\n");
1310 goto out;
1311 }
1312 }
1313
1314 sb = journal->j_superblock;
1315
1316 err = -EINVAL;
1317
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001318 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001319 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1320 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1321 goto out;
1322 }
1323
1324 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001325 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001326 journal->j_format_version = 1;
1327 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001328 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001329 journal->j_format_version = 2;
1330 break;
1331 default:
1332 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1333 goto out;
1334 }
1335
1336 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1337 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1338 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1339 printk (KERN_WARNING "JBD: journal file too short\n");
1340 goto out;
1341 }
1342
1343 return 0;
1344
1345out:
1346 journal_fail_superblock(journal);
1347 return err;
1348}
1349
1350/*
1351 * Load the on-disk journal superblock and read the key fields into the
1352 * journal_t.
1353 */
1354
1355static int load_superblock(journal_t *journal)
1356{
1357 int err;
1358 journal_superblock_t *sb;
1359
1360 err = journal_get_superblock(journal);
1361 if (err)
1362 return err;
1363
1364 sb = journal->j_superblock;
1365
1366 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1367 journal->j_tail = be32_to_cpu(sb->s_start);
1368 journal->j_first = be32_to_cpu(sb->s_first);
1369 journal->j_last = be32_to_cpu(sb->s_maxlen);
1370 journal->j_errno = be32_to_cpu(sb->s_errno);
1371
1372 return 0;
1373}
1374
1375
1376/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001377 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001378 * @journal: Journal to act on.
1379 *
1380 * Given a journal_t structure which tells us which disk blocks contain
1381 * a journal, read the journal from disk to initialise the in-memory
1382 * structures.
1383 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001384int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001385{
1386 int err;
1387 journal_superblock_t *sb;
1388
1389 err = load_superblock(journal);
1390 if (err)
1391 return err;
1392
1393 sb = journal->j_superblock;
1394 /* If this is a V2 superblock, then we have to check the
1395 * features flags on it. */
1396
1397 if (journal->j_format_version >= 2) {
1398 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001399 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001401 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001402 printk (KERN_WARNING
1403 "JBD: Unrecognised features on journal\n");
1404 return -EINVAL;
1405 }
1406 }
1407
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 /* Let the recovery code check whether it needs to recover any
1409 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001410 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001411 goto recovery_error;
1412
1413 /* OK, we've finished with the dynamic journal bits:
1414 * reinitialise the dynamic contents of the superblock in memory
1415 * and reset them on disk. */
1416 if (journal_reset(journal))
1417 goto recovery_error;
1418
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001419 journal->j_flags &= ~JBD2_ABORT;
1420 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001421 return 0;
1422
1423recovery_error:
1424 printk (KERN_WARNING "JBD: recovery failed\n");
1425 return -EIO;
1426}
1427
1428/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001429 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001430 * @journal: Journal to act on.
1431 *
1432 * Release a journal_t structure once it is no longer in use by the
1433 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001434 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001436int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001437{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001438 int err = 0;
1439
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440 /* Wait for the commit thread to wake up and die. */
1441 journal_kill_thread(journal);
1442
1443 /* Force a final log commit */
1444 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001445 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001446
1447 /* Force any old transactions to disk */
1448
1449 /* Totally anal locking here... */
1450 spin_lock(&journal->j_list_lock);
1451 while (journal->j_checkpoint_transactions != NULL) {
1452 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001453 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001454 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001455 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001456 spin_lock(&journal->j_list_lock);
1457 }
1458
1459 J_ASSERT(journal->j_running_transaction == NULL);
1460 J_ASSERT(journal->j_committing_transaction == NULL);
1461 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1462 spin_unlock(&journal->j_list_lock);
1463
Dave Kleikamp470decc2006-10-11 01:20:57 -07001464 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001465 if (!is_journal_aborted(journal)) {
1466 /* We can now mark the journal as empty. */
1467 journal->j_tail = 0;
1468 journal->j_tail_sequence =
1469 ++journal->j_transaction_sequence;
1470 jbd2_journal_update_superblock(journal, 1);
1471 } else {
1472 err = -EIO;
1473 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001474 brelse(journal->j_sb_buffer);
1475 }
1476
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001477 if (journal->j_proc_entry)
1478 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001479 if (journal->j_inode)
1480 iput(journal->j_inode);
1481 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001482 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001483 kfree(journal->j_wbuf);
1484 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001485
1486 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487}
1488
1489
1490/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001491 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001492 * @journal: Journal to check.
1493 * @compat: bitmask of compatible features
1494 * @ro: bitmask of features that force read-only mount
1495 * @incompat: bitmask of incompatible features
1496 *
1497 * Check whether the journal uses all of a given set of
1498 * features. Return true (non-zero) if it does.
1499 **/
1500
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001501int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001502 unsigned long ro, unsigned long incompat)
1503{
1504 journal_superblock_t *sb;
1505
1506 if (!compat && !ro && !incompat)
1507 return 1;
1508 if (journal->j_format_version == 1)
1509 return 0;
1510
1511 sb = journal->j_superblock;
1512
1513 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1514 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1515 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1516 return 1;
1517
1518 return 0;
1519}
1520
1521/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001522 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001523 * @journal: Journal to check.
1524 * @compat: bitmask of compatible features
1525 * @ro: bitmask of features that force read-only mount
1526 * @incompat: bitmask of incompatible features
1527 *
1528 * Check whether the journaling code supports the use of
1529 * all of a given set of features on this journal. Return true
1530 * (non-zero) if it can. */
1531
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001532int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001533 unsigned long ro, unsigned long incompat)
1534{
1535 journal_superblock_t *sb;
1536
1537 if (!compat && !ro && !incompat)
1538 return 1;
1539
1540 sb = journal->j_superblock;
1541
1542 /* We can support any known requested features iff the
1543 * superblock is in version 2. Otherwise we fail to support any
1544 * extended sb features. */
1545
1546 if (journal->j_format_version != 2)
1547 return 0;
1548
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001549 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1550 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1551 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001552 return 1;
1553
1554 return 0;
1555}
1556
1557/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001558 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559 * @journal: Journal to act on.
1560 * @compat: bitmask of compatible features
1561 * @ro: bitmask of features that force read-only mount
1562 * @incompat: bitmask of incompatible features
1563 *
1564 * Mark a given journal feature as present on the
1565 * superblock. Returns true if the requested features could be set.
1566 *
1567 */
1568
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001569int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 unsigned long ro, unsigned long incompat)
1571{
1572 journal_superblock_t *sb;
1573
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001574 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001575 return 1;
1576
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001577 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001578 return 0;
1579
1580 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1581 compat, ro, incompat);
1582
1583 sb = journal->j_superblock;
1584
1585 sb->s_feature_compat |= cpu_to_be32(compat);
1586 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1587 sb->s_feature_incompat |= cpu_to_be32(incompat);
1588
1589 return 1;
1590}
1591
Girish Shilamkar818d2762008-01-28 23:58:27 -05001592/*
1593 * jbd2_journal_clear_features () - Clear a given journal feature in the
1594 * superblock
1595 * @journal: Journal to act on.
1596 * @compat: bitmask of compatible features
1597 * @ro: bitmask of features that force read-only mount
1598 * @incompat: bitmask of incompatible features
1599 *
1600 * Clear a given journal feature as present on the
1601 * superblock.
1602 */
1603void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1604 unsigned long ro, unsigned long incompat)
1605{
1606 journal_superblock_t *sb;
1607
1608 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1609 compat, ro, incompat);
1610
1611 sb = journal->j_superblock;
1612
1613 sb->s_feature_compat &= ~cpu_to_be32(compat);
1614 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1615 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1616}
1617EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001618
1619/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001620 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001621 * @journal: Journal to act on.
1622 *
1623 * Given an initialised but unloaded journal struct, poke about in the
1624 * on-disk structure to update it to the most recent supported version.
1625 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001626int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627{
1628 journal_superblock_t *sb;
1629 int err;
1630
1631 err = journal_get_superblock(journal);
1632 if (err)
1633 return err;
1634
1635 sb = journal->j_superblock;
1636
1637 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001640 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001641 return journal_convert_superblock_v1(journal, sb);
1642 default:
1643 break;
1644 }
1645 return -EINVAL;
1646}
1647
1648static int journal_convert_superblock_v1(journal_t *journal,
1649 journal_superblock_t *sb)
1650{
1651 int offset, blocksize;
1652 struct buffer_head *bh;
1653
1654 printk(KERN_WARNING
1655 "JBD: Converting superblock from version 1 to 2.\n");
1656
1657 /* Pre-initialise new fields to zero */
1658 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1659 blocksize = be32_to_cpu(sb->s_blocksize);
1660 memset(&sb->s_feature_compat, 0, blocksize-offset);
1661
1662 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001663 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664 journal->j_format_version = 2;
1665
1666 bh = journal->j_sb_buffer;
1667 BUFFER_TRACE(bh, "marking dirty");
1668 mark_buffer_dirty(bh);
1669 sync_dirty_buffer(bh);
1670 return 0;
1671}
1672
1673
1674/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001675 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001676 * @journal: Journal to act on.
1677 *
1678 * Flush all data for a given journal to disk and empty the journal.
1679 * Filesystems can use this when remounting readonly to ensure that
1680 * recovery does not need to happen on remount.
1681 */
1682
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001683int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001684{
1685 int err = 0;
1686 transaction_t *transaction = NULL;
1687 unsigned long old_tail;
1688
1689 spin_lock(&journal->j_state_lock);
1690
1691 /* Force everything buffered to the log... */
1692 if (journal->j_running_transaction) {
1693 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001694 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001695 } else if (journal->j_committing_transaction)
1696 transaction = journal->j_committing_transaction;
1697
1698 /* Wait for the log commit to complete... */
1699 if (transaction) {
1700 tid_t tid = transaction->t_tid;
1701
1702 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001703 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 } else {
1705 spin_unlock(&journal->j_state_lock);
1706 }
1707
1708 /* ...and flush everything in the log out to disk. */
1709 spin_lock(&journal->j_list_lock);
1710 while (!err && journal->j_checkpoint_transactions != NULL) {
1711 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001712 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001713 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001714 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715 spin_lock(&journal->j_list_lock);
1716 }
1717 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001718
1719 if (is_journal_aborted(journal))
1720 return -EIO;
1721
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001722 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001723
1724 /* Finally, mark the journal as really needing no recovery.
1725 * This sets s_start==0 in the underlying superblock, which is
1726 * the magic code for a fully-recovered superblock. Any future
1727 * commits of data to the journal will restore the current
1728 * s_start value. */
1729 spin_lock(&journal->j_state_lock);
1730 old_tail = journal->j_tail;
1731 journal->j_tail = 0;
1732 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001733 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001734 spin_lock(&journal->j_state_lock);
1735 journal->j_tail = old_tail;
1736
1737 J_ASSERT(!journal->j_running_transaction);
1738 J_ASSERT(!journal->j_committing_transaction);
1739 J_ASSERT(!journal->j_checkpoint_transactions);
1740 J_ASSERT(journal->j_head == journal->j_tail);
1741 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1742 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001743 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001744}
1745
1746/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001747 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001748 * @journal: Journal to act on.
1749 * @write: flag (see below)
1750 *
1751 * Wipe out all of the contents of a journal, safely. This will produce
1752 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001753 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754 *
1755 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1756 * we merely suppress recovery.
1757 */
1758
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001759int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760{
1761 journal_superblock_t *sb;
1762 int err = 0;
1763
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001764 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001765
1766 err = load_superblock(journal);
1767 if (err)
1768 return err;
1769
1770 sb = journal->j_superblock;
1771
1772 if (!journal->j_tail)
1773 goto no_recovery;
1774
1775 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1776 write ? "Clearing" : "Ignoring");
1777
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001778 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001780 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001781
1782 no_recovery:
1783 return err;
1784}
1785
1786/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787 * Journal abort has very specific semantics, which we describe
1788 * for journal abort.
1789 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001790 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 * itself are here.
1792 */
1793
1794/*
1795 * Quick version for internal journal use (doesn't lock the journal).
1796 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1797 * and don't attempt to make any other journal updates.
1798 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001799void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001800{
1801 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001803 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804 return;
1805
1806 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001807 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808
1809 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001810 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 transaction = journal->j_running_transaction;
1812 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001813 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814 spin_unlock(&journal->j_state_lock);
1815}
1816
1817/* Soft abort: record the abort error status in the journal superblock,
1818 * but don't do any other IO. */
1819static void __journal_abort_soft (journal_t *journal, int errno)
1820{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001821 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822 return;
1823
1824 if (!journal->j_errno)
1825 journal->j_errno = errno;
1826
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001827 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828
1829 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001830 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001831}
1832
1833/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001834 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001835 * @journal: the journal to shutdown.
1836 * @errno: an error number to record in the journal indicating
1837 * the reason for the shutdown.
1838 *
1839 * Perform a complete, immediate shutdown of the ENTIRE
1840 * journal (not of a single transaction). This operation cannot be
1841 * undone without closing and reopening the journal.
1842 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001843 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001844 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1845 * mode.
1846 *
1847 * Journal abort has very specific semantics. Any existing dirty,
1848 * unjournaled buffers in the main filesystem will still be written to
1849 * disk by bdflush, but the journaling mechanism will be suspended
1850 * immediately and no further transaction commits will be honoured.
1851 *
1852 * Any dirty, journaled buffers will be written back to disk without
1853 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1854 * filesystem, but we _do_ attempt to leave as much data as possible
1855 * behind for fsck to use for cleanup.
1856 *
1857 * Any attempt to get a new transaction handle on a journal which is in
1858 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001859 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001860 * entered abort state during the update.
1861 *
1862 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001863 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001864 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001865 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866 * which will be recorded (if possible) in the journal superblock. This
1867 * allows a client to record failure conditions in the middle of a
1868 * transaction without having to complete the transaction to record the
1869 * failure to disk. ext3_error, for example, now uses this
1870 * functionality.
1871 *
1872 * Errors which originate from within the journaling layer will NOT
1873 * supply an errno; a null errno implies that absolutely no further
1874 * writes are done to the journal (unless there are any already in
1875 * progress).
1876 *
1877 */
1878
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001879void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880{
1881 __journal_abort_soft(journal, errno);
1882}
1883
1884/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001885 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001886 * @journal: journal to examine.
1887 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001888 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889 * time the journal was mounted - if the journal was stopped
1890 * without calling abort this will be 0.
1891 *
1892 * If the journal has been aborted on this mount time -EROFS will
1893 * be returned.
1894 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001895int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001896{
1897 int err;
1898
1899 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001900 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001901 err = -EROFS;
1902 else
1903 err = journal->j_errno;
1904 spin_unlock(&journal->j_state_lock);
1905 return err;
1906}
1907
1908/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 * @journal: journal to act on.
1911 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001912 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 * mode.
1914 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001915int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001916{
1917 int err = 0;
1918
1919 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001920 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001921 err = -EROFS;
1922 else
1923 journal->j_errno = 0;
1924 spin_unlock(&journal->j_state_lock);
1925 return err;
1926}
1927
1928/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001929 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001930 * @journal: journal to act on.
1931 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001932 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001933 * mode.
1934 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001935void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936{
1937 spin_lock(&journal->j_state_lock);
1938 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001939 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001940 spin_unlock(&journal->j_state_lock);
1941}
1942
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001943int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001944{
1945 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1946}
1947
1948/*
Zach Brownb517bea2006-10-11 01:21:08 -07001949 * helper functions to deal with 32 or 64bit block numbers.
1950 */
1951size_t journal_tag_bytes(journal_t *journal)
1952{
1953 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001954 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001955 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001956 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001957}
1958
1959/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 * Journal_head storage management
1961 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001962static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001963#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1965#endif
1966
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001967static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968{
1969 int retval;
1970
Al Viro1076d172008-03-29 03:07:18 +00001971 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001972 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973 sizeof(struct journal_head),
1974 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001975 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001976 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001977 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001978 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001979 retval = -ENOMEM;
1980 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1981 }
1982 return retval;
1983}
1984
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001985static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001986{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001987 if (jbd2_journal_head_cache) {
1988 kmem_cache_destroy(jbd2_journal_head_cache);
1989 jbd2_journal_head_cache = NULL;
1990 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991}
1992
1993/*
1994 * journal_head splicing and dicing
1995 */
1996static struct journal_head *journal_alloc_journal_head(void)
1997{
1998 struct journal_head *ret;
1999 static unsigned long last_warning;
2000
Jose R. Santose23291b2007-07-18 08:57:06 -04002001#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002002 atomic_inc(&nr_journal_heads);
2003#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002004 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002005 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002006 jbd_debug(1, "out of memory for journal_head\n");
2007 if (time_after(jiffies, last_warning + 5*HZ)) {
2008 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002009 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002010 last_warning = jiffies;
2011 }
Al Viro1076d172008-03-29 03:07:18 +00002012 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002014 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002015 }
2016 }
2017 return ret;
2018}
2019
2020static void journal_free_journal_head(struct journal_head *jh)
2021{
Jose R. Santose23291b2007-07-18 08:57:06 -04002022#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002024 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002025#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002026 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002027}
2028
2029/*
2030 * A journal_head is attached to a buffer_head whenever JBD has an
2031 * interest in the buffer.
2032 *
2033 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2034 * is set. This bit is tested in core kernel code where we need to take
2035 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2036 * there.
2037 *
2038 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2039 *
2040 * When a buffer has its BH_JBD bit set it is immune from being released by
2041 * core kernel code, mainly via ->b_count.
2042 *
2043 * A journal_head may be detached from its buffer_head when the journal_head's
2044 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002045 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002046 * journal_head can be dropped if needed.
2047 *
2048 * Various places in the kernel want to attach a journal_head to a buffer_head
2049 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002050 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002051 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002052 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002053 *
2054 * So the typical usage would be:
2055 *
2056 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002057 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058 * ...
2059 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002060 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002061 *
2062 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2063 * because it has a non-zero b_transaction.
2064 */
2065
2066/*
2067 * Give a buffer_head a journal_head.
2068 *
2069 * Doesn't need the journal lock.
2070 * May sleep.
2071 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002072struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002073{
2074 struct journal_head *jh;
2075 struct journal_head *new_jh = NULL;
2076
2077repeat:
2078 if (!buffer_jbd(bh)) {
2079 new_jh = journal_alloc_journal_head();
2080 memset(new_jh, 0, sizeof(*new_jh));
2081 }
2082
2083 jbd_lock_bh_journal_head(bh);
2084 if (buffer_jbd(bh)) {
2085 jh = bh2jh(bh);
2086 } else {
2087 J_ASSERT_BH(bh,
2088 (atomic_read(&bh->b_count) > 0) ||
2089 (bh->b_page && bh->b_page->mapping));
2090
2091 if (!new_jh) {
2092 jbd_unlock_bh_journal_head(bh);
2093 goto repeat;
2094 }
2095
2096 jh = new_jh;
2097 new_jh = NULL; /* We consumed it */
2098 set_buffer_jbd(bh);
2099 bh->b_private = jh;
2100 jh->b_bh = bh;
2101 get_bh(bh);
2102 BUFFER_TRACE(bh, "added journal_head");
2103 }
2104 jh->b_jcount++;
2105 jbd_unlock_bh_journal_head(bh);
2106 if (new_jh)
2107 journal_free_journal_head(new_jh);
2108 return bh->b_private;
2109}
2110
2111/*
2112 * Grab a ref against this buffer_head's journal_head. If it ended up not
2113 * having a journal_head, return NULL
2114 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002115struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002116{
2117 struct journal_head *jh = NULL;
2118
2119 jbd_lock_bh_journal_head(bh);
2120 if (buffer_jbd(bh)) {
2121 jh = bh2jh(bh);
2122 jh->b_jcount++;
2123 }
2124 jbd_unlock_bh_journal_head(bh);
2125 return jh;
2126}
2127
2128static void __journal_remove_journal_head(struct buffer_head *bh)
2129{
2130 struct journal_head *jh = bh2jh(bh);
2131
2132 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2133
2134 get_bh(bh);
2135 if (jh->b_jcount == 0) {
2136 if (jh->b_transaction == NULL &&
2137 jh->b_next_transaction == NULL &&
2138 jh->b_cp_transaction == NULL) {
2139 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2140 J_ASSERT_BH(bh, buffer_jbd(bh));
2141 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2142 BUFFER_TRACE(bh, "remove journal_head");
2143 if (jh->b_frozen_data) {
2144 printk(KERN_WARNING "%s: freeing "
2145 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002146 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002147 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002148 }
2149 if (jh->b_committed_data) {
2150 printk(KERN_WARNING "%s: freeing "
2151 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002152 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002153 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002154 }
2155 bh->b_private = NULL;
2156 jh->b_bh = NULL; /* debug, really */
2157 clear_buffer_jbd(bh);
2158 __brelse(bh);
2159 journal_free_journal_head(jh);
2160 } else {
2161 BUFFER_TRACE(bh, "journal_head was locked");
2162 }
2163 }
2164}
2165
2166/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002167 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002168 * and has a zero b_jcount then remove and release its journal_head. If we did
2169 * see that the buffer is not used by any transaction we also "logically"
2170 * decrement ->b_count.
2171 *
2172 * We in fact take an additional increment on ->b_count as a convenience,
2173 * because the caller usually wants to do additional things with the bh
2174 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002175 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002176 * time. Once the caller has run __brelse(), the buffer is eligible for
2177 * reaping by try_to_free_buffers().
2178 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002179void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002180{
2181 jbd_lock_bh_journal_head(bh);
2182 __journal_remove_journal_head(bh);
2183 jbd_unlock_bh_journal_head(bh);
2184}
2185
2186/*
2187 * Drop a reference on the passed journal_head. If it fell to zero then try to
2188 * release the journal_head from the buffer_head.
2189 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002190void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002191{
2192 struct buffer_head *bh = jh2bh(jh);
2193
2194 jbd_lock_bh_journal_head(bh);
2195 J_ASSERT_JH(jh, jh->b_jcount > 0);
2196 --jh->b_jcount;
2197 if (!jh->b_jcount && !jh->b_transaction) {
2198 __journal_remove_journal_head(bh);
2199 __brelse(bh);
2200 }
2201 jbd_unlock_bh_journal_head(bh);
2202}
2203
2204/*
Jan Karac851ed52008-07-11 19:27:31 -04002205 * Initialize jbd inode head
2206 */
2207void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2208{
2209 jinode->i_transaction = NULL;
2210 jinode->i_next_transaction = NULL;
2211 jinode->i_vfs_inode = inode;
2212 jinode->i_flags = 0;
2213 INIT_LIST_HEAD(&jinode->i_list);
2214}
2215
2216/*
2217 * Function to be called before we start removing inode from memory (i.e.,
2218 * clear_inode() is a fine place to be called from). It removes inode from
2219 * transaction's lists.
2220 */
2221void jbd2_journal_release_jbd_inode(journal_t *journal,
2222 struct jbd2_inode *jinode)
2223{
2224 int writeout = 0;
2225
2226 if (!journal)
2227 return;
2228restart:
2229 spin_lock(&journal->j_list_lock);
2230 /* Is commit writing out inode - we have to wait */
2231 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2232 wait_queue_head_t *wq;
2233 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2234 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2235 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2236 spin_unlock(&journal->j_list_lock);
2237 schedule();
2238 finish_wait(wq, &wait.wait);
2239 goto restart;
2240 }
2241
2242 /* Do we need to wait for data writeback? */
2243 if (journal->j_committing_transaction == jinode->i_transaction)
2244 writeout = 1;
2245 if (jinode->i_transaction) {
2246 list_del(&jinode->i_list);
2247 jinode->i_transaction = NULL;
2248 }
2249 spin_unlock(&journal->j_list_lock);
2250}
2251
2252/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002253 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002255#ifdef CONFIG_JBD2_DEBUG
2256u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002257EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002258
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002259#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002260
Jose R. Santos6f38c742007-10-16 18:38:25 -04002261static struct dentry *jbd2_debugfs_dir;
2262static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002263
2264static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002265{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002266 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2267 if (jbd2_debugfs_dir)
2268 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2269 jbd2_debugfs_dir,
2270 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002271}
2272
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002273static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002274{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002275 debugfs_remove(jbd2_debug);
2276 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002277}
2278
2279#else
2280
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002281static void __init jbd2_create_debugfs_entry(void)
2282{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002283}
2284
2285static void __exit jbd2_remove_debugfs_entry(void)
2286{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002287}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288
2289#endif
2290
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002291#ifdef CONFIG_PROC_FS
2292
2293#define JBD2_STATS_PROC_NAME "fs/jbd2"
2294
2295static void __init jbd2_create_jbd_stats_proc_entry(void)
2296{
2297 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2298}
2299
2300static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2301{
2302 if (proc_jbd2_stats)
2303 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2304}
2305
2306#else
2307
2308#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2309#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2310
2311#endif
2312
Christoph Lametere18b8902006-12-06 20:33:20 -08002313struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002314
2315static int __init journal_init_handle_cache(void)
2316{
Johann Lombardia920e942006-10-11 01:21:00 -07002317 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002318 sizeof(handle_t),
2319 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002320 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002321 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002322 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002323 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2324 return -ENOMEM;
2325 }
2326 return 0;
2327}
2328
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002329static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002330{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002331 if (jbd2_handle_cache)
2332 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002333}
2334
2335/*
2336 * Module startup and shutdown
2337 */
2338
2339static int __init journal_init_caches(void)
2340{
2341 int ret;
2342
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002343 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002344 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002345 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002346 if (ret == 0)
2347 ret = journal_init_handle_cache();
2348 return ret;
2349}
2350
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002351static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002352{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002353 jbd2_journal_destroy_revoke_caches();
2354 jbd2_journal_destroy_jbd2_journal_head_cache();
2355 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002356}
2357
2358static int __init journal_init(void)
2359{
2360 int ret;
2361
2362 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2363
2364 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002365 if (ret == 0) {
2366 jbd2_create_debugfs_entry();
2367 jbd2_create_jbd_stats_proc_entry();
2368 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002369 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002370 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002371 return ret;
2372}
2373
2374static void __exit journal_exit(void)
2375{
Jose R. Santose23291b2007-07-18 08:57:06 -04002376#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002377 int n = atomic_read(&nr_journal_heads);
2378 if (n)
2379 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2380#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002381 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002382 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002383 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002384}
2385
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002386/*
2387 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2388 * tracing infrastructure to map a dev_t to a device name.
2389 *
2390 * The caller should use rcu_read_lock() in order to make sure the
2391 * device name stays valid until its done with it. We use
2392 * rcu_read_lock() as well to make sure we're safe in case the caller
2393 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2394 * nested.
2395 */
2396struct devname_cache {
2397 struct rcu_head rcu;
2398 dev_t device;
2399 char devname[BDEVNAME_SIZE];
2400};
2401#define CACHE_SIZE_BITS 6
2402static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2403static DEFINE_SPINLOCK(devname_cache_lock);
2404
2405static void free_devcache(struct rcu_head *rcu)
2406{
2407 kfree(rcu);
2408}
2409
2410const char *jbd2_dev_to_name(dev_t device)
2411{
2412 int i = hash_32(device, CACHE_SIZE_BITS);
2413 char *ret;
2414 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002415 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002416
2417 rcu_read_lock();
2418 if (devcache[i] && devcache[i]->device == device) {
2419 ret = devcache[i]->devname;
2420 rcu_read_unlock();
2421 return ret;
2422 }
2423 rcu_read_unlock();
2424
Theodore Ts'ob5744802009-06-20 23:34:44 -04002425 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2426 if (!new_dev)
2427 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002428 spin_lock(&devname_cache_lock);
2429 if (devcache[i]) {
2430 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002431 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002432 ret = devcache[i]->devname;
2433 spin_unlock(&devname_cache_lock);
2434 return ret;
2435 }
2436 call_rcu(&devcache[i]->rcu, free_devcache);
2437 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002438 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002439 devcache[i]->device = device;
2440 bd = bdget(device);
2441 if (bd) {
2442 bdevname(bd, devcache[i]->devname);
2443 bdput(bd);
2444 } else
2445 __bdevname(device, devcache[i]->devname);
2446 ret = devcache[i]->devname;
2447 spin_unlock(&devname_cache_lock);
2448 return ret;
2449}
2450EXPORT_SYMBOL(jbd2_dev_to_name);
2451
Dave Kleikamp470decc2006-10-11 01:20:57 -07002452MODULE_LICENSE("GPL");
2453module_init(journal_init);
2454module_exit(journal_exit);
2455