File Index
Files and directories
Sign in
FILE

journal.c

File preview with syntax highlighting.

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