journal.c 60.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * journal.c
 *
 * Defines functions of journalling api
 *
 * Copyright (C) 2003, 2004 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/highmem.h>
#include <linux/kthread.h>
31 32
#include <linux/time.h>
#include <linux/random.h>
33
#include <linux/delay.h>
34 35 36 37 38 39

#include <cluster/masklog.h>

#include "ocfs2.h"

#include "alloc.h"
40
#include "blockcheck.h"
41
#include "dir.h"
42 43 44 45 46 47 48 49 50
#include "dlmglue.h"
#include "extent_map.h"
#include "heartbeat.h"
#include "inode.h"
#include "journal.h"
#include "localalloc.h"
#include "slot_map.h"
#include "super.h"
#include "sysfile.h"
51
#include "uptodate.h"
J
Jan Kara 已提交
52
#include "quota.h"
53 54
#include "file.h"
#include "namei.h"
55 56

#include "buffer_head_io.h"
T
Tao Ma 已提交
57
#include "ocfs2_trace.h"
58

I
Ingo Molnar 已提交
59
DEFINE_SPINLOCK(trans_inc_lock);
60

61 62
#define ORPHAN_SCAN_SCHEDULE_TIMEOUT 300000

63 64
static int ocfs2_force_read_journal(struct inode *inode);
static int ocfs2_recover_node(struct ocfs2_super *osb,
J
Jan Kara 已提交
65
			      int node_num, int slot_num);
66 67
static int __ocfs2_recovery_thread(void *arg);
static int ocfs2_commit_cache(struct ocfs2_super *osb);
68
static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
69
static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
70
				      int dirty, int replayed);
71 72 73
static int ocfs2_trylock_journal(struct ocfs2_super *osb,
				 int slot_num);
static int ocfs2_recover_orphans(struct ocfs2_super *osb,
74 75
				 int slot,
				 enum ocfs2_orphan_reco_type orphan_reco_type);
76
static int ocfs2_commit_thread(void *arg);
77 78 79 80
static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
					    int slot_num,
					    struct ocfs2_dinode *la_dinode,
					    struct ocfs2_dinode *tl_dinode,
81 82
					    struct ocfs2_quota_recovery *qrec,
					    enum ocfs2_orphan_reco_type orphan_reco_type);
83

84 85 86 87 88 89 90 91 92 93
static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
{
	return __ocfs2_wait_on_mount(osb, 0);
}

static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
{
	return __ocfs2_wait_on_mount(osb, 1);
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/*
 * This replay_map is to track online/offline slots, so we could recover
 * offline slots during recovery and mount
 */

enum ocfs2_replay_state {
	REPLAY_UNNEEDED = 0,	/* Replay is not needed, so ignore this map */
	REPLAY_NEEDED, 		/* Replay slots marked in rm_replay_slots */
	REPLAY_DONE 		/* Replay was already queued */
};

struct ocfs2_replay_map {
	unsigned int rm_slots;
	enum ocfs2_replay_state rm_state;
	unsigned char rm_replay_slots[0];
};

void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
{
	if (!osb->replay_map)
		return;

	/* If we've already queued the replay, we don't have any more to do */
	if (osb->replay_map->rm_state == REPLAY_DONE)
		return;

	osb->replay_map->rm_state = state;
}

int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
{
	struct ocfs2_replay_map *replay_map;
	int i, node_num;

	/* If replay map is already set, we don't do it again */
	if (osb->replay_map)
		return 0;

	replay_map = kzalloc(sizeof(struct ocfs2_replay_map) +
			     (osb->max_slots * sizeof(char)), GFP_KERNEL);

	if (!replay_map) {
		mlog_errno(-ENOMEM);
		return -ENOMEM;
	}

	spin_lock(&osb->osb_lock);

	replay_map->rm_slots = osb->max_slots;
	replay_map->rm_state = REPLAY_UNNEEDED;

	/* set rm_replay_slots for offline slot(s) */
	for (i = 0; i < replay_map->rm_slots; i++) {
		if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
			replay_map->rm_replay_slots[i] = 1;
	}

	osb->replay_map = replay_map;
	spin_unlock(&osb->osb_lock);
	return 0;
}

156 157
void ocfs2_queue_replay_slots(struct ocfs2_super *osb,
		enum ocfs2_orphan_reco_type orphan_reco_type)
158 159 160 161 162 163 164 165 166 167 168 169 170
{
	struct ocfs2_replay_map *replay_map = osb->replay_map;
	int i;

	if (!replay_map)
		return;

	if (replay_map->rm_state != REPLAY_NEEDED)
		return;

	for (i = 0; i < replay_map->rm_slots; i++)
		if (replay_map->rm_replay_slots[i])
			ocfs2_queue_recovery_completion(osb->journal, i, NULL,
171 172
							NULL, NULL,
							orphan_reco_type);
173 174 175 176 177 178 179 180 181 182 183 184 185 186
	replay_map->rm_state = REPLAY_DONE;
}

void ocfs2_free_replay_slots(struct ocfs2_super *osb)
{
	struct ocfs2_replay_map *replay_map = osb->replay_map;

	if (!osb->replay_map)
		return;

	kfree(replay_map);
	osb->replay_map = NULL;
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
int ocfs2_recovery_init(struct ocfs2_super *osb)
{
	struct ocfs2_recovery_map *rm;

	mutex_init(&osb->recovery_lock);
	osb->disable_recovery = 0;
	osb->recovery_thread_task = NULL;
	init_waitqueue_head(&osb->recovery_event);

	rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
		     osb->max_slots * sizeof(unsigned int),
		     GFP_KERNEL);
	if (!rm) {
		mlog_errno(-ENOMEM);
		return -ENOMEM;
	}

	rm->rm_entries = (unsigned int *)((char *)rm +
					  sizeof(struct ocfs2_recovery_map));
	osb->recovery_map = rm;

	return 0;
}

/* we can't grab the goofy sem lock from inside wait_event, so we use
 * memory barriers to make sure that we'll see the null task before
 * being woken up */
static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
{
	mb();
	return osb->recovery_thread_task != NULL;
}

void ocfs2_recovery_exit(struct ocfs2_super *osb)
{
	struct ocfs2_recovery_map *rm;

	/* disable any new recovery threads and wait for any currently
	 * running ones to exit. Do this before setting the vol_state. */
	mutex_lock(&osb->recovery_lock);
	osb->disable_recovery = 1;
	mutex_unlock(&osb->recovery_lock);
	wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));

	/* At this point, we know that no more recovery threads can be
	 * launched, so wait for any recovery completion work to
	 * complete. */
	flush_workqueue(ocfs2_wq);

	/*
	 * Now that recovery is shut down, and the osb is about to be
	 * freed,  the osb_lock is not taken here.
	 */
	rm = osb->recovery_map;
	/* XXX: Should we bug if there are dirty entries? */

	kfree(rm);
}

static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
				     unsigned int node_num)
{
	int i;
	struct ocfs2_recovery_map *rm = osb->recovery_map;

	assert_spin_locked(&osb->osb_lock);

	for (i = 0; i < rm->rm_used; i++) {
		if (rm->rm_entries[i] == node_num)
			return 1;
	}

	return 0;
}

/* Behaves like test-and-set.  Returns the previous value */
static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
				  unsigned int node_num)
{
	struct ocfs2_recovery_map *rm = osb->recovery_map;

	spin_lock(&osb->osb_lock);
	if (__ocfs2_recovery_map_test(osb, node_num)) {
		spin_unlock(&osb->osb_lock);
		return 1;
	}

	/* XXX: Can this be exploited? Not from o2dlm... */
	BUG_ON(rm->rm_used >= osb->max_slots);

	rm->rm_entries[rm->rm_used] = node_num;
	rm->rm_used++;
	spin_unlock(&osb->osb_lock);

	return 0;
}

static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
				     unsigned int node_num)
{
	int i;
	struct ocfs2_recovery_map *rm = osb->recovery_map;

	spin_lock(&osb->osb_lock);

	for (i = 0; i < rm->rm_used; i++) {
		if (rm->rm_entries[i] == node_num)
			break;
	}

	if (i < rm->rm_used) {
		/* XXX: be careful with the pointer math */
		memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
			(rm->rm_used - i - 1) * sizeof(unsigned int));
		rm->rm_used--;
	}

	spin_unlock(&osb->osb_lock);
}

307 308 309 310 311 312 313 314 315 316 317
static int ocfs2_commit_cache(struct ocfs2_super *osb)
{
	int status = 0;
	unsigned int flushed;
	struct ocfs2_journal *journal = NULL;

	journal = osb->journal;

	/* Flush all pending commits and checkpoint the journal. */
	down_write(&journal->j_trans_barrier);

T
Tao Ma 已提交
318 319 320
	flushed = atomic_read(&journal->j_num_trans);
	trace_ocfs2_commit_cache_begin(flushed);
	if (flushed == 0) {
321 322 323 324
		up_write(&journal->j_trans_barrier);
		goto finally;
	}

J
Joel Becker 已提交
325 326 327
	jbd2_journal_lock_updates(journal->j_journal);
	status = jbd2_journal_flush(journal->j_journal);
	jbd2_journal_unlock_updates(journal->j_journal);
328 329 330 331 332 333
	if (status < 0) {
		up_write(&journal->j_trans_barrier);
		mlog_errno(status);
		goto finally;
	}

334
	ocfs2_inc_trans_id(journal);
335 336 337 338 339

	flushed = atomic_read(&journal->j_num_trans);
	atomic_set(&journal->j_num_trans, 0);
	up_write(&journal->j_trans_barrier);

T
Tao Ma 已提交
340
	trace_ocfs2_commit_cache_end(journal->j_trans_id, flushed);
341

M
Mark Fasheh 已提交
342
	ocfs2_wake_downconvert_thread(osb);
343 344 345 346 347
	wake_up(&journal->j_checkpointed);
finally:
	return status;
}

348
handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
349 350
{
	journal_t *journal = osb->journal->j_journal;
351
	handle_t *handle;
352

353
	BUG_ON(!osb || !osb->journal->j_journal);
354

355 356
	if (ocfs2_is_hard_readonly(osb))
		return ERR_PTR(-EROFS);
357 358 359 360

	BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
	BUG_ON(max_buffs <= 0);

J
Jan Kara 已提交
361 362 363
	/* Nested transaction? Just return the handle... */
	if (journal_current_handle())
		return jbd2_journal_start(journal, max_buffs);
364

365 366
	sb_start_intwrite(osb->sb);

367 368
	down_read(&osb->journal->j_trans_barrier);

J
Joel Becker 已提交
369
	handle = jbd2_journal_start(journal, max_buffs);
370
	if (IS_ERR(handle)) {
371
		up_read(&osb->journal->j_trans_barrier);
372
		sb_end_intwrite(osb->sb);
373

374
		mlog_errno(PTR_ERR(handle));
375 376 377

		if (is_journal_aborted(journal)) {
			ocfs2_abort(osb->sb, "Detected aborted journal");
378
			handle = ERR_PTR(-EROFS);
379
		}
S
Sunil Mushran 已提交
380 381 382 383
	} else {
		if (!ocfs2_mount_local(osb))
			atomic_inc(&(osb->journal->j_num_trans));
	}
384 385 386 387

	return handle;
}

388 389
int ocfs2_commit_trans(struct ocfs2_super *osb,
		       handle_t *handle)
390
{
J
Jan Kara 已提交
391
	int ret, nested;
392
	struct ocfs2_journal *journal = osb->journal;
393 394 395

	BUG_ON(!handle);

J
Jan Kara 已提交
396
	nested = handle->h_ref > 1;
J
Joel Becker 已提交
397
	ret = jbd2_journal_stop(handle);
398 399
	if (ret < 0)
		mlog_errno(ret);
400

401
	if (!nested) {
J
Jan Kara 已提交
402
		up_read(&journal->j_trans_barrier);
403 404
		sb_end_intwrite(osb->sb);
	}
405

406
	return ret;
407 408 409
}

/*
410
 * 'nblocks' is what you want to add to the current transaction.
411
 *
J
Joel Becker 已提交
412
 * This might call jbd2_journal_restart() which will commit dirty buffers
413 414 415 416 417
 * and then restart the transaction. Before calling
 * ocfs2_extend_trans(), any changed blocks should have been
 * dirtied. After calling it, all blocks which need to be changed must
 * go through another set of journal_access/journal_dirty calls.
 *
418 419 420 421 422 423 424 425
 * WARNING: This will not release any semaphores or disk locks taken
 * during the transaction, so make sure they were taken *before*
 * start_trans or we'll have ordering deadlocks.
 *
 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
 * good because transaction ids haven't yet been recorded on the
 * cluster locks associated with this handle.
 */
426
int ocfs2_extend_trans(handle_t *handle, int nblocks)
427
{
428
	int status, old_nblocks;
429 430

	BUG_ON(!handle);
431
	BUG_ON(nblocks < 0);
432

433 434 435 436
	if (!nblocks)
		return 0;

	old_nblocks = handle->h_buffer_credits;
437

T
Tao Ma 已提交
438
	trace_ocfs2_extend_trans(old_nblocks, nblocks);
439

440
#ifdef CONFIG_OCFS2_DEBUG_FS
441 442
	status = 1;
#else
J
Joel Becker 已提交
443
	status = jbd2_journal_extend(handle, nblocks);
444 445 446 447
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}
448
#endif
449 450

	if (status > 0) {
T
Tao Ma 已提交
451
		trace_ocfs2_extend_trans_restart(old_nblocks + nblocks);
452 453
		status = jbd2_journal_restart(handle,
					      old_nblocks + nblocks);
454 455 456 457
		if (status < 0) {
			mlog_errno(status);
			goto bail;
		}
458
	}
459 460 461 462 463 464

	status = 0;
bail:
	return status;
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/*
 * If we have fewer than thresh credits, extend by OCFS2_MAX_TRANS_DATA.
 * If that fails, restart the transaction & regain write access for the
 * buffer head which is used for metadata modifications.
 * Taken from Ext4: extend_or_restart_transaction()
 */
int ocfs2_allocate_extend_trans(handle_t *handle, int thresh)
{
	int status, old_nblks;

	BUG_ON(!handle);

	old_nblks = handle->h_buffer_credits;
	trace_ocfs2_allocate_extend_trans(old_nblks, thresh);

	if (old_nblks < thresh)
		return 0;

	status = jbd2_journal_extend(handle, OCFS2_MAX_TRANS_DATA);
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

	if (status > 0) {
		status = jbd2_journal_restart(handle, OCFS2_MAX_TRANS_DATA);
		if (status < 0)
			mlog_errno(status);
	}

bail:
	return status;
}


500 501 502 503 504 505 506 507 508 509
struct ocfs2_triggers {
	struct jbd2_buffer_trigger_type	ot_triggers;
	int				ot_offset;
};

static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
{
	return container_of(triggers, struct ocfs2_triggers, ot_triggers);
}

510
static void ocfs2_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
				 struct buffer_head *bh,
				 void *data, size_t size)
{
	struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);

	/*
	 * We aren't guaranteed to have the superblock here, so we
	 * must unconditionally compute the ecc data.
	 * __ocfs2_journal_access() will only set the triggers if
	 * metaecc is enabled.
	 */
	ocfs2_block_check_compute(data, size, data + ot->ot_offset);
}

/*
 * Quota blocks have their own trigger because the struct ocfs2_block_check
 * offset depends on the blocksize.
 */
529
static void ocfs2_dq_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
				 struct buffer_head *bh,
				 void *data, size_t size)
{
	struct ocfs2_disk_dqtrailer *dqt =
		ocfs2_block_dqtrailer(size, data);

	/*
	 * We aren't guaranteed to have the superblock here, so we
	 * must unconditionally compute the ecc data.
	 * __ocfs2_journal_access() will only set the triggers if
	 * metaecc is enabled.
	 */
	ocfs2_block_check_compute(data, size, &dqt->dq_check);
}

545 546 547 548
/*
 * Directory blocks also have their own trigger because the
 * struct ocfs2_block_check offset depends on the blocksize.
 */
549
static void ocfs2_db_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
				 struct buffer_head *bh,
				 void *data, size_t size)
{
	struct ocfs2_dir_block_trailer *trailer =
		ocfs2_dir_trailer_from_size(size, data);

	/*
	 * We aren't guaranteed to have the superblock here, so we
	 * must unconditionally compute the ecc data.
	 * __ocfs2_journal_access() will only set the triggers if
	 * metaecc is enabled.
	 */
	ocfs2_block_check_compute(data, size, &trailer->db_check);
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
				struct buffer_head *bh)
{
	mlog(ML_ERROR,
	     "ocfs2_abort_trigger called by JBD2.  bh = 0x%lx, "
	     "bh->b_blocknr = %llu\n",
	     (unsigned long)bh,
	     (unsigned long long)bh->b_blocknr);

	/* We aren't guaranteed to have the superblock here - but if we
	 * don't, it'll just crash. */
	ocfs2_error(bh->b_assoc_map->host->i_sb,
		    "JBD2 has aborted our journal, ocfs2 cannot continue\n");
}

static struct ocfs2_triggers di_triggers = {
	.ot_triggers = {
582
		.t_frozen = ocfs2_frozen_trigger,
583 584 585 586 587 588 589
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_dinode, i_check),
};

static struct ocfs2_triggers eb_triggers = {
	.ot_triggers = {
590
		.t_frozen = ocfs2_frozen_trigger,
591 592 593 594 595
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_extent_block, h_check),
};

596 597
static struct ocfs2_triggers rb_triggers = {
	.ot_triggers = {
598
		.t_frozen = ocfs2_frozen_trigger,
599 600 601 602 603
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_refcount_block, rf_check),
};

604 605
static struct ocfs2_triggers gd_triggers = {
	.ot_triggers = {
606
		.t_frozen = ocfs2_frozen_trigger,
607 608 609 610 611
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_group_desc, bg_check),
};

612 613
static struct ocfs2_triggers db_triggers = {
	.ot_triggers = {
614
		.t_frozen = ocfs2_db_frozen_trigger,
615 616 617 618
		.t_abort = ocfs2_abort_trigger,
	},
};

619 620
static struct ocfs2_triggers xb_triggers = {
	.ot_triggers = {
621
		.t_frozen = ocfs2_frozen_trigger,
622 623 624 625 626 627 628
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_xattr_block, xb_check),
};

static struct ocfs2_triggers dq_triggers = {
	.ot_triggers = {
629
		.t_frozen = ocfs2_dq_frozen_trigger,
630 631 632 633
		.t_abort = ocfs2_abort_trigger,
	},
};

634 635
static struct ocfs2_triggers dr_triggers = {
	.ot_triggers = {
636
		.t_frozen = ocfs2_frozen_trigger,
637 638 639 640 641 642 643
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_dx_root_block, dr_check),
};

static struct ocfs2_triggers dl_triggers = {
	.ot_triggers = {
644
		.t_frozen = ocfs2_frozen_trigger,
645 646 647 648 649
		.t_abort = ocfs2_abort_trigger,
	},
	.ot_offset	= offsetof(struct ocfs2_dx_leaf, dl_check),
};

650
static int __ocfs2_journal_access(handle_t *handle,
651
				  struct ocfs2_caching_info *ci,
652 653 654
				  struct buffer_head *bh,
				  struct ocfs2_triggers *triggers,
				  int type)
655 656
{
	int status;
657 658
	struct ocfs2_super *osb =
		OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
659

660
	BUG_ON(!ci || !ci->ci_ops);
661 662 663
	BUG_ON(!handle);
	BUG_ON(!bh);

T
Tao Ma 已提交
664 665 666
	trace_ocfs2_journal_access(
		(unsigned long long)ocfs2_metadata_cache_owner(ci),
		(unsigned long long)bh->b_blocknr, type, bh->b_size);
667 668 669 670 671 672 673 674 675

	/* we can safely remove this assertion after testing. */
	if (!buffer_uptodate(bh)) {
		mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
		mlog(ML_ERROR, "b_blocknr=%llu\n",
		     (unsigned long long)bh->b_blocknr);
		BUG();
	}

676
	/* Set the current transaction information on the ci so
677
	 * that the locking code knows whether it can drop it's locks
678
	 * on this ci or not. We're protected from the commit
679 680 681
	 * thread updating the current transaction id until
	 * ocfs2_commit_trans() because ocfs2_start_trans() took
	 * j_trans_barrier for us. */
682
	ocfs2_set_ci_lock_trans(osb->journal, ci);
683

684
	ocfs2_metadata_cache_io_lock(ci);
685 686 687
	switch (type) {
	case OCFS2_JOURNAL_ACCESS_CREATE:
	case OCFS2_JOURNAL_ACCESS_WRITE:
J
Joel Becker 已提交
688
		status = jbd2_journal_get_write_access(handle, bh);
689 690 691
		break;

	case OCFS2_JOURNAL_ACCESS_UNDO:
J
Joel Becker 已提交
692
		status = jbd2_journal_get_undo_access(handle, bh);
693 694 695 696
		break;

	default:
		status = -EINVAL;
697
		mlog(ML_ERROR, "Unknown access type!\n");
698
	}
699
	if (!status && ocfs2_meta_ecc(osb) && triggers)
700
		jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
701
	ocfs2_metadata_cache_io_unlock(ci);
702 703 704 705 706 707 708 709

	if (status < 0)
		mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
		     status, type);

	return status;
}

710 711
int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
			    struct buffer_head *bh, int type)
712
{
713
	return __ocfs2_journal_access(handle, ci, bh, &di_triggers, type);
714 715
}

716
int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
717 718
			    struct buffer_head *bh, int type)
{
719
	return __ocfs2_journal_access(handle, ci, bh, &eb_triggers, type);
720 721
}

722 723 724 725 726 727 728
int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
			    struct buffer_head *bh, int type)
{
	return __ocfs2_journal_access(handle, ci, bh, &rb_triggers,
				      type);
}

729
int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
730 731
			    struct buffer_head *bh, int type)
{
732
	return __ocfs2_journal_access(handle, ci, bh, &gd_triggers, type);
733 734
}

735
int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
736 737
			    struct buffer_head *bh, int type)
{
738
	return __ocfs2_journal_access(handle, ci, bh, &db_triggers, type);
739 740
}

741
int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
742 743
			    struct buffer_head *bh, int type)
{
744
	return __ocfs2_journal_access(handle, ci, bh, &xb_triggers, type);
745 746
}

747
int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
748 749
			    struct buffer_head *bh, int type)
{
750
	return __ocfs2_journal_access(handle, ci, bh, &dq_triggers, type);
751 752
}

753
int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
754 755
			    struct buffer_head *bh, int type)
{
756
	return __ocfs2_journal_access(handle, ci, bh, &dr_triggers, type);
757 758
}

759
int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
760 761
			    struct buffer_head *bh, int type)
{
762
	return __ocfs2_journal_access(handle, ci, bh, &dl_triggers, type);
763 764
}

765
int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
766 767
			 struct buffer_head *bh, int type)
{
768
	return __ocfs2_journal_access(handle, ci, bh, NULL, type);
769 770
}

771
void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh)
772 773 774
{
	int status;

T
Tao Ma 已提交
775
	trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr);
776

J
Joel Becker 已提交
777
	status = jbd2_journal_dirty_metadata(handle, bh);
778
	BUG_ON(status);
779 780
}

J
Joel Becker 已提交
781
#define OCFS2_DEFAULT_COMMIT_INTERVAL	(HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
782 783 784 785

void ocfs2_set_journal_params(struct ocfs2_super *osb)
{
	journal_t *journal = osb->journal->j_journal;
786 787 788 789
	unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;

	if (osb->osb_commit_interval)
		commit_interval = osb->osb_commit_interval;
790

791
	write_lock(&journal->j_state_lock);
792
	journal->j_commit_interval = commit_interval;
793
	if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
J
Joel Becker 已提交
794
		journal->j_flags |= JBD2_BARRIER;
795
	else
J
Joel Becker 已提交
796
		journal->j_flags &= ~JBD2_BARRIER;
797
	write_unlock(&journal->j_state_lock);
798 799 800 801 802 803 804 805 806 807
}

int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
{
	int status = -1;
	struct inode *inode = NULL; /* the journal inode */
	journal_t *j_journal = NULL;
	struct ocfs2_dinode *di = NULL;
	struct buffer_head *bh = NULL;
	struct ocfs2_super *osb;
M
Mark Fasheh 已提交
808
	int inode_lock = 0;
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

	BUG_ON(!journal);

	osb = journal->j_osb;

	/* already have the inode for our journal */
	inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
					    osb->slot_num);
	if (inode == NULL) {
		status = -EACCES;
		mlog_errno(status);
		goto done;
	}
	if (is_bad_inode(inode)) {
		mlog(ML_ERROR, "access error (bad inode)\n");
		iput(inode);
		inode = NULL;
		status = -EACCES;
		goto done;
	}

	SET_INODE_JOURNAL(inode);
	OCFS2_I(inode)->ip_open_count++;

833 834 835
	/* Skip recovery waits here - journal inode metadata never
	 * changes in a live cluster so it can be considered an
	 * exception to the rule. */
M
Mark Fasheh 已提交
836
	status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
837 838 839 840 841 842
	if (status < 0) {
		if (status != -ERESTARTSYS)
			mlog(ML_ERROR, "Could not get lock on journal!\n");
		goto done;
	}

M
Mark Fasheh 已提交
843
	inode_lock = 1;
844 845
	di = (struct ocfs2_dinode *)bh->b_data;

846
	if (i_size_read(inode) <  OCFS2_MIN_JOURNAL_SIZE) {
847
		mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
848
		     i_size_read(inode));
849 850 851 852
		status = -EINVAL;
		goto done;
	}

853
	trace_ocfs2_journal_init(i_size_read(inode),
T
Tao Ma 已提交
854 855
				 (unsigned long long)inode->i_blocks,
				 OCFS2_I(inode)->ip_clusters);
856 857

	/* call the kernels journal init function now */
J
Joel Becker 已提交
858
	j_journal = jbd2_journal_init_inode(inode);
859 860 861 862 863 864
	if (j_journal == NULL) {
		mlog(ML_ERROR, "Linux journal layer error\n");
		status = -EINVAL;
		goto done;
	}

T
Tao Ma 已提交
865
	trace_ocfs2_journal_init_maxlen(j_journal->j_maxlen);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

	*dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
		  OCFS2_JOURNAL_DIRTY_FL);

	journal->j_journal = j_journal;
	journal->j_inode = inode;
	journal->j_bh = bh;

	ocfs2_set_journal_params(osb);

	journal->j_state = OCFS2_JOURNAL_LOADED;

	status = 0;
done:
	if (status < 0) {
M
Mark Fasheh 已提交
881 882
		if (inode_lock)
			ocfs2_inode_unlock(inode, 1);
883
		brelse(bh);
884 885 886 887 888 889 890 891 892
		if (inode) {
			OCFS2_I(inode)->ip_open_count--;
			iput(inode);
		}
	}

	return status;
}

893 894 895 896 897 898 899 900 901 902
static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
{
	le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
}

static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
{
	return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
}

903
static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
904
				      int dirty, int replayed)
905 906 907 908 909 910 911 912
{
	int status;
	unsigned int flags;
	struct ocfs2_journal *journal = osb->journal;
	struct buffer_head *bh = journal->j_bh;
	struct ocfs2_dinode *fe;

	fe = (struct ocfs2_dinode *)bh->b_data;
913 914 915 916 917

	/* The journal bh on the osb always comes from ocfs2_journal_init()
	 * and was validated there inside ocfs2_inode_lock_full().  It's a
	 * code bug if we mess it up. */
	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
918 919 920 921 922 923 924 925

	flags = le32_to_cpu(fe->id1.journal1.ij_flags);
	if (dirty)
		flags |= OCFS2_JOURNAL_DIRTY_FL;
	else
		flags &= ~OCFS2_JOURNAL_DIRTY_FL;
	fe->id1.journal1.ij_flags = cpu_to_le32(flags);

926 927 928
	if (replayed)
		ocfs2_bump_recovery_generation(fe);

929
	ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
930
	status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode));
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
	if (status < 0)
		mlog_errno(status);

	return status;
}

