blob: 663e47cbaa7810d398ad9dbd8f001de6d24c4776 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -050040#include <linux/math64.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040041#include <linux/hash.h>
Theodore Ts'od2eecb02009-12-07 10:36:20 -050042#include <linux/log2.h>
43#include <linux/vmalloc.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040044#include <linux/backing-dev.h>
Brian King39e3ac22010-10-27 21:25:12 -040045#include <linux/bitops.h>
Theodore Ts'o670be5a2010-12-17 10:44:16 -050046#include <linux/ratelimit.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040047
48#define CREATE_TRACE_POINTS
49#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070050
51#include <asm/uaccess.h>
52#include <asm/page.h>
Brian King39e3ac22010-10-27 21:25:12 -040053#include <asm/system.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070054
Mingming Caof7f4bcc2006-10-11 01:20:59 -070055EXPORT_SYMBOL(jbd2_journal_extend);
56EXPORT_SYMBOL(jbd2_journal_stop);
57EXPORT_SYMBOL(jbd2_journal_lock_updates);
58EXPORT_SYMBOL(jbd2_journal_unlock_updates);
59EXPORT_SYMBOL(jbd2_journal_get_write_access);
60EXPORT_SYMBOL(jbd2_journal_get_create_access);
61EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070062EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070063EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
64EXPORT_SYMBOL(jbd2_journal_release_buffer);
65EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070066#if 0
67EXPORT_SYMBOL(journal_sync_buffer);
68#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070069EXPORT_SYMBOL(jbd2_journal_flush);
70EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070071
Mingming Caof7f4bcc2006-10-11 01:20:59 -070072EXPORT_SYMBOL(jbd2_journal_init_dev);
73EXPORT_SYMBOL(jbd2_journal_init_inode);
74EXPORT_SYMBOL(jbd2_journal_update_format);
75EXPORT_SYMBOL(jbd2_journal_check_used_features);
76EXPORT_SYMBOL(jbd2_journal_check_available_features);
77EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070078EXPORT_SYMBOL(jbd2_journal_load);
79EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070080EXPORT_SYMBOL(jbd2_journal_abort);
81EXPORT_SYMBOL(jbd2_journal_errno);
82EXPORT_SYMBOL(jbd2_journal_ack_err);
83EXPORT_SYMBOL(jbd2_journal_clear_err);
84EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050085EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070086EXPORT_SYMBOL(jbd2_journal_start_commit);
87EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
88EXPORT_SYMBOL(jbd2_journal_wipe);
89EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
90EXPORT_SYMBOL(jbd2_journal_invalidatepage);
91EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
92EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040093EXPORT_SYMBOL(jbd2_journal_file_inode);
94EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
95EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
96EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050097EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -070098
99static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
100static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -0500101static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700102
103/*
104 * Helper function used to manage commit timeouts
105 */
106
107static void commit_timeout(unsigned long __data)
108{
109 struct task_struct * p = (struct task_struct *) __data;
110
111 wake_up_process(p);
112}
113
114/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700115 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700116 * journal.
117 *
118 * This kernel thread is responsible for two things:
119 *
120 * 1) COMMIT: Every so often we need to commit the current state of the
121 * filesystem to disk. The journal thread is responsible for writing
122 * all of the metadata buffers to disk.
123 *
124 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
125 * of the data in that part of the log has been rewritten elsewhere on
126 * the disk. Flushing these old buffers to reclaim space in the log is
127 * known as checkpointing, and this thread is responsible for that job.
128 */
129
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700130static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700131{
132 journal_t *journal = arg;
133 transaction_t *transaction;
134
135 /*
136 * Set up an interval timer which can be used to trigger a commit wakeup
137 * after the commit interval expires
138 */
139 setup_timer(&journal->j_commit_timer, commit_timeout,
140 (unsigned long)current);
141
Nigel Cunningham35c80422012-02-03 19:59:41 +1100142 set_freezable();
143
Dave Kleikamp470decc2006-10-11 01:20:57 -0700144 /* Record that the journal thread is running */
145 journal->j_task = current;
146 wake_up(&journal->j_wait_done_commit);
147
Dave Kleikamp470decc2006-10-11 01:20:57 -0700148 /*
149 * And now, wait forever for commit wakeup events.
150 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400151 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700152
153loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700154 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700155 goto end_loop;
156
157 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
158 journal->j_commit_sequence, journal->j_commit_request);
159
160 if (journal->j_commit_sequence != journal->j_commit_request) {
161 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400162 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700163 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700164 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400165 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700166 goto loop;
167 }
168
169 wake_up(&journal->j_wait_done_commit);
170 if (freezing(current)) {
171 /*
172 * The simpler the better. Flushing journal isn't a
173 * good idea, because that depends on threads that may
174 * be already stopped.
175 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700176 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400177 write_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800178 try_to_freeze();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400179 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700180 } else {
181 /*
182 * We assume on resume that commits are already there,
183 * so we don't sleep
184 */
185 DEFINE_WAIT(wait);
186 int should_sleep = 1;
187
188 prepare_to_wait(&journal->j_wait_commit, &wait,
189 TASK_INTERRUPTIBLE);
190 if (journal->j_commit_sequence != journal->j_commit_request)
191 should_sleep = 0;
192 transaction = journal->j_running_transaction;
193 if (transaction && time_after_eq(jiffies,
194 transaction->t_expires))
195 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700196 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197 should_sleep = 0;
198 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400199 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700200 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400201 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700202 }
203 finish_wait(&journal->j_wait_commit, &wait);
204 }
205
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700206 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700207
208 /*
209 * Were we woken up by a commit wakeup event?
210 */
211 transaction = journal->j_running_transaction;
212 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
213 journal->j_commit_request = transaction->t_tid;
214 jbd_debug(1, "woke because of timeout\n");
215 }
216 goto loop;
217
218end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400219 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700220 del_timer_sync(&journal->j_commit_timer);
221 journal->j_task = NULL;
222 wake_up(&journal->j_wait_done_commit);
223 jbd_debug(1, "Journal thread exiting.\n");
224 return 0;
225}
226
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700228{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700229 struct task_struct *t;
230
Theodore Ts'o90576c02009-09-29 15:51:30 -0400231 t = kthread_run(kjournald2, journal, "jbd2/%s",
232 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700233 if (IS_ERR(t))
234 return PTR_ERR(t);
235
Al Viro1076d172008-03-29 03:07:18 +0000236 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700237 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700238}
239
240static void journal_kill_thread(journal_t *journal)
241{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400242 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700243 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244
245 while (journal->j_task) {
246 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400247 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000248 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400249 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700250 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400251 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700252}
253
254/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700255 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700256 *
257 * Writes a metadata buffer to a given disk block. The actual IO is not
258 * performed but a new buffer_head is constructed which labels the data
259 * to be written with the correct destination disk block.
260 *
261 * Any magic-number escaping which needs to be done will cause a
262 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700263 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700264 * magic number is only written to the log for descripter blocks. In
265 * this case, we copy the data and replace the first word with 0, and we
266 * return a result code which indicates that this buffer needs to be
267 * marked as an escaped buffer in the corresponding log descriptor
268 * block. The missing word can then be restored when the block is read
269 * during recovery.
270 *
271 * If the source buffer has already been modified by a new transaction
272 * since we took the last commit snapshot, we use the frozen copy of
273 * that data for IO. If we end up using the existing buffer_head's data
274 * for the write, then we *have* to lock the buffer to prevent anyone
275 * else from using and possibly modifying it while the IO is in
276 * progress.
277 *
278 * The function returns a pointer to the buffer_heads to be used for IO.
279 *
280 * We assume that the journal has already been locked in this function.
281 *
282 * Return value:
283 * <0: Error
284 * >=0: Finished OK
285 *
286 * On success:
287 * Bit 0 set == escape performed on the data
288 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
289 */
290
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700291int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700292 struct journal_head *jh_in,
293 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700294 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700295{
296 int need_copy_out = 0;
297 int done_copy_out = 0;
298 int do_escape = 0;
299 char *mapped_data;
300 struct buffer_head *new_bh;
301 struct journal_head *new_jh;
302 struct page *new_page;
303 unsigned int new_offset;
304 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400305 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700306
307 /*
308 * The buffer really shouldn't be locked: only the current committing
309 * transaction is allowed to write it, so nobody else is allowed
310 * to do any IO.
311 *
312 * akpm: except if we're journalling data, and write() output is
313 * also part of a shared mapping, and another thread has
314 * decided to launch a writepage() against this buffer.
315 */
316 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
317
Theodore Ts'o47def822010-07-27 11:56:05 -0400318retry_alloc:
319 new_bh = alloc_buffer_head(GFP_NOFS);
320 if (!new_bh) {
321 /*
322 * Failure is not an option, but __GFP_NOFAIL is going
323 * away; so we retry ourselves here.
324 */
325 congestion_wait(BLK_RW_ASYNC, HZ/50);
326 goto retry_alloc;
327 }
328
dingdinghua96577c42009-07-13 17:55:35 -0400329 /* keep subsequent assertions sane */
330 new_bh->b_state = 0;
331 init_buffer(new_bh, NULL, NULL);
332 atomic_set(&new_bh->b_count, 1);
333 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700334
335 /*
336 * If a new transaction has already done a buffer copy-out, then
337 * we use that version of the data for the commit.
338 */
339 jbd_lock_bh_state(bh_in);
340repeat:
341 if (jh_in->b_frozen_data) {
342 done_copy_out = 1;
343 new_page = virt_to_page(jh_in->b_frozen_data);
344 new_offset = offset_in_page(jh_in->b_frozen_data);
345 } else {
346 new_page = jh2bh(jh_in)->b_page;
347 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
348 }
349
350 mapped_data = kmap_atomic(new_page, KM_USER0);
351 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200352 * Fire data frozen trigger if data already wasn't frozen. Do this
353 * before checking for escaping, as the trigger may modify the magic
354 * offset. If a copy-out happens afterwards, it will have the correct
355 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700356 */
Jan Kara13ceef02010-07-14 07:56:33 +0200357 if (!done_copy_out)
358 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
359 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700360
361 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700362 * Check for escaping
363 */
364 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700365 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700366 need_copy_out = 1;
367 do_escape = 1;
368 }
369 kunmap_atomic(mapped_data, KM_USER0);
370
371 /*
372 * Do we need to do a data copy?
373 */
374 if (need_copy_out && !done_copy_out) {
375 char *tmp;
376
377 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400378 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500379 if (!tmp) {
380 jbd2_journal_put_journal_head(new_jh);
381 return -ENOMEM;
382 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700383 jbd_lock_bh_state(bh_in);
384 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400385 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700386 goto repeat;
387 }
388
389 jh_in->b_frozen_data = tmp;
390 mapped_data = kmap_atomic(new_page, KM_USER0);
391 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
392 kunmap_atomic(mapped_data, KM_USER0);
393
394 new_page = virt_to_page(tmp);
395 new_offset = offset_in_page(tmp);
396 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700397
398 /*
399 * This isn't strictly necessary, as we're using frozen
400 * data for the escaping, but it keeps consistency with
401 * b_frozen_data usage.
402 */
403 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404 }
405
406 /*
407 * Did we need to do an escaping? Now we've done all the
408 * copying, we can finally do so.
409 */
410 if (do_escape) {
411 mapped_data = kmap_atomic(new_page, KM_USER0);
412 *((unsigned int *)(mapped_data + new_offset)) = 0;
413 kunmap_atomic(mapped_data, KM_USER0);
414 }
415
Dave Kleikamp470decc2006-10-11 01:20:57 -0700416 set_bh_page(new_bh, new_page, new_offset);
417 new_jh->b_transaction = NULL;
418 new_bh->b_size = jh2bh(jh_in)->b_size;
419 new_bh->b_bdev = transaction->t_journal->j_dev;
420 new_bh->b_blocknr = blocknr;
421 set_buffer_mapped(new_bh);
422 set_buffer_dirty(new_bh);
423
424 *jh_out = new_jh;
425
426 /*
427 * The to-be-written buffer needs to get moved to the io queue,
428 * and the original buffer whose contents we are shadowing or
429 * copying is moved to the transaction's shadow queue.
430 */
431 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400432 spin_lock(&journal->j_list_lock);
433 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
434 spin_unlock(&journal->j_list_lock);
435 jbd_unlock_bh_state(bh_in);
436
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700438 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700439
440 return do_escape | (done_copy_out << 1);
441}
442
443/*
444 * Allocation code for the journal file. Manage the space left in the
445 * journal, so that we can begin checkpointing when appropriate.
446 */
447
448/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700449 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700450 *
451 * Called with the journal already locked.
452 *
453 * Called under j_state_lock
454 */
455
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700456int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457{
458 int left = journal->j_free;
459
Theodore Ts'oa931da62010-08-03 21:35:12 -0400460 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700461
462 /*
463 * Be pessimistic here about the number of those free blocks which
464 * might be required for log descriptor control blocks.
465 */
466
467#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
468
469 left -= MIN_LOG_RESERVED_BLOCKS;
470
471 if (left <= 0)
472 return 0;
473 left -= (left >> 3);
474 return left;
475}
476
477/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500478 * Called with j_state_lock locked for writing.
479 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700480 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700481int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700482{
483 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400484 * The only transaction we can possibly wait upon is the
485 * currently running transaction (if it exists). Otherwise,
486 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700487 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400488 if (journal->j_running_transaction &&
489 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700490 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200491 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700492 * commit thread. We do _not_ do the commit ourselves.
493 */
494
495 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400496 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497 journal->j_commit_request,
498 journal->j_commit_sequence);
499 wake_up(&journal->j_wait_commit);
500 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400501 } else if (!tid_geq(journal->j_commit_request, target))
502 /* This should never happen, but if it does, preserve
503 the evidence before kjournald goes into a loop and
504 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400505 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400506 journal->j_commit_request,
507 journal->j_commit_sequence,
508 target, journal->j_running_transaction ?
509 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700510 return 0;
511}
512
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700513int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700514{
515 int ret;
516
Theodore Ts'oa931da62010-08-03 21:35:12 -0400517 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700518 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400519 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 return ret;
521}
522
523/*
524 * Force and wait upon a commit if the calling process is not within
525 * transaction. This is used for forcing out undo-protected data which contains
526 * bitmaps, when the fs is running out of space.
527 *
528 * We can only force the running transaction if we don't have an active handle;
529 * otherwise, we will deadlock.
530 *
531 * Returns true if a transaction was started.
532 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700533int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700534{
535 transaction_t *transaction = NULL;
536 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500537 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700538
Theodore Ts'oa931da62010-08-03 21:35:12 -0400539 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540 if (journal->j_running_transaction && !current->journal_info) {
541 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500542 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
543 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544 } else if (journal->j_committing_transaction)
545 transaction = journal->j_committing_transaction;
546
547 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400548 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700549 return 0; /* Nothing to retry */
550 }
551
552 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400553 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500554 if (need_to_start)
555 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700556 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557 return 1;
558}
559
560/*
561 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500562 * if a transaction is going to be committed (or is currently already
563 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700565int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700566{
567 int ret = 0;
568
Theodore Ts'oa931da62010-08-03 21:35:12 -0400569 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570 if (journal->j_running_transaction) {
571 tid_t tid = journal->j_running_transaction->t_tid;
572
Jan Karac88ccea2009-02-10 11:27:46 -0500573 __jbd2_log_start_commit(journal, tid);
574 /* There's a running transaction and we've just made sure
575 * it's commit has been scheduled. */
576 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700577 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500578 ret = 1;
579 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700580 /*
581 * If ext3_write_super() recently started a commit, then we
582 * have to wait for completion of that transaction
583 */
Jan Karac88ccea2009-02-10 11:27:46 -0500584 if (ptid)
585 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586 ret = 1;
587 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400588 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700589 return ret;
590}
591
592/*
Jan Karabbd2be32011-05-24 11:59:18 -0400593 * Return 1 if a given transaction has not yet sent barrier request
594 * connected with a transaction commit. If 0 is returned, transaction
595 * may or may not have sent the barrier. Used to avoid sending barrier
596 * twice in common cases.
597 */
598int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
599{
600 int ret = 0;
601 transaction_t *commit_trans;
602
603 if (!(journal->j_flags & JBD2_BARRIER))
604 return 0;
605 read_lock(&journal->j_state_lock);
606 /* Transaction already committed? */
607 if (tid_geq(journal->j_commit_sequence, tid))
608 goto out;
609 commit_trans = journal->j_committing_transaction;
610 if (!commit_trans || commit_trans->t_tid != tid) {
611 ret = 1;
612 goto out;
613 }
614 /*
615 * Transaction is being committed and we already proceeded to
616 * submitting a flush to fs partition?
617 */
618 if (journal->j_fs_dev != journal->j_dev) {
619 if (!commit_trans->t_need_data_flush ||
620 commit_trans->t_state >= T_COMMIT_DFLUSH)
621 goto out;
622 } else {
623 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
624 goto out;
625 }
626 ret = 1;
627out:
628 read_unlock(&journal->j_state_lock);
629 return ret;
630}
631EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
632
633/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700634 * Wait for a specified commit to complete.
635 * The caller may not hold the journal lock.
636 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700637int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700638{
639 int err = 0;
640
Theodore Ts'oa931da62010-08-03 21:35:12 -0400641 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400642#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700643 if (!tid_geq(journal->j_commit_request, tid)) {
644 printk(KERN_EMERG
645 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400646 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700648#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700649 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400650 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700651 tid, journal->j_commit_sequence);
652 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400653 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700654 wait_event(journal->j_wait_done_commit,
655 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400656 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700657 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400658 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700659
660 if (unlikely(is_journal_aborted(journal))) {
661 printk(KERN_EMERG "journal commit I/O error\n");
662 err = -EIO;
663 }
664 return err;
665}
666
667/*
668 * Log buffer allocation routines:
669 */
670
Mingming Cao18eba7a2006-10-11 01:21:13 -0700671int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700672{
673 unsigned long blocknr;
674
Theodore Ts'oa931da62010-08-03 21:35:12 -0400675 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700676 J_ASSERT(journal->j_free > 1);
677
678 blocknr = journal->j_head;
679 journal->j_head++;
680 journal->j_free--;
681 if (journal->j_head == journal->j_last)
682 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400683 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700684 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700685}
686
687/*
688 * Conversion of logical to physical block numbers for the journal
689 *
690 * On external journals the journal blocks are identity-mapped, so
691 * this is a no-op. If needed, we can use j_blk_offset - everything is
692 * ready.
693 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700694int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700695 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700696{
697 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700698 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700699
700 if (journal->j_inode) {
701 ret = bmap(journal->j_inode, blocknr);
702 if (ret)
703 *retp = ret;
704 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700705 printk(KERN_ALERT "%s: journal block not found "
706 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400707 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700708 err = -EIO;
709 __journal_abort_soft(journal, err);
710 }
711 } else {
712 *retp = blocknr; /* +journal->j_blk_offset */
713 }
714 return err;
715}
716
717/*
718 * We play buffer_head aliasing tricks to write data/metadata blocks to
719 * the journal without copying their contents, but for journal
720 * descriptor blocks we do need to generate bona fide buffers.
721 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700722 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700723 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
724 * But we don't bother doing that, so there will be coherency problems with
725 * mmaps of blockdevs which hold live JBD-controlled filesystems.
726 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700727struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700728{
729 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700730 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700731 int err;
732
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700733 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700734
735 if (err)
736 return NULL;
737
738 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500739 if (!bh)
740 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700741 lock_buffer(bh);
742 memset(bh->b_data, 0, journal->j_blocksize);
743 set_buffer_uptodate(bh);
744 unlock_buffer(bh);
745 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700746 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700747}
748
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500749struct jbd2_stats_proc_session {
750 journal_t *journal;
751 struct transaction_stats_s *stats;
752 int start;
753 int max;
754};
755
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500756static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
757{
758 return *pos ? NULL : SEQ_START_TOKEN;
759}
760
761static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
762{
763 return NULL;
764}
765
766static int jbd2_seq_info_show(struct seq_file *seq, void *v)
767{
768 struct jbd2_stats_proc_session *s = seq->private;
769
770 if (v != SEQ_START_TOKEN)
771 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400772 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500773 s->stats->ts_tid,
774 s->journal->j_max_transaction_buffers);
775 if (s->stats->ts_tid == 0)
776 return 0;
777 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400778 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500779 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400780 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500781 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400782 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500783 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400784 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500785 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400786 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500787 seq_printf(seq, " %lluus average transaction commit time\n",
788 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500789 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400790 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500791 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400792 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500793 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400794 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500795 return 0;
796}
797
798static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
799{
800}
801
James Morris88e9d342009-09-22 16:43:43 -0700802static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500803 .start = jbd2_seq_info_start,
804 .next = jbd2_seq_info_next,
805 .stop = jbd2_seq_info_stop,
806 .show = jbd2_seq_info_show,
807};
808
809static int jbd2_seq_info_open(struct inode *inode, struct file *file)
810{
811 journal_t *journal = PDE(inode)->data;
812 struct jbd2_stats_proc_session *s;
813 int rc, size;
814
815 s = kmalloc(sizeof(*s), GFP_KERNEL);
816 if (s == NULL)
817 return -ENOMEM;
818 size = sizeof(struct transaction_stats_s);
819 s->stats = kmalloc(size, GFP_KERNEL);
820 if (s->stats == NULL) {
821 kfree(s);
822 return -ENOMEM;
823 }
824 spin_lock(&journal->j_history_lock);
825 memcpy(s->stats, &journal->j_stats, size);
826 s->journal = journal;
827 spin_unlock(&journal->j_history_lock);
828
829 rc = seq_open(file, &jbd2_seq_info_ops);
830 if (rc == 0) {
831 struct seq_file *m = file->private_data;
832 m->private = s;
833 } else {
834 kfree(s->stats);
835 kfree(s);
836 }
837 return rc;
838
839}
840
841static int jbd2_seq_info_release(struct inode *inode, struct file *file)
842{
843 struct seq_file *seq = file->private_data;
844 struct jbd2_stats_proc_session *s = seq->private;
845 kfree(s->stats);
846 kfree(s);
847 return seq_release(inode, file);
848}
849
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700850static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500851 .owner = THIS_MODULE,
852 .open = jbd2_seq_info_open,
853 .read = seq_read,
854 .llseek = seq_lseek,
855 .release = jbd2_seq_info_release,
856};
857
858static struct proc_dir_entry *proc_jbd2_stats;
859
860static void jbd2_stats_proc_init(journal_t *journal)
861{
Theodore Ts'o05496762008-09-16 14:36:17 -0400862 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500863 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700864 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
865 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500866 }
867}
868
869static void jbd2_stats_proc_exit(journal_t *journal)
870{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500871 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400872 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500873}
874
Dave Kleikamp470decc2006-10-11 01:20:57 -0700875/*
876 * Management for journal control blocks: functions to create and
877 * destroy journal_t structures, and to initialise and read existing
878 * journal blocks from disk. */
879
880/* First: create and setup a journal_t object in memory. We initialise
881 * very few fields yet: that has to wait until we have created the
882 * journal structures from from scratch, or loaded them from disk. */
883
884static journal_t * journal_init_common (void)
885{
886 journal_t *journal;
887 int err;
888
Andrew Morton3ebfdf82009-12-23 08:05:15 -0500889 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700890 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500891 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700892
893 init_waitqueue_head(&journal->j_wait_transaction_locked);
894 init_waitqueue_head(&journal->j_wait_logspace);
895 init_waitqueue_head(&journal->j_wait_done_commit);
896 init_waitqueue_head(&journal->j_wait_checkpoint);
897 init_waitqueue_head(&journal->j_wait_commit);
898 init_waitqueue_head(&journal->j_wait_updates);
899 mutex_init(&journal->j_barrier);
900 mutex_init(&journal->j_checkpoint_mutex);
901 spin_lock_init(&journal->j_revoke_lock);
902 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400903 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700904
Mingming Caocd02ff02007-10-16 18:38:25 -0400905 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -0500906 journal->j_min_batch_time = 0;
907 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700908
909 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700910 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700911
912 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700913 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700914 if (err) {
915 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -0500916 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700917 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500918
Theodore Ts'obf699322009-09-30 00:32:06 -0400919 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500920
Dave Kleikamp470decc2006-10-11 01:20:57 -0700921 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700922}
923
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700924/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700925 *
926 * Create a journal structure assigned some fixed set of disk blocks to
927 * the journal. We don't actually touch those disk blocks yet, but we
928 * need to set up all of the mapping information to tell the journaling
929 * system where the journal blocks are.
930 *
931 */
932
933/**
Randy Dunlap5648ba52008-04-17 10:38:59 -0400934 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -0700935 * @bdev: Block device on which to create the journal
936 * @fs_dev: Device which hold journalled filesystem for this journal.
937 * @start: Block nr Start of journal.
938 * @len: Length of the journal in blocks.
939 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -0400940 *
941 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700942 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700943 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -0700944 * range of blocks on an arbitrary block device.
945 *
946 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700947journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700948 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700949 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700950{
951 journal_t *journal = journal_init_common();
952 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -0400953 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700954 int n;
955
956 if (!journal)
957 return NULL;
958
959 /* journal descriptor can store up to n blocks -bzzz */
960 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -0500961 journal->j_dev = bdev;
962 journal->j_fs_dev = fs_dev;
963 journal->j_blk_offset = start;
964 journal->j_maxlen = len;
965 bdevname(journal->j_dev, journal->j_devname);
966 p = journal->j_devname;
967 while ((p = strchr(p, '/')))
968 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -0500969 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700970 n = journal->j_blocksize / sizeof(journal_block_tag_t);
971 journal->j_wbufsize = n;
972 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
973 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300974 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400975 __func__);
Jan Kara4b905672009-01-06 14:53:35 -0500976 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700977 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700978
979 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500980 if (!bh) {
981 printk(KERN_ERR
982 "%s: Cannot get buffer for journal superblock\n",
983 __func__);
984 goto out_err;
985 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700986 journal->j_sb_buffer = bh;
987 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -0500988
Dave Kleikamp470decc2006-10-11 01:20:57 -0700989 return journal;
Jan Kara4b905672009-01-06 14:53:35 -0500990out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800991 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -0500992 jbd2_stats_proc_exit(journal);
993 kfree(journal);
994 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700995}
996
997/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700998 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700999 * @inode: An inode to create the journal in
1000 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001001 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002 * the journal. The inode must exist already, must support bmap() and
1003 * must have all data blocks preallocated.
1004 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001005journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001006{
1007 struct buffer_head *bh;
1008 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001009 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001010 int err;
1011 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001012 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001013
1014 if (!journal)
1015 return NULL;
1016
1017 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1018 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001019 bdevname(journal->j_dev, journal->j_devname);
1020 p = journal->j_devname;
1021 while ((p = strchr(p, '/')))
1022 *p = '!';
1023 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001024 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001025 jbd_debug(1,
1026 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1027 journal, inode->i_sb->s_id, inode->i_ino,
1028 (long long) inode->i_size,
1029 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1030
1031 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1032 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001033 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034
1035 /* journal descriptor can store up to n blocks -bzzz */
1036 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1037 journal->j_wbufsize = n;
1038 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1039 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001040 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001041 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001042 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043 }
1044
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001045 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 /* If that failed, give up */
1047 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001048 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001049 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001050 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051 }
1052
1053 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001054 if (!bh) {
1055 printk(KERN_ERR
1056 "%s: Cannot get buffer for journal superblock\n",
1057 __func__);
1058 goto out_err;
1059 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001060 journal->j_sb_buffer = bh;
1061 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1062
1063 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001064out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001065 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001066 jbd2_stats_proc_exit(journal);
1067 kfree(journal);
1068 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001069}
1070
1071/*
1072 * If the journal init or create aborts, we need to mark the journal
1073 * superblock as being NULL to prevent the journal destroy from writing
1074 * back a bogus superblock.
1075 */
1076static void journal_fail_superblock (journal_t *journal)
1077{
1078 struct buffer_head *bh = journal->j_sb_buffer;
1079 brelse(bh);
1080 journal->j_sb_buffer = NULL;
1081}
1082
1083/*
1084 * Given a journal_t structure, initialise the various fields for
1085 * startup of a new journaling session. We use this both when creating
1086 * a journal, and after recovering an old journal to reset it for
1087 * subsequent use.
1088 */
1089
1090static int journal_reset(journal_t *journal)
1091{
1092 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001093 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094
1095 first = be32_to_cpu(sb->s_first);
1096 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001097 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001098 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001099 first, last);
1100 journal_fail_superblock(journal);
1101 return -EINVAL;
1102 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001103
1104 journal->j_first = first;
1105 journal->j_last = last;
1106
1107 journal->j_head = first;
1108 journal->j_tail = first;
1109 journal->j_free = last - first;
1110
1111 journal->j_tail_sequence = journal->j_transaction_sequence;
1112 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1113 journal->j_commit_request = journal->j_commit_sequence;
1114
1115 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1116
1117 /* Add the dynamic fields and write it to disk. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001118 jbd2_journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -07001119 return jbd2_journal_start_thread(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001120}
1121
1122/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001123 * void jbd2_journal_update_superblock() - Update journal sb on disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001124 * @journal: The journal to update.
1125 * @wait: Set to '0' if you don't want to wait for IO completion.
1126 *
1127 * Update a journal's dynamic superblock fields and write it to disk,
1128 * optionally waiting for the IO to complete.
1129 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001130void jbd2_journal_update_superblock(journal_t *journal, int wait)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131{
1132 journal_superblock_t *sb = journal->j_superblock;
1133 struct buffer_head *bh = journal->j_sb_buffer;
1134
1135 /*
1136 * As a special case, if the on-disk copy is already marked as needing
1137 * no recovery (s_start == 0) and there are no outstanding transactions
1138 * in the filesystem, then we can safely defer the superblock update
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001139 * until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001140 * attempting a write to a potential-readonly device.
1141 */
1142 if (sb->s_start == 0 && journal->j_tail_sequence ==
1143 journal->j_transaction_sequence) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001144 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001145 "(start %ld, seq %d, errno %d)\n",
1146 journal->j_tail, journal->j_tail_sequence,
1147 journal->j_errno);
1148 goto out;
1149 }
1150
Theodore Ts'o914258b2008-10-06 21:35:40 -04001151 if (buffer_write_io_error(bh)) {
1152 /*
1153 * Oh, dear. A previous attempt to write the journal
1154 * superblock failed. This could happen because the
1155 * USB device was yanked out. Or it could happen to
1156 * be a transient write error and maybe the block will
1157 * be remapped. Nothing we can do but to retry the
1158 * write and hope for the best.
1159 */
1160 printk(KERN_ERR "JBD2: previous I/O error detected "
1161 "for journal superblock update for %s.\n",
1162 journal->j_devname);
1163 clear_buffer_write_io_error(bh);
1164 set_buffer_uptodate(bh);
1165 }
1166
Theodore Ts'oa931da62010-08-03 21:35:12 -04001167 read_lock(&journal->j_state_lock);
Eryu Guanf2a44522011-11-01 19:09:18 -04001168 jbd_debug(1, "JBD2: updating superblock (start %ld, seq %d, errno %d)\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1170
1171 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1172 sb->s_start = cpu_to_be32(journal->j_tail);
1173 sb->s_errno = cpu_to_be32(journal->j_errno);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001174 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001175
1176 BUFFER_TRACE(bh, "marking dirty");
1177 mark_buffer_dirty(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001178 if (wait) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001179 sync_dirty_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001180 if (buffer_write_io_error(bh)) {
1181 printk(KERN_ERR "JBD2: I/O error detected "
1182 "when updating journal superblock for %s.\n",
1183 journal->j_devname);
1184 clear_buffer_write_io_error(bh);
1185 set_buffer_uptodate(bh);
1186 }
1187 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001188 write_dirty_buffer(bh, WRITE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001189
1190out:
1191 /* If we have just flushed the log (by marking s_start==0), then
1192 * any future commit will have to be careful to update the
1193 * superblock again to re-record the true start of the log. */
1194
Theodore Ts'oa931da62010-08-03 21:35:12 -04001195 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001196 if (sb->s_start)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001197 journal->j_flags &= ~JBD2_FLUSHED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001198 else
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001199 journal->j_flags |= JBD2_FLUSHED;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001200 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001201}
1202
1203/*
1204 * Read the superblock for a given journal, performing initial
1205 * validation of the format.
1206 */
1207
1208static int journal_get_superblock(journal_t *journal)
1209{
1210 struct buffer_head *bh;
1211 journal_superblock_t *sb;
1212 int err = -EIO;
1213
1214 bh = journal->j_sb_buffer;
1215
1216 J_ASSERT(bh != NULL);
1217 if (!buffer_uptodate(bh)) {
1218 ll_rw_block(READ, 1, &bh);
1219 wait_on_buffer(bh);
1220 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001221 printk(KERN_ERR
1222 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001223 goto out;
1224 }
1225 }
1226
1227 sb = journal->j_superblock;
1228
1229 err = -EINVAL;
1230
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001231 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001232 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001233 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001234 goto out;
1235 }
1236
1237 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001238 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001239 journal->j_format_version = 1;
1240 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001241 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001242 journal->j_format_version = 2;
1243 break;
1244 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001245 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001246 goto out;
1247 }
1248
1249 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1250 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1251 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001252 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001253 goto out;
1254 }
1255
Eryu Guan87622022011-11-01 19:04:59 -04001256 if (be32_to_cpu(sb->s_first) == 0 ||
1257 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1258 printk(KERN_WARNING
1259 "JBD2: Invalid start block of journal: %u\n",
1260 be32_to_cpu(sb->s_first));
1261 goto out;
1262 }
1263
Dave Kleikamp470decc2006-10-11 01:20:57 -07001264 return 0;
1265
1266out:
1267 journal_fail_superblock(journal);
1268 return err;
1269}
1270
1271/*
1272 * Load the on-disk journal superblock and read the key fields into the
1273 * journal_t.
1274 */
1275
1276static int load_superblock(journal_t *journal)
1277{
1278 int err;
1279 journal_superblock_t *sb;
1280
1281 err = journal_get_superblock(journal);
1282 if (err)
1283 return err;
1284
1285 sb = journal->j_superblock;
1286
1287 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1288 journal->j_tail = be32_to_cpu(sb->s_start);
1289 journal->j_first = be32_to_cpu(sb->s_first);
1290 journal->j_last = be32_to_cpu(sb->s_maxlen);
1291 journal->j_errno = be32_to_cpu(sb->s_errno);
1292
1293 return 0;
1294}
1295
1296
1297/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001298 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001299 * @journal: Journal to act on.
1300 *
1301 * Given a journal_t structure which tells us which disk blocks contain
1302 * a journal, read the journal from disk to initialise the in-memory
1303 * structures.
1304 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001305int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001306{
1307 int err;
1308 journal_superblock_t *sb;
1309
1310 err = load_superblock(journal);
1311 if (err)
1312 return err;
1313
1314 sb = journal->j_superblock;
1315 /* If this is a V2 superblock, then we have to check the
1316 * features flags on it. */
1317
1318 if (journal->j_format_version >= 2) {
1319 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001320 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001321 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001322 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001323 printk(KERN_WARNING
1324 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001325 return -EINVAL;
1326 }
1327 }
1328
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001329 /*
1330 * Create a slab for this blocksize
1331 */
1332 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1333 if (err)
1334 return err;
1335
Dave Kleikamp470decc2006-10-11 01:20:57 -07001336 /* Let the recovery code check whether it needs to recover any
1337 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001338 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001339 goto recovery_error;
1340
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001341 if (journal->j_failed_commit) {
1342 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1343 "is corrupt.\n", journal->j_failed_commit,
1344 journal->j_devname);
1345 return -EIO;
1346 }
1347
Dave Kleikamp470decc2006-10-11 01:20:57 -07001348 /* OK, we've finished with the dynamic journal bits:
1349 * reinitialise the dynamic contents of the superblock in memory
1350 * and reset them on disk. */
1351 if (journal_reset(journal))
1352 goto recovery_error;
1353
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001354 journal->j_flags &= ~JBD2_ABORT;
1355 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001356 return 0;
1357
1358recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001359 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001360 return -EIO;
1361}
1362
1363/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001364 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001365 * @journal: Journal to act on.
1366 *
1367 * Release a journal_t structure once it is no longer in use by the
1368 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001369 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001371int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001372{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001373 int err = 0;
1374
Dave Kleikamp470decc2006-10-11 01:20:57 -07001375 /* Wait for the commit thread to wake up and die. */
1376 journal_kill_thread(journal);
1377
1378 /* Force a final log commit */
1379 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001380 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001381
1382 /* Force any old transactions to disk */
1383
1384 /* Totally anal locking here... */
1385 spin_lock(&journal->j_list_lock);
1386 while (journal->j_checkpoint_transactions != NULL) {
1387 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001388 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001389 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001390 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391 spin_lock(&journal->j_list_lock);
1392 }
1393
1394 J_ASSERT(journal->j_running_transaction == NULL);
1395 J_ASSERT(journal->j_committing_transaction == NULL);
1396 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1397 spin_unlock(&journal->j_list_lock);
1398
Dave Kleikamp470decc2006-10-11 01:20:57 -07001399 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001400 if (!is_journal_aborted(journal)) {
1401 /* We can now mark the journal as empty. */
1402 journal->j_tail = 0;
1403 journal->j_tail_sequence =
1404 ++journal->j_transaction_sequence;
1405 jbd2_journal_update_superblock(journal, 1);
1406 } else {
1407 err = -EIO;
1408 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001409 brelse(journal->j_sb_buffer);
1410 }
1411
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001412 if (journal->j_proc_entry)
1413 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001414 if (journal->j_inode)
1415 iput(journal->j_inode);
1416 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001417 jbd2_journal_destroy_revoke(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001418 kfree(journal->j_wbuf);
1419 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001420
1421 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001422}
1423
1424
1425/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001426 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 * @journal: Journal to check.
1428 * @compat: bitmask of compatible features
1429 * @ro: bitmask of features that force read-only mount
1430 * @incompat: bitmask of incompatible features
1431 *
1432 * Check whether the journal uses all of a given set of
1433 * features. Return true (non-zero) if it does.
1434 **/
1435
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001436int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001437 unsigned long ro, unsigned long incompat)
1438{
1439 journal_superblock_t *sb;
1440
1441 if (!compat && !ro && !incompat)
1442 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001443 /* Load journal superblock if it is not loaded yet. */
1444 if (journal->j_format_version == 0 &&
1445 journal_get_superblock(journal) != 0)
1446 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001447 if (journal->j_format_version == 1)
1448 return 0;
1449
1450 sb = journal->j_superblock;
1451
1452 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1453 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1454 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1455 return 1;
1456
1457 return 0;
1458}
1459
1460/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001461 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001462 * @journal: Journal to check.
1463 * @compat: bitmask of compatible features
1464 * @ro: bitmask of features that force read-only mount
1465 * @incompat: bitmask of incompatible features
1466 *
1467 * Check whether the journaling code supports the use of
1468 * all of a given set of features on this journal. Return true
1469 * (non-zero) if it can. */
1470
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001471int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 unsigned long ro, unsigned long incompat)
1473{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001474 if (!compat && !ro && !incompat)
1475 return 1;
1476
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 /* We can support any known requested features iff the
1478 * superblock is in version 2. Otherwise we fail to support any
1479 * extended sb features. */
1480
1481 if (journal->j_format_version != 2)
1482 return 0;
1483
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001484 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1485 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1486 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001487 return 1;
1488
1489 return 0;
1490}
1491
1492/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001493 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001494 * @journal: Journal to act on.
1495 * @compat: bitmask of compatible features
1496 * @ro: bitmask of features that force read-only mount
1497 * @incompat: bitmask of incompatible features
1498 *
1499 * Mark a given journal feature as present on the
1500 * superblock. Returns true if the requested features could be set.
1501 *
1502 */
1503
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001504int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001505 unsigned long ro, unsigned long incompat)
1506{
1507 journal_superblock_t *sb;
1508
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001509 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001510 return 1;
1511
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001512 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001513 return 0;
1514
1515 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1516 compat, ro, incompat);
1517
1518 sb = journal->j_superblock;
1519
1520 sb->s_feature_compat |= cpu_to_be32(compat);
1521 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1522 sb->s_feature_incompat |= cpu_to_be32(incompat);
1523
1524 return 1;
1525}
1526
Girish Shilamkar818d2762008-01-28 23:58:27 -05001527/*
1528 * jbd2_journal_clear_features () - Clear a given journal feature in the
1529 * superblock
1530 * @journal: Journal to act on.
1531 * @compat: bitmask of compatible features
1532 * @ro: bitmask of features that force read-only mount
1533 * @incompat: bitmask of incompatible features
1534 *
1535 * Clear a given journal feature as present on the
1536 * superblock.
1537 */
1538void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1539 unsigned long ro, unsigned long incompat)
1540{
1541 journal_superblock_t *sb;
1542
1543 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1544 compat, ro, incompat);
1545
1546 sb = journal->j_superblock;
1547
1548 sb->s_feature_compat &= ~cpu_to_be32(compat);
1549 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1550 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1551}
1552EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001553
1554/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001555 * int jbd2_journal_update_format () - Update on-disk journal structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001556 * @journal: Journal to act on.
1557 *
1558 * Given an initialised but unloaded journal struct, poke about in the
1559 * on-disk structure to update it to the most recent supported version.
1560 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001561int jbd2_journal_update_format (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001562{
1563 journal_superblock_t *sb;
1564 int err;
1565
1566 err = journal_get_superblock(journal);
1567 if (err)
1568 return err;
1569
1570 sb = journal->j_superblock;
1571
1572 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001573 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001574 return 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001575 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001576 return journal_convert_superblock_v1(journal, sb);
1577 default:
1578 break;
1579 }
1580 return -EINVAL;
1581}
1582
1583static int journal_convert_superblock_v1(journal_t *journal,
1584 journal_superblock_t *sb)
1585{
1586 int offset, blocksize;
1587 struct buffer_head *bh;
1588
1589 printk(KERN_WARNING
Eryu Guanf2a44522011-11-01 19:09:18 -04001590 "JBD2: Converting superblock from version 1 to 2.\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001591
1592 /* Pre-initialise new fields to zero */
1593 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1594 blocksize = be32_to_cpu(sb->s_blocksize);
1595 memset(&sb->s_feature_compat, 0, blocksize-offset);
1596
1597 sb->s_nr_users = cpu_to_be32(1);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001598 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 journal->j_format_version = 2;
1600
1601 bh = journal->j_sb_buffer;
1602 BUFFER_TRACE(bh, "marking dirty");
1603 mark_buffer_dirty(bh);
1604 sync_dirty_buffer(bh);
1605 return 0;
1606}
1607
1608
1609/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001610 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001611 * @journal: Journal to act on.
1612 *
1613 * Flush all data for a given journal to disk and empty the journal.
1614 * Filesystems can use this when remounting readonly to ensure that
1615 * recovery does not need to happen on remount.
1616 */
1617
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001618int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001619{
1620 int err = 0;
1621 transaction_t *transaction = NULL;
1622 unsigned long old_tail;
1623
Theodore Ts'oa931da62010-08-03 21:35:12 -04001624 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001625
1626 /* Force everything buffered to the log... */
1627 if (journal->j_running_transaction) {
1628 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001629 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630 } else if (journal->j_committing_transaction)
1631 transaction = journal->j_committing_transaction;
1632
1633 /* Wait for the log commit to complete... */
1634 if (transaction) {
1635 tid_t tid = transaction->t_tid;
1636
Theodore Ts'oa931da62010-08-03 21:35:12 -04001637 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001640 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001641 }
1642
1643 /* ...and flush everything in the log out to disk. */
1644 spin_lock(&journal->j_list_lock);
1645 while (!err && journal->j_checkpoint_transactions != NULL) {
1646 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001647 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001648 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001649 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 spin_lock(&journal->j_list_lock);
1651 }
1652 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001653
1654 if (is_journal_aborted(journal))
1655 return -EIO;
1656
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001657 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658
1659 /* Finally, mark the journal as really needing no recovery.
1660 * This sets s_start==0 in the underlying superblock, which is
1661 * the magic code for a fully-recovered superblock. Any future
1662 * commits of data to the journal will restore the current
1663 * s_start value. */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001664 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 old_tail = journal->j_tail;
1666 journal->j_tail = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001667 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001668 jbd2_journal_update_superblock(journal, 1);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001669 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670 journal->j_tail = old_tail;
1671
1672 J_ASSERT(!journal->j_running_transaction);
1673 J_ASSERT(!journal->j_committing_transaction);
1674 J_ASSERT(!journal->j_checkpoint_transactions);
1675 J_ASSERT(journal->j_head == journal->j_tail);
1676 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001677 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001678 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001679}
1680
1681/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001682 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001683 * @journal: Journal to act on.
1684 * @write: flag (see below)
1685 *
1686 * Wipe out all of the contents of a journal, safely. This will produce
1687 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001688 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001689 *
1690 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1691 * we merely suppress recovery.
1692 */
1693
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001694int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001695{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001696 int err = 0;
1697
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001698 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001699
1700 err = load_superblock(journal);
1701 if (err)
1702 return err;
1703
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 if (!journal->j_tail)
1705 goto no_recovery;
1706
Eryu Guanf2a44522011-11-01 19:09:18 -04001707 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001708 write ? "Clearing" : "Ignoring");
1709
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001710 err = jbd2_journal_skip_recovery(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001711 if (write)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001712 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713
1714 no_recovery:
1715 return err;
1716}
1717
1718/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001719 * Journal abort has very specific semantics, which we describe
1720 * for journal abort.
1721 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001722 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001723 * itself are here.
1724 */
1725
1726/*
1727 * Quick version for internal journal use (doesn't lock the journal).
1728 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1729 * and don't attempt to make any other journal updates.
1730 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001731void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732{
1733 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001734
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001735 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001736 return;
1737
1738 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001739 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001740
Theodore Ts'oa931da62010-08-03 21:35:12 -04001741 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001742 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001743 transaction = journal->j_running_transaction;
1744 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001745 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001746 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001747}
1748
1749/* Soft abort: record the abort error status in the journal superblock,
1750 * but don't do any other IO. */
1751static void __journal_abort_soft (journal_t *journal, int errno)
1752{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001753 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754 return;
1755
1756 if (!journal->j_errno)
1757 journal->j_errno = errno;
1758
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001759 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760
1761 if (errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001762 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001763}
1764
1765/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001766 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001767 * @journal: the journal to shutdown.
1768 * @errno: an error number to record in the journal indicating
1769 * the reason for the shutdown.
1770 *
1771 * Perform a complete, immediate shutdown of the ENTIRE
1772 * journal (not of a single transaction). This operation cannot be
1773 * undone without closing and reopening the journal.
1774 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001775 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1777 * mode.
1778 *
1779 * Journal abort has very specific semantics. Any existing dirty,
1780 * unjournaled buffers in the main filesystem will still be written to
1781 * disk by bdflush, but the journaling mechanism will be suspended
1782 * immediately and no further transaction commits will be honoured.
1783 *
1784 * Any dirty, journaled buffers will be written back to disk without
1785 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1786 * filesystem, but we _do_ attempt to leave as much data as possible
1787 * behind for fsck to use for cleanup.
1788 *
1789 * Any attempt to get a new transaction handle on a journal which is in
1790 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001791 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07001792 * entered abort state during the update.
1793 *
1794 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001797 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 * which will be recorded (if possible) in the journal superblock. This
1799 * allows a client to record failure conditions in the middle of a
1800 * transaction without having to complete the transaction to record the
1801 * failure to disk. ext3_error, for example, now uses this
1802 * functionality.
1803 *
1804 * Errors which originate from within the journaling layer will NOT
1805 * supply an errno; a null errno implies that absolutely no further
1806 * writes are done to the journal (unless there are any already in
1807 * progress).
1808 *
1809 */
1810
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001811void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001812{
1813 __journal_abort_soft(journal, errno);
1814}
1815
1816/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001817 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001818 * @journal: journal to examine.
1819 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001820 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07001821 * time the journal was mounted - if the journal was stopped
1822 * without calling abort this will be 0.
1823 *
1824 * If the journal has been aborted on this mount time -EROFS will
1825 * be returned.
1826 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001827int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001828{
1829 int err;
1830
Theodore Ts'oa931da62010-08-03 21:35:12 -04001831 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001832 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001833 err = -EROFS;
1834 else
1835 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001836 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001837 return err;
1838}
1839
1840/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001841 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 * @journal: journal to act on.
1843 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001844 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001845 * mode.
1846 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001847int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001848{
1849 int err = 0;
1850
Theodore Ts'oa931da62010-08-03 21:35:12 -04001851 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001852 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853 err = -EROFS;
1854 else
1855 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001856 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001857 return err;
1858}
1859
1860/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001861 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 * @journal: journal to act on.
1863 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001864 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07001865 * mode.
1866 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001867void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001868{
Theodore Ts'oa931da62010-08-03 21:35:12 -04001869 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001870 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001871 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001872 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001873}
1874
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001875int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001876{
1877 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1878}
1879
1880/*
Zach Brownb517bea2006-10-11 01:21:08 -07001881 * helper functions to deal with 32 or 64bit block numbers.
1882 */
1883size_t journal_tag_bytes(journal_t *journal)
1884{
1885 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Mingming Caocd02ff02007-10-16 18:38:25 -04001886 return JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07001887 else
Mingming Caocd02ff02007-10-16 18:38:25 -04001888 return JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07001889}
1890
1891/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001892 * JBD memory management
1893 *
1894 * These functions are used to allocate block-sized chunks of memory
1895 * used for making copies of buffer_head data. Very often it will be
1896 * page-sized chunks of data, but sometimes it will be in
1897 * sub-page-size chunks. (For example, 16k pages on Power systems
1898 * with a 4k block file system.) For blocks smaller than a page, we
1899 * use a SLAB allocator. There are slab caches for each block size,
1900 * which are allocated at mount time, if necessary, and we only free
1901 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1902 * this reason we don't need to a mutex to protect access to
1903 * jbd2_slab[] allocating or releasing memory; only in
1904 * jbd2_journal_create_slab().
1905 */
1906#define JBD2_MAX_SLABS 8
1907static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001908
1909static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1910 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1911 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1912};
1913
1914
1915static void jbd2_journal_destroy_slabs(void)
1916{
1917 int i;
1918
1919 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1920 if (jbd2_slab[i])
1921 kmem_cache_destroy(jbd2_slab[i]);
1922 jbd2_slab[i] = NULL;
1923 }
1924}
1925
1926static int jbd2_journal_create_slab(size_t size)
1927{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001928 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001929 int i = order_base_2(size) - 10;
1930 size_t slab_size;
1931
1932 if (size == PAGE_SIZE)
1933 return 0;
1934
1935 if (i >= JBD2_MAX_SLABS)
1936 return -EINVAL;
1937
1938 if (unlikely(i < 0))
1939 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001940 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001941 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001942 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001943 return 0; /* Already created */
1944 }
1945
1946 slab_size = 1 << (i+10);
1947 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1948 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02001949 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001950 if (!jbd2_slab[i]) {
1951 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1952 return -ENOMEM;
1953 }
1954 return 0;
1955}
1956
1957static struct kmem_cache *get_slab(size_t size)
1958{
1959 int i = order_base_2(size) - 10;
1960
1961 BUG_ON(i >= JBD2_MAX_SLABS);
1962 if (unlikely(i < 0))
1963 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04001964 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001965 return jbd2_slab[i];
1966}
1967
1968void *jbd2_alloc(size_t size, gfp_t flags)
1969{
1970 void *ptr;
1971
1972 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1973
1974 flags |= __GFP_REPEAT;
1975 if (size == PAGE_SIZE)
1976 ptr = (void *)__get_free_pages(flags, 0);
1977 else if (size > PAGE_SIZE) {
1978 int order = get_order(size);
1979
1980 if (order < 3)
1981 ptr = (void *)__get_free_pages(flags, order);
1982 else
1983 ptr = vmalloc(size);
1984 } else
1985 ptr = kmem_cache_alloc(get_slab(size), flags);
1986
1987 /* Check alignment; SLUB has gotten this wrong in the past,
1988 * and this can lead to user data corruption! */
1989 BUG_ON(((unsigned long) ptr) & (size-1));
1990
1991 return ptr;
1992}
1993
1994void jbd2_free(void *ptr, size_t size)
1995{
1996 if (size == PAGE_SIZE) {
1997 free_pages((unsigned long)ptr, 0);
1998 return;
1999 }
2000 if (size > PAGE_SIZE) {
2001 int order = get_order(size);
2002
2003 if (order < 3)
2004 free_pages((unsigned long)ptr, order);
2005 else
2006 vfree(ptr);
2007 return;
2008 }
2009 kmem_cache_free(get_slab(size), ptr);
2010};
2011
2012/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 * Journal_head storage management
2014 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002015static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002016#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002017static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2018#endif
2019
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002020static int journal_init_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021{
2022 int retval;
2023
Al Viro1076d172008-03-29 03:07:18 +00002024 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002025 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002026 sizeof(struct journal_head),
2027 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002028 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002029 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002030 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002031 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002032 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002033 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002034 }
2035 return retval;
2036}
2037
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002038static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002039{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002040 if (jbd2_journal_head_cache) {
2041 kmem_cache_destroy(jbd2_journal_head_cache);
2042 jbd2_journal_head_cache = NULL;
2043 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044}
2045
2046/*
2047 * journal_head splicing and dicing
2048 */
2049static struct journal_head *journal_alloc_journal_head(void)
2050{
2051 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052
Jose R. Santose23291b2007-07-18 08:57:06 -04002053#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054 atomic_inc(&nr_journal_heads);
2055#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002056 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002057 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002059 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002060 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002061 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002062 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002063 }
2064 }
2065 return ret;
2066}
2067
2068static void journal_free_journal_head(struct journal_head *jh)
2069{
Jose R. Santose23291b2007-07-18 08:57:06 -04002070#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002071 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002072 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002073#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002074 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002075}
2076
2077/*
2078 * A journal_head is attached to a buffer_head whenever JBD has an
2079 * interest in the buffer.
2080 *
2081 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2082 * is set. This bit is tested in core kernel code where we need to take
2083 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2084 * there.
2085 *
2086 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2087 *
2088 * When a buffer has its BH_JBD bit set it is immune from being released by
2089 * core kernel code, mainly via ->b_count.
2090 *
Jan Karade1b7942011-06-13 15:38:22 -04002091 * A journal_head is detached from its buffer_head when the journal_head's
2092 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2093 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002094 *
2095 * Various places in the kernel want to attach a journal_head to a buffer_head
2096 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002097 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002098 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002099 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100 *
2101 * So the typical usage would be:
2102 *
2103 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002104 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002105 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002106 * (Get another reference for transaction)
2107 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002108 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002109 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002110 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002111 */
2112
2113/*
2114 * Give a buffer_head a journal_head.
2115 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002116 * May sleep.
2117 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002118struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002119{
2120 struct journal_head *jh;
2121 struct journal_head *new_jh = NULL;
2122
2123repeat:
2124 if (!buffer_jbd(bh)) {
2125 new_jh = journal_alloc_journal_head();
2126 memset(new_jh, 0, sizeof(*new_jh));
2127 }
2128
2129 jbd_lock_bh_journal_head(bh);
2130 if (buffer_jbd(bh)) {
2131 jh = bh2jh(bh);
2132 } else {
2133 J_ASSERT_BH(bh,
2134 (atomic_read(&bh->b_count) > 0) ||
2135 (bh->b_page && bh->b_page->mapping));
2136
2137 if (!new_jh) {
2138 jbd_unlock_bh_journal_head(bh);
2139 goto repeat;
2140 }
2141
2142 jh = new_jh;
2143 new_jh = NULL; /* We consumed it */
2144 set_buffer_jbd(bh);
2145 bh->b_private = jh;
2146 jh->b_bh = bh;
2147 get_bh(bh);
2148 BUFFER_TRACE(bh, "added journal_head");
2149 }
2150 jh->b_jcount++;
2151 jbd_unlock_bh_journal_head(bh);
2152 if (new_jh)
2153 journal_free_journal_head(new_jh);
2154 return bh->b_private;
2155}
2156
2157/*
2158 * Grab a ref against this buffer_head's journal_head. If it ended up not
2159 * having a journal_head, return NULL
2160 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002161struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002162{
2163 struct journal_head *jh = NULL;
2164
2165 jbd_lock_bh_journal_head(bh);
2166 if (buffer_jbd(bh)) {
2167 jh = bh2jh(bh);
2168 jh->b_jcount++;
2169 }
2170 jbd_unlock_bh_journal_head(bh);
2171 return jh;
2172}
2173
2174static void __journal_remove_journal_head(struct buffer_head *bh)
2175{
2176 struct journal_head *jh = bh2jh(bh);
2177
2178 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002179 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2180 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2181 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2182 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2183 J_ASSERT_BH(bh, buffer_jbd(bh));
2184 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2185 BUFFER_TRACE(bh, "remove journal_head");
2186 if (jh->b_frozen_data) {
2187 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2188 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002189 }
Jan Karade1b7942011-06-13 15:38:22 -04002190 if (jh->b_committed_data) {
2191 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2192 jbd2_free(jh->b_committed_data, bh->b_size);
2193 }
2194 bh->b_private = NULL;
2195 jh->b_bh = NULL; /* debug, really */
2196 clear_buffer_jbd(bh);
2197 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002198}
2199
2200/*
Jan Karade1b7942011-06-13 15:38:22 -04002201 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002202 * release the journal_head from the buffer_head.
2203 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002204void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002205{
2206 struct buffer_head *bh = jh2bh(jh);
2207
2208 jbd_lock_bh_journal_head(bh);
2209 J_ASSERT_JH(jh, jh->b_jcount > 0);
2210 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002211 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002212 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002213 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002214 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002215 } else
2216 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002217}
2218
2219/*
Jan Karac851ed52008-07-11 19:27:31 -04002220 * Initialize jbd inode head
2221 */
2222void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2223{
2224 jinode->i_transaction = NULL;
2225 jinode->i_next_transaction = NULL;
2226 jinode->i_vfs_inode = inode;
2227 jinode->i_flags = 0;
2228 INIT_LIST_HEAD(&jinode->i_list);
2229}
2230
2231/*
2232 * Function to be called before we start removing inode from memory (i.e.,
2233 * clear_inode() is a fine place to be called from). It removes inode from
2234 * transaction's lists.
2235 */
2236void jbd2_journal_release_jbd_inode(journal_t *journal,
2237 struct jbd2_inode *jinode)
2238{
Jan Karac851ed52008-07-11 19:27:31 -04002239 if (!journal)
2240 return;
2241restart:
2242 spin_lock(&journal->j_list_lock);
2243 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002244 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002245 wait_queue_head_t *wq;
2246 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2247 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2248 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2249 spin_unlock(&journal->j_list_lock);
2250 schedule();
2251 finish_wait(wq, &wait.wait);
2252 goto restart;
2253 }
2254
Jan Karac851ed52008-07-11 19:27:31 -04002255 if (jinode->i_transaction) {
2256 list_del(&jinode->i_list);
2257 jinode->i_transaction = NULL;
2258 }
2259 spin_unlock(&journal->j_list_lock);
2260}
2261
2262/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002263 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002264 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002265#ifdef CONFIG_JBD2_DEBUG
2266u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002267EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002268
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002269#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002270
Jose R. Santos6f38c742007-10-16 18:38:25 -04002271static struct dentry *jbd2_debugfs_dir;
2272static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002273
2274static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002275{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002276 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2277 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002278 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2279 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002280 jbd2_debugfs_dir,
2281 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002282}
2283
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002284static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002285{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002286 debugfs_remove(jbd2_debug);
2287 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288}
2289
2290#else
2291
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002292static void __init jbd2_create_debugfs_entry(void)
2293{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002294}
2295
2296static void __exit jbd2_remove_debugfs_entry(void)
2297{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002298}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002299
2300#endif
2301
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002302#ifdef CONFIG_PROC_FS
2303
2304#define JBD2_STATS_PROC_NAME "fs/jbd2"
2305
2306static void __init jbd2_create_jbd_stats_proc_entry(void)
2307{
2308 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2309}
2310
2311static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2312{
2313 if (proc_jbd2_stats)
2314 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2315}
2316
2317#else
2318
2319#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2320#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2321
2322#endif
2323
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002324struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002325
2326static int __init journal_init_handle_cache(void)
2327{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002328 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002329 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002330 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2331 return -ENOMEM;
2332 }
2333 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2334 if (jbd2_inode_cache == NULL) {
2335 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2336 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002337 return -ENOMEM;
2338 }
2339 return 0;
2340}
2341
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002342static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002343{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002344 if (jbd2_handle_cache)
2345 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002346 if (jbd2_inode_cache)
2347 kmem_cache_destroy(jbd2_inode_cache);
2348
Dave Kleikamp470decc2006-10-11 01:20:57 -07002349}
2350
2351/*
2352 * Module startup and shutdown
2353 */
2354
2355static int __init journal_init_caches(void)
2356{
2357 int ret;
2358
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002359 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360 if (ret == 0)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002361 ret = journal_init_jbd2_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362 if (ret == 0)
2363 ret = journal_init_handle_cache();
2364 return ret;
2365}
2366
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002367static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002368{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002369 jbd2_journal_destroy_revoke_caches();
2370 jbd2_journal_destroy_jbd2_journal_head_cache();
2371 jbd2_journal_destroy_handle_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002372 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002373}
2374
2375static int __init journal_init(void)
2376{
2377 int ret;
2378
2379 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2380
2381 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002382 if (ret == 0) {
2383 jbd2_create_debugfs_entry();
2384 jbd2_create_jbd_stats_proc_entry();
2385 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002386 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002387 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002388 return ret;
2389}
2390
2391static void __exit journal_exit(void)
2392{
Jose R. Santose23291b2007-07-18 08:57:06 -04002393#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002394 int n = atomic_read(&nr_journal_heads);
2395 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002396 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002397#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002398 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002399 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002400 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002401}
2402
Dave Kleikamp470decc2006-10-11 01:20:57 -07002403MODULE_LICENSE("GPL");
2404module_init(journal_init);
2405module_exit(journal_exit);
2406