blob: 1a80e3146a59f0cba8435783b207a7aa16b96bae [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);
536 wake_up(&journal->j_wait_commit);
537 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400538 } else if (!tid_geq(journal->j_commit_request, target))
539 /* This should never happen, but if it does, preserve
540 the evidence before kjournald goes into a loop and
541 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400542 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400543 journal->j_commit_request,
544 journal->j_commit_sequence,
545 target, journal->j_running_transaction ?
546 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547 return 0;
548}
549
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700550int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551{
552 int ret;
553
Theodore Ts'oa931da62010-08-03 21:35:12 -0400554 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700555 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400556 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557 return ret;
558}
559
560/*
561 * Force and wait upon a commit if the calling process is not within
562 * transaction. This is used for forcing out undo-protected data which contains
563 * bitmaps, when the fs is running out of space.
564 *
565 * We can only force the running transaction if we don't have an active handle;
566 * otherwise, we will deadlock.
567 *
568 * Returns true if a transaction was started.
569 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700570int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700571{
572 transaction_t *transaction = NULL;
573 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500574 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700575
Theodore Ts'oa931da62010-08-03 21:35:12 -0400576 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700577 if (journal->j_running_transaction && !current->journal_info) {
578 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500579 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
580 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700581 } else if (journal->j_committing_transaction)
582 transaction = journal->j_committing_transaction;
583
584 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400585 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586 return 0; /* Nothing to retry */
587 }
588
589 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400590 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500591 if (need_to_start)
592 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700593 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594 return 1;
595}
596
597/*
598 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500599 * if a transaction is going to be committed (or is currently already
600 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700601 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700602int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700603{
604 int ret = 0;
605
Theodore Ts'oa931da62010-08-03 21:35:12 -0400606 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700607 if (journal->j_running_transaction) {
608 tid_t tid = journal->j_running_transaction->t_tid;
609
Jan Karac88ccea2009-02-10 11:27:46 -0500610 __jbd2_log_start_commit(journal, tid);
611 /* There's a running transaction and we've just made sure
612 * it's commit has been scheduled. */
613 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500615 ret = 1;
616 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700617 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300618 * If commit has been started, then we have to wait for
619 * completion of that transaction.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700620 */
Jan Karac88ccea2009-02-10 11:27:46 -0500621 if (ptid)
622 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623 ret = 1;
624 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400625 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700626 return ret;
627}
628
629/*
Jan Karabbd2be32011-05-24 11:59:18 -0400630 * Return 1 if a given transaction has not yet sent barrier request
631 * connected with a transaction commit. If 0 is returned, transaction
632 * may or may not have sent the barrier. Used to avoid sending barrier
633 * twice in common cases.
634 */
635int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
636{
637 int ret = 0;
638 transaction_t *commit_trans;
639
640 if (!(journal->j_flags & JBD2_BARRIER))
641 return 0;
642 read_lock(&journal->j_state_lock);
643 /* Transaction already committed? */
644 if (tid_geq(journal->j_commit_sequence, tid))
645 goto out;
646 commit_trans = journal->j_committing_transaction;
647 if (!commit_trans || commit_trans->t_tid != tid) {
648 ret = 1;
649 goto out;
650 }
651 /*
652 * Transaction is being committed and we already proceeded to
653 * submitting a flush to fs partition?
654 */
655 if (journal->j_fs_dev != journal->j_dev) {
656 if (!commit_trans->t_need_data_flush ||
657 commit_trans->t_state >= T_COMMIT_DFLUSH)
658 goto out;
659 } else {
660 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
661 goto out;
662 }
663 ret = 1;
664out:
665 read_unlock(&journal->j_state_lock);
666 return ret;
667}
668EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
669
670/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 * Wait for a specified commit to complete.
672 * The caller may not hold the journal lock.
673 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700674int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700675{
676 int err = 0;
677
Theodore Ts'oa931da62010-08-03 21:35:12 -0400678 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400679#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700680 if (!tid_geq(journal->j_commit_request, tid)) {
681 printk(KERN_EMERG
682 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400683 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700684 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700685#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700686 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400687 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700688 tid, journal->j_commit_sequence);
689 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400690 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700691 wait_event(journal->j_wait_done_commit,
692 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400693 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700694 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400695 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700696
697 if (unlikely(is_journal_aborted(journal))) {
698 printk(KERN_EMERG "journal commit I/O error\n");
699 err = -EIO;
700 }
701 return err;
702}
703
704/*
705 * Log buffer allocation routines:
706 */
707
Mingming Cao18eba7a2006-10-11 01:21:13 -0700708int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700709{
710 unsigned long blocknr;
711
Theodore Ts'oa931da62010-08-03 21:35:12 -0400712 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700713 J_ASSERT(journal->j_free > 1);
714
715 blocknr = journal->j_head;
716 journal->j_head++;
717 journal->j_free--;
718 if (journal->j_head == journal->j_last)
719 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400720 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700721 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700722}
723
724/*
725 * Conversion of logical to physical block numbers for the journal
726 *
727 * On external journals the journal blocks are identity-mapped, so
728 * this is a no-op. If needed, we can use j_blk_offset - everything is
729 * ready.
730 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700731int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700732 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700733{
734 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700735 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700736
737 if (journal->j_inode) {
738 ret = bmap(journal->j_inode, blocknr);
739 if (ret)
740 *retp = ret;
741 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700742 printk(KERN_ALERT "%s: journal block not found "
743 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400744 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700745 err = -EIO;
746 __journal_abort_soft(journal, err);
747 }
748 } else {
749 *retp = blocknr; /* +journal->j_blk_offset */
750 }
751 return err;
752}
753
754/*
755 * We play buffer_head aliasing tricks to write data/metadata blocks to
756 * the journal without copying their contents, but for journal
757 * descriptor blocks we do need to generate bona fide buffers.
758 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700759 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700760 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
761 * But we don't bother doing that, so there will be coherency problems with
762 * mmaps of blockdevs which hold live JBD-controlled filesystems.
763 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700764struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700765{
766 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700767 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700768 int err;
769
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700770 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700771
772 if (err)
773 return NULL;
774
775 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500776 if (!bh)
777 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700778 lock_buffer(bh);
779 memset(bh->b_data, 0, journal->j_blocksize);
780 set_buffer_uptodate(bh);
781 unlock_buffer(bh);
782 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700783 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700784}
785
Jan Kara79feb522012-03-13 22:22:54 -0400786/*
787 * Return tid of the oldest transaction in the journal and block in the journal
788 * where the transaction starts.
789 *
790 * If the journal is now empty, return which will be the next transaction ID
791 * we will write and where will that transaction start.
792 *
793 * The return value is 0 if journal tail cannot be pushed any further, 1 if
794 * it can.
795 */
796int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
797 unsigned long *block)
798{
799 transaction_t *transaction;
800 int ret;
801
802 read_lock(&journal->j_state_lock);
803 spin_lock(&journal->j_list_lock);
804 transaction = journal->j_checkpoint_transactions;
805 if (transaction) {
806 *tid = transaction->t_tid;
807 *block = transaction->t_log_start;
808 } else if ((transaction = journal->j_committing_transaction) != NULL) {
809 *tid = transaction->t_tid;
810 *block = transaction->t_log_start;
811 } else if ((transaction = journal->j_running_transaction) != NULL) {
812 *tid = transaction->t_tid;
813 *block = journal->j_head;
814 } else {
815 *tid = journal->j_transaction_sequence;
816 *block = journal->j_head;
817 }
818 ret = tid_gt(*tid, journal->j_tail_sequence);
819 spin_unlock(&journal->j_list_lock);
820 read_unlock(&journal->j_state_lock);
821
822 return ret;
823}
824
825/*
826 * Update information in journal structure and in on disk journal superblock
827 * about log tail. This function does not check whether information passed in
828 * really pushes log tail further. It's responsibility of the caller to make
829 * sure provided log tail information is valid (e.g. by holding
830 * j_checkpoint_mutex all the time between computing log tail and calling this
831 * function as is the case with jbd2_cleanup_journal_tail()).
832 *
833 * Requires j_checkpoint_mutex
834 */
835void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
836{
837 unsigned long freed;
838
839 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
840
841 /*
842 * We cannot afford for write to remain in drive's caches since as
843 * soon as we update j_tail, next transaction can start reusing journal
844 * space and if we lose sb update during power failure we'd replay
845 * old transaction with possibly newly overwritten data.
846 */
847 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
848 write_lock(&journal->j_state_lock);
849 freed = block - journal->j_tail;
850 if (block < journal->j_tail)
851 freed += journal->j_last - journal->j_first;
852
853 trace_jbd2_update_log_tail(journal, tid, block, freed);
854 jbd_debug(1,
855 "Cleaning journal tail from %d to %d (offset %lu), "
856 "freeing %lu\n",
857 journal->j_tail_sequence, tid, block, freed);
858
859 journal->j_free += freed;
860 journal->j_tail_sequence = tid;
861 journal->j_tail = block;
862 write_unlock(&journal->j_state_lock);
863}
864
Jan Kara33395782012-03-13 22:45:38 -0400865/*
866 * This is a variaon of __jbd2_update_log_tail which checks for validity of
867 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
868 * with other threads updating log tail.
869 */
870void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
871{
872 mutex_lock(&journal->j_checkpoint_mutex);
873 if (tid_gt(tid, journal->j_tail_sequence))
874 __jbd2_update_log_tail(journal, tid, block);
875 mutex_unlock(&journal->j_checkpoint_mutex);
876}
877
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500878struct jbd2_stats_proc_session {
879 journal_t *journal;
880 struct transaction_stats_s *stats;
881 int start;
882 int max;
883};
884
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500885static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
886{
887 return *pos ? NULL : SEQ_START_TOKEN;
888}
889
890static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
891{
892 return NULL;
893}
894
895static int jbd2_seq_info_show(struct seq_file *seq, void *v)
896{
897 struct jbd2_stats_proc_session *s = seq->private;
898
899 if (v != SEQ_START_TOKEN)
900 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400901 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500902 s->stats->ts_tid,
903 s->journal->j_max_transaction_buffers);
904 if (s->stats->ts_tid == 0)
905 return 0;
906 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400907 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500908 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400909 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500910 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400911 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500912 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400913 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500914 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400915 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500916 seq_printf(seq, " %lluus average transaction commit time\n",
917 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500918 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400919 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500920 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400921 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500922 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400923 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500924 return 0;
925}
926
927static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
928{
929}
930
James Morris88e9d342009-09-22 16:43:43 -0700931static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500932 .start = jbd2_seq_info_start,
933 .next = jbd2_seq_info_next,
934 .stop = jbd2_seq_info_stop,
935 .show = jbd2_seq_info_show,
936};
937
938static int jbd2_seq_info_open(struct inode *inode, struct file *file)
939{
940 journal_t *journal = PDE(inode)->data;
941 struct jbd2_stats_proc_session *s;
942 int rc, size;
943
944 s = kmalloc(sizeof(*s), GFP_KERNEL);
945 if (s == NULL)
946 return -ENOMEM;
947 size = sizeof(struct transaction_stats_s);
948 s->stats = kmalloc(size, GFP_KERNEL);
949 if (s->stats == NULL) {
950 kfree(s);
951 return -ENOMEM;
952 }
953 spin_lock(&journal->j_history_lock);
954 memcpy(s->stats, &journal->j_stats, size);
955 s->journal = journal;
956 spin_unlock(&journal->j_history_lock);
957
958 rc = seq_open(file, &jbd2_seq_info_ops);
959 if (rc == 0) {
960 struct seq_file *m = file->private_data;
961 m->private = s;
962 } else {
963 kfree(s->stats);
964 kfree(s);
965 }
966 return rc;
967
968}
969
970static int jbd2_seq_info_release(struct inode *inode, struct file *file)
971{
972 struct seq_file *seq = file->private_data;
973 struct jbd2_stats_proc_session *s = seq->private;
974 kfree(s->stats);
975 kfree(s);
976 return seq_release(inode, file);
977}
978
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700979static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500980 .owner = THIS_MODULE,
981 .open = jbd2_seq_info_open,
982 .read = seq_read,
983 .llseek = seq_lseek,
984 .release = jbd2_seq_info_release,
985};
986
987static struct proc_dir_entry *proc_jbd2_stats;
988
989static void jbd2_stats_proc_init(journal_t *journal)
990{
Theodore Ts'o05496762008-09-16 14:36:17 -0400991 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500992 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700993 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
994 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500995 }
996}
997
998static void jbd2_stats_proc_exit(journal_t *journal)
999{
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001000 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -04001001 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001002}
1003
Dave Kleikamp470decc2006-10-11 01:20:57 -07001004/*
1005 * Management for journal control blocks: functions to create and
1006 * destroy journal_t structures, and to initialise and read existing
1007 * journal blocks from disk. */
1008
1009/* First: create and setup a journal_t object in memory. We initialise
1010 * very few fields yet: that has to wait until we have created the
1011 * journal structures from from scratch, or loaded them from disk. */
1012
1013static journal_t * journal_init_common (void)
1014{
1015 journal_t *journal;
1016 int err;
1017
Andrew Morton3ebfdf82009-12-23 08:05:15 -05001018 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001019 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001020 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001021
1022 init_waitqueue_head(&journal->j_wait_transaction_locked);
1023 init_waitqueue_head(&journal->j_wait_logspace);
1024 init_waitqueue_head(&journal->j_wait_done_commit);
1025 init_waitqueue_head(&journal->j_wait_checkpoint);
1026 init_waitqueue_head(&journal->j_wait_commit);
1027 init_waitqueue_head(&journal->j_wait_updates);
1028 mutex_init(&journal->j_barrier);
1029 mutex_init(&journal->j_checkpoint_mutex);
1030 spin_lock_init(&journal->j_revoke_lock);
1031 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001032 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001033
Mingming Caocd02ff02007-10-16 18:38:25 -04001034 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -05001035 journal->j_min_batch_time = 0;
1036 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001037
1038 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001039 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040
1041 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001042 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043 if (err) {
1044 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001045 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001047
Theodore Ts'obf699322009-09-30 00:32:06 -04001048 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001049
Dave Kleikamp470decc2006-10-11 01:20:57 -07001050 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051}
1052
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001053/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001054 *
1055 * Create a journal structure assigned some fixed set of disk blocks to
1056 * the journal. We don't actually touch those disk blocks yet, but we
1057 * need to set up all of the mapping information to tell the journaling
1058 * system where the journal blocks are.
1059 *
1060 */
1061
1062/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001063 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001064 * @bdev: Block device on which to create the journal
1065 * @fs_dev: Device which hold journalled filesystem for this journal.
1066 * @start: Block nr Start of journal.
1067 * @len: Length of the journal in blocks.
1068 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001069 *
1070 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001071 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001072 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001073 * range of blocks on an arbitrary block device.
1074 *
1075 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001076journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001077 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001078 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079{
1080 journal_t *journal = journal_init_common();
1081 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001082 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001083 int n;
1084
1085 if (!journal)
1086 return NULL;
1087
1088 /* journal descriptor can store up to n blocks -bzzz */
1089 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001090 journal->j_dev = bdev;
1091 journal->j_fs_dev = fs_dev;
1092 journal->j_blk_offset = start;
1093 journal->j_maxlen = len;
1094 bdevname(journal->j_dev, journal->j_devname);
1095 p = journal->j_devname;
1096 while ((p = strchr(p, '/')))
1097 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001098 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001099 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1100 journal->j_wbufsize = n;
1101 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1102 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001103 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001104 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001105 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001106 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001107
1108 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001109 if (!bh) {
1110 printk(KERN_ERR
1111 "%s: Cannot get buffer for journal superblock\n",
1112 __func__);
1113 goto out_err;
1114 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115 journal->j_sb_buffer = bh;
1116 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001117
Dave Kleikamp470decc2006-10-11 01:20:57 -07001118 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001119out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001120 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001121 jbd2_stats_proc_exit(journal);
1122 kfree(journal);
1123 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001124}
1125
1126/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001127 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001128 * @inode: An inode to create the journal in
1129 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001130 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131 * the journal. The inode must exist already, must support bmap() and
1132 * must have all data blocks preallocated.
1133 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001134journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001135{
1136 struct buffer_head *bh;
1137 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001138 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001139 int err;
1140 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001141 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001142
1143 if (!journal)
1144 return NULL;
1145
1146 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1147 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001148 bdevname(journal->j_dev, journal->j_devname);
1149 p = journal->j_devname;
1150 while ((p = strchr(p, '/')))
1151 *p = '!';
1152 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001153 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001154 jbd_debug(1,
1155 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1156 journal, inode->i_sb->s_id, inode->i_ino,
1157 (long long) inode->i_size,
1158 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1159
1160 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1161 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001162 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001163
1164 /* journal descriptor can store up to n blocks -bzzz */
1165 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1166 journal->j_wbufsize = n;
1167 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1168 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001169 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001170 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001171 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001172 }
1173
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001174 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001175 /* If that failed, give up */
1176 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001177 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001178 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001179 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001180 }
1181
1182 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001183 if (!bh) {
1184 printk(KERN_ERR
1185 "%s: Cannot get buffer for journal superblock\n",
1186 __func__);
1187 goto out_err;
1188 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001189 journal->j_sb_buffer = bh;
1190 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1191
1192 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001193out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001194 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001195 jbd2_stats_proc_exit(journal);
1196 kfree(journal);
1197 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001198}
1199
1200/*
1201 * If the journal init or create aborts, we need to mark the journal
1202 * superblock as being NULL to prevent the journal destroy from writing
1203 * back a bogus superblock.
1204 */
1205static void journal_fail_superblock (journal_t *journal)
1206{
1207 struct buffer_head *bh = journal->j_sb_buffer;
1208 brelse(bh);
1209 journal->j_sb_buffer = NULL;
1210}
1211
1212/*
1213 * Given a journal_t structure, initialise the various fields for
1214 * startup of a new journaling session. We use this both when creating
1215 * a journal, and after recovering an old journal to reset it for
1216 * subsequent use.
1217 */
1218
1219static int journal_reset(journal_t *journal)
1220{
1221 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001222 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001223
1224 first = be32_to_cpu(sb->s_first);
1225 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001226 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001227 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001228 first, last);
1229 journal_fail_superblock(journal);
1230 return -EINVAL;
1231 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001232
1233 journal->j_first = first;
1234 journal->j_last = last;
1235
1236 journal->j_head = first;
1237 journal->j_tail = first;
1238 journal->j_free = last - first;
1239
1240 journal->j_tail_sequence = journal->j_transaction_sequence;
1241 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1242 journal->j_commit_request = journal->j_commit_sequence;
1243
1244 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1245
Dave Kleikamp470decc2006-10-11 01:20:57 -07001246 /*
1247 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001248 * no recovery (s_start == 0), then we can safely defer the superblock
1249 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001250 * attempting a write to a potential-readonly device.
1251 */
Jan Kara24bcc892012-03-13 15:41:04 -04001252 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001253 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 "(start %ld, seq %d, errno %d)\n",
1255 journal->j_tail, journal->j_tail_sequence,
1256 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001257 journal->j_flags |= JBD2_FLUSHED;
1258 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001259 /* Lock here to make assertions happy... */
1260 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001261 /*
1262 * Update log tail information. We use WRITE_FUA since new
1263 * transaction will start reusing journal space and so we
1264 * must make sure information about current log tail is on
1265 * disk before that.
1266 */
1267 jbd2_journal_update_sb_log_tail(journal,
1268 journal->j_tail_sequence,
1269 journal->j_tail,
1270 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001271 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001272 }
Jan Kara24bcc892012-03-13 15:41:04 -04001273 return jbd2_journal_start_thread(journal);
1274}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001275
Jan Kara79feb522012-03-13 22:22:54 -04001276static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001277{
1278 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001279 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001280
Jan Kara79feb522012-03-13 22:22:54 -04001281 trace_jbd2_write_superblock(journal, write_op);
1282 if (!(journal->j_flags & JBD2_BARRIER))
1283 write_op &= ~(REQ_FUA | REQ_FLUSH);
1284 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001285 if (buffer_write_io_error(bh)) {
1286 /*
1287 * Oh, dear. A previous attempt to write the journal
1288 * superblock failed. This could happen because the
1289 * USB device was yanked out. Or it could happen to
1290 * be a transient write error and maybe the block will
1291 * be remapped. Nothing we can do but to retry the
1292 * write and hope for the best.
1293 */
1294 printk(KERN_ERR "JBD2: previous I/O error detected "
1295 "for journal superblock update for %s.\n",
1296 journal->j_devname);
1297 clear_buffer_write_io_error(bh);
1298 set_buffer_uptodate(bh);
1299 }
Jan Kara79feb522012-03-13 22:22:54 -04001300 get_bh(bh);
1301 bh->b_end_io = end_buffer_write_sync;
1302 ret = submit_bh(write_op, bh);
1303 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001304 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001305 clear_buffer_write_io_error(bh);
1306 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001307 ret = -EIO;
1308 }
1309 if (ret) {
1310 printk(KERN_ERR "JBD2: Error %d detected when updating "
1311 "journal superblock for %s.\n", ret,
1312 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001313 }
1314}
Theodore Ts'o914258b2008-10-06 21:35:40 -04001315
Jan Kara24bcc892012-03-13 15:41:04 -04001316/**
1317 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1318 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001319 * @tail_tid: TID of the new transaction at the tail of the log
1320 * @tail_block: The first block of the transaction at the tail of the log
1321 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001322 *
1323 * Update a journal's superblock information about log tail and write it to
1324 * disk, waiting for the IO to complete.
1325 */
Jan Kara79feb522012-03-13 22:22:54 -04001326void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1327 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001328{
1329 journal_superblock_t *sb = journal->j_superblock;
1330
Jan Karaa78bb112012-03-13 15:43:04 -04001331 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001332 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1333 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001334
Jan Kara79feb522012-03-13 22:22:54 -04001335 sb->s_sequence = cpu_to_be32(tail_tid);
1336 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001337
Jan Kara79feb522012-03-13 22:22:54 -04001338 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001339
1340 /* Log is no longer empty */
1341 write_lock(&journal->j_state_lock);
1342 WARN_ON(!sb->s_sequence);
1343 journal->j_flags &= ~JBD2_FLUSHED;
1344 write_unlock(&journal->j_state_lock);
1345}
1346
1347/**
1348 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1349 * @journal: The journal to update.
1350 *
1351 * Update a journal's dynamic superblock fields to show that journal is empty.
1352 * Write updated superblock to disk waiting for IO to complete.
1353 */
1354static void jbd2_mark_journal_empty(journal_t *journal)
1355{
1356 journal_superblock_t *sb = journal->j_superblock;
1357
Jan Karaa78bb112012-03-13 15:43:04 -04001358 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001359 read_lock(&journal->j_state_lock);
Eric Sandeeneeecef02012-08-18 22:29:40 -04001360 /* Is it already empty? */
1361 if (sb->s_start == 0) {
1362 read_unlock(&journal->j_state_lock);
1363 return;
1364 }
Jan Kara24bcc892012-03-13 15:41:04 -04001365 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1366 journal->j_tail_sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001367
1368 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
Jan Kara24bcc892012-03-13 15:41:04 -04001369 sb->s_start = cpu_to_be32(0);
1370 read_unlock(&journal->j_state_lock);
1371
Jan Kara79feb522012-03-13 22:22:54 -04001372 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001373
1374 /* Log is no longer empty */
1375 write_lock(&journal->j_state_lock);
1376 journal->j_flags |= JBD2_FLUSHED;
1377 write_unlock(&journal->j_state_lock);
1378}
1379
1380
1381/**
1382 * jbd2_journal_update_sb_errno() - Update error in the journal.
1383 * @journal: The journal to update.
1384 *
1385 * Update a journal's errno. Write updated superblock to disk waiting for IO
1386 * to complete.
1387 */
Theodore Ts'od796c522012-08-05 19:04:57 -04001388void jbd2_journal_update_sb_errno(journal_t *journal)
Jan Kara24bcc892012-03-13 15:41:04 -04001389{
1390 journal_superblock_t *sb = journal->j_superblock;
1391
1392 read_lock(&journal->j_state_lock);
1393 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1394 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395 sb->s_errno = cpu_to_be32(journal->j_errno);
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001396 jbd2_superblock_csum_set(journal, sb);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001397 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001398
Jan Kara79feb522012-03-13 22:22:54 -04001399 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400}
Theodore Ts'od796c522012-08-05 19:04:57 -04001401EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001402
1403/*
1404 * Read the superblock for a given journal, performing initial
1405 * validation of the format.
1406 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001407static int journal_get_superblock(journal_t *journal)
1408{
1409 struct buffer_head *bh;
1410 journal_superblock_t *sb;
1411 int err = -EIO;
1412
1413 bh = journal->j_sb_buffer;
1414
1415 J_ASSERT(bh != NULL);
1416 if (!buffer_uptodate(bh)) {
1417 ll_rw_block(READ, 1, &bh);
1418 wait_on_buffer(bh);
1419 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001420 printk(KERN_ERR
1421 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001422 goto out;
1423 }
1424 }
1425
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001426 if (buffer_verified(bh))
1427 return 0;
1428
Dave Kleikamp470decc2006-10-11 01:20:57 -07001429 sb = journal->j_superblock;
1430
1431 err = -EINVAL;
1432
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001433 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001434 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001435 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436 goto out;
1437 }
1438
1439 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001440 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 journal->j_format_version = 1;
1442 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001443 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001444 journal->j_format_version = 2;
1445 break;
1446 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001447 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001448 goto out;
1449 }
1450
1451 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1452 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1453 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001454 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001455 goto out;
1456 }
1457
Eryu Guan87622022011-11-01 19:04:59 -04001458 if (be32_to_cpu(sb->s_first) == 0 ||
1459 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1460 printk(KERN_WARNING
1461 "JBD2: Invalid start block of journal: %u\n",
1462 be32_to_cpu(sb->s_first));
1463 goto out;
1464 }
1465
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001466 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1467 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1468 /* Can't have checksum v1 and v2 on at the same time! */
1469 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1470 "at the same time!\n");
1471 goto out;
1472 }
1473
1474 if (!jbd2_verify_csum_type(journal, sb)) {
1475 printk(KERN_ERR "JBD: Unknown checksum type\n");
1476 goto out;
1477 }
1478
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001479 /* Load the checksum driver */
1480 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1481 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1482 if (IS_ERR(journal->j_chksum_driver)) {
1483 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1484 err = PTR_ERR(journal->j_chksum_driver);
1485 journal->j_chksum_driver = NULL;
1486 goto out;
1487 }
1488 }
1489
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001490 /* Check superblock checksum */
1491 if (!jbd2_superblock_csum_verify(journal, sb)) {
1492 printk(KERN_ERR "JBD: journal checksum error\n");
1493 goto out;
1494 }
1495
1496 /* Precompute checksum seed for all metadata */
1497 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1498 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1499 sizeof(sb->s_uuid));
1500
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001501 set_buffer_verified(bh);
1502
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 return 0;
1504
1505out:
1506 journal_fail_superblock(journal);
1507 return err;
1508}
1509
1510/*
1511 * Load the on-disk journal superblock and read the key fields into the
1512 * journal_t.
1513 */
1514
1515static int load_superblock(journal_t *journal)
1516{
1517 int err;
1518 journal_superblock_t *sb;
1519
1520 err = journal_get_superblock(journal);
1521 if (err)
1522 return err;
1523
1524 sb = journal->j_superblock;
1525
1526 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1527 journal->j_tail = be32_to_cpu(sb->s_start);
1528 journal->j_first = be32_to_cpu(sb->s_first);
1529 journal->j_last = be32_to_cpu(sb->s_maxlen);
1530 journal->j_errno = be32_to_cpu(sb->s_errno);
1531
1532 return 0;
1533}
1534
1535
1536/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001537 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001538 * @journal: Journal to act on.
1539 *
1540 * Given a journal_t structure which tells us which disk blocks contain
1541 * a journal, read the journal from disk to initialise the in-memory
1542 * structures.
1543 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001544int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001545{
1546 int err;
1547 journal_superblock_t *sb;
1548
1549 err = load_superblock(journal);
1550 if (err)
1551 return err;
1552
1553 sb = journal->j_superblock;
1554 /* If this is a V2 superblock, then we have to check the
1555 * features flags on it. */
1556
1557 if (journal->j_format_version >= 2) {
1558 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001559 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001560 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001561 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001562 printk(KERN_WARNING
1563 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 return -EINVAL;
1565 }
1566 }
1567
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001568 /*
1569 * Create a slab for this blocksize
1570 */
1571 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1572 if (err)
1573 return err;
1574
Dave Kleikamp470decc2006-10-11 01:20:57 -07001575 /* Let the recovery code check whether it needs to recover any
1576 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001577 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001578 goto recovery_error;
1579
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001580 if (journal->j_failed_commit) {
1581 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1582 "is corrupt.\n", journal->j_failed_commit,
1583 journal->j_devname);
1584 return -EIO;
1585 }
1586
Dave Kleikamp470decc2006-10-11 01:20:57 -07001587 /* OK, we've finished with the dynamic journal bits:
1588 * reinitialise the dynamic contents of the superblock in memory
1589 * and reset them on disk. */
1590 if (journal_reset(journal))
1591 goto recovery_error;
1592
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001593 journal->j_flags &= ~JBD2_ABORT;
1594 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001595 return 0;
1596
1597recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001598 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 return -EIO;
1600}
1601
1602/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001603 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001604 * @journal: Journal to act on.
1605 *
1606 * Release a journal_t structure once it is no longer in use by the
1607 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001608 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001610int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001611{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001612 int err = 0;
1613
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614 /* Wait for the commit thread to wake up and die. */
1615 journal_kill_thread(journal);
1616
1617 /* Force a final log commit */
1618 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001619 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620
1621 /* Force any old transactions to disk */
1622
1623 /* Totally anal locking here... */
1624 spin_lock(&journal->j_list_lock);
1625 while (journal->j_checkpoint_transactions != NULL) {
1626 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001627 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001628 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001629 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630 spin_lock(&journal->j_list_lock);
1631 }
1632
1633 J_ASSERT(journal->j_running_transaction == NULL);
1634 J_ASSERT(journal->j_committing_transaction == NULL);
1635 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1636 spin_unlock(&journal->j_list_lock);
1637
Dave Kleikamp470decc2006-10-11 01:20:57 -07001638 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001639 if (!is_journal_aborted(journal)) {
Jan Karaa78bb112012-03-13 15:43:04 -04001640 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001641 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001642 mutex_unlock(&journal->j_checkpoint_mutex);
1643 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001644 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645 brelse(journal->j_sb_buffer);
1646 }
1647
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001648 if (journal->j_proc_entry)
1649 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 if (journal->j_inode)
1651 iput(journal->j_inode);
1652 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001653 jbd2_journal_destroy_revoke(journal);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001654 if (journal->j_chksum_driver)
1655 crypto_free_shash(journal->j_chksum_driver);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001656 kfree(journal->j_wbuf);
1657 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001658
1659 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001660}
1661
1662
1663/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001664 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 * @journal: Journal to check.
1666 * @compat: bitmask of compatible features
1667 * @ro: bitmask of features that force read-only mount
1668 * @incompat: bitmask of incompatible features
1669 *
1670 * Check whether the journal uses all of a given set of
1671 * features. Return true (non-zero) if it does.
1672 **/
1673
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001674int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001675 unsigned long ro, unsigned long incompat)
1676{
1677 journal_superblock_t *sb;
1678
1679 if (!compat && !ro && !incompat)
1680 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001681 /* Load journal superblock if it is not loaded yet. */
1682 if (journal->j_format_version == 0 &&
1683 journal_get_superblock(journal) != 0)
1684 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001685 if (journal->j_format_version == 1)
1686 return 0;
1687
1688 sb = journal->j_superblock;
1689
1690 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1691 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1692 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1693 return 1;
1694
1695 return 0;
1696}
1697
1698/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001699 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001700 * @journal: Journal to check.
1701 * @compat: bitmask of compatible features
1702 * @ro: bitmask of features that force read-only mount
1703 * @incompat: bitmask of incompatible features
1704 *
1705 * Check whether the journaling code supports the use of
1706 * all of a given set of features on this journal. Return true
1707 * (non-zero) if it can. */
1708
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001709int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001710 unsigned long ro, unsigned long incompat)
1711{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001712 if (!compat && !ro && !incompat)
1713 return 1;
1714
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715 /* We can support any known requested features iff the
1716 * superblock is in version 2. Otherwise we fail to support any
1717 * extended sb features. */
1718
1719 if (journal->j_format_version != 2)
1720 return 0;
1721
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001722 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1723 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1724 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001725 return 1;
1726
1727 return 0;
1728}
1729
1730/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732 * @journal: Journal to act on.
1733 * @compat: bitmask of compatible features
1734 * @ro: bitmask of features that force read-only mount
1735 * @incompat: bitmask of incompatible features
1736 *
1737 * Mark a given journal feature as present on the
1738 * superblock. Returns true if the requested features could be set.
1739 *
1740 */
1741
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001742int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001743 unsigned long ro, unsigned long incompat)
1744{
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001745#define INCOMPAT_FEATURE_ON(f) \
1746 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1747#define COMPAT_FEATURE_ON(f) \
1748 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 journal_superblock_t *sb;
1750
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001751 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752 return 1;
1753
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001754 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001755 return 0;
1756
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001757 /* Asking for checksumming v2 and v1? Only give them v2. */
1758 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1759 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1760 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1761
Dave Kleikamp470decc2006-10-11 01:20:57 -07001762 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1763 compat, ro, incompat);
1764
1765 sb = journal->j_superblock;
1766
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001767 /* If enabling v2 checksums, update superblock */
1768 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1769 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1770 sb->s_feature_compat &=
1771 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001772
1773 /* Load the checksum driver */
1774 if (journal->j_chksum_driver == NULL) {
1775 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1776 0, 0);
1777 if (IS_ERR(journal->j_chksum_driver)) {
1778 printk(KERN_ERR "JBD: Cannot load crc32c "
1779 "driver.\n");
1780 journal->j_chksum_driver = NULL;
1781 return 0;
1782 }
1783 }
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001784
1785 /* Precompute checksum seed for all metadata */
1786 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1787 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1788 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1789 sb->s_uuid,
1790 sizeof(sb->s_uuid));
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001791 }
1792
1793 /* If enabling v1 checksums, downgrade superblock */
1794 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1795 sb->s_feature_incompat &=
1796 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1797
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 sb->s_feature_compat |= cpu_to_be32(compat);
1799 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1800 sb->s_feature_incompat |= cpu_to_be32(incompat);
1801
1802 return 1;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001803#undef COMPAT_FEATURE_ON
1804#undef INCOMPAT_FEATURE_ON
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805}
1806
Girish Shilamkar818d2762008-01-28 23:58:27 -05001807/*
1808 * jbd2_journal_clear_features () - Clear a given journal feature in the
1809 * superblock
1810 * @journal: Journal to act on.
1811 * @compat: bitmask of compatible features
1812 * @ro: bitmask of features that force read-only mount
1813 * @incompat: bitmask of incompatible features
1814 *
1815 * Clear a given journal feature as present on the
1816 * superblock.
1817 */
1818void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1819 unsigned long ro, unsigned long incompat)
1820{
1821 journal_superblock_t *sb;
1822
1823 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1824 compat, ro, incompat);
1825
1826 sb = journal->j_superblock;
1827
1828 sb->s_feature_compat &= ~cpu_to_be32(compat);
1829 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1830 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1831}
1832EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001833
1834/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001835 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001836 * @journal: Journal to act on.
1837 *
1838 * Flush all data for a given journal to disk and empty the journal.
1839 * Filesystems can use this when remounting readonly to ensure that
1840 * recovery does not need to happen on remount.
1841 */
1842
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001843int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001844{
1845 int err = 0;
1846 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001847
Theodore Ts'oa931da62010-08-03 21:35:12 -04001848 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849
1850 /* Force everything buffered to the log... */
1851 if (journal->j_running_transaction) {
1852 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001853 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001854 } else if (journal->j_committing_transaction)
1855 transaction = journal->j_committing_transaction;
1856
1857 /* Wait for the log commit to complete... */
1858 if (transaction) {
1859 tid_t tid = transaction->t_tid;
1860
Theodore Ts'oa931da62010-08-03 21:35:12 -04001861 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001862 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001863 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001864 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001865 }
1866
1867 /* ...and flush everything in the log out to disk. */
1868 spin_lock(&journal->j_list_lock);
1869 while (!err && journal->j_checkpoint_transactions != NULL) {
1870 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001871 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001872 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001873 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001874 spin_lock(&journal->j_list_lock);
1875 }
1876 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001877
1878 if (is_journal_aborted(journal))
1879 return -EIO;
1880
Jan Karaa78bb112012-03-13 15:43:04 -04001881 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001882 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883
1884 /* Finally, mark the journal as really needing no recovery.
1885 * This sets s_start==0 in the underlying superblock, which is
1886 * the magic code for a fully-recovered superblock. Any future
1887 * commits of data to the journal will restore the current
1888 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001889 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001890 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001891 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001892 J_ASSERT(!journal->j_running_transaction);
1893 J_ASSERT(!journal->j_committing_transaction);
1894 J_ASSERT(!journal->j_checkpoint_transactions);
1895 J_ASSERT(journal->j_head == journal->j_tail);
1896 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001897 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001898 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899}
1900
1901/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001902 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001903 * @journal: Journal to act on.
1904 * @write: flag (see below)
1905 *
1906 * Wipe out all of the contents of a journal, safely. This will produce
1907 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001908 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001909 *
1910 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1911 * we merely suppress recovery.
1912 */
1913
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001914int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001915{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001916 int err = 0;
1917
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919
1920 err = load_superblock(journal);
1921 if (err)
1922 return err;
1923
Dave Kleikamp470decc2006-10-11 01:20:57 -07001924 if (!journal->j_tail)
1925 goto no_recovery;
1926
Eryu Guanf2a44522011-11-01 19:09:18 -04001927 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928 write ? "Clearing" : "Ignoring");
1929
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001930 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001931 if (write) {
1932 /* Lock to make assertions happy... */
1933 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001934 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001935 mutex_unlock(&journal->j_checkpoint_mutex);
1936 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001937
1938 no_recovery:
1939 return err;
1940}
1941
1942/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001943 * Journal abort has very specific semantics, which we describe
1944 * for journal abort.
1945 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001946 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001947 * itself are here.
1948 */
1949
1950/*
1951 * Quick version for internal journal use (doesn't lock the journal).
1952 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1953 * and don't attempt to make any other journal updates.
1954 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001955void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001956{
1957 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001958
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001959 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 return;
1961
1962 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001963 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964
Theodore Ts'oa931da62010-08-03 21:35:12 -04001965 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 transaction = journal->j_running_transaction;
1968 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001969 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001970 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971}
1972
1973/* Soft abort: record the abort error status in the journal superblock,
1974 * but don't do any other IO. */
1975static void __journal_abort_soft (journal_t *journal, int errno)
1976{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001977 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001978 return;
1979
1980 if (!journal->j_errno)
1981 journal->j_errno = errno;
1982
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001983 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984
1985 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04001986 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001987}
1988
1989/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001990 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991 * @journal: the journal to shutdown.
1992 * @errno: an error number to record in the journal indicating
1993 * the reason for the shutdown.
1994 *
1995 * Perform a complete, immediate shutdown of the ENTIRE
1996 * journal (not of a single transaction). This operation cannot be
1997 * undone without closing and reopening the journal.
1998 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001999 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2001 * mode.
2002 *
2003 * Journal abort has very specific semantics. Any existing dirty,
2004 * unjournaled buffers in the main filesystem will still be written to
2005 * disk by bdflush, but the journaling mechanism will be suspended
2006 * immediately and no further transaction commits will be honoured.
2007 *
2008 * Any dirty, journaled buffers will be written back to disk without
2009 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2010 * filesystem, but we _do_ attempt to leave as much data as possible
2011 * behind for fsck to use for cleanup.
2012 *
2013 * Any attempt to get a new transaction handle on a journal which is in
2014 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002015 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 * entered abort state during the update.
2017 *
2018 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002019 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002020 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002021 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022 * which will be recorded (if possible) in the journal superblock. This
2023 * allows a client to record failure conditions in the middle of a
2024 * transaction without having to complete the transaction to record the
2025 * failure to disk. ext3_error, for example, now uses this
2026 * functionality.
2027 *
2028 * Errors which originate from within the journaling layer will NOT
2029 * supply an errno; a null errno implies that absolutely no further
2030 * writes are done to the journal (unless there are any already in
2031 * progress).
2032 *
2033 */
2034
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002035void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002036{
2037 __journal_abort_soft(journal, errno);
2038}
2039
2040/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 * @journal: journal to examine.
2043 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002044 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07002045 * time the journal was mounted - if the journal was stopped
2046 * without calling abort this will be 0.
2047 *
2048 * If the journal has been aborted on this mount time -EROFS will
2049 * be returned.
2050 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052{
2053 int err;
2054
Theodore Ts'oa931da62010-08-03 21:35:12 -04002055 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002056 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002057 err = -EROFS;
2058 else
2059 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002060 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002061 return err;
2062}
2063
2064/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002065 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07002066 * @journal: journal to act on.
2067 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002068 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002069 * mode.
2070 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002071int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002072{
2073 int err = 0;
2074
Theodore Ts'oa931da62010-08-03 21:35:12 -04002075 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002076 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002077 err = -EROFS;
2078 else
2079 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002080 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002081 return err;
2082}
2083
2084/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002085 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002086 * @journal: journal to act on.
2087 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002088 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002089 * mode.
2090 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002091void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002092{
Theodore Ts'oa931da62010-08-03 21:35:12 -04002093 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002094 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002095 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002096 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002097}
2098
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002099int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100{
2101 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2102}
2103
2104/*
Zach Brownb517bea2006-10-11 01:21:08 -07002105 * helper functions to deal with 32 or 64bit block numbers.
2106 */
2107size_t journal_tag_bytes(journal_t *journal)
2108{
Darrick J. Wongc3900872012-05-27 08:12:12 -04002109 journal_block_tag_t tag;
2110 size_t x = 0;
2111
2112 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2113 x += sizeof(tag.t_checksum);
2114
Zach Brownb517bea2006-10-11 01:21:08 -07002115 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Darrick J. Wongc3900872012-05-27 08:12:12 -04002116 return x + JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07002117 else
Darrick J. Wongc3900872012-05-27 08:12:12 -04002118 return x + JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07002119}
2120
2121/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002122 * JBD memory management
2123 *
2124 * These functions are used to allocate block-sized chunks of memory
2125 * used for making copies of buffer_head data. Very often it will be
2126 * page-sized chunks of data, but sometimes it will be in
2127 * sub-page-size chunks. (For example, 16k pages on Power systems
2128 * with a 4k block file system.) For blocks smaller than a page, we
2129 * use a SLAB allocator. There are slab caches for each block size,
2130 * which are allocated at mount time, if necessary, and we only free
2131 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2132 * this reason we don't need to a mutex to protect access to
2133 * jbd2_slab[] allocating or releasing memory; only in
2134 * jbd2_journal_create_slab().
2135 */
2136#define JBD2_MAX_SLABS 8
2137static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002138
2139static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2140 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2141 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2142};
2143
2144
2145static void jbd2_journal_destroy_slabs(void)
2146{
2147 int i;
2148
2149 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2150 if (jbd2_slab[i])
2151 kmem_cache_destroy(jbd2_slab[i]);
2152 jbd2_slab[i] = NULL;
2153 }
2154}
2155
2156static int jbd2_journal_create_slab(size_t size)
2157{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002158 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002159 int i = order_base_2(size) - 10;
2160 size_t slab_size;
2161
2162 if (size == PAGE_SIZE)
2163 return 0;
2164
2165 if (i >= JBD2_MAX_SLABS)
2166 return -EINVAL;
2167
2168 if (unlikely(i < 0))
2169 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002170 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002171 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002172 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002173 return 0; /* Already created */
2174 }
2175
2176 slab_size = 1 << (i+10);
2177 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2178 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002179 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002180 if (!jbd2_slab[i]) {
2181 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2182 return -ENOMEM;
2183 }
2184 return 0;
2185}
2186
2187static struct kmem_cache *get_slab(size_t size)
2188{
2189 int i = order_base_2(size) - 10;
2190
2191 BUG_ON(i >= JBD2_MAX_SLABS);
2192 if (unlikely(i < 0))
2193 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002194 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002195 return jbd2_slab[i];
2196}
2197
2198void *jbd2_alloc(size_t size, gfp_t flags)
2199{
2200 void *ptr;
2201
2202 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2203
2204 flags |= __GFP_REPEAT;
2205 if (size == PAGE_SIZE)
2206 ptr = (void *)__get_free_pages(flags, 0);
2207 else if (size > PAGE_SIZE) {
2208 int order = get_order(size);
2209
2210 if (order < 3)
2211 ptr = (void *)__get_free_pages(flags, order);
2212 else
2213 ptr = vmalloc(size);
2214 } else
2215 ptr = kmem_cache_alloc(get_slab(size), flags);
2216
2217 /* Check alignment; SLUB has gotten this wrong in the past,
2218 * and this can lead to user data corruption! */
2219 BUG_ON(((unsigned long) ptr) & (size-1));
2220
2221 return ptr;
2222}
2223
2224void jbd2_free(void *ptr, size_t size)
2225{
2226 if (size == PAGE_SIZE) {
2227 free_pages((unsigned long)ptr, 0);
2228 return;
2229 }
2230 if (size > PAGE_SIZE) {
2231 int order = get_order(size);
2232
2233 if (order < 3)
2234 free_pages((unsigned long)ptr, order);
2235 else
2236 vfree(ptr);
2237 return;
2238 }
2239 kmem_cache_free(get_slab(size), ptr);
2240};
2241
2242/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002243 * Journal_head storage management
2244 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002245static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002246#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002247static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2248#endif
2249
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002250static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002251{
2252 int retval;
2253
Al Viro1076d172008-03-29 03:07:18 +00002254 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002255 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002256 sizeof(struct journal_head),
2257 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002258 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002259 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002260 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002261 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002262 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002263 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002264 }
2265 return retval;
2266}
2267
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002268static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002269{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002270 if (jbd2_journal_head_cache) {
2271 kmem_cache_destroy(jbd2_journal_head_cache);
2272 jbd2_journal_head_cache = NULL;
2273 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002274}
2275
2276/*
2277 * journal_head splicing and dicing
2278 */
2279static struct journal_head *journal_alloc_journal_head(void)
2280{
2281 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002282
Jose R. Santose23291b2007-07-18 08:57:06 -04002283#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002284 atomic_inc(&nr_journal_heads);
2285#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002286 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002287 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002289 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002290 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002291 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002292 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002293 }
2294 }
2295 return ret;
2296}
2297
2298static void journal_free_journal_head(struct journal_head *jh)
2299{
Jose R. Santose23291b2007-07-18 08:57:06 -04002300#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002301 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002302 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002303#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002304 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002305}
2306
2307/*
2308 * A journal_head is attached to a buffer_head whenever JBD has an
2309 * interest in the buffer.
2310 *
2311 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2312 * is set. This bit is tested in core kernel code where we need to take
2313 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2314 * there.
2315 *
2316 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2317 *
2318 * When a buffer has its BH_JBD bit set it is immune from being released by
2319 * core kernel code, mainly via ->b_count.
2320 *
Jan Karade1b7942011-06-13 15:38:22 -04002321 * A journal_head is detached from its buffer_head when the journal_head's
2322 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2323 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002324 *
2325 * Various places in the kernel want to attach a journal_head to a buffer_head
2326 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002327 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002328 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002329 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002330 *
2331 * So the typical usage would be:
2332 *
2333 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002334 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002335 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002336 * (Get another reference for transaction)
2337 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002338 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002339 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341 */
2342
2343/*
2344 * Give a buffer_head a journal_head.
2345 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002346 * May sleep.
2347 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002348struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002349{
2350 struct journal_head *jh;
2351 struct journal_head *new_jh = NULL;
2352
2353repeat:
2354 if (!buffer_jbd(bh)) {
2355 new_jh = journal_alloc_journal_head();
2356 memset(new_jh, 0, sizeof(*new_jh));
2357 }
2358
2359 jbd_lock_bh_journal_head(bh);
2360 if (buffer_jbd(bh)) {
2361 jh = bh2jh(bh);
2362 } else {
2363 J_ASSERT_BH(bh,
2364 (atomic_read(&bh->b_count) > 0) ||
2365 (bh->b_page && bh->b_page->mapping));
2366
2367 if (!new_jh) {
2368 jbd_unlock_bh_journal_head(bh);
2369 goto repeat;
2370 }
2371
2372 jh = new_jh;
2373 new_jh = NULL; /* We consumed it */
2374 set_buffer_jbd(bh);
2375 bh->b_private = jh;
2376 jh->b_bh = bh;
2377 get_bh(bh);
2378 BUFFER_TRACE(bh, "added journal_head");
2379 }
2380 jh->b_jcount++;
2381 jbd_unlock_bh_journal_head(bh);
2382 if (new_jh)
2383 journal_free_journal_head(new_jh);
2384 return bh->b_private;
2385}
2386
2387/*
2388 * Grab a ref against this buffer_head's journal_head. If it ended up not
2389 * having a journal_head, return NULL
2390 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002391struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002392{
2393 struct journal_head *jh = NULL;
2394
2395 jbd_lock_bh_journal_head(bh);
2396 if (buffer_jbd(bh)) {
2397 jh = bh2jh(bh);
2398 jh->b_jcount++;
2399 }
2400 jbd_unlock_bh_journal_head(bh);
2401 return jh;
2402}
2403
2404static void __journal_remove_journal_head(struct buffer_head *bh)
2405{
2406 struct journal_head *jh = bh2jh(bh);
2407
2408 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002409 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2410 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2411 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2412 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2413 J_ASSERT_BH(bh, buffer_jbd(bh));
2414 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2415 BUFFER_TRACE(bh, "remove journal_head");
2416 if (jh->b_frozen_data) {
2417 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2418 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002419 }
Jan Karade1b7942011-06-13 15:38:22 -04002420 if (jh->b_committed_data) {
2421 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2422 jbd2_free(jh->b_committed_data, bh->b_size);
2423 }
2424 bh->b_private = NULL;
2425 jh->b_bh = NULL; /* debug, really */
2426 clear_buffer_jbd(bh);
2427 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002428}
2429
2430/*
Jan Karade1b7942011-06-13 15:38:22 -04002431 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002432 * release the journal_head from the buffer_head.
2433 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002434void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002435{
2436 struct buffer_head *bh = jh2bh(jh);
2437
2438 jbd_lock_bh_journal_head(bh);
2439 J_ASSERT_JH(jh, jh->b_jcount > 0);
2440 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002441 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002442 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002443 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002444 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002445 } else
2446 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002447}
2448
2449/*
Jan Karac851ed52008-07-11 19:27:31 -04002450 * Initialize jbd inode head
2451 */
2452void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2453{
2454 jinode->i_transaction = NULL;
2455 jinode->i_next_transaction = NULL;
2456 jinode->i_vfs_inode = inode;
2457 jinode->i_flags = 0;
2458 INIT_LIST_HEAD(&jinode->i_list);
2459}
2460
2461/*
2462 * Function to be called before we start removing inode from memory (i.e.,
2463 * clear_inode() is a fine place to be called from). It removes inode from
2464 * transaction's lists.
2465 */
2466void jbd2_journal_release_jbd_inode(journal_t *journal,
2467 struct jbd2_inode *jinode)
2468{
Jan Karac851ed52008-07-11 19:27:31 -04002469 if (!journal)
2470 return;
2471restart:
2472 spin_lock(&journal->j_list_lock);
2473 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002474 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002475 wait_queue_head_t *wq;
2476 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2477 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2478 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2479 spin_unlock(&journal->j_list_lock);
2480 schedule();
2481 finish_wait(wq, &wait.wait);
2482 goto restart;
2483 }
2484
Jan Karac851ed52008-07-11 19:27:31 -04002485 if (jinode->i_transaction) {
2486 list_del(&jinode->i_list);
2487 jinode->i_transaction = NULL;
2488 }
2489 spin_unlock(&journal->j_list_lock);
2490}
2491
2492/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002493 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002494 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002495#ifdef CONFIG_JBD2_DEBUG
2496u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002497EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002498
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002499#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002500
Jose R. Santos6f38c742007-10-16 18:38:25 -04002501static struct dentry *jbd2_debugfs_dir;
2502static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002503
2504static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002505{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002506 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2507 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002508 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2509 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002510 jbd2_debugfs_dir,
2511 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002512}
2513
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002514static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002515{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002516 debugfs_remove(jbd2_debug);
2517 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002518}
2519
2520#else
2521
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002522static void __init jbd2_create_debugfs_entry(void)
2523{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002524}
2525
2526static void __exit jbd2_remove_debugfs_entry(void)
2527{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002528}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002529
2530#endif
2531
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002532#ifdef CONFIG_PROC_FS
2533
2534#define JBD2_STATS_PROC_NAME "fs/jbd2"
2535
2536static void __init jbd2_create_jbd_stats_proc_entry(void)
2537{
2538 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2539}
2540
2541static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2542{
2543 if (proc_jbd2_stats)
2544 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2545}
2546
2547#else
2548
2549#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2550#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2551
2552#endif
2553
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002554struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002555
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002556static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002557{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002558 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002559 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002560 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2561 return -ENOMEM;
2562 }
2563 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2564 if (jbd2_inode_cache == NULL) {
2565 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2566 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002567 return -ENOMEM;
2568 }
2569 return 0;
2570}
2571
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002572static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002573{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002574 if (jbd2_handle_cache)
2575 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002576 if (jbd2_inode_cache)
2577 kmem_cache_destroy(jbd2_inode_cache);
2578
Dave Kleikamp470decc2006-10-11 01:20:57 -07002579}
2580
2581/*
2582 * Module startup and shutdown
2583 */
2584
2585static int __init journal_init_caches(void)
2586{
2587 int ret;
2588
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002589 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002590 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002591 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002592 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002593 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002594 if (ret == 0)
2595 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002596 return ret;
2597}
2598
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002599static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002600{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002601 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002602 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002603 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002604 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002605 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002606}
2607
2608static int __init journal_init(void)
2609{
2610 int ret;
2611
2612 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2613
2614 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002615 if (ret == 0) {
2616 jbd2_create_debugfs_entry();
2617 jbd2_create_jbd_stats_proc_entry();
2618 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002619 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002620 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002621 return ret;
2622}
2623
2624static void __exit journal_exit(void)
2625{
Jose R. Santose23291b2007-07-18 08:57:06 -04002626#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002627 int n = atomic_read(&nr_journal_heads);
2628 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002629 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002630#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002631 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002632 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002633 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002634}
2635
Dave Kleikamp470decc2006-10-11 01:20:57 -07002636MODULE_LICENSE("GPL");
2637module_init(journal_init);
2638module_exit(journal_exit);
2639