xref: /linux/fs/xfs/xfs_log_priv.h (revision 114d23aae51233b2bc62d8e2a632bcb55de1953d)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #ifndef	__XFS_LOG_PRIV_H__
191da177e4SLinus Torvalds #define __XFS_LOG_PRIV_H__
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds struct xfs_buf;
221da177e4SLinus Torvalds struct ktrace;
231da177e4SLinus Torvalds struct log;
24a844f451SNathan Scott struct xlog_ticket;
251da177e4SLinus Torvalds struct xfs_buf_cancel;
261da177e4SLinus Torvalds struct xfs_mount;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * Macros, structures, prototypes for internal log manager use.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define XLOG_MIN_ICLOGS		2
331da177e4SLinus Torvalds #define XLOG_MAX_ICLOGS		8
341da177e4SLinus Torvalds #define XLOG_HEADER_MAGIC_NUM	0xFEEDbabe	/* Invalid cycle number */
351da177e4SLinus Torvalds #define XLOG_VERSION_1		1
361da177e4SLinus Torvalds #define XLOG_VERSION_2		2		/* Large IClogs, Log sunit */
371da177e4SLinus Torvalds #define XLOG_VERSION_OKBITS	(XLOG_VERSION_1 | XLOG_VERSION_2)
38dcb3b83fSEric Sandeen #define XLOG_MIN_RECORD_BSIZE	(16*1024)	/* eventually 32k */
391da177e4SLinus Torvalds #define XLOG_BIG_RECORD_BSIZE	(32*1024)	/* 32k buffers */
401da177e4SLinus Torvalds #define XLOG_MAX_RECORD_BSIZE	(256*1024)
411da177e4SLinus Torvalds #define XLOG_HEADER_CYCLE_SIZE	(32*1024)	/* cycle data in header */
42dcb3b83fSEric Sandeen #define XLOG_MIN_RECORD_BSHIFT	14		/* 16384 == 1 << 14 */
431da177e4SLinus Torvalds #define XLOG_BIG_RECORD_BSHIFT	15		/* 32k == 1 << 15 */
441da177e4SLinus Torvalds #define XLOG_MAX_RECORD_BSHIFT	18		/* 256k == 1 << 18 */
451da177e4SLinus Torvalds #define XLOG_BTOLSUNIT(log, b)  (((b)+(log)->l_mp->m_sb.sb_logsunit-1) / \
461da177e4SLinus Torvalds                                  (log)->l_mp->m_sb.sb_logsunit)
471da177e4SLinus Torvalds #define XLOG_LSUNITTOB(log, su) ((su) * (log)->l_mp->m_sb.sb_logsunit)
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #define XLOG_HEADER_SIZE	512
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define XLOG_REC_SHIFT(log) \
5262118709SEric Sandeen 	BTOBB(1 << (xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? \
531da177e4SLinus Torvalds 	 XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT))
541da177e4SLinus Torvalds #define XLOG_TOTAL_REC_SHIFT(log) \
5562118709SEric Sandeen 	BTOBB(XLOG_MAX_ICLOGS << (xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? \
561da177e4SLinus Torvalds 	 XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT))
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 
5903bea6feSChristoph Hellwig static inline xfs_lsn_t xlog_assign_lsn(uint cycle, uint block)
6003bea6feSChristoph Hellwig {
6103bea6feSChristoph Hellwig 	return ((xfs_lsn_t)cycle << 32) | block;
621da177e4SLinus Torvalds }
6303bea6feSChristoph Hellwig 
6403bea6feSChristoph Hellwig static inline uint xlog_get_cycle(char *ptr)
6503bea6feSChristoph Hellwig {
66b53e675dSChristoph Hellwig 	if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
67b53e675dSChristoph Hellwig 		return be32_to_cpu(*((__be32 *)ptr + 1));
6803bea6feSChristoph Hellwig 	else
69b53e675dSChristoph Hellwig 		return be32_to_cpu(*(__be32 *)ptr);
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define BLK_AVG(blk1, blk2)	((blk1+blk2) >> 1)
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #ifdef __KERNEL__
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds  * get client id from packed copy.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * this hack is here because the xlog_pack code copies four bytes
801da177e4SLinus Torvalds  * of xlog_op_header containing the fields oh_clientid, oh_flags
811da177e4SLinus Torvalds  * and oh_res2 into the packed copy.
821da177e4SLinus Torvalds  *
831da177e4SLinus Torvalds  * later on this four byte chunk is treated as an int and the
841da177e4SLinus Torvalds  * client id is pulled out.
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  * this has endian issues, of course.
871da177e4SLinus Torvalds  */
88b53e675dSChristoph Hellwig static inline uint xlog_get_client_id(__be32 i)
8903bea6feSChristoph Hellwig {
90b53e675dSChristoph Hellwig 	return be32_to_cpu(i) >> 24;
9103bea6feSChristoph Hellwig }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define xlog_panic(args...)	cmn_err(CE_PANIC, ## args)
941da177e4SLinus Torvalds #define xlog_exit(args...)	cmn_err(CE_PANIC, ## args)
951da177e4SLinus Torvalds #define xlog_warn(args...)	cmn_err(CE_WARN, ## args)
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds  * In core log state
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds #define XLOG_STATE_ACTIVE    0x0001 /* Current IC log being written to */
1011da177e4SLinus Torvalds #define XLOG_STATE_WANT_SYNC 0x0002 /* Want to sync this iclog; no more writes */
1021da177e4SLinus Torvalds #define XLOG_STATE_SYNCING   0x0004 /* This IC log is syncing */
1031da177e4SLinus Torvalds #define XLOG_STATE_DONE_SYNC 0x0008 /* Done syncing to disk */
1041da177e4SLinus Torvalds #define XLOG_STATE_DO_CALLBACK \
1051da177e4SLinus Torvalds 			     0x0010 /* Process callback functions */
1061da177e4SLinus Torvalds #define XLOG_STATE_CALLBACK  0x0020 /* Callback functions now */
1071da177e4SLinus Torvalds #define XLOG_STATE_DIRTY     0x0040 /* Dirty IC log, not ready for ACTIVE status*/
1081da177e4SLinus Torvalds #define XLOG_STATE_IOERROR   0x0080 /* IO error happened in sync'ing log */
1091da177e4SLinus Torvalds #define XLOG_STATE_ALL	     0x7FFF /* All possible valid flags */
1101da177e4SLinus Torvalds #define XLOG_STATE_NOTUSED   0x8000 /* This IC log not being used */
1111da177e4SLinus Torvalds #endif	/* __KERNEL__ */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /*
1141da177e4SLinus Torvalds  * Flags to log operation header
1151da177e4SLinus Torvalds  *
1161da177e4SLinus Torvalds  * The first write of a new transaction will be preceded with a start
1171da177e4SLinus Torvalds  * record, XLOG_START_TRANS.  Once a transaction is committed, a commit
1181da177e4SLinus Torvalds  * record is written, XLOG_COMMIT_TRANS.  If a single region can not fit into
1191da177e4SLinus Torvalds  * the remainder of the current active in-core log, it is split up into
1201da177e4SLinus Torvalds  * multiple regions.  Each partial region will be marked with a
1211da177e4SLinus Torvalds  * XLOG_CONTINUE_TRANS until the last one, which gets marked with XLOG_END_TRANS.
1221da177e4SLinus Torvalds  *
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds #define XLOG_START_TRANS	0x01	/* Start a new transaction */
1251da177e4SLinus Torvalds #define XLOG_COMMIT_TRANS	0x02	/* Commit this transaction */
1261da177e4SLinus Torvalds #define XLOG_CONTINUE_TRANS	0x04	/* Cont this trans into new region */
1271da177e4SLinus Torvalds #define XLOG_WAS_CONT_TRANS	0x08	/* Cont this trans into new region */
1281da177e4SLinus Torvalds #define XLOG_END_TRANS		0x10	/* End a continued transaction */
1291da177e4SLinus Torvalds #define XLOG_UNMOUNT_TRANS	0x20	/* Unmount a filesystem transaction */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #ifdef __KERNEL__
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * Flags to log ticket
1341da177e4SLinus Torvalds  */
1351da177e4SLinus Torvalds #define XLOG_TIC_INITED		0x1	/* has been initialized */
1361da177e4SLinus Torvalds #define XLOG_TIC_PERM_RESERV	0x2	/* permanent reservation */
1371da177e4SLinus Torvalds #define XLOG_TIC_IN_Q		0x4
1381da177e4SLinus Torvalds #endif	/* __KERNEL__ */
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds #define XLOG_UNMOUNT_TYPE	0x556e	/* Un for Unmount */
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /*
1431da177e4SLinus Torvalds  * Flags for log structure
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds #define XLOG_CHKSUM_MISMATCH	0x1	/* used only during recovery */
1461da177e4SLinus Torvalds #define XLOG_ACTIVE_RECOVERY	0x2	/* in the middle of recovery */
1471da177e4SLinus Torvalds #define	XLOG_RECOVERY_NEEDED	0x4	/* log was recovered */
1481da177e4SLinus Torvalds #define XLOG_IO_ERROR		0x8	/* log hit an I/O error, and being
1491da177e4SLinus Torvalds 					   shutdown */
1501da177e4SLinus Torvalds typedef __uint32_t xlog_tid_t;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds #ifdef __KERNEL__
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds  * Below are states for covering allocation transactions.
1561da177e4SLinus Torvalds  * By covering, we mean changing the h_tail_lsn in the last on-disk
1571da177e4SLinus Torvalds  * log write such that no allocation transactions will be re-done during
1581da177e4SLinus Torvalds  * recovery after a system crash. Recovery starts at the last on-disk
1591da177e4SLinus Torvalds  * log write.
1601da177e4SLinus Torvalds  *
1611da177e4SLinus Torvalds  * These states are used to insert dummy log entries to cover
1621da177e4SLinus Torvalds  * space allocation transactions which can undo non-transactional changes
1631da177e4SLinus Torvalds  * after a crash. Writes to a file with space
1641da177e4SLinus Torvalds  * already allocated do not result in any transactions. Allocations
1651da177e4SLinus Torvalds  * might include space beyond the EOF. So if we just push the EOF a
1661da177e4SLinus Torvalds  * little, the last transaction for the file could contain the wrong
1671da177e4SLinus Torvalds  * size. If there is no file system activity, after an allocation
1681da177e4SLinus Torvalds  * transaction, and the system crashes, the allocation transaction
1691da177e4SLinus Torvalds  * will get replayed and the file will be truncated. This could
1701da177e4SLinus Torvalds  * be hours/days/... after the allocation occurred.
1711da177e4SLinus Torvalds  *
1721da177e4SLinus Torvalds  * The fix for this is to do two dummy transactions when the
1731da177e4SLinus Torvalds  * system is idle. We need two dummy transaction because the h_tail_lsn
1741da177e4SLinus Torvalds  * in the log record header needs to point beyond the last possible
1751da177e4SLinus Torvalds  * non-dummy transaction. The first dummy changes the h_tail_lsn to
1761da177e4SLinus Torvalds  * the first transaction before the dummy. The second dummy causes
1771da177e4SLinus Torvalds  * h_tail_lsn to point to the first dummy. Recovery starts at h_tail_lsn.
1781da177e4SLinus Torvalds  *
1791da177e4SLinus Torvalds  * These dummy transactions get committed when everything
1801da177e4SLinus Torvalds  * is idle (after there has been some activity).
1811da177e4SLinus Torvalds  *
1821da177e4SLinus Torvalds  * There are 5 states used to control this.
1831da177e4SLinus Torvalds  *
1841da177e4SLinus Torvalds  *  IDLE -- no logging has been done on the file system or
1851da177e4SLinus Torvalds  *		we are done covering previous transactions.
1861da177e4SLinus Torvalds  *  NEED -- logging has occurred and we need a dummy transaction
1871da177e4SLinus Torvalds  *		when the log becomes idle.
1881da177e4SLinus Torvalds  *  DONE -- we were in the NEED state and have committed a dummy
1891da177e4SLinus Torvalds  *		transaction.
1901da177e4SLinus Torvalds  *  NEED2 -- we detected that a dummy transaction has gone to the
1911da177e4SLinus Torvalds  *		on disk log with no other transactions.
1921da177e4SLinus Torvalds  *  DONE2 -- we committed a dummy transaction when in the NEED2 state.
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * There are two places where we switch states:
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  * 1.) In xfs_sync, when we detect an idle log and are in NEED or NEED2.
1971da177e4SLinus Torvalds  *	We commit the dummy transaction and switch to DONE or DONE2,
1981da177e4SLinus Torvalds  *	respectively. In all other states, we don't do anything.
1991da177e4SLinus Torvalds  *
2001da177e4SLinus Torvalds  * 2.) When we finish writing the on-disk log (xlog_state_clean_log).
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  *	No matter what state we are in, if this isn't the dummy
2031da177e4SLinus Torvalds  *	transaction going out, the next state is NEED.
2041da177e4SLinus Torvalds  *	So, if we aren't in the DONE or DONE2 states, the next state
2051da177e4SLinus Torvalds  *	is NEED. We can't be finishing a write of the dummy record
2061da177e4SLinus Torvalds  *	unless it was committed and the state switched to DONE or DONE2.
2071da177e4SLinus Torvalds  *
2081da177e4SLinus Torvalds  *	If we are in the DONE state and this was a write of the
2091da177e4SLinus Torvalds  *		dummy transaction, we move to NEED2.
2101da177e4SLinus Torvalds  *
2111da177e4SLinus Torvalds  *	If we are in the DONE2 state and this was a write of the
2121da177e4SLinus Torvalds  *		dummy transaction, we move to IDLE.
2131da177e4SLinus Torvalds  *
2141da177e4SLinus Torvalds  *
2151da177e4SLinus Torvalds  * Writing only one dummy transaction can get appended to
2161da177e4SLinus Torvalds  * one file space allocation. When this happens, the log recovery
2171da177e4SLinus Torvalds  * code replays the space allocation and a file could be truncated.
2181da177e4SLinus Torvalds  * This is why we have the NEED2 and DONE2 states before going idle.
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds #define XLOG_STATE_COVER_IDLE	0
2221da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED	1
2231da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE	2
2241da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED2	3
2251da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE2	4
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds #define XLOG_COVER_OPS		5
2281da177e4SLinus Torvalds 
2297e9c6396STim Shimmin 
2307e9c6396STim Shimmin /* Ticket reservation region accounting */
2317e9c6396STim Shimmin #define XLOG_TIC_LEN_MAX	15
2327e9c6396STim Shimmin 
2337e9c6396STim Shimmin /*
2347e9c6396STim Shimmin  * Reservation region
2357e9c6396STim Shimmin  * As would be stored in xfs_log_iovec but without the i_addr which
2367e9c6396STim Shimmin  * we don't care about.
2377e9c6396STim Shimmin  */
2387e9c6396STim Shimmin typedef struct xlog_res {
2391259845dSTim Shimmin 	uint	r_len;	/* region length		:4 */
2401259845dSTim Shimmin 	uint	r_type;	/* region's transaction type	:4 */
2417e9c6396STim Shimmin } xlog_res_t;
2427e9c6396STim Shimmin 
2431da177e4SLinus Torvalds typedef struct xlog_ticket {
2441da177e4SLinus Torvalds 	sv_t		   t_sema;	 /* sleep on this semaphore      : 20 */
2457e9c6396STim Shimmin  	struct xlog_ticket *t_next;	 /*			         :4|8 */
2467e9c6396STim Shimmin 	struct xlog_ticket *t_prev;	 /*				 :4|8 */
2471da177e4SLinus Torvalds 	xlog_tid_t	   t_tid;	 /* transaction identifier	 : 4  */
2481da177e4SLinus Torvalds 	int		   t_curr_res;	 /* current reservation in bytes : 4  */
2491da177e4SLinus Torvalds 	int		   t_unit_res;	 /* unit reservation in bytes    : 4  */
2507e9c6396STim Shimmin 	char		   t_ocnt;	 /* original count		 : 1  */
2517e9c6396STim Shimmin 	char		   t_cnt;	 /* current count		 : 1  */
2527e9c6396STim Shimmin 	char		   t_clientid;	 /* who does this belong to;	 : 1  */
2537e9c6396STim Shimmin 	char		   t_flags;	 /* properties of reservation	 : 1  */
2547e9c6396STim Shimmin 	uint		   t_trans_type; /* transaction type             : 4  */
2557e9c6396STim Shimmin 
2567e9c6396STim Shimmin         /* reservation array fields */
2577e9c6396STim Shimmin 	uint		   t_res_num;                    /* num in array : 4 */
2587e9c6396STim Shimmin 	uint		   t_res_num_ophdrs;		 /* num op hdrs  : 4 */
2597e9c6396STim Shimmin 	uint		   t_res_arr_sum;		 /* array sum    : 4 */
2607e9c6396STim Shimmin 	uint		   t_res_o_flow;		 /* sum overflow : 4 */
2611259845dSTim Shimmin 	xlog_res_t	   t_res_arr[XLOG_TIC_LEN_MAX];  /* array of res : 8 * 15 */
2621da177e4SLinus Torvalds } xlog_ticket_t;
2637e9c6396STim Shimmin 
2641da177e4SLinus Torvalds #endif
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds typedef struct xlog_op_header {
26867fcb7bfSChristoph Hellwig 	__be32	   oh_tid;	/* transaction id of operation	:  4 b */
26967fcb7bfSChristoph Hellwig 	__be32	   oh_len;	/* bytes in data region		:  4 b */
27067fcb7bfSChristoph Hellwig 	__u8	   oh_clientid;	/* who sent me this		:  1 b */
27167fcb7bfSChristoph Hellwig 	__u8	   oh_flags;	/*				:  1 b */
27267fcb7bfSChristoph Hellwig 	__u16	   oh_res2;	/* 32 bit align			:  2 b */
2731da177e4SLinus Torvalds } xlog_op_header_t;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds /* valid values for h_fmt */
2771da177e4SLinus Torvalds #define XLOG_FMT_UNKNOWN  0
2781da177e4SLinus Torvalds #define XLOG_FMT_LINUX_LE 1
2791da177e4SLinus Torvalds #define XLOG_FMT_LINUX_BE 2
2801da177e4SLinus Torvalds #define XLOG_FMT_IRIX_BE  3
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /* our fmt */
283f016bad6SNathan Scott #ifdef XFS_NATIVE_HOST
2841da177e4SLinus Torvalds #define XLOG_FMT XLOG_FMT_LINUX_BE
2851da177e4SLinus Torvalds #else
286f016bad6SNathan Scott #define XLOG_FMT XLOG_FMT_LINUX_LE
2871da177e4SLinus Torvalds #endif
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds typedef struct xlog_rec_header {
290b53e675dSChristoph Hellwig 	__be32	  h_magicno;	/* log record (LR) identifier		:  4 */
291b53e675dSChristoph Hellwig 	__be32	  h_cycle;	/* write cycle of log			:  4 */
292b53e675dSChristoph Hellwig 	__be32	  h_version;	/* LR version				:  4 */
293b53e675dSChristoph Hellwig 	__be32	  h_len;	/* len in bytes; should be 64-bit aligned: 4 */
294b53e675dSChristoph Hellwig 	__be64	  h_lsn;	/* lsn of this LR			:  8 */
295b53e675dSChristoph Hellwig 	__be64	  h_tail_lsn;	/* lsn of 1st LR w/ buffers not committed: 8 */
296b53e675dSChristoph Hellwig 	__be32	  h_chksum;	/* may not be used; non-zero if used	:  4 */
297b53e675dSChristoph Hellwig 	__be32	  h_prev_block; /* block number to previous LR		:  4 */
298b53e675dSChristoph Hellwig 	__be32	  h_num_logops;	/* number of log operations in this LR	:  4 */
299b53e675dSChristoph Hellwig 	__be32	  h_cycle_data[XLOG_HEADER_CYCLE_SIZE / BBSIZE];
3001da177e4SLinus Torvalds 	/* new fields */
301b53e675dSChristoph Hellwig 	__be32    h_fmt;        /* format of log record                 :  4 */
3021da177e4SLinus Torvalds 	uuid_t	  h_fs_uuid;    /* uuid of FS                           : 16 */
303b53e675dSChristoph Hellwig 	__be32	  h_size;	/* iclog size				:  4 */
3041da177e4SLinus Torvalds } xlog_rec_header_t;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds typedef struct xlog_rec_ext_header {
307b53e675dSChristoph Hellwig 	__be32	  xh_cycle;	/* write cycle of log			: 4 */
308b53e675dSChristoph Hellwig 	__be32	  xh_cycle_data[XLOG_HEADER_CYCLE_SIZE / BBSIZE]; /*	: 256 */
3091da177e4SLinus Torvalds } xlog_rec_ext_header_t;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds #ifdef __KERNEL__
3121da177e4SLinus Torvalds /*
3131da177e4SLinus Torvalds  * - A log record header is 512 bytes.  There is plenty of room to grow the
3141da177e4SLinus Torvalds  *	xlog_rec_header_t into the reserved space.
3151da177e4SLinus Torvalds  * - ic_data follows, so a write to disk can start at the beginning of
3161da177e4SLinus Torvalds  *	the iclog.
3171da177e4SLinus Torvalds  * - ic_forcesema is used to implement synchronous forcing of the iclog to disk.
3181da177e4SLinus Torvalds  * - ic_next is the pointer to the next iclog in the ring.
3191da177e4SLinus Torvalds  * - ic_bp is a pointer to the buffer used to write this incore log to disk.
3201da177e4SLinus Torvalds  * - ic_log is a pointer back to the global log structure.
3211da177e4SLinus Torvalds  * - ic_callback is a linked list of callback function/argument pairs to be
3221da177e4SLinus Torvalds  *	called after an iclog finishes writing.
3231da177e4SLinus Torvalds  * - ic_size is the full size of the header plus data.
3241da177e4SLinus Torvalds  * - ic_offset is the current number of bytes written to in this iclog.
3251da177e4SLinus Torvalds  * - ic_refcnt is bumped when someone is writing to the log.
3261da177e4SLinus Torvalds  * - ic_state is the state of the iclog.
327*114d23aaSDavid Chinner  *
328*114d23aaSDavid Chinner  * Because of cacheline contention on large machines, we need to separate
329*114d23aaSDavid Chinner  * various resources onto different cachelines. To start with, make the
330*114d23aaSDavid Chinner  * structure cacheline aligned. The following fields can be contended on
331*114d23aaSDavid Chinner  * by independent processes:
332*114d23aaSDavid Chinner  *
333*114d23aaSDavid Chinner  *	- ic_callback_*
334*114d23aaSDavid Chinner  *	- ic_refcnt
335*114d23aaSDavid Chinner  *	- fields protected by the global l_icloglock
336*114d23aaSDavid Chinner  *
337*114d23aaSDavid Chinner  * so we need to ensure that these fields are located in separate cachelines.
338*114d23aaSDavid Chinner  * We'll put all the read-only and l_icloglock fields in the first cacheline,
339*114d23aaSDavid Chinner  * and move everything else out to subsequent cachelines.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds typedef struct xlog_iclog_fields {
3421da177e4SLinus Torvalds 	sv_t			ic_forcesema;
3431da177e4SLinus Torvalds 	sv_t			ic_writesema;
3441da177e4SLinus Torvalds 	struct xlog_in_core	*ic_next;
3451da177e4SLinus Torvalds 	struct xlog_in_core	*ic_prev;
3461da177e4SLinus Torvalds 	struct xfs_buf		*ic_bp;
3471da177e4SLinus Torvalds 	struct log		*ic_log;
3481da177e4SLinus Torvalds 	int			ic_size;
3491da177e4SLinus Torvalds 	int			ic_offset;
3501da177e4SLinus Torvalds 	int			ic_bwritecnt;
3511da177e4SLinus Torvalds 	ushort_t		ic_state;
3521da177e4SLinus Torvalds 	char			*ic_datap;	/* pointer to iclog data */
353*114d23aaSDavid Chinner #ifdef XFS_LOG_TRACE
354*114d23aaSDavid Chinner 	struct ktrace		*ic_trace;
355*114d23aaSDavid Chinner #endif
356*114d23aaSDavid Chinner 
357*114d23aaSDavid Chinner 	/* Callback structures need their own cacheline */
358*114d23aaSDavid Chinner 	spinlock_t		ic_callback_lock ____cacheline_aligned_in_smp;
359*114d23aaSDavid Chinner 	xfs_log_callback_t	*ic_callback;
360*114d23aaSDavid Chinner 	xfs_log_callback_t	**ic_callback_tail;
361*114d23aaSDavid Chinner 
362*114d23aaSDavid Chinner 	/* reference counts need their own cacheline */
363*114d23aaSDavid Chinner 	atomic_t		ic_refcnt ____cacheline_aligned_in_smp;
364*114d23aaSDavid Chinner } xlog_iclog_fields_t ____cacheline_aligned_in_smp;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds typedef union xlog_in_core2 {
3671da177e4SLinus Torvalds 	xlog_rec_header_t	hic_header;
3681da177e4SLinus Torvalds 	xlog_rec_ext_header_t	hic_xheader;
3691da177e4SLinus Torvalds 	char			hic_sector[XLOG_HEADER_SIZE];
3701da177e4SLinus Torvalds } xlog_in_core_2_t;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds typedef struct xlog_in_core {
3731da177e4SLinus Torvalds 	xlog_iclog_fields_t	hic_fields;
3741da177e4SLinus Torvalds 	xlog_in_core_2_t	*hic_data;
3751da177e4SLinus Torvalds } xlog_in_core_t;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /*
3781da177e4SLinus Torvalds  * Defines to save our code from this glop.
3791da177e4SLinus Torvalds  */
3801da177e4SLinus Torvalds #define	ic_forcesema	hic_fields.ic_forcesema
3811da177e4SLinus Torvalds #define ic_writesema	hic_fields.ic_writesema
3821da177e4SLinus Torvalds #define	ic_next		hic_fields.ic_next
3831da177e4SLinus Torvalds #define	ic_prev		hic_fields.ic_prev
3841da177e4SLinus Torvalds #define	ic_bp		hic_fields.ic_bp
3851da177e4SLinus Torvalds #define	ic_log		hic_fields.ic_log
3861da177e4SLinus Torvalds #define	ic_callback	hic_fields.ic_callback
387*114d23aaSDavid Chinner #define	ic_callback_lock hic_fields.ic_callback_lock
3881da177e4SLinus Torvalds #define	ic_callback_tail hic_fields.ic_callback_tail
3891da177e4SLinus Torvalds #define	ic_trace	hic_fields.ic_trace
3901da177e4SLinus Torvalds #define	ic_size		hic_fields.ic_size
3911da177e4SLinus Torvalds #define	ic_offset	hic_fields.ic_offset
3921da177e4SLinus Torvalds #define	ic_refcnt	hic_fields.ic_refcnt
3931da177e4SLinus Torvalds #define	ic_bwritecnt	hic_fields.ic_bwritecnt
3941da177e4SLinus Torvalds #define	ic_state	hic_fields.ic_state
3951da177e4SLinus Torvalds #define ic_datap	hic_fields.ic_datap
3961da177e4SLinus Torvalds #define ic_header	hic_data->hic_header
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  * The reservation head lsn is not made up of a cycle number and block number.
4001da177e4SLinus Torvalds  * Instead, it uses a cycle number and byte number.  Logs don't expect to
4011da177e4SLinus Torvalds  * overflow 31 bits worth of byte offset, so using a byte number will mean
4021da177e4SLinus Torvalds  * that round off problems won't occur when releasing partial reservations.
4031da177e4SLinus Torvalds  */
4041da177e4SLinus Torvalds typedef struct log {
4051da177e4SLinus Torvalds 	/* The following block of fields are changed while holding icloglock */
4061da177e4SLinus Torvalds 	sema_t			l_flushsema;    /* iclog flushing semaphore */
4071da177e4SLinus Torvalds 	int			l_flushcnt;	/* # of procs waiting on this
4081da177e4SLinus Torvalds 						 * sema */
4091da177e4SLinus Torvalds 	int			l_ticket_cnt;	/* free ticket count */
4101da177e4SLinus Torvalds 	int			l_ticket_tcnt;	/* total ticket count */
4111da177e4SLinus Torvalds 	int			l_covered_state;/* state of "covering disk
4121da177e4SLinus Torvalds 						 * log entries" */
4131da177e4SLinus Torvalds 	xlog_ticket_t		*l_freelist;    /* free list of tickets */
4141da177e4SLinus Torvalds 	xlog_ticket_t		*l_unmount_free;/* kmem_free these addresses */
4151da177e4SLinus Torvalds 	xlog_ticket_t		*l_tail;        /* free list of tickets */
4161da177e4SLinus Torvalds 	xlog_in_core_t		*l_iclog;       /* head log queue	*/
417b22cd72cSEric Sandeen 	spinlock_t		l_icloglock;    /* grab to change iclog state */
4181da177e4SLinus Torvalds 	xfs_lsn_t		l_tail_lsn;     /* lsn of 1st LR with unflushed
4191da177e4SLinus Torvalds 						 * buffers */
4201da177e4SLinus Torvalds 	xfs_lsn_t		l_last_sync_lsn;/* lsn of last LR on disk */
4211da177e4SLinus Torvalds 	struct xfs_mount	*l_mp;	        /* mount point */
4221da177e4SLinus Torvalds 	struct xfs_buf		*l_xbuf;        /* extra buffer for log
4231da177e4SLinus Torvalds 						 * wrapping */
4241da177e4SLinus Torvalds 	struct xfs_buftarg	*l_targ;        /* buftarg of log */
4251da177e4SLinus Torvalds 	xfs_daddr_t		l_logBBstart;   /* start block of log */
4261da177e4SLinus Torvalds 	int			l_logsize;      /* size of log in bytes */
4271da177e4SLinus Torvalds 	int			l_logBBsize;    /* size of log in BB chunks */
4281da177e4SLinus Torvalds 	int			l_curr_cycle;   /* Cycle number of log writes */
4291da177e4SLinus Torvalds 	int			l_prev_cycle;   /* Cycle number before last
4301da177e4SLinus Torvalds 						 * block increment */
4311da177e4SLinus Torvalds 	int			l_curr_block;   /* current logical log block */
4321da177e4SLinus Torvalds 	int			l_prev_block;   /* previous logical log block */
4331da177e4SLinus Torvalds 	int			l_iclog_size;	/* size of log in bytes */
4341da177e4SLinus Torvalds 	int			l_iclog_size_log; /* log power size of log */
4351da177e4SLinus Torvalds 	int			l_iclog_bufs;	/* number of iclog buffers */
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	/* The following field are used for debugging; need to hold icloglock */
4381da177e4SLinus Torvalds 	char			*l_iclog_bak[XLOG_MAX_ICLOGS];
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	/* The following block of fields are changed while holding grant_lock */
441c8b5ea28SEric Sandeen 	spinlock_t		l_grant_lock;
4421da177e4SLinus Torvalds 	xlog_ticket_t		*l_reserve_headq;
4431da177e4SLinus Torvalds 	xlog_ticket_t		*l_write_headq;
4441da177e4SLinus Torvalds 	int			l_grant_reserve_cycle;
4451da177e4SLinus Torvalds 	int			l_grant_reserve_bytes;
4461da177e4SLinus Torvalds 	int			l_grant_write_cycle;
4471da177e4SLinus Torvalds 	int			l_grant_write_bytes;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	/* The following fields don't need locking */
4501da177e4SLinus Torvalds #ifdef XFS_LOG_TRACE
4511da177e4SLinus Torvalds 	struct ktrace		*l_trace;
4521da177e4SLinus Torvalds 	struct ktrace		*l_grant_trace;
4531da177e4SLinus Torvalds #endif
4541da177e4SLinus Torvalds 	uint			l_flags;
4551da177e4SLinus Torvalds 	uint			l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */
4561da177e4SLinus Torvalds 	struct xfs_buf_cancel	**l_buf_cancel_table;
4571da177e4SLinus Torvalds 	int			l_iclog_hsize;  /* size of iclog header */
4581da177e4SLinus Torvalds 	int			l_iclog_heads;  /* # of iclog header sectors */
4591da177e4SLinus Torvalds 	uint			l_sectbb_log;   /* log2 of sector size in BBs */
4601da177e4SLinus Torvalds 	uint			l_sectbb_mask;  /* sector size (in BBs)
4611da177e4SLinus Torvalds 						 * alignment mask */
4621da177e4SLinus Torvalds } xlog_t;
4631da177e4SLinus Torvalds 
464cfcbbbd0SNathan Scott #define XLOG_FORCED_SHUTDOWN(log)	((log)->l_flags & XLOG_IO_ERROR)
465cfcbbbd0SNathan Scott 
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /* common routines */
4681da177e4SLinus Torvalds extern xfs_lsn_t xlog_assign_tail_lsn(struct xfs_mount *mp);
4691da177e4SLinus Torvalds extern int	 xlog_find_tail(xlog_t	*log,
4701da177e4SLinus Torvalds 				xfs_daddr_t *head_blk,
47165be6054SEric Sandeen 				xfs_daddr_t *tail_blk);
47265be6054SEric Sandeen extern int	 xlog_recover(xlog_t *log);
4731da177e4SLinus Torvalds extern int	 xlog_recover_finish(xlog_t *log, int mfsi_flags);
4741da177e4SLinus Torvalds extern void	 xlog_pack_data(xlog_t *log, xlog_in_core_t *iclog, int);
4751da177e4SLinus Torvalds extern void	 xlog_recover_process_iunlinks(xlog_t *log);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds extern struct xfs_buf *xlog_get_bp(xlog_t *, int);
4781da177e4SLinus Torvalds extern void	 xlog_put_bp(struct xfs_buf *);
4791da177e4SLinus Torvalds extern int	 xlog_bread(xlog_t *, xfs_daddr_t, int, struct xfs_buf *);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds /* iclog tracing */
4821da177e4SLinus Torvalds #define XLOG_TRACE_GRAB_FLUSH  1
4831da177e4SLinus Torvalds #define XLOG_TRACE_REL_FLUSH   2
4841da177e4SLinus Torvalds #define XLOG_TRACE_SLEEP_FLUSH 3
4851da177e4SLinus Torvalds #define XLOG_TRACE_WAKE_FLUSH  4
4861da177e4SLinus Torvalds 
487955e47adSTim Shimmin /*
488955e47adSTim Shimmin  * Unmount record type is used as a pseudo transaction type for the ticket.
489955e47adSTim Shimmin  * It's value must be outside the range of XFS_TRANS_* values.
490955e47adSTim Shimmin  */
491955e47adSTim Shimmin #define XLOG_UNMOUNT_REC_TYPE	(-1U)
492955e47adSTim Shimmin 
4931da177e4SLinus Torvalds #endif	/* __KERNEL__ */
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds #endif	/* __XFS_LOG_PRIV_H__ */
496