/*
 *	Copyright 1994 by Sun Microsystems, Inc.
 *	  All Rights Reserved
 */

#ifndef	_SYS_FS_UFS_PANIC_H
#define	_SYS_FS_UFS_PANIC_H

#pragma ident	"@(#)ufs_panic.h	1.7	98/07/02 SMI"

#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fs.h>

#ifdef	__cplusplus
extern "C" {
#endif

#if defined(_KERNEL)

/*
 * failures have an associated state
 *  making them bit values simplifies state transition validity checking
 */

typedef enum ufs_failure_states {
	/* initial states, set mostly by thread encountering failure */
	UF_UNDEF	= 0x0000,	/* freshly-allocated memory */
	UF_INIT		= 0x0001,	/* being created */
	UF_QUEUE	= 0x0002,	/* queued for fix thread */

	/* transitional states, set mostly by fix failure thread */
	UF_TRYLCK	= 0x0010,	/* attempting to be locked */
	UF_LOCKED	= 0x0020,	/* error lock set */
	UF_UMOUNT	= 0x0040,	/* attempting to be unmounted */
	UF_FIXING	= 0x0080,	/* fsck started; attempting unlock */

	/* terminal states, once in this state, fix failure thread is done */
	UF_FIXED	= 0x0100,	/* no problemo, man */
	UF_NOTFIX	= 0x0200,	/* can't fix; shouldn't panic */
	UF_REPLICA	= 0x0400,	/* replica panic; fix original only */
	UF_PANIC	= 0x0800,	/* gave up/exceeded limits/can't fix */
					/* not strictly a terminal state, */
					/* because we will do some actions */
					/* if we find a failure in this state */
					/* but those actions will be terminal */

	/* handy, but used only as terminators and placeholders */
	UF_ILLEGAL	= 0xffff,	/* invalid state */
	UF_ALLSTATES	= 0x0ff3	/* all possible state */
} ufs_failure_states_t;

/*
 * each manifestation of a fault (ie. "panic") is
 * associated with a distinct ufs_failure event
 */

typedef struct ufs_failure
{
	struct ufs_failure	*uf_chain[2];	/* protected by ufs_elock mux */
	struct ufs_failure	*uf_orig;	/* if duplicate event, */
						/* here's the original */
	struct ufs_failure	*uf_master;	/* if sharing a logged device */
						/* here's the master failure */
	struct buf		*uf_bp;		/* ptr to buf containing sb */
	kmutex_t		*uf_vfs_lockp;	/* ptr to vfs_lock */
	struct vfs_ufsfx	*uf_vfs_ufsfxp;	/* ptr to fix-on-panic per fs */
	struct vfs		*uf_vfsp;	/* ptr to vfs */
	struct ufsvfs		*uf_ufsvfsp;	/* to match if unmounted */
	dev_t			 uf_dev;	/* device id */
	ufs_failure_states_t	 uf_s;		/* current failure state */
	int			 uf_flags;	/* internal flags */
	time_t			 uf_begin_tm;	/* when did panic begin? */
	time_t			 uf_end_tm;	/* ... end? */
	time_t			 uf_entered_tm;	/* ... was state entered? */
	struct lockfs		 uf_lf;		/* needed to set lockfs lock */
	int			 uf_lf_err;	/* errno if lockfs fails  */
	long			 uf_retry;	/* seconds */
	unsigned		 uf_counter;	/* of state-specific actions */
	kmutex_t		 uf_mutex;	/* protects struct body */
	char		uf_fsname[MAXMNTLEN];	/* for post-unmount errors */
						/* after ufsvfsp is free'd */
	char uf_panic_str[LOCKFS_MAXCOMMENTLEN]; /* original panic message */
						/* XXX could be smaller */
} ufs_failure_t;

#define	uf_next	uf_chain[0]
#define	uf_prev	uf_chain[1]
#define	uf_fs	uf_bp->b_un.b_fs

/*
 * per-filesystem panic event state
 */
typedef struct vfs_ufsfx {
	long		 fx_flags;		/* see ufs_panic.h for the */
	ufs_failure_t	*fx_current;		/* currently being fixed */
} vfs_ufsfx_t;

/*
 * External entry points
 *
 *  ufs_fault(vnode_t *, char *fmt, ...)
 *	replaces calls to cmn_err(CE_PANIC, char *fmt, ...)
 *  	The vnode is any vnode in the filesystem.
 *	ufs_fault returns an errno to bubble up.
 *  ufsfx_init()
 *	is called at modload time to set global values etc.
 *  ufsfx_mount()
 *	is called at mount time to do per-fs initialization
 *	returns 0 (ok) or errno
 *  ufsfx_unmount()
 *	is called at unmount time to prevent spinning on work
 *	to fix an unmounted fs
 *  ufsfx_lockfs()
 *  ufsfx_unlockfs()
 *      are called at upon (un)locking of a fs for coordination
 *  ufsfx_get_failure_qlen()
 *	is called by the hlock thread to coordinate with the fix
 *	failure thread
 */

/*PRINTFLIKE2*/
int	ufs_fault(vnode_t *, char *fmt, ...);
void	ufsfx_init(void);
int	ufsfx_mount(struct ufsvfs *, int);
void	ufsfx_unmount(struct ufsvfs *);
void	ufsfx_lockfs(struct ufsvfs *);
void	ufsfx_unlockfs(struct ufsvfs *);
int	ufsfx_get_failure_qlen(void);

extern struct ufs_q ufs_fix;

#endif /* _KERNEL */

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_FS_UFS_PANIC_H */
