blob: 06dfd778cae5efbe493e1c10ad5d1f1f5ffd9d3a [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);
Dave Kleikamp470decc2006-10-11 01:20:57 -070097
98static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
99static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -0500100static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700101
102/*
103 * Helper function used to manage commit timeouts
104 */
105
106static void commit_timeout(unsigned long __data)
107{
108 struct task_struct * p = (struct task_struct *) __data;
109
110 wake_up_process(p);
111}
112
113/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700114 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700115 * journal.
116 *
117 * This kernel thread is responsible for two things:
118 *
119 * 1) COMMIT: Every so often we need to commit the current state of the
120 * filesystem to disk. The journal thread is responsible for writing
121 * all of the metadata buffers to disk.
122 *
123 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
124 * of the data in that part of the log has been rewritten elsewhere on
125 * the disk. Flushing these old buffers to reclaim space in the log is
126 * known as checkpointing, and this thread is responsible for that job.
127 */
128
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700129static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700130{
131 journal_t *journal = arg;
132 transaction_t *transaction;
133
134 /*
135 * Set up an interval timer which can be used to trigger a commit wakeup
136 * after the commit interval expires
137 */
138 setup_timer(&journal->j_commit_timer, commit_timeout,
139 (unsigned long)current);
140
141 /* Record that the journal thread is running */
142 journal->j_task = current;
143 wake_up(&journal->j_wait_done_commit);
144
Dave Kleikamp470decc2006-10-11 01:20:57 -0700145 /*
146 * And now, wait forever for commit wakeup events.
147 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400148 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149
150loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700151 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700152 goto end_loop;
153
154 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
155 journal->j_commit_sequence, journal->j_commit_request);
156
157 if (journal->j_commit_sequence != journal->j_commit_request) {
158 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400159 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700161 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400162 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700163 goto loop;
164 }
165
166 wake_up(&journal->j_wait_done_commit);
167 if (freezing(current)) {
168 /*
169 * The simpler the better. Flushing journal isn't a
170 * good idea, because that depends on threads that may
171 * be already stopped.
172 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700173 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400174 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700175 refrigerator();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400176 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700177 } else {
178 /*
179 * We assume on resume that commits are already there,
180 * so we don't sleep
181 */
182 DEFINE_WAIT(wait);
183 int should_sleep = 1;
184
185 prepare_to_wait(&journal->j_wait_commit, &wait,
186 TASK_INTERRUPTIBLE);
187 if (journal->j_commit_sequence != journal->j_commit_request)
188 should_sleep = 0;
189 transaction = journal->j_running_transaction;
190 if (transaction && time_after_eq(jiffies,
191 transaction->t_expires))
192 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700193 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700194 should_sleep = 0;
195 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400196 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400198 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700199 }
200 finish_wait(&journal->j_wait_commit, &wait);
201 }
202
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700203 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700204
205 /*
206 * Were we woken up by a commit wakeup event?
207 */
208 transaction = journal->j_running_transaction;
209 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
210 journal->j_commit_request = transaction->t_tid;
211 jbd_debug(1, "woke because of timeout\n");
212 }
213 goto loop;
214
215end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400216 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700217 del_timer_sync(&journal->j_commit_timer);
218 journal->j_task = NULL;
219 wake_up(&journal->j_wait_done_commit);
220 jbd_debug(1, "Journal thread exiting.\n");
221 return 0;
222}
223
Pavel Emelianov97f06782007-05-08 00:30:42 -0700224static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700225{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700226 struct task_struct *t;
227
Theodore Ts'o90576c02009-09-29 15:51:30 -0400228 t = kthread_run(kjournald2, journal, "jbd2/%s",
229 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700230 if (IS_ERR(t))
231 return PTR_ERR(t);
232
Al Viro1076d172008-03-29 03:07:18 +0000233 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700234 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700235}
236
237static void journal_kill_thread(journal_t *journal)
238{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400239 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700240 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700241
242 while (journal->j_task) {
243 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400244 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000245 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400246 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700247 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400248 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700249}
250
251/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700252 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700253 *
254 * Writes a metadata buffer to a given disk block. The actual IO is not
255 * performed but a new buffer_head is constructed which labels the data
256 * to be written with the correct destination disk block.
257 *
258 * Any magic-number escaping which needs to be done will cause a
259 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700260 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700261 * magic number is only written to the log for descripter blocks. In
262 * this case, we copy the data and replace the first word with 0, and we
263 * return a result code which indicates that this buffer needs to be
264 * marked as an escaped buffer in the corresponding log descriptor
265 * block. The missing word can then be restored when the block is read
266 * during recovery.
267 *
268 * If the source buffer has already been modified by a new transaction
269 * since we took the last commit snapshot, we use the frozen copy of
270 * that data for IO. If we end up using the existing buffer_head's data
271 * for the write, then we *have* to lock the buffer to prevent anyone
272 * else from using and possibly modifying it while the IO is in
273 * progress.
274 *
275 * The function returns a pointer to the buffer_heads to be used for IO.
276 *
277 * We assume that the journal has already been locked in this function.
278 *
279 * Return value:
280 * <0: Error
281 * >=0: Finished OK
282 *
283 * On success:
284 * Bit 0 set == escape performed on the data
285 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
286 */
287
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700288int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700289 struct journal_head *jh_in,
290 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700291 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700292{
293 int need_copy_out = 0;
294 int done_copy_out = 0;
295 int do_escape = 0;
296 char *mapped_data;
297 struct buffer_head *new_bh;
298 struct journal_head *new_jh;
299 struct page *new_page;
300 unsigned int new_offset;
301 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400302 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700303
304 /*
305 * The buffer really shouldn't be locked: only the current committing
306 * transaction is allowed to write it, so nobody else is allowed
307 * to do any IO.
308 *
309 * akpm: except if we're journalling data, and write() output is
310 * also part of a shared mapping, and another thread has
311 * decided to launch a writepage() against this buffer.
312 */
313 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
314
Theodore Ts'o47def822010-07-27 11:56:05 -0400315retry_alloc:
316 new_bh = alloc_buffer_head(GFP_NOFS);
317 if (!new_bh) {
318 /*
319 * Failure is not an option, but __GFP_NOFAIL is going
320 * away; so we retry ourselves here.
321 */
322 congestion_wait(BLK_RW_ASYNC, HZ/50);
323 goto retry_alloc;
324 }
325
dingdinghua96577c42009-07-13 17:55:35 -0400326 /* keep subsequent assertions sane */
327 new_bh->b_state = 0;
328 init_buffer(new_bh, NULL, NULL);
329 atomic_set(&new_bh->b_count, 1);
330 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700331
332 /*
333 * If a new transaction has already done a buffer copy-out, then
334 * we use that version of the data for the commit.
335 */
336 jbd_lock_bh_state(bh_in);
337repeat:
338 if (jh_in->b_frozen_data) {
339 done_copy_out = 1;
340 new_page = virt_to_page(jh_in->b_frozen_data);
341 new_offset = offset_in_page(jh_in->b_frozen_data);
342 } else {
343 new_page = jh2bh(jh_in)->b_page;
344 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
345 }
346
347 mapped_data = kmap_atomic(new_page, KM_USER0);
348 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200349 * Fire data frozen trigger if data already wasn't frozen. Do this
350 * before checking for escaping, as the trigger may modify the magic
351 * offset. If a copy-out happens afterwards, it will have the correct
352 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700353 */
Jan Kara13ceef02010-07-14 07:56:33 +0200354 if (!done_copy_out)
355 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
356 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700357
358 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700359 * Check for escaping
360 */
361 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700362 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700363 need_copy_out = 1;
364 do_escape = 1;
365 }
366 kunmap_atomic(mapped_data, KM_USER0);
367
368 /*
369 * Do we need to do a data copy?
370 */
371 if (need_copy_out && !done_copy_out) {
372 char *tmp;
373
374 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400375 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500376 if (!tmp) {
377 jbd2_journal_put_journal_head(new_jh);
378 return -ENOMEM;
379 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700380 jbd_lock_bh_state(bh_in);
381 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400382 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700383 goto repeat;
384 }
385
386 jh_in->b_frozen_data = tmp;
387 mapped_data = kmap_atomic(new_page, KM_USER0);
388 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
389 kunmap_atomic(mapped_data, KM_USER0);
390
391 new_page = virt_to_page(tmp);
392 new_offset = offset_in_page(tmp);
393 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700394
395 /*
396 * This isn't strictly necessary, as we're using frozen
397 * data for the escaping, but it keeps consistency with
398 * b_frozen_data usage.
399 */
400 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700401 }
402
403 /*
404 * Did we need to do an escaping? Now we've done all the
405 * copying, we can finally do so.
406 */
407 if (do_escape) {
408 mapped_data = kmap_atomic(new_page, KM_USER0);
409 *((unsigned int *)(mapped_data + new_offset)) = 0;
410 kunmap_atomic(mapped_data, KM_USER0);
411 }
412
Dave Kleikamp470decc2006-10-11 01:20:57 -0700413 set_bh_page(new_bh, new_page, new_offset);
414 new_jh->b_transaction = NULL;
415 new_bh->b_size = jh2bh(jh_in)->b_size;
416 new_bh->b_bdev = transaction->t_journal->j_dev;
417 new_bh->b_blocknr = blocknr;
418 set_buffer_mapped(new_bh);
419 set_buffer_dirty(new_bh);
420
421 *jh_out = new_jh;
422
423 /*
424 * The to-be-written buffer needs to get moved to the io queue,
425 * and the original buffer whose contents we are shadowing or
426 * copying is moved to the transaction's shadow queue.
427 */
428 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400429 spin_lock(&journal->j_list_lock);
430 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
431 spin_unlock(&journal->j_list_lock);
432 jbd_unlock_bh_state(bh_in);
433
Dave Kleikamp470decc2006-10-11 01:20:57 -0700434 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700435 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700436
437 return do_escape | (done_copy_out << 1);
438}
439
440/*
441 * Allocation code for the journal file. Manage the space left in the
442 * journal, so that we can begin checkpointing when appropriate.
443 */
444
445/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700446 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700447 *
448 * Called with the journal already locked.
449 *
450 * Called under j_state_lock
451 */
452
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700453int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700454{
455 int left = journal->j_free;
456
Theodore Ts'oa931da62010-08-03 21:35:12 -0400457 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700458
459 /*
460 * Be pessimistic here about the number of those free blocks which
461 * might be required for log descriptor control blocks.
462 */
463
464#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
465
466 left -= MIN_LOG_RESERVED_BLOCKS;
467
468 if (left <= 0)
469 return 0;
470 left -= (left >> 3);
471 return left;
472}
473
474/*
Jan Karac88ccea2009-02-10 11:27:46 -0500475 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700476 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700477int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478{
479 /*
480 * Are we already doing a recent enough commit?
481 */
482 if (!tid_geq(journal->j_commit_request, target)) {
483 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200484 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485 * commit thread. We do _not_ do the commit ourselves.
486 */
487
488 journal->j_commit_request = target;
489 jbd_debug(1, "JBD: requesting commit %d/%d\n",
490 journal->j_commit_request,
491 journal->j_commit_sequence);
492 wake_up(&journal->j_wait_commit);
493 return 1;
494 }
495 return 0;
496}
497
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700498int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700499{
500 int ret;
501
Theodore Ts'oa931da62010-08-03 21:35:12 -0400502 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400504 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505 return ret;
506}
507
508/*
509 * Force and wait upon a commit if the calling process is not within
510 * transaction. This is used for forcing out undo-protected data which contains
511 * bitmaps, when the fs is running out of space.
512 *
513 * We can only force the running transaction if we don't have an active handle;
514 * otherwise, we will deadlock.
515 *
516 * Returns true if a transaction was started.
517 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700518int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700519{
520 transaction_t *transaction = NULL;
521 tid_t tid;
522
Theodore Ts'oa931da62010-08-03 21:35:12 -0400523 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700524 if (journal->j_running_transaction && !current->journal_info) {
525 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700526 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 } else if (journal->j_committing_transaction)
528 transaction = journal->j_committing_transaction;
529
530 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400531 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532 return 0; /* Nothing to retry */
533 }
534
535 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400536 read_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700537 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700538 return 1;
539}
540
541/*
542 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500543 * if a transaction is going to be committed (or is currently already
544 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700545 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700546int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547{
548 int ret = 0;
549
Theodore Ts'oa931da62010-08-03 21:35:12 -0400550 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551 if (journal->j_running_transaction) {
552 tid_t tid = journal->j_running_transaction->t_tid;
553
Jan Karac88ccea2009-02-10 11:27:46 -0500554 __jbd2_log_start_commit(journal, tid);
555 /* There's a running transaction and we've just made sure
556 * it's commit has been scheduled. */
557 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700558 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500559 ret = 1;
560 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700561 /*
562 * If ext3_write_super() recently started a commit, then we
563 * have to wait for completion of that transaction
564 */
Jan Karac88ccea2009-02-10 11:27:46 -0500565 if (ptid)
566 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700567 ret = 1;
568 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400569 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570 return ret;
571}
572
573/*
574 * Wait for a specified commit to complete.
575 * The caller may not hold the journal lock.
576 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700577int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700578{
579 int err = 0;
580
Theodore Ts'oa931da62010-08-03 21:35:12 -0400581 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400582#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700583 if (!tid_geq(journal->j_commit_request, tid)) {
584 printk(KERN_EMERG
585 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400586 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700588#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700589 while (tid_gt(tid, journal->j_commit_sequence)) {
590 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
591 tid, journal->j_commit_sequence);
592 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400593 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594 wait_event(journal->j_wait_done_commit,
595 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400596 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700597 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400598 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700599
600 if (unlikely(is_journal_aborted(journal))) {
601 printk(KERN_EMERG "journal commit I/O error\n");
602 err = -EIO;
603 }
604 return err;
605}
606
607/*
608 * Log buffer allocation routines:
609 */
610
Mingming Cao18eba7a2006-10-11 01:21:13 -0700611int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700612{
613 unsigned long blocknr;
614
Theodore Ts'oa931da62010-08-03 21:35:12 -0400615 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700616 J_ASSERT(journal->j_free > 1);
617
618 blocknr = journal->j_head;
619 journal->j_head++;
620 journal->j_free--;
621 if (journal->j_head == journal->j_last)
622 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400623 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700624 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625}
626
627/*
628 * Conversion of logical to physical block numbers for the journal
629 *
630 * On external journals the journal blocks are identity-mapped, so
631 * this is a no-op. If needed, we can use j_blk_offset - everything is
632 * ready.
633 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700634int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700635 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700636{
637 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700638 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700639
640 if (journal->j_inode) {
641 ret = bmap(journal->j_inode, blocknr);
642 if (ret)
643 *retp = ret;
644 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645 printk(KERN_ALERT "%s: journal block not found "
646 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400647 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700648 err = -EIO;
649 __journal_abort_soft(journal, err);
650 }
651 } else {
652 *retp = blocknr; /* +journal->j_blk_offset */
653 }
654 return err;
655}
656
657/*
658 * We play buffer_head aliasing tricks to write data/metadata blocks to
659 * the journal without copying their contents, but for journal
660 * descriptor blocks we do need to generate bona fide buffers.
661 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700662 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700663 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
664 * But we don't bother doing that, so there will be coherency problems with
665 * mmaps of blockdevs which hold live JBD-controlled filesystems.
666 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700667struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700668{
669 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700670 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 int err;
672
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700673 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700674
675 if (err)
676 return NULL;
677
678 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500679 if (!bh)
680 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700681 lock_buffer(bh);
682 memset(bh->b_data, 0, journal->j_blocksize);
683 set_buffer_uptodate(bh);
684 unlock_buffer(bh);
685 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700686 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700687}
688
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500689struct jbd2_stats_proc_session {
690 journal_t *journal;
691 struct transaction_stats_s *stats;
692 int start;
693 int max;
694};
695
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500696static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
697{
698 return *pos ? NULL : SEQ_START_TOKEN;
699}
700
701static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
702{
703 return NULL;
704}
705
706static int jbd2_seq_info_show(struct seq_file *seq, void *v)
707{
708 struct jbd2_stats_proc_session *s = seq->private;
709
710 if (v != SEQ_START_TOKEN)
711 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400712 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500713 s->stats->ts_tid,
714 s->journal->j_max_transaction_buffers);
715 if (s->stats->ts_tid == 0)
716 return 0;
717 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400718 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500719 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400720 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500721 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400722 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500723 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400724 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500725 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400726 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500727 seq_printf(seq, " %lluus average transaction commit time\n",
728 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500729 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400730 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500731 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400732 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500733 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400734 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500735 return 0;
736}
737
738static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
739{
740}
741
James Morris88e9d342009-09-22 16:43:43 -0700742static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500743 .start = jbd2_seq_info_start,
744 .next = jbd2_seq_info_next,
745 .stop = jbd2_seq_info_stop,
746 .show = jbd2_seq_info_show,
747};
748
749static int jbd2_seq_info_open(struct inode *inode, struct file *file)
750{
751 journal_t *journal = PDE(inode)->data;
752 struct jbd2_stats_proc_session *s;
753 int rc, size;
754
755 s = kmalloc(sizeof(*s), GFP_KERNEL);
756 if (s == NULL)
757 return -ENOMEM;
758 size = sizeof(struct transaction_stats_s);
759 s->stats = kmalloc(size, GFP_KERNEL);
760 if (s->stats == NULL) {
761 kfree(s);
762 return -ENOMEM;
763 }
764 spin_lock(&journal->j_history_lock);
765 memcpy(s->stats, &journal->j_stats, size);
766 s->journal = journal;
767 spin_unlock(&journal->j_history_lock);
768
769 rc = seq_open(file, &jbd2_seq_info_ops);
770 if (rc == 0) {
771 struct seq_file *m = file->private_data;
772 m->private = s;
773 } else {
774 kfree(s->stats);
775 kfree(s);
776 }
777 return rc;
778
779}
780
781static int jbd2_seq_info_release(struct inode *inode, struct file *file)
782{
783 struct seq_file *seq = file->private_data;
784 struct jbd2_stats_proc_session *s = seq->private;
785 kfree(s->stats);
786 kfree(s);
787 return seq_release(inode, file);
788}
789
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700790static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500791 .owner = THIS_MODULE,
792 .open = jbd2_seq_info_open,
793 .read = seq_read,
794 .llseek = seq_lseek,
795 .release = jbd2_seq_info_release,
796};
797
798static struct proc_dir_entry *proc_jbd2_stats;
799
800static void jbd2_stats_proc_init(journal_t *journal)
801{
Theodore Ts'o05496762008-09-16 14:36:17 -0400802 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500803 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700804 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
805 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500806 }
807}
808
809static void jbd2_stats_proc_exit(journal_t *journal)
810{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500811 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400812 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500813}
814
Dave Kleikamp470decc2006-10-11 01:20:57 -0700815/*
816 * Management for journal control blocks: functions to create and
817 * destroy journal_t structures, and to initialise and read existing
818 * journal blocks from disk. */
819
820/* First: create and setup a journal_t object in memory. We initialise
821 * very few fields yet: that has to wait until we have created the
822 * journal structures from from scratch, or loaded them from disk. */
823
824static journal_t * journal_init_common (void)
825{
826 journal_t *journal;
827 int err;
828
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500829 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700830 if (!journal)
831 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700832
833 init_waitqueue_head(&journal->j_wait_transaction_locked);
834 init_waitqueue_head(&journal->j_wait_logspace);
835 init_waitqueue_head(&journal->j_wait_done_commit);
836 init_waitqueue_head(&journal->j_wait_checkpoint);
837 init_waitqueue_head(&journal->j_wait_commit);
838 init_waitqueue_head(&journal->j_wait_updates);
839 mutex_init(&journal->j_barrier);
840 mutex_init(&journal->j_checkpoint_mutex);
841 spin_lock_init(&journal->j_revoke_lock);
842 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400843 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700844
Mingming Caocd02ff02007-10-16 18:38:25 -0400845 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500846 journal->j_min_batch_time = 0;
847 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700848
849 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700850 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700851
852 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700853 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700854 if (err) {
855 kfree(journal);
856 goto fail;
857 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500858
Theodore Ts'obf699322009-09-30 00:32:06 -0400859 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500860
Dave Kleikamp470decc2006-10-11 01:20:57 -0700861 return journal;
862fail:
863 return NULL;
864}
865
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700866/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700867 *
868 * Create a journal structure assigned some fixed set of disk blocks to
869 * the journal. We don't actually touch those disk blocks yet, but we
870 * need to set up all of the mapping information to tell the journaling
871 * system where the journal blocks are.
872 *
873 */
874
875/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400876 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700877 * @bdev: Block device on which to create the journal
878 * @fs_dev: Device which hold journalled filesystem for this journal.
879 * @start: Block nr Start of journal.
880 * @len: Length of the journal in blocks.
881 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400882 *
883 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700884 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700885 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700886 * range of blocks on an arbitrary block device.
887 *
888 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700889journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700890 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700891 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700892{
893 journal_t *journal = journal_init_common();
894 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400895 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700896 int n;
897
898 if (!journal)
899 return NULL;
900
901 /* journal descriptor can store up to n blocks -bzzz */
902 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -0500903 journal->j_dev = bdev;
904 journal->j_fs_dev = fs_dev;
905 journal->j_blk_offset = start;
906 journal->j_maxlen = len;
907 bdevname(journal->j_dev, journal->j_devname);
908 p = journal->j_devname;
909 while ((p = strchr(p, '/')))
910 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -0500911 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700912 n = journal->j_blocksize / sizeof(journal_block_tag_t);
913 journal->j_wbufsize = n;
914 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
915 if (!journal->j_wbuf) {
916 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400917 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500918 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700919 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700920
921 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500922 if (!bh) {
923 printk(KERN_ERR
924 "%s: Cannot get buffer for journal superblock\n",
925 __func__);
926 goto out_err;
927 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700928 journal->j_sb_buffer = bh;
929 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500930
Dave Kleikamp470decc2006-10-11 01:20:57 -0700931 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500932out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800933 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500934 jbd2_stats_proc_exit(journal);
935 kfree(journal);
936 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700937}
938
939/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700940 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700941 * @inode: An inode to create the journal in
942 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700943 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700944 * the journal. The inode must exist already, must support bmap() and
945 * must have all data blocks preallocated.
946 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700947journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700948{
949 struct buffer_head *bh;
950 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400951 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700952 int err;
953 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700954 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700955
956 if (!journal)
957 return NULL;
958
959 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
960 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400961 bdevname(journal->j_dev, journal->j_devname);
962 p = journal->j_devname;
963 while ((p = strchr(p, '/')))
964 *p = '!';
965 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400966 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700967 jbd_debug(1,
968 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
969 journal, inode->i_sb->s_id, inode->i_ino,
970 (long long) inode->i_size,
971 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
972
973 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
974 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500975 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700976
977 /* journal descriptor can store up to n blocks -bzzz */
978 n = journal->j_blocksize / sizeof(journal_block_tag_t);
979 journal->j_wbufsize = n;
980 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
981 if (!journal->j_wbuf) {
982 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400983 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500984 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700985 }
986
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700987 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700988 /* If that failed, give up */
989 if (err) {
990 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400991 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500992 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700993 }
994
995 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500996 if (!bh) {
997 printk(KERN_ERR
998 "%s: Cannot get buffer for journal superblock\n",
999 __func__);
1000 goto out_err;
1001 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002 journal->j_sb_buffer = bh;
1003 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1004
1005 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001006out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001007 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001008 jbd2_stats_proc_exit(journal);
1009 kfree(journal);
1010 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001011}
1012
1013/*
1014 * If the journal init or create aborts, we need to mark the journal
1015 * superblock as being NULL to prevent the journal destroy from writing
1016 * back a bogus superblock.
1017 */
1018static void journal_fail_superblock (journal_t *journal)
1019{
1020 struct buffer_head *bh = journal->j_sb_buffer;
1021 brelse(bh);
1022 journal->j_sb_buffer = NULL;
1023}
1024
1025/*
1026 * Given a journal_t structure, initialise the various fields for
1027 * startup of a new journaling session. We use this both when creating
1028 * a journal, and after recovering an old journal to reset it for
1029 * subsequent use.
1030 */
1031
1032static int journal_reset(journal_t *journal)
1033{
1034 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001035 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001036
1037 first = be32_to_cpu(sb->s_first);
1038 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001039 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1040 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1041 first, last);
1042 journal_fail_superblock(journal);
1043 return -EINVAL;
1044 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001045
1046 journal->j_first = first;
1047 journal->j_last = last;
1048
1049 journal->j_head = first;
1050 journal->j_tail = first;
1051 journal->j_free = last - first;
1052
1053 journal->j_tail_sequence = journal->j_transaction_sequence;
1054 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1055 journal->j_commit_request = journal->j_commit_sequence;
1056
1057 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1058
1059 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001060 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001061 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001062}
1063
1064/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001065 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001066 * @journal: The journal to update.
1067 * @wait: Set to '0' if you don't want to wait for IO completion.
1068 *
1069 * Update a journal's dynamic superblock fields and write it to disk,
1070 * optionally waiting for the IO to complete.
1071 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001072void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001073{
1074 journal_superblock_t *sb = journal->j_superblock;
1075 struct buffer_head *bh = journal->j_sb_buffer;
1076
1077 /*
1078 * As a special case, if the on-disk copy is already marked as needing
1079 * no recovery (s_start == 0) and there are no outstanding transactions
1080 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001081 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001082 * attempting a write to a potential-readonly device.
1083 */
1084 if (sb->s_start == 0 && journal->j_tail_sequence ==
1085 journal->j_transaction_sequence) {
1086 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1087 "(start %ld, seq %d, errno %d)\n",
1088 journal->j_tail, journal->j_tail_sequence,
1089 journal->j_errno);
1090 goto out;
1091 }
1092
Theodore Ts'o914258b2008-10-06 21:35:40 -04001093 if (buffer_write_io_error(bh)) {
1094 /*
1095 * Oh, dear. A previous attempt to write the journal
1096 * superblock failed. This could happen because the
1097 * USB device was yanked out. Or it could happen to
1098 * be a transient write error and maybe the block will
1099 * be remapped. Nothing we can do but to retry the
1100 * write and hope for the best.
1101 */
1102 printk(KERN_ERR "JBD2: previous I/O error detected "
1103 "for journal superblock update for %s.\n",
1104 journal->j_devname);
1105 clear_buffer_write_io_error(bh);
1106 set_buffer_uptodate(bh);
1107 }
1108
Theodore Ts'oa931da62010-08-03 21:35:12 -04001109 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001110 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1111 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1112
1113 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1114 sb->s_start = cpu_to_be32(journal->j_tail);
1115 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001116 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117
1118 BUFFER_TRACE(bh, "marking dirty");
1119 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001120 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001122 if (buffer_write_io_error(bh)) {
1123 printk(KERN_ERR "JBD2: I/O error detected "
1124 "when updating journal superblock for %s.\n",
1125 journal->j_devname);
1126 clear_buffer_write_io_error(bh);
1127 set_buffer_uptodate(bh);
1128 }
1129 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001130 write_dirty_buffer(bh, WRITE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131
1132out:
1133 /* If we have just flushed the log (by marking s_start==0), then
1134 * any future commit will have to be careful to update the
1135 * superblock again to re-record the true start of the log. */
1136
Theodore Ts'oa931da62010-08-03 21:35:12 -04001137 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001138 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001139 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001140 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001141 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001142 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143}
1144
1145/*
1146 * Read the superblock for a given journal, performing initial
1147 * validation of the format.
1148 */
1149
1150static int journal_get_superblock(journal_t *journal)
1151{
1152 struct buffer_head *bh;
1153 journal_superblock_t *sb;
1154 int err = -EIO;
1155
1156 bh = journal->j_sb_buffer;
1157
1158 J_ASSERT(bh != NULL);
1159 if (!buffer_uptodate(bh)) {
1160 ll_rw_block(READ, 1, &bh);
1161 wait_on_buffer(bh);
1162 if (!buffer_uptodate(bh)) {
1163 printk (KERN_ERR
1164 "JBD: IO error reading journal superblock\n");
1165 goto out;
1166 }
1167 }
1168
1169 sb = journal->j_superblock;
1170
1171 err = -EINVAL;
1172
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001173 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001174 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1175 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1176 goto out;
1177 }
1178
1179 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 journal->j_format_version = 1;
1182 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001183 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001184 journal->j_format_version = 2;
1185 break;
1186 default:
1187 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1188 goto out;
1189 }
1190
1191 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1192 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1193 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1194 printk (KERN_WARNING "JBD: journal file too short\n");
1195 goto out;
1196 }
1197
1198 return 0;
1199
1200out:
1201 journal_fail_superblock(journal);
1202 return err;
1203}
1204
1205/*
1206 * Load the on-disk journal superblock and read the key fields into the
1207 * journal_t.
1208 */
1209
1210static int load_superblock(journal_t *journal)
1211{
1212 int err;
1213 journal_superblock_t *sb;
1214
1215 err = journal_get_superblock(journal);
1216 if (err)
1217 return err;
1218
1219 sb = journal->j_superblock;
1220
1221 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1222 journal->j_tail = be32_to_cpu(sb->s_start);
1223 journal->j_first = be32_to_cpu(sb->s_first);
1224 journal->j_last = be32_to_cpu(sb->s_maxlen);
1225 journal->j_errno = be32_to_cpu(sb->s_errno);
1226
1227 return 0;
1228}
1229
1230
1231/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001232 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001233 * @journal: Journal to act on.
1234 *
1235 * Given a journal_t structure which tells us which disk blocks contain
1236 * a journal, read the journal from disk to initialise the in-memory
1237 * structures.
1238 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001239int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001240{
1241 int err;
1242 journal_superblock_t *sb;
1243
1244 err = load_superblock(journal);
1245 if (err)
1246 return err;
1247
1248 sb = journal->j_superblock;
1249 /* If this is a V2 superblock, then we have to check the
1250 * features flags on it. */
1251
1252 if (journal->j_format_version >= 2) {
1253 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001254 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001255 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001256 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 printk (KERN_WARNING
1258 "JBD: Unrecognised features on journal\n");
1259 return -EINVAL;
1260 }
1261 }
1262
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001263 /*
1264 * Create a slab for this blocksize
1265 */
1266 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1267 if (err)
1268 return err;
1269
Dave Kleikamp470decc2006-10-11 01:20:57 -07001270 /* Let the recovery code check whether it needs to recover any
1271 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001272 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001273 goto recovery_error;
1274
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001275 if (journal->j_failed_commit) {
1276 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1277 "is corrupt.\n", journal->j_failed_commit,
1278 journal->j_devname);
1279 return -EIO;
1280 }
1281
Dave Kleikamp470decc2006-10-11 01:20:57 -07001282 /* OK, we've finished with the dynamic journal bits:
1283 * reinitialise the dynamic contents of the superblock in memory
1284 * and reset them on disk. */
1285 if (journal_reset(journal))
1286 goto recovery_error;
1287
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001288 journal->j_flags &= ~JBD2_ABORT;
1289 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001290 return 0;
1291
1292recovery_error:
1293 printk (KERN_WARNING "JBD: recovery failed\n");
1294 return -EIO;
1295}
1296
1297/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001298 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001299 * @journal: Journal to act on.
1300 *
1301 * Release a journal_t structure once it is no longer in use by the
1302 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001303 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001304 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001305int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001306{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001307 int err = 0;
1308
Dave Kleikamp470decc2006-10-11 01:20:57 -07001309 /* Wait for the commit thread to wake up and die. */
1310 journal_kill_thread(journal);
1311
1312 /* Force a final log commit */
1313 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001314 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001315
1316 /* Force any old transactions to disk */
1317
1318 /* Totally anal locking here... */
1319 spin_lock(&journal->j_list_lock);
1320 while (journal->j_checkpoint_transactions != NULL) {
1321 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001322 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001323 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001324 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001325 spin_lock(&journal->j_list_lock);
1326 }
1327
1328 J_ASSERT(journal->j_running_transaction == NULL);
1329 J_ASSERT(journal->j_committing_transaction == NULL);
1330 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1331 spin_unlock(&journal->j_list_lock);
1332
Dave Kleikamp470decc2006-10-11 01:20:57 -07001333 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001334 if (!is_journal_aborted(journal)) {
1335 /* We can now mark the journal as empty. */
1336 journal->j_tail = 0;
1337 journal->j_tail_sequence =
1338 ++journal->j_transaction_sequence;
1339 jbd2_journal_update_superblock(journal, 1);
1340 } else {
1341 err = -EIO;
1342 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001343 brelse(journal->j_sb_buffer);
1344 }
1345
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001346 if (journal->j_proc_entry)
1347 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001348 if (journal->j_inode)
1349 iput(journal->j_inode);
1350 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001351 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001352 kfree(journal->j_wbuf);
1353 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001354
1355 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001356}
1357
1358
1359/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001360 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001361 * @journal: Journal to check.
1362 * @compat: bitmask of compatible features
1363 * @ro: bitmask of features that force read-only mount
1364 * @incompat: bitmask of incompatible features
1365 *
1366 * Check whether the journal uses all of a given set of
1367 * features. Return true (non-zero) if it does.
1368 **/
1369
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001370int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001371 unsigned long ro, unsigned long incompat)
1372{
1373 journal_superblock_t *sb;
1374
1375 if (!compat && !ro && !incompat)
1376 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001377 /* Load journal superblock if it is not loaded yet. */
1378 if (journal->j_format_version == 0 &&
1379 journal_get_superblock(journal) != 0)
1380 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001381 if (journal->j_format_version == 1)
1382 return 0;
1383
1384 sb = journal->j_superblock;
1385
1386 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1387 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1388 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1389 return 1;
1390
1391 return 0;
1392}
1393
1394/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001395 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001396 * @journal: Journal to check.
1397 * @compat: bitmask of compatible features
1398 * @ro: bitmask of features that force read-only mount
1399 * @incompat: bitmask of incompatible features
1400 *
1401 * Check whether the journaling code supports the use of
1402 * all of a given set of features on this journal. Return true
1403 * (non-zero) if it can. */
1404
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001405int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 unsigned long ro, unsigned long incompat)
1407{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 if (!compat && !ro && !incompat)
1409 return 1;
1410
Dave Kleikamp470decc2006-10-11 01:20:57 -07001411 /* We can support any known requested features iff the
1412 * superblock is in version 2. Otherwise we fail to support any
1413 * extended sb features. */
1414
1415 if (journal->j_format_version != 2)
1416 return 0;
1417
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001418 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1419 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1420 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001421 return 1;
1422
1423 return 0;
1424}
1425
1426/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001427 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428 * @journal: Journal to act on.
1429 * @compat: bitmask of compatible features
1430 * @ro: bitmask of features that force read-only mount
1431 * @incompat: bitmask of incompatible features
1432 *
1433 * Mark a given journal feature as present on the
1434 * superblock. Returns true if the requested features could be set.
1435 *
1436 */
1437
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001438int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001439 unsigned long ro, unsigned long incompat)
1440{
1441 journal_superblock_t *sb;
1442
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001443 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001444 return 1;
1445
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001446 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001447 return 0;
1448
1449 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1450 compat, ro, incompat);
1451
1452 sb = journal->j_superblock;
1453
1454 sb->s_feature_compat |= cpu_to_be32(compat);
1455 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1456 sb->s_feature_incompat |= cpu_to_be32(incompat);
1457
1458 return 1;
1459}
1460
Girish Shilamkar818d2762008-01-28 23:58:27 -05001461/*
1462 * jbd2_journal_clear_features () - Clear a given journal feature in the
1463 * superblock
1464 * @journal: Journal to act on.
1465 * @compat: bitmask of compatible features
1466 * @ro: bitmask of features that force read-only mount
1467 * @incompat: bitmask of incompatible features
1468 *
1469 * Clear a given journal feature as present on the
1470 * superblock.
1471 */
1472void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1473 unsigned long ro, unsigned long incompat)
1474{
1475 journal_superblock_t *sb;
1476
1477 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1478 compat, ro, incompat);
1479
1480 sb = journal->j_superblock;
1481
1482 sb->s_feature_compat &= ~cpu_to_be32(compat);
1483 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1484 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1485}
1486EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487
1488/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001489 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001490 * @journal: Journal to act on.
1491 *
1492 * Given an initialised but unloaded journal struct, poke about in the
1493 * on-disk structure to update it to the most recent supported version.
1494 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001495int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001496{
1497 journal_superblock_t *sb;
1498 int err;
1499
1500 err = journal_get_superblock(journal);
1501 if (err)
1502 return err;
1503
1504 sb = journal->j_superblock;
1505
1506 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001507 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001508 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001509 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001510 return journal_convert_superblock_v1(journal, sb);
1511 default:
1512 break;
1513 }
1514 return -EINVAL;
1515}
1516
1517static int journal_convert_superblock_v1(journal_t *journal,
1518 journal_superblock_t *sb)
1519{
1520 int offset, blocksize;
1521 struct buffer_head *bh;
1522
1523 printk(KERN_WARNING
1524 "JBD: Converting superblock from version 1 to 2.\n");
1525
1526 /* Pre-initialise new fields to zero */
1527 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1528 blocksize = be32_to_cpu(sb->s_blocksize);
1529 memset(&sb->s_feature_compat, 0, blocksize-offset);
1530
1531 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001532 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001533 journal->j_format_version = 2;
1534
1535 bh = journal->j_sb_buffer;
1536 BUFFER_TRACE(bh, "marking dirty");
1537 mark_buffer_dirty(bh);
1538 sync_dirty_buffer(bh);
1539 return 0;
1540}
1541
1542
1543/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001544 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001545 * @journal: Journal to act on.
1546 *
1547 * Flush all data for a given journal to disk and empty the journal.
1548 * Filesystems can use this when remounting readonly to ensure that
1549 * recovery does not need to happen on remount.
1550 */
1551
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001552int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001553{
1554 int err = 0;
1555 transaction_t *transaction = NULL;
1556 unsigned long old_tail;
1557
Theodore Ts'oa931da62010-08-03 21:35:12 -04001558 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559
1560 /* Force everything buffered to the log... */
1561 if (journal->j_running_transaction) {
1562 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001563 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 } else if (journal->j_committing_transaction)
1565 transaction = journal->j_committing_transaction;
1566
1567 /* Wait for the log commit to complete... */
1568 if (transaction) {
1569 tid_t tid = transaction->t_tid;
1570
Theodore Ts'oa931da62010-08-03 21:35:12 -04001571 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001572 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001573 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001574 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001575 }
1576
1577 /* ...and flush everything in the log out to disk. */
1578 spin_lock(&journal->j_list_lock);
1579 while (!err && journal->j_checkpoint_transactions != NULL) {
1580 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001581 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001582 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001583 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001584 spin_lock(&journal->j_list_lock);
1585 }
1586 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001587
1588 if (is_journal_aborted(journal))
1589 return -EIO;
1590
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001591 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001592
1593 /* Finally, mark the journal as really needing no recovery.
1594 * This sets s_start==0 in the underlying superblock, which is
1595 * the magic code for a fully-recovered superblock. Any future
1596 * commits of data to the journal will restore the current
1597 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001598 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 old_tail = journal->j_tail;
1600 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001601 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001602 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001603 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001604 journal->j_tail = old_tail;
1605
1606 J_ASSERT(!journal->j_running_transaction);
1607 J_ASSERT(!journal->j_committing_transaction);
1608 J_ASSERT(!journal->j_checkpoint_transactions);
1609 J_ASSERT(journal->j_head == journal->j_tail);
1610 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001611 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001612 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001613}
1614
1615/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001616 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617 * @journal: Journal to act on.
1618 * @write: flag (see below)
1619 *
1620 * Wipe out all of the contents of a journal, safely. This will produce
1621 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001622 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001623 *
1624 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1625 * we merely suppress recovery.
1626 */
1627
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001628int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001629{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630 int err = 0;
1631
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633
1634 err = load_superblock(journal);
1635 if (err)
1636 return err;
1637
Dave Kleikamp470decc2006-10-11 01:20:57 -07001638 if (!journal->j_tail)
1639 goto no_recovery;
1640
1641 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1642 write ? "Clearing" : "Ignoring");
1643
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001644 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001646 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647
1648 no_recovery:
1649 return err;
1650}
1651
1652/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 * Journal abort has very specific semantics, which we describe
1654 * for journal abort.
1655 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001656 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001657 * itself are here.
1658 */
1659
1660/*
1661 * Quick version for internal journal use (doesn't lock the journal).
1662 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1663 * and don't attempt to make any other journal updates.
1664 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001665void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666{
1667 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001668
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001669 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670 return;
1671
1672 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001673 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674
Theodore Ts'oa931da62010-08-03 21:35:12 -04001675 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001676 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001677 transaction = journal->j_running_transaction;
1678 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001680 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001681}
1682
1683/* Soft abort: record the abort error status in the journal superblock,
1684 * but don't do any other IO. */
1685static void __journal_abort_soft (journal_t *journal, int errno)
1686{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001687 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001688 return;
1689
1690 if (!journal->j_errno)
1691 journal->j_errno = errno;
1692
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001693 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001694
1695 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001696 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001697}
1698
1699/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001700 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701 * @journal: the journal to shutdown.
1702 * @errno: an error number to record in the journal indicating
1703 * the reason for the shutdown.
1704 *
1705 * Perform a complete, immediate shutdown of the ENTIRE
1706 * journal (not of a single transaction). This operation cannot be
1707 * undone without closing and reopening the journal.
1708 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001709 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001710 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1711 * mode.
1712 *
1713 * Journal abort has very specific semantics. Any existing dirty,
1714 * unjournaled buffers in the main filesystem will still be written to
1715 * disk by bdflush, but the journaling mechanism will be suspended
1716 * immediately and no further transaction commits will be honoured.
1717 *
1718 * Any dirty, journaled buffers will be written back to disk without
1719 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1720 * filesystem, but we _do_ attempt to leave as much data as possible
1721 * behind for fsck to use for cleanup.
1722 *
1723 * Any attempt to get a new transaction handle on a journal which is in
1724 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001725 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001726 * entered abort state during the update.
1727 *
1728 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001729 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001730 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732 * which will be recorded (if possible) in the journal superblock. This
1733 * allows a client to record failure conditions in the middle of a
1734 * transaction without having to complete the transaction to record the
1735 * failure to disk. ext3_error, for example, now uses this
1736 * functionality.
1737 *
1738 * Errors which originate from within the journaling layer will NOT
1739 * supply an errno; a null errno implies that absolutely no further
1740 * writes are done to the journal (unless there are any already in
1741 * progress).
1742 *
1743 */
1744
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001745void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001746{
1747 __journal_abort_soft(journal, errno);
1748}
1749
1750/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001751 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752 * @journal: journal to examine.
1753 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001754 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001755 * time the journal was mounted - if the journal was stopped
1756 * without calling abort this will be 0.
1757 *
1758 * If the journal has been aborted on this mount time -EROFS will
1759 * be returned.
1760 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001761int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001762{
1763 int err;
1764
Theodore Ts'oa931da62010-08-03 21:35:12 -04001765 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001766 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001767 err = -EROFS;
1768 else
1769 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001770 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001771 return err;
1772}
1773
1774/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001775 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776 * @journal: journal to act on.
1777 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001778 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779 * mode.
1780 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001781int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001782{
1783 int err = 0;
1784
Theodore Ts'oa931da62010-08-03 21:35:12 -04001785 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001786 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787 err = -EROFS;
1788 else
1789 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001790 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 return err;
1792}
1793
1794/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 * @journal: journal to act on.
1797 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001798 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001799 * mode.
1800 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001801void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001803 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001805 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001806 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001807}
1808
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001809int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001810{
1811 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1812}
1813
1814/*
Zach Brownb517bea2006-10-11 01:21:08 -07001815 * helper functions to deal with 32 or 64bit block numbers.
1816 */
1817size_t journal_tag_bytes(journal_t *journal)
1818{
1819 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001820 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001821 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001822 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001823}
1824
1825/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001826 * JBD memory management
1827 *
1828 * These functions are used to allocate block-sized chunks of memory
1829 * used for making copies of buffer_head data. Very often it will be
1830 * page-sized chunks of data, but sometimes it will be in
1831 * sub-page-size chunks. (For example, 16k pages on Power systems
1832 * with a 4k block file system.) For blocks smaller than a page, we
1833 * use a SLAB allocator. There are slab caches for each block size,
1834 * which are allocated at mount time, if necessary, and we only free
1835 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1836 * this reason we don't need to a mutex to protect access to
1837 * jbd2_slab[] allocating or releasing memory; only in
1838 * jbd2_journal_create_slab().
1839 */
1840#define JBD2_MAX_SLABS 8
1841static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001842
1843static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1844 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1845 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1846};
1847
1848
1849static void jbd2_journal_destroy_slabs(void)
1850{
1851 int i;
1852
1853 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1854 if (jbd2_slab[i])
1855 kmem_cache_destroy(jbd2_slab[i]);
1856 jbd2_slab[i] = NULL;
1857 }
1858}
1859
1860static int jbd2_journal_create_slab(size_t size)
1861{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001862 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001863 int i = order_base_2(size) - 10;
1864 size_t slab_size;
1865
1866 if (size == PAGE_SIZE)
1867 return 0;
1868
1869 if (i >= JBD2_MAX_SLABS)
1870 return -EINVAL;
1871
1872 if (unlikely(i < 0))
1873 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001874 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001875 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001876 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001877 return 0; /* Already created */
1878 }
1879
1880 slab_size = 1 << (i+10);
1881 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1882 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001883 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001884 if (!jbd2_slab[i]) {
1885 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1886 return -ENOMEM;
1887 }
1888 return 0;
1889}
1890
1891static struct kmem_cache *get_slab(size_t size)
1892{
1893 int i = order_base_2(size) - 10;
1894
1895 BUG_ON(i >= JBD2_MAX_SLABS);
1896 if (unlikely(i < 0))
1897 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001898 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001899 return jbd2_slab[i];
1900}
1901
1902void *jbd2_alloc(size_t size, gfp_t flags)
1903{
1904 void *ptr;
1905
1906 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1907
1908 flags |= __GFP_REPEAT;
1909 if (size == PAGE_SIZE)
1910 ptr = (void *)__get_free_pages(flags, 0);
1911 else if (size > PAGE_SIZE) {
1912 int order = get_order(size);
1913
1914 if (order < 3)
1915 ptr = (void *)__get_free_pages(flags, order);
1916 else
1917 ptr = vmalloc(size);
1918 } else
1919 ptr = kmem_cache_alloc(get_slab(size), flags);
1920
1921 /* Check alignment; SLUB has gotten this wrong in the past,
1922 * and this can lead to user data corruption! */
1923 BUG_ON(((unsigned long) ptr) & (size-1));
1924
1925 return ptr;
1926}
1927
1928void jbd2_free(void *ptr, size_t size)
1929{
1930 if (size == PAGE_SIZE) {
1931 free_pages((unsigned long)ptr, 0);
1932 return;
1933 }
1934 if (size > PAGE_SIZE) {
1935 int order = get_order(size);
1936
1937 if (order < 3)
1938 free_pages((unsigned long)ptr, order);
1939 else
1940 vfree(ptr);
1941 return;
1942 }
1943 kmem_cache_free(get_slab(size), ptr);
1944};
1945
1946/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001947 * Journal_head storage management
1948 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001949static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001950#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001951static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1952#endif
1953
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001954static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955{
1956 int retval;
1957
Al Viro1076d172008-03-29 03:07:18 +00001958 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001959 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 sizeof(struct journal_head),
1961 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001962 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001963 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001965 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001966 retval = -ENOMEM;
1967 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1968 }
1969 return retval;
1970}
1971
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001972static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001974 if (jbd2_journal_head_cache) {
1975 kmem_cache_destroy(jbd2_journal_head_cache);
1976 jbd2_journal_head_cache = NULL;
1977 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001978}
1979
1980/*
1981 * journal_head splicing and dicing
1982 */
1983static struct journal_head *journal_alloc_journal_head(void)
1984{
1985 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001986
Jose R. Santose23291b2007-07-18 08:57:06 -04001987#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001988 atomic_inc(&nr_journal_heads);
1989#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001990 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001991 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001992 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05001993 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00001994 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001995 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001996 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 }
1998 }
1999 return ret;
2000}
2001
2002static void journal_free_journal_head(struct journal_head *jh)
2003{
Jose R. Santose23291b2007-07-18 08:57:06 -04002004#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002005 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002006 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002008 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002009}
2010
2011/*
2012 * A journal_head is attached to a buffer_head whenever JBD has an
2013 * interest in the buffer.
2014 *
2015 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2016 * is set. This bit is tested in core kernel code where we need to take
2017 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2018 * there.
2019 *
2020 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2021 *
2022 * When a buffer has its BH_JBD bit set it is immune from being released by
2023 * core kernel code, mainly via ->b_count.
2024 *
2025 * A journal_head may be detached from its buffer_head when the journal_head's
2026 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028 * journal_head can be dropped if needed.
2029 *
2030 * Various places in the kernel want to attach a journal_head to a buffer_head
2031 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002032 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002034 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002035 *
2036 * So the typical usage would be:
2037 *
2038 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002039 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002040 * ...
2041 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002042 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002043 *
2044 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2045 * because it has a non-zero b_transaction.
2046 */
2047
2048/*
2049 * Give a buffer_head a journal_head.
2050 *
2051 * Doesn't need the journal lock.
2052 * May sleep.
2053 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002054struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002055{
2056 struct journal_head *jh;
2057 struct journal_head *new_jh = NULL;
2058
2059repeat:
2060 if (!buffer_jbd(bh)) {
2061 new_jh = journal_alloc_journal_head();
2062 memset(new_jh, 0, sizeof(*new_jh));
2063 }
2064
2065 jbd_lock_bh_journal_head(bh);
2066 if (buffer_jbd(bh)) {
2067 jh = bh2jh(bh);
2068 } else {
2069 J_ASSERT_BH(bh,
2070 (atomic_read(&bh->b_count) > 0) ||
2071 (bh->b_page && bh->b_page->mapping));
2072
2073 if (!new_jh) {
2074 jbd_unlock_bh_journal_head(bh);
2075 goto repeat;
2076 }
2077
2078 jh = new_jh;
2079 new_jh = NULL; /* We consumed it */
2080 set_buffer_jbd(bh);
2081 bh->b_private = jh;
2082 jh->b_bh = bh;
2083 get_bh(bh);
2084 BUFFER_TRACE(bh, "added journal_head");
2085 }
2086 jh->b_jcount++;
2087 jbd_unlock_bh_journal_head(bh);
2088 if (new_jh)
2089 journal_free_journal_head(new_jh);
2090 return bh->b_private;
2091}
2092
2093/*
2094 * Grab a ref against this buffer_head's journal_head. If it ended up not
2095 * having a journal_head, return NULL
2096 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002097struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002098{
2099 struct journal_head *jh = NULL;
2100
2101 jbd_lock_bh_journal_head(bh);
2102 if (buffer_jbd(bh)) {
2103 jh = bh2jh(bh);
2104 jh->b_jcount++;
2105 }
2106 jbd_unlock_bh_journal_head(bh);
2107 return jh;
2108}
2109
2110static void __journal_remove_journal_head(struct buffer_head *bh)
2111{
2112 struct journal_head *jh = bh2jh(bh);
2113
2114 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2115
2116 get_bh(bh);
2117 if (jh->b_jcount == 0) {
2118 if (jh->b_transaction == NULL &&
2119 jh->b_next_transaction == NULL &&
2120 jh->b_cp_transaction == NULL) {
2121 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2122 J_ASSERT_BH(bh, buffer_jbd(bh));
2123 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2124 BUFFER_TRACE(bh, "remove journal_head");
2125 if (jh->b_frozen_data) {
2126 printk(KERN_WARNING "%s: freeing "
2127 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002128 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002129 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002130 }
2131 if (jh->b_committed_data) {
2132 printk(KERN_WARNING "%s: freeing "
2133 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002134 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002135 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002136 }
2137 bh->b_private = NULL;
2138 jh->b_bh = NULL; /* debug, really */
2139 clear_buffer_jbd(bh);
2140 __brelse(bh);
2141 journal_free_journal_head(jh);
2142 } else {
2143 BUFFER_TRACE(bh, "journal_head was locked");
2144 }
2145 }
2146}
2147
2148/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002149 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002150 * and has a zero b_jcount then remove and release its journal_head. If we did
2151 * see that the buffer is not used by any transaction we also "logically"
2152 * decrement ->b_count.
2153 *
2154 * We in fact take an additional increment on ->b_count as a convenience,
2155 * because the caller usually wants to do additional things with the bh
2156 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002157 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002158 * time. Once the caller has run __brelse(), the buffer is eligible for
2159 * reaping by try_to_free_buffers().
2160 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002161void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002162{
2163 jbd_lock_bh_journal_head(bh);
2164 __journal_remove_journal_head(bh);
2165 jbd_unlock_bh_journal_head(bh);
2166}
2167
2168/*
2169 * Drop a reference on the passed journal_head. If it fell to zero then try to
2170 * release the journal_head from the buffer_head.
2171 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002172void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002173{
2174 struct buffer_head *bh = jh2bh(jh);
2175
2176 jbd_lock_bh_journal_head(bh);
2177 J_ASSERT_JH(jh, jh->b_jcount > 0);
2178 --jh->b_jcount;
2179 if (!jh->b_jcount && !jh->b_transaction) {
2180 __journal_remove_journal_head(bh);
2181 __brelse(bh);
2182 }
2183 jbd_unlock_bh_journal_head(bh);
2184}
2185
2186/*
Jan Karac851ed52008-07-11 19:27:31 -04002187 * Initialize jbd inode head
2188 */
2189void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2190{
2191 jinode->i_transaction = NULL;
2192 jinode->i_next_transaction = NULL;
2193 jinode->i_vfs_inode = inode;
2194 jinode->i_flags = 0;
2195 INIT_LIST_HEAD(&jinode->i_list);
2196}
2197
2198/*
2199 * Function to be called before we start removing inode from memory (i.e.,
2200 * clear_inode() is a fine place to be called from). It removes inode from
2201 * transaction's lists.
2202 */
2203void jbd2_journal_release_jbd_inode(journal_t *journal,
2204 struct jbd2_inode *jinode)
2205{
Jan Karac851ed52008-07-11 19:27:31 -04002206 if (!journal)
2207 return;
2208restart:
2209 spin_lock(&journal->j_list_lock);
2210 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002211 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002212 wait_queue_head_t *wq;
2213 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2214 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2215 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2216 spin_unlock(&journal->j_list_lock);
2217 schedule();
2218 finish_wait(wq, &wait.wait);
2219 goto restart;
2220 }
2221
Jan Karac851ed52008-07-11 19:27:31 -04002222 if (jinode->i_transaction) {
2223 list_del(&jinode->i_list);
2224 jinode->i_transaction = NULL;
2225 }
2226 spin_unlock(&journal->j_list_lock);
2227}
2228
2229/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002230 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002231 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002232#ifdef CONFIG_JBD2_DEBUG
2233u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002234EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002235
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002236#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002237
Jose R. Santos6f38c742007-10-16 18:38:25 -04002238static struct dentry *jbd2_debugfs_dir;
2239static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002240
2241static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002242{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002243 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2244 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002245 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2246 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002247 jbd2_debugfs_dir,
2248 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002249}
2250
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002251static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002252{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002253 debugfs_remove(jbd2_debug);
2254 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002255}
2256
2257#else
2258
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002259static void __init jbd2_create_debugfs_entry(void)
2260{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002261}
2262
2263static void __exit jbd2_remove_debugfs_entry(void)
2264{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002265}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002266
2267#endif
2268
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002269#ifdef CONFIG_PROC_FS
2270
2271#define JBD2_STATS_PROC_NAME "fs/jbd2"
2272
2273static void __init jbd2_create_jbd_stats_proc_entry(void)
2274{
2275 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2276}
2277
2278static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2279{
2280 if (proc_jbd2_stats)
2281 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2282}
2283
2284#else
2285
2286#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2287#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2288
2289#endif
2290
Christoph Lametere18b8902006-12-06 20:33:20 -08002291struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002292
2293static int __init journal_init_handle_cache(void)
2294{
Johann Lombardia920e942006-10-11 01:21:00 -07002295 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002296 sizeof(handle_t),
2297 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002298 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002299 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002300 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002301 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2302 return -ENOMEM;
2303 }
2304 return 0;
2305}
2306
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002307static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002308{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002309 if (jbd2_handle_cache)
2310 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002311}
2312
2313/*
2314 * Module startup and shutdown
2315 */
2316
2317static int __init journal_init_caches(void)
2318{
2319 int ret;
2320
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002321 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002322 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002323 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002324 if (ret == 0)
2325 ret = journal_init_handle_cache();
2326 return ret;
2327}
2328
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002329static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002330{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002331 jbd2_journal_destroy_revoke_caches();
2332 jbd2_journal_destroy_jbd2_journal_head_cache();
2333 jbd2_journal_destroy_handle_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002334 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002335}
2336
2337static int __init journal_init(void)
2338{
2339 int ret;
2340
2341 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2342
2343 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002344 if (ret == 0) {
2345 jbd2_create_debugfs_entry();
2346 jbd2_create_jbd_stats_proc_entry();
2347 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002348 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002349 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002350 return ret;
2351}
2352
2353static void __exit journal_exit(void)
2354{
Jose R. Santose23291b2007-07-18 08:57:06 -04002355#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002356 int n = atomic_read(&nr_journal_heads);
2357 if (n)
2358 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2359#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002360 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002361 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002362 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002363}
2364
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002365/*
2366 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2367 * tracing infrastructure to map a dev_t to a device name.
2368 *
2369 * The caller should use rcu_read_lock() in order to make sure the
2370 * device name stays valid until its done with it. We use
2371 * rcu_read_lock() as well to make sure we're safe in case the caller
2372 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2373 * nested.
2374 */
2375struct devname_cache {
2376 struct rcu_head rcu;
2377 dev_t device;
2378 char devname[BDEVNAME_SIZE];
2379};
2380#define CACHE_SIZE_BITS 6
2381static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2382static DEFINE_SPINLOCK(devname_cache_lock);
2383
2384static void free_devcache(struct rcu_head *rcu)
2385{
2386 kfree(rcu);
2387}
2388
2389const char *jbd2_dev_to_name(dev_t device)
2390{
2391 int i = hash_32(device, CACHE_SIZE_BITS);
2392 char *ret;
2393 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002394 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002395
2396 rcu_read_lock();
2397 if (devcache[i] && devcache[i]->device == device) {
2398 ret = devcache[i]->devname;
2399 rcu_read_unlock();
2400 return ret;
2401 }
2402 rcu_read_unlock();
2403
Theodore Ts'ob5744802009-06-20 23:34:44 -04002404 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2405 if (!new_dev)
2406 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002407 spin_lock(&devname_cache_lock);
2408 if (devcache[i]) {
2409 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002410 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002411 ret = devcache[i]->devname;
2412 spin_unlock(&devname_cache_lock);
2413 return ret;
2414 }
2415 call_rcu(&devcache[i]->rcu, free_devcache);
2416 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002417 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002418 devcache[i]->device = device;
2419 bd = bdget(device);
2420 if (bd) {
2421 bdevname(bd, devcache[i]->devname);
2422 bdput(bd);
2423 } else
2424 __bdevname(device, devcache[i]->devname);
2425 ret = devcache[i]->devname;
2426 spin_unlock(&devname_cache_lock);
2427 return ret;
2428}
2429EXPORT_SYMBOL(jbd2_dev_to_name);
2430
Dave Kleikamp470decc2006-10-11 01:20:57 -07002431MODULE_LICENSE("GPL");
2432module_init(journal_init);
2433module_exit(journal_exit);
2434