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