blob: 53b86e16e5fe3a4bc29f8825db8b2a54b7e60fde [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
James Morris88e9d342009-09-22 16:43:43 -0700771static const struct seq_operations jbd2_seq_history_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500772 .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
James Morris88e9d342009-09-22 16:43:43 -0700875static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500876 .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);
Jan Karaf6f50e22009-07-17 10:40:01 -04001190 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1191 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1192 first, last);
1193 journal_fail_superblock(journal);
1194 return -EINVAL;
1195 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001196
1197 journal->j_first = first;
1198 journal->j_last = last;
1199
1200 journal->j_head = first;
1201 journal->j_tail = first;
1202 journal->j_free = last - first;
1203
1204 journal->j_tail_sequence = journal->j_transaction_sequence;
1205 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1206 journal->j_commit_request = journal->j_commit_sequence;
1207
1208 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1209
1210 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001211 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001212 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001213}
1214
1215/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001216 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001217 * @journal: The journal to update.
1218 * @wait: Set to '0' if you don't want to wait for IO completion.
1219 *
1220 * Update a journal's dynamic superblock fields and write it to disk,
1221 * optionally waiting for the IO to complete.
1222 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001223void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001224{
1225 journal_superblock_t *sb = journal->j_superblock;
1226 struct buffer_head *bh = journal->j_sb_buffer;
1227
1228 /*
1229 * As a special case, if the on-disk copy is already marked as needing
1230 * no recovery (s_start == 0) and there are no outstanding transactions
1231 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001232 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001233 * attempting a write to a potential-readonly device.
1234 */
1235 if (sb->s_start == 0 && journal->j_tail_sequence ==
1236 journal->j_transaction_sequence) {
1237 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1238 "(start %ld, seq %d, errno %d)\n",
1239 journal->j_tail, journal->j_tail_sequence,
1240 journal->j_errno);
1241 goto out;
1242 }
1243
Theodore Ts'o914258b2008-10-06 21:35:40 -04001244 if (buffer_write_io_error(bh)) {
1245 /*
1246 * Oh, dear. A previous attempt to write the journal
1247 * superblock failed. This could happen because the
1248 * USB device was yanked out. Or it could happen to
1249 * be a transient write error and maybe the block will
1250 * be remapped. Nothing we can do but to retry the
1251 * write and hope for the best.
1252 */
1253 printk(KERN_ERR "JBD2: previous I/O error detected "
1254 "for journal superblock update for %s.\n",
1255 journal->j_devname);
1256 clear_buffer_write_io_error(bh);
1257 set_buffer_uptodate(bh);
1258 }
1259
Dave Kleikamp470decc2006-10-11 01:20:57 -07001260 spin_lock(&journal->j_state_lock);
1261 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1262 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1263
1264 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1265 sb->s_start = cpu_to_be32(journal->j_tail);
1266 sb->s_errno = cpu_to_be32(journal->j_errno);
1267 spin_unlock(&journal->j_state_lock);
1268
1269 BUFFER_TRACE(bh, "marking dirty");
1270 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001271 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001272 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001273 if (buffer_write_io_error(bh)) {
1274 printk(KERN_ERR "JBD2: I/O error detected "
1275 "when updating journal superblock for %s.\n",
1276 journal->j_devname);
1277 clear_buffer_write_io_error(bh);
1278 set_buffer_uptodate(bh);
1279 }
1280 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001281 ll_rw_block(SWRITE, 1, &bh);
1282
1283out:
1284 /* If we have just flushed the log (by marking s_start==0), then
1285 * any future commit will have to be careful to update the
1286 * superblock again to re-record the true start of the log. */
1287
1288 spin_lock(&journal->j_state_lock);
1289 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001290 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001291 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001292 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001293 spin_unlock(&journal->j_state_lock);
1294}
1295
1296/*
1297 * Read the superblock for a given journal, performing initial
1298 * validation of the format.
1299 */
1300
1301static int journal_get_superblock(journal_t *journal)
1302{
1303 struct buffer_head *bh;
1304 journal_superblock_t *sb;
1305 int err = -EIO;
1306
1307 bh = journal->j_sb_buffer;
1308
1309 J_ASSERT(bh != NULL);
1310 if (!buffer_uptodate(bh)) {
1311 ll_rw_block(READ, 1, &bh);
1312 wait_on_buffer(bh);
1313 if (!buffer_uptodate(bh)) {
1314 printk (KERN_ERR
1315 "JBD: IO error reading journal superblock\n");
1316 goto out;
1317 }
1318 }
1319
1320 sb = journal->j_superblock;
1321
1322 err = -EINVAL;
1323
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001324 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001325 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1326 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1327 goto out;
1328 }
1329
1330 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001331 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001332 journal->j_format_version = 1;
1333 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001334 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001335 journal->j_format_version = 2;
1336 break;
1337 default:
1338 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1339 goto out;
1340 }
1341
1342 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1343 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1344 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1345 printk (KERN_WARNING "JBD: journal file too short\n");
1346 goto out;
1347 }
1348
1349 return 0;
1350
1351out:
1352 journal_fail_superblock(journal);
1353 return err;
1354}
1355
1356/*
1357 * Load the on-disk journal superblock and read the key fields into the
1358 * journal_t.
1359 */
1360
1361static int load_superblock(journal_t *journal)
1362{
1363 int err;
1364 journal_superblock_t *sb;
1365
1366 err = journal_get_superblock(journal);
1367 if (err)
1368 return err;
1369
1370 sb = journal->j_superblock;
1371
1372 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1373 journal->j_tail = be32_to_cpu(sb->s_start);
1374 journal->j_first = be32_to_cpu(sb->s_first);
1375 journal->j_last = be32_to_cpu(sb->s_maxlen);
1376 journal->j_errno = be32_to_cpu(sb->s_errno);
1377
1378 return 0;
1379}
1380
1381
1382/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001383 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001384 * @journal: Journal to act on.
1385 *
1386 * Given a journal_t structure which tells us which disk blocks contain
1387 * a journal, read the journal from disk to initialise the in-memory
1388 * structures.
1389 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001390int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391{
1392 int err;
1393 journal_superblock_t *sb;
1394
1395 err = load_superblock(journal);
1396 if (err)
1397 return err;
1398
1399 sb = journal->j_superblock;
1400 /* If this is a V2 superblock, then we have to check the
1401 * features flags on it. */
1402
1403 if (journal->j_format_version >= 2) {
1404 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001405 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001407 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 printk (KERN_WARNING
1409 "JBD: Unrecognised features on journal\n");
1410 return -EINVAL;
1411 }
1412 }
1413
Dave Kleikamp470decc2006-10-11 01:20:57 -07001414 /* Let the recovery code check whether it needs to recover any
1415 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001416 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001417 goto recovery_error;
1418
1419 /* OK, we've finished with the dynamic journal bits:
1420 * reinitialise the dynamic contents of the superblock in memory
1421 * and reset them on disk. */
1422 if (journal_reset(journal))
1423 goto recovery_error;
1424
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001425 journal->j_flags &= ~JBD2_ABORT;
1426 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 return 0;
1428
1429recovery_error:
1430 printk (KERN_WARNING "JBD: recovery failed\n");
1431 return -EIO;
1432}
1433
1434/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001435 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436 * @journal: Journal to act on.
1437 *
1438 * Release a journal_t structure once it is no longer in use by the
1439 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001440 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001442int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001444 int err = 0;
1445
Dave Kleikamp470decc2006-10-11 01:20:57 -07001446 /* Wait for the commit thread to wake up and die. */
1447 journal_kill_thread(journal);
1448
1449 /* Force a final log commit */
1450 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001451 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001452
1453 /* Force any old transactions to disk */
1454
1455 /* Totally anal locking here... */
1456 spin_lock(&journal->j_list_lock);
1457 while (journal->j_checkpoint_transactions != NULL) {
1458 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001459 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001460 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001461 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 spin_lock(&journal->j_list_lock);
1463 }
1464
1465 J_ASSERT(journal->j_running_transaction == NULL);
1466 J_ASSERT(journal->j_committing_transaction == NULL);
1467 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1468 spin_unlock(&journal->j_list_lock);
1469
Dave Kleikamp470decc2006-10-11 01:20:57 -07001470 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001471 if (!is_journal_aborted(journal)) {
1472 /* We can now mark the journal as empty. */
1473 journal->j_tail = 0;
1474 journal->j_tail_sequence =
1475 ++journal->j_transaction_sequence;
1476 jbd2_journal_update_superblock(journal, 1);
1477 } else {
1478 err = -EIO;
1479 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001480 brelse(journal->j_sb_buffer);
1481 }
1482
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001483 if (journal->j_proc_entry)
1484 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001485 if (journal->j_inode)
1486 iput(journal->j_inode);
1487 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001488 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001489 kfree(journal->j_wbuf);
1490 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001491
1492 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001493}
1494
1495
1496/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001497 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001498 * @journal: Journal to check.
1499 * @compat: bitmask of compatible features
1500 * @ro: bitmask of features that force read-only mount
1501 * @incompat: bitmask of incompatible features
1502 *
1503 * Check whether the journal uses all of a given set of
1504 * features. Return true (non-zero) if it does.
1505 **/
1506
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001507int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001508 unsigned long ro, unsigned long incompat)
1509{
1510 journal_superblock_t *sb;
1511
1512 if (!compat && !ro && !incompat)
1513 return 1;
1514 if (journal->j_format_version == 1)
1515 return 0;
1516
1517 sb = journal->j_superblock;
1518
1519 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1520 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1521 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1522 return 1;
1523
1524 return 0;
1525}
1526
1527/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001528 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001529 * @journal: Journal to check.
1530 * @compat: bitmask of compatible features
1531 * @ro: bitmask of features that force read-only mount
1532 * @incompat: bitmask of incompatible features
1533 *
1534 * Check whether the journaling code supports the use of
1535 * all of a given set of features on this journal. Return true
1536 * (non-zero) if it can. */
1537
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001538int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001539 unsigned long ro, unsigned long incompat)
1540{
1541 journal_superblock_t *sb;
1542
1543 if (!compat && !ro && !incompat)
1544 return 1;
1545
1546 sb = journal->j_superblock;
1547
1548 /* We can support any known requested features iff the
1549 * superblock is in version 2. Otherwise we fail to support any
1550 * extended sb features. */
1551
1552 if (journal->j_format_version != 2)
1553 return 0;
1554
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001555 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1556 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1557 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001558 return 1;
1559
1560 return 0;
1561}
1562
1563/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001564 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001565 * @journal: Journal to act on.
1566 * @compat: bitmask of compatible features
1567 * @ro: bitmask of features that force read-only mount
1568 * @incompat: bitmask of incompatible features
1569 *
1570 * Mark a given journal feature as present on the
1571 * superblock. Returns true if the requested features could be set.
1572 *
1573 */
1574
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001575int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001576 unsigned long ro, unsigned long incompat)
1577{
1578 journal_superblock_t *sb;
1579
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001580 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001581 return 1;
1582
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001583 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001584 return 0;
1585
1586 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1587 compat, ro, incompat);
1588
1589 sb = journal->j_superblock;
1590
1591 sb->s_feature_compat |= cpu_to_be32(compat);
1592 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1593 sb->s_feature_incompat |= cpu_to_be32(incompat);
1594
1595 return 1;
1596}
1597
Girish Shilamkar818d2762008-01-28 23:58:27 -05001598/*
1599 * jbd2_journal_clear_features () - Clear a given journal feature in the
1600 * superblock
1601 * @journal: Journal to act on.
1602 * @compat: bitmask of compatible features
1603 * @ro: bitmask of features that force read-only mount
1604 * @incompat: bitmask of incompatible features
1605 *
1606 * Clear a given journal feature as present on the
1607 * superblock.
1608 */
1609void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1610 unsigned long ro, unsigned long incompat)
1611{
1612 journal_superblock_t *sb;
1613
1614 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1615 compat, ro, incompat);
1616
1617 sb = journal->j_superblock;
1618
1619 sb->s_feature_compat &= ~cpu_to_be32(compat);
1620 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1621 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1622}
1623EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624
1625/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001626 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627 * @journal: Journal to act on.
1628 *
1629 * Given an initialised but unloaded journal struct, poke about in the
1630 * on-disk structure to update it to the most recent supported version.
1631 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633{
1634 journal_superblock_t *sb;
1635 int err;
1636
1637 err = journal_get_superblock(journal);
1638 if (err)
1639 return err;
1640
1641 sb = journal->j_superblock;
1642
1643 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001644 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001646 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647 return journal_convert_superblock_v1(journal, sb);
1648 default:
1649 break;
1650 }
1651 return -EINVAL;
1652}
1653
1654static int journal_convert_superblock_v1(journal_t *journal,
1655 journal_superblock_t *sb)
1656{
1657 int offset, blocksize;
1658 struct buffer_head *bh;
1659
1660 printk(KERN_WARNING
1661 "JBD: Converting superblock from version 1 to 2.\n");
1662
1663 /* Pre-initialise new fields to zero */
1664 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1665 blocksize = be32_to_cpu(sb->s_blocksize);
1666 memset(&sb->s_feature_compat, 0, blocksize-offset);
1667
1668 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001669 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670 journal->j_format_version = 2;
1671
1672 bh = journal->j_sb_buffer;
1673 BUFFER_TRACE(bh, "marking dirty");
1674 mark_buffer_dirty(bh);
1675 sync_dirty_buffer(bh);
1676 return 0;
1677}
1678
1679
1680/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001681 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001682 * @journal: Journal to act on.
1683 *
1684 * Flush all data for a given journal to disk and empty the journal.
1685 * Filesystems can use this when remounting readonly to ensure that
1686 * recovery does not need to happen on remount.
1687 */
1688
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001689int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001690{
1691 int err = 0;
1692 transaction_t *transaction = NULL;
1693 unsigned long old_tail;
1694
1695 spin_lock(&journal->j_state_lock);
1696
1697 /* Force everything buffered to the log... */
1698 if (journal->j_running_transaction) {
1699 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001700 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701 } else if (journal->j_committing_transaction)
1702 transaction = journal->j_committing_transaction;
1703
1704 /* Wait for the log commit to complete... */
1705 if (transaction) {
1706 tid_t tid = transaction->t_tid;
1707
1708 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001709 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001710 } else {
1711 spin_unlock(&journal->j_state_lock);
1712 }
1713
1714 /* ...and flush everything in the log out to disk. */
1715 spin_lock(&journal->j_list_lock);
1716 while (!err && journal->j_checkpoint_transactions != NULL) {
1717 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001718 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001719 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001720 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001721 spin_lock(&journal->j_list_lock);
1722 }
1723 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001724
1725 if (is_journal_aborted(journal))
1726 return -EIO;
1727
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001728 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729
1730 /* Finally, mark the journal as really needing no recovery.
1731 * This sets s_start==0 in the underlying superblock, which is
1732 * the magic code for a fully-recovered superblock. Any future
1733 * commits of data to the journal will restore the current
1734 * s_start value. */
1735 spin_lock(&journal->j_state_lock);
1736 old_tail = journal->j_tail;
1737 journal->j_tail = 0;
1738 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001739 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001740 spin_lock(&journal->j_state_lock);
1741 journal->j_tail = old_tail;
1742
1743 J_ASSERT(!journal->j_running_transaction);
1744 J_ASSERT(!journal->j_committing_transaction);
1745 J_ASSERT(!journal->j_checkpoint_transactions);
1746 J_ASSERT(journal->j_head == journal->j_tail);
1747 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1748 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001749 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750}
1751
1752/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001753 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754 * @journal: Journal to act on.
1755 * @write: flag (see below)
1756 *
1757 * Wipe out all of the contents of a journal, safely. This will produce
1758 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001759 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760 *
1761 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1762 * we merely suppress recovery.
1763 */
1764
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001765int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766{
1767 journal_superblock_t *sb;
1768 int err = 0;
1769
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001770 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001771
1772 err = load_superblock(journal);
1773 if (err)
1774 return err;
1775
1776 sb = journal->j_superblock;
1777
1778 if (!journal->j_tail)
1779 goto no_recovery;
1780
1781 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1782 write ? "Clearing" : "Ignoring");
1783
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001784 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001785 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001786 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787
1788 no_recovery:
1789 return err;
1790}
1791
1792/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 * Journal abort has very specific semantics, which we describe
1794 * for journal abort.
1795 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001796 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001797 * itself are here.
1798 */
1799
1800/*
1801 * Quick version for internal journal use (doesn't lock the journal).
1802 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1803 * and don't attempt to make any other journal updates.
1804 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001805void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806{
1807 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001809 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001810 return;
1811
1812 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001813 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814
1815 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001816 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001817 transaction = journal->j_running_transaction;
1818 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001819 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820 spin_unlock(&journal->j_state_lock);
1821}
1822
1823/* Soft abort: record the abort error status in the journal superblock,
1824 * but don't do any other IO. */
1825static void __journal_abort_soft (journal_t *journal, int errno)
1826{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001827 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828 return;
1829
1830 if (!journal->j_errno)
1831 journal->j_errno = errno;
1832
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001833 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001834
1835 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001836 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001837}
1838
1839/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001840 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841 * @journal: the journal to shutdown.
1842 * @errno: an error number to record in the journal indicating
1843 * the reason for the shutdown.
1844 *
1845 * Perform a complete, immediate shutdown of the ENTIRE
1846 * journal (not of a single transaction). This operation cannot be
1847 * undone without closing and reopening the journal.
1848 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001849 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001850 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1851 * mode.
1852 *
1853 * Journal abort has very specific semantics. Any existing dirty,
1854 * unjournaled buffers in the main filesystem will still be written to
1855 * disk by bdflush, but the journaling mechanism will be suspended
1856 * immediately and no further transaction commits will be honoured.
1857 *
1858 * Any dirty, journaled buffers will be written back to disk without
1859 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1860 * filesystem, but we _do_ attempt to leave as much data as possible
1861 * behind for fsck to use for cleanup.
1862 *
1863 * Any attempt to get a new transaction handle on a journal which is in
1864 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001865 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866 * entered abort state during the update.
1867 *
1868 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001869 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001870 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001871 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001872 * which will be recorded (if possible) in the journal superblock. This
1873 * allows a client to record failure conditions in the middle of a
1874 * transaction without having to complete the transaction to record the
1875 * failure to disk. ext3_error, for example, now uses this
1876 * functionality.
1877 *
1878 * Errors which originate from within the journaling layer will NOT
1879 * supply an errno; a null errno implies that absolutely no further
1880 * writes are done to the journal (unless there are any already in
1881 * progress).
1882 *
1883 */
1884
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001885void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001886{
1887 __journal_abort_soft(journal, errno);
1888}
1889
1890/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001891 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001892 * @journal: journal to examine.
1893 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001894 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001895 * time the journal was mounted - if the journal was stopped
1896 * without calling abort this will be 0.
1897 *
1898 * If the journal has been aborted on this mount time -EROFS will
1899 * be returned.
1900 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001901int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001902{
1903 int err;
1904
1905 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001906 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907 err = -EROFS;
1908 else
1909 err = journal->j_errno;
1910 spin_unlock(&journal->j_state_lock);
1911 return err;
1912}
1913
1914/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001915 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001916 * @journal: journal to act on.
1917 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001918 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 * mode.
1920 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001921int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001922{
1923 int err = 0;
1924
1925 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001926 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001927 err = -EROFS;
1928 else
1929 journal->j_errno = 0;
1930 spin_unlock(&journal->j_state_lock);
1931 return err;
1932}
1933
1934/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001935 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936 * @journal: journal to act on.
1937 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001938 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001939 * mode.
1940 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001941void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001942{
1943 spin_lock(&journal->j_state_lock);
1944 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001945 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001946 spin_unlock(&journal->j_state_lock);
1947}
1948
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001949int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001950{
1951 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1952}
1953
1954/*
Zach Brownb517bea2006-10-11 01:21:08 -07001955 * helper functions to deal with 32 or 64bit block numbers.
1956 */
1957size_t journal_tag_bytes(journal_t *journal)
1958{
1959 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001960 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001961 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001962 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001963}
1964
1965/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001966 * Journal_head storage management
1967 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001968static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001969#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001970static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1971#endif
1972
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001973static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001974{
1975 int retval;
1976
Al Viro1076d172008-03-29 03:07:18 +00001977 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001978 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001979 sizeof(struct journal_head),
1980 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001981 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001982 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001983 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001984 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001985 retval = -ENOMEM;
1986 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1987 }
1988 return retval;
1989}
1990
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001991static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001992{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001993 if (jbd2_journal_head_cache) {
1994 kmem_cache_destroy(jbd2_journal_head_cache);
1995 jbd2_journal_head_cache = NULL;
1996 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997}
1998
1999/*
2000 * journal_head splicing and dicing
2001 */
2002static struct journal_head *journal_alloc_journal_head(void)
2003{
2004 struct journal_head *ret;
2005 static unsigned long last_warning;
2006
Jose R. Santose23291b2007-07-18 08:57:06 -04002007#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002008 atomic_inc(&nr_journal_heads);
2009#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002010 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002011 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 jbd_debug(1, "out of memory for journal_head\n");
2013 if (time_after(jiffies, last_warning + 5*HZ)) {
2014 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002015 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 last_warning = jiffies;
2017 }
Al Viro1076d172008-03-29 03:07:18 +00002018 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002019 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002020 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021 }
2022 }
2023 return ret;
2024}
2025
2026static void journal_free_journal_head(struct journal_head *jh)
2027{
Jose R. Santose23291b2007-07-18 08:57:06 -04002028#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002029 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002030 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002031#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002032 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033}
2034
2035/*
2036 * A journal_head is attached to a buffer_head whenever JBD has an
2037 * interest in the buffer.
2038 *
2039 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2040 * is set. This bit is tested in core kernel code where we need to take
2041 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2042 * there.
2043 *
2044 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2045 *
2046 * When a buffer has its BH_JBD bit set it is immune from being released by
2047 * core kernel code, mainly via ->b_count.
2048 *
2049 * A journal_head may be detached from its buffer_head when the journal_head's
2050 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 * journal_head can be dropped if needed.
2053 *
2054 * Various places in the kernel want to attach a journal_head to a buffer_head
2055 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002056 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002057 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002058 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002059 *
2060 * So the typical usage would be:
2061 *
2062 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002063 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064 * ...
2065 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002066 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002067 *
2068 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2069 * because it has a non-zero b_transaction.
2070 */
2071
2072/*
2073 * Give a buffer_head a journal_head.
2074 *
2075 * Doesn't need the journal lock.
2076 * May sleep.
2077 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002078struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002079{
2080 struct journal_head *jh;
2081 struct journal_head *new_jh = NULL;
2082
2083repeat:
2084 if (!buffer_jbd(bh)) {
2085 new_jh = journal_alloc_journal_head();
2086 memset(new_jh, 0, sizeof(*new_jh));
2087 }
2088
2089 jbd_lock_bh_journal_head(bh);
2090 if (buffer_jbd(bh)) {
2091 jh = bh2jh(bh);
2092 } else {
2093 J_ASSERT_BH(bh,
2094 (atomic_read(&bh->b_count) > 0) ||
2095 (bh->b_page && bh->b_page->mapping));
2096
2097 if (!new_jh) {
2098 jbd_unlock_bh_journal_head(bh);
2099 goto repeat;
2100 }
2101
2102 jh = new_jh;
2103 new_jh = NULL; /* We consumed it */
2104 set_buffer_jbd(bh);
2105 bh->b_private = jh;
2106 jh->b_bh = bh;
2107 get_bh(bh);
2108 BUFFER_TRACE(bh, "added journal_head");
2109 }
2110 jh->b_jcount++;
2111 jbd_unlock_bh_journal_head(bh);
2112 if (new_jh)
2113 journal_free_journal_head(new_jh);
2114 return bh->b_private;
2115}
2116
2117/*
2118 * Grab a ref against this buffer_head's journal_head. If it ended up not
2119 * having a journal_head, return NULL
2120 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002121struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002122{
2123 struct journal_head *jh = NULL;
2124
2125 jbd_lock_bh_journal_head(bh);
2126 if (buffer_jbd(bh)) {
2127 jh = bh2jh(bh);
2128 jh->b_jcount++;
2129 }
2130 jbd_unlock_bh_journal_head(bh);
2131 return jh;
2132}
2133
2134static void __journal_remove_journal_head(struct buffer_head *bh)
2135{
2136 struct journal_head *jh = bh2jh(bh);
2137
2138 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2139
2140 get_bh(bh);
2141 if (jh->b_jcount == 0) {
2142 if (jh->b_transaction == NULL &&
2143 jh->b_next_transaction == NULL &&
2144 jh->b_cp_transaction == NULL) {
2145 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2146 J_ASSERT_BH(bh, buffer_jbd(bh));
2147 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2148 BUFFER_TRACE(bh, "remove journal_head");
2149 if (jh->b_frozen_data) {
2150 printk(KERN_WARNING "%s: freeing "
2151 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002152 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002153 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002154 }
2155 if (jh->b_committed_data) {
2156 printk(KERN_WARNING "%s: freeing "
2157 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002158 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002159 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002160 }
2161 bh->b_private = NULL;
2162 jh->b_bh = NULL; /* debug, really */
2163 clear_buffer_jbd(bh);
2164 __brelse(bh);
2165 journal_free_journal_head(jh);
2166 } else {
2167 BUFFER_TRACE(bh, "journal_head was locked");
2168 }
2169 }
2170}
2171
2172/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002173 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002174 * and has a zero b_jcount then remove and release its journal_head. If we did
2175 * see that the buffer is not used by any transaction we also "logically"
2176 * decrement ->b_count.
2177 *
2178 * We in fact take an additional increment on ->b_count as a convenience,
2179 * because the caller usually wants to do additional things with the bh
2180 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002181 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002182 * time. Once the caller has run __brelse(), the buffer is eligible for
2183 * reaping by try_to_free_buffers().
2184 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002185void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002186{
2187 jbd_lock_bh_journal_head(bh);
2188 __journal_remove_journal_head(bh);
2189 jbd_unlock_bh_journal_head(bh);
2190}
2191
2192/*
2193 * Drop a reference on the passed journal_head. If it fell to zero then try to
2194 * release the journal_head from the buffer_head.
2195 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002196void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002197{
2198 struct buffer_head *bh = jh2bh(jh);
2199
2200 jbd_lock_bh_journal_head(bh);
2201 J_ASSERT_JH(jh, jh->b_jcount > 0);
2202 --jh->b_jcount;
2203 if (!jh->b_jcount && !jh->b_transaction) {
2204 __journal_remove_journal_head(bh);
2205 __brelse(bh);
2206 }
2207 jbd_unlock_bh_journal_head(bh);
2208}
2209
2210/*
Jan Karac851ed52008-07-11 19:27:31 -04002211 * Initialize jbd inode head
2212 */
2213void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2214{
2215 jinode->i_transaction = NULL;
2216 jinode->i_next_transaction = NULL;
2217 jinode->i_vfs_inode = inode;
2218 jinode->i_flags = 0;
2219 INIT_LIST_HEAD(&jinode->i_list);
2220}
2221
2222/*
2223 * Function to be called before we start removing inode from memory (i.e.,
2224 * clear_inode() is a fine place to be called from). It removes inode from
2225 * transaction's lists.
2226 */
2227void jbd2_journal_release_jbd_inode(journal_t *journal,
2228 struct jbd2_inode *jinode)
2229{
2230 int writeout = 0;
2231
2232 if (!journal)
2233 return;
2234restart:
2235 spin_lock(&journal->j_list_lock);
2236 /* Is commit writing out inode - we have to wait */
2237 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2238 wait_queue_head_t *wq;
2239 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2240 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2241 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2242 spin_unlock(&journal->j_list_lock);
2243 schedule();
2244 finish_wait(wq, &wait.wait);
2245 goto restart;
2246 }
2247
2248 /* Do we need to wait for data writeback? */
2249 if (journal->j_committing_transaction == jinode->i_transaction)
2250 writeout = 1;
2251 if (jinode->i_transaction) {
2252 list_del(&jinode->i_list);
2253 jinode->i_transaction = NULL;
2254 }
2255 spin_unlock(&journal->j_list_lock);
2256}
2257
2258/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002259 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002260 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002261#ifdef CONFIG_JBD2_DEBUG
2262u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002263EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002264
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002265#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002266
Jose R. Santos6f38c742007-10-16 18:38:25 -04002267static struct dentry *jbd2_debugfs_dir;
2268static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002269
2270static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002271{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002272 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2273 if (jbd2_debugfs_dir)
2274 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2275 jbd2_debugfs_dir,
2276 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002277}
2278
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002279static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002280{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002281 debugfs_remove(jbd2_debug);
2282 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002283}
2284
2285#else
2286
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002287static void __init jbd2_create_debugfs_entry(void)
2288{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002289}
2290
2291static void __exit jbd2_remove_debugfs_entry(void)
2292{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002293}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002294
2295#endif
2296
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002297#ifdef CONFIG_PROC_FS
2298
2299#define JBD2_STATS_PROC_NAME "fs/jbd2"
2300
2301static void __init jbd2_create_jbd_stats_proc_entry(void)
2302{
2303 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2304}
2305
2306static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2307{
2308 if (proc_jbd2_stats)
2309 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2310}
2311
2312#else
2313
2314#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2315#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2316
2317#endif
2318
Christoph Lametere18b8902006-12-06 20:33:20 -08002319struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002320
2321static int __init journal_init_handle_cache(void)
2322{
Johann Lombardia920e942006-10-11 01:21:00 -07002323 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002324 sizeof(handle_t),
2325 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002326 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002327 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002328 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002329 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2330 return -ENOMEM;
2331 }
2332 return 0;
2333}
2334
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002335static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002336{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002337 if (jbd2_handle_cache)
2338 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002339}
2340
2341/*
2342 * Module startup and shutdown
2343 */
2344
2345static int __init journal_init_caches(void)
2346{
2347 int ret;
2348
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002349 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002350 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002351 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002352 if (ret == 0)
2353 ret = journal_init_handle_cache();
2354 return ret;
2355}
2356
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002357static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002358{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002359 jbd2_journal_destroy_revoke_caches();
2360 jbd2_journal_destroy_jbd2_journal_head_cache();
2361 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362}
2363
2364static int __init journal_init(void)
2365{
2366 int ret;
2367
2368 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2369
2370 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002371 if (ret == 0) {
2372 jbd2_create_debugfs_entry();
2373 jbd2_create_jbd_stats_proc_entry();
2374 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002375 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002376 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002377 return ret;
2378}
2379
2380static void __exit journal_exit(void)
2381{
Jose R. Santose23291b2007-07-18 08:57:06 -04002382#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002383 int n = atomic_read(&nr_journal_heads);
2384 if (n)
2385 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2386#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002387 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002388 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002389 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002390}
2391
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002392/*
2393 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2394 * tracing infrastructure to map a dev_t to a device name.
2395 *
2396 * The caller should use rcu_read_lock() in order to make sure the
2397 * device name stays valid until its done with it. We use
2398 * rcu_read_lock() as well to make sure we're safe in case the caller
2399 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2400 * nested.
2401 */
2402struct devname_cache {
2403 struct rcu_head rcu;
2404 dev_t device;
2405 char devname[BDEVNAME_SIZE];
2406};
2407#define CACHE_SIZE_BITS 6
2408static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2409static DEFINE_SPINLOCK(devname_cache_lock);
2410
2411static void free_devcache(struct rcu_head *rcu)
2412{
2413 kfree(rcu);
2414}
2415
2416const char *jbd2_dev_to_name(dev_t device)
2417{
2418 int i = hash_32(device, CACHE_SIZE_BITS);
2419 char *ret;
2420 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002421 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002422
2423 rcu_read_lock();
2424 if (devcache[i] && devcache[i]->device == device) {
2425 ret = devcache[i]->devname;
2426 rcu_read_unlock();
2427 return ret;
2428 }
2429 rcu_read_unlock();
2430
Theodore Ts'ob5744802009-06-20 23:34:44 -04002431 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2432 if (!new_dev)
2433 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002434 spin_lock(&devname_cache_lock);
2435 if (devcache[i]) {
2436 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002437 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002438 ret = devcache[i]->devname;
2439 spin_unlock(&devname_cache_lock);
2440 return ret;
2441 }
2442 call_rcu(&devcache[i]->rcu, free_devcache);
2443 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002444 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002445 devcache[i]->device = device;
2446 bd = bdget(device);
2447 if (bd) {
2448 bdevname(bd, devcache[i]->devname);
2449 bdput(bd);
2450 } else
2451 __bdevname(device, devcache[i]->devname);
2452 ret = devcache[i]->devname;
2453 spin_unlock(&devname_cache_lock);
2454 return ret;
2455}
2456EXPORT_SYMBOL(jbd2_dev_to_name);
2457
Dave Kleikamp470decc2006-10-11 01:20:57 -07002458MODULE_LICENSE("GPL");
2459module_init(journal_init);
2460module_exit(journal_exit);
2461