1153323Srodrigc/*
2159451Srodrigc * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3159451Srodrigc * All Rights Reserved.
4153323Srodrigc *
5159451Srodrigc * This program is free software; you can redistribute it and/or
6159451Srodrigc * modify it under the terms of the GNU General Public License as
7153323Srodrigc * published by the Free Software Foundation.
8153323Srodrigc *
9159451Srodrigc * This program is distributed in the hope that it would be useful,
10159451Srodrigc * but WITHOUT ANY WARRANTY; without even the implied warranty of
11159451Srodrigc * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12159451Srodrigc * GNU General Public License for more details.
13153323Srodrigc *
14159451Srodrigc * You should have received a copy of the GNU General Public License
15159451Srodrigc * along with this program; if not, write the Free Software Foundation,
16159451Srodrigc * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17153323Srodrigc */
18153323Srodrigc#ifndef	__XFS_LOG_PRIV_H__
19153323Srodrigc#define __XFS_LOG_PRIV_H__
20153323Srodrigc
21153323Srodrigcstruct xfs_buf;
22153323Srodrigcstruct ktrace;
23153323Srodrigcstruct log;
24159451Srodrigcstruct xlog_ticket;
25153323Srodrigcstruct xfs_buf_cancel;
26153323Srodrigcstruct xfs_mount;
27153323Srodrigc
28153323Srodrigc/*
29153323Srodrigc * Macros, structures, prototypes for internal log manager use.
30153323Srodrigc */
31153323Srodrigc
32153323Srodrigc#define XLOG_MIN_ICLOGS		2
33153323Srodrigc#define XLOG_MED_ICLOGS		4
34153323Srodrigc#define XLOG_MAX_ICLOGS		8
35153323Srodrigc#define XLOG_CALLBACK_SIZE	10
36153323Srodrigc#define XLOG_HEADER_MAGIC_NUM	0xFEEDbabe	/* Invalid cycle number */
37153323Srodrigc#define XLOG_VERSION_1		1
38153323Srodrigc#define XLOG_VERSION_2		2		/* Large IClogs, Log sunit */
39153323Srodrigc#define XLOG_VERSION_OKBITS	(XLOG_VERSION_1 | XLOG_VERSION_2)
40153323Srodrigc#define XLOG_RECORD_BSIZE	(16*1024)	/* eventually 32k */
41153323Srodrigc#define XLOG_BIG_RECORD_BSIZE	(32*1024)	/* 32k buffers */
42153323Srodrigc#define XLOG_MAX_RECORD_BSIZE	(256*1024)
43153323Srodrigc#define XLOG_HEADER_CYCLE_SIZE	(32*1024)	/* cycle data in header */
44153323Srodrigc#define XLOG_RECORD_BSHIFT	14		/* 16384 == 1 << 14 */
45153323Srodrigc#define XLOG_BIG_RECORD_BSHIFT	15		/* 32k == 1 << 15 */
46153323Srodrigc#define XLOG_MAX_RECORD_BSHIFT	18		/* 256k == 1 << 18 */
47153323Srodrigc#define XLOG_BTOLSUNIT(log, b)  (((b)+(log)->l_mp->m_sb.sb_logsunit-1) / \
48153323Srodrigc                                 (log)->l_mp->m_sb.sb_logsunit)
49153323Srodrigc#define XLOG_LSUNITTOB(log, su) ((su) * (log)->l_mp->m_sb.sb_logsunit)
50153323Srodrigc
51153323Srodrigc#define XLOG_HEADER_SIZE	512
52153323Srodrigc
53153323Srodrigc#define XLOG_REC_SHIFT(log) \
54153323Srodrigc	BTOBB(1 << (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? \
55153323Srodrigc	 XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT))
56153323Srodrigc#define XLOG_TOTAL_REC_SHIFT(log) \
57153323Srodrigc	BTOBB(XLOG_MAX_ICLOGS << (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? \
58153323Srodrigc	 XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT))
59153323Srodrigc
60153323Srodrigc/*
61153323Srodrigc *  set lsns
62153323Srodrigc */
63153323Srodrigc
64159451Srodrigc#define ASSIGN_ANY_LSN_HOST(lsn,cycle,block)  \
65153323Srodrigc    { \
66159451Srodrigc	(lsn) = ((xfs_lsn_t)(cycle)<<32)|(block); \
67153323Srodrigc    }
68159451Srodrigc#define ASSIGN_ANY_LSN_DISK(lsn,cycle,block)  \
69159451Srodrigc    { \
70159451Srodrigc	INT_SET(((uint *)&(lsn))[0], ARCH_CONVERT, (cycle)); \
71159451Srodrigc	INT_SET(((uint *)&(lsn))[1], ARCH_CONVERT, (block)); \
72159451Srodrigc    }
73159451Srodrigc#define ASSIGN_LSN(lsn,log) \
74159451Srodrigc    ASSIGN_ANY_LSN_DISK(lsn,(log)->l_curr_cycle,(log)->l_curr_block);
75153323Srodrigc
76153323Srodrigc#define XLOG_SET(f,b)		(((f) & (b)) == (b))
77153323Srodrigc
78153323Srodrigc#define GET_CYCLE(ptr, arch) \
79153323Srodrigc    (INT_GET(*(uint *)(ptr), arch) == XLOG_HEADER_MAGIC_NUM ? \
80153323Srodrigc	 INT_GET(*((uint *)(ptr)+1), arch) : \
81153323Srodrigc	 INT_GET(*(uint *)(ptr), arch) \
82153323Srodrigc    )
83153323Srodrigc
84153323Srodrigc#define BLK_AVG(blk1, blk2)	((blk1+blk2) >> 1)
85153323Srodrigc
86153323Srodrigc
87153323Srodrigc#ifdef __KERNEL__
88153323Srodrigc
89153323Srodrigc/*
90153323Srodrigc * get client id from packed copy.
91153323Srodrigc *
92153323Srodrigc * this hack is here because the xlog_pack code copies four bytes
93153323Srodrigc * of xlog_op_header containing the fields oh_clientid, oh_flags
94153323Srodrigc * and oh_res2 into the packed copy.
95153323Srodrigc *
96153323Srodrigc * later on this four byte chunk is treated as an int and the
97153323Srodrigc * client id is pulled out.
98153323Srodrigc *
99153323Srodrigc * this has endian issues, of course.
100153323Srodrigc */
101153323Srodrigc
102159451Srodrigc#ifndef XFS_NATIVE_HOST
103153323Srodrigc#define GET_CLIENT_ID(i,arch) \
104153323Srodrigc    ((i) & 0xff)
105153323Srodrigc#else
106153323Srodrigc#define GET_CLIENT_ID(i,arch) \
107153323Srodrigc    ((i) >> 24)
108153323Srodrigc#endif
109153323Srodrigc
110153323Srodrigc#define GRANT_LOCK(log)		mutex_spinlock(&(log)->l_grant_lock)
111153323Srodrigc#define GRANT_UNLOCK(log, s)	mutex_spinunlock(&(log)->l_grant_lock, s)
112153323Srodrigc#define LOG_LOCK(log)		mutex_spinlock(&(log)->l_icloglock)
113153323Srodrigc#define LOG_UNLOCK(log, s)	mutex_spinunlock(&(log)->l_icloglock, s)
114153323Srodrigc
115153323Srodrigc#define xlog_panic(args...)	cmn_err(CE_PANIC, ## args)
116153323Srodrigc#define xlog_exit(args...)	cmn_err(CE_PANIC, ## args)
117153323Srodrigc#define xlog_warn(args...)	cmn_err(CE_WARN, ## args)
118153323Srodrigc
119153323Srodrigc/*
120153323Srodrigc * In core log state
121153323Srodrigc */
122153323Srodrigc#define XLOG_STATE_ACTIVE    0x0001 /* Current IC log being written to */
123153323Srodrigc#define XLOG_STATE_WANT_SYNC 0x0002 /* Want to sync this iclog; no more writes */
124153323Srodrigc#define XLOG_STATE_SYNCING   0x0004 /* This IC log is syncing */
125153323Srodrigc#define XLOG_STATE_DONE_SYNC 0x0008 /* Done syncing to disk */
126153323Srodrigc#define XLOG_STATE_DO_CALLBACK \
127153323Srodrigc			     0x0010 /* Process callback functions */
128153323Srodrigc#define XLOG_STATE_CALLBACK  0x0020 /* Callback functions now */
129153323Srodrigc#define XLOG_STATE_DIRTY     0x0040 /* Dirty IC log, not ready for ACTIVE status*/
130153323Srodrigc#define XLOG_STATE_IOERROR   0x0080 /* IO error happened in sync'ing log */
131153323Srodrigc#define XLOG_STATE_ALL	     0x7FFF /* All possible valid flags */
132153323Srodrigc#define XLOG_STATE_NOTUSED   0x8000 /* This IC log not being used */
133153323Srodrigc#endif	/* __KERNEL__ */
134153323Srodrigc
135153323Srodrigc/*
136153323Srodrigc * Flags to log operation header
137153323Srodrigc *
138153323Srodrigc * The first write of a new transaction will be preceded with a start
139153323Srodrigc * record, XLOG_START_TRANS.  Once a transaction is committed, a commit
140153323Srodrigc * record is written, XLOG_COMMIT_TRANS.  If a single region can not fit into
141153323Srodrigc * the remainder of the current active in-core log, it is split up into
142153323Srodrigc * multiple regions.  Each partial region will be marked with a
143153323Srodrigc * XLOG_CONTINUE_TRANS until the last one, which gets marked with XLOG_END_TRANS.
144153323Srodrigc *
145153323Srodrigc */
146153323Srodrigc#define XLOG_START_TRANS	0x01	/* Start a new transaction */
147153323Srodrigc#define XLOG_COMMIT_TRANS	0x02	/* Commit this transaction */
148153323Srodrigc#define XLOG_CONTINUE_TRANS	0x04	/* Cont this trans into new region */
149153323Srodrigc#define XLOG_WAS_CONT_TRANS	0x08	/* Cont this trans into new region */
150153323Srodrigc#define XLOG_END_TRANS		0x10	/* End a continued transaction */
151153323Srodrigc#define XLOG_UNMOUNT_TRANS	0x20	/* Unmount a filesystem transaction */
152153323Srodrigc#define XLOG_SKIP_TRANS		(XLOG_COMMIT_TRANS | XLOG_CONTINUE_TRANS | \
153153323Srodrigc				 XLOG_WAS_CONT_TRANS | XLOG_END_TRANS | \
154153323Srodrigc				 XLOG_UNMOUNT_TRANS)
155153323Srodrigc
156153323Srodrigc#ifdef __KERNEL__
157153323Srodrigc/*
158153323Srodrigc * Flags to log ticket
159153323Srodrigc */
160153323Srodrigc#define XLOG_TIC_INITED		0x1	/* has been initialized */
161153323Srodrigc#define XLOG_TIC_PERM_RESERV	0x2	/* permanent reservation */
162153323Srodrigc#define XLOG_TIC_IN_Q		0x4
163153323Srodrigc#endif	/* __KERNEL__ */
164153323Srodrigc
165153323Srodrigc#define XLOG_UNMOUNT_TYPE	0x556e	/* Un for Unmount */
166153323Srodrigc
167153323Srodrigc/*
168153323Srodrigc * Flags for log structure
169153323Srodrigc */
170153323Srodrigc#define XLOG_CHKSUM_MISMATCH	0x1	/* used only during recovery */
171153323Srodrigc#define XLOG_ACTIVE_RECOVERY	0x2	/* in the middle of recovery */
172153323Srodrigc#define	XLOG_RECOVERY_NEEDED	0x4	/* log was recovered */
173153323Srodrigc#define XLOG_IO_ERROR		0x8	/* log hit an I/O error, and being
174153323Srodrigc					   shutdown */
175153323Srodrigctypedef __uint32_t xlog_tid_t;
176153323Srodrigc
177153323Srodrigc
178153323Srodrigc#ifdef __KERNEL__
179153323Srodrigc/*
180153323Srodrigc * Below are states for covering allocation transactions.
181153323Srodrigc * By covering, we mean changing the h_tail_lsn in the last on-disk
182153323Srodrigc * log write such that no allocation transactions will be re-done during
183153323Srodrigc * recovery after a system crash. Recovery starts at the last on-disk
184153323Srodrigc * log write.
185153323Srodrigc *
186153323Srodrigc * These states are used to insert dummy log entries to cover
187153323Srodrigc * space allocation transactions which can undo non-transactional changes
188153323Srodrigc * after a crash. Writes to a file with space
189153323Srodrigc * already allocated do not result in any transactions. Allocations
190153323Srodrigc * might include space beyond the EOF. So if we just push the EOF a
191153323Srodrigc * little, the last transaction for the file could contain the wrong
192153323Srodrigc * size. If there is no file system activity, after an allocation
193153323Srodrigc * transaction, and the system crashes, the allocation transaction
194153323Srodrigc * will get replayed and the file will be truncated. This could
195153323Srodrigc * be hours/days/... after the allocation occurred.
196153323Srodrigc *
197153323Srodrigc * The fix for this is to do two dummy transactions when the
198153323Srodrigc * system is idle. We need two dummy transaction because the h_tail_lsn
199153323Srodrigc * in the log record header needs to point beyond the last possible
200153323Srodrigc * non-dummy transaction. The first dummy changes the h_tail_lsn to
201153323Srodrigc * the first transaction before the dummy. The second dummy causes
202153323Srodrigc * h_tail_lsn to point to the first dummy. Recovery starts at h_tail_lsn.
203153323Srodrigc *
204153323Srodrigc * These dummy transactions get committed when everything
205153323Srodrigc * is idle (after there has been some activity).
206153323Srodrigc *
207153323Srodrigc * There are 5 states used to control this.
208153323Srodrigc *
209153323Srodrigc *  IDLE -- no logging has been done on the file system or
210153323Srodrigc *		we are done covering previous transactions.
211153323Srodrigc *  NEED -- logging has occurred and we need a dummy transaction
212153323Srodrigc *		when the log becomes idle.
213153323Srodrigc *  DONE -- we were in the NEED state and have committed a dummy
214153323Srodrigc *		transaction.
215153323Srodrigc *  NEED2 -- we detected that a dummy transaction has gone to the
216153323Srodrigc *		on disk log with no other transactions.
217153323Srodrigc *  DONE2 -- we committed a dummy transaction when in the NEED2 state.
218153323Srodrigc *
219153323Srodrigc * There are two places where we switch states:
220153323Srodrigc *
221153323Srodrigc * 1.) In xfs_sync, when we detect an idle log and are in NEED or NEED2.
222153323Srodrigc *	We commit the dummy transaction and switch to DONE or DONE2,
223153323Srodrigc *	respectively. In all other states, we don't do anything.
224153323Srodrigc *
225153323Srodrigc * 2.) When we finish writing the on-disk log (xlog_state_clean_log).
226153323Srodrigc *
227153323Srodrigc *	No matter what state we are in, if this isn't the dummy
228153323Srodrigc *	transaction going out, the next state is NEED.
229153323Srodrigc *	So, if we aren't in the DONE or DONE2 states, the next state
230153323Srodrigc *	is NEED. We can't be finishing a write of the dummy record
231153323Srodrigc *	unless it was committed and the state switched to DONE or DONE2.
232153323Srodrigc *
233153323Srodrigc *	If we are in the DONE state and this was a write of the
234153323Srodrigc *		dummy transaction, we move to NEED2.
235153323Srodrigc *
236153323Srodrigc *	If we are in the DONE2 state and this was a write of the
237153323Srodrigc *		dummy transaction, we move to IDLE.
238153323Srodrigc *
239153323Srodrigc *
240153323Srodrigc * Writing only one dummy transaction can get appended to
241153323Srodrigc * one file space allocation. When this happens, the log recovery
242153323Srodrigc * code replays the space allocation and a file could be truncated.
243153323Srodrigc * This is why we have the NEED2 and DONE2 states before going idle.
244153323Srodrigc */
245153323Srodrigc
246153323Srodrigc#define XLOG_STATE_COVER_IDLE	0
247153323Srodrigc#define XLOG_STATE_COVER_NEED	1
248153323Srodrigc#define XLOG_STATE_COVER_DONE	2
249153323Srodrigc#define XLOG_STATE_COVER_NEED2	3
250153323Srodrigc#define XLOG_STATE_COVER_DONE2	4
251153323Srodrigc
252153323Srodrigc#define XLOG_COVER_OPS		5
253153323Srodrigc
254159451Srodrigc
255159451Srodrigc/* Ticket reservation region accounting */
256159451Srodrigc#define XLOG_TIC_LEN_MAX	15
257159451Srodrigc#define XLOG_TIC_RESET_RES(t) ((t)->t_res_num = \
258159451Srodrigc				(t)->t_res_arr_sum = (t)->t_res_num_ophdrs = 0)
259159451Srodrigc#define XLOG_TIC_ADD_OPHDR(t) ((t)->t_res_num_ophdrs++)
260159451Srodrigc#define XLOG_TIC_ADD_REGION(t, len, type)				\
261159451Srodrigc	do {								\
262159451Srodrigc		if ((t)->t_res_num == XLOG_TIC_LEN_MAX) { 		\
263159451Srodrigc			/* add to overflow and start again */		\
264159451Srodrigc			(t)->t_res_o_flow += (t)->t_res_arr_sum;	\
265159451Srodrigc			(t)->t_res_num = 0;				\
266159451Srodrigc			(t)->t_res_arr_sum = 0;				\
267159451Srodrigc		}							\
268159451Srodrigc		(t)->t_res_arr[(t)->t_res_num].r_len = (len);		\
269159451Srodrigc		(t)->t_res_arr[(t)->t_res_num].r_type = (type);		\
270159451Srodrigc		(t)->t_res_arr_sum += (len);				\
271159451Srodrigc		(t)->t_res_num++;					\
272159451Srodrigc	} while (0)
273159451Srodrigc
274159451Srodrigc/*
275159451Srodrigc * Reservation region
276159451Srodrigc * As would be stored in xfs_log_iovec but without the i_addr which
277159451Srodrigc * we don't care about.
278159451Srodrigc */
279159451Srodrigctypedef struct xlog_res {
280159451Srodrigc	uint	r_len;	/* region length		:4 */
281159451Srodrigc	uint	r_type;	/* region's transaction type	:4 */
282159451Srodrigc} xlog_res_t;
283159451Srodrigc
284153323Srodrigctypedef struct xlog_ticket {
285159451Srodrigc	sv_t		   t_sema;	 /* sleep on this semaphore      : 20 */
286159451Srodrigc 	struct xlog_ticket *t_next;	 /*			         :4|8 */
287159451Srodrigc	struct xlog_ticket *t_prev;	 /*				 :4|8 */
288159451Srodrigc	xlog_tid_t	   t_tid;	 /* transaction identifier	 : 4  */
289159451Srodrigc	int		   t_curr_res;	 /* current reservation in bytes : 4  */
290159451Srodrigc	int		   t_unit_res;	 /* unit reservation in bytes    : 4  */
291159451Srodrigc	char		   t_ocnt;	 /* original count		 : 1  */
292159451Srodrigc	char		   t_cnt;	 /* current count		 : 1  */
293159451Srodrigc	char		   t_clientid;	 /* who does this belong to;	 : 1  */
294159451Srodrigc	char		   t_flags;	 /* properties of reservation	 : 1  */
295159451Srodrigc	uint		   t_trans_type; /* transaction type             : 4  */
296159451Srodrigc
297159451Srodrigc        /* reservation array fields */
298159451Srodrigc	uint		   t_res_num;                    /* num in array : 4 */
299159451Srodrigc	uint		   t_res_num_ophdrs;		 /* num op hdrs  : 4 */
300159451Srodrigc	uint		   t_res_arr_sum;		 /* array sum    : 4 */
301159451Srodrigc	uint		   t_res_o_flow;		 /* sum overflow : 4 */
302159451Srodrigc	xlog_res_t	   t_res_arr[XLOG_TIC_LEN_MAX];  /* array of res : 8 * 15 */
303153323Srodrigc} xlog_ticket_t;
304159451Srodrigc
305153323Srodrigc#endif
306153323Srodrigc
307153323Srodrigc
308153323Srodrigctypedef struct xlog_op_header {
309153323Srodrigc	xlog_tid_t oh_tid;	/* transaction id of operation	:  4 b */
310153323Srodrigc	int	   oh_len;	/* bytes in data region		:  4 b */
311153323Srodrigc	__uint8_t  oh_clientid;	/* who sent me this		:  1 b */
312153323Srodrigc	__uint8_t  oh_flags;	/*				:  1 b */
313153323Srodrigc	ushort	   oh_res2;	/* 32 bit align			:  2 b */
314153323Srodrigc} xlog_op_header_t;
315153323Srodrigc
316153323Srodrigc
317153323Srodrigc/* valid values for h_fmt */
318153323Srodrigc#define XLOG_FMT_UNKNOWN  0
319153323Srodrigc#define XLOG_FMT_LINUX_LE 1
320153323Srodrigc#define XLOG_FMT_LINUX_BE 2
321153323Srodrigc#define XLOG_FMT_IRIX_BE  3
322153323Srodrigc
323153323Srodrigc/* our fmt */
324159451Srodrigc#ifdef XFS_NATIVE_HOST
325153323Srodrigc#define XLOG_FMT XLOG_FMT_LINUX_BE
326153323Srodrigc#else
327159451Srodrigc#define XLOG_FMT XLOG_FMT_LINUX_LE
328153323Srodrigc#endif
329153323Srodrigc
330153323Srodrigctypedef struct xlog_rec_header {
331153323Srodrigc	uint	  h_magicno;	/* log record (LR) identifier		:  4 */
332153323Srodrigc	uint	  h_cycle;	/* write cycle of log			:  4 */
333153323Srodrigc	int	  h_version;	/* LR version				:  4 */
334153323Srodrigc	int	  h_len;	/* len in bytes; should be 64-bit aligned: 4 */
335153323Srodrigc	xfs_lsn_t h_lsn;	/* lsn of this LR			:  8 */
336153323Srodrigc	xfs_lsn_t h_tail_lsn;	/* lsn of 1st LR w/ buffers not committed: 8 */
337153323Srodrigc	uint	  h_chksum;	/* may not be used; non-zero if used	:  4 */
338153323Srodrigc	int	  h_prev_block; /* block number to previous LR		:  4 */
339153323Srodrigc	int	  h_num_logops;	/* number of log operations in this LR	:  4 */
340153323Srodrigc	uint	  h_cycle_data[XLOG_HEADER_CYCLE_SIZE / BBSIZE];
341153323Srodrigc	/* new fields */
342153323Srodrigc	int       h_fmt;        /* format of log record                 :  4 */
343153323Srodrigc	uuid_t    h_fs_uuid;    /* uuid of FS                           : 16 */
344153323Srodrigc	int       h_size;	/* iclog size				:  4 */
345153323Srodrigc} xlog_rec_header_t;
346153323Srodrigc
347153323Srodrigctypedef struct xlog_rec_ext_header {
348153323Srodrigc	uint	  xh_cycle;	/* write cycle of log			: 4 */
349153323Srodrigc	uint	  xh_cycle_data[XLOG_HEADER_CYCLE_SIZE / BBSIZE]; /*	: 256 */
350153323Srodrigc} xlog_rec_ext_header_t;
351153323Srodrigc
352153323Srodrigc#ifdef __KERNEL__
353153323Srodrigc/*
354153323Srodrigc * - A log record header is 512 bytes.  There is plenty of room to grow the
355153323Srodrigc *	xlog_rec_header_t into the reserved space.
356153323Srodrigc * - ic_data follows, so a write to disk can start at the beginning of
357153323Srodrigc *	the iclog.
358153323Srodrigc * - ic_forcesema is used to implement synchronous forcing of the iclog to disk.
359153323Srodrigc * - ic_next is the pointer to the next iclog in the ring.
360153323Srodrigc * - ic_bp is a pointer to the buffer used to write this incore log to disk.
361153323Srodrigc * - ic_log is a pointer back to the global log structure.
362153323Srodrigc * - ic_callback is a linked list of callback function/argument pairs to be
363153323Srodrigc *	called after an iclog finishes writing.
364153323Srodrigc * - ic_size is the full size of the header plus data.
365153323Srodrigc * - ic_offset is the current number of bytes written to in this iclog.
366153323Srodrigc * - ic_refcnt is bumped when someone is writing to the log.
367153323Srodrigc * - ic_state is the state of the iclog.
368153323Srodrigc */
369153323Srodrigctypedef struct xlog_iclog_fields {
370153323Srodrigc	sv_t			ic_forcesema;
371153323Srodrigc	sv_t			ic_writesema;
372153323Srodrigc	struct xlog_in_core	*ic_next;
373153323Srodrigc	struct xlog_in_core	*ic_prev;
374153323Srodrigc	struct xfs_buf		*ic_bp;
375153323Srodrigc	struct log		*ic_log;
376153323Srodrigc	xfs_log_callback_t	*ic_callback;
377153323Srodrigc	xfs_log_callback_t	**ic_callback_tail;
378153323Srodrigc#ifdef XFS_LOG_TRACE
379153323Srodrigc	struct ktrace		*ic_trace;
380153323Srodrigc#endif
381153323Srodrigc	int			ic_size;
382153323Srodrigc	int			ic_offset;
383153323Srodrigc	int			ic_refcnt;
384153323Srodrigc	int			ic_bwritecnt;
385153323Srodrigc	ushort_t		ic_state;
386153323Srodrigc	char			*ic_datap;	/* pointer to iclog data */
387153323Srodrigc} xlog_iclog_fields_t;
388153323Srodrigc
389153323Srodrigctypedef union xlog_in_core2 {
390153323Srodrigc	xlog_rec_header_t	hic_header;
391153323Srodrigc	xlog_rec_ext_header_t	hic_xheader;
392153323Srodrigc	char			hic_sector[XLOG_HEADER_SIZE];
393153323Srodrigc} xlog_in_core_2_t;
394153323Srodrigc
395153323Srodrigctypedef struct xlog_in_core {
396153323Srodrigc	xlog_iclog_fields_t	hic_fields;
397153323Srodrigc	xlog_in_core_2_t	*hic_data;
398153323Srodrigc} xlog_in_core_t;
399153323Srodrigc
400153323Srodrigc/*
401153323Srodrigc * Defines to save our code from this glop.
402153323Srodrigc */
403153323Srodrigc#define	ic_forcesema	hic_fields.ic_forcesema
404153323Srodrigc#define ic_writesema	hic_fields.ic_writesema
405153323Srodrigc#define	ic_next		hic_fields.ic_next
406153323Srodrigc#define	ic_prev		hic_fields.ic_prev
407153323Srodrigc#define	ic_bp		hic_fields.ic_bp
408153323Srodrigc#define	ic_log		hic_fields.ic_log
409153323Srodrigc#define	ic_callback	hic_fields.ic_callback
410153323Srodrigc#define	ic_callback_tail hic_fields.ic_callback_tail
411153323Srodrigc#define	ic_trace	hic_fields.ic_trace
412153323Srodrigc#define	ic_size		hic_fields.ic_size
413153323Srodrigc#define	ic_offset	hic_fields.ic_offset
414153323Srodrigc#define	ic_refcnt	hic_fields.ic_refcnt
415153323Srodrigc#define	ic_bwritecnt	hic_fields.ic_bwritecnt
416153323Srodrigc#define	ic_state	hic_fields.ic_state
417153323Srodrigc#define ic_datap	hic_fields.ic_datap
418153323Srodrigc#define ic_header	hic_data->hic_header
419153323Srodrigc
420153323Srodrigc/*
421153323Srodrigc * The reservation head lsn is not made up of a cycle number and block number.
422153323Srodrigc * Instead, it uses a cycle number and byte number.  Logs don't expect to
423153323Srodrigc * overflow 31 bits worth of byte offset, so using a byte number will mean
424153323Srodrigc * that round off problems won't occur when releasing partial reservations.
425153323Srodrigc */
426153323Srodrigctypedef struct log {
427153323Srodrigc	/* The following block of fields are changed while holding icloglock */
428153323Srodrigc	sema_t			l_flushsema;    /* iclog flushing semaphore */
429153323Srodrigc	int			l_flushcnt;	/* # of procs waiting on this
430153323Srodrigc						 * sema */
431153323Srodrigc	int			l_ticket_cnt;	/* free ticket count */
432153323Srodrigc	int			l_ticket_tcnt;	/* total ticket count */
433153323Srodrigc	int			l_covered_state;/* state of "covering disk
434153323Srodrigc						 * log entries" */
435153323Srodrigc	xlog_ticket_t		*l_freelist;    /* free list of tickets */
436153323Srodrigc	xlog_ticket_t		*l_unmount_free;/* kmem_free these addresses */
437153323Srodrigc	xlog_ticket_t		*l_tail;        /* free list of tickets */
438153323Srodrigc	xlog_in_core_t		*l_iclog;       /* head log queue	*/
439153323Srodrigc	lock_t			l_icloglock;    /* grab to change iclog state */
440153323Srodrigc	xfs_lsn_t		l_tail_lsn;     /* lsn of 1st LR with unflushed
441153323Srodrigc						 * buffers */
442153323Srodrigc	xfs_lsn_t		l_last_sync_lsn;/* lsn of last LR on disk */
443153323Srodrigc	struct xfs_mount	*l_mp;	        /* mount point */
444153323Srodrigc	struct xfs_buf		*l_xbuf;        /* extra buffer for log
445153323Srodrigc						 * wrapping */
446153323Srodrigc	struct xfs_buftarg	*l_targ;        /* buftarg of log */
447153323Srodrigc	xfs_daddr_t		l_logBBstart;   /* start block of log */
448153323Srodrigc	int			l_logsize;      /* size of log in bytes */
449153323Srodrigc	int			l_logBBsize;    /* size of log in BB chunks */
450153323Srodrigc	int			l_curr_cycle;   /* Cycle number of log writes */
451153323Srodrigc	int			l_prev_cycle;   /* Cycle number before last
452153323Srodrigc						 * block increment */
453153323Srodrigc	int			l_curr_block;   /* current logical log block */
454153323Srodrigc	int			l_prev_block;   /* previous logical log block */
455153323Srodrigc	int			l_iclog_size;	/* size of log in bytes */
456153323Srodrigc	int			l_iclog_size_log; /* log power size of log */
457153323Srodrigc	int			l_iclog_bufs;	/* number of iclog buffers */
458153323Srodrigc
459153323Srodrigc	/* The following field are used for debugging; need to hold icloglock */
460153323Srodrigc	char			*l_iclog_bak[XLOG_MAX_ICLOGS];
461153323Srodrigc
462153323Srodrigc	/* The following block of fields are changed while holding grant_lock */
463153323Srodrigc	lock_t			l_grant_lock;
464153323Srodrigc	xlog_ticket_t		*l_reserve_headq;
465153323Srodrigc	xlog_ticket_t		*l_write_headq;
466153323Srodrigc	int			l_grant_reserve_cycle;
467153323Srodrigc	int			l_grant_reserve_bytes;
468153323Srodrigc	int			l_grant_write_cycle;
469153323Srodrigc	int			l_grant_write_bytes;
470153323Srodrigc
471153323Srodrigc	/* The following fields don't need locking */
472153323Srodrigc#ifdef XFS_LOG_TRACE
473153323Srodrigc	struct ktrace		*l_trace;
474153323Srodrigc	struct ktrace		*l_grant_trace;
475153323Srodrigc#endif
476153323Srodrigc	uint			l_flags;
477153323Srodrigc	uint			l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */
478153323Srodrigc	struct xfs_buf_cancel	**l_buf_cancel_table;
479153323Srodrigc	int			l_iclog_hsize;  /* size of iclog header */
480153323Srodrigc	int			l_iclog_heads;  /* # of iclog header sectors */
481153323Srodrigc	uint			l_sectbb_log;   /* log2 of sector size in BBs */
482153323Srodrigc	uint			l_sectbb_mask;  /* sector size (in BBs)
483153323Srodrigc						 * alignment mask */
484153323Srodrigc} xlog_t;
485153323Srodrigc
486159451Srodrigc#define XLOG_FORCED_SHUTDOWN(log)	((log)->l_flags & XLOG_IO_ERROR)
487153323Srodrigc
488159451Srodrigc
489153323Srodrigc/* common routines */
490153323Srodrigcextern xfs_lsn_t xlog_assign_tail_lsn(struct xfs_mount *mp);
491153323Srodrigcextern int	 xlog_find_tail(xlog_t	*log,
492153323Srodrigc				xfs_daddr_t *head_blk,
493159451Srodrigc				xfs_daddr_t *tail_blk);
494159451Srodrigcextern int	 xlog_recover(xlog_t *log);
495153323Srodrigcextern int	 xlog_recover_finish(xlog_t *log, int mfsi_flags);
496159451Srodrigcextern void	 xlog_pack_data(xlog_t *log, xlog_in_core_t *iclog, int);
497153323Srodrigcextern void	 xlog_recover_process_iunlinks(xlog_t *log);
498153323Srodrigc
499153323Srodrigcextern struct xfs_buf *xlog_get_bp(xlog_t *, int);
500153323Srodrigcextern void	 xlog_put_bp(struct xfs_buf *);
501153323Srodrigcextern int	 xlog_bread(xlog_t *, xfs_daddr_t, int, struct xfs_buf *);
502153323Srodrigc
503159451Srodrigc/* iclog tracing */
504153323Srodrigc#define XLOG_TRACE_GRAB_FLUSH  1
505153323Srodrigc#define XLOG_TRACE_REL_FLUSH   2
506153323Srodrigc#define XLOG_TRACE_SLEEP_FLUSH 3
507153323Srodrigc#define XLOG_TRACE_WAKE_FLUSH  4
508153323Srodrigc
509153323Srodrigc#endif	/* __KERNEL__ */
510153323Srodrigc
511153323Srodrigc#endif	/* __XFS_LOG_PRIV_H__ */
512