xref: /linux/fs/xfs/xfs_log_priv.h (revision d4ca1d550d052accec85ae26ac5b9d3d8b8f81f1)
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;
22ad223e60SMark Tinguely struct xlog;
23a844f451SNathan Scott struct xlog_ticket;
241da177e4SLinus Torvalds struct xfs_mount;
25239880efSDave Chinner struct xfs_log_callback;
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
28fc06c6d0SDave Chinner  * Flags for log structure
291da177e4SLinus Torvalds  */
30fc06c6d0SDave Chinner #define XLOG_ACTIVE_RECOVERY	0x2	/* in the middle of recovery */
31fc06c6d0SDave Chinner #define	XLOG_RECOVERY_NEEDED	0x4	/* log was recovered */
32fc06c6d0SDave Chinner #define XLOG_IO_ERROR		0x8	/* log hit an I/O error, and being
33fc06c6d0SDave Chinner 					   shutdown */
34fc06c6d0SDave Chinner #define XLOG_TAIL_WARN		0x10	/* log tail verify warning issued */
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * get client id from packed copy.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * this hack is here because the xlog_pack code copies four bytes
401da177e4SLinus Torvalds  * of xlog_op_header containing the fields oh_clientid, oh_flags
411da177e4SLinus Torvalds  * and oh_res2 into the packed copy.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * later on this four byte chunk is treated as an int and the
441da177e4SLinus Torvalds  * client id is pulled out.
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * this has endian issues, of course.
471da177e4SLinus Torvalds  */
48b53e675dSChristoph Hellwig static inline uint xlog_get_client_id(__be32 i)
4903bea6feSChristoph Hellwig {
50b53e675dSChristoph Hellwig 	return be32_to_cpu(i) >> 24;
5103bea6feSChristoph Hellwig }
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * In core log state
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds #define XLOG_STATE_ACTIVE    0x0001 /* Current IC log being written to */
571da177e4SLinus Torvalds #define XLOG_STATE_WANT_SYNC 0x0002 /* Want to sync this iclog; no more writes */
581da177e4SLinus Torvalds #define XLOG_STATE_SYNCING   0x0004 /* This IC log is syncing */
591da177e4SLinus Torvalds #define XLOG_STATE_DONE_SYNC 0x0008 /* Done syncing to disk */
601da177e4SLinus Torvalds #define XLOG_STATE_DO_CALLBACK \
611da177e4SLinus Torvalds 			     0x0010 /* Process callback functions */
621da177e4SLinus Torvalds #define XLOG_STATE_CALLBACK  0x0020 /* Callback functions now */
631da177e4SLinus Torvalds #define XLOG_STATE_DIRTY     0x0040 /* Dirty IC log, not ready for ACTIVE status*/
641da177e4SLinus Torvalds #define XLOG_STATE_IOERROR   0x0080 /* IO error happened in sync'ing log */
65609adfc2SBrian Foster #define XLOG_STATE_IOABORT   0x0100 /* force abort on I/O completion (debug) */
661da177e4SLinus Torvalds #define XLOG_STATE_ALL	     0x7FFF /* All possible valid flags */
671da177e4SLinus Torvalds #define XLOG_STATE_NOTUSED   0x8000 /* This IC log not being used */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * Flags to log ticket
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds #define XLOG_TIC_INITED		0x1	/* has been initialized */
731da177e4SLinus Torvalds #define XLOG_TIC_PERM_RESERV	0x2	/* permanent reservation */
740b1b213fSChristoph Hellwig 
750b1b213fSChristoph Hellwig #define XLOG_TIC_FLAGS \
760b1b213fSChristoph Hellwig 	{ XLOG_TIC_INITED,	"XLOG_TIC_INITED" }, \
7710547941SDave Chinner 	{ XLOG_TIC_PERM_RESERV,	"XLOG_TIC_PERM_RESERV" }
780b1b213fSChristoph Hellwig 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  * Below are states for covering allocation transactions.
811da177e4SLinus Torvalds  * By covering, we mean changing the h_tail_lsn in the last on-disk
821da177e4SLinus Torvalds  * log write such that no allocation transactions will be re-done during
831da177e4SLinus Torvalds  * recovery after a system crash. Recovery starts at the last on-disk
841da177e4SLinus Torvalds  * log write.
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  * These states are used to insert dummy log entries to cover
871da177e4SLinus Torvalds  * space allocation transactions which can undo non-transactional changes
881da177e4SLinus Torvalds  * after a crash. Writes to a file with space
891da177e4SLinus Torvalds  * already allocated do not result in any transactions. Allocations
901da177e4SLinus Torvalds  * might include space beyond the EOF. So if we just push the EOF a
911da177e4SLinus Torvalds  * little, the last transaction for the file could contain the wrong
921da177e4SLinus Torvalds  * size. If there is no file system activity, after an allocation
931da177e4SLinus Torvalds  * transaction, and the system crashes, the allocation transaction
941da177e4SLinus Torvalds  * will get replayed and the file will be truncated. This could
951da177e4SLinus Torvalds  * be hours/days/... after the allocation occurred.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * The fix for this is to do two dummy transactions when the
981da177e4SLinus Torvalds  * system is idle. We need two dummy transaction because the h_tail_lsn
991da177e4SLinus Torvalds  * in the log record header needs to point beyond the last possible
1001da177e4SLinus Torvalds  * non-dummy transaction. The first dummy changes the h_tail_lsn to
1011da177e4SLinus Torvalds  * the first transaction before the dummy. The second dummy causes
1021da177e4SLinus Torvalds  * h_tail_lsn to point to the first dummy. Recovery starts at h_tail_lsn.
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * These dummy transactions get committed when everything
1051da177e4SLinus Torvalds  * is idle (after there has been some activity).
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * There are 5 states used to control this.
1081da177e4SLinus Torvalds  *
1091da177e4SLinus Torvalds  *  IDLE -- no logging has been done on the file system or
1101da177e4SLinus Torvalds  *		we are done covering previous transactions.
1111da177e4SLinus Torvalds  *  NEED -- logging has occurred and we need a dummy transaction
1121da177e4SLinus Torvalds  *		when the log becomes idle.
1131da177e4SLinus Torvalds  *  DONE -- we were in the NEED state and have committed a dummy
1141da177e4SLinus Torvalds  *		transaction.
1151da177e4SLinus Torvalds  *  NEED2 -- we detected that a dummy transaction has gone to the
1161da177e4SLinus Torvalds  *		on disk log with no other transactions.
1171da177e4SLinus Torvalds  *  DONE2 -- we committed a dummy transaction when in the NEED2 state.
1181da177e4SLinus Torvalds  *
1191da177e4SLinus Torvalds  * There are two places where we switch states:
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  * 1.) In xfs_sync, when we detect an idle log and are in NEED or NEED2.
1221da177e4SLinus Torvalds  *	We commit the dummy transaction and switch to DONE or DONE2,
1231da177e4SLinus Torvalds  *	respectively. In all other states, we don't do anything.
1241da177e4SLinus Torvalds  *
1251da177e4SLinus Torvalds  * 2.) When we finish writing the on-disk log (xlog_state_clean_log).
1261da177e4SLinus Torvalds  *
1271da177e4SLinus Torvalds  *	No matter what state we are in, if this isn't the dummy
1281da177e4SLinus Torvalds  *	transaction going out, the next state is NEED.
1291da177e4SLinus Torvalds  *	So, if we aren't in the DONE or DONE2 states, the next state
1301da177e4SLinus Torvalds  *	is NEED. We can't be finishing a write of the dummy record
1311da177e4SLinus Torvalds  *	unless it was committed and the state switched to DONE or DONE2.
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  *	If we are in the DONE state and this was a write of the
1341da177e4SLinus Torvalds  *		dummy transaction, we move to NEED2.
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  *	If we are in the DONE2 state and this was a write of the
1371da177e4SLinus Torvalds  *		dummy transaction, we move to IDLE.
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  * Writing only one dummy transaction can get appended to
1411da177e4SLinus Torvalds  * one file space allocation. When this happens, the log recovery
1421da177e4SLinus Torvalds  * code replays the space allocation and a file could be truncated.
1431da177e4SLinus Torvalds  * This is why we have the NEED2 and DONE2 states before going idle.
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds #define XLOG_STATE_COVER_IDLE	0
1471da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED	1
1481da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE	2
1491da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED2	3
1501da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE2	4
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define XLOG_COVER_OPS		5
1531da177e4SLinus Torvalds 
1547e9c6396STim Shimmin /* Ticket reservation region accounting */
1557e9c6396STim Shimmin #define XLOG_TIC_LEN_MAX	15
1567e9c6396STim Shimmin 
1577e9c6396STim Shimmin /*
1587e9c6396STim Shimmin  * Reservation region
1597e9c6396STim Shimmin  * As would be stored in xfs_log_iovec but without the i_addr which
1607e9c6396STim Shimmin  * we don't care about.
1617e9c6396STim Shimmin  */
1627e9c6396STim Shimmin typedef struct xlog_res {
1631259845dSTim Shimmin 	uint	r_len;	/* region length		:4 */
1641259845dSTim Shimmin 	uint	r_type;	/* region's transaction type	:4 */
1657e9c6396STim Shimmin } xlog_res_t;
1667e9c6396STim Shimmin 
1671da177e4SLinus Torvalds typedef struct xlog_ticket {
16810547941SDave Chinner 	struct list_head   t_queue;	 /* reserve/write queue */
16914a7235fSChristoph Hellwig 	struct task_struct *t_task;	 /* task that owns this ticket */
1701da177e4SLinus Torvalds 	xlog_tid_t	   t_tid;	 /* transaction identifier	 : 4  */
171cc09c0dcSDave Chinner 	atomic_t	   t_ref;	 /* ticket reference count       : 4  */
1721da177e4SLinus Torvalds 	int		   t_curr_res;	 /* current reservation in bytes : 4  */
1731da177e4SLinus Torvalds 	int		   t_unit_res;	 /* unit reservation in bytes    : 4  */
1747e9c6396STim Shimmin 	char		   t_ocnt;	 /* original count		 : 1  */
1757e9c6396STim Shimmin 	char		   t_cnt;	 /* current count		 : 1  */
1767e9c6396STim Shimmin 	char		   t_clientid;	 /* who does this belong to;	 : 1  */
1777e9c6396STim Shimmin 	char		   t_flags;	 /* properties of reservation	 : 1  */
1787e9c6396STim Shimmin 
1797e9c6396STim Shimmin         /* reservation array fields */
1807e9c6396STim Shimmin 	uint		   t_res_num;                    /* num in array : 4 */
1817e9c6396STim Shimmin 	uint		   t_res_num_ophdrs;		 /* num op hdrs  : 4 */
1827e9c6396STim Shimmin 	uint		   t_res_arr_sum;		 /* array sum    : 4 */
1837e9c6396STim Shimmin 	uint		   t_res_o_flow;		 /* sum overflow : 4 */
1841259845dSTim Shimmin 	xlog_res_t	   t_res_arr[XLOG_TIC_LEN_MAX];  /* array of res : 8 * 15 */
1851da177e4SLinus Torvalds } xlog_ticket_t;
1867e9c6396STim Shimmin 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * - A log record header is 512 bytes.  There is plenty of room to grow the
1891da177e4SLinus Torvalds  *	xlog_rec_header_t into the reserved space.
1901da177e4SLinus Torvalds  * - ic_data follows, so a write to disk can start at the beginning of
1911da177e4SLinus Torvalds  *	the iclog.
19212017fafSDavid Chinner  * - ic_forcewait is used to implement synchronous forcing of the iclog to disk.
1931da177e4SLinus Torvalds  * - ic_next is the pointer to the next iclog in the ring.
1941da177e4SLinus Torvalds  * - ic_bp is a pointer to the buffer used to write this incore log to disk.
1951da177e4SLinus Torvalds  * - ic_log is a pointer back to the global log structure.
1961da177e4SLinus Torvalds  * - ic_callback is a linked list of callback function/argument pairs to be
1971da177e4SLinus Torvalds  *	called after an iclog finishes writing.
1981da177e4SLinus Torvalds  * - ic_size is the full size of the header plus data.
1991da177e4SLinus Torvalds  * - ic_offset is the current number of bytes written to in this iclog.
2001da177e4SLinus Torvalds  * - ic_refcnt is bumped when someone is writing to the log.
2011da177e4SLinus Torvalds  * - ic_state is the state of the iclog.
202114d23aaSDavid Chinner  *
203114d23aaSDavid Chinner  * Because of cacheline contention on large machines, we need to separate
204114d23aaSDavid Chinner  * various resources onto different cachelines. To start with, make the
205114d23aaSDavid Chinner  * structure cacheline aligned. The following fields can be contended on
206114d23aaSDavid Chinner  * by independent processes:
207114d23aaSDavid Chinner  *
208114d23aaSDavid Chinner  *	- ic_callback_*
209114d23aaSDavid Chinner  *	- ic_refcnt
210114d23aaSDavid Chinner  *	- fields protected by the global l_icloglock
211114d23aaSDavid Chinner  *
212114d23aaSDavid Chinner  * so we need to ensure that these fields are located in separate cachelines.
213114d23aaSDavid Chinner  * We'll put all the read-only and l_icloglock fields in the first cacheline,
214114d23aaSDavid Chinner  * and move everything else out to subsequent cachelines.
2151da177e4SLinus Torvalds  */
216b28708d6SChristoph Hellwig typedef struct xlog_in_core {
217eb40a875SDave Chinner 	wait_queue_head_t	ic_force_wait;
218eb40a875SDave Chinner 	wait_queue_head_t	ic_write_wait;
2191da177e4SLinus Torvalds 	struct xlog_in_core	*ic_next;
2201da177e4SLinus Torvalds 	struct xlog_in_core	*ic_prev;
2211da177e4SLinus Torvalds 	struct xfs_buf		*ic_bp;
222ad223e60SMark Tinguely 	struct xlog		*ic_log;
2231da177e4SLinus Torvalds 	int			ic_size;
2241da177e4SLinus Torvalds 	int			ic_offset;
2251da177e4SLinus Torvalds 	int			ic_bwritecnt;
226a5687787SChristoph Hellwig 	unsigned short		ic_state;
2271da177e4SLinus Torvalds 	char			*ic_datap;	/* pointer to iclog data */
228114d23aaSDavid Chinner 
229114d23aaSDavid Chinner 	/* Callback structures need their own cacheline */
230114d23aaSDavid Chinner 	spinlock_t		ic_callback_lock ____cacheline_aligned_in_smp;
231239880efSDave Chinner 	struct xfs_log_callback	*ic_callback;
232239880efSDave Chinner 	struct xfs_log_callback	**ic_callback_tail;
233114d23aaSDavid Chinner 
234114d23aaSDavid Chinner 	/* reference counts need their own cacheline */
235114d23aaSDavid Chinner 	atomic_t		ic_refcnt ____cacheline_aligned_in_smp;
236b28708d6SChristoph Hellwig 	xlog_in_core_2_t	*ic_data;
237b28708d6SChristoph Hellwig #define ic_header	ic_data->hic_header
2381da177e4SLinus Torvalds } xlog_in_core_t;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds /*
24171e330b5SDave Chinner  * The CIL context is used to aggregate per-transaction details as well be
24271e330b5SDave Chinner  * passed to the iclog for checkpoint post-commit processing.  After being
24371e330b5SDave Chinner  * passed to the iclog, another context needs to be allocated for tracking the
24471e330b5SDave Chinner  * next set of transactions to be aggregated into a checkpoint.
24571e330b5SDave Chinner  */
24671e330b5SDave Chinner struct xfs_cil;
24771e330b5SDave Chinner 
24871e330b5SDave Chinner struct xfs_cil_ctx {
24971e330b5SDave Chinner 	struct xfs_cil		*cil;
25071e330b5SDave Chinner 	xfs_lsn_t		sequence;	/* chkpt sequence # */
25171e330b5SDave Chinner 	xfs_lsn_t		start_lsn;	/* first LSN of chkpt commit */
25271e330b5SDave Chinner 	xfs_lsn_t		commit_lsn;	/* chkpt commit record lsn */
25371e330b5SDave Chinner 	struct xlog_ticket	*ticket;	/* chkpt ticket */
25471e330b5SDave Chinner 	int			nvecs;		/* number of regions */
25571e330b5SDave Chinner 	int			space_used;	/* aggregate size of regions */
25671e330b5SDave Chinner 	struct list_head	busy_extents;	/* busy extents in chkpt */
25771e330b5SDave Chinner 	struct xfs_log_vec	*lv_chain;	/* logvecs being pushed */
258239880efSDave Chinner 	struct xfs_log_callback	log_cb;		/* completion callback hook. */
25971e330b5SDave Chinner 	struct list_head	committing;	/* ctx committing list */
2604560e78fSChristoph Hellwig 	struct work_struct	discard_endio_work;
26171e330b5SDave Chinner };
26271e330b5SDave Chinner 
26371e330b5SDave Chinner /*
26471e330b5SDave Chinner  * Committed Item List structure
26571e330b5SDave Chinner  *
26671e330b5SDave Chinner  * This structure is used to track log items that have been committed but not
26771e330b5SDave Chinner  * yet written into the log. It is used only when the delayed logging mount
26871e330b5SDave Chinner  * option is enabled.
26971e330b5SDave Chinner  *
27071e330b5SDave Chinner  * This structure tracks the list of committing checkpoint contexts so
27171e330b5SDave Chinner  * we can avoid the problem of having to hold out new transactions during a
27271e330b5SDave Chinner  * flush until we have a the commit record LSN of the checkpoint. We can
27371e330b5SDave Chinner  * traverse the list of committing contexts in xlog_cil_push_lsn() to find a
27471e330b5SDave Chinner  * sequence match and extract the commit LSN directly from there. If the
27571e330b5SDave Chinner  * checkpoint is still in the process of committing, we can block waiting for
27671e330b5SDave Chinner  * the commit LSN to be determined as well. This should make synchronous
27771e330b5SDave Chinner  * operations almost as efficient as the old logging methods.
27871e330b5SDave Chinner  */
27971e330b5SDave Chinner struct xfs_cil {
280ad223e60SMark Tinguely 	struct xlog		*xc_log;
28171e330b5SDave Chinner 	struct list_head	xc_cil;
28271e330b5SDave Chinner 	spinlock_t		xc_cil_lock;
2834bb928cdSDave Chinner 
2844bb928cdSDave Chinner 	struct rw_semaphore	xc_ctx_lock ____cacheline_aligned_in_smp;
28571e330b5SDave Chinner 	struct xfs_cil_ctx	*xc_ctx;
2864bb928cdSDave Chinner 
2874bb928cdSDave Chinner 	spinlock_t		xc_push_lock ____cacheline_aligned_in_smp;
2884bb928cdSDave Chinner 	xfs_lsn_t		xc_push_seq;
28971e330b5SDave Chinner 	struct list_head	xc_committing;
290eb40a875SDave Chinner 	wait_queue_head_t	xc_commit_wait;
291a44f13edSDave Chinner 	xfs_lsn_t		xc_current_sequence;
2924c2d542fSDave Chinner 	struct work_struct	xc_push_work;
2934bb928cdSDave Chinner } ____cacheline_aligned_in_smp;
29471e330b5SDave Chinner 
29571e330b5SDave Chinner /*
29680168676SDave Chinner  * The amount of log space we allow the CIL to aggregate is difficult to size.
29780168676SDave Chinner  * Whatever we choose, we have to make sure we can get a reservation for the
29880168676SDave Chinner  * log space effectively, that it is large enough to capture sufficient
29980168676SDave Chinner  * relogging to reduce log buffer IO significantly, but it is not too large for
30080168676SDave Chinner  * the log or induces too much latency when writing out through the iclogs. We
30180168676SDave Chinner  * track both space consumed and the number of vectors in the checkpoint
30280168676SDave Chinner  * context, so we need to decide which to use for limiting.
303df806158SDave Chinner  *
304df806158SDave Chinner  * Every log buffer we write out during a push needs a header reserved, which
305df806158SDave Chinner  * is at least one sector and more for v2 logs. Hence we need a reservation of
306df806158SDave Chinner  * at least 512 bytes per 32k of log space just for the LR headers. That means
307df806158SDave Chinner  * 16KB of reservation per megabyte of delayed logging space we will consume,
308df806158SDave Chinner  * plus various headers.  The number of headers will vary based on the num of
309df806158SDave Chinner  * io vectors, so limiting on a specific number of vectors is going to result
310df806158SDave Chinner  * in transactions of varying size. IOWs, it is more consistent to track and
311df806158SDave Chinner  * limit space consumed in the log rather than by the number of objects being
312df806158SDave Chinner  * logged in order to prevent checkpoint ticket overruns.
313df806158SDave Chinner  *
314df806158SDave Chinner  * Further, use of static reservations through the log grant mechanism is
315df806158SDave Chinner  * problematic. It introduces a lot of complexity (e.g. reserve grant vs write
316df806158SDave Chinner  * grant) and a significant deadlock potential because regranting write space
317df806158SDave Chinner  * can block on log pushes. Hence if we have to regrant log space during a log
318df806158SDave Chinner  * push, we can deadlock.
319df806158SDave Chinner  *
320df806158SDave Chinner  * However, we can avoid this by use of a dynamic "reservation stealing"
321df806158SDave Chinner  * technique during transaction commit whereby unused reservation space in the
322df806158SDave Chinner  * transaction ticket is transferred to the CIL ctx commit ticket to cover the
323df806158SDave Chinner  * space needed by the checkpoint transaction. This means that we never need to
324df806158SDave Chinner  * specifically reserve space for the CIL checkpoint transaction, nor do we
325df806158SDave Chinner  * need to regrant space once the checkpoint completes. This also means the
326df806158SDave Chinner  * checkpoint transaction ticket is specific to the checkpoint context, rather
327df806158SDave Chinner  * than the CIL itself.
328df806158SDave Chinner  *
32980168676SDave Chinner  * With dynamic reservations, we can effectively make up arbitrary limits for
33080168676SDave Chinner  * the checkpoint size so long as they don't violate any other size rules.
33180168676SDave Chinner  * Recovery imposes a rule that no transaction exceed half the log, so we are
33280168676SDave Chinner  * limited by that.  Furthermore, the log transaction reservation subsystem
33380168676SDave Chinner  * tries to keep 25% of the log free, so we need to keep below that limit or we
33480168676SDave Chinner  * risk running out of free log space to start any new transactions.
33580168676SDave Chinner  *
33680168676SDave Chinner  * In order to keep background CIL push efficient, we will set a lower
33780168676SDave Chinner  * threshold at which background pushing is attempted without blocking current
33880168676SDave Chinner  * transaction commits.  A separate, higher bound defines when CIL pushes are
33980168676SDave Chinner  * enforced to ensure we stay within our maximum checkpoint size bounds.
34080168676SDave Chinner  * threshold, yet give us plenty of space for aggregation on large logs.
341df806158SDave Chinner  */
34280168676SDave Chinner #define XLOG_CIL_SPACE_LIMIT(log)	(log->l_logsize >> 3)
343df806158SDave Chinner 
344df806158SDave Chinner /*
34528496968SChristoph Hellwig  * ticket grant locks, queues and accounting have their own cachlines
34628496968SChristoph Hellwig  * as these are quite hot and can be operated on concurrently.
34728496968SChristoph Hellwig  */
34828496968SChristoph Hellwig struct xlog_grant_head {
34928496968SChristoph Hellwig 	spinlock_t		lock ____cacheline_aligned_in_smp;
35028496968SChristoph Hellwig 	struct list_head	waiters;
35128496968SChristoph Hellwig 	atomic64_t		grant;
35228496968SChristoph Hellwig };
35328496968SChristoph Hellwig 
35428496968SChristoph Hellwig /*
3551da177e4SLinus Torvalds  * The reservation head lsn is not made up of a cycle number and block number.
3561da177e4SLinus Torvalds  * Instead, it uses a cycle number and byte number.  Logs don't expect to
3571da177e4SLinus Torvalds  * overflow 31 bits worth of byte offset, so using a byte number will mean
3581da177e4SLinus Torvalds  * that round off problems won't occur when releasing partial reservations.
3591da177e4SLinus Torvalds  */
3609a8d2fdbSMark Tinguely struct xlog {
3614679b2d3SDavid Chinner 	/* The following fields don't need locking */
3624679b2d3SDavid Chinner 	struct xfs_mount	*l_mp;	        /* mount point */
363a9c21c1bSDavid Chinner 	struct xfs_ail		*l_ailp;	/* AIL log is working with */
36471e330b5SDave Chinner 	struct xfs_cil		*l_cilp;	/* CIL log is working with */
3654679b2d3SDavid Chinner 	struct xfs_buf		*l_xbuf;        /* extra buffer for log
3664679b2d3SDavid Chinner 						 * wrapping */
3674679b2d3SDavid Chinner 	struct xfs_buftarg	*l_targ;        /* buftarg of log */
368f661f1e0SDave Chinner 	struct delayed_work	l_work;		/* background flush work */
3694679b2d3SDavid Chinner 	uint			l_flags;
3704679b2d3SDavid Chinner 	uint			l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */
371d5689eaaSChristoph Hellwig 	struct list_head	*l_buf_cancel_table;
3724679b2d3SDavid Chinner 	int			l_iclog_hsize;  /* size of iclog header */
3734679b2d3SDavid Chinner 	int			l_iclog_heads;  /* # of iclog header sectors */
37448389ef1SAlex Elder 	uint			l_sectBBsize;   /* sector size in BBs (2^n) */
3754679b2d3SDavid Chinner 	int			l_iclog_size;	/* size of log in bytes */
3764679b2d3SDavid Chinner 	int			l_iclog_size_log; /* log power size of log */
3774679b2d3SDavid Chinner 	int			l_iclog_bufs;	/* number of iclog buffers */
3784679b2d3SDavid Chinner 	xfs_daddr_t		l_logBBstart;   /* start block of log */
3794679b2d3SDavid Chinner 	int			l_logsize;      /* size of log in bytes */
3804679b2d3SDavid Chinner 	int			l_logBBsize;    /* size of log in BB chunks */
3814679b2d3SDavid Chinner 
3821da177e4SLinus Torvalds 	/* The following block of fields are changed while holding icloglock */
383eb40a875SDave Chinner 	wait_queue_head_t	l_flush_wait ____cacheline_aligned_in_smp;
384d748c623SMatthew Wilcox 						/* waiting for iclog flush */
3851da177e4SLinus Torvalds 	int			l_covered_state;/* state of "covering disk
3861da177e4SLinus Torvalds 						 * log entries" */
3871da177e4SLinus Torvalds 	xlog_in_core_t		*l_iclog;       /* head log queue	*/
388b22cd72cSEric Sandeen 	spinlock_t		l_icloglock;    /* grab to change iclog state */
3891da177e4SLinus Torvalds 	int			l_curr_cycle;   /* Cycle number of log writes */
3901da177e4SLinus Torvalds 	int			l_prev_cycle;   /* Cycle number before last
3911da177e4SLinus Torvalds 						 * block increment */
3921da177e4SLinus Torvalds 	int			l_curr_block;   /* current logical log block */
3931da177e4SLinus Torvalds 	int			l_prev_block;   /* previous logical log block */
3941da177e4SLinus Torvalds 
39584f3c683SDave Chinner 	/*
3961c3cb9ecSDave Chinner 	 * l_last_sync_lsn and l_tail_lsn are atomics so they can be set and
3971c3cb9ecSDave Chinner 	 * read without needing to hold specific locks. To avoid operations
3981c3cb9ecSDave Chinner 	 * contending with other hot objects, place each of them on a separate
3991c3cb9ecSDave Chinner 	 * cacheline.
40084f3c683SDave Chinner 	 */
40184f3c683SDave Chinner 	/* lsn of last LR on disk */
40284f3c683SDave Chinner 	atomic64_t		l_last_sync_lsn ____cacheline_aligned_in_smp;
4031c3cb9ecSDave Chinner 	/* lsn of 1st LR with unflushed * buffers */
4041c3cb9ecSDave Chinner 	atomic64_t		l_tail_lsn ____cacheline_aligned_in_smp;
40584f3c683SDave Chinner 
40628496968SChristoph Hellwig 	struct xlog_grant_head	l_reserve_head;
40728496968SChristoph Hellwig 	struct xlog_grant_head	l_write_head;
4083f16b985SDave Chinner 
409baff4e44SBrian Foster 	struct xfs_kobj		l_kobj;
410baff4e44SBrian Foster 
4114679b2d3SDavid Chinner 	/* The following field are used for debugging; need to hold icloglock */
4124679b2d3SDavid Chinner #ifdef DEBUG
4135809d5e0SChristoph Hellwig 	void			*l_iclog_bak[XLOG_MAX_ICLOGS];
414609adfc2SBrian Foster 	/* log record crc error injection factor */
415609adfc2SBrian Foster 	uint32_t		l_badcrc_factor;
4164679b2d3SDavid Chinner #endif
41712818d24SBrian Foster 	/* log recovery lsn tracking (for buffer submission */
41812818d24SBrian Foster 	xfs_lsn_t		l_recovery_lsn;
4199a8d2fdbSMark Tinguely };
4201da177e4SLinus Torvalds 
421d5689eaaSChristoph Hellwig #define XLOG_BUF_CANCEL_BUCKET(log, blkno) \
422d5689eaaSChristoph Hellwig 	((log)->l_buf_cancel_table + ((__uint64_t)blkno % XLOG_BC_TABLE_SIZE))
423d5689eaaSChristoph Hellwig 
424cfcbbbd0SNathan Scott #define XLOG_FORCED_SHUTDOWN(log)	((log)->l_flags & XLOG_IO_ERROR)
425cfcbbbd0SNathan Scott 
4261da177e4SLinus Torvalds /* common routines */
4279a8d2fdbSMark Tinguely extern int
4289a8d2fdbSMark Tinguely xlog_recover(
4299a8d2fdbSMark Tinguely 	struct xlog		*log);
4309a8d2fdbSMark Tinguely extern int
4319a8d2fdbSMark Tinguely xlog_recover_finish(
4329a8d2fdbSMark Tinguely 	struct xlog		*log);
433f0b2efadSBrian Foster extern int
434f0b2efadSBrian Foster xlog_recover_cancel(struct xlog *);
4350e446be4SChristoph Hellwig 
436f9668a09SDave Chinner extern __le32	 xlog_cksum(struct xlog *log, struct xlog_rec_header *rhead,
4370e446be4SChristoph Hellwig 			    char *dp, int size);
4381da177e4SLinus Torvalds 
439eb01c9cdSDavid Chinner extern kmem_zone_t *xfs_log_ticket_zone;
440ad223e60SMark Tinguely struct xlog_ticket *
441ad223e60SMark Tinguely xlog_ticket_alloc(
442ad223e60SMark Tinguely 	struct xlog	*log,
443ad223e60SMark Tinguely 	int		unit_bytes,
444ad223e60SMark Tinguely 	int		count,
445ad223e60SMark Tinguely 	char		client,
446ad223e60SMark Tinguely 	bool		permanent,
44777ba7877SAl Viro 	xfs_km_flags_t	alloc_flags);
44871e330b5SDave Chinner 
449eb01c9cdSDavid Chinner 
450e6b1f273SChristoph Hellwig static inline void
451e6b1f273SChristoph Hellwig xlog_write_adv_cnt(void **ptr, int *len, int *off, size_t bytes)
452e6b1f273SChristoph Hellwig {
453e6b1f273SChristoph Hellwig 	*ptr += bytes;
454e6b1f273SChristoph Hellwig 	*len -= bytes;
455e6b1f273SChristoph Hellwig 	*off += bytes;
456e6b1f273SChristoph Hellwig }
457e6b1f273SChristoph Hellwig 
45871e330b5SDave Chinner void	xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket);
459*d4ca1d55SBrian Foster void	xlog_print_trans(struct xfs_trans *);
460ad223e60SMark Tinguely int
461ad223e60SMark Tinguely xlog_write(
462ad223e60SMark Tinguely 	struct xlog		*log,
463ad223e60SMark Tinguely 	struct xfs_log_vec	*log_vector,
464ad223e60SMark Tinguely 	struct xlog_ticket	*tic,
465ad223e60SMark Tinguely 	xfs_lsn_t		*start_lsn,
466ad223e60SMark Tinguely 	struct xlog_in_core	**commit_iclog,
467ad223e60SMark Tinguely 	uint			flags);
46871e330b5SDave Chinner 
46971e330b5SDave Chinner /*
4701c3cb9ecSDave Chinner  * When we crack an atomic LSN, we sample it first so that the value will not
4711c3cb9ecSDave Chinner  * change while we are cracking it into the component values. This means we
4721c3cb9ecSDave Chinner  * will always get consistent component values to work from. This should always
47325985edcSLucas De Marchi  * be used to sample and crack LSNs that are stored and updated in atomic
4741c3cb9ecSDave Chinner  * variables.
4751c3cb9ecSDave Chinner  */
4761c3cb9ecSDave Chinner static inline void
4771c3cb9ecSDave Chinner xlog_crack_atomic_lsn(atomic64_t *lsn, uint *cycle, uint *block)
4781c3cb9ecSDave Chinner {
4791c3cb9ecSDave Chinner 	xfs_lsn_t val = atomic64_read(lsn);
4801c3cb9ecSDave Chinner 
4811c3cb9ecSDave Chinner 	*cycle = CYCLE_LSN(val);
4821c3cb9ecSDave Chinner 	*block = BLOCK_LSN(val);
4831c3cb9ecSDave Chinner }
4841c3cb9ecSDave Chinner 
4851c3cb9ecSDave Chinner /*
4861c3cb9ecSDave Chinner  * Calculate and assign a value to an atomic LSN variable from component pieces.
4871c3cb9ecSDave Chinner  */
4881c3cb9ecSDave Chinner static inline void
4891c3cb9ecSDave Chinner xlog_assign_atomic_lsn(atomic64_t *lsn, uint cycle, uint block)
4901c3cb9ecSDave Chinner {
4911c3cb9ecSDave Chinner 	atomic64_set(lsn, xlog_assign_lsn(cycle, block));
4921c3cb9ecSDave Chinner }
4931c3cb9ecSDave Chinner 
4941c3cb9ecSDave Chinner /*
495d0eb2f38SDave Chinner  * When we crack the grant head, we sample it first so that the value will not
496a69ed03cSDave Chinner  * change while we are cracking it into the component values. This means we
497a69ed03cSDave Chinner  * will always get consistent component values to work from.
498a69ed03cSDave Chinner  */
499a69ed03cSDave Chinner static inline void
500d0eb2f38SDave Chinner xlog_crack_grant_head_val(int64_t val, int *cycle, int *space)
501a69ed03cSDave Chinner {
502a69ed03cSDave Chinner 	*cycle = val >> 32;
503a69ed03cSDave Chinner 	*space = val & 0xffffffff;
504a69ed03cSDave Chinner }
505a69ed03cSDave Chinner 
506a69ed03cSDave Chinner static inline void
507d0eb2f38SDave Chinner xlog_crack_grant_head(atomic64_t *head, int *cycle, int *space)
508d0eb2f38SDave Chinner {
509d0eb2f38SDave Chinner 	xlog_crack_grant_head_val(atomic64_read(head), cycle, space);
510d0eb2f38SDave Chinner }
511d0eb2f38SDave Chinner 
512d0eb2f38SDave Chinner static inline int64_t
513d0eb2f38SDave Chinner xlog_assign_grant_head_val(int cycle, int space)
514d0eb2f38SDave Chinner {
515d0eb2f38SDave Chinner 	return ((int64_t)cycle << 32) | space;
516d0eb2f38SDave Chinner }
517d0eb2f38SDave Chinner 
518d0eb2f38SDave Chinner static inline void
519c8a09ff8SDave Chinner xlog_assign_grant_head(atomic64_t *head, int cycle, int space)
520a69ed03cSDave Chinner {
521d0eb2f38SDave Chinner 	atomic64_set(head, xlog_assign_grant_head_val(cycle, space));
522a69ed03cSDave Chinner }
523a69ed03cSDave Chinner 
524a69ed03cSDave Chinner /*
52571e330b5SDave Chinner  * Committed Item List interfaces
52671e330b5SDave Chinner  */
5272c6e24ceSDave Chinner int	xlog_cil_init(struct xlog *log);
5282c6e24ceSDave Chinner void	xlog_cil_init_post_recovery(struct xlog *log);
5292c6e24ceSDave Chinner void	xlog_cil_destroy(struct xlog *log);
5302c6e24ceSDave Chinner bool	xlog_cil_empty(struct xlog *log);
53171e330b5SDave Chinner 
532a44f13edSDave Chinner /*
533a44f13edSDave Chinner  * CIL force routines
534a44f13edSDave Chinner  */
535ad223e60SMark Tinguely xfs_lsn_t
536ad223e60SMark Tinguely xlog_cil_force_lsn(
537ad223e60SMark Tinguely 	struct xlog *log,
538ad223e60SMark Tinguely 	xfs_lsn_t sequence);
539a44f13edSDave Chinner 
540a44f13edSDave Chinner static inline void
541ad223e60SMark Tinguely xlog_cil_force(struct xlog *log)
542a44f13edSDave Chinner {
543a44f13edSDave Chinner 	xlog_cil_force_lsn(log, log->l_cilp->xc_current_sequence);
544a44f13edSDave Chinner }
54571e330b5SDave Chinner 
546955e47adSTim Shimmin /*
547955e47adSTim Shimmin  * Unmount record type is used as a pseudo transaction type for the ticket.
548955e47adSTim Shimmin  * It's value must be outside the range of XFS_TRANS_* values.
549955e47adSTim Shimmin  */
550955e47adSTim Shimmin #define XLOG_UNMOUNT_REC_TYPE	(-1U)
551955e47adSTim Shimmin 
552eb40a875SDave Chinner /*
553eb40a875SDave Chinner  * Wrapper function for waiting on a wait queue serialised against wakeups
554eb40a875SDave Chinner  * by a spinlock. This matches the semantics of all the wait queues used in the
555eb40a875SDave Chinner  * log code.
556eb40a875SDave Chinner  */
557eb40a875SDave Chinner static inline void xlog_wait(wait_queue_head_t *wq, spinlock_t *lock)
558eb40a875SDave Chinner {
559eb40a875SDave Chinner 	DECLARE_WAITQUEUE(wait, current);
560eb40a875SDave Chinner 
561eb40a875SDave Chinner 	add_wait_queue_exclusive(wq, &wait);
562eb40a875SDave Chinner 	__set_current_state(TASK_UNINTERRUPTIBLE);
563eb40a875SDave Chinner 	spin_unlock(lock);
564eb40a875SDave Chinner 	schedule();
565eb40a875SDave Chinner 	remove_wait_queue(wq, &wait);
566eb40a875SDave Chinner }
5671da177e4SLinus Torvalds 
568a45086e2SBrian Foster /*
569a45086e2SBrian Foster  * The LSN is valid so long as it is behind the current LSN. If it isn't, this
570a45086e2SBrian Foster  * means that the next log record that includes this metadata could have a
571a45086e2SBrian Foster  * smaller LSN. In turn, this means that the modification in the log would not
572a45086e2SBrian Foster  * replay.
573a45086e2SBrian Foster  */
574a45086e2SBrian Foster static inline bool
575a45086e2SBrian Foster xlog_valid_lsn(
576a45086e2SBrian Foster 	struct xlog	*log,
577a45086e2SBrian Foster 	xfs_lsn_t	lsn)
578a45086e2SBrian Foster {
579a45086e2SBrian Foster 	int		cur_cycle;
580a45086e2SBrian Foster 	int		cur_block;
581a45086e2SBrian Foster 	bool		valid = true;
582a45086e2SBrian Foster 
583a45086e2SBrian Foster 	/*
584a45086e2SBrian Foster 	 * First, sample the current lsn without locking to avoid added
585a45086e2SBrian Foster 	 * contention from metadata I/O. The current cycle and block are updated
586a45086e2SBrian Foster 	 * (in xlog_state_switch_iclogs()) and read here in a particular order
587a45086e2SBrian Foster 	 * to avoid false negatives (e.g., thinking the metadata LSN is valid
588a45086e2SBrian Foster 	 * when it is not).
589a45086e2SBrian Foster 	 *
590a45086e2SBrian Foster 	 * The current block is always rewound before the cycle is bumped in
591a45086e2SBrian Foster 	 * xlog_state_switch_iclogs() to ensure the current LSN is never seen in
592a45086e2SBrian Foster 	 * a transiently forward state. Instead, we can see the LSN in a
593a45086e2SBrian Foster 	 * transiently behind state if we happen to race with a cycle wrap.
594a45086e2SBrian Foster 	 */
595a45086e2SBrian Foster 	cur_cycle = ACCESS_ONCE(log->l_curr_cycle);
596a45086e2SBrian Foster 	smp_rmb();
597a45086e2SBrian Foster 	cur_block = ACCESS_ONCE(log->l_curr_block);
598a45086e2SBrian Foster 
599a45086e2SBrian Foster 	if ((CYCLE_LSN(lsn) > cur_cycle) ||
600a45086e2SBrian Foster 	    (CYCLE_LSN(lsn) == cur_cycle && BLOCK_LSN(lsn) > cur_block)) {
601a45086e2SBrian Foster 		/*
602a45086e2SBrian Foster 		 * If the metadata LSN appears invalid, it's possible the check
603a45086e2SBrian Foster 		 * above raced with a wrap to the next log cycle. Grab the lock
604a45086e2SBrian Foster 		 * to check for sure.
605a45086e2SBrian Foster 		 */
606a45086e2SBrian Foster 		spin_lock(&log->l_icloglock);
607a45086e2SBrian Foster 		cur_cycle = log->l_curr_cycle;
608a45086e2SBrian Foster 		cur_block = log->l_curr_block;
609a45086e2SBrian Foster 		spin_unlock(&log->l_icloglock);
610a45086e2SBrian Foster 
611a45086e2SBrian Foster 		if ((CYCLE_LSN(lsn) > cur_cycle) ||
612a45086e2SBrian Foster 		    (CYCLE_LSN(lsn) == cur_cycle && BLOCK_LSN(lsn) > cur_block))
613a45086e2SBrian Foster 			valid = false;
614a45086e2SBrian Foster 	}
615a45086e2SBrian Foster 
616a45086e2SBrian Foster 	return valid;
617a45086e2SBrian Foster }
618a45086e2SBrian Foster 
6191da177e4SLinus Torvalds #endif	/* __XFS_LOG_PRIV_H__ */
620