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);
68 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
70 struct ocfs2_dinode *la_dinode,
71 struct ocfs2_dinode *tl_dinode,
72 struct ocfs2_quota_recovery *qrec);
74 static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
76 return __ocfs2_wait_on_mount(osb, 0);
79 static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
81 return __ocfs2_wait_on_mount(osb, 1);
85 * This replay_map is to track online/offline slots, so we could recover
86 * offline slots during recovery and mount
89 enum ocfs2_replay_state {
90 REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */
91 REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */
92 REPLAY_DONE /* Replay was already queued */
95 struct ocfs2_replay_map {
96 unsigned int rm_slots;
97 enum ocfs2_replay_state rm_state;
98 unsigned char rm_replay_slots[0];
101 void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
103 if (!osb->replay_map)
106 /* If we've already queued the replay, we don't have any more to do */
107 if (osb->replay_map->rm_state == REPLAY_DONE)
110 osb->replay_map->rm_state = state;
113 int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
115 struct ocfs2_replay_map *replay_map;
118 /* If replay map is already set, we don't do it again */
122 replay_map = kzalloc(sizeof(struct ocfs2_replay_map) +
123 (osb->max_slots * sizeof(char)), GFP_KERNEL);
130 spin_lock(&osb->osb_lock);
132 replay_map->rm_slots = osb->max_slots;
133 replay_map->rm_state = REPLAY_UNNEEDED;
135 /* set rm_replay_slots for offline slot(s) */
136 for (i = 0; i < replay_map->rm_slots; i++) {
137 if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
138 replay_map->rm_replay_slots[i] = 1;
141 osb->replay_map = replay_map;
142 spin_unlock(&osb->osb_lock);
146 void ocfs2_queue_replay_slots(struct ocfs2_super *osb)
148 struct ocfs2_replay_map *replay_map = osb->replay_map;
154 if (replay_map->rm_state != REPLAY_NEEDED)
157 for (i = 0; i < replay_map->rm_slots; i++)
158 if (replay_map->rm_replay_slots[i])
159 ocfs2_queue_recovery_completion(osb->journal, i, NULL,
161 replay_map->rm_state = REPLAY_DONE;
164 void ocfs2_free_replay_slots(struct ocfs2_super *osb)
166 struct ocfs2_replay_map *replay_map = osb->replay_map;
168 if (!osb->replay_map)
172 osb->replay_map = NULL;
175 int ocfs2_recovery_init(struct ocfs2_super *osb)
177 struct ocfs2_recovery_map *rm;
179 mutex_init(&osb->recovery_lock);
180 osb->disable_recovery = 0;
181 osb->recovery_thread_task = NULL;
182 init_waitqueue_head(&osb->recovery_event);
184 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
185 osb->max_slots * sizeof(unsigned int),
192 rm->rm_entries = (unsigned int *)((char *)rm +
193 sizeof(struct ocfs2_recovery_map));
194 osb->recovery_map = rm;
199 /* we can't grab the goofy sem lock from inside wait_event, so we use
200 * memory barriers to make sure that we'll see the null task before
202 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
205 return osb->recovery_thread_task != NULL;
208 void ocfs2_recovery_exit(struct ocfs2_super *osb)
210 struct ocfs2_recovery_map *rm;
212 /* disable any new recovery threads and wait for any currently
213 * running ones to exit. Do this before setting the vol_state. */
214 mutex_lock(&osb->recovery_lock);
215 osb->disable_recovery = 1;
216 mutex_unlock(&osb->recovery_lock);
217 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
219 /* At this point, we know that no more recovery threads can be
220 * launched, so wait for any recovery completion work to
222 flush_workqueue(ocfs2_wq);
225 * Now that recovery is shut down, and the osb is about to be
226 * freed, the osb_lock is not taken here.
228 rm = osb->recovery_map;
229 /* XXX: Should we bug if there are dirty entries? */
234 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
235 unsigned int node_num)
238 struct ocfs2_recovery_map *rm = osb->recovery_map;
240 assert_spin_locked(&osb->osb_lock);
242 for (i = 0; i < rm->rm_used; i++) {
243 if (rm->rm_entries[i] == node_num)
250 /* Behaves like test-and-set. Returns the previous value */
251 static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
252 unsigned int node_num)
254 struct ocfs2_recovery_map *rm = osb->recovery_map;
256 spin_lock(&osb->osb_lock);
257 if (__ocfs2_recovery_map_test(osb, node_num)) {
258 spin_unlock(&osb->osb_lock);
262 /* XXX: Can this be exploited? Not from o2dlm... */
263 BUG_ON(rm->rm_used >= osb->max_slots);
265 rm->rm_entries[rm->rm_used] = node_num;
267 spin_unlock(&osb->osb_lock);
272 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
273 unsigned int node_num)
276 struct ocfs2_recovery_map *rm = osb->recovery_map;
278 spin_lock(&osb->osb_lock);
280 for (i = 0; i < rm->rm_used; i++) {
281 if (rm->rm_entries[i] == node_num)
285 if (i < rm->rm_used) {
286 /* XXX: be careful with the pointer math */
287 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
288 (rm->rm_used - i - 1) * sizeof(unsigned int));
292 spin_unlock(&osb->osb_lock);
295 static int ocfs2_commit_cache(struct ocfs2_super *osb)
298 unsigned int flushed;
299 unsigned long old_id;
300 struct ocfs2_journal *journal = NULL;
304 journal = osb->journal;
306 /* Flush all pending commits and checkpoint the journal. */
307 down_write(&journal->j_trans_barrier);
309 if (atomic_read(&journal->j_num_trans) == 0) {
310 up_write(&journal->j_trans_barrier);
311 mlog(0, "No transactions for me to flush!\n");
315 jbd2_journal_lock_updates(journal->j_journal);
316 status = jbd2_journal_flush(journal->j_journal);
317 jbd2_journal_unlock_updates(journal->j_journal);
319 up_write(&journal->j_trans_barrier);
324 old_id = ocfs2_inc_trans_id(journal);
326 flushed = atomic_read(&journal->j_num_trans);
327 atomic_set(&journal->j_num_trans, 0);
328 up_write(&journal->j_trans_barrier);
330 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
331 journal->j_trans_id, flushed);
333 ocfs2_wake_downconvert_thread(osb);
334 wake_up(&journal->j_checkpointed);
340 /* pass it NULL and it will allocate a new handle object for you. If
341 * you pass it a handle however, it may still return error, in which
342 * case it has free'd the passed handle for you. */
343 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
345 journal_t *journal = osb->journal->j_journal;
348 BUG_ON(!osb || !osb->journal->j_journal);
350 if (ocfs2_is_hard_readonly(osb))
351 return ERR_PTR(-EROFS);
353 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
354 BUG_ON(max_buffs <= 0);
356 /* Nested transaction? Just return the handle... */
357 if (journal_current_handle())
358 return jbd2_journal_start(journal, max_buffs);
360 down_read(&osb->journal->j_trans_barrier);
362 handle = jbd2_journal_start(journal, max_buffs);
363 if (IS_ERR(handle)) {
364 up_read(&osb->journal->j_trans_barrier);
366 mlog_errno(PTR_ERR(handle));
368 if (is_journal_aborted(journal)) {
369 ocfs2_abort(osb->sb, "Detected aborted journal");
370 handle = ERR_PTR(-EROFS);
373 if (!ocfs2_mount_local(osb))
374 atomic_inc(&(osb->journal->j_num_trans));
380 int ocfs2_commit_trans(struct ocfs2_super *osb,
384 struct ocfs2_journal *journal = osb->journal;
388 nested = handle->h_ref > 1;
389 ret = jbd2_journal_stop(handle);
394 up_read(&journal->j_trans_barrier);
400 * 'nblocks' is what you want to add to the current
401 * transaction. extend_trans will either extend the current handle by
402 * nblocks, or commit it and start a new one with nblocks credits.
404 * This might call jbd2_journal_restart() which will commit dirty buffers
405 * and then restart the transaction. Before calling
406 * ocfs2_extend_trans(), any changed blocks should have been
407 * dirtied. After calling it, all blocks which need to be changed must
408 * go through another set of journal_access/journal_dirty calls.
410 * WARNING: This will not release any semaphores or disk locks taken
411 * during the transaction, so make sure they were taken *before*
412 * start_trans or we'll have ordering deadlocks.
414 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
415 * good because transaction ids haven't yet been recorded on the
416 * cluster locks associated with this handle.
418 int ocfs2_extend_trans(handle_t *handle, int nblocks)
427 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
429 #ifdef CONFIG_OCFS2_DEBUG_FS
432 status = jbd2_journal_extend(handle, nblocks);
441 "jbd2_journal_extend failed, trying "
442 "jbd2_journal_restart\n");
443 status = jbd2_journal_restart(handle, nblocks);
457 struct ocfs2_triggers {
458 struct jbd2_buffer_trigger_type ot_triggers;
462 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
464 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
467 static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
468 struct buffer_head *bh,
469 void *data, size_t size)
471 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
474 * We aren't guaranteed to have the superblock here, so we
475 * must unconditionally compute the ecc data.
476 * __ocfs2_journal_access() will only set the triggers if
477 * metaecc is enabled.
479 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
483 * Quota blocks have their own trigger because the struct ocfs2_block_check
484 * offset depends on the blocksize.
486 static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
487 struct buffer_head *bh,
488 void *data, size_t size)
490 struct ocfs2_disk_dqtrailer *dqt =
491 ocfs2_block_dqtrailer(size, data);
494 * We aren't guaranteed to have the superblock here, so we
495 * must unconditionally compute the ecc data.
496 * __ocfs2_journal_access() will only set the triggers if
497 * metaecc is enabled.
499 ocfs2_block_check_compute(data, size, &dqt->dq_check);
503 * Directory blocks also have their own trigger because the
504 * struct ocfs2_block_check offset depends on the blocksize.
506 static void ocfs2_db_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
507 struct buffer_head *bh,
508 void *data, size_t size)
510 struct ocfs2_dir_block_trailer *trailer =
511 ocfs2_dir_trailer_from_size(size, data);
514 * We aren't guaranteed to have the superblock here, so we
515 * must unconditionally compute the ecc data.
516 * __ocfs2_journal_access() will only set the triggers if
517 * metaecc is enabled.
519 ocfs2_block_check_compute(data, size, &trailer->db_check);
522 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
523 struct buffer_head *bh)
526 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
527 "bh->b_blocknr = %llu\n",
529 (unsigned long long)bh->b_blocknr);
531 /* We aren't guaranteed to have the superblock here - but if we
532 * don't, it'll just crash. */
533 ocfs2_error(bh->b_assoc_map->host->i_sb,
534 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
537 static struct ocfs2_triggers di_triggers = {
539 .t_commit = ocfs2_commit_trigger,
540 .t_abort = ocfs2_abort_trigger,
542 .ot_offset = offsetof(struct ocfs2_dinode, i_check),
545 static struct ocfs2_triggers eb_triggers = {
547 .t_commit = ocfs2_commit_trigger,
548 .t_abort = ocfs2_abort_trigger,
550 .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
553 static struct ocfs2_triggers gd_triggers = {
555 .t_commit = ocfs2_commit_trigger,
556 .t_abort = ocfs2_abort_trigger,
558 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
561 static struct ocfs2_triggers db_triggers = {
563 .t_commit = ocfs2_db_commit_trigger,
564 .t_abort = ocfs2_abort_trigger,
568 static struct ocfs2_triggers xb_triggers = {
570 .t_commit = ocfs2_commit_trigger,
571 .t_abort = ocfs2_abort_trigger,
573 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
576 static struct ocfs2_triggers dq_triggers = {
578 .t_commit = ocfs2_dq_commit_trigger,
579 .t_abort = ocfs2_abort_trigger,
583 static struct ocfs2_triggers dr_triggers = {
585 .t_commit = ocfs2_commit_trigger,
586 .t_abort = ocfs2_abort_trigger,
588 .ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check),
591 static struct ocfs2_triggers dl_triggers = {
593 .t_commit = ocfs2_commit_trigger,
594 .t_abort = ocfs2_abort_trigger,
596 .ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check),
599 static int __ocfs2_journal_access(handle_t *handle,
601 struct buffer_head *bh,
602 struct ocfs2_triggers *triggers,
611 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
612 (unsigned long long)bh->b_blocknr, type,
613 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
614 "OCFS2_JOURNAL_ACCESS_CREATE" :
615 "OCFS2_JOURNAL_ACCESS_WRITE",
618 /* we can safely remove this assertion after testing. */
619 if (!buffer_uptodate(bh)) {
620 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
621 mlog(ML_ERROR, "b_blocknr=%llu\n",
622 (unsigned long long)bh->b_blocknr);
626 /* Set the current transaction information on the inode so
627 * that the locking code knows whether it can drop it's locks
628 * on this inode or not. We're protected from the commit
629 * thread updating the current transaction id until
630 * ocfs2_commit_trans() because ocfs2_start_trans() took
631 * j_trans_barrier for us. */
632 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
634 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
636 case OCFS2_JOURNAL_ACCESS_CREATE:
637 case OCFS2_JOURNAL_ACCESS_WRITE:
638 status = jbd2_journal_get_write_access(handle, bh);
641 case OCFS2_JOURNAL_ACCESS_UNDO:
642 status = jbd2_journal_get_undo_access(handle, bh);
647 mlog(ML_ERROR, "Uknown access type!\n");
649 if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
650 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
651 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
654 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
661 int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
662 struct buffer_head *bh, int type)
664 return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
668 int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
669 struct buffer_head *bh, int type)
671 return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
675 int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
676 struct buffer_head *bh, int type)
678 return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
682 int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
683 struct buffer_head *bh, int type)
685 return __ocfs2_journal_access(handle, inode, bh, &db_triggers,
689 int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
690 struct buffer_head *bh, int type)
692 return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
696 int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
697 struct buffer_head *bh, int type)
699 return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
703 int ocfs2_journal_access_dr(handle_t *handle, struct inode *inode,
704 struct buffer_head *bh, int type)
706 return __ocfs2_journal_access(handle, inode, bh, &dr_triggers,
710 int ocfs2_journal_access_dl(handle_t *handle, struct inode *inode,
711 struct buffer_head *bh, int type)
713 return __ocfs2_journal_access(handle, inode, bh, &dl_triggers,
717 int ocfs2_journal_access(handle_t *handle, struct inode *inode,
718 struct buffer_head *bh, int type)
720 return __ocfs2_journal_access(handle, inode, bh, NULL, type);
723 int ocfs2_journal_dirty(handle_t *handle,
724 struct buffer_head *bh)
728 mlog_entry("(bh->b_blocknr=%llu)\n",
729 (unsigned long long)bh->b_blocknr);
731 status = jbd2_journal_dirty_metadata(handle, bh);
733 mlog(ML_ERROR, "Could not dirty metadata buffer. "
734 "(bh->b_blocknr=%llu)\n",
735 (unsigned long long)bh->b_blocknr);
741 #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
743 void ocfs2_set_journal_params(struct ocfs2_super *osb)
745 journal_t *journal = osb->journal->j_journal;
746 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
748 if (osb->osb_commit_interval)
749 commit_interval = osb->osb_commit_interval;
751 spin_lock(&journal->j_state_lock);
752 journal->j_commit_interval = commit_interval;
753 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
754 journal->j_flags |= JBD2_BARRIER;
756 journal->j_flags &= ~JBD2_BARRIER;
757 spin_unlock(&journal->j_state_lock);
760 int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
763 struct inode *inode = NULL; /* the journal inode */
764 journal_t *j_journal = NULL;
765 struct ocfs2_dinode *di = NULL;
766 struct buffer_head *bh = NULL;
767 struct ocfs2_super *osb;
774 osb = journal->j_osb;
776 /* already have the inode for our journal */
777 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
784 if (is_bad_inode(inode)) {
785 mlog(ML_ERROR, "access error (bad inode)\n");
792 SET_INODE_JOURNAL(inode);
793 OCFS2_I(inode)->ip_open_count++;
795 /* Skip recovery waits here - journal inode metadata never
796 * changes in a live cluster so it can be considered an
797 * exception to the rule. */
798 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
800 if (status != -ERESTARTSYS)
801 mlog(ML_ERROR, "Could not get lock on journal!\n");
806 di = (struct ocfs2_dinode *)bh->b_data;
808 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
809 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
815 mlog(0, "inode->i_size = %lld\n", inode->i_size);
816 mlog(0, "inode->i_blocks = %llu\n",
817 (unsigned long long)inode->i_blocks);
818 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
820 /* call the kernels journal init function now */
821 j_journal = jbd2_journal_init_inode(inode);
822 if (j_journal == NULL) {
823 mlog(ML_ERROR, "Linux journal layer error\n");
828 mlog(0, "Returned from jbd2_journal_init_inode\n");
829 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
831 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
832 OCFS2_JOURNAL_DIRTY_FL);
834 journal->j_journal = j_journal;
835 journal->j_inode = inode;
838 ocfs2_set_journal_params(osb);
840 journal->j_state = OCFS2_JOURNAL_LOADED;
846 ocfs2_inode_unlock(inode, 1);
849 OCFS2_I(inode)->ip_open_count--;
858 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
860 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
863 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
865 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
868 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
869 int dirty, int replayed)
873 struct ocfs2_journal *journal = osb->journal;
874 struct buffer_head *bh = journal->j_bh;
875 struct ocfs2_dinode *fe;
879 fe = (struct ocfs2_dinode *)bh->b_data;
881 /* The journal bh on the osb always comes from ocfs2_journal_init()
882 * and was validated there inside ocfs2_inode_lock_full(). It's a
883 * code bug if we mess it up. */
884 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
886 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
888 flags |= OCFS2_JOURNAL_DIRTY_FL;
890 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
891 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
894 ocfs2_bump_recovery_generation(fe);
896 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
897 status = ocfs2_write_block(osb, bh, journal->j_inode);
906 * If the journal has been kmalloc'd it needs to be freed after this
909 void ocfs2_journal_shutdown(struct ocfs2_super *osb)
911 struct ocfs2_journal *journal = NULL;
913 struct inode *inode = NULL;
914 int num_running_trans = 0;
920 journal = osb->journal;
924 inode = journal->j_inode;
926 if (journal->j_state != OCFS2_JOURNAL_LOADED)
929 /* need to inc inode use count - jbd2_journal_destroy will iput. */
933 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
934 if (num_running_trans > 0)
935 mlog(0, "Shutting down journal: must wait on %d "
936 "running transactions!\n",
939 /* Do a commit_cache here. It will flush our journal, *and*
940 * release any locks that are still held.
941 * set the SHUTDOWN flag and release the trans lock.
942 * the commit thread will take the trans lock for us below. */
943 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
945 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
946 * drop the trans_lock (which we want to hold until we
947 * completely destroy the journal. */
948 if (osb->commit_task) {
949 /* Wait for the commit thread */
950 mlog(0, "Waiting for ocfs2commit to exit....\n");
951 kthread_stop(osb->commit_task);
952 osb->commit_task = NULL;
955 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
957 if (ocfs2_mount_local(osb)) {
958 jbd2_journal_lock_updates(journal->j_journal);
959 status = jbd2_journal_flush(journal->j_journal);
960 jbd2_journal_unlock_updates(journal->j_journal);
967 * Do not toggle if flush was unsuccessful otherwise
968 * will leave dirty metadata in a "clean" journal
970 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
975 /* Shutdown the kernel journal system */
976 jbd2_journal_destroy(journal->j_journal);
977 journal->j_journal = NULL;
979 OCFS2_I(inode)->ip_open_count--;
981 /* unlock our journal */
982 ocfs2_inode_unlock(inode, 1);
984 brelse(journal->j_bh);
985 journal->j_bh = NULL;
987 journal->j_state = OCFS2_JOURNAL_FREE;
989 // up_write(&journal->j_trans_barrier);
996 static void ocfs2_clear_journal_error(struct super_block *sb,
1002 olderr = jbd2_journal_errno(journal);
1004 mlog(ML_ERROR, "File system error %d recorded in "
1005 "journal %u.\n", olderr, slot);
1006 mlog(ML_ERROR, "File system on device %s needs checking.\n",
1009 jbd2_journal_ack_err(journal);
1010 jbd2_journal_clear_err(journal);
1014 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
1017 struct ocfs2_super *osb;
1023 osb = journal->j_osb;
1025 status = jbd2_journal_load(journal->j_journal);
1027 mlog(ML_ERROR, "Failed to load journal!\n");
1031 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
1033 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
1039 /* Launch the commit thread */
1041 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
1043 if (IS_ERR(osb->commit_task)) {
1044 status = PTR_ERR(osb->commit_task);
1045 osb->commit_task = NULL;
1046 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
1047 "error=%d", status);
1051 osb->commit_task = NULL;
1059 /* 'full' flag tells us whether we clear out all blocks or if we just
1060 * mark the journal clean */
1061 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
1069 status = jbd2_journal_wipe(journal->j_journal, full);
1075 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
1084 static int ocfs2_recovery_completed(struct ocfs2_super *osb)
1087 struct ocfs2_recovery_map *rm = osb->recovery_map;
1089 spin_lock(&osb->osb_lock);
1090 empty = (rm->rm_used == 0);
1091 spin_unlock(&osb->osb_lock);
1096 void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1098 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1102 * JBD Might read a cached version of another nodes journal file. We
1103 * don't want this as this file changes often and we get no
1104 * notification on those changes. The only way to be sure that we've
1105 * got the most up to date version of those blocks then is to force
1106 * read them off disk. Just searching through the buffer cache won't
1107 * work as there may be pages backing this file which are still marked
1108 * up to date. We know things can't change on this file underneath us
1109 * as we have the lock by now :)
1111 static int ocfs2_force_read_journal(struct inode *inode)
1115 u64 v_blkno, p_blkno, p_blocks, num_blocks;
1116 #define CONCURRENT_JOURNAL_FILL 32ULL
1117 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
1121 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
1123 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
1125 while (v_blkno < num_blocks) {
1126 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
1127 &p_blkno, &p_blocks, NULL);
1133 if (p_blocks > CONCURRENT_JOURNAL_FILL)
1134 p_blocks = CONCURRENT_JOURNAL_FILL;
1136 /* We are reading journal data which should not
1137 * be put in the uptodate cache */
1138 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
1139 p_blkno, p_blocks, bhs);
1145 for(i = 0; i < p_blocks; i++) {
1150 v_blkno += p_blocks;
1154 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
1160 struct ocfs2_la_recovery_item {
1161 struct list_head lri_list;
1163 struct ocfs2_dinode *lri_la_dinode;
1164 struct ocfs2_dinode *lri_tl_dinode;
1165 struct ocfs2_quota_recovery *lri_qrec;
1168 /* Does the second half of the recovery process. By this point, the
1169 * node is marked clean and can actually be considered recovered,
1170 * hence it's no longer in the recovery map, but there's still some
1171 * cleanup we can do which shouldn't happen within the recovery thread
1172 * as locking in that context becomes very difficult if we are to take
1173 * recovering nodes into account.
1175 * NOTE: This function can and will sleep on recovery of other nodes
1176 * during cluster locking, just like any other ocfs2 process.
1178 void ocfs2_complete_recovery(struct work_struct *work)
1181 struct ocfs2_journal *journal =
1182 container_of(work, struct ocfs2_journal, j_recovery_work);
1183 struct ocfs2_super *osb = journal->j_osb;
1184 struct ocfs2_dinode *la_dinode, *tl_dinode;
1185 struct ocfs2_la_recovery_item *item, *n;
1186 struct ocfs2_quota_recovery *qrec;
1187 LIST_HEAD(tmp_la_list);
1191 mlog(0, "completing recovery from keventd\n");
1193 spin_lock(&journal->j_lock);
1194 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1195 spin_unlock(&journal->j_lock);
1197 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
1198 list_del_init(&item->lri_list);
1200 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1202 ocfs2_wait_on_quotas(osb);
1204 la_dinode = item->lri_la_dinode;
1206 mlog(0, "Clean up local alloc %llu\n",
1207 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
1209 ret = ocfs2_complete_local_alloc_recovery(osb,
1217 tl_dinode = item->lri_tl_dinode;
1219 mlog(0, "Clean up truncate log %llu\n",
1220 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
1222 ret = ocfs2_complete_truncate_log_recovery(osb,
1230 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1234 qrec = item->lri_qrec;
1236 mlog(0, "Recovering quota files");
1237 ret = ocfs2_finish_quota_recovery(osb, qrec,
1241 /* Recovery info is already freed now */
1247 mlog(0, "Recovery completion\n");
1251 /* NOTE: This function always eats your references to la_dinode and
1252 * tl_dinode, either manually on error, or by passing them to
1253 * ocfs2_complete_recovery */
1254 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1256 struct ocfs2_dinode *la_dinode,
1257 struct ocfs2_dinode *tl_dinode,
1258 struct ocfs2_quota_recovery *qrec)
1260 struct ocfs2_la_recovery_item *item;
1262 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
1264 /* Though we wish to avoid it, we are in fact safe in
1265 * skipping local alloc cleanup as fsck.ocfs2 is more
1266 * than capable of reclaiming unused space. */
1274 ocfs2_free_quota_recovery(qrec);
1276 mlog_errno(-ENOMEM);
1280 INIT_LIST_HEAD(&item->lri_list);
1281 item->lri_la_dinode = la_dinode;
1282 item->lri_slot = slot_num;
1283 item->lri_tl_dinode = tl_dinode;
1284 item->lri_qrec = qrec;
1286 spin_lock(&journal->j_lock);
1287 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1288 queue_work(ocfs2_wq, &journal->j_recovery_work);
1289 spin_unlock(&journal->j_lock);
1292 /* Called by the mount code to queue recovery the last part of
1293 * recovery for it's own and offline slot(s). */
1294 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1296 struct ocfs2_journal *journal = osb->journal;
1298 /* No need to queue up our truncate_log as regular cleanup will catch
1300 ocfs2_queue_recovery_completion(journal, osb->slot_num,
1301 osb->local_alloc_copy, NULL, NULL);
1302 ocfs2_schedule_truncate_log_flush(osb, 0);
1304 osb->local_alloc_copy = NULL;
1307 /* queue to recover orphan slots for all offline slots */
1308 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1309 ocfs2_queue_replay_slots(osb);
1310 ocfs2_free_replay_slots(osb);
1313 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1315 if (osb->quota_rec) {
1316 ocfs2_queue_recovery_completion(osb->journal,
1321 osb->quota_rec = NULL;
1325 static int __ocfs2_recovery_thread(void *arg)
1327 int status, node_num, slot_num;
1328 struct ocfs2_super *osb = arg;
1329 struct ocfs2_recovery_map *rm = osb->recovery_map;
1330 int *rm_quota = NULL;
1331 int rm_quota_used = 0, i;
1332 struct ocfs2_quota_recovery *qrec;
1336 status = ocfs2_wait_on_mount(osb);
1341 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1347 status = ocfs2_super_lock(osb, 1);
1353 status = ocfs2_compute_replay_slots(osb);
1357 /* queue recovery for our own slot */
1358 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1361 spin_lock(&osb->osb_lock);
1362 while (rm->rm_used) {
1363 /* It's always safe to remove entry zero, as we won't
1364 * clear it until ocfs2_recover_node() has succeeded. */
1365 node_num = rm->rm_entries[0];
1366 spin_unlock(&osb->osb_lock);
1367 mlog(0, "checking node %d\n", node_num);
1368 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1369 if (slot_num == -ENOENT) {
1371 mlog(0, "no slot for this node, so no recovery"
1375 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1377 /* It is a bit subtle with quota recovery. We cannot do it
1378 * immediately because we have to obtain cluster locks from
1379 * quota files and we also don't want to just skip it because
1380 * then quota usage would be out of sync until some node takes
1381 * the slot. So we remember which nodes need quota recovery
1382 * and when everything else is done, we recover quotas. */
1383 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1384 if (i == rm_quota_used)
1385 rm_quota[rm_quota_used++] = slot_num;
1387 status = ocfs2_recover_node(osb, node_num, slot_num);
1390 ocfs2_recovery_map_clear(osb, node_num);
1393 "Error %d recovering node %d on device (%u,%u)!\n",
1395 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1396 mlog(ML_ERROR, "Volume requires unmount.\n");
1399 spin_lock(&osb->osb_lock);
1401 spin_unlock(&osb->osb_lock);
1402 mlog(0, "All nodes recovered\n");
1404 /* Refresh all journal recovery generations from disk */
1405 status = ocfs2_check_journals_nolocks(osb);
1406 status = (status == -EROFS) ? 0 : status;
1410 /* Now it is right time to recover quotas... We have to do this under
1411 * superblock lock so that noone can start using the slot (and crash)
1412 * before we recover it */
1413 for (i = 0; i < rm_quota_used; i++) {
1414 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1416 status = PTR_ERR(qrec);
1420 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1424 ocfs2_super_unlock(osb, 1);
1426 /* queue recovery for offline slots */
1427 ocfs2_queue_replay_slots(osb);
1430 mutex_lock(&osb->recovery_lock);
1431 if (!status && !ocfs2_recovery_completed(osb)) {
1432 mutex_unlock(&osb->recovery_lock);
1436 ocfs2_free_replay_slots(osb);
1437 osb->recovery_thread_task = NULL;
1438 mb(); /* sync with ocfs2_recovery_thread_running */
1439 wake_up(&osb->recovery_event);
1441 mutex_unlock(&osb->recovery_lock);
1447 /* no one is callint kthread_stop() for us so the kthread() api
1448 * requires that we call do_exit(). And it isn't exported, but
1449 * complete_and_exit() seems to be a minimal wrapper around it. */
1450 complete_and_exit(NULL, status);
1454 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1456 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1457 node_num, osb->node_num);
1459 mutex_lock(&osb->recovery_lock);
1460 if (osb->disable_recovery)
1463 /* People waiting on recovery will wait on
1464 * the recovery map to empty. */
1465 if (ocfs2_recovery_map_set(osb, node_num))
1466 mlog(0, "node %d already in recovery map.\n", node_num);
1468 mlog(0, "starting recovery thread...\n");
1470 if (osb->recovery_thread_task)
1473 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
1475 if (IS_ERR(osb->recovery_thread_task)) {
1476 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1477 osb->recovery_thread_task = NULL;
1481 mutex_unlock(&osb->recovery_lock);
1482 wake_up(&osb->recovery_event);
1487 static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1489 struct buffer_head **bh,
1490 struct inode **ret_inode)
1492 int status = -EACCES;
1493 struct inode *inode = NULL;
1495 BUG_ON(slot_num >= osb->max_slots);
1497 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1499 if (!inode || is_bad_inode(inode)) {
1503 SET_INODE_JOURNAL(inode);
1505 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
1515 if (status || !ret_inode)
1523 /* Does the actual journal replay and marks the journal inode as
1524 * clean. Will only replay if the journal inode is marked dirty. */
1525 static int ocfs2_replay_journal(struct ocfs2_super *osb,
1532 struct inode *inode = NULL;
1533 struct ocfs2_dinode *fe;
1534 journal_t *journal = NULL;
1535 struct buffer_head *bh = NULL;
1538 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1544 fe = (struct ocfs2_dinode *)bh->b_data;
1545 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1550 * As the fs recovery is asynchronous, there is a small chance that
1551 * another node mounted (and recovered) the slot before the recovery
1552 * thread could get the lock. To handle that, we dirty read the journal
1553 * inode for that slot to get the recovery generation. If it is
1554 * different than what we expected, the slot has been recovered.
1555 * If not, it needs recovery.
1557 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1558 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1559 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1560 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1565 /* Continue with recovery as the journal has not yet been recovered */
1567 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
1569 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
1570 if (status != -ERESTARTSYS)
1571 mlog(ML_ERROR, "Could not lock journal!\n");
1576 fe = (struct ocfs2_dinode *) bh->b_data;
1578 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1579 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1581 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1582 mlog(0, "No recovery required for node %d\n", node_num);
1583 /* Refresh recovery generation for the slot */
1584 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1588 /* we need to run complete recovery for offline orphan slots */
1589 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1591 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1593 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1595 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1597 status = ocfs2_force_read_journal(inode);
1603 mlog(0, "calling journal_init_inode\n");
1604 journal = jbd2_journal_init_inode(inode);
1605 if (journal == NULL) {
1606 mlog(ML_ERROR, "Linux journal layer error\n");
1611 status = jbd2_journal_load(journal);
1616 jbd2_journal_destroy(journal);
1620 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1622 /* wipe the journal */
1623 mlog(0, "flushing the journal.\n");
1624 jbd2_journal_lock_updates(journal);
1625 status = jbd2_journal_flush(journal);
1626 jbd2_journal_unlock_updates(journal);
1630 /* This will mark the node clean */
1631 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1632 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1633 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1635 /* Increment recovery generation to indicate successful recovery */
1636 ocfs2_bump_recovery_generation(fe);
1637 osb->slot_recovery_generations[slot_num] =
1638 ocfs2_get_recovery_generation(fe);
1640 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1641 status = ocfs2_write_block(osb, bh, inode);
1648 jbd2_journal_destroy(journal);
1651 /* drop the lock on this nodes journal */
1653 ocfs2_inode_unlock(inode, 1);
1665 * Do the most important parts of node recovery:
1666 * - Replay it's journal
1667 * - Stamp a clean local allocator file
1668 * - Stamp a clean truncate log
1669 * - Mark the node clean
1671 * If this function completes without error, a node in OCFS2 can be
1672 * said to have been safely recovered. As a result, failure during the
1673 * second part of a nodes recovery process (local alloc recovery) is
1674 * far less concerning.
1676 static int ocfs2_recover_node(struct ocfs2_super *osb,
1677 int node_num, int slot_num)
1680 struct ocfs2_dinode *la_copy = NULL;
1681 struct ocfs2_dinode *tl_copy = NULL;
1683 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1684 node_num, slot_num, osb->node_num);
1686 /* Should not ever be called to recover ourselves -- in that
1687 * case we should've called ocfs2_journal_load instead. */
1688 BUG_ON(osb->node_num == node_num);
1690 status = ocfs2_replay_journal(osb, node_num, slot_num);
1692 if (status == -EBUSY) {
1693 mlog(0, "Skipping recovery for slot %u (node %u) "
1694 "as another node has recovered it\n", slot_num,
1703 /* Stamp a clean local alloc file AFTER recovering the journal... */
1704 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1710 /* An error from begin_truncate_log_recovery is not
1711 * serious enough to warrant halting the rest of
1713 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1717 /* Likewise, this would be a strange but ultimately not so
1718 * harmful place to get an error... */
1719 status = ocfs2_clear_slot(osb, slot_num);
1723 /* This will kfree the memory pointed to by la_copy and tl_copy */
1724 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1734 /* Test node liveness by trylocking his journal. If we get the lock,
1735 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1736 * still alive (we couldn't get the lock) and < 0 on error. */
1737 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1741 struct inode *inode = NULL;
1743 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1745 if (inode == NULL) {
1746 mlog(ML_ERROR, "access error\n");
1750 if (is_bad_inode(inode)) {
1751 mlog(ML_ERROR, "access error (bad inode)\n");
1757 SET_INODE_JOURNAL(inode);
1759 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
1760 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1762 if (status != -EAGAIN)
1767 ocfs2_inode_unlock(inode, 1);
1775 /* Call this underneath ocfs2_super_lock. It also assumes that the
1776 * slot info struct has been updated from disk. */
1777 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1779 unsigned int node_num;
1782 struct buffer_head *bh = NULL;
1783 struct ocfs2_dinode *di;
1785 /* This is called with the super block cluster lock, so we
1786 * know that the slot map can't change underneath us. */
1788 for (i = 0; i < osb->max_slots; i++) {
1789 /* Read journal inode to get the recovery generation */
1790 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1795 di = (struct ocfs2_dinode *)bh->b_data;
1796 gen = ocfs2_get_recovery_generation(di);
1800 spin_lock(&osb->osb_lock);
1801 osb->slot_recovery_generations[i] = gen;
1803 mlog(0, "Slot %u recovery generation is %u\n", i,
1804 osb->slot_recovery_generations[i]);
1806 if (i == osb->slot_num) {
1807 spin_unlock(&osb->osb_lock);
1811 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1812 if (status == -ENOENT) {
1813 spin_unlock(&osb->osb_lock);
1817 if (__ocfs2_recovery_map_test(osb, node_num)) {
1818 spin_unlock(&osb->osb_lock);
1821 spin_unlock(&osb->osb_lock);
1823 /* Ok, we have a slot occupied by another node which
1824 * is not in the recovery map. We trylock his journal
1825 * file here to test if he's alive. */
1826 status = ocfs2_trylock_journal(osb, i);
1828 /* Since we're called from mount, we know that
1829 * the recovery thread can't race us on
1830 * setting / checking the recovery bits. */
1831 ocfs2_recovery_thread(osb, node_num);
1832 } else if ((status < 0) && (status != -EAGAIN)) {
1844 struct ocfs2_orphan_filldir_priv {
1846 struct ocfs2_super *osb;
1849 static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1850 loff_t pos, u64 ino, unsigned type)
1852 struct ocfs2_orphan_filldir_priv *p = priv;
1855 if (name_len == 1 && !strncmp(".", name, 1))
1857 if (name_len == 2 && !strncmp("..", name, 2))
1860 /* Skip bad inodes so that recovery can continue */
1861 iter = ocfs2_iget(p->osb, ino,
1862 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
1866 mlog(0, "queue orphan %llu\n",
1867 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1868 /* No locking is required for the next_orphan queue as there
1869 * is only ever a single process doing orphan recovery. */
1870 OCFS2_I(iter)->ip_next_orphan = p->head;
1876 static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1878 struct inode **head)
1881 struct inode *orphan_dir_inode = NULL;
1882 struct ocfs2_orphan_filldir_priv priv;
1888 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1889 ORPHAN_DIR_SYSTEM_INODE,
1891 if (!orphan_dir_inode) {
1897 mutex_lock(&orphan_dir_inode->i_mutex);
1898 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
1904 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1905 ocfs2_orphan_filldir);
1914 ocfs2_inode_unlock(orphan_dir_inode, 0);
1916 mutex_unlock(&orphan_dir_inode->i_mutex);
1917 iput(orphan_dir_inode);
1921 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1926 spin_lock(&osb->osb_lock);
1927 ret = !osb->osb_orphan_wipes[slot];
1928 spin_unlock(&osb->osb_lock);
1932 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1935 spin_lock(&osb->osb_lock);
1936 /* Mark ourselves such that new processes in delete_inode()
1937 * know to quit early. */
1938 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1939 while (osb->osb_orphan_wipes[slot]) {
1940 /* If any processes are already in the middle of an
1941 * orphan wipe on this dir, then we need to wait for
1943 spin_unlock(&osb->osb_lock);
1944 wait_event_interruptible(osb->osb_wipe_event,
1945 ocfs2_orphan_recovery_can_continue(osb, slot));
1946 spin_lock(&osb->osb_lock);
1948 spin_unlock(&osb->osb_lock);
1951 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1954 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1958 * Orphan recovery. Each mounted node has it's own orphan dir which we
1959 * must run during recovery. Our strategy here is to build a list of
1960 * the inodes in the orphan dir and iget/iput them. The VFS does
1961 * (most) of the rest of the work.
1963 * Orphan recovery can happen at any time, not just mount so we have a
1964 * couple of extra considerations.
1966 * - We grab as many inodes as we can under the orphan dir lock -
1967 * doing iget() outside the orphan dir risks getting a reference on
1969 * - We must be sure not to deadlock with other processes on the
1970 * system wanting to run delete_inode(). This can happen when they go
1971 * to lock the orphan dir and the orphan recovery process attempts to
1972 * iget() inside the orphan dir lock. This can be avoided by
1973 * advertising our state to ocfs2_delete_inode().
1975 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1979 struct inode *inode = NULL;
1981 struct ocfs2_inode_info *oi;
1983 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1985 ocfs2_mark_recovering_orphan_dir(osb, slot);
1986 ret = ocfs2_queue_orphans(osb, slot, &inode);
1987 ocfs2_clear_recovering_orphan_dir(osb, slot);
1989 /* Error here should be noted, but we want to continue with as
1990 * many queued inodes as we've got. */
1995 oi = OCFS2_I(inode);
1996 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
1998 iter = oi->ip_next_orphan;
2000 spin_lock(&oi->ip_lock);
2001 /* The remote delete code may have set these on the
2002 * assumption that the other node would wipe them
2003 * successfully. If they are still in the node's
2004 * orphan dir, we need to reset that state. */
2005 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
2007 /* Set the proper information to get us going into
2008 * ocfs2_delete_inode. */
2009 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2010 spin_unlock(&oi->ip_lock);
2020 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
2022 /* This check is good because ocfs2 will wait on our recovery
2023 * thread before changing it to something other than MOUNTED
2025 wait_event(osb->osb_mount_event,
2026 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
2027 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
2028 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
2030 /* If there's an error on mount, then we may never get to the
2031 * MOUNTED flag, but this is set right before
2032 * dismount_volume() so we can trust it. */
2033 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
2034 mlog(0, "mount error, exiting!\n");
2041 static int ocfs2_commit_thread(void *arg)
2044 struct ocfs2_super *osb = arg;
2045 struct ocfs2_journal *journal = osb->journal;
2047 /* we can trust j_num_trans here because _should_stop() is only set in
2048 * shutdown and nobody other than ourselves should be able to start
2049 * transactions. committing on shutdown might take a few iterations
2050 * as final transactions put deleted inodes on the list */
2051 while (!(kthread_should_stop() &&
2052 atomic_read(&journal->j_num_trans) == 0)) {
2054 wait_event_interruptible(osb->checkpoint_event,
2055 atomic_read(&journal->j_num_trans)
2056 || kthread_should_stop());
2058 status = ocfs2_commit_cache(osb);
2062 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
2064 "commit_thread: %u transactions pending on "
2066 atomic_read(&journal->j_num_trans));
2073 /* Reads all the journal inodes without taking any cluster locks. Used
2074 * for hard readonly access to determine whether any journal requires
2075 * recovery. Also used to refresh the recovery generation numbers after
2076 * a journal has been recovered by another node.
2078 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
2082 struct buffer_head *di_bh = NULL;
2083 struct ocfs2_dinode *di;
2084 int journal_dirty = 0;
2086 for(slot = 0; slot < osb->max_slots; slot++) {
2087 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
2093 di = (struct ocfs2_dinode *) di_bh->b_data;
2095 osb->slot_recovery_generations[slot] =
2096 ocfs2_get_recovery_generation(di);
2098 if (le32_to_cpu(di->id1.journal1.ij_flags) &
2099 OCFS2_JOURNAL_DIRTY_FL)