blob: 9a78269903041934f0896148e1999e8467fd0872 [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);
74EXPORT_SYMBOL(jbd2_journal_update_format);
75EXPORT_SYMBOL(jbd2_journal_check_used_features);
76EXPORT_SYMBOL(jbd2_journal_check_available_features);
77EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070078EXPORT_SYMBOL(jbd2_journal_load);
79EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070080EXPORT_SYMBOL(jbd2_journal_abort);
81EXPORT_SYMBOL(jbd2_journal_errno);
82EXPORT_SYMBOL(jbd2_journal_ack_err);
83EXPORT_SYMBOL(jbd2_journal_clear_err);
84EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050085EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070086EXPORT_SYMBOL(jbd2_journal_start_commit);
87EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
88EXPORT_SYMBOL(jbd2_journal_wipe);
89EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
90EXPORT_SYMBOL(jbd2_journal_invalidatepage);
91EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
92EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040093EXPORT_SYMBOL(jbd2_journal_file_inode);
94EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
95EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
96EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050097EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -070098
99static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
100static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -0500101static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700102
103/*
104 * Helper function used to manage commit timeouts
105 */
106
107static void commit_timeout(unsigned long __data)
108{
109 struct task_struct * p = (struct task_struct *) __data;
110
111 wake_up_process(p);
112}
113
114/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700115 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700116 * journal.
117 *
118 * This kernel thread is responsible for two things:
119 *
120 * 1) COMMIT: Every so often we need to commit the current state of the
121 * filesystem to disk. The journal thread is responsible for writing
122 * all of the metadata buffers to disk.
123 *
124 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
125 * of the data in that part of the log has been rewritten elsewhere on
126 * the disk. Flushing these old buffers to reclaim space in the log is
127 * known as checkpointing, and this thread is responsible for that job.
128 */
129
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700130static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700131{
132 journal_t *journal = arg;
133 transaction_t *transaction;
134
135 /*
136 * Set up an interval timer which can be used to trigger a commit wakeup
137 * after the commit interval expires
138 */
139 setup_timer(&journal->j_commit_timer, commit_timeout,
140 (unsigned long)current);
141
142 /* Record that the journal thread is running */
143 journal->j_task = current;
144 wake_up(&journal->j_wait_done_commit);
145
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146 /*
147 * And now, wait forever for commit wakeup events.
148 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400149 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150
151loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700152 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700153 goto end_loop;
154
155 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
156 journal->j_commit_sequence, journal->j_commit_request);
157
158 if (journal->j_commit_sequence != journal->j_commit_request) {
159 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400160 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700161 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700162 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400163 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700164 goto loop;
165 }
166
167 wake_up(&journal->j_wait_done_commit);
168 if (freezing(current)) {
169 /*
170 * The simpler the better. Flushing journal isn't a
171 * good idea, because that depends on threads that may
172 * be already stopped.
173 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700174 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400175 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700176 refrigerator();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400177 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700178 } else {
179 /*
180 * We assume on resume that commits are already there,
181 * so we don't sleep
182 */
183 DEFINE_WAIT(wait);
184 int should_sleep = 1;
185
186 prepare_to_wait(&journal->j_wait_commit, &wait,
187 TASK_INTERRUPTIBLE);
188 if (journal->j_commit_sequence != journal->j_commit_request)
189 should_sleep = 0;
190 transaction = journal->j_running_transaction;
191 if (transaction && time_after_eq(jiffies,
192 transaction->t_expires))
193 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700194 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700195 should_sleep = 0;
196 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400197 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400199 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700200 }
201 finish_wait(&journal->j_wait_commit, &wait);
202 }
203
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700204 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700205
206 /*
207 * Were we woken up by a commit wakeup event?
208 */
209 transaction = journal->j_running_transaction;
210 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
211 journal->j_commit_request = transaction->t_tid;
212 jbd_debug(1, "woke because of timeout\n");
213 }
214 goto loop;
215
216end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400217 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700218 del_timer_sync(&journal->j_commit_timer);
219 journal->j_task = NULL;
220 wake_up(&journal->j_wait_done_commit);
221 jbd_debug(1, "Journal thread exiting.\n");
222 return 0;
223}
224
Pavel Emelianov97f06782007-05-08 00:30:42 -0700225static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700226{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227 struct task_struct *t;
228
Theodore Ts'o90576c02009-09-29 15:51:30 -0400229 t = kthread_run(kjournald2, journal, "jbd2/%s",
230 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700231 if (IS_ERR(t))
232 return PTR_ERR(t);
233
Al Viro1076d172008-03-29 03:07:18 +0000234 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700235 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700236}
237
238static void journal_kill_thread(journal_t *journal)
239{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400240 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700241 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700242
243 while (journal->j_task) {
244 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400245 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000246 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400247 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700248 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400249 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250}
251
252/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700253 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700254 *
255 * Writes a metadata buffer to a given disk block. The actual IO is not
256 * performed but a new buffer_head is constructed which labels the data
257 * to be written with the correct destination disk block.
258 *
259 * Any magic-number escaping which needs to be done will cause a
260 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700261 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700262 * magic number is only written to the log for descripter blocks. In
263 * this case, we copy the data and replace the first word with 0, and we
264 * return a result code which indicates that this buffer needs to be
265 * marked as an escaped buffer in the corresponding log descriptor
266 * block. The missing word can then be restored when the block is read
267 * during recovery.
268 *
269 * If the source buffer has already been modified by a new transaction
270 * since we took the last commit snapshot, we use the frozen copy of
271 * that data for IO. If we end up using the existing buffer_head's data
272 * for the write, then we *have* to lock the buffer to prevent anyone
273 * else from using and possibly modifying it while the IO is in
274 * progress.
275 *
276 * The function returns a pointer to the buffer_heads to be used for IO.
277 *
278 * We assume that the journal has already been locked in this function.
279 *
280 * Return value:
281 * <0: Error
282 * >=0: Finished OK
283 *
284 * On success:
285 * Bit 0 set == escape performed on the data
286 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
287 */
288
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700289int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700290 struct journal_head *jh_in,
291 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700292 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700293{
294 int need_copy_out = 0;
295 int done_copy_out = 0;
296 int do_escape = 0;
297 char *mapped_data;
298 struct buffer_head *new_bh;
299 struct journal_head *new_jh;
300 struct page *new_page;
301 unsigned int new_offset;
302 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400303 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700304
305 /*
306 * The buffer really shouldn't be locked: only the current committing
307 * transaction is allowed to write it, so nobody else is allowed
308 * to do any IO.
309 *
310 * akpm: except if we're journalling data, and write() output is
311 * also part of a shared mapping, and another thread has
312 * decided to launch a writepage() against this buffer.
313 */
314 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
315
Theodore Ts'o47def822010-07-27 11:56:05 -0400316retry_alloc:
317 new_bh = alloc_buffer_head(GFP_NOFS);
318 if (!new_bh) {
319 /*
320 * Failure is not an option, but __GFP_NOFAIL is going
321 * away; so we retry ourselves here.
322 */
323 congestion_wait(BLK_RW_ASYNC, HZ/50);
324 goto retry_alloc;
325 }
326
dingdinghua96577c42009-07-13 17:55:35 -0400327 /* keep subsequent assertions sane */
328 new_bh->b_state = 0;
329 init_buffer(new_bh, NULL, NULL);
330 atomic_set(&new_bh->b_count, 1);
331 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700332
333 /*
334 * If a new transaction has already done a buffer copy-out, then
335 * we use that version of the data for the commit.
336 */
337 jbd_lock_bh_state(bh_in);
338repeat:
339 if (jh_in->b_frozen_data) {
340 done_copy_out = 1;
341 new_page = virt_to_page(jh_in->b_frozen_data);
342 new_offset = offset_in_page(jh_in->b_frozen_data);
343 } else {
344 new_page = jh2bh(jh_in)->b_page;
345 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
346 }
347
348 mapped_data = kmap_atomic(new_page, KM_USER0);
349 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200350 * Fire data frozen trigger if data already wasn't frozen. Do this
351 * before checking for escaping, as the trigger may modify the magic
352 * offset. If a copy-out happens afterwards, it will have the correct
353 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700354 */
Jan Kara13ceef02010-07-14 07:56:33 +0200355 if (!done_copy_out)
356 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
357 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700358
359 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700360 * Check for escaping
361 */
362 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700363 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700364 need_copy_out = 1;
365 do_escape = 1;
366 }
367 kunmap_atomic(mapped_data, KM_USER0);
368
369 /*
370 * Do we need to do a data copy?
371 */
372 if (need_copy_out && !done_copy_out) {
373 char *tmp;
374
375 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400376 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500377 if (!tmp) {
378 jbd2_journal_put_journal_head(new_jh);
379 return -ENOMEM;
380 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700381 jbd_lock_bh_state(bh_in);
382 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400383 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700384 goto repeat;
385 }
386
387 jh_in->b_frozen_data = tmp;
388 mapped_data = kmap_atomic(new_page, KM_USER0);
389 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
390 kunmap_atomic(mapped_data, KM_USER0);
391
392 new_page = virt_to_page(tmp);
393 new_offset = offset_in_page(tmp);
394 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700395
396 /*
397 * This isn't strictly necessary, as we're using frozen
398 * data for the escaping, but it keeps consistency with
399 * b_frozen_data usage.
400 */
401 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700402 }
403
404 /*
405 * Did we need to do an escaping? Now we've done all the
406 * copying, we can finally do so.
407 */
408 if (do_escape) {
409 mapped_data = kmap_atomic(new_page, KM_USER0);
410 *((unsigned int *)(mapped_data + new_offset)) = 0;
411 kunmap_atomic(mapped_data, KM_USER0);
412 }
413
Dave Kleikamp470decc2006-10-11 01:20:57 -0700414 set_bh_page(new_bh, new_page, new_offset);
415 new_jh->b_transaction = NULL;
416 new_bh->b_size = jh2bh(jh_in)->b_size;
417 new_bh->b_bdev = transaction->t_journal->j_dev;
418 new_bh->b_blocknr = blocknr;
419 set_buffer_mapped(new_bh);
420 set_buffer_dirty(new_bh);
421
422 *jh_out = new_jh;
423
424 /*
425 * The to-be-written buffer needs to get moved to the io queue,
426 * and the original buffer whose contents we are shadowing or
427 * copying is moved to the transaction's shadow queue.
428 */
429 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400430 spin_lock(&journal->j_list_lock);
431 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
432 spin_unlock(&journal->j_list_lock);
433 jbd_unlock_bh_state(bh_in);
434
Dave Kleikamp470decc2006-10-11 01:20:57 -0700435 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700436 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437
438 return do_escape | (done_copy_out << 1);
439}
440
441/*
442 * Allocation code for the journal file. Manage the space left in the
443 * journal, so that we can begin checkpointing when appropriate.
444 */
445
446/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700447 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700448 *
449 * Called with the journal already locked.
450 *
451 * Called under j_state_lock
452 */
453
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700454int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700455{
456 int left = journal->j_free;
457
Theodore Ts'oa931da62010-08-03 21:35:12 -0400458 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459
460 /*
461 * Be pessimistic here about the number of those free blocks which
462 * might be required for log descriptor control blocks.
463 */
464
465#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
466
467 left -= MIN_LOG_RESERVED_BLOCKS;
468
469 if (left <= 0)
470 return 0;
471 left -= (left >> 3);
472 return left;
473}
474
475/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500476 * Called with j_state_lock locked for writing.
477 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700479int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700480{
481 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400482 * The only transaction we can possibly wait upon is the
483 * currently running transaction (if it exists). Otherwise,
484 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400486 if (journal->j_running_transaction &&
487 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700488 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200489 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700490 * commit thread. We do _not_ do the commit ourselves.
491 */
492
493 journal->j_commit_request = target;
494 jbd_debug(1, "JBD: requesting commit %d/%d\n",
495 journal->j_commit_request,
496 journal->j_commit_sequence);
497 wake_up(&journal->j_wait_commit);
498 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400499 } else if (!tid_geq(journal->j_commit_request, target))
500 /* This should never happen, but if it does, preserve
501 the evidence before kjournald goes into a loop and
502 increments j_commit_sequence beyond all recognition. */
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400503 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
504 journal->j_commit_request,
505 journal->j_commit_sequence,
506 target, journal->j_running_transaction ?
507 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700508 return 0;
509}
510
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700511int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512{
513 int ret;
514
Theodore Ts'oa931da62010-08-03 21:35:12 -0400515 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700516 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400517 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700518 return ret;
519}
520
521/*
522 * Force and wait upon a commit if the calling process is not within
523 * transaction. This is used for forcing out undo-protected data which contains
524 * bitmaps, when the fs is running out of space.
525 *
526 * We can only force the running transaction if we don't have an active handle;
527 * otherwise, we will deadlock.
528 *
529 * Returns true if a transaction was started.
530 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700531int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532{
533 transaction_t *transaction = NULL;
534 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500535 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536
Theodore Ts'oa931da62010-08-03 21:35:12 -0400537 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700538 if (journal->j_running_transaction && !current->journal_info) {
539 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500540 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
541 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 } else if (journal->j_committing_transaction)
543 transaction = journal->j_committing_transaction;
544
545 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400546 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547 return 0; /* Nothing to retry */
548 }
549
550 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400551 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500552 if (need_to_start)
553 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700554 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555 return 1;
556}
557
558/*
559 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500560 * if a transaction is going to be committed (or is currently already
561 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700562 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700563int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564{
565 int ret = 0;
566
Theodore Ts'oa931da62010-08-03 21:35:12 -0400567 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568 if (journal->j_running_transaction) {
569 tid_t tid = journal->j_running_transaction->t_tid;
570
Jan Karac88ccea2009-02-10 11:27:46 -0500571 __jbd2_log_start_commit(journal, tid);
572 /* There's a running transaction and we've just made sure
573 * it's commit has been scheduled. */
574 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700575 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500576 ret = 1;
577 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700578 /*
579 * If ext3_write_super() recently started a commit, then we
580 * have to wait for completion of that transaction
581 */
Jan Karac88ccea2009-02-10 11:27:46 -0500582 if (ptid)
583 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700584 ret = 1;
585 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400586 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587 return ret;
588}
589
590/*
Jan Karabbd2be32011-05-24 11:59:18 -0400591 * Return 1 if a given transaction has not yet sent barrier request
592 * connected with a transaction commit. If 0 is returned, transaction
593 * may or may not have sent the barrier. Used to avoid sending barrier
594 * twice in common cases.
595 */
596int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
597{
598 int ret = 0;
599 transaction_t *commit_trans;
600
601 if (!(journal->j_flags & JBD2_BARRIER))
602 return 0;
603 read_lock(&journal->j_state_lock);
604 /* Transaction already committed? */
605 if (tid_geq(journal->j_commit_sequence, tid))
606 goto out;
607 commit_trans = journal->j_committing_transaction;
608 if (!commit_trans || commit_trans->t_tid != tid) {
609 ret = 1;
610 goto out;
611 }
612 /*
613 * Transaction is being committed and we already proceeded to
614 * submitting a flush to fs partition?
615 */
616 if (journal->j_fs_dev != journal->j_dev) {
617 if (!commit_trans->t_need_data_flush ||
618 commit_trans->t_state >= T_COMMIT_DFLUSH)
619 goto out;
620 } else {
621 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
622 goto out;
623 }
624 ret = 1;
625out:
626 read_unlock(&journal->j_state_lock);
627 return ret;
628}
629EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
630
631/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700632 * Wait for a specified commit to complete.
633 * The caller may not hold the journal lock.
634 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700635int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700636{
637 int err = 0;
638
Theodore Ts'oa931da62010-08-03 21:35:12 -0400639 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400640#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700641 if (!tid_geq(journal->j_commit_request, tid)) {
642 printk(KERN_EMERG
643 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400644 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647 while (tid_gt(tid, journal->j_commit_sequence)) {
648 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
649 tid, journal->j_commit_sequence);
650 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400651 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700652 wait_event(journal->j_wait_done_commit,
653 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400654 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700655 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400656 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700657
658 if (unlikely(is_journal_aborted(journal))) {
659 printk(KERN_EMERG "journal commit I/O error\n");
660 err = -EIO;
661 }
662 return err;
663}
664
665/*
666 * Log buffer allocation routines:
667 */
668
Mingming Cao18eba7a2006-10-11 01:21:13 -0700669int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700670{
671 unsigned long blocknr;
672
Theodore Ts'oa931da62010-08-03 21:35:12 -0400673 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700674 J_ASSERT(journal->j_free > 1);
675
676 blocknr = journal->j_head;
677 journal->j_head++;
678 journal->j_free--;
679 if (journal->j_head == journal->j_last)
680 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400681 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700682 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700683}
684
685/*
686 * Conversion of logical to physical block numbers for the journal
687 *
688 * On external journals the journal blocks are identity-mapped, so
689 * this is a no-op. If needed, we can use j_blk_offset - everything is
690 * ready.
691 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700692int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700693 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700694{
695 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700696 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700697
698 if (journal->j_inode) {
699 ret = bmap(journal->j_inode, blocknr);
700 if (ret)
701 *retp = ret;
702 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700703 printk(KERN_ALERT "%s: journal block not found "
704 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400705 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700706 err = -EIO;
707 __journal_abort_soft(journal, err);
708 }
709 } else {
710 *retp = blocknr; /* +journal->j_blk_offset */
711 }
712 return err;
713}
714
715/*
716 * We play buffer_head aliasing tricks to write data/metadata blocks to
717 * the journal without copying their contents, but for journal
718 * descriptor blocks we do need to generate bona fide buffers.
719 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700720 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700721 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
722 * But we don't bother doing that, so there will be coherency problems with
723 * mmaps of blockdevs which hold live JBD-controlled filesystems.
724 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700725struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700726{
727 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700728 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700729 int err;
730
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700731 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700732
733 if (err)
734 return NULL;
735
736 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500737 if (!bh)
738 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700739 lock_buffer(bh);
740 memset(bh->b_data, 0, journal->j_blocksize);
741 set_buffer_uptodate(bh);
742 unlock_buffer(bh);
743 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700744 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700745}
746
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500747struct jbd2_stats_proc_session {
748 journal_t *journal;
749 struct transaction_stats_s *stats;
750 int start;
751 int max;
752};
753
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500754static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
755{
756 return *pos ? NULL : SEQ_START_TOKEN;
757}
758
759static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
760{
761 return NULL;
762}
763
764static int jbd2_seq_info_show(struct seq_file *seq, void *v)
765{
766 struct jbd2_stats_proc_session *s = seq->private;
767
768 if (v != SEQ_START_TOKEN)
769 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400770 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500771 s->stats->ts_tid,
772 s->journal->j_max_transaction_buffers);
773 if (s->stats->ts_tid == 0)
774 return 0;
775 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400776 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500777 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400778 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500779 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400780 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500781 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400782 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500783 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400784 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500785 seq_printf(seq, " %lluus average transaction commit time\n",
786 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500787 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400788 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500789 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400790 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500791 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400792 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500793 return 0;
794}
795
796static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
797{
798}
799
James Morris88e9d342009-09-22 16:43:43 -0700800static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500801 .start = jbd2_seq_info_start,
802 .next = jbd2_seq_info_next,
803 .stop = jbd2_seq_info_stop,
804 .show = jbd2_seq_info_show,
805};
806
807static int jbd2_seq_info_open(struct inode *inode, struct file *file)
808{
809 journal_t *journal = PDE(inode)->data;
810 struct jbd2_stats_proc_session *s;
811 int rc, size;
812
813 s = kmalloc(sizeof(*s), GFP_KERNEL);
814 if (s == NULL)
815 return -ENOMEM;
816 size = sizeof(struct transaction_stats_s);
817 s->stats = kmalloc(size, GFP_KERNEL);
818 if (s->stats == NULL) {
819 kfree(s);
820 return -ENOMEM;
821 }
822 spin_lock(&journal->j_history_lock);
823 memcpy(s->stats, &journal->j_stats, size);
824 s->journal = journal;
825 spin_unlock(&journal->j_history_lock);
826
827 rc = seq_open(file, &jbd2_seq_info_ops);
828 if (rc == 0) {
829 struct seq_file *m = file->private_data;
830 m->private = s;
831 } else {
832 kfree(s->stats);
833 kfree(s);
834 }
835 return rc;
836
837}
838
839static int jbd2_seq_info_release(struct inode *inode, struct file *file)
840{
841 struct seq_file *seq = file->private_data;
842 struct jbd2_stats_proc_session *s = seq->private;
843 kfree(s->stats);
844 kfree(s);
845 return seq_release(inode, file);
846}
847
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700848static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500849 .owner = THIS_MODULE,
850 .open = jbd2_seq_info_open,
851 .read = seq_read,
852 .llseek = seq_lseek,
853 .release = jbd2_seq_info_release,
854};
855
856static struct proc_dir_entry *proc_jbd2_stats;
857
858static void jbd2_stats_proc_init(journal_t *journal)
859{
Theodore Ts'o05496762008-09-16 14:36:17 -0400860 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500861 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700862 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
863 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500864 }
865}
866
867static void jbd2_stats_proc_exit(journal_t *journal)
868{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500869 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400870 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500871}
872
Dave Kleikamp470decc2006-10-11 01:20:57 -0700873/*
874 * Management for journal control blocks: functions to create and
875 * destroy journal_t structures, and to initialise and read existing
876 * journal blocks from disk. */
877
878/* First: create and setup a journal_t object in memory. We initialise
879 * very few fields yet: that has to wait until we have created the
880 * journal structures from from scratch, or loaded them from disk. */
881
882static journal_t * journal_init_common (void)
883{
884 journal_t *journal;
885 int err;
886
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500887 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700888 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500889 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700890
891 init_waitqueue_head(&journal->j_wait_transaction_locked);
892 init_waitqueue_head(&journal->j_wait_logspace);
893 init_waitqueue_head(&journal->j_wait_done_commit);
894 init_waitqueue_head(&journal->j_wait_checkpoint);
895 init_waitqueue_head(&journal->j_wait_commit);
896 init_waitqueue_head(&journal->j_wait_updates);
897 mutex_init(&journal->j_barrier);
898 mutex_init(&journal->j_checkpoint_mutex);
899 spin_lock_init(&journal->j_revoke_lock);
900 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400901 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700902
Mingming Caocd02ff02007-10-16 18:38:25 -0400903 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500904 journal->j_min_batch_time = 0;
905 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700906
907 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700908 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700909
910 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700911 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700912 if (err) {
913 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500914 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700915 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500916
Theodore Ts'obf699322009-09-30 00:32:06 -0400917 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500918
Dave Kleikamp470decc2006-10-11 01:20:57 -0700919 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700920}
921
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700922/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700923 *
924 * Create a journal structure assigned some fixed set of disk blocks to
925 * the journal. We don't actually touch those disk blocks yet, but we
926 * need to set up all of the mapping information to tell the journaling
927 * system where the journal blocks are.
928 *
929 */
930
931/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400932 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700933 * @bdev: Block device on which to create the journal
934 * @fs_dev: Device which hold journalled filesystem for this journal.
935 * @start: Block nr Start of journal.
936 * @len: Length of the journal in blocks.
937 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400938 *
939 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700940 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700941 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700942 * range of blocks on an arbitrary block device.
943 *
944 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700945journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700946 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700947 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700948{
949 journal_t *journal = journal_init_common();
950 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400951 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700952 int n;
953
954 if (!journal)
955 return NULL;
956
957 /* journal descriptor can store up to n blocks -bzzz */
958 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -0500959 journal->j_dev = bdev;
960 journal->j_fs_dev = fs_dev;
961 journal->j_blk_offset = start;
962 journal->j_maxlen = len;
963 bdevname(journal->j_dev, journal->j_devname);
964 p = journal->j_devname;
965 while ((p = strchr(p, '/')))
966 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -0500967 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700968 n = journal->j_blocksize / sizeof(journal_block_tag_t);
969 journal->j_wbufsize = n;
970 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
971 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300972 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400973 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500974 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700976
977 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500978 if (!bh) {
979 printk(KERN_ERR
980 "%s: Cannot get buffer for journal superblock\n",
981 __func__);
982 goto out_err;
983 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700984 journal->j_sb_buffer = bh;
985 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500986
Dave Kleikamp470decc2006-10-11 01:20:57 -0700987 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500988out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800989 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500990 jbd2_stats_proc_exit(journal);
991 kfree(journal);
992 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700993}
994
995/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700996 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700997 * @inode: An inode to create the journal in
998 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700999 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001000 * the journal. The inode must exist already, must support bmap() and
1001 * must have all data blocks preallocated.
1002 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001003journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001004{
1005 struct buffer_head *bh;
1006 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001007 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001008 int err;
1009 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001010 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001011
1012 if (!journal)
1013 return NULL;
1014
1015 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1016 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001017 bdevname(journal->j_dev, journal->j_devname);
1018 p = journal->j_devname;
1019 while ((p = strchr(p, '/')))
1020 *p = '!';
1021 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001022 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001023 jbd_debug(1,
1024 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1025 journal, inode->i_sb->s_id, inode->i_ino,
1026 (long long) inode->i_size,
1027 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1028
1029 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1030 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001031 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001032
1033 /* journal descriptor can store up to n blocks -bzzz */
1034 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1035 journal->j_wbufsize = n;
1036 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1037 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001038 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001039 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001040 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001041 }
1042
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001043 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001044 /* If that failed, give up */
1045 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001046 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001047 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001048 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049 }
1050
1051 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001052 if (!bh) {
1053 printk(KERN_ERR
1054 "%s: Cannot get buffer for journal superblock\n",
1055 __func__);
1056 goto out_err;
1057 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001058 journal->j_sb_buffer = bh;
1059 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1060
1061 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001062out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001063 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001064 jbd2_stats_proc_exit(journal);
1065 kfree(journal);
1066 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001067}
1068
1069/*
1070 * If the journal init or create aborts, we need to mark the journal
1071 * superblock as being NULL to prevent the journal destroy from writing
1072 * back a bogus superblock.
1073 */
1074static void journal_fail_superblock (journal_t *journal)
1075{
1076 struct buffer_head *bh = journal->j_sb_buffer;
1077 brelse(bh);
1078 journal->j_sb_buffer = NULL;
1079}
1080
1081/*
1082 * Given a journal_t structure, initialise the various fields for
1083 * startup of a new journaling session. We use this both when creating
1084 * a journal, and after recovering an old journal to reset it for
1085 * subsequent use.
1086 */
1087
1088static int journal_reset(journal_t *journal)
1089{
1090 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001091 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001092
1093 first = be32_to_cpu(sb->s_first);
1094 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001095 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1096 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1097 first, last);
1098 journal_fail_superblock(journal);
1099 return -EINVAL;
1100 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101
1102 journal->j_first = first;
1103 journal->j_last = last;
1104
1105 journal->j_head = first;
1106 journal->j_tail = first;
1107 journal->j_free = last - first;
1108
1109 journal->j_tail_sequence = journal->j_transaction_sequence;
1110 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1111 journal->j_commit_request = journal->j_commit_sequence;
1112
1113 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1114
1115 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001116 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001117 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001118}
1119
1120/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001121 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001122 * @journal: The journal to update.
1123 * @wait: Set to '0' if you don't want to wait for IO completion.
1124 *
1125 * Update a journal's dynamic superblock fields and write it to disk,
1126 * optionally waiting for the IO to complete.
1127 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001128void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001129{
1130 journal_superblock_t *sb = journal->j_superblock;
1131 struct buffer_head *bh = journal->j_sb_buffer;
1132
1133 /*
1134 * As a special case, if the on-disk copy is already marked as needing
1135 * no recovery (s_start == 0) and there are no outstanding transactions
1136 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001137 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001138 * attempting a write to a potential-readonly device.
1139 */
1140 if (sb->s_start == 0 && journal->j_tail_sequence ==
1141 journal->j_transaction_sequence) {
1142 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1143 "(start %ld, seq %d, errno %d)\n",
1144 journal->j_tail, journal->j_tail_sequence,
1145 journal->j_errno);
1146 goto out;
1147 }
1148
Theodore Ts'o914258b2008-10-06 21:35:40 -04001149 if (buffer_write_io_error(bh)) {
1150 /*
1151 * Oh, dear. A previous attempt to write the journal
1152 * superblock failed. This could happen because the
1153 * USB device was yanked out. Or it could happen to
1154 * be a transient write error and maybe the block will
1155 * be remapped. Nothing we can do but to retry the
1156 * write and hope for the best.
1157 */
1158 printk(KERN_ERR "JBD2: previous I/O error detected "
1159 "for journal superblock update for %s.\n",
1160 journal->j_devname);
1161 clear_buffer_write_io_error(bh);
1162 set_buffer_uptodate(bh);
1163 }
1164
Theodore Ts'oa931da62010-08-03 21:35:12 -04001165 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001166 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1167 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1168
1169 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1170 sb->s_start = cpu_to_be32(journal->j_tail);
1171 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001172 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001173
1174 BUFFER_TRACE(bh, "marking dirty");
1175 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001176 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001177 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001178 if (buffer_write_io_error(bh)) {
1179 printk(KERN_ERR "JBD2: I/O error detected "
1180 "when updating journal superblock for %s.\n",
1181 journal->j_devname);
1182 clear_buffer_write_io_error(bh);
1183 set_buffer_uptodate(bh);
1184 }
1185 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001186 write_dirty_buffer(bh, WRITE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001187
1188out:
1189 /* If we have just flushed the log (by marking s_start==0), then
1190 * any future commit will have to be careful to update the
1191 * superblock again to re-record the true start of the log. */
1192
Theodore Ts'oa931da62010-08-03 21:35:12 -04001193 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001194 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001195 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001196 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001197 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001198 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001199}
1200
1201/*
1202 * Read the superblock for a given journal, performing initial
1203 * validation of the format.
1204 */
1205
1206static int journal_get_superblock(journal_t *journal)
1207{
1208 struct buffer_head *bh;
1209 journal_superblock_t *sb;
1210 int err = -EIO;
1211
1212 bh = journal->j_sb_buffer;
1213
1214 J_ASSERT(bh != NULL);
1215 if (!buffer_uptodate(bh)) {
1216 ll_rw_block(READ, 1, &bh);
1217 wait_on_buffer(bh);
1218 if (!buffer_uptodate(bh)) {
1219 printk (KERN_ERR
1220 "JBD: IO error reading journal superblock\n");
1221 goto out;
1222 }
1223 }
1224
1225 sb = journal->j_superblock;
1226
1227 err = -EINVAL;
1228
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001229 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001230 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1231 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1232 goto out;
1233 }
1234
1235 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001236 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001237 journal->j_format_version = 1;
1238 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001239 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001240 journal->j_format_version = 2;
1241 break;
1242 default:
1243 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1244 goto out;
1245 }
1246
1247 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1248 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1249 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1250 printk (KERN_WARNING "JBD: journal file too short\n");
1251 goto out;
1252 }
1253
1254 return 0;
1255
1256out:
1257 journal_fail_superblock(journal);
1258 return err;
1259}
1260
1261/*
1262 * Load the on-disk journal superblock and read the key fields into the
1263 * journal_t.
1264 */
1265
1266static int load_superblock(journal_t *journal)
1267{
1268 int err;
1269 journal_superblock_t *sb;
1270
1271 err = journal_get_superblock(journal);
1272 if (err)
1273 return err;
1274
1275 sb = journal->j_superblock;
1276
1277 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1278 journal->j_tail = be32_to_cpu(sb->s_start);
1279 journal->j_first = be32_to_cpu(sb->s_first);
1280 journal->j_last = be32_to_cpu(sb->s_maxlen);
1281 journal->j_errno = be32_to_cpu(sb->s_errno);
1282
1283 return 0;
1284}
1285
1286
1287/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001288 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289 * @journal: Journal to act on.
1290 *
1291 * Given a journal_t structure which tells us which disk blocks contain
1292 * a journal, read the journal from disk to initialise the in-memory
1293 * structures.
1294 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001295int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001296{
1297 int err;
1298 journal_superblock_t *sb;
1299
1300 err = load_superblock(journal);
1301 if (err)
1302 return err;
1303
1304 sb = journal->j_superblock;
1305 /* If this is a V2 superblock, then we have to check the
1306 * features flags on it. */
1307
1308 if (journal->j_format_version >= 2) {
1309 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001310 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001311 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001312 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001313 printk (KERN_WARNING
1314 "JBD: Unrecognised features on journal\n");
1315 return -EINVAL;
1316 }
1317 }
1318
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001319 /*
1320 * Create a slab for this blocksize
1321 */
1322 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1323 if (err)
1324 return err;
1325
Dave Kleikamp470decc2006-10-11 01:20:57 -07001326 /* Let the recovery code check whether it needs to recover any
1327 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001328 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001329 goto recovery_error;
1330
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001331 if (journal->j_failed_commit) {
1332 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1333 "is corrupt.\n", journal->j_failed_commit,
1334 journal->j_devname);
1335 return -EIO;
1336 }
1337
Dave Kleikamp470decc2006-10-11 01:20:57 -07001338 /* OK, we've finished with the dynamic journal bits:
1339 * reinitialise the dynamic contents of the superblock in memory
1340 * and reset them on disk. */
1341 if (journal_reset(journal))
1342 goto recovery_error;
1343
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001344 journal->j_flags &= ~JBD2_ABORT;
1345 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001346 return 0;
1347
1348recovery_error:
1349 printk (KERN_WARNING "JBD: recovery failed\n");
1350 return -EIO;
1351}
1352
1353/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001354 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001355 * @journal: Journal to act on.
1356 *
1357 * Release a journal_t structure once it is no longer in use by the
1358 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001359 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001360 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001361int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001362{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001363 int err = 0;
1364
Dave Kleikamp470decc2006-10-11 01:20:57 -07001365 /* Wait for the commit thread to wake up and die. */
1366 journal_kill_thread(journal);
1367
1368 /* Force a final log commit */
1369 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001370 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001371
1372 /* Force any old transactions to disk */
1373
1374 /* Totally anal locking here... */
1375 spin_lock(&journal->j_list_lock);
1376 while (journal->j_checkpoint_transactions != NULL) {
1377 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001378 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001379 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001380 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001381 spin_lock(&journal->j_list_lock);
1382 }
1383
1384 J_ASSERT(journal->j_running_transaction == NULL);
1385 J_ASSERT(journal->j_committing_transaction == NULL);
1386 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1387 spin_unlock(&journal->j_list_lock);
1388
Dave Kleikamp470decc2006-10-11 01:20:57 -07001389 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001390 if (!is_journal_aborted(journal)) {
1391 /* We can now mark the journal as empty. */
1392 journal->j_tail = 0;
1393 journal->j_tail_sequence =
1394 ++journal->j_transaction_sequence;
1395 jbd2_journal_update_superblock(journal, 1);
1396 } else {
1397 err = -EIO;
1398 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001399 brelse(journal->j_sb_buffer);
1400 }
1401
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001402 if (journal->j_proc_entry)
1403 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001404 if (journal->j_inode)
1405 iput(journal->j_inode);
1406 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001407 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 kfree(journal->j_wbuf);
1409 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001410
1411 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001412}
1413
1414
1415/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001416 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001417 * @journal: Journal to check.
1418 * @compat: bitmask of compatible features
1419 * @ro: bitmask of features that force read-only mount
1420 * @incompat: bitmask of incompatible features
1421 *
1422 * Check whether the journal uses all of a given set of
1423 * features. Return true (non-zero) if it does.
1424 **/
1425
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001426int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 unsigned long ro, unsigned long incompat)
1428{
1429 journal_superblock_t *sb;
1430
1431 if (!compat && !ro && !incompat)
1432 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001433 /* Load journal superblock if it is not loaded yet. */
1434 if (journal->j_format_version == 0 &&
1435 journal_get_superblock(journal) != 0)
1436 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001437 if (journal->j_format_version == 1)
1438 return 0;
1439
1440 sb = journal->j_superblock;
1441
1442 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1443 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1444 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1445 return 1;
1446
1447 return 0;
1448}
1449
1450/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001451 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001452 * @journal: Journal to check.
1453 * @compat: bitmask of compatible features
1454 * @ro: bitmask of features that force read-only mount
1455 * @incompat: bitmask of incompatible features
1456 *
1457 * Check whether the journaling code supports the use of
1458 * all of a given set of features on this journal. Return true
1459 * (non-zero) if it can. */
1460
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001461int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 unsigned long ro, unsigned long incompat)
1463{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001464 if (!compat && !ro && !incompat)
1465 return 1;
1466
Dave Kleikamp470decc2006-10-11 01:20:57 -07001467 /* We can support any known requested features iff the
1468 * superblock is in version 2. Otherwise we fail to support any
1469 * extended sb features. */
1470
1471 if (journal->j_format_version != 2)
1472 return 0;
1473
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001474 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1475 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1476 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 return 1;
1478
1479 return 0;
1480}
1481
1482/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001483 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001484 * @journal: Journal to act on.
1485 * @compat: bitmask of compatible features
1486 * @ro: bitmask of features that force read-only mount
1487 * @incompat: bitmask of incompatible features
1488 *
1489 * Mark a given journal feature as present on the
1490 * superblock. Returns true if the requested features could be set.
1491 *
1492 */
1493
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001494int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001495 unsigned long ro, unsigned long incompat)
1496{
1497 journal_superblock_t *sb;
1498
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001499 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500 return 1;
1501
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 return 0;
1504
1505 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1506 compat, ro, incompat);
1507
1508 sb = journal->j_superblock;
1509
1510 sb->s_feature_compat |= cpu_to_be32(compat);
1511 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1512 sb->s_feature_incompat |= cpu_to_be32(incompat);
1513
1514 return 1;
1515}
1516
Girish Shilamkar818d2762008-01-28 23:58:27 -05001517/*
1518 * jbd2_journal_clear_features () - Clear a given journal feature in the
1519 * superblock
1520 * @journal: Journal to act on.
1521 * @compat: bitmask of compatible features
1522 * @ro: bitmask of features that force read-only mount
1523 * @incompat: bitmask of incompatible features
1524 *
1525 * Clear a given journal feature as present on the
1526 * superblock.
1527 */
1528void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1529 unsigned long ro, unsigned long incompat)
1530{
1531 journal_superblock_t *sb;
1532
1533 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1534 compat, ro, incompat);
1535
1536 sb = journal->j_superblock;
1537
1538 sb->s_feature_compat &= ~cpu_to_be32(compat);
1539 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1540 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1541}
1542EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001543
1544/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001545 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001546 * @journal: Journal to act on.
1547 *
1548 * Given an initialised but unloaded journal struct, poke about in the
1549 * on-disk structure to update it to the most recent supported version.
1550 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001551int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001552{
1553 journal_superblock_t *sb;
1554 int err;
1555
1556 err = journal_get_superblock(journal);
1557 if (err)
1558 return err;
1559
1560 sb = journal->j_superblock;
1561
1562 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001563 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001565 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001566 return journal_convert_superblock_v1(journal, sb);
1567 default:
1568 break;
1569 }
1570 return -EINVAL;
1571}
1572
1573static int journal_convert_superblock_v1(journal_t *journal,
1574 journal_superblock_t *sb)
1575{
1576 int offset, blocksize;
1577 struct buffer_head *bh;
1578
1579 printk(KERN_WARNING
1580 "JBD: Converting superblock from version 1 to 2.\n");
1581
1582 /* Pre-initialise new fields to zero */
1583 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1584 blocksize = be32_to_cpu(sb->s_blocksize);
1585 memset(&sb->s_feature_compat, 0, blocksize-offset);
1586
1587 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001588 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001589 journal->j_format_version = 2;
1590
1591 bh = journal->j_sb_buffer;
1592 BUFFER_TRACE(bh, "marking dirty");
1593 mark_buffer_dirty(bh);
1594 sync_dirty_buffer(bh);
1595 return 0;
1596}
1597
1598
1599/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 * @journal: Journal to act on.
1602 *
1603 * Flush all data for a given journal to disk and empty the journal.
1604 * Filesystems can use this when remounting readonly to ensure that
1605 * recovery does not need to happen on remount.
1606 */
1607
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001608int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609{
1610 int err = 0;
1611 transaction_t *transaction = NULL;
1612 unsigned long old_tail;
1613
Theodore Ts'oa931da62010-08-03 21:35:12 -04001614 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001615
1616 /* Force everything buffered to the log... */
1617 if (journal->j_running_transaction) {
1618 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001619 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620 } else if (journal->j_committing_transaction)
1621 transaction = journal->j_committing_transaction;
1622
1623 /* Wait for the log commit to complete... */
1624 if (transaction) {
1625 tid_t tid = transaction->t_tid;
1626
Theodore Ts'oa931da62010-08-03 21:35:12 -04001627 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001628 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001629 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001630 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001631 }
1632
1633 /* ...and flush everything in the log out to disk. */
1634 spin_lock(&journal->j_list_lock);
1635 while (!err && journal->j_checkpoint_transactions != NULL) {
1636 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001637 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001639 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001640 spin_lock(&journal->j_list_lock);
1641 }
1642 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001643
1644 if (is_journal_aborted(journal))
1645 return -EIO;
1646
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001647 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001648
1649 /* Finally, mark the journal as really needing no recovery.
1650 * This sets s_start==0 in the underlying superblock, which is
1651 * the magic code for a fully-recovered superblock. Any future
1652 * commits of data to the journal will restore the current
1653 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001654 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001655 old_tail = journal->j_tail;
1656 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001657 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001658 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001659 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001660 journal->j_tail = old_tail;
1661
1662 J_ASSERT(!journal->j_running_transaction);
1663 J_ASSERT(!journal->j_committing_transaction);
1664 J_ASSERT(!journal->j_checkpoint_transactions);
1665 J_ASSERT(journal->j_head == journal->j_tail);
1666 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001667 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001668 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001669}
1670
1671/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001672 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001673 * @journal: Journal to act on.
1674 * @write: flag (see below)
1675 *
1676 * Wipe out all of the contents of a journal, safely. This will produce
1677 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001678 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001679 *
1680 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1681 * we merely suppress recovery.
1682 */
1683
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001684int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001685{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001686 int err = 0;
1687
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001688 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001689
1690 err = load_superblock(journal);
1691 if (err)
1692 return err;
1693
Dave Kleikamp470decc2006-10-11 01:20:57 -07001694 if (!journal->j_tail)
1695 goto no_recovery;
1696
1697 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1698 write ? "Clearing" : "Ignoring");
1699
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001700 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001702 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001703
1704 no_recovery:
1705 return err;
1706}
1707
1708/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 * Journal abort has very specific semantics, which we describe
1710 * for journal abort.
1711 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001712 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713 * itself are here.
1714 */
1715
1716/*
1717 * Quick version for internal journal use (doesn't lock the journal).
1718 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1719 * and don't attempt to make any other journal updates.
1720 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722{
1723 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001724
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001725 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001726 return;
1727
1728 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001729 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001730
Theodore Ts'oa931da62010-08-03 21:35:12 -04001731 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001732 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001733 transaction = journal->j_running_transaction;
1734 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001735 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001736 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001737}
1738
1739/* Soft abort: record the abort error status in the journal superblock,
1740 * but don't do any other IO. */
1741static void __journal_abort_soft (journal_t *journal, int errno)
1742{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001743 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001744 return;
1745
1746 if (!journal->j_errno)
1747 journal->j_errno = errno;
1748
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001749 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750
1751 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001752 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001753}
1754
1755/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001756 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001757 * @journal: the journal to shutdown.
1758 * @errno: an error number to record in the journal indicating
1759 * the reason for the shutdown.
1760 *
1761 * Perform a complete, immediate shutdown of the ENTIRE
1762 * journal (not of a single transaction). This operation cannot be
1763 * undone without closing and reopening the journal.
1764 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001765 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1767 * mode.
1768 *
1769 * Journal abort has very specific semantics. Any existing dirty,
1770 * unjournaled buffers in the main filesystem will still be written to
1771 * disk by bdflush, but the journaling mechanism will be suspended
1772 * immediately and no further transaction commits will be honoured.
1773 *
1774 * Any dirty, journaled buffers will be written back to disk without
1775 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1776 * filesystem, but we _do_ attempt to leave as much data as possible
1777 * behind for fsck to use for cleanup.
1778 *
1779 * Any attempt to get a new transaction handle on a journal which is in
1780 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001781 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001782 * entered abort state during the update.
1783 *
1784 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001785 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001786 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001787 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001788 * which will be recorded (if possible) in the journal superblock. This
1789 * allows a client to record failure conditions in the middle of a
1790 * transaction without having to complete the transaction to record the
1791 * failure to disk. ext3_error, for example, now uses this
1792 * functionality.
1793 *
1794 * Errors which originate from within the journaling layer will NOT
1795 * supply an errno; a null errno implies that absolutely no further
1796 * writes are done to the journal (unless there are any already in
1797 * progress).
1798 *
1799 */
1800
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001801void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802{
1803 __journal_abort_soft(journal, errno);
1804}
1805
1806/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001807 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808 * @journal: journal to examine.
1809 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001810 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 * time the journal was mounted - if the journal was stopped
1812 * without calling abort this will be 0.
1813 *
1814 * If the journal has been aborted on this mount time -EROFS will
1815 * be returned.
1816 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001817int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001818{
1819 int err;
1820
Theodore Ts'oa931da62010-08-03 21:35:12 -04001821 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001822 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001823 err = -EROFS;
1824 else
1825 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001826 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001827 return err;
1828}
1829
1830/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001831 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001832 * @journal: journal to act on.
1833 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001834 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001835 * mode.
1836 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001837int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001838{
1839 int err = 0;
1840
Theodore Ts'oa931da62010-08-03 21:35:12 -04001841 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001842 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001843 err = -EROFS;
1844 else
1845 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001846 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001847 return err;
1848}
1849
1850/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001851 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001852 * @journal: journal to act on.
1853 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001854 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855 * mode.
1856 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001857void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001859 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001860 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001861 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001862 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001863}
1864
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001865int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866{
1867 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1868}
1869
1870/*
Zach Brownb517bea2006-10-11 01:21:08 -07001871 * helper functions to deal with 32 or 64bit block numbers.
1872 */
1873size_t journal_tag_bytes(journal_t *journal)
1874{
1875 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001876 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001877 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001878 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001879}
1880
1881/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001882 * JBD memory management
1883 *
1884 * These functions are used to allocate block-sized chunks of memory
1885 * used for making copies of buffer_head data. Very often it will be
1886 * page-sized chunks of data, but sometimes it will be in
1887 * sub-page-size chunks. (For example, 16k pages on Power systems
1888 * with a 4k block file system.) For blocks smaller than a page, we
1889 * use a SLAB allocator. There are slab caches for each block size,
1890 * which are allocated at mount time, if necessary, and we only free
1891 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1892 * this reason we don't need to a mutex to protect access to
1893 * jbd2_slab[] allocating or releasing memory; only in
1894 * jbd2_journal_create_slab().
1895 */
1896#define JBD2_MAX_SLABS 8
1897static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001898
1899static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1900 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1901 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1902};
1903
1904
1905static void jbd2_journal_destroy_slabs(void)
1906{
1907 int i;
1908
1909 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1910 if (jbd2_slab[i])
1911 kmem_cache_destroy(jbd2_slab[i]);
1912 jbd2_slab[i] = NULL;
1913 }
1914}
1915
1916static int jbd2_journal_create_slab(size_t size)
1917{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001918 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001919 int i = order_base_2(size) - 10;
1920 size_t slab_size;
1921
1922 if (size == PAGE_SIZE)
1923 return 0;
1924
1925 if (i >= JBD2_MAX_SLABS)
1926 return -EINVAL;
1927
1928 if (unlikely(i < 0))
1929 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001930 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001931 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001932 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001933 return 0; /* Already created */
1934 }
1935
1936 slab_size = 1 << (i+10);
1937 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1938 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001939 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001940 if (!jbd2_slab[i]) {
1941 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1942 return -ENOMEM;
1943 }
1944 return 0;
1945}
1946
1947static struct kmem_cache *get_slab(size_t size)
1948{
1949 int i = order_base_2(size) - 10;
1950
1951 BUG_ON(i >= JBD2_MAX_SLABS);
1952 if (unlikely(i < 0))
1953 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001954 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001955 return jbd2_slab[i];
1956}
1957
1958void *jbd2_alloc(size_t size, gfp_t flags)
1959{
1960 void *ptr;
1961
1962 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1963
1964 flags |= __GFP_REPEAT;
1965 if (size == PAGE_SIZE)
1966 ptr = (void *)__get_free_pages(flags, 0);
1967 else if (size > PAGE_SIZE) {
1968 int order = get_order(size);
1969
1970 if (order < 3)
1971 ptr = (void *)__get_free_pages(flags, order);
1972 else
1973 ptr = vmalloc(size);
1974 } else
1975 ptr = kmem_cache_alloc(get_slab(size), flags);
1976
1977 /* Check alignment; SLUB has gotten this wrong in the past,
1978 * and this can lead to user data corruption! */
1979 BUG_ON(((unsigned long) ptr) & (size-1));
1980
1981 return ptr;
1982}
1983
1984void jbd2_free(void *ptr, size_t size)
1985{
1986 if (size == PAGE_SIZE) {
1987 free_pages((unsigned long)ptr, 0);
1988 return;
1989 }
1990 if (size > PAGE_SIZE) {
1991 int order = get_order(size);
1992
1993 if (order < 3)
1994 free_pages((unsigned long)ptr, order);
1995 else
1996 vfree(ptr);
1997 return;
1998 }
1999 kmem_cache_free(get_slab(size), ptr);
2000};
2001
2002/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002003 * Journal_head storage management
2004 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002005static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002006#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2008#endif
2009
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002010static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011{
2012 int retval;
2013
Al Viro1076d172008-03-29 03:07:18 +00002014 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002015 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 sizeof(struct journal_head),
2017 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002018 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002019 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002020 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002021 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022 retval = -ENOMEM;
2023 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
2024 }
2025 return retval;
2026}
2027
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002028static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002029{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002030 if (jbd2_journal_head_cache) {
2031 kmem_cache_destroy(jbd2_journal_head_cache);
2032 jbd2_journal_head_cache = NULL;
2033 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002034}
2035
2036/*
2037 * journal_head splicing and dicing
2038 */
2039static struct journal_head *journal_alloc_journal_head(void)
2040{
2041 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042
Jose R. Santose23291b2007-07-18 08:57:06 -04002043#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 atomic_inc(&nr_journal_heads);
2045#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002046 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002047 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002048 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002049 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002050 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002051 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002052 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002053 }
2054 }
2055 return ret;
2056}
2057
2058static void journal_free_journal_head(struct journal_head *jh)
2059{
Jose R. Santose23291b2007-07-18 08:57:06 -04002060#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002061 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002062 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002063#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002064 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002065}
2066
2067/*
2068 * A journal_head is attached to a buffer_head whenever JBD has an
2069 * interest in the buffer.
2070 *
2071 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2072 * is set. This bit is tested in core kernel code where we need to take
2073 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2074 * there.
2075 *
2076 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2077 *
2078 * When a buffer has its BH_JBD bit set it is immune from being released by
2079 * core kernel code, mainly via ->b_count.
2080 *
2081 * A journal_head may be detached from its buffer_head when the journal_head's
2082 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002083 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002084 * journal_head can be dropped if needed.
2085 *
2086 * Various places in the kernel want to attach a journal_head to a buffer_head
2087 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002088 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002089 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002090 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002091 *
2092 * So the typical usage would be:
2093 *
2094 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002095 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002096 * ...
2097 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002098 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002099 *
2100 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2101 * because it has a non-zero b_transaction.
2102 */
2103
2104/*
2105 * Give a buffer_head a journal_head.
2106 *
2107 * Doesn't need the journal lock.
2108 * May sleep.
2109 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002110struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002111{
2112 struct journal_head *jh;
2113 struct journal_head *new_jh = NULL;
2114
2115repeat:
2116 if (!buffer_jbd(bh)) {
2117 new_jh = journal_alloc_journal_head();
2118 memset(new_jh, 0, sizeof(*new_jh));
2119 }
2120
2121 jbd_lock_bh_journal_head(bh);
2122 if (buffer_jbd(bh)) {
2123 jh = bh2jh(bh);
2124 } else {
2125 J_ASSERT_BH(bh,
2126 (atomic_read(&bh->b_count) > 0) ||
2127 (bh->b_page && bh->b_page->mapping));
2128
2129 if (!new_jh) {
2130 jbd_unlock_bh_journal_head(bh);
2131 goto repeat;
2132 }
2133
2134 jh = new_jh;
2135 new_jh = NULL; /* We consumed it */
2136 set_buffer_jbd(bh);
2137 bh->b_private = jh;
2138 jh->b_bh = bh;
2139 get_bh(bh);
2140 BUFFER_TRACE(bh, "added journal_head");
2141 }
2142 jh->b_jcount++;
2143 jbd_unlock_bh_journal_head(bh);
2144 if (new_jh)
2145 journal_free_journal_head(new_jh);
2146 return bh->b_private;
2147}
2148
2149/*
2150 * Grab a ref against this buffer_head's journal_head. If it ended up not
2151 * having a journal_head, return NULL
2152 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002153struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002154{
2155 struct journal_head *jh = NULL;
2156
2157 jbd_lock_bh_journal_head(bh);
2158 if (buffer_jbd(bh)) {
2159 jh = bh2jh(bh);
2160 jh->b_jcount++;
2161 }
2162 jbd_unlock_bh_journal_head(bh);
2163 return jh;
2164}
2165
2166static void __journal_remove_journal_head(struct buffer_head *bh)
2167{
2168 struct journal_head *jh = bh2jh(bh);
2169
2170 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2171
2172 get_bh(bh);
2173 if (jh->b_jcount == 0) {
2174 if (jh->b_transaction == NULL &&
2175 jh->b_next_transaction == NULL &&
2176 jh->b_cp_transaction == NULL) {
2177 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2178 J_ASSERT_BH(bh, buffer_jbd(bh));
2179 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2180 BUFFER_TRACE(bh, "remove journal_head");
2181 if (jh->b_frozen_data) {
2182 printk(KERN_WARNING "%s: freeing "
2183 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002184 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002185 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002186 }
2187 if (jh->b_committed_data) {
2188 printk(KERN_WARNING "%s: freeing "
2189 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002190 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002191 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002192 }
2193 bh->b_private = NULL;
2194 jh->b_bh = NULL; /* debug, really */
2195 clear_buffer_jbd(bh);
2196 __brelse(bh);
2197 journal_free_journal_head(jh);
2198 } else {
2199 BUFFER_TRACE(bh, "journal_head was locked");
2200 }
2201 }
2202}
2203
2204/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002205 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002206 * and has a zero b_jcount then remove and release its journal_head. If we did
2207 * see that the buffer is not used by any transaction we also "logically"
2208 * decrement ->b_count.
2209 *
2210 * We in fact take an additional increment on ->b_count as a convenience,
2211 * because the caller usually wants to do additional things with the bh
2212 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002213 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002214 * time. Once the caller has run __brelse(), the buffer is eligible for
2215 * reaping by try_to_free_buffers().
2216 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002217void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002218{
2219 jbd_lock_bh_journal_head(bh);
2220 __journal_remove_journal_head(bh);
2221 jbd_unlock_bh_journal_head(bh);
2222}
2223
2224/*
2225 * Drop a reference on the passed journal_head. If it fell to zero then try to
2226 * release the journal_head from the buffer_head.
2227 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002228void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002229{
2230 struct buffer_head *bh = jh2bh(jh);
2231
2232 jbd_lock_bh_journal_head(bh);
2233 J_ASSERT_JH(jh, jh->b_jcount > 0);
2234 --jh->b_jcount;
2235 if (!jh->b_jcount && !jh->b_transaction) {
2236 __journal_remove_journal_head(bh);
2237 __brelse(bh);
2238 }
2239 jbd_unlock_bh_journal_head(bh);
2240}
2241
2242/*
Jan Karac851ed52008-07-11 19:27:31 -04002243 * Initialize jbd inode head
2244 */
2245void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2246{
2247 jinode->i_transaction = NULL;
2248 jinode->i_next_transaction = NULL;
2249 jinode->i_vfs_inode = inode;
2250 jinode->i_flags = 0;
2251 INIT_LIST_HEAD(&jinode->i_list);
2252}
2253
2254/*
2255 * Function to be called before we start removing inode from memory (i.e.,
2256 * clear_inode() is a fine place to be called from). It removes inode from
2257 * transaction's lists.
2258 */
2259void jbd2_journal_release_jbd_inode(journal_t *journal,
2260 struct jbd2_inode *jinode)
2261{
Jan Karac851ed52008-07-11 19:27:31 -04002262 if (!journal)
2263 return;
2264restart:
2265 spin_lock(&journal->j_list_lock);
2266 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002267 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002268 wait_queue_head_t *wq;
2269 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2270 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2271 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2272 spin_unlock(&journal->j_list_lock);
2273 schedule();
2274 finish_wait(wq, &wait.wait);
2275 goto restart;
2276 }
2277
Jan Karac851ed52008-07-11 19:27:31 -04002278 if (jinode->i_transaction) {
2279 list_del(&jinode->i_list);
2280 jinode->i_transaction = NULL;
2281 }
2282 spin_unlock(&journal->j_list_lock);
2283}
2284
2285/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002286 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002287 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002288#ifdef CONFIG_JBD2_DEBUG
2289u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002290EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002291
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002292#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002293
Jose R. Santos6f38c742007-10-16 18:38:25 -04002294static struct dentry *jbd2_debugfs_dir;
2295static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002296
2297static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002298{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002299 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2300 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002301 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2302 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002303 jbd2_debugfs_dir,
2304 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002305}
2306
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002307static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002308{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002309 debugfs_remove(jbd2_debug);
2310 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002311}
2312
2313#else
2314
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002315static void __init jbd2_create_debugfs_entry(void)
2316{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002317}
2318
2319static void __exit jbd2_remove_debugfs_entry(void)
2320{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002321}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002322
2323#endif
2324
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002325#ifdef CONFIG_PROC_FS
2326
2327#define JBD2_STATS_PROC_NAME "fs/jbd2"
2328
2329static void __init jbd2_create_jbd_stats_proc_entry(void)
2330{
2331 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2332}
2333
2334static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2335{
2336 if (proc_jbd2_stats)
2337 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2338}
2339
2340#else
2341
2342#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2343#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2344
2345#endif
2346
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002347struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002348
2349static int __init journal_init_handle_cache(void)
2350{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002351 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002352 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002353 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2354 return -ENOMEM;
2355 }
2356 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2357 if (jbd2_inode_cache == NULL) {
2358 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2359 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360 return -ENOMEM;
2361 }
2362 return 0;
2363}
2364
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002365static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002366{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002367 if (jbd2_handle_cache)
2368 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002369 if (jbd2_inode_cache)
2370 kmem_cache_destroy(jbd2_inode_cache);
2371
Dave Kleikamp470decc2006-10-11 01:20:57 -07002372}
2373
2374/*
2375 * Module startup and shutdown
2376 */
2377
2378static int __init journal_init_caches(void)
2379{
2380 int ret;
2381
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002382 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002383 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002384 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002385 if (ret == 0)
2386 ret = journal_init_handle_cache();
2387 return ret;
2388}
2389
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002390static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002391{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002392 jbd2_journal_destroy_revoke_caches();
2393 jbd2_journal_destroy_jbd2_journal_head_cache();
2394 jbd2_journal_destroy_handle_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002395 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002396}
2397
2398static int __init journal_init(void)
2399{
2400 int ret;
2401
2402 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2403
2404 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002405 if (ret == 0) {
2406 jbd2_create_debugfs_entry();
2407 jbd2_create_jbd_stats_proc_entry();
2408 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002409 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002410 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002411 return ret;
2412}
2413
2414static void __exit journal_exit(void)
2415{
Jose R. Santose23291b2007-07-18 08:57:06 -04002416#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002417 int n = atomic_read(&nr_journal_heads);
2418 if (n)
2419 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2420#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002421 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002422 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002423 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002424}
2425
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002426/*
2427 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2428 * tracing infrastructure to map a dev_t to a device name.
2429 *
2430 * The caller should use rcu_read_lock() in order to make sure the
2431 * device name stays valid until its done with it. We use
2432 * rcu_read_lock() as well to make sure we're safe in case the caller
2433 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2434 * nested.
2435 */
2436struct devname_cache {
2437 struct rcu_head rcu;
2438 dev_t device;
2439 char devname[BDEVNAME_SIZE];
2440};
2441#define CACHE_SIZE_BITS 6
2442static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2443static DEFINE_SPINLOCK(devname_cache_lock);
2444
2445static void free_devcache(struct rcu_head *rcu)
2446{
2447 kfree(rcu);
2448}
2449
2450const char *jbd2_dev_to_name(dev_t device)
2451{
2452 int i = hash_32(device, CACHE_SIZE_BITS);
2453 char *ret;
2454 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002455 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002456
2457 rcu_read_lock();
2458 if (devcache[i] && devcache[i]->device == device) {
2459 ret = devcache[i]->devname;
2460 rcu_read_unlock();
2461 return ret;
2462 }
2463 rcu_read_unlock();
2464
Theodore Ts'ob5744802009-06-20 23:34:44 -04002465 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2466 if (!new_dev)
2467 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Zhu Yanhai50f689a2011-04-04 12:58:12 -04002468 bd = bdget(device);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002469 spin_lock(&devname_cache_lock);
2470 if (devcache[i]) {
2471 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002472 kfree(new_dev);
Zhu Yanhai50f689a2011-04-04 12:58:12 -04002473 bdput(bd);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002474 ret = devcache[i]->devname;
2475 spin_unlock(&devname_cache_lock);
2476 return ret;
2477 }
2478 call_rcu(&devcache[i]->rcu, free_devcache);
2479 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002480 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002481 devcache[i]->device = device;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002482 if (bd) {
2483 bdevname(bd, devcache[i]->devname);
2484 bdput(bd);
2485 } else
2486 __bdevname(device, devcache[i]->devname);
2487 ret = devcache[i]->devname;
2488 spin_unlock(&devname_cache_lock);
2489 return ret;
2490}
2491EXPORT_SYMBOL(jbd2_dev_to_name);
2492
Dave Kleikamp470decc2006-10-11 01:20:57 -07002493MODULE_LICENSE("GPL");
2494module_init(journal_init);
2495module_exit(journal_exit);
2496