1 /*
2  * linux/fs/jbd2/journal.c
3  *
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>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/seq_file.h>
40 #include <linux/math64.h>
41 #include <linux/hash.h>
42 #include <linux/log2.h>
43 #include <linux/vmalloc.h>
44 #include <linux/backing-dev.h>
45 #include <linux/bitops.h>
46 #include <linux/ratelimit.h>
47 
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/jbd2.h>
50 
51 #include <asm/uaccess.h>
52 #include <asm/page.h>
53 
54 EXPORT_SYMBOL(jbd2_journal_extend);
55 EXPORT_SYMBOL(jbd2_journal_stop);
56 EXPORT_SYMBOL(jbd2_journal_lock_updates);
57 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
58 EXPORT_SYMBOL(jbd2_journal_get_write_access);
59 EXPORT_SYMBOL(jbd2_journal_get_create_access);
60 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
61 EXPORT_SYMBOL(jbd2_journal_set_triggers);
62 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
63 EXPORT_SYMBOL(jbd2_journal_release_buffer);
64 EXPORT_SYMBOL(jbd2_journal_forget);
65 #if 0
66 EXPORT_SYMBOL(journal_sync_buffer);
67 #endif
68 EXPORT_SYMBOL(jbd2_journal_flush);
69 EXPORT_SYMBOL(jbd2_journal_revoke);
70 
71 EXPORT_SYMBOL(jbd2_journal_init_dev);
72 EXPORT_SYMBOL(jbd2_journal_init_inode);
73 EXPORT_SYMBOL(jbd2_journal_check_used_features);
74 EXPORT_SYMBOL(jbd2_journal_check_available_features);
75 EXPORT_SYMBOL(jbd2_journal_set_features);
76 EXPORT_SYMBOL(jbd2_journal_load);
77 EXPORT_SYMBOL(jbd2_journal_destroy);
78 EXPORT_SYMBOL(jbd2_journal_abort);
79 EXPORT_SYMBOL(jbd2_journal_errno);
80 EXPORT_SYMBOL(jbd2_journal_ack_err);
81 EXPORT_SYMBOL(jbd2_journal_clear_err);
82 EXPORT_SYMBOL(jbd2_log_wait_commit);
83 EXPORT_SYMBOL(jbd2_log_start_commit);
84 EXPORT_SYMBOL(jbd2_journal_start_commit);
85 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
86 EXPORT_SYMBOL(jbd2_journal_wipe);
87 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
88 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
89 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
90 EXPORT_SYMBOL(jbd2_journal_force_commit);
91 EXPORT_SYMBOL(jbd2_journal_file_inode);
92 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
93 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
94 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
95 EXPORT_SYMBOL(jbd2_inode_cache);
96 
97 static void __journal_abort_soft (journal_t *journal, int errno);
98 static int jbd2_journal_create_slab(size_t slab_size);
99 
100 /*
101  * Helper function used to manage commit timeouts
102  */
103 
commit_timeout(unsigned long __data)104 static void commit_timeout(unsigned long __data)
105 {
106 	struct task_struct * p = (struct task_struct *) __data;
107 
108 	wake_up_process(p);
109 }
110 
111 /*
112  * kjournald2: The main thread function used to manage a logging device
113  * journal.
114  *
115  * This kernel thread is responsible for two things:
116  *
117  * 1) COMMIT:  Every so often we need to commit the current state of the
118  *    filesystem to disk.  The journal thread is responsible for writing
119  *    all of the metadata buffers to disk.
120  *
121  * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
122  *    of the data in that part of the log has been rewritten elsewhere on
123  *    the disk.  Flushing these old buffers to reclaim space in the log is
124  *    known as checkpointing, and this thread is responsible for that job.
125  */
126 
kjournald2(void * arg)127 static int kjournald2(void *arg)
128 {
129 	journal_t *journal = arg;
130 	transaction_t *transaction;
131 
132 	/*
133 	 * Set up an interval timer which can be used to trigger a commit wakeup
134 	 * after the commit interval expires
135 	 */
136 	setup_timer(&journal->j_commit_timer, commit_timeout,
137 			(unsigned long)current);
138 
139 	set_freezable();
140 
141 	/* Record that the journal thread is running */
142 	journal->j_task = current;
143 	wake_up(&journal->j_wait_done_commit);
144 
145 	/*
146 	 * And now, wait forever for commit wakeup events.
147 	 */
148 	write_lock(&journal->j_state_lock);
149 
150 loop:
151 	if (journal->j_flags & JBD2_UNMOUNT)
152 		goto end_loop;
153 
154 	jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
155 		journal->j_commit_sequence, journal->j_commit_request);
156 
157 	if (journal->j_commit_sequence != journal->j_commit_request) {
158 		jbd_debug(1, "OK, requests differ\n");
159 		write_unlock(&journal->j_state_lock);
160 		del_timer_sync(&journal->j_commit_timer);
161 		jbd2_journal_commit_transaction(journal);
162 		write_lock(&journal->j_state_lock);
163 		goto loop;
164 	}
165 
166 	wake_up(&journal->j_wait_done_commit);
167 	if (freezing(current)) {
168 		/*
169 		 * The simpler the better. Flushing journal isn't a
170 		 * good idea, because that depends on threads that may
171 		 * be already stopped.
172 		 */
173 		jbd_debug(1, "Now suspending kjournald2\n");
174 		write_unlock(&journal->j_state_lock);
175 		try_to_freeze();
176 		write_lock(&journal->j_state_lock);
177 	} else {
178 		/*
179 		 * We assume on resume that commits are already there,
180 		 * so we don't sleep
181 		 */
182 		DEFINE_WAIT(wait);
183 		int should_sleep = 1;
184 
185 		prepare_to_wait(&journal->j_wait_commit, &wait,
186 				TASK_INTERRUPTIBLE);
187 		if (journal->j_commit_sequence != journal->j_commit_request)
188 			should_sleep = 0;
189 		transaction = journal->j_running_transaction;
190 		if (transaction && time_after_eq(jiffies,
191 						transaction->t_expires))
192 			should_sleep = 0;
193 		if (journal->j_flags & JBD2_UNMOUNT)
194 			should_sleep = 0;
195 		if (should_sleep) {
196 			write_unlock(&journal->j_state_lock);
197 			schedule();
198 			write_lock(&journal->j_state_lock);
199 		}
200 		finish_wait(&journal->j_wait_commit, &wait);
201 	}
202 
203 	jbd_debug(1, "kjournald2 wakes\n");
204 
205 	/*
206 	 * Were we woken up by a commit wakeup event?
207 	 */
208 	transaction = journal->j_running_transaction;
209 	if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
210 		journal->j_commit_request = transaction->t_tid;
211 		jbd_debug(1, "woke because of timeout\n");
212 	}
213 	goto loop;
214 
215 end_loop:
216 	write_unlock(&journal->j_state_lock);
217 	del_timer_sync(&journal->j_commit_timer);
218 	journal->j_task = NULL;
219 	wake_up(&journal->j_wait_done_commit);
220 	jbd_debug(1, "Journal thread exiting.\n");
221 	return 0;
222 }
223 
jbd2_journal_start_thread(journal_t * journal)224 static int jbd2_journal_start_thread(journal_t *journal)
225 {
226 	struct task_struct *t;
227 
228 	t = kthread_run(kjournald2, journal, "jbd2/%s",
229 			journal->j_devname);
230 	if (IS_ERR(t))
231 		return PTR_ERR(t);
232 
233 	wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
234 	return 0;
235 }
236 
journal_kill_thread(journal_t * journal)237 static void journal_kill_thread(journal_t *journal)
238 {
239 	write_lock(&journal->j_state_lock);
240 	journal->j_flags |= JBD2_UNMOUNT;
241 
242 	while (journal->j_task) {
243 		wake_up(&journal->j_wait_commit);
244 		write_unlock(&journal->j_state_lock);
245 		wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
246 		write_lock(&journal->j_state_lock);
247 	}
248 	write_unlock(&journal->j_state_lock);
249 }
250 
251 /*
252  * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
253  *
254  * Writes a metadata buffer to a given disk block.  The actual IO is not
255  * performed but a new buffer_head is constructed which labels the data
256  * to be written with the correct destination disk block.
257  *
258  * Any magic-number escaping which needs to be done will cause a
259  * copy-out here.  If the buffer happens to start with the
260  * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
261  * magic number is only written to the log for descripter blocks.  In
262  * this case, we copy the data and replace the first word with 0, and we
263  * return a result code which indicates that this buffer needs to be
264  * marked as an escaped buffer in the corresponding log descriptor
265  * block.  The missing word can then be restored when the block is read
266  * during recovery.
267  *
268  * If the source buffer has already been modified by a new transaction
269  * since we took the last commit snapshot, we use the frozen copy of
270  * that data for IO.  If we end up using the existing buffer_head's data
271  * for the write, then we *have* to lock the buffer to prevent anyone
272  * else from using and possibly modifying it while the IO is in
273  * progress.
274  *
275  * The function returns a pointer to the buffer_heads to be used for IO.
276  *
277  * We assume that the journal has already been locked in this function.
278  *
279  * Return value:
280  *  <0: Error
281  * >=0: Finished OK
282  *
283  * On success:
284  * Bit 0 set == escape performed on the data
285  * Bit 1 set == buffer copy-out performed (kfree the data after IO)
286  */
287 
jbd2_journal_write_metadata_buffer(transaction_t * transaction,struct journal_head * jh_in,struct journal_head ** jh_out,unsigned long long blocknr)288 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
289 				  struct journal_head  *jh_in,
290 				  struct journal_head **jh_out,
291 				  unsigned long long blocknr)
292 {
293 	int need_copy_out = 0;
294 	int done_copy_out = 0;
295 	int do_escape = 0;
296 	char *mapped_data;
297 	struct buffer_head *new_bh;
298 	struct journal_head *new_jh;
299 	struct page *new_page;
300 	unsigned int new_offset;
301 	struct buffer_head *bh_in = jh2bh(jh_in);
302 	journal_t *journal = transaction->t_journal;
303 
304 	/*
305 	 * The buffer really shouldn't be locked: only the current committing
306 	 * transaction is allowed to write it, so nobody else is allowed
307 	 * to do any IO.
308 	 *
309 	 * akpm: except if we're journalling data, and write() output is
310 	 * also part of a shared mapping, and another thread has
311 	 * decided to launch a writepage() against this buffer.
312 	 */
313 	J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
314 
315 retry_alloc:
316 	new_bh = alloc_buffer_head(GFP_NOFS);
317 	if (!new_bh) {
318 		/*
319 		 * Failure is not an option, but __GFP_NOFAIL is going
320 		 * away; so we retry ourselves here.
321 		 */
322 		congestion_wait(BLK_RW_ASYNC, HZ/50);
323 		goto retry_alloc;
324 	}
325 
326 	/* keep subsequent assertions sane */
327 	new_bh->b_state = 0;
328 	init_buffer(new_bh, NULL, NULL);
329 	atomic_set(&new_bh->b_count, 1);
330 	new_jh = jbd2_journal_add_journal_head(new_bh);	/* This sleeps */
331 
332 	/*
333 	 * If a new transaction has already done a buffer copy-out, then
334 	 * we use that version of the data for the commit.
335 	 */
336 	jbd_lock_bh_state(bh_in);
337 repeat:
338 	if (jh_in->b_frozen_data) {
339 		done_copy_out = 1;
340 		new_page = virt_to_page(jh_in->b_frozen_data);
341 		new_offset = offset_in_page(jh_in->b_frozen_data);
342 	} else {
343 		new_page = jh2bh(jh_in)->b_page;
344 		new_offset = offset_in_page(jh2bh(jh_in)->b_data);
345 	}
346 
347 	mapped_data = kmap_atomic(new_page);
348 	/*
349 	 * Fire data frozen trigger if data already wasn't frozen.  Do this
350 	 * before checking for escaping, as the trigger may modify the magic
351 	 * offset.  If a copy-out happens afterwards, it will have the correct
352 	 * data in the buffer.
353 	 */
354 	if (!done_copy_out)
355 		jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
356 					   jh_in->b_triggers);
357 
358 	/*
359 	 * Check for escaping
360 	 */
361 	if (*((__be32 *)(mapped_data + new_offset)) ==
362 				cpu_to_be32(JBD2_MAGIC_NUMBER)) {
363 		need_copy_out = 1;
364 		do_escape = 1;
365 	}
366 	kunmap_atomic(mapped_data);
367 
368 	/*
369 	 * Do we need to do a data copy?
370 	 */
371 	if (need_copy_out && !done_copy_out) {
372 		char *tmp;
373 
374 		jbd_unlock_bh_state(bh_in);
375 		tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
376 		if (!tmp) {
377 			jbd2_journal_put_journal_head(new_jh);
378 			return -ENOMEM;
379 		}
380 		jbd_lock_bh_state(bh_in);
381 		if (jh_in->b_frozen_data) {
382 			jbd2_free(tmp, bh_in->b_size);
383 			goto repeat;
384 		}
385 
386 		jh_in->b_frozen_data = tmp;
387 		mapped_data = kmap_atomic(new_page);
388 		memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
389 		kunmap_atomic(mapped_data);
390 
391 		new_page = virt_to_page(tmp);
392 		new_offset = offset_in_page(tmp);
393 		done_copy_out = 1;
394 
395 		/*
396 		 * This isn't strictly necessary, as we're using frozen
397 		 * data for the escaping, but it keeps consistency with
398 		 * b_frozen_data usage.
399 		 */
400 		jh_in->b_frozen_triggers = jh_in->b_triggers;
401 	}
402 
403 	/*
404 	 * Did we need to do an escaping?  Now we've done all the
405 	 * copying, we can finally do so.
406 	 */
407 	if (do_escape) {
408 		mapped_data = kmap_atomic(new_page);
409 		*((unsigned int *)(mapped_data + new_offset)) = 0;
410 		kunmap_atomic(mapped_data);
411 	}
412 
413 	set_bh_page(new_bh, new_page, new_offset);
414 	new_jh->b_transaction = NULL;
415 	new_bh->b_size = jh2bh(jh_in)->b_size;
416 	new_bh->b_bdev = transaction->t_journal->j_dev;
417 	new_bh->b_blocknr = blocknr;
418 	set_buffer_mapped(new_bh);
419 	set_buffer_dirty(new_bh);
420 
421 	*jh_out = new_jh;
422 
423 	/*
424 	 * The to-be-written buffer needs to get moved to the io queue,
425 	 * and the original buffer whose contents we are shadowing or
426 	 * copying is moved to the transaction's shadow queue.
427 	 */
428 	JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
429 	spin_lock(&journal->j_list_lock);
430 	__jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
431 	spin_unlock(&journal->j_list_lock);
432 	jbd_unlock_bh_state(bh_in);
433 
434 	JBUFFER_TRACE(new_jh, "file as BJ_IO");
435 	jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
436 
437 	return do_escape | (done_copy_out << 1);
438 }
439 
440 /*
441  * Allocation code for the journal file.  Manage the space left in the
442  * journal, so that we can begin checkpointing when appropriate.
443  */
444 
445 /*
446  * __jbd2_log_space_left: Return the number of free blocks left in the journal.
447  *
448  * Called with the journal already locked.
449  *
450  * Called under j_state_lock
451  */
452 
__jbd2_log_space_left(journal_t * journal)453 int __jbd2_log_space_left(journal_t *journal)
454 {
455 	int left = journal->j_free;
456 
457 	/* assert_spin_locked(&journal->j_state_lock); */
458 
459 	/*
460 	 * Be pessimistic here about the number of those free blocks which
461 	 * might be required for log descriptor control blocks.
462 	 */
463 
464 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
465 
466 	left -= MIN_LOG_RESERVED_BLOCKS;
467 
468 	if (left <= 0)
469 		return 0;
470 	left -= (left >> 3);
471 	return left;
472 }
473 
474 /*
475  * Called with j_state_lock locked for writing.
476  * Returns true if a transaction commit was started.
477  */
__jbd2_log_start_commit(journal_t * journal,tid_t target)478 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
479 {
480 	/*
481 	 * The only transaction we can possibly wait upon is the
482 	 * currently running transaction (if it exists).  Otherwise,
483 	 * the target tid must be an old one.
484 	 */
485 	if (journal->j_running_transaction &&
486 	    journal->j_running_transaction->t_tid == target) {
487 		/*
488 		 * We want a new commit: OK, mark the request and wakeup the
489 		 * commit thread.  We do _not_ do the commit ourselves.
490 		 */
491 
492 		journal->j_commit_request = target;
493 		jbd_debug(1, "JBD2: requesting commit %d/%d\n",
494 			  journal->j_commit_request,
495 			  journal->j_commit_sequence);
496 		wake_up(&journal->j_wait_commit);
497 		return 1;
498 	} else if (!tid_geq(journal->j_commit_request, target))
499 		/* This should never happen, but if it does, preserve
500 		   the evidence before kjournald goes into a loop and
501 		   increments j_commit_sequence beyond all recognition. */
502 		WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
503 			  journal->j_commit_request,
504 			  journal->j_commit_sequence,
505 			  target, journal->j_running_transaction ?
506 			  journal->j_running_transaction->t_tid : 0);
507 	return 0;
508 }
509 
jbd2_log_start_commit(journal_t * journal,tid_t tid)510 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
511 {
512 	int ret;
513 
514 	write_lock(&journal->j_state_lock);
515 	ret = __jbd2_log_start_commit(journal, tid);
516 	write_unlock(&journal->j_state_lock);
517 	return ret;
518 }
519 
520 /*
521  * Force and wait upon a commit if the calling process is not within
522  * transaction.  This is used for forcing out undo-protected data which contains
523  * bitmaps, when the fs is running out of space.
524  *
525  * We can only force the running transaction if we don't have an active handle;
526  * otherwise, we will deadlock.
527  *
528  * Returns true if a transaction was started.
529  */
jbd2_journal_force_commit_nested(journal_t * journal)530 int jbd2_journal_force_commit_nested(journal_t *journal)
531 {
532 	transaction_t *transaction = NULL;
533 	tid_t tid;
534 	int need_to_start = 0;
535 
536 	read_lock(&journal->j_state_lock);
537 	if (journal->j_running_transaction && !current->journal_info) {
538 		transaction = journal->j_running_transaction;
539 		if (!tid_geq(journal->j_commit_request, transaction->t_tid))
540 			need_to_start = 1;
541 	} else if (journal->j_committing_transaction)
542 		transaction = journal->j_committing_transaction;
543 
544 	if (!transaction) {
545 		read_unlock(&journal->j_state_lock);
546 		return 0;	/* Nothing to retry */
547 	}
548 
549 	tid = transaction->t_tid;
550 	read_unlock(&journal->j_state_lock);
551 	if (need_to_start)
552 		jbd2_log_start_commit(journal, tid);
553 	jbd2_log_wait_commit(journal, tid);
554 	return 1;
555 }
556 
557 /*
558  * Start a commit of the current running transaction (if any).  Returns true
559  * if a transaction is going to be committed (or is currently already
560  * committing), and fills its tid in at *ptid
561  */
jbd2_journal_start_commit(journal_t * journal,tid_t * ptid)562 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
563 {
564 	int ret = 0;
565 
566 	write_lock(&journal->j_state_lock);
567 	if (journal->j_running_transaction) {
568 		tid_t tid = journal->j_running_transaction->t_tid;
569 
570 		__jbd2_log_start_commit(journal, tid);
571 		/* There's a running transaction and we've just made sure
572 		 * it's commit has been scheduled. */
573 		if (ptid)
574 			*ptid = tid;
575 		ret = 1;
576 	} else if (journal->j_committing_transaction) {
577 		/*
578 		 * If ext3_write_super() recently started a commit, then we
579 		 * have to wait for completion of that transaction
580 		 */
581 		if (ptid)
582 			*ptid = journal->j_committing_transaction->t_tid;
583 		ret = 1;
584 	}
585 	write_unlock(&journal->j_state_lock);
586 	return ret;
587 }
588 
589 /*
590  * Return 1 if a given transaction has not yet sent barrier request
591  * connected with a transaction commit. If 0 is returned, transaction
592  * may or may not have sent the barrier. Used to avoid sending barrier
593  * twice in common cases.
594  */
jbd2_trans_will_send_data_barrier(journal_t * journal,tid_t tid)595 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
596 {
597 	int ret = 0;
598 	transaction_t *commit_trans;
599 
600 	if (!(journal->j_flags & JBD2_BARRIER))
601 		return 0;
602 	read_lock(&journal->j_state_lock);
603 	/* Transaction already committed? */
604 	if (tid_geq(journal->j_commit_sequence, tid))
605 		goto out;
606 	commit_trans = journal->j_committing_transaction;
607 	if (!commit_trans || commit_trans->t_tid != tid) {
608 		ret = 1;
609 		goto out;
610 	}
611 	/*
612 	 * Transaction is being committed and we already proceeded to
613 	 * submitting a flush to fs partition?
614 	 */
615 	if (journal->j_fs_dev != journal->j_dev) {
616 		if (!commit_trans->t_need_data_flush ||
617 		    commit_trans->t_state >= T_COMMIT_DFLUSH)
618 			goto out;
619 	} else {
620 		if (commit_trans->t_state >= T_COMMIT_JFLUSH)
621 			goto out;
622 	}
623 	ret = 1;
624 out:
625 	read_unlock(&journal->j_state_lock);
626 	return ret;
627 }
628 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
629 
630 /*
631  * Wait for a specified commit to complete.
632  * The caller may not hold the journal lock.
633  */
jbd2_log_wait_commit(journal_t * journal,tid_t tid)634 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
635 {
636 	int err = 0;
637 
638 	read_lock(&journal->j_state_lock);
639 #ifdef CONFIG_JBD2_DEBUG
640 	if (!tid_geq(journal->j_commit_request, tid)) {
641 		printk(KERN_EMERG
642 		       "%s: error: j_commit_request=%d, tid=%d\n",
643 		       __func__, journal->j_commit_request, tid);
644 	}
645 #endif
646 	while (tid_gt(tid, journal->j_commit_sequence)) {
647 		jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
648 				  tid, journal->j_commit_sequence);
649 		wake_up(&journal->j_wait_commit);
650 		read_unlock(&journal->j_state_lock);
651 		wait_event(journal->j_wait_done_commit,
652 				!tid_gt(tid, journal->j_commit_sequence));
653 		read_lock(&journal->j_state_lock);
654 	}
655 	read_unlock(&journal->j_state_lock);
656 
657 	if (unlikely(is_journal_aborted(journal))) {
658 		printk(KERN_EMERG "journal commit I/O error\n");
659 		err = -EIO;
660 	}
661 	return err;
662 }
663 
664 /*
665  * When this function returns the transaction corresponding to tid
666  * will be completed.  If the transaction has currently running, start
667  * committing that transaction before waiting for it to complete.  If
668  * the transaction id is stale, it is by definition already completed,
669  * so just return SUCCESS.
670  */
jbd2_complete_transaction(journal_t * journal,tid_t tid)671 int jbd2_complete_transaction(journal_t *journal, tid_t tid)
672 {
673 	int	need_to_wait = 1;
674 
675 	read_lock(&journal->j_state_lock);
676 	if (journal->j_running_transaction &&
677 	    journal->j_running_transaction->t_tid == tid) {
678 		if (journal->j_commit_request != tid) {
679 			/* transaction not yet started, so request it */
680 			read_unlock(&journal->j_state_lock);
681 			jbd2_log_start_commit(journal, tid);
682 			goto wait_commit;
683 		}
684 	} else if (!(journal->j_committing_transaction &&
685 		     journal->j_committing_transaction->t_tid == tid))
686 		need_to_wait = 0;
687 	read_unlock(&journal->j_state_lock);
688 	if (!need_to_wait)
689 		return 0;
690 wait_commit:
691 	return jbd2_log_wait_commit(journal, tid);
692 }
693 EXPORT_SYMBOL(jbd2_complete_transaction);
694 
695 /*
696  * Log buffer allocation routines:
697  */
698 
jbd2_journal_next_log_block(journal_t * journal,unsigned long long * retp)699 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
700 {
701 	unsigned long blocknr;
702 
703 	write_lock(&journal->j_state_lock);
704 	J_ASSERT(journal->j_free > 1);
705 
706 	blocknr = journal->j_head;
707 	journal->j_head++;
708 	journal->j_free--;
709 	if (journal->j_head == journal->j_last)
710 		journal->j_head = journal->j_first;
711 	write_unlock(&journal->j_state_lock);
712 	return jbd2_journal_bmap(journal, blocknr, retp);
713 }
714 
715 /*
716  * Conversion of logical to physical block numbers for the journal
717  *
718  * On external journals the journal blocks are identity-mapped, so
719  * this is a no-op.  If needed, we can use j_blk_offset - everything is
720  * ready.
721  */
jbd2_journal_bmap(journal_t * journal,unsigned long blocknr,unsigned long long * retp)722 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
723 		 unsigned long long *retp)
724 {
725 	int err = 0;
726 	unsigned long long ret;
727 
728 	if (journal->j_inode) {
729 		ret = bmap(journal->j_inode, blocknr);
730 		if (ret)
731 			*retp = ret;
732 		else {
733 			printk(KERN_ALERT "%s: journal block not found "
734 					"at offset %lu on %s\n",
735 			       __func__, blocknr, journal->j_devname);
736 			err = -EIO;
737 			__journal_abort_soft(journal, err);
738 		}
739 	} else {
740 		*retp = blocknr; /* +journal->j_blk_offset */
741 	}
742 	return err;
743 }
744 
745 /*
746  * We play buffer_head aliasing tricks to write data/metadata blocks to
747  * the journal without copying their contents, but for journal
748  * descriptor blocks we do need to generate bona fide buffers.
749  *
750  * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
751  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
752  * But we don't bother doing that, so there will be coherency problems with
753  * mmaps of blockdevs which hold live JBD-controlled filesystems.
754  */
jbd2_journal_get_descriptor_buffer(journal_t * journal)755 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
756 {
757 	struct buffer_head *bh;
758 	unsigned long long blocknr;
759 	int err;
760 
761 	err = jbd2_journal_next_log_block(journal, &blocknr);
762 
763 	if (err)
764 		return NULL;
765 
766 	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
767 	if (!bh)
768 		return NULL;
769 	lock_buffer(bh);
770 	memset(bh->b_data, 0, journal->j_blocksize);
771 	set_buffer_uptodate(bh);
772 	unlock_buffer(bh);
773 	BUFFER_TRACE(bh, "return this buffer");
774 	return jbd2_journal_add_journal_head(bh);
775 }
776 
777 /*
778  * Return tid of the oldest transaction in the journal and block in the journal
779  * where the transaction starts.
780  *
781  * If the journal is now empty, return which will be the next transaction ID
782  * we will write and where will that transaction start.
783  *
784  * The return value is 0 if journal tail cannot be pushed any further, 1 if
785  * it can.
786  */
jbd2_journal_get_log_tail(journal_t * journal,tid_t * tid,unsigned long * block)787 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
788 			      unsigned long *block)
789 {
790 	transaction_t *transaction;
791 	int ret;
792 
793 	read_lock(&journal->j_state_lock);
794 	spin_lock(&journal->j_list_lock);
795 	transaction = journal->j_checkpoint_transactions;
796 	if (transaction) {
797 		*tid = transaction->t_tid;
798 		*block = transaction->t_log_start;
799 	} else if ((transaction = journal->j_committing_transaction) != NULL) {
800 		*tid = transaction->t_tid;
801 		*block = transaction->t_log_start;
802 	} else if ((transaction = journal->j_running_transaction) != NULL) {
803 		*tid = transaction->t_tid;
804 		*block = journal->j_head;
805 	} else {
806 		*tid = journal->j_transaction_sequence;
807 		*block = journal->j_head;
808 	}
809 	ret = tid_gt(*tid, journal->j_tail_sequence);
810 	spin_unlock(&journal->j_list_lock);
811 	read_unlock(&journal->j_state_lock);
812 
813 	return ret;
814 }
815 
816 /*
817  * Update information in journal structure and in on disk journal superblock
818  * about log tail. This function does not check whether information passed in
819  * really pushes log tail further. It's responsibility of the caller to make
820  * sure provided log tail information is valid (e.g. by holding
821  * j_checkpoint_mutex all the time between computing log tail and calling this
822  * function as is the case with jbd2_cleanup_journal_tail()).
823  *
824  * Requires j_checkpoint_mutex
825  */
__jbd2_update_log_tail(journal_t * journal,tid_t tid,unsigned long block)826 void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
827 {
828 	unsigned long freed;
829 
830 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
831 
832 	/*
833 	 * We cannot afford for write to remain in drive's caches since as
834 	 * soon as we update j_tail, next transaction can start reusing journal
835 	 * space and if we lose sb update during power failure we'd replay
836 	 * old transaction with possibly newly overwritten data.
837 	 */
838 	jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
839 	write_lock(&journal->j_state_lock);
840 	freed = block - journal->j_tail;
841 	if (block < journal->j_tail)
842 		freed += journal->j_last - journal->j_first;
843 
844 	trace_jbd2_update_log_tail(journal, tid, block, freed);
845 	jbd_debug(1,
846 		  "Cleaning journal tail from %d to %d (offset %lu), "
847 		  "freeing %lu\n",
848 		  journal->j_tail_sequence, tid, block, freed);
849 
850 	journal->j_free += freed;
851 	journal->j_tail_sequence = tid;
852 	journal->j_tail = block;
853 	write_unlock(&journal->j_state_lock);
854 }
855 
856 /*
857  * This is a variaon of __jbd2_update_log_tail which checks for validity of
858  * provided log tail and locks j_checkpoint_mutex. So it is safe against races
859  * with other threads updating log tail.
860  */
jbd2_update_log_tail(journal_t * journal,tid_t tid,unsigned long block)861 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
862 {
863 	mutex_lock(&journal->j_checkpoint_mutex);
864 	if (tid_gt(tid, journal->j_tail_sequence))
865 		__jbd2_update_log_tail(journal, tid, block);
866 	mutex_unlock(&journal->j_checkpoint_mutex);
867 }
868 
869 struct jbd2_stats_proc_session {
870 	journal_t *journal;
871 	struct transaction_stats_s *stats;
872 	int start;
873 	int max;
874 };
875 
jbd2_seq_info_start(struct seq_file * seq,loff_t * pos)876 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
877 {
878 	return *pos ? NULL : SEQ_START_TOKEN;
879 }
880 
jbd2_seq_info_next(struct seq_file * seq,void * v,loff_t * pos)881 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
882 {
883 	return NULL;
884 }
885 
jbd2_seq_info_show(struct seq_file * seq,void * v)886 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
887 {
888 	struct jbd2_stats_proc_session *s = seq->private;
889 
890 	if (v != SEQ_START_TOKEN)
891 		return 0;
892 	seq_printf(seq, "%lu transaction, each up to %u blocks\n",
893 			s->stats->ts_tid,
894 			s->journal->j_max_transaction_buffers);
895 	if (s->stats->ts_tid == 0)
896 		return 0;
897 	seq_printf(seq, "average: \n  %ums waiting for transaction\n",
898 	    jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
899 	seq_printf(seq, "  %ums running transaction\n",
900 	    jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
901 	seq_printf(seq, "  %ums transaction was being locked\n",
902 	    jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
903 	seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
904 	    jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
905 	seq_printf(seq, "  %ums logging transaction\n",
906 	    jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
907 	seq_printf(seq, "  %lluus average transaction commit time\n",
908 		   div_u64(s->journal->j_average_commit_time, 1000));
909 	seq_printf(seq, "  %lu handles per transaction\n",
910 	    s->stats->run.rs_handle_count / s->stats->ts_tid);
911 	seq_printf(seq, "  %lu blocks per transaction\n",
912 	    s->stats->run.rs_blocks / s->stats->ts_tid);
913 	seq_printf(seq, "  %lu logged blocks per transaction\n",
914 	    s->stats->run.rs_blocks_logged / s->stats->ts_tid);
915 	return 0;
916 }
917 
jbd2_seq_info_stop(struct seq_file * seq,void * v)918 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
919 {
920 }
921 
922 static const struct seq_operations jbd2_seq_info_ops = {
923 	.start  = jbd2_seq_info_start,
924 	.next   = jbd2_seq_info_next,
925 	.stop   = jbd2_seq_info_stop,
926 	.show   = jbd2_seq_info_show,
927 };
928 
jbd2_seq_info_open(struct inode * inode,struct file * file)929 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
930 {
931 	journal_t *journal = PDE(inode)->data;
932 	struct jbd2_stats_proc_session *s;
933 	int rc, size;
934 
935 	s = kmalloc(sizeof(*s), GFP_KERNEL);
936 	if (s == NULL)
937 		return -ENOMEM;
938 	size = sizeof(struct transaction_stats_s);
939 	s->stats = kmalloc(size, GFP_KERNEL);
940 	if (s->stats == NULL) {
941 		kfree(s);
942 		return -ENOMEM;
943 	}
944 	spin_lock(&journal->j_history_lock);
945 	memcpy(s->stats, &journal->j_stats, size);
946 	s->journal = journal;
947 	spin_unlock(&journal->j_history_lock);
948 
949 	rc = seq_open(file, &jbd2_seq_info_ops);
950 	if (rc == 0) {
951 		struct seq_file *m = file->private_data;
952 		m->private = s;
953 	} else {
954 		kfree(s->stats);
955 		kfree(s);
956 	}
957 	return rc;
958 
959 }
960 
jbd2_seq_info_release(struct inode * inode,struct file * file)961 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
962 {
963 	struct seq_file *seq = file->private_data;
964 	struct jbd2_stats_proc_session *s = seq->private;
965 	kfree(s->stats);
966 	kfree(s);
967 	return seq_release(inode, file);
968 }
969 
970 static const struct file_operations jbd2_seq_info_fops = {
971 	.owner		= THIS_MODULE,
972 	.open           = jbd2_seq_info_open,
973 	.read           = seq_read,
974 	.llseek         = seq_lseek,
975 	.release        = jbd2_seq_info_release,
976 };
977 
978 static struct proc_dir_entry *proc_jbd2_stats;
979 
jbd2_stats_proc_init(journal_t * journal)980 static void jbd2_stats_proc_init(journal_t *journal)
981 {
982 	journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
983 	if (journal->j_proc_entry) {
984 		proc_create_data("info", S_IRUGO, journal->j_proc_entry,
985 				 &jbd2_seq_info_fops, journal);
986 	}
987 }
988 
jbd2_stats_proc_exit(journal_t * journal)989 static void jbd2_stats_proc_exit(journal_t *journal)
990 {
991 	remove_proc_entry("info", journal->j_proc_entry);
992 	remove_proc_entry(journal->j_devname, proc_jbd2_stats);
993 }
994 
995 /*
996  * Management for journal control blocks: functions to create and
997  * destroy journal_t structures, and to initialise and read existing
998  * journal blocks from disk.  */
999 
1000 /* First: create and setup a journal_t object in memory.  We initialise
1001  * very few fields yet: that has to wait until we have created the
1002  * journal structures from from scratch, or loaded them from disk. */
1003 
journal_init_common(void)1004 static journal_t * journal_init_common (void)
1005 {
1006 	journal_t *journal;
1007 	int err;
1008 
1009 	journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1010 	if (!journal)
1011 		return NULL;
1012 
1013 	init_waitqueue_head(&journal->j_wait_transaction_locked);
1014 	init_waitqueue_head(&journal->j_wait_logspace);
1015 	init_waitqueue_head(&journal->j_wait_done_commit);
1016 	init_waitqueue_head(&journal->j_wait_checkpoint);
1017 	init_waitqueue_head(&journal->j_wait_commit);
1018 	init_waitqueue_head(&journal->j_wait_updates);
1019 	mutex_init(&journal->j_barrier);
1020 	mutex_init(&journal->j_checkpoint_mutex);
1021 	spin_lock_init(&journal->j_revoke_lock);
1022 	spin_lock_init(&journal->j_list_lock);
1023 	rwlock_init(&journal->j_state_lock);
1024 
1025 	journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1026 	journal->j_min_batch_time = 0;
1027 	journal->j_max_batch_time = 15000; /* 15ms */
1028 
1029 	/* The journal is marked for error until we succeed with recovery! */
1030 	journal->j_flags = JBD2_ABORT;
1031 
1032 	/* Set up a default-sized revoke table for the new mount. */
1033 	err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1034 	if (err) {
1035 		kfree(journal);
1036 		return NULL;
1037 	}
1038 
1039 	spin_lock_init(&journal->j_history_lock);
1040 
1041 	return journal;
1042 }
1043 
1044 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1045  *
1046  * Create a journal structure assigned some fixed set of disk blocks to
1047  * the journal.  We don't actually touch those disk blocks yet, but we
1048  * need to set up all of the mapping information to tell the journaling
1049  * system where the journal blocks are.
1050  *
1051  */
1052 
1053 /**
1054  *  journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1055  *  @bdev: Block device on which to create the journal
1056  *  @fs_dev: Device which hold journalled filesystem for this journal.
1057  *  @start: Block nr Start of journal.
1058  *  @len:  Length of the journal in blocks.
1059  *  @blocksize: blocksize of journalling device
1060  *
1061  *  Returns: a newly created journal_t *
1062  *
1063  *  jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1064  *  range of blocks on an arbitrary block device.
1065  *
1066  */
jbd2_journal_init_dev(struct block_device * bdev,struct block_device * fs_dev,unsigned long long start,int len,int blocksize)1067 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1068 			struct block_device *fs_dev,
1069 			unsigned long long start, int len, int blocksize)
1070 {
1071 	journal_t *journal = journal_init_common();
1072 	struct buffer_head *bh;
1073 	char *p;
1074 	int n;
1075 
1076 	if (!journal)
1077 		return NULL;
1078 
1079 	/* journal descriptor can store up to n blocks -bzzz */
1080 	journal->j_blocksize = blocksize;
1081 	journal->j_dev = bdev;
1082 	journal->j_fs_dev = fs_dev;
1083 	journal->j_blk_offset = start;
1084 	journal->j_maxlen = len;
1085 	bdevname(journal->j_dev, journal->j_devname);
1086 	p = journal->j_devname;
1087 	while ((p = strchr(p, '/')))
1088 		*p = '!';
1089 	jbd2_stats_proc_init(journal);
1090 	n = journal->j_blocksize / sizeof(journal_block_tag_t);
1091 	journal->j_wbufsize = n;
1092 	journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1093 	if (!journal->j_wbuf) {
1094 		printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1095 			__func__);
1096 		goto out_err;
1097 	}
1098 
1099 	bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1100 	if (!bh) {
1101 		printk(KERN_ERR
1102 		       "%s: Cannot get buffer for journal superblock\n",
1103 		       __func__);
1104 		goto out_err;
1105 	}
1106 	journal->j_sb_buffer = bh;
1107 	journal->j_superblock = (journal_superblock_t *)bh->b_data;
1108 
1109 	return journal;
1110 out_err:
1111 	kfree(journal->j_wbuf);
1112 	jbd2_stats_proc_exit(journal);
1113 	kfree(journal);
1114 	return NULL;
1115 }
1116 
1117 /**
1118  *  journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1119  *  @inode: An inode to create the journal in
1120  *
1121  * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1122  * the journal.  The inode must exist already, must support bmap() and
1123  * must have all data blocks preallocated.
1124  */
jbd2_journal_init_inode(struct inode * inode)1125 journal_t * jbd2_journal_init_inode (struct inode *inode)
1126 {
1127 	struct buffer_head *bh;
1128 	journal_t *journal = journal_init_common();
1129 	char *p;
1130 	int err;
1131 	int n;
1132 	unsigned long long blocknr;
1133 
1134 	if (!journal)
1135 		return NULL;
1136 
1137 	journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1138 	journal->j_inode = inode;
1139 	bdevname(journal->j_dev, journal->j_devname);
1140 	p = journal->j_devname;
1141 	while ((p = strchr(p, '/')))
1142 		*p = '!';
1143 	p = journal->j_devname + strlen(journal->j_devname);
1144 	sprintf(p, "-%lu", journal->j_inode->i_ino);
1145 	jbd_debug(1,
1146 		  "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1147 		  journal, inode->i_sb->s_id, inode->i_ino,
1148 		  (long long) inode->i_size,
1149 		  inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1150 
1151 	journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1152 	journal->j_blocksize = inode->i_sb->s_blocksize;
1153 	jbd2_stats_proc_init(journal);
1154 
1155 	/* journal descriptor can store up to n blocks -bzzz */
1156 	n = journal->j_blocksize / sizeof(journal_block_tag_t);
1157 	journal->j_wbufsize = n;
1158 	journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1159 	if (!journal->j_wbuf) {
1160 		printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1161 			__func__);
1162 		goto out_err;
1163 	}
1164 
1165 	err = jbd2_journal_bmap(journal, 0, &blocknr);
1166 	/* If that failed, give up */
1167 	if (err) {
1168 		printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1169 		       __func__);
1170 		goto out_err;
1171 	}
1172 
1173 	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1174 	if (!bh) {
1175 		printk(KERN_ERR
1176 		       "%s: Cannot get buffer for journal superblock\n",
1177 		       __func__);
1178 		goto out_err;
1179 	}
1180 	journal->j_sb_buffer = bh;
1181 	journal->j_superblock = (journal_superblock_t *)bh->b_data;
1182 
1183 	return journal;
1184 out_err:
1185 	kfree(journal->j_wbuf);
1186 	jbd2_stats_proc_exit(journal);
1187 	kfree(journal);
1188 	return NULL;
1189 }
1190 
1191 /*
1192  * If the journal init or create aborts, we need to mark the journal
1193  * superblock as being NULL to prevent the journal destroy from writing
1194  * back a bogus superblock.
1195  */
journal_fail_superblock(journal_t * journal)1196 static void journal_fail_superblock (journal_t *journal)
1197 {
1198 	struct buffer_head *bh = journal->j_sb_buffer;
1199 	brelse(bh);
1200 	journal->j_sb_buffer = NULL;
1201 }
1202 
1203 /*
1204  * Given a journal_t structure, initialise the various fields for
1205  * startup of a new journaling session.  We use this both when creating
1206  * a journal, and after recovering an old journal to reset it for
1207  * subsequent use.
1208  */
1209 
journal_reset(journal_t * journal)1210 static int journal_reset(journal_t *journal)
1211 {
1212 	journal_superblock_t *sb = journal->j_superblock;
1213 	unsigned long long first, last;
1214 
1215 	first = be32_to_cpu(sb->s_first);
1216 	last = be32_to_cpu(sb->s_maxlen);
1217 	if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1218 		printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1219 		       first, last);
1220 		journal_fail_superblock(journal);
1221 		return -EINVAL;
1222 	}
1223 
1224 	journal->j_first = first;
1225 	journal->j_last = last;
1226 
1227 	journal->j_head = first;
1228 	journal->j_tail = first;
1229 	journal->j_free = last - first;
1230 
1231 	journal->j_tail_sequence = journal->j_transaction_sequence;
1232 	journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1233 	journal->j_commit_request = journal->j_commit_sequence;
1234 
1235 	journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1236 
1237 	/*
1238 	 * As a special case, if the on-disk copy is already marked as needing
1239 	 * no recovery (s_start == 0), then we can safely defer the superblock
1240 	 * update until the next commit by setting JBD2_FLUSHED.  This avoids
1241 	 * attempting a write to a potential-readonly device.
1242 	 */
1243 	if (sb->s_start == 0) {
1244 		jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1245 			"(start %ld, seq %d, errno %d)\n",
1246 			journal->j_tail, journal->j_tail_sequence,
1247 			journal->j_errno);
1248 		journal->j_flags |= JBD2_FLUSHED;
1249 	} else {
1250 		/* Lock here to make assertions happy... */
1251 		mutex_lock(&journal->j_checkpoint_mutex);
1252 		/*
1253 		 * Update log tail information. We use WRITE_FUA since new
1254 		 * transaction will start reusing journal space and so we
1255 		 * must make sure information about current log tail is on
1256 		 * disk before that.
1257 		 */
1258 		jbd2_journal_update_sb_log_tail(journal,
1259 						journal->j_tail_sequence,
1260 						journal->j_tail,
1261 						WRITE_FUA);
1262 		mutex_unlock(&journal->j_checkpoint_mutex);
1263 	}
1264 	return jbd2_journal_start_thread(journal);
1265 }
1266 
jbd2_write_superblock(journal_t * journal,int write_op)1267 static void jbd2_write_superblock(journal_t *journal, int write_op)
1268 {
1269 	struct buffer_head *bh = journal->j_sb_buffer;
1270 	int ret;
1271 
1272 	trace_jbd2_write_superblock(journal, write_op);
1273 	if (!(journal->j_flags & JBD2_BARRIER))
1274 		write_op &= ~(REQ_FUA | REQ_FLUSH);
1275 	lock_buffer(bh);
1276 	if (buffer_write_io_error(bh)) {
1277 		/*
1278 		 * Oh, dear.  A previous attempt to write the journal
1279 		 * superblock failed.  This could happen because the
1280 		 * USB device was yanked out.  Or it could happen to
1281 		 * be a transient write error and maybe the block will
1282 		 * be remapped.  Nothing we can do but to retry the
1283 		 * write and hope for the best.
1284 		 */
1285 		printk(KERN_ERR "JBD2: previous I/O error detected "
1286 		       "for journal superblock update for %s.\n",
1287 		       journal->j_devname);
1288 		clear_buffer_write_io_error(bh);
1289 		set_buffer_uptodate(bh);
1290 	}
1291 	get_bh(bh);
1292 	bh->b_end_io = end_buffer_write_sync;
1293 	ret = submit_bh(write_op, bh);
1294 	wait_on_buffer(bh);
1295 	if (buffer_write_io_error(bh)) {
1296 		clear_buffer_write_io_error(bh);
1297 		set_buffer_uptodate(bh);
1298 		ret = -EIO;
1299 	}
1300 	if (ret) {
1301 		printk(KERN_ERR "JBD2: Error %d detected when updating "
1302 		       "journal superblock for %s.\n", ret,
1303 		       journal->j_devname);
1304 	}
1305 }
1306 
1307 /**
1308  * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1309  * @journal: The journal to update.
1310  * @tail_tid: TID of the new transaction at the tail of the log
1311  * @tail_block: The first block of the transaction at the tail of the log
1312  * @write_op: With which operation should we write the journal sb
1313  *
1314  * Update a journal's superblock information about log tail and write it to
1315  * disk, waiting for the IO to complete.
1316  */
jbd2_journal_update_sb_log_tail(journal_t * journal,tid_t tail_tid,unsigned long tail_block,int write_op)1317 void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1318 				     unsigned long tail_block, int write_op)
1319 {
1320 	journal_superblock_t *sb = journal->j_superblock;
1321 
1322 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1323 	jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1324 		  tail_block, tail_tid);
1325 
1326 	sb->s_sequence = cpu_to_be32(tail_tid);
1327 	sb->s_start    = cpu_to_be32(tail_block);
1328 
1329 	jbd2_write_superblock(journal, write_op);
1330 
1331 	/* Log is no longer empty */
1332 	write_lock(&journal->j_state_lock);
1333 	WARN_ON(!sb->s_sequence);
1334 	journal->j_flags &= ~JBD2_FLUSHED;
1335 	write_unlock(&journal->j_state_lock);
1336 }
1337 
1338 /**
1339  * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1340  * @journal: The journal to update.
1341  *
1342  * Update a journal's dynamic superblock fields to show that journal is empty.
1343  * Write updated superblock to disk waiting for IO to complete.
1344  */
jbd2_mark_journal_empty(journal_t * journal)1345 static void jbd2_mark_journal_empty(journal_t *journal)
1346 {
1347 	journal_superblock_t *sb = journal->j_superblock;
1348 
1349 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1350 	read_lock(&journal->j_state_lock);
1351 	/* Is it already empty? */
1352 	if (sb->s_start == 0) {
1353 		read_unlock(&journal->j_state_lock);
1354 		return;
1355 	}
1356 	jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1357 		  journal->j_tail_sequence);
1358 
1359 	sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1360 	sb->s_start    = cpu_to_be32(0);
1361 	read_unlock(&journal->j_state_lock);
1362 
1363 	jbd2_write_superblock(journal, WRITE_FUA);
1364 
1365 	/* Log is no longer empty */
1366 	write_lock(&journal->j_state_lock);
1367 	journal->j_flags |= JBD2_FLUSHED;
1368 	write_unlock(&journal->j_state_lock);
1369 }
1370 
1371 
1372 /**
1373  * jbd2_journal_update_sb_errno() - Update error in the journal.
1374  * @journal: The journal to update.
1375  *
1376  * Update a journal's errno.  Write updated superblock to disk waiting for IO
1377  * to complete.
1378  */
jbd2_journal_update_sb_errno(journal_t * journal)1379 void jbd2_journal_update_sb_errno(journal_t *journal)
1380 {
1381 	journal_superblock_t *sb = journal->j_superblock;
1382 
1383 	read_lock(&journal->j_state_lock);
1384 	jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1385 		  journal->j_errno);
1386 	sb->s_errno    = cpu_to_be32(journal->j_errno);
1387 	read_unlock(&journal->j_state_lock);
1388 
1389 	jbd2_write_superblock(journal, WRITE_SYNC);
1390 }
1391 EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1392 
1393 /*
1394  * Read the superblock for a given journal, performing initial
1395  * validation of the format.
1396  */
journal_get_superblock(journal_t * journal)1397 static int journal_get_superblock(journal_t *journal)
1398 {
1399 	struct buffer_head *bh;
1400 	journal_superblock_t *sb;
1401 	int err = -EIO;
1402 
1403 	bh = journal->j_sb_buffer;
1404 
1405 	J_ASSERT(bh != NULL);
1406 	if (!buffer_uptodate(bh)) {
1407 		ll_rw_block(READ, 1, &bh);
1408 		wait_on_buffer(bh);
1409 		if (!buffer_uptodate(bh)) {
1410 			printk(KERN_ERR
1411 				"JBD2: IO error reading journal superblock\n");
1412 			goto out;
1413 		}
1414 	}
1415 
1416 	sb = journal->j_superblock;
1417 
1418 	err = -EINVAL;
1419 
1420 	if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1421 	    sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1422 		printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1423 		goto out;
1424 	}
1425 
1426 	switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1427 	case JBD2_SUPERBLOCK_V1:
1428 		journal->j_format_version = 1;
1429 		break;
1430 	case JBD2_SUPERBLOCK_V2:
1431 		journal->j_format_version = 2;
1432 		break;
1433 	default:
1434 		printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1435 		goto out;
1436 	}
1437 
1438 	if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1439 		journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1440 	else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1441 		printk(KERN_WARNING "JBD2: journal file too short\n");
1442 		goto out;
1443 	}
1444 
1445 	if (be32_to_cpu(sb->s_first) == 0 ||
1446 	    be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1447 		printk(KERN_WARNING
1448 			"JBD2: Invalid start block of journal: %u\n",
1449 			be32_to_cpu(sb->s_first));
1450 		goto out;
1451 	}
1452 
1453 	return 0;
1454 
1455 out:
1456 	journal_fail_superblock(journal);
1457 	return err;
1458 }
1459 
1460 /*
1461  * Load the on-disk journal superblock and read the key fields into the
1462  * journal_t.
1463  */
1464 
load_superblock(journal_t * journal)1465 static int load_superblock(journal_t *journal)
1466 {
1467 	int err;
1468 	journal_superblock_t *sb;
1469 
1470 	err = journal_get_superblock(journal);
1471 	if (err)
1472 		return err;
1473 
1474 	sb = journal->j_superblock;
1475 
1476 	journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1477 	journal->j_tail = be32_to_cpu(sb->s_start);
1478 	journal->j_first = be32_to_cpu(sb->s_first);
1479 	journal->j_last = be32_to_cpu(sb->s_maxlen);
1480 	journal->j_errno = be32_to_cpu(sb->s_errno);
1481 
1482 	return 0;
1483 }
1484 
1485 
1486 /**
1487  * int jbd2_journal_load() - Read journal from disk.
1488  * @journal: Journal to act on.
1489  *
1490  * Given a journal_t structure which tells us which disk blocks contain
1491  * a journal, read the journal from disk to initialise the in-memory
1492  * structures.
1493  */
jbd2_journal_load(journal_t * journal)1494 int jbd2_journal_load(journal_t *journal)
1495 {
1496 	int err;
1497 	journal_superblock_t *sb;
1498 
1499 	err = load_superblock(journal);
1500 	if (err)
1501 		return err;
1502 
1503 	sb = journal->j_superblock;
1504 	/* If this is a V2 superblock, then we have to check the
1505 	 * features flags on it. */
1506 
1507 	if (journal->j_format_version >= 2) {
1508 		if ((sb->s_feature_ro_compat &
1509 		     ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1510 		    (sb->s_feature_incompat &
1511 		     ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1512 			printk(KERN_WARNING
1513 				"JBD2: Unrecognised features on journal\n");
1514 			return -EINVAL;
1515 		}
1516 	}
1517 
1518 	/*
1519 	 * Create a slab for this blocksize
1520 	 */
1521 	err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1522 	if (err)
1523 		return err;
1524 
1525 	/* Let the recovery code check whether it needs to recover any
1526 	 * data from the journal. */
1527 	if (jbd2_journal_recover(journal))
1528 		goto recovery_error;
1529 
1530 	if (journal->j_failed_commit) {
1531 		printk(KERN_ERR "JBD2: journal transaction %u on %s "
1532 		       "is corrupt.\n", journal->j_failed_commit,
1533 		       journal->j_devname);
1534 		return -EIO;
1535 	}
1536 
1537 	/* OK, we've finished with the dynamic journal bits:
1538 	 * reinitialise the dynamic contents of the superblock in memory
1539 	 * and reset them on disk. */
1540 	if (journal_reset(journal))
1541 		goto recovery_error;
1542 
1543 	journal->j_flags &= ~JBD2_ABORT;
1544 	journal->j_flags |= JBD2_LOADED;
1545 	return 0;
1546 
1547 recovery_error:
1548 	printk(KERN_WARNING "JBD2: recovery failed\n");
1549 	return -EIO;
1550 }
1551 
1552 /**
1553  * void jbd2_journal_destroy() - Release a journal_t structure.
1554  * @journal: Journal to act on.
1555  *
1556  * Release a journal_t structure once it is no longer in use by the
1557  * journaled object.
1558  * Return <0 if we couldn't clean up the journal.
1559  */
jbd2_journal_destroy(journal_t * journal)1560 int jbd2_journal_destroy(journal_t *journal)
1561 {
1562 	int err = 0;
1563 
1564 	/* Wait for the commit thread to wake up and die. */
1565 	journal_kill_thread(journal);
1566 
1567 	/* Force a final log commit */
1568 	if (journal->j_running_transaction)
1569 		jbd2_journal_commit_transaction(journal);
1570 
1571 	/* Force any old transactions to disk */
1572 
1573 	/* Totally anal locking here... */
1574 	spin_lock(&journal->j_list_lock);
1575 	while (journal->j_checkpoint_transactions != NULL) {
1576 		spin_unlock(&journal->j_list_lock);
1577 		mutex_lock(&journal->j_checkpoint_mutex);
1578 		jbd2_log_do_checkpoint(journal);
1579 		mutex_unlock(&journal->j_checkpoint_mutex);
1580 		spin_lock(&journal->j_list_lock);
1581 	}
1582 
1583 	J_ASSERT(journal->j_running_transaction == NULL);
1584 	J_ASSERT(journal->j_committing_transaction == NULL);
1585 	J_ASSERT(journal->j_checkpoint_transactions == NULL);
1586 	spin_unlock(&journal->j_list_lock);
1587 
1588 	if (journal->j_sb_buffer) {
1589 		if (!is_journal_aborted(journal)) {
1590 			mutex_lock(&journal->j_checkpoint_mutex);
1591 			jbd2_mark_journal_empty(journal);
1592 			mutex_unlock(&journal->j_checkpoint_mutex);
1593 		} else
1594 			err = -EIO;
1595 		brelse(journal->j_sb_buffer);
1596 	}
1597 
1598 	if (journal->j_proc_entry)
1599 		jbd2_stats_proc_exit(journal);
1600 	if (journal->j_inode)
1601 		iput(journal->j_inode);
1602 	if (journal->j_revoke)
1603 		jbd2_journal_destroy_revoke(journal);
1604 	kfree(journal->j_wbuf);
1605 	kfree(journal);
1606 
1607 	return err;
1608 }
1609 
1610 
1611 /**
1612  *int jbd2_journal_check_used_features () - Check if features specified are used.
1613  * @journal: Journal to check.
1614  * @compat: bitmask of compatible features
1615  * @ro: bitmask of features that force read-only mount
1616  * @incompat: bitmask of incompatible features
1617  *
1618  * Check whether the journal uses all of a given set of
1619  * features.  Return true (non-zero) if it does.
1620  **/
1621 
jbd2_journal_check_used_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)1622 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1623 				 unsigned long ro, unsigned long incompat)
1624 {
1625 	journal_superblock_t *sb;
1626 
1627 	if (!compat && !ro && !incompat)
1628 		return 1;
1629 	/* Load journal superblock if it is not loaded yet. */
1630 	if (journal->j_format_version == 0 &&
1631 	    journal_get_superblock(journal) != 0)
1632 		return 0;
1633 	if (journal->j_format_version == 1)
1634 		return 0;
1635 
1636 	sb = journal->j_superblock;
1637 
1638 	if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1639 	    ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1640 	    ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1641 		return 1;
1642 
1643 	return 0;
1644 }
1645 
1646 /**
1647  * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1648  * @journal: Journal to check.
1649  * @compat: bitmask of compatible features
1650  * @ro: bitmask of features that force read-only mount
1651  * @incompat: bitmask of incompatible features
1652  *
1653  * Check whether the journaling code supports the use of
1654  * all of a given set of features on this journal.  Return true
1655  * (non-zero) if it can. */
1656 
jbd2_journal_check_available_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)1657 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1658 				      unsigned long ro, unsigned long incompat)
1659 {
1660 	if (!compat && !ro && !incompat)
1661 		return 1;
1662 
1663 	/* We can support any known requested features iff the
1664 	 * superblock is in version 2.  Otherwise we fail to support any
1665 	 * extended sb features. */
1666 
1667 	if (journal->j_format_version != 2)
1668 		return 0;
1669 
1670 	if ((compat   & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1671 	    (ro       & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1672 	    (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1673 		return 1;
1674 
1675 	return 0;
1676 }
1677 
1678 /**
1679  * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1680  * @journal: Journal to act on.
1681  * @compat: bitmask of compatible features
1682  * @ro: bitmask of features that force read-only mount
1683  * @incompat: bitmask of incompatible features
1684  *
1685  * Mark a given journal feature as present on the
1686  * superblock.  Returns true if the requested features could be set.
1687  *
1688  */
1689 
jbd2_journal_set_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)1690 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1691 			  unsigned long ro, unsigned long incompat)
1692 {
1693 	journal_superblock_t *sb;
1694 
1695 	if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1696 		return 1;
1697 
1698 	if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1699 		return 0;
1700 
1701 	jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1702 		  compat, ro, incompat);
1703 
1704 	sb = journal->j_superblock;
1705 
1706 	sb->s_feature_compat    |= cpu_to_be32(compat);
1707 	sb->s_feature_ro_compat |= cpu_to_be32(ro);
1708 	sb->s_feature_incompat  |= cpu_to_be32(incompat);
1709 
1710 	return 1;
1711 }
1712 
1713 /*
1714  * jbd2_journal_clear_features () - Clear a given journal feature in the
1715  * 				    superblock
1716  * @journal: Journal to act on.
1717  * @compat: bitmask of compatible features
1718  * @ro: bitmask of features that force read-only mount
1719  * @incompat: bitmask of incompatible features
1720  *
1721  * Clear a given journal feature as present on the
1722  * superblock.
1723  */
jbd2_journal_clear_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)1724 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1725 				unsigned long ro, unsigned long incompat)
1726 {
1727 	journal_superblock_t *sb;
1728 
1729 	jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1730 		  compat, ro, incompat);
1731 
1732 	sb = journal->j_superblock;
1733 
1734 	sb->s_feature_compat    &= ~cpu_to_be32(compat);
1735 	sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1736 	sb->s_feature_incompat  &= ~cpu_to_be32(incompat);
1737 }
1738 EXPORT_SYMBOL(jbd2_journal_clear_features);
1739 
1740 /**
1741  * int jbd2_journal_flush () - Flush journal
1742  * @journal: Journal to act on.
1743  *
1744  * Flush all data for a given journal to disk and empty the journal.
1745  * Filesystems can use this when remounting readonly to ensure that
1746  * recovery does not need to happen on remount.
1747  */
1748 
jbd2_journal_flush(journal_t * journal)1749 int jbd2_journal_flush(journal_t *journal)
1750 {
1751 	int err = 0;
1752 	transaction_t *transaction = NULL;
1753 
1754 	write_lock(&journal->j_state_lock);
1755 
1756 	/* Force everything buffered to the log... */
1757 	if (journal->j_running_transaction) {
1758 		transaction = journal->j_running_transaction;
1759 		__jbd2_log_start_commit(journal, transaction->t_tid);
1760 	} else if (journal->j_committing_transaction)
1761 		transaction = journal->j_committing_transaction;
1762 
1763 	/* Wait for the log commit to complete... */
1764 	if (transaction) {
1765 		tid_t tid = transaction->t_tid;
1766 
1767 		write_unlock(&journal->j_state_lock);
1768 		jbd2_log_wait_commit(journal, tid);
1769 	} else {
1770 		write_unlock(&journal->j_state_lock);
1771 	}
1772 
1773 	/* ...and flush everything in the log out to disk. */
1774 	spin_lock(&journal->j_list_lock);
1775 	while (!err && journal->j_checkpoint_transactions != NULL) {
1776 		spin_unlock(&journal->j_list_lock);
1777 		mutex_lock(&journal->j_checkpoint_mutex);
1778 		err = jbd2_log_do_checkpoint(journal);
1779 		mutex_unlock(&journal->j_checkpoint_mutex);
1780 		spin_lock(&journal->j_list_lock);
1781 	}
1782 	spin_unlock(&journal->j_list_lock);
1783 
1784 	if (is_journal_aborted(journal))
1785 		return -EIO;
1786 
1787 	mutex_lock(&journal->j_checkpoint_mutex);
1788 	jbd2_cleanup_journal_tail(journal);
1789 
1790 	/* Finally, mark the journal as really needing no recovery.
1791 	 * This sets s_start==0 in the underlying superblock, which is
1792 	 * the magic code for a fully-recovered superblock.  Any future
1793 	 * commits of data to the journal will restore the current
1794 	 * s_start value. */
1795 	jbd2_mark_journal_empty(journal);
1796 	mutex_unlock(&journal->j_checkpoint_mutex);
1797 	write_lock(&journal->j_state_lock);
1798 	J_ASSERT(!journal->j_running_transaction);
1799 	J_ASSERT(!journal->j_committing_transaction);
1800 	J_ASSERT(!journal->j_checkpoint_transactions);
1801 	J_ASSERT(journal->j_head == journal->j_tail);
1802 	J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1803 	write_unlock(&journal->j_state_lock);
1804 	return 0;
1805 }
1806 
1807 /**
1808  * int jbd2_journal_wipe() - Wipe journal contents
1809  * @journal: Journal to act on.
1810  * @write: flag (see below)
1811  *
1812  * Wipe out all of the contents of a journal, safely.  This will produce
1813  * a warning if the journal contains any valid recovery information.
1814  * Must be called between journal_init_*() and jbd2_journal_load().
1815  *
1816  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1817  * we merely suppress recovery.
1818  */
1819 
jbd2_journal_wipe(journal_t * journal,int write)1820 int jbd2_journal_wipe(journal_t *journal, int write)
1821 {
1822 	int err = 0;
1823 
1824 	J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1825 
1826 	err = load_superblock(journal);
1827 	if (err)
1828 		return err;
1829 
1830 	if (!journal->j_tail)
1831 		goto no_recovery;
1832 
1833 	printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
1834 		write ? "Clearing" : "Ignoring");
1835 
1836 	err = jbd2_journal_skip_recovery(journal);
1837 	if (write) {
1838 		/* Lock to make assertions happy... */
1839 		mutex_lock(&journal->j_checkpoint_mutex);
1840 		jbd2_mark_journal_empty(journal);
1841 		mutex_unlock(&journal->j_checkpoint_mutex);
1842 	}
1843 
1844  no_recovery:
1845 	return err;
1846 }
1847 
1848 /*
1849  * Journal abort has very specific semantics, which we describe
1850  * for journal abort.
1851  *
1852  * Two internal functions, which provide abort to the jbd layer
1853  * itself are here.
1854  */
1855 
1856 /*
1857  * Quick version for internal journal use (doesn't lock the journal).
1858  * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1859  * and don't attempt to make any other journal updates.
1860  */
__jbd2_journal_abort_hard(journal_t * journal)1861 void __jbd2_journal_abort_hard(journal_t *journal)
1862 {
1863 	transaction_t *transaction;
1864 
1865 	if (journal->j_flags & JBD2_ABORT)
1866 		return;
1867 
1868 	printk(KERN_ERR "Aborting journal on device %s.\n",
1869 	       journal->j_devname);
1870 
1871 	write_lock(&journal->j_state_lock);
1872 	journal->j_flags |= JBD2_ABORT;
1873 	transaction = journal->j_running_transaction;
1874 	if (transaction)
1875 		__jbd2_log_start_commit(journal, transaction->t_tid);
1876 	write_unlock(&journal->j_state_lock);
1877 }
1878 
1879 /* Soft abort: record the abort error status in the journal superblock,
1880  * but don't do any other IO. */
__journal_abort_soft(journal_t * journal,int errno)1881 static void __journal_abort_soft (journal_t *journal, int errno)
1882 {
1883 	if (journal->j_flags & JBD2_ABORT)
1884 		return;
1885 
1886 	if (!journal->j_errno)
1887 		journal->j_errno = errno;
1888 
1889 	__jbd2_journal_abort_hard(journal);
1890 
1891 	if (errno)
1892 		jbd2_journal_update_sb_errno(journal);
1893 }
1894 
1895 /**
1896  * void jbd2_journal_abort () - Shutdown the journal immediately.
1897  * @journal: the journal to shutdown.
1898  * @errno:   an error number to record in the journal indicating
1899  *           the reason for the shutdown.
1900  *
1901  * Perform a complete, immediate shutdown of the ENTIRE
1902  * journal (not of a single transaction).  This operation cannot be
1903  * undone without closing and reopening the journal.
1904  *
1905  * The jbd2_journal_abort function is intended to support higher level error
1906  * recovery mechanisms such as the ext2/ext3 remount-readonly error
1907  * mode.
1908  *
1909  * Journal abort has very specific semantics.  Any existing dirty,
1910  * unjournaled buffers in the main filesystem will still be written to
1911  * disk by bdflush, but the journaling mechanism will be suspended
1912  * immediately and no further transaction commits will be honoured.
1913  *
1914  * Any dirty, journaled buffers will be written back to disk without
1915  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
1916  * filesystem, but we _do_ attempt to leave as much data as possible
1917  * behind for fsck to use for cleanup.
1918  *
1919  * Any attempt to get a new transaction handle on a journal which is in
1920  * ABORT state will just result in an -EROFS error return.  A
1921  * jbd2_journal_stop on an existing handle will return -EIO if we have
1922  * entered abort state during the update.
1923  *
1924  * Recursive transactions are not disturbed by journal abort until the
1925  * final jbd2_journal_stop, which will receive the -EIO error.
1926  *
1927  * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1928  * which will be recorded (if possible) in the journal superblock.  This
1929  * allows a client to record failure conditions in the middle of a
1930  * transaction without having to complete the transaction to record the
1931  * failure to disk.  ext3_error, for example, now uses this
1932  * functionality.
1933  *
1934  * Errors which originate from within the journaling layer will NOT
1935  * supply an errno; a null errno implies that absolutely no further
1936  * writes are done to the journal (unless there are any already in
1937  * progress).
1938  *
1939  */
1940 
jbd2_journal_abort(journal_t * journal,int errno)1941 void jbd2_journal_abort(journal_t *journal, int errno)
1942 {
1943 	__journal_abort_soft(journal, errno);
1944 }
1945 
1946 /**
1947  * int jbd2_journal_errno () - returns the journal's error state.
1948  * @journal: journal to examine.
1949  *
1950  * This is the errno number set with jbd2_journal_abort(), the last
1951  * time the journal was mounted - if the journal was stopped
1952  * without calling abort this will be 0.
1953  *
1954  * If the journal has been aborted on this mount time -EROFS will
1955  * be returned.
1956  */
jbd2_journal_errno(journal_t * journal)1957 int jbd2_journal_errno(journal_t *journal)
1958 {
1959 	int err;
1960 
1961 	read_lock(&journal->j_state_lock);
1962 	if (journal->j_flags & JBD2_ABORT)
1963 		err = -EROFS;
1964 	else
1965 		err = journal->j_errno;
1966 	read_unlock(&journal->j_state_lock);
1967 	return err;
1968 }
1969 
1970 /**
1971  * int jbd2_journal_clear_err () - clears the journal's error state
1972  * @journal: journal to act on.
1973  *
1974  * An error must be cleared or acked to take a FS out of readonly
1975  * mode.
1976  */
jbd2_journal_clear_err(journal_t * journal)1977 int jbd2_journal_clear_err(journal_t *journal)
1978 {
1979 	int err = 0;
1980 
1981 	write_lock(&journal->j_state_lock);
1982 	if (journal->j_flags & JBD2_ABORT)
1983 		err = -EROFS;
1984 	else
1985 		journal->j_errno = 0;
1986 	write_unlock(&journal->j_state_lock);
1987 	return err;
1988 }
1989 
1990 /**
1991  * void jbd2_journal_ack_err() - Ack journal err.
1992  * @journal: journal to act on.
1993  *
1994  * An error must be cleared or acked to take a FS out of readonly
1995  * mode.
1996  */
jbd2_journal_ack_err(journal_t * journal)1997 void jbd2_journal_ack_err(journal_t *journal)
1998 {
1999 	write_lock(&journal->j_state_lock);
2000 	if (journal->j_errno)
2001 		journal->j_flags |= JBD2_ACK_ERR;
2002 	write_unlock(&journal->j_state_lock);
2003 }
2004 
jbd2_journal_blocks_per_page(struct inode * inode)2005 int jbd2_journal_blocks_per_page(struct inode *inode)
2006 {
2007 	return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2008 }
2009 
2010 /*
2011  * helper functions to deal with 32 or 64bit block numbers.
2012  */
journal_tag_bytes(journal_t * journal)2013 size_t journal_tag_bytes(journal_t *journal)
2014 {
2015 	if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2016 		return JBD2_TAG_SIZE64;
2017 	else
2018 		return JBD2_TAG_SIZE32;
2019 }
2020 
2021 /*
2022  * JBD memory management
2023  *
2024  * These functions are used to allocate block-sized chunks of memory
2025  * used for making copies of buffer_head data.  Very often it will be
2026  * page-sized chunks of data, but sometimes it will be in
2027  * sub-page-size chunks.  (For example, 16k pages on Power systems
2028  * with a 4k block file system.)  For blocks smaller than a page, we
2029  * use a SLAB allocator.  There are slab caches for each block size,
2030  * which are allocated at mount time, if necessary, and we only free
2031  * (all of) the slab caches when/if the jbd2 module is unloaded.  For
2032  * this reason we don't need to a mutex to protect access to
2033  * jbd2_slab[] allocating or releasing memory; only in
2034  * jbd2_journal_create_slab().
2035  */
2036 #define JBD2_MAX_SLABS 8
2037 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2038 
2039 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2040 	"jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2041 	"jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2042 };
2043 
2044 
jbd2_journal_destroy_slabs(void)2045 static void jbd2_journal_destroy_slabs(void)
2046 {
2047 	int i;
2048 
2049 	for (i = 0; i < JBD2_MAX_SLABS; i++) {
2050 		if (jbd2_slab[i])
2051 			kmem_cache_destroy(jbd2_slab[i]);
2052 		jbd2_slab[i] = NULL;
2053 	}
2054 }
2055 
jbd2_journal_create_slab(size_t size)2056 static int jbd2_journal_create_slab(size_t size)
2057 {
2058 	static DEFINE_MUTEX(jbd2_slab_create_mutex);
2059 	int i = order_base_2(size) - 10;
2060 	size_t slab_size;
2061 
2062 	if (size == PAGE_SIZE)
2063 		return 0;
2064 
2065 	if (i >= JBD2_MAX_SLABS)
2066 		return -EINVAL;
2067 
2068 	if (unlikely(i < 0))
2069 		i = 0;
2070 	mutex_lock(&jbd2_slab_create_mutex);
2071 	if (jbd2_slab[i]) {
2072 		mutex_unlock(&jbd2_slab_create_mutex);
2073 		return 0;	/* Already created */
2074 	}
2075 
2076 	slab_size = 1 << (i+10);
2077 	jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2078 					 slab_size, 0, NULL);
2079 	mutex_unlock(&jbd2_slab_create_mutex);
2080 	if (!jbd2_slab[i]) {
2081 		printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2082 		return -ENOMEM;
2083 	}
2084 	return 0;
2085 }
2086 
get_slab(size_t size)2087 static struct kmem_cache *get_slab(size_t size)
2088 {
2089 	int i = order_base_2(size) - 10;
2090 
2091 	BUG_ON(i >= JBD2_MAX_SLABS);
2092 	if (unlikely(i < 0))
2093 		i = 0;
2094 	BUG_ON(jbd2_slab[i] == NULL);
2095 	return jbd2_slab[i];
2096 }
2097 
jbd2_alloc(size_t size,gfp_t flags)2098 void *jbd2_alloc(size_t size, gfp_t flags)
2099 {
2100 	void *ptr;
2101 
2102 	BUG_ON(size & (size-1)); /* Must be a power of 2 */
2103 
2104 	flags |= __GFP_REPEAT;
2105 	if (size == PAGE_SIZE)
2106 		ptr = (void *)__get_free_pages(flags, 0);
2107 	else if (size > PAGE_SIZE) {
2108 		int order = get_order(size);
2109 
2110 		if (order < 3)
2111 			ptr = (void *)__get_free_pages(flags, order);
2112 		else
2113 			ptr = vmalloc(size);
2114 	} else
2115 		ptr = kmem_cache_alloc(get_slab(size), flags);
2116 
2117 	/* Check alignment; SLUB has gotten this wrong in the past,
2118 	 * and this can lead to user data corruption! */
2119 	BUG_ON(((unsigned long) ptr) & (size-1));
2120 
2121 	return ptr;
2122 }
2123 
jbd2_free(void * ptr,size_t size)2124 void jbd2_free(void *ptr, size_t size)
2125 {
2126 	if (size == PAGE_SIZE) {
2127 		free_pages((unsigned long)ptr, 0);
2128 		return;
2129 	}
2130 	if (size > PAGE_SIZE) {
2131 		int order = get_order(size);
2132 
2133 		if (order < 3)
2134 			free_pages((unsigned long)ptr, order);
2135 		else
2136 			vfree(ptr);
2137 		return;
2138 	}
2139 	kmem_cache_free(get_slab(size), ptr);
2140 };
2141 
2142 /*
2143  * Journal_head storage management
2144  */
2145 static struct kmem_cache *jbd2_journal_head_cache;
2146 #ifdef CONFIG_JBD2_DEBUG
2147 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2148 #endif
2149 
jbd2_journal_init_journal_head_cache(void)2150 static int jbd2_journal_init_journal_head_cache(void)
2151 {
2152 	int retval;
2153 
2154 	J_ASSERT(jbd2_journal_head_cache == NULL);
2155 	jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2156 				sizeof(struct journal_head),
2157 				0,		/* offset */
2158 				SLAB_TEMPORARY,	/* flags */
2159 				NULL);		/* ctor */
2160 	retval = 0;
2161 	if (!jbd2_journal_head_cache) {
2162 		retval = -ENOMEM;
2163 		printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2164 	}
2165 	return retval;
2166 }
2167 
jbd2_journal_destroy_journal_head_cache(void)2168 static void jbd2_journal_destroy_journal_head_cache(void)
2169 {
2170 	if (jbd2_journal_head_cache) {
2171 		kmem_cache_destroy(jbd2_journal_head_cache);
2172 		jbd2_journal_head_cache = NULL;
2173 	}
2174 }
2175 
2176 /*
2177  * journal_head splicing and dicing
2178  */
journal_alloc_journal_head(void)2179 static struct journal_head *journal_alloc_journal_head(void)
2180 {
2181 	struct journal_head *ret;
2182 
2183 #ifdef CONFIG_JBD2_DEBUG
2184 	atomic_inc(&nr_journal_heads);
2185 #endif
2186 	ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2187 	if (!ret) {
2188 		jbd_debug(1, "out of memory for journal_head\n");
2189 		pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2190 		while (!ret) {
2191 			yield();
2192 			ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2193 		}
2194 	}
2195 	return ret;
2196 }
2197 
journal_free_journal_head(struct journal_head * jh)2198 static void journal_free_journal_head(struct journal_head *jh)
2199 {
2200 #ifdef CONFIG_JBD2_DEBUG
2201 	atomic_dec(&nr_journal_heads);
2202 	memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2203 #endif
2204 	kmem_cache_free(jbd2_journal_head_cache, jh);
2205 }
2206 
2207 /*
2208  * A journal_head is attached to a buffer_head whenever JBD has an
2209  * interest in the buffer.
2210  *
2211  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2212  * is set.  This bit is tested in core kernel code where we need to take
2213  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
2214  * there.
2215  *
2216  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2217  *
2218  * When a buffer has its BH_JBD bit set it is immune from being released by
2219  * core kernel code, mainly via ->b_count.
2220  *
2221  * A journal_head is detached from its buffer_head when the journal_head's
2222  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2223  * transaction (b_cp_transaction) hold their references to b_jcount.
2224  *
2225  * Various places in the kernel want to attach a journal_head to a buffer_head
2226  * _before_ attaching the journal_head to a transaction.  To protect the
2227  * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2228  * journal_head's b_jcount refcount by one.  The caller must call
2229  * jbd2_journal_put_journal_head() to undo this.
2230  *
2231  * So the typical usage would be:
2232  *
2233  *	(Attach a journal_head if needed.  Increments b_jcount)
2234  *	struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2235  *	...
2236  *      (Get another reference for transaction)
2237  *	jbd2_journal_grab_journal_head(bh);
2238  *	jh->b_transaction = xxx;
2239  *	(Put original reference)
2240  *	jbd2_journal_put_journal_head(jh);
2241  */
2242 
2243 /*
2244  * Give a buffer_head a journal_head.
2245  *
2246  * May sleep.
2247  */
jbd2_journal_add_journal_head(struct buffer_head * bh)2248 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2249 {
2250 	struct journal_head *jh;
2251 	struct journal_head *new_jh = NULL;
2252 
2253 repeat:
2254 	if (!buffer_jbd(bh)) {
2255 		new_jh = journal_alloc_journal_head();
2256 		memset(new_jh, 0, sizeof(*new_jh));
2257 	}
2258 
2259 	jbd_lock_bh_journal_head(bh);
2260 	if (buffer_jbd(bh)) {
2261 		jh = bh2jh(bh);
2262 	} else {
2263 		J_ASSERT_BH(bh,
2264 			(atomic_read(&bh->b_count) > 0) ||
2265 			(bh->b_page && bh->b_page->mapping));
2266 
2267 		if (!new_jh) {
2268 			jbd_unlock_bh_journal_head(bh);
2269 			goto repeat;
2270 		}
2271 
2272 		jh = new_jh;
2273 		new_jh = NULL;		/* We consumed it */
2274 		set_buffer_jbd(bh);
2275 		bh->b_private = jh;
2276 		jh->b_bh = bh;
2277 		get_bh(bh);
2278 		BUFFER_TRACE(bh, "added journal_head");
2279 	}
2280 	jh->b_jcount++;
2281 	jbd_unlock_bh_journal_head(bh);
2282 	if (new_jh)
2283 		journal_free_journal_head(new_jh);
2284 	return bh->b_private;
2285 }
2286 
2287 /*
2288  * Grab a ref against this buffer_head's journal_head.  If it ended up not
2289  * having a journal_head, return NULL
2290  */
jbd2_journal_grab_journal_head(struct buffer_head * bh)2291 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2292 {
2293 	struct journal_head *jh = NULL;
2294 
2295 	jbd_lock_bh_journal_head(bh);
2296 	if (buffer_jbd(bh)) {
2297 		jh = bh2jh(bh);
2298 		jh->b_jcount++;
2299 	}
2300 	jbd_unlock_bh_journal_head(bh);
2301 	return jh;
2302 }
2303 
__journal_remove_journal_head(struct buffer_head * bh)2304 static void __journal_remove_journal_head(struct buffer_head *bh)
2305 {
2306 	struct journal_head *jh = bh2jh(bh);
2307 
2308 	J_ASSERT_JH(jh, jh->b_jcount >= 0);
2309 	J_ASSERT_JH(jh, jh->b_transaction == NULL);
2310 	J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2311 	J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2312 	J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2313 	J_ASSERT_BH(bh, buffer_jbd(bh));
2314 	J_ASSERT_BH(bh, jh2bh(jh) == bh);
2315 	BUFFER_TRACE(bh, "remove journal_head");
2316 	if (jh->b_frozen_data) {
2317 		printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2318 		jbd2_free(jh->b_frozen_data, bh->b_size);
2319 	}
2320 	if (jh->b_committed_data) {
2321 		printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2322 		jbd2_free(jh->b_committed_data, bh->b_size);
2323 	}
2324 	bh->b_private = NULL;
2325 	jh->b_bh = NULL;	/* debug, really */
2326 	clear_buffer_jbd(bh);
2327 	journal_free_journal_head(jh);
2328 }
2329 
2330 /*
2331  * Drop a reference on the passed journal_head.  If it fell to zero then
2332  * release the journal_head from the buffer_head.
2333  */
jbd2_journal_put_journal_head(struct journal_head * jh)2334 void jbd2_journal_put_journal_head(struct journal_head *jh)
2335 {
2336 	struct buffer_head *bh = jh2bh(jh);
2337 
2338 	jbd_lock_bh_journal_head(bh);
2339 	J_ASSERT_JH(jh, jh->b_jcount > 0);
2340 	--jh->b_jcount;
2341 	if (!jh->b_jcount) {
2342 		__journal_remove_journal_head(bh);
2343 		jbd_unlock_bh_journal_head(bh);
2344 		__brelse(bh);
2345 	} else
2346 		jbd_unlock_bh_journal_head(bh);
2347 }
2348 
2349 /*
2350  * Initialize jbd inode head
2351  */
jbd2_journal_init_jbd_inode(struct jbd2_inode * jinode,struct inode * inode)2352 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2353 {
2354 	jinode->i_transaction = NULL;
2355 	jinode->i_next_transaction = NULL;
2356 	jinode->i_vfs_inode = inode;
2357 	jinode->i_flags = 0;
2358 	INIT_LIST_HEAD(&jinode->i_list);
2359 }
2360 
2361 /*
2362  * Function to be called before we start removing inode from memory (i.e.,
2363  * clear_inode() is a fine place to be called from). It removes inode from
2364  * transaction's lists.
2365  */
jbd2_journal_release_jbd_inode(journal_t * journal,struct jbd2_inode * jinode)2366 void jbd2_journal_release_jbd_inode(journal_t *journal,
2367 				    struct jbd2_inode *jinode)
2368 {
2369 	if (!journal)
2370 		return;
2371 restart:
2372 	spin_lock(&journal->j_list_lock);
2373 	/* Is commit writing out inode - we have to wait */
2374 	if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2375 		wait_queue_head_t *wq;
2376 		DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2377 		wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2378 		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2379 		spin_unlock(&journal->j_list_lock);
2380 		schedule();
2381 		finish_wait(wq, &wait.wait);
2382 		goto restart;
2383 	}
2384 
2385 	if (jinode->i_transaction) {
2386 		list_del(&jinode->i_list);
2387 		jinode->i_transaction = NULL;
2388 	}
2389 	spin_unlock(&journal->j_list_lock);
2390 }
2391 
2392 /*
2393  * debugfs tunables
2394  */
2395 #ifdef CONFIG_JBD2_DEBUG
2396 u8 jbd2_journal_enable_debug __read_mostly;
2397 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2398 
2399 #define JBD2_DEBUG_NAME "jbd2-debug"
2400 
2401 static struct dentry *jbd2_debugfs_dir;
2402 static struct dentry *jbd2_debug;
2403 
jbd2_create_debugfs_entry(void)2404 static void __init jbd2_create_debugfs_entry(void)
2405 {
2406 	jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2407 	if (jbd2_debugfs_dir)
2408 		jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2409 					       S_IRUGO | S_IWUSR,
2410 					       jbd2_debugfs_dir,
2411 					       &jbd2_journal_enable_debug);
2412 }
2413 
jbd2_remove_debugfs_entry(void)2414 static void __exit jbd2_remove_debugfs_entry(void)
2415 {
2416 	debugfs_remove(jbd2_debug);
2417 	debugfs_remove(jbd2_debugfs_dir);
2418 }
2419 
2420 #else
2421 
jbd2_create_debugfs_entry(void)2422 static void __init jbd2_create_debugfs_entry(void)
2423 {
2424 }
2425 
jbd2_remove_debugfs_entry(void)2426 static void __exit jbd2_remove_debugfs_entry(void)
2427 {
2428 }
2429 
2430 #endif
2431 
2432 #ifdef CONFIG_PROC_FS
2433 
2434 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2435 
jbd2_create_jbd_stats_proc_entry(void)2436 static void __init jbd2_create_jbd_stats_proc_entry(void)
2437 {
2438 	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2439 }
2440 
jbd2_remove_jbd_stats_proc_entry(void)2441 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2442 {
2443 	if (proc_jbd2_stats)
2444 		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2445 }
2446 
2447 #else
2448 
2449 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2450 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2451 
2452 #endif
2453 
2454 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2455 
jbd2_journal_init_handle_cache(void)2456 static int __init jbd2_journal_init_handle_cache(void)
2457 {
2458 	jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2459 	if (jbd2_handle_cache == NULL) {
2460 		printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2461 		return -ENOMEM;
2462 	}
2463 	jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2464 	if (jbd2_inode_cache == NULL) {
2465 		printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2466 		kmem_cache_destroy(jbd2_handle_cache);
2467 		return -ENOMEM;
2468 	}
2469 	return 0;
2470 }
2471 
jbd2_journal_destroy_handle_cache(void)2472 static void jbd2_journal_destroy_handle_cache(void)
2473 {
2474 	if (jbd2_handle_cache)
2475 		kmem_cache_destroy(jbd2_handle_cache);
2476 	if (jbd2_inode_cache)
2477 		kmem_cache_destroy(jbd2_inode_cache);
2478 
2479 }
2480 
2481 /*
2482  * Module startup and shutdown
2483  */
2484 
journal_init_caches(void)2485 static int __init journal_init_caches(void)
2486 {
2487 	int ret;
2488 
2489 	ret = jbd2_journal_init_revoke_caches();
2490 	if (ret == 0)
2491 		ret = jbd2_journal_init_journal_head_cache();
2492 	if (ret == 0)
2493 		ret = jbd2_journal_init_handle_cache();
2494 	if (ret == 0)
2495 		ret = jbd2_journal_init_transaction_cache();
2496 	return ret;
2497 }
2498 
jbd2_journal_destroy_caches(void)2499 static void jbd2_journal_destroy_caches(void)
2500 {
2501 	jbd2_journal_destroy_revoke_caches();
2502 	jbd2_journal_destroy_journal_head_cache();
2503 	jbd2_journal_destroy_handle_cache();
2504 	jbd2_journal_destroy_transaction_cache();
2505 	jbd2_journal_destroy_slabs();
2506 }
2507 
journal_init(void)2508 static int __init journal_init(void)
2509 {
2510 	int ret;
2511 
2512 	BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2513 
2514 	ret = journal_init_caches();
2515 	if (ret == 0) {
2516 		jbd2_create_debugfs_entry();
2517 		jbd2_create_jbd_stats_proc_entry();
2518 	} else {
2519 		jbd2_journal_destroy_caches();
2520 	}
2521 	return ret;
2522 }
2523 
journal_exit(void)2524 static void __exit journal_exit(void)
2525 {
2526 #ifdef CONFIG_JBD2_DEBUG
2527 	int n = atomic_read(&nr_journal_heads);
2528 	if (n)
2529 		printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
2530 #endif
2531 	jbd2_remove_debugfs_entry();
2532 	jbd2_remove_jbd_stats_proc_entry();
2533 	jbd2_journal_destroy_caches();
2534 }
2535 
2536 MODULE_LICENSE("GPL");
2537 module_init(journal_init);
2538 module_exit(journal_exit);
2539 
2540