📄 journal.c
字号:
mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev)); OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); status = ocfs2_force_read_journal(inode); if (status < 0) { mlog_errno(status); goto done; } mlog(0, "calling journal_init_inode\n"); journal = journal_init_inode(inode); if (journal == NULL) { mlog(ML_ERROR, "Linux journal layer error\n"); status = -EIO; goto done; } status = journal_load(journal); if (status < 0) { mlog_errno(status); if (!igrab(inode)) BUG(); journal_destroy(journal); goto done; } ocfs2_clear_journal_error(osb->sb, journal, slot_num); /* wipe the journal */ mlog(0, "flushing the journal.\n"); journal_lock_updates(journal); status = journal_flush(journal); journal_unlock_updates(journal); if (status < 0) mlog_errno(status); /* This will mark the node clean */ flags = le32_to_cpu(fe->id1.journal1.ij_flags); flags &= ~OCFS2_JOURNAL_DIRTY_FL; fe->id1.journal1.ij_flags = cpu_to_le32(flags); /* Increment recovery generation to indicate successful recovery */ ocfs2_bump_recovery_generation(fe); osb->slot_recovery_generations[slot_num] = ocfs2_get_recovery_generation(fe); status = ocfs2_write_block(osb, bh, inode); if (status < 0) mlog_errno(status); if (!igrab(inode)) BUG(); journal_destroy(journal);done: /* drop the lock on this nodes journal */ if (got_lock) ocfs2_inode_unlock(inode, 1); if (inode) iput(inode); if (bh) brelse(bh); mlog_exit(status); return status;}/* * Do the most important parts of node recovery: * - Replay it's journal * - Stamp a clean local allocator file * - Stamp a clean truncate log * - Mark the node clean * * If this function completes without error, a node in OCFS2 can be * said to have been safely recovered. As a result, failure during the * second part of a nodes recovery process (local alloc recovery) is * far less concerning. */static int ocfs2_recover_node(struct ocfs2_super *osb, int node_num){ int status = 0; int slot_num; struct ocfs2_slot_info *si = osb->slot_info; struct ocfs2_dinode *la_copy = NULL; struct ocfs2_dinode *tl_copy = NULL; mlog_entry("(node_num=%d, osb->node_num = %d)\n", node_num, osb->node_num); mlog(0, "checking node %d\n", node_num); /* Should not ever be called to recover ourselves -- in that * case we should've called ocfs2_journal_load instead. */ BUG_ON(osb->node_num == node_num); slot_num = ocfs2_node_num_to_slot(si, node_num); if (slot_num == OCFS2_INVALID_SLOT) { status = 0; mlog(0, "no slot for this node, so no recovery required.\n"); goto done; } mlog(0, "node %d was using slot %d\n", node_num, slot_num); status = ocfs2_replay_journal(osb, node_num, slot_num); if (status < 0) { if (status == -EBUSY) { mlog(0, "Skipping recovery for slot %u (node %u) " "as another node has recovered it\n", slot_num, node_num); status = 0; goto done; } mlog_errno(status); goto done; } /* Stamp a clean local alloc file AFTER recovering the journal... */ status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy); if (status < 0) { mlog_errno(status); goto done; } /* An error from begin_truncate_log_recovery is not * serious enough to warrant halting the rest of * recovery. */ status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy); if (status < 0) mlog_errno(status); /* Likewise, this would be a strange but ultimately not so * harmful place to get an error... */ ocfs2_clear_slot(si, slot_num); status = ocfs2_update_disk_slots(osb, si); if (status < 0) mlog_errno(status); /* This will kfree the memory pointed to by la_copy and tl_copy */ ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy, tl_copy); status = 0;done: mlog_exit(status); return status;}/* Test node liveness by trylocking his journal. If we get the lock, * we drop it here. Return 0 if we got the lock, -EAGAIN if node is * still alive (we couldn't get the lock) and < 0 on error. */static int ocfs2_trylock_journal(struct ocfs2_super *osb, int slot_num){ int status, flags; struct inode *inode = NULL; inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, slot_num); if (inode == NULL) { mlog(ML_ERROR, "access error\n"); status = -EACCES; goto bail; } if (is_bad_inode(inode)) { mlog(ML_ERROR, "access error (bad inode)\n"); iput(inode); inode = NULL; status = -EACCES; goto bail; } SET_INODE_JOURNAL(inode); flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE; status = ocfs2_inode_lock_full(inode, NULL, 1, flags); if (status < 0) { if (status != -EAGAIN) mlog_errno(status); goto bail; } ocfs2_inode_unlock(inode, 1);bail: if (inode) iput(inode); return status;}/* Call this underneath ocfs2_super_lock. It also assumes that the * slot info struct has been updated from disk. */int ocfs2_mark_dead_nodes(struct ocfs2_super *osb){ int status, i, node_num; struct ocfs2_slot_info *si = osb->slot_info; struct buffer_head *bh = NULL; struct ocfs2_dinode *di; /* This is called with the super block cluster lock, so we * know that the slot map can't change underneath us. */ spin_lock(&si->si_lock); for(i = 0; i < si->si_num_slots; i++) { /* Read journal inode to get the recovery generation */ status = ocfs2_read_journal_inode(osb, i, &bh, NULL); if (status) { mlog_errno(status); goto bail; } di = (struct ocfs2_dinode *)bh->b_data; osb->slot_recovery_generations[i] = ocfs2_get_recovery_generation(di); brelse(bh); bh = NULL; mlog(0, "Slot %u recovery generation is %u\n", i, osb->slot_recovery_generations[i]); if (i == osb->slot_num) continue; if (ocfs2_is_empty_slot(si, i)) continue; node_num = si->si_global_node_nums[i]; if (ocfs2_node_map_test_bit(osb, &osb->recovery_map, node_num)) continue; spin_unlock(&si->si_lock); /* Ok, we have a slot occupied by another node which * is not in the recovery map. We trylock his journal * file here to test if he's alive. */ status = ocfs2_trylock_journal(osb, i); if (!status) { /* Since we're called from mount, we know that * the recovery thread can't race us on * setting / checking the recovery bits. */ ocfs2_recovery_thread(osb, node_num); } else if ((status < 0) && (status != -EAGAIN)) { mlog_errno(status); goto bail; } spin_lock(&si->si_lock); } spin_unlock(&si->si_lock); status = 0;bail: mlog_exit(status); return status;}struct ocfs2_orphan_filldir_priv { struct inode *head; struct ocfs2_super *osb;};static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len, loff_t pos,#ifdef FILLDIR_T_WITH_INO_T ino_t ino,#else u64 ino,#endif unsigned type){ struct ocfs2_orphan_filldir_priv *p = priv; struct inode *iter; if (name_len == 1 && !strncmp(".", name, 1)) return 0; if (name_len == 2 && !strncmp("..", name, 2)) return 0; /* Skip bad inodes so that recovery can continue */ iter = ocfs2_iget(p->osb, ino, OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0); if (IS_ERR(iter)) return 0; mlog(0, "queue orphan %llu\n", (unsigned long long)OCFS2_I(iter)->ip_blkno); /* No locking is required for the next_orphan queue as there * is only ever a single process doing orphan recovery. */ OCFS2_I(iter)->ip_next_orphan = p->head; p->head = iter; return 0;}static int ocfs2_queue_orphans(struct ocfs2_super *osb, int slot, struct inode **head){ int status; struct inode *orphan_dir_inode = NULL; struct ocfs2_orphan_filldir_priv priv; loff_t pos = 0; priv.osb = osb; priv.head = *head; orphan_dir_inode = ocfs2_get_system_file_inode(osb, ORPHAN_DIR_SYSTEM_INODE, slot); if (!orphan_dir_inode) { status = -ENOENT; mlog_errno(status); return status; } mutex_lock(&orphan_dir_inode->i_mutex); status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0); if (status < 0) { mlog_errno(status); goto out; } status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv, ocfs2_orphan_filldir); if (status) { mlog_errno(status); goto out_cluster; } *head = priv.head;out_cluster: ocfs2_inode_unlock(orphan_dir_inode, 0);out: mutex_unlock(&orphan_dir_inode->i_mutex); iput(orphan_dir_inode); return status;}static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb, int slot){ int ret; spin_lock(&osb->osb_lock); ret = !osb->osb_orphan_wipes[slot]; spin_unlock(&osb->osb_lock); return ret;}static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb, int slot){ spin_lock(&osb->osb_lock); /* Mark ourselves such that new processes in delete_inode() * know to quit early. */ ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot); while (osb->osb_orphan_wipes[slot]) { /* If any processes are already in the middle of an * orphan wipe on this dir, then we need to wait for * them. */ spin_unlock(&osb->osb_lock); wait_event_interruptible(osb->osb_wipe_event, ocfs2_orphan_recovery_can_continue(osb, slot)); spin_lock(&osb->osb_lock); } spin_unlock(&osb->osb_lock);}static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb, int slot){ ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);}/* * Orphan recovery. Each mounted node has it's own orphan dir which we * must run during recovery. Our strategy here is to build a list of * the inodes in the orphan dir and iget/iput them. The VFS does * (most) of the rest of the work. * * Orphan recovery can happen at any time, not just mount so we have a * couple of extra considerations. * * - We grab as many inodes as we can under the orphan dir lock - * doing iget() outside the orphan dir risks getting a reference on * an invalid inode. * - We must be sure not to deadlock with other processes on the * system wanting to run delete_inode(). This can happen when they go * to lock the orphan dir and the orphan recovery process attempts to * iget() inside the orphan dir lock. This can be avoided by * advertising our state to ocfs2_delete_inode(). */static int ocfs2_recover_orphans(struct ocfs2_super *osb, int slot){ int ret = 0; struct inode *inode = NULL; struct inode *iter; struct ocfs2_inode_info *oi; mlog(0, "Recover inodes from orphan dir in slot %d\n", slot); ocfs2_mark_recovering_orphan_dir(osb, slot); ret = ocfs2_queue_orphans(osb, slot, &inode); ocfs2_clear_recovering_orphan_dir(osb, slot); /* Error here should be noted, but we want to continue with as * many queued inodes as we've got. */ if (ret) mlog_errno(ret); while (inode) { oi = OCFS2_I(inode); mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno); iter = oi->ip_next_orphan; spin_lock(&oi->ip_lock); /* The remote delete code may have set these on the * assumption that the other node would wipe them * successfully. If they are still in the node's * orphan dir, we need to reset that state. */ oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE); /* Set the proper information to get us going into * ocfs2_delete_inode. */ oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED; spin_unlock(&oi->ip_lock); iput(inode); inode = iter; } return ret;}static int ocfs2_wait_on_mount(struct ocfs2_super *osb){ /* This check is good because ocfs2 will wait on our recovery * thread before changing it to something other than MOUNTED * or DISABLED. */ wait_event(osb->osb_mount_event, atomic_read(&osb->vol_state) == VOLUME_MOUNTED || atomic_read(&osb->vol_state) == VOLUME_DISABLED); /* If there's an error on mount, then we may never get to the * MOUNTED flag, but this is set right before * dismount_volume() so we can trust it. */ if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) { mlog(0, "mount error, exiting!\n"); return -EBUSY; } return 0;}static int ocfs2_commit_thread(void *arg){ int status; struct ocfs2_super *osb = arg; struct ocfs2_journal *journal = osb->journal; /* we can trust j_num_trans here because _should_stop() is only set in * shutdown and nobody other than ourselves should be able to start * transactions. committing on shutdown might take a few iterations * as final transactions put deleted inodes on the list */ while (!(kthread_should_stop() && atomic_read(&journal->j_num_trans) == 0)) { wait_event_interruptible(osb->checkpoint_event, atomic_read(&journal->j_num_trans) || kthread_should_stop()); status = ocfs2_commit_cache(osb); if (status < 0) mlog_errno(status); if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){ mlog(ML_KTHREAD, "commit_thread: %u transactions pending on " "shutdown\n", atomic_read(&journal->j_num_trans)); } } return 0;}/* Reads all the journal inodes without taking any cluster locks. Used * for hard readonly access to determine whether any journal requires * recovery. Also used to refresh the recovery generation numbers after * a journal has been recovered by another node. */int ocfs2_check_journals_nolocks(struct ocfs2_super *osb){ int ret = 0; unsigned int slot; struct buffer_head *di_bh = NULL; struct ocfs2_dinode *di; int journal_dirty = 0; for(slot = 0; slot < osb->max_slots; slot++) { ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL); if (ret) { mlog_errno(ret); goto out; } di = (struct ocfs2_dinode *) di_bh->b_data; osb->slot_recovery_generations[slot] = ocfs2_get_recovery_generation(di); if (le32_to_cpu(di->id1.journal1.ij_flags) & OCFS2_JOURNAL_DIRTY_FL) journal_dirty = 1; brelse(di_bh); di_bh = NULL; }out: if (journal_dirty) ret = -EROFS; return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -