1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Defines functions of journalling api
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/kthread.h>
32 #define MLOG_MASK_PREFIX ML_JOURNAL
33 #include <cluster/masklog.h>
38 #include "blockcheck.h"
41 #include "extent_map.h"
42 #include "heartbeat.h"
45 #include "localalloc.h"
51 #include "buffer_head_io.h"
53 DEFINE_SPINLOCK(trans_inc_lock);
55 static int ocfs2_force_read_journal(struct inode *inode);
56 static int ocfs2_recover_node(struct ocfs2_super *osb,
57 int node_num, int slot_num);
58 static int __ocfs2_recovery_thread(void *arg);
59 static int ocfs2_commit_cache(struct ocfs2_super *osb);
60 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
61 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
62 int dirty, int replayed);
63 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
65 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
67 static int ocfs2_commit_thread(void *arg);
69 static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
71 return __ocfs2_wait_on_mount(osb, 0);
74 static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
76 return __ocfs2_wait_on_mount(osb, 1);
79 int ocfs2_recovery_init(struct ocfs2_super *osb)
81 struct ocfs2_recovery_map *rm;
83 mutex_init(&osb->recovery_lock);
84 osb->disable_recovery = 0;
85 osb->recovery_thread_task = NULL;
86 init_waitqueue_head(&osb->recovery_event);
88 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
89 osb->max_slots * sizeof(unsigned int),
96 rm->rm_entries = (unsigned int *)((char *)rm +
97 sizeof(struct ocfs2_recovery_map));
98 osb->recovery_map = rm;
103 /* we can't grab the goofy sem lock from inside wait_event, so we use
104 * memory barriers to make sure that we'll see the null task before
106 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
109 return osb->recovery_thread_task != NULL;
112 void ocfs2_recovery_exit(struct ocfs2_super *osb)
114 struct ocfs2_recovery_map *rm;
116 /* disable any new recovery threads and wait for any currently
117 * running ones to exit. Do this before setting the vol_state. */
118 mutex_lock(&osb->recovery_lock);
119 osb->disable_recovery = 1;
120 mutex_unlock(&osb->recovery_lock);
121 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
123 /* At this point, we know that no more recovery threads can be
124 * launched, so wait for any recovery completion work to
126 flush_workqueue(ocfs2_wq);
129 * Now that recovery is shut down, and the osb is about to be
130 * freed, the osb_lock is not taken here.
132 rm = osb->recovery_map;
133 /* XXX: Should we bug if there are dirty entries? */
138 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
139 unsigned int node_num)
142 struct ocfs2_recovery_map *rm = osb->recovery_map;
144 assert_spin_locked(&osb->osb_lock);
146 for (i = 0; i < rm->rm_used; i++) {
147 if (rm->rm_entries[i] == node_num)
154 /* Behaves like test-and-set. Returns the previous value */
155 static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
156 unsigned int node_num)
158 struct ocfs2_recovery_map *rm = osb->recovery_map;
160 spin_lock(&osb->osb_lock);
161 if (__ocfs2_recovery_map_test(osb, node_num)) {
162 spin_unlock(&osb->osb_lock);
166 /* XXX: Can this be exploited? Not from o2dlm... */
167 BUG_ON(rm->rm_used >= osb->max_slots);
169 rm->rm_entries[rm->rm_used] = node_num;
171 spin_unlock(&osb->osb_lock);
176 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
177 unsigned int node_num)
180 struct ocfs2_recovery_map *rm = osb->recovery_map;
182 spin_lock(&osb->osb_lock);
184 for (i = 0; i < rm->rm_used; i++) {
185 if (rm->rm_entries[i] == node_num)
189 if (i < rm->rm_used) {
190 /* XXX: be careful with the pointer math */
191 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
192 (rm->rm_used - i - 1) * sizeof(unsigned int));
196 spin_unlock(&osb->osb_lock);
199 static int ocfs2_commit_cache(struct ocfs2_super *osb)
202 unsigned int flushed;
203 unsigned long old_id;
204 struct ocfs2_journal *journal = NULL;
208 journal = osb->journal;
210 /* Flush all pending commits and checkpoint the journal. */
211 down_write(&journal->j_trans_barrier);
213 if (atomic_read(&journal->j_num_trans) == 0) {
214 up_write(&journal->j_trans_barrier);
215 mlog(0, "No transactions for me to flush!\n");
219 jbd2_journal_lock_updates(journal->j_journal);
220 status = jbd2_journal_flush(journal->j_journal);
221 jbd2_journal_unlock_updates(journal->j_journal);
223 up_write(&journal->j_trans_barrier);
228 old_id = ocfs2_inc_trans_id(journal);
230 flushed = atomic_read(&journal->j_num_trans);
231 atomic_set(&journal->j_num_trans, 0);
232 up_write(&journal->j_trans_barrier);
234 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
235 journal->j_trans_id, flushed);
237 ocfs2_wake_downconvert_thread(osb);
238 wake_up(&journal->j_checkpointed);
244 /* pass it NULL and it will allocate a new handle object for you. If
245 * you pass it a handle however, it may still return error, in which
246 * case it has free'd the passed handle for you. */
247 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
249 journal_t *journal = osb->journal->j_journal;
252 BUG_ON(!osb || !osb->journal->j_journal);
254 if (ocfs2_is_hard_readonly(osb))
255 return ERR_PTR(-EROFS);
257 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
258 BUG_ON(max_buffs <= 0);
260 /* Nested transaction? Just return the handle... */
261 if (journal_current_handle())
262 return jbd2_journal_start(journal, max_buffs);
264 down_read(&osb->journal->j_trans_barrier);
266 handle = jbd2_journal_start(journal, max_buffs);
267 if (IS_ERR(handle)) {
268 up_read(&osb->journal->j_trans_barrier);
270 mlog_errno(PTR_ERR(handle));
272 if (is_journal_aborted(journal)) {
273 ocfs2_abort(osb->sb, "Detected aborted journal");
274 handle = ERR_PTR(-EROFS);
277 if (!ocfs2_mount_local(osb))
278 atomic_inc(&(osb->journal->j_num_trans));
284 int ocfs2_commit_trans(struct ocfs2_super *osb,
288 struct ocfs2_journal *journal = osb->journal;
292 nested = handle->h_ref > 1;
293 ret = jbd2_journal_stop(handle);
298 up_read(&journal->j_trans_barrier);
304 * 'nblocks' is what you want to add to the current
305 * transaction. extend_trans will either extend the current handle by
306 * nblocks, or commit it and start a new one with nblocks credits.
308 * This might call jbd2_journal_restart() which will commit dirty buffers
309 * and then restart the transaction. Before calling
310 * ocfs2_extend_trans(), any changed blocks should have been
311 * dirtied. After calling it, all blocks which need to be changed must
312 * go through another set of journal_access/journal_dirty calls.
314 * WARNING: This will not release any semaphores or disk locks taken
315 * during the transaction, so make sure they were taken *before*
316 * start_trans or we'll have ordering deadlocks.
318 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
319 * good because transaction ids haven't yet been recorded on the
320 * cluster locks associated with this handle.
322 int ocfs2_extend_trans(handle_t *handle, int nblocks)
331 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
333 #ifdef CONFIG_OCFS2_DEBUG_FS
336 status = jbd2_journal_extend(handle, nblocks);
345 "jbd2_journal_extend failed, trying "
346 "jbd2_journal_restart\n");
347 status = jbd2_journal_restart(handle, nblocks);
361 struct ocfs2_triggers {
362 struct jbd2_buffer_trigger_type ot_triggers;
366 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
368 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
371 static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
372 struct buffer_head *bh,
373 void *data, size_t size)
375 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
378 * We aren't guaranteed to have the superblock here, so we
379 * must unconditionally compute the ecc data.
380 * __ocfs2_journal_access() will only set the triggers if
381 * metaecc is enabled.
383 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
387 * Quota blocks have their own trigger because the struct ocfs2_block_check
388 * offset depends on the blocksize.
390 static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
391 struct buffer_head *bh,
392 void *data, size_t size)
394 struct ocfs2_disk_dqtrailer *dqt =
395 ocfs2_block_dqtrailer(size, data);
398 * We aren't guaranteed to have the superblock here, so we
399 * must unconditionally compute the ecc data.
400 * __ocfs2_journal_access() will only set the triggers if
401 * metaecc is enabled.
403 ocfs2_block_check_compute(data, size, &dqt->dq_check);
407 * Directory blocks also have their own trigger because the
408 * struct ocfs2_block_check offset depends on the blocksize.
410 static void ocfs2_db_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
411 struct buffer_head *bh,
412 void *data, size_t size)
414 struct ocfs2_dir_block_trailer *trailer =
415 ocfs2_dir_trailer_from_size(size, data);
418 * We aren't guaranteed to have the superblock here, so we
419 * must unconditionally compute the ecc data.
420 * __ocfs2_journal_access() will only set the triggers if
421 * metaecc is enabled.
423 ocfs2_block_check_compute(data, size, &trailer->db_check);
426 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
427 struct buffer_head *bh)
430 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
431 "bh->b_blocknr = %llu\n",
433 (unsigned long long)bh->b_blocknr);
435 /* We aren't guaranteed to have the superblock here - but if we
436 * don't, it'll just crash. */
437 ocfs2_error(bh->b_assoc_map->host->i_sb,
438 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
441 static struct ocfs2_triggers di_triggers = {
443 .t_commit = ocfs2_commit_trigger,
444 .t_abort = ocfs2_abort_trigger,
446 .ot_offset = offsetof(struct ocfs2_dinode, i_check),
449 static struct ocfs2_triggers eb_triggers = {
451 .t_commit = ocfs2_commit_trigger,
452 .t_abort = ocfs2_abort_trigger,
454 .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
457 static struct ocfs2_triggers gd_triggers = {
459 .t_commit = ocfs2_commit_trigger,
460 .t_abort = ocfs2_abort_trigger,
462 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
465 static struct ocfs2_triggers db_triggers = {
467 .t_commit = ocfs2_db_commit_trigger,
468 .t_abort = ocfs2_abort_trigger,
472 static struct ocfs2_triggers xb_triggers = {
474 .t_commit = ocfs2_commit_trigger,
475 .t_abort = ocfs2_abort_trigger,
477 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
480 static struct ocfs2_triggers dq_triggers = {
482 .t_commit = ocfs2_dq_commit_trigger,
483 .t_abort = ocfs2_abort_trigger,
487 static int __ocfs2_journal_access(handle_t *handle,
489 struct buffer_head *bh,
490 struct ocfs2_triggers *triggers,
499 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
500 (unsigned long long)bh->b_blocknr, type,
501 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
502 "OCFS2_JOURNAL_ACCESS_CREATE" :
503 "OCFS2_JOURNAL_ACCESS_WRITE",
506 /* we can safely remove this assertion after testing. */
507 if (!buffer_uptodate(bh)) {
508 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
509 mlog(ML_ERROR, "b_blocknr=%llu\n",
510 (unsigned long long)bh->b_blocknr);
514 /* Set the current transaction information on the inode so
515 * that the locking code knows whether it can drop it's locks
516 * on this inode or not. We're protected from the commit
517 * thread updating the current transaction id until
518 * ocfs2_commit_trans() because ocfs2_start_trans() took
519 * j_trans_barrier for us. */
520 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
522 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
524 case OCFS2_JOURNAL_ACCESS_CREATE:
525 case OCFS2_JOURNAL_ACCESS_WRITE:
526 status = jbd2_journal_get_write_access(handle, bh);
529 case OCFS2_JOURNAL_ACCESS_UNDO:
530 status = jbd2_journal_get_undo_access(handle, bh);
535 mlog(ML_ERROR, "Uknown access type!\n");
537 if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
538 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
539 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
542 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
549 int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
550 struct buffer_head *bh, int type)
552 return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
556 int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
557 struct buffer_head *bh, int type)
559 return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
563 int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
564 struct buffer_head *bh, int type)
566 return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
570 int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
571 struct buffer_head *bh, int type)
573 return __ocfs2_journal_access(handle, inode, bh, &db_triggers,
577 int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
578 struct buffer_head *bh, int type)
580 return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
584 int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
585 struct buffer_head *bh, int type)
587 return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
591 int ocfs2_journal_access(handle_t *handle, struct inode *inode,
592 struct buffer_head *bh, int type)
594 return __ocfs2_journal_access(handle, inode, bh, NULL, type);
597 int ocfs2_journal_dirty(handle_t *handle,
598 struct buffer_head *bh)
602 mlog_entry("(bh->b_blocknr=%llu)\n",
603 (unsigned long long)bh->b_blocknr);
605 status = jbd2_journal_dirty_metadata(handle, bh);
607 mlog(ML_ERROR, "Could not dirty metadata buffer. "
608 "(bh->b_blocknr=%llu)\n",
609 (unsigned long long)bh->b_blocknr);
615 #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
617 void ocfs2_set_journal_params(struct ocfs2_super *osb)
619 journal_t *journal = osb->journal->j_journal;
620 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
622 if (osb->osb_commit_interval)
623 commit_interval = osb->osb_commit_interval;
625 spin_lock(&journal->j_state_lock);
626 journal->j_commit_interval = commit_interval;
627 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
628 journal->j_flags |= JBD2_BARRIER;
630 journal->j_flags &= ~JBD2_BARRIER;
631 spin_unlock(&journal->j_state_lock);
634 int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
637 struct inode *inode = NULL; /* the journal inode */
638 journal_t *j_journal = NULL;
639 struct ocfs2_dinode *di = NULL;
640 struct buffer_head *bh = NULL;
641 struct ocfs2_super *osb;
648 osb = journal->j_osb;
650 /* already have the inode for our journal */
651 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
658 if (is_bad_inode(inode)) {
659 mlog(ML_ERROR, "access error (bad inode)\n");
666 SET_INODE_JOURNAL(inode);
667 OCFS2_I(inode)->ip_open_count++;
669 /* Skip recovery waits here - journal inode metadata never
670 * changes in a live cluster so it can be considered an
671 * exception to the rule. */
672 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
674 if (status != -ERESTARTSYS)
675 mlog(ML_ERROR, "Could not get lock on journal!\n");
680 di = (struct ocfs2_dinode *)bh->b_data;
682 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
683 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
689 mlog(0, "inode->i_size = %lld\n", inode->i_size);
690 mlog(0, "inode->i_blocks = %llu\n",
691 (unsigned long long)inode->i_blocks);
692 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
694 /* call the kernels journal init function now */
695 j_journal = jbd2_journal_init_inode(inode);
696 if (j_journal == NULL) {
697 mlog(ML_ERROR, "Linux journal layer error\n");
702 mlog(0, "Returned from jbd2_journal_init_inode\n");
703 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
705 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
706 OCFS2_JOURNAL_DIRTY_FL);
708 journal->j_journal = j_journal;
709 journal->j_inode = inode;
712 ocfs2_set_journal_params(osb);
714 journal->j_state = OCFS2_JOURNAL_LOADED;
720 ocfs2_inode_unlock(inode, 1);
723 OCFS2_I(inode)->ip_open_count--;
732 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
734 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
737 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
739 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
742 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
743 int dirty, int replayed)
747 struct ocfs2_journal *journal = osb->journal;
748 struct buffer_head *bh = journal->j_bh;
749 struct ocfs2_dinode *fe;
753 fe = (struct ocfs2_dinode *)bh->b_data;
755 /* The journal bh on the osb always comes from ocfs2_journal_init()
756 * and was validated there inside ocfs2_inode_lock_full(). It's a
757 * code bug if we mess it up. */
758 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
760 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
762 flags |= OCFS2_JOURNAL_DIRTY_FL;
764 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
765 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
768 ocfs2_bump_recovery_generation(fe);
770 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
771 status = ocfs2_write_block(osb, bh, journal->j_inode);
780 * If the journal has been kmalloc'd it needs to be freed after this
783 void ocfs2_journal_shutdown(struct ocfs2_super *osb)
785 struct ocfs2_journal *journal = NULL;
787 struct inode *inode = NULL;
788 int num_running_trans = 0;
794 journal = osb->journal;
798 inode = journal->j_inode;
800 if (journal->j_state != OCFS2_JOURNAL_LOADED)
803 /* need to inc inode use count - jbd2_journal_destroy will iput. */
807 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
808 if (num_running_trans > 0)
809 mlog(0, "Shutting down journal: must wait on %d "
810 "running transactions!\n",
813 /* Do a commit_cache here. It will flush our journal, *and*
814 * release any locks that are still held.
815 * set the SHUTDOWN flag and release the trans lock.
816 * the commit thread will take the trans lock for us below. */
817 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
819 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
820 * drop the trans_lock (which we want to hold until we
821 * completely destroy the journal. */
822 if (osb->commit_task) {
823 /* Wait for the commit thread */
824 mlog(0, "Waiting for ocfs2commit to exit....\n");
825 kthread_stop(osb->commit_task);
826 osb->commit_task = NULL;
829 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
831 if (ocfs2_mount_local(osb)) {
832 jbd2_journal_lock_updates(journal->j_journal);
833 status = jbd2_journal_flush(journal->j_journal);
834 jbd2_journal_unlock_updates(journal->j_journal);
841 * Do not toggle if flush was unsuccessful otherwise
842 * will leave dirty metadata in a "clean" journal
844 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
849 /* Shutdown the kernel journal system */
850 jbd2_journal_destroy(journal->j_journal);
851 journal->j_journal = NULL;
853 OCFS2_I(inode)->ip_open_count--;
855 /* unlock our journal */
856 ocfs2_inode_unlock(inode, 1);
858 brelse(journal->j_bh);
859 journal->j_bh = NULL;
861 journal->j_state = OCFS2_JOURNAL_FREE;
863 // up_write(&journal->j_trans_barrier);
870 static void ocfs2_clear_journal_error(struct super_block *sb,
876 olderr = jbd2_journal_errno(journal);
878 mlog(ML_ERROR, "File system error %d recorded in "
879 "journal %u.\n", olderr, slot);
880 mlog(ML_ERROR, "File system on device %s needs checking.\n",
883 jbd2_journal_ack_err(journal);
884 jbd2_journal_clear_err(journal);
888 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
891 struct ocfs2_super *osb;
897 osb = journal->j_osb;
899 status = jbd2_journal_load(journal->j_journal);
901 mlog(ML_ERROR, "Failed to load journal!\n");
905 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
907 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
913 /* Launch the commit thread */
915 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
917 if (IS_ERR(osb->commit_task)) {
918 status = PTR_ERR(osb->commit_task);
919 osb->commit_task = NULL;
920 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
925 osb->commit_task = NULL;
933 /* 'full' flag tells us whether we clear out all blocks or if we just
934 * mark the journal clean */
935 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
943 status = jbd2_journal_wipe(journal->j_journal, full);
949 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
958 static int ocfs2_recovery_completed(struct ocfs2_super *osb)
961 struct ocfs2_recovery_map *rm = osb->recovery_map;
963 spin_lock(&osb->osb_lock);
964 empty = (rm->rm_used == 0);
965 spin_unlock(&osb->osb_lock);
970 void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
972 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
976 * JBD Might read a cached version of another nodes journal file. We
977 * don't want this as this file changes often and we get no
978 * notification on those changes. The only way to be sure that we've
979 * got the most up to date version of those blocks then is to force
980 * read them off disk. Just searching through the buffer cache won't
981 * work as there may be pages backing this file which are still marked
982 * up to date. We know things can't change on this file underneath us
983 * as we have the lock by now :)
985 static int ocfs2_force_read_journal(struct inode *inode)
989 u64 v_blkno, p_blkno, p_blocks, num_blocks;
990 #define CONCURRENT_JOURNAL_FILL 32ULL
991 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
995 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
997 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
999 while (v_blkno < num_blocks) {
1000 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
1001 &p_blkno, &p_blocks, NULL);
1007 if (p_blocks > CONCURRENT_JOURNAL_FILL)
1008 p_blocks = CONCURRENT_JOURNAL_FILL;
1010 /* We are reading journal data which should not
1011 * be put in the uptodate cache */
1012 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
1013 p_blkno, p_blocks, bhs);
1019 for(i = 0; i < p_blocks; i++) {
1024 v_blkno += p_blocks;
1028 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
1034 struct ocfs2_la_recovery_item {
1035 struct list_head lri_list;
1037 struct ocfs2_dinode *lri_la_dinode;
1038 struct ocfs2_dinode *lri_tl_dinode;
1039 struct ocfs2_quota_recovery *lri_qrec;
1042 /* Does the second half of the recovery process. By this point, the
1043 * node is marked clean and can actually be considered recovered,
1044 * hence it's no longer in the recovery map, but there's still some
1045 * cleanup we can do which shouldn't happen within the recovery thread
1046 * as locking in that context becomes very difficult if we are to take
1047 * recovering nodes into account.
1049 * NOTE: This function can and will sleep on recovery of other nodes
1050 * during cluster locking, just like any other ocfs2 process.
1052 void ocfs2_complete_recovery(struct work_struct *work)
1055 struct ocfs2_journal *journal =
1056 container_of(work, struct ocfs2_journal, j_recovery_work);
1057 struct ocfs2_super *osb = journal->j_osb;
1058 struct ocfs2_dinode *la_dinode, *tl_dinode;
1059 struct ocfs2_la_recovery_item *item, *n;
1060 struct ocfs2_quota_recovery *qrec;
1061 LIST_HEAD(tmp_la_list);
1065 mlog(0, "completing recovery from keventd\n");
1067 spin_lock(&journal->j_lock);
1068 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1069 spin_unlock(&journal->j_lock);
1071 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
1072 list_del_init(&item->lri_list);
1074 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1076 ocfs2_wait_on_quotas(osb);
1078 la_dinode = item->lri_la_dinode;
1080 mlog(0, "Clean up local alloc %llu\n",
1081 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
1083 ret = ocfs2_complete_local_alloc_recovery(osb,
1091 tl_dinode = item->lri_tl_dinode;
1093 mlog(0, "Clean up truncate log %llu\n",
1094 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
1096 ret = ocfs2_complete_truncate_log_recovery(osb,
1104 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1108 qrec = item->lri_qrec;
1110 mlog(0, "Recovering quota files");
1111 ret = ocfs2_finish_quota_recovery(osb, qrec,
1115 /* Recovery info is already freed now */
1121 mlog(0, "Recovery completion\n");
1125 /* NOTE: This function always eats your references to la_dinode and
1126 * tl_dinode, either manually on error, or by passing them to
1127 * ocfs2_complete_recovery */
1128 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1130 struct ocfs2_dinode *la_dinode,
1131 struct ocfs2_dinode *tl_dinode,
1132 struct ocfs2_quota_recovery *qrec)
1134 struct ocfs2_la_recovery_item *item;
1136 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
1138 /* Though we wish to avoid it, we are in fact safe in
1139 * skipping local alloc cleanup as fsck.ocfs2 is more
1140 * than capable of reclaiming unused space. */
1148 ocfs2_free_quota_recovery(qrec);
1150 mlog_errno(-ENOMEM);
1154 INIT_LIST_HEAD(&item->lri_list);
1155 item->lri_la_dinode = la_dinode;
1156 item->lri_slot = slot_num;
1157 item->lri_tl_dinode = tl_dinode;
1158 item->lri_qrec = qrec;
1160 spin_lock(&journal->j_lock);
1161 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1162 queue_work(ocfs2_wq, &journal->j_recovery_work);
1163 spin_unlock(&journal->j_lock);
1166 /* Called by the mount code to queue recovery the last part of
1167 * recovery for it's own slot. */
1168 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1170 struct ocfs2_journal *journal = osb->journal;
1173 /* No need to queue up our truncate_log as regular
1174 * cleanup will catch that. */
1175 ocfs2_queue_recovery_completion(journal,
1177 osb->local_alloc_copy,
1180 ocfs2_schedule_truncate_log_flush(osb, 0);
1182 osb->local_alloc_copy = NULL;
1187 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1189 if (osb->quota_rec) {
1190 ocfs2_queue_recovery_completion(osb->journal,
1195 osb->quota_rec = NULL;
1199 static int __ocfs2_recovery_thread(void *arg)
1201 int status, node_num, slot_num;
1202 struct ocfs2_super *osb = arg;
1203 struct ocfs2_recovery_map *rm = osb->recovery_map;
1204 int *rm_quota = NULL;
1205 int rm_quota_used = 0, i;
1206 struct ocfs2_quota_recovery *qrec;
1210 status = ocfs2_wait_on_mount(osb);
1215 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1221 status = ocfs2_super_lock(osb, 1);
1227 spin_lock(&osb->osb_lock);
1228 while (rm->rm_used) {
1229 /* It's always safe to remove entry zero, as we won't
1230 * clear it until ocfs2_recover_node() has succeeded. */
1231 node_num = rm->rm_entries[0];
1232 spin_unlock(&osb->osb_lock);
1233 mlog(0, "checking node %d\n", node_num);
1234 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1235 if (slot_num == -ENOENT) {
1237 mlog(0, "no slot for this node, so no recovery"
1241 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1243 /* It is a bit subtle with quota recovery. We cannot do it
1244 * immediately because we have to obtain cluster locks from
1245 * quota files and we also don't want to just skip it because
1246 * then quota usage would be out of sync until some node takes
1247 * the slot. So we remember which nodes need quota recovery
1248 * and when everything else is done, we recover quotas. */
1249 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1250 if (i == rm_quota_used)
1251 rm_quota[rm_quota_used++] = slot_num;
1253 status = ocfs2_recover_node(osb, node_num, slot_num);
1256 ocfs2_recovery_map_clear(osb, node_num);
1259 "Error %d recovering node %d on device (%u,%u)!\n",
1261 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1262 mlog(ML_ERROR, "Volume requires unmount.\n");
1265 spin_lock(&osb->osb_lock);
1267 spin_unlock(&osb->osb_lock);
1268 mlog(0, "All nodes recovered\n");
1270 /* Refresh all journal recovery generations from disk */
1271 status = ocfs2_check_journals_nolocks(osb);
1272 status = (status == -EROFS) ? 0 : status;
1276 /* Now it is right time to recover quotas... We have to do this under
1277 * superblock lock so that noone can start using the slot (and crash)
1278 * before we recover it */
1279 for (i = 0; i < rm_quota_used; i++) {
1280 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1282 status = PTR_ERR(qrec);
1286 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1290 ocfs2_super_unlock(osb, 1);
1292 /* We always run recovery on our own orphan dir - the dead
1293 * node(s) may have disallowd a previos inode delete. Re-processing
1294 * is therefore required. */
1295 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1299 mutex_lock(&osb->recovery_lock);
1300 if (!status && !ocfs2_recovery_completed(osb)) {
1301 mutex_unlock(&osb->recovery_lock);
1305 osb->recovery_thread_task = NULL;
1306 mb(); /* sync with ocfs2_recovery_thread_running */
1307 wake_up(&osb->recovery_event);
1309 mutex_unlock(&osb->recovery_lock);
1315 /* no one is callint kthread_stop() for us so the kthread() api
1316 * requires that we call do_exit(). And it isn't exported, but
1317 * complete_and_exit() seems to be a minimal wrapper around it. */
1318 complete_and_exit(NULL, status);
1322 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1324 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1325 node_num, osb->node_num);
1327 mutex_lock(&osb->recovery_lock);
1328 if (osb->disable_recovery)
1331 /* People waiting on recovery will wait on
1332 * the recovery map to empty. */
1333 if (ocfs2_recovery_map_set(osb, node_num))
1334 mlog(0, "node %d already in recovery map.\n", node_num);
1336 mlog(0, "starting recovery thread...\n");
1338 if (osb->recovery_thread_task)
1341 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
1343 if (IS_ERR(osb->recovery_thread_task)) {
1344 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1345 osb->recovery_thread_task = NULL;
1349 mutex_unlock(&osb->recovery_lock);
1350 wake_up(&osb->recovery_event);
1355 static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1357 struct buffer_head **bh,
1358 struct inode **ret_inode)
1360 int status = -EACCES;
1361 struct inode *inode = NULL;
1363 BUG_ON(slot_num >= osb->max_slots);
1365 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1367 if (!inode || is_bad_inode(inode)) {
1371 SET_INODE_JOURNAL(inode);
1373 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
1383 if (status || !ret_inode)
1391 /* Does the actual journal replay and marks the journal inode as
1392 * clean. Will only replay if the journal inode is marked dirty. */
1393 static int ocfs2_replay_journal(struct ocfs2_super *osb,
1400 struct inode *inode = NULL;
1401 struct ocfs2_dinode *fe;
1402 journal_t *journal = NULL;
1403 struct buffer_head *bh = NULL;
1406 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1412 fe = (struct ocfs2_dinode *)bh->b_data;
1413 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1418 * As the fs recovery is asynchronous, there is a small chance that
1419 * another node mounted (and recovered) the slot before the recovery
1420 * thread could get the lock. To handle that, we dirty read the journal
1421 * inode for that slot to get the recovery generation. If it is
1422 * different than what we expected, the slot has been recovered.
1423 * If not, it needs recovery.
1425 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1426 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1427 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1428 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1433 /* Continue with recovery as the journal has not yet been recovered */
1435 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
1437 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
1438 if (status != -ERESTARTSYS)
1439 mlog(ML_ERROR, "Could not lock journal!\n");
1444 fe = (struct ocfs2_dinode *) bh->b_data;
1446 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1447 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1449 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1450 mlog(0, "No recovery required for node %d\n", node_num);
1451 /* Refresh recovery generation for the slot */
1452 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1456 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1458 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1460 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1462 status = ocfs2_force_read_journal(inode);
1468 mlog(0, "calling journal_init_inode\n");
1469 journal = jbd2_journal_init_inode(inode);
1470 if (journal == NULL) {
1471 mlog(ML_ERROR, "Linux journal layer error\n");
1476 status = jbd2_journal_load(journal);
1481 jbd2_journal_destroy(journal);
1485 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1487 /* wipe the journal */
1488 mlog(0, "flushing the journal.\n");
1489 jbd2_journal_lock_updates(journal);
1490 status = jbd2_journal_flush(journal);
1491 jbd2_journal_unlock_updates(journal);
1495 /* This will mark the node clean */
1496 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1497 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1498 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1500 /* Increment recovery generation to indicate successful recovery */
1501 ocfs2_bump_recovery_generation(fe);
1502 osb->slot_recovery_generations[slot_num] =
1503 ocfs2_get_recovery_generation(fe);
1505 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1506 status = ocfs2_write_block(osb, bh, inode);
1513 jbd2_journal_destroy(journal);
1516 /* drop the lock on this nodes journal */
1518 ocfs2_inode_unlock(inode, 1);
1530 * Do the most important parts of node recovery:
1531 * - Replay it's journal
1532 * - Stamp a clean local allocator file
1533 * - Stamp a clean truncate log
1534 * - Mark the node clean
1536 * If this function completes without error, a node in OCFS2 can be
1537 * said to have been safely recovered. As a result, failure during the
1538 * second part of a nodes recovery process (local alloc recovery) is
1539 * far less concerning.
1541 static int ocfs2_recover_node(struct ocfs2_super *osb,
1542 int node_num, int slot_num)
1545 struct ocfs2_dinode *la_copy = NULL;
1546 struct ocfs2_dinode *tl_copy = NULL;
1548 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1549 node_num, slot_num, osb->node_num);
1551 /* Should not ever be called to recover ourselves -- in that
1552 * case we should've called ocfs2_journal_load instead. */
1553 BUG_ON(osb->node_num == node_num);
1555 status = ocfs2_replay_journal(osb, node_num, slot_num);
1557 if (status == -EBUSY) {
1558 mlog(0, "Skipping recovery for slot %u (node %u) "
1559 "as another node has recovered it\n", slot_num,
1568 /* Stamp a clean local alloc file AFTER recovering the journal... */
1569 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1575 /* An error from begin_truncate_log_recovery is not
1576 * serious enough to warrant halting the rest of
1578 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1582 /* Likewise, this would be a strange but ultimately not so
1583 * harmful place to get an error... */
1584 status = ocfs2_clear_slot(osb, slot_num);
1588 /* This will kfree the memory pointed to by la_copy and tl_copy */
1589 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1599 /* Test node liveness by trylocking his journal. If we get the lock,
1600 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1601 * still alive (we couldn't get the lock) and < 0 on error. */
1602 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1606 struct inode *inode = NULL;
1608 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1610 if (inode == NULL) {
1611 mlog(ML_ERROR, "access error\n");
1615 if (is_bad_inode(inode)) {
1616 mlog(ML_ERROR, "access error (bad inode)\n");
1622 SET_INODE_JOURNAL(inode);
1624 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
1625 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1627 if (status != -EAGAIN)
1632 ocfs2_inode_unlock(inode, 1);
1640 /* Call this underneath ocfs2_super_lock. It also assumes that the
1641 * slot info struct has been updated from disk. */
1642 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1644 unsigned int node_num;
1647 struct buffer_head *bh = NULL;
1648 struct ocfs2_dinode *di;
1650 /* This is called with the super block cluster lock, so we
1651 * know that the slot map can't change underneath us. */
1653 for (i = 0; i < osb->max_slots; i++) {
1654 /* Read journal inode to get the recovery generation */
1655 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1660 di = (struct ocfs2_dinode *)bh->b_data;
1661 gen = ocfs2_get_recovery_generation(di);
1665 spin_lock(&osb->osb_lock);
1666 osb->slot_recovery_generations[i] = gen;
1668 mlog(0, "Slot %u recovery generation is %u\n", i,
1669 osb->slot_recovery_generations[i]);
1671 if (i == osb->slot_num) {
1672 spin_unlock(&osb->osb_lock);
1676 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1677 if (status == -ENOENT) {
1678 spin_unlock(&osb->osb_lock);
1682 if (__ocfs2_recovery_map_test(osb, node_num)) {
1683 spin_unlock(&osb->osb_lock);
1686 spin_unlock(&osb->osb_lock);
1688 /* Ok, we have a slot occupied by another node which
1689 * is not in the recovery map. We trylock his journal
1690 * file here to test if he's alive. */
1691 status = ocfs2_trylock_journal(osb, i);
1693 /* Since we're called from mount, we know that
1694 * the recovery thread can't race us on
1695 * setting / checking the recovery bits. */
1696 ocfs2_recovery_thread(osb, node_num);
1697 } else if ((status < 0) && (status != -EAGAIN)) {
1709 struct ocfs2_orphan_filldir_priv {
1711 struct ocfs2_super *osb;
1714 static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1715 loff_t pos, u64 ino, unsigned type)
1717 struct ocfs2_orphan_filldir_priv *p = priv;
1720 if (name_len == 1 && !strncmp(".", name, 1))
1722 if (name_len == 2 && !strncmp("..", name, 2))
1725 /* Skip bad inodes so that recovery can continue */
1726 iter = ocfs2_iget(p->osb, ino,
1727 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
1731 mlog(0, "queue orphan %llu\n",
1732 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1733 /* No locking is required for the next_orphan queue as there
1734 * is only ever a single process doing orphan recovery. */
1735 OCFS2_I(iter)->ip_next_orphan = p->head;
1741 static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1743 struct inode **head)
1746 struct inode *orphan_dir_inode = NULL;
1747 struct ocfs2_orphan_filldir_priv priv;
1753 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1754 ORPHAN_DIR_SYSTEM_INODE,
1756 if (!orphan_dir_inode) {
1762 mutex_lock(&orphan_dir_inode->i_mutex);
1763 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
1769 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1770 ocfs2_orphan_filldir);
1779 ocfs2_inode_unlock(orphan_dir_inode, 0);
1781 mutex_unlock(&orphan_dir_inode->i_mutex);
1782 iput(orphan_dir_inode);
1786 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1791 spin_lock(&osb->osb_lock);
1792 ret = !osb->osb_orphan_wipes[slot];
1793 spin_unlock(&osb->osb_lock);
1797 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1800 spin_lock(&osb->osb_lock);
1801 /* Mark ourselves such that new processes in delete_inode()
1802 * know to quit early. */
1803 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1804 while (osb->osb_orphan_wipes[slot]) {
1805 /* If any processes are already in the middle of an
1806 * orphan wipe on this dir, then we need to wait for
1808 spin_unlock(&osb->osb_lock);
1809 wait_event_interruptible(osb->osb_wipe_event,
1810 ocfs2_orphan_recovery_can_continue(osb, slot));
1811 spin_lock(&osb->osb_lock);
1813 spin_unlock(&osb->osb_lock);
1816 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1819 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1823 * Orphan recovery. Each mounted node has it's own orphan dir which we
1824 * must run during recovery. Our strategy here is to build a list of
1825 * the inodes in the orphan dir and iget/iput them. The VFS does
1826 * (most) of the rest of the work.
1828 * Orphan recovery can happen at any time, not just mount so we have a
1829 * couple of extra considerations.
1831 * - We grab as many inodes as we can under the orphan dir lock -
1832 * doing iget() outside the orphan dir risks getting a reference on
1834 * - We must be sure not to deadlock with other processes on the
1835 * system wanting to run delete_inode(). This can happen when they go
1836 * to lock the orphan dir and the orphan recovery process attempts to
1837 * iget() inside the orphan dir lock. This can be avoided by
1838 * advertising our state to ocfs2_delete_inode().
1840 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1844 struct inode *inode = NULL;
1846 struct ocfs2_inode_info *oi;
1848 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1850 ocfs2_mark_recovering_orphan_dir(osb, slot);
1851 ret = ocfs2_queue_orphans(osb, slot, &inode);
1852 ocfs2_clear_recovering_orphan_dir(osb, slot);
1854 /* Error here should be noted, but we want to continue with as
1855 * many queued inodes as we've got. */
1860 oi = OCFS2_I(inode);
1861 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
1863 iter = oi->ip_next_orphan;
1865 spin_lock(&oi->ip_lock);
1866 /* The remote delete code may have set these on the
1867 * assumption that the other node would wipe them
1868 * successfully. If they are still in the node's
1869 * orphan dir, we need to reset that state. */
1870 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1872 /* Set the proper information to get us going into
1873 * ocfs2_delete_inode. */
1874 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
1875 spin_unlock(&oi->ip_lock);
1885 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
1887 /* This check is good because ocfs2 will wait on our recovery
1888 * thread before changing it to something other than MOUNTED
1890 wait_event(osb->osb_mount_event,
1891 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
1892 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
1893 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1895 /* If there's an error on mount, then we may never get to the
1896 * MOUNTED flag, but this is set right before
1897 * dismount_volume() so we can trust it. */
1898 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1899 mlog(0, "mount error, exiting!\n");
1906 static int ocfs2_commit_thread(void *arg)
1909 struct ocfs2_super *osb = arg;
1910 struct ocfs2_journal *journal = osb->journal;
1912 /* we can trust j_num_trans here because _should_stop() is only set in
1913 * shutdown and nobody other than ourselves should be able to start
1914 * transactions. committing on shutdown might take a few iterations
1915 * as final transactions put deleted inodes on the list */
1916 while (!(kthread_should_stop() &&
1917 atomic_read(&journal->j_num_trans) == 0)) {
1919 wait_event_interruptible(osb->checkpoint_event,
1920 atomic_read(&journal->j_num_trans)
1921 || kthread_should_stop());
1923 status = ocfs2_commit_cache(osb);
1927 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1929 "commit_thread: %u transactions pending on "
1931 atomic_read(&journal->j_num_trans));
1938 /* Reads all the journal inodes without taking any cluster locks. Used
1939 * for hard readonly access to determine whether any journal requires
1940 * recovery. Also used to refresh the recovery generation numbers after
1941 * a journal has been recovered by another node.
1943 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1947 struct buffer_head *di_bh = NULL;
1948 struct ocfs2_dinode *di;
1949 int journal_dirty = 0;
1951 for(slot = 0; slot < osb->max_slots; slot++) {
1952 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1958 di = (struct ocfs2_dinode *) di_bh->b_data;
1960 osb->slot_recovery_generations[slot] =
1961 ocfs2_get_recovery_generation(di);
1963 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1964 OCFS2_JOURNAL_DIRTY_FL)