/*
 * If the journal has been kmalloc'd it needs to be freed after this
 * call.
 */
void ocfs2_journal_shutdown(struct ocfs2_super *osb)
{
	struct ocfs2_journal *journal = NULL;
	int status = 0;
	struct inode *inode = NULL;
	int num_running_trans = 0;

948
	BUG_ON(!osb);
949 950 951 952 953 954 955 956 957 958

	journal = osb->journal;
	if (!journal)
		goto done;

	inode = journal->j_inode;

	if (journal->j_state != OCFS2_JOURNAL_LOADED)
		goto done;

J
Joel Becker 已提交
959
	/* need to inc inode use count - jbd2_journal_destroy will iput. */
960 961 962 963
	if (!igrab(inode))
		BUG();

	num_running_trans = atomic_read(&(osb->journal->j_num_trans));
T
Tao Ma 已提交
964
	trace_ocfs2_journal_shutdown(num_running_trans);
965 966 967 968 969 970 971 972 973 974 975 976

	/* Do a commit_cache here. It will flush our journal, *and*
	 * release any locks that are still held.
	 * set the SHUTDOWN flag and release the trans lock.
	 * the commit thread will take the trans lock for us below. */
	journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;

	/* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
	 * drop the trans_lock (which we want to hold until we
	 * completely destroy the journal. */
	if (osb->commit_task) {
		/* Wait for the commit thread */
T
Tao Ma 已提交
977
		trace_ocfs2_journal_shutdown_wait(osb->commit_task);
978 979 980 981 982 983
		kthread_stop(osb->commit_task);
		osb->commit_task = NULL;
	}

	BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);

S
Sunil Mushran 已提交
984
	if (ocfs2_mount_local(osb)) {
J
Joel Becker 已提交
985 986 987
		jbd2_journal_lock_updates(journal->j_journal);
		status = jbd2_journal_flush(journal->j_journal);
		jbd2_journal_unlock_updates(journal->j_journal);
S
Sunil Mushran 已提交
988 989 990 991 992 993 994 995 996
		if (status < 0)
			mlog_errno(status);
	}

	if (status == 0) {
		/*
		 * Do not toggle if flush was unsuccessful otherwise
		 * will leave dirty metadata in a "clean" journal
		 */
997
		status = ocfs2_journal_toggle_dirty(osb, 0, 0);
S
Sunil Mushran 已提交
998 999 1000
		if (status < 0)
			mlog_errno(status);
	}
1001 1002

	/* Shutdown the kernel journal system */
J
Joel Becker 已提交
1003
	jbd2_journal_destroy(journal->j_journal);
1004
	journal->j_journal = NULL;
1005 1006 1007 1008

	OCFS2_I(inode)->ip_open_count--;

	/* unlock our journal */
M
Mark Fasheh 已提交
1009
	ocfs2_inode_unlock(inode, 1);
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

	brelse(journal->j_bh);
	journal->j_bh = NULL;

	journal->j_state = OCFS2_JOURNAL_FREE;

//	up_write(&journal->j_trans_barrier);
done:
	if (inode)
		iput(inode);
}

static void ocfs2_clear_journal_error(struct super_block *sb,
				      journal_t *journal,
				      int slot)
{
	int olderr;

J
Joel Becker 已提交
1028
	olderr = jbd2_journal_errno(journal);
1029 1030 1031 1032 1033 1034
	if (olderr) {
		mlog(ML_ERROR, "File system error %d recorded in "
		     "journal %u.\n", olderr, slot);
		mlog(ML_ERROR, "File system on device %s needs checking.\n",
		     sb->s_id);

J
Joel Becker 已提交
1035 1036
		jbd2_journal_ack_err(journal);
		jbd2_journal_clear_err(journal);
1037 1038 1039
	}
}

1040
int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
1041 1042 1043 1044
{
	int status = 0;
	struct ocfs2_super *osb;

J
Julia Lawall 已提交
1045
	BUG_ON(!journal);
1046 1047 1048

	osb = journal->j_osb;

J
Joel Becker 已提交
1049
	status = jbd2_journal_load(journal->j_journal);
1050 1051 1052 1053 1054 1055 1056
	if (status < 0) {
		mlog(ML_ERROR, "Failed to load journal!\n");
		goto done;
	}

	ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);

1057
	status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
1058 1059 1060 1061 1062 1063
	if (status < 0) {
		mlog_errno(status);
		goto done;
	}

	/* Launch the commit thread */
S
Sunil Mushran 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	if (!local) {
		osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
					       "ocfs2cmt");
		if (IS_ERR(osb->commit_task)) {
			status = PTR_ERR(osb->commit_task);
			osb->commit_task = NULL;
			mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
			     "error=%d", status);
			goto done;
		}
	} else
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		osb->commit_task = NULL;

done:
	return status;
}


/* 'full' flag tells us whether we clear out all blocks or if we just
 * mark the journal clean */
int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
{
	int status;

1088
	BUG_ON(!journal);
1089

J
Joel Becker 已提交
1090
	status = jbd2_journal_wipe(journal->j_journal, full);
1091 1092 1093 1094 1095
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

1096
	status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
1097 1098 1099 1100 1101 1102 1103
	if (status < 0)
		mlog_errno(status);

bail:
	return status;
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
static int ocfs2_recovery_completed(struct ocfs2_super *osb)
{
	int empty;
	struct ocfs2_recovery_map *rm = osb->recovery_map;

	spin_lock(&osb->osb_lock);
	empty = (rm->rm_used == 0);
	spin_unlock(&osb->osb_lock);

	return empty;
}

void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
{
	wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
/*
 * JBD Might read a cached version of another nodes journal file. We
 * don't want this as this file changes often and we get no
 * notification on those changes. The only way to be sure that we've
 * got the most up to date version of those blocks then is to force
 * read them off disk. Just searching through the buffer cache won't
 * work as there may be pages backing this file which are still marked
 * up to date. We know things can't change on this file underneath us
 * as we have the lock by now :)
 */
static int ocfs2_force_read_journal(struct inode *inode)
{
	int status = 0;
1134
	int i;
1135
	u64 v_blkno, p_blkno, p_blocks, num_blocks;
1136
#define CONCURRENT_JOURNAL_FILL 32ULL
1137 1138 1139 1140
	struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];

	memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);

1141
	num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
1142
	v_blkno = 0;
1143
	while (v_blkno < num_blocks) {
1144
		status = ocfs2_extent_map_get_blocks(inode, v_blkno,
1145
						     &p_blkno, &p_blocks, NULL);
1146 1147 1148 1149 1150 1151 1152 1153
		if (status < 0) {
			mlog_errno(status);
			goto bail;
		}

		if (p_blocks > CONCURRENT_JOURNAL_FILL)
			p_blocks = CONCURRENT_JOURNAL_FILL;

1154 1155
		/* We are reading journal data which should not
		 * be put in the uptodate cache */
1156 1157
		status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
						p_blkno, p_blocks, bhs);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
		if (status < 0) {
			mlog_errno(status);
			goto bail;
		}

		for(i = 0; i < p_blocks; i++) {
			brelse(bhs[i]);
			bhs[i] = NULL;
		}

		v_blkno += p_blocks;
	}

bail:
	for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
1173
		brelse(bhs[i]);
1174 1175 1176 1177 1178 1179 1180 1181
	return status;
}

struct ocfs2_la_recovery_item {
	struct list_head	lri_list;
	int			lri_slot;
	struct ocfs2_dinode	*lri_la_dinode;
	struct ocfs2_dinode	*lri_tl_dinode;
J
Jan Kara 已提交
1182
	struct ocfs2_quota_recovery *lri_qrec;
1183
	enum ocfs2_orphan_reco_type  lri_orphan_reco_type;
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
};

/* Does the second half of the recovery process. By this point, the
 * node is marked clean and can actually be considered recovered,
 * hence it's no longer in the recovery map, but there's still some
 * cleanup we can do which shouldn't happen within the recovery thread
 * as locking in that context becomes very difficult if we are to take
 * recovering nodes into account.
 *
 * NOTE: This function can and will sleep on recovery of other nodes
 * during cluster locking, just like any other ocfs2 process.
 */
