blob: 4ba2e81e35ac8da784f7172612519a01bb6cb8af [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>
Brian King39e3ac22010-10-27 21:25:12 -040045#include <linux/bitops.h>
Theodore Ts'o670be5a2010-12-17 10:44:16 -050046#include <linux/ratelimit.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040047
48#define CREATE_TRACE_POINTS
49#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070050
51#include <asm/uaccess.h>
52#include <asm/page.h>
53
Mingming Caof7f4bcc2006-10-11 01:20:59 -070054EXPORT_SYMBOL(jbd2_journal_extend);
55EXPORT_SYMBOL(jbd2_journal_stop);
56EXPORT_SYMBOL(jbd2_journal_lock_updates);
57EXPORT_SYMBOL(jbd2_journal_unlock_updates);
58EXPORT_SYMBOL(jbd2_journal_get_write_access);
59EXPORT_SYMBOL(jbd2_journal_get_create_access);
60EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070061EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070062EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070064#if 0
65EXPORT_SYMBOL(journal_sync_buffer);
66#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070067EXPORT_SYMBOL(jbd2_journal_flush);
68EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070069
Mingming Caof7f4bcc2006-10-11 01:20:59 -070070EXPORT_SYMBOL(jbd2_journal_init_dev);
71EXPORT_SYMBOL(jbd2_journal_init_inode);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070072EXPORT_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);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050094EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -070095
Dave Kleikamp470decc2006-10-11 01:20:57 -070096static 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
Darrick J. Wong25ed6e82012-05-27 07:48:56 -040099/* Checksumming functions */
100int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
101{
102 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
103 return 1;
104
105 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
106}
107
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -0400108static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
109{
110 __u32 csum, old_csum;
111
112 old_csum = sb->s_checksum;
113 sb->s_checksum = 0;
114 csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
115 sb->s_checksum = old_csum;
116
117 return cpu_to_be32(csum);
118}
119
120int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
121{
122 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
123 return 1;
124
125 return sb->s_checksum == jbd2_superblock_csum(j, sb);
126}
127
128void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
129{
130 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
131 return;
132
133 sb->s_checksum = jbd2_superblock_csum(j, sb);
134}
135
Dave Kleikamp470decc2006-10-11 01:20:57 -0700136/*
137 * Helper function used to manage commit timeouts
138 */
139
140static void commit_timeout(unsigned long __data)
141{
142 struct task_struct * p = (struct task_struct *) __data;
143
144 wake_up_process(p);
145}
146
147/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700148 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149 * journal.
150 *
151 * This kernel thread is responsible for two things:
152 *
153 * 1) COMMIT: Every so often we need to commit the current state of the
154 * filesystem to disk. The journal thread is responsible for writing
155 * all of the metadata buffers to disk.
156 *
157 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
158 * of the data in that part of the log has been rewritten elsewhere on
159 * the disk. Flushing these old buffers to reclaim space in the log is
160 * known as checkpointing, and this thread is responsible for that job.
161 */
162
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700163static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700164{
165 journal_t *journal = arg;
166 transaction_t *transaction;
167
168 /*
169 * Set up an interval timer which can be used to trigger a commit wakeup
170 * after the commit interval expires
171 */
172 setup_timer(&journal->j_commit_timer, commit_timeout,
173 (unsigned long)current);
174
Nigel Cunningham35c80422012-02-03 19:59:41 +1100175 set_freezable();
176
Dave Kleikamp470decc2006-10-11 01:20:57 -0700177 /* Record that the journal thread is running */
178 journal->j_task = current;
179 wake_up(&journal->j_wait_done_commit);
180
Dave Kleikamp470decc2006-10-11 01:20:57 -0700181 /*
182 * And now, wait forever for commit wakeup events.
183 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400184 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700185
186loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700187 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700188 goto end_loop;
189
190 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
191 journal->j_commit_sequence, journal->j_commit_request);
192
193 if (journal->j_commit_sequence != journal->j_commit_request) {
194 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400195 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700196 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700197 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400198 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700199 goto loop;
200 }
201
202 wake_up(&journal->j_wait_done_commit);
203 if (freezing(current)) {
204 /*
205 * The simpler the better. Flushing journal isn't a
206 * good idea, because that depends on threads that may
207 * be already stopped.
208 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700209 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400210 write_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800211 try_to_freeze();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400212 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700213 } else {
214 /*
215 * We assume on resume that commits are already there,
216 * so we don't sleep
217 */
218 DEFINE_WAIT(wait);
219 int should_sleep = 1;
220
221 prepare_to_wait(&journal->j_wait_commit, &wait,
222 TASK_INTERRUPTIBLE);
223 if (journal->j_commit_sequence != journal->j_commit_request)
224 should_sleep = 0;
225 transaction = journal->j_running_transaction;
226 if (transaction && time_after_eq(jiffies,
227 transaction->t_expires))
228 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700229 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700230 should_sleep = 0;
231 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400232 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700233 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400234 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700235 }
236 finish_wait(&journal->j_wait_commit, &wait);
237 }
238
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700239 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700240
241 /*
242 * Were we woken up by a commit wakeup event?
243 */
244 transaction = journal->j_running_transaction;
245 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
246 journal->j_commit_request = transaction->t_tid;
247 jbd_debug(1, "woke because of timeout\n");
248 }
249 goto loop;
250
251end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400252 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700253 del_timer_sync(&journal->j_commit_timer);
254 journal->j_task = NULL;
255 wake_up(&journal->j_wait_done_commit);
256 jbd_debug(1, "Journal thread exiting.\n");
257 return 0;
258}
259
Pavel Emelianov97f06782007-05-08 00:30:42 -0700260static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700261{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700262 struct task_struct *t;
263
Theodore Ts'o90576c02009-09-29 15:51:30 -0400264 t = kthread_run(kjournald2, journal, "jbd2/%s",
265 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700266 if (IS_ERR(t))
267 return PTR_ERR(t);
268
Al Viro1076d172008-03-29 03:07:18 +0000269 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700270 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700271}
272
273static void journal_kill_thread(journal_t *journal)
274{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400275 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700276 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700277
278 while (journal->j_task) {
279 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400280 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000281 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400282 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700283 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400284 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700285}
286
287/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700288 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700289 *
290 * Writes a metadata buffer to a given disk block. The actual IO is not
291 * performed but a new buffer_head is constructed which labels the data
292 * to be written with the correct destination disk block.
293 *
294 * Any magic-number escaping which needs to be done will cause a
295 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700296 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700297 * magic number is only written to the log for descripter blocks. In
298 * this case, we copy the data and replace the first word with 0, and we
299 * return a result code which indicates that this buffer needs to be
300 * marked as an escaped buffer in the corresponding log descriptor
301 * block. The missing word can then be restored when the block is read
302 * during recovery.
303 *
304 * If the source buffer has already been modified by a new transaction
305 * since we took the last commit snapshot, we use the frozen copy of
306 * that data for IO. If we end up using the existing buffer_head's data
307 * for the write, then we *have* to lock the buffer to prevent anyone
308 * else from using and possibly modifying it while the IO is in
309 * progress.
310 *
311 * The function returns a pointer to the buffer_heads to be used for IO.
312 *
313 * We assume that the journal has already been locked in this function.
314 *
315 * Return value:
316 * <0: Error
317 * >=0: Finished OK
318 *
319 * On success:
320 * Bit 0 set == escape performed on the data
321 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
322 */
323
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700324int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325 struct journal_head *jh_in,
326 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700327 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700328{
329 int need_copy_out = 0;
330 int done_copy_out = 0;
331 int do_escape = 0;
332 char *mapped_data;
333 struct buffer_head *new_bh;
334 struct journal_head *new_jh;
335 struct page *new_page;
336 unsigned int new_offset;
337 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400338 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700339
340 /*
341 * The buffer really shouldn't be locked: only the current committing
342 * transaction is allowed to write it, so nobody else is allowed
343 * to do any IO.
344 *
345 * akpm: except if we're journalling data, and write() output is
346 * also part of a shared mapping, and another thread has
347 * decided to launch a writepage() against this buffer.
348 */
349 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
350
Theodore Ts'o47def822010-07-27 11:56:05 -0400351retry_alloc:
352 new_bh = alloc_buffer_head(GFP_NOFS);
353 if (!new_bh) {
354 /*
355 * Failure is not an option, but __GFP_NOFAIL is going
356 * away; so we retry ourselves here.
357 */
358 congestion_wait(BLK_RW_ASYNC, HZ/50);
359 goto retry_alloc;
360 }
361
dingdinghua96577c42009-07-13 17:55:35 -0400362 /* keep subsequent assertions sane */
363 new_bh->b_state = 0;
364 init_buffer(new_bh, NULL, NULL);
365 atomic_set(&new_bh->b_count, 1);
366 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700367
368 /*
369 * If a new transaction has already done a buffer copy-out, then
370 * we use that version of the data for the commit.
371 */
372 jbd_lock_bh_state(bh_in);
373repeat:
374 if (jh_in->b_frozen_data) {
375 done_copy_out = 1;
376 new_page = virt_to_page(jh_in->b_frozen_data);
377 new_offset = offset_in_page(jh_in->b_frozen_data);
378 } else {
379 new_page = jh2bh(jh_in)->b_page;
380 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
381 }
382
Cong Wang303a8f22011-11-25 23:14:31 +0800383 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700384 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200385 * Fire data frozen trigger if data already wasn't frozen. Do this
386 * before checking for escaping, as the trigger may modify the magic
387 * offset. If a copy-out happens afterwards, it will have the correct
388 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700389 */
Jan Kara13ceef02010-07-14 07:56:33 +0200390 if (!done_copy_out)
391 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
392 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700393
394 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700395 * Check for escaping
396 */
397 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700398 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700399 need_copy_out = 1;
400 do_escape = 1;
401 }
Cong Wang303a8f22011-11-25 23:14:31 +0800402 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700403
404 /*
405 * Do we need to do a data copy?
406 */
407 if (need_copy_out && !done_copy_out) {
408 char *tmp;
409
410 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400411 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500412 if (!tmp) {
413 jbd2_journal_put_journal_head(new_jh);
414 return -ENOMEM;
415 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700416 jbd_lock_bh_state(bh_in);
417 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400418 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700419 goto repeat;
420 }
421
422 jh_in->b_frozen_data = tmp;
Cong Wang303a8f22011-11-25 23:14:31 +0800423 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700424 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800425 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700426
427 new_page = virt_to_page(tmp);
428 new_offset = offset_in_page(tmp);
429 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700430
431 /*
432 * This isn't strictly necessary, as we're using frozen
433 * data for the escaping, but it keeps consistency with
434 * b_frozen_data usage.
435 */
436 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437 }
438
439 /*
440 * Did we need to do an escaping? Now we've done all the
441 * copying, we can finally do so.
442 */
443 if (do_escape) {
Cong Wang303a8f22011-11-25 23:14:31 +0800444 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700445 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang303a8f22011-11-25 23:14:31 +0800446 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700447 }
448
Dave Kleikamp470decc2006-10-11 01:20:57 -0700449 set_bh_page(new_bh, new_page, new_offset);
450 new_jh->b_transaction = NULL;
451 new_bh->b_size = jh2bh(jh_in)->b_size;
452 new_bh->b_bdev = transaction->t_journal->j_dev;
453 new_bh->b_blocknr = blocknr;
454 set_buffer_mapped(new_bh);
455 set_buffer_dirty(new_bh);
456
457 *jh_out = new_jh;
458
459 /*
460 * The to-be-written buffer needs to get moved to the io queue,
461 * and the original buffer whose contents we are shadowing or
462 * copying is moved to the transaction's shadow queue.
463 */
464 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400465 spin_lock(&journal->j_list_lock);
466 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
467 spin_unlock(&journal->j_list_lock);
468 jbd_unlock_bh_state(bh_in);
469
Dave Kleikamp470decc2006-10-11 01:20:57 -0700470 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700471 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700472
473 return do_escape | (done_copy_out << 1);
474}
475
476/*
477 * Allocation code for the journal file. Manage the space left in the
478 * journal, so that we can begin checkpointing when appropriate.
479 */
480
481/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700482 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700483 *
484 * Called with the journal already locked.
485 *
486 * Called under j_state_lock
487 */
488
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700489int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700490{
491 int left = journal->j_free;
492
Theodore Ts'oa931da62010-08-03 21:35:12 -0400493 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700494
495 /*
496 * Be pessimistic here about the number of those free blocks which
497 * might be required for log descriptor control blocks.
498 */
499
500#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
501
502 left -= MIN_LOG_RESERVED_BLOCKS;
503
504 if (left <= 0)
505 return 0;
506 left -= (left >> 3);
507 return left;
508}
509
510/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500511 * Called with j_state_lock locked for writing.
512 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700513 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700514int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700515{
Eric Sandeene7b04ac2013-01-30 00:39:28 -0500516 /* Return if the txn has already requested to be committed */
517 if (journal->j_commit_request == target)
518 return 0;
519
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400521 * The only transaction we can possibly wait upon is the
522 * currently running transaction (if it exists). Otherwise,
523 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700524 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400525 if (journal->j_running_transaction &&
526 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200528 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529 * commit thread. We do _not_ do the commit ourselves.
530 */
531
532 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400533 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700534 journal->j_commit_request,
535 journal->j_commit_sequence);
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500536 journal->j_running_transaction->t_requested = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700537 wake_up(&journal->j_wait_commit);
538 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400539 } else if (!tid_geq(journal->j_commit_request, target))
540 /* This should never happen, but if it does, preserve
541 the evidence before kjournald goes into a loop and
542 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400543 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400544 journal->j_commit_request,
545 journal->j_commit_sequence,
546 target, journal->j_running_transaction ?
547 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700548 return 0;
549}
550
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700551int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700552{
553 int ret;
554
Theodore Ts'oa931da62010-08-03 21:35:12 -0400555 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700556 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400557 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700558 return ret;
559}
560
561/*
562 * Force and wait upon a commit if the calling process is not within
563 * transaction. This is used for forcing out undo-protected data which contains
564 * bitmaps, when the fs is running out of space.
565 *
566 * We can only force the running transaction if we don't have an active handle;
567 * otherwise, we will deadlock.
568 *
569 * Returns true if a transaction was started.
570 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700571int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700572{
573 transaction_t *transaction = NULL;
574 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500575 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700576
Theodore Ts'oa931da62010-08-03 21:35:12 -0400577 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700578 if (journal->j_running_transaction && !current->journal_info) {
579 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500580 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
581 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700582 } else if (journal->j_committing_transaction)
583 transaction = journal->j_committing_transaction;
584
585 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400586 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587 return 0; /* Nothing to retry */
588 }
589
590 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400591 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500592 if (need_to_start)
593 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700594 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595 return 1;
596}
597
598/*
599 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500600 * if a transaction is going to be committed (or is currently already
601 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700602 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700603int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604{
605 int ret = 0;
606
Theodore Ts'oa931da62010-08-03 21:35:12 -0400607 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700608 if (journal->j_running_transaction) {
609 tid_t tid = journal->j_running_transaction->t_tid;
610
Jan Karac88ccea2009-02-10 11:27:46 -0500611 __jbd2_log_start_commit(journal, tid);
612 /* There's a running transaction and we've just made sure
613 * it's commit has been scheduled. */
614 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700615 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500616 ret = 1;
617 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700618 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300619 * If commit has been started, then we have to wait for
620 * completion of that transaction.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700621 */
Jan Karac88ccea2009-02-10 11:27:46 -0500622 if (ptid)
623 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700624 ret = 1;
625 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400626 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700627 return ret;
628}
629
630/*
Jan Karabbd2be32011-05-24 11:59:18 -0400631 * Return 1 if a given transaction has not yet sent barrier request
632 * connected with a transaction commit. If 0 is returned, transaction
633 * may or may not have sent the barrier. Used to avoid sending barrier
634 * twice in common cases.
635 */
636int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
637{
638 int ret = 0;
639 transaction_t *commit_trans;
640
641 if (!(journal->j_flags & JBD2_BARRIER))
642 return 0;
643 read_lock(&journal->j_state_lock);
644 /* Transaction already committed? */
645 if (tid_geq(journal->j_commit_sequence, tid))
646 goto out;
647 commit_trans = journal->j_committing_transaction;
648 if (!commit_trans || commit_trans->t_tid != tid) {
649 ret = 1;
650 goto out;
651 }
652 /*
653 * Transaction is being committed and we already proceeded to
654 * submitting a flush to fs partition?
655 */
656 if (journal->j_fs_dev != journal->j_dev) {
657 if (!commit_trans->t_need_data_flush ||
658 commit_trans->t_state >= T_COMMIT_DFLUSH)
659 goto out;
660 } else {
661 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
662 goto out;
663 }
664 ret = 1;
665out:
666 read_unlock(&journal->j_state_lock);
667 return ret;
668}
669EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
670
671/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700672 * Wait for a specified commit to complete.
673 * The caller may not hold the journal lock.
674 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700675int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700676{
677 int err = 0;
678
Theodore Ts'oa931da62010-08-03 21:35:12 -0400679 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400680#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700681 if (!tid_geq(journal->j_commit_request, tid)) {
682 printk(KERN_EMERG
683 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400684 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700685 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700686#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700687 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400688 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700689 tid, journal->j_commit_sequence);
690 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400691 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700692 wait_event(journal->j_wait_done_commit,
693 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400694 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700695 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400696 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700697
698 if (unlikely(is_journal_aborted(journal))) {
699 printk(KERN_EMERG "journal commit I/O error\n");
700 err = -EIO;
701 }
702 return err;
703}
704
705/*
706 * Log buffer allocation routines:
707 */
708
Mingming Cao18eba7a2006-10-11 01:21:13 -0700709int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700710{
711 unsigned long blocknr;
712
Theodore Ts'oa931da62010-08-03 21:35:12 -0400713 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700714 J_ASSERT(journal->j_free > 1);
715
716 blocknr = journal->j_head;
717 journal->j_head++;
718 journal->j_free--;
719 if (journal->j_head == journal->j_last)
720 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400721 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700722 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700723}
724
725/*
726 * Conversion of logical to physical block numbers for the journal
727 *
728 * On external journals the journal blocks are identity-mapped, so
729 * this is a no-op. If needed, we can use j_blk_offset - everything is
730 * ready.
731 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700732int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700733 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700734{
735 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700736 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700737
738 if (journal->j_inode) {
739 ret = bmap(journal->j_inode, blocknr);
740 if (ret)
741 *retp = ret;
742 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700743 printk(KERN_ALERT "%s: journal block not found "
744 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400745 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700746 err = -EIO;
747 __journal_abort_soft(journal, err);
748 }
749 } else {
750 *retp = blocknr; /* +journal->j_blk_offset */
751 }
752 return err;
753}
754
755/*
756 * We play buffer_head aliasing tricks to write data/metadata blocks to
757 * the journal without copying their contents, but for journal
758 * descriptor blocks we do need to generate bona fide buffers.
759 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700760 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700761 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
762 * But we don't bother doing that, so there will be coherency problems with
763 * mmaps of blockdevs which hold live JBD-controlled filesystems.
764 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700765struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700766{
767 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700768 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700769 int err;
770
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700771 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700772
773 if (err)
774 return NULL;
775
776 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500777 if (!bh)
778 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700779 lock_buffer(bh);
780 memset(bh->b_data, 0, journal->j_blocksize);
781 set_buffer_uptodate(bh);
782 unlock_buffer(bh);
783 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700784 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700785}
786
Jan Kara79feb522012-03-13 22:22:54 -0400787/*
788 * Return tid of the oldest transaction in the journal and block in the journal
789 * where the transaction starts.
790 *
791 * If the journal is now empty, return which will be the next transaction ID
792 * we will write and where will that transaction start.
793 *
794 * The return value is 0 if journal tail cannot be pushed any further, 1 if
795 * it can.
796 */
797int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
798 unsigned long *block)
799{
800 transaction_t *transaction;
801 int ret;
802
803 read_lock(&journal->j_state_lock);
804 spin_lock(&journal->j_list_lock);
805 transaction = journal->j_checkpoint_transactions;
806 if (transaction) {
807 *tid = transaction->t_tid;
808 *block = transaction->t_log_start;
809 } else if ((transaction = journal->j_committing_transaction) != NULL) {
810 *tid = transaction->t_tid;
811 *block = transaction->t_log_start;
812 } else if ((transaction = journal->j_running_transaction) != NULL) {
813 *tid = transaction->t_tid;
814 *block = journal->j_head;
815 } else {
816 *tid = journal->j_transaction_sequence;
817 *block = journal->j_head;
818 }
819 ret = tid_gt(*tid, journal->j_tail_sequence);
820 spin_unlock(&journal->j_list_lock);
821 read_unlock(&journal->j_state_lock);
822
823 return ret;
824}
825
826/*
827 * Update information in journal structure and in on disk journal superblock
828 * about log tail. This function does not check whether information passed in
829 * really pushes log tail further. It's responsibility of the caller to make
830 * sure provided log tail information is valid (e.g. by holding
831 * j_checkpoint_mutex all the time between computing log tail and calling this
832 * function as is the case with jbd2_cleanup_journal_tail()).
833 *
834 * Requires j_checkpoint_mutex
835 */
836void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
837{
838 unsigned long freed;
839
840 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
841
842 /*
843 * We cannot afford for write to remain in drive's caches since as
844 * soon as we update j_tail, next transaction can start reusing journal
845 * space and if we lose sb update during power failure we'd replay
846 * old transaction with possibly newly overwritten data.
847 */
848 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
849 write_lock(&journal->j_state_lock);
850 freed = block - journal->j_tail;
851 if (block < journal->j_tail)
852 freed += journal->j_last - journal->j_first;
853
854 trace_jbd2_update_log_tail(journal, tid, block, freed);
855 jbd_debug(1,
856 "Cleaning journal tail from %d to %d (offset %lu), "
857 "freeing %lu\n",
858 journal->j_tail_sequence, tid, block, freed);
859
860 journal->j_free += freed;
861 journal->j_tail_sequence = tid;
862 journal->j_tail = block;
863 write_unlock(&journal->j_state_lock);
864}
865
Jan Kara33395782012-03-13 22:45:38 -0400866/*
867 * This is a variaon of __jbd2_update_log_tail which checks for validity of
868 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
869 * with other threads updating log tail.
870 */
871void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
872{
873 mutex_lock(&journal->j_checkpoint_mutex);
874 if (tid_gt(tid, journal->j_tail_sequence))
875 __jbd2_update_log_tail(journal, tid, block);
876 mutex_unlock(&journal->j_checkpoint_mutex);
877}
878
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500879struct jbd2_stats_proc_session {
880 journal_t *journal;
881 struct transaction_stats_s *stats;
882 int start;
883 int max;
884};
885
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500886static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
887{
888 return *pos ? NULL : SEQ_START_TOKEN;
889}
890
891static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
892{
893 return NULL;
894}
895
896static int jbd2_seq_info_show(struct seq_file *seq, void *v)
897{
898 struct jbd2_stats_proc_session *s = seq->private;
899
900 if (v != SEQ_START_TOKEN)
901 return 0;
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500902 seq_printf(seq, "%lu transactions (%lu requested), "
903 "each up to %u blocks\n",
904 s->stats->ts_tid, s->stats->ts_requested,
905 s->journal->j_max_transaction_buffers);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500906 if (s->stats->ts_tid == 0)
907 return 0;
908 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400909 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500910 seq_printf(seq, " %ums request delay\n",
911 (s->stats->ts_requested == 0) ? 0 :
912 jiffies_to_msecs(s->stats->run.rs_request_delay /
913 s->stats->ts_requested));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500914 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400915 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500916 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400917 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500918 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400919 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500920 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400921 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500922 seq_printf(seq, " %lluus average transaction commit time\n",
923 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500924 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400925 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500926 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400927 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500928 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400929 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500930 return 0;
931}
932
933static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
934{
935}
936
James Morris88e9d342009-09-22 16:43:43 -0700937static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500938 .start = jbd2_seq_info_start,
939 .next = jbd2_seq_info_next,
940 .stop = jbd2_seq_info_stop,
941 .show = jbd2_seq_info_show,
942};
943
944static int jbd2_seq_info_open(struct inode *inode, struct file *file)
945{
946 journal_t *journal = PDE(inode)->data;
947 struct jbd2_stats_proc_session *s;
948 int rc, size;
949
950 s = kmalloc(sizeof(*s), GFP_KERNEL);
951 if (s == NULL)
952 return -ENOMEM;
953 size = sizeof(struct transaction_stats_s);
954 s->stats = kmalloc(size, GFP_KERNEL);
955 if (s->stats == NULL) {
956 kfree(s);
957 return -ENOMEM;
958 }
959 spin_lock(&journal->j_history_lock);
960 memcpy(s->stats, &journal->j_stats, size);
961 s->journal = journal;
962 spin_unlock(&journal->j_history_lock);
963
964 rc = seq_open(file, &jbd2_seq_info_ops);
965 if (rc == 0) {
966 struct seq_file *m = file->private_data;
967 m->private = s;
968 } else {
969 kfree(s->stats);
970 kfree(s);
971 }
972 return rc;
973
974}
975
976static int jbd2_seq_info_release(struct inode *inode, struct file *file)
977{
978 struct seq_file *seq = file->private_data;
979 struct jbd2_stats_proc_session *s = seq->private;
980 kfree(s->stats);
981 kfree(s);
982 return seq_release(inode, file);
983}
984
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700985static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500986 .owner = THIS_MODULE,
987 .open = jbd2_seq_info_open,
988 .read = seq_read,
989 .llseek = seq_lseek,
990 .release = jbd2_seq_info_release,
991};
992
993static struct proc_dir_entry *proc_jbd2_stats;
994
995static void jbd2_stats_proc_init(journal_t *journal)
996{
Theodore Ts'o05496762008-09-16 14:36:17 -0400997 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500998 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700999 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1000 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001001 }
1002}
1003
1004static void jbd2_stats_proc_exit(journal_t *journal)
1005{
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001006 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -04001007 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001008}
1009
Dave Kleikamp470decc2006-10-11 01:20:57 -07001010/*
1011 * Management for journal control blocks: functions to create and
1012 * destroy journal_t structures, and to initialise and read existing
1013 * journal blocks from disk. */
1014
1015/* First: create and setup a journal_t object in memory. We initialise
1016 * very few fields yet: that has to wait until we have created the
1017 * journal structures from from scratch, or loaded them from disk. */
1018
1019static journal_t * journal_init_common (void)
1020{
1021 journal_t *journal;
1022 int err;
1023
Andrew Morton3ebfdf82009-12-23 08:05:15 -05001024 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001025 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001026 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001027
1028 init_waitqueue_head(&journal->j_wait_transaction_locked);
1029 init_waitqueue_head(&journal->j_wait_logspace);
1030 init_waitqueue_head(&journal->j_wait_done_commit);
1031 init_waitqueue_head(&journal->j_wait_checkpoint);
1032 init_waitqueue_head(&journal->j_wait_commit);
1033 init_waitqueue_head(&journal->j_wait_updates);
1034 mutex_init(&journal->j_barrier);
1035 mutex_init(&journal->j_checkpoint_mutex);
1036 spin_lock_init(&journal->j_revoke_lock);
1037 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001038 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001039
Mingming Caocd02ff02007-10-16 18:38:25 -04001040 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -05001041 journal->j_min_batch_time = 0;
1042 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043
1044 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001045 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046
1047 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001048 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049 if (err) {
1050 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001051 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001052 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001053
Theodore Ts'obf699322009-09-30 00:32:06 -04001054 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001055
Dave Kleikamp470decc2006-10-11 01:20:57 -07001056 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001057}
1058
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001059/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001060 *
1061 * Create a journal structure assigned some fixed set of disk blocks to
1062 * the journal. We don't actually touch those disk blocks yet, but we
1063 * need to set up all of the mapping information to tell the journaling
1064 * system where the journal blocks are.
1065 *
1066 */
1067
1068/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001069 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070 * @bdev: Block device on which to create the journal
1071 * @fs_dev: Device which hold journalled filesystem for this journal.
1072 * @start: Block nr Start of journal.
1073 * @len: Length of the journal in blocks.
1074 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001075 *
1076 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001077 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001078 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079 * range of blocks on an arbitrary block device.
1080 *
1081 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001082journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001083 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001084 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001085{
1086 journal_t *journal = journal_init_common();
1087 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001088 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001089 int n;
1090
1091 if (!journal)
1092 return NULL;
1093
1094 /* journal descriptor can store up to n blocks -bzzz */
1095 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001096 journal->j_dev = bdev;
1097 journal->j_fs_dev = fs_dev;
1098 journal->j_blk_offset = start;
1099 journal->j_maxlen = len;
1100 bdevname(journal->j_dev, journal->j_devname);
1101 p = journal->j_devname;
1102 while ((p = strchr(p, '/')))
1103 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001104 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001105 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1106 journal->j_wbufsize = n;
1107 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1108 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001109 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001110 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001111 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001112 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113
1114 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001115 if (!bh) {
1116 printk(KERN_ERR
1117 "%s: Cannot get buffer for journal superblock\n",
1118 __func__);
1119 goto out_err;
1120 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121 journal->j_sb_buffer = bh;
1122 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001123
Dave Kleikamp470decc2006-10-11 01:20:57 -07001124 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001125out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001126 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001127 jbd2_stats_proc_exit(journal);
1128 kfree(journal);
1129 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001130}
1131
1132/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001133 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001134 * @inode: An inode to create the journal in
1135 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001136 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001137 * the journal. The inode must exist already, must support bmap() and
1138 * must have all data blocks preallocated.
1139 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001140journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001141{
1142 struct buffer_head *bh;
1143 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001144 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001145 int err;
1146 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001147 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001148
1149 if (!journal)
1150 return NULL;
1151
1152 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1153 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001154 bdevname(journal->j_dev, journal->j_devname);
1155 p = journal->j_devname;
1156 while ((p = strchr(p, '/')))
1157 *p = '!';
1158 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001159 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160 jbd_debug(1,
1161 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1162 journal, inode->i_sb->s_id, inode->i_ino,
1163 (long long) inode->i_size,
1164 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1165
1166 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1167 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001168 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169
1170 /* journal descriptor can store up to n blocks -bzzz */
1171 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1172 journal->j_wbufsize = n;
1173 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1174 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001175 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001176 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001177 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001178 }
1179
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 /* If that failed, give up */
1182 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001183 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001184 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001185 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001186 }
1187
1188 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001189 if (!bh) {
1190 printk(KERN_ERR
1191 "%s: Cannot get buffer for journal superblock\n",
1192 __func__);
1193 goto out_err;
1194 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001195 journal->j_sb_buffer = bh;
1196 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1197
1198 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001199out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001200 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001201 jbd2_stats_proc_exit(journal);
1202 kfree(journal);
1203 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001204}
1205
1206/*
1207 * If the journal init or create aborts, we need to mark the journal
1208 * superblock as being NULL to prevent the journal destroy from writing
1209 * back a bogus superblock.
1210 */
1211static void journal_fail_superblock (journal_t *journal)
1212{
1213 struct buffer_head *bh = journal->j_sb_buffer;
1214 brelse(bh);
1215 journal->j_sb_buffer = NULL;
1216}
1217
1218/*
1219 * Given a journal_t structure, initialise the various fields for
1220 * startup of a new journaling session. We use this both when creating
1221 * a journal, and after recovering an old journal to reset it for
1222 * subsequent use.
1223 */
1224
1225static int journal_reset(journal_t *journal)
1226{
1227 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001228 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001229
1230 first = be32_to_cpu(sb->s_first);
1231 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001232 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001233 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001234 first, last);
1235 journal_fail_superblock(journal);
1236 return -EINVAL;
1237 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001238
1239 journal->j_first = first;
1240 journal->j_last = last;
1241
1242 journal->j_head = first;
1243 journal->j_tail = first;
1244 journal->j_free = last - first;
1245
1246 journal->j_tail_sequence = journal->j_transaction_sequence;
1247 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1248 journal->j_commit_request = journal->j_commit_sequence;
1249
1250 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1251
Dave Kleikamp470decc2006-10-11 01:20:57 -07001252 /*
1253 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001254 * no recovery (s_start == 0), then we can safely defer the superblock
1255 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001256 * attempting a write to a potential-readonly device.
1257 */
Jan Kara24bcc892012-03-13 15:41:04 -04001258 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001259 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001260 "(start %ld, seq %d, errno %d)\n",
1261 journal->j_tail, journal->j_tail_sequence,
1262 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001263 journal->j_flags |= JBD2_FLUSHED;
1264 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001265 /* Lock here to make assertions happy... */
1266 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001267 /*
1268 * Update log tail information. We use WRITE_FUA since new
1269 * transaction will start reusing journal space and so we
1270 * must make sure information about current log tail is on
1271 * disk before that.
1272 */
1273 jbd2_journal_update_sb_log_tail(journal,
1274 journal->j_tail_sequence,
1275 journal->j_tail,
1276 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001277 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001278 }
Jan Kara24bcc892012-03-13 15:41:04 -04001279 return jbd2_journal_start_thread(journal);
1280}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001281
Jan Kara79feb522012-03-13 22:22:54 -04001282static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001283{
1284 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001285 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001286
Jan Kara79feb522012-03-13 22:22:54 -04001287 trace_jbd2_write_superblock(journal, write_op);
1288 if (!(journal->j_flags & JBD2_BARRIER))
1289 write_op &= ~(REQ_FUA | REQ_FLUSH);
1290 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001291 if (buffer_write_io_error(bh)) {
1292 /*
1293 * Oh, dear. A previous attempt to write the journal
1294 * superblock failed. This could happen because the
1295 * USB device was yanked out. Or it could happen to
1296 * be a transient write error and maybe the block will
1297 * be remapped. Nothing we can do but to retry the
1298 * write and hope for the best.
1299 */
1300 printk(KERN_ERR "JBD2: previous I/O error detected "
1301 "for journal superblock update for %s.\n",
1302 journal->j_devname);
1303 clear_buffer_write_io_error(bh);
1304 set_buffer_uptodate(bh);
1305 }
Jan Kara79feb522012-03-13 22:22:54 -04001306 get_bh(bh);
1307 bh->b_end_io = end_buffer_write_sync;
1308 ret = submit_bh(write_op, bh);
1309 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001310 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001311 clear_buffer_write_io_error(bh);
1312 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001313 ret = -EIO;
1314 }
1315 if (ret) {
1316 printk(KERN_ERR "JBD2: Error %d detected when updating "
1317 "journal superblock for %s.\n", ret,
1318 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001319 }
1320}
Theodore Ts'o914258b2008-10-06 21:35:40 -04001321
Jan Kara24bcc892012-03-13 15:41:04 -04001322/**
1323 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1324 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001325 * @tail_tid: TID of the new transaction at the tail of the log
1326 * @tail_block: The first block of the transaction at the tail of the log
1327 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001328 *
1329 * Update a journal's superblock information about log tail and write it to
1330 * disk, waiting for the IO to complete.
1331 */
Jan Kara79feb522012-03-13 22:22:54 -04001332void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1333 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001334{
1335 journal_superblock_t *sb = journal->j_superblock;
1336
Jan Karaa78bb112012-03-13 15:43:04 -04001337 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001338 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1339 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001340
Jan Kara79feb522012-03-13 22:22:54 -04001341 sb->s_sequence = cpu_to_be32(tail_tid);
1342 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001343
Jan Kara79feb522012-03-13 22:22:54 -04001344 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001345
1346 /* Log is no longer empty */
1347 write_lock(&journal->j_state_lock);
1348 WARN_ON(!sb->s_sequence);
1349 journal->j_flags &= ~JBD2_FLUSHED;
1350 write_unlock(&journal->j_state_lock);
1351}
1352
1353/**
1354 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1355 * @journal: The journal to update.
1356 *
1357 * Update a journal's dynamic superblock fields to show that journal is empty.
1358 * Write updated superblock to disk waiting for IO to complete.
1359 */
1360static void jbd2_mark_journal_empty(journal_t *journal)
1361{
1362 journal_superblock_t *sb = journal->j_superblock;
1363
Jan Karaa78bb112012-03-13 15:43:04 -04001364 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001365 read_lock(&journal->j_state_lock);
Eric Sandeeneeecef02012-08-18 22:29:40 -04001366 /* Is it already empty? */
1367 if (sb->s_start == 0) {
1368 read_unlock(&journal->j_state_lock);
1369 return;
1370 }
Jan Kara24bcc892012-03-13 15:41:04 -04001371 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1372 journal->j_tail_sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001373
1374 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
Jan Kara24bcc892012-03-13 15:41:04 -04001375 sb->s_start = cpu_to_be32(0);
1376 read_unlock(&journal->j_state_lock);
1377
Jan Kara79feb522012-03-13 22:22:54 -04001378 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001379
1380 /* Log is no longer empty */
1381 write_lock(&journal->j_state_lock);
1382 journal->j_flags |= JBD2_FLUSHED;
1383 write_unlock(&journal->j_state_lock);
1384}
1385
1386
1387/**
1388 * jbd2_journal_update_sb_errno() - Update error in the journal.
1389 * @journal: The journal to update.
1390 *
1391 * Update a journal's errno. Write updated superblock to disk waiting for IO
1392 * to complete.
1393 */
Theodore Ts'od796c522012-08-05 19:04:57 -04001394void jbd2_journal_update_sb_errno(journal_t *journal)
Jan Kara24bcc892012-03-13 15:41:04 -04001395{
1396 journal_superblock_t *sb = journal->j_superblock;
1397
1398 read_lock(&journal->j_state_lock);
1399 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1400 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001401 sb->s_errno = cpu_to_be32(journal->j_errno);
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001402 jbd2_superblock_csum_set(journal, sb);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001403 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001404
Jan Kara79feb522012-03-13 22:22:54 -04001405 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406}
Theodore Ts'od796c522012-08-05 19:04:57 -04001407EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408
1409/*
1410 * Read the superblock for a given journal, performing initial
1411 * validation of the format.
1412 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001413static int journal_get_superblock(journal_t *journal)
1414{
1415 struct buffer_head *bh;
1416 journal_superblock_t *sb;
1417 int err = -EIO;
1418
1419 bh = journal->j_sb_buffer;
1420
1421 J_ASSERT(bh != NULL);
1422 if (!buffer_uptodate(bh)) {
1423 ll_rw_block(READ, 1, &bh);
1424 wait_on_buffer(bh);
1425 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001426 printk(KERN_ERR
1427 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428 goto out;
1429 }
1430 }
1431
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001432 if (buffer_verified(bh))
1433 return 0;
1434
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435 sb = journal->j_superblock;
1436
1437 err = -EINVAL;
1438
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001439 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001441 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001442 goto out;
1443 }
1444
1445 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001446 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001447 journal->j_format_version = 1;
1448 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001449 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001450 journal->j_format_version = 2;
1451 break;
1452 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001453 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001454 goto out;
1455 }
1456
1457 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1458 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1459 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001460 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001461 goto out;
1462 }
1463
Eryu Guan87622022011-11-01 19:04:59 -04001464 if (be32_to_cpu(sb->s_first) == 0 ||
1465 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1466 printk(KERN_WARNING
1467 "JBD2: Invalid start block of journal: %u\n",
1468 be32_to_cpu(sb->s_first));
1469 goto out;
1470 }
1471
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001472 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1473 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1474 /* Can't have checksum v1 and v2 on at the same time! */
1475 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1476 "at the same time!\n");
1477 goto out;
1478 }
1479
1480 if (!jbd2_verify_csum_type(journal, sb)) {
1481 printk(KERN_ERR "JBD: Unknown checksum type\n");
1482 goto out;
1483 }
1484
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001485 /* Load the checksum driver */
1486 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1487 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1488 if (IS_ERR(journal->j_chksum_driver)) {
1489 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1490 err = PTR_ERR(journal->j_chksum_driver);
1491 journal->j_chksum_driver = NULL;
1492 goto out;
1493 }
1494 }
1495
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001496 /* Check superblock checksum */
1497 if (!jbd2_superblock_csum_verify(journal, sb)) {
1498 printk(KERN_ERR "JBD: journal checksum error\n");
1499 goto out;
1500 }
1501
1502 /* Precompute checksum seed for all metadata */
1503 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1504 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1505 sizeof(sb->s_uuid));
1506
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001507 set_buffer_verified(bh);
1508
Dave Kleikamp470decc2006-10-11 01:20:57 -07001509 return 0;
1510
1511out:
1512 journal_fail_superblock(journal);
1513 return err;
1514}
1515
1516/*
1517 * Load the on-disk journal superblock and read the key fields into the
1518 * journal_t.
1519 */
1520
1521static int load_superblock(journal_t *journal)
1522{
1523 int err;
1524 journal_superblock_t *sb;
1525
1526 err = journal_get_superblock(journal);
1527 if (err)
1528 return err;
1529
1530 sb = journal->j_superblock;
1531
1532 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1533 journal->j_tail = be32_to_cpu(sb->s_start);
1534 journal->j_first = be32_to_cpu(sb->s_first);
1535 journal->j_last = be32_to_cpu(sb->s_maxlen);
1536 journal->j_errno = be32_to_cpu(sb->s_errno);
1537
1538 return 0;
1539}
1540
1541
1542/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001543 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001544 * @journal: Journal to act on.
1545 *
1546 * Given a journal_t structure which tells us which disk blocks contain
1547 * a journal, read the journal from disk to initialise the in-memory
1548 * structures.
1549 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001550int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001551{
1552 int err;
1553 journal_superblock_t *sb;
1554
1555 err = load_superblock(journal);
1556 if (err)
1557 return err;
1558
1559 sb = journal->j_superblock;
1560 /* If this is a V2 superblock, then we have to check the
1561 * features flags on it. */
1562
1563 if (journal->j_format_version >= 2) {
1564 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001565 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001566 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001568 printk(KERN_WARNING
1569 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 return -EINVAL;
1571 }
1572 }
1573
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001574 /*
1575 * Create a slab for this blocksize
1576 */
1577 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1578 if (err)
1579 return err;
1580
Dave Kleikamp470decc2006-10-11 01:20:57 -07001581 /* Let the recovery code check whether it needs to recover any
1582 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001583 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001584 goto recovery_error;
1585
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001586 if (journal->j_failed_commit) {
1587 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1588 "is corrupt.\n", journal->j_failed_commit,
1589 journal->j_devname);
1590 return -EIO;
1591 }
1592
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593 /* OK, we've finished with the dynamic journal bits:
1594 * reinitialise the dynamic contents of the superblock in memory
1595 * and reset them on disk. */
1596 if (journal_reset(journal))
1597 goto recovery_error;
1598
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001599 journal->j_flags &= ~JBD2_ABORT;
1600 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 return 0;
1602
1603recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001604 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001605 return -EIO;
1606}
1607
1608/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001609 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001610 * @journal: Journal to act on.
1611 *
1612 * Release a journal_t structure once it is no longer in use by the
1613 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001614 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001615 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001616int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001618 int err = 0;
1619
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620 /* Wait for the commit thread to wake up and die. */
1621 journal_kill_thread(journal);
1622
1623 /* Force a final log commit */
1624 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001625 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001626
1627 /* Force any old transactions to disk */
1628
1629 /* Totally anal locking here... */
1630 spin_lock(&journal->j_list_lock);
1631 while (journal->j_checkpoint_transactions != NULL) {
1632 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001633 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001634 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001635 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001636 spin_lock(&journal->j_list_lock);
1637 }
1638
1639 J_ASSERT(journal->j_running_transaction == NULL);
1640 J_ASSERT(journal->j_committing_transaction == NULL);
1641 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1642 spin_unlock(&journal->j_list_lock);
1643
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001645 if (!is_journal_aborted(journal)) {
Jan Karaa78bb112012-03-13 15:43:04 -04001646 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001647 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001648 mutex_unlock(&journal->j_checkpoint_mutex);
1649 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001650 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001651 brelse(journal->j_sb_buffer);
1652 }
1653
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001654 if (journal->j_proc_entry)
1655 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001656 if (journal->j_inode)
1657 iput(journal->j_inode);
1658 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001659 jbd2_journal_destroy_revoke(journal);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001660 if (journal->j_chksum_driver)
1661 crypto_free_shash(journal->j_chksum_driver);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001662 kfree(journal->j_wbuf);
1663 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001664
1665 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666}
1667
1668
1669/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001670 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671 * @journal: Journal to check.
1672 * @compat: bitmask of compatible features
1673 * @ro: bitmask of features that force read-only mount
1674 * @incompat: bitmask of incompatible features
1675 *
1676 * Check whether the journal uses all of a given set of
1677 * features. Return true (non-zero) if it does.
1678 **/
1679
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001680int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001681 unsigned long ro, unsigned long incompat)
1682{
1683 journal_superblock_t *sb;
1684
1685 if (!compat && !ro && !incompat)
1686 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001687 /* Load journal superblock if it is not loaded yet. */
1688 if (journal->j_format_version == 0 &&
1689 journal_get_superblock(journal) != 0)
1690 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001691 if (journal->j_format_version == 1)
1692 return 0;
1693
1694 sb = journal->j_superblock;
1695
1696 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1697 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1698 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1699 return 1;
1700
1701 return 0;
1702}
1703
1704/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001705 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001706 * @journal: Journal to check.
1707 * @compat: bitmask of compatible features
1708 * @ro: bitmask of features that force read-only mount
1709 * @incompat: bitmask of incompatible features
1710 *
1711 * Check whether the journaling code supports the use of
1712 * all of a given set of features on this journal. Return true
1713 * (non-zero) if it can. */
1714
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001715int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001716 unsigned long ro, unsigned long incompat)
1717{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718 if (!compat && !ro && !incompat)
1719 return 1;
1720
Dave Kleikamp470decc2006-10-11 01:20:57 -07001721 /* We can support any known requested features iff the
1722 * superblock is in version 2. Otherwise we fail to support any
1723 * extended sb features. */
1724
1725 if (journal->j_format_version != 2)
1726 return 0;
1727
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001728 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1729 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1730 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001731 return 1;
1732
1733 return 0;
1734}
1735
1736/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001737 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001738 * @journal: Journal to act on.
1739 * @compat: bitmask of compatible features
1740 * @ro: bitmask of features that force read-only mount
1741 * @incompat: bitmask of incompatible features
1742 *
1743 * Mark a given journal feature as present on the
1744 * superblock. Returns true if the requested features could be set.
1745 *
1746 */
1747
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001748int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 unsigned long ro, unsigned long incompat)
1750{
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001751#define INCOMPAT_FEATURE_ON(f) \
1752 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1753#define COMPAT_FEATURE_ON(f) \
1754 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001755 journal_superblock_t *sb;
1756
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001757 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001758 return 1;
1759
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001760 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001761 return 0;
1762
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001763 /* Asking for checksumming v2 and v1? Only give them v2. */
1764 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1765 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1766 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1767
Dave Kleikamp470decc2006-10-11 01:20:57 -07001768 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1769 compat, ro, incompat);
1770
1771 sb = journal->j_superblock;
1772
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001773 /* If enabling v2 checksums, update superblock */
1774 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1775 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1776 sb->s_feature_compat &=
1777 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001778
1779 /* Load the checksum driver */
1780 if (journal->j_chksum_driver == NULL) {
1781 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1782 0, 0);
1783 if (IS_ERR(journal->j_chksum_driver)) {
1784 printk(KERN_ERR "JBD: Cannot load crc32c "
1785 "driver.\n");
1786 journal->j_chksum_driver = NULL;
1787 return 0;
1788 }
1789 }
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001790
1791 /* Precompute checksum seed for all metadata */
1792 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1793 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1794 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1795 sb->s_uuid,
1796 sizeof(sb->s_uuid));
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001797 }
1798
1799 /* If enabling v1 checksums, downgrade superblock */
1800 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1801 sb->s_feature_incompat &=
1802 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1803
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804 sb->s_feature_compat |= cpu_to_be32(compat);
1805 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1806 sb->s_feature_incompat |= cpu_to_be32(incompat);
1807
1808 return 1;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001809#undef COMPAT_FEATURE_ON
1810#undef INCOMPAT_FEATURE_ON
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811}
1812
Girish Shilamkar818d2762008-01-28 23:58:27 -05001813/*
1814 * jbd2_journal_clear_features () - Clear a given journal feature in the
1815 * superblock
1816 * @journal: Journal to act on.
1817 * @compat: bitmask of compatible features
1818 * @ro: bitmask of features that force read-only mount
1819 * @incompat: bitmask of incompatible features
1820 *
1821 * Clear a given journal feature as present on the
1822 * superblock.
1823 */
1824void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1825 unsigned long ro, unsigned long incompat)
1826{
1827 journal_superblock_t *sb;
1828
1829 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1830 compat, ro, incompat);
1831
1832 sb = journal->j_superblock;
1833
1834 sb->s_feature_compat &= ~cpu_to_be32(compat);
1835 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1836 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1837}
1838EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001839
1840/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001841 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 * @journal: Journal to act on.
1843 *
1844 * Flush all data for a given journal to disk and empty the journal.
1845 * Filesystems can use this when remounting readonly to ensure that
1846 * recovery does not need to happen on remount.
1847 */
1848
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001849int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001850{
1851 int err = 0;
1852 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853
Theodore Ts'oa931da62010-08-03 21:35:12 -04001854 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855
1856 /* Force everything buffered to the log... */
1857 if (journal->j_running_transaction) {
1858 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001859 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001860 } else if (journal->j_committing_transaction)
1861 transaction = journal->j_committing_transaction;
1862
1863 /* Wait for the log commit to complete... */
1864 if (transaction) {
1865 tid_t tid = transaction->t_tid;
1866
Theodore Ts'oa931da62010-08-03 21:35:12 -04001867 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001868 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001869 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001870 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001871 }
1872
1873 /* ...and flush everything in the log out to disk. */
1874 spin_lock(&journal->j_list_lock);
1875 while (!err && journal->j_checkpoint_transactions != NULL) {
1876 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001877 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001878 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001879 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880 spin_lock(&journal->j_list_lock);
1881 }
1882 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001883
1884 if (is_journal_aborted(journal))
1885 return -EIO;
1886
Jan Karaa78bb112012-03-13 15:43:04 -04001887 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001888 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889
1890 /* Finally, mark the journal as really needing no recovery.
1891 * This sets s_start==0 in the underlying superblock, which is
1892 * the magic code for a fully-recovered superblock. Any future
1893 * commits of data to the journal will restore the current
1894 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001895 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001896 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001897 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001898 J_ASSERT(!journal->j_running_transaction);
1899 J_ASSERT(!journal->j_committing_transaction);
1900 J_ASSERT(!journal->j_checkpoint_transactions);
1901 J_ASSERT(journal->j_head == journal->j_tail);
1902 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001903 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001904 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001905}
1906
1907/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001908 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001909 * @journal: Journal to act on.
1910 * @write: flag (see below)
1911 *
1912 * Wipe out all of the contents of a journal, safely. This will produce
1913 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001914 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001915 *
1916 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1917 * we merely suppress recovery.
1918 */
1919
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001920int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001921{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001922 int err = 0;
1923
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001924 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925
1926 err = load_superblock(journal);
1927 if (err)
1928 return err;
1929
Dave Kleikamp470decc2006-10-11 01:20:57 -07001930 if (!journal->j_tail)
1931 goto no_recovery;
1932
Eryu Guanf2a44522011-11-01 19:09:18 -04001933 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934 write ? "Clearing" : "Ignoring");
1935
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001936 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001937 if (write) {
1938 /* Lock to make assertions happy... */
1939 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001940 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001941 mutex_unlock(&journal->j_checkpoint_mutex);
1942 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001943
1944 no_recovery:
1945 return err;
1946}
1947
1948/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001949 * Journal abort has very specific semantics, which we describe
1950 * for journal abort.
1951 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001952 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001953 * itself are here.
1954 */
1955
1956/*
1957 * Quick version for internal journal use (doesn't lock the journal).
1958 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1959 * and don't attempt to make any other journal updates.
1960 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001961void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962{
1963 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001965 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001966 return;
1967
1968 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001969 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001970
Theodore Ts'oa931da62010-08-03 21:35:12 -04001971 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001972 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973 transaction = journal->j_running_transaction;
1974 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001975 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001976 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001977}
1978
1979/* Soft abort: record the abort error status in the journal superblock,
1980 * but don't do any other IO. */
1981static void __journal_abort_soft (journal_t *journal, int errno)
1982{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001983 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984 return;
1985
1986 if (!journal->j_errno)
1987 journal->j_errno = errno;
1988
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001989 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001990
1991 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04001992 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001993}
1994
1995/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001996 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 * @journal: the journal to shutdown.
1998 * @errno: an error number to record in the journal indicating
1999 * the reason for the shutdown.
2000 *
2001 * Perform a complete, immediate shutdown of the ENTIRE
2002 * journal (not of a single transaction). This operation cannot be
2003 * undone without closing and reopening the journal.
2004 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002005 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07002006 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2007 * mode.
2008 *
2009 * Journal abort has very specific semantics. Any existing dirty,
2010 * unjournaled buffers in the main filesystem will still be written to
2011 * disk by bdflush, but the journaling mechanism will be suspended
2012 * immediately and no further transaction commits will be honoured.
2013 *
2014 * Any dirty, journaled buffers will be written back to disk without
2015 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2016 * filesystem, but we _do_ attempt to leave as much data as possible
2017 * behind for fsck to use for cleanup.
2018 *
2019 * Any attempt to get a new transaction handle on a journal which is in
2020 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002021 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022 * entered abort state during the update.
2023 *
2024 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002025 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002026 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028 * which will be recorded (if possible) in the journal superblock. This
2029 * allows a client to record failure conditions in the middle of a
2030 * transaction without having to complete the transaction to record the
2031 * failure to disk. ext3_error, for example, now uses this
2032 * functionality.
2033 *
2034 * Errors which originate from within the journaling layer will NOT
2035 * supply an errno; a null errno implies that absolutely no further
2036 * writes are done to the journal (unless there are any already in
2037 * progress).
2038 *
2039 */
2040
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042{
2043 __journal_abort_soft(journal, errno);
2044}
2045
2046/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002047 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002048 * @journal: journal to examine.
2049 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002050 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07002051 * time the journal was mounted - if the journal was stopped
2052 * without calling abort this will be 0.
2053 *
2054 * If the journal has been aborted on this mount time -EROFS will
2055 * be returned.
2056 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002057int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058{
2059 int err;
2060
Theodore Ts'oa931da62010-08-03 21:35:12 -04002061 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002062 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002063 err = -EROFS;
2064 else
2065 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002066 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002067 return err;
2068}
2069
2070/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002071 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07002072 * @journal: journal to act on.
2073 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002074 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002075 * mode.
2076 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002077int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002078{
2079 int err = 0;
2080
Theodore Ts'oa931da62010-08-03 21:35:12 -04002081 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002082 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002083 err = -EROFS;
2084 else
2085 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002086 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002087 return err;
2088}
2089
2090/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002091 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002092 * @journal: journal to act on.
2093 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002094 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002095 * mode.
2096 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002097void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002098{
Theodore Ts'oa931da62010-08-03 21:35:12 -04002099 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002101 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002102 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002103}
2104
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002105int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002106{
2107 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2108}
2109
2110/*
Zach Brownb517bea2006-10-11 01:21:08 -07002111 * helper functions to deal with 32 or 64bit block numbers.
2112 */
2113size_t journal_tag_bytes(journal_t *journal)
2114{
Darrick J. Wongc3900872012-05-27 08:12:12 -04002115 journal_block_tag_t tag;
2116 size_t x = 0;
2117
2118 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2119 x += sizeof(tag.t_checksum);
2120
Zach Brownb517bea2006-10-11 01:21:08 -07002121 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Darrick J. Wongc3900872012-05-27 08:12:12 -04002122 return x + JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07002123 else
Darrick J. Wongc3900872012-05-27 08:12:12 -04002124 return x + JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07002125}
2126
2127/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002128 * JBD memory management
2129 *
2130 * These functions are used to allocate block-sized chunks of memory
2131 * used for making copies of buffer_head data. Very often it will be
2132 * page-sized chunks of data, but sometimes it will be in
2133 * sub-page-size chunks. (For example, 16k pages on Power systems
2134 * with a 4k block file system.) For blocks smaller than a page, we
2135 * use a SLAB allocator. There are slab caches for each block size,
2136 * which are allocated at mount time, if necessary, and we only free
2137 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2138 * this reason we don't need to a mutex to protect access to
2139 * jbd2_slab[] allocating or releasing memory; only in
2140 * jbd2_journal_create_slab().
2141 */
2142#define JBD2_MAX_SLABS 8
2143static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002144
2145static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2146 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2147 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2148};
2149
2150
2151static void jbd2_journal_destroy_slabs(void)
2152{
2153 int i;
2154
2155 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2156 if (jbd2_slab[i])
2157 kmem_cache_destroy(jbd2_slab[i]);
2158 jbd2_slab[i] = NULL;
2159 }
2160}
2161
2162static int jbd2_journal_create_slab(size_t size)
2163{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002164 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002165 int i = order_base_2(size) - 10;
2166 size_t slab_size;
2167
2168 if (size == PAGE_SIZE)
2169 return 0;
2170
2171 if (i >= JBD2_MAX_SLABS)
2172 return -EINVAL;
2173
2174 if (unlikely(i < 0))
2175 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002176 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002177 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002178 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002179 return 0; /* Already created */
2180 }
2181
2182 slab_size = 1 << (i+10);
2183 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2184 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002185 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002186 if (!jbd2_slab[i]) {
2187 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2188 return -ENOMEM;
2189 }
2190 return 0;
2191}
2192
2193static struct kmem_cache *get_slab(size_t size)
2194{
2195 int i = order_base_2(size) - 10;
2196
2197 BUG_ON(i >= JBD2_MAX_SLABS);
2198 if (unlikely(i < 0))
2199 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002200 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002201 return jbd2_slab[i];
2202}
2203
2204void *jbd2_alloc(size_t size, gfp_t flags)
2205{
2206 void *ptr;
2207
2208 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2209
2210 flags |= __GFP_REPEAT;
2211 if (size == PAGE_SIZE)
2212 ptr = (void *)__get_free_pages(flags, 0);
2213 else if (size > PAGE_SIZE) {
2214 int order = get_order(size);
2215
2216 if (order < 3)
2217 ptr = (void *)__get_free_pages(flags, order);
2218 else
2219 ptr = vmalloc(size);
2220 } else
2221 ptr = kmem_cache_alloc(get_slab(size), flags);
2222
2223 /* Check alignment; SLUB has gotten this wrong in the past,
2224 * and this can lead to user data corruption! */
2225 BUG_ON(((unsigned long) ptr) & (size-1));
2226
2227 return ptr;
2228}
2229
2230void jbd2_free(void *ptr, size_t size)
2231{
2232 if (size == PAGE_SIZE) {
2233 free_pages((unsigned long)ptr, 0);
2234 return;
2235 }
2236 if (size > PAGE_SIZE) {
2237 int order = get_order(size);
2238
2239 if (order < 3)
2240 free_pages((unsigned long)ptr, order);
2241 else
2242 vfree(ptr);
2243 return;
2244 }
2245 kmem_cache_free(get_slab(size), ptr);
2246};
2247
2248/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002249 * Journal_head storage management
2250 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002251static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002252#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002253static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2254#endif
2255
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002256static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257{
2258 int retval;
2259
Al Viro1076d172008-03-29 03:07:18 +00002260 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002261 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002262 sizeof(struct journal_head),
2263 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002264 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002265 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002266 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002267 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002268 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002269 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002270 }
2271 return retval;
2272}
2273
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002274static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002275{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002276 if (jbd2_journal_head_cache) {
2277 kmem_cache_destroy(jbd2_journal_head_cache);
2278 jbd2_journal_head_cache = NULL;
2279 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002280}
2281
2282/*
2283 * journal_head splicing and dicing
2284 */
2285static struct journal_head *journal_alloc_journal_head(void)
2286{
2287 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288
Jose R. Santose23291b2007-07-18 08:57:06 -04002289#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002290 atomic_inc(&nr_journal_heads);
2291#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002292 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002293 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002294 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002295 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002296 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002297 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002298 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002299 }
2300 }
2301 return ret;
2302}
2303
2304static void journal_free_journal_head(struct journal_head *jh)
2305{
Jose R. Santose23291b2007-07-18 08:57:06 -04002306#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002307 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002308 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002309#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002310 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002311}
2312
2313/*
2314 * A journal_head is attached to a buffer_head whenever JBD has an
2315 * interest in the buffer.
2316 *
2317 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2318 * is set. This bit is tested in core kernel code where we need to take
2319 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2320 * there.
2321 *
2322 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2323 *
2324 * When a buffer has its BH_JBD bit set it is immune from being released by
2325 * core kernel code, mainly via ->b_count.
2326 *
Jan Karade1b7942011-06-13 15:38:22 -04002327 * A journal_head is detached from its buffer_head when the journal_head's
2328 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2329 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002330 *
2331 * Various places in the kernel want to attach a journal_head to a buffer_head
2332 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002333 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002334 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002335 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002336 *
2337 * So the typical usage would be:
2338 *
2339 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002342 * (Get another reference for transaction)
2343 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002344 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002345 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002346 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002347 */
2348
2349/*
2350 * Give a buffer_head a journal_head.
2351 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002352 * May sleep.
2353 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002354struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002355{
2356 struct journal_head *jh;
2357 struct journal_head *new_jh = NULL;
2358
2359repeat:
2360 if (!buffer_jbd(bh)) {
2361 new_jh = journal_alloc_journal_head();
2362 memset(new_jh, 0, sizeof(*new_jh));
2363 }
2364
2365 jbd_lock_bh_journal_head(bh);
2366 if (buffer_jbd(bh)) {
2367 jh = bh2jh(bh);
2368 } else {
2369 J_ASSERT_BH(bh,
2370 (atomic_read(&bh->b_count) > 0) ||
2371 (bh->b_page && bh->b_page->mapping));
2372
2373 if (!new_jh) {
2374 jbd_unlock_bh_journal_head(bh);
2375 goto repeat;
2376 }
2377
2378 jh = new_jh;
2379 new_jh = NULL; /* We consumed it */
2380 set_buffer_jbd(bh);
2381 bh->b_private = jh;
2382 jh->b_bh = bh;
2383 get_bh(bh);
2384 BUFFER_TRACE(bh, "added journal_head");
2385 }
2386 jh->b_jcount++;
2387 jbd_unlock_bh_journal_head(bh);
2388 if (new_jh)
2389 journal_free_journal_head(new_jh);
2390 return bh->b_private;
2391}
2392
2393/*
2394 * Grab a ref against this buffer_head's journal_head. If it ended up not
2395 * having a journal_head, return NULL
2396 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002397struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002398{
2399 struct journal_head *jh = NULL;
2400
2401 jbd_lock_bh_journal_head(bh);
2402 if (buffer_jbd(bh)) {
2403 jh = bh2jh(bh);
2404 jh->b_jcount++;
2405 }
2406 jbd_unlock_bh_journal_head(bh);
2407 return jh;
2408}
2409
2410static void __journal_remove_journal_head(struct buffer_head *bh)
2411{
2412 struct journal_head *jh = bh2jh(bh);
2413
2414 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002415 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2416 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2417 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2418 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2419 J_ASSERT_BH(bh, buffer_jbd(bh));
2420 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2421 BUFFER_TRACE(bh, "remove journal_head");
2422 if (jh->b_frozen_data) {
2423 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2424 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002425 }
Jan Karade1b7942011-06-13 15:38:22 -04002426 if (jh->b_committed_data) {
2427 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2428 jbd2_free(jh->b_committed_data, bh->b_size);
2429 }
2430 bh->b_private = NULL;
2431 jh->b_bh = NULL; /* debug, really */
2432 clear_buffer_jbd(bh);
2433 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002434}
2435
2436/*
Jan Karade1b7942011-06-13 15:38:22 -04002437 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002438 * release the journal_head from the buffer_head.
2439 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002440void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002441{
2442 struct buffer_head *bh = jh2bh(jh);
2443
2444 jbd_lock_bh_journal_head(bh);
2445 J_ASSERT_JH(jh, jh->b_jcount > 0);
2446 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002447 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002448 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002449 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002450 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002451 } else
2452 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002453}
2454
2455/*
Jan Karac851ed52008-07-11 19:27:31 -04002456 * Initialize jbd inode head
2457 */
2458void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2459{
2460 jinode->i_transaction = NULL;
2461 jinode->i_next_transaction = NULL;
2462 jinode->i_vfs_inode = inode;
2463 jinode->i_flags = 0;
2464 INIT_LIST_HEAD(&jinode->i_list);
2465}
2466
2467/*
2468 * Function to be called before we start removing inode from memory (i.e.,
2469 * clear_inode() is a fine place to be called from). It removes inode from
2470 * transaction's lists.
2471 */
2472void jbd2_journal_release_jbd_inode(journal_t *journal,
2473 struct jbd2_inode *jinode)
2474{
Jan Karac851ed52008-07-11 19:27:31 -04002475 if (!journal)
2476 return;
2477restart:
2478 spin_lock(&journal->j_list_lock);
2479 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002480 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002481 wait_queue_head_t *wq;
2482 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2483 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2484 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2485 spin_unlock(&journal->j_list_lock);
2486 schedule();
2487 finish_wait(wq, &wait.wait);
2488 goto restart;
2489 }
2490
Jan Karac851ed52008-07-11 19:27:31 -04002491 if (jinode->i_transaction) {
2492 list_del(&jinode->i_list);
2493 jinode->i_transaction = NULL;
2494 }
2495 spin_unlock(&journal->j_list_lock);
2496}
2497
2498/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002499 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002500 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002501#ifdef CONFIG_JBD2_DEBUG
2502u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002503EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002504
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002505#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002506
Jose R. Santos6f38c742007-10-16 18:38:25 -04002507static struct dentry *jbd2_debugfs_dir;
2508static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002509
2510static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002511{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002512 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2513 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002514 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2515 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002516 jbd2_debugfs_dir,
2517 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002518}
2519
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002520static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002521{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002522 debugfs_remove(jbd2_debug);
2523 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002524}
2525
2526#else
2527
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002528static void __init jbd2_create_debugfs_entry(void)
2529{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002530}
2531
2532static void __exit jbd2_remove_debugfs_entry(void)
2533{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002534}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002535
2536#endif
2537
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002538#ifdef CONFIG_PROC_FS
2539
2540#define JBD2_STATS_PROC_NAME "fs/jbd2"
2541
2542static void __init jbd2_create_jbd_stats_proc_entry(void)
2543{
2544 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2545}
2546
2547static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2548{
2549 if (proc_jbd2_stats)
2550 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2551}
2552
2553#else
2554
2555#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2556#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2557
2558#endif
2559
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002560struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002561
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002562static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002563{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002564 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002565 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002566 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2567 return -ENOMEM;
2568 }
2569 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2570 if (jbd2_inode_cache == NULL) {
2571 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2572 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002573 return -ENOMEM;
2574 }
2575 return 0;
2576}
2577
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002578static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002579{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002580 if (jbd2_handle_cache)
2581 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002582 if (jbd2_inode_cache)
2583 kmem_cache_destroy(jbd2_inode_cache);
2584
Dave Kleikamp470decc2006-10-11 01:20:57 -07002585}
2586
2587/*
2588 * Module startup and shutdown
2589 */
2590
2591static int __init journal_init_caches(void)
2592{
2593 int ret;
2594
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002595 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002596 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002597 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002598 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002599 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002600 if (ret == 0)
2601 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002602 return ret;
2603}
2604
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002605static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002606{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002607 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002608 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002609 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002610 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002611 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002612}
2613
2614static int __init journal_init(void)
2615{
2616 int ret;
2617
2618 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2619
2620 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002621 if (ret == 0) {
2622 jbd2_create_debugfs_entry();
2623 jbd2_create_jbd_stats_proc_entry();
2624 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002625 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002626 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002627 return ret;
2628}
2629
2630static void __exit journal_exit(void)
2631{
Jose R. Santose23291b2007-07-18 08:57:06 -04002632#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002633 int n = atomic_read(&nr_journal_heads);
2634 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002635 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002636#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002637 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002638 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002639 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002640}
2641
Dave Kleikamp470decc2006-10-11 01:20:57 -07002642MODULE_LICENSE("GPL");
2643module_init(journal_init);
2644module_exit(journal_exit);
2645