blob: e7bf0fd9cec77d0e1f136be64caff82b87c3bf95 [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>
Theodore Ts'od2eecb02009-12-07 10:36:20 -050042#include <linux/log2.h>
43#include <linux/vmalloc.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040044#include <linux/backing-dev.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040045
46#define CREATE_TRACE_POINTS
47#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070048
49#include <asm/uaccess.h>
50#include <asm/page.h>
51
Mingming Caof7f4bcc2006-10-11 01:20:59 -070052EXPORT_SYMBOL(jbd2_journal_extend);
53EXPORT_SYMBOL(jbd2_journal_stop);
54EXPORT_SYMBOL(jbd2_journal_lock_updates);
55EXPORT_SYMBOL(jbd2_journal_unlock_updates);
56EXPORT_SYMBOL(jbd2_journal_get_write_access);
57EXPORT_SYMBOL(jbd2_journal_get_create_access);
58EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070059EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070060EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
61EXPORT_SYMBOL(jbd2_journal_release_buffer);
62EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070063#if 0
64EXPORT_SYMBOL(journal_sync_buffer);
65#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070066EXPORT_SYMBOL(jbd2_journal_flush);
67EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070068
Mingming Caof7f4bcc2006-10-11 01:20:59 -070069EXPORT_SYMBOL(jbd2_journal_init_dev);
70EXPORT_SYMBOL(jbd2_journal_init_inode);
71EXPORT_SYMBOL(jbd2_journal_update_format);
72EXPORT_SYMBOL(jbd2_journal_check_used_features);
73EXPORT_SYMBOL(jbd2_journal_check_available_features);
74EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070075EXPORT_SYMBOL(jbd2_journal_load);
76EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070077EXPORT_SYMBOL(jbd2_journal_abort);
78EXPORT_SYMBOL(jbd2_journal_errno);
79EXPORT_SYMBOL(jbd2_journal_ack_err);
80EXPORT_SYMBOL(jbd2_journal_clear_err);
81EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050082EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070083EXPORT_SYMBOL(jbd2_journal_start_commit);
84EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
85EXPORT_SYMBOL(jbd2_journal_wipe);
86EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
87EXPORT_SYMBOL(jbd2_journal_invalidatepage);
88EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
89EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040090EXPORT_SYMBOL(jbd2_journal_file_inode);
91EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
92EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
93EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070094
95static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
96static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -050097static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -070098
99/*
100 * Helper function used to manage commit timeouts
101 */
102
103static void commit_timeout(unsigned long __data)
104{
105 struct task_struct * p = (struct task_struct *) __data;
106
107 wake_up_process(p);
108}
109
110/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700111 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700112 * journal.
113 *
114 * This kernel thread is responsible for two things:
115 *
116 * 1) COMMIT: Every so often we need to commit the current state of the
117 * filesystem to disk. The journal thread is responsible for writing
118 * all of the metadata buffers to disk.
119 *
120 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
121 * of the data in that part of the log has been rewritten elsewhere on
122 * the disk. Flushing these old buffers to reclaim space in the log is
123 * known as checkpointing, and this thread is responsible for that job.
124 */
125
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700126static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700127{
128 journal_t *journal = arg;
129 transaction_t *transaction;
130
131 /*
132 * Set up an interval timer which can be used to trigger a commit wakeup
133 * after the commit interval expires
134 */
135 setup_timer(&journal->j_commit_timer, commit_timeout,
136 (unsigned long)current);
137
138 /* Record that the journal thread is running */
139 journal->j_task = current;
140 wake_up(&journal->j_wait_done_commit);
141
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142 /*
143 * And now, wait forever for commit wakeup events.
144 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400145 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146
147loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700148 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149 goto end_loop;
150
151 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
152 journal->j_commit_sequence, journal->j_commit_request);
153
154 if (journal->j_commit_sequence != journal->j_commit_request) {
155 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400156 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700157 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700158 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400159 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 goto loop;
161 }
162
163 wake_up(&journal->j_wait_done_commit);
164 if (freezing(current)) {
165 /*
166 * The simpler the better. Flushing journal isn't a
167 * good idea, because that depends on threads that may
168 * be already stopped.
169 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700170 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400171 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700172 refrigerator();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400173 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700174 } else {
175 /*
176 * We assume on resume that commits are already there,
177 * so we don't sleep
178 */
179 DEFINE_WAIT(wait);
180 int should_sleep = 1;
181
182 prepare_to_wait(&journal->j_wait_commit, &wait,
183 TASK_INTERRUPTIBLE);
184 if (journal->j_commit_sequence != journal->j_commit_request)
185 should_sleep = 0;
186 transaction = journal->j_running_transaction;
187 if (transaction && time_after_eq(jiffies,
188 transaction->t_expires))
189 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700190 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700191 should_sleep = 0;
192 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400193 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700194 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400195 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700196 }
197 finish_wait(&journal->j_wait_commit, &wait);
198 }
199
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700200 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700201
202 /*
203 * Were we woken up by a commit wakeup event?
204 */
205 transaction = journal->j_running_transaction;
206 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
207 journal->j_commit_request = transaction->t_tid;
208 jbd_debug(1, "woke because of timeout\n");
209 }
210 goto loop;
211
212end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400213 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700214 del_timer_sync(&journal->j_commit_timer);
215 journal->j_task = NULL;
216 wake_up(&journal->j_wait_done_commit);
217 jbd_debug(1, "Journal thread exiting.\n");
218 return 0;
219}
220
Pavel Emelianov97f06782007-05-08 00:30:42 -0700221static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700222{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700223 struct task_struct *t;
224
Theodore Ts'o90576c02009-09-29 15:51:30 -0400225 t = kthread_run(kjournald2, journal, "jbd2/%s",
226 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227 if (IS_ERR(t))
228 return PTR_ERR(t);
229
Al Viro1076d172008-03-29 03:07:18 +0000230 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700231 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232}
233
234static void journal_kill_thread(journal_t *journal)
235{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400236 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700237 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238
239 while (journal->j_task) {
240 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400241 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000242 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400243 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400245 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246}
247
248/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700249 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250 *
251 * Writes a metadata buffer to a given disk block. The actual IO is not
252 * performed but a new buffer_head is constructed which labels the data
253 * to be written with the correct destination disk block.
254 *
255 * Any magic-number escaping which needs to be done will cause a
256 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700257 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258 * magic number is only written to the log for descripter blocks. In
259 * this case, we copy the data and replace the first word with 0, and we
260 * return a result code which indicates that this buffer needs to be
261 * marked as an escaped buffer in the corresponding log descriptor
262 * block. The missing word can then be restored when the block is read
263 * during recovery.
264 *
265 * If the source buffer has already been modified by a new transaction
266 * since we took the last commit snapshot, we use the frozen copy of
267 * that data for IO. If we end up using the existing buffer_head's data
268 * for the write, then we *have* to lock the buffer to prevent anyone
269 * else from using and possibly modifying it while the IO is in
270 * progress.
271 *
272 * The function returns a pointer to the buffer_heads to be used for IO.
273 *
274 * We assume that the journal has already been locked in this function.
275 *
276 * Return value:
277 * <0: Error
278 * >=0: Finished OK
279 *
280 * On success:
281 * Bit 0 set == escape performed on the data
282 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
283 */
284
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700285int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286 struct journal_head *jh_in,
287 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700288 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700289{
290 int need_copy_out = 0;
291 int done_copy_out = 0;
292 int do_escape = 0;
293 char *mapped_data;
294 struct buffer_head *new_bh;
295 struct journal_head *new_jh;
296 struct page *new_page;
297 unsigned int new_offset;
298 struct buffer_head *bh_in = jh2bh(jh_in);
Joel Beckere06c8222008-09-11 15:35:47 -0700299 struct jbd2_buffer_trigger_type *triggers;
dingdinghua96577c42009-07-13 17:55:35 -0400300 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700301
302 /*
303 * The buffer really shouldn't be locked: only the current committing
304 * transaction is allowed to write it, so nobody else is allowed
305 * to do any IO.
306 *
307 * akpm: except if we're journalling data, and write() output is
308 * also part of a shared mapping, and another thread has
309 * decided to launch a writepage() against this buffer.
310 */
311 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
312
Theodore Ts'o47def822010-07-27 11:56:05 -0400313retry_alloc:
314 new_bh = alloc_buffer_head(GFP_NOFS);
315 if (!new_bh) {
316 /*
317 * Failure is not an option, but __GFP_NOFAIL is going
318 * away; so we retry ourselves here.
319 */
320 congestion_wait(BLK_RW_ASYNC, HZ/50);
321 goto retry_alloc;
322 }
323
dingdinghua96577c42009-07-13 17:55:35 -0400324 /* keep subsequent assertions sane */
325 new_bh->b_state = 0;
326 init_buffer(new_bh, NULL, NULL);
327 atomic_set(&new_bh->b_count, 1);
328 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700329
330 /*
331 * If a new transaction has already done a buffer copy-out, then
332 * we use that version of the data for the commit.
333 */
334 jbd_lock_bh_state(bh_in);
335repeat:
336 if (jh_in->b_frozen_data) {
337 done_copy_out = 1;
338 new_page = virt_to_page(jh_in->b_frozen_data);
339 new_offset = offset_in_page(jh_in->b_frozen_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700340 triggers = jh_in->b_frozen_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 } else {
342 new_page = jh2bh(jh_in)->b_page;
343 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
Joel Beckere06c8222008-09-11 15:35:47 -0700344 triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700345 }
346
347 mapped_data = kmap_atomic(new_page, KM_USER0);
348 /*
Joel Beckere06c8222008-09-11 15:35:47 -0700349 * Fire any commit trigger. Do this before checking for escaping,
350 * as the trigger may modify the magic offset. If a copy-out
351 * happens afterwards, it will have the correct data in the buffer.
352 */
353 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
354 triggers);
355
356 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700357 * Check for escaping
358 */
359 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700360 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700361 need_copy_out = 1;
362 do_escape = 1;
363 }
364 kunmap_atomic(mapped_data, KM_USER0);
365
366 /*
367 * Do we need to do a data copy?
368 */
369 if (need_copy_out && !done_copy_out) {
370 char *tmp;
371
372 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400373 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500374 if (!tmp) {
375 jbd2_journal_put_journal_head(new_jh);
376 return -ENOMEM;
377 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700378 jbd_lock_bh_state(bh_in);
379 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400380 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700381 goto repeat;
382 }
383
384 jh_in->b_frozen_data = tmp;
385 mapped_data = kmap_atomic(new_page, KM_USER0);
386 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
387 kunmap_atomic(mapped_data, KM_USER0);
388
389 new_page = virt_to_page(tmp);
390 new_offset = offset_in_page(tmp);
391 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700392
393 /*
394 * This isn't strictly necessary, as we're using frozen
395 * data for the escaping, but it keeps consistency with
396 * b_frozen_data usage.
397 */
398 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700399 }
400
401 /*
402 * Did we need to do an escaping? Now we've done all the
403 * copying, we can finally do so.
404 */
405 if (do_escape) {
406 mapped_data = kmap_atomic(new_page, KM_USER0);
407 *((unsigned int *)(mapped_data + new_offset)) = 0;
408 kunmap_atomic(mapped_data, KM_USER0);
409 }
410
Dave Kleikamp470decc2006-10-11 01:20:57 -0700411 set_bh_page(new_bh, new_page, new_offset);
412 new_jh->b_transaction = NULL;
413 new_bh->b_size = jh2bh(jh_in)->b_size;
414 new_bh->b_bdev = transaction->t_journal->j_dev;
415 new_bh->b_blocknr = blocknr;
416 set_buffer_mapped(new_bh);
417 set_buffer_dirty(new_bh);
418
419 *jh_out = new_jh;
420
421 /*
422 * The to-be-written buffer needs to get moved to the io queue,
423 * and the original buffer whose contents we are shadowing or
424 * copying is moved to the transaction's shadow queue.
425 */
426 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400427 spin_lock(&journal->j_list_lock);
428 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
429 spin_unlock(&journal->j_list_lock);
430 jbd_unlock_bh_state(bh_in);
431
Dave Kleikamp470decc2006-10-11 01:20:57 -0700432 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700433 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700434
435 return do_escape | (done_copy_out << 1);
436}
437
438/*
439 * Allocation code for the journal file. Manage the space left in the
440 * journal, so that we can begin checkpointing when appropriate.
441 */
442
443/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700444 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700445 *
446 * Called with the journal already locked.
447 *
448 * Called under j_state_lock
449 */
450
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700451int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700452{
453 int left = journal->j_free;
454
Theodore Ts'oa931da62010-08-03 21:35:12 -0400455 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456
457 /*
458 * Be pessimistic here about the number of those free blocks which
459 * might be required for log descriptor control blocks.
460 */
461
462#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
463
464 left -= MIN_LOG_RESERVED_BLOCKS;
465
466 if (left <= 0)
467 return 0;
468 left -= (left >> 3);
469 return left;
470}
471
472/*
Jan Karac88ccea2009-02-10 11:27:46 -0500473 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700474 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700475int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700476{
477 /*
478 * Are we already doing a recent enough commit?
479 */
480 if (!tid_geq(journal->j_commit_request, target)) {
481 /*
482 * We want a new commit: OK, mark the request and wakup the
483 * commit thread. We do _not_ do the commit ourselves.
484 */
485
486 journal->j_commit_request = target;
487 jbd_debug(1, "JBD: requesting commit %d/%d\n",
488 journal->j_commit_request,
489 journal->j_commit_sequence);
490 wake_up(&journal->j_wait_commit);
491 return 1;
492 }
493 return 0;
494}
495
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700496int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497{
498 int ret;
499
Theodore Ts'oa931da62010-08-03 21:35:12 -0400500 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700501 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400502 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700503 return ret;
504}
505
506/*
507 * Force and wait upon a commit if the calling process is not within
508 * transaction. This is used for forcing out undo-protected data which contains
509 * bitmaps, when the fs is running out of space.
510 *
511 * We can only force the running transaction if we don't have an active handle;
512 * otherwise, we will deadlock.
513 *
514 * Returns true if a transaction was started.
515 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700516int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700517{
518 transaction_t *transaction = NULL;
519 tid_t tid;
520
Theodore Ts'oa931da62010-08-03 21:35:12 -0400521 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700522 if (journal->j_running_transaction && !current->journal_info) {
523 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700524 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700525 } else if (journal->j_committing_transaction)
526 transaction = journal->j_committing_transaction;
527
528 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400529 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700530 return 0; /* Nothing to retry */
531 }
532
533 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400534 read_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700535 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 return 1;
537}
538
539/*
540 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500541 * if a transaction is going to be committed (or is currently already
542 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700543 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700544int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700545{
546 int ret = 0;
547
Theodore Ts'oa931da62010-08-03 21:35:12 -0400548 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700549 if (journal->j_running_transaction) {
550 tid_t tid = journal->j_running_transaction->t_tid;
551
Jan Karac88ccea2009-02-10 11:27:46 -0500552 __jbd2_log_start_commit(journal, tid);
553 /* There's a running transaction and we've just made sure
554 * it's commit has been scheduled. */
555 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700556 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500557 ret = 1;
558 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559 /*
560 * If ext3_write_super() recently started a commit, then we
561 * have to wait for completion of that transaction
562 */
Jan Karac88ccea2009-02-10 11:27:46 -0500563 if (ptid)
564 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700565 ret = 1;
566 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400567 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568 return ret;
569}
570
571/*
572 * Wait for a specified commit to complete.
573 * The caller may not hold the journal lock.
574 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700575int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700576{
577 int err = 0;
578
Theodore Ts'oa931da62010-08-03 21:35:12 -0400579 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400580#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700581 if (!tid_geq(journal->j_commit_request, tid)) {
582 printk(KERN_EMERG
583 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400584 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700585 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587 while (tid_gt(tid, journal->j_commit_sequence)) {
588 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
589 tid, journal->j_commit_sequence);
590 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400591 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592 wait_event(journal->j_wait_done_commit,
593 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400594 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400596 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700597
598 if (unlikely(is_journal_aborted(journal))) {
599 printk(KERN_EMERG "journal commit I/O error\n");
600 err = -EIO;
601 }
602 return err;
603}
604
605/*
606 * Log buffer allocation routines:
607 */
608
Mingming Cao18eba7a2006-10-11 01:21:13 -0700609int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700610{
611 unsigned long blocknr;
612
Theodore Ts'oa931da62010-08-03 21:35:12 -0400613 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 J_ASSERT(journal->j_free > 1);
615
616 blocknr = journal->j_head;
617 journal->j_head++;
618 journal->j_free--;
619 if (journal->j_head == journal->j_last)
620 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400621 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700622 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623}
624
625/*
626 * Conversion of logical to physical block numbers for the journal
627 *
628 * On external journals the journal blocks are identity-mapped, so
629 * this is a no-op. If needed, we can use j_blk_offset - everything is
630 * ready.
631 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700632int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700633 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700634{
635 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700636 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700637
638 if (journal->j_inode) {
639 ret = bmap(journal->j_inode, blocknr);
640 if (ret)
641 *retp = ret;
642 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700643 printk(KERN_ALERT "%s: journal block not found "
644 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400645 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646 err = -EIO;
647 __journal_abort_soft(journal, err);
648 }
649 } else {
650 *retp = blocknr; /* +journal->j_blk_offset */
651 }
652 return err;
653}
654
655/*
656 * We play buffer_head aliasing tricks to write data/metadata blocks to
657 * the journal without copying their contents, but for journal
658 * descriptor blocks we do need to generate bona fide buffers.
659 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700660 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700661 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
662 * But we don't bother doing that, so there will be coherency problems with
663 * mmaps of blockdevs which hold live JBD-controlled filesystems.
664 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700665struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700666{
667 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700668 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700669 int err;
670
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700671 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700672
673 if (err)
674 return NULL;
675
676 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500677 if (!bh)
678 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700679 lock_buffer(bh);
680 memset(bh->b_data, 0, journal->j_blocksize);
681 set_buffer_uptodate(bh);
682 unlock_buffer(bh);
683 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700684 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700685}
686
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500687struct jbd2_stats_proc_session {
688 journal_t *journal;
689 struct transaction_stats_s *stats;
690 int start;
691 int max;
692};
693
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500694static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
695{
696 return *pos ? NULL : SEQ_START_TOKEN;
697}
698
699static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
700{
701 return NULL;
702}
703
704static int jbd2_seq_info_show(struct seq_file *seq, void *v)
705{
706 struct jbd2_stats_proc_session *s = seq->private;
707
708 if (v != SEQ_START_TOKEN)
709 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400710 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500711 s->stats->ts_tid,
712 s->journal->j_max_transaction_buffers);
713 if (s->stats->ts_tid == 0)
714 return 0;
715 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400716 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500717 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400718 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500719 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400720 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500721 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400722 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500723 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400724 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500725 seq_printf(seq, " %lluus average transaction commit time\n",
726 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500727 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400728 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500729 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400730 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500731 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400732 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500733 return 0;
734}
735
736static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
737{
738}
739
James Morris88e9d342009-09-22 16:43:43 -0700740static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500741 .start = jbd2_seq_info_start,
742 .next = jbd2_seq_info_next,
743 .stop = jbd2_seq_info_stop,
744 .show = jbd2_seq_info_show,
745};
746
747static int jbd2_seq_info_open(struct inode *inode, struct file *file)
748{
749 journal_t *journal = PDE(inode)->data;
750 struct jbd2_stats_proc_session *s;
751 int rc, size;
752
753 s = kmalloc(sizeof(*s), GFP_KERNEL);
754 if (s == NULL)
755 return -ENOMEM;
756 size = sizeof(struct transaction_stats_s);
757 s->stats = kmalloc(size, GFP_KERNEL);
758 if (s->stats == NULL) {
759 kfree(s);
760 return -ENOMEM;
761 }
762 spin_lock(&journal->j_history_lock);
763 memcpy(s->stats, &journal->j_stats, size);
764 s->journal = journal;
765 spin_unlock(&journal->j_history_lock);
766
767 rc = seq_open(file, &jbd2_seq_info_ops);
768 if (rc == 0) {
769 struct seq_file *m = file->private_data;
770 m->private = s;
771 } else {
772 kfree(s->stats);
773 kfree(s);
774 }
775 return rc;
776
777}
778
779static int jbd2_seq_info_release(struct inode *inode, struct file *file)
780{
781 struct seq_file *seq = file->private_data;
782 struct jbd2_stats_proc_session *s = seq->private;
783 kfree(s->stats);
784 kfree(s);
785 return seq_release(inode, file);
786}
787
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700788static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500789 .owner = THIS_MODULE,
790 .open = jbd2_seq_info_open,
791 .read = seq_read,
792 .llseek = seq_lseek,
793 .release = jbd2_seq_info_release,
794};
795
796static struct proc_dir_entry *proc_jbd2_stats;
797
798static void jbd2_stats_proc_init(journal_t *journal)
799{
Theodore Ts'o05496762008-09-16 14:36:17 -0400800 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500801 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700802 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
803 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500804 }
805}
806
807static void jbd2_stats_proc_exit(journal_t *journal)
808{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500809 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400810 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500811}
812
Dave Kleikamp470decc2006-10-11 01:20:57 -0700813/*
814 * Management for journal control blocks: functions to create and
815 * destroy journal_t structures, and to initialise and read existing
816 * journal blocks from disk. */
817
818/* First: create and setup a journal_t object in memory. We initialise
819 * very few fields yet: that has to wait until we have created the
820 * journal structures from from scratch, or loaded them from disk. */
821
822static journal_t * journal_init_common (void)
823{
824 journal_t *journal;
825 int err;
826
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500827 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700828 if (!journal)
829 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700830
831 init_waitqueue_head(&journal->j_wait_transaction_locked);
832 init_waitqueue_head(&journal->j_wait_logspace);
833 init_waitqueue_head(&journal->j_wait_done_commit);
834 init_waitqueue_head(&journal->j_wait_checkpoint);
835 init_waitqueue_head(&journal->j_wait_commit);
836 init_waitqueue_head(&journal->j_wait_updates);
837 mutex_init(&journal->j_barrier);
838 mutex_init(&journal->j_checkpoint_mutex);
839 spin_lock_init(&journal->j_revoke_lock);
840 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400841 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700842
Mingming Caocd02ff02007-10-16 18:38:25 -0400843 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500844 journal->j_min_batch_time = 0;
845 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700846
847 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700848 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700849
850 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700851 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700852 if (err) {
853 kfree(journal);
854 goto fail;
855 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500856
Theodore Ts'obf699322009-09-30 00:32:06 -0400857 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500858
Dave Kleikamp470decc2006-10-11 01:20:57 -0700859 return journal;
860fail:
861 return NULL;
862}
863
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700864/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700865 *
866 * Create a journal structure assigned some fixed set of disk blocks to
867 * the journal. We don't actually touch those disk blocks yet, but we
868 * need to set up all of the mapping information to tell the journaling
869 * system where the journal blocks are.
870 *
871 */
872
873/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400874 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700875 * @bdev: Block device on which to create the journal
876 * @fs_dev: Device which hold journalled filesystem for this journal.
877 * @start: Block nr Start of journal.
878 * @len: Length of the journal in blocks.
879 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400880 *
881 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700882 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700883 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700884 * range of blocks on an arbitrary block device.
885 *
886 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700887journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700888 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700889 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700890{
891 journal_t *journal = journal_init_common();
892 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400893 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700894 int n;
895
896 if (!journal)
897 return NULL;
898
899 /* journal descriptor can store up to n blocks -bzzz */
900 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -0500901 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700902 n = journal->j_blocksize / sizeof(journal_block_tag_t);
903 journal->j_wbufsize = n;
904 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
905 if (!journal->j_wbuf) {
906 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400907 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500908 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700909 }
910 journal->j_dev = bdev;
911 journal->j_fs_dev = fs_dev;
912 journal->j_blk_offset = start;
913 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -0400914 bdevname(journal->j_dev, journal->j_devname);
915 p = journal->j_devname;
916 while ((p = strchr(p, '/')))
917 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -0700918
919 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500920 if (!bh) {
921 printk(KERN_ERR
922 "%s: Cannot get buffer for journal superblock\n",
923 __func__);
924 goto out_err;
925 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700926 journal->j_sb_buffer = bh;
927 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500928
Dave Kleikamp470decc2006-10-11 01:20:57 -0700929 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500930out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800931 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500932 jbd2_stats_proc_exit(journal);
933 kfree(journal);
934 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700935}
936
937/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700938 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700939 * @inode: An inode to create the journal in
940 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700941 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700942 * the journal. The inode must exist already, must support bmap() and
943 * must have all data blocks preallocated.
944 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700945journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700946{
947 struct buffer_head *bh;
948 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400949 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700950 int err;
951 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700952 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700953
954 if (!journal)
955 return NULL;
956
957 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
958 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400959 bdevname(journal->j_dev, journal->j_devname);
960 p = journal->j_devname;
961 while ((p = strchr(p, '/')))
962 *p = '!';
963 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400964 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700965 jbd_debug(1,
966 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
967 journal, inode->i_sb->s_id, inode->i_ino,
968 (long long) inode->i_size,
969 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
970
971 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
972 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500973 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700974
975 /* journal descriptor can store up to n blocks -bzzz */
976 n = journal->j_blocksize / sizeof(journal_block_tag_t);
977 journal->j_wbufsize = n;
978 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
979 if (!journal->j_wbuf) {
980 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400981 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500982 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700983 }
984
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700985 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700986 /* If that failed, give up */
987 if (err) {
988 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400989 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500990 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700991 }
992
993 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500994 if (!bh) {
995 printk(KERN_ERR
996 "%s: Cannot get buffer for journal superblock\n",
997 __func__);
998 goto out_err;
999 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001000 journal->j_sb_buffer = bh;
1001 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1002
1003 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001004out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001005 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001006 jbd2_stats_proc_exit(journal);
1007 kfree(journal);
1008 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001009}
1010
1011/*
1012 * If the journal init or create aborts, we need to mark the journal
1013 * superblock as being NULL to prevent the journal destroy from writing
1014 * back a bogus superblock.
1015 */
1016static void journal_fail_superblock (journal_t *journal)
1017{
1018 struct buffer_head *bh = journal->j_sb_buffer;
1019 brelse(bh);
1020 journal->j_sb_buffer = NULL;
1021}
1022
1023/*
1024 * Given a journal_t structure, initialise the various fields for
1025 * startup of a new journaling session. We use this both when creating
1026 * a journal, and after recovering an old journal to reset it for
1027 * subsequent use.
1028 */
1029
1030static int journal_reset(journal_t *journal)
1031{
1032 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001033 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034
1035 first = be32_to_cpu(sb->s_first);
1036 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001037 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1038 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1039 first, last);
1040 journal_fail_superblock(journal);
1041 return -EINVAL;
1042 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043
1044 journal->j_first = first;
1045 journal->j_last = last;
1046
1047 journal->j_head = first;
1048 journal->j_tail = first;
1049 journal->j_free = last - first;
1050
1051 journal->j_tail_sequence = journal->j_transaction_sequence;
1052 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1053 journal->j_commit_request = journal->j_commit_sequence;
1054
1055 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1056
1057 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001058 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001059 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001060}
1061
1062/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001063 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001064 * @journal: The journal to update.
1065 * @wait: Set to '0' if you don't want to wait for IO completion.
1066 *
1067 * Update a journal's dynamic superblock fields and write it to disk,
1068 * optionally waiting for the IO to complete.
1069 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001070void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001071{
1072 journal_superblock_t *sb = journal->j_superblock;
1073 struct buffer_head *bh = journal->j_sb_buffer;
1074
1075 /*
1076 * As a special case, if the on-disk copy is already marked as needing
1077 * no recovery (s_start == 0) and there are no outstanding transactions
1078 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001079 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001080 * attempting a write to a potential-readonly device.
1081 */
1082 if (sb->s_start == 0 && journal->j_tail_sequence ==
1083 journal->j_transaction_sequence) {
1084 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1085 "(start %ld, seq %d, errno %d)\n",
1086 journal->j_tail, journal->j_tail_sequence,
1087 journal->j_errno);
1088 goto out;
1089 }
1090
Theodore Ts'o914258b2008-10-06 21:35:40 -04001091 if (buffer_write_io_error(bh)) {
1092 /*
1093 * Oh, dear. A previous attempt to write the journal
1094 * superblock failed. This could happen because the
1095 * USB device was yanked out. Or it could happen to
1096 * be a transient write error and maybe the block will
1097 * be remapped. Nothing we can do but to retry the
1098 * write and hope for the best.
1099 */
1100 printk(KERN_ERR "JBD2: previous I/O error detected "
1101 "for journal superblock update for %s.\n",
1102 journal->j_devname);
1103 clear_buffer_write_io_error(bh);
1104 set_buffer_uptodate(bh);
1105 }
1106
Theodore Ts'oa931da62010-08-03 21:35:12 -04001107 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001108 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1109 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1110
1111 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1112 sb->s_start = cpu_to_be32(journal->j_tail);
1113 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001114 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115
1116 BUFFER_TRACE(bh, "marking dirty");
1117 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001118 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001119 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001120 if (buffer_write_io_error(bh)) {
1121 printk(KERN_ERR "JBD2: I/O error detected "
1122 "when updating journal superblock for %s.\n",
1123 journal->j_devname);
1124 clear_buffer_write_io_error(bh);
1125 set_buffer_uptodate(bh);
1126 }
1127 } else
Dave Kleikamp470decc2006-10-11 01:20:57 -07001128 ll_rw_block(SWRITE, 1, &bh);
1129
1130out:
1131 /* If we have just flushed the log (by marking s_start==0), then
1132 * any future commit will have to be careful to update the
1133 * superblock again to re-record the true start of the log. */
1134
Theodore Ts'oa931da62010-08-03 21:35:12 -04001135 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001136 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001137 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001138 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001139 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001140 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001141}
1142
1143/*
1144 * Read the superblock for a given journal, performing initial
1145 * validation of the format.
1146 */
1147
1148static int journal_get_superblock(journal_t *journal)
1149{
1150 struct buffer_head *bh;
1151 journal_superblock_t *sb;
1152 int err = -EIO;
1153
1154 bh = journal->j_sb_buffer;
1155
1156 J_ASSERT(bh != NULL);
1157 if (!buffer_uptodate(bh)) {
1158 ll_rw_block(READ, 1, &bh);
1159 wait_on_buffer(bh);
1160 if (!buffer_uptodate(bh)) {
1161 printk (KERN_ERR
1162 "JBD: IO error reading journal superblock\n");
1163 goto out;
1164 }
1165 }
1166
1167 sb = journal->j_superblock;
1168
1169 err = -EINVAL;
1170
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001171 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001172 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1173 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1174 goto out;
1175 }
1176
1177 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001178 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001179 journal->j_format_version = 1;
1180 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001181 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001182 journal->j_format_version = 2;
1183 break;
1184 default:
1185 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1186 goto out;
1187 }
1188
1189 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1190 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1191 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1192 printk (KERN_WARNING "JBD: journal file too short\n");
1193 goto out;
1194 }
1195
1196 return 0;
1197
1198out:
1199 journal_fail_superblock(journal);
1200 return err;
1201}
1202
1203/*
1204 * Load the on-disk journal superblock and read the key fields into the
1205 * journal_t.
1206 */
1207
1208static int load_superblock(journal_t *journal)
1209{
1210 int err;
1211 journal_superblock_t *sb;
1212
1213 err = journal_get_superblock(journal);
1214 if (err)
1215 return err;
1216
1217 sb = journal->j_superblock;
1218
1219 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1220 journal->j_tail = be32_to_cpu(sb->s_start);
1221 journal->j_first = be32_to_cpu(sb->s_first);
1222 journal->j_last = be32_to_cpu(sb->s_maxlen);
1223 journal->j_errno = be32_to_cpu(sb->s_errno);
1224
1225 return 0;
1226}
1227
1228
1229/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001230 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 * @journal: Journal to act on.
1232 *
1233 * Given a journal_t structure which tells us which disk blocks contain
1234 * a journal, read the journal from disk to initialise the in-memory
1235 * structures.
1236 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001237int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238{
1239 int err;
1240 journal_superblock_t *sb;
1241
1242 err = load_superblock(journal);
1243 if (err)
1244 return err;
1245
1246 sb = journal->j_superblock;
1247 /* If this is a V2 superblock, then we have to check the
1248 * features flags on it. */
1249
1250 if (journal->j_format_version >= 2) {
1251 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001252 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001253 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001254 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001255 printk (KERN_WARNING
1256 "JBD: Unrecognised features on journal\n");
1257 return -EINVAL;
1258 }
1259 }
1260
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001261 /*
1262 * Create a slab for this blocksize
1263 */
1264 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1265 if (err)
1266 return err;
1267
Dave Kleikamp470decc2006-10-11 01:20:57 -07001268 /* Let the recovery code check whether it needs to recover any
1269 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001270 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001271 goto recovery_error;
1272
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001273 if (journal->j_failed_commit) {
1274 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1275 "is corrupt.\n", journal->j_failed_commit,
1276 journal->j_devname);
1277 return -EIO;
1278 }
1279
Dave Kleikamp470decc2006-10-11 01:20:57 -07001280 /* OK, we've finished with the dynamic journal bits:
1281 * reinitialise the dynamic contents of the superblock in memory
1282 * and reset them on disk. */
1283 if (journal_reset(journal))
1284 goto recovery_error;
1285
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001286 journal->j_flags &= ~JBD2_ABORT;
1287 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001288 return 0;
1289
1290recovery_error:
1291 printk (KERN_WARNING "JBD: recovery failed\n");
1292 return -EIO;
1293}
1294
1295/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001296 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001297 * @journal: Journal to act on.
1298 *
1299 * Release a journal_t structure once it is no longer in use by the
1300 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001301 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001302 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001303int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001304{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001305 int err = 0;
1306
Dave Kleikamp470decc2006-10-11 01:20:57 -07001307 /* Wait for the commit thread to wake up and die. */
1308 journal_kill_thread(journal);
1309
1310 /* Force a final log commit */
1311 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001312 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001313
1314 /* Force any old transactions to disk */
1315
1316 /* Totally anal locking here... */
1317 spin_lock(&journal->j_list_lock);
1318 while (journal->j_checkpoint_transactions != NULL) {
1319 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001320 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001321 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001322 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001323 spin_lock(&journal->j_list_lock);
1324 }
1325
1326 J_ASSERT(journal->j_running_transaction == NULL);
1327 J_ASSERT(journal->j_committing_transaction == NULL);
1328 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1329 spin_unlock(&journal->j_list_lock);
1330
Dave Kleikamp470decc2006-10-11 01:20:57 -07001331 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001332 if (!is_journal_aborted(journal)) {
1333 /* We can now mark the journal as empty. */
1334 journal->j_tail = 0;
1335 journal->j_tail_sequence =
1336 ++journal->j_transaction_sequence;
1337 jbd2_journal_update_superblock(journal, 1);
1338 } else {
1339 err = -EIO;
1340 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001341 brelse(journal->j_sb_buffer);
1342 }
1343
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001344 if (journal->j_proc_entry)
1345 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001346 if (journal->j_inode)
1347 iput(journal->j_inode);
1348 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001349 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001350 kfree(journal->j_wbuf);
1351 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001352
1353 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001354}
1355
1356
1357/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001358 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001359 * @journal: Journal to check.
1360 * @compat: bitmask of compatible features
1361 * @ro: bitmask of features that force read-only mount
1362 * @incompat: bitmask of incompatible features
1363 *
1364 * Check whether the journal uses all of a given set of
1365 * features. Return true (non-zero) if it does.
1366 **/
1367
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001368int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001369 unsigned long ro, unsigned long incompat)
1370{
1371 journal_superblock_t *sb;
1372
1373 if (!compat && !ro && !incompat)
1374 return 1;
1375 if (journal->j_format_version == 1)
1376 return 0;
1377
1378 sb = journal->j_superblock;
1379
1380 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1381 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1382 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1383 return 1;
1384
1385 return 0;
1386}
1387
1388/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001389 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001390 * @journal: Journal to check.
1391 * @compat: bitmask of compatible features
1392 * @ro: bitmask of features that force read-only mount
1393 * @incompat: bitmask of incompatible features
1394 *
1395 * Check whether the journaling code supports the use of
1396 * all of a given set of features on this journal. Return true
1397 * (non-zero) if it can. */
1398
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001399int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 unsigned long ro, unsigned long incompat)
1401{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001402 if (!compat && !ro && !incompat)
1403 return 1;
1404
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 /* We can support any known requested features iff the
1406 * superblock is in version 2. Otherwise we fail to support any
1407 * extended sb features. */
1408
1409 if (journal->j_format_version != 2)
1410 return 0;
1411
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001412 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1413 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1414 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001415 return 1;
1416
1417 return 0;
1418}
1419
1420/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001421 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001422 * @journal: Journal to act on.
1423 * @compat: bitmask of compatible features
1424 * @ro: bitmask of features that force read-only mount
1425 * @incompat: bitmask of incompatible features
1426 *
1427 * Mark a given journal feature as present on the
1428 * superblock. Returns true if the requested features could be set.
1429 *
1430 */
1431
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001432int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001433 unsigned long ro, unsigned long incompat)
1434{
1435 journal_superblock_t *sb;
1436
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001437 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438 return 1;
1439
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001440 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 return 0;
1442
1443 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1444 compat, ro, incompat);
1445
1446 sb = journal->j_superblock;
1447
1448 sb->s_feature_compat |= cpu_to_be32(compat);
1449 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1450 sb->s_feature_incompat |= cpu_to_be32(incompat);
1451
1452 return 1;
1453}
1454
Girish Shilamkar818d2762008-01-28 23:58:27 -05001455/*
1456 * jbd2_journal_clear_features () - Clear a given journal feature in the
1457 * superblock
1458 * @journal: Journal to act on.
1459 * @compat: bitmask of compatible features
1460 * @ro: bitmask of features that force read-only mount
1461 * @incompat: bitmask of incompatible features
1462 *
1463 * Clear a given journal feature as present on the
1464 * superblock.
1465 */
1466void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1467 unsigned long ro, unsigned long incompat)
1468{
1469 journal_superblock_t *sb;
1470
1471 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1472 compat, ro, incompat);
1473
1474 sb = journal->j_superblock;
1475
1476 sb->s_feature_compat &= ~cpu_to_be32(compat);
1477 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1478 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1479}
1480EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481
1482/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001483 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001484 * @journal: Journal to act on.
1485 *
1486 * Given an initialised but unloaded journal struct, poke about in the
1487 * on-disk structure to update it to the most recent supported version.
1488 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001489int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001490{
1491 journal_superblock_t *sb;
1492 int err;
1493
1494 err = journal_get_superblock(journal);
1495 if (err)
1496 return err;
1497
1498 sb = journal->j_superblock;
1499
1500 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001501 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001502 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001503 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001504 return journal_convert_superblock_v1(journal, sb);
1505 default:
1506 break;
1507 }
1508 return -EINVAL;
1509}
1510
1511static int journal_convert_superblock_v1(journal_t *journal,
1512 journal_superblock_t *sb)
1513{
1514 int offset, blocksize;
1515 struct buffer_head *bh;
1516
1517 printk(KERN_WARNING
1518 "JBD: Converting superblock from version 1 to 2.\n");
1519
1520 /* Pre-initialise new fields to zero */
1521 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1522 blocksize = be32_to_cpu(sb->s_blocksize);
1523 memset(&sb->s_feature_compat, 0, blocksize-offset);
1524
1525 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001526 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001527 journal->j_format_version = 2;
1528
1529 bh = journal->j_sb_buffer;
1530 BUFFER_TRACE(bh, "marking dirty");
1531 mark_buffer_dirty(bh);
1532 sync_dirty_buffer(bh);
1533 return 0;
1534}
1535
1536
1537/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001538 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001539 * @journal: Journal to act on.
1540 *
1541 * Flush all data for a given journal to disk and empty the journal.
1542 * Filesystems can use this when remounting readonly to ensure that
1543 * recovery does not need to happen on remount.
1544 */
1545
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001546int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001547{
1548 int err = 0;
1549 transaction_t *transaction = NULL;
1550 unsigned long old_tail;
1551
Theodore Ts'oa931da62010-08-03 21:35:12 -04001552 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001553
1554 /* Force everything buffered to the log... */
1555 if (journal->j_running_transaction) {
1556 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001557 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001558 } else if (journal->j_committing_transaction)
1559 transaction = journal->j_committing_transaction;
1560
1561 /* Wait for the log commit to complete... */
1562 if (transaction) {
1563 tid_t tid = transaction->t_tid;
1564
Theodore Ts'oa931da62010-08-03 21:35:12 -04001565 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001566 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001567 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001568 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001569 }
1570
1571 /* ...and flush everything in the log out to disk. */
1572 spin_lock(&journal->j_list_lock);
1573 while (!err && journal->j_checkpoint_transactions != NULL) {
1574 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001575 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001576 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001577 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001578 spin_lock(&journal->j_list_lock);
1579 }
1580 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001581
1582 if (is_journal_aborted(journal))
1583 return -EIO;
1584
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001585 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001586
1587 /* Finally, mark the journal as really needing no recovery.
1588 * This sets s_start==0 in the underlying superblock, which is
1589 * the magic code for a fully-recovered superblock. Any future
1590 * commits of data to the journal will restore the current
1591 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001592 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593 old_tail = journal->j_tail;
1594 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001595 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001596 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001597 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001598 journal->j_tail = old_tail;
1599
1600 J_ASSERT(!journal->j_running_transaction);
1601 J_ASSERT(!journal->j_committing_transaction);
1602 J_ASSERT(!journal->j_checkpoint_transactions);
1603 J_ASSERT(journal->j_head == journal->j_tail);
1604 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001605 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001606 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001607}
1608
1609/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001610 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001611 * @journal: Journal to act on.
1612 * @write: flag (see below)
1613 *
1614 * Wipe out all of the contents of a journal, safely. This will produce
1615 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001616 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617 *
1618 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1619 * we merely suppress recovery.
1620 */
1621
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001622int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001623{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624 int err = 0;
1625
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001626 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627
1628 err = load_superblock(journal);
1629 if (err)
1630 return err;
1631
Dave Kleikamp470decc2006-10-11 01:20:57 -07001632 if (!journal->j_tail)
1633 goto no_recovery;
1634
1635 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1636 write ? "Clearing" : "Ignoring");
1637
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001640 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001641
1642 no_recovery:
1643 return err;
1644}
1645
1646/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647 * Journal abort has very specific semantics, which we describe
1648 * for journal abort.
1649 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001650 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001651 * itself are here.
1652 */
1653
1654/*
1655 * Quick version for internal journal use (doesn't lock the journal).
1656 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1657 * and don't attempt to make any other journal updates.
1658 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001659void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001660{
1661 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001662
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001663 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664 return;
1665
1666 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001667 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001668
Theodore Ts'oa931da62010-08-03 21:35:12 -04001669 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001670 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671 transaction = journal->j_running_transaction;
1672 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001673 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001674 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001675}
1676
1677/* Soft abort: record the abort error status in the journal superblock,
1678 * but don't do any other IO. */
1679static void __journal_abort_soft (journal_t *journal, int errno)
1680{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001681 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001682 return;
1683
1684 if (!journal->j_errno)
1685 journal->j_errno = errno;
1686
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001687 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001688
1689 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001690 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001691}
1692
1693/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001694 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001695 * @journal: the journal to shutdown.
1696 * @errno: an error number to record in the journal indicating
1697 * the reason for the shutdown.
1698 *
1699 * Perform a complete, immediate shutdown of the ENTIRE
1700 * journal (not of a single transaction). This operation cannot be
1701 * undone without closing and reopening the journal.
1702 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001703 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1705 * mode.
1706 *
1707 * Journal abort has very specific semantics. Any existing dirty,
1708 * unjournaled buffers in the main filesystem will still be written to
1709 * disk by bdflush, but the journaling mechanism will be suspended
1710 * immediately and no further transaction commits will be honoured.
1711 *
1712 * Any dirty, journaled buffers will be written back to disk without
1713 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1714 * filesystem, but we _do_ attempt to leave as much data as possible
1715 * behind for fsck to use for cleanup.
1716 *
1717 * Any attempt to get a new transaction handle on a journal which is in
1718 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001719 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001720 * entered abort state during the update.
1721 *
1722 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001723 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001724 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001725 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001726 * which will be recorded (if possible) in the journal superblock. This
1727 * allows a client to record failure conditions in the middle of a
1728 * transaction without having to complete the transaction to record the
1729 * failure to disk. ext3_error, for example, now uses this
1730 * functionality.
1731 *
1732 * Errors which originate from within the journaling layer will NOT
1733 * supply an errno; a null errno implies that absolutely no further
1734 * writes are done to the journal (unless there are any already in
1735 * progress).
1736 *
1737 */
1738
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001739void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001740{
1741 __journal_abort_soft(journal, errno);
1742}
1743
1744/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001745 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001746 * @journal: journal to examine.
1747 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001748 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 * time the journal was mounted - if the journal was stopped
1750 * without calling abort this will be 0.
1751 *
1752 * If the journal has been aborted on this mount time -EROFS will
1753 * be returned.
1754 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001755int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001756{
1757 int err;
1758
Theodore Ts'oa931da62010-08-03 21:35:12 -04001759 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001760 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001761 err = -EROFS;
1762 else
1763 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001764 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001765 return err;
1766}
1767
1768/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001769 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001770 * @journal: journal to act on.
1771 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001772 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001773 * mode.
1774 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001775int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776{
1777 int err = 0;
1778
Theodore Ts'oa931da62010-08-03 21:35:12 -04001779 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001780 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001781 err = -EROFS;
1782 else
1783 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001784 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001785 return err;
1786}
1787
1788/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001789 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001790 * @journal: journal to act on.
1791 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001792 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 * mode.
1794 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001797 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001799 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001800 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001801}
1802
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001803int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804{
1805 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1806}
1807
1808/*
Zach Brownb517bea2006-10-11 01:21:08 -07001809 * helper functions to deal with 32 or 64bit block numbers.
1810 */
1811size_t journal_tag_bytes(journal_t *journal)
1812{
1813 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001814 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001815 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001816 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001817}
1818
1819/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001820 * JBD memory management
1821 *
1822 * These functions are used to allocate block-sized chunks of memory
1823 * used for making copies of buffer_head data. Very often it will be
1824 * page-sized chunks of data, but sometimes it will be in
1825 * sub-page-size chunks. (For example, 16k pages on Power systems
1826 * with a 4k block file system.) For blocks smaller than a page, we
1827 * use a SLAB allocator. There are slab caches for each block size,
1828 * which are allocated at mount time, if necessary, and we only free
1829 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1830 * this reason we don't need to a mutex to protect access to
1831 * jbd2_slab[] allocating or releasing memory; only in
1832 * jbd2_journal_create_slab().
1833 */
1834#define JBD2_MAX_SLABS 8
1835static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
1836static DECLARE_MUTEX(jbd2_slab_create_sem);
1837
1838static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1839 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1840 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1841};
1842
1843
1844static void jbd2_journal_destroy_slabs(void)
1845{
1846 int i;
1847
1848 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1849 if (jbd2_slab[i])
1850 kmem_cache_destroy(jbd2_slab[i]);
1851 jbd2_slab[i] = NULL;
1852 }
1853}
1854
1855static int jbd2_journal_create_slab(size_t size)
1856{
1857 int i = order_base_2(size) - 10;
1858 size_t slab_size;
1859
1860 if (size == PAGE_SIZE)
1861 return 0;
1862
1863 if (i >= JBD2_MAX_SLABS)
1864 return -EINVAL;
1865
1866 if (unlikely(i < 0))
1867 i = 0;
1868 down(&jbd2_slab_create_sem);
1869 if (jbd2_slab[i]) {
1870 up(&jbd2_slab_create_sem);
1871 return 0; /* Already created */
1872 }
1873
1874 slab_size = 1 << (i+10);
1875 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1876 slab_size, 0, NULL);
1877 up(&jbd2_slab_create_sem);
1878 if (!jbd2_slab[i]) {
1879 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1880 return -ENOMEM;
1881 }
1882 return 0;
1883}
1884
1885static struct kmem_cache *get_slab(size_t size)
1886{
1887 int i = order_base_2(size) - 10;
1888
1889 BUG_ON(i >= JBD2_MAX_SLABS);
1890 if (unlikely(i < 0))
1891 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001892 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001893 return jbd2_slab[i];
1894}
1895
1896void *jbd2_alloc(size_t size, gfp_t flags)
1897{
1898 void *ptr;
1899
1900 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1901
1902 flags |= __GFP_REPEAT;
1903 if (size == PAGE_SIZE)
1904 ptr = (void *)__get_free_pages(flags, 0);
1905 else if (size > PAGE_SIZE) {
1906 int order = get_order(size);
1907
1908 if (order < 3)
1909 ptr = (void *)__get_free_pages(flags, order);
1910 else
1911 ptr = vmalloc(size);
1912 } else
1913 ptr = kmem_cache_alloc(get_slab(size), flags);
1914
1915 /* Check alignment; SLUB has gotten this wrong in the past,
1916 * and this can lead to user data corruption! */
1917 BUG_ON(((unsigned long) ptr) & (size-1));
1918
1919 return ptr;
1920}
1921
1922void jbd2_free(void *ptr, size_t size)
1923{
1924 if (size == PAGE_SIZE) {
1925 free_pages((unsigned long)ptr, 0);
1926 return;
1927 }
1928 if (size > PAGE_SIZE) {
1929 int order = get_order(size);
1930
1931 if (order < 3)
1932 free_pages((unsigned long)ptr, order);
1933 else
1934 vfree(ptr);
1935 return;
1936 }
1937 kmem_cache_free(get_slab(size), ptr);
1938};
1939
1940/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001941 * Journal_head storage management
1942 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001943static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001944#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001945static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1946#endif
1947
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001948static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001949{
1950 int retval;
1951
Al Viro1076d172008-03-29 03:07:18 +00001952 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001953 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001954 sizeof(struct journal_head),
1955 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001956 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001957 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001958 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001959 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 retval = -ENOMEM;
1961 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1962 }
1963 return retval;
1964}
1965
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001968 if (jbd2_journal_head_cache) {
1969 kmem_cache_destroy(jbd2_journal_head_cache);
1970 jbd2_journal_head_cache = NULL;
1971 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001972}
1973
1974/*
1975 * journal_head splicing and dicing
1976 */
1977static struct journal_head *journal_alloc_journal_head(void)
1978{
1979 struct journal_head *ret;
1980 static unsigned long last_warning;
1981
Jose R. Santose23291b2007-07-18 08:57:06 -04001982#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001983 atomic_inc(&nr_journal_heads);
1984#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001985 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001986 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001987 jbd_debug(1, "out of memory for journal_head\n");
1988 if (time_after(jiffies, last_warning + 5*HZ)) {
1989 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001990 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991 last_warning = jiffies;
1992 }
Al Viro1076d172008-03-29 03:07:18 +00001993 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001994 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001995 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001996 }
1997 }
1998 return ret;
1999}
2000
2001static void journal_free_journal_head(struct journal_head *jh)
2002{
Jose R. Santose23291b2007-07-18 08:57:06 -04002003#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002005 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002006#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002007 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002008}
2009
2010/*
2011 * A journal_head is attached to a buffer_head whenever JBD has an
2012 * interest in the buffer.
2013 *
2014 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2015 * is set. This bit is tested in core kernel code where we need to take
2016 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2017 * there.
2018 *
2019 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2020 *
2021 * When a buffer has its BH_JBD bit set it is immune from being released by
2022 * core kernel code, mainly via ->b_count.
2023 *
2024 * A journal_head may be detached from its buffer_head when the journal_head's
2025 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002026 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002027 * journal_head can be dropped if needed.
2028 *
2029 * Various places in the kernel want to attach a journal_head to a buffer_head
2030 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002031 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002032 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002033 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002034 *
2035 * So the typical usage would be:
2036 *
2037 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002038 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002039 * ...
2040 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 *
2043 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2044 * because it has a non-zero b_transaction.
2045 */
2046
2047/*
2048 * Give a buffer_head a journal_head.
2049 *
2050 * Doesn't need the journal lock.
2051 * May sleep.
2052 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054{
2055 struct journal_head *jh;
2056 struct journal_head *new_jh = NULL;
2057
2058repeat:
2059 if (!buffer_jbd(bh)) {
2060 new_jh = journal_alloc_journal_head();
2061 memset(new_jh, 0, sizeof(*new_jh));
2062 }
2063
2064 jbd_lock_bh_journal_head(bh);
2065 if (buffer_jbd(bh)) {
2066 jh = bh2jh(bh);
2067 } else {
2068 J_ASSERT_BH(bh,
2069 (atomic_read(&bh->b_count) > 0) ||
2070 (bh->b_page && bh->b_page->mapping));
2071
2072 if (!new_jh) {
2073 jbd_unlock_bh_journal_head(bh);
2074 goto repeat;
2075 }
2076
2077 jh = new_jh;
2078 new_jh = NULL; /* We consumed it */
2079 set_buffer_jbd(bh);
2080 bh->b_private = jh;
2081 jh->b_bh = bh;
2082 get_bh(bh);
2083 BUFFER_TRACE(bh, "added journal_head");
2084 }
2085 jh->b_jcount++;
2086 jbd_unlock_bh_journal_head(bh);
2087 if (new_jh)
2088 journal_free_journal_head(new_jh);
2089 return bh->b_private;
2090}
2091
2092/*
2093 * Grab a ref against this buffer_head's journal_head. If it ended up not
2094 * having a journal_head, return NULL
2095 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002096struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002097{
2098 struct journal_head *jh = NULL;
2099
2100 jbd_lock_bh_journal_head(bh);
2101 if (buffer_jbd(bh)) {
2102 jh = bh2jh(bh);
2103 jh->b_jcount++;
2104 }
2105 jbd_unlock_bh_journal_head(bh);
2106 return jh;
2107}
2108
2109static void __journal_remove_journal_head(struct buffer_head *bh)
2110{
2111 struct journal_head *jh = bh2jh(bh);
2112
2113 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2114
2115 get_bh(bh);
2116 if (jh->b_jcount == 0) {
2117 if (jh->b_transaction == NULL &&
2118 jh->b_next_transaction == NULL &&
2119 jh->b_cp_transaction == NULL) {
2120 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2121 J_ASSERT_BH(bh, buffer_jbd(bh));
2122 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2123 BUFFER_TRACE(bh, "remove journal_head");
2124 if (jh->b_frozen_data) {
2125 printk(KERN_WARNING "%s: freeing "
2126 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002127 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002128 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002129 }
2130 if (jh->b_committed_data) {
2131 printk(KERN_WARNING "%s: freeing "
2132 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002133 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002134 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002135 }
2136 bh->b_private = NULL;
2137 jh->b_bh = NULL; /* debug, really */
2138 clear_buffer_jbd(bh);
2139 __brelse(bh);
2140 journal_free_journal_head(jh);
2141 } else {
2142 BUFFER_TRACE(bh, "journal_head was locked");
2143 }
2144 }
2145}
2146
2147/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002148 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002149 * and has a zero b_jcount then remove and release its journal_head. If we did
2150 * see that the buffer is not used by any transaction we also "logically"
2151 * decrement ->b_count.
2152 *
2153 * We in fact take an additional increment on ->b_count as a convenience,
2154 * because the caller usually wants to do additional things with the bh
2155 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002156 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002157 * time. Once the caller has run __brelse(), the buffer is eligible for
2158 * reaping by try_to_free_buffers().
2159 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002160void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002161{
2162 jbd_lock_bh_journal_head(bh);
2163 __journal_remove_journal_head(bh);
2164 jbd_unlock_bh_journal_head(bh);
2165}
2166
2167/*
2168 * Drop a reference on the passed journal_head. If it fell to zero then try to
2169 * release the journal_head from the buffer_head.
2170 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002171void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002172{
2173 struct buffer_head *bh = jh2bh(jh);
2174
2175 jbd_lock_bh_journal_head(bh);
2176 J_ASSERT_JH(jh, jh->b_jcount > 0);
2177 --jh->b_jcount;
2178 if (!jh->b_jcount && !jh->b_transaction) {
2179 __journal_remove_journal_head(bh);
2180 __brelse(bh);
2181 }
2182 jbd_unlock_bh_journal_head(bh);
2183}
2184
2185/*
Jan Karac851ed52008-07-11 19:27:31 -04002186 * Initialize jbd inode head
2187 */
2188void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2189{
2190 jinode->i_transaction = NULL;
2191 jinode->i_next_transaction = NULL;
2192 jinode->i_vfs_inode = inode;
2193 jinode->i_flags = 0;
2194 INIT_LIST_HEAD(&jinode->i_list);
2195}
2196
2197/*
2198 * Function to be called before we start removing inode from memory (i.e.,
2199 * clear_inode() is a fine place to be called from). It removes inode from
2200 * transaction's lists.
2201 */
2202void jbd2_journal_release_jbd_inode(journal_t *journal,
2203 struct jbd2_inode *jinode)
2204{
Jan Karac851ed52008-07-11 19:27:31 -04002205 if (!journal)
2206 return;
2207restart:
2208 spin_lock(&journal->j_list_lock);
2209 /* Is commit writing out inode - we have to wait */
2210 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2211 wait_queue_head_t *wq;
2212 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2213 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2214 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2215 spin_unlock(&journal->j_list_lock);
2216 schedule();
2217 finish_wait(wq, &wait.wait);
2218 goto restart;
2219 }
2220
Jan Karac851ed52008-07-11 19:27:31 -04002221 if (jinode->i_transaction) {
2222 list_del(&jinode->i_list);
2223 jinode->i_transaction = NULL;
2224 }
2225 spin_unlock(&journal->j_list_lock);
2226}
2227
2228/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002229 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002230 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002231#ifdef CONFIG_JBD2_DEBUG
2232u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002233EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002234
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002235#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002236
Jose R. Santos6f38c742007-10-16 18:38:25 -04002237static struct dentry *jbd2_debugfs_dir;
2238static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002239
2240static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002241{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002242 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2243 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002244 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2245 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002246 jbd2_debugfs_dir,
2247 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002248}
2249
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002250static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002251{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002252 debugfs_remove(jbd2_debug);
2253 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254}
2255
2256#else
2257
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002258static void __init jbd2_create_debugfs_entry(void)
2259{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002260}
2261
2262static void __exit jbd2_remove_debugfs_entry(void)
2263{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002264}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002265
2266#endif
2267
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002268#ifdef CONFIG_PROC_FS
2269
2270#define JBD2_STATS_PROC_NAME "fs/jbd2"
2271
2272static void __init jbd2_create_jbd_stats_proc_entry(void)
2273{
2274 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2275}
2276
2277static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2278{
2279 if (proc_jbd2_stats)
2280 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2281}
2282
2283#else
2284
2285#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2286#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2287
2288#endif
2289
Christoph Lametere18b8902006-12-06 20:33:20 -08002290struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002291
2292static int __init journal_init_handle_cache(void)
2293{
Johann Lombardia920e942006-10-11 01:21:00 -07002294 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002295 sizeof(handle_t),
2296 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002297 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002298 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002299 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002300 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2301 return -ENOMEM;
2302 }
2303 return 0;
2304}
2305
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002306static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002307{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002308 if (jbd2_handle_cache)
2309 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002310}
2311
2312/*
2313 * Module startup and shutdown
2314 */
2315
2316static int __init journal_init_caches(void)
2317{
2318 int ret;
2319
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002320 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002321 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002322 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002323 if (ret == 0)
2324 ret = journal_init_handle_cache();
2325 return ret;
2326}
2327
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002328static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002329{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002330 jbd2_journal_destroy_revoke_caches();
2331 jbd2_journal_destroy_jbd2_journal_head_cache();
2332 jbd2_journal_destroy_handle_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002333 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002334}
2335
2336static int __init journal_init(void)
2337{
2338 int ret;
2339
2340 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2341
2342 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002343 if (ret == 0) {
2344 jbd2_create_debugfs_entry();
2345 jbd2_create_jbd_stats_proc_entry();
2346 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002347 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002348 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002349 return ret;
2350}
2351
2352static void __exit journal_exit(void)
2353{
Jose R. Santose23291b2007-07-18 08:57:06 -04002354#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002355 int n = atomic_read(&nr_journal_heads);
2356 if (n)
2357 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2358#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002359 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002360 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002361 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362}
2363
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002364/*
2365 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2366 * tracing infrastructure to map a dev_t to a device name.
2367 *
2368 * The caller should use rcu_read_lock() in order to make sure the
2369 * device name stays valid until its done with it. We use
2370 * rcu_read_lock() as well to make sure we're safe in case the caller
2371 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2372 * nested.
2373 */
2374struct devname_cache {
2375 struct rcu_head rcu;
2376 dev_t device;
2377 char devname[BDEVNAME_SIZE];
2378};
2379#define CACHE_SIZE_BITS 6
2380static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2381static DEFINE_SPINLOCK(devname_cache_lock);
2382
2383static void free_devcache(struct rcu_head *rcu)
2384{
2385 kfree(rcu);
2386}
2387
2388const char *jbd2_dev_to_name(dev_t device)
2389{
2390 int i = hash_32(device, CACHE_SIZE_BITS);
2391 char *ret;
2392 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002393 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002394
2395 rcu_read_lock();
2396 if (devcache[i] && devcache[i]->device == device) {
2397 ret = devcache[i]->devname;
2398 rcu_read_unlock();
2399 return ret;
2400 }
2401 rcu_read_unlock();
2402
Theodore Ts'ob5744802009-06-20 23:34:44 -04002403 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2404 if (!new_dev)
2405 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002406 spin_lock(&devname_cache_lock);
2407 if (devcache[i]) {
2408 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002409 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002410 ret = devcache[i]->devname;
2411 spin_unlock(&devname_cache_lock);
2412 return ret;
2413 }
2414 call_rcu(&devcache[i]->rcu, free_devcache);
2415 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002416 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002417 devcache[i]->device = device;
2418 bd = bdget(device);
2419 if (bd) {
2420 bdevname(bd, devcache[i]->devname);
2421 bdput(bd);
2422 } else
2423 __bdevname(device, devcache[i]->devname);
2424 ret = devcache[i]->devname;
2425 spin_unlock(&devname_cache_lock);
2426 return ret;
2427}
2428EXPORT_SYMBOL(jbd2_dev_to_name);
2429
Dave Kleikamp470decc2006-10-11 01:20:57 -07002430MODULE_LICENSE("GPL");
2431module_init(journal_init);
2432module_exit(journal_exit);
2433