xref: /linux/fs/xfs/xfs_log_priv.h (revision 84f3c683c4d3f36d3c3ed320babd960a332ac458)
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 log;
23a844f451SNathan Scott struct xlog_ticket;
241da177e4SLinus Torvalds struct xfs_mount;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  * Macros, structures, prototypes for internal log manager use.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define XLOG_MIN_ICLOGS		2
311da177e4SLinus Torvalds #define XLOG_MAX_ICLOGS		8
321da177e4SLinus Torvalds #define XLOG_HEADER_MAGIC_NUM	0xFEEDbabe	/* Invalid cycle number */
331da177e4SLinus Torvalds #define XLOG_VERSION_1		1
341da177e4SLinus Torvalds #define XLOG_VERSION_2		2		/* Large IClogs, Log sunit */
351da177e4SLinus Torvalds #define XLOG_VERSION_OKBITS	(XLOG_VERSION_1 | XLOG_VERSION_2)
36dcb3b83fSEric Sandeen #define XLOG_MIN_RECORD_BSIZE	(16*1024)	/* eventually 32k */
371da177e4SLinus Torvalds #define XLOG_BIG_RECORD_BSIZE	(32*1024)	/* 32k buffers */
381da177e4SLinus Torvalds #define XLOG_MAX_RECORD_BSIZE	(256*1024)
391da177e4SLinus Torvalds #define XLOG_HEADER_CYCLE_SIZE	(32*1024)	/* cycle data in header */
40dcb3b83fSEric Sandeen #define XLOG_MIN_RECORD_BSHIFT	14		/* 16384 == 1 << 14 */
411da177e4SLinus Torvalds #define XLOG_BIG_RECORD_BSHIFT	15		/* 32k == 1 << 15 */
421da177e4SLinus Torvalds #define XLOG_MAX_RECORD_BSHIFT	18		/* 256k == 1 << 18 */
431da177e4SLinus Torvalds #define XLOG_BTOLSUNIT(log, b)  (((b)+(log)->l_mp->m_sb.sb_logsunit-1) / \
441da177e4SLinus Torvalds                                  (log)->l_mp->m_sb.sb_logsunit)
451da177e4SLinus Torvalds #define XLOG_LSUNITTOB(log, su) ((su) * (log)->l_mp->m_sb.sb_logsunit)
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define XLOG_HEADER_SIZE	512
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #define XLOG_REC_SHIFT(log) \
5062118709SEric Sandeen 	BTOBB(1 << (xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? \
511da177e4SLinus Torvalds 	 XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT))
521da177e4SLinus Torvalds #define XLOG_TOTAL_REC_SHIFT(log) \
5362118709SEric Sandeen 	BTOBB(XLOG_MAX_ICLOGS << (xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? \
541da177e4SLinus Torvalds 	 XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT))
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 
5703bea6feSChristoph Hellwig static inline xfs_lsn_t xlog_assign_lsn(uint cycle, uint block)
5803bea6feSChristoph Hellwig {
5903bea6feSChristoph Hellwig 	return ((xfs_lsn_t)cycle << 32) | block;
601da177e4SLinus Torvalds }
6103bea6feSChristoph Hellwig 
6203bea6feSChristoph Hellwig static inline uint xlog_get_cycle(char *ptr)
6303bea6feSChristoph Hellwig {
64b53e675dSChristoph Hellwig 	if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
65b53e675dSChristoph Hellwig 		return be32_to_cpu(*((__be32 *)ptr + 1));
6603bea6feSChristoph Hellwig 	else
67b53e675dSChristoph Hellwig 		return be32_to_cpu(*(__be32 *)ptr);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define BLK_AVG(blk1, blk2)	((blk1+blk2) >> 1)
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #ifdef __KERNEL__
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * get client id from packed copy.
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  * this hack is here because the xlog_pack code copies four bytes
781da177e4SLinus Torvalds  * of xlog_op_header containing the fields oh_clientid, oh_flags
791da177e4SLinus Torvalds  * and oh_res2 into the packed copy.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * later on this four byte chunk is treated as an int and the
821da177e4SLinus Torvalds  * client id is pulled out.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * this has endian issues, of course.
851da177e4SLinus Torvalds  */
86b53e675dSChristoph Hellwig static inline uint xlog_get_client_id(__be32 i)
8703bea6feSChristoph Hellwig {
88b53e675dSChristoph Hellwig 	return be32_to_cpu(i) >> 24;
8903bea6feSChristoph Hellwig }
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #define xlog_panic(args...)	cmn_err(CE_PANIC, ## args)
921da177e4SLinus Torvalds #define xlog_exit(args...)	cmn_err(CE_PANIC, ## args)
931da177e4SLinus Torvalds #define xlog_warn(args...)	cmn_err(CE_WARN, ## args)
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /*
961da177e4SLinus Torvalds  * In core log state
971da177e4SLinus Torvalds  */
981da177e4SLinus Torvalds #define XLOG_STATE_ACTIVE    0x0001 /* Current IC log being written to */
991da177e4SLinus Torvalds #define XLOG_STATE_WANT_SYNC 0x0002 /* Want to sync this iclog; no more writes */
1001da177e4SLinus Torvalds #define XLOG_STATE_SYNCING   0x0004 /* This IC log is syncing */
1011da177e4SLinus Torvalds #define XLOG_STATE_DONE_SYNC 0x0008 /* Done syncing to disk */
1021da177e4SLinus Torvalds #define XLOG_STATE_DO_CALLBACK \
1031da177e4SLinus Torvalds 			     0x0010 /* Process callback functions */
1041da177e4SLinus Torvalds #define XLOG_STATE_CALLBACK  0x0020 /* Callback functions now */
1051da177e4SLinus Torvalds #define XLOG_STATE_DIRTY     0x0040 /* Dirty IC log, not ready for ACTIVE status*/
1061da177e4SLinus Torvalds #define XLOG_STATE_IOERROR   0x0080 /* IO error happened in sync'ing log */
1071da177e4SLinus Torvalds #define XLOG_STATE_ALL	     0x7FFF /* All possible valid flags */
1081da177e4SLinus Torvalds #define XLOG_STATE_NOTUSED   0x8000 /* This IC log not being used */
1091da177e4SLinus Torvalds #endif	/* __KERNEL__ */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * Flags to log operation header
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * The first write of a new transaction will be preceded with a start
1151da177e4SLinus Torvalds  * record, XLOG_START_TRANS.  Once a transaction is committed, a commit
1161da177e4SLinus Torvalds  * record is written, XLOG_COMMIT_TRANS.  If a single region can not fit into
1171da177e4SLinus Torvalds  * the remainder of the current active in-core log, it is split up into
1181da177e4SLinus Torvalds  * multiple regions.  Each partial region will be marked with a
1191da177e4SLinus Torvalds  * XLOG_CONTINUE_TRANS until the last one, which gets marked with XLOG_END_TRANS.
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds #define XLOG_START_TRANS	0x01	/* Start a new transaction */
1231da177e4SLinus Torvalds #define XLOG_COMMIT_TRANS	0x02	/* Commit this transaction */
1241da177e4SLinus Torvalds #define XLOG_CONTINUE_TRANS	0x04	/* Cont this trans into new region */
1251da177e4SLinus Torvalds #define XLOG_WAS_CONT_TRANS	0x08	/* Cont this trans into new region */
1261da177e4SLinus Torvalds #define XLOG_END_TRANS		0x10	/* End a continued transaction */
1271da177e4SLinus Torvalds #define XLOG_UNMOUNT_TRANS	0x20	/* Unmount a filesystem transaction */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds #ifdef __KERNEL__
1301da177e4SLinus Torvalds /*
1311da177e4SLinus Torvalds  * Flags to log ticket
1321da177e4SLinus Torvalds  */
1331da177e4SLinus Torvalds #define XLOG_TIC_INITED		0x1	/* has been initialized */
1341da177e4SLinus Torvalds #define XLOG_TIC_PERM_RESERV	0x2	/* permanent reservation */
1350b1b213fSChristoph Hellwig 
1360b1b213fSChristoph Hellwig #define XLOG_TIC_FLAGS \
1370b1b213fSChristoph Hellwig 	{ XLOG_TIC_INITED,	"XLOG_TIC_INITED" }, \
13810547941SDave Chinner 	{ XLOG_TIC_PERM_RESERV,	"XLOG_TIC_PERM_RESERV" }
1390b1b213fSChristoph Hellwig 
1401da177e4SLinus Torvalds #endif	/* __KERNEL__ */
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define XLOG_UNMOUNT_TYPE	0x556e	/* Un for Unmount */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * Flags for log structure
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds #define XLOG_CHKSUM_MISMATCH	0x1	/* used only during recovery */
1481da177e4SLinus Torvalds #define XLOG_ACTIVE_RECOVERY	0x2	/* in the middle of recovery */
1491da177e4SLinus Torvalds #define	XLOG_RECOVERY_NEEDED	0x4	/* log was recovered */
1501da177e4SLinus Torvalds #define XLOG_IO_ERROR		0x8	/* log hit an I/O error, and being
1511da177e4SLinus Torvalds 					   shutdown */
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 {
244eb40a875SDave Chinner 	wait_queue_head_t  t_wait;	 /* ticket wait queue */
24510547941SDave Chinner 	struct list_head   t_queue;	 /* reserve/write queue */
2461da177e4SLinus Torvalds 	xlog_tid_t	   t_tid;	 /* transaction identifier	 : 4  */
247cc09c0dcSDave Chinner 	atomic_t	   t_ref;	 /* ticket reference count       : 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__
312b28708d6SChristoph Hellwig 
313b28708d6SChristoph Hellwig /*
314b28708d6SChristoph Hellwig  * Quite misnamed, because this union lays out the actual on-disk log buffer.
315b28708d6SChristoph Hellwig  */
316b28708d6SChristoph Hellwig typedef union xlog_in_core2 {
317b28708d6SChristoph Hellwig 	xlog_rec_header_t	hic_header;
318b28708d6SChristoph Hellwig 	xlog_rec_ext_header_t	hic_xheader;
319b28708d6SChristoph Hellwig 	char			hic_sector[XLOG_HEADER_SIZE];
320b28708d6SChristoph Hellwig } xlog_in_core_2_t;
321b28708d6SChristoph Hellwig 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * - A log record header is 512 bytes.  There is plenty of room to grow the
3241da177e4SLinus Torvalds  *	xlog_rec_header_t into the reserved space.
3251da177e4SLinus Torvalds  * - ic_data follows, so a write to disk can start at the beginning of
3261da177e4SLinus Torvalds  *	the iclog.
32712017fafSDavid Chinner  * - ic_forcewait is used to implement synchronous forcing of the iclog to disk.
3281da177e4SLinus Torvalds  * - ic_next is the pointer to the next iclog in the ring.
3291da177e4SLinus Torvalds  * - ic_bp is a pointer to the buffer used to write this incore log to disk.
3301da177e4SLinus Torvalds  * - ic_log is a pointer back to the global log structure.
3311da177e4SLinus Torvalds  * - ic_callback is a linked list of callback function/argument pairs to be
3321da177e4SLinus Torvalds  *	called after an iclog finishes writing.
3331da177e4SLinus Torvalds  * - ic_size is the full size of the header plus data.
3341da177e4SLinus Torvalds  * - ic_offset is the current number of bytes written to in this iclog.
3351da177e4SLinus Torvalds  * - ic_refcnt is bumped when someone is writing to the log.
3361da177e4SLinus Torvalds  * - ic_state is the state of the iclog.
337114d23aaSDavid Chinner  *
338114d23aaSDavid Chinner  * Because of cacheline contention on large machines, we need to separate
339114d23aaSDavid Chinner  * various resources onto different cachelines. To start with, make the
340114d23aaSDavid Chinner  * structure cacheline aligned. The following fields can be contended on
341114d23aaSDavid Chinner  * by independent processes:
342114d23aaSDavid Chinner  *
343114d23aaSDavid Chinner  *	- ic_callback_*
344114d23aaSDavid Chinner  *	- ic_refcnt
345114d23aaSDavid Chinner  *	- fields protected by the global l_icloglock
346114d23aaSDavid Chinner  *
347114d23aaSDavid Chinner  * so we need to ensure that these fields are located in separate cachelines.
348114d23aaSDavid Chinner  * We'll put all the read-only and l_icloglock fields in the first cacheline,
349114d23aaSDavid Chinner  * and move everything else out to subsequent cachelines.
3501da177e4SLinus Torvalds  */
351b28708d6SChristoph Hellwig typedef struct xlog_in_core {
352eb40a875SDave Chinner 	wait_queue_head_t	ic_force_wait;
353eb40a875SDave Chinner 	wait_queue_head_t	ic_write_wait;
3541da177e4SLinus Torvalds 	struct xlog_in_core	*ic_next;
3551da177e4SLinus Torvalds 	struct xlog_in_core	*ic_prev;
3561da177e4SLinus Torvalds 	struct xfs_buf		*ic_bp;
3571da177e4SLinus Torvalds 	struct log		*ic_log;
3581da177e4SLinus Torvalds 	int			ic_size;
3591da177e4SLinus Torvalds 	int			ic_offset;
3601da177e4SLinus Torvalds 	int			ic_bwritecnt;
361a5687787SChristoph Hellwig 	unsigned short		ic_state;
3621da177e4SLinus Torvalds 	char			*ic_datap;	/* pointer to iclog data */
363114d23aaSDavid Chinner 
364114d23aaSDavid Chinner 	/* Callback structures need their own cacheline */
365114d23aaSDavid Chinner 	spinlock_t		ic_callback_lock ____cacheline_aligned_in_smp;
366114d23aaSDavid Chinner 	xfs_log_callback_t	*ic_callback;
367114d23aaSDavid Chinner 	xfs_log_callback_t	**ic_callback_tail;
368114d23aaSDavid Chinner 
369114d23aaSDavid Chinner 	/* reference counts need their own cacheline */
370114d23aaSDavid Chinner 	atomic_t		ic_refcnt ____cacheline_aligned_in_smp;
371b28708d6SChristoph Hellwig 	xlog_in_core_2_t	*ic_data;
372b28708d6SChristoph Hellwig #define ic_header	ic_data->hic_header
3731da177e4SLinus Torvalds } xlog_in_core_t;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /*
37671e330b5SDave Chinner  * The CIL context is used to aggregate per-transaction details as well be
37771e330b5SDave Chinner  * passed to the iclog for checkpoint post-commit processing.  After being
37871e330b5SDave Chinner  * passed to the iclog, another context needs to be allocated for tracking the
37971e330b5SDave Chinner  * next set of transactions to be aggregated into a checkpoint.
38071e330b5SDave Chinner  */
38171e330b5SDave Chinner struct xfs_cil;
38271e330b5SDave Chinner 
38371e330b5SDave Chinner struct xfs_cil_ctx {
38471e330b5SDave Chinner 	struct xfs_cil		*cil;
38571e330b5SDave Chinner 	xfs_lsn_t		sequence;	/* chkpt sequence # */
38671e330b5SDave Chinner 	xfs_lsn_t		start_lsn;	/* first LSN of chkpt commit */
38771e330b5SDave Chinner 	xfs_lsn_t		commit_lsn;	/* chkpt commit record lsn */
38871e330b5SDave Chinner 	struct xlog_ticket	*ticket;	/* chkpt ticket */
38971e330b5SDave Chinner 	int			nvecs;		/* number of regions */
39071e330b5SDave Chinner 	int			space_used;	/* aggregate size of regions */
39171e330b5SDave Chinner 	struct list_head	busy_extents;	/* busy extents in chkpt */
39271e330b5SDave Chinner 	struct xfs_log_vec	*lv_chain;	/* logvecs being pushed */
39371e330b5SDave Chinner 	xfs_log_callback_t	log_cb;		/* completion callback hook. */
39471e330b5SDave Chinner 	struct list_head	committing;	/* ctx committing list */
39571e330b5SDave Chinner };
39671e330b5SDave Chinner 
39771e330b5SDave Chinner /*
39871e330b5SDave Chinner  * Committed Item List structure
39971e330b5SDave Chinner  *
40071e330b5SDave Chinner  * This structure is used to track log items that have been committed but not
40171e330b5SDave Chinner  * yet written into the log. It is used only when the delayed logging mount
40271e330b5SDave Chinner  * option is enabled.
40371e330b5SDave Chinner  *
40471e330b5SDave Chinner  * This structure tracks the list of committing checkpoint contexts so
40571e330b5SDave Chinner  * we can avoid the problem of having to hold out new transactions during a
40671e330b5SDave Chinner  * flush until we have a the commit record LSN of the checkpoint. We can
40771e330b5SDave Chinner  * traverse the list of committing contexts in xlog_cil_push_lsn() to find a
40871e330b5SDave Chinner  * sequence match and extract the commit LSN directly from there. If the
40971e330b5SDave Chinner  * checkpoint is still in the process of committing, we can block waiting for
41071e330b5SDave Chinner  * the commit LSN to be determined as well. This should make synchronous
41171e330b5SDave Chinner  * operations almost as efficient as the old logging methods.
41271e330b5SDave Chinner  */
41371e330b5SDave Chinner struct xfs_cil {
41471e330b5SDave Chinner 	struct log		*xc_log;
41571e330b5SDave Chinner 	struct list_head	xc_cil;
41671e330b5SDave Chinner 	spinlock_t		xc_cil_lock;
41771e330b5SDave Chinner 	struct xfs_cil_ctx	*xc_ctx;
41871e330b5SDave Chinner 	struct rw_semaphore	xc_ctx_lock;
41971e330b5SDave Chinner 	struct list_head	xc_committing;
420eb40a875SDave Chinner 	wait_queue_head_t	xc_commit_wait;
421a44f13edSDave Chinner 	xfs_lsn_t		xc_current_sequence;
42271e330b5SDave Chinner };
42371e330b5SDave Chinner 
42471e330b5SDave Chinner /*
42580168676SDave Chinner  * The amount of log space we allow the CIL to aggregate is difficult to size.
42680168676SDave Chinner  * Whatever we choose, we have to make sure we can get a reservation for the
42780168676SDave Chinner  * log space effectively, that it is large enough to capture sufficient
42880168676SDave Chinner  * relogging to reduce log buffer IO significantly, but it is not too large for
42980168676SDave Chinner  * the log or induces too much latency when writing out through the iclogs. We
43080168676SDave Chinner  * track both space consumed and the number of vectors in the checkpoint
43180168676SDave Chinner  * context, so we need to decide which to use for limiting.
432df806158SDave Chinner  *
433df806158SDave Chinner  * Every log buffer we write out during a push needs a header reserved, which
434df806158SDave Chinner  * is at least one sector and more for v2 logs. Hence we need a reservation of
435df806158SDave Chinner  * at least 512 bytes per 32k of log space just for the LR headers. That means
436df806158SDave Chinner  * 16KB of reservation per megabyte of delayed logging space we will consume,
437df806158SDave Chinner  * plus various headers.  The number of headers will vary based on the num of
438df806158SDave Chinner  * io vectors, so limiting on a specific number of vectors is going to result
439df806158SDave Chinner  * in transactions of varying size. IOWs, it is more consistent to track and
440df806158SDave Chinner  * limit space consumed in the log rather than by the number of objects being
441df806158SDave Chinner  * logged in order to prevent checkpoint ticket overruns.
442df806158SDave Chinner  *
443df806158SDave Chinner  * Further, use of static reservations through the log grant mechanism is
444df806158SDave Chinner  * problematic. It introduces a lot of complexity (e.g. reserve grant vs write
445df806158SDave Chinner  * grant) and a significant deadlock potential because regranting write space
446df806158SDave Chinner  * can block on log pushes. Hence if we have to regrant log space during a log
447df806158SDave Chinner  * push, we can deadlock.
448df806158SDave Chinner  *
449df806158SDave Chinner  * However, we can avoid this by use of a dynamic "reservation stealing"
450df806158SDave Chinner  * technique during transaction commit whereby unused reservation space in the
451df806158SDave Chinner  * transaction ticket is transferred to the CIL ctx commit ticket to cover the
452df806158SDave Chinner  * space needed by the checkpoint transaction. This means that we never need to
453df806158SDave Chinner  * specifically reserve space for the CIL checkpoint transaction, nor do we
454df806158SDave Chinner  * need to regrant space once the checkpoint completes. This also means the
455df806158SDave Chinner  * checkpoint transaction ticket is specific to the checkpoint context, rather
456df806158SDave Chinner  * than the CIL itself.
457df806158SDave Chinner  *
45880168676SDave Chinner  * With dynamic reservations, we can effectively make up arbitrary limits for
45980168676SDave Chinner  * the checkpoint size so long as they don't violate any other size rules.
46080168676SDave Chinner  * Recovery imposes a rule that no transaction exceed half the log, so we are
46180168676SDave Chinner  * limited by that.  Furthermore, the log transaction reservation subsystem
46280168676SDave Chinner  * tries to keep 25% of the log free, so we need to keep below that limit or we
46380168676SDave Chinner  * risk running out of free log space to start any new transactions.
46480168676SDave Chinner  *
46580168676SDave Chinner  * In order to keep background CIL push efficient, we will set a lower
46680168676SDave Chinner  * threshold at which background pushing is attempted without blocking current
46780168676SDave Chinner  * transaction commits.  A separate, higher bound defines when CIL pushes are
46880168676SDave Chinner  * enforced to ensure we stay within our maximum checkpoint size bounds.
46980168676SDave Chinner  * threshold, yet give us plenty of space for aggregation on large logs.
470df806158SDave Chinner  */
47180168676SDave Chinner #define XLOG_CIL_SPACE_LIMIT(log)	(log->l_logsize >> 3)
47280168676SDave Chinner #define XLOG_CIL_HARD_SPACE_LIMIT(log)	(3 * (log->l_logsize >> 4))
473df806158SDave Chinner 
474df806158SDave Chinner /*
4751da177e4SLinus Torvalds  * The reservation head lsn is not made up of a cycle number and block number.
4761da177e4SLinus Torvalds  * Instead, it uses a cycle number and byte number.  Logs don't expect to
4771da177e4SLinus Torvalds  * overflow 31 bits worth of byte offset, so using a byte number will mean
4781da177e4SLinus Torvalds  * that round off problems won't occur when releasing partial reservations.
4791da177e4SLinus Torvalds  */
4801da177e4SLinus Torvalds typedef struct log {
4814679b2d3SDavid Chinner 	/* The following fields don't need locking */
4824679b2d3SDavid Chinner 	struct xfs_mount	*l_mp;	        /* mount point */
483a9c21c1bSDavid Chinner 	struct xfs_ail		*l_ailp;	/* AIL log is working with */
48471e330b5SDave Chinner 	struct xfs_cil		*l_cilp;	/* CIL log is working with */
4854679b2d3SDavid Chinner 	struct xfs_buf		*l_xbuf;        /* extra buffer for log
4864679b2d3SDavid Chinner 						 * wrapping */
4874679b2d3SDavid Chinner 	struct xfs_buftarg	*l_targ;        /* buftarg of log */
4884679b2d3SDavid Chinner 	uint			l_flags;
4894679b2d3SDavid Chinner 	uint			l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */
490d5689eaaSChristoph Hellwig 	struct list_head	*l_buf_cancel_table;
4914679b2d3SDavid Chinner 	int			l_iclog_hsize;  /* size of iclog header */
4924679b2d3SDavid Chinner 	int			l_iclog_heads;  /* # of iclog header sectors */
49348389ef1SAlex Elder 	uint			l_sectBBsize;   /* sector size in BBs (2^n) */
4944679b2d3SDavid Chinner 	int			l_iclog_size;	/* size of log in bytes */
4954679b2d3SDavid Chinner 	int			l_iclog_size_log; /* log power size of log */
4964679b2d3SDavid Chinner 	int			l_iclog_bufs;	/* number of iclog buffers */
4974679b2d3SDavid Chinner 	xfs_daddr_t		l_logBBstart;   /* start block of log */
4984679b2d3SDavid Chinner 	int			l_logsize;      /* size of log in bytes */
4994679b2d3SDavid Chinner 	int			l_logBBsize;    /* size of log in BB chunks */
5004679b2d3SDavid Chinner 
5011da177e4SLinus Torvalds 	/* The following block of fields are changed while holding icloglock */
502eb40a875SDave Chinner 	wait_queue_head_t	l_flush_wait ____cacheline_aligned_in_smp;
503d748c623SMatthew Wilcox 						/* waiting for iclog flush */
5041da177e4SLinus Torvalds 	int			l_covered_state;/* state of "covering disk
5051da177e4SLinus Torvalds 						 * log entries" */
5061da177e4SLinus Torvalds 	xlog_in_core_t		*l_iclog;       /* head log queue	*/
507b22cd72cSEric Sandeen 	spinlock_t		l_icloglock;    /* grab to change iclog state */
5081da177e4SLinus Torvalds 	xfs_lsn_t		l_tail_lsn;     /* lsn of 1st LR with unflushed
5091da177e4SLinus Torvalds 						 * buffers */
5101da177e4SLinus Torvalds 	int			l_curr_cycle;   /* Cycle number of log writes */
5111da177e4SLinus Torvalds 	int			l_prev_cycle;   /* Cycle number before last
5121da177e4SLinus Torvalds 						 * block increment */
5131da177e4SLinus Torvalds 	int			l_curr_block;   /* current logical log block */
5141da177e4SLinus Torvalds 	int			l_prev_block;   /* previous logical log block */
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* The following block of fields are changed while holding grant_lock */
5174679b2d3SDavid Chinner 	spinlock_t		l_grant_lock ____cacheline_aligned_in_smp;
51810547941SDave Chinner 	struct list_head	l_reserveq;
51910547941SDave Chinner 	struct list_head	l_writeq;
520a69ed03cSDave Chinner 	int64_t			l_grant_reserve_head;
521a69ed03cSDave Chinner 	int64_t			l_grant_write_head;
5221da177e4SLinus Torvalds 
523*84f3c683SDave Chinner 	/*
524*84f3c683SDave Chinner 	 * l_last_sync_lsn is an atomic so it can be set and read without
525*84f3c683SDave Chinner 	 * needing to hold specific locks. To avoid operations contending with
526*84f3c683SDave Chinner 	 * other hot objects, place it on a separate cacheline.
527*84f3c683SDave Chinner 	 */
528*84f3c683SDave Chinner 	/* lsn of last LR on disk */
529*84f3c683SDave Chinner 	atomic64_t		l_last_sync_lsn ____cacheline_aligned_in_smp;
530*84f3c683SDave Chinner 
5314679b2d3SDavid Chinner 	/* The following field are used for debugging; need to hold icloglock */
5324679b2d3SDavid Chinner #ifdef DEBUG
5334679b2d3SDavid Chinner 	char			*l_iclog_bak[XLOG_MAX_ICLOGS];
5344679b2d3SDavid Chinner #endif
5354679b2d3SDavid Chinner 
5361da177e4SLinus Torvalds } xlog_t;
5371da177e4SLinus Torvalds 
538d5689eaaSChristoph Hellwig #define XLOG_BUF_CANCEL_BUCKET(log, blkno) \
539d5689eaaSChristoph Hellwig 	((log)->l_buf_cancel_table + ((__uint64_t)blkno % XLOG_BC_TABLE_SIZE))
540d5689eaaSChristoph Hellwig 
541cfcbbbd0SNathan Scott #define XLOG_FORCED_SHUTDOWN(log)	((log)->l_flags & XLOG_IO_ERROR)
542cfcbbbd0SNathan Scott 
5431da177e4SLinus Torvalds /* common routines */
5441da177e4SLinus Torvalds extern xfs_lsn_t xlog_assign_tail_lsn(struct xfs_mount *mp);
54565be6054SEric Sandeen extern int	 xlog_recover(xlog_t *log);
5464249023aSChristoph Hellwig extern int	 xlog_recover_finish(xlog_t *log);
5471da177e4SLinus Torvalds extern void	 xlog_pack_data(xlog_t *log, xlog_in_core_t *iclog, int);
5481da177e4SLinus Torvalds 
549eb01c9cdSDavid Chinner extern kmem_zone_t *xfs_log_ticket_zone;
55071e330b5SDave Chinner struct xlog_ticket *xlog_ticket_alloc(struct log *log, int unit_bytes,
55171e330b5SDave Chinner 				int count, char client, uint xflags,
55271e330b5SDave Chinner 				int alloc_flags);
55371e330b5SDave Chinner 
554eb01c9cdSDavid Chinner 
555e6b1f273SChristoph Hellwig static inline void
556e6b1f273SChristoph Hellwig xlog_write_adv_cnt(void **ptr, int *len, int *off, size_t bytes)
557e6b1f273SChristoph Hellwig {
558e6b1f273SChristoph Hellwig 	*ptr += bytes;
559e6b1f273SChristoph Hellwig 	*len -= bytes;
560e6b1f273SChristoph Hellwig 	*off += bytes;
561e6b1f273SChristoph Hellwig }
562e6b1f273SChristoph Hellwig 
56371e330b5SDave Chinner void	xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket);
56471e330b5SDave Chinner int	xlog_write(struct log *log, struct xfs_log_vec *log_vector,
56571e330b5SDave Chinner 				struct xlog_ticket *tic, xfs_lsn_t *start_lsn,
56671e330b5SDave Chinner 				xlog_in_core_t **commit_iclog, uint flags);
56771e330b5SDave Chinner 
56871e330b5SDave Chinner /*
569a69ed03cSDave Chinner  * When we crack the grrant head, we sample it first so that the value will not
570a69ed03cSDave Chinner  * change while we are cracking it into the component values. This means we
571a69ed03cSDave Chinner  * will always get consistent component values to work from.
572a69ed03cSDave Chinner  */
573a69ed03cSDave Chinner static inline void
574a69ed03cSDave Chinner xlog_crack_grant_head(int64_t *head, int *cycle, int *space)
575a69ed03cSDave Chinner {
576a69ed03cSDave Chinner 	int64_t	val = *head;
577a69ed03cSDave Chinner 
578a69ed03cSDave Chinner 	*cycle = val >> 32;
579a69ed03cSDave Chinner 	*space = val & 0xffffffff;
580a69ed03cSDave Chinner }
581a69ed03cSDave Chinner 
582a69ed03cSDave Chinner static inline void
583a69ed03cSDave Chinner xlog_assign_grant_head(int64_t *head, int cycle, int space)
584a69ed03cSDave Chinner {
585a69ed03cSDave Chinner 	*head = ((int64_t)cycle << 32) | space;
586a69ed03cSDave Chinner }
587a69ed03cSDave Chinner 
588a69ed03cSDave Chinner /*
58971e330b5SDave Chinner  * Committed Item List interfaces
59071e330b5SDave Chinner  */
59171e330b5SDave Chinner int	xlog_cil_init(struct log *log);
59271e330b5SDave Chinner void	xlog_cil_init_post_recovery(struct log *log);
59371e330b5SDave Chinner void	xlog_cil_destroy(struct log *log);
59471e330b5SDave Chinner 
595a44f13edSDave Chinner /*
596a44f13edSDave Chinner  * CIL force routines
597a44f13edSDave Chinner  */
598a44f13edSDave Chinner xfs_lsn_t xlog_cil_force_lsn(struct log *log, xfs_lsn_t sequence);
599a44f13edSDave Chinner 
600a44f13edSDave Chinner static inline void
601a44f13edSDave Chinner xlog_cil_force(struct log *log)
602a44f13edSDave Chinner {
603a44f13edSDave Chinner 	xlog_cil_force_lsn(log, log->l_cilp->xc_current_sequence);
604a44f13edSDave Chinner }
60571e330b5SDave Chinner 
606955e47adSTim Shimmin /*
607955e47adSTim Shimmin  * Unmount record type is used as a pseudo transaction type for the ticket.
608955e47adSTim Shimmin  * It's value must be outside the range of XFS_TRANS_* values.
609955e47adSTim Shimmin  */
610955e47adSTim Shimmin #define XLOG_UNMOUNT_REC_TYPE	(-1U)
611955e47adSTim Shimmin 
612eb40a875SDave Chinner /*
613eb40a875SDave Chinner  * Wrapper function for waiting on a wait queue serialised against wakeups
614eb40a875SDave Chinner  * by a spinlock. This matches the semantics of all the wait queues used in the
615eb40a875SDave Chinner  * log code.
616eb40a875SDave Chinner  */
617eb40a875SDave Chinner static inline void xlog_wait(wait_queue_head_t *wq, spinlock_t *lock)
618eb40a875SDave Chinner {
619eb40a875SDave Chinner 	DECLARE_WAITQUEUE(wait, current);
620eb40a875SDave Chinner 
621eb40a875SDave Chinner 	add_wait_queue_exclusive(wq, &wait);
622eb40a875SDave Chinner 	__set_current_state(TASK_UNINTERRUPTIBLE);
623eb40a875SDave Chinner 	spin_unlock(lock);
624eb40a875SDave Chinner 	schedule();
625eb40a875SDave Chinner 	remove_wait_queue(wq, &wait);
626eb40a875SDave Chinner }
6271da177e4SLinus Torvalds #endif	/* __KERNEL__ */
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds #endif	/* __XFS_LOG_PRIV_H__ */
630