blob: 977a8dafb76d995f297b23918de8caf18fe59ac0 [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
Dave Kleikamp470decc2006-10-11 01:20:57 -0700139 /*
140 * And now, wait forever for commit wakeup events.
141 */
142 spin_lock(&journal->j_state_lock);
143
144loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700145 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146 goto end_loop;
147
148 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
149 journal->j_commit_sequence, journal->j_commit_request);
150
151 if (journal->j_commit_sequence != journal->j_commit_request) {
152 jbd_debug(1, "OK, requests differ\n");
153 spin_unlock(&journal->j_state_lock);
154 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700155 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700156 spin_lock(&journal->j_state_lock);
157 goto loop;
158 }
159
160 wake_up(&journal->j_wait_done_commit);
161 if (freezing(current)) {
162 /*
163 * The simpler the better. Flushing journal isn't a
164 * good idea, because that depends on threads that may
165 * be already stopped.
166 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700167 jbd_debug(1, "Now suspending kjournald2\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700168 spin_unlock(&journal->j_state_lock);
169 refrigerator();
170 spin_lock(&journal->j_state_lock);
171 } else {
172 /*
173 * We assume on resume that commits are already there,
174 * so we don't sleep
175 */
176 DEFINE_WAIT(wait);
177 int should_sleep = 1;
178
179 prepare_to_wait(&journal->j_wait_commit, &wait,
180 TASK_INTERRUPTIBLE);
181 if (journal->j_commit_sequence != journal->j_commit_request)
182 should_sleep = 0;
183 transaction = journal->j_running_transaction;
184 if (transaction && time_after_eq(jiffies,
185 transaction->t_expires))
186 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700187 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700188 should_sleep = 0;
189 if (should_sleep) {
190 spin_unlock(&journal->j_state_lock);
191 schedule();
192 spin_lock(&journal->j_state_lock);
193 }
194 finish_wait(&journal->j_wait_commit, &wait);
195 }
196
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700197 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198
199 /*
200 * Were we woken up by a commit wakeup event?
201 */
202 transaction = journal->j_running_transaction;
203 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
204 journal->j_commit_request = transaction->t_tid;
205 jbd_debug(1, "woke because of timeout\n");
206 }
207 goto loop;
208
209end_loop:
210 spin_unlock(&journal->j_state_lock);
211 del_timer_sync(&journal->j_commit_timer);
212 journal->j_task = NULL;
213 wake_up(&journal->j_wait_done_commit);
214 jbd_debug(1, "Journal thread exiting.\n");
215 return 0;
216}
217
Pavel Emelianov97f06782007-05-08 00:30:42 -0700218static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700219{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700220 struct task_struct *t;
221
Theodore Ts'o90576c02009-09-29 15:51:30 -0400222 t = kthread_run(kjournald2, journal, "jbd2/%s",
223 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700224 if (IS_ERR(t))
225 return PTR_ERR(t);
226
Al Viro1076d172008-03-29 03:07:18 +0000227 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700228 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700229}
230
231static void journal_kill_thread(journal_t *journal)
232{
233 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700234 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700235
236 while (journal->j_task) {
237 wake_up(&journal->j_wait_commit);
238 spin_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000239 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700240 spin_lock(&journal->j_state_lock);
241 }
242 spin_unlock(&journal->j_state_lock);
243}
244
245/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700246 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700247 *
248 * Writes a metadata buffer to a given disk block. The actual IO is not
249 * performed but a new buffer_head is constructed which labels the data
250 * to be written with the correct destination disk block.
251 *
252 * Any magic-number escaping which needs to be done will cause a
253 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700254 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700255 * magic number is only written to the log for descripter blocks. In
256 * this case, we copy the data and replace the first word with 0, and we
257 * return a result code which indicates that this buffer needs to be
258 * marked as an escaped buffer in the corresponding log descriptor
259 * block. The missing word can then be restored when the block is read
260 * during recovery.
261 *
262 * If the source buffer has already been modified by a new transaction
263 * since we took the last commit snapshot, we use the frozen copy of
264 * that data for IO. If we end up using the existing buffer_head's data
265 * for the write, then we *have* to lock the buffer to prevent anyone
266 * else from using and possibly modifying it while the IO is in
267 * progress.
268 *
269 * The function returns a pointer to the buffer_heads to be used for IO.
270 *
271 * We assume that the journal has already been locked in this function.
272 *
273 * Return value:
274 * <0: Error
275 * >=0: Finished OK
276 *
277 * On success:
278 * Bit 0 set == escape performed on the data
279 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
280 */
281
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700282int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700283 struct journal_head *jh_in,
284 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700285 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286{
287 int need_copy_out = 0;
288 int done_copy_out = 0;
289 int do_escape = 0;
290 char *mapped_data;
291 struct buffer_head *new_bh;
292 struct journal_head *new_jh;
293 struct page *new_page;
294 unsigned int new_offset;
295 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700296 struct jbd2_buffer_trigger_type *triggers;
dingdinghua96577c42009-07-13 17:55:35 -0400297 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700298
299 /*
300 * The buffer really shouldn't be locked: only the current committing
301 * transaction is allowed to write it, so nobody else is allowed
302 * to do any IO.
303 *
304 * akpm: except if we're journalling data, and write() output is
305 * also part of a shared mapping, and another thread has
306 * decided to launch a writepage() against this buffer.
307 */
308 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
309
310 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghua96577c42009-07-13 17:55:35 -0400311 /* keep subsequent assertions sane */
312 new_bh->b_state = 0;
313 init_buffer(new_bh, NULL, NULL);
314 atomic_set(&new_bh->b_count, 1);
315 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700316
317 /*
318 * If a new transaction has already done a buffer copy-out, then
319 * we use that version of the data for the commit.
320 */
321 jbd_lock_bh_state(bh_in);
322repeat:
323 if (jh_in->b_frozen_data) {
324 done_copy_out = 1;
325 new_page = virt_to_page(jh_in->b_frozen_data);
326 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700327 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700328 } else {
329 new_page = jh2bh(jh_in)->b_page;
330 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700331 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700332 }
333
334 mapped_data = kmap_atomic(new_page, KM_USER0);
335 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700336 * Fire any commit trigger. Do this before checking for escaping,
337 * as the trigger may modify the magic offset. If a copy-out
338 * happens afterwards, it will have the correct data in the buffer.
339 */
340 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
341 triggers);
342
343 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700344 * Check for escaping
345 */
346 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700347 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700348 need_copy_out = 1;
349 do_escape = 1;
350 }
351 kunmap_atomic(mapped_data, KM_USER0);
352
353 /*
354 * Do we need to do a data copy?
355 */
356 if (need_copy_out && !done_copy_out) {
357 char *tmp;
358
359 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400360 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700361 jbd_lock_bh_state(bh_in);
362 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400363 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700364 goto repeat;
365 }
366
367 jh_in->b_frozen_data = tmp;
368 mapped_data = kmap_atomic(new_page, KM_USER0);
369 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
370 kunmap_atomic(mapped_data, KM_USER0);
371
372 new_page = virt_to_page(tmp);
373 new_offset = offset_in_page(tmp);
374 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700375
376 /*
377 * This isn't strictly necessary, as we're using frozen
378 * data for the escaping, but it keeps consistency with
379 * b_frozen_data usage.
380 */
381 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700382 }
383
384 /*
385 * Did we need to do an escaping? Now we've done all the
386 * copying, we can finally do so.
387 */
388 if (do_escape) {
389 mapped_data = kmap_atomic(new_page, KM_USER0);
390 *((unsigned int *)(mapped_data + new_offset)) = 0;
391 kunmap_atomic(mapped_data, KM_USER0);
392 }
393
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394 set_bh_page(new_bh, new_page, new_offset);
395 new_jh->b_transaction = NULL;
396 new_bh->b_size = jh2bh(jh_in)->b_size;
397 new_bh->b_bdev = transaction->t_journal->j_dev;
398 new_bh->b_blocknr = blocknr;
399 set_buffer_mapped(new_bh);
400 set_buffer_dirty(new_bh);
401
402 *jh_out = new_jh;
403
404 /*
405 * The to-be-written buffer needs to get moved to the io queue,
406 * and the original buffer whose contents we are shadowing or
407 * copying is moved to the transaction's shadow queue.
408 */
409 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400410 spin_lock(&journal->j_list_lock);
411 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
412 spin_unlock(&journal->j_list_lock);
413 jbd_unlock_bh_state(bh_in);
414
Dave Kleikamp470decc2006-10-11 01:20:57 -0700415 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700416 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700417
418 return do_escape | (done_copy_out << 1);
419}
420
421/*
422 * Allocation code for the journal file. Manage the space left in the
423 * journal, so that we can begin checkpointing when appropriate.
424 */
425
426/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700427 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700428 *
429 * Called with the journal already locked.
430 *
431 * Called under j_state_lock
432 */
433
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700434int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700435{
436 int left = journal->j_free;
437
438 assert_spin_locked(&journal->j_state_lock);
439
440 /*
441 * Be pessimistic here about the number of those free blocks which
442 * might be required for log descriptor control blocks.
443 */
444
445#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
446
447 left -= MIN_LOG_RESERVED_BLOCKS;
448
449 if (left <= 0)
450 return 0;
451 left -= (left >> 3);
452 return left;
453}
454
455/*
Jan Karac88ccea2009-02-10 11:27:46 -0500456 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700458int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459{
460 /*
461 * Are we already doing a recent enough commit?
462 */
463 if (!tid_geq(journal->j_commit_request, target)) {
464 /*
465 * We want a new commit: OK, mark the request and wakup the
466 * commit thread. We do _not_ do the commit ourselves.
467 */
468
469 journal->j_commit_request = target;
470 jbd_debug(1, "JBD: requesting commit %d/%d\n",
471 journal->j_commit_request,
472 journal->j_commit_sequence);
473 wake_up(&journal->j_wait_commit);
474 return 1;
475 }
476 return 0;
477}
478
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700479int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700480{
481 int ret;
482
483 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700484 ret = __jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485 spin_unlock(&journal->j_state_lock);
486 return ret;
487}
488
489/*
490 * Force and wait upon a commit if the calling process is not within
491 * transaction. This is used for forcing out undo-protected data which contains
492 * bitmaps, when the fs is running out of space.
493 *
494 * We can only force the running transaction if we don't have an active handle;
495 * otherwise, we will deadlock.
496 *
497 * Returns true if a transaction was started.
498 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700499int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700500{
501 transaction_t *transaction = NULL;
502 tid_t tid;
503
504 spin_lock(&journal->j_state_lock);
505 if (journal->j_running_transaction && !current->journal_info) {
506 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700507 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700508 } else if (journal->j_committing_transaction)
509 transaction = journal->j_committing_transaction;
510
511 if (!transaction) {
512 spin_unlock(&journal->j_state_lock);
513 return 0; /* Nothing to retry */
514 }
515
516 tid = transaction->t_tid;
517 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700518 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700519 return 1;
520}
521
522/*
523 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500524 * if a transaction is going to be committed (or is currently already
525 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700527int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700528{
529 int ret = 0;
530
531 spin_lock(&journal->j_state_lock);
532 if (journal->j_running_transaction) {
533 tid_t tid = journal->j_running_transaction->t_tid;
534
Jan Karac88ccea2009-02-10 11:27:46 -0500535 __jbd2_log_start_commit(journal, tid);
536 /* There's a running transaction and we've just made sure
537 * it's commit has been scheduled. */
538 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700539 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500540 ret = 1;
541 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 /*
543 * If ext3_write_super() recently started a commit, then we
544 * have to wait for completion of that transaction
545 */
Jan Karac88ccea2009-02-10 11:27:46 -0500546 if (ptid)
547 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700548 ret = 1;
549 }
550 spin_unlock(&journal->j_state_lock);
551 return ret;
552}
553
554/*
555 * Wait for a specified commit to complete.
556 * The caller may not hold the journal lock.
557 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700558int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559{
560 int err = 0;
561
Jose R. Santose23291b2007-07-18 08:57:06 -0400562#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700563 spin_lock(&journal->j_state_lock);
564 if (!tid_geq(journal->j_commit_request, tid)) {
565 printk(KERN_EMERG
566 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400567 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568 }
569 spin_unlock(&journal->j_state_lock);
570#endif
571 spin_lock(&journal->j_state_lock);
572 while (tid_gt(tid, journal->j_commit_sequence)) {
573 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
574 tid, journal->j_commit_sequence);
575 wake_up(&journal->j_wait_commit);
576 spin_unlock(&journal->j_state_lock);
577 wait_event(journal->j_wait_done_commit,
578 !tid_gt(tid, journal->j_commit_sequence));
579 spin_lock(&journal->j_state_lock);
580 }
581 spin_unlock(&journal->j_state_lock);
582
583 if (unlikely(is_journal_aborted(journal))) {
584 printk(KERN_EMERG "journal commit I/O error\n");
585 err = -EIO;
586 }
587 return err;
588}
589
590/*
591 * Log buffer allocation routines:
592 */
593
Mingming Cao18eba7a2006-10-11 01:21:13 -0700594int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595{
596 unsigned long blocknr;
597
598 spin_lock(&journal->j_state_lock);
599 J_ASSERT(journal->j_free > 1);
600
601 blocknr = journal->j_head;
602 journal->j_head++;
603 journal->j_free--;
604 if (journal->j_head == journal->j_last)
605 journal->j_head = journal->j_first;
606 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700607 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700608}
609
610/*
611 * Conversion of logical to physical block numbers for the journal
612 *
613 * On external journals the journal blocks are identity-mapped, so
614 * this is a no-op. If needed, we can use j_blk_offset - everything is
615 * ready.
616 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700617int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700618 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619{
620 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700621 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622
623 if (journal->j_inode) {
624 ret = bmap(journal->j_inode, blocknr);
625 if (ret)
626 *retp = ret;
627 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 printk(KERN_ALERT "%s: journal block not found "
629 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400630 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700631 err = -EIO;
632 __journal_abort_soft(journal, err);
633 }
634 } else {
635 *retp = blocknr; /* +journal->j_blk_offset */
636 }
637 return err;
638}
639
640/*
641 * We play buffer_head aliasing tricks to write data/metadata blocks to
642 * the journal without copying their contents, but for journal
643 * descriptor blocks we do need to generate bona fide buffers.
644 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700645 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
647 * But we don't bother doing that, so there will be coherency problems with
648 * mmaps of blockdevs which hold live JBD-controlled filesystems.
649 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700650struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700651{
652 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700653 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654 int err;
655
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700656 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700657
658 if (err)
659 return NULL;
660
661 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500662 if (!bh)
663 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700664 lock_buffer(bh);
665 memset(bh->b_data, 0, journal->j_blocksize);
666 set_buffer_uptodate(bh);
667 unlock_buffer(bh);
668 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700669 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700670}
671
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500672struct jbd2_stats_proc_session {
673 journal_t *journal;
674 struct transaction_stats_s *stats;
675 int start;
676 int max;
677};
678
679static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
680 struct transaction_stats_s *ts,
681 int first)
682{
683 if (ts == s->stats + s->max)
684 ts = s->stats;
685 if (!first && ts == s->stats + s->start)
686 return NULL;
687 while (ts->ts_type == 0) {
688 ts++;
689 if (ts == s->stats + s->max)
690 ts = s->stats;
691 if (ts == s->stats + s->start)
692 return NULL;
693 }
694 return ts;
695
696}
697
698static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
699{
700 struct jbd2_stats_proc_session *s = seq->private;
701 struct transaction_stats_s *ts;
702 int l = *pos;
703
704 if (l == 0)
705 return SEQ_START_TOKEN;
706 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
707 if (!ts)
708 return NULL;
709 l--;
710 while (l) {
711 ts = jbd2_history_skip_empty(s, ++ts, 0);
712 if (!ts)
713 break;
714 l--;
715 }
716 return ts;
717}
718
719static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
720{
721 struct jbd2_stats_proc_session *s = seq->private;
722 struct transaction_stats_s *ts = v;
723
724 ++*pos;
725 if (v == SEQ_START_TOKEN)
726 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
727 else
728 return jbd2_history_skip_empty(s, ++ts, 0);
729}
730
731static int jbd2_seq_history_show(struct seq_file *seq, void *v)
732{
733 struct transaction_stats_s *ts = v;
734 if (v == SEQ_START_TOKEN) {
735 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
736 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
737 "wait", "run", "lock", "flush", "log", "hndls",
738 "block", "inlog", "ctime", "write", "drop",
739 "close");
740 return 0;
741 }
742 if (ts->ts_type == JBD2_STATS_RUN)
743 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
744 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
745 jiffies_to_msecs(ts->u.run.rs_wait),
746 jiffies_to_msecs(ts->u.run.rs_running),
747 jiffies_to_msecs(ts->u.run.rs_locked),
748 jiffies_to_msecs(ts->u.run.rs_flushing),
749 jiffies_to_msecs(ts->u.run.rs_logging),
750 ts->u.run.rs_handle_count,
751 ts->u.run.rs_blocks,
752 ts->u.run.rs_blocks_logged);
753 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
754 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
755 "C", ts->ts_tid, " ",
756 jiffies_to_msecs(ts->u.chp.cs_chp_time),
757 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
758 ts->u.chp.cs_forced_to_close);
759 else
760 J_ASSERT(0);
761 return 0;
762}
763
764static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
765{
766}
767
James Morris88e9d342009-09-22 16:43:43 -0700768static const struct seq_operations jbd2_seq_history_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500769 .start = jbd2_seq_history_start,
770 .next = jbd2_seq_history_next,
771 .stop = jbd2_seq_history_stop,
772 .show = jbd2_seq_history_show,
773};
774
775static int jbd2_seq_history_open(struct inode *inode, struct file *file)
776{
777 journal_t *journal = PDE(inode)->data;
778 struct jbd2_stats_proc_session *s;
779 int rc, size;
780
781 s = kmalloc(sizeof(*s), GFP_KERNEL);
782 if (s == NULL)
783 return -ENOMEM;
784 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
785 s->stats = kmalloc(size, GFP_KERNEL);
786 if (s->stats == NULL) {
787 kfree(s);
788 return -ENOMEM;
789 }
790 spin_lock(&journal->j_history_lock);
791 memcpy(s->stats, journal->j_history, size);
792 s->max = journal->j_history_max;
793 s->start = journal->j_history_cur % s->max;
794 spin_unlock(&journal->j_history_lock);
795
796 rc = seq_open(file, &jbd2_seq_history_ops);
797 if (rc == 0) {
798 struct seq_file *m = file->private_data;
799 m->private = s;
800 } else {
801 kfree(s->stats);
802 kfree(s);
803 }
804 return rc;
805
806}
807
808static int jbd2_seq_history_release(struct inode *inode, struct file *file)
809{
810 struct seq_file *seq = file->private_data;
811 struct jbd2_stats_proc_session *s = seq->private;
812
813 kfree(s->stats);
814 kfree(s);
815 return seq_release(inode, file);
816}
817
818static struct file_operations jbd2_seq_history_fops = {
819 .owner = THIS_MODULE,
820 .open = jbd2_seq_history_open,
821 .read = seq_read,
822 .llseek = seq_lseek,
823 .release = jbd2_seq_history_release,
824};
825
826static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
827{
828 return *pos ? NULL : SEQ_START_TOKEN;
829}
830
831static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
832{
833 return NULL;
834}
835
836static int jbd2_seq_info_show(struct seq_file *seq, void *v)
837{
838 struct jbd2_stats_proc_session *s = seq->private;
839
840 if (v != SEQ_START_TOKEN)
841 return 0;
842 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
843 s->stats->ts_tid,
844 s->journal->j_max_transaction_buffers);
845 if (s->stats->ts_tid == 0)
846 return 0;
847 seq_printf(seq, "average: \n %ums waiting for transaction\n",
848 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
849 seq_printf(seq, " %ums running transaction\n",
850 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
851 seq_printf(seq, " %ums transaction was being locked\n",
852 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
853 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
854 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
855 seq_printf(seq, " %ums logging transaction\n",
856 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500857 seq_printf(seq, " %lluus average transaction commit time\n",
858 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500859 seq_printf(seq, " %lu handles per transaction\n",
860 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
861 seq_printf(seq, " %lu blocks per transaction\n",
862 s->stats->u.run.rs_blocks / s->stats->ts_tid);
863 seq_printf(seq, " %lu logged blocks per transaction\n",
864 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
865 return 0;
866}
867
868static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
869{
870}
871
James Morris88e9d342009-09-22 16:43:43 -0700872static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500873 .start = jbd2_seq_info_start,
874 .next = jbd2_seq_info_next,
875 .stop = jbd2_seq_info_stop,
876 .show = jbd2_seq_info_show,
877};
878
879static int jbd2_seq_info_open(struct inode *inode, struct file *file)
880{
881 journal_t *journal = PDE(inode)->data;
882 struct jbd2_stats_proc_session *s;
883 int rc, size;
884
885 s = kmalloc(sizeof(*s), GFP_KERNEL);
886 if (s == NULL)
887 return -ENOMEM;
888 size = sizeof(struct transaction_stats_s);
889 s->stats = kmalloc(size, GFP_KERNEL);
890 if (s->stats == NULL) {
891 kfree(s);
892 return -ENOMEM;
893 }
894 spin_lock(&journal->j_history_lock);
895 memcpy(s->stats, &journal->j_stats, size);
896 s->journal = journal;
897 spin_unlock(&journal->j_history_lock);
898
899 rc = seq_open(file, &jbd2_seq_info_ops);
900 if (rc == 0) {
901 struct seq_file *m = file->private_data;
902 m->private = s;
903 } else {
904 kfree(s->stats);
905 kfree(s);
906 }
907 return rc;
908
909}
910
911static int jbd2_seq_info_release(struct inode *inode, struct file *file)
912{
913 struct seq_file *seq = file->private_data;
914 struct jbd2_stats_proc_session *s = seq->private;
915 kfree(s->stats);
916 kfree(s);
917 return seq_release(inode, file);
918}
919
920static struct file_operations jbd2_seq_info_fops = {
921 .owner = THIS_MODULE,
922 .open = jbd2_seq_info_open,
923 .read = seq_read,
924 .llseek = seq_lseek,
925 .release = jbd2_seq_info_release,
926};
927
928static struct proc_dir_entry *proc_jbd2_stats;
929
930static void jbd2_stats_proc_init(journal_t *journal)
931{
Theodore Ts'o05496762008-09-16 14:36:17 -0400932 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500933 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700934 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
935 &jbd2_seq_history_fops, journal);
936 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
937 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500938 }
939}
940
941static void jbd2_stats_proc_exit(journal_t *journal)
942{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500943 remove_proc_entry("info", journal->j_proc_entry);
944 remove_proc_entry("history", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400945 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500946}
947
948static void journal_init_stats(journal_t *journal)
949{
950 int size;
951
952 if (!proc_jbd2_stats)
953 return;
954
955 journal->j_history_max = 100;
956 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
957 journal->j_history = kzalloc(size, GFP_KERNEL);
958 if (!journal->j_history) {
959 journal->j_history_max = 0;
960 return;
961 }
962 spin_lock_init(&journal->j_history_lock);
963}
964
Dave Kleikamp470decc2006-10-11 01:20:57 -0700965/*
966 * Management for journal control blocks: functions to create and
967 * destroy journal_t structures, and to initialise and read existing
968 * journal blocks from disk. */
969
970/* First: create and setup a journal_t object in memory. We initialise
971 * very few fields yet: that has to wait until we have created the
972 * journal structures from from scratch, or loaded them from disk. */
973
974static journal_t * journal_init_common (void)
975{
976 journal_t *journal;
977 int err;
978
Mingming Caod802ffa2007-10-16 18:38:25 -0400979 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700980 if (!journal)
981 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700982
983 init_waitqueue_head(&journal->j_wait_transaction_locked);
984 init_waitqueue_head(&journal->j_wait_logspace);
985 init_waitqueue_head(&journal->j_wait_done_commit);
986 init_waitqueue_head(&journal->j_wait_checkpoint);
987 init_waitqueue_head(&journal->j_wait_commit);
988 init_waitqueue_head(&journal->j_wait_updates);
989 mutex_init(&journal->j_barrier);
990 mutex_init(&journal->j_checkpoint_mutex);
991 spin_lock_init(&journal->j_revoke_lock);
992 spin_lock_init(&journal->j_list_lock);
993 spin_lock_init(&journal->j_state_lock);
994
Mingming Caocd02ff02007-10-16 18:38:25 -0400995 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500996 journal->j_min_batch_time = 0;
997 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700998
999 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001000 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001001
1002 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001003 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001004 if (err) {
1005 kfree(journal);
1006 goto fail;
1007 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001008
1009 journal_init_stats(journal);
1010
Dave Kleikamp470decc2006-10-11 01:20:57 -07001011 return journal;
1012fail:
1013 return NULL;
1014}
1015
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001016/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001017 *
1018 * Create a journal structure assigned some fixed set of disk blocks to
1019 * the journal. We don't actually touch those disk blocks yet, but we
1020 * need to set up all of the mapping information to tell the journaling
1021 * system where the journal blocks are.
1022 *
1023 */
1024
1025/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001026 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001027 * @bdev: Block device on which to create the journal
1028 * @fs_dev: Device which hold journalled filesystem for this journal.
1029 * @start: Block nr Start of journal.
1030 * @len: Length of the journal in blocks.
1031 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001032 *
1033 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001035 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001036 * range of blocks on an arbitrary block device.
1037 *
1038 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001039journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001041 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001042{
1043 journal_t *journal = journal_init_common();
1044 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001045 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 int n;
1047
1048 if (!journal)
1049 return NULL;
1050
1051 /* journal descriptor can store up to n blocks -bzzz */
1052 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -05001053 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001054 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1055 journal->j_wbufsize = n;
1056 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1057 if (!journal->j_wbuf) {
1058 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001059 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001060 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001061 }
1062 journal->j_dev = bdev;
1063 journal->j_fs_dev = fs_dev;
1064 journal->j_blk_offset = start;
1065 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -04001066 bdevname(journal->j_dev, journal->j_devname);
1067 p = journal->j_devname;
1068 while ((p = strchr(p, '/')))
1069 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070
1071 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001072 if (!bh) {
1073 printk(KERN_ERR
1074 "%s: Cannot get buffer for journal superblock\n",
1075 __func__);
1076 goto out_err;
1077 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001078 journal->j_sb_buffer = bh;
1079 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001080
Dave Kleikamp470decc2006-10-11 01:20:57 -07001081 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001082out_err:
1083 jbd2_stats_proc_exit(journal);
1084 kfree(journal);
1085 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001086}
1087
1088/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001089 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 * @inode: An inode to create the journal in
1091 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001092 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001093 * the journal. The inode must exist already, must support bmap() and
1094 * must have all data blocks preallocated.
1095 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001096journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001097{
1098 struct buffer_head *bh;
1099 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001100 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101 int err;
1102 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001103 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104
1105 if (!journal)
1106 return NULL;
1107
1108 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1109 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001110 bdevname(journal->j_dev, journal->j_devname);
1111 p = journal->j_devname;
1112 while ((p = strchr(p, '/')))
1113 *p = '!';
1114 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001115 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001116 jbd_debug(1,
1117 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1118 journal, inode->i_sb->s_id, inode->i_ino,
1119 (long long) inode->i_size,
1120 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1121
1122 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1123 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001124 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001125
1126 /* journal descriptor can store up to n blocks -bzzz */
1127 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1128 journal->j_wbufsize = n;
1129 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1130 if (!journal->j_wbuf) {
1131 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001132 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001133 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001134 }
1135
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001136 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001137 /* If that failed, give up */
1138 if (err) {
1139 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001140 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001141 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001142 }
1143
1144 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001145 if (!bh) {
1146 printk(KERN_ERR
1147 "%s: Cannot get buffer for journal superblock\n",
1148 __func__);
1149 goto out_err;
1150 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001151 journal->j_sb_buffer = bh;
1152 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1153
1154 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001155out_err:
1156 jbd2_stats_proc_exit(journal);
1157 kfree(journal);
1158 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001159}
1160
1161/*
1162 * If the journal init or create aborts, we need to mark the journal
1163 * superblock as being NULL to prevent the journal destroy from writing
1164 * back a bogus superblock.
1165 */
1166static void journal_fail_superblock (journal_t *journal)
1167{
1168 struct buffer_head *bh = journal->j_sb_buffer;
1169 brelse(bh);
1170 journal->j_sb_buffer = NULL;
1171}
1172
1173/*
1174 * Given a journal_t structure, initialise the various fields for
1175 * startup of a new journaling session. We use this both when creating
1176 * a journal, and after recovering an old journal to reset it for
1177 * subsequent use.
1178 */
1179
1180static int journal_reset(journal_t *journal)
1181{
1182 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001183 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001184
1185 first = be32_to_cpu(sb->s_first);
1186 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001187 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1188 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1189 first, last);
1190 journal_fail_superblock(journal);
1191 return -EINVAL;
1192 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001193
1194 journal->j_first = first;
1195 journal->j_last = last;
1196
1197 journal->j_head = first;
1198 journal->j_tail = first;
1199 journal->j_free = last - first;
1200
1201 journal->j_tail_sequence = journal->j_transaction_sequence;
1202 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1203 journal->j_commit_request = journal->j_commit_sequence;
1204
1205 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1206
1207 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001208 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001209 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001210}
1211
1212/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001213 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001214 * @journal: The journal to update.
1215 * @wait: Set to '0' if you don't want to wait for IO completion.
1216 *
1217 * Update a journal's dynamic superblock fields and write it to disk,
1218 * optionally waiting for the IO to complete.
1219 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001220void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001221{
1222 journal_superblock_t *sb = journal->j_superblock;
1223 struct buffer_head *bh = journal->j_sb_buffer;
1224
1225 /*
1226 * As a special case, if the on-disk copy is already marked as needing
1227 * no recovery (s_start == 0) and there are no outstanding transactions
1228 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001229 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001230 * attempting a write to a potential-readonly device.
1231 */
1232 if (sb->s_start == 0 && journal->j_tail_sequence ==
1233 journal->j_transaction_sequence) {
1234 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1235 "(start %ld, seq %d, errno %d)\n",
1236 journal->j_tail, journal->j_tail_sequence,
1237 journal->j_errno);
1238 goto out;
1239 }
1240
Theodore Ts'o914258b2008-10-06 21:35:40 -04001241 if (buffer_write_io_error(bh)) {
1242 /*
1243 * Oh, dear. A previous attempt to write the journal
1244 * superblock failed. This could happen because the
1245 * USB device was yanked out. Or it could happen to
1246 * be a transient write error and maybe the block will
1247 * be remapped. Nothing we can do but to retry the
1248 * write and hope for the best.
1249 */
1250 printk(KERN_ERR "JBD2: previous I/O error detected "
1251 "for journal superblock update for %s.\n",
1252 journal->j_devname);
1253 clear_buffer_write_io_error(bh);
1254 set_buffer_uptodate(bh);
1255 }
1256
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 spin_lock(&journal->j_state_lock);
1258 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1259 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1260
1261 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1262 sb->s_start = cpu_to_be32(journal->j_tail);
1263 sb->s_errno = cpu_to_be32(journal->j_errno);
1264 spin_unlock(&journal->j_state_lock);
1265
1266 BUFFER_TRACE(bh, "marking dirty");
1267 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001268 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001269 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001270 if (buffer_write_io_error(bh)) {
1271 printk(KERN_ERR "JBD2: I/O error detected "
1272 "when updating journal superblock for %s.\n",
1273 journal->j_devname);
1274 clear_buffer_write_io_error(bh);
1275 set_buffer_uptodate(bh);
1276 }
1277 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001278 ll_rw_block(SWRITE, 1, &bh);
1279
1280out:
1281 /* If we have just flushed the log (by marking s_start==0), then
1282 * any future commit will have to be careful to update the
1283 * superblock again to re-record the true start of the log. */
1284
1285 spin_lock(&journal->j_state_lock);
1286 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001287 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001288 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001289 journal->j_flags |= JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001290 spin_unlock(&journal->j_state_lock);
1291}
1292
1293/*
1294 * Read the superblock for a given journal, performing initial
1295 * validation of the format.
1296 */
1297
1298static int journal_get_superblock(journal_t *journal)
1299{
1300 struct buffer_head *bh;
1301 journal_superblock_t *sb;
1302 int err = -EIO;
1303
1304 bh = journal->j_sb_buffer;
1305
1306 J_ASSERT(bh != NULL);
1307 if (!buffer_uptodate(bh)) {
1308 ll_rw_block(READ, 1, &bh);
1309 wait_on_buffer(bh);
1310 if (!buffer_uptodate(bh)) {
1311 printk (KERN_ERR
1312 "JBD: IO error reading journal superblock\n");
1313 goto out;
1314 }
1315 }
1316
1317 sb = journal->j_superblock;
1318
1319 err = -EINVAL;
1320
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001321 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001322 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1323 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1324 goto out;
1325 }
1326
1327 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001328 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001329 journal->j_format_version = 1;
1330 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001331 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001332 journal->j_format_version = 2;
1333 break;
1334 default:
1335 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1336 goto out;
1337 }
1338
1339 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1340 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1341 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1342 printk (KERN_WARNING "JBD: journal file too short\n");
1343 goto out;
1344 }
1345
1346 return 0;
1347
1348out:
1349 journal_fail_superblock(journal);
1350 return err;
1351}
1352
1353/*
1354 * Load the on-disk journal superblock and read the key fields into the
1355 * journal_t.
1356 */
1357
1358static int load_superblock(journal_t *journal)
1359{
1360 int err;
1361 journal_superblock_t *sb;
1362
1363 err = journal_get_superblock(journal);
1364 if (err)
1365 return err;
1366
1367 sb = journal->j_superblock;
1368
1369 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1370 journal->j_tail = be32_to_cpu(sb->s_start);
1371 journal->j_first = be32_to_cpu(sb->s_first);
1372 journal->j_last = be32_to_cpu(sb->s_maxlen);
1373 journal->j_errno = be32_to_cpu(sb->s_errno);
1374
1375 return 0;
1376}
1377
1378
1379/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001380 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001381 * @journal: Journal to act on.
1382 *
1383 * Given a journal_t structure which tells us which disk blocks contain
1384 * a journal, read the journal from disk to initialise the in-memory
1385 * structures.
1386 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001387int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001388{
1389 int err;
1390 journal_superblock_t *sb;
1391
1392 err = load_superblock(journal);
1393 if (err)
1394 return err;
1395
1396 sb = journal->j_superblock;
1397 /* If this is a V2 superblock, then we have to check the
1398 * features flags on it. */
1399
1400 if (journal->j_format_version >= 2) {
1401 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001402 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001404 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 printk (KERN_WARNING
1406 "JBD: Unrecognised features on journal\n");
1407 return -EINVAL;
1408 }
1409 }
1410
Dave Kleikamp470decc2006-10-11 01:20:57 -07001411 /* Let the recovery code check whether it needs to recover any
1412 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001413 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001414 goto recovery_error;
1415
1416 /* OK, we've finished with the dynamic journal bits:
1417 * reinitialise the dynamic contents of the superblock in memory
1418 * and reset them on disk. */
1419 if (journal_reset(journal))
1420 goto recovery_error;
1421
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001422 journal->j_flags &= ~JBD2_ABORT;
1423 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001424 return 0;
1425
1426recovery_error:
1427 printk (KERN_WARNING "JBD: recovery failed\n");
1428 return -EIO;
1429}
1430
1431/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001432 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001433 * @journal: Journal to act on.
1434 *
1435 * Release a journal_t structure once it is no longer in use by the
1436 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001437 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001439int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001441 int err = 0;
1442
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443 /* Wait for the commit thread to wake up and die. */
1444 journal_kill_thread(journal);
1445
1446 /* Force a final log commit */
1447 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001448 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001449
1450 /* Force any old transactions to disk */
1451
1452 /* Totally anal locking here... */
1453 spin_lock(&journal->j_list_lock);
1454 while (journal->j_checkpoint_transactions != NULL) {
1455 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001456 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001457 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001458 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001459 spin_lock(&journal->j_list_lock);
1460 }
1461
1462 J_ASSERT(journal->j_running_transaction == NULL);
1463 J_ASSERT(journal->j_committing_transaction == NULL);
1464 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1465 spin_unlock(&journal->j_list_lock);
1466
Dave Kleikamp470decc2006-10-11 01:20:57 -07001467 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001468 if (!is_journal_aborted(journal)) {
1469 /* We can now mark the journal as empty. */
1470 journal->j_tail = 0;
1471 journal->j_tail_sequence =
1472 ++journal->j_transaction_sequence;
1473 jbd2_journal_update_superblock(journal, 1);
1474 } else {
1475 err = -EIO;
1476 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 brelse(journal->j_sb_buffer);
1478 }
1479
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001480 if (journal->j_proc_entry)
1481 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001482 if (journal->j_inode)
1483 iput(journal->j_inode);
1484 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001485 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001486 kfree(journal->j_wbuf);
1487 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001488
1489 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001490}
1491
1492
1493/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001494 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001495 * @journal: Journal to check.
1496 * @compat: bitmask of compatible features
1497 * @ro: bitmask of features that force read-only mount
1498 * @incompat: bitmask of incompatible features
1499 *
1500 * Check whether the journal uses all of a given set of
1501 * features. Return true (non-zero) if it does.
1502 **/
1503
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001504int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001505 unsigned long ro, unsigned long incompat)
1506{
1507 journal_superblock_t *sb;
1508
1509 if (!compat && !ro && !incompat)
1510 return 1;
1511 if (journal->j_format_version == 1)
1512 return 0;
1513
1514 sb = journal->j_superblock;
1515
1516 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1517 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1518 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1519 return 1;
1520
1521 return 0;
1522}
1523
1524/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001525 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001526 * @journal: Journal to check.
1527 * @compat: bitmask of compatible features
1528 * @ro: bitmask of features that force read-only mount
1529 * @incompat: bitmask of incompatible features
1530 *
1531 * Check whether the journaling code supports the use of
1532 * all of a given set of features on this journal. Return true
1533 * (non-zero) if it can. */
1534
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001535int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001536 unsigned long ro, unsigned long incompat)
1537{
1538 journal_superblock_t *sb;
1539
1540 if (!compat && !ro && !incompat)
1541 return 1;
1542
1543 sb = journal->j_superblock;
1544
1545 /* We can support any known requested features iff the
1546 * superblock is in version 2. Otherwise we fail to support any
1547 * extended sb features. */
1548
1549 if (journal->j_format_version != 2)
1550 return 0;
1551
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001552 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1553 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1554 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001555 return 1;
1556
1557 return 0;
1558}
1559
1560/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001561 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001562 * @journal: Journal to act on.
1563 * @compat: bitmask of compatible features
1564 * @ro: bitmask of features that force read-only mount
1565 * @incompat: bitmask of incompatible features
1566 *
1567 * Mark a given journal feature as present on the
1568 * superblock. Returns true if the requested features could be set.
1569 *
1570 */
1571
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001572int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001573 unsigned long ro, unsigned long incompat)
1574{
1575 journal_superblock_t *sb;
1576
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001577 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001578 return 1;
1579
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001580 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001581 return 0;
1582
1583 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1584 compat, ro, incompat);
1585
1586 sb = journal->j_superblock;
1587
1588 sb->s_feature_compat |= cpu_to_be32(compat);
1589 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1590 sb->s_feature_incompat |= cpu_to_be32(incompat);
1591
1592 return 1;
1593}
1594
Girish Shilamkar818d2762008-01-28 23:58:27 -05001595/*
1596 * jbd2_journal_clear_features () - Clear a given journal feature in the
1597 * superblock
1598 * @journal: Journal to act on.
1599 * @compat: bitmask of compatible features
1600 * @ro: bitmask of features that force read-only mount
1601 * @incompat: bitmask of incompatible features
1602 *
1603 * Clear a given journal feature as present on the
1604 * superblock.
1605 */
1606void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1607 unsigned long ro, unsigned long incompat)
1608{
1609 journal_superblock_t *sb;
1610
1611 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1612 compat, ro, incompat);
1613
1614 sb = journal->j_superblock;
1615
1616 sb->s_feature_compat &= ~cpu_to_be32(compat);
1617 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1618 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1619}
1620EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001621
1622/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001623 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624 * @journal: Journal to act on.
1625 *
1626 * Given an initialised but unloaded journal struct, poke about in the
1627 * on-disk structure to update it to the most recent supported version.
1628 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001629int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630{
1631 journal_superblock_t *sb;
1632 int err;
1633
1634 err = journal_get_superblock(journal);
1635 if (err)
1636 return err;
1637
1638 sb = journal->j_superblock;
1639
1640 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001641 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001643 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644 return journal_convert_superblock_v1(journal, sb);
1645 default:
1646 break;
1647 }
1648 return -EINVAL;
1649}
1650
1651static int journal_convert_superblock_v1(journal_t *journal,
1652 journal_superblock_t *sb)
1653{
1654 int offset, blocksize;
1655 struct buffer_head *bh;
1656
1657 printk(KERN_WARNING
1658 "JBD: Converting superblock from version 1 to 2.\n");
1659
1660 /* Pre-initialise new fields to zero */
1661 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1662 blocksize = be32_to_cpu(sb->s_blocksize);
1663 memset(&sb->s_feature_compat, 0, blocksize-offset);
1664
1665 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001666 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001667 journal->j_format_version = 2;
1668
1669 bh = journal->j_sb_buffer;
1670 BUFFER_TRACE(bh, "marking dirty");
1671 mark_buffer_dirty(bh);
1672 sync_dirty_buffer(bh);
1673 return 0;
1674}
1675
1676
1677/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001678 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001679 * @journal: Journal to act on.
1680 *
1681 * Flush all data for a given journal to disk and empty the journal.
1682 * Filesystems can use this when remounting readonly to ensure that
1683 * recovery does not need to happen on remount.
1684 */
1685
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001686int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001687{
1688 int err = 0;
1689 transaction_t *transaction = NULL;
1690 unsigned long old_tail;
1691
1692 spin_lock(&journal->j_state_lock);
1693
1694 /* Force everything buffered to the log... */
1695 if (journal->j_running_transaction) {
1696 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001697 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001698 } else if (journal->j_committing_transaction)
1699 transaction = journal->j_committing_transaction;
1700
1701 /* Wait for the log commit to complete... */
1702 if (transaction) {
1703 tid_t tid = transaction->t_tid;
1704
1705 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001706 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001707 } else {
1708 spin_unlock(&journal->j_state_lock);
1709 }
1710
1711 /* ...and flush everything in the log out to disk. */
1712 spin_lock(&journal->j_list_lock);
1713 while (!err && journal->j_checkpoint_transactions != NULL) {
1714 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001715 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001716 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001717 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718 spin_lock(&journal->j_list_lock);
1719 }
1720 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001721
1722 if (is_journal_aborted(journal))
1723 return -EIO;
1724
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001725 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001726
1727 /* Finally, mark the journal as really needing no recovery.
1728 * This sets s_start==0 in the underlying superblock, which is
1729 * the magic code for a fully-recovered superblock. Any future
1730 * commits of data to the journal will restore the current
1731 * s_start value. */
1732 spin_lock(&journal->j_state_lock);
1733 old_tail = journal->j_tail;
1734 journal->j_tail = 0;
1735 spin_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001736 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001737 spin_lock(&journal->j_state_lock);
1738 journal->j_tail = old_tail;
1739
1740 J_ASSERT(!journal->j_running_transaction);
1741 J_ASSERT(!journal->j_committing_transaction);
1742 J_ASSERT(!journal->j_checkpoint_transactions);
1743 J_ASSERT(journal->j_head == journal->j_tail);
1744 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1745 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001746 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001747}
1748
1749/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001750 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001751 * @journal: Journal to act on.
1752 * @write: flag (see below)
1753 *
1754 * Wipe out all of the contents of a journal, safely. This will produce
1755 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001756 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001757 *
1758 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1759 * we merely suppress recovery.
1760 */
1761
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001762int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001763{
1764 journal_superblock_t *sb;
1765 int err = 0;
1766
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001767 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001768
1769 err = load_superblock(journal);
1770 if (err)
1771 return err;
1772
1773 sb = journal->j_superblock;
1774
1775 if (!journal->j_tail)
1776 goto no_recovery;
1777
1778 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1779 write ? "Clearing" : "Ignoring");
1780
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001781 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001782 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001783 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784
1785 no_recovery:
1786 return err;
1787}
1788
1789/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001790 * Journal abort has very specific semantics, which we describe
1791 * for journal abort.
1792 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001793 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001794 * itself are here.
1795 */
1796
1797/*
1798 * Quick version for internal journal use (doesn't lock the journal).
1799 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1800 * and don't attempt to make any other journal updates.
1801 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001802void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001803{
1804 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001806 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001807 return;
1808
1809 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001810 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811
1812 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001813 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814 transaction = journal->j_running_transaction;
1815 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001816 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001817 spin_unlock(&journal->j_state_lock);
1818}
1819
1820/* Soft abort: record the abort error status in the journal superblock,
1821 * but don't do any other IO. */
1822static void __journal_abort_soft (journal_t *journal, int errno)
1823{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001824 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001825 return;
1826
1827 if (!journal->j_errno)
1828 journal->j_errno = errno;
1829
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001830 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001831
1832 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001833 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001834}
1835
1836/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001837 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001838 * @journal: the journal to shutdown.
1839 * @errno: an error number to record in the journal indicating
1840 * the reason for the shutdown.
1841 *
1842 * Perform a complete, immediate shutdown of the ENTIRE
1843 * journal (not of a single transaction). This operation cannot be
1844 * undone without closing and reopening the journal.
1845 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001846 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001847 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1848 * mode.
1849 *
1850 * Journal abort has very specific semantics. Any existing dirty,
1851 * unjournaled buffers in the main filesystem will still be written to
1852 * disk by bdflush, but the journaling mechanism will be suspended
1853 * immediately and no further transaction commits will be honoured.
1854 *
1855 * Any dirty, journaled buffers will be written back to disk without
1856 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1857 * filesystem, but we _do_ attempt to leave as much data as possible
1858 * behind for fsck to use for cleanup.
1859 *
1860 * Any attempt to get a new transaction handle on a journal which is in
1861 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001862 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001863 * entered abort state during the update.
1864 *
1865 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001866 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001867 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001868 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001869 * which will be recorded (if possible) in the journal superblock. This
1870 * allows a client to record failure conditions in the middle of a
1871 * transaction without having to complete the transaction to record the
1872 * failure to disk. ext3_error, for example, now uses this
1873 * functionality.
1874 *
1875 * Errors which originate from within the journaling layer will NOT
1876 * supply an errno; a null errno implies that absolutely no further
1877 * writes are done to the journal (unless there are any already in
1878 * progress).
1879 *
1880 */
1881
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001882void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883{
1884 __journal_abort_soft(journal, errno);
1885}
1886
1887/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001888 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889 * @journal: journal to examine.
1890 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001891 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001892 * time the journal was mounted - if the journal was stopped
1893 * without calling abort this will be 0.
1894 *
1895 * If the journal has been aborted on this mount time -EROFS will
1896 * be returned.
1897 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001898int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899{
1900 int err;
1901
1902 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001903 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001904 err = -EROFS;
1905 else
1906 err = journal->j_errno;
1907 spin_unlock(&journal->j_state_lock);
1908 return err;
1909}
1910
1911/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001912 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 * @journal: journal to act on.
1914 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001915 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001916 * mode.
1917 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919{
1920 int err = 0;
1921
1922 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001923 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001924 err = -EROFS;
1925 else
1926 journal->j_errno = 0;
1927 spin_unlock(&journal->j_state_lock);
1928 return err;
1929}
1930
1931/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001932 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001933 * @journal: journal to act on.
1934 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001935 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936 * mode.
1937 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001938void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001939{
1940 spin_lock(&journal->j_state_lock);
1941 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001942 journal->j_flags |= JBD2_ACK_ERR;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001943 spin_unlock(&journal->j_state_lock);
1944}
1945
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001946int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001947{
1948 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1949}
1950
1951/*
Zach Brownb517bea2006-10-11 01:21:08 -07001952 * helper functions to deal with 32 or 64bit block numbers.
1953 */
1954size_t journal_tag_bytes(journal_t *journal)
1955{
1956 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001957 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001958 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001959 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001960}
1961
1962/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001963 * Journal_head storage management
1964 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001965static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001966#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1968#endif
1969
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001970static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971{
1972 int retval;
1973
Al Viro1076d172008-03-29 03:07:18 +00001974 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001975 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001976 sizeof(struct journal_head),
1977 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001978 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001979 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001980 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001981 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001982 retval = -ENOMEM;
1983 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1984 }
1985 return retval;
1986}
1987
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001988static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001989{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001990 if (jbd2_journal_head_cache) {
1991 kmem_cache_destroy(jbd2_journal_head_cache);
1992 jbd2_journal_head_cache = NULL;
1993 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001994}
1995
1996/*
1997 * journal_head splicing and dicing
1998 */
1999static struct journal_head *journal_alloc_journal_head(void)
2000{
2001 struct journal_head *ret;
2002 static unsigned long last_warning;
2003
Jose R. Santose23291b2007-07-18 08:57:06 -04002004#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002005 atomic_inc(&nr_journal_heads);
2006#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002007 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002008 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002009 jbd_debug(1, "out of memory for journal_head\n");
2010 if (time_after(jiffies, last_warning + 5*HZ)) {
2011 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002012 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 last_warning = jiffies;
2014 }
Al Viro1076d172008-03-29 03:07:18 +00002015 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002017 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002018 }
2019 }
2020 return ret;
2021}
2022
2023static void journal_free_journal_head(struct journal_head *jh)
2024{
Jose R. Santose23291b2007-07-18 08:57:06 -04002025#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002026 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002027 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002029 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002030}
2031
2032/*
2033 * A journal_head is attached to a buffer_head whenever JBD has an
2034 * interest in the buffer.
2035 *
2036 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2037 * is set. This bit is tested in core kernel code where we need to take
2038 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2039 * there.
2040 *
2041 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2042 *
2043 * When a buffer has its BH_JBD bit set it is immune from being released by
2044 * core kernel code, mainly via ->b_count.
2045 *
2046 * A journal_head may be detached from its buffer_head when the journal_head's
2047 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002048 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049 * journal_head can be dropped if needed.
2050 *
2051 * Various places in the kernel want to attach a journal_head to a buffer_head
2052 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002055 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002056 *
2057 * So the typical usage would be:
2058 *
2059 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002060 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002061 * ...
2062 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002063 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064 *
2065 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2066 * because it has a non-zero b_transaction.
2067 */
2068
2069/*
2070 * Give a buffer_head a journal_head.
2071 *
2072 * Doesn't need the journal lock.
2073 * May sleep.
2074 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002075struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002076{
2077 struct journal_head *jh;
2078 struct journal_head *new_jh = NULL;
2079
2080repeat:
2081 if (!buffer_jbd(bh)) {
2082 new_jh = journal_alloc_journal_head();
2083 memset(new_jh, 0, sizeof(*new_jh));
2084 }
2085
2086 jbd_lock_bh_journal_head(bh);
2087 if (buffer_jbd(bh)) {
2088 jh = bh2jh(bh);
2089 } else {
2090 J_ASSERT_BH(bh,
2091 (atomic_read(&bh->b_count) > 0) ||
2092 (bh->b_page && bh->b_page->mapping));
2093
2094 if (!new_jh) {
2095 jbd_unlock_bh_journal_head(bh);
2096 goto repeat;
2097 }
2098
2099 jh = new_jh;
2100 new_jh = NULL; /* We consumed it */
2101 set_buffer_jbd(bh);
2102 bh->b_private = jh;
2103 jh->b_bh = bh;
2104 get_bh(bh);
2105 BUFFER_TRACE(bh, "added journal_head");
2106 }
2107 jh->b_jcount++;
2108 jbd_unlock_bh_journal_head(bh);
2109 if (new_jh)
2110 journal_free_journal_head(new_jh);
2111 return bh->b_private;
2112}
2113
2114/*
2115 * Grab a ref against this buffer_head's journal_head. If it ended up not
2116 * having a journal_head, return NULL
2117 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002118struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002119{
2120 struct journal_head *jh = NULL;
2121
2122 jbd_lock_bh_journal_head(bh);
2123 if (buffer_jbd(bh)) {
2124 jh = bh2jh(bh);
2125 jh->b_jcount++;
2126 }
2127 jbd_unlock_bh_journal_head(bh);
2128 return jh;
2129}
2130
2131static void __journal_remove_journal_head(struct buffer_head *bh)
2132{
2133 struct journal_head *jh = bh2jh(bh);
2134
2135 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2136
2137 get_bh(bh);
2138 if (jh->b_jcount == 0) {
2139 if (jh->b_transaction == NULL &&
2140 jh->b_next_transaction == NULL &&
2141 jh->b_cp_transaction == NULL) {
2142 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2143 J_ASSERT_BH(bh, buffer_jbd(bh));
2144 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2145 BUFFER_TRACE(bh, "remove journal_head");
2146 if (jh->b_frozen_data) {
2147 printk(KERN_WARNING "%s: freeing "
2148 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002149 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002150 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002151 }
2152 if (jh->b_committed_data) {
2153 printk(KERN_WARNING "%s: freeing "
2154 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002155 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002156 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002157 }
2158 bh->b_private = NULL;
2159 jh->b_bh = NULL; /* debug, really */
2160 clear_buffer_jbd(bh);
2161 __brelse(bh);
2162 journal_free_journal_head(jh);
2163 } else {
2164 BUFFER_TRACE(bh, "journal_head was locked");
2165 }
2166 }
2167}
2168
2169/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002170 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002171 * and has a zero b_jcount then remove and release its journal_head. If we did
2172 * see that the buffer is not used by any transaction we also "logically"
2173 * decrement ->b_count.
2174 *
2175 * We in fact take an additional increment on ->b_count as a convenience,
2176 * because the caller usually wants to do additional things with the bh
2177 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002178 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002179 * time. Once the caller has run __brelse(), the buffer is eligible for
2180 * reaping by try_to_free_buffers().
2181 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002182void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002183{
2184 jbd_lock_bh_journal_head(bh);
2185 __journal_remove_journal_head(bh);
2186 jbd_unlock_bh_journal_head(bh);
2187}
2188
2189/*
2190 * Drop a reference on the passed journal_head. If it fell to zero then try to
2191 * release the journal_head from the buffer_head.
2192 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002193void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002194{
2195 struct buffer_head *bh = jh2bh(jh);
2196
2197 jbd_lock_bh_journal_head(bh);
2198 J_ASSERT_JH(jh, jh->b_jcount > 0);
2199 --jh->b_jcount;
2200 if (!jh->b_jcount && !jh->b_transaction) {
2201 __journal_remove_journal_head(bh);
2202 __brelse(bh);
2203 }
2204 jbd_unlock_bh_journal_head(bh);
2205}
2206
2207/*
Jan Karac851ed52008-07-11 19:27:31 -04002208 * Initialize jbd inode head
2209 */
2210void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2211{
2212 jinode->i_transaction = NULL;
2213 jinode->i_next_transaction = NULL;
2214 jinode->i_vfs_inode = inode;
2215 jinode->i_flags = 0;
2216 INIT_LIST_HEAD(&jinode->i_list);
2217}
2218
2219/*
2220 * Function to be called before we start removing inode from memory (i.e.,
2221 * clear_inode() is a fine place to be called from). It removes inode from
2222 * transaction's lists.
2223 */
2224void jbd2_journal_release_jbd_inode(journal_t *journal,
2225 struct jbd2_inode *jinode)
2226{
2227 int writeout = 0;
2228
2229 if (!journal)
2230 return;
2231restart:
2232 spin_lock(&journal->j_list_lock);
2233 /* Is commit writing out inode - we have to wait */
2234 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2235 wait_queue_head_t *wq;
2236 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2237 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2238 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2239 spin_unlock(&journal->j_list_lock);
2240 schedule();
2241 finish_wait(wq, &wait.wait);
2242 goto restart;
2243 }
2244
2245 /* Do we need to wait for data writeback? */
2246 if (journal->j_committing_transaction == jinode->i_transaction)
2247 writeout = 1;
2248 if (jinode->i_transaction) {
2249 list_del(&jinode->i_list);
2250 jinode->i_transaction = NULL;
2251 }
2252 spin_unlock(&journal->j_list_lock);
2253}
2254
2255/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002256 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002258#ifdef CONFIG_JBD2_DEBUG
2259u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002260EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002261
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002262#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002263
Jose R. Santos6f38c742007-10-16 18:38:25 -04002264static struct dentry *jbd2_debugfs_dir;
2265static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002266
2267static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002268{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002269 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2270 if (jbd2_debugfs_dir)
2271 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2272 jbd2_debugfs_dir,
2273 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002274}
2275
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002276static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002277{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002278 debugfs_remove(jbd2_debug);
2279 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002280}
2281
2282#else
2283
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002284static void __init jbd2_create_debugfs_entry(void)
2285{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002286}
2287
2288static void __exit jbd2_remove_debugfs_entry(void)
2289{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002290}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002291
2292#endif
2293
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002294#ifdef CONFIG_PROC_FS
2295
2296#define JBD2_STATS_PROC_NAME "fs/jbd2"
2297
2298static void __init jbd2_create_jbd_stats_proc_entry(void)
2299{
2300 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2301}
2302
2303static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2304{
2305 if (proc_jbd2_stats)
2306 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2307}
2308
2309#else
2310
2311#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2312#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2313
2314#endif
2315
Christoph Lametere18b8902006-12-06 20:33:20 -08002316struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002317
2318static int __init journal_init_handle_cache(void)
2319{
Johann Lombardia920e942006-10-11 01:21:00 -07002320 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002321 sizeof(handle_t),
2322 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002323 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002324 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002325 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002326 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2327 return -ENOMEM;
2328 }
2329 return 0;
2330}
2331
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002332static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002333{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002334 if (jbd2_handle_cache)
2335 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002336}
2337
2338/*
2339 * Module startup and shutdown
2340 */
2341
2342static int __init journal_init_caches(void)
2343{
2344 int ret;
2345
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002346 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002347 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002348 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002349 if (ret == 0)
2350 ret = journal_init_handle_cache();
2351 return ret;
2352}
2353
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002354static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002355{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002356 jbd2_journal_destroy_revoke_caches();
2357 jbd2_journal_destroy_jbd2_journal_head_cache();
2358 jbd2_journal_destroy_handle_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002359}
2360
2361static int __init journal_init(void)
2362{
2363 int ret;
2364
2365 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2366
2367 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002368 if (ret == 0) {
2369 jbd2_create_debugfs_entry();
2370 jbd2_create_jbd_stats_proc_entry();
2371 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002372 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002373 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002374 return ret;
2375}
2376
2377static void __exit journal_exit(void)
2378{
Jose R. Santose23291b2007-07-18 08:57:06 -04002379#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002380 int n = atomic_read(&nr_journal_heads);
2381 if (n)
2382 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2383#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002384 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002385 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002386 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002387}
2388
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002389/*
2390 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2391 * tracing infrastructure to map a dev_t to a device name.
2392 *
2393 * The caller should use rcu_read_lock() in order to make sure the
2394 * device name stays valid until its done with it. We use
2395 * rcu_read_lock() as well to make sure we're safe in case the caller
2396 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2397 * nested.
2398 */
2399struct devname_cache {
2400 struct rcu_head rcu;
2401 dev_t device;
2402 char devname[BDEVNAME_SIZE];
2403};
2404#define CACHE_SIZE_BITS 6
2405static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2406static DEFINE_SPINLOCK(devname_cache_lock);
2407
2408static void free_devcache(struct rcu_head *rcu)
2409{
2410 kfree(rcu);
2411}
2412
2413const char *jbd2_dev_to_name(dev_t device)
2414{
2415 int i = hash_32(device, CACHE_SIZE_BITS);
2416 char *ret;
2417 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002418 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002419
2420 rcu_read_lock();
2421 if (devcache[i] && devcache[i]->device == device) {
2422 ret = devcache[i]->devname;
2423 rcu_read_unlock();
2424 return ret;
2425 }
2426 rcu_read_unlock();
2427
Theodore Ts'ob5744802009-06-20 23:34:44 -04002428 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2429 if (!new_dev)
2430 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002431 spin_lock(&devname_cache_lock);
2432 if (devcache[i]) {
2433 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002434 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002435 ret = devcache[i]->devname;
2436 spin_unlock(&devname_cache_lock);
2437 return ret;
2438 }
2439 call_rcu(&devcache[i]->rcu, free_devcache);
2440 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002441 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002442 devcache[i]->device = device;
2443 bd = bdget(device);
2444 if (bd) {
2445 bdevname(bd, devcache[i]->devname);
2446 bdput(bd);
2447 } else
2448 __bdevname(device, devcache[i]->devname);
2449 ret = devcache[i]->devname;
2450 spin_unlock(&devname_cache_lock);
2451 return ret;
2452}
2453EXPORT_SYMBOL(jbd2_dev_to_name);
2454
Dave Kleikamp470decc2006-10-11 01:20:57 -07002455MODULE_LICENSE("GPL");
2456module_init(journal_init);
2457module_exit(journal_exit);
2458