blob: 75e1b5a0bc2defa4aac837354dcb95cd4dedd328 [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'o879c5e62009-06-17 11:47:48 -040046
47#define CREATE_TRACE_POINTS
48#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070049
50#include <asm/uaccess.h>
51#include <asm/page.h>
Brian King39e3ac22010-10-27 21:25:12 -040052#include <asm/system.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070053
Mingming Caof7f4bcc2006-10-11 01:20:59 -070054EXPORT_SYMBOL(jbd2_journal_extend);
55EXPORT_SYMBOL(jbd2_journal_stop);
56EXPORT_SYMBOL(jbd2_journal_lock_updates);
57EXPORT_SYMBOL(jbd2_journal_unlock_updates);
58EXPORT_SYMBOL(jbd2_journal_get_write_access);
59EXPORT_SYMBOL(jbd2_journal_get_create_access);
60EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070061EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070062EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
63EXPORT_SYMBOL(jbd2_journal_release_buffer);
64EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070065#if 0
66EXPORT_SYMBOL(journal_sync_buffer);
67#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070068EXPORT_SYMBOL(jbd2_journal_flush);
69EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070070
Mingming Caof7f4bcc2006-10-11 01:20:59 -070071EXPORT_SYMBOL(jbd2_journal_init_dev);
72EXPORT_SYMBOL(jbd2_journal_init_inode);
73EXPORT_SYMBOL(jbd2_journal_update_format);
74EXPORT_SYMBOL(jbd2_journal_check_used_features);
75EXPORT_SYMBOL(jbd2_journal_check_available_features);
76EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070077EXPORT_SYMBOL(jbd2_journal_load);
78EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070079EXPORT_SYMBOL(jbd2_journal_abort);
80EXPORT_SYMBOL(jbd2_journal_errno);
81EXPORT_SYMBOL(jbd2_journal_ack_err);
82EXPORT_SYMBOL(jbd2_journal_clear_err);
83EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050084EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070085EXPORT_SYMBOL(jbd2_journal_start_commit);
86EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
87EXPORT_SYMBOL(jbd2_journal_wipe);
88EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
89EXPORT_SYMBOL(jbd2_journal_invalidatepage);
90EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
91EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040092EXPORT_SYMBOL(jbd2_journal_file_inode);
93EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
94EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
95EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070096
97static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
98static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -050099static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700100
101/*
102 * Helper function used to manage commit timeouts
103 */
104
105static void commit_timeout(unsigned long __data)
106{
107 struct task_struct * p = (struct task_struct *) __data;
108
109 wake_up_process(p);
110}
111
112/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700113 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700114 * journal.
115 *
116 * This kernel thread is responsible for two things:
117 *
118 * 1) COMMIT: Every so often we need to commit the current state of the
119 * filesystem to disk. The journal thread is responsible for writing
120 * all of the metadata buffers to disk.
121 *
122 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
123 * of the data in that part of the log has been rewritten elsewhere on
124 * the disk. Flushing these old buffers to reclaim space in the log is
125 * known as checkpointing, and this thread is responsible for that job.
126 */
127
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700128static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700129{
130 journal_t *journal = arg;
131 transaction_t *transaction;
132
133 /*
134 * Set up an interval timer which can be used to trigger a commit wakeup
135 * after the commit interval expires
136 */
137 setup_timer(&journal->j_commit_timer, commit_timeout,
138 (unsigned long)current);
139
140 /* Record that the journal thread is running */
141 journal->j_task = current;
142 wake_up(&journal->j_wait_done_commit);
143
Dave Kleikamp470decc2006-10-11 01:20:57 -0700144 /*
145 * And now, wait forever for commit wakeup events.
146 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400147 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700148
149loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700150 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700151 goto end_loop;
152
153 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
154 journal->j_commit_sequence, journal->j_commit_request);
155
156 if (journal->j_commit_sequence != journal->j_commit_request) {
157 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400158 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700159 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700160 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400161 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700162 goto loop;
163 }
164
165 wake_up(&journal->j_wait_done_commit);
166 if (freezing(current)) {
167 /*
168 * The simpler the better. Flushing journal isn't a
169 * good idea, because that depends on threads that may
170 * be already stopped.
171 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700172 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400173 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700174 refrigerator();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400175 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700176 } else {
177 /*
178 * We assume on resume that commits are already there,
179 * so we don't sleep
180 */
181 DEFINE_WAIT(wait);
182 int should_sleep = 1;
183
184 prepare_to_wait(&journal->j_wait_commit, &wait,
185 TASK_INTERRUPTIBLE);
186 if (journal->j_commit_sequence != journal->j_commit_request)
187 should_sleep = 0;
188 transaction = journal->j_running_transaction;
189 if (transaction && time_after_eq(jiffies,
190 transaction->t_expires))
191 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700192 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700193 should_sleep = 0;
194 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400195 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700196 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400197 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198 }
199 finish_wait(&journal->j_wait_commit, &wait);
200 }
201
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700202 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700203
204 /*
205 * Were we woken up by a commit wakeup event?
206 */
207 transaction = journal->j_running_transaction;
208 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
209 journal->j_commit_request = transaction->t_tid;
210 jbd_debug(1, "woke because of timeout\n");
211 }
212 goto loop;
213
214end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400215 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700216 del_timer_sync(&journal->j_commit_timer);
217 journal->j_task = NULL;
218 wake_up(&journal->j_wait_done_commit);
219 jbd_debug(1, "Journal thread exiting.\n");
220 return 0;
221}
222
Pavel Emelianov97f06782007-05-08 00:30:42 -0700223static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700224{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700225 struct task_struct *t;
226
Theodore Ts'o90576c02009-09-29 15:51:30 -0400227 t = kthread_run(kjournald2, journal, "jbd2/%s",
228 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700229 if (IS_ERR(t))
230 return PTR_ERR(t);
231
Al Viro1076d172008-03-29 03:07:18 +0000232 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700233 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234}
235
236static void journal_kill_thread(journal_t *journal)
237{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400238 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700239 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700240
241 while (journal->j_task) {
242 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400243 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000244 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400245 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400247 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700248}
249
250/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700251 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700252 *
253 * Writes a metadata buffer to a given disk block. The actual IO is not
254 * performed but a new buffer_head is constructed which labels the data
255 * to be written with the correct destination disk block.
256 *
257 * Any magic-number escaping which needs to be done will cause a
258 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700259 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700260 * magic number is only written to the log for descripter blocks. In
261 * this case, we copy the data and replace the first word with 0, and we
262 * return a result code which indicates that this buffer needs to be
263 * marked as an escaped buffer in the corresponding log descriptor
264 * block. The missing word can then be restored when the block is read
265 * during recovery.
266 *
267 * If the source buffer has already been modified by a new transaction
268 * since we took the last commit snapshot, we use the frozen copy of
269 * that data for IO. If we end up using the existing buffer_head's data
270 * for the write, then we *have* to lock the buffer to prevent anyone
271 * else from using and possibly modifying it while the IO is in
272 * progress.
273 *
274 * The function returns a pointer to the buffer_heads to be used for IO.
275 *
276 * We assume that the journal has already been locked in this function.
277 *
278 * Return value:
279 * <0: Error
280 * >=0: Finished OK
281 *
282 * On success:
283 * Bit 0 set == escape performed on the data
284 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
285 */
286
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700287int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700288 struct journal_head *jh_in,
289 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700290 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700291{
292 int need_copy_out = 0;
293 int done_copy_out = 0;
294 int do_escape = 0;
295 char *mapped_data;
296 struct buffer_head *new_bh;
297 struct journal_head *new_jh;
298 struct page *new_page;
299 unsigned int new_offset;
300 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400301 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700302
303 /*
304 * The buffer really shouldn't be locked: only the current committing
305 * transaction is allowed to write it, so nobody else is allowed
306 * to do any IO.
307 *
308 * akpm: except if we're journalling data, and write() output is
309 * also part of a shared mapping, and another thread has
310 * decided to launch a writepage() against this buffer.
311 */
312 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
313
Theodore Ts'o47def822010-07-27 11:56:05 -0400314retry_alloc:
315 new_bh = alloc_buffer_head(GFP_NOFS);
316 if (!new_bh) {
317 /*
318 * Failure is not an option, but __GFP_NOFAIL is going
319 * away; so we retry ourselves here.
320 */
321 congestion_wait(BLK_RW_ASYNC, HZ/50);
322 goto retry_alloc;
323 }
324
dingdinghua96577c42009-07-13 17:55:35 -0400325 /* keep subsequent assertions sane */
326 new_bh->b_state = 0;
327 init_buffer(new_bh, NULL, NULL);
328 atomic_set(&new_bh->b_count, 1);
329 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700330
331 /*
332 * If a new transaction has already done a buffer copy-out, then
333 * we use that version of the data for the commit.
334 */
335 jbd_lock_bh_state(bh_in);
336repeat:
337 if (jh_in->b_frozen_data) {
338 done_copy_out = 1;
339 new_page = virt_to_page(jh_in->b_frozen_data);
340 new_offset = offset_in_page(jh_in->b_frozen_data);
341 } else {
342 new_page = jh2bh(jh_in)->b_page;
343 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
344 }
345
346 mapped_data = kmap_atomic(new_page, KM_USER0);
347 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200348 * Fire data frozen trigger if data already wasn't frozen. Do this
349 * before checking for escaping, as the trigger may modify the magic
350 * offset. If a copy-out happens afterwards, it will have the correct
351 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700352 */
Jan Kara13ceef02010-07-14 07:56:33 +0200353 if (!done_copy_out)
354 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
355 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700356
357 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700358 * Check for escaping
359 */
360 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700361 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700362 need_copy_out = 1;
363 do_escape = 1;
364 }
365 kunmap_atomic(mapped_data, KM_USER0);
366
367 /*
368 * Do we need to do a data copy?
369 */
370 if (need_copy_out && !done_copy_out) {
371 char *tmp;
372
373 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400374 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500375 if (!tmp) {
376 jbd2_journal_put_journal_head(new_jh);
377 return -ENOMEM;
378 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700379 jbd_lock_bh_state(bh_in);
380 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400381 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700382 goto repeat;
383 }
384
385 jh_in->b_frozen_data = tmp;
386 mapped_data = kmap_atomic(new_page, KM_USER0);
387 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
388 kunmap_atomic(mapped_data, KM_USER0);
389
390 new_page = virt_to_page(tmp);
391 new_offset = offset_in_page(tmp);
392 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700393
394 /*
395 * This isn't strictly necessary, as we're using frozen
396 * data for the escaping, but it keeps consistency with
397 * b_frozen_data usage.
398 */
399 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700400 }
401
402 /*
403 * Did we need to do an escaping? Now we've done all the
404 * copying, we can finally do so.
405 */
406 if (do_escape) {
407 mapped_data = kmap_atomic(new_page, KM_USER0);
408 *((unsigned int *)(mapped_data + new_offset)) = 0;
409 kunmap_atomic(mapped_data, KM_USER0);
410 }
411
Dave Kleikamp470decc2006-10-11 01:20:57 -0700412 set_bh_page(new_bh, new_page, new_offset);
413 new_jh->b_transaction = NULL;
414 new_bh->b_size = jh2bh(jh_in)->b_size;
415 new_bh->b_bdev = transaction->t_journal->j_dev;
416 new_bh->b_blocknr = blocknr;
417 set_buffer_mapped(new_bh);
418 set_buffer_dirty(new_bh);
419
420 *jh_out = new_jh;
421
422 /*
423 * The to-be-written buffer needs to get moved to the io queue,
424 * and the original buffer whose contents we are shadowing or
425 * copying is moved to the transaction's shadow queue.
426 */
427 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400428 spin_lock(&journal->j_list_lock);
429 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
430 spin_unlock(&journal->j_list_lock);
431 jbd_unlock_bh_state(bh_in);
432
Dave Kleikamp470decc2006-10-11 01:20:57 -0700433 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700434 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700435
436 return do_escape | (done_copy_out << 1);
437}
438
439/*
440 * Allocation code for the journal file. Manage the space left in the
441 * journal, so that we can begin checkpointing when appropriate.
442 */
443
444/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700445 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446 *
447 * Called with the journal already locked.
448 *
449 * Called under j_state_lock
450 */
451
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700452int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700453{
454 int left = journal->j_free;
455
Theodore Ts'oa931da62010-08-03 21:35:12 -0400456 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457
458 /*
459 * Be pessimistic here about the number of those free blocks which
460 * might be required for log descriptor control blocks.
461 */
462
463#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
464
465 left -= MIN_LOG_RESERVED_BLOCKS;
466
467 if (left <= 0)
468 return 0;
469 left -= (left >> 3);
470 return left;
471}
472
473/*
Jan Karac88ccea2009-02-10 11:27:46 -0500474 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700475 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477{
478 /*
479 * Are we already doing a recent enough commit?
480 */
481 if (!tid_geq(journal->j_commit_request, target)) {
482 /*
483 * We want a new commit: OK, mark the request and wakup the
484 * commit thread. We do _not_ do the commit ourselves.
485 */
486
487 journal->j_commit_request = target;
488 jbd_debug(1, "JBD: requesting commit %d/%d\n",
489 journal->j_commit_request,
490 journal->j_commit_sequence);
491 wake_up(&journal->j_wait_commit);
492 return 1;
493 }
494 return 0;
495}
496
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700497int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700498{
499 int ret;
500
Theodore Ts'oa931da62010-08-03 21:35:12 -0400501 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700502 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400503 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504 return ret;
505}
506
507/*
508 * Force and wait upon a commit if the calling process is not within
509 * transaction. This is used for forcing out undo-protected data which contains
510 * bitmaps, when the fs is running out of space.
511 *
512 * We can only force the running transaction if we don't have an active handle;
513 * otherwise, we will deadlock.
514 *
515 * Returns true if a transaction was started.
516 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700517int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700518{
519 transaction_t *transaction = NULL;
520 tid_t tid;
521
Theodore Ts'oa931da62010-08-03 21:35:12 -0400522 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523 if (journal->j_running_transaction && !current->journal_info) {
524 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700525 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 } else if (journal->j_committing_transaction)
527 transaction = journal->j_committing_transaction;
528
529 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400530 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531 return 0; /* Nothing to retry */
532 }
533
534 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400535 read_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700536 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700537 return 1;
538}
539
540/*
541 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500542 * if a transaction is going to be committed (or is currently already
543 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700545int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700546{
547 int ret = 0;
548
Theodore Ts'oa931da62010-08-03 21:35:12 -0400549 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700550 if (journal->j_running_transaction) {
551 tid_t tid = journal->j_running_transaction->t_tid;
552
Jan Karac88ccea2009-02-10 11:27:46 -0500553 __jbd2_log_start_commit(journal, tid);
554 /* There's a running transaction and we've just made sure
555 * it's commit has been scheduled. */
556 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500558 ret = 1;
559 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700560 /*
561 * If ext3_write_super() recently started a commit, then we
562 * have to wait for completion of that transaction
563 */
Jan Karac88ccea2009-02-10 11:27:46 -0500564 if (ptid)
565 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700566 ret = 1;
567 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400568 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700569 return ret;
570}
571
572/*
573 * Wait for a specified commit to complete.
574 * The caller may not hold the journal lock.
575 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700576int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700577{
578 int err = 0;
579
Theodore Ts'oa931da62010-08-03 21:35:12 -0400580 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400581#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700582 if (!tid_geq(journal->j_commit_request, tid)) {
583 printk(KERN_EMERG
584 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400585 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700588 while (tid_gt(tid, journal->j_commit_sequence)) {
589 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
590 tid, journal->j_commit_sequence);
591 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400592 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700593 wait_event(journal->j_wait_done_commit,
594 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400595 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700596 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400597 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700598
599 if (unlikely(is_journal_aborted(journal))) {
600 printk(KERN_EMERG "journal commit I/O error\n");
601 err = -EIO;
602 }
603 return err;
604}
605
606/*
607 * Log buffer allocation routines:
608 */
609
Mingming Cao18eba7a2006-10-11 01:21:13 -0700610int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700611{
612 unsigned long blocknr;
613
Theodore Ts'oa931da62010-08-03 21:35:12 -0400614 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700615 J_ASSERT(journal->j_free > 1);
616
617 blocknr = journal->j_head;
618 journal->j_head++;
619 journal->j_free--;
620 if (journal->j_head == journal->j_last)
621 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400622 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700623 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700624}
625
626/*
627 * Conversion of logical to physical block numbers for the journal
628 *
629 * On external journals the journal blocks are identity-mapped, so
630 * this is a no-op. If needed, we can use j_blk_offset - everything is
631 * ready.
632 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700633int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700634 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700635{
636 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700637 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700638
639 if (journal->j_inode) {
640 ret = bmap(journal->j_inode, blocknr);
641 if (ret)
642 *retp = ret;
643 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700644 printk(KERN_ALERT "%s: journal block not found "
645 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400646 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647 err = -EIO;
648 __journal_abort_soft(journal, err);
649 }
650 } else {
651 *retp = blocknr; /* +journal->j_blk_offset */
652 }
653 return err;
654}
655
656/*
657 * We play buffer_head aliasing tricks to write data/metadata blocks to
658 * the journal without copying their contents, but for journal
659 * descriptor blocks we do need to generate bona fide buffers.
660 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700661 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700662 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
663 * But we don't bother doing that, so there will be coherency problems with
664 * mmaps of blockdevs which hold live JBD-controlled filesystems.
665 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700666struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700667{
668 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700669 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700670 int err;
671
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700672 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700673
674 if (err)
675 return NULL;
676
677 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500678 if (!bh)
679 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700680 lock_buffer(bh);
681 memset(bh->b_data, 0, journal->j_blocksize);
682 set_buffer_uptodate(bh);
683 unlock_buffer(bh);
684 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700685 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700686}
687
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500688struct jbd2_stats_proc_session {
689 journal_t *journal;
690 struct transaction_stats_s *stats;
691 int start;
692 int max;
693};
694
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500695static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
696{
697 return *pos ? NULL : SEQ_START_TOKEN;
698}
699
700static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
701{
702 return NULL;
703}
704
705static int jbd2_seq_info_show(struct seq_file *seq, void *v)
706{
707 struct jbd2_stats_proc_session *s = seq->private;
708
709 if (v != SEQ_START_TOKEN)
710 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400711 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500712 s->stats->ts_tid,
713 s->journal->j_max_transaction_buffers);
714 if (s->stats->ts_tid == 0)
715 return 0;
716 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400717 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500718 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400719 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500720 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400721 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500722 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400723 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500724 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400725 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500726 seq_printf(seq, " %lluus average transaction commit time\n",
727 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500728 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400729 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500730 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400731 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500732 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400733 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500734 return 0;
735}
736
737static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
738{
739}
740
James Morris88e9d342009-09-22 16:43:43 -0700741static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500742 .start = jbd2_seq_info_start,
743 .next = jbd2_seq_info_next,
744 .stop = jbd2_seq_info_stop,
745 .show = jbd2_seq_info_show,
746};
747
748static int jbd2_seq_info_open(struct inode *inode, struct file *file)
749{
750 journal_t *journal = PDE(inode)->data;
751 struct jbd2_stats_proc_session *s;
752 int rc, size;
753
754 s = kmalloc(sizeof(*s), GFP_KERNEL);
755 if (s == NULL)
756 return -ENOMEM;
757 size = sizeof(struct transaction_stats_s);
758 s->stats = kmalloc(size, GFP_KERNEL);
759 if (s->stats == NULL) {
760 kfree(s);
761 return -ENOMEM;
762 }
763 spin_lock(&journal->j_history_lock);
764 memcpy(s->stats, &journal->j_stats, size);
765 s->journal = journal;
766 spin_unlock(&journal->j_history_lock);
767
768 rc = seq_open(file, &jbd2_seq_info_ops);
769 if (rc == 0) {
770 struct seq_file *m = file->private_data;
771 m->private = s;
772 } else {
773 kfree(s->stats);
774 kfree(s);
775 }
776 return rc;
777
778}
779
780static int jbd2_seq_info_release(struct inode *inode, struct file *file)
781{
782 struct seq_file *seq = file->private_data;
783 struct jbd2_stats_proc_session *s = seq->private;
784 kfree(s->stats);
785 kfree(s);
786 return seq_release(inode, file);
787}
788
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700789static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500790 .owner = THIS_MODULE,
791 .open = jbd2_seq_info_open,
792 .read = seq_read,
793 .llseek = seq_lseek,
794 .release = jbd2_seq_info_release,
795};
796
797static struct proc_dir_entry *proc_jbd2_stats;
798
799static void jbd2_stats_proc_init(journal_t *journal)
800{
Theodore Ts'o05496762008-09-16 14:36:17 -0400801 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500802 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700803 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
804 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500805 }
806}
807
808static void jbd2_stats_proc_exit(journal_t *journal)
809{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500810 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400811 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500812}
813
Dave Kleikamp470decc2006-10-11 01:20:57 -0700814/*
815 * Management for journal control blocks: functions to create and
816 * destroy journal_t structures, and to initialise and read existing
817 * journal blocks from disk. */
818
819/* First: create and setup a journal_t object in memory. We initialise
820 * very few fields yet: that has to wait until we have created the
821 * journal structures from from scratch, or loaded them from disk. */
822
823static journal_t * journal_init_common (void)
824{
825 journal_t *journal;
826 int err;
827
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500828 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700829 if (!journal)
830 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700831
832 init_waitqueue_head(&journal->j_wait_transaction_locked);
833 init_waitqueue_head(&journal->j_wait_logspace);
834 init_waitqueue_head(&journal->j_wait_done_commit);
835 init_waitqueue_head(&journal->j_wait_checkpoint);
836 init_waitqueue_head(&journal->j_wait_commit);
837 init_waitqueue_head(&journal->j_wait_updates);
838 mutex_init(&journal->j_barrier);
839 mutex_init(&journal->j_checkpoint_mutex);
840 spin_lock_init(&journal->j_revoke_lock);
841 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400842 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700843
Mingming Caocd02ff02007-10-16 18:38:25 -0400844 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500845 journal->j_min_batch_time = 0;
846 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700847
848 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700849 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700850
851 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700852 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700853 if (err) {
854 kfree(journal);
855 goto fail;
856 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500857
Theodore Ts'obf699322009-09-30 00:32:06 -0400858 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500859
Dave Kleikamp470decc2006-10-11 01:20:57 -0700860 return journal;
861fail:
862 return NULL;
863}
864
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700865/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700866 *
867 * Create a journal structure assigned some fixed set of disk blocks to
868 * the journal. We don't actually touch those disk blocks yet, but we
869 * need to set up all of the mapping information to tell the journaling
870 * system where the journal blocks are.
871 *
872 */
873
874/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400875 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700876 * @bdev: Block device on which to create the journal
877 * @fs_dev: Device which hold journalled filesystem for this journal.
878 * @start: Block nr Start of journal.
879 * @len: Length of the journal in blocks.
880 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400881 *
882 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700883 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700884 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700885 * range of blocks on an arbitrary block device.
886 *
887 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700888journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700889 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700890 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700891{
892 journal_t *journal = journal_init_common();
893 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400894 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700895 int n;
896
897 if (!journal)
898 return NULL;
899
900 /* journal descriptor can store up to n blocks -bzzz */
901 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -0500902 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700903 n = journal->j_blocksize / sizeof(journal_block_tag_t);
904 journal->j_wbufsize = n;
905 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
906 if (!journal->j_wbuf) {
907 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400908 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500909 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700910 }
911 journal->j_dev = bdev;
912 journal->j_fs_dev = fs_dev;
913 journal->j_blk_offset = start;
914 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -0400915 bdevname(journal->j_dev, journal->j_devname);
916 p = journal->j_devname;
917 while ((p = strchr(p, '/')))
918 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -0700919
920 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500921 if (!bh) {
922 printk(KERN_ERR
923 "%s: Cannot get buffer for journal superblock\n",
924 __func__);
925 goto out_err;
926 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700927 journal->j_sb_buffer = bh;
928 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500929
Dave Kleikamp470decc2006-10-11 01:20:57 -0700930 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500931out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800932 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500933 jbd2_stats_proc_exit(journal);
934 kfree(journal);
935 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700936}
937
938/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700939 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700940 * @inode: An inode to create the journal in
941 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700942 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700943 * the journal. The inode must exist already, must support bmap() and
944 * must have all data blocks preallocated.
945 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700946journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700947{
948 struct buffer_head *bh;
949 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400950 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700951 int err;
952 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700953 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700954
955 if (!journal)
956 return NULL;
957
958 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
959 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400960 bdevname(journal->j_dev, journal->j_devname);
961 p = journal->j_devname;
962 while ((p = strchr(p, '/')))
963 *p = '!';
964 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400965 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700966 jbd_debug(1,
967 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
968 journal, inode->i_sb->s_id, inode->i_ino,
969 (long long) inode->i_size,
970 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
971
972 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
973 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500974 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975
976 /* journal descriptor can store up to n blocks -bzzz */
977 n = journal->j_blocksize / sizeof(journal_block_tag_t);
978 journal->j_wbufsize = n;
979 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
980 if (!journal->j_wbuf) {
981 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400982 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500983 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700984 }
985
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700986 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700987 /* If that failed, give up */
988 if (err) {
989 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400990 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500991 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700992 }
993
994 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500995 if (!bh) {
996 printk(KERN_ERR
997 "%s: Cannot get buffer for journal superblock\n",
998 __func__);
999 goto out_err;
1000 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001001 journal->j_sb_buffer = bh;
1002 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1003
1004 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001005out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001006 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001007 jbd2_stats_proc_exit(journal);
1008 kfree(journal);
1009 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001010}
1011
1012/*
1013 * If the journal init or create aborts, we need to mark the journal
1014 * superblock as being NULL to prevent the journal destroy from writing
1015 * back a bogus superblock.
1016 */
1017static void journal_fail_superblock (journal_t *journal)
1018{
1019 struct buffer_head *bh = journal->j_sb_buffer;
1020 brelse(bh);
1021 journal->j_sb_buffer = NULL;
1022}
1023
1024/*
1025 * Given a journal_t structure, initialise the various fields for
1026 * startup of a new journaling session. We use this both when creating
1027 * a journal, and after recovering an old journal to reset it for
1028 * subsequent use.
1029 */
1030
1031static int journal_reset(journal_t *journal)
1032{
1033 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001034 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001035
1036 first = be32_to_cpu(sb->s_first);
1037 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001038 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1039 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1040 first, last);
1041 journal_fail_superblock(journal);
1042 return -EINVAL;
1043 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001044
1045 journal->j_first = first;
1046 journal->j_last = last;
1047
1048 journal->j_head = first;
1049 journal->j_tail = first;
1050 journal->j_free = last - first;
1051
1052 journal->j_tail_sequence = journal->j_transaction_sequence;
1053 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1054 journal->j_commit_request = journal->j_commit_sequence;
1055
1056 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1057
1058 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001059 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001060 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001061}
1062
1063/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001064 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001065 * @journal: The journal to update.
1066 * @wait: Set to '0' if you don't want to wait for IO completion.
1067 *
1068 * Update a journal's dynamic superblock fields and write it to disk,
1069 * optionally waiting for the IO to complete.
1070 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001071void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001072{
1073 journal_superblock_t *sb = journal->j_superblock;
1074 struct buffer_head *bh = journal->j_sb_buffer;
1075
1076 /*
1077 * As a special case, if the on-disk copy is already marked as needing
1078 * no recovery (s_start == 0) and there are no outstanding transactions
1079 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001080 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001081 * attempting a write to a potential-readonly device.
1082 */
1083 if (sb->s_start == 0 && journal->j_tail_sequence ==
1084 journal->j_transaction_sequence) {
1085 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1086 "(start %ld, seq %d, errno %d)\n",
1087 journal->j_tail, journal->j_tail_sequence,
1088 journal->j_errno);
1089 goto out;
1090 }
1091
Theodore Ts'o914258b2008-10-06 21:35:40 -04001092 if (buffer_write_io_error(bh)) {
1093 /*
1094 * Oh, dear. A previous attempt to write the journal
1095 * superblock failed. This could happen because the
1096 * USB device was yanked out. Or it could happen to
1097 * be a transient write error and maybe the block will
1098 * be remapped. Nothing we can do but to retry the
1099 * write and hope for the best.
1100 */
1101 printk(KERN_ERR "JBD2: previous I/O error detected "
1102 "for journal superblock update for %s.\n",
1103 journal->j_devname);
1104 clear_buffer_write_io_error(bh);
1105 set_buffer_uptodate(bh);
1106 }
1107
Theodore Ts'oa931da62010-08-03 21:35:12 -04001108 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001109 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1110 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1111
1112 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1113 sb->s_start = cpu_to_be32(journal->j_tail);
1114 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001115 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001116
1117 BUFFER_TRACE(bh, "marking dirty");
1118 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001119 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001120 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001121 if (buffer_write_io_error(bh)) {
1122 printk(KERN_ERR "JBD2: I/O error detected "
1123 "when updating journal superblock for %s.\n",
1124 journal->j_devname);
1125 clear_buffer_write_io_error(bh);
1126 set_buffer_uptodate(bh);
1127 }
1128 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001129 write_dirty_buffer(bh, WRITE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001130
1131out:
1132 /* If we have just flushed the log (by marking s_start==0), then
1133 * any future commit will have to be careful to update the
1134 * superblock again to re-record the true start of the log. */
1135
Theodore Ts'oa931da62010-08-03 21:35:12 -04001136 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001137 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001138 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001139 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001140 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001141 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001142}
1143
1144/*
1145 * Read the superblock for a given journal, performing initial
1146 * validation of the format.
1147 */
1148
1149static int journal_get_superblock(journal_t *journal)
1150{
1151 struct buffer_head *bh;
1152 journal_superblock_t *sb;
1153 int err = -EIO;
1154
1155 bh = journal->j_sb_buffer;
1156
1157 J_ASSERT(bh != NULL);
1158 if (!buffer_uptodate(bh)) {
1159 ll_rw_block(READ, 1, &bh);
1160 wait_on_buffer(bh);
1161 if (!buffer_uptodate(bh)) {
1162 printk (KERN_ERR
1163 "JBD: IO error reading journal superblock\n");
1164 goto out;
1165 }
1166 }
1167
1168 sb = journal->j_superblock;
1169
1170 err = -EINVAL;
1171
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001172 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001173 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1174 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1175 goto out;
1176 }
1177
1178 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001179 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001180 journal->j_format_version = 1;
1181 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001182 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001183 journal->j_format_version = 2;
1184 break;
1185 default:
1186 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1187 goto out;
1188 }
1189
1190 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1191 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1192 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1193 printk (KERN_WARNING "JBD: journal file too short\n");
1194 goto out;
1195 }
1196
1197 return 0;
1198
1199out:
1200 journal_fail_superblock(journal);
1201 return err;
1202}
1203
1204/*
1205 * Load the on-disk journal superblock and read the key fields into the
1206 * journal_t.
1207 */
1208
1209static int load_superblock(journal_t *journal)
1210{
1211 int err;
1212 journal_superblock_t *sb;
1213
1214 err = journal_get_superblock(journal);
1215 if (err)
1216 return err;
1217
1218 sb = journal->j_superblock;
1219
1220 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1221 journal->j_tail = be32_to_cpu(sb->s_start);
1222 journal->j_first = be32_to_cpu(sb->s_first);
1223 journal->j_last = be32_to_cpu(sb->s_maxlen);
1224 journal->j_errno = be32_to_cpu(sb->s_errno);
1225
1226 return 0;
1227}
1228
1229
1230/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001231 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001232 * @journal: Journal to act on.
1233 *
1234 * Given a journal_t structure which tells us which disk blocks contain
1235 * a journal, read the journal from disk to initialise the in-memory
1236 * structures.
1237 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001238int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001239{
1240 int err;
1241 journal_superblock_t *sb;
1242
1243 err = load_superblock(journal);
1244 if (err)
1245 return err;
1246
1247 sb = journal->j_superblock;
1248 /* If this is a V2 superblock, then we have to check the
1249 * features flags on it. */
1250
1251 if (journal->j_format_version >= 2) {
1252 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001253 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001255 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001256 printk (KERN_WARNING
1257 "JBD: Unrecognised features on journal\n");
1258 return -EINVAL;
1259 }
1260 }
1261
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001262 /*
1263 * Create a slab for this blocksize
1264 */
1265 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1266 if (err)
1267 return err;
1268
Dave Kleikamp470decc2006-10-11 01:20:57 -07001269 /* Let the recovery code check whether it needs to recover any
1270 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001271 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001272 goto recovery_error;
1273
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001274 if (journal->j_failed_commit) {
1275 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1276 "is corrupt.\n", journal->j_failed_commit,
1277 journal->j_devname);
1278 return -EIO;
1279 }
1280
Dave Kleikamp470decc2006-10-11 01:20:57 -07001281 /* OK, we've finished with the dynamic journal bits:
1282 * reinitialise the dynamic contents of the superblock in memory
1283 * and reset them on disk. */
1284 if (journal_reset(journal))
1285 goto recovery_error;
1286
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001287 journal->j_flags &= ~JBD2_ABORT;
1288 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289 return 0;
1290
1291recovery_error:
1292 printk (KERN_WARNING "JBD: recovery failed\n");
1293 return -EIO;
1294}
1295
1296/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001297 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001298 * @journal: Journal to act on.
1299 *
1300 * Release a journal_t structure once it is no longer in use by the
1301 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001302 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001303 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001304int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001305{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001306 int err = 0;
1307
Dave Kleikamp470decc2006-10-11 01:20:57 -07001308 /* Wait for the commit thread to wake up and die. */
1309 journal_kill_thread(journal);
1310
1311 /* Force a final log commit */
1312 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001313 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001314
1315 /* Force any old transactions to disk */
1316
1317 /* Totally anal locking here... */
1318 spin_lock(&journal->j_list_lock);
1319 while (journal->j_checkpoint_transactions != NULL) {
1320 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001321 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001322 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001323 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001324 spin_lock(&journal->j_list_lock);
1325 }
1326
1327 J_ASSERT(journal->j_running_transaction == NULL);
1328 J_ASSERT(journal->j_committing_transaction == NULL);
1329 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1330 spin_unlock(&journal->j_list_lock);
1331
Dave Kleikamp470decc2006-10-11 01:20:57 -07001332 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001333 if (!is_journal_aborted(journal)) {
1334 /* We can now mark the journal as empty. */
1335 journal->j_tail = 0;
1336 journal->j_tail_sequence =
1337 ++journal->j_transaction_sequence;
1338 jbd2_journal_update_superblock(journal, 1);
1339 } else {
1340 err = -EIO;
1341 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001342 brelse(journal->j_sb_buffer);
1343 }
1344
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001345 if (journal->j_proc_entry)
1346 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001347 if (journal->j_inode)
1348 iput(journal->j_inode);
1349 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001350 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001351 kfree(journal->j_wbuf);
1352 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001353
1354 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001355}
1356
1357
1358/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001359 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001360 * @journal: Journal to check.
1361 * @compat: bitmask of compatible features
1362 * @ro: bitmask of features that force read-only mount
1363 * @incompat: bitmask of incompatible features
1364 *
1365 * Check whether the journal uses all of a given set of
1366 * features. Return true (non-zero) if it does.
1367 **/
1368
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001369int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 unsigned long ro, unsigned long incompat)
1371{
1372 journal_superblock_t *sb;
1373
1374 if (!compat && !ro && !incompat)
1375 return 1;
1376 if (journal->j_format_version == 1)
1377 return 0;
1378
1379 sb = journal->j_superblock;
1380
1381 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1382 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1383 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1384 return 1;
1385
1386 return 0;
1387}
1388
1389/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001390 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391 * @journal: Journal to check.
1392 * @compat: bitmask of compatible features
1393 * @ro: bitmask of features that force read-only mount
1394 * @incompat: bitmask of incompatible features
1395 *
1396 * Check whether the journaling code supports the use of
1397 * all of a given set of features on this journal. Return true
1398 * (non-zero) if it can. */
1399
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001400int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001401 unsigned long ro, unsigned long incompat)
1402{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 if (!compat && !ro && !incompat)
1404 return 1;
1405
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 /* We can support any known requested features iff the
1407 * superblock is in version 2. Otherwise we fail to support any
1408 * extended sb features. */
1409
1410 if (journal->j_format_version != 2)
1411 return 0;
1412
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001413 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1414 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1415 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001416 return 1;
1417
1418 return 0;
1419}
1420
1421/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001422 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001423 * @journal: Journal to act on.
1424 * @compat: bitmask of compatible features
1425 * @ro: bitmask of features that force read-only mount
1426 * @incompat: bitmask of incompatible features
1427 *
1428 * Mark a given journal feature as present on the
1429 * superblock. Returns true if the requested features could be set.
1430 *
1431 */
1432
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001433int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001434 unsigned long ro, unsigned long incompat)
1435{
1436 journal_superblock_t *sb;
1437
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001438 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001439 return 1;
1440
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001441 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001442 return 0;
1443
1444 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1445 compat, ro, incompat);
1446
1447 sb = journal->j_superblock;
1448
1449 sb->s_feature_compat |= cpu_to_be32(compat);
1450 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1451 sb->s_feature_incompat |= cpu_to_be32(incompat);
1452
1453 return 1;
1454}
1455
Girish Shilamkar818d2762008-01-28 23:58:27 -05001456/*
1457 * jbd2_journal_clear_features () - Clear a given journal feature in the
1458 * superblock
1459 * @journal: Journal to act on.
1460 * @compat: bitmask of compatible features
1461 * @ro: bitmask of features that force read-only mount
1462 * @incompat: bitmask of incompatible features
1463 *
1464 * Clear a given journal feature as present on the
1465 * superblock.
1466 */
1467void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1468 unsigned long ro, unsigned long incompat)
1469{
1470 journal_superblock_t *sb;
1471
1472 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1473 compat, ro, incompat);
1474
1475 sb = journal->j_superblock;
1476
1477 sb->s_feature_compat &= ~cpu_to_be32(compat);
1478 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1479 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1480}
1481EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001482
1483/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001484 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001485 * @journal: Journal to act on.
1486 *
1487 * Given an initialised but unloaded journal struct, poke about in the
1488 * on-disk structure to update it to the most recent supported version.
1489 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001490int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001491{
1492 journal_superblock_t *sb;
1493 int err;
1494
1495 err = journal_get_superblock(journal);
1496 if (err)
1497 return err;
1498
1499 sb = journal->j_superblock;
1500
1501 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001504 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001505 return journal_convert_superblock_v1(journal, sb);
1506 default:
1507 break;
1508 }
1509 return -EINVAL;
1510}
1511
1512static int journal_convert_superblock_v1(journal_t *journal,
1513 journal_superblock_t *sb)
1514{
1515 int offset, blocksize;
1516 struct buffer_head *bh;
1517
1518 printk(KERN_WARNING
1519 "JBD: Converting superblock from version 1 to 2.\n");
1520
1521 /* Pre-initialise new fields to zero */
1522 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1523 blocksize = be32_to_cpu(sb->s_blocksize);
1524 memset(&sb->s_feature_compat, 0, blocksize-offset);
1525
1526 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001527 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001528 journal->j_format_version = 2;
1529
1530 bh = journal->j_sb_buffer;
1531 BUFFER_TRACE(bh, "marking dirty");
1532 mark_buffer_dirty(bh);
1533 sync_dirty_buffer(bh);
1534 return 0;
1535}
1536
1537
1538/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001539 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001540 * @journal: Journal to act on.
1541 *
1542 * Flush all data for a given journal to disk and empty the journal.
1543 * Filesystems can use this when remounting readonly to ensure that
1544 * recovery does not need to happen on remount.
1545 */
1546
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001547int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001548{
1549 int err = 0;
1550 transaction_t *transaction = NULL;
1551 unsigned long old_tail;
1552
Theodore Ts'oa931da62010-08-03 21:35:12 -04001553 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001554
1555 /* Force everything buffered to the log... */
1556 if (journal->j_running_transaction) {
1557 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001558 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559 } else if (journal->j_committing_transaction)
1560 transaction = journal->j_committing_transaction;
1561
1562 /* Wait for the log commit to complete... */
1563 if (transaction) {
1564 tid_t tid = transaction->t_tid;
1565
Theodore Ts'oa931da62010-08-03 21:35:12 -04001566 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001568 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001569 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 }
1571
1572 /* ...and flush everything in the log out to disk. */
1573 spin_lock(&journal->j_list_lock);
1574 while (!err && journal->j_checkpoint_transactions != NULL) {
1575 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001576 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001577 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001578 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001579 spin_lock(&journal->j_list_lock);
1580 }
1581 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001582
1583 if (is_journal_aborted(journal))
1584 return -EIO;
1585
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001586 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001587
1588 /* Finally, mark the journal as really needing no recovery.
1589 * This sets s_start==0 in the underlying superblock, which is
1590 * the magic code for a fully-recovered superblock. Any future
1591 * commits of data to the journal will restore the current
1592 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001593 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001594 old_tail = journal->j_tail;
1595 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001596 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001597 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001598 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 journal->j_tail = old_tail;
1600
1601 J_ASSERT(!journal->j_running_transaction);
1602 J_ASSERT(!journal->j_committing_transaction);
1603 J_ASSERT(!journal->j_checkpoint_transactions);
1604 J_ASSERT(journal->j_head == journal->j_tail);
1605 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001606 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001607 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001608}
1609
1610/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001611 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001612 * @journal: Journal to act on.
1613 * @write: flag (see below)
1614 *
1615 * Wipe out all of the contents of a journal, safely. This will produce
1616 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001617 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001618 *
1619 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1620 * we merely suppress recovery.
1621 */
1622
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001623int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001625 int err = 0;
1626
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001627 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001628
1629 err = load_superblock(journal);
1630 if (err)
1631 return err;
1632
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633 if (!journal->j_tail)
1634 goto no_recovery;
1635
1636 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1637 write ? "Clearing" : "Ignoring");
1638
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001639 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001640 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001641 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642
1643 no_recovery:
1644 return err;
1645}
1646
1647/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001648 * Journal abort has very specific semantics, which we describe
1649 * for journal abort.
1650 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001651 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001652 * itself are here.
1653 */
1654
1655/*
1656 * Quick version for internal journal use (doesn't lock the journal).
1657 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1658 * and don't attempt to make any other journal updates.
1659 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001660void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001661{
1662 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001663
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001664 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 return;
1666
1667 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001668 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001669
Theodore Ts'oa931da62010-08-03 21:35:12 -04001670 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001671 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001672 transaction = journal->j_running_transaction;
1673 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001674 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001675 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001676}
1677
1678/* Soft abort: record the abort error status in the journal superblock,
1679 * but don't do any other IO. */
1680static void __journal_abort_soft (journal_t *journal, int errno)
1681{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001682 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001683 return;
1684
1685 if (!journal->j_errno)
1686 journal->j_errno = errno;
1687
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001688 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001689
1690 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001691 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001692}
1693
1694/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001695 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001696 * @journal: the journal to shutdown.
1697 * @errno: an error number to record in the journal indicating
1698 * the reason for the shutdown.
1699 *
1700 * Perform a complete, immediate shutdown of the ENTIRE
1701 * journal (not of a single transaction). This operation cannot be
1702 * undone without closing and reopening the journal.
1703 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001704 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001705 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1706 * mode.
1707 *
1708 * Journal abort has very specific semantics. Any existing dirty,
1709 * unjournaled buffers in the main filesystem will still be written to
1710 * disk by bdflush, but the journaling mechanism will be suspended
1711 * immediately and no further transaction commits will be honoured.
1712 *
1713 * Any dirty, journaled buffers will be written back to disk without
1714 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1715 * filesystem, but we _do_ attempt to leave as much data as possible
1716 * behind for fsck to use for cleanup.
1717 *
1718 * Any attempt to get a new transaction handle on a journal which is in
1719 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001720 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001721 * entered abort state during the update.
1722 *
1723 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001724 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001725 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001726 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001727 * which will be recorded (if possible) in the journal superblock. This
1728 * allows a client to record failure conditions in the middle of a
1729 * transaction without having to complete the transaction to record the
1730 * failure to disk. ext3_error, for example, now uses this
1731 * functionality.
1732 *
1733 * Errors which originate from within the journaling layer will NOT
1734 * supply an errno; a null errno implies that absolutely no further
1735 * writes are done to the journal (unless there are any already in
1736 * progress).
1737 *
1738 */
1739
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001740void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001741{
1742 __journal_abort_soft(journal, errno);
1743}
1744
1745/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001746 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001747 * @journal: journal to examine.
1748 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001749 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750 * time the journal was mounted - if the journal was stopped
1751 * without calling abort this will be 0.
1752 *
1753 * If the journal has been aborted on this mount time -EROFS will
1754 * be returned.
1755 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001756int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001757{
1758 int err;
1759
Theodore Ts'oa931da62010-08-03 21:35:12 -04001760 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001761 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001762 err = -EROFS;
1763 else
1764 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001765 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001766 return err;
1767}
1768
1769/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001770 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001771 * @journal: journal to act on.
1772 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001773 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001774 * mode.
1775 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001776int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001777{
1778 int err = 0;
1779
Theodore Ts'oa931da62010-08-03 21:35:12 -04001780 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001781 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001782 err = -EROFS;
1783 else
1784 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001785 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001786 return err;
1787}
1788
1789/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001790 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 * @journal: journal to act on.
1792 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001793 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001794 * mode.
1795 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001796void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001797{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001798 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001799 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001800 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001801 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802}
1803
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001804int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001805{
1806 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1807}
1808
1809/*
Zach Brownb517bea2006-10-11 01:21:08 -07001810 * helper functions to deal with 32 or 64bit block numbers.
1811 */
1812size_t journal_tag_bytes(journal_t *journal)
1813{
1814 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001815 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001816 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001817 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001818}
1819
1820/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001821 * JBD memory management
1822 *
1823 * These functions are used to allocate block-sized chunks of memory
1824 * used for making copies of buffer_head data. Very often it will be
1825 * page-sized chunks of data, but sometimes it will be in
1826 * sub-page-size chunks. (For example, 16k pages on Power systems
1827 * with a 4k block file system.) For blocks smaller than a page, we
1828 * use a SLAB allocator. There are slab caches for each block size,
1829 * which are allocated at mount time, if necessary, and we only free
1830 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1831 * this reason we don't need to a mutex to protect access to
1832 * jbd2_slab[] allocating or releasing memory; only in
1833 * jbd2_journal_create_slab().
1834 */
1835#define JBD2_MAX_SLABS 8
1836static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
1837static DECLARE_MUTEX(jbd2_slab_create_sem);
1838
1839static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1840 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1841 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1842};
1843
1844
1845static void jbd2_journal_destroy_slabs(void)
1846{
1847 int i;
1848
1849 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1850 if (jbd2_slab[i])
1851 kmem_cache_destroy(jbd2_slab[i]);
1852 jbd2_slab[i] = NULL;
1853 }
1854}
1855
1856static int jbd2_journal_create_slab(size_t size)
1857{
1858 int i = order_base_2(size) - 10;
1859 size_t slab_size;
1860
1861 if (size == PAGE_SIZE)
1862 return 0;
1863
1864 if (i >= JBD2_MAX_SLABS)
1865 return -EINVAL;
1866
1867 if (unlikely(i < 0))
1868 i = 0;
1869 down(&jbd2_slab_create_sem);
1870 if (jbd2_slab[i]) {
1871 up(&jbd2_slab_create_sem);
1872 return 0; /* Already created */
1873 }
1874
1875 slab_size = 1 << (i+10);
1876 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1877 slab_size, 0, NULL);
1878 up(&jbd2_slab_create_sem);
1879 if (!jbd2_slab[i]) {
1880 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1881 return -ENOMEM;
1882 }
1883 return 0;
1884}
1885
1886static struct kmem_cache *get_slab(size_t size)
1887{
1888 int i = order_base_2(size) - 10;
1889
1890 BUG_ON(i >= JBD2_MAX_SLABS);
1891 if (unlikely(i < 0))
1892 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001893 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001894 return jbd2_slab[i];
1895}
1896
1897void *jbd2_alloc(size_t size, gfp_t flags)
1898{
1899 void *ptr;
1900
1901 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1902
1903 flags |= __GFP_REPEAT;
1904 if (size == PAGE_SIZE)
1905 ptr = (void *)__get_free_pages(flags, 0);
1906 else if (size > PAGE_SIZE) {
1907 int order = get_order(size);
1908
1909 if (order < 3)
1910 ptr = (void *)__get_free_pages(flags, order);
1911 else
1912 ptr = vmalloc(size);
1913 } else
1914 ptr = kmem_cache_alloc(get_slab(size), flags);
1915
1916 /* Check alignment; SLUB has gotten this wrong in the past,
1917 * and this can lead to user data corruption! */
1918 BUG_ON(((unsigned long) ptr) & (size-1));
1919
1920 return ptr;
1921}
1922
1923void jbd2_free(void *ptr, size_t size)
1924{
1925 if (size == PAGE_SIZE) {
1926 free_pages((unsigned long)ptr, 0);
1927 return;
1928 }
1929 if (size > PAGE_SIZE) {
1930 int order = get_order(size);
1931
1932 if (order < 3)
1933 free_pages((unsigned long)ptr, order);
1934 else
1935 vfree(ptr);
1936 return;
1937 }
1938 kmem_cache_free(get_slab(size), ptr);
1939};
1940
1941/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001942 * Journal_head storage management
1943 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001944static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001945#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001946static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1947#endif
1948
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001949static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001950{
1951 int retval;
1952
Al Viro1076d172008-03-29 03:07:18 +00001953 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001954 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955 sizeof(struct journal_head),
1956 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001957 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001958 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001959 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001960 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961 retval = -ENOMEM;
1962 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1963 }
1964 return retval;
1965}
1966
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001967static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001969 if (jbd2_journal_head_cache) {
1970 kmem_cache_destroy(jbd2_journal_head_cache);
1971 jbd2_journal_head_cache = NULL;
1972 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973}
1974
1975/*
1976 * journal_head splicing and dicing
1977 */
1978static struct journal_head *journal_alloc_journal_head(void)
1979{
1980 struct journal_head *ret;
1981 static unsigned long last_warning;
1982
Jose R. Santose23291b2007-07-18 08:57:06 -04001983#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984 atomic_inc(&nr_journal_heads);
1985#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001986 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001987 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001988 jbd_debug(1, "out of memory for journal_head\n");
1989 if (time_after(jiffies, last_warning + 5*HZ)) {
1990 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001991 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001992 last_warning = jiffies;
1993 }
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