blob: bda564f638642ec9e0735935292012bba12e3d96 [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>
Brian King39e3ac22010-10-27 21:25:12 -040053#include <asm/system.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070054
Mingming Caof7f4bcc2006-10-11 01:20:59 -070055EXPORT_SYMBOL(jbd2_journal_extend);
56EXPORT_SYMBOL(jbd2_journal_stop);
57EXPORT_SYMBOL(jbd2_journal_lock_updates);
58EXPORT_SYMBOL(jbd2_journal_unlock_updates);
59EXPORT_SYMBOL(jbd2_journal_get_write_access);
60EXPORT_SYMBOL(jbd2_journal_get_create_access);
61EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070062EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
64EXPORT_SYMBOL(jbd2_journal_release_buffer);
65EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070066#if 0
67EXPORT_SYMBOL(journal_sync_buffer);
68#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070069EXPORT_SYMBOL(jbd2_journal_flush);
70EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070071
Mingming Caof7f4bcc2006-10-11 01:20:59 -070072EXPORT_SYMBOL(jbd2_journal_init_dev);
73EXPORT_SYMBOL(jbd2_journal_init_inode);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070074EXPORT_SYMBOL(jbd2_journal_check_used_features);
75EXPORT_SYMBOL(jbd2_journal_check_available_features);
76EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070077EXPORT_SYMBOL(jbd2_journal_load);
78EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070079EXPORT_SYMBOL(jbd2_journal_abort);
80EXPORT_SYMBOL(jbd2_journal_errno);
81EXPORT_SYMBOL(jbd2_journal_ack_err);
82EXPORT_SYMBOL(jbd2_journal_clear_err);
83EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050084EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070085EXPORT_SYMBOL(jbd2_journal_start_commit);
86EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
87EXPORT_SYMBOL(jbd2_journal_wipe);
88EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
89EXPORT_SYMBOL(jbd2_journal_invalidatepage);
90EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
91EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040092EXPORT_SYMBOL(jbd2_journal_file_inode);
93EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
94EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
95EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050096EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -070097
Dave Kleikamp470decc2006-10-11 01:20:57 -070098static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -050099static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700100
101/*
102 * Helper function used to manage commit timeouts
103 */
104
105static void commit_timeout(unsigned long __data)
106{
107 struct task_struct * p = (struct task_struct *) __data;
108
109 wake_up_process(p);
110}
111
112/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700113 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700114 * journal.
115 *
116 * This kernel thread is responsible for two things:
117 *
118 * 1) COMMIT: Every so often we need to commit the current state of the
119 * filesystem to disk. The journal thread is responsible for writing
120 * all of the metadata buffers to disk.
121 *
122 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
123 * of the data in that part of the log has been rewritten elsewhere on
124 * the disk. Flushing these old buffers to reclaim space in the log is
125 * known as checkpointing, and this thread is responsible for that job.
126 */
127
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700128static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700129{
130 journal_t *journal = arg;
131 transaction_t *transaction;
132
133 /*
134 * Set up an interval timer which can be used to trigger a commit wakeup
135 * after the commit interval expires
136 */
137 setup_timer(&journal->j_commit_timer, commit_timeout,
138 (unsigned long)current);
139
140 /* Record that the journal thread is running */
141 journal->j_task = current;
142 wake_up(&journal->j_wait_done_commit);
143
Dave Kleikamp470decc2006-10-11 01:20:57 -0700144 /*
145 * And now, wait forever for commit wakeup events.
146 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400147 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700148
149loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700150 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700151 goto end_loop;
152
153 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
154 journal->j_commit_sequence, journal->j_commit_request);
155
156 if (journal->j_commit_sequence != journal->j_commit_request) {
157 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400158 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700159 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700160 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400161 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700162 goto loop;
163 }
164
165 wake_up(&journal->j_wait_done_commit);
166 if (freezing(current)) {
167 /*
168 * The simpler the better. Flushing journal isn't a
169 * good idea, because that depends on threads that may
170 * be already stopped.
171 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700172 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400173 write_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800174 try_to_freeze();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400175 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700176 } else {
177 /*
178 * We assume on resume that commits are already there,
179 * so we don't sleep
180 */
181 DEFINE_WAIT(wait);
182 int should_sleep = 1;
183
184 prepare_to_wait(&journal->j_wait_commit, &wait,
185 TASK_INTERRUPTIBLE);
186 if (journal->j_commit_sequence != journal->j_commit_request)
187 should_sleep = 0;
188 transaction = journal->j_running_transaction;
189 if (transaction && time_after_eq(jiffies,
190 transaction->t_expires))
191 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700192 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700193 should_sleep = 0;
194 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400195 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700196 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400197 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198 }
199 finish_wait(&journal->j_wait_commit, &wait);
200 }
201
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700202 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700203
204 /*
205 * Were we woken up by a commit wakeup event?
206 */
207 transaction = journal->j_running_transaction;
208 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
209 journal->j_commit_request = transaction->t_tid;
210 jbd_debug(1, "woke because of timeout\n");
211 }
212 goto loop;
213
214end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400215 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700216 del_timer_sync(&journal->j_commit_timer);
217 journal->j_task = NULL;
218 wake_up(&journal->j_wait_done_commit);
219 jbd_debug(1, "Journal thread exiting.\n");
220 return 0;
221}
222
Pavel Emelianov97f06782007-05-08 00:30:42 -0700223static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700224{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700225 struct task_struct *t;
226
Theodore Ts'o90576c02009-09-29 15:51:30 -0400227 t = kthread_run(kjournald2, journal, "jbd2/%s",
228 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700229 if (IS_ERR(t))
230 return PTR_ERR(t);
231
Al Viro1076d172008-03-29 03:07:18 +0000232 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700233 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234}
235
236static void journal_kill_thread(journal_t *journal)
237{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400238 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700239 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700240
241 while (journal->j_task) {
242 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400243 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000244 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400245 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400247 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700248}
249
250/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700251 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700252 *
253 * Writes a metadata buffer to a given disk block. The actual IO is not
254 * performed but a new buffer_head is constructed which labels the data
255 * to be written with the correct destination disk block.
256 *
257 * Any magic-number escaping which needs to be done will cause a
258 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700259 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700260 * magic number is only written to the log for descripter blocks. In
261 * this case, we copy the data and replace the first word with 0, and we
262 * return a result code which indicates that this buffer needs to be
263 * marked as an escaped buffer in the corresponding log descriptor
264 * block. The missing word can then be restored when the block is read
265 * during recovery.
266 *
267 * If the source buffer has already been modified by a new transaction
268 * since we took the last commit snapshot, we use the frozen copy of
269 * that data for IO. If we end up using the existing buffer_head's data
270 * for the write, then we *have* to lock the buffer to prevent anyone
271 * else from using and possibly modifying it while the IO is in
272 * progress.
273 *
274 * The function returns a pointer to the buffer_heads to be used for IO.
275 *
276 * We assume that the journal has already been locked in this function.
277 *
278 * Return value:
279 * <0: Error
280 * >=0: Finished OK
281 *
282 * On success:
283 * Bit 0 set == escape performed on the data
284 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
285 */
286
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700287int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700288 struct journal_head *jh_in,
289 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700290 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700291{
292 int need_copy_out = 0;
293 int done_copy_out = 0;
294 int do_escape = 0;
295 char *mapped_data;
296 struct buffer_head *new_bh;
297 struct journal_head *new_jh;
298 struct page *new_page;
299 unsigned int new_offset;
300 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400301 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700302
303 /*
304 * The buffer really shouldn't be locked: only the current committing
305 * transaction is allowed to write it, so nobody else is allowed
306 * to do any IO.
307 *
308 * akpm: except if we're journalling data, and write() output is
309 * also part of a shared mapping, and another thread has
310 * decided to launch a writepage() against this buffer.
311 */
312 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
313
Theodore Ts'o47def822010-07-27 11:56:05 -0400314retry_alloc:
315 new_bh = alloc_buffer_head(GFP_NOFS);
316 if (!new_bh) {
317 /*
318 * Failure is not an option, but __GFP_NOFAIL is going
319 * away; so we retry ourselves here.
320 */
321 congestion_wait(BLK_RW_ASYNC, HZ/50);
322 goto retry_alloc;
323 }
324
dingdinghua96577c42009-07-13 17:55:35 -0400325 /* keep subsequent assertions sane */
326 new_bh->b_state = 0;
327 init_buffer(new_bh, NULL, NULL);
328 atomic_set(&new_bh->b_count, 1);
329 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700330
331 /*
332 * If a new transaction has already done a buffer copy-out, then
333 * we use that version of the data for the commit.
334 */
335 jbd_lock_bh_state(bh_in);
336repeat:
337 if (jh_in->b_frozen_data) {
338 done_copy_out = 1;
339 new_page = virt_to_page(jh_in->b_frozen_data);
340 new_offset = offset_in_page(jh_in->b_frozen_data);
341 } else {
342 new_page = jh2bh(jh_in)->b_page;
343 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
344 }
345
346 mapped_data = kmap_atomic(new_page, KM_USER0);
347 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200348 * Fire data frozen trigger if data already wasn't frozen. Do this
349 * before checking for escaping, as the trigger may modify the magic
350 * offset. If a copy-out happens afterwards, it will have the correct
351 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700352 */
Jan Kara13ceef02010-07-14 07:56:33 +0200353 if (!done_copy_out)
354 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
355 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700356
357 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700358 * Check for escaping
359 */
360 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700361 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700362 need_copy_out = 1;
363 do_escape = 1;
364 }
365 kunmap_atomic(mapped_data, KM_USER0);
366
367 /*
368 * Do we need to do a data copy?
369 */
370 if (need_copy_out && !done_copy_out) {
371 char *tmp;
372
373 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400374 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500375 if (!tmp) {
376 jbd2_journal_put_journal_head(new_jh);
377 return -ENOMEM;
378 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700379 jbd_lock_bh_state(bh_in);
380 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400381 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700382 goto repeat;
383 }
384
385 jh_in->b_frozen_data = tmp;
386 mapped_data = kmap_atomic(new_page, KM_USER0);
387 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
388 kunmap_atomic(mapped_data, KM_USER0);
389
390 new_page = virt_to_page(tmp);
391 new_offset = offset_in_page(tmp);
392 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700393
394 /*
395 * This isn't strictly necessary, as we're using frozen
396 * data for the escaping, but it keeps consistency with
397 * b_frozen_data usage.
398 */
399 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700400 }
401
402 /*
403 * Did we need to do an escaping? Now we've done all the
404 * copying, we can finally do so.
405 */
406 if (do_escape) {
407 mapped_data = kmap_atomic(new_page, KM_USER0);
408 *((unsigned int *)(mapped_data + new_offset)) = 0;
409 kunmap_atomic(mapped_data, KM_USER0);
410 }
411
Dave Kleikamp470decc2006-10-11 01:20:57 -0700412 set_bh_page(new_bh, new_page, new_offset);
413 new_jh->b_transaction = NULL;
414 new_bh->b_size = jh2bh(jh_in)->b_size;
415 new_bh->b_bdev = transaction->t_journal->j_dev;
416 new_bh->b_blocknr = blocknr;
417 set_buffer_mapped(new_bh);
418 set_buffer_dirty(new_bh);
419
420 *jh_out = new_jh;
421
422 /*
423 * The to-be-written buffer needs to get moved to the io queue,
424 * and the original buffer whose contents we are shadowing or
425 * copying is moved to the transaction's shadow queue.
426 */
427 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400428 spin_lock(&journal->j_list_lock);
429 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
430 spin_unlock(&journal->j_list_lock);
431 jbd_unlock_bh_state(bh_in);
432
Dave Kleikamp470decc2006-10-11 01:20:57 -0700433 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700434 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700435
436 return do_escape | (done_copy_out << 1);
437}
438
439/*
440 * Allocation code for the journal file. Manage the space left in the
441 * journal, so that we can begin checkpointing when appropriate.
442 */
443
444/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700445 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446 *
447 * Called with the journal already locked.
448 *
449 * Called under j_state_lock
450 */
451
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700452int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700453{
454 int left = journal->j_free;
455
Theodore Ts'oa931da62010-08-03 21:35:12 -0400456 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457
458 /*
459 * Be pessimistic here about the number of those free blocks which
460 * might be required for log descriptor control blocks.
461 */
462
463#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
464
465 left -= MIN_LOG_RESERVED_BLOCKS;
466
467 if (left <= 0)
468 return 0;
469 left -= (left >> 3);
470 return left;
471}
472
473/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500474 * Called with j_state_lock locked for writing.
475 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700476 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700477int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478{
479 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400480 * The only transaction we can possibly wait upon is the
481 * currently running transaction (if it exists). Otherwise,
482 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700483 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400484 if (journal->j_running_transaction &&
485 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200487 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700488 * commit thread. We do _not_ do the commit ourselves.
489 */
490
491 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400492 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700493 journal->j_commit_request,
494 journal->j_commit_sequence);
495 wake_up(&journal->j_wait_commit);
496 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400497 } else if (!tid_geq(journal->j_commit_request, target))
498 /* This should never happen, but if it does, preserve
499 the evidence before kjournald goes into a loop and
500 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400501 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400502 journal->j_commit_request,
503 journal->j_commit_sequence,
504 target, journal->j_running_transaction ?
505 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700506 return 0;
507}
508
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700509int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700510{
511 int ret;
512
Theodore Ts'oa931da62010-08-03 21:35:12 -0400513 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700514 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400515 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516 return ret;
517}
518
519/*
520 * Force and wait upon a commit if the calling process is not within
521 * transaction. This is used for forcing out undo-protected data which contains
522 * bitmaps, when the fs is running out of space.
523 *
524 * We can only force the running transaction if we don't have an active handle;
525 * otherwise, we will deadlock.
526 *
527 * Returns true if a transaction was started.
528 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700529int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700530{
531 transaction_t *transaction = NULL;
532 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500533 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700534
Theodore Ts'oa931da62010-08-03 21:35:12 -0400535 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 if (journal->j_running_transaction && !current->journal_info) {
537 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500538 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
539 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540 } else if (journal->j_committing_transaction)
541 transaction = journal->j_committing_transaction;
542
543 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400544 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700545 return 0; /* Nothing to retry */
546 }
547
548 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400549 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500550 if (need_to_start)
551 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700552 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700553 return 1;
554}
555
556/*
557 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500558 * if a transaction is going to be committed (or is currently already
559 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700560 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700561int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700562{
563 int ret = 0;
564
Theodore Ts'oa931da62010-08-03 21:35:12 -0400565 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700566 if (journal->j_running_transaction) {
567 tid_t tid = journal->j_running_transaction->t_tid;
568
Jan Karac88ccea2009-02-10 11:27:46 -0500569 __jbd2_log_start_commit(journal, tid);
570 /* There's a running transaction and we've just made sure
571 * it's commit has been scheduled. */
572 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700573 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500574 ret = 1;
575 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700576 /*
577 * If ext3_write_super() recently started a commit, then we
578 * have to wait for completion of that transaction
579 */
Jan Karac88ccea2009-02-10 11:27:46 -0500580 if (ptid)
581 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700582 ret = 1;
583 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400584 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700585 return ret;
586}
587
588/*
Jan Karabbd2be32011-05-24 11:59:18 -0400589 * Return 1 if a given transaction has not yet sent barrier request
590 * connected with a transaction commit. If 0 is returned, transaction
591 * may or may not have sent the barrier. Used to avoid sending barrier
592 * twice in common cases.
593 */
594int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
595{
596 int ret = 0;
597 transaction_t *commit_trans;
598
599 if (!(journal->j_flags & JBD2_BARRIER))
600 return 0;
601 read_lock(&journal->j_state_lock);
602 /* Transaction already committed? */
603 if (tid_geq(journal->j_commit_sequence, tid))
604 goto out;
605 commit_trans = journal->j_committing_transaction;
606 if (!commit_trans || commit_trans->t_tid != tid) {
607 ret = 1;
608 goto out;
609 }
610 /*
611 * Transaction is being committed and we already proceeded to
612 * submitting a flush to fs partition?
613 */
614 if (journal->j_fs_dev != journal->j_dev) {
615 if (!commit_trans->t_need_data_flush ||
616 commit_trans->t_state >= T_COMMIT_DFLUSH)
617 goto out;
618 } else {
619 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
620 goto out;
621 }
622 ret = 1;
623out:
624 read_unlock(&journal->j_state_lock);
625 return ret;
626}
627EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
628
629/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700630 * Wait for a specified commit to complete.
631 * The caller may not hold the journal lock.
632 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700633int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700634{
635 int err = 0;
636
Theodore Ts'oa931da62010-08-03 21:35:12 -0400637 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400638#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700639 if (!tid_geq(journal->j_commit_request, tid)) {
640 printk(KERN_EMERG
641 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400642 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700643 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700644#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400646 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647 tid, journal->j_commit_sequence);
648 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400649 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700650 wait_event(journal->j_wait_done_commit,
651 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400652 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700653 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400654 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700655
656 if (unlikely(is_journal_aborted(journal))) {
657 printk(KERN_EMERG "journal commit I/O error\n");
658 err = -EIO;
659 }
660 return err;
661}
662
663/*
664 * Log buffer allocation routines:
665 */
666
Mingming Cao18eba7a2006-10-11 01:21:13 -0700667int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700668{
669 unsigned long blocknr;
670
Theodore Ts'oa931da62010-08-03 21:35:12 -0400671 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700672 J_ASSERT(journal->j_free > 1);
673
674 blocknr = journal->j_head;
675 journal->j_head++;
676 journal->j_free--;
677 if (journal->j_head == journal->j_last)
678 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400679 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700680 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700681}
682
683/*
684 * Conversion of logical to physical block numbers for the journal
685 *
686 * On external journals the journal blocks are identity-mapped, so
687 * this is a no-op. If needed, we can use j_blk_offset - everything is
688 * ready.
689 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700690int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700691 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700692{
693 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700694 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700695
696 if (journal->j_inode) {
697 ret = bmap(journal->j_inode, blocknr);
698 if (ret)
699 *retp = ret;
700 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700701 printk(KERN_ALERT "%s: journal block not found "
702 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400703 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700704 err = -EIO;
705 __journal_abort_soft(journal, err);
706 }
707 } else {
708 *retp = blocknr; /* +journal->j_blk_offset */
709 }
710 return err;
711}
712
713/*
714 * We play buffer_head aliasing tricks to write data/metadata blocks to
715 * the journal without copying their contents, but for journal
716 * descriptor blocks we do need to generate bona fide buffers.
717 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700718 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700719 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
720 * But we don't bother doing that, so there will be coherency problems with
721 * mmaps of blockdevs which hold live JBD-controlled filesystems.
722 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700723struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700724{
725 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700726 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700727 int err;
728
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700729 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700730
731 if (err)
732 return NULL;
733
734 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500735 if (!bh)
736 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700737 lock_buffer(bh);
738 memset(bh->b_data, 0, journal->j_blocksize);
739 set_buffer_uptodate(bh);
740 unlock_buffer(bh);
741 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700742 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700743}
744
Jan Kara79feb522012-03-13 22:22:54 -0400745/*
746 * Return tid of the oldest transaction in the journal and block in the journal
747 * where the transaction starts.
748 *
749 * If the journal is now empty, return which will be the next transaction ID
750 * we will write and where will that transaction start.
751 *
752 * The return value is 0 if journal tail cannot be pushed any further, 1 if
753 * it can.
754 */
755int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
756 unsigned long *block)
757{
758 transaction_t *transaction;
759 int ret;
760
761 read_lock(&journal->j_state_lock);
762 spin_lock(&journal->j_list_lock);
763 transaction = journal->j_checkpoint_transactions;
764 if (transaction) {
765 *tid = transaction->t_tid;
766 *block = transaction->t_log_start;
767 } else if ((transaction = journal->j_committing_transaction) != NULL) {
768 *tid = transaction->t_tid;
769 *block = transaction->t_log_start;
770 } else if ((transaction = journal->j_running_transaction) != NULL) {
771 *tid = transaction->t_tid;
772 *block = journal->j_head;
773 } else {
774 *tid = journal->j_transaction_sequence;
775 *block = journal->j_head;
776 }
777 ret = tid_gt(*tid, journal->j_tail_sequence);
778 spin_unlock(&journal->j_list_lock);
779 read_unlock(&journal->j_state_lock);
780
781 return ret;
782}
783
784/*
785 * Update information in journal structure and in on disk journal superblock
786 * about log tail. This function does not check whether information passed in
787 * really pushes log tail further. It's responsibility of the caller to make
788 * sure provided log tail information is valid (e.g. by holding
789 * j_checkpoint_mutex all the time between computing log tail and calling this
790 * function as is the case with jbd2_cleanup_journal_tail()).
791 *
792 * Requires j_checkpoint_mutex
793 */
794void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
795{
796 unsigned long freed;
797
798 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
799
800 /*
801 * We cannot afford for write to remain in drive's caches since as
802 * soon as we update j_tail, next transaction can start reusing journal
803 * space and if we lose sb update during power failure we'd replay
804 * old transaction with possibly newly overwritten data.
805 */
806 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
807 write_lock(&journal->j_state_lock);
808 freed = block - journal->j_tail;
809 if (block < journal->j_tail)
810 freed += journal->j_last - journal->j_first;
811
812 trace_jbd2_update_log_tail(journal, tid, block, freed);
813 jbd_debug(1,
814 "Cleaning journal tail from %d to %d (offset %lu), "
815 "freeing %lu\n",
816 journal->j_tail_sequence, tid, block, freed);
817
818 journal->j_free += freed;
819 journal->j_tail_sequence = tid;
820 journal->j_tail = block;
821 write_unlock(&journal->j_state_lock);
822}
823
Jan Kara33395782012-03-13 22:45:38 -0400824/*
825 * This is a variaon of __jbd2_update_log_tail which checks for validity of
826 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
827 * with other threads updating log tail.
828 */
829void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
830{
831 mutex_lock(&journal->j_checkpoint_mutex);
832 if (tid_gt(tid, journal->j_tail_sequence))
833 __jbd2_update_log_tail(journal, tid, block);
834 mutex_unlock(&journal->j_checkpoint_mutex);
835}
836
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500837struct jbd2_stats_proc_session {
838 journal_t *journal;
839 struct transaction_stats_s *stats;
840 int start;
841 int max;
842};
843
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500844static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
845{
846 return *pos ? NULL : SEQ_START_TOKEN;
847}
848
849static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
850{
851 return NULL;
852}
853
854static int jbd2_seq_info_show(struct seq_file *seq, void *v)
855{
856 struct jbd2_stats_proc_session *s = seq->private;
857
858 if (v != SEQ_START_TOKEN)
859 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400860 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500861 s->stats->ts_tid,
862 s->journal->j_max_transaction_buffers);
863 if (s->stats->ts_tid == 0)
864 return 0;
865 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400866 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500867 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400868 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500869 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400870 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500871 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400872 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500873 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400874 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500875 seq_printf(seq, " %lluus average transaction commit time\n",
876 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500877 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400878 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500879 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400880 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500881 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400882 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500883 return 0;
884}
885
886static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
887{
888}
889
James Morris88e9d342009-09-22 16:43:43 -0700890static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500891 .start = jbd2_seq_info_start,
892 .next = jbd2_seq_info_next,
893 .stop = jbd2_seq_info_stop,
894 .show = jbd2_seq_info_show,
895};
896
897static int jbd2_seq_info_open(struct inode *inode, struct file *file)
898{
899 journal_t *journal = PDE(inode)->data;
900 struct jbd2_stats_proc_session *s;
901 int rc, size;
902
903 s = kmalloc(sizeof(*s), GFP_KERNEL);
904 if (s == NULL)
905 return -ENOMEM;
906 size = sizeof(struct transaction_stats_s);
907 s->stats = kmalloc(size, GFP_KERNEL);
908 if (s->stats == NULL) {
909 kfree(s);
910 return -ENOMEM;
911 }
912 spin_lock(&journal->j_history_lock);
913 memcpy(s->stats, &journal->j_stats, size);
914 s->journal = journal;
915 spin_unlock(&journal->j_history_lock);
916
917 rc = seq_open(file, &jbd2_seq_info_ops);
918 if (rc == 0) {
919 struct seq_file *m = file->private_data;
920 m->private = s;
921 } else {
922 kfree(s->stats);
923 kfree(s);
924 }
925 return rc;
926
927}
928
929static int jbd2_seq_info_release(struct inode *inode, struct file *file)
930{
931 struct seq_file *seq = file->private_data;
932 struct jbd2_stats_proc_session *s = seq->private;
933 kfree(s->stats);
934 kfree(s);
935 return seq_release(inode, file);
936}
937
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700938static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500939 .owner = THIS_MODULE,
940 .open = jbd2_seq_info_open,
941 .read = seq_read,
942 .llseek = seq_lseek,
943 .release = jbd2_seq_info_release,
944};
945
946static struct proc_dir_entry *proc_jbd2_stats;
947
948static void jbd2_stats_proc_init(journal_t *journal)
949{
Theodore Ts'o05496762008-09-16 14:36:17 -0400950 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500951 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700952 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
953 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500954 }
955}
956
957static void jbd2_stats_proc_exit(journal_t *journal)
958{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500959 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400960 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500961}
962
Dave Kleikamp470decc2006-10-11 01:20:57 -0700963/*
964 * Management for journal control blocks: functions to create and
965 * destroy journal_t structures, and to initialise and read existing
966 * journal blocks from disk. */
967
968/* First: create and setup a journal_t object in memory. We initialise
969 * very few fields yet: that has to wait until we have created the
970 * journal structures from from scratch, or loaded them from disk. */
971
972static journal_t * journal_init_common (void)
973{
974 journal_t *journal;
975 int err;
976
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500977 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700978 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500979 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700980
981 init_waitqueue_head(&journal->j_wait_transaction_locked);
982 init_waitqueue_head(&journal->j_wait_logspace);
983 init_waitqueue_head(&journal->j_wait_done_commit);
984 init_waitqueue_head(&journal->j_wait_checkpoint);
985 init_waitqueue_head(&journal->j_wait_commit);
986 init_waitqueue_head(&journal->j_wait_updates);
987 mutex_init(&journal->j_barrier);
988 mutex_init(&journal->j_checkpoint_mutex);
989 spin_lock_init(&journal->j_revoke_lock);
990 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400991 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700992
Mingming Caocd02ff02007-10-16 18:38:25 -0400993 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500994 journal->j_min_batch_time = 0;
995 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700996
997 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700998 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700999
1000 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001001 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002 if (err) {
1003 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001004 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001005 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001006
Theodore Ts'obf699322009-09-30 00:32:06 -04001007 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001008
Dave Kleikamp470decc2006-10-11 01:20:57 -07001009 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001010}
1011
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001012/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001013 *
1014 * Create a journal structure assigned some fixed set of disk blocks to
1015 * the journal. We don't actually touch those disk blocks yet, but we
1016 * need to set up all of the mapping information to tell the journaling
1017 * system where the journal blocks are.
1018 *
1019 */
1020
1021/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001022 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001023 * @bdev: Block device on which to create the journal
1024 * @fs_dev: Device which hold journalled filesystem for this journal.
1025 * @start: Block nr Start of journal.
1026 * @len: Length of the journal in blocks.
1027 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001028 *
1029 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001030 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001031 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001032 * range of blocks on an arbitrary block device.
1033 *
1034 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001035journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001036 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001037 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001038{
1039 journal_t *journal = journal_init_common();
1040 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001041 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001042 int n;
1043
1044 if (!journal)
1045 return NULL;
1046
1047 /* journal descriptor can store up to n blocks -bzzz */
1048 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001049 journal->j_dev = bdev;
1050 journal->j_fs_dev = fs_dev;
1051 journal->j_blk_offset = start;
1052 journal->j_maxlen = len;
1053 bdevname(journal->j_dev, journal->j_devname);
1054 p = journal->j_devname;
1055 while ((p = strchr(p, '/')))
1056 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001057 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001058 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1059 journal->j_wbufsize = n;
1060 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1061 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001062 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001063 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001064 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001065 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001066
1067 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001068 if (!bh) {
1069 printk(KERN_ERR
1070 "%s: Cannot get buffer for journal superblock\n",
1071 __func__);
1072 goto out_err;
1073 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001074 journal->j_sb_buffer = bh;
1075 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001076
Dave Kleikamp470decc2006-10-11 01:20:57 -07001077 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001078out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001079 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001080 jbd2_stats_proc_exit(journal);
1081 kfree(journal);
1082 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001083}
1084
1085/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001086 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001087 * @inode: An inode to create the journal in
1088 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001089 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 * the journal. The inode must exist already, must support bmap() and
1091 * must have all data blocks preallocated.
1092 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001093journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094{
1095 struct buffer_head *bh;
1096 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001097 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098 int err;
1099 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001100 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101
1102 if (!journal)
1103 return NULL;
1104
1105 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1106 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001107 bdevname(journal->j_dev, journal->j_devname);
1108 p = journal->j_devname;
1109 while ((p = strchr(p, '/')))
1110 *p = '!';
1111 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001112 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113 jbd_debug(1,
1114 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1115 journal, inode->i_sb->s_id, inode->i_ino,
1116 (long long) inode->i_size,
1117 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1118
1119 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1120 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001121 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001122
1123 /* journal descriptor can store up to n blocks -bzzz */
1124 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1125 journal->j_wbufsize = n;
1126 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1127 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001128 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001129 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001130 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131 }
1132
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001133 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001134 /* If that failed, give up */
1135 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001136 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001137 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001138 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001139 }
1140
1141 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001142 if (!bh) {
1143 printk(KERN_ERR
1144 "%s: Cannot get buffer for journal superblock\n",
1145 __func__);
1146 goto out_err;
1147 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001148 journal->j_sb_buffer = bh;
1149 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1150
1151 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001152out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001153 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001154 jbd2_stats_proc_exit(journal);
1155 kfree(journal);
1156 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001157}
1158
1159/*
1160 * If the journal init or create aborts, we need to mark the journal
1161 * superblock as being NULL to prevent the journal destroy from writing
1162 * back a bogus superblock.
1163 */
1164static void journal_fail_superblock (journal_t *journal)
1165{
1166 struct buffer_head *bh = journal->j_sb_buffer;
1167 brelse(bh);
1168 journal->j_sb_buffer = NULL;
1169}
1170
1171/*
1172 * Given a journal_t structure, initialise the various fields for
1173 * startup of a new journaling session. We use this both when creating
1174 * a journal, and after recovering an old journal to reset it for
1175 * subsequent use.
1176 */
1177
1178static int journal_reset(journal_t *journal)
1179{
1180 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001181 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001182
1183 first = be32_to_cpu(sb->s_first);
1184 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001185 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001186 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001187 first, last);
1188 journal_fail_superblock(journal);
1189 return -EINVAL;
1190 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001191
1192 journal->j_first = first;
1193 journal->j_last = last;
1194
1195 journal->j_head = first;
1196 journal->j_tail = first;
1197 journal->j_free = last - first;
1198
1199 journal->j_tail_sequence = journal->j_transaction_sequence;
1200 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1201 journal->j_commit_request = journal->j_commit_sequence;
1202
1203 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1204
Dave Kleikamp470decc2006-10-11 01:20:57 -07001205 /*
1206 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001207 * no recovery (s_start == 0), then we can safely defer the superblock
1208 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001209 * attempting a write to a potential-readonly device.
1210 */
Jan Kara24bcc892012-03-13 15:41:04 -04001211 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001212 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001213 "(start %ld, seq %d, errno %d)\n",
1214 journal->j_tail, journal->j_tail_sequence,
1215 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001216 journal->j_flags |= JBD2_FLUSHED;
1217 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001218 /* Lock here to make assertions happy... */
1219 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001220 /*
1221 * Update log tail information. We use WRITE_FUA since new
1222 * transaction will start reusing journal space and so we
1223 * must make sure information about current log tail is on
1224 * disk before that.
1225 */
1226 jbd2_journal_update_sb_log_tail(journal,
1227 journal->j_tail_sequence,
1228 journal->j_tail,
1229 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001230 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 }
Jan Kara24bcc892012-03-13 15:41:04 -04001232 return jbd2_journal_start_thread(journal);
1233}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001234
Jan Kara79feb522012-03-13 22:22:54 -04001235static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001236{
1237 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001238 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001239
Jan Kara79feb522012-03-13 22:22:54 -04001240 trace_jbd2_write_superblock(journal, write_op);
1241 if (!(journal->j_flags & JBD2_BARRIER))
1242 write_op &= ~(REQ_FUA | REQ_FLUSH);
1243 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001244 if (buffer_write_io_error(bh)) {
1245 /*
1246 * Oh, dear. A previous attempt to write the journal
1247 * superblock failed. This could happen because the
1248 * USB device was yanked out. Or it could happen to
1249 * be a transient write error and maybe the block will
1250 * be remapped. Nothing we can do but to retry the
1251 * write and hope for the best.
1252 */
1253 printk(KERN_ERR "JBD2: previous I/O error detected "
1254 "for journal superblock update for %s.\n",
1255 journal->j_devname);
1256 clear_buffer_write_io_error(bh);
1257 set_buffer_uptodate(bh);
1258 }
Jan Kara79feb522012-03-13 22:22:54 -04001259 get_bh(bh);
1260 bh->b_end_io = end_buffer_write_sync;
1261 ret = submit_bh(write_op, bh);
1262 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001263 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001264 clear_buffer_write_io_error(bh);
1265 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001266 ret = -EIO;
1267 }
1268 if (ret) {
1269 printk(KERN_ERR "JBD2: Error %d detected when updating "
1270 "journal superblock for %s.\n", ret,
1271 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001272 }
1273}
1274
1275/**
1276 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1277 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001278 * @tail_tid: TID of the new transaction at the tail of the log
1279 * @tail_block: The first block of the transaction at the tail of the log
1280 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001281 *
1282 * Update a journal's superblock information about log tail and write it to
1283 * disk, waiting for the IO to complete.
1284 */
Jan Kara79feb522012-03-13 22:22:54 -04001285void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1286 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001287{
1288 journal_superblock_t *sb = journal->j_superblock;
1289
Jan Karaa78bb112012-03-13 15:43:04 -04001290 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001291 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1292 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001293
Jan Kara79feb522012-03-13 22:22:54 -04001294 sb->s_sequence = cpu_to_be32(tail_tid);
1295 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001296
Jan Kara79feb522012-03-13 22:22:54 -04001297 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001298
1299 /* Log is no longer empty */
1300 write_lock(&journal->j_state_lock);
1301 WARN_ON(!sb->s_sequence);
1302 journal->j_flags &= ~JBD2_FLUSHED;
1303 write_unlock(&journal->j_state_lock);
1304}
1305
1306/**
1307 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1308 * @journal: The journal to update.
1309 *
1310 * Update a journal's dynamic superblock fields to show that journal is empty.
1311 * Write updated superblock to disk waiting for IO to complete.
1312 */
1313static void jbd2_mark_journal_empty(journal_t *journal)
1314{
1315 journal_superblock_t *sb = journal->j_superblock;
1316
Jan Karaa78bb112012-03-13 15:43:04 -04001317 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara24bcc892012-03-13 15:41:04 -04001318 read_lock(&journal->j_state_lock);
1319 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1320 journal->j_tail_sequence);
1321
1322 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1323 sb->s_start = cpu_to_be32(0);
1324 read_unlock(&journal->j_state_lock);
1325
Jan Kara79feb522012-03-13 22:22:54 -04001326 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001327
1328 /* Log is no longer empty */
1329 write_lock(&journal->j_state_lock);
1330 journal->j_flags |= JBD2_FLUSHED;
1331 write_unlock(&journal->j_state_lock);
1332}
1333
1334
1335/**
1336 * jbd2_journal_update_sb_errno() - Update error in the journal.
1337 * @journal: The journal to update.
1338 *
1339 * Update a journal's errno. Write updated superblock to disk waiting for IO
1340 * to complete.
1341 */
1342static void jbd2_journal_update_sb_errno(journal_t *journal)
1343{
1344 journal_superblock_t *sb = journal->j_superblock;
1345
1346 read_lock(&journal->j_state_lock);
1347 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1348 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001349 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001350 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001351
Jan Kara79feb522012-03-13 22:22:54 -04001352 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001353}
1354
1355/*
1356 * Read the superblock for a given journal, performing initial
1357 * validation of the format.
1358 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001359static int journal_get_superblock(journal_t *journal)
1360{
1361 struct buffer_head *bh;
1362 journal_superblock_t *sb;
1363 int err = -EIO;
1364
1365 bh = journal->j_sb_buffer;
1366
1367 J_ASSERT(bh != NULL);
1368 if (!buffer_uptodate(bh)) {
1369 ll_rw_block(READ, 1, &bh);
1370 wait_on_buffer(bh);
1371 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001372 printk(KERN_ERR
1373 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001374 goto out;
1375 }
1376 }
1377
1378 sb = journal->j_superblock;
1379
1380 err = -EINVAL;
1381
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001382 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001383 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001384 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001385 goto out;
1386 }
1387
1388 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001389 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001390 journal->j_format_version = 1;
1391 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001392 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001393 journal->j_format_version = 2;
1394 break;
1395 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001396 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001397 goto out;
1398 }
1399
1400 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1401 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1402 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001403 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001404 goto out;
1405 }
1406
Eryu Guan87622022011-11-01 19:04:59 -04001407 if (be32_to_cpu(sb->s_first) == 0 ||
1408 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1409 printk(KERN_WARNING
1410 "JBD2: Invalid start block of journal: %u\n",
1411 be32_to_cpu(sb->s_first));
1412 goto out;
1413 }
1414
Dave Kleikamp470decc2006-10-11 01:20:57 -07001415 return 0;
1416
1417out:
1418 journal_fail_superblock(journal);
1419 return err;
1420}
1421
1422/*
1423 * Load the on-disk journal superblock and read the key fields into the
1424 * journal_t.
1425 */
1426
1427static int load_superblock(journal_t *journal)
1428{
1429 int err;
1430 journal_superblock_t *sb;
1431
1432 err = journal_get_superblock(journal);
1433 if (err)
1434 return err;
1435
1436 sb = journal->j_superblock;
1437
1438 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1439 journal->j_tail = be32_to_cpu(sb->s_start);
1440 journal->j_first = be32_to_cpu(sb->s_first);
1441 journal->j_last = be32_to_cpu(sb->s_maxlen);
1442 journal->j_errno = be32_to_cpu(sb->s_errno);
1443
1444 return 0;
1445}
1446
1447
1448/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001449 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001450 * @journal: Journal to act on.
1451 *
1452 * Given a journal_t structure which tells us which disk blocks contain
1453 * a journal, read the journal from disk to initialise the in-memory
1454 * structures.
1455 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001456int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001457{
1458 int err;
1459 journal_superblock_t *sb;
1460
1461 err = load_superblock(journal);
1462 if (err)
1463 return err;
1464
1465 sb = journal->j_superblock;
1466 /* If this is a V2 superblock, then we have to check the
1467 * features flags on it. */
1468
1469 if (journal->j_format_version >= 2) {
1470 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001471 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001473 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001474 printk(KERN_WARNING
1475 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001476 return -EINVAL;
1477 }
1478 }
1479
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001480 /*
1481 * Create a slab for this blocksize
1482 */
1483 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1484 if (err)
1485 return err;
1486
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487 /* Let the recovery code check whether it needs to recover any
1488 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001489 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001490 goto recovery_error;
1491
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001492 if (journal->j_failed_commit) {
1493 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1494 "is corrupt.\n", journal->j_failed_commit,
1495 journal->j_devname);
1496 return -EIO;
1497 }
1498
Dave Kleikamp470decc2006-10-11 01:20:57 -07001499 /* OK, we've finished with the dynamic journal bits:
1500 * reinitialise the dynamic contents of the superblock in memory
1501 * and reset them on disk. */
1502 if (journal_reset(journal))
1503 goto recovery_error;
1504
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001505 journal->j_flags &= ~JBD2_ABORT;
1506 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001507 return 0;
1508
1509recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001510 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001511 return -EIO;
1512}
1513
1514/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001515 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001516 * @journal: Journal to act on.
1517 *
1518 * Release a journal_t structure once it is no longer in use by the
1519 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001520 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001521 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001522int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001523{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001524 int err = 0;
1525
Dave Kleikamp470decc2006-10-11 01:20:57 -07001526 /* Wait for the commit thread to wake up and die. */
1527 journal_kill_thread(journal);
1528
1529 /* Force a final log commit */
1530 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001531 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001532
1533 /* Force any old transactions to disk */
1534
1535 /* Totally anal locking here... */
1536 spin_lock(&journal->j_list_lock);
1537 while (journal->j_checkpoint_transactions != NULL) {
1538 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001539 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001540 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001541 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001542 spin_lock(&journal->j_list_lock);
1543 }
1544
1545 J_ASSERT(journal->j_running_transaction == NULL);
1546 J_ASSERT(journal->j_committing_transaction == NULL);
1547 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1548 spin_unlock(&journal->j_list_lock);
1549
Dave Kleikamp470decc2006-10-11 01:20:57 -07001550 if (journal->j_sb_buffer) {
Jan Karaa78bb112012-03-13 15:43:04 -04001551 if (!is_journal_aborted(journal)) {
1552 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001553 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001554 mutex_unlock(&journal->j_checkpoint_mutex);
1555 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001556 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001557 brelse(journal->j_sb_buffer);
1558 }
1559
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001560 if (journal->j_proc_entry)
1561 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001562 if (journal->j_inode)
1563 iput(journal->j_inode);
1564 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001565 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001566 kfree(journal->j_wbuf);
1567 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001568
1569 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570}
1571
1572
1573/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001574 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001575 * @journal: Journal to check.
1576 * @compat: bitmask of compatible features
1577 * @ro: bitmask of features that force read-only mount
1578 * @incompat: bitmask of incompatible features
1579 *
1580 * Check whether the journal uses all of a given set of
1581 * features. Return true (non-zero) if it does.
1582 **/
1583
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001584int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001585 unsigned long ro, unsigned long incompat)
1586{
1587 journal_superblock_t *sb;
1588
1589 if (!compat && !ro && !incompat)
1590 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001591 /* Load journal superblock if it is not loaded yet. */
1592 if (journal->j_format_version == 0 &&
1593 journal_get_superblock(journal) != 0)
1594 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001595 if (journal->j_format_version == 1)
1596 return 0;
1597
1598 sb = journal->j_superblock;
1599
1600 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1601 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1602 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1603 return 1;
1604
1605 return 0;
1606}
1607
1608/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001609 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001610 * @journal: Journal to check.
1611 * @compat: bitmask of compatible features
1612 * @ro: bitmask of features that force read-only mount
1613 * @incompat: bitmask of incompatible features
1614 *
1615 * Check whether the journaling code supports the use of
1616 * all of a given set of features on this journal. Return true
1617 * (non-zero) if it can. */
1618
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001619int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620 unsigned long ro, unsigned long incompat)
1621{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001622 if (!compat && !ro && !incompat)
1623 return 1;
1624
Dave Kleikamp470decc2006-10-11 01:20:57 -07001625 /* We can support any known requested features iff the
1626 * superblock is in version 2. Otherwise we fail to support any
1627 * extended sb features. */
1628
1629 if (journal->j_format_version != 2)
1630 return 0;
1631
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1633 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1634 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001635 return 1;
1636
1637 return 0;
1638}
1639
1640/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001641 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 * @journal: Journal to act on.
1643 * @compat: bitmask of compatible features
1644 * @ro: bitmask of features that force read-only mount
1645 * @incompat: bitmask of incompatible features
1646 *
1647 * Mark a given journal feature as present on the
1648 * superblock. Returns true if the requested features could be set.
1649 *
1650 */
1651
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001652int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 unsigned long ro, unsigned long incompat)
1654{
1655 journal_superblock_t *sb;
1656
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001657 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 return 1;
1659
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001660 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001661 return 0;
1662
1663 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1664 compat, ro, incompat);
1665
1666 sb = journal->j_superblock;
1667
1668 sb->s_feature_compat |= cpu_to_be32(compat);
1669 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1670 sb->s_feature_incompat |= cpu_to_be32(incompat);
1671
1672 return 1;
1673}
1674
Girish Shilamkar818d2762008-01-28 23:58:27 -05001675/*
1676 * jbd2_journal_clear_features () - Clear a given journal feature in the
1677 * superblock
1678 * @journal: Journal to act on.
1679 * @compat: bitmask of compatible features
1680 * @ro: bitmask of features that force read-only mount
1681 * @incompat: bitmask of incompatible features
1682 *
1683 * Clear a given journal feature as present on the
1684 * superblock.
1685 */
1686void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1687 unsigned long ro, unsigned long incompat)
1688{
1689 journal_superblock_t *sb;
1690
1691 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1692 compat, ro, incompat);
1693
1694 sb = journal->j_superblock;
1695
1696 sb->s_feature_compat &= ~cpu_to_be32(compat);
1697 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1698 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1699}
1700EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701
1702/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001703 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 * @journal: Journal to act on.
1705 *
1706 * Flush all data for a given journal to disk and empty the journal.
1707 * Filesystems can use this when remounting readonly to ensure that
1708 * recovery does not need to happen on remount.
1709 */
1710
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001711int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001712{
1713 int err = 0;
1714 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715
Theodore Ts'oa931da62010-08-03 21:35:12 -04001716 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001717
1718 /* Force everything buffered to the log... */
1719 if (journal->j_running_transaction) {
1720 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 } else if (journal->j_committing_transaction)
1723 transaction = journal->j_committing_transaction;
1724
1725 /* Wait for the log commit to complete... */
1726 if (transaction) {
1727 tid_t tid = transaction->t_tid;
1728
Theodore Ts'oa931da62010-08-03 21:35:12 -04001729 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001730 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001731 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001732 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001733 }
1734
1735 /* ...and flush everything in the log out to disk. */
1736 spin_lock(&journal->j_list_lock);
1737 while (!err && journal->j_checkpoint_transactions != NULL) {
1738 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001739 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001740 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001741 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001742 spin_lock(&journal->j_list_lock);
1743 }
1744 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001745
1746 if (is_journal_aborted(journal))
1747 return -EIO;
1748
Jan Karaa78bb112012-03-13 15:43:04 -04001749 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001750 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001751
1752 /* Finally, mark the journal as really needing no recovery.
1753 * This sets s_start==0 in the underlying superblock, which is
1754 * the magic code for a fully-recovered superblock. Any future
1755 * commits of data to the journal will restore the current
1756 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001757 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001758 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001759 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760 J_ASSERT(!journal->j_running_transaction);
1761 J_ASSERT(!journal->j_committing_transaction);
1762 J_ASSERT(!journal->j_checkpoint_transactions);
1763 J_ASSERT(journal->j_head == journal->j_tail);
1764 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001765 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001766 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001767}
1768
1769/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001770 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001771 * @journal: Journal to act on.
1772 * @write: flag (see below)
1773 *
1774 * Wipe out all of the contents of a journal, safely. This will produce
1775 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001776 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001777 *
1778 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1779 * we merely suppress recovery.
1780 */
1781
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001782int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001783{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 int err = 0;
1785
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001786 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787
1788 err = load_superblock(journal);
1789 if (err)
1790 return err;
1791
Dave Kleikamp470decc2006-10-11 01:20:57 -07001792 if (!journal->j_tail)
1793 goto no_recovery;
1794
Eryu Guanf2a44522011-11-01 19:09:18 -04001795 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 write ? "Clearing" : "Ignoring");
1797
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001798 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001799 if (write) {
1800 /* Lock to make assertions happy... */
1801 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001802 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001803 mutex_unlock(&journal->j_checkpoint_mutex);
1804 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805
1806 no_recovery:
1807 return err;
1808}
1809
1810/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 * Journal abort has very specific semantics, which we describe
1812 * for journal abort.
1813 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001814 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001815 * itself are here.
1816 */
1817
1818/*
1819 * Quick version for internal journal use (doesn't lock the journal).
1820 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1821 * and don't attempt to make any other journal updates.
1822 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001823void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001824{
1825 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001826
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001827 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828 return;
1829
1830 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001831 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001832
Theodore Ts'oa931da62010-08-03 21:35:12 -04001833 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001834 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001835 transaction = journal->j_running_transaction;
1836 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001837 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001838 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001839}
1840
1841/* Soft abort: record the abort error status in the journal superblock,
1842 * but don't do any other IO. */
1843static void __journal_abort_soft (journal_t *journal, int errno)
1844{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001845 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001846 return;
1847
1848 if (!journal->j_errno)
1849 journal->j_errno = errno;
1850
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001851 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001852
1853 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04001854 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855}
1856
1857/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001858 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001859 * @journal: the journal to shutdown.
1860 * @errno: an error number to record in the journal indicating
1861 * the reason for the shutdown.
1862 *
1863 * Perform a complete, immediate shutdown of the ENTIRE
1864 * journal (not of a single transaction). This operation cannot be
1865 * undone without closing and reopening the journal.
1866 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001867 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001868 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1869 * mode.
1870 *
1871 * Journal abort has very specific semantics. Any existing dirty,
1872 * unjournaled buffers in the main filesystem will still be written to
1873 * disk by bdflush, but the journaling mechanism will be suspended
1874 * immediately and no further transaction commits will be honoured.
1875 *
1876 * Any dirty, journaled buffers will be written back to disk without
1877 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1878 * filesystem, but we _do_ attempt to leave as much data as possible
1879 * behind for fsck to use for cleanup.
1880 *
1881 * Any attempt to get a new transaction handle on a journal which is in
1882 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001883 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001884 * entered abort state during the update.
1885 *
1886 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001887 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001888 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001889 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890 * which will be recorded (if possible) in the journal superblock. This
1891 * allows a client to record failure conditions in the middle of a
1892 * transaction without having to complete the transaction to record the
1893 * failure to disk. ext3_error, for example, now uses this
1894 * functionality.
1895 *
1896 * Errors which originate from within the journaling layer will NOT
1897 * supply an errno; a null errno implies that absolutely no further
1898 * writes are done to the journal (unless there are any already in
1899 * progress).
1900 *
1901 */
1902
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001903void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001904{
1905 __journal_abort_soft(journal, errno);
1906}
1907
1908/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001909 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 * @journal: journal to examine.
1911 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001912 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 * time the journal was mounted - if the journal was stopped
1914 * without calling abort this will be 0.
1915 *
1916 * If the journal has been aborted on this mount time -EROFS will
1917 * be returned.
1918 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001919int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001920{
1921 int err;
1922
Theodore Ts'oa931da62010-08-03 21:35:12 -04001923 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001924 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925 err = -EROFS;
1926 else
1927 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001928 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001929 return err;
1930}
1931
1932/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001933 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934 * @journal: journal to act on.
1935 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001936 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001937 * mode.
1938 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001939int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001940{
1941 int err = 0;
1942
Theodore Ts'oa931da62010-08-03 21:35:12 -04001943 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001944 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001945 err = -EROFS;
1946 else
1947 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001948 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001949 return err;
1950}
1951
1952/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001953 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001954 * @journal: journal to act on.
1955 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001956 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 * mode.
1958 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001959void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001961 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001963 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001964 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001965}
1966
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001967int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968{
1969 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1970}
1971
1972/*
Zach Brownb517bea2006-10-11 01:21:08 -07001973 * helper functions to deal with 32 or 64bit block numbers.
1974 */
1975size_t journal_tag_bytes(journal_t *journal)
1976{
1977 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001978 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001979 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001980 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001981}
1982
1983/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001984 * JBD memory management
1985 *
1986 * These functions are used to allocate block-sized chunks of memory
1987 * used for making copies of buffer_head data. Very often it will be
1988 * page-sized chunks of data, but sometimes it will be in
1989 * sub-page-size chunks. (For example, 16k pages on Power systems
1990 * with a 4k block file system.) For blocks smaller than a page, we
1991 * use a SLAB allocator. There are slab caches for each block size,
1992 * which are allocated at mount time, if necessary, and we only free
1993 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1994 * this reason we don't need to a mutex to protect access to
1995 * jbd2_slab[] allocating or releasing memory; only in
1996 * jbd2_journal_create_slab().
1997 */
1998#define JBD2_MAX_SLABS 8
1999static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002000
2001static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2002 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2003 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2004};
2005
2006
2007static void jbd2_journal_destroy_slabs(void)
2008{
2009 int i;
2010
2011 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2012 if (jbd2_slab[i])
2013 kmem_cache_destroy(jbd2_slab[i]);
2014 jbd2_slab[i] = NULL;
2015 }
2016}
2017
2018static int jbd2_journal_create_slab(size_t size)
2019{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002020 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002021 int i = order_base_2(size) - 10;
2022 size_t slab_size;
2023
2024 if (size == PAGE_SIZE)
2025 return 0;
2026
2027 if (i >= JBD2_MAX_SLABS)
2028 return -EINVAL;
2029
2030 if (unlikely(i < 0))
2031 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002032 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002033 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002034 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002035 return 0; /* Already created */
2036 }
2037
2038 slab_size = 1 << (i+10);
2039 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2040 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002041 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002042 if (!jbd2_slab[i]) {
2043 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2044 return -ENOMEM;
2045 }
2046 return 0;
2047}
2048
2049static struct kmem_cache *get_slab(size_t size)
2050{
2051 int i = order_base_2(size) - 10;
2052
2053 BUG_ON(i >= JBD2_MAX_SLABS);
2054 if (unlikely(i < 0))
2055 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002056 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002057 return jbd2_slab[i];
2058}
2059
2060void *jbd2_alloc(size_t size, gfp_t flags)
2061{
2062 void *ptr;
2063
2064 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2065
2066 flags |= __GFP_REPEAT;
2067 if (size == PAGE_SIZE)
2068 ptr = (void *)__get_free_pages(flags, 0);
2069 else if (size > PAGE_SIZE) {
2070 int order = get_order(size);
2071
2072 if (order < 3)
2073 ptr = (void *)__get_free_pages(flags, order);
2074 else
2075 ptr = vmalloc(size);
2076 } else
2077 ptr = kmem_cache_alloc(get_slab(size), flags);
2078
2079 /* Check alignment; SLUB has gotten this wrong in the past,
2080 * and this can lead to user data corruption! */
2081 BUG_ON(((unsigned long) ptr) & (size-1));
2082
2083 return ptr;
2084}
2085
2086void jbd2_free(void *ptr, size_t size)
2087{
2088 if (size == PAGE_SIZE) {
2089 free_pages((unsigned long)ptr, 0);
2090 return;
2091 }
2092 if (size > PAGE_SIZE) {
2093 int order = get_order(size);
2094
2095 if (order < 3)
2096 free_pages((unsigned long)ptr, order);
2097 else
2098 vfree(ptr);
2099 return;
2100 }
2101 kmem_cache_free(get_slab(size), ptr);
2102};
2103
2104/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002105 * Journal_head storage management
2106 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002107static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002108#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002109static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2110#endif
2111
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002112static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002113{
2114 int retval;
2115
Al Viro1076d172008-03-29 03:07:18 +00002116 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002117 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002118 sizeof(struct journal_head),
2119 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002120 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002121 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002122 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002123 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002124 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002125 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002126 }
2127 return retval;
2128}
2129
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002130static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002131{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002132 if (jbd2_journal_head_cache) {
2133 kmem_cache_destroy(jbd2_journal_head_cache);
2134 jbd2_journal_head_cache = NULL;
2135 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002136}
2137
2138/*
2139 * journal_head splicing and dicing
2140 */
2141static struct journal_head *journal_alloc_journal_head(void)
2142{
2143 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002144
Jose R. Santose23291b2007-07-18 08:57:06 -04002145#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002146 atomic_inc(&nr_journal_heads);
2147#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002148 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002149 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002150 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002151 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002152 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002153 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002154 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002155 }
2156 }
2157 return ret;
2158}
2159
2160static void journal_free_journal_head(struct journal_head *jh)
2161{
Jose R. Santose23291b2007-07-18 08:57:06 -04002162#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002163 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002164 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002165#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002166 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002167}
2168
2169/*
2170 * A journal_head is attached to a buffer_head whenever JBD has an
2171 * interest in the buffer.
2172 *
2173 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2174 * is set. This bit is tested in core kernel code where we need to take
2175 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2176 * there.
2177 *
2178 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2179 *
2180 * When a buffer has its BH_JBD bit set it is immune from being released by
2181 * core kernel code, mainly via ->b_count.
2182 *
Jan Karade1b7942011-06-13 15:38:22 -04002183 * A journal_head is detached from its buffer_head when the journal_head's
2184 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2185 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002186 *
2187 * Various places in the kernel want to attach a journal_head to a buffer_head
2188 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002189 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002190 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002191 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002192 *
2193 * So the typical usage would be:
2194 *
2195 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002196 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002197 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002198 * (Get another reference for transaction)
2199 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002200 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002201 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002202 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002203 */
2204
2205/*
2206 * Give a buffer_head a journal_head.
2207 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002208 * May sleep.
2209 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002210struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002211{
2212 struct journal_head *jh;
2213 struct journal_head *new_jh = NULL;
2214
2215repeat:
2216 if (!buffer_jbd(bh)) {
2217 new_jh = journal_alloc_journal_head();
2218 memset(new_jh, 0, sizeof(*new_jh));
2219 }
2220
2221 jbd_lock_bh_journal_head(bh);
2222 if (buffer_jbd(bh)) {
2223 jh = bh2jh(bh);
2224 } else {
2225 J_ASSERT_BH(bh,
2226 (atomic_read(&bh->b_count) > 0) ||
2227 (bh->b_page && bh->b_page->mapping));
2228
2229 if (!new_jh) {
2230 jbd_unlock_bh_journal_head(bh);
2231 goto repeat;
2232 }
2233
2234 jh = new_jh;
2235 new_jh = NULL; /* We consumed it */
2236 set_buffer_jbd(bh);
2237 bh->b_private = jh;
2238 jh->b_bh = bh;
2239 get_bh(bh);
2240 BUFFER_TRACE(bh, "added journal_head");
2241 }
2242 jh->b_jcount++;
2243 jbd_unlock_bh_journal_head(bh);
2244 if (new_jh)
2245 journal_free_journal_head(new_jh);
2246 return bh->b_private;
2247}
2248
2249/*
2250 * Grab a ref against this buffer_head's journal_head. If it ended up not
2251 * having a journal_head, return NULL
2252 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002253struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254{
2255 struct journal_head *jh = NULL;
2256
2257 jbd_lock_bh_journal_head(bh);
2258 if (buffer_jbd(bh)) {
2259 jh = bh2jh(bh);
2260 jh->b_jcount++;
2261 }
2262 jbd_unlock_bh_journal_head(bh);
2263 return jh;
2264}
2265
2266static void __journal_remove_journal_head(struct buffer_head *bh)
2267{
2268 struct journal_head *jh = bh2jh(bh);
2269
2270 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002271 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2272 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2273 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2274 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2275 J_ASSERT_BH(bh, buffer_jbd(bh));
2276 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2277 BUFFER_TRACE(bh, "remove journal_head");
2278 if (jh->b_frozen_data) {
2279 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2280 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002281 }
Jan Karade1b7942011-06-13 15:38:22 -04002282 if (jh->b_committed_data) {
2283 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2284 jbd2_free(jh->b_committed_data, bh->b_size);
2285 }
2286 bh->b_private = NULL;
2287 jh->b_bh = NULL; /* debug, really */
2288 clear_buffer_jbd(bh);
2289 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002290}
2291
2292/*
Jan Karade1b7942011-06-13 15:38:22 -04002293 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002294 * release the journal_head from the buffer_head.
2295 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002296void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002297{
2298 struct buffer_head *bh = jh2bh(jh);
2299
2300 jbd_lock_bh_journal_head(bh);
2301 J_ASSERT_JH(jh, jh->b_jcount > 0);
2302 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002303 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002304 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002305 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002306 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002307 } else
2308 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002309}
2310
2311/*
Jan Karac851ed52008-07-11 19:27:31 -04002312 * Initialize jbd inode head
2313 */
2314void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2315{
2316 jinode->i_transaction = NULL;
2317 jinode->i_next_transaction = NULL;
2318 jinode->i_vfs_inode = inode;
2319 jinode->i_flags = 0;
2320 INIT_LIST_HEAD(&jinode->i_list);
2321}
2322
2323/*
2324 * Function to be called before we start removing inode from memory (i.e.,
2325 * clear_inode() is a fine place to be called from). It removes inode from
2326 * transaction's lists.
2327 */
2328void jbd2_journal_release_jbd_inode(journal_t *journal,
2329 struct jbd2_inode *jinode)
2330{
Jan Karac851ed52008-07-11 19:27:31 -04002331 if (!journal)
2332 return;
2333restart:
2334 spin_lock(&journal->j_list_lock);
2335 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002336 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002337 wait_queue_head_t *wq;
2338 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2339 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2340 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2341 spin_unlock(&journal->j_list_lock);
2342 schedule();
2343 finish_wait(wq, &wait.wait);
2344 goto restart;
2345 }
2346
Jan Karac851ed52008-07-11 19:27:31 -04002347 if (jinode->i_transaction) {
2348 list_del(&jinode->i_list);
2349 jinode->i_transaction = NULL;
2350 }
2351 spin_unlock(&journal->j_list_lock);
2352}
2353
2354/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002355 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002356 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002357#ifdef CONFIG_JBD2_DEBUG
2358u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002359EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002361#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362
Jose R. Santos6f38c742007-10-16 18:38:25 -04002363static struct dentry *jbd2_debugfs_dir;
2364static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002365
2366static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002367{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002368 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2369 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002370 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2371 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002372 jbd2_debugfs_dir,
2373 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002374}
2375
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002376static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002377{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002378 debugfs_remove(jbd2_debug);
2379 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002380}
2381
2382#else
2383
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002384static void __init jbd2_create_debugfs_entry(void)
2385{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002386}
2387
2388static void __exit jbd2_remove_debugfs_entry(void)
2389{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002390}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002391
2392#endif
2393
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002394#ifdef CONFIG_PROC_FS
2395
2396#define JBD2_STATS_PROC_NAME "fs/jbd2"
2397
2398static void __init jbd2_create_jbd_stats_proc_entry(void)
2399{
2400 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2401}
2402
2403static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2404{
2405 if (proc_jbd2_stats)
2406 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2407}
2408
2409#else
2410
2411#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2412#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2413
2414#endif
2415
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002416struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002417
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002418static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002419{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002420 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002421 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002422 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2423 return -ENOMEM;
2424 }
2425 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2426 if (jbd2_inode_cache == NULL) {
2427 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2428 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002429 return -ENOMEM;
2430 }
2431 return 0;
2432}
2433
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002434static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002435{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002436 if (jbd2_handle_cache)
2437 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002438 if (jbd2_inode_cache)
2439 kmem_cache_destroy(jbd2_inode_cache);
2440
Dave Kleikamp470decc2006-10-11 01:20:57 -07002441}
2442
2443/*
2444 * Module startup and shutdown
2445 */
2446
2447static int __init journal_init_caches(void)
2448{
2449 int ret;
2450
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002451 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002452 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002453 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002454 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002455 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002456 if (ret == 0)
2457 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002458 return ret;
2459}
2460
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002461static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002462{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002463 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002464 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002465 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002466 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002467 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002468}
2469
2470static int __init journal_init(void)
2471{
2472 int ret;
2473
2474 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2475
2476 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002477 if (ret == 0) {
2478 jbd2_create_debugfs_entry();
2479 jbd2_create_jbd_stats_proc_entry();
2480 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002481 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002482 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002483 return ret;
2484}
2485
2486static void __exit journal_exit(void)
2487{
Jose R. Santose23291b2007-07-18 08:57:06 -04002488#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002489 int n = atomic_read(&nr_journal_heads);
2490 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002491 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002492#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002493 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002494 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002495 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002496}
2497
Dave Kleikamp470decc2006-10-11 01:20:57 -07002498MODULE_LICENSE("GPL");
2499module_init(journal_init);
2500module_exit(journal_exit);
2501