blob: 63175f9391abf8f6ef5d694bb1a5d446f59d5685 [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);
63EXPORT_SYMBOL(jbd2_journal_release_buffer);
64EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070065#if 0
66EXPORT_SYMBOL(journal_sync_buffer);
67#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070068EXPORT_SYMBOL(jbd2_journal_flush);
69EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070070
Mingming Caof7f4bcc2006-10-11 01:20:59 -070071EXPORT_SYMBOL(jbd2_journal_init_dev);
72EXPORT_SYMBOL(jbd2_journal_init_inode);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070073EXPORT_SYMBOL(jbd2_journal_check_used_features);
74EXPORT_SYMBOL(jbd2_journal_check_available_features);
75EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070076EXPORT_SYMBOL(jbd2_journal_load);
77EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070078EXPORT_SYMBOL(jbd2_journal_abort);
79EXPORT_SYMBOL(jbd2_journal_errno);
80EXPORT_SYMBOL(jbd2_journal_ack_err);
81EXPORT_SYMBOL(jbd2_journal_clear_err);
82EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050083EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070084EXPORT_SYMBOL(jbd2_journal_start_commit);
85EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
86EXPORT_SYMBOL(jbd2_journal_wipe);
87EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
88EXPORT_SYMBOL(jbd2_journal_invalidatepage);
89EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
90EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040091EXPORT_SYMBOL(jbd2_journal_file_inode);
92EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
93EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
94EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050095EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -070096
Dave Kleikamp470decc2006-10-11 01:20:57 -070097static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -050098static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -070099
Darrick J. Wong25ed6e82012-05-27 07:48:56 -0400100/* Checksumming functions */
101int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
102{
103 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
104 return 1;
105
106 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
107}
108
Dave Kleikamp470decc2006-10-11 01:20:57 -0700109/*
110 * Helper function used to manage commit timeouts
111 */
112
113static void commit_timeout(unsigned long __data)
114{
115 struct task_struct * p = (struct task_struct *) __data;
116
117 wake_up_process(p);
118}
119
120/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700121 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700122 * journal.
123 *
124 * This kernel thread is responsible for two things:
125 *
126 * 1) COMMIT: Every so often we need to commit the current state of the
127 * filesystem to disk. The journal thread is responsible for writing
128 * all of the metadata buffers to disk.
129 *
130 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
131 * of the data in that part of the log has been rewritten elsewhere on
132 * the disk. Flushing these old buffers to reclaim space in the log is
133 * known as checkpointing, and this thread is responsible for that job.
134 */
135
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700136static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700137{
138 journal_t *journal = arg;
139 transaction_t *transaction;
140
141 /*
142 * Set up an interval timer which can be used to trigger a commit wakeup
143 * after the commit interval expires
144 */
145 setup_timer(&journal->j_commit_timer, commit_timeout,
146 (unsigned long)current);
147
Nigel Cunningham35c80422012-02-03 19:59:41 +1100148 set_freezable();
149
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150 /* Record that the journal thread is running */
151 journal->j_task = current;
152 wake_up(&journal->j_wait_done_commit);
153
Dave Kleikamp470decc2006-10-11 01:20:57 -0700154 /*
155 * And now, wait forever for commit wakeup events.
156 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400157 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700158
159loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700160 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700161 goto end_loop;
162
163 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
164 journal->j_commit_sequence, journal->j_commit_request);
165
166 if (journal->j_commit_sequence != journal->j_commit_request) {
167 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400168 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700169 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700170 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400171 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700172 goto loop;
173 }
174
175 wake_up(&journal->j_wait_done_commit);
176 if (freezing(current)) {
177 /*
178 * The simpler the better. Flushing journal isn't a
179 * good idea, because that depends on threads that may
180 * be already stopped.
181 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700182 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400183 write_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800184 try_to_freeze();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400185 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700186 } else {
187 /*
188 * We assume on resume that commits are already there,
189 * so we don't sleep
190 */
191 DEFINE_WAIT(wait);
192 int should_sleep = 1;
193
194 prepare_to_wait(&journal->j_wait_commit, &wait,
195 TASK_INTERRUPTIBLE);
196 if (journal->j_commit_sequence != journal->j_commit_request)
197 should_sleep = 0;
198 transaction = journal->j_running_transaction;
199 if (transaction && time_after_eq(jiffies,
200 transaction->t_expires))
201 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700202 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700203 should_sleep = 0;
204 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400205 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700206 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400207 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700208 }
209 finish_wait(&journal->j_wait_commit, &wait);
210 }
211
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700212 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700213
214 /*
215 * Were we woken up by a commit wakeup event?
216 */
217 transaction = journal->j_running_transaction;
218 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
219 journal->j_commit_request = transaction->t_tid;
220 jbd_debug(1, "woke because of timeout\n");
221 }
222 goto loop;
223
224end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400225 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700226 del_timer_sync(&journal->j_commit_timer);
227 journal->j_task = NULL;
228 wake_up(&journal->j_wait_done_commit);
229 jbd_debug(1, "Journal thread exiting.\n");
230 return 0;
231}
232
Pavel Emelianov97f06782007-05-08 00:30:42 -0700233static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700235 struct task_struct *t;
236
Theodore Ts'o90576c02009-09-29 15:51:30 -0400237 t = kthread_run(kjournald2, journal, "jbd2/%s",
238 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700239 if (IS_ERR(t))
240 return PTR_ERR(t);
241
Al Viro1076d172008-03-29 03:07:18 +0000242 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700243 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244}
245
246static void journal_kill_thread(journal_t *journal)
247{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400248 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700249 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250
251 while (journal->j_task) {
252 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400253 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000254 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400255 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700256 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400257 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258}
259
260/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700261 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700262 *
263 * Writes a metadata buffer to a given disk block. The actual IO is not
264 * performed but a new buffer_head is constructed which labels the data
265 * to be written with the correct destination disk block.
266 *
267 * Any magic-number escaping which needs to be done will cause a
268 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700269 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700270 * magic number is only written to the log for descripter blocks. In
271 * this case, we copy the data and replace the first word with 0, and we
272 * return a result code which indicates that this buffer needs to be
273 * marked as an escaped buffer in the corresponding log descriptor
274 * block. The missing word can then be restored when the block is read
275 * during recovery.
276 *
277 * If the source buffer has already been modified by a new transaction
278 * since we took the last commit snapshot, we use the frozen copy of
279 * that data for IO. If we end up using the existing buffer_head's data
280 * for the write, then we *have* to lock the buffer to prevent anyone
281 * else from using and possibly modifying it while the IO is in
282 * progress.
283 *
284 * The function returns a pointer to the buffer_heads to be used for IO.
285 *
286 * We assume that the journal has already been locked in this function.
287 *
288 * Return value:
289 * <0: Error
290 * >=0: Finished OK
291 *
292 * On success:
293 * Bit 0 set == escape performed on the data
294 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
295 */
296
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700297int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700298 struct journal_head *jh_in,
299 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700300 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700301{
302 int need_copy_out = 0;
303 int done_copy_out = 0;
304 int do_escape = 0;
305 char *mapped_data;
306 struct buffer_head *new_bh;
307 struct journal_head *new_jh;
308 struct page *new_page;
309 unsigned int new_offset;
310 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400311 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700312
313 /*
314 * The buffer really shouldn't be locked: only the current committing
315 * transaction is allowed to write it, so nobody else is allowed
316 * to do any IO.
317 *
318 * akpm: except if we're journalling data, and write() output is
319 * also part of a shared mapping, and another thread has
320 * decided to launch a writepage() against this buffer.
321 */
322 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
323
Theodore Ts'o47def822010-07-27 11:56:05 -0400324retry_alloc:
325 new_bh = alloc_buffer_head(GFP_NOFS);
326 if (!new_bh) {
327 /*
328 * Failure is not an option, but __GFP_NOFAIL is going
329 * away; so we retry ourselves here.
330 */
331 congestion_wait(BLK_RW_ASYNC, HZ/50);
332 goto retry_alloc;
333 }
334
dingdinghua96577c42009-07-13 17:55:35 -0400335 /* keep subsequent assertions sane */
336 new_bh->b_state = 0;
337 init_buffer(new_bh, NULL, NULL);
338 atomic_set(&new_bh->b_count, 1);
339 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700340
341 /*
342 * If a new transaction has already done a buffer copy-out, then
343 * we use that version of the data for the commit.
344 */
345 jbd_lock_bh_state(bh_in);
346repeat:
347 if (jh_in->b_frozen_data) {
348 done_copy_out = 1;
349 new_page = virt_to_page(jh_in->b_frozen_data);
350 new_offset = offset_in_page(jh_in->b_frozen_data);
351 } else {
352 new_page = jh2bh(jh_in)->b_page;
353 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
354 }
355
Cong Wang303a8f22011-11-25 23:14:31 +0800356 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700357 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200358 * Fire data frozen trigger if data already wasn't frozen. Do this
359 * before checking for escaping, as the trigger may modify the magic
360 * offset. If a copy-out happens afterwards, it will have the correct
361 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700362 */
Jan Kara13ceef02010-07-14 07:56:33 +0200363 if (!done_copy_out)
364 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
365 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700366
367 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700368 * Check for escaping
369 */
370 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700371 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700372 need_copy_out = 1;
373 do_escape = 1;
374 }
Cong Wang303a8f22011-11-25 23:14:31 +0800375 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700376
377 /*
378 * Do we need to do a data copy?
379 */
380 if (need_copy_out && !done_copy_out) {
381 char *tmp;
382
383 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400384 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500385 if (!tmp) {
386 jbd2_journal_put_journal_head(new_jh);
387 return -ENOMEM;
388 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700389 jbd_lock_bh_state(bh_in);
390 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400391 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700392 goto repeat;
393 }
394
395 jh_in->b_frozen_data = tmp;
Cong Wang303a8f22011-11-25 23:14:31 +0800396 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700397 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800398 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700399
400 new_page = virt_to_page(tmp);
401 new_offset = offset_in_page(tmp);
402 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700403
404 /*
405 * This isn't strictly necessary, as we're using frozen
406 * data for the escaping, but it keeps consistency with
407 * b_frozen_data usage.
408 */
409 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700410 }
411
412 /*
413 * Did we need to do an escaping? Now we've done all the
414 * copying, we can finally do so.
415 */
416 if (do_escape) {
Cong Wang303a8f22011-11-25 23:14:31 +0800417 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700418 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang303a8f22011-11-25 23:14:31 +0800419 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420 }
421
Dave Kleikamp470decc2006-10-11 01:20:57 -0700422 set_bh_page(new_bh, new_page, new_offset);
423 new_jh->b_transaction = NULL;
424 new_bh->b_size = jh2bh(jh_in)->b_size;
425 new_bh->b_bdev = transaction->t_journal->j_dev;
426 new_bh->b_blocknr = blocknr;
427 set_buffer_mapped(new_bh);
428 set_buffer_dirty(new_bh);
429
430 *jh_out = new_jh;
431
432 /*
433 * The to-be-written buffer needs to get moved to the io queue,
434 * and the original buffer whose contents we are shadowing or
435 * copying is moved to the transaction's shadow queue.
436 */
437 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400438 spin_lock(&journal->j_list_lock);
439 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
440 spin_unlock(&journal->j_list_lock);
441 jbd_unlock_bh_state(bh_in);
442
Dave Kleikamp470decc2006-10-11 01:20:57 -0700443 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700444 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700445
446 return do_escape | (done_copy_out << 1);
447}
448
449/*
450 * Allocation code for the journal file. Manage the space left in the
451 * journal, so that we can begin checkpointing when appropriate.
452 */
453
454/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700455 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456 *
457 * Called with the journal already locked.
458 *
459 * Called under j_state_lock
460 */
461
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700462int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700463{
464 int left = journal->j_free;
465
Theodore Ts'oa931da62010-08-03 21:35:12 -0400466 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700467
468 /*
469 * Be pessimistic here about the number of those free blocks which
470 * might be required for log descriptor control blocks.
471 */
472
473#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
474
475 left -= MIN_LOG_RESERVED_BLOCKS;
476
477 if (left <= 0)
478 return 0;
479 left -= (left >> 3);
480 return left;
481}
482
483/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500484 * Called with j_state_lock locked for writing.
485 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700487int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700488{
489 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400490 * The only transaction we can possibly wait upon is the
491 * currently running transaction (if it exists). Otherwise,
492 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700493 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400494 if (journal->j_running_transaction &&
495 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200497 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700498 * commit thread. We do _not_ do the commit ourselves.
499 */
500
501 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400502 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700503 journal->j_commit_request,
504 journal->j_commit_sequence);
505 wake_up(&journal->j_wait_commit);
506 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400507 } else if (!tid_geq(journal->j_commit_request, target))
508 /* This should never happen, but if it does, preserve
509 the evidence before kjournald goes into a loop and
510 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400511 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400512 journal->j_commit_request,
513 journal->j_commit_sequence,
514 target, journal->j_running_transaction ?
515 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516 return 0;
517}
518
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700519int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520{
521 int ret;
522
Theodore Ts'oa931da62010-08-03 21:35:12 -0400523 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700524 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400525 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 return ret;
527}
528
529/*
530 * Force and wait upon a commit if the calling process is not within
531 * transaction. This is used for forcing out undo-protected data which contains
532 * bitmaps, when the fs is running out of space.
533 *
534 * We can only force the running transaction if we don't have an active handle;
535 * otherwise, we will deadlock.
536 *
537 * Returns true if a transaction was started.
538 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700539int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540{
541 transaction_t *transaction = NULL;
542 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500543 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544
Theodore Ts'oa931da62010-08-03 21:35:12 -0400545 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700546 if (journal->j_running_transaction && !current->journal_info) {
547 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500548 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
549 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700550 } else if (journal->j_committing_transaction)
551 transaction = journal->j_committing_transaction;
552
553 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400554 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555 return 0; /* Nothing to retry */
556 }
557
558 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400559 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500560 if (need_to_start)
561 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700562 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700563 return 1;
564}
565
566/*
567 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500568 * if a transaction is going to be committed (or is currently already
569 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700571int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700572{
573 int ret = 0;
574
Theodore Ts'oa931da62010-08-03 21:35:12 -0400575 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700576 if (journal->j_running_transaction) {
577 tid_t tid = journal->j_running_transaction->t_tid;
578
Jan Karac88ccea2009-02-10 11:27:46 -0500579 __jbd2_log_start_commit(journal, tid);
580 /* There's a running transaction and we've just made sure
581 * it's commit has been scheduled. */
582 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700583 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500584 ret = 1;
585 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586 /*
587 * If ext3_write_super() recently started a commit, then we
588 * have to wait for completion of that transaction
589 */
Jan Karac88ccea2009-02-10 11:27:46 -0500590 if (ptid)
591 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592 ret = 1;
593 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400594 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595 return ret;
596}
597
598/*
Jan Karabbd2be32011-05-24 11:59:18 -0400599 * Return 1 if a given transaction has not yet sent barrier request
600 * connected with a transaction commit. If 0 is returned, transaction
601 * may or may not have sent the barrier. Used to avoid sending barrier
602 * twice in common cases.
603 */
604int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
605{
606 int ret = 0;
607 transaction_t *commit_trans;
608
609 if (!(journal->j_flags & JBD2_BARRIER))
610 return 0;
611 read_lock(&journal->j_state_lock);
612 /* Transaction already committed? */
613 if (tid_geq(journal->j_commit_sequence, tid))
614 goto out;
615 commit_trans = journal->j_committing_transaction;
616 if (!commit_trans || commit_trans->t_tid != tid) {
617 ret = 1;
618 goto out;
619 }
620 /*
621 * Transaction is being committed and we already proceeded to
622 * submitting a flush to fs partition?
623 */
624 if (journal->j_fs_dev != journal->j_dev) {
625 if (!commit_trans->t_need_data_flush ||
626 commit_trans->t_state >= T_COMMIT_DFLUSH)
627 goto out;
628 } else {
629 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
630 goto out;
631 }
632 ret = 1;
633out:
634 read_unlock(&journal->j_state_lock);
635 return ret;
636}
637EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
638
639/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700640 * Wait for a specified commit to complete.
641 * The caller may not hold the journal lock.
642 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700643int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700644{
645 int err = 0;
646
Theodore Ts'oa931da62010-08-03 21:35:12 -0400647 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400648#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700649 if (!tid_geq(journal->j_commit_request, tid)) {
650 printk(KERN_EMERG
651 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400652 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700653 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700655 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400656 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700657 tid, journal->j_commit_sequence);
658 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400659 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700660 wait_event(journal->j_wait_done_commit,
661 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400662 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700663 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400664 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700665
666 if (unlikely(is_journal_aborted(journal))) {
667 printk(KERN_EMERG "journal commit I/O error\n");
668 err = -EIO;
669 }
670 return err;
671}
672
673/*
674 * Log buffer allocation routines:
675 */
676
Mingming Cao18eba7a2006-10-11 01:21:13 -0700677int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700678{
679 unsigned long blocknr;
680
Theodore Ts'oa931da62010-08-03 21:35:12 -0400681 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700682 J_ASSERT(journal->j_free > 1);
683
684 blocknr = journal->j_head;
685 journal->j_head++;
686 journal->j_free--;
687 if (journal->j_head == journal->j_last)
688 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400689 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700690 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700691}
692
693/*
694 * Conversion of logical to physical block numbers for the journal
695 *
696 * On external journals the journal blocks are identity-mapped, so
697 * this is a no-op. If needed, we can use j_blk_offset - everything is
698 * ready.
699 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700700int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700701 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700702{
703 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700704 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700705
706 if (journal->j_inode) {
707 ret = bmap(journal->j_inode, blocknr);
708 if (ret)
709 *retp = ret;
710 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700711 printk(KERN_ALERT "%s: journal block not found "
712 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400713 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700714 err = -EIO;
715 __journal_abort_soft(journal, err);
716 }
717 } else {
718 *retp = blocknr; /* +journal->j_blk_offset */
719 }
720 return err;
721}
722
723/*
724 * We play buffer_head aliasing tricks to write data/metadata blocks to
725 * the journal without copying their contents, but for journal
726 * descriptor blocks we do need to generate bona fide buffers.
727 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700728 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700729 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
730 * But we don't bother doing that, so there will be coherency problems with
731 * mmaps of blockdevs which hold live JBD-controlled filesystems.
732 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700733struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700734{
735 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700736 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700737 int err;
738
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700739 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700740
741 if (err)
742 return NULL;
743
744 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500745 if (!bh)
746 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700747 lock_buffer(bh);
748 memset(bh->b_data, 0, journal->j_blocksize);
749 set_buffer_uptodate(bh);
750 unlock_buffer(bh);
751 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700752 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700753}
754
Jan Kara79feb522012-03-13 22:22:54 -0400755/*
756 * Return tid of the oldest transaction in the journal and block in the journal
757 * where the transaction starts.
758 *
759 * If the journal is now empty, return which will be the next transaction ID
760 * we will write and where will that transaction start.
761 *
762 * The return value is 0 if journal tail cannot be pushed any further, 1 if
763 * it can.
764 */
765int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
766 unsigned long *block)
767{
768 transaction_t *transaction;
769 int ret;
770
771 read_lock(&journal->j_state_lock);
772 spin_lock(&journal->j_list_lock);
773 transaction = journal->j_checkpoint_transactions;
774 if (transaction) {
775 *tid = transaction->t_tid;
776 *block = transaction->t_log_start;
777 } else if ((transaction = journal->j_committing_transaction) != NULL) {
778 *tid = transaction->t_tid;
779 *block = transaction->t_log_start;
780 } else if ((transaction = journal->j_running_transaction) != NULL) {
781 *tid = transaction->t_tid;
782 *block = journal->j_head;
783 } else {
784 *tid = journal->j_transaction_sequence;
785 *block = journal->j_head;
786 }
787 ret = tid_gt(*tid, journal->j_tail_sequence);
788 spin_unlock(&journal->j_list_lock);
789 read_unlock(&journal->j_state_lock);
790
791 return ret;
792}
793
794/*
795 * Update information in journal structure and in on disk journal superblock
796 * about log tail. This function does not check whether information passed in
797 * really pushes log tail further. It's responsibility of the caller to make
798 * sure provided log tail information is valid (e.g. by holding
799 * j_checkpoint_mutex all the time between computing log tail and calling this
800 * function as is the case with jbd2_cleanup_journal_tail()).
801 *
802 * Requires j_checkpoint_mutex
803 */
804void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
805{
806 unsigned long freed;
807
808 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
809
810 /*
811 * We cannot afford for write to remain in drive's caches since as
812 * soon as we update j_tail, next transaction can start reusing journal
813 * space and if we lose sb update during power failure we'd replay
814 * old transaction with possibly newly overwritten data.
815 */
816 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
817 write_lock(&journal->j_state_lock);
818 freed = block - journal->j_tail;
819 if (block < journal->j_tail)
820 freed += journal->j_last - journal->j_first;
821
822 trace_jbd2_update_log_tail(journal, tid, block, freed);
823 jbd_debug(1,
824 "Cleaning journal tail from %d to %d (offset %lu), "
825 "freeing %lu\n",
826 journal->j_tail_sequence, tid, block, freed);
827
828 journal->j_free += freed;
829 journal->j_tail_sequence = tid;
830 journal->j_tail = block;
831 write_unlock(&journal->j_state_lock);
832}
833
Jan Kara33395782012-03-13 22:45:38 -0400834/*
835 * This is a variaon of __jbd2_update_log_tail which checks for validity of
836 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
837 * with other threads updating log tail.
838 */
839void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
840{
841 mutex_lock(&journal->j_checkpoint_mutex);
842 if (tid_gt(tid, journal->j_tail_sequence))
843 __jbd2_update_log_tail(journal, tid, block);
844 mutex_unlock(&journal->j_checkpoint_mutex);
845}
846
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500847struct jbd2_stats_proc_session {
848 journal_t *journal;
849 struct transaction_stats_s *stats;
850 int start;
851 int max;
852};
853
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500854static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
855{
856 return *pos ? NULL : SEQ_START_TOKEN;
857}
858
859static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
860{
861 return NULL;
862}
863
864static int jbd2_seq_info_show(struct seq_file *seq, void *v)
865{
866 struct jbd2_stats_proc_session *s = seq->private;
867
868 if (v != SEQ_START_TOKEN)
869 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400870 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500871 s->stats->ts_tid,
872 s->journal->j_max_transaction_buffers);
873 if (s->stats->ts_tid == 0)
874 return 0;
875 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400876 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500877 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400878 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500879 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400880 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500881 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400882 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500883 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400884 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500885 seq_printf(seq, " %lluus average transaction commit time\n",
886 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500887 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400888 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500889 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400890 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500891 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400892 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500893 return 0;
894}
895
896static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
897{
898}
899
James Morris88e9d342009-09-22 16:43:43 -0700900static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500901 .start = jbd2_seq_info_start,
902 .next = jbd2_seq_info_next,
903 .stop = jbd2_seq_info_stop,
904 .show = jbd2_seq_info_show,
905};
906
907static int jbd2_seq_info_open(struct inode *inode, struct file *file)
908{
909 journal_t *journal = PDE(inode)->data;
910 struct jbd2_stats_proc_session *s;
911 int rc, size;
912
913 s = kmalloc(sizeof(*s), GFP_KERNEL);
914 if (s == NULL)
915 return -ENOMEM;
916 size = sizeof(struct transaction_stats_s);
917 s->stats = kmalloc(size, GFP_KERNEL);
918 if (s->stats == NULL) {
919 kfree(s);
920 return -ENOMEM;
921 }
922 spin_lock(&journal->j_history_lock);
923 memcpy(s->stats, &journal->j_stats, size);
924 s->journal = journal;
925 spin_unlock(&journal->j_history_lock);
926
927 rc = seq_open(file, &jbd2_seq_info_ops);
928 if (rc == 0) {
929 struct seq_file *m = file->private_data;
930 m->private = s;
931 } else {
932 kfree(s->stats);
933 kfree(s);
934 }
935 return rc;
936
937}
938
939static int jbd2_seq_info_release(struct inode *inode, struct file *file)
940{
941 struct seq_file *seq = file->private_data;
942 struct jbd2_stats_proc_session *s = seq->private;
943 kfree(s->stats);
944 kfree(s);
945 return seq_release(inode, file);
946}
947
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700948static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500949 .owner = THIS_MODULE,
950 .open = jbd2_seq_info_open,
951 .read = seq_read,
952 .llseek = seq_lseek,
953 .release = jbd2_seq_info_release,
954};
955
956static struct proc_dir_entry *proc_jbd2_stats;
957
958static void jbd2_stats_proc_init(journal_t *journal)
959{
Theodore Ts'o05496762008-09-16 14:36:17 -0400960 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500961 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700962 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
963 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500964 }
965}
966
967static void jbd2_stats_proc_exit(journal_t *journal)
968{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500969 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400970 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500971}
972
Dave Kleikamp470decc2006-10-11 01:20:57 -0700973/*
974 * Management for journal control blocks: functions to create and
975 * destroy journal_t structures, and to initialise and read existing
976 * journal blocks from disk. */
977
978/* First: create and setup a journal_t object in memory. We initialise
979 * very few fields yet: that has to wait until we have created the
980 * journal structures from from scratch, or loaded them from disk. */
981
982static journal_t * journal_init_common (void)
983{
984 journal_t *journal;
985 int err;
986
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500987 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700988 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500989 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700990
991 init_waitqueue_head(&journal->j_wait_transaction_locked);
992 init_waitqueue_head(&journal->j_wait_logspace);
993 init_waitqueue_head(&journal->j_wait_done_commit);
994 init_waitqueue_head(&journal->j_wait_checkpoint);
995 init_waitqueue_head(&journal->j_wait_commit);
996 init_waitqueue_head(&journal->j_wait_updates);
997 mutex_init(&journal->j_barrier);
998 mutex_init(&journal->j_checkpoint_mutex);
999 spin_lock_init(&journal->j_revoke_lock);
1000 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001001 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002
Mingming Caocd02ff02007-10-16 18:38:25 -04001003 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -05001004 journal->j_min_batch_time = 0;
1005 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001006
1007 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001008 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001009
1010 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001011 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001012 if (err) {
1013 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001014 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001016
Theodore Ts'obf699322009-09-30 00:32:06 -04001017 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001018
Dave Kleikamp470decc2006-10-11 01:20:57 -07001019 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001020}
1021
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001022/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001023 *
1024 * Create a journal structure assigned some fixed set of disk blocks to
1025 * the journal. We don't actually touch those disk blocks yet, but we
1026 * need to set up all of the mapping information to tell the journaling
1027 * system where the journal blocks are.
1028 *
1029 */
1030
1031/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001032 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001033 * @bdev: Block device on which to create the journal
1034 * @fs_dev: Device which hold journalled filesystem for this journal.
1035 * @start: Block nr Start of journal.
1036 * @len: Length of the journal in blocks.
1037 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001038 *
1039 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001041 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001042 * range of blocks on an arbitrary block device.
1043 *
1044 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001045journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001047 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001048{
1049 journal_t *journal = journal_init_common();
1050 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001051 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001052 int n;
1053
1054 if (!journal)
1055 return NULL;
1056
1057 /* journal descriptor can store up to n blocks -bzzz */
1058 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001059 journal->j_dev = bdev;
1060 journal->j_fs_dev = fs_dev;
1061 journal->j_blk_offset = start;
1062 journal->j_maxlen = len;
1063 bdevname(journal->j_dev, journal->j_devname);
1064 p = journal->j_devname;
1065 while ((p = strchr(p, '/')))
1066 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001067 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001068 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1069 journal->j_wbufsize = n;
1070 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1071 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001072 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001073 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001074 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001075 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001076
1077 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001078 if (!bh) {
1079 printk(KERN_ERR
1080 "%s: Cannot get buffer for journal superblock\n",
1081 __func__);
1082 goto out_err;
1083 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001084 journal->j_sb_buffer = bh;
1085 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001086
Dave Kleikamp470decc2006-10-11 01:20:57 -07001087 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001088out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001089 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001090 jbd2_stats_proc_exit(journal);
1091 kfree(journal);
1092 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001093}
1094
1095/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001096 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001097 * @inode: An inode to create the journal in
1098 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001099 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001100 * the journal. The inode must exist already, must support bmap() and
1101 * must have all data blocks preallocated.
1102 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001103journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104{
1105 struct buffer_head *bh;
1106 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001107 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001108 int err;
1109 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001110 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001111
1112 if (!journal)
1113 return NULL;
1114
1115 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1116 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001117 bdevname(journal->j_dev, journal->j_devname);
1118 p = journal->j_devname;
1119 while ((p = strchr(p, '/')))
1120 *p = '!';
1121 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001122 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001123 jbd_debug(1,
1124 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1125 journal, inode->i_sb->s_id, inode->i_ino,
1126 (long long) inode->i_size,
1127 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1128
1129 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1130 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001131 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001132
1133 /* journal descriptor can store up to n blocks -bzzz */
1134 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1135 journal->j_wbufsize = n;
1136 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1137 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001138 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001139 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001140 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001141 }
1142
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001143 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001144 /* If that failed, give up */
1145 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001146 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001147 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001148 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001149 }
1150
1151 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001152 if (!bh) {
1153 printk(KERN_ERR
1154 "%s: Cannot get buffer for journal superblock\n",
1155 __func__);
1156 goto out_err;
1157 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001158 journal->j_sb_buffer = bh;
1159 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1160
1161 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001162out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001163 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001164 jbd2_stats_proc_exit(journal);
1165 kfree(journal);
1166 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001167}
1168
1169/*
1170 * If the journal init or create aborts, we need to mark the journal
1171 * superblock as being NULL to prevent the journal destroy from writing
1172 * back a bogus superblock.
1173 */
1174static void journal_fail_superblock (journal_t *journal)
1175{
1176 struct buffer_head *bh = journal->j_sb_buffer;
1177 brelse(bh);
1178 journal->j_sb_buffer = NULL;
1179}
1180
1181/*
1182 * Given a journal_t structure, initialise the various fields for
1183 * startup of a new journaling session. We use this both when creating
1184 * a journal, and after recovering an old journal to reset it for
1185 * subsequent use.
1186 */
1187
1188static int journal_reset(journal_t *journal)
1189{
1190 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001191 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001192
1193 first = be32_to_cpu(sb->s_first);
1194 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001195 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001196 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001197 first, last);
1198 journal_fail_superblock(journal);
1199 return -EINVAL;
1200 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001201
1202 journal->j_first = first;
1203 journal->j_last = last;
1204
1205 journal->j_head = first;
1206 journal->j_tail = first;
1207 journal->j_free = last - first;
1208
1209 journal->j_tail_sequence = journal->j_transaction_sequence;
1210 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1211 journal->j_commit_request = journal->j_commit_sequence;
1212
1213 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1214
Dave Kleikamp470decc2006-10-11 01:20:57 -07001215 /*
1216 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001217 * no recovery (s_start == 0), then we can safely defer the superblock
1218 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001219 * attempting a write to a potential-readonly device.
1220 */
Jan Kara24bcc892012-03-13 15:41:04 -04001221 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001222 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001223 "(start %ld, seq %d, errno %d)\n",
1224 journal->j_tail, journal->j_tail_sequence,
1225 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001226 journal->j_flags |= JBD2_FLUSHED;
1227 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001228 /* Lock here to make assertions happy... */
1229 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001230 /*
1231 * Update log tail information. We use WRITE_FUA since new
1232 * transaction will start reusing journal space and so we
1233 * must make sure information about current log tail is on
1234 * disk before that.
1235 */
1236 jbd2_journal_update_sb_log_tail(journal,
1237 journal->j_tail_sequence,
1238 journal->j_tail,
1239 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001240 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001241 }
Jan Kara24bcc892012-03-13 15:41:04 -04001242 return jbd2_journal_start_thread(journal);
1243}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001244
Jan Kara79feb522012-03-13 22:22:54 -04001245static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001246{
1247 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001248 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001249
Jan Kara79feb522012-03-13 22:22:54 -04001250 trace_jbd2_write_superblock(journal, write_op);
1251 if (!(journal->j_flags & JBD2_BARRIER))
1252 write_op &= ~(REQ_FUA | REQ_FLUSH);
1253 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001254 if (buffer_write_io_error(bh)) {
1255 /*
1256 * Oh, dear. A previous attempt to write the journal
1257 * superblock failed. This could happen because the
1258 * USB device was yanked out. Or it could happen to
1259 * be a transient write error and maybe the block will
1260 * be remapped. Nothing we can do but to retry the
1261 * write and hope for the best.
1262 */
1263 printk(KERN_ERR "JBD2: previous I/O error detected "
1264 "for journal superblock update for %s.\n",
1265 journal->j_devname);
1266 clear_buffer_write_io_error(bh);
1267 set_buffer_uptodate(bh);
1268 }
Jan Kara79feb522012-03-13 22:22:54 -04001269 get_bh(bh);
1270 bh->b_end_io = end_buffer_write_sync;
1271 ret = submit_bh(write_op, bh);
1272 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001273 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001274 clear_buffer_write_io_error(bh);
1275 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001276 ret = -EIO;
1277 }
1278 if (ret) {
1279 printk(KERN_ERR "JBD2: Error %d detected when updating "
1280 "journal superblock for %s.\n", ret,
1281 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001282 }
1283}
Theodore Ts'o914258b2008-10-06 21:35:40 -04001284
Jan Kara24bcc892012-03-13 15:41:04 -04001285/**
1286 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1287 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001288 * @tail_tid: TID of the new transaction at the tail of the log
1289 * @tail_block: The first block of the transaction at the tail of the log
1290 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001291 *
1292 * Update a journal's superblock information about log tail and write it to
1293 * disk, waiting for the IO to complete.
1294 */
Jan Kara79feb522012-03-13 22:22:54 -04001295void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1296 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001297{
1298 journal_superblock_t *sb = journal->j_superblock;
1299
Jan Karaa78bb112012-03-13 15:43:04 -04001300 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001301 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1302 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001303
Jan Kara79feb522012-03-13 22:22:54 -04001304 sb->s_sequence = cpu_to_be32(tail_tid);
1305 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001306
Jan Kara79feb522012-03-13 22:22:54 -04001307 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001308
1309 /* Log is no longer empty */
1310 write_lock(&journal->j_state_lock);
1311 WARN_ON(!sb->s_sequence);
1312 journal->j_flags &= ~JBD2_FLUSHED;
1313 write_unlock(&journal->j_state_lock);
1314}
1315
1316/**
1317 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1318 * @journal: The journal to update.
1319 *
1320 * Update a journal's dynamic superblock fields to show that journal is empty.
1321 * Write updated superblock to disk waiting for IO to complete.
1322 */
1323static void jbd2_mark_journal_empty(journal_t *journal)
1324{
1325 journal_superblock_t *sb = journal->j_superblock;
1326
Jan Karaa78bb112012-03-13 15:43:04 -04001327 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001328 read_lock(&journal->j_state_lock);
Jan Kara24bcc892012-03-13 15:41:04 -04001329 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1330 journal->j_tail_sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001331
1332 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
Jan Kara24bcc892012-03-13 15:41:04 -04001333 sb->s_start = cpu_to_be32(0);
1334 read_unlock(&journal->j_state_lock);
1335
Jan Kara79feb522012-03-13 22:22:54 -04001336 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001337
1338 /* Log is no longer empty */
1339 write_lock(&journal->j_state_lock);
1340 journal->j_flags |= JBD2_FLUSHED;
1341 write_unlock(&journal->j_state_lock);
1342}
1343
1344
1345/**
1346 * jbd2_journal_update_sb_errno() - Update error in the journal.
1347 * @journal: The journal to update.
1348 *
1349 * Update a journal's errno. Write updated superblock to disk waiting for IO
1350 * to complete.
1351 */
1352static void jbd2_journal_update_sb_errno(journal_t *journal)
1353{
1354 journal_superblock_t *sb = journal->j_superblock;
1355
1356 read_lock(&journal->j_state_lock);
1357 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1358 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001359 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001360 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001361
Jan Kara79feb522012-03-13 22:22:54 -04001362 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001363}
1364
1365/*
1366 * Read the superblock for a given journal, performing initial
1367 * validation of the format.
1368 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001369static int journal_get_superblock(journal_t *journal)
1370{
1371 struct buffer_head *bh;
1372 journal_superblock_t *sb;
1373 int err = -EIO;
1374
1375 bh = journal->j_sb_buffer;
1376
1377 J_ASSERT(bh != NULL);
1378 if (!buffer_uptodate(bh)) {
1379 ll_rw_block(READ, 1, &bh);
1380 wait_on_buffer(bh);
1381 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001382 printk(KERN_ERR
1383 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001384 goto out;
1385 }
1386 }
1387
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001388 if (buffer_verified(bh))
1389 return 0;
1390
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391 sb = journal->j_superblock;
1392
1393 err = -EINVAL;
1394
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001395 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001396 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001397 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001398 goto out;
1399 }
1400
1401 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001402 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 journal->j_format_version = 1;
1404 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001405 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 journal->j_format_version = 2;
1407 break;
1408 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001409 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001410 goto out;
1411 }
1412
1413 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1414 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1415 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001416 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001417 goto out;
1418 }
1419
Eryu Guan87622022011-11-01 19:04:59 -04001420 if (be32_to_cpu(sb->s_first) == 0 ||
1421 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1422 printk(KERN_WARNING
1423 "JBD2: Invalid start block of journal: %u\n",
1424 be32_to_cpu(sb->s_first));
1425 goto out;
1426 }
1427
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001428 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1429 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1430 /* Can't have checksum v1 and v2 on at the same time! */
1431 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1432 "at the same time!\n");
1433 goto out;
1434 }
1435
1436 if (!jbd2_verify_csum_type(journal, sb)) {
1437 printk(KERN_ERR "JBD: Unknown checksum type\n");
1438 goto out;
1439 }
1440
1441 set_buffer_verified(bh);
1442
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443 return 0;
1444
1445out:
1446 journal_fail_superblock(journal);
1447 return err;
1448}
1449
1450/*
1451 * Load the on-disk journal superblock and read the key fields into the
1452 * journal_t.
1453 */
1454
1455static int load_superblock(journal_t *journal)
1456{
1457 int err;
1458 journal_superblock_t *sb;
1459
1460 err = journal_get_superblock(journal);
1461 if (err)
1462 return err;
1463
1464 sb = journal->j_superblock;
1465
1466 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1467 journal->j_tail = be32_to_cpu(sb->s_start);
1468 journal->j_first = be32_to_cpu(sb->s_first);
1469 journal->j_last = be32_to_cpu(sb->s_maxlen);
1470 journal->j_errno = be32_to_cpu(sb->s_errno);
1471
1472 return 0;
1473}
1474
1475
1476/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001477 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001478 * @journal: Journal to act on.
1479 *
1480 * Given a journal_t structure which tells us which disk blocks contain
1481 * a journal, read the journal from disk to initialise the in-memory
1482 * structures.
1483 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001484int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001485{
1486 int err;
1487 journal_superblock_t *sb;
1488
1489 err = load_superblock(journal);
1490 if (err)
1491 return err;
1492
1493 sb = journal->j_superblock;
1494 /* If this is a V2 superblock, then we have to check the
1495 * features flags on it. */
1496
1497 if (journal->j_format_version >= 2) {
1498 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001499 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001501 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001502 printk(KERN_WARNING
1503 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001504 return -EINVAL;
1505 }
1506 }
1507
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001508 /*
1509 * Create a slab for this blocksize
1510 */
1511 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1512 if (err)
1513 return err;
1514
Dave Kleikamp470decc2006-10-11 01:20:57 -07001515 /* Let the recovery code check whether it needs to recover any
1516 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001517 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001518 goto recovery_error;
1519
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001520 if (journal->j_failed_commit) {
1521 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1522 "is corrupt.\n", journal->j_failed_commit,
1523 journal->j_devname);
1524 return -EIO;
1525 }
1526
Dave Kleikamp470decc2006-10-11 01:20:57 -07001527 /* OK, we've finished with the dynamic journal bits:
1528 * reinitialise the dynamic contents of the superblock in memory
1529 * and reset them on disk. */
1530 if (journal_reset(journal))
1531 goto recovery_error;
1532
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001533 journal->j_flags &= ~JBD2_ABORT;
1534 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001535 return 0;
1536
1537recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001538 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001539 return -EIO;
1540}
1541
1542/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001543 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001544 * @journal: Journal to act on.
1545 *
1546 * Release a journal_t structure once it is no longer in use by the
1547 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001548 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001549 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001550int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001551{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001552 int err = 0;
1553
Dave Kleikamp470decc2006-10-11 01:20:57 -07001554 /* Wait for the commit thread to wake up and die. */
1555 journal_kill_thread(journal);
1556
1557 /* Force a final log commit */
1558 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001559 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001560
1561 /* Force any old transactions to disk */
1562
1563 /* Totally anal locking here... */
1564 spin_lock(&journal->j_list_lock);
1565 while (journal->j_checkpoint_transactions != NULL) {
1566 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001567 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001568 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001569 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 spin_lock(&journal->j_list_lock);
1571 }
1572
1573 J_ASSERT(journal->j_running_transaction == NULL);
1574 J_ASSERT(journal->j_committing_transaction == NULL);
1575 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1576 spin_unlock(&journal->j_list_lock);
1577
Dave Kleikamp470decc2006-10-11 01:20:57 -07001578 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001579 if (!is_journal_aborted(journal)) {
Jan Karaa78bb112012-03-13 15:43:04 -04001580 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001581 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001582 mutex_unlock(&journal->j_checkpoint_mutex);
1583 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001584 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001585 brelse(journal->j_sb_buffer);
1586 }
1587
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001588 if (journal->j_proc_entry)
1589 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001590 if (journal->j_inode)
1591 iput(journal->j_inode);
1592 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001593 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001594 kfree(journal->j_wbuf);
1595 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001596
1597 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001598}
1599
1600
1601/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001602 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001603 * @journal: Journal to check.
1604 * @compat: bitmask of compatible features
1605 * @ro: bitmask of features that force read-only mount
1606 * @incompat: bitmask of incompatible features
1607 *
1608 * Check whether the journal uses all of a given set of
1609 * features. Return true (non-zero) if it does.
1610 **/
1611
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001612int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001613 unsigned long ro, unsigned long incompat)
1614{
1615 journal_superblock_t *sb;
1616
1617 if (!compat && !ro && !incompat)
1618 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001619 /* Load journal superblock if it is not loaded yet. */
1620 if (journal->j_format_version == 0 &&
1621 journal_get_superblock(journal) != 0)
1622 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001623 if (journal->j_format_version == 1)
1624 return 0;
1625
1626 sb = journal->j_superblock;
1627
1628 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1629 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1630 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1631 return 1;
1632
1633 return 0;
1634}
1635
1636/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001637 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001638 * @journal: Journal to check.
1639 * @compat: bitmask of compatible features
1640 * @ro: bitmask of features that force read-only mount
1641 * @incompat: bitmask of incompatible features
1642 *
1643 * Check whether the journaling code supports the use of
1644 * all of a given set of features on this journal. Return true
1645 * (non-zero) if it can. */
1646
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001647int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001648 unsigned long ro, unsigned long incompat)
1649{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 if (!compat && !ro && !incompat)
1651 return 1;
1652
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 /* We can support any known requested features iff the
1654 * superblock is in version 2. Otherwise we fail to support any
1655 * extended sb features. */
1656
1657 if (journal->j_format_version != 2)
1658 return 0;
1659
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001660 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1661 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1662 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001663 return 1;
1664
1665 return 0;
1666}
1667
1668/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001669 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670 * @journal: Journal to act on.
1671 * @compat: bitmask of compatible features
1672 * @ro: bitmask of features that force read-only mount
1673 * @incompat: bitmask of incompatible features
1674 *
1675 * Mark a given journal feature as present on the
1676 * superblock. Returns true if the requested features could be set.
1677 *
1678 */
1679
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001680int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001681 unsigned long ro, unsigned long incompat)
1682{
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001683#define INCOMPAT_FEATURE_ON(f) \
1684 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1685#define COMPAT_FEATURE_ON(f) \
1686 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001687 journal_superblock_t *sb;
1688
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001689 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001690 return 1;
1691
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693 return 0;
1694
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001695 /* Asking for checksumming v2 and v1? Only give them v2. */
1696 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1697 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1698 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1699
Dave Kleikamp470decc2006-10-11 01:20:57 -07001700 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1701 compat, ro, incompat);
1702
1703 sb = journal->j_superblock;
1704
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001705 /* If enabling v2 checksums, update superblock */
1706 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1707 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1708 sb->s_feature_compat &=
1709 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
1710 }
1711
1712 /* If enabling v1 checksums, downgrade superblock */
1713 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1714 sb->s_feature_incompat &=
1715 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1716
Dave Kleikamp470decc2006-10-11 01:20:57 -07001717 sb->s_feature_compat |= cpu_to_be32(compat);
1718 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1719 sb->s_feature_incompat |= cpu_to_be32(incompat);
1720
1721 return 1;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001722#undef COMPAT_FEATURE_ON
1723#undef INCOMPAT_FEATURE_ON
Dave Kleikamp470decc2006-10-11 01:20:57 -07001724}
1725
Girish Shilamkar818d2762008-01-28 23:58:27 -05001726/*
1727 * jbd2_journal_clear_features () - Clear a given journal feature in the
1728 * superblock
1729 * @journal: Journal to act on.
1730 * @compat: bitmask of compatible features
1731 * @ro: bitmask of features that force read-only mount
1732 * @incompat: bitmask of incompatible features
1733 *
1734 * Clear a given journal feature as present on the
1735 * superblock.
1736 */
1737void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1738 unsigned long ro, unsigned long incompat)
1739{
1740 journal_superblock_t *sb;
1741
1742 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1743 compat, ro, incompat);
1744
1745 sb = journal->j_superblock;
1746
1747 sb->s_feature_compat &= ~cpu_to_be32(compat);
1748 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1749 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1750}
1751EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752
1753/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001754 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001755 * @journal: Journal to act on.
1756 *
1757 * Flush all data for a given journal to disk and empty the journal.
1758 * Filesystems can use this when remounting readonly to ensure that
1759 * recovery does not need to happen on remount.
1760 */
1761
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001762int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001763{
1764 int err = 0;
1765 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766
Theodore Ts'oa931da62010-08-03 21:35:12 -04001767 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001768
1769 /* Force everything buffered to the log... */
1770 if (journal->j_running_transaction) {
1771 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001772 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001773 } else if (journal->j_committing_transaction)
1774 transaction = journal->j_committing_transaction;
1775
1776 /* Wait for the log commit to complete... */
1777 if (transaction) {
1778 tid_t tid = transaction->t_tid;
1779
Theodore Ts'oa931da62010-08-03 21:35:12 -04001780 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001781 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001782 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001783 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 }
1785
1786 /* ...and flush everything in the log out to disk. */
1787 spin_lock(&journal->j_list_lock);
1788 while (!err && journal->j_checkpoint_transactions != NULL) {
1789 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001790 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001791 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001792 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 spin_lock(&journal->j_list_lock);
1794 }
1795 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001796
1797 if (is_journal_aborted(journal))
1798 return -EIO;
1799
Jan Karaa78bb112012-03-13 15:43:04 -04001800 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001801 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802
1803 /* Finally, mark the journal as really needing no recovery.
1804 * This sets s_start==0 in the underlying superblock, which is
1805 * the magic code for a fully-recovered superblock. Any future
1806 * commits of data to the journal will restore the current
1807 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001808 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001809 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001810 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 J_ASSERT(!journal->j_running_transaction);
1812 J_ASSERT(!journal->j_committing_transaction);
1813 J_ASSERT(!journal->j_checkpoint_transactions);
1814 J_ASSERT(journal->j_head == journal->j_tail);
1815 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001816 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001817 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001818}
1819
1820/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001821 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822 * @journal: Journal to act on.
1823 * @write: flag (see below)
1824 *
1825 * Wipe out all of the contents of a journal, safely. This will produce
1826 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001827 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828 *
1829 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1830 * we merely suppress recovery.
1831 */
1832
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001833int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001834{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001835 int err = 0;
1836
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001837 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001838
1839 err = load_superblock(journal);
1840 if (err)
1841 return err;
1842
Dave Kleikamp470decc2006-10-11 01:20:57 -07001843 if (!journal->j_tail)
1844 goto no_recovery;
1845
Eryu Guanf2a44522011-11-01 19:09:18 -04001846 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001847 write ? "Clearing" : "Ignoring");
1848
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001849 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001850 if (write) {
1851 /* Lock to make assertions happy... */
1852 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001853 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001854 mutex_unlock(&journal->j_checkpoint_mutex);
1855 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001856
1857 no_recovery:
1858 return err;
1859}
1860
1861/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 * Journal abort has very specific semantics, which we describe
1863 * for journal abort.
1864 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001865 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866 * itself are here.
1867 */
1868
1869/*
1870 * Quick version for internal journal use (doesn't lock the journal).
1871 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1872 * and don't attempt to make any other journal updates.
1873 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001874void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001875{
1876 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001877
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001878 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001879 return;
1880
1881 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001882 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883
Theodore Ts'oa931da62010-08-03 21:35:12 -04001884 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001885 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001886 transaction = journal->j_running_transaction;
1887 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001888 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001889 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890}
1891
1892/* Soft abort: record the abort error status in the journal superblock,
1893 * but don't do any other IO. */
1894static void __journal_abort_soft (journal_t *journal, int errno)
1895{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001896 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001897 return;
1898
1899 if (!journal->j_errno)
1900 journal->j_errno = errno;
1901
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001902 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001903
1904 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04001905 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001906}
1907
1908/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 * @journal: the journal to shutdown.
1911 * @errno: an error number to record in the journal indicating
1912 * the reason for the shutdown.
1913 *
1914 * Perform a complete, immediate shutdown of the ENTIRE
1915 * journal (not of a single transaction). This operation cannot be
1916 * undone without closing and reopening the journal.
1917 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1920 * mode.
1921 *
1922 * Journal abort has very specific semantics. Any existing dirty,
1923 * unjournaled buffers in the main filesystem will still be written to
1924 * disk by bdflush, but the journaling mechanism will be suspended
1925 * immediately and no further transaction commits will be honoured.
1926 *
1927 * Any dirty, journaled buffers will be written back to disk without
1928 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1929 * filesystem, but we _do_ attempt to leave as much data as possible
1930 * behind for fsck to use for cleanup.
1931 *
1932 * Any attempt to get a new transaction handle on a journal which is in
1933 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001934 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001935 * entered abort state during the update.
1936 *
1937 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001938 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001939 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001940 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001941 * which will be recorded (if possible) in the journal superblock. This
1942 * allows a client to record failure conditions in the middle of a
1943 * transaction without having to complete the transaction to record the
1944 * failure to disk. ext3_error, for example, now uses this
1945 * functionality.
1946 *
1947 * Errors which originate from within the journaling layer will NOT
1948 * supply an errno; a null errno implies that absolutely no further
1949 * writes are done to the journal (unless there are any already in
1950 * progress).
1951 *
1952 */
1953
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001954void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955{
1956 __journal_abort_soft(journal, errno);
1957}
1958
1959/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001960 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961 * @journal: journal to examine.
1962 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001963 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964 * time the journal was mounted - if the journal was stopped
1965 * without calling abort this will be 0.
1966 *
1967 * If the journal has been aborted on this mount time -EROFS will
1968 * be returned.
1969 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001970int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971{
1972 int err;
1973
Theodore Ts'oa931da62010-08-03 21:35:12 -04001974 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001975 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001976 err = -EROFS;
1977 else
1978 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001979 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001980 return err;
1981}
1982
1983/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001984 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001985 * @journal: journal to act on.
1986 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001987 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001988 * mode.
1989 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001990int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991{
1992 int err = 0;
1993
Theodore Ts'oa931da62010-08-03 21:35:12 -04001994 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001995 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001996 err = -EROFS;
1997 else
1998 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001999 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000 return err;
2001}
2002
2003/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002004 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002005 * @journal: journal to act on.
2006 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002007 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002008 * mode.
2009 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002010void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011{
Theodore Ts'oa931da62010-08-03 21:35:12 -04002012 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002014 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002015 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016}
2017
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002018int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002019{
2020 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2021}
2022
2023/*
Zach Brownb517bea2006-10-11 01:21:08 -07002024 * helper functions to deal with 32 or 64bit block numbers.
2025 */
2026size_t journal_tag_bytes(journal_t *journal)
2027{
2028 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04002029 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07002030 else
Mingming Caocd02ff02007-10-16 18:38:25 -04002031 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07002032}
2033
2034/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002035 * JBD memory management
2036 *
2037 * These functions are used to allocate block-sized chunks of memory
2038 * used for making copies of buffer_head data. Very often it will be
2039 * page-sized chunks of data, but sometimes it will be in
2040 * sub-page-size chunks. (For example, 16k pages on Power systems
2041 * with a 4k block file system.) For blocks smaller than a page, we
2042 * use a SLAB allocator. There are slab caches for each block size,
2043 * which are allocated at mount time, if necessary, and we only free
2044 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2045 * this reason we don't need to a mutex to protect access to
2046 * jbd2_slab[] allocating or releasing memory; only in
2047 * jbd2_journal_create_slab().
2048 */
2049#define JBD2_MAX_SLABS 8
2050static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002051
2052static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2053 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2054 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2055};
2056
2057
2058static void jbd2_journal_destroy_slabs(void)
2059{
2060 int i;
2061
2062 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2063 if (jbd2_slab[i])
2064 kmem_cache_destroy(jbd2_slab[i]);
2065 jbd2_slab[i] = NULL;
2066 }
2067}
2068
2069static int jbd2_journal_create_slab(size_t size)
2070{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002071 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002072 int i = order_base_2(size) - 10;
2073 size_t slab_size;
2074
2075 if (size == PAGE_SIZE)
2076 return 0;
2077
2078 if (i >= JBD2_MAX_SLABS)
2079 return -EINVAL;
2080
2081 if (unlikely(i < 0))
2082 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002083 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002084 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002085 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002086 return 0; /* Already created */
2087 }
2088
2089 slab_size = 1 << (i+10);
2090 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2091 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002092 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002093 if (!jbd2_slab[i]) {
2094 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2095 return -ENOMEM;
2096 }
2097 return 0;
2098}
2099
2100static struct kmem_cache *get_slab(size_t size)
2101{
2102 int i = order_base_2(size) - 10;
2103
2104 BUG_ON(i >= JBD2_MAX_SLABS);
2105 if (unlikely(i < 0))
2106 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002107 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002108 return jbd2_slab[i];
2109}
2110
2111void *jbd2_alloc(size_t size, gfp_t flags)
2112{
2113 void *ptr;
2114
2115 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2116
2117 flags |= __GFP_REPEAT;
2118 if (size == PAGE_SIZE)
2119 ptr = (void *)__get_free_pages(flags, 0);
2120 else if (size > PAGE_SIZE) {
2121 int order = get_order(size);
2122
2123 if (order < 3)
2124 ptr = (void *)__get_free_pages(flags, order);
2125 else
2126 ptr = vmalloc(size);
2127 } else
2128 ptr = kmem_cache_alloc(get_slab(size), flags);
2129
2130 /* Check alignment; SLUB has gotten this wrong in the past,
2131 * and this can lead to user data corruption! */
2132 BUG_ON(((unsigned long) ptr) & (size-1));
2133
2134 return ptr;
2135}
2136
2137void jbd2_free(void *ptr, size_t size)
2138{
2139 if (size == PAGE_SIZE) {
2140 free_pages((unsigned long)ptr, 0);
2141 return;
2142 }
2143 if (size > PAGE_SIZE) {
2144 int order = get_order(size);
2145
2146 if (order < 3)
2147 free_pages((unsigned long)ptr, order);
2148 else
2149 vfree(ptr);
2150 return;
2151 }
2152 kmem_cache_free(get_slab(size), ptr);
2153};
2154
2155/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002156 * Journal_head storage management
2157 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002158static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002159#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002160static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2161#endif
2162
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002163static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002164{
2165 int retval;
2166
Al Viro1076d172008-03-29 03:07:18 +00002167 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002168 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002169 sizeof(struct journal_head),
2170 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002171 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002172 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002173 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002174 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002175 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002176 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002177 }
2178 return retval;
2179}
2180
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002181static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002182{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002183 if (jbd2_journal_head_cache) {
2184 kmem_cache_destroy(jbd2_journal_head_cache);
2185 jbd2_journal_head_cache = NULL;
2186 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002187}
2188
2189/*
2190 * journal_head splicing and dicing
2191 */
2192static struct journal_head *journal_alloc_journal_head(void)
2193{
2194 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002195
Jose R. Santose23291b2007-07-18 08:57:06 -04002196#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002197 atomic_inc(&nr_journal_heads);
2198#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002199 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002200 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002201 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002202 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002203 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002204 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002205 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002206 }
2207 }
2208 return ret;
2209}
2210
2211static void journal_free_journal_head(struct journal_head *jh)
2212{
Jose R. Santose23291b2007-07-18 08:57:06 -04002213#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002214 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002215 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002216#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002217 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002218}
2219
2220/*
2221 * A journal_head is attached to a buffer_head whenever JBD has an
2222 * interest in the buffer.
2223 *
2224 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2225 * is set. This bit is tested in core kernel code where we need to take
2226 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2227 * there.
2228 *
2229 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2230 *
2231 * When a buffer has its BH_JBD bit set it is immune from being released by
2232 * core kernel code, mainly via ->b_count.
2233 *
Jan Karade1b7942011-06-13 15:38:22 -04002234 * A journal_head is detached from its buffer_head when the journal_head's
2235 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2236 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002237 *
2238 * Various places in the kernel want to attach a journal_head to a buffer_head
2239 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002240 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002241 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002242 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002243 *
2244 * So the typical usage would be:
2245 *
2246 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002247 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002248 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002249 * (Get another reference for transaction)
2250 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002251 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002252 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002253 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254 */
2255
2256/*
2257 * Give a buffer_head a journal_head.
2258 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002259 * May sleep.
2260 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002261struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002262{
2263 struct journal_head *jh;
2264 struct journal_head *new_jh = NULL;
2265
2266repeat:
2267 if (!buffer_jbd(bh)) {
2268 new_jh = journal_alloc_journal_head();
2269 memset(new_jh, 0, sizeof(*new_jh));
2270 }
2271
2272 jbd_lock_bh_journal_head(bh);
2273 if (buffer_jbd(bh)) {
2274 jh = bh2jh(bh);
2275 } else {
2276 J_ASSERT_BH(bh,
2277 (atomic_read(&bh->b_count) > 0) ||
2278 (bh->b_page && bh->b_page->mapping));
2279
2280 if (!new_jh) {
2281 jbd_unlock_bh_journal_head(bh);
2282 goto repeat;
2283 }
2284
2285 jh = new_jh;
2286 new_jh = NULL; /* We consumed it */
2287 set_buffer_jbd(bh);
2288 bh->b_private = jh;
2289 jh->b_bh = bh;
2290 get_bh(bh);
2291 BUFFER_TRACE(bh, "added journal_head");
2292 }
2293 jh->b_jcount++;
2294 jbd_unlock_bh_journal_head(bh);
2295 if (new_jh)
2296 journal_free_journal_head(new_jh);
2297 return bh->b_private;
2298}
2299
2300/*
2301 * Grab a ref against this buffer_head's journal_head. If it ended up not
2302 * having a journal_head, return NULL
2303 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002304struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002305{
2306 struct journal_head *jh = NULL;
2307
2308 jbd_lock_bh_journal_head(bh);
2309 if (buffer_jbd(bh)) {
2310 jh = bh2jh(bh);
2311 jh->b_jcount++;
2312 }
2313 jbd_unlock_bh_journal_head(bh);
2314 return jh;
2315}
2316
2317static void __journal_remove_journal_head(struct buffer_head *bh)
2318{
2319 struct journal_head *jh = bh2jh(bh);
2320
2321 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002322 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2323 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2324 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2325 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2326 J_ASSERT_BH(bh, buffer_jbd(bh));
2327 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2328 BUFFER_TRACE(bh, "remove journal_head");
2329 if (jh->b_frozen_data) {
2330 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2331 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002332 }
Jan Karade1b7942011-06-13 15:38:22 -04002333 if (jh->b_committed_data) {
2334 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2335 jbd2_free(jh->b_committed_data, bh->b_size);
2336 }
2337 bh->b_private = NULL;
2338 jh->b_bh = NULL; /* debug, really */
2339 clear_buffer_jbd(bh);
2340 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341}
2342
2343/*
Jan Karade1b7942011-06-13 15:38:22 -04002344 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002345 * release the journal_head from the buffer_head.
2346 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002347void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002348{
2349 struct buffer_head *bh = jh2bh(jh);
2350
2351 jbd_lock_bh_journal_head(bh);
2352 J_ASSERT_JH(jh, jh->b_jcount > 0);
2353 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002354 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002355 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002356 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002357 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002358 } else
2359 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360}
2361
2362/*
Jan Karac851ed52008-07-11 19:27:31 -04002363 * Initialize jbd inode head
2364 */
2365void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2366{
2367 jinode->i_transaction = NULL;
2368 jinode->i_next_transaction = NULL;
2369 jinode->i_vfs_inode = inode;
2370 jinode->i_flags = 0;
2371 INIT_LIST_HEAD(&jinode->i_list);
2372}
2373
2374/*
2375 * Function to be called before we start removing inode from memory (i.e.,
2376 * clear_inode() is a fine place to be called from). It removes inode from
2377 * transaction's lists.
2378 */
2379void jbd2_journal_release_jbd_inode(journal_t *journal,
2380 struct jbd2_inode *jinode)
2381{
Jan Karac851ed52008-07-11 19:27:31 -04002382 if (!journal)
2383 return;
2384restart:
2385 spin_lock(&journal->j_list_lock);
2386 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002387 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002388 wait_queue_head_t *wq;
2389 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2390 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2391 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2392 spin_unlock(&journal->j_list_lock);
2393 schedule();
2394 finish_wait(wq, &wait.wait);
2395 goto restart;
2396 }
2397
Jan Karac851ed52008-07-11 19:27:31 -04002398 if (jinode->i_transaction) {
2399 list_del(&jinode->i_list);
2400 jinode->i_transaction = NULL;
2401 }
2402 spin_unlock(&journal->j_list_lock);
2403}
2404
2405/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002406 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002407 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002408#ifdef CONFIG_JBD2_DEBUG
2409u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002410EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002411
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002412#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002413
Jose R. Santos6f38c742007-10-16 18:38:25 -04002414static struct dentry *jbd2_debugfs_dir;
2415static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002416
2417static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002418{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002419 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2420 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002421 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2422 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002423 jbd2_debugfs_dir,
2424 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002425}
2426
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002427static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002428{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002429 debugfs_remove(jbd2_debug);
2430 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002431}
2432
2433#else
2434
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002435static void __init jbd2_create_debugfs_entry(void)
2436{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002437}
2438
2439static void __exit jbd2_remove_debugfs_entry(void)
2440{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002441}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002442
2443#endif
2444
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002445#ifdef CONFIG_PROC_FS
2446
2447#define JBD2_STATS_PROC_NAME "fs/jbd2"
2448
2449static void __init jbd2_create_jbd_stats_proc_entry(void)
2450{
2451 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2452}
2453
2454static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2455{
2456 if (proc_jbd2_stats)
2457 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2458}
2459
2460#else
2461
2462#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2463#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2464
2465#endif
2466
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002467struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002468
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002469static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002470{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002471 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002472 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002473 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2474 return -ENOMEM;
2475 }
2476 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2477 if (jbd2_inode_cache == NULL) {
2478 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2479 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002480 return -ENOMEM;
2481 }
2482 return 0;
2483}
2484
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002485static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002486{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002487 if (jbd2_handle_cache)
2488 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002489 if (jbd2_inode_cache)
2490 kmem_cache_destroy(jbd2_inode_cache);
2491
Dave Kleikamp470decc2006-10-11 01:20:57 -07002492}
2493
2494/*
2495 * Module startup and shutdown
2496 */
2497
2498static int __init journal_init_caches(void)
2499{
2500 int ret;
2501
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002502 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002503 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002504 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002505 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002506 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002507 if (ret == 0)
2508 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002509 return ret;
2510}
2511
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002512static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002513{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002514 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002515 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002516 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002517 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002518 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002519}
2520
2521static int __init journal_init(void)
2522{
2523 int ret;
2524
2525 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2526
2527 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002528 if (ret == 0) {
2529 jbd2_create_debugfs_entry();
2530 jbd2_create_jbd_stats_proc_entry();
2531 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002532 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002533 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002534 return ret;
2535}
2536
2537static void __exit journal_exit(void)
2538{
Jose R. Santose23291b2007-07-18 08:57:06 -04002539#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002540 int n = atomic_read(&nr_journal_heads);
2541 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002542 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002543#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002544 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002545 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002546 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002547}
2548
Dave Kleikamp470decc2006-10-11 01:20:57 -07002549MODULE_LICENSE("GPL");
2550module_init(journal_init);
2551module_exit(journal_exit);
2552