xref: /linux/fs/xfs/xfs_log_priv.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #ifndef	__XFS_LOG_PRIV_H__
71da177e4SLinus Torvalds #define __XFS_LOG_PRIV_H__
81da177e4SLinus Torvalds 
9428c4435SDave Chinner #include "xfs_extent_busy.h"	/* for struct xfs_busy_extents */
10428c4435SDave Chinner 
111da177e4SLinus Torvalds struct xfs_buf;
12ad223e60SMark Tinguely struct xlog;
13a844f451SNathan Scott struct xlog_ticket;
141da177e4SLinus Torvalds struct xfs_mount;
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  * get client id from packed copy.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * this hack is here because the xlog_pack code copies four bytes
201da177e4SLinus Torvalds  * of xlog_op_header containing the fields oh_clientid, oh_flags
211da177e4SLinus Torvalds  * and oh_res2 into the packed copy.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * later on this four byte chunk is treated as an int and the
241da177e4SLinus Torvalds  * client id is pulled out.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * this has endian issues, of course.
271da177e4SLinus Torvalds  */
xlog_get_client_id(__be32 i)28b53e675dSChristoph Hellwig static inline uint xlog_get_client_id(__be32 i)
2903bea6feSChristoph Hellwig {
30b53e675dSChristoph Hellwig 	return be32_to_cpu(i) >> 24;
3103bea6feSChristoph Hellwig }
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  * In core log state
351da177e4SLinus Torvalds  */
361858bb0bSChristoph Hellwig enum xlog_iclog_state {
371858bb0bSChristoph Hellwig 	XLOG_STATE_ACTIVE,	/* Current IC log being written to */
381858bb0bSChristoph Hellwig 	XLOG_STATE_WANT_SYNC,	/* Want to sync this iclog; no more writes */
391858bb0bSChristoph Hellwig 	XLOG_STATE_SYNCING,	/* This IC log is syncing */
401858bb0bSChristoph Hellwig 	XLOG_STATE_DONE_SYNC,	/* Done syncing to disk */
411858bb0bSChristoph Hellwig 	XLOG_STATE_CALLBACK,	/* Callback functions now */
421858bb0bSChristoph Hellwig 	XLOG_STATE_DIRTY,	/* Dirty IC log, not ready for ACTIVE status */
431858bb0bSChristoph Hellwig };
441da177e4SLinus Torvalds 
45956f6daaSDave Chinner #define XLOG_STATE_STRINGS \
46956f6daaSDave Chinner 	{ XLOG_STATE_ACTIVE,	"XLOG_STATE_ACTIVE" }, \
47956f6daaSDave Chinner 	{ XLOG_STATE_WANT_SYNC,	"XLOG_STATE_WANT_SYNC" }, \
48956f6daaSDave Chinner 	{ XLOG_STATE_SYNCING,	"XLOG_STATE_SYNCING" }, \
49956f6daaSDave Chinner 	{ XLOG_STATE_DONE_SYNC,	"XLOG_STATE_DONE_SYNC" }, \
50956f6daaSDave Chinner 	{ XLOG_STATE_CALLBACK,	"XLOG_STATE_CALLBACK" }, \
515112e206SDave Chinner 	{ XLOG_STATE_DIRTY,	"XLOG_STATE_DIRTY" }
52956f6daaSDave Chinner 
53b2ae3a9eSDave Chinner /*
54b2ae3a9eSDave Chinner  * In core log flags
55b2ae3a9eSDave Chinner  */
56c60d13eaSDave Chinner #define XLOG_ICL_NEED_FLUSH	(1u << 0)	/* iclog needs REQ_PREFLUSH */
57c60d13eaSDave Chinner #define XLOG_ICL_NEED_FUA	(1u << 1)	/* iclog needs REQ_FUA */
58b2ae3a9eSDave Chinner 
59b2ae3a9eSDave Chinner #define XLOG_ICL_STRINGS \
60b2ae3a9eSDave Chinner 	{ XLOG_ICL_NEED_FLUSH,	"XLOG_ICL_NEED_FLUSH" }, \
61b2ae3a9eSDave Chinner 	{ XLOG_ICL_NEED_FUA,	"XLOG_ICL_NEED_FUA" }
62b2ae3a9eSDave Chinner 
63956f6daaSDave Chinner 
641da177e4SLinus Torvalds /*
6570e42f2dSDave Chinner  * Log ticket flags
661da177e4SLinus Torvalds  */
67c60d13eaSDave Chinner #define XLOG_TIC_PERM_RESERV	(1u << 0)	/* permanent reservation */
680b1b213fSChristoph Hellwig 
690b1b213fSChristoph Hellwig #define XLOG_TIC_FLAGS \
7010547941SDave Chinner 	{ XLOG_TIC_PERM_RESERV,	"XLOG_TIC_PERM_RESERV" }
710b1b213fSChristoph Hellwig 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  * Below are states for covering allocation transactions.
741da177e4SLinus Torvalds  * By covering, we mean changing the h_tail_lsn in the last on-disk
751da177e4SLinus Torvalds  * log write such that no allocation transactions will be re-done during
761da177e4SLinus Torvalds  * recovery after a system crash. Recovery starts at the last on-disk
771da177e4SLinus Torvalds  * log write.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * These states are used to insert dummy log entries to cover
801da177e4SLinus Torvalds  * space allocation transactions which can undo non-transactional changes
811da177e4SLinus Torvalds  * after a crash. Writes to a file with space
821da177e4SLinus Torvalds  * already allocated do not result in any transactions. Allocations
831da177e4SLinus Torvalds  * might include space beyond the EOF. So if we just push the EOF a
841da177e4SLinus Torvalds  * little, the last transaction for the file could contain the wrong
851da177e4SLinus Torvalds  * size. If there is no file system activity, after an allocation
861da177e4SLinus Torvalds  * transaction, and the system crashes, the allocation transaction
871da177e4SLinus Torvalds  * will get replayed and the file will be truncated. This could
881da177e4SLinus Torvalds  * be hours/days/... after the allocation occurred.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * The fix for this is to do two dummy transactions when the
911da177e4SLinus Torvalds  * system is idle. We need two dummy transaction because the h_tail_lsn
921da177e4SLinus Torvalds  * in the log record header needs to point beyond the last possible
931da177e4SLinus Torvalds  * non-dummy transaction. The first dummy changes the h_tail_lsn to
941da177e4SLinus Torvalds  * the first transaction before the dummy. The second dummy causes
951da177e4SLinus Torvalds  * h_tail_lsn to point to the first dummy. Recovery starts at h_tail_lsn.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * These dummy transactions get committed when everything
981da177e4SLinus Torvalds  * is idle (after there has been some activity).
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  * There are 5 states used to control this.
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  *  IDLE -- no logging has been done on the file system or
1031da177e4SLinus Torvalds  *		we are done covering previous transactions.
1041da177e4SLinus Torvalds  *  NEED -- logging has occurred and we need a dummy transaction
1051da177e4SLinus Torvalds  *		when the log becomes idle.
1061da177e4SLinus Torvalds  *  DONE -- we were in the NEED state and have committed a dummy
1071da177e4SLinus Torvalds  *		transaction.
1081da177e4SLinus Torvalds  *  NEED2 -- we detected that a dummy transaction has gone to the
1091da177e4SLinus Torvalds  *		on disk log with no other transactions.
1101da177e4SLinus Torvalds  *  DONE2 -- we committed a dummy transaction when in the NEED2 state.
1111da177e4SLinus Torvalds  *
1121da177e4SLinus Torvalds  * There are two places where we switch states:
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * 1.) In xfs_sync, when we detect an idle log and are in NEED or NEED2.
1151da177e4SLinus Torvalds  *	We commit the dummy transaction and switch to DONE or DONE2,
1161da177e4SLinus Torvalds  *	respectively. In all other states, we don't do anything.
1171da177e4SLinus Torvalds  *
1181da177e4SLinus Torvalds  * 2.) When we finish writing the on-disk log (xlog_state_clean_log).
1191da177e4SLinus Torvalds  *
1201da177e4SLinus Torvalds  *	No matter what state we are in, if this isn't the dummy
1211da177e4SLinus Torvalds  *	transaction going out, the next state is NEED.
1221da177e4SLinus Torvalds  *	So, if we aren't in the DONE or DONE2 states, the next state
1231da177e4SLinus Torvalds  *	is NEED. We can't be finishing a write of the dummy record
1241da177e4SLinus Torvalds  *	unless it was committed and the state switched to DONE or DONE2.
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  *	If we are in the DONE state and this was a write of the
1271da177e4SLinus Torvalds  *		dummy transaction, we move to NEED2.
1281da177e4SLinus Torvalds  *
1291da177e4SLinus Torvalds  *	If we are in the DONE2 state and this was a write of the
1301da177e4SLinus Torvalds  *		dummy transaction, we move to IDLE.
1311da177e4SLinus Torvalds  *
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  * Writing only one dummy transaction can get appended to
1341da177e4SLinus Torvalds  * one file space allocation. When this happens, the log recovery
1351da177e4SLinus Torvalds  * code replays the space allocation and a file could be truncated.
1361da177e4SLinus Torvalds  * This is why we have the NEED2 and DONE2 states before going idle.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #define XLOG_STATE_COVER_IDLE	0
1401da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED	1
1411da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE	2
1421da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED2	3
1431da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE2	4
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #define XLOG_COVER_OPS		5
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds typedef struct xlog_ticket {
14810547941SDave Chinner 	struct list_head	t_queue;	/* reserve/write queue */
14914a7235fSChristoph Hellwig 	struct task_struct	*t_task;	/* task that owns this ticket */
15031151cc3SDave Chinner 	xlog_tid_t		t_tid;		/* transaction identifier */
15131151cc3SDave Chinner 	atomic_t		t_ref;		/* ticket reference count */
15231151cc3SDave Chinner 	int			t_curr_res;	/* current reservation */
15331151cc3SDave Chinner 	int			t_unit_res;	/* unit reservation */
15431151cc3SDave Chinner 	char			t_ocnt;		/* original unit count */
15531151cc3SDave Chinner 	char			t_cnt;		/* current unit count */
15631151cc3SDave Chinner 	uint8_t			t_flags;	/* properties of reservation */
15731151cc3SDave Chinner 	int			t_iclog_hdrs;	/* iclog hdrs in t_curr_res */
1581da177e4SLinus Torvalds } xlog_ticket_t;
1597e9c6396STim Shimmin 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  * - A log record header is 512 bytes.  There is plenty of room to grow the
1621da177e4SLinus Torvalds  *	xlog_rec_header_t into the reserved space.
1631da177e4SLinus Torvalds  * - ic_data follows, so a write to disk can start at the beginning of
1641da177e4SLinus Torvalds  *	the iclog.
16512017fafSDavid Chinner  * - ic_forcewait is used to implement synchronous forcing of the iclog to disk.
1661da177e4SLinus Torvalds  * - ic_next is the pointer to the next iclog in the ring.
1671da177e4SLinus Torvalds  * - ic_log is a pointer back to the global log structure.
16879b54d9bSChristoph Hellwig  * - ic_size is the full size of the log buffer, minus the cycle headers.
1691da177e4SLinus Torvalds  * - ic_offset is the current number of bytes written to in this iclog.
1701da177e4SLinus Torvalds  * - ic_refcnt is bumped when someone is writing to the log.
1711da177e4SLinus Torvalds  * - ic_state is the state of the iclog.
172114d23aaSDavid Chinner  *
173114d23aaSDavid Chinner  * Because of cacheline contention on large machines, we need to separate
174114d23aaSDavid Chinner  * various resources onto different cachelines. To start with, make the
175114d23aaSDavid Chinner  * structure cacheline aligned. The following fields can be contended on
176114d23aaSDavid Chinner  * by independent processes:
177114d23aaSDavid Chinner  *
17889ae379dSChristoph Hellwig  *	- ic_callbacks
179114d23aaSDavid Chinner  *	- ic_refcnt
180114d23aaSDavid Chinner  *	- fields protected by the global l_icloglock
181114d23aaSDavid Chinner  *
182114d23aaSDavid Chinner  * so we need to ensure that these fields are located in separate cachelines.
183114d23aaSDavid Chinner  * We'll put all the read-only and l_icloglock fields in the first cacheline,
184114d23aaSDavid Chinner  * and move everything else out to subsequent cachelines.
1851da177e4SLinus Torvalds  */
186b28708d6SChristoph Hellwig typedef struct xlog_in_core {
187eb40a875SDave Chinner 	wait_queue_head_t	ic_force_wait;
188eb40a875SDave Chinner 	wait_queue_head_t	ic_write_wait;
1891da177e4SLinus Torvalds 	struct xlog_in_core	*ic_next;
1901da177e4SLinus Torvalds 	struct xlog_in_core	*ic_prev;
191ad223e60SMark Tinguely 	struct xlog		*ic_log;
19279b54d9bSChristoph Hellwig 	u32			ic_size;
19379b54d9bSChristoph Hellwig 	u32			ic_offset;
1941858bb0bSChristoph Hellwig 	enum xlog_iclog_state	ic_state;
195eef983ffSDave Chinner 	unsigned int		ic_flags;
196decb545fSChristoph Hellwig 	void			*ic_datap;	/* pointer to iclog data */
19789ae379dSChristoph Hellwig 	struct list_head	ic_callbacks;
198114d23aaSDavid Chinner 
199114d23aaSDavid Chinner 	/* reference counts need their own cacheline */
200114d23aaSDavid Chinner 	atomic_t		ic_refcnt ____cacheline_aligned_in_smp;
201b28708d6SChristoph Hellwig 	xlog_in_core_2_t	*ic_data;
202b28708d6SChristoph Hellwig #define ic_header	ic_data->hic_header
203366fc4b8SChristoph Hellwig #ifdef DEBUG
204366fc4b8SChristoph Hellwig 	bool			ic_fail_crc : 1;
205366fc4b8SChristoph Hellwig #endif
20679b54d9bSChristoph Hellwig 	struct semaphore	ic_sema;
20779b54d9bSChristoph Hellwig 	struct work_struct	ic_end_io_work;
20879b54d9bSChristoph Hellwig 	struct bio		ic_bio;
20979b54d9bSChristoph Hellwig 	struct bio_vec		ic_bvec[];
2101da177e4SLinus Torvalds } xlog_in_core_t;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
21371e330b5SDave Chinner  * The CIL context is used to aggregate per-transaction details as well be
21471e330b5SDave Chinner  * passed to the iclog for checkpoint post-commit processing.  After being
21571e330b5SDave Chinner  * passed to the iclog, another context needs to be allocated for tracking the
21671e330b5SDave Chinner  * next set of transactions to be aggregated into a checkpoint.
21771e330b5SDave Chinner  */
21871e330b5SDave Chinner struct xfs_cil;
21971e330b5SDave Chinner 
22071e330b5SDave Chinner struct xfs_cil_ctx {
22171e330b5SDave Chinner 	struct xfs_cil		*cil;
2225f9b4b0dSDave Chinner 	xfs_csn_t		sequence;	/* chkpt sequence # */
22371e330b5SDave Chinner 	xfs_lsn_t		start_lsn;	/* first LSN of chkpt commit */
22471e330b5SDave Chinner 	xfs_lsn_t		commit_lsn;	/* chkpt commit record lsn */
225caa80090SDave Chinner 	struct xlog_in_core	*commit_iclog;
22671e330b5SDave Chinner 	struct xlog_ticket	*ticket;	/* chkpt ticket */
2277c8ade21SDave Chinner 	atomic_t		space_used;	/* aggregate size of regions */
228428c4435SDave Chinner 	struct xfs_busy_extents	busy_extents;
229c0fb4765SDave Chinner 	struct list_head	log_items;	/* log items in chkpt */
23016924853SDave Chinner 	struct list_head	lv_chain;	/* logvecs being pushed */
23189ae379dSChristoph Hellwig 	struct list_head	iclog_entry;
23271e330b5SDave Chinner 	struct list_head	committing;	/* ctx committing list */
23339823d0fSDave Chinner 	struct work_struct	push_work;
234016a2338SDave Chinner 	atomic_t		order_id;
235ecd49f7aSDarrick J. Wong 
236ecd49f7aSDarrick J. Wong 	/*
237ecd49f7aSDarrick J. Wong 	 * CPUs that could have added items to the percpu CIL data.  Access is
238ecd49f7aSDarrick J. Wong 	 * coordinated with xc_ctx_lock.
239ecd49f7aSDarrick J. Wong 	 */
240ecd49f7aSDarrick J. Wong 	struct cpumask		cil_pcpmask;
24171e330b5SDave Chinner };
24271e330b5SDave Chinner 
24371e330b5SDave Chinner /*
244af1c2146SDave Chinner  * Per-cpu CIL tracking items
245af1c2146SDave Chinner  */
246af1c2146SDave Chinner struct xlog_cil_pcp {
2477c8ade21SDave Chinner 	int32_t			space_used;
2481dd2a2c1SDave Chinner 	uint32_t		space_reserved;
249af1c2146SDave Chinner 	struct list_head	busy_extents;
250af1c2146SDave Chinner 	struct list_head	log_items;
251af1c2146SDave Chinner };
252af1c2146SDave Chinner 
253af1c2146SDave Chinner /*
25471e330b5SDave Chinner  * Committed Item List structure
25571e330b5SDave Chinner  *
25671e330b5SDave Chinner  * This structure is used to track log items that have been committed but not
25771e330b5SDave Chinner  * yet written into the log. It is used only when the delayed logging mount
25871e330b5SDave Chinner  * option is enabled.
25971e330b5SDave Chinner  *
26071e330b5SDave Chinner  * This structure tracks the list of committing checkpoint contexts so
26171e330b5SDave Chinner  * we can avoid the problem of having to hold out new transactions during a
26271e330b5SDave Chinner  * flush until we have a the commit record LSN of the checkpoint. We can
26371e330b5SDave Chinner  * traverse the list of committing contexts in xlog_cil_push_lsn() to find a
26471e330b5SDave Chinner  * sequence match and extract the commit LSN directly from there. If the
26571e330b5SDave Chinner  * checkpoint is still in the process of committing, we can block waiting for
26671e330b5SDave Chinner  * the commit LSN to be determined as well. This should make synchronous
26771e330b5SDave Chinner  * operations almost as efficient as the old logging methods.
26871e330b5SDave Chinner  */
26971e330b5SDave Chinner struct xfs_cil {
270ad223e60SMark Tinguely 	struct xlog		*xc_log;
27188591e7fSDave Chinner 	unsigned long		xc_flags;
27231151cc3SDave Chinner 	atomic_t		xc_iclog_hdrs;
27333c0dd78SDave Chinner 	struct workqueue_struct	*xc_push_wq;
2744bb928cdSDave Chinner 
2754bb928cdSDave Chinner 	struct rw_semaphore	xc_ctx_lock ____cacheline_aligned_in_smp;
27671e330b5SDave Chinner 	struct xfs_cil_ctx	*xc_ctx;
2774bb928cdSDave Chinner 
2784bb928cdSDave Chinner 	spinlock_t		xc_push_lock ____cacheline_aligned_in_smp;
2795f9b4b0dSDave Chinner 	xfs_csn_t		xc_push_seq;
2800020a190SDave Chinner 	bool			xc_push_commit_stable;
28171e330b5SDave Chinner 	struct list_head	xc_committing;
282eb40a875SDave Chinner 	wait_queue_head_t	xc_commit_wait;
28368a74dcaSDave Chinner 	wait_queue_head_t	xc_start_wait;
2845f9b4b0dSDave Chinner 	xfs_csn_t		xc_current_sequence;
285c7f87f39SDave Chinner 	wait_queue_head_t	xc_push_wait;	/* background push throttle */
286af1c2146SDave Chinner 
287af1c2146SDave Chinner 	void __percpu		*xc_pcp;	/* percpu CIL structures */
2884bb928cdSDave Chinner } ____cacheline_aligned_in_smp;
28971e330b5SDave Chinner 
29088591e7fSDave Chinner /* xc_flags bit values */
29188591e7fSDave Chinner #define	XLOG_CIL_EMPTY		1
2927c8ade21SDave Chinner #define XLOG_CIL_PCP_SPACE	2
29388591e7fSDave Chinner 
29471e330b5SDave Chinner /*
29580168676SDave Chinner  * The amount of log space we allow the CIL to aggregate is difficult to size.
29680168676SDave Chinner  * Whatever we choose, we have to make sure we can get a reservation for the
29780168676SDave Chinner  * log space effectively, that it is large enough to capture sufficient
29880168676SDave Chinner  * relogging to reduce log buffer IO significantly, but it is not too large for
29980168676SDave Chinner  * the log or induces too much latency when writing out through the iclogs. We
30080168676SDave Chinner  * track both space consumed and the number of vectors in the checkpoint
30180168676SDave Chinner  * context, so we need to decide which to use for limiting.
302df806158SDave Chinner  *
303df806158SDave Chinner  * Every log buffer we write out during a push needs a header reserved, which
304df806158SDave Chinner  * is at least one sector and more for v2 logs. Hence we need a reservation of
305df806158SDave Chinner  * at least 512 bytes per 32k of log space just for the LR headers. That means
306df806158SDave Chinner  * 16KB of reservation per megabyte of delayed logging space we will consume,
307df806158SDave Chinner  * plus various headers.  The number of headers will vary based on the num of
308df806158SDave Chinner  * io vectors, so limiting on a specific number of vectors is going to result
309df806158SDave Chinner  * in transactions of varying size. IOWs, it is more consistent to track and
310df806158SDave Chinner  * limit space consumed in the log rather than by the number of objects being
311df806158SDave Chinner  * logged in order to prevent checkpoint ticket overruns.
312df806158SDave Chinner  *
313df806158SDave Chinner  * Further, use of static reservations through the log grant mechanism is
314df806158SDave Chinner  * problematic. It introduces a lot of complexity (e.g. reserve grant vs write
315df806158SDave Chinner  * grant) and a significant deadlock potential because regranting write space
316df806158SDave Chinner  * can block on log pushes. Hence if we have to regrant log space during a log
317df806158SDave Chinner  * push, we can deadlock.
318df806158SDave Chinner  *
319df806158SDave Chinner  * However, we can avoid this by use of a dynamic "reservation stealing"
320df806158SDave Chinner  * technique during transaction commit whereby unused reservation space in the
321df806158SDave Chinner  * transaction ticket is transferred to the CIL ctx commit ticket to cover the
322df806158SDave Chinner  * space needed by the checkpoint transaction. This means that we never need to
323df806158SDave Chinner  * specifically reserve space for the CIL checkpoint transaction, nor do we
324df806158SDave Chinner  * need to regrant space once the checkpoint completes. This also means the
325df806158SDave Chinner  * checkpoint transaction ticket is specific to the checkpoint context, rather
326df806158SDave Chinner  * than the CIL itself.
327df806158SDave Chinner  *
32880168676SDave Chinner  * With dynamic reservations, we can effectively make up arbitrary limits for
32980168676SDave Chinner  * the checkpoint size so long as they don't violate any other size rules.
33080168676SDave Chinner  * Recovery imposes a rule that no transaction exceed half the log, so we are
33180168676SDave Chinner  * limited by that.  Furthermore, the log transaction reservation subsystem
33280168676SDave Chinner  * tries to keep 25% of the log free, so we need to keep below that limit or we
33380168676SDave Chinner  * risk running out of free log space to start any new transactions.
33480168676SDave Chinner  *
335108a4235SDave Chinner  * In order to keep background CIL push efficient, we only need to ensure the
336108a4235SDave Chinner  * CIL is large enough to maintain sufficient in-memory relogging to avoid
337108a4235SDave Chinner  * repeated physical writes of frequently modified metadata. If we allow the CIL
338108a4235SDave Chinner  * to grow to a substantial fraction of the log, then we may be pinning hundreds
339108a4235SDave Chinner  * of megabytes of metadata in memory until the CIL flushes. This can cause
340108a4235SDave Chinner  * issues when we are running low on memory - pinned memory cannot be reclaimed,
341108a4235SDave Chinner  * and the CIL consumes a lot of memory. Hence we need to set an upper physical
342108a4235SDave Chinner  * size limit for the CIL that limits the maximum amount of memory pinned by the
343108a4235SDave Chinner  * CIL but does not limit performance by reducing relogging efficiency
344108a4235SDave Chinner  * significantly.
345108a4235SDave Chinner  *
346108a4235SDave Chinner  * As such, the CIL push threshold ends up being the smaller of two thresholds:
347108a4235SDave Chinner  * - a threshold large enough that it allows CIL to be pushed and progress to be
348108a4235SDave Chinner  *   made without excessive blocking of incoming transaction commits. This is
349108a4235SDave Chinner  *   defined to be 12.5% of the log space - half the 25% push threshold of the
350108a4235SDave Chinner  *   AIL.
351108a4235SDave Chinner  * - small enough that it doesn't pin excessive amounts of memory but maintains
352108a4235SDave Chinner  *   close to peak relogging efficiency. This is defined to be 16x the iclog
353108a4235SDave Chinner  *   buffer window (32MB) as measurements have shown this to be roughly the
354108a4235SDave Chinner  *   point of diminishing performance increases under highly concurrent
355108a4235SDave Chinner  *   modification workloads.
3560e7ab7efSDave Chinner  *
3570e7ab7efSDave Chinner  * To prevent the CIL from overflowing upper commit size bounds, we introduce a
3580e7ab7efSDave Chinner  * new threshold at which we block committing transactions until the background
3590e7ab7efSDave Chinner  * CIL commit commences and switches to a new context. While this is not a hard
3600e7ab7efSDave Chinner  * limit, it forces the process committing a transaction to the CIL to block and
3610e7ab7efSDave Chinner  * yeild the CPU, giving the CIL push work a chance to be scheduled and start
3620e7ab7efSDave Chinner  * work. This prevents a process running lots of transactions from overfilling
3630e7ab7efSDave Chinner  * the CIL because it is not yielding the CPU. We set the blocking limit at
3640e7ab7efSDave Chinner  * twice the background push space threshold so we keep in line with the AIL
3650e7ab7efSDave Chinner  * push thresholds.
3660e7ab7efSDave Chinner  *
3670e7ab7efSDave Chinner  * Note: this is not a -hard- limit as blocking is applied after the transaction
3680e7ab7efSDave Chinner  * is inserted into the CIL and the push has been triggered. It is largely a
3690e7ab7efSDave Chinner  * throttling mechanism that allows the CIL push to be scheduled and run. A hard
3700e7ab7efSDave Chinner  * limit will be difficult to implement without introducing global serialisation
3710e7ab7efSDave Chinner  * in the CIL commit fast path, and it's not at all clear that we actually need
3720e7ab7efSDave Chinner  * such hard limits given the ~7 years we've run without a hard limit before
3730e7ab7efSDave Chinner  * finding the first situation where a checkpoint size overflow actually
3740e7ab7efSDave Chinner  * occurred. Hence the simple throttle, and an ASSERT check to tell us that
3750e7ab7efSDave Chinner  * we've overrun the max size.
376df806158SDave Chinner  */
377108a4235SDave Chinner #define XLOG_CIL_SPACE_LIMIT(log)	\
378108a4235SDave Chinner 	min_t(int, (log)->l_logsize >> 3, BBTOB(XLOG_TOTAL_REC_SHIFT(log)) << 4)
379df806158SDave Chinner 
3800e7ab7efSDave Chinner #define XLOG_CIL_BLOCKING_SPACE_LIMIT(log)	\
3810e7ab7efSDave Chinner 	(XLOG_CIL_SPACE_LIMIT(log) * 2)
3820e7ab7efSDave Chinner 
383df806158SDave Chinner /*
38428496968SChristoph Hellwig  * ticket grant locks, queues and accounting have their own cachlines
38528496968SChristoph Hellwig  * as these are quite hot and can be operated on concurrently.
38628496968SChristoph Hellwig  */
38728496968SChristoph Hellwig struct xlog_grant_head {
38828496968SChristoph Hellwig 	spinlock_t		lock ____cacheline_aligned_in_smp;
38928496968SChristoph Hellwig 	struct list_head	waiters;
39028496968SChristoph Hellwig 	atomic64_t		grant;
39128496968SChristoph Hellwig };
39228496968SChristoph Hellwig 
39328496968SChristoph Hellwig /*
3941da177e4SLinus Torvalds  * The reservation head lsn is not made up of a cycle number and block number.
3951da177e4SLinus Torvalds  * Instead, it uses a cycle number and byte number.  Logs don't expect to
3961da177e4SLinus Torvalds  * overflow 31 bits worth of byte offset, so using a byte number will mean
3971da177e4SLinus Torvalds  * that round off problems won't occur when releasing partial reservations.
3981da177e4SLinus Torvalds  */
3999a8d2fdbSMark Tinguely struct xlog {
4004679b2d3SDavid Chinner 	/* The following fields don't need locking */
4014679b2d3SDavid Chinner 	struct xfs_mount	*l_mp;	        /* mount point */
402a9c21c1bSDavid Chinner 	struct xfs_ail		*l_ailp;	/* AIL log is working with */
40371e330b5SDave Chinner 	struct xfs_cil		*l_cilp;	/* CIL log is working with */
4044679b2d3SDavid Chinner 	struct xfs_buftarg	*l_targ;        /* buftarg of log */
4051058d0f5SChristoph Hellwig 	struct workqueue_struct	*l_ioend_workqueue; /* for I/O completions */
406f661f1e0SDave Chinner 	struct delayed_work	l_work;		/* background flush work */
407e1d06e5fSDave Chinner 	long			l_opstate;	/* operational state */
4084679b2d3SDavid Chinner 	uint			l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */
409d5689eaaSChristoph Hellwig 	struct list_head	*l_buf_cancel_table;
41003f7767cSDarrick J. Wong 	struct list_head	r_dfops;	/* recovered log intent items */
4114679b2d3SDavid Chinner 	int			l_iclog_hsize;  /* size of iclog header */
4124679b2d3SDavid Chinner 	int			l_iclog_heads;  /* # of iclog header sectors */
41348389ef1SAlex Elder 	uint			l_sectBBsize;   /* sector size in BBs (2^n) */
4144679b2d3SDavid Chinner 	int			l_iclog_size;	/* size of log in bytes */
4154679b2d3SDavid Chinner 	int			l_iclog_bufs;	/* number of iclog buffers */
4164679b2d3SDavid Chinner 	xfs_daddr_t		l_logBBstart;   /* start block of log */
4174679b2d3SDavid Chinner 	int			l_logsize;      /* size of log in bytes */
4184679b2d3SDavid Chinner 	int			l_logBBsize;    /* size of log in BB chunks */
4194679b2d3SDavid Chinner 
4201da177e4SLinus Torvalds 	/* The following block of fields are changed while holding icloglock */
421eb40a875SDave Chinner 	wait_queue_head_t	l_flush_wait ____cacheline_aligned_in_smp;
422d748c623SMatthew Wilcox 						/* waiting for iclog flush */
4231da177e4SLinus Torvalds 	int			l_covered_state;/* state of "covering disk
4241da177e4SLinus Torvalds 						 * log entries" */
4251da177e4SLinus Torvalds 	xlog_in_core_t		*l_iclog;       /* head log queue	*/
426b22cd72cSEric Sandeen 	spinlock_t		l_icloglock;    /* grab to change iclog state */
4271da177e4SLinus Torvalds 	int			l_curr_cycle;   /* Cycle number of log writes */
4281da177e4SLinus Torvalds 	int			l_prev_cycle;   /* Cycle number before last
4291da177e4SLinus Torvalds 						 * block increment */
4301da177e4SLinus Torvalds 	int			l_curr_block;   /* current logical log block */
4311da177e4SLinus Torvalds 	int			l_prev_block;   /* previous logical log block */
4321da177e4SLinus Torvalds 
43384f3c683SDave Chinner 	/*
4340dcd5a10SDave Chinner 	 * l_tail_lsn is atomic so it can be set and read without needing to
4350dcd5a10SDave Chinner 	 * hold specific locks. To avoid operations contending with other hot
4360dcd5a10SDave Chinner 	 * objects, it on a separate cacheline.
43784f3c683SDave Chinner 	 */
4381c3cb9ecSDave Chinner 	/* lsn of 1st LR with unflushed * buffers */
4391c3cb9ecSDave Chinner 	atomic64_t		l_tail_lsn ____cacheline_aligned_in_smp;
44084f3c683SDave Chinner 
44128496968SChristoph Hellwig 	struct xlog_grant_head	l_reserve_head;
44228496968SChristoph Hellwig 	struct xlog_grant_head	l_write_head;
443551bf13bSDave Chinner 	uint64_t		l_tail_space;
4443f16b985SDave Chinner 
445baff4e44SBrian Foster 	struct xfs_kobj		l_kobj;
446baff4e44SBrian Foster 
44712818d24SBrian Foster 	/* log recovery lsn tracking (for buffer submission */
44812818d24SBrian Foster 	xfs_lsn_t		l_recovery_lsn;
449a6a65fefSDave Chinner 
450a6a65fefSDave Chinner 	uint32_t		l_iclog_roundoff;/* padding roundoff */
4519a8d2fdbSMark Tinguely };
4521da177e4SLinus Torvalds 
453e1d06e5fSDave Chinner /*
454e1d06e5fSDave Chinner  * Bits for operational state
455e1d06e5fSDave Chinner  */
456e1d06e5fSDave Chinner #define XLOG_ACTIVE_RECOVERY	0	/* in the middle of recovery */
457e1d06e5fSDave Chinner #define XLOG_RECOVERY_NEEDED	1	/* log was recovered */
458e1d06e5fSDave Chinner #define XLOG_IO_ERROR		2	/* log hit an I/O error, and being
459e1d06e5fSDave Chinner 				   shutdown */
460e1d06e5fSDave Chinner #define XLOG_TAIL_WARN		3	/* log tail verify warning issued */
461e1d06e5fSDave Chinner 
462e1d06e5fSDave Chinner static inline bool
xlog_recovery_needed(struct xlog * log)463e1d06e5fSDave Chinner xlog_recovery_needed(struct xlog *log)
464e1d06e5fSDave Chinner {
465e1d06e5fSDave Chinner 	return test_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate);
466e1d06e5fSDave Chinner }
467e1d06e5fSDave Chinner 
468e1d06e5fSDave Chinner static inline bool
xlog_in_recovery(struct xlog * log)469e1d06e5fSDave Chinner xlog_in_recovery(struct xlog *log)
470e1d06e5fSDave Chinner {
471e1d06e5fSDave Chinner 	return test_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
472e1d06e5fSDave Chinner }
473e1d06e5fSDave Chinner 
4742039a272SDave Chinner static inline bool
xlog_is_shutdown(struct xlog * log)4752039a272SDave Chinner xlog_is_shutdown(struct xlog *log)
4762039a272SDave Chinner {
477e1d06e5fSDave Chinner 	return test_bit(XLOG_IO_ERROR, &log->l_opstate);
4782039a272SDave Chinner }
479cfcbbbd0SNathan Scott 
48041e63621SDave Chinner /*
48141e63621SDave Chinner  * Wait until the xlog_force_shutdown() has marked the log as shut down
48241e63621SDave Chinner  * so xlog_is_shutdown() will always return true.
48341e63621SDave Chinner  */
48441e63621SDave Chinner static inline void
xlog_shutdown_wait(struct xlog * log)48541e63621SDave Chinner xlog_shutdown_wait(
48641e63621SDave Chinner 	struct xlog	*log)
48741e63621SDave Chinner {
48841e63621SDave Chinner 	wait_var_event(&log->l_opstate, xlog_is_shutdown(log));
48941e63621SDave Chinner }
49041e63621SDave Chinner 
4911da177e4SLinus Torvalds /* common routines */
4929a8d2fdbSMark Tinguely extern int
4939a8d2fdbSMark Tinguely xlog_recover(
4949a8d2fdbSMark Tinguely 	struct xlog		*log);
4959a8d2fdbSMark Tinguely extern int
4969a8d2fdbSMark Tinguely xlog_recover_finish(
4979a8d2fdbSMark Tinguely 	struct xlog		*log);
498a7a9250eSHariprasad Kelam extern void
499f0b2efadSBrian Foster xlog_recover_cancel(struct xlog *);
5000e446be4SChristoph Hellwig 
501f9668a09SDave Chinner extern __le32	 xlog_cksum(struct xlog *log, struct xlog_rec_header *rhead,
5020e446be4SChristoph Hellwig 			    char *dp, int size);
5031da177e4SLinus Torvalds 
504182696fbSDarrick J. Wong extern struct kmem_cache *xfs_log_ticket_cache;
505c7610dceSDave Chinner struct xlog_ticket *xlog_ticket_alloc(struct xlog *log, int unit_bytes,
506c7610dceSDave Chinner 		int count, bool permanent);
507e6b1f273SChristoph Hellwig 
50871e330b5SDave Chinner void	xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket);
509d4ca1d55SBrian Foster void	xlog_print_trans(struct xfs_trans *);
510c45aba40SDave Chinner int	xlog_write(struct xlog *log, struct xfs_cil_ctx *ctx,
51116924853SDave Chinner 		struct list_head *lv_chain, struct xlog_ticket *tic,
51214b07ecdSDave Chinner 		uint32_t len);
5138b41e3f9SChristoph Hellwig void	xfs_log_ticket_ungrant(struct xlog *log, struct xlog_ticket *ticket);
5148b41e3f9SChristoph Hellwig void	xfs_log_ticket_regrant(struct xlog *log, struct xlog_ticket *ticket);
51571e330b5SDave Chinner 
5160020a190SDave Chinner void xlog_state_switch_iclogs(struct xlog *log, struct xlog_in_core *iclog,
5170020a190SDave Chinner 		int eventual_size);
518d9f68777SDave Chinner int xlog_state_release_iclog(struct xlog *log, struct xlog_in_core *iclog,
519d9f68777SDave Chinner 		struct xlog_ticket *ticket);
520eef983ffSDave Chinner 
52171e330b5SDave Chinner /*
5221c3cb9ecSDave Chinner  * When we crack an atomic LSN, we sample it first so that the value will not
5231c3cb9ecSDave Chinner  * change while we are cracking it into the component values. This means we
5241c3cb9ecSDave Chinner  * will always get consistent component values to work from. This should always
52525985edcSLucas De Marchi  * be used to sample and crack LSNs that are stored and updated in atomic
5261c3cb9ecSDave Chinner  * variables.
5271c3cb9ecSDave Chinner  */
5281c3cb9ecSDave Chinner static inline void
xlog_crack_atomic_lsn(atomic64_t * lsn,uint * cycle,uint * block)5291c3cb9ecSDave Chinner xlog_crack_atomic_lsn(atomic64_t *lsn, uint *cycle, uint *block)
5301c3cb9ecSDave Chinner {
5311c3cb9ecSDave Chinner 	xfs_lsn_t val = atomic64_read(lsn);
5321c3cb9ecSDave Chinner 
5331c3cb9ecSDave Chinner 	*cycle = CYCLE_LSN(val);
5341c3cb9ecSDave Chinner 	*block = BLOCK_LSN(val);
5351c3cb9ecSDave Chinner }
5361c3cb9ecSDave Chinner 
5371c3cb9ecSDave Chinner /*
5381c3cb9ecSDave Chinner  * Calculate and assign a value to an atomic LSN variable from component pieces.
5391c3cb9ecSDave Chinner  */
5401c3cb9ecSDave Chinner static inline void
xlog_assign_atomic_lsn(atomic64_t * lsn,uint cycle,uint block)5411c3cb9ecSDave Chinner xlog_assign_atomic_lsn(atomic64_t *lsn, uint cycle, uint block)
5421c3cb9ecSDave Chinner {
5431c3cb9ecSDave Chinner 	atomic64_set(lsn, xlog_assign_lsn(cycle, block));
5441c3cb9ecSDave Chinner }
5451c3cb9ecSDave Chinner 
5461c3cb9ecSDave Chinner /*
54771e330b5SDave Chinner  * Committed Item List interfaces
54871e330b5SDave Chinner  */
5492c6e24ceSDave Chinner int	xlog_cil_init(struct xlog *log);
5502c6e24ceSDave Chinner void	xlog_cil_init_post_recovery(struct xlog *log);
5512c6e24ceSDave Chinner void	xlog_cil_destroy(struct xlog *log);
5522c6e24ceSDave Chinner bool	xlog_cil_empty(struct xlog *log);
5535f9b4b0dSDave Chinner void	xlog_cil_commit(struct xlog *log, struct xfs_trans *tp,
5545f9b4b0dSDave Chinner 			xfs_csn_t *commit_seq, bool regrant);
555c45aba40SDave Chinner void	xlog_cil_set_ctx_write_state(struct xfs_cil_ctx *ctx,
556c45aba40SDave Chinner 			struct xlog_in_core *iclog);
557c45aba40SDave Chinner 
55871e330b5SDave Chinner 
559a44f13edSDave Chinner /*
560a44f13edSDave Chinner  * CIL force routines
561a44f13edSDave Chinner  */
5620020a190SDave Chinner void xlog_cil_flush(struct xlog *log);
5635f9b4b0dSDave Chinner xfs_lsn_t xlog_cil_force_seq(struct xlog *log, xfs_csn_t sequence);
564a44f13edSDave Chinner 
565a44f13edSDave Chinner static inline void
xlog_cil_force(struct xlog * log)566ad223e60SMark Tinguely xlog_cil_force(struct xlog *log)
567a44f13edSDave Chinner {
5685f9b4b0dSDave Chinner 	xlog_cil_force_seq(log, log->l_cilp->xc_current_sequence);
569a44f13edSDave Chinner }
57071e330b5SDave Chinner 
571955e47adSTim Shimmin /*
572eb40a875SDave Chinner  * Wrapper function for waiting on a wait queue serialised against wakeups
573eb40a875SDave Chinner  * by a spinlock. This matches the semantics of all the wait queues used in the
574eb40a875SDave Chinner  * log code.
575eb40a875SDave Chinner  */
576f7559793SDarrick J. Wong static inline void
xlog_wait(struct wait_queue_head * wq,struct spinlock * lock)577f7559793SDarrick J. Wong xlog_wait(
578f7559793SDarrick J. Wong 	struct wait_queue_head	*wq,
579f7559793SDarrick J. Wong 	struct spinlock		*lock)
580f7559793SDarrick J. Wong 		__releases(lock)
581eb40a875SDave Chinner {
582eb40a875SDave Chinner 	DECLARE_WAITQUEUE(wait, current);
583eb40a875SDave Chinner 
584eb40a875SDave Chinner 	add_wait_queue_exclusive(wq, &wait);
585eb40a875SDave Chinner 	__set_current_state(TASK_UNINTERRUPTIBLE);
586eb40a875SDave Chinner 	spin_unlock(lock);
587eb40a875SDave Chinner 	schedule();
588eb40a875SDave Chinner 	remove_wait_queue(wq, &wait);
589eb40a875SDave Chinner }
5901da177e4SLinus Torvalds 
5912c03d956SDave Chinner int xlog_wait_on_iclog(struct xlog_in_core *iclog)
5922c03d956SDave Chinner 		__releases(iclog->ic_log->l_icloglock);
593a79b28c2SDave Chinner 
594b50b4c49SDave Chinner /* Calculate the distance between two LSNs in bytes */
595b50b4c49SDave Chinner static inline uint64_t
xlog_lsn_sub(struct xlog * log,xfs_lsn_t high,xfs_lsn_t low)596b50b4c49SDave Chinner xlog_lsn_sub(
597b50b4c49SDave Chinner 	struct xlog	*log,
598b50b4c49SDave Chinner 	xfs_lsn_t	high,
599b50b4c49SDave Chinner 	xfs_lsn_t	low)
600b50b4c49SDave Chinner {
601b50b4c49SDave Chinner 	uint32_t	hi_cycle = CYCLE_LSN(high);
602b50b4c49SDave Chinner 	uint32_t	hi_block = BLOCK_LSN(high);
603b50b4c49SDave Chinner 	uint32_t	lo_cycle = CYCLE_LSN(low);
604b50b4c49SDave Chinner 	uint32_t	lo_block = BLOCK_LSN(low);
605b50b4c49SDave Chinner 
606b50b4c49SDave Chinner 	if (hi_cycle == lo_cycle)
607b50b4c49SDave Chinner 		return BBTOB(hi_block - lo_block);
608b50b4c49SDave Chinner 	ASSERT((hi_cycle == lo_cycle + 1) || xlog_is_shutdown(log));
609b50b4c49SDave Chinner 	return (uint64_t)log->l_logsize - BBTOB(lo_block - hi_block);
610b50b4c49SDave Chinner }
611b50b4c49SDave Chinner 
612*c1220522SDave Chinner void xlog_grant_return_space(struct xlog *log, xfs_lsn_t old_head,
613*c1220522SDave Chinner 		xfs_lsn_t new_head);
614*c1220522SDave Chinner 
615a45086e2SBrian Foster /*
616a45086e2SBrian Foster  * The LSN is valid so long as it is behind the current LSN. If it isn't, this
617a45086e2SBrian Foster  * means that the next log record that includes this metadata could have a
618a45086e2SBrian Foster  * smaller LSN. In turn, this means that the modification in the log would not
619a45086e2SBrian Foster  * replay.
620a45086e2SBrian Foster  */
621a45086e2SBrian Foster static inline bool
xlog_valid_lsn(struct xlog * log,xfs_lsn_t lsn)622a45086e2SBrian Foster xlog_valid_lsn(
623a45086e2SBrian Foster 	struct xlog	*log,
624a45086e2SBrian Foster 	xfs_lsn_t	lsn)
625a45086e2SBrian Foster {
626a45086e2SBrian Foster 	int		cur_cycle;
627a45086e2SBrian Foster 	int		cur_block;
628a45086e2SBrian Foster 	bool		valid = true;
629a45086e2SBrian Foster 
630a45086e2SBrian Foster 	/*
631a45086e2SBrian Foster 	 * First, sample the current lsn without locking to avoid added
632a45086e2SBrian Foster 	 * contention from metadata I/O. The current cycle and block are updated
633a45086e2SBrian Foster 	 * (in xlog_state_switch_iclogs()) and read here in a particular order
634a45086e2SBrian Foster 	 * to avoid false negatives (e.g., thinking the metadata LSN is valid
635a45086e2SBrian Foster 	 * when it is not).
636a45086e2SBrian Foster 	 *
637a45086e2SBrian Foster 	 * The current block is always rewound before the cycle is bumped in
638a45086e2SBrian Foster 	 * xlog_state_switch_iclogs() to ensure the current LSN is never seen in
639a45086e2SBrian Foster 	 * a transiently forward state. Instead, we can see the LSN in a
640a45086e2SBrian Foster 	 * transiently behind state if we happen to race with a cycle wrap.
641a45086e2SBrian Foster 	 */
6426aa7de05SMark Rutland 	cur_cycle = READ_ONCE(log->l_curr_cycle);
643a45086e2SBrian Foster 	smp_rmb();
6446aa7de05SMark Rutland 	cur_block = READ_ONCE(log->l_curr_block);
645a45086e2SBrian Foster 
646a45086e2SBrian Foster 	if ((CYCLE_LSN(lsn) > cur_cycle) ||
647a45086e2SBrian Foster 	    (CYCLE_LSN(lsn) == cur_cycle && BLOCK_LSN(lsn) > cur_block)) {
648a45086e2SBrian Foster 		/*
649a45086e2SBrian Foster 		 * If the metadata LSN appears invalid, it's possible the check
650a45086e2SBrian Foster 		 * above raced with a wrap to the next log cycle. Grab the lock
651a45086e2SBrian Foster 		 * to check for sure.
652a45086e2SBrian Foster 		 */
653a45086e2SBrian Foster 		spin_lock(&log->l_icloglock);
654a45086e2SBrian Foster 		cur_cycle = log->l_curr_cycle;
655a45086e2SBrian Foster 		cur_block = log->l_curr_block;
656a45086e2SBrian Foster 		spin_unlock(&log->l_icloglock);
657a45086e2SBrian Foster 
658a45086e2SBrian Foster 		if ((CYCLE_LSN(lsn) > cur_cycle) ||
659a45086e2SBrian Foster 		    (CYCLE_LSN(lsn) == cur_cycle && BLOCK_LSN(lsn) > cur_block))
660a45086e2SBrian Foster 			valid = false;
661a45086e2SBrian Foster 	}
662a45086e2SBrian Foster 
663a45086e2SBrian Foster 	return valid;
664a45086e2SBrian Foster }
665a45086e2SBrian Foster 
66645ff8b47SDave Chinner /*
66745ff8b47SDave Chinner  * Log vector and shadow buffers can be large, so we need to use kvmalloc() here
66845ff8b47SDave Chinner  * to ensure success. Unfortunately, kvmalloc() only allows GFP_KERNEL contexts
66945ff8b47SDave Chinner  * to fall back to vmalloc, so we can't actually do anything useful with gfp
67045ff8b47SDave Chinner  * flags to control the kmalloc() behaviour within kvmalloc(). Hence kmalloc()
67145ff8b47SDave Chinner  * will do direct reclaim and compaction in the slow path, both of which are
67245ff8b47SDave Chinner  * horrendously expensive. We just want kmalloc to fail fast and fall back to
6737d7c82a0SThorsten Blum  * vmalloc if it can't get something straight away from the free lists or
67445ff8b47SDave Chinner  * buddy allocator. Hence we have to open code kvmalloc outselves here.
67545ff8b47SDave Chinner  *
67645ff8b47SDave Chinner  * This assumes that the caller uses memalloc_nofs_save task context here, so
67745ff8b47SDave Chinner  * despite the use of GFP_KERNEL here, we are going to be doing GFP_NOFS
67845ff8b47SDave Chinner  * allocations. This is actually the only way to make vmalloc() do GFP_NOFS
67945ff8b47SDave Chinner  * allocations, so lets just all pretend this is a GFP_KERNEL context
68045ff8b47SDave Chinner  * operation....
68145ff8b47SDave Chinner  */
68245ff8b47SDave Chinner static inline void *
xlog_kvmalloc(size_t buf_size)68345ff8b47SDave Chinner xlog_kvmalloc(
68445ff8b47SDave Chinner 	size_t		buf_size)
68545ff8b47SDave Chinner {
68645ff8b47SDave Chinner 	gfp_t		flags = GFP_KERNEL;
68745ff8b47SDave Chinner 	void		*p;
68845ff8b47SDave Chinner 
68945ff8b47SDave Chinner 	flags &= ~__GFP_DIRECT_RECLAIM;
69045ff8b47SDave Chinner 	flags |= __GFP_NOWARN | __GFP_NORETRY;
69145ff8b47SDave Chinner 	do {
69245ff8b47SDave Chinner 		p = kmalloc(buf_size, flags);
69345ff8b47SDave Chinner 		if (!p)
69445ff8b47SDave Chinner 			p = vmalloc(buf_size);
69545ff8b47SDave Chinner 	} while (!p);
69645ff8b47SDave Chinner 
69745ff8b47SDave Chinner 	return p;
69845ff8b47SDave Chinner }
69945ff8b47SDave Chinner 
7001da177e4SLinus Torvalds #endif	/* __XFS_LOG_PRIV_H__ */
701