xref: /linux/fs/ocfs2/journal.c (revision be81084e032c2d74f51173e30f687ce13476cb73)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * journal.c
4  *
5  * Defines functions of journalling api
6  *
7  * Copyright (C) 2003, 2004 Oracle.  All rights reserved.
8  */
9 
10 #include <linux/fs.h>
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>
19 
20 #include <cluster/masklog.h>
21 
22 #include "ocfs2.h"
23 
24 #include "alloc.h"
25 #include "blockcheck.h"
26 #include "dir.h"
27 #include "dlmglue.h"
28 #include "extent_map.h"
29 #include "heartbeat.h"
30 #include "inode.h"
31 #include "journal.h"
32 #include "localalloc.h"
33 #include "slot_map.h"
34 #include "super.h"
35 #include "sysfile.h"
36 #include "uptodate.h"
37 #include "quota.h"
38 #include "file.h"
39 #include "namei.h"
40 
41 #include "buffer_head_io.h"
42 #include "ocfs2_trace.h"
43 
44 DEFINE_SPINLOCK(trans_inc_lock);
45 
46 #define ORPHAN_SCAN_SCHEDULE_TIMEOUT 300000
47 
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,
57 				 int slot_num);
58 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
59 				 int slot,
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,
63 					    int slot_num,
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);
68 
69 static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
70 {
71 	return __ocfs2_wait_on_mount(osb, 0);
72 }
73 
74 static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
75 {
76 	return __ocfs2_wait_on_mount(osb, 1);
77 }
78 
79 /*
80  * This replay_map is to track online/offline slots, so we could recover
81  * offline slots during recovery and mount
82  */
83 
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 */
88 };
89 
90 struct ocfs2_replay_map {
91 	unsigned int rm_slots;
92 	enum ocfs2_replay_state rm_state;
93 	unsigned char rm_replay_slots[] __counted_by(rm_slots);
94 };
95 
96 static void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
97 {
98 	if (!osb->replay_map)
99 		return;
100 
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)
103 		return;
104 
105 	osb->replay_map->rm_state = state;
106 }
107 
108 int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
109 {
110 	struct ocfs2_replay_map *replay_map;
111 	int i, node_num;
112 
113 	/* If replay map is already set, we don't do it again */
114 	if (osb->replay_map)
115 		return 0;
116 
117 	replay_map = kzalloc_flex(*replay_map, rm_replay_slots, osb->max_slots);
118 	if (!replay_map) {
119 		mlog_errno(-ENOMEM);
120 		return -ENOMEM;
121 	}
122 
123 	spin_lock(&osb->osb_lock);
124 
125 	replay_map->rm_slots = osb->max_slots;
126 	replay_map->rm_state = REPLAY_UNNEEDED;
127 
128 	/* set rm_replay_slots for offline slot(s) */
129 	for (i = 0; i < replay_map->rm_slots; i++) {
130 		if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
131 			replay_map->rm_replay_slots[i] = 1;
132 	}
133 
134 	osb->replay_map = replay_map;
135 	spin_unlock(&osb->osb_lock);
136 	return 0;
137 }
138 
139 static void ocfs2_queue_replay_slots(struct ocfs2_super *osb,
140 		enum ocfs2_orphan_reco_type orphan_reco_type)
141 {
142 	struct ocfs2_replay_map *replay_map = osb->replay_map;
143 	int i;
144 
145 	if (!replay_map)
146 		return;
147 
148 	if (replay_map->rm_state != REPLAY_NEEDED)
149 		return;
150 
151 	for (i = 0; i < replay_map->rm_slots; i++)
152 		if (replay_map->rm_replay_slots[i])
153 			ocfs2_queue_recovery_completion(osb->journal, i, NULL,
154 							NULL, NULL,
155 							orphan_reco_type);
156 	replay_map->rm_state = REPLAY_DONE;
157 }
158 
159 void ocfs2_free_replay_slots(struct ocfs2_super *osb)
160 {
161 	struct ocfs2_replay_map *replay_map = osb->replay_map;
162 
163 	if (!osb->replay_map)
164 		return;
165 
166 	kfree(replay_map);
167 	osb->replay_map = NULL;
168 }
169 
170 int ocfs2_recovery_init(struct ocfs2_super *osb)
171 {
172 	struct ocfs2_recovery_map *rm;
173 
174 	mutex_init(&osb->recovery_lock);
175 	osb->recovery_state = OCFS2_REC_ENABLED;
176 	osb->recovery_thread_task = NULL;
177 	init_waitqueue_head(&osb->recovery_event);
178 
179 	rm = kzalloc_flex(*rm, rm_entries, osb->max_slots);
180 	if (!rm) {
181 		mlog_errno(-ENOMEM);
182 		return -ENOMEM;
183 	}
184 
185 	osb->recovery_map = rm;
186 
187 	return 0;
188 }
189 
190 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
191 {
192 	return osb->recovery_thread_task != NULL;
193 }
194 
195 static void ocfs2_recovery_disable(struct ocfs2_super *osb,
196 				   enum ocfs2_recovery_state state)
197 {
198 	mutex_lock(&osb->recovery_lock);
199 	/*
200 	 * If recovery thread is not running, we can directly transition to
201 	 * final state.
202 	 */
203 	if (!ocfs2_recovery_thread_running(osb)) {
204 		osb->recovery_state = state + 1;
205 		goto out_lock;
206 	}
207 	osb->recovery_state = state;
208 	/* Wait for recovery thread to acknowledge state transition */
209 	wait_event_cmd(osb->recovery_event,
210 		       !ocfs2_recovery_thread_running(osb) ||
211 				osb->recovery_state >= state + 1,
212 		       mutex_unlock(&osb->recovery_lock),
213 		       mutex_lock(&osb->recovery_lock));
214 out_lock:
215 	mutex_unlock(&osb->recovery_lock);
216 
217 	/*
218 	 * At this point we know that no more recovery work can be queued so
219 	 * wait for any recovery completion work to complete.
220 	 */
221 	if (osb->ocfs2_wq)
222 		flush_workqueue(osb->ocfs2_wq);
223 }
224 
225 void ocfs2_recovery_disable_quota(struct ocfs2_super *osb)
226 {
227 	ocfs2_recovery_disable(osb, OCFS2_REC_QUOTA_WANT_DISABLE);
228 }
229 
230 void ocfs2_recovery_exit(struct ocfs2_super *osb)
231 {
232 	struct ocfs2_recovery_map *rm;
233 
234 	/* disable any new recovery threads and wait for any currently
235 	 * running ones to exit. Do this before setting the vol_state. */
236 	ocfs2_recovery_disable(osb, OCFS2_REC_WANT_DISABLE);
237 
238 	/*
239 	 * Now that recovery is shut down, and the osb is about to be
240 	 * freed,  the osb_lock is not taken here.
241 	 */
242 	rm = osb->recovery_map;
243 	/* XXX: Should we bug if there are dirty entries? */
244 
245 	kfree(rm);
246 }
247 
248 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
249 				     unsigned int node_num)
250 {
251 	int i;
252 	struct ocfs2_recovery_map *rm = osb->recovery_map;
253 
254 	assert_spin_locked(&osb->osb_lock);
255 
256 	for (i = 0; i < rm->rm_used; i++) {
257 		if (rm->rm_entries[i] == node_num)
258 			return 1;
259 	}
260 
261 	return 0;
262 }
263 
264 /* Behaves like test-and-set.  Returns the previous value */
265 static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
266 				  unsigned int node_num)
267 {
268 	struct ocfs2_recovery_map *rm = osb->recovery_map;
269 
270 	spin_lock(&osb->osb_lock);
271 	if (__ocfs2_recovery_map_test(osb, node_num)) {
272 		spin_unlock(&osb->osb_lock);
273 		return 1;
274 	}
275 
276 	/* XXX: Can this be exploited? Not from o2dlm... */
277 	BUG_ON(rm->rm_used >= osb->max_slots);
278 
279 	rm->rm_entries[rm->rm_used] = node_num;
280 	rm->rm_used++;
281 	spin_unlock(&osb->osb_lock);
282 
283 	return 0;
284 }
285 
286 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
287 				     unsigned int node_num)
288 {
289 	int i;
290 	struct ocfs2_recovery_map *rm = osb->recovery_map;
291 
292 	spin_lock(&osb->osb_lock);
293 
294 	for (i = 0; i < rm->rm_used; i++) {
295 		if (rm->rm_entries[i] == node_num)
296 			break;
297 	}
298 
299 	if (i < rm->rm_used) {
300 		/* XXX: be careful with the pointer math */
301 		memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
302 			(rm->rm_used - i - 1) * sizeof(unsigned int));
303 		rm->rm_used--;
304 	}
305 
306 	spin_unlock(&osb->osb_lock);
307 }
308 
309 static int ocfs2_commit_cache(struct ocfs2_super *osb)
310 {
311 	int status = 0;
312 	unsigned int flushed;
313 	struct ocfs2_journal *journal = NULL;
314 
315 	journal = osb->journal;
316 
317 	/* Flush all pending commits and checkpoint the journal. */
318 	down_write(&journal->j_trans_barrier);
319 
320 	flushed = atomic_read(&journal->j_num_trans);
321 	trace_ocfs2_commit_cache_begin(flushed);
322 	if (flushed == 0) {
323 		up_write(&journal->j_trans_barrier);
324 		goto finally;
325 	}
326 
327 	jbd2_journal_lock_updates(journal->j_journal);
328 	status = jbd2_journal_flush(journal->j_journal, 0);
329 	jbd2_journal_unlock_updates(journal->j_journal);
330 	if (status < 0) {
331 		up_write(&journal->j_trans_barrier);
332 		mlog_errno(status);
333 		goto finally;
334 	}
335 
336 	ocfs2_inc_trans_id(journal);
337 
338 	flushed = atomic_read(&journal->j_num_trans);
339 	atomic_set(&journal->j_num_trans, 0);
340 	up_write(&journal->j_trans_barrier);
341 
342 	trace_ocfs2_commit_cache_end(journal->j_trans_id, flushed);
343 
344 	ocfs2_wake_downconvert_thread(osb);
345 	wake_up(&journal->j_checkpointed);
346 finally:
347 	return status;
348 }
349 
350 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
351 {
352 	journal_t *journal = osb->journal->j_journal;
353 	handle_t *handle;
354 
355 	BUG_ON(!osb || !osb->journal->j_journal);
356 
357 	if (ocfs2_is_hard_readonly(osb))
358 		return ERR_PTR(-EROFS);
359 
360 	BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
361 	BUG_ON(max_buffs <= 0);
362 
363 	/* Nested transaction? Just return the handle... */
364 	if (journal_current_handle())
365 		return jbd2_journal_start(journal, max_buffs);
366 
367 	sb_start_intwrite(osb->sb);
368 
369 	down_read(&osb->journal->j_trans_barrier);
370 
371 	handle = jbd2_journal_start(journal, max_buffs);
372 	if (IS_ERR(handle)) {
373 		up_read(&osb->journal->j_trans_barrier);
374 		sb_end_intwrite(osb->sb);
375 
376 		mlog_errno(PTR_ERR(handle));
377 
378 		if (is_journal_aborted(journal)) {
379 			ocfs2_abort(osb->sb, "Detected aborted journal\n");
380 			handle = ERR_PTR(-EROFS);
381 		}
382 	} else {
383 		if (!ocfs2_mount_local(osb))
384 			atomic_inc(&(osb->journal->j_num_trans));
385 	}
386 
387 	return handle;
388 }
389 
390 int ocfs2_commit_trans(struct ocfs2_super *osb,
391 		       handle_t *handle)
392 {
393 	int ret, nested;
394 	struct ocfs2_journal *journal = osb->journal;
395 
396 	BUG_ON(!handle);
397 
398 	nested = handle->h_ref > 1;
399 	ret = jbd2_journal_stop(handle);
400 	if (ret < 0)
401 		mlog_errno(ret);
402 
403 	if (!nested) {
404 		up_read(&journal->j_trans_barrier);
405 		sb_end_intwrite(osb->sb);
406 	}
407 
408 	return ret;
409 }
410 
411 /*
412  * 'nblocks' is what you want to add to the current transaction.
413  *
414  * This might call jbd2_journal_restart() which will commit dirty buffers
415  * and then restart the transaction. Before calling
416  * ocfs2_extend_trans(), any changed blocks should have been
417  * dirtied. After calling it, all blocks which need to be changed must
418  * go through another set of journal_access/journal_dirty calls.
419  *
420  * WARNING: This will not release any semaphores or disk locks taken
421  * during the transaction, so make sure they were taken *before*
422  * start_trans or we'll have ordering deadlocks.
423  *
424  * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
425  * good because transaction ids haven't yet been recorded on the
426  * cluster locks associated with this handle.
427  */
428 int ocfs2_extend_trans(handle_t *handle, int nblocks)
429 {
430 	int status, old_nblocks;
431 
432 	BUG_ON(!handle);
433 	BUG_ON(nblocks < 0);
434 
435 	if (!nblocks)
436 		return 0;
437 
438 	old_nblocks = jbd2_handle_buffer_credits(handle);
439 
440 	trace_ocfs2_extend_trans(old_nblocks, nblocks);
441 
442 #ifdef CONFIG_OCFS2_DEBUG_FS
443 	status = 1;
444 #else
445 	status = jbd2_journal_extend(handle, nblocks, 0);
446 	if (status < 0) {
447 		mlog_errno(status);
448 		goto bail;
449 	}
450 #endif
451 
452 	if (status > 0) {
453 		trace_ocfs2_extend_trans_restart(old_nblocks + nblocks);
454 		status = jbd2_journal_restart(handle,
455 					      old_nblocks + nblocks);
456 		if (status < 0) {
457 			mlog_errno(status);
458 			goto bail;
459 		}
460 	}
461 
462 	status = 0;
463 bail:
464 	return status;
465 }
466 
467 /*
468  * Make sure handle has at least 'nblocks' credits available. If it does not
469  * have that many credits available, we will try to extend the handle to have
470  * enough credits. If that fails, we will restart transaction to have enough
471  * credits. Similar notes regarding data consistency and locking implications
472  * as for ocfs2_extend_trans() apply here.
473  */
474 int ocfs2_assure_trans_credits(handle_t *handle, int nblocks)
475 {
476 	int old_nblks = jbd2_handle_buffer_credits(handle);
477 
478 	trace_ocfs2_assure_trans_credits(old_nblks);
479 	if (old_nblks >= nblocks)
480 		return 0;
481 	return ocfs2_extend_trans(handle, nblocks - old_nblks);
482 }
483 
484 /*
485  * If we have fewer than thresh credits, extend by OCFS2_MAX_TRANS_DATA.
486  * If that fails, restart the transaction & regain write access for the
487  * buffer head which is used for metadata modifications.
488  * Taken from Ext4: extend_or_restart_transaction()
489  */
490 int ocfs2_allocate_extend_trans(handle_t *handle, int thresh)
491 {
492 	int status, old_nblks;
493 
494 	BUG_ON(!handle);
495 
496 	old_nblks = jbd2_handle_buffer_credits(handle);
497 	trace_ocfs2_allocate_extend_trans(old_nblks, thresh);
498 
499 	if (old_nblks < thresh)
500 		return 0;
501 
502 	status = jbd2_journal_extend(handle, OCFS2_MAX_TRANS_DATA, 0);
503 	if (status < 0) {
504 		mlog_errno(status);
505 		goto bail;
506 	}
507 
508 	if (status > 0) {
509 		status = jbd2_journal_restart(handle, OCFS2_MAX_TRANS_DATA);
510 		if (status < 0)
511 			mlog_errno(status);
512 	}
513 
514 bail:
515 	return status;
516 }
517 
518 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
519 {
520 	return container_of(triggers, struct ocfs2_triggers, ot_triggers);
521 }
522 
523 static void ocfs2_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
524 				 struct buffer_head *bh,
525 				 void *data, size_t size)
526 {
527 	struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
528 
529 	/*
530 	 * We aren't guaranteed to have the superblock here, so we
531 	 * must unconditionally compute the ecc data.
532 	 * __ocfs2_journal_access() will only set the triggers if
533 	 * metaecc is enabled.
534 	 */
535 	ocfs2_block_check_compute(data, size, data + ot->ot_offset);
536 }
537 
538 /*
539  * Quota blocks have their own trigger because the struct ocfs2_block_check
540  * offset depends on the blocksize.
541  */
542 static void ocfs2_dq_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
543 				 struct buffer_head *bh,
544 				 void *data, size_t size)
545 {
546 	struct ocfs2_disk_dqtrailer *dqt =
547 		ocfs2_block_dqtrailer(size, data);
548 
549 	/*
550 	 * We aren't guaranteed to have the superblock here, so we
551 	 * must unconditionally compute the ecc data.
552 	 * __ocfs2_journal_access() will only set the triggers if
553 	 * metaecc is enabled.
554 	 */
555 	ocfs2_block_check_compute(data, size, &dqt->dq_check);
556 }
557 
558 /*
559  * Directory blocks also have their own trigger because the
560  * struct ocfs2_block_check offset depends on the blocksize.
561  */
562 static void ocfs2_db_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
563 				 struct buffer_head *bh,
564 				 void *data, size_t size)
565 {
566 	struct ocfs2_dir_block_trailer *trailer =
567 		ocfs2_dir_trailer_from_size(size, data);
568 
569 	/*
570 	 * We aren't guaranteed to have the superblock here, so we
571 	 * must unconditionally compute the ecc data.
572 	 * __ocfs2_journal_access() will only set the triggers if
573 	 * metaecc is enabled.
574 	 */
575 	ocfs2_block_check_compute(data, size, &trailer->db_check);
576 }
577 
578 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
579 				struct buffer_head *bh)
580 {
581 	struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
582 
583 	mlog(ML_ERROR,
584 	     "ocfs2_abort_trigger called by JBD2.  bh = 0x%lx, "
585 	     "bh->b_blocknr = %llu\n",
586 	     (unsigned long)bh,
587 	     (unsigned long long)bh->b_blocknr);
588 
589 	ocfs2_error(ot->sb,
590 		    "JBD2 has aborted our journal, ocfs2 cannot continue\n");
591 }
592 
593 static void ocfs2_setup_csum_triggers(struct super_block *sb,
594 				      enum ocfs2_journal_trigger_type type,
595 				      struct ocfs2_triggers *ot)
596 {
597 	BUG_ON(type >= OCFS2_JOURNAL_TRIGGER_COUNT);
598 
599 	switch (type) {
600 	case OCFS2_JTR_DI:
601 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
602 		ot->ot_offset = offsetof(struct ocfs2_dinode, i_check);
603 		break;
604 	case OCFS2_JTR_EB:
605 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
606 		ot->ot_offset = offsetof(struct ocfs2_extent_block, h_check);
607 		break;
608 	case OCFS2_JTR_RB:
609 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
610 		ot->ot_offset = offsetof(struct ocfs2_refcount_block, rf_check);
611 		break;
612 	case OCFS2_JTR_GD:
613 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
614 		ot->ot_offset = offsetof(struct ocfs2_group_desc, bg_check);
615 		break;
616 	case OCFS2_JTR_DB:
617 		ot->ot_triggers.t_frozen = ocfs2_db_frozen_trigger;
618 		break;
619 	case OCFS2_JTR_XB:
620 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
621 		ot->ot_offset = offsetof(struct ocfs2_xattr_block, xb_check);
622 		break;
623 	case OCFS2_JTR_DQ:
624 		ot->ot_triggers.t_frozen = ocfs2_dq_frozen_trigger;
625 		break;
626 	case OCFS2_JTR_DR:
627 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
628 		ot->ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check);
629 		break;
630 	case OCFS2_JTR_DL:
631 		ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
632 		ot->ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check);
633 		break;
634 	case OCFS2_JTR_NONE:
635 		/* To make compiler happy... */
636 		return;
637 	}
638 
639 	ot->ot_triggers.t_abort = ocfs2_abort_trigger;
640 	ot->sb = sb;
641 }
642 
643 void ocfs2_initialize_journal_triggers(struct super_block *sb,
644 				       struct ocfs2_triggers triggers[])
645 {
646 	enum ocfs2_journal_trigger_type type;
647 
648 	for (type = OCFS2_JTR_DI; type < OCFS2_JOURNAL_TRIGGER_COUNT; type++)
649 		ocfs2_setup_csum_triggers(sb, type, &triggers[type]);
650 }
651 
652 static int __ocfs2_journal_access(handle_t *handle,
653 				  struct ocfs2_caching_info *ci,
654 				  struct buffer_head *bh,
655 				  struct ocfs2_triggers *triggers,
656 				  int type)
657 {
658 	int status;
659 	struct ocfs2_super *osb =
660 		OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
661 
662 	BUG_ON(!ci || !ci->ci_ops);
663 	BUG_ON(!handle);
664 	BUG_ON(!bh);
665 
666 	trace_ocfs2_journal_access(
667 		(unsigned long long)ocfs2_metadata_cache_owner(ci),
668 		(unsigned long long)bh->b_blocknr, type, bh->b_size);
669 
670 	/* we can safely remove this assertion after testing. */
671 	if (!buffer_uptodate(bh)) {
672 		mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
673 		mlog(ML_ERROR, "b_blocknr=%llu, b_state=0x%lx\n",
674 		     (unsigned long long)bh->b_blocknr, bh->b_state);
675 
676 		lock_buffer(bh);
677 		/*
678 		 * A previous transaction with a couple of buffer heads fail
679 		 * to checkpoint, so all the bhs are marked as BH_Write_EIO.
680 		 * For current transaction, the bh is just among those error
681 		 * bhs which previous transaction handle. We can't just clear
682 		 * its BH_Write_EIO and reuse directly, since other bhs are
683 		 * not written to disk yet and that will cause metadata
684 		 * inconsistency. So we should set fs read-only to avoid
685 		 * further damage.
686 		 */
687 		if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) {
688 			unlock_buffer(bh);
689 			return ocfs2_error(osb->sb, "A previous attempt to "
690 					"write this buffer head failed\n");
691 		}
692 		unlock_buffer(bh);
693 	}
694 
695 	/* Set the current transaction information on the ci so
696 	 * that the locking code knows whether it can drop it's locks
697 	 * on this ci or not. We're protected from the commit
698 	 * thread updating the current transaction id until
699 	 * ocfs2_commit_trans() because ocfs2_start_trans() took
700 	 * j_trans_barrier for us. */
701 	ocfs2_set_ci_lock_trans(osb->journal, ci);
702 
703 	ocfs2_metadata_cache_io_lock(ci);
704 	switch (type) {
705 	case OCFS2_JOURNAL_ACCESS_CREATE:
706 	case OCFS2_JOURNAL_ACCESS_WRITE:
707 		status = jbd2_journal_get_write_access(handle, bh);
708 		break;
709 
710 	case OCFS2_JOURNAL_ACCESS_UNDO:
711 		status = jbd2_journal_get_undo_access(handle, bh);
712 		break;
713 
714 	default:
715 		status = -EINVAL;
716 		mlog(ML_ERROR, "Unknown access type!\n");
717 	}
718 	if (!status && ocfs2_meta_ecc(osb) && triggers)
719 		jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
720 	ocfs2_metadata_cache_io_unlock(ci);
721 
722 	if (status < 0)
723 		mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
724 		     status, type);
725 
726 	return status;
727 }
728 
729 int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
730 			    struct buffer_head *bh, int type)
731 {
732 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
733 
734 	return __ocfs2_journal_access(handle, ci, bh,
735 				      &osb->s_journal_triggers[OCFS2_JTR_DI],
736 				      type);
737 }
738 
739 int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
740 			    struct buffer_head *bh, int type)
741 {
742 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
743 
744 	return __ocfs2_journal_access(handle, ci, bh,
745 				      &osb->s_journal_triggers[OCFS2_JTR_EB],
746 				      type);
747 }
748 
749 int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
750 			    struct buffer_head *bh, int type)
751 {
752 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
753 
754 	return __ocfs2_journal_access(handle, ci, bh,
755 				      &osb->s_journal_triggers[OCFS2_JTR_RB],
756 				      type);
757 }
758 
759 int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
760 			    struct buffer_head *bh, int type)
761 {
762 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
763 
764 	return __ocfs2_journal_access(handle, ci, bh,
765 				     &osb->s_journal_triggers[OCFS2_JTR_GD],
766 				     type);
767 }
768 
769 int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
770 			    struct buffer_head *bh, int type)
771 {
772 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
773 
774 	return __ocfs2_journal_access(handle, ci, bh,
775 				     &osb->s_journal_triggers[OCFS2_JTR_DB],
776 				     type);
777 }
778 
779 int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
780 			    struct buffer_head *bh, int type)
781 {
782 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
783 
784 	return __ocfs2_journal_access(handle, ci, bh,
785 				     &osb->s_journal_triggers[OCFS2_JTR_XB],
786 				     type);
787 }
788 
789 int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
790 			    struct buffer_head *bh, int type)
791 {
792 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
793 
794 	return __ocfs2_journal_access(handle, ci, bh,
795 				     &osb->s_journal_triggers[OCFS2_JTR_DQ],
796 				     type);
797 }
798 
799 int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
800 			    struct buffer_head *bh, int type)
801 {
802 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
803 
804 	return __ocfs2_journal_access(handle, ci, bh,
805 				     &osb->s_journal_triggers[OCFS2_JTR_DR],
806 				     type);
807 }
808 
809 int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
810 			    struct buffer_head *bh, int type)
811 {
812 	struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
813 
814 	return __ocfs2_journal_access(handle, ci, bh,
815 				     &osb->s_journal_triggers[OCFS2_JTR_DL],
816 				     type);
817 }
818 
819 int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
820 			 struct buffer_head *bh, int type)
821 {
822 	return __ocfs2_journal_access(handle, ci, bh, NULL, type);
823 }
824 
825 void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh)
826 {
827 	int status;
828 
829 	trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr);
830 
831 	status = jbd2_journal_dirty_metadata(handle, bh);
832 	if (status) {
833 		mlog_errno(status);
834 		if (!is_handle_aborted(handle)) {
835 			journal_t *journal = handle->h_transaction->t_journal;
836 
837 			mlog(ML_ERROR, "jbd2_journal_dirty_metadata failed: "
838 			     "handle type %u started at line %u, credits %u/%u "
839 			     "errcode %d. Aborting transaction and journal.\n",
840 			     handle->h_type, handle->h_line_no,
841 			     handle->h_requested_credits,
842 			     jbd2_handle_buffer_credits(handle), status);
843 			handle->h_err = status;
844 			jbd2_journal_abort_handle(handle);
845 			jbd2_journal_abort(journal, status);
846 		}
847 	}
848 }
849 
850 #define OCFS2_DEFAULT_COMMIT_INTERVAL	(HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
851 
852 void ocfs2_set_journal_params(struct ocfs2_super *osb)
853 {
854 	journal_t *journal = osb->journal->j_journal;
855 	unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
856 
857 	if (osb->osb_commit_interval)
858 		commit_interval = osb->osb_commit_interval;
859 
860 	write_lock(&journal->j_state_lock);
861 	journal->j_commit_interval = commit_interval;
862 	if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
863 		journal->j_flags |= JBD2_BARRIER;
864 	else
865 		journal->j_flags &= ~JBD2_BARRIER;
866 	write_unlock(&journal->j_state_lock);
867 }
868 
869 /*
870  * alloc & initialize skeleton for journal structure.
871  * ocfs2_journal_init() will make fs have journal ability.
872  */
873 int ocfs2_journal_alloc(struct ocfs2_super *osb)
874 {
875 	int status = 0;
876 	struct ocfs2_journal *journal;
877 
878 	journal = kzalloc_obj(struct ocfs2_journal);
879 	if (!journal) {
880 		mlog(ML_ERROR, "unable to alloc journal\n");
881 		status = -ENOMEM;
882 		goto bail;
883 	}
884 	osb->journal = journal;
885 	journal->j_osb = osb;
886 
887 	atomic_set(&journal->j_num_trans, 0);
888 	init_rwsem(&journal->j_trans_barrier);
889 	init_waitqueue_head(&journal->j_checkpointed);
890 	spin_lock_init(&journal->j_lock);
891 	journal->j_trans_id = 1UL;
892 	INIT_LIST_HEAD(&journal->j_la_cleanups);
893 	INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
894 	journal->j_state = OCFS2_JOURNAL_FREE;
895 
896 bail:
897 	return status;
898 }
899 
900 static int ocfs2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
901 {
902 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
903 	loff_t range_start, range_end;
904 
905 	if (!jbd2_jinode_get_dirty_range(jinode, &range_start, &range_end))
906 		return 0;
907 
908 	return filemap_fdatawrite_range(mapping, range_start, range_end);
909 }
910 
911 int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty)
912 {
913 	int status = -1;
914 	struct inode *inode = NULL; /* the journal inode */
915 	journal_t *j_journal = NULL;
916 	struct ocfs2_journal *journal = osb->journal;
917 	struct ocfs2_dinode *di = NULL;
918 	struct buffer_head *bh = NULL;
919 	int inode_lock = 0;
920 
921 	BUG_ON(!journal);
922 	/* already have the inode for our journal */
923 	inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
924 					    osb->slot_num);
925 	if (inode == NULL) {
926 		status = -EACCES;
927 		mlog_errno(status);
928 		goto done;
929 	}
930 	if (is_bad_inode(inode)) {
931 		mlog(ML_ERROR, "access error (bad inode)\n");
932 		iput(inode);
933 		inode = NULL;
934 		status = -EACCES;
935 		goto done;
936 	}
937 
938 	SET_INODE_JOURNAL(inode);
939 	OCFS2_I(inode)->ip_open_count++;
940 
941 	/* Skip recovery waits here - journal inode metadata never
942 	 * changes in a live cluster so it can be considered an
943 	 * exception to the rule. */
944 	status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
945 	if (status < 0) {
946 		if (status != -ERESTARTSYS)
947 			mlog(ML_ERROR, "Could not get lock on journal!\n");
948 		goto done;
949 	}
950 
951 	inode_lock = 1;
952 	di = (struct ocfs2_dinode *)bh->b_data;
953 
954 	if (i_size_read(inode) <  OCFS2_MIN_JOURNAL_SIZE) {
955 		mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
956 		     i_size_read(inode));
957 		status = -EINVAL;
958 		goto done;
959 	}
960 
961 	trace_ocfs2_journal_init(i_size_read(inode),
962 				 (unsigned long long)inode->i_blocks,
963 				 OCFS2_I(inode)->ip_clusters);
964 
965 	/* call the kernels journal init function now */
966 	j_journal = jbd2_journal_init_inode(inode);
967 	if (IS_ERR(j_journal)) {
968 		mlog(ML_ERROR, "Linux journal layer error\n");
969 		status = PTR_ERR(j_journal);
970 		goto done;
971 	}
972 
973 	trace_ocfs2_journal_init_maxlen(j_journal->j_total_len);
974 
975 	*dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
976 		  OCFS2_JOURNAL_DIRTY_FL);
977 
978 	journal->j_journal = j_journal;
979 	journal->j_journal->j_submit_inode_data_buffers =
980 		ocfs2_journal_submit_inode_data_buffers;
981 	journal->j_journal->j_finish_inode_data_buffers =
982 		jbd2_journal_finish_inode_data_buffers;
983 	journal->j_inode = inode;
984 	journal->j_bh = bh;
985 
986 	ocfs2_set_journal_params(osb);
987 
988 	journal->j_state = OCFS2_JOURNAL_LOADED;
989 
990 	status = 0;
991 done:
992 	if (status < 0) {
993 		if (inode_lock)
994 			ocfs2_inode_unlock(inode, 1);
995 		brelse(bh);
996 		if (inode) {
997 			OCFS2_I(inode)->ip_open_count--;
998 			iput(inode);
999 		}
1000 	}
1001 
1002 	return status;
1003 }
1004 
1005 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
1006 {
1007 	le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
1008 }
1009 
1010 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
1011 {
1012 	return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
1013 }
1014 
1015 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
1016 				      int dirty, int replayed)
1017 {
1018 	int status;
1019 	unsigned int flags;
1020 	struct ocfs2_journal *journal = osb->journal;
1021 	struct buffer_head *bh = journal->j_bh;
1022 	struct ocfs2_dinode *fe;
1023 
1024 	fe = (struct ocfs2_dinode *)bh->b_data;
1025 
1026 	/* The journal bh on the osb always comes from ocfs2_journal_init()
1027 	 * and was validated there inside ocfs2_inode_lock_full().  It's a
1028 	 * code bug if we mess it up. */
1029 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1030 
1031 	flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1032 	if (dirty)
1033 		flags |= OCFS2_JOURNAL_DIRTY_FL;
1034 	else
1035 		flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1036 	fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1037 
1038 	if (replayed)
1039 		ocfs2_bump_recovery_generation(fe);
1040 
1041 	ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1042 	status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode));
1043 	if (status < 0)
1044 		mlog_errno(status);
1045 
1046 	return status;
1047 }
1048 
1049 /*
1050  * If the journal has been kmalloc'd it needs to be freed after this
1051  * call.
1052  */
1053 void ocfs2_journal_shutdown(struct ocfs2_super *osb)
1054 {
1055 	struct ocfs2_journal *journal = NULL;
1056 	int status = 0;
1057 	struct inode *inode = NULL;
1058 	int num_running_trans = 0;
1059 
1060 	BUG_ON(!osb);
1061 
1062 	journal = osb->journal;
1063 	if (!journal)
1064 		goto done;
1065 
1066 	inode = journal->j_inode;
1067 
1068 	if (journal->j_state != OCFS2_JOURNAL_LOADED)
1069 		goto done;
1070 
1071 	/* need to inc inode use count - jbd2_journal_destroy will iput. */
1072 	if (!igrab(inode))
1073 		BUG();
1074 
1075 	num_running_trans = atomic_read(&(journal->j_num_trans));
1076 	trace_ocfs2_journal_shutdown(num_running_trans);
1077 
1078 	/* Do a commit_cache here. It will flush our journal, *and*
1079 	 * release any locks that are still held.
1080 	 * set the SHUTDOWN flag and release the trans lock.
1081 	 * the commit thread will take the trans lock for us below. */
1082 	journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
1083 
1084 	/* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
1085 	 * drop the trans_lock (which we want to hold until we
1086 	 * completely destroy the journal. */
1087 	if (osb->commit_task) {
1088 		/* Wait for the commit thread */
1089 		trace_ocfs2_journal_shutdown_wait(osb->commit_task);
1090 		kthread_stop(osb->commit_task);
1091 		osb->commit_task = NULL;
1092 	}
1093 
1094 	BUG_ON(atomic_read(&(journal->j_num_trans)) != 0);
1095 
1096 	if (ocfs2_mount_local(osb) &&
1097 	    (journal->j_journal->j_flags & JBD2_LOADED)) {
1098 		jbd2_journal_lock_updates(journal->j_journal);
1099 		status = jbd2_journal_flush(journal->j_journal, 0);
1100 		jbd2_journal_unlock_updates(journal->j_journal);
1101 		if (status < 0)
1102 			mlog_errno(status);
1103 	}
1104 
1105 	/* Shutdown the kernel journal system */
1106 	if (!jbd2_journal_destroy(journal->j_journal) && !status) {
1107 		/*
1108 		 * Do not toggle if flush was unsuccessful otherwise
1109 		 * will leave dirty metadata in a "clean" journal
1110 		 */
1111 		status = ocfs2_journal_toggle_dirty(osb, 0, 0);
1112 		if (status < 0)
1113 			mlog_errno(status);
1114 	}
1115 	journal->j_journal = NULL;
1116 
1117 	OCFS2_I(inode)->ip_open_count--;
1118 
1119 	/* unlock our journal */
1120 	ocfs2_inode_unlock(inode, 1);
1121 
1122 	brelse(journal->j_bh);
1123 	journal->j_bh = NULL;
1124 
1125 	journal->j_state = OCFS2_JOURNAL_FREE;
1126 
1127 done:
1128 	iput(inode);
1129 	kfree(journal);
1130 	osb->journal = NULL;
1131 }
1132 
1133 static void ocfs2_clear_journal_error(struct super_block *sb,
1134 				      journal_t *journal,
1135 				      int slot)
1136 {
1137 	int olderr;
1138 
1139 	olderr = jbd2_journal_errno(journal);
1140 	if (olderr) {
1141 		mlog(ML_ERROR, "File system error %d recorded in "
1142 		     "journal %u.\n", olderr, slot);
1143 		mlog(ML_ERROR, "File system on device %s needs checking.\n",
1144 		     sb->s_id);
1145 
1146 		jbd2_journal_ack_err(journal);
1147 		jbd2_journal_clear_err(journal);
1148 	}
1149 }
1150 
1151 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
1152 {
1153 	int status = 0;
1154 	struct ocfs2_super *osb;
1155 
1156 	BUG_ON(!journal);
1157 
1158 	osb = journal->j_osb;
1159 
1160 	status = jbd2_journal_load(journal->j_journal);
1161 	if (status < 0) {
1162 		mlog(ML_ERROR, "Failed to load journal!\n");
1163 		goto done;
1164 	}
1165 
1166 	ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
1167 
1168 	if (replayed) {
1169 		jbd2_journal_lock_updates(journal->j_journal);
1170 		status = jbd2_journal_flush(journal->j_journal, 0);
1171 		jbd2_journal_unlock_updates(journal->j_journal);
1172 		if (status < 0)
1173 			mlog_errno(status);
1174 	}
1175 
1176 	status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
1177 	if (status < 0) {
1178 		mlog_errno(status);
1179 		goto done;
1180 	}
1181 
1182 	/* Launch the commit thread */
1183 	if (!local) {
1184 		osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
1185 				"ocfs2cmt-%s", osb->uuid_str);
1186 		if (IS_ERR(osb->commit_task)) {
1187 			status = PTR_ERR(osb->commit_task);
1188 			osb->commit_task = NULL;
1189 			mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
1190 			     "error=%d", status);
1191 			goto done;
1192 		}
1193 	} else
1194 		osb->commit_task = NULL;
1195 
1196 done:
1197 	return status;
1198 }
1199 
1200 
1201 /* 'full' flag tells us whether we clear out all blocks or if we just
1202  * mark the journal clean */
1203 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
1204 {
1205 	int status;
1206 
1207 	BUG_ON(!journal);
1208 
1209 	status = jbd2_journal_wipe(journal->j_journal, full);
1210 	if (status < 0) {
1211 		mlog_errno(status);
1212 		goto bail;
1213 	}
1214 
1215 	status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
1216 	if (status < 0)
1217 		mlog_errno(status);
1218 
1219 bail:
1220 	return status;
1221 }
1222 
1223 static int ocfs2_recovery_completed(struct ocfs2_super *osb)
1224 {
1225 	int empty;
1226 	struct ocfs2_recovery_map *rm = osb->recovery_map;
1227 
1228 	spin_lock(&osb->osb_lock);
1229 	empty = (rm->rm_used == 0);
1230 	spin_unlock(&osb->osb_lock);
1231 
1232 	return empty;
1233 }
1234 
1235 void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1236 {
1237 	wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1238 }
1239 
1240 /*
1241  * JBD Might read a cached version of another nodes journal file. We
1242  * don't want this as this file changes often and we get no
1243  * notification on those changes. The only way to be sure that we've
1244  * got the most up to date version of those blocks then is to force
1245  * read them off disk. Just searching through the buffer cache won't
1246  * work as there may be pages backing this file which are still marked
1247  * up to date. We know things can't change on this file underneath us
1248  * as we have the lock by now :)
1249  */
1250 static int ocfs2_force_read_journal(struct inode *inode)
1251 {
1252 	int status = 0;
1253 	int i;
1254 	u64 v_blkno, p_blkno, p_blocks, num_blocks;
1255 	struct buffer_head *bh = NULL;
1256 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1257 
1258 	num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
1259 	v_blkno = 0;
1260 	while (v_blkno < num_blocks) {
1261 		status = ocfs2_extent_map_get_blocks(inode, v_blkno,
1262 						     &p_blkno, &p_blocks, NULL);
1263 		if (status < 0) {
1264 			mlog_errno(status);
1265 			goto bail;
1266 		}
1267 
1268 		for (i = 0; i < p_blocks; i++, p_blkno++) {
1269 			bh = __find_get_block_nonatomic(osb->sb->s_bdev, p_blkno,
1270 					osb->sb->s_blocksize);
1271 			/* block not cached. */
1272 			if (!bh)
1273 				continue;
1274 
1275 			brelse(bh);
1276 			bh = NULL;
1277 			/* We are reading journal data which should not
1278 			 * be put in the uptodate cache.
1279 			 */
1280 			status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh);
1281 			if (status < 0) {
1282 				mlog_errno(status);
1283 				goto bail;
1284 			}
1285 
1286 			brelse(bh);
1287 			bh = NULL;
1288 		}
1289 
1290 		v_blkno += p_blocks;
1291 	}
1292 
1293 bail:
1294 	return status;
1295 }
1296 
1297 struct ocfs2_la_recovery_item {
1298 	struct list_head	lri_list;
1299 	int			lri_slot;
1300 	struct ocfs2_dinode	*lri_la_dinode;
1301 	struct ocfs2_dinode	*lri_tl_dinode;
1302 	struct ocfs2_quota_recovery *lri_qrec;
1303 	enum ocfs2_orphan_reco_type  lri_orphan_reco_type;
1304 };
1305 
1306 /* Does the second half of the recovery process. By this point, the
1307  * node is marked clean and can actually be considered recovered,
1308  * hence it's no longer in the recovery map, but there's still some
1309  * cleanup we can do which shouldn't happen within the recovery thread
1310  * as locking in that context becomes very difficult if we are to take
1311  * recovering nodes into account.
1312  *
1313  * NOTE: This function can and will sleep on recovery of other nodes
1314  * during cluster locking, just like any other ocfs2 process.
1315  */
1316 void ocfs2_complete_recovery(struct work_struct *work)
1317 {
1318 	int ret = 0;
1319 	struct ocfs2_journal *journal =
1320 		container_of(work, struct ocfs2_journal, j_recovery_work);
1321 	struct ocfs2_super *osb = journal->j_osb;
1322 	struct ocfs2_dinode *la_dinode, *tl_dinode;
1323 	struct ocfs2_la_recovery_item *item, *n;
1324 	struct ocfs2_quota_recovery *qrec;
1325 	enum ocfs2_orphan_reco_type orphan_reco_type;
1326 	LIST_HEAD(tmp_la_list);
1327 
1328 	trace_ocfs2_complete_recovery(
1329 		(unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno);
1330 
1331 	spin_lock(&journal->j_lock);
1332 	list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1333 	spin_unlock(&journal->j_lock);
1334 
1335 	list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
1336 		list_del_init(&item->lri_list);
1337 
1338 		ocfs2_wait_on_quotas(osb);
1339 
1340 		la_dinode = item->lri_la_dinode;
1341 		tl_dinode = item->lri_tl_dinode;
1342 		qrec = item->lri_qrec;
1343 		orphan_reco_type = item->lri_orphan_reco_type;
1344 
1345 		trace_ocfs2_complete_recovery_slot(item->lri_slot,
1346 			la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0,
1347 			tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0,
1348 			qrec);
1349 
1350 		if (la_dinode) {
1351 			ret = ocfs2_complete_local_alloc_recovery(osb,
1352 								  la_dinode);
1353 			if (ret < 0)
1354 				mlog_errno(ret);
1355 
1356 			kfree(la_dinode);
1357 		}
1358 
1359 		if (tl_dinode) {
1360 			ret = ocfs2_complete_truncate_log_recovery(osb,
1361 								   tl_dinode);
1362 			if (ret < 0)
1363 				mlog_errno(ret);
1364 
1365 			kfree(tl_dinode);
1366 		}
1367 
1368 		ret = ocfs2_recover_orphans(osb, item->lri_slot,
1369 				orphan_reco_type);
1370 		if (ret < 0)
1371 			mlog_errno(ret);
1372 
1373 		if (qrec) {
1374 			ret = ocfs2_finish_quota_recovery(osb, qrec,
1375 							  item->lri_slot);
1376 			if (ret < 0)
1377 				mlog_errno(ret);
1378 			/* Recovery info is already freed now */
1379 		}
1380 
1381 		kfree(item);
1382 	}
1383 
1384 	trace_ocfs2_complete_recovery_end(ret);
1385 }
1386 
1387 /* NOTE: This function always eats your references to la_dinode and
1388  * tl_dinode, either manually on error, or by passing them to
1389  * ocfs2_complete_recovery */
1390 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1391 					    int slot_num,
1392 					    struct ocfs2_dinode *la_dinode,
1393 					    struct ocfs2_dinode *tl_dinode,
1394 					    struct ocfs2_quota_recovery *qrec,
1395 					    enum ocfs2_orphan_reco_type orphan_reco_type)
1396 {
1397 	struct ocfs2_la_recovery_item *item;
1398 
1399 	item = kmalloc_obj(struct ocfs2_la_recovery_item, GFP_NOFS);
1400 	if (!item) {
1401 		/* Though we wish to avoid it, we are in fact safe in
1402 		 * skipping local alloc cleanup as fsck.ocfs2 is more
1403 		 * than capable of reclaiming unused space. */
1404 		kfree(la_dinode);
1405 		kfree(tl_dinode);
1406 
1407 		if (qrec)
1408 			ocfs2_free_quota_recovery(qrec);
1409 
1410 		mlog_errno(-ENOMEM);
1411 		return;
1412 	}
1413 
1414 	INIT_LIST_HEAD(&item->lri_list);
1415 	item->lri_la_dinode = la_dinode;
1416 	item->lri_slot = slot_num;
1417 	item->lri_tl_dinode = tl_dinode;
1418 	item->lri_qrec = qrec;
1419 	item->lri_orphan_reco_type = orphan_reco_type;
1420 
1421 	spin_lock(&journal->j_lock);
1422 	list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1423 	queue_work(journal->j_osb->ocfs2_wq, &journal->j_recovery_work);
1424 	spin_unlock(&journal->j_lock);
1425 }
1426 
1427 /* Called by the mount code to queue recovery the last part of
1428  * recovery for it's own and offline slot(s). */
1429 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1430 {
1431 	struct ocfs2_journal *journal = osb->journal;
1432 
1433 	if (ocfs2_is_hard_readonly(osb))
1434 		return;
1435 
1436 	/* No need to queue up our truncate_log as regular cleanup will catch
1437 	 * that */
1438 	ocfs2_queue_recovery_completion(journal, osb->slot_num,
1439 					osb->local_alloc_copy, NULL, NULL,
1440 					ORPHAN_NEED_TRUNCATE);
1441 	ocfs2_schedule_truncate_log_flush(osb, 0);
1442 
1443 	osb->local_alloc_copy = NULL;
1444 
1445 	/* queue to recover orphan slots for all offline slots */
1446 	ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1447 	ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
1448 	ocfs2_free_replay_slots(osb);
1449 }
1450 
1451 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1452 {
1453 	if (osb->quota_rec) {
1454 		ocfs2_queue_recovery_completion(osb->journal,
1455 						osb->slot_num,
1456 						NULL,
1457 						NULL,
1458 						osb->quota_rec,
1459 						ORPHAN_NEED_TRUNCATE);
1460 		osb->quota_rec = NULL;
1461 	}
1462 }
1463 
1464 static int __ocfs2_recovery_thread(void *arg)
1465 {
1466 	int status, node_num, slot_num;
1467 	struct ocfs2_super *osb = arg;
1468 	struct ocfs2_recovery_map *rm = osb->recovery_map;
1469 	int *rm_quota = NULL;
1470 	int rm_quota_used = 0, i;
1471 	struct ocfs2_quota_recovery *qrec;
1472 
1473 	/* Whether the quota supported. */
1474 	int quota_enabled = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
1475 			OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
1476 		|| OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
1477 			OCFS2_FEATURE_RO_COMPAT_GRPQUOTA);
1478 
1479 	status = ocfs2_wait_on_mount(osb);
1480 	if (status < 0) {
1481 		goto bail;
1482 	}
1483 
1484 	if (quota_enabled) {
1485 		rm_quota = kzalloc_objs(int, osb->max_slots, GFP_NOFS);
1486 		if (!rm_quota) {
1487 			status = -ENOMEM;
1488 			goto bail;
1489 		}
1490 	}
1491 restart:
1492 	if (quota_enabled) {
1493 		mutex_lock(&osb->recovery_lock);
1494 		/* Confirm that recovery thread will no longer recover quotas */
1495 		if (osb->recovery_state == OCFS2_REC_QUOTA_WANT_DISABLE) {
1496 			osb->recovery_state = OCFS2_REC_QUOTA_DISABLED;
1497 			wake_up(&osb->recovery_event);
1498 		}
1499 		if (osb->recovery_state >= OCFS2_REC_QUOTA_DISABLED)
1500 			quota_enabled = 0;
1501 		mutex_unlock(&osb->recovery_lock);
1502 	}
1503 
1504 	status = ocfs2_super_lock(osb, 1);
1505 	if (status < 0) {
1506 		mlog_errno(status);
1507 		goto bail;
1508 	}
1509 
1510 	status = ocfs2_compute_replay_slots(osb);
1511 	if (status < 0)
1512 		mlog_errno(status);
1513 
1514 	/* queue recovery for our own slot */
1515 	ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1516 					NULL, NULL, ORPHAN_NO_NEED_TRUNCATE);
1517 
1518 	spin_lock(&osb->osb_lock);
1519 	while (rm->rm_used) {
1520 		/* It's always safe to remove entry zero, as we won't
1521 		 * clear it until ocfs2_recover_node() has succeeded. */
1522 		node_num = rm->rm_entries[0];
1523 		spin_unlock(&osb->osb_lock);
1524 		slot_num = ocfs2_node_num_to_slot(osb, node_num);
1525 		trace_ocfs2_recovery_thread_node(node_num, slot_num);
1526 		if (slot_num == -ENOENT) {
1527 			status = 0;
1528 			goto skip_recovery;
1529 		}
1530 
1531 		/* It is a bit subtle with quota recovery. We cannot do it
1532 		 * immediately because we have to obtain cluster locks from
1533 		 * quota files and we also don't want to just skip it because
1534 		 * then quota usage would be out of sync until some node takes
1535 		 * the slot. So we remember which nodes need quota recovery
1536 		 * and when everything else is done, we recover quotas. */
1537 		if (quota_enabled) {
1538 			for (i = 0; i < rm_quota_used
1539 					&& rm_quota[i] != slot_num; i++)
1540 				;
1541 
1542 			if (i == rm_quota_used)
1543 				rm_quota[rm_quota_used++] = slot_num;
1544 		}
1545 
1546 		status = ocfs2_recover_node(osb, node_num, slot_num);
1547 skip_recovery:
1548 		if (!status) {
1549 			ocfs2_recovery_map_clear(osb, node_num);
1550 		} else {
1551 			mlog(ML_ERROR,
1552 			     "Error %d recovering node %d on device (%u,%u)!\n",
1553 			     status, node_num,
1554 			     MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1555 			mlog(ML_ERROR, "Volume requires unmount.\n");
1556 		}
1557 
1558 		spin_lock(&osb->osb_lock);
1559 	}
1560 	spin_unlock(&osb->osb_lock);
1561 	trace_ocfs2_recovery_thread_end(status);
1562 
1563 	/* Refresh all journal recovery generations from disk */
1564 	status = ocfs2_check_journals_nolocks(osb);
1565 	status = (status == -EROFS) ? 0 : status;
1566 	if (status < 0)
1567 		mlog_errno(status);
1568 
1569 	/* Now it is right time to recover quotas... We have to do this under
1570 	 * superblock lock so that no one can start using the slot (and crash)
1571 	 * before we recover it */
1572 	if (quota_enabled) {
1573 		for (i = 0; i < rm_quota_used; i++) {
1574 			qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1575 			if (IS_ERR(qrec)) {
1576 				status = PTR_ERR(qrec);
1577 				mlog_errno(status);
1578 				continue;
1579 			}
1580 			ocfs2_queue_recovery_completion(osb->journal,
1581 					rm_quota[i],
1582 					NULL, NULL, qrec,
1583 					ORPHAN_NEED_TRUNCATE);
1584 		}
1585 	}
1586 
1587 	ocfs2_super_unlock(osb, 1);
1588 
1589 	/* queue recovery for offline slots */
1590 	ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
1591 
1592 bail:
1593 	mutex_lock(&osb->recovery_lock);
1594 	if (!status && !ocfs2_recovery_completed(osb)) {
1595 		mutex_unlock(&osb->recovery_lock);
1596 		goto restart;
1597 	}
1598 
1599 	ocfs2_free_replay_slots(osb);
1600 	osb->recovery_thread_task = NULL;
1601 	if (osb->recovery_state == OCFS2_REC_WANT_DISABLE)
1602 		osb->recovery_state = OCFS2_REC_DISABLED;
1603 	wake_up(&osb->recovery_event);
1604 
1605 	mutex_unlock(&osb->recovery_lock);
1606 
1607 	kfree(rm_quota);
1608 
1609 	return status;
1610 }
1611 
1612 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1613 {
1614 	int was_set = -1;
1615 
1616 	mutex_lock(&osb->recovery_lock);
1617 	if (osb->recovery_state < OCFS2_REC_WANT_DISABLE)
1618 		was_set = ocfs2_recovery_map_set(osb, node_num);
1619 
1620 	trace_ocfs2_recovery_thread(node_num, osb->node_num,
1621 		osb->recovery_state, osb->recovery_thread_task, was_set);
1622 
1623 	if (osb->recovery_state >= OCFS2_REC_WANT_DISABLE)
1624 		goto out;
1625 
1626 	if (osb->recovery_thread_task)
1627 		goto out;
1628 
1629 	osb->recovery_thread_task =  kthread_run(__ocfs2_recovery_thread, osb,
1630 			"ocfs2rec-%s", osb->uuid_str);
1631 	if (IS_ERR(osb->recovery_thread_task)) {
1632 		mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1633 		osb->recovery_thread_task = NULL;
1634 	}
1635 
1636 out:
1637 	mutex_unlock(&osb->recovery_lock);
1638 	wake_up(&osb->recovery_event);
1639 }
1640 
1641 static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1642 				    int slot_num,
1643 				    struct buffer_head **bh,
1644 				    struct inode **ret_inode)
1645 {
1646 	int status = -EACCES;
1647 	struct inode *inode = NULL;
1648 
1649 	BUG_ON(slot_num >= osb->max_slots);
1650 
1651 	inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1652 					    slot_num);
1653 	if (!inode || is_bad_inode(inode)) {
1654 		mlog_errno(status);
1655 		goto bail;
1656 	}
1657 	SET_INODE_JOURNAL(inode);
1658 
1659 	status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
1660 	if (status < 0) {
1661 		mlog_errno(status);
1662 		goto bail;
1663 	}
1664 
1665 	status = 0;
1666 
1667 bail:
1668 	if (inode) {
1669 		if (status || !ret_inode)
1670 			iput(inode);
1671 		else
1672 			*ret_inode = inode;
1673 	}
1674 	return status;
1675 }
1676 
1677 /* Does the actual journal replay and marks the journal inode as
1678  * clean. Will only replay if the journal inode is marked dirty. */
1679 static int ocfs2_replay_journal(struct ocfs2_super *osb,
1680 				int node_num,
1681 				int slot_num)
1682 {
1683 	int status;
1684 	int got_lock = 0;
1685 	unsigned int flags;
1686 	struct inode *inode = NULL;
1687 	struct ocfs2_dinode *fe;
1688 	journal_t *journal = NULL;
1689 	struct buffer_head *bh = NULL;
1690 	u32 slot_reco_gen;
1691 
1692 	status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1693 	if (status) {
1694 		mlog_errno(status);
1695 		goto done;
1696 	}
1697 
1698 	fe = (struct ocfs2_dinode *)bh->b_data;
1699 	slot_reco_gen = ocfs2_get_recovery_generation(fe);
1700 	brelse(bh);
1701 	bh = NULL;
1702 
1703 	/*
1704 	 * As the fs recovery is asynchronous, there is a small chance that
1705 	 * another node mounted (and recovered) the slot before the recovery
1706 	 * thread could get the lock. To handle that, we dirty read the journal
1707 	 * inode for that slot to get the recovery generation. If it is
1708 	 * different than what we expected, the slot has been recovered.
1709 	 * If not, it needs recovery.
1710 	 */
1711 	if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1712 		trace_ocfs2_replay_journal_recovered(slot_num,
1713 		     osb->slot_recovery_generations[slot_num], slot_reco_gen);
1714 		osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1715 		status = -EBUSY;
1716 		goto done;
1717 	}
1718 
1719 	/* Continue with recovery as the journal has not yet been recovered */
1720 
1721 	status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
1722 	if (status < 0) {
1723 		trace_ocfs2_replay_journal_lock_err(status);
1724 		if (status != -ERESTARTSYS)
1725 			mlog(ML_ERROR, "Could not lock journal!\n");
1726 		goto done;
1727 	}
1728 	got_lock = 1;
1729 
1730 	fe = (struct ocfs2_dinode *) bh->b_data;
1731 
1732 	flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1733 	slot_reco_gen = ocfs2_get_recovery_generation(fe);
1734 
1735 	if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1736 		trace_ocfs2_replay_journal_skip(node_num);
1737 		/* Refresh recovery generation for the slot */
1738 		osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1739 		goto done;
1740 	}
1741 
1742 	/* we need to run complete recovery for offline orphan slots */
1743 	ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1744 
1745 	printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\
1746 	       "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
1747 	       MINOR(osb->sb->s_dev));
1748 
1749 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1750 
1751 	status = ocfs2_force_read_journal(inode);
1752 	if (status < 0) {
1753 		mlog_errno(status);
1754 		goto done;
1755 	}
1756 
1757 	journal = jbd2_journal_init_inode(inode);
1758 	if (IS_ERR(journal)) {
1759 		mlog(ML_ERROR, "Linux journal layer error\n");
1760 		status = PTR_ERR(journal);
1761 		goto done;
1762 	}
1763 
1764 	status = jbd2_journal_load(journal);
1765 	if (status < 0) {
1766 		mlog_errno(status);
1767 		BUG_ON(!igrab(inode));
1768 		jbd2_journal_destroy(journal);
1769 		goto done;
1770 	}
1771 
1772 	ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1773 
1774 	/* wipe the journal */
1775 	jbd2_journal_lock_updates(journal);
1776 	status = jbd2_journal_flush(journal, 0);
1777 	jbd2_journal_unlock_updates(journal);
1778 	if (status < 0)
1779 		mlog_errno(status);
1780 
1781 	/* This will mark the node clean */
1782 	flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1783 	flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1784 	fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1785 
1786 	/* Increment recovery generation to indicate successful recovery */
1787 	ocfs2_bump_recovery_generation(fe);
1788 	osb->slot_recovery_generations[slot_num] =
1789 					ocfs2_get_recovery_generation(fe);
1790 
1791 	ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1792 	status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
1793 	if (status < 0)
1794 		mlog_errno(status);
1795 
1796 	BUG_ON(!igrab(inode));
1797 
1798 	jbd2_journal_destroy(journal);
1799 
1800 	printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\
1801 	       "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
1802 	       MINOR(osb->sb->s_dev));
1803 done:
1804 	/* drop the lock on this nodes journal */
1805 	if (got_lock)
1806 		ocfs2_inode_unlock(inode, 1);
1807 
1808 	iput(inode);
1809 	brelse(bh);
1810 
1811 	return status;
1812 }
1813 
1814 /*
1815  * Do the most important parts of node recovery:
1816  *  - Replay it's journal
1817  *  - Stamp a clean local allocator file
1818  *  - Stamp a clean truncate log
1819  *  - Mark the node clean
1820  *
1821  * If this function completes without error, a node in OCFS2 can be
1822  * said to have been safely recovered. As a result, failure during the
1823  * second part of a nodes recovery process (local alloc recovery) is
1824  * far less concerning.
1825  */
1826 static int ocfs2_recover_node(struct ocfs2_super *osb,
1827 			      int node_num, int slot_num)
1828 {
1829 	int status = 0;
1830 	struct ocfs2_dinode *la_copy = NULL;
1831 	struct ocfs2_dinode *tl_copy = NULL;
1832 
1833 	trace_ocfs2_recover_node(node_num, slot_num, osb->node_num);
1834 
1835 	/* Should not ever be called to recover ourselves -- in that
1836 	 * case we should've called ocfs2_journal_load instead. */
1837 	BUG_ON(osb->node_num == node_num);
1838 
1839 	status = ocfs2_replay_journal(osb, node_num, slot_num);
1840 	if (status < 0) {
1841 		if (status == -EBUSY) {
1842 			trace_ocfs2_recover_node_skip(slot_num, node_num);
1843 			status = 0;
1844 			goto done;
1845 		}
1846 		mlog_errno(status);
1847 		goto done;
1848 	}
1849 
1850 	/* Stamp a clean local alloc file AFTER recovering the journal... */
1851 	status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1852 	if (status < 0) {
1853 		mlog_errno(status);
1854 		goto done;
1855 	}
1856 
1857 	/* An error from begin_truncate_log_recovery is not
1858 	 * serious enough to warrant halting the rest of
1859 	 * recovery. */
1860 	status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1861 	if (status < 0)
1862 		mlog_errno(status);
1863 
1864 	/* Likewise, this would be a strange but ultimately not so
1865 	 * harmful place to get an error... */
1866 	status = ocfs2_clear_slot(osb, slot_num);
1867 	if (status < 0)
1868 		mlog_errno(status);
1869 
1870 	/* This will kfree the memory pointed to by la_copy and tl_copy */
1871 	ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1872 					tl_copy, NULL, ORPHAN_NEED_TRUNCATE);
1873 
1874 	status = 0;
1875 done:
1876 
1877 	return status;
1878 }
1879 
1880 /* Test node liveness by trylocking his journal. If we get the lock,
1881  * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1882  * still alive (we couldn't get the lock) and < 0 on error. */
1883 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1884 				 int slot_num)
1885 {
1886 	int status, flags;
1887 	struct inode *inode = NULL;
1888 
1889 	inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1890 					    slot_num);
1891 	if (inode == NULL) {
1892 		mlog(ML_ERROR, "access error\n");
1893 		status = -EACCES;
1894 		goto bail;
1895 	}
1896 	if (is_bad_inode(inode)) {
1897 		mlog(ML_ERROR, "access error (bad inode)\n");
1898 		iput(inode);
1899 		inode = NULL;
1900 		status = -EACCES;
1901 		goto bail;
1902 	}
1903 	SET_INODE_JOURNAL(inode);
1904 
1905 	flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
1906 	status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1907 	if (status < 0) {
1908 		if (status != -EAGAIN)
1909 			mlog_errno(status);
1910 		goto bail;
1911 	}
1912 
1913 	ocfs2_inode_unlock(inode, 1);
1914 bail:
1915 	iput(inode);
1916 
1917 	return status;
1918 }
1919 
1920 /* Call this underneath ocfs2_super_lock. It also assumes that the
1921  * slot info struct has been updated from disk. */
1922 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1923 {
1924 	unsigned int node_num;
1925 	int status, i;
1926 	u32 gen;
1927 	struct buffer_head *bh = NULL;
1928 	struct ocfs2_dinode *di;
1929 
1930 	/* This is called with the super block cluster lock, so we
1931 	 * know that the slot map can't change underneath us. */
1932 
1933 	for (i = 0; i < osb->max_slots; i++) {
1934 		/* Read journal inode to get the recovery generation */
1935 		status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1936 		if (status) {
1937 			mlog_errno(status);
1938 			goto bail;
1939 		}
1940 		di = (struct ocfs2_dinode *)bh->b_data;
1941 		gen = ocfs2_get_recovery_generation(di);
1942 		brelse(bh);
1943 		bh = NULL;
1944 
1945 		spin_lock(&osb->osb_lock);
1946 		osb->slot_recovery_generations[i] = gen;
1947 
1948 		trace_ocfs2_mark_dead_nodes(i,
1949 					    osb->slot_recovery_generations[i]);
1950 
1951 		if (i == osb->slot_num) {
1952 			spin_unlock(&osb->osb_lock);
1953 			continue;
1954 		}
1955 
1956 		status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1957 		if (status == -ENOENT) {
1958 			spin_unlock(&osb->osb_lock);
1959 			continue;
1960 		}
1961 
1962 		if (__ocfs2_recovery_map_test(osb, node_num)) {
1963 			spin_unlock(&osb->osb_lock);
1964 			continue;
1965 		}
1966 		spin_unlock(&osb->osb_lock);
1967 
1968 		/* Ok, we have a slot occupied by another node which
1969 		 * is not in the recovery map. We trylock his journal
1970 		 * file here to test if he's alive. */
1971 		status = ocfs2_trylock_journal(osb, i);
1972 		if (!status) {
1973 			/* Since we're called from mount, we know that
1974 			 * the recovery thread can't race us on
1975 			 * setting / checking the recovery bits. */
1976 			ocfs2_recovery_thread(osb, node_num);
1977 		} else if ((status < 0) && (status != -EAGAIN)) {
1978 			mlog_errno(status);
1979 			goto bail;
1980 		}
1981 	}
1982 
1983 	status = 0;
1984 bail:
1985 	return status;
1986 }
1987 
1988 /*
1989  * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some
1990  * randomness to the timeout to minimize multiple nodes firing the timer at the
1991  * same time.
1992  */
1993 static inline unsigned long ocfs2_orphan_scan_timeout(void)
1994 {
1995 	unsigned long time;
1996 
1997 	get_random_bytes(&time, sizeof(time));
1998 	time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000);
1999 	return msecs_to_jiffies(time);
2000 }
2001 
2002 /*
2003  * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for
2004  * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This
2005  * is done to catch any orphans that are left over in orphan directories.
2006  *
2007  * It scans all slots, even ones that are in use. It does so to handle the
2008  * case described below:
2009  *
2010  *   Node 1 has an inode it was using. The dentry went away due to memory
2011  *   pressure.  Node 1 closes the inode, but it's on the free list. The node
2012  *   has the open lock.
2013  *   Node 2 unlinks the inode. It grabs the dentry lock to notify others,
2014  *   but node 1 has no dentry and doesn't get the message. It trylocks the
2015  *   open lock, sees that another node has a PR, and does nothing.
2016  *   Later node 2 runs its orphan dir. It igets the inode, trylocks the
2017  *   open lock, sees the PR still, and does nothing.
2018  *   Basically, we have to trigger an orphan iput on node 1. The only way
2019  *   for this to happen is if node 1 runs node 2's orphan dir.
2020  *
2021  * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT
2022  * seconds.  It gets an EX lock on os_lockres and checks sequence number
2023  * stored in LVB. If the sequence number has changed, it means some other
2024  * node has done the scan.  This node skips the scan and tracks the
2025  * sequence number.  If the sequence number didn't change, it means a scan
2026  * hasn't happened.  The node queues a scan and increments the
2027  * sequence number in the LVB.
2028  */
2029 static void ocfs2_queue_orphan_scan(struct ocfs2_super *osb)
2030 {
2031 	struct ocfs2_orphan_scan *os;
2032 	int status, i;
2033 	u32 seqno = 0;
2034 
2035 	os = &osb->osb_orphan_scan;
2036 
2037 	if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
2038 		goto out;
2039 
2040 	trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno,
2041 					    atomic_read(&os->os_state));
2042 
2043 	status = ocfs2_orphan_scan_lock(osb, &seqno);
2044 	if (status < 0) {
2045 		if (status != -EAGAIN)
2046 			mlog_errno(status);
2047 		goto out;
2048 	}
2049 
2050 	/* Do no queue the tasks if the volume is being umounted */
2051 	if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
2052 		goto unlock;
2053 
2054 	if (os->os_seqno != seqno) {
2055 		os->os_seqno = seqno;
2056 		goto unlock;
2057 	}
2058 
2059 	for (i = 0; i < osb->max_slots; i++)
2060 		ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL,
2061 						NULL, ORPHAN_NO_NEED_TRUNCATE);
2062 	/*
2063 	 * We queued a recovery on orphan slots, increment the sequence
2064 	 * number and update LVB so other node will skip the scan for a while
2065 	 */
2066 	seqno++;
2067 	os->os_count++;
2068 	os->os_scantime = ktime_get_seconds();
2069 unlock:
2070 	ocfs2_orphan_scan_unlock(osb, seqno);
2071 out:
2072 	trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno,
2073 					  atomic_read(&os->os_state));
2074 	return;
2075 }
2076 
2077 /* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */
2078 static void ocfs2_orphan_scan_work(struct work_struct *work)
2079 {
2080 	struct ocfs2_orphan_scan *os;
2081 	struct ocfs2_super *osb;
2082 
2083 	os = container_of(work, struct ocfs2_orphan_scan,
2084 			  os_orphan_scan_work.work);
2085 	osb = os->os_osb;
2086 
2087 	mutex_lock(&os->os_lock);
2088 	ocfs2_queue_orphan_scan(osb);
2089 	if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE)
2090 		queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
2091 				      ocfs2_orphan_scan_timeout());
2092 	mutex_unlock(&os->os_lock);
2093 }
2094 
2095 void ocfs2_orphan_scan_stop(struct ocfs2_super *osb)
2096 {
2097 	struct ocfs2_orphan_scan *os;
2098 
2099 	os = &osb->osb_orphan_scan;
2100 	if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) {
2101 		atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
2102 		mutex_lock(&os->os_lock);
2103 		cancel_delayed_work(&os->os_orphan_scan_work);
2104 		mutex_unlock(&os->os_lock);
2105 	}
2106 }
2107 
2108 void ocfs2_orphan_scan_init(struct ocfs2_super *osb)
2109 {
2110 	struct ocfs2_orphan_scan *os;
2111 
2112 	os = &osb->osb_orphan_scan;
2113 	os->os_osb = osb;
2114 	os->os_count = 0;
2115 	os->os_seqno = 0;
2116 	mutex_init(&os->os_lock);
2117 	INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work);
2118 }
2119 
2120 void ocfs2_orphan_scan_start(struct ocfs2_super *osb)
2121 {
2122 	struct ocfs2_orphan_scan *os;
2123 
2124 	os = &osb->osb_orphan_scan;
2125 	os->os_scantime = ktime_get_seconds();
2126 	if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
2127 		atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
2128 	else {
2129 		atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE);
2130 		queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
2131 				   ocfs2_orphan_scan_timeout());
2132 	}
2133 }
2134 
2135 struct ocfs2_orphan_filldir_priv {
2136 	struct dir_context	ctx;
2137 	struct inode		*head;
2138 	struct ocfs2_super	*osb;
2139 	enum ocfs2_orphan_reco_type orphan_reco_type;
2140 };
2141 
2142 static bool ocfs2_orphan_filldir(struct dir_context *ctx, const char *name,
2143 				int name_len, loff_t pos, u64 ino,
2144 				unsigned type)
2145 {
2146 	struct ocfs2_orphan_filldir_priv *p =
2147 		container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx);
2148 	struct inode *iter;
2149 
2150 	if (name_len == 1 && !strncmp(".", name, 1))
2151 		return true;
2152 	if (name_len == 2 && !strncmp("..", name, 2))
2153 		return true;
2154 
2155 	/* do not include dio entry in case of orphan scan */
2156 	if ((p->orphan_reco_type == ORPHAN_NO_NEED_TRUNCATE) &&
2157 			(!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
2158 			OCFS2_DIO_ORPHAN_PREFIX_LEN)))
2159 		return true;
2160 
2161 	/* Skip bad inodes so that recovery can continue */
2162 	iter = ocfs2_iget(p->osb, ino,
2163 			  OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
2164 	if (IS_ERR(iter))
2165 		return true;
2166 
2167 	if (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
2168 			OCFS2_DIO_ORPHAN_PREFIX_LEN))
2169 		OCFS2_I(iter)->ip_flags |= OCFS2_INODE_DIO_ORPHAN_ENTRY;
2170 
2171 	/* Skip inodes which are already added to recover list, since dio may
2172 	 * happen concurrently with unlink/rename */
2173 	if (OCFS2_I(iter)->ip_next_orphan) {
2174 		iput(iter);
2175 		return true;
2176 	}
2177 
2178 	trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno);
2179 	/* No locking is required for the next_orphan queue as there
2180 	 * is only ever a single process doing orphan recovery. */
2181 	OCFS2_I(iter)->ip_next_orphan = p->head;
2182 	p->head = iter;
2183 
2184 	return true;
2185 }
2186 
2187 static int ocfs2_queue_orphans(struct ocfs2_super *osb,
2188 			       int slot,
2189 			       struct inode **head,
2190 			       enum ocfs2_orphan_reco_type orphan_reco_type)
2191 {
2192 	int status;
2193 	struct inode *orphan_dir_inode = NULL;
2194 	struct ocfs2_orphan_filldir_priv priv = {
2195 		.ctx.actor = ocfs2_orphan_filldir,
2196 		.osb = osb,
2197 		.head = *head,
2198 		.orphan_reco_type = orphan_reco_type
2199 	};
2200 
2201 	orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2202 						       ORPHAN_DIR_SYSTEM_INODE,
2203 						       slot);
2204 	if  (!orphan_dir_inode) {
2205 		status = -ENOENT;
2206 		mlog_errno(status);
2207 		return status;
2208 	}
2209 
2210 	inode_lock(orphan_dir_inode);
2211 	status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
2212 	if (status < 0) {
2213 		mlog_errno(status);
2214 		goto out;
2215 	}
2216 
2217 	status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx);
2218 	if (status) {
2219 		mlog_errno(status);
2220 		goto out_cluster;
2221 	}
2222 
2223 	*head = priv.head;
2224 
2225 out_cluster:
2226 	ocfs2_inode_unlock(orphan_dir_inode, 0);
2227 out:
2228 	inode_unlock(orphan_dir_inode);
2229 	iput(orphan_dir_inode);
2230 	return status;
2231 }
2232 
2233 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
2234 					      int slot)
2235 {
2236 	int ret;
2237 
2238 	spin_lock(&osb->osb_lock);
2239 	ret = !osb->osb_orphan_wipes[slot];
2240 	spin_unlock(&osb->osb_lock);
2241 	return ret;
2242 }
2243 
2244 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
2245 					     int slot)
2246 {
2247 	spin_lock(&osb->osb_lock);
2248 	/* Mark ourselves such that new processes in delete_inode()
2249 	 * know to quit early. */
2250 	ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
2251 	while (osb->osb_orphan_wipes[slot]) {
2252 		/* If any processes are already in the middle of an
2253 		 * orphan wipe on this dir, then we need to wait for
2254 		 * them. */
2255 		spin_unlock(&osb->osb_lock);
2256 		wait_event_interruptible(osb->osb_wipe_event,
2257 					 ocfs2_orphan_recovery_can_continue(osb, slot));
2258 		spin_lock(&osb->osb_lock);
2259 	}
2260 	spin_unlock(&osb->osb_lock);
2261 }
2262 
2263 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
2264 					      int slot)
2265 {
2266 	ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
2267 }
2268 
2269 /*
2270  * Orphan recovery. Each mounted node has it's own orphan dir which we
2271  * must run during recovery. Our strategy here is to build a list of
2272  * the inodes in the orphan dir and iget/iput them. The VFS does
2273  * (most) of the rest of the work.
2274  *
2275  * Orphan recovery can happen at any time, not just mount so we have a
2276  * couple of extra considerations.
2277  *
2278  * - We grab as many inodes as we can under the orphan dir lock -
2279  *   doing iget() outside the orphan dir risks getting a reference on
2280  *   an invalid inode.
2281  * - We must be sure not to deadlock with other processes on the
2282  *   system wanting to run delete_inode(). This can happen when they go
2283  *   to lock the orphan dir and the orphan recovery process attempts to
2284  *   iget() inside the orphan dir lock. This can be avoided by
2285  *   advertising our state to ocfs2_delete_inode().
2286  */
2287 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
2288 				 int slot,
2289 				 enum ocfs2_orphan_reco_type orphan_reco_type)
2290 {
2291 	int ret = 0;
2292 	struct inode *inode = NULL;
2293 	struct inode *iter;
2294 	struct ocfs2_inode_info *oi;
2295 	struct buffer_head *di_bh = NULL;
2296 	struct ocfs2_dinode *di = NULL;
2297 
2298 	trace_ocfs2_recover_orphans(slot);
2299 
2300 	ocfs2_mark_recovering_orphan_dir(osb, slot);
2301 	ret = ocfs2_queue_orphans(osb, slot, &inode, orphan_reco_type);
2302 	ocfs2_clear_recovering_orphan_dir(osb, slot);
2303 
2304 	/* Error here should be noted, but we want to continue with as
2305 	 * many queued inodes as we've got. */
2306 	if (ret)
2307 		mlog_errno(ret);
2308 
2309 	while (inode) {
2310 		oi = OCFS2_I(inode);
2311 		trace_ocfs2_recover_orphans_iput(
2312 					(unsigned long long)oi->ip_blkno);
2313 
2314 		iter = oi->ip_next_orphan;
2315 		oi->ip_next_orphan = NULL;
2316 
2317 		if (oi->ip_flags & OCFS2_INODE_DIO_ORPHAN_ENTRY) {
2318 			inode_lock(inode);
2319 			ret = ocfs2_rw_lock(inode, 1);
2320 			if (ret < 0) {
2321 				mlog_errno(ret);
2322 				goto unlock_mutex;
2323 			}
2324 			/*
2325 			 * We need to take and drop the inode lock to
2326 			 * force read inode from disk.
2327 			 */
2328 			ret = ocfs2_inode_lock(inode, &di_bh, 1);
2329 			if (ret) {
2330 				mlog_errno(ret);
2331 				goto unlock_rw;
2332 			}
2333 
2334 			di = (struct ocfs2_dinode *)di_bh->b_data;
2335 
2336 			if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) {
2337 				ret = ocfs2_truncate_file(inode, di_bh,
2338 						i_size_read(inode));
2339 				if (ret < 0) {
2340 					if (ret != -ENOSPC)
2341 						mlog_errno(ret);
2342 					goto unlock_inode;
2343 				}
2344 
2345 				ret = ocfs2_del_inode_from_orphan(osb, inode,
2346 						di_bh, 0, 0);
2347 				if (ret)
2348 					mlog_errno(ret);
2349 			}
2350 unlock_inode:
2351 			ocfs2_inode_unlock(inode, 1);
2352 			brelse(di_bh);
2353 			di_bh = NULL;
2354 unlock_rw:
2355 			ocfs2_rw_unlock(inode, 1);
2356 unlock_mutex:
2357 			inode_unlock(inode);
2358 
2359 			/* clear dio flag in ocfs2_inode_info */
2360 			oi->ip_flags &= ~OCFS2_INODE_DIO_ORPHAN_ENTRY;
2361 		} else {
2362 			spin_lock(&oi->ip_lock);
2363 			/* Set the proper information to get us going into
2364 			 * ocfs2_delete_inode. */
2365 			oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2366 			spin_unlock(&oi->ip_lock);
2367 		}
2368 
2369 		iput(inode);
2370 		inode = iter;
2371 	}
2372 
2373 	return ret;
2374 }
2375 
2376 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
2377 {
2378 	/* This check is good because ocfs2 will wait on our recovery
2379 	 * thread before changing it to something other than MOUNTED
2380 	 * or DISABLED. */
2381 	wait_event(osb->osb_mount_event,
2382 		  (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
2383 		   atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
2384 		   atomic_read(&osb->vol_state) == VOLUME_DISABLED);
2385 
2386 	/* If there's an error on mount, then we may never get to the
2387 	 * MOUNTED flag, but this is set right before
2388 	 * dismount_volume() so we can trust it. */
2389 	if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
2390 		trace_ocfs2_wait_on_mount(VOLUME_DISABLED);
2391 		mlog(0, "mount error, exiting!\n");
2392 		return -EBUSY;
2393 	}
2394 
2395 	return 0;
2396 }
2397 
2398 static int ocfs2_commit_thread(void *arg)
2399 {
2400 	int status;
2401 	struct ocfs2_super *osb = arg;
2402 	struct ocfs2_journal *journal = osb->journal;
2403 
2404 	/* we can trust j_num_trans here because _should_stop() is only set in
2405 	 * shutdown and nobody other than ourselves should be able to start
2406 	 * transactions.  committing on shutdown might take a few iterations
2407 	 * as final transactions put deleted inodes on the list */
2408 	while (!(kthread_should_stop() &&
2409 		 atomic_read(&journal->j_num_trans) == 0)) {
2410 
2411 		wait_event_interruptible(osb->checkpoint_event,
2412 					 atomic_read(&journal->j_num_trans)
2413 					 || kthread_should_stop());
2414 
2415 		status = ocfs2_commit_cache(osb);
2416 		if (status < 0) {
2417 			static unsigned long abort_warn_time;
2418 
2419 			/* Warn about this once per minute */
2420 			if (printk_timed_ratelimit(&abort_warn_time, 60*HZ))
2421 				mlog(ML_ERROR, "status = %d, journal is "
2422 						"already aborted.\n", status);
2423 			/*
2424 			 * After ocfs2_commit_cache() fails, j_num_trans has a
2425 			 * non-zero value.  Sleep here to avoid a busy-wait
2426 			 * loop.
2427 			 */
2428 			msleep_interruptible(1000);
2429 		}
2430 
2431 		if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
2432 			mlog(ML_KTHREAD,
2433 			     "commit_thread: %u transactions pending on "
2434 			     "shutdown\n",
2435 			     atomic_read(&journal->j_num_trans));
2436 		}
2437 	}
2438 
2439 	return 0;
2440 }
2441 
2442 /* Reads all the journal inodes without taking any cluster locks. Used
2443  * for hard readonly access to determine whether any journal requires
2444  * recovery. Also used to refresh the recovery generation numbers after
2445  * a journal has been recovered by another node.
2446  */
2447 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
2448 {
2449 	int ret = 0;
2450 	unsigned int slot;
2451 	struct buffer_head *di_bh = NULL;
2452 	struct ocfs2_dinode *di;
2453 	int journal_dirty = 0;
2454 
2455 	for(slot = 0; slot < osb->max_slots; slot++) {
2456 		ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
2457 		if (ret) {
2458 			mlog_errno(ret);
2459 			goto out;
2460 		}
2461 
2462 		di = (struct ocfs2_dinode *) di_bh->b_data;
2463 
2464 		osb->slot_recovery_generations[slot] =
2465 					ocfs2_get_recovery_generation(di);
2466 
2467 		if (le32_to_cpu(di->id1.journal1.ij_flags) &
2468 		    OCFS2_JOURNAL_DIRTY_FL)
2469 			journal_dirty = 1;
2470 
2471 		brelse(di_bh);
2472 		di_bh = NULL;
2473 	}
2474 
2475 out:
2476 	if (journal_dirty)
2477 		ret = -EROFS;
2478 	return ret;
2479 }
2480