D
David Howells 已提交
1196
void ocfs2_complete_recovery(struct work_struct *work)
1197
{
T
Tao Ma 已提交
1198
	int ret = 0;
D
David Howells 已提交
1199 1200 1201
	struct ocfs2_journal *journal =
		container_of(work, struct ocfs2_journal, j_recovery_work);
	struct ocfs2_super *osb = journal->j_osb;
1202
	struct ocfs2_dinode *la_dinode, *tl_dinode;
1203
	struct ocfs2_la_recovery_item *item, *n;
J
Jan Kara 已提交
1204
	struct ocfs2_quota_recovery *qrec;
1205
	enum ocfs2_orphan_reco_type orphan_reco_type;
1206 1207
	LIST_HEAD(tmp_la_list);

T
Tao Ma 已提交
1208 1209
	trace_ocfs2_complete_recovery(
		(unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno);
1210 1211 1212 1213 1214

	spin_lock(&journal->j_lock);
	list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
	spin_unlock(&journal->j_lock);

1215
	list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
1216 1217
		list_del_init(&item->lri_list);

1218 1219
		ocfs2_wait_on_quotas(osb);

1220
		la_dinode = item->lri_la_dinode;
T
Tao Ma 已提交
1221 1222
		tl_dinode = item->lri_tl_dinode;
		qrec = item->lri_qrec;
1223
		orphan_reco_type = item->lri_orphan_reco_type;
1224

T
Tao Ma 已提交
1225 1226 1227 1228 1229 1230
		trace_ocfs2_complete_recovery_slot(item->lri_slot,
			la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0,
			tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0,
			qrec);

		if (la_dinode) {
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
			ret = ocfs2_complete_local_alloc_recovery(osb,
								  la_dinode);
			if (ret < 0)
				mlog_errno(ret);

			kfree(la_dinode);
		}

		if (tl_dinode) {
			ret = ocfs2_complete_truncate_log_recovery(osb,
								   tl_dinode);
			if (ret < 0)
				mlog_errno(ret);

			kfree(tl_dinode);
		}

1248 1249
		ret = ocfs2_recover_orphans(osb, item->lri_slot,
				orphan_reco_type);
1250 1251 1252
		if (ret < 0)
			mlog_errno(ret);

J
Jan Kara 已提交
1253 1254 1255 1256 1257 1258 1259 1260
		if (qrec) {
			ret = ocfs2_finish_quota_recovery(osb, qrec,
							  item->lri_slot);
			if (ret < 0)
				mlog_errno(ret);
			/* Recovery info is already freed now */
		}

1261 1262 1263
		kfree(item);
	}

T
Tao Ma 已提交
1264
	trace_ocfs2_complete_recovery_end(ret);
1265 1266 1267 1268 1269 1270 1271 1272
}

/* NOTE: This function always eats your references to la_dinode and
 * tl_dinode, either manually on error, or by passing them to
 * ocfs2_complete_recovery */
static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
					    int slot_num,
					    struct ocfs2_dinode *la_dinode,
J
Jan Kara 已提交
1273
					    struct ocfs2_dinode *tl_dinode,
1274 1275
					    struct ocfs2_quota_recovery *qrec,
					    enum ocfs2_orphan_reco_type orphan_reco_type)
1276 1277 1278
{
	struct ocfs2_la_recovery_item *item;

1279
	item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
1280 1281 1282 1283
	if (!item) {
		/* Though we wish to avoid it, we are in fact safe in
		 * skipping local alloc cleanup as fsck.ocfs2 is more
		 * than capable of reclaiming unused space. */
1284 1285
		kfree(la_dinode);
		kfree(tl_dinode);
1286

J
Jan Kara 已提交
1287 1288 1289
		if (qrec)
			ocfs2_free_quota_recovery(qrec);

1290 1291 1292 1293 1294 1295 1296 1297
		mlog_errno(-ENOMEM);
		return;
	}

	INIT_LIST_HEAD(&item->lri_list);
	item->lri_la_dinode = la_dinode;
	item->lri_slot = slot_num;
	item->lri_tl_dinode = tl_dinode;
J
Jan Kara 已提交
1298
	item->lri_qrec = qrec;
1299
	item->lri_orphan_reco_type = orphan_reco_type;
1300 1301 1302 1303 1304 1305 1306 1307

	spin_lock(&journal->j_lock);
	list_add_tail(&item->lri_list, &journal->j_la_cleanups);
	queue_work(ocfs2_wq, &journal->j_recovery_work);
	spin_unlock(&journal->j_lock);
}

/* Called by the mount code to queue recovery the last part of
1308
 * recovery for it's own and offline slot(s). */
1309 1310 1311 1312
void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
{
	struct ocfs2_journal *journal = osb->journal;

1313 1314 1315
	if (ocfs2_is_hard_readonly(osb))
		return;

1316 1317 1318
	/* No need to queue up our truncate_log as regular cleanup will catch
	 * that */
	ocfs2_queue_recovery_completion(journal, osb->slot_num,
1319 1320
					osb->local_alloc_copy, NULL, NULL,
					ORPHAN_NEED_TRUNCATE);
1321
	ocfs2_schedule_truncate_log_flush(osb, 0);
1322

1323 1324 1325 1326 1327
	osb->local_alloc_copy = NULL;
	osb->dirty = 0;

	/* queue to recover orphan slots for all offline slots */
	ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1328
	ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
1329
	ocfs2_free_replay_slots(osb);
1330 1331
}

J
Jan Kara 已提交
1332 1333 1334 1335 1336 1337 1338
void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
{
	if (osb->quota_rec) {
		ocfs2_queue_recovery_completion(osb->journal,
						osb->slot_num,
						NULL,
						NULL,
1339 1340
						osb->quota_rec,
						ORPHAN_NEED_TRUNCATE);
J
Jan Kara 已提交
1341 1342 1343 1344
		osb->quota_rec = NULL;
	}
}

1345 1346
static int __ocfs2_recovery_thread(void *arg)
{
J
Jan Kara 已提交
1347
	int status, node_num, slot_num;
1348
	struct ocfs2_super *osb = arg;
1349
	struct ocfs2_recovery_map *rm = osb->recovery_map;
J
Jan Kara 已提交
1350 1351 1352
	int *rm_quota = NULL;
	int rm_quota_used = 0, i;
	struct ocfs2_quota_recovery *qrec;
1353 1354 1355 1356 1357 1358

	status = ocfs2_wait_on_mount(osb);
	if (status < 0) {
		goto bail;
	}

J
Jan Kara 已提交
1359 1360 1361 1362 1363
	rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
	if (!rm_quota) {
		status = -ENOMEM;
		goto bail;
	}
1364 1365 1366 1367 1368 1369 1370
restart:
	status = ocfs2_super_lock(osb, 1);
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

1371 1372 1373 1374 1375 1376
	status = ocfs2_compute_replay_slots(osb);
	if (status < 0)
		mlog_errno(status);

	/* queue recovery for our own slot */
	ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1377
					NULL, NULL, ORPHAN_NO_NEED_TRUNCATE);
1378

1379 1380 1381 1382 1383 1384
	spin_lock(&osb->osb_lock);
	while (rm->rm_used) {
		/* It's always safe to remove entry zero, as we won't
		 * clear it until ocfs2_recover_node() has succeeded. */
		node_num = rm->rm_entries[0];
		spin_unlock(&osb->osb_lock);
J
Jan Kara 已提交
1385
		slot_num = ocfs2_node_num_to_slot(osb, node_num);
T
Tao Ma 已提交
1386
		trace_ocfs2_recovery_thread_node(node_num, slot_num);
J
Jan Kara 已提交
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
		if (slot_num == -ENOENT) {
			status = 0;
			goto skip_recovery;
		}

		/* It is a bit subtle with quota recovery. We cannot do it
		 * immediately because we have to obtain cluster locks from
		 * quota files and we also don't want to just skip it because
		 * then quota usage would be out of sync until some node takes
		 * the slot. So we remember which nodes need quota recovery
		 * and when everything else is done, we recover quotas. */
		for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
		if (i == rm_quota_used)
			rm_quota[rm_quota_used++] = slot_num;

		status = ocfs2_recover_node(osb, node_num, slot_num);
skip_recovery:
1404 1405 1406
		if (!status) {
			ocfs2_recovery_map_clear(osb, node_num);
		} else {
1407 1408 1409 1410 1411 1412 1413
			mlog(ML_ERROR,
			     "Error %d recovering node %d on device (%u,%u)!\n",
			     status, node_num,
			     MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
			mlog(ML_ERROR, "Volume requires unmount.\n");
		}

1414
		spin_lock(&osb->osb_lock);
1415
	}
1416
	spin_unlock(&osb->osb_lock);
T
Tao Ma 已提交
1417
	trace_ocfs2_recovery_thread_end(status);
1418

1419 1420 1421 1422 1423 1424
	/* Refresh all journal recovery generations from disk */
	status = ocfs2_check_journals_nolocks(osb);
	status = (status == -EROFS) ? 0 : status;
	if (status < 0)
		mlog_errno(status);

J
Jan Kara 已提交
1425
	/* Now it is right time to recover quotas... We have to do this under
L
Lucas De Marchi 已提交
1426
	 * superblock lock so that no one can start using the slot (and crash)
J
Jan Kara 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435
	 * before we recover it */
	for (i = 0; i < rm_quota_used; i++) {
		qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
		if (IS_ERR(qrec)) {
			status = PTR_ERR(qrec);
			mlog_errno(status);
			continue;
		}
		ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1436 1437
						NULL, NULL, qrec,
						ORPHAN_NEED_TRUNCATE);
J
Jan Kara 已提交
1438 1439
	}

1440 1441
	ocfs2_super_unlock(osb, 1);

1442
	/* queue recovery for offline slots */
1443
	ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
1444 1445

bail:
1446
	mutex_lock(&osb->recovery_lock);
1447
	if (!status && !ocfs2_recovery_completed(osb)) {
1448
		mutex_unlock(&osb->recovery_lock);
1449 1450 1451
		goto restart;
	}

1452
	ocfs2_free_replay_slots(osb);
1453 1454 1455 1456
	osb->recovery_thread_task = NULL;
	mb(); /* sync with ocfs2_recovery_thread_running */
	wake_up(&osb->recovery_event);

1457
	mutex_unlock(&osb->recovery_lock);
1458

1459
	kfree(rm_quota);
J
Jan Kara 已提交
1460

1461 1462 1463 1464 1465 1466 1467 1468
	/* no one is callint kthread_stop() for us so the kthread() api
	 * requires that we call do_exit().  And it isn't exported, but
	 * complete_and_exit() seems to be a minimal wrapper around it. */
	complete_and_exit(NULL, status);
}

