blob: 262419f83d800bfb6e4bbfb0ca93e3af2c3f64be [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>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040045
46#define CREATE_TRACE_POINTS
47#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070048
49#include <asm/uaccess.h>
50#include <asm/page.h>
51
Mingming Caof7f4bcc2006-10-11 01:20:59 -070052EXPORT_SYMBOL(jbd2_journal_extend);
53EXPORT_SYMBOL(jbd2_journal_stop);
54EXPORT_SYMBOL(jbd2_journal_lock_updates);
55EXPORT_SYMBOL(jbd2_journal_unlock_updates);
56EXPORT_SYMBOL(jbd2_journal_get_write_access);
57EXPORT_SYMBOL(jbd2_journal_get_create_access);
58EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070059EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070060EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
61EXPORT_SYMBOL(jbd2_journal_release_buffer);
62EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070063#if 0
64EXPORT_SYMBOL(journal_sync_buffer);
65#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070066EXPORT_SYMBOL(jbd2_journal_flush);
67EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070068
Mingming Caof7f4bcc2006-10-11 01:20:59 -070069EXPORT_SYMBOL(jbd2_journal_init_dev);
70EXPORT_SYMBOL(jbd2_journal_init_inode);
71EXPORT_SYMBOL(jbd2_journal_update_format);
72EXPORT_SYMBOL(jbd2_journal_check_used_features);
73EXPORT_SYMBOL(jbd2_journal_check_available_features);
74EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070075EXPORT_SYMBOL(jbd2_journal_load);
76EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070077EXPORT_SYMBOL(jbd2_journal_abort);
78EXPORT_SYMBOL(jbd2_journal_errno);
79EXPORT_SYMBOL(jbd2_journal_ack_err);
80EXPORT_SYMBOL(jbd2_journal_clear_err);
81EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050082EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070083EXPORT_SYMBOL(jbd2_journal_start_commit);
84EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
85EXPORT_SYMBOL(jbd2_journal_wipe);
86EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
87EXPORT_SYMBOL(jbd2_journal_invalidatepage);
88EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
89EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040090EXPORT_SYMBOL(jbd2_journal_file_inode);
91EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
92EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
93EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Dave Kleikamp470decc2006-10-11 01:20:57 -070094
95static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
96static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -050097static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -070098
99/*
100 * Helper function used to manage commit timeouts
101 */
102
103static void commit_timeout(unsigned long __data)
104{
105 struct task_struct * p = (struct task_struct *) __data;
106
107 wake_up_process(p);
108}
109
110/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700111 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700112 * journal.
113 *
114 * This kernel thread is responsible for two things:
115 *
116 * 1) COMMIT: Every so often we need to commit the current state of the
117 * filesystem to disk. The journal thread is responsible for writing
118 * all of the metadata buffers to disk.
119 *
120 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
121 * of the data in that part of the log has been rewritten elsewhere on
122 * the disk. Flushing these old buffers to reclaim space in the log is
123 * known as checkpointing, and this thread is responsible for that job.
124 */
125
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700126static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700127{
128 journal_t *journal = arg;
129 transaction_t *transaction;
130
131 /*
132 * Set up an interval timer which can be used to trigger a commit wakeup
133 * after the commit interval expires
134 */
135 setup_timer(&journal->j_commit_timer, commit_timeout,
136 (unsigned long)current);
137
138 /* Record that the journal thread is running */
139 journal->j_task = current;
140 wake_up(&journal->j_wait_done_commit);
141
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142 /*
143 * And now, wait forever for commit wakeup events.
144 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400145 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146
147loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700148 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149 goto end_loop;
150
151 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
152 journal->j_commit_sequence, journal->j_commit_request);
153
154 if (journal->j_commit_sequence != journal->j_commit_request) {
155 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400156 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700157 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700158 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400159 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 goto loop;
161 }
162
163 wake_up(&journal->j_wait_done_commit);
164 if (freezing(current)) {
165 /*
166 * The simpler the better. Flushing journal isn't a
167 * good idea, because that depends on threads that may
168 * be already stopped.
169 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700170 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400171 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700172 refrigerator();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400173 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700174 } else {
175 /*
176 * We assume on resume that commits are already there,
177 * so we don't sleep
178 */
179 DEFINE_WAIT(wait);
180 int should_sleep = 1;
181
182 prepare_to_wait(&journal->j_wait_commit, &wait,
183 TASK_INTERRUPTIBLE);
184 if (journal->j_commit_sequence != journal->j_commit_request)
185 should_sleep = 0;
186 transaction = journal->j_running_transaction;
187 if (transaction && time_after_eq(jiffies,
188 transaction->t_expires))
189 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700190 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700191 should_sleep = 0;
192 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400193 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700194 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400195 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700196 }
197 finish_wait(&journal->j_wait_commit, &wait);
198 }
199
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700200 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700201
202 /*
203 * Were we woken up by a commit wakeup event?
204 */
205 transaction = journal->j_running_transaction;
206 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
207 journal->j_commit_request = transaction->t_tid;
208 jbd_debug(1, "woke because of timeout\n");
209 }
210 goto loop;
211
212end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400213 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700214 del_timer_sync(&journal->j_commit_timer);
215 journal->j_task = NULL;
216 wake_up(&journal->j_wait_done_commit);
217 jbd_debug(1, "Journal thread exiting.\n");
218 return 0;
219}
220
Pavel Emelianov97f06782007-05-08 00:30:42 -0700221static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700222{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700223 struct task_struct *t;
224
Theodore Ts'o90576c02009-09-29 15:51:30 -0400225 t = kthread_run(kjournald2, journal, "jbd2/%s",
226 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227 if (IS_ERR(t))
228 return PTR_ERR(t);
229
Al Viro1076d172008-03-29 03:07:18 +0000230 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700231 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232}
233
234static void journal_kill_thread(journal_t *journal)
235{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400236 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700237 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238
239 while (journal->j_task) {
240 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400241 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000242 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400243 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400245 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246}
247
248/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700249 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250 *
251 * Writes a metadata buffer to a given disk block. The actual IO is not
252 * performed but a new buffer_head is constructed which labels the data
253 * to be written with the correct destination disk block.
254 *
255 * Any magic-number escaping which needs to be done will cause a
256 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700257 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258 * magic number is only written to the log for descripter blocks. In
259 * this case, we copy the data and replace the first word with 0, and we
260 * return a result code which indicates that this buffer needs to be
261 * marked as an escaped buffer in the corresponding log descriptor
262 * block. The missing word can then be restored when the block is read
263 * during recovery.
264 *
265 * If the source buffer has already been modified by a new transaction
266 * since we took the last commit snapshot, we use the frozen copy of
267 * that data for IO. If we end up using the existing buffer_head's data
268 * for the write, then we *have* to lock the buffer to prevent anyone
269 * else from using and possibly modifying it while the IO is in
270 * progress.
271 *
272 * The function returns a pointer to the buffer_heads to be used for IO.
273 *
274 * We assume that the journal has already been locked in this function.
275 *
276 * Return value:
277 * <0: Error
278 * >=0: Finished OK
279 *
280 * On success:
281 * Bit 0 set == escape performed on the data
282 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
283 */
284
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700285int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286 struct journal_head *jh_in,
287 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700288 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700289{
290 int need_copy_out = 0;
291 int done_copy_out = 0;
292 int do_escape = 0;
293 char *mapped_data;
294 struct buffer_head *new_bh;
295 struct journal_head *new_jh;
296 struct page *new_page;
297 unsigned int new_offset;
298 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400299 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700300
301 /*
302 * The buffer really shouldn't be locked: only the current committing
303 * transaction is allowed to write it, so nobody else is allowed
304 * to do any IO.
305 *
306 * akpm: except if we're journalling data, and write() output is
307 * also part of a shared mapping, and another thread has
308 * decided to launch a writepage() against this buffer.
309 */
310 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
311
Theodore Ts'o47def822010-07-27 11:56:05 -0400312retry_alloc:
313 new_bh = alloc_buffer_head(GFP_NOFS);
314 if (!new_bh) {
315 /*
316 * Failure is not an option, but __GFP_NOFAIL is going
317 * away; so we retry ourselves here.
318 */
319 congestion_wait(BLK_RW_ASYNC, HZ/50);
320 goto retry_alloc;
321 }
322
dingdinghua96577c42009-07-13 17:55:35 -0400323 /* keep subsequent assertions sane */
324 new_bh->b_state = 0;
325 init_buffer(new_bh, NULL, NULL);
326 atomic_set(&new_bh->b_count, 1);
327 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700328
329 /*
330 * If a new transaction has already done a buffer copy-out, then
331 * we use that version of the data for the commit.
332 */
333 jbd_lock_bh_state(bh_in);
334repeat:
335 if (jh_in->b_frozen_data) {
336 done_copy_out = 1;
337 new_page = virt_to_page(jh_in->b_frozen_data);
338 new_offset = offset_in_page(jh_in->b_frozen_data);
339 } else {
340 new_page = jh2bh(jh_in)->b_page;
341 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
342 }
343
344 mapped_data = kmap_atomic(new_page, KM_USER0);
345 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200346 * Fire data frozen trigger if data already wasn't frozen. Do this
347 * before checking for escaping, as the trigger may modify the magic
348 * offset. If a copy-out happens afterwards, it will have the correct
349 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700350 */
Jan Kara13ceef02010-07-14 07:56:33 +0200351 if (!done_copy_out)
352 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
353 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700354
355 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700356 * Check for escaping
357 */
358 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700359 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700360 need_copy_out = 1;
361 do_escape = 1;
362 }
363 kunmap_atomic(mapped_data, KM_USER0);
364
365 /*
366 * Do we need to do a data copy?
367 */
368 if (need_copy_out && !done_copy_out) {
369 char *tmp;
370
371 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400372 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500373 if (!tmp) {
374 jbd2_journal_put_journal_head(new_jh);
375 return -ENOMEM;
376 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700377 jbd_lock_bh_state(bh_in);
378 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400379 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700380 goto repeat;
381 }
382
383 jh_in->b_frozen_data = tmp;
384 mapped_data = kmap_atomic(new_page, KM_USER0);
385 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
386 kunmap_atomic(mapped_data, KM_USER0);
387
388 new_page = virt_to_page(tmp);
389 new_offset = offset_in_page(tmp);
390 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700391
392 /*
393 * This isn't strictly necessary, as we're using frozen
394 * data for the escaping, but it keeps consistency with
395 * b_frozen_data usage.
396 */
397 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700398 }
399
400 /*
401 * Did we need to do an escaping? Now we've done all the
402 * copying, we can finally do so.
403 */
404 if (do_escape) {
405 mapped_data = kmap_atomic(new_page, KM_USER0);
406 *((unsigned int *)(mapped_data + new_offset)) = 0;
407 kunmap_atomic(mapped_data, KM_USER0);
408 }
409
Dave Kleikamp470decc2006-10-11 01:20:57 -0700410 set_bh_page(new_bh, new_page, new_offset);
411 new_jh->b_transaction = NULL;
412 new_bh->b_size = jh2bh(jh_in)->b_size;
413 new_bh->b_bdev = transaction->t_journal->j_dev;
414 new_bh->b_blocknr = blocknr;
415 set_buffer_mapped(new_bh);
416 set_buffer_dirty(new_bh);
417
418 *jh_out = new_jh;
419
420 /*
421 * The to-be-written buffer needs to get moved to the io queue,
422 * and the original buffer whose contents we are shadowing or
423 * copying is moved to the transaction's shadow queue.
424 */
425 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400426 spin_lock(&journal->j_list_lock);
427 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
428 spin_unlock(&journal->j_list_lock);
429 jbd_unlock_bh_state(bh_in);
430
Dave Kleikamp470decc2006-10-11 01:20:57 -0700431 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700432 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700433
434 return do_escape | (done_copy_out << 1);
435}
436
437/*
438 * Allocation code for the journal file. Manage the space left in the
439 * journal, so that we can begin checkpointing when appropriate.
440 */
441
442/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700443 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700444 *
445 * Called with the journal already locked.
446 *
447 * Called under j_state_lock
448 */
449
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700450int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700451{
452 int left = journal->j_free;
453
Theodore Ts'oa931da62010-08-03 21:35:12 -0400454 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700455
456 /*
457 * Be pessimistic here about the number of those free blocks which
458 * might be required for log descriptor control blocks.
459 */
460
461#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
462
463 left -= MIN_LOG_RESERVED_BLOCKS;
464
465 if (left <= 0)
466 return 0;
467 left -= (left >> 3);
468 return left;
469}
470
471/*
Jan Karac88ccea2009-02-10 11:27:46 -0500472 * Called under j_state_lock. Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700473 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700474int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700475{
476 /*
477 * Are we already doing a recent enough commit?
478 */
479 if (!tid_geq(journal->j_commit_request, target)) {
480 /*
481 * We want a new commit: OK, mark the request and wakup the
482 * commit thread. We do _not_ do the commit ourselves.
483 */
484
485 journal->j_commit_request = target;
486 jbd_debug(1, "JBD: requesting commit %d/%d\n",
487 journal->j_commit_request,
488 journal->j_commit_sequence);
489 wake_up(&journal->j_wait_commit);
490 return 1;
491 }
492 return 0;
493}
494
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700495int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496{
497 int ret;
498
Theodore Ts'oa931da62010-08-03 21:35:12 -0400499 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700500 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400501 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700502 return ret;
503}
504
505/*
506 * Force and wait upon a commit if the calling process is not within
507 * transaction. This is used for forcing out undo-protected data which contains
508 * bitmaps, when the fs is running out of space.
509 *
510 * We can only force the running transaction if we don't have an active handle;
511 * otherwise, we will deadlock.
512 *
513 * Returns true if a transaction was started.
514 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700515int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516{
517 transaction_t *transaction = NULL;
518 tid_t tid;
519
Theodore Ts'oa931da62010-08-03 21:35:12 -0400520 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700521 if (journal->j_running_transaction && !current->journal_info) {
522 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700523 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700524 } else if (journal->j_committing_transaction)
525 transaction = journal->j_committing_transaction;
526
527 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400528 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529 return 0; /* Nothing to retry */
530 }
531
532 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400533 read_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700534 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700535 return 1;
536}
537
538/*
539 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500540 * if a transaction is going to be committed (or is currently already
541 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700543int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544{
545 int ret = 0;
546
Theodore Ts'oa931da62010-08-03 21:35:12 -0400547 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700548 if (journal->j_running_transaction) {
549 tid_t tid = journal->j_running_transaction->t_tid;
550
Jan Karac88ccea2009-02-10 11:27:46 -0500551 __jbd2_log_start_commit(journal, tid);
552 /* There's a running transaction and we've just made sure
553 * it's commit has been scheduled. */
554 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500556 ret = 1;
557 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700558 /*
559 * If ext3_write_super() recently started a commit, then we
560 * have to wait for completion of that transaction
561 */
Jan Karac88ccea2009-02-10 11:27:46 -0500562 if (ptid)
563 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564 ret = 1;
565 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400566 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700567 return ret;
568}
569
570/*
571 * Wait for a specified commit to complete.
572 * The caller may not hold the journal lock.
573 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700574int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700575{
576 int err = 0;
577
Theodore Ts'oa931da62010-08-03 21:35:12 -0400578 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400579#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700580 if (!tid_geq(journal->j_commit_request, tid)) {
581 printk(KERN_EMERG
582 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400583 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700584 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700585#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586 while (tid_gt(tid, journal->j_commit_sequence)) {
587 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
588 tid, journal->j_commit_sequence);
589 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400590 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700591 wait_event(journal->j_wait_done_commit,
592 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400593 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400595 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700596
597 if (unlikely(is_journal_aborted(journal))) {
598 printk(KERN_EMERG "journal commit I/O error\n");
599 err = -EIO;
600 }
601 return err;
602}
603
604/*
605 * Log buffer allocation routines:
606 */
607
Mingming Cao18eba7a2006-10-11 01:21:13 -0700608int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700609{
610 unsigned long blocknr;
611
Theodore Ts'oa931da62010-08-03 21:35:12 -0400612 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700613 J_ASSERT(journal->j_free > 1);
614
615 blocknr = journal->j_head;
616 journal->j_head++;
617 journal->j_free--;
618 if (journal->j_head == journal->j_last)
619 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400620 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700621 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622}
623
624/*
625 * Conversion of logical to physical block numbers for the journal
626 *
627 * On external journals the journal blocks are identity-mapped, so
628 * this is a no-op. If needed, we can use j_blk_offset - everything is
629 * ready.
630 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700631int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700632 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700633{
634 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700635 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700636
637 if (journal->j_inode) {
638 ret = bmap(journal->j_inode, blocknr);
639 if (ret)
640 *retp = ret;
641 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700642 printk(KERN_ALERT "%s: journal block not found "
643 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400644 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645 err = -EIO;
646 __journal_abort_soft(journal, err);
647 }
648 } else {
649 *retp = blocknr; /* +journal->j_blk_offset */
650 }
651 return err;
652}
653
654/*
655 * We play buffer_head aliasing tricks to write data/metadata blocks to
656 * the journal without copying their contents, but for journal
657 * descriptor blocks we do need to generate bona fide buffers.
658 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700659 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700660 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
661 * But we don't bother doing that, so there will be coherency problems with
662 * mmaps of blockdevs which hold live JBD-controlled filesystems.
663 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700664struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700665{
666 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700667 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700668 int err;
669
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700670 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671
672 if (err)
673 return NULL;
674
675 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500676 if (!bh)
677 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700678 lock_buffer(bh);
679 memset(bh->b_data, 0, journal->j_blocksize);
680 set_buffer_uptodate(bh);
681 unlock_buffer(bh);
682 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700683 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700684}
685
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500686struct jbd2_stats_proc_session {
687 journal_t *journal;
688 struct transaction_stats_s *stats;
689 int start;
690 int max;
691};
692
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500693static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
694{
695 return *pos ? NULL : SEQ_START_TOKEN;
696}
697
698static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
699{
700 return NULL;
701}
702
703static int jbd2_seq_info_show(struct seq_file *seq, void *v)
704{
705 struct jbd2_stats_proc_session *s = seq->private;
706
707 if (v != SEQ_START_TOKEN)
708 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400709 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500710 s->stats->ts_tid,
711 s->journal->j_max_transaction_buffers);
712 if (s->stats->ts_tid == 0)
713 return 0;
714 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400715 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500716 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400717 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500718 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400719 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500720 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400721 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500722 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400723 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500724 seq_printf(seq, " %lluus average transaction commit time\n",
725 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500726 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400727 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500728 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400729 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500730 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400731 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500732 return 0;
733}
734
735static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
736{
737}
738
James Morris88e9d342009-09-22 16:43:43 -0700739static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500740 .start = jbd2_seq_info_start,
741 .next = jbd2_seq_info_next,
742 .stop = jbd2_seq_info_stop,
743 .show = jbd2_seq_info_show,
744};
745
746static int jbd2_seq_info_open(struct inode *inode, struct file *file)
747{
748 journal_t *journal = PDE(inode)->data;
749 struct jbd2_stats_proc_session *s;
750 int rc, size;
751
752 s = kmalloc(sizeof(*s), GFP_KERNEL);
753 if (s == NULL)
754 return -ENOMEM;
755 size = sizeof(struct transaction_stats_s);
756 s->stats = kmalloc(size, GFP_KERNEL);
757 if (s->stats == NULL) {
758 kfree(s);
759 return -ENOMEM;
760 }
761 spin_lock(&journal->j_history_lock);
762 memcpy(s->stats, &journal->j_stats, size);
763 s->journal = journal;
764 spin_unlock(&journal->j_history_lock);
765
766 rc = seq_open(file, &jbd2_seq_info_ops);
767 if (rc == 0) {
768 struct seq_file *m = file->private_data;
769 m->private = s;
770 } else {
771 kfree(s->stats);
772 kfree(s);
773 }
774 return rc;
775
776}
777
778static int jbd2_seq_info_release(struct inode *inode, struct file *file)
779{
780 struct seq_file *seq = file->private_data;
781 struct jbd2_stats_proc_session *s = seq->private;
782 kfree(s->stats);
783 kfree(s);
784 return seq_release(inode, file);
785}
786
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700787static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500788 .owner = THIS_MODULE,
789 .open = jbd2_seq_info_open,
790 .read = seq_read,
791 .llseek = seq_lseek,
792 .release = jbd2_seq_info_release,
793};
794
795static struct proc_dir_entry *proc_jbd2_stats;
796
797static void jbd2_stats_proc_init(journal_t *journal)
798{
Theodore Ts'o05496762008-09-16 14:36:17 -0400799 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500800 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700801 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
802 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500803 }
804}
805
806static void jbd2_stats_proc_exit(journal_t *journal)
807{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500808 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400809 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500810}
811
Dave Kleikamp470decc2006-10-11 01:20:57 -0700812/*
813 * Management for journal control blocks: functions to create and
814 * destroy journal_t structures, and to initialise and read existing
815 * journal blocks from disk. */
816
817/* First: create and setup a journal_t object in memory. We initialise
818 * very few fields yet: that has to wait until we have created the
819 * journal structures from from scratch, or loaded them from disk. */
820
821static journal_t * journal_init_common (void)
822{
823 journal_t *journal;
824 int err;
825
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500826 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700827 if (!journal)
828 goto fail;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700829
830 init_waitqueue_head(&journal->j_wait_transaction_locked);
831 init_waitqueue_head(&journal->j_wait_logspace);
832 init_waitqueue_head(&journal->j_wait_done_commit);
833 init_waitqueue_head(&journal->j_wait_checkpoint);
834 init_waitqueue_head(&journal->j_wait_commit);
835 init_waitqueue_head(&journal->j_wait_updates);
836 mutex_init(&journal->j_barrier);
837 mutex_init(&journal->j_checkpoint_mutex);
838 spin_lock_init(&journal->j_revoke_lock);
839 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400840 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700841
Mingming Caocd02ff02007-10-16 18:38:25 -0400842 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500843 journal->j_min_batch_time = 0;
844 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700845
846 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700847 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700848
849 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700850 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700851 if (err) {
852 kfree(journal);
853 goto fail;
854 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500855
Theodore Ts'obf699322009-09-30 00:32:06 -0400856 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500857
Dave Kleikamp470decc2006-10-11 01:20:57 -0700858 return journal;
859fail:
860 return NULL;
861}
862
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700863/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700864 *
865 * Create a journal structure assigned some fixed set of disk blocks to
866 * the journal. We don't actually touch those disk blocks yet, but we
867 * need to set up all of the mapping information to tell the journaling
868 * system where the journal blocks are.
869 *
870 */
871
872/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400873 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700874 * @bdev: Block device on which to create the journal
875 * @fs_dev: Device which hold journalled filesystem for this journal.
876 * @start: Block nr Start of journal.
877 * @len: Length of the journal in blocks.
878 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400879 *
880 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700881 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700882 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700883 * range of blocks on an arbitrary block device.
884 *
885 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700886journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700887 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700888 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700889{
890 journal_t *journal = journal_init_common();
891 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400892 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700893 int n;
894
895 if (!journal)
896 return NULL;
897
898 /* journal descriptor can store up to n blocks -bzzz */
899 journal->j_blocksize = blocksize;
Jan Kara4b905672009-01-06 14:53:35 -0500900 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700901 n = journal->j_blocksize / sizeof(journal_block_tag_t);
902 journal->j_wbufsize = n;
903 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
904 if (!journal->j_wbuf) {
905 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400906 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500907 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700908 }
909 journal->j_dev = bdev;
910 journal->j_fs_dev = fs_dev;
911 journal->j_blk_offset = start;
912 journal->j_maxlen = len;
Theodore Ts'o05496762008-09-16 14:36:17 -0400913 bdevname(journal->j_dev, journal->j_devname);
914 p = journal->j_devname;
915 while ((p = strchr(p, '/')))
916 *p = '!';
Dave Kleikamp470decc2006-10-11 01:20:57 -0700917
918 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500919 if (!bh) {
920 printk(KERN_ERR
921 "%s: Cannot get buffer for journal superblock\n",
922 __func__);
923 goto out_err;
924 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700925 journal->j_sb_buffer = bh;
926 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500927
Dave Kleikamp470decc2006-10-11 01:20:57 -0700928 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500929out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800930 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500931 jbd2_stats_proc_exit(journal);
932 kfree(journal);
933 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700934}
935
936/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700937 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700938 * @inode: An inode to create the journal in
939 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700940 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -0700941 * the journal. The inode must exist already, must support bmap() and
942 * must have all data blocks preallocated.
943 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700944journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945{
946 struct buffer_head *bh;
947 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -0400948 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700949 int err;
950 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700951 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700952
953 if (!journal)
954 return NULL;
955
956 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
957 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -0400958 bdevname(journal->j_dev, journal->j_devname);
959 p = journal->j_devname;
960 while ((p = strchr(p, '/')))
961 *p = '!';
962 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -0400963 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700964 jbd_debug(1,
965 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
966 journal, inode->i_sb->s_id, inode->i_ino,
967 (long long) inode->i_size,
968 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
969
970 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
971 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500972 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700973
974 /* journal descriptor can store up to n blocks -bzzz */
975 n = journal->j_blocksize / sizeof(journal_block_tag_t);
976 journal->j_wbufsize = n;
977 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
978 if (!journal->j_wbuf) {
979 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400980 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500981 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700982 }
983
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700984 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700985 /* If that failed, give up */
986 if (err) {
987 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400988 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500989 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700990 }
991
992 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500993 if (!bh) {
994 printk(KERN_ERR
995 "%s: Cannot get buffer for journal superblock\n",
996 __func__);
997 goto out_err;
998 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700999 journal->j_sb_buffer = bh;
1000 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1001
1002 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001003out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001004 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001005 jbd2_stats_proc_exit(journal);
1006 kfree(journal);
1007 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001008}
1009
1010/*
1011 * If the journal init or create aborts, we need to mark the journal
1012 * superblock as being NULL to prevent the journal destroy from writing
1013 * back a bogus superblock.
1014 */
1015static void journal_fail_superblock (journal_t *journal)
1016{
1017 struct buffer_head *bh = journal->j_sb_buffer;
1018 brelse(bh);
1019 journal->j_sb_buffer = NULL;
1020}
1021
1022/*
1023 * Given a journal_t structure, initialise the various fields for
1024 * startup of a new journaling session. We use this both when creating
1025 * a journal, and after recovering an old journal to reset it for
1026 * subsequent use.
1027 */
1028
1029static int journal_reset(journal_t *journal)
1030{
1031 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001032 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001033
1034 first = be32_to_cpu(sb->s_first);
1035 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001036 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1037 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1038 first, last);
1039 journal_fail_superblock(journal);
1040 return -EINVAL;
1041 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001042
1043 journal->j_first = first;
1044 journal->j_last = last;
1045
1046 journal->j_head = first;
1047 journal->j_tail = first;
1048 journal->j_free = last - first;
1049
1050 journal->j_tail_sequence = journal->j_transaction_sequence;
1051 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1052 journal->j_commit_request = journal->j_commit_sequence;
1053
1054 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1055
1056 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001057 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001058 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001059}
1060
1061/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001062 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063 * @journal: The journal to update.
1064 * @wait: Set to '0' if you don't want to wait for IO completion.
1065 *
1066 * Update a journal's dynamic superblock fields and write it to disk,
1067 * optionally waiting for the IO to complete.
1068 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001069void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070{
1071 journal_superblock_t *sb = journal->j_superblock;
1072 struct buffer_head *bh = journal->j_sb_buffer;
1073
1074 /*
1075 * As a special case, if the on-disk copy is already marked as needing
1076 * no recovery (s_start == 0) and there are no outstanding transactions
1077 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001078 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079 * attempting a write to a potential-readonly device.
1080 */
1081 if (sb->s_start == 0 && journal->j_tail_sequence ==
1082 journal->j_transaction_sequence) {
1083 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1084 "(start %ld, seq %d, errno %d)\n",
1085 journal->j_tail, journal->j_tail_sequence,
1086 journal->j_errno);
1087 goto out;
1088 }
1089
Theodore Ts'o914258b2008-10-06 21:35:40 -04001090 if (buffer_write_io_error(bh)) {
1091 /*
1092 * Oh, dear. A previous attempt to write the journal
1093 * superblock failed. This could happen because the
1094 * USB device was yanked out. Or it could happen to
1095 * be a transient write error and maybe the block will
1096 * be remapped. Nothing we can do but to retry the
1097 * write and hope for the best.
1098 */
1099 printk(KERN_ERR "JBD2: previous I/O error detected "
1100 "for journal superblock update for %s.\n",
1101 journal->j_devname);
1102 clear_buffer_write_io_error(bh);
1103 set_buffer_uptodate(bh);
1104 }
1105
Theodore Ts'oa931da62010-08-03 21:35:12 -04001106 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001107 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1108 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1109
1110 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1111 sb->s_start = cpu_to_be32(journal->j_tail);
1112 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001113 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001114
1115 BUFFER_TRACE(bh, "marking dirty");
1116 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001117 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001118 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001119 if (buffer_write_io_error(bh)) {
1120 printk(KERN_ERR "JBD2: I/O error detected "
1121 "when updating journal superblock for %s.\n",
1122 journal->j_devname);
1123 clear_buffer_write_io_error(bh);
1124 set_buffer_uptodate(bh);
1125 }
1126 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001127 write_dirty_buffer(bh, WRITE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001128
1129out:
1130 /* If we have just flushed the log (by marking s_start==0), then
1131 * any future commit will have to be careful to update the
1132 * superblock again to re-record the true start of the log. */
1133
Theodore Ts'oa931da62010-08-03 21:35:12 -04001134 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001135 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001136 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001137 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001138 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001139 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001140}
1141
1142/*
1143 * Read the superblock for a given journal, performing initial
1144 * validation of the format.
1145 */
1146
1147static int journal_get_superblock(journal_t *journal)
1148{
1149 struct buffer_head *bh;
1150 journal_superblock_t *sb;
1151 int err = -EIO;
1152
1153 bh = journal->j_sb_buffer;
1154
1155 J_ASSERT(bh != NULL);
1156 if (!buffer_uptodate(bh)) {
1157 ll_rw_block(READ, 1, &bh);
1158 wait_on_buffer(bh);
1159 if (!buffer_uptodate(bh)) {
1160 printk (KERN_ERR
1161 "JBD: IO error reading journal superblock\n");
1162 goto out;
1163 }
1164 }
1165
1166 sb = journal->j_superblock;
1167
1168 err = -EINVAL;
1169
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001170 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001171 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1172 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1173 goto out;
1174 }
1175
1176 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001177 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001178 journal->j_format_version = 1;
1179 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001180 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001181 journal->j_format_version = 2;
1182 break;
1183 default:
1184 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1185 goto out;
1186 }
1187
1188 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1189 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1190 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1191 printk (KERN_WARNING "JBD: journal file too short\n");
1192 goto out;
1193 }
1194
1195 return 0;
1196
1197out:
1198 journal_fail_superblock(journal);
1199 return err;
1200}
1201
1202/*
1203 * Load the on-disk journal superblock and read the key fields into the
1204 * journal_t.
1205 */
1206
1207static int load_superblock(journal_t *journal)
1208{
1209 int err;
1210 journal_superblock_t *sb;
1211
1212 err = journal_get_superblock(journal);
1213 if (err)
1214 return err;
1215
1216 sb = journal->j_superblock;
1217
1218 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1219 journal->j_tail = be32_to_cpu(sb->s_start);
1220 journal->j_first = be32_to_cpu(sb->s_first);
1221 journal->j_last = be32_to_cpu(sb->s_maxlen);
1222 journal->j_errno = be32_to_cpu(sb->s_errno);
1223
1224 return 0;
1225}
1226
1227
1228/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001229 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001230 * @journal: Journal to act on.
1231 *
1232 * Given a journal_t structure which tells us which disk blocks contain
1233 * a journal, read the journal from disk to initialise the in-memory
1234 * structures.
1235 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001236int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001237{
1238 int err;
1239 journal_superblock_t *sb;
1240
1241 err = load_superblock(journal);
1242 if (err)
1243 return err;
1244
1245 sb = journal->j_superblock;
1246 /* If this is a V2 superblock, then we have to check the
1247 * features flags on it. */
1248
1249 if (journal->j_format_version >= 2) {
1250 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001251 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001252 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001253 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 printk (KERN_WARNING
1255 "JBD: Unrecognised features on journal\n");
1256 return -EINVAL;
1257 }
1258 }
1259
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001260 /*
1261 * Create a slab for this blocksize
1262 */
1263 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1264 if (err)
1265 return err;
1266
Dave Kleikamp470decc2006-10-11 01:20:57 -07001267 /* Let the recovery code check whether it needs to recover any
1268 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001269 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001270 goto recovery_error;
1271
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001272 if (journal->j_failed_commit) {
1273 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1274 "is corrupt.\n", journal->j_failed_commit,
1275 journal->j_devname);
1276 return -EIO;
1277 }
1278
Dave Kleikamp470decc2006-10-11 01:20:57 -07001279 /* OK, we've finished with the dynamic journal bits:
1280 * reinitialise the dynamic contents of the superblock in memory
1281 * and reset them on disk. */
1282 if (journal_reset(journal))
1283 goto recovery_error;
1284
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001285 journal->j_flags &= ~JBD2_ABORT;
1286 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001287 return 0;
1288
1289recovery_error:
1290 printk (KERN_WARNING "JBD: recovery failed\n");
1291 return -EIO;
1292}
1293
1294/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001295 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001296 * @journal: Journal to act on.
1297 *
1298 * Release a journal_t structure once it is no longer in use by the
1299 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001300 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001301 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001302int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001303{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001304 int err = 0;
1305
Dave Kleikamp470decc2006-10-11 01:20:57 -07001306 /* Wait for the commit thread to wake up and die. */
1307 journal_kill_thread(journal);
1308
1309 /* Force a final log commit */
1310 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001311 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001312
1313 /* Force any old transactions to disk */
1314
1315 /* Totally anal locking here... */
1316 spin_lock(&journal->j_list_lock);
1317 while (journal->j_checkpoint_transactions != NULL) {
1318 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001319 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001320 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001321 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001322 spin_lock(&journal->j_list_lock);
1323 }
1324
1325 J_ASSERT(journal->j_running_transaction == NULL);
1326 J_ASSERT(journal->j_committing_transaction == NULL);
1327 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1328 spin_unlock(&journal->j_list_lock);
1329
Dave Kleikamp470decc2006-10-11 01:20:57 -07001330 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001331 if (!is_journal_aborted(journal)) {
1332 /* We can now mark the journal as empty. */
1333 journal->j_tail = 0;
1334 journal->j_tail_sequence =
1335 ++journal->j_transaction_sequence;
1336 jbd2_journal_update_superblock(journal, 1);
1337 } else {
1338 err = -EIO;
1339 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001340 brelse(journal->j_sb_buffer);
1341 }
1342
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001343 if (journal->j_proc_entry)
1344 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001345 if (journal->j_inode)
1346 iput(journal->j_inode);
1347 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001348 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001349 kfree(journal->j_wbuf);
1350 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001351
1352 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001353}
1354
1355
1356/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001357 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001358 * @journal: Journal to check.
1359 * @compat: bitmask of compatible features
1360 * @ro: bitmask of features that force read-only mount
1361 * @incompat: bitmask of incompatible features
1362 *
1363 * Check whether the journal uses all of a given set of
1364 * features. Return true (non-zero) if it does.
1365 **/
1366
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001367int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001368 unsigned long ro, unsigned long incompat)
1369{
1370 journal_superblock_t *sb;
1371
1372 if (!compat && !ro && !incompat)
1373 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001374 /* Load journal superblock if it is not loaded yet. */
1375 if (journal->j_format_version == 0 &&
1376 journal_get_superblock(journal) != 0)
1377 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001378 if (journal->j_format_version == 1)
1379 return 0;
1380
1381 sb = journal->j_superblock;
1382
1383 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1384 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1385 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1386 return 1;
1387
1388 return 0;
1389}
1390
1391/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001392 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001393 * @journal: Journal to check.
1394 * @compat: bitmask of compatible features
1395 * @ro: bitmask of features that force read-only mount
1396 * @incompat: bitmask of incompatible features
1397 *
1398 * Check whether the journaling code supports the use of
1399 * all of a given set of features on this journal. Return true
1400 * (non-zero) if it can. */
1401
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001402int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 unsigned long ro, unsigned long incompat)
1404{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001405 if (!compat && !ro && !incompat)
1406 return 1;
1407
Dave Kleikamp470decc2006-10-11 01:20:57 -07001408 /* We can support any known requested features iff the
1409 * superblock is in version 2. Otherwise we fail to support any
1410 * extended sb features. */
1411
1412 if (journal->j_format_version != 2)
1413 return 0;
1414
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001415 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1416 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1417 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001418 return 1;
1419
1420 return 0;
1421}
1422
1423/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001424 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001425 * @journal: Journal to act on.
1426 * @compat: bitmask of compatible features
1427 * @ro: bitmask of features that force read-only mount
1428 * @incompat: bitmask of incompatible features
1429 *
1430 * Mark a given journal feature as present on the
1431 * superblock. Returns true if the requested features could be set.
1432 *
1433 */
1434
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001435int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436 unsigned long ro, unsigned long incompat)
1437{
1438 journal_superblock_t *sb;
1439
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001440 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 return 1;
1442
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001443 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001444 return 0;
1445
1446 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1447 compat, ro, incompat);
1448
1449 sb = journal->j_superblock;
1450
1451 sb->s_feature_compat |= cpu_to_be32(compat);
1452 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1453 sb->s_feature_incompat |= cpu_to_be32(incompat);
1454
1455 return 1;
1456}
1457
Girish Shilamkar818d2762008-01-28 23:58:27 -05001458/*
1459 * jbd2_journal_clear_features () - Clear a given journal feature in the
1460 * superblock
1461 * @journal: Journal to act on.
1462 * @compat: bitmask of compatible features
1463 * @ro: bitmask of features that force read-only mount
1464 * @incompat: bitmask of incompatible features
1465 *
1466 * Clear a given journal feature as present on the
1467 * superblock.
1468 */
1469void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1470 unsigned long ro, unsigned long incompat)
1471{
1472 journal_superblock_t *sb;
1473
1474 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1475 compat, ro, incompat);
1476
1477 sb = journal->j_superblock;
1478
1479 sb->s_feature_compat &= ~cpu_to_be32(compat);
1480 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1481 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1482}
1483EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001484
1485/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001486 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487 * @journal: Journal to act on.
1488 *
1489 * Given an initialised but unloaded journal struct, poke about in the
1490 * on-disk structure to update it to the most recent supported version.
1491 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001492int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001493{
1494 journal_superblock_t *sb;
1495 int err;
1496
1497 err = journal_get_superblock(journal);
1498 if (err)
1499 return err;
1500
1501 sb = journal->j_superblock;
1502
1503 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001504 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001505 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001506 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001507 return journal_convert_superblock_v1(journal, sb);
1508 default:
1509 break;
1510 }
1511 return -EINVAL;
1512}
1513
1514static int journal_convert_superblock_v1(journal_t *journal,
1515 journal_superblock_t *sb)
1516{
1517 int offset, blocksize;
1518 struct buffer_head *bh;
1519
1520 printk(KERN_WARNING
1521 "JBD: Converting superblock from version 1 to 2.\n");
1522
1523 /* Pre-initialise new fields to zero */
1524 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1525 blocksize = be32_to_cpu(sb->s_blocksize);
1526 memset(&sb->s_feature_compat, 0, blocksize-offset);
1527
1528 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001529 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001530 journal->j_format_version = 2;
1531
1532 bh = journal->j_sb_buffer;
1533 BUFFER_TRACE(bh, "marking dirty");
1534 mark_buffer_dirty(bh);
1535 sync_dirty_buffer(bh);
1536 return 0;
1537}
1538
1539
1540/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001541 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001542 * @journal: Journal to act on.
1543 *
1544 * Flush all data for a given journal to disk and empty the journal.
1545 * Filesystems can use this when remounting readonly to ensure that
1546 * recovery does not need to happen on remount.
1547 */
1548
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001549int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001550{
1551 int err = 0;
1552 transaction_t *transaction = NULL;
1553 unsigned long old_tail;
1554
Theodore Ts'oa931da62010-08-03 21:35:12 -04001555 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001556
1557 /* Force everything buffered to the log... */
1558 if (journal->j_running_transaction) {
1559 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001560 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001561 } else if (journal->j_committing_transaction)
1562 transaction = journal->j_committing_transaction;
1563
1564 /* Wait for the log commit to complete... */
1565 if (transaction) {
1566 tid_t tid = transaction->t_tid;
1567
Theodore Ts'oa931da62010-08-03 21:35:12 -04001568 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001569 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001571 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572 }
1573
1574 /* ...and flush everything in the log out to disk. */
1575 spin_lock(&journal->j_list_lock);
1576 while (!err && journal->j_checkpoint_transactions != NULL) {
1577 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001578 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001579 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001580 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001581 spin_lock(&journal->j_list_lock);
1582 }
1583 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001584
1585 if (is_journal_aborted(journal))
1586 return -EIO;
1587
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001588 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001589
1590 /* Finally, mark the journal as really needing no recovery.
1591 * This sets s_start==0 in the underlying superblock, which is
1592 * the magic code for a fully-recovered superblock. Any future
1593 * commits of data to the journal will restore the current
1594 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001595 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596 old_tail = journal->j_tail;
1597 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001598 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001599 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001600 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 journal->j_tail = old_tail;
1602
1603 J_ASSERT(!journal->j_running_transaction);
1604 J_ASSERT(!journal->j_committing_transaction);
1605 J_ASSERT(!journal->j_checkpoint_transactions);
1606 J_ASSERT(journal->j_head == journal->j_tail);
1607 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001608 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001609 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001610}
1611
1612/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001613 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614 * @journal: Journal to act on.
1615 * @write: flag (see below)
1616 *
1617 * Wipe out all of the contents of a journal, safely. This will produce
1618 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001619 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620 *
1621 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1622 * we merely suppress recovery.
1623 */
1624
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001625int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001626{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627 int err = 0;
1628
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001629 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630
1631 err = load_superblock(journal);
1632 if (err)
1633 return err;
1634
Dave Kleikamp470decc2006-10-11 01:20:57 -07001635 if (!journal->j_tail)
1636 goto no_recovery;
1637
1638 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1639 write ? "Clearing" : "Ignoring");
1640
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001641 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001643 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644
1645 no_recovery:
1646 return err;
1647}
1648
1649/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 * Journal abort has very specific semantics, which we describe
1651 * for journal abort.
1652 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001653 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001654 * itself are here.
1655 */
1656
1657/*
1658 * Quick version for internal journal use (doesn't lock the journal).
1659 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1660 * and don't attempt to make any other journal updates.
1661 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001662void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001663{
1664 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001666 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001667 return;
1668
1669 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001670 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671
Theodore Ts'oa931da62010-08-03 21:35:12 -04001672 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001673 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 transaction = journal->j_running_transaction;
1675 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001676 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001677 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001678}
1679
1680/* Soft abort: record the abort error status in the journal superblock,
1681 * but don't do any other IO. */
1682static void __journal_abort_soft (journal_t *journal, int errno)
1683{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001684 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001685 return;
1686
1687 if (!journal->j_errno)
1688 journal->j_errno = errno;
1689
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001690 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001691
1692 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001693 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001694}
1695
1696/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001697 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001698 * @journal: the journal to shutdown.
1699 * @errno: an error number to record in the journal indicating
1700 * the reason for the shutdown.
1701 *
1702 * Perform a complete, immediate shutdown of the ENTIRE
1703 * journal (not of a single transaction). This operation cannot be
1704 * undone without closing and reopening the journal.
1705 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001706 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001707 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1708 * mode.
1709 *
1710 * Journal abort has very specific semantics. Any existing dirty,
1711 * unjournaled buffers in the main filesystem will still be written to
1712 * disk by bdflush, but the journaling mechanism will be suspended
1713 * immediately and no further transaction commits will be honoured.
1714 *
1715 * Any dirty, journaled buffers will be written back to disk without
1716 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1717 * filesystem, but we _do_ attempt to leave as much data as possible
1718 * behind for fsck to use for cleanup.
1719 *
1720 * Any attempt to get a new transaction handle on a journal which is in
1721 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001722 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001723 * entered abort state during the update.
1724 *
1725 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001726 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001727 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001728 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729 * which will be recorded (if possible) in the journal superblock. This
1730 * allows a client to record failure conditions in the middle of a
1731 * transaction without having to complete the transaction to record the
1732 * failure to disk. ext3_error, for example, now uses this
1733 * functionality.
1734 *
1735 * Errors which originate from within the journaling layer will NOT
1736 * supply an errno; a null errno implies that absolutely no further
1737 * writes are done to the journal (unless there are any already in
1738 * progress).
1739 *
1740 */
1741
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001742void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001743{
1744 __journal_abort_soft(journal, errno);
1745}
1746
1747/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001748 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 * @journal: journal to examine.
1750 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001751 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752 * time the journal was mounted - if the journal was stopped
1753 * without calling abort this will be 0.
1754 *
1755 * If the journal has been aborted on this mount time -EROFS will
1756 * be returned.
1757 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001758int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001759{
1760 int err;
1761
Theodore Ts'oa931da62010-08-03 21:35:12 -04001762 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001763 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001764 err = -EROFS;
1765 else
1766 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001767 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001768 return err;
1769}
1770
1771/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001772 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001773 * @journal: journal to act on.
1774 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001775 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776 * mode.
1777 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001778int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779{
1780 int err = 0;
1781
Theodore Ts'oa931da62010-08-03 21:35:12 -04001782 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001783 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 err = -EROFS;
1785 else
1786 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001787 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001788 return err;
1789}
1790
1791/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001792 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 * @journal: journal to act on.
1794 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001795 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 * mode.
1797 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001798void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001799{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001800 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001801 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001802 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001803 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804}
1805
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001806int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001807{
1808 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1809}
1810
1811/*
Zach Brownb517bea2006-10-11 01:21:08 -07001812 * helper functions to deal with 32 or 64bit block numbers.
1813 */
1814size_t journal_tag_bytes(journal_t *journal)
1815{
1816 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001817 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001818 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001819 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001820}
1821
1822/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001823 * JBD memory management
1824 *
1825 * These functions are used to allocate block-sized chunks of memory
1826 * used for making copies of buffer_head data. Very often it will be
1827 * page-sized chunks of data, but sometimes it will be in
1828 * sub-page-size chunks. (For example, 16k pages on Power systems
1829 * with a 4k block file system.) For blocks smaller than a page, we
1830 * use a SLAB allocator. There are slab caches for each block size,
1831 * which are allocated at mount time, if necessary, and we only free
1832 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1833 * this reason we don't need to a mutex to protect access to
1834 * jbd2_slab[] allocating or releasing memory; only in
1835 * jbd2_journal_create_slab().
1836 */
1837#define JBD2_MAX_SLABS 8
1838static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
1839static DECLARE_MUTEX(jbd2_slab_create_sem);
1840
1841static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1842 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1843 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1844};
1845
1846
1847static void jbd2_journal_destroy_slabs(void)
1848{
1849 int i;
1850
1851 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1852 if (jbd2_slab[i])
1853 kmem_cache_destroy(jbd2_slab[i]);
1854 jbd2_slab[i] = NULL;
1855 }
1856}
1857
1858static int jbd2_journal_create_slab(size_t size)
1859{
1860 int i = order_base_2(size) - 10;
1861 size_t slab_size;
1862
1863 if (size == PAGE_SIZE)
1864 return 0;
1865
1866 if (i >= JBD2_MAX_SLABS)
1867 return -EINVAL;
1868
1869 if (unlikely(i < 0))
1870 i = 0;
1871 down(&jbd2_slab_create_sem);
1872 if (jbd2_slab[i]) {
1873 up(&jbd2_slab_create_sem);
1874 return 0; /* Already created */
1875 }
1876
1877 slab_size = 1 << (i+10);
1878 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1879 slab_size, 0, NULL);
1880 up(&jbd2_slab_create_sem);
1881 if (!jbd2_slab[i]) {
1882 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1883 return -ENOMEM;
1884 }
1885 return 0;
1886}
1887
1888static struct kmem_cache *get_slab(size_t size)
1889{
1890 int i = order_base_2(size) - 10;
1891
1892 BUG_ON(i >= JBD2_MAX_SLABS);
1893 if (unlikely(i < 0))
1894 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001895 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001896 return jbd2_slab[i];
1897}
1898
1899void *jbd2_alloc(size_t size, gfp_t flags)
1900{
1901 void *ptr;
1902
1903 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1904
1905 flags |= __GFP_REPEAT;
1906 if (size == PAGE_SIZE)
1907 ptr = (void *)__get_free_pages(flags, 0);
1908 else if (size > PAGE_SIZE) {
1909 int order = get_order(size);
1910
1911 if (order < 3)
1912 ptr = (void *)__get_free_pages(flags, order);
1913 else
1914 ptr = vmalloc(size);
1915 } else
1916 ptr = kmem_cache_alloc(get_slab(size), flags);
1917
1918 /* Check alignment; SLUB has gotten this wrong in the past,
1919 * and this can lead to user data corruption! */
1920 BUG_ON(((unsigned long) ptr) & (size-1));
1921
1922 return ptr;
1923}
1924
1925void jbd2_free(void *ptr, size_t size)
1926{
1927 if (size == PAGE_SIZE) {
1928 free_pages((unsigned long)ptr, 0);
1929 return;
1930 }
1931 if (size > PAGE_SIZE) {
1932 int order = get_order(size);
1933
1934 if (order < 3)
1935 free_pages((unsigned long)ptr, order);
1936 else
1937 vfree(ptr);
1938 return;
1939 }
1940 kmem_cache_free(get_slab(size), ptr);
1941};
1942
1943/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001944 * Journal_head storage management
1945 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001946static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04001947#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001948static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1949#endif
1950
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001951static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001952{
1953 int retval;
1954
Al Viro1076d172008-03-29 03:07:18 +00001955 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07001956 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 sizeof(struct journal_head),
1958 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05001959 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001960 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001962 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001963 retval = -ENOMEM;
1964 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1965 }
1966 return retval;
1967}
1968
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001969static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001970{
Duane Griffin8a9362e2008-04-17 10:38:59 -04001971 if (jbd2_journal_head_cache) {
1972 kmem_cache_destroy(jbd2_journal_head_cache);
1973 jbd2_journal_head_cache = NULL;
1974 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001975}
1976
1977/*
1978 * journal_head splicing and dicing
1979 */
1980static struct journal_head *journal_alloc_journal_head(void)
1981{
1982 struct journal_head *ret;
1983 static unsigned long last_warning;
1984
Jose R. Santose23291b2007-07-18 08:57:06 -04001985#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07001986 atomic_inc(&nr_journal_heads);
1987#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001988 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00001989 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001990 jbd_debug(1, "out of memory for journal_head\n");
1991 if (time_after(jiffies, last_warning + 5*HZ)) {
1992 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001993 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001994 last_warning = jiffies;
1995 }
Al Viro1076d172008-03-29 03:07:18 +00001996 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001998 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 }
2000 }
2001 return ret;
2002}
2003
2004static void journal_free_journal_head(struct journal_head *jh)
2005{
Jose R. Santose23291b2007-07-18 08:57:06 -04002006#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002008 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002009#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002010 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011}
2012
2013/*
2014 * A journal_head is attached to a buffer_head whenever JBD has an
2015 * interest in the buffer.
2016 *
2017 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2018 * is set. This bit is tested in core kernel code where we need to take
2019 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2020 * there.
2021 *
2022 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2023 *
2024 * When a buffer has its BH_JBD bit set it is immune from being released by
2025 * core kernel code, mainly via ->b_count.
2026 *
2027 * A journal_head may be detached from its buffer_head when the journal_head's
2028 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002029 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002030 * journal_head can be dropped if needed.
2031 *
2032 * Various places in the kernel want to attach a journal_head to a buffer_head
2033 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002034 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002035 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002036 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002037 *
2038 * So the typical usage would be:
2039 *
2040 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 * ...
2043 * jh->b_transaction = xxx;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002044 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002045 *
2046 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2047 * because it has a non-zero b_transaction.
2048 */
2049
2050/*
2051 * Give a buffer_head a journal_head.
2052 *
2053 * Doesn't need the journal lock.
2054 * May sleep.
2055 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002056struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002057{
2058 struct journal_head *jh;
2059 struct journal_head *new_jh = NULL;
2060
2061repeat:
2062 if (!buffer_jbd(bh)) {
2063 new_jh = journal_alloc_journal_head();
2064 memset(new_jh, 0, sizeof(*new_jh));
2065 }
2066
2067 jbd_lock_bh_journal_head(bh);
2068 if (buffer_jbd(bh)) {
2069 jh = bh2jh(bh);
2070 } else {
2071 J_ASSERT_BH(bh,
2072 (atomic_read(&bh->b_count) > 0) ||
2073 (bh->b_page && bh->b_page->mapping));
2074
2075 if (!new_jh) {
2076 jbd_unlock_bh_journal_head(bh);
2077 goto repeat;
2078 }
2079
2080 jh = new_jh;
2081 new_jh = NULL; /* We consumed it */
2082 set_buffer_jbd(bh);
2083 bh->b_private = jh;
2084 jh->b_bh = bh;
2085 get_bh(bh);
2086 BUFFER_TRACE(bh, "added journal_head");
2087 }
2088 jh->b_jcount++;
2089 jbd_unlock_bh_journal_head(bh);
2090 if (new_jh)
2091 journal_free_journal_head(new_jh);
2092 return bh->b_private;
2093}
2094
2095/*
2096 * Grab a ref against this buffer_head's journal_head. If it ended up not
2097 * having a journal_head, return NULL
2098 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002099struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100{
2101 struct journal_head *jh = NULL;
2102
2103 jbd_lock_bh_journal_head(bh);
2104 if (buffer_jbd(bh)) {
2105 jh = bh2jh(bh);
2106 jh->b_jcount++;
2107 }
2108 jbd_unlock_bh_journal_head(bh);
2109 return jh;
2110}
2111
2112static void __journal_remove_journal_head(struct buffer_head *bh)
2113{
2114 struct journal_head *jh = bh2jh(bh);
2115
2116 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2117
2118 get_bh(bh);
2119 if (jh->b_jcount == 0) {
2120 if (jh->b_transaction == NULL &&
2121 jh->b_next_transaction == NULL &&
2122 jh->b_cp_transaction == NULL) {
2123 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2124 J_ASSERT_BH(bh, buffer_jbd(bh));
2125 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2126 BUFFER_TRACE(bh, "remove journal_head");
2127 if (jh->b_frozen_data) {
2128 printk(KERN_WARNING "%s: freeing "
2129 "b_frozen_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002130 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002131 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002132 }
2133 if (jh->b_committed_data) {
2134 printk(KERN_WARNING "%s: freeing "
2135 "b_committed_data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04002136 __func__);
Mingming Caoaf1e76d2007-10-16 18:38:25 -04002137 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002138 }
2139 bh->b_private = NULL;
2140 jh->b_bh = NULL; /* debug, really */
2141 clear_buffer_jbd(bh);
2142 __brelse(bh);
2143 journal_free_journal_head(jh);
2144 } else {
2145 BUFFER_TRACE(bh, "journal_head was locked");
2146 }
2147 }
2148}
2149
2150/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002151 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07002152 * and has a zero b_jcount then remove and release its journal_head. If we did
2153 * see that the buffer is not used by any transaction we also "logically"
2154 * decrement ->b_count.
2155 *
2156 * We in fact take an additional increment on ->b_count as a convenience,
2157 * because the caller usually wants to do additional things with the bh
2158 * after calling here.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002159 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
Dave Kleikamp470decc2006-10-11 01:20:57 -07002160 * time. Once the caller has run __brelse(), the buffer is eligible for
2161 * reaping by try_to_free_buffers().
2162 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002163void jbd2_journal_remove_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002164{
2165 jbd_lock_bh_journal_head(bh);
2166 __journal_remove_journal_head(bh);
2167 jbd_unlock_bh_journal_head(bh);
2168}
2169
2170/*
2171 * Drop a reference on the passed journal_head. If it fell to zero then try to
2172 * release the journal_head from the buffer_head.
2173 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002174void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002175{
2176 struct buffer_head *bh = jh2bh(jh);
2177
2178 jbd_lock_bh_journal_head(bh);
2179 J_ASSERT_JH(jh, jh->b_jcount > 0);
2180 --jh->b_jcount;
2181 if (!jh->b_jcount && !jh->b_transaction) {
2182 __journal_remove_journal_head(bh);
2183 __brelse(bh);
2184 }
2185 jbd_unlock_bh_journal_head(bh);
2186}
2187
2188/*
Jan Karac851ed52008-07-11 19:27:31 -04002189 * Initialize jbd inode head
2190 */
2191void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2192{
2193 jinode->i_transaction = NULL;
2194 jinode->i_next_transaction = NULL;
2195 jinode->i_vfs_inode = inode;
2196 jinode->i_flags = 0;
2197 INIT_LIST_HEAD(&jinode->i_list);
2198}
2199
2200/*
2201 * Function to be called before we start removing inode from memory (i.e.,
2202 * clear_inode() is a fine place to be called from). It removes inode from
2203 * transaction's lists.
2204 */
2205void jbd2_journal_release_jbd_inode(journal_t *journal,
2206 struct jbd2_inode *jinode)
2207{
Jan Karac851ed52008-07-11 19:27:31 -04002208 if (!journal)
2209 return;
2210restart:
2211 spin_lock(&journal->j_list_lock);
2212 /* Is commit writing out inode - we have to wait */
2213 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2214 wait_queue_head_t *wq;
2215 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2216 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2217 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2218 spin_unlock(&journal->j_list_lock);
2219 schedule();
2220 finish_wait(wq, &wait.wait);
2221 goto restart;
2222 }
2223
Jan Karac851ed52008-07-11 19:27:31 -04002224 if (jinode->i_transaction) {
2225 list_del(&jinode->i_list);
2226 jinode->i_transaction = NULL;
2227 }
2228 spin_unlock(&journal->j_list_lock);
2229}
2230
2231/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002232 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002233 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002234#ifdef CONFIG_JBD2_DEBUG
2235u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002236EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002237
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002238#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002239
Jose R. Santos6f38c742007-10-16 18:38:25 -04002240static struct dentry *jbd2_debugfs_dir;
2241static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002242
2243static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002244{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002245 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2246 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002247 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2248 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002249 jbd2_debugfs_dir,
2250 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002251}
2252
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002253static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002254{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002255 debugfs_remove(jbd2_debug);
2256 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257}
2258
2259#else
2260
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002261static void __init jbd2_create_debugfs_entry(void)
2262{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002263}
2264
2265static void __exit jbd2_remove_debugfs_entry(void)
2266{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002267}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002268
2269#endif
2270
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002271#ifdef CONFIG_PROC_FS
2272
2273#define JBD2_STATS_PROC_NAME "fs/jbd2"
2274
2275static void __init jbd2_create_jbd_stats_proc_entry(void)
2276{
2277 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2278}
2279
2280static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2281{
2282 if (proc_jbd2_stats)
2283 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2284}
2285
2286#else
2287
2288#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2289#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2290
2291#endif
2292
Christoph Lametere18b8902006-12-06 20:33:20 -08002293struct kmem_cache *jbd2_handle_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002294
2295static int __init journal_init_handle_cache(void)
2296{
Johann Lombardia920e942006-10-11 01:21:00 -07002297 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002298 sizeof(handle_t),
2299 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002300 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002301 NULL); /* ctor */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002302 if (jbd2_handle_cache == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002303 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2304 return -ENOMEM;
2305 }
2306 return 0;
2307}
2308
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002309static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002310{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002311 if (jbd2_handle_cache)
2312 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002313}
2314
2315/*
2316 * Module startup and shutdown
2317 */
2318
2319static int __init journal_init_caches(void)
2320{
2321 int ret;
2322
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002323 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002324 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002325 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002326 if (ret == 0)
2327 ret = journal_init_handle_cache();
2328 return ret;
2329}
2330
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002331static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002332{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002333 jbd2_journal_destroy_revoke_caches();
2334 jbd2_journal_destroy_jbd2_journal_head_cache();
2335 jbd2_journal_destroy_handle_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002336 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002337}
2338
2339static int __init journal_init(void)
2340{
2341 int ret;
2342
2343 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2344
2345 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002346 if (ret == 0) {
2347 jbd2_create_debugfs_entry();
2348 jbd2_create_jbd_stats_proc_entry();
2349 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002350 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002351 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002352 return ret;
2353}
2354
2355static void __exit journal_exit(void)
2356{
Jose R. Santose23291b2007-07-18 08:57:06 -04002357#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002358 int n = atomic_read(&nr_journal_heads);
2359 if (n)
2360 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2361#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002362 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002363 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002364 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002365}
2366
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002367/*
2368 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2369 * tracing infrastructure to map a dev_t to a device name.
2370 *
2371 * The caller should use rcu_read_lock() in order to make sure the
2372 * device name stays valid until its done with it. We use
2373 * rcu_read_lock() as well to make sure we're safe in case the caller
2374 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2375 * nested.
2376 */
2377struct devname_cache {
2378 struct rcu_head rcu;
2379 dev_t device;
2380 char devname[BDEVNAME_SIZE];
2381};
2382#define CACHE_SIZE_BITS 6
2383static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2384static DEFINE_SPINLOCK(devname_cache_lock);
2385
2386static void free_devcache(struct rcu_head *rcu)
2387{
2388 kfree(rcu);
2389}
2390
2391const char *jbd2_dev_to_name(dev_t device)
2392{
2393 int i = hash_32(device, CACHE_SIZE_BITS);
2394 char *ret;
2395 struct block_device *bd;
Theodore Ts'ob5744802009-06-20 23:34:44 -04002396 static struct devname_cache *new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002397
2398 rcu_read_lock();
2399 if (devcache[i] && devcache[i]->device == device) {
2400 ret = devcache[i]->devname;
2401 rcu_read_unlock();
2402 return ret;
2403 }
2404 rcu_read_unlock();
2405
Theodore Ts'ob5744802009-06-20 23:34:44 -04002406 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2407 if (!new_dev)
2408 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002409 spin_lock(&devname_cache_lock);
2410 if (devcache[i]) {
2411 if (devcache[i]->device == device) {
Theodore Ts'ob5744802009-06-20 23:34:44 -04002412 kfree(new_dev);
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002413 ret = devcache[i]->devname;
2414 spin_unlock(&devname_cache_lock);
2415 return ret;
2416 }
2417 call_rcu(&devcache[i]->rcu, free_devcache);
2418 }
Theodore Ts'ob5744802009-06-20 23:34:44 -04002419 devcache[i] = new_dev;
Theodore Ts'o879c5e62009-06-17 11:47:48 -04002420 devcache[i]->device = device;
2421 bd = bdget(device);
2422 if (bd) {
2423 bdevname(bd, devcache[i]->devname);
2424 bdput(bd);
2425 } else
2426 __bdevname(device, devcache[i]->devname);
2427 ret = devcache[i]->devname;
2428 spin_unlock(&devname_cache_lock);
2429 return ret;
2430}
2431EXPORT_SYMBOL(jbd2_dev_to_name);
2432
Dave Kleikamp470decc2006-10-11 01:20:57 -07002433MODULE_LICENSE("GPL");
2434module_init(journal_init);
2435module_exit(journal_exit);
2436