xref: /openbmc/linux/fs/ext4/ext4_jbd2.h (revision 83448bdf)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0+
23dcf5451SChristoph Hellwig /*
33dcf5451SChristoph Hellwig  * ext4_jbd2.h
43dcf5451SChristoph Hellwig  *
53dcf5451SChristoph Hellwig  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
63dcf5451SChristoph Hellwig  *
73dcf5451SChristoph Hellwig  * Copyright 1998--1999 Red Hat corp --- All Rights Reserved
83dcf5451SChristoph Hellwig  *
93dcf5451SChristoph Hellwig  * Ext4-specific journaling extensions.
103dcf5451SChristoph Hellwig  */
113dcf5451SChristoph Hellwig 
123dcf5451SChristoph Hellwig #ifndef _EXT4_JBD2_H
133dcf5451SChristoph Hellwig #define _EXT4_JBD2_H
143dcf5451SChristoph Hellwig 
153dcf5451SChristoph Hellwig #include <linux/fs.h>
163dcf5451SChristoph Hellwig #include <linux/jbd2.h>
173dcf5451SChristoph Hellwig #include "ext4.h"
183dcf5451SChristoph Hellwig 
193dcf5451SChristoph Hellwig #define EXT4_JOURNAL(inode)	(EXT4_SB((inode)->i_sb)->s_journal)
203dcf5451SChristoph Hellwig 
213dcf5451SChristoph Hellwig /* Define the number of blocks we need to account to a transaction to
223dcf5451SChristoph Hellwig  * modify one block of data.
233dcf5451SChristoph Hellwig  *
243dcf5451SChristoph Hellwig  * We may have to touch one inode, one bitmap buffer, up to three
253dcf5451SChristoph Hellwig  * indirection blocks, the group and superblock summaries, and the data
263dcf5451SChristoph Hellwig  * block to complete the transaction.
273dcf5451SChristoph Hellwig  *
283dcf5451SChristoph Hellwig  * For extents-enabled fs we may have to allocate and modify up to
29f45a5ef9SJan Kara  * 5 levels of tree, data block (for each of these we need bitmap + group
30f45a5ef9SJan Kara  * summaries), root which is stored in the inode, sb
31f45a5ef9SJan Kara  */
323dcf5451SChristoph Hellwig 
333dcf5451SChristoph Hellwig #define EXT4_SINGLEDATA_TRANS_BLOCKS(sb)				\
34e2b911c5SDarrick J. Wong 	(ext4_has_feature_extents(sb) ? 20U : 8U)
353dcf5451SChristoph Hellwig 
363dcf5451SChristoph Hellwig /* Extended attribute operations touch at most two data buffers,
373dcf5451SChristoph Hellwig  * two bitmap buffers, and two group summaries, in addition to the inode
383dcf5451SChristoph Hellwig  * and the superblock, which are already accounted for. */
393dcf5451SChristoph Hellwig 
403dcf5451SChristoph Hellwig #define EXT4_XATTR_TRANS_BLOCKS		6U
413dcf5451SChristoph Hellwig 
423dcf5451SChristoph Hellwig /* Define the minimum size for a transaction which modifies data.  This
433dcf5451SChristoph Hellwig  * needs to take into account the fact that we may end up modifying two
443dcf5451SChristoph Hellwig  * quota files too (one for the group, one for the user quota).  The
453dcf5451SChristoph Hellwig  * superblock only gets updated once, of course, so don't bother
463dcf5451SChristoph Hellwig  * counting that again for the quota updates. */
473dcf5451SChristoph Hellwig 
483dcf5451SChristoph Hellwig #define EXT4_DATA_TRANS_BLOCKS(sb)	(EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + \
493dcf5451SChristoph Hellwig 					 EXT4_XATTR_TRANS_BLOCKS - 2 + \
505aca07ebSDmitry Monakhov 					 EXT4_MAXQUOTAS_TRANS_BLOCKS(sb))
513dcf5451SChristoph Hellwig 
52a02908f1SMingming Cao /*
53a02908f1SMingming Cao  * Define the number of metadata blocks we need to account to modify data.
54a02908f1SMingming Cao  *
55a02908f1SMingming Cao  * This include super block, inode block, quota blocks and xattr blocks
56a02908f1SMingming Cao  */
57a02908f1SMingming Cao #define EXT4_META_TRANS_BLOCKS(sb)	(EXT4_XATTR_TRANS_BLOCKS + \
585aca07ebSDmitry Monakhov 					EXT4_MAXQUOTAS_TRANS_BLOCKS(sb))
59a02908f1SMingming Cao 
603dcf5451SChristoph Hellwig /* Define an arbitrary limit for the amount of data we will anticipate
613dcf5451SChristoph Hellwig  * writing to any given transaction.  For unbounded transactions such as
623dcf5451SChristoph Hellwig  * write(2) and truncate(2) we can write more than this, but we always
633dcf5451SChristoph Hellwig  * start off at the maximum transaction size and grow the transaction
643dcf5451SChristoph Hellwig  * optimistically as we go. */
653dcf5451SChristoph Hellwig 
663dcf5451SChristoph Hellwig #define EXT4_MAX_TRANS_DATA		64U
673dcf5451SChristoph Hellwig 
683dcf5451SChristoph Hellwig /* We break up a large truncate or write transaction once the handle's
693dcf5451SChristoph Hellwig  * buffer credits gets this low, we need either to extend the
703dcf5451SChristoph Hellwig  * transaction or to start a new one.  Reserve enough space here for
713dcf5451SChristoph Hellwig  * inode, bitmap, superblock, group and indirection updates for at least
723dcf5451SChristoph Hellwig  * one block, plus two quota updates.  Quota allocations are not
733dcf5451SChristoph Hellwig  * needed. */
743dcf5451SChristoph Hellwig 
753dcf5451SChristoph Hellwig #define EXT4_RESERVE_TRANS_BLOCKS	12U
763dcf5451SChristoph Hellwig 
77e08ac99fSArtem Blagodarenko /*
78e08ac99fSArtem Blagodarenko  * Number of credits needed if we need to insert an entry into a
79e08ac99fSArtem Blagodarenko  * directory.  For each new index block, we need 4 blocks (old index
80e08ac99fSArtem Blagodarenko  * block, new index block, bitmap block, bg summary).  For normal
81e08ac99fSArtem Blagodarenko  * htree directories there are 2 levels; if the largedir feature
82e08ac99fSArtem Blagodarenko  * enabled it's 3 levels.
83e08ac99fSArtem Blagodarenko  */
84e08ac99fSArtem Blagodarenko #define EXT4_INDEX_EXTRA_TRANS_BLOCKS	12U
853dcf5451SChristoph Hellwig 
863dcf5451SChristoph Hellwig #ifdef CONFIG_QUOTA
873dcf5451SChristoph Hellwig /* Amount of blocks needed for quota update - we know that the structure was
8821f97697SJan Kara  * allocated so we need to update only data block */
897c319d32SAditya Kali #define EXT4_QUOTA_TRANS_BLOCKS(sb) ((test_opt(sb, QUOTA) ||\
90e2b911c5SDarrick J. Wong 		ext4_has_feature_quota(sb)) ? 1 : 0)
913dcf5451SChristoph Hellwig /* Amount of blocks needed for quota insert/delete - we do some block writes
923dcf5451SChristoph Hellwig  * but inode, sb and group updates are done only once */
937c319d32SAditya Kali #define EXT4_QUOTA_INIT_BLOCKS(sb) ((test_opt(sb, QUOTA) ||\
94e2b911c5SDarrick J. Wong 		ext4_has_feature_quota(sb)) ?\
957c319d32SAditya Kali 		(DQUOT_INIT_ALLOC*(EXT4_SINGLEDATA_TRANS_BLOCKS(sb)-3)\
967c319d32SAditya Kali 		 +3+DQUOT_INIT_REWRITE) : 0)
975aca07ebSDmitry Monakhov 
987c319d32SAditya Kali #define EXT4_QUOTA_DEL_BLOCKS(sb) ((test_opt(sb, QUOTA) ||\
99e2b911c5SDarrick J. Wong 		ext4_has_feature_quota(sb)) ?\
1007c319d32SAditya Kali 		(DQUOT_DEL_ALLOC*(EXT4_SINGLEDATA_TRANS_BLOCKS(sb)-3)\
1017c319d32SAditya Kali 		 +3+DQUOT_DEL_REWRITE) : 0)
1023dcf5451SChristoph Hellwig #else
1033dcf5451SChristoph Hellwig #define EXT4_QUOTA_TRANS_BLOCKS(sb) 0
1043dcf5451SChristoph Hellwig #define EXT4_QUOTA_INIT_BLOCKS(sb) 0
1053dcf5451SChristoph Hellwig #define EXT4_QUOTA_DEL_BLOCKS(sb) 0
1063dcf5451SChristoph Hellwig #endif
107a2d4a646SJan Kara #define EXT4_MAXQUOTAS_TRANS_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_TRANS_BLOCKS(sb))
108a2d4a646SJan Kara #define EXT4_MAXQUOTAS_INIT_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_INIT_BLOCKS(sb))
109a2d4a646SJan Kara #define EXT4_MAXQUOTAS_DEL_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_DEL_BLOCKS(sb))
1103dcf5451SChristoph Hellwig 
1119924a92aSTheodore Ts'o /*
1129924a92aSTheodore Ts'o  * Ext4 handle operation types -- for logging purposes
1139924a92aSTheodore Ts'o  */
1149924a92aSTheodore Ts'o #define EXT4_HT_MISC             0
1159924a92aSTheodore Ts'o #define EXT4_HT_INODE            1
1169924a92aSTheodore Ts'o #define EXT4_HT_WRITE_PAGE       2
1179924a92aSTheodore Ts'o #define EXT4_HT_MAP_BLOCKS       3
1189924a92aSTheodore Ts'o #define EXT4_HT_DIR              4
1199924a92aSTheodore Ts'o #define EXT4_HT_TRUNCATE         5
1209924a92aSTheodore Ts'o #define EXT4_HT_QUOTA            6
1219924a92aSTheodore Ts'o #define EXT4_HT_RESIZE           7
1229924a92aSTheodore Ts'o #define EXT4_HT_MIGRATE          8
1239924a92aSTheodore Ts'o #define EXT4_HT_MOVE_EXTENTS     9
1249924a92aSTheodore Ts'o #define EXT4_HT_XATTR           10
1256b523df4SJan Kara #define EXT4_HT_EXT_CONVERT     11
1266b523df4SJan Kara #define EXT4_HT_MAX             12
1279924a92aSTheodore Ts'o 
12818aadd47SBobi Jam /**
12918aadd47SBobi Jam  *   struct ext4_journal_cb_entry - Base structure for callback information.
13018aadd47SBobi Jam  *
13118aadd47SBobi Jam  *   This struct is a 'seed' structure for a using with your own callback
13218aadd47SBobi Jam  *   structs. If you are using callbacks you must allocate one of these
13318aadd47SBobi Jam  *   or another struct of your own definition which has this struct
13418aadd47SBobi Jam  *   as it's first element and pass it to ext4_journal_callback_add().
13518aadd47SBobi Jam  */
13618aadd47SBobi Jam struct ext4_journal_cb_entry {
13718aadd47SBobi Jam 	/* list information for other callbacks attached to the same handle */
13818aadd47SBobi Jam 	struct list_head jce_list;
13918aadd47SBobi Jam 
14018aadd47SBobi Jam 	/*  Function to call with this callback structure */
14118aadd47SBobi Jam 	void (*jce_func)(struct super_block *sb,
14218aadd47SBobi Jam 			 struct ext4_journal_cb_entry *jce, int error);
14318aadd47SBobi Jam 
14418aadd47SBobi Jam 	/* user data goes here */
14518aadd47SBobi Jam };
14618aadd47SBobi Jam 
14718aadd47SBobi Jam /**
14818aadd47SBobi Jam  * ext4_journal_callback_add: add a function to call after transaction commit
14918aadd47SBobi Jam  * @handle: active journal transaction handle to register callback on
15018aadd47SBobi Jam  * @func: callback function to call after the transaction has committed:
15118aadd47SBobi Jam  *        @sb: superblock of current filesystem for transaction
15218aadd47SBobi Jam  *        @jce: returned journal callback data
15318aadd47SBobi Jam  *        @rc: journal state at commit (0 = transaction committed properly)
15418aadd47SBobi Jam  * @jce: journal callback data (internal and function private data struct)
15518aadd47SBobi Jam  *
15618aadd47SBobi Jam  * The registered function will be called in the context of the journal thread
15718aadd47SBobi Jam  * after the transaction for which the handle was created has completed.
15818aadd47SBobi Jam  *
15918aadd47SBobi Jam  * No locks are held when the callback function is called, so it is safe to
16018aadd47SBobi Jam  * call blocking functions from within the callback, but the callback should
16118aadd47SBobi Jam  * not block or run for too long, or the filesystem will be blocked waiting for
16218aadd47SBobi Jam  * the next transaction to commit. No journaling functions can be used, or
16318aadd47SBobi Jam  * there is a risk of deadlock.
16418aadd47SBobi Jam  *
16518aadd47SBobi Jam  * There is no guaranteed calling order of multiple registered callbacks on
16618aadd47SBobi Jam  * the same transaction.
16718aadd47SBobi Jam  */
168d08854f5STheodore Ts'o static inline void _ext4_journal_callback_add(handle_t *handle,
169d08854f5STheodore Ts'o 			struct ext4_journal_cb_entry *jce)
170d08854f5STheodore Ts'o {
171d08854f5STheodore Ts'o 	/* Add the jce to transaction's private list */
172d08854f5STheodore Ts'o 	list_add_tail(&jce->jce_list, &handle->h_transaction->t_private_list);
173d08854f5STheodore Ts'o }
174d08854f5STheodore Ts'o 
17518aadd47SBobi Jam static inline void ext4_journal_callback_add(handle_t *handle,
17618aadd47SBobi Jam 			void (*func)(struct super_block *sb,
17718aadd47SBobi Jam 				     struct ext4_journal_cb_entry *jce,
17818aadd47SBobi Jam 				     int rc),
17918aadd47SBobi Jam 			struct ext4_journal_cb_entry *jce)
18018aadd47SBobi Jam {
18118aadd47SBobi Jam 	struct ext4_sb_info *sbi =
18218aadd47SBobi Jam 			EXT4_SB(handle->h_transaction->t_journal->j_private);
18318aadd47SBobi Jam 
18418aadd47SBobi Jam 	/* Add the jce to transaction's private list */
18518aadd47SBobi Jam 	jce->jce_func = func;
18618aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
187d08854f5STheodore Ts'o 	_ext4_journal_callback_add(handle, jce);
18818aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
18918aadd47SBobi Jam }
19018aadd47SBobi Jam 
191d08854f5STheodore Ts'o 
19218aadd47SBobi Jam /**
19318aadd47SBobi Jam  * ext4_journal_callback_del: delete a registered callback
19418aadd47SBobi Jam  * @handle: active journal transaction handle on which callback was registered
19518aadd47SBobi Jam  * @jce: registered journal callback entry to unregister
19670261f56SAnatol Pomozov  * Return true if object was successfully removed
19718aadd47SBobi Jam  */
1985d3ee208SDmitry Monakhov static inline bool ext4_journal_callback_try_del(handle_t *handle,
19918aadd47SBobi Jam 					     struct ext4_journal_cb_entry *jce)
20018aadd47SBobi Jam {
2015d3ee208SDmitry Monakhov 	bool deleted;
20218aadd47SBobi Jam 	struct ext4_sb_info *sbi =
20318aadd47SBobi Jam 			EXT4_SB(handle->h_transaction->t_journal->j_private);
20418aadd47SBobi Jam 
20518aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
2065d3ee208SDmitry Monakhov 	deleted = !list_empty(&jce->jce_list);
20718aadd47SBobi Jam 	list_del_init(&jce->jce_list);
20818aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
2095d3ee208SDmitry Monakhov 	return deleted;
21018aadd47SBobi Jam }
21118aadd47SBobi Jam 
2123dcf5451SChristoph Hellwig int
2133dcf5451SChristoph Hellwig ext4_mark_iloc_dirty(handle_t *handle,
2143dcf5451SChristoph Hellwig 		     struct inode *inode,
2153dcf5451SChristoph Hellwig 		     struct ext4_iloc *iloc);
2163dcf5451SChristoph Hellwig 
2173dcf5451SChristoph Hellwig /*
2183dcf5451SChristoph Hellwig  * On success, We end up with an outstanding reference count against
2193dcf5451SChristoph Hellwig  * iloc->bh.  This _must_ be cleaned up later.
2203dcf5451SChristoph Hellwig  */
2213dcf5451SChristoph Hellwig 
2223dcf5451SChristoph Hellwig int ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
2233dcf5451SChristoph Hellwig 			struct ext4_iloc *iloc);
2243dcf5451SChristoph Hellwig 
2253dcf5451SChristoph Hellwig int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode);
2263dcf5451SChristoph Hellwig 
227c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode,
228c03b45b8SMiao Xie 			    unsigned int new_extra_isize,
229c03b45b8SMiao Xie 			    struct ext4_iloc *iloc);
2303dcf5451SChristoph Hellwig /*
231e4684b3fSTheodore Ts'o  * Wrapper functions with which ext4 calls into JBD.
2323dcf5451SChristoph Hellwig  */
23390c7201bSTheodore Ts'o int __ext4_journal_get_write_access(const char *where, unsigned int line,
23490c7201bSTheodore Ts'o 				    handle_t *handle, struct buffer_head *bh);
2353dcf5451SChristoph Hellwig 
23690c7201bSTheodore Ts'o int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
23790c7201bSTheodore Ts'o 		  int is_metadata, struct inode *inode,
23890c7201bSTheodore Ts'o 		  struct buffer_head *bh, ext4_fsblk_t blocknr);
23990c7201bSTheodore Ts'o 
24090c7201bSTheodore Ts'o int __ext4_journal_get_create_access(const char *where, unsigned int line,
24190c7201bSTheodore Ts'o 				handle_t *handle, struct buffer_head *bh);
24290c7201bSTheodore Ts'o 
24390c7201bSTheodore Ts'o int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
24490c7201bSTheodore Ts'o 				 handle_t *handle, struct inode *inode,
24590c7201bSTheodore Ts'o 				 struct buffer_head *bh);
24690c7201bSTheodore Ts'o 
24790c7201bSTheodore Ts'o int __ext4_handle_dirty_super(const char *where, unsigned int line,
248b50924c2SArtem Bityutskiy 			      handle_t *handle, struct super_block *sb);
249a0375156STheodore Ts'o 
2503dcf5451SChristoph Hellwig #define ext4_journal_get_write_access(handle, bh) \
25190c7201bSTheodore Ts'o 	__ext4_journal_get_write_access(__func__, __LINE__, (handle), (bh))
252d6797d14STheodore Ts'o #define ext4_forget(handle, is_metadata, inode, bh, block_nr) \
25390c7201bSTheodore Ts'o 	__ext4_forget(__func__, __LINE__, (handle), (is_metadata), (inode), \
25490c7201bSTheodore Ts'o 		      (bh), (block_nr))
2553dcf5451SChristoph Hellwig #define ext4_journal_get_create_access(handle, bh) \
25690c7201bSTheodore Ts'o 	__ext4_journal_get_create_access(__func__, __LINE__, (handle), (bh))
2570390131bSFrank Mayhar #define ext4_handle_dirty_metadata(handle, inode, bh) \
25890c7201bSTheodore Ts'o 	__ext4_handle_dirty_metadata(__func__, __LINE__, (handle), (inode), \
25990c7201bSTheodore Ts'o 				     (bh))
260a0375156STheodore Ts'o #define ext4_handle_dirty_super(handle, sb) \
261b50924c2SArtem Bityutskiy 	__ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb))
2623dcf5451SChristoph Hellwig 
2639924a92aSTheodore Ts'o handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
26483448bdfSJan Kara 				  int type, int blocks, int rsv_blocks,
26583448bdfSJan Kara 				  int revoke_creds);
266c398eda0STheodore Ts'o int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle);
2673dcf5451SChristoph Hellwig 
268d3d1faf6SCurt Wohlgemuth #define EXT4_NOJOURNAL_MAX_REF_COUNT ((unsigned long) 4096)
2690390131bSFrank Mayhar 
270d3d1faf6SCurt Wohlgemuth /* Note:  Do not use this for NULL handles.  This is only to determine if
271d3d1faf6SCurt Wohlgemuth  * a properly allocated handle is using a journal or not. */
2720390131bSFrank Mayhar static inline int ext4_handle_valid(handle_t *handle)
2730390131bSFrank Mayhar {
274d3d1faf6SCurt Wohlgemuth 	if ((unsigned long)handle < EXT4_NOJOURNAL_MAX_REF_COUNT)
2750390131bSFrank Mayhar 		return 0;
2760390131bSFrank Mayhar 	return 1;
2770390131bSFrank Mayhar }
2780390131bSFrank Mayhar 
2790390131bSFrank Mayhar static inline void ext4_handle_sync(handle_t *handle)
2800390131bSFrank Mayhar {
2810390131bSFrank Mayhar 	if (ext4_handle_valid(handle))
2820390131bSFrank Mayhar 		handle->h_sync = 1;
2830390131bSFrank Mayhar }
2840390131bSFrank Mayhar 
2850390131bSFrank Mayhar static inline int ext4_handle_is_aborted(handle_t *handle)
2860390131bSFrank Mayhar {
2870390131bSFrank Mayhar 	if (ext4_handle_valid(handle))
2880390131bSFrank Mayhar 		return is_handle_aborted(handle);
2890390131bSFrank Mayhar 	return 0;
2900390131bSFrank Mayhar }
2910390131bSFrank Mayhar 
29283448bdfSJan Kara static inline int ext4_free_metadata_revoke_credits(struct super_block *sb,
29383448bdfSJan Kara 						    int blocks)
29483448bdfSJan Kara {
29583448bdfSJan Kara 	/* Freeing each metadata block can result in freeing one cluster */
29683448bdfSJan Kara 	return blocks * EXT4_SB(sb)->s_cluster_ratio;
29783448bdfSJan Kara }
29883448bdfSJan Kara 
29983448bdfSJan Kara static inline int ext4_trans_default_revoke_credits(struct super_block *sb)
30083448bdfSJan Kara {
30183448bdfSJan Kara 	return ext4_free_metadata_revoke_credits(sb, 8);
30283448bdfSJan Kara }
30383448bdfSJan Kara 
3049924a92aSTheodore Ts'o #define ext4_journal_start_sb(sb, type, nblocks)			\
30583448bdfSJan Kara 	__ext4_journal_start_sb((sb), __LINE__, (type), (nblocks), 0,	\
30683448bdfSJan Kara 				ext4_trans_default_revoke_credits(sb))
3079924a92aSTheodore Ts'o 
3089924a92aSTheodore Ts'o #define ext4_journal_start(inode, type, nblocks)			\
30983448bdfSJan Kara 	__ext4_journal_start((inode), __LINE__, (type), (nblocks), 0,	\
31083448bdfSJan Kara 			     ext4_trans_default_revoke_credits((inode)->i_sb))
3115fe2fe89SJan Kara 
3125fe2fe89SJan Kara #define ext4_journal_start_with_reserve(inode, type, blocks, rsv_blocks)\
31383448bdfSJan Kara 	__ext4_journal_start((inode), __LINE__, (type), (blocks), (rsv_blocks),\
31483448bdfSJan Kara 			     ext4_trans_default_revoke_credits((inode)->i_sb))
31583448bdfSJan Kara 
31683448bdfSJan Kara #define ext4_journal_start_with_revoke(inode, type, blocks, revoke_creds) \
31783448bdfSJan Kara 	__ext4_journal_start((inode), __LINE__, (type), (blocks), 0,	\
31883448bdfSJan Kara 			     (revoke_creds))
3199924a92aSTheodore Ts'o 
3209924a92aSTheodore Ts'o static inline handle_t *__ext4_journal_start(struct inode *inode,
3219924a92aSTheodore Ts'o 					     unsigned int line, int type,
32283448bdfSJan Kara 					     int blocks, int rsv_blocks,
32383448bdfSJan Kara 					     int revoke_creds)
3243dcf5451SChristoph Hellwig {
3255fe2fe89SJan Kara 	return __ext4_journal_start_sb(inode->i_sb, line, type, blocks,
32683448bdfSJan Kara 				       rsv_blocks, revoke_creds);
3273dcf5451SChristoph Hellwig }
3283dcf5451SChristoph Hellwig 
3293dcf5451SChristoph Hellwig #define ext4_journal_stop(handle) \
330c398eda0STheodore Ts'o 	__ext4_journal_stop(__func__, __LINE__, (handle))
3313dcf5451SChristoph Hellwig 
3326b523df4SJan Kara #define ext4_journal_start_reserved(handle, type) \
3335fe2fe89SJan Kara 	__ext4_journal_start_reserved((handle), __LINE__, (type))
3345fe2fe89SJan Kara 
3355fe2fe89SJan Kara handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line,
3365fe2fe89SJan Kara 					int type);
3375fe2fe89SJan Kara 
3385fe2fe89SJan Kara static inline void ext4_journal_free_reserved(handle_t *handle)
3395fe2fe89SJan Kara {
3405fe2fe89SJan Kara 	if (ext4_handle_valid(handle))
3415fe2fe89SJan Kara 		jbd2_journal_free_reserved(handle);
3425fe2fe89SJan Kara }
3435fe2fe89SJan Kara 
3443dcf5451SChristoph Hellwig static inline handle_t *ext4_journal_current_handle(void)
3453dcf5451SChristoph Hellwig {
3463dcf5451SChristoph Hellwig 	return journal_current_handle();
3473dcf5451SChristoph Hellwig }
3483dcf5451SChristoph Hellwig 
34983448bdfSJan Kara static inline int ext4_journal_extend(handle_t *handle, int nblocks, int revoke)
3503dcf5451SChristoph Hellwig {
3510390131bSFrank Mayhar 	if (ext4_handle_valid(handle))
35283448bdfSJan Kara 		return jbd2_journal_extend(handle, nblocks, revoke);
3530390131bSFrank Mayhar 	return 0;
3543dcf5451SChristoph Hellwig }
3553dcf5451SChristoph Hellwig 
35683448bdfSJan Kara static inline int ext4_journal_restart(handle_t *handle, int nblocks,
35783448bdfSJan Kara 				       int revoke)
3583dcf5451SChristoph Hellwig {
3590390131bSFrank Mayhar 	if (ext4_handle_valid(handle))
36083448bdfSJan Kara 		return jbd2__journal_restart(handle, nblocks, revoke, GFP_NOFS);
3610390131bSFrank Mayhar 	return 0;
3623dcf5451SChristoph Hellwig }
3633dcf5451SChristoph Hellwig 
364a4130367SJan Kara int __ext4_journal_ensure_credits(handle_t *handle, int check_cred,
36583448bdfSJan Kara 				  int extend_cred, int revoke_cred);
366a4130367SJan Kara 
367a4130367SJan Kara 
368a4130367SJan Kara /*
369a4130367SJan Kara  * Ensure @handle has at least @check_creds credits available. If not,
370a4130367SJan Kara  * transaction will be extended or restarted to contain at least @extend_cred
371a4130367SJan Kara  * credits. Before restarting transaction @fn is executed to allow for cleanup
372a4130367SJan Kara  * before the transaction is restarted.
373a4130367SJan Kara  *
374a4130367SJan Kara  * The return value is < 0 in case of error, 0 in case the handle has enough
375a4130367SJan Kara  * credits or transaction extension succeeded, 1 in case transaction had to be
376a4130367SJan Kara  * restarted.
377a4130367SJan Kara  */
37883448bdfSJan Kara #define ext4_journal_ensure_credits_fn(handle, check_cred, extend_cred,	\
37983448bdfSJan Kara 				       revoke_cred, fn) \
380a4130367SJan Kara ({									\
381a4130367SJan Kara 	__label__ __ensure_end;						\
382a4130367SJan Kara 	int err = __ext4_journal_ensure_credits((handle), (check_cred),	\
38383448bdfSJan Kara 					(extend_cred), (revoke_cred));	\
384a4130367SJan Kara 									\
385a4130367SJan Kara 	if (err <= 0)							\
386a4130367SJan Kara 		goto __ensure_end;					\
387a4130367SJan Kara 	err = (fn);							\
388a4130367SJan Kara 	if (err < 0)							\
389a4130367SJan Kara 		goto __ensure_end;					\
39083448bdfSJan Kara 	err = ext4_journal_restart((handle), (extend_cred), (revoke_cred)); \
391a4130367SJan Kara 	if (err == 0)							\
392a4130367SJan Kara 		err = 1;						\
393a4130367SJan Kara __ensure_end:								\
394a4130367SJan Kara 	err;								\
395a4130367SJan Kara })
396a4130367SJan Kara 
397a4130367SJan Kara /*
398a4130367SJan Kara  * Ensure given handle has at least requested amount of credits available,
39983448bdfSJan Kara  * possibly restarting transaction if needed. We also make sure the transaction
40083448bdfSJan Kara  * has space for at least ext4_trans_default_revoke_credits(sb) revoke records
40183448bdfSJan Kara  * as freeing one or two blocks is very common pattern and requesting this is
40283448bdfSJan Kara  * very cheap.
403a4130367SJan Kara  */
40483448bdfSJan Kara static inline int ext4_journal_ensure_credits(handle_t *handle, int credits,
40583448bdfSJan Kara 					      int revoke_creds)
406a4130367SJan Kara {
40783448bdfSJan Kara 	return ext4_journal_ensure_credits_fn(handle, credits, credits,
40883448bdfSJan Kara 				revoke_creds, 0);
409a4130367SJan Kara }
410a4130367SJan Kara 
4113dcf5451SChristoph Hellwig static inline int ext4_journal_blocks_per_page(struct inode *inode)
4123dcf5451SChristoph Hellwig {
4130390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode) != NULL)
4143dcf5451SChristoph Hellwig 		return jbd2_journal_blocks_per_page(inode);
4150390131bSFrank Mayhar 	return 0;
4163dcf5451SChristoph Hellwig }
4173dcf5451SChristoph Hellwig 
4183dcf5451SChristoph Hellwig static inline int ext4_journal_force_commit(journal_t *journal)
4193dcf5451SChristoph Hellwig {
4200390131bSFrank Mayhar 	if (journal)
4213dcf5451SChristoph Hellwig 		return jbd2_journal_force_commit(journal);
4220390131bSFrank Mayhar 	return 0;
4233dcf5451SChristoph Hellwig }
4243dcf5451SChristoph Hellwig 
425ee0876bcSJan Kara static inline int ext4_jbd2_inode_add_write(handle_t *handle,
42673131fbbSRoss Zwisler 		struct inode *inode, loff_t start_byte, loff_t length)
427678aaf48SJan Kara {
4280390131bSFrank Mayhar 	if (ext4_handle_valid(handle))
42973131fbbSRoss Zwisler 		return jbd2_journal_inode_ranged_write(handle,
43073131fbbSRoss Zwisler 				EXT4_I(inode)->jinode, start_byte, length);
4310390131bSFrank Mayhar 	return 0;
432678aaf48SJan Kara }
433678aaf48SJan Kara 
434ee0876bcSJan Kara static inline int ext4_jbd2_inode_add_wait(handle_t *handle,
43573131fbbSRoss Zwisler 		struct inode *inode, loff_t start_byte, loff_t length)
436ee0876bcSJan Kara {
437ee0876bcSJan Kara 	if (ext4_handle_valid(handle))
43873131fbbSRoss Zwisler 		return jbd2_journal_inode_ranged_wait(handle,
43973131fbbSRoss Zwisler 				EXT4_I(inode)->jinode, start_byte, length);
440ee0876bcSJan Kara 	return 0;
441ee0876bcSJan Kara }
442ee0876bcSJan Kara 
443b436b9beSJan Kara static inline void ext4_update_inode_fsync_trans(handle_t *handle,
444b436b9beSJan Kara 						 struct inode *inode,
445b436b9beSJan Kara 						 int datasync)
446b436b9beSJan Kara {
447b436b9beSJan Kara 	struct ext4_inode_info *ei = EXT4_I(inode);
448b436b9beSJan Kara 
449fa30dde3SJiufei Xue 	if (ext4_handle_valid(handle) && !is_handle_aborted(handle)) {
450b436b9beSJan Kara 		ei->i_sync_tid = handle->h_transaction->t_tid;
451b436b9beSJan Kara 		if (datasync)
452b436b9beSJan Kara 			ei->i_datasync_tid = handle->h_transaction->t_tid;
453b436b9beSJan Kara 	}
454b436b9beSJan Kara }
455b436b9beSJan Kara 
4563dcf5451SChristoph Hellwig /* super.c */
4573dcf5451SChristoph Hellwig int ext4_force_commit(struct super_block *sb);
4583dcf5451SChristoph Hellwig 
4593d2b1582SLukas Czerner /*
4603d2b1582SLukas Czerner  * Ext4 inode journal modes
4613d2b1582SLukas Czerner  */
4623d2b1582SLukas Czerner #define EXT4_INODE_JOURNAL_DATA_MODE	0x01 /* journal data mode */
4633d2b1582SLukas Czerner #define EXT4_INODE_ORDERED_DATA_MODE	0x02 /* ordered data mode */
4643d2b1582SLukas Czerner #define EXT4_INODE_WRITEBACK_DATA_MODE	0x04 /* writeback data mode */
4653d2b1582SLukas Czerner 
4663d2b1582SLukas Czerner static inline int ext4_inode_journal_mode(struct inode *inode)
4673dcf5451SChristoph Hellwig {
4680390131bSFrank Mayhar 	if (EXT4_JOURNAL(inode) == NULL)
4693d2b1582SLukas Czerner 		return EXT4_INODE_WRITEBACK_DATA_MODE;	/* writeback */
4703d2b1582SLukas Czerner 	/* We do not support data journalling with delayed allocation */
4713d2b1582SLukas Czerner 	if (!S_ISREG(inode->i_mode) ||
472321238fbSJan Kara 	    ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE) ||
47373b92a2aSSergey Karamov 	    test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
47473b92a2aSSergey Karamov 	    (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) &&
47573b92a2aSSergey Karamov 	    !test_opt(inode->i_sb, DELALLOC))) {
47673b92a2aSSergey Karamov 		/* We do not support data journalling for encrypted data */
477592ddec7SChandan Rajendra 		if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
47873b92a2aSSergey Karamov 			return EXT4_INODE_ORDERED_DATA_MODE;  /* ordered */
4793d2b1582SLukas Czerner 		return EXT4_INODE_JOURNAL_DATA_MODE;	/* journal data */
48073b92a2aSSergey Karamov 	}
4813d2b1582SLukas Czerner 	if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
4823d2b1582SLukas Czerner 		return EXT4_INODE_ORDERED_DATA_MODE;	/* ordered */
4833d2b1582SLukas Czerner 	if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
4843d2b1582SLukas Czerner 		return EXT4_INODE_WRITEBACK_DATA_MODE;	/* writeback */
4853d2b1582SLukas Czerner 	BUG();
4863d2b1582SLukas Czerner }
4873d2b1582SLukas Czerner 
4883d2b1582SLukas Czerner static inline int ext4_should_journal_data(struct inode *inode)
4893d2b1582SLukas Czerner {
4903d2b1582SLukas Czerner 	return ext4_inode_journal_mode(inode) & EXT4_INODE_JOURNAL_DATA_MODE;
4913dcf5451SChristoph Hellwig }
4923dcf5451SChristoph Hellwig 
4933dcf5451SChristoph Hellwig static inline int ext4_should_order_data(struct inode *inode)
4943dcf5451SChristoph Hellwig {
4953d2b1582SLukas Czerner 	return ext4_inode_journal_mode(inode) & EXT4_INODE_ORDERED_DATA_MODE;
4963dcf5451SChristoph Hellwig }
4973dcf5451SChristoph Hellwig 
4983dcf5451SChristoph Hellwig static inline int ext4_should_writeback_data(struct inode *inode)
4993dcf5451SChristoph Hellwig {
5003d2b1582SLukas Czerner 	return ext4_inode_journal_mode(inode) & EXT4_INODE_WRITEBACK_DATA_MODE;
5013dcf5451SChristoph Hellwig }
5023dcf5451SChristoph Hellwig 
50383448bdfSJan Kara static inline int ext4_free_data_revoke_credits(struct inode *inode, int blocks)
50483448bdfSJan Kara {
50583448bdfSJan Kara 	if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
50683448bdfSJan Kara 		return 0;
50783448bdfSJan Kara 	if (!ext4_should_journal_data(inode))
50883448bdfSJan Kara 		return 0;
50983448bdfSJan Kara 	/*
51083448bdfSJan Kara 	 * Data blocks in one extent are contiguous, just account for partial
51183448bdfSJan Kara 	 * clusters at extent boundaries
51283448bdfSJan Kara 	 */
51383448bdfSJan Kara 	return blocks + 2*(EXT4_SB(inode->i_sb)->s_cluster_ratio - 1);
51483448bdfSJan Kara }
51583448bdfSJan Kara 
516744692dcSJiaying Zhang /*
517744692dcSJiaying Zhang  * This function controls whether or not we should try to go down the
518744692dcSJiaying Zhang  * dioread_nolock code paths, which makes it safe to avoid taking
519744692dcSJiaying Zhang  * i_mutex for direct I/O reads.  This only works for extent-based
520206f7ab4SChristoph Hellwig  * files, and it doesn't work if data journaling is enabled, since the
521206f7ab4SChristoph Hellwig  * dioread_nolock code uses b_private to pass information back to the
522206f7ab4SChristoph Hellwig  * I/O completion handler, and this conflicts with the jbd's use of
523206f7ab4SChristoph Hellwig  * b_private.
524744692dcSJiaying Zhang  */
525744692dcSJiaying Zhang static inline int ext4_should_dioread_nolock(struct inode *inode)
526744692dcSJiaying Zhang {
527744692dcSJiaying Zhang 	if (!test_opt(inode->i_sb, DIOREAD_NOLOCK))
528744692dcSJiaying Zhang 		return 0;
529744692dcSJiaying Zhang 	if (!S_ISREG(inode->i_mode))
530744692dcSJiaying Zhang 		return 0;
53112e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
532744692dcSJiaying Zhang 		return 0;
533744692dcSJiaying Zhang 	if (ext4_should_journal_data(inode))
534744692dcSJiaying Zhang 		return 0;
535744692dcSJiaying Zhang 	return 1;
536744692dcSJiaying Zhang }
537744692dcSJiaying Zhang 
5383dcf5451SChristoph Hellwig #endif	/* _EXT4_JBD2_H */
539