void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
{
1469
	mutex_lock(&osb->recovery_lock);
1470

T
Tao Ma 已提交
1471 1472 1473 1474
	trace_ocfs2_recovery_thread(node_num, osb->node_num,
		osb->disable_recovery, osb->recovery_thread_task,
		osb->disable_recovery ?
		-1 : ocfs2_recovery_map_set(osb, node_num));
1475

T
Tao Ma 已提交
1476 1477
	if (osb->disable_recovery)
		goto out;
1478 1479 1480 1481 1482

	if (osb->recovery_thread_task)
		goto out;

	osb->recovery_thread_task =  kthread_run(__ocfs2_recovery_thread, osb,
M
Mark Fasheh 已提交
1483
						 "ocfs2rec");
1484 1485 1486 1487 1488 1489
	if (IS_ERR(osb->recovery_thread_task)) {
		mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
		osb->recovery_thread_task = NULL;
	}

out:
1490
	mutex_unlock(&osb->recovery_lock);
1491 1492 1493
	wake_up(&osb->recovery_event);
}

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
				    int slot_num,
				    struct buffer_head **bh,
				    struct inode **ret_inode)
{
	int status = -EACCES;
	struct inode *inode = NULL;

	BUG_ON(slot_num >= osb->max_slots);

	inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
					    slot_num);
	if (!inode || is_bad_inode(inode)) {
		mlog_errno(status);
		goto bail;
	}
	SET_INODE_JOURNAL(inode);

1512
	status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

	status = 0;

bail:
	if (inode) {
		if (status || !ret_inode)
			iput(inode);
		else
			*ret_inode = inode;
	}
	return status;
}

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
/* Does the actual journal replay and marks the journal inode as
 * clean. Will only replay if the journal inode is marked dirty. */
static int ocfs2_replay_journal(struct ocfs2_super *osb,
				int node_num,
				int slot_num)
{
	int status;
	int got_lock = 0;
	unsigned int flags;
	struct inode *inode = NULL;
	struct ocfs2_dinode *fe;
	journal_t *journal = NULL;
	struct buffer_head *bh = NULL;
1543
	u32 slot_reco_gen;
1544

1545 1546
	status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
	if (status) {
1547 1548 1549
		mlog_errno(status);
		goto done;
	}
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564

	fe = (struct ocfs2_dinode *)bh->b_data;
	slot_reco_gen = ocfs2_get_recovery_generation(fe);
	brelse(bh);
	bh = NULL;

	/*
	 * As the fs recovery is asynchronous, there is a small chance that
	 * another node mounted (and recovered) the slot before the recovery
	 * thread could get the lock. To handle that, we dirty read the journal
	 * inode for that slot to get the recovery generation. If it is
	 * different than what we expected, the slot has been recovered.
	 * If not, it needs recovery.
	 */
	if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
T
Tao Ma 已提交
1565
		trace_ocfs2_replay_journal_recovered(slot_num,
1566 1567 1568
		     osb->slot_recovery_generations[slot_num], slot_reco_gen);
		osb->slot_recovery_generations[slot_num] = slot_reco_gen;
		status = -EBUSY;
1569 1570
		goto done;
	}
1571 1572

	/* Continue with recovery as the journal has not yet been recovered */
1573

M
Mark Fasheh 已提交
1574
	status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
1575
	if (status < 0) {
T
Tao Ma 已提交
1576
		trace_ocfs2_replay_journal_lock_err(status);
1577 1578 1579 1580 1581 1582 1583 1584 1585
		if (status != -ERESTARTSYS)
			mlog(ML_ERROR, "Could not lock journal!\n");
		goto done;
	}
	got_lock = 1;

	fe = (struct ocfs2_dinode *) bh->b_data;

	flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1586
	slot_reco_gen = ocfs2_get_recovery_generation(fe);
1587 1588

	if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
T
Tao Ma 已提交
1589
		trace_ocfs2_replay_journal_skip(node_num);
1590 1591
		/* Refresh recovery generation for the slot */
		osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1592 1593 1594
		goto done;
	}

1595 1596 1597
	/* we need to run complete recovery for offline orphan slots */
	ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);

1598 1599 1600
	printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\
	       "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
	       MINOR(osb->sb->s_dev));
1601 1602 1603 1604 1605 1606 1607 1608 1609

	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);

	status = ocfs2_force_read_journal(inode);
	if (status < 0) {
		mlog_errno(status);
		goto done;
	}

J
Joel Becker 已提交
1610
	journal = jbd2_journal_init_inode(inode);
1611 1612 1613 1614 1615 1616
	if (journal == NULL) {
		mlog(ML_ERROR, "Linux journal layer error\n");
		status = -EIO;
		goto done;
	}

J
Joel Becker 已提交
1617
	status = jbd2_journal_load(journal);
1618 1619 1620 1621
	if (status < 0) {
		mlog_errno(status);
		if (!igrab(inode))
			BUG();
J
Joel Becker 已提交
1622
		jbd2_journal_destroy(journal);
1623 1624 1625 1626 1627 1628
		goto done;
	}

	ocfs2_clear_journal_error(osb->sb, journal, slot_num);

	/* wipe the journal */
J
Joel Becker 已提交
1629 1630 1631
	jbd2_journal_lock_updates(journal);
	status = jbd2_journal_flush(journal);
	jbd2_journal_unlock_updates(journal);
1632 1633 1634 1635 1636 1637 1638 1639
	if (status < 0)
		mlog_errno(status);

	/* This will mark the node clean */
	flags = le32_to_cpu(fe->id1.journal1.ij_flags);
	flags &= ~OCFS2_JOURNAL_DIRTY_FL;
	fe->id1.journal1.ij_flags = cpu_to_le32(flags);

1640 1641 1642 1643 1644
	/* Increment recovery generation to indicate successful recovery */
	ocfs2_bump_recovery_generation(fe);
	osb->slot_recovery_generations[slot_num] =
					ocfs2_get_recovery_generation(fe);

1645
	ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1646
	status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
1647 1648 1649 1650 1651 1652
	if (status < 0)
		mlog_errno(status);

	if (!igrab(inode))
		BUG();

J
Joel Becker 已提交
1653
	jbd2_journal_destroy(journal);
1654

1655 1656 1657
	printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\
	       "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
	       MINOR(osb->sb->s_dev));
1658 1659 1660
done:
	/* drop the lock on this nodes journal */
	if (got_lock)
M
Mark Fasheh 已提交
1661
		ocfs2_inode_unlock(inode, 1);
1662 1663 1664 1665

	if (inode)
		iput(inode);

1666
	brelse(bh);
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683

	return status;
}

/*
 * Do the most important parts of node recovery:
 *  - Replay it's journal
 *  - Stamp a clean local allocator file
 *  - Stamp a clean truncate log
 *  - Mark the node clean
 *
 * If this function completes without error, a node in OCFS2 can be
 * said to have been safely recovered. As a result, failure during the
 * second part of a nodes recovery process (local alloc recovery) is
 * far less concerning.
 */
static int ocfs2_recover_node(struct ocfs2_super *osb,
J
Jan Kara 已提交
1684
			      int node_num, int slot_num)
1685 1686 1687 1688 1689
{
	int status = 0;
	struct ocfs2_dinode *la_copy = NULL;
	struct ocfs2_dinode *tl_copy = NULL;

T
Tao Ma 已提交
1690
	trace_ocfs2_recover_node(node_num, slot_num, osb->node_num);
1691 1692 1693

	/* Should not ever be called to recover ourselves -- in that
	 * case we should've called ocfs2_journal_load instead. */
1694
	BUG_ON(osb->node_num == node_num);
1695 1696 1697

	status = ocfs2_replay_journal(osb, node_num, slot_num);
	if (status < 0) {
1698
		if (status == -EBUSY) {
T
Tao Ma 已提交
1699
			trace_ocfs2_recover_node_skip(slot_num, node_num);
1700 1701 1702
			status = 0;
			goto done;
		}
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
		mlog_errno(status);
		goto done;
	}

	/* Stamp a clean local alloc file AFTER recovering the journal... */
	status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
	if (status < 0) {
		mlog_errno(status);
		goto done;
	}

	/* An error from begin_truncate_log_recovery is not
	 * serious enough to warrant halting the rest of
	 * recovery. */
	status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
	if (status < 0)
		mlog_errno(status);

	/* Likewise, this would be a strange but ultimately not so
	 * harmful place to get an error... */
1723
	status = ocfs2_clear_slot(osb, slot_num);
1724 1725 1726 1727 1728
	if (status < 0)
		mlog_errno(status);

	/* This will kfree the memory pointed to by la_copy and tl_copy */
	ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1729
					tl_copy, NULL, ORPHAN_NEED_TRUNCATE);
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762

	status = 0;
done:

	return status;
}

/* Test node liveness by trylocking his journal. If we get the lock,
 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
 * still alive (we couldn't get the lock) and < 0 on error. */
static int ocfs2_trylock_journal(struct ocfs2_super *osb,
				 int slot_num)
{
	int status, flags;
	struct inode *inode = NULL;

	inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
					    slot_num);
	if (inode == NULL) {
		mlog(ML_ERROR, "access error\n");
		status = -EACCES;
		goto bail;
	}
	if (is_bad_inode(inode)) {
		mlog(ML_ERROR, "access error (bad inode)\n");
		iput(inode);
		inode = NULL;
		status = -EACCES;
		goto bail;
	}
	SET_INODE_JOURNAL(inode);

	flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
M
Mark Fasheh 已提交
1763
	status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1764 1765 1766 1767 1768 1769
	if (status < 0) {
		if (status != -EAGAIN)
			mlog_errno(status);
		goto bail;
	}

M
Mark Fasheh 已提交
1770
	ocfs2_inode_unlock(inode, 1);
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
bail:
	if (inode)
		iput(inode);

	return status;
}

/* Call this underneath ocfs2_super_lock. It also assumes that the
 * slot info struct has been updated from disk. */
int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
{
1782 1783
	unsigned int node_num;
	int status, i;
1784
	u32 gen;
1785 1786
	struct buffer_head *bh = NULL;
	struct ocfs2_dinode *di;
1787 1788 1789 1790

	/* This is called with the super block cluster lock, so we
	 * know that the slot map can't change underneath us. */

1791
	for (i = 0; i < osb->max_slots; i++) {
1792 1793 1794 1795 1796 1797 1798
		/* Read journal inode to get the recovery generation */
		status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
		if (status) {
			mlog_errno(status);
			goto bail;
		}
		di = (struct ocfs2_dinode *)bh->b_data;
1799
		gen = ocfs2_get_recovery_generation(di);
1800 1801 1802
		brelse(bh);
		bh = NULL;

1803 1804 1805
		spin_lock(&osb->osb_lock);
		osb->slot_recovery_generations[i] = gen;

T
Tao Ma 已提交
1806 1807
		trace_ocfs2_mark_dead_nodes(i,
					    osb->slot_recovery_generations[i]);
1808

1809 1810
		if (i == osb->slot_num) {
			spin_unlock(&osb->osb_lock);
1811
			continue;
1812
		}
1813 1814

		status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1815 1816
		if (status == -ENOENT) {
			spin_unlock(&osb->osb_lock);
1817
			continue;
1818
		}
1819

1820 1821
		if (__ocfs2_recovery_map_test(osb, node_num)) {
			spin_unlock(&osb->osb_lock);
1822
			continue;
1823
		}
1824
		spin_unlock(&osb->osb_lock);
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845

		/* Ok, we have a slot occupied by another node which
		 * is not in the recovery map. We trylock his journal
		 * file here to test if he's alive. */
		status = ocfs2_trylock_journal(osb, i);
		if (!status) {
			/* Since we're called from mount, we know that
			 * the recovery thread can't race us on
			 * setting / checking the recovery bits. */
			ocfs2_recovery_thread(osb, node_num);
		} else if ((status < 0) && (status != -EAGAIN)) {
			mlog_errno(status);
			goto bail;
		}
	}

	status = 0;
bail:
	return status;
}

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
/*
 * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some
 * randomness to the timeout to minimize multple nodes firing the timer at the
 * same time.
 */
static inline unsigned long ocfs2_orphan_scan_timeout(void)
{
	unsigned long time;

	get_random_bytes(&time, sizeof(time));
	time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000);
	return msecs_to_jiffies(time);
}

/*
 * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for
 * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This
 * is done to catch any orphans that are left over in orphan directories.
 *
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
 * It scans all slots, even ones that are in use. It does so to handle the
 * case described below:
 *
 *   Node 1 has an inode it was using. The dentry went away due to memory
 *   pressure.  Node 1 closes the inode, but it's on the free list. The node
 *   has the open lock.
 *   Node 2 unlinks the inode. It grabs the dentry lock to notify others,
 *   but node 1 has no dentry and doesn't get the message. It trylocks the
 *   open lock, sees that another node has a PR, and does nothing.
 *   Later node 2 runs its orphan dir. It igets the inode, trylocks the
 *   open lock, sees the PR still, and does nothing.
 *   Basically, we have to trigger an orphan iput on node 1. The only way
 *   for this to happen is if node 1 runs node 2's orphan dir.
 *
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
 * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT
 * seconds.  It gets an EX lock on os_lockres and checks sequence number
 * stored in LVB. If the sequence number has changed, it means some other
 * node has done the scan.  This node skips the scan and tracks the
 * sequence number.  If the sequence number didn't change, it means a scan
 * hasn't happened.  The node queues a scan and increments the
 * sequence number in the LVB.
 */
void ocfs2_queue_orphan_scan(struct ocfs2_super *osb)
{
	struct ocfs2_orphan_scan *os;
	int status, i;
	u32 seqno = 0;

	os = &osb->osb_orphan_scan;

1895 1896 1897
	if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
		goto out;

T
Tao Ma 已提交
1898 1899 1900
	trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno,
					    atomic_read(&os->os_state));

1901
	status = ocfs2_orphan_scan_lock(osb, &seqno);
1902 1903 1904 1905 1906 1907
	if (status < 0) {
		if (status != -EAGAIN)
			mlog_errno(status);
		goto out;
	}

1908 1909 1910 1911
	/* Do no queue the tasks if the volume is being umounted */
	if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
		goto unlock;

1912 1913 1914 1915 1916 1917 1918
	if (os->os_seqno != seqno) {
		os->os_seqno = seqno;
		goto unlock;
	}

	for (i = 0; i < osb->max_slots; i++)
		ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL,
1919
						NULL, ORPHAN_NO_NEED_TRUNCATE);
1920 1921 1922 1923 1924
	/*
	 * We queued a recovery on orphan slots, increment the sequence
	 * number and update LVB so other node will skip the scan for a while
	 */
	seqno++;
1925 1926
	os->os_count++;
	os->os_scantime = CURRENT_TIME;
1927
unlock:
1928
	ocfs2_orphan_scan_unlock(osb, seqno);
1929
out:
T
Tao Ma 已提交
1930 1931
	trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno,
					  atomic_read(&os->os_state));
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
	return;
}

/* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */
void ocfs2_orphan_scan_work(struct work_struct *work)
{
	struct ocfs2_orphan_scan *os;
	struct ocfs2_super *osb;

	os = container_of(work, struct ocfs2_orphan_scan,
			  os_orphan_scan_work.work);
	osb = os->os_osb;

	mutex_lock(&os->os_lock);
	ocfs2_queue_orphan_scan(osb);
1947
	if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE)
1948
		queue_delayed_work(ocfs2_wq, &os->os_orphan_scan_work,
1949
				      ocfs2_orphan_scan_timeout());
1950 1951 1952 1953 1954 1955 1956 1957
	mutex_unlock(&os->os_lock);
}

void ocfs2_orphan_scan_stop(struct ocfs2_super *osb)
{
	struct ocfs2_orphan_scan *os;

	os = &osb->osb_orphan_scan;
1958 1959 1960 1961 1962 1963
	if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) {
		atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
		mutex_lock(&os->os_lock);
		cancel_delayed_work(&os->os_orphan_scan_work);
		mutex_unlock(&os->os_lock);
	}
1964 1965
}

1966
void ocfs2_orphan_scan_init(struct ocfs2_super *osb)
1967 1968 1969 1970 1971
{
	struct ocfs2_orphan_scan *os;

	os = &osb->osb_orphan_scan;
	os->os_osb = osb;
1972
	os->os_count = 0;
1973
	os->os_seqno = 0;
1974
	mutex_init(&os->os_lock);
1975
	INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work);
1976
}
1977

1978 1979 1980 1981 1982 1983
void ocfs2_orphan_scan_start(struct ocfs2_super *osb)
{
	struct ocfs2_orphan_scan *os;

	os = &osb->osb_orphan_scan;
	os->os_scantime = CURRENT_TIME;
1984 1985 1986 1987
	if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
		atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
	else {
		atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE);
1988 1989
		queue_delayed_work(ocfs2_wq, &os->os_orphan_scan_work,
				   ocfs2_orphan_scan_timeout());
1990
	}
1991 1992
}

M
Mark Fasheh 已提交
1993
struct ocfs2_orphan_filldir_priv {
A
Al Viro 已提交
1994
	struct dir_context	ctx;
M
Mark Fasheh 已提交
1995 1996 1997 1998
	struct inode		*head;
	struct ocfs2_super	*osb;
};

1999 2000 2001
static int ocfs2_orphan_filldir(struct dir_context *ctx, const char *name,
				int name_len, loff_t pos, u64 ino,
				unsigned type)
M
Mark Fasheh 已提交
2002
{
2003 2004
	struct ocfs2_orphan_filldir_priv *p =
		container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx);
M
Mark Fasheh 已提交
2005 2006 2007 2008 2009 2010 2011 2012 2013
	struct inode *iter;

	if (name_len == 1 && !strncmp(".", name, 1))
		return 0;
	if (name_len == 2 && !strncmp("..", name, 2))
		return 0;

	/* Skip bad inodes so that recovery can continue */
	iter = ocfs2_iget(p->osb, ino,
J
Jan Kara 已提交
2014
			  OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
M
Mark Fasheh 已提交
2015 2016 2017
	if (IS_ERR(iter))
		return 0;

2018 2019 2020 2021 2022 2023 2024
	/* Skip inodes which are already added to recover list, since dio may
	 * happen concurrently with unlink/rename */
	if (OCFS2_I(iter)->ip_next_orphan) {
		iput(iter);
		return 0;
	}

T
Tao Ma 已提交
2025
	trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno);
M
Mark Fasheh 已提交
2026 2027 2028 2029 2030 2031 2032 2033
	/* No locking is required for the next_orphan queue as there
	 * is only ever a single process doing orphan recovery. */
	OCFS2_I(iter)->ip_next_orphan = p->head;
	p->head = iter;

	return 0;
}

2034 2035 2036
static int ocfs2_queue_orphans(struct ocfs2_super *osb,
			       int slot,
			       struct inode **head)
2037
{
2038
	int status;
2039
	struct inode *orphan_dir_inode = NULL;
A
Al Viro 已提交
2040 2041 2042 2043 2044
	struct ocfs2_orphan_filldir_priv priv = {
		.ctx.actor = ocfs2_orphan_filldir,
		.osb = osb,
		.head = *head
	};
2045 2046 2047 2048 2049 2050 2051

	orphan_dir_inode = ocfs2_get_system_file_inode(osb,
						       ORPHAN_DIR_SYSTEM_INODE,
						       slot);
	if  (!orphan_dir_inode) {
		status = -ENOENT;
		mlog_errno(status);
2052
		return status;
2053
	}
2054

2055
	mutex_lock(&orphan_dir_inode->i_mutex);
M
Mark Fasheh 已提交
2056
	status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
2057 2058 2059 2060 2061
	if (status < 0) {
		mlog_errno(status);
		goto out;
	}

A
Al Viro 已提交
2062
	status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx);
M
Mark Fasheh 已提交
2063 2064
	if (status) {
		mlog_errno(status);
2065
		goto out_cluster;
2066 2067
	}

M
Mark Fasheh 已提交
2068 2069
	*head = priv.head;

2070
out_cluster:
M
Mark Fasheh 已提交
2071
	ocfs2_inode_unlock(orphan_dir_inode, 0);
2072 2073
out:
	mutex_unlock(&orphan_dir_inode->i_mutex);
2074
	iput(orphan_dir_inode);
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
	return status;
}

static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
					      int slot)
{
	int ret;

	spin_lock(&osb->osb_lock);
	ret = !osb->osb_orphan_wipes[slot];
	spin_unlock(&osb->osb_lock);
	return ret;
}

static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
					     int slot)
{
	spin_lock(&osb->osb_lock);
	/* Mark ourselves such that new processes in delete_inode()
	 * know to quit early. */
	ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
	while (osb->osb_orphan_wipes[slot]) {
		/* If any processes are already in the middle of an
		 * orphan wipe on this dir, then we need to wait for
		 * them. */
		spin_unlock(&osb->osb_lock);
		wait_event_interruptible(osb->osb_wipe_event,
					 ocfs2_orphan_recovery_can_continue(osb, slot));
		spin_lock(&osb->osb_lock);
	}
	spin_unlock(&osb->osb_lock);
}

static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
					      int slot)
{
	ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
}

/*
 * Orphan recovery. Each mounted node has it's own orphan dir which we
 * must run during recovery. Our strategy here is to build a list of
 * the inodes in the orphan dir and iget/iput them. The VFS does
 * (most) of the rest of the work.
 *
 * Orphan recovery can happen at any time, not just mount so we have a
 * couple of extra considerations.
 *
 * - We grab as many inodes as we can under the orphan dir lock -
 *   doing iget() outside the orphan dir risks getting a reference on
 *   an invalid inode.
 * - We must be sure not to deadlock with other processes on the
 *   system wanting to run delete_inode(). This can happen when they go
 *   to lock the orphan dir and the orphan recovery process attempts to
 *   iget() inside the orphan dir lock. This can be avoided by
 *   advertising our state to ocfs2_delete_inode().
 */
static int ocfs2_recover_orphans(struct ocfs2_super *osb,
2133 2134
				 int slot,
				 enum ocfs2_orphan_reco_type orphan_reco_type)
2135 2136 2137 2138 2139 2140
{
	int ret = 0;
	struct inode *inode = NULL;
	struct inode *iter;
	struct ocfs2_inode_info *oi;

T
Tao Ma 已提交
2141
	trace_ocfs2_recover_orphans(slot);
2142 2143 2144 2145 2146 2147 2148 2149 2150

	ocfs2_mark_recovering_orphan_dir(osb, slot);
	ret = ocfs2_queue_orphans(osb, slot, &inode);
	ocfs2_clear_recovering_orphan_dir(osb, slot);

	/* Error here should be noted, but we want to continue with as
	 * many queued inodes as we've got. */
	if (ret)
		mlog_errno(ret);
2151 2152 2153

	while (inode) {
		oi = OCFS2_I(inode);
T
Tao Ma 已提交
2154 2155
		trace_ocfs2_recover_orphans_iput(
					(unsigned long long)oi->ip_blkno);
2156 2157

		iter = oi->ip_next_orphan;
2158
		oi->ip_next_orphan = NULL;
2159

2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
		/*
		 * We need to take and drop the inode lock to
		 * force read inode from disk.
		 */
		ret = ocfs2_inode_lock(inode, NULL, 0);
		if (ret) {
			mlog_errno(ret);
			goto next;
		}
		ocfs2_inode_unlock(inode, 0);

		if (inode->i_nlink == 0) {
			spin_lock(&oi->ip_lock);
			/* Set the proper information to get us going into
			 * ocfs2_delete_inode. */
			oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
			spin_unlock(&oi->ip_lock);
		} else if (orphan_reco_type == ORPHAN_NEED_TRUNCATE) {
			struct buffer_head *di_bh = NULL;

			ret = ocfs2_rw_lock(inode, 1);
			if (ret) {
				mlog_errno(ret);
				goto next;
			}

			ret = ocfs2_inode_lock(inode, &di_bh, 1);
			if (ret < 0) {
				ocfs2_rw_unlock(inode, 1);
				mlog_errno(ret);
				goto next;
			}

			ret = ocfs2_truncate_file(inode, di_bh,
					i_size_read(inode));
			ocfs2_inode_unlock(inode, 1);
			ocfs2_rw_unlock(inode, 1);
			brelse(di_bh);
			if (ret < 0) {
				if (ret != -ENOSPC)
					mlog_errno(ret);
				goto next;
			}

			ret = ocfs2_del_inode_from_orphan(osb, inode, 0, 0);
			if (ret)
				mlog_errno(ret);
2207 2208

			wake_up(&OCFS2_I(inode)->append_dio_wq);
2209
		} /* else if ORPHAN_NO_NEED_TRUNCATE, do nothing */
2210

2211
next:
2212 2213 2214 2215 2216
		iput(inode);

		inode = iter;
	}

2217
	return ret;
2218 2219
}

2220
static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
2221 2222 2223 2224 2225
{
	/* This check is good because ocfs2 will wait on our recovery
	 * thread before changing it to something other than MOUNTED
	 * or DISABLED. */
	wait_event(osb->osb_mount_event,
2226 2227
		  (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
		   atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
2228 2229 2230 2231 2232 2233
		   atomic_read(&osb->vol_state) == VOLUME_DISABLED);

	/* If there's an error on mount, then we may never get to the
	 * MOUNTED flag, but this is set right before
	 * dismount_volume() so we can trust it. */
	if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
T
Tao Ma 已提交
2234
		trace_ocfs2_wait_on_mount(VOLUME_DISABLED);
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
		mlog(0, "mount error, exiting!\n");
		return -EBUSY;
	}

	return 0;
}

static int ocfs2_commit_thread(void *arg)
{
	int status;
	struct ocfs2_super *osb = arg;
	struct ocfs2_journal *journal = osb->journal;

	/* we can trust j_num_trans here because _should_stop() is only set in
	 * shutdown and nobody other than ourselves should be able to start
	 * transactions.  committing on shutdown might take a few iterations
	 * as final transactions put deleted inodes on the list */
	while (!(kthread_should_stop() &&
		 atomic_read(&journal->j_num_trans) == 0)) {

2255 2256 2257
		wait_event_interruptible(osb->checkpoint_event,
					 atomic_read(&journal->j_num_trans)
					 || kthread_should_stop());
2258 2259

		status = ocfs2_commit_cache(osb);
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
		if (status < 0) {
			static unsigned long abort_warn_time;

			/* Warn about this once per minute */
			if (printk_timed_ratelimit(&abort_warn_time, 60*HZ))
				mlog(ML_ERROR, "status = %d, journal is "
						"already aborted.\n", status);
			/*
			 * After ocfs2_commit_cache() fails, j_num_trans has a
			 * non-zero value.  Sleep here to avoid a busy-wait
			 * loop.
			 */
			msleep_interruptible(1000);
		}
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285

		if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
			mlog(ML_KTHREAD,
			     "commit_thread: %u transactions pending on "
			     "shutdown\n",
			     atomic_read(&journal->j_num_trans));
		}
	}

	return 0;
}

2286 2287 2288 2289 2290
/* Reads all the journal inodes without taking any cluster locks. Used
 * for hard readonly access to determine whether any journal requires
 * recovery. Also used to refresh the recovery generation numbers after
 * a journal has been recovered by another node.
 */
2291 2292 2293 2294
int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
{
	int ret = 0;
	unsigned int slot;
2295
	struct buffer_head *di_bh = NULL;
2296
	struct ocfs2_dinode *di;
2297
	int journal_dirty = 0;
2298 2299

	for(slot = 0; slot < osb->max_slots; slot++) {
2300 2301
		ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
		if (ret) {
2302 2303 2304 2305 2306 2307
			mlog_errno(ret);
			goto out;
		}

		di = (struct ocfs2_dinode *) di_bh->b_data;

2308 2309 2310
		osb->slot_recovery_generations[slot] =
					ocfs2_get_recovery_generation(di);

2311 2312
		if (le32_to_cpu(di->id1.journal1.ij_flags) &
		    OCFS2_JOURNAL_DIRTY_FL)
2313
			journal_dirty = 1;
2314 2315

		brelse(di_bh);
2316
		di_bh = NULL;
2317 2318 2319
	}

out:
2320 2321
	if (journal_dirty)
		ret = -EROFS;
2322 2323
	return ret;
}