Branch data Line data Source code
1 : : /*
2 : : * linux/fs/jbd/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/jbd.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/ratelimit.h>
40 : :
41 : : #define CREATE_TRACE_POINTS
42 : : #include <trace/events/jbd.h>
43 : :
44 : : #include <asm/uaccess.h>
45 : : #include <asm/page.h>
46 : :
47 : : EXPORT_SYMBOL(journal_start);
48 : : EXPORT_SYMBOL(journal_restart);
49 : : EXPORT_SYMBOL(journal_extend);
50 : : EXPORT_SYMBOL(journal_stop);
51 : : EXPORT_SYMBOL(journal_lock_updates);
52 : : EXPORT_SYMBOL(journal_unlock_updates);
53 : : EXPORT_SYMBOL(journal_get_write_access);
54 : : EXPORT_SYMBOL(journal_get_create_access);
55 : : EXPORT_SYMBOL(journal_get_undo_access);
56 : : EXPORT_SYMBOL(journal_dirty_data);
57 : : EXPORT_SYMBOL(journal_dirty_metadata);
58 : : EXPORT_SYMBOL(journal_release_buffer);
59 : : EXPORT_SYMBOL(journal_forget);
60 : : #if 0
61 : : EXPORT_SYMBOL(journal_sync_buffer);
62 : : #endif
63 : : EXPORT_SYMBOL(journal_flush);
64 : : EXPORT_SYMBOL(journal_revoke);
65 : :
66 : : EXPORT_SYMBOL(journal_init_dev);
67 : : EXPORT_SYMBOL(journal_init_inode);
68 : : EXPORT_SYMBOL(journal_update_format);
69 : : EXPORT_SYMBOL(journal_check_used_features);
70 : : EXPORT_SYMBOL(journal_check_available_features);
71 : : EXPORT_SYMBOL(journal_set_features);
72 : : EXPORT_SYMBOL(journal_create);
73 : : EXPORT_SYMBOL(journal_load);
74 : : EXPORT_SYMBOL(journal_destroy);
75 : : EXPORT_SYMBOL(journal_abort);
76 : : EXPORT_SYMBOL(journal_errno);
77 : : EXPORT_SYMBOL(journal_ack_err);
78 : : EXPORT_SYMBOL(journal_clear_err);
79 : : EXPORT_SYMBOL(log_wait_commit);
80 : : EXPORT_SYMBOL(log_start_commit);
81 : : EXPORT_SYMBOL(journal_start_commit);
82 : : EXPORT_SYMBOL(journal_force_commit_nested);
83 : : EXPORT_SYMBOL(journal_wipe);
84 : : EXPORT_SYMBOL(journal_blocks_per_page);
85 : : EXPORT_SYMBOL(journal_invalidatepage);
86 : : EXPORT_SYMBOL(journal_try_to_free_buffers);
87 : : EXPORT_SYMBOL(journal_force_commit);
88 : :
89 : : static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90 : : static void __journal_abort_soft (journal_t *journal, int errno);
91 : : static const char *journal_dev_name(journal_t *journal, char *buffer);
92 : :
93 : : #ifdef CONFIG_JBD_DEBUG
94 : : void __jbd_debug(int level, const char *file, const char *func,
95 : : unsigned int line, const char *fmt, ...)
96 : : {
97 : : struct va_format vaf;
98 : : va_list args;
99 : :
100 : : if (level > journal_enable_debug)
101 : : return;
102 : : va_start(args, fmt);
103 : : vaf.fmt = fmt;
104 : : vaf.va = &args;
105 : : printk(KERN_DEBUG "%s: (%s, %u): %pV\n", file, func, line, &vaf);
106 : : va_end(args);
107 : : }
108 : : EXPORT_SYMBOL(__jbd_debug);
109 : : #endif
110 : :
111 : : /*
112 : : * Helper function used to manage commit timeouts
113 : : */
114 : :
115 : 0 : static void commit_timeout(unsigned long __data)
116 : : {
117 : 0 : struct task_struct * p = (struct task_struct *) __data;
118 : :
119 : 0 : wake_up_process(p);
120 : 0 : }
121 : :
122 : : /*
123 : : * kjournald: The main thread function used to manage a logging device
124 : : * journal.
125 : : *
126 : : * This kernel thread is responsible for two things:
127 : : *
128 : : * 1) COMMIT: Every so often we need to commit the current state of the
129 : : * filesystem to disk. The journal thread is responsible for writing
130 : : * all of the metadata buffers to disk.
131 : : *
132 : : * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
133 : : * of the data in that part of the log has been rewritten elsewhere on
134 : : * the disk. Flushing these old buffers to reclaim space in the log is
135 : : * known as checkpointing, and this thread is responsible for that job.
136 : : */
137 : :
138 : 0 : static int kjournald(void *arg)
139 : : {
140 : : journal_t *journal = arg;
141 : : transaction_t *transaction;
142 : :
143 : : /*
144 : : * Set up an interval timer which can be used to trigger a commit wakeup
145 : : * after the commit interval expires
146 : : */
147 : 2 : setup_timer(&journal->j_commit_timer, commit_timeout,
148 : : (unsigned long)current);
149 : :
150 : 2 : set_freezable();
151 : :
152 : : /* Record that the journal thread is running */
153 : 2 : journal->j_task = current;
154 : 2 : wake_up(&journal->j_wait_done_commit);
155 : :
156 : 2 : printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
157 : 2 : journal->j_commit_interval / HZ);
158 : :
159 : : /*
160 : : * And now, wait forever for commit wakeup events.
161 : : */
162 : : spin_lock(&journal->j_state_lock);
163 : :
164 : : loop:
165 [ + + ]: 6 : if (journal->j_flags & JFS_UNMOUNT)
166 : : goto end_loop;
167 : :
168 : : jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
169 : : journal->j_commit_sequence, journal->j_commit_request);
170 : :
171 [ + + ]: 4 : if (journal->j_commit_sequence != journal->j_commit_request) {
172 : : jbd_debug(1, "OK, requests differ\n");
173 : : spin_unlock(&journal->j_state_lock);
174 : 1 : del_timer_sync(&journal->j_commit_timer);
175 : 1 : journal_commit_transaction(journal);
176 : : spin_lock(&journal->j_state_lock);
177 : : goto loop;
178 : : }
179 : :
180 : 3 : wake_up(&journal->j_wait_done_commit);
181 [ - + ]: 3 : if (freezing(current)) {
182 : : /*
183 : : * The simpler the better. Flushing journal isn't a
184 : : * good idea, because that depends on threads that may
185 : : * be already stopped.
186 : : */
187 : : jbd_debug(1, "Now suspending kjournald\n");
188 : : spin_unlock(&journal->j_state_lock);
189 : : try_to_freeze();
190 : : spin_lock(&journal->j_state_lock);
191 : : } else {
192 : : /*
193 : : * We assume on resume that commits are already there,
194 : : * so we don't sleep
195 : : */
196 : 6 : DEFINE_WAIT(wait);
197 : : int should_sleep = 1;
198 : :
199 : 3 : prepare_to_wait(&journal->j_wait_commit, &wait,
200 : : TASK_INTERRUPTIBLE);
201 [ - + ]: 3 : if (journal->j_commit_sequence != journal->j_commit_request)
202 : : should_sleep = 0;
203 : 3 : transaction = journal->j_running_transaction;
204 [ - + ][ # # ]: 3 : if (transaction && time_after_eq(jiffies,
205 : : transaction->t_expires))
206 : : should_sleep = 0;
207 [ - + ]: 3 : if (journal->j_flags & JFS_UNMOUNT)
208 : : should_sleep = 0;
209 [ + ]: 3 : if (should_sleep) {
210 : : spin_unlock(&journal->j_state_lock);
211 : 3 : schedule();
212 : : spin_lock(&journal->j_state_lock);
213 : : }
214 : 1 : finish_wait(&journal->j_wait_commit, &wait);
215 : : }
216 : :
217 : : jbd_debug(1, "kjournald wakes\n");
218 : :
219 : : /*
220 : : * Were we woken up by a commit wakeup event?
221 : : */
222 : 3 : transaction = journal->j_running_transaction;
223 [ + + ][ + - ]: 3 : if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
224 : 0 : journal->j_commit_request = transaction->t_tid;
225 : : jbd_debug(1, "woke because of timeout\n");
226 : : }
227 : : goto loop;
228 : :
229 : : end_loop:
230 : : spin_unlock(&journal->j_state_lock);
231 : 2 : del_timer_sync(&journal->j_commit_timer);
232 : 2 : journal->j_task = NULL;
233 : 2 : wake_up(&journal->j_wait_done_commit);
234 : : jbd_debug(1, "Journal thread exiting.\n");
235 : 2 : return 0;
236 : : }
237 : :
238 : 0 : static int journal_start_thread(journal_t *journal)
239 : : {
240 : : struct task_struct *t;
241 : :
242 [ + - ]: 2 : t = kthread_run(kjournald, journal, "kjournald");
243 [ - + ]: 2 : if (IS_ERR(t))
244 : 0 : return PTR_ERR(t);
245 : :
246 [ + - ][ + + ]: 4 : wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
247 : : return 0;
248 : : }
249 : :
250 : 0 : static void journal_kill_thread(journal_t *journal)
251 : : {
252 : : spin_lock(&journal->j_state_lock);
253 : 2 : journal->j_flags |= JFS_UNMOUNT;
254 : :
255 [ + + ]: 4 : while (journal->j_task) {
256 : 2 : wake_up(&journal->j_wait_commit);
257 : : spin_unlock(&journal->j_state_lock);
258 [ + - ][ + + ]: 4 : wait_event(journal->j_wait_done_commit,
259 : : journal->j_task == NULL);
260 : : spin_lock(&journal->j_state_lock);
261 : : }
262 : : spin_unlock(&journal->j_state_lock);
263 : 2 : }
264 : :
265 : : /*
266 : : * journal_write_metadata_buffer: write a metadata buffer to the journal.
267 : : *
268 : : * Writes a metadata buffer to a given disk block. The actual IO is not
269 : : * performed but a new buffer_head is constructed which labels the data
270 : : * to be written with the correct destination disk block.
271 : : *
272 : : * Any magic-number escaping which needs to be done will cause a
273 : : * copy-out here. If the buffer happens to start with the
274 : : * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
275 : : * magic number is only written to the log for descripter blocks. In
276 : : * this case, we copy the data and replace the first word with 0, and we
277 : : * return a result code which indicates that this buffer needs to be
278 : : * marked as an escaped buffer in the corresponding log descriptor
279 : : * block. The missing word can then be restored when the block is read
280 : : * during recovery.
281 : : *
282 : : * If the source buffer has already been modified by a new transaction
283 : : * since we took the last commit snapshot, we use the frozen copy of
284 : : * that data for IO. If we end up using the existing buffer_head's data
285 : : * for the write, then we *have* to lock the buffer to prevent anyone
286 : : * else from using and possibly modifying it while the IO is in
287 : : * progress.
288 : : *
289 : : * The function returns a pointer to the buffer_heads to be used for IO.
290 : : *
291 : : * We assume that the journal has already been locked in this function.
292 : : *
293 : : * Return value:
294 : : * <0: Error
295 : : * >=0: Finished OK
296 : : *
297 : : * On success:
298 : : * Bit 0 set == escape performed on the data
299 : : * Bit 1 set == buffer copy-out performed (kfree the data after IO)
300 : : */
301 : :
302 : 0 : int journal_write_metadata_buffer(transaction_t *transaction,
303 : 27 : struct journal_head *jh_in,
304 : : struct journal_head **jh_out,
305 : : unsigned int blocknr)
306 : : {
307 : : int need_copy_out = 0;
308 : : int done_copy_out = 0;
309 : : int do_escape = 0;
310 : : char *mapped_data;
311 : : struct buffer_head *new_bh;
312 : : struct journal_head *new_jh;
313 : : struct page *new_page;
314 : : unsigned int new_offset;
315 : : struct buffer_head *bh_in = jh2bh(jh_in);
316 : 9 : journal_t *journal = transaction->t_journal;
317 : :
318 : : /*
319 : : * The buffer really shouldn't be locked: only the current committing
320 : : * transaction is allowed to write it, so nobody else is allowed
321 : : * to do any IO.
322 : : *
323 : : * akpm: except if we're journalling data, and write() output is
324 : : * also part of a shared mapping, and another thread has
325 : : * decided to launch a writepage() against this buffer.
326 : : */
327 [ - + ]: 9 : J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
328 : :
329 : 9 : new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
330 : : /* keep subsequent assertions sane */
331 : 9 : atomic_set(&new_bh->b_count, 1);
332 : 9 : new_jh = journal_add_journal_head(new_bh); /* This sleeps */
333 : :
334 : : /*
335 : : * If a new transaction has already done a buffer copy-out, then
336 : : * we use that version of the data for the commit.
337 : : */
338 : : jbd_lock_bh_state(bh_in);
339 : : repeat:
340 [ - + ]: 9 : if (jh_in->b_frozen_data) {
341 : : done_copy_out = 1;
342 : 0 : new_page = virt_to_page(jh_in->b_frozen_data);
343 : 0 : new_offset = offset_in_page(jh_in->b_frozen_data);
344 : : } else {
345 : 9 : new_page = jh2bh(jh_in)->b_page;
346 : 9 : new_offset = offset_in_page(jh2bh(jh_in)->b_data);
347 : : }
348 : :
349 : 9 : mapped_data = kmap_atomic(new_page);
350 : : /*
351 : : * Check for escaping
352 : : */
353 [ - + ]: 9 : if (*((__be32 *)(mapped_data + new_offset)) ==
354 : : cpu_to_be32(JFS_MAGIC_NUMBER)) {
355 : : need_copy_out = 1;
356 : : do_escape = 1;
357 : : }
358 : 9 : kunmap_atomic(mapped_data);
359 : :
360 : : /*
361 : : * Do we need to do a data copy?
362 : : */
363 [ - + ]: 9 : if (need_copy_out && !done_copy_out) {
364 : : char *tmp;
365 : :
366 : : jbd_unlock_bh_state(bh_in);
367 : 0 : tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
368 : : jbd_lock_bh_state(bh_in);
369 [ # # ]: 0 : if (jh_in->b_frozen_data) {
370 : 0 : jbd_free(tmp, bh_in->b_size);
371 : : goto repeat;
372 : : }
373 : :
374 : 0 : jh_in->b_frozen_data = tmp;
375 : 0 : mapped_data = kmap_atomic(new_page);
376 : 0 : memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
377 : 0 : kunmap_atomic(mapped_data);
378 : :
379 : 0 : new_page = virt_to_page(tmp);
380 : 0 : new_offset = offset_in_page(tmp);
381 : : done_copy_out = 1;
382 : : }
383 : :
384 : : /*
385 : : * Did we need to do an escaping? Now we've done all the
386 : : * copying, we can finally do so.
387 : : */
388 [ - + ]: 9 : if (do_escape) {
389 : 0 : mapped_data = kmap_atomic(new_page);
390 : 0 : *((unsigned int *)(mapped_data + new_offset)) = 0;
391 : 0 : kunmap_atomic(mapped_data);
392 : : }
393 : :
394 : 9 : set_bh_page(new_bh, new_page, new_offset);
395 : 9 : new_jh->b_transaction = NULL;
396 : 9 : new_bh->b_size = jh2bh(jh_in)->b_size;
397 : 9 : new_bh->b_bdev = transaction->t_journal->j_dev;
398 : 9 : new_bh->b_blocknr = blocknr;
399 : : set_buffer_mapped(new_bh);
400 : : set_buffer_dirty(new_bh);
401 : :
402 : 9 : *jh_out = new_jh;
403 : :
404 : : /*
405 : : * The to-be-written buffer needs to get moved to the io queue,
406 : : * and the original buffer whose contents we are shadowing or
407 : : * copying is moved to the transaction's shadow queue.
408 : : */
409 : : JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
410 : : spin_lock(&journal->j_list_lock);
411 : 9 : __journal_file_buffer(jh_in, transaction, BJ_Shadow);
412 : : spin_unlock(&journal->j_list_lock);
413 : : jbd_unlock_bh_state(bh_in);
414 : :
415 : : JBUFFER_TRACE(new_jh, "file as BJ_IO");
416 : 9 : journal_file_buffer(new_jh, transaction, BJ_IO);
417 : :
418 : 9 : return do_escape | (done_copy_out << 1);
419 : : }
420 : :
421 : : /*
422 : : * Allocation code for the journal file. Manage the space left in the
423 : : * journal, so that we can begin checkpointing when appropriate.
424 : : */
425 : :
426 : : /*
427 : : * __log_space_left: Return the number of free blocks left in the journal.
428 : : *
429 : : * Called with the journal already locked.
430 : : *
431 : : * Called under j_state_lock
432 : : */
433 : :
434 : 0 : int __log_space_left(journal_t *journal)
435 : : {
436 : 45 : int left = journal->j_free;
437 : :
438 [ - + ]: 45 : assert_spin_locked(&journal->j_state_lock);
439 : :
440 : : /*
441 : : * Be pessimistic here about the number of those free blocks which
442 : : * might be required for log descriptor control blocks.
443 : : */
444 : :
445 : : #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
446 : :
447 : 45 : left -= MIN_LOG_RESERVED_BLOCKS;
448 : :
449 [ + - ]: 45 : if (left <= 0)
450 : : return 0;
451 : 45 : left -= (left >> 3);
452 : 45 : return left;
453 : : }
454 : :
455 : : /*
456 : : * Called under j_state_lock. Returns true if a transaction commit was started.
457 : : */
458 : 0 : int __log_start_commit(journal_t *journal, tid_t target)
459 : : {
460 : : /*
461 : : * The only transaction we can possibly wait upon is the
462 : : * currently running transaction (if it exists). Otherwise,
463 : : * the target tid must be an old one.
464 : : */
465 [ + - ][ + - ]: 1 : if (journal->j_commit_request != target &&
466 [ + - ]: 1 : journal->j_running_transaction &&
467 : 1 : journal->j_running_transaction->t_tid == target) {
468 : : /*
469 : : * We want a new commit: OK, mark the request and wakeup the
470 : : * commit thread. We do _not_ do the commit ourselves.
471 : : */
472 : :
473 : 1 : journal->j_commit_request = target;
474 : : jbd_debug(1, "JBD: requesting commit %d/%d\n",
475 : : journal->j_commit_request,
476 : : journal->j_commit_sequence);
477 : 1 : wake_up(&journal->j_wait_commit);
478 : 1 : return 1;
479 [ # # ]: 0 : } else if (!tid_geq(journal->j_commit_request, target))
480 : : /* This should never happen, but if it does, preserve
481 : : the evidence before kjournald goes into a loop and
482 : : increments j_commit_sequence beyond all recognition. */
483 [ # # ][ # # ]: 0 : WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
[ # # ]
484 : : journal->j_commit_request, journal->j_commit_sequence,
485 : : target, journal->j_running_transaction ?
486 : : journal->j_running_transaction->t_tid : 0);
487 : : return 0;
488 : : }
489 : :
490 : 0 : int log_start_commit(journal_t *journal, tid_t tid)
491 : : {
492 : : int ret;
493 : :
494 : : spin_lock(&journal->j_state_lock);
495 : 0 : ret = __log_start_commit(journal, tid);
496 : : spin_unlock(&journal->j_state_lock);
497 : 0 : return ret;
498 : : }
499 : :
500 : : /*
501 : : * Force and wait upon a commit if the calling process is not within
502 : : * transaction. This is used for forcing out undo-protected data which contains
503 : : * bitmaps, when the fs is running out of space.
504 : : *
505 : : * We can only force the running transaction if we don't have an active handle;
506 : : * otherwise, we will deadlock.
507 : : *
508 : : * Returns true if a transaction was started.
509 : : */
510 : 0 : int journal_force_commit_nested(journal_t *journal)
511 : : {
512 : : transaction_t *transaction = NULL;
513 : : tid_t tid;
514 : :
515 : : spin_lock(&journal->j_state_lock);
516 [ # # ][ # # ]: 0 : if (journal->j_running_transaction && !current->journal_info) {
517 : : transaction = journal->j_running_transaction;
518 : 0 : __log_start_commit(journal, transaction->t_tid);
519 [ # # ]: 0 : } else if (journal->j_committing_transaction)
520 : : transaction = journal->j_committing_transaction;
521 : :
522 [ # # ]: 0 : if (!transaction) {
523 : : spin_unlock(&journal->j_state_lock);
524 : 0 : return 0; /* Nothing to retry */
525 : : }
526 : :
527 : 0 : tid = transaction->t_tid;
528 : : spin_unlock(&journal->j_state_lock);
529 : 0 : log_wait_commit(journal, tid);
530 : 0 : return 1;
531 : : }
532 : :
533 : : /*
534 : : * Start a commit of the current running transaction (if any). Returns true
535 : : * if a transaction is going to be committed (or is currently already
536 : : * committing), and fills its tid in at *ptid
537 : : */
538 : 0 : int journal_start_commit(journal_t *journal, tid_t *ptid)
539 : : {
540 : : int ret = 0;
541 : :
542 : : spin_lock(&journal->j_state_lock);
543 [ + + ]: 4 : if (journal->j_running_transaction) {
544 : 1 : tid_t tid = journal->j_running_transaction->t_tid;
545 : :
546 : 1 : __log_start_commit(journal, tid);
547 : : /* There's a running transaction and we've just made sure
548 : : * it's commit has been scheduled. */
549 [ + - ]: 1 : if (ptid)
550 : 1 : *ptid = tid;
551 : : ret = 1;
552 [ + + ]: 3 : } else if (journal->j_committing_transaction) {
553 : : /*
554 : : * If commit has been started, then we have to wait for
555 : : * completion of that transaction.
556 : : */
557 [ + - ]: 1 : if (ptid)
558 : 1 : *ptid = journal->j_committing_transaction->t_tid;
559 : : ret = 1;
560 : : }
561 : : spin_unlock(&journal->j_state_lock);
562 : 4 : return ret;
563 : : }
564 : :
565 : : /*
566 : : * Wait for a specified commit to complete.
567 : : * The caller may not hold the journal lock.
568 : : */
569 : 0 : int log_wait_commit(journal_t *journal, tid_t tid)
570 : : {
571 : : int err = 0;
572 : :
573 : : #ifdef CONFIG_JBD_DEBUG
574 : : spin_lock(&journal->j_state_lock);
575 : : if (!tid_geq(journal->j_commit_request, tid)) {
576 : : printk(KERN_ERR
577 : : "%s: error: j_commit_request=%d, tid=%d\n",
578 : : __func__, journal->j_commit_request, tid);
579 : : }
580 : : spin_unlock(&journal->j_state_lock);
581 : : #endif
582 : : spin_lock(&journal->j_state_lock);
583 : : /*
584 : : * Not running or committing trans? Must be already committed. This
585 : : * saves us from waiting for a *long* time when tid overflows.
586 : : */
587 [ - + ][ # # ]: 1 : if (!((journal->j_running_transaction &&
[ + - ]
588 : 0 : journal->j_running_transaction->t_tid == tid) ||
589 [ + - ]: 1 : (journal->j_committing_transaction &&
590 : 1 : journal->j_committing_transaction->t_tid == tid)))
591 : : goto out_unlock;
592 : :
593 [ + - ]: 1 : if (!tid_geq(journal->j_commit_waited, tid))
594 : 1 : journal->j_commit_waited = tid;
595 [ + + ]: 2 : while (tid_gt(tid, journal->j_commit_sequence)) {
596 : : jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
597 : : tid, journal->j_commit_sequence);
598 : 1 : wake_up(&journal->j_wait_commit);
599 : : spin_unlock(&journal->j_state_lock);
600 [ + - ][ + + ]: 2 : wait_event(journal->j_wait_done_commit,
601 : : !tid_gt(tid, journal->j_commit_sequence));
602 : : spin_lock(&journal->j_state_lock);
603 : : }
604 : : out_unlock:
605 : : spin_unlock(&journal->j_state_lock);
606 : :
607 [ - + ]: 1 : if (unlikely(is_journal_aborted(journal)))
608 : : err = -EIO;
609 : 1 : return err;
610 : : }
611 : :
612 : : /*
613 : : * Return 1 if a given transaction has not yet sent barrier request
614 : : * connected with a transaction commit. If 0 is returned, transaction
615 : : * may or may not have sent the barrier. Used to avoid sending barrier
616 : : * twice in common cases.
617 : : */
618 : 0 : int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
619 : : {
620 : : int ret = 0;
621 : : transaction_t *commit_trans;
622 : :
623 [ # # ]: 0 : if (!(journal->j_flags & JFS_BARRIER))
624 : : return 0;
625 : : spin_lock(&journal->j_state_lock);
626 : : /* Transaction already committed? */
627 [ # # ]: 0 : if (tid_geq(journal->j_commit_sequence, tid))
628 : : goto out;
629 : : /*
630 : : * Transaction is being committed and we already proceeded to
631 : : * writing commit record?
632 : : */
633 : 0 : commit_trans = journal->j_committing_transaction;
634 [ # # ][ # # ]: 0 : if (commit_trans && commit_trans->t_tid == tid &&
[ # # ]
635 : 0 : commit_trans->t_state >= T_COMMIT_RECORD)
636 : : goto out;
637 : : ret = 1;
638 : : out:
639 : : spin_unlock(&journal->j_state_lock);
640 : 0 : return ret;
641 : : }
642 : : EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
643 : :
644 : : /*
645 : : * Log buffer allocation routines:
646 : : */
647 : :
648 : 0 : int journal_next_log_block(journal_t *journal, unsigned int *retp)
649 : : {
650 : : unsigned int blocknr;
651 : :
652 : : spin_lock(&journal->j_state_lock);
653 [ - + ]: 12 : J_ASSERT(journal->j_free > 1);
654 : :
655 : 12 : blocknr = journal->j_head;
656 : 12 : journal->j_head++;
657 : 12 : journal->j_free--;
658 [ - + ]: 12 : if (journal->j_head == journal->j_last)
659 : 0 : journal->j_head = journal->j_first;
660 : : spin_unlock(&journal->j_state_lock);
661 : 12 : return journal_bmap(journal, blocknr, retp);
662 : : }
663 : :
664 : : /*
665 : : * Conversion of logical to physical block numbers for the journal
666 : : *
667 : : * On external journals the journal blocks are identity-mapped, so
668 : : * this is a no-op. If needed, we can use j_blk_offset - everything is
669 : : * ready.
670 : : */
671 : 0 : int journal_bmap(journal_t *journal, unsigned int blocknr,
672 : : unsigned int *retp)
673 : : {
674 : : int err = 0;
675 : : unsigned int ret;
676 : :
677 [ + - ]: 14 : if (journal->j_inode) {
678 : 14 : ret = bmap(journal->j_inode, blocknr);
679 [ + - ]: 28 : if (ret)
680 : 14 : *retp = ret;
681 : : else {
682 : : char b[BDEVNAME_SIZE];
683 : :
684 : 0 : printk(KERN_ALERT "%s: journal block not found "
685 : : "at offset %u on %s\n",
686 : : __func__,
687 : : blocknr,
688 : : bdevname(journal->j_dev, b));
689 : : err = -EIO;
690 : 0 : __journal_abort_soft(journal, err);
691 : : }
692 : : } else {
693 : 0 : *retp = blocknr; /* +journal->j_blk_offset */
694 : : }
695 : 14 : return err;
696 : : }
697 : :
698 : : /*
699 : : * We play buffer_head aliasing tricks to write data/metadata blocks to
700 : : * the journal without copying their contents, but for journal
701 : : * descriptor blocks we do need to generate bona fide buffers.
702 : : *
703 : : * After the caller of journal_get_descriptor_buffer() has finished modifying
704 : : * the buffer's contents they really should run flush_dcache_page(bh->b_page).
705 : : * But we don't bother doing that, so there will be coherency problems with
706 : : * mmaps of blockdevs which hold live JBD-controlled filesystems.
707 : : */
708 : 0 : struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
709 : : {
710 : : struct buffer_head *bh;
711 : : unsigned int blocknr;
712 : : int err;
713 : :
714 : 3 : err = journal_next_log_block(journal, &blocknr);
715 : :
716 [ + - ]: 3 : if (err)
717 : : return NULL;
718 : :
719 : 3 : bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
720 [ + - ]: 3 : if (!bh)
721 : : return NULL;
722 : : lock_buffer(bh);
723 [ + - ]: 3 : memset(bh->b_data, 0, journal->j_blocksize);
724 : : set_buffer_uptodate(bh);
725 : 3 : unlock_buffer(bh);
726 : : BUFFER_TRACE(bh, "return this buffer");
727 : 3 : return journal_add_journal_head(bh);
728 : : }
729 : :
730 : : /*
731 : : * Management for journal control blocks: functions to create and
732 : : * destroy journal_t structures, and to initialise and read existing
733 : : * journal blocks from disk. */
734 : :
735 : : /* First: create and setup a journal_t object in memory. We initialise
736 : : * very few fields yet: that has to wait until we have created the
737 : : * journal structures from from scratch, or loaded them from disk. */
738 : :
739 : 0 : static journal_t * journal_init_common (void)
740 : : {
741 : : journal_t *journal;
742 : : int err;
743 : :
744 : : journal = kzalloc(sizeof(*journal), GFP_KERNEL);
745 [ + - ]: 2 : if (!journal)
746 : : goto fail;
747 : :
748 : 2 : init_waitqueue_head(&journal->j_wait_transaction_locked);
749 : 2 : init_waitqueue_head(&journal->j_wait_logspace);
750 : 2 : init_waitqueue_head(&journal->j_wait_done_commit);
751 : 2 : init_waitqueue_head(&journal->j_wait_checkpoint);
752 : 2 : init_waitqueue_head(&journal->j_wait_commit);
753 : 2 : init_waitqueue_head(&journal->j_wait_updates);
754 : 2 : mutex_init(&journal->j_checkpoint_mutex);
755 : 2 : spin_lock_init(&journal->j_revoke_lock);
756 : 2 : spin_lock_init(&journal->j_list_lock);
757 : 2 : spin_lock_init(&journal->j_state_lock);
758 : :
759 : 2 : journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
760 : :
761 : : /* The journal is marked for error until we succeed with recovery! */
762 : 2 : journal->j_flags = JFS_ABORT;
763 : :
764 : : /* Set up a default-sized revoke table for the new mount. */
765 : 2 : err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
766 [ - + ]: 2 : if (err) {
767 : 0 : kfree(journal);
768 : 0 : goto fail;
769 : : }
770 : : return journal;
771 : : fail:
772 : : return NULL;
773 : : }
774 : :
775 : : /* journal_init_dev and journal_init_inode:
776 : : *
777 : : * Create a journal structure assigned some fixed set of disk blocks to
778 : : * the journal. We don't actually touch those disk blocks yet, but we
779 : : * need to set up all of the mapping information to tell the journaling
780 : : * system where the journal blocks are.
781 : : *
782 : : */
783 : :
784 : : /**
785 : : * journal_t * journal_init_dev() - creates and initialises a journal structure
786 : : * @bdev: Block device on which to create the journal
787 : : * @fs_dev: Device which hold journalled filesystem for this journal.
788 : : * @start: Block nr Start of journal.
789 : : * @len: Length of the journal in blocks.
790 : : * @blocksize: blocksize of journalling device
791 : : *
792 : : * Returns: a newly created journal_t *
793 : : *
794 : : * journal_init_dev creates a journal which maps a fixed contiguous
795 : : * range of blocks on an arbitrary block device.
796 : : *
797 : : */
798 : 0 : journal_t * journal_init_dev(struct block_device *bdev,
799 : : struct block_device *fs_dev,
800 : : int start, int len, int blocksize)
801 : : {
802 : 0 : journal_t *journal = journal_init_common();
803 : : struct buffer_head *bh;
804 : : int n;
805 : :
806 [ # # ]: 0 : if (!journal)
807 : : return NULL;
808 : :
809 : : /* journal descriptor can store up to n blocks -bzzz */
810 : 0 : journal->j_blocksize = blocksize;
811 : 0 : n = journal->j_blocksize / sizeof(journal_block_tag_t);
812 : 0 : journal->j_wbufsize = n;
813 : 0 : journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
814 [ # # ]: 0 : if (!journal->j_wbuf) {
815 : 0 : printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
816 : : __func__);
817 : 0 : goto out_err;
818 : : }
819 : 0 : journal->j_dev = bdev;
820 : 0 : journal->j_fs_dev = fs_dev;
821 : 0 : journal->j_blk_offset = start;
822 : 0 : journal->j_maxlen = len;
823 : :
824 : 0 : bh = __getblk(journal->j_dev, start, journal->j_blocksize);
825 [ # # ]: 0 : if (!bh) {
826 : 0 : printk(KERN_ERR
827 : : "%s: Cannot get buffer for journal superblock\n",
828 : : __func__);
829 : 0 : goto out_err;
830 : : }
831 : 0 : journal->j_sb_buffer = bh;
832 : 0 : journal->j_superblock = (journal_superblock_t *)bh->b_data;
833 : :
834 : 0 : return journal;
835 : : out_err:
836 : 0 : kfree(journal->j_wbuf);
837 : 0 : kfree(journal);
838 : 0 : return NULL;
839 : : }
840 : :
841 : : /**
842 : : * journal_t * journal_init_inode () - creates a journal which maps to a inode.
843 : : * @inode: An inode to create the journal in
844 : : *
845 : : * journal_init_inode creates a journal which maps an on-disk inode as
846 : : * the journal. The inode must exist already, must support bmap() and
847 : : * must have all data blocks preallocated.
848 : : */
849 : 0 : journal_t * journal_init_inode (struct inode *inode)
850 : : {
851 : : struct buffer_head *bh;
852 : 2 : journal_t *journal = journal_init_common();
853 : : int err;
854 : : int n;
855 : : unsigned int blocknr;
856 : :
857 [ + ]: 2 : if (!journal)
858 : : return NULL;
859 : :
860 : 2 : journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
861 : 2 : journal->j_inode = inode;
862 : : jbd_debug(1,
863 : : "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
864 : : journal, inode->i_sb->s_id, inode->i_ino,
865 : : (long long) inode->i_size,
866 : : inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
867 : :
868 : 2 : journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
869 : 2 : journal->j_blocksize = inode->i_sb->s_blocksize;
870 : :
871 : : /* journal descriptor can store up to n blocks -bzzz */
872 : 2 : n = journal->j_blocksize / sizeof(journal_block_tag_t);
873 : 2 : journal->j_wbufsize = n;
874 : 4 : journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
875 [ - + ]: 2 : if (!journal->j_wbuf) {
876 : 0 : printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
877 : : __func__);
878 : 0 : goto out_err;
879 : : }
880 : :
881 : 2 : err = journal_bmap(journal, 0, &blocknr);
882 : : /* If that failed, give up */
883 [ - + ]: 2 : if (err) {
884 : 0 : printk(KERN_ERR "%s: Cannot locate journal superblock\n",
885 : : __func__);
886 : 0 : goto out_err;
887 : : }
888 : :
889 : 2 : bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
890 [ - + ]: 2 : if (!bh) {
891 : 0 : printk(KERN_ERR
892 : : "%s: Cannot get buffer for journal superblock\n",
893 : : __func__);
894 : 0 : goto out_err;
895 : : }
896 : 2 : journal->j_sb_buffer = bh;
897 : 2 : journal->j_superblock = (journal_superblock_t *)bh->b_data;
898 : :
899 : 2 : return journal;
900 : : out_err:
901 : 0 : kfree(journal->j_wbuf);
902 : 0 : kfree(journal);
903 : 0 : return NULL;
904 : : }
905 : :
906 : : /*
907 : : * If the journal init or create aborts, we need to mark the journal
908 : : * superblock as being NULL to prevent the journal destroy from writing
909 : : * back a bogus superblock.
910 : : */
911 : : static void journal_fail_superblock (journal_t *journal)
912 : : {
913 : 0 : struct buffer_head *bh = journal->j_sb_buffer;
914 : : brelse(bh);
915 : 0 : journal->j_sb_buffer = NULL;
916 : : }
917 : :
918 : : /*
919 : : * Given a journal_t structure, initialise the various fields for
920 : : * startup of a new journaling session. We use this both when creating
921 : : * a journal, and after recovering an old journal to reset it for
922 : : * subsequent use.
923 : : */
924 : :
925 : 0 : static int journal_reset(journal_t *journal)
926 : : {
927 : 2 : journal_superblock_t *sb = journal->j_superblock;
928 : : unsigned int first, last;
929 : :
930 [ - + ]: 2 : first = be32_to_cpu(sb->s_first);
931 [ - + ]: 2 : last = be32_to_cpu(sb->s_maxlen);
932 [ - + ]: 2 : if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
933 : 0 : printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
934 : : first, last);
935 : : journal_fail_superblock(journal);
936 : 0 : return -EINVAL;
937 : : }
938 : :
939 : 2 : journal->j_first = first;
940 : 2 : journal->j_last = last;
941 : :
942 : 2 : journal->j_head = first;
943 : 2 : journal->j_tail = first;
944 : 2 : journal->j_free = last - first;
945 : :
946 : 2 : journal->j_tail_sequence = journal->j_transaction_sequence;
947 : 2 : journal->j_commit_sequence = journal->j_transaction_sequence - 1;
948 : 2 : journal->j_commit_request = journal->j_commit_sequence;
949 : :
950 : 2 : journal->j_max_transaction_buffers = journal->j_maxlen / 4;
951 : :
952 : : /*
953 : : * As a special case, if the on-disk copy is already marked as needing
954 : : * no recovery (s_start == 0), then we can safely defer the superblock
955 : : * update until the next commit by setting JFS_FLUSHED. This avoids
956 : : * attempting a write to a potential-readonly device.
957 : : */
958 [ + - ]: 2 : if (sb->s_start == 0) {
959 : : jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
960 : : "(start %u, seq %d, errno %d)\n",
961 : : journal->j_tail, journal->j_tail_sequence,
962 : : journal->j_errno);
963 : 2 : journal->j_flags |= JFS_FLUSHED;
964 : : } else {
965 : : /* Lock here to make assertions happy... */
966 : 0 : mutex_lock(&journal->j_checkpoint_mutex);
967 : : /*
968 : : * Update log tail information. We use WRITE_FUA since new
969 : : * transaction will start reusing journal space and so we
970 : : * must make sure information about current log tail is on
971 : : * disk before that.
972 : : */
973 : 0 : journal_update_sb_log_tail(journal,
974 : : journal->j_tail_sequence,
975 : : journal->j_tail,
976 : : WRITE_FUA);
977 : 0 : mutex_unlock(&journal->j_checkpoint_mutex);
978 : : }
979 : 2 : return journal_start_thread(journal);
980 : : }
981 : :
982 : : /**
983 : : * int journal_create() - Initialise the new journal file
984 : : * @journal: Journal to create. This structure must have been initialised
985 : : *
986 : : * Given a journal_t structure which tells us which disk blocks we can
987 : : * use, create a new journal superblock and initialise all of the
988 : : * journal fields from scratch.
989 : : **/
990 : 0 : int journal_create(journal_t *journal)
991 : : {
992 : : unsigned int blocknr;
993 : : struct buffer_head *bh;
994 : : journal_superblock_t *sb;
995 : : int i, err;
996 : :
997 [ # # ]: 0 : if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
998 : 0 : printk (KERN_ERR "Journal length (%d blocks) too short.\n",
999 : : journal->j_maxlen);
1000 : : journal_fail_superblock(journal);
1001 : 0 : return -EINVAL;
1002 : : }
1003 : :
1004 [ # # ]: 0 : if (journal->j_inode == NULL) {
1005 : : /*
1006 : : * We don't know what block to start at!
1007 : : */
1008 : 0 : printk(KERN_EMERG
1009 : : "%s: creation of journal on external device!\n",
1010 : : __func__);
1011 : 0 : BUG();
1012 : : }
1013 : :
1014 : : /* Zero out the entire journal on disk. We cannot afford to
1015 : : have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
1016 : : jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1017 [ # # ]: 0 : for (i = 0; i < journal->j_maxlen; i++) {
1018 : 0 : err = journal_bmap(journal, i, &blocknr);
1019 [ # # ]: 0 : if (err)
1020 : : return err;
1021 : 0 : bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1022 [ # # ]: 0 : if (unlikely(!bh))
1023 : : return -ENOMEM;
1024 : : lock_buffer(bh);
1025 [ # # ]: 0 : memset (bh->b_data, 0, journal->j_blocksize);
1026 : : BUFFER_TRACE(bh, "marking dirty");
1027 : 0 : mark_buffer_dirty(bh);
1028 : : BUFFER_TRACE(bh, "marking uptodate");
1029 : : set_buffer_uptodate(bh);
1030 : 0 : unlock_buffer(bh);
1031 : 0 : __brelse(bh);
1032 : : }
1033 : :
1034 : 0 : sync_blockdev(journal->j_dev);
1035 : : jbd_debug(1, "JBD: journal cleared.\n");
1036 : :
1037 : : /* OK, fill in the initial static fields in the new superblock */
1038 : 0 : sb = journal->j_superblock;
1039 : :
1040 : 0 : sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
1041 : 0 : sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1042 : :
1043 [ # # ]: 0 : sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1044 [ # # ]: 0 : sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1045 : 0 : sb->s_first = cpu_to_be32(1);
1046 : :
1047 : 0 : journal->j_transaction_sequence = 1;
1048 : :
1049 : 0 : journal->j_flags &= ~JFS_ABORT;
1050 : 0 : journal->j_format_version = 2;
1051 : :
1052 : 0 : return journal_reset(journal);
1053 : : }
1054 : :
1055 : 0 : static void journal_write_superblock(journal_t *journal, int write_op)
1056 : : {
1057 : 3 : struct buffer_head *bh = journal->j_sb_buffer;
1058 : : int ret;
1059 : :
1060 : : trace_journal_write_superblock(journal, write_op);
1061 [ - + ]: 3 : if (!(journal->j_flags & JFS_BARRIER))
1062 : 0 : write_op &= ~(REQ_FUA | REQ_FLUSH);
1063 : : lock_buffer(bh);
1064 [ - + ]: 3 : if (buffer_write_io_error(bh)) {
1065 : : char b[BDEVNAME_SIZE];
1066 : : /*
1067 : : * Oh, dear. A previous attempt to write the journal
1068 : : * superblock failed. This could happen because the
1069 : : * USB device was yanked out. Or it could happen to
1070 : : * be a transient write error and maybe the block will
1071 : : * be remapped. Nothing we can do but to retry the
1072 : : * write and hope for the best.
1073 : : */
1074 : 0 : printk(KERN_ERR "JBD: previous I/O error detected "
1075 : : "for journal superblock update for %s.\n",
1076 : : journal_dev_name(journal, b));
1077 : : clear_buffer_write_io_error(bh);
1078 : : set_buffer_uptodate(bh);
1079 : : }
1080 : :
1081 : : get_bh(bh);
1082 : 3 : bh->b_end_io = end_buffer_write_sync;
1083 : 3 : ret = submit_bh(write_op, bh);
1084 : : wait_on_buffer(bh);
1085 [ - + ]: 3 : if (buffer_write_io_error(bh)) {
1086 : : clear_buffer_write_io_error(bh);
1087 : : set_buffer_uptodate(bh);
1088 : : ret = -EIO;
1089 : : }
1090 [ - + ]: 3 : if (ret) {
1091 : : char b[BDEVNAME_SIZE];
1092 : 0 : printk(KERN_ERR "JBD: Error %d detected "
1093 : : "when updating journal superblock for %s.\n",
1094 : : ret, journal_dev_name(journal, b));
1095 : : }
1096 : 3 : }
1097 : :
1098 : : /**
1099 : : * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1100 : : * @journal: The journal to update.
1101 : : * @tail_tid: TID of the new transaction at the tail of the log
1102 : : * @tail_block: The first block of the transaction at the tail of the log
1103 : : * @write_op: With which operation should we write the journal sb
1104 : : *
1105 : : * Update a journal's superblock information about log tail and write it to
1106 : : * disk, waiting for the IO to complete.
1107 : : */
1108 : 0 : void journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1109 : : unsigned int tail_block, int write_op)
1110 : : {
1111 : 2 : journal_superblock_t *sb = journal->j_superblock;
1112 : :
1113 [ - + ]: 2 : BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1114 : : jbd_debug(1,"JBD: updating superblock (start %u, seq %u)\n",
1115 : : tail_block, tail_tid);
1116 : :
1117 [ - + ]: 2 : sb->s_sequence = cpu_to_be32(tail_tid);
1118 [ - + ]: 2 : sb->s_start = cpu_to_be32(tail_block);
1119 : :
1120 : 2 : journal_write_superblock(journal, write_op);
1121 : :
1122 : : /* Log is no longer empty */
1123 : : spin_lock(&journal->j_state_lock);
1124 [ - + ]: 2 : WARN_ON(!sb->s_sequence);
1125 : 0 : journal->j_flags &= ~JFS_FLUSHED;
1126 : : spin_unlock(&journal->j_state_lock);
1127 : 2 : }
1128 : :
1129 : : /**
1130 : : * mark_journal_empty() - Mark on disk journal as empty.
1131 : : * @journal: The journal to update.
1132 : : *
1133 : : * Update a journal's dynamic superblock fields to show that journal is empty.
1134 : : * Write updated superblock to disk waiting for IO to complete.
1135 : : */
1136 : 0 : static void mark_journal_empty(journal_t *journal)
1137 : : {
1138 : 2 : journal_superblock_t *sb = journal->j_superblock;
1139 : :
1140 [ - + ]: 2 : BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1141 : : spin_lock(&journal->j_state_lock);
1142 : : /* Is it already empty? */
1143 [ + + ]: 4 : if (sb->s_start == 0) {
1144 : : spin_unlock(&journal->j_state_lock);
1145 : 2 : return;
1146 : : }
1147 : : jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1148 : : journal->j_tail_sequence);
1149 : :
1150 [ - + ]: 1 : sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1151 : 1 : sb->s_start = cpu_to_be32(0);
1152 : : spin_unlock(&journal->j_state_lock);
1153 : :
1154 : 1 : journal_write_superblock(journal, WRITE_FUA);
1155 : :
1156 : : spin_lock(&journal->j_state_lock);
1157 : : /* Log is empty */
1158 : 1 : journal->j_flags |= JFS_FLUSHED;
1159 : : spin_unlock(&journal->j_state_lock);
1160 : : }
1161 : :
1162 : : /**
1163 : : * journal_update_sb_errno() - Update error in the journal.
1164 : : * @journal: The journal to update.
1165 : : *
1166 : : * Update a journal's errno. Write updated superblock to disk waiting for IO
1167 : : * to complete.
1168 : : */
1169 : 0 : static void journal_update_sb_errno(journal_t *journal)
1170 : : {
1171 : 0 : journal_superblock_t *sb = journal->j_superblock;
1172 : :
1173 : : spin_lock(&journal->j_state_lock);
1174 : : jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1175 : : journal->j_errno);
1176 [ # # ]: 0 : sb->s_errno = cpu_to_be32(journal->j_errno);
1177 : : spin_unlock(&journal->j_state_lock);
1178 : :
1179 : 0 : journal_write_superblock(journal, WRITE_SYNC);
1180 : 0 : }
1181 : :
1182 : : /*
1183 : : * Read the superblock for a given journal, performing initial
1184 : : * validation of the format.
1185 : : */
1186 : :
1187 : 0 : static int journal_get_superblock(journal_t *journal)
1188 : : {
1189 : : struct buffer_head *bh;
1190 : : journal_superblock_t *sb;
1191 : : int err = -EIO;
1192 : :
1193 : 4 : bh = journal->j_sb_buffer;
1194 : :
1195 [ - + ]: 4 : J_ASSERT(bh != NULL);
1196 [ + + ]: 4 : if (!buffer_uptodate(bh)) {
1197 : 2 : ll_rw_block(READ, 1, &bh);
1198 : 2 : wait_on_buffer(bh);
1199 [ - + ]: 2 : if (!buffer_uptodate(bh)) {
1200 : 0 : printk (KERN_ERR
1201 : : "JBD: IO error reading journal superblock\n");
1202 : 0 : goto out;
1203 : : }
1204 : : }
1205 : :
1206 : 4 : sb = journal->j_superblock;
1207 : :
1208 : : err = -EINVAL;
1209 : :
1210 [ + - ][ + ]: 8 : if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1211 [ - + ]: 4 : sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1212 : 0 : printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1213 : 0 : goto out;
1214 : : }
1215 : :
1216 [ + + ]: 12 : switch(be32_to_cpu(sb->s_header.h_blocktype)) {
[ + + - ]
1217 : : case JFS_SUPERBLOCK_V1:
1218 : 0 : journal->j_format_version = 1;
1219 : 4 : break;
1220 : : case JFS_SUPERBLOCK_V2:
1221 : 4 : journal->j_format_version = 2;
1222 : 4 : break;
1223 : : default:
1224 : 0 : printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1225 : 0 : goto out;
1226 : : }
1227 : :
1228 [ - + ][ - + ]: 8 : if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1229 [ # # ]: 0 : journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1230 [ - + ][ - + ]: 8 : else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1231 : 0 : printk (KERN_WARNING "JBD: journal file too short\n");
1232 : 0 : goto out;
1233 : : }
1234 : :
1235 [ - + ][ # # ]: 8 : if (be32_to_cpu(sb->s_first) == 0 ||
[ + - ][ - + ]
1236 [ - + ]: 4 : be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1237 [ # # ]: 0 : printk(KERN_WARNING
1238 : : "JBD: Invalid start block of journal: %u\n",
1239 : 0 : be32_to_cpu(sb->s_first));
1240 : 0 : goto out;
1241 : : }
1242 : :
1243 : : return 0;
1244 : :
1245 : : out:
1246 : : journal_fail_superblock(journal);
1247 : 0 : return err;
1248 : : }
1249 : :
1250 : : /*
1251 : : * Load the on-disk journal superblock and read the key fields into the
1252 : : * journal_t.
1253 : : */
1254 : :
1255 : 0 : static int load_superblock(journal_t *journal)
1256 : : {
1257 : : int err;
1258 : : journal_superblock_t *sb;
1259 : :
1260 : 4 : err = journal_get_superblock(journal);
1261 [ + ]: 4 : if (err)
1262 : : return err;
1263 : :
1264 : 8 : sb = journal->j_superblock;
1265 : :
1266 [ + + ]: 8 : journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1267 [ + + ]: 8 : journal->j_tail = be32_to_cpu(sb->s_start);
1268 [ - + ]: 8 : journal->j_first = be32_to_cpu(sb->s_first);
1269 [ - + ]: 4 : journal->j_last = be32_to_cpu(sb->s_maxlen);
1270 [ - + ]: 4 : journal->j_errno = be32_to_cpu(sb->s_errno);
1271 : :
1272 : 4 : return 0;
1273 : : }
1274 : :
1275 : :
1276 : : /**
1277 : : * int journal_load() - Read journal from disk.
1278 : : * @journal: Journal to act on.
1279 : : *
1280 : : * Given a journal_t structure which tells us which disk blocks contain
1281 : : * a journal, read the journal from disk to initialise the in-memory
1282 : : * structures.
1283 : : */
1284 : 0 : int journal_load(journal_t *journal)
1285 : : {
1286 : : int err;
1287 : : journal_superblock_t *sb;
1288 : :
1289 : 2 : err = load_superblock(journal);
1290 [ + - ]: 2 : if (err)
1291 : : return err;
1292 : :
1293 : 2 : sb = journal->j_superblock;
1294 : : /* If this is a V2 superblock, then we have to check the
1295 : : * features flags on it. */
1296 : :
1297 [ + - ]: 2 : if (journal->j_format_version >= 2) {
1298 [ + - ]: 2 : if ((sb->s_feature_ro_compat &
1299 [ - + ]: 2 : ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1300 : 2 : (sb->s_feature_incompat &
1301 : : ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1302 : 0 : printk (KERN_WARNING
1303 : : "JBD: Unrecognised features on journal\n");
1304 : 0 : return -EINVAL;
1305 : : }
1306 : : }
1307 : :
1308 : : /* Let the recovery code check whether it needs to recover any
1309 : : * data from the journal. */
1310 [ + - ]: 2 : if (journal_recover(journal))
1311 : : goto recovery_error;
1312 : :
1313 : : /* OK, we've finished with the dynamic journal bits:
1314 : : * reinitialise the dynamic contents of the superblock in memory
1315 : : * and reset them on disk. */
1316 [ + - ]: 2 : if (journal_reset(journal))
1317 : : goto recovery_error;
1318 : :
1319 : 2 : journal->j_flags &= ~JFS_ABORT;
1320 : 2 : journal->j_flags |= JFS_LOADED;
1321 : 2 : return 0;
1322 : :
1323 : : recovery_error:
1324 : 0 : printk (KERN_WARNING "JBD: recovery failed\n");
1325 : 0 : return -EIO;
1326 : : }
1327 : :
1328 : : /**
1329 : : * void journal_destroy() - Release a journal_t structure.
1330 : : * @journal: Journal to act on.
1331 : : *
1332 : : * Release a journal_t structure once it is no longer in use by the
1333 : : * journaled object.
1334 : : * Return <0 if we couldn't clean up the journal.
1335 : : */
1336 : 0 : int journal_destroy(journal_t *journal)
1337 : : {
1338 : : int err = 0;
1339 : :
1340 : :
1341 : : /* Wait for the commit thread to wake up and die. */
1342 : 2 : journal_kill_thread(journal);
1343 : :
1344 : : /* Force a final log commit */
1345 [ - + ]: 2 : if (journal->j_running_transaction)
1346 : 0 : journal_commit_transaction(journal);
1347 : :
1348 : : /* Force any old transactions to disk */
1349 : :
1350 : : /* We cannot race with anybody but must keep assertions happy */
1351 : 2 : mutex_lock(&journal->j_checkpoint_mutex);
1352 : : /* Totally anal locking here... */
1353 : : spin_lock(&journal->j_list_lock);
1354 [ + + ]: 5 : while (journal->j_checkpoint_transactions != NULL) {
1355 : : spin_unlock(&journal->j_list_lock);
1356 : 1 : log_do_checkpoint(journal);
1357 : : spin_lock(&journal->j_list_lock);
1358 : : }
1359 : :
1360 [ - + ]: 2 : J_ASSERT(journal->j_running_transaction == NULL);
1361 [ - + ]: 2 : J_ASSERT(journal->j_committing_transaction == NULL);
1362 [ - + ]: 2 : J_ASSERT(journal->j_checkpoint_transactions == NULL);
1363 : : spin_unlock(&journal->j_list_lock);
1364 : :
1365 [ + - ]: 2 : if (journal->j_sb_buffer) {
1366 [ + - ]: 2 : if (!is_journal_aborted(journal)) {
1367 : 2 : journal->j_tail_sequence =
1368 : 2 : ++journal->j_transaction_sequence;
1369 : 2 : mark_journal_empty(journal);
1370 : : } else
1371 : : err = -EIO;
1372 : 2 : brelse(journal->j_sb_buffer);
1373 : : }
1374 : 2 : mutex_unlock(&journal->j_checkpoint_mutex);
1375 : :
1376 [ + - ]: 2 : if (journal->j_inode)
1377 : 2 : iput(journal->j_inode);
1378 [ + - ]: 2 : if (journal->j_revoke)
1379 : 2 : journal_destroy_revoke(journal);
1380 : 2 : kfree(journal->j_wbuf);
1381 : 2 : kfree(journal);
1382 : :
1383 : 2 : return err;
1384 : : }
1385 : :
1386 : :
1387 : : /**
1388 : : *int journal_check_used_features () - Check if features specified are used.
1389 : : * @journal: Journal to check.
1390 : : * @compat: bitmask of compatible features
1391 : : * @ro: bitmask of features that force read-only mount
1392 : : * @incompat: bitmask of incompatible features
1393 : : *
1394 : : * Check whether the journal uses all of a given set of
1395 : : * features. Return true (non-zero) if it does.
1396 : : **/
1397 : :
1398 : 0 : int journal_check_used_features (journal_t *journal, unsigned long compat,
1399 : : unsigned long ro, unsigned long incompat)
1400 : : {
1401 : : journal_superblock_t *sb;
1402 : :
1403 [ + - ]: 1 : if (!compat && !ro && !incompat)
1404 : : return 1;
1405 [ + - ]: 1 : if (journal->j_format_version == 1)
1406 : : return 0;
1407 : :
1408 : 1 : sb = journal->j_superblock;
1409 : :
1410 [ - + ][ + - ]: 3 : if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
[ + ]
1411 [ - + ][ + ]: 2 : ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1412 [ - + ]: 2 : ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1413 : : return 1;
1414 : :
1415 : 1 : return 0;
1416 : : }
1417 : :
1418 : : /**
1419 : : * int journal_check_available_features() - Check feature set in journalling layer
1420 : : * @journal: Journal to check.
1421 : : * @compat: bitmask of compatible features
1422 : : * @ro: bitmask of features that force read-only mount
1423 : : * @incompat: bitmask of incompatible features
1424 : : *
1425 : : * Check whether the journaling code supports the use of
1426 : : * all of a given set of features on this journal. Return true
1427 : : * (non-zero) if it can. */
1428 : :
1429 : 0 : int journal_check_available_features (journal_t *journal, unsigned long compat,
1430 : : unsigned long ro, unsigned long incompat)
1431 : : {
1432 [ + - ][ + - ]: 3 : if (!compat && !ro && !incompat)
1433 : : return 1;
1434 : :
1435 : : /* We can support any known requested features iff the
1436 : : * superblock is in version 2. Otherwise we fail to support any
1437 : : * extended sb features. */
1438 : :
1439 [ + - ][ + - ]: 3 : if (journal->j_format_version != 2)
1440 : : return 0;
1441 : :
1442 [ + - ][ + - ]: 3 : if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1443 [ - + ][ - + ]: 3 : (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1444 : 3 : (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1445 : : return 1;
1446 : :
1447 : 0 : return 0;
1448 : : }
1449 : :
1450 : : /**
1451 : : * int journal_set_features () - Mark a given journal feature in the superblock
1452 : : * @journal: Journal to act on.
1453 : : * @compat: bitmask of compatible features
1454 : : * @ro: bitmask of features that force read-only mount
1455 : : * @incompat: bitmask of incompatible features
1456 : : *
1457 : : * Mark a given journal feature as present on the
1458 : : * superblock. Returns true if the requested features could be set.
1459 : : *
1460 : : */
1461 : :
1462 : 0 : int journal_set_features (journal_t *journal, unsigned long compat,
1463 : : unsigned long ro, unsigned long incompat)
1464 : : {
1465 : : journal_superblock_t *sb;
1466 : :
1467 [ + - ]: 1 : if (journal_check_used_features(journal, compat, ro, incompat))
1468 : : return 1;
1469 : :
1470 [ + ]: 1 : if (!journal_check_available_features(journal, compat, ro, incompat))
1471 : : return 0;
1472 : :
1473 : : jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1474 : : compat, ro, incompat);
1475 : :
1476 : 2 : sb = journal->j_superblock;
1477 : :
1478 [ + + ]: 2 : sb->s_feature_compat |= cpu_to_be32(compat);
1479 [ - + ]: 2 : sb->s_feature_ro_compat |= cpu_to_be32(ro);
1480 [ - + ]: 1 : sb->s_feature_incompat |= cpu_to_be32(incompat);
1481 : :
1482 : 1 : return 1;
1483 : : }
1484 : :
1485 : :
1486 : : /**
1487 : : * int journal_update_format () - Update on-disk journal structure.
1488 : : * @journal: Journal to act on.
1489 : : *
1490 : : * Given an initialised but unloaded journal struct, poke about in the
1491 : : * on-disk structure to update it to the most recent supported version.
1492 : : */
1493 : 0 : int journal_update_format (journal_t *journal)
1494 : : {
1495 : : journal_superblock_t *sb;
1496 : : int err;
1497 : :
1498 : 0 : err = journal_get_superblock(journal);
1499 [ # # ]: 0 : if (err)
1500 : : return err;
1501 : :
1502 : 0 : sb = journal->j_superblock;
1503 : :
1504 [ # # ]: 0 : switch (be32_to_cpu(sb->s_header.h_blocktype)) {
[ # # # ]
1505 : : case JFS_SUPERBLOCK_V2:
1506 : : return 0;
1507 : : case JFS_SUPERBLOCK_V1:
1508 : 0 : return journal_convert_superblock_v1(journal, sb);
1509 : : default:
1510 : : break;
1511 : : }
1512 : 0 : return -EINVAL;
1513 : : }
1514 : :
1515 : 0 : static int journal_convert_superblock_v1(journal_t *journal,
1516 : : journal_superblock_t *sb)
1517 : : {
1518 : : int offset, blocksize;
1519 : : struct buffer_head *bh;
1520 : :
1521 : 0 : printk(KERN_WARNING
1522 : : "JBD: Converting superblock from version 1 to 2.\n");
1523 : :
1524 : : /* Pre-initialise new fields to zero */
1525 : : offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1526 [ # # ]: 0 : blocksize = be32_to_cpu(sb->s_blocksize);
1527 [ # # ]: 0 : memset(&sb->s_feature_compat, 0, blocksize-offset);
1528 : :
1529 : 0 : sb->s_nr_users = cpu_to_be32(1);
1530 : 0 : sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1531 : 0 : journal->j_format_version = 2;
1532 : :
1533 : 0 : bh = journal->j_sb_buffer;
1534 : : BUFFER_TRACE(bh, "marking dirty");
1535 : 0 : mark_buffer_dirty(bh);
1536 : 0 : sync_dirty_buffer(bh);
1537 : 0 : return 0;
1538 : : }
1539 : :
1540 : :
1541 : : /**
1542 : : * int journal_flush () - Flush journal
1543 : : * @journal: Journal to act on.
1544 : : *
1545 : : * Flush all data for a given journal to disk and empty the journal.
1546 : : * Filesystems can use this when remounting readonly to ensure that
1547 : : * recovery does not need to happen on remount.
1548 : : */
1549 : :
1550 : 0 : int journal_flush(journal_t *journal)
1551 : : {
1552 : : int err = 0;
1553 : : transaction_t *transaction = NULL;
1554 : :
1555 : : spin_lock(&journal->j_state_lock);
1556 : :
1557 : : /* Force everything buffered to the log... */
1558 [ # # ]: 0 : if (journal->j_running_transaction) {
1559 : : transaction = journal->j_running_transaction;
1560 : 0 : __log_start_commit(journal, transaction->t_tid);
1561 [ # # ]: 0 : } else if (journal->j_committing_transaction)
1562 : : transaction = journal->j_committing_transaction;
1563 : :
1564 : : /* Wait for the log commit to complete... */
1565 [ # # ]: 0 : if (transaction) {
1566 : 0 : tid_t tid = transaction->t_tid;
1567 : :
1568 : : spin_unlock(&journal->j_state_lock);
1569 : 0 : log_wait_commit(journal, tid);
1570 : : } else {
1571 : : spin_unlock(&journal->j_state_lock);
1572 : : }
1573 : :
1574 : : /* ...and flush everything in the log out to disk. */
1575 : : spin_lock(&journal->j_list_lock);
1576 [ # # ][ # # ]: 0 : while (!err && journal->j_checkpoint_transactions != NULL) {
1577 : : spin_unlock(&journal->j_list_lock);
1578 : 0 : mutex_lock(&journal->j_checkpoint_mutex);
1579 : 0 : err = log_do_checkpoint(journal);
1580 : 0 : mutex_unlock(&journal->j_checkpoint_mutex);
1581 : : spin_lock(&journal->j_list_lock);
1582 : : }
1583 : : spin_unlock(&journal->j_list_lock);
1584 : :
1585 [ # # ]: 0 : if (is_journal_aborted(journal))
1586 : : return -EIO;
1587 : :
1588 : 0 : mutex_lock(&journal->j_checkpoint_mutex);
1589 : 0 : cleanup_journal_tail(journal);
1590 : :
1591 : : /* Finally, mark the journal as really needing no recovery.
1592 : : * This sets s_start==0 in the underlying superblock, which is
1593 : : * the magic code for a fully-recovered superblock. Any future
1594 : : * commits of data to the journal will restore the current
1595 : : * s_start value. */
1596 : 0 : mark_journal_empty(journal);
1597 : 0 : mutex_unlock(&journal->j_checkpoint_mutex);
1598 : : spin_lock(&journal->j_state_lock);
1599 [ # # ]: 0 : J_ASSERT(!journal->j_running_transaction);
1600 [ # # ]: 0 : J_ASSERT(!journal->j_committing_transaction);
1601 [ # # ]: 0 : J_ASSERT(!journal->j_checkpoint_transactions);
1602 [ # # ]: 0 : J_ASSERT(journal->j_head == journal->j_tail);
1603 [ # # ]: 0 : J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1604 : : spin_unlock(&journal->j_state_lock);
1605 : 0 : return 0;
1606 : : }
1607 : :
1608 : : /**
1609 : : * int journal_wipe() - Wipe journal contents
1610 : : * @journal: Journal to act on.
1611 : : * @write: flag (see below)
1612 : : *
1613 : : * Wipe out all of the contents of a journal, safely. This will produce
1614 : : * a warning if the journal contains any valid recovery information.
1615 : : * Must be called between journal_init_*() and journal_load().
1616 : : *
1617 : : * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1618 : : * we merely suppress recovery.
1619 : : */
1620 : :
1621 : 0 : int journal_wipe(journal_t *journal, int write)
1622 : : {
1623 : : int err = 0;
1624 : :
1625 [ - + ]: 2 : J_ASSERT (!(journal->j_flags & JFS_LOADED));
1626 : :
1627 : 2 : err = load_superblock(journal);
1628 [ + - ]: 2 : if (err)
1629 : : return err;
1630 : :
1631 [ - + ]: 2 : if (!journal->j_tail)
1632 : : goto no_recovery;
1633 : :
1634 [ # # ]: 0 : printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1635 : : write ? "Clearing" : "Ignoring");
1636 : :
1637 : 0 : err = journal_skip_recovery(journal);
1638 [ # # ]: 0 : if (write) {
1639 : : /* Lock to make assertions happy... */
1640 : 0 : mutex_lock(&journal->j_checkpoint_mutex);
1641 : 0 : mark_journal_empty(journal);
1642 : 0 : mutex_unlock(&journal->j_checkpoint_mutex);
1643 : : }
1644 : :
1645 : : no_recovery:
1646 : 2 : return err;
1647 : : }
1648 : :
1649 : : /*
1650 : : * journal_dev_name: format a character string to describe on what
1651 : : * device this journal is present.
1652 : : */
1653 : :
1654 : : static const char *journal_dev_name(journal_t *journal, char *buffer)
1655 : : {
1656 : : struct block_device *bdev;
1657 : :
1658 [ # # ][ # # ]: 0 : if (journal->j_inode)
[ # # ]
1659 : 0 : bdev = journal->j_inode->i_sb->s_bdev;
1660 : : else
1661 : : bdev = journal->j_dev;
1662 : :
1663 : 0 : return bdevname(bdev, buffer);
1664 : : }
1665 : :
1666 : : /*
1667 : : * Journal abort has very specific semantics, which we describe
1668 : : * for journal abort.
1669 : : *
1670 : : * Two internal function, which provide abort to te jbd layer
1671 : : * itself are here.
1672 : : */
1673 : :
1674 : : /*
1675 : : * Quick version for internal journal use (doesn't lock the journal).
1676 : : * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1677 : : * and don't attempt to make any other journal updates.
1678 : : */
1679 : 0 : static void __journal_abort_hard(journal_t *journal)
1680 : : {
1681 : : transaction_t *transaction;
1682 : : char b[BDEVNAME_SIZE];
1683 : :
1684 [ # # ]: 0 : if (journal->j_flags & JFS_ABORT)
1685 : 0 : return;
1686 : :
1687 : 0 : printk(KERN_ERR "Aborting journal on device %s.\n",
1688 : : journal_dev_name(journal, b));
1689 : :
1690 : : spin_lock(&journal->j_state_lock);
1691 : 0 : journal->j_flags |= JFS_ABORT;
1692 : 0 : transaction = journal->j_running_transaction;
1693 [ # # ]: 0 : if (transaction)
1694 : 0 : __log_start_commit(journal, transaction->t_tid);
1695 : : spin_unlock(&journal->j_state_lock);
1696 : : }
1697 : :
1698 : : /* Soft abort: record the abort error status in the journal superblock,
1699 : : * but don't do any other IO. */
1700 : 0 : static void __journal_abort_soft (journal_t *journal, int errno)
1701 : : {
1702 [ # # ]: 0 : if (journal->j_flags & JFS_ABORT)
1703 : 0 : return;
1704 : :
1705 [ # # ]: 0 : if (!journal->j_errno)
1706 : 0 : journal->j_errno = errno;
1707 : :
1708 : 0 : __journal_abort_hard(journal);
1709 : :
1710 [ # # ]: 0 : if (errno)
1711 : 0 : journal_update_sb_errno(journal);
1712 : : }
1713 : :
1714 : : /**
1715 : : * void journal_abort () - Shutdown the journal immediately.
1716 : : * @journal: the journal to shutdown.
1717 : : * @errno: an error number to record in the journal indicating
1718 : : * the reason for the shutdown.
1719 : : *
1720 : : * Perform a complete, immediate shutdown of the ENTIRE
1721 : : * journal (not of a single transaction). This operation cannot be
1722 : : * undone without closing and reopening the journal.
1723 : : *
1724 : : * The journal_abort function is intended to support higher level error
1725 : : * recovery mechanisms such as the ext2/ext3 remount-readonly error
1726 : : * mode.
1727 : : *
1728 : : * Journal abort has very specific semantics. Any existing dirty,
1729 : : * unjournaled buffers in the main filesystem will still be written to
1730 : : * disk by bdflush, but the journaling mechanism will be suspended
1731 : : * immediately and no further transaction commits will be honoured.
1732 : : *
1733 : : * Any dirty, journaled buffers will be written back to disk without
1734 : : * hitting the journal. Atomicity cannot be guaranteed on an aborted
1735 : : * filesystem, but we _do_ attempt to leave as much data as possible
1736 : : * behind for fsck to use for cleanup.
1737 : : *
1738 : : * Any attempt to get a new transaction handle on a journal which is in
1739 : : * ABORT state will just result in an -EROFS error return. A
1740 : : * journal_stop on an existing handle will return -EIO if we have
1741 : : * entered abort state during the update.
1742 : : *
1743 : : * Recursive transactions are not disturbed by journal abort until the
1744 : : * final journal_stop, which will receive the -EIO error.
1745 : : *
1746 : : * Finally, the journal_abort call allows the caller to supply an errno
1747 : : * which will be recorded (if possible) in the journal superblock. This
1748 : : * allows a client to record failure conditions in the middle of a
1749 : : * transaction without having to complete the transaction to record the
1750 : : * failure to disk. ext3_error, for example, now uses this
1751 : : * functionality.
1752 : : *
1753 : : * Errors which originate from within the journaling layer will NOT
1754 : : * supply an errno; a null errno implies that absolutely no further
1755 : : * writes are done to the journal (unless there are any already in
1756 : : * progress).
1757 : : *
1758 : : */
1759 : :
1760 : 0 : void journal_abort(journal_t *journal, int errno)
1761 : : {
1762 : 0 : __journal_abort_soft(journal, errno);
1763 : 0 : }
1764 : :
1765 : : /**
1766 : : * int journal_errno () - returns the journal's error state.
1767 : : * @journal: journal to examine.
1768 : : *
1769 : : * This is the errno numbet set with journal_abort(), the last
1770 : : * time the journal was mounted - if the journal was stopped
1771 : : * without calling abort this will be 0.
1772 : : *
1773 : : * If the journal has been aborted on this mount time -EROFS will
1774 : : * be returned.
1775 : : */
1776 : 0 : int journal_errno(journal_t *journal)
1777 : : {
1778 : : int err;
1779 : :
1780 : : spin_lock(&journal->j_state_lock);
1781 [ + - ]: 2 : if (journal->j_flags & JFS_ABORT)
1782 : : err = -EROFS;
1783 : : else
1784 : 2 : err = journal->j_errno;
1785 : : spin_unlock(&journal->j_state_lock);
1786 : 2 : return err;
1787 : : }
1788 : :
1789 : : /**
1790 : : * int journal_clear_err () - clears the journal's error state
1791 : : * @journal: journal to act on.
1792 : : *
1793 : : * An error must be cleared or Acked to take a FS out of readonly
1794 : : * mode.
1795 : : */
1796 : 0 : int journal_clear_err(journal_t *journal)
1797 : : {
1798 : : int err = 0;
1799 : :
1800 : : spin_lock(&journal->j_state_lock);
1801 [ # # ]: 0 : if (journal->j_flags & JFS_ABORT)
1802 : : err = -EROFS;
1803 : : else
1804 : 0 : journal->j_errno = 0;
1805 : : spin_unlock(&journal->j_state_lock);
1806 : 0 : return err;
1807 : : }
1808 : :
1809 : : /**
1810 : : * void journal_ack_err() - Ack journal err.
1811 : : * @journal: journal to act on.
1812 : : *
1813 : : * An error must be cleared or Acked to take a FS out of readonly
1814 : : * mode.
1815 : : */
1816 : 0 : void journal_ack_err(journal_t *journal)
1817 : : {
1818 : : spin_lock(&journal->j_state_lock);
1819 [ # # ]: 0 : if (journal->j_errno)
1820 : 0 : journal->j_flags |= JFS_ACK_ERR;
1821 : : spin_unlock(&journal->j_state_lock);
1822 : 0 : }
1823 : :
1824 : 0 : int journal_blocks_per_page(struct inode *inode)
1825 : : {
1826 : 6 : return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1827 : : }
1828 : :
1829 : : /*
1830 : : * Journal_head storage management
1831 : : */
1832 : : static struct kmem_cache *journal_head_cache;
1833 : : #ifdef CONFIG_JBD_DEBUG
1834 : : static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1835 : : #endif
1836 : :
1837 : 0 : static int journal_init_journal_head_cache(void)
1838 : : {
1839 : : int retval;
1840 : :
1841 [ # # ]: 0 : J_ASSERT(journal_head_cache == NULL);
1842 : 0 : journal_head_cache = kmem_cache_create("journal_head",
1843 : : sizeof(struct journal_head),
1844 : : 0, /* offset */
1845 : : SLAB_TEMPORARY, /* flags */
1846 : : NULL); /* ctor */
1847 : : retval = 0;
1848 [ # # ]: 0 : if (!journal_head_cache) {
1849 : : retval = -ENOMEM;
1850 : 0 : printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1851 : : }
1852 : 0 : return retval;
1853 : : }
1854 : :
1855 : : static void journal_destroy_journal_head_cache(void)
1856 : : {
1857 [ # # ]: 0 : if (journal_head_cache) {
1858 : 0 : kmem_cache_destroy(journal_head_cache);
1859 : 0 : journal_head_cache = NULL;
1860 : : }
1861 : : }
1862 : :
1863 : : /*
1864 : : * journal_head splicing and dicing
1865 : : */
1866 : 0 : static struct journal_head *journal_alloc_journal_head(void)
1867 : : {
1868 : : struct journal_head *ret;
1869 : :
1870 : : #ifdef CONFIG_JBD_DEBUG
1871 : : atomic_inc(&nr_journal_heads);
1872 : : #endif
1873 : 28 : ret = kmem_cache_zalloc(journal_head_cache, GFP_NOFS);
1874 [ - + ]: 28 : if (ret == NULL) {
1875 : : jbd_debug(1, "out of memory for journal_head\n");
1876 [ # # ]: 0 : printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1877 : : __func__);
1878 : :
1879 [ # # ]: 0 : while (ret == NULL) {
1880 : 0 : yield();
1881 : 0 : ret = kmem_cache_zalloc(journal_head_cache, GFP_NOFS);
1882 : : }
1883 : : }
1884 : 0 : return ret;
1885 : : }
1886 : :
1887 : : static void journal_free_journal_head(struct journal_head *jh)
1888 : : {
1889 : : #ifdef CONFIG_JBD_DEBUG
1890 : : atomic_dec(&nr_journal_heads);
1891 : : memset(jh, JBD_POISON_FREE, sizeof(*jh));
1892 : : #endif
1893 : 28 : kmem_cache_free(journal_head_cache, jh);
1894 : : }
1895 : :
1896 : : /*
1897 : : * A journal_head is attached to a buffer_head whenever JBD has an
1898 : : * interest in the buffer.
1899 : : *
1900 : : * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1901 : : * is set. This bit is tested in core kernel code where we need to take
1902 : : * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1903 : : * there.
1904 : : *
1905 : : * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1906 : : *
1907 : : * When a buffer has its BH_JBD bit set it is immune from being released by
1908 : : * core kernel code, mainly via ->b_count.
1909 : : *
1910 : : * A journal_head is detached from its buffer_head when the journal_head's
1911 : : * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1912 : : * transaction (b_cp_transaction) hold their references to b_jcount.
1913 : : *
1914 : : * Various places in the kernel want to attach a journal_head to a buffer_head
1915 : : * _before_ attaching the journal_head to a transaction. To protect the
1916 : : * journal_head in this situation, journal_add_journal_head elevates the
1917 : : * journal_head's b_jcount refcount by one. The caller must call
1918 : : * journal_put_journal_head() to undo this.
1919 : : *
1920 : : * So the typical usage would be:
1921 : : *
1922 : : * (Attach a journal_head if needed. Increments b_jcount)
1923 : : * struct journal_head *jh = journal_add_journal_head(bh);
1924 : : * ...
1925 : : * (Get another reference for transaction)
1926 : : * journal_grab_journal_head(bh);
1927 : : * jh->b_transaction = xxx;
1928 : : * (Put original reference)
1929 : : * journal_put_journal_head(jh);
1930 : : */
1931 : :
1932 : : /*
1933 : : * Give a buffer_head a journal_head.
1934 : : *
1935 : : * May sleep.
1936 : : */
1937 : 348 : struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1938 : : {
1939 : : struct journal_head *jh;
1940 : : struct journal_head *new_jh = NULL;
1941 : :
1942 : : repeat:
1943 [ + + ]: 188 : if (!buffer_jbd(bh))
1944 : 28 : new_jh = journal_alloc_journal_head();
1945 : :
1946 : : jbd_lock_bh_journal_head(bh);
1947 [ + + ]: 376 : if (buffer_jbd(bh)) {
1948 : : jh = bh2jh(bh);
1949 : : } else {
1950 [ + + ][ + - ]: 28 : J_ASSERT_BH(bh,
[ + - ][ - + ]
1951 : : (atomic_read(&bh->b_count) > 0) ||
1952 : : (bh->b_page && bh->b_page->mapping));
1953 : :
1954 [ - + ]: 28 : if (!new_jh) {
1955 : : jbd_unlock_bh_journal_head(bh);
1956 : : goto repeat;
1957 : : }
1958 : :
1959 : : jh = new_jh;
1960 : : new_jh = NULL; /* We consumed it */
1961 : : set_buffer_jbd(bh);
1962 : 28 : bh->b_private = jh;
1963 : 28 : jh->b_bh = bh;
1964 : : get_bh(bh);
1965 : : BUFFER_TRACE(bh, "added journal_head");
1966 : : }
1967 : 188 : jh->b_jcount++;
1968 : : jbd_unlock_bh_journal_head(bh);
1969 [ - + ]: 188 : if (new_jh)
1970 : : journal_free_journal_head(new_jh);
1971 : 188 : return bh->b_private;
1972 : : }
1973 : :
1974 : : /*
1975 : : * Grab a ref against this buffer_head's journal_head. If it ended up not
1976 : : * having a journal_head, return NULL
1977 : : */
1978 : 0 : struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1979 : : {
1980 : : struct journal_head *jh = NULL;
1981 : :
1982 : : jbd_lock_bh_journal_head(bh);
1983 [ + - ]: 42 : if (buffer_jbd(bh)) {
1984 : : jh = bh2jh(bh);
1985 : 42 : jh->b_jcount++;
1986 : : }
1987 : : jbd_unlock_bh_journal_head(bh);
1988 : 42 : return jh;
1989 : : }
1990 : :
1991 : 0 : static void __journal_remove_journal_head(struct buffer_head *bh)
1992 : : {
1993 : 28 : struct journal_head *jh = bh2jh(bh);
1994 : :
1995 [ - + ]: 28 : J_ASSERT_JH(jh, jh->b_jcount >= 0);
1996 [ - + ]: 28 : J_ASSERT_JH(jh, jh->b_transaction == NULL);
1997 [ - + ]: 28 : J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1998 [ - + ]: 28 : J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1999 [ - + ]: 28 : J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2000 [ - + ]: 28 : J_ASSERT_BH(bh, buffer_jbd(bh));
2001 [ - + ]: 28 : J_ASSERT_BH(bh, jh2bh(jh) == bh);
2002 : : BUFFER_TRACE(bh, "remove journal_head");
2003 [ - + ]: 28 : if (jh->b_frozen_data) {
2004 : 0 : printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2005 : 0 : jbd_free(jh->b_frozen_data, bh->b_size);
2006 : : }
2007 [ - + ]: 28 : if (jh->b_committed_data) {
2008 : 0 : printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2009 : 0 : jbd_free(jh->b_committed_data, bh->b_size);
2010 : : }
2011 : 28 : bh->b_private = NULL;
2012 : 28 : jh->b_bh = NULL; /* debug, really */
2013 : : clear_buffer_jbd(bh);
2014 : : journal_free_journal_head(jh);
2015 : 28 : }
2016 : :
2017 : : /*
2018 : : * Drop a reference on the passed journal_head. If it fell to zero then
2019 : : * release the journal_head from the buffer_head.
2020 : : */
2021 : 0 : void journal_put_journal_head(struct journal_head *jh)
2022 : : {
2023 : : struct buffer_head *bh = jh2bh(jh);
2024 : :
2025 : : jbd_lock_bh_journal_head(bh);
2026 [ - + ]: 230 : J_ASSERT_JH(jh, jh->b_jcount > 0);
2027 : 230 : --jh->b_jcount;
2028 [ + + ]: 230 : if (!jh->b_jcount) {
2029 : 28 : __journal_remove_journal_head(bh);
2030 : : jbd_unlock_bh_journal_head(bh);
2031 : 28 : __brelse(bh);
2032 : : } else
2033 : : jbd_unlock_bh_journal_head(bh);
2034 : 230 : }
2035 : :
2036 : : /*
2037 : : * debugfs tunables
2038 : : */
2039 : : #ifdef CONFIG_JBD_DEBUG
2040 : :
2041 : : u8 journal_enable_debug __read_mostly;
2042 : : EXPORT_SYMBOL(journal_enable_debug);
2043 : :
2044 : : static struct dentry *jbd_debugfs_dir;
2045 : : static struct dentry *jbd_debug;
2046 : :
2047 : : static void __init jbd_create_debugfs_entry(void)
2048 : : {
2049 : : jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
2050 : : if (jbd_debugfs_dir)
2051 : : jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
2052 : : jbd_debugfs_dir,
2053 : : &journal_enable_debug);
2054 : : }
2055 : :
2056 : : static void __exit jbd_remove_debugfs_entry(void)
2057 : : {
2058 : : debugfs_remove(jbd_debug);
2059 : : debugfs_remove(jbd_debugfs_dir);
2060 : : }
2061 : :
2062 : : #else
2063 : :
2064 : : static inline void jbd_create_debugfs_entry(void)
2065 : : {
2066 : : }
2067 : :
2068 : : static inline void jbd_remove_debugfs_entry(void)
2069 : : {
2070 : : }
2071 : :
2072 : : #endif
2073 : :
2074 : : struct kmem_cache *jbd_handle_cache;
2075 : :
2076 : 0 : static int __init journal_init_handle_cache(void)
2077 : : {
2078 : 0 : jbd_handle_cache = kmem_cache_create("journal_handle",
2079 : : sizeof(handle_t),
2080 : : 0, /* offset */
2081 : : SLAB_TEMPORARY, /* flags */
2082 : : NULL); /* ctor */
2083 [ # # ]: 0 : if (jbd_handle_cache == NULL) {
2084 : 0 : printk(KERN_EMERG "JBD: failed to create handle cache\n");
2085 : 0 : return -ENOMEM;
2086 : : }
2087 : : return 0;
2088 : : }
2089 : :
2090 : : static void journal_destroy_handle_cache(void)
2091 : : {
2092 [ # # ]: 0 : if (jbd_handle_cache)
2093 : 0 : kmem_cache_destroy(jbd_handle_cache);
2094 : : }
2095 : :
2096 : : /*
2097 : : * Module startup and shutdown
2098 : : */
2099 : :
2100 : 0 : static int __init journal_init_caches(void)
2101 : : {
2102 : : int ret;
2103 : :
2104 : 0 : ret = journal_init_revoke_caches();
2105 [ # # ]: 0 : if (ret == 0)
2106 : 0 : ret = journal_init_journal_head_cache();
2107 [ # # ]: 0 : if (ret == 0)
2108 : 0 : ret = journal_init_handle_cache();
2109 : 0 : return ret;
2110 : : }
2111 : :
2112 : 0 : static void journal_destroy_caches(void)
2113 : : {
2114 : 0 : journal_destroy_revoke_caches();
2115 : : journal_destroy_journal_head_cache();
2116 : : journal_destroy_handle_cache();
2117 : 0 : }
2118 : :
2119 : 0 : static int __init journal_init(void)
2120 : : {
2121 : : int ret;
2122 : :
2123 : : BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2124 : :
2125 : 0 : ret = journal_init_caches();
2126 [ # # ]: 0 : if (ret != 0)
2127 : 0 : journal_destroy_caches();
2128 : : jbd_create_debugfs_entry();
2129 : 0 : return ret;
2130 : : }
2131 : :
2132 : 0 : static void __exit journal_exit(void)
2133 : : {
2134 : : #ifdef CONFIG_JBD_DEBUG
2135 : : int n = atomic_read(&nr_journal_heads);
2136 : : if (n)
2137 : : printk(KERN_ERR "JBD: leaked %d journal_heads!\n", n);
2138 : : #endif
2139 : : jbd_remove_debugfs_entry();
2140 : 0 : journal_destroy_caches();
2141 : 0 : }
2142 : :
2143 : : MODULE_LICENSE("GPL");
2144 : : module_init(journal_init);
2145 : : module_exit(journal_exit);
2146 : :
|