blob: 97e73469b2c48f0ed1104d5a12e1ddb54102983e [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 /*
482 * Are we already doing a recent enough commit?
483 */
484 if (!tid_geq(journal->j_commit_request, target)) {
485 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200486 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700487 * commit thread. We do _not_ do the commit ourselves.
488 */
489
490 journal->j_commit_request = target;
491 jbd_debug(1, "JBD: requesting commit %d/%d\n",
492 journal->j_commit_request,
493 journal->j_commit_sequence);
494 wake_up(&journal->j_wait_commit);
495 return 1;
496 }
497 return 0;
498}
499
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700500int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700501{
502 int ret;
503
Theodore Ts'oa931da62010-08-03 21:35:12 -0400504 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700505 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400506 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700507 return ret;
508}
509
510/*
511 * Force and wait upon a commit if the calling process is not within
512 * transaction. This is used for forcing out undo-protected data which contains
513 * bitmaps, when the fs is running out of space.
514 *
515 * We can only force the running transaction if we don't have an active handle;
516 * otherwise, we will deadlock.
517 *
518 * Returns true if a transaction was started.
519 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700520int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700521{
522 transaction_t *transaction = NULL;
523 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500524 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700525
Theodore Ts'oa931da62010-08-03 21:35:12 -0400526 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 if (journal->j_running_transaction && !current->journal_info) {
528 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500529 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
530 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531 } else if (journal->j_committing_transaction)
532 transaction = journal->j_committing_transaction;
533
534 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400535 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 return 0; /* Nothing to retry */
537 }
538
539 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400540 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500541 if (need_to_start)
542 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700543 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544 return 1;
545}
546
547/*
548 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500549 * if a transaction is going to be committed (or is currently already
550 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700552int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700553{
554 int ret = 0;
555
Theodore Ts'oa931da62010-08-03 21:35:12 -0400556 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557 if (journal->j_running_transaction) {
558 tid_t tid = journal->j_running_transaction->t_tid;
559
Jan Karac88ccea2009-02-10 11:27:46 -0500560 __jbd2_log_start_commit(journal, tid);
561 /* There's a running transaction and we've just made sure
562 * it's commit has been scheduled. */
563 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500565 ret = 1;
566 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700567 /*
568 * If ext3_write_super() recently started a commit, then we
569 * have to wait for completion of that transaction
570 */
Jan Karac88ccea2009-02-10 11:27:46 -0500571 if (ptid)
572 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700573 ret = 1;
574 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400575 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700576 return ret;
577}
578
579/*
580 * Wait for a specified commit to complete.
581 * The caller may not hold the journal lock.
582 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700583int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700584{
585 int err = 0;
586
Theodore Ts'oa931da62010-08-03 21:35:12 -0400587 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400588#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700589 if (!tid_geq(journal->j_commit_request, tid)) {
590 printk(KERN_EMERG
591 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400592 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700593 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700595 while (tid_gt(tid, journal->j_commit_sequence)) {
596 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
597 tid, journal->j_commit_sequence);
598 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400599 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700600 wait_event(journal->j_wait_done_commit,
601 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400602 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700603 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400604 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700605
606 if (unlikely(is_journal_aborted(journal))) {
607 printk(KERN_EMERG "journal commit I/O error\n");
608 err = -EIO;
609 }
610 return err;
611}
612
613/*
614 * Log buffer allocation routines:
615 */
616
Mingming Cao18eba7a2006-10-11 01:21:13 -0700617int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700618{
619 unsigned long blocknr;
620
Theodore Ts'oa931da62010-08-03 21:35:12 -0400621 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622 J_ASSERT(journal->j_free > 1);
623
624 blocknr = journal->j_head;
625 journal->j_head++;
626 journal->j_free--;
627 if (journal->j_head == journal->j_last)
628 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400629 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700630 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700631}
632
633/*
634 * Conversion of logical to physical block numbers for the journal
635 *
636 * On external journals the journal blocks are identity-mapped, so
637 * this is a no-op. If needed, we can use j_blk_offset - everything is
638 * ready.
639 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700640int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700641 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700642{
643 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700644 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645
646 if (journal->j_inode) {
647 ret = bmap(journal->j_inode, blocknr);
648 if (ret)
649 *retp = ret;
650 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700651 printk(KERN_ALERT "%s: journal block not found "
652 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400653 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654 err = -EIO;
655 __journal_abort_soft(journal, err);
656 }
657 } else {
658 *retp = blocknr; /* +journal->j_blk_offset */
659 }
660 return err;
661}
662
663/*
664 * We play buffer_head aliasing tricks to write data/metadata blocks to
665 * the journal without copying their contents, but for journal
666 * descriptor blocks we do need to generate bona fide buffers.
667 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700668 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700669 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
670 * But we don't bother doing that, so there will be coherency problems with
671 * mmaps of blockdevs which hold live JBD-controlled filesystems.
672 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700673struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700674{
675 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700676 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700677 int err;
678
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700679 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700680
681 if (err)
682 return NULL;
683
684 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500685 if (!bh)
686 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700687 lock_buffer(bh);
688 memset(bh->b_data, 0, journal->j_blocksize);
689 set_buffer_uptodate(bh);
690 unlock_buffer(bh);
691 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700692 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700693}
694
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500695struct jbd2_stats_proc_session {
696 journal_t *journal;
697 struct transaction_stats_s *stats;
698 int start;
699 int max;
700};
701
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500702static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
703{
704 return *pos ? NULL : SEQ_START_TOKEN;
705}
706
707static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
708{
709 return NULL;
710}
711
712static int jbd2_seq_info_show(struct seq_file *seq, void *v)
713{
714 struct jbd2_stats_proc_session *s = seq->private;
715
716 if (v != SEQ_START_TOKEN)
717 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400718 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500719 s->stats->ts_tid,
720 s->journal->j_max_transaction_buffers);
721 if (s->stats->ts_tid == 0)
722 return 0;
723 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400724 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500725 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400726 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500727 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400728 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500729 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400730 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500731 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400732 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500733 seq_printf(seq, " %lluus average transaction commit time\n",
734 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500735 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400736 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500737 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400738 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500739 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400740 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500741 return 0;
742}
743
744static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
745{
746}
747
James Morris88e9d342009-09-22 16:43:43 -0700748static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500749 .start = jbd2_seq_info_start,
750 .next = jbd2_seq_info_next,
751 .stop = jbd2_seq_info_stop,
752 .show = jbd2_seq_info_show,
753};
754
755static int jbd2_seq_info_open(struct inode *inode, struct file *file)
756{
757 journal_t *journal = PDE(inode)->data;
758 struct jbd2_stats_proc_session *s;
759 int rc, size;
760
761 s = kmalloc(sizeof(*s), GFP_KERNEL);
762 if (s == NULL)
763 return -ENOMEM;
764 size = sizeof(struct transaction_stats_s);
765 s->stats = kmalloc(size, GFP_KERNEL);
766 if (s->stats == NULL) {
767 kfree(s);
768 return -ENOMEM;
769 }
770 spin_lock(&journal->j_history_lock);
771 memcpy(s->stats, &journal->j_stats, size);
772 s->journal = journal;
773 spin_unlock(&journal->j_history_lock);
774
775 rc = seq_open(file, &jbd2_seq_info_ops);
776 if (rc == 0) {
777 struct seq_file *m = file->private_data;
778 m->private = s;
779 } else {
780 kfree(s->stats);
781 kfree(s);
782 }
783 return rc;
784
785}
786
787static int jbd2_seq_info_release(struct inode *inode, struct file *file)
788{
789 struct seq_file *seq = file->private_data;
790 struct jbd2_stats_proc_session *s = seq->private;
791 kfree(s->stats);
792 kfree(s);
793 return seq_release(inode, file);
794}
795
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700796static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500797 .owner = THIS_MODULE,
798 .open = jbd2_seq_info_open,
799 .read = seq_read,
800 .llseek = seq_lseek,
801 .release = jbd2_seq_info_release,
802};
803
804static struct proc_dir_entry *proc_jbd2_stats;
805
806static void jbd2_stats_proc_init(journal_t *journal)
807{
Theodore Ts'o05496762008-09-16 14:36:17 -0400808 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500809 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700810 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
811 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500812 }
813}
814
815static void jbd2_stats_proc_exit(journal_t *journal)
816{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500817 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400818 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500819}
820
Dave Kleikamp470decc2006-10-11 01:20:57 -0700821/*
822 * Management for journal control blocks: functions to create and
823 * destroy journal_t structures, and to initialise and read existing
824 * journal blocks from disk. */
825
826/* First: create and setup a journal_t object in memory. We initialise
827 * very few fields yet: that has to wait until we have created the
828 * journal structures from from scratch, or loaded them from disk. */
829
830static journal_t * journal_init_common (void)
831{
832 journal_t *journal;
833 int err;
834
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500835 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700836 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500837 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700838
839 init_waitqueue_head(&journal->j_wait_transaction_locked);
840 init_waitqueue_head(&journal->j_wait_logspace);
841 init_waitqueue_head(&journal->j_wait_done_commit);
842 init_waitqueue_head(&journal->j_wait_checkpoint);
843 init_waitqueue_head(&journal->j_wait_commit);
844 init_waitqueue_head(&journal->j_wait_updates);
845 mutex_init(&journal->j_barrier);
846 mutex_init(&journal->j_checkpoint_mutex);
847 spin_lock_init(&journal->j_revoke_lock);
848 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400849 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700850
Mingming Caocd02ff02007-10-16 18:38:25 -0400851 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500852 journal->j_min_batch_time = 0;
853 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700854
855 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700856 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700857
858 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700859 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700860 if (err) {
861 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500862 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700863 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500864
Theodore Ts'obf699322009-09-30 00:32:06 -0400865 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500866
Dave Kleikamp470decc2006-10-11 01:20:57 -0700867 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700868}
869
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700870/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700871 *
872 * Create a journal structure assigned some fixed set of disk blocks to
873 * the journal. We don't actually touch those disk blocks yet, but we
874 * need to set up all of the mapping information to tell the journaling
875 * system where the journal blocks are.
876 *
877 */
878
879/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400880 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700881 * @bdev: Block device on which to create the journal
882 * @fs_dev: Device which hold journalled filesystem for this journal.
883 * @start: Block nr Start of journal.
884 * @len: Length of the journal in blocks.
885 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400886 *
887 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700888 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700889 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700890 * range of blocks on an arbitrary block device.
891 *
892 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700893journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700894 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700895 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700896{
897 journal_t *journal = journal_init_common();
898 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400899 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700900 int n;
901
902 if (!journal)
903 return NULL;
904
905 /* journal descriptor can store up to n blocks -bzzz */
906 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -0500907 journal->j_dev = bdev;
908 journal->j_fs_dev = fs_dev;
909 journal->j_blk_offset = start;
910 journal->j_maxlen = len;
911 bdevname(journal->j_dev, journal->j_devname);
912 p = journal->j_devname;
913 while ((p = strchr(p, '/')))
914 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -0500915 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700916 n = journal->j_blocksize / sizeof(journal_block_tag_t);
917 journal->j_wbufsize = n;
918 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
919 if (!journal->j_wbuf) {
920 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400921 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500922 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700923 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700924
925 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500926 if (!bh) {
927 printk(KERN_ERR
928 "%s: Cannot get buffer for journal superblock\n",
929 __func__);
930 goto out_err;
931 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700932 journal->j_sb_buffer = bh;
933 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500934
Dave Kleikamp470decc2006-10-11 01:20:57 -0700935 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500936out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800937 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500938 jbd2_stats_proc_exit(journal);
939 kfree(journal);
940 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700941}
942
943/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700944 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945 * @inode: An inode to create the journal in
946 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700947 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700948 * the journal. The inode must exist already, must support bmap() and
949 * must have all data blocks preallocated.
950 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700951journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700952{
953 struct buffer_head *bh;
954 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400955 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700956 int err;
957 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700958 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700959
960 if (!journal)
961 return NULL;
962
963 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
964 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400965 bdevname(journal->j_dev, journal->j_devname);
966 p = journal->j_devname;
967 while ((p = strchr(p, '/')))
968 *p = '!';
969 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400970 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700971 jbd_debug(1,
972 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
973 journal, inode->i_sb->s_id, inode->i_ino,
974 (long long) inode->i_size,
975 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
976
977 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
978 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500979 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700980
981 /* journal descriptor can store up to n blocks -bzzz */
982 n = journal->j_blocksize / sizeof(journal_block_tag_t);
983 journal->j_wbufsize = n;
984 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
985 if (!journal->j_wbuf) {
986 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400987 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500988 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700989 }
990
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700991 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700992 /* If that failed, give up */
993 if (err) {
994 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400995 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500996 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700997 }
998
999 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001000 if (!bh) {
1001 printk(KERN_ERR
1002 "%s: Cannot get buffer for journal superblock\n",
1003 __func__);
1004 goto out_err;
1005 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001006 journal->j_sb_buffer = bh;
1007 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1008
1009 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001010out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001011 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001012 jbd2_stats_proc_exit(journal);
1013 kfree(journal);
1014 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015}
1016
1017/*
1018 * If the journal init or create aborts, we need to mark the journal
1019 * superblock as being NULL to prevent the journal destroy from writing
1020 * back a bogus superblock.
1021 */
1022static void journal_fail_superblock (journal_t *journal)
1023{
1024 struct buffer_head *bh = journal->j_sb_buffer;
1025 brelse(bh);
1026 journal->j_sb_buffer = NULL;
1027}
1028
1029/*
1030 * Given a journal_t structure, initialise the various fields for
1031 * startup of a new journaling session. We use this both when creating
1032 * a journal, and after recovering an old journal to reset it for
1033 * subsequent use.
1034 */
1035
1036static int journal_reset(journal_t *journal)
1037{
1038 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001039 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040
1041 first = be32_to_cpu(sb->s_first);
1042 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001043 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1044 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1045 first, last);
1046 journal_fail_superblock(journal);
1047 return -EINVAL;
1048 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001049
1050 journal->j_first = first;
1051 journal->j_last = last;
1052
1053 journal->j_head = first;
1054 journal->j_tail = first;
1055 journal->j_free = last - first;
1056
1057 journal->j_tail_sequence = journal->j_transaction_sequence;
1058 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1059 journal->j_commit_request = journal->j_commit_sequence;
1060
1061 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1062
1063 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001064 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001065 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001066}
1067
1068/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001069 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070 * @journal: The journal to update.
1071 * @wait: Set to '0' if you don't want to wait for IO completion.
1072 *
1073 * Update a journal's dynamic superblock fields and write it to disk,
1074 * optionally waiting for the IO to complete.
1075 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001076void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001077{
1078 journal_superblock_t *sb = journal->j_superblock;
1079 struct buffer_head *bh = journal->j_sb_buffer;
1080
1081 /*
1082 * As a special case, if the on-disk copy is already marked as needing
1083 * no recovery (s_start == 0) and there are no outstanding transactions
1084 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001085 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001086 * attempting a write to a potential-readonly device.
1087 */
1088 if (sb->s_start == 0 && journal->j_tail_sequence ==
1089 journal->j_transaction_sequence) {
1090 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1091 "(start %ld, seq %d, errno %d)\n",
1092 journal->j_tail, journal->j_tail_sequence,
1093 journal->j_errno);
1094 goto out;
1095 }
1096
Theodore Ts'o914258b2008-10-06 21:35:40 -04001097 if (buffer_write_io_error(bh)) {
1098 /*
1099 * Oh, dear. A previous attempt to write the journal
1100 * superblock failed. This could happen because the
1101 * USB device was yanked out. Or it could happen to
1102 * be a transient write error and maybe the block will
1103 * be remapped. Nothing we can do but to retry the
1104 * write and hope for the best.
1105 */
1106 printk(KERN_ERR "JBD2: previous I/O error detected "
1107 "for journal superblock update for %s.\n",
1108 journal->j_devname);
1109 clear_buffer_write_io_error(bh);
1110 set_buffer_uptodate(bh);
1111 }
1112
Theodore Ts'oa931da62010-08-03 21:35:12 -04001113 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001114 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1115 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1116
1117 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1118 sb->s_start = cpu_to_be32(journal->j_tail);
1119 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001120 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121
1122 BUFFER_TRACE(bh, "marking dirty");
1123 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001124 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001125 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001126 if (buffer_write_io_error(bh)) {
1127 printk(KERN_ERR "JBD2: I/O error detected "
1128 "when updating journal superblock for %s.\n",
1129 journal->j_devname);
1130 clear_buffer_write_io_error(bh);
1131 set_buffer_uptodate(bh);
1132 }
1133 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001134 write_dirty_buffer(bh, WRITE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001135
1136out:
1137 /* If we have just flushed the log (by marking s_start==0), then
1138 * any future commit will have to be careful to update the
1139 * superblock again to re-record the true start of the log. */
1140
Theodore Ts'oa931da62010-08-03 21:35:12 -04001141 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001142 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001143 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001144 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001145 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001146 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001147}
1148
1149/*
1150 * Read the superblock for a given journal, performing initial
1151 * validation of the format.
1152 */
1153
1154static int journal_get_superblock(journal_t *journal)
1155{
1156 struct buffer_head *bh;
1157 journal_superblock_t *sb;
1158 int err = -EIO;
1159
1160 bh = journal->j_sb_buffer;
1161
1162 J_ASSERT(bh != NULL);
1163 if (!buffer_uptodate(bh)) {
1164 ll_rw_block(READ, 1, &bh);
1165 wait_on_buffer(bh);
1166 if (!buffer_uptodate(bh)) {
1167 printk (KERN_ERR
1168 "JBD: IO error reading journal superblock\n");
1169 goto out;
1170 }
1171 }
1172
1173 sb = journal->j_superblock;
1174
1175 err = -EINVAL;
1176
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001177 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001178 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1179 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1180 goto out;
1181 }
1182
1183 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001184 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001185 journal->j_format_version = 1;
1186 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001187 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001188 journal->j_format_version = 2;
1189 break;
1190 default:
1191 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1192 goto out;
1193 }
1194
1195 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1196 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1197 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1198 printk (KERN_WARNING "JBD: journal file too short\n");
1199 goto out;
1200 }
1201
1202 return 0;
1203
1204out:
1205 journal_fail_superblock(journal);
1206 return err;
1207}
1208
1209/*
1210 * Load the on-disk journal superblock and read the key fields into the
1211 * journal_t.
1212 */
1213
1214static int load_superblock(journal_t *journal)
1215{
1216 int err;
1217 journal_superblock_t *sb;
1218
1219 err = journal_get_superblock(journal);
1220 if (err)
1221 return err;
1222
1223 sb = journal->j_superblock;
1224
1225 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1226 journal->j_tail = be32_to_cpu(sb->s_start);
1227 journal->j_first = be32_to_cpu(sb->s_first);
1228 journal->j_last = be32_to_cpu(sb->s_maxlen);
1229 journal->j_errno = be32_to_cpu(sb->s_errno);
1230
1231 return 0;
1232}
1233
1234
1235/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001236 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001237 * @journal: Journal to act on.
1238 *
1239 * Given a journal_t structure which tells us which disk blocks contain
1240 * a journal, read the journal from disk to initialise the in-memory
1241 * structures.
1242 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001243int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001244{
1245 int err;
1246 journal_superblock_t *sb;
1247
1248 err = load_superblock(journal);
1249 if (err)
1250 return err;
1251
1252 sb = journal->j_superblock;
1253 /* If this is a V2 superblock, then we have to check the
1254 * features flags on it. */
1255
1256 if (journal->j_format_version >= 2) {
1257 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001258 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001259 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001260 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001261 printk (KERN_WARNING
1262 "JBD: Unrecognised features on journal\n");
1263 return -EINVAL;
1264 }
1265 }
1266
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001267 /*
1268 * Create a slab for this blocksize
1269 */
1270 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1271 if (err)
1272 return err;
1273
Dave Kleikamp470decc2006-10-11 01:20:57 -07001274 /* Let the recovery code check whether it needs to recover any
1275 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001276 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001277 goto recovery_error;
1278
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001279 if (journal->j_failed_commit) {
1280 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1281 "is corrupt.\n", journal->j_failed_commit,
1282 journal->j_devname);
1283 return -EIO;
1284 }
1285
Dave Kleikamp470decc2006-10-11 01:20:57 -07001286 /* OK, we've finished with the dynamic journal bits:
1287 * reinitialise the dynamic contents of the superblock in memory
1288 * and reset them on disk. */
1289 if (journal_reset(journal))
1290 goto recovery_error;
1291
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001292 journal->j_flags &= ~JBD2_ABORT;
1293 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294 return 0;
1295
1296recovery_error:
1297 printk (KERN_WARNING "JBD: recovery failed\n");
1298 return -EIO;
1299}
1300
1301/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001302 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001303 * @journal: Journal to act on.
1304 *
1305 * Release a journal_t structure once it is no longer in use by the
1306 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001307 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001308 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001309int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001310{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001311 int err = 0;
1312
Dave Kleikamp470decc2006-10-11 01:20:57 -07001313 /* Wait for the commit thread to wake up and die. */
1314 journal_kill_thread(journal);
1315
1316 /* Force a final log commit */
1317 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001318 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001319
1320 /* Force any old transactions to disk */
1321
1322 /* Totally anal locking here... */
1323 spin_lock(&journal->j_list_lock);
1324 while (journal->j_checkpoint_transactions != NULL) {
1325 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001326 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001327 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001328 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001329 spin_lock(&journal->j_list_lock);
1330 }
1331
1332 J_ASSERT(journal->j_running_transaction == NULL);
1333 J_ASSERT(journal->j_committing_transaction == NULL);
1334 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1335 spin_unlock(&journal->j_list_lock);
1336
Dave Kleikamp470decc2006-10-11 01:20:57 -07001337 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001338 if (!is_journal_aborted(journal)) {
1339 /* We can now mark the journal as empty. */
1340 journal->j_tail = 0;
1341 journal->j_tail_sequence =
1342 ++journal->j_transaction_sequence;
1343 jbd2_journal_update_superblock(journal, 1);
1344 } else {
1345 err = -EIO;
1346 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001347 brelse(journal->j_sb_buffer);
1348 }
1349
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001350 if (journal->j_proc_entry)
1351 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001352 if (journal->j_inode)
1353 iput(journal->j_inode);
1354 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001355 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001356 kfree(journal->j_wbuf);
1357 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001358
1359 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001360}
1361
1362
1363/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001364 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001365 * @journal: Journal to check.
1366 * @compat: bitmask of compatible features
1367 * @ro: bitmask of features that force read-only mount
1368 * @incompat: bitmask of incompatible features
1369 *
1370 * Check whether the journal uses all of a given set of
1371 * features. Return true (non-zero) if it does.
1372 **/
1373
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001374int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001375 unsigned long ro, unsigned long incompat)
1376{
1377 journal_superblock_t *sb;
1378
1379 if (!compat && !ro && !incompat)
1380 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001381 /* Load journal superblock if it is not loaded yet. */
1382 if (journal->j_format_version == 0 &&
1383 journal_get_superblock(journal) != 0)
1384 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001385 if (journal->j_format_version == 1)
1386 return 0;
1387
1388 sb = journal->j_superblock;
1389
1390 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1391 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1392 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1393 return 1;
1394
1395 return 0;
1396}
1397
1398/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001399 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400 * @journal: Journal to check.
1401 * @compat: bitmask of compatible features
1402 * @ro: bitmask of features that force read-only mount
1403 * @incompat: bitmask of incompatible features
1404 *
1405 * Check whether the journaling code supports the use of
1406 * all of a given set of features on this journal. Return true
1407 * (non-zero) if it can. */
1408
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001409int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001410 unsigned long ro, unsigned long incompat)
1411{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001412 if (!compat && !ro && !incompat)
1413 return 1;
1414
Dave Kleikamp470decc2006-10-11 01:20:57 -07001415 /* We can support any known requested features iff the
1416 * superblock is in version 2. Otherwise we fail to support any
1417 * extended sb features. */
1418
1419 if (journal->j_format_version != 2)
1420 return 0;
1421
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001422 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1423 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1424 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001425 return 1;
1426
1427 return 0;
1428}
1429
1430/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001431 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001432 * @journal: Journal to act on.
1433 * @compat: bitmask of compatible features
1434 * @ro: bitmask of features that force read-only mount
1435 * @incompat: bitmask of incompatible features
1436 *
1437 * Mark a given journal feature as present on the
1438 * superblock. Returns true if the requested features could be set.
1439 *
1440 */
1441
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001442int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443 unsigned long ro, unsigned long incompat)
1444{
1445 journal_superblock_t *sb;
1446
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001447 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001448 return 1;
1449
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001450 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001451 return 0;
1452
1453 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1454 compat, ro, incompat);
1455
1456 sb = journal->j_superblock;
1457
1458 sb->s_feature_compat |= cpu_to_be32(compat);
1459 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1460 sb->s_feature_incompat |= cpu_to_be32(incompat);
1461
1462 return 1;
1463}
1464
Girish Shilamkar818d2762008-01-28 23:58:27 -05001465/*
1466 * jbd2_journal_clear_features () - Clear a given journal feature in the
1467 * superblock
1468 * @journal: Journal to act on.
1469 * @compat: bitmask of compatible features
1470 * @ro: bitmask of features that force read-only mount
1471 * @incompat: bitmask of incompatible features
1472 *
1473 * Clear a given journal feature as present on the
1474 * superblock.
1475 */
1476void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1477 unsigned long ro, unsigned long incompat)
1478{
1479 journal_superblock_t *sb;
1480
1481 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1482 compat, ro, incompat);
1483
1484 sb = journal->j_superblock;
1485
1486 sb->s_feature_compat &= ~cpu_to_be32(compat);
1487 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1488 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1489}
1490EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001491
1492/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001493 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001494 * @journal: Journal to act on.
1495 *
1496 * Given an initialised but unloaded journal struct, poke about in the
1497 * on-disk structure to update it to the most recent supported version.
1498 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001499int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500{
1501 journal_superblock_t *sb;
1502 int err;
1503
1504 err = journal_get_superblock(journal);
1505 if (err)
1506 return err;
1507
1508 sb = journal->j_superblock;
1509
1510 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001511 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001512 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001513 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001514 return journal_convert_superblock_v1(journal, sb);
1515 default:
1516 break;
1517 }
1518 return -EINVAL;
1519}
1520
1521static int journal_convert_superblock_v1(journal_t *journal,
1522 journal_superblock_t *sb)
1523{
1524 int offset, blocksize;
1525 struct buffer_head *bh;
1526
1527 printk(KERN_WARNING
1528 "JBD: Converting superblock from version 1 to 2.\n");
1529
1530 /* Pre-initialise new fields to zero */
1531 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1532 blocksize = be32_to_cpu(sb->s_blocksize);
1533 memset(&sb->s_feature_compat, 0, blocksize-offset);
1534
1535 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001536 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001537 journal->j_format_version = 2;
1538
1539 bh = journal->j_sb_buffer;
1540 BUFFER_TRACE(bh, "marking dirty");
1541 mark_buffer_dirty(bh);
1542 sync_dirty_buffer(bh);
1543 return 0;
1544}
1545
1546
1547/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001548 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001549 * @journal: Journal to act on.
1550 *
1551 * Flush all data for a given journal to disk and empty the journal.
1552 * Filesystems can use this when remounting readonly to ensure that
1553 * recovery does not need to happen on remount.
1554 */
1555
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001556int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001557{
1558 int err = 0;
1559 transaction_t *transaction = NULL;
1560 unsigned long old_tail;
1561
Theodore Ts'oa931da62010-08-03 21:35:12 -04001562 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001563
1564 /* Force everything buffered to the log... */
1565 if (journal->j_running_transaction) {
1566 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001568 } else if (journal->j_committing_transaction)
1569 transaction = journal->j_committing_transaction;
1570
1571 /* Wait for the log commit to complete... */
1572 if (transaction) {
1573 tid_t tid = transaction->t_tid;
1574
Theodore Ts'oa931da62010-08-03 21:35:12 -04001575 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001576 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001577 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001578 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001579 }
1580
1581 /* ...and flush everything in the log out to disk. */
1582 spin_lock(&journal->j_list_lock);
1583 while (!err && journal->j_checkpoint_transactions != NULL) {
1584 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001585 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001586 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001587 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001588 spin_lock(&journal->j_list_lock);
1589 }
1590 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001591
1592 if (is_journal_aborted(journal))
1593 return -EIO;
1594
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001595 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596
1597 /* Finally, mark the journal as really needing no recovery.
1598 * This sets s_start==0 in the underlying superblock, which is
1599 * the magic code for a fully-recovered superblock. Any future
1600 * commits of data to the journal will restore the current
1601 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001602 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001603 old_tail = journal->j_tail;
1604 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001605 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001606 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001607 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001608 journal->j_tail = old_tail;
1609
1610 J_ASSERT(!journal->j_running_transaction);
1611 J_ASSERT(!journal->j_committing_transaction);
1612 J_ASSERT(!journal->j_checkpoint_transactions);
1613 J_ASSERT(journal->j_head == journal->j_tail);
1614 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001615 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001616 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617}
1618
1619/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001620 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001621 * @journal: Journal to act on.
1622 * @write: flag (see below)
1623 *
1624 * Wipe out all of the contents of a journal, safely. This will produce
1625 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001626 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627 *
1628 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1629 * we merely suppress recovery.
1630 */
1631
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001634 int err = 0;
1635
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001636 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001637
1638 err = load_superblock(journal);
1639 if (err)
1640 return err;
1641
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 if (!journal->j_tail)
1643 goto no_recovery;
1644
1645 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1646 write ? "Clearing" : "Ignoring");
1647
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001648 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001649 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001650 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001651
1652 no_recovery:
1653 return err;
1654}
1655
1656/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001657 * Journal abort has very specific semantics, which we describe
1658 * for journal abort.
1659 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001660 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001661 * itself are here.
1662 */
1663
1664/*
1665 * Quick version for internal journal use (doesn't lock the journal).
1666 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1667 * and don't attempt to make any other journal updates.
1668 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001669void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670{
1671 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001672
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001673 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 return;
1675
1676 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001677 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001678
Theodore Ts'oa931da62010-08-03 21:35:12 -04001679 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001680 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001681 transaction = journal->j_running_transaction;
1682 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001683 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001684 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001685}
1686
1687/* Soft abort: record the abort error status in the journal superblock,
1688 * but don't do any other IO. */
1689static void __journal_abort_soft (journal_t *journal, int errno)
1690{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001691 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001692 return;
1693
1694 if (!journal->j_errno)
1695 journal->j_errno = errno;
1696
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001697 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001698
1699 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001700 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701}
1702
1703/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001704 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001705 * @journal: the journal to shutdown.
1706 * @errno: an error number to record in the journal indicating
1707 * the reason for the shutdown.
1708 *
1709 * Perform a complete, immediate shutdown of the ENTIRE
1710 * journal (not of a single transaction). This operation cannot be
1711 * undone without closing and reopening the journal.
1712 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001713 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001714 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1715 * mode.
1716 *
1717 * Journal abort has very specific semantics. Any existing dirty,
1718 * unjournaled buffers in the main filesystem will still be written to
1719 * disk by bdflush, but the journaling mechanism will be suspended
1720 * immediately and no further transaction commits will be honoured.
1721 *
1722 * Any dirty, journaled buffers will be written back to disk without
1723 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1724 * filesystem, but we _do_ attempt to leave as much data as possible
1725 * behind for fsck to use for cleanup.
1726 *
1727 * Any attempt to get a new transaction handle on a journal which is in
1728 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001729 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001730 * entered abort state during the update.
1731 *
1732 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001733 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001734 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001735 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001736 * which will be recorded (if possible) in the journal superblock. This
1737 * allows a client to record failure conditions in the middle of a
1738 * transaction without having to complete the transaction to record the
1739 * failure to disk. ext3_error, for example, now uses this
1740 * functionality.
1741 *
1742 * Errors which originate from within the journaling layer will NOT
1743 * supply an errno; a null errno implies that absolutely no further
1744 * writes are done to the journal (unless there are any already in
1745 * progress).
1746 *
1747 */
1748
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001749void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750{
1751 __journal_abort_soft(journal, errno);
1752}
1753
1754/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001755 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001756 * @journal: journal to examine.
1757 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001758 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001759 * time the journal was mounted - if the journal was stopped
1760 * without calling abort this will be 0.
1761 *
1762 * If the journal has been aborted on this mount time -EROFS will
1763 * be returned.
1764 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001765int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766{
1767 int err;
1768
Theodore Ts'oa931da62010-08-03 21:35:12 -04001769 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001770 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001771 err = -EROFS;
1772 else
1773 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001774 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001775 return err;
1776}
1777
1778/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001779 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001780 * @journal: journal to act on.
1781 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001782 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001783 * mode.
1784 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001785int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001786{
1787 int err = 0;
1788
Theodore Ts'oa931da62010-08-03 21:35:12 -04001789 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001790 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 err = -EROFS;
1792 else
1793 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001794 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001795 return err;
1796}
1797
1798/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001799 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001800 * @journal: journal to act on.
1801 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001802 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001803 * mode.
1804 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001805void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001807 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001809 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001810 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811}
1812
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001813int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001814{
1815 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1816}
1817
1818/*
Zach Brownb517bea2006-10-11 01:21:08 -07001819 * helper functions to deal with 32 or 64bit block numbers.
1820 */
1821size_t journal_tag_bytes(journal_t *journal)
1822{
1823 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001824 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001825 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001826 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001827}
1828
1829/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001830 * JBD memory management
1831 *
1832 * These functions are used to allocate block-sized chunks of memory
1833 * used for making copies of buffer_head data. Very often it will be
1834 * page-sized chunks of data, but sometimes it will be in
1835 * sub-page-size chunks. (For example, 16k pages on Power systems
1836 * with a 4k block file system.) For blocks smaller than a page, we
1837 * use a SLAB allocator. There are slab caches for each block size,
1838 * which are allocated at mount time, if necessary, and we only free
1839 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1840 * this reason we don't need to a mutex to protect access to
1841 * jbd2_slab[] allocating or releasing memory; only in
1842 * jbd2_journal_create_slab().
1843 */
1844#define JBD2_MAX_SLABS 8
1845static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001846
1847static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1848 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1849 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1850};
1851
1852
1853static void jbd2_journal_destroy_slabs(void)
1854{
1855 int i;
1856
1857 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1858 if (jbd2_slab[i])
1859 kmem_cache_destroy(jbd2_slab[i]);
1860 jbd2_slab[i] = NULL;
1861 }
1862}
1863
1864static int jbd2_journal_create_slab(size_t size)
1865{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001866 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001867 int i = order_base_2(size) - 10;
1868 size_t slab_size;
1869
1870 if (size == PAGE_SIZE)
1871 return 0;
1872
1873 if (i >= JBD2_MAX_SLABS)
1874 return -EINVAL;
1875
1876 if (unlikely(i < 0))
1877 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001878 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001879 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001880 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001881 return 0; /* Already created */
1882 }
1883
1884 slab_size = 1 << (i+10);
1885 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1886 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001887 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001888 if (!jbd2_slab[i]) {
1889 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1890 return -ENOMEM;
1891 }
1892 return 0;
1893}
1894
1895static struct kmem_cache *get_slab(size_t size)
1896{
1897 int i = order_base_2(size) - 10;
1898
1899 BUG_ON(i >= JBD2_MAX_SLABS);
1900 if (unlikely(i < 0))
1901 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001902 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001903 return jbd2_slab[i];
1904}
1905
1906void *jbd2_alloc(size_t size, gfp_t flags)
1907{
1908 void *ptr;
1909
1910 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1911
1912 flags |= __GFP_REPEAT;
1913 if (size == PAGE_SIZE)
1914 ptr = (void *)__get_free_pages(flags, 0);
1915 else if (size > PAGE_SIZE) {
1916 int order = get_order(size);
1917
1918 if (order < 3)
1919 ptr = (void *)__get_free_pages(flags, order);
1920 else
1921 ptr = vmalloc(size);
1922 } else
1923 ptr = kmem_cache_alloc(get_slab(size), flags);
1924
1925 /* Check alignment; SLUB has gotten this wrong in the past,
1926 * and this can lead to user data corruption! */
1927 BUG_ON(((unsigned long) ptr) & (size-1));
1928
1929 return ptr;
1930}
1931
1932void jbd2_free(void *ptr, size_t size)
1933{
1934 if (size == PAGE_SIZE) {
1935 free_pages((unsigned long)ptr, 0);
1936 return;
1937 }
1938 if (size > PAGE_SIZE) {
1939 int order = get_order(size);
1940
1941 if (order < 3)
1942 free_pages((unsigned long)ptr, order);
1943 else
1944 vfree(ptr);
1945 return;
1946 }
1947 kmem_cache_free(get_slab(size), ptr);
1948};
1949
1950/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001951 * Journal_head storage management
1952 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001953static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001954#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1956#endif
1957
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001958static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001959{
1960 int retval;
1961
Al Viro1076d172008-03-29 03:07:18 +00001962 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001963 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964 sizeof(struct journal_head),
1965 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001966 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001967 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001969 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001970 retval = -ENOMEM;
1971 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1972 }
1973 return retval;
1974}
1975
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001976static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001977{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001978 if (jbd2_journal_head_cache) {
1979 kmem_cache_destroy(jbd2_journal_head_cache);
1980 jbd2_journal_head_cache = NULL;
1981 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001982}
1983
1984/*
1985 * journal_head splicing and dicing
1986 */
1987static struct journal_head *journal_alloc_journal_head(void)
1988{
1989 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001990
Jose R. Santose23291b2007-07-18 08:57:06 -04001991#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001992 atomic_inc(&nr_journal_heads);
1993#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001994 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001995 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001996 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05001997 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00001998 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002000 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002001 }
2002 }
2003 return ret;
2004}
2005
2006static void journal_free_journal_head(struct journal_head *jh)
2007{
Jose R. Santose23291b2007-07-18 08:57:06 -04002008#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002009 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002010 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002012 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013}
2014
2015/*
2016 * A journal_head is attached to a buffer_head whenever JBD has an
2017 * interest in the buffer.
2018 *
2019 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2020 * is set. This bit is tested in core kernel code where we need to take
2021 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2022 * there.
2023 *
2024 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2025 *
2026 * When a buffer has its BH_JBD bit set it is immune from being released by
2027 * core kernel code, mainly via ->b_count.
2028 *
2029 * A journal_head may be detached from its buffer_head when the journal_head's
2030 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002031 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002032 * journal_head can be dropped if needed.
2033 *
2034 * Various places in the kernel want to attach a journal_head to a buffer_head
2035 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002036 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002037 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002038 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002039 *
2040 * So the typical usage would be:
2041 *
2042 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002043 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 * ...
2045 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002046 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002047 *
2048 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2049 * because it has a non-zero b_transaction.
2050 */
2051
2052/*
2053 * Give a buffer_head a journal_head.
2054 *
2055 * Doesn't need the journal lock.
2056 * May sleep.
2057 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002058struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002059{
2060 struct journal_head *jh;
2061 struct journal_head *new_jh = NULL;
2062
2063repeat:
2064 if (!buffer_jbd(bh)) {
2065 new_jh = journal_alloc_journal_head();
2066 memset(new_jh, 0, sizeof(*new_jh));
2067 }
2068
2069 jbd_lock_bh_journal_head(bh);
2070 if (buffer_jbd(bh)) {
2071 jh = bh2jh(bh);
2072 } else {
2073 J_ASSERT_BH(bh,
2074 (atomic_read(&bh->b_count) > 0) ||
2075 (bh->b_page && bh->b_page->mapping));
2076
2077 if (!new_jh) {
2078 jbd_unlock_bh_journal_head(bh);
2079 goto repeat;
2080 }
2081
2082 jh = new_jh;
2083 new_jh = NULL; /* We consumed it */
2084 set_buffer_jbd(bh);
2085 bh->b_private = jh;
2086 jh->b_bh = bh;
2087 get_bh(bh);
2088 BUFFER_TRACE(bh, "added journal_head");
2089 }
2090 jh->b_jcount++;
2091 jbd_unlock_bh_journal_head(bh);
2092 if (new_jh)
2093 journal_free_journal_head(new_jh);
2094 return bh->b_private;
2095}
2096
2097/*
2098 * Grab a ref against this buffer_head's journal_head. If it ended up not
2099 * having a journal_head, return NULL
2100 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002101struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002102{
2103 struct journal_head *jh = NULL;
2104
2105 jbd_lock_bh_journal_head(bh);
2106 if (buffer_jbd(bh)) {
2107 jh = bh2jh(bh);
2108 jh->b_jcount++;
2109 }
2110 jbd_unlock_bh_journal_head(bh);
2111 return jh;
2112}
2113
2114static void __journal_remove_journal_head(struct buffer_head *bh)
2115{
2116 struct journal_head *jh = bh2jh(bh);
2117
2118 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2119
2120 get_bh(bh);
2121 if (jh->b_jcount == 0) {
2122 if (jh->b_transaction == NULL &&
2123 jh->b_next_transaction == NULL &&
2124 jh->b_cp_transaction == NULL) {
2125 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2126 J_ASSERT_BH(bh, buffer_jbd(bh));
2127 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2128 BUFFER_TRACE(bh, "remove journal_head");
2129 if (jh->b_frozen_data) {
2130 printk(KERN_WARNING "%s: freeing "
2131 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002132 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002133 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002134 }
2135 if (jh->b_committed_data) {
2136 printk(KERN_WARNING "%s: freeing "
2137 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002138 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002139 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002140 }
2141 bh->b_private = NULL;
2142 jh->b_bh = NULL; /* debug, really */
2143 clear_buffer_jbd(bh);
2144 __brelse(bh);
2145 journal_free_journal_head(jh);
2146 } else {
2147 BUFFER_TRACE(bh, "journal_head was locked");
2148 }
2149 }
2150}
2151
2152/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002153 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002154 * and has a zero b_jcount then remove and release its journal_head. If we did
2155 * see that the buffer is not used by any transaction we also "logically"
2156 * decrement ->b_count.
2157 *
2158 * We in fact take an additional increment on ->b_count as a convenience,
2159 * because the caller usually wants to do additional things with the bh
2160 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002161 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002162 * time. Once the caller has run __brelse(), the buffer is eligible for
2163 * reaping by try_to_free_buffers().
2164 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002165void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002166{
2167 jbd_lock_bh_journal_head(bh);
2168 __journal_remove_journal_head(bh);
2169 jbd_unlock_bh_journal_head(bh);
2170}
2171
2172/*
2173 * Drop a reference on the passed journal_head. If it fell to zero then try to
2174 * release the journal_head from the buffer_head.
2175 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002176void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002177{
2178 struct buffer_head *bh = jh2bh(jh);
2179
2180 jbd_lock_bh_journal_head(bh);
2181 J_ASSERT_JH(jh, jh->b_jcount > 0);
2182 --jh->b_jcount;
2183 if (!jh->b_jcount && !jh->b_transaction) {
2184 __journal_remove_journal_head(bh);
2185 __brelse(bh);
2186 }
2187 jbd_unlock_bh_journal_head(bh);
2188}
2189
2190/*
Jan Karac851ed52008-07-11 19:27:31 -04002191 * Initialize jbd inode head
2192 */
2193void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2194{
2195 jinode->i_transaction = NULL;
2196 jinode->i_next_transaction = NULL;
2197 jinode->i_vfs_inode = inode;
2198 jinode->i_flags = 0;
2199 INIT_LIST_HEAD(&jinode->i_list);
2200}
2201
2202/*
2203 * Function to be called before we start removing inode from memory (i.e.,
2204 * clear_inode() is a fine place to be called from). It removes inode from
2205 * transaction's lists.
2206 */
2207void jbd2_journal_release_jbd_inode(journal_t *journal,
2208 struct jbd2_inode *jinode)
2209{
Jan Karac851ed52008-07-11 19:27:31 -04002210 if (!journal)
2211 return;
2212restart:
2213 spin_lock(&journal->j_list_lock);
2214 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002215 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002216 wait_queue_head_t *wq;
2217 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2218 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2219 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2220 spin_unlock(&journal->j_list_lock);
2221 schedule();
2222 finish_wait(wq, &wait.wait);
2223 goto restart;
2224 }
2225
Jan Karac851ed52008-07-11 19:27:31 -04002226 if (jinode->i_transaction) {
2227 list_del(&jinode->i_list);
2228 jinode->i_transaction = NULL;
2229 }
2230 spin_unlock(&journal->j_list_lock);
2231}
2232
2233/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002234 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002235 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002236#ifdef CONFIG_JBD2_DEBUG
2237u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002238EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002239
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002240#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002241
Jose R. Santos6f38c742007-10-16 18:38:25 -04002242static struct dentry *jbd2_debugfs_dir;
2243static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002244
2245static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002246{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002247 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2248 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002249 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2250 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002251 jbd2_debugfs_dir,
2252 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002253}
2254
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002255static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002256{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002257 debugfs_remove(jbd2_debug);
2258 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002259}
2260
2261#else
2262
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002263static void __init jbd2_create_debugfs_entry(void)
2264{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002265}
2266
2267static void __exit jbd2_remove_debugfs_entry(void)
2268{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002269}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002270
2271#endif
2272
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002273#ifdef CONFIG_PROC_FS
2274
2275#define JBD2_STATS_PROC_NAME "fs/jbd2"
2276
2277static void __init jbd2_create_jbd_stats_proc_entry(void)
2278{
2279 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2280}
2281
2282static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2283{
2284 if (proc_jbd2_stats)
2285 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2286}
2287
2288#else
2289
2290#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2291#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2292
2293#endif
2294
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002295struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002296
2297static int __init journal_init_handle_cache(void)
2298{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002299 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002300 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002301 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2302 return -ENOMEM;
2303 }
2304 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2305 if (jbd2_inode_cache == NULL) {
2306 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2307 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002308 return -ENOMEM;
2309 }
2310 return 0;
2311}
2312
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002313static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002314{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002315 if (jbd2_handle_cache)
2316 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002317 if (jbd2_inode_cache)
2318 kmem_cache_destroy(jbd2_inode_cache);
2319
Dave Kleikamp470decc2006-10-11 01:20:57 -07002320}
2321
2322/*
2323 * Module startup and shutdown
2324 */
2325
2326static int __init journal_init_caches(void)
2327{
2328 int ret;
2329
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002330 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002331 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002332 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002333 if (ret == 0)
2334 ret = journal_init_handle_cache();
2335 return ret;
2336}
2337
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002338static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002339{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340 jbd2_journal_destroy_revoke_caches();
2341 jbd2_journal_destroy_jbd2_journal_head_cache();
2342 jbd2_journal_destroy_handle_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002343 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002344}
2345
2346static int __init journal_init(void)
2347{
2348 int ret;
2349
2350 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2351
2352 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002353 if (ret == 0) {
2354 jbd2_create_debugfs_entry();
2355 jbd2_create_jbd_stats_proc_entry();
2356 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002357 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002358 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002359 return ret;
2360}
2361
2362static void __exit journal_exit(void)
2363{
Jose R. Santose23291b2007-07-18 08:57:06 -04002364#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002365 int n = atomic_read(&nr_journal_heads);
2366 if (n)
2367 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2368#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002369 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002370 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002371 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002372}
2373
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002374/*
2375 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2376 * tracing infrastructure to map a dev_t to a device name.
2377 *
2378 * The caller should use rcu_read_lock() in order to make sure the
2379 * device name stays valid until its done with it. We use
2380 * rcu_read_lock() as well to make sure we're safe in case the caller
2381 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2382 * nested.
2383 */
2384struct devname_cache {
2385 struct rcu_head rcu;
2386 dev_t device;
2387 char devname[BDEVNAME_SIZE];
2388};
2389#define CACHE_SIZE_BITS 6
2390static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2391static DEFINE_SPINLOCK(devname_cache_lock);
2392
2393static void free_devcache(struct rcu_head *rcu)
2394{
2395 kfree(rcu);
2396}
2397
2398const char *jbd2_dev_to_name(dev_t device)
2399{
2400 int i = hash_32(device, CACHE_SIZE_BITS);
2401 char *ret;
2402 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002403 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002404
2405 rcu_read_lock();
2406 if (devcache[i] && devcache[i]->device == device) {
2407 ret = devcache[i]->devname;
2408 rcu_read_unlock();
2409 return ret;
2410 }
2411 rcu_read_unlock();
2412
Theodore Ts'ob5744802009-06-20 23:34:44 -04002413 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2414 if (!new_dev)
2415 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002416 spin_lock(&devname_cache_lock);
2417 if (devcache[i]) {
2418 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002419 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002420 ret = devcache[i]->devname;
2421 spin_unlock(&devname_cache_lock);
2422 return ret;
2423 }
2424 call_rcu(&devcache[i]->rcu, free_devcache);
2425 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002426 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002427 devcache[i]->device = device;
2428 bd = bdget(device);
2429 if (bd) {
2430 bdevname(bd, devcache[i]->devname);
2431 bdput(bd);
2432 } else
2433 __bdevname(device, devcache[i]->devname);
2434 ret = devcache[i]->devname;
2435 spin_unlock(&devname_cache_lock);
2436 return ret;
2437}
2438EXPORT_SYMBOL(jbd2_dev_to_name);
2439
Dave Kleikamp470decc2006-10-11 01:20:57 -07002440MODULE_LICENSE("GPL");
2441module_init(journal_init);
2442module_exit(journal_exit);
2443