xref: /linux/fs/xfs/xfs_log_priv.h (revision 12017faf387437c01ff63bbe46b629550b15bd70)
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 {
244*12017fafSDavid Chinner 	sv_t		   t_wait;	 /* ticket wait queue            : 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.
317*12017fafSDavid Chinner  * - ic_forcewait 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.
327114d23aaSDavid Chinner  *
328114d23aaSDavid Chinner  * Because of cacheline contention on large machines, we need to separate
329114d23aaSDavid Chinner  * various resources onto different cachelines. To start with, make the
330114d23aaSDavid Chinner  * structure cacheline aligned. The following fields can be contended on
331114d23aaSDavid Chinner  * by independent processes:
332114d23aaSDavid Chinner  *
333114d23aaSDavid Chinner  *	- ic_callback_*
334114d23aaSDavid Chinner  *	- ic_refcnt
335114d23aaSDavid Chinner  *	- fields protected by the global l_icloglock
336114d23aaSDavid Chinner  *
337114d23aaSDavid Chinner  * so we need to ensure that these fields are located in separate cachelines.
338114d23aaSDavid Chinner  * We'll put all the read-only and l_icloglock fields in the first cacheline,
339114d23aaSDavid Chinner  * and move everything else out to subsequent cachelines.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds typedef struct xlog_iclog_fields {
342*12017fafSDavid Chinner 	sv_t			ic_force_wait;
343*12017fafSDavid Chinner 	sv_t			ic_write_wait;
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 */
353114d23aaSDavid Chinner #ifdef XFS_LOG_TRACE
354114d23aaSDavid Chinner 	struct ktrace		*ic_trace;
355114d23aaSDavid Chinner #endif
356114d23aaSDavid Chinner 
357114d23aaSDavid Chinner 	/* Callback structures need their own cacheline */
358114d23aaSDavid Chinner 	spinlock_t		ic_callback_lock ____cacheline_aligned_in_smp;
359114d23aaSDavid Chinner 	xfs_log_callback_t	*ic_callback;
360114d23aaSDavid Chinner 	xfs_log_callback_t	**ic_callback_tail;
361114d23aaSDavid Chinner 
362114d23aaSDavid Chinner 	/* reference counts need their own cacheline */
363114d23aaSDavid Chinner 	atomic_t		ic_refcnt ____cacheline_aligned_in_smp;
3644679b2d3SDavid Chinner } xlog_iclog_fields_t;
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  */
380*12017fafSDavid Chinner #define	ic_force_wait	hic_fields.ic_force_wait
381*12017fafSDavid Chinner #define ic_write_wait	hic_fields.ic_write_wait
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
387114d23aaSDavid 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 {
4054679b2d3SDavid Chinner 	/* The following fields don't need locking */
4064679b2d3SDavid Chinner 	struct xfs_mount	*l_mp;	        /* mount point */
4074679b2d3SDavid Chinner 	struct xfs_buf		*l_xbuf;        /* extra buffer for log
4084679b2d3SDavid Chinner 						 * wrapping */
4094679b2d3SDavid Chinner 	struct xfs_buftarg	*l_targ;        /* buftarg of log */
4104679b2d3SDavid Chinner 	uint			l_flags;
4114679b2d3SDavid Chinner 	uint			l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */
4124679b2d3SDavid Chinner 	struct xfs_buf_cancel	**l_buf_cancel_table;
4134679b2d3SDavid Chinner 	int			l_iclog_hsize;  /* size of iclog header */
4144679b2d3SDavid Chinner 	int			l_iclog_heads;  /* # of iclog header sectors */
4154679b2d3SDavid Chinner 	uint			l_sectbb_log;   /* log2 of sector size in BBs */
4164679b2d3SDavid Chinner 	uint			l_sectbb_mask;  /* sector size (in BBs)
4174679b2d3SDavid Chinner 						 * alignment mask */
4184679b2d3SDavid Chinner 	int			l_iclog_size;	/* size of log in bytes */
4194679b2d3SDavid Chinner 	int			l_iclog_size_log; /* log power size of log */
4204679b2d3SDavid Chinner 	int			l_iclog_bufs;	/* number of iclog buffers */
4214679b2d3SDavid Chinner 	xfs_daddr_t		l_logBBstart;   /* start block of log */
4224679b2d3SDavid Chinner 	int			l_logsize;      /* size of log in bytes */
4234679b2d3SDavid Chinner 	int			l_logBBsize;    /* size of log in BB chunks */
4244679b2d3SDavid Chinner 
4251da177e4SLinus Torvalds 	/* The following block of fields are changed while holding icloglock */
426d748c623SMatthew Wilcox 	sv_t			l_flush_wait ____cacheline_aligned_in_smp;
427d748c623SMatthew Wilcox 						/* waiting for iclog flush */
4281da177e4SLinus Torvalds 	int			l_covered_state;/* state of "covering disk
4291da177e4SLinus Torvalds 						 * log entries" */
4301da177e4SLinus Torvalds 	xlog_in_core_t		*l_iclog;       /* head log queue	*/
431b22cd72cSEric Sandeen 	spinlock_t		l_icloglock;    /* grab to change iclog state */
4321da177e4SLinus Torvalds 	xfs_lsn_t		l_tail_lsn;     /* lsn of 1st LR with unflushed
4331da177e4SLinus Torvalds 						 * buffers */
4341da177e4SLinus Torvalds 	xfs_lsn_t		l_last_sync_lsn;/* lsn of last LR on disk */
4351da177e4SLinus Torvalds 	int			l_curr_cycle;   /* Cycle number of log writes */
4361da177e4SLinus Torvalds 	int			l_prev_cycle;   /* Cycle number before last
4371da177e4SLinus Torvalds 						 * block increment */
4381da177e4SLinus Torvalds 	int			l_curr_block;   /* current logical log block */
4391da177e4SLinus Torvalds 	int			l_prev_block;   /* previous logical log block */
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	/* The following block of fields are changed while holding grant_lock */
4424679b2d3SDavid Chinner 	spinlock_t		l_grant_lock ____cacheline_aligned_in_smp;
4431da177e4SLinus Torvalds 	xlog_ticket_t		*l_reserve_headq;
4441da177e4SLinus Torvalds 	xlog_ticket_t		*l_write_headq;
4451da177e4SLinus Torvalds 	int			l_grant_reserve_cycle;
4461da177e4SLinus Torvalds 	int			l_grant_reserve_bytes;
4471da177e4SLinus Torvalds 	int			l_grant_write_cycle;
4481da177e4SLinus Torvalds 	int			l_grant_write_bytes;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds #ifdef XFS_LOG_TRACE
4511da177e4SLinus Torvalds 	struct ktrace		*l_trace;
4521da177e4SLinus Torvalds 	struct ktrace		*l_grant_trace;
4531da177e4SLinus Torvalds #endif
4544679b2d3SDavid Chinner 
4554679b2d3SDavid Chinner 	/* The following field are used for debugging; need to hold icloglock */
4564679b2d3SDavid Chinner #ifdef DEBUG
4574679b2d3SDavid Chinner 	char			*l_iclog_bak[XLOG_MAX_ICLOGS];
4584679b2d3SDavid Chinner #endif
4594679b2d3SDavid Chinner 
4601da177e4SLinus Torvalds } xlog_t;
4611da177e4SLinus Torvalds 
462cfcbbbd0SNathan Scott #define XLOG_FORCED_SHUTDOWN(log)	((log)->l_flags & XLOG_IO_ERROR)
463cfcbbbd0SNathan Scott 
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds /* common routines */
4661da177e4SLinus Torvalds extern xfs_lsn_t xlog_assign_tail_lsn(struct xfs_mount *mp);
4671da177e4SLinus Torvalds extern int	 xlog_find_tail(xlog_t	*log,
4681da177e4SLinus Torvalds 				xfs_daddr_t *head_blk,
46965be6054SEric Sandeen 				xfs_daddr_t *tail_blk);
47065be6054SEric Sandeen extern int	 xlog_recover(xlog_t *log);
4711da177e4SLinus Torvalds extern int	 xlog_recover_finish(xlog_t *log, int mfsi_flags);
4721da177e4SLinus Torvalds extern void	 xlog_pack_data(xlog_t *log, xlog_in_core_t *iclog, int);
4731da177e4SLinus Torvalds extern void	 xlog_recover_process_iunlinks(xlog_t *log);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds extern struct xfs_buf *xlog_get_bp(xlog_t *, int);
4761da177e4SLinus Torvalds extern void	 xlog_put_bp(struct xfs_buf *);
4771da177e4SLinus Torvalds extern int	 xlog_bread(xlog_t *, xfs_daddr_t, int, struct xfs_buf *);
4781da177e4SLinus Torvalds 
479eb01c9cdSDavid Chinner extern kmem_zone_t	*xfs_log_ticket_zone;
480eb01c9cdSDavid Chinner 
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