1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Defines functions of journalling api
7 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 #include <linux/highmem.h>
14 #include <linux/kthread.h>
15 #include <linux/time.h>
16 #include <linux/random.h>
17 #include <linux/delay.h>
18 #include <linux/writeback.h>
20 #include <cluster/masklog.h>
25 #include "blockcheck.h"
28 #include "extent_map.h"
29 #include "heartbeat.h"
32 #include "localalloc.h"
41 #include "buffer_head_io.h"
42 #include "ocfs2_trace.h"
44 DEFINE_SPINLOCK(trans_inc_lock);
46 #define ORPHAN_SCAN_SCHEDULE_TIMEOUT 300000
48 static int ocfs2_force_read_journal(struct inode *inode);
49 static int ocfs2_recover_node(struct ocfs2_super *osb,
50 int node_num, int slot_num);
51 static int __ocfs2_recovery_thread(void *arg);
52 static int ocfs2_commit_cache(struct ocfs2_super *osb);
53 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
54 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
55 int dirty, int replayed);
56 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
58 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
60 enum ocfs2_orphan_reco_type orphan_reco_type);
61 static int ocfs2_commit_thread(void *arg);
62 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
64 struct ocfs2_dinode *la_dinode,
65 struct ocfs2_dinode *tl_dinode,
66 struct ocfs2_quota_recovery *qrec,
67 enum ocfs2_orphan_reco_type orphan_reco_type);
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);
80 * This replay_map is to track online/offline slots, so we could recover
81 * offline slots during recovery and mount
84 enum ocfs2_replay_state {
85 REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */
86 REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */
87 REPLAY_DONE /* Replay was already queued */
90 struct ocfs2_replay_map {
91 unsigned int rm_slots;
92 enum ocfs2_replay_state rm_state;
93 unsigned char rm_replay_slots[];
96 static void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
101 /* If we've already queued the replay, we don't have any more to do */
102 if (osb->replay_map->rm_state == REPLAY_DONE)
105 osb->replay_map->rm_state = state;
108 int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
110 struct ocfs2_replay_map *replay_map;
113 /* If replay map is already set, we don't do it again */
117 replay_map = kzalloc(sizeof(struct ocfs2_replay_map) +
118 (osb->max_slots * sizeof(char)), GFP_KERNEL);
125 spin_lock(&osb->osb_lock);
127 replay_map->rm_slots = osb->max_slots;
128 replay_map->rm_state = REPLAY_UNNEEDED;
130 /* set rm_replay_slots for offline slot(s) */
131 for (i = 0; i < replay_map->rm_slots; i++) {
132 if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
133 replay_map->rm_replay_slots[i] = 1;
136 osb->replay_map = replay_map;
137 spin_unlock(&osb->osb_lock);
141 static void ocfs2_queue_replay_slots(struct ocfs2_super *osb,
142 enum ocfs2_orphan_reco_type orphan_reco_type)
144 struct ocfs2_replay_map *replay_map = osb->replay_map;
150 if (replay_map->rm_state != REPLAY_NEEDED)
153 for (i = 0; i < replay_map->rm_slots; i++)
154 if (replay_map->rm_replay_slots[i])
155 ocfs2_queue_recovery_completion(osb->journal, i, NULL,
158 replay_map->rm_state = REPLAY_DONE;
161 void ocfs2_free_replay_slots(struct ocfs2_super *osb)
163 struct ocfs2_replay_map *replay_map = osb->replay_map;
165 if (!osb->replay_map)
169 osb->replay_map = NULL;
172 int ocfs2_recovery_init(struct ocfs2_super *osb)
174 struct ocfs2_recovery_map *rm;
176 mutex_init(&osb->recovery_lock);
177 osb->disable_recovery = 0;
178 osb->recovery_thread_task = NULL;
179 init_waitqueue_head(&osb->recovery_event);
181 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
182 osb->max_slots * sizeof(unsigned int),
189 rm->rm_entries = (unsigned int *)((char *)rm +
190 sizeof(struct ocfs2_recovery_map));
191 osb->recovery_map = rm;
196 /* we can't grab the goofy sem lock from inside wait_event, so we use
197 * memory barriers to make sure that we'll see the null task before
199 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
202 return osb->recovery_thread_task != NULL;
205 void ocfs2_recovery_exit(struct ocfs2_super *osb)
207 struct ocfs2_recovery_map *rm;
209 /* disable any new recovery threads and wait for any currently
210 * running ones to exit. Do this before setting the vol_state. */
211 mutex_lock(&osb->recovery_lock);
212 osb->disable_recovery = 1;
213 mutex_unlock(&osb->recovery_lock);
214 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
216 /* At this point, we know that no more recovery threads can be
217 * launched, so wait for any recovery completion work to
220 flush_workqueue(osb->ocfs2_wq);
223 * Now that recovery is shut down, and the osb is about to be
224 * freed, the osb_lock is not taken here.
226 rm = osb->recovery_map;
227 /* XXX: Should we bug if there are dirty entries? */
232 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
233 unsigned int node_num)
236 struct ocfs2_recovery_map *rm = osb->recovery_map;
238 assert_spin_locked(&osb->osb_lock);
240 for (i = 0; i < rm->rm_used; i++) {
241 if (rm->rm_entries[i] == node_num)
248 /* Behaves like test-and-set. Returns the previous value */
249 static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
250 unsigned int node_num)
252 struct ocfs2_recovery_map *rm = osb->recovery_map;
254 spin_lock(&osb->osb_lock);
255 if (__ocfs2_recovery_map_test(osb, node_num)) {
256 spin_unlock(&osb->osb_lock);
260 /* XXX: Can this be exploited? Not from o2dlm... */
261 BUG_ON(rm->rm_used >= osb->max_slots);
263 rm->rm_entries[rm->rm_used] = node_num;
265 spin_unlock(&osb->osb_lock);
270 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
271 unsigned int node_num)
274 struct ocfs2_recovery_map *rm = osb->recovery_map;
276 spin_lock(&osb->osb_lock);
278 for (i = 0; i < rm->rm_used; i++) {
279 if (rm->rm_entries[i] == node_num)
283 if (i < rm->rm_used) {
284 /* XXX: be careful with the pointer math */
285 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
286 (rm->rm_used - i - 1) * sizeof(unsigned int));
290 spin_unlock(&osb->osb_lock);
293 static int ocfs2_commit_cache(struct ocfs2_super *osb)
296 unsigned int flushed;
297 struct ocfs2_journal *journal = NULL;
299 journal = osb->journal;
301 /* Flush all pending commits and checkpoint the journal. */
302 down_write(&journal->j_trans_barrier);
304 flushed = atomic_read(&journal->j_num_trans);
305 trace_ocfs2_commit_cache_begin(flushed);
307 up_write(&journal->j_trans_barrier);
311 jbd2_journal_lock_updates(journal->j_journal);
312 status = jbd2_journal_flush(journal->j_journal, 0);
313 jbd2_journal_unlock_updates(journal->j_journal);
315 up_write(&journal->j_trans_barrier);
320 ocfs2_inc_trans_id(journal);
322 flushed = atomic_read(&journal->j_num_trans);
323 atomic_set(&journal->j_num_trans, 0);
324 up_write(&journal->j_trans_barrier);
326 trace_ocfs2_commit_cache_end(journal->j_trans_id, flushed);
328 ocfs2_wake_downconvert_thread(osb);
329 wake_up(&journal->j_checkpointed);
334 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
336 journal_t *journal = osb->journal->j_journal;
339 BUG_ON(!osb || !osb->journal->j_journal);
341 if (ocfs2_is_hard_readonly(osb))
342 return ERR_PTR(-EROFS);
344 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
345 BUG_ON(max_buffs <= 0);
347 /* Nested transaction? Just return the handle... */
348 if (journal_current_handle())
349 return jbd2_journal_start(journal, max_buffs);
351 sb_start_intwrite(osb->sb);
353 down_read(&osb->journal->j_trans_barrier);
355 handle = jbd2_journal_start(journal, max_buffs);
356 if (IS_ERR(handle)) {
357 up_read(&osb->journal->j_trans_barrier);
358 sb_end_intwrite(osb->sb);
360 mlog_errno(PTR_ERR(handle));
362 if (is_journal_aborted(journal)) {
363 ocfs2_abort(osb->sb, "Detected aborted journal\n");
364 handle = ERR_PTR(-EROFS);
367 if (!ocfs2_mount_local(osb))
368 atomic_inc(&(osb->journal->j_num_trans));
374 int ocfs2_commit_trans(struct ocfs2_super *osb,
378 struct ocfs2_journal *journal = osb->journal;
382 nested = handle->h_ref > 1;
383 ret = jbd2_journal_stop(handle);
388 up_read(&journal->j_trans_barrier);
389 sb_end_intwrite(osb->sb);
396 * 'nblocks' is what you want to add to the current transaction.
398 * This might call jbd2_journal_restart() which will commit dirty buffers
399 * and then restart the transaction. Before calling
400 * ocfs2_extend_trans(), any changed blocks should have been
401 * dirtied. After calling it, all blocks which need to be changed must
402 * go through another set of journal_access/journal_dirty calls.
404 * WARNING: This will not release any semaphores or disk locks taken
405 * during the transaction, so make sure they were taken *before*
406 * start_trans or we'll have ordering deadlocks.
408 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
409 * good because transaction ids haven't yet been recorded on the
410 * cluster locks associated with this handle.
412 int ocfs2_extend_trans(handle_t *handle, int nblocks)
414 int status, old_nblocks;
422 old_nblocks = jbd2_handle_buffer_credits(handle);
424 trace_ocfs2_extend_trans(old_nblocks, nblocks);
426 #ifdef CONFIG_OCFS2_DEBUG_FS
429 status = jbd2_journal_extend(handle, nblocks, 0);
437 trace_ocfs2_extend_trans_restart(old_nblocks + nblocks);
438 status = jbd2_journal_restart(handle,
439 old_nblocks + nblocks);
452 * If we have fewer than thresh credits, extend by OCFS2_MAX_TRANS_DATA.
453 * If that fails, restart the transaction & regain write access for the
454 * buffer head which is used for metadata modifications.
455 * Taken from Ext4: extend_or_restart_transaction()
457 int ocfs2_allocate_extend_trans(handle_t *handle, int thresh)
459 int status, old_nblks;
463 old_nblks = jbd2_handle_buffer_credits(handle);
464 trace_ocfs2_allocate_extend_trans(old_nblks, thresh);
466 if (old_nblks < thresh)
469 status = jbd2_journal_extend(handle, OCFS2_MAX_TRANS_DATA, 0);
476 status = jbd2_journal_restart(handle, OCFS2_MAX_TRANS_DATA);
486 struct ocfs2_triggers {
487 struct jbd2_buffer_trigger_type ot_triggers;
491 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
493 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
496 static void ocfs2_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
497 struct buffer_head *bh,
498 void *data, size_t size)
500 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
503 * We aren't guaranteed to have the superblock here, so we
504 * must unconditionally compute the ecc data.
505 * __ocfs2_journal_access() will only set the triggers if
506 * metaecc is enabled.
508 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
512 * Quota blocks have their own trigger because the struct ocfs2_block_check
513 * offset depends on the blocksize.
515 static void ocfs2_dq_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
516 struct buffer_head *bh,
517 void *data, size_t size)
519 struct ocfs2_disk_dqtrailer *dqt =
520 ocfs2_block_dqtrailer(size, data);
523 * We aren't guaranteed to have the superblock here, so we
524 * must unconditionally compute the ecc data.
525 * __ocfs2_journal_access() will only set the triggers if
526 * metaecc is enabled.
528 ocfs2_block_check_compute(data, size, &dqt->dq_check);
532 * Directory blocks also have their own trigger because the
533 * struct ocfs2_block_check offset depends on the blocksize.
535 static void ocfs2_db_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
536 struct buffer_head *bh,
537 void *data, size_t size)
539 struct ocfs2_dir_block_trailer *trailer =
540 ocfs2_dir_trailer_from_size(size, data);
543 * We aren't guaranteed to have the superblock here, so we
544 * must unconditionally compute the ecc data.
545 * __ocfs2_journal_access() will only set the triggers if
546 * metaecc is enabled.
548 ocfs2_block_check_compute(data, size, &trailer->db_check);
551 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
552 struct buffer_head *bh)
555 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
556 "bh->b_blocknr = %llu\n",
558 (unsigned long long)bh->b_blocknr);
560 ocfs2_error(bh->b_bdev->bd_super,
561 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
564 static struct ocfs2_triggers di_triggers = {
566 .t_frozen = ocfs2_frozen_trigger,
567 .t_abort = ocfs2_abort_trigger,
569 .ot_offset = offsetof(struct ocfs2_dinode, i_check),
572 static struct ocfs2_triggers eb_triggers = {
574 .t_frozen = ocfs2_frozen_trigger,
575 .t_abort = ocfs2_abort_trigger,
577 .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
580 static struct ocfs2_triggers rb_triggers = {
582 .t_frozen = ocfs2_frozen_trigger,
583 .t_abort = ocfs2_abort_trigger,
585 .ot_offset = offsetof(struct ocfs2_refcount_block, rf_check),
588 static struct ocfs2_triggers gd_triggers = {
590 .t_frozen = ocfs2_frozen_trigger,
591 .t_abort = ocfs2_abort_trigger,
593 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
596 static struct ocfs2_triggers db_triggers = {
598 .t_frozen = ocfs2_db_frozen_trigger,
599 .t_abort = ocfs2_abort_trigger,
603 static struct ocfs2_triggers xb_triggers = {
605 .t_frozen = ocfs2_frozen_trigger,
606 .t_abort = ocfs2_abort_trigger,
608 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
611 static struct ocfs2_triggers dq_triggers = {
613 .t_frozen = ocfs2_dq_frozen_trigger,
614 .t_abort = ocfs2_abort_trigger,
618 static struct ocfs2_triggers dr_triggers = {
620 .t_frozen = ocfs2_frozen_trigger,
621 .t_abort = ocfs2_abort_trigger,
623 .ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check),
626 static struct ocfs2_triggers dl_triggers = {
628 .t_frozen = ocfs2_frozen_trigger,
629 .t_abort = ocfs2_abort_trigger,
631 .ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check),
634 static int __ocfs2_journal_access(handle_t *handle,
635 struct ocfs2_caching_info *ci,
636 struct buffer_head *bh,
637 struct ocfs2_triggers *triggers,
641 struct ocfs2_super *osb =
642 OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
644 BUG_ON(!ci || !ci->ci_ops);
648 trace_ocfs2_journal_access(
649 (unsigned long long)ocfs2_metadata_cache_owner(ci),
650 (unsigned long long)bh->b_blocknr, type, bh->b_size);
652 /* we can safely remove this assertion after testing. */
653 if (!buffer_uptodate(bh)) {
654 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
655 mlog(ML_ERROR, "b_blocknr=%llu, b_state=0x%lx\n",
656 (unsigned long long)bh->b_blocknr, bh->b_state);
660 * A previous transaction with a couple of buffer heads fail
661 * to checkpoint, so all the bhs are marked as BH_Write_EIO.
662 * For current transaction, the bh is just among those error
663 * bhs which previous transaction handle. We can't just clear
664 * its BH_Write_EIO and reuse directly, since other bhs are
665 * not written to disk yet and that will cause metadata
666 * inconsistency. So we should set fs read-only to avoid
669 if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) {
671 return ocfs2_error(osb->sb, "A previous attempt to "
672 "write this buffer head failed\n");
677 /* Set the current transaction information on the ci so
678 * that the locking code knows whether it can drop it's locks
679 * on this ci or not. We're protected from the commit
680 * thread updating the current transaction id until
681 * ocfs2_commit_trans() because ocfs2_start_trans() took
682 * j_trans_barrier for us. */
683 ocfs2_set_ci_lock_trans(osb->journal, ci);
685 ocfs2_metadata_cache_io_lock(ci);
687 case OCFS2_JOURNAL_ACCESS_CREATE:
688 case OCFS2_JOURNAL_ACCESS_WRITE:
689 status = jbd2_journal_get_write_access(handle, bh);
692 case OCFS2_JOURNAL_ACCESS_UNDO:
693 status = jbd2_journal_get_undo_access(handle, bh);
698 mlog(ML_ERROR, "Unknown access type!\n");
700 if (!status && ocfs2_meta_ecc(osb) && triggers)
701 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
702 ocfs2_metadata_cache_io_unlock(ci);
705 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
711 int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
712 struct buffer_head *bh, int type)
714 return __ocfs2_journal_access(handle, ci, bh, &di_triggers, type);
717 int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
718 struct buffer_head *bh, int type)
720 return __ocfs2_journal_access(handle, ci, bh, &eb_triggers, type);
723 int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
724 struct buffer_head *bh, int type)
726 return __ocfs2_journal_access(handle, ci, bh, &rb_triggers,
730 int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
731 struct buffer_head *bh, int type)
733 return __ocfs2_journal_access(handle, ci, bh, &gd_triggers, type);
736 int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
737 struct buffer_head *bh, int type)
739 return __ocfs2_journal_access(handle, ci, bh, &db_triggers, type);
742 int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
743 struct buffer_head *bh, int type)
745 return __ocfs2_journal_access(handle, ci, bh, &xb_triggers, type);
748 int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
749 struct buffer_head *bh, int type)
751 return __ocfs2_journal_access(handle, ci, bh, &dq_triggers, type);
754 int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
755 struct buffer_head *bh, int type)
757 return __ocfs2_journal_access(handle, ci, bh, &dr_triggers, type);
760 int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
761 struct buffer_head *bh, int type)
763 return __ocfs2_journal_access(handle, ci, bh, &dl_triggers, type);
766 int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
767 struct buffer_head *bh, int type)
769 return __ocfs2_journal_access(handle, ci, bh, NULL, type);
772 void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh)
776 trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr);
778 status = jbd2_journal_dirty_metadata(handle, bh);
781 if (!is_handle_aborted(handle)) {
782 journal_t *journal = handle->h_transaction->t_journal;
783 struct super_block *sb = bh->b_bdev->bd_super;
785 mlog(ML_ERROR, "jbd2_journal_dirty_metadata failed. "
786 "Aborting transaction and journal.\n");
787 handle->h_err = status;
788 jbd2_journal_abort_handle(handle);
789 jbd2_journal_abort(journal, status);
790 ocfs2_abort(sb, "Journal already aborted.\n");
795 #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
797 void ocfs2_set_journal_params(struct ocfs2_super *osb)
799 journal_t *journal = osb->journal->j_journal;
800 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
802 if (osb->osb_commit_interval)
803 commit_interval = osb->osb_commit_interval;
805 write_lock(&journal->j_state_lock);
806 journal->j_commit_interval = commit_interval;
807 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
808 journal->j_flags |= JBD2_BARRIER;
810 journal->j_flags &= ~JBD2_BARRIER;
811 write_unlock(&journal->j_state_lock);
815 * alloc & initialize skeleton for journal structure.
816 * ocfs2_journal_init() will make fs have journal ability.
818 int ocfs2_journal_alloc(struct ocfs2_super *osb)
821 struct ocfs2_journal *journal;
823 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
825 mlog(ML_ERROR, "unable to alloc journal\n");
829 osb->journal = journal;
830 journal->j_osb = osb;
832 atomic_set(&journal->j_num_trans, 0);
833 init_rwsem(&journal->j_trans_barrier);
834 init_waitqueue_head(&journal->j_checkpointed);
835 spin_lock_init(&journal->j_lock);
836 journal->j_trans_id = 1UL;
837 INIT_LIST_HEAD(&journal->j_la_cleanups);
838 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
839 journal->j_state = OCFS2_JOURNAL_FREE;
845 static int ocfs2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
847 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
848 struct writeback_control wbc = {
849 .sync_mode = WB_SYNC_ALL,
850 .nr_to_write = mapping->nrpages * 2,
851 .range_start = jinode->i_dirty_start,
852 .range_end = jinode->i_dirty_end,
855 return filemap_fdatawrite_wbc(mapping, &wbc);
858 int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty)
861 struct inode *inode = NULL; /* the journal inode */
862 journal_t *j_journal = NULL;
863 struct ocfs2_journal *journal = osb->journal;
864 struct ocfs2_dinode *di = NULL;
865 struct buffer_head *bh = NULL;
869 /* already have the inode for our journal */
870 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
877 if (is_bad_inode(inode)) {
878 mlog(ML_ERROR, "access error (bad inode)\n");
885 SET_INODE_JOURNAL(inode);
886 OCFS2_I(inode)->ip_open_count++;
888 /* Skip recovery waits here - journal inode metadata never
889 * changes in a live cluster so it can be considered an
890 * exception to the rule. */
891 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
893 if (status != -ERESTARTSYS)
894 mlog(ML_ERROR, "Could not get lock on journal!\n");
899 di = (struct ocfs2_dinode *)bh->b_data;
901 if (i_size_read(inode) < OCFS2_MIN_JOURNAL_SIZE) {
902 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
908 trace_ocfs2_journal_init(i_size_read(inode),
909 (unsigned long long)inode->i_blocks,
910 OCFS2_I(inode)->ip_clusters);
912 /* call the kernels journal init function now */
913 j_journal = jbd2_journal_init_inode(inode);
914 if (j_journal == NULL) {
915 mlog(ML_ERROR, "Linux journal layer error\n");
920 trace_ocfs2_journal_init_maxlen(j_journal->j_total_len);
922 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
923 OCFS2_JOURNAL_DIRTY_FL);
925 journal->j_journal = j_journal;
926 journal->j_journal->j_submit_inode_data_buffers =
927 ocfs2_journal_submit_inode_data_buffers;
928 journal->j_journal->j_finish_inode_data_buffers =
929 jbd2_journal_finish_inode_data_buffers;
930 journal->j_inode = inode;
933 ocfs2_set_journal_params(osb);
935 journal->j_state = OCFS2_JOURNAL_LOADED;
941 ocfs2_inode_unlock(inode, 1);
944 OCFS2_I(inode)->ip_open_count--;
952 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
954 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
957 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
959 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
962 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
963 int dirty, int replayed)
967 struct ocfs2_journal *journal = osb->journal;
968 struct buffer_head *bh = journal->j_bh;
969 struct ocfs2_dinode *fe;
971 fe = (struct ocfs2_dinode *)bh->b_data;
973 /* The journal bh on the osb always comes from ocfs2_journal_init()
974 * and was validated there inside ocfs2_inode_lock_full(). It's a
975 * code bug if we mess it up. */
976 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
978 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
980 flags |= OCFS2_JOURNAL_DIRTY_FL;
982 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
983 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
986 ocfs2_bump_recovery_generation(fe);
988 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
989 status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode));
997 * If the journal has been kmalloc'd it needs to be freed after this
1000 void ocfs2_journal_shutdown(struct ocfs2_super *osb)
1002 struct ocfs2_journal *journal = NULL;
1004 struct inode *inode = NULL;
1005 int num_running_trans = 0;
1009 journal = osb->journal;
1013 inode = journal->j_inode;
1015 if (journal->j_state != OCFS2_JOURNAL_LOADED)
1018 /* need to inc inode use count - jbd2_journal_destroy will iput. */
1022 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
1023 trace_ocfs2_journal_shutdown(num_running_trans);
1025 /* Do a commit_cache here. It will flush our journal, *and*
1026 * release any locks that are still held.
1027 * set the SHUTDOWN flag and release the trans lock.
1028 * the commit thread will take the trans lock for us below. */
1029 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
1031 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
1032 * drop the trans_lock (which we want to hold until we
1033 * completely destroy the journal. */
1034 if (osb->commit_task) {
1035 /* Wait for the commit thread */
1036 trace_ocfs2_journal_shutdown_wait(osb->commit_task);
1037 kthread_stop(osb->commit_task);
1038 osb->commit_task = NULL;
1041 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
1043 if (ocfs2_mount_local(osb)) {
1044 jbd2_journal_lock_updates(journal->j_journal);
1045 status = jbd2_journal_flush(journal->j_journal, 0);
1046 jbd2_journal_unlock_updates(journal->j_journal);
1051 /* Shutdown the kernel journal system */
1052 if (!jbd2_journal_destroy(journal->j_journal) && !status) {
1054 * Do not toggle if flush was unsuccessful otherwise
1055 * will leave dirty metadata in a "clean" journal
1057 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
1061 journal->j_journal = NULL;
1063 OCFS2_I(inode)->ip_open_count--;
1065 /* unlock our journal */
1066 ocfs2_inode_unlock(inode, 1);
1068 brelse(journal->j_bh);
1069 journal->j_bh = NULL;
1071 journal->j_state = OCFS2_JOURNAL_FREE;
1076 osb->journal = NULL;
1079 static void ocfs2_clear_journal_error(struct super_block *sb,
1085 olderr = jbd2_journal_errno(journal);
1087 mlog(ML_ERROR, "File system error %d recorded in "
1088 "journal %u.\n", olderr, slot);
1089 mlog(ML_ERROR, "File system on device %s needs checking.\n",
1092 jbd2_journal_ack_err(journal);
1093 jbd2_journal_clear_err(journal);
1097 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
1100 struct ocfs2_super *osb;
1104 osb = journal->j_osb;
1106 status = jbd2_journal_load(journal->j_journal);
1108 mlog(ML_ERROR, "Failed to load journal!\n");
1112 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
1115 jbd2_journal_lock_updates(journal->j_journal);
1116 status = jbd2_journal_flush(journal->j_journal, 0);
1117 jbd2_journal_unlock_updates(journal->j_journal);
1122 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
1128 /* Launch the commit thread */
1130 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
1131 "ocfs2cmt-%s", osb->uuid_str);
1132 if (IS_ERR(osb->commit_task)) {
1133 status = PTR_ERR(osb->commit_task);
1134 osb->commit_task = NULL;
1135 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
1136 "error=%d", status);
1140 osb->commit_task = NULL;
1147 /* 'full' flag tells us whether we clear out all blocks or if we just
1148 * mark the journal clean */
1149 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
1155 status = jbd2_journal_wipe(journal->j_journal, full);
1161 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
1169 static int ocfs2_recovery_completed(struct ocfs2_super *osb)
1172 struct ocfs2_recovery_map *rm = osb->recovery_map;
1174 spin_lock(&osb->osb_lock);
1175 empty = (rm->rm_used == 0);
1176 spin_unlock(&osb->osb_lock);
1181 void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1183 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1187 * JBD Might read a cached version of another nodes journal file. We
1188 * don't want this as this file changes often and we get no
1189 * notification on those changes. The only way to be sure that we've
1190 * got the most up to date version of those blocks then is to force
1191 * read them off disk. Just searching through the buffer cache won't
1192 * work as there may be pages backing this file which are still marked
1193 * up to date. We know things can't change on this file underneath us
1194 * as we have the lock by now :)
1196 static int ocfs2_force_read_journal(struct inode *inode)
1200 u64 v_blkno, p_blkno, p_blocks, num_blocks;
1201 struct buffer_head *bh = NULL;
1202 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1204 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
1206 while (v_blkno < num_blocks) {
1207 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
1208 &p_blkno, &p_blocks, NULL);
1214 for (i = 0; i < p_blocks; i++, p_blkno++) {
1215 bh = __find_get_block(osb->sb->s_bdev, p_blkno,
1216 osb->sb->s_blocksize);
1217 /* block not cached. */
1223 /* We are reading journal data which should not
1224 * be put in the uptodate cache.
1226 status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh);
1236 v_blkno += p_blocks;
1243 struct ocfs2_la_recovery_item {
1244 struct list_head lri_list;
1246 struct ocfs2_dinode *lri_la_dinode;
1247 struct ocfs2_dinode *lri_tl_dinode;
1248 struct ocfs2_quota_recovery *lri_qrec;
1249 enum ocfs2_orphan_reco_type lri_orphan_reco_type;
1252 /* Does the second half of the recovery process. By this point, the
1253 * node is marked clean and can actually be considered recovered,
1254 * hence it's no longer in the recovery map, but there's still some
1255 * cleanup we can do which shouldn't happen within the recovery thread
1256 * as locking in that context becomes very difficult if we are to take
1257 * recovering nodes into account.
1259 * NOTE: This function can and will sleep on recovery of other nodes
1260 * during cluster locking, just like any other ocfs2 process.
1262 void ocfs2_complete_recovery(struct work_struct *work)
1265 struct ocfs2_journal *journal =
1266 container_of(work, struct ocfs2_journal, j_recovery_work);
1267 struct ocfs2_super *osb = journal->j_osb;
1268 struct ocfs2_dinode *la_dinode, *tl_dinode;
1269 struct ocfs2_la_recovery_item *item, *n;
1270 struct ocfs2_quota_recovery *qrec;
1271 enum ocfs2_orphan_reco_type orphan_reco_type;
1272 LIST_HEAD(tmp_la_list);
1274 trace_ocfs2_complete_recovery(
1275 (unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno);
1277 spin_lock(&journal->j_lock);
1278 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1279 spin_unlock(&journal->j_lock);
1281 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
1282 list_del_init(&item->lri_list);
1284 ocfs2_wait_on_quotas(osb);
1286 la_dinode = item->lri_la_dinode;
1287 tl_dinode = item->lri_tl_dinode;
1288 qrec = item->lri_qrec;
1289 orphan_reco_type = item->lri_orphan_reco_type;
1291 trace_ocfs2_complete_recovery_slot(item->lri_slot,
1292 la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0,
1293 tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0,
1297 ret = ocfs2_complete_local_alloc_recovery(osb,
1306 ret = ocfs2_complete_truncate_log_recovery(osb,
1314 ret = ocfs2_recover_orphans(osb, item->lri_slot,
1320 ret = ocfs2_finish_quota_recovery(osb, qrec,
1324 /* Recovery info is already freed now */
1330 trace_ocfs2_complete_recovery_end(ret);
1333 /* NOTE: This function always eats your references to la_dinode and
1334 * tl_dinode, either manually on error, or by passing them to
1335 * ocfs2_complete_recovery */
1336 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1338 struct ocfs2_dinode *la_dinode,
1339 struct ocfs2_dinode *tl_dinode,
1340 struct ocfs2_quota_recovery *qrec,
1341 enum ocfs2_orphan_reco_type orphan_reco_type)
1343 struct ocfs2_la_recovery_item *item;
1345 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
1347 /* Though we wish to avoid it, we are in fact safe in
1348 * skipping local alloc cleanup as fsck.ocfs2 is more
1349 * than capable of reclaiming unused space. */
1354 ocfs2_free_quota_recovery(qrec);
1356 mlog_errno(-ENOMEM);
1360 INIT_LIST_HEAD(&item->lri_list);
1361 item->lri_la_dinode = la_dinode;
1362 item->lri_slot = slot_num;
1363 item->lri_tl_dinode = tl_dinode;
1364 item->lri_qrec = qrec;
1365 item->lri_orphan_reco_type = orphan_reco_type;
1367 spin_lock(&journal->j_lock);
1368 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1369 queue_work(journal->j_osb->ocfs2_wq, &journal->j_recovery_work);
1370 spin_unlock(&journal->j_lock);
1373 /* Called by the mount code to queue recovery the last part of
1374 * recovery for it's own and offline slot(s). */
1375 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1377 struct ocfs2_journal *journal = osb->journal;
1379 if (ocfs2_is_hard_readonly(osb))
1382 /* No need to queue up our truncate_log as regular cleanup will catch
1384 ocfs2_queue_recovery_completion(journal, osb->slot_num,
1385 osb->local_alloc_copy, NULL, NULL,
1386 ORPHAN_NEED_TRUNCATE);
1387 ocfs2_schedule_truncate_log_flush(osb, 0);
1389 osb->local_alloc_copy = NULL;
1391 /* queue to recover orphan slots for all offline slots */
1392 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1393 ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
1394 ocfs2_free_replay_slots(osb);
1397 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1399 if (osb->quota_rec) {
1400 ocfs2_queue_recovery_completion(osb->journal,
1405 ORPHAN_NEED_TRUNCATE);
1406 osb->quota_rec = NULL;
1410 static int __ocfs2_recovery_thread(void *arg)
1412 int status, node_num, slot_num;
1413 struct ocfs2_super *osb = arg;
1414 struct ocfs2_recovery_map *rm = osb->recovery_map;
1415 int *rm_quota = NULL;
1416 int rm_quota_used = 0, i;
1417 struct ocfs2_quota_recovery *qrec;
1419 /* Whether the quota supported. */
1420 int quota_enabled = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
1421 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
1422 || OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
1423 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA);
1425 status = ocfs2_wait_on_mount(osb);
1430 if (quota_enabled) {
1431 rm_quota = kcalloc(osb->max_slots, sizeof(int), GFP_NOFS);
1438 status = ocfs2_super_lock(osb, 1);
1444 status = ocfs2_compute_replay_slots(osb);
1448 /* queue recovery for our own slot */
1449 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1450 NULL, NULL, ORPHAN_NO_NEED_TRUNCATE);
1452 spin_lock(&osb->osb_lock);
1453 while (rm->rm_used) {
1454 /* It's always safe to remove entry zero, as we won't
1455 * clear it until ocfs2_recover_node() has succeeded. */
1456 node_num = rm->rm_entries[0];
1457 spin_unlock(&osb->osb_lock);
1458 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1459 trace_ocfs2_recovery_thread_node(node_num, slot_num);
1460 if (slot_num == -ENOENT) {
1465 /* It is a bit subtle with quota recovery. We cannot do it
1466 * immediately because we have to obtain cluster locks from
1467 * quota files and we also don't want to just skip it because
1468 * then quota usage would be out of sync until some node takes
1469 * the slot. So we remember which nodes need quota recovery
1470 * and when everything else is done, we recover quotas. */
1471 if (quota_enabled) {
1472 for (i = 0; i < rm_quota_used
1473 && rm_quota[i] != slot_num; i++)
1476 if (i == rm_quota_used)
1477 rm_quota[rm_quota_used++] = slot_num;
1480 status = ocfs2_recover_node(osb, node_num, slot_num);
1483 ocfs2_recovery_map_clear(osb, node_num);
1486 "Error %d recovering node %d on device (%u,%u)!\n",
1488 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1489 mlog(ML_ERROR, "Volume requires unmount.\n");
1492 spin_lock(&osb->osb_lock);
1494 spin_unlock(&osb->osb_lock);
1495 trace_ocfs2_recovery_thread_end(status);
1497 /* Refresh all journal recovery generations from disk */
1498 status = ocfs2_check_journals_nolocks(osb);
1499 status = (status == -EROFS) ? 0 : status;
1503 /* Now it is right time to recover quotas... We have to do this under
1504 * superblock lock so that no one can start using the slot (and crash)
1505 * before we recover it */
1506 if (quota_enabled) {
1507 for (i = 0; i < rm_quota_used; i++) {
1508 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1510 status = PTR_ERR(qrec);
1514 ocfs2_queue_recovery_completion(osb->journal,
1517 ORPHAN_NEED_TRUNCATE);
1521 ocfs2_super_unlock(osb, 1);
1523 /* queue recovery for offline slots */
1524 ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
1527 mutex_lock(&osb->recovery_lock);
1528 if (!status && !ocfs2_recovery_completed(osb)) {
1529 mutex_unlock(&osb->recovery_lock);
1533 ocfs2_free_replay_slots(osb);
1534 osb->recovery_thread_task = NULL;
1535 mb(); /* sync with ocfs2_recovery_thread_running */
1536 wake_up(&osb->recovery_event);
1538 mutex_unlock(&osb->recovery_lock);
1546 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1548 mutex_lock(&osb->recovery_lock);
1550 trace_ocfs2_recovery_thread(node_num, osb->node_num,
1551 osb->disable_recovery, osb->recovery_thread_task,
1552 osb->disable_recovery ?
1553 -1 : ocfs2_recovery_map_set(osb, node_num));
1555 if (osb->disable_recovery)
1558 if (osb->recovery_thread_task)
1561 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
1562 "ocfs2rec-%s", osb->uuid_str);
1563 if (IS_ERR(osb->recovery_thread_task)) {
1564 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1565 osb->recovery_thread_task = NULL;
1569 mutex_unlock(&osb->recovery_lock);
1570 wake_up(&osb->recovery_event);
1573 static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1575 struct buffer_head **bh,
1576 struct inode **ret_inode)
1578 int status = -EACCES;
1579 struct inode *inode = NULL;
1581 BUG_ON(slot_num >= osb->max_slots);
1583 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1585 if (!inode || is_bad_inode(inode)) {
1589 SET_INODE_JOURNAL(inode);
1591 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
1601 if (status || !ret_inode)
1609 /* Does the actual journal replay and marks the journal inode as
1610 * clean. Will only replay if the journal inode is marked dirty. */
1611 static int ocfs2_replay_journal(struct ocfs2_super *osb,
1618 struct inode *inode = NULL;
1619 struct ocfs2_dinode *fe;
1620 journal_t *journal = NULL;
1621 struct buffer_head *bh = NULL;
1624 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1630 fe = (struct ocfs2_dinode *)bh->b_data;
1631 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1636 * As the fs recovery is asynchronous, there is a small chance that
1637 * another node mounted (and recovered) the slot before the recovery
1638 * thread could get the lock. To handle that, we dirty read the journal
1639 * inode for that slot to get the recovery generation. If it is
1640 * different than what we expected, the slot has been recovered.
1641 * If not, it needs recovery.
1643 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1644 trace_ocfs2_replay_journal_recovered(slot_num,
1645 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1646 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1651 /* Continue with recovery as the journal has not yet been recovered */
1653 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
1655 trace_ocfs2_replay_journal_lock_err(status);
1656 if (status != -ERESTARTSYS)
1657 mlog(ML_ERROR, "Could not lock journal!\n");
1662 fe = (struct ocfs2_dinode *) bh->b_data;
1664 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1665 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1667 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1668 trace_ocfs2_replay_journal_skip(node_num);
1669 /* Refresh recovery generation for the slot */
1670 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1674 /* we need to run complete recovery for offline orphan slots */
1675 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1677 printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\
1678 "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
1679 MINOR(osb->sb->s_dev));
1681 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1683 status = ocfs2_force_read_journal(inode);
1689 journal = jbd2_journal_init_inode(inode);
1690 if (journal == NULL) {
1691 mlog(ML_ERROR, "Linux journal layer error\n");
1696 status = jbd2_journal_load(journal);
1699 BUG_ON(!igrab(inode));
1700 jbd2_journal_destroy(journal);
1704 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1706 /* wipe the journal */
1707 jbd2_journal_lock_updates(journal);
1708 status = jbd2_journal_flush(journal, 0);
1709 jbd2_journal_unlock_updates(journal);
1713 /* This will mark the node clean */
1714 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1715 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1716 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1718 /* Increment recovery generation to indicate successful recovery */
1719 ocfs2_bump_recovery_generation(fe);
1720 osb->slot_recovery_generations[slot_num] =
1721 ocfs2_get_recovery_generation(fe);
1723 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1724 status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
1728 BUG_ON(!igrab(inode));
1730 jbd2_journal_destroy(journal);
1732 printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\
1733 "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
1734 MINOR(osb->sb->s_dev));
1736 /* drop the lock on this nodes journal */
1738 ocfs2_inode_unlock(inode, 1);
1747 * Do the most important parts of node recovery:
1748 * - Replay it's journal
1749 * - Stamp a clean local allocator file
1750 * - Stamp a clean truncate log
1751 * - Mark the node clean
1753 * If this function completes without error, a node in OCFS2 can be
1754 * said to have been safely recovered. As a result, failure during the
1755 * second part of a nodes recovery process (local alloc recovery) is
1756 * far less concerning.
1758 static int ocfs2_recover_node(struct ocfs2_super *osb,
1759 int node_num, int slot_num)
1762 struct ocfs2_dinode *la_copy = NULL;
1763 struct ocfs2_dinode *tl_copy = NULL;
1765 trace_ocfs2_recover_node(node_num, slot_num, osb->node_num);
1767 /* Should not ever be called to recover ourselves -- in that
1768 * case we should've called ocfs2_journal_load instead. */
1769 BUG_ON(osb->node_num == node_num);
1771 status = ocfs2_replay_journal(osb, node_num, slot_num);
1773 if (status == -EBUSY) {
1774 trace_ocfs2_recover_node_skip(slot_num, node_num);
1782 /* Stamp a clean local alloc file AFTER recovering the journal... */
1783 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1789 /* An error from begin_truncate_log_recovery is not
1790 * serious enough to warrant halting the rest of
1792 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1796 /* Likewise, this would be a strange but ultimately not so
1797 * harmful place to get an error... */
1798 status = ocfs2_clear_slot(osb, slot_num);
1802 /* This will kfree the memory pointed to by la_copy and tl_copy */
1803 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1804 tl_copy, NULL, ORPHAN_NEED_TRUNCATE);
1812 /* Test node liveness by trylocking his journal. If we get the lock,
1813 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1814 * still alive (we couldn't get the lock) and < 0 on error. */
1815 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1819 struct inode *inode = NULL;
1821 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1823 if (inode == NULL) {
1824 mlog(ML_ERROR, "access error\n");
1828 if (is_bad_inode(inode)) {
1829 mlog(ML_ERROR, "access error (bad inode)\n");
1835 SET_INODE_JOURNAL(inode);
1837 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
1838 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1840 if (status != -EAGAIN)
1845 ocfs2_inode_unlock(inode, 1);
1852 /* Call this underneath ocfs2_super_lock. It also assumes that the
1853 * slot info struct has been updated from disk. */
1854 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1856 unsigned int node_num;
1859 struct buffer_head *bh = NULL;
1860 struct ocfs2_dinode *di;
1862 /* This is called with the super block cluster lock, so we
1863 * know that the slot map can't change underneath us. */
1865 for (i = 0; i < osb->max_slots; i++) {
1866 /* Read journal inode to get the recovery generation */
1867 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1872 di = (struct ocfs2_dinode *)bh->b_data;
1873 gen = ocfs2_get_recovery_generation(di);
1877 spin_lock(&osb->osb_lock);
1878 osb->slot_recovery_generations[i] = gen;
1880 trace_ocfs2_mark_dead_nodes(i,
1881 osb->slot_recovery_generations[i]);
1883 if (i == osb->slot_num) {
1884 spin_unlock(&osb->osb_lock);
1888 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1889 if (status == -ENOENT) {
1890 spin_unlock(&osb->osb_lock);
1894 if (__ocfs2_recovery_map_test(osb, node_num)) {
1895 spin_unlock(&osb->osb_lock);
1898 spin_unlock(&osb->osb_lock);
1900 /* Ok, we have a slot occupied by another node which
1901 * is not in the recovery map. We trylock his journal
1902 * file here to test if he's alive. */
1903 status = ocfs2_trylock_journal(osb, i);
1905 /* Since we're called from mount, we know that
1906 * the recovery thread can't race us on
1907 * setting / checking the recovery bits. */
1908 ocfs2_recovery_thread(osb, node_num);
1909 } else if ((status < 0) && (status != -EAGAIN)) {
1921 * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some
1922 * randomness to the timeout to minimize multple nodes firing the timer at the
1925 static inline unsigned long ocfs2_orphan_scan_timeout(void)
1929 get_random_bytes(&time, sizeof(time));
1930 time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000);
1931 return msecs_to_jiffies(time);
1935 * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for
1936 * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This
1937 * is done to catch any orphans that are left over in orphan directories.
1939 * It scans all slots, even ones that are in use. It does so to handle the
1940 * case described below:
1942 * Node 1 has an inode it was using. The dentry went away due to memory
1943 * pressure. Node 1 closes the inode, but it's on the free list. The node
1944 * has the open lock.
1945 * Node 2 unlinks the inode. It grabs the dentry lock to notify others,
1946 * but node 1 has no dentry and doesn't get the message. It trylocks the
1947 * open lock, sees that another node has a PR, and does nothing.
1948 * Later node 2 runs its orphan dir. It igets the inode, trylocks the
1949 * open lock, sees the PR still, and does nothing.
1950 * Basically, we have to trigger an orphan iput on node 1. The only way
1951 * for this to happen is if node 1 runs node 2's orphan dir.
1953 * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT
1954 * seconds. It gets an EX lock on os_lockres and checks sequence number
1955 * stored in LVB. If the sequence number has changed, it means some other
1956 * node has done the scan. This node skips the scan and tracks the
1957 * sequence number. If the sequence number didn't change, it means a scan
1958 * hasn't happened. The node queues a scan and increments the
1959 * sequence number in the LVB.
1961 static void ocfs2_queue_orphan_scan(struct ocfs2_super *osb)
1963 struct ocfs2_orphan_scan *os;
1967 os = &osb->osb_orphan_scan;
1969 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
1972 trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno,
1973 atomic_read(&os->os_state));
1975 status = ocfs2_orphan_scan_lock(osb, &seqno);
1977 if (status != -EAGAIN)
1982 /* Do no queue the tasks if the volume is being umounted */
1983 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
1986 if (os->os_seqno != seqno) {
1987 os->os_seqno = seqno;
1991 for (i = 0; i < osb->max_slots; i++)
1992 ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL,
1993 NULL, ORPHAN_NO_NEED_TRUNCATE);
1995 * We queued a recovery on orphan slots, increment the sequence
1996 * number and update LVB so other node will skip the scan for a while
2000 os->os_scantime = ktime_get_seconds();
2002 ocfs2_orphan_scan_unlock(osb, seqno);
2004 trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno,
2005 atomic_read(&os->os_state));
2009 /* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */
2010 static void ocfs2_orphan_scan_work(struct work_struct *work)
2012 struct ocfs2_orphan_scan *os;
2013 struct ocfs2_super *osb;
2015 os = container_of(work, struct ocfs2_orphan_scan,
2016 os_orphan_scan_work.work);
2019 mutex_lock(&os->os_lock);
2020 ocfs2_queue_orphan_scan(osb);
2021 if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE)
2022 queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
2023 ocfs2_orphan_scan_timeout());
2024 mutex_unlock(&os->os_lock);
2027 void ocfs2_orphan_scan_stop(struct ocfs2_super *osb)
2029 struct ocfs2_orphan_scan *os;
2031 os = &osb->osb_orphan_scan;
2032 if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) {
2033 atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
2034 mutex_lock(&os->os_lock);
2035 cancel_delayed_work(&os->os_orphan_scan_work);
2036 mutex_unlock(&os->os_lock);
2040 void ocfs2_orphan_scan_init(struct ocfs2_super *osb)
2042 struct ocfs2_orphan_scan *os;
2044 os = &osb->osb_orphan_scan;
2048 mutex_init(&os->os_lock);
2049 INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work);
2052 void ocfs2_orphan_scan_start(struct ocfs2_super *osb)
2054 struct ocfs2_orphan_scan *os;
2056 os = &osb->osb_orphan_scan;
2057 os->os_scantime = ktime_get_seconds();
2058 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
2059 atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
2061 atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE);
2062 queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
2063 ocfs2_orphan_scan_timeout());
2067 struct ocfs2_orphan_filldir_priv {
2068 struct dir_context ctx;
2070 struct ocfs2_super *osb;
2071 enum ocfs2_orphan_reco_type orphan_reco_type;
2074 static bool ocfs2_orphan_filldir(struct dir_context *ctx, const char *name,
2075 int name_len, loff_t pos, u64 ino,
2078 struct ocfs2_orphan_filldir_priv *p =
2079 container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx);
2082 if (name_len == 1 && !strncmp(".", name, 1))
2084 if (name_len == 2 && !strncmp("..", name, 2))
2087 /* do not include dio entry in case of orphan scan */
2088 if ((p->orphan_reco_type == ORPHAN_NO_NEED_TRUNCATE) &&
2089 (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
2090 OCFS2_DIO_ORPHAN_PREFIX_LEN)))
2093 /* Skip bad inodes so that recovery can continue */
2094 iter = ocfs2_iget(p->osb, ino,
2095 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
2099 if (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
2100 OCFS2_DIO_ORPHAN_PREFIX_LEN))
2101 OCFS2_I(iter)->ip_flags |= OCFS2_INODE_DIO_ORPHAN_ENTRY;
2103 /* Skip inodes which are already added to recover list, since dio may
2104 * happen concurrently with unlink/rename */
2105 if (OCFS2_I(iter)->ip_next_orphan) {
2110 trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno);
2111 /* No locking is required for the next_orphan queue as there
2112 * is only ever a single process doing orphan recovery. */
2113 OCFS2_I(iter)->ip_next_orphan = p->head;
2119 static int ocfs2_queue_orphans(struct ocfs2_super *osb,
2121 struct inode **head,
2122 enum ocfs2_orphan_reco_type orphan_reco_type)
2125 struct inode *orphan_dir_inode = NULL;
2126 struct ocfs2_orphan_filldir_priv priv = {
2127 .ctx.actor = ocfs2_orphan_filldir,
2130 .orphan_reco_type = orphan_reco_type
2133 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2134 ORPHAN_DIR_SYSTEM_INODE,
2136 if (!orphan_dir_inode) {
2142 inode_lock(orphan_dir_inode);
2143 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
2149 status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx);
2158 ocfs2_inode_unlock(orphan_dir_inode, 0);
2160 inode_unlock(orphan_dir_inode);
2161 iput(orphan_dir_inode);
2165 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
2170 spin_lock(&osb->osb_lock);
2171 ret = !osb->osb_orphan_wipes[slot];
2172 spin_unlock(&osb->osb_lock);
2176 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
2179 spin_lock(&osb->osb_lock);
2180 /* Mark ourselves such that new processes in delete_inode()
2181 * know to quit early. */
2182 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
2183 while (osb->osb_orphan_wipes[slot]) {
2184 /* If any processes are already in the middle of an
2185 * orphan wipe on this dir, then we need to wait for
2187 spin_unlock(&osb->osb_lock);
2188 wait_event_interruptible(osb->osb_wipe_event,
2189 ocfs2_orphan_recovery_can_continue(osb, slot));
2190 spin_lock(&osb->osb_lock);
2192 spin_unlock(&osb->osb_lock);
2195 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
2198 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
2202 * Orphan recovery. Each mounted node has it's own orphan dir which we
2203 * must run during recovery. Our strategy here is to build a list of
2204 * the inodes in the orphan dir and iget/iput them. The VFS does
2205 * (most) of the rest of the work.
2207 * Orphan recovery can happen at any time, not just mount so we have a
2208 * couple of extra considerations.
2210 * - We grab as many inodes as we can under the orphan dir lock -
2211 * doing iget() outside the orphan dir risks getting a reference on
2213 * - We must be sure not to deadlock with other processes on the
2214 * system wanting to run delete_inode(). This can happen when they go
2215 * to lock the orphan dir and the orphan recovery process attempts to
2216 * iget() inside the orphan dir lock. This can be avoided by
2217 * advertising our state to ocfs2_delete_inode().
2219 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
2221 enum ocfs2_orphan_reco_type orphan_reco_type)
2224 struct inode *inode = NULL;
2226 struct ocfs2_inode_info *oi;
2227 struct buffer_head *di_bh = NULL;
2228 struct ocfs2_dinode *di = NULL;
2230 trace_ocfs2_recover_orphans(slot);
2232 ocfs2_mark_recovering_orphan_dir(osb, slot);
2233 ret = ocfs2_queue_orphans(osb, slot, &inode, orphan_reco_type);
2234 ocfs2_clear_recovering_orphan_dir(osb, slot);
2236 /* Error here should be noted, but we want to continue with as
2237 * many queued inodes as we've got. */
2242 oi = OCFS2_I(inode);
2243 trace_ocfs2_recover_orphans_iput(
2244 (unsigned long long)oi->ip_blkno);
2246 iter = oi->ip_next_orphan;
2247 oi->ip_next_orphan = NULL;
2249 if (oi->ip_flags & OCFS2_INODE_DIO_ORPHAN_ENTRY) {
2251 ret = ocfs2_rw_lock(inode, 1);
2257 * We need to take and drop the inode lock to
2258 * force read inode from disk.
2260 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2266 di = (struct ocfs2_dinode *)di_bh->b_data;
2268 if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) {
2269 ret = ocfs2_truncate_file(inode, di_bh,
2270 i_size_read(inode));
2277 ret = ocfs2_del_inode_from_orphan(osb, inode,
2283 ocfs2_inode_unlock(inode, 1);
2287 ocfs2_rw_unlock(inode, 1);
2289 inode_unlock(inode);
2291 /* clear dio flag in ocfs2_inode_info */
2292 oi->ip_flags &= ~OCFS2_INODE_DIO_ORPHAN_ENTRY;
2294 spin_lock(&oi->ip_lock);
2295 /* Set the proper information to get us going into
2296 * ocfs2_delete_inode. */
2297 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2298 spin_unlock(&oi->ip_lock);
2308 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
2310 /* This check is good because ocfs2 will wait on our recovery
2311 * thread before changing it to something other than MOUNTED
2313 wait_event(osb->osb_mount_event,
2314 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
2315 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
2316 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
2318 /* If there's an error on mount, then we may never get to the
2319 * MOUNTED flag, but this is set right before
2320 * dismount_volume() so we can trust it. */
2321 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
2322 trace_ocfs2_wait_on_mount(VOLUME_DISABLED);
2323 mlog(0, "mount error, exiting!\n");
2330 static int ocfs2_commit_thread(void *arg)
2333 struct ocfs2_super *osb = arg;
2334 struct ocfs2_journal *journal = osb->journal;
2336 /* we can trust j_num_trans here because _should_stop() is only set in
2337 * shutdown and nobody other than ourselves should be able to start
2338 * transactions. committing on shutdown might take a few iterations
2339 * as final transactions put deleted inodes on the list */
2340 while (!(kthread_should_stop() &&
2341 atomic_read(&journal->j_num_trans) == 0)) {
2343 wait_event_interruptible(osb->checkpoint_event,
2344 atomic_read(&journal->j_num_trans)
2345 || kthread_should_stop());
2347 status = ocfs2_commit_cache(osb);
2349 static unsigned long abort_warn_time;
2351 /* Warn about this once per minute */
2352 if (printk_timed_ratelimit(&abort_warn_time, 60*HZ))
2353 mlog(ML_ERROR, "status = %d, journal is "
2354 "already aborted.\n", status);
2356 * After ocfs2_commit_cache() fails, j_num_trans has a
2357 * non-zero value. Sleep here to avoid a busy-wait
2360 msleep_interruptible(1000);
2363 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
2365 "commit_thread: %u transactions pending on "
2367 atomic_read(&journal->j_num_trans));
2374 /* Reads all the journal inodes without taking any cluster locks. Used
2375 * for hard readonly access to determine whether any journal requires
2376 * recovery. Also used to refresh the recovery generation numbers after
2377 * a journal has been recovered by another node.
2379 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
2383 struct buffer_head *di_bh = NULL;
2384 struct ocfs2_dinode *di;
2385 int journal_dirty = 0;
2387 for(slot = 0; slot < osb->max_slots; slot++) {
2388 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
2394 di = (struct ocfs2_dinode *) di_bh->b_data;
2396 osb->slot_recovery_generations[slot] =
2397 ocfs2_get_recovery_generation(di);
2399 if (le32_to_cpu(di->id1.journal1.ij_flags) &
2400 OCFS2_JOURNAL_DIRTY_